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.73 2008/05/26 08:33:22 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 */
75 bool noremoteip
= 0; /* Let him have no IP address */
77 /* Hook for a plugin to know when IP protocol has come up */
78 void (*ip_up_hook
) __P((void)) = NULL
;
80 /* Hook for a plugin to know when IP protocol has come down */
81 void (*ip_down_hook
) __P((void)) = NULL
;
83 /* Hook for a plugin to choose the remote IP address */
84 void (*ip_choose_hook
) __P((u_int32_t
*)) = NULL
;
86 /* Notifiers for when IPCP goes up and down */
87 struct notifier
*ip_up_notifier
= NULL
;
88 struct notifier
*ip_down_notifier
= NULL
;
91 static int default_route_set
[NUM_PPP
]; /* Have set up a default route */
92 static int proxy_arp_set
[NUM_PPP
]; /* Have created proxy arp entry */
93 static bool usepeerdns
; /* Ask peer for DNS addrs */
94 static int ipcp_is_up
; /* have called np_up() */
95 static int ipcp_is_open
; /* haven't called np_finished() */
96 static bool ask_for_local
; /* request our address from peer */
97 static char vj_value
[8]; /* string form of vj option value */
98 static char netmask_str
[20]; /* string form of netmask value */
101 * Callbacks for fsm code. (CI = Configuration Information)
103 static void ipcp_resetci
__P((fsm
*)); /* Reset our CI */
104 static int ipcp_cilen
__P((fsm
*)); /* Return length of our CI */
105 static void ipcp_addci
__P((fsm
*, u_char
*, int *)); /* Add our CI */
106 static int ipcp_ackci
__P((fsm
*, u_char
*, int)); /* Peer ack'd our CI */
107 static int ipcp_nakci
__P((fsm
*, u_char
*, int, int));/* Peer nak'd our CI */
108 static int ipcp_rejci
__P((fsm
*, u_char
*, int)); /* Peer rej'd our CI */
109 static int ipcp_reqci
__P((fsm
*, u_char
*, int *, int)); /* Rcv CI */
110 static void ipcp_up
__P((fsm
*)); /* We're UP */
111 static void ipcp_down
__P((fsm
*)); /* We're DOWN */
112 static void ipcp_finished
__P((fsm
*)); /* Don't need lower layer */
114 fsm ipcp_fsm
[NUM_PPP
]; /* IPCP fsm structure */
116 static fsm_callbacks ipcp_callbacks
= { /* IPCP callback routines */
117 ipcp_resetci
, /* Reset our Configuration Information */
118 ipcp_cilen
, /* Length of our Configuration Information */
119 ipcp_addci
, /* Add our Configuration Information */
120 ipcp_ackci
, /* ACK our Configuration Information */
121 ipcp_nakci
, /* NAK our Configuration Information */
122 ipcp_rejci
, /* Reject our Configuration Information */
123 ipcp_reqci
, /* Request peer's Configuration Information */
124 ipcp_up
, /* Called when fsm reaches OPENED state */
125 ipcp_down
, /* Called when fsm leaves OPENED state */
126 NULL
, /* Called when we want the lower layer up */
127 ipcp_finished
, /* Called when we want the lower layer down */
128 NULL
, /* Called when Protocol-Reject received */
129 NULL
, /* Retransmission is necessary */
130 NULL
, /* Called to handle protocol-specific codes */
131 "IPCP" /* String name of protocol */
135 * Command-line options.
137 static int setvjslots
__P((char **));
138 static int setdnsaddr
__P((char **));
139 static int setwinsaddr
__P((char **));
140 static int setnetmask
__P((char **));
141 int setipaddr
__P((char *, char **, int));
142 static void printipaddr
__P((option_t
*, void (*)(void *, char *,...),void *));
144 static option_t ipcp_option_list
[] = {
145 { "noip", o_bool
, &ipcp_protent
.enabled_flag
,
146 "Disable IP and IPCP" },
147 { "-ip", o_bool
, &ipcp_protent
.enabled_flag
,
148 "Disable IP and IPCP", OPT_ALIAS
},
150 { "novj", o_bool
, &ipcp_wantoptions
[0].neg_vj
,
151 "Disable VJ compression", OPT_A2CLR
, &ipcp_allowoptions
[0].neg_vj
},
152 { "-vj", o_bool
, &ipcp_wantoptions
[0].neg_vj
,
153 "Disable VJ compression", OPT_ALIAS
| OPT_A2CLR
,
154 &ipcp_allowoptions
[0].neg_vj
},
156 { "novjccomp", o_bool
, &ipcp_wantoptions
[0].cflag
,
157 "Disable VJ connection-ID compression", OPT_A2CLR
,
158 &ipcp_allowoptions
[0].cflag
},
159 { "-vjccomp", o_bool
, &ipcp_wantoptions
[0].cflag
,
160 "Disable VJ connection-ID compression", OPT_ALIAS
| OPT_A2CLR
,
161 &ipcp_allowoptions
[0].cflag
},
163 { "vj-max-slots", o_special
, (void *)setvjslots
,
164 "Set maximum VJ header slots",
165 OPT_PRIO
| OPT_A2STRVAL
| OPT_STATIC
, vj_value
},
167 { "ipcp-accept-local", o_bool
, &ipcp_wantoptions
[0].accept_local
,
168 "Accept peer's address for us", 1 },
169 { "ipcp-accept-remote", o_bool
, &ipcp_wantoptions
[0].accept_remote
,
170 "Accept peer's address for it", 1 },
172 { "ipparam", o_string
, &ipparam
,
173 "Set ip script parameter", OPT_PRIO
},
175 { "noipdefault", o_bool
, &disable_defaultip
,
176 "Don't use name for default IP adrs", 1 },
178 { "ms-dns", 1, (void *)setdnsaddr
,
179 "DNS address for the peer's use" },
180 { "ms-wins", 1, (void *)setwinsaddr
,
181 "Nameserver for SMB over TCP/IP for peer" },
183 { "ipcp-restart", o_int
, &ipcp_fsm
[0].timeouttime
,
184 "Set timeout for IPCP", OPT_PRIO
},
185 { "ipcp-max-terminate", o_int
, &ipcp_fsm
[0].maxtermtransmits
,
186 "Set max #xmits for term-reqs", OPT_PRIO
},
187 { "ipcp-max-configure", o_int
, &ipcp_fsm
[0].maxconfreqtransmits
,
188 "Set max #xmits for conf-reqs", OPT_PRIO
},
189 { "ipcp-max-failure", o_int
, &ipcp_fsm
[0].maxnakloops
,
190 "Set max #conf-naks for IPCP", OPT_PRIO
},
192 { "defaultroute", o_bool
, &ipcp_wantoptions
[0].default_route
,
193 "Add default route", OPT_ENABLE
|1, &ipcp_allowoptions
[0].default_route
},
194 { "nodefaultroute", o_bool
, &ipcp_allowoptions
[0].default_route
,
195 "disable defaultroute option", OPT_A2CLR
,
196 &ipcp_wantoptions
[0].default_route
},
197 { "-defaultroute", o_bool
, &ipcp_allowoptions
[0].default_route
,
198 "disable defaultroute option", OPT_ALIAS
| OPT_A2CLR
,
199 &ipcp_wantoptions
[0].default_route
},
201 { "proxyarp", o_bool
, &ipcp_wantoptions
[0].proxy_arp
,
202 "Add proxy ARP entry", OPT_ENABLE
|1, &ipcp_allowoptions
[0].proxy_arp
},
203 { "noproxyarp", o_bool
, &ipcp_allowoptions
[0].proxy_arp
,
204 "disable proxyarp option", OPT_A2CLR
,
205 &ipcp_wantoptions
[0].proxy_arp
},
206 { "-proxyarp", o_bool
, &ipcp_allowoptions
[0].proxy_arp
,
207 "disable proxyarp option", OPT_ALIAS
| OPT_A2CLR
,
208 &ipcp_wantoptions
[0].proxy_arp
},
210 { "usepeerdns", o_bool
, &usepeerdns
,
211 "Ask peer for DNS address(es)", 1 },
213 { "netmask", o_special
, (void *)setnetmask
,
214 "set netmask", OPT_PRIO
| OPT_A2STRVAL
| OPT_STATIC
, netmask_str
},
216 { "ipcp-no-addresses", o_bool
, &ipcp_wantoptions
[0].old_addrs
,
217 "Disable old-style IP-Addresses usage", OPT_A2CLR
,
218 &ipcp_allowoptions
[0].old_addrs
},
219 { "ipcp-no-address", o_bool
, &ipcp_wantoptions
[0].neg_addr
,
220 "Disable IP-Address usage", OPT_A2CLR
,
221 &ipcp_allowoptions
[0].neg_addr
},
223 { "noremoteip", o_bool
, &noremoteip
,
224 "Allow peer to have no IP address", 1 },
226 { "nosendip", o_bool
, &ipcp_wantoptions
[0].neg_addr
,
227 "Don't send our IP address to peer", OPT_A2CLR
,
228 &ipcp_wantoptions
[0].old_addrs
},
230 { "IP addresses", o_wild
, (void *) &setipaddr
,
231 "set local and remote IP addresses",
232 OPT_NOARG
| OPT_A2PRINTER
, (void *) &printipaddr
},
238 * Protocol entry points from main code.
240 static void ipcp_init
__P((int));
241 static void ipcp_open
__P((int));
242 static void ipcp_close
__P((int, char *));
243 static void ipcp_lowerup
__P((int));
244 static void ipcp_lowerdown
__P((int));
245 static void ipcp_input
__P((int, u_char
*, int));
246 static void ipcp_protrej
__P((int));
247 static int ipcp_printpkt
__P((u_char
*, int,
248 void (*) __P((void *, char *, ...)), void *));
249 static void ip_check_options
__P((void));
250 static int ip_demand_conf
__P((int));
251 static int ip_active_pkt
__P((u_char
*, int));
252 static void create_resolv
__P((u_int32_t
, u_int32_t
));
254 struct protent ipcp_protent
= {
274 static void ipcp_clear_addrs
__P((int, u_int32_t
, u_int32_t
));
275 static void ipcp_script
__P((char *, int)); /* Run an up/down script */
276 static void ipcp_script_done
__P((void *));
279 * Lengths of configuration options.
282 #define CILEN_COMPRESS 4 /* min length for compression protocol opt. */
283 #define CILEN_VJ 6 /* length for RFC1332 Van-Jacobson opt. */
284 #define CILEN_ADDR 6 /* new-style single address option */
285 #define CILEN_ADDRS 10 /* old-style dual address option */
288 #define CODENAME(x) ((x) == CONFACK ? "ACK" : \
289 (x) == CONFNAK ? "NAK" : "REJ")
292 * This state variable is used to ensure that we don't
293 * run an ipcp-up/down script while one is already running.
295 static enum script_state
{
299 static pid_t ipcp_script_pid
;
302 * Make a string representation of a network IP address.
310 slprintf(b
, sizeof(b
), "%I", ipaddr
);
319 * setvjslots - set maximum number of connection slots for VJ compression
327 if (!int_option(*argv
, &value
))
329 if (value
< 2 || value
> 16) {
330 option_error("vj-max-slots value must be between 2 and 16");
333 ipcp_wantoptions
[0].maxslotindex
=
334 ipcp_allowoptions
[0].maxslotindex
= value
- 1;
335 slprintf(vj_value
, sizeof(vj_value
), "%d", value
);
340 * setdnsaddr - set the dns address(es)
349 dns
= inet_addr(*argv
);
350 if (dns
== (u_int32_t
) -1) {
351 if ((hp
= gethostbyname(*argv
)) == NULL
) {
352 option_error("invalid address parameter '%s' for ms-dns option",
356 dns
= *(u_int32_t
*)hp
->h_addr
;
359 /* We take the last 2 values given, the 2nd-last as the primary
360 and the last as the secondary. If only one is given it
361 becomes both primary and secondary. */
362 if (ipcp_allowoptions
[0].dnsaddr
[1] == 0)
363 ipcp_allowoptions
[0].dnsaddr
[0] = dns
;
365 ipcp_allowoptions
[0].dnsaddr
[0] = ipcp_allowoptions
[0].dnsaddr
[1];
367 /* always set the secondary address value. */
368 ipcp_allowoptions
[0].dnsaddr
[1] = dns
;
374 * setwinsaddr - set the wins address(es)
375 * This is primrarly used with the Samba package under UNIX or for pointing
376 * the caller to the existing WINS server on a Windows NT platform.
385 wins
= inet_addr(*argv
);
386 if (wins
== (u_int32_t
) -1) {
387 if ((hp
= gethostbyname(*argv
)) == NULL
) {
388 option_error("invalid address parameter '%s' for ms-wins option",
392 wins
= *(u_int32_t
*)hp
->h_addr
;
395 /* We take the last 2 values given, the 2nd-last as the primary
396 and the last as the secondary. If only one is given it
397 becomes both primary and secondary. */
398 if (ipcp_allowoptions
[0].winsaddr
[1] == 0)
399 ipcp_allowoptions
[0].winsaddr
[0] = wins
;
401 ipcp_allowoptions
[0].winsaddr
[0] = ipcp_allowoptions
[0].winsaddr
[1];
403 /* always set the secondary address value. */
404 ipcp_allowoptions
[0].winsaddr
[1] = wins
;
410 * setipaddr - Set the IP address
411 * If doit is 0, the call is to check whether this option is
412 * potentially an IP address specification.
413 * Not static so that plugins can call it to set the addresses
416 setipaddr(arg
, argv
, doit
)
423 u_int32_t local
, remote
;
424 ipcp_options
*wo
= &ipcp_wantoptions
[0];
425 static int prio_local
= 0, prio_remote
= 0;
428 * IP address pair separated by ":".
430 if ((colon
= strchr(arg
, ':')) == NULL
)
436 * If colon first character, then no local addr.
438 if (colon
!= arg
&& option_priority
>= prio_local
) {
440 if ((local
= inet_addr(arg
)) == (u_int32_t
) -1) {
441 if ((hp
= gethostbyname(arg
)) == NULL
) {
442 option_error("unknown host: %s", arg
);
445 local
= *(u_int32_t
*)hp
->h_addr
;
447 if (bad_ip_adrs(local
)) {
448 option_error("bad local IP address %s", ip_ntoa(local
));
454 prio_local
= option_priority
;
458 * If colon last character, then no remote addr.
460 if (*++colon
!= '\0' && option_priority
>= prio_remote
) {
461 if ((remote
= inet_addr(colon
)) == (u_int32_t
) -1) {
462 if ((hp
= gethostbyname(colon
)) == NULL
) {
463 option_error("unknown host: %s", colon
);
466 remote
= *(u_int32_t
*)hp
->h_addr
;
467 if (remote_name
[0] == 0)
468 strlcpy(remote_name
, colon
, sizeof(remote_name
));
470 if (bad_ip_adrs(remote
)) {
471 option_error("bad remote IP address %s", ip_ntoa(remote
));
475 wo
->hisaddr
= remote
;
476 prio_remote
= option_priority
;
483 printipaddr(opt
, printer
, arg
)
485 void (*printer
) __P((void *, char *, ...));
488 ipcp_options
*wo
= &ipcp_wantoptions
[0];
490 if (wo
->ouraddr
!= 0)
491 printer(arg
, "%I", wo
->ouraddr
);
493 if (wo
->hisaddr
!= 0)
494 printer(arg
, "%I", wo
->hisaddr
);
498 * setnetmask - set the netmask to be used on the interface.
509 * Unfortunately, if we use inet_addr, we can't tell whether
510 * a result of all 1s is an error or a valid 255.255.255.255.
513 n
= parse_dotted_ip(p
, &mask
);
517 if (n
== 0 || p
[n
] != 0 || (netmask
& ~mask
) != 0) {
518 option_error("invalid netmask value '%s'", *argv
);
523 slprintf(netmask_str
, sizeof(netmask_str
), "%I", mask
);
529 parse_dotted_ip(p
, vp
)
539 b
= strtoul(p
, &endp
, 0);
545 /* accept e.g. 0xffffff00 */
563 * ipcp_init - Initialize IPCP.
569 fsm
*f
= &ipcp_fsm
[unit
];
570 ipcp_options
*wo
= &ipcp_wantoptions
[unit
];
571 ipcp_options
*ao
= &ipcp_allowoptions
[unit
];
574 f
->protocol
= PPP_IPCP
;
575 f
->callbacks
= &ipcp_callbacks
;
576 fsm_init(&ipcp_fsm
[unit
]);
579 * Some 3G modems use repeated IPCP NAKs as a way of stalling
580 * until they can contact a server on the network, so we increase
581 * the default number of NAKs we accept before we start treating
584 f
->maxnakloops
= 100;
586 memset(wo
, 0, sizeof(*wo
));
587 memset(ao
, 0, sizeof(*ao
));
589 wo
->neg_addr
= wo
->old_addrs
= 1;
591 wo
->vj_protocol
= IPCP_VJ_COMP
;
592 wo
->maxslotindex
= MAX_STATES
- 1; /* really max index */
596 /* max slots and slot-id compression are currently hardwired in */
597 /* ppp_if.c to 16 and 1, this needs to be changed (among other */
600 ao
->neg_addr
= ao
->old_addrs
= 1;
602 ao
->maxslotindex
= MAX_STATES
- 1;
606 * XXX These control whether the user may use the proxyarp
607 * and defaultroute options.
610 ao
->default_route
= 1;
615 * ipcp_open - IPCP is allowed to come up.
621 fsm_open(&ipcp_fsm
[unit
]);
627 * ipcp_close - Take IPCP down.
630 ipcp_close(unit
, reason
)
634 fsm_close(&ipcp_fsm
[unit
], reason
);
639 * ipcp_lowerup - The lower layer is up.
645 fsm_lowerup(&ipcp_fsm
[unit
]);
650 * ipcp_lowerdown - The lower layer is down.
656 fsm_lowerdown(&ipcp_fsm
[unit
]);
661 * ipcp_input - Input IPCP packet.
664 ipcp_input(unit
, p
, len
)
669 fsm_input(&ipcp_fsm
[unit
], p
, len
);
674 * ipcp_protrej - A Protocol-Reject was received for IPCP.
676 * Pretend the lower layer went down, so we shut up.
682 fsm_lowerdown(&ipcp_fsm
[unit
]);
687 * ipcp_resetci - Reset our CI.
688 * Called by fsm_sconfreq, Send Configure Request.
694 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
695 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
696 ipcp_options
*ao
= &ipcp_allowoptions
[f
->unit
];
698 wo
->req_addr
= (wo
->neg_addr
|| wo
->old_addrs
) &&
699 (ao
->neg_addr
|| ao
->old_addrs
);
700 if (wo
->ouraddr
== 0)
701 wo
->accept_local
= 1;
702 if (wo
->hisaddr
== 0)
703 wo
->accept_remote
= 1;
704 wo
->req_dns1
= usepeerdns
; /* Request DNS addresses from the peer */
705 wo
->req_dns2
= usepeerdns
;
709 if (ip_choose_hook
) {
710 ip_choose_hook(&wo
->hisaddr
);
712 wo
->accept_remote
= 0;
715 BZERO(&ipcp_hisoptions
[f
->unit
], sizeof(ipcp_options
));
720 * ipcp_cilen - Return length of our CI.
721 * Called by fsm_sconfreq, Send Configure Request.
727 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
728 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
729 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
731 #define LENCIADDRS(neg) (neg ? CILEN_ADDRS : 0)
732 #define LENCIVJ(neg, old) (neg ? (old? CILEN_COMPRESS : CILEN_VJ) : 0)
733 #define LENCIADDR(neg) (neg ? CILEN_ADDR : 0)
734 #define LENCIDNS(neg) LENCIADDR(neg)
735 #define LENCIWINS(neg) LENCIADDR(neg)
738 * First see if we want to change our options to the old
739 * forms because we have received old forms from the peer.
741 if (go
->neg_addr
&& go
->old_addrs
&& !ho
->neg_addr
&& ho
->old_addrs
)
743 if (wo
->neg_vj
&& !go
->neg_vj
&& !go
->old_vj
) {
744 /* try an older style of VJ negotiation */
745 /* use the old style only if the peer did */
746 if (ho
->neg_vj
&& ho
->old_vj
) {
749 go
->vj_protocol
= ho
->vj_protocol
;
753 return (LENCIADDRS(!go
->neg_addr
&& go
->old_addrs
) +
754 LENCIVJ(go
->neg_vj
, go
->old_vj
) +
755 LENCIADDR(go
->neg_addr
) +
756 LENCIDNS(go
->req_dns1
) +
757 LENCIDNS(go
->req_dns2
) +
758 LENCIWINS(go
->winsaddr
[0]) +
759 LENCIWINS(go
->winsaddr
[1])) ;
764 * ipcp_addci - Add our desired CIs to a packet.
765 * Called by fsm_sconfreq, Send Configure Request.
768 ipcp_addci(f
, ucp
, lenp
)
773 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
776 #define ADDCIADDRS(opt, neg, val1, val2) \
778 if (len >= CILEN_ADDRS) { \
781 PUTCHAR(CILEN_ADDRS, ucp); \
786 len -= CILEN_ADDRS; \
791 #define ADDCIVJ(opt, neg, val, old, maxslotindex, cflag) \
793 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
794 if (len >= vjlen) { \
796 PUTCHAR(vjlen, ucp); \
797 PUTSHORT(val, ucp); \
799 PUTCHAR(maxslotindex, ucp); \
800 PUTCHAR(cflag, ucp); \
807 #define ADDCIADDR(opt, neg, val) \
809 if (len >= CILEN_ADDR) { \
812 PUTCHAR(CILEN_ADDR, ucp); \
820 #define ADDCIDNS(opt, neg, addr) \
822 if (len >= CILEN_ADDR) { \
825 PUTCHAR(CILEN_ADDR, ucp); \
833 #define ADDCIWINS(opt, addr) \
835 if (len >= CILEN_ADDR) { \
838 PUTCHAR(CILEN_ADDR, ucp); \
846 ADDCIADDRS(CI_ADDRS
, !go
->neg_addr
&& go
->old_addrs
, go
->ouraddr
,
849 ADDCIVJ(CI_COMPRESSTYPE
, go
->neg_vj
, go
->vj_protocol
, go
->old_vj
,
850 go
->maxslotindex
, go
->cflag
);
852 ADDCIADDR(CI_ADDR
, go
->neg_addr
, go
->ouraddr
);
854 ADDCIDNS(CI_MS_DNS1
, go
->req_dns1
, go
->dnsaddr
[0]);
856 ADDCIDNS(CI_MS_DNS2
, go
->req_dns2
, go
->dnsaddr
[1]);
858 ADDCIWINS(CI_MS_WINS1
, go
->winsaddr
[0]);
860 ADDCIWINS(CI_MS_WINS2
, go
->winsaddr
[1]);
867 * ipcp_ackci - Ack our CIs.
868 * Called by fsm_rconfack, Receive Configure ACK.
875 ipcp_ackci(f
, p
, len
)
880 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
881 u_short cilen
, citype
, cishort
;
883 u_char cimaxslotindex
, cicflag
;
886 * CIs must be in exactly the same order that we sent...
887 * Check packet length and CI length at each step.
888 * If we find any deviations, then this packet is bad.
891 #define ACKCIADDRS(opt, neg, val1, val2) \
894 if ((len -= CILEN_ADDRS) < 0) \
896 GETCHAR(citype, p); \
898 if (cilen != CILEN_ADDRS || \
903 if (val1 != cilong) \
907 if (val2 != cilong) \
911 #define ACKCIVJ(opt, neg, val, old, maxslotindex, cflag) \
913 int vjlen = old? CILEN_COMPRESS : CILEN_VJ; \
914 if ((len -= vjlen) < 0) \
916 GETCHAR(citype, p); \
918 if (cilen != vjlen || \
921 GETSHORT(cishort, p); \
922 if (cishort != val) \
925 GETCHAR(cimaxslotindex, p); \
926 if (cimaxslotindex != maxslotindex) \
928 GETCHAR(cicflag, p); \
929 if (cicflag != cflag) \
934 #define ACKCIADDR(opt, neg, val) \
937 if ((len -= CILEN_ADDR) < 0) \
939 GETCHAR(citype, p); \
941 if (cilen != CILEN_ADDR || \
950 #define ACKCIDNS(opt, neg, addr) \
953 if ((len -= CILEN_ADDR) < 0) \
955 GETCHAR(citype, p); \
957 if (cilen != CILEN_ADDR || citype != opt) \
961 if (addr != cilong) \
965 ACKCIADDRS(CI_ADDRS
, !go
->neg_addr
&& go
->old_addrs
, go
->ouraddr
,
968 ACKCIVJ(CI_COMPRESSTYPE
, go
->neg_vj
, go
->vj_protocol
, go
->old_vj
,
969 go
->maxslotindex
, go
->cflag
);
971 ACKCIADDR(CI_ADDR
, go
->neg_addr
, go
->ouraddr
);
973 ACKCIDNS(CI_MS_DNS1
, go
->req_dns1
, go
->dnsaddr
[0]);
975 ACKCIDNS(CI_MS_DNS2
, go
->req_dns2
, go
->dnsaddr
[1]);
978 * If there are any remaining CIs, then this packet is bad.
985 IPCPDEBUG(("ipcp_ackci: received bad Ack!"));
990 * ipcp_nakci - Peer has sent a NAK for some of our CIs.
991 * This should not modify any state if the Nak is bad
992 * or if IPCP is in the OPENED state.
993 * Calback from fsm_rconfnakrej - Receive Configure-Nak or Configure-Reject.
1000 ipcp_nakci(f
, p
, len
, treat_as_reject
)
1004 int treat_as_reject
;
1006 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
1007 u_char cimaxslotindex
, cicflag
;
1008 u_char citype
, cilen
, *next
;
1010 u_int32_t ciaddr1
, ciaddr2
, l
, cidnsaddr
;
1011 ipcp_options no
; /* options we've seen Naks for */
1012 ipcp_options
try; /* options to request next time */
1014 BZERO(&no
, sizeof(no
));
1018 * Any Nak'd CIs must be in exactly the same order that we sent.
1019 * Check packet length and CI length at each step.
1020 * If we find any deviations, then this packet is bad.
1022 #define NAKCIADDRS(opt, neg, code) \
1024 (cilen = p[1]) == CILEN_ADDRS && \
1030 ciaddr1 = htonl(l); \
1032 ciaddr2 = htonl(l); \
1037 #define NAKCIVJ(opt, neg, code) \
1039 ((cilen = p[1]) == CILEN_COMPRESS || cilen == CILEN_VJ) && \
1044 GETSHORT(cishort, p); \
1049 #define NAKCIADDR(opt, neg, code) \
1051 (cilen = p[1]) == CILEN_ADDR && \
1057 ciaddr1 = htonl(l); \
1062 #define NAKCIDNS(opt, neg, code) \
1064 ((cilen = p[1]) == CILEN_ADDR) && \
1070 cidnsaddr = htonl(l); \
1076 * Accept the peer's idea of {our,his} address, if different
1077 * from our idea, only if the accept_{local,remote} flag is set.
1079 NAKCIADDRS(CI_ADDRS
, !go
->neg_addr
&& go
->old_addrs
,
1080 if (treat_as_reject
) {
1083 if (go
->accept_local
&& ciaddr1
) {
1084 /* take his idea of our address */
1085 try.ouraddr
= ciaddr1
;
1087 if (go
->accept_remote
&& ciaddr2
) {
1088 /* take his idea of his address */
1089 try.hisaddr
= ciaddr2
;
1095 * Accept the peer's value of maxslotindex provided that it
1096 * is less than what we asked for. Turn off slot-ID compression
1097 * if the peer wants. Send old-style compress-type option if
1100 NAKCIVJ(CI_COMPRESSTYPE
, neg_vj
,
1101 if (treat_as_reject
) {
1103 } else if (cilen
== CILEN_VJ
) {
1104 GETCHAR(cimaxslotindex
, p
);
1105 GETCHAR(cicflag
, p
);
1106 if (cishort
== IPCP_VJ_COMP
) {
1108 if (cimaxslotindex
< go
->maxslotindex
)
1109 try.maxslotindex
= cimaxslotindex
;
1116 if (cishort
== IPCP_VJ_COMP
|| cishort
== IPCP_VJ_COMP_OLD
) {
1118 try.vj_protocol
= cishort
;
1125 NAKCIADDR(CI_ADDR
, neg_addr
,
1126 if (treat_as_reject
) {
1129 } else if (go
->accept_local
&& ciaddr1
) {
1130 /* take his idea of our address */
1131 try.ouraddr
= ciaddr1
;
1135 NAKCIDNS(CI_MS_DNS1
, req_dns1
,
1136 if (treat_as_reject
) {
1139 try.dnsaddr
[0] = cidnsaddr
;
1143 NAKCIDNS(CI_MS_DNS2
, req_dns2
,
1144 if (treat_as_reject
) {
1147 try.dnsaddr
[1] = cidnsaddr
;
1152 * There may be remaining CIs, if the peer is requesting negotiation
1153 * on an option that we didn't include in our request packet.
1154 * If they want to negotiate about IP addresses, we comply.
1155 * If they want us to ask for compression, we refuse.
1156 * If they want us to ask for ms-dns, we do that, since some
1157 * peers get huffy if we don't.
1159 while (len
>= CILEN_VOID
) {
1162 if ( cilen
< CILEN_VOID
|| (len
-= cilen
) < 0 )
1164 next
= p
+ cilen
- 2;
1167 case CI_COMPRESSTYPE
:
1168 if (go
->neg_vj
|| no
.neg_vj
||
1169 (cilen
!= CILEN_VJ
&& cilen
!= CILEN_COMPRESS
))
1174 if ((!go
->neg_addr
&& go
->old_addrs
) || no
.old_addrs
1175 || cilen
!= CILEN_ADDRS
)
1180 if (ciaddr1
&& go
->accept_local
)
1181 try.ouraddr
= ciaddr1
;
1184 if (ciaddr2
&& go
->accept_remote
)
1185 try.hisaddr
= ciaddr2
;
1189 if (go
->neg_addr
|| no
.neg_addr
|| cilen
!= CILEN_ADDR
)
1194 if (ciaddr1
&& go
->accept_local
)
1195 try.ouraddr
= ciaddr1
;
1196 if (try.ouraddr
!= 0)
1201 if (go
->req_dns1
|| no
.req_dns1
|| cilen
!= CILEN_ADDR
)
1204 try.dnsaddr
[0] = htonl(l
);
1209 if (go
->req_dns2
|| no
.req_dns2
|| cilen
!= CILEN_ADDR
)
1212 try.dnsaddr
[1] = htonl(l
);
1218 if (cilen
!= CILEN_ADDR
)
1223 try.winsaddr
[citype
== CI_MS_WINS2
] = ciaddr1
;
1230 * OK, the Nak is good. Now we can update state.
1231 * If there are any remaining options, we ignore them.
1233 if (f
->state
!= OPENED
)
1239 IPCPDEBUG(("ipcp_nakci: received bad Nak!"));
1245 * ipcp_rejci - Reject some of our CIs.
1246 * Callback from fsm_rconfnakrej.
1249 ipcp_rejci(f
, p
, len
)
1254 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
1255 u_char cimaxslotindex
, ciflag
, cilen
;
1258 ipcp_options
try; /* options to request next time */
1262 * Any Rejected CIs must be in exactly the same order that we sent.
1263 * Check packet length and CI length at each step.
1264 * If we find any deviations, then this packet is bad.
1266 #define REJCIADDRS(opt, neg, val1, val2) \
1268 (cilen = p[1]) == CILEN_ADDRS && \
1275 cilong = htonl(l); \
1276 /* Check rejected value. */ \
1277 if (cilong != val1) \
1280 cilong = htonl(l); \
1281 /* Check rejected value. */ \
1282 if (cilong != val2) \
1284 try.old_addrs = 0; \
1287 #define REJCIVJ(opt, neg, val, old, maxslot, cflag) \
1289 p[1] == (old? CILEN_COMPRESS : CILEN_VJ) && \
1294 GETSHORT(cishort, p); \
1295 /* Check rejected value. */ \
1296 if (cishort != val) \
1299 GETCHAR(cimaxslotindex, p); \
1300 if (cimaxslotindex != maxslot) \
1302 GETCHAR(ciflag, p); \
1303 if (ciflag != cflag) \
1309 #define REJCIADDR(opt, neg, val) \
1311 (cilen = p[1]) == CILEN_ADDR && \
1318 cilong = htonl(l); \
1319 /* Check rejected value. */ \
1320 if (cilong != val) \
1325 #define REJCIDNS(opt, neg, dnsaddr) \
1327 ((cilen = p[1]) == CILEN_ADDR) && \
1334 cilong = htonl(l); \
1335 /* Check rejected value. */ \
1336 if (cilong != dnsaddr) \
1341 #define REJCIWINS(opt, addr) \
1343 ((cilen = p[1]) == CILEN_ADDR) && \
1350 cilong = htonl(l); \
1351 /* Check rejected value. */ \
1352 if (cilong != addr) \
1354 try.winsaddr[opt == CI_MS_WINS2] = 0; \
1357 REJCIADDRS(CI_ADDRS
, !go
->neg_addr
&& go
->old_addrs
,
1358 go
->ouraddr
, go
->hisaddr
);
1360 REJCIVJ(CI_COMPRESSTYPE
, neg_vj
, go
->vj_protocol
, go
->old_vj
,
1361 go
->maxslotindex
, go
->cflag
);
1363 REJCIADDR(CI_ADDR
, neg_addr
, go
->ouraddr
);
1365 REJCIDNS(CI_MS_DNS1
, req_dns1
, go
->dnsaddr
[0]);
1367 REJCIDNS(CI_MS_DNS2
, req_dns2
, go
->dnsaddr
[1]);
1369 REJCIWINS(CI_MS_WINS1
, go
->winsaddr
[0]);
1371 REJCIWINS(CI_MS_WINS2
, go
->winsaddr
[1]);
1374 * If there are any remaining CIs, then this packet is bad.
1379 * Now we can update state.
1381 if (f
->state
!= OPENED
)
1386 IPCPDEBUG(("ipcp_rejci: received bad Reject!"));
1392 * ipcp_reqci - Check the peer's requested CIs and send appropriate response.
1393 * Callback from fsm_rconfreq, Receive Configure Request
1395 * Returns: CONFACK, CONFNAK or CONFREJ and input packet modified
1396 * appropriately. If reject_if_disagree is non-zero, doesn't return
1397 * CONFNAK; returns CONFREJ if it can't return CONFACK.
1400 ipcp_reqci(f
, inp
, len
, reject_if_disagree
)
1402 u_char
*inp
; /* Requested CIs */
1403 int *len
; /* Length of requested CIs */
1404 int reject_if_disagree
;
1406 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
1407 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
1408 ipcp_options
*ao
= &ipcp_allowoptions
[f
->unit
];
1409 u_char
*cip
, *next
; /* Pointer to current and next CIs */
1410 u_short cilen
, citype
; /* Parsed len, type */
1411 u_short cishort
; /* Parsed short value */
1412 u_int32_t tl
, ciaddr1
, ciaddr2
;/* Parsed address values */
1413 int rc
= CONFACK
; /* Final packet return code */
1414 int orc
; /* Individual option return code */
1415 u_char
*p
; /* Pointer to next char to parse */
1416 u_char
*ucp
= inp
; /* Pointer to current output char */
1417 int l
= *len
; /* Length left */
1418 u_char maxslotindex
, cflag
;
1422 * Reset all his options.
1424 BZERO(ho
, sizeof(*ho
));
1427 * Process all his options.
1431 orc
= CONFACK
; /* Assume success */
1432 cip
= p
= next
; /* Remember begining of CI */
1433 if (l
< 2 || /* Not enough data for CI header or */
1434 p
[1] < 2 || /* CI length too small or */
1435 p
[1] > l
) { /* CI length too big? */
1436 IPCPDEBUG(("ipcp_reqci: bad CI length!"));
1437 orc
= CONFREJ
; /* Reject bad CI */
1438 cilen
= l
; /* Reject till end of packet */
1439 l
= 0; /* Don't loop again */
1442 GETCHAR(citype
, p
); /* Parse CI type */
1443 GETCHAR(cilen
, p
); /* Parse CI length */
1444 l
-= cilen
; /* Adjust remaining length */
1445 next
+= cilen
; /* Step to next CI */
1447 switch (citype
) { /* Check CI type */
1449 if (!ao
->old_addrs
|| ho
->neg_addr
||
1450 cilen
!= CILEN_ADDRS
) { /* Check CI length */
1451 orc
= CONFREJ
; /* Reject CI */
1456 * If he has no address, or if we both have his address but
1457 * disagree about it, then NAK it with our idea.
1458 * In particular, if we don't know his address, but he does,
1461 GETLONG(tl
, p
); /* Parse source address (his) */
1462 ciaddr1
= htonl(tl
);
1463 if (ciaddr1
!= wo
->hisaddr
1464 && (ciaddr1
== 0 || !wo
->accept_remote
)) {
1466 if (!reject_if_disagree
) {
1467 DECPTR(sizeof(u_int32_t
), p
);
1468 tl
= ntohl(wo
->hisaddr
);
1471 } else if (ciaddr1
== 0 && wo
->hisaddr
== 0) {
1473 * If neither we nor he knows his address, reject the option.
1476 wo
->req_addr
= 0; /* don't NAK with 0.0.0.0 later */
1481 * If he doesn't know our address, or if we both have our address
1482 * but disagree about it, then NAK it with our idea.
1484 GETLONG(tl
, p
); /* Parse desination address (ours) */
1485 ciaddr2
= htonl(tl
);
1486 if (ciaddr2
!= wo
->ouraddr
) {
1487 if (ciaddr2
== 0 || !wo
->accept_local
) {
1489 if (!reject_if_disagree
) {
1490 DECPTR(sizeof(u_int32_t
), p
);
1491 tl
= ntohl(wo
->ouraddr
);
1495 wo
->ouraddr
= ciaddr2
; /* accept peer's idea */
1500 ho
->hisaddr
= ciaddr1
;
1501 ho
->ouraddr
= ciaddr2
;
1505 if (!ao
->neg_addr
|| ho
->old_addrs
||
1506 cilen
!= CILEN_ADDR
) { /* Check CI length */
1507 orc
= CONFREJ
; /* Reject CI */
1512 * If he has no address, or if we both have his address but
1513 * disagree about it, then NAK it with our idea.
1514 * In particular, if we don't know his address, but he does,
1517 GETLONG(tl
, p
); /* Parse source address (his) */
1518 ciaddr1
= htonl(tl
);
1519 if (ciaddr1
!= wo
->hisaddr
1520 && (ciaddr1
== 0 || !wo
->accept_remote
)) {
1522 if (!reject_if_disagree
) {
1523 DECPTR(sizeof(u_int32_t
), p
);
1524 tl
= ntohl(wo
->hisaddr
);
1527 } else if (ciaddr1
== 0 && wo
->hisaddr
== 0) {
1529 * Don't ACK an address of 0.0.0.0 - reject it instead.
1532 wo
->req_addr
= 0; /* don't NAK with 0.0.0.0 later */
1537 ho
->hisaddr
= ciaddr1
;
1542 /* Microsoft primary or secondary DNS request */
1543 d
= citype
== CI_MS_DNS2
;
1545 /* If we do not have a DNS address then we cannot send it */
1546 if (ao
->dnsaddr
[d
] == 0 ||
1547 cilen
!= CILEN_ADDR
) { /* Check CI length */
1548 orc
= CONFREJ
; /* Reject CI */
1552 if (htonl(tl
) != ao
->dnsaddr
[d
]) {
1553 DECPTR(sizeof(u_int32_t
), p
);
1554 tl
= ntohl(ao
->dnsaddr
[d
]);
1562 /* Microsoft primary or secondary WINS request */
1563 d
= citype
== CI_MS_WINS2
;
1565 /* If we do not have a DNS address then we cannot send it */
1566 if (ao
->winsaddr
[d
] == 0 ||
1567 cilen
!= CILEN_ADDR
) { /* Check CI length */
1568 orc
= CONFREJ
; /* Reject CI */
1572 if (htonl(tl
) != ao
->winsaddr
[d
]) {
1573 DECPTR(sizeof(u_int32_t
), p
);
1574 tl
= ntohl(ao
->winsaddr
[d
]);
1580 case CI_COMPRESSTYPE
:
1582 (cilen
!= CILEN_VJ
&& cilen
!= CILEN_COMPRESS
)) {
1586 GETSHORT(cishort
, p
);
1588 if (!(cishort
== IPCP_VJ_COMP
||
1589 (cishort
== IPCP_VJ_COMP_OLD
&& cilen
== CILEN_COMPRESS
))) {
1595 ho
->vj_protocol
= cishort
;
1596 if (cilen
== CILEN_VJ
) {
1597 GETCHAR(maxslotindex
, p
);
1598 if (maxslotindex
> ao
->maxslotindex
) {
1600 if (!reject_if_disagree
){
1602 PUTCHAR(ao
->maxslotindex
, p
);
1606 if (cflag
&& !ao
->cflag
) {
1608 if (!reject_if_disagree
){
1610 PUTCHAR(wo
->cflag
, p
);
1613 ho
->maxslotindex
= maxslotindex
;
1617 ho
->maxslotindex
= MAX_STATES
- 1;
1627 if (orc
== CONFACK
&& /* Good CI */
1628 rc
!= CONFACK
) /* but prior CI wasnt? */
1629 continue; /* Don't send this one */
1631 if (orc
== CONFNAK
) { /* Nak this CI? */
1632 if (reject_if_disagree
) /* Getting fed up with sending NAKs? */
1633 orc
= CONFREJ
; /* Get tough if so */
1635 if (rc
== CONFREJ
) /* Rejecting prior CI? */
1636 continue; /* Don't send this one */
1637 if (rc
== CONFACK
) { /* Ack'd all prior CIs? */
1638 rc
= CONFNAK
; /* Not anymore... */
1639 ucp
= inp
; /* Backup */
1644 if (orc
== CONFREJ
&& /* Reject this CI */
1645 rc
!= CONFREJ
) { /* but no prior ones? */
1647 ucp
= inp
; /* Backup */
1650 /* Need to move CI? */
1652 BCOPY(cip
, ucp
, cilen
); /* Move it */
1654 /* Update output pointer */
1659 * If we aren't rejecting this packet, and we want to negotiate
1660 * their address, and they didn't send their address, then we
1661 * send a NAK with a CI_ADDR option appended. We assume the
1662 * input buffer is long enough that we can append the extra
1665 if (rc
!= CONFREJ
&& !ho
->neg_addr
&& !ho
->old_addrs
&&
1666 wo
->req_addr
&& !reject_if_disagree
&& !noremoteip
) {
1667 if (rc
== CONFACK
) {
1669 ucp
= inp
; /* reset pointer */
1670 wo
->req_addr
= 0; /* don't ask again */
1672 PUTCHAR(CI_ADDR
, ucp
);
1673 PUTCHAR(CILEN_ADDR
, ucp
);
1674 tl
= ntohl(wo
->hisaddr
);
1678 *len
= ucp
- inp
; /* Compute output length */
1679 IPCPDEBUG(("ipcp: returning Configure-%s", CODENAME(rc
)));
1680 return (rc
); /* Return final code */
1685 * ip_check_options - check that any IP-related options are OK,
1686 * and assign appropriate defaults.
1693 ipcp_options
*wo
= &ipcp_wantoptions
[0];
1696 * Default our local IP address based on our hostname.
1697 * If local IP address already given, don't bother.
1699 if (wo
->ouraddr
== 0 && !disable_defaultip
) {
1701 * Look up our hostname (possibly with domain name appended)
1702 * and take the first IP address as our local IP address.
1703 * If there isn't an IP address for our hostname, too bad.
1705 wo
->accept_local
= 1; /* don't insist on this default value */
1706 if ((hp
= gethostbyname(hostname
)) != NULL
) {
1707 local
= *(u_int32_t
*)hp
->h_addr
;
1708 if (local
!= 0 && !bad_ip_adrs(local
))
1709 wo
->ouraddr
= local
;
1712 ask_for_local
= wo
->ouraddr
!= 0 || !disable_defaultip
;
1717 * ip_demand_conf - configure the interface as though
1718 * IPCP were up, for use with dial-on-demand.
1724 ipcp_options
*wo
= &ipcp_wantoptions
[u
];
1726 if (wo
->hisaddr
== 0 && !noremoteip
) {
1727 /* make up an arbitrary address for the peer */
1728 wo
->hisaddr
= htonl(0x0a707070 + ifunit
);
1729 wo
->accept_remote
= 1;
1731 if (wo
->ouraddr
== 0) {
1732 /* make up an arbitrary address for us */
1733 wo
->ouraddr
= htonl(0x0a404040 + ifunit
);
1734 wo
->accept_local
= 1;
1735 ask_for_local
= 0; /* don't tell the peer this address */
1737 if (!sifaddr(u
, wo
->ouraddr
, wo
->hisaddr
, GetMask(wo
->ouraddr
)))
1739 ipcp_script(_PATH_IPPREUP
, 1);
1742 if (!sifnpmode(u
, PPP_IP
, NPMODE_QUEUE
))
1744 if (wo
->default_route
)
1745 if (sifdefaultroute(u
, wo
->ouraddr
, wo
->hisaddr
))
1746 default_route_set
[u
] = 1;
1748 if (sifproxyarp(u
, wo
->hisaddr
))
1749 proxy_arp_set
[u
] = 1;
1751 notice("local IP address %I", wo
->ouraddr
);
1753 notice("remote IP address %I", wo
->hisaddr
);
1760 * ipcp_up - IPCP has come UP.
1762 * Configure the IP network interface appropriately and bring it up.
1769 ipcp_options
*ho
= &ipcp_hisoptions
[f
->unit
];
1770 ipcp_options
*go
= &ipcp_gotoptions
[f
->unit
];
1771 ipcp_options
*wo
= &ipcp_wantoptions
[f
->unit
];
1773 IPCPDEBUG(("ipcp: up"));
1776 * We must have a non-zero IP address for both ends of the link.
1778 if (!ho
->neg_addr
&& !ho
->old_addrs
)
1779 ho
->hisaddr
= wo
->hisaddr
;
1781 if (!(go
->neg_addr
|| go
->old_addrs
) && (wo
->neg_addr
|| wo
->old_addrs
)
1782 && wo
->ouraddr
!= 0) {
1783 error("Peer refused to agree to our IP address");
1784 ipcp_close(f
->unit
, "Refused our IP address");
1787 if (go
->ouraddr
== 0) {
1788 error("Could not determine local IP address");
1789 ipcp_close(f
->unit
, "Could not determine local IP address");
1792 if (ho
->hisaddr
== 0 && !noremoteip
) {
1793 ho
->hisaddr
= htonl(0x0a404040 + ifunit
);
1794 warn("Could not determine remote IP address: defaulting to %I",
1797 script_setenv("IPLOCAL", ip_ntoa(go
->ouraddr
), 0);
1798 if (ho
->hisaddr
!= 0)
1799 script_setenv("IPREMOTE", ip_ntoa(ho
->hisaddr
), 1);
1806 script_setenv("DNS1", ip_ntoa(go
->dnsaddr
[0]), 0);
1808 script_setenv("DNS2", ip_ntoa(go
->dnsaddr
[1]), 0);
1809 if (usepeerdns
&& (go
->dnsaddr
[0] || go
->dnsaddr
[1])) {
1810 script_setenv("USEPEERDNS", "1", 0);
1811 create_resolv(go
->dnsaddr
[0], go
->dnsaddr
[1]);
1815 * Check that the peer is allowed to use the IP address it wants.
1817 if (ho
->hisaddr
!= 0 && !auth_ip_addr(f
->unit
, ho
->hisaddr
)) {
1818 error("Peer is not authorized to use remote address %I", ho
->hisaddr
);
1819 ipcp_close(f
->unit
, "Unauthorized remote IP address");
1823 /* set tcp compression */
1824 sifvjcomp(f
->unit
, ho
->neg_vj
, ho
->cflag
, ho
->maxslotindex
);
1827 * If we are doing dial-on-demand, the interface is already
1828 * configured, so we put out any saved-up packets, then set the
1829 * interface to pass IP packets.
1832 if (go
->ouraddr
!= wo
->ouraddr
|| ho
->hisaddr
!= wo
->hisaddr
) {
1833 ipcp_clear_addrs(f
->unit
, wo
->ouraddr
, wo
->hisaddr
);
1834 if (go
->ouraddr
!= wo
->ouraddr
) {
1835 warn("Local IP address changed to %I", go
->ouraddr
);
1836 script_setenv("OLDIPLOCAL", ip_ntoa(wo
->ouraddr
), 0);
1837 wo
->ouraddr
= go
->ouraddr
;
1839 script_unsetenv("OLDIPLOCAL");
1840 if (ho
->hisaddr
!= wo
->hisaddr
&& wo
->hisaddr
!= 0) {
1841 warn("Remote IP address changed to %I", ho
->hisaddr
);
1842 script_setenv("OLDIPREMOTE", ip_ntoa(wo
->hisaddr
), 0);
1843 wo
->hisaddr
= ho
->hisaddr
;
1845 script_unsetenv("OLDIPREMOTE");
1847 /* Set the interface to the new addresses */
1848 mask
= GetMask(go
->ouraddr
);
1849 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1851 warn("Interface configuration failed");
1852 ipcp_close(f
->unit
, "Interface configuration failed");
1856 /* assign a default route through the interface if required */
1857 if (ipcp_wantoptions
[f
->unit
].default_route
)
1858 if (sifdefaultroute(f
->unit
, go
->ouraddr
, ho
->hisaddr
))
1859 default_route_set
[f
->unit
] = 1;
1861 /* Make a proxy ARP entry if requested. */
1862 if (ho
->hisaddr
!= 0 && ipcp_wantoptions
[f
->unit
].proxy_arp
)
1863 if (sifproxyarp(f
->unit
, ho
->hisaddr
))
1864 proxy_arp_set
[f
->unit
] = 1;
1867 demand_rexmit(PPP_IP
,go
->ouraddr
);
1868 sifnpmode(f
->unit
, PPP_IP
, NPMODE_PASS
);
1872 * Set IP addresses and (if specified) netmask.
1874 mask
= GetMask(go
->ouraddr
);
1876 #if !(defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1877 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1879 warn("Interface configuration failed");
1880 ipcp_close(f
->unit
, "Interface configuration failed");
1885 /* run the pre-up script, if any, and wait for it to finish */
1886 ipcp_script(_PATH_IPPREUP
, 1);
1888 /* bring the interface up for IP */
1889 if (!sifup(f
->unit
)) {
1891 warn("Interface failed to come up");
1892 ipcp_close(f
->unit
, "Interface configuration failed");
1896 #if (defined(SVR4) && (defined(SNI) || defined(__USLC__)))
1897 if (!sifaddr(f
->unit
, go
->ouraddr
, ho
->hisaddr
, mask
)) {
1899 warn("Interface configuration failed");
1900 ipcp_close(f
->unit
, "Interface configuration failed");
1904 sifnpmode(f
->unit
, PPP_IP
, NPMODE_PASS
);
1906 /* assign a default route through the interface if required */
1907 if (ipcp_wantoptions
[f
->unit
].default_route
)
1908 if (sifdefaultroute(f
->unit
, go
->ouraddr
, ho
->hisaddr
))
1909 default_route_set
[f
->unit
] = 1;
1911 /* Make a proxy ARP entry if requested. */
1912 if (ho
->hisaddr
!= 0 && ipcp_wantoptions
[f
->unit
].proxy_arp
)
1913 if (sifproxyarp(f
->unit
, ho
->hisaddr
))
1914 proxy_arp_set
[f
->unit
] = 1;
1916 ipcp_wantoptions
[0].ouraddr
= go
->ouraddr
;
1918 notice("local IP address %I", go
->ouraddr
);
1919 if (ho
->hisaddr
!= 0)
1920 notice("remote IP address %I", ho
->hisaddr
);
1922 notice("primary DNS address %I", go
->dnsaddr
[0]);
1924 notice("secondary DNS address %I", go
->dnsaddr
[1]);
1927 reset_link_stats(f
->unit
);
1929 np_up(f
->unit
, PPP_IP
);
1932 notify(ip_up_notifier
, 0);
1937 * Execute the ip-up script, like this:
1938 * /etc/ppp/ip-up interface tty speed local-IP remote-IP
1940 if (ipcp_script_state
== s_down
&& ipcp_script_pid
== 0) {
1941 ipcp_script_state
= s_up
;
1942 ipcp_script(path_ipup
, 0);
1948 * ipcp_down - IPCP has gone DOWN.
1950 * Take the IP network interface down, clear its addresses
1951 * and delete routes through it.
1957 IPCPDEBUG(("ipcp: down"));
1958 /* XXX a bit IPv4-centric here, we only need to get the stats
1959 * before the interface is marked down. */
1960 /* XXX more correct: we must get the stats before running the notifiers,
1961 * at least for the radius plugin */
1962 update_link_stats(f
->unit
);
1963 notify(ip_down_notifier
, 0);
1968 np_down(f
->unit
, PPP_IP
);
1970 sifvjcomp(f
->unit
, 0, 0, 0);
1972 print_link_stats(); /* _after_ running the notifiers and ip_down_hook(),
1973 * because print_link_stats() sets link_stats_valid
1977 * If we are doing dial-on-demand, set the interface
1978 * to queue up outgoing packets (for now).
1981 sifnpmode(f
->unit
, PPP_IP
, NPMODE_QUEUE
);
1983 sifnpmode(f
->unit
, PPP_IP
, NPMODE_DROP
);
1985 ipcp_clear_addrs(f
->unit
, ipcp_gotoptions
[f
->unit
].ouraddr
,
1986 ipcp_hisoptions
[f
->unit
].hisaddr
);
1989 /* Execute the ip-down script */
1990 if (ipcp_script_state
== s_up
&& ipcp_script_pid
== 0) {
1991 ipcp_script_state
= s_down
;
1992 ipcp_script(path_ipdown
, 0);
1998 * ipcp_clear_addrs() - clear the interface addresses, routes,
1999 * proxy arp entries, etc.
2002 ipcp_clear_addrs(unit
, ouraddr
, hisaddr
)
2004 u_int32_t ouraddr
; /* local address */
2005 u_int32_t hisaddr
; /* remote address */
2007 if (proxy_arp_set
[unit
]) {
2008 cifproxyarp(unit
, hisaddr
);
2009 proxy_arp_set
[unit
] = 0;
2011 if (default_route_set
[unit
]) {
2012 cifdefaultroute(unit
, ouraddr
, hisaddr
);
2013 default_route_set
[unit
] = 0;
2015 cifaddr(unit
, ouraddr
, hisaddr
);
2020 * ipcp_finished - possibly shut down the lower layers.
2028 np_finished(f
->unit
, PPP_IP
);
2034 * ipcp_script_done - called when the ip-up or ip-down script
2038 ipcp_script_done(arg
)
2041 ipcp_script_pid
= 0;
2042 switch (ipcp_script_state
) {
2044 if (ipcp_fsm
[0].state
!= OPENED
) {
2045 ipcp_script_state
= s_down
;
2046 ipcp_script(path_ipdown
, 0);
2050 if (ipcp_fsm
[0].state
== OPENED
) {
2051 ipcp_script_state
= s_up
;
2052 ipcp_script(path_ipup
, 0);
2060 * ipcp_script - Execute a script with arguments
2061 * interface-name tty-name speed local-IP remote-IP.
2064 ipcp_script(script
, wait
)
2068 char strspeed
[32], strlocal
[32], strremote
[32];
2071 slprintf(strspeed
, sizeof(strspeed
), "%d", baud_rate
);
2072 slprintf(strlocal
, sizeof(strlocal
), "%I", ipcp_gotoptions
[0].ouraddr
);
2073 slprintf(strremote
, sizeof(strremote
), "%I", ipcp_hisoptions
[0].hisaddr
);
2080 argv
[5] = strremote
;
2084 run_program(script
, argv
, 0, NULL
, NULL
, 1);
2086 ipcp_script_pid
= run_program(script
, argv
, 0, ipcp_script_done
,
2091 * create_resolv - create the replacement resolv.conf file
2094 create_resolv(peerdns1
, peerdns2
)
2095 u_int32_t peerdns1
, peerdns2
;
2099 f
= fopen(_PATH_RESOLV
, "w");
2101 error("Failed to create %s: %m", _PATH_RESOLV
);
2106 fprintf(f
, "nameserver %s\n", ip_ntoa(peerdns1
));
2109 fprintf(f
, "nameserver %s\n", ip_ntoa(peerdns2
));
2112 error("Write failed to %s: %m", _PATH_RESOLV
);
2118 * ipcp_printpkt - print the contents of an IPCP packet.
2120 static char *ipcp_codenames
[] = {
2121 "ConfReq", "ConfAck", "ConfNak", "ConfRej",
2122 "TermReq", "TermAck", "CodeRej"
2126 ipcp_printpkt(p
, plen
, printer
, arg
)
2129 void (*printer
) __P((void *, char *, ...));
2132 int code
, id
, len
, olen
;
2133 u_char
*pstart
, *optend
;
2137 if (plen
< HEADERLEN
)
2143 if (len
< HEADERLEN
|| len
> plen
)
2146 if (code
>= 1 && code
<= sizeof(ipcp_codenames
) / sizeof(char *))
2147 printer(arg
, " %s", ipcp_codenames
[code
-1]);
2149 printer(arg
, " code=0x%x", code
);
2150 printer(arg
, " id=0x%x", id
);
2157 /* print option list */
2162 if (olen
< 2 || olen
> len
) {
2170 if (olen
== CILEN_ADDRS
) {
2173 printer(arg
, "addrs %I", htonl(cilong
));
2175 printer(arg
, " %I", htonl(cilong
));
2178 case CI_COMPRESSTYPE
:
2179 if (olen
>= CILEN_COMPRESS
) {
2181 GETSHORT(cishort
, p
);
2182 printer(arg
, "compress ");
2187 case IPCP_VJ_COMP_OLD
:
2188 printer(arg
, "old-VJ");
2191 printer(arg
, "0x%x", cishort
);
2196 if (olen
== CILEN_ADDR
) {
2199 printer(arg
, "addr %I", htonl(cilong
));
2206 printer(arg
, "ms-dns%d %I", (code
== CI_MS_DNS1
? 1: 2),
2213 printer(arg
, "ms-wins %I", htonl(cilong
));
2216 while (p
< optend
) {
2218 printer(arg
, " %.2x", code
);
2226 if (len
> 0 && *p
>= ' ' && *p
< 0x7f) {
2228 print_string((char *)p
, len
, printer
, arg
);
2235 /* print the rest of the bytes in the packet */
2236 for (; len
> 0; --len
) {
2238 printer(arg
, " %.2x", code
);
2245 * ip_active_pkt - see if this IP packet is worth bringing the link up for.
2246 * We don't bring the link up for IP fragments or for TCP FIN packets
2249 #define IP_HDRLEN 20 /* bytes */
2250 #define IP_OFFMASK 0x1fff
2252 #define IPPROTO_TCP 6
2254 #define TCP_HDRLEN 20
2258 * We use these macros because the IP header may be at an odd address,
2259 * and some compilers might use word loads to get th_off or ip_hl.
2262 #define net_short(x) (((x)[0] << 8) + (x)[1])
2263 #define get_iphl(x) (((unsigned char *)(x))[0] & 0xF)
2264 #define get_ipoff(x) net_short((unsigned char *)(x) + 6)
2265 #define get_ipproto(x) (((unsigned char *)(x))[9])
2266 #define get_tcpoff(x) (((unsigned char *)(x))[12] >> 4)
2267 #define get_tcpflags(x) (((unsigned char *)(x))[13])
2270 ip_active_pkt(pkt
, len
)
2279 if (len
< IP_HDRLEN
)
2281 if ((get_ipoff(pkt
) & IP_OFFMASK
) != 0)
2283 if (get_ipproto(pkt
) != IPPROTO_TCP
)
2285 hlen
= get_iphl(pkt
) * 4;
2286 if (len
< hlen
+ TCP_HDRLEN
)
2289 if ((get_tcpflags(tcp
) & TH_FIN
) != 0 && len
== hlen
+ get_tcpoff(tcp
) * 4)