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