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.60 2002/01/22 16:02:58 dfs 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;
659 if (ip_choose_hook) {
660 ip_choose_hook(&wo->hisaddr);
662 wo->accept_remote = 0;
669 * ipcp_cilen - Return length of our CI.
670 * Called by fsm_sconfreq, Send Configure Request.
676 ipcp_options *go = &ipcp_gotoptions[f->unit];
677 ipcp_options *wo = &ipcp_wantoptions[f->unit];
678 ipcp_options *ho = &ipcp_hisoptions[f->unit];
680 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
681 #define LENCIADDR(neg, old) (neg ? (old? CILEN_ADDRS : CILEN_ADDR) : 0)
682 #define LENCIDNS(neg) (neg ? (CILEN_ADDR) : 0)
685 * First see if we want to change our options to the old
686 * forms because we have received old forms from the peer.
688 if (wo->neg_addr && !go->neg_addr && !go->old_addrs) {
689 /* use the old style of address negotiation */
693 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
694 /* try an older style of VJ negotiation */
695 /* use the old style only if the peer did */
696 if (ho->neg_vj && ho->old_vj) {
699 go->vj_protocol = ho->vj_protocol;
703 return (LENCIADDR(go->neg_addr, go->old_addrs) +
704 LENCIVJ(go->neg_vj, go->old_vj) +
705 LENCIDNS(go->req_dns1) +
706 LENCIDNS(go->req_dns2)) ;
711 * ipcp_addci - Add our desired CIs to a packet.
712 * Called by fsm_sconfreq, Send Configure Request.
715 ipcp_addci(f, ucp, lenp)
720 ipcp_options *go = &ipcp_gotoptions[f->unit];
723 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
725 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
726 if (len >= vjlen) { \
728 PUTCHAR(vjlen, ucp); \
729 PUTSHORT(val, ucp); \
731 PUTCHAR(maxslotindex, ucp); \
732 PUTCHAR(cflag, ucp); \
739 #define ADDCIADDR(opt, neg, old, val1, val2) \
741 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
742 if (len >= addrlen) { \
745 PUTCHAR(addrlen, ucp); \
757 #define ADDCIDNS(opt, neg, addr) \
759 if (len >= CILEN_ADDR) { \
762 PUTCHAR(CILEN_ADDR, ucp); \
770 ADDCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
771 go->old_addrs, go->ouraddr, go->hisaddr);
773 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
774 go->maxslotindex, go->cflag);
776 ADDCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
778 ADDCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
785 * ipcp_ackci - Ack our CIs.
786 * Called by fsm_rconfack, Receive Configure ACK.
793 ipcp_ackci(f, p, len)
798 ipcp_options *go = &ipcp_gotoptions[f->unit];
799 u_short cilen, citype, cishort;
801 u_char cimaxslotindex, cicflag;
804 * CIs must be in exactly the same order that we sent...
805 * Check packet length and CI length at each step.
806 * If we find any deviations, then this packet is bad.
809 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
811 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
812 if ((len -= vjlen) < 0) \
814 GETCHAR(citype, p); \
816 if (cilen != vjlen || \
819 GETSHORT(cishort, p); \
820 if (cishort != val) \
823 GETCHAR(cimaxslotindex, p); \
824 if (cimaxslotindex != maxslotindex) \
826 GETCHAR(cicflag, p); \
827 if (cicflag != cflag) \
832 #define ACKCIADDR(opt, neg, old, val1, val2) \
834 int addrlen = (old? CILEN_ADDRS: CILEN_ADDR); \
836 if ((len -= addrlen) < 0) \
838 GETCHAR(citype, p); \
840 if (cilen != addrlen || \
845 if (val1 != cilong) \
850 if (val2 != cilong) \
855 #define ACKCIDNS(opt, neg, addr) \
858 if ((len -= CILEN_ADDR) < 0) \
860 GETCHAR(citype, p); \
862 if (cilen != CILEN_ADDR || citype != opt) \
866 if (addr != cilong) \
870 ACKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), go->neg_addr,
871 go->old_addrs, go->ouraddr, go->hisaddr);
873 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
874 go->maxslotindex, go->cflag);
876 ACKCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
878 ACKCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
881 * If there are any remaining CIs, then this packet is bad.
888 IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
893 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
894 * This should not modify any state if the Nak is bad
895 * or if IPCP is in the OPENED state.
896 * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
903 ipcp_nakci(f, p, len)
908 ipcp_options *go = &ipcp_gotoptions[f->unit];
909 u_char cimaxslotindex, cicflag;
910 u_char citype, cilen, *next;
912 u_int32_t ciaddr1, ciaddr2, l, cidnsaddr;
913 ipcp_options no; /* options we've seen Naks for */
914 ipcp_options try; /* options to request next time */
916 BZERO(&no, sizeof(no));
920 * Any Nak'd CIs must be in exactly the same order that we sent.
921 * Check packet length and CI length at each step.
922 * If we find any deviations, then this packet is bad.
924 #define NAKCIADDR(opt, neg, old, code) \
926 len >= (cilen = (old? CILEN_ADDRS: CILEN_ADDR)) && \
932 ciaddr1 = htonl(l); \
935 ciaddr2 = htonl(l); \
943 #define NAKCIVJ(opt, neg, code) \
945 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
950 GETSHORT(cishort, p); \
955 #define NAKCIDNS(opt, neg, code) \
957 ((cilen = p[1]) == CILEN_ADDR) && \
963 cidnsaddr = htonl(l); \
969 * Accept the peer's idea of {our,his} address, if different
970 * from our idea, only if the accept_{local,remote} flag is set.
972 NAKCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr, go->old_addrs,
973 if (go->accept_local && ciaddr1) { /* Do we know our address? */
974 try.ouraddr = ciaddr1;
976 if (go->accept_remote && ciaddr2) { /* Does he know his? */
977 try.hisaddr = ciaddr2;
982 * Accept the peer's value of maxslotindex provided that it
983 * is less than what we asked for. Turn off slot-ID compression
984 * if the peer wants. Send old-style compress-type option if
987 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
988 if (cilen == CILEN_VJ) {
989 GETCHAR(cimaxslotindex, p);
991 if (cishort == IPCP_VJ_COMP) {
993 if (cimaxslotindex < go->maxslotindex)
994 try.maxslotindex = cimaxslotindex;
1001 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
1003 try.vj_protocol = cishort;
1010 NAKCIDNS(CI_MS_DNS1, req_dns1,
1011 try.dnsaddr[0] = cidnsaddr;
1014 NAKCIDNS(CI_MS_DNS2, req_dns2,
1015 try.dnsaddr[1] = cidnsaddr;
1019 * There may be remaining CIs, if the peer is requesting negotiation
1020 * on an option that we didn't include in our request packet.
1021 * If they want to negotiate about IP addresses, we comply.
1022 * If they want us to ask for compression, we refuse.
1024 while (len > CILEN_VOID) {
1027 if( (len -= cilen) < 0 )
1029 next = p + cilen - 2;
1032 case CI_COMPRESSTYPE:
1033 if (go->neg_vj || no.neg_vj ||
1034 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
1039 if ((go->neg_addr && go->old_addrs) || no.old_addrs
1040 || cilen != CILEN_ADDRS)
1046 if (ciaddr1 && go->accept_local)
1047 try.ouraddr = ciaddr1;
1050 if (ciaddr2 && go->accept_remote)
1051 try.hisaddr = ciaddr2;
1055 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
1060 if (ciaddr1 && go->accept_local)
1061 try.ouraddr = ciaddr1;
1062 if (try.ouraddr != 0)
1071 * OK, the Nak is good. Now we can update state.
1072 * If there are any remaining options, we ignore them.
1074 if (f->state != OPENED)
1080 IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
1086 * ipcp_rejci - Reject some of our CIs.
1087 * Callback from fsm_rconfnakrej.
1090 ipcp_rejci(f, p, len)
1095 ipcp_options *go = &ipcp_gotoptions[f->unit];
1096 u_char cimaxslotindex, ciflag, cilen;
1099 ipcp_options try; /* options to request next time */
1103 * Any Rejected CIs must be in exactly the same order that we sent.
1104 * Check packet length and CI length at each step.
1105 * If we find any deviations, then this packet is bad.
1107 #define REJCIADDR(opt, neg, old, val1, val2) \
1109 len >= (cilen = old? CILEN_ADDRS: CILEN_ADDR) && \
1116 cilong = htonl(l); \
1117 /* Check rejected value. */ \
1118 if (cilong != val1) \
1122 cilong = htonl(l); \
1123 /* Check rejected value. */ \
1124 if (cilong != val2) \
1130 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
1132 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
1137 GETSHORT(cishort, p); \
1138 /* Check rejected value. */ \
1139 if (cishort != val) \
1142 GETCHAR(cimaxslotindex, p); \
1143 if (cimaxslotindex != maxslot) \
1145 GETCHAR(ciflag, p); \
1146 if (ciflag != cflag) \
1152 #define REJCIDNS(opt, neg, dnsaddr) \
1154 ((cilen = p[1]) == CILEN_ADDR) && \
1161 cilong = htonl(l); \
1162 /* Check rejected value. */ \
1163 if (cilong != dnsaddr) \
1169 REJCIADDR((go->old_addrs? CI_ADDRS: CI_ADDR), neg_addr,
1170 go->old_addrs, go->ouraddr, go->hisaddr);
1172 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
1173 go->maxslotindex, go->cflag);
1175 REJCIDNS(CI_MS_DNS1, req_dns1, go->dnsaddr[0]);
1177 REJCIDNS(CI_MS_DNS2, req_dns2, go->dnsaddr[1]);
1180 * If there are any remaining CIs, then this packet is bad.
1185 * Now we can update state.
1187 if (f->state != OPENED)
1192 IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
1198 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
1199 * Callback from fsm_rconfreq, Receive Configure Request
1201 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1202 * appropriately. If reject_if_disagree is non-zero, doesn't return
1203 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1206 ipcp_reqci(f, inp, len, reject_if_disagree)
1208 u_char *inp; /* Requested CIs */
1209 int *len; /* Length of requested CIs */
1210 int reject_if_disagree;
1212 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1213 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1214 ipcp_options *ao = &ipcp_allowoptions[f->unit];
1215 ipcp_options *go = &ipcp_gotoptions[f->unit];
1216 u_char *cip, *next; /* Pointer to current and next CIs */
1217 u_short cilen, citype; /* Parsed len, type */
1218 u_short cishort; /* Parsed short value */
1219 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
1220 int rc = CONFACK; /* Final packet return code */
1221 int orc; /* Individual option return code */
1222 u_char *p; /* Pointer to next char to parse */
1223 u_char *ucp = inp; /* Pointer to current output char */
1224 int l = *len; /* Length left */
1225 u_char maxslotindex, cflag;
1229 * Reset all his options.
1231 BZERO(ho, sizeof(*ho));
1234 * Process all his options.
1238 orc = CONFACK; /* Assume success */
1239 cip = p = next; /* Remember begining of CI */
1240 if (l < 2 || /* Not enough data for CI header or */
1241 p[1] < 2 || /* CI length too small or */
1242 p[1] > l) { /* CI length too big? */
1243 IPCPDEBUG(("ipcp_reqci: bad CI length!"));
1244 orc = CONFREJ; /* Reject bad CI */
1245 cilen = l; /* Reject till end of packet */
1246 l = 0; /* Don't loop again */
1249 GETCHAR(citype, p); /* Parse CI type */
1250 GETCHAR(cilen, p); /* Parse CI length */
1251 l -= cilen; /* Adjust remaining length */
1252 next += cilen; /* Step to next CI */
1254 switch (citype) { /* Check CI type */
1256 if (!ao->neg_addr ||
1257 cilen != CILEN_ADDRS) { /* Check CI length */
1258 orc = CONFREJ; /* Reject CI */
1263 * If he has no address, or if we both have his address but
1264 * disagree about it, then NAK it with our idea.
1265 * In particular, if we don't know his address, but he does,
1268 GETLONG(tl, p); /* Parse source address (his) */
1269 ciaddr1 = htonl(tl);
1270 if (ciaddr1 != wo->hisaddr
1271 && (ciaddr1 == 0 || !wo->accept_remote)) {
1273 if (!reject_if_disagree) {
1274 DECPTR(sizeof(u_int32_t), p);
1275 tl = ntohl(wo->hisaddr);
1278 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1280 * If neither we nor he knows his address, reject the option.
1283 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1288 * If he doesn't know our address, or if we both have our address
1289 * but disagree about it, then NAK it with our idea.
1291 GETLONG(tl, p); /* Parse desination address (ours) */
1292 ciaddr2 = htonl(tl);
1293 if (ciaddr2 != wo->ouraddr) {
1294 if (ciaddr2 == 0 || !wo->accept_local) {
1296 if (!reject_if_disagree) {
1297 DECPTR(sizeof(u_int32_t), p);
1298 tl = ntohl(wo->ouraddr);
1302 go->ouraddr = ciaddr2; /* accept peer's idea */
1308 ho->hisaddr = ciaddr1;
1309 ho->ouraddr = ciaddr2;
1313 if (!ao->neg_addr ||
1314 cilen != CILEN_ADDR) { /* Check CI length */
1315 orc = CONFREJ; /* Reject CI */
1320 * If he has no address, or if we both have his address but
1321 * disagree about it, then NAK it with our idea.
1322 * In particular, if we don't know his address, but he does,
1325 GETLONG(tl, p); /* Parse source address (his) */
1326 ciaddr1 = htonl(tl);
1327 if (ciaddr1 != wo->hisaddr
1328 && (ciaddr1 == 0 || !wo->accept_remote)) {
1330 if (!reject_if_disagree) {
1331 DECPTR(sizeof(u_int32_t), p);
1332 tl = ntohl(wo->hisaddr);
1335 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1337 * Don't ACK an address of 0.0.0.0 - reject it instead.
1340 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1345 ho->hisaddr = ciaddr1;
1350 /* Microsoft primary or secondary DNS request */
1351 d = citype == CI_MS_DNS2;
1353 /* If we do not have a DNS address then we cannot send it */
1354 if (ao->dnsaddr[d] == 0 ||
1355 cilen != CILEN_ADDR) { /* Check CI length */
1356 orc = CONFREJ; /* Reject CI */
1360 if (htonl(tl) != ao->dnsaddr[d]) {
1361 DECPTR(sizeof(u_int32_t), p);
1362 tl = ntohl(ao->dnsaddr[d]);
1370 /* Microsoft primary or secondary WINS request */
1371 d = citype == CI_MS_WINS2;
1373 /* If we do not have a DNS address then we cannot send it */
1374 if (ao->winsaddr[d] == 0 ||
1375 cilen != CILEN_ADDR) { /* Check CI length */
1376 orc = CONFREJ; /* Reject CI */
1380 if (htonl(tl) != ao->winsaddr[d]) {
1381 DECPTR(sizeof(u_int32_t), p);
1382 tl = ntohl(ao->winsaddr[d]);
1388 case CI_COMPRESSTYPE:
1390 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1394 GETSHORT(cishort, p);
1396 if (!(cishort == IPCP_VJ_COMP ||
1397 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1403 ho->vj_protocol = cishort;
1404 if (cilen == CILEN_VJ) {
1405 GETCHAR(maxslotindex, p);
1406 if (maxslotindex > ao->maxslotindex) {
1408 if (!reject_if_disagree){
1410 PUTCHAR(ao->maxslotindex, p);
1414 if (cflag && !ao->cflag) {
1416 if (!reject_if_disagree){
1418 PUTCHAR(wo->cflag, p);
1421 ho->maxslotindex = maxslotindex;
1425 ho->maxslotindex = MAX_STATES - 1;
1435 if (orc == CONFACK && /* Good CI */
1436 rc != CONFACK) /* but prior CI wasnt? */
1437 continue; /* Don't send this one */
1439 if (orc == CONFNAK) { /* Nak this CI? */
1440 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1441 orc = CONFREJ; /* Get tough if so */
1443 if (rc == CONFREJ) /* Rejecting prior CI? */
1444 continue; /* Don't send this one */
1445 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1446 rc = CONFNAK; /* Not anymore... */
1447 ucp = inp; /* Backup */
1452 if (orc == CONFREJ && /* Reject this CI */
1453 rc != CONFREJ) { /* but no prior ones? */
1455 ucp = inp; /* Backup */
1458 /* Need to move CI? */
1460 BCOPY(cip, ucp, cilen); /* Move it */
1462 /* Update output pointer */
1467 * If we aren't rejecting this packet, and we want to negotiate
1468 * their address, and they didn't send their address, then we
1469 * send a NAK with a CI_ADDR option appended. We assume the
1470 * input buffer is long enough that we can append the extra
1473 if (rc != CONFREJ && !ho->neg_addr &&
1474 wo->req_addr && !reject_if_disagree) {
1475 if (rc == CONFACK) {
1477 ucp = inp; /* reset pointer */
1478 wo->req_addr = 0; /* don't ask again */
1480 PUTCHAR(CI_ADDR, ucp);
1481 PUTCHAR(CILEN_ADDR, ucp);
1482 tl = ntohl(wo->hisaddr);
1486 *len = ucp - inp; /* Compute output length */
1487 IPCPDEBUG(("ipcp: returning Configure-%s", CODENAME(rc)));
1488 return (rc); /* Return final code */
1493 * ip_check_options - check that any IP-related options are OK,
1494 * and assign appropriate defaults.
1501 ipcp_options *wo = &ipcp_wantoptions[0];
1504 * Default our local IP address based on our hostname.
1505 * If local IP address already given, don't bother.
1507 if (wo->ouraddr == 0 && !disable_defaultip) {
1509 * Look up our hostname (possibly with domain name appended)
1510 * and take the first IP address as our local IP address.
1511 * If there isn't an IP address for our hostname, too bad.
1513 wo->accept_local = 1; /* don't insist on this default value */
1514 if ((hp = gethostbyname(hostname)) != NULL) {
1515 local = *(u_int32_t *)hp->h_addr;
1516 if (local != 0 && !bad_ip_adrs(local))
1517 wo->ouraddr = local;
1520 ask_for_local = wo->ouraddr != 0 || !disable_defaultip;
1525 * ip_demand_conf - configure the interface as though
1526 * IPCP were up, for use with dial-on-demand.
1532 ipcp_options *wo = &ipcp_wantoptions[u];
1534 if (wo->hisaddr == 0) {
1535 /* make up an arbitrary address for the peer */
1536 wo->hisaddr = htonl(0x0a707070 + ifunit);
1537 wo->accept_remote = 1;
1539 if (wo->ouraddr == 0) {
1540 /* make up an arbitrary address for us */
1541 wo->ouraddr = htonl(0x0a404040 + ifunit);
1542 wo->accept_local = 1;
1543 ask_for_local = 0; /* don't tell the peer this address */
1545 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1549 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1551 if (wo->default_route)
1552 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1553 default_route_set[u] = 1;
1555 if (sifproxyarp(u, wo->hisaddr))
1556 proxy_arp_set[u] = 1;
1558 notice("local IP address %I", wo->ouraddr);
1559 notice("remote IP address %I", wo->hisaddr);
1566 * ipcp_up - IPCP has come UP.
1568 * Configure the IP network interface appropriately and bring it up.
1575 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1576 ipcp_options *go = &ipcp_gotoptions[f->unit];
1577 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1579 IPCPDEBUG(("ipcp: up"));
1582 * We must have a non-zero IP address for both ends of the link.
1585 ho->hisaddr = wo->hisaddr;
1587 if (go->ouraddr == 0) {
1588 error("Could not determine local IP address");
1589 ipcp_close(f->unit, "Could not determine local IP address");
1592 if (ho->hisaddr == 0) {
1593 ho->hisaddr = htonl(0x0a404040 + ifunit);
1594 warn("Could not determine remote IP address: defaulting to %I",
1597 script_setenv("IPLOCAL", ip_ntoa(go->ouraddr), 0);
1598 script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr), 1);
1600 if (usepeerdns && (go->dnsaddr[0] || go->dnsaddr[1])) {
1601 script_setenv("USEPEERDNS", "1", 0);
1603 script_setenv("DNS1", ip_ntoa(go->dnsaddr[0]), 0);
1605 script_setenv("DNS2", ip_ntoa(go->dnsaddr[1]), 0);
1606 create_resolv(go->dnsaddr[0], go->dnsaddr[1]);
1610 * Check that the peer is allowed to use the IP address it wants.
1612 if (!auth_ip_addr(f->unit, ho->hisaddr)) {
1613 error("Peer is not authorized to use remote address %I", ho->hisaddr);
1614 ipcp_close(f->unit, "Unauthorized remote IP address");
1618 /* set tcp compression */
1619 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1622 * If we are doing dial-on-demand, the interface is already
1623 * configured, so we put out any saved-up packets, then set the
1624 * interface to pass IP packets.
1627 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1628 ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr);
1629 if (go->ouraddr != wo->ouraddr) {
1630 warn("Local IP address changed to %I", go->ouraddr);
1631 script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr), 0);
1632 wo->ouraddr = go->ouraddr;
1634 script_unsetenv("OLDIPLOCAL");
1635 if (ho->hisaddr != wo->hisaddr) {
1636 warn("Remote IP address changed to %I", ho->hisaddr);
1637 script_setenv("OLDIPREMOTE", ip_ntoa(wo->hisaddr), 0);
1638 wo->hisaddr = ho->hisaddr;
1640 script_unsetenv("OLDIPREMOTE");
1642 /* Set the interface to the new addresses */
1643 mask = GetMask(go->ouraddr);
1644 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1646 warn("Interface configuration failed");
1647 ipcp_close(f->unit, "Interface configuration failed");
1651 /* assign a default route through the interface if required */
1652 if (ipcp_wantoptions[f->unit].default_route)
1653 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1654 default_route_set[f->unit] = 1;
1656 /* Make a proxy ARP entry if requested. */
1657 if (ipcp_wantoptions[f->unit].proxy_arp)
1658 if (sifproxyarp(f->unit, ho->hisaddr))
1659 proxy_arp_set[f->unit] = 1;
1662 demand_rexmit(PPP_IP);
1663 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1667 * Set IP addresses and (if specified) netmask.
1669 mask = GetMask(go->ouraddr);
1671 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1672 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1674 warn("Interface configuration failed");
1675 ipcp_close(f->unit, "Interface configuration failed");
1680 /* bring the interface up for IP */
1681 if (!sifup(f->unit)) {
1683 warn("Interface failed to come up");
1684 ipcp_close(f->unit, "Interface configuration failed");
1688 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1689 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1691 warn("Interface configuration failed");
1692 ipcp_close(f->unit, "Interface configuration failed");
1696 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1698 /* assign a default route through the interface if required */
1699 if (ipcp_wantoptions[f->unit].default_route)
1700 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1701 default_route_set[f->unit] = 1;
1703 /* Make a proxy ARP entry if requested. */
1704 if (ipcp_wantoptions[f->unit].proxy_arp)
1705 if (sifproxyarp(f->unit, ho->hisaddr))
1706 proxy_arp_set[f->unit] = 1;
1708 ipcp_wantoptions[0].ouraddr = go->ouraddr;
1710 notice("local IP address %I", go->ouraddr);
1711 notice("remote IP address %I", ho->hisaddr);
1713 notice("primary DNS address %I", go->dnsaddr[0]);
1715 notice("secondary DNS address %I", go->dnsaddr[1]);
1718 np_up(f->unit, PPP_IP);
1721 notify(ip_up_notifier, 0);
1726 * Execute the ip-up script, like this:
1727 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1729 if (ipcp_script_state == s_down && ipcp_script_pid == 0) {
1730 ipcp_script_state = s_up;
1731 ipcp_script(_PATH_IPUP);
1737 * ipcp_down - IPCP has gone DOWN.
1739 * Take the IP network interface down, clear its addresses
1740 * and delete routes through it.
1746 IPCPDEBUG(("ipcp: down"));
1747 /* XXX a bit IPv4-centric here, we only need to get the stats
1748 * before the interface is marked down. */
1749 update_link_stats(f->unit);
1750 notify(ip_down_notifier, 0);
1755 np_down(f->unit, PPP_IP);
1757 sifvjcomp(f->unit, 0, 0, 0);
1760 * If we are doing dial-on-demand, set the interface
1761 * to queue up outgoing packets (for now).
1764 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1766 sifnpmode(f->unit, PPP_IP, NPMODE_DROP);
1768 ipcp_clear_addrs(f->unit, ipcp_gotoptions[f->unit].ouraddr,
1769 ipcp_hisoptions[f->unit].hisaddr);
1772 /* Execute the ip-down script */
1773 if (ipcp_script_state == s_up && ipcp_script_pid == 0) {
1774 ipcp_script_state = s_down;
1775 ipcp_script(_PATH_IPDOWN);
1781 * ipcp_clear_addrs() - clear the interface addresses, routes,
1782 * proxy arp entries, etc.
1785 ipcp_clear_addrs(unit, ouraddr, hisaddr)
1787 u_int32_t ouraddr; /* local address */
1788 u_int32_t hisaddr; /* remote address */
1790 if (proxy_arp_set[unit]) {
1791 cifproxyarp(unit, hisaddr);
1792 proxy_arp_set[unit] = 0;
1794 if (default_route_set[unit]) {
1795 cifdefaultroute(unit, ouraddr, hisaddr);
1796 default_route_set[unit] = 0;
1798 cifaddr(unit, ouraddr, hisaddr);
1803 * ipcp_finished - possibly shut down the lower layers.
1809 np_finished(f->unit, PPP_IP);
1814 * ipcp_script_done - called when the ip-up or ip-down script
1818 ipcp_script_done(arg)
1821 ipcp_script_pid = 0;
1822 switch (ipcp_script_state) {
1824 if (ipcp_fsm[0].state != OPENED) {
1825 ipcp_script_state = s_down;
1826 ipcp_script(_PATH_IPDOWN);
1830 if (ipcp_fsm[0].state == OPENED) {
1831 ipcp_script_state = s_up;
1832 ipcp_script(_PATH_IPUP);
1840 * ipcp_script - Execute a script with arguments
1841 * interface-name tty-name speed local-IP remote-IP.
1847 char strspeed[32], strlocal[32], strremote[32];
1850 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
1851 slprintf(strlocal, sizeof(strlocal), "%I", ipcp_gotoptions[0].ouraddr);
1852 slprintf(strremote, sizeof(strremote), "%I", ipcp_hisoptions[0].hisaddr);
1859 argv[5] = strremote;
1862 ipcp_script_pid = run_program(script, argv, 0, ipcp_script_done, NULL);
1866 * create_resolv - create the replacement resolv.conf file
1869 create_resolv(peerdns1, peerdns2)
1870 u_int32_t peerdns1, peerdns2;
1874 f = fopen(_PATH_RESOLV, "w");
1876 error("Failed to create %s: %m", _PATH_RESOLV);
1881 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns1));
1884 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns2));
1887 error("Write failed to %s: %m", _PATH_RESOLV);
1893 * ipcp_printpkt - print the contents of an IPCP packet.
1895 static char *ipcp_codenames[] = {
1896 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
1897 "TermReq", "TermAck", "CodeRej"
1901 ipcp_printpkt(p, plen, printer, arg)
1904 void (*printer) __P((void *, char *, ...));
1907 int code, id, len, olen;
1908 u_char *pstart, *optend;
1912 if (plen < HEADERLEN)
1918 if (len < HEADERLEN || len > plen)
1921 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
1922 printer(arg, " %s", ipcp_codenames[code-1]);
1924 printer(arg, " code=0x%x", code);
1925 printer(arg, " id=0x%x", id);
1932 /* print option list */
1937 if (olen < 2 || olen > len) {
1945 if (olen == CILEN_ADDRS) {
1948 printer(arg, "addrs %I", htonl(cilong));
1950 printer(arg, " %I", htonl(cilong));
1953 case CI_COMPRESSTYPE:
1954 if (olen >= CILEN_COMPRESS) {
1956 GETSHORT(cishort, p);
1957 printer(arg, "compress ");
1962 case IPCP_VJ_COMP_OLD:
1963 printer(arg, "old-VJ");
1966 printer(arg, "0x%x", cishort);
1971 if (olen == CILEN_ADDR) {
1974 printer(arg, "addr %I", htonl(cilong));
1981 printer(arg, "ms-dns%d %I", code - CI_MS_DNS1 + 1,
1988 printer(arg, "ms-wins %I", htonl(cilong));
1991 while (p < optend) {
1993 printer(arg, " %.2x", code);
2001 if (len > 0 && *p >= ' ' && *p < 0x7f) {
2003 print_string((char *)p, len, printer, arg);
2010 /* print the rest of the bytes in the packet */
2011 for (; len > 0; --len) {
2013 printer(arg, " %.2x", code);
2020 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
2021 * We don't bring the link up for IP fragments or for TCP FIN packets
2024 #define IP_HDRLEN 20 /* bytes */
2025 #define IP_OFFMASK 0x1fff
2027 #define IPPROTO_TCP 6
2029 #define TCP_HDRLEN 20
2033 * We use these macros because the IP header may be at an odd address,
2034 * and some compilers might use word loads to get th_off or ip_hl.
2037 #define net_short(x) (((x)[0] << 8) + (x)[1])
2038 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
2039 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
2040 #define get_ipproto(x) (((unsigned char *)(x))[9])
2041 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
2042 #define get_tcpflags(x) (((unsigned char *)(x))[13])
2045 ip_active_pkt(pkt, len)
2054 if (len < IP_HDRLEN)
2056 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
2058 if (get_ipproto(pkt) != IPPROTO_TCP)
2060 hlen = get_iphl(pkt) * 4;
2061 if (len < hlen + TCP_HDRLEN)
2064 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)