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.
21 static char rcsid[] = "$Id: ipcp.c,v 1.35 1998/11/07 06:59:26 paulus Exp $";
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 #include <netinet/in.h>
36 #include <arpa/inet.h>
41 #include "pathnames.h"
44 ipcp_options ipcp_wantoptions[NUM_PPP]; /* Options that we want to request */
45 ipcp_options ipcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
46 ipcp_options ipcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
47 ipcp_options ipcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
49 bool disable_defaultip = 0; /* Don't use hostname for default IP adrs */
52 static int cis_received[NUM_PPP]; /* # Conf-Reqs received */
53 static int default_route_set[NUM_PPP]; /* Have set up a default route */
54 static int proxy_arp_set[NUM_PPP]; /* Have created proxy arp entry */
57 * Callbacks for fsm code. (CI = Configuration Information)
59 static void ipcp_resetci __P((fsm *)); /* Reset our CI */
60 static int ipcp_cilen __P((fsm *)); /* Return length of our CI */
61 static void ipcp_addci __P((fsm *, u_char *, int *)); /* Add our CI */
62 static int ipcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
63 static int ipcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
64 static int ipcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
65 static int ipcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv CI */
66 static void ipcp_up __P((fsm *)); /* We're UP */
67 static void ipcp_down __P((fsm *)); /* We're DOWN */
68 static void ipcp_finished __P((fsm *)); /* Don't need lower layer */
70 fsm ipcp_fsm[NUM_PPP]; /* IPCP fsm structure */
72 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
73 ipcp_resetci, /* Reset our Configuration Information */
74 ipcp_cilen, /* Length of our Configuration Information */
75 ipcp_addci, /* Add our Configuration Information */
76 ipcp_ackci, /* ACK our Configuration Information */
77 ipcp_nakci, /* NAK our Configuration Information */
78 ipcp_rejci, /* Reject our Configuration Information */
79 ipcp_reqci, /* Request peer's Configuration Information */
80 ipcp_up, /* Called when fsm reaches OPENED state */
81 ipcp_down, /* Called when fsm leaves OPENED state */
82 NULL, /* Called when we want the lower layer up */
83 ipcp_finished, /* Called when we want the lower layer down */
84 NULL, /* Called when Protocol-Reject received */
85 NULL, /* Retransmission is necessary */
86 NULL, /* Called to handle protocol-specific codes */
87 "IPCP" /* String name of protocol */
91 * Command-line options.
93 static int setvjslots __P((char **));
94 static int setdnsaddr __P((char **));
95 static int setwinsaddr __P((char **));
97 static option_t ipcp_option_list[] = {
98 { "noip", o_bool, &ipcp_protent.enabled_flag,
99 "Disable IP and IPCP" },
100 { "-ip", o_bool, &ipcp_protent.enabled_flag,
101 "Disable IP and IPCP" },
102 { "novj", o_bool, &ipcp_wantoptions[0].neg_vj,
103 "Disable VJ compression", OPT_A2COPY, &ipcp_allowoptions[0].neg_vj },
104 { "-vj", o_bool, &ipcp_wantoptions[0].neg_vj,
105 "Disable VJ compression", OPT_A2COPY, &ipcp_allowoptions[0].neg_vj },
106 { "novjccomp", o_bool, &ipcp_wantoptions[0].cflag,
107 "Disable VJ connection-ID compression", OPT_A2COPY,
108 &ipcp_allowoptions[0].cflag },
109 { "-vjccomp", o_bool, &ipcp_wantoptions[0].cflag,
110 "Disable VJ connection-ID compression", OPT_A2COPY,
111 &ipcp_allowoptions[0].cflag },
112 { "vj-max-slots", 1, setvjslots,
113 "Set maximum VJ header slots" },
114 { "ipcp-accept-local", o_bool, &ipcp_wantoptions[0].accept_local,
115 "Accept peer's address for us", 1 },
116 { "ipcp-accept-remote", o_bool, &ipcp_wantoptions[0].accept_remote,
117 "Accept peer's address for it", 1 },
118 { "ipparam", o_string, &ipparam,
119 "Set ip script parameter" },
120 { "noipdefault", o_bool, &disable_defaultip,
121 "Don't use name for default IP adrs", 1 },
122 { "ms-dns", 1, setdnsaddr,
123 "DNS address for the peer's use" },
124 { "ms-wins", 1, setwinsaddr,
125 "Nameserver for SMB over TCP/IP for peer" },
126 { "ipcp-restart", o_int, &ipcp_fsm[0].timeouttime,
127 "Set timeout for IPCP" },
128 { "ipcp-max-terminate", o_int, &ipcp_fsm[0].maxtermtransmits,
129 "Set max #xmits for term-reqs" },
130 { "ipcp-max-configure", o_int, &ipcp_fsm[0].maxconfreqtransmits,
131 "Set max #xmits for conf-reqs" },
132 { "ipcp-max-failure", o_int, &ipcp_fsm[0].maxnakloops,
133 "Set max #conf-naks for IPCP" },
134 { "defaultroute", o_bool, &ipcp_wantoptions[0].default_route,
135 "Add default route", OPT_ENABLE|1, &ipcp_allowoptions[0].default_route },
136 { "nodefaultroute", o_bool, &ipcp_allowoptions[0].default_route,
137 "disable defaultroute option", OPT_A2COPY,
138 &ipcp_wantoptions[0].default_route },
139 { "-defaultroute", o_bool, &ipcp_allowoptions[0].default_route,
140 "disable defaultroute option", OPT_A2COPY,
141 &ipcp_wantoptions[0].default_route },
142 { "proxyarp", o_bool, &ipcp_wantoptions[0].proxy_arp,
143 "Add proxy ARP entry", OPT_ENABLE|1, &ipcp_allowoptions[0].proxy_arp },
144 { "noproxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
145 "disable proxyarp option", OPT_A2COPY,
146 &ipcp_wantoptions[0].proxy_arp },
147 { "-proxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
148 "disable proxyarp option", OPT_A2COPY,
149 &ipcp_wantoptions[0].proxy_arp },
154 * Protocol entry points from main code.
156 static void ipcp_init __P((int));
157 static void ipcp_open __P((int));
158 static void ipcp_close __P((int, char *));
159 static void ipcp_lowerup __P((int));
160 static void ipcp_lowerdown __P((int));
161 static void ipcp_input __P((int, u_char *, int));
162 static void ipcp_protrej __P((int));
163 static int ipcp_printpkt __P((u_char *, int,
164 void (*) __P((void *, char *, ...)), void *));
165 static void ip_check_options __P((void));
166 static int ip_demand_conf __P((int));
167 static int ip_active_pkt __P((u_char *, int));
169 struct protent ipcp_protent = {
188 static void ipcp_clear_addrs __P((int));
189 static void ipcp_script __P((char *)); /* Run an up/down script */
190 static void ipcp_script_done __P((void *));
193 * Lengths of configuration options.
196 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
197 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
198 #define CILEN_ADDR 6 /* new-style single address option */
199 #define CILEN_ADDRS 10 /* old-style dual address option */
202 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
203 (x) == CONFNAK ? "NAK" : "REJ")
206 * This state variable is used to ensure that we don't
207 * run an ipcp-up/down script while one is already running.
209 static enum script_state {
213 static pid_t ipcp_script_pid;
216 * Make a string representation of a network IP address.
224 ipaddr = ntohl(ipaddr);
226 sprintf(b, "%d.%d.%d.%d",
227 (u_char)(ipaddr >> 24),
228 (u_char)(ipaddr >> 16),
229 (u_char)(ipaddr >> 8),
239 * setvjslots - set maximum number of connection slots for VJ compression
247 if (!int_option(*argv, &value))
249 if (value < 2 || value > 16) {
250 option_error("vj-max-slots value must be between 2 and 16");
253 ipcp_wantoptions [0].maxslotindex =
254 ipcp_allowoptions[0].maxslotindex = value - 1;
259 * setdnsaddr - set the dns address(es)
268 dns = inet_addr(*argv);
270 if ((hp = gethostbyname(*argv)) == NULL) {
271 option_error("invalid address parameter '%s' for ms-dns option",
275 dns = *(u_int32_t *)hp->h_addr;
278 /* if there is no primary then update it. */
279 if (ipcp_allowoptions[0].dnsaddr[0] == 0)
280 ipcp_allowoptions[0].dnsaddr[0] = dns;
282 /* always set the secondary address value to the same value. */
283 ipcp_allowoptions[0].dnsaddr[1] = dns;
289 * setwinsaddr - set the wins address(es)
290 * This is primrarly used with the Samba package under UNIX or for pointing
291 * the caller to the existing WINS server on a Windows NT platform.
300 wins = inet_addr(*argv);
302 if ((hp = gethostbyname(*argv)) == NULL) {
303 option_error("invalid address parameter '%s' for ms-wins option",
307 wins = *(u_int32_t *)hp->h_addr;
310 /* if there is no primary then update it. */
311 if (ipcp_allowoptions[0].winsaddr[0] == 0)
312 ipcp_allowoptions[0].winsaddr[0] = wins;
314 /* always set the secondary address value to the same value. */
315 ipcp_allowoptions[0].winsaddr[1] = wins;
322 * ipcp_init - Initialize IPCP.
328 fsm *f = &ipcp_fsm[unit];
329 ipcp_options *wo = &ipcp_wantoptions[unit];
330 ipcp_options *ao = &ipcp_allowoptions[unit];
333 f->protocol = PPP_IPCP;
334 f->callbacks = &ipcp_callbacks;
335 fsm_init(&ipcp_fsm[unit]);
337 memset(wo, 0, sizeof(*wo));
338 memset(ao, 0, sizeof(*ao));
342 wo->vj_protocol = IPCP_VJ_COMP;
343 wo->maxslotindex = MAX_STATES - 1; /* really max index */
346 /* max slots and slot-id compression are currently hardwired in */
347 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
352 ao->maxslotindex = MAX_STATES - 1;
356 * XXX These control whether the user may use the proxyarp
357 * and defaultroute options.
360 ao->default_route = 1;
365 * ipcp_open - IPCP is allowed to come up.
371 fsm_open(&ipcp_fsm[unit]);
376 * ipcp_close - Take IPCP down.
379 ipcp_close(unit, reason)
383 fsm_close(&ipcp_fsm[unit], reason);
388 * ipcp_lowerup - The lower layer is up.
394 fsm_lowerup(&ipcp_fsm[unit]);
399 * ipcp_lowerdown - The lower layer is down.
405 fsm_lowerdown(&ipcp_fsm[unit]);
410 * ipcp_input - Input IPCP packet.
413 ipcp_input(unit, p, len)
418 fsm_input(&ipcp_fsm[unit], p, len);
423 * ipcp_protrej - A Protocol-Reject was received for IPCP.
425 * Pretend the lower layer went down, so we shut up.
431 fsm_lowerdown(&ipcp_fsm[unit]);
436 * ipcp_resetci - Reset our CI.
442 ipcp_options *wo = &ipcp_wantoptions[f->unit];
444 wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
445 if (wo->ouraddr == 0)
446 wo->accept_local = 1;
447 if (wo->hisaddr == 0)
448 wo->accept_remote = 1;
449 ipcp_gotoptions[f->unit] = *wo;
450 cis_received[f->unit] = 0;
455 * ipcp_cilen - Return length of our CI.
461 ipcp_options *go = &ipcp_gotoptions[f->unit];
462 ipcp_options *wo = &ipcp_wantoptions[f->unit];
463 ipcp_options *ho = &ipcp_hisoptions[f->unit];
465 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
466 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
469 * First see if we want to change our options to the old
470 * forms because we have received old forms from the peer.
472 if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
473 /* use the old style of address negotiation */
477 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
478 /* try an older style of VJ negotiation */
479 if (cis_received[f->unit] == 0) {
480 /* keep trying the new style until we see some CI from the peer */
483 /* use the old style only if the peer did */
484 if (ho->neg_vj && ho->old_vj) {
487 go->vj_protocol = ho->vj_protocol;
492 return (LENCIADDR(go->neg_addr, go->old_addrs) +
493 LENCIVJ(go->neg_vj, go->old_vj));
498 * ipcp_addci - Add our desired CIs to a packet.
501 ipcp_addci(f, ucp, lenp)
506 ipcp_options *go = &ipcp_gotoptions[f->unit];
509 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
511 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
512 if (len >= vjlen) { \
514 PUTCHAR(vjlen, ucp); \
515 PUTSHORT(val, ucp); \
517 PUTCHAR(maxslotindex, ucp); \
518 PUTCHAR(cflag, ucp); \
525 #define ADDCIADDR(opt, neg, old, val1, val2) \
527 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
528 if (len >= addrlen) { \
531 PUTCHAR(addrlen, ucp); \
543 ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
544 go->old_addrs, go->ouraddr, go->hisaddr);
546 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
547 go->maxslotindex, go->cflag);
554 * ipcp_ackci - Ack our CIs.
561 ipcp_ackci(f, p, len)
566 ipcp_options *go = &ipcp_gotoptions[f->unit];
567 u_short cilen, citype, cishort;
569 u_char cimaxslotindex, cicflag;
572 * CIs must be in exactly the same order that we sent...
573 * Check packet length and CI length at each step.
574 * If we find any deviations, then this packet is bad.
577 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
579 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
580 if ((len -= vjlen) < 0) \
582 GETCHAR(citype, p); \
584 if (cilen != vjlen || \
587 GETSHORT(cishort, p); \
588 if (cishort != val) \
591 GETCHAR(cimaxslotindex, p); \
592 if (cimaxslotindex != maxslotindex) \
594 GETCHAR(cicflag, p); \
595 if (cicflag != cflag) \
600 #define ACKCIADDR(opt, neg, old, val1, val2) \
602 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
604 if ((len -= addrlen) < 0) \
606 GETCHAR(citype, p); \
608 if (cilen != addrlen || \
613 if (val1 != cilong) \
618 if (val2 != cilong) \
623 ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
624 go->old_addrs, go->ouraddr, go->hisaddr);
626 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
627 go->maxslotindex, go->cflag);
630 * If there are any remaining CIs, then this packet is bad.
637 IPCPDEBUG((LOG_INFO, "ipcp_ackci: received bad Ack!"));
642 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
643 * This should not modify any state if the Nak is bad
644 * or if IPCP is in the OPENED state.
651 ipcp_nakci(f, p, len)
656 ipcp_options *go = &ipcp_gotoptions[f->unit];
657 u_char cimaxslotindex, cicflag;
658 u_char citype, cilen, *next;
660 u_int32_t ciaddr1, ciaddr2, l;
661 ipcp_options no; /* options we've seen Naks for */
662 ipcp_options try; /* options to request next time */
664 BZERO(&no, sizeof(no));
668 * Any Nak'd CIs must be in exactly the same order that we sent.
669 * Check packet length and CI length at each step.
670 * If we find any deviations, then this packet is bad.
672 #define NAKCIADDR(opt, neg, old, code) \
674 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
680 ciaddr1 = htonl(l); \
683 ciaddr2 = htonl(l); \
691 #define NAKCIVJ(opt, neg, code) \
693 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
698 GETSHORT(cishort, p); \
704 * Accept the peer's idea of {our,his} address, if different
705 * from our idea, only if the accept_{local,remote} flag is set.
707 NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
708 if (go->accept_local && ciaddr1) { /* Do we know our address? */
709 try.ouraddr = ciaddr1;
710 IPCPDEBUG((LOG_INFO, "local IP address %s",
713 if (go->accept_remote && ciaddr2) { /* Does he know his? */
714 try.hisaddr = ciaddr2;
715 IPCPDEBUG((LOG_INFO, "remote IP address %s",
721 * Accept the peer's value of maxslotindex provided that it
722 * is less than what we asked for. Turn off slot-ID compression
723 * if the peer wants. Send old-style compress-type option if
726 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
727 if (cilen == CILEN_VJ) {
728 GETCHAR(cimaxslotindex, p);
730 if (cishort == IPCP_VJ_COMP) {
732 if (cimaxslotindex < go->maxslotindex)
733 try.maxslotindex = cimaxslotindex;
740 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
742 try.vj_protocol = cishort;
750 * There may be remaining CIs, if the peer is requesting negotiation
751 * on an option that we didn't include in our request packet.
752 * If they want to negotiate about IP addresses, we comply.
753 * If they want us to ask for compression, we refuse.
755 while (len > CILEN_VOID) {
758 if( (len -= cilen) < 0 )
760 next = p + cilen - 2;
763 case CI_COMPRESSTYPE:
764 if (go->neg_vj || no.neg_vj ||
765 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
770 if ((go->neg_addr && go->old_addrs) || no.old_addrs
771 || cilen != CILEN_ADDRS)
777 if (ciaddr1 && go->accept_local)
778 try.ouraddr = ciaddr1;
781 if (ciaddr2 && go->accept_remote)
782 try.hisaddr = ciaddr2;
786 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
791 if (ciaddr1 && go->accept_local)
792 try.ouraddr = ciaddr1;
793 if (try.ouraddr != 0)
801 /* If there is still anything left, this packet is bad. */
806 * OK, the Nak is good. Now we can update state.
808 if (f->state != OPENED)
814 IPCPDEBUG((LOG_INFO, "ipcp_nakci: received bad Nak!"));
820 * ipcp_rejci - Reject some of our CIs.
823 ipcp_rejci(f, p, len)
828 ipcp_options *go = &ipcp_gotoptions[f->unit];
829 u_char cimaxslotindex, ciflag, cilen;
832 ipcp_options try; /* options to request next time */
836 * Any Rejected CIs must be in exactly the same order that we sent.
837 * Check packet length and CI length at each step.
838 * If we find any deviations, then this packet is bad.
840 #define REJCIADDR(opt, neg, old, val1, val2) \
842 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
850 /* Check rejected value. */ \
851 if (cilong != val1) \
856 /* Check rejected value. */ \
857 if (cilong != val2) \
863 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
865 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
870 GETSHORT(cishort, p); \
871 /* Check rejected value. */ \
872 if (cishort != val) \
875 GETCHAR(cimaxslotindex, p); \
876 if (cimaxslotindex != maxslot) \
878 GETCHAR(ciflag, p); \
879 if (ciflag != cflag) \
885 REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
886 go->old_addrs, go->ouraddr, go->hisaddr);
888 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
889 go->maxslotindex, go->cflag);
892 * If there are any remaining CIs, then this packet is bad.
897 * Now we can update state.
899 if (f->state != OPENED)
904 IPCPDEBUG((LOG_INFO, "ipcp_rejci: received bad Reject!"));
910 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
912 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
913 * appropriately. If reject_if_disagree is non-zero, doesn't return
914 * CONFNAK; returns CONFREJ if it can't return CONFACK.
917 ipcp_reqci(f, inp, len, reject_if_disagree)
919 u_char *inp; /* Requested CIs */
920 int *len; /* Length of requested CIs */
921 int reject_if_disagree;
923 ipcp_options *wo = &ipcp_wantoptions[f->unit];
924 ipcp_options *ho = &ipcp_hisoptions[f->unit];
925 ipcp_options *ao = &ipcp_allowoptions[f->unit];
926 ipcp_options *go = &ipcp_gotoptions[f->unit];
927 u_char *cip, *next; /* Pointer to current and next CIs */
928 u_short cilen, citype; /* Parsed len, type */
929 u_short cishort; /* Parsed short value */
930 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
931 int rc = CONFACK; /* Final packet return code */
932 int orc; /* Individual option return code */
933 u_char *p; /* Pointer to next char to parse */
934 u_char *ucp = inp; /* Pointer to current output char */
935 int l = *len; /* Length left */
936 u_char maxslotindex, cflag;
939 cis_received[f->unit] = 1;
942 * Reset all his options.
944 BZERO(ho, sizeof(*ho));
947 * Process all his options.
951 orc = CONFACK; /* Assume success */
952 cip = p = next; /* Remember begining of CI */
953 if (l < 2 || /* Not enough data for CI header or */
954 p[1] < 2 || /* CI length too small or */
955 p[1] > l) { /* CI length too big? */
956 IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!"));
957 orc = CONFREJ; /* Reject bad CI */
958 cilen = l; /* Reject till end of packet */
959 l = 0; /* Don't loop again */
962 GETCHAR(citype, p); /* Parse CI type */
963 GETCHAR(cilen, p); /* Parse CI length */
964 l -= cilen; /* Adjust remaining length */
965 next += cilen; /* Step to next CI */
967 switch (citype) { /* Check CI type */
969 IPCPDEBUG((LOG_INFO, "ipcp: received ADDRS "));
971 cilen != CILEN_ADDRS) { /* Check CI length */
972 orc = CONFREJ; /* Reject CI */
977 * If he has no address, or if we both have his address but
978 * disagree about it, then NAK it with our idea.
979 * In particular, if we don't know his address, but he does,
982 GETLONG(tl, p); /* Parse source address (his) */
984 IPCPDEBUG((LOG_INFO, "(%s:", ip_ntoa(ciaddr1)));
985 if (ciaddr1 != wo->hisaddr
986 && (ciaddr1 == 0 || !wo->accept_remote)) {
988 if (!reject_if_disagree) {
989 DECPTR(sizeof(u_int32_t), p);
990 tl = ntohl(wo->hisaddr);
993 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
995 * If neither we nor he knows his address, reject the option.
998 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1003 * If he doesn't know our address, or if we both have our address
1004 * but disagree about it, then NAK it with our idea.
1006 GETLONG(tl, p); /* Parse desination address (ours) */
1007 ciaddr2 = htonl(tl);
1008 IPCPDEBUG((LOG_INFO, "%s)", ip_ntoa(ciaddr2)));
1009 if (ciaddr2 != wo->ouraddr) {
1010 if (ciaddr2 == 0 || !wo->accept_local) {
1012 if (!reject_if_disagree) {
1013 DECPTR(sizeof(u_int32_t), p);
1014 tl = ntohl(wo->ouraddr);
1018 go->ouraddr = ciaddr2; /* accept peer's idea */
1024 ho->hisaddr = ciaddr1;
1025 ho->ouraddr = ciaddr2;
1029 IPCPDEBUG((LOG_INFO, "ipcp: received ADDR "));
1031 if (!ao->neg_addr ||
1032 cilen != CILEN_ADDR) { /* Check CI length */
1033 orc = CONFREJ; /* Reject CI */
1038 * If he has no address, or if we both have his address but
1039 * disagree about it, then NAK it with our idea.
1040 * In particular, if we don't know his address, but he does,
1043 GETLONG(tl, p); /* Parse source address (his) */
1044 ciaddr1 = htonl(tl);
1045 IPCPDEBUG((LOG_INFO, "(%s)", ip_ntoa(ciaddr1)));
1046 if (ciaddr1 != wo->hisaddr
1047 && (ciaddr1 == 0 || !wo->accept_remote)) {
1049 if (!reject_if_disagree) {
1050 DECPTR(sizeof(u_int32_t), p);
1051 tl = ntohl(wo->hisaddr);
1054 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1056 * Don't ACK an address of 0.0.0.0 - reject it instead.
1059 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1064 ho->hisaddr = ciaddr1;
1069 /* Microsoft primary or secondary DNS request */
1070 d = citype == CI_MS_DNS2;
1071 IPCPDEBUG((LOG_INFO, "ipcp: received DNS%d Request ", d+1));
1073 /* If we do not have a DNS address then we cannot send it */
1074 if (ao->dnsaddr[d] == 0 ||
1075 cilen != CILEN_ADDR) { /* Check CI length */
1076 orc = CONFREJ; /* Reject CI */
1080 if (htonl(tl) != ao->dnsaddr[d]) {
1081 DECPTR(sizeof(u_int32_t), p);
1082 tl = ntohl(ao->dnsaddr[d]);
1090 /* Microsoft primary or secondary WINS request */
1091 d = citype == CI_MS_WINS2;
1092 IPCPDEBUG((LOG_INFO, "ipcp: received WINS%d Request ", d+1));
1094 /* If we do not have a DNS address then we cannot send it */
1095 if (ao->winsaddr[d] == 0 ||
1096 cilen != CILEN_ADDR) { /* Check CI length */
1097 orc = CONFREJ; /* Reject CI */
1101 if (htonl(tl) != ao->winsaddr[d]) {
1102 DECPTR(sizeof(u_int32_t), p);
1103 tl = ntohl(ao->winsaddr[d]);
1109 case CI_COMPRESSTYPE:
1110 IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
1112 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1116 GETSHORT(cishort, p);
1117 IPCPDEBUG((LOG_INFO, "(%d)", cishort));
1119 if (!(cishort == IPCP_VJ_COMP ||
1120 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1126 ho->vj_protocol = cishort;
1127 if (cilen == CILEN_VJ) {
1128 GETCHAR(maxslotindex, p);
1129 if (maxslotindex > ao->maxslotindex) {
1131 if (!reject_if_disagree){
1133 PUTCHAR(ao->maxslotindex, p);
1137 if (cflag && !ao->cflag) {
1139 if (!reject_if_disagree){
1141 PUTCHAR(wo->cflag, p);
1144 ho->maxslotindex = maxslotindex;
1148 ho->maxslotindex = MAX_STATES - 1;
1159 IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
1161 if (orc == CONFACK && /* Good CI */
1162 rc != CONFACK) /* but prior CI wasnt? */
1163 continue; /* Don't send this one */
1165 if (orc == CONFNAK) { /* Nak this CI? */
1166 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1167 orc = CONFREJ; /* Get tough if so */
1169 if (rc == CONFREJ) /* Rejecting prior CI? */
1170 continue; /* Don't send this one */
1171 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1172 rc = CONFNAK; /* Not anymore... */
1173 ucp = inp; /* Backup */
1178 if (orc == CONFREJ && /* Reject this CI */
1179 rc != CONFREJ) { /* but no prior ones? */
1181 ucp = inp; /* Backup */
1184 /* Need to move CI? */
1186 BCOPY(cip, ucp, cilen); /* Move it */
1188 /* Update output pointer */
1193 * If we aren't rejecting this packet, and we want to negotiate
1194 * their address, and they didn't send their address, then we
1195 * send a NAK with a CI_ADDR option appended. We assume the
1196 * input buffer is long enough that we can append the extra
1199 if (rc != CONFREJ && !ho->neg_addr &&
1200 wo->req_addr && !reject_if_disagree) {
1201 if (rc == CONFACK) {
1203 ucp = inp; /* reset pointer */
1204 wo->req_addr = 0; /* don't ask again */
1206 PUTCHAR(CI_ADDR, ucp);
1207 PUTCHAR(CILEN_ADDR, ucp);
1208 tl = ntohl(wo->hisaddr);
1212 *len = ucp - inp; /* Compute output length */
1213 IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
1214 return (rc); /* Return final code */
1219 * ip_check_options - check that any IP-related options are OK,
1220 * and assign appropriate defaults.
1227 ipcp_options *wo = &ipcp_wantoptions[0];
1230 * Default our local IP address based on our hostname.
1231 * If local IP address already given, don't bother.
1233 if (wo->ouraddr == 0 && !disable_defaultip) {
1235 * Look up our hostname (possibly with domain name appended)
1236 * and take the first IP address as our local IP address.
1237 * If there isn't an IP address for our hostname, too bad.
1239 wo->accept_local = 1; /* don't insist on this default value */
1240 if ((hp = gethostbyname(hostname)) != NULL) {
1241 local = *(u_int32_t *)hp->h_addr;
1242 if (local != 0 && !bad_ip_adrs(local))
1243 wo->ouraddr = local;
1247 if (demand && wo->hisaddr == 0) {
1248 option_error("remote IP address required for demand-dialling\n");
1252 if (demand && wo->accept_remote) {
1253 option_error("ipcp-accept-remote is incompatible with demand\n");
1261 * ip_demand_conf - configure the interface as though
1262 * IPCP were up, for use with dial-on-demand.
1268 ipcp_options *wo = &ipcp_wantoptions[u];
1270 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1274 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1276 if (wo->default_route)
1277 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1278 default_route_set[u] = 1;
1280 if (sifproxyarp(u, wo->hisaddr))
1281 proxy_arp_set[u] = 1;
1283 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(wo->ouraddr));
1284 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(wo->hisaddr));
1291 * ipcp_up - IPCP has come UP.
1293 * Configure the IP network interface appropriately and bring it up.
1300 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1301 ipcp_options *go = &ipcp_gotoptions[f->unit];
1302 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1304 np_up(f->unit, PPP_IP);
1305 IPCPDEBUG((LOG_INFO, "ipcp: up"));
1308 * We must have a non-zero IP address for both ends of the link.
1311 ho->hisaddr = wo->hisaddr;
1313 if (ho->hisaddr == 0) {
1314 syslog(LOG_ERR, "Could not determine remote IP address");
1315 ipcp_close(f->unit, "Could not determine remote IP address");
1318 if (go->ouraddr == 0) {
1319 syslog(LOG_ERR, "Could not determine local IP address");
1320 ipcp_close(f->unit, "Could not determine local IP address");
1323 script_setenv("IPLOCAL", ip_ntoa(go->ouraddr));
1324 script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr));
1327 * Check that the peer is allowed to use the IP address it wants.
1329 if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1330 syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
1331 ip_ntoa(ho->hisaddr));
1332 ipcp_close(f->unit, "Unauthorized remote IP address");
1336 /* set tcp compression */
1337 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1340 * If we are doing dial-on-demand, the interface is already
1341 * configured, so we put out any saved-up packets, then set the
1342 * interface to pass IP packets.
1345 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1346 if (go->ouraddr != wo->ouraddr) {
1347 syslog(LOG_WARNING, "Local IP address changed to %s",
1348 ip_ntoa(go->ouraddr));
1349 script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr));
1350 wo->ouraddr = go->ouraddr;
1352 script_unsetenv("OLDIPLOCAL");
1353 if (ho->hisaddr != wo->hisaddr) {
1354 syslog(LOG_WARNING, "Remote IP address changed to %s",
1355 ip_ntoa(ho->hisaddr));
1356 script_setenv("OLDIPREMOTE", ip_ntoa(wo->hisaddr));
1357 wo->hisaddr = ho->hisaddr;
1359 script_unsetenv("OLDIPREMOTE");
1360 ipcp_clear_addrs(f->unit);
1362 /* Set the interface to the new addresses */
1363 mask = GetMask(go->ouraddr);
1364 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1365 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1366 ipcp_close(f->unit, "Interface configuration failed");
1370 /* assign a default route through the interface if required */
1371 if (ipcp_wantoptions[f->unit].default_route)
1372 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1373 default_route_set[f->unit] = 1;
1375 /* Make a proxy ARP entry if requested. */
1376 if (ipcp_wantoptions[f->unit].proxy_arp)
1377 if (sifproxyarp(f->unit, ho->hisaddr))
1378 proxy_arp_set[f->unit] = 1;
1381 demand_rexmit(PPP_IP);
1382 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1386 * Set IP addresses and (if specified) netmask.
1388 mask = GetMask(go->ouraddr);
1390 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1391 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1392 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1393 ipcp_close(f->unit, "Interface configuration failed");
1398 /* bring the interface up for IP */
1399 if (!sifup(f->unit)) {
1400 IPCPDEBUG((LOG_WARNING, "sifup failed"));
1401 ipcp_close(f->unit, "Interface configuration failed");
1405 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1406 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1407 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1408 ipcp_close(f->unit, "Interface configuration failed");
1412 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1414 /* assign a default route through the interface if required */
1415 if (ipcp_wantoptions[f->unit].default_route)
1416 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1417 default_route_set[f->unit] = 1;
1419 /* Make a proxy ARP entry if requested. */
1420 if (ipcp_wantoptions[f->unit].proxy_arp)
1421 if (sifproxyarp(f->unit, ho->hisaddr))
1422 proxy_arp_set[f->unit] = 1;
1424 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(go->ouraddr));
1425 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1429 * Execute the ip-up script, like this:
1430 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1432 if (ipcp_script_state == s_down && ipcp_script_pid == 0) {
1433 ipcp_script_state = s_up;
1434 ipcp_script(_PATH_IPUP);
1440 * ipcp_down - IPCP has gone DOWN.
1442 * Take the IP network interface down, clear its addresses
1443 * and delete routes through it.
1449 IPCPDEBUG((LOG_INFO, "ipcp: down"));
1450 np_down(f->unit, PPP_IP);
1451 sifvjcomp(f->unit, 0, 0, 0);
1454 * If we are doing dial-on-demand, set the interface
1455 * to queue up outgoing packets (for now).
1458 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1461 ipcp_clear_addrs(f->unit);
1464 /* Execute the ip-down script */
1465 if (ipcp_script_state == s_up && ipcp_script_pid == 0) {
1466 ipcp_script_state = s_down;
1467 ipcp_script(_PATH_IPDOWN);
1473 * ipcp_clear_addrs() - clear the interface addresses, routes,
1474 * proxy arp entries, etc.
1477 ipcp_clear_addrs(unit)
1480 u_int32_t ouraddr, hisaddr;
1482 ouraddr = ipcp_gotoptions[unit].ouraddr;
1483 hisaddr = ipcp_hisoptions[unit].hisaddr;
1484 if (proxy_arp_set[unit]) {
1485 cifproxyarp(unit, hisaddr);
1486 proxy_arp_set[unit] = 0;
1488 if (default_route_set[unit]) {
1489 cifdefaultroute(unit, ouraddr, hisaddr);
1490 default_route_set[unit] = 0;
1492 cifaddr(unit, ouraddr, hisaddr);
1497 * ipcp_finished - possibly shut down the lower layers.
1503 np_finished(f->unit, PPP_IP);
1508 * ipcp_script_done - called when the ip-up or ip-down script
1512 ipcp_script_done(void *arg)
1514 ipcp_script_pid = 0;
1515 switch (ipcp_script_state) {
1517 if (ipcp_fsm[0].state != OPENED) {
1518 ipcp_script_state = s_down;
1519 ipcp_script(_PATH_IPDOWN);
1523 if (ipcp_fsm[0].state == OPENED) {
1524 ipcp_script_state = s_up;
1525 ipcp_script(_PATH_IPUP);
1533 * ipcp_script - Execute a script with arguments
1534 * interface-name tty-name speed local-IP remote-IP.
1540 char strspeed[32], strlocal[32], strremote[32];
1543 sprintf(strspeed, "%d", baud_rate);
1544 strcpy(strlocal, ip_ntoa(ipcp_gotoptions[0].ouraddr));
1545 strcpy(strremote, ip_ntoa(ipcp_hisoptions[0].hisaddr));
1552 argv[5] = strremote;
1555 ipcp_script_pid = run_program(script, argv, 0, ipcp_script_done, NULL);
1559 * ipcp_printpkt - print the contents of an IPCP packet.
1561 static char *ipcp_codenames[] = {
1562 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1563 "TermReq", "TermAck", "CodeRej"
1567 ipcp_printpkt(p, plen, printer, arg)
1570 void (*printer) __P((void *, char *, ...));
1573 int code, id, len, olen;
1574 u_char *pstart, *optend;
1578 if (plen < HEADERLEN)
1584 if (len < HEADERLEN || len > plen)
1587 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1588 printer(arg, " %s", ipcp_codenames[code-1]);
1590 printer(arg, " code=0x%x", code);
1591 printer(arg, " id=0x%x", id);
1598 /* print option list */
1603 if (olen < 2 || olen > len) {
1611 if (olen == CILEN_ADDRS) {
1614 printer(arg, "addrs %I", htonl(cilong));
1616 printer(arg, " %I", htonl(cilong));
1619 case CI_COMPRESSTYPE:
1620 if (olen >= CILEN_COMPRESS) {
1622 GETSHORT(cishort, p);
1623 printer(arg, "compress ");
1628 case IPCP_VJ_COMP_OLD:
1629 printer(arg, "old-VJ");
1632 printer(arg, "0x%x", cishort);
1637 if (olen == CILEN_ADDR) {
1640 printer(arg, "addr %I", htonl(cilong));
1647 printer(arg, "ms-dns %I", htonl(cilong));
1653 printer(arg, "ms-wins %I", htonl(cilong));
1656 while (p < optend) {
1658 printer(arg, " %.2x", code);
1666 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1668 print_string(p, len, printer, arg);
1675 /* print the rest of the bytes in the packet */
1676 for (; len > 0; --len) {
1678 printer(arg, " %.2x", code);
1685 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
1686 * We don't bring the link up for IP fragments or for TCP FIN packets
1689 #define IP_HDRLEN 20 /* bytes */
1690 #define IP_OFFMASK 0x1fff
1691 #define IPPROTO_TCP 6
1692 #define TCP_HDRLEN 20
1696 * We use these macros because the IP header may be at an odd address,
1697 * and some compilers might use word loads to get th_off or ip_hl.
1700 #define net_short(x) (((x)[0] << 8) + (x)[1])
1701 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
1702 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
1703 #define get_ipproto(x) (((unsigned char *)(x))[9])
1704 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
1705 #define get_tcpflags(x) (((unsigned char *)(x))[13])
1708 ip_active_pkt(pkt, len)
1717 if (len < IP_HDRLEN)
1719 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
1721 if (get_ipproto(pkt) != IPPROTO_TCP)
1723 hlen = get_iphl(pkt) * 4;
1724 if (len < hlen + TCP_HDRLEN)
1727 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)