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