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.
55 #include <sys/param.h>
56 #include <sys/types.h>
57 #include <sys/socket.h>
58 #include <netinet/in.h>
59 #include <arpa/inet.h>
65 #include "pathnames.h"
69 ipcp_options ipcp_wantoptions[NUM_PPP]; /* Options that we want to request */
70 ipcp_options ipcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
71 ipcp_options ipcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
72 ipcp_options ipcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
74 u_int32_t netmask = 0; /* IP netmask to set on interface */
76 bool disable_defaultip = 0; /* Don't use hostname for default IP adrs */
77 bool noremoteip = 0; /* Let him have no IP address */
79 /* Hook for a plugin to know when IP protocol has come up */
80 void (*ip_up_hook)(void) = NULL;
82 /* Hook for a plugin to know when IP protocol has come down */
83 void (*ip_down_hook)(void) = NULL;
85 /* Hook for a plugin to choose the remote IP address */
86 void (*ip_choose_hook)(u_int32_t *) = NULL;
88 /* Notifiers for when IPCP goes up and down */
89 struct notifier *ip_up_notifier = NULL;
90 struct notifier *ip_down_notifier = NULL;
93 static int default_route_set[NUM_PPP]; /* Have set up a default route */
94 static int proxy_arp_set[NUM_PPP]; /* Have created proxy arp entry */
95 static bool usepeerdns; /* Ask peer for DNS addrs */
96 static int ipcp_is_up; /* have called np_up() */
97 static int ipcp_is_open; /* haven't called np_finished() */
98 static bool ask_for_local; /* request our address from peer */
99 static char vj_value[8]; /* string form of vj option value */
100 static char netmask_str[20]; /* string form of netmask value */
103 * Callbacks for fsm code. (CI = Configuration Information)
105 static void ipcp_resetci (fsm *); /* Reset our CI */
106 static int ipcp_cilen (fsm *); /* Return length of our CI */
107 static void ipcp_addci (fsm *, u_char *, int *); /* Add our CI */
108 static int ipcp_ackci (fsm *, u_char *, int); /* Peer ack'd our CI */
109 static int ipcp_nakci (fsm *, u_char *, int, int);/* Peer nak'd our CI */
110 static int ipcp_rejci (fsm *, u_char *, int); /* Peer rej'd our CI */
111 static int ipcp_reqci (fsm *, u_char *, int *, int); /* Rcv CI */
112 static void ipcp_up (fsm *); /* We're UP */
113 static void ipcp_down (fsm *); /* We're DOWN */
114 static void ipcp_finished (fsm *); /* Don't need lower layer */
116 fsm ipcp_fsm[NUM_PPP]; /* IPCP fsm structure */
118 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
119 ipcp_resetci, /* Reset our Configuration Information */
120 ipcp_cilen, /* Length of our Configuration Information */
121 ipcp_addci, /* Add our Configuration Information */
122 ipcp_ackci, /* ACK our Configuration Information */
123 ipcp_nakci, /* NAK our Configuration Information */
124 ipcp_rejci, /* Reject our Configuration Information */
125 ipcp_reqci, /* Request peer's Configuration Information */
126 ipcp_up, /* Called when fsm reaches OPENED state */
127 ipcp_down, /* Called when fsm leaves OPENED state */
128 NULL, /* Called when we want the lower layer up */
129 ipcp_finished, /* Called when we want the lower layer down */
130 NULL, /* Called when Protocol-Reject received */
131 NULL, /* Retransmission is necessary */
132 NULL, /* Called to handle protocol-specific codes */
133 "IPCP" /* String name of protocol */
137 * Command-line options.
139 static int setvjslots (char **);
140 static int setdnsaddr (char **);
141 static int setwinsaddr (char **);
142 static int setnetmask (char **);
143 int setipaddr (char *, char **, int);
144 static void printipaddr (option_t *, void (*)(void *, char *,...),void *);
146 static option_t ipcp_option_list[] = {
147 { "noip", o_bool, &ipcp_protent.enabled_flag,
148 "Disable IP and IPCP" },
149 { "-ip", o_bool, &ipcp_protent.enabled_flag,
150 "Disable IP and IPCP", OPT_ALIAS },
152 { "novj", o_bool, &ipcp_wantoptions[0].neg_vj,
153 "Disable VJ compression", OPT_A2CLR, &ipcp_allowoptions[0].neg_vj },
154 { "-vj", o_bool, &ipcp_wantoptions[0].neg_vj,
155 "Disable VJ compression", OPT_ALIAS | OPT_A2CLR,
156 &ipcp_allowoptions[0].neg_vj },
158 { "novjccomp", o_bool, &ipcp_wantoptions[0].cflag,
159 "Disable VJ connection-ID compression", OPT_A2CLR,
160 &ipcp_allowoptions[0].cflag },
161 { "-vjccomp", o_bool, &ipcp_wantoptions[0].cflag,
162 "Disable VJ connection-ID compression", OPT_ALIAS | OPT_A2CLR,
163 &ipcp_allowoptions[0].cflag },
165 { "vj-max-slots", o_special, (void *)setvjslots,
166 "Set maximum VJ header slots",
167 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, vj_value },
169 { "ipcp-accept-local", o_bool, &ipcp_wantoptions[0].accept_local,
170 "Accept peer's address for us", 1 },
171 { "ipcp-accept-remote", o_bool, &ipcp_wantoptions[0].accept_remote,
172 "Accept peer's address for it", 1 },
174 { "ipparam", o_string, &ipparam,
175 "Set ip script parameter", OPT_PRIO },
177 { "noipdefault", o_bool, &disable_defaultip,
178 "Don't use name for default IP adrs", 1 },
180 { "ms-dns", o_special, (void *)setdnsaddr,
181 "DNS address for the peer's use", OPT_A2LIST },
182 { "ms-wins", o_special, (void *)setwinsaddr,
183 "Nameserver for SMB over TCP/IP for peer", OPT_A2LIST },
185 { "ipcp-restart", o_int, &ipcp_fsm[0].timeouttime,
186 "Set timeout for IPCP", OPT_PRIO },
187 { "ipcp-max-terminate", o_int, &ipcp_fsm[0].maxtermtransmits,
188 "Set max #xmits for term-reqs", OPT_PRIO },
189 { "ipcp-max-configure", o_int, &ipcp_fsm[0].maxconfreqtransmits,
190 "Set max #xmits for conf-reqs", OPT_PRIO },
191 { "ipcp-max-failure", o_int, &ipcp_fsm[0].maxnakloops,
192 "Set max #conf-naks for IPCP", OPT_PRIO },
194 { "defaultroute", o_bool, &ipcp_wantoptions[0].default_route,
195 "Add default route", OPT_ENABLE|1, &ipcp_allowoptions[0].default_route },
196 { "nodefaultroute", o_bool, &ipcp_allowoptions[0].default_route,
197 "disable defaultroute option", OPT_A2CLR,
198 &ipcp_wantoptions[0].default_route },
199 { "-defaultroute", o_bool, &ipcp_allowoptions[0].default_route,
200 "disable defaultroute option", OPT_ALIAS | OPT_A2CLR,
201 &ipcp_wantoptions[0].default_route },
204 { "replacedefaultroute", o_bool,
205 &ipcp_wantoptions[0].replace_default_route,
206 "Replace default route", OPT_PRIV | 1
208 { "noreplacedefaultroute", o_bool,
209 &ipcp_wantoptions[0].replace_default_route,
210 "Do not replace default route", 0 },
212 { "proxyarp", o_bool, &ipcp_wantoptions[0].proxy_arp,
213 "Add proxy ARP entry", OPT_ENABLE|1, &ipcp_allowoptions[0].proxy_arp },
214 { "noproxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
215 "disable proxyarp option", OPT_A2CLR,
216 &ipcp_wantoptions[0].proxy_arp },
217 { "-proxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
218 "disable proxyarp option", OPT_ALIAS | OPT_A2CLR,
219 &ipcp_wantoptions[0].proxy_arp },
221 { "usepeerdns", o_bool, &usepeerdns,
222 "Ask peer for DNS 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 (wo->hisaddr && !wo->accept_remote);
688 if (wo->ouraddr == 0)
689 wo->accept_local = 1;
690 if (wo->hisaddr == 0)
691 wo->accept_remote = 1;
692 wo->req_dns1 = usepeerdns; /* Request DNS addresses from the peer */
693 wo->req_dns2 = usepeerdns;
697 if (ip_choose_hook) {
698 ip_choose_hook(&wo->hisaddr);
700 wo->accept_remote = 0;
703 BZERO(&ipcp_hisoptions[f->unit], sizeof(ipcp_options));
708 * ipcp_cilen - Return length of our CI.
709 * Called by fsm_sconfreq, Send Configure Request.
714 ipcp_options *go = &ipcp_gotoptions[f->unit];
715 ipcp_options *wo = &ipcp_wantoptions[f->unit];
716 ipcp_options *ho = &ipcp_hisoptions[f->unit];
718 #define LENCIADDRS(neg) (neg ? CILEN_ADDRS : 0)
719 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
720 #define LENCIADDR(neg) (neg ? CILEN_ADDR : 0)
721 #define LENCIDNS(neg) LENCIADDR(neg)
722 #define LENCIWINS(neg) LENCIADDR(neg)
725 * First see if we want to change our options to the old
726 * forms because we have received old forms from the peer.
728 if (go->neg_addr && go->old_addrs && !ho->neg_addr && ho->old_addrs)
730 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
731 /* try an older style of VJ negotiation */
732 /* use the old style only if the peer did */
733 if (ho->neg_vj && ho->old_vj) {
736 go->vj_protocol = ho->vj_protocol;
740 return (LENCIADDRS(!go->neg_addr && go->old_addrs) +
741 LENCIVJ(go->neg_vj, go->old_vj) +
742 LENCIADDR(go->neg_addr) +
743 LENCIDNS(go->req_dns1) +
744 LENCIDNS(go->req_dns2) +
745 LENCIWINS(go->winsaddr[0]) +
746 LENCIWINS(go->winsaddr[1])) ;
751 * ipcp_addci - Add our desired CIs to a packet.
752 * Called by fsm_sconfreq, Send Configure Request.
755 ipcp_addci(fsm *f, u_char *ucp, int *lenp)
757 ipcp_options *go = &ipcp_gotoptions[f->unit];
760 #define ADDCIADDRS(opt, neg, val1, val2) \
762 if (len >= CILEN_ADDRS) { \
765 PUTCHAR(CILEN_ADDRS, ucp); \
770 len -= CILEN_ADDRS; \
775 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
777 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
778 if (len >= vjlen) { \
780 PUTCHAR(vjlen, ucp); \
781 PUTSHORT(val, ucp); \
783 PUTCHAR(maxslotindex, ucp); \
784 PUTCHAR(cflag, ucp); \
791 #define ADDCIADDR(opt, neg, val) \
793 if (len >= CILEN_ADDR) { \
796 PUTCHAR(CILEN_ADDR, ucp); \
804 #define ADDCIDNS(opt, neg, addr) \
806 if (len >= CILEN_ADDR) { \
809 PUTCHAR(CILEN_ADDR, ucp); \
817 #define ADDCIWINS(opt, addr) \
819 if (len >= CILEN_ADDR) { \
822 PUTCHAR(CILEN_ADDR, ucp); \
830 ADDCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs, go->ouraddr,
833 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
834 go->maxslotindex, go->cflag);
836 ADDCIADDR(CI_ADDR, go->neg_addr, go->ouraddr);
838 ADDCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
840 ADDCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
842 ADDCIWINS(CI_MS_WINS1, go->winsaddr[0]);
844 ADDCIWINS(CI_MS_WINS2, go->winsaddr[1]);
851 * ipcp_ackci - Ack our CIs.
852 * Called by fsm_rconfack, Receive Configure ACK.
859 ipcp_ackci(fsm *f, u_char *p, int len)
861 ipcp_options *go = &ipcp_gotoptions[f->unit];
862 u_short cilen, citype, cishort;
864 u_char cimaxslotindex, cicflag;
867 * CIs must be in exactly the same order that we sent...
868 * Check packet length and CI length at each step.
869 * If we find any deviations, then this packet is bad.
872 #define ACKCIADDRS(opt, neg, val1, val2) \
875 if ((len -= CILEN_ADDRS) < 0) \
877 GETCHAR(citype, p); \
879 if (cilen != CILEN_ADDRS || \
884 if (val1 != cilong) \
888 if (val2 != cilong) \
892 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
894 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
895 if ((len -= vjlen) < 0) \
897 GETCHAR(citype, p); \
899 if (cilen != vjlen || \
902 GETSHORT(cishort, p); \
903 if (cishort != val) \
906 GETCHAR(cimaxslotindex, p); \
907 if (cimaxslotindex != maxslotindex) \
909 GETCHAR(cicflag, p); \
910 if (cicflag != cflag) \
915 #define ACKCIADDR(opt, neg, val) \
918 if ((len -= CILEN_ADDR) < 0) \
920 GETCHAR(citype, p); \
922 if (cilen != CILEN_ADDR || \
931 #define ACKCIDNS(opt, neg, addr) \
934 if ((len -= CILEN_ADDR) < 0) \
936 GETCHAR(citype, p); \
938 if (cilen != CILEN_ADDR || citype != opt) \
942 if (addr != cilong) \
946 #define ACKCIWINS(opt, addr) \
949 if ((len -= CILEN_ADDR) < 0) \
951 GETCHAR(citype, p); \
953 if (cilen != CILEN_ADDR || citype != opt) \
957 if (addr != cilong) \
961 ACKCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs, go->ouraddr,
964 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
965 go->maxslotindex, go->cflag);
967 ACKCIADDR(CI_ADDR, go->neg_addr, go->ouraddr);
969 ACKCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
971 ACKCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
973 ACKCIWINS(CI_MS_WINS1, go->winsaddr[0]);
975 ACKCIWINS(CI_MS_WINS2, go->winsaddr[1]);
978 * If there are any remaining CIs, then this packet is bad.
985 IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
990 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
991 * This should not modify any state if the Nak is bad
992 * or if IPCP is in the OPENED state.
993 * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
1000 ipcp_nakci(fsm *f, u_char *p, int len, int treat_as_reject)
1002 ipcp_options *wo = &ipcp_wantoptions[f->unit];
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;
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); \
1073 * Accept the peer's idea of {our,his} address, if different
1074 * from our idea, only if the accept_{local,remote} flag is set.
1076 NAKCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs,
1077 if (treat_as_reject) {
1080 if (go->accept_local && ciaddr1) {
1081 /* take his idea of our address */
1082 try.ouraddr = ciaddr1;
1084 if (go->accept_remote && ciaddr2) {
1085 /* take his idea of his address */
1086 try.hisaddr = ciaddr2;
1092 * Accept the peer's value of maxslotindex provided that it
1093 * is less than what we asked for. Turn off slot-ID compression
1094 * if the peer wants. Send old-style compress-type option if
1097 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
1098 if (treat_as_reject) {
1100 } else if (cilen == CILEN_VJ) {
1101 GETCHAR(cimaxslotindex, p);
1102 GETCHAR(cicflag, p);
1103 if (cishort == IPCP_VJ_COMP) {
1105 if (cimaxslotindex < go->maxslotindex)
1106 try.maxslotindex = cimaxslotindex;
1113 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
1115 try.vj_protocol = cishort;
1122 NAKCIADDR(CI_ADDR, neg_addr,
1123 if (treat_as_reject) {
1126 } else if (go->accept_local && ciaddr1) {
1127 /* take his idea of our address */
1128 try.ouraddr = ciaddr1;
1132 NAKCIDNS(CI_MS_DNS1, req_dns1,
1133 if (treat_as_reject) {
1136 try.dnsaddr[0] = cidnsaddr;
1140 NAKCIDNS(CI_MS_DNS2, req_dns2,
1141 if (treat_as_reject) {
1144 try.dnsaddr[1] = cidnsaddr;
1149 * There may be remaining CIs, if the peer is requesting negotiation
1150 * on an option that we didn't include in our request packet.
1151 * If they want to negotiate about IP addresses, we comply.
1152 * If they want us to ask for compression, we refuse.
1153 * If they want us to ask for ms-dns, we do that, since some
1154 * peers get huffy if we don't.
1156 while (len >= CILEN_VOID) {
1159 if ( cilen < CILEN_VOID || (len -= cilen) < 0 )
1161 next = p + cilen - 2;
1164 case CI_COMPRESSTYPE:
1165 if (go->neg_vj || no.neg_vj ||
1166 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
1171 if ((!go->neg_addr && go->old_addrs) || no.old_addrs
1172 || cilen != CILEN_ADDRS)
1177 if (ciaddr1 && go->accept_local)
1178 try.ouraddr = wo->old_addrs ? ciaddr1 : 0;
1181 if (ciaddr2 && go->accept_remote)
1182 try.hisaddr = ciaddr2;
1186 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
1191 if (ciaddr1 && go->accept_local)
1192 try.ouraddr = ciaddr1;
1193 if (try.ouraddr != 0 && wo->neg_addr)
1198 if (go->req_dns1 || no.req_dns1 || cilen != CILEN_ADDR)
1201 try.dnsaddr[0] = htonl(l);
1206 if (go->req_dns2 || no.req_dns2 || cilen != CILEN_ADDR)
1209 try.dnsaddr[1] = htonl(l);
1215 if (cilen != CILEN_ADDR)
1220 try.winsaddr[citype == CI_MS_WINS2] = ciaddr1;
1227 * OK, the Nak is good. Now we can update state.
1228 * If there are any remaining options, we ignore them.
1230 if (f->state != OPENED)
1236 IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
1242 * ipcp_rejci - Reject some of our CIs.
1243 * Callback from fsm_rconfnakrej.
1246 ipcp_rejci(fsm *f, u_char *p, int len)
1248 ipcp_options *go = &ipcp_gotoptions[f->unit];
1249 u_char cimaxslotindex, ciflag, cilen;
1252 ipcp_options try; /* options to request next time */
1256 * Any Rejected CIs must be in exactly the same order that we sent.
1257 * Check packet length and CI length at each step.
1258 * If we find any deviations, then this packet is bad.
1260 #define REJCIADDRS(opt, neg, val1, val2) \
1262 (cilen = p[1]) == CILEN_ADDRS && \
1269 cilong = htonl(l); \
1270 /* Check rejected value. */ \
1271 if (cilong != val1) \
1274 cilong = htonl(l); \
1275 /* Check rejected value. */ \
1276 if (cilong != val2) \
1278 try.old_addrs = 0; \
1281 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
1283 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
1288 GETSHORT(cishort, p); \
1289 /* Check rejected value. */ \
1290 if (cishort != val) \
1293 GETCHAR(cimaxslotindex, p); \
1294 if (cimaxslotindex != maxslot) \
1296 GETCHAR(ciflag, p); \
1297 if (ciflag != cflag) \
1303 #define REJCIADDR(opt, neg, val) \
1305 (cilen = p[1]) == CILEN_ADDR && \
1312 cilong = htonl(l); \
1313 /* Check rejected value. */ \
1314 if (cilong != val) \
1319 #define REJCIDNS(opt, neg, dnsaddr) \
1321 ((cilen = p[1]) == CILEN_ADDR) && \
1328 cilong = htonl(l); \
1329 /* Check rejected value. */ \
1330 if (cilong != dnsaddr) \
1335 #define REJCIWINS(opt, addr) \
1337 ((cilen = p[1]) == CILEN_ADDR) && \
1344 cilong = htonl(l); \
1345 /* Check rejected value. */ \
1346 if (cilong != addr) \
1348 try.winsaddr[opt == CI_MS_WINS2] = 0; \
1351 REJCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs,
1352 go->ouraddr, go->hisaddr);
1354 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
1355 go->maxslotindex, go->cflag);
1357 REJCIADDR(CI_ADDR, neg_addr, go->ouraddr);
1359 REJCIDNS(CI_MS_DNS1, req_dns1, go->dnsaddr[0]);
1361 REJCIDNS(CI_MS_DNS2, req_dns2, go->dnsaddr[1]);
1363 REJCIWINS(CI_MS_WINS1, go->winsaddr[0]);
1365 REJCIWINS(CI_MS_WINS2, go->winsaddr[1]);
1368 * If there are any remaining CIs, then this packet is bad.
1373 * Now we can update state.
1375 if (f->state != OPENED)
1380 IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
1386 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
1387 * Callback from fsm_rconfreq, Receive Configure Request
1389 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1390 * appropriately. If reject_if_disagree is non-zero, doesn't return
1391 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1394 ipcp_reqci(fsm *f, u_char *inp, int *len, int reject_if_disagree)
1396 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1397 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1398 ipcp_options *ao = &ipcp_allowoptions[f->unit];
1399 u_char *cip, *next; /* Pointer to current and next CIs */
1400 u_short cilen, citype; /* Parsed len, type */
1401 u_short cishort; /* Parsed short value */
1402 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
1403 int rc = CONFACK; /* Final packet return code */
1404 int orc; /* Individual option return code */
1405 u_char *p; /* Pointer to next char to parse */
1406 u_char *ucp = inp; /* Pointer to current output char */
1407 int l = *len; /* Length left */
1408 u_char maxslotindex, cflag;
1412 * Reset all his options.
1414 BZERO(ho, sizeof(*ho));
1417 * Process all his options.
1421 orc = CONFACK; /* Assume success */
1422 cip = p = next; /* Remember begining of CI */
1423 if (l < 2 || /* Not enough data for CI header or */
1424 p[1] < 2 || /* CI length too small or */
1425 p[1] > l) { /* CI length too big? */
1426 IPCPDEBUG(("ipcp_reqci: bad CI length!"));
1427 orc = CONFREJ; /* Reject bad CI */
1428 cilen = l; /* Reject till end of packet */
1429 l = 0; /* Don't loop again */
1432 GETCHAR(citype, p); /* Parse CI type */
1433 GETCHAR(cilen, p); /* Parse CI length */
1434 l -= cilen; /* Adjust remaining length */
1435 next += cilen; /* Step to next CI */
1437 switch (citype) { /* Check CI type */
1439 if (!ao->old_addrs || ho->neg_addr ||
1440 cilen != CILEN_ADDRS) { /* Check CI length */
1441 orc = CONFREJ; /* Reject CI */
1446 * If he has no address, or if we both have his address but
1447 * disagree about it, then NAK it with our idea.
1448 * In particular, if we don't know his address, but he does,
1451 GETLONG(tl, p); /* Parse source address (his) */
1452 ciaddr1 = htonl(tl);
1453 if (ciaddr1 != wo->hisaddr
1454 && (ciaddr1 == 0 || !wo->accept_remote)) {
1456 if (!reject_if_disagree) {
1457 DECPTR(sizeof(u_int32_t), p);
1458 tl = ntohl(wo->hisaddr);
1461 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1463 * If neither we nor he knows his address, reject the option.
1466 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1471 * If he doesn't know our address, or if we both have our address
1472 * but disagree about it, then NAK it with our idea.
1474 GETLONG(tl, p); /* Parse desination address (ours) */
1475 ciaddr2 = htonl(tl);
1476 if (ciaddr2 != wo->ouraddr) {
1477 if (ciaddr2 == 0 || !wo->accept_local) {
1479 if (!reject_if_disagree && wo->old_addrs) {
1480 DECPTR(sizeof(u_int32_t), p);
1481 tl = ntohl(wo->ouraddr);
1485 wo->ouraddr = ciaddr2; /* accept peer's idea */
1490 ho->hisaddr = ciaddr1;
1491 ho->ouraddr = ciaddr2;
1495 if (!ao->neg_addr || ho->old_addrs ||
1496 cilen != CILEN_ADDR) { /* Check CI length */
1497 orc = CONFREJ; /* Reject CI */
1502 * If he has no address, or if we both have his address but
1503 * disagree about it, then NAK it with our idea.
1504 * In particular, if we don't know his address, but he does,
1507 GETLONG(tl, p); /* Parse source address (his) */
1508 ciaddr1 = htonl(tl);
1509 if (ciaddr1 != wo->hisaddr
1510 && (ciaddr1 == 0 || !wo->accept_remote)) {
1512 if (!reject_if_disagree) {
1513 DECPTR(sizeof(u_int32_t), p);
1514 tl = ntohl(wo->hisaddr);
1517 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1519 * Don't ACK an address of 0.0.0.0 - reject it instead.
1522 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1527 ho->hisaddr = ciaddr1;
1532 /* Microsoft primary or secondary DNS request */
1533 d = citype == CI_MS_DNS2;
1535 /* If we do not have a DNS address then we cannot send it */
1536 if (ao->dnsaddr[d] == 0 ||
1537 cilen != CILEN_ADDR) { /* Check CI length */
1538 orc = CONFREJ; /* Reject CI */
1542 if (htonl(tl) != ao->dnsaddr[d]) {
1543 DECPTR(sizeof(u_int32_t), p);
1544 tl = ntohl(ao->dnsaddr[d]);
1552 /* Microsoft primary or secondary WINS request */
1553 d = citype == CI_MS_WINS2;
1555 /* If we do not have a DNS address then we cannot send it */
1556 if (ao->winsaddr[d] == 0 ||
1557 cilen != CILEN_ADDR) { /* Check CI length */
1558 orc = CONFREJ; /* Reject CI */
1562 if (htonl(tl) != ao->winsaddr[d]) {
1563 DECPTR(sizeof(u_int32_t), p);
1564 tl = ntohl(ao->winsaddr[d]);
1570 case CI_COMPRESSTYPE:
1572 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1576 GETSHORT(cishort, p);
1578 if (!(cishort == IPCP_VJ_COMP ||
1579 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1585 ho->vj_protocol = cishort;
1586 if (cilen == CILEN_VJ) {
1587 GETCHAR(maxslotindex, p);
1588 if (maxslotindex > ao->maxslotindex) {
1590 if (!reject_if_disagree){
1592 PUTCHAR(ao->maxslotindex, p);
1596 if (cflag && !ao->cflag) {
1598 if (!reject_if_disagree){
1600 PUTCHAR(wo->cflag, p);
1603 ho->maxslotindex = maxslotindex;
1607 ho->maxslotindex = MAX_STATES - 1;
1617 if (orc == CONFACK && /* Good CI */
1618 rc != CONFACK) /* but prior CI wasnt? */
1619 continue; /* Don't send this one */
1621 if (orc == CONFNAK) { /* Nak this CI? */
1622 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1623 orc = CONFREJ; /* Get tough if so */
1625 if (rc == CONFREJ) /* Rejecting prior CI? */
1626 continue; /* Don't send this one */
1627 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1628 rc = CONFNAK; /* Not anymore... */
1629 ucp = inp; /* Backup */
1634 if (orc == CONFREJ && /* Reject this CI */
1635 rc != CONFREJ) { /* but no prior ones? */
1637 ucp = inp; /* Backup */
1640 /* Need to move CI? */
1642 BCOPY(cip, ucp, cilen); /* Move it */
1644 /* Update output pointer */
1649 * If we aren't rejecting this packet, and we want to negotiate
1650 * their address, and they didn't send their address, then we
1651 * send a NAK with a CI_ADDR option appended. We assume the
1652 * input buffer is long enough that we can append the extra
1655 if (rc != CONFREJ && !ho->neg_addr && !ho->old_addrs &&
1656 wo->req_addr && !reject_if_disagree &&
1657 ((wo->hisaddr && !wo->accept_remote) || !noremoteip)) {
1658 if (rc == CONFACK) {
1660 ucp = inp; /* reset pointer */
1661 wo->req_addr = 0; /* don't ask again */
1663 PUTCHAR(CI_ADDR, ucp);
1664 PUTCHAR(CILEN_ADDR, ucp);
1665 tl = ntohl(wo->hisaddr);
1669 *len = ucp - inp; /* Compute output length */
1670 IPCPDEBUG(("ipcp: returning Configure-%s", CODENAME(rc)));
1671 return (rc); /* Return final code */
1676 * ip_check_options - check that any IP-related options are OK,
1677 * and assign appropriate defaults.
1680 ip_check_options(void)
1684 ipcp_options *wo = &ipcp_wantoptions[0];
1687 * Default our local IP address based on our hostname.
1688 * If local IP address already given, don't bother.
1690 if (wo->ouraddr == 0 && !disable_defaultip) {
1692 * Look up our hostname (possibly with domain name appended)
1693 * and take the first IP address as our local IP address.
1694 * If there isn't an IP address for our hostname, too bad.
1696 wo->accept_local = 1; /* don't insist on this default value */
1697 if ((hp = gethostbyname(hostname)) != NULL) {
1698 local = *(u_int32_t *)hp->h_addr;
1699 if (local != 0 && !bad_ip_adrs(local))
1700 wo->ouraddr = local;
1703 ask_for_local = wo->ouraddr != 0 || !disable_defaultip;
1708 * ip_demand_conf - configure the interface as though
1709 * IPCP were up, for use with dial-on-demand.
1712 ip_demand_conf(int u)
1714 ipcp_options *wo = &ipcp_wantoptions[u];
1716 if (wo->hisaddr == 0 && !noremoteip) {
1717 /* make up an arbitrary address for the peer */
1718 wo->hisaddr = htonl(0x0a707070 + ifunit);
1719 wo->accept_remote = 1;
1721 if (wo->ouraddr == 0) {
1722 /* make up an arbitrary address for us */
1723 wo->ouraddr = htonl(0x0a404040 + ifunit);
1724 wo->accept_local = 1;
1725 ask_for_local = 0; /* don't tell the peer this address */
1727 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1729 ipcp_script(_PATH_IPPREUP, 1);
1732 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1734 if (wo->default_route)
1735 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr,
1736 wo->replace_default_route))
1737 default_route_set[u] = 1;
1739 if (sifproxyarp(u, wo->hisaddr))
1740 proxy_arp_set[u] = 1;
1742 notice("local IP address %I", wo->ouraddr);
1744 notice("remote IP address %I", wo->hisaddr);
1751 * ipcp_up - IPCP has come UP.
1753 * Configure the IP network interface appropriately and bring it up.
1759 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1760 ipcp_options *go = &ipcp_gotoptions[f->unit];
1761 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1764 IPCPDEBUG(("ipcp: up"));
1767 * We must have a non-zero IP address for both ends of the link.
1770 if (wo->hisaddr && !wo->accept_remote && (!(ho->neg_addr || ho->old_addrs) || ho->hisaddr != wo->hisaddr)) {
1771 error("Peer refused to agree to his IP address");
1772 ipcp_close(f->unit, "Refused his IP address");
1775 if (!ho->neg_addr && !ho->old_addrs)
1776 ho->hisaddr = wo->hisaddr;
1778 if (!(go->neg_addr || go->old_addrs) && (wo->neg_addr || wo->old_addrs)
1779 && wo->ouraddr != 0) {
1780 error("Peer refused to agree to our IP address");
1781 ipcp_close(f->unit, "Refused our IP address");
1784 if (go->ouraddr == 0) {
1785 error("Could not determine local IP address");
1786 ipcp_close(f->unit, "Could not determine local IP address");
1789 if (ho->hisaddr == 0 && !noremoteip) {
1790 ho->hisaddr = htonl(0x0a404040 + ifunit);
1791 warn("Could not determine remote IP address: defaulting to %I",
1794 script_setenv("IPLOCAL", ip_ntoa(go->ouraddr), 0);
1795 if (ho->hisaddr != 0)
1796 script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr), 1);
1803 script_setenv("DNS1", ip_ntoa(go->dnsaddr[0]), 0);
1805 script_setenv("DNS2", ip_ntoa(go->dnsaddr[1]), 0);
1806 if (usepeerdns && (go->dnsaddr[0] || go->dnsaddr[1])) {
1807 script_setenv("USEPEERDNS", "1", 0);
1808 create_resolv(go->dnsaddr[0], go->dnsaddr[1]);
1812 * Check that the peer is allowed to use the IP address it wants.
1814 if (ho->hisaddr != 0 && !auth_ip_addr(f->unit, ho->hisaddr)) {
1815 error("Peer is not authorized to use remote address %I", ho->hisaddr);
1816 ipcp_close(f->unit, "Unauthorized remote IP address");
1820 /* set tcp compression */
1821 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1824 * If we are doing dial-on-demand, the interface is already
1825 * configured, so we put out any saved-up packets, then set the
1826 * interface to pass IP packets.
1829 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1830 ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr,
1831 wo->replace_default_route);
1832 if (go->ouraddr != wo->ouraddr) {
1833 warn("Local IP address changed to %I", go->ouraddr);
1834 script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr), 0);
1835 wo->ouraddr = go->ouraddr;
1837 script_unsetenv("OLDIPLOCAL");
1838 if (ho->hisaddr != wo->hisaddr) {
1839 warn("Remote IP address changed to %I", ho->hisaddr);
1840 if (wo->hisaddr != 0)
1841 script_setenv("OLDIPREMOTE", ip_ntoa(wo->hisaddr), 0);
1842 wo->hisaddr = ho->hisaddr;
1844 script_unsetenv("OLDIPREMOTE");
1846 /* Set the interface to the new addresses */
1847 mask = GetMask(go->ouraddr);
1848 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1850 warn("Interface configuration failed");
1851 ipcp_close(f->unit, "Interface configuration failed");
1855 /* assign a default route through the interface if required */
1856 if (ipcp_wantoptions[f->unit].default_route)
1857 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr,
1858 wo->replace_default_route))
1859 default_route_set[f->unit] = 1;
1861 /* Make a proxy ARP entry if requested. */
1862 if (ho->hisaddr != 0 && ipcp_wantoptions[f->unit].proxy_arp)
1863 if (sifproxyarp(f->unit, ho->hisaddr))
1864 proxy_arp_set[f->unit] = 1;
1867 demand_rexmit(PPP_IP);
1868 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1872 * Set IP addresses and (if specified) netmask.
1874 mask = GetMask(go->ouraddr);
1876 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1877 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1879 warn("Interface configuration failed");
1880 ipcp_close(f->unit, "Interface configuration failed");
1885 ifindex = if_nametoindex(ifname);
1887 /* run the pre-up script, if any, and wait for it to finish */
1888 ipcp_script(_PATH_IPPREUP, 1);
1890 /* check if preup script renamed the interface */
1891 if (!if_indextoname(ifindex, ifname)) {
1892 error("Interface index %d failed to get renamed by a pre-up script", ifindex);
1893 ipcp_close(f->unit, "Interface configuration failed");
1897 /* bring the interface up for IP */
1898 if (!sifup(f->unit)) {
1900 warn("Interface failed to come up");
1901 ipcp_close(f->unit, "Interface configuration failed");
1905 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1906 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1908 warn("Interface configuration failed");
1909 ipcp_close(f->unit, "Interface configuration failed");
1913 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1915 /* assign a default route through the interface if required */
1916 if (ipcp_wantoptions[f->unit].default_route)
1917 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr,
1918 wo->replace_default_route))
1919 default_route_set[f->unit] = 1;
1921 /* Make a proxy ARP entry if requested. */
1922 if (ho->hisaddr != 0 && ipcp_wantoptions[f->unit].proxy_arp)
1923 if (sifproxyarp(f->unit, ho->hisaddr))
1924 proxy_arp_set[f->unit] = 1;
1926 ipcp_wantoptions[0].ouraddr = go->ouraddr;
1928 notice("local IP address %I", go->ouraddr);
1929 if (ho->hisaddr != 0)
1930 notice("remote IP address %I", ho->hisaddr);
1932 notice("primary DNS address %I", go->dnsaddr[0]);
1934 notice("secondary DNS address %I", go->dnsaddr[1]);
1937 reset_link_stats(f->unit);
1939 np_up(f->unit, PPP_IP);
1942 notify(ip_up_notifier, 0);
1947 * Execute the ip-up script, like this:
1948 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1950 if (ipcp_script_state == s_down && ipcp_script_pid == 0) {
1951 ipcp_script_state = s_up;
1952 ipcp_script(path_ipup, 0);
1958 * ipcp_down - IPCP has gone DOWN.
1960 * Take the IP network interface down, clear its addresses
1961 * and delete routes through it.
1966 IPCPDEBUG(("ipcp: down"));
1967 /* XXX a bit IPv4-centric here, we only need to get the stats
1968 * before the interface is marked down. */
1969 /* XXX more correct: we must get the stats before running the notifiers,
1970 * at least for the radius plugin */
1971 update_link_stats(f->unit);
1972 notify(ip_down_notifier, 0);
1977 np_down(f->unit, PPP_IP);
1979 sifvjcomp(f->unit, 0, 0, 0);
1981 print_link_stats(); /* _after_ running the notifiers and ip_down_hook(),
1982 * because print_link_stats() sets link_stats_valid
1986 * If we are doing dial-on-demand, set the interface
1987 * to queue up outgoing packets (for now).
1990 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1992 sifnpmode(f->unit, PPP_IP, NPMODE_DROP);
1994 ipcp_clear_addrs(f->unit, ipcp_gotoptions[f->unit].ouraddr,
1995 ipcp_hisoptions[f->unit].hisaddr, 0);
1998 /* Execute the ip-down script */
1999 if (ipcp_script_state == s_up && ipcp_script_pid == 0) {
2000 ipcp_script_state = s_down;
2001 ipcp_script(path_ipdown, 0);
2007 * ipcp_clear_addrs() - clear the interface addresses, routes,
2008 * proxy arp entries, etc.
2011 ipcp_clear_addrs(int unit, u_int32_t ouraddr, u_int32_t hisaddr, bool replacedefaultroute)
2013 if (proxy_arp_set[unit]) {
2014 cifproxyarp(unit, hisaddr);
2015 proxy_arp_set[unit] = 0;
2017 /* If replacedefaultroute, sifdefaultroute will be called soon
2018 * with replacedefaultroute set and that will overwrite the current
2019 * default route. This is the case only when doing demand, otherwise
2020 * during demand, this cifdefaultroute would restore the old default
2021 * route which is not what we want in this case. In the non-demand
2022 * case, we'll delete the default route and restore the old if there
2023 * is one saved by an sifdefaultroute with replacedefaultroute.
2025 if (!replacedefaultroute && default_route_set[unit]) {
2026 cifdefaultroute(unit, ouraddr, hisaddr);
2027 default_route_set[unit] = 0;
2029 cifaddr(unit, ouraddr, hisaddr);
2034 * ipcp_finished - possibly shut down the lower layers.
2037 ipcp_finished(fsm *f)
2041 np_finished(f->unit, PPP_IP);
2047 * ipcp_script_done - called when the ip-up or ip-down script
2051 ipcp_script_done(void *arg)
2053 ipcp_script_pid = 0;
2054 switch (ipcp_script_state) {
2056 if (ipcp_fsm[0].state != OPENED) {
2057 ipcp_script_state = s_down;
2058 ipcp_script(path_ipdown, 0);
2062 if (ipcp_fsm[0].state == OPENED) {
2063 ipcp_script_state = s_up;
2064 ipcp_script(path_ipup, 0);
2072 * ipcp_script - Execute a script with arguments
2073 * interface-name tty-name speed local-IP remote-IP.
2076 ipcp_script(char *script, int wait)
2078 char strspeed[32], strlocal[32], strremote[32];
2081 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2082 slprintf(strlocal, sizeof(strlocal), "%I", ipcp_gotoptions[0].ouraddr);
2083 slprintf(strremote, sizeof(strremote), "%I", ipcp_hisoptions[0].hisaddr);
2090 argv[5] = strremote;
2094 run_program(script, argv, 0, NULL, NULL, 1);
2096 ipcp_script_pid = run_program(script, argv, 0, ipcp_script_done,
2101 * create_resolv - create the replacement resolv.conf file
2104 create_resolv(u_int32_t peerdns1, u_int32_t peerdns2)
2108 f = fopen(_PATH_RESOLV, "w");
2110 error("Failed to create %s: %m", _PATH_RESOLV);
2115 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns1));
2118 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns2));
2121 error("Write failed to %s: %m", _PATH_RESOLV);
2127 * ipcp_printpkt - print the contents of an IPCP packet.
2129 static char *ipcp_codenames[] = {
2130 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
2131 "TermReq", "TermAck", "CodeRej"
2135 ipcp_printpkt(u_char *p, int plen,
2136 void (*printer) (void *, char *, ...), void *arg)
2138 int code, id, len, olen;
2139 u_char *pstart, *optend;
2143 if (plen < HEADERLEN)
2149 if (len < HEADERLEN || len > plen)
2152 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
2153 printer(arg, " %s", ipcp_codenames[code-1]);
2155 printer(arg, " code=0x%x", code);
2156 printer(arg, " id=0x%x", id);
2163 /* print option list */
2168 if (olen < 2 || olen > len) {
2176 if (olen == CILEN_ADDRS) {
2179 printer(arg, "addrs %I", htonl(cilong));
2181 printer(arg, " %I", htonl(cilong));
2184 case CI_COMPRESSTYPE:
2185 if (olen >= CILEN_COMPRESS) {
2187 GETSHORT(cishort, p);
2188 printer(arg, "compress ");
2193 case IPCP_VJ_COMP_OLD:
2194 printer(arg, "old-VJ");
2197 printer(arg, "0x%x", cishort);
2202 if (olen == CILEN_ADDR) {
2205 printer(arg, "addr %I", htonl(cilong));
2212 printer(arg, "ms-dns%d %I", (code == CI_MS_DNS1? 1: 2),
2219 printer(arg, "ms-wins %I", htonl(cilong));
2222 while (p < optend) {
2224 printer(arg, " %.2x", code);
2232 if (len > 0 && *p >= ' ' && *p < 0x7f) {
2234 print_string((char *)p, len, printer, arg);
2241 /* print the rest of the bytes in the packet */
2242 for (; len > 0; --len) {
2244 printer(arg, " %.2x", code);
2251 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
2252 * We don't bring the link up for IP fragments or for TCP FIN packets
2255 #define IP_HDRLEN 20 /* bytes */
2256 #define IP_OFFMASK 0x1fff
2258 #define IPPROTO_TCP 6
2260 #define TCP_HDRLEN 20
2264 * We use these macros because the IP header may be at an odd address,
2265 * and some compilers might use word loads to get th_off or ip_hl.
2268 #define net_short(x) (((x)[0] << 8) + (x)[1])
2269 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
2270 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
2271 #define get_ipproto(x) (((unsigned char *)(x))[9])
2272 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
2273 #define get_tcpflags(x) (((unsigned char *)(x))[13])
2276 ip_active_pkt(u_char *pkt, int len)
2283 if (len < IP_HDRLEN)
2285 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
2287 if (get_ipproto(pkt) != IPPROTO_TCP)
2289 hlen = get_iphl(pkt) * 4;
2290 if (len < hlen + TCP_HDRLEN)
2293 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)