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