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