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