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