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