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