if_spppsubr.c revision 1.203 1 /* $NetBSD: if_spppsubr.c,v 1.203 2020/11/25 09:57:26 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.203 2020/11/25 09:57:26 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 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
3040 (sp->pp_flags & PP_NEEDAUTH) != 0)
3041 sppp_change_phase(sp, SPPP_PHASE_AUTHENTICATE);
3042 else
3043 sppp_change_phase(sp, SPPP_PHASE_NETWORK);
3044
3045 /*
3046 * Open all authentication protocols. This is even required
3047 * if we already proceeded to network phase, since it might be
3048 * that remote wants us to authenticate, so we might have to
3049 * send a PAP request. Undesired authentication protocols
3050 * don't do anything when they get an Open event.
3051 */
3052 for (i = 0; i < IDX_COUNT; i++)
3053 if ((cps[i])->flags & CP_AUTH) {
3054 sppp_wq_add(sp->wq_cp,
3055 &sp->scp[(cps[i])->protoidx].work_open);
3056 }
3057
3058 if (sp->pp_phase == SPPP_PHASE_NETWORK) {
3059 /* Notify all NCPs. */
3060 for (i = 0; i < IDX_COUNT; i++)
3061 if ((cps[i])->flags & CP_NCP) {
3062 sppp_wq_add(sp->wq_cp,
3063 &sp->scp[(cps[i])->protoidx].work_open);
3064 }
3065 }
3066
3067 /* notify low-level driver of state change */
3068 sppp_notify_chg_wlocked(sp);
3069 }
3070
3071 static void
3072 sppp_lcp_tld(struct sppp *sp)
3073 {
3074 int i, pi, phase;
3075
3076 KASSERT(SPPP_WLOCKED(sp));
3077
3078 phase = sp->pp_phase;
3079
3080 sppp_change_phase(sp, SPPP_PHASE_TERMINATE);
3081
3082 /*
3083 * Take upper layers down. We send the Down event first and
3084 * the Close second to prevent the upper layers from sending
3085 * ``a flurry of terminate-request packets'', as the RFC
3086 * describes it.
3087 */
3088 for (i = 0; i < IDX_COUNT; i++) {
3089 pi = (cps[i])->protoidx;
3090 if (((cps[i])->flags & CP_LCP) == 0) {
3091 /* skip if ncp was not started */
3092 if (phase != SPPP_PHASE_NETWORK &&
3093 ((cps[i])->flags & CP_NCP) != 0)
3094 continue;
3095
3096 sppp_wq_add(sp->wq_cp, &sp->scp[pi].work_down);
3097 sppp_wq_add(sp->wq_cp, &sp->scp[pi].work_close);
3098 }
3099 }
3100 }
3101
3102 static void
3103 sppp_lcp_tls(const struct cp *cp __unused, struct sppp *sp)
3104 {
3105
3106 KASSERT(SPPP_WLOCKED(sp));
3107
3108 if (sp->pp_max_auth_fail != 0 && sp->pp_auth_failures >= sp->pp_max_auth_fail) {
3109 printf("%s: authentication failed %d times, not retrying again\n",
3110 sp->pp_if.if_xname, sp->pp_auth_failures);
3111
3112 SPPP_UNLOCK(sp);
3113 if_down(&sp->pp_if);
3114 SPPP_LOCK(sp, RW_WRITER);
3115 return;
3116 }
3117
3118 sppp_change_phase(sp, SPPP_PHASE_ESTABLISH);
3119
3120 /* Notify lower layer if desired. */
3121 sppp_notify_tls_wlocked(sp);
3122 }
3123
3124 static void
3125 sppp_lcp_tlf(const struct cp *cp __unused, struct sppp *sp)
3126 {
3127
3128 KASSERT(SPPP_WLOCKED(sp));
3129
3130 sppp_change_phase(sp, SPPP_PHASE_DEAD);
3131
3132 /* Notify lower layer if desired. */
3133 sppp_notify_tlf_wlocked(sp);
3134 }
3135
3136 static void
3137 sppp_lcp_scr(struct sppp *sp)
3138 {
3139 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
3140 int i = 0;
3141 u_short authproto;
3142
3143 KASSERT(SPPP_WLOCKED(sp));
3144
3145 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
3146 if (! sp->lcp.magic)
3147 sp->lcp.magic = cprng_fast32();
3148 opt[i++] = LCP_OPT_MAGIC;
3149 opt[i++] = 6;
3150 opt[i++] = sp->lcp.magic >> 24;
3151 opt[i++] = sp->lcp.magic >> 16;
3152 opt[i++] = sp->lcp.magic >> 8;
3153 opt[i++] = sp->lcp.magic;
3154 }
3155
3156 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
3157 opt[i++] = LCP_OPT_MRU;
3158 opt[i++] = 4;
3159 opt[i++] = sp->lcp.mru >> 8;
3160 opt[i++] = sp->lcp.mru;
3161 }
3162
3163 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
3164 authproto = sp->hisauth.proto;
3165 opt[i++] = LCP_OPT_AUTH_PROTO;
3166 opt[i++] = authproto == PPP_CHAP? 5: 4;
3167 opt[i++] = authproto >> 8;
3168 opt[i++] = authproto;
3169 if (authproto == PPP_CHAP)
3170 opt[i++] = CHAP_MD5;
3171 }
3172
3173 sp->scp[IDX_LCP].confid = ++sp->scp[IDX_LCP].seq;
3174 sppp_cp_send(sp, PPP_LCP, CONF_REQ, sp->scp[IDX_LCP].confid, i, &opt);
3175 }
3176
3177 /*
3178 * Check the open NCPs, return true if at least one NCP is open.
3179 */
3180
3181 static int
3182 sppp_cp_check(struct sppp *sp, u_char cp_flags)
3183 {
3184 int i, mask;
3185
3186 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
3187 if ((sp->lcp.protos & mask) && (cps[i])->flags & cp_flags)
3188 return 1;
3189 return 0;
3190 }
3191
3192 /*
3193 * Re-check the open NCPs and see if we should terminate the link.
3194 * Called by the NCPs during their tlf action handling.
3195 */
3196 static void
3197 sppp_lcp_check_and_close(struct sppp *sp)
3198 {
3199
3200 KASSERT(SPPP_WLOCKED(sp));
3201
3202 if (sp->pp_phase < SPPP_PHASE_AUTHENTICATE) {
3203 /* don't bother, we are already going down */
3204 return;
3205 }
3206
3207 if (sp->pp_phase == SPPP_PHASE_AUTHENTICATE &&
3208 sppp_cp_check(sp, CP_AUTH))
3209 return;
3210
3211 if (sp->pp_phase >= SPPP_PHASE_NETWORK &&
3212 sppp_cp_check(sp, CP_NCP))
3213 return;
3214
3215 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_close);
3216 }
3217
3218 /*
3219 *--------------------------------------------------------------------------*
3220 * *
3221 * The IPCP implementation. *
3222 * *
3223 *--------------------------------------------------------------------------*
3224 */
3225
3226 static void
3227 sppp_ipcp_init(struct sppp *sp)
3228 {
3229 int error;
3230
3231 KASSERT(SPPP_WLOCKED(sp));
3232
3233 sp->ipcp.opts = 0;
3234 sp->ipcp.flags = 0;
3235 sp->scp[IDX_IPCP].state = STATE_INITIAL;
3236 sp->scp[IDX_IPCP].fail_counter = 0;
3237 sp->scp[IDX_IPCP].seq = 0;
3238 sp->scp[IDX_IPCP].rseq = 0;
3239 sppp_cp_init(&ipcp, sp);
3240
3241 error = workqueue_create(&sp->ipcp.update_addrs_wq, "ipcp_addr",
3242 sppp_update_ip_addrs_work, sp, PRI_SOFTNET, IPL_NET, 0);
3243 if (error)
3244 panic("%s: update_addrs workqueue_create failed (%d)\n",
3245 __func__, error);
3246 sp->ipcp.update_addrs_q = pcq_create(IPCP_UPDATE_LIMIT, KM_SLEEP);
3247
3248 sp->ipcp.update_addrs_enqueued = 0;
3249 }
3250
3251 static void
3252 sppp_ipcp_open(struct sppp *sp, void *xcp)
3253 {
3254 STDDCL;
3255 uint32_t myaddr, hisaddr;
3256
3257 KASSERT(SPPP_WLOCKED(sp));
3258
3259 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN|IPCP_MYADDR_SEEN|IPCP_MYADDR_DYN|IPCP_HISADDR_DYN);
3260 sp->ipcp.req_myaddr = 0;
3261 sp->ipcp.req_hisaddr = 0;
3262 memset(&sp->dns_addrs, 0, sizeof sp->dns_addrs);
3263
3264 #ifdef INET
3265 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
3266 #else
3267 myaddr = hisaddr = 0;
3268 #endif
3269 /*
3270 * If we don't have his address, this probably means our
3271 * interface doesn't want to talk IP at all. (This could
3272 * be the case if somebody wants to speak only IPX, for
3273 * example.) Don't open IPCP in this case.
3274 */
3275 if (hisaddr == 0) {
3276 /* XXX this message should go away */
3277 if (debug)
3278 log(LOG_DEBUG, "%s: ipcp_open(): no IP interface\n",
3279 ifp->if_xname);
3280 return;
3281 }
3282
3283 if (myaddr == 0) {
3284 /*
3285 * I don't have an assigned address, so i need to
3286 * negotiate my address.
3287 */
3288 sp->ipcp.flags |= IPCP_MYADDR_DYN;
3289 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3290 }
3291 if (hisaddr == 1) {
3292 /*
3293 * XXX - remove this hack!
3294 * remote has no valid address, we need to get one assigned.
3295 */
3296 sp->ipcp.flags |= IPCP_HISADDR_DYN;
3297 }
3298 sppp_open_event(sp, xcp);
3299 }
3300
3301 static void
3302 sppp_ipcp_close(struct sppp *sp, void *xcp)
3303 {
3304
3305 KASSERT(SPPP_WLOCKED(sp));
3306
3307 sppp_close_event(sp, xcp);
3308
3309 #ifdef INET
3310 if (sp->ipcp.flags & (IPCP_MYADDR_DYN|IPCP_HISADDR_DYN))
3311 /*
3312 * Some address was dynamic, clear it again.
3313 */
3314 sppp_clear_ip_addrs(sp);
3315 #endif
3316 }
3317
3318 /*
3319 * Analyze a configure request. Return true if it was agreeable, and
3320 * caused action sca, false if it has been rejected or nak'ed, and
3321 * caused action scn. (The return value is used to make the state
3322 * transition decision in the state automaton.)
3323 */
3324 static int
3325 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int origlen)
3326 {
3327 u_char *buf, *r, *p, l, blen, type;
3328 struct ifnet *ifp = &sp->pp_if;
3329 int rlen, len, debug = ifp->if_flags & IFF_DEBUG;
3330 uint32_t hisaddr, desiredaddr;
3331
3332 KASSERT(SPPP_WLOCKED(sp));
3333
3334 if (origlen < sizeof(*h))
3335 return 0;
3336
3337 origlen -= sizeof(*h);
3338 type = 0;
3339
3340 /*
3341 * Make sure to allocate a buf that can at least hold a
3342 * conf-nak with an `address' option. We might need it below.
3343 */
3344 blen = MAX(6, origlen);
3345
3346 buf = kmem_intr_alloc(blen, KM_NOSLEEP);
3347 if (buf == NULL)
3348 return 0;
3349
3350 /* pass 1: see if we can recognize them */
3351 if (debug)
3352 log(LOG_DEBUG, "%s: ipcp parse opts:",
3353 ifp->if_xname);
3354 p = (void *)(h + 1);
3355 r = buf;
3356 rlen = 0;
3357 for (len = origlen; len > 1; len -= l, p += l) {
3358 l = p[1];
3359 if (l == 0)
3360 break;
3361
3362 /* Sanity check option length */
3363 if (l > len) {
3364 /* XXX should we just RXJ? */
3365 addlog("%s: malicious IPCP option received, dropping\n",
3366 ifp->if_xname);
3367 rlen = -1;
3368 goto end;
3369 }
3370 if (debug)
3371 addlog(" %s", sppp_ipcp_opt_name(*p));
3372 switch (p[0]) {
3373 #ifdef notyet
3374 case IPCP_OPT_COMPRESSION:
3375 if (len >= 6 && l >= 6) {
3376 /* correctly formed compress option */
3377 continue;
3378 }
3379 if (debug)
3380 addlog(" [invalid]");
3381 break;
3382 #endif
3383 case IPCP_OPT_ADDRESS:
3384 if (len >= 6 && l == 6) {
3385 /* correctly formed address option */
3386 continue;
3387 }
3388 if (debug)
3389 addlog(" [invalid]");
3390 break;
3391 default:
3392 /* Others not supported. */
3393 if (debug)
3394 addlog(" [rej]");
3395 break;
3396 }
3397 /* Add the option to rejected list. */
3398 if (rlen + l > blen) {
3399 if (debug)
3400 addlog(" [overflow]");
3401 continue;
3402 }
3403 memcpy(r, p, l);
3404 r += l;
3405 rlen += l;
3406 }
3407
3408 if (rlen > 0) {
3409 type = CONF_REJ;
3410 goto end;
3411 }
3412
3413 if (debug)
3414 addlog("\n");
3415
3416 /* pass 2: parse option values */
3417 if (sp->ipcp.flags & IPCP_HISADDR_SEEN)
3418 hisaddr = sp->ipcp.req_hisaddr; /* we already aggreed on that */
3419 else
3420 #ifdef INET
3421 sppp_get_ip_addrs(sp, 0, &hisaddr, 0); /* user configuration */
3422 #else
3423 hisaddr = 0;
3424 #endif
3425 if (debug)
3426 log(LOG_DEBUG, "%s: ipcp parse opt values: ",
3427 ifp->if_xname);
3428 p = (void *)(h + 1);
3429 r = buf;
3430 rlen = 0;
3431 for (len = origlen; len > 1; len -= l, p += l) {
3432 l = p[1];
3433 if (l == 0)
3434 break;
3435
3436 if (debug)
3437 addlog(" %s", sppp_ipcp_opt_name(*p));
3438 switch (p[0]) {
3439 #ifdef notyet
3440 case IPCP_OPT_COMPRESSION:
3441 continue;
3442 #endif
3443 case IPCP_OPT_ADDRESS:
3444 desiredaddr = p[2] << 24 | p[3] << 16 |
3445 p[4] << 8 | p[5];
3446 if (desiredaddr == hisaddr ||
3447 ((sp->ipcp.flags & IPCP_HISADDR_DYN) && desiredaddr != 0)) {
3448 /*
3449 * Peer's address is same as our value,
3450 * this is agreeable. Gonna conf-ack
3451 * it.
3452 */
3453 if (debug)
3454 addlog(" %s [ack]",
3455 sppp_dotted_quad(hisaddr));
3456 /* record that we've seen it already */
3457 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3458 sp->ipcp.req_hisaddr = desiredaddr;
3459 hisaddr = desiredaddr;
3460 continue;
3461 }
3462 /*
3463 * The address wasn't agreeable. This is either
3464 * he sent us 0.0.0.0, asking to assign him an
3465 * address, or he send us another address not
3466 * matching our value. Either case, we gonna
3467 * conf-nak it with our value.
3468 */
3469 if (debug) {
3470 if (desiredaddr == 0)
3471 addlog(" [addr requested]");
3472 else
3473 addlog(" %s [not agreed]",
3474 sppp_dotted_quad(desiredaddr));
3475 }
3476
3477 p[2] = hisaddr >> 24;
3478 p[3] = hisaddr >> 16;
3479 p[4] = hisaddr >> 8;
3480 p[5] = hisaddr;
3481 break;
3482 }
3483 if (rlen + l > blen) {
3484 if (debug)
3485 addlog(" [overflow]");
3486 continue;
3487 }
3488 /* Add the option to nak'ed list. */
3489 memcpy(r, p, l);
3490 r += l;
3491 rlen += l;
3492 }
3493
3494 if (rlen > 0) {
3495 type = CONF_NAK;
3496 } else {
3497 if ((sp->ipcp.flags & IPCP_HISADDR_SEEN) == 0) {
3498 /*
3499 * If we are about to conf-ack the request, but haven't seen
3500 * his address so far, gonna conf-nak it instead, with the
3501 * `address' option present and our idea of his address being
3502 * filled in there, to request negotiation of both addresses.
3503 *
3504 * XXX This can result in an endless req - nak loop if peer
3505 * doesn't want to send us his address. Q: What should we do
3506 * about it? XXX A: implement the max-failure counter.
3507 */
3508 buf[0] = IPCP_OPT_ADDRESS;
3509 buf[1] = 6;
3510 buf[2] = hisaddr >> 24;
3511 buf[3] = hisaddr >> 16;
3512 buf[4] = hisaddr >> 8;
3513 buf[5] = hisaddr;
3514 rlen = 6;
3515 if (debug)
3516 addlog(" still need hisaddr");
3517 type = CONF_NAK;
3518 } else {
3519 type = CONF_ACK;
3520 rlen = origlen;
3521 memcpy(r, h + 1, rlen);
3522 }
3523 }
3524
3525 end:
3526 if (debug)
3527 addlog("\n");
3528
3529 if (rlen < 0) {
3530 kmem_intr_free(buf, blen);
3531 return -1;
3532 }
3533
3534 if (sp->scp[IDX_IPCP].rcr_buf != NULL) {
3535 kmem_intr_free(sp->scp[IDX_IPCP].rcr_buf,
3536 sp->scp[IDX_IPCP].rcr_blen);
3537 }
3538
3539 sp->scp[IDX_IPCP].rcr_type = type;
3540 sp->scp[IDX_IPCP].rcr_buf = buf;
3541 sp->scp[IDX_IPCP].rcr_blen = blen;
3542 sp->scp[IDX_IPCP].rcr_rlen = rlen;
3543
3544 if (type != CONF_ACK)
3545 return 0;
3546 return 1;
3547 }
3548
3549 /*
3550 * Analyze the IPCP Configure-Reject option list, and adjust our
3551 * negotiation.
3552 */
3553 static void
3554 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3555 {
3556 u_char *p, l;
3557 struct ifnet *ifp = &sp->pp_if;
3558 int debug = ifp->if_flags & IFF_DEBUG;
3559
3560 KASSERT(SPPP_WLOCKED(sp));
3561
3562 if (len <= sizeof(*h))
3563 return;
3564
3565 len -= sizeof(*h);
3566
3567 if (debug)
3568 log(LOG_DEBUG, "%s: ipcp rej opts:",
3569 ifp->if_xname);
3570
3571 p = (void *)(h + 1);
3572 for (; len > 1; len -= l, p += l) {
3573 l = p[1];
3574 if (l == 0)
3575 break;
3576
3577 /* Sanity check option length */
3578 if (l > len) {
3579 /* XXX should we just RXJ? */
3580 addlog("%s: malicious IPCP option received, dropping\n",
3581 ifp->if_xname);
3582 goto end;
3583 }
3584 if (debug)
3585 addlog(" %s", sppp_ipcp_opt_name(*p));
3586 switch (p[0]) {
3587 case IPCP_OPT_ADDRESS:
3588 /*
3589 * Peer doesn't grok address option. This is
3590 * bad. XXX Should we better give up here?
3591 */
3592 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3593 break;
3594 #ifdef notyet
3595 case IPCP_OPT_COMPRESS:
3596 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
3597 break;
3598 #endif
3599 }
3600 }
3601 if (debug)
3602 addlog("\n");
3603 end:
3604 return;
3605 }
3606
3607 /*
3608 * Analyze the IPCP Configure-NAK option list, and adjust our
3609 * negotiation.
3610 */
3611 static void
3612 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3613 {
3614 u_char *p, l;
3615 struct ifnet *ifp = &sp->pp_if;
3616 int debug = ifp->if_flags & IFF_DEBUG;
3617 uint32_t wantaddr;
3618
3619 KASSERT(SPPP_WLOCKED(sp));
3620
3621 len -= sizeof(*h);
3622
3623 if (debug)
3624 log(LOG_DEBUG, "%s: ipcp nak opts:",
3625 ifp->if_xname);
3626
3627 p = (void *)(h + 1);
3628 for (; len > 1; len -= l, p += l) {
3629 l = p[1];
3630 if (l == 0)
3631 break;
3632
3633 /* Sanity check option length */
3634 if (l > len) {
3635 /* XXX should we just RXJ? */
3636 addlog("%s: malicious IPCP option received, dropping\n",
3637 ifp->if_xname);
3638 return;
3639 }
3640 if (debug)
3641 addlog(" %s", sppp_ipcp_opt_name(*p));
3642 switch (*p) {
3643 case IPCP_OPT_ADDRESS:
3644 /*
3645 * Peer doesn't like our local IP address. See
3646 * if we can do something for him. We'll drop
3647 * him our address then.
3648 */
3649 if (len >= 6 && l == 6) {
3650 wantaddr = p[2] << 24 | p[3] << 16 |
3651 p[4] << 8 | p[5];
3652 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3653 if (debug)
3654 addlog(" [wantaddr %s]",
3655 sppp_dotted_quad(wantaddr));
3656 /*
3657 * When doing dynamic address assignment,
3658 * we accept his offer. Otherwise, we
3659 * ignore it and thus continue to negotiate
3660 * our already existing value.
3661 */
3662 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3663 if (debug)
3664 addlog(" [agree]");
3665 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3666 sp->ipcp.req_myaddr = wantaddr;
3667 }
3668 }
3669 break;
3670
3671 case IPCP_OPT_PRIMDNS:
3672 if (len >= 6 && l == 6) {
3673 sp->dns_addrs[0] = p[2] << 24 | p[3] << 16 |
3674 p[4] << 8 | p[5];
3675 }
3676 break;
3677
3678 case IPCP_OPT_SECDNS:
3679 if (len >= 6 && l == 6) {
3680 sp->dns_addrs[1] = p[2] << 24 | p[3] << 16 |
3681 p[4] << 8 | p[5];
3682 }
3683 break;
3684 #ifdef notyet
3685 case IPCP_OPT_COMPRESS:
3686 /*
3687 * Peer wants different compression parameters.
3688 */
3689 break;
3690 #endif
3691 }
3692 }
3693 if (debug)
3694 addlog("\n");
3695 }
3696
3697 static void
3698 sppp_ipcp_tlu(struct sppp *sp)
3699 {
3700 #ifdef INET
3701 KASSERT(SPPP_WLOCKED(sp));
3702 /* we are up. Set addresses and notify anyone interested */
3703 sppp_set_ip_addrs(sp);
3704 #endif
3705 }
3706
3707 static void
3708 sppp_ipcp_scr(struct sppp *sp)
3709 {
3710 uint8_t opt[6 /* compression */ + 6 /* address */ + 12 /* dns addresses */];
3711 #ifdef INET
3712 uint32_t ouraddr;
3713 #endif
3714 int i = 0;
3715
3716 KASSERT(SPPP_WLOCKED(sp));
3717
3718 #ifdef notyet
3719 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3720 opt[i++] = IPCP_OPT_COMPRESSION;
3721 opt[i++] = 6;
3722 opt[i++] = 0; /* VJ header compression */
3723 opt[i++] = 0x2d; /* VJ header compression */
3724 opt[i++] = max_slot_id;
3725 opt[i++] = comp_slot_id;
3726 }
3727 #endif
3728
3729 #ifdef INET
3730 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3731 if (sp->ipcp.flags & IPCP_MYADDR_SEEN)
3732 ouraddr = sp->ipcp.req_myaddr; /* not sure if this can ever happen */
3733 else
3734 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3735 opt[i++] = IPCP_OPT_ADDRESS;
3736 opt[i++] = 6;
3737 opt[i++] = ouraddr >> 24;
3738 opt[i++] = ouraddr >> 16;
3739 opt[i++] = ouraddr >> 8;
3740 opt[i++] = ouraddr;
3741 }
3742 #endif
3743
3744 if (sp->query_dns & 1) {
3745 opt[i++] = IPCP_OPT_PRIMDNS;
3746 opt[i++] = 6;
3747 opt[i++] = sp->dns_addrs[0] >> 24;
3748 opt[i++] = sp->dns_addrs[0] >> 16;
3749 opt[i++] = sp->dns_addrs[0] >> 8;
3750 opt[i++] = sp->dns_addrs[0];
3751 }
3752 if (sp->query_dns & 2) {
3753 opt[i++] = IPCP_OPT_SECDNS;
3754 opt[i++] = 6;
3755 opt[i++] = sp->dns_addrs[1] >> 24;
3756 opt[i++] = sp->dns_addrs[1] >> 16;
3757 opt[i++] = sp->dns_addrs[1] >> 8;
3758 opt[i++] = sp->dns_addrs[1];
3759 }
3760
3761 sp->scp[IDX_IPCP].confid = ++sp->scp[IDX_IPCP].seq;
3762 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->scp[IDX_IPCP].confid, i, &opt);
3763 }
3764
3765
3766 /*
3767 *--------------------------------------------------------------------------*
3768 * *
3769 * The IPv6CP implementation. *
3770 * *
3771 *--------------------------------------------------------------------------*
3772 */
3773
3774 #ifdef INET6
3775 static void
3776 sppp_ipv6cp_init(struct sppp *sp)
3777 {
3778
3779 KASSERT(SPPP_WLOCKED(sp));
3780
3781 sp->ipv6cp.opts = 0;
3782 sp->ipv6cp.flags = 0;
3783 sp->scp[IDX_IPV6CP].state = STATE_INITIAL;
3784 sp->scp[IDX_IPV6CP].fail_counter = 0;
3785 sp->scp[IDX_IPV6CP].seq = 0;
3786 sp->scp[IDX_IPV6CP].rseq = 0;
3787 sppp_cp_init(&ipv6cp, sp);
3788 }
3789
3790 static void
3791 sppp_ipv6cp_open(struct sppp *sp, void *xcp)
3792 {
3793 STDDCL;
3794 struct in6_addr myaddr, hisaddr;
3795
3796 KASSERT(SPPP_WLOCKED(sp));
3797
3798 #ifdef IPV6CP_MYIFID_DYN
3799 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3800 #else
3801 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3802 #endif
3803
3804 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3805 /*
3806 * If we don't have our address, this probably means our
3807 * interface doesn't want to talk IPv6 at all. (This could
3808 * be the case if somebody wants to speak only IPX, for
3809 * example.) Don't open IPv6CP in this case.
3810 */
3811 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3812 /* XXX this message should go away */
3813 if (debug)
3814 log(LOG_DEBUG, "%s: ipv6cp_open(): no IPv6 interface\n",
3815 ifp->if_xname);
3816 return;
3817 }
3818
3819 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3820 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3821 sppp_open_event(sp, xcp);
3822 }
3823
3824 /*
3825 * Analyze a configure request. Return true if it was agreeable, and
3826 * caused action sca, false if it has been rejected or nak'ed, and
3827 * caused action scn. (The return value is used to make the state
3828 * transition decision in the state automaton.)
3829 */
3830 static int
3831 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int origlen)
3832 {
3833 u_char *buf, *r, *p, l, blen;
3834 struct ifnet *ifp = &sp->pp_if;
3835 int rlen, len, debug = ifp->if_flags & IFF_DEBUG;
3836 struct in6_addr myaddr, desiredaddr, suggestaddr;
3837 int ifidcount;
3838 int type;
3839 int collision, nohisaddr;
3840 char ip6buf[INET6_ADDRSTRLEN];
3841
3842 KASSERT(SPPP_WLOCKED(sp));
3843
3844 if (origlen < sizeof(*h))
3845 return 0;
3846
3847 origlen -= sizeof(*h);
3848 type = 0;
3849
3850 /*
3851 * Make sure to allocate a buf that can at least hold a
3852 * conf-nak with an `address' option. We might need it below.
3853 */
3854 blen = MAX(6, origlen);
3855
3856 buf = kmem_intr_alloc(blen, KM_NOSLEEP);
3857 if (buf == NULL)
3858 return 0;
3859
3860 /* pass 1: see if we can recognize them */
3861 if (debug)
3862 log(LOG_DEBUG, "%s: ipv6cp parse opts:",
3863 ifp->if_xname);
3864 p = (void *)(h + 1);
3865 r = buf;
3866 rlen = 0;
3867 ifidcount = 0;
3868 for (len = origlen; len > 1; len -= l, p += l) {
3869 l = p[1];
3870 if (l == 0)
3871 break;
3872
3873 /* Sanity check option length */
3874 if (l > len) {
3875 /* XXX just RXJ? */
3876 addlog("%s: received malicious IPCPv6 option, "
3877 "dropping\n", ifp->if_xname);
3878 rlen = -1;
3879 goto end;
3880 }
3881 if (debug)
3882 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3883 switch (p[0]) {
3884 case IPV6CP_OPT_IFID:
3885 if (len >= 10 && l == 10 && ifidcount == 0) {
3886 /* correctly formed address option */
3887 ifidcount++;
3888 continue;
3889 }
3890 if (debug)
3891 addlog(" [invalid]");
3892 break;
3893 #ifdef notyet
3894 case IPV6CP_OPT_COMPRESSION:
3895 if (len >= 4 && l >= 4) {
3896 /* correctly formed compress option */
3897 continue;
3898 }
3899 if (debug)
3900 addlog(" [invalid]");
3901 break;
3902 #endif
3903 default:
3904 /* Others not supported. */
3905 if (debug)
3906 addlog(" [rej]");
3907 break;
3908 }
3909 if (rlen + l > blen) {
3910 if (debug)
3911 addlog(" [overflow]");
3912 continue;
3913 }
3914 /* Add the option to rejected list. */
3915 memcpy(r, p, l);
3916 r += l;
3917 rlen += l;
3918 }
3919
3920 if (rlen > 0) {
3921 type = CONF_REJ;
3922 goto end;
3923 }
3924
3925 if (debug)
3926 addlog("\n");
3927
3928 /* pass 2: parse option values */
3929 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3930 if (debug)
3931 log(LOG_DEBUG, "%s: ipv6cp parse opt values: ",
3932 ifp->if_xname);
3933 p = (void *)(h + 1);
3934 r = buf;
3935 rlen = 0;
3936 type = CONF_ACK;
3937 for (len = origlen; len > 1; len -= l, p += l) {
3938 l = p[1];
3939 if (l == 0)
3940 break;
3941
3942 if (debug)
3943 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3944 switch (p[0]) {
3945 #ifdef notyet
3946 case IPV6CP_OPT_COMPRESSION:
3947 continue;
3948 #endif
3949 case IPV6CP_OPT_IFID:
3950 memset(&desiredaddr, 0, sizeof(desiredaddr));
3951 memcpy(&desiredaddr.s6_addr[8], &p[2], 8);
3952 collision = (memcmp(&desiredaddr.s6_addr[8],
3953 &myaddr.s6_addr[8], 8) == 0);
3954 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3955
3956 desiredaddr.s6_addr16[0] = htons(0xfe80);
3957 (void)in6_setscope(&desiredaddr, &sp->pp_if, NULL);
3958
3959 if (!collision && !nohisaddr) {
3960 /* no collision, hisaddr known - Conf-Ack */
3961 type = CONF_ACK;
3962
3963 if (debug) {
3964 addlog(" %s [%s]",
3965 IN6_PRINT(ip6buf, &desiredaddr),
3966 sppp_cp_type_name(type));
3967 }
3968 continue;
3969 }
3970
3971 memset(&suggestaddr, 0, sizeof(suggestaddr));
3972 if (collision && nohisaddr) {
3973 /* collision, hisaddr unknown - Conf-Rej */
3974 type = CONF_REJ;
3975 memset(&p[2], 0, 8);
3976 } else {
3977 /*
3978 * - no collision, hisaddr unknown, or
3979 * - collision, hisaddr known
3980 * Conf-Nak, suggest hisaddr
3981 */
3982 type = CONF_NAK;
3983 sppp_suggest_ip6_addr(sp, &suggestaddr);
3984 memcpy(&p[2], &suggestaddr.s6_addr[8], 8);
3985 }
3986 if (debug)
3987 addlog(" %s [%s]", IN6_PRINT(ip6buf, &desiredaddr),
3988 sppp_cp_type_name(type));
3989 break;
3990 }
3991 if (rlen + l > blen) {
3992 if (debug)
3993 addlog(" [overflow]");
3994 continue;
3995 }
3996 /* Add the option to nak'ed list. */
3997 memcpy(r, p, l);
3998 r += l;
3999 rlen += l;
4000 }
4001
4002 if (rlen > 0) {
4003 if (type != CONF_ACK) {
4004 if (debug) {
4005 addlog(" send %s suggest %s\n",
4006 sppp_cp_type_name(type),
4007 IN6_PRINT(ip6buf, &suggestaddr));
4008 }
4009 }
4010 #ifdef notdef
4011 if (type == CONF_ACK)
4012 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
4013 #endif
4014 } else {
4015 if (type == CONF_ACK) {
4016 rlen = origlen;
4017 memcpy(r, h + 1, rlen);
4018 }
4019 }
4020 end:
4021 if (debug)
4022 addlog("\n");
4023
4024 if (rlen < 0) {
4025 kmem_intr_free(buf, blen);
4026 return -1;
4027 }
4028
4029 if (sp->scp[IDX_IPV6CP].rcr_buf != NULL) {
4030 kmem_intr_free(sp->scp[IDX_IPV6CP].rcr_buf,
4031 sp->scp[IDX_IPV6CP].rcr_blen);
4032 }
4033
4034 sp->scp[IDX_IPV6CP].rcr_type = type;
4035 sp->scp[IDX_IPV6CP].rcr_buf = buf;
4036 sp->scp[IDX_IPV6CP].rcr_blen = blen;
4037 sp->scp[IDX_IPV6CP].rcr_rlen = rlen;
4038
4039 if (type != CONF_ACK)
4040 return 0;
4041 return 0;
4042 }
4043
4044 /*
4045 * Analyze the IPv6CP Configure-Reject option list, and adjust our
4046 * negotiation.
4047 */
4048 static void
4049 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
4050 {
4051 u_char *p, l;
4052 struct ifnet *ifp = &sp->pp_if;
4053 int debug = ifp->if_flags & IFF_DEBUG;
4054
4055 KASSERT(SPPP_WLOCKED(sp));
4056
4057 if (len <= sizeof(*h))
4058 return;
4059
4060 len -= sizeof(*h);
4061
4062 if (debug)
4063 log(LOG_DEBUG, "%s: ipv6cp rej opts:",
4064 ifp->if_xname);
4065
4066 p = (void *)(h + 1);
4067 for (; len > 1; len -= l, p += l) {
4068 l = p[1];
4069 if (l == 0)
4070 break;
4071
4072 if (l > len) {
4073 /* XXX just RXJ? */
4074 addlog("%s: received malicious IPCPv6 option, "
4075 "dropping\n", ifp->if_xname);
4076 goto end;
4077 }
4078 if (debug)
4079 addlog(" %s", sppp_ipv6cp_opt_name(*p));
4080 switch (p[0]) {
4081 case IPV6CP_OPT_IFID:
4082 /*
4083 * Peer doesn't grok address option. This is
4084 * bad. XXX Should we better give up here?
4085 */
4086 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
4087 break;
4088 #ifdef notyet
4089 case IPV6CP_OPT_COMPRESS:
4090 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
4091 break;
4092 #endif
4093 }
4094 }
4095 if (debug)
4096 addlog("\n");
4097 end:
4098 return;
4099 }
4100
4101 /*
4102 * Analyze the IPv6CP Configure-NAK option list, and adjust our
4103 * negotiation.
4104 */
4105 static void
4106 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
4107 {
4108 u_char *p, l;
4109 struct ifnet *ifp = &sp->pp_if;
4110 int debug = ifp->if_flags & IFF_DEBUG;
4111 struct in6_addr suggestaddr;
4112 char ip6buf[INET6_ADDRSTRLEN];
4113
4114 KASSERT(SPPP_WLOCKED(sp));
4115
4116 if (len <= sizeof(*h))
4117 return;
4118
4119 len -= sizeof(*h);
4120
4121 if (debug)
4122 log(LOG_DEBUG, "%s: ipv6cp nak opts:",
4123 ifp->if_xname);
4124
4125 p = (void *)(h + 1);
4126 for (; len > 1; len -= l, p += l) {
4127 l = p[1];
4128 if (l == 0)
4129 break;
4130
4131 if (l > len) {
4132 /* XXX just RXJ? */
4133 addlog("%s: received malicious IPCPv6 option, "
4134 "dropping\n", ifp->if_xname);
4135 goto end;
4136 }
4137 if (debug)
4138 addlog(" %s", sppp_ipv6cp_opt_name(*p));
4139 switch (p[0]) {
4140 case IPV6CP_OPT_IFID:
4141 /*
4142 * Peer doesn't like our local ifid. See
4143 * if we can do something for him. We'll drop
4144 * him our address then.
4145 */
4146 if (len < 10 || l != 10)
4147 break;
4148 memset(&suggestaddr, 0, sizeof(suggestaddr));
4149 suggestaddr.s6_addr16[0] = htons(0xfe80);
4150 (void)in6_setscope(&suggestaddr, &sp->pp_if, NULL);
4151 memcpy(&suggestaddr.s6_addr[8], &p[2], 8);
4152
4153 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
4154 if (debug)
4155 addlog(" [suggestaddr %s]",
4156 IN6_PRINT(ip6buf, &suggestaddr));
4157 #ifdef IPV6CP_MYIFID_DYN
4158 /*
4159 * When doing dynamic address assignment,
4160 * we accept his offer.
4161 */
4162 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
4163 struct in6_addr lastsuggest;
4164 /*
4165 * If <suggested myaddr from peer> equals to
4166 * <hisaddr we have suggested last time>,
4167 * we have a collision. generate new random
4168 * ifid.
4169 */
4170 sppp_suggest_ip6_addr(&lastsuggest);
4171 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
4172 lastsuggest)) {
4173 if (debug)
4174 addlog(" [random]");
4175 sppp_gen_ip6_addr(sp, &suggestaddr);
4176 }
4177 sppp_set_ip6_addr(sp, &suggestaddr, 0);
4178 if (debug)
4179 addlog(" [agree]");
4180 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
4181 }
4182 #else
4183 /*
4184 * Since we do not do dynamic address assignment,
4185 * we ignore it and thus continue to negotiate
4186 * our already existing value. This can possibly
4187 * go into infinite request-reject loop.
4188 *
4189 * This is not likely because we normally use
4190 * ifid based on MAC-address.
4191 * If you have no ethernet card on the node, too bad.
4192 * XXX should we use fail_counter?
4193 */
4194 #endif
4195 break;
4196 #ifdef notyet
4197 case IPV6CP_OPT_COMPRESS:
4198 /*
4199 * Peer wants different compression parameters.
4200 */
4201 break;
4202 #endif
4203 }
4204 }
4205 if (debug)
4206 addlog("\n");
4207 end:
4208 return;
4209 }
4210
4211 static void
4212 sppp_ipv6cp_tlu(struct sppp *sp)
4213 {
4214
4215 KASSERT(SPPP_WLOCKED(sp));
4216 /* we are up - notify isdn daemon */
4217 sppp_notify_con_wlocked(sp);
4218 }
4219
4220 static void
4221 sppp_ipv6cp_scr(struct sppp *sp)
4222 {
4223 char opt[10 /* ifid */ + 4 /* compression, minimum */];
4224 struct in6_addr ouraddr;
4225 int i = 0;
4226
4227 KASSERT(SPPP_WLOCKED(sp));
4228
4229 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
4230 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
4231 opt[i++] = IPV6CP_OPT_IFID;
4232 opt[i++] = 10;
4233 memcpy(&opt[i], &ouraddr.s6_addr[8], 8);
4234 i += 8;
4235 }
4236
4237 #ifdef notyet
4238 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
4239 opt[i++] = IPV6CP_OPT_COMPRESSION;
4240 opt[i++] = 4;
4241 opt[i++] = 0; /* TBD */
4242 opt[i++] = 0; /* TBD */
4243 /* variable length data may follow */
4244 }
4245 #endif
4246
4247 sp->scp[IDX_IPV6CP].confid = ++sp->scp[IDX_IPV6CP].seq;
4248 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->scp[IDX_IPV6CP].confid, i, &opt);
4249 }
4250 #else /*INET6*/
4251 static void
4252 sppp_ipv6cp_init(struct sppp *sp)
4253 {
4254
4255 KASSERT(SPPP_WLOCKED(sp));
4256 }
4257
4258 static void
4259 sppp_ipv6cp_open(struct sppp *sp, void *xcp)
4260 {
4261
4262 KASSERT(SPPP_WLOCKED(sp));
4263 }
4264
4265 static int
4266 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h,
4267 int len)
4268 {
4269
4270 KASSERT(SPPP_WLOCKED(sp));
4271 return 0;
4272 }
4273
4274 static void
4275 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h,
4276 int len)
4277 {
4278
4279 KASSERT(SPPP_WLOCKED(sp));
4280 }
4281
4282 static void
4283 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h,
4284 int len)
4285 {
4286
4287 KASSERT(SPPP_WLOCKED(sp));
4288 }
4289
4290 static void
4291 sppp_ipv6cp_tlu(struct sppp *sp)
4292 {
4293
4294 KASSERT(SPPP_WLOCKED(sp));
4295 }
4296
4297 static void
4298 sppp_ipv6cp_scr(struct sppp *sp)
4299 {
4300
4301 KASSERT(SPPP_WLOCKED(sp));
4302 }
4303 #endif /*INET6*/
4304
4305
4306 /*
4307 *--------------------------------------------------------------------------*
4308 * *
4309 * The CHAP implementation. *
4310 * *
4311 *--------------------------------------------------------------------------*
4312 */
4313 /*
4314 * The authentication protocols is implemented on the state machine for
4315 * control protocols. And it uses following actions and events.
4316 *
4317 * Actions:
4318 * - scr: send CHAP_CHALLENGE and CHAP_RESPONSE
4319 * - sca: send CHAP_SUCCESS
4320 * - scn: send CHAP_FAILURE and shutdown lcp
4321 * Events:
4322 * - RCR+: receive CHAP_RESPONSE containing correct digest
4323 * - RCR-: receive CHAP_RESPONSE containing wrong digest
4324 * - RCA: receive CHAP_SUCCESS
4325 * - RCN: (this event is unused)
4326 * - TO+: re-send CHAP_CHALLENGE and CHAP_RESPONSE
4327 * - TO-: this layer finish
4328 */
4329
4330 /*
4331 * Handle incoming CHAP packets.
4332 */
4333 void
4334 sppp_chap_input(struct sppp *sp, struct mbuf *m)
4335 {
4336 STDDCL;
4337 struct lcp_header *h;
4338 int len, x;
4339 u_char *value, *name, digest[sizeof(sp->chap.challenge)];
4340 int value_len, name_len;
4341 MD5_CTX ctx;
4342
4343 len = m->m_pkthdr.len;
4344 if (len < 4) {
4345 if (debug)
4346 log(LOG_DEBUG,
4347 "%s: chap invalid packet length: %d bytes\n",
4348 ifp->if_xname, len);
4349 return;
4350 }
4351 h = mtod(m, struct lcp_header *);
4352 if (len > ntohs(h->len))
4353 len = ntohs(h->len);
4354
4355 SPPP_LOCK(sp, RW_WRITER);
4356
4357 switch (h->type) {
4358 /* challenge, failure and success are his authproto */
4359 case CHAP_CHALLENGE:
4360 if (sp->myauth.secret == NULL || sp->myauth.name == NULL) {
4361 /* can't do anything useful */
4362 sp->pp_auth_failures++;
4363 printf("%s: chap input without my name and my secret being set\n",
4364 ifp->if_xname);
4365 break;
4366 }
4367 value = 1 + (u_char *)(h + 1);
4368 value_len = value[-1];
4369 name = value + value_len;
4370 name_len = len - value_len - 5;
4371 if (name_len < 0) {
4372 if (debug) {
4373 log(LOG_DEBUG,
4374 "%s: chap corrupted challenge "
4375 "<%s id=0x%x len=%d",
4376 ifp->if_xname,
4377 sppp_auth_type_name(PPP_CHAP, h->type),
4378 h->ident, ntohs(h->len));
4379 if (len > 4)
4380 sppp_print_bytes((u_char *)(h + 1),
4381 len - 4);
4382 addlog(">\n");
4383 }
4384 break;
4385 }
4386
4387 if (debug) {
4388 log(LOG_DEBUG,
4389 "%s: chap input <%s id=0x%x len=%d name=",
4390 ifp->if_xname,
4391 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
4392 ntohs(h->len));
4393 sppp_print_string((char *) name, name_len);
4394 addlog(" value-size=%d value=", value_len);
4395 sppp_print_bytes(value, value_len);
4396 addlog(">\n");
4397 }
4398
4399 /* Compute reply value. */
4400 MD5Init(&ctx);
4401 MD5Update(&ctx, &h->ident, 1);
4402 MD5Update(&ctx, sp->myauth.secret, sp->myauth.secret_len);
4403 MD5Update(&ctx, value, value_len);
4404 MD5Final(sp->chap.digest, &ctx);
4405 sp->chap.digest_len = sizeof(sp->chap.digest);
4406 sp->scp[IDX_CHAP].rconfid = h->ident;
4407
4408 sppp_wq_add(sp->wq_cp, &sp->chap.work_challenge_rcvd);
4409 break;
4410
4411 case CHAP_SUCCESS:
4412 if (debug) {
4413 log(LOG_DEBUG, "%s: chap success",
4414 ifp->if_xname);
4415 if (len > 4) {
4416 addlog(": ");
4417 sppp_print_string((char *)(h + 1), len - 4);
4418 }
4419 addlog("\n");
4420 }
4421
4422 if (h->ident != sp->scp[IDX_CHAP].rconfid) {
4423 if (debug) {
4424 log(LOG_DEBUG, "%s: %s id mismatch 0x%x != 0x%x\n",
4425 ifp->if_xname, chap.name,
4426 h->ident, sp->scp[IDX_CHAP].rconfid);
4427 }
4428 if_statinc(ifp, if_ierrors);
4429 break;
4430 }
4431
4432 if (sp->chap.digest_len == 0) {
4433 if (debug) {
4434 log(LOG_DEBUG,
4435 "%s: receive CHAP success without challenge\n",
4436 ifp->if_xname);
4437 }
4438 if_statinc(ifp, if_ierrors);
4439 break;
4440 }
4441
4442 x = splnet();
4443 sp->pp_auth_failures = 0;
4444 sp->pp_flags &= ~PP_NEEDAUTH;
4445 splx(x);
4446 memset(sp->chap.digest, 0, sizeof(sp->chap.digest));
4447 sp->chap.digest_len = 0;
4448
4449 if (!ISSET(sppp_auth_role(&chap, sp), SPPP_AUTH_SERV)) {
4450 /*
4451 * we are not authenticator for CHAP,
4452 * generate a dummy RCR+ event without CHAP_RESPONSE
4453 */
4454 sp->scp[IDX_CHAP].rcr_type = CONF_ACK;
4455 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_CHAP].work_rcr);
4456 }
4457 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_CHAP].work_rca);
4458 break;
4459
4460 case CHAP_FAILURE:
4461 if (h->ident != sp->scp[IDX_CHAP].rconfid) {
4462 if (debug) {
4463 log(LOG_DEBUG, "%s: %s id mismatch 0x%x != 0x%x\n",
4464 ifp->if_xname, chap.name,
4465 h->ident, sp->scp[IDX_CHAP].rconfid);
4466 }
4467 if_statinc(ifp, if_ierrors);
4468 break;
4469 }
4470
4471 if (sp->chap.digest_len == 0) {
4472 if (debug) {
4473 log(LOG_DEBUG,
4474 "%s: receive CHAP failure without challenge\n",
4475 ifp->if_xname);
4476 }
4477 if_statinc(ifp, if_ierrors);
4478 break;
4479 }
4480
4481 x = splnet();
4482 sp->pp_auth_failures++;
4483 splx(x);
4484 if (debug) {
4485 log(LOG_INFO, "%s: chap failure",
4486 ifp->if_xname);
4487 if (len > 4) {
4488 addlog(": ");
4489 sppp_print_string((char *)(h + 1), len - 4);
4490 }
4491 addlog("\n");
4492 } else
4493 log(LOG_INFO, "%s: chap failure\n",
4494 ifp->if_xname);
4495
4496 memset(sp->chap.digest, 0, sizeof(sp->chap.digest));
4497 sp->chap.digest_len = 0;
4498 /*
4499 * await LCP shutdown by authenticator,
4500 * so we don't have to enqueue sc->scp[IDX_CHAP].work_rcn
4501 */
4502 break;
4503
4504 /* response is my authproto */
4505 case CHAP_RESPONSE:
4506 if (sp->hisauth.name == NULL || sp->hisauth.secret == NULL) {
4507 /* can't do anything useful */
4508 printf("%s: chap response"
4509 " without his name and his secret being set\n",
4510 ifp->if_xname);
4511 break;
4512 }
4513 value = 1 + (u_char *)(h + 1);
4514 value_len = value[-1];
4515 name = value + value_len;
4516 name_len = len - value_len - 5;
4517 if (name_len < 0) {
4518 if (debug) {
4519 log(LOG_DEBUG,
4520 "%s: chap corrupted response "
4521 "<%s id=0x%x len=%d",
4522 ifp->if_xname,
4523 sppp_auth_type_name(PPP_CHAP, h->type),
4524 h->ident, ntohs(h->len));
4525 if (len > 4)
4526 sppp_print_bytes((u_char *)(h + 1),
4527 len - 4);
4528 addlog(">\n");
4529 }
4530 break;
4531 }
4532 if (h->ident != sp->scp[IDX_CHAP].confid) {
4533 if (debug)
4534 log(LOG_DEBUG,
4535 "%s: chap dropping response for old ID "
4536 "(got %d, expected %d)\n",
4537 ifp->if_xname,
4538 h->ident, sp->scp[IDX_CHAP].confid);
4539 break;
4540 } else {
4541 sp->scp[IDX_CHAP].rconfid = h->ident;
4542 }
4543
4544 if (sp->hisauth.name != NULL &&
4545 (name_len != sp->hisauth.name_len
4546 || memcmp(name, sp->hisauth.name, name_len) != 0)) {
4547 log(LOG_INFO, "%s: chap response, his name ",
4548 ifp->if_xname);
4549 sppp_print_string(name, name_len);
4550 addlog(" != expected ");
4551 sppp_print_string(sp->hisauth.name,
4552 sp->hisauth.name_len);
4553 addlog("\n");
4554
4555 /* generate RCR- event */
4556 sp->scp[IDX_CHAP].rcr_type = CONF_NAK;
4557 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_CHAP].work_rcr);
4558 break;
4559 }
4560
4561 if (debug) {
4562 log(LOG_DEBUG, "%s: chap input(%s) "
4563 "<%s id=0x%x len=%d name=",
4564 ifp->if_xname,
4565 sppp_state_name(sp->scp[IDX_CHAP].state),
4566 sppp_auth_type_name(PPP_CHAP, h->type),
4567 h->ident, ntohs(h->len));
4568 sppp_print_string((char *)name, name_len);
4569 addlog(" value-size=%d value=", value_len);
4570 sppp_print_bytes(value, value_len);
4571 addlog(">\n");
4572 }
4573
4574 if (value_len == sizeof(sp->chap.challenge) &&
4575 value_len == sizeof(sp->chap.digest)) {
4576 MD5Init(&ctx);
4577 MD5Update(&ctx, &h->ident, 1);
4578 MD5Update(&ctx, sp->hisauth.secret, sp->hisauth.secret_len);
4579 MD5Update(&ctx, sp->chap.challenge, sizeof(sp->chap.challenge));
4580 MD5Final(digest, &ctx);
4581
4582 if (memcmp(digest, value, value_len) == 0) {
4583 sp->scp[IDX_CHAP].rcr_type = CONF_ACK;
4584 if (!ISSET(sppp_auth_role(&chap, sp), SPPP_AUTH_PEER) ||
4585 sp->chap.rechallenging) {
4586 /* generate a dummy RCA event*/
4587 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_CHAP].work_rca);
4588 }
4589 } else {
4590 sp->scp[IDX_CHAP].rcr_type = CONF_NAK;
4591 }
4592 } else {
4593 if (debug)
4594 log(LOG_DEBUG,
4595 "%s: chap bad hash value length: "
4596 "%d bytes, should be %zu\n",
4597 ifp->if_xname, value_len,
4598 sizeof(sp->chap.challenge));
4599
4600 sp->scp[IDX_CHAP].rcr_type = CONF_NAK;
4601 }
4602
4603 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_CHAP].work_rcr);
4604 break;
4605
4606 default:
4607 /* Unknown CHAP packet type -- ignore. */
4608 if (debug) {
4609 log(LOG_DEBUG, "%s: chap unknown input(%s) "
4610 "<0x%x id=0x%xh len=%d",
4611 ifp->if_xname,
4612 sppp_state_name(sp->scp[IDX_CHAP].state),
4613 h->type, h->ident, ntohs(h->len));
4614 if (len > 4)
4615 sppp_print_bytes((u_char *)(h + 1), len - 4);
4616 addlog(">\n");
4617 }
4618 break;
4619
4620 }
4621
4622 SPPP_UNLOCK(sp);
4623 }
4624
4625 static void
4626 sppp_chap_init(struct sppp *sp)
4627 {
4628
4629 KASSERT(SPPP_WLOCKED(sp));
4630
4631 sp->scp[IDX_CHAP].state = STATE_INITIAL;
4632 sp->scp[IDX_CHAP].fail_counter = 0;
4633 sp->scp[IDX_CHAP].seq = 0;
4634 sp->scp[IDX_CHAP].rseq = 0;
4635 SPPP_WQ_SET(&sp->chap.work_challenge_rcvd,
4636 sppp_chap_rcv_challenge_event, &chap);
4637 sppp_cp_init(&chap, sp);
4638 }
4639
4640 static void
4641 sppp_chap_open(struct sppp *sp, void *xcp)
4642 {
4643
4644 KASSERT(SPPP_WLOCKED(sp));
4645
4646 memset(sp->chap.digest, 0, sizeof(sp->chap.digest));
4647 sp->chap.digest_len = 0;
4648 sp->chap.rechallenging = false;
4649 sp->chap.response_rcvd = false;
4650 sppp_open_event(sp, xcp);
4651 }
4652
4653 static void
4654 sppp_chap_tlu(struct sppp *sp)
4655 {
4656 STDDCL;
4657 int i, x;
4658
4659 KASSERT(SPPP_WLOCKED(sp));
4660
4661 i = 0;
4662 sp->scp[IDX_CHAP].rst_counter = sp->lcp.max_configure;
4663 x = splnet();
4664 sp->pp_auth_failures = 0;
4665 splx(x);
4666
4667 log(LOG_DEBUG, "%s: chap %s", ifp->if_xname,
4668 sp->pp_phase == SPPP_PHASE_NETWORK ? "reconfirmed" : "tlu");
4669
4670 /*
4671 * Some broken CHAP implementations (Conware CoNet, firmware
4672 * 4.0.?) don't want to re-authenticate their CHAP once the
4673 * initial challenge-response exchange has taken place.
4674 * Provide for an option to avoid rechallenges.
4675 */
4676 if (ISSET(sppp_auth_role(&chap, sp), SPPP_AUTH_SERV) &&
4677 (sp->hisauth.flags & SPPP_AUTHFLAG_NORECHALLENGE) == 0) {
4678 /*
4679 * Compute the re-challenge timeout. This will yield
4680 * a number between 300 and 810 seconds.
4681 */
4682 i = 300 + ((unsigned)(cprng_fast32() & 0xff00) >> 7);
4683 callout_schedule(&sp->scp[IDX_CHAP].ch, i * hz);
4684
4685 if (debug) {
4686 addlog(", next rechallenge in %d seconds", i);
4687 }
4688 }
4689
4690 addlog("\n");
4691
4692 /*
4693 * If we are already in phase network, we are done here. This
4694 * is the case if this is a dummy tlu event after a re-challenge.
4695 */
4696 if (sp->pp_phase != SPPP_PHASE_NETWORK)
4697 sppp_phase_network(sp);
4698 }
4699
4700 static void
4701 sppp_chap_scr(struct sppp *sp)
4702 {
4703 uint32_t *ch;
4704 u_char clen, dsize;
4705 int role;
4706
4707 KASSERT(SPPP_WLOCKED(sp));
4708
4709 role = sppp_auth_role(&chap, sp);
4710
4711 if (ISSET(role, SPPP_AUTH_SERV) &&
4712 !sp->chap.response_rcvd) {
4713 /* we are authenticator for CHAP, send challenge */
4714 ch = (uint32_t *)sp->chap.challenge;
4715 clen = sizeof(sp->chap.challenge);
4716 /* Compute random challenge. */
4717 cprng_strong(kern_cprng, ch, clen, 0);
4718
4719 sp->scp[IDX_CHAP].confid = ++sp->scp[IDX_CHAP].seq;
4720 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->scp[IDX_CHAP].confid,
4721 sizeof(clen), (const char *)&clen,
4722 sizeof(sp->chap.challenge), sp->chap.challenge,
4723 0);
4724 }
4725
4726 if (ISSET(role, SPPP_AUTH_PEER) &&
4727 sp->chap.digest_len > 0) {
4728 /* we are peer for CHAP, send response */
4729 dsize = sp->chap.digest_len;
4730
4731 sppp_auth_send(&chap, sp, CHAP_RESPONSE, sp->scp[IDX_CHAP].rconfid,
4732 sizeof(dsize), (const char *)&dsize,
4733 sp->chap.digest_len, sp->chap.digest,
4734 sp->myauth.name_len, sp->myauth.name, 0);
4735 }
4736 }
4737
4738 static void
4739 sppp_chap_rcv_challenge_event(struct sppp *sp, void *xcp)
4740 {
4741 const struct cp *cp = xcp;
4742
4743 sp->chap.rechallenging = false;
4744
4745 switch (sp->scp[IDX_CHAP].state) {
4746 case STATE_REQ_SENT:
4747 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
4748 cp->scr(sp);
4749 break;
4750 case STATE_OPENED:
4751 sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
4752 cp->scr(sp);
4753 break;
4754 }
4755 }
4756
4757 /*
4758 *--------------------------------------------------------------------------*
4759 * *
4760 * The PAP implementation. *
4761 * *
4762 *--------------------------------------------------------------------------*
4763 */
4764 /*
4765 * PAP uses following actions and events.
4766 * Actions:
4767 * - scr: send PAP_REQ
4768 * - sca: send PAP_ACK
4769 * - scn: send PAP_NAK
4770 * Events:
4771 * - RCR+: receive PAP_REQ containing correct username and password
4772 * - RCR-: receive PAP_REQ containing wrong username and password
4773 * - RCA: receive PAP_ACK
4774 * - RCN: (this event is unused)
4775 * - TO+: re-send PAP_REQ
4776 * - TO-: this layer finish
4777 */
4778
4779 /*
4780 * Handle incoming PAP packets. */
4781 static void
4782 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4783 {
4784 STDDCL;
4785 struct lcp_header *h;
4786 int len, x;
4787 char *name, *secret;
4788 int name_len, secret_len;
4789
4790 /*
4791 * Malicious input might leave this uninitialized, so
4792 * init to an impossible value.
4793 */
4794 secret_len = -1;
4795
4796 len = m->m_pkthdr.len;
4797 if (len < 5) {
4798 if (debug)
4799 log(LOG_DEBUG,
4800 "%s: pap invalid packet length: %d bytes\n",
4801 ifp->if_xname, len);
4802 return;
4803 }
4804 h = mtod(m, struct lcp_header *);
4805 if (len > ntohs(h->len))
4806 len = ntohs(h->len);
4807
4808 SPPP_LOCK(sp, RW_WRITER);
4809
4810 switch (h->type) {
4811 /* PAP request is my authproto */
4812 case PAP_REQ:
4813 if (sp->hisauth.name == NULL || sp->hisauth.secret == NULL) {
4814 /* can't do anything useful */
4815 printf("%s: pap request"
4816 " without his name and his secret being set\n",
4817 ifp->if_xname);
4818 break;
4819 }
4820 name = 1 + (u_char *)(h + 1);
4821 name_len = name[-1];
4822 secret = name + name_len + 1;
4823 if (name_len > len - 6 ||
4824 (secret_len = secret[-1]) > len - 6 - name_len) {
4825 if (debug) {
4826 log(LOG_DEBUG, "%s: pap corrupted input "
4827 "<%s id=0x%x len=%d",
4828 ifp->if_xname,
4829 sppp_auth_type_name(PPP_PAP, h->type),
4830 h->ident, ntohs(h->len));
4831 if (len > 4)
4832 sppp_print_bytes((u_char *)(h + 1),
4833 len - 4);
4834 addlog(">\n");
4835 }
4836 break;
4837 }
4838 if (debug) {
4839 log(LOG_DEBUG, "%s: pap input(%s) "
4840 "<%s id=0x%x len=%d name=",
4841 ifp->if_xname,
4842 sppp_state_name(sp->scp[IDX_PAP].state),
4843 sppp_auth_type_name(PPP_PAP, h->type),
4844 h->ident, ntohs(h->len));
4845 sppp_print_string((char *)name, name_len);
4846 addlog(" secret=");
4847 sppp_print_string((char *)secret, secret_len);
4848 addlog(">\n");
4849 }
4850
4851 sp->scp[IDX_PAP].rconfid = h->ident;
4852
4853 if (name_len == sp->hisauth.name_len &&
4854 memcmp(name, sp->hisauth.name, name_len) == 0 &&
4855 secret_len == sp->hisauth.secret_len &&
4856 memcmp(secret, sp->hisauth.secret, secret_len) == 0) {
4857 sp->scp[IDX_PAP].rcr_type = CONF_ACK;
4858 if (!ISSET(sppp_auth_role(&pap, sp), SPPP_AUTH_PEER)) {
4859 /* generate a dummy RCA event*/
4860 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_PAP].work_rca);
4861 }
4862 } else {
4863 sp->scp[IDX_PAP].rcr_type = CONF_NAK;
4864 }
4865
4866 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_PAP].work_rcr);
4867 break;
4868
4869 /* ack and nak are his authproto */
4870 case PAP_ACK:
4871 if (debug) {
4872 log(LOG_DEBUG, "%s: pap success",
4873 ifp->if_xname);
4874 name = 1 + (u_char *)(h + 1);
4875 name_len = name[-1];
4876 if (len > 5 && name_len < len+4) {
4877 addlog(": ");
4878 sppp_print_string(name, name_len);
4879 }
4880 addlog("\n");
4881 }
4882
4883 if (h->ident != sp->scp[IDX_PAP].confid) {
4884 if (debug) {
4885 log(LOG_DEBUG, "%s: %s id mismatch 0x%x != 0x%x\n",
4886 ifp->if_xname, pap.name,
4887 h->ident, sp->scp[IDX_PAP].rconfid);
4888 }
4889 if_statinc(ifp, if_ierrors);
4890 break;
4891 }
4892
4893 x = splnet();
4894 sp->pp_auth_failures = 0;
4895 sp->pp_flags &= ~PP_NEEDAUTH;
4896 splx(x);
4897
4898 /* we are not authenticator, generate a dummy RCR+ event */
4899 if (!ISSET(sppp_auth_role(&pap, sp), SPPP_AUTH_SERV)) {
4900 sp->scp[IDX_PAP].rcr_type = CONF_ACK;
4901 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_PAP].work_rcr);
4902 }
4903
4904 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_PAP].work_rca);
4905 break;
4906
4907 case PAP_NAK:
4908 if (debug) {
4909 log(LOG_INFO, "%s: pap failure",
4910 ifp->if_xname);
4911 name = 1 + (u_char *)(h + 1);
4912 name_len = name[-1];
4913 if (len > 5 && name_len < len+4) {
4914 addlog(": ");
4915 sppp_print_string(name, name_len);
4916 }
4917 addlog("\n");
4918 } else
4919 log(LOG_INFO, "%s: pap failure\n",
4920 ifp->if_xname);
4921
4922 if (h->ident != sp->scp[IDX_PAP].confid) {
4923 if (debug) {
4924 log(LOG_DEBUG, "%s: %s id mismatch 0x%x != 0x%x\n",
4925 ifp->if_xname, pap.name,
4926 h->ident, sp->scp[IDX_PAP].rconfid);
4927 }
4928 if_statinc(ifp, if_ierrors);
4929 break;
4930 }
4931
4932 sp->pp_auth_failures++;
4933 /*
4934 * await LCP shutdown by authenticator,
4935 * so we don't have to enqueue sc->scp[IDX_PAP].work_rcn
4936 */
4937 break;
4938
4939 default:
4940 /* Unknown PAP packet type -- ignore. */
4941 if (debug) {
4942 log(LOG_DEBUG, "%s: pap corrupted input "
4943 "<0x%x id=0x%x len=%d",
4944 ifp->if_xname,
4945 h->type, h->ident, ntohs(h->len));
4946 if (len > 4)
4947 sppp_print_bytes((u_char *)(h + 1), len - 4);
4948 addlog(">\n");
4949 }
4950 break;
4951 }
4952
4953 SPPP_UNLOCK(sp);
4954 }
4955
4956 static void
4957 sppp_pap_init(struct sppp *sp)
4958 {
4959
4960 KASSERT(SPPP_WLOCKED(sp));
4961
4962 sp->scp[IDX_PAP].state = STATE_INITIAL;
4963 sp->scp[IDX_PAP].fail_counter = 0;
4964 sp->scp[IDX_PAP].seq = 0;
4965 sp->scp[IDX_PAP].rseq = 0;
4966 sppp_cp_init(&pap, sp);
4967 }
4968
4969 static void
4970 sppp_pap_tlu(struct sppp *sp)
4971 {
4972 STDDCL;
4973 int x;
4974
4975 sp->scp[IDX_PAP].rst_counter = sp->lcp.max_configure;
4976
4977 if (debug)
4978 log(LOG_DEBUG, "%s: %s tlu\n",
4979 ifp->if_xname, pap.name);
4980
4981 x = splnet();
4982 sp->pp_auth_failures = 0;
4983 splx(x);
4984
4985 if (sp->pp_phase < SPPP_PHASE_NETWORK)
4986 sppp_phase_network(sp);
4987 }
4988
4989 static void
4990 sppp_pap_scr(struct sppp *sp)
4991 {
4992 u_char idlen, pwdlen;
4993
4994 KASSERT(SPPP_WLOCKED(sp));
4995
4996 if (ISSET(sppp_auth_role(&pap, sp), SPPP_AUTH_PEER) &&
4997 sp->scp[IDX_PAP].state != STATE_ACK_RCVD) {
4998 if (sp->myauth.secret == NULL || sp->myauth.name == NULL) {
4999 log(LOG_DEBUG, "%s: couldn't send PAP_REQ "
5000 "because of no name or no secret\n",
5001 sp->pp_if.if_xname);
5002 } else {
5003 sp->scp[IDX_PAP].confid = ++sp->scp[IDX_PAP].seq;
5004 pwdlen = sp->myauth.secret_len;
5005 idlen = sp->myauth.name_len;
5006
5007 sppp_auth_send(&pap, sp, PAP_REQ, sp->scp[IDX_PAP].confid,
5008 sizeof idlen, (const char *)&idlen,
5009 idlen, sp->myauth.name,
5010 sizeof pwdlen, (const char *)&pwdlen,
5011 pwdlen, sp->myauth.secret,
5012 0);
5013 }
5014 }
5015 }
5016
5017 /*
5018 * Random miscellaneous functions.
5019 */
5020
5021 /*
5022 * Send a PAP or CHAP proto packet.
5023 *
5024 * Varadic function, each of the elements for the ellipsis is of type
5025 * ``size_t mlen, const u_char *msg''. Processing will stop iff
5026 * mlen == 0.
5027 * NOTE: never declare variadic functions with types subject to type
5028 * promotion (i.e. u_char). This is asking for big trouble depending
5029 * on the architecture you are on...
5030 */
5031
5032 static void
5033 sppp_auth_send(const struct cp *cp, struct sppp *sp,
5034 unsigned int type, unsigned int id,
5035 ...)
5036 {
5037 STDDCL;
5038 struct lcp_header *lh;
5039 struct mbuf *m;
5040 u_char *p;
5041 int len;
5042 size_t pkthdrlen;
5043 unsigned int mlen;
5044 const char *msg;
5045 va_list ap;
5046
5047 KASSERT(SPPP_WLOCKED(sp));
5048
5049 MGETHDR(m, M_DONTWAIT, MT_DATA);
5050 if (! m)
5051 return;
5052 m_reset_rcvif(m);
5053
5054 if (sp->pp_flags & PP_NOFRAMING) {
5055 *mtod(m, uint16_t *) = htons(cp->proto);
5056 pkthdrlen = 2;
5057 lh = (struct lcp_header *)(mtod(m, uint8_t *)+2);
5058 } else {
5059 struct ppp_header *h;
5060 h = mtod(m, struct ppp_header *);
5061 h->address = PPP_ALLSTATIONS; /* broadcast address */
5062 h->control = PPP_UI; /* Unnumbered Info */
5063 h->protocol = htons(cp->proto);
5064 pkthdrlen = PPP_HEADER_LEN;
5065
5066 lh = (struct lcp_header *)(h + 1);
5067 }
5068
5069 lh->type = type;
5070 lh->ident = id;
5071 p = (u_char *)(lh + 1);
5072
5073 va_start(ap, id);
5074 len = 0;
5075
5076 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
5077 msg = va_arg(ap, const char *);
5078 len += mlen;
5079 if (len > MHLEN - pkthdrlen - LCP_HEADER_LEN) {
5080 va_end(ap);
5081 m_freem(m);
5082 return;
5083 }
5084
5085 memcpy(p, msg, mlen);
5086 p += mlen;
5087 }
5088 va_end(ap);
5089
5090 m->m_pkthdr.len = m->m_len = pkthdrlen + LCP_HEADER_LEN + len;
5091 lh->len = htons(LCP_HEADER_LEN + len);
5092
5093 if (debug) {
5094 log(LOG_DEBUG, "%s: %s output <%s id=0x%x len=%d",
5095 ifp->if_xname, cp->name,
5096 sppp_auth_type_name(cp->proto, lh->type),
5097 lh->ident, ntohs(lh->len));
5098 if (len)
5099 sppp_print_bytes((u_char *)(lh + 1), len);
5100 addlog(">\n");
5101 }
5102 if (IF_QFULL(&sp->pp_cpq)) {
5103 IF_DROP(&sp->pp_fastq);
5104 IF_DROP(&ifp->if_snd);
5105 m_freem(m);
5106 if_statinc(ifp, if_oerrors);
5107 return;
5108 }
5109
5110 if_statadd(ifp, if_obytes, m->m_pkthdr.len + sp->pp_framebytes);
5111 IF_ENQUEUE(&sp->pp_cpq, m);
5112
5113 if (! (ifp->if_flags & IFF_OACTIVE)) {
5114 SPPP_UNLOCK(sp);
5115 if_start_lock(ifp);
5116 SPPP_LOCK(sp, RW_WRITER);
5117 }
5118 }
5119
5120 static int
5121 sppp_auth_role(const struct cp *cp, struct sppp *sp)
5122 {
5123 int role;
5124
5125 role = SPPP_AUTH_NOROLE;
5126
5127 if (sp->hisauth.proto == cp->proto &&
5128 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0)
5129 SET(role, SPPP_AUTH_SERV);
5130
5131 if (sp->myauth.proto == cp->proto)
5132 SET(role, SPPP_AUTH_PEER);
5133
5134 return role;
5135 }
5136
5137 static void
5138 sppp_auth_to_event(struct sppp *sp, void *xcp)
5139 {
5140 const struct cp *cp = xcp;
5141 bool override;
5142 int state;
5143 STDDCL;
5144
5145 KASSERT(SPPP_WLOCKED(sp));
5146
5147 override = false;
5148 state = sp->scp[cp->protoidx].state;
5149
5150 if (sp->scp[cp->protoidx].rst_counter > 0) {
5151 /* override TO+ event */
5152 switch (state) {
5153 case STATE_OPENED:
5154 if ((sp->hisauth.flags & SPPP_AUTHFLAG_NORECHALLENGE) == 0) {
5155 override = true;
5156 sp->chap.rechallenging = true;
5157 sp->chap.response_rcvd = false;
5158 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
5159 cp->scr(sp);
5160 }
5161 break;
5162
5163 case STATE_ACK_RCVD:
5164 override = true;
5165 cp->scr(sp);
5166 callout_schedule(&sp->scp[cp->protoidx].ch, sp->lcp.timeout);
5167 break;
5168 }
5169 }
5170
5171 if (override) {
5172 if (debug)
5173 log(LOG_DEBUG, "%s: %s TO(%s) rst_counter = %d\n",
5174 ifp->if_xname, cp->name,
5175 sppp_state_name(state),
5176 sp->scp[cp->protoidx].rst_counter);
5177 sp->scp[cp->protoidx].rst_counter--;
5178 } else {
5179 sppp_to_event(sp, xcp);
5180 }
5181 }
5182
5183 static void
5184 sppp_auth_sca_scn(const struct cp *cp, struct sppp *sp)
5185 {
5186 static const char *succmsg = "Welcome!";
5187 static const char *failmsg = "Failed...";
5188 const char *msg;
5189 u_char type, rconfid, mlen;
5190
5191 KASSERT(SPPP_WLOCKED(sp));
5192
5193 if (!ISSET(sppp_auth_role(cp, sp), SPPP_AUTH_SERV))
5194 return;
5195
5196 rconfid = sp->scp[cp->protoidx].rconfid;
5197
5198 if (sp->scp[cp->protoidx].rcr_type == CONF_ACK) {
5199 type = cp->proto == PPP_CHAP ? CHAP_SUCCESS : PAP_ACK;
5200 msg = succmsg;
5201 mlen = sizeof(succmsg) - 1;
5202
5203 sp->pp_auth_failures = 0;
5204 } else {
5205 type = cp->proto == PPP_CHAP ? CHAP_FAILURE : PAP_NAK;
5206 msg = failmsg;
5207 mlen = sizeof(failmsg) - 1;
5208
5209 /* shutdown LCP if auth failed */
5210 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_close);
5211 sp->pp_auth_failures++;
5212 }
5213
5214 sppp_auth_send(cp, sp, type, rconfid,
5215 mlen, (const u_char *)msg, 0);
5216 }
5217
5218 /*
5219 * Send keepalive packets, every 10 seconds.
5220 */
5221 static void
5222 sppp_keepalive(void *dummy)
5223 {
5224 struct sppp *sp;
5225 int s;
5226 time_t now;
5227
5228 SPPPQ_LOCK();
5229
5230 s = splnet();
5231 now = time_uptime;
5232 for (sp=spppq; sp; sp=sp->pp_next) {
5233 struct ifnet *ifp = NULL;
5234
5235 SPPP_LOCK(sp, RW_WRITER);
5236 ifp = &sp->pp_if;
5237
5238 /* check idle timeout */
5239 if ((sp->pp_idle_timeout != 0) && (ifp->if_flags & IFF_RUNNING)
5240 && (sp->pp_phase == SPPP_PHASE_NETWORK)) {
5241 /* idle timeout is enabled for this interface */
5242 if ((now-sp->pp_last_activity) >= sp->pp_idle_timeout) {
5243 if (ifp->if_flags & IFF_DEBUG)
5244 printf("%s: no activity for %lu seconds\n",
5245 sp->pp_if.if_xname,
5246 (unsigned long)(now-sp->pp_last_activity));
5247 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_close);
5248 SPPP_UNLOCK(sp);
5249 continue;
5250 }
5251 }
5252
5253 /* Keepalive mode disabled or channel down? */
5254 if (! (sp->pp_flags & PP_KEEPALIVE) ||
5255 ! (ifp->if_flags & IFF_RUNNING)) {
5256 SPPP_UNLOCK(sp);
5257 continue;
5258 }
5259
5260
5261 /* No keepalive in PPP mode if LCP not opened yet. */
5262 if (! (sp->pp_flags & PP_CISCO) &&
5263 sp->pp_phase < SPPP_PHASE_AUTHENTICATE) {
5264 SPPP_UNLOCK(sp);
5265 continue;
5266 }
5267
5268 /* No echo reply, but maybe user data passed through? */
5269 if ((now - sp->pp_last_receive) < sp->pp_max_noreceive) {
5270 sp->pp_alivecnt = 0;
5271 SPPP_UNLOCK(sp);
5272 continue;
5273 }
5274
5275 if (sp->pp_alivecnt >= sp->pp_maxalive) {
5276 /* No keepalive packets got. Stop the interface. */
5277 sppp_wq_add(sp->wq_cp, &sp->work_ifdown);
5278
5279 if (! (sp->pp_flags & PP_CISCO)) {
5280 printf("%s: LCP keepalive timed out, going to restart the connection\n",
5281 ifp->if_xname);
5282 sp->pp_alivecnt = 0;
5283
5284 /* we are down, close all open protocols */
5285 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_close);
5286
5287 /* And now prepare LCP to reestablish the link, if configured to do so. */
5288 sp->lcp.reestablish = true;
5289
5290 SPPP_UNLOCK(sp);
5291 continue;
5292 }
5293 }
5294 if (sp->pp_alivecnt < sp->pp_maxalive)
5295 ++sp->pp_alivecnt;
5296 if (sp->pp_flags & PP_CISCO)
5297 sppp_cisco_send(sp, CISCO_KEEPALIVE_REQ,
5298 ++sp->scp[IDX_LCP].seq, sp->scp[IDX_LCP].rseq);
5299 else if (sp->pp_phase >= SPPP_PHASE_AUTHENTICATE) {
5300 int32_t nmagic = htonl(sp->lcp.magic);
5301 sp->lcp.echoid = ++sp->scp[IDX_LCP].seq;
5302 sppp_cp_send(sp, PPP_LCP, ECHO_REQ,
5303 sp->lcp.echoid, 4, &nmagic);
5304 }
5305
5306 SPPP_UNLOCK(sp);
5307 }
5308 splx(s);
5309 callout_reset(&keepalive_ch, hz * LCP_KEEPALIVE_INTERVAL, sppp_keepalive, NULL);
5310
5311 SPPPQ_UNLOCK();
5312 }
5313
5314 #ifdef INET
5315 /*
5316 * Get both IP addresses.
5317 */
5318 static void
5319 sppp_get_ip_addrs(struct sppp *sp, uint32_t *src, uint32_t *dst, uint32_t *srcmask)
5320 {
5321 struct ifnet *ifp = &sp->pp_if;
5322 struct ifaddr *ifa;
5323 struct sockaddr_in *si, *sm;
5324 uint32_t ssrc, ddst;
5325 int s;
5326 struct psref psref;
5327
5328 sm = NULL;
5329 ssrc = ddst = 0;
5330 /*
5331 * Pick the first AF_INET address from the list,
5332 * aliases don't make any sense on a p2p link anyway.
5333 */
5334 si = 0;
5335 s = pserialize_read_enter();
5336 IFADDR_READER_FOREACH(ifa, ifp) {
5337 if (ifa->ifa_addr->sa_family == AF_INET) {
5338 si = (struct sockaddr_in *)ifa->ifa_addr;
5339 sm = (struct sockaddr_in *)ifa->ifa_netmask;
5340 if (si) {
5341 ifa_acquire(ifa, &psref);
5342 break;
5343 }
5344 }
5345 }
5346 pserialize_read_exit(s);
5347 if (ifa) {
5348 if (si && si->sin_addr.s_addr) {
5349 ssrc = si->sin_addr.s_addr;
5350 if (srcmask)
5351 *srcmask = ntohl(sm->sin_addr.s_addr);
5352 }
5353
5354 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
5355 if (si && si->sin_addr.s_addr)
5356 ddst = si->sin_addr.s_addr;
5357 ifa_release(ifa, &psref);
5358 }
5359
5360 if (dst) *dst = ntohl(ddst);
5361 if (src) *src = ntohl(ssrc);
5362 }
5363
5364 /*
5365 * Set IP addresses. Must be called at splnet.
5366 * If an address is 0, leave it the way it is.
5367 */
5368 static void
5369 sppp_set_ip_addrs_work(struct work *wk, struct sppp *sp)
5370 {
5371 STDDCL;
5372 struct ifaddr *ifa;
5373 struct sockaddr_in *si, *dest;
5374 uint32_t myaddr = 0, hisaddr = 0;
5375 int s;
5376
5377 IFNET_LOCK(ifp);
5378
5379 /*
5380 * Pick the first AF_INET address from the list,
5381 * aliases don't make any sense on a p2p link anyway.
5382 */
5383 si = dest = NULL;
5384 s = pserialize_read_enter();
5385 IFADDR_READER_FOREACH(ifa, ifp) {
5386 if (ifa->ifa_addr->sa_family == AF_INET) {
5387 si = (struct sockaddr_in *)ifa->ifa_addr;
5388 dest = (struct sockaddr_in *)ifa->ifa_dstaddr;
5389 break;
5390 }
5391 }
5392 pserialize_read_exit(s);
5393
5394 if ((sp->ipcp.flags & IPCP_MYADDR_DYN) && (sp->ipcp.flags & IPCP_MYADDR_SEEN))
5395 myaddr = sp->ipcp.req_myaddr;
5396 else if (si != NULL)
5397 myaddr = ntohl(si->sin_addr.s_addr);
5398
5399 if ((sp->ipcp.flags & IPCP_HISADDR_DYN) && (sp->ipcp.flags & IPCP_HISADDR_SEEN))
5400 hisaddr = sp->ipcp.req_hisaddr;
5401 else if (dest != NULL)
5402 hisaddr = ntohl(dest->sin_addr.s_addr);
5403
5404 if (si != NULL && dest != NULL) {
5405 int error;
5406 struct sockaddr_in new_sin = *si;
5407 struct sockaddr_in new_dst = *dest;
5408
5409 if (myaddr != 0)
5410 new_sin.sin_addr.s_addr = htonl(myaddr);
5411 if (hisaddr != 0) {
5412 new_dst.sin_addr.s_addr = htonl(hisaddr);
5413 if (new_dst.sin_addr.s_addr != dest->sin_addr.s_addr)
5414 sp->ipcp.saved_hisaddr = dest->sin_addr.s_addr;
5415 }
5416
5417 in_addrhash_remove(ifatoia(ifa));
5418
5419 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, &new_dst, 0);
5420
5421 in_addrhash_insert(ifatoia(ifa));
5422
5423 if (debug && error)
5424 {
5425 log(LOG_DEBUG, "%s: %s: in_ifinit failed, error=%d\n",
5426 ifp->if_xname, __func__, error);
5427 }
5428 if (!error) {
5429 pfil_run_addrhooks(if_pfil, SIOCAIFADDR, ifa);
5430 }
5431 }
5432
5433 if (ifp->if_mtu > sp->lcp.their_mru) {
5434 sp->pp_saved_mtu = ifp->if_mtu;
5435 ifp->if_mtu = sp->lcp.their_mru;
5436 if (debug)
5437 log(LOG_DEBUG,
5438 "%s: setting MTU to %" PRIu64 " bytes\n",
5439 ifp->if_xname, ifp->if_mtu);
5440 }
5441
5442 IFNET_UNLOCK(ifp);
5443
5444 sppp_notify_con(sp);
5445 }
5446
5447 static void
5448 sppp_set_ip_addrs(struct sppp *sp)
5449 {
5450 struct ifnet *ifp = &sp->pp_if;
5451
5452 if (!pcq_put(sp->ipcp.update_addrs_q, (void *)IPCP_SET_ADDRS)) {
5453 log(LOG_WARNING, "%s: cannot enqueued, ignore sppp_clear_ip_addrs\n",
5454 ifp->if_xname);
5455 return;
5456 }
5457
5458 if (atomic_swap_uint(&sp->ipcp.update_addrs_enqueued, 1) == 1)
5459 return;
5460
5461 workqueue_enqueue(sp->ipcp.update_addrs_wq, &sp->ipcp.update_addrs_wk, NULL);
5462 }
5463
5464 /*
5465 * Clear IP addresses. Must be called at splnet.
5466 */
5467 static void
5468 sppp_clear_ip_addrs_work(struct work *wk, struct sppp *sp)
5469 {
5470 STDDCL;
5471 struct ifaddr *ifa;
5472 struct sockaddr_in *si, *dest;
5473 int s;
5474
5475 IFNET_LOCK(ifp);
5476
5477 /*
5478 * Pick the first AF_INET address from the list,
5479 * aliases don't make any sense on a p2p link anyway.
5480 */
5481 si = dest = NULL;
5482 s = pserialize_read_enter();
5483 IFADDR_READER_FOREACH(ifa, ifp) {
5484 if (ifa->ifa_addr->sa_family == AF_INET) {
5485 si = (struct sockaddr_in *)ifa->ifa_addr;
5486 dest = (struct sockaddr_in *)ifa->ifa_dstaddr;
5487 break;
5488 }
5489 }
5490 pserialize_read_exit(s);
5491
5492 if (si != NULL) {
5493 struct sockaddr_in new_sin = *si;
5494 struct sockaddr_in new_dst = *dest;
5495 int error;
5496
5497 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
5498 new_sin.sin_addr.s_addr = 0;
5499 if (sp->ipcp.flags & IPCP_HISADDR_DYN)
5500 new_dst.sin_addr.s_addr = sp->ipcp.saved_hisaddr;
5501
5502 in_addrhash_remove(ifatoia(ifa));
5503
5504 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, &new_dst, 0);
5505
5506 in_addrhash_insert(ifatoia(ifa));
5507
5508 if (debug && error)
5509 {
5510 log(LOG_DEBUG, "%s: %s: in_ifinit failed, error=%d\n",
5511 ifp->if_xname, __func__, error);
5512 }
5513 if (!error) {
5514 pfil_run_addrhooks(if_pfil, SIOCAIFADDR, ifa);
5515 }
5516 }
5517
5518 if (sp->pp_saved_mtu > 0) {
5519 ifp->if_mtu = sp->pp_saved_mtu;
5520 sp->pp_saved_mtu = 0;
5521 if (debug)
5522 log(LOG_DEBUG,
5523 "%s: resetting MTU to %" PRIu64 " bytes\n",
5524 ifp->if_xname, ifp->if_mtu);
5525 }
5526
5527 IFNET_UNLOCK(ifp);
5528 }
5529
5530 static void
5531 sppp_clear_ip_addrs(struct sppp *sp)
5532 {
5533 struct ifnet *ifp = &sp->pp_if;
5534
5535 if (!pcq_put(sp->ipcp.update_addrs_q, (void *)IPCP_CLEAR_ADDRS)) {
5536 log(LOG_WARNING, "%s: cannot enqueued, ignore sppp_clear_ip_addrs\n",
5537 ifp->if_xname);
5538 return;
5539 }
5540
5541 if (atomic_swap_uint(&sp->ipcp.update_addrs_enqueued, 1) == 1)
5542 return;
5543
5544 workqueue_enqueue(sp->ipcp.update_addrs_wq, &sp->ipcp.update_addrs_wk, NULL);
5545 }
5546
5547 static void
5548 sppp_update_ip_addrs_work(struct work *wk, void *arg)
5549 {
5550 struct sppp *sp = arg;
5551 void *work;
5552
5553 atomic_swap_uint(&sp->ipcp.update_addrs_enqueued, 0);
5554
5555 while ((work = pcq_get(sp->ipcp.update_addrs_q)) != NULL) {
5556 int update = (intptr_t)work;
5557
5558 if (update == IPCP_SET_ADDRS)
5559 sppp_set_ip_addrs_work(wk, sp);
5560 else if (update == IPCP_CLEAR_ADDRS)
5561 sppp_clear_ip_addrs_work(wk, sp);
5562 }
5563 }
5564 #endif
5565
5566 #ifdef INET6
5567 /*
5568 * Get both IPv6 addresses.
5569 */
5570 static void
5571 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
5572 struct in6_addr *srcmask)
5573 {
5574 struct ifnet *ifp = &sp->pp_if;
5575 struct ifaddr *ifa;
5576 struct sockaddr_in6 *si, *sm;
5577 struct in6_addr ssrc, ddst;
5578 int s;
5579 struct psref psref;
5580
5581 sm = NULL;
5582 memset(&ssrc, 0, sizeof(ssrc));
5583 memset(&ddst, 0, sizeof(ddst));
5584 /*
5585 * Pick the first link-local AF_INET6 address from the list,
5586 * aliases don't make any sense on a p2p link anyway.
5587 */
5588 si = 0;
5589 s = pserialize_read_enter();
5590 IFADDR_READER_FOREACH(ifa, ifp) {
5591 if (ifa->ifa_addr->sa_family == AF_INET6) {
5592 si = (struct sockaddr_in6 *)ifa->ifa_addr;
5593 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
5594 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr)) {
5595 ifa_acquire(ifa, &psref);
5596 break;
5597 }
5598 }
5599 }
5600 pserialize_read_exit(s);
5601
5602 if (ifa) {
5603 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
5604 memcpy(&ssrc, &si->sin6_addr, sizeof(ssrc));
5605 if (srcmask) {
5606 memcpy(srcmask, &sm->sin6_addr,
5607 sizeof(*srcmask));
5608 }
5609 }
5610
5611 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
5612 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
5613 memcpy(&ddst, &si->sin6_addr, sizeof(ddst));
5614 ifa_release(ifa, &psref);
5615 }
5616
5617 if (dst)
5618 memcpy(dst, &ddst, sizeof(*dst));
5619 if (src)
5620 memcpy(src, &ssrc, sizeof(*src));
5621 }
5622
5623 #ifdef IPV6CP_MYIFID_DYN
5624 /*
5625 * Generate random ifid.
5626 */
5627 static void
5628 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
5629 {
5630 /* TBD */
5631 }
5632
5633 /*
5634 * Set my IPv6 address. Must be called at splnet.
5635 */
5636 static void
5637 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
5638 {
5639 STDDCL;
5640 struct ifaddr *ifa;
5641 struct sockaddr_in6 *sin6;
5642 int s;
5643 struct psref psref;
5644
5645 IFNET_LOCK(ifp);
5646
5647 /*
5648 * Pick the first link-local AF_INET6 address from the list,
5649 * aliases don't make any sense on a p2p link anyway.
5650 */
5651
5652 sin6 = NULL;
5653 s = pserialize_read_enter();
5654 IFADDR_READER_FOREACH(ifa, ifp)
5655 {
5656 if (ifa->ifa_addr->sa_family == AF_INET6)
5657 {
5658 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
5659 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5660 ifa_acquire(ifa, &psref);
5661 break;
5662 }
5663 }
5664 }
5665 pserialize_read_exit(s);
5666
5667 if (ifa && sin6)
5668 {
5669 int error;
5670 struct sockaddr_in6 new_sin6 = *sin6;
5671
5672 memcpy(&new_sin6.sin6_addr, src, sizeof(new_sin6.sin6_addr));
5673 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5674 if (debug && error)
5675 {
5676 log(LOG_DEBUG, "%s: %s: in6_ifinit failed, error=%d\n",
5677 ifp->if_xname, __func__, error);
5678 }
5679 if (!error) {
5680 pfil_run_addrhooks(if_pfil, SIOCAIFADDR_IN6, ifa);
5681 }
5682 ifa_release(ifa, &psref);
5683 }
5684
5685 IFNET_UNLOCK(ifp);
5686 }
5687 #endif
5688
5689 /*
5690 * Suggest a candidate address to be used by peer.
5691 */
5692 static void
5693 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5694 {
5695 struct in6_addr myaddr;
5696 struct timeval tv;
5697
5698 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5699
5700 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5701 microtime(&tv);
5702 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5703 myaddr.s6_addr[14] ^= 0xff;
5704 myaddr.s6_addr[15] ^= 0xff;
5705 } else {
5706 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5707 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5708 }
5709 if (suggest)
5710 memcpy(suggest, &myaddr, sizeof(myaddr));
5711 }
5712 #endif /*INET6*/
5713
5714 /*
5715 * Process ioctl requests specific to the PPP interface.
5716 * Permissions have already been checked.
5717 */
5718 static int
5719 sppp_params(struct sppp *sp, u_long cmd, void *data)
5720 {
5721 switch (cmd) {
5722 case SPPPGETAUTHCFG:
5723 {
5724 struct spppauthcfg *cfg = (struct spppauthcfg *)data;
5725 int error;
5726 size_t len;
5727
5728 SPPP_LOCK(sp, RW_READER);
5729
5730 cfg->myauthflags = sp->myauth.flags;
5731 cfg->hisauthflags = sp->hisauth.flags;
5732 strlcpy(cfg->ifname, sp->pp_if.if_xname, sizeof(cfg->ifname));
5733 cfg->hisauth = 0;
5734 if (sp->hisauth.proto)
5735 cfg->hisauth = (sp->hisauth.proto == PPP_PAP) ? SPPP_AUTHPROTO_PAP : SPPP_AUTHPROTO_CHAP;
5736 cfg->myauth = 0;
5737 if (sp->myauth.proto)
5738 cfg->myauth = (sp->myauth.proto == PPP_PAP) ? SPPP_AUTHPROTO_PAP : SPPP_AUTHPROTO_CHAP;
5739 if (cfg->myname_length == 0) {
5740 if (sp->myauth.name != NULL)
5741 cfg->myname_length = sp->myauth.name_len + 1;
5742 } else {
5743 if (sp->myauth.name == NULL) {
5744 cfg->myname_length = 0;
5745 } else {
5746 len = sp->myauth.name_len + 1;
5747
5748
5749 if (cfg->myname_length < len) {
5750 SPPP_UNLOCK(sp);
5751 return (ENAMETOOLONG);
5752 }
5753 error = copyout(sp->myauth.name, cfg->myname, len);
5754 if (error) {
5755 SPPP_UNLOCK(sp);
5756 return error;
5757 }
5758 }
5759 }
5760 if (cfg->hisname_length == 0) {
5761 if (sp->hisauth.name != NULL)
5762 cfg->hisname_length = sp->hisauth.name_len + 1;
5763 } else {
5764 if (sp->hisauth.name == NULL) {
5765 cfg->hisname_length = 0;
5766 } else {
5767 len = sp->hisauth.name_len + 1;
5768
5769 if (cfg->hisname_length < len) {
5770 SPPP_UNLOCK(sp);
5771 return (ENAMETOOLONG);
5772 }
5773 error = copyout(sp->hisauth.name, cfg->hisname, len);
5774 if (error) {
5775 SPPP_UNLOCK(sp);
5776 return error;
5777 }
5778 }
5779 }
5780 SPPP_UNLOCK(sp);
5781 }
5782 break;
5783 case SPPPSETAUTHCFG:
5784 {
5785 struct spppauthcfg *cfg = (struct spppauthcfg *)data;
5786 int error;
5787
5788 SPPP_LOCK(sp, RW_WRITER);
5789
5790 if (sp->myauth.name) {
5791 free(sp->myauth.name, M_DEVBUF);
5792 sp->myauth.name = NULL;
5793 }
5794 if (sp->myauth.secret) {
5795 free(sp->myauth.secret, M_DEVBUF);
5796 sp->myauth.secret = NULL;
5797 }
5798 if (sp->hisauth.name) {
5799 free(sp->hisauth.name, M_DEVBUF);
5800 sp->hisauth.name = NULL;
5801 }
5802 if (sp->hisauth.secret) {
5803 free(sp->hisauth.secret, M_DEVBUF);
5804 sp->hisauth.secret = NULL;
5805 }
5806
5807 if (cfg->hisname != NULL && cfg->hisname_length > 0) {
5808 if (cfg->hisname_length >= MCLBYTES) {
5809 SPPP_UNLOCK(sp);
5810 return (ENAMETOOLONG);
5811 }
5812 sp->hisauth.name = malloc(cfg->hisname_length, M_DEVBUF, M_WAITOK);
5813 error = copyin(cfg->hisname, sp->hisauth.name, cfg->hisname_length);
5814 if (error) {
5815 free(sp->hisauth.name, M_DEVBUF);
5816 sp->hisauth.name = NULL;
5817 SPPP_UNLOCK(sp);
5818 return error;
5819 }
5820 sp->hisauth.name_len = cfg->hisname_length - 1;
5821 sp->hisauth.name[sp->hisauth.name_len] = 0;
5822 }
5823 if (cfg->hissecret != NULL && cfg->hissecret_length > 0) {
5824 if (cfg->hissecret_length >= MCLBYTES) {
5825 SPPP_UNLOCK(sp);
5826 return (ENAMETOOLONG);
5827 }
5828 sp->hisauth.secret = malloc(cfg->hissecret_length,
5829 M_DEVBUF, M_WAITOK);
5830 error = copyin(cfg->hissecret, sp->hisauth.secret,
5831 cfg->hissecret_length);
5832 if (error) {
5833 free(sp->hisauth.secret, M_DEVBUF);
5834 sp->hisauth.secret = NULL;
5835 SPPP_UNLOCK(sp);
5836 return error;
5837 }
5838 sp->hisauth.secret_len = cfg->hissecret_length - 1;
5839 sp->hisauth.secret[sp->hisauth.secret_len] = 0;
5840 }
5841 if (cfg->myname != NULL && cfg->myname_length > 0) {
5842 if (cfg->myname_length >= MCLBYTES) {
5843 SPPP_UNLOCK(sp);
5844 return (ENAMETOOLONG);
5845 }
5846 sp->myauth.name = malloc(cfg->myname_length, M_DEVBUF, M_WAITOK);
5847 error = copyin(cfg->myname, sp->myauth.name, cfg->myname_length);
5848 if (error) {
5849 free(sp->myauth.name, M_DEVBUF);
5850 sp->myauth.name = NULL;
5851 SPPP_UNLOCK(sp);
5852 return error;
5853 }
5854 sp->myauth.name_len = cfg->myname_length - 1;
5855 sp->myauth.name[sp->myauth.name_len] = 0;
5856 }
5857 if (cfg->mysecret != NULL && cfg->mysecret_length > 0) {
5858 if (cfg->mysecret_length >= MCLBYTES) {
5859 SPPP_UNLOCK(sp);
5860 return (ENAMETOOLONG);
5861 }
5862 sp->myauth.secret = malloc(cfg->mysecret_length,
5863 M_DEVBUF, M_WAITOK);
5864 error = copyin(cfg->mysecret, sp->myauth.secret,
5865 cfg->mysecret_length);
5866 if (error) {
5867 free(sp->myauth.secret, M_DEVBUF);
5868 sp->myauth.secret = NULL;
5869 SPPP_UNLOCK(sp);
5870 return error;
5871 }
5872 sp->myauth.secret_len = cfg->mysecret_length - 1;
5873 sp->myauth.secret[sp->myauth.secret_len] = 0;
5874 }
5875 sp->myauth.flags = cfg->myauthflags;
5876 if (cfg->myauth)
5877 sp->myauth.proto = (cfg->myauth == SPPP_AUTHPROTO_PAP) ? PPP_PAP : PPP_CHAP;
5878 sp->hisauth.flags = cfg->hisauthflags;
5879 if (cfg->hisauth)
5880 sp->hisauth.proto = (cfg->hisauth == SPPP_AUTHPROTO_PAP) ? PPP_PAP : PPP_CHAP;
5881 sp->pp_auth_failures = 0;
5882 if (sp->hisauth.proto != 0)
5883 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
5884 else
5885 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
5886
5887 SPPP_UNLOCK(sp);
5888 }
5889 break;
5890 case SPPPGETLCPCFG:
5891 {
5892 struct sppplcpcfg *lcpp = (struct sppplcpcfg *)data;
5893
5894 SPPP_LOCK(sp, RW_READER);
5895 lcpp->lcp_timeout = sp->lcp.timeout;
5896 SPPP_UNLOCK(sp);
5897 }
5898 break;
5899 case SPPPSETLCPCFG:
5900 {
5901 struct sppplcpcfg *lcpp = (struct sppplcpcfg *)data;
5902
5903 SPPP_LOCK(sp, RW_WRITER);
5904 sp->lcp.timeout = lcpp->lcp_timeout;
5905 SPPP_UNLOCK(sp);
5906 }
5907 break;
5908 case SPPPGETSTATUS:
5909 {
5910 struct spppstatus *status = (struct spppstatus *)data;
5911
5912 SPPP_LOCK(sp, RW_READER);
5913 status->phase = sp->pp_phase;
5914 SPPP_UNLOCK(sp);
5915 }
5916 break;
5917 case SPPPGETSTATUSNCP:
5918 {
5919 struct spppstatusncp *status = (struct spppstatusncp *)data;
5920
5921 SPPP_LOCK(sp, RW_READER);
5922 status->phase = sp->pp_phase;
5923 status->ncpup = sppp_cp_check(sp, CP_NCP);
5924 SPPP_UNLOCK(sp);
5925 }
5926 break;
5927 case SPPPGETIDLETO:
5928 {
5929 struct spppidletimeout *to = (struct spppidletimeout *)data;
5930
5931 SPPP_LOCK(sp, RW_READER);
5932 to->idle_seconds = sp->pp_idle_timeout;
5933 SPPP_UNLOCK(sp);
5934 }
5935 break;
5936 case SPPPSETIDLETO:
5937 {
5938 struct spppidletimeout *to = (struct spppidletimeout *)data;
5939
5940 SPPP_LOCK(sp, RW_WRITER);
5941 sp->pp_idle_timeout = to->idle_seconds;
5942 SPPP_UNLOCK(sp);
5943 }
5944 break;
5945 case SPPPSETAUTHFAILURE:
5946 {
5947 struct spppauthfailuresettings *afsettings =
5948 (struct spppauthfailuresettings *)data;
5949
5950 SPPP_LOCK(sp, RW_WRITER);
5951 sp->pp_max_auth_fail = afsettings->max_failures;
5952 sp->pp_auth_failures = 0;
5953 SPPP_UNLOCK(sp);
5954 }
5955 break;
5956 case SPPPGETAUTHFAILURES:
5957 {
5958 struct spppauthfailurestats *stats = (struct spppauthfailurestats *)data;
5959
5960 SPPP_LOCK(sp, RW_READER);
5961 stats->auth_failures = sp->pp_auth_failures;
5962 stats->max_failures = sp->pp_max_auth_fail;
5963 SPPP_UNLOCK(sp);
5964 }
5965 break;
5966 case SPPPSETDNSOPTS:
5967 {
5968 struct spppdnssettings *req = (struct spppdnssettings *)data;
5969
5970 SPPP_LOCK(sp, RW_WRITER);
5971 sp->query_dns = req->query_dns & 3;
5972 SPPP_UNLOCK(sp);
5973 }
5974 break;
5975 case SPPPGETDNSOPTS:
5976 {
5977 struct spppdnssettings *req = (struct spppdnssettings *)data;
5978
5979 SPPP_LOCK(sp, RW_READER);
5980 req->query_dns = sp->query_dns;
5981 SPPP_UNLOCK(sp);
5982 }
5983 break;
5984 case SPPPGETDNSADDRS:
5985 {
5986 struct spppdnsaddrs *addrs = (struct spppdnsaddrs *)data;
5987
5988 SPPP_LOCK(sp, RW_READER);
5989 memcpy(&addrs->dns, &sp->dns_addrs, sizeof addrs->dns);
5990 SPPP_UNLOCK(sp);
5991 }
5992 break;
5993 case SPPPGETKEEPALIVE:
5994 {
5995 struct spppkeepalivesettings *settings =
5996 (struct spppkeepalivesettings*)data;
5997
5998 SPPP_LOCK(sp, RW_READER);
5999 settings->maxalive = sp->pp_maxalive;
6000 settings->max_noreceive = sp->pp_max_noreceive;
6001 SPPP_UNLOCK(sp);
6002 }
6003 break;
6004 case SPPPSETKEEPALIVE:
6005 {
6006 struct spppkeepalivesettings *settings =
6007 (struct spppkeepalivesettings*)data;
6008
6009 SPPP_LOCK(sp, RW_WRITER);
6010 sp->pp_maxalive = settings->maxalive;
6011 sp->pp_max_noreceive = settings->max_noreceive;
6012 SPPP_UNLOCK(sp);
6013 }
6014 break;
6015 default:
6016 {
6017 int ret;
6018
6019 MODULE_HOOK_CALL(sppp_params_50_hook, (sp, cmd, data),
6020 enosys(), ret);
6021 if (ret != ENOSYS)
6022 return ret;
6023 return (EINVAL);
6024 }
6025 }
6026 return (0);
6027 }
6028
6029 static void
6030 sppp_phase_network(struct sppp *sp)
6031 {
6032 int i;
6033
6034 KASSERT(SPPP_WLOCKED(sp));
6035
6036 sppp_change_phase(sp, SPPP_PHASE_NETWORK);
6037
6038 /* Notify NCPs now. */
6039 for (i = 0; i < IDX_COUNT; i++)
6040 if ((cps[i])->flags & CP_NCP)
6041 sppp_wq_add(sp->wq_cp, &sp->scp[i].work_open);
6042 }
6043
6044 static const char *
6045 sppp_cp_type_name(u_char type)
6046 {
6047 static char buf[12];
6048 switch (type) {
6049 case CONF_REQ: return "conf-req";
6050 case CONF_ACK: return "conf-ack";
6051 case CONF_NAK: return "conf-nak";
6052 case CONF_REJ: return "conf-rej";
6053 case TERM_REQ: return "term-req";
6054 case TERM_ACK: return "term-ack";
6055 case CODE_REJ: return "code-rej";
6056 case PROTO_REJ: return "proto-rej";
6057 case ECHO_REQ: return "echo-req";
6058 case ECHO_REPLY: return "echo-reply";
6059 case DISC_REQ: return "discard-req";
6060 }
6061 snprintf(buf, sizeof(buf), "0x%x", type);
6062 return buf;
6063 }
6064
6065 static const char *
6066 sppp_auth_type_name(u_short proto, u_char type)
6067 {
6068 static char buf[32];
6069 const char *name;
6070
6071 switch (proto) {
6072 case PPP_CHAP:
6073 switch (type) {
6074 case CHAP_CHALLENGE: return "challenge";
6075 case CHAP_RESPONSE: return "response";
6076 case CHAP_SUCCESS: return "success";
6077 case CHAP_FAILURE: return "failure";
6078 default: name = "chap"; break;
6079 }
6080 break;
6081
6082 case PPP_PAP:
6083 switch (type) {
6084 case PAP_REQ: return "req";
6085 case PAP_ACK: return "ack";
6086 case PAP_NAK: return "nak";
6087 default: name = "pap"; break;
6088 }
6089 break;
6090
6091 default:
6092 name = "bad";
6093 break;
6094 }
6095
6096 snprintf(buf, sizeof(buf), "%s(%#x) %#x", name, proto, type);
6097 return buf;
6098 }
6099
6100 static const char *
6101 sppp_lcp_opt_name(u_char opt)
6102 {
6103 static char buf[12];
6104 switch (opt) {
6105 case LCP_OPT_MRU: return "mru";
6106 case LCP_OPT_ASYNC_MAP: return "async-map";
6107 case LCP_OPT_AUTH_PROTO: return "auth-proto";
6108 case LCP_OPT_QUAL_PROTO: return "qual-proto";
6109 case LCP_OPT_MAGIC: return "magic";
6110 case LCP_OPT_PROTO_COMP: return "proto-comp";
6111 case LCP_OPT_ADDR_COMP: return "addr-comp";
6112 case LCP_OPT_SELF_DESC_PAD: return "sdpad";
6113 case LCP_OPT_CALL_BACK: return "callback";
6114 case LCP_OPT_COMPOUND_FRMS: return "cmpd-frms";
6115 case LCP_OPT_MP_MRRU: return "mrru";
6116 case LCP_OPT_MP_SSNHF: return "mp-ssnhf";
6117 case LCP_OPT_MP_EID: return "mp-eid";
6118 }
6119 snprintf(buf, sizeof(buf), "0x%x", opt);
6120 return buf;
6121 }
6122
6123 static const char *
6124 sppp_ipcp_opt_name(u_char opt)
6125 {
6126 static char buf[12];
6127 switch (opt) {
6128 case IPCP_OPT_ADDRESSES: return "addresses";
6129 case IPCP_OPT_COMPRESSION: return "compression";
6130 case IPCP_OPT_ADDRESS: return "address";
6131 }
6132 snprintf(buf, sizeof(buf), "0x%x", opt);
6133 return buf;
6134 }
6135
6136 #ifdef INET6
6137 static const char *
6138 sppp_ipv6cp_opt_name(u_char opt)
6139 {
6140 static char buf[12];
6141 switch (opt) {
6142 case IPV6CP_OPT_IFID: return "ifid";
6143 case IPV6CP_OPT_COMPRESSION: return "compression";
6144 }
6145 snprintf(buf, sizeof(buf), "0x%x", opt);
6146 return buf;
6147 }
6148 #endif
6149
6150 static const char *
6151 sppp_state_name(int state)
6152 {
6153 switch (state) {
6154 case STATE_INITIAL: return "initial";
6155 case STATE_STARTING: return "starting";
6156 case STATE_CLOSED: return "closed";
6157 case STATE_STOPPED: return "stopped";
6158 case STATE_CLOSING: return "closing";
6159 case STATE_STOPPING: return "stopping";
6160 case STATE_REQ_SENT: return "req-sent";
6161 case STATE_ACK_RCVD: return "ack-rcvd";
6162 case STATE_ACK_SENT: return "ack-sent";
6163 case STATE_OPENED: return "opened";
6164 }
6165 return "illegal";
6166 }
6167
6168 static const char *
6169 sppp_phase_name(int phase)
6170 {
6171 switch (phase) {
6172 case SPPP_PHASE_DEAD: return "dead";
6173 case SPPP_PHASE_ESTABLISH: return "establish";
6174 case SPPP_PHASE_TERMINATE: return "terminate";
6175 case SPPP_PHASE_AUTHENTICATE: return "authenticate";
6176 case SPPP_PHASE_NETWORK: return "network";
6177 }
6178 return "illegal";
6179 }
6180
6181 static const char *
6182 sppp_proto_name(u_short proto)
6183 {
6184 static char buf[12];
6185 switch (proto) {
6186 case PPP_LCP: return "lcp";
6187 case PPP_IPCP: return "ipcp";
6188 case PPP_PAP: return "pap";
6189 case PPP_CHAP: return "chap";
6190 case PPP_IPV6CP: return "ipv6cp";
6191 }
6192 snprintf(buf, sizeof(buf), "0x%x", (unsigned)proto);
6193 return buf;
6194 }
6195
6196 static void
6197 sppp_print_bytes(const u_char *p, u_short len)
6198 {
6199 addlog(" %02x", *p++);
6200 while (--len > 0)
6201 addlog("-%02x", *p++);
6202 }
6203
6204 static void
6205 sppp_print_string(const char *p, u_short len)
6206 {
6207 u_char c;
6208
6209 while (len-- > 0) {
6210 c = *p++;
6211 /*
6212 * Print only ASCII chars directly. RFC 1994 recommends
6213 * using only them, but we don't rely on it. */
6214 if (c < ' ' || c > '~')
6215 addlog("\\x%x", c);
6216 else
6217 addlog("%c", c);
6218 }
6219 }
6220
6221 static const char *
6222 sppp_dotted_quad(uint32_t addr)
6223 {
6224 static char s[16];
6225 snprintf(s, sizeof(s), "%d.%d.%d.%d",
6226 (int)((addr >> 24) & 0xff),
6227 (int)((addr >> 16) & 0xff),
6228 (int)((addr >> 8) & 0xff),
6229 (int)(addr & 0xff));
6230 return s;
6231 }
6232
6233 /* a dummy, used to drop uninteresting events */
6234 static void
6235 sppp_null(struct sppp *unused)
6236 {
6237 /* do just nothing */
6238 }
6239
6240 static void
6241 sppp_tls(const struct cp *cp, struct sppp *sp)
6242 {
6243
6244 /* notify lcp that is lower layer */
6245 sp->lcp.protos |= (1 << cp->protoidx);
6246
6247 if (sp->scp[IDX_LCP].state == STATE_OPENED)
6248 sppp_wq_add(sp->wq_cp, &sp->scp[cp->protoidx].work_up);
6249 }
6250
6251 static void
6252 sppp_tlf(const struct cp *cp, struct sppp *sp)
6253 {
6254 STDDCL;
6255
6256 if (debug)
6257 log(LOG_DEBUG, "%s: %s tlf\n", ifp->if_xname, cp->name);
6258
6259 /* notify lcp that is lower layer */
6260 sp->lcp.protos &= ~(1 << cp->protoidx);
6261
6262 /* cleanup */
6263 if (sp->scp[cp->protoidx].rcr_buf != NULL) {
6264 kmem_free(sp->scp[cp->protoidx].rcr_buf,
6265 sp->scp[cp->protoidx].rcr_blen);
6266 }
6267
6268 sp->scp[cp->protoidx].rcr_buf = NULL;
6269 sp->scp[cp->protoidx].rcr_blen = 0;
6270 sp->scp[cp->protoidx].rcr_rlen = 0;
6271
6272 sppp_lcp_check_and_close(sp);
6273 }
6274
6275 static void
6276 sppp_sca_scn(const struct cp *cp, struct sppp *sp)
6277 {
6278 STDDCL;
6279 u_char rconfid, type, rlen;
6280 void *buf;
6281 size_t blen;
6282
6283 rconfid = sp->scp[cp->protoidx].rconfid;
6284 type = sp->scp[cp->protoidx].rcr_type;
6285 buf = sp->scp[cp->protoidx].rcr_buf;
6286 rlen = sp->scp[cp->protoidx].rcr_rlen;
6287 blen = sp->scp[cp->protoidx].rcr_blen;
6288
6289 sp->scp[cp->protoidx].rcr_buf = NULL;
6290 sp->scp[cp->protoidx].rcr_blen = 0;
6291
6292 if (buf != NULL) {
6293 if (rlen > 0) {
6294 if (debug) {
6295 log(LOG_DEBUG, "%s: send %s\n",
6296 ifp->if_xname, sppp_cp_type_name(type));
6297 }
6298 sppp_cp_send(sp, cp->proto, type, rconfid, rlen, buf);
6299 }
6300 kmem_free(buf, blen);
6301 }
6302 }
6303
6304 static void
6305 sppp_ifdown(struct sppp *sp, void *xcp __unused)
6306 {
6307
6308 SPPP_UNLOCK(sp);
6309 if_down(&sp->pp_if);
6310 IF_PURGE(&sp->pp_cpq);
6311 SPPP_LOCK(sp, RW_WRITER);
6312 }
6313
6314 /*
6315 * This file is large. Tell emacs to highlight it nevertheless.
6316 *
6317 * Local Variables:
6318 * hilit-auto-highlight-maxout: 120000
6319 * End:
6320 */
6321
6322 /*
6323 * Module glue
6324 */
6325 MODULE(MODULE_CLASS_MISC, sppp_subr, NULL);
6326
6327 static int
6328 sppp_subr_modcmd(modcmd_t cmd, void *arg)
6329 {
6330 switch (cmd) {
6331 case MODULE_CMD_INIT:
6332 case MODULE_CMD_FINI:
6333 return 0;
6334 case MODULE_CMD_STAT:
6335 case MODULE_CMD_AUTOUNLOAD:
6336 default:
6337 return ENOTTY;
6338 }
6339 }
6340
6341 static void
6342 sppp_notify_up(struct sppp *sp)
6343 {
6344
6345 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_up);
6346 }
6347
6348 static void
6349 sppp_notify_down(struct sppp *sp)
6350 {
6351
6352 sppp_wq_add(sp->wq_cp, &sp->scp[IDX_LCP].work_down);
6353 }
6354
6355 static void
6356 sppp_notify_tls_wlocked(struct sppp *sp)
6357 {
6358
6359 KASSERT(SPPP_WLOCKED(sp));
6360
6361 if (!sp->pp_tls)
6362 return;
6363
6364 SPPP_UNLOCK(sp);
6365 sp->pp_tls(sp);
6366 SPPP_LOCK(sp, RW_WRITER);
6367 }
6368
6369 static void
6370 sppp_notify_tlf_wlocked(struct sppp *sp)
6371 {
6372
6373 KASSERT(SPPP_WLOCKED(sp));
6374
6375 if (!sp->pp_tlf)
6376 return;
6377
6378 SPPP_UNLOCK(sp);
6379 sp->pp_tlf(sp);
6380 SPPP_LOCK(sp, RW_WRITER);
6381 }
6382
6383 static void
6384 sppp_notify_con(struct sppp *sp)
6385 {
6386
6387 if (!sp->pp_con)
6388 return;
6389
6390 sp->pp_con(sp);
6391 }
6392
6393 #ifdef INET6
6394 static void
6395 sppp_notify_con_wlocked(struct sppp *sp)
6396 {
6397
6398 KASSERT(SPPP_WLOCKED(sp));
6399
6400 SPPP_UNLOCK(sp);
6401 sppp_notify_con(sp);
6402 SPPP_LOCK(sp, RW_WRITER);
6403
6404 }
6405 #endif
6406
6407 static void
6408 sppp_notify_chg_wlocked(struct sppp *sp)
6409 {
6410
6411 KASSERT(SPPP_WLOCKED(sp));
6412
6413 if (!sp->pp_chg)
6414 return;
6415
6416 SPPP_UNLOCK(sp);
6417 sp->pp_chg(sp, sp->pp_phase);
6418 SPPP_LOCK(sp, RW_WRITER);
6419 }
6420
6421 static void
6422 sppp_wq_work(struct work *wk, void *xsp)
6423 {
6424 struct sppp *sp;
6425 struct sppp_work *work;
6426
6427 sp = xsp;
6428 work = container_of(wk, struct sppp_work, work);
6429 atomic_cas_uint(&work->state, SPPP_WK_BUSY, SPPP_WK_FREE);
6430
6431 SPPP_LOCK(sp, RW_WRITER);
6432 work->func(sp, work->arg);
6433 SPPP_UNLOCK(sp);
6434 }
6435
6436 static struct workqueue *
6437 sppp_wq_create(struct sppp *sp, const char *xnamebuf, pri_t prio, int ipl, int flags)
6438 {
6439 struct workqueue *wq;
6440 int error;
6441
6442 error = workqueue_create(&wq, xnamebuf, sppp_wq_work,
6443 (void *)sp, prio, ipl, flags);
6444 if (error) {
6445 panic("%s: workqueue_create failed [%s, %d]\n",
6446 sp->pp_if.if_xname, xnamebuf, error);
6447 }
6448
6449 return wq;
6450 }
6451
6452 static void
6453 sppp_wq_destroy(struct sppp *sp __unused, struct workqueue *wq)
6454 {
6455
6456 workqueue_destroy(wq);
6457 }
6458
6459 static void
6460 sppp_wq_set(struct sppp_work *work,
6461 void (*func)(struct sppp *, void *), void *arg)
6462 {
6463
6464 work->func = func;
6465 work->arg = arg;
6466 }
6467
6468 static void
6469 sppp_wq_add(struct workqueue *wq, struct sppp_work *work)
6470 {
6471
6472 if (atomic_cas_uint(&work->state, SPPP_WK_FREE, SPPP_WK_BUSY)
6473 != SPPP_WK_FREE)
6474 return;
6475
6476 KASSERT(work->func != NULL);
6477 kpreempt_disable();
6478 workqueue_enqueue(wq, &work->work, NULL);
6479 kpreempt_enable();
6480 }
6481 static void
6482 sppp_wq_wait(struct workqueue *wq, struct sppp_work *work)
6483 {
6484
6485 atomic_swap_uint(&work->state, SPPP_WK_UNAVAIL);
6486 workqueue_wait(wq, &work->work);
6487 }
6488