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