if_spppsubr.c revision 1.178 1 /* $NetBSD: if_spppsubr.c,v 1.178 2017/12/28 07:06:36 ozaki-r Exp $ */
2
3 /*
4 * Synchronous PPP/Cisco link level subroutines.
5 * Keepalive protocol implemented in both Cisco and PPP modes.
6 *
7 * Copyright (C) 1994-1996 Cronyx Engineering Ltd.
8 * Author: Serge Vakulenko, <vak (at) cronyx.ru>
9 *
10 * Heavily revamped to conform to RFC 1661.
11 * Copyright (C) 1997, Joerg Wunsch.
12 *
13 * RFC2472 IPv6CP support.
14 * Copyright (C) 2000, Jun-ichiro itojun Hagino <itojun (at) iijlab.net>.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions are met:
18 * 1. Redistributions of source code must retain the above copyright notice,
19 * this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright notice,
21 * this list of conditions and the following disclaimer in the documentation
22 * and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 *
36 * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997
37 *
38 * From: if_spppsubr.c,v 1.39 1998/04/04 13:26:03 phk Exp
39 *
40 * From: Id: if_spppsubr.c,v 1.23 1999/02/23 14:47:50 hm Exp
41 */
42
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: if_spppsubr.c,v 1.178 2017/12/28 07:06:36 ozaki-r Exp $");
45
46 #if defined(_KERNEL_OPT)
47 #include "opt_inet.h"
48 #include "opt_modular.h"
49 #include "opt_compat_netbsd.h"
50 #include "opt_net_mpsafe.h"
51 #endif
52
53
54 #include <sys/param.h>
55 #include <sys/proc.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
58 #include <sys/sockio.h>
59 #include <sys/socket.h>
60 #include <sys/syslog.h>
61 #include <sys/malloc.h>
62 #include <sys/mbuf.h>
63 #include <sys/callout.h>
64 #include <sys/md5.h>
65 #include <sys/inttypes.h>
66 #include <sys/kauth.h>
67 #include <sys/cprng.h>
68 #include <sys/module.h>
69 #include <sys/workqueue.h>
70 #include <sys/atomic.h>
71
72 #include <net/if.h>
73 #include <net/netisr.h>
74 #include <net/if_types.h>
75 #include <net/route.h>
76 #include <net/ppp_defs.h>
77
78 #include <netinet/in.h>
79 #include <netinet/in_systm.h>
80 #include <netinet/in_var.h>
81 #ifdef INET
82 #include <netinet/ip.h>
83 #include <netinet/tcp.h>
84 #endif
85 #include <net/ethertypes.h>
86
87 #ifdef INET6
88 #include <netinet6/scope6_var.h>
89 #endif
90
91 #include <net/if_sppp.h>
92 #include <net/if_spppvar.h>
93
94 #ifdef NET_MPSAFE
95 #define SPPPSUBR_MPSAFE 1
96 #endif
97
98 #define LCP_KEEPALIVE_INTERVAL 10 /* seconds between checks */
99 #define LOOPALIVECNT 3 /* loopback detection tries */
100 #define DEFAULT_MAXALIVECNT 3 /* max. missed alive packets */
101 #define DEFAULT_NORECV_TIME 15 /* before we get worried */
102 #define DEFAULT_MAX_AUTH_FAILURES 5 /* max. auth. failures */
103
104 /*
105 * Interface flags that can be set in an ifconfig command.
106 *
107 * Setting link0 will make the link passive, i.e. it will be marked
108 * as being administrative openable, but won't be opened to begin
109 * with. Incoming calls will be answered, or subsequent calls with
110 * -link1 will cause the administrative open of the LCP layer.
111 *
112 * Setting link1 will cause the link to auto-dial only as packets
113 * arrive to be sent.
114 *
115 * Setting IFF_DEBUG will syslog the option negotiation and state
116 * transitions at level kern.debug. Note: all logs consistently look
117 * like
118 *
119 * <if-name><unit>: <proto-name> <additional info...>
120 *
121 * with <if-name><unit> being something like "bppp0", and <proto-name>
122 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
123 */
124
125 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
126 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
127
128 #define CONF_REQ 1 /* PPP configure request */
129 #define CONF_ACK 2 /* PPP configure acknowledge */
130 #define CONF_NAK 3 /* PPP configure negative ack */
131 #define CONF_REJ 4 /* PPP configure reject */
132 #define TERM_REQ 5 /* PPP terminate request */
133 #define TERM_ACK 6 /* PPP terminate acknowledge */
134 #define CODE_REJ 7 /* PPP code reject */
135 #define PROTO_REJ 8 /* PPP protocol reject */
136 #define ECHO_REQ 9 /* PPP echo request */
137 #define ECHO_REPLY 10 /* PPP echo reply */
138 #define DISC_REQ 11 /* PPP discard request */
139
140 #define LCP_OPT_MRU 1 /* maximum receive unit */
141 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
142 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
143 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
144 #define LCP_OPT_MAGIC 5 /* magic number */
145 #define LCP_OPT_RESERVED 6 /* reserved */
146 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
147 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
148
149 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
150 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol */
151 #define IPCP_OPT_ADDRESS 3 /* local IP address */
152 #define IPCP_OPT_PRIMDNS 129 /* primary remote dns address */
153 #define IPCP_OPT_SECDNS 131 /* secondary remote dns address */
154
155 #define IPCP_UPDATE_LIMIT 8 /* limit of pending IP updating job */
156 #define IPCP_SET_ADDRS 1 /* marker for IP address setting job */
157 #define IPCP_CLEAR_ADDRS 2 /* marker for IP address clearing job */
158
159 #define IPV6CP_OPT_IFID 1 /* interface identifier */
160 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
161
162 #define PAP_REQ 1 /* PAP name/password request */
163 #define PAP_ACK 2 /* PAP acknowledge */
164 #define PAP_NAK 3 /* PAP fail */
165
166 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
167 #define CHAP_RESPONSE 2 /* CHAP challenge response */
168 #define CHAP_SUCCESS 3 /* CHAP response ok */
169 #define CHAP_FAILURE 4 /* CHAP response failed */
170
171 #define CHAP_MD5 5 /* hash algorithm - MD5 */
172
173 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
174 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
175 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
176 #define CISCO_ADDR_REQ 0 /* Cisco address request */
177 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
178 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
179
180 /* states are named and numbered according to RFC 1661 */
181 #define STATE_INITIAL 0
182 #define STATE_STARTING 1
183 #define STATE_CLOSED 2
184 #define STATE_STOPPED 3
185 #define STATE_CLOSING 4
186 #define STATE_STOPPING 5
187 #define STATE_REQ_SENT 6
188 #define STATE_ACK_RCVD 7
189 #define STATE_ACK_SENT 8
190 #define STATE_OPENED 9
191
192 struct ppp_header {
193 uint8_t address;
194 uint8_t control;
195 uint16_t protocol;
196 } __packed;
197 #define PPP_HEADER_LEN sizeof (struct ppp_header)
198
199 struct lcp_header {
200 uint8_t type;
201 uint8_t ident;
202 uint16_t len;
203 } __packed;
204 #define LCP_HEADER_LEN sizeof (struct lcp_header)
205
206 struct cisco_packet {
207 uint32_t type;
208 uint32_t par1;
209 uint32_t par2;
210 uint16_t rel;
211 uint16_t time0;
212 uint16_t time1;
213 } __packed;
214 #define CISCO_PACKET_LEN 18
215
216 /*
217 * We follow the spelling and capitalization of RFC 1661 here, to make
218 * it easier comparing with the standard. Please refer to this RFC in
219 * case you can't make sense out of these abbreviation; it will also
220 * explain the semantics related to the various events and actions.
221 */
222 struct cp {
223 u_short proto; /* PPP control protocol number */
224 u_char protoidx; /* index into state table in struct sppp */
225 u_char flags;
226 #define CP_LCP 0x01 /* this is the LCP */
227 #define CP_AUTH 0x02 /* this is an authentication protocol */
228 #define CP_NCP 0x04 /* this is a NCP */
229 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
230 const char *name; /* name of this control protocol */
231 /* event handlers */
232 void (*Up)(struct sppp *sp);
233 void (*Down)(struct sppp *sp);
234 void (*Open)(struct sppp *sp);
235 void (*Close)(struct sppp *sp);
236 void (*TO)(void *sp);
237 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
238 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
239 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
240 /* actions */
241 void (*tlu)(struct sppp *sp);
242 void (*tld)(struct sppp *sp);
243 void (*tls)(struct sppp *sp);
244 void (*tlf)(struct sppp *sp);
245 void (*scr)(struct sppp *sp);
246 };
247
248 static struct sppp *spppq;
249 static kmutex_t *spppq_lock = NULL;
250 static callout_t keepalive_ch;
251
252 #define SPPPQ_LOCK() if (spppq_lock) \
253 mutex_enter(spppq_lock);
254 #define SPPPQ_UNLOCK() if (spppq_lock) \
255 mutex_exit(spppq_lock);
256
257 #define SPPP_LOCK(_sp, _op) rw_enter(&(_sp)->pp_lock, (_op))
258 #define SPPP_UNLOCK(_sp) rw_exit(&(_sp)->pp_lock)
259 #define SPPP_WLOCKED(_sp) rw_write_held(&(_sp)->pp_lock)
260 #define SPPP_UPGRADE(_sp) do{ \
261 SPPP_UNLOCK(_sp); \
262 SPPP_LOCK(_sp, RW_WRITER); \
263 }while (0)
264 #define SPPP_DOWNGRADE(_sp) rw_downgrade(&(_sp)->pp_lock)
265
266 #ifdef INET
267 #ifndef SPPPSUBR_MPSAFE
268 /*
269 * The following disgusting hack gets around the problem that IP TOS
270 * can't be set yet. We want to put "interactive" traffic on a high
271 * priority queue. To decide if traffic is interactive, we check that
272 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
273 *
274 * XXX is this really still necessary? - joerg -
275 */
276 static u_short interactive_ports[8] = {
277 0, 513, 0, 0,
278 0, 21, 0, 23,
279 };
280 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
281 #endif /* SPPPSUBR_MPSAFE */
282 #endif
283
284 /* almost every function needs these */
285 #define STDDCL \
286 struct ifnet *ifp = &sp->pp_if; \
287 int debug = ifp->if_flags & IFF_DEBUG
288
289 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
290 const struct sockaddr *dst, const struct rtentry *rt);
291
292 static void sppp_cisco_send(struct sppp *sp, int type, int32_t par1, int32_t par2);
293 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
294
295 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
296 struct mbuf *m);
297 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
298 u_char ident, u_short len, void *data);
299 /* static void sppp_cp_timeout(void *arg); */
300 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
301 int newstate);
302 static void sppp_auth_send(const struct cp *cp,
303 struct sppp *sp, unsigned int type, unsigned int id,
304 ...);
305
306 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
307 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
308 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
309 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
310 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
311
312 static void sppp_null(struct sppp *sp);
313
314 static void sppp_lcp_init(struct sppp *sp);
315 static void sppp_lcp_up(struct sppp *sp);
316 static void sppp_lcp_down(struct sppp *sp);
317 static void sppp_lcp_open(struct sppp *sp);
318 static void sppp_lcp_close(struct sppp *sp);
319 static void sppp_lcp_TO(void *sp);
320 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
321 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
322 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
323 static void sppp_lcp_tlu(struct sppp *sp);
324 static void sppp_lcp_tld(struct sppp *sp);
325 static void sppp_lcp_tls(struct sppp *sp);
326 static void sppp_lcp_tlf(struct sppp *sp);
327 static void sppp_lcp_scr(struct sppp *sp);
328 static void sppp_lcp_check_and_close(struct sppp *sp);
329 static int sppp_ncp_check(struct sppp *sp);
330
331 static void sppp_ipcp_init(struct sppp *sp);
332 static void sppp_ipcp_up(struct sppp *sp);
333 static void sppp_ipcp_down(struct sppp *sp);
334 static void sppp_ipcp_open(struct sppp *sp);
335 static void sppp_ipcp_close(struct sppp *sp);
336 static void sppp_ipcp_TO(void *sp);
337 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
338 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
339 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
340 static void sppp_ipcp_tlu(struct sppp *sp);
341 static void sppp_ipcp_tld(struct sppp *sp);
342 static void sppp_ipcp_tls(struct sppp *sp);
343 static void sppp_ipcp_tlf(struct sppp *sp);
344 static void sppp_ipcp_scr(struct sppp *sp);
345
346 static void sppp_ipv6cp_init(struct sppp *sp);
347 static void sppp_ipv6cp_up(struct sppp *sp);
348 static void sppp_ipv6cp_down(struct sppp *sp);
349 static void sppp_ipv6cp_open(struct sppp *sp);
350 static void sppp_ipv6cp_close(struct sppp *sp);
351 static void sppp_ipv6cp_TO(void *sp);
352 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
353 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
354 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
355 static void sppp_ipv6cp_tlu(struct sppp *sp);
356 static void sppp_ipv6cp_tld(struct sppp *sp);
357 static void sppp_ipv6cp_tls(struct sppp *sp);
358 static void sppp_ipv6cp_tlf(struct sppp *sp);
359 static void sppp_ipv6cp_scr(struct sppp *sp);
360
361 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
362 static void sppp_pap_init(struct sppp *sp);
363 static void sppp_pap_open(struct sppp *sp);
364 static void sppp_pap_close(struct sppp *sp);
365 static void sppp_pap_TO(void *sp);
366 static void sppp_pap_my_TO(void *sp);
367 static void sppp_pap_tlu(struct sppp *sp);
368 static void sppp_pap_tld(struct sppp *sp);
369 static void sppp_pap_scr(struct sppp *sp);
370
371 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
372 static void sppp_chap_init(struct sppp *sp);
373 static void sppp_chap_open(struct sppp *sp);
374 static void sppp_chap_close(struct sppp *sp);
375 static void sppp_chap_TO(void *sp);
376 static void sppp_chap_tlu(struct sppp *sp);
377 static void sppp_chap_tld(struct sppp *sp);
378 static void sppp_chap_scr(struct sppp *sp);
379
380 static const char *sppp_auth_type_name(u_short proto, u_char type);
381 static const char *sppp_cp_type_name(u_char type);
382 static const char *sppp_dotted_quad(uint32_t addr);
383 static const char *sppp_ipcp_opt_name(u_char opt);
384 #ifdef INET6
385 static const char *sppp_ipv6cp_opt_name(u_char opt);
386 #endif
387 static const char *sppp_lcp_opt_name(u_char opt);
388 static const char *sppp_phase_name(int phase);
389 static const char *sppp_proto_name(u_short proto);
390 static const char *sppp_state_name(int state);
391 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
392 #ifdef INET
393 static void sppp_get_ip_addrs(struct sppp *sp, uint32_t *src, uint32_t *dst,
394 uint32_t *srcmask);
395 static void sppp_set_ip_addrs_work(struct work *wk, struct sppp *sp);
396 static void sppp_set_ip_addrs(struct sppp *sp);
397 static void sppp_clear_ip_addrs_work(struct work *wk, struct sppp *sp);
398 static void sppp_clear_ip_addrs(struct sppp *sp);
399 static void sppp_update_ip_addrs_work(struct work *wk, void *arg);
400 #endif
401 static void sppp_keepalive(void *dummy);
402 static void sppp_phase_network(struct sppp *sp);
403 static void sppp_print_bytes(const u_char *p, u_short len);
404 static void sppp_print_string(const char *p, u_short len);
405 #ifdef INET6
406 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
407 struct in6_addr *dst, struct in6_addr *srcmask);
408 #ifdef IPV6CP_MYIFID_DYN
409 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
410 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
411 #endif
412 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
413 #endif
414
415 static void sppp_notify_up(struct sppp *);
416 static void sppp_notify_down(struct sppp *);
417 static void sppp_notify_tls_wlocked(struct sppp *);
418 static void sppp_notify_tlf_wlocked(struct sppp *);
419 #ifdef INET6
420 static void sppp_notify_con_wlocked(struct sppp *);
421 #endif
422 static void sppp_notify_con(struct sppp *);
423
424 static void sppp_notify_chg_wlocked(struct sppp *);
425
426
427 /* our control protocol descriptors */
428 static const struct cp lcp = {
429 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
430 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
431 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
432 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
433 sppp_lcp_scr
434 };
435
436 static const struct cp ipcp = {
437 PPP_IPCP, IDX_IPCP,
438 #ifdef INET
439 CP_NCP, /*don't run IPCP if there's no IPv4 support*/
440 #else
441 0,
442 #endif
443 "ipcp",
444 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
445 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
446 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
447 sppp_ipcp_scr
448 };
449
450 static const struct cp ipv6cp = {
451 PPP_IPV6CP, IDX_IPV6CP,
452 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
453 CP_NCP,
454 #else
455 0,
456 #endif
457 "ipv6cp",
458 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
459 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
460 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
461 sppp_ipv6cp_scr
462 };
463
464 static const struct cp pap = {
465 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
466 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
467 sppp_pap_TO, 0, 0, 0,
468 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
469 sppp_pap_scr
470 };
471
472 static const struct cp chap = {
473 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
474 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
475 sppp_chap_TO, 0, 0, 0,
476 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
477 sppp_chap_scr
478 };
479
480 static const struct cp *cps[IDX_COUNT] = {
481 &lcp, /* IDX_LCP */
482 &ipcp, /* IDX_IPCP */
483 &ipv6cp, /* IDX_IPV6CP */
484 &pap, /* IDX_PAP */
485 &chap, /* IDX_CHAP */
486 };
487
488 static void
489 sppp_change_phase(struct sppp *sp, int phase)
490 {
491 STDDCL;
492
493 KASSERT(SPPP_WLOCKED(sp));
494
495 if (sp->pp_phase == phase)
496 return;
497
498 sp->pp_phase = phase;
499
500 if (phase == SPPP_PHASE_NETWORK)
501 if_link_state_change(ifp, LINK_STATE_UP);
502 else
503 if_link_state_change(ifp, LINK_STATE_DOWN);
504
505 if (debug)
506 {
507 log(LOG_INFO, "%s: phase %s\n", ifp->if_xname,
508 sppp_phase_name(sp->pp_phase));
509 }
510 }
511
512 /*
513 * Exported functions, comprising our interface to the lower layer.
514 */
515
516 /*
517 * Process the received packet.
518 */
519 void
520 sppp_input(struct ifnet *ifp, struct mbuf *m)
521 {
522 struct ppp_header *h = NULL;
523 pktqueue_t *pktq = NULL;
524 struct ifqueue *inq = NULL;
525 uint16_t protocol;
526 struct sppp *sp = (struct sppp *)ifp;
527 int debug = ifp->if_flags & IFF_DEBUG;
528 int isr = 0;
529
530 SPPP_LOCK(sp, RW_READER);
531
532 if (ifp->if_flags & IFF_UP) {
533 /* Count received bytes, add hardware framing */
534 ifp->if_ibytes += m->m_pkthdr.len + sp->pp_framebytes;
535 /* Note time of last receive */
536 sp->pp_last_receive = time_uptime;
537 }
538
539 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
540 /* Too small packet, drop it. */
541 if (debug)
542 log(LOG_DEBUG,
543 "%s: input packet is too small, %d bytes\n",
544 ifp->if_xname, m->m_pkthdr.len);
545 drop:
546 ++ifp->if_ierrors;
547 ++ifp->if_iqdrops;
548 m_freem(m);
549 SPPP_UNLOCK(sp);
550 return;
551 }
552
553 if (sp->pp_flags & PP_NOFRAMING) {
554 memcpy(&protocol, mtod(m, void *), 2);
555 protocol = ntohs(protocol);
556 m_adj(m, 2);
557 } else {
558
559 /* Get PPP header. */
560 h = mtod(m, struct ppp_header *);
561 m_adj(m, PPP_HEADER_LEN);
562
563 switch (h->address) {
564 case PPP_ALLSTATIONS:
565 if (h->control != PPP_UI)
566 goto invalid;
567 if (sp->pp_flags & PP_CISCO) {
568 if (debug)
569 log(LOG_DEBUG,
570 "%s: PPP packet in Cisco mode "
571 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
572 ifp->if_xname,
573 h->address, h->control, ntohs(h->protocol));
574 goto drop;
575 }
576 break;
577 case CISCO_MULTICAST:
578 case CISCO_UNICAST:
579 /* Don't check the control field here (RFC 1547). */
580 if (! (sp->pp_flags & PP_CISCO)) {
581 if (debug)
582 log(LOG_DEBUG,
583 "%s: Cisco packet in PPP mode "
584 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
585 ifp->if_xname,
586 h->address, h->control, ntohs(h->protocol));
587 goto drop;
588 }
589 switch (ntohs(h->protocol)) {
590 default:
591 ++ifp->if_noproto;
592 goto invalid;
593 case CISCO_KEEPALIVE:
594 SPPP_UNLOCK(sp);
595 sppp_cisco_input((struct sppp *) ifp, m);
596 m_freem(m);
597 return;
598 #ifdef INET
599 case ETHERTYPE_IP:
600 pktq = ip_pktq;
601 break;
602 #endif
603 #ifdef INET6
604 case ETHERTYPE_IPV6:
605 pktq = ip6_pktq;
606 break;
607 #endif
608 }
609 goto queue_pkt;
610 default: /* Invalid PPP packet. */
611 invalid:
612 if (debug)
613 log(LOG_DEBUG,
614 "%s: invalid input packet "
615 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
616 ifp->if_xname,
617 h->address, h->control, ntohs(h->protocol));
618 goto drop;
619 }
620 protocol = ntohs(h->protocol);
621 }
622
623 switch (protocol) {
624 default:
625 if (sp->state[IDX_LCP] == STATE_OPENED) {
626 uint16_t prot = htons(protocol);
627
628 SPPP_UPGRADE(sp);
629 sppp_cp_send(sp, PPP_LCP, PROTO_REJ,
630 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
631 &prot);
632 SPPP_DOWNGRADE(sp);
633 }
634 if (debug)
635 log(LOG_DEBUG,
636 "%s: invalid input protocol "
637 "<proto=0x%x>\n", ifp->if_xname, ntohs(protocol));
638 ++ifp->if_noproto;
639 goto drop;
640 case PPP_LCP:
641 SPPP_UNLOCK(sp);
642 sppp_cp_input(&lcp, sp, m);
643 m_freem(m);
644 return;
645 case PPP_PAP:
646 SPPP_UNLOCK(sp);
647 if (sp->pp_phase >= SPPP_PHASE_AUTHENTICATE) {
648 sppp_pap_input(sp, m);
649 }
650 m_freem(m);
651 return;
652 case PPP_CHAP:
653 SPPP_UNLOCK(sp);
654 if (sp->pp_phase >= SPPP_PHASE_AUTHENTICATE) {
655 sppp_chap_input(sp, m);
656 }
657 m_freem(m);
658 return;
659 #ifdef INET
660 case PPP_IPCP:
661 SPPP_UNLOCK(sp);
662 if (sp->pp_phase == SPPP_PHASE_NETWORK) {
663 sppp_cp_input(&ipcp, sp, m);
664 }
665 m_freem(m);
666 return;
667 case PPP_IP:
668 if (sp->state[IDX_IPCP] == STATE_OPENED) {
669 sp->pp_last_activity = time_uptime;
670 pktq = ip_pktq;
671 }
672 break;
673 #endif
674 #ifdef INET6
675 case PPP_IPV6CP:
676 SPPP_UNLOCK(sp);
677 if (sp->pp_phase == SPPP_PHASE_NETWORK) {
678 sppp_cp_input(&ipv6cp, sp, m);
679 }
680 m_freem(m);
681 return;
682
683 case PPP_IPV6:
684 if (sp->state[IDX_IPV6CP] == STATE_OPENED) {
685 sp->pp_last_activity = time_uptime;
686 pktq = ip6_pktq;
687 }
688 break;
689 #endif
690 }
691
692 queue_pkt:
693 if ((ifp->if_flags & IFF_UP) == 0 || (!inq && !pktq)) {
694 goto drop;
695 }
696
697 /* Check queue. */
698 if (__predict_true(pktq)) {
699 if (__predict_false(!pktq_enqueue(pktq, m, 0))) {
700 goto drop;
701 }
702 SPPP_UNLOCK(sp);
703 return;
704 }
705
706 SPPP_UNLOCK(sp);
707
708 IFQ_LOCK(inq);
709 if (IF_QFULL(inq)) {
710 /* Queue overflow. */
711 IF_DROP(inq);
712 IFQ_UNLOCK(inq);
713 if (debug)
714 log(LOG_DEBUG, "%s: protocol queue overflow\n",
715 ifp->if_xname);
716
717 SPPP_LOCK(sp, RW_READER);
718 goto drop;
719 }
720 IF_ENQUEUE(inq, m);
721 IFQ_UNLOCK(inq);
722 schednetisr(isr);
723 }
724
725 /*
726 * Enqueue transmit packet.
727 */
728 static int
729 sppp_output(struct ifnet *ifp, struct mbuf *m,
730 const struct sockaddr *dst, const struct rtentry *rt)
731 {
732 struct sppp *sp = (struct sppp *) ifp;
733 struct ppp_header *h = NULL;
734 #ifndef SPPPSUBR_MPSAFE
735 struct ifqueue *ifq = NULL; /* XXX */
736 #endif
737 int s, error = 0;
738 uint16_t protocol;
739 size_t pktlen;
740
741 s = splnet();
742 SPPP_LOCK(sp, RW_READER);
743
744 sp->pp_last_activity = time_uptime;
745
746 if ((ifp->if_flags & IFF_UP) == 0 ||
747 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
748 SPPP_UNLOCK(sp);
749 splx(s);
750
751 m_freem(m);
752
753 return (ENETDOWN);
754 }
755
756 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
757 /*
758 * Interface is not yet running, but auto-dial. Need
759 * to start LCP for it.
760 */
761 ifp->if_flags |= IFF_RUNNING;
762
763 SPPP_UNLOCK(sp);
764 splx(s);
765
766 SPPP_LOCK(sp, RW_WRITER);
767 lcp.Open(sp);
768 SPPP_UNLOCK(sp);
769
770 s = splnet();
771 SPPP_LOCK(sp, RW_READER);
772 }
773
774 /*
775 * If the queueing discipline needs packet classification,
776 * do it before prepending link headers.
777 */
778 IFQ_CLASSIFY(&ifp->if_snd, m, dst->sa_family);
779
780 #ifdef INET
781 if (dst->sa_family == AF_INET) {
782 struct ip *ip = NULL;
783 #ifndef SPPPSUBR_MPSAFE
784 struct tcphdr *th = NULL;
785 #endif
786
787 if (m->m_len >= sizeof(struct ip)) {
788 ip = mtod(m, struct ip *);
789 #ifndef SPPPSUBR_MPSAFE
790 if (ip->ip_p == IPPROTO_TCP &&
791 m->m_len >= sizeof(struct ip) + (ip->ip_hl << 2) +
792 sizeof(struct tcphdr)) {
793 th = (struct tcphdr *)
794 ((char *)ip + (ip->ip_hl << 2));
795 }
796 #endif
797 } else
798 ip = NULL;
799
800 /*
801 * When using dynamic local IP address assignment by using
802 * 0.0.0.0 as a local address, the first TCP session will
803 * not connect because the local TCP checksum is computed
804 * using 0.0.0.0 which will later become our real IP address
805 * so the TCP checksum computed at the remote end will
806 * become invalid. So we
807 * - don't let packets with src ip addr 0 thru
808 * - we flag TCP packets with src ip 0 as an error
809 */
810 if (ip && ip->ip_src.s_addr == INADDR_ANY) {
811 uint8_t proto = ip->ip_p;
812
813 SPPP_UNLOCK(sp);
814 splx(s);
815
816 m_freem(m);
817 if (proto == IPPROTO_TCP)
818 return (EADDRNOTAVAIL);
819 else
820 return (0);
821 }
822
823 #ifndef SPPPSUBR_MPSAFE
824 /*
825 * Put low delay, telnet, rlogin and ftp control packets
826 * in front of the queue.
827 */
828 if (!IF_QFULL(&sp->pp_fastq) &&
829 ((ip && (ip->ip_tos & IPTOS_LOWDELAY)) ||
830 (th && (INTERACTIVE(ntohs(th->th_sport)) ||
831 INTERACTIVE(ntohs(th->th_dport))))))
832 ifq = &sp->pp_fastq;
833 #endif /* !SPPPSUBR_MPSAFE */
834 }
835 #endif
836
837 #ifdef INET6
838 if (dst->sa_family == AF_INET6) {
839 /* XXX do something tricky here? */
840 }
841 #endif
842
843 if ((sp->pp_flags & PP_NOFRAMING) == 0) {
844 /*
845 * Prepend general data packet PPP header. For now, IP only.
846 */
847 M_PREPEND(m, PPP_HEADER_LEN, M_DONTWAIT);
848 if (! m) {
849 if (ifp->if_flags & IFF_DEBUG)
850 log(LOG_DEBUG, "%s: no memory for transmit header\n",
851 ifp->if_xname);
852 ++ifp->if_oerrors;
853 SPPP_UNLOCK(sp);
854 splx(s);
855 return (ENOBUFS);
856 }
857 /*
858 * May want to check size of packet
859 * (albeit due to the implementation it's always enough)
860 */
861 h = mtod(m, struct ppp_header *);
862 if (sp->pp_flags & PP_CISCO) {
863 h->address = CISCO_UNICAST; /* unicast address */
864 h->control = 0;
865 } else {
866 h->address = PPP_ALLSTATIONS; /* broadcast address */
867 h->control = PPP_UI; /* Unnumbered Info */
868 }
869 }
870
871 switch (dst->sa_family) {
872 #ifdef INET
873 case AF_INET: /* Internet Protocol */
874 if (sp->pp_flags & PP_CISCO)
875 protocol = htons(ETHERTYPE_IP);
876 else {
877 /*
878 * Don't choke with an ENETDOWN early. It's
879 * possible that we just started dialing out,
880 * so don't drop the packet immediately. If
881 * we notice that we run out of buffer space
882 * below, we will however remember that we are
883 * not ready to carry IP packets, and return
884 * ENETDOWN, as opposed to ENOBUFS.
885 */
886 protocol = htons(PPP_IP);
887 if (sp->state[IDX_IPCP] != STATE_OPENED)
888 error = ENETDOWN;
889 }
890 break;
891 #endif
892 #ifdef INET6
893 case AF_INET6: /* Internet Protocol version 6 */
894 if (sp->pp_flags & PP_CISCO)
895 protocol = htons(ETHERTYPE_IPV6);
896 else {
897 /*
898 * Don't choke with an ENETDOWN early. It's
899 * possible that we just started dialing out,
900 * so don't drop the packet immediately. If
901 * we notice that we run out of buffer space
902 * below, we will however remember that we are
903 * not ready to carry IP packets, and return
904 * ENETDOWN, as opposed to ENOBUFS.
905 */
906 protocol = htons(PPP_IPV6);
907 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
908 error = ENETDOWN;
909 }
910 break;
911 #endif
912 default:
913 m_freem(m);
914 ++ifp->if_oerrors;
915 SPPP_UNLOCK(sp);
916 splx(s);
917 return (EAFNOSUPPORT);
918 }
919
920 if (sp->pp_flags & PP_NOFRAMING) {
921 M_PREPEND(m, 2, M_DONTWAIT);
922 if (m == NULL) {
923 if (ifp->if_flags & IFF_DEBUG)
924 log(LOG_DEBUG, "%s: no memory for transmit header\n",
925 ifp->if_xname);
926 ++ifp->if_oerrors;
927 SPPP_UNLOCK(sp);
928 splx(s);
929 return (ENOBUFS);
930 }
931 *mtod(m, uint16_t *) = protocol;
932 } else {
933 h->protocol = protocol;
934 }
935
936 pktlen = m->m_pkthdr.len;
937 #ifdef SPPPSUBR_MPSAFE
938 SPPP_UNLOCK(sp);
939 error = if_transmit_lock(ifp, m);
940 SPPP_LOCK(sp, RW_READER);
941 if (error == 0)
942 ifp->if_obytes += pktlen + sp->pp_framebytes;
943 #else /* !SPPPSUBR_MPSAFE */
944 error = ifq_enqueue2(ifp, ifq, m);
945
946 if (error == 0) {
947 /*
948 * Count output packets and bytes.
949 * The packet length includes header + additional hardware
950 * framing according to RFC 1333.
951 */
952 if (!(ifp->if_flags & IFF_OACTIVE)) {
953 SPPP_UNLOCK(sp);
954 if_start_lock(ifp);
955 SPPP_LOCK(sp, RW_READER);
956 }
957 ifp->if_obytes += pktlen + sp->pp_framebytes;
958 }
959 #endif /* !SPPPSUBR_MPSAFE */
960 SPPP_UNLOCK(sp);
961 splx(s);
962 return error;
963 }
964
965 static int
966 sppp_mediachange(struct ifnet *ifp)
967 {
968
969 return (0);
970 }
971
972 static void
973 sppp_mediastatus(struct ifnet *ifp, struct ifmediareq *imr)
974 {
975 struct sppp *sp = (struct sppp *)ifp;
976
977 SPPP_LOCK(sp, RW_WRITER);
978
979 switch (ifp->if_link_state) {
980 case LINK_STATE_UP:
981 imr->ifm_status = IFM_AVALID | IFM_ACTIVE;
982 break;
983 case LINK_STATE_DOWN:
984 imr->ifm_status = IFM_AVALID;
985 break;
986 default:
987 /* Should be impossible as we set link state down in attach. */
988 imr->ifm_status = 0;
989 break;
990 }
991
992 SPPP_UNLOCK(sp);
993 }
994
995 void
996 sppp_attach(struct ifnet *ifp)
997 {
998 struct sppp *sp = (struct sppp *) ifp;
999
1000 /* Initialize keepalive handler. */
1001 if (! spppq) {
1002 callout_init(&keepalive_ch, CALLOUT_MPSAFE);
1003 callout_reset(&keepalive_ch, hz * LCP_KEEPALIVE_INTERVAL, sppp_keepalive, NULL);
1004 }
1005
1006 if (! spppq_lock)
1007 spppq_lock = mutex_obj_alloc(MUTEX_DEFAULT, IPL_SOFTNET);
1008
1009 /* Insert new entry into the keepalive list. */
1010 sp->pp_next = spppq;
1011 spppq = sp;
1012
1013 sp->pp_if.if_type = IFT_PPP;
1014 sp->pp_if.if_output = sppp_output;
1015 sp->pp_fastq.ifq_maxlen = 32;
1016 sp->pp_cpq.ifq_maxlen = 20;
1017 sp->pp_loopcnt = 0;
1018 sp->pp_alivecnt = 0;
1019 sp->pp_last_activity = 0;
1020 sp->pp_last_receive = 0;
1021 sp->pp_maxalive = DEFAULT_MAXALIVECNT;
1022 sp->pp_max_noreceive = DEFAULT_NORECV_TIME;
1023 sp->pp_idle_timeout = 0;
1024 memset(&sp->pp_seq[0], 0, sizeof(sp->pp_seq));
1025 memset(&sp->pp_rseq[0], 0, sizeof(sp->pp_rseq));
1026 sp->pp_auth_failures = 0;
1027 sp->pp_max_auth_fail = DEFAULT_MAX_AUTH_FAILURES;
1028 sp->pp_phase = SPPP_PHASE_DEAD;
1029 sp->pp_up = sppp_notify_up;
1030 sp->pp_down = sppp_notify_down;
1031 rw_init(&sp->pp_lock);
1032
1033 if_alloc_sadl(ifp);
1034
1035 /* Lets not beat about the bush, we know we're down. */
1036 ifp->if_link_state = LINK_STATE_DOWN;
1037 /* There is no media for PPP, but it's needed to report link status. */
1038 ifmedia_init(&sp->pp_im, 0, sppp_mediachange, sppp_mediastatus);
1039
1040 memset(&sp->myauth, 0, sizeof sp->myauth);
1041 memset(&sp->hisauth, 0, sizeof sp->hisauth);
1042 SPPP_LOCK(sp, RW_WRITER);
1043 sppp_lcp_init(sp);
1044 sppp_ipcp_init(sp);
1045 sppp_ipv6cp_init(sp);
1046 sppp_pap_init(sp);
1047 sppp_chap_init(sp);
1048 SPPP_UNLOCK(sp);
1049 }
1050
1051 void
1052 sppp_detach(struct ifnet *ifp)
1053 {
1054 struct sppp **q, *p, *sp = (struct sppp *) ifp;
1055
1056 /* Remove the entry from the keepalive list. */
1057 SPPPQ_LOCK();
1058 for (q = &spppq; (p = *q); q = &p->pp_next)
1059 if (p == sp) {
1060 *q = p->pp_next;
1061 break;
1062 }
1063 SPPPQ_UNLOCK();
1064
1065 if (! spppq) {
1066 /* Stop keepalive handler. */
1067 callout_stop(&keepalive_ch);
1068 mutex_obj_free(spppq_lock);
1069 spppq_lock = NULL;
1070 }
1071
1072 SPPP_LOCK(sp, RW_WRITER);
1073
1074 /* to avoid workqueue enqueued */
1075 atomic_swap_uint(&sp->ipcp.update_addrs_enqueued, 1);
1076 workqueue_wait(sp->ipcp.update_addrs_wq, &sp->ipcp.update_addrs_wk);
1077 workqueue_destroy(sp->ipcp.update_addrs_wq);
1078 pcq_destroy(sp->ipcp.update_addrs_q);
1079
1080 callout_stop(&sp->ch[IDX_LCP]);
1081 callout_stop(&sp->ch[IDX_IPCP]);
1082 callout_stop(&sp->ch[IDX_PAP]);
1083 callout_stop(&sp->ch[IDX_CHAP]);
1084 #ifdef INET6
1085 callout_stop(&sp->ch[IDX_IPV6CP]);
1086 #endif
1087 callout_stop(&sp->pap_my_to_ch);
1088
1089 /* free authentication info */
1090 if (sp->myauth.name) free(sp->myauth.name, M_DEVBUF);
1091 if (sp->myauth.secret) free(sp->myauth.secret, M_DEVBUF);
1092 if (sp->hisauth.name) free(sp->hisauth.name, M_DEVBUF);
1093 if (sp->hisauth.secret) free(sp->hisauth.secret, M_DEVBUF);
1094 SPPP_UNLOCK(sp);
1095 rw_destroy(&sp->pp_lock);
1096
1097 /* Safety - shouldn't be needed as there is no media to set. */
1098 ifmedia_delete_instance(&sp->pp_im, IFM_INST_ANY);
1099 }
1100
1101 /*
1102 * Flush the interface output queue.
1103 */
1104 void
1105 sppp_flush(struct ifnet *ifp)
1106 {
1107 struct sppp *sp = (struct sppp *) ifp;
1108
1109 SPPP_LOCK(sp, RW_WRITER);
1110 IFQ_PURGE(&sp->pp_if.if_snd);
1111 IF_PURGE(&sp->pp_fastq);
1112 IF_PURGE(&sp->pp_cpq);
1113 SPPP_UNLOCK(sp);
1114 }
1115
1116 /*
1117 * Check if the output queue is empty.
1118 */
1119 int
1120 sppp_isempty(struct ifnet *ifp)
1121 {
1122 struct sppp *sp = (struct sppp *) ifp;
1123 int empty, s;
1124
1125 s = splnet();
1126 SPPP_LOCK(sp, RW_READER);
1127 empty = IF_IS_EMPTY(&sp->pp_fastq) && IF_IS_EMPTY(&sp->pp_cpq) &&
1128 IFQ_IS_EMPTY(&sp->pp_if.if_snd);
1129 SPPP_UNLOCK(sp);
1130 splx(s);
1131 return (empty);
1132 }
1133
1134 /*
1135 * Get next packet to send.
1136 */
1137 struct mbuf *
1138 sppp_dequeue(struct ifnet *ifp)
1139 {
1140 struct sppp *sp = (struct sppp *) ifp;
1141 struct mbuf *m;
1142 int s;
1143
1144 s = splnet();
1145 SPPP_LOCK(sp, RW_WRITER);
1146 /*
1147 * Process only the control protocol queue until we have at
1148 * least one NCP open.
1149 *
1150 * Do always serve all three queues in Cisco mode.
1151 */
1152 IF_DEQUEUE(&sp->pp_cpq, m);
1153 if (m == NULL &&
1154 (sppp_ncp_check(sp) || (sp->pp_flags & PP_CISCO) != 0)) {
1155 IF_DEQUEUE(&sp->pp_fastq, m);
1156 if (m == NULL)
1157 IFQ_DEQUEUE(&sp->pp_if.if_snd, m);
1158 }
1159 SPPP_UNLOCK(sp);
1160 splx(s);
1161 return m;
1162 }
1163
1164 /*
1165 * Process an ioctl request. Called on low priority level.
1166 */
1167 int
1168 sppp_ioctl(struct ifnet *ifp, u_long cmd, void *data)
1169 {
1170 struct lwp *l = curlwp; /* XXX */
1171 struct ifreq *ifr = (struct ifreq *) data;
1172 struct ifaddr *ifa = (struct ifaddr *) data;
1173 struct sppp *sp = (struct sppp *) ifp;
1174 int s, error=0, going_up, going_down, newmode;
1175
1176 s = splnet();
1177 switch (cmd) {
1178 case SIOCINITIFADDR:
1179 ifa->ifa_rtrequest = p2p_rtrequest;
1180 break;
1181
1182 case SIOCSIFFLAGS:
1183 if ((error = ifioctl_common(ifp, cmd, data)) != 0)
1184 break;
1185
1186 SPPP_LOCK(sp, RW_WRITER);
1187 going_up = ifp->if_flags & IFF_UP &&
1188 (ifp->if_flags & IFF_RUNNING) == 0;
1189 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1190 ifp->if_flags & IFF_RUNNING;
1191 newmode = ifp->if_flags & (IFF_AUTO | IFF_PASSIVE);
1192 if (newmode == (IFF_AUTO | IFF_PASSIVE)) {
1193 /* sanity */
1194 newmode = IFF_PASSIVE;
1195 ifp->if_flags &= ~IFF_AUTO;
1196 }
1197
1198 if (going_up || going_down) {
1199 lcp.Close(sp);
1200 }
1201 if (going_up && newmode == 0) {
1202 /* neither auto-dial nor passive */
1203 ifp->if_flags |= IFF_RUNNING;
1204 if (!(sp->pp_flags & PP_CISCO))
1205 lcp.Open(sp);
1206 } else if (going_down) {
1207 SPPP_UNLOCK(sp);
1208 sppp_flush(ifp);
1209 SPPP_LOCK(sp, RW_WRITER);
1210
1211 ifp->if_flags &= ~IFF_RUNNING;
1212 }
1213 SPPP_UNLOCK(sp);
1214 break;
1215
1216 case SIOCSIFMTU:
1217 if (ifr->ifr_mtu < PPP_MINMRU ||
1218 ifr->ifr_mtu > sp->lcp.their_mru) {
1219 error = EINVAL;
1220 break;
1221 }
1222 /*FALLTHROUGH*/
1223 case SIOCGIFMTU:
1224 if ((error = ifioctl_common(ifp, cmd, data)) == ENETRESET)
1225 error = 0;
1226 break;
1227 case SIOCADDMULTI:
1228 case SIOCDELMULTI:
1229 break;
1230
1231 case SPPPSETAUTHCFG:
1232 case SPPPSETLCPCFG:
1233 case SPPPSETIDLETO:
1234 case SPPPSETAUTHFAILURE:
1235 case SPPPSETDNSOPTS:
1236 case SPPPSETKEEPALIVE:
1237 #if defined(COMPAT_50) || defined(MODULAR)
1238 case __SPPPSETIDLETO50:
1239 case __SPPPSETKEEPALIVE50:
1240 #endif /* COMPAT_50 || MODULAR */
1241 error = kauth_authorize_network(l->l_cred,
1242 KAUTH_NETWORK_INTERFACE,
1243 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV, ifp, (void *)cmd,
1244 NULL);
1245 if (error)
1246 break;
1247 error = sppp_params(sp, cmd, data);
1248 break;
1249
1250 case SPPPGETAUTHCFG:
1251 case SPPPGETLCPCFG:
1252 case SPPPGETAUTHFAILURES:
1253 error = kauth_authorize_network(l->l_cred,
1254 KAUTH_NETWORK_INTERFACE,
1255 KAUTH_REQ_NETWORK_INTERFACE_GETPRIV, ifp, (void *)cmd,
1256 NULL);
1257 if (error)
1258 break;
1259 error = sppp_params(sp, cmd, data);
1260 break;
1261
1262 case SPPPGETSTATUS:
1263 case SPPPGETSTATUSNCP:
1264 case SPPPGETIDLETO:
1265 case SPPPGETDNSOPTS:
1266 case SPPPGETDNSADDRS:
1267 case SPPPGETKEEPALIVE:
1268 #if defined(COMPAT_50) || defined(MODULAR)
1269 case __SPPPGETIDLETO50:
1270 case __SPPPGETKEEPALIVE50:
1271 #endif /* COMPAT_50 || MODULAR */
1272 error = sppp_params(sp, cmd, data);
1273 break;
1274
1275 case SIOCGIFMEDIA:
1276 error = ifmedia_ioctl(ifp, ifr, &sp->pp_im, cmd);
1277 break;
1278
1279 default:
1280 error = ifioctl_common(ifp, cmd, data);
1281 break;
1282 }
1283 splx(s);
1284 return (error);
1285 }
1286
1287
1288 /*
1289 * Cisco framing implementation.
1290 */
1291
1292 /*
1293 * Handle incoming Cisco keepalive protocol packets.
1294 */
1295 static void
1296 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1297 {
1298 STDDCL;
1299 struct cisco_packet *h;
1300 #ifdef INET
1301 uint32_t me, mymask = 0; /* XXX: GCC */
1302 #endif
1303
1304 SPPP_LOCK(sp, RW_WRITER);
1305
1306 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1307 if (debug)
1308 log(LOG_DEBUG,
1309 "%s: cisco invalid packet length: %d bytes\n",
1310 ifp->if_xname, m->m_pkthdr.len);
1311 SPPP_UNLOCK(sp);
1312 return;
1313 }
1314 h = mtod(m, struct cisco_packet *);
1315 if (debug)
1316 log(LOG_DEBUG,
1317 "%s: cisco input: %d bytes "
1318 "<0x%x 0x%x 0x%x 0x%x 0x%x-0x%x>\n",
1319 ifp->if_xname, m->m_pkthdr.len,
1320 ntohl(h->type), h->par1, h->par2, (u_int)h->rel,
1321 (u_int)h->time0, (u_int)h->time1);
1322 switch (ntohl(h->type)) {
1323 default:
1324 if (debug)
1325 addlog("%s: cisco unknown packet type: 0x%x\n",
1326 ifp->if_xname, ntohl(h->type));
1327 break;
1328 case CISCO_ADDR_REPLY:
1329 /* Reply on address request, ignore */
1330 break;
1331 case CISCO_KEEPALIVE_REQ:
1332 sp->pp_alivecnt = 0;
1333 sp->pp_rseq[IDX_LCP] = ntohl(h->par1);
1334 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1335 /* Local and remote sequence numbers are equal.
1336 * Probably, the line is in loopback mode. */
1337 if (sp->pp_loopcnt >= LOOPALIVECNT) {
1338 printf ("%s: loopback\n",
1339 ifp->if_xname);
1340 sp->pp_loopcnt = 0;
1341 if (ifp->if_flags & IFF_UP) {
1342 SPPP_UNLOCK(sp);
1343 if_down(ifp);
1344 SPPP_LOCK(sp, RW_WRITER);
1345
1346 IF_PURGE(&sp->pp_cpq);
1347 }
1348 }
1349 ++sp->pp_loopcnt;
1350
1351 /* Generate new local sequence number */
1352 sp->pp_seq[IDX_LCP] = cprng_fast32();
1353 break;
1354 }
1355 sp->pp_loopcnt = 0;
1356 if (! (ifp->if_flags & IFF_UP) &&
1357 (ifp->if_flags & IFF_RUNNING)) {
1358 SPPP_UNLOCK(sp);
1359 if_up(ifp);
1360 SPPP_LOCK(sp, RW_WRITER);
1361 }
1362 break;
1363 case CISCO_ADDR_REQ:
1364 #ifdef INET
1365 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1366 if (me != 0L)
1367 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1368 #endif
1369 break;
1370 }
1371 SPPP_UNLOCK(sp);
1372 }
1373
1374 /*
1375 * Send Cisco keepalive packet.
1376 */
1377 static void
1378 sppp_cisco_send(struct sppp *sp, int type, int32_t par1, int32_t par2)
1379 {
1380 STDDCL;
1381 struct ppp_header *h;
1382 struct cisco_packet *ch;
1383 struct mbuf *m;
1384 uint32_t t;
1385
1386 KASSERT(SPPP_WLOCKED(sp));
1387
1388 t = time_uptime * 1000;
1389 MGETHDR(m, M_DONTWAIT, MT_DATA);
1390 if (! m)
1391 return;
1392 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1393 m_reset_rcvif(m);
1394
1395 h = mtod(m, struct ppp_header *);
1396 h->address = CISCO_MULTICAST;
1397 h->control = 0;
1398 h->protocol = htons(CISCO_KEEPALIVE);
1399
1400 ch = (struct cisco_packet *)(h + 1);
1401 ch->type = htonl(type);
1402 ch->par1 = htonl(par1);
1403 ch->par2 = htonl(par2);
1404 ch->rel = -1;
1405
1406 ch->time0 = htons((u_short)(t >> 16));
1407 ch->time1 = htons((u_short) t);
1408
1409 if (debug)
1410 log(LOG_DEBUG,
1411 "%s: cisco output: <0x%x 0x%x 0x%x 0x%x 0x%x-0x%x>\n",
1412 ifp->if_xname, ntohl(ch->type), ch->par1,
1413 ch->par2, (u_int)ch->rel, (u_int)ch->time0,
1414 (u_int)ch->time1);
1415
1416 if (IF_QFULL(&sp->pp_cpq)) {
1417 IF_DROP(&sp->pp_fastq);
1418 IF_DROP(&ifp->if_snd);
1419 m_freem(m);
1420 ++ifp->if_oerrors;
1421 return;
1422 }
1423
1424 ifp->if_obytes += m->m_pkthdr.len + sp->pp_framebytes;
1425 IF_ENQUEUE(&sp->pp_cpq, m);
1426
1427 if (! (ifp->if_flags & IFF_OACTIVE)) {
1428 SPPP_UNLOCK(sp);
1429 if_start_lock(ifp);
1430 SPPP_LOCK(sp, RW_WRITER);
1431 }
1432 }
1433
1434 /*
1435 * PPP protocol implementation.
1436 */
1437
1438 /*
1439 * Send PPP control protocol packet.
1440 */
1441 static void
1442 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1443 u_char ident, u_short len, void *data)
1444 {
1445 STDDCL;
1446 struct lcp_header *lh;
1447 struct mbuf *m;
1448 size_t pkthdrlen;
1449
1450 KASSERT(SPPP_WLOCKED(sp));
1451
1452 pkthdrlen = (sp->pp_flags & PP_NOFRAMING) ? 2 : PPP_HEADER_LEN;
1453
1454 if (len > MHLEN - pkthdrlen - LCP_HEADER_LEN)
1455 len = MHLEN - pkthdrlen - LCP_HEADER_LEN;
1456 MGETHDR(m, M_DONTWAIT, MT_DATA);
1457 if (! m) {
1458 return;
1459 }
1460 m->m_pkthdr.len = m->m_len = pkthdrlen + LCP_HEADER_LEN + len;
1461 m_reset_rcvif(m);
1462
1463 if (sp->pp_flags & PP_NOFRAMING) {
1464 *mtod(m, uint16_t *) = htons(proto);
1465 lh = (struct lcp_header *)(mtod(m, uint8_t *) + 2);
1466 } else {
1467 struct ppp_header *h;
1468 h = mtod(m, struct ppp_header *);
1469 h->address = PPP_ALLSTATIONS; /* broadcast address */
1470 h->control = PPP_UI; /* Unnumbered Info */
1471 h->protocol = htons(proto); /* Link Control Protocol */
1472 lh = (struct lcp_header *)(h + 1);
1473 }
1474 lh->type = type;
1475 lh->ident = ident;
1476 lh->len = htons(LCP_HEADER_LEN + len);
1477 if (len)
1478 memcpy(lh + 1, data, len);
1479
1480 if (debug) {
1481 log(LOG_DEBUG, "%s: %s output <%s id=0x%x len=%d",
1482 ifp->if_xname,
1483 sppp_proto_name(proto),
1484 sppp_cp_type_name(lh->type), lh->ident, ntohs(lh->len));
1485 if (len)
1486 sppp_print_bytes((u_char *)(lh + 1), len);
1487 addlog(">\n");
1488 }
1489 if (IF_QFULL(&sp->pp_cpq)) {
1490 IF_DROP(&sp->pp_fastq);
1491 IF_DROP(&ifp->if_snd);
1492 m_freem(m);
1493 ++ifp->if_oerrors;
1494 return;
1495 }
1496
1497 ifp->if_obytes += m->m_pkthdr.len + sp->pp_framebytes;
1498 IF_ENQUEUE(&sp->pp_cpq, m);
1499
1500
1501 if (! (ifp->if_flags & IFF_OACTIVE)) {
1502 SPPP_UNLOCK(sp);
1503 if_start_lock(ifp);
1504 SPPP_LOCK(sp, RW_WRITER);
1505 }
1506 }
1507
1508 /*
1509 * Handle incoming PPP control protocol packets.
1510 */
1511 static void
1512 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1513 {
1514 STDDCL;
1515 struct lcp_header *h;
1516 int printlen, len = m->m_pkthdr.len;
1517 int rv;
1518 u_char *p;
1519 uint32_t u32;
1520
1521 SPPP_LOCK(sp, RW_WRITER);
1522
1523 if (len < 4) {
1524 if (debug)
1525 log(LOG_DEBUG,
1526 "%s: %s invalid packet length: %d bytes\n",
1527 ifp->if_xname, cp->name, len);
1528 SPPP_UNLOCK(sp);
1529 return;
1530 }
1531 h = mtod(m, struct lcp_header *);
1532 if (debug) {
1533 printlen = ntohs(h->len);
1534 log(LOG_DEBUG,
1535 "%s: %s input(%s): <%s id=0x%x len=%d",
1536 ifp->if_xname, cp->name,
1537 sppp_state_name(sp->state[cp->protoidx]),
1538 sppp_cp_type_name(h->type), h->ident, printlen);
1539 if (len < printlen)
1540 printlen = len;
1541 if (printlen > 4)
1542 sppp_print_bytes((u_char *)(h + 1), printlen - 4);
1543 addlog(">\n");
1544 }
1545 if (len > ntohs(h->len))
1546 len = ntohs(h->len);
1547 p = (u_char *)(h + 1);
1548 switch (h->type) {
1549 case CONF_REQ:
1550 if (len < 4) {
1551 if (debug)
1552 addlog("%s: %s invalid conf-req length %d\n",
1553 ifp->if_xname, cp->name,
1554 len);
1555 ++ifp->if_ierrors;
1556 break;
1557 }
1558 /* handle states where RCR doesn't get a SCA/SCN */
1559 switch (sp->state[cp->protoidx]) {
1560 case STATE_CLOSING:
1561 case STATE_STOPPING:
1562 SPPP_UNLOCK(sp);
1563 return;
1564 case STATE_CLOSED:
1565 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1566 0, 0);
1567 SPPP_UNLOCK(sp);
1568 return;
1569 }
1570 rv = (cp->RCR)(sp, h, len);
1571 if (rv < 0) {
1572 /* fatal error, shut down */
1573 (cp->tld)(sp);
1574 sppp_lcp_tlf(sp);
1575 SPPP_UNLOCK(sp);
1576 return;
1577 }
1578 switch (sp->state[cp->protoidx]) {
1579 case STATE_OPENED:
1580 (cp->tld)(sp);
1581 (cp->scr)(sp);
1582 /* fall through... */
1583 case STATE_ACK_SENT:
1584 case STATE_REQ_SENT:
1585 sppp_cp_change_state(cp, sp, rv?
1586 STATE_ACK_SENT: STATE_REQ_SENT);
1587 break;
1588 case STATE_STOPPED:
1589 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1590 (cp->scr)(sp);
1591 sppp_cp_change_state(cp, sp, rv?
1592 STATE_ACK_SENT: STATE_REQ_SENT);
1593 break;
1594 case STATE_ACK_RCVD:
1595 if (rv) {
1596 sppp_cp_change_state(cp, sp, STATE_OPENED);
1597 if (debug)
1598 log(LOG_DEBUG, "%s: %s tlu\n",
1599 ifp->if_xname,
1600 cp->name);
1601 (cp->tlu)(sp);
1602 } else
1603 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1604 break;
1605 default:
1606 printf("%s: %s illegal %s in state %s\n",
1607 ifp->if_xname, cp->name,
1608 sppp_cp_type_name(h->type),
1609 sppp_state_name(sp->state[cp->protoidx]));
1610 ++ifp->if_ierrors;
1611 }
1612 break;
1613 case CONF_ACK:
1614 if (h->ident != sp->confid[cp->protoidx]) {
1615 if (debug)
1616 addlog("%s: %s id mismatch 0x%x != 0x%x\n",
1617 ifp->if_xname, cp->name,
1618 h->ident, sp->confid[cp->protoidx]);
1619 ++ifp->if_ierrors;
1620 break;
1621 }
1622 switch (sp->state[cp->protoidx]) {
1623 case STATE_CLOSED:
1624 case STATE_STOPPED:
1625 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1626 break;
1627 case STATE_CLOSING:
1628 case STATE_STOPPING:
1629 break;
1630 case STATE_REQ_SENT:
1631 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1632 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1633 break;
1634 case STATE_OPENED:
1635 (cp->tld)(sp);
1636 /* fall through */
1637 case STATE_ACK_RCVD:
1638 (cp->scr)(sp);
1639 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1640 break;
1641 case STATE_ACK_SENT:
1642 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1643 sppp_cp_change_state(cp, sp, STATE_OPENED);
1644 if (debug)
1645 log(LOG_DEBUG, "%s: %s tlu\n",
1646 ifp->if_xname, cp->name);
1647 (cp->tlu)(sp);
1648 break;
1649 default:
1650 printf("%s: %s illegal %s in state %s\n",
1651 ifp->if_xname, cp->name,
1652 sppp_cp_type_name(h->type),
1653 sppp_state_name(sp->state[cp->protoidx]));
1654 ++ifp->if_ierrors;
1655 }
1656 break;
1657 case CONF_NAK:
1658 case CONF_REJ:
1659 if (h->ident != sp->confid[cp->protoidx]) {
1660 if (debug)
1661 addlog("%s: %s id mismatch 0x%x != 0x%x\n",
1662 ifp->if_xname, cp->name,
1663 h->ident, sp->confid[cp->protoidx]);
1664 ++ifp->if_ierrors;
1665 break;
1666 }
1667 if (h->type == CONF_NAK)
1668 (cp->RCN_nak)(sp, h, len);
1669 else /* CONF_REJ */
1670 (cp->RCN_rej)(sp, h, len);
1671
1672 switch (sp->state[cp->protoidx]) {
1673 case STATE_CLOSED:
1674 case STATE_STOPPED:
1675 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1676 break;
1677 case STATE_REQ_SENT:
1678 case STATE_ACK_SENT:
1679 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1680 (cp->scr)(sp);
1681 break;
1682 case STATE_OPENED:
1683 (cp->tld)(sp);
1684 /* fall through */
1685 case STATE_ACK_RCVD:
1686 sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
1687 (cp->scr)(sp);
1688 break;
1689 case STATE_CLOSING:
1690 case STATE_STOPPING:
1691 break;
1692 default:
1693 printf("%s: %s illegal %s in state %s\n",
1694 ifp->if_xname, cp->name,
1695 sppp_cp_type_name(h->type),
1696 sppp_state_name(sp->state[cp->protoidx]));
1697 ++ifp->if_ierrors;
1698 }
1699 break;
1700
1701 case TERM_REQ:
1702 switch (sp->state[cp->protoidx]) {
1703 case STATE_ACK_RCVD:
1704 case STATE_ACK_SENT:
1705 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1706 /* fall through */
1707 case STATE_CLOSED:
1708 case STATE_STOPPED:
1709 case STATE_CLOSING:
1710 case STATE_STOPPING:
1711 case STATE_REQ_SENT:
1712 sta:
1713 /* Send Terminate-Ack packet. */
1714 if (debug)
1715 log(LOG_DEBUG, "%s: %s send terminate-ack\n",
1716 ifp->if_xname, cp->name);
1717 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1718 break;
1719 case STATE_OPENED:
1720 (cp->tld)(sp);
1721 sp->rst_counter[cp->protoidx] = 0;
1722 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1723 goto sta;
1724 default:
1725 printf("%s: %s illegal %s in state %s\n",
1726 ifp->if_xname, cp->name,
1727 sppp_cp_type_name(h->type),
1728 sppp_state_name(sp->state[cp->protoidx]));
1729 ++ifp->if_ierrors;
1730 }
1731 break;
1732 case TERM_ACK:
1733 switch (sp->state[cp->protoidx]) {
1734 case STATE_CLOSED:
1735 case STATE_STOPPED:
1736 case STATE_REQ_SENT:
1737 case STATE_ACK_SENT:
1738 break;
1739 case STATE_CLOSING:
1740 (cp->tlf)(sp);
1741 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1742 sppp_lcp_check_and_close(sp);
1743 break;
1744 case STATE_STOPPING:
1745 (cp->tlf)(sp);
1746 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1747 sppp_lcp_check_and_close(sp);
1748 break;
1749 case STATE_ACK_RCVD:
1750 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1751 break;
1752 case STATE_OPENED:
1753 (cp->tld)(sp);
1754 (cp->scr)(sp);
1755 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1756 break;
1757 default:
1758 printf("%s: %s illegal %s in state %s\n",
1759 ifp->if_xname, cp->name,
1760 sppp_cp_type_name(h->type),
1761 sppp_state_name(sp->state[cp->protoidx]));
1762 ++ifp->if_ierrors;
1763 }
1764 break;
1765 case CODE_REJ:
1766 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1767 log(LOG_INFO,
1768 "%s: %s: ignoring RXJ (%s) for code ?, "
1769 "danger will robinson\n",
1770 ifp->if_xname, cp->name,
1771 sppp_cp_type_name(h->type));
1772 switch (sp->state[cp->protoidx]) {
1773 case STATE_CLOSED:
1774 case STATE_STOPPED:
1775 case STATE_REQ_SENT:
1776 case STATE_ACK_SENT:
1777 case STATE_CLOSING:
1778 case STATE_STOPPING:
1779 case STATE_OPENED:
1780 break;
1781 case STATE_ACK_RCVD:
1782 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1783 break;
1784 default:
1785 printf("%s: %s illegal %s in state %s\n",
1786 ifp->if_xname, cp->name,
1787 sppp_cp_type_name(h->type),
1788 sppp_state_name(sp->state[cp->protoidx]));
1789 ++ifp->if_ierrors;
1790 }
1791 break;
1792 case PROTO_REJ:
1793 {
1794 int catastrophic;
1795 const struct cp *upper;
1796 int i;
1797 uint16_t proto;
1798
1799 catastrophic = 0;
1800 upper = NULL;
1801 proto = p[0] << 8 | p[1];
1802 for (i = 0; i < IDX_COUNT; i++) {
1803 if (cps[i]->proto == proto) {
1804 upper = cps[i];
1805 break;
1806 }
1807 }
1808 if (upper == NULL)
1809 catastrophic++;
1810
1811 if (debug)
1812 log(LOG_INFO,
1813 "%s: %s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1814 ifp->if_xname, cp->name, catastrophic ? '-' : '+',
1815 sppp_cp_type_name(h->type), proto,
1816 upper ? upper->name : "unknown",
1817 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1818
1819 /*
1820 * if we got RXJ+ against conf-req, the peer does not implement
1821 * this particular protocol type. terminate the protocol.
1822 */
1823 if (upper && !catastrophic) {
1824 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1825 upper->Close(sp);
1826 break;
1827 }
1828 }
1829
1830 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1831 switch (sp->state[cp->protoidx]) {
1832 case STATE_CLOSED:
1833 case STATE_STOPPED:
1834 case STATE_REQ_SENT:
1835 case STATE_ACK_SENT:
1836 case STATE_CLOSING:
1837 case STATE_STOPPING:
1838 case STATE_OPENED:
1839 break;
1840 case STATE_ACK_RCVD:
1841 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1842 break;
1843 default:
1844 printf("%s: %s illegal %s in state %s\n",
1845 ifp->if_xname, cp->name,
1846 sppp_cp_type_name(h->type),
1847 sppp_state_name(sp->state[cp->protoidx]));
1848 ++ifp->if_ierrors;
1849 }
1850 break;
1851 }
1852 case DISC_REQ:
1853 if (cp->proto != PPP_LCP)
1854 goto illegal;
1855 /* Discard the packet. */
1856 break;
1857 case ECHO_REQ:
1858 if (cp->proto != PPP_LCP)
1859 goto illegal;
1860 if (sp->state[cp->protoidx] != STATE_OPENED) {
1861 if (debug)
1862 addlog("%s: lcp echo req but lcp closed\n",
1863 ifp->if_xname);
1864 ++ifp->if_ierrors;
1865 break;
1866 }
1867 if (len < 8) {
1868 if (debug)
1869 addlog("%s: invalid lcp echo request "
1870 "packet length: %d bytes\n",
1871 ifp->if_xname, len);
1872 break;
1873 }
1874 memcpy(&u32, h + 1, sizeof u32);
1875 if (ntohl(u32) == sp->lcp.magic) {
1876 /* Line loopback mode detected. */
1877 printf("%s: loopback\n", ifp->if_xname);
1878 SPPP_UNLOCK(sp);
1879 if_down(ifp);
1880 SPPP_LOCK(sp, RW_WRITER);
1881
1882 IF_PURGE(&sp->pp_cpq);
1883
1884 /* Shut down the PPP link. */
1885 /* XXX */
1886 lcp.Down(sp);
1887 lcp.Up(sp);
1888 break;
1889 }
1890 u32 = htonl(sp->lcp.magic);
1891 memcpy(h + 1, &u32, sizeof u32);
1892 if (debug)
1893 addlog("%s: got lcp echo req, sending echo rep\n",
1894 ifp->if_xname);
1895 sppp_cp_send(sp, PPP_LCP, ECHO_REPLY, h->ident, len - 4,
1896 h + 1);
1897 break;
1898 case ECHO_REPLY:
1899 if (cp->proto != PPP_LCP)
1900 goto illegal;
1901 if (h->ident != sp->lcp.echoid) {
1902 ++ifp->if_ierrors;
1903 break;
1904 }
1905 if (len < 8) {
1906 if (debug)
1907 addlog("%s: lcp invalid echo reply "
1908 "packet length: %d bytes\n",
1909 ifp->if_xname, len);
1910 break;
1911 }
1912 if (debug)
1913 addlog("%s: lcp got echo rep\n",
1914 ifp->if_xname);
1915 memcpy(&u32, h + 1, sizeof u32);
1916 if (ntohl(u32) != sp->lcp.magic)
1917 sp->pp_alivecnt = 0;
1918 break;
1919 default:
1920 /* Unknown packet type -- send Code-Reject packet. */
1921 illegal:
1922 if (debug)
1923 addlog("%s: %s send code-rej for 0x%x\n",
1924 ifp->if_xname, cp->name, h->type);
1925 sppp_cp_send(sp, cp->proto, CODE_REJ,
1926 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1927 ++ifp->if_ierrors;
1928 }
1929
1930 SPPP_UNLOCK(sp);
1931 }
1932
1933
1934 /*
1935 * The generic part of all Up/Down/Open/Close/TO event handlers.
1936 * Basically, the state transition handling in the automaton.
1937 */
1938 static void
1939 sppp_up_event(const struct cp *cp, struct sppp *sp)
1940 {
1941 STDDCL;
1942
1943 KASSERT(SPPP_WLOCKED(sp));
1944
1945 if (debug)
1946 log(LOG_DEBUG, "%s: %s up(%s)\n",
1947 ifp->if_xname, cp->name,
1948 sppp_state_name(sp->state[cp->protoidx]));
1949
1950 switch (sp->state[cp->protoidx]) {
1951 case STATE_INITIAL:
1952 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1953 break;
1954 case STATE_STARTING:
1955 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1956 (cp->scr)(sp);
1957 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1958 break;
1959 default:
1960 printf("%s: %s illegal up in state %s\n",
1961 ifp->if_xname, cp->name,
1962 sppp_state_name(sp->state[cp->protoidx]));
1963 }
1964 }
1965
1966 static void
1967 sppp_down_event(const struct cp *cp, struct sppp *sp)
1968 {
1969 STDDCL;
1970
1971 KASSERT(SPPP_WLOCKED(sp));
1972
1973 if (debug)
1974 log(LOG_DEBUG, "%s: %s down(%s)\n",
1975 ifp->if_xname, cp->name,
1976 sppp_state_name(sp->state[cp->protoidx]));
1977
1978 switch (sp->state[cp->protoidx]) {
1979 case STATE_CLOSED:
1980 case STATE_CLOSING:
1981 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1982 break;
1983 case STATE_STOPPED:
1984 (cp->tls)(sp);
1985 /* fall through */
1986 case STATE_STOPPING:
1987 case STATE_REQ_SENT:
1988 case STATE_ACK_RCVD:
1989 case STATE_ACK_SENT:
1990 sppp_cp_change_state(cp, sp, STATE_STARTING);
1991 break;
1992 case STATE_OPENED:
1993 (cp->tld)(sp);
1994 sppp_cp_change_state(cp, sp, STATE_STARTING);
1995 break;
1996 default:
1997 printf("%s: %s illegal down in state %s\n",
1998 ifp->if_xname, cp->name,
1999 sppp_state_name(sp->state[cp->protoidx]));
2000 }
2001 }
2002
2003
2004 static void
2005 sppp_open_event(const struct cp *cp, struct sppp *sp)
2006 {
2007 STDDCL;
2008
2009 KASSERT(SPPP_WLOCKED(sp));
2010
2011 if (debug)
2012 log(LOG_DEBUG, "%s: %s open(%s)\n",
2013 ifp->if_xname, cp->name,
2014 sppp_state_name(sp->state[cp->protoidx]));
2015
2016 switch (sp->state[cp->protoidx]) {
2017 case STATE_INITIAL:
2018 sppp_cp_change_state(cp, sp, STATE_STARTING);
2019 (cp->tls)(sp);
2020 break;
2021 case STATE_STARTING:
2022 break;
2023 case STATE_CLOSED:
2024 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
2025 (cp->scr)(sp);
2026 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2027 break;
2028 case STATE_STOPPED:
2029 case STATE_STOPPING:
2030 case STATE_REQ_SENT:
2031 case STATE_ACK_RCVD:
2032 case STATE_ACK_SENT:
2033 case STATE_OPENED:
2034 break;
2035 case STATE_CLOSING:
2036 sppp_cp_change_state(cp, sp, STATE_STOPPING);
2037 break;
2038 }
2039 }
2040
2041
2042 static void
2043 sppp_close_event(const struct cp *cp, struct sppp *sp)
2044 {
2045 STDDCL;
2046
2047 KASSERT(SPPP_WLOCKED(sp));
2048
2049 if (debug)
2050 log(LOG_DEBUG, "%s: %s close(%s)\n",
2051 ifp->if_xname, cp->name,
2052 sppp_state_name(sp->state[cp->protoidx]));
2053
2054 switch (sp->state[cp->protoidx]) {
2055 case STATE_INITIAL:
2056 case STATE_CLOSED:
2057 case STATE_CLOSING:
2058 break;
2059 case STATE_STARTING:
2060 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2061 (cp->tlf)(sp);
2062 break;
2063 case STATE_STOPPED:
2064 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2065 break;
2066 case STATE_STOPPING:
2067 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2068 break;
2069 case STATE_OPENED:
2070 (cp->tld)(sp);
2071 /* fall through */
2072 case STATE_REQ_SENT:
2073 case STATE_ACK_RCVD:
2074 case STATE_ACK_SENT:
2075 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2076 sppp_cp_send(sp, cp->proto, TERM_REQ,
2077 ++sp->pp_seq[cp->protoidx], 0, 0);
2078 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2079 break;
2080 }
2081 }
2082
2083 static void
2084 sppp_to_event(const struct cp *cp, struct sppp *sp)
2085 {
2086 STDDCL;
2087 int s;
2088
2089 KASSERT(SPPP_WLOCKED(sp));
2090
2091 s = splnet();
2092
2093 if (debug)
2094 log(LOG_DEBUG, "%s: %s TO(%s) rst_counter = %d\n",
2095 ifp->if_xname, cp->name,
2096 sppp_state_name(sp->state[cp->protoidx]),
2097 sp->rst_counter[cp->protoidx]);
2098
2099 if (--sp->rst_counter[cp->protoidx] < 0)
2100 /* TO- event */
2101 switch (sp->state[cp->protoidx]) {
2102 case STATE_CLOSING:
2103 (cp->tlf)(sp);
2104 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2105 sppp_lcp_check_and_close(sp);
2106 break;
2107 case STATE_STOPPING:
2108 (cp->tlf)(sp);
2109 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2110 sppp_lcp_check_and_close(sp);
2111 break;
2112 case STATE_REQ_SENT:
2113 case STATE_ACK_RCVD:
2114 case STATE_ACK_SENT:
2115 (cp->tlf)(sp);
2116 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2117 sppp_lcp_check_and_close(sp);
2118 break;
2119 }
2120 else
2121 /* TO+ event */
2122 switch (sp->state[cp->protoidx]) {
2123 case STATE_CLOSING:
2124 case STATE_STOPPING:
2125 sppp_cp_send(sp, cp->proto, TERM_REQ,
2126 ++sp->pp_seq[cp->protoidx], 0, 0);
2127 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2128 cp->TO, sp);
2129 break;
2130 case STATE_REQ_SENT:
2131 case STATE_ACK_RCVD:
2132 (cp->scr)(sp);
2133 /* sppp_cp_change_state() will restart the timer */
2134 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2135 break;
2136 case STATE_ACK_SENT:
2137 (cp->scr)(sp);
2138 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2139 cp->TO, sp);
2140 break;
2141 }
2142
2143 splx(s);
2144 }
2145
2146 /*
2147 * Change the state of a control protocol in the state automaton.
2148 * Takes care of starting/stopping the restart timer.
2149 */
2150 void
2151 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2152 {
2153
2154 KASSERT(SPPP_WLOCKED(sp));
2155
2156 sp->state[cp->protoidx] = newstate;
2157 callout_stop(&sp->ch[cp->protoidx]);
2158 switch (newstate) {
2159 case STATE_INITIAL:
2160 case STATE_STARTING:
2161 case STATE_CLOSED:
2162 case STATE_STOPPED:
2163 case STATE_OPENED:
2164 break;
2165 case STATE_CLOSING:
2166 case STATE_STOPPING:
2167 case STATE_REQ_SENT:
2168 case STATE_ACK_RCVD:
2169 case STATE_ACK_SENT:
2170 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2171 cp->TO, sp);
2172 break;
2173 }
2174 }
2175
2176 /*
2177 *--------------------------------------------------------------------------*
2178 * *
2179 * The LCP implementation. *
2180 * *
2181 *--------------------------------------------------------------------------*
2182 */
2183 static void
2184 sppp_lcp_init(struct sppp *sp)
2185 {
2186
2187 KASSERT(SPPP_WLOCKED(sp));
2188
2189 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2190 sp->lcp.magic = 0;
2191 sp->state[IDX_LCP] = STATE_INITIAL;
2192 sp->fail_counter[IDX_LCP] = 0;
2193 sp->pp_seq[IDX_LCP] = 0;
2194 sp->pp_rseq[IDX_LCP] = 0;
2195 sp->lcp.protos = 0;
2196
2197 /*
2198 * Initialize counters and timeout values. Note that we don't
2199 * use the 3 seconds suggested in RFC 1661 since we are likely
2200 * running on a fast link. XXX We should probably implement
2201 * the exponential backoff option. Note that these values are
2202 * relevant for all control protocols, not just LCP only.
2203 */
2204 sp->lcp.timeout = 1 * hz;
2205 sp->lcp.max_terminate = 2;
2206 sp->lcp.max_configure = 10;
2207 sp->lcp.max_failure = 10;
2208 callout_init(&sp->ch[IDX_LCP], CALLOUT_MPSAFE);
2209 }
2210
2211 static void
2212 sppp_lcp_up(struct sppp *sp)
2213 {
2214 STDDCL;
2215
2216 KASSERT(SPPP_WLOCKED(sp));
2217
2218 /* Initialize activity timestamp: opening a connection is an activity */
2219 sp->pp_last_receive = sp->pp_last_activity = time_uptime;
2220
2221 /*
2222 * If this interface is passive or dial-on-demand, and we are
2223 * still in Initial state, it means we've got an incoming
2224 * call. Activate the interface.
2225 */
2226 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2227 if (debug)
2228 log(LOG_DEBUG,
2229 "%s: Up event", ifp->if_xname);
2230 ifp->if_flags |= IFF_RUNNING;
2231 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2232 if (debug)
2233 addlog("(incoming call)\n");
2234 sp->pp_flags |= PP_CALLIN;
2235 lcp.Open(sp);
2236 } else if (debug)
2237 addlog("\n");
2238 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2239 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2240 ifp->if_flags |= IFF_RUNNING;
2241 lcp.Open(sp);
2242 }
2243
2244 sppp_up_event(&lcp, sp);
2245 }
2246
2247 static void
2248 sppp_lcp_down(struct sppp *sp)
2249 {
2250 STDDCL;
2251
2252 KASSERT(SPPP_WLOCKED(sp));
2253
2254 sppp_down_event(&lcp, sp);
2255
2256 /*
2257 * If this is neither a dial-on-demand nor a passive
2258 * interface, simulate an ``ifconfig down'' action, so the
2259 * administrator can force a redial by another ``ifconfig
2260 * up''. XXX For leased line operation, should we immediately
2261 * try to reopen the connection here?
2262 */
2263 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2264 if (debug)
2265 log(LOG_INFO,
2266 "%s: Down event (carrier loss), taking interface down.\n",
2267 ifp->if_xname);
2268 SPPP_UNLOCK(sp);
2269 if_down(ifp);
2270 SPPP_LOCK(sp, RW_WRITER);
2271 } else {
2272 if (debug)
2273 log(LOG_DEBUG,
2274 "%s: Down event (carrier loss)\n",
2275 ifp->if_xname);
2276 }
2277 sp->pp_flags &= ~PP_CALLIN;
2278 if (sp->state[IDX_LCP] != STATE_INITIAL)
2279 lcp.Close(sp);
2280 ifp->if_flags &= ~IFF_RUNNING;
2281 }
2282
2283 static void
2284 sppp_lcp_open(struct sppp *sp)
2285 {
2286
2287 KASSERT(SPPP_WLOCKED(sp));
2288
2289 if (sp->pp_if.if_mtu < PP_MTU) {
2290 sp->lcp.mru = sp->pp_if.if_mtu;
2291 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2292 } else
2293 sp->lcp.mru = PP_MTU;
2294 sp->lcp.their_mru = PP_MTU;
2295
2296 /*
2297 * If we are authenticator, negotiate LCP_AUTH
2298 */
2299 if (sp->hisauth.proto != 0)
2300 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2301 else
2302 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2303 sp->pp_flags &= ~PP_NEEDAUTH;
2304 sppp_open_event(&lcp, sp);
2305 }
2306
2307 static void
2308 sppp_lcp_close(struct sppp *sp)
2309 {
2310
2311 KASSERT(SPPP_WLOCKED(sp));
2312 sppp_close_event(&lcp, sp);
2313 }
2314
2315 static void
2316 sppp_lcp_TO(void *cookie)
2317 {
2318 struct sppp *sp = (struct sppp*)cookie;
2319
2320 SPPP_LOCK(sp, RW_WRITER);
2321 sppp_to_event(&lcp, sp);
2322 SPPP_UNLOCK(sp);
2323 }
2324
2325 /*
2326 * Analyze a configure request. Return true if it was agreeable, and
2327 * caused action sca, false if it has been rejected or nak'ed, and
2328 * caused action scn. (The return value is used to make the state
2329 * transition decision in the state automaton.)
2330 */
2331 static int
2332 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2333 {
2334 STDDCL;
2335 u_char *buf, *r, *p, l, blen;
2336 int origlen, rlen;
2337 uint32_t nmagic;
2338 u_short authproto;
2339
2340 KASSERT(SPPP_WLOCKED(sp));
2341
2342 len -= 4;
2343 origlen = len;
2344 buf = r = malloc (blen = len, M_TEMP, M_NOWAIT);
2345 if (! buf)
2346 return (0);
2347
2348 if (debug)
2349 log(LOG_DEBUG, "%s: lcp parse opts:",
2350 ifp->if_xname);
2351
2352 /* pass 1: check for things that need to be rejected */
2353 p = (void *)(h + 1);
2354 for (rlen = 0; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
2355 /* Sanity check option length */
2356 if (l > len) {
2357 /*
2358 * Malicious option - drop immediately.
2359 * XXX Maybe we should just RXJ it?
2360 */
2361 addlog("%s: received malicious LCP option 0x%02x, "
2362 "length 0x%02x, (len: 0x%02x) dropping.\n", ifp->if_xname,
2363 p[0], l, len);
2364 goto drop;
2365 }
2366 if (debug)
2367 addlog(" %s", sppp_lcp_opt_name(*p));
2368 switch (*p) {
2369 case LCP_OPT_MAGIC:
2370 /* Magic number. */
2371 /* fall through, both are same length */
2372 case LCP_OPT_ASYNC_MAP:
2373 /* Async control character map. */
2374 if (len >= 6 || l == 6)
2375 continue;
2376 if (debug)
2377 addlog(" [invalid]");
2378 break;
2379 case LCP_OPT_MRU:
2380 /* Maximum receive unit. */
2381 if (len >= 4 && l == 4)
2382 continue;
2383 if (debug)
2384 addlog(" [invalid]");
2385 break;
2386 case LCP_OPT_AUTH_PROTO:
2387 if (len < 4) {
2388 if (debug)
2389 addlog(" [invalid]");
2390 break;
2391 }
2392 authproto = (p[2] << 8) + p[3];
2393 if (authproto == PPP_CHAP && l != 5) {
2394 if (debug)
2395 addlog(" [invalid chap len]");
2396 break;
2397 }
2398 if (sp->myauth.proto == 0) {
2399 /* we are not configured to do auth */
2400 if (debug)
2401 addlog(" [not configured]");
2402 break;
2403 }
2404 /*
2405 * Remote want us to authenticate, remember this,
2406 * so we stay in SPPP_PHASE_AUTHENTICATE after LCP got
2407 * up.
2408 */
2409 sp->pp_flags |= PP_NEEDAUTH;
2410 continue;
2411 default:
2412 /* Others not supported. */
2413 if (debug)
2414 addlog(" [rej]");
2415 break;
2416 }
2417 if (rlen + l > blen) {
2418 if (debug)
2419 addlog(" [overflow]");
2420 continue;
2421 }
2422 /* Add the option to rejected list. */
2423 memcpy(r, p, l);
2424 r += l;
2425 rlen += l;
2426 }
2427 if (rlen) {
2428 if (debug)
2429 addlog(" send conf-rej\n");
2430 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2431 goto end;
2432 } else if (debug)
2433 addlog("\n");
2434
2435 /*
2436 * pass 2: check for option values that are unacceptable and
2437 * thus require to be nak'ed.
2438 */
2439 if (debug)
2440 log(LOG_DEBUG, "%s: lcp parse opt values: ",
2441 ifp->if_xname);
2442
2443 p = (void *)(h + 1);
2444 len = origlen;
2445 for (rlen = 0; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
2446 if (debug)
2447 addlog(" %s", sppp_lcp_opt_name(*p));
2448 switch (*p) {
2449 case LCP_OPT_MAGIC:
2450 /* Magic number -- extract. */
2451 nmagic = (uint32_t)p[2] << 24 |
2452 (uint32_t)p[3] << 16 | p[4] << 8 | p[5];
2453 if (nmagic != sp->lcp.magic) {
2454 if (debug)
2455 addlog(" 0x%x", nmagic);
2456 continue;
2457 }
2458 /*
2459 * Local and remote magics equal -- loopback?
2460 */
2461 if (sp->pp_loopcnt >= LOOPALIVECNT*5) {
2462 printf ("%s: loopback\n",
2463 ifp->if_xname);
2464 sp->pp_loopcnt = 0;
2465 if (ifp->if_flags & IFF_UP) {
2466 SPPP_UNLOCK(sp);
2467 if_down(ifp);
2468 SPPP_LOCK(sp, RW_WRITER);
2469
2470 IF_PURGE(&sp->pp_cpq);
2471 /* XXX ? */
2472 lcp.Down(sp);
2473 lcp.Up(sp);
2474 }
2475 } else if (debug)
2476 addlog(" [glitch]");
2477 ++sp->pp_loopcnt;
2478 /*
2479 * We negate our magic here, and NAK it. If
2480 * we see it later in an NAK packet, we
2481 * suggest a new one.
2482 */
2483 nmagic = ~sp->lcp.magic;
2484 /* Gonna NAK it. */
2485 p[2] = nmagic >> 24;
2486 p[3] = nmagic >> 16;
2487 p[4] = nmagic >> 8;
2488 p[5] = nmagic;
2489 break;
2490
2491 case LCP_OPT_ASYNC_MAP:
2492 /*
2493 * Async control character map -- just ignore it.
2494 *
2495 * Quote from RFC 1662, chapter 6:
2496 * To enable this functionality, synchronous PPP
2497 * implementations MUST always respond to the
2498 * Async-Control-Character-Map Configuration
2499 * Option with the LCP Configure-Ack. However,
2500 * acceptance of the Configuration Option does
2501 * not imply that the synchronous implementation
2502 * will do any ACCM mapping. Instead, all such
2503 * octet mapping will be performed by the
2504 * asynchronous-to-synchronous converter.
2505 */
2506 continue;
2507
2508 case LCP_OPT_MRU:
2509 /*
2510 * Maximum receive unit. Always agreeable,
2511 * but ignored by now.
2512 */
2513 sp->lcp.their_mru = p[2] * 256 + p[3];
2514 if (debug)
2515 addlog(" %ld", sp->lcp.their_mru);
2516 continue;
2517
2518 case LCP_OPT_AUTH_PROTO:
2519 authproto = (p[2] << 8) + p[3];
2520 if (sp->myauth.proto != authproto) {
2521 /* not agreed, nak */
2522 if (debug)
2523 addlog(" [mine %s != his %s]",
2524 sppp_proto_name(sp->myauth.proto),
2525 sppp_proto_name(authproto));
2526 p[2] = sp->myauth.proto >> 8;
2527 p[3] = sp->myauth.proto;
2528 break;
2529 }
2530 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2531 if (debug)
2532 addlog(" [chap not MD5]");
2533 p[4] = CHAP_MD5;
2534 break;
2535 }
2536 continue;
2537 }
2538 if (rlen + l > blen) {
2539 if (debug)
2540 addlog(" [overflow]");
2541 continue;
2542 }
2543 /* Add the option to nak'ed list. */
2544 memcpy(r, p, l);
2545 r += l;
2546 rlen += l;
2547 }
2548 if (rlen) {
2549 if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2550 if (debug)
2551 addlog(" max_failure (%d) exceeded, "
2552 "send conf-rej\n",
2553 sp->lcp.max_failure);
2554 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2555 } else {
2556 if (debug)
2557 addlog(" send conf-nak\n");
2558 sppp_cp_send(sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2559 }
2560 goto end;
2561 } else {
2562 if (debug)
2563 addlog(" send conf-ack\n");
2564 sp->fail_counter[IDX_LCP] = 0;
2565 sp->pp_loopcnt = 0;
2566 sppp_cp_send(sp, PPP_LCP, CONF_ACK, h->ident, origlen, h + 1);
2567 }
2568
2569 end:
2570 free(buf, M_TEMP);
2571 return (rlen == 0);
2572
2573 drop:
2574 free(buf, M_TEMP);
2575 return -1;
2576 }
2577
2578 /*
2579 * Analyze the LCP Configure-Reject option list, and adjust our
2580 * negotiation.
2581 */
2582 static void
2583 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2584 {
2585 STDDCL;
2586 u_char *buf, *p, l;
2587
2588 KASSERT(SPPP_WLOCKED(sp));
2589
2590 len -= 4;
2591 buf = malloc (len, M_TEMP, M_NOWAIT);
2592 if (!buf)
2593 return;
2594
2595 if (debug)
2596 log(LOG_DEBUG, "%s: lcp rej opts:",
2597 ifp->if_xname);
2598
2599 p = (void *)(h + 1);
2600 for (; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
2601 /* Sanity check option length */
2602 if (l > len) {
2603 /*
2604 * Malicious option - drop immediately.
2605 * XXX Maybe we should just RXJ it?
2606 */
2607 addlog("%s: received malicious LCP option, "
2608 "dropping.\n", ifp->if_xname);
2609 goto drop;
2610 }
2611 if (debug)
2612 addlog(" %s", sppp_lcp_opt_name(*p));
2613 switch (*p) {
2614 case LCP_OPT_MAGIC:
2615 /* Magic number -- can't use it, use 0 */
2616 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2617 sp->lcp.magic = 0;
2618 break;
2619 case LCP_OPT_MRU:
2620 /*
2621 * We try to negotiate a lower MRU if the underlying
2622 * link's MTU is less than PP_MTU (e.g. PPPoE). If the
2623 * peer rejects this lower rate, fallback to the
2624 * default.
2625 */
2626 if (debug) {
2627 addlog("%s: warning: peer rejected our MRU of "
2628 "%ld bytes. Defaulting to %d bytes\n",
2629 ifp->if_xname, sp->lcp.mru, PP_MTU);
2630 }
2631 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2632 sp->lcp.mru = PP_MTU;
2633 break;
2634 case LCP_OPT_AUTH_PROTO:
2635 /*
2636 * Peer doesn't want to authenticate himself,
2637 * deny unless this is a dialout call, and
2638 * SPPP_AUTHFLAG_NOCALLOUT is set.
2639 */
2640 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2641 (sp->hisauth.flags & SPPP_AUTHFLAG_NOCALLOUT) != 0) {
2642 if (debug)
2643 addlog(" [don't insist on auth "
2644 "for callout]");
2645 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2646 break;
2647 }
2648 if (debug)
2649 addlog("[access denied]\n");
2650 lcp.Close(sp);
2651 break;
2652 }
2653 }
2654 if (debug)
2655 addlog("\n");
2656 drop:
2657 free(buf, M_TEMP);
2658 return;
2659 }
2660
2661 /*
2662 * Analyze the LCP Configure-NAK option list, and adjust our
2663 * negotiation.
2664 */
2665 static void
2666 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2667 {
2668 STDDCL;
2669 u_char *buf, *p, l, blen;
2670 uint32_t magic;
2671
2672 KASSERT(SPPP_WLOCKED(sp));
2673
2674 len -= 4;
2675 buf = malloc (blen = len, M_TEMP, M_NOWAIT);
2676 if (!buf)
2677 return;
2678
2679 if (debug)
2680 log(LOG_DEBUG, "%s: lcp nak opts:",
2681 ifp->if_xname);
2682
2683 p = (void *)(h + 1);
2684 for (; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
2685 /* Sanity check option length */
2686 if (l > len) {
2687 /*
2688 * Malicious option - drop immediately.
2689 * XXX Maybe we should just RXJ it?
2690 */
2691 addlog("%s: received malicious LCP option, "
2692 "dropping.\n", ifp->if_xname);
2693 goto drop;
2694 }
2695 if (debug)
2696 addlog(" %s", sppp_lcp_opt_name(*p));
2697 switch (*p) {
2698 case LCP_OPT_MAGIC:
2699 /* Magic number -- renegotiate */
2700 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2701 len >= 6 && l == 6) {
2702 magic = (uint32_t)p[2] << 24 |
2703 (uint32_t)p[3] << 16 | p[4] << 8 | p[5];
2704 /*
2705 * If the remote magic is our negated one,
2706 * this looks like a loopback problem.
2707 * Suggest a new magic to make sure.
2708 */
2709 if (magic == ~sp->lcp.magic) {
2710 if (debug)
2711 addlog(" magic glitch");
2712 sp->lcp.magic = cprng_fast32();
2713 } else {
2714 sp->lcp.magic = magic;
2715 if (debug)
2716 addlog(" %d", magic);
2717 }
2718 }
2719 break;
2720 case LCP_OPT_MRU:
2721 /*
2722 * Peer wants to advise us to negotiate an MRU.
2723 * Agree on it if it's reasonable, or use
2724 * default otherwise.
2725 */
2726 if (len >= 4 && l == 4) {
2727 u_int mru = p[2] * 256 + p[3];
2728 if (debug)
2729 addlog(" %d", mru);
2730 if (mru < PPP_MINMRU || mru > sp->pp_if.if_mtu)
2731 mru = sp->pp_if.if_mtu;
2732 sp->lcp.mru = mru;
2733 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2734 }
2735 break;
2736 case LCP_OPT_AUTH_PROTO:
2737 /*
2738 * Peer doesn't like our authentication method,
2739 * deny.
2740 */
2741 if (debug)
2742 addlog("[access denied]\n");
2743 lcp.Close(sp);
2744 break;
2745 }
2746 }
2747 if (debug)
2748 addlog("\n");
2749 drop:
2750 free(buf, M_TEMP);
2751 return;
2752 }
2753
2754 static void
2755 sppp_lcp_tlu(struct sppp *sp)
2756 {
2757 struct ifnet *ifp = &sp->pp_if;
2758 int i;
2759 uint32_t mask;
2760
2761 KASSERT(SPPP_WLOCKED(sp));
2762
2763 /* XXX ? */
2764 if (! (ifp->if_flags & IFF_UP) &&
2765 (ifp->if_flags & IFF_RUNNING)) {
2766 /* Coming out of loopback mode. */
2767 SPPP_UNLOCK(sp);
2768 if_up(ifp);
2769 SPPP_LOCK(sp, RW_WRITER);
2770 }
2771
2772 for (i = 0; i < IDX_COUNT; i++)
2773 if ((cps[i])->flags & CP_QUAL)
2774 (cps[i])->Open(sp);
2775
2776 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2777 (sp->pp_flags & PP_NEEDAUTH) != 0)
2778 sppp_change_phase(sp, SPPP_PHASE_AUTHENTICATE);
2779 else
2780 sppp_change_phase(sp, SPPP_PHASE_NETWORK);
2781
2782 /*
2783 * Open all authentication protocols. This is even required
2784 * if we already proceeded to network phase, since it might be
2785 * that remote wants us to authenticate, so we might have to
2786 * send a PAP request. Undesired authentication protocols
2787 * don't do anything when they get an Open event.
2788 */
2789 for (i = 0; i < IDX_COUNT; i++)
2790 if ((cps[i])->flags & CP_AUTH)
2791 (cps[i])->Open(sp);
2792
2793 if (sp->pp_phase == SPPP_PHASE_NETWORK) {
2794 /* Notify all NCPs. */
2795 for (i = 0; i < IDX_COUNT; i++)
2796 if ((cps[i])->flags & CP_NCP)
2797 (cps[i])->Open(sp);
2798 }
2799
2800 /* Send Up events to all started protos. */
2801 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1) {
2802 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2803 (cps[i])->Up(sp);
2804 }
2805 }
2806
2807 /* notify low-level driver of state change */
2808 sppp_notify_chg_wlocked(sp);
2809
2810 if (sp->pp_phase == SPPP_PHASE_NETWORK)
2811 /* if no NCP is starting, close down */
2812 sppp_lcp_check_and_close(sp);
2813 }
2814
2815 static void
2816 sppp_lcp_tld(struct sppp *sp)
2817 {
2818 int i;
2819 uint32_t mask;
2820
2821 KASSERT(SPPP_WLOCKED(sp));
2822
2823 sppp_change_phase(sp, SPPP_PHASE_TERMINATE);
2824
2825 /*
2826 * Take upper layers down. We send the Down event first and
2827 * the Close second to prevent the upper layers from sending
2828 * ``a flurry of terminate-request packets'', as the RFC
2829 * describes it.
2830 */
2831 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1) {
2832 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2833 (cps[i])->Down(sp);
2834 (cps[i])->Close(sp);
2835 }
2836 }
2837 }
2838
2839 static void
2840 sppp_lcp_tls(struct sppp *sp)
2841 {
2842
2843 KASSERT(SPPP_WLOCKED(sp));
2844
2845
2846 if (sp->pp_max_auth_fail != 0 && sp->pp_auth_failures >= sp->pp_max_auth_fail) {
2847 printf("%s: authentication failed %d times, not retrying again\n",
2848 sp->pp_if.if_xname, sp->pp_auth_failures);
2849
2850 SPPP_UNLOCK(sp);
2851 if_down(&sp->pp_if);
2852 SPPP_LOCK(sp, RW_WRITER);
2853 return;
2854 }
2855
2856 sppp_change_phase(sp, SPPP_PHASE_ESTABLISH);
2857
2858 /* Notify lower layer if desired. */
2859 sppp_notify_tls_wlocked(sp);
2860 }
2861
2862 static void
2863 sppp_lcp_tlf(struct sppp *sp)
2864 {
2865
2866 KASSERT(SPPP_WLOCKED(sp));
2867
2868 sppp_change_phase(sp, SPPP_PHASE_DEAD);
2869
2870 /* Notify lower layer if desired. */
2871 sppp_notify_tlf_wlocked(sp);
2872 }
2873
2874 static void
2875 sppp_lcp_scr(struct sppp *sp)
2876 {
2877 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2878 int i = 0;
2879 u_short authproto;
2880
2881 KASSERT(SPPP_WLOCKED(sp));
2882
2883 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2884 if (! sp->lcp.magic)
2885 sp->lcp.magic = cprng_fast32();
2886 opt[i++] = LCP_OPT_MAGIC;
2887 opt[i++] = 6;
2888 opt[i++] = sp->lcp.magic >> 24;
2889 opt[i++] = sp->lcp.magic >> 16;
2890 opt[i++] = sp->lcp.magic >> 8;
2891 opt[i++] = sp->lcp.magic;
2892 }
2893
2894 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2895 opt[i++] = LCP_OPT_MRU;
2896 opt[i++] = 4;
2897 opt[i++] = sp->lcp.mru >> 8;
2898 opt[i++] = sp->lcp.mru;
2899 }
2900
2901 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2902 authproto = sp->hisauth.proto;
2903 opt[i++] = LCP_OPT_AUTH_PROTO;
2904 opt[i++] = authproto == PPP_CHAP? 5: 4;
2905 opt[i++] = authproto >> 8;
2906 opt[i++] = authproto;
2907 if (authproto == PPP_CHAP)
2908 opt[i++] = CHAP_MD5;
2909 }
2910
2911 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2912 sppp_cp_send(sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2913 }
2914
2915 /*
2916 * Check the open NCPs, return true if at least one NCP is open.
2917 */
2918 static int
2919 sppp_ncp_check(struct sppp *sp)
2920 {
2921 int i, mask;
2922
2923 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2924 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2925 return 1;
2926 return 0;
2927 }
2928
2929 /*
2930 * Re-check the open NCPs and see if we should terminate the link.
2931 * Called by the NCPs during their tlf action handling.
2932 */
2933 static void
2934 sppp_lcp_check_and_close(struct sppp *sp)
2935 {
2936
2937 KASSERT(SPPP_WLOCKED(sp));
2938
2939 if (sp->pp_phase < SPPP_PHASE_NETWORK)
2940 /* don't bother, we are already going down */
2941 return;
2942
2943 if (sppp_ncp_check(sp))
2944 return;
2945 lcp.Close(sp);
2946 }
2947
2948
2949 /*
2950 *--------------------------------------------------------------------------*
2951 * *
2952 * The IPCP implementation. *
2953 * *
2954 *--------------------------------------------------------------------------*
2955 */
2956
2957 static void
2958 sppp_ipcp_init(struct sppp *sp)
2959 {
2960 int error;
2961
2962 KASSERT(SPPP_WLOCKED(sp));
2963
2964 sp->ipcp.opts = 0;
2965 sp->ipcp.flags = 0;
2966 sp->state[IDX_IPCP] = STATE_INITIAL;
2967 sp->fail_counter[IDX_IPCP] = 0;
2968 sp->pp_seq[IDX_IPCP] = 0;
2969 sp->pp_rseq[IDX_IPCP] = 0;
2970 callout_init(&sp->ch[IDX_IPCP], CALLOUT_MPSAFE);
2971
2972 error = workqueue_create(&sp->ipcp.update_addrs_wq, "ipcp_update_addrs",
2973 sppp_update_ip_addrs_work, sp, PRI_SOFTNET, IPL_NET, 0);
2974 if (error)
2975 panic("%s: update_addrs workqueue_create failed (%d)\n",
2976 __func__, error);
2977 sp->ipcp.update_addrs_q = pcq_create(IPCP_UPDATE_LIMIT, KM_SLEEP);
2978
2979 sp->ipcp.update_addrs_enqueued = 0;
2980 }
2981
2982 static void
2983 sppp_ipcp_up(struct sppp *sp)
2984 {
2985 KASSERT(SPPP_WLOCKED(sp));
2986 sppp_up_event(&ipcp, sp);
2987 }
2988
2989 static void
2990 sppp_ipcp_down(struct sppp *sp)
2991 {
2992 KASSERT(SPPP_WLOCKED(sp));
2993 sppp_down_event(&ipcp, sp);
2994 }
2995
2996 static void
2997 sppp_ipcp_open(struct sppp *sp)
2998 {
2999 STDDCL;
3000 uint32_t myaddr, hisaddr;
3001
3002 KASSERT(SPPP_WLOCKED(sp));
3003
3004 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN|IPCP_MYADDR_SEEN|IPCP_MYADDR_DYN|IPCP_HISADDR_DYN);
3005 sp->ipcp.req_myaddr = 0;
3006 sp->ipcp.req_hisaddr = 0;
3007 memset(&sp->dns_addrs, 0, sizeof sp->dns_addrs);
3008
3009 #ifdef INET
3010 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
3011 #else
3012 myaddr = hisaddr = 0;
3013 #endif
3014 /*
3015 * If we don't have his address, this probably means our
3016 * interface doesn't want to talk IP at all. (This could
3017 * be the case if somebody wants to speak only IPX, for
3018 * example.) Don't open IPCP in this case.
3019 */
3020 if (hisaddr == 0) {
3021 /* XXX this message should go away */
3022 if (debug)
3023 log(LOG_DEBUG, "%s: ipcp_open(): no IP interface\n",
3024 ifp->if_xname);
3025 return;
3026 }
3027
3028 if (myaddr == 0) {
3029 /*
3030 * I don't have an assigned address, so i need to
3031 * negotiate my address.
3032 */
3033 sp->ipcp.flags |= IPCP_MYADDR_DYN;
3034 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3035 }
3036 if (hisaddr == 1) {
3037 /*
3038 * XXX - remove this hack!
3039 * remote has no valid address, we need to get one assigned.
3040 */
3041 sp->ipcp.flags |= IPCP_HISADDR_DYN;
3042 }
3043 sppp_open_event(&ipcp, sp);
3044 }
3045
3046 static void
3047 sppp_ipcp_close(struct sppp *sp)
3048 {
3049
3050 KASSERT(SPPP_WLOCKED(sp));
3051
3052 sppp_close_event(&ipcp, sp);
3053
3054 #ifdef INET
3055 if (sp->ipcp.flags & (IPCP_MYADDR_DYN|IPCP_HISADDR_DYN))
3056 /*
3057 * Some address was dynamic, clear it again.
3058 */
3059 sppp_clear_ip_addrs(sp);
3060 #endif
3061 }
3062
3063 static void
3064 sppp_ipcp_TO(void *cookie)
3065 {
3066 struct sppp *sp = cookie;
3067
3068 SPPP_LOCK(sp, RW_WRITER);
3069 sppp_to_event(&ipcp, sp);
3070 SPPP_UNLOCK(sp);
3071 }
3072
3073 /*
3074 * Analyze a configure request. Return true if it was agreeable, and
3075 * caused action sca, false if it has been rejected or nak'ed, and
3076 * caused action scn. (The return value is used to make the state
3077 * transition decision in the state automaton.)
3078 */
3079 static int
3080 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3081 {
3082 u_char *buf, *r, *p, l, blen;
3083 struct ifnet *ifp = &sp->pp_if;
3084 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3085 uint32_t hisaddr, desiredaddr;
3086
3087 KASSERT(SPPP_WLOCKED(sp));
3088
3089 len -= 4;
3090 origlen = len;
3091 /*
3092 * Make sure to allocate a buf that can at least hold a
3093 * conf-nak with an `address' option. We might need it below.
3094 */
3095 blen = len < 6 ? 6 : len;
3096 buf = r = malloc (blen, M_TEMP, M_NOWAIT);
3097 if (! buf)
3098 return (0);
3099
3100 /* pass 1: see if we can recognize them */
3101 if (debug)
3102 log(LOG_DEBUG, "%s: ipcp parse opts:",
3103 ifp->if_xname);
3104 p = (void *)(h + 1);
3105 for (rlen = 0; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
3106 /* Sanity check option length */
3107 if (l > len) {
3108 /* XXX should we just RXJ? */
3109 addlog("%s: malicious IPCP option received, dropping\n",
3110 ifp->if_xname);
3111 goto drop;
3112 }
3113 if (debug)
3114 addlog(" %s", sppp_ipcp_opt_name(*p));
3115 switch (*p) {
3116 #ifdef notyet
3117 case IPCP_OPT_COMPRESSION:
3118 if (len >= 6 && l >= 6) {
3119 /* correctly formed compress option */
3120 continue;
3121 }
3122 if (debug)
3123 addlog(" [invalid]");
3124 break;
3125 #endif
3126 case IPCP_OPT_ADDRESS:
3127 if (len >= 6 && l == 6) {
3128 /* correctly formed address option */
3129 continue;
3130 }
3131 if (debug)
3132 addlog(" [invalid]");
3133 break;
3134 default:
3135 /* Others not supported. */
3136 if (debug)
3137 addlog(" [rej]");
3138 break;
3139 }
3140 /* Add the option to rejected list. */
3141 if (rlen + l > blen) {
3142 if (debug)
3143 addlog(" [overflow]");
3144 continue;
3145 }
3146 memcpy(r, p, l);
3147 r += l;
3148 rlen += l;
3149 }
3150 if (rlen) {
3151 if (debug)
3152 addlog(" send conf-rej\n");
3153 sppp_cp_send(sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
3154 goto end;
3155 } else if (debug)
3156 addlog("\n");
3157
3158 /* pass 2: parse option values */
3159 if (sp->ipcp.flags & IPCP_HISADDR_SEEN)
3160 hisaddr = sp->ipcp.req_hisaddr; /* we already aggreed on that */
3161 else
3162 #ifdef INET
3163 sppp_get_ip_addrs(sp, 0, &hisaddr, 0); /* user configuration */
3164 #else
3165 hisaddr = 0;
3166 #endif
3167 if (debug)
3168 log(LOG_DEBUG, "%s: ipcp parse opt values: ",
3169 ifp->if_xname);
3170 p = (void *)(h + 1);
3171 len = origlen;
3172 for (rlen=0; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
3173 if (debug)
3174 addlog(" %s", sppp_ipcp_opt_name(*p));
3175 switch (*p) {
3176 #ifdef notyet
3177 case IPCP_OPT_COMPRESSION:
3178 continue;
3179 #endif
3180 case IPCP_OPT_ADDRESS:
3181 desiredaddr = p[2] << 24 | p[3] << 16 |
3182 p[4] << 8 | p[5];
3183 if (desiredaddr == hisaddr ||
3184 ((sp->ipcp.flags & IPCP_HISADDR_DYN) && desiredaddr != 0)) {
3185 /*
3186 * Peer's address is same as our value,
3187 * this is agreeable. Gonna conf-ack
3188 * it.
3189 */
3190 if (debug)
3191 addlog(" %s [ack]",
3192 sppp_dotted_quad(hisaddr));
3193 /* record that we've seen it already */
3194 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3195 sp->ipcp.req_hisaddr = desiredaddr;
3196 hisaddr = desiredaddr;
3197 continue;
3198 }
3199 /*
3200 * The address wasn't agreeable. This is either
3201 * he sent us 0.0.0.0, asking to assign him an
3202 * address, or he send us another address not
3203 * matching our value. Either case, we gonna
3204 * conf-nak it with our value.
3205 */
3206 if (debug) {
3207 if (desiredaddr == 0)
3208 addlog(" [addr requested]");
3209 else
3210 addlog(" %s [not agreed]",
3211 sppp_dotted_quad(desiredaddr));
3212 }
3213
3214 p[2] = hisaddr >> 24;
3215 p[3] = hisaddr >> 16;
3216 p[4] = hisaddr >> 8;
3217 p[5] = hisaddr;
3218 break;
3219 }
3220 if (rlen + l > blen) {
3221 if (debug)
3222 addlog(" [overflow]");
3223 continue;
3224 }
3225 /* Add the option to nak'ed list. */
3226 memcpy(r, p, l);
3227 r += l;
3228 rlen += l;
3229 }
3230
3231 /*
3232 * If we are about to conf-ack the request, but haven't seen
3233 * his address so far, gonna conf-nak it instead, with the
3234 * `address' option present and our idea of his address being
3235 * filled in there, to request negotiation of both addresses.
3236 *
3237 * XXX This can result in an endless req - nak loop if peer
3238 * doesn't want to send us his address. Q: What should we do
3239 * about it? XXX A: implement the max-failure counter.
3240 */
3241 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN)) {
3242 buf[0] = IPCP_OPT_ADDRESS;
3243 buf[1] = 6;
3244 buf[2] = hisaddr >> 24;
3245 buf[3] = hisaddr >> 16;
3246 buf[4] = hisaddr >> 8;
3247 buf[5] = hisaddr;
3248 rlen = 6;
3249 if (debug)
3250 addlog(" still need hisaddr");
3251 }
3252
3253 if (rlen) {
3254 if (debug)
3255 addlog(" send conf-nak\n");
3256 sppp_cp_send(sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3257 } else {
3258 if (debug)
3259 addlog(" send conf-ack\n");
3260 sppp_cp_send(sp, PPP_IPCP, CONF_ACK, h->ident, origlen, h + 1);
3261 }
3262
3263 end:
3264 free(buf, M_TEMP);
3265 return (rlen == 0);
3266
3267 drop:
3268 free(buf, M_TEMP);
3269 return -1;
3270 }
3271
3272 /*
3273 * Analyze the IPCP Configure-Reject option list, and adjust our
3274 * negotiation.
3275 */
3276 static void
3277 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3278 {
3279 u_char *buf, *p, l, blen;
3280 struct ifnet *ifp = &sp->pp_if;
3281 int debug = ifp->if_flags & IFF_DEBUG;
3282
3283 KASSERT(SPPP_WLOCKED(sp));
3284
3285 len -= 4;
3286 buf = malloc (blen = len, M_TEMP, M_NOWAIT);
3287 if (!buf)
3288 return;
3289
3290 if (debug)
3291 log(LOG_DEBUG, "%s: ipcp rej opts:",
3292 ifp->if_xname);
3293
3294 p = (void *)(h + 1);
3295 for (; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
3296 /* Sanity check option length */
3297 if (l > len) {
3298 /* XXX should we just RXJ? */
3299 addlog("%s: malicious IPCP option received, dropping\n",
3300 ifp->if_xname);
3301 goto drop;
3302 }
3303 if (debug)
3304 addlog(" %s", sppp_ipcp_opt_name(*p));
3305 switch (*p) {
3306 case IPCP_OPT_ADDRESS:
3307 /*
3308 * Peer doesn't grok address option. This is
3309 * bad. XXX Should we better give up here?
3310 */
3311 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3312 break;
3313 #ifdef notyet
3314 case IPCP_OPT_COMPRESS:
3315 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
3316 break;
3317 #endif
3318 }
3319 }
3320 if (debug)
3321 addlog("\n");
3322 drop:
3323 free(buf, M_TEMP);
3324 return;
3325 }
3326
3327 /*
3328 * Analyze the IPCP Configure-NAK option list, and adjust our
3329 * negotiation.
3330 */
3331 static void
3332 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3333 {
3334 u_char *p, l;
3335 struct ifnet *ifp = &sp->pp_if;
3336 int debug = ifp->if_flags & IFF_DEBUG;
3337 uint32_t wantaddr;
3338
3339 KASSERT(SPPP_WLOCKED(sp));
3340
3341 len -= 4;
3342
3343 if (debug)
3344 log(LOG_DEBUG, "%s: ipcp nak opts:",
3345 ifp->if_xname);
3346
3347 p = (void *)(h + 1);
3348 for (; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
3349 /* Sanity check option length */
3350 if (l > len) {
3351 /* XXX should we just RXJ? */
3352 addlog("%s: malicious IPCP option received, dropping\n",
3353 ifp->if_xname);
3354 return;
3355 }
3356 if (debug)
3357 addlog(" %s", sppp_ipcp_opt_name(*p));
3358 switch (*p) {
3359 case IPCP_OPT_ADDRESS:
3360 /*
3361 * Peer doesn't like our local IP address. See
3362 * if we can do something for him. We'll drop
3363 * him our address then.
3364 */
3365 if (len >= 6 && l == 6) {
3366 wantaddr = p[2] << 24 | p[3] << 16 |
3367 p[4] << 8 | p[5];
3368 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3369 if (debug)
3370 addlog(" [wantaddr %s]",
3371 sppp_dotted_quad(wantaddr));
3372 /*
3373 * When doing dynamic address assignment,
3374 * we accept his offer. Otherwise, we
3375 * ignore it and thus continue to negotiate
3376 * our already existing value.
3377 */
3378 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3379 if (debug)
3380 addlog(" [agree]");
3381 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3382 sp->ipcp.req_myaddr = wantaddr;
3383 }
3384 }
3385 break;
3386
3387 case IPCP_OPT_PRIMDNS:
3388 if (len >= 6 && l == 6) {
3389 sp->dns_addrs[0] = p[2] << 24 | p[3] << 16 |
3390 p[4] << 8 | p[5];
3391 }
3392 break;
3393
3394 case IPCP_OPT_SECDNS:
3395 if (len >= 6 && l == 6) {
3396 sp->dns_addrs[1] = p[2] << 24 | p[3] << 16 |
3397 p[4] << 8 | p[5];
3398 }
3399 break;
3400 #ifdef notyet
3401 case IPCP_OPT_COMPRESS:
3402 /*
3403 * Peer wants different compression parameters.
3404 */
3405 break;
3406 #endif
3407 }
3408 }
3409 if (debug)
3410 addlog("\n");
3411 }
3412
3413 static void
3414 sppp_ipcp_tlu(struct sppp *sp)
3415 {
3416 #ifdef INET
3417 KASSERT(SPPP_WLOCKED(sp));
3418 /* we are up. Set addresses and notify anyone interested */
3419 sppp_set_ip_addrs(sp);
3420 #endif
3421 }
3422
3423 static void
3424 sppp_ipcp_tld(struct sppp *sp)
3425 {
3426
3427 KASSERT(SPPP_WLOCKED(sp));
3428 }
3429
3430 static void
3431 sppp_ipcp_tls(struct sppp *sp)
3432 {
3433
3434 KASSERT(SPPP_WLOCKED(sp));
3435 /* indicate to LCP that it must stay alive */
3436 sp->lcp.protos |= (1 << IDX_IPCP);
3437 }
3438
3439 static void
3440 sppp_ipcp_tlf(struct sppp *sp)
3441 {
3442
3443 KASSERT(SPPP_WLOCKED(sp));
3444
3445 /* we no longer need LCP */
3446 sp->lcp.protos &= ~(1 << IDX_IPCP);
3447 }
3448
3449 static void
3450 sppp_ipcp_scr(struct sppp *sp)
3451 {
3452 uint8_t opt[6 /* compression */ + 6 /* address */ + 12 /* dns addresses */];
3453 #ifdef INET
3454 uint32_t ouraddr;
3455 #endif
3456 int i = 0;
3457
3458 KASSERT(SPPP_WLOCKED(sp));
3459
3460 #ifdef notyet
3461 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3462 opt[i++] = IPCP_OPT_COMPRESSION;
3463 opt[i++] = 6;
3464 opt[i++] = 0; /* VJ header compression */
3465 opt[i++] = 0x2d; /* VJ header compression */
3466 opt[i++] = max_slot_id;
3467 opt[i++] = comp_slot_id;
3468 }
3469 #endif
3470
3471 #ifdef INET
3472 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3473 if (sp->ipcp.flags & IPCP_MYADDR_SEEN)
3474 ouraddr = sp->ipcp.req_myaddr; /* not sure if this can ever happen */
3475 else
3476 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3477 opt[i++] = IPCP_OPT_ADDRESS;
3478 opt[i++] = 6;
3479 opt[i++] = ouraddr >> 24;
3480 opt[i++] = ouraddr >> 16;
3481 opt[i++] = ouraddr >> 8;
3482 opt[i++] = ouraddr;
3483 }
3484 #endif
3485
3486 if (sp->query_dns & 1) {
3487 opt[i++] = IPCP_OPT_PRIMDNS;
3488 opt[i++] = 6;
3489 opt[i++] = sp->dns_addrs[0] >> 24;
3490 opt[i++] = sp->dns_addrs[0] >> 16;
3491 opt[i++] = sp->dns_addrs[0] >> 8;
3492 opt[i++] = sp->dns_addrs[0];
3493 }
3494 if (sp->query_dns & 2) {
3495 opt[i++] = IPCP_OPT_SECDNS;
3496 opt[i++] = 6;
3497 opt[i++] = sp->dns_addrs[1] >> 24;
3498 opt[i++] = sp->dns_addrs[1] >> 16;
3499 opt[i++] = sp->dns_addrs[1] >> 8;
3500 opt[i++] = sp->dns_addrs[1];
3501 }
3502
3503 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3504 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3505 }
3506
3507
3508 /*
3509 *--------------------------------------------------------------------------*
3510 * *
3511 * The IPv6CP implementation. *
3512 * *
3513 *--------------------------------------------------------------------------*
3514 */
3515
3516 #ifdef INET6
3517 static void
3518 sppp_ipv6cp_init(struct sppp *sp)
3519 {
3520
3521 KASSERT(SPPP_WLOCKED(sp));
3522
3523 sp->ipv6cp.opts = 0;
3524 sp->ipv6cp.flags = 0;
3525 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3526 sp->fail_counter[IDX_IPV6CP] = 0;
3527 sp->pp_seq[IDX_IPV6CP] = 0;
3528 sp->pp_rseq[IDX_IPV6CP] = 0;
3529 callout_init(&sp->ch[IDX_IPV6CP], CALLOUT_MPSAFE);
3530 }
3531
3532 static void
3533 sppp_ipv6cp_up(struct sppp *sp)
3534 {
3535
3536 KASSERT(SPPP_WLOCKED(sp));
3537 sppp_up_event(&ipv6cp, sp);
3538 }
3539
3540 static void
3541 sppp_ipv6cp_down(struct sppp *sp)
3542 {
3543
3544 KASSERT(SPPP_WLOCKED(sp));
3545 sppp_down_event(&ipv6cp, sp);
3546 }
3547
3548 static void
3549 sppp_ipv6cp_open(struct sppp *sp)
3550 {
3551 STDDCL;
3552 struct in6_addr myaddr, hisaddr;
3553
3554 KASSERT(SPPP_WLOCKED(sp));
3555
3556 #ifdef IPV6CP_MYIFID_DYN
3557 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3558 #else
3559 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3560 #endif
3561
3562 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3563 /*
3564 * If we don't have our address, this probably means our
3565 * interface doesn't want to talk IPv6 at all. (This could
3566 * be the case if somebody wants to speak only IPX, for
3567 * example.) Don't open IPv6CP in this case.
3568 */
3569 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3570 /* XXX this message should go away */
3571 if (debug)
3572 log(LOG_DEBUG, "%s: ipv6cp_open(): no IPv6 interface\n",
3573 ifp->if_xname);
3574 return;
3575 }
3576
3577 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3578 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3579 sppp_open_event(&ipv6cp, sp);
3580 }
3581
3582 static void
3583 sppp_ipv6cp_close(struct sppp *sp)
3584 {
3585
3586 KASSERT(SPPP_WLOCKED(sp));
3587 sppp_close_event(&ipv6cp, sp);
3588 }
3589
3590 static void
3591 sppp_ipv6cp_TO(void *cookie)
3592 {
3593 struct sppp *sp = cookie;
3594
3595 SPPP_LOCK(sp, RW_WRITER);
3596 sppp_to_event(&ipv6cp, sp);
3597 SPPP_UNLOCK(sp);
3598 }
3599
3600 /*
3601 * Analyze a configure request. Return true if it was agreeable, and
3602 * caused action sca, false if it has been rejected or nak'ed, and
3603 * caused action scn. (The return value is used to make the state
3604 * transition decision in the state automaton.)
3605 */
3606 static int
3607 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3608 {
3609 u_char *buf, *r, *p, l, blen;
3610 struct ifnet *ifp = &sp->pp_if;
3611 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3612 struct in6_addr myaddr, desiredaddr, suggestaddr;
3613 int ifidcount;
3614 int type;
3615 int collision, nohisaddr;
3616 char ip6buf[INET6_ADDRSTRLEN];
3617
3618 KASSERT(SPPP_WLOCKED(sp));
3619
3620 len -= 4;
3621 origlen = len;
3622 /*
3623 * Make sure to allocate a buf that can at least hold a
3624 * conf-nak with an `address' option. We might need it below.
3625 */
3626 blen = len < 6 ? 6 : len;
3627 buf = r = malloc (blen, M_TEMP, M_NOWAIT);
3628 if (! buf)
3629 return (0);
3630
3631 /* pass 1: see if we can recognize them */
3632 if (debug)
3633 log(LOG_DEBUG, "%s: ipv6cp parse opts:",
3634 ifp->if_xname);
3635 p = (void *)(h + 1);
3636 ifidcount = 0;
3637 for (rlen = 0; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
3638 /* Sanity check option length */
3639 if (l > len) {
3640 /* XXX just RXJ? */
3641 addlog("%s: received malicious IPCPv6 option, "
3642 "dropping\n", ifp->if_xname);
3643 goto drop;
3644 }
3645 if (debug)
3646 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3647 switch (*p) {
3648 case IPV6CP_OPT_IFID:
3649 if (len >= 10 && l == 10 && ifidcount == 0) {
3650 /* correctly formed address option */
3651 ifidcount++;
3652 continue;
3653 }
3654 if (debug)
3655 addlog(" [invalid]");
3656 break;
3657 #ifdef notyet
3658 case IPV6CP_OPT_COMPRESSION:
3659 if (len >= 4 && l >= 4) {
3660 /* correctly formed compress option */
3661 continue;
3662 }
3663 if (debug)
3664 addlog(" [invalid]");
3665 break;
3666 #endif
3667 default:
3668 /* Others not supported. */
3669 if (debug)
3670 addlog(" [rej]");
3671 break;
3672 }
3673 if (rlen + l > blen) {
3674 if (debug)
3675 addlog(" [overflow]");
3676 continue;
3677 }
3678 /* Add the option to rejected list. */
3679 memcpy(r, p, l);
3680 r += l;
3681 rlen += l;
3682 }
3683 if (rlen) {
3684 if (debug)
3685 addlog(" send conf-rej\n");
3686 sppp_cp_send(sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3687 goto end;
3688 } else if (debug)
3689 addlog("\n");
3690
3691 /* pass 2: parse option values */
3692 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3693 if (debug)
3694 log(LOG_DEBUG, "%s: ipv6cp parse opt values: ",
3695 ifp->if_xname);
3696 p = (void *)(h + 1);
3697 len = origlen;
3698 type = CONF_ACK;
3699 for (rlen = 0; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
3700 if (debug)
3701 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3702 switch (*p) {
3703 #ifdef notyet
3704 case IPV6CP_OPT_COMPRESSION:
3705 continue;
3706 #endif
3707 case IPV6CP_OPT_IFID:
3708 memset(&desiredaddr, 0, sizeof(desiredaddr));
3709 memcpy(&desiredaddr.s6_addr[8], &p[2], 8);
3710 collision = (memcmp(&desiredaddr.s6_addr[8],
3711 &myaddr.s6_addr[8], 8) == 0);
3712 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3713
3714 desiredaddr.s6_addr16[0] = htons(0xfe80);
3715 (void)in6_setscope(&desiredaddr, &sp->pp_if, NULL);
3716
3717 if (!collision && !nohisaddr) {
3718 /* no collision, hisaddr known - Conf-Ack */
3719 type = CONF_ACK;
3720
3721 if (debug) {
3722 addlog(" %s [%s]",
3723 IN6_PRINT(ip6buf, &desiredaddr),
3724 sppp_cp_type_name(type));
3725 }
3726 continue;
3727 }
3728
3729 memset(&suggestaddr, 0, sizeof(suggestaddr));
3730 if (collision && nohisaddr) {
3731 /* collision, hisaddr unknown - Conf-Rej */
3732 type = CONF_REJ;
3733 memset(&p[2], 0, 8);
3734 } else {
3735 /*
3736 * - no collision, hisaddr unknown, or
3737 * - collision, hisaddr known
3738 * Conf-Nak, suggest hisaddr
3739 */
3740 type = CONF_NAK;
3741 sppp_suggest_ip6_addr(sp, &suggestaddr);
3742 memcpy(&p[2], &suggestaddr.s6_addr[8], 8);
3743 }
3744 if (debug)
3745 addlog(" %s [%s]", IN6_PRINT(ip6buf, &desiredaddr),
3746 sppp_cp_type_name(type));
3747 break;
3748 }
3749 if (rlen + l > blen) {
3750 if (debug)
3751 addlog(" [overflow]");
3752 continue;
3753 }
3754 /* Add the option to nak'ed list. */
3755 memcpy(r, p, l);
3756 r += l;
3757 rlen += l;
3758 }
3759
3760 if (rlen == 0 && type == CONF_ACK) {
3761 if (debug)
3762 addlog(" send %s\n", sppp_cp_type_name(type));
3763 sppp_cp_send(sp, PPP_IPV6CP, type, h->ident, origlen, h + 1);
3764 } else {
3765 #ifdef notdef
3766 if (type == CONF_ACK)
3767 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3768 #endif
3769
3770 if (debug) {
3771 addlog(" send %s suggest %s\n",
3772 sppp_cp_type_name(type), IN6_PRINT(ip6buf, &suggestaddr));
3773 }
3774 sppp_cp_send(sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3775 }
3776
3777 end:
3778 free(buf, M_TEMP);
3779 return (rlen == 0);
3780
3781 drop:
3782 free(buf, M_TEMP);
3783 return -1;
3784 }
3785
3786 /*
3787 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3788 * negotiation.
3789 */
3790 static void
3791 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3792 {
3793 u_char *buf, *p, l, blen;
3794 struct ifnet *ifp = &sp->pp_if;
3795 int debug = ifp->if_flags & IFF_DEBUG;
3796
3797 KASSERT(SPPP_WLOCKED(sp));
3798
3799 len -= 4;
3800 buf = malloc (blen = len, M_TEMP, M_NOWAIT);
3801 if (!buf)
3802 return;
3803
3804 if (debug)
3805 log(LOG_DEBUG, "%s: ipv6cp rej opts:",
3806 ifp->if_xname);
3807
3808 p = (void *)(h + 1);
3809 for (; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
3810 if (l > len) {
3811 /* XXX just RXJ? */
3812 addlog("%s: received malicious IPCPv6 option, "
3813 "dropping\n", ifp->if_xname);
3814 goto drop;
3815 }
3816 if (debug)
3817 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3818 switch (*p) {
3819 case IPV6CP_OPT_IFID:
3820 /*
3821 * Peer doesn't grok address option. This is
3822 * bad. XXX Should we better give up here?
3823 */
3824 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3825 break;
3826 #ifdef notyet
3827 case IPV6CP_OPT_COMPRESS:
3828 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3829 break;
3830 #endif
3831 }
3832 }
3833 if (debug)
3834 addlog("\n");
3835 drop:
3836 free(buf, M_TEMP);
3837 return;
3838 }
3839
3840 /*
3841 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3842 * negotiation.
3843 */
3844 static void
3845 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3846 {
3847 u_char *buf, *p, l, blen;
3848 struct ifnet *ifp = &sp->pp_if;
3849 int debug = ifp->if_flags & IFF_DEBUG;
3850 struct in6_addr suggestaddr;
3851 char ip6buf[INET6_ADDRSTRLEN];
3852
3853 KASSERT(SPPP_WLOCKED(sp));
3854
3855 len -= 4;
3856 buf = malloc (blen = len, M_TEMP, M_NOWAIT);
3857 if (!buf)
3858 return;
3859
3860 if (debug)
3861 log(LOG_DEBUG, "%s: ipv6cp nak opts:",
3862 ifp->if_xname);
3863
3864 p = (void *)(h + 1);
3865 for (; len > 1 && (l = p[1]) != 0; len -= l, p += l) {
3866 if (l > len) {
3867 /* XXX just RXJ? */
3868 addlog("%s: received malicious IPCPv6 option, "
3869 "dropping\n", ifp->if_xname);
3870 goto drop;
3871 }
3872 if (debug)
3873 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3874 switch (*p) {
3875 case IPV6CP_OPT_IFID:
3876 /*
3877 * Peer doesn't like our local ifid. See
3878 * if we can do something for him. We'll drop
3879 * him our address then.
3880 */
3881 if (len < 10 || l != 10)
3882 break;
3883 memset(&suggestaddr, 0, sizeof(suggestaddr));
3884 suggestaddr.s6_addr16[0] = htons(0xfe80);
3885 (void)in6_setscope(&suggestaddr, &sp->pp_if, NULL);
3886 memcpy(&suggestaddr.s6_addr[8], &p[2], 8);
3887
3888 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3889 if (debug)
3890 addlog(" [suggestaddr %s]",
3891 IN6_PRINT(ip6buf, &suggestaddr));
3892 #ifdef IPV6CP_MYIFID_DYN
3893 /*
3894 * When doing dynamic address assignment,
3895 * we accept his offer.
3896 */
3897 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3898 struct in6_addr lastsuggest;
3899 /*
3900 * If <suggested myaddr from peer> equals to
3901 * <hisaddr we have suggested last time>,
3902 * we have a collision. generate new random
3903 * ifid.
3904 */
3905 sppp_suggest_ip6_addr(&lastsuggest);
3906 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3907 lastsuggest)) {
3908 if (debug)
3909 addlog(" [random]");
3910 sppp_gen_ip6_addr(sp, &suggestaddr);
3911 }
3912 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3913 if (debug)
3914 addlog(" [agree]");
3915 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3916 }
3917 #else
3918 /*
3919 * Since we do not do dynamic address assignment,
3920 * we ignore it and thus continue to negotiate
3921 * our already existing value. This can possibly
3922 * go into infinite request-reject loop.
3923 *
3924 * This is not likely because we normally use
3925 * ifid based on MAC-address.
3926 * If you have no ethernet card on the node, too bad.
3927 * XXX should we use fail_counter?
3928 */
3929 #endif
3930 break;
3931 #ifdef notyet
3932 case IPV6CP_OPT_COMPRESS:
3933 /*
3934 * Peer wants different compression parameters.
3935 */
3936 break;
3937 #endif
3938 }
3939 }
3940 if (debug)
3941 addlog("\n");
3942 drop:
3943 free(buf, M_TEMP);
3944 return;
3945 }
3946
3947 static void
3948 sppp_ipv6cp_tlu(struct sppp *sp)
3949 {
3950
3951 KASSERT(SPPP_WLOCKED(sp));
3952 /* we are up - notify isdn daemon */
3953 sppp_notify_con_wlocked(sp);
3954 }
3955
3956 static void
3957 sppp_ipv6cp_tld(struct sppp *sp)
3958 {
3959
3960 KASSERT(SPPP_WLOCKED(sp));
3961 }
3962
3963 static void
3964 sppp_ipv6cp_tls(struct sppp *sp)
3965 {
3966
3967 KASSERT(SPPP_WLOCKED(sp));
3968 /* indicate to LCP that it must stay alive */
3969 sp->lcp.protos |= (1 << IDX_IPV6CP);
3970 }
3971
3972 static void
3973 sppp_ipv6cp_tlf(struct sppp *sp)
3974 {
3975
3976 KASSERT(SPPP_WLOCKED(sp));
3977 /* we no longer need LCP */
3978 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3979 }
3980
3981 static void
3982 sppp_ipv6cp_scr(struct sppp *sp)
3983 {
3984 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3985 struct in6_addr ouraddr;
3986 int i = 0;
3987
3988 KASSERT(SPPP_WLOCKED(sp));
3989
3990 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3991 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3992 opt[i++] = IPV6CP_OPT_IFID;
3993 opt[i++] = 10;
3994 memcpy(&opt[i], &ouraddr.s6_addr[8], 8);
3995 i += 8;
3996 }
3997
3998 #ifdef notyet
3999 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
4000 opt[i++] = IPV6CP_OPT_COMPRESSION;
4001 opt[i++] = 4;
4002 opt[i++] = 0; /* TBD */
4003 opt[i++] = 0; /* TBD */
4004 /* variable length data may follow */
4005 }
4006 #endif
4007
4008 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
4009 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
4010 }
4011 #else /*INET6*/
4012 static void
4013 sppp_ipv6cp_init(struct sppp *sp)
4014 {
4015
4016 KASSERT(SPPP_WLOCKED(sp));
4017 }
4018
4019 static void
4020 sppp_ipv6cp_up(struct sppp *sp)
4021 {
4022
4023 KASSERT(SPPP_WLOCKED(sp));
4024 }
4025
4026 static void
4027 sppp_ipv6cp_down(struct sppp *sp)
4028 {
4029
4030 KASSERT(SPPP_WLOCKED(sp));
4031 }
4032
4033 static void
4034 sppp_ipv6cp_open(struct sppp *sp)
4035 {
4036
4037 KASSERT(SPPP_WLOCKED(sp));
4038 }
4039
4040 static void
4041 sppp_ipv6cp_close(struct sppp *sp)
4042 {
4043
4044 KASSERT(SPPP_WLOCKED(sp));
4045 }
4046
4047 static void
4048 sppp_ipv6cp_TO(void *cookie)
4049 {
4050 struct sppp *sp __diagused = cookie;
4051
4052 KASSERT(SPPP_WLOCKED(sp));
4053 }
4054
4055 static int
4056 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h,
4057 int len)
4058 {
4059
4060 KASSERT(SPPP_WLOCKED(sp));
4061 return 0;
4062 }
4063
4064 static void
4065 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h,
4066 int len)
4067 {
4068
4069 KASSERT(SPPP_WLOCKED(sp));
4070 }
4071
4072 static void
4073 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h,
4074 int len)
4075 {
4076
4077 KASSERT(SPPP_WLOCKED(sp));
4078 }
4079
4080 static void
4081 sppp_ipv6cp_tlu(struct sppp *sp)
4082 {
4083
4084 KASSERT(SPPP_WLOCKED(sp));
4085 }
4086
4087 static void
4088 sppp_ipv6cp_tld(struct sppp *sp)
4089 {
4090
4091 KASSERT(SPPP_WLOCKED(sp));
4092 }
4093
4094 static void
4095 sppp_ipv6cp_tls(struct sppp *sp)
4096 {
4097
4098 KASSERT(SPPP_WLOCKED(sp));
4099 }
4100
4101 static void
4102 sppp_ipv6cp_tlf(struct sppp *sp)
4103 {
4104
4105 KASSERT(SPPP_WLOCKED(sp));
4106 }
4107
4108 static void
4109 sppp_ipv6cp_scr(struct sppp *sp)
4110 {
4111
4112 KASSERT(SPPP_WLOCKED(sp));
4113 }
4114 #endif /*INET6*/
4115
4116
4117 /*
4118 *--------------------------------------------------------------------------*
4119 * *
4120 * The CHAP implementation. *
4121 * *
4122 *--------------------------------------------------------------------------*
4123 */
4124
4125 /*
4126 * The authentication protocols don't employ a full-fledged state machine as
4127 * the control protocols do, since they do have Open and Close events, but
4128 * not Up and Down, nor are they explicitly terminated. Also, use of the
4129 * authentication protocols may be different in both directions (this makes
4130 * sense, think of a machine that never accepts incoming calls but only
4131 * calls out, it doesn't require the called party to authenticate itself).
4132 *
4133 * Our state machine for the local authentication protocol (we are requesting
4134 * the peer to authenticate) looks like:
4135 *
4136 * RCA-
4137 * +--------------------------------------------+
4138 * V scn,tld|
4139 * +--------+ Close +---------+ RCA+
4140 * | |<----------------------------------| |------+
4141 * +--->| Closed | TO* | Opened | sca |
4142 * | | |-----+ +-------| |<-----+
4143 * | +--------+ irc | | +---------+
4144 * | ^ | | ^
4145 * | | | | |
4146 * | | | | |
4147 * | TO-| | | |
4148 * | |tld TO+ V | |
4149 * | | +------->+ | |
4150 * | | | | | |
4151 * | +--------+ V | |
4152 * | | |<----+<--------------------+ |
4153 * | | Req- | scr |
4154 * | | Sent | |
4155 * | | | |
4156 * | +--------+ |
4157 * | RCA- | | RCA+ |
4158 * +------+ +------------------------------------------+
4159 * scn,tld sca,irc,ict,tlu
4160 *
4161 *
4162 * with:
4163 *
4164 * Open: LCP reached authentication phase
4165 * Close: LCP reached terminate phase
4166 *
4167 * RCA+: received reply (pap-req, chap-response), acceptable
4168 * RCN: received reply (pap-req, chap-response), not acceptable
4169 * TO+: timeout with restart counter >= 0
4170 * TO-: timeout with restart counter < 0
4171 * TO*: reschedule timeout for CHAP
4172 *
4173 * scr: send request packet (none for PAP, chap-challenge)
4174 * sca: send ack packet (pap-ack, chap-success)
4175 * scn: send nak packet (pap-nak, chap-failure)
4176 * ict: initialize re-challenge timer (CHAP only)
4177 *
4178 * tlu: this-layer-up, LCP reaches network phase
4179 * tld: this-layer-down, LCP enters terminate phase
4180 *
4181 * Note that in CHAP mode, after sending a new challenge, while the state
4182 * automaton falls back into Req-Sent state, it doesn't signal a tld
4183 * event to LCP, so LCP remains in network phase. Only after not getting
4184 * any response (or after getting an unacceptable response), CHAP closes,
4185 * causing LCP to enter terminate phase.
4186 *
4187 * With PAP, there is no initial request that can be sent. The peer is
4188 * expected to send one based on the successful negotiation of PAP as
4189 * the authentication protocol during the LCP option negotiation.
4190 *
4191 * Incoming authentication protocol requests (remote requests
4192 * authentication, we are peer) don't employ a state machine at all,
4193 * they are simply answered. Some peers [Ascend P50 firmware rev
4194 * 4.50] react allergically when sending IPCP/IPv6CP requests while they are
4195 * still in authentication phase (thereby violating the standard that
4196 * demands that these NCP packets are to be discarded), so we keep
4197 * track of the peer demanding us to authenticate, and only proceed to
4198 * phase network once we've seen a positive acknowledge for the
4199 * authentication.
4200 */
4201
4202 /*
4203 * Handle incoming CHAP packets.
4204 */
4205 void
4206 sppp_chap_input(struct sppp *sp, struct mbuf *m)
4207 {
4208 STDDCL;
4209 struct lcp_header *h;
4210 int len, x;
4211 u_char *value, *name, digest[sizeof(sp->myauth.challenge)], dsize;
4212 int value_len, name_len;
4213 MD5_CTX ctx;
4214
4215 len = m->m_pkthdr.len;
4216 if (len < 4) {
4217 if (debug)
4218 log(LOG_DEBUG,
4219 "%s: chap invalid packet length: %d bytes\n",
4220 ifp->if_xname, len);
4221 return;
4222 }
4223 h = mtod(m, struct lcp_header *);
4224 if (len > ntohs(h->len))
4225 len = ntohs(h->len);
4226
4227 SPPP_LOCK(sp, RW_WRITER);
4228
4229 switch (h->type) {
4230 /* challenge, failure and success are his authproto */
4231 case CHAP_CHALLENGE:
4232 if (sp->myauth.secret == NULL || sp->myauth.name == NULL) {
4233 /* can't do anything useful */
4234 sp->pp_auth_failures++;
4235 printf("%s: chap input without my name and my secret being set\n",
4236 ifp->if_xname);
4237 break;
4238 }
4239 value = 1 + (u_char *)(h + 1);
4240 value_len = value[-1];
4241 name = value + value_len;
4242 name_len = len - value_len - 5;
4243 if (name_len < 0) {
4244 if (debug) {
4245 log(LOG_DEBUG,
4246 "%s: chap corrupted challenge "
4247 "<%s id=0x%x len=%d",
4248 ifp->if_xname,
4249 sppp_auth_type_name(PPP_CHAP, h->type),
4250 h->ident, ntohs(h->len));
4251 if (len > 4)
4252 sppp_print_bytes((u_char *)(h + 1),
4253 len - 4);
4254 addlog(">\n");
4255 }
4256 break;
4257 }
4258
4259 if (debug) {
4260 log(LOG_DEBUG,
4261 "%s: chap input <%s id=0x%x len=%d name=",
4262 ifp->if_xname,
4263 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
4264 ntohs(h->len));
4265 sppp_print_string((char *) name, name_len);
4266 addlog(" value-size=%d value=", value_len);
4267 sppp_print_bytes(value, value_len);
4268 addlog(">\n");
4269 }
4270
4271 /* Compute reply value. */
4272 MD5Init(&ctx);
4273 MD5Update(&ctx, &h->ident, 1);
4274 MD5Update(&ctx, sp->myauth.secret, sp->myauth.secret_len);
4275 MD5Update(&ctx, value, value_len);
4276 MD5Final(digest, &ctx);
4277 dsize = sizeof digest;
4278
4279 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
4280 sizeof dsize, (const char *)&dsize,
4281 sizeof digest, digest,
4282 sp->myauth.name_len,
4283 sp->myauth.name,
4284 0);
4285 break;
4286
4287 case CHAP_SUCCESS:
4288 if (debug) {
4289 log(LOG_DEBUG, "%s: chap success",
4290 ifp->if_xname);
4291 if (len > 4) {
4292 addlog(": ");
4293 sppp_print_string((char *)(h + 1), len - 4);
4294 }
4295 addlog("\n");
4296 }
4297 x = splnet();
4298 sp->pp_auth_failures = 0;
4299 sp->pp_flags &= ~PP_NEEDAUTH;
4300 if (sp->myauth.proto == PPP_CHAP &&
4301 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4302 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
4303 /*
4304 * We are authenticator for CHAP but didn't
4305 * complete yet. Leave it to tlu to proceed
4306 * to network phase.
4307 */
4308 splx(x);
4309 break;
4310 }
4311 splx(x);
4312 sppp_phase_network(sp);
4313 break;
4314
4315 case CHAP_FAILURE:
4316 x = splnet();
4317 sp->pp_auth_failures++;
4318 splx(x);
4319 if (debug) {
4320 log(LOG_INFO, "%s: chap failure",
4321 ifp->if_xname);
4322 if (len > 4) {
4323 addlog(": ");
4324 sppp_print_string((char *)(h + 1), len - 4);
4325 }
4326 addlog("\n");
4327 } else
4328 log(LOG_INFO, "%s: chap failure\n",
4329 ifp->if_xname);
4330 /* await LCP shutdown by authenticator */
4331 break;
4332
4333 /* response is my authproto */
4334 case CHAP_RESPONSE:
4335 if (sp->hisauth.secret == NULL) {
4336 /* can't do anything useful */
4337 printf("%s: chap input without his secret being set\n",
4338 ifp->if_xname);
4339 break;
4340 }
4341 value = 1 + (u_char *)(h + 1);
4342 value_len = value[-1];
4343 name = value + value_len;
4344 name_len = len - value_len - 5;
4345 if (name_len < 0) {
4346 if (debug) {
4347 log(LOG_DEBUG,
4348 "%s: chap corrupted response "
4349 "<%s id=0x%x len=%d",
4350 ifp->if_xname,
4351 sppp_auth_type_name(PPP_CHAP, h->type),
4352 h->ident, ntohs(h->len));
4353 if (len > 4)
4354 sppp_print_bytes((u_char *)(h + 1),
4355 len - 4);
4356 addlog(">\n");
4357 }
4358 break;
4359 }
4360 if (h->ident != sp->confid[IDX_CHAP]) {
4361 if (debug)
4362 log(LOG_DEBUG,
4363 "%s: chap dropping response for old ID "
4364 "(got %d, expected %d)\n",
4365 ifp->if_xname,
4366 h->ident, sp->confid[IDX_CHAP]);
4367 break;
4368 }
4369 if (sp->hisauth.name != NULL &&
4370 (name_len != sp->hisauth.name_len
4371 || memcmp(name, sp->hisauth.name, name_len) != 0)) {
4372 log(LOG_INFO, "%s: chap response, his name ",
4373 ifp->if_xname);
4374 sppp_print_string(name, name_len);
4375 addlog(" != expected ");
4376 sppp_print_string(sp->hisauth.name,
4377 sp->hisauth.name_len);
4378 addlog("\n");
4379 goto chap_failure;
4380 }
4381 if (debug) {
4382 log(LOG_DEBUG, "%s: chap input(%s) "
4383 "<%s id=0x%x len=%d name=",
4384 ifp->if_xname,
4385 sppp_state_name(sp->state[IDX_CHAP]),
4386 sppp_auth_type_name(PPP_CHAP, h->type),
4387 h->ident, ntohs(h->len));
4388 sppp_print_string((char *)name, name_len);
4389 addlog(" value-size=%d value=", value_len);
4390 sppp_print_bytes(value, value_len);
4391 addlog(">\n");
4392 }
4393 if (value_len != sizeof(sp->hisauth.challenge)) {
4394 if (debug)
4395 log(LOG_DEBUG,
4396 "%s: chap bad hash value length: "
4397 "%d bytes, should be %zu\n",
4398 ifp->if_xname, value_len,
4399 sizeof(sp->hisauth.challenge));
4400 goto chap_failure;
4401 }
4402
4403 MD5Init(&ctx);
4404 MD5Update(&ctx, &h->ident, 1);
4405 MD5Update(&ctx, sp->hisauth.secret, sp->hisauth.secret_len);
4406 MD5Update(&ctx, sp->hisauth.challenge, sizeof(sp->hisauth.challenge));
4407 MD5Final(digest, &ctx);
4408
4409 #define FAILMSG "Failed..."
4410 #define SUCCMSG "Welcome!"
4411
4412 if (value_len != sizeof digest ||
4413 memcmp(digest, value, value_len) != 0) {
4414 chap_failure:
4415 KASSERT(SPPP_WLOCKED(sp));
4416 /* action scn, tld */
4417 sp->pp_auth_failures++;
4418 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4419 sizeof(FAILMSG) - 1, (const u_char *)FAILMSG,
4420 0);
4421 chap.tld(sp);
4422 break;
4423 }
4424 sp->pp_auth_failures = 0;
4425 /* action sca, perhaps tlu */
4426 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4427 sp->state[IDX_CHAP] == STATE_OPENED)
4428 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4429 sizeof(SUCCMSG) - 1, (const u_char *)SUCCMSG,
4430 0);
4431 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4432 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4433 chap.tlu(sp);
4434 }
4435 break;
4436
4437 default:
4438 /* Unknown CHAP packet type -- ignore. */
4439 if (debug) {
4440 log(LOG_DEBUG, "%s: chap unknown input(%s) "
4441 "<0x%x id=0x%xh len=%d",
4442 ifp->if_xname,
4443 sppp_state_name(sp->state[IDX_CHAP]),
4444 h->type, h->ident, ntohs(h->len));
4445 if (len > 4)
4446 sppp_print_bytes((u_char *)(h + 1), len - 4);
4447 addlog(">\n");
4448 }
4449 break;
4450
4451 }
4452
4453 SPPP_UNLOCK(sp);
4454 }
4455
4456 static void
4457 sppp_chap_init(struct sppp *sp)
4458 {
4459
4460 KASSERT(SPPP_WLOCKED(sp));
4461
4462 /* Chap doesn't have STATE_INITIAL at all. */
4463 sp->state[IDX_CHAP] = STATE_CLOSED;
4464 sp->fail_counter[IDX_CHAP] = 0;
4465 sp->pp_seq[IDX_CHAP] = 0;
4466 sp->pp_rseq[IDX_CHAP] = 0;
4467 callout_init(&sp->ch[IDX_CHAP], CALLOUT_MPSAFE);
4468 }
4469
4470 static void
4471 sppp_chap_open(struct sppp *sp)
4472 {
4473
4474 KASSERT(SPPP_WLOCKED(sp));
4475 if (sp->hisauth.proto == PPP_CHAP &&
4476 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4477 /* we are authenticator for CHAP, start it */
4478 chap.scr(sp);
4479 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4480 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4481 }
4482 /* nothing to be done if we are peer, await a challenge */
4483 }
4484
4485 static void
4486 sppp_chap_close(struct sppp *sp)
4487 {
4488
4489 KASSERT(SPPP_WLOCKED(sp));
4490 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4491 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4492 }
4493
4494 static void
4495 sppp_chap_TO(void *cookie)
4496 {
4497 struct sppp *sp = (struct sppp *)cookie;
4498 STDDCL;
4499 int s;
4500
4501 s = splnet();
4502
4503 SPPP_LOCK(sp, RW_WRITER);
4504
4505 if (debug)
4506 log(LOG_DEBUG, "%s: chap TO(%s) rst_counter = %d\n",
4507 ifp->if_xname,
4508 sppp_state_name(sp->state[IDX_CHAP]),
4509 sp->rst_counter[IDX_CHAP]);
4510
4511 if (--sp->rst_counter[IDX_CHAP] < 0)
4512 /* TO- event */
4513 switch (sp->state[IDX_CHAP]) {
4514 case STATE_REQ_SENT:
4515 chap.tld(sp);
4516 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4517 break;
4518 }
4519 else
4520 /* TO+ (or TO*) event */
4521 switch (sp->state[IDX_CHAP]) {
4522 case STATE_OPENED:
4523 /* TO* event */
4524 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4525 /* fall through */
4526 case STATE_REQ_SENT:
4527 chap.scr(sp);
4528 /* sppp_cp_change_state() will restart the timer */
4529 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4530 break;
4531 }
4532
4533 SPPP_UNLOCK(sp);
4534 splx(s);
4535 }
4536
4537 static void
4538 sppp_chap_tlu(struct sppp *sp)
4539 {
4540 STDDCL;
4541 int i, x;
4542
4543 KASSERT(SPPP_WLOCKED(sp));
4544
4545 i = 0;
4546 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4547
4548 /*
4549 * Some broken CHAP implementations (Conware CoNet, firmware
4550 * 4.0.?) don't want to re-authenticate their CHAP once the
4551 * initial challenge-response exchange has taken place.
4552 * Provide for an option to avoid rechallenges.
4553 */
4554 if ((sp->hisauth.flags & SPPP_AUTHFLAG_NORECHALLENGE) == 0) {
4555 /*
4556 * Compute the re-challenge timeout. This will yield
4557 * a number between 300 and 810 seconds.
4558 */
4559 i = 300 + ((unsigned)(cprng_fast32() & 0xff00) >> 7);
4560
4561 callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, sp);
4562 }
4563
4564 if (debug) {
4565 log(LOG_DEBUG,
4566 "%s: chap %s, ",
4567 ifp->if_xname,
4568 sp->pp_phase == SPPP_PHASE_NETWORK? "reconfirmed": "tlu");
4569 if ((sp->hisauth.flags & SPPP_AUTHFLAG_NORECHALLENGE) == 0)
4570 addlog("next re-challenge in %d seconds\n", i);
4571 else
4572 addlog("re-challenging supressed\n");
4573 }
4574
4575 x = splnet();
4576 sp->pp_auth_failures = 0;
4577 /* indicate to LCP that we need to be closed down */
4578 sp->lcp.protos |= (1 << IDX_CHAP);
4579
4580 if (sp->pp_flags & PP_NEEDAUTH) {
4581 /*
4582 * Remote is authenticator, but his auth proto didn't
4583 * complete yet. Defer the transition to network
4584 * phase.
4585 */
4586 splx(x);
4587 return;
4588 }
4589 splx(x);
4590
4591 /*
4592 * If we are already in phase network, we are done here. This
4593 * is the case if this is a dummy tlu event after a re-challenge.
4594 */
4595 if (sp->pp_phase != SPPP_PHASE_NETWORK)
4596 sppp_phase_network(sp);
4597 }
4598
4599 static void
4600 sppp_chap_tld(struct sppp *sp)
4601 {
4602 STDDCL;
4603
4604 KASSERT(SPPP_WLOCKED(sp));
4605
4606 if (debug)
4607 log(LOG_DEBUG, "%s: chap tld\n", ifp->if_xname);
4608 callout_stop(&sp->ch[IDX_CHAP]);
4609 sp->lcp.protos &= ~(1 << IDX_CHAP);
4610
4611 lcp.Close(sp);
4612 }
4613
4614 static void
4615 sppp_chap_scr(struct sppp *sp)
4616 {
4617 uint32_t *ch;
4618 u_char clen = 4 * sizeof(uint32_t);
4619
4620 KASSERT(SPPP_WLOCKED(sp));
4621
4622 if (sp->hisauth.name == NULL) {
4623 /* can't do anything useful */
4624 printf("%s: chap starting without his name being set\n",
4625 sp->pp_if.if_xname);
4626 return;
4627 }
4628
4629 /* Compute random challenge. */
4630 ch = (uint32_t *)sp->hisauth.challenge;
4631 cprng_strong(kern_cprng, ch, clen, 0);
4632
4633 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4634
4635 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4636 sizeof clen, (const char *)&clen,
4637 sizeof(sp->hisauth.challenge), sp->hisauth.challenge,
4638 0);
4639 }
4640
4641 /*
4642 *--------------------------------------------------------------------------*
4643 * *
4644 * The PAP implementation. *
4645 * *
4646 *--------------------------------------------------------------------------*
4647 */
4648 /*
4649 * For PAP, we need to keep a little state also if we are the peer, not the
4650 * authenticator. This is since we don't get a request to authenticate, but
4651 * have to repeatedly authenticate ourself until we got a response (or the
4652 * retry counter is expired).
4653 */
4654
4655 /*
4656 * Handle incoming PAP packets. */
4657 static void
4658 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4659 {
4660 STDDCL;
4661 struct lcp_header *h;
4662 int len, x;
4663 u_char mlen;
4664 char *name, *secret;
4665 int name_len, secret_len;
4666
4667 /*
4668 * Malicious input might leave this uninitialized, so
4669 * init to an impossible value.
4670 */
4671 secret_len = -1;
4672
4673 len = m->m_pkthdr.len;
4674 if (len < 5) {
4675 if (debug)
4676 log(LOG_DEBUG,
4677 "%s: pap invalid packet length: %d bytes\n",
4678 ifp->if_xname, len);
4679 return;
4680 }
4681 h = mtod(m, struct lcp_header *);
4682 if (len > ntohs(h->len))
4683 len = ntohs(h->len);
4684
4685 SPPP_LOCK(sp, RW_WRITER);
4686
4687 switch (h->type) {
4688 /* PAP request is my authproto */
4689 case PAP_REQ:
4690 if (sp->hisauth.name == NULL || sp->hisauth.secret == NULL) {
4691 /* can't do anything useful */
4692 printf("%s: "
4693 "pap request without his name and his secret being set\n",
4694 ifp->if_xname);
4695 break;
4696 }
4697 name = 1 + (u_char *)(h + 1);
4698 name_len = name[-1];
4699 secret = name + name_len + 1;
4700 if (name_len > len - 6 ||
4701 (secret_len = secret[-1]) > len - 6 - name_len) {
4702 if (debug) {
4703 log(LOG_DEBUG, "%s: pap corrupted input "
4704 "<%s id=0x%x len=%d",
4705 ifp->if_xname,
4706 sppp_auth_type_name(PPP_PAP, h->type),
4707 h->ident, ntohs(h->len));
4708 if (len > 4)
4709 sppp_print_bytes((u_char *)(h + 1),
4710 len - 4);
4711 addlog(">\n");
4712 }
4713 break;
4714 }
4715 if (debug) {
4716 log(LOG_DEBUG, "%s: pap input(%s) "
4717 "<%s id=0x%x len=%d name=",
4718 ifp->if_xname,
4719 sppp_state_name(sp->state[IDX_PAP]),
4720 sppp_auth_type_name(PPP_PAP, h->type),
4721 h->ident, ntohs(h->len));
4722 sppp_print_string((char *)name, name_len);
4723 addlog(" secret=");
4724 sppp_print_string((char *)secret, secret_len);
4725 addlog(">\n");
4726 }
4727 if (name_len != sp->hisauth.name_len ||
4728 secret_len != sp->hisauth.secret_len ||
4729 memcmp(name, sp->hisauth.name, name_len) != 0 ||
4730 memcmp(secret, sp->hisauth.secret, secret_len) != 0) {
4731 /* action scn, tld */
4732 sp->pp_auth_failures++;
4733 mlen = sizeof(FAILMSG) - 1;
4734 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4735 sizeof mlen, (const char *)&mlen,
4736 sizeof(FAILMSG) - 1, (const u_char *)FAILMSG,
4737 0);
4738 pap.tld(sp);
4739 break;
4740 }
4741 /* action sca, perhaps tlu */
4742 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4743 sp->state[IDX_PAP] == STATE_OPENED) {
4744 mlen = sizeof(SUCCMSG) - 1;
4745 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4746 sizeof mlen, (const char *)&mlen,
4747 sizeof(SUCCMSG) - 1, (const u_char *)SUCCMSG,
4748 0);
4749 }
4750 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4751 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4752 pap.tlu(sp);
4753 }
4754 break;
4755
4756 /* ack and nak are his authproto */
4757 case PAP_ACK:
4758 callout_stop(&sp->pap_my_to_ch);
4759 if (debug) {
4760 log(LOG_DEBUG, "%s: pap success",
4761 ifp->if_xname);
4762 name = 1 + (u_char *)(h + 1);
4763 name_len = name[-1];
4764 if (len > 5 && name_len < len+4) {
4765 addlog(": ");
4766 sppp_print_string(name, name_len);
4767 }
4768 addlog("\n");
4769 }
4770 x = splnet();
4771 sp->pp_auth_failures = 0;
4772 sp->pp_flags &= ~PP_NEEDAUTH;
4773 if (sp->myauth.proto == PPP_PAP &&
4774 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4775 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4776 /*
4777 * We are authenticator for PAP but didn't
4778 * complete yet. Leave it to tlu to proceed
4779 * to network phase.
4780 */
4781 splx(x);
4782 break;
4783 }
4784 splx(x);
4785 sppp_phase_network(sp);
4786 break;
4787
4788 case PAP_NAK:
4789 callout_stop(&sp->pap_my_to_ch);
4790 sp->pp_auth_failures++;
4791 if (debug) {
4792 log(LOG_INFO, "%s: pap failure",
4793 ifp->if_xname);
4794 name = 1 + (u_char *)(h + 1);
4795 name_len = name[-1];
4796 if (len > 5 && name_len < len+4) {
4797 addlog(": ");
4798 sppp_print_string(name, name_len);
4799 }
4800 addlog("\n");
4801 } else
4802 log(LOG_INFO, "%s: pap failure\n",
4803 ifp->if_xname);
4804 /* await LCP shutdown by authenticator */
4805 break;
4806
4807 default:
4808 /* Unknown PAP packet type -- ignore. */
4809 if (debug) {
4810 log(LOG_DEBUG, "%s: pap corrupted input "
4811 "<0x%x id=0x%x len=%d",
4812 ifp->if_xname,
4813 h->type, h->ident, ntohs(h->len));
4814 if (len > 4)
4815 sppp_print_bytes((u_char *)(h + 1), len - 4);
4816 addlog(">\n");
4817 }
4818 break;
4819 }
4820
4821 SPPP_UNLOCK(sp);
4822 }
4823
4824 static void
4825 sppp_pap_init(struct sppp *sp)
4826 {
4827
4828 KASSERT(SPPP_WLOCKED(sp));
4829
4830 /* PAP doesn't have STATE_INITIAL at all. */
4831 sp->state[IDX_PAP] = STATE_CLOSED;
4832 sp->fail_counter[IDX_PAP] = 0;
4833 sp->pp_seq[IDX_PAP] = 0;
4834 sp->pp_rseq[IDX_PAP] = 0;
4835 callout_init(&sp->ch[IDX_PAP], CALLOUT_MPSAFE);
4836 callout_init(&sp->pap_my_to_ch, CALLOUT_MPSAFE);
4837 }
4838
4839 static void
4840 sppp_pap_open(struct sppp *sp)
4841 {
4842
4843 KASSERT(SPPP_WLOCKED(sp));
4844
4845 if (sp->hisauth.proto == PPP_PAP &&
4846 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4847 /* we are authenticator for PAP, start our timer */
4848 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4849 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4850 }
4851 if (sp->myauth.proto == PPP_PAP) {
4852 /* we are peer, send a request, and start a timer */
4853 pap.scr(sp);
4854 callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
4855 sppp_pap_my_TO, sp);
4856 }
4857 }
4858
4859 static void
4860 sppp_pap_close(struct sppp *sp)
4861 {
4862
4863 KASSERT(SPPP_WLOCKED(sp));
4864
4865 if (sp->state[IDX_PAP] != STATE_CLOSED)
4866 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4867 }
4868
4869 /*
4870 * That's the timeout routine if we are authenticator. Since the
4871 * authenticator is basically passive in PAP, we can't do much here.
4872 */
4873 static void
4874 sppp_pap_TO(void *cookie)
4875 {
4876 struct sppp *sp = (struct sppp *)cookie;
4877 STDDCL;
4878 int s;
4879
4880 s = splnet();
4881 SPPP_LOCK(sp, RW_WRITER);
4882
4883 if (debug)
4884 log(LOG_DEBUG, "%s: pap TO(%s) rst_counter = %d\n",
4885 ifp->if_xname,
4886 sppp_state_name(sp->state[IDX_PAP]),
4887 sp->rst_counter[IDX_PAP]);
4888
4889 if (--sp->rst_counter[IDX_PAP] < 0)
4890 /* TO- event */
4891 switch (sp->state[IDX_PAP]) {
4892 case STATE_REQ_SENT:
4893 pap.tld(sp);
4894 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4895 break;
4896 }
4897 else
4898 /* TO+ event, not very much we could do */
4899 switch (sp->state[IDX_PAP]) {
4900 case STATE_REQ_SENT:
4901 /* sppp_cp_change_state() will restart the timer */
4902 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4903 break;
4904 }
4905
4906 SPPP_UNLOCK(sp);
4907 splx(s);
4908 }
4909
4910 /*
4911 * That's the timeout handler if we are peer. Since the peer is active,
4912 * we need to retransmit our PAP request since it is apparently lost.
4913 * XXX We should impose a max counter.
4914 */
4915 static void
4916 sppp_pap_my_TO(void *cookie)
4917 {
4918 struct sppp *sp = (struct sppp *)cookie;
4919 STDDCL;
4920
4921 SPPP_LOCK(sp, RW_WRITER);
4922 if (debug)
4923 log(LOG_DEBUG, "%s: pap peer TO\n",
4924 ifp->if_xname);
4925
4926 pap.scr(sp);
4927
4928 SPPP_UNLOCK(sp);
4929 }
4930
4931 static void
4932 sppp_pap_tlu(struct sppp *sp)
4933 {
4934 STDDCL;
4935 int x;
4936
4937 KASSERT(SPPP_WLOCKED(sp));
4938
4939 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4940
4941 if (debug)
4942 log(LOG_DEBUG, "%s: %s tlu\n",
4943 ifp->if_xname, pap.name);
4944
4945 x = splnet();
4946 sp->pp_auth_failures = 0;
4947 /* indicate to LCP that we need to be closed down */
4948 sp->lcp.protos |= (1 << IDX_PAP);
4949
4950 if (sp->pp_flags & PP_NEEDAUTH) {
4951 /*
4952 * Remote is authenticator, but his auth proto didn't
4953 * complete yet. Defer the transition to network
4954 * phase.
4955 */
4956 splx(x);
4957 return;
4958 }
4959 splx(x);
4960 sppp_phase_network(sp);
4961 }
4962
4963 static void
4964 sppp_pap_tld(struct sppp *sp)
4965 {
4966 STDDCL;
4967
4968 KASSERT(SPPP_WLOCKED(sp));
4969
4970 if (debug)
4971 log(LOG_DEBUG, "%s: pap tld\n", ifp->if_xname);
4972 callout_stop(&sp->ch[IDX_PAP]);
4973 callout_stop(&sp->pap_my_to_ch);
4974 sp->lcp.protos &= ~(1 << IDX_PAP);
4975
4976 lcp.Close(sp);
4977 }
4978
4979 static void
4980 sppp_pap_scr(struct sppp *sp)
4981 {
4982 u_char idlen, pwdlen;
4983
4984 KASSERT(SPPP_WLOCKED(sp));
4985
4986 if (sp->myauth.secret == NULL || sp->myauth.name == NULL) {
4987 /* can't do anything useful */
4988 printf("%s: pap starting without my name and secret being set\n",
4989 sp->pp_if.if_xname);
4990 return;
4991 }
4992
4993 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4994 pwdlen = sp->myauth.secret_len;
4995 idlen = sp->myauth.name_len;
4996
4997 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4998 sizeof idlen, (const char *)&idlen,
4999 idlen, sp->myauth.name,
5000 sizeof pwdlen, (const char *)&pwdlen,
5001 pwdlen, sp->myauth.secret,
5002 0);
5003 }
5004
5005 /*
5006 * Random miscellaneous functions.
5007 */
5008
5009 /*
5010 * Send a PAP or CHAP proto packet.
5011 *
5012 * Varadic function, each of the elements for the ellipsis is of type
5013 * ``size_t mlen, const u_char *msg''. Processing will stop iff
5014 * mlen == 0.
5015 * NOTE: never declare variadic functions with types subject to type
5016 * promotion (i.e. u_char). This is asking for big trouble depending
5017 * on the architecture you are on...
5018 */
5019
5020 static void
5021 sppp_auth_send(const struct cp *cp, struct sppp *sp,
5022 unsigned int type, unsigned int id,
5023 ...)
5024 {
5025 STDDCL;
5026 struct lcp_header *lh;
5027 struct mbuf *m;
5028 u_char *p;
5029 int len;
5030 size_t pkthdrlen;
5031 unsigned int mlen;
5032 const char *msg;
5033 va_list ap;
5034
5035 KASSERT(SPPP_WLOCKED(sp));
5036
5037 MGETHDR(m, M_DONTWAIT, MT_DATA);
5038 if (! m)
5039 return;
5040 m_reset_rcvif(m);
5041
5042 if (sp->pp_flags & PP_NOFRAMING) {
5043 *mtod(m, uint16_t *) = htons(cp->proto);
5044 pkthdrlen = 2;
5045 lh = (struct lcp_header *)(mtod(m, uint8_t *)+2);
5046 } else {
5047 struct ppp_header *h;
5048 h = mtod(m, struct ppp_header *);
5049 h->address = PPP_ALLSTATIONS; /* broadcast address */
5050 h->control = PPP_UI; /* Unnumbered Info */
5051 h->protocol = htons(cp->proto);
5052 pkthdrlen = PPP_HEADER_LEN;
5053
5054 lh = (struct lcp_header *)(h + 1);
5055 }
5056
5057 lh->type = type;
5058 lh->ident = id;
5059 p = (u_char *)(lh + 1);
5060
5061 va_start(ap, id);
5062 len = 0;
5063
5064 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
5065 msg = va_arg(ap, const char *);
5066 len += mlen;
5067 if (len > MHLEN - pkthdrlen - LCP_HEADER_LEN) {
5068 va_end(ap);
5069 m_freem(m);
5070 return;
5071 }
5072
5073 memcpy(p, msg, mlen);
5074 p += mlen;
5075 }
5076 va_end(ap);
5077
5078 m->m_pkthdr.len = m->m_len = pkthdrlen + LCP_HEADER_LEN + len;
5079 lh->len = htons(LCP_HEADER_LEN + len);
5080
5081 if (debug) {
5082 log(LOG_DEBUG, "%s: %s output <%s id=0x%x len=%d",
5083 ifp->if_xname, cp->name,
5084 sppp_auth_type_name(cp->proto, lh->type),
5085 lh->ident, ntohs(lh->len));
5086 if (len)
5087 sppp_print_bytes((u_char *)(lh + 1), len);
5088 addlog(">\n");
5089 }
5090 if (IF_QFULL(&sp->pp_cpq)) {
5091 IF_DROP(&sp->pp_fastq);
5092 IF_DROP(&ifp->if_snd);
5093 m_freem(m);
5094 ++ifp->if_oerrors;
5095 return;
5096 }
5097
5098 ifp->if_obytes += m->m_pkthdr.len + sp->pp_framebytes;
5099 IF_ENQUEUE(&sp->pp_cpq, m);
5100
5101 if (! (ifp->if_flags & IFF_OACTIVE)) {
5102 SPPP_UNLOCK(sp);
5103 if_start_lock(ifp);
5104 SPPP_LOCK(sp, RW_WRITER);
5105 }
5106 }
5107
5108 /*
5109 * Send keepalive packets, every 10 seconds.
5110 */
5111 static void
5112 sppp_keepalive(void *dummy)
5113 {
5114 struct sppp *sp;
5115 int s;
5116 time_t now;
5117
5118 SPPPQ_LOCK();
5119
5120 s = splnet();
5121 now = time_uptime;
5122 for (sp=spppq; sp; sp=sp->pp_next) {
5123 struct ifnet *ifp = NULL;
5124
5125 SPPP_LOCK(sp, RW_WRITER);
5126 ifp = &sp->pp_if;
5127
5128 /* check idle timeout */
5129 if ((sp->pp_idle_timeout != 0) && (ifp->if_flags & IFF_RUNNING)
5130 && (sp->pp_phase == SPPP_PHASE_NETWORK)) {
5131 /* idle timeout is enabled for this interface */
5132 if ((now-sp->pp_last_activity) >= sp->pp_idle_timeout) {
5133 if (ifp->if_flags & IFF_DEBUG)
5134 printf("%s: no activity for %lu seconds\n",
5135 sp->pp_if.if_xname,
5136 (unsigned long)(now-sp->pp_last_activity));
5137 lcp.Close(sp);
5138 SPPP_UNLOCK(sp);
5139 continue;
5140 }
5141 }
5142
5143 /* Keepalive mode disabled or channel down? */
5144 if (! (sp->pp_flags & PP_KEEPALIVE) ||
5145 ! (ifp->if_flags & IFF_RUNNING)) {
5146 SPPP_UNLOCK(sp);
5147 continue;
5148 }
5149
5150
5151 /* No keepalive in PPP mode if LCP not opened yet. */
5152 if (! (sp->pp_flags & PP_CISCO) &&
5153 sp->pp_phase < SPPP_PHASE_AUTHENTICATE) {
5154 SPPP_UNLOCK(sp);
5155 continue;
5156 }
5157
5158 /* No echo reply, but maybe user data passed through? */
5159 if ((now - sp->pp_last_receive) < sp->pp_max_noreceive) {
5160 sp->pp_alivecnt = 0;
5161 SPPP_UNLOCK(sp);
5162 continue;
5163 }
5164
5165 if (sp->pp_alivecnt >= sp->pp_maxalive) {
5166 /* No keepalive packets got. Stop the interface. */
5167 SPPP_UNLOCK(sp);
5168 if_down (ifp);
5169 SPPP_LOCK(sp, RW_WRITER);
5170
5171 IF_PURGE(&sp->pp_cpq);
5172
5173 if (! (sp->pp_flags & PP_CISCO)) {
5174 printf("%s: LCP keepalive timed out, going to restart the connection\n",
5175 ifp->if_xname);
5176 sp->pp_alivecnt = 0;
5177
5178 /* we are down, close all open protocols */
5179 lcp.Close(sp);
5180
5181 /* And now prepare LCP to reestablish the link, if configured to do so. */
5182 sppp_cp_change_state(&lcp, sp, STATE_STOPPED);
5183
5184 /* Close connection immediately, completition of this
5185 * will summon the magic needed to reestablish it. */
5186 sppp_notify_tlf_wlocked(sp);
5187
5188 SPPP_UNLOCK(sp);
5189 continue;
5190 }
5191 }
5192 if (sp->pp_alivecnt < sp->pp_maxalive)
5193 ++sp->pp_alivecnt;
5194 if (sp->pp_flags & PP_CISCO)
5195 sppp_cisco_send(sp, CISCO_KEEPALIVE_REQ,
5196 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
5197 else if (sp->pp_phase >= SPPP_PHASE_AUTHENTICATE) {
5198 int32_t nmagic = htonl(sp->lcp.magic);
5199 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
5200 sppp_cp_send(sp, PPP_LCP, ECHO_REQ,
5201 sp->lcp.echoid, 4, &nmagic);
5202 }
5203
5204 SPPP_UNLOCK(sp);
5205 }
5206 splx(s);
5207 callout_reset(&keepalive_ch, hz * LCP_KEEPALIVE_INTERVAL, sppp_keepalive, NULL);
5208
5209 SPPPQ_UNLOCK();
5210 }
5211
5212 #ifdef INET
5213 /*
5214 * Get both IP addresses.
5215 */
5216 static void
5217 sppp_get_ip_addrs(struct sppp *sp, uint32_t *src, uint32_t *dst, uint32_t *srcmask)
5218 {
5219 struct ifnet *ifp = &sp->pp_if;
5220 struct ifaddr *ifa;
5221 struct sockaddr_in *si, *sm;
5222 uint32_t ssrc, ddst;
5223 int s;
5224 struct psref psref;
5225
5226 sm = NULL;
5227 ssrc = ddst = 0;
5228 /*
5229 * Pick the first AF_INET address from the list,
5230 * aliases don't make any sense on a p2p link anyway.
5231 */
5232 si = 0;
5233 s = pserialize_read_enter();
5234 IFADDR_READER_FOREACH(ifa, ifp) {
5235 if (ifa->ifa_addr->sa_family == AF_INET) {
5236 si = (struct sockaddr_in *)ifa->ifa_addr;
5237 sm = (struct sockaddr_in *)ifa->ifa_netmask;
5238 if (si) {
5239 ifa_acquire(ifa, &psref);
5240 break;
5241 }
5242 }
5243 }
5244 pserialize_read_exit(s);
5245 if (ifa) {
5246 if (si && si->sin_addr.s_addr) {
5247 ssrc = si->sin_addr.s_addr;
5248 if (srcmask)
5249 *srcmask = ntohl(sm->sin_addr.s_addr);
5250 }
5251
5252 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
5253 if (si && si->sin_addr.s_addr)
5254 ddst = si->sin_addr.s_addr;
5255 ifa_release(ifa, &psref);
5256 }
5257
5258 if (dst) *dst = ntohl(ddst);
5259 if (src) *src = ntohl(ssrc);
5260 }
5261
5262 /*
5263 * Set IP addresses. Must be called at splnet.
5264 * If an address is 0, leave it the way it is.
5265 */
5266 static void
5267 sppp_set_ip_addrs_work(struct work *wk, struct sppp *sp)
5268 {
5269 STDDCL;
5270 struct ifaddr *ifa;
5271 struct sockaddr_in *si, *dest;
5272 uint32_t myaddr = 0, hisaddr = 0;
5273 int s;
5274
5275 IFNET_LOCK(ifp);
5276
5277 /*
5278 * Pick the first AF_INET address from the list,
5279 * aliases don't make any sense on a p2p link anyway.
5280 */
5281 si = dest = NULL;
5282 s = pserialize_read_enter();
5283 IFADDR_READER_FOREACH(ifa, ifp) {
5284 if (ifa->ifa_addr->sa_family == AF_INET) {
5285 si = (struct sockaddr_in *)ifa->ifa_addr;
5286 dest = (struct sockaddr_in *)ifa->ifa_dstaddr;
5287 break;
5288 }
5289 }
5290 pserialize_read_exit(s);
5291
5292 if ((sp->ipcp.flags & IPCP_MYADDR_DYN) && (sp->ipcp.flags & IPCP_MYADDR_SEEN))
5293 myaddr = sp->ipcp.req_myaddr;
5294 else if (si != NULL)
5295 myaddr = ntohl(si->sin_addr.s_addr);
5296
5297 if ((sp->ipcp.flags & IPCP_HISADDR_DYN) && (sp->ipcp.flags & IPCP_HISADDR_SEEN))
5298 hisaddr = sp->ipcp.req_hisaddr;
5299 else if (dest != NULL)
5300 hisaddr = ntohl(dest->sin_addr.s_addr);
5301
5302 if (si != NULL && dest != NULL) {
5303 int error;
5304 struct sockaddr_in new_sin = *si;
5305 struct sockaddr_in new_dst = *dest;
5306
5307 if (myaddr != 0)
5308 new_sin.sin_addr.s_addr = htonl(myaddr);
5309 if (hisaddr != 0) {
5310 new_dst.sin_addr.s_addr = htonl(hisaddr);
5311 if (new_dst.sin_addr.s_addr != dest->sin_addr.s_addr)
5312 sp->ipcp.saved_hisaddr = dest->sin_addr.s_addr;
5313 }
5314
5315 in_addrhash_remove(ifatoia(ifa));
5316
5317 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, &new_dst, 0);
5318
5319 in_addrhash_insert(ifatoia(ifa));
5320
5321 if (debug && error)
5322 {
5323 log(LOG_DEBUG, "%s: %s: in_ifinit failed, error=%d\n",
5324 ifp->if_xname, __func__, error);
5325 }
5326 if (!error) {
5327 pfil_run_addrhooks(if_pfil, SIOCAIFADDR, ifa);
5328 }
5329 }
5330
5331 if (ifp->if_mtu > sp->lcp.their_mru) {
5332 sp->pp_saved_mtu = ifp->if_mtu;
5333 ifp->if_mtu = sp->lcp.their_mru;
5334 if (debug)
5335 log(LOG_DEBUG,
5336 "%s: setting MTU to %" PRIu64 " bytes\n",
5337 ifp->if_xname, ifp->if_mtu);
5338 }
5339
5340 IFNET_UNLOCK(ifp);
5341
5342 sppp_notify_con(sp);
5343 }
5344
5345 static void
5346 sppp_set_ip_addrs(struct sppp *sp)
5347 {
5348 struct ifnet *ifp = &sp->pp_if;
5349
5350 if (!pcq_put(sp->ipcp.update_addrs_q, (void *)IPCP_SET_ADDRS)) {
5351 log(LOG_WARNING, "%s: cannot enqueued, ignore sppp_clear_ip_addrs\n",
5352 ifp->if_xname);
5353 return;
5354 }
5355
5356 if (atomic_swap_uint(&sp->ipcp.update_addrs_enqueued, 1) == 1)
5357 return;
5358
5359 workqueue_enqueue(sp->ipcp.update_addrs_wq, &sp->ipcp.update_addrs_wk, NULL);
5360 }
5361
5362 /*
5363 * Clear IP addresses. Must be called at splnet.
5364 */
5365 static void
5366 sppp_clear_ip_addrs_work(struct work *wk, struct sppp *sp)
5367 {
5368 STDDCL;
5369 struct ifaddr *ifa;
5370 struct sockaddr_in *si, *dest;
5371 int s;
5372
5373 IFNET_LOCK(ifp);
5374
5375 /*
5376 * Pick the first AF_INET address from the list,
5377 * aliases don't make any sense on a p2p link anyway.
5378 */
5379 si = dest = NULL;
5380 s = pserialize_read_enter();
5381 IFADDR_READER_FOREACH(ifa, ifp) {
5382 if (ifa->ifa_addr->sa_family == AF_INET) {
5383 si = (struct sockaddr_in *)ifa->ifa_addr;
5384 dest = (struct sockaddr_in *)ifa->ifa_dstaddr;
5385 break;
5386 }
5387 }
5388 pserialize_read_exit(s);
5389
5390 if (si != NULL) {
5391 struct sockaddr_in new_sin = *si;
5392 struct sockaddr_in new_dst = *dest;
5393 int error;
5394
5395 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
5396 new_sin.sin_addr.s_addr = 0;
5397 if (sp->ipcp.flags & IPCP_HISADDR_DYN)
5398 new_dst.sin_addr.s_addr = sp->ipcp.saved_hisaddr;
5399
5400 in_addrhash_remove(ifatoia(ifa));
5401
5402 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, &new_dst, 0);
5403
5404 in_addrhash_insert(ifatoia(ifa));
5405
5406 if (debug && error)
5407 {
5408 log(LOG_DEBUG, "%s: %s: in_ifinit failed, error=%d\n",
5409 ifp->if_xname, __func__, error);
5410 }
5411 if (!error) {
5412 pfil_run_addrhooks(if_pfil, SIOCAIFADDR, ifa);
5413 }
5414 }
5415
5416 if (sp->pp_saved_mtu > 0) {
5417 ifp->if_mtu = sp->pp_saved_mtu;
5418 sp->pp_saved_mtu = 0;
5419 if (debug)
5420 log(LOG_DEBUG,
5421 "%s: resetting MTU to %" PRIu64 " bytes\n",
5422 ifp->if_xname, ifp->if_mtu);
5423 }
5424
5425 IFNET_UNLOCK(ifp);
5426 }
5427
5428 static void
5429 sppp_clear_ip_addrs(struct sppp *sp)
5430 {
5431 struct ifnet *ifp = &sp->pp_if;
5432
5433 if (!pcq_put(sp->ipcp.update_addrs_q, (void *)IPCP_CLEAR_ADDRS)) {
5434 log(LOG_WARNING, "%s: cannot enqueued, ignore sppp_clear_ip_addrs\n",
5435 ifp->if_xname);
5436 return;
5437 }
5438
5439 if (atomic_swap_uint(&sp->ipcp.update_addrs_enqueued, 1) == 1)
5440 return;
5441
5442 workqueue_enqueue(sp->ipcp.update_addrs_wq, &sp->ipcp.update_addrs_wk, NULL);
5443 }
5444
5445 static void
5446 sppp_update_ip_addrs_work(struct work *wk, void *arg)
5447 {
5448 struct sppp *sp = arg;
5449 void *work;
5450
5451 atomic_swap_uint(&sp->ipcp.update_addrs_enqueued, 0);
5452
5453 while ((work = pcq_get(sp->ipcp.update_addrs_q)) != NULL) {
5454 int update = (intptr_t)work;
5455
5456 if (update == IPCP_SET_ADDRS)
5457 sppp_set_ip_addrs_work(wk, sp);
5458 else if (update == IPCP_CLEAR_ADDRS)
5459 sppp_clear_ip_addrs_work(wk, sp);
5460 }
5461 }
5462 #endif
5463
5464 #ifdef INET6
5465 /*
5466 * Get both IPv6 addresses.
5467 */
5468 static void
5469 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
5470 struct in6_addr *srcmask)
5471 {
5472 struct ifnet *ifp = &sp->pp_if;
5473 struct ifaddr *ifa;
5474 struct sockaddr_in6 *si, *sm;
5475 struct in6_addr ssrc, ddst;
5476 int s;
5477 struct psref psref;
5478
5479 sm = NULL;
5480 memset(&ssrc, 0, sizeof(ssrc));
5481 memset(&ddst, 0, sizeof(ddst));
5482 /*
5483 * Pick the first link-local AF_INET6 address from the list,
5484 * aliases don't make any sense on a p2p link anyway.
5485 */
5486 si = 0;
5487 s = pserialize_read_enter();
5488 IFADDR_READER_FOREACH(ifa, ifp) {
5489 if (ifa->ifa_addr->sa_family == AF_INET6) {
5490 si = (struct sockaddr_in6 *)ifa->ifa_addr;
5491 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
5492 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr)) {
5493 ifa_acquire(ifa, &psref);
5494 break;
5495 }
5496 }
5497 }
5498 pserialize_read_exit(s);
5499
5500 if (ifa) {
5501 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
5502 memcpy(&ssrc, &si->sin6_addr, sizeof(ssrc));
5503 if (srcmask) {
5504 memcpy(srcmask, &sm->sin6_addr,
5505 sizeof(*srcmask));
5506 }
5507 }
5508
5509 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
5510 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
5511 memcpy(&ddst, &si->sin6_addr, sizeof(ddst));
5512 ifa_release(ifa, &psref);
5513 }
5514
5515 if (dst)
5516 memcpy(dst, &ddst, sizeof(*dst));
5517 if (src)
5518 memcpy(src, &ssrc, sizeof(*src));
5519 }
5520
5521 #ifdef IPV6CP_MYIFID_DYN
5522 /*
5523 * Generate random ifid.
5524 */
5525 static void
5526 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
5527 {
5528 /* TBD */
5529 }
5530
5531 /*
5532 * Set my IPv6 address. Must be called at splnet.
5533 */
5534 static void
5535 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
5536 {
5537 STDDCL;
5538 struct ifaddr *ifa;
5539 struct sockaddr_in6 *sin6;
5540 int s;
5541 struct psref psref;
5542
5543 IFNET_LOCK(ifp);
5544
5545 /*
5546 * Pick the first link-local AF_INET6 address from the list,
5547 * aliases don't make any sense on a p2p link anyway.
5548 */
5549
5550 sin6 = NULL;
5551 s = pserialize_read_enter();
5552 IFADDR_READER_FOREACH(ifa, ifp)
5553 {
5554 if (ifa->ifa_addr->sa_family == AF_INET6)
5555 {
5556 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
5557 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5558 ifa_acquire(ifa, &psref);
5559 break;
5560 }
5561 }
5562 }
5563 pserialize_read_exit(s);
5564
5565 if (ifa && sin6)
5566 {
5567 int error;
5568 struct sockaddr_in6 new_sin6 = *sin6;
5569
5570 memcpy(&new_sin6.sin6_addr, src, sizeof(new_sin6.sin6_addr));
5571 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5572 if (debug && error)
5573 {
5574 log(LOG_DEBUG, "%s: %s: in6_ifinit failed, error=%d\n",
5575 ifp->if_xname, __func__, error);
5576 }
5577 if (!error) {
5578 pfil_run_addrhooks(if_pfil, SIOCAIFADDR_IN6, ifa);
5579 }
5580 ifa_release(ifa, &psref);
5581 }
5582
5583 IFNET_UNLOCK(ifp);
5584 }
5585 #endif
5586
5587 /*
5588 * Suggest a candidate address to be used by peer.
5589 */
5590 static void
5591 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5592 {
5593 struct in6_addr myaddr;
5594 struct timeval tv;
5595
5596 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5597
5598 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5599 microtime(&tv);
5600 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5601 myaddr.s6_addr[14] ^= 0xff;
5602 myaddr.s6_addr[15] ^= 0xff;
5603 } else {
5604 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5605 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5606 }
5607 if (suggest)
5608 memcpy(suggest, &myaddr, sizeof(myaddr));
5609 }
5610 #endif /*INET6*/
5611
5612 /*
5613 * Process ioctl requests specific to the PPP interface.
5614 * Permissions have already been checked.
5615 */
5616 static int
5617 sppp_params(struct sppp *sp, u_long cmd, void *data)
5618 {
5619 switch (cmd) {
5620 case SPPPGETAUTHCFG:
5621 {
5622 struct spppauthcfg *cfg = (struct spppauthcfg *)data;
5623 int error;
5624 size_t len;
5625
5626 SPPP_LOCK(sp, RW_READER);
5627
5628 cfg->myauthflags = sp->myauth.flags;
5629 cfg->hisauthflags = sp->hisauth.flags;
5630 strlcpy(cfg->ifname, sp->pp_if.if_xname, sizeof(cfg->ifname));
5631 cfg->hisauth = 0;
5632 if (sp->hisauth.proto)
5633 cfg->hisauth = (sp->hisauth.proto == PPP_PAP) ? SPPP_AUTHPROTO_PAP : SPPP_AUTHPROTO_CHAP;
5634 cfg->myauth = 0;
5635 if (sp->myauth.proto)
5636 cfg->myauth = (sp->myauth.proto == PPP_PAP) ? SPPP_AUTHPROTO_PAP : SPPP_AUTHPROTO_CHAP;
5637 if (cfg->myname_length == 0) {
5638 if (sp->myauth.name != NULL)
5639 cfg->myname_length = sp->myauth.name_len + 1;
5640 } else {
5641 if (sp->myauth.name == NULL) {
5642 cfg->myname_length = 0;
5643 } else {
5644 len = sp->myauth.name_len + 1;
5645
5646
5647 if (cfg->myname_length < len) {
5648 SPPP_UNLOCK(sp);
5649 return (ENAMETOOLONG);
5650 }
5651 error = copyout(sp->myauth.name, cfg->myname, len);
5652 if (error) {
5653 SPPP_UNLOCK(sp);
5654 return error;
5655 }
5656 }
5657 }
5658 if (cfg->hisname_length == 0) {
5659 if (sp->hisauth.name != NULL)
5660 cfg->hisname_length = sp->hisauth.name_len + 1;
5661 } else {
5662 if (sp->hisauth.name == NULL) {
5663 cfg->hisname_length = 0;
5664 } else {
5665 len = sp->hisauth.name_len + 1;
5666
5667 if (cfg->hisname_length < len) {
5668 SPPP_UNLOCK(sp);
5669 return (ENAMETOOLONG);
5670 }
5671 error = copyout(sp->hisauth.name, cfg->hisname, len);
5672 if (error) {
5673 SPPP_UNLOCK(sp);
5674 return error;
5675 }
5676 }
5677 }
5678 SPPP_UNLOCK(sp);
5679 }
5680 break;
5681 case SPPPSETAUTHCFG:
5682 {
5683 struct spppauthcfg *cfg = (struct spppauthcfg *)data;
5684 int error;
5685
5686 SPPP_LOCK(sp, RW_WRITER);
5687
5688 if (sp->myauth.name) {
5689 free(sp->myauth.name, M_DEVBUF);
5690 sp->myauth.name = NULL;
5691 }
5692 if (sp->myauth.secret) {
5693 free(sp->myauth.secret, M_DEVBUF);
5694 sp->myauth.secret = NULL;
5695 }
5696 if (sp->hisauth.name) {
5697 free(sp->hisauth.name, M_DEVBUF);
5698 sp->hisauth.name = NULL;
5699 }
5700 if (sp->hisauth.secret) {
5701 free(sp->hisauth.secret, M_DEVBUF);
5702 sp->hisauth.secret = NULL;
5703 }
5704
5705 if (cfg->hisname != NULL && cfg->hisname_length > 0) {
5706 if (cfg->hisname_length >= MCLBYTES) {
5707 SPPP_UNLOCK(sp);
5708 return (ENAMETOOLONG);
5709 }
5710 sp->hisauth.name = malloc(cfg->hisname_length, M_DEVBUF, M_WAITOK);
5711 error = copyin(cfg->hisname, sp->hisauth.name, cfg->hisname_length);
5712 if (error) {
5713 free(sp->hisauth.name, M_DEVBUF);
5714 sp->hisauth.name = NULL;
5715 SPPP_UNLOCK(sp);
5716 return error;
5717 }
5718 sp->hisauth.name_len = cfg->hisname_length - 1;
5719 sp->hisauth.name[sp->hisauth.name_len] = 0;
5720 }
5721 if (cfg->hissecret != NULL && cfg->hissecret_length > 0) {
5722 if (cfg->hissecret_length >= MCLBYTES) {
5723 SPPP_UNLOCK(sp);
5724 return (ENAMETOOLONG);
5725 }
5726 sp->hisauth.secret = malloc(cfg->hissecret_length,
5727 M_DEVBUF, M_WAITOK);
5728 error = copyin(cfg->hissecret, sp->hisauth.secret,
5729 cfg->hissecret_length);
5730 if (error) {
5731 free(sp->hisauth.secret, M_DEVBUF);
5732 sp->hisauth.secret = NULL;
5733 SPPP_UNLOCK(sp);
5734 return error;
5735 }
5736 sp->hisauth.secret_len = cfg->hissecret_length - 1;
5737 sp->hisauth.secret[sp->hisauth.secret_len] = 0;
5738 }
5739 if (cfg->myname != NULL && cfg->myname_length > 0) {
5740 if (cfg->myname_length >= MCLBYTES) {
5741 SPPP_UNLOCK(sp);
5742 return (ENAMETOOLONG);
5743 }
5744 sp->myauth.name = malloc(cfg->myname_length, M_DEVBUF, M_WAITOK);
5745 error = copyin(cfg->myname, sp->myauth.name, cfg->myname_length);
5746 if (error) {
5747 free(sp->myauth.name, M_DEVBUF);
5748 sp->myauth.name = NULL;
5749 SPPP_UNLOCK(sp);
5750 return error;
5751 }
5752 sp->myauth.name_len = cfg->myname_length - 1;
5753 sp->myauth.name[sp->myauth.name_len] = 0;
5754 }
5755 if (cfg->mysecret != NULL && cfg->mysecret_length > 0) {
5756 if (cfg->mysecret_length >= MCLBYTES) {
5757 SPPP_UNLOCK(sp);
5758 return (ENAMETOOLONG);
5759 }
5760 sp->myauth.secret = malloc(cfg->mysecret_length,
5761 M_DEVBUF, M_WAITOK);
5762 error = copyin(cfg->mysecret, sp->myauth.secret,
5763 cfg->mysecret_length);
5764 if (error) {
5765 free(sp->myauth.secret, M_DEVBUF);
5766 sp->myauth.secret = NULL;
5767 SPPP_UNLOCK(sp);
5768 return error;
5769 }
5770 sp->myauth.secret_len = cfg->mysecret_length - 1;
5771 sp->myauth.secret[sp->myauth.secret_len] = 0;
5772 }
5773 sp->myauth.flags = cfg->myauthflags;
5774 if (cfg->myauth)
5775 sp->myauth.proto = (cfg->myauth == SPPP_AUTHPROTO_PAP) ? PPP_PAP : PPP_CHAP;
5776 sp->hisauth.flags = cfg->hisauthflags;
5777 if (cfg->hisauth)
5778 sp->hisauth.proto = (cfg->hisauth == SPPP_AUTHPROTO_PAP) ? PPP_PAP : PPP_CHAP;
5779 sp->pp_auth_failures = 0;
5780 if (sp->hisauth.proto != 0)
5781 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
5782 else
5783 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
5784
5785 SPPP_UNLOCK(sp);
5786 }
5787 break;
5788 case SPPPGETLCPCFG:
5789 {
5790 struct sppplcpcfg *lcpp = (struct sppplcpcfg *)data;
5791
5792 SPPP_LOCK(sp, RW_READER);
5793 lcpp->lcp_timeout = sp->lcp.timeout;
5794 SPPP_UNLOCK(sp);
5795 }
5796 break;
5797 case SPPPSETLCPCFG:
5798 {
5799 struct sppplcpcfg *lcpp = (struct sppplcpcfg *)data;
5800
5801 SPPP_LOCK(sp, RW_WRITER);
5802 sp->lcp.timeout = lcpp->lcp_timeout;
5803 SPPP_UNLOCK(sp);
5804 }
5805 break;
5806 case SPPPGETSTATUS:
5807 {
5808 struct spppstatus *status = (struct spppstatus *)data;
5809
5810 SPPP_LOCK(sp, RW_READER);
5811 status->phase = sp->pp_phase;
5812 SPPP_UNLOCK(sp);
5813 }
5814 break;
5815 case SPPPGETSTATUSNCP:
5816 {
5817 struct spppstatusncp *status = (struct spppstatusncp *)data;
5818
5819 SPPP_LOCK(sp, RW_READER);
5820 status->phase = sp->pp_phase;
5821 status->ncpup = sppp_ncp_check(sp);
5822 SPPP_UNLOCK(sp);
5823 }
5824 break;
5825 case SPPPGETIDLETO:
5826 {
5827 struct spppidletimeout *to = (struct spppidletimeout *)data;
5828
5829 SPPP_LOCK(sp, RW_READER);
5830 to->idle_seconds = sp->pp_idle_timeout;
5831 SPPP_UNLOCK(sp);
5832 }
5833 break;
5834 case SPPPSETIDLETO:
5835 {
5836 struct spppidletimeout *to = (struct spppidletimeout *)data;
5837
5838 SPPP_LOCK(sp, RW_WRITER);
5839 sp->pp_idle_timeout = to->idle_seconds;
5840 SPPP_UNLOCK(sp);
5841 }
5842 break;
5843 case SPPPSETAUTHFAILURE:
5844 {
5845 struct spppauthfailuresettings *afsettings =
5846 (struct spppauthfailuresettings *)data;
5847
5848 SPPP_LOCK(sp, RW_WRITER);
5849 sp->pp_max_auth_fail = afsettings->max_failures;
5850 sp->pp_auth_failures = 0;
5851 SPPP_UNLOCK(sp);
5852 }
5853 break;
5854 case SPPPGETAUTHFAILURES:
5855 {
5856 struct spppauthfailurestats *stats = (struct spppauthfailurestats *)data;
5857
5858 SPPP_LOCK(sp, RW_READER);
5859 stats->auth_failures = sp->pp_auth_failures;
5860 stats->max_failures = sp->pp_max_auth_fail;
5861 SPPP_UNLOCK(sp);
5862 }
5863 break;
5864 case SPPPSETDNSOPTS:
5865 {
5866 struct spppdnssettings *req = (struct spppdnssettings *)data;
5867
5868 SPPP_LOCK(sp, RW_WRITER);
5869 sp->query_dns = req->query_dns & 3;
5870 SPPP_UNLOCK(sp);
5871 }
5872 break;
5873 case SPPPGETDNSOPTS:
5874 {
5875 struct spppdnssettings *req = (struct spppdnssettings *)data;
5876
5877 SPPP_LOCK(sp, RW_READER);
5878 req->query_dns = sp->query_dns;
5879 SPPP_UNLOCK(sp);
5880 }
5881 break;
5882 case SPPPGETDNSADDRS:
5883 {
5884 struct spppdnsaddrs *addrs = (struct spppdnsaddrs *)data;
5885
5886 SPPP_LOCK(sp, RW_READER);
5887 memcpy(&addrs->dns, &sp->dns_addrs, sizeof addrs->dns);
5888 SPPP_UNLOCK(sp);
5889 }
5890 break;
5891 case SPPPGETKEEPALIVE:
5892 {
5893 struct spppkeepalivesettings *settings =
5894 (struct spppkeepalivesettings*)data;
5895
5896 SPPP_LOCK(sp, RW_READER);
5897 settings->maxalive = sp->pp_maxalive;
5898 settings->max_noreceive = sp->pp_max_noreceive;
5899 SPPP_UNLOCK(sp);
5900 }
5901 break;
5902 case SPPPSETKEEPALIVE:
5903 {
5904 struct spppkeepalivesettings *settings =
5905 (struct spppkeepalivesettings*)data;
5906
5907 SPPP_LOCK(sp, RW_WRITER);
5908 sp->pp_maxalive = settings->maxalive;
5909 sp->pp_max_noreceive = settings->max_noreceive;
5910 SPPP_UNLOCK(sp);
5911 }
5912 break;
5913 #if defined(COMPAT_50) || defined(MODULAR)
5914 case __SPPPGETIDLETO50:
5915 {
5916 struct spppidletimeout50 *to = (struct spppidletimeout50 *)data;
5917
5918 SPPP_LOCK(sp, RW_READER);
5919 to->idle_seconds = (uint32_t)sp->pp_idle_timeout;
5920 SPPP_UNLOCK(sp);
5921 }
5922 break;
5923 case __SPPPSETIDLETO50:
5924 {
5925 struct spppidletimeout50 *to = (struct spppidletimeout50 *)data;
5926
5927 SPPP_LOCK(sp, RW_WRITER);
5928 sp->pp_idle_timeout = (time_t)to->idle_seconds;
5929 SPPP_UNLOCK(sp);
5930 }
5931 break;
5932 case __SPPPGETKEEPALIVE50:
5933 {
5934 struct spppkeepalivesettings50 *settings =
5935 (struct spppkeepalivesettings50*)data;
5936
5937 SPPP_LOCK(sp, RW_READER);
5938 settings->maxalive = sp->pp_maxalive;
5939 settings->max_noreceive = (uint32_t)sp->pp_max_noreceive;
5940 SPPP_UNLOCK(sp);
5941 }
5942 break;
5943 case __SPPPSETKEEPALIVE50:
5944 {
5945 struct spppkeepalivesettings50 *settings =
5946 (struct spppkeepalivesettings50*)data;
5947
5948 SPPP_LOCK(sp, RW_WRITER);
5949 sp->pp_maxalive = settings->maxalive;
5950 sp->pp_max_noreceive = (time_t)settings->max_noreceive;
5951 SPPP_UNLOCK(sp);
5952 }
5953 break;
5954 #endif /* COMPAT_50 || MODULAR */
5955 default:
5956 return (EINVAL);
5957 }
5958
5959 return (0);
5960 }
5961
5962 static void
5963 sppp_phase_network(struct sppp *sp)
5964 {
5965 int i;
5966 uint32_t mask;
5967
5968 KASSERT(SPPP_WLOCKED(sp));
5969
5970 sppp_change_phase(sp, SPPP_PHASE_NETWORK);
5971
5972 /* Notify NCPs now. */
5973 for (i = 0; i < IDX_COUNT; i++)
5974 if ((cps[i])->flags & CP_NCP)
5975 (cps[i])->Open(sp);
5976
5977 /* Send Up events to all NCPs. */
5978 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5979 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP)) {
5980 (cps[i])->Up(sp);
5981 }
5982
5983 /* if no NCP is starting, all this was in vain, close down */
5984 sppp_lcp_check_and_close(sp);
5985 }
5986
5987
5988 static const char *
5989 sppp_cp_type_name(u_char type)
5990 {
5991 static char buf[12];
5992 switch (type) {
5993 case CONF_REQ: return "conf-req";
5994 case CONF_ACK: return "conf-ack";
5995 case CONF_NAK: return "conf-nak";
5996 case CONF_REJ: return "conf-rej";
5997 case TERM_REQ: return "term-req";
5998 case TERM_ACK: return "term-ack";
5999 case CODE_REJ: return "code-rej";
6000 case PROTO_REJ: return "proto-rej";
6001 case ECHO_REQ: return "echo-req";
6002 case ECHO_REPLY: return "echo-reply";
6003 case DISC_REQ: return "discard-req";
6004 }
6005 snprintf(buf, sizeof(buf), "0x%x", type);
6006 return buf;
6007 }
6008
6009 static const char *
6010 sppp_auth_type_name(u_short proto, u_char type)
6011 {
6012 static char buf[32];
6013 const char *name;
6014
6015 switch (proto) {
6016 case PPP_CHAP:
6017 switch (type) {
6018 case CHAP_CHALLENGE: return "challenge";
6019 case CHAP_RESPONSE: return "response";
6020 case CHAP_SUCCESS: return "success";
6021 case CHAP_FAILURE: return "failure";
6022 default: name = "chap"; break;
6023 }
6024 break;
6025
6026 case PPP_PAP:
6027 switch (type) {
6028 case PAP_REQ: return "req";
6029 case PAP_ACK: return "ack";
6030 case PAP_NAK: return "nak";
6031 default: name = "pap"; break;
6032 }
6033 break;
6034
6035 default:
6036 name = "bad";
6037 break;
6038 }
6039
6040 snprintf(buf, sizeof(buf), "%s(%#x) %#x", name, proto, type);
6041 return buf;
6042 }
6043
6044 static const char *
6045 sppp_lcp_opt_name(u_char opt)
6046 {
6047 static char buf[12];
6048 switch (opt) {
6049 case LCP_OPT_MRU: return "mru";
6050 case LCP_OPT_ASYNC_MAP: return "async-map";
6051 case LCP_OPT_AUTH_PROTO: return "auth-proto";
6052 case LCP_OPT_QUAL_PROTO: return "qual-proto";
6053 case LCP_OPT_MAGIC: return "magic";
6054 case LCP_OPT_PROTO_COMP: return "proto-comp";
6055 case LCP_OPT_ADDR_COMP: return "addr-comp";
6056 }
6057 snprintf(buf, sizeof(buf), "0x%x", opt);
6058 return buf;
6059 }
6060
6061 static const char *
6062 sppp_ipcp_opt_name(u_char opt)
6063 {
6064 static char buf[12];
6065 switch (opt) {
6066 case IPCP_OPT_ADDRESSES: return "addresses";
6067 case IPCP_OPT_COMPRESSION: return "compression";
6068 case IPCP_OPT_ADDRESS: return "address";
6069 }
6070 snprintf(buf, sizeof(buf), "0x%x", opt);
6071 return buf;
6072 }
6073
6074 #ifdef INET6
6075 static const char *
6076 sppp_ipv6cp_opt_name(u_char opt)
6077 {
6078 static char buf[12];
6079 switch (opt) {
6080 case IPV6CP_OPT_IFID: return "ifid";
6081 case IPV6CP_OPT_COMPRESSION: return "compression";
6082 }
6083 snprintf(buf, sizeof(buf), "0x%x", opt);
6084 return buf;
6085 }
6086 #endif
6087
6088 static const char *
6089 sppp_state_name(int state)
6090 {
6091 switch (state) {
6092 case STATE_INITIAL: return "initial";
6093 case STATE_STARTING: return "starting";
6094 case STATE_CLOSED: return "closed";
6095 case STATE_STOPPED: return "stopped";
6096 case STATE_CLOSING: return "closing";
6097 case STATE_STOPPING: return "stopping";
6098 case STATE_REQ_SENT: return "req-sent";
6099 case STATE_ACK_RCVD: return "ack-rcvd";
6100 case STATE_ACK_SENT: return "ack-sent";
6101 case STATE_OPENED: return "opened";
6102 }
6103 return "illegal";
6104 }
6105
6106 static const char *
6107 sppp_phase_name(int phase)
6108 {
6109 switch (phase) {
6110 case SPPP_PHASE_DEAD: return "dead";
6111 case SPPP_PHASE_ESTABLISH: return "establish";
6112 case SPPP_PHASE_TERMINATE: return "terminate";
6113 case SPPP_PHASE_AUTHENTICATE: return "authenticate";
6114 case SPPP_PHASE_NETWORK: return "network";
6115 }
6116 return "illegal";
6117 }
6118
6119 static const char *
6120 sppp_proto_name(u_short proto)
6121 {
6122 static char buf[12];
6123 switch (proto) {
6124 case PPP_LCP: return "lcp";
6125 case PPP_IPCP: return "ipcp";
6126 case PPP_PAP: return "pap";
6127 case PPP_CHAP: return "chap";
6128 case PPP_IPV6CP: return "ipv6cp";
6129 }
6130 snprintf(buf, sizeof(buf), "0x%x", (unsigned)proto);
6131 return buf;
6132 }
6133
6134 static void
6135 sppp_print_bytes(const u_char *p, u_short len)
6136 {
6137 addlog(" %02x", *p++);
6138 while (--len > 0)
6139 addlog("-%02x", *p++);
6140 }
6141
6142 static void
6143 sppp_print_string(const char *p, u_short len)
6144 {
6145 u_char c;
6146
6147 while (len-- > 0) {
6148 c = *p++;
6149 /*
6150 * Print only ASCII chars directly. RFC 1994 recommends
6151 * using only them, but we don't rely on it. */
6152 if (c < ' ' || c > '~')
6153 addlog("\\x%x", c);
6154 else
6155 addlog("%c", c);
6156 }
6157 }
6158
6159 static const char *
6160 sppp_dotted_quad(uint32_t addr)
6161 {
6162 static char s[16];
6163 snprintf(s, sizeof(s), "%d.%d.%d.%d",
6164 (int)((addr >> 24) & 0xff),
6165 (int)((addr >> 16) & 0xff),
6166 (int)((addr >> 8) & 0xff),
6167 (int)(addr & 0xff));
6168 return s;
6169 }
6170
6171 /* a dummy, used to drop uninteresting events */
6172 static void
6173 sppp_null(struct sppp *unused)
6174 {
6175 /* do just nothing */
6176 }
6177 /*
6178 * This file is large. Tell emacs to highlight it nevertheless.
6179 *
6180 * Local Variables:
6181 * hilit-auto-highlight-maxout: 120000
6182 * End:
6183 */
6184
6185 /*
6186 * Module glue
6187 */
6188 MODULE(MODULE_CLASS_MISC, sppp_subr, NULL);
6189
6190 static int
6191 sppp_subr_modcmd(modcmd_t cmd, void *arg)
6192 {
6193 switch (cmd) {
6194 case MODULE_CMD_INIT:
6195 case MODULE_CMD_FINI:
6196 return 0;
6197 case MODULE_CMD_STAT:
6198 case MODULE_CMD_AUTOUNLOAD:
6199 default:
6200 return ENOTTY;
6201 }
6202 }
6203
6204 static void
6205 sppp_notify_up(struct sppp *sp)
6206 {
6207
6208 SPPP_LOCK(sp, RW_WRITER);
6209 lcp.Up(sp);
6210 SPPP_UNLOCK(sp);
6211 }
6212
6213 static void
6214 sppp_notify_down(struct sppp *sp)
6215 {
6216
6217 SPPP_LOCK(sp, RW_WRITER);
6218 lcp.Down(sp);
6219 SPPP_UNLOCK(sp);
6220 }
6221
6222 static void
6223 sppp_notify_tls_wlocked(struct sppp *sp)
6224 {
6225
6226 KASSERT(SPPP_WLOCKED(sp));
6227
6228 if (!sp->pp_tls)
6229 return;
6230
6231 SPPP_UNLOCK(sp);
6232 sp->pp_tls(sp);
6233 SPPP_LOCK(sp, RW_WRITER);
6234 }
6235
6236 static void
6237 sppp_notify_tlf_wlocked(struct sppp *sp)
6238 {
6239
6240 KASSERT(SPPP_WLOCKED(sp));
6241
6242 if (!sp->pp_tlf)
6243 return;
6244
6245 SPPP_UNLOCK(sp);
6246 sp->pp_tlf(sp);
6247 SPPP_LOCK(sp, RW_WRITER);
6248 }
6249
6250 static void
6251 sppp_notify_con(struct sppp *sp)
6252 {
6253
6254 if (!sp->pp_con)
6255 return;
6256
6257 sp->pp_con(sp);
6258 }
6259
6260 #ifdef INET6
6261 static void
6262 sppp_notify_con_wlocked(struct sppp *sp)
6263 {
6264
6265 KASSERT(SPPP_WLOCKED(sp));
6266
6267 SPPP_UNLOCK(sp);
6268 sppp_notify_con(sp);
6269 SPPP_LOCK(sp, RW_WRITER);
6270
6271 }
6272 #endif
6273
6274 static void
6275 sppp_notify_chg_wlocked(struct sppp *sp)
6276 {
6277
6278 KASSERT(SPPP_WLOCKED(sp));
6279
6280 if (!sp->pp_chg)
6281 return;
6282
6283 SPPP_UNLOCK(sp);
6284 sp->pp_chg(sp, sp->pp_phase);
6285 SPPP_LOCK(sp, RW_WRITER);
6286 }
6287