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