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