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