2 * ipcp.c - PPP IP Control Protocol.
4 * Copyright (c) 1984-2000 Carnegie Mellon University. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * 3. The name "Carnegie Mellon University" must not be used to
19 * endorse or promote products derived from this software without
20 * prior written permission. For permission or any legal
21 * details, please contact
22 * Office of Technology Transfer
23 * Carnegie Mellon University
25 * Pittsburgh, PA 15213-3890
26 * (412) 268-4387, fax: (412) 268-7395
27 * tech-transfer@andrew.cmu.edu
29 * 4. Redistributions of any form whatsoever must retain the following
31 * "This product includes software developed by Computing Services
32 * at Carnegie Mellon University (http://www.cmu.edu/computing/)."
34 * CARNEGIE MELLON UNIVERSITY DISCLAIMS ALL WARRANTIES WITH REGARD TO
35 * THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
36 * AND FITNESS, IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY BE LIABLE
37 * FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
38 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
39 * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
40 * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
51 #include <sys/param.h>
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <netinet/in.h>
55 #include <arpa/inet.h>
61 #include "pathnames.h"
65 ipcp_options ipcp_wantoptions[NUM_PPP]; /* Options that we want to request */
66 ipcp_options ipcp_gotoptions[NUM_PPP]; /* Options that peer ack'd */
67 ipcp_options ipcp_allowoptions[NUM_PPP]; /* Options we allow peer to request */
68 ipcp_options ipcp_hisoptions[NUM_PPP]; /* Options that we ack'd */
70 u_int32_t netmask = 0; /* IP netmask to set on interface */
72 bool disable_defaultip = 0; /* Don't use hostname for default IP adrs */
73 bool noremoteip = 0; /* Let him have no IP address */
75 /* Hook for a plugin to know when IP protocol has come up */
76 void (*ip_up_hook)(void) = NULL;
78 /* Hook for a plugin to know when IP protocol has come down */
79 void (*ip_down_hook)(void) = NULL;
81 /* Hook for a plugin to choose the remote IP address */
82 void (*ip_choose_hook)(u_int32_t *) = NULL;
84 /* Notifiers for when IPCP goes up and down */
85 struct notifier *ip_up_notifier = NULL;
86 struct notifier *ip_down_notifier = NULL;
89 static int default_route_set[NUM_PPP]; /* Have set up a default route */
90 static int proxy_arp_set[NUM_PPP]; /* Have created proxy arp entry */
91 static bool usepeerdns; /* Ask peer for DNS addrs */
92 static int ipcp_is_up; /* have called np_up() */
93 static int ipcp_is_open; /* haven't called np_finished() */
94 static bool ask_for_local; /* request our address from peer */
95 static char vj_value[8]; /* string form of vj option value */
96 static char netmask_str[20]; /* string form of netmask value */
99 * Callbacks for fsm code. (CI = Configuration Information)
101 static void ipcp_resetci (fsm *); /* Reset our CI */
102 static int ipcp_cilen (fsm *); /* Return length of our CI */
103 static void ipcp_addci (fsm *, u_char *, int *); /* Add our CI */
104 static int ipcp_ackci (fsm *, u_char *, int); /* Peer ack'd our CI */
105 static int ipcp_nakci (fsm *, u_char *, int, int);/* Peer nak'd our CI */
106 static int ipcp_rejci (fsm *, u_char *, int); /* Peer rej'd our CI */
107 static int ipcp_reqci (fsm *, u_char *, int *, int); /* Rcv CI */
108 static void ipcp_up (fsm *); /* We're UP */
109 static void ipcp_down (fsm *); /* We're DOWN */
110 static void ipcp_finished (fsm *); /* Don't need lower layer */
112 fsm ipcp_fsm[NUM_PPP]; /* IPCP fsm structure */
114 static fsm_callbacks ipcp_callbacks = { /* IPCP callback routines */
115 ipcp_resetci, /* Reset our Configuration Information */
116 ipcp_cilen, /* Length of our Configuration Information */
117 ipcp_addci, /* Add our Configuration Information */
118 ipcp_ackci, /* ACK our Configuration Information */
119 ipcp_nakci, /* NAK our Configuration Information */
120 ipcp_rejci, /* Reject our Configuration Information */
121 ipcp_reqci, /* Request peer's Configuration Information */
122 ipcp_up, /* Called when fsm reaches OPENED state */
123 ipcp_down, /* Called when fsm leaves OPENED state */
124 NULL, /* Called when we want the lower layer up */
125 ipcp_finished, /* Called when we want the lower layer down */
126 NULL, /* Called when Protocol-Reject received */
127 NULL, /* Retransmission is necessary */
128 NULL, /* Called to handle protocol-specific codes */
129 "IPCP" /* String name of protocol */
133 * Command-line options.
135 static int setvjslots (char **);
136 static int setdnsaddr (char **);
137 static int setwinsaddr (char **);
138 static int setnetmask (char **);
139 int setipaddr (char *, char **, int);
140 static void printipaddr (option_t *, void (*)(void *, char *,...),void *);
142 static option_t ipcp_option_list[] = {
143 { "noip", o_bool, &ipcp_protent.enabled_flag,
144 "Disable IP and IPCP" },
145 { "-ip", o_bool, &ipcp_protent.enabled_flag,
146 "Disable IP and IPCP", OPT_ALIAS },
148 { "novj", o_bool, &ipcp_wantoptions[0].neg_vj,
149 "Disable VJ compression", OPT_A2CLR, &ipcp_allowoptions[0].neg_vj },
150 { "-vj", o_bool, &ipcp_wantoptions[0].neg_vj,
151 "Disable VJ compression", OPT_ALIAS | OPT_A2CLR,
152 &ipcp_allowoptions[0].neg_vj },
154 { "novjccomp", o_bool, &ipcp_wantoptions[0].cflag,
155 "Disable VJ connection-ID compression", OPT_A2CLR,
156 &ipcp_allowoptions[0].cflag },
157 { "-vjccomp", o_bool, &ipcp_wantoptions[0].cflag,
158 "Disable VJ connection-ID compression", OPT_ALIAS | OPT_A2CLR,
159 &ipcp_allowoptions[0].cflag },
161 { "vj-max-slots", o_special, (void *)setvjslots,
162 "Set maximum VJ header slots",
163 OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, vj_value },
165 { "ipcp-accept-local", o_bool, &ipcp_wantoptions[0].accept_local,
166 "Accept peer's address for us", 1 },
167 { "ipcp-accept-remote", o_bool, &ipcp_wantoptions[0].accept_remote,
168 "Accept peer's address for it", 1 },
170 { "ipparam", o_string, &ipparam,
171 "Set ip script parameter", OPT_PRIO },
173 { "noipdefault", o_bool, &disable_defaultip,
174 "Don't use name for default IP adrs", 1 },
176 { "ms-dns", o_special, (void *)setdnsaddr,
177 "DNS address for the peer's use", OPT_A2LIST },
178 { "ms-wins", o_special, (void *)setwinsaddr,
179 "Nameserver for SMB over TCP/IP for peer", OPT_A2LIST },
181 { "ipcp-restart", o_int, &ipcp_fsm[0].timeouttime,
182 "Set timeout for IPCP", OPT_PRIO },
183 { "ipcp-max-terminate", o_int, &ipcp_fsm[0].maxtermtransmits,
184 "Set max #xmits for term-reqs", OPT_PRIO },
185 { "ipcp-max-configure", o_int, &ipcp_fsm[0].maxconfreqtransmits,
186 "Set max #xmits for conf-reqs", OPT_PRIO },
187 { "ipcp-max-failure", o_int, &ipcp_fsm[0].maxnakloops,
188 "Set max #conf-naks for IPCP", OPT_PRIO },
190 { "defaultroute", o_bool, &ipcp_wantoptions[0].default_route,
191 "Add default route", OPT_ENABLE|1, &ipcp_allowoptions[0].default_route },
192 { "nodefaultroute", o_bool, &ipcp_allowoptions[0].default_route,
193 "disable defaultroute option", OPT_A2CLR,
194 &ipcp_wantoptions[0].default_route },
195 { "-defaultroute", o_bool, &ipcp_allowoptions[0].default_route,
196 "disable defaultroute option", OPT_ALIAS | OPT_A2CLR,
197 &ipcp_wantoptions[0].default_route },
199 { "proxyarp", o_bool, &ipcp_wantoptions[0].proxy_arp,
200 "Add proxy ARP entry", OPT_ENABLE|1, &ipcp_allowoptions[0].proxy_arp },
201 { "noproxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
202 "disable proxyarp option", OPT_A2CLR,
203 &ipcp_wantoptions[0].proxy_arp },
204 { "-proxyarp", o_bool, &ipcp_allowoptions[0].proxy_arp,
205 "disable proxyarp option", OPT_ALIAS | OPT_A2CLR,
206 &ipcp_wantoptions[0].proxy_arp },
208 { "usepeerdns", o_bool, &usepeerdns,
209 "Ask peer for DNS address(es)", 1 },
211 { "netmask", o_special, (void *)setnetmask,
212 "set netmask", OPT_PRIO | OPT_A2STRVAL | OPT_STATIC, netmask_str },
214 { "ipcp-no-addresses", o_bool, &ipcp_wantoptions[0].old_addrs,
215 "Disable old-style IP-Addresses usage", OPT_A2CLR,
216 &ipcp_allowoptions[0].old_addrs },
217 { "ipcp-no-address", o_bool, &ipcp_wantoptions[0].neg_addr,
218 "Disable IP-Address usage", OPT_A2CLR,
219 &ipcp_allowoptions[0].neg_addr },
221 { "noremoteip", o_bool, &noremoteip,
222 "Allow peer to have no IP address", 1 },
224 { "nosendip", o_bool, &ipcp_wantoptions[0].neg_addr,
225 "Don't send our IP address to peer", OPT_A2CLR,
226 &ipcp_wantoptions[0].old_addrs},
228 { "IP addresses", o_wild, (void *) &setipaddr,
229 "set local and remote IP addresses",
230 OPT_NOARG | OPT_A2PRINTER, (void *) &printipaddr },
236 * Protocol entry points from main code.
238 static void ipcp_init (int);
239 static void ipcp_open (int);
240 static void ipcp_close (int, char *);
241 static void ipcp_lowerup (int);
242 static void ipcp_lowerdown (int);
243 static void ipcp_input (int, u_char *, int);
244 static void ipcp_protrej (int);
245 static int ipcp_printpkt (u_char *, int,
246 void (*) (void *, char *, ...), void *);
247 static void ip_check_options (void);
248 static int ip_demand_conf (int);
249 static int ip_active_pkt (u_char *, int);
250 static void create_resolv (u_int32_t, u_int32_t);
252 struct protent ipcp_protent = {
272 static void ipcp_clear_addrs (int, u_int32_t, u_int32_t);
273 static void ipcp_script (char *, int); /* Run an up/down script */
274 static void ipcp_script_done (void *);
277 * Lengths of configuration options.
280 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
281 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
282 #define CILEN_ADDR 6 /* new-style single address option */
283 #define CILEN_ADDRS 10 /* old-style dual address option */
286 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
287 (x) == CONFNAK ? "NAK" : "REJ")
290 * This state variable is used to ensure that we don't
291 * run an ipcp-up/down script while one is already running.
293 static enum script_state {
297 static pid_t ipcp_script_pid;
300 * Make a string representation of a network IP address.
303 ip_ntoa(u_int32_t ipaddr)
307 slprintf(b, sizeof(b), "%I", ipaddr);
316 * setvjslots - set maximum number of connection slots for VJ compression
319 setvjslots(char **argv)
323 if (!int_option(*argv, &value))
325 if (value < 2 || value > 16) {
326 option_error("vj-max-slots value must be between 2 and 16");
329 ipcp_wantoptions [0].maxslotindex =
330 ipcp_allowoptions[0].maxslotindex = value - 1;
331 slprintf(vj_value, sizeof(vj_value), "%d", value);
336 * setdnsaddr - set the dns address(es)
339 setdnsaddr(char **argv)
344 dns = inet_addr(*argv);
345 if (dns == (u_int32_t) -1) {
346 if ((hp = gethostbyname(*argv)) == NULL) {
347 option_error("invalid address parameter '%s' for ms-dns option",
351 dns = *(u_int32_t *)hp->h_addr;
354 /* We take the last 2 values given, the 2nd-last as the primary
355 and the last as the secondary. If only one is given it
356 becomes both primary and secondary. */
357 if (ipcp_allowoptions[0].dnsaddr[1] == 0)
358 ipcp_allowoptions[0].dnsaddr[0] = dns;
360 ipcp_allowoptions[0].dnsaddr[0] = ipcp_allowoptions[0].dnsaddr[1];
362 /* always set the secondary address value. */
363 ipcp_allowoptions[0].dnsaddr[1] = dns;
369 * setwinsaddr - set the wins address(es)
370 * This is primrarly used with the Samba package under UNIX or for pointing
371 * the caller to the existing WINS server on a Windows NT platform.
374 setwinsaddr(char **argv)
379 wins = inet_addr(*argv);
380 if (wins == (u_int32_t) -1) {
381 if ((hp = gethostbyname(*argv)) == NULL) {
382 option_error("invalid address parameter '%s' for ms-wins option",
386 wins = *(u_int32_t *)hp->h_addr;
389 /* We take the last 2 values given, the 2nd-last as the primary
390 and the last as the secondary. If only one is given it
391 becomes both primary and secondary. */
392 if (ipcp_allowoptions[0].winsaddr[1] == 0)
393 ipcp_allowoptions[0].winsaddr[0] = wins;
395 ipcp_allowoptions[0].winsaddr[0] = ipcp_allowoptions[0].winsaddr[1];
397 /* always set the secondary address value. */
398 ipcp_allowoptions[0].winsaddr[1] = wins;
404 * setipaddr - Set the IP address
405 * If doit is 0, the call is to check whether this option is
406 * potentially an IP address specification.
407 * Not static so that plugins can call it to set the addresses
410 setipaddr(char *arg, char **argv, int doit)
414 u_int32_t local, remote;
415 ipcp_options *wo = &ipcp_wantoptions[0];
416 static int prio_local = 0, prio_remote = 0;
419 * IP address pair separated by ":".
421 if ((colon = strchr(arg, ':')) == NULL)
427 * If colon first character, then no local addr.
429 if (colon != arg && option_priority >= prio_local) {
431 if ((local = inet_addr(arg)) == (u_int32_t) -1) {
432 if ((hp = gethostbyname(arg)) == NULL) {
433 option_error("unknown host: %s", arg);
436 local = *(u_int32_t *)hp->h_addr;
438 if (bad_ip_adrs(local)) {
439 option_error("bad local IP address %s", ip_ntoa(local));
445 prio_local = option_priority;
449 * If colon last character, then no remote addr.
451 if (*++colon != '\0' && option_priority >= prio_remote) {
452 if ((remote = inet_addr(colon)) == (u_int32_t) -1) {
453 if ((hp = gethostbyname(colon)) == NULL) {
454 option_error("unknown host: %s", colon);
457 remote = *(u_int32_t *)hp->h_addr;
458 if (remote_name[0] == 0)
459 strlcpy(remote_name, colon, sizeof(remote_name));
461 if (bad_ip_adrs(remote)) {
462 option_error("bad remote IP address %s", ip_ntoa(remote));
466 wo->hisaddr = remote;
467 prio_remote = option_priority;
474 printipaddr(option_t *opt, void (*printer) (void *, char *, ...), void *arg)
476 ipcp_options *wo = &ipcp_wantoptions[0];
478 if (wo->ouraddr != 0)
479 printer(arg, "%I", wo->ouraddr);
481 if (wo->hisaddr != 0)
482 printer(arg, "%I", wo->hisaddr);
486 * setnetmask - set the netmask to be used on the interface.
489 setnetmask(char **argv)
496 * Unfortunately, if we use inet_addr, we can't tell whether
497 * a result of all 1s is an error or a valid 255.255.255.255.
500 n = parse_dotted_ip(p, &mask);
504 if (n == 0 || p[n] != 0 || (netmask & ~mask) != 0) {
505 option_error("invalid netmask value '%s'", *argv);
510 slprintf(netmask_str, sizeof(netmask_str), "%I", mask);
516 parse_dotted_ip(char *p, u_int32_t *vp)
524 b = strtoul(p, &endp, 0);
530 /* accept e.g. 0xffffff00 */
548 * ipcp_init - Initialize IPCP.
553 fsm *f = &ipcp_fsm[unit];
554 ipcp_options *wo = &ipcp_wantoptions[unit];
555 ipcp_options *ao = &ipcp_allowoptions[unit];
558 f->protocol = PPP_IPCP;
559 f->callbacks = &ipcp_callbacks;
560 fsm_init(&ipcp_fsm[unit]);
563 * Some 3G modems use repeated IPCP NAKs as a way of stalling
564 * until they can contact a server on the network, so we increase
565 * the default number of NAKs we accept before we start treating
568 f->maxnakloops = 100;
570 memset(wo, 0, sizeof(*wo));
571 memset(ao, 0, sizeof(*ao));
573 wo->neg_addr = wo->old_addrs = 1;
575 wo->vj_protocol = IPCP_VJ_COMP;
576 wo->maxslotindex = MAX_STATES - 1; /* really max index */
580 /* max slots and slot-id compression are currently hardwired in */
581 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
584 ao->neg_addr = ao->old_addrs = 1;
586 ao->maxslotindex = MAX_STATES - 1;
590 * XXX These control whether the user may use the proxyarp
591 * and defaultroute options.
594 ao->default_route = 1;
599 * ipcp_open - IPCP is allowed to come up.
604 fsm_open(&ipcp_fsm[unit]);
610 * ipcp_close - Take IPCP down.
613 ipcp_close(int unit, char *reason)
615 fsm_close(&ipcp_fsm[unit], reason);
620 * ipcp_lowerup - The lower layer is up.
623 ipcp_lowerup(int unit)
625 fsm_lowerup(&ipcp_fsm[unit]);
630 * ipcp_lowerdown - The lower layer is down.
633 ipcp_lowerdown(int unit)
635 fsm_lowerdown(&ipcp_fsm[unit]);
640 * ipcp_input - Input IPCP packet.
643 ipcp_input(int unit, u_char *p, int len)
645 fsm_input(&ipcp_fsm[unit], p, len);
650 * ipcp_protrej - A Protocol-Reject was received for IPCP.
652 * Pretend the lower layer went down, so we shut up.
655 ipcp_protrej(int unit)
657 fsm_lowerdown(&ipcp_fsm[unit]);
662 * ipcp_resetci - Reset our CI.
663 * Called by fsm_sconfreq, Send Configure Request.
668 ipcp_options *wo = &ipcp_wantoptions[f->unit];
669 ipcp_options *go = &ipcp_gotoptions[f->unit];
670 ipcp_options *ao = &ipcp_allowoptions[f->unit];
672 wo->req_addr = (wo->neg_addr || wo->old_addrs) &&
673 (ao->neg_addr || ao->old_addrs);
674 if (wo->ouraddr == 0)
675 wo->accept_local = 1;
676 if (wo->hisaddr == 0)
677 wo->accept_remote = 1;
678 wo->req_dns1 = usepeerdns; /* Request DNS addresses from the peer */
679 wo->req_dns2 = usepeerdns;
683 if (ip_choose_hook) {
684 ip_choose_hook(&wo->hisaddr);
686 wo->accept_remote = 0;
689 BZERO(&ipcp_hisoptions[f->unit], sizeof(ipcp_options));
694 * ipcp_cilen - Return length of our CI.
695 * Called by fsm_sconfreq, Send Configure Request.
700 ipcp_options *go = &ipcp_gotoptions[f->unit];
701 ipcp_options *wo = &ipcp_wantoptions[f->unit];
702 ipcp_options *ho = &ipcp_hisoptions[f->unit];
704 #define LENCIADDRS(neg) (neg ? CILEN_ADDRS : 0)
705 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
706 #define LENCIADDR(neg) (neg ? CILEN_ADDR : 0)
707 #define LENCIDNS(neg) LENCIADDR(neg)
708 #define LENCIWINS(neg) LENCIADDR(neg)
711 * First see if we want to change our options to the old
712 * forms because we have received old forms from the peer.
714 if (go->neg_addr && go->old_addrs && !ho->neg_addr && ho->old_addrs)
716 if (wo->neg_vj && !go->neg_vj && !go->old_vj) {
717 /* try an older style of VJ negotiation */
718 /* use the old style only if the peer did */
719 if (ho->neg_vj && ho->old_vj) {
722 go->vj_protocol = ho->vj_protocol;
726 return (LENCIADDRS(!go->neg_addr && go->old_addrs) +
727 LENCIVJ(go->neg_vj, go->old_vj) +
728 LENCIADDR(go->neg_addr) +
729 LENCIDNS(go->req_dns1) +
730 LENCIDNS(go->req_dns2) +
731 LENCIWINS(go->winsaddr[0]) +
732 LENCIWINS(go->winsaddr[1])) ;
737 * ipcp_addci - Add our desired CIs to a packet.
738 * Called by fsm_sconfreq, Send Configure Request.
741 ipcp_addci(fsm *f, u_char *ucp, int *lenp)
743 ipcp_options *go = &ipcp_gotoptions[f->unit];
746 #define ADDCIADDRS(opt, neg, val1, val2) \
748 if (len >= CILEN_ADDRS) { \
751 PUTCHAR(CILEN_ADDRS, ucp); \
756 len -= CILEN_ADDRS; \
761 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
763 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
764 if (len >= vjlen) { \
766 PUTCHAR(vjlen, ucp); \
767 PUTSHORT(val, ucp); \
769 PUTCHAR(maxslotindex, ucp); \
770 PUTCHAR(cflag, ucp); \
777 #define ADDCIADDR(opt, neg, val) \
779 if (len >= CILEN_ADDR) { \
782 PUTCHAR(CILEN_ADDR, ucp); \
790 #define ADDCIDNS(opt, neg, addr) \
792 if (len >= CILEN_ADDR) { \
795 PUTCHAR(CILEN_ADDR, ucp); \
803 #define ADDCIWINS(opt, addr) \
805 if (len >= CILEN_ADDR) { \
808 PUTCHAR(CILEN_ADDR, ucp); \
816 ADDCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs, go->ouraddr,
819 ADDCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
820 go->maxslotindex, go->cflag);
822 ADDCIADDR(CI_ADDR, go->neg_addr, go->ouraddr);
824 ADDCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
826 ADDCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
828 ADDCIWINS(CI_MS_WINS1, go->winsaddr[0]);
830 ADDCIWINS(CI_MS_WINS2, go->winsaddr[1]);
837 * ipcp_ackci - Ack our CIs.
838 * Called by fsm_rconfack, Receive Configure ACK.
845 ipcp_ackci(fsm *f, u_char *p, int len)
847 ipcp_options *go = &ipcp_gotoptions[f->unit];
848 u_short cilen, citype, cishort;
850 u_char cimaxslotindex, cicflag;
853 * CIs must be in exactly the same order that we sent...
854 * Check packet length and CI length at each step.
855 * If we find any deviations, then this packet is bad.
858 #define ACKCIADDRS(opt, neg, val1, val2) \
861 if ((len -= CILEN_ADDRS) < 0) \
863 GETCHAR(citype, p); \
865 if (cilen != CILEN_ADDRS || \
870 if (val1 != cilong) \
874 if (val2 != cilong) \
878 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
880 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
881 if ((len -= vjlen) < 0) \
883 GETCHAR(citype, p); \
885 if (cilen != vjlen || \
888 GETSHORT(cishort, p); \
889 if (cishort != val) \
892 GETCHAR(cimaxslotindex, p); \
893 if (cimaxslotindex != maxslotindex) \
895 GETCHAR(cicflag, p); \
896 if (cicflag != cflag) \
901 #define ACKCIADDR(opt, neg, val) \
904 if ((len -= CILEN_ADDR) < 0) \
906 GETCHAR(citype, p); \
908 if (cilen != CILEN_ADDR || \
917 #define ACKCIDNS(opt, neg, addr) \
920 if ((len -= CILEN_ADDR) < 0) \
922 GETCHAR(citype, p); \
924 if (cilen != CILEN_ADDR || citype != opt) \
928 if (addr != cilong) \
932 #define ACKCIWINS(opt, addr) \
935 if ((len -= CILEN_ADDR) < 0) \
937 GETCHAR(citype, p); \
939 if (cilen != CILEN_ADDR || citype != opt) \
943 if (addr != cilong) \
947 ACKCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs, go->ouraddr,
950 ACKCIVJ(CI_COMPRESSTYPE, go->neg_vj, go->vj_protocol, go->old_vj,
951 go->maxslotindex, go->cflag);
953 ACKCIADDR(CI_ADDR, go->neg_addr, go->ouraddr);
955 ACKCIDNS(CI_MS_DNS1, go->req_dns1, go->dnsaddr[0]);
957 ACKCIDNS(CI_MS_DNS2, go->req_dns2, go->dnsaddr[1]);
959 ACKCIWINS(CI_MS_WINS1, go->winsaddr[0]);
961 ACKCIWINS(CI_MS_WINS2, go->winsaddr[1]);
964 * If there are any remaining CIs, then this packet is bad.
971 IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
976 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
977 * This should not modify any state if the Nak is bad
978 * or if IPCP is in the OPENED state.
979 * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
986 ipcp_nakci(fsm *f, u_char *p, int len, int treat_as_reject)
988 ipcp_options *go = &ipcp_gotoptions[f->unit];
989 u_char cimaxslotindex, cicflag;
990 u_char citype, cilen, *next;
992 u_int32_t ciaddr1, ciaddr2, l, cidnsaddr;
993 ipcp_options no; /* options we've seen Naks for */
994 ipcp_options try; /* options to request next time */
996 BZERO(&no, sizeof(no));
1000 * Any Nak'd CIs must be in exactly the same order that we sent.
1001 * Check packet length and CI length at each step.
1002 * If we find any deviations, then this packet is bad.
1004 #define NAKCIADDRS(opt, neg, code) \
1006 (cilen = p[1]) == CILEN_ADDRS && \
1012 ciaddr1 = htonl(l); \
1014 ciaddr2 = htonl(l); \
1019 #define NAKCIVJ(opt, neg, code) \
1021 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
1026 GETSHORT(cishort, p); \
1031 #define NAKCIADDR(opt, neg, code) \
1033 (cilen = p[1]) == CILEN_ADDR && \
1039 ciaddr1 = htonl(l); \
1044 #define NAKCIDNS(opt, neg, code) \
1046 ((cilen = p[1]) == CILEN_ADDR) && \
1052 cidnsaddr = htonl(l); \
1058 * Accept the peer's idea of {our,his} address, if different
1059 * from our idea, only if the accept_{local,remote} flag is set.
1061 NAKCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs,
1062 if (treat_as_reject) {
1065 if (go->accept_local && ciaddr1) {
1066 /* take his idea of our address */
1067 try.ouraddr = ciaddr1;
1069 if (go->accept_remote && ciaddr2) {
1070 /* take his idea of his address */
1071 try.hisaddr = ciaddr2;
1077 * Accept the peer's value of maxslotindex provided that it
1078 * is less than what we asked for. Turn off slot-ID compression
1079 * if the peer wants. Send old-style compress-type option if
1082 NAKCIVJ(CI_COMPRESSTYPE, neg_vj,
1083 if (treat_as_reject) {
1085 } else if (cilen == CILEN_VJ) {
1086 GETCHAR(cimaxslotindex, p);
1087 GETCHAR(cicflag, p);
1088 if (cishort == IPCP_VJ_COMP) {
1090 if (cimaxslotindex < go->maxslotindex)
1091 try.maxslotindex = cimaxslotindex;
1098 if (cishort == IPCP_VJ_COMP || cishort == IPCP_VJ_COMP_OLD) {
1100 try.vj_protocol = cishort;
1107 NAKCIADDR(CI_ADDR, neg_addr,
1108 if (treat_as_reject) {
1111 } else if (go->accept_local && ciaddr1) {
1112 /* take his idea of our address */
1113 try.ouraddr = ciaddr1;
1117 NAKCIDNS(CI_MS_DNS1, req_dns1,
1118 if (treat_as_reject) {
1121 try.dnsaddr[0] = cidnsaddr;
1125 NAKCIDNS(CI_MS_DNS2, req_dns2,
1126 if (treat_as_reject) {
1129 try.dnsaddr[1] = cidnsaddr;
1134 * There may be remaining CIs, if the peer is requesting negotiation
1135 * on an option that we didn't include in our request packet.
1136 * If they want to negotiate about IP addresses, we comply.
1137 * If they want us to ask for compression, we refuse.
1138 * If they want us to ask for ms-dns, we do that, since some
1139 * peers get huffy if we don't.
1141 while (len >= CILEN_VOID) {
1144 if ( cilen < CILEN_VOID || (len -= cilen) < 0 )
1146 next = p + cilen - 2;
1149 case CI_COMPRESSTYPE:
1150 if (go->neg_vj || no.neg_vj ||
1151 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS))
1156 if ((!go->neg_addr && go->old_addrs) || no.old_addrs
1157 || cilen != CILEN_ADDRS)
1162 if (ciaddr1 && go->accept_local)
1163 try.ouraddr = ciaddr1;
1166 if (ciaddr2 && go->accept_remote)
1167 try.hisaddr = ciaddr2;
1171 if (go->neg_addr || no.neg_addr || cilen != CILEN_ADDR)
1176 if (ciaddr1 && go->accept_local)
1177 try.ouraddr = ciaddr1;
1178 if (try.ouraddr != 0)
1183 if (go->req_dns1 || no.req_dns1 || cilen != CILEN_ADDR)
1186 try.dnsaddr[0] = htonl(l);
1191 if (go->req_dns2 || no.req_dns2 || cilen != CILEN_ADDR)
1194 try.dnsaddr[1] = htonl(l);
1200 if (cilen != CILEN_ADDR)
1205 try.winsaddr[citype == CI_MS_WINS2] = ciaddr1;
1212 * OK, the Nak is good. Now we can update state.
1213 * If there are any remaining options, we ignore them.
1215 if (f->state != OPENED)
1221 IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
1227 * ipcp_rejci - Reject some of our CIs.
1228 * Callback from fsm_rconfnakrej.
1231 ipcp_rejci(fsm *f, u_char *p, int len)
1233 ipcp_options *go = &ipcp_gotoptions[f->unit];
1234 u_char cimaxslotindex, ciflag, cilen;
1237 ipcp_options try; /* options to request next time */
1241 * Any Rejected CIs must be in exactly the same order that we sent.
1242 * Check packet length and CI length at each step.
1243 * If we find any deviations, then this packet is bad.
1245 #define REJCIADDRS(opt, neg, val1, val2) \
1247 (cilen = p[1]) == CILEN_ADDRS && \
1254 cilong = htonl(l); \
1255 /* Check rejected value. */ \
1256 if (cilong != val1) \
1259 cilong = htonl(l); \
1260 /* Check rejected value. */ \
1261 if (cilong != val2) \
1263 try.old_addrs = 0; \
1266 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
1268 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
1273 GETSHORT(cishort, p); \
1274 /* Check rejected value. */ \
1275 if (cishort != val) \
1278 GETCHAR(cimaxslotindex, p); \
1279 if (cimaxslotindex != maxslot) \
1281 GETCHAR(ciflag, p); \
1282 if (ciflag != cflag) \
1288 #define REJCIADDR(opt, neg, val) \
1290 (cilen = p[1]) == CILEN_ADDR && \
1297 cilong = htonl(l); \
1298 /* Check rejected value. */ \
1299 if (cilong != val) \
1304 #define REJCIDNS(opt, neg, dnsaddr) \
1306 ((cilen = p[1]) == CILEN_ADDR) && \
1313 cilong = htonl(l); \
1314 /* Check rejected value. */ \
1315 if (cilong != dnsaddr) \
1320 #define REJCIWINS(opt, addr) \
1322 ((cilen = p[1]) == CILEN_ADDR) && \
1329 cilong = htonl(l); \
1330 /* Check rejected value. */ \
1331 if (cilong != addr) \
1333 try.winsaddr[opt == CI_MS_WINS2] = 0; \
1336 REJCIADDRS(CI_ADDRS, !go->neg_addr && go->old_addrs,
1337 go->ouraddr, go->hisaddr);
1339 REJCIVJ(CI_COMPRESSTYPE, neg_vj, go->vj_protocol, go->old_vj,
1340 go->maxslotindex, go->cflag);
1342 REJCIADDR(CI_ADDR, neg_addr, go->ouraddr);
1344 REJCIDNS(CI_MS_DNS1, req_dns1, go->dnsaddr[0]);
1346 REJCIDNS(CI_MS_DNS2, req_dns2, go->dnsaddr[1]);
1348 REJCIWINS(CI_MS_WINS1, go->winsaddr[0]);
1350 REJCIWINS(CI_MS_WINS2, go->winsaddr[1]);
1353 * If there are any remaining CIs, then this packet is bad.
1358 * Now we can update state.
1360 if (f->state != OPENED)
1365 IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
1371 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
1372 * Callback from fsm_rconfreq, Receive Configure Request
1374 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1375 * appropriately. If reject_if_disagree is non-zero, doesn't return
1376 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1379 ipcp_reqci(fsm *f, u_char *inp, int *len, int reject_if_disagree)
1381 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1382 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1383 ipcp_options *ao = &ipcp_allowoptions[f->unit];
1384 u_char *cip, *next; /* Pointer to current and next CIs */
1385 u_short cilen, citype; /* Parsed len, type */
1386 u_short cishort; /* Parsed short value */
1387 u_int32_t tl, ciaddr1, ciaddr2;/* Parsed address values */
1388 int rc = CONFACK; /* Final packet return code */
1389 int orc; /* Individual option return code */
1390 u_char *p; /* Pointer to next char to parse */
1391 u_char *ucp = inp; /* Pointer to current output char */
1392 int l = *len; /* Length left */
1393 u_char maxslotindex, cflag;
1397 * Reset all his options.
1399 BZERO(ho, sizeof(*ho));
1402 * Process all his options.
1406 orc = CONFACK; /* Assume success */
1407 cip = p = next; /* Remember begining of CI */
1408 if (l < 2 || /* Not enough data for CI header or */
1409 p[1] < 2 || /* CI length too small or */
1410 p[1] > l) { /* CI length too big? */
1411 IPCPDEBUG(("ipcp_reqci: bad CI length!"));
1412 orc = CONFREJ; /* Reject bad CI */
1413 cilen = l; /* Reject till end of packet */
1414 l = 0; /* Don't loop again */
1417 GETCHAR(citype, p); /* Parse CI type */
1418 GETCHAR(cilen, p); /* Parse CI length */
1419 l -= cilen; /* Adjust remaining length */
1420 next += cilen; /* Step to next CI */
1422 switch (citype) { /* Check CI type */
1424 if (!ao->old_addrs || ho->neg_addr ||
1425 cilen != CILEN_ADDRS) { /* Check CI length */
1426 orc = CONFREJ; /* Reject CI */
1431 * If he has no address, or if we both have his address but
1432 * disagree about it, then NAK it with our idea.
1433 * In particular, if we don't know his address, but he does,
1436 GETLONG(tl, p); /* Parse source address (his) */
1437 ciaddr1 = htonl(tl);
1438 if (ciaddr1 != wo->hisaddr
1439 && (ciaddr1 == 0 || !wo->accept_remote)) {
1441 if (!reject_if_disagree) {
1442 DECPTR(sizeof(u_int32_t), p);
1443 tl = ntohl(wo->hisaddr);
1446 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1448 * If neither we nor he knows his address, reject the option.
1451 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1456 * If he doesn't know our address, or if we both have our address
1457 * but disagree about it, then NAK it with our idea.
1459 GETLONG(tl, p); /* Parse desination address (ours) */
1460 ciaddr2 = htonl(tl);
1461 if (ciaddr2 != wo->ouraddr) {
1462 if (ciaddr2 == 0 || !wo->accept_local) {
1464 if (!reject_if_disagree) {
1465 DECPTR(sizeof(u_int32_t), p);
1466 tl = ntohl(wo->ouraddr);
1470 wo->ouraddr = ciaddr2; /* accept peer's idea */
1475 ho->hisaddr = ciaddr1;
1476 ho->ouraddr = ciaddr2;
1480 if (!ao->neg_addr || ho->old_addrs ||
1481 cilen != CILEN_ADDR) { /* Check CI length */
1482 orc = CONFREJ; /* Reject CI */
1487 * If he has no address, or if we both have his address but
1488 * disagree about it, then NAK it with our idea.
1489 * In particular, if we don't know his address, but he does,
1492 GETLONG(tl, p); /* Parse source address (his) */
1493 ciaddr1 = htonl(tl);
1494 if (ciaddr1 != wo->hisaddr
1495 && (ciaddr1 == 0 || !wo->accept_remote)) {
1497 if (!reject_if_disagree) {
1498 DECPTR(sizeof(u_int32_t), p);
1499 tl = ntohl(wo->hisaddr);
1502 } else if (ciaddr1 == 0 && wo->hisaddr == 0) {
1504 * Don't ACK an address of 0.0.0.0 - reject it instead.
1507 wo->req_addr = 0; /* don't NAK with 0.0.0.0 later */
1512 ho->hisaddr = ciaddr1;
1517 /* Microsoft primary or secondary DNS request */
1518 d = citype == CI_MS_DNS2;
1520 /* If we do not have a DNS address then we cannot send it */
1521 if (ao->dnsaddr[d] == 0 ||
1522 cilen != CILEN_ADDR) { /* Check CI length */
1523 orc = CONFREJ; /* Reject CI */
1527 if (htonl(tl) != ao->dnsaddr[d]) {
1528 DECPTR(sizeof(u_int32_t), p);
1529 tl = ntohl(ao->dnsaddr[d]);
1537 /* Microsoft primary or secondary WINS request */
1538 d = citype == CI_MS_WINS2;
1540 /* If we do not have a DNS address then we cannot send it */
1541 if (ao->winsaddr[d] == 0 ||
1542 cilen != CILEN_ADDR) { /* Check CI length */
1543 orc = CONFREJ; /* Reject CI */
1547 if (htonl(tl) != ao->winsaddr[d]) {
1548 DECPTR(sizeof(u_int32_t), p);
1549 tl = ntohl(ao->winsaddr[d]);
1555 case CI_COMPRESSTYPE:
1557 (cilen != CILEN_VJ && cilen != CILEN_COMPRESS)) {
1561 GETSHORT(cishort, p);
1563 if (!(cishort == IPCP_VJ_COMP ||
1564 (cishort == IPCP_VJ_COMP_OLD && cilen == CILEN_COMPRESS))) {
1570 ho->vj_protocol = cishort;
1571 if (cilen == CILEN_VJ) {
1572 GETCHAR(maxslotindex, p);
1573 if (maxslotindex > ao->maxslotindex) {
1575 if (!reject_if_disagree){
1577 PUTCHAR(ao->maxslotindex, p);
1581 if (cflag && !ao->cflag) {
1583 if (!reject_if_disagree){
1585 PUTCHAR(wo->cflag, p);
1588 ho->maxslotindex = maxslotindex;
1592 ho->maxslotindex = MAX_STATES - 1;
1602 if (orc == CONFACK && /* Good CI */
1603 rc != CONFACK) /* but prior CI wasnt? */
1604 continue; /* Don't send this one */
1606 if (orc == CONFNAK) { /* Nak this CI? */
1607 if (reject_if_disagree) /* Getting fed up with sending NAKs? */
1608 orc = CONFREJ; /* Get tough if so */
1610 if (rc == CONFREJ) /* Rejecting prior CI? */
1611 continue; /* Don't send this one */
1612 if (rc == CONFACK) { /* Ack'd all prior CIs? */
1613 rc = CONFNAK; /* Not anymore... */
1614 ucp = inp; /* Backup */
1619 if (orc == CONFREJ && /* Reject this CI */
1620 rc != CONFREJ) { /* but no prior ones? */
1622 ucp = inp; /* Backup */
1625 /* Need to move CI? */
1627 BCOPY(cip, ucp, cilen); /* Move it */
1629 /* Update output pointer */
1634 * If we aren't rejecting this packet, and we want to negotiate
1635 * their address, and they didn't send their address, then we
1636 * send a NAK with a CI_ADDR option appended. We assume the
1637 * input buffer is long enough that we can append the extra
1640 if (rc != CONFREJ && !ho->neg_addr && !ho->old_addrs &&
1641 wo->req_addr && !reject_if_disagree && !noremoteip) {
1642 if (rc == CONFACK) {
1644 ucp = inp; /* reset pointer */
1645 wo->req_addr = 0; /* don't ask again */
1647 PUTCHAR(CI_ADDR, ucp);
1648 PUTCHAR(CILEN_ADDR, ucp);
1649 tl = ntohl(wo->hisaddr);
1653 *len = ucp - inp; /* Compute output length */
1654 IPCPDEBUG(("ipcp: returning Configure-%s", CODENAME(rc)));
1655 return (rc); /* Return final code */
1660 * ip_check_options - check that any IP-related options are OK,
1661 * and assign appropriate defaults.
1664 ip_check_options(void)
1668 ipcp_options *wo = &ipcp_wantoptions[0];
1671 * Default our local IP address based on our hostname.
1672 * If local IP address already given, don't bother.
1674 if (wo->ouraddr == 0 && !disable_defaultip) {
1676 * Look up our hostname (possibly with domain name appended)
1677 * and take the first IP address as our local IP address.
1678 * If there isn't an IP address for our hostname, too bad.
1680 wo->accept_local = 1; /* don't insist on this default value */
1681 if ((hp = gethostbyname(hostname)) != NULL) {
1682 local = *(u_int32_t *)hp->h_addr;
1683 if (local != 0 && !bad_ip_adrs(local))
1684 wo->ouraddr = local;
1687 ask_for_local = wo->ouraddr != 0 || !disable_defaultip;
1692 * ip_demand_conf - configure the interface as though
1693 * IPCP were up, for use with dial-on-demand.
1696 ip_demand_conf(int u)
1698 ipcp_options *wo = &ipcp_wantoptions[u];
1700 if (wo->hisaddr == 0 && !noremoteip) {
1701 /* make up an arbitrary address for the peer */
1702 wo->hisaddr = htonl(0x0a707070 + ifunit);
1703 wo->accept_remote = 1;
1705 if (wo->ouraddr == 0) {
1706 /* make up an arbitrary address for us */
1707 wo->ouraddr = htonl(0x0a404040 + ifunit);
1708 wo->accept_local = 1;
1709 ask_for_local = 0; /* don't tell the peer this address */
1711 if (!sifaddr(u, wo->ouraddr, wo->hisaddr, GetMask(wo->ouraddr)))
1713 ipcp_script(_PATH_IPPREUP, 1);
1716 if (!sifnpmode(u, PPP_IP, NPMODE_QUEUE))
1718 if (wo->default_route)
1719 if (sifdefaultroute(u, wo->ouraddr, wo->hisaddr))
1720 default_route_set[u] = 1;
1722 if (sifproxyarp(u, wo->hisaddr))
1723 proxy_arp_set[u] = 1;
1725 notice("local IP address %I", wo->ouraddr);
1727 notice("remote IP address %I", wo->hisaddr);
1734 * ipcp_up - IPCP has come UP.
1736 * Configure the IP network interface appropriately and bring it up.
1742 ipcp_options *ho = &ipcp_hisoptions[f->unit];
1743 ipcp_options *go = &ipcp_gotoptions[f->unit];
1744 ipcp_options *wo = &ipcp_wantoptions[f->unit];
1747 IPCPDEBUG(("ipcp: up"));
1750 * We must have a non-zero IP address for both ends of the link.
1752 if (!ho->neg_addr && !ho->old_addrs)
1753 ho->hisaddr = wo->hisaddr;
1755 if (!(go->neg_addr || go->old_addrs) && (wo->neg_addr || wo->old_addrs)
1756 && wo->ouraddr != 0) {
1757 error("Peer refused to agree to our IP address");
1758 ipcp_close(f->unit, "Refused our IP address");
1761 if (go->ouraddr == 0) {
1762 error("Could not determine local IP address");
1763 ipcp_close(f->unit, "Could not determine local IP address");
1766 if (ho->hisaddr == 0 && !noremoteip) {
1767 ho->hisaddr = htonl(0x0a404040 + ifunit);
1768 warn("Could not determine remote IP address: defaulting to %I",
1771 script_setenv("IPLOCAL", ip_ntoa(go->ouraddr), 0);
1772 if (ho->hisaddr != 0)
1773 script_setenv("IPREMOTE", ip_ntoa(ho->hisaddr), 1);
1780 script_setenv("DNS1", ip_ntoa(go->dnsaddr[0]), 0);
1782 script_setenv("DNS2", ip_ntoa(go->dnsaddr[1]), 0);
1783 if (usepeerdns && (go->dnsaddr[0] || go->dnsaddr[1])) {
1784 script_setenv("USEPEERDNS", "1", 0);
1785 create_resolv(go->dnsaddr[0], go->dnsaddr[1]);
1789 * Check that the peer is allowed to use the IP address it wants.
1791 if (ho->hisaddr != 0 && !auth_ip_addr(f->unit, ho->hisaddr)) {
1792 error("Peer is not authorized to use remote address %I", ho->hisaddr);
1793 ipcp_close(f->unit, "Unauthorized remote IP address");
1797 /* set tcp compression */
1798 sifvjcomp(f->unit, ho->neg_vj, ho->cflag, ho->maxslotindex);
1801 * If we are doing dial-on-demand, the interface is already
1802 * configured, so we put out any saved-up packets, then set the
1803 * interface to pass IP packets.
1806 if (go->ouraddr != wo->ouraddr || ho->hisaddr != wo->hisaddr) {
1807 ipcp_clear_addrs(f->unit, wo->ouraddr, wo->hisaddr);
1808 if (go->ouraddr != wo->ouraddr) {
1809 warn("Local IP address changed to %I", go->ouraddr);
1810 script_setenv("OLDIPLOCAL", ip_ntoa(wo->ouraddr), 0);
1811 wo->ouraddr = go->ouraddr;
1813 script_unsetenv("OLDIPLOCAL");
1814 if (ho->hisaddr != wo->hisaddr && wo->hisaddr != 0) {
1815 warn("Remote IP address changed to %I", ho->hisaddr);
1816 script_setenv("OLDIPREMOTE", ip_ntoa(wo->hisaddr), 0);
1817 wo->hisaddr = ho->hisaddr;
1819 script_unsetenv("OLDIPREMOTE");
1821 /* Set the interface to the new addresses */
1822 mask = GetMask(go->ouraddr);
1823 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1825 warn("Interface configuration failed");
1826 ipcp_close(f->unit, "Interface configuration failed");
1830 /* assign a default route through the interface if required */
1831 if (ipcp_wantoptions[f->unit].default_route)
1832 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1833 default_route_set[f->unit] = 1;
1835 /* Make a proxy ARP entry if requested. */
1836 if (ho->hisaddr != 0 && ipcp_wantoptions[f->unit].proxy_arp)
1837 if (sifproxyarp(f->unit, ho->hisaddr))
1838 proxy_arp_set[f->unit] = 1;
1841 demand_rexmit(PPP_IP);
1842 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1846 * Set IP addresses and (if specified) netmask.
1848 mask = GetMask(go->ouraddr);
1850 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1851 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1853 warn("Interface configuration failed");
1854 ipcp_close(f->unit, "Interface configuration failed");
1859 ifindex = if_nametoindex(ifname);
1861 /* run the pre-up script, if any, and wait for it to finish */
1862 ipcp_script(_PATH_IPPREUP, 1);
1864 /* check if preup script renamed the interface */
1865 if (!if_indextoname(ifindex, ifname)) {
1866 error("Interface index %d failed to get renamed by a pre-up script", ifindex);
1867 ipcp_close(f->unit, "Interface configuration failed");
1871 /* bring the interface up for IP */
1872 if (!sifup(f->unit)) {
1874 warn("Interface failed to come up");
1875 ipcp_close(f->unit, "Interface configuration failed");
1879 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1880 if (!sifaddr(f->unit, go->ouraddr, ho->hisaddr, mask)) {
1882 warn("Interface configuration failed");
1883 ipcp_close(f->unit, "Interface configuration failed");
1887 sifnpmode(f->unit, PPP_IP, NPMODE_PASS);
1889 /* assign a default route through the interface if required */
1890 if (ipcp_wantoptions[f->unit].default_route)
1891 if (sifdefaultroute(f->unit, go->ouraddr, ho->hisaddr))
1892 default_route_set[f->unit] = 1;
1894 /* Make a proxy ARP entry if requested. */
1895 if (ho->hisaddr != 0 && ipcp_wantoptions[f->unit].proxy_arp)
1896 if (sifproxyarp(f->unit, ho->hisaddr))
1897 proxy_arp_set[f->unit] = 1;
1899 ipcp_wantoptions[0].ouraddr = go->ouraddr;
1901 notice("local IP address %I", go->ouraddr);
1902 if (ho->hisaddr != 0)
1903 notice("remote IP address %I", ho->hisaddr);
1905 notice("primary DNS address %I", go->dnsaddr[0]);
1907 notice("secondary DNS address %I", go->dnsaddr[1]);
1910 reset_link_stats(f->unit);
1912 np_up(f->unit, PPP_IP);
1915 notify(ip_up_notifier, 0);
1920 * Execute the ip-up script, like this:
1921 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1923 if (ipcp_script_state == s_down && ipcp_script_pid == 0) {
1924 ipcp_script_state = s_up;
1925 ipcp_script(_PATH_IPUP, 0);
1931 * ipcp_down - IPCP has gone DOWN.
1933 * Take the IP network interface down, clear its addresses
1934 * and delete routes through it.
1939 IPCPDEBUG(("ipcp: down"));
1940 /* XXX a bit IPv4-centric here, we only need to get the stats
1941 * before the interface is marked down. */
1942 /* XXX more correct: we must get the stats before running the notifiers,
1943 * at least for the radius plugin */
1944 update_link_stats(f->unit);
1945 notify(ip_down_notifier, 0);
1950 np_down(f->unit, PPP_IP);
1952 sifvjcomp(f->unit, 0, 0, 0);
1954 print_link_stats(); /* _after_ running the notifiers and ip_down_hook(),
1955 * because print_link_stats() sets link_stats_valid
1959 * If we are doing dial-on-demand, set the interface
1960 * to queue up outgoing packets (for now).
1963 sifnpmode(f->unit, PPP_IP, NPMODE_QUEUE);
1965 sifnpmode(f->unit, PPP_IP, NPMODE_DROP);
1967 ipcp_clear_addrs(f->unit, ipcp_gotoptions[f->unit].ouraddr,
1968 ipcp_hisoptions[f->unit].hisaddr);
1971 /* Execute the ip-down script */
1972 if (ipcp_script_state == s_up && ipcp_script_pid == 0) {
1973 ipcp_script_state = s_down;
1974 ipcp_script(_PATH_IPDOWN, 0);
1980 * ipcp_clear_addrs() - clear the interface addresses, routes,
1981 * proxy arp entries, etc.
1984 ipcp_clear_addrs(int unit, u_int32_t ouraddr, u_int32_t hisaddr)
1986 if (proxy_arp_set[unit]) {
1987 cifproxyarp(unit, hisaddr);
1988 proxy_arp_set[unit] = 0;
1990 if (default_route_set[unit]) {
1991 cifdefaultroute(unit, ouraddr, hisaddr);
1992 default_route_set[unit] = 0;
1994 cifaddr(unit, ouraddr, hisaddr);
1999 * ipcp_finished - possibly shut down the lower layers.
2002 ipcp_finished(fsm *f)
2006 np_finished(f->unit, PPP_IP);
2012 * ipcp_script_done - called when the ip-up or ip-down script
2016 ipcp_script_done(void *arg)
2018 ipcp_script_pid = 0;
2019 switch (ipcp_script_state) {
2021 if (ipcp_fsm[0].state != OPENED) {
2022 ipcp_script_state = s_down;
2023 ipcp_script(_PATH_IPDOWN, 0);
2027 if (ipcp_fsm[0].state == OPENED) {
2028 ipcp_script_state = s_up;
2029 ipcp_script(_PATH_IPUP, 0);
2037 * ipcp_script - Execute a script with arguments
2038 * interface-name tty-name speed local-IP remote-IP.
2041 ipcp_script(char *script, int wait)
2043 char strspeed[32], strlocal[32], strremote[32];
2046 slprintf(strspeed, sizeof(strspeed), "%d", baud_rate);
2047 slprintf(strlocal, sizeof(strlocal), "%I", ipcp_gotoptions[0].ouraddr);
2048 slprintf(strremote, sizeof(strremote), "%I", ipcp_hisoptions[0].hisaddr);
2055 argv[5] = strremote;
2059 run_program(script, argv, 0, NULL, NULL, 1);
2061 ipcp_script_pid = run_program(script, argv, 0, ipcp_script_done,
2066 * create_resolv - create the replacement resolv.conf file
2069 create_resolv(u_int32_t peerdns1, u_int32_t peerdns2)
2073 f = fopen(_PATH_RESOLV, "w");
2075 error("Failed to create %s: %m", _PATH_RESOLV);
2080 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns1));
2083 fprintf(f, "nameserver %s\n", ip_ntoa(peerdns2));
2086 error("Write failed to %s: %m", _PATH_RESOLV);
2092 * ipcp_printpkt - print the contents of an IPCP packet.
2094 static char *ipcp_codenames[] = {
2095 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
2096 "TermReq", "TermAck", "CodeRej"
2100 ipcp_printpkt(u_char *p, int plen,
2101 void (*printer) (void *, char *, ...), void *arg)
2103 int code, id, len, olen;
2104 u_char *pstart, *optend;
2108 if (plen < HEADERLEN)
2114 if (len < HEADERLEN || len > plen)
2117 if (code >= 1 && code <= sizeof(ipcp_codenames) / sizeof(char *))
2118 printer(arg, " %s", ipcp_codenames[code-1]);
2120 printer(arg, " code=0x%x", code);
2121 printer(arg, " id=0x%x", id);
2128 /* print option list */
2133 if (olen < 2 || olen > len) {
2141 if (olen == CILEN_ADDRS) {
2144 printer(arg, "addrs %I", htonl(cilong));
2146 printer(arg, " %I", htonl(cilong));
2149 case CI_COMPRESSTYPE:
2150 if (olen >= CILEN_COMPRESS) {
2152 GETSHORT(cishort, p);
2153 printer(arg, "compress ");
2158 case IPCP_VJ_COMP_OLD:
2159 printer(arg, "old-VJ");
2162 printer(arg, "0x%x", cishort);
2167 if (olen == CILEN_ADDR) {
2170 printer(arg, "addr %I", htonl(cilong));
2177 printer(arg, "ms-dns%d %I", (code == CI_MS_DNS1? 1: 2),
2184 printer(arg, "ms-wins %I", htonl(cilong));
2187 while (p < optend) {
2189 printer(arg, " %.2x", code);
2197 if (len > 0 && *p >= ' ' && *p < 0x7f) {
2199 print_string((char *)p, len, printer, arg);
2206 /* print the rest of the bytes in the packet */
2207 for (; len > 0; --len) {
2209 printer(arg, " %.2x", code);
2216 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
2217 * We don't bring the link up for IP fragments or for TCP FIN packets
2220 #define IP_HDRLEN 20 /* bytes */
2221 #define IP_OFFMASK 0x1fff
2223 #define IPPROTO_TCP 6
2225 #define TCP_HDRLEN 20
2229 * We use these macros because the IP header may be at an odd address,
2230 * and some compilers might use word loads to get th_off or ip_hl.
2233 #define net_short(x) (((x)[0] << 8) + (x)[1])
2234 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
2235 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
2236 #define get_ipproto(x) (((unsigned char *)(x))[9])
2237 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
2238 #define get_tcpflags(x) (((unsigned char *)(x))[13])
2241 ip_active_pkt(u_char *pkt, int len)
2248 if (len < IP_HDRLEN)
2250 if ((get_ipoff(pkt) & IP_OFFMASK) != 0)
2252 if (get_ipproto(pkt) != IPPROTO_TCP)
2254 hlen = get_iphl(pkt) * 4;
2255 if (len < hlen + TCP_HDRLEN)
2258 if ((get_tcpflags(tcp) & TH_FIN) != 0 && len == hlen + get_tcpoff(tcp) * 4)