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.36 1999/03/02 05:35:09 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 */
55 static bool usepeerdns; /* Ask peer for DNS addrs */
58 * Callbacks for fsm code. (CI = Configuration Information)
60 static void ipcp_resetci __P((fsm *)); /* Reset our CI */
61 static int ipcp_cilen __P((fsm *)); /* Return length of our CI */
62 static void ipcp_addci __P((fsm *, u_char *, int *)); /* Add our CI */
63 static int ipcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
64 static int ipcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
65 static int ipcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
66 static int ipcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv CI */
67 static void ipcp_up __P((fsm *)); /* We're UP */
68 static void ipcp_down __P((fsm *)); /* We're DOWN */
69 static void ipcp_finished __P((fsm *)); /* Don't need lower layer */
71 fsm ipcp_fsm[NUM_PPP]; /* IPCP fsm structure */
73 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
74 ipcp_resetci, /* Reset our Configuration Information */
75 ipcp_cilen, /* Length of our Configuration Information */
76 ipcp_addci, /* Add our Configuration Information */
77 ipcp_ackci, /* ACK our Configuration Information */
78 ipcp_nakci, /* NAK our Configuration Information */
79 ipcp_rejci, /* Reject our Configuration Information */
80 ipcp_reqci, /* Request peer's Configuration Information */
81 ipcp_up, /* Called when fsm reaches OPENED state */
82 ipcp_down, /* Called when fsm leaves OPENED state */
83 NULL, /* Called when we want the lower layer up */
84 ipcp_finished, /* Called when we want the lower layer down */
85 NULL, /* Called when Protocol-Reject received */
86 NULL, /* Retransmission is necessary */
87 NULL, /* Called to handle protocol-specific codes */
88 "IPCP" /* String name of protocol */
92 * Command-line options.
94 static int setvjslots __P((char **));
95 static int setdnsaddr __P((char **));
96 static int setwinsaddr __P((char **));
98 static option_t ipcp_option_list[] = {
99 { "noip", o_bool, &ipcp_protent.enabled_flag,
100 "Disable IP and IPCP" },
101 { "-ip", o_bool, &ipcp_protent.enabled_flag,
102 "Disable IP and IPCP" },
103 { "novj", o_bool, &ipcp_wantoptions[0].neg_vj,
104 "Disable VJ compression", OPT_A2COPY, &ipcp_allowoptions[0].neg_vj },
105 { "-vj", o_bool, &ipcp_wantoptions[0].neg_vj,
106 "Disable VJ compression", OPT_A2COPY, &ipcp_allowoptions[0].neg_vj },
107 { "novjccomp", o_bool, &ipcp_wantoptions[0].cflag,
108 "Disable VJ connection-ID compression", OPT_A2COPY,
109 &ipcp_allowoptions[0].cflag },
110 { "-vjccomp", o_bool, &ipcp_wantoptions[0].cflag,
111 "Disable VJ connection-ID compression", OPT_A2COPY,
112 &ipcp_allowoptions[0].cflag },
113 { "vj-max-slots", 1, setvjslots,
114 "Set maximum VJ header slots" },
115 { "ipcp-accept-local", o_bool, &ipcp_wantoptions[0].accept_local,
116 "Accept peer's address for us", 1 },
117 { "ipcp-accept-remote", o_bool, &ipcp_wantoptions[0].accept_remote,
118 "Accept peer's address for it", 1 },
119 { "ipparam", o_string, &ipparam,
120 "Set ip script parameter" },
121 { "noipdefault", o_bool, &disable_defaultip,
122 "Don't use name for default IP adrs", 1 },
123 { "ms-dns", 1, setdnsaddr,
124 "DNS address for the peer's use" },
125 { "ms-wins", 1, setwinsaddr,
126 "Nameserver for SMB over TCP/IP for peer" },
127 { "ipcp-restart", o_int, &ipcp_fsm[0].timeouttime,
128 "Set timeout for IPCP" },
129 { "ipcp-max-terminate", o_int, &ipcp_fsm[0].maxtermtransmits,
130 "Set max #xmits for term-reqs" },
131 { "ipcp-max-configure", o_int, &ipcp_fsm[0].maxconfreqtransmits,
132 "Set max #xmits for conf-reqs" },
133 { "ipcp-max-failure", o_int, &ipcp_fsm[0].maxnakloops,
134 "Set max #conf-naks for IPCP" },
135 { "defaultroute", o_bool, &ipcp_wantoptions[0].default_route,
136 "Add default route", OPT_ENABLE|1, &ipcp_allowoptions[0].default_route },
137 { "nodefaultroute", o_bool, &ipcp_allowoptions[0].default_route,
138 "disable defaultroute option", OPT_A2COPY,
139 &ipcp_wantoptions[0].default_route },
140 { "-defaultroute", o_bool, &ipcp_allowoptions[0].default_route,
141 "disable defaultroute option", OPT_A2COPY,
142 &ipcp_wantoptions[0].default_route },
143 { "proxyarp", o_bool, &ipcp_wantoptions[0].proxy_arp,
144 "Add proxy ARP entry", OPT_ENABLE|1, &ipcp_allowoptions[0].proxy_arp },
145 { "noproxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
146 "disable proxyarp option", OPT_A2COPY,
147 &ipcp_wantoptions[0].proxy_arp },
148 { "-proxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
149 "disable proxyarp option", OPT_A2COPY,
150 &ipcp_wantoptions[0].proxy_arp },
151 { "usepeerdns", o_bool, &usepeerdns,
152 "Ask peer for DNS address(es)" },
157 * Protocol entry points from main code.
159 static void ipcp_init __P((int));
160 static void ipcp_open __P((int));
161 static void ipcp_close __P((int, char *));
162 static void ipcp_lowerup __P((int));
163 static void ipcp_lowerdown __P((int));
164 static void ipcp_input __P((int, u_char *, int));
165 static void ipcp_protrej __P((int));
166 static int ipcp_printpkt __P((u_char *, int,
167 void (*) __P((void *, char *, ...)), void *));
168 static void ip_check_options __P((void));
169 static int ip_demand_conf __P((int));
170 static int ip_active_pkt __P((u_char *, int));
171 static void create_resolv __P((u_int32_t, u_int32_t));
173 struct protent ipcp_protent = {
192 static void ipcp_clear_addrs __P((int));
193 static void ipcp_script __P((char *)); /* Run an up/down script */
194 static void ipcp_script_done __P((void *));
197 * Lengths of configuration options.
200 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
201 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
202 #define CILEN_ADDR 6 /* new-style single address option */
203 #define CILEN_ADDRS 10 /* old-style dual address option */
206 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
207 (x) == CONFNAK ? "NAK" : "REJ")
210 * This state variable is used to ensure that we don't
211 * run an ipcp-up/down script while one is already running.
213 static enum script_state {
217 static pid_t ipcp_script_pid;
220 * Make a string representation of a network IP address.
228 ipaddr = ntohl(ipaddr);
230 sprintf(b, "%d.%d.%d.%d",
231 (u_char)(ipaddr >> 24),
232 (u_char)(ipaddr >> 16),
233 (u_char)(ipaddr >> 8),
243 * setvjslots - set maximum number of connection slots for VJ compression
251 if (!int_option(*argv, &value))
253 if (value < 2 || value > 16) {
254 option_error("vj-max-slots value must be between 2 and 16");
257 ipcp_wantoptions [0].maxslotindex =
258 ipcp_allowoptions[0].maxslotindex = value - 1;
263 * setdnsaddr - set the dns address(es)
272 dns = inet_addr(*argv);
274 if ((hp = gethostbyname(*argv)) == NULL) {
275 option_error("invalid address parameter '%s' for ms-dns option",
279 dns = *(u_int32_t *)hp->h_addr;
282 /* if there is no primary then update it. */
283 if (ipcp_allowoptions[0].dnsaddr[0] == 0)
284 ipcp_allowoptions[0].dnsaddr[0] = dns;
286 /* always set the secondary address value to the same value. */
287 ipcp_allowoptions[0].dnsaddr[1] = dns;
293 * setwinsaddr - set the wins address(es)
294 * This is primrarly used with the Samba package under UNIX or for pointing
295 * the caller to the existing WINS server on a Windows NT platform.
304 wins = inet_addr(*argv);
306 if ((hp = gethostbyname(*argv)) == NULL) {
307 option_error("invalid address parameter '%s' for ms-wins option",
311 wins = *(u_int32_t *)hp->h_addr;
314 /* if there is no primary then update it. */
315 if (ipcp_allowoptions[0].winsaddr[0] == 0)
316 ipcp_allowoptions[0].winsaddr[0] = wins;
318 /* always set the secondary address value to the same value. */
319 ipcp_allowoptions[0].winsaddr[1] = wins;
326 * ipcp_init - Initialize IPCP.
332 fsm *f = &ipcp_fsm[unit];
333 ipcp_options *wo = &ipcp_wantoptions[unit];
334 ipcp_options *ao = &ipcp_allowoptions[unit];
337 f->protocol = PPP_IPCP;
338 f->callbacks = &ipcp_callbacks;
339 fsm_init(&ipcp_fsm[unit]);
341 memset(wo, 0, sizeof(*wo));
342 memset(ao, 0, sizeof(*ao));
346 wo->vj_protocol = IPCP_VJ_COMP;
347 wo->maxslotindex = MAX_STATES - 1; /* really max index */
350 wo->req_dns1 = usepeerdns; /* Request DNS addresses from the peer */
351 wo->req_dns2 = usepeerdns;
354 /* max slots and slot-id compression are currently hardwired in */
355 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
360 ao->maxslotindex = MAX_STATES - 1;
364 * XXX These control whether the user may use the proxyarp
365 * and defaultroute options.
368 ao->default_route = 1;
373 * ipcp_open - IPCP is allowed to come up.
379 fsm_open(&ipcp_fsm[unit]);
384 * ipcp_close - Take IPCP down.
387 ipcp_close(unit, reason)
391 fsm_close(&ipcp_fsm[unit], reason);
396 * ipcp_lowerup - The lower layer is up.
402 fsm_lowerup(&ipcp_fsm[unit]);
407 * ipcp_lowerdown - The lower layer is down.
413 fsm_lowerdown(&ipcp_fsm[unit]);
418 * ipcp_input - Input IPCP packet.
421 ipcp_input(unit, p, len)
426 fsm_input(&ipcp_fsm[unit], p, len);
431 * ipcp_protrej - A Protocol-Reject was received for IPCP.
433 * Pretend the lower layer went down, so we shut up.
439 fsm_lowerdown(&ipcp_fsm[unit]);
444 * ipcp_resetci - Reset our CI.
445 * Called by fsm_sconfreq, Send Configure Request.
451 ipcp_options *wo = &ipcp_wantoptions[f->unit];
453 wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
454 if (wo->ouraddr == 0)
455 wo->accept_local = 1;
456 if (wo->hisaddr == 0)
457 wo->accept_remote = 1;
458 ipcp_gotoptions[f->unit] = *wo;
459 cis_received[f->unit] = 0;
464 * ipcp_cilen - Return length of our CI.
465 * Called by fsm_sconfreq, Send Configure Request.
471 ipcp_options *go = &ipcp_gotoptions[f->unit];
472 ipcp_options *wo = &ipcp_wantoptions[f->unit];
473 ipcp_options *ho = &ipcp_hisoptions[f->unit];
475 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
476 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
477 #define LENCIDNS(neg) (neg ? (CILEN_ADDR) : 0)
480 * First see if we want to change our options to the old
481 * forms because we have received old forms from the peer.
483 if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
484 /* use the old style of address negotiation */
488 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
489 /* try an older style of VJ negotiation */
490 if (cis_received[f->unit] == 0) {
491 /* keep trying the new style until we see some CI from the peer */
494 /* use the old style only if the peer did */
495 if (ho->neg_vj && ho->old_vj) {
498 go->vj_protocol = ho->vj_protocol;
503 return (LENCIADDR(go->neg_addr, go->old_addrs) +
504 LENCIVJ(go->neg_vj, go->old_vj) +
505 LENCIDNS(go->req_dns1) +
506 LENCIDNS(go->req_dns2)) ;
511 * ipcp_addci - Add our desired CIs to a packet.
512 * Called by fsm_sconfreq, Send Configure Request.
515 ipcp_addci(f, ucp, lenp)
520 ipcp_options *go = &ipcp_gotoptions[f->unit];
523 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
525 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
526 if (len >= vjlen) { \
528 PUTCHAR(vjlen, ucp); \
529 PUTSHORT(val, ucp); \
531 PUTCHAR(maxslotindex, ucp); \
532 PUTCHAR(cflag, ucp); \
539 #define ADDCIADDR(opt, neg, old, val1, val2) \
541 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
542 if (len >= addrlen) { \
545 PUTCHAR(addrlen, ucp); \
557 #define ADDCIDNS(opt, neg) \
559 int addrlen = CILEN_ADDR; \
560 if (len >= addrlen) { \
563 PUTCHAR(addrlen, ucp); \
571 ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
572 go->old_addrs, go->ouraddr, go->hisaddr);
574 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
575 go->maxslotindex, go->cflag);
577 ADDCIDNS(CI_MS_DNS1, go->req_dns1);
579 ADDCIDNS(CI_MS_DNS2, go->req_dns2);
586 * ipcp_ackci - Ack our CIs.
587 * Called by fsm_rconfack, Receive Configure ACK.
594 ipcp_ackci(f, p, len)
599 ipcp_options *go = &ipcp_gotoptions[f->unit];
600 u_short cilen, citype, cishort;
602 u_char cimaxslotindex, cicflag;
605 * CIs must be in exactly the same order that we sent...
606 * Check packet length and CI length at each step.
607 * If we find any deviations, then this packet is bad.
610 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
612 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
613 if ((len -= vjlen) < 0) \
615 GETCHAR(citype, p); \
617 if (cilen != vjlen || \
620 GETSHORT(cishort, p); \
621 if (cishort != val) \
624 GETCHAR(cimaxslotindex, p); \
625 if (cimaxslotindex != maxslotindex) \
627 GETCHAR(cicflag, p); \
628 if (cicflag != cflag) \
633 #define ACKCIADDR(opt, neg, old, val1, val2) \
635 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
637 if ((len -= addrlen) < 0) \
639 GETCHAR(citype, p); \
641 if (cilen != addrlen || \
646 if (val1 != cilong) \
651 if (val2 != cilong) \
656 ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
657 go->old_addrs, go->ouraddr, go->hisaddr);
659 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
660 go->maxslotindex, go->cflag);
663 * If there are any remaining CIs, then this packet is bad.
670 IPCPDEBUG((LOG_INFO, "ipcp_ackci: received bad Ack!"));
675 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
676 * This should not modify any state if the Nak is bad
677 * or if IPCP is in the OPENED state.
678 * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
685 ipcp_nakci(f, p, len)
690 ipcp_options *go = &ipcp_gotoptions[f->unit];
691 u_char cimaxslotindex, cicflag;
692 u_char citype, cilen, *next;
694 u_int32_t ciaddr1, ciaddr2, l, cidnsaddr;
695 ipcp_options no; /* options we've seen Naks for */
696 ipcp_options try; /* options to request next time */
698 BZERO(&no, sizeof(no));
702 * Any Nak'd CIs must be in exactly the same order that we sent.
703 * Check packet length and CI length at each step.
704 * If we find any deviations, then this packet is bad.
706 #define NAKCIADDR(opt, neg, old, code) \
708 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
714 ciaddr1 = htonl(l); \
717 ciaddr2 = htonl(l); \
725 #define NAKCIVJ(opt, neg, code) \
727 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
732 GETSHORT(cishort, p); \
738 * Peer returns DNS address in a NAK packet
740 #define NAKCIDNS(opt, neg, code) \
742 ((cilen = p[1]) == CILEN_ADDR) && \
748 cidnsaddr = htonl(l); \
754 * Accept the peer's idea of {our,his} address, if different
755 * from our idea, only if the accept_{local,remote} flag is set.
757 NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
758 if (go->accept_local && ciaddr1) { /* Do we know our address? */
759 try.ouraddr = ciaddr1;
760 IPCPDEBUG((LOG_INFO, "local IP address %s",
763 if (go->accept_remote && ciaddr2) { /* Does he know his? */
764 try.hisaddr = ciaddr2;
765 IPCPDEBUG((LOG_INFO, "remote IP address %s",
771 * Accept the peer's value of maxslotindex provided that it
772 * is less than what we asked for. Turn off slot-ID compression
773 * if the peer wants. Send old-style compress-type option if
776 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
777 if (cilen == CILEN_VJ) {
778 GETCHAR(cimaxslotindex, p);
780 if (cishort == IPCP_VJ_COMP) {
782 if (cimaxslotindex < go->maxslotindex)
783 try.maxslotindex = cimaxslotindex;
790 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
792 try.vj_protocol = cishort;
799 NAKCIDNS(CI_MS_DNS1, req_dns1,
800 try.dnsaddr[0] = cidnsaddr;
801 IPCPDEBUG((LOG_INFO, "Received DNS address %s", ip_ntoa(cidnsaddr)));
805 NAKCIDNS(CI_MS_DNS2, req_dns2,
806 try.dnsaddr[1] = cidnsaddr;
807 IPCPDEBUG((LOG_INFO, "Received DNS address %s", ip_ntoa(cidnsaddr)));
812 * There may be remaining CIs, if the peer is requesting negotiation
813 * on an option that we didn't include in our request packet.
814 * If they want to negotiate about IP addresses, we comply.
815 * If they want us to ask for compression, we refuse.
817 while (len > CILEN_VOID) {
820 if( (len -= cilen) < 0 )
822 next = p + cilen - 2;
825 case CI_COMPRESSTYPE:
826 if (go->neg_vj || no.neg_vj ||
827 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
832 if ((go->neg_addr && go->old_addrs) || no.old_addrs
833 || cilen != CILEN_ADDRS)
839 if (ciaddr1 && go->accept_local)
840 try.ouraddr = ciaddr1;
843 if (ciaddr2 && go->accept_remote)
844 try.hisaddr = ciaddr2;
848 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
853 if (ciaddr1 && go->accept_local)
854 try.ouraddr = ciaddr1;
855 if (try.ouraddr != 0)
863 /* If there is still anything left, this packet is bad. */
868 * OK, the Nak is good. Now we can update state.
870 if (f->state != OPENED)
876 IPCPDEBUG((LOG_INFO, "ipcp_nakci: received bad Nak!"));
882 * ipcp_rejci - Reject some of our CIs.
883 * Callback from fsm_rconfnakrej.
886 ipcp_rejci(f, p, len)
891 ipcp_options *go = &ipcp_gotoptions[f->unit];
892 u_char cimaxslotindex, ciflag, cilen;
895 ipcp_options try; /* options to request next time */
899 * Any Rejected CIs must be in exactly the same order that we sent.
900 * Check packet length and CI length at each step.
901 * If we find any deviations, then this packet is bad.
903 #define REJCIADDR(opt, neg, old, val1, val2) \
905 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
913 /* Check rejected value. */ \
914 if (cilong != val1) \
919 /* Check rejected value. */ \
920 if (cilong != val2) \
926 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
928 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
933 GETSHORT(cishort, p); \
934 /* Check rejected value. */ \
935 if (cishort != val) \
938 GETCHAR(cimaxslotindex, p); \
939 if (cimaxslotindex != maxslot) \
941 GETCHAR(ciflag, p); \
942 if (ciflag != cflag) \
948 #define REJCIDNS(opt, neg, dnsaddr) \
950 ((cilen = p[1]) == CI_MS_DNS1) && \
958 /* Check rejected value. */ \
959 if (cilong != dnsaddr) \
965 REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
966 go->old_addrs, go->ouraddr, go->hisaddr);
968 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
969 go->maxslotindex, go->cflag);
971 REJCIDNS(CI_MS_DNS1, req_dns1, go->dnsaddr[0]);
973 REJCIDNS(CI_MS_DNS2, req_dns2, go->dnsaddr[1]);
976 * If there are any remaining CIs, then this packet is bad.
981 * Now we can update state.
983 if (f->state != OPENED)
988 IPCPDEBUG((LOG_INFO, "ipcp_rejci: received bad Reject!"));
994 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
995 * Callback from fsm_rconfreq, Receive Configure Request
997 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
998 * appropriately. If reject_if_disagree is non-zero, doesn't return
999 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1002 ipcp_reqci(f, inp, len, reject_if_disagree)
1004 u_char *inp; /* Requested CIs */
1005 int *len; /* Length of requested CIs */
1006 int reject_if_disagree;
1008 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1009 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1010 ipcp_options *ao = &ipcp_allowoptions[f->unit];
1011 ipcp_options *go = &ipcp_gotoptions[f->unit];
1012 u_char *cip, *next; /* Pointer to current and next CIs */
1013 u_short cilen, citype; /* Parsed len, type */
1014 u_short cishort; /* Parsed short value */
1015 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
1016 int rc = CONFACK; /* Final packet return code */
1017 int orc; /* Individual option return code */
1018 u_char *p; /* Pointer to next char to parse */
1019 u_char *ucp = inp; /* Pointer to current output char */
1020 int l = *len; /* Length left */
1021 u_char maxslotindex, cflag;
1024 cis_received[f->unit] = 1;
1027 * Reset all his options.
1029 BZERO(ho, sizeof(*ho));
1032 * Process all his options.
1036 orc = CONFACK; /* Assume success */
1037 cip = p = next; /* Remember begining of CI */
1038 if (l < 2 || /* Not enough data for CI header or */
1039 p[1] < 2 || /* CI length too small or */
1040 p[1] > l) { /* CI length too big? */
1041 IPCPDEBUG((LOG_INFO, "ipcp_reqci: bad CI length!"));
1042 orc = CONFREJ; /* Reject bad CI */
1043 cilen = l; /* Reject till end of packet */
1044 l = 0; /* Don't loop again */
1047 GETCHAR(citype, p); /* Parse CI type */
1048 GETCHAR(cilen, p); /* Parse CI length */
1049 l -= cilen; /* Adjust remaining length */
1050 next += cilen; /* Step to next CI */
1052 switch (citype) { /* Check CI type */
1054 IPCPDEBUG((LOG_INFO, "ipcp: received ADDRS "));
1055 if (!ao->neg_addr ||
1056 cilen != CILEN_ADDRS) { /* Check CI length */
1057 orc = CONFREJ; /* Reject CI */
1062 * If he has no address, or if we both have his address but
1063 * disagree about it, then NAK it with our idea.
1064 * In particular, if we don't know his address, but he does,
1067 GETLONG(tl, p); /* Parse source address (his) */
1068 ciaddr1 = htonl(tl);
1069 IPCPDEBUG((LOG_INFO, "(%s:", ip_ntoa(ciaddr1)));
1070 if (ciaddr1 != wo->hisaddr
1071 && (ciaddr1 == 0 || !wo->accept_remote)) {
1073 if (!reject_if_disagree) {
1074 DECPTR(sizeof(u_int32_t), p);
1075 tl = ntohl(wo->hisaddr);
1078 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1080 * If neither we nor he knows his address, reject the option.
1083 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1088 * If he doesn't know our address, or if we both have our address
1089 * but disagree about it, then NAK it with our idea.
1091 GETLONG(tl, p); /* Parse desination address (ours) */
1092 ciaddr2 = htonl(tl);
1093 IPCPDEBUG((LOG_INFO, "%s)", ip_ntoa(ciaddr2)));
1094 if (ciaddr2 != wo->ouraddr) {
1095 if (ciaddr2 == 0 || !wo->accept_local) {
1097 if (!reject_if_disagree) {
1098 DECPTR(sizeof(u_int32_t), p);
1099 tl = ntohl(wo->ouraddr);
1103 go->ouraddr = ciaddr2; /* accept peer's idea */
1109 ho->hisaddr = ciaddr1;
1110 ho->ouraddr = ciaddr2;
1114 IPCPDEBUG((LOG_INFO, "ipcp: received ADDR "));
1116 if (!ao->neg_addr ||
1117 cilen != CILEN_ADDR) { /* Check CI length */
1118 orc = CONFREJ; /* Reject CI */
1123 * If he has no address, or if we both have his address but
1124 * disagree about it, then NAK it with our idea.
1125 * In particular, if we don't know his address, but he does,
1128 GETLONG(tl, p); /* Parse source address (his) */
1129 ciaddr1 = htonl(tl);
1130 IPCPDEBUG((LOG_INFO, "(%s)", ip_ntoa(ciaddr1)));
1131 if (ciaddr1 != wo->hisaddr
1132 && (ciaddr1 == 0 || !wo->accept_remote)) {
1134 if (!reject_if_disagree) {
1135 DECPTR(sizeof(u_int32_t), p);
1136 tl = ntohl(wo->hisaddr);
1139 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1141 * Don't ACK an address of 0.0.0.0 - reject it instead.
1144 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1149 ho->hisaddr = ciaddr1;
1154 /* Microsoft primary or secondary DNS request */
1155 d = citype == CI_MS_DNS2;
1156 IPCPDEBUG((LOG_INFO, "ipcp: received DNS%d Request ", d+1));
1158 /* If we do not have a DNS address then we cannot send it */
1159 if (ao->dnsaddr[d] == 0 ||
1160 cilen != CILEN_ADDR) { /* Check CI length */
1161 orc = CONFREJ; /* Reject CI */
1165 if (htonl(tl) != ao->dnsaddr[d]) {
1166 DECPTR(sizeof(u_int32_t), p);
1167 tl = ntohl(ao->dnsaddr[d]);
1175 /* Microsoft primary or secondary WINS request */
1176 d = citype == CI_MS_WINS2;
1177 IPCPDEBUG((LOG_INFO, "ipcp: received WINS%d Request ", d+1));
1179 /* If we do not have a DNS address then we cannot send it */
1180 if (ao->winsaddr[d] == 0 ||
1181 cilen != CILEN_ADDR) { /* Check CI length */
1182 orc = CONFREJ; /* Reject CI */
1186 if (htonl(tl) != ao->winsaddr[d]) {
1187 DECPTR(sizeof(u_int32_t), p);
1188 tl = ntohl(ao->winsaddr[d]);
1194 case CI_COMPRESSTYPE:
1195 IPCPDEBUG((LOG_INFO, "ipcp: received COMPRESSTYPE "));
1197 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1201 GETSHORT(cishort, p);
1202 IPCPDEBUG((LOG_INFO, "(%d)", cishort));
1204 if (!(cishort == IPCP_VJ_COMP ||
1205 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1211 ho->vj_protocol = cishort;
1212 if (cilen == CILEN_VJ) {
1213 GETCHAR(maxslotindex, p);
1214 if (maxslotindex > ao->maxslotindex) {
1216 if (!reject_if_disagree){
1218 PUTCHAR(ao->maxslotindex, p);
1222 if (cflag && !ao->cflag) {
1224 if (!reject_if_disagree){
1226 PUTCHAR(wo->cflag, p);
1229 ho->maxslotindex = maxslotindex;
1233 ho->maxslotindex = MAX_STATES - 1;
1244 IPCPDEBUG((LOG_INFO, " (%s)\n", CODENAME(orc)));
1246 if (orc == CONFACK && /* Good CI */
1247 rc != CONFACK) /* but prior CI wasnt? */
1248 continue; /* Don't send this one */
1250 if (orc == CONFNAK) { /* Nak this CI? */
1251 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1252 orc = CONFREJ; /* Get tough if so */
1254 if (rc == CONFREJ) /* Rejecting prior CI? */
1255 continue; /* Don't send this one */
1256 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1257 rc = CONFNAK; /* Not anymore... */
1258 ucp = inp; /* Backup */
1263 if (orc == CONFREJ && /* Reject this CI */
1264 rc != CONFREJ) { /* but no prior ones? */
1266 ucp = inp; /* Backup */
1269 /* Need to move CI? */
1271 BCOPY(cip, ucp, cilen); /* Move it */
1273 /* Update output pointer */
1278 * If we aren't rejecting this packet, and we want to negotiate
1279 * their address, and they didn't send their address, then we
1280 * send a NAK with a CI_ADDR option appended. We assume the
1281 * input buffer is long enough that we can append the extra
1284 if (rc != CONFREJ && !ho->neg_addr &&
1285 wo->req_addr && !reject_if_disagree) {
1286 if (rc == CONFACK) {
1288 ucp = inp; /* reset pointer */
1289 wo->req_addr = 0; /* don't ask again */
1291 PUTCHAR(CI_ADDR, ucp);
1292 PUTCHAR(CILEN_ADDR, ucp);
1293 tl = ntohl(wo->hisaddr);
1297 *len = ucp - inp; /* Compute output length */
1298 IPCPDEBUG((LOG_INFO, "ipcp: returning Configure-%s", CODENAME(rc)));
1299 return (rc); /* Return final code */
1304 * ip_check_options - check that any IP-related options are OK,
1305 * and assign appropriate defaults.
1312 ipcp_options *wo = &ipcp_wantoptions[0];
1315 * Default our local IP address based on our hostname.
1316 * If local IP address already given, don't bother.
1318 if (wo->ouraddr == 0 && !disable_defaultip) {
1320 * Look up our hostname (possibly with domain name appended)
1321 * and take the first IP address as our local IP address.
1322 * If there isn't an IP address for our hostname, too bad.
1324 wo->accept_local = 1; /* don't insist on this default value */
1325 if ((hp = gethostbyname(hostname)) != NULL) {
1326 local = *(u_int32_t *)hp->h_addr;
1327 if (local != 0 && !bad_ip_adrs(local))
1328 wo->ouraddr = local;
1332 if (demand && wo->hisaddr == 0) {
1333 option_error("remote IP address required for demand-dialling\n");
1337 if (demand && wo->accept_remote) {
1338 option_error("ipcp-accept-remote is incompatible with demand\n");
1346 * ip_demand_conf - configure the interface as though
1347 * IPCP were up, for use with dial-on-demand.
1353 ipcp_options *wo = &ipcp_wantoptions[u];
1355 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1359 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1361 if (wo->default_route)
1362 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1363 default_route_set[u] = 1;
1365 if (sifproxyarp(u, wo->hisaddr))
1366 proxy_arp_set[u] = 1;
1368 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(wo->ouraddr));
1369 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(wo->hisaddr));
1376 * ipcp_up - IPCP has come UP.
1378 * Configure the IP network interface appropriately and bring it up.
1385 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1386 ipcp_options *go = &ipcp_gotoptions[f->unit];
1387 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1389 np_up(f->unit, PPP_IP);
1390 IPCPDEBUG((LOG_INFO, "ipcp: up"));
1393 * We must have a non-zero IP address for both ends of the link.
1396 ho->hisaddr = wo->hisaddr;
1398 if (ho->hisaddr == 0) {
1399 syslog(LOG_ERR, "Could not determine remote IP address");
1400 ipcp_close(f->unit, "Could not determine remote IP address");
1403 if (go->ouraddr == 0) {
1404 syslog(LOG_ERR, "Could not determine local IP address");
1405 ipcp_close(f->unit, "Could not determine local IP address");
1408 script_setenv("IPLOCAL", ip_ntoa(go->ouraddr));
1409 script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr));
1411 if (usepeerdns && (go->dnsaddr[0] || go->dnsaddr[1])) {
1412 script_setenv("USEPEERDNS", "1");
1414 script_setenv("DNS1", ip_ntoa(go->dnsaddr[0]));
1416 script_setenv("DNS2", ip_ntoa(go->dnsaddr[1]));
1417 create_resolv(go->dnsaddr[0], go->dnsaddr[1]);
1421 * Check that the peer is allowed to use the IP address it wants.
1423 if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1424 syslog(LOG_ERR, "Peer is not authorized to use remote address %s",
1425 ip_ntoa(ho->hisaddr));
1426 ipcp_close(f->unit, "Unauthorized remote IP address");
1430 /* set tcp compression */
1431 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1434 * If we are doing dial-on-demand, the interface is already
1435 * configured, so we put out any saved-up packets, then set the
1436 * interface to pass IP packets.
1439 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1440 if (go->ouraddr != wo->ouraddr) {
1441 syslog(LOG_WARNING, "Local IP address changed to %s",
1442 ip_ntoa(go->ouraddr));
1443 script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr));
1444 wo->ouraddr = go->ouraddr;
1446 script_unsetenv("OLDIPLOCAL");
1447 if (ho->hisaddr != wo->hisaddr) {
1448 syslog(LOG_WARNING, "Remote IP address changed to %s",
1449 ip_ntoa(ho->hisaddr));
1450 script_setenv("OLDIPREMOTE", ip_ntoa(wo->hisaddr));
1451 wo->hisaddr = ho->hisaddr;
1453 script_unsetenv("OLDIPREMOTE");
1454 ipcp_clear_addrs(f->unit);
1456 /* Set the interface to the new addresses */
1457 mask = GetMask(go->ouraddr);
1458 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1459 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1460 ipcp_close(f->unit, "Interface configuration failed");
1464 /* assign a default route through the interface if required */
1465 if (ipcp_wantoptions[f->unit].default_route)
1466 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1467 default_route_set[f->unit] = 1;
1469 /* Make a proxy ARP entry if requested. */
1470 if (ipcp_wantoptions[f->unit].proxy_arp)
1471 if (sifproxyarp(f->unit, ho->hisaddr))
1472 proxy_arp_set[f->unit] = 1;
1475 demand_rexmit(PPP_IP);
1476 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1480 * Set IP addresses and (if specified) netmask.
1482 mask = GetMask(go->ouraddr);
1484 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1485 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1486 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1487 ipcp_close(f->unit, "Interface configuration failed");
1492 /* bring the interface up for IP */
1493 if (!sifup(f->unit)) {
1494 IPCPDEBUG((LOG_WARNING, "sifup failed"));
1495 ipcp_close(f->unit, "Interface configuration failed");
1499 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1500 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1501 IPCPDEBUG((LOG_WARNING, "sifaddr failed"));
1502 ipcp_close(f->unit, "Interface configuration failed");
1506 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1508 /* assign a default route through the interface if required */
1509 if (ipcp_wantoptions[f->unit].default_route)
1510 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1511 default_route_set[f->unit] = 1;
1513 /* Make a proxy ARP entry if requested. */
1514 if (ipcp_wantoptions[f->unit].proxy_arp)
1515 if (sifproxyarp(f->unit, ho->hisaddr))
1516 proxy_arp_set[f->unit] = 1;
1518 syslog(LOG_NOTICE, "local IP address %s", ip_ntoa(go->ouraddr));
1519 syslog(LOG_NOTICE, "remote IP address %s", ip_ntoa(ho->hisaddr));
1520 syslog(LOG_NOTICE, "primary DNS address %s", ip_ntoa(go->dnsaddr[0]));
1521 syslog(LOG_NOTICE, "secondary DNS address %s", ip_ntoa(go->dnsaddr[1]));
1525 * Execute the ip-up script, like this:
1526 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1528 if (ipcp_script_state == s_down && ipcp_script_pid == 0) {
1529 ipcp_script_state = s_up;
1530 ipcp_script(_PATH_IPUP);
1536 * ipcp_down - IPCP has gone DOWN.
1538 * Take the IP network interface down, clear its addresses
1539 * and delete routes through it.
1545 IPCPDEBUG((LOG_INFO, "ipcp: down"));
1546 np_down(f->unit, PPP_IP);
1547 sifvjcomp(f->unit, 0, 0, 0);
1550 * If we are doing dial-on-demand, set the interface
1551 * to queue up outgoing packets (for now).
1554 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1557 ipcp_clear_addrs(f->unit);
1560 /* Execute the ip-down script */
1561 if (ipcp_script_state == s_up && ipcp_script_pid == 0) {
1562 ipcp_script_state = s_down;
1563 ipcp_script(_PATH_IPDOWN);
1569 * ipcp_clear_addrs() - clear the interface addresses, routes,
1570 * proxy arp entries, etc.
1573 ipcp_clear_addrs(unit)
1576 u_int32_t ouraddr, hisaddr;
1578 ouraddr = ipcp_gotoptions[unit].ouraddr;
1579 hisaddr = ipcp_hisoptions[unit].hisaddr;
1580 if (proxy_arp_set[unit]) {
1581 cifproxyarp(unit, hisaddr);
1582 proxy_arp_set[unit] = 0;
1584 if (default_route_set[unit]) {
1585 cifdefaultroute(unit, ouraddr, hisaddr);
1586 default_route_set[unit] = 0;
1588 cifaddr(unit, ouraddr, hisaddr);
1593 * ipcp_finished - possibly shut down the lower layers.
1599 np_finished(f->unit, PPP_IP);
1604 * ipcp_script_done - called when the ip-up or ip-down script
1608 ipcp_script_done(void *arg)
1610 ipcp_script_pid = 0;
1611 switch (ipcp_script_state) {
1613 if (ipcp_fsm[0].state != OPENED) {
1614 ipcp_script_state = s_down;
1615 ipcp_script(_PATH_IPDOWN);
1619 if (ipcp_fsm[0].state == OPENED) {
1620 ipcp_script_state = s_up;
1621 ipcp_script(_PATH_IPUP);
1629 * ipcp_script - Execute a script with arguments
1630 * interface-name tty-name speed local-IP remote-IP.
1636 char strspeed[32], strlocal[32], strremote[32];
1639 sprintf(strspeed, "%d", baud_rate);
1640 strcpy(strlocal, ip_ntoa(ipcp_gotoptions[0].ouraddr));
1641 strcpy(strremote, ip_ntoa(ipcp_hisoptions[0].hisaddr));
1648 argv[5] = strremote;
1651 ipcp_script_pid = run_program(script, argv, 0, ipcp_script_done, NULL);
1655 * create_resolv - create the replacement resolv.conf file
1658 create_resolv(peerdns1, peerdns2)
1659 u_int32_t peerdns1, peerdns2;
1663 f = fopen(_PATH_RESOLV, "w");
1665 syslog(LOG_ERR, "Failed to create %s: %m", _PATH_RESOLV);
1670 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns1));
1673 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns2));
1676 syslog(LOG_ERR, "Write failed to %s: %m", _PATH_RESOLV);
1682 * ipcp_printpkt - print the contents of an IPCP packet.
1684 static char *ipcp_codenames[] = {
1685 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1686 "TermReq", "TermAck", "CodeRej"
1690 ipcp_printpkt(p, plen, printer, arg)
1693 void (*printer) __P((void *, char *, ...));
1696 int code, id, len, olen;
1697 u_char *pstart, *optend;
1701 if (plen < HEADERLEN)
1707 if (len < HEADERLEN || len > plen)
1710 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1711 printer(arg, " %s", ipcp_codenames[code-1]);
1713 printer(arg, " code=0x%x", code);
1714 printer(arg, " id=0x%x", id);
1721 /* print option list */
1726 if (olen < 2 || olen > len) {
1734 if (olen == CILEN_ADDRS) {
1737 printer(arg, "addrs %I", htonl(cilong));
1739 printer(arg, " %I", htonl(cilong));
1742 case CI_COMPRESSTYPE:
1743 if (olen >= CILEN_COMPRESS) {
1745 GETSHORT(cishort, p);
1746 printer(arg, "compress ");
1751 case IPCP_VJ_COMP_OLD:
1752 printer(arg, "old-VJ");
1755 printer(arg, "0x%x", cishort);
1760 if (olen == CILEN_ADDR) {
1763 printer(arg, "addr %I", htonl(cilong));
1770 printer(arg, "ms-dns %I", htonl(cilong));
1776 printer(arg, "ms-wins %I", htonl(cilong));
1779 while (p < optend) {
1781 printer(arg, " %.2x", code);
1789 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1791 print_string(p, len, printer, arg);
1798 /* print the rest of the bytes in the packet */
1799 for (; len > 0; --len) {
1801 printer(arg, " %.2x", code);
1808 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
1809 * We don't bring the link up for IP fragments or for TCP FIN packets
1812 #define IP_HDRLEN 20 /* bytes */
1813 #define IP_OFFMASK 0x1fff
1814 #define IPPROTO_TCP 6
1815 #define TCP_HDRLEN 20
1819 * We use these macros because the IP header may be at an odd address,
1820 * and some compilers might use word loads to get th_off or ip_hl.
1823 #define net_short(x) (((x)[0] << 8) + (x)[1])
1824 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
1825 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
1826 #define get_ipproto(x) (((unsigned char *)(x))[9])
1827 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
1828 #define get_tcpflags(x) (((unsigned char *)(x))[13])
1831 ip_active_pkt(pkt, len)
1840 if (len < IP_HDRLEN)
1842 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
1844 if (get_ipproto(pkt) != IPPROTO_TCP)
1846 hlen = get_iphl(pkt) * 4;
1847 if (len < hlen + TCP_HDRLEN)
1850 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)