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>
60 #include "pathnames.h"
64 ipcp_options ipcp_wantoptions[NUM_PPP]; /* Options that we want to request */
65 ipcp_options ipcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
66 ipcp_options ipcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
67 ipcp_options ipcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
69 u_int32_t netmask = 0; /* IP netmask to set on interface */
71 bool disable_defaultip = 0; /* Don't use hostname for default IP adrs */
72 bool noremoteip = 0; /* Let him have no IP address */
74 /* Hook for a plugin to know when IP protocol has come up */
75 void (*ip_up_hook)(void) = NULL;
77 /* Hook for a plugin to know when IP protocol has come down */
78 void (*ip_down_hook)(void) = NULL;
80 /* Hook for a plugin to choose the remote IP address */
81 void (*ip_choose_hook)(u_int32_t *) = NULL;
83 /* Notifiers for when IPCP goes up and down */
84 struct notifier *ip_up_notifier = NULL;
85 struct notifier *ip_down_notifier = NULL;
88 static int default_route_set[NUM_PPP]; /* Have set up a default route */
89 static int proxy_arp_set[NUM_PPP]; /* Have created proxy arp entry */
90 static bool usepeerdns; /* Ask peer for DNS addrs */
91 static int ipcp_is_up; /* have called np_up() */
92 static int ipcp_is_open; /* haven't called np_finished() */
93 static bool ask_for_local; /* request our address from peer */
94 static char vj_value[8]; /* string form of vj option value */
95 static char netmask_str[20]; /* string form of netmask value */
98 * Callbacks for fsm code. (CI = Configuration Information)
100 static void ipcp_resetci (fsm *); /* Reset our CI */
101 static int ipcp_cilen (fsm *); /* Return length of our CI */
102 static void ipcp_addci (fsm *, u_char *, int *); /* Add our CI */
103 static int ipcp_ackci (fsm *, u_char *, int); /* Peer ack'd our CI */
104 static int ipcp_nakci (fsm *, u_char *, int, int);/* Peer nak'd our CI */
105 static int ipcp_rejci (fsm *, u_char *, int); /* Peer rej'd our CI */
106 static int ipcp_reqci (fsm *, u_char *, int *, int); /* Rcv CI */
107 static void ipcp_up (fsm *); /* We're UP */
108 static void ipcp_down (fsm *); /* We're DOWN */
109 static void ipcp_finished (fsm *); /* Don't need lower layer */
111 fsm ipcp_fsm[NUM_PPP]; /* IPCP fsm structure */
113 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
114 ipcp_resetci, /* Reset our Configuration Information */
115 ipcp_cilen, /* Length of our Configuration Information */
116 ipcp_addci, /* Add our Configuration Information */
117 ipcp_ackci, /* ACK our Configuration Information */
118 ipcp_nakci, /* NAK our Configuration Information */
119 ipcp_rejci, /* Reject our Configuration Information */
120 ipcp_reqci, /* Request peer's Configuration Information */
121 ipcp_up, /* Called when fsm reaches OPENED state */
122 ipcp_down, /* Called when fsm leaves OPENED state */
123 NULL, /* Called when we want the lower layer up */
124 ipcp_finished, /* Called when we want the lower layer down */
125 NULL, /* Called when Protocol-Reject received */
126 NULL, /* Retransmission is necessary */
127 NULL, /* Called to handle protocol-specific codes */
128 "IPCP" /* String name of protocol */
132 * Command-line options.
134 static int setvjslots (char **);
135 static int setdnsaddr (char **);
136 static int setwinsaddr (char **);
137 static int setnetmask (char **);
138 int setipaddr (char *, char **, int);
139 static void printipaddr (option_t *, void (*)(void *, char *,...),void *);
141 static option_t ipcp_option_list[] = {
142 { "noip", o_bool, &ipcp_protent.enabled_flag,
143 "Disable IP and IPCP" },
144 { "-ip", o_bool, &ipcp_protent.enabled_flag,
145 "Disable IP and IPCP", OPT_ALIAS },
147 { "novj", o_bool, &ipcp_wantoptions[0].neg_vj,
148 "Disable VJ compression", OPT_A2CLR, &ipcp_allowoptions[0].neg_vj },
149 { "-vj", o_bool, &ipcp_wantoptions[0].neg_vj,
150 "Disable VJ compression", OPT_ALIAS | OPT_A2CLR,
151 &ipcp_allowoptions[0].neg_vj },
153 { "novjccomp", o_bool, &ipcp_wantoptions[0].cflag,
154 "Disable VJ connection-ID compression", OPT_A2CLR,
155 &ipcp_allowoptions[0].cflag },
156 { "-vjccomp", o_bool, &ipcp_wantoptions[0].cflag,
157 "Disable VJ connection-ID compression", OPT_ALIAS | OPT_A2CLR,
158 &ipcp_allowoptions[0].cflag },
160 { "vj-max-slots", o_special, (void *)setvjslots,
161 "Set maximum VJ header slots",
162 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, vj_value },
164 { "ipcp-accept-local", o_bool, &ipcp_wantoptions[0].accept_local,
165 "Accept peer's address for us", 1 },
166 { "ipcp-accept-remote", o_bool, &ipcp_wantoptions[0].accept_remote,
167 "Accept peer's address for it", 1 },
169 { "ipparam", o_string, &ipparam,
170 "Set ip script parameter", OPT_PRIO },
172 { "noipdefault", o_bool, &disable_defaultip,
173 "Don't use name for default IP adrs", 1 },
175 { "ms-dns", o_special, (void *)setdnsaddr,
176 "DNS address for the peer's use", OPT_A2LIST },
177 { "ms-wins", o_special, (void *)setwinsaddr,
178 "Nameserver for SMB over TCP/IP for peer", OPT_A2LIST },
180 { "ipcp-restart", o_int, &ipcp_fsm[0].timeouttime,
181 "Set timeout for IPCP", OPT_PRIO },
182 { "ipcp-max-terminate", o_int, &ipcp_fsm[0].maxtermtransmits,
183 "Set max #xmits for term-reqs", OPT_PRIO },
184 { "ipcp-max-configure", o_int, &ipcp_fsm[0].maxconfreqtransmits,
185 "Set max #xmits for conf-reqs", OPT_PRIO },
186 { "ipcp-max-failure", o_int, &ipcp_fsm[0].maxnakloops,
187 "Set max #conf-naks for IPCP", OPT_PRIO },
189 { "defaultroute", o_bool, &ipcp_wantoptions[0].default_route,
190 "Add default route", OPT_ENABLE|1, &ipcp_allowoptions[0].default_route },
191 { "nodefaultroute", o_bool, &ipcp_allowoptions[0].default_route,
192 "disable defaultroute option", OPT_A2CLR,
193 &ipcp_wantoptions[0].default_route },
194 { "-defaultroute", o_bool, &ipcp_allowoptions[0].default_route,
195 "disable defaultroute option", OPT_ALIAS | OPT_A2CLR,
196 &ipcp_wantoptions[0].default_route },
198 { "proxyarp", o_bool, &ipcp_wantoptions[0].proxy_arp,
199 "Add proxy ARP entry", OPT_ENABLE|1, &ipcp_allowoptions[0].proxy_arp },
200 { "noproxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
201 "disable proxyarp option", OPT_A2CLR,
202 &ipcp_wantoptions[0].proxy_arp },
203 { "-proxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
204 "disable proxyarp option", OPT_ALIAS | OPT_A2CLR,
205 &ipcp_wantoptions[0].proxy_arp },
207 { "usepeerdns", o_bool, &usepeerdns,
208 "Ask peer for DNS address(es)", 1 },
210 { "netmask", o_special, (void *)setnetmask,
211 "set netmask", OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, netmask_str },
213 { "ipcp-no-addresses", o_bool, &ipcp_wantoptions[0].old_addrs,
214 "Disable old-style IP-Addresses usage", OPT_A2CLR,
215 &ipcp_allowoptions[0].old_addrs },
216 { "ipcp-no-address", o_bool, &ipcp_wantoptions[0].neg_addr,
217 "Disable IP-Address usage", OPT_A2CLR,
218 &ipcp_allowoptions[0].neg_addr },
220 { "noremoteip", o_bool, &noremoteip,
221 "Allow peer to have no IP address", 1 },
223 { "nosendip", o_bool, &ipcp_wantoptions[0].neg_addr,
224 "Don't send our IP address to peer", OPT_A2CLR,
225 &ipcp_wantoptions[0].old_addrs},
227 { "IP addresses", o_wild, (void *) &setipaddr,
228 "set local and remote IP addresses",
229 OPT_NOARG | OPT_A2PRINTER, (void *) &printipaddr },
235 * Protocol entry points from main code.
237 static void ipcp_init (int);
238 static void ipcp_open (int);
239 static void ipcp_close (int, char *);
240 static void ipcp_lowerup (int);
241 static void ipcp_lowerdown (int);
242 static void ipcp_input (int, u_char *, int);
243 static void ipcp_protrej (int);
244 static int ipcp_printpkt (u_char *, int,
245 void (*) (void *, char *, ...), void *);
246 static void ip_check_options (void);
247 static int ip_demand_conf (int);
248 static int ip_active_pkt (u_char *, int);
249 static void create_resolv (u_int32_t, u_int32_t);
251 struct protent ipcp_protent = {
271 static void ipcp_clear_addrs (int, u_int32_t, u_int32_t);
272 static void ipcp_script (char *, int); /* Run an up/down script */
273 static void ipcp_script_done (void *);
276 * Lengths of configuration options.
279 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
280 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
281 #define CILEN_ADDR 6 /* new-style single address option */
282 #define CILEN_ADDRS 10 /* old-style dual address option */
285 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
286 (x) == CONFNAK ? "NAK" : "REJ")
289 * This state variable is used to ensure that we don't
290 * run an ipcp-up/down script while one is already running.
292 static enum script_state {
296 static pid_t ipcp_script_pid;
299 * Make a string representation of a network IP address.
302 ip_ntoa(u_int32_t ipaddr)
306 slprintf(b, sizeof(b), "%I", ipaddr);
315 * setvjslots - set maximum number of connection slots for VJ compression
318 setvjslots(char **argv)
322 if (!int_option(*argv, &value))
324 if (value < 2 || value > 16) {
325 option_error("vj-max-slots value must be between 2 and 16");
328 ipcp_wantoptions [0].maxslotindex =
329 ipcp_allowoptions[0].maxslotindex = value - 1;
330 slprintf(vj_value, sizeof(vj_value), "%d", value);
335 * setdnsaddr - set the dns address(es)
338 setdnsaddr(char **argv)
343 dns = inet_addr(*argv);
344 if (dns == (u_int32_t) -1) {
345 if ((hp = gethostbyname(*argv)) == NULL) {
346 option_error("invalid address parameter '%s' for ms-dns option",
350 dns = *(u_int32_t *)hp->h_addr;
353 /* We take the last 2 values given, the 2nd-last as the primary
354 and the last as the secondary. If only one is given it
355 becomes both primary and secondary. */
356 if (ipcp_allowoptions[0].dnsaddr[1] == 0)
357 ipcp_allowoptions[0].dnsaddr[0] = dns;
359 ipcp_allowoptions[0].dnsaddr[0] = ipcp_allowoptions[0].dnsaddr[1];
361 /* always set the secondary address value. */
362 ipcp_allowoptions[0].dnsaddr[1] = dns;
368 * setwinsaddr - set the wins address(es)
369 * This is primrarly used with the Samba package under UNIX or for pointing
370 * the caller to the existing WINS server on a Windows NT platform.
373 setwinsaddr(char **argv)
378 wins = inet_addr(*argv);
379 if (wins == (u_int32_t) -1) {
380 if ((hp = gethostbyname(*argv)) == NULL) {
381 option_error("invalid address parameter '%s' for ms-wins option",
385 wins = *(u_int32_t *)hp->h_addr;
388 /* We take the last 2 values given, the 2nd-last as the primary
389 and the last as the secondary. If only one is given it
390 becomes both primary and secondary. */
391 if (ipcp_allowoptions[0].winsaddr[1] == 0)
392 ipcp_allowoptions[0].winsaddr[0] = wins;
394 ipcp_allowoptions[0].winsaddr[0] = ipcp_allowoptions[0].winsaddr[1];
396 /* always set the secondary address value. */
397 ipcp_allowoptions[0].winsaddr[1] = wins;
403 * setipaddr - Set the IP address
404 * If doit is 0, the call is to check whether this option is
405 * potentially an IP address specification.
406 * Not static so that plugins can call it to set the addresses
409 setipaddr(char *arg, char **argv, int doit)
413 u_int32_t local, remote;
414 ipcp_options *wo = &ipcp_wantoptions[0];
415 static int prio_local = 0, prio_remote = 0;
418 * IP address pair separated by ":".
420 if ((colon = strchr(arg, ':')) == NULL)
426 * If colon first character, then no local addr.
428 if (colon != arg && option_priority >= prio_local) {
430 if ((local = inet_addr(arg)) == (u_int32_t) -1) {
431 if ((hp = gethostbyname(arg)) == NULL) {
432 option_error("unknown host: %s", arg);
435 local = *(u_int32_t *)hp->h_addr;
437 if (bad_ip_adrs(local)) {
438 option_error("bad local IP address %s", ip_ntoa(local));
444 prio_local = option_priority;
448 * If colon last character, then no remote addr.
450 if (*++colon != '\0' && option_priority >= prio_remote) {
451 if ((remote = inet_addr(colon)) == (u_int32_t) -1) {
452 if ((hp = gethostbyname(colon)) == NULL) {
453 option_error("unknown host: %s", colon);
456 remote = *(u_int32_t *)hp->h_addr;
457 if (remote_name[0] == 0)
458 strlcpy(remote_name, colon, sizeof(remote_name));
460 if (bad_ip_adrs(remote)) {
461 option_error("bad remote IP address %s", ip_ntoa(remote));
465 wo->hisaddr = remote;
466 prio_remote = option_priority;
473 printipaddr(option_t *opt, void (*printer) (void *, char *, ...), void *arg)
475 ipcp_options *wo = &ipcp_wantoptions[0];
477 if (wo->ouraddr != 0)
478 printer(arg, "%I", wo->ouraddr);
480 if (wo->hisaddr != 0)
481 printer(arg, "%I", wo->hisaddr);
485 * setnetmask - set the netmask to be used on the interface.
488 setnetmask(char **argv)
495 * Unfortunately, if we use inet_addr, we can't tell whether
496 * a result of all 1s is an error or a valid 255.255.255.255.
499 n = parse_dotted_ip(p, &mask);
503 if (n == 0 || p[n] != 0 || (netmask & ~mask) != 0) {
504 option_error("invalid netmask value '%s'", *argv);
509 slprintf(netmask_str, sizeof(netmask_str), "%I", mask);
515 parse_dotted_ip(char *p, u_int32_t *vp)
523 b = strtoul(p, &endp, 0);
529 /* accept e.g. 0xffffff00 */
547 * ipcp_init - Initialize IPCP.
552 fsm *f = &ipcp_fsm[unit];
553 ipcp_options *wo = &ipcp_wantoptions[unit];
554 ipcp_options *ao = &ipcp_allowoptions[unit];
557 f->protocol = PPP_IPCP;
558 f->callbacks = &ipcp_callbacks;
559 fsm_init(&ipcp_fsm[unit]);
562 * Some 3G modems use repeated IPCP NAKs as a way of stalling
563 * until they can contact a server on the network, so we increase
564 * the default number of NAKs we accept before we start treating
567 f->maxnakloops = 100;
569 memset(wo, 0, sizeof(*wo));
570 memset(ao, 0, sizeof(*ao));
572 wo->neg_addr = wo->old_addrs = 1;
574 wo->vj_protocol = IPCP_VJ_COMP;
575 wo->maxslotindex = MAX_STATES - 1; /* really max index */
579 /* max slots and slot-id compression are currently hardwired in */
580 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
583 ao->neg_addr = ao->old_addrs = 1;
585 ao->maxslotindex = MAX_STATES - 1;
589 * XXX These control whether the user may use the proxyarp
590 * and defaultroute options.
593 ao->default_route = 1;
598 * ipcp_open - IPCP is allowed to come up.
603 fsm_open(&ipcp_fsm[unit]);
609 * ipcp_close - Take IPCP down.
612 ipcp_close(int unit, char *reason)
614 fsm_close(&ipcp_fsm[unit], reason);
619 * ipcp_lowerup - The lower layer is up.
622 ipcp_lowerup(int unit)
624 fsm_lowerup(&ipcp_fsm[unit]);
629 * ipcp_lowerdown - The lower layer is down.
632 ipcp_lowerdown(int unit)
634 fsm_lowerdown(&ipcp_fsm[unit]);
639 * ipcp_input - Input IPCP packet.
642 ipcp_input(int unit, u_char *p, int len)
644 fsm_input(&ipcp_fsm[unit], p, len);
649 * ipcp_protrej - A Protocol-Reject was received for IPCP.
651 * Pretend the lower layer went down, so we shut up.
654 ipcp_protrej(int unit)
656 fsm_lowerdown(&ipcp_fsm[unit]);
661 * ipcp_resetci - Reset our CI.
662 * Called by fsm_sconfreq, Send Configure Request.
667 ipcp_options *wo = &ipcp_wantoptions[f->unit];
668 ipcp_options *go = &ipcp_gotoptions[f->unit];
669 ipcp_options *ao = &ipcp_allowoptions[f->unit];
671 wo->req_addr = (wo->neg_addr || wo->old_addrs) &&
672 (ao->neg_addr || ao->old_addrs);
673 if (wo->ouraddr == 0)
674 wo->accept_local = 1;
675 if (wo->hisaddr == 0)
676 wo->accept_remote = 1;
677 wo->req_dns1 = usepeerdns; /* Request DNS addresses from the peer */
678 wo->req_dns2 = usepeerdns;
682 if (ip_choose_hook) {
683 ip_choose_hook(&wo->hisaddr);
685 wo->accept_remote = 0;
688 BZERO(&ipcp_hisoptions[f->unit], sizeof(ipcp_options));
693 * ipcp_cilen - Return length of our CI.
694 * Called by fsm_sconfreq, Send Configure Request.
699 ipcp_options *go = &ipcp_gotoptions[f->unit];
700 ipcp_options *wo = &ipcp_wantoptions[f->unit];
701 ipcp_options *ho = &ipcp_hisoptions[f->unit];
703 #define LENCIADDRS(neg) (neg ? CILEN_ADDRS : 0)
704 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
705 #define LENCIADDR(neg) (neg ? CILEN_ADDR : 0)
706 #define LENCIDNS(neg) LENCIADDR(neg)
707 #define LENCIWINS(neg) LENCIADDR(neg)
710 * First see if we want to change our options to the old
711 * forms because we have received old forms from the peer.
713 if (go->neg_addr && go->old_addrs && !ho->neg_addr && ho->old_addrs)
715 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
716 /* try an older style of VJ negotiation */
717 /* use the old style only if the peer did */
718 if (ho->neg_vj && ho->old_vj) {
721 go->vj_protocol = ho->vj_protocol;
725 return (LENCIADDRS(!go->neg_addr && go->old_addrs) +
726 LENCIVJ(go->neg_vj, go->old_vj) +
727 LENCIADDR(go->neg_addr) +
728 LENCIDNS(go->req_dns1) +
729 LENCIDNS(go->req_dns2) +
730 LENCIWINS(go->winsaddr[0]) +
731 LENCIWINS(go->winsaddr[1])) ;
736 * ipcp_addci - Add our desired CIs to a packet.
737 * Called by fsm_sconfreq, Send Configure Request.
740 ipcp_addci(fsm *f, u_char *ucp, int *lenp)
742 ipcp_options *go = &ipcp_gotoptions[f->unit];
745 #define ADDCIADDRS(opt, neg, val1, val2) \
747 if (len >= CILEN_ADDRS) { \
750 PUTCHAR(CILEN_ADDRS, ucp); \
755 len -= CILEN_ADDRS; \
760 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
762 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
763 if (len >= vjlen) { \
765 PUTCHAR(vjlen, ucp); \
766 PUTSHORT(val, ucp); \
768 PUTCHAR(maxslotindex, ucp); \
769 PUTCHAR(cflag, ucp); \
776 #define ADDCIADDR(opt, neg, val) \
778 if (len >= CILEN_ADDR) { \
781 PUTCHAR(CILEN_ADDR, ucp); \
789 #define ADDCIDNS(opt, neg, addr) \
791 if (len >= CILEN_ADDR) { \
794 PUTCHAR(CILEN_ADDR, ucp); \
802 #define ADDCIWINS(opt, addr) \
804 if (len >= CILEN_ADDR) { \
807 PUTCHAR(CILEN_ADDR, ucp); \
815 ADDCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs, go->ouraddr,
818 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
819 go->maxslotindex, go->cflag);
821 ADDCIADDR(CI_ADDR, go->neg_addr, go->ouraddr);
823 ADDCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
825 ADDCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
827 ADDCIWINS(CI_MS_WINS1, go->winsaddr[0]);
829 ADDCIWINS(CI_MS_WINS2, go->winsaddr[1]);
836 * ipcp_ackci - Ack our CIs.
837 * Called by fsm_rconfack, Receive Configure ACK.
844 ipcp_ackci(fsm *f, u_char *p, int len)
846 ipcp_options *go = &ipcp_gotoptions[f->unit];
847 u_short cilen, citype, cishort;
849 u_char cimaxslotindex, cicflag;
852 * CIs must be in exactly the same order that we sent...
853 * Check packet length and CI length at each step.
854 * If we find any deviations, then this packet is bad.
857 #define ACKCIADDRS(opt, neg, val1, val2) \
860 if ((len -= CILEN_ADDRS) < 0) \
862 GETCHAR(citype, p); \
864 if (cilen != CILEN_ADDRS || \
869 if (val1 != cilong) \
873 if (val2 != cilong) \
877 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
879 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
880 if ((len -= vjlen) < 0) \
882 GETCHAR(citype, p); \
884 if (cilen != vjlen || \
887 GETSHORT(cishort, p); \
888 if (cishort != val) \
891 GETCHAR(cimaxslotindex, p); \
892 if (cimaxslotindex != maxslotindex) \
894 GETCHAR(cicflag, p); \
895 if (cicflag != cflag) \
900 #define ACKCIADDR(opt, neg, val) \
903 if ((len -= CILEN_ADDR) < 0) \
905 GETCHAR(citype, p); \
907 if (cilen != CILEN_ADDR || \
916 #define ACKCIDNS(opt, neg, addr) \
919 if ((len -= CILEN_ADDR) < 0) \
921 GETCHAR(citype, p); \
923 if (cilen != CILEN_ADDR || citype != opt) \
927 if (addr != cilong) \
931 #define ACKCIWINS(opt, addr) \
934 if ((len -= CILEN_ADDR) < 0) \
936 GETCHAR(citype, p); \
938 if (cilen != CILEN_ADDR || citype != opt) \
942 if (addr != cilong) \
946 ACKCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs, go->ouraddr,
949 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
950 go->maxslotindex, go->cflag);
952 ACKCIADDR(CI_ADDR, go->neg_addr, go->ouraddr);
954 ACKCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
956 ACKCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
958 ACKCIWINS(CI_MS_WINS1, go->winsaddr[0]);
960 ACKCIWINS(CI_MS_WINS2, go->winsaddr[1]);
963 * If there are any remaining CIs, then this packet is bad.
970 IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
975 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
976 * This should not modify any state if the Nak is bad
977 * or if IPCP is in the OPENED state.
978 * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
985 ipcp_nakci(fsm *f, u_char *p, int len, int treat_as_reject)
987 ipcp_options *go = &ipcp_gotoptions[f->unit];
988 u_char cimaxslotindex, cicflag;
989 u_char citype, cilen, *next;
991 u_int32_t ciaddr1, ciaddr2, l, cidnsaddr;
992 ipcp_options no; /* options we've seen Naks for */
993 ipcp_options try; /* options to request next time */
995 BZERO(&no, sizeof(no));
999 * Any Nak'd CIs must be in exactly the same order that we sent.
1000 * Check packet length and CI length at each step.
1001 * If we find any deviations, then this packet is bad.
1003 #define NAKCIADDRS(opt, neg, code) \
1005 (cilen = p[1]) == CILEN_ADDRS && \
1011 ciaddr1 = htonl(l); \
1013 ciaddr2 = htonl(l); \
1018 #define NAKCIVJ(opt, neg, code) \
1020 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
1025 GETSHORT(cishort, p); \
1030 #define NAKCIADDR(opt, neg, code) \
1032 (cilen = p[1]) == CILEN_ADDR && \
1038 ciaddr1 = htonl(l); \
1043 #define NAKCIDNS(opt, neg, code) \
1045 ((cilen = p[1]) == CILEN_ADDR) && \
1051 cidnsaddr = htonl(l); \
1057 * Accept the peer's idea of {our,his} address, if different
1058 * from our idea, only if the accept_{local,remote} flag is set.
1060 NAKCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs,
1061 if (treat_as_reject) {
1064 if (go->accept_local && ciaddr1) {
1065 /* take his idea of our address */
1066 try.ouraddr = ciaddr1;
1068 if (go->accept_remote && ciaddr2) {
1069 /* take his idea of his address */
1070 try.hisaddr = ciaddr2;
1076 * Accept the peer's value of maxslotindex provided that it
1077 * is less than what we asked for. Turn off slot-ID compression
1078 * if the peer wants. Send old-style compress-type option if
1081 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
1082 if (treat_as_reject) {
1084 } else if (cilen == CILEN_VJ) {
1085 GETCHAR(cimaxslotindex, p);
1086 GETCHAR(cicflag, p);
1087 if (cishort == IPCP_VJ_COMP) {
1089 if (cimaxslotindex < go->maxslotindex)
1090 try.maxslotindex = cimaxslotindex;
1097 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
1099 try.vj_protocol = cishort;
1106 NAKCIADDR(CI_ADDR, neg_addr,
1107 if (treat_as_reject) {
1110 } else if (go->accept_local && ciaddr1) {
1111 /* take his idea of our address */
1112 try.ouraddr = ciaddr1;
1116 NAKCIDNS(CI_MS_DNS1, req_dns1,
1117 if (treat_as_reject) {
1120 try.dnsaddr[0] = cidnsaddr;
1124 NAKCIDNS(CI_MS_DNS2, req_dns2,
1125 if (treat_as_reject) {
1128 try.dnsaddr[1] = cidnsaddr;
1133 * There may be remaining CIs, if the peer is requesting negotiation
1134 * on an option that we didn't include in our request packet.
1135 * If they want to negotiate about IP addresses, we comply.
1136 * If they want us to ask for compression, we refuse.
1137 * If they want us to ask for ms-dns, we do that, since some
1138 * peers get huffy if we don't.
1140 while (len >= CILEN_VOID) {
1143 if ( cilen < CILEN_VOID || (len -= cilen) < 0 )
1145 next = p + cilen - 2;
1148 case CI_COMPRESSTYPE:
1149 if (go->neg_vj || no.neg_vj ||
1150 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
1155 if ((!go->neg_addr && go->old_addrs) || no.old_addrs
1156 || cilen != CILEN_ADDRS)
1161 if (ciaddr1 && go->accept_local)
1162 try.ouraddr = ciaddr1;
1165 if (ciaddr2 && go->accept_remote)
1166 try.hisaddr = ciaddr2;
1170 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
1175 if (ciaddr1 && go->accept_local)
1176 try.ouraddr = ciaddr1;
1177 if (try.ouraddr != 0)
1182 if (go->req_dns1 || no.req_dns1 || cilen != CILEN_ADDR)
1185 try.dnsaddr[0] = htonl(l);
1190 if (go->req_dns2 || no.req_dns2 || cilen != CILEN_ADDR)
1193 try.dnsaddr[1] = htonl(l);
1199 if (cilen != CILEN_ADDR)
1204 try.winsaddr[citype == CI_MS_WINS2] = ciaddr1;
1211 * OK, the Nak is good. Now we can update state.
1212 * If there are any remaining options, we ignore them.
1214 if (f->state != OPENED)
1220 IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
1226 * ipcp_rejci - Reject some of our CIs.
1227 * Callback from fsm_rconfnakrej.
1230 ipcp_rejci(fsm *f, u_char *p, int len)
1232 ipcp_options *go = &ipcp_gotoptions[f->unit];
1233 u_char cimaxslotindex, ciflag, cilen;
1236 ipcp_options try; /* options to request next time */
1240 * Any Rejected CIs must be in exactly the same order that we sent.
1241 * Check packet length and CI length at each step.
1242 * If we find any deviations, then this packet is bad.
1244 #define REJCIADDRS(opt, neg, val1, val2) \
1246 (cilen = p[1]) == CILEN_ADDRS && \
1253 cilong = htonl(l); \
1254 /* Check rejected value. */ \
1255 if (cilong != val1) \
1258 cilong = htonl(l); \
1259 /* Check rejected value. */ \
1260 if (cilong != val2) \
1262 try.old_addrs = 0; \
1265 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
1267 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
1272 GETSHORT(cishort, p); \
1273 /* Check rejected value. */ \
1274 if (cishort != val) \
1277 GETCHAR(cimaxslotindex, p); \
1278 if (cimaxslotindex != maxslot) \
1280 GETCHAR(ciflag, p); \
1281 if (ciflag != cflag) \
1287 #define REJCIADDR(opt, neg, val) \
1289 (cilen = p[1]) == CILEN_ADDR && \
1296 cilong = htonl(l); \
1297 /* Check rejected value. */ \
1298 if (cilong != val) \
1303 #define REJCIDNS(opt, neg, dnsaddr) \
1305 ((cilen = p[1]) == CILEN_ADDR) && \
1312 cilong = htonl(l); \
1313 /* Check rejected value. */ \
1314 if (cilong != dnsaddr) \
1319 #define REJCIWINS(opt, addr) \
1321 ((cilen = p[1]) == CILEN_ADDR) && \
1328 cilong = htonl(l); \
1329 /* Check rejected value. */ \
1330 if (cilong != addr) \
1332 try.winsaddr[opt == CI_MS_WINS2] = 0; \
1335 REJCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs,
1336 go->ouraddr, go->hisaddr);
1338 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
1339 go->maxslotindex, go->cflag);
1341 REJCIADDR(CI_ADDR, neg_addr, go->ouraddr);
1343 REJCIDNS(CI_MS_DNS1, req_dns1, go->dnsaddr[0]);
1345 REJCIDNS(CI_MS_DNS2, req_dns2, go->dnsaddr[1]);
1347 REJCIWINS(CI_MS_WINS1, go->winsaddr[0]);
1349 REJCIWINS(CI_MS_WINS2, go->winsaddr[1]);
1352 * If there are any remaining CIs, then this packet is bad.
1357 * Now we can update state.
1359 if (f->state != OPENED)
1364 IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
1370 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
1371 * Callback from fsm_rconfreq, Receive Configure Request
1373 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1374 * appropriately. If reject_if_disagree is non-zero, doesn't return
1375 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1378 ipcp_reqci(fsm *f, u_char *inp, int *len, int reject_if_disagree)
1380 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1381 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1382 ipcp_options *ao = &ipcp_allowoptions[f->unit];
1383 u_char *cip, *next; /* Pointer to current and next CIs */
1384 u_short cilen, citype; /* Parsed len, type */
1385 u_short cishort; /* Parsed short value */
1386 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
1387 int rc = CONFACK; /* Final packet return code */
1388 int orc; /* Individual option return code */
1389 u_char *p; /* Pointer to next char to parse */
1390 u_char *ucp = inp; /* Pointer to current output char */
1391 int l = *len; /* Length left */
1392 u_char maxslotindex, cflag;
1396 * Reset all his options.
1398 BZERO(ho, sizeof(*ho));
1401 * Process all his options.
1405 orc = CONFACK; /* Assume success */
1406 cip = p = next; /* Remember begining of CI */
1407 if (l < 2 || /* Not enough data for CI header or */
1408 p[1] < 2 || /* CI length too small or */
1409 p[1] > l) { /* CI length too big? */
1410 IPCPDEBUG(("ipcp_reqci: bad CI length!"));
1411 orc = CONFREJ; /* Reject bad CI */
1412 cilen = l; /* Reject till end of packet */
1413 l = 0; /* Don't loop again */
1416 GETCHAR(citype, p); /* Parse CI type */
1417 GETCHAR(cilen, p); /* Parse CI length */
1418 l -= cilen; /* Adjust remaining length */
1419 next += cilen; /* Step to next CI */
1421 switch (citype) { /* Check CI type */
1423 if (!ao->old_addrs || ho->neg_addr ||
1424 cilen != CILEN_ADDRS) { /* Check CI length */
1425 orc = CONFREJ; /* Reject CI */
1430 * If he has no address, or if we both have his address but
1431 * disagree about it, then NAK it with our idea.
1432 * In particular, if we don't know his address, but he does,
1435 GETLONG(tl, p); /* Parse source address (his) */
1436 ciaddr1 = htonl(tl);
1437 if (ciaddr1 != wo->hisaddr
1438 && (ciaddr1 == 0 || !wo->accept_remote)) {
1440 if (!reject_if_disagree) {
1441 DECPTR(sizeof(u_int32_t), p);
1442 tl = ntohl(wo->hisaddr);
1445 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1447 * If neither we nor he knows his address, reject the option.
1450 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1455 * If he doesn't know our address, or if we both have our address
1456 * but disagree about it, then NAK it with our idea.
1458 GETLONG(tl, p); /* Parse desination address (ours) */
1459 ciaddr2 = htonl(tl);
1460 if (ciaddr2 != wo->ouraddr) {
1461 if (ciaddr2 == 0 || !wo->accept_local) {
1463 if (!reject_if_disagree) {
1464 DECPTR(sizeof(u_int32_t), p);
1465 tl = ntohl(wo->ouraddr);
1469 wo->ouraddr = ciaddr2; /* accept peer's idea */
1474 ho->hisaddr = ciaddr1;
1475 ho->ouraddr = ciaddr2;
1479 if (!ao->neg_addr || ho->old_addrs ||
1480 cilen != CILEN_ADDR) { /* Check CI length */
1481 orc = CONFREJ; /* Reject CI */
1486 * If he has no address, or if we both have his address but
1487 * disagree about it, then NAK it with our idea.
1488 * In particular, if we don't know his address, but he does,
1491 GETLONG(tl, p); /* Parse source address (his) */
1492 ciaddr1 = htonl(tl);
1493 if (ciaddr1 != wo->hisaddr
1494 && (ciaddr1 == 0 || !wo->accept_remote)) {
1496 if (!reject_if_disagree) {
1497 DECPTR(sizeof(u_int32_t), p);
1498 tl = ntohl(wo->hisaddr);
1501 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1503 * Don't ACK an address of 0.0.0.0 - reject it instead.
1506 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1511 ho->hisaddr = ciaddr1;
1516 /* Microsoft primary or secondary DNS request */
1517 d = citype == CI_MS_DNS2;
1519 /* If we do not have a DNS address then we cannot send it */
1520 if (ao->dnsaddr[d] == 0 ||
1521 cilen != CILEN_ADDR) { /* Check CI length */
1522 orc = CONFREJ; /* Reject CI */
1526 if (htonl(tl) != ao->dnsaddr[d]) {
1527 DECPTR(sizeof(u_int32_t), p);
1528 tl = ntohl(ao->dnsaddr[d]);
1536 /* Microsoft primary or secondary WINS request */
1537 d = citype == CI_MS_WINS2;
1539 /* If we do not have a DNS address then we cannot send it */
1540 if (ao->winsaddr[d] == 0 ||
1541 cilen != CILEN_ADDR) { /* Check CI length */
1542 orc = CONFREJ; /* Reject CI */
1546 if (htonl(tl) != ao->winsaddr[d]) {
1547 DECPTR(sizeof(u_int32_t), p);
1548 tl = ntohl(ao->winsaddr[d]);
1554 case CI_COMPRESSTYPE:
1556 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1560 GETSHORT(cishort, p);
1562 if (!(cishort == IPCP_VJ_COMP ||
1563 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1569 ho->vj_protocol = cishort;
1570 if (cilen == CILEN_VJ) {
1571 GETCHAR(maxslotindex, p);
1572 if (maxslotindex > ao->maxslotindex) {
1574 if (!reject_if_disagree){
1576 PUTCHAR(ao->maxslotindex, p);
1580 if (cflag && !ao->cflag) {
1582 if (!reject_if_disagree){
1584 PUTCHAR(wo->cflag, p);
1587 ho->maxslotindex = maxslotindex;
1591 ho->maxslotindex = MAX_STATES - 1;
1601 if (orc == CONFACK && /* Good CI */
1602 rc != CONFACK) /* but prior CI wasnt? */
1603 continue; /* Don't send this one */
1605 if (orc == CONFNAK) { /* Nak this CI? */
1606 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1607 orc = CONFREJ; /* Get tough if so */
1609 if (rc == CONFREJ) /* Rejecting prior CI? */
1610 continue; /* Don't send this one */
1611 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1612 rc = CONFNAK; /* Not anymore... */
1613 ucp = inp; /* Backup */
1618 if (orc == CONFREJ && /* Reject this CI */
1619 rc != CONFREJ) { /* but no prior ones? */
1621 ucp = inp; /* Backup */
1624 /* Need to move CI? */
1626 BCOPY(cip, ucp, cilen); /* Move it */
1628 /* Update output pointer */
1633 * If we aren't rejecting this packet, and we want to negotiate
1634 * their address, and they didn't send their address, then we
1635 * send a NAK with a CI_ADDR option appended. We assume the
1636 * input buffer is long enough that we can append the extra
1639 if (rc != CONFREJ && !ho->neg_addr && !ho->old_addrs &&
1640 wo->req_addr && !reject_if_disagree && !noremoteip) {
1641 if (rc == CONFACK) {
1643 ucp = inp; /* reset pointer */
1644 wo->req_addr = 0; /* don't ask again */
1646 PUTCHAR(CI_ADDR, ucp);
1647 PUTCHAR(CILEN_ADDR, ucp);
1648 tl = ntohl(wo->hisaddr);
1652 *len = ucp - inp; /* Compute output length */
1653 IPCPDEBUG(("ipcp: returning Configure-%s", CODENAME(rc)));
1654 return (rc); /* Return final code */
1659 * ip_check_options - check that any IP-related options are OK,
1660 * and assign appropriate defaults.
1663 ip_check_options(void)
1667 ipcp_options *wo = &ipcp_wantoptions[0];
1670 * Default our local IP address based on our hostname.
1671 * If local IP address already given, don't bother.
1673 if (wo->ouraddr == 0 && !disable_defaultip) {
1675 * Look up our hostname (possibly with domain name appended)
1676 * and take the first IP address as our local IP address.
1677 * If there isn't an IP address for our hostname, too bad.
1679 wo->accept_local = 1; /* don't insist on this default value */
1680 if ((hp = gethostbyname(hostname)) != NULL) {
1681 local = *(u_int32_t *)hp->h_addr;
1682 if (local != 0 && !bad_ip_adrs(local))
1683 wo->ouraddr = local;
1686 ask_for_local = wo->ouraddr != 0 || !disable_defaultip;
1691 * ip_demand_conf - configure the interface as though
1692 * IPCP were up, for use with dial-on-demand.
1695 ip_demand_conf(int u)
1697 ipcp_options *wo = &ipcp_wantoptions[u];
1699 if (wo->hisaddr == 0 && !noremoteip) {
1700 /* make up an arbitrary address for the peer */
1701 wo->hisaddr = htonl(0x0a707070 + ifunit);
1702 wo->accept_remote = 1;
1704 if (wo->ouraddr == 0) {
1705 /* make up an arbitrary address for us */
1706 wo->ouraddr = htonl(0x0a404040 + ifunit);
1707 wo->accept_local = 1;
1708 ask_for_local = 0; /* don't tell the peer this address */
1710 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1712 ipcp_script(_PATH_IPPREUP, 1);
1715 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1717 if (wo->default_route)
1718 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1719 default_route_set[u] = 1;
1721 if (sifproxyarp(u, wo->hisaddr))
1722 proxy_arp_set[u] = 1;
1724 notice("local IP address %I", wo->ouraddr);
1726 notice("remote IP address %I", wo->hisaddr);
1733 * ipcp_up - IPCP has come UP.
1735 * Configure the IP network interface appropriately and bring it up.
1741 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1742 ipcp_options *go = &ipcp_gotoptions[f->unit];
1743 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1745 IPCPDEBUG(("ipcp: up"));
1748 * We must have a non-zero IP address for both ends of the link.
1750 if (!ho->neg_addr && !ho->old_addrs)
1751 ho->hisaddr = wo->hisaddr;
1753 if (!(go->neg_addr || go->old_addrs) && (wo->neg_addr || wo->old_addrs)
1754 && wo->ouraddr != 0) {
1755 error("Peer refused to agree to our IP address");
1756 ipcp_close(f->unit, "Refused our IP address");
1759 if (go->ouraddr == 0) {
1760 error("Could not determine local IP address");
1761 ipcp_close(f->unit, "Could not determine local IP address");
1764 if (ho->hisaddr == 0 && !noremoteip) {
1765 ho->hisaddr = htonl(0x0a404040 + ifunit);
1766 warn("Could not determine remote IP address: defaulting to %I",
1769 script_setenv("IPLOCAL", ip_ntoa(go->ouraddr), 0);
1770 if (ho->hisaddr != 0)
1771 script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr), 1);
1778 script_setenv("DNS1", ip_ntoa(go->dnsaddr[0]), 0);
1780 script_setenv("DNS2", ip_ntoa(go->dnsaddr[1]), 0);
1781 if (usepeerdns && (go->dnsaddr[0] || go->dnsaddr[1])) {
1782 script_setenv("USEPEERDNS", "1", 0);
1783 create_resolv(go->dnsaddr[0], go->dnsaddr[1]);
1787 * Check that the peer is allowed to use the IP address it wants.
1789 if (ho->hisaddr != 0 && !auth_ip_addr(f->unit, ho->hisaddr)) {
1790 error("Peer is not authorized to use remote address %I", ho->hisaddr);
1791 ipcp_close(f->unit, "Unauthorized remote IP address");
1795 /* set tcp compression */
1796 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1799 * If we are doing dial-on-demand, the interface is already
1800 * configured, so we put out any saved-up packets, then set the
1801 * interface to pass IP packets.
1804 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1805 ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr);
1806 if (go->ouraddr != wo->ouraddr) {
1807 warn("Local IP address changed to %I", go->ouraddr);
1808 script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr), 0);
1809 wo->ouraddr = go->ouraddr;
1811 script_unsetenv("OLDIPLOCAL");
1812 if (ho->hisaddr != wo->hisaddr && wo->hisaddr != 0) {
1813 warn("Remote IP address changed to %I", ho->hisaddr);
1814 script_setenv("OLDIPREMOTE", ip_ntoa(wo->hisaddr), 0);
1815 wo->hisaddr = ho->hisaddr;
1817 script_unsetenv("OLDIPREMOTE");
1819 /* Set the interface to the new addresses */
1820 mask = GetMask(go->ouraddr);
1821 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1823 warn("Interface configuration failed");
1824 ipcp_close(f->unit, "Interface configuration failed");
1828 /* assign a default route through the interface if required */
1829 if (ipcp_wantoptions[f->unit].default_route)
1830 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1831 default_route_set[f->unit] = 1;
1833 /* Make a proxy ARP entry if requested. */
1834 if (ho->hisaddr != 0 && ipcp_wantoptions[f->unit].proxy_arp)
1835 if (sifproxyarp(f->unit, ho->hisaddr))
1836 proxy_arp_set[f->unit] = 1;
1839 demand_rexmit(PPP_IP);
1840 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1844 * Set IP addresses and (if specified) netmask.
1846 mask = GetMask(go->ouraddr);
1848 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1849 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1851 warn("Interface configuration failed");
1852 ipcp_close(f->unit, "Interface configuration failed");
1857 /* run the pre-up script, if any, and wait for it to finish */
1858 ipcp_script(_PATH_IPPREUP, 1);
1860 /* bring the interface up for IP */
1861 if (!sifup(f->unit)) {
1863 warn("Interface failed to come up");
1864 ipcp_close(f->unit, "Interface configuration failed");
1868 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1869 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1871 warn("Interface configuration failed");
1872 ipcp_close(f->unit, "Interface configuration failed");
1876 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1878 /* assign a default route through the interface if required */
1879 if (ipcp_wantoptions[f->unit].default_route)
1880 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1881 default_route_set[f->unit] = 1;
1883 /* Make a proxy ARP entry if requested. */
1884 if (ho->hisaddr != 0 && ipcp_wantoptions[f->unit].proxy_arp)
1885 if (sifproxyarp(f->unit, ho->hisaddr))
1886 proxy_arp_set[f->unit] = 1;
1888 ipcp_wantoptions[0].ouraddr = go->ouraddr;
1890 notice("local IP address %I", go->ouraddr);
1891 if (ho->hisaddr != 0)
1892 notice("remote IP address %I", ho->hisaddr);
1894 notice("primary DNS address %I", go->dnsaddr[0]);
1896 notice("secondary DNS address %I", go->dnsaddr[1]);
1899 reset_link_stats(f->unit);
1901 np_up(f->unit, PPP_IP);
1904 notify(ip_up_notifier, 0);
1909 * Execute the ip-up script, like this:
1910 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1912 if (ipcp_script_state == s_down && ipcp_script_pid == 0) {
1913 ipcp_script_state = s_up;
1914 ipcp_script(_PATH_IPUP, 0);
1920 * ipcp_down - IPCP has gone DOWN.
1922 * Take the IP network interface down, clear its addresses
1923 * and delete routes through it.
1928 IPCPDEBUG(("ipcp: down"));
1929 /* XXX a bit IPv4-centric here, we only need to get the stats
1930 * before the interface is marked down. */
1931 /* XXX more correct: we must get the stats before running the notifiers,
1932 * at least for the radius plugin */
1933 update_link_stats(f->unit);
1934 notify(ip_down_notifier, 0);
1939 np_down(f->unit, PPP_IP);
1941 sifvjcomp(f->unit, 0, 0, 0);
1943 print_link_stats(); /* _after_ running the notifiers and ip_down_hook(),
1944 * because print_link_stats() sets link_stats_valid
1948 * If we are doing dial-on-demand, set the interface
1949 * to queue up outgoing packets (for now).
1952 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1954 sifnpmode(f->unit, PPP_IP, NPMODE_DROP);
1956 ipcp_clear_addrs(f->unit, ipcp_gotoptions[f->unit].ouraddr,
1957 ipcp_hisoptions[f->unit].hisaddr);
1960 /* Execute the ip-down script */
1961 if (ipcp_script_state == s_up && ipcp_script_pid == 0) {
1962 ipcp_script_state = s_down;
1963 ipcp_script(_PATH_IPDOWN, 0);
1969 * ipcp_clear_addrs() - clear the interface addresses, routes,
1970 * proxy arp entries, etc.
1973 ipcp_clear_addrs(int unit, u_int32_t ouraddr, u_int32_t hisaddr)
1975 if (proxy_arp_set[unit]) {
1976 cifproxyarp(unit, hisaddr);
1977 proxy_arp_set[unit] = 0;
1979 if (default_route_set[unit]) {
1980 cifdefaultroute(unit, ouraddr, hisaddr);
1981 default_route_set[unit] = 0;
1983 cifaddr(unit, ouraddr, hisaddr);
1988 * ipcp_finished - possibly shut down the lower layers.
1991 ipcp_finished(fsm *f)
1995 np_finished(f->unit, PPP_IP);
2001 * ipcp_script_done - called when the ip-up or ip-down script
2005 ipcp_script_done(void *arg)
2007 ipcp_script_pid = 0;
2008 switch (ipcp_script_state) {
2010 if (ipcp_fsm[0].state != OPENED) {
2011 ipcp_script_state = s_down;
2012 ipcp_script(_PATH_IPDOWN, 0);
2016 if (ipcp_fsm[0].state == OPENED) {
2017 ipcp_script_state = s_up;
2018 ipcp_script(_PATH_IPUP, 0);
2026 * ipcp_script - Execute a script with arguments
2027 * interface-name tty-name speed local-IP remote-IP.
2030 ipcp_script(char *script, int wait)
2032 char strspeed[32], strlocal[32], strremote[32];
2035 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2036 slprintf(strlocal, sizeof(strlocal), "%I", ipcp_gotoptions[0].ouraddr);
2037 slprintf(strremote, sizeof(strremote), "%I", ipcp_hisoptions[0].hisaddr);
2044 argv[5] = strremote;
2048 run_program(script, argv, 0, NULL, NULL, 1);
2050 ipcp_script_pid = run_program(script, argv, 0, ipcp_script_done,
2055 * create_resolv - create the replacement resolv.conf file
2058 create_resolv(u_int32_t peerdns1, u_int32_t peerdns2)
2062 f = fopen(_PATH_RESOLV, "w");
2064 error("Failed to create %s: %m", _PATH_RESOLV);
2069 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns1));
2072 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns2));
2075 error("Write failed to %s: %m", _PATH_RESOLV);
2081 * ipcp_printpkt - print the contents of an IPCP packet.
2083 static char *ipcp_codenames[] = {
2084 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
2085 "TermReq", "TermAck", "CodeRej"
2089 ipcp_printpkt(u_char *p, int plen,
2090 void (*printer) (void *, char *, ...), void *arg)
2092 int code, id, len, olen;
2093 u_char *pstart, *optend;
2097 if (plen < HEADERLEN)
2103 if (len < HEADERLEN || len > plen)
2106 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
2107 printer(arg, " %s", ipcp_codenames[code-1]);
2109 printer(arg, " code=0x%x", code);
2110 printer(arg, " id=0x%x", id);
2117 /* print option list */
2122 if (olen < 2 || olen > len) {
2130 if (olen == CILEN_ADDRS) {
2133 printer(arg, "addrs %I", htonl(cilong));
2135 printer(arg, " %I", htonl(cilong));
2138 case CI_COMPRESSTYPE:
2139 if (olen >= CILEN_COMPRESS) {
2141 GETSHORT(cishort, p);
2142 printer(arg, "compress ");
2147 case IPCP_VJ_COMP_OLD:
2148 printer(arg, "old-VJ");
2151 printer(arg, "0x%x", cishort);
2156 if (olen == CILEN_ADDR) {
2159 printer(arg, "addr %I", htonl(cilong));
2166 printer(arg, "ms-dns%d %I", (code == CI_MS_DNS1? 1: 2),
2173 printer(arg, "ms-wins %I", htonl(cilong));
2176 while (p < optend) {
2178 printer(arg, " %.2x", code);
2186 if (len > 0 && *p >= ' ' && *p < 0x7f) {
2188 print_string((char *)p, len, printer, arg);
2195 /* print the rest of the bytes in the packet */
2196 for (; len > 0; --len) {
2198 printer(arg, " %.2x", code);
2205 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
2206 * We don't bring the link up for IP fragments or for TCP FIN packets
2209 #define IP_HDRLEN 20 /* bytes */
2210 #define IP_OFFMASK 0x1fff
2212 #define IPPROTO_TCP 6
2214 #define TCP_HDRLEN 20
2218 * We use these macros because the IP header may be at an odd address,
2219 * and some compilers might use word loads to get th_off or ip_hl.
2222 #define net_short(x) (((x)[0] << 8) + (x)[1])
2223 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
2224 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
2225 #define get_ipproto(x) (((unsigned char *)(x))[9])
2226 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
2227 #define get_tcpflags(x) (((unsigned char *)(x))[13])
2230 ip_active_pkt(u_char *pkt, int len)
2237 if (len < IP_HDRLEN)
2239 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
2241 if (get_ipproto(pkt) != IPPROTO_TCP)
2243 hlen = get_iphl(pkt) * 4;
2244 if (len < hlen + TCP_HDRLEN)
2247 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)