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