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