2 * ipcp.c - PPP IP Control Protocol.
4 * Copyright (c) 1989 Carnegie Mellon University.
7 * Redistribution and use in source and binary forms are permitted
8 * provided that the above copyright notice and this paragraph are
9 * duplicated in all such forms and that any documentation,
10 * advertising materials, and other materials related to such
11 * distribution and use acknowledge that the software was developed
12 * by Carnegie Mellon University. The name of the
13 * University may not be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
17 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
19 * $FreeBSD: src/usr.sbin/pppd/ipcp.c,v 1.12 1999/08/28 01:19:03 peter Exp $
20 * $DragonFly: src/usr.sbin/pppd/ipcp.c,v 1.3 2003/11/03 19:31:40 eirikn Exp $
31 #include <sys/param.h>
32 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
39 #include "pathnames.h"
42 ipcp_options ipcp_wantoptions[NUM_PPP]; /* Options that we want to request */
43 ipcp_options ipcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
44 ipcp_options ipcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
45 ipcp_options ipcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
48 static int cis_received[NUM_PPP]; /* # Conf-Reqs received */
49 static int default_route_set[NUM_PPP]; /* Have set up a default route */
50 static int proxy_arp_set[NUM_PPP]; /* Have created proxy arp entry */
53 * Callbacks for fsm code. (CI = Configuration Information)
55 static void ipcp_resetci(fsm *); /* Reset our CI */
56 static int ipcp_cilen(fsm *); /* Return length of our CI */
57 static void ipcp_addci(fsm *, u_char *, int *); /* Add our CI */
58 static int ipcp_ackci(fsm *, u_char *, int); /* Peer ack'd our CI */
59 static int ipcp_nakci(fsm *, u_char *, int); /* Peer nak'd our CI */
60 static int ipcp_rejci(fsm *, u_char *, int); /* Peer rej'd our CI */
61 static int ipcp_reqci(fsm *, u_char *, int *, int); /* Rcv CI */
62 static void ipcp_up(fsm *); /* We're UP */
63 static void ipcp_down(fsm *); /* We're DOWN */
64 static void ipcp_script(fsm *, char *); /* Run an up/down script */
65 static void ipcp_finished(fsm *); /* Don't need lower layer */
67 fsm ipcp_fsm[NUM_PPP]; /* IPCP fsm structure */
69 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
70 ipcp_resetci, /* Reset our Configuration Information */
71 ipcp_cilen, /* Length of our Configuration Information */
72 ipcp_addci, /* Add our Configuration Information */
73 ipcp_ackci, /* ACK our Configuration Information */
74 ipcp_nakci, /* NAK our Configuration Information */
75 ipcp_rejci, /* Reject our Configuration Information */
76 ipcp_reqci, /* Request peer's Configuration Information */
77 ipcp_up, /* Called when fsm reaches OPENED state */
78 ipcp_down, /* Called when fsm leaves OPENED state */
79 NULL, /* Called when we want the lower layer up */
80 ipcp_finished, /* Called when we want the lower layer down */
81 NULL, /* Called when Protocol-Reject received */
82 NULL, /* Retransmission is necessary */
83 NULL, /* Called to handle protocol-specific codes */
84 "IPCP" /* String name of protocol */
88 * Protocol entry points from main code.
90 static void ipcp_init(int);
91 static void ipcp_open(int);
92 static void ipcp_close(int, char *);
93 static void ipcp_lowerup(int);
94 static void ipcp_lowerdown(int);
95 static void ipcp_input(int, u_char *, int);
96 static void ipcp_protrej(int);
97 static int ipcp_printpkt(u_char *, int,
98 void (*)(void *, char *, ...), void *);
99 static void ip_check_options(void);
100 static int ip_demand_conf(int);
101 static int ip_active_pkt(u_char *, int);
103 struct protent ipcp_protent = {
121 static void ipcp_clear_addrs(int);
124 * Lengths of configuration options.
127 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
128 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
129 #define CILEN_ADDR 6 /* new-style single address option */
130 #define CILEN_ADDRS 10 /* old-style dual address option */
133 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
134 (x) == CONFNAK ? "NAK" : "REJ")
138 * Make a string representation of a network IP address.
146 ipaddr = ntohl(ipaddr);
148 sprintf(b, "%d.%d.%d.%d",
149 (u_char)(ipaddr >> 24),
150 (u_char)(ipaddr >> 16),
151 (u_char)(ipaddr >> 8),
158 * ipcp_init - Initialize IPCP.
164 fsm *f = &ipcp_fsm[unit];
165 ipcp_options *wo = &ipcp_wantoptions[unit];
166 ipcp_options *ao = &ipcp_allowoptions[unit];
169 f->protocol = PPP_IPCP;
170 f->callbacks = &ipcp_callbacks;
171 fsm_init(&ipcp_fsm[unit]);
173 memset(wo, 0, sizeof(*wo));
174 memset(ao, 0, sizeof(*ao));
178 wo->vj_protocol = IPCP_VJ_COMP;
179 wo->maxslotindex = MAX_STATES - 1; /* really max index */
182 /* max slots and slot-id compression are currently hardwired in */
183 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
188 ao->maxslotindex = MAX_STATES - 1;
192 * XXX These control whether the user may use the proxyarp
193 * and defaultroute options.
196 ao->default_route = 1;
201 * ipcp_open - IPCP is allowed to come up.
207 fsm_open(&ipcp_fsm[unit]);
212 * ipcp_close - Take IPCP down.
215 ipcp_close(unit, reason)
219 fsm_close(&ipcp_fsm[unit], reason);
224 * ipcp_lowerup - The lower layer is up.
230 fsm_lowerup(&ipcp_fsm[unit]);
235 * ipcp_lowerdown - The lower layer is down.
241 fsm_lowerdown(&ipcp_fsm[unit]);
246 * ipcp_input - Input IPCP packet.
249 ipcp_input(unit, p, len)
254 fsm_input(&ipcp_fsm[unit], p, len);
259 * ipcp_protrej - A Protocol-Reject was received for IPCP.
261 * Pretend the lower layer went down, so we shut up.
267 fsm_lowerdown(&ipcp_fsm[unit]);
272 * ipcp_resetci - Reset our CI.
278 ipcp_options *wo = &ipcp_wantoptions[f->unit];
280 wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
281 if (wo->ouraddr == 0)
282 wo->accept_local = 1;
283 if (wo->hisaddr == 0)
284 wo->accept_remote = 1;
285 ipcp_gotoptions[f->unit] = *wo;
286 cis_received[f->unit] = 0;
291 * ipcp_cilen - Return length of our CI.
297 ipcp_options *go = &ipcp_gotoptions[f->unit];
298 ipcp_options *wo = &ipcp_wantoptions[f->unit];
299 ipcp_options *ho = &ipcp_hisoptions[f->unit];
301 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
302 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
305 * First see if we want to change our options to the old
306 * forms because we have received old forms from the peer.
308 if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
309 /* use the old style of address negotiation */
313 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
314 /* try an older style of VJ negotiation */
315 if (cis_received[f->unit] == 0) {
316 /* keep trying the new style until we see some CI from the peer */
319 /* use the old style only if the peer did */
320 if (ho->neg_vj && ho->old_vj) {
323 go->vj_protocol = ho->vj_protocol;
328 return (LENCIADDR(go->neg_addr, go->old_addrs) +
329 LENCIVJ(go->neg_vj, go->old_vj));
334 * ipcp_addci - Add our desired CIs to a packet.
337 ipcp_addci(f, ucp, lenp)
342 ipcp_options *go = &ipcp_gotoptions[f->unit];
345 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
347 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
348 if (len >= vjlen) { \
350 PUTCHAR(vjlen, ucp); \
351 PUTSHORT(val, ucp); \
353 PUTCHAR(maxslotindex, ucp); \
354 PUTCHAR(cflag, ucp); \
361 #define ADDCIADDR(opt, neg, old, val1, val2) \
363 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
364 if (len >= addrlen) { \
367 PUTCHAR(addrlen, ucp); \
379 ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
380 go->old_addrs, go->ouraddr, go->hisaddr);
382 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
383 go->maxslotindex, go->cflag);
390 * ipcp_ackci - Ack our CIs.
397 ipcp_ackci(f, p, len)
402 ipcp_options *go = &ipcp_gotoptions[f->unit];
403 u_short cilen, citype, cishort;
405 u_char cimaxslotindex, cicflag;
408 * CIs must be in exactly the same order that we sent...
409 * Check packet length and CI length at each step.
410 * If we find any deviations, then this packet is bad.
413 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
415 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
416 if ((len -= vjlen) < 0) \
418 GETCHAR(citype, p); \
420 if (cilen != vjlen || \
423 GETSHORT(cishort, p); \
424 if (cishort != val) \
427 GETCHAR(cimaxslotindex, p); \
428 if (cimaxslotindex != maxslotindex) \
430 GETCHAR(cicflag, p); \
431 if (cicflag != cflag) \
436 #define ACKCIADDR(opt, neg, old, val1, val2) \
438 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
440 if ((len -= addrlen) < 0) \
442 GETCHAR(citype, p); \
444 if (cilen != addrlen || \
449 if (val1 != cilong) \
454 if (val2 != cilong) \
459 ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
460 go->old_addrs, go->ouraddr, go->hisaddr);
462 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
463 go->maxslotindex, go->cflag);
466 * If there are any remaining CIs, then this packet is bad.
473 IPCPDEBUG((LOG_INFO, "ipcp_ackci: received bad Ack!"));
478 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
479 * This should not modify any state if the Nak is bad
480 * or if IPCP is in the OPENED state.
487 ipcp_nakci(f, p, len)
492 ipcp_options *go = &ipcp_gotoptions[f->unit];
493 u_char cimaxslotindex, cicflag;
494 u_char citype, cilen, *next;
496 u_int32_t ciaddr1, ciaddr2, l;
497 ipcp_options no; /* options we've seen Naks for */
498 ipcp_options try; /* options to request next time */
500 BZERO(&no, sizeof(no));
504 * Any Nak'd CIs must be in exactly the same order that we sent.
505 * Check packet length and CI length at each step.
506 * If we find any deviations, then this packet is bad.
508 #define NAKCIADDR(opt, neg, old, code) \
510 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
516 ciaddr1 = htonl(l); \
519 ciaddr2 = htonl(l); \
527 #define NAKCIVJ(opt, neg, code) \
529 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
534 GETSHORT(cishort, p); \
540 * Accept the peer's idea of {our,his} address, if different
541 * from our idea, only if the accept_{local,remote} flag is set.
543 NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
544 if (go->accept_local && ciaddr1) { /* Do we know our address? */
545 try.ouraddr = ciaddr1;
546 IPCPDEBUG((LOG_INFO, "local IP address %s",
549 if (go->accept_remote && ciaddr2) { /* Does he know his? */
550 try.hisaddr = ciaddr2;
551 IPCPDEBUG((LOG_INFO, "remote IP address %s",
557 * Accept the peer's value of maxslotindex provided that it
558 * is less than what we asked for. Turn off slot-ID compression
559 * if the peer wants. Send old-style compress-type option if
562 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
563 if (cilen == CILEN_VJ) {
564 GETCHAR(cimaxslotindex, p);
566 if (cishort == IPCP_VJ_COMP) {
568 if (cimaxslotindex < go->maxslotindex)
569 try.maxslotindex = cimaxslotindex;
576 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
578 try.vj_protocol = cishort;
586 * There may be remaining CIs, if the peer is requesting negotiation
587 * on an option that we didn't include in our request packet.
588 * If they want to negotiate about IP addresses, we comply.
589 * If they want us to ask for compression, we refuse.
591 while (len > CILEN_VOID) {
594 if( (len -= cilen) < 0 )
596 next = p + cilen - 2;
599 case CI_COMPRESSTYPE:
600 if (go->neg_vj || no.neg_vj ||
601 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
606 if ((go->neg_addr && go->old_addrs) || no.old_addrs
607 || cilen != CILEN_ADDRS)
613 if (ciaddr1 && go->accept_local)
614 try.ouraddr = ciaddr1;
617 if (ciaddr2 && go->accept_remote)
618 try.hisaddr = ciaddr2;
622 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
627 if (ciaddr1 && go->accept_local)
628 try.ouraddr = ciaddr1;
629 if (try.ouraddr != 0)
637 /* If there is still anything left, this packet is bad. */
642 * OK, the Nak is good. Now we can update state.
644 if (f->state != OPENED)
650 IPCPDEBUG((LOG_INFO, "ipcp_nakci: received bad Nak!"));
656 * ipcp_rejci - Reject some of our CIs.
659 ipcp_rejci(f, p, len)
664 ipcp_options *go = &ipcp_gotoptions[f->unit];
665 u_char cimaxslotindex, ciflag, cilen;
668 ipcp_options try; /* options to request next time */
672 * Any Rejected CIs must be in exactly the same order that we sent.
673 * Check packet length and CI length at each step.
674 * If we find any deviations, then this packet is bad.
676 #define REJCIADDR(opt, neg, old, val1, val2) \
678 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
686 /* Check rejected value. */ \
687 if (cilong != val1) \
692 /* Check rejected value. */ \
693 if (cilong != val2) \
699 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
701 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
706 GETSHORT(cishort, p); \
707 /* Check rejected value. */ \
708 if (cishort != val) \
711 GETCHAR(cimaxslotindex, p); \
712 if (cimaxslotindex != maxslot) \
714 GETCHAR(ciflag, p); \
715 if (ciflag != cflag) \
721 REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
722 go->old_addrs, go->ouraddr, go->hisaddr);
724 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
725 go->maxslotindex, go->cflag);
728 * If there are any remaining CIs, then this packet is bad.
733 * Now we can update state.
735 if (f->state != OPENED)
740 IPCPDEBUG((LOG_INFO, "ipcp_rejci: received bad Reject!"));
746 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
748 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
749 * appropriately. If reject_if_disagree is non-zero, doesn't return
750 * CONFNAK; returns CONFREJ if it can't return CONFACK.
753 ipcp_reqci(f, inp, len, reject_if_disagree)
755 u_char *inp; /* Requested CIs */
756 int *len; /* Length of requested CIs */
757 int reject_if_disagree;
759 ipcp_options *wo = &ipcp_wantoptions[f->unit];
760 ipcp_options *ho = &ipcp_hisoptions[f->unit];
761 ipcp_options *ao = &ipcp_allowoptions[f->unit];
762 ipcp_options *go = &ipcp_gotoptions[f->unit];
763 u_char *cip, *next; /* Pointer to current and next CIs */
764 u_short cilen, citype; /* Parsed len, type */
765 u_short cishort; /* Parsed short value */
766 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
767 int rc = CONFACK; /* Final packet return code */
768 int orc; /* Individual option return code */
769 u_char *p; /* Pointer to next char to parse */
770 u_char *ucp = inp; /* Pointer to current output char */
771 int l = *len; /* Length left */
772 u_char maxslotindex, cflag;
775 cis_received[f->unit] = 1;
778 * Reset all his options.
780 BZERO(ho, sizeof(*ho));
783 * Process all his options.
787 orc = CONFACK; /* Assume success */
788 cip = p = next; /* Remember begining of CI */
789 if (l < 2 || /* Not enough data for CI header or */
790 p[1] < 2 || /* CI length too small or */
791 p[1] > l) { /* CI length too big? */
792 IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!"));
793 orc = CONFREJ; /* Reject bad CI */
794 cilen = l; /* Reject till end of packet */
795 l = 0; /* Don't loop again */
798 GETCHAR(citype, p); /* Parse CI type */
799 GETCHAR(cilen, p); /* Parse CI length */
800 l -= cilen; /* Adjust remaining length */
801 next += cilen; /* Step to next CI */
803 switch (citype) { /* Check CI type */
805 IPCPDEBUG((LOG_INFO, "ipcp: received ADDRS "));
807 cilen != CILEN_ADDRS) { /* Check CI length */
808 orc = CONFREJ; /* Reject CI */
813 * If he has no address, or if we both have his address but
814 * disagree about it, then NAK it with our idea.
815 * In particular, if we don't know his address, but he does,
818 GETLONG(tl, p); /* Parse source address (his) */
820 IPCPDEBUG((LOG_INFO, "(%s:", ip_ntoa(ciaddr1)));
821 if (ciaddr1 != wo->hisaddr
822 && (ciaddr1 == 0 || !wo->accept_remote)) {
824 if (!reject_if_disagree) {
825 DECPTR(sizeof(u_int32_t), p);
826 tl = ntohl(wo->hisaddr);
829 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
831 * If neither we nor he knows his address, reject the option.
834 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
839 * If he doesn't know our address, or if we both have our address
840 * but disagree about it, then NAK it with our idea.
842 GETLONG(tl, p); /* Parse desination address (ours) */
844 IPCPDEBUG((LOG_INFO, "%s)", ip_ntoa(ciaddr2)));
845 if (ciaddr2 != wo->ouraddr) {
846 if (ciaddr2 == 0 || !wo->accept_local) {
848 if (!reject_if_disagree) {
849 DECPTR(sizeof(u_int32_t), p);
850 tl = ntohl(wo->ouraddr);
854 go->ouraddr = ciaddr2; /* accept peer's idea */
860 ho->hisaddr = ciaddr1;
861 ho->ouraddr = ciaddr2;
865 IPCPDEBUG((LOG_INFO, "ipcp: received ADDR "));
868 cilen != CILEN_ADDR) { /* Check CI length */
869 orc = CONFREJ; /* Reject CI */
874 * If he has no address, or if we both have his address but
875 * disagree about it, then NAK it with our idea.
876 * In particular, if we don't know his address, but he does,
879 GETLONG(tl, p); /* Parse source address (his) */
881 IPCPDEBUG((LOG_INFO, "(%s)", ip_ntoa(ciaddr1)));
882 if (ciaddr1 != wo->hisaddr
883 && (ciaddr1 == 0 || !wo->accept_remote)) {
885 if (!reject_if_disagree) {
886 DECPTR(sizeof(u_int32_t), p);
887 tl = ntohl(wo->hisaddr);
890 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
892 * Don't ACK an address of 0.0.0.0 - reject it instead.
895 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
900 ho->hisaddr = ciaddr1;
905 /* Microsoft primary or secondary DNS request */
906 d = citype == CI_MS_DNS2;
907 IPCPDEBUG((LOG_INFO, "ipcp: received DNS%d Request ", d+1));
909 /* If we do not have a DNS address then we cannot send it */
910 if (ao->dnsaddr[d] == 0 ||
911 cilen != CILEN_ADDR) { /* Check CI length */
912 orc = CONFREJ; /* Reject CI */
916 if (htonl(tl) != ao->dnsaddr[d]) {
917 DECPTR(sizeof(u_int32_t), p);
918 tl = ntohl(ao->dnsaddr[d]);
926 /* Microsoft primary or secondary WINS request */
927 d = citype == CI_MS_WINS2;
928 IPCPDEBUG((LOG_INFO, "ipcp: received WINS%d Request ", d+1));
930 /* If we do not have a DNS address then we cannot send it */
931 if (ao->winsaddr[d] == 0 ||
932 cilen != CILEN_ADDR) { /* Check CI length */
933 orc = CONFREJ; /* Reject CI */
937 if (htonl(tl) != ao->winsaddr[d]) {
938 DECPTR(sizeof(u_int32_t), p);
939 tl = ntohl(ao->winsaddr[d]);
945 case CI_COMPRESSTYPE:
946 IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
948 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
952 GETSHORT(cishort, p);
953 IPCPDEBUG((LOG_INFO, "(%d)", cishort));
955 if (!(cishort == IPCP_VJ_COMP ||
956 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
962 ho->vj_protocol = cishort;
963 if (cilen == CILEN_VJ) {
964 GETCHAR(maxslotindex, p);
965 if (maxslotindex > ao->maxslotindex) {
967 if (!reject_if_disagree){
969 PUTCHAR(ao->maxslotindex, p);
973 if (cflag && !ao->cflag) {
975 if (!reject_if_disagree){
977 PUTCHAR(wo->cflag, p);
980 ho->maxslotindex = maxslotindex;
984 ho->maxslotindex = MAX_STATES - 1;
995 IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
997 if (orc == CONFACK && /* Good CI */
998 rc != CONFACK) /* but prior CI wasnt? */
999 continue; /* Don't send this one */
1001 if (orc == CONFNAK) { /* Nak this CI? */
1002 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1003 orc = CONFREJ; /* Get tough if so */
1005 if (rc == CONFREJ) /* Rejecting prior CI? */
1006 continue; /* Don't send this one */
1007 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1008 rc = CONFNAK; /* Not anymore... */
1009 ucp = inp; /* Backup */
1014 if (orc == CONFREJ && /* Reject this CI */
1015 rc != CONFREJ) { /* but no prior ones? */
1017 ucp = inp; /* Backup */
1020 /* Need to move CI? */
1022 BCOPY(cip, ucp, cilen); /* Move it */
1024 /* Update output pointer */
1029 * If we aren't rejecting this packet, and we want to negotiate
1030 * their address, and they didn't send their address, then we
1031 * send a NAK with a CI_ADDR option appended. We assume the
1032 * input buffer is long enough that we can append the extra
1035 if (rc != CONFREJ && !ho->neg_addr &&
1036 wo->req_addr && !reject_if_disagree) {
1037 if (rc == CONFACK) {
1039 ucp = inp; /* reset pointer */
1040 wo->req_addr = 0; /* don't ask again */
1042 PUTCHAR(CI_ADDR, ucp);
1043 PUTCHAR(CILEN_ADDR, ucp);
1044 tl = ntohl(wo->hisaddr);
1048 *len = ucp - inp; /* Compute output length */
1049 IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
1050 return (rc); /* Return final code */
1055 * ip_check_options - check that any IP-related options are OK,
1056 * and assign appropriate defaults.
1063 ipcp_options *wo = &ipcp_wantoptions[0];
1066 * Default our local IP address based on our hostname.
1067 * If local IP address already given, don't bother.
1069 if (wo->ouraddr == 0 && !disable_defaultip) {
1071 * Look up our hostname (possibly with domain name appended)
1072 * and take the first IP address as our local IP address.
1073 * If there isn't an IP address for our hostname, too bad.
1075 wo->accept_local = 1; /* don't insist on this default value */
1076 if ((hp = gethostbyname(hostname)) != NULL) {
1077 local = *(u_int32_t *)hp->h_addr;
1078 if (local != 0 && !bad_ip_adrs(local))
1079 wo->ouraddr = local;
1083 if (demand && wo->hisaddr == 0) {
1084 option_error("remote IP address required for demand-dialling\n");
1088 if (demand && wo->accept_remote) {
1089 option_error("ipcp-accept-remote is incompatible with demand\n");
1097 * ip_demand_conf - configure the interface as though
1098 * IPCP were up, for use with dial-on-demand.
1104 ipcp_options *wo = &ipcp_wantoptions[u];
1106 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1110 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1112 if (wo->default_route)
1113 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1114 default_route_set[u] = 1;
1116 if (sifproxyarp(u, wo->hisaddr))
1117 proxy_arp_set[u] = 1;
1119 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(wo->ouraddr));
1120 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(wo->hisaddr));
1127 * ipcp_up - IPCP has come UP.
1129 * Configure the IP network interface appropriately and bring it up.
1136 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1137 ipcp_options *go = &ipcp_gotoptions[f->unit];
1138 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1140 np_up(f->unit, PPP_IP);
1141 IPCPDEBUG((LOG_INFO, "ipcp: up"));
1144 * We must have a non-zero IP address for both ends of the link.
1147 ho->hisaddr = wo->hisaddr;
1149 if (ho->hisaddr == 0) {
1150 syslog(LOG_ERR, "Could not determine remote IP address");
1151 ipcp_close(f->unit, "Could not determine remote IP address");
1154 if (go->ouraddr == 0) {
1155 syslog(LOG_ERR, "Could not determine local IP address");
1156 ipcp_close(f->unit, "Could not determine local IP address");
1159 script_setenv("IPLOCAL", ip_ntoa(go->ouraddr));
1160 script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr));
1163 * Check that the peer is allowed to use the IP address it wants.
1165 if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1166 syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
1167 ip_ntoa(ho->hisaddr));
1168 ipcp_close(f->unit, "Unauthorized remote IP address");
1172 /* set tcp compression */
1173 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1176 * If we are doing dial-on-demand, the interface is already
1177 * configured, so we put out any saved-up packets, then set the
1178 * interface to pass IP packets.
1181 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1182 if (go->ouraddr != wo->ouraddr)
1183 syslog(LOG_WARNING, "Local IP address changed to %s",
1184 ip_ntoa(go->ouraddr));
1185 if (ho->hisaddr != wo->hisaddr)
1186 syslog(LOG_WARNING, "Remote IP address changed to %s",
1187 ip_ntoa(ho->hisaddr));
1188 ipcp_clear_addrs(f->unit);
1190 /* Set the interface to the new addresses */
1191 mask = GetMask(go->ouraddr);
1192 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1193 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1194 ipcp_close(f->unit, "Interface configuration failed");
1198 /* assign a default route through the interface if required */
1199 if (ipcp_wantoptions[f->unit].default_route)
1200 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1201 default_route_set[f->unit] = 1;
1203 /* Make a proxy ARP entry if requested. */
1204 if (ipcp_wantoptions[f->unit].proxy_arp)
1205 if (sifproxyarp(f->unit, ho->hisaddr))
1206 proxy_arp_set[f->unit] = 1;
1209 demand_rexmit(PPP_IP);
1210 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1214 * Set IP addresses and (if specified) netmask.
1216 mask = GetMask(go->ouraddr);
1218 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1219 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1220 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1221 ipcp_close(f->unit, "Interface configuration failed");
1226 /* bring the interface up for IP */
1227 if (!sifup(f->unit)) {
1228 IPCPDEBUG((LOG_WARNING, "sifup failed"));
1229 ipcp_close(f->unit, "Interface configuration failed");
1233 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1234 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1235 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1236 ipcp_close(f->unit, "Interface configuration failed");
1240 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1242 /* assign a default route through the interface if required */
1243 if (ipcp_wantoptions[f->unit].default_route)
1244 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1245 default_route_set[f->unit] = 1;
1247 /* Make a proxy ARP entry if requested. */
1248 if (ipcp_wantoptions[f->unit].proxy_arp)
1249 if (sifproxyarp(f->unit, ho->hisaddr))
1250 proxy_arp_set[f->unit] = 1;
1252 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(go->ouraddr));
1253 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1257 * Execute the ip-up script, like this:
1258 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1260 ipcp_script(f, _PATH_IPUP);
1266 * ipcp_down - IPCP has gone DOWN.
1268 * Take the IP network interface down, clear its addresses
1269 * and delete routes through it.
1275 IPCPDEBUG((LOG_INFO, "ipcp: down"));
1276 np_down(f->unit, PPP_IP);
1277 sifvjcomp(f->unit, 0, 0, 0);
1280 * If we are doing dial-on-demand, set the interface
1281 * to queue up outgoing packets (for now).
1284 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1287 ipcp_clear_addrs(f->unit);
1290 /* Execute the ip-down script */
1291 ipcp_script(f, _PATH_IPDOWN);
1296 * ipcp_clear_addrs() - clear the interface addresses, routes,
1297 * proxy arp entries, etc.
1300 ipcp_clear_addrs(unit)
1303 u_int32_t ouraddr, hisaddr;
1305 ouraddr = ipcp_gotoptions[unit].ouraddr;
1306 hisaddr = ipcp_hisoptions[unit].hisaddr;
1307 if (proxy_arp_set[unit]) {
1308 cifproxyarp(unit, hisaddr);
1309 proxy_arp_set[unit] = 0;
1311 if (default_route_set[unit]) {
1312 cifdefaultroute(unit, ouraddr, hisaddr);
1313 default_route_set[unit] = 0;
1315 cifaddr(unit, ouraddr, hisaddr);
1320 * ipcp_finished - possibly shut down the lower layers.
1326 np_finished(f->unit, PPP_IP);
1331 * ipcp_script - Execute a script with arguments
1332 * interface-name tty-name speed local-IP remote-IP.
1335 ipcp_script(f, script)
1339 char strspeed[32], strlocal[32], strremote[32];
1342 sprintf(strspeed, "%d", baud_rate);
1343 strcpy(strlocal, ip_ntoa(ipcp_gotoptions[f->unit].ouraddr));
1344 strcpy(strremote, ip_ntoa(ipcp_hisoptions[f->unit].hisaddr));
1351 argv[5] = strremote;
1354 run_program(script, argv, 0);
1358 * ipcp_printpkt - print the contents of an IPCP packet.
1360 static char *ipcp_codenames[] = {
1361 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1362 "TermReq", "TermAck", "CodeRej"
1366 ipcp_printpkt(p, plen, printer, arg)
1369 void (*printer)(void *, char *, ...);
1372 int code, id, len, olen;
1373 u_char *pstart, *optend;
1377 if (plen < HEADERLEN)
1383 if (len < HEADERLEN || len > plen)
1386 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1387 printer(arg, " %s", ipcp_codenames[code-1]);
1389 printer(arg, " code=0x%x", code);
1390 printer(arg, " id=0x%x", id);
1397 /* print option list */
1402 if (olen < 2 || olen > len) {
1410 if (olen == CILEN_ADDRS) {
1413 printer(arg, "addrs %I", htonl(cilong));
1415 printer(arg, " %I", htonl(cilong));
1418 case CI_COMPRESSTYPE:
1419 if (olen >= CILEN_COMPRESS) {
1421 GETSHORT(cishort, p);
1422 printer(arg, "compress ");
1427 case IPCP_VJ_COMP_OLD:
1428 printer(arg, "old-VJ");
1431 printer(arg, "0x%x", cishort);
1436 if (olen == CILEN_ADDR) {
1439 printer(arg, "addr %I", htonl(cilong));
1446 printer(arg, "ms-dns %I", htonl(cilong));
1452 printer(arg, "ms-wins %I", htonl(cilong));
1455 while (p < optend) {
1457 printer(arg, " %.2x", code);
1465 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1467 print_string(p, len, printer, arg);
1474 /* print the rest of the bytes in the packet */
1475 for (; len > 0; --len) {
1477 printer(arg, " %.2x", code);
1484 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
1485 * We don't bring the link up for IP fragments or for TCP FIN packets
1488 #define IP_HDRLEN 20 /* bytes */
1489 #define IP_OFFMASK 0x1fff
1490 #define IPPROTO_TCP 6
1491 #define TCP_HDRLEN 20
1495 * We use these macros because the IP header may be at an odd address,
1496 * and some compilers might use word loads to get th_off or ip_hl.
1499 #define net_short(x) (((x)[0] << 8) + (x)[1])
1500 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
1501 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
1502 #define get_ipproto(x) (((unsigned char *)(x))[9])
1503 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
1504 #define get_tcpflags(x) (((unsigned char *)(x))[13])
1507 ip_active_pkt(pkt, len)
1516 if (len < IP_HDRLEN)
1518 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
1520 if (get_ipproto(pkt) != IPPROTO_TCP)
1522 hlen = get_iphl(pkt) * 4;
1523 if (len < hlen + TCP_HDRLEN)
1526 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)