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.58 2001/04/27 23:13:06 paulus 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 static 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.
376 setipaddr(arg, argv, doit)
383 u_int32_t local, remote;
384 ipcp_options *wo = &ipcp_wantoptions[0];
385 static int prio_local = 0, prio_remote = 0;
388 * IP address pair separated by ":".
390 if ((colon = strchr(arg, ':')) == NULL)
396 * If colon first character, then no local addr.
398 if (colon != arg && option_priority >= prio_local) {
400 if ((local = inet_addr(arg)) == (u_int32_t) -1) {
401 if ((hp = gethostbyname(arg)) == NULL) {
402 option_error("unknown host: %s", arg);
405 local = *(u_int32_t *)hp->h_addr;
407 if (bad_ip_adrs(local)) {
408 option_error("bad local IP address %s", ip_ntoa(local));
414 prio_local = option_priority;
418 * If colon last character, then no remote addr.
420 if (*++colon != '\0' && option_priority >= prio_remote) {
421 if ((remote = inet_addr(colon)) == (u_int32_t) -1) {
422 if ((hp = gethostbyname(colon)) == NULL) {
423 option_error("unknown host: %s", colon);
426 remote = *(u_int32_t *)hp->h_addr;
427 if (remote_name[0] == 0)
428 strlcpy(remote_name, colon, sizeof(remote_name));
430 if (bad_ip_adrs(remote)) {
431 option_error("bad remote IP address %s", ip_ntoa(remote));
435 wo->hisaddr = remote;
436 prio_remote = option_priority;
443 printipaddr(opt, printer, arg)
445 void (*printer) __P((void *, char *, ...));
448 ipcp_options *wo = &ipcp_wantoptions[0];
450 if (wo->ouraddr != 0)
451 printer(arg, "%I", wo->ouraddr);
453 if (wo->hisaddr != 0)
454 printer(arg, "%I", wo->hisaddr);
458 * setnetmask - set the netmask to be used on the interface.
469 * Unfortunately, if we use inet_addr, we can't tell whether
470 * a result of all 1s is an error or a valid 255.255.255.255.
473 n = parse_dotted_ip(p, &mask);
477 if (n == 0 || p[n] != 0 || (netmask & ~mask) != 0) {
478 option_error("invalid netmask value '%s'", *argv);
483 slprintf(netmask_str, sizeof(netmask_str), "%I", mask);
489 parse_dotted_ip(p, vp)
499 b = strtoul(p, &endp, 0);
505 /* accept e.g. 0xffffff00 */
523 * ipcp_init - Initialize IPCP.
529 fsm *f = &ipcp_fsm[unit];
530 ipcp_options *wo = &ipcp_wantoptions[unit];
531 ipcp_options *ao = &ipcp_allowoptions[unit];
534 f->protocol = PPP_IPCP;
535 f->callbacks = &ipcp_callbacks;
536 fsm_init(&ipcp_fsm[unit]);
538 memset(wo, 0, sizeof(*wo));
539 memset(ao, 0, sizeof(*ao));
543 wo->vj_protocol = IPCP_VJ_COMP;
544 wo->maxslotindex = MAX_STATES - 1; /* really max index */
548 /* max slots and slot-id compression are currently hardwired in */
549 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
554 ao->maxslotindex = MAX_STATES - 1;
558 * XXX These control whether the user may use the proxyarp
559 * and defaultroute options.
562 ao->default_route = 1;
567 * ipcp_open - IPCP is allowed to come up.
573 fsm_open(&ipcp_fsm[unit]);
578 * ipcp_close - Take IPCP down.
581 ipcp_close(unit, reason)
585 fsm_close(&ipcp_fsm[unit], reason);
590 * ipcp_lowerup - The lower layer is up.
596 fsm_lowerup(&ipcp_fsm[unit]);
601 * ipcp_lowerdown - The lower layer is down.
607 fsm_lowerdown(&ipcp_fsm[unit]);
612 * ipcp_input - Input IPCP packet.
615 ipcp_input(unit, p, len)
620 fsm_input(&ipcp_fsm[unit], p, len);
625 * ipcp_protrej - A Protocol-Reject was received for IPCP.
627 * Pretend the lower layer went down, so we shut up.
633 fsm_lowerdown(&ipcp_fsm[unit]);
638 * ipcp_resetci - Reset our CI.
639 * Called by fsm_sconfreq, Send Configure Request.
645 ipcp_options *wo = &ipcp_wantoptions[f->unit];
646 ipcp_options *go = &ipcp_gotoptions[f->unit];
648 wo->req_addr = wo->neg_addr && ipcp_allowoptions[f->unit].neg_addr;
649 if (wo->ouraddr == 0)
650 wo->accept_local = 1;
651 if (wo->hisaddr == 0)
652 wo->accept_remote = 1;
653 wo->req_dns1 = usepeerdns; /* Request DNS addresses from the peer */
654 wo->req_dns2 = usepeerdns;
659 ip_choose_hook(&wo->hisaddr);
664 * ipcp_cilen - Return length of our CI.
665 * Called by fsm_sconfreq, Send Configure Request.
671 ipcp_options *go = &ipcp_gotoptions[f->unit];
672 ipcp_options *wo = &ipcp_wantoptions[f->unit];
673 ipcp_options *ho = &ipcp_hisoptions[f->unit];
675 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
676 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
677 #define LENCIDNS(neg) (neg ? (CILEN_ADDR) : 0)
680 * First see if we want to change our options to the old
681 * forms because we have received old forms from the peer.
683 if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
684 /* use the old style of address negotiation */
688 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
689 /* try an older style of VJ negotiation */
690 /* use the old style only if the peer did */
691 if (ho->neg_vj && ho->old_vj) {
694 go->vj_protocol = ho->vj_protocol;
698 return (LENCIADDR(go->neg_addr, go->old_addrs) +
699 LENCIVJ(go->neg_vj, go->old_vj) +
700 LENCIDNS(go->req_dns1) +
701 LENCIDNS(go->req_dns2)) ;
706 * ipcp_addci - Add our desired CIs to a packet.
707 * Called by fsm_sconfreq, Send Configure Request.
710 ipcp_addci(f, ucp, lenp)
715 ipcp_options *go = &ipcp_gotoptions[f->unit];
718 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
720 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
721 if (len >= vjlen) { \
723 PUTCHAR(vjlen, ucp); \
724 PUTSHORT(val, ucp); \
726 PUTCHAR(maxslotindex, ucp); \
727 PUTCHAR(cflag, ucp); \
734 #define ADDCIADDR(opt, neg, old, val1, val2) \
736 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
737 if (len >= addrlen) { \
740 PUTCHAR(addrlen, ucp); \
752 #define ADDCIDNS(opt, neg, addr) \
754 if (len >= CILEN_ADDR) { \
757 PUTCHAR(CILEN_ADDR, ucp); \
765 ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
766 go->old_addrs, go->ouraddr, go->hisaddr);
768 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
769 go->maxslotindex, go->cflag);
771 ADDCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
773 ADDCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
780 * ipcp_ackci - Ack our CIs.
781 * Called by fsm_rconfack, Receive Configure ACK.
788 ipcp_ackci(f, p, len)
793 ipcp_options *go = &ipcp_gotoptions[f->unit];
794 u_short cilen, citype, cishort;
796 u_char cimaxslotindex, cicflag;
799 * CIs must be in exactly the same order that we sent...
800 * Check packet length and CI length at each step.
801 * If we find any deviations, then this packet is bad.
804 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
806 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
807 if ((len -= vjlen) < 0) \
809 GETCHAR(citype, p); \
811 if (cilen != vjlen || \
814 GETSHORT(cishort, p); \
815 if (cishort != val) \
818 GETCHAR(cimaxslotindex, p); \
819 if (cimaxslotindex != maxslotindex) \
821 GETCHAR(cicflag, p); \
822 if (cicflag != cflag) \
827 #define ACKCIADDR(opt, neg, old, val1, val2) \
829 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
831 if ((len -= addrlen) < 0) \
833 GETCHAR(citype, p); \
835 if (cilen != addrlen || \
840 if (val1 != cilong) \
845 if (val2 != cilong) \
850 #define ACKCIDNS(opt, neg, addr) \
853 if ((len -= CILEN_ADDR) < 0) \
855 GETCHAR(citype, p); \
857 if (cilen != CILEN_ADDR || citype != opt) \
861 if (addr != cilong) \
865 ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
866 go->old_addrs, go->ouraddr, go->hisaddr);
868 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
869 go->maxslotindex, go->cflag);
871 ACKCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
873 ACKCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
876 * If there are any remaining CIs, then this packet is bad.
883 IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
888 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
889 * This should not modify any state if the Nak is bad
890 * or if IPCP is in the OPENED state.
891 * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
898 ipcp_nakci(f, p, len)
903 ipcp_options *go = &ipcp_gotoptions[f->unit];
904 u_char cimaxslotindex, cicflag;
905 u_char citype, cilen, *next;
907 u_int32_t ciaddr1, ciaddr2, l, cidnsaddr;
908 ipcp_options no; /* options we've seen Naks for */
909 ipcp_options try; /* options to request next time */
911 BZERO(&no, sizeof(no));
915 * Any Nak'd CIs must be in exactly the same order that we sent.
916 * Check packet length and CI length at each step.
917 * If we find any deviations, then this packet is bad.
919 #define NAKCIADDR(opt, neg, old, code) \
921 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
927 ciaddr1 = htonl(l); \
930 ciaddr2 = htonl(l); \
938 #define NAKCIVJ(opt, neg, code) \
940 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
945 GETSHORT(cishort, p); \
950 #define NAKCIDNS(opt, neg, code) \
952 ((cilen = p[1]) == CILEN_ADDR) && \
958 cidnsaddr = htonl(l); \
964 * Accept the peer's idea of {our,his} address, if different
965 * from our idea, only if the accept_{local,remote} flag is set.
967 NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
968 if (go->accept_local && ciaddr1) { /* Do we know our address? */
969 try.ouraddr = ciaddr1;
971 if (go->accept_remote && ciaddr2) { /* Does he know his? */
972 try.hisaddr = ciaddr2;
977 * Accept the peer's value of maxslotindex provided that it
978 * is less than what we asked for. Turn off slot-ID compression
979 * if the peer wants. Send old-style compress-type option if
982 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
983 if (cilen == CILEN_VJ) {
984 GETCHAR(cimaxslotindex, p);
986 if (cishort == IPCP_VJ_COMP) {
988 if (cimaxslotindex < go->maxslotindex)
989 try.maxslotindex = cimaxslotindex;
996 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
998 try.vj_protocol = cishort;
1005 NAKCIDNS(CI_MS_DNS1, req_dns1,
1006 try.dnsaddr[0] = cidnsaddr;
1009 NAKCIDNS(CI_MS_DNS2, req_dns2,
1010 try.dnsaddr[1] = cidnsaddr;
1014 * There may be remaining CIs, if the peer is requesting negotiation
1015 * on an option that we didn't include in our request packet.
1016 * If they want to negotiate about IP addresses, we comply.
1017 * If they want us to ask for compression, we refuse.
1019 while (len > CILEN_VOID) {
1022 if( (len -= cilen) < 0 )
1024 next = p + cilen - 2;
1027 case CI_COMPRESSTYPE:
1028 if (go->neg_vj || no.neg_vj ||
1029 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
1034 if ((go->neg_addr && go->old_addrs) || no.old_addrs
1035 || cilen != CILEN_ADDRS)
1041 if (ciaddr1 && go->accept_local)
1042 try.ouraddr = ciaddr1;
1045 if (ciaddr2 && go->accept_remote)
1046 try.hisaddr = ciaddr2;
1050 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
1055 if (ciaddr1 && go->accept_local)
1056 try.ouraddr = ciaddr1;
1057 if (try.ouraddr != 0)
1066 * OK, the Nak is good. Now we can update state.
1067 * If there are any remaining options, we ignore them.
1069 if (f->state != OPENED)
1075 IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
1081 * ipcp_rejci - Reject some of our CIs.
1082 * Callback from fsm_rconfnakrej.
1085 ipcp_rejci(f, p, len)
1090 ipcp_options *go = &ipcp_gotoptions[f->unit];
1091 u_char cimaxslotindex, ciflag, cilen;
1094 ipcp_options try; /* options to request next time */
1098 * Any Rejected CIs must be in exactly the same order that we sent.
1099 * Check packet length and CI length at each step.
1100 * If we find any deviations, then this packet is bad.
1102 #define REJCIADDR(opt, neg, old, val1, val2) \
1104 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
1111 cilong = htonl(l); \
1112 /* Check rejected value. */ \
1113 if (cilong != val1) \
1117 cilong = htonl(l); \
1118 /* Check rejected value. */ \
1119 if (cilong != val2) \
1125 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
1127 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
1132 GETSHORT(cishort, p); \
1133 /* Check rejected value. */ \
1134 if (cishort != val) \
1137 GETCHAR(cimaxslotindex, p); \
1138 if (cimaxslotindex != maxslot) \
1140 GETCHAR(ciflag, p); \
1141 if (ciflag != cflag) \
1147 #define REJCIDNS(opt, neg, dnsaddr) \
1149 ((cilen = p[1]) == CILEN_ADDR) && \
1156 cilong = htonl(l); \
1157 /* Check rejected value. */ \
1158 if (cilong != dnsaddr) \
1164 REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
1165 go->old_addrs, go->ouraddr, go->hisaddr);
1167 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
1168 go->maxslotindex, go->cflag);
1170 REJCIDNS(CI_MS_DNS1, req_dns1, go->dnsaddr[0]);
1172 REJCIDNS(CI_MS_DNS2, req_dns2, go->dnsaddr[1]);
1175 * If there are any remaining CIs, then this packet is bad.
1180 * Now we can update state.
1182 if (f->state != OPENED)
1187 IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
1193 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
1194 * Callback from fsm_rconfreq, Receive Configure Request
1196 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1197 * appropriately. If reject_if_disagree is non-zero, doesn't return
1198 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1201 ipcp_reqci(f, inp, len, reject_if_disagree)
1203 u_char *inp; /* Requested CIs */
1204 int *len; /* Length of requested CIs */
1205 int reject_if_disagree;
1207 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1208 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1209 ipcp_options *ao = &ipcp_allowoptions[f->unit];
1210 ipcp_options *go = &ipcp_gotoptions[f->unit];
1211 u_char *cip, *next; /* Pointer to current and next CIs */
1212 u_short cilen, citype; /* Parsed len, type */
1213 u_short cishort; /* Parsed short value */
1214 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
1215 int rc = CONFACK; /* Final packet return code */
1216 int orc; /* Individual option return code */
1217 u_char *p; /* Pointer to next char to parse */
1218 u_char *ucp = inp; /* Pointer to current output char */
1219 int l = *len; /* Length left */
1220 u_char maxslotindex, cflag;
1224 * Reset all his options.
1226 BZERO(ho, sizeof(*ho));
1229 * Process all his options.
1233 orc = CONFACK; /* Assume success */
1234 cip = p = next; /* Remember begining of CI */
1235 if (l < 2 || /* Not enough data for CI header or */
1236 p[1] < 2 || /* CI length too small or */
1237 p[1] > l) { /* CI length too big? */
1238 IPCPDEBUG(("ipcp_reqci: bad CI length!"));
1239 orc = CONFREJ; /* Reject bad CI */
1240 cilen = l; /* Reject till end of packet */
1241 l = 0; /* Don't loop again */
1244 GETCHAR(citype, p); /* Parse CI type */
1245 GETCHAR(cilen, p); /* Parse CI length */
1246 l -= cilen; /* Adjust remaining length */
1247 next += cilen; /* Step to next CI */
1249 switch (citype) { /* Check CI type */
1251 if (!ao->neg_addr ||
1252 cilen != CILEN_ADDRS) { /* Check CI length */
1253 orc = CONFREJ; /* Reject CI */
1258 * If he has no address, or if we both have his address but
1259 * disagree about it, then NAK it with our idea.
1260 * In particular, if we don't know his address, but he does,
1263 GETLONG(tl, p); /* Parse source address (his) */
1264 ciaddr1 = htonl(tl);
1265 if (ciaddr1 != wo->hisaddr
1266 && (ciaddr1 == 0 || !wo->accept_remote)) {
1268 if (!reject_if_disagree) {
1269 DECPTR(sizeof(u_int32_t), p);
1270 tl = ntohl(wo->hisaddr);
1273 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1275 * If neither we nor he knows his address, reject the option.
1278 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1283 * If he doesn't know our address, or if we both have our address
1284 * but disagree about it, then NAK it with our idea.
1286 GETLONG(tl, p); /* Parse desination address (ours) */
1287 ciaddr2 = htonl(tl);
1288 if (ciaddr2 != wo->ouraddr) {
1289 if (ciaddr2 == 0 || !wo->accept_local) {
1291 if (!reject_if_disagree) {
1292 DECPTR(sizeof(u_int32_t), p);
1293 tl = ntohl(wo->ouraddr);
1297 go->ouraddr = ciaddr2; /* accept peer's idea */
1303 ho->hisaddr = ciaddr1;
1304 ho->ouraddr = ciaddr2;
1308 if (!ao->neg_addr ||
1309 cilen != CILEN_ADDR) { /* Check CI length */
1310 orc = CONFREJ; /* Reject CI */
1315 * If he has no address, or if we both have his address but
1316 * disagree about it, then NAK it with our idea.
1317 * In particular, if we don't know his address, but he does,
1320 GETLONG(tl, p); /* Parse source address (his) */
1321 ciaddr1 = htonl(tl);
1322 if (ciaddr1 != wo->hisaddr
1323 && (ciaddr1 == 0 || !wo->accept_remote)) {
1325 if (!reject_if_disagree) {
1326 DECPTR(sizeof(u_int32_t), p);
1327 tl = ntohl(wo->hisaddr);
1330 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1332 * Don't ACK an address of 0.0.0.0 - reject it instead.
1335 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1340 ho->hisaddr = ciaddr1;
1345 /* Microsoft primary or secondary DNS request */
1346 d = citype == CI_MS_DNS2;
1348 /* If we do not have a DNS address then we cannot send it */
1349 if (ao->dnsaddr[d] == 0 ||
1350 cilen != CILEN_ADDR) { /* Check CI length */
1351 orc = CONFREJ; /* Reject CI */
1355 if (htonl(tl) != ao->dnsaddr[d]) {
1356 DECPTR(sizeof(u_int32_t), p);
1357 tl = ntohl(ao->dnsaddr[d]);
1365 /* Microsoft primary or secondary WINS request */
1366 d = citype == CI_MS_WINS2;
1368 /* If we do not have a DNS address then we cannot send it */
1369 if (ao->winsaddr[d] == 0 ||
1370 cilen != CILEN_ADDR) { /* Check CI length */
1371 orc = CONFREJ; /* Reject CI */
1375 if (htonl(tl) != ao->winsaddr[d]) {
1376 DECPTR(sizeof(u_int32_t), p);
1377 tl = ntohl(ao->winsaddr[d]);
1383 case CI_COMPRESSTYPE:
1385 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1389 GETSHORT(cishort, p);
1391 if (!(cishort == IPCP_VJ_COMP ||
1392 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1398 ho->vj_protocol = cishort;
1399 if (cilen == CILEN_VJ) {
1400 GETCHAR(maxslotindex, p);
1401 if (maxslotindex > ao->maxslotindex) {
1403 if (!reject_if_disagree){
1405 PUTCHAR(ao->maxslotindex, p);
1409 if (cflag && !ao->cflag) {
1411 if (!reject_if_disagree){
1413 PUTCHAR(wo->cflag, p);
1416 ho->maxslotindex = maxslotindex;
1420 ho->maxslotindex = MAX_STATES - 1;
1430 if (orc == CONFACK && /* Good CI */
1431 rc != CONFACK) /* but prior CI wasnt? */
1432 continue; /* Don't send this one */
1434 if (orc == CONFNAK) { /* Nak this CI? */
1435 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1436 orc = CONFREJ; /* Get tough if so */
1438 if (rc == CONFREJ) /* Rejecting prior CI? */
1439 continue; /* Don't send this one */
1440 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1441 rc = CONFNAK; /* Not anymore... */
1442 ucp = inp; /* Backup */
1447 if (orc == CONFREJ && /* Reject this CI */
1448 rc != CONFREJ) { /* but no prior ones? */
1450 ucp = inp; /* Backup */
1453 /* Need to move CI? */
1455 BCOPY(cip, ucp, cilen); /* Move it */
1457 /* Update output pointer */
1462 * If we aren't rejecting this packet, and we want to negotiate
1463 * their address, and they didn't send their address, then we
1464 * send a NAK with a CI_ADDR option appended. We assume the
1465 * input buffer is long enough that we can append the extra
1468 if (rc != CONFREJ && !ho->neg_addr &&
1469 wo->req_addr && !reject_if_disagree) {
1470 if (rc == CONFACK) {
1472 ucp = inp; /* reset pointer */
1473 wo->req_addr = 0; /* don't ask again */
1475 PUTCHAR(CI_ADDR, ucp);
1476 PUTCHAR(CILEN_ADDR, ucp);
1477 tl = ntohl(wo->hisaddr);
1481 *len = ucp - inp; /* Compute output length */
1482 IPCPDEBUG(("ipcp: returning Configure-%s", CODENAME(rc)));
1483 return (rc); /* Return final code */
1488 * ip_check_options - check that any IP-related options are OK,
1489 * and assign appropriate defaults.
1496 ipcp_options *wo = &ipcp_wantoptions[0];
1499 * Default our local IP address based on our hostname.
1500 * If local IP address already given, don't bother.
1502 if (wo->ouraddr == 0 && !disable_defaultip) {
1504 * Look up our hostname (possibly with domain name appended)
1505 * and take the first IP address as our local IP address.
1506 * If there isn't an IP address for our hostname, too bad.
1508 wo->accept_local = 1; /* don't insist on this default value */
1509 if ((hp = gethostbyname(hostname)) != NULL) {
1510 local = *(u_int32_t *)hp->h_addr;
1511 if (local != 0 && !bad_ip_adrs(local))
1512 wo->ouraddr = local;
1515 ask_for_local = wo->ouraddr != 0 || !disable_defaultip;
1520 * ip_demand_conf - configure the interface as though
1521 * IPCP were up, for use with dial-on-demand.
1527 ipcp_options *wo = &ipcp_wantoptions[u];
1529 if (wo->hisaddr == 0) {
1530 /* make up an arbitrary address for the peer */
1531 wo->hisaddr = htonl(0x0a707070 + ifunit);
1532 wo->accept_remote = 1;
1534 if (wo->ouraddr == 0) {
1535 /* make up an arbitrary address for us */
1536 wo->ouraddr = htonl(0x0a404040 + ifunit);
1537 wo->accept_local = 1;
1538 ask_for_local = 0; /* don't tell the peer this address */
1540 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1544 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1546 if (wo->default_route)
1547 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1548 default_route_set[u] = 1;
1550 if (sifproxyarp(u, wo->hisaddr))
1551 proxy_arp_set[u] = 1;
1553 notice("local IP address %I", wo->ouraddr);
1554 notice("remote IP address %I", wo->hisaddr);
1561 * ipcp_up - IPCP has come UP.
1563 * Configure the IP network interface appropriately and bring it up.
1570 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1571 ipcp_options *go = &ipcp_gotoptions[f->unit];
1572 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1574 IPCPDEBUG(("ipcp: up"));
1577 * We must have a non-zero IP address for both ends of the link.
1580 ho->hisaddr = wo->hisaddr;
1582 if (go->ouraddr == 0) {
1583 error("Could not determine local IP address");
1584 ipcp_close(f->unit, "Could not determine local IP address");
1587 if (ho->hisaddr == 0) {
1588 ho->hisaddr = htonl(0x0a404040 + ifunit);
1589 warn("Could not determine remote IP address: defaulting to %I",
1592 script_setenv("IPLOCAL", ip_ntoa(go->ouraddr), 0);
1593 script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr), 1);
1595 if (usepeerdns && (go->dnsaddr[0] || go->dnsaddr[1])) {
1596 script_setenv("USEPEERDNS", "1", 0);
1598 script_setenv("DNS1", ip_ntoa(go->dnsaddr[0]), 0);
1600 script_setenv("DNS2", ip_ntoa(go->dnsaddr[1]), 0);
1601 create_resolv(go->dnsaddr[0], go->dnsaddr[1]);
1605 * Check that the peer is allowed to use the IP address it wants.
1607 if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1608 error("Peer is not authorized to use remote address %I", ho->hisaddr);
1609 ipcp_close(f->unit, "Unauthorized remote IP address");
1613 /* set tcp compression */
1614 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1617 * If we are doing dial-on-demand, the interface is already
1618 * configured, so we put out any saved-up packets, then set the
1619 * interface to pass IP packets.
1622 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1623 ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr);
1624 if (go->ouraddr != wo->ouraddr) {
1625 warn("Local IP address changed to %I", go->ouraddr);
1626 script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr), 0);
1627 wo->ouraddr = go->ouraddr;
1629 script_unsetenv("OLDIPLOCAL");
1630 if (ho->hisaddr != wo->hisaddr) {
1631 warn("Remote IP address changed to %I", ho->hisaddr);
1632 script_setenv("OLDIPREMOTE", ip_ntoa(wo->hisaddr), 0);
1633 wo->hisaddr = ho->hisaddr;
1635 script_unsetenv("OLDIPREMOTE");
1637 /* Set the interface to the new addresses */
1638 mask = GetMask(go->ouraddr);
1639 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1641 warn("Interface configuration failed");
1642 ipcp_close(f->unit, "Interface configuration failed");
1646 /* assign a default route through the interface if required */
1647 if (ipcp_wantoptions[f->unit].default_route)
1648 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1649 default_route_set[f->unit] = 1;
1651 /* Make a proxy ARP entry if requested. */
1652 if (ipcp_wantoptions[f->unit].proxy_arp)
1653 if (sifproxyarp(f->unit, ho->hisaddr))
1654 proxy_arp_set[f->unit] = 1;
1657 demand_rexmit(PPP_IP);
1658 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1662 * Set IP addresses and (if specified) netmask.
1664 mask = GetMask(go->ouraddr);
1666 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1667 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1669 warn("Interface configuration failed");
1670 ipcp_close(f->unit, "Interface configuration failed");
1675 /* bring the interface up for IP */
1676 if (!sifup(f->unit)) {
1678 warn("Interface failed to come up");
1679 ipcp_close(f->unit, "Interface configuration failed");
1683 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1684 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1686 warn("Interface configuration failed");
1687 ipcp_close(f->unit, "Interface configuration failed");
1691 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1693 /* assign a default route through the interface if required */
1694 if (ipcp_wantoptions[f->unit].default_route)
1695 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1696 default_route_set[f->unit] = 1;
1698 /* Make a proxy ARP entry if requested. */
1699 if (ipcp_wantoptions[f->unit].proxy_arp)
1700 if (sifproxyarp(f->unit, ho->hisaddr))
1701 proxy_arp_set[f->unit] = 1;
1703 ipcp_wantoptions[0].ouraddr = go->ouraddr;
1705 notice("local IP address %I", go->ouraddr);
1706 notice("remote IP address %I", ho->hisaddr);
1708 notice("primary DNS address %I", go->dnsaddr[0]);
1710 notice("secondary DNS address %I", go->dnsaddr[1]);
1713 np_up(f->unit, PPP_IP);
1716 notify(ip_up_notifier, 0);
1721 * Execute the ip-up script, like this:
1722 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1724 if (ipcp_script_state == s_down && ipcp_script_pid == 0) {
1725 ipcp_script_state = s_up;
1726 ipcp_script(_PATH_IPUP);
1732 * ipcp_down - IPCP has gone DOWN.
1734 * Take the IP network interface down, clear its addresses
1735 * and delete routes through it.
1741 IPCPDEBUG(("ipcp: down"));
1742 /* XXX a bit IPv4-centric here, we only need to get the stats
1743 * before the interface is marked down. */
1744 update_link_stats(f->unit);
1745 notify(ip_down_notifier, 0);
1750 np_down(f->unit, PPP_IP);
1752 sifvjcomp(f->unit, 0, 0, 0);
1755 * If we are doing dial-on-demand, set the interface
1756 * to queue up outgoing packets (for now).
1759 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1761 sifnpmode(f->unit, PPP_IP, NPMODE_DROP);
1763 ipcp_clear_addrs(f->unit, ipcp_gotoptions[f->unit].ouraddr,
1764 ipcp_hisoptions[f->unit].hisaddr);
1767 /* Execute the ip-down script */
1768 if (ipcp_script_state == s_up && ipcp_script_pid == 0) {
1769 ipcp_script_state = s_down;
1770 ipcp_script(_PATH_IPDOWN);
1776 * ipcp_clear_addrs() - clear the interface addresses, routes,
1777 * proxy arp entries, etc.
1780 ipcp_clear_addrs(unit, ouraddr, hisaddr)
1782 u_int32_t ouraddr; /* local address */
1783 u_int32_t hisaddr; /* remote address */
1785 if (proxy_arp_set[unit]) {
1786 cifproxyarp(unit, hisaddr);
1787 proxy_arp_set[unit] = 0;
1789 if (default_route_set[unit]) {
1790 cifdefaultroute(unit, ouraddr, hisaddr);
1791 default_route_set[unit] = 0;
1793 cifaddr(unit, ouraddr, hisaddr);
1798 * ipcp_finished - possibly shut down the lower layers.
1804 np_finished(f->unit, PPP_IP);
1809 * ipcp_script_done - called when the ip-up or ip-down script
1813 ipcp_script_done(arg)
1816 ipcp_script_pid = 0;
1817 switch (ipcp_script_state) {
1819 if (ipcp_fsm[0].state != OPENED) {
1820 ipcp_script_state = s_down;
1821 ipcp_script(_PATH_IPDOWN);
1825 if (ipcp_fsm[0].state == OPENED) {
1826 ipcp_script_state = s_up;
1827 ipcp_script(_PATH_IPUP);
1835 * ipcp_script - Execute a script with arguments
1836 * interface-name tty-name speed local-IP remote-IP.
1842 char strspeed[32], strlocal[32], strremote[32];
1845 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
1846 slprintf(strlocal, sizeof(strlocal), "%I", ipcp_gotoptions[0].ouraddr);
1847 slprintf(strremote, sizeof(strremote), "%I", ipcp_hisoptions[0].hisaddr);
1854 argv[5] = strremote;
1857 ipcp_script_pid = run_program(script, argv, 0, ipcp_script_done, NULL);
1861 * create_resolv - create the replacement resolv.conf file
1864 create_resolv(peerdns1, peerdns2)
1865 u_int32_t peerdns1, peerdns2;
1869 f = fopen(_PATH_RESOLV, "w");
1871 error("Failed to create %s: %m", _PATH_RESOLV);
1876 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns1));
1879 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns2));
1882 error("Write failed to %s: %m", _PATH_RESOLV);
1888 * ipcp_printpkt - print the contents of an IPCP packet.
1890 static char *ipcp_codenames[] = {
1891 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1892 "TermReq", "TermAck", "CodeRej"
1896 ipcp_printpkt(p, plen, printer, arg)
1899 void (*printer) __P((void *, char *, ...));
1902 int code, id, len, olen;
1903 u_char *pstart, *optend;
1907 if (plen < HEADERLEN)
1913 if (len < HEADERLEN || len > plen)
1916 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1917 printer(arg, " %s", ipcp_codenames[code-1]);
1919 printer(arg, " code=0x%x", code);
1920 printer(arg, " id=0x%x", id);
1927 /* print option list */
1932 if (olen < 2 || olen > len) {
1940 if (olen == CILEN_ADDRS) {
1943 printer(arg, "addrs %I", htonl(cilong));
1945 printer(arg, " %I", htonl(cilong));
1948 case CI_COMPRESSTYPE:
1949 if (olen >= CILEN_COMPRESS) {
1951 GETSHORT(cishort, p);
1952 printer(arg, "compress ");
1957 case IPCP_VJ_COMP_OLD:
1958 printer(arg, "old-VJ");
1961 printer(arg, "0x%x", cishort);
1966 if (olen == CILEN_ADDR) {
1969 printer(arg, "addr %I", htonl(cilong));
1976 printer(arg, "ms-dns%d %I", code - CI_MS_DNS1 + 1,
1983 printer(arg, "ms-wins %I", htonl(cilong));
1986 while (p < optend) {
1988 printer(arg, " %.2x", code);
1996 if (len > 0 && *p >= ' ' && *p < 0x7f) {
1998 print_string((char *)p, len, printer, arg);
2005 /* print the rest of the bytes in the packet */
2006 for (; len > 0; --len) {
2008 printer(arg, " %.2x", code);
2015 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
2016 * We don't bring the link up for IP fragments or for TCP FIN packets
2019 #define IP_HDRLEN 20 /* bytes */
2020 #define IP_OFFMASK 0x1fff
2021 #define IPPROTO_TCP 6
2022 #define TCP_HDRLEN 20
2026 * We use these macros because the IP header may be at an odd address,
2027 * and some compilers might use word loads to get th_off or ip_hl.
2030 #define net_short(x) (((x)[0] << 8) + (x)[1])
2031 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
2032 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
2033 #define get_ipproto(x) (((unsigned char *)(x))[9])
2034 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
2035 #define get_tcpflags(x) (((unsigned char *)(x))[13])
2038 ip_active_pkt(pkt, len)
2047 if (len < IP_HDRLEN)
2049 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
2051 if (get_ipproto(pkt) != IPPROTO_TCP)
2053 hlen = get_iphl(pkt) * 4;
2054 if (len < hlen + TCP_HDRLEN)
2057 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)