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