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