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