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.
20 #define RCSID "$Id: ipcp.c,v 1.59 2002/01/11 18:31:42 etbe Exp $"
30 #include <sys/param.h>
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
39 #include "pathnames.h"
41 static const char rcsid[] = RCSID;
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 u_int32_t netmask = 0; /* IP netmask to set on interface */
51 bool disable_defaultip = 0; /* Don't use hostname for default IP adrs */
53 /* Hook for a plugin to know when IP protocol has come up */
54 void (*ip_up_hook) __P((void)) = NULL;
56 /* Hook for a plugin to know when IP protocol has come down */
57 void (*ip_down_hook) __P((void)) = NULL;
59 /* Hook for a plugin to choose the remote IP address */
60 void (*ip_choose_hook) __P((u_int32_t *)) = NULL;
62 /* Notifiers for when IPCP goes up and down */
63 struct notifier *ip_up_notifier = NULL;
64 struct notifier *ip_down_notifier = NULL;
67 static int default_route_set[NUM_PPP]; /* Have set up a default route */
68 static int proxy_arp_set[NUM_PPP]; /* Have created proxy arp entry */
69 static bool usepeerdns; /* Ask peer for DNS addrs */
70 static int ipcp_is_up; /* have called np_up() */
71 static bool ask_for_local; /* request our address from peer */
72 static char vj_value[8]; /* string form of vj option value */
73 static char netmask_str[20]; /* string form of netmask value */
76 * Callbacks for fsm code. (CI = Configuration Information)
78 static void ipcp_resetci __P((fsm *)); /* Reset our CI */
79 static int ipcp_cilen __P((fsm *)); /* Return length of our CI */
80 static void ipcp_addci __P((fsm *, u_char *, int *)); /* Add our CI */
81 static int ipcp_ackci __P((fsm *, u_char *, int)); /* Peer ack'd our CI */
82 static int ipcp_nakci __P((fsm *, u_char *, int)); /* Peer nak'd our CI */
83 static int ipcp_rejci __P((fsm *, u_char *, int)); /* Peer rej'd our CI */
84 static int ipcp_reqci __P((fsm *, u_char *, int *, int)); /* Rcv CI */
85 static void ipcp_up __P((fsm *)); /* We're UP */
86 static void ipcp_down __P((fsm *)); /* We're DOWN */
87 static void ipcp_finished __P((fsm *)); /* Don't need lower layer */
89 fsm ipcp_fsm[NUM_PPP]; /* IPCP fsm structure */
91 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
92 ipcp_resetci, /* Reset our Configuration Information */
93 ipcp_cilen, /* Length of our Configuration Information */
94 ipcp_addci, /* Add our Configuration Information */
95 ipcp_ackci, /* ACK our Configuration Information */
96 ipcp_nakci, /* NAK our Configuration Information */
97 ipcp_rejci, /* Reject our Configuration Information */
98 ipcp_reqci, /* Request peer's Configuration Information */
99 ipcp_up, /* Called when fsm reaches OPENED state */
100 ipcp_down, /* Called when fsm leaves OPENED state */
101 NULL, /* Called when we want the lower layer up */
102 ipcp_finished, /* Called when we want the lower layer down */
103 NULL, /* Called when Protocol-Reject received */
104 NULL, /* Retransmission is necessary */
105 NULL, /* Called to handle protocol-specific codes */
106 "IPCP" /* String name of protocol */
110 * Command-line options.
112 static int setvjslots __P((char **));
113 static int setdnsaddr __P((char **));
114 static int setwinsaddr __P((char **));
115 static int setnetmask __P((char **));
116 int setipaddr __P((char *, char **, int));
117 static void printipaddr __P((option_t *, void (*)(void *, char *,...),void *));
119 static option_t ipcp_option_list[] = {
120 { "noip", o_bool, &ipcp_protent.enabled_flag,
121 "Disable IP and IPCP" },
122 { "-ip", o_bool, &ipcp_protent.enabled_flag,
123 "Disable IP and IPCP", OPT_ALIAS },
125 { "novj", o_bool, &ipcp_wantoptions[0].neg_vj,
126 "Disable VJ compression", OPT_A2CLR, &ipcp_allowoptions[0].neg_vj },
127 { "-vj", o_bool, &ipcp_wantoptions[0].neg_vj,
128 "Disable VJ compression", OPT_ALIAS | OPT_A2CLR,
129 &ipcp_allowoptions[0].neg_vj },
131 { "novjccomp", o_bool, &ipcp_wantoptions[0].cflag,
132 "Disable VJ connection-ID compression", OPT_A2CLR,
133 &ipcp_allowoptions[0].cflag },
134 { "-vjccomp", o_bool, &ipcp_wantoptions[0].cflag,
135 "Disable VJ connection-ID compression", OPT_ALIAS | OPT_A2CLR,
136 &ipcp_allowoptions[0].cflag },
138 { "vj-max-slots", o_special, (void *)setvjslots,
139 "Set maximum VJ header slots",
140 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, vj_value },
142 { "ipcp-accept-local", o_bool, &ipcp_wantoptions[0].accept_local,
143 "Accept peer's address for us", 1 },
144 { "ipcp-accept-remote", o_bool, &ipcp_wantoptions[0].accept_remote,
145 "Accept peer's address for it", 1 },
147 { "ipparam", o_string, &ipparam,
148 "Set ip script parameter", OPT_PRIO },
150 { "noipdefault", o_bool, &disable_defaultip,
151 "Don't use name for default IP adrs", 1 },
153 { "ms-dns", 1, (void *)setdnsaddr,
154 "DNS address for the peer's use" },
155 { "ms-wins", 1, (void *)setwinsaddr,
156 "Nameserver for SMB over TCP/IP for peer" },
158 { "ipcp-restart", o_int, &ipcp_fsm[0].timeouttime,
159 "Set timeout for IPCP", OPT_PRIO },
160 { "ipcp-max-terminate", o_int, &ipcp_fsm[0].maxtermtransmits,
161 "Set max #xmits for term-reqs", OPT_PRIO },
162 { "ipcp-max-configure", o_int, &ipcp_fsm[0].maxconfreqtransmits,
163 "Set max #xmits for conf-reqs", OPT_PRIO },
164 { "ipcp-max-failure", o_int, &ipcp_fsm[0].maxnakloops,
165 "Set max #conf-naks for IPCP", OPT_PRIO },
167 { "defaultroute", o_bool, &ipcp_wantoptions[0].default_route,
168 "Add default route", OPT_ENABLE|1, &ipcp_allowoptions[0].default_route },
169 { "nodefaultroute", o_bool, &ipcp_allowoptions[0].default_route,
170 "disable defaultroute option", OPT_A2CLR,
171 &ipcp_wantoptions[0].default_route },
172 { "-defaultroute", o_bool, &ipcp_allowoptions[0].default_route,
173 "disable defaultroute option", OPT_ALIAS | OPT_A2CLR,
174 &ipcp_wantoptions[0].default_route },
176 { "proxyarp", o_bool, &ipcp_wantoptions[0].proxy_arp,
177 "Add proxy ARP entry", OPT_ENABLE|1, &ipcp_allowoptions[0].proxy_arp },
178 { "noproxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
179 "disable proxyarp option", OPT_A2CLR,
180 &ipcp_wantoptions[0].proxy_arp },
181 { "-proxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
182 "disable proxyarp option", OPT_ALIAS | OPT_A2CLR,
183 &ipcp_wantoptions[0].proxy_arp },
185 { "usepeerdns", o_bool, &usepeerdns,
186 "Ask peer for DNS address(es)", 1 },
188 { "netmask", o_special, (void *)setnetmask,
189 "set netmask", OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, netmask_str },
191 { "IP addresses", o_wild, (void *) &setipaddr,
192 "set local and remote IP addresses",
193 OPT_NOARG | OPT_A2PRINTER, (void *) &printipaddr },
199 * Protocol entry points from main code.
201 static void ipcp_init __P((int));
202 static void ipcp_open __P((int));
203 static void ipcp_close __P((int, char *));
204 static void ipcp_lowerup __P((int));
205 static void ipcp_lowerdown __P((int));
206 static void ipcp_input __P((int, u_char *, int));
207 static void ipcp_protrej __P((int));
208 static int ipcp_printpkt __P((u_char *, int,
209 void (*) __P((void *, char *, ...)), void *));
210 static void ip_check_options __P((void));
211 static int ip_demand_conf __P((int));
212 static int ip_active_pkt __P((u_char *, int));
213 static void create_resolv __P((u_int32_t, u_int32_t));
215 struct protent ipcp_protent = {
235 static void ipcp_clear_addrs __P((int, u_int32_t, u_int32_t));
236 static void ipcp_script __P((char *)); /* Run an up/down script */
237 static void ipcp_script_done __P((void *));
240 * Lengths of configuration options.
243 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
244 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
245 #define CILEN_ADDR 6 /* new-style single address option */
246 #define CILEN_ADDRS 10 /* old-style dual address option */
249 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
250 (x) == CONFNAK ? "NAK" : "REJ")
253 * This state variable is used to ensure that we don't
254 * run an ipcp-up/down script while one is already running.
256 static enum script_state {
260 static pid_t ipcp_script_pid;
263 * Make a string representation of a network IP address.
271 slprintf(b, sizeof(b), "%I", ipaddr);
280 * setvjslots - set maximum number of connection slots for VJ compression
288 if (!int_option(*argv, &value))
290 if (value < 2 || value > 16) {
291 option_error("vj-max-slots value must be between 2 and 16");
294 ipcp_wantoptions [0].maxslotindex =
295 ipcp_allowoptions[0].maxslotindex = value - 1;
296 slprintf(vj_value, sizeof(vj_value), "%d", value);
301 * setdnsaddr - set the dns address(es)
310 dns = inet_addr(*argv);
311 if (dns == (u_int32_t) -1) {
312 if ((hp = gethostbyname(*argv)) == NULL) {
313 option_error("invalid address parameter '%s' for ms-dns option",
317 dns = *(u_int32_t *)hp->h_addr;
320 /* We take the last 2 values given, the 2nd-last as the primary
321 and the last as the secondary. If only one is given it
322 becomes both primary and secondary. */
323 if (ipcp_allowoptions[0].dnsaddr[1] == 0)
324 ipcp_allowoptions[0].dnsaddr[0] = dns;
326 ipcp_allowoptions[0].dnsaddr[0] = ipcp_allowoptions[0].dnsaddr[1];
328 /* always set the secondary address value. */
329 ipcp_allowoptions[0].dnsaddr[1] = dns;
335 * setwinsaddr - set the wins address(es)
336 * This is primrarly used with the Samba package under UNIX or for pointing
337 * the caller to the existing WINS server on a Windows NT platform.
346 wins = inet_addr(*argv);
347 if (wins == (u_int32_t) -1) {
348 if ((hp = gethostbyname(*argv)) == NULL) {
349 option_error("invalid address parameter '%s' for ms-wins option",
353 wins = *(u_int32_t *)hp->h_addr;
356 /* We take the last 2 values given, the 2nd-last as the primary
357 and the last as the secondary. If only one is given it
358 becomes both primary and secondary. */
359 if (ipcp_allowoptions[0].winsaddr[1] == 0)
360 ipcp_allowoptions[0].winsaddr[0] = wins;
362 ipcp_allowoptions[0].winsaddr[0] = ipcp_allowoptions[0].winsaddr[1];
364 /* always set the secondary address value. */
365 ipcp_allowoptions[0].winsaddr[1] = wins;
371 * setipaddr - Set the IP address
372 * If doit is 0, the call is to check whether this option is
373 * potentially an IP address specification.
374 * Not static so that plugins can call it to set the addresses
377 setipaddr(arg, argv, doit)
384 u_int32_t local, remote;
385 ipcp_options *wo = &ipcp_wantoptions[0];
386 static int prio_local = 0, prio_remote = 0;
389 * IP address pair separated by ":".
391 if ((colon = strchr(arg, ':')) == NULL)
397 * If colon first character, then no local addr.
399 if (colon != arg && option_priority >= prio_local) {
401 if ((local = inet_addr(arg)) == (u_int32_t) -1) {
402 if ((hp = gethostbyname(arg)) == NULL) {
403 option_error("unknown host: %s", arg);
406 local = *(u_int32_t *)hp->h_addr;
408 if (bad_ip_adrs(local)) {
409 option_error("bad local IP address %s", ip_ntoa(local));
415 prio_local = option_priority;
419 * If colon last character, then no remote addr.
421 if (*++colon != '\0' && option_priority >= prio_remote) {
422 if ((remote = inet_addr(colon)) == (u_int32_t) -1) {
423 if ((hp = gethostbyname(colon)) == NULL) {
424 option_error("unknown host: %s", colon);
427 remote = *(u_int32_t *)hp->h_addr;
428 if (remote_name[0] == 0)
429 strlcpy(remote_name, colon, sizeof(remote_name));
431 if (bad_ip_adrs(remote)) {
432 option_error("bad remote IP address %s", ip_ntoa(remote));
436 wo->hisaddr = remote;
437 prio_remote = option_priority;
444 printipaddr(opt, printer, arg)
446 void (*printer) __P((void *, char *, ...));
449 ipcp_options *wo = &ipcp_wantoptions[0];
451 if (wo->ouraddr != 0)
452 printer(arg, "%I", wo->ouraddr);
454 if (wo->hisaddr != 0)
455 printer(arg, "%I", wo->hisaddr);
459 * setnetmask - set the netmask to be used on the interface.
470 * Unfortunately, if we use inet_addr, we can't tell whether
471 * a result of all 1s is an error or a valid 255.255.255.255.
474 n = parse_dotted_ip(p, &mask);
478 if (n == 0 || p[n] != 0 || (netmask & ~mask) != 0) {
479 option_error("invalid netmask value '%s'", *argv);
484 slprintf(netmask_str, sizeof(netmask_str), "%I", mask);
490 parse_dotted_ip(p, vp)
500 b = strtoul(p, &endp, 0);
506 /* accept e.g. 0xffffff00 */
524 * ipcp_init - Initialize IPCP.
530 fsm *f = &ipcp_fsm[unit];
531 ipcp_options *wo = &ipcp_wantoptions[unit];
532 ipcp_options *ao = &ipcp_allowoptions[unit];
535 f->protocol = PPP_IPCP;
536 f->callbacks = &ipcp_callbacks;
537 fsm_init(&ipcp_fsm[unit]);
539 memset(wo, 0, sizeof(*wo));
540 memset(ao, 0, sizeof(*ao));
544 wo->vj_protocol = IPCP_VJ_COMP;
545 wo->maxslotindex = MAX_STATES - 1; /* really max index */
549 /* max slots and slot-id compression are currently hardwired in */
550 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
555 ao->maxslotindex = MAX_STATES - 1;
559 * XXX These control whether the user may use the proxyarp
560 * and defaultroute options.
563 ao->default_route = 1;
568 * ipcp_open - IPCP is allowed to come up.
574 fsm_open(&ipcp_fsm[unit]);
579 * ipcp_close - Take IPCP down.
582 ipcp_close(unit, reason)
586 fsm_close(&ipcp_fsm[unit], reason);
591 * ipcp_lowerup - The lower layer is up.
597 fsm_lowerup(&ipcp_fsm[unit]);
602 * ipcp_lowerdown - The lower layer is down.
608 fsm_lowerdown(&ipcp_fsm[unit]);
613 * ipcp_input - Input IPCP packet.
616 ipcp_input(unit, p, len)
621 fsm_input(&ipcp_fsm[unit], p, len);
626 * ipcp_protrej - A Protocol-Reject was received for IPCP.
628 * Pretend the lower layer went down, so we shut up.
634 fsm_lowerdown(&ipcp_fsm[unit]);
639 * ipcp_resetci - Reset our CI.
640 * Called by fsm_sconfreq, Send Configure Request.
646 ipcp_options *wo = &ipcp_wantoptions[f->unit];
647 ipcp_options *go = &ipcp_gotoptions[f->unit];
649 wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
650 if (wo->ouraddr == 0)
651 wo->accept_local = 1;
652 if (wo->hisaddr == 0)
653 wo->accept_remote = 1;
654 wo->req_dns1 = usepeerdns; /* Request DNS addresses from the peer */
655 wo->req_dns2 = usepeerdns;
660 ip_choose_hook(&wo->hisaddr);
665 * ipcp_cilen - Return length of our CI.
666 * Called by fsm_sconfreq, Send Configure Request.
672 ipcp_options *go = &ipcp_gotoptions[f->unit];
673 ipcp_options *wo = &ipcp_wantoptions[f->unit];
674 ipcp_options *ho = &ipcp_hisoptions[f->unit];
676 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
677 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
678 #define LENCIDNS(neg) (neg ? (CILEN_ADDR) : 0)
681 * First see if we want to change our options to the old
682 * forms because we have received old forms from the peer.
684 if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
685 /* use the old style of address negotiation */
689 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
690 /* try an older style of VJ negotiation */
691 /* use the old style only if the peer did */
692 if (ho->neg_vj && ho->old_vj) {
695 go->vj_protocol = ho->vj_protocol;
699 return (LENCIADDR(go->neg_addr, go->old_addrs) +
700 LENCIVJ(go->neg_vj, go->old_vj) +
701 LENCIDNS(go->req_dns1) +
702 LENCIDNS(go->req_dns2)) ;
707 * ipcp_addci - Add our desired CIs to a packet.
708 * Called by fsm_sconfreq, Send Configure Request.
711 ipcp_addci(f, ucp, lenp)
716 ipcp_options *go = &ipcp_gotoptions[f->unit];
719 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
721 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
722 if (len >= vjlen) { \
724 PUTCHAR(vjlen, ucp); \
725 PUTSHORT(val, ucp); \
727 PUTCHAR(maxslotindex, ucp); \
728 PUTCHAR(cflag, ucp); \
735 #define ADDCIADDR(opt, neg, old, val1, val2) \
737 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
738 if (len >= addrlen) { \
741 PUTCHAR(addrlen, ucp); \
753 #define ADDCIDNS(opt, neg, addr) \
755 if (len >= CILEN_ADDR) { \
758 PUTCHAR(CILEN_ADDR, ucp); \
766 ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
767 go->old_addrs, go->ouraddr, go->hisaddr);
769 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
770 go->maxslotindex, go->cflag);
772 ADDCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
774 ADDCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
781 * ipcp_ackci - Ack our CIs.
782 * Called by fsm_rconfack, Receive Configure ACK.
789 ipcp_ackci(f, p, len)
794 ipcp_options *go = &ipcp_gotoptions[f->unit];
795 u_short cilen, citype, cishort;
797 u_char cimaxslotindex, cicflag;
800 * CIs must be in exactly the same order that we sent...
801 * Check packet length and CI length at each step.
802 * If we find any deviations, then this packet is bad.
805 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
807 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
808 if ((len -= vjlen) < 0) \
810 GETCHAR(citype, p); \
812 if (cilen != vjlen || \
815 GETSHORT(cishort, p); \
816 if (cishort != val) \
819 GETCHAR(cimaxslotindex, p); \
820 if (cimaxslotindex != maxslotindex) \
822 GETCHAR(cicflag, p); \
823 if (cicflag != cflag) \
828 #define ACKCIADDR(opt, neg, old, val1, val2) \
830 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
832 if ((len -= addrlen) < 0) \
834 GETCHAR(citype, p); \
836 if (cilen != addrlen || \
841 if (val1 != cilong) \
846 if (val2 != cilong) \
851 #define ACKCIDNS(opt, neg, addr) \
854 if ((len -= CILEN_ADDR) < 0) \
856 GETCHAR(citype, p); \
858 if (cilen != CILEN_ADDR || citype != opt) \
862 if (addr != cilong) \
866 ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
867 go->old_addrs, go->ouraddr, go->hisaddr);
869 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
870 go->maxslotindex, go->cflag);
872 ACKCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
874 ACKCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
877 * If there are any remaining CIs, then this packet is bad.
884 IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
889 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
890 * This should not modify any state if the Nak is bad
891 * or if IPCP is in the OPENED state.
892 * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
899 ipcp_nakci(f, p, len)
904 ipcp_options *go = &ipcp_gotoptions[f->unit];
905 u_char cimaxslotindex, cicflag;
906 u_char citype, cilen, *next;
908 u_int32_t ciaddr1, ciaddr2, l, cidnsaddr;
909 ipcp_options no; /* options we've seen Naks for */
910 ipcp_options try; /* options to request next time */
912 BZERO(&no, sizeof(no));
916 * Any Nak'd CIs must be in exactly the same order that we sent.
917 * Check packet length and CI length at each step.
918 * If we find any deviations, then this packet is bad.
920 #define NAKCIADDR(opt, neg, old, code) \
922 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
928 ciaddr1 = htonl(l); \
931 ciaddr2 = htonl(l); \
939 #define NAKCIVJ(opt, neg, code) \
941 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
946 GETSHORT(cishort, p); \
951 #define NAKCIDNS(opt, neg, code) \
953 ((cilen = p[1]) == CILEN_ADDR) && \
959 cidnsaddr = htonl(l); \
965 * Accept the peer's idea of {our,his} address, if different
966 * from our idea, only if the accept_{local,remote} flag is set.
968 NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
969 if (go->accept_local && ciaddr1) { /* Do we know our address? */
970 try.ouraddr = ciaddr1;
972 if (go->accept_remote && ciaddr2) { /* Does he know his? */
973 try.hisaddr = ciaddr2;
978 * Accept the peer's value of maxslotindex provided that it
979 * is less than what we asked for. Turn off slot-ID compression
980 * if the peer wants. Send old-style compress-type option if
983 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
984 if (cilen == CILEN_VJ) {
985 GETCHAR(cimaxslotindex, p);
987 if (cishort == IPCP_VJ_COMP) {
989 if (cimaxslotindex < go->maxslotindex)
990 try.maxslotindex = cimaxslotindex;
997 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
999 try.vj_protocol = cishort;
1006 NAKCIDNS(CI_MS_DNS1, req_dns1,
1007 try.dnsaddr[0] = cidnsaddr;
1010 NAKCIDNS(CI_MS_DNS2, req_dns2,
1011 try.dnsaddr[1] = cidnsaddr;
1015 * There may be remaining CIs, if the peer is requesting negotiation
1016 * on an option that we didn't include in our request packet.
1017 * If they want to negotiate about IP addresses, we comply.
1018 * If they want us to ask for compression, we refuse.
1020 while (len > CILEN_VOID) {
1023 if( (len -= cilen) < 0 )
1025 next = p + cilen - 2;
1028 case CI_COMPRESSTYPE:
1029 if (go->neg_vj || no.neg_vj ||
1030 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
1035 if ((go->neg_addr && go->old_addrs) || no.old_addrs
1036 || cilen != CILEN_ADDRS)
1042 if (ciaddr1 && go->accept_local)
1043 try.ouraddr = ciaddr1;
1046 if (ciaddr2 && go->accept_remote)
1047 try.hisaddr = ciaddr2;
1051 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
1056 if (ciaddr1 && go->accept_local)
1057 try.ouraddr = ciaddr1;
1058 if (try.ouraddr != 0)
1067 * OK, the Nak is good. Now we can update state.
1068 * If there are any remaining options, we ignore them.
1070 if (f->state != OPENED)
1076 IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
1082 * ipcp_rejci - Reject some of our CIs.
1083 * Callback from fsm_rconfnakrej.
1086 ipcp_rejci(f, p, len)
1091 ipcp_options *go = &ipcp_gotoptions[f->unit];
1092 u_char cimaxslotindex, ciflag, cilen;
1095 ipcp_options try; /* options to request next time */
1099 * Any Rejected CIs must be in exactly the same order that we sent.
1100 * Check packet length and CI length at each step.
1101 * If we find any deviations, then this packet is bad.
1103 #define REJCIADDR(opt, neg, old, val1, val2) \
1105 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
1112 cilong = htonl(l); \
1113 /* Check rejected value. */ \
1114 if (cilong != val1) \
1118 cilong = htonl(l); \
1119 /* Check rejected value. */ \
1120 if (cilong != val2) \
1126 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
1128 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
1133 GETSHORT(cishort, p); \
1134 /* Check rejected value. */ \
1135 if (cishort != val) \
1138 GETCHAR(cimaxslotindex, p); \
1139 if (cimaxslotindex != maxslot) \
1141 GETCHAR(ciflag, p); \
1142 if (ciflag != cflag) \
1148 #define REJCIDNS(opt, neg, dnsaddr) \
1150 ((cilen = p[1]) == CILEN_ADDR) && \
1157 cilong = htonl(l); \
1158 /* Check rejected value. */ \
1159 if (cilong != dnsaddr) \
1165 REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
1166 go->old_addrs, go->ouraddr, go->hisaddr);
1168 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
1169 go->maxslotindex, go->cflag);
1171 REJCIDNS(CI_MS_DNS1, req_dns1, go->dnsaddr[0]);
1173 REJCIDNS(CI_MS_DNS2, req_dns2, go->dnsaddr[1]);
1176 * If there are any remaining CIs, then this packet is bad.
1181 * Now we can update state.
1183 if (f->state != OPENED)
1188 IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
1194 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
1195 * Callback from fsm_rconfreq, Receive Configure Request
1197 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1198 * appropriately. If reject_if_disagree is non-zero, doesn't return
1199 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1202 ipcp_reqci(f, inp, len, reject_if_disagree)
1204 u_char *inp; /* Requested CIs */
1205 int *len; /* Length of requested CIs */
1206 int reject_if_disagree;
1208 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1209 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1210 ipcp_options *ao = &ipcp_allowoptions[f->unit];
1211 ipcp_options *go = &ipcp_gotoptions[f->unit];
1212 u_char *cip, *next; /* Pointer to current and next CIs */
1213 u_short cilen, citype; /* Parsed len, type */
1214 u_short cishort; /* Parsed short value */
1215 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
1216 int rc = CONFACK; /* Final packet return code */
1217 int orc; /* Individual option return code */
1218 u_char *p; /* Pointer to next char to parse */
1219 u_char *ucp = inp; /* Pointer to current output char */
1220 int l = *len; /* Length left */
1221 u_char maxslotindex, cflag;
1225 * Reset all his options.
1227 BZERO(ho, sizeof(*ho));
1230 * Process all his options.
1234 orc = CONFACK; /* Assume success */
1235 cip = p = next; /* Remember begining of CI */
1236 if (l < 2 || /* Not enough data for CI header or */
1237 p[1] < 2 || /* CI length too small or */
1238 p[1] > l) { /* CI length too big? */
1239 IPCPDEBUG(("ipcp_reqci: bad CI length!"));
1240 orc = CONFREJ; /* Reject bad CI */
1241 cilen = l; /* Reject till end of packet */
1242 l = 0; /* Don't loop again */
1245 GETCHAR(citype, p); /* Parse CI type */
1246 GETCHAR(cilen, p); /* Parse CI length */
1247 l -= cilen; /* Adjust remaining length */
1248 next += cilen; /* Step to next CI */
1250 switch (citype) { /* Check CI type */
1252 if (!ao->neg_addr ||
1253 cilen != CILEN_ADDRS) { /* Check CI length */
1254 orc = CONFREJ; /* Reject CI */
1259 * If he has no address, or if we both have his address but
1260 * disagree about it, then NAK it with our idea.
1261 * In particular, if we don't know his address, but he does,
1264 GETLONG(tl, p); /* Parse source address (his) */
1265 ciaddr1 = htonl(tl);
1266 if (ciaddr1 != wo->hisaddr
1267 && (ciaddr1 == 0 || !wo->accept_remote)) {
1269 if (!reject_if_disagree) {
1270 DECPTR(sizeof(u_int32_t), p);
1271 tl = ntohl(wo->hisaddr);
1274 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1276 * If neither we nor he knows his address, reject the option.
1279 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1284 * If he doesn't know our address, or if we both have our address
1285 * but disagree about it, then NAK it with our idea.
1287 GETLONG(tl, p); /* Parse desination address (ours) */
1288 ciaddr2 = htonl(tl);
1289 if (ciaddr2 != wo->ouraddr) {
1290 if (ciaddr2 == 0 || !wo->accept_local) {
1292 if (!reject_if_disagree) {
1293 DECPTR(sizeof(u_int32_t), p);
1294 tl = ntohl(wo->ouraddr);
1298 go->ouraddr = ciaddr2; /* accept peer's idea */
1304 ho->hisaddr = ciaddr1;
1305 ho->ouraddr = ciaddr2;
1309 if (!ao->neg_addr ||
1310 cilen != CILEN_ADDR) { /* Check CI length */
1311 orc = CONFREJ; /* Reject CI */
1316 * If he has no address, or if we both have his address but
1317 * disagree about it, then NAK it with our idea.
1318 * In particular, if we don't know his address, but he does,
1321 GETLONG(tl, p); /* Parse source address (his) */
1322 ciaddr1 = htonl(tl);
1323 if (ciaddr1 != wo->hisaddr
1324 && (ciaddr1 == 0 || !wo->accept_remote)) {
1326 if (!reject_if_disagree) {
1327 DECPTR(sizeof(u_int32_t), p);
1328 tl = ntohl(wo->hisaddr);
1331 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1333 * Don't ACK an address of 0.0.0.0 - reject it instead.
1336 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1341 ho->hisaddr = ciaddr1;
1346 /* Microsoft primary or secondary DNS request */
1347 d = citype == CI_MS_DNS2;
1349 /* If we do not have a DNS address then we cannot send it */
1350 if (ao->dnsaddr[d] == 0 ||
1351 cilen != CILEN_ADDR) { /* Check CI length */
1352 orc = CONFREJ; /* Reject CI */
1356 if (htonl(tl) != ao->dnsaddr[d]) {
1357 DECPTR(sizeof(u_int32_t), p);
1358 tl = ntohl(ao->dnsaddr[d]);
1366 /* Microsoft primary or secondary WINS request */
1367 d = citype == CI_MS_WINS2;
1369 /* If we do not have a DNS address then we cannot send it */
1370 if (ao->winsaddr[d] == 0 ||
1371 cilen != CILEN_ADDR) { /* Check CI length */
1372 orc = CONFREJ; /* Reject CI */
1376 if (htonl(tl) != ao->winsaddr[d]) {
1377 DECPTR(sizeof(u_int32_t), p);
1378 tl = ntohl(ao->winsaddr[d]);
1384 case CI_COMPRESSTYPE:
1386 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1390 GETSHORT(cishort, p);
1392 if (!(cishort == IPCP_VJ_COMP ||
1393 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1399 ho->vj_protocol = cishort;
1400 if (cilen == CILEN_VJ) {
1401 GETCHAR(maxslotindex, p);
1402 if (maxslotindex > ao->maxslotindex) {
1404 if (!reject_if_disagree){
1406 PUTCHAR(ao->maxslotindex, p);
1410 if (cflag && !ao->cflag) {
1412 if (!reject_if_disagree){
1414 PUTCHAR(wo->cflag, p);
1417 ho->maxslotindex = maxslotindex;
1421 ho->maxslotindex = MAX_STATES - 1;
1431 if (orc == CONFACK && /* Good CI */
1432 rc != CONFACK) /* but prior CI wasnt? */
1433 continue; /* Don't send this one */
1435 if (orc == CONFNAK) { /* Nak this CI? */
1436 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1437 orc = CONFREJ; /* Get tough if so */
1439 if (rc == CONFREJ) /* Rejecting prior CI? */
1440 continue; /* Don't send this one */
1441 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1442 rc = CONFNAK; /* Not anymore... */
1443 ucp = inp; /* Backup */
1448 if (orc == CONFREJ && /* Reject this CI */
1449 rc != CONFREJ) { /* but no prior ones? */
1451 ucp = inp; /* Backup */
1454 /* Need to move CI? */
1456 BCOPY(cip, ucp, cilen); /* Move it */
1458 /* Update output pointer */
1463 * If we aren't rejecting this packet, and we want to negotiate
1464 * their address, and they didn't send their address, then we
1465 * send a NAK with a CI_ADDR option appended. We assume the
1466 * input buffer is long enough that we can append the extra
1469 if (rc != CONFREJ && !ho->neg_addr &&
1470 wo->req_addr && !reject_if_disagree) {
1471 if (rc == CONFACK) {
1473 ucp = inp; /* reset pointer */
1474 wo->req_addr = 0; /* don't ask again */
1476 PUTCHAR(CI_ADDR, ucp);
1477 PUTCHAR(CILEN_ADDR, ucp);
1478 tl = ntohl(wo->hisaddr);
1482 *len = ucp - inp; /* Compute output length */
1483 IPCPDEBUG(("ipcp: returning Configure-%s", CODENAME(rc)));
1484 return (rc); /* Return final code */
1489 * ip_check_options - check that any IP-related options are OK,
1490 * and assign appropriate defaults.
1497 ipcp_options *wo = &ipcp_wantoptions[0];
1500 * Default our local IP address based on our hostname.
1501 * If local IP address already given, don't bother.
1503 if (wo->ouraddr == 0 && !disable_defaultip) {
1505 * Look up our hostname (possibly with domain name appended)
1506 * and take the first IP address as our local IP address.
1507 * If there isn't an IP address for our hostname, too bad.
1509 wo->accept_local = 1; /* don't insist on this default value */
1510 if ((hp = gethostbyname(hostname)) != NULL) {
1511 local = *(u_int32_t *)hp->h_addr;
1512 if (local != 0 && !bad_ip_adrs(local))
1513 wo->ouraddr = local;
1516 ask_for_local = wo->ouraddr != 0 || !disable_defaultip;
1521 * ip_demand_conf - configure the interface as though
1522 * IPCP were up, for use with dial-on-demand.
1528 ipcp_options *wo = &ipcp_wantoptions[u];
1530 if (wo->hisaddr == 0) {
1531 /* make up an arbitrary address for the peer */
1532 wo->hisaddr = htonl(0x0a707070 + ifunit);
1533 wo->accept_remote = 1;
1535 if (wo->ouraddr == 0) {
1536 /* make up an arbitrary address for us */
1537 wo->ouraddr = htonl(0x0a404040 + ifunit);
1538 wo->accept_local = 1;
1539 ask_for_local = 0; /* don't tell the peer this address */
1541 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1545 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1547 if (wo->default_route)
1548 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1549 default_route_set[u] = 1;
1551 if (sifproxyarp(u, wo->hisaddr))
1552 proxy_arp_set[u] = 1;
1554 notice("local IP address %I", wo->ouraddr);
1555 notice("remote IP address %I", wo->hisaddr);
1562 * ipcp_up - IPCP has come UP.
1564 * Configure the IP network interface appropriately and bring it up.
1571 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1572 ipcp_options *go = &ipcp_gotoptions[f->unit];
1573 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1575 IPCPDEBUG(("ipcp: up"));
1578 * We must have a non-zero IP address for both ends of the link.
1581 ho->hisaddr = wo->hisaddr;
1583 if (go->ouraddr == 0) {
1584 error("Could not determine local IP address");
1585 ipcp_close(f->unit, "Could not determine local IP address");
1588 if (ho->hisaddr == 0) {
1589 ho->hisaddr = htonl(0x0a404040 + ifunit);
1590 warn("Could not determine remote IP address: defaulting to %I",
1593 script_setenv("IPLOCAL", ip_ntoa(go->ouraddr), 0);
1594 script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr), 1);
1596 if (usepeerdns && (go->dnsaddr[0] || go->dnsaddr[1])) {
1597 script_setenv("USEPEERDNS", "1", 0);
1599 script_setenv("DNS1", ip_ntoa(go->dnsaddr[0]), 0);
1601 script_setenv("DNS2", ip_ntoa(go->dnsaddr[1]), 0);
1602 create_resolv(go->dnsaddr[0], go->dnsaddr[1]);
1606 * Check that the peer is allowed to use the IP address it wants.
1608 if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1609 error("Peer is not authorized to use remote address %I", ho->hisaddr);
1610 ipcp_close(f->unit, "Unauthorized remote IP address");
1614 /* set tcp compression */
1615 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1618 * If we are doing dial-on-demand, the interface is already
1619 * configured, so we put out any saved-up packets, then set the
1620 * interface to pass IP packets.
1623 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1624 ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr);
1625 if (go->ouraddr != wo->ouraddr) {
1626 warn("Local IP address changed to %I", go->ouraddr);
1627 script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr), 0);
1628 wo->ouraddr = go->ouraddr;
1630 script_unsetenv("OLDIPLOCAL");
1631 if (ho->hisaddr != wo->hisaddr) {
1632 warn("Remote IP address changed to %I", ho->hisaddr);
1633 script_setenv("OLDIPREMOTE", ip_ntoa(wo->hisaddr), 0);
1634 wo->hisaddr = ho->hisaddr;
1636 script_unsetenv("OLDIPREMOTE");
1638 /* Set the interface to the new addresses */
1639 mask = GetMask(go->ouraddr);
1640 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1642 warn("Interface configuration failed");
1643 ipcp_close(f->unit, "Interface configuration failed");
1647 /* assign a default route through the interface if required */
1648 if (ipcp_wantoptions[f->unit].default_route)
1649 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1650 default_route_set[f->unit] = 1;
1652 /* Make a proxy ARP entry if requested. */
1653 if (ipcp_wantoptions[f->unit].proxy_arp)
1654 if (sifproxyarp(f->unit, ho->hisaddr))
1655 proxy_arp_set[f->unit] = 1;
1658 demand_rexmit(PPP_IP);
1659 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1663 * Set IP addresses and (if specified) netmask.
1665 mask = GetMask(go->ouraddr);
1667 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1668 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1670 warn("Interface configuration failed");
1671 ipcp_close(f->unit, "Interface configuration failed");
1676 /* bring the interface up for IP */
1677 if (!sifup(f->unit)) {
1679 warn("Interface failed to come up");
1680 ipcp_close(f->unit, "Interface configuration failed");
1684 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1685 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1687 warn("Interface configuration failed");
1688 ipcp_close(f->unit, "Interface configuration failed");
1692 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1694 /* assign a default route through the interface if required */
1695 if (ipcp_wantoptions[f->unit].default_route)
1696 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1697 default_route_set[f->unit] = 1;
1699 /* Make a proxy ARP entry if requested. */
1700 if (ipcp_wantoptions[f->unit].proxy_arp)
1701 if (sifproxyarp(f->unit, ho->hisaddr))
1702 proxy_arp_set[f->unit] = 1;
1704 ipcp_wantoptions[0].ouraddr = go->ouraddr;
1706 notice("local IP address %I", go->ouraddr);
1707 notice("remote IP address %I", ho->hisaddr);
1709 notice("primary DNS address %I", go->dnsaddr[0]);
1711 notice("secondary DNS address %I", go->dnsaddr[1]);
1714 np_up(f->unit, PPP_IP);
1717 notify(ip_up_notifier, 0);
1722 * Execute the ip-up script, like this:
1723 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1725 if (ipcp_script_state == s_down && ipcp_script_pid == 0) {
1726 ipcp_script_state = s_up;
1727 ipcp_script(_PATH_IPUP);
1733 * ipcp_down - IPCP has gone DOWN.
1735 * Take the IP network interface down, clear its addresses
1736 * and delete routes through it.
1742 IPCPDEBUG(("ipcp: down"));
1743 /* XXX a bit IPv4-centric here, we only need to get the stats
1744 * before the interface is marked down. */
1745 update_link_stats(f->unit);
1746 notify(ip_down_notifier, 0);
1751 np_down(f->unit, PPP_IP);
1753 sifvjcomp(f->unit, 0, 0, 0);
1756 * If we are doing dial-on-demand, set the interface
1757 * to queue up outgoing packets (for now).
1760 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1762 sifnpmode(f->unit, PPP_IP, NPMODE_DROP);
1764 ipcp_clear_addrs(f->unit, ipcp_gotoptions[f->unit].ouraddr,
1765 ipcp_hisoptions[f->unit].hisaddr);
1768 /* Execute the ip-down script */
1769 if (ipcp_script_state == s_up && ipcp_script_pid == 0) {
1770 ipcp_script_state = s_down;
1771 ipcp_script(_PATH_IPDOWN);
1777 * ipcp_clear_addrs() - clear the interface addresses, routes,
1778 * proxy arp entries, etc.
1781 ipcp_clear_addrs(unit, ouraddr, hisaddr)
1783 u_int32_t ouraddr; /* local address */
1784 u_int32_t hisaddr; /* remote address */
1786 if (proxy_arp_set[unit]) {
1787 cifproxyarp(unit, hisaddr);
1788 proxy_arp_set[unit] = 0;
1790 if (default_route_set[unit]) {
1791 cifdefaultroute(unit, ouraddr, hisaddr);
1792 default_route_set[unit] = 0;
1794 cifaddr(unit, ouraddr, hisaddr);
1799 * ipcp_finished - possibly shut down the lower layers.
1805 np_finished(f->unit, PPP_IP);
1810 * ipcp_script_done - called when the ip-up or ip-down script
1814 ipcp_script_done(arg)
1817 ipcp_script_pid = 0;
1818 switch (ipcp_script_state) {
1820 if (ipcp_fsm[0].state != OPENED) {
1821 ipcp_script_state = s_down;
1822 ipcp_script(_PATH_IPDOWN);
1826 if (ipcp_fsm[0].state == OPENED) {
1827 ipcp_script_state = s_up;
1828 ipcp_script(_PATH_IPUP);
1836 * ipcp_script - Execute a script with arguments
1837 * interface-name tty-name speed local-IP remote-IP.
1843 char strspeed[32], strlocal[32], strremote[32];
1846 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
1847 slprintf(strlocal, sizeof(strlocal), "%I", ipcp_gotoptions[0].ouraddr);
1848 slprintf(strremote, sizeof(strremote), "%I", ipcp_hisoptions[0].hisaddr);
1855 argv[5] = strremote;
1858 ipcp_script_pid = run_program(script, argv, 0, ipcp_script_done, NULL);
1862 * create_resolv - create the replacement resolv.conf file
1865 create_resolv(peerdns1, peerdns2)
1866 u_int32_t peerdns1, peerdns2;
1870 f = fopen(_PATH_RESOLV, "w");
1872 error("Failed to create %s: %m", _PATH_RESOLV);
1877 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns1));
1880 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns2));
1883 error("Write failed to %s: %m", _PATH_RESOLV);
1889 * ipcp_printpkt - print the contents of an IPCP packet.
1891 static char *ipcp_codenames[] = {
1892 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1893 "TermReq", "TermAck", "CodeRej"
1897 ipcp_printpkt(p, plen, printer, arg)
1900 void (*printer) __P((void *, char *, ...));
1903 int code, id, len, olen;
1904 u_char *pstart, *optend;
1908 if (plen < HEADERLEN)
1914 if (len < HEADERLEN || len > plen)
1917 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1918 printer(arg, " %s", ipcp_codenames[code-1]);
1920 printer(arg, " code=0x%x", code);
1921 printer(arg, " id=0x%x", id);
1928 /* print option list */
1933 if (olen < 2 || olen > len) {
1941 if (olen == CILEN_ADDRS) {
1944 printer(arg, "addrs %I", htonl(cilong));
1946 printer(arg, " %I", htonl(cilong));
1949 case CI_COMPRESSTYPE:
1950 if (olen >= CILEN_COMPRESS) {
1952 GETSHORT(cishort, p);
1953 printer(arg, "compress ");
1958 case IPCP_VJ_COMP_OLD:
1959 printer(arg, "old-VJ");
1962 printer(arg, "0x%x", cishort);
1967 if (olen == CILEN_ADDR) {
1970 printer(arg, "addr %I", htonl(cilong));
1977 printer(arg, "ms-dns%d %I", code - CI_MS_DNS1 + 1,
1984 printer(arg, "ms-wins %I", htonl(cilong));
1987 while (p < optend) {
1989 printer(arg, " %.2x", code);
1997 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1999 print_string((char *)p, len, printer, arg);
2006 /* print the rest of the bytes in the packet */
2007 for (; len > 0; --len) {
2009 printer(arg, " %.2x", code);
2016 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
2017 * We don't bring the link up for IP fragments or for TCP FIN packets
2020 #define IP_HDRLEN 20 /* bytes */
2021 #define IP_OFFMASK 0x1fff
2023 #define IPPROTO_TCP 6
2025 #define TCP_HDRLEN 20
2029 * We use these macros because the IP header may be at an odd address,
2030 * and some compilers might use word loads to get th_off or ip_hl.
2033 #define net_short(x) (((x)[0] << 8) + (x)[1])
2034 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
2035 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
2036 #define get_ipproto(x) (((unsigned char *)(x))[9])
2037 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
2038 #define get_tcpflags(x) (((unsigned char *)(x))[13])
2041 ip_active_pkt(pkt, len)
2050 if (len < IP_HDRLEN)
2052 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
2054 if (get_ipproto(pkt) != IPPROTO_TCP)
2056 hlen = get_iphl(pkt) * 4;
2057 if (len < hlen + TCP_HDRLEN)
2060 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)