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