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