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