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