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 int ipcp_is_up; /* have called np_up() */
93 static int ipcp_is_open; /* haven't called np_finished() */
94 static bool ask_for_local; /* request our address from peer */
95 static char vj_value[8]; /* string form of vj option value */
96 static char netmask_str[20]; /* string form of netmask value */
99 * Callbacks for fsm code. (CI = Configuration Information)
101 static void ipcp_resetci (fsm *); /* Reset our CI */
102 static int ipcp_cilen (fsm *); /* Return length of our CI */
103 static void ipcp_addci (fsm *, u_char *, int *); /* Add our CI */
104 static int ipcp_ackci (fsm *, u_char *, int); /* Peer ack'd our CI */
105 static int ipcp_nakci (fsm *, u_char *, int, int);/* Peer nak'd our CI */
106 static int ipcp_rejci (fsm *, u_char *, int); /* Peer rej'd our CI */
107 static int ipcp_reqci (fsm *, u_char *, int *, int); /* Rcv CI */
108 static void ipcp_up (fsm *); /* We're UP */
109 static void ipcp_down (fsm *); /* We're DOWN */
110 static void ipcp_finished (fsm *); /* Don't need lower layer */
112 fsm ipcp_fsm[NUM_PPP]; /* IPCP fsm structure */
114 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
115 ipcp_resetci, /* Reset our Configuration Information */
116 ipcp_cilen, /* Length of our Configuration Information */
117 ipcp_addci, /* Add our Configuration Information */
118 ipcp_ackci, /* ACK our Configuration Information */
119 ipcp_nakci, /* NAK our Configuration Information */
120 ipcp_rejci, /* Reject our Configuration Information */
121 ipcp_reqci, /* Request peer's Configuration Information */
122 ipcp_up, /* Called when fsm reaches OPENED state */
123 ipcp_down, /* Called when fsm leaves OPENED state */
124 NULL, /* Called when we want the lower layer up */
125 ipcp_finished, /* Called when we want the lower layer down */
126 NULL, /* Called when Protocol-Reject received */
127 NULL, /* Retransmission is necessary */
128 NULL, /* Called to handle protocol-specific codes */
129 "IPCP" /* String name of protocol */
133 * Command-line options.
135 static int setvjslots (char **);
136 static int setdnsaddr (char **);
137 static int setwinsaddr (char **);
138 static int setnetmask (char **);
139 int setipaddr (char *, char **, int);
140 static void printipaddr (option_t *, void (*)(void *, char *,...),void *);
142 static option_t ipcp_option_list[] = {
143 { "noip", o_bool, &ipcp_protent.enabled_flag,
144 "Disable IP and IPCP" },
145 { "-ip", o_bool, &ipcp_protent.enabled_flag,
146 "Disable IP and IPCP", OPT_ALIAS },
148 { "novj", o_bool, &ipcp_wantoptions[0].neg_vj,
149 "Disable VJ compression", OPT_A2CLR, &ipcp_allowoptions[0].neg_vj },
150 { "-vj", o_bool, &ipcp_wantoptions[0].neg_vj,
151 "Disable VJ compression", OPT_ALIAS | OPT_A2CLR,
152 &ipcp_allowoptions[0].neg_vj },
154 { "novjccomp", o_bool, &ipcp_wantoptions[0].cflag,
155 "Disable VJ connection-ID compression", OPT_A2CLR,
156 &ipcp_allowoptions[0].cflag },
157 { "-vjccomp", o_bool, &ipcp_wantoptions[0].cflag,
158 "Disable VJ connection-ID compression", OPT_ALIAS | OPT_A2CLR,
159 &ipcp_allowoptions[0].cflag },
161 { "vj-max-slots", o_special, (void *)setvjslots,
162 "Set maximum VJ header slots",
163 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, vj_value },
165 { "ipcp-accept-local", o_bool, &ipcp_wantoptions[0].accept_local,
166 "Accept peer's address for us", 1 },
167 { "ipcp-accept-remote", o_bool, &ipcp_wantoptions[0].accept_remote,
168 "Accept peer's address for it", 1 },
170 { "ipparam", o_string, &ipparam,
171 "Set ip script parameter", OPT_PRIO },
173 { "noipdefault", o_bool, &disable_defaultip,
174 "Don't use name for default IP adrs", 1 },
176 { "ms-dns", o_special, (void *)setdnsaddr,
177 "DNS address for the peer's use", OPT_A2LIST },
178 { "ms-wins", o_special, (void *)setwinsaddr,
179 "Nameserver for SMB over TCP/IP for peer", OPT_A2LIST },
181 { "ipcp-restart", o_int, &ipcp_fsm[0].timeouttime,
182 "Set timeout for IPCP", OPT_PRIO },
183 { "ipcp-max-terminate", o_int, &ipcp_fsm[0].maxtermtransmits,
184 "Set max #xmits for term-reqs", OPT_PRIO },
185 { "ipcp-max-configure", o_int, &ipcp_fsm[0].maxconfreqtransmits,
186 "Set max #xmits for conf-reqs", OPT_PRIO },
187 { "ipcp-max-failure", o_int, &ipcp_fsm[0].maxnakloops,
188 "Set max #conf-naks for IPCP", OPT_PRIO },
190 { "defaultroute", o_bool, &ipcp_wantoptions[0].default_route,
191 "Add default route", OPT_ENABLE|1, &ipcp_allowoptions[0].default_route },
192 { "nodefaultroute", o_bool, &ipcp_allowoptions[0].default_route,
193 "disable defaultroute option", OPT_A2CLR,
194 &ipcp_wantoptions[0].default_route },
195 { "-defaultroute", o_bool, &ipcp_allowoptions[0].default_route,
196 "disable defaultroute option", OPT_ALIAS | OPT_A2CLR,
197 &ipcp_wantoptions[0].default_route },
200 { "replacedefaultroute", o_bool,
201 &ipcp_wantoptions[0].replace_default_route,
202 "Replace default route", OPT_PRIV | 1
204 { "noreplacedefaultroute", o_bool,
205 &ipcp_wantoptions[0].replace_default_route,
206 "Do not replace default route", 0 },
208 { "proxyarp", o_bool, &ipcp_wantoptions[0].proxy_arp,
209 "Add proxy ARP entry", OPT_ENABLE|1, &ipcp_allowoptions[0].proxy_arp },
210 { "noproxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
211 "disable proxyarp option", OPT_A2CLR,
212 &ipcp_wantoptions[0].proxy_arp },
213 { "-proxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
214 "disable proxyarp option", OPT_ALIAS | OPT_A2CLR,
215 &ipcp_wantoptions[0].proxy_arp },
217 { "usepeerdns", o_bool, &usepeerdns,
218 "Ask peer for DNS address(es)", 1 },
220 { "netmask", o_special, (void *)setnetmask,
221 "set netmask", OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, netmask_str },
223 { "ipcp-no-addresses", o_bool, &ipcp_wantoptions[0].old_addrs,
224 "Disable old-style IP-Addresses usage", OPT_A2CLR,
225 &ipcp_allowoptions[0].old_addrs },
226 { "ipcp-no-address", o_bool, &ipcp_wantoptions[0].neg_addr,
227 "Disable IP-Address usage", OPT_A2CLR,
228 &ipcp_allowoptions[0].neg_addr },
230 { "noremoteip", o_bool, &noremoteip,
231 "Allow peer to have no IP address", 1 },
233 { "nosendip", o_bool, &ipcp_wantoptions[0].neg_addr,
234 "Don't send our IP address to peer", OPT_A2CLR,
235 &ipcp_wantoptions[0].old_addrs},
237 { "IP addresses", o_wild, (void *) &setipaddr,
238 "set local and remote IP addresses",
239 OPT_NOARG | OPT_A2PRINTER, (void *) &printipaddr },
245 * Protocol entry points from main code.
247 static void ipcp_init (int);
248 static void ipcp_open (int);
249 static void ipcp_close (int, char *);
250 static void ipcp_lowerup (int);
251 static void ipcp_lowerdown (int);
252 static void ipcp_input (int, u_char *, int);
253 static void ipcp_protrej (int);
254 static int ipcp_printpkt (u_char *, int,
255 void (*) (void *, char *, ...), void *);
256 static void ip_check_options (void);
257 static int ip_demand_conf (int);
258 static int ip_active_pkt (u_char *, int);
259 static void create_resolv (u_int32_t, u_int32_t);
261 struct protent ipcp_protent = {
281 static void ipcp_clear_addrs (int, u_int32_t, u_int32_t, bool);
282 static void ipcp_script (char *, int); /* Run an up/down script */
283 static void ipcp_script_done (void *);
286 * Lengths of configuration options.
289 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
290 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
291 #define CILEN_ADDR 6 /* new-style single address option */
292 #define CILEN_ADDRS 10 /* old-style dual address option */
295 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
296 (x) == CONFNAK ? "NAK" : "REJ")
299 * This state variable is used to ensure that we don't
300 * run an ipcp-up/down script while one is already running.
302 static enum script_state {
306 static pid_t ipcp_script_pid;
309 * Make a string representation of a network IP address.
312 ip_ntoa(u_int32_t ipaddr)
316 slprintf(b, sizeof(b), "%I", ipaddr);
325 * setvjslots - set maximum number of connection slots for VJ compression
328 setvjslots(char **argv)
332 if (!int_option(*argv, &value))
334 if (value < 2 || value > 16) {
335 option_error("vj-max-slots value must be between 2 and 16");
338 ipcp_wantoptions [0].maxslotindex =
339 ipcp_allowoptions[0].maxslotindex = value - 1;
340 slprintf(vj_value, sizeof(vj_value), "%d", value);
345 * setdnsaddr - set the dns address(es)
348 setdnsaddr(char **argv)
353 dns = inet_addr(*argv);
354 if (dns == (u_int32_t) -1) {
355 if ((hp = gethostbyname(*argv)) == NULL) {
356 option_error("invalid address parameter '%s' for ms-dns option",
360 dns = *(u_int32_t *)hp->h_addr;
363 /* We take the last 2 values given, the 2nd-last as the primary
364 and the last as the secondary. If only one is given it
365 becomes both primary and secondary. */
366 if (ipcp_allowoptions[0].dnsaddr[1] == 0)
367 ipcp_allowoptions[0].dnsaddr[0] = dns;
369 ipcp_allowoptions[0].dnsaddr[0] = ipcp_allowoptions[0].dnsaddr[1];
371 /* always set the secondary address value. */
372 ipcp_allowoptions[0].dnsaddr[1] = dns;
378 * setwinsaddr - set the wins address(es)
379 * This is primrarly used with the Samba package under UNIX or for pointing
380 * the caller to the existing WINS server on a Windows NT platform.
383 setwinsaddr(char **argv)
388 wins = inet_addr(*argv);
389 if (wins == (u_int32_t) -1) {
390 if ((hp = gethostbyname(*argv)) == NULL) {
391 option_error("invalid address parameter '%s' for ms-wins option",
395 wins = *(u_int32_t *)hp->h_addr;
398 /* We take the last 2 values given, the 2nd-last as the primary
399 and the last as the secondary. If only one is given it
400 becomes both primary and secondary. */
401 if (ipcp_allowoptions[0].winsaddr[1] == 0)
402 ipcp_allowoptions[0].winsaddr[0] = wins;
404 ipcp_allowoptions[0].winsaddr[0] = ipcp_allowoptions[0].winsaddr[1];
406 /* always set the secondary address value. */
407 ipcp_allowoptions[0].winsaddr[1] = wins;
413 * setipaddr - Set the IP address
414 * If doit is 0, the call is to check whether this option is
415 * potentially an IP address specification.
416 * Not static so that plugins can call it to set the addresses
419 setipaddr(char *arg, char **argv, int doit)
423 u_int32_t local, remote;
424 ipcp_options *wo = &ipcp_wantoptions[0];
425 static int prio_local = 0, prio_remote = 0;
428 * IP address pair separated by ":".
430 if ((colon = strchr(arg, ':')) == NULL)
436 * If colon first character, then no local addr.
438 if (colon != arg && option_priority >= prio_local) {
440 if ((local = inet_addr(arg)) == (u_int32_t) -1) {
441 if ((hp = gethostbyname(arg)) == NULL) {
442 option_error("unknown host: %s", arg);
445 local = *(u_int32_t *)hp->h_addr;
447 if (bad_ip_adrs(local)) {
448 option_error("bad local IP address %s", ip_ntoa(local));
454 prio_local = option_priority;
458 * If colon last character, then no remote addr.
460 if (*++colon != '\0' && option_priority >= prio_remote) {
461 if ((remote = inet_addr(colon)) == (u_int32_t) -1) {
462 if ((hp = gethostbyname(colon)) == NULL) {
463 option_error("unknown host: %s", colon);
466 remote = *(u_int32_t *)hp->h_addr;
467 if (remote_name[0] == 0)
468 strlcpy(remote_name, colon, sizeof(remote_name));
470 if (bad_ip_adrs(remote)) {
471 option_error("bad remote IP address %s", ip_ntoa(remote));
475 wo->hisaddr = remote;
476 prio_remote = option_priority;
483 printipaddr(option_t *opt, void (*printer) (void *, char *, ...), void *arg)
485 ipcp_options *wo = &ipcp_wantoptions[0];
487 if (wo->ouraddr != 0)
488 printer(arg, "%I", wo->ouraddr);
490 if (wo->hisaddr != 0)
491 printer(arg, "%I", wo->hisaddr);
495 * setnetmask - set the netmask to be used on the interface.
498 setnetmask(char **argv)
505 * Unfortunately, if we use inet_addr, we can't tell whether
506 * a result of all 1s is an error or a valid 255.255.255.255.
509 n = parse_dotted_ip(p, &mask);
513 if (n == 0 || p[n] != 0 || (netmask & ~mask) != 0) {
514 option_error("invalid netmask value '%s'", *argv);
519 slprintf(netmask_str, sizeof(netmask_str), "%I", mask);
525 parse_dotted_ip(char *p, u_int32_t *vp)
533 b = strtoul(p, &endp, 0);
539 /* accept e.g. 0xffffff00 */
557 * ipcp_init - Initialize IPCP.
562 fsm *f = &ipcp_fsm[unit];
563 ipcp_options *wo = &ipcp_wantoptions[unit];
564 ipcp_options *ao = &ipcp_allowoptions[unit];
567 f->protocol = PPP_IPCP;
568 f->callbacks = &ipcp_callbacks;
569 fsm_init(&ipcp_fsm[unit]);
572 * Some 3G modems use repeated IPCP NAKs as a way of stalling
573 * until they can contact a server on the network, so we increase
574 * the default number of NAKs we accept before we start treating
577 f->maxnakloops = 100;
579 memset(wo, 0, sizeof(*wo));
580 memset(ao, 0, sizeof(*ao));
582 wo->neg_addr = wo->old_addrs = 1;
584 wo->vj_protocol = IPCP_VJ_COMP;
585 wo->maxslotindex = MAX_STATES - 1; /* really max index */
589 /* max slots and slot-id compression are currently hardwired in */
590 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
593 ao->neg_addr = ao->old_addrs = 1;
595 ao->maxslotindex = MAX_STATES - 1;
599 * XXX These control whether the user may use the proxyarp
600 * and defaultroute options.
603 ao->default_route = 1;
608 * ipcp_open - IPCP is allowed to come up.
613 fsm_open(&ipcp_fsm[unit]);
619 * ipcp_close - Take IPCP down.
622 ipcp_close(int unit, char *reason)
624 fsm_close(&ipcp_fsm[unit], reason);
629 * ipcp_lowerup - The lower layer is up.
632 ipcp_lowerup(int unit)
634 fsm_lowerup(&ipcp_fsm[unit]);
639 * ipcp_lowerdown - The lower layer is down.
642 ipcp_lowerdown(int unit)
644 fsm_lowerdown(&ipcp_fsm[unit]);
649 * ipcp_input - Input IPCP packet.
652 ipcp_input(int unit, u_char *p, int len)
654 fsm_input(&ipcp_fsm[unit], p, len);
659 * ipcp_protrej - A Protocol-Reject was received for IPCP.
661 * Pretend the lower layer went down, so we shut up.
664 ipcp_protrej(int unit)
666 fsm_lowerdown(&ipcp_fsm[unit]);
671 * ipcp_resetci - Reset our CI.
672 * Called by fsm_sconfreq, Send Configure Request.
677 ipcp_options *wo = &ipcp_wantoptions[f->unit];
678 ipcp_options *go = &ipcp_gotoptions[f->unit];
679 ipcp_options *ao = &ipcp_allowoptions[f->unit];
681 wo->req_addr = (wo->neg_addr || wo->old_addrs) &&
682 (ao->neg_addr || ao->old_addrs);
683 if (wo->ouraddr == 0)
684 wo->accept_local = 1;
685 if (wo->hisaddr == 0)
686 wo->accept_remote = 1;
687 wo->req_dns1 = usepeerdns; /* Request DNS addresses from the peer */
688 wo->req_dns2 = usepeerdns;
692 if (ip_choose_hook) {
693 ip_choose_hook(&wo->hisaddr);
695 wo->accept_remote = 0;
698 BZERO(&ipcp_hisoptions[f->unit], sizeof(ipcp_options));
703 * ipcp_cilen - Return length of our CI.
704 * Called by fsm_sconfreq, Send Configure Request.
709 ipcp_options *go = &ipcp_gotoptions[f->unit];
710 ipcp_options *wo = &ipcp_wantoptions[f->unit];
711 ipcp_options *ho = &ipcp_hisoptions[f->unit];
713 #define LENCIADDRS(neg) (neg ? CILEN_ADDRS : 0)
714 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
715 #define LENCIADDR(neg) (neg ? CILEN_ADDR : 0)
716 #define LENCIDNS(neg) LENCIADDR(neg)
717 #define LENCIWINS(neg) LENCIADDR(neg)
720 * First see if we want to change our options to the old
721 * forms because we have received old forms from the peer.
723 if (go->neg_addr && go->old_addrs && !ho->neg_addr && ho->old_addrs)
725 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
726 /* try an older style of VJ negotiation */
727 /* use the old style only if the peer did */
728 if (ho->neg_vj && ho->old_vj) {
731 go->vj_protocol = ho->vj_protocol;
735 return (LENCIADDRS(!go->neg_addr && go->old_addrs) +
736 LENCIVJ(go->neg_vj, go->old_vj) +
737 LENCIADDR(go->neg_addr) +
738 LENCIDNS(go->req_dns1) +
739 LENCIDNS(go->req_dns2) +
740 LENCIWINS(go->winsaddr[0]) +
741 LENCIWINS(go->winsaddr[1])) ;
746 * ipcp_addci - Add our desired CIs to a packet.
747 * Called by fsm_sconfreq, Send Configure Request.
750 ipcp_addci(fsm *f, u_char *ucp, int *lenp)
752 ipcp_options *go = &ipcp_gotoptions[f->unit];
755 #define ADDCIADDRS(opt, neg, val1, val2) \
757 if (len >= CILEN_ADDRS) { \
760 PUTCHAR(CILEN_ADDRS, ucp); \
765 len -= CILEN_ADDRS; \
770 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
772 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
773 if (len >= vjlen) { \
775 PUTCHAR(vjlen, ucp); \
776 PUTSHORT(val, ucp); \
778 PUTCHAR(maxslotindex, ucp); \
779 PUTCHAR(cflag, ucp); \
786 #define ADDCIADDR(opt, neg, val) \
788 if (len >= CILEN_ADDR) { \
791 PUTCHAR(CILEN_ADDR, ucp); \
799 #define ADDCIDNS(opt, neg, addr) \
801 if (len >= CILEN_ADDR) { \
804 PUTCHAR(CILEN_ADDR, ucp); \
812 #define ADDCIWINS(opt, addr) \
814 if (len >= CILEN_ADDR) { \
817 PUTCHAR(CILEN_ADDR, ucp); \
825 ADDCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs, go->ouraddr,
828 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
829 go->maxslotindex, go->cflag);
831 ADDCIADDR(CI_ADDR, go->neg_addr, go->ouraddr);
833 ADDCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
835 ADDCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
837 ADDCIWINS(CI_MS_WINS1, go->winsaddr[0]);
839 ADDCIWINS(CI_MS_WINS2, go->winsaddr[1]);
846 * ipcp_ackci - Ack our CIs.
847 * Called by fsm_rconfack, Receive Configure ACK.
854 ipcp_ackci(fsm *f, u_char *p, int len)
856 ipcp_options *go = &ipcp_gotoptions[f->unit];
857 u_short cilen, citype, cishort;
859 u_char cimaxslotindex, cicflag;
862 * CIs must be in exactly the same order that we sent...
863 * Check packet length and CI length at each step.
864 * If we find any deviations, then this packet is bad.
867 #define ACKCIADDRS(opt, neg, val1, val2) \
870 if ((len -= CILEN_ADDRS) < 0) \
872 GETCHAR(citype, p); \
874 if (cilen != CILEN_ADDRS || \
879 if (val1 != cilong) \
883 if (val2 != cilong) \
887 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
889 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
890 if ((len -= vjlen) < 0) \
892 GETCHAR(citype, p); \
894 if (cilen != vjlen || \
897 GETSHORT(cishort, p); \
898 if (cishort != val) \
901 GETCHAR(cimaxslotindex, p); \
902 if (cimaxslotindex != maxslotindex) \
904 GETCHAR(cicflag, p); \
905 if (cicflag != cflag) \
910 #define ACKCIADDR(opt, neg, val) \
913 if ((len -= CILEN_ADDR) < 0) \
915 GETCHAR(citype, p); \
917 if (cilen != CILEN_ADDR || \
926 #define ACKCIDNS(opt, neg, addr) \
929 if ((len -= CILEN_ADDR) < 0) \
931 GETCHAR(citype, p); \
933 if (cilen != CILEN_ADDR || citype != opt) \
937 if (addr != cilong) \
941 #define ACKCIWINS(opt, addr) \
944 if ((len -= CILEN_ADDR) < 0) \
946 GETCHAR(citype, p); \
948 if (cilen != CILEN_ADDR || citype != opt) \
952 if (addr != cilong) \
956 ACKCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs, go->ouraddr,
959 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
960 go->maxslotindex, go->cflag);
962 ACKCIADDR(CI_ADDR, go->neg_addr, go->ouraddr);
964 ACKCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
966 ACKCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
968 ACKCIWINS(CI_MS_WINS1, go->winsaddr[0]);
970 ACKCIWINS(CI_MS_WINS2, go->winsaddr[1]);
973 * If there are any remaining CIs, then this packet is bad.
980 IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
985 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
986 * This should not modify any state if the Nak is bad
987 * or if IPCP is in the OPENED state.
988 * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
995 ipcp_nakci(fsm *f, u_char *p, int len, int treat_as_reject)
997 ipcp_options *wo = &ipcp_wantoptions[f->unit];
998 ipcp_options *go = &ipcp_gotoptions[f->unit];
999 u_char cimaxslotindex, cicflag;
1000 u_char citype, cilen, *next;
1002 u_int32_t ciaddr1, ciaddr2, l, cidnsaddr;
1003 ipcp_options no; /* options we've seen Naks for */
1004 ipcp_options try; /* options to request next time */
1006 BZERO(&no, sizeof(no));
1010 * Any Nak'd CIs must be in exactly the same order that we sent.
1011 * Check packet length and CI length at each step.
1012 * If we find any deviations, then this packet is bad.
1014 #define NAKCIADDRS(opt, neg, code) \
1016 (cilen = p[1]) == CILEN_ADDRS && \
1022 ciaddr1 = htonl(l); \
1024 ciaddr2 = htonl(l); \
1029 #define NAKCIVJ(opt, neg, code) \
1031 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
1036 GETSHORT(cishort, p); \
1041 #define NAKCIADDR(opt, neg, code) \
1043 (cilen = p[1]) == CILEN_ADDR && \
1049 ciaddr1 = htonl(l); \
1054 #define NAKCIDNS(opt, neg, code) \
1056 ((cilen = p[1]) == CILEN_ADDR) && \
1062 cidnsaddr = htonl(l); \
1068 * Accept the peer's idea of {our,his} address, if different
1069 * from our idea, only if the accept_{local,remote} flag is set.
1071 NAKCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs,
1072 if (treat_as_reject) {
1075 if (go->accept_local && ciaddr1) {
1076 /* take his idea of our address */
1077 try.ouraddr = ciaddr1;
1079 if (go->accept_remote && ciaddr2) {
1080 /* take his idea of his address */
1081 try.hisaddr = ciaddr2;
1087 * Accept the peer's value of maxslotindex provided that it
1088 * is less than what we asked for. Turn off slot-ID compression
1089 * if the peer wants. Send old-style compress-type option if
1092 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
1093 if (treat_as_reject) {
1095 } else if (cilen == CILEN_VJ) {
1096 GETCHAR(cimaxslotindex, p);
1097 GETCHAR(cicflag, p);
1098 if (cishort == IPCP_VJ_COMP) {
1100 if (cimaxslotindex < go->maxslotindex)
1101 try.maxslotindex = cimaxslotindex;
1108 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
1110 try.vj_protocol = cishort;
1117 NAKCIADDR(CI_ADDR, neg_addr,
1118 if (treat_as_reject) {
1121 } else if (go->accept_local && ciaddr1) {
1122 /* take his idea of our address */
1123 try.ouraddr = ciaddr1;
1127 NAKCIDNS(CI_MS_DNS1, req_dns1,
1128 if (treat_as_reject) {
1131 try.dnsaddr[0] = cidnsaddr;
1135 NAKCIDNS(CI_MS_DNS2, req_dns2,
1136 if (treat_as_reject) {
1139 try.dnsaddr[1] = cidnsaddr;
1144 * There may be remaining CIs, if the peer is requesting negotiation
1145 * on an option that we didn't include in our request packet.
1146 * If they want to negotiate about IP addresses, we comply.
1147 * If they want us to ask for compression, we refuse.
1148 * If they want us to ask for ms-dns, we do that, since some
1149 * peers get huffy if we don't.
1151 while (len >= CILEN_VOID) {
1154 if ( cilen < CILEN_VOID || (len -= cilen) < 0 )
1156 next = p + cilen - 2;
1159 case CI_COMPRESSTYPE:
1160 if (go->neg_vj || no.neg_vj ||
1161 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
1166 if ((!go->neg_addr && go->old_addrs) || no.old_addrs
1167 || cilen != CILEN_ADDRS)
1172 if (ciaddr1 && go->accept_local)
1173 try.ouraddr = wo->old_addrs ? ciaddr1 : 0;
1176 if (ciaddr2 && go->accept_remote)
1177 try.hisaddr = ciaddr2;
1181 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
1186 if (ciaddr1 && go->accept_local)
1187 try.ouraddr = ciaddr1;
1188 if (try.ouraddr != 0 && wo->neg_addr)
1193 if (go->req_dns1 || no.req_dns1 || cilen != CILEN_ADDR)
1196 try.dnsaddr[0] = htonl(l);
1201 if (go->req_dns2 || no.req_dns2 || cilen != CILEN_ADDR)
1204 try.dnsaddr[1] = htonl(l);
1210 if (cilen != CILEN_ADDR)
1215 try.winsaddr[citype == CI_MS_WINS2] = ciaddr1;
1222 * OK, the Nak is good. Now we can update state.
1223 * If there are any remaining options, we ignore them.
1225 if (f->state != OPENED)
1231 IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
1237 * ipcp_rejci - Reject some of our CIs.
1238 * Callback from fsm_rconfnakrej.
1241 ipcp_rejci(fsm *f, u_char *p, int len)
1243 ipcp_options *go = &ipcp_gotoptions[f->unit];
1244 u_char cimaxslotindex, ciflag, cilen;
1247 ipcp_options try; /* options to request next time */
1251 * Any Rejected CIs must be in exactly the same order that we sent.
1252 * Check packet length and CI length at each step.
1253 * If we find any deviations, then this packet is bad.
1255 #define REJCIADDRS(opt, neg, val1, val2) \
1257 (cilen = p[1]) == CILEN_ADDRS && \
1264 cilong = htonl(l); \
1265 /* Check rejected value. */ \
1266 if (cilong != val1) \
1269 cilong = htonl(l); \
1270 /* Check rejected value. */ \
1271 if (cilong != val2) \
1273 try.old_addrs = 0; \
1276 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
1278 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
1283 GETSHORT(cishort, p); \
1284 /* Check rejected value. */ \
1285 if (cishort != val) \
1288 GETCHAR(cimaxslotindex, p); \
1289 if (cimaxslotindex != maxslot) \
1291 GETCHAR(ciflag, p); \
1292 if (ciflag != cflag) \
1298 #define REJCIADDR(opt, neg, val) \
1300 (cilen = p[1]) == CILEN_ADDR && \
1307 cilong = htonl(l); \
1308 /* Check rejected value. */ \
1309 if (cilong != val) \
1314 #define REJCIDNS(opt, neg, dnsaddr) \
1316 ((cilen = p[1]) == CILEN_ADDR) && \
1323 cilong = htonl(l); \
1324 /* Check rejected value. */ \
1325 if (cilong != dnsaddr) \
1330 #define REJCIWINS(opt, addr) \
1332 ((cilen = p[1]) == CILEN_ADDR) && \
1339 cilong = htonl(l); \
1340 /* Check rejected value. */ \
1341 if (cilong != addr) \
1343 try.winsaddr[opt == CI_MS_WINS2] = 0; \
1346 REJCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs,
1347 go->ouraddr, go->hisaddr);
1349 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
1350 go->maxslotindex, go->cflag);
1352 REJCIADDR(CI_ADDR, neg_addr, go->ouraddr);
1354 REJCIDNS(CI_MS_DNS1, req_dns1, go->dnsaddr[0]);
1356 REJCIDNS(CI_MS_DNS2, req_dns2, go->dnsaddr[1]);
1358 REJCIWINS(CI_MS_WINS1, go->winsaddr[0]);
1360 REJCIWINS(CI_MS_WINS2, go->winsaddr[1]);
1363 * If there are any remaining CIs, then this packet is bad.
1368 * Now we can update state.
1370 if (f->state != OPENED)
1375 IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
1381 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
1382 * Callback from fsm_rconfreq, Receive Configure Request
1384 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1385 * appropriately. If reject_if_disagree is non-zero, doesn't return
1386 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1389 ipcp_reqci(fsm *f, u_char *inp, int *len, int reject_if_disagree)
1391 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1392 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1393 ipcp_options *ao = &ipcp_allowoptions[f->unit];
1394 u_char *cip, *next; /* Pointer to current and next CIs */
1395 u_short cilen, citype; /* Parsed len, type */
1396 u_short cishort; /* Parsed short value */
1397 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
1398 int rc = CONFACK; /* Final packet return code */
1399 int orc; /* Individual option return code */
1400 u_char *p; /* Pointer to next char to parse */
1401 u_char *ucp = inp; /* Pointer to current output char */
1402 int l = *len; /* Length left */
1403 u_char maxslotindex, cflag;
1407 * Reset all his options.
1409 BZERO(ho, sizeof(*ho));
1412 * Process all his options.
1416 orc = CONFACK; /* Assume success */
1417 cip = p = next; /* Remember begining of CI */
1418 if (l < 2 || /* Not enough data for CI header or */
1419 p[1] < 2 || /* CI length too small or */
1420 p[1] > l) { /* CI length too big? */
1421 IPCPDEBUG(("ipcp_reqci: bad CI length!"));
1422 orc = CONFREJ; /* Reject bad CI */
1423 cilen = l; /* Reject till end of packet */
1424 l = 0; /* Don't loop again */
1427 GETCHAR(citype, p); /* Parse CI type */
1428 GETCHAR(cilen, p); /* Parse CI length */
1429 l -= cilen; /* Adjust remaining length */
1430 next += cilen; /* Step to next CI */
1432 switch (citype) { /* Check CI type */
1434 if (!ao->old_addrs || ho->neg_addr ||
1435 cilen != CILEN_ADDRS) { /* Check CI length */
1436 orc = CONFREJ; /* Reject CI */
1441 * If he has no address, or if we both have his address but
1442 * disagree about it, then NAK it with our idea.
1443 * In particular, if we don't know his address, but he does,
1446 GETLONG(tl, p); /* Parse source address (his) */
1447 ciaddr1 = htonl(tl);
1448 if (ciaddr1 != wo->hisaddr
1449 && (ciaddr1 == 0 || !wo->accept_remote)) {
1451 if (!reject_if_disagree) {
1452 DECPTR(sizeof(u_int32_t), p);
1453 tl = ntohl(wo->hisaddr);
1456 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1458 * If neither we nor he knows his address, reject the option.
1461 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1466 * If he doesn't know our address, or if we both have our address
1467 * but disagree about it, then NAK it with our idea.
1469 GETLONG(tl, p); /* Parse desination address (ours) */
1470 ciaddr2 = htonl(tl);
1471 if (ciaddr2 != wo->ouraddr) {
1472 if (ciaddr2 == 0 || !wo->accept_local) {
1474 if (!reject_if_disagree && wo->old_addrs) {
1475 DECPTR(sizeof(u_int32_t), p);
1476 tl = ntohl(wo->ouraddr);
1480 wo->ouraddr = ciaddr2; /* accept peer's idea */
1485 ho->hisaddr = ciaddr1;
1486 ho->ouraddr = ciaddr2;
1490 if (!ao->neg_addr || ho->old_addrs ||
1491 cilen != CILEN_ADDR) { /* Check CI length */
1492 orc = CONFREJ; /* Reject CI */
1497 * If he has no address, or if we both have his address but
1498 * disagree about it, then NAK it with our idea.
1499 * In particular, if we don't know his address, but he does,
1502 GETLONG(tl, p); /* Parse source address (his) */
1503 ciaddr1 = htonl(tl);
1504 if (ciaddr1 != wo->hisaddr
1505 && (ciaddr1 == 0 || !wo->accept_remote)) {
1507 if (!reject_if_disagree) {
1508 DECPTR(sizeof(u_int32_t), p);
1509 tl = ntohl(wo->hisaddr);
1512 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1514 * Don't ACK an address of 0.0.0.0 - reject it instead.
1517 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1522 ho->hisaddr = ciaddr1;
1527 /* Microsoft primary or secondary DNS request */
1528 d = citype == CI_MS_DNS2;
1530 /* If we do not have a DNS address then we cannot send it */
1531 if (ao->dnsaddr[d] == 0 ||
1532 cilen != CILEN_ADDR) { /* Check CI length */
1533 orc = CONFREJ; /* Reject CI */
1537 if (htonl(tl) != ao->dnsaddr[d]) {
1538 DECPTR(sizeof(u_int32_t), p);
1539 tl = ntohl(ao->dnsaddr[d]);
1547 /* Microsoft primary or secondary WINS request */
1548 d = citype == CI_MS_WINS2;
1550 /* If we do not have a DNS address then we cannot send it */
1551 if (ao->winsaddr[d] == 0 ||
1552 cilen != CILEN_ADDR) { /* Check CI length */
1553 orc = CONFREJ; /* Reject CI */
1557 if (htonl(tl) != ao->winsaddr[d]) {
1558 DECPTR(sizeof(u_int32_t), p);
1559 tl = ntohl(ao->winsaddr[d]);
1565 case CI_COMPRESSTYPE:
1567 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1571 GETSHORT(cishort, p);
1573 if (!(cishort == IPCP_VJ_COMP ||
1574 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1580 ho->vj_protocol = cishort;
1581 if (cilen == CILEN_VJ) {
1582 GETCHAR(maxslotindex, p);
1583 if (maxslotindex > ao->maxslotindex) {
1585 if (!reject_if_disagree){
1587 PUTCHAR(ao->maxslotindex, p);
1591 if (cflag && !ao->cflag) {
1593 if (!reject_if_disagree){
1595 PUTCHAR(wo->cflag, p);
1598 ho->maxslotindex = maxslotindex;
1602 ho->maxslotindex = MAX_STATES - 1;
1612 if (orc == CONFACK && /* Good CI */
1613 rc != CONFACK) /* but prior CI wasnt? */
1614 continue; /* Don't send this one */
1616 if (orc == CONFNAK) { /* Nak this CI? */
1617 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1618 orc = CONFREJ; /* Get tough if so */
1620 if (rc == CONFREJ) /* Rejecting prior CI? */
1621 continue; /* Don't send this one */
1622 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1623 rc = CONFNAK; /* Not anymore... */
1624 ucp = inp; /* Backup */
1629 if (orc == CONFREJ && /* Reject this CI */
1630 rc != CONFREJ) { /* but no prior ones? */
1632 ucp = inp; /* Backup */
1635 /* Need to move CI? */
1637 BCOPY(cip, ucp, cilen); /* Move it */
1639 /* Update output pointer */
1644 * If we aren't rejecting this packet, and we want to negotiate
1645 * their address, and they didn't send their address, then we
1646 * send a NAK with a CI_ADDR option appended. We assume the
1647 * input buffer is long enough that we can append the extra
1650 if (rc != CONFREJ && !ho->neg_addr && !ho->old_addrs &&
1651 wo->req_addr && !reject_if_disagree && !noremoteip) {
1652 if (rc == CONFACK) {
1654 ucp = inp; /* reset pointer */
1655 wo->req_addr = 0; /* don't ask again */
1657 PUTCHAR(CI_ADDR, ucp);
1658 PUTCHAR(CILEN_ADDR, ucp);
1659 tl = ntohl(wo->hisaddr);
1663 *len = ucp - inp; /* Compute output length */
1664 IPCPDEBUG(("ipcp: returning Configure-%s", CODENAME(rc)));
1665 return (rc); /* Return final code */
1670 * ip_check_options - check that any IP-related options are OK,
1671 * and assign appropriate defaults.
1674 ip_check_options(void)
1678 ipcp_options *wo = &ipcp_wantoptions[0];
1681 * Default our local IP address based on our hostname.
1682 * If local IP address already given, don't bother.
1684 if (wo->ouraddr == 0 && !disable_defaultip) {
1686 * Look up our hostname (possibly with domain name appended)
1687 * and take the first IP address as our local IP address.
1688 * If there isn't an IP address for our hostname, too bad.
1690 wo->accept_local = 1; /* don't insist on this default value */
1691 if ((hp = gethostbyname(hostname)) != NULL) {
1692 local = *(u_int32_t *)hp->h_addr;
1693 if (local != 0 && !bad_ip_adrs(local))
1694 wo->ouraddr = local;
1697 ask_for_local = wo->ouraddr != 0 || !disable_defaultip;
1702 * ip_demand_conf - configure the interface as though
1703 * IPCP were up, for use with dial-on-demand.
1706 ip_demand_conf(int u)
1708 ipcp_options *wo = &ipcp_wantoptions[u];
1710 if (wo->hisaddr == 0 && !noremoteip) {
1711 /* make up an arbitrary address for the peer */
1712 wo->hisaddr = htonl(0x0a707070 + ifunit);
1713 wo->accept_remote = 1;
1715 if (wo->ouraddr == 0) {
1716 /* make up an arbitrary address for us */
1717 wo->ouraddr = htonl(0x0a404040 + ifunit);
1718 wo->accept_local = 1;
1719 ask_for_local = 0; /* don't tell the peer this address */
1721 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1723 ipcp_script(_PATH_IPPREUP, 1);
1726 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1728 if (wo->default_route)
1729 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr,
1730 wo->replace_default_route))
1731 default_route_set[u] = 1;
1733 if (sifproxyarp(u, wo->hisaddr))
1734 proxy_arp_set[u] = 1;
1736 notice("local IP address %I", wo->ouraddr);
1738 notice("remote IP address %I", wo->hisaddr);
1745 * ipcp_up - IPCP has come UP.
1747 * Configure the IP network interface appropriately and bring it up.
1753 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1754 ipcp_options *go = &ipcp_gotoptions[f->unit];
1755 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1758 IPCPDEBUG(("ipcp: up"));
1761 * We must have a non-zero IP address for both ends of the link.
1764 if (wo->hisaddr && !wo->accept_remote && (!(ho->neg_addr || ho->old_addrs) || ho->hisaddr != wo->hisaddr)) {
1765 error("Peer refused to agree to his IP address");
1766 ipcp_close(f->unit, "Refused his IP address");
1769 if (!ho->neg_addr && !ho->old_addrs)
1770 ho->hisaddr = wo->hisaddr;
1772 if (!(go->neg_addr || go->old_addrs) && (wo->neg_addr || wo->old_addrs)
1773 && wo->ouraddr != 0) {
1774 error("Peer refused to agree to our IP address");
1775 ipcp_close(f->unit, "Refused our IP address");
1778 if (go->ouraddr == 0) {
1779 error("Could not determine local IP address");
1780 ipcp_close(f->unit, "Could not determine local IP address");
1783 if (ho->hisaddr == 0 && !noremoteip) {
1784 ho->hisaddr = htonl(0x0a404040 + ifunit);
1785 warn("Could not determine remote IP address: defaulting to %I",
1788 script_setenv("IPLOCAL", ip_ntoa(go->ouraddr), 0);
1789 if (ho->hisaddr != 0)
1790 script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr), 1);
1797 script_setenv("DNS1", ip_ntoa(go->dnsaddr[0]), 0);
1799 script_setenv("DNS2", ip_ntoa(go->dnsaddr[1]), 0);
1800 if (usepeerdns && (go->dnsaddr[0] || go->dnsaddr[1])) {
1801 script_setenv("USEPEERDNS", "1", 0);
1802 create_resolv(go->dnsaddr[0], go->dnsaddr[1]);
1806 * Check that the peer is allowed to use the IP address it wants.
1808 if (ho->hisaddr != 0 && !auth_ip_addr(f->unit, ho->hisaddr)) {
1809 error("Peer is not authorized to use remote address %I", ho->hisaddr);
1810 ipcp_close(f->unit, "Unauthorized remote IP address");
1814 /* set tcp compression */
1815 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1818 * If we are doing dial-on-demand, the interface is already
1819 * configured, so we put out any saved-up packets, then set the
1820 * interface to pass IP packets.
1823 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1824 ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr,
1825 wo->replace_default_route);
1826 if (go->ouraddr != wo->ouraddr) {
1827 warn("Local IP address changed to %I", go->ouraddr);
1828 script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr), 0);
1829 wo->ouraddr = go->ouraddr;
1831 script_unsetenv("OLDIPLOCAL");
1832 if (ho->hisaddr != wo->hisaddr) {
1833 warn("Remote IP address changed to %I", ho->hisaddr);
1834 if (wo->hisaddr != 0)
1835 script_setenv("OLDIPREMOTE", ip_ntoa(wo->hisaddr), 0);
1836 wo->hisaddr = ho->hisaddr;
1838 script_unsetenv("OLDIPREMOTE");
1840 /* Set the interface to the new addresses */
1841 mask = GetMask(go->ouraddr);
1842 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1844 warn("Interface configuration failed");
1845 ipcp_close(f->unit, "Interface configuration failed");
1849 /* assign a default route through the interface if required */
1850 if (ipcp_wantoptions[f->unit].default_route)
1851 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr,
1852 wo->replace_default_route))
1853 default_route_set[f->unit] = 1;
1855 /* Make a proxy ARP entry if requested. */
1856 if (ho->hisaddr != 0 && ipcp_wantoptions[f->unit].proxy_arp)
1857 if (sifproxyarp(f->unit, ho->hisaddr))
1858 proxy_arp_set[f->unit] = 1;
1861 demand_rexmit(PPP_IP);
1862 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1866 * Set IP addresses and (if specified) netmask.
1868 mask = GetMask(go->ouraddr);
1870 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1871 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1873 warn("Interface configuration failed");
1874 ipcp_close(f->unit, "Interface configuration failed");
1879 ifindex = if_nametoindex(ifname);
1881 /* run the pre-up script, if any, and wait for it to finish */
1882 ipcp_script(_PATH_IPPREUP, 1);
1884 /* check if preup script renamed the interface */
1885 if (!if_indextoname(ifindex, ifname)) {
1886 error("Interface index %d failed to get renamed by a pre-up script", ifindex);
1887 ipcp_close(f->unit, "Interface configuration failed");
1891 /* bring the interface up for IP */
1892 if (!sifup(f->unit)) {
1894 warn("Interface failed to come up");
1895 ipcp_close(f->unit, "Interface configuration failed");
1899 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1900 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1902 warn("Interface configuration failed");
1903 ipcp_close(f->unit, "Interface configuration failed");
1907 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1909 /* assign a default route through the interface if required */
1910 if (ipcp_wantoptions[f->unit].default_route)
1911 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr,
1912 wo->replace_default_route))
1913 default_route_set[f->unit] = 1;
1915 /* Make a proxy ARP entry if requested. */
1916 if (ho->hisaddr != 0 && ipcp_wantoptions[f->unit].proxy_arp)
1917 if (sifproxyarp(f->unit, ho->hisaddr))
1918 proxy_arp_set[f->unit] = 1;
1920 ipcp_wantoptions[0].ouraddr = go->ouraddr;
1922 notice("local IP address %I", go->ouraddr);
1923 if (ho->hisaddr != 0)
1924 notice("remote IP address %I", ho->hisaddr);
1926 notice("primary DNS address %I", go->dnsaddr[0]);
1928 notice("secondary DNS address %I", go->dnsaddr[1]);
1931 reset_link_stats(f->unit);
1933 np_up(f->unit, PPP_IP);
1936 notify(ip_up_notifier, 0);
1941 * Execute the ip-up script, like this:
1942 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1944 if (ipcp_script_state == s_down && ipcp_script_pid == 0) {
1945 ipcp_script_state = s_up;
1946 ipcp_script(path_ipup, 0);
1952 * ipcp_down - IPCP has gone DOWN.
1954 * Take the IP network interface down, clear its addresses
1955 * and delete routes through it.
1960 IPCPDEBUG(("ipcp: down"));
1961 /* XXX a bit IPv4-centric here, we only need to get the stats
1962 * before the interface is marked down. */
1963 /* XXX more correct: we must get the stats before running the notifiers,
1964 * at least for the radius plugin */
1965 update_link_stats(f->unit);
1966 notify(ip_down_notifier, 0);
1971 np_down(f->unit, PPP_IP);
1973 sifvjcomp(f->unit, 0, 0, 0);
1975 print_link_stats(); /* _after_ running the notifiers and ip_down_hook(),
1976 * because print_link_stats() sets link_stats_valid
1980 * If we are doing dial-on-demand, set the interface
1981 * to queue up outgoing packets (for now).
1984 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1986 sifnpmode(f->unit, PPP_IP, NPMODE_DROP);
1988 ipcp_clear_addrs(f->unit, ipcp_gotoptions[f->unit].ouraddr,
1989 ipcp_hisoptions[f->unit].hisaddr, 0);
1992 /* Execute the ip-down script */
1993 if (ipcp_script_state == s_up && ipcp_script_pid == 0) {
1994 ipcp_script_state = s_down;
1995 ipcp_script(path_ipdown, 0);
2001 * ipcp_clear_addrs() - clear the interface addresses, routes,
2002 * proxy arp entries, etc.
2005 ipcp_clear_addrs(int unit, u_int32_t ouraddr, u_int32_t hisaddr, bool replacedefaultroute)
2007 if (proxy_arp_set[unit]) {
2008 cifproxyarp(unit, hisaddr);
2009 proxy_arp_set[unit] = 0;
2011 /* If replacedefaultroute, sifdefaultroute will be called soon
2012 * with replacedefaultroute set and that will overwrite the current
2013 * default route. This is the case only when doing demand, otherwise
2014 * during demand, this cifdefaultroute would restore the old default
2015 * route which is not what we want in this case. In the non-demand
2016 * case, we'll delete the default route and restore the old if there
2017 * is one saved by an sifdefaultroute with replacedefaultroute.
2019 if (!replacedefaultroute && default_route_set[unit]) {
2020 cifdefaultroute(unit, ouraddr, hisaddr);
2021 default_route_set[unit] = 0;
2023 cifaddr(unit, ouraddr, hisaddr);
2028 * ipcp_finished - possibly shut down the lower layers.
2031 ipcp_finished(fsm *f)
2035 np_finished(f->unit, PPP_IP);
2041 * ipcp_script_done - called when the ip-up or ip-down script
2045 ipcp_script_done(void *arg)
2047 ipcp_script_pid = 0;
2048 switch (ipcp_script_state) {
2050 if (ipcp_fsm[0].state != OPENED) {
2051 ipcp_script_state = s_down;
2052 ipcp_script(path_ipdown, 0);
2056 if (ipcp_fsm[0].state == OPENED) {
2057 ipcp_script_state = s_up;
2058 ipcp_script(path_ipup, 0);
2066 * ipcp_script - Execute a script with arguments
2067 * interface-name tty-name speed local-IP remote-IP.
2070 ipcp_script(char *script, int wait)
2072 char strspeed[32], strlocal[32], strremote[32];
2075 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2076 slprintf(strlocal, sizeof(strlocal), "%I", ipcp_gotoptions[0].ouraddr);
2077 slprintf(strremote, sizeof(strremote), "%I", ipcp_hisoptions[0].hisaddr);
2084 argv[5] = strremote;
2088 run_program(script, argv, 0, NULL, NULL, 1);
2090 ipcp_script_pid = run_program(script, argv, 0, ipcp_script_done,
2095 * create_resolv - create the replacement resolv.conf file
2098 create_resolv(u_int32_t peerdns1, u_int32_t peerdns2)
2102 f = fopen(_PATH_RESOLV, "w");
2104 error("Failed to create %s: %m", _PATH_RESOLV);
2109 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns1));
2112 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns2));
2115 error("Write failed to %s: %m", _PATH_RESOLV);
2121 * ipcp_printpkt - print the contents of an IPCP packet.
2123 static char *ipcp_codenames[] = {
2124 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
2125 "TermReq", "TermAck", "CodeRej"
2129 ipcp_printpkt(u_char *p, int plen,
2130 void (*printer) (void *, char *, ...), void *arg)
2132 int code, id, len, olen;
2133 u_char *pstart, *optend;
2137 if (plen < HEADERLEN)
2143 if (len < HEADERLEN || len > plen)
2146 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
2147 printer(arg, " %s", ipcp_codenames[code-1]);
2149 printer(arg, " code=0x%x", code);
2150 printer(arg, " id=0x%x", id);
2157 /* print option list */
2162 if (olen < 2 || olen > len) {
2170 if (olen == CILEN_ADDRS) {
2173 printer(arg, "addrs %I", htonl(cilong));
2175 printer(arg, " %I", htonl(cilong));
2178 case CI_COMPRESSTYPE:
2179 if (olen >= CILEN_COMPRESS) {
2181 GETSHORT(cishort, p);
2182 printer(arg, "compress ");
2187 case IPCP_VJ_COMP_OLD:
2188 printer(arg, "old-VJ");
2191 printer(arg, "0x%x", cishort);
2196 if (olen == CILEN_ADDR) {
2199 printer(arg, "addr %I", htonl(cilong));
2206 printer(arg, "ms-dns%d %I", (code == CI_MS_DNS1? 1: 2),
2213 printer(arg, "ms-wins %I", htonl(cilong));
2216 while (p < optend) {
2218 printer(arg, " %.2x", code);
2226 if (len > 0 && *p >= ' ' && *p < 0x7f) {
2228 print_string((char *)p, len, printer, arg);
2235 /* print the rest of the bytes in the packet */
2236 for (; len > 0; --len) {
2238 printer(arg, " %.2x", code);
2245 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
2246 * We don't bring the link up for IP fragments or for TCP FIN packets
2249 #define IP_HDRLEN 20 /* bytes */
2250 #define IP_OFFMASK 0x1fff
2252 #define IPPROTO_TCP 6
2254 #define TCP_HDRLEN 20
2258 * We use these macros because the IP header may be at an odd address,
2259 * and some compilers might use word loads to get th_off or ip_hl.
2262 #define net_short(x) (((x)[0] << 8) + (x)[1])
2263 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
2264 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
2265 #define get_ipproto(x) (((unsigned char *)(x))[9])
2266 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
2267 #define get_tcpflags(x) (((unsigned char *)(x))[13])
2270 ip_active_pkt(u_char *pkt, int len)
2277 if (len < IP_HDRLEN)
2279 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
2281 if (get_ipproto(pkt) != IPPROTO_TCP)
2283 hlen = get_iphl(pkt) * 4;
2284 if (len < hlen + TCP_HDRLEN)
2287 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)