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.
43 #define RCSID "$Id: ipcp.c,v 1.69 2004/11/13 12:03:26 paulus Exp $"
53 #include <sys/param.h>
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <netinet/in.h>
57 #include <arpa/inet.h>
62 #include "pathnames.h"
64 static const char rcsid
[] = RCSID
;
67 ipcp_options ipcp_wantoptions
[NUM_PPP
]; /* Options that we want to request */
68 ipcp_options ipcp_gotoptions
[NUM_PPP
]; /* Options that peer ack'd */
69 ipcp_options ipcp_allowoptions
[NUM_PPP
]; /* Options we allow peer to request */
70 ipcp_options ipcp_hisoptions
[NUM_PPP
]; /* Options that we ack'd */
72 u_int32_t netmask
= 0; /* IP netmask to set on interface */
74 bool disable_defaultip
= 0; /* Don't use hostname for default IP adrs */
76 /* Hook for a plugin to know when IP protocol has come up */
77 void (*ip_up_hook
) __P((void)) = NULL
;
79 /* Hook for a plugin to know when IP protocol has come down */
80 void (*ip_down_hook
) __P((void)) = NULL
;
82 /* Hook for a plugin to choose the remote IP address */
83 void (*ip_choose_hook
) __P((u_int32_t
*)) = NULL
;
85 /* Notifiers for when IPCP goes up and down */
86 struct notifier
*ip_up_notifier
= NULL
;
87 struct notifier
*ip_down_notifier
= NULL
;
90 static int default_route_set
[NUM_PPP
]; /* Have set up a default route */
91 static int proxy_arp_set
[NUM_PPP
]; /* Have created proxy arp entry */
92 static bool usepeerdns
; /* Ask peer for DNS addrs */
93 static int ipcp_is_up
; /* have called np_up() */
94 static int ipcp_is_open
; /* haven't called np_finished() */
95 static bool ask_for_local
; /* request our address from peer */
96 static char vj_value
[8]; /* string form of vj option value */
97 static char netmask_str
[20]; /* string form of netmask value */
100 * Callbacks for fsm code. (CI = Configuration Information)
102 static void ipcp_resetci
__P((fsm
*)); /* Reset our CI */
103 static int ipcp_cilen
__P((fsm
*)); /* Return length of our CI */
104 static void ipcp_addci
__P((fsm
*, u_char
*, int *)); /* Add our CI */
105 static int ipcp_ackci
__P((fsm
*, u_char
*, int)); /* Peer ack'd our CI */
106 static int ipcp_nakci
__P((fsm
*, u_char
*, int, int));/* Peer nak'd our CI */
107 static int ipcp_rejci
__P((fsm
*, u_char
*, int)); /* Peer rej'd our CI */
108 static int ipcp_reqci
__P((fsm
*, u_char
*, int *, int)); /* Rcv CI */
109 static void ipcp_up
__P((fsm
*)); /* We're UP */
110 static void ipcp_down
__P((fsm
*)); /* We're DOWN */
111 static void ipcp_finished
__P((fsm
*)); /* Don't need lower layer */
113 fsm ipcp_fsm
[NUM_PPP
]; /* IPCP fsm structure */
115 static fsm_callbacks ipcp_callbacks
= { /* IPCP callback routines */
116 ipcp_resetci
, /* Reset our Configuration Information */
117 ipcp_cilen
, /* Length of our Configuration Information */
118 ipcp_addci
, /* Add our Configuration Information */
119 ipcp_ackci
, /* ACK our Configuration Information */
120 ipcp_nakci
, /* NAK our Configuration Information */
121 ipcp_rejci
, /* Reject our Configuration Information */
122 ipcp_reqci
, /* Request peer's Configuration Information */
123 ipcp_up
, /* Called when fsm reaches OPENED state */
124 ipcp_down
, /* Called when fsm leaves OPENED state */
125 NULL
, /* Called when we want the lower layer up */
126 ipcp_finished
, /* Called when we want the lower layer down */
127 NULL
, /* Called when Protocol-Reject received */
128 NULL
, /* Retransmission is necessary */
129 NULL
, /* Called to handle protocol-specific codes */
130 "IPCP" /* String name of protocol */
134 * Command-line options.
136 static int setvjslots
__P((char **));
137 static int setdnsaddr
__P((char **));
138 static int setwinsaddr
__P((char **));
139 static int setnetmask
__P((char **));
140 int setipaddr
__P((char *, char **, int));
141 static void printipaddr
__P((option_t
*, void (*)(void *, char *,...),void *));
143 static option_t ipcp_option_list
[] = {
144 { "noip", o_bool
, &ipcp_protent
.enabled_flag
,
145 "Disable IP and IPCP" },
146 { "-ip", o_bool
, &ipcp_protent
.enabled_flag
,
147 "Disable IP and IPCP", OPT_ALIAS
},
149 { "novj", o_bool
, &ipcp_wantoptions
[0].neg_vj
,
150 "Disable VJ compression", OPT_A2CLR
, &ipcp_allowoptions
[0].neg_vj
},
151 { "-vj", o_bool
, &ipcp_wantoptions
[0].neg_vj
,
152 "Disable VJ compression", OPT_ALIAS
| OPT_A2CLR
,
153 &ipcp_allowoptions
[0].neg_vj
},
155 { "novjccomp", o_bool
, &ipcp_wantoptions
[0].cflag
,
156 "Disable VJ connection-ID compression", OPT_A2CLR
,
157 &ipcp_allowoptions
[0].cflag
},
158 { "-vjccomp", o_bool
, &ipcp_wantoptions
[0].cflag
,
159 "Disable VJ connection-ID compression", OPT_ALIAS
| OPT_A2CLR
,
160 &ipcp_allowoptions
[0].cflag
},
162 { "vj-max-slots", o_special
, (void *)setvjslots
,
163 "Set maximum VJ header slots",
164 OPT_PRIO
| OPT_A2STRVAL
| OPT_STATIC
, vj_value
},
166 { "ipcp-accept-local", o_bool
, &ipcp_wantoptions
[0].accept_local
,
167 "Accept peer's address for us", 1 },
168 { "ipcp-accept-remote", o_bool
, &ipcp_wantoptions
[0].accept_remote
,
169 "Accept peer's address for it", 1 },
171 { "ipparam", o_string
, &ipparam
,
172 "Set ip script parameter", OPT_PRIO
},
174 { "noipdefault", o_bool
, &disable_defaultip
,
175 "Don't use name for default IP adrs", 1 },
177 { "ms-dns", 1, (void *)setdnsaddr
,
178 "DNS address for the peer's use" },
179 { "ms-wins", 1, (void *)setwinsaddr
,
180 "Nameserver for SMB over TCP/IP for peer" },
182 { "ipcp-restart", o_int
, &ipcp_fsm
[0].timeouttime
,
183 "Set timeout for IPCP", OPT_PRIO
},
184 { "ipcp-max-terminate", o_int
, &ipcp_fsm
[0].maxtermtransmits
,
185 "Set max #xmits for term-reqs", OPT_PRIO
},
186 { "ipcp-max-configure", o_int
, &ipcp_fsm
[0].maxconfreqtransmits
,
187 "Set max #xmits for conf-reqs", OPT_PRIO
},
188 { "ipcp-max-failure", o_int
, &ipcp_fsm
[0].maxnakloops
,
189 "Set max #conf-naks for IPCP", OPT_PRIO
},
191 { "defaultroute", o_bool
, &ipcp_wantoptions
[0].default_route
,
192 "Add default route", OPT_ENABLE
|1, &ipcp_allowoptions
[0].default_route
},
193 { "nodefaultroute", o_bool
, &ipcp_allowoptions
[0].default_route
,
194 "disable defaultroute option", OPT_A2CLR
,
195 &ipcp_wantoptions
[0].default_route
},
196 { "-defaultroute", o_bool
, &ipcp_allowoptions
[0].default_route
,
197 "disable defaultroute option", OPT_ALIAS
| OPT_A2CLR
,
198 &ipcp_wantoptions
[0].default_route
},
200 { "proxyarp", o_bool
, &ipcp_wantoptions
[0].proxy_arp
,
201 "Add proxy ARP entry", OPT_ENABLE
|1, &ipcp_allowoptions
[0].proxy_arp
},
202 { "noproxyarp", o_bool
, &ipcp_allowoptions
[0].proxy_arp
,
203 "disable proxyarp option", OPT_A2CLR
,
204 &ipcp_wantoptions
[0].proxy_arp
},
205 { "-proxyarp", o_bool
, &ipcp_allowoptions
[0].proxy_arp
,
206 "disable proxyarp option", OPT_ALIAS
| OPT_A2CLR
,
207 &ipcp_wantoptions
[0].proxy_arp
},
209 { "usepeerdns", o_bool
, &usepeerdns
,
210 "Ask peer for DNS address(es)", 1 },
212 { "netmask", o_special
, (void *)setnetmask
,
213 "set netmask", OPT_PRIO
| OPT_A2STRVAL
| OPT_STATIC
, netmask_str
},
215 { "ipcp-no-addresses", o_bool
, &ipcp_wantoptions
[0].old_addrs
,
216 "Disable old-style IP-Addresses usage", OPT_A2CLR
,
217 &ipcp_allowoptions
[0].old_addrs
},
218 { "ipcp-no-address", o_bool
, &ipcp_wantoptions
[0].neg_addr
,
219 "Disable IP-Address usage", OPT_A2CLR
,
220 &ipcp_allowoptions
[0].neg_addr
},
222 { "IP addresses", o_wild
, (void *) &setipaddr
,
223 "set local and remote IP addresses",
224 OPT_NOARG
| OPT_A2PRINTER
, (void *) &printipaddr
},
230 * Protocol entry points from main code.
232 static void ipcp_init
__P((int));
233 static void ipcp_open
__P((int));
234 static void ipcp_close
__P((int, char *));
235 static void ipcp_lowerup
__P((int));
236 static void ipcp_lowerdown
__P((int));
237 static void ipcp_input
__P((int, u_char
*, int));
238 static void ipcp_protrej
__P((int));
239 static int ipcp_printpkt
__P((u_char
*, int,
240 void (*) __P((void *, char *, ...)), void *));
241 static void ip_check_options
__P((void));
242 static int ip_demand_conf
__P((int));
243 static int ip_active_pkt
__P((u_char
*, int));
244 static void create_resolv
__P((u_int32_t
, u_int32_t
));
246 struct protent ipcp_protent
= {
266 static void ipcp_clear_addrs
__P((int, u_int32_t
, u_int32_t
));
267 static void ipcp_script
__P((char *)); /* Run an up/down script */
268 static void ipcp_script_done
__P((void *));
271 * Lengths of configuration options.
274 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
275 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
276 #define CILEN_ADDR 6 /* new-style single address option */
277 #define CILEN_ADDRS 10 /* old-style dual address option */
280 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
281 (x) == CONFNAK ? "NAK" : "REJ")
284 * This state variable is used to ensure that we don't
285 * run an ipcp-up/down script while one is already running.
287 static enum script_state
{
291 static pid_t ipcp_script_pid
;
294 * Make a string representation of a network IP address.
302 slprintf(b
, sizeof(b
), "%I", ipaddr
);
311 * setvjslots - set maximum number of connection slots for VJ compression
319 if (!int_option(*argv
, &value
))
321 if (value
< 2 || value
> 16) {
322 option_error("vj-max-slots value must be between 2 and 16");
325 ipcp_wantoptions
[0].maxslotindex
=
326 ipcp_allowoptions
[0].maxslotindex
= value
- 1;
327 slprintf(vj_value
, sizeof(vj_value
), "%d", value
);
332 * setdnsaddr - set the dns address(es)
341 dns
= inet_addr(*argv
);
342 if (dns
== (u_int32_t
) -1) {
343 if ((hp
= gethostbyname(*argv
)) == NULL
) {
344 option_error("invalid address parameter '%s' for ms-dns option",
348 dns
= *(u_int32_t
*)hp
->h_addr
;
351 /* We take the last 2 values given, the 2nd-last as the primary
352 and the last as the secondary. If only one is given it
353 becomes both primary and secondary. */
354 if (ipcp_allowoptions
[0].dnsaddr
[1] == 0)
355 ipcp_allowoptions
[0].dnsaddr
[0] = dns
;
357 ipcp_allowoptions
[0].dnsaddr
[0] = ipcp_allowoptions
[0].dnsaddr
[1];
359 /* always set the secondary address value. */
360 ipcp_allowoptions
[0].dnsaddr
[1] = dns
;
366 * setwinsaddr - set the wins address(es)
367 * This is primrarly used with the Samba package under UNIX or for pointing
368 * the caller to the existing WINS server on a Windows NT platform.
377 wins
= inet_addr(*argv
);
378 if (wins
== (u_int32_t
) -1) {
379 if ((hp
= gethostbyname(*argv
)) == NULL
) {
380 option_error("invalid address parameter '%s' for ms-wins option",
384 wins
= *(u_int32_t
*)hp
->h_addr
;
387 /* We take the last 2 values given, the 2nd-last as the primary
388 and the last as the secondary. If only one is given it
389 becomes both primary and secondary. */
390 if (ipcp_allowoptions
[0].winsaddr
[1] == 0)
391 ipcp_allowoptions
[0].winsaddr
[0] = wins
;
393 ipcp_allowoptions
[0].winsaddr
[0] = ipcp_allowoptions
[0].winsaddr
[1];
395 /* always set the secondary address value. */
396 ipcp_allowoptions
[0].winsaddr
[1] = wins
;
402 * setipaddr - Set the IP address
403 * If doit is 0, the call is to check whether this option is
404 * potentially an IP address specification.
405 * Not static so that plugins can call it to set the addresses
408 setipaddr(arg
, argv
, doit
)
415 u_int32_t local
, remote
;
416 ipcp_options
*wo
= &ipcp_wantoptions
[0];
417 static int prio_local
= 0, prio_remote
= 0;
420 * IP address pair separated by ":".
422 if ((colon
= strchr(arg
, ':')) == NULL
)
428 * If colon first character, then no local addr.
430 if (colon
!= arg
&& option_priority
>= prio_local
) {
432 if ((local
= inet_addr(arg
)) == (u_int32_t
) -1) {
433 if ((hp
= gethostbyname(arg
)) == NULL
) {
434 option_error("unknown host: %s", arg
);
437 local
= *(u_int32_t
*)hp
->h_addr
;
439 if (bad_ip_adrs(local
)) {
440 option_error("bad local IP address %s", ip_ntoa(local
));
446 prio_local
= option_priority
;
450 * If colon last character, then no remote addr.
452 if (*++colon
!= '\0' && option_priority
>= prio_remote
) {
453 if ((remote
= inet_addr(colon
)) == (u_int32_t
) -1) {
454 if ((hp
= gethostbyname(colon
)) == NULL
) {
455 option_error("unknown host: %s", colon
);
458 remote
= *(u_int32_t
*)hp
->h_addr
;
459 if (remote_name
[0] == 0)
460 strlcpy(remote_name
, colon
, sizeof(remote_name
));
462 if (bad_ip_adrs(remote
)) {
463 option_error("bad remote IP address %s", ip_ntoa(remote
));
467 wo
->hisaddr
= remote
;
468 prio_remote
= option_priority
;
475 printipaddr(opt
, printer
, arg
)
477 void (*printer
) __P((void *, char *, ...));
480 ipcp_options
*wo
= &ipcp_wantoptions
[0];
482 if (wo
->ouraddr
!= 0)
483 printer(arg
, "%I", wo
->ouraddr
);
485 if (wo
->hisaddr
!= 0)
486 printer(arg
, "%I", wo
->hisaddr
);
490 * setnetmask - set the netmask to be used on the interface.
501 * Unfortunately, if we use inet_addr, we can't tell whether
502 * a result of all 1s is an error or a valid 255.255.255.255.
505 n
= parse_dotted_ip(p
, &mask
);
509 if (n
== 0 || p
[n
] != 0 || (netmask
& ~mask
) != 0) {
510 option_error("invalid netmask value '%s'", *argv
);
515 slprintf(netmask_str
, sizeof(netmask_str
), "%I", mask
);
521 parse_dotted_ip(p
, vp
)
531 b
= strtoul(p
, &endp
, 0);
537 /* accept e.g. 0xffffff00 */
555 * ipcp_init - Initialize IPCP.
561 fsm
*f
= &ipcp_fsm
[unit
];
562 ipcp_options
*wo
= &ipcp_wantoptions
[unit
];
563 ipcp_options
*ao
= &ipcp_allowoptions
[unit
];
566 f
->protocol
= PPP_IPCP
;
567 f
->callbacks
= &ipcp_callbacks
;
568 fsm_init(&ipcp_fsm
[unit
]);
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.
605 fsm_open(&ipcp_fsm
[unit
]);
611 * ipcp_close - Take IPCP down.
614 ipcp_close(unit
, reason
)
618 fsm_close(&ipcp_fsm
[unit
], reason
);
623 * ipcp_lowerup - The lower layer is up.
629 fsm_lowerup(&ipcp_fsm
[unit
]);
634 * ipcp_lowerdown - The lower layer is down.
640 fsm_lowerdown(&ipcp_fsm
[unit
]);
645 * ipcp_input - Input IPCP packet.
648 ipcp_input(unit
, p
, len
)
653 fsm_input(&ipcp_fsm
[unit
], p
, len
);
658 * ipcp_protrej - A Protocol-Reject was received for IPCP.
660 * Pretend the lower layer went down, so we shut up.
666 fsm_lowerdown(&ipcp_fsm
[unit
]);
671 * ipcp_resetci - Reset our CI.
672 * Called by fsm_sconfreq, Send Configure Request.
678 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
679 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
680 ipcp_options
*ao
= &ipcp_allowoptions
[f
->unit
];
682 wo
->req_addr
= (wo
->neg_addr
|| wo
->old_addrs
) &&
683 (ao
->neg_addr
|| ao
->old_addrs
);
684 if (wo
->ouraddr
== 0)
685 wo
->accept_local
= 1;
686 if (wo
->hisaddr
== 0)
687 wo
->accept_remote
= 1;
688 wo
->req_dns1
= usepeerdns
; /* Request DNS addresses from the peer */
689 wo
->req_dns2
= usepeerdns
;
693 if (ip_choose_hook
) {
694 ip_choose_hook(&wo
->hisaddr
);
696 wo
->accept_remote
= 0;
699 BZERO(&ipcp_hisoptions
[f
->unit
], sizeof(ipcp_options
));
704 * ipcp_cilen - Return length of our CI.
705 * Called by fsm_sconfreq, Send Configure Request.
711 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
712 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
713 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
715 #define LENCIADDRS(neg) (neg ? CILEN_ADDRS : 0)
716 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
717 #define LENCIADDR(neg) (neg ? CILEN_ADDR : 0)
718 #define LENCIDNS(neg) (neg ? (CILEN_ADDR) : 0)
721 * First see if we want to change our options to the old
722 * forms because we have received old forms from the peer.
724 if (go
->neg_addr
&& go
->old_addrs
&& !ho
->neg_addr
&& ho
->old_addrs
)
726 if (wo
->neg_vj
&& !go
->neg_vj
&& !go
->old_vj
) {
727 /* try an older style of VJ negotiation */
728 /* use the old style only if the peer did */
729 if (ho
->neg_vj
&& ho
->old_vj
) {
732 go
->vj_protocol
= ho
->vj_protocol
;
736 return (LENCIADDRS(!go
->neg_addr
&& go
->old_addrs
) +
737 LENCIVJ(go
->neg_vj
, go
->old_vj
) +
738 LENCIADDR(go
->neg_addr
) +
739 LENCIDNS(go
->req_dns1
) +
740 LENCIDNS(go
->req_dns2
)) ;
745 * ipcp_addci - Add our desired CIs to a packet.
746 * Called by fsm_sconfreq, Send Configure Request.
749 ipcp_addci(f
, ucp
, lenp
)
754 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
757 #define ADDCIADDRS(opt, neg, val1, val2) \
759 if (len >= CILEN_ADDRS) { \
762 PUTCHAR(CILEN_ADDRS, ucp); \
767 len -= CILEN_ADDRS; \
772 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
774 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
775 if (len >= vjlen) { \
777 PUTCHAR(vjlen, ucp); \
778 PUTSHORT(val, ucp); \
780 PUTCHAR(maxslotindex, ucp); \
781 PUTCHAR(cflag, ucp); \
788 #define ADDCIADDR(opt, neg, val) \
790 if (len >= CILEN_ADDR) { \
793 PUTCHAR(CILEN_ADDR, ucp); \
801 #define ADDCIDNS(opt, neg, addr) \
803 if (len >= CILEN_ADDR) { \
806 PUTCHAR(CILEN_ADDR, ucp); \
814 ADDCIADDRS(CI_ADDRS
, !go
->neg_addr
&& go
->old_addrs
, go
->ouraddr
,
817 ADDCIVJ(CI_COMPRESSTYPE
, go
->neg_vj
, go
->vj_protocol
, go
->old_vj
,
818 go
->maxslotindex
, go
->cflag
);
820 ADDCIADDR(CI_ADDR
, go
->neg_addr
, go
->ouraddr
);
822 ADDCIDNS(CI_MS_DNS1
, go
->req_dns1
, go
->dnsaddr
[0]);
824 ADDCIDNS(CI_MS_DNS2
, go
->req_dns2
, go
->dnsaddr
[1]);
831 * ipcp_ackci - Ack our CIs.
832 * Called by fsm_rconfack, Receive Configure ACK.
839 ipcp_ackci(f
, p
, len
)
844 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
845 u_short cilen
, citype
, cishort
;
847 u_char cimaxslotindex
, cicflag
;
850 * CIs must be in exactly the same order that we sent...
851 * Check packet length and CI length at each step.
852 * If we find any deviations, then this packet is bad.
855 #define ACKCIADDRS(opt, neg, val1, val2) \
858 if ((len -= CILEN_ADDRS) < 0) \
860 GETCHAR(citype, p); \
862 if (cilen != CILEN_ADDRS || \
867 if (val1 != cilong) \
871 if (val2 != cilong) \
875 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
877 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
878 if ((len -= vjlen) < 0) \
880 GETCHAR(citype, p); \
882 if (cilen != vjlen || \
885 GETSHORT(cishort, p); \
886 if (cishort != val) \
889 GETCHAR(cimaxslotindex, p); \
890 if (cimaxslotindex != maxslotindex) \
892 GETCHAR(cicflag, p); \
893 if (cicflag != cflag) \
898 #define ACKCIADDR(opt, neg, val) \
901 if ((len -= CILEN_ADDR) < 0) \
903 GETCHAR(citype, p); \
905 if (cilen != CILEN_ADDR || \
914 #define ACKCIDNS(opt, neg, addr) \
917 if ((len -= CILEN_ADDR) < 0) \
919 GETCHAR(citype, p); \
921 if (cilen != CILEN_ADDR || citype != opt) \
925 if (addr != cilong) \
929 ACKCIADDRS(CI_ADDRS
, !go
->neg_addr
&& go
->old_addrs
, go
->ouraddr
,
932 ACKCIVJ(CI_COMPRESSTYPE
, go
->neg_vj
, go
->vj_protocol
, go
->old_vj
,
933 go
->maxslotindex
, go
->cflag
);
935 ACKCIADDR(CI_ADDR
, go
->neg_addr
, go
->ouraddr
);
937 ACKCIDNS(CI_MS_DNS1
, go
->req_dns1
, go
->dnsaddr
[0]);
939 ACKCIDNS(CI_MS_DNS2
, go
->req_dns2
, go
->dnsaddr
[1]);
942 * If there are any remaining CIs, then this packet is bad.
949 IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
954 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
955 * This should not modify any state if the Nak is bad
956 * or if IPCP is in the OPENED state.
957 * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
964 ipcp_nakci(f
, p
, len
, treat_as_reject
)
970 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
971 u_char cimaxslotindex
, cicflag
;
972 u_char citype
, cilen
, *next
;
974 u_int32_t ciaddr1
, ciaddr2
, l
, cidnsaddr
;
975 ipcp_options no
; /* options we've seen Naks for */
976 ipcp_options
try; /* options to request next time */
978 BZERO(&no
, sizeof(no
));
982 * Any Nak'd CIs must be in exactly the same order that we sent.
983 * Check packet length and CI length at each step.
984 * If we find any deviations, then this packet is bad.
986 #define NAKCIADDRS(opt, neg, code) \
988 (cilen = p[1]) == CILEN_ADDRS && \
994 ciaddr1 = htonl(l); \
996 ciaddr2 = htonl(l); \
1001 #define NAKCIVJ(opt, neg, code) \
1003 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
1008 GETSHORT(cishort, p); \
1013 #define NAKCIADDR(opt, neg, code) \
1015 (cilen = p[1]) == CILEN_ADDR && \
1021 ciaddr1 = htonl(l); \
1026 #define NAKCIDNS(opt, neg, code) \
1028 ((cilen = p[1]) == CILEN_ADDR) && \
1034 cidnsaddr = htonl(l); \
1040 * Accept the peer's idea of {our,his} address, if different
1041 * from our idea, only if the accept_{local,remote} flag is set.
1043 NAKCIADDRS(CI_ADDRS
, !go
->neg_addr
&& go
->old_addrs
,
1044 if (treat_as_reject
) {
1047 if (go
->accept_local
&& ciaddr1
) {
1048 /* take his idea of our address */
1049 try.ouraddr
= ciaddr1
;
1051 if (go
->accept_remote
&& ciaddr2
) {
1052 /* take his idea of his address */
1053 try.hisaddr
= ciaddr2
;
1059 * Accept the peer's value of maxslotindex provided that it
1060 * is less than what we asked for. Turn off slot-ID compression
1061 * if the peer wants. Send old-style compress-type option if
1064 NAKCIVJ(CI_COMPRESSTYPE
, neg_vj
,
1065 if (treat_as_reject
) {
1067 } else if (cilen
== CILEN_VJ
) {
1068 GETCHAR(cimaxslotindex
, p
);
1069 GETCHAR(cicflag
, p
);
1070 if (cishort
== IPCP_VJ_COMP
) {
1072 if (cimaxslotindex
< go
->maxslotindex
)
1073 try.maxslotindex
= cimaxslotindex
;
1080 if (cishort
== IPCP_VJ_COMP
|| cishort
== IPCP_VJ_COMP_OLD
) {
1082 try.vj_protocol
= cishort
;
1089 NAKCIADDR(CI_ADDR
, neg_addr
,
1090 if (treat_as_reject
) {
1093 } else if (go
->accept_local
&& ciaddr1
) {
1094 /* take his idea of our address */
1095 try.ouraddr
= ciaddr1
;
1099 NAKCIDNS(CI_MS_DNS1
, req_dns1
,
1100 if (treat_as_reject
) {
1103 try.dnsaddr
[0] = cidnsaddr
;
1107 NAKCIDNS(CI_MS_DNS2
, req_dns2
,
1108 if (treat_as_reject
) {
1111 try.dnsaddr
[1] = cidnsaddr
;
1116 * There may be remaining CIs, if the peer is requesting negotiation
1117 * on an option that we didn't include in our request packet.
1118 * If they want to negotiate about IP addresses, we comply.
1119 * If they want us to ask for compression, we refuse.
1121 while (len
>= CILEN_VOID
) {
1124 if ( cilen
< CILEN_VOID
|| (len
-= cilen
) < 0 )
1126 next
= p
+ cilen
- 2;
1129 case CI_COMPRESSTYPE
:
1130 if (go
->neg_vj
|| no
.neg_vj
||
1131 (cilen
!= CILEN_VJ
&& cilen
!= CILEN_COMPRESS
))
1136 if ((!go
->neg_addr
&& go
->old_addrs
) || no
.old_addrs
1137 || cilen
!= CILEN_ADDRS
)
1142 if (ciaddr1
&& go
->accept_local
)
1143 try.ouraddr
= ciaddr1
;
1146 if (ciaddr2
&& go
->accept_remote
)
1147 try.hisaddr
= ciaddr2
;
1151 if (go
->neg_addr
|| no
.neg_addr
|| cilen
!= CILEN_ADDR
)
1156 if (ciaddr1
&& go
->accept_local
)
1157 try.ouraddr
= ciaddr1
;
1158 if (try.ouraddr
!= 0)
1167 * OK, the Nak is good. Now we can update state.
1168 * If there are any remaining options, we ignore them.
1170 if (f
->state
!= OPENED
)
1176 IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
1182 * ipcp_rejci - Reject some of our CIs.
1183 * Callback from fsm_rconfnakrej.
1186 ipcp_rejci(f
, p
, len
)
1191 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
1192 u_char cimaxslotindex
, ciflag
, cilen
;
1195 ipcp_options
try; /* options to request next time */
1199 * Any Rejected CIs must be in exactly the same order that we sent.
1200 * Check packet length and CI length at each step.
1201 * If we find any deviations, then this packet is bad.
1203 #define REJCIADDRS(opt, neg, val1, val2) \
1205 (cilen = p[1]) == CILEN_ADDRS && \
1212 cilong = htonl(l); \
1213 /* Check rejected value. */ \
1214 if (cilong != val1) \
1217 cilong = htonl(l); \
1218 /* Check rejected value. */ \
1219 if (cilong != val2) \
1221 try.old_addrs = 0; \
1224 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
1226 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
1231 GETSHORT(cishort, p); \
1232 /* Check rejected value. */ \
1233 if (cishort != val) \
1236 GETCHAR(cimaxslotindex, p); \
1237 if (cimaxslotindex != maxslot) \
1239 GETCHAR(ciflag, p); \
1240 if (ciflag != cflag) \
1246 #define REJCIADDR(opt, neg, val) \
1248 (cilen = p[1]) == CILEN_ADDR && \
1255 cilong = htonl(l); \
1256 /* Check rejected value. */ \
1257 if (cilong != val) \
1262 #define REJCIDNS(opt, neg, dnsaddr) \
1264 ((cilen = p[1]) == CILEN_ADDR) && \
1271 cilong = htonl(l); \
1272 /* Check rejected value. */ \
1273 if (cilong != dnsaddr) \
1279 REJCIADDRS(CI_ADDRS
, !go
->neg_addr
&& go
->old_addrs
,
1280 go
->ouraddr
, go
->hisaddr
);
1282 REJCIVJ(CI_COMPRESSTYPE
, neg_vj
, go
->vj_protocol
, go
->old_vj
,
1283 go
->maxslotindex
, go
->cflag
);
1285 REJCIADDR(CI_ADDR
, neg_addr
, go
->ouraddr
);
1287 REJCIDNS(CI_MS_DNS1
, req_dns1
, go
->dnsaddr
[0]);
1289 REJCIDNS(CI_MS_DNS2
, req_dns2
, go
->dnsaddr
[1]);
1292 * If there are any remaining CIs, then this packet is bad.
1297 * Now we can update state.
1299 if (f
->state
!= OPENED
)
1304 IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
1310 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
1311 * Callback from fsm_rconfreq, Receive Configure Request
1313 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1314 * appropriately. If reject_if_disagree is non-zero, doesn't return
1315 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1318 ipcp_reqci(f
, inp
, len
, reject_if_disagree
)
1320 u_char
*inp
; /* Requested CIs */
1321 int *len
; /* Length of requested CIs */
1322 int reject_if_disagree
;
1324 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
1325 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
1326 ipcp_options
*ao
= &ipcp_allowoptions
[f
->unit
];
1327 u_char
*cip
, *next
; /* Pointer to current and next CIs */
1328 u_short cilen
, citype
; /* Parsed len, type */
1329 u_short cishort
; /* Parsed short value */
1330 u_int32_t tl
, ciaddr1
, ciaddr2
;/* Parsed address values */
1331 int rc
= CONFACK
; /* Final packet return code */
1332 int orc
; /* Individual option return code */
1333 u_char
*p
; /* Pointer to next char to parse */
1334 u_char
*ucp
= inp
; /* Pointer to current output char */
1335 int l
= *len
; /* Length left */
1336 u_char maxslotindex
, cflag
;
1340 * Reset all his options.
1342 BZERO(ho
, sizeof(*ho
));
1345 * Process all his options.
1349 orc
= CONFACK
; /* Assume success */
1350 cip
= p
= next
; /* Remember begining of CI */
1351 if (l
< 2 || /* Not enough data for CI header or */
1352 p
[1] < 2 || /* CI length too small or */
1353 p
[1] > l
) { /* CI length too big? */
1354 IPCPDEBUG(("ipcp_reqci: bad CI length!"));
1355 orc
= CONFREJ
; /* Reject bad CI */
1356 cilen
= l
; /* Reject till end of packet */
1357 l
= 0; /* Don't loop again */
1360 GETCHAR(citype
, p
); /* Parse CI type */
1361 GETCHAR(cilen
, p
); /* Parse CI length */
1362 l
-= cilen
; /* Adjust remaining length */
1363 next
+= cilen
; /* Step to next CI */
1365 switch (citype
) { /* Check CI type */
1367 if (!ao
->old_addrs
|| ho
->neg_addr
||
1368 cilen
!= CILEN_ADDRS
) { /* Check CI length */
1369 orc
= CONFREJ
; /* Reject CI */
1374 * If he has no address, or if we both have his address but
1375 * disagree about it, then NAK it with our idea.
1376 * In particular, if we don't know his address, but he does,
1379 GETLONG(tl
, p
); /* Parse source address (his) */
1380 ciaddr1
= htonl(tl
);
1381 if (ciaddr1
!= wo
->hisaddr
1382 && (ciaddr1
== 0 || !wo
->accept_remote
)) {
1384 if (!reject_if_disagree
) {
1385 DECPTR(sizeof(u_int32_t
), p
);
1386 tl
= ntohl(wo
->hisaddr
);
1389 } else if (ciaddr1
== 0 && wo
->hisaddr
== 0) {
1391 * If neither we nor he knows his address, reject the option.
1394 wo
->req_addr
= 0; /* don't NAK with 0.0.0.0 later */
1399 * If he doesn't know our address, or if we both have our address
1400 * but disagree about it, then NAK it with our idea.
1402 GETLONG(tl
, p
); /* Parse desination address (ours) */
1403 ciaddr2
= htonl(tl
);
1404 if (ciaddr2
!= wo
->ouraddr
) {
1405 if (ciaddr2
== 0 || !wo
->accept_local
) {
1407 if (!reject_if_disagree
) {
1408 DECPTR(sizeof(u_int32_t
), p
);
1409 tl
= ntohl(wo
->ouraddr
);
1413 wo
->ouraddr
= ciaddr2
; /* accept peer's idea */
1418 ho
->hisaddr
= ciaddr1
;
1419 ho
->ouraddr
= ciaddr2
;
1423 if (!ao
->neg_addr
|| ho
->old_addrs
||
1424 cilen
!= CILEN_ADDR
) { /* 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 * Don't ACK an address of 0.0.0.0 - reject it instead.
1450 wo
->req_addr
= 0; /* don't NAK with 0.0.0.0 later */
1455 ho
->hisaddr
= ciaddr1
;
1460 /* Microsoft primary or secondary DNS request */
1461 d
= citype
== CI_MS_DNS2
;
1463 /* If we do not have a DNS address then we cannot send it */
1464 if (ao
->dnsaddr
[d
] == 0 ||
1465 cilen
!= CILEN_ADDR
) { /* Check CI length */
1466 orc
= CONFREJ
; /* Reject CI */
1470 if (htonl(tl
) != ao
->dnsaddr
[d
]) {
1471 DECPTR(sizeof(u_int32_t
), p
);
1472 tl
= ntohl(ao
->dnsaddr
[d
]);
1480 /* Microsoft primary or secondary WINS request */
1481 d
= citype
== CI_MS_WINS2
;
1483 /* If we do not have a DNS address then we cannot send it */
1484 if (ao
->winsaddr
[d
] == 0 ||
1485 cilen
!= CILEN_ADDR
) { /* Check CI length */
1486 orc
= CONFREJ
; /* Reject CI */
1490 if (htonl(tl
) != ao
->winsaddr
[d
]) {
1491 DECPTR(sizeof(u_int32_t
), p
);
1492 tl
= ntohl(ao
->winsaddr
[d
]);
1498 case CI_COMPRESSTYPE
:
1500 (cilen
!= CILEN_VJ
&& cilen
!= CILEN_COMPRESS
)) {
1504 GETSHORT(cishort
, p
);
1506 if (!(cishort
== IPCP_VJ_COMP
||
1507 (cishort
== IPCP_VJ_COMP_OLD
&& cilen
== CILEN_COMPRESS
))) {
1513 ho
->vj_protocol
= cishort
;
1514 if (cilen
== CILEN_VJ
) {
1515 GETCHAR(maxslotindex
, p
);
1516 if (maxslotindex
> ao
->maxslotindex
) {
1518 if (!reject_if_disagree
){
1520 PUTCHAR(ao
->maxslotindex
, p
);
1524 if (cflag
&& !ao
->cflag
) {
1526 if (!reject_if_disagree
){
1528 PUTCHAR(wo
->cflag
, p
);
1531 ho
->maxslotindex
= maxslotindex
;
1535 ho
->maxslotindex
= MAX_STATES
- 1;
1545 if (orc
== CONFACK
&& /* Good CI */
1546 rc
!= CONFACK
) /* but prior CI wasnt? */
1547 continue; /* Don't send this one */
1549 if (orc
== CONFNAK
) { /* Nak this CI? */
1550 if (reject_if_disagree
) /* Getting fed up with sending NAKs? */
1551 orc
= CONFREJ
; /* Get tough if so */
1553 if (rc
== CONFREJ
) /* Rejecting prior CI? */
1554 continue; /* Don't send this one */
1555 if (rc
== CONFACK
) { /* Ack'd all prior CIs? */
1556 rc
= CONFNAK
; /* Not anymore... */
1557 ucp
= inp
; /* Backup */
1562 if (orc
== CONFREJ
&& /* Reject this CI */
1563 rc
!= CONFREJ
) { /* but no prior ones? */
1565 ucp
= inp
; /* Backup */
1568 /* Need to move CI? */
1570 BCOPY(cip
, ucp
, cilen
); /* Move it */
1572 /* Update output pointer */
1577 * If we aren't rejecting this packet, and we want to negotiate
1578 * their address, and they didn't send their address, then we
1579 * send a NAK with a CI_ADDR option appended. We assume the
1580 * input buffer is long enough that we can append the extra
1583 if (rc
!= CONFREJ
&& !ho
->neg_addr
&& !ho
->old_addrs
&&
1584 wo
->req_addr
&& !reject_if_disagree
) {
1585 if (rc
== CONFACK
) {
1587 ucp
= inp
; /* reset pointer */
1588 wo
->req_addr
= 0; /* don't ask again */
1590 PUTCHAR(CI_ADDR
, ucp
);
1591 PUTCHAR(CILEN_ADDR
, ucp
);
1592 tl
= ntohl(wo
->hisaddr
);
1596 *len
= ucp
- inp
; /* Compute output length */
1597 IPCPDEBUG(("ipcp: returning Configure-%s", CODENAME(rc
)));
1598 return (rc
); /* Return final code */
1603 * ip_check_options - check that any IP-related options are OK,
1604 * and assign appropriate defaults.
1611 ipcp_options
*wo
= &ipcp_wantoptions
[0];
1614 * Default our local IP address based on our hostname.
1615 * If local IP address already given, don't bother.
1617 if (wo
->ouraddr
== 0 && !disable_defaultip
) {
1619 * Look up our hostname (possibly with domain name appended)
1620 * and take the first IP address as our local IP address.
1621 * If there isn't an IP address for our hostname, too bad.
1623 wo
->accept_local
= 1; /* don't insist on this default value */
1624 if ((hp
= gethostbyname(hostname
)) != NULL
) {
1625 local
= *(u_int32_t
*)hp
->h_addr
;
1626 if (local
!= 0 && !bad_ip_adrs(local
))
1627 wo
->ouraddr
= local
;
1630 ask_for_local
= wo
->ouraddr
!= 0 || !disable_defaultip
;
1635 * ip_demand_conf - configure the interface as though
1636 * IPCP were up, for use with dial-on-demand.
1642 ipcp_options
*wo
= &ipcp_wantoptions
[u
];
1644 if (wo
->hisaddr
== 0) {
1645 /* make up an arbitrary address for the peer */
1646 wo
->hisaddr
= htonl(0x0a707070 + ifunit
);
1647 wo
->accept_remote
= 1;
1649 if (wo
->ouraddr
== 0) {
1650 /* make up an arbitrary address for us */
1651 wo
->ouraddr
= htonl(0x0a404040 + ifunit
);
1652 wo
->accept_local
= 1;
1653 ask_for_local
= 0; /* don't tell the peer this address */
1655 if (!sifaddr(u
, wo
->ouraddr
, wo
->hisaddr
, GetMask(wo
->ouraddr
)))
1659 if (!sifnpmode(u
, PPP_IP
, NPMODE_QUEUE
))
1661 if (wo
->default_route
)
1662 if (sifdefaultroute(u
, wo
->ouraddr
, wo
->hisaddr
))
1663 default_route_set
[u
] = 1;
1665 if (sifproxyarp(u
, wo
->hisaddr
))
1666 proxy_arp_set
[u
] = 1;
1668 notice("local IP address %I", wo
->ouraddr
);
1669 notice("remote IP address %I", wo
->hisaddr
);
1676 * ipcp_up - IPCP has come UP.
1678 * Configure the IP network interface appropriately and bring it up.
1685 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
1686 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
1687 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
1689 IPCPDEBUG(("ipcp: up"));
1692 * We must have a non-zero IP address for both ends of the link.
1694 if (!ho
->neg_addr
&& !ho
->old_addrs
)
1695 ho
->hisaddr
= wo
->hisaddr
;
1697 if (!(go
->neg_addr
|| go
->old_addrs
) && (wo
->neg_addr
|| wo
->old_addrs
)
1698 && wo
->ouraddr
!= 0) {
1699 error("Peer refused to agree to our IP address");
1700 ipcp_close(f
->unit
, "Refused our IP address");
1703 if (go
->ouraddr
== 0) {
1704 error("Could not determine local IP address");
1705 ipcp_close(f
->unit
, "Could not determine local IP address");
1708 if (ho
->hisaddr
== 0) {
1709 ho
->hisaddr
= htonl(0x0a404040 + ifunit
);
1710 warn("Could not determine remote IP address: defaulting to %I",
1713 script_setenv("IPLOCAL", ip_ntoa(go
->ouraddr
), 0);
1714 script_setenv("IPREMOTE", ip_ntoa(ho
->hisaddr
), 1);
1717 script_setenv("DNS1", ip_ntoa(go
->dnsaddr
[0]), 0);
1719 script_setenv("DNS2", ip_ntoa(go
->dnsaddr
[1]), 0);
1720 if (usepeerdns
&& (go
->dnsaddr
[0] || go
->dnsaddr
[1])) {
1721 script_setenv("USEPEERDNS", "1", 0);
1722 create_resolv(go
->dnsaddr
[0], go
->dnsaddr
[1]);
1726 * Check that the peer is allowed to use the IP address it wants.
1728 if (!auth_ip_addr(f
->unit
, ho
->hisaddr
)) {
1729 error("Peer is not authorized to use remote address %I", ho
->hisaddr
);
1730 ipcp_close(f
->unit
, "Unauthorized remote IP address");
1734 /* set tcp compression */
1735 sifvjcomp(f
->unit
, ho
->neg_vj
, ho
->cflag
, ho
->maxslotindex
);
1738 * If we are doing dial-on-demand, the interface is already
1739 * configured, so we put out any saved-up packets, then set the
1740 * interface to pass IP packets.
1743 if (go
->ouraddr
!= wo
->ouraddr
|| ho
->hisaddr
!= wo
->hisaddr
) {
1744 ipcp_clear_addrs(f
->unit
, wo
->ouraddr
, wo
->hisaddr
);
1745 if (go
->ouraddr
!= wo
->ouraddr
) {
1746 warn("Local IP address changed to %I", go
->ouraddr
);
1747 script_setenv("OLDIPLOCAL", ip_ntoa(wo
->ouraddr
), 0);
1748 wo
->ouraddr
= go
->ouraddr
;
1750 script_unsetenv("OLDIPLOCAL");
1751 if (ho
->hisaddr
!= wo
->hisaddr
) {
1752 warn("Remote IP address changed to %I", ho
->hisaddr
);
1753 script_setenv("OLDIPREMOTE", ip_ntoa(wo
->hisaddr
), 0);
1754 wo
->hisaddr
= ho
->hisaddr
;
1756 script_unsetenv("OLDIPREMOTE");
1758 /* Set the interface to the new addresses */
1759 mask
= GetMask(go
->ouraddr
);
1760 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1762 warn("Interface configuration failed");
1763 ipcp_close(f
->unit
, "Interface configuration failed");
1767 /* assign a default route through the interface if required */
1768 if (ipcp_wantoptions
[f
->unit
].default_route
)
1769 if (sifdefaultroute(f
->unit
, go
->ouraddr
, ho
->hisaddr
))
1770 default_route_set
[f
->unit
] = 1;
1772 /* Make a proxy ARP entry if requested. */
1773 if (ipcp_wantoptions
[f
->unit
].proxy_arp
)
1774 if (sifproxyarp(f
->unit
, ho
->hisaddr
))
1775 proxy_arp_set
[f
->unit
] = 1;
1778 demand_rexmit(PPP_IP
);
1779 sifnpmode(f
->unit
, PPP_IP
, NPMODE_PASS
);
1783 * Set IP addresses and (if specified) netmask.
1785 mask
= GetMask(go
->ouraddr
);
1787 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1788 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1790 warn("Interface configuration failed");
1791 ipcp_close(f
->unit
, "Interface configuration failed");
1796 /* bring the interface up for IP */
1797 if (!sifup(f
->unit
)) {
1799 warn("Interface failed to come up");
1800 ipcp_close(f
->unit
, "Interface configuration failed");
1804 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1805 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1807 warn("Interface configuration failed");
1808 ipcp_close(f
->unit
, "Interface configuration failed");
1812 sifnpmode(f
->unit
, PPP_IP
, NPMODE_PASS
);
1814 /* assign a default route through the interface if required */
1815 if (ipcp_wantoptions
[f
->unit
].default_route
)
1816 if (sifdefaultroute(f
->unit
, go
->ouraddr
, ho
->hisaddr
))
1817 default_route_set
[f
->unit
] = 1;
1819 /* Make a proxy ARP entry if requested. */
1820 if (ipcp_wantoptions
[f
->unit
].proxy_arp
)
1821 if (sifproxyarp(f
->unit
, ho
->hisaddr
))
1822 proxy_arp_set
[f
->unit
] = 1;
1824 ipcp_wantoptions
[0].ouraddr
= go
->ouraddr
;
1826 notice("local IP address %I", go
->ouraddr
);
1827 notice("remote IP address %I", ho
->hisaddr
);
1829 notice("primary DNS address %I", go
->dnsaddr
[0]);
1831 notice("secondary DNS address %I", go
->dnsaddr
[1]);
1834 reset_link_stats(f
->unit
);
1836 np_up(f
->unit
, PPP_IP
);
1839 notify(ip_up_notifier
, 0);
1844 * Execute the ip-up script, like this:
1845 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1847 if (ipcp_script_state
== s_down
&& ipcp_script_pid
== 0) {
1848 ipcp_script_state
= s_up
;
1849 ipcp_script(_PATH_IPUP
);
1855 * ipcp_down - IPCP has gone DOWN.
1857 * Take the IP network interface down, clear its addresses
1858 * and delete routes through it.
1864 IPCPDEBUG(("ipcp: down"));
1865 /* XXX a bit IPv4-centric here, we only need to get the stats
1866 * before the interface is marked down. */
1867 /* XXX more correct: we must get the stats before running the notifiers,
1868 * at least for the radius plugin */
1869 update_link_stats(f
->unit
);
1870 notify(ip_down_notifier
, 0);
1875 np_down(f
->unit
, PPP_IP
);
1877 sifvjcomp(f
->unit
, 0, 0, 0);
1879 print_link_stats(); /* _after_ running the notifiers and ip_down_hook(),
1880 * because print_link_stats() sets link_stats_valid
1884 * If we are doing dial-on-demand, set the interface
1885 * to queue up outgoing packets (for now).
1888 sifnpmode(f
->unit
, PPP_IP
, NPMODE_QUEUE
);
1890 sifnpmode(f
->unit
, PPP_IP
, NPMODE_DROP
);
1892 ipcp_clear_addrs(f
->unit
, ipcp_gotoptions
[f
->unit
].ouraddr
,
1893 ipcp_hisoptions
[f
->unit
].hisaddr
);
1896 /* Execute the ip-down script */
1897 if (ipcp_script_state
== s_up
&& ipcp_script_pid
== 0) {
1898 ipcp_script_state
= s_down
;
1899 ipcp_script(_PATH_IPDOWN
);
1905 * ipcp_clear_addrs() - clear the interface addresses, routes,
1906 * proxy arp entries, etc.
1909 ipcp_clear_addrs(unit
, ouraddr
, hisaddr
)
1911 u_int32_t ouraddr
; /* local address */
1912 u_int32_t hisaddr
; /* remote address */
1914 if (proxy_arp_set
[unit
]) {
1915 cifproxyarp(unit
, hisaddr
);
1916 proxy_arp_set
[unit
] = 0;
1918 if (default_route_set
[unit
]) {
1919 cifdefaultroute(unit
, ouraddr
, hisaddr
);
1920 default_route_set
[unit
] = 0;
1922 cifaddr(unit
, ouraddr
, hisaddr
);
1927 * ipcp_finished - possibly shut down the lower layers.
1935 np_finished(f
->unit
, PPP_IP
);
1941 * ipcp_script_done - called when the ip-up or ip-down script
1945 ipcp_script_done(arg
)
1948 ipcp_script_pid
= 0;
1949 switch (ipcp_script_state
) {
1951 if (ipcp_fsm
[0].state
!= OPENED
) {
1952 ipcp_script_state
= s_down
;
1953 ipcp_script(_PATH_IPDOWN
);
1957 if (ipcp_fsm
[0].state
== OPENED
) {
1958 ipcp_script_state
= s_up
;
1959 ipcp_script(_PATH_IPUP
);
1967 * ipcp_script - Execute a script with arguments
1968 * interface-name tty-name speed local-IP remote-IP.
1974 char strspeed
[32], strlocal
[32], strremote
[32];
1977 slprintf(strspeed
, sizeof(strspeed
), "%d", baud_rate
);
1978 slprintf(strlocal
, sizeof(strlocal
), "%I", ipcp_gotoptions
[0].ouraddr
);
1979 slprintf(strremote
, sizeof(strremote
), "%I", ipcp_hisoptions
[0].hisaddr
);
1986 argv
[5] = strremote
;
1989 ipcp_script_pid
= run_program(script
, argv
, 0, ipcp_script_done
, NULL
);
1993 * create_resolv - create the replacement resolv.conf file
1996 create_resolv(peerdns1
, peerdns2
)
1997 u_int32_t peerdns1
, peerdns2
;
2001 f
= fopen(_PATH_RESOLV
, "w");
2003 error("Failed to create %s: %m", _PATH_RESOLV
);
2008 fprintf(f
, "nameserver %s\n", ip_ntoa(peerdns1
));
2011 fprintf(f
, "nameserver %s\n", ip_ntoa(peerdns2
));
2014 error("Write failed to %s: %m", _PATH_RESOLV
);
2020 * ipcp_printpkt - print the contents of an IPCP packet.
2022 static char *ipcp_codenames
[] = {
2023 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
2024 "TermReq", "TermAck", "CodeRej"
2028 ipcp_printpkt(p
, plen
, printer
, arg
)
2031 void (*printer
) __P((void *, char *, ...));
2034 int code
, id
, len
, olen
;
2035 u_char
*pstart
, *optend
;
2039 if (plen
< HEADERLEN
)
2045 if (len
< HEADERLEN
|| len
> plen
)
2048 if (code
>= 1 && code
<= sizeof(ipcp_codenames
) / sizeof(char *))
2049 printer(arg
, " %s", ipcp_codenames
[code
-1]);
2051 printer(arg
, " code=0x%x", code
);
2052 printer(arg
, " id=0x%x", id
);
2059 /* print option list */
2064 if (olen
< 2 || olen
> len
) {
2072 if (olen
== CILEN_ADDRS
) {
2075 printer(arg
, "addrs %I", htonl(cilong
));
2077 printer(arg
, " %I", htonl(cilong
));
2080 case CI_COMPRESSTYPE
:
2081 if (olen
>= CILEN_COMPRESS
) {
2083 GETSHORT(cishort
, p
);
2084 printer(arg
, "compress ");
2089 case IPCP_VJ_COMP_OLD
:
2090 printer(arg
, "old-VJ");
2093 printer(arg
, "0x%x", cishort
);
2098 if (olen
== CILEN_ADDR
) {
2101 printer(arg
, "addr %I", htonl(cilong
));
2108 printer(arg
, "ms-dns%d %I", code
- CI_MS_DNS1
+ 1,
2115 printer(arg
, "ms-wins %I", htonl(cilong
));
2118 while (p
< optend
) {
2120 printer(arg
, " %.2x", code
);
2128 if (len
> 0 && *p
>= ' ' && *p
< 0x7f) {
2130 print_string((char *)p
, len
, printer
, arg
);
2137 /* print the rest of the bytes in the packet */
2138 for (; len
> 0; --len
) {
2140 printer(arg
, " %.2x", code
);
2147 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
2148 * We don't bring the link up for IP fragments or for TCP FIN packets
2151 #define IP_HDRLEN 20 /* bytes */
2152 #define IP_OFFMASK 0x1fff
2154 #define IPPROTO_TCP 6
2156 #define TCP_HDRLEN 20
2160 * We use these macros because the IP header may be at an odd address,
2161 * and some compilers might use word loads to get th_off or ip_hl.
2164 #define net_short(x) (((x)[0] << 8) + (x)[1])
2165 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
2166 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
2167 #define get_ipproto(x) (((unsigned char *)(x))[9])
2168 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
2169 #define get_tcpflags(x) (((unsigned char *)(x))[13])
2172 ip_active_pkt(pkt
, len
)
2181 if (len
< IP_HDRLEN
)
2183 if ((get_ipoff(pkt
) & IP_OFFMASK
) != 0)
2185 if (get_ipproto(pkt
) != IPPROTO_TCP
)
2187 hlen
= get_iphl(pkt
) * 4;
2188 if (len
< hlen
+ TCP_HDRLEN
)
2191 if ((get_tcpflags(tcp
) & TH_FIN
) != 0 && len
== hlen
+ get_tcpoff(tcp
) * 4)