2 * ipcp.c - PPP IP Control Protocol.
4 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * 3. The name "Carnegie Mellon University" must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission. For permission or any legal
21 * details, please contact
22 * Office of Technology Transfer
23 * Carnegie Mellon University
25 * Pittsburgh, PA 15213-3890
26 * (412) 268-4387, fax: (412) 268-7395
27 * tech-transfer@andrew.cmu.edu
29 * 4. Redistributions of any form whatsoever must retain the following
31 * "This product includes software developed by Computing Services
32 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
34 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
51 #include <sys/param.h>
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
61 #include "pathnames.h"
65 ipcp_options ipcp_wantoptions[NUM_PPP]; /* Options that we want to request */
66 ipcp_options ipcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
67 ipcp_options ipcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
68 ipcp_options ipcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
70 u_int32_t netmask = 0; /* IP netmask to set on interface */
72 bool disable_defaultip = 0; /* Don't use hostname for default IP adrs */
73 bool noremoteip = 0; /* Let him have no IP address */
75 /* Hook for a plugin to know when IP protocol has come up */
76 void (*ip_up_hook)(void) = NULL;
78 /* Hook for a plugin to know when IP protocol has come down */
79 void (*ip_down_hook)(void) = NULL;
81 /* Hook for a plugin to choose the remote IP address */
82 void (*ip_choose_hook)(u_int32_t *) = NULL;
84 /* Notifiers for when IPCP goes up and down */
85 struct notifier *ip_up_notifier = NULL;
86 struct notifier *ip_down_notifier = NULL;
89 static int default_route_set[NUM_PPP]; /* Have set up a default route */
90 static int proxy_arp_set[NUM_PPP]; /* Have created proxy arp entry */
91 static bool usepeerdns; /* Ask peer for DNS addrs */
92 static bool usepeerwins; /* Ask peer for WINS addrs */
93 static int ipcp_is_up; /* have called np_up() */
94 static int ipcp_is_open; /* haven't called np_finished() */
95 static bool ask_for_local; /* request our address from peer */
96 static char vj_value[8]; /* string form of vj option value */
97 static char netmask_str[20]; /* string form of netmask value */
100 * Callbacks for fsm code. (CI = Configuration Information)
102 static void ipcp_resetci (fsm *); /* Reset our CI */
103 static int ipcp_cilen (fsm *); /* Return length of our CI */
104 static void ipcp_addci (fsm *, u_char *, int *); /* Add our CI */
105 static int ipcp_ackci (fsm *, u_char *, int); /* Peer ack'd our CI */
106 static int ipcp_nakci (fsm *, u_char *, int, int);/* Peer nak'd our CI */
107 static int ipcp_rejci (fsm *, u_char *, int); /* Peer rej'd our CI */
108 static int ipcp_reqci (fsm *, u_char *, int *, int); /* Rcv CI */
109 static void ipcp_up (fsm *); /* We're UP */
110 static void ipcp_down (fsm *); /* We're DOWN */
111 static void ipcp_finished (fsm *); /* Don't need lower layer */
113 fsm ipcp_fsm[NUM_PPP]; /* IPCP fsm structure */
115 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
116 ipcp_resetci, /* Reset our Configuration Information */
117 ipcp_cilen, /* Length of our Configuration Information */
118 ipcp_addci, /* Add our Configuration Information */
119 ipcp_ackci, /* ACK our Configuration Information */
120 ipcp_nakci, /* NAK our Configuration Information */
121 ipcp_rejci, /* Reject our Configuration Information */
122 ipcp_reqci, /* Request peer's Configuration Information */
123 ipcp_up, /* Called when fsm reaches OPENED state */
124 ipcp_down, /* Called when fsm leaves OPENED state */
125 NULL, /* Called when we want the lower layer up */
126 ipcp_finished, /* Called when we want the lower layer down */
127 NULL, /* Called when Protocol-Reject received */
128 NULL, /* Retransmission is necessary */
129 NULL, /* Called to handle protocol-specific codes */
130 "IPCP" /* String name of protocol */
134 * Command-line options.
136 static int setvjslots (char **);
137 static int setdnsaddr (char **);
138 static int setwinsaddr (char **);
139 static int setnetmask (char **);
140 int setipaddr (char *, char **, int);
141 static void printipaddr (option_t *, void (*)(void *, char *,...),void *);
143 static option_t ipcp_option_list[] = {
144 { "noip", o_bool, &ipcp_protent.enabled_flag,
145 "Disable IP and IPCP" },
146 { "-ip", o_bool, &ipcp_protent.enabled_flag,
147 "Disable IP and IPCP", OPT_ALIAS },
149 { "novj", o_bool, &ipcp_wantoptions[0].neg_vj,
150 "Disable VJ compression", OPT_A2CLR, &ipcp_allowoptions[0].neg_vj },
151 { "-vj", o_bool, &ipcp_wantoptions[0].neg_vj,
152 "Disable VJ compression", OPT_ALIAS | OPT_A2CLR,
153 &ipcp_allowoptions[0].neg_vj },
155 { "novjccomp", o_bool, &ipcp_wantoptions[0].cflag,
156 "Disable VJ connection-ID compression", OPT_A2CLR,
157 &ipcp_allowoptions[0].cflag },
158 { "-vjccomp", o_bool, &ipcp_wantoptions[0].cflag,
159 "Disable VJ connection-ID compression", OPT_ALIAS | OPT_A2CLR,
160 &ipcp_allowoptions[0].cflag },
162 { "vj-max-slots", o_special, (void *)setvjslots,
163 "Set maximum VJ header slots",
164 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, vj_value },
166 { "ipcp-accept-local", o_bool, &ipcp_wantoptions[0].accept_local,
167 "Accept peer's address for us", 1 },
168 { "ipcp-accept-remote", o_bool, &ipcp_wantoptions[0].accept_remote,
169 "Accept peer's address for it", 1 },
171 { "ipparam", o_string, &ipparam,
172 "Set ip script parameter", OPT_PRIO },
174 { "noipdefault", o_bool, &disable_defaultip,
175 "Don't use name for default IP adrs", 1 },
177 { "ms-dns", o_special, (void *)setdnsaddr,
178 "DNS address for the peer's use", OPT_A2LIST },
179 { "ms-wins", o_special, (void *)setwinsaddr,
180 "Nameserver for SMB over TCP/IP for peer", OPT_A2LIST },
182 { "ipcp-restart", o_int, &ipcp_fsm[0].timeouttime,
183 "Set timeout for IPCP", OPT_PRIO },
184 { "ipcp-max-terminate", o_int, &ipcp_fsm[0].maxtermtransmits,
185 "Set max #xmits for term-reqs", OPT_PRIO },
186 { "ipcp-max-configure", o_int, &ipcp_fsm[0].maxconfreqtransmits,
187 "Set max #xmits for conf-reqs", OPT_PRIO },
188 { "ipcp-max-failure", o_int, &ipcp_fsm[0].maxnakloops,
189 "Set max #conf-naks for IPCP", OPT_PRIO },
191 { "defaultroute", o_bool, &ipcp_wantoptions[0].default_route,
192 "Add default route", OPT_ENABLE|1, &ipcp_allowoptions[0].default_route },
193 { "nodefaultroute", o_bool, &ipcp_allowoptions[0].default_route,
194 "disable defaultroute option", OPT_A2CLR,
195 &ipcp_wantoptions[0].default_route },
196 { "-defaultroute", o_bool, &ipcp_allowoptions[0].default_route,
197 "disable defaultroute option", OPT_ALIAS | OPT_A2CLR,
198 &ipcp_wantoptions[0].default_route },
201 { "replacedefaultroute", o_bool,
202 &ipcp_wantoptions[0].replace_default_route,
203 "Replace default route", OPT_PRIV | 1
205 { "noreplacedefaultroute", o_bool,
206 &ipcp_wantoptions[0].replace_default_route,
207 "Do not replace default route", 0 },
209 { "proxyarp", o_bool, &ipcp_wantoptions[0].proxy_arp,
210 "Add proxy ARP entry", OPT_ENABLE|1, &ipcp_allowoptions[0].proxy_arp },
211 { "noproxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
212 "disable proxyarp option", OPT_A2CLR,
213 &ipcp_wantoptions[0].proxy_arp },
214 { "-proxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
215 "disable proxyarp option", OPT_ALIAS | OPT_A2CLR,
216 &ipcp_wantoptions[0].proxy_arp },
218 { "usepeerdns", o_bool, &usepeerdns,
219 "Ask peer for DNS address(es)", 1 },
221 { "usepeerwins", o_bool, &usepeerwins,
222 "Ask peer for WINS address(es)", 1 },
224 { "netmask", o_special, (void *)setnetmask,
225 "set netmask", OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, netmask_str },
227 { "ipcp-no-addresses", o_bool, &ipcp_wantoptions[0].old_addrs,
228 "Disable old-style IP-Addresses usage", OPT_A2CLR,
229 &ipcp_allowoptions[0].old_addrs },
230 { "ipcp-no-address", o_bool, &ipcp_wantoptions[0].neg_addr,
231 "Disable IP-Address usage", OPT_A2CLR,
232 &ipcp_allowoptions[0].neg_addr },
234 { "noremoteip", o_bool, &noremoteip,
235 "Allow peer to have no IP address", 1 },
237 { "nosendip", o_bool, &ipcp_wantoptions[0].neg_addr,
238 "Don't send our IP address to peer", OPT_A2CLR,
239 &ipcp_wantoptions[0].old_addrs},
241 { "IP addresses", o_wild, (void *) &setipaddr,
242 "set local and remote IP addresses",
243 OPT_NOARG | OPT_A2PRINTER, (void *) &printipaddr },
249 * Protocol entry points from main code.
251 static void ipcp_init (int);
252 static void ipcp_open (int);
253 static void ipcp_close (int, char *);
254 static void ipcp_lowerup (int);
255 static void ipcp_lowerdown (int);
256 static void ipcp_input (int, u_char *, int);
257 static void ipcp_protrej (int);
258 static int ipcp_printpkt (u_char *, int,
259 void (*) (void *, char *, ...), void *);
260 static void ip_check_options (void);
261 static int ip_demand_conf (int);
262 static int ip_active_pkt (u_char *, int);
263 static void create_resolv (u_int32_t, u_int32_t);
265 struct protent ipcp_protent = {
285 static void ipcp_clear_addrs (int, u_int32_t, u_int32_t, bool);
286 static void ipcp_script (char *, int); /* Run an up/down script */
287 static void ipcp_script_done (void *);
290 * Lengths of configuration options.
293 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
294 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
295 #define CILEN_ADDR 6 /* new-style single address option */
296 #define CILEN_ADDRS 10 /* old-style dual address option */
299 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
300 (x) == CONFNAK ? "NAK" : "REJ")
303 * This state variable is used to ensure that we don't
304 * run an ipcp-up/down script while one is already running.
306 static enum script_state {
310 static pid_t ipcp_script_pid;
313 * Make a string representation of a network IP address.
316 ip_ntoa(u_int32_t ipaddr)
320 slprintf(b, sizeof(b), "%I", ipaddr);
329 * setvjslots - set maximum number of connection slots for VJ compression
332 setvjslots(char **argv)
336 if (!int_option(*argv, &value))
338 if (value < 2 || value > 16) {
339 option_error("vj-max-slots value must be between 2 and 16");
342 ipcp_wantoptions [0].maxslotindex =
343 ipcp_allowoptions[0].maxslotindex = value - 1;
344 slprintf(vj_value, sizeof(vj_value), "%d", value);
349 * setdnsaddr - set the dns address(es)
352 setdnsaddr(char **argv)
357 dns = inet_addr(*argv);
358 if (dns == (u_int32_t) -1) {
359 if ((hp = gethostbyname(*argv)) == NULL) {
360 option_error("invalid address parameter '%s' for ms-dns option",
364 dns = *(u_int32_t *)hp->h_addr;
367 /* We take the last 2 values given, the 2nd-last as the primary
368 and the last as the secondary. If only one is given it
369 becomes both primary and secondary. */
370 if (ipcp_allowoptions[0].dnsaddr[1] == 0)
371 ipcp_allowoptions[0].dnsaddr[0] = dns;
373 ipcp_allowoptions[0].dnsaddr[0] = ipcp_allowoptions[0].dnsaddr[1];
375 /* always set the secondary address value. */
376 ipcp_allowoptions[0].dnsaddr[1] = dns;
382 * setwinsaddr - set the wins address(es)
383 * This is primrarly used with the Samba package under UNIX or for pointing
384 * the caller to the existing WINS server on a Windows NT platform.
387 setwinsaddr(char **argv)
392 wins = inet_addr(*argv);
393 if (wins == (u_int32_t) -1) {
394 if ((hp = gethostbyname(*argv)) == NULL) {
395 option_error("invalid address parameter '%s' for ms-wins option",
399 wins = *(u_int32_t *)hp->h_addr;
402 /* We take the last 2 values given, the 2nd-last as the primary
403 and the last as the secondary. If only one is given it
404 becomes both primary and secondary. */
405 if (ipcp_allowoptions[0].winsaddr[1] == 0)
406 ipcp_allowoptions[0].winsaddr[0] = wins;
408 ipcp_allowoptions[0].winsaddr[0] = ipcp_allowoptions[0].winsaddr[1];
410 /* always set the secondary address value. */
411 ipcp_allowoptions[0].winsaddr[1] = wins;
417 * setipaddr - Set the IP address
418 * If doit is 0, the call is to check whether this option is
419 * potentially an IP address specification.
420 * Not static so that plugins can call it to set the addresses
423 setipaddr(char *arg, char **argv, int doit)
427 u_int32_t local, remote;
428 ipcp_options *wo = &ipcp_wantoptions[0];
429 static int prio_local = 0, prio_remote = 0;
432 * IP address pair separated by ":".
434 if ((colon = strchr(arg, ':')) == NULL)
440 * If colon first character, then no local addr.
442 if (colon != arg && option_priority >= prio_local) {
444 if ((local = inet_addr(arg)) == (u_int32_t) -1) {
445 if ((hp = gethostbyname(arg)) == NULL) {
446 option_error("unknown host: %s", arg);
449 local = *(u_int32_t *)hp->h_addr;
451 if (bad_ip_adrs(local)) {
452 option_error("bad local IP address %s", ip_ntoa(local));
458 prio_local = option_priority;
462 * If colon last character, then no remote addr.
464 if (*++colon != '\0' && option_priority >= prio_remote) {
465 if ((remote = inet_addr(colon)) == (u_int32_t) -1) {
466 if ((hp = gethostbyname(colon)) == NULL) {
467 option_error("unknown host: %s", colon);
470 remote = *(u_int32_t *)hp->h_addr;
471 if (remote_name[0] == 0)
472 strlcpy(remote_name, colon, sizeof(remote_name));
474 if (bad_ip_adrs(remote)) {
475 option_error("bad remote IP address %s", ip_ntoa(remote));
479 wo->hisaddr = remote;
480 prio_remote = option_priority;
487 printipaddr(option_t *opt, void (*printer) (void *, char *, ...), void *arg)
489 ipcp_options *wo = &ipcp_wantoptions[0];
491 if (wo->ouraddr != 0)
492 printer(arg, "%I", wo->ouraddr);
494 if (wo->hisaddr != 0)
495 printer(arg, "%I", wo->hisaddr);
499 * setnetmask - set the netmask to be used on the interface.
502 setnetmask(char **argv)
509 * Unfortunately, if we use inet_addr, we can't tell whether
510 * a result of all 1s is an error or a valid 255.255.255.255.
513 n = parse_dotted_ip(p, &mask);
517 if (n == 0 || p[n] != 0 || (netmask & ~mask) != 0) {
518 option_error("invalid netmask value '%s'", *argv);
523 slprintf(netmask_str, sizeof(netmask_str), "%I", mask);
529 parse_dotted_ip(char *p, u_int32_t *vp)
537 b = strtoul(p, &endp, 0);
543 /* accept e.g. 0xffffff00 */
561 * ipcp_init - Initialize IPCP.
566 fsm *f = &ipcp_fsm[unit];
567 ipcp_options *wo = &ipcp_wantoptions[unit];
568 ipcp_options *ao = &ipcp_allowoptions[unit];
571 f->protocol = PPP_IPCP;
572 f->callbacks = &ipcp_callbacks;
573 fsm_init(&ipcp_fsm[unit]);
576 * Some 3G modems use repeated IPCP NAKs as a way of stalling
577 * until they can contact a server on the network, so we increase
578 * the default number of NAKs we accept before we start treating
581 f->maxnakloops = 100;
583 memset(wo, 0, sizeof(*wo));
584 memset(ao, 0, sizeof(*ao));
586 wo->neg_addr = wo->old_addrs = 1;
588 wo->vj_protocol = IPCP_VJ_COMP;
589 wo->maxslotindex = MAX_STATES - 1; /* really max index */
593 /* max slots and slot-id compression are currently hardwired in */
594 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
597 ao->neg_addr = ao->old_addrs = 1;
599 ao->maxslotindex = MAX_STATES - 1;
603 * XXX These control whether the user may use the proxyarp
604 * and defaultroute options.
607 ao->default_route = 1;
612 * ipcp_open - IPCP is allowed to come up.
617 fsm_open(&ipcp_fsm[unit]);
623 * ipcp_close - Take IPCP down.
626 ipcp_close(int unit, char *reason)
628 fsm_close(&ipcp_fsm[unit], reason);
633 * ipcp_lowerup - The lower layer is up.
636 ipcp_lowerup(int unit)
638 fsm_lowerup(&ipcp_fsm[unit]);
643 * ipcp_lowerdown - The lower layer is down.
646 ipcp_lowerdown(int unit)
648 fsm_lowerdown(&ipcp_fsm[unit]);
653 * ipcp_input - Input IPCP packet.
656 ipcp_input(int unit, u_char *p, int len)
658 fsm_input(&ipcp_fsm[unit], p, len);
663 * ipcp_protrej - A Protocol-Reject was received for IPCP.
665 * Pretend the lower layer went down, so we shut up.
668 ipcp_protrej(int unit)
670 fsm_lowerdown(&ipcp_fsm[unit]);
675 * ipcp_resetci - Reset our CI.
676 * Called by fsm_sconfreq, Send Configure Request.
681 ipcp_options *wo = &ipcp_wantoptions[f->unit];
682 ipcp_options *go = &ipcp_gotoptions[f->unit];
683 ipcp_options *ao = &ipcp_allowoptions[f->unit];
685 wo->req_addr = (wo->neg_addr || wo->old_addrs) &&
686 (ao->neg_addr || ao->old_addrs);
687 if (wo->ouraddr == 0)
688 wo->accept_local = 1;
689 if (wo->hisaddr == 0)
690 wo->accept_remote = 1;
691 wo->req_dns1 = usepeerdns; /* Request DNS addresses from the peer */
692 wo->req_dns2 = usepeerdns;
693 wo->req_wins1 = usepeerwins; /* Request WINS addresses from the peer */
694 wo->req_wins2 = usepeerwins;
698 if (ip_choose_hook) {
699 ip_choose_hook(&wo->hisaddr);
701 wo->accept_remote = 0;
704 BZERO(&ipcp_hisoptions[f->unit], sizeof(ipcp_options));
709 * ipcp_cilen - Return length of our CI.
710 * Called by fsm_sconfreq, Send Configure Request.
715 ipcp_options *go = &ipcp_gotoptions[f->unit];
716 ipcp_options *wo = &ipcp_wantoptions[f->unit];
717 ipcp_options *ho = &ipcp_hisoptions[f->unit];
719 #define LENCIADDRS(neg) (neg ? CILEN_ADDRS : 0)
720 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
721 #define LENCIADDR(neg) (neg ? CILEN_ADDR : 0)
722 #define LENCIDNS(neg) LENCIADDR(neg)
723 #define LENCIWINS(neg) LENCIADDR(neg)
726 * First see if we want to change our options to the old
727 * forms because we have received old forms from the peer.
729 if (go->neg_addr && go->old_addrs && !ho->neg_addr && ho->old_addrs)
731 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
732 /* try an older style of VJ negotiation */
733 /* use the old style only if the peer did */
734 if (ho->neg_vj && ho->old_vj) {
737 go->vj_protocol = ho->vj_protocol;
741 return (LENCIADDRS(!go->neg_addr && go->old_addrs) +
742 LENCIVJ(go->neg_vj, go->old_vj) +
743 LENCIADDR(go->neg_addr) +
744 LENCIDNS(go->req_dns1) +
745 LENCIDNS(go->req_dns2) +
746 LENCIWINS(go->req_wins1) +
747 LENCIWINS(go->req_wins2)) ;
752 * ipcp_addci - Add our desired CIs to a packet.
753 * Called by fsm_sconfreq, Send Configure Request.
756 ipcp_addci(fsm *f, u_char *ucp, int *lenp)
758 ipcp_options *go = &ipcp_gotoptions[f->unit];
761 #define ADDCIADDRS(opt, neg, val1, val2) \
763 if (len >= CILEN_ADDRS) { \
766 PUTCHAR(CILEN_ADDRS, ucp); \
771 len -= CILEN_ADDRS; \
776 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
778 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
779 if (len >= vjlen) { \
781 PUTCHAR(vjlen, ucp); \
782 PUTSHORT(val, ucp); \
784 PUTCHAR(maxslotindex, ucp); \
785 PUTCHAR(cflag, ucp); \
792 #define ADDCIADDR(opt, neg, val) \
794 if (len >= CILEN_ADDR) { \
797 PUTCHAR(CILEN_ADDR, ucp); \
805 #define ADDCIDNS(opt, neg, addr) \
807 if (len >= CILEN_ADDR) { \
810 PUTCHAR(CILEN_ADDR, ucp); \
818 #define ADDCIWINS(opt, neg, addr) \
820 if (len >= CILEN_ADDR) { \
823 PUTCHAR(CILEN_ADDR, ucp); \
831 ADDCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs, go->ouraddr,
834 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
835 go->maxslotindex, go->cflag);
837 ADDCIADDR(CI_ADDR, go->neg_addr, go->ouraddr);
839 ADDCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
841 ADDCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
843 ADDCIWINS(CI_MS_WINS1, go->req_wins1, go->winsaddr[0]);
845 ADDCIWINS(CI_MS_WINS2, go->req_wins2, go->winsaddr[1]);
852 * ipcp_ackci - Ack our CIs.
853 * Called by fsm_rconfack, Receive Configure ACK.
860 ipcp_ackci(fsm *f, u_char *p, int len)
862 ipcp_options *go = &ipcp_gotoptions[f->unit];
863 u_short cilen, citype, cishort;
865 u_char cimaxslotindex, cicflag;
868 * CIs must be in exactly the same order that we sent...
869 * Check packet length and CI length at each step.
870 * If we find any deviations, then this packet is bad.
873 #define ACKCIADDRS(opt, neg, val1, val2) \
876 if ((len -= CILEN_ADDRS) < 0) \
878 GETCHAR(citype, p); \
880 if (cilen != CILEN_ADDRS || \
885 if (val1 != cilong) \
889 if (val2 != cilong) \
893 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
895 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
896 if ((len -= vjlen) < 0) \
898 GETCHAR(citype, p); \
900 if (cilen != vjlen || \
903 GETSHORT(cishort, p); \
904 if (cishort != val) \
907 GETCHAR(cimaxslotindex, p); \
908 if (cimaxslotindex != maxslotindex) \
910 GETCHAR(cicflag, p); \
911 if (cicflag != cflag) \
916 #define ACKCIADDR(opt, neg, val) \
919 if ((len -= CILEN_ADDR) < 0) \
921 GETCHAR(citype, p); \
923 if (cilen != CILEN_ADDR || \
932 #define ACKCIDNS(opt, neg, addr) \
935 if ((len -= CILEN_ADDR) < 0) \
937 GETCHAR(citype, p); \
939 if (cilen != CILEN_ADDR || citype != opt) \
943 if (addr != cilong) \
947 #define ACKCIWINS(opt, neg, addr) \
950 if ((len -= CILEN_ADDR) < 0) \
952 GETCHAR(citype, p); \
954 if (cilen != CILEN_ADDR || citype != opt) \
958 if (addr != cilong) \
962 ACKCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs, go->ouraddr,
965 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
966 go->maxslotindex, go->cflag);
968 ACKCIADDR(CI_ADDR, go->neg_addr, go->ouraddr);
970 ACKCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
972 ACKCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
974 ACKCIWINS(CI_MS_WINS1, go->req_wins1, go->winsaddr[0]);
976 ACKCIWINS(CI_MS_WINS2, go->req_wins2, go->winsaddr[1]);
979 * If there are any remaining CIs, then this packet is bad.
986 IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
991 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
992 * This should not modify any state if the Nak is bad
993 * or if IPCP is in the OPENED state.
994 * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
1001 ipcp_nakci(fsm *f, u_char *p, int len, int treat_as_reject)
1003 ipcp_options *go = &ipcp_gotoptions[f->unit];
1004 u_char cimaxslotindex, cicflag;
1005 u_char citype, cilen, *next;
1007 u_int32_t ciaddr1, ciaddr2, l, cidnsaddr, ciwinsaddr;
1008 ipcp_options no; /* options we've seen Naks for */
1009 ipcp_options try; /* options to request next time */
1011 BZERO(&no, sizeof(no));
1015 * Any Nak'd CIs must be in exactly the same order that we sent.
1016 * Check packet length and CI length at each step.
1017 * If we find any deviations, then this packet is bad.
1019 #define NAKCIADDRS(opt, neg, code) \
1021 (cilen = p[1]) == CILEN_ADDRS && \
1027 ciaddr1 = htonl(l); \
1029 ciaddr2 = htonl(l); \
1034 #define NAKCIVJ(opt, neg, code) \
1036 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
1041 GETSHORT(cishort, p); \
1046 #define NAKCIADDR(opt, neg, code) \
1048 (cilen = p[1]) == CILEN_ADDR && \
1054 ciaddr1 = htonl(l); \
1059 #define NAKCIDNS(opt, neg, code) \
1061 ((cilen = p[1]) == CILEN_ADDR) && \
1067 cidnsaddr = htonl(l); \
1072 #define NAKCIWINS(opt, neg, code) \
1074 ((cilen = p[1]) == CILEN_ADDR) && \
1080 ciwinsaddr = htonl(l); \
1086 * Accept the peer's idea of {our,his} address, if different
1087 * from our idea, only if the accept_{local,remote} flag is set.
1089 NAKCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs,
1090 if (treat_as_reject) {
1093 if (go->accept_local && ciaddr1) {
1094 /* take his idea of our address */
1095 try.ouraddr = ciaddr1;
1097 if (go->accept_remote && ciaddr2) {
1098 /* take his idea of his address */
1099 try.hisaddr = ciaddr2;
1105 * Accept the peer's value of maxslotindex provided that it
1106 * is less than what we asked for. Turn off slot-ID compression
1107 * if the peer wants. Send old-style compress-type option if
1110 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
1111 if (treat_as_reject) {
1113 } else if (cilen == CILEN_VJ) {
1114 GETCHAR(cimaxslotindex, p);
1115 GETCHAR(cicflag, p);
1116 if (cishort == IPCP_VJ_COMP) {
1118 if (cimaxslotindex < go->maxslotindex)
1119 try.maxslotindex = cimaxslotindex;
1126 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
1128 try.vj_protocol = cishort;
1135 NAKCIADDR(CI_ADDR, neg_addr,
1136 if (treat_as_reject) {
1139 } else if (go->accept_local && ciaddr1) {
1140 /* take his idea of our address */
1141 try.ouraddr = ciaddr1;
1145 NAKCIDNS(CI_MS_DNS1, req_dns1,
1146 if (treat_as_reject) {
1149 try.dnsaddr[0] = cidnsaddr;
1153 NAKCIDNS(CI_MS_DNS2, req_dns2,
1154 if (treat_as_reject) {
1157 try.dnsaddr[1] = cidnsaddr;
1161 NAKCIWINS(CI_MS_WINS1, req_wins1,
1162 if (treat_as_reject) {
1165 try.winsaddr[0] = ciwinsaddr;
1169 NAKCIWINS(CI_MS_WINS2, req_wins2,
1170 if (treat_as_reject) {
1173 try.winsaddr[1] = ciwinsaddr;
1178 * There may be remaining CIs, if the peer is requesting negotiation
1179 * on an option that we didn't include in our request packet.
1180 * If they want to negotiate about IP addresses, we comply.
1181 * If they want us to ask for compression, we refuse.
1182 * If they want us to ask for ms-dns, we do that, since some
1183 * peers get huffy if we don't.
1185 while (len >= CILEN_VOID) {
1188 if ( cilen < CILEN_VOID || (len -= cilen) < 0 )
1190 next = p + cilen - 2;
1193 case CI_COMPRESSTYPE:
1194 if (go->neg_vj || no.neg_vj ||
1195 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
1200 if ((!go->neg_addr && go->old_addrs) || no.old_addrs
1201 || cilen != CILEN_ADDRS)
1206 if (ciaddr1 && go->accept_local)
1207 try.ouraddr = ciaddr1;
1210 if (ciaddr2 && go->accept_remote)
1211 try.hisaddr = ciaddr2;
1215 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
1220 if (ciaddr1 && go->accept_local)
1221 try.ouraddr = ciaddr1;
1222 if (try.ouraddr != 0)
1227 if (go->req_dns1 || no.req_dns1 || cilen != CILEN_ADDR)
1230 try.dnsaddr[0] = htonl(l);
1235 if (go->req_dns2 || no.req_dns2 || cilen != CILEN_ADDR)
1238 try.dnsaddr[1] = htonl(l);
1244 if (cilen != CILEN_ADDR)
1249 try.winsaddr[citype == CI_MS_WINS2] = ciaddr1;
1256 * OK, the Nak is good. Now we can update state.
1257 * If there are any remaining options, we ignore them.
1259 if (f->state != OPENED)
1265 IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
1270 * ipcp_rejci - Reject some of our CIs.
1271 * Callback from fsm_rconfnakrej.
1274 ipcp_rejci(fsm *f, u_char *p, int len)
1276 ipcp_options *go = &ipcp_gotoptions[f->unit];
1277 u_char cimaxslotindex, ciflag, cilen;
1280 ipcp_options try; /* options to request next time */
1284 * Any Rejected CIs must be in exactly the same order that we sent.
1285 * Check packet length and CI length at each step.
1286 * If we find any deviations, then this packet is bad.
1288 #define REJCIADDRS(opt, neg, val1, val2) \
1290 (cilen = p[1]) == CILEN_ADDRS && \
1297 cilong = htonl(l); \
1298 /* Check rejected value. */ \
1299 if (cilong != val1) \
1302 cilong = htonl(l); \
1303 /* Check rejected value. */ \
1304 if (cilong != val2) \
1306 try.old_addrs = 0; \
1309 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
1311 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
1316 GETSHORT(cishort, p); \
1317 /* Check rejected value. */ \
1318 if (cishort != val) \
1321 GETCHAR(cimaxslotindex, p); \
1322 if (cimaxslotindex != maxslot) \
1324 GETCHAR(ciflag, p); \
1325 if (ciflag != cflag) \
1331 #define REJCIADDR(opt, neg, val) \
1333 (cilen = p[1]) == CILEN_ADDR && \
1340 cilong = htonl(l); \
1341 /* Check rejected value. */ \
1342 if (cilong != val) \
1347 #define REJCIDNS(opt, neg, dnsaddr) \
1349 ((cilen = p[1]) == CILEN_ADDR) && \
1356 cilong = htonl(l); \
1357 /* Check rejected value. */ \
1358 if (cilong != dnsaddr) \
1363 #define REJCIWINS(opt, neg, addr) \
1365 ((cilen = p[1]) == CILEN_ADDR) && \
1372 cilong = htonl(l); \
1373 /* Check rejected value. */ \
1374 if (cilong != addr) \
1379 REJCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs,
1380 go->ouraddr, go->hisaddr);
1382 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
1383 go->maxslotindex, go->cflag);
1385 REJCIADDR(CI_ADDR, neg_addr, go->ouraddr);
1387 REJCIDNS(CI_MS_DNS1, req_dns1, go->dnsaddr[0]);
1389 REJCIDNS(CI_MS_DNS2, req_dns2, go->dnsaddr[1]);
1391 REJCIWINS(CI_MS_WINS1, req_wins1, go->winsaddr[0]);
1393 REJCIWINS(CI_MS_WINS2, req_wins2, go->winsaddr[1]);
1396 * If there are any remaining CIs, then this packet is bad.
1401 * Now we can update state.
1403 if (f->state != OPENED)
1408 IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
1414 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
1415 * Callback from fsm_rconfreq, Receive Configure Request
1417 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1418 * appropriately. If reject_if_disagree is non-zero, doesn't return
1419 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1422 ipcp_reqci(fsm *f, u_char *inp, int *len, int reject_if_disagree)
1424 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1425 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1426 ipcp_options *ao = &ipcp_allowoptions[f->unit];
1427 u_char *cip, *next; /* Pointer to current and next CIs */
1428 u_short cilen, citype; /* Parsed len, type */
1429 u_short cishort; /* Parsed short value */
1430 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
1431 int rc = CONFACK; /* Final packet return code */
1432 int orc; /* Individual option return code */
1433 u_char *p; /* Pointer to next char to parse */
1434 u_char *ucp = inp; /* Pointer to current output char */
1435 int l = *len; /* Length left */
1436 u_char maxslotindex, cflag;
1440 * Reset all his options.
1442 BZERO(ho, sizeof(*ho));
1445 * Process all his options.
1449 orc = CONFACK; /* Assume success */
1450 cip = p = next; /* Remember begining of CI */
1451 if (l < 2 || /* Not enough data for CI header or */
1452 p[1] < 2 || /* CI length too small or */
1453 p[1] > l) { /* CI length too big? */
1454 IPCPDEBUG(("ipcp_reqci: bad CI length!"));
1455 orc = CONFREJ; /* Reject bad CI */
1456 cilen = l; /* Reject till end of packet */
1457 l = 0; /* Don't loop again */
1460 GETCHAR(citype, p); /* Parse CI type */
1461 GETCHAR(cilen, p); /* Parse CI length */
1462 l -= cilen; /* Adjust remaining length */
1463 next += cilen; /* Step to next CI */
1465 switch (citype) { /* Check CI type */
1467 if (!ao->old_addrs || ho->neg_addr ||
1468 cilen != CILEN_ADDRS) { /* Check CI length */
1469 orc = CONFREJ; /* Reject CI */
1474 * If he has no address, or if we both have his address but
1475 * disagree about it, then NAK it with our idea.
1476 * In particular, if we don't know his address, but he does,
1479 GETLONG(tl, p); /* Parse source address (his) */
1480 ciaddr1 = htonl(tl);
1481 if (ciaddr1 != wo->hisaddr
1482 && (ciaddr1 == 0 || !wo->accept_remote)) {
1484 if (!reject_if_disagree) {
1485 DECPTR(sizeof(u_int32_t), p);
1486 tl = ntohl(wo->hisaddr);
1489 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1491 * If neither we nor he knows his address, reject the option.
1494 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1499 * If he doesn't know our address, or if we both have our address
1500 * but disagree about it, then NAK it with our idea.
1502 GETLONG(tl, p); /* Parse desination address (ours) */
1503 ciaddr2 = htonl(tl);
1504 if (ciaddr2 != wo->ouraddr) {
1505 if (ciaddr2 == 0 || !wo->accept_local) {
1507 if (!reject_if_disagree) {
1508 DECPTR(sizeof(u_int32_t), p);
1509 tl = ntohl(wo->ouraddr);
1513 wo->ouraddr = ciaddr2; /* accept peer's idea */
1518 ho->hisaddr = ciaddr1;
1519 ho->ouraddr = ciaddr2;
1523 if (!ao->neg_addr || ho->old_addrs ||
1524 cilen != CILEN_ADDR) { /* Check CI length */
1525 orc = CONFREJ; /* Reject CI */
1530 * If he has no address, or if we both have his address but
1531 * disagree about it, then NAK it with our idea.
1532 * In particular, if we don't know his address, but he does,
1535 GETLONG(tl, p); /* Parse source address (his) */
1536 ciaddr1 = htonl(tl);
1537 if (ciaddr1 != wo->hisaddr
1538 && (ciaddr1 == 0 || !wo->accept_remote)) {
1540 if (!reject_if_disagree) {
1541 DECPTR(sizeof(u_int32_t), p);
1542 tl = ntohl(wo->hisaddr);
1545 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1547 * Don't ACK an address of 0.0.0.0 - reject it instead.
1550 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1555 ho->hisaddr = ciaddr1;
1560 /* Microsoft primary or secondary DNS request */
1561 d = citype == CI_MS_DNS2;
1563 /* If we do not have a DNS address then we cannot send it */
1564 if (ao->dnsaddr[d] == 0 ||
1565 cilen != CILEN_ADDR) { /* Check CI length */
1566 orc = CONFREJ; /* Reject CI */
1570 if (htonl(tl) != ao->dnsaddr[d]) {
1571 DECPTR(sizeof(u_int32_t), p);
1572 tl = ntohl(ao->dnsaddr[d]);
1580 /* Microsoft primary or secondary WINS request */
1581 d = citype == CI_MS_WINS2;
1583 /* If we do not have a WINS address then we cannot send it */
1584 if (ao->winsaddr[d] == 0 ||
1585 cilen != CILEN_ADDR) { /* Check CI length */
1586 orc = CONFREJ; /* Reject CI */
1590 if (htonl(tl) != ao->winsaddr[d]) {
1591 DECPTR(sizeof(u_int32_t), p);
1592 tl = ntohl(ao->winsaddr[d]);
1598 case CI_COMPRESSTYPE:
1600 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1604 GETSHORT(cishort, p);
1606 if (!(cishort == IPCP_VJ_COMP ||
1607 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1613 ho->vj_protocol = cishort;
1614 if (cilen == CILEN_VJ) {
1615 GETCHAR(maxslotindex, p);
1616 if (maxslotindex > ao->maxslotindex) {
1618 if (!reject_if_disagree){
1620 PUTCHAR(ao->maxslotindex, p);
1624 if (cflag && !ao->cflag) {
1626 if (!reject_if_disagree){
1628 PUTCHAR(wo->cflag, p);
1631 ho->maxslotindex = maxslotindex;
1635 ho->maxslotindex = MAX_STATES - 1;
1645 if (orc == CONFACK && /* Good CI */
1646 rc != CONFACK) /* but prior CI wasnt? */
1647 continue; /* Don't send this one */
1649 if (orc == CONFNAK) { /* Nak this CI? */
1650 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1651 orc = CONFREJ; /* Get tough if so */
1653 if (rc == CONFREJ) /* Rejecting prior CI? */
1654 continue; /* Don't send this one */
1655 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1656 rc = CONFNAK; /* Not anymore... */
1657 ucp = inp; /* Backup */
1662 if (orc == CONFREJ && /* Reject this CI */
1663 rc != CONFREJ) { /* but no prior ones? */
1665 ucp = inp; /* Backup */
1668 /* Need to move CI? */
1670 BCOPY(cip, ucp, cilen); /* Move it */
1672 /* Update output pointer */
1677 * If we aren't rejecting this packet, and we want to negotiate
1678 * their address, and they didn't send their address, then we
1679 * send a NAK with a CI_ADDR option appended. We assume the
1680 * input buffer is long enough that we can append the extra
1683 if (rc != CONFREJ && !ho->neg_addr && !ho->old_addrs &&
1684 wo->req_addr && !reject_if_disagree && !noremoteip) {
1685 if (rc == CONFACK) {
1687 ucp = inp; /* reset pointer */
1688 wo->req_addr = 0; /* don't ask again */
1690 PUTCHAR(CI_ADDR, ucp);
1691 PUTCHAR(CILEN_ADDR, ucp);
1692 tl = ntohl(wo->hisaddr);
1696 *len = ucp - inp; /* Compute output length */
1697 IPCPDEBUG(("ipcp: returning Configure-%s", CODENAME(rc)));
1698 return (rc); /* Return final code */
1703 * ip_check_options - check that any IP-related options are OK,
1704 * and assign appropriate defaults.
1707 ip_check_options(void)
1711 ipcp_options *wo = &ipcp_wantoptions[0];
1714 * Default our local IP address based on our hostname.
1715 * If local IP address already given, don't bother.
1717 if (wo->ouraddr == 0 && !disable_defaultip) {
1719 * Look up our hostname (possibly with domain name appended)
1720 * and take the first IP address as our local IP address.
1721 * If there isn't an IP address for our hostname, too bad.
1723 wo->accept_local = 1; /* don't insist on this default value */
1724 if ((hp = gethostbyname(hostname)) != NULL) {
1725 local = *(u_int32_t *)hp->h_addr;
1726 if (local != 0 && !bad_ip_adrs(local))
1727 wo->ouraddr = local;
1730 ask_for_local = wo->ouraddr != 0 || !disable_defaultip;
1735 * ip_demand_conf - configure the interface as though
1736 * IPCP were up, for use with dial-on-demand.
1739 ip_demand_conf(int u)
1741 ipcp_options *wo = &ipcp_wantoptions[u];
1743 if (wo->hisaddr == 0 && !noremoteip) {
1744 /* make up an arbitrary address for the peer */
1745 wo->hisaddr = htonl(0x0a707070 + ifunit);
1746 wo->accept_remote = 1;
1748 if (wo->ouraddr == 0) {
1749 /* make up an arbitrary address for us */
1750 wo->ouraddr = htonl(0x0a404040 + ifunit);
1751 wo->accept_local = 1;
1752 ask_for_local = 0; /* don't tell the peer this address */
1754 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1756 ipcp_script(_PATH_IPPREUP, 1);
1759 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1761 if (wo->default_route)
1762 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr,
1763 wo->replace_default_route))
1764 default_route_set[u] = 1;
1766 if (sifproxyarp(u, wo->hisaddr))
1767 proxy_arp_set[u] = 1;
1769 notice("local IP address %I", wo->ouraddr);
1771 notice("remote IP address %I", wo->hisaddr);
1778 * ipcp_up - IPCP has come UP.
1780 * Configure the IP network interface appropriately and bring it up.
1786 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1787 ipcp_options *go = &ipcp_gotoptions[f->unit];
1788 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1791 IPCPDEBUG(("ipcp: up"));
1794 * We must have a non-zero IP address for both ends of the link.
1796 if (!ho->neg_addr && !ho->old_addrs)
1797 ho->hisaddr = wo->hisaddr;
1799 if (!(go->neg_addr || go->old_addrs) && (wo->neg_addr || wo->old_addrs)
1800 && wo->ouraddr != 0) {
1801 error("Peer refused to agree to our IP address");
1802 ipcp_close(f->unit, "Refused our IP address");
1805 if (go->ouraddr == 0) {
1806 error("Could not determine local IP address");
1807 ipcp_close(f->unit, "Could not determine local IP address");
1810 if (ho->hisaddr == 0 && !noremoteip) {
1811 ho->hisaddr = htonl(0x0a404040 + ifunit);
1812 warn("Could not determine remote IP address: defaulting to %I",
1815 script_setenv("IPLOCAL", ip_ntoa(go->ouraddr), 0);
1816 if (ho->hisaddr != 0)
1817 script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr), 1);
1824 script_setenv("DNS1", ip_ntoa(go->dnsaddr[0]), 0);
1826 script_setenv("DNS2", ip_ntoa(go->dnsaddr[1]), 0);
1827 if (usepeerdns && (go->dnsaddr[0] || go->dnsaddr[1])) {
1828 script_setenv("USEPEERDNS", "1", 0);
1829 create_resolv(go->dnsaddr[0], go->dnsaddr[1]);
1832 if (go->winsaddr[0])
1833 script_setenv("WINS1", ip_ntoa(go->winsaddr[0]), 0);
1834 if (go->winsaddr[1])
1835 script_setenv("WINS2", ip_ntoa(go->winsaddr[1]), 0);
1836 if (usepeerwins && (go->winsaddr[0] || go->winsaddr[1]))
1837 script_setenv("USEPEERWINS", "1", 0);
1840 * Check that the peer is allowed to use the IP address it wants.
1842 if (ho->hisaddr != 0 && !auth_ip_addr(f->unit, ho->hisaddr)) {
1843 error("Peer is not authorized to use remote address %I", ho->hisaddr);
1844 ipcp_close(f->unit, "Unauthorized remote IP address");
1848 /* set tcp compression */
1849 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1852 * If we are doing dial-on-demand, the interface is already
1853 * configured, so we put out any saved-up packets, then set the
1854 * interface to pass IP packets.
1857 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1858 ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr,
1859 wo->replace_default_route);
1860 if (go->ouraddr != wo->ouraddr) {
1861 warn("Local IP address changed to %I", go->ouraddr);
1862 script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr), 0);
1863 wo->ouraddr = go->ouraddr;
1865 script_unsetenv("OLDIPLOCAL");
1866 if (ho->hisaddr != wo->hisaddr && wo->hisaddr != 0) {
1867 warn("Remote IP address changed to %I", ho->hisaddr);
1868 script_setenv("OLDIPREMOTE", ip_ntoa(wo->hisaddr), 0);
1869 wo->hisaddr = ho->hisaddr;
1871 script_unsetenv("OLDIPREMOTE");
1873 /* Set the interface to the new addresses */
1874 mask = GetMask(go->ouraddr);
1875 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1877 warn("Interface configuration failed");
1878 ipcp_close(f->unit, "Interface configuration failed");
1882 /* assign a default route through the interface if required */
1883 if (ipcp_wantoptions[f->unit].default_route)
1884 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr,
1885 wo->replace_default_route))
1886 default_route_set[f->unit] = 1;
1888 /* Make a proxy ARP entry if requested. */
1889 if (ho->hisaddr != 0 && ipcp_wantoptions[f->unit].proxy_arp)
1890 if (sifproxyarp(f->unit, ho->hisaddr))
1891 proxy_arp_set[f->unit] = 1;
1894 demand_rexmit(PPP_IP);
1895 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1899 * Set IP addresses and (if specified) netmask.
1901 mask = GetMask(go->ouraddr);
1903 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1904 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1906 warn("Interface configuration failed");
1907 ipcp_close(f->unit, "Interface configuration failed");
1912 ifindex = if_nametoindex(ifname);
1914 /* run the pre-up script, if any, and wait for it to finish */
1915 ipcp_script(_PATH_IPPREUP, 1);
1917 /* check if preup script renamed the interface */
1918 if (!if_indextoname(ifindex, ifname)) {
1919 error("Interface index %d failed to get renamed by a pre-up script", ifindex);
1920 ipcp_close(f->unit, "Interface configuration failed");
1924 /* bring the interface up for IP */
1925 if (!sifup(f->unit)) {
1927 warn("Interface failed to come up");
1928 ipcp_close(f->unit, "Interface configuration failed");
1932 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1933 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1935 warn("Interface configuration failed");
1936 ipcp_close(f->unit, "Interface configuration failed");
1940 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1942 /* assign a default route through the interface if required */
1943 if (ipcp_wantoptions[f->unit].default_route)
1944 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr,
1945 wo->replace_default_route))
1946 default_route_set[f->unit] = 1;
1948 /* Make a proxy ARP entry if requested. */
1949 if (ho->hisaddr != 0 && ipcp_wantoptions[f->unit].proxy_arp)
1950 if (sifproxyarp(f->unit, ho->hisaddr))
1951 proxy_arp_set[f->unit] = 1;
1953 ipcp_wantoptions[0].ouraddr = go->ouraddr;
1955 notice("local IP address %I", go->ouraddr);
1956 if (ho->hisaddr != 0)
1957 notice("remote IP address %I", ho->hisaddr);
1959 notice("primary DNS address %I", go->dnsaddr[0]);
1961 notice("secondary DNS address %I", go->dnsaddr[1]);
1964 reset_link_stats(f->unit);
1966 np_up(f->unit, PPP_IP);
1969 notify(ip_up_notifier, 0);
1974 * Execute the ip-up script, like this:
1975 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1977 if (ipcp_script_state == s_down && ipcp_script_pid == 0) {
1978 ipcp_script_state = s_up;
1979 ipcp_script(path_ipup, 0);
1985 * ipcp_down - IPCP has gone DOWN.
1987 * Take the IP network interface down, clear its addresses
1988 * and delete routes through it.
1993 IPCPDEBUG(("ipcp: down"));
1994 /* XXX a bit IPv4-centric here, we only need to get the stats
1995 * before the interface is marked down. */
1996 /* XXX more correct: we must get the stats before running the notifiers,
1997 * at least for the radius plugin */
1998 update_link_stats(f->unit);
1999 notify(ip_down_notifier, 0);
2004 np_down(f->unit, PPP_IP);
2006 sifvjcomp(f->unit, 0, 0, 0);
2008 print_link_stats(); /* _after_ running the notifiers and ip_down_hook(),
2009 * because print_link_stats() sets link_stats_valid
2013 * If we are doing dial-on-demand, set the interface
2014 * to queue up outgoing packets (for now).
2017 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
2019 sifnpmode(f->unit, PPP_IP, NPMODE_DROP);
2021 ipcp_clear_addrs(f->unit, ipcp_gotoptions[f->unit].ouraddr,
2022 ipcp_hisoptions[f->unit].hisaddr, 0);
2025 /* Execute the ip-down script */
2026 if (ipcp_script_state == s_up && ipcp_script_pid == 0) {
2027 ipcp_script_state = s_down;
2028 ipcp_script(path_ipdown, 0);
2034 * ipcp_clear_addrs() - clear the interface addresses, routes,
2035 * proxy arp entries, etc.
2038 ipcp_clear_addrs(int unit, u_int32_t ouraddr, u_int32_t hisaddr, bool replacedefaultroute)
2040 if (proxy_arp_set[unit]) {
2041 cifproxyarp(unit, hisaddr);
2042 proxy_arp_set[unit] = 0;
2044 /* If replacedefaultroute, sifdefaultroute will be called soon
2045 * with replacedefaultroute set and that will overwrite the current
2046 * default route. This is the case only when doing demand, otherwise
2047 * during demand, this cifdefaultroute would restore the old default
2048 * route which is not what we want in this case. In the non-demand
2049 * case, we'll delete the default route and restore the old if there
2050 * is one saved by an sifdefaultroute with replacedefaultroute.
2052 if (!replacedefaultroute && default_route_set[unit]) {
2053 cifdefaultroute(unit, ouraddr, hisaddr);
2054 default_route_set[unit] = 0;
2056 cifaddr(unit, ouraddr, hisaddr);
2061 * ipcp_finished - possibly shut down the lower layers.
2064 ipcp_finished(fsm *f)
2068 np_finished(f->unit, PPP_IP);
2074 * ipcp_script_done - called when the ip-up or ip-down script
2078 ipcp_script_done(void *arg)
2080 ipcp_script_pid = 0;
2081 switch (ipcp_script_state) {
2083 if (ipcp_fsm[0].state != OPENED) {
2084 ipcp_script_state = s_down;
2085 ipcp_script(path_ipdown, 0);
2089 if (ipcp_fsm[0].state == OPENED) {
2090 ipcp_script_state = s_up;
2091 ipcp_script(path_ipup, 0);
2099 * ipcp_script - Execute a script with arguments
2100 * interface-name tty-name speed local-IP remote-IP.
2103 ipcp_script(char *script, int wait)
2105 char strspeed[32], strlocal[32], strremote[32];
2108 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2109 slprintf(strlocal, sizeof(strlocal), "%I", ipcp_gotoptions[0].ouraddr);
2110 slprintf(strremote, sizeof(strremote), "%I", ipcp_hisoptions[0].hisaddr);
2117 argv[5] = strremote;
2121 run_program(script, argv, 0, NULL, NULL, 1);
2123 ipcp_script_pid = run_program(script, argv, 0, ipcp_script_done,
2128 * create_resolv - create the replacement resolv.conf file
2131 create_resolv(u_int32_t peerdns1, u_int32_t peerdns2)
2135 f = fopen(_PATH_RESOLV, "w");
2137 error("Failed to create %s: %m", _PATH_RESOLV);
2142 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns1));
2145 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns2));
2148 error("Write failed to %s: %m", _PATH_RESOLV);
2154 * ipcp_printpkt - print the contents of an IPCP packet.
2156 static char *ipcp_codenames[] = {
2157 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
2158 "TermReq", "TermAck", "CodeRej"
2162 ipcp_printpkt(u_char *p, int plen,
2163 void (*printer) (void *, char *, ...), void *arg)
2165 int code, id, len, olen;
2166 u_char *pstart, *optend;
2170 if (plen < HEADERLEN)
2176 if (len < HEADERLEN || len > plen)
2179 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
2180 printer(arg, " %s", ipcp_codenames[code-1]);
2182 printer(arg, " code=0x%x", code);
2183 printer(arg, " id=0x%x", id);
2190 /* print option list */
2195 if (olen < 2 || olen > len) {
2203 if (olen == CILEN_ADDRS) {
2206 printer(arg, "addrs %I", htonl(cilong));
2208 printer(arg, " %I", htonl(cilong));
2211 case CI_COMPRESSTYPE:
2212 if (olen >= CILEN_COMPRESS) {
2214 GETSHORT(cishort, p);
2215 printer(arg, "compress ");
2220 case IPCP_VJ_COMP_OLD:
2221 printer(arg, "old-VJ");
2224 printer(arg, "0x%x", cishort);
2229 if (olen == CILEN_ADDR) {
2232 printer(arg, "addr %I", htonl(cilong));
2239 printer(arg, "ms-dns%d %I", (code == CI_MS_DNS1? 1: 2),
2246 printer(arg, "ms-wins %I", htonl(cilong));
2249 while (p < optend) {
2251 printer(arg, " %.2x", code);
2259 if (len > 0 && *p >= ' ' && *p < 0x7f) {
2261 print_string((char *)p, len, printer, arg);
2268 /* print the rest of the bytes in the packet */
2269 for (; len > 0; --len) {
2271 printer(arg, " %.2x", code);
2278 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
2279 * We don't bring the link up for IP fragments or for TCP FIN packets
2282 #define IP_HDRLEN 20 /* bytes */
2283 #define IP_OFFMASK 0x1fff
2285 #define IPPROTO_TCP 6
2287 #define TCP_HDRLEN 20
2291 * We use these macros because the IP header may be at an odd address,
2292 * and some compilers might use word loads to get th_off or ip_hl.
2295 #define net_short(x) (((x)[0] << 8) + (x)[1])
2296 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
2297 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
2298 #define get_ipproto(x) (((unsigned char *)(x))[9])
2299 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
2300 #define get_tcpflags(x) (((unsigned char *)(x))[13])
2303 ip_active_pkt(u_char *pkt, int len)
2310 if (len < IP_HDRLEN)
2312 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
2314 if (get_ipproto(pkt) != IPPROTO_TCP)
2316 hlen = get_iphl(pkt) * 4;
2317 if (len < hlen + TCP_HDRLEN)
2320 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)