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