Home | History | Annotate | Line # | Download | only in net
if_spppsubr.c revision 1.24
      1 /*	$NetBSD: if_spppsubr.c,v 1.24 2001/07/17 19:12:02 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 = splnet();
    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 = splnet();
    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 = splnet();
    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 = splnet();
   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 = splnet();
   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= splnet ();
   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 = splnet();
   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 			sppp_lcp_check_and_close(sp);
   1611 			break;
   1612 		case STATE_STOPPING:
   1613 			(cp->tlf)(sp);
   1614 			sppp_cp_change_state(cp, sp, STATE_STOPPED);
   1615 			sppp_lcp_check_and_close(sp);
   1616 			break;
   1617 		case STATE_ACK_RCVD:
   1618 			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   1619 			break;
   1620 		case STATE_OPENED:
   1621 			(cp->tld)(sp);
   1622 			(cp->scr)(sp);
   1623 			sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
   1624 			break;
   1625 		default:
   1626 			printf(SPP_FMT "%s illegal %s in state %s\n",
   1627 			       SPP_ARGS(ifp), cp->name,
   1628 			       sppp_cp_type_name(h->type),
   1629 			       sppp_state_name(sp->state[cp->protoidx]));
   1630 			++ifp->if_ierrors;
   1631 		}
   1632 		break;
   1633 	case CODE_REJ:
   1634 		/* XXX catastrophic rejects (RXJ-) aren't handled yet. */
   1635 		log(LOG_INFO,
   1636 		    SPP_FMT "%s: ignoring RXJ (%s) for code ?, "
   1637 		    "danger will robinson\n",
   1638 		    SPP_ARGS(ifp), cp->name,
   1639 		    sppp_cp_type_name(h->type));
   1640 		switch (sp->state[cp->protoidx]) {
   1641 		case STATE_CLOSED:
   1642 		case STATE_STOPPED:
   1643 		case STATE_REQ_SENT:
   1644 		case STATE_ACK_SENT:
   1645 		case STATE_CLOSING:
   1646 		case STATE_STOPPING:
   1647 		case STATE_OPENED:
   1648 			break;
   1649 		case STATE_ACK_RCVD:
   1650 			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   1651 			break;
   1652 		default:
   1653 			printf(SPP_FMT "%s illegal %s in state %s\n",
   1654 			       SPP_ARGS(ifp), cp->name,
   1655 			       sppp_cp_type_name(h->type),
   1656 			       sppp_state_name(sp->state[cp->protoidx]));
   1657 			++ifp->if_ierrors;
   1658 		}
   1659 		break;
   1660 	case PROTO_REJ:
   1661 	    {
   1662 		int catastrophic;
   1663 		const struct cp *upper;
   1664 		int i;
   1665 		u_int16_t proto;
   1666 
   1667 		catastrophic = 0;
   1668 		upper = NULL;
   1669 		proto = ntohs(*((u_int16_t *)p));
   1670 		for (i = 0; i < IDX_COUNT; i++) {
   1671 			if (cps[i]->proto == proto) {
   1672 				upper = cps[i];
   1673 				break;
   1674 			}
   1675 		}
   1676 		if (upper == NULL)
   1677 			catastrophic++;
   1678 
   1679 		log(LOG_INFO,
   1680 		    SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
   1681 		    SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
   1682 		    sppp_cp_type_name(h->type), proto,
   1683 		    upper ? upper->name : "unknown",
   1684 		    upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
   1685 
   1686 		/*
   1687 		 * if we got RXJ+ against conf-req, the peer does not implement
   1688 		 * this particular protocol type.  terminate the protocol.
   1689 		 */
   1690 		if (upper && !catastrophic) {
   1691 			if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
   1692 				upper->Close(sp);
   1693 				break;
   1694 			}
   1695 		}
   1696 
   1697 		/* XXX catastrophic rejects (RXJ-) aren't handled yet. */
   1698 		switch (sp->state[cp->protoidx]) {
   1699 		case STATE_CLOSED:
   1700 		case STATE_STOPPED:
   1701 		case STATE_REQ_SENT:
   1702 		case STATE_ACK_SENT:
   1703 		case STATE_CLOSING:
   1704 		case STATE_STOPPING:
   1705 		case STATE_OPENED:
   1706 			break;
   1707 		case STATE_ACK_RCVD:
   1708 			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   1709 			break;
   1710 		default:
   1711 			printf(SPP_FMT "%s illegal %s in state %s\n",
   1712 			       SPP_ARGS(ifp), cp->name,
   1713 			       sppp_cp_type_name(h->type),
   1714 			       sppp_state_name(sp->state[cp->protoidx]));
   1715 			++ifp->if_ierrors;
   1716 		}
   1717 		break;
   1718 	    }
   1719 	case DISC_REQ:
   1720 		if (cp->proto != PPP_LCP)
   1721 			goto illegal;
   1722 		/* Discard the packet. */
   1723 		break;
   1724 	case ECHO_REQ:
   1725 		if (cp->proto != PPP_LCP)
   1726 			goto illegal;
   1727 		if (sp->state[cp->protoidx] != STATE_OPENED) {
   1728 			if (debug)
   1729 				addlog(SPP_FMT "lcp echo req but lcp closed\n",
   1730 				       SPP_ARGS(ifp));
   1731 			++ifp->if_ierrors;
   1732 			break;
   1733 		}
   1734 		if (len < 8) {
   1735 			if (debug)
   1736 				addlog(SPP_FMT "invalid lcp echo request "
   1737 				       "packet length: %d bytes\n",
   1738 				       SPP_ARGS(ifp), len);
   1739 			break;
   1740 		}
   1741 		if (ntohl (*(long*)(h+1)) == sp->lcp.magic) {
   1742 			/* Line loopback mode detected. */
   1743 			printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
   1744 			if_down (ifp);
   1745 			IFQ_PURGE (&sp->pp_cpq);
   1746 
   1747 			/* Shut down the PPP link. */
   1748 			/* XXX */
   1749 			lcp.Down(sp);
   1750 			lcp.Up(sp);
   1751 			break;
   1752 		}
   1753 		*(long*)(h+1) = htonl (sp->lcp.magic);
   1754 		if (debug)
   1755 			addlog(SPP_FMT "got lcp echo req, sending echo rep\n",
   1756 			       SPP_ARGS(ifp));
   1757 		sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
   1758 		break;
   1759 	case ECHO_REPLY:
   1760 		if (cp->proto != PPP_LCP)
   1761 			goto illegal;
   1762 		if (h->ident != sp->lcp.echoid) {
   1763 			++ifp->if_ierrors;
   1764 			break;
   1765 		}
   1766 		if (len < 8) {
   1767 			if (debug)
   1768 				addlog(SPP_FMT "lcp invalid echo reply "
   1769 				       "packet length: %d bytes\n",
   1770 				       SPP_ARGS(ifp), len);
   1771 			break;
   1772 		}
   1773 		if (debug)
   1774 			addlog(SPP_FMT "lcp got echo rep\n",
   1775 			       SPP_ARGS(ifp));
   1776 		if (ntohl (*(long*)(h+1)) != sp->lcp.magic)
   1777 			sp->pp_alivecnt = 0;
   1778 		break;
   1779 	default:
   1780 		/* Unknown packet type -- send Code-Reject packet. */
   1781 	  illegal:
   1782 		if (debug)
   1783 			addlog(SPP_FMT "%s send code-rej for 0x%x\n",
   1784 			       SPP_ARGS(ifp), cp->name, h->type);
   1785 		sppp_cp_send(sp, cp->proto, CODE_REJ,
   1786 		    ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
   1787 		++ifp->if_ierrors;
   1788 	}
   1789 }
   1790 
   1791 
   1792 /*
   1793  * The generic part of all Up/Down/Open/Close/TO event handlers.
   1794  * Basically, the state transition handling in the automaton.
   1795  */
   1796 static void
   1797 sppp_up_event(const struct cp *cp, struct sppp *sp)
   1798 {
   1799 	STDDCL;
   1800 
   1801 	if (debug)
   1802 		log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
   1803 		    SPP_ARGS(ifp), cp->name,
   1804 		    sppp_state_name(sp->state[cp->protoidx]));
   1805 
   1806 	switch (sp->state[cp->protoidx]) {
   1807 	case STATE_INITIAL:
   1808 		sppp_cp_change_state(cp, sp, STATE_CLOSED);
   1809 		break;
   1810 	case STATE_STARTING:
   1811 		sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
   1812 		(cp->scr)(sp);
   1813 		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   1814 		break;
   1815 	default:
   1816 		printf(SPP_FMT "%s illegal up in state %s\n",
   1817 		       SPP_ARGS(ifp), cp->name,
   1818 		       sppp_state_name(sp->state[cp->protoidx]));
   1819 	}
   1820 }
   1821 
   1822 static void
   1823 sppp_down_event(const struct cp *cp, struct sppp *sp)
   1824 {
   1825 	STDDCL;
   1826 
   1827 	if (debug)
   1828 		log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
   1829 		    SPP_ARGS(ifp), cp->name,
   1830 		    sppp_state_name(sp->state[cp->protoidx]));
   1831 
   1832 	switch (sp->state[cp->protoidx]) {
   1833 	case STATE_CLOSED:
   1834 	case STATE_CLOSING:
   1835 		sppp_cp_change_state(cp, sp, STATE_INITIAL);
   1836 		break;
   1837 	case STATE_STOPPED:
   1838 		(cp->tls)(sp);
   1839 		/* fall through */
   1840 	case STATE_STOPPING:
   1841 	case STATE_REQ_SENT:
   1842 	case STATE_ACK_RCVD:
   1843 	case STATE_ACK_SENT:
   1844 		sppp_cp_change_state(cp, sp, STATE_STARTING);
   1845 		break;
   1846 	case STATE_OPENED:
   1847 		(cp->tld)(sp);
   1848 		sppp_cp_change_state(cp, sp, STATE_STARTING);
   1849 		break;
   1850 	default:
   1851 		printf(SPP_FMT "%s illegal down in state %s\n",
   1852 		       SPP_ARGS(ifp), cp->name,
   1853 		       sppp_state_name(sp->state[cp->protoidx]));
   1854 	}
   1855 }
   1856 
   1857 
   1858 static void
   1859 sppp_open_event(const struct cp *cp, struct sppp *sp)
   1860 {
   1861 	STDDCL;
   1862 
   1863 	if (debug)
   1864 		log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
   1865 		    SPP_ARGS(ifp), cp->name,
   1866 		    sppp_state_name(sp->state[cp->protoidx]));
   1867 
   1868 	switch (sp->state[cp->protoidx]) {
   1869 	case STATE_INITIAL:
   1870 		(cp->tls)(sp);
   1871 		sppp_cp_change_state(cp, sp, STATE_STARTING);
   1872 		break;
   1873 	case STATE_STARTING:
   1874 		break;
   1875 	case STATE_CLOSED:
   1876 		sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
   1877 		(cp->scr)(sp);
   1878 		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   1879 		break;
   1880 	case STATE_STOPPED:
   1881 	case STATE_STOPPING:
   1882 	case STATE_REQ_SENT:
   1883 	case STATE_ACK_RCVD:
   1884 	case STATE_ACK_SENT:
   1885 	case STATE_OPENED:
   1886 		break;
   1887 	case STATE_CLOSING:
   1888 		sppp_cp_change_state(cp, sp, STATE_STOPPING);
   1889 		break;
   1890 	}
   1891 }
   1892 
   1893 
   1894 static void
   1895 sppp_close_event(const struct cp *cp, struct sppp *sp)
   1896 {
   1897 	STDDCL;
   1898 
   1899 	if (debug)
   1900 		log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
   1901 		    SPP_ARGS(ifp), cp->name,
   1902 		    sppp_state_name(sp->state[cp->protoidx]));
   1903 
   1904 	switch (sp->state[cp->protoidx]) {
   1905 	case STATE_INITIAL:
   1906 	case STATE_CLOSED:
   1907 	case STATE_CLOSING:
   1908 		break;
   1909 	case STATE_STARTING:
   1910 		(cp->tlf)(sp);
   1911 		sppp_cp_change_state(cp, sp, STATE_INITIAL);
   1912 		break;
   1913 	case STATE_STOPPED:
   1914 		sppp_cp_change_state(cp, sp, STATE_CLOSED);
   1915 		break;
   1916 	case STATE_STOPPING:
   1917 		sppp_cp_change_state(cp, sp, STATE_CLOSING);
   1918 		break;
   1919 	case STATE_OPENED:
   1920 		(cp->tld)(sp);
   1921 		/* fall through */
   1922 	case STATE_REQ_SENT:
   1923 	case STATE_ACK_RCVD:
   1924 	case STATE_ACK_SENT:
   1925 		sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
   1926 		sppp_cp_send(sp, cp->proto, TERM_REQ,
   1927 		    ++sp->pp_seq[cp->protoidx], 0, 0);
   1928 		sppp_cp_change_state(cp, sp, STATE_CLOSING);
   1929 		break;
   1930 	}
   1931 }
   1932 
   1933 static void
   1934 sppp_to_event(const struct cp *cp, struct sppp *sp)
   1935 {
   1936 	STDDCL;
   1937 	int s;
   1938 
   1939 	s = splnet();
   1940 	if (debug)
   1941 		log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
   1942 		    SPP_ARGS(ifp), cp->name,
   1943 		    sppp_state_name(sp->state[cp->protoidx]),
   1944 		    sp->rst_counter[cp->protoidx]);
   1945 
   1946 	if (--sp->rst_counter[cp->protoidx] < 0)
   1947 		/* TO- event */
   1948 		switch (sp->state[cp->protoidx]) {
   1949 		case STATE_CLOSING:
   1950 			(cp->tlf)(sp);
   1951 			sppp_cp_change_state(cp, sp, STATE_CLOSED);
   1952 			sppp_lcp_check_and_close(sp);
   1953 			break;
   1954 		case STATE_STOPPING:
   1955 			(cp->tlf)(sp);
   1956 			sppp_cp_change_state(cp, sp, STATE_STOPPED);
   1957 			sppp_lcp_check_and_close(sp);
   1958 			break;
   1959 		case STATE_REQ_SENT:
   1960 		case STATE_ACK_RCVD:
   1961 		case STATE_ACK_SENT:
   1962 			(cp->tlf)(sp);
   1963 			sppp_cp_change_state(cp, sp, STATE_STOPPED);
   1964 			sppp_lcp_check_and_close(sp);
   1965 			break;
   1966 		}
   1967 	else
   1968 		/* TO+ event */
   1969 		switch (sp->state[cp->protoidx]) {
   1970 		case STATE_CLOSING:
   1971 		case STATE_STOPPING:
   1972 			sppp_cp_send(sp, cp->proto, TERM_REQ,
   1973 			    ++sp->pp_seq[cp->protoidx], 0, 0);
   1974 #if defined(__NetBSD__)
   1975 			callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
   1976 			    cp->TO, sp);
   1977 #else
   1978 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   1979 			sp->ch[cp->protoidx] =
   1980 #endif
   1981 			timeout(cp->TO, (void *)sp, sp->lcp.timeout);
   1982 #endif /* __NetBSD__ */
   1983 			break;
   1984 		case STATE_REQ_SENT:
   1985 		case STATE_ACK_RCVD:
   1986 			(cp->scr)(sp);
   1987 			/* sppp_cp_change_state() will restart the timer */
   1988 			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
   1989 			break;
   1990 		case STATE_ACK_SENT:
   1991 			(cp->scr)(sp);
   1992 #if defined(__NetBSD__)
   1993 			callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
   1994 			    cp->TO, sp);
   1995 #else
   1996 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   1997 			sp->ch[cp->protoidx] =
   1998 #endif
   1999 			timeout(cp->TO, (void *)sp, sp->lcp.timeout);
   2000 #endif /* __NetBSD__ */
   2001 			break;
   2002 		}
   2003 
   2004 	splx(s);
   2005 }
   2006 
   2007 /*
   2008  * Change the state of a control protocol in the state automaton.
   2009  * Takes care of starting/stopping the restart timer.
   2010  */
   2011 void
   2012 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
   2013 {
   2014 	sp->state[cp->protoidx] = newstate;
   2015 
   2016 #if defined(__NetBSD__)
   2017 	callout_stop(&sp->ch[cp->protoidx]);
   2018 #else
   2019 	untimeout(cp->TO, (void *)sp
   2020 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   2021 	, sp->ch[cp->protoidx]
   2022 #endif
   2023 	);
   2024 #endif /* __NetBSD__ */
   2025 	switch (newstate) {
   2026 	case STATE_INITIAL:
   2027 	case STATE_STARTING:
   2028 	case STATE_CLOSED:
   2029 	case STATE_STOPPED:
   2030 	case STATE_OPENED:
   2031 		break;
   2032 	case STATE_CLOSING:
   2033 	case STATE_STOPPING:
   2034 	case STATE_REQ_SENT:
   2035 	case STATE_ACK_RCVD:
   2036 	case STATE_ACK_SENT:
   2037 #if defined(__NetBSD__)
   2038 		callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
   2039 		    cp->TO, sp);
   2040 #else
   2041 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   2042 		sp->ch[cp->protoidx]  =
   2043 #endif
   2044 		timeout(cp->TO, (void *)sp, sp->lcp.timeout);
   2045 #endif /* __NetBSD__ */
   2046 		break;
   2047 	}
   2048 }
   2049 /*
   2051  *--------------------------------------------------------------------------*
   2052  *                                                                          *
   2053  *                         The LCP implementation.                          *
   2054  *                                                                          *
   2055  *--------------------------------------------------------------------------*
   2056  */
   2057 static void
   2058 sppp_lcp_init(struct sppp *sp)
   2059 {
   2060 	sp->lcp.opts = (1 << LCP_OPT_MAGIC);
   2061 	sp->lcp.magic = 0;
   2062 	sp->state[IDX_LCP] = STATE_INITIAL;
   2063 	sp->fail_counter[IDX_LCP] = 0;
   2064 	sp->pp_seq[IDX_LCP] = 0;
   2065 	sp->pp_rseq[IDX_LCP] = 0;
   2066 	sp->lcp.protos = 0;
   2067 	sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
   2068 
   2069 	/*
   2070 	 * Initialize counters and timeout values.  Note that we don't
   2071 	 * use the 3 seconds suggested in RFC 1661 since we are likely
   2072 	 * running on a fast link.  XXX We should probably implement
   2073 	 * the exponential backoff option.  Note that these values are
   2074 	 * relevant for all control protocols, not just LCP only.
   2075 	 */
   2076 	sp->lcp.timeout = 1 * hz;
   2077 	sp->lcp.max_terminate = 2;
   2078 	sp->lcp.max_configure = 10;
   2079 	sp->lcp.max_failure = 10;
   2080 #if defined(__NetBSD__)
   2081 	callout_init(&sp->ch[IDX_LCP]);
   2082 #endif
   2083 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   2084 	callout_handle_init(&sp->ch[IDX_LCP]);
   2085 #endif
   2086 }
   2087 
   2088 static void
   2089 sppp_lcp_up(struct sppp *sp)
   2090 {
   2091 	STDDCL;
   2092 
   2093 	/*
   2094 	 * If this interface is passive or dial-on-demand, and we are
   2095 	 * still in Initial state, it means we've got an incoming
   2096 	 * call.  Activate the interface.
   2097 	 */
   2098 	if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
   2099 		if (debug)
   2100 			log(LOG_DEBUG,
   2101 			    SPP_FMT "Up event", SPP_ARGS(ifp));
   2102 		ifp->if_flags |= IFF_RUNNING;
   2103 		if (sp->state[IDX_LCP] == STATE_INITIAL) {
   2104 			if (debug)
   2105 				addlog("(incoming call)\n");
   2106 			sp->pp_flags |= PP_CALLIN;
   2107 			lcp.Open(sp);
   2108 		} else if (debug)
   2109 			addlog("\n");
   2110 	}
   2111 
   2112 	sppp_up_event(&lcp, sp);
   2113 }
   2114 
   2115 static void
   2116 sppp_lcp_down(struct sppp *sp)
   2117 {
   2118 	STDDCL;
   2119 
   2120 	sppp_down_event(&lcp, sp);
   2121 
   2122 	/*
   2123 	 * If this is neither a dial-on-demand nor a passive
   2124 	 * interface, simulate an ``ifconfig down'' action, so the
   2125 	 * administrator can force a redial by another ``ifconfig
   2126 	 * up''.  XXX For leased line operation, should we immediately
   2127 	 * try to reopen the connection here?
   2128 	 */
   2129 	if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
   2130 		log(LOG_INFO,
   2131 		    SPP_FMT "Down event (carrier loss), taking interface down.\n",
   2132 		    SPP_ARGS(ifp));
   2133 		if_down(ifp);
   2134 	} else {
   2135 		if (debug)
   2136 			log(LOG_DEBUG,
   2137 			    SPP_FMT "Down event (carrier loss)\n",
   2138 			    SPP_ARGS(ifp));
   2139 	}
   2140 	sp->pp_flags &= ~PP_CALLIN;
   2141 	if (sp->state[IDX_LCP] != STATE_INITIAL)
   2142 		lcp.Close(sp);
   2143 	ifp->if_flags &= ~IFF_RUNNING;
   2144 }
   2145 
   2146 static void
   2147 sppp_lcp_open(struct sppp *sp)
   2148 {
   2149 	/*
   2150 	 * If we are authenticator, negotiate LCP_AUTH
   2151 	 */
   2152 	if (sp->hisauth.proto != 0)
   2153 		sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
   2154 	else
   2155 		sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
   2156 	sp->pp_flags &= ~PP_NEEDAUTH;
   2157 	sppp_open_event(&lcp, sp);
   2158 }
   2159 
   2160 static void
   2161 sppp_lcp_close(struct sppp *sp)
   2162 {
   2163 	sppp_close_event(&lcp, sp);
   2164 }
   2165 
   2166 static void
   2167 sppp_lcp_TO(void *cookie)
   2168 {
   2169 	sppp_to_event(&lcp, (struct sppp *)cookie);
   2170 }
   2171 
   2172 /*
   2173  * Analyze a configure request.  Return true if it was agreeable, and
   2174  * caused action sca, false if it has been rejected or nak'ed, and
   2175  * caused action scn.  (The return value is used to make the state
   2176  * transition decision in the state automaton.)
   2177  */
   2178 static int
   2179 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
   2180 {
   2181 	STDDCL;
   2182 	u_char *buf, *r, *p;
   2183 	int origlen, rlen;
   2184 	u_long nmagic;
   2185 	u_short authproto;
   2186 
   2187 	len -= 4;
   2188 	origlen = len;
   2189 	buf = r = malloc (len, M_TEMP, M_NOWAIT);
   2190 	if (! buf)
   2191 		return (0);
   2192 
   2193 	if (debug)
   2194 		log(LOG_DEBUG, SPP_FMT "lcp parse opts:",
   2195 		    SPP_ARGS(ifp));
   2196 
   2197 	/* pass 1: check for things that need to be rejected */
   2198 	p = (void*) (h+1);
   2199 	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
   2200 		if (debug)
   2201 			addlog(" %s", sppp_lcp_opt_name(*p));
   2202 		switch (*p) {
   2203 		case LCP_OPT_MAGIC:
   2204 			/* Magic number. */
   2205 			/* fall through, both are same length */
   2206 		case LCP_OPT_ASYNC_MAP:
   2207 			/* Async control character map. */
   2208 			if (len >= 6 || p[1] == 6)
   2209 				continue;
   2210 			if (debug)
   2211 				addlog(" [invalid]");
   2212 			break;
   2213 		case LCP_OPT_MRU:
   2214 			/* Maximum receive unit. */
   2215 			if (len >= 4 && p[1] == 4)
   2216 				continue;
   2217 			if (debug)
   2218 				addlog(" [invalid]");
   2219 			break;
   2220 		case LCP_OPT_AUTH_PROTO:
   2221 			if (len < 4) {
   2222 				if (debug)
   2223 					addlog(" [invalid]");
   2224 				break;
   2225 			}
   2226 			authproto = (p[2] << 8) + p[3];
   2227 			if (authproto == PPP_CHAP && p[1] != 5) {
   2228 				if (debug)
   2229 					addlog(" [invalid chap len]");
   2230 				break;
   2231 			}
   2232 			if (sp->myauth.proto == 0) {
   2233 				/* we are not configured to do auth */
   2234 				if (debug)
   2235 					addlog(" [not configured]");
   2236 				break;
   2237 			}
   2238 			/*
   2239 			 * Remote want us to authenticate, remember this,
   2240 			 * so we stay in PHASE_AUTHENTICATE after LCP got
   2241 			 * up.
   2242 			 */
   2243 			sp->pp_flags |= PP_NEEDAUTH;
   2244 			continue;
   2245 		default:
   2246 			/* Others not supported. */
   2247 			if (debug)
   2248 				addlog(" [rej]");
   2249 			break;
   2250 		}
   2251 		/* Add the option to rejected list. */
   2252 		bcopy (p, r, p[1]);
   2253 		r += p[1];
   2254 		rlen += p[1];
   2255 	}
   2256 	if (rlen) {
   2257 		if (debug)
   2258 			addlog(" send conf-rej\n");
   2259 		sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
   2260 		goto end;
   2261 	} else if (debug)
   2262 		addlog("\n");
   2263 
   2264 	/*
   2265 	 * pass 2: check for option values that are unacceptable and
   2266 	 * thus require to be nak'ed.
   2267 	 */
   2268 	if (debug)
   2269 		log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
   2270 		    SPP_ARGS(ifp));
   2271 
   2272 	p = (void*) (h+1);
   2273 	len = origlen;
   2274 	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
   2275 		if (debug)
   2276 			addlog(" %s", sppp_lcp_opt_name(*p));
   2277 		switch (*p) {
   2278 		case LCP_OPT_MAGIC:
   2279 			/* Magic number -- extract. */
   2280 			nmagic = (u_long)p[2] << 24 |
   2281 				(u_long)p[3] << 16 | p[4] << 8 | p[5];
   2282 			if (nmagic != sp->lcp.magic) {
   2283 				if (debug)
   2284 					addlog(" 0x%lx", nmagic);
   2285 				continue;
   2286 			}
   2287 			/*
   2288 			 * Local and remote magics equal -- loopback?
   2289 			 */
   2290 			if (sp->pp_loopcnt >= MAXALIVECNT*5) {
   2291 				printf (SPP_FMT "loopback\n",
   2292 					SPP_ARGS(ifp));
   2293 				sp->pp_loopcnt = 0;
   2294 				if (ifp->if_flags & IFF_UP) {
   2295 					if_down(ifp);
   2296 					IFQ_PURGE(&sp->pp_cpq);
   2297 					/* XXX ? */
   2298 					lcp.Down(sp);
   2299 					lcp.Up(sp);
   2300 				}
   2301 			} else if (debug)
   2302 				addlog(" [glitch]");
   2303 			++sp->pp_loopcnt;
   2304 			/*
   2305 			 * We negate our magic here, and NAK it.  If
   2306 			 * we see it later in an NAK packet, we
   2307 			 * suggest a new one.
   2308 			 */
   2309 			nmagic = ~sp->lcp.magic;
   2310 			/* Gonna NAK it. */
   2311 			p[2] = nmagic >> 24;
   2312 			p[3] = nmagic >> 16;
   2313 			p[4] = nmagic >> 8;
   2314 			p[5] = nmagic;
   2315 			break;
   2316 
   2317 		case LCP_OPT_ASYNC_MAP:
   2318 			/* Async control character map -- check to be zero. */
   2319 			if (! p[2] && ! p[3] && ! p[4] && ! p[5]) {
   2320 				if (debug)
   2321 					addlog(" [empty]");
   2322 				continue;
   2323 			}
   2324 			if (debug)
   2325 				addlog(" [non-empty]");
   2326 			/* suggest a zero one */
   2327 			p[2] = p[3] = p[4] = p[5] = 0;
   2328 			break;
   2329 
   2330 		case LCP_OPT_MRU:
   2331 			/*
   2332 			 * Maximum receive unit.  Always agreeable,
   2333 			 * but ignored by now.
   2334 			 */
   2335 			sp->lcp.their_mru = p[2] * 256 + p[3];
   2336 			if (debug)
   2337 				addlog(" %ld", sp->lcp.their_mru);
   2338 			continue;
   2339 
   2340 		case LCP_OPT_AUTH_PROTO:
   2341 			authproto = (p[2] << 8) + p[3];
   2342 			if (sp->myauth.proto != authproto) {
   2343 				/* not agreed, nak */
   2344 				if (debug)
   2345 					addlog(" [mine %s != his %s]",
   2346 					       sppp_proto_name(sp->hisauth.proto),
   2347 					       sppp_proto_name(authproto));
   2348 				p[2] = sp->myauth.proto >> 8;
   2349 				p[3] = sp->myauth.proto;
   2350 				break;
   2351 			}
   2352 			if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
   2353 				if (debug)
   2354 					addlog(" [chap not MD5]");
   2355 				p[4] = CHAP_MD5;
   2356 				break;
   2357 			}
   2358 			continue;
   2359 		}
   2360 		/* Add the option to nak'ed list. */
   2361 		bcopy (p, r, p[1]);
   2362 		r += p[1];
   2363 		rlen += p[1];
   2364 	}
   2365 	if (rlen) {
   2366 		if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
   2367 			if (debug)
   2368 				addlog(" max_failure (%d) exceeded, "
   2369 				       "send conf-rej\n",
   2370 				       sp->lcp.max_failure);
   2371 			sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
   2372 		} else {
   2373 			if (debug)
   2374 				addlog(" send conf-nak\n");
   2375 			sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
   2376 		}
   2377 		goto end;
   2378 	} else {
   2379 		if (debug)
   2380 			addlog(" send conf-ack\n");
   2381 		sp->fail_counter[IDX_LCP] = 0;
   2382 		sp->pp_loopcnt = 0;
   2383 		sppp_cp_send (sp, PPP_LCP, CONF_ACK,
   2384 			      h->ident, origlen, h+1);
   2385 	}
   2386 
   2387  end:
   2388 	free (buf, M_TEMP);
   2389 	return (rlen == 0);
   2390 }
   2391 
   2392 /*
   2393  * Analyze the LCP Configure-Reject option list, and adjust our
   2394  * negotiation.
   2395  */
   2396 static void
   2397 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
   2398 {
   2399 	STDDCL;
   2400 	u_char *buf, *p;
   2401 
   2402 	len -= 4;
   2403 	buf = malloc (len, M_TEMP, M_NOWAIT);
   2404 	if (!buf)
   2405 		return;
   2406 
   2407 	if (debug)
   2408 		log(LOG_DEBUG, SPP_FMT "lcp rej opts:",
   2409 		    SPP_ARGS(ifp));
   2410 
   2411 	p = (void*) (h+1);
   2412 	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
   2413 		if (debug)
   2414 			addlog(" %s", sppp_lcp_opt_name(*p));
   2415 		switch (*p) {
   2416 		case LCP_OPT_MAGIC:
   2417 			/* Magic number -- can't use it, use 0 */
   2418 			sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
   2419 			sp->lcp.magic = 0;
   2420 			break;
   2421 		case LCP_OPT_MRU:
   2422 			/*
   2423 			 * Should not be rejected anyway, since we only
   2424 			 * negotiate a MRU if explicitly requested by
   2425 			 * peer.
   2426 			 */
   2427 			sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
   2428 			break;
   2429 		case LCP_OPT_AUTH_PROTO:
   2430 			/*
   2431 			 * Peer doesn't want to authenticate himself,
   2432 			 * deny unless this is a dialout call, and
   2433 			 * AUTHFLAG_NOCALLOUT is set.
   2434 			 */
   2435 			if ((sp->pp_flags & PP_CALLIN) == 0 &&
   2436 			    (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
   2437 				if (debug)
   2438 					addlog(" [don't insist on auth "
   2439 					       "for callout]");
   2440 				sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
   2441 				break;
   2442 			}
   2443 			if (debug)
   2444 				addlog("[access denied]\n");
   2445 			lcp.Close(sp);
   2446 			break;
   2447 		}
   2448 	}
   2449 	if (debug)
   2450 		addlog("\n");
   2451 	free (buf, M_TEMP);
   2452 	return;
   2453 }
   2454 
   2455 /*
   2456  * Analyze the LCP Configure-NAK option list, and adjust our
   2457  * negotiation.
   2458  */
   2459 static void
   2460 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
   2461 {
   2462 	STDDCL;
   2463 	u_char *buf, *p;
   2464 	u_long magic;
   2465 
   2466 	len -= 4;
   2467 	buf = malloc (len, M_TEMP, M_NOWAIT);
   2468 	if (!buf)
   2469 		return;
   2470 
   2471 	if (debug)
   2472 		log(LOG_DEBUG, SPP_FMT "lcp nak opts:",
   2473 		    SPP_ARGS(ifp));
   2474 
   2475 	p = (void*) (h+1);
   2476 	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
   2477 		if (debug)
   2478 			addlog(" %s", sppp_lcp_opt_name(*p));
   2479 		switch (*p) {
   2480 		case LCP_OPT_MAGIC:
   2481 			/* Magic number -- renegotiate */
   2482 			if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
   2483 			    len >= 6 && p[1] == 6) {
   2484 				magic = (u_long)p[2] << 24 |
   2485 					(u_long)p[3] << 16 | p[4] << 8 | p[5];
   2486 				/*
   2487 				 * If the remote magic is our negated one,
   2488 				 * this looks like a loopback problem.
   2489 				 * Suggest a new magic to make sure.
   2490 				 */
   2491 				if (magic == ~sp->lcp.magic) {
   2492 					if (debug)
   2493 						addlog(" magic glitch");
   2494 #if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD__ >= 3)
   2495 					sp->lcp.magic = random();
   2496 #else
   2497 					sp->lcp.magic = time.tv_sec + time.tv_usec;
   2498 #endif
   2499 				} else {
   2500 					sp->lcp.magic = magic;
   2501 					if (debug)
   2502 						addlog(" %ld", magic);
   2503 				}
   2504 			}
   2505 			break;
   2506 		case LCP_OPT_MRU:
   2507 			/*
   2508 			 * Peer wants to advise us to negotiate an MRU.
   2509 			 * Agree on it if it's reasonable, or use
   2510 			 * default otherwise.
   2511 			 */
   2512 			if (len >= 4 && p[1] == 4) {
   2513 				u_int mru = p[2] * 256 + p[3];
   2514 				if (debug)
   2515 					addlog(" %d", mru);
   2516 				if (mru < PP_MTU || mru > PP_MAX_MRU)
   2517 					mru = PP_MTU;
   2518 				sp->lcp.mru = mru;
   2519 				sp->lcp.opts |= (1 << LCP_OPT_MRU);
   2520 			}
   2521 			break;
   2522 		case LCP_OPT_AUTH_PROTO:
   2523 			/*
   2524 			 * Peer doesn't like our authentication method,
   2525 			 * deny.
   2526 			 */
   2527 			if (debug)
   2528 				addlog("[access denied]\n");
   2529 			lcp.Close(sp);
   2530 			break;
   2531 		}
   2532 	}
   2533 	if (debug)
   2534 		addlog("\n");
   2535 	free (buf, M_TEMP);
   2536 	return;
   2537 }
   2538 
   2539 static void
   2540 sppp_lcp_tlu(struct sppp *sp)
   2541 {
   2542 	STDDCL;
   2543 	int i;
   2544 	u_long mask;
   2545 
   2546 	/* XXX ? */
   2547 	if (! (ifp->if_flags & IFF_UP) &&
   2548 	    (ifp->if_flags & IFF_RUNNING)) {
   2549 		/* Coming out of loopback mode. */
   2550 		if_up(ifp);
   2551 		printf (SPP_FMT "up\n", SPP_ARGS(ifp));
   2552 	}
   2553 
   2554 	for (i = 0; i < IDX_COUNT; i++)
   2555 		if ((cps[i])->flags & CP_QUAL)
   2556 			(cps[i])->Open(sp);
   2557 
   2558 	if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
   2559 	    (sp->pp_flags & PP_NEEDAUTH) != 0)
   2560 		sp->pp_phase = PHASE_AUTHENTICATE;
   2561 	else
   2562 		sp->pp_phase = PHASE_NETWORK;
   2563 
   2564 	if(debug)
   2565 	{
   2566 		log(LOG_INFO, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
   2567 		    sppp_phase_name(sp->pp_phase));
   2568 	}
   2569 
   2570 	/*
   2571 	 * Open all authentication protocols.  This is even required
   2572 	 * if we already proceeded to network phase, since it might be
   2573 	 * that remote wants us to authenticate, so we might have to
   2574 	 * send a PAP request.  Undesired authentication protocols
   2575 	 * don't do anything when they get an Open event.
   2576 	 */
   2577 	for (i = 0; i < IDX_COUNT; i++)
   2578 		if ((cps[i])->flags & CP_AUTH)
   2579 			(cps[i])->Open(sp);
   2580 
   2581 	if (sp->pp_phase == PHASE_NETWORK) {
   2582 		/* Notify all NCPs. */
   2583 		for (i = 0; i < IDX_COUNT; i++)
   2584 			if ((cps[i])->flags & CP_NCP)
   2585 				(cps[i])->Open(sp);
   2586 	}
   2587 
   2588 	/* Send Up events to all started protos. */
   2589 	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
   2590 		if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
   2591 			(cps[i])->Up(sp);
   2592 
   2593 	/* notify low-level driver of state change */
   2594 	if (sp->pp_chg)
   2595 		sp->pp_chg(sp, (int)sp->pp_phase);
   2596 
   2597 	if (sp->pp_phase == PHASE_NETWORK)
   2598 		/* if no NCP is starting, close down */
   2599 		sppp_lcp_check_and_close(sp);
   2600 }
   2601 
   2602 static void
   2603 sppp_lcp_tld(struct sppp *sp)
   2604 {
   2605 	STDDCL;
   2606 	int i;
   2607 	u_long mask;
   2608 
   2609 	sp->pp_phase = PHASE_TERMINATE;
   2610 
   2611 	if(debug)
   2612 	{
   2613 		log(LOG_INFO, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
   2614 			sppp_phase_name(sp->pp_phase));
   2615 	}
   2616 
   2617 	/*
   2618 	 * Take upper layers down.  We send the Down event first and
   2619 	 * the Close second to prevent the upper layers from sending
   2620 	 * ``a flurry of terminate-request packets'', as the RFC
   2621 	 * describes it.
   2622 	 */
   2623 	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
   2624 		if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
   2625 			(cps[i])->Down(sp);
   2626 			(cps[i])->Close(sp);
   2627 		}
   2628 }
   2629 
   2630 static void
   2631 sppp_lcp_tls(struct sppp *sp)
   2632 {
   2633 	STDDCL;
   2634 
   2635 	sp->pp_phase = PHASE_ESTABLISH;
   2636 
   2637 	if(debug)
   2638 	{
   2639 		log(LOG_INFO, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
   2640 			sppp_phase_name(sp->pp_phase));
   2641 	}
   2642 
   2643 	/* Notify lower layer if desired. */
   2644 	if (sp->pp_tls)
   2645 		(sp->pp_tls)(sp);
   2646 }
   2647 
   2648 static void
   2649 sppp_lcp_tlf(struct sppp *sp)
   2650 {
   2651 	STDDCL;
   2652 
   2653 	sp->pp_phase = PHASE_DEAD;
   2654 
   2655 	if(debug)
   2656 	{
   2657 		log(LOG_INFO, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
   2658 			sppp_phase_name(sp->pp_phase));
   2659 	}
   2660 
   2661 	/* Notify lower layer if desired. */
   2662 	if (sp->pp_tlf)
   2663 		(sp->pp_tlf)(sp);
   2664 }
   2665 
   2666 static void
   2667 sppp_lcp_scr(struct sppp *sp)
   2668 {
   2669 	char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
   2670 	int i = 0;
   2671 	u_short authproto;
   2672 
   2673 	if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
   2674 		if (! sp->lcp.magic)
   2675 #if defined(__NetBSD__) || (defined(__FreeBSD__) && __FreeBSD__ >= 3)
   2676 			sp->lcp.magic = random();
   2677 #else
   2678 			sp->lcp.magic = time.tv_sec + time.tv_usec;
   2679 #endif
   2680 		opt[i++] = LCP_OPT_MAGIC;
   2681 		opt[i++] = 6;
   2682 		opt[i++] = sp->lcp.magic >> 24;
   2683 		opt[i++] = sp->lcp.magic >> 16;
   2684 		opt[i++] = sp->lcp.magic >> 8;
   2685 		opt[i++] = sp->lcp.magic;
   2686 	}
   2687 
   2688 	if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
   2689 		opt[i++] = LCP_OPT_MRU;
   2690 		opt[i++] = 4;
   2691 		opt[i++] = sp->lcp.mru >> 8;
   2692 		opt[i++] = sp->lcp.mru;
   2693 	}
   2694 
   2695 	if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
   2696 		authproto = sp->hisauth.proto;
   2697 		opt[i++] = LCP_OPT_AUTH_PROTO;
   2698 		opt[i++] = authproto == PPP_CHAP? 5: 4;
   2699 		opt[i++] = authproto >> 8;
   2700 		opt[i++] = authproto;
   2701 		if (authproto == PPP_CHAP)
   2702 			opt[i++] = CHAP_MD5;
   2703 	}
   2704 
   2705 	sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
   2706 	sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
   2707 }
   2708 
   2709 /*
   2710  * Check the open NCPs, return true if at least one NCP is open.
   2711  */
   2712 static int
   2713 sppp_ncp_check(struct sppp *sp)
   2714 {
   2715 	int i, mask;
   2716 
   2717 	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
   2718 		if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
   2719 			return 1;
   2720 	return 0;
   2721 }
   2722 
   2723 /*
   2724  * Re-check the open NCPs and see if we should terminate the link.
   2725  * Called by the NCPs during their tlf action handling.
   2726  */
   2727 static void
   2728 sppp_lcp_check_and_close(struct sppp *sp)
   2729 {
   2730 
   2731 	if (sp->pp_phase < PHASE_NETWORK)
   2732 		/* don't bother, we are already going down */
   2733 		return;
   2734 
   2735 	if (sppp_ncp_check(sp))
   2736 		return;
   2737 
   2738 	lcp.Close(sp);
   2739 }
   2740 
   2741 
   2742 /*
   2744  *--------------------------------------------------------------------------*
   2745  *                                                                          *
   2746  *                        The IPCP implementation.                          *
   2747  *                                                                          *
   2748  *--------------------------------------------------------------------------*
   2749  */
   2750 
   2751 static void
   2752 sppp_ipcp_init(struct sppp *sp)
   2753 {
   2754 	sp->ipcp.opts = 0;
   2755 	sp->ipcp.flags = 0;
   2756 	sp->state[IDX_IPCP] = STATE_INITIAL;
   2757 	sp->fail_counter[IDX_IPCP] = 0;
   2758 	sp->pp_seq[IDX_IPCP] = 0;
   2759 	sp->pp_rseq[IDX_IPCP] = 0;
   2760 #if defined(__NetBSD__)
   2761 	callout_init(&sp->ch[IDX_IPCP]);
   2762 #endif
   2763 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   2764 	callout_handle_init(&sp->ch[IDX_IPCP]);
   2765 #endif
   2766 }
   2767 
   2768 static void
   2769 sppp_ipcp_up(struct sppp *sp)
   2770 {
   2771 	sppp_up_event(&ipcp, sp);
   2772 }
   2773 
   2774 static void
   2775 sppp_ipcp_down(struct sppp *sp)
   2776 {
   2777 	sppp_down_event(&ipcp, sp);
   2778 }
   2779 
   2780 static void
   2781 sppp_ipcp_open(struct sppp *sp)
   2782 {
   2783 	STDDCL;
   2784 	u_long myaddr, hisaddr;
   2785 
   2786 	sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN|IPCP_MYADDR_SEEN|IPCP_MYADDR_DYN);
   2787 
   2788 	sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
   2789 	/*
   2790 	 * If we don't have his address, this probably means our
   2791 	 * interface doesn't want to talk IP at all.  (This could
   2792 	 * be the case if somebody wants to speak only IPX, for
   2793 	 * example.)  Don't open IPCP in this case.
   2794 	 */
   2795 	if (hisaddr == 0L) {
   2796 		/* XXX this message should go away */
   2797 		if (debug)
   2798 			log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
   2799 			    SPP_ARGS(ifp));
   2800 		return;
   2801 	}
   2802 
   2803 	if (myaddr == 0L) {
   2804 		/*
   2805 		 * I don't have an assigned address, so i need to
   2806 		 * negotiate my address.
   2807 		 */
   2808 		sp->ipcp.flags |= IPCP_MYADDR_DYN;
   2809 		sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
   2810 	} else
   2811 		sp->ipcp.flags |= IPCP_MYADDR_SEEN;
   2812 	sppp_open_event(&ipcp, sp);
   2813 }
   2814 
   2815 static void
   2816 sppp_ipcp_close(struct sppp *sp)
   2817 {
   2818 	sppp_close_event(&ipcp, sp);
   2819 	if (sp->ipcp.flags & IPCP_MYADDR_DYN)
   2820 		/*
   2821 		 * My address was dynamic, clear it again.
   2822 		 */
   2823 		sppp_set_ip_addr(sp, 0L);
   2824 }
   2825 
   2826 static void
   2827 sppp_ipcp_TO(void *cookie)
   2828 {
   2829 	sppp_to_event(&ipcp, (struct sppp *)cookie);
   2830 }
   2831 
   2832 /*
   2833  * Analyze a configure request.  Return true if it was agreeable, and
   2834  * caused action sca, false if it has been rejected or nak'ed, and
   2835  * caused action scn.  (The return value is used to make the state
   2836  * transition decision in the state automaton.)
   2837  */
   2838 static int
   2839 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
   2840 {
   2841 	u_char *buf, *r, *p;
   2842 	struct ifnet *ifp = &sp->pp_if;
   2843 	int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
   2844 	u_long hisaddr, desiredaddr;
   2845 	int gotmyaddr = 0;
   2846 
   2847 	len -= 4;
   2848 	origlen = len;
   2849 	/*
   2850 	 * Make sure to allocate a buf that can at least hold a
   2851 	 * conf-nak with an `address' option.  We might need it below.
   2852 	 */
   2853 	buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
   2854 	if (! buf)
   2855 		return (0);
   2856 
   2857 	/* pass 1: see if we can recognize them */
   2858 	if (debug)
   2859 		log(LOG_DEBUG, SPP_FMT "ipcp parse opts:",
   2860 		    SPP_ARGS(ifp));
   2861 	p = (void*) (h+1);
   2862 	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
   2863 		if (debug)
   2864 			addlog(" %s", sppp_ipcp_opt_name(*p));
   2865 		switch (*p) {
   2866 #ifdef notyet
   2867 		case IPCP_OPT_COMPRESSION:
   2868 			if (len >= 6 && p[1] >= 6) {
   2869 				/* correctly formed compress option */
   2870 				continue;
   2871 			}
   2872 			if (debug)
   2873 				addlog(" [invalid]");
   2874 			break;
   2875 #endif
   2876 		case IPCP_OPT_ADDRESS:
   2877 			if (len >= 6 && p[1] == 6) {
   2878 				/* correctly formed address option */
   2879 				continue;
   2880 			}
   2881 			if (debug)
   2882 				addlog(" [invalid]");
   2883 			break;
   2884 		default:
   2885 			/* Others not supported. */
   2886 			if (debug)
   2887 				addlog(" [rej]");
   2888 			break;
   2889 		}
   2890 		/* Add the option to rejected list. */
   2891 		bcopy (p, r, p[1]);
   2892 		r += p[1];
   2893 		rlen += p[1];
   2894 	}
   2895 	if (rlen) {
   2896 		if (debug)
   2897 			addlog(" send conf-rej\n");
   2898 		sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
   2899 		goto end;
   2900 	} else if (debug)
   2901 		addlog("\n");
   2902 
   2903 	/* pass 2: parse option values */
   2904 	sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
   2905 	if (debug)
   2906 		log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
   2907 		       SPP_ARGS(ifp));
   2908 	p = (void*) (h+1);
   2909 	len = origlen;
   2910 	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
   2911 		if (debug)
   2912 			addlog(" %s", sppp_ipcp_opt_name(*p));
   2913 		switch (*p) {
   2914 #ifdef notyet
   2915 		case IPCP_OPT_COMPRESSION:
   2916 			continue;
   2917 #endif
   2918 		case IPCP_OPT_ADDRESS:
   2919 			desiredaddr = p[2] << 24 | p[3] << 16 |
   2920 				p[4] << 8 | p[5];
   2921 			if (!(sp->ipcp.flags & IPCP_MYADDR_SEEN) &&
   2922 			        (sp->ipcp.flags & IPCP_MYADDR_DYN)) {
   2923 				/*
   2924 				 * hopefully this is our address !!
   2925 				 */
   2926 			 	if (debug)
   2927 					addlog(" [wantmyaddr %s]",
   2928 						sppp_dotted_quad(desiredaddr));
   2929 				/*
   2930 				 * When doing dynamic address assignment,
   2931 			   	 * we accept his offer.  Otherwise, we
   2932 			    	 * ignore it and thus continue to negotiate
   2933 			     	 * our already existing value.
   2934 		      		 */
   2935 				sppp_set_ip_addr(sp, desiredaddr);
   2936 				if (debug)
   2937 					addlog(" [agree]");
   2938 				sp->ipcp.flags |= IPCP_MYADDR_SEEN;
   2939 				gotmyaddr++;
   2940 				continue;
   2941 			} else {
   2942 				if (desiredaddr == hisaddr ||
   2943 			    	(hisaddr == 1 && desiredaddr != 0)) {
   2944 					/*
   2945 				 	* Peer's address is same as our value,
   2946 				 	* this is agreeable.  Gonna conf-ack
   2947 				 	* it.
   2948 				 	*/
   2949 					if (debug)
   2950 						addlog(" %s [ack]",
   2951 					       		sppp_dotted_quad(hisaddr));
   2952 					/* record that we've seen it already */
   2953 					sp->ipcp.flags |= IPCP_HISADDR_SEEN;
   2954 					continue;
   2955 				}
   2956 				/*
   2957 			 	* The address wasn't agreeable.  This is either
   2958 			 	* he sent us 0.0.0.0, asking to assign him an
   2959 			 	* address, or he send us another address not
   2960 			 	* matching our value.  Either case, we gonna
   2961 			 	* conf-nak it with our value.
   2962 			 	*/
   2963 				if (debug) {
   2964 					if (desiredaddr == 0)
   2965 						addlog(" [addr requested]");
   2966 					else
   2967 						addlog(" %s [not agreed]",
   2968 					       		sppp_dotted_quad(desiredaddr));
   2969 				}
   2970 
   2971 				p[2] = hisaddr >> 24;
   2972 				p[3] = hisaddr >> 16;
   2973 				p[4] = hisaddr >> 8;
   2974 				p[5] = hisaddr;
   2975 				break;
   2976 			}
   2977 		}
   2978 		/* Add the option to nak'ed list. */
   2979 		bcopy (p, r, p[1]);
   2980 		r += p[1];
   2981 		rlen += p[1];
   2982 	}
   2983 
   2984 	/*
   2985 	 * If we are about to conf-ack the request, but haven't seen
   2986 	 * his address so far, gonna conf-nak it instead, with the
   2987 	 * `address' option present and our idea of his address being
   2988 	 * filled in there, to request negotiation of both addresses.
   2989 	 *
   2990 	 * XXX This can result in an endless req - nak loop if peer
   2991 	 * doesn't want to send us his address.  Q: What should we do
   2992 	 * about it?  XXX  A: implement the max-failure counter.
   2993 	 */
   2994 	if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
   2995 		buf[0] = IPCP_OPT_ADDRESS;
   2996 		buf[1] = 6;
   2997 		buf[2] = hisaddr >> 24;
   2998 		buf[3] = hisaddr >> 16;
   2999 		buf[4] = hisaddr >> 8;
   3000 		buf[5] = hisaddr;
   3001 		rlen = 6;
   3002 		if (debug)
   3003 			addlog(" still need hisaddr");
   3004 	}
   3005 
   3006 	if (rlen) {
   3007 		if (debug)
   3008 			addlog(" send conf-nak\n");
   3009 		sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
   3010 	} else {
   3011 		if (debug)
   3012 			addlog(" send conf-ack\n");
   3013 		sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
   3014 			      h->ident, origlen, h+1);
   3015 	}
   3016 
   3017  end:
   3018 	free (buf, M_TEMP);
   3019 	return (rlen == 0);
   3020 }
   3021 
   3022 /*
   3023  * Analyze the IPCP Configure-Reject option list, and adjust our
   3024  * negotiation.
   3025  */
   3026 static void
   3027 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
   3028 {
   3029 	u_char *buf, *p;
   3030 	struct ifnet *ifp = &sp->pp_if;
   3031 	int debug = ifp->if_flags & IFF_DEBUG;
   3032 
   3033 	len -= 4;
   3034 	buf = malloc (len, M_TEMP, M_NOWAIT);
   3035 	if (!buf)
   3036 		return;
   3037 
   3038 	if (debug)
   3039 		log(LOG_DEBUG, SPP_FMT "ipcp rej opts:",
   3040 		    SPP_ARGS(ifp));
   3041 
   3042 	p = (void*) (h+1);
   3043 	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
   3044 		if (debug)
   3045 			addlog(" %s", sppp_ipcp_opt_name(*p));
   3046 		switch (*p) {
   3047 		case IPCP_OPT_ADDRESS:
   3048 			/*
   3049 			 * Peer doesn't grok address option.  This is
   3050 			 * bad.  XXX  Should we better give up here?
   3051 			 */
   3052 			sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
   3053 			break;
   3054 #ifdef notyet
   3055 		case IPCP_OPT_COMPRESS:
   3056 			sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
   3057 			break;
   3058 #endif
   3059 		}
   3060 	}
   3061 	if (debug)
   3062 		addlog("\n");
   3063 	free (buf, M_TEMP);
   3064 	return;
   3065 }
   3066 
   3067 /*
   3068  * Analyze the IPCP Configure-NAK option list, and adjust our
   3069  * negotiation.
   3070  */
   3071 static void
   3072 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
   3073 {
   3074 	u_char *buf, *p;
   3075 	struct ifnet *ifp = &sp->pp_if;
   3076 	int debug = ifp->if_flags & IFF_DEBUG;
   3077 	u_long wantaddr;
   3078 
   3079 	len -= 4;
   3080 	buf = malloc (len, M_TEMP, M_NOWAIT);
   3081 	if (!buf)
   3082 		return;
   3083 
   3084 	if (debug)
   3085 		log(LOG_DEBUG, SPP_FMT "ipcp nak opts:",
   3086 		    SPP_ARGS(ifp));
   3087 
   3088 	p = (void*) (h+1);
   3089 	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
   3090 		if (debug)
   3091 			addlog(" %s", sppp_ipcp_opt_name(*p));
   3092 		switch (*p) {
   3093 		case IPCP_OPT_ADDRESS:
   3094 			/*
   3095 			 * Peer doesn't like our local IP address.  See
   3096 			 * if we can do something for him.  We'll drop
   3097 			 * him our address then.
   3098 			 */
   3099 			if (len >= 6 && p[1] == 6) {
   3100 				wantaddr = p[2] << 24 | p[3] << 16 |
   3101 					p[4] << 8 | p[5];
   3102 				sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
   3103 				if (debug)
   3104 					addlog(" [wantaddr %s]",
   3105 					       sppp_dotted_quad(wantaddr));
   3106 				/*
   3107 				 * When doing dynamic address assignment,
   3108 				 * we accept his offer.  Otherwise, we
   3109 				 * ignore it and thus continue to negotiate
   3110 				 * our already existing value.
   3111 				 */
   3112 				if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
   3113 					sppp_set_ip_addr(sp, wantaddr);
   3114 					if (debug)
   3115 						addlog(" [agree]");
   3116 					sp->ipcp.flags |= IPCP_MYADDR_SEEN;
   3117 				}
   3118 			}
   3119 			break;
   3120 #ifdef notyet
   3121 		case IPCP_OPT_COMPRESS:
   3122 			/*
   3123 			 * Peer wants different compression parameters.
   3124 			 */
   3125 			break;
   3126 #endif
   3127 		}
   3128 	}
   3129 	if (debug)
   3130 		addlog("\n");
   3131 	free (buf, M_TEMP);
   3132 	return;
   3133 }
   3134 
   3135 static void
   3136 sppp_ipcp_tlu(struct sppp *sp)
   3137 {
   3138 	/* we are up - notify isdn daemon */
   3139 	if (sp->pp_con)
   3140 		sp->pp_con(sp);
   3141 }
   3142 
   3143 static void
   3144 sppp_ipcp_tld(struct sppp *sp)
   3145 {
   3146 }
   3147 
   3148 static void
   3149 sppp_ipcp_tls(struct sppp *sp)
   3150 {
   3151 	/* indicate to LCP that it must stay alive */
   3152 	sp->lcp.protos |= (1 << IDX_IPCP);
   3153 }
   3154 
   3155 static void
   3156 sppp_ipcp_tlf(struct sppp *sp)
   3157 {
   3158 	/* we no longer need LCP */
   3159 	sp->lcp.protos &= ~(1 << IDX_IPCP);
   3160 }
   3161 
   3162 static void
   3163 sppp_ipcp_scr(struct sppp *sp)
   3164 {
   3165 	char opt[6 /* compression */ + 6 /* address */];
   3166 	u_long ouraddr;
   3167 	int i = 0;
   3168 
   3169 #ifdef notyet
   3170 	if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
   3171 		opt[i++] = IPCP_OPT_COMPRESSION;
   3172 		opt[i++] = 6;
   3173 		opt[i++] = 0;	/* VJ header compression */
   3174 		opt[i++] = 0x2d; /* VJ header compression */
   3175 		opt[i++] = max_slot_id;
   3176 		opt[i++] = comp_slot_id;
   3177 	}
   3178 #endif
   3179 
   3180 	if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
   3181 		sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
   3182 		opt[i++] = IPCP_OPT_ADDRESS;
   3183 		opt[i++] = 6;
   3184 		opt[i++] = ouraddr >> 24;
   3185 		opt[i++] = ouraddr >> 16;
   3186 		opt[i++] = ouraddr >> 8;
   3187 		opt[i++] = ouraddr;
   3188 	}
   3189 
   3190 	sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
   3191 	sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
   3192 }
   3193 
   3194 
   3195 /*
   3197  *--------------------------------------------------------------------------*
   3198  *                                                                          *
   3199  *                      The IPv6CP implementation.                          *
   3200  *                                                                          *
   3201  *--------------------------------------------------------------------------*
   3202  */
   3203 
   3204 #ifdef INET6
   3205 static void
   3206 sppp_ipv6cp_init(struct sppp *sp)
   3207 {
   3208 	sp->ipv6cp.opts = 0;
   3209 	sp->ipv6cp.flags = 0;
   3210 	sp->state[IDX_IPV6CP] = STATE_INITIAL;
   3211 	sp->fail_counter[IDX_IPV6CP] = 0;
   3212 	sp->pp_seq[IDX_IPV6CP] = 0;
   3213 	sp->pp_rseq[IDX_IPV6CP] = 0;
   3214 #if defined(__NetBSD__)
   3215 	callout_init(&sp->ch[IDX_IPV6CP]);
   3216 #endif
   3217 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   3218 	callout_handle_init(&sp->ch[IDX_IPV6CP]);
   3219 #endif
   3220 }
   3221 
   3222 static void
   3223 sppp_ipv6cp_up(struct sppp *sp)
   3224 {
   3225 	sppp_up_event(&ipv6cp, sp);
   3226 }
   3227 
   3228 static void
   3229 sppp_ipv6cp_down(struct sppp *sp)
   3230 {
   3231 	sppp_down_event(&ipv6cp, sp);
   3232 }
   3233 
   3234 static void
   3235 sppp_ipv6cp_open(struct sppp *sp)
   3236 {
   3237 	STDDCL;
   3238 	struct in6_addr myaddr, hisaddr;
   3239 
   3240 #ifdef IPV6CP_MYIFID_DYN
   3241 	sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
   3242 #else
   3243 	sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
   3244 #endif
   3245 
   3246 	sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
   3247 	/*
   3248 	 * If we don't have our address, this probably means our
   3249 	 * interface doesn't want to talk IPv6 at all.  (This could
   3250 	 * be the case if somebody wants to speak only IPX, for
   3251 	 * example.)  Don't open IPv6CP in this case.
   3252 	 */
   3253 	if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
   3254 		/* XXX this message should go away */
   3255 		if (debug)
   3256 			log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
   3257 			    SPP_ARGS(ifp));
   3258 		return;
   3259 	}
   3260 
   3261 	sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
   3262 	sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
   3263 	sppp_open_event(&ipv6cp, sp);
   3264 }
   3265 
   3266 static void
   3267 sppp_ipv6cp_close(struct sppp *sp)
   3268 {
   3269 	sppp_close_event(&ipv6cp, sp);
   3270 }
   3271 
   3272 static void
   3273 sppp_ipv6cp_TO(void *cookie)
   3274 {
   3275 	sppp_to_event(&ipv6cp, (struct sppp *)cookie);
   3276 }
   3277 
   3278 /*
   3279  * Analyze a configure request.  Return true if it was agreeable, and
   3280  * caused action sca, false if it has been rejected or nak'ed, and
   3281  * caused action scn.  (The return value is used to make the state
   3282  * transition decision in the state automaton.)
   3283  */
   3284 static int
   3285 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
   3286 {
   3287 	u_char *buf, *r, *p;
   3288 	struct ifnet *ifp = &sp->pp_if;
   3289 	int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
   3290 	struct in6_addr myaddr, desiredaddr, suggestaddr;
   3291 	int ifidcount;
   3292 	int type;
   3293 	int collision, nohisaddr;
   3294 
   3295 	len -= 4;
   3296 	origlen = len;
   3297 	/*
   3298 	 * Make sure to allocate a buf that can at least hold a
   3299 	 * conf-nak with an `address' option.  We might need it below.
   3300 	 */
   3301 	buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
   3302 	if (! buf)
   3303 		return (0);
   3304 
   3305 	/* pass 1: see if we can recognize them */
   3306 	if (debug)
   3307 		log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
   3308 		    SPP_ARGS(ifp));
   3309 	p = (void*) (h+1);
   3310 	ifidcount = 0;
   3311 	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
   3312 		if (debug)
   3313 			addlog(" %s", sppp_ipv6cp_opt_name(*p));
   3314 		switch (*p) {
   3315 		case IPV6CP_OPT_IFID:
   3316 			if (len >= 10 && p[1] == 10 && ifidcount == 0) {
   3317 				/* correctly formed address option */
   3318 				ifidcount++;
   3319 				continue;
   3320 			}
   3321 			if (debug)
   3322 				addlog(" [invalid]");
   3323 			break;
   3324 #ifdef notyet
   3325 		case IPV6CP_OPT_COMPRESSION:
   3326 			if (len >= 4 && p[1] >= 4) {
   3327 				/* correctly formed compress option */
   3328 				continue;
   3329 			}
   3330 			if (debug)
   3331 				addlog(" [invalid]");
   3332 			break;
   3333 #endif
   3334 		default:
   3335 			/* Others not supported. */
   3336 			if (debug)
   3337 				addlog(" [rej]");
   3338 			break;
   3339 		}
   3340 		/* Add the option to rejected list. */
   3341 		bcopy (p, r, p[1]);
   3342 		r += p[1];
   3343 		rlen += p[1];
   3344 	}
   3345 	if (rlen) {
   3346 		if (debug)
   3347 			addlog(" send conf-rej\n");
   3348 		sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
   3349 		goto end;
   3350 	} else if (debug)
   3351 		addlog("\n");
   3352 
   3353 	/* pass 2: parse option values */
   3354 	sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
   3355 	if (debug)
   3356 		log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
   3357 		       SPP_ARGS(ifp));
   3358 	p = (void*) (h+1);
   3359 	len = origlen;
   3360 	type = CONF_ACK;
   3361 	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
   3362 		if (debug)
   3363 			addlog(" %s", sppp_ipv6cp_opt_name(*p));
   3364 		switch (*p) {
   3365 #ifdef notyet
   3366 		case IPV6CP_OPT_COMPRESSION:
   3367 			continue;
   3368 #endif
   3369 		case IPV6CP_OPT_IFID:
   3370 			bzero(&desiredaddr, sizeof(desiredaddr));
   3371 			bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
   3372 			collision = (bcmp(&desiredaddr.s6_addr[8],
   3373 					&myaddr.s6_addr[8], 8) == 0);
   3374 			nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
   3375 
   3376 			desiredaddr.s6_addr16[0] = htons(0xfe80);
   3377 			desiredaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
   3378 
   3379 			if (!collision && !nohisaddr) {
   3380 				/* no collision, hisaddr known - Conf-Ack */
   3381 				type = CONF_ACK;
   3382 
   3383 				if (debug) {
   3384 					addlog(" %s [%s]",
   3385 					    ip6_sprintf(&desiredaddr),
   3386 					    sppp_cp_type_name(type));
   3387 				}
   3388 				continue;
   3389 			}
   3390 
   3391 			bzero(&suggestaddr, sizeof(&suggestaddr));
   3392 			if (collision && nohisaddr) {
   3393 				/* collision, hisaddr unknown - Conf-Rej */
   3394 				type = CONF_REJ;
   3395 				bzero(&p[2], 8);
   3396 			} else {
   3397 				/*
   3398 				 * - no collision, hisaddr unknown, or
   3399 				 * - collision, hisaddr known
   3400 				 * Conf-Nak, suggest hisaddr
   3401 				 */
   3402 				type = CONF_NAK;
   3403 				sppp_suggest_ip6_addr(sp, &suggestaddr);
   3404 				bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
   3405 			}
   3406 			if (debug)
   3407 				addlog(" %s [%s]", ip6_sprintf(&desiredaddr),
   3408 				    sppp_cp_type_name(type));
   3409 			break;
   3410 		}
   3411 		/* Add the option to nak'ed list. */
   3412 		bcopy (p, r, p[1]);
   3413 		r += p[1];
   3414 		rlen += p[1];
   3415 	}
   3416 
   3417 	if (rlen == 0 && type == CONF_ACK) {
   3418 		if (debug)
   3419 			addlog(" send %s\n", sppp_cp_type_name(type));
   3420 		sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
   3421 	} else {
   3422 #ifdef DIAGNOSTIC
   3423 		if (type == CONF_ACK)
   3424 			panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
   3425 #endif
   3426 
   3427 		if (debug) {
   3428 			addlog(" send %s suggest %s\n",
   3429 			    sppp_cp_type_name(type), ip6_sprintf(&suggestaddr));
   3430 		}
   3431 		sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
   3432 	}
   3433 
   3434  end:
   3435 	free (buf, M_TEMP);
   3436 	return (rlen == 0);
   3437 }
   3438 
   3439 /*
   3440  * Analyze the IPv6CP Configure-Reject option list, and adjust our
   3441  * negotiation.
   3442  */
   3443 static void
   3444 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
   3445 {
   3446 	u_char *buf, *p;
   3447 	struct ifnet *ifp = &sp->pp_if;
   3448 	int debug = ifp->if_flags & IFF_DEBUG;
   3449 
   3450 	len -= 4;
   3451 	buf = malloc (len, M_TEMP, M_NOWAIT);
   3452 	if (!buf)
   3453 		return;
   3454 
   3455 	if (debug)
   3456 		log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
   3457 		    SPP_ARGS(ifp));
   3458 
   3459 	p = (void*) (h+1);
   3460 	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
   3461 		if (debug)
   3462 			addlog(" %s", sppp_ipv6cp_opt_name(*p));
   3463 		switch (*p) {
   3464 		case IPV6CP_OPT_IFID:
   3465 			/*
   3466 			 * Peer doesn't grok address option.  This is
   3467 			 * bad.  XXX  Should we better give up here?
   3468 			 */
   3469 			sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
   3470 			break;
   3471 #ifdef notyet
   3472 		case IPV6CP_OPT_COMPRESS:
   3473 			sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
   3474 			break;
   3475 #endif
   3476 		}
   3477 	}
   3478 	if (debug)
   3479 		addlog("\n");
   3480 	free (buf, M_TEMP);
   3481 	return;
   3482 }
   3483 
   3484 /*
   3485  * Analyze the IPv6CP Configure-NAK option list, and adjust our
   3486  * negotiation.
   3487  */
   3488 static void
   3489 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
   3490 {
   3491 	u_char *buf, *p;
   3492 	struct ifnet *ifp = &sp->pp_if;
   3493 	int debug = ifp->if_flags & IFF_DEBUG;
   3494 	struct in6_addr suggestaddr;
   3495 
   3496 	len -= 4;
   3497 	buf = malloc (len, M_TEMP, M_NOWAIT);
   3498 	if (!buf)
   3499 		return;
   3500 
   3501 	if (debug)
   3502 		log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
   3503 		    SPP_ARGS(ifp));
   3504 
   3505 	p = (void*) (h+1);
   3506 	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
   3507 		if (debug)
   3508 			addlog(" %s", sppp_ipv6cp_opt_name(*p));
   3509 		switch (*p) {
   3510 		case IPV6CP_OPT_IFID:
   3511 			/*
   3512 			 * Peer doesn't like our local ifid.  See
   3513 			 * if we can do something for him.  We'll drop
   3514 			 * him our address then.
   3515 			 */
   3516 			if (len < 10 || p[1] != 10)
   3517 				break;
   3518 			bzero(&suggestaddr, sizeof(suggestaddr));
   3519 			suggestaddr.s6_addr16[0] = htons(0xfe80);
   3520 			suggestaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
   3521 			bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
   3522 
   3523 			sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
   3524 			if (debug)
   3525 				addlog(" [suggestaddr %s]",
   3526 				       ip6_sprintf(&suggestaddr));
   3527 #ifdef IPV6CP_MYIFID_DYN
   3528 			/*
   3529 			 * When doing dynamic address assignment,
   3530 			 * we accept his offer.
   3531 			 */
   3532 			if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
   3533 				struct in6_addr lastsuggest;
   3534 				/*
   3535 				 * If <suggested myaddr from peer> equals to
   3536 				 * <hisaddr we have suggested last time>,
   3537 				 * we have a collision.  generate new random
   3538 				 * ifid.
   3539 				 */
   3540 				sppp_suggest_ip6_addr(&lastsuggest);
   3541 				if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
   3542 						 lastsuggest)) {
   3543 					if (debug)
   3544 						addlog(" [random]");
   3545 					sppp_gen_ip6_addr(sp, &suggestaddr);
   3546 				}
   3547 				sppp_set_ip6_addr(sp, &suggestaddr, 0);
   3548 				if (debug)
   3549 					addlog(" [agree]");
   3550 				sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
   3551 			}
   3552 #else
   3553 			/*
   3554 			 * Since we do not do dynamic address assignment,
   3555 			 * we ignore it and thus continue to negotiate
   3556 			 * our already existing value.  This can possibly
   3557 			 * go into infinite request-reject loop.
   3558 			 *
   3559 			 * This is not likely because we normally use
   3560 			 * ifid based on MAC-address.
   3561 			 * If you have no ethernet card on the node, too bad.
   3562 			 * XXX should we use fail_counter?
   3563 			 */
   3564 #endif
   3565 			break;
   3566 #ifdef notyet
   3567 		case IPV6CP_OPT_COMPRESS:
   3568 			/*
   3569 			 * Peer wants different compression parameters.
   3570 			 */
   3571 			break;
   3572 #endif
   3573 		}
   3574 	}
   3575 	if (debug)
   3576 		addlog("\n");
   3577 	free (buf, M_TEMP);
   3578 	return;
   3579 }
   3580 
   3581 static void
   3582 sppp_ipv6cp_tlu(struct sppp *sp)
   3583 {
   3584 	/* we are up - notify isdn daemon */
   3585 	if (sp->pp_con)
   3586 		sp->pp_con(sp);
   3587 }
   3588 
   3589 static void
   3590 sppp_ipv6cp_tld(struct sppp *sp)
   3591 {
   3592 }
   3593 
   3594 static void
   3595 sppp_ipv6cp_tls(struct sppp *sp)
   3596 {
   3597 	/* indicate to LCP that it must stay alive */
   3598 	sp->lcp.protos |= (1 << IDX_IPV6CP);
   3599 }
   3600 
   3601 static void
   3602 sppp_ipv6cp_tlf(struct sppp *sp)
   3603 {
   3604 	/* we no longer need LCP */
   3605 	sp->lcp.protos &= ~(1 << IDX_IPV6CP);
   3606 }
   3607 
   3608 static void
   3609 sppp_ipv6cp_scr(struct sppp *sp)
   3610 {
   3611 	char opt[10 /* ifid */ + 4 /* compression, minimum */];
   3612 	struct in6_addr ouraddr;
   3613 	int i = 0;
   3614 
   3615 	if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
   3616 		sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
   3617 		opt[i++] = IPV6CP_OPT_IFID;
   3618 		opt[i++] = 10;
   3619 		bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
   3620 		i += 8;
   3621 	}
   3622 
   3623 #ifdef notyet
   3624 	if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
   3625 		opt[i++] = IPV6CP_OPT_COMPRESSION;
   3626 		opt[i++] = 4;
   3627 		opt[i++] = 0;	/* TBD */
   3628 		opt[i++] = 0;	/* TBD */
   3629 		/* variable length data may follow */
   3630 	}
   3631 #endif
   3632 
   3633 	sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
   3634 	sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
   3635 }
   3636 #else /*INET6*/
   3637 static void sppp_ipv6cp_init(struct sppp *sp)
   3638 {
   3639 }
   3640 
   3641 static void sppp_ipv6cp_up(struct sppp *sp)
   3642 {
   3643 }
   3644 
   3645 static void sppp_ipv6cp_down(struct sppp *sp)
   3646 {
   3647 }
   3648 
   3649 
   3650 static void sppp_ipv6cp_open(struct sppp *sp)
   3651 {
   3652 }
   3653 
   3654 static void sppp_ipv6cp_close(struct sppp *sp)
   3655 {
   3656 }
   3657 
   3658 static void sppp_ipv6cp_TO(void *sp)
   3659 {
   3660 }
   3661 
   3662 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
   3663 {
   3664 	return 0;
   3665 }
   3666 
   3667 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
   3668 {
   3669 }
   3670 
   3671 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
   3672 {
   3673 }
   3674 
   3675 static void sppp_ipv6cp_tlu(struct sppp *sp)
   3676 {
   3677 }
   3678 
   3679 static void sppp_ipv6cp_tld(struct sppp *sp)
   3680 {
   3681 }
   3682 
   3683 static void sppp_ipv6cp_tls(struct sppp *sp)
   3684 {
   3685 }
   3686 
   3687 static void sppp_ipv6cp_tlf(struct sppp *sp)
   3688 {
   3689 }
   3690 
   3691 static void sppp_ipv6cp_scr(struct sppp *sp)
   3692 {
   3693 }
   3694 #endif /*INET6*/
   3695 
   3696 
   3697 /*
   3699  *--------------------------------------------------------------------------*
   3700  *                                                                          *
   3701  *                        The CHAP implementation.                          *
   3702  *                                                                          *
   3703  *--------------------------------------------------------------------------*
   3704  */
   3705 
   3706 /*
   3707  * The authentication protocols don't employ a full-fledged state machine as
   3708  * the control protocols do, since they do have Open and Close events, but
   3709  * not Up and Down, nor are they explicitly terminated.  Also, use of the
   3710  * authentication protocols may be different in both directions (this makes
   3711  * sense, think of a machine that never accepts incoming calls but only
   3712  * calls out, it doesn't require the called party to authenticate itself).
   3713  *
   3714  * Our state machine for the local authentication protocol (we are requesting
   3715  * the peer to authenticate) looks like:
   3716  *
   3717  *						    RCA-
   3718  *	      +--------------------------------------------+
   3719  *	      V					    scn,tld|
   3720  *	  +--------+			       Close   +---------+ RCA+
   3721  *	  |	   |<----------------------------------|	 |------+
   3722  *   +--->| Closed |				TO*    | Opened	 | sca	|
   3723  *   |	  |	   |-----+		       +-------|	 |<-----+
   3724  *   |	  +--------+ irc |		       |       +---------+
   3725  *   |	    ^		 |		       |	   ^
   3726  *   |	    |		 |		       |	   |
   3727  *   |	    |		 |		       |	   |
   3728  *   |	 TO-|		 |		       |	   |
   3729  *   |	    |tld  TO+	 V		       |	   |
   3730  *   |	    |	+------->+		       |	   |
   3731  *   |	    |	|	 |		       |	   |
   3732  *   |	  +--------+	 V		       |	   |
   3733  *   |	  |	   |<----+<--------------------+	   |
   3734  *   |	  | Req-   | scr				   |
   3735  *   |	  | Sent   |					   |
   3736  *   |	  |	   |					   |
   3737  *   |	  +--------+					   |
   3738  *   | RCA- |	| RCA+					   |
   3739  *   +------+	+------------------------------------------+
   3740  *   scn,tld	  sca,irc,ict,tlu
   3741  *
   3742  *
   3743  *   with:
   3744  *
   3745  *	Open:	LCP reached authentication phase
   3746  *	Close:	LCP reached terminate phase
   3747  *
   3748  *	RCA+:	received reply (pap-req, chap-response), acceptable
   3749  *	RCN:	received reply (pap-req, chap-response), not acceptable
   3750  *	TO+:	timeout with restart counter >= 0
   3751  *	TO-:	timeout with restart counter < 0
   3752  *	TO*:	reschedule timeout for CHAP
   3753  *
   3754  *	scr:	send request packet (none for PAP, chap-challenge)
   3755  *	sca:	send ack packet (pap-ack, chap-success)
   3756  *	scn:	send nak packet (pap-nak, chap-failure)
   3757  *	ict:	initialize re-challenge timer (CHAP only)
   3758  *
   3759  *	tlu:	this-layer-up, LCP reaches network phase
   3760  *	tld:	this-layer-down, LCP enters terminate phase
   3761  *
   3762  * Note that in CHAP mode, after sending a new challenge, while the state
   3763  * automaton falls back into Req-Sent state, it doesn't signal a tld
   3764  * event to LCP, so LCP remains in network phase.  Only after not getting
   3765  * any response (or after getting an unacceptable response), CHAP closes,
   3766  * causing LCP to enter terminate phase.
   3767  *
   3768  * With PAP, there is no initial request that can be sent.  The peer is
   3769  * expected to send one based on the successful negotiation of PAP as
   3770  * the authentication protocol during the LCP option negotiation.
   3771  *
   3772  * Incoming authentication protocol requests (remote requests
   3773  * authentication, we are peer) don't employ a state machine at all,
   3774  * they are simply answered.  Some peers [Ascend P50 firmware rev
   3775  * 4.50] react allergically when sending IPCP/IPv6CP requests while they are
   3776  * still in authentication phase (thereby violating the standard that
   3777  * demands that these NCP packets are to be discarded), so we keep
   3778  * track of the peer demanding us to authenticate, and only proceed to
   3779  * phase network once we've seen a positive acknowledge for the
   3780  * authentication.
   3781  */
   3782 
   3783 /*
   3784  * Handle incoming CHAP packets.
   3785  */
   3786 void
   3787 sppp_chap_input(struct sppp *sp, struct mbuf *m)
   3788 {
   3789 	STDDCL;
   3790 	struct lcp_header *h;
   3791 	int len, x;
   3792 	u_char *value, *name, digest[AUTHKEYLEN], dsize;
   3793 	int value_len, name_len;
   3794 	MD5_CTX ctx;
   3795 
   3796 	len = m->m_pkthdr.len;
   3797 	if (len < 4) {
   3798 		if (debug)
   3799 			log(LOG_DEBUG,
   3800 			    SPP_FMT "chap invalid packet length: %d bytes\n",
   3801 			    SPP_ARGS(ifp), len);
   3802 		return;
   3803 	}
   3804 	h = mtod (m, struct lcp_header*);
   3805 	if (len > ntohs (h->len))
   3806 		len = ntohs (h->len);
   3807 
   3808 	switch (h->type) {
   3809 	/* challenge, failure and success are his authproto */
   3810 	case CHAP_CHALLENGE:
   3811 		value = 1 + (u_char*)(h+1);
   3812 		value_len = value[-1];
   3813 		name = value + value_len;
   3814 		name_len = len - value_len - 5;
   3815 		if (name_len < 0) {
   3816 			if (debug) {
   3817 				log(LOG_DEBUG,
   3818 				    SPP_FMT "chap corrupted challenge "
   3819 				    "<%s id=0x%x len=%d",
   3820 				    SPP_ARGS(ifp),
   3821 				    sppp_auth_type_name(PPP_CHAP, h->type),
   3822 				    h->ident, ntohs(h->len));
   3823 				if (len > 4)
   3824 					sppp_print_bytes((u_char*) (h+1), len-4);
   3825 				addlog(">\n");
   3826 			}
   3827 			break;
   3828 		}
   3829 
   3830 		if (debug) {
   3831 			log(LOG_DEBUG,
   3832 			    SPP_FMT "chap input <%s id=0x%x len=%d name=",
   3833 			    SPP_ARGS(ifp),
   3834 			    sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
   3835 			    ntohs(h->len));
   3836 			sppp_print_string((char*) name, name_len);
   3837 			addlog(" value-size=%d value=", value_len);
   3838 			sppp_print_bytes(value, value_len);
   3839 			addlog(">\n");
   3840 		}
   3841 
   3842 		/* Compute reply value. */
   3843 		MD5Init(&ctx);
   3844 		MD5Update(&ctx, &h->ident, 1);
   3845 		MD5Update(&ctx, sp->myauth.secret,
   3846 			  sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
   3847 		MD5Update(&ctx, value, value_len);
   3848 		MD5Final(digest, &ctx);
   3849 		dsize = sizeof digest;
   3850 
   3851 		sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
   3852 			       sizeof dsize, (const char *)&dsize,
   3853 			       sizeof digest, digest,
   3854 			       (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
   3855 			       sp->myauth.name,
   3856 			       0);
   3857 		break;
   3858 
   3859 	case CHAP_SUCCESS:
   3860 		if (debug) {
   3861 			log(LOG_DEBUG, SPP_FMT "chap success",
   3862 			    SPP_ARGS(ifp));
   3863 			if (len > 4) {
   3864 				addlog(": ");
   3865 				sppp_print_string((char*)(h + 1), len - 4);
   3866 			}
   3867 			addlog("\n");
   3868 		}
   3869 		x = splnet();
   3870 		sp->pp_flags &= ~PP_NEEDAUTH;
   3871 		if (sp->myauth.proto == PPP_CHAP &&
   3872 		    (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
   3873 		    (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
   3874 			/*
   3875 			 * We are authenticator for CHAP but didn't
   3876 			 * complete yet.  Leave it to tlu to proceed
   3877 			 * to network phase.
   3878 			 */
   3879 			splx(x);
   3880 			break;
   3881 		}
   3882 		splx(x);
   3883 		sppp_phase_network(sp);
   3884 		break;
   3885 
   3886 	case CHAP_FAILURE:
   3887 		if (debug) {
   3888 			log(LOG_INFO, SPP_FMT "chap failure",
   3889 			    SPP_ARGS(ifp));
   3890 			if (len > 4) {
   3891 				addlog(": ");
   3892 				sppp_print_string((char*)(h + 1), len - 4);
   3893 			}
   3894 			addlog("\n");
   3895 		} else
   3896 			log(LOG_INFO, SPP_FMT "chap failure\n",
   3897 			    SPP_ARGS(ifp));
   3898 		/* await LCP shutdown by authenticator */
   3899 		break;
   3900 
   3901 	/* response is my authproto */
   3902 	case CHAP_RESPONSE:
   3903 		value = 1 + (u_char*)(h+1);
   3904 		value_len = value[-1];
   3905 		name = value + value_len;
   3906 		name_len = len - value_len - 5;
   3907 		if (name_len < 0) {
   3908 			if (debug) {
   3909 				log(LOG_DEBUG,
   3910 				    SPP_FMT "chap corrupted response "
   3911 				    "<%s id=0x%x len=%d",
   3912 				    SPP_ARGS(ifp),
   3913 				    sppp_auth_type_name(PPP_CHAP, h->type),
   3914 				    h->ident, ntohs(h->len));
   3915 				if (len > 4)
   3916 					sppp_print_bytes((u_char*)(h+1), len-4);
   3917 				addlog(">\n");
   3918 			}
   3919 			break;
   3920 		}
   3921 		if (h->ident != sp->confid[IDX_CHAP]) {
   3922 			if (debug)
   3923 				log(LOG_DEBUG,
   3924 				    SPP_FMT "chap dropping response for old ID "
   3925 				    "(got %d, expected %d)\n",
   3926 				    SPP_ARGS(ifp),
   3927 				    h->ident, sp->confid[IDX_CHAP]);
   3928 			break;
   3929 		}
   3930 		if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
   3931 		    || bcmp(name, sp->hisauth.name, name_len) != 0) {
   3932 			log(LOG_INFO, SPP_FMT "chap response, his name ",
   3933 			    SPP_ARGS(ifp));
   3934 			sppp_print_string(name, name_len);
   3935 			addlog(" != expected ");
   3936 			sppp_print_string(sp->hisauth.name,
   3937 					  sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
   3938 			addlog("\n");
   3939 		}
   3940 		if (debug) {
   3941 			log(LOG_DEBUG, SPP_FMT "chap input(%s) "
   3942 			    "<%s id=0x%x len=%d name=",
   3943 			    SPP_ARGS(ifp),
   3944 			    sppp_state_name(sp->state[IDX_CHAP]),
   3945 			    sppp_auth_type_name(PPP_CHAP, h->type),
   3946 			    h->ident, ntohs (h->len));
   3947 			sppp_print_string((char*)name, name_len);
   3948 			addlog(" value-size=%d value=", value_len);
   3949 			sppp_print_bytes(value, value_len);
   3950 			addlog(">\n");
   3951 		}
   3952 		if (value_len != AUTHKEYLEN) {
   3953 			if (debug)
   3954 				log(LOG_DEBUG,
   3955 				    SPP_FMT "chap bad hash value length: "
   3956 				    "%d bytes, should be %d\n",
   3957 				    SPP_ARGS(ifp), value_len,
   3958 				    AUTHKEYLEN);
   3959 			break;
   3960 		}
   3961 
   3962 		MD5Init(&ctx);
   3963 		MD5Update(&ctx, &h->ident, 1);
   3964 		MD5Update(&ctx, sp->hisauth.secret,
   3965 			  sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
   3966 		MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
   3967 		MD5Final(digest, &ctx);
   3968 
   3969 #define FAILMSG "Failed..."
   3970 #define SUCCMSG "Welcome!"
   3971 
   3972 		if (value_len != sizeof digest ||
   3973 		    bcmp(digest, value, value_len) != 0) {
   3974 			/* action scn, tld */
   3975 			sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
   3976 				       sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
   3977 				       0);
   3978 			chap.tld(sp);
   3979 			break;
   3980 		}
   3981 		/* action sca, perhaps tlu */
   3982 		if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
   3983 		    sp->state[IDX_CHAP] == STATE_OPENED)
   3984 			sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
   3985 				       sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
   3986 				       0);
   3987 		if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
   3988 			sppp_cp_change_state(&chap, sp, STATE_OPENED);
   3989 			chap.tlu(sp);
   3990 		}
   3991 		break;
   3992 
   3993 	default:
   3994 		/* Unknown CHAP packet type -- ignore. */
   3995 		if (debug) {
   3996 			log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
   3997 			    "<0x%x id=0x%xh len=%d",
   3998 			    SPP_ARGS(ifp),
   3999 			    sppp_state_name(sp->state[IDX_CHAP]),
   4000 			    h->type, h->ident, ntohs(h->len));
   4001 			if (len > 4)
   4002 				sppp_print_bytes((u_char*)(h+1), len-4);
   4003 			addlog(">\n");
   4004 		}
   4005 		break;
   4006 
   4007 	}
   4008 }
   4009 
   4010 static void
   4011 sppp_chap_init(struct sppp *sp)
   4012 {
   4013 	/* Chap doesn't have STATE_INITIAL at all. */
   4014 	sp->state[IDX_CHAP] = STATE_CLOSED;
   4015 	sp->fail_counter[IDX_CHAP] = 0;
   4016 	sp->pp_seq[IDX_CHAP] = 0;
   4017 	sp->pp_rseq[IDX_CHAP] = 0;
   4018 #if defined(__NetBSD__)
   4019 	callout_init(&sp->ch[IDX_CHAP]);
   4020 #endif
   4021 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4022 	callout_handle_init(&sp->ch[IDX_CHAP]);
   4023 #endif
   4024 }
   4025 
   4026 static void
   4027 sppp_chap_open(struct sppp *sp)
   4028 {
   4029 	if (sp->myauth.proto == PPP_CHAP &&
   4030 	    (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
   4031 		/* we are authenticator for CHAP, start it */
   4032 		chap.scr(sp);
   4033 		sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
   4034 		sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
   4035 	}
   4036 	/* nothing to be done if we are peer, await a challenge */
   4037 }
   4038 
   4039 static void
   4040 sppp_chap_close(struct sppp *sp)
   4041 {
   4042 	if (sp->state[IDX_CHAP] != STATE_CLOSED)
   4043 		sppp_cp_change_state(&chap, sp, STATE_CLOSED);
   4044 }
   4045 
   4046 static void
   4047 sppp_chap_TO(void *cookie)
   4048 {
   4049 	struct sppp *sp = (struct sppp *)cookie;
   4050 	STDDCL;
   4051 	int s;
   4052 
   4053 	s = splnet();
   4054 	if (debug)
   4055 		log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
   4056 		    SPP_ARGS(ifp),
   4057 		    sppp_state_name(sp->state[IDX_CHAP]),
   4058 		    sp->rst_counter[IDX_CHAP]);
   4059 
   4060 	if (--sp->rst_counter[IDX_CHAP] < 0)
   4061 		/* TO- event */
   4062 		switch (sp->state[IDX_CHAP]) {
   4063 		case STATE_REQ_SENT:
   4064 			chap.tld(sp);
   4065 			sppp_cp_change_state(&chap, sp, STATE_CLOSED);
   4066 			break;
   4067 		}
   4068 	else
   4069 		/* TO+ (or TO*) event */
   4070 		switch (sp->state[IDX_CHAP]) {
   4071 		case STATE_OPENED:
   4072 			/* TO* event */
   4073 			sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
   4074 			/* fall through */
   4075 		case STATE_REQ_SENT:
   4076 			chap.scr(sp);
   4077 			/* sppp_cp_change_state() will restart the timer */
   4078 			sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
   4079 			break;
   4080 		}
   4081 
   4082 	splx(s);
   4083 }
   4084 
   4085 static void
   4086 sppp_chap_tlu(struct sppp *sp)
   4087 {
   4088 	STDDCL;
   4089 	int i, x;
   4090 
   4091 	i = 0;
   4092 	sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
   4093 
   4094 	/*
   4095 	 * Some broken CHAP implementations (Conware CoNet, firmware
   4096 	 * 4.0.?) don't want to re-authenticate their CHAP once the
   4097 	 * initial challenge-response exchange has taken place.
   4098 	 * Provide for an option to avoid rechallenges.
   4099 	 */
   4100 	if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
   4101 		/*
   4102 		 * Compute the re-challenge timeout.  This will yield
   4103 		 * a number between 300 and 810 seconds.
   4104 		 */
   4105 		i = 300 + ((unsigned)(random() & 0xff00) >> 7);
   4106 
   4107 #if defined(__NetBSD__)
   4108 		callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, sp);
   4109 #else
   4110 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4111 		sp->ch[IDX_CHAP] =
   4112 #endif
   4113 		timeout(chap.TO, (void *)sp, i * hz);
   4114 #endif /* __NetBSD__ */
   4115 	}
   4116 
   4117 	if (debug) {
   4118 		log(LOG_DEBUG,
   4119 		    SPP_FMT "chap %s, ",
   4120 		    SPP_ARGS(ifp),
   4121 		    sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
   4122 		if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
   4123 			addlog("next re-challenge in %d seconds\n", i);
   4124 		else
   4125 			addlog("re-challenging supressed\n");
   4126 	}
   4127 
   4128 	x = splnet();
   4129 	/* indicate to LCP that we need to be closed down */
   4130 	sp->lcp.protos |= (1 << IDX_CHAP);
   4131 
   4132 	if (sp->pp_flags & PP_NEEDAUTH) {
   4133 		/*
   4134 		 * Remote is authenticator, but his auth proto didn't
   4135 		 * complete yet.  Defer the transition to network
   4136 		 * phase.
   4137 		 */
   4138 		splx(x);
   4139 		return;
   4140 	}
   4141 	splx(x);
   4142 
   4143 	/*
   4144 	 * If we are already in phase network, we are done here.  This
   4145 	 * is the case if this is a dummy tlu event after a re-challenge.
   4146 	 */
   4147 	if (sp->pp_phase != PHASE_NETWORK)
   4148 		sppp_phase_network(sp);
   4149 }
   4150 
   4151 static void
   4152 sppp_chap_tld(struct sppp *sp)
   4153 {
   4154 	STDDCL;
   4155 
   4156 	if (debug)
   4157 		log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
   4158 #if defined(__NetBSD__)
   4159 	callout_stop(&sp->ch[IDX_CHAP]);
   4160 #else
   4161 	untimeout(chap.TO, (void *)sp
   4162 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4163 	, sp->ch[IDX_CHAP]
   4164 #endif
   4165 	);
   4166 #endif /* __NetBSD__ */
   4167 	sp->lcp.protos &= ~(1 << IDX_CHAP);
   4168 
   4169 	lcp.Close(sp);
   4170 }
   4171 
   4172 static void
   4173 sppp_chap_scr(struct sppp *sp)
   4174 {
   4175 	struct timeval tv;
   4176 	u_long *ch, seed;
   4177 	u_char clen;
   4178 
   4179 	/* Compute random challenge. */
   4180 	ch = (u_long *)sp->myauth.challenge;
   4181 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4182 	read_random(&seed, sizeof seed);
   4183 #else
   4184 	microtime(&tv);
   4185 	seed = tv.tv_sec ^ tv.tv_usec;
   4186 #endif
   4187 	ch[0] = seed ^ random();
   4188 	ch[1] = seed ^ random();
   4189 	ch[2] = seed ^ random();
   4190 	ch[3] = seed ^ random();
   4191 	clen = AUTHKEYLEN;
   4192 
   4193 	sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
   4194 
   4195 	sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
   4196 		       sizeof clen, (const char *)&clen,
   4197 		       (size_t)AUTHKEYLEN, sp->myauth.challenge,
   4198 		       (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
   4199 		       sp->myauth.name,
   4200 		       0);
   4201 }
   4202 /*
   4204  *--------------------------------------------------------------------------*
   4205  *                                                                          *
   4206  *                        The PAP implementation.                           *
   4207  *                                                                          *
   4208  *--------------------------------------------------------------------------*
   4209  */
   4210 /*
   4211  * For PAP, we need to keep a little state also if we are the peer, not the
   4212  * authenticator.  This is since we don't get a request to authenticate, but
   4213  * have to repeatedly authenticate ourself until we got a response (or the
   4214  * retry counter is expired).
   4215  */
   4216 
   4217 /*
   4218  * Handle incoming PAP packets.  */
   4219 static void
   4220 sppp_pap_input(struct sppp *sp, struct mbuf *m)
   4221 {
   4222 	STDDCL;
   4223 	struct lcp_header *h;
   4224 	int len, x;
   4225 	u_char *name, *passwd, mlen;
   4226 	int name_len, passwd_len;
   4227 
   4228 	len = m->m_pkthdr.len;
   4229 	if (len < 5) {
   4230 		if (debug)
   4231 			log(LOG_DEBUG,
   4232 			    SPP_FMT "pap invalid packet length: %d bytes\n",
   4233 			    SPP_ARGS(ifp), len);
   4234 		return;
   4235 	}
   4236 	h = mtod (m, struct lcp_header*);
   4237 	if (len > ntohs (h->len))
   4238 		len = ntohs (h->len);
   4239 	switch (h->type) {
   4240 	/* PAP request is my authproto */
   4241 	case PAP_REQ:
   4242 		name = 1 + (u_char*)(h+1);
   4243 		name_len = name[-1];
   4244 		passwd = name + name_len + 1;
   4245 		if (name_len > len - 6 ||
   4246 		    (passwd_len = passwd[-1]) > len - 6 - name_len) {
   4247 			if (debug) {
   4248 				log(LOG_DEBUG, SPP_FMT "pap corrupted input "
   4249 				    "<%s id=0x%x len=%d",
   4250 				    SPP_ARGS(ifp),
   4251 				    sppp_auth_type_name(PPP_PAP, h->type),
   4252 				    h->ident, ntohs(h->len));
   4253 				if (len > 4)
   4254 					sppp_print_bytes((u_char*)(h+1), len-4);
   4255 				addlog(">\n");
   4256 			}
   4257 			break;
   4258 		}
   4259 		if (debug) {
   4260 			log(LOG_DEBUG, SPP_FMT "pap input(%s) "
   4261 			    "<%s id=0x%x len=%d name=",
   4262 			    SPP_ARGS(ifp),
   4263 			    sppp_state_name(sp->state[IDX_PAP]),
   4264 			    sppp_auth_type_name(PPP_PAP, h->type),
   4265 			    h->ident, ntohs(h->len));
   4266 			sppp_print_string((char*)name, name_len);
   4267 			addlog(" passwd=");
   4268 			sppp_print_string((char*)passwd, passwd_len);
   4269 			addlog(">\n");
   4270 		}
   4271 		if (name_len > AUTHNAMELEN ||
   4272 		    passwd_len > AUTHKEYLEN ||
   4273 		    bcmp(name, sp->hisauth.name, name_len) != 0 ||
   4274 		    bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
   4275 			/* action scn, tld */
   4276 			mlen = sizeof(FAILMSG) - 1;
   4277 			sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
   4278 				       sizeof mlen, (const char *)&mlen,
   4279 				       sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
   4280 				       0);
   4281 			pap.tld(sp);
   4282 			break;
   4283 		}
   4284 		/* action sca, perhaps tlu */
   4285 		if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
   4286 		    sp->state[IDX_PAP] == STATE_OPENED) {
   4287 			mlen = sizeof(SUCCMSG) - 1;
   4288 			sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
   4289 				       sizeof mlen, (const char *)&mlen,
   4290 				       sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
   4291 				       0);
   4292 		}
   4293 		if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
   4294 			sppp_cp_change_state(&pap, sp, STATE_OPENED);
   4295 			pap.tlu(sp);
   4296 		}
   4297 		break;
   4298 
   4299 	/* ack and nak are his authproto */
   4300 	case PAP_ACK:
   4301 #if defined(__NetBSD__)
   4302 		callout_stop(&sp->pap_my_to_ch);
   4303 #else
   4304 		untimeout(sppp_pap_my_TO, (void *)sp
   4305 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4306 		, sp->pap_my_to_ch
   4307 #endif
   4308 		);
   4309 #endif /* __NetBSD__ */
   4310 		if (debug) {
   4311 			log(LOG_DEBUG, SPP_FMT "pap success",
   4312 			    SPP_ARGS(ifp));
   4313 			name_len = *((char *)h);
   4314 			if (len > 5 && name_len) {
   4315 				addlog(": ");
   4316 				sppp_print_string((char*)(h+1), name_len);
   4317 			}
   4318 			addlog("\n");
   4319 		}
   4320 		x = splnet();
   4321 		sp->pp_flags &= ~PP_NEEDAUTH;
   4322 		if (sp->myauth.proto == PPP_PAP &&
   4323 		    (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
   4324 		    (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
   4325 			/*
   4326 			 * We are authenticator for PAP but didn't
   4327 			 * complete yet.  Leave it to tlu to proceed
   4328 			 * to network phase.
   4329 			 */
   4330 			splx(x);
   4331 			break;
   4332 		}
   4333 		splx(x);
   4334 		sppp_phase_network(sp);
   4335 		break;
   4336 
   4337 	case PAP_NAK:
   4338 #if defined(__NetBSD__)
   4339 		callout_stop(&sp->pap_my_to_ch);
   4340 #else
   4341 		untimeout(sppp_pap_my_TO, (void *)sp
   4342 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4343 		, sp->pap_my_to_ch
   4344 #endif
   4345 		);
   4346 #endif /* __NetBSD__ */
   4347 		if (debug) {
   4348 			log(LOG_INFO, SPP_FMT "pap failure",
   4349 			    SPP_ARGS(ifp));
   4350 			name_len = *((char *)h);
   4351 			if (len > 5 && name_len) {
   4352 				addlog(": ");
   4353 				sppp_print_string((char*)(h+1), name_len);
   4354 			}
   4355 			addlog("\n");
   4356 		} else
   4357 			log(LOG_INFO, SPP_FMT "pap failure\n",
   4358 			    SPP_ARGS(ifp));
   4359 		/* await LCP shutdown by authenticator */
   4360 		break;
   4361 
   4362 	default:
   4363 		/* Unknown PAP packet type -- ignore. */
   4364 		if (debug) {
   4365 			log(LOG_DEBUG, SPP_FMT "pap corrupted input "
   4366 			    "<0x%x id=0x%x len=%d",
   4367 			    SPP_ARGS(ifp),
   4368 			    h->type, h->ident, ntohs(h->len));
   4369 			if (len > 4)
   4370 				sppp_print_bytes((u_char*)(h+1), len-4);
   4371 			addlog(">\n");
   4372 		}
   4373 		break;
   4374 
   4375 	}
   4376 }
   4377 
   4378 static void
   4379 sppp_pap_init(struct sppp *sp)
   4380 {
   4381 	/* PAP doesn't have STATE_INITIAL at all. */
   4382 	sp->state[IDX_PAP] = STATE_CLOSED;
   4383 	sp->fail_counter[IDX_PAP] = 0;
   4384 	sp->pp_seq[IDX_PAP] = 0;
   4385 	sp->pp_rseq[IDX_PAP] = 0;
   4386 #if defined(__NetBSD__)
   4387 	callout_init(&sp->ch[IDX_PAP]);
   4388 	callout_init(&sp->pap_my_to_ch);
   4389 #endif
   4390 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4391 	callout_handle_init(&sp->ch[IDX_PAP]);
   4392 	callout_handle_init(&sp->pap_my_to_ch);
   4393 #endif
   4394 }
   4395 
   4396 static void
   4397 sppp_pap_open(struct sppp *sp)
   4398 {
   4399 	if (sp->hisauth.proto == PPP_PAP &&
   4400 	    (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
   4401 		/* we are authenticator for PAP, start our timer */
   4402 		sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
   4403 		sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
   4404 	}
   4405 	if (sp->myauth.proto == PPP_PAP) {
   4406 		/* we are peer, send a request, and start a timer */
   4407 		pap.scr(sp);
   4408 #if defined(__NetBSD__)
   4409 		callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
   4410 		    sppp_pap_my_TO, sp);
   4411 #else
   4412 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4413 		sp->pap_my_to_ch =
   4414 #endif
   4415 		timeout(sppp_pap_my_TO, (void *)sp, sp->lcp.timeout);
   4416 #endif /* __NetBSD__ */
   4417 	}
   4418 }
   4419 
   4420 static void
   4421 sppp_pap_close(struct sppp *sp)
   4422 {
   4423 	if (sp->state[IDX_PAP] != STATE_CLOSED)
   4424 		sppp_cp_change_state(&pap, sp, STATE_CLOSED);
   4425 }
   4426 
   4427 /*
   4428  * That's the timeout routine if we are authenticator.  Since the
   4429  * authenticator is basically passive in PAP, we can't do much here.
   4430  */
   4431 static void
   4432 sppp_pap_TO(void *cookie)
   4433 {
   4434 	struct sppp *sp = (struct sppp *)cookie;
   4435 	STDDCL;
   4436 	int s;
   4437 
   4438 	s = splnet();
   4439 	if (debug)
   4440 		log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
   4441 		    SPP_ARGS(ifp),
   4442 		    sppp_state_name(sp->state[IDX_PAP]),
   4443 		    sp->rst_counter[IDX_PAP]);
   4444 
   4445 	if (--sp->rst_counter[IDX_PAP] < 0)
   4446 		/* TO- event */
   4447 		switch (sp->state[IDX_PAP]) {
   4448 		case STATE_REQ_SENT:
   4449 			pap.tld(sp);
   4450 			sppp_cp_change_state(&pap, sp, STATE_CLOSED);
   4451 			break;
   4452 		}
   4453 	else
   4454 		/* TO+ event, not very much we could do */
   4455 		switch (sp->state[IDX_PAP]) {
   4456 		case STATE_REQ_SENT:
   4457 			/* sppp_cp_change_state() will restart the timer */
   4458 			sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
   4459 			break;
   4460 		}
   4461 
   4462 	splx(s);
   4463 }
   4464 
   4465 /*
   4466  * That's the timeout handler if we are peer.  Since the peer is active,
   4467  * we need to retransmit our PAP request since it is apparently lost.
   4468  * XXX We should impose a max counter.
   4469  */
   4470 static void
   4471 sppp_pap_my_TO(void *cookie)
   4472 {
   4473 	struct sppp *sp = (struct sppp *)cookie;
   4474 	STDDCL;
   4475 
   4476 	if (debug)
   4477 		log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
   4478 		    SPP_ARGS(ifp));
   4479 
   4480 	pap.scr(sp);
   4481 }
   4482 
   4483 static void
   4484 sppp_pap_tlu(struct sppp *sp)
   4485 {
   4486 	STDDCL;
   4487 	int x;
   4488 
   4489 	sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
   4490 
   4491 	if (debug)
   4492 		log(LOG_DEBUG, SPP_FMT "%s tlu\n",
   4493 		    SPP_ARGS(ifp), pap.name);
   4494 
   4495 	x = splnet();
   4496 	/* indicate to LCP that we need to be closed down */
   4497 	sp->lcp.protos |= (1 << IDX_PAP);
   4498 
   4499 	if (sp->pp_flags & PP_NEEDAUTH) {
   4500 		/*
   4501 		 * Remote is authenticator, but his auth proto didn't
   4502 		 * complete yet.  Defer the transition to network
   4503 		 * phase.
   4504 		 */
   4505 		splx(x);
   4506 		return;
   4507 	}
   4508 	splx(x);
   4509 	sppp_phase_network(sp);
   4510 }
   4511 
   4512 static void
   4513 sppp_pap_tld(struct sppp *sp)
   4514 {
   4515 	STDDCL;
   4516 
   4517 	if (debug)
   4518 		log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
   4519 #if defined(__NetBSD__)
   4520 	callout_stop(&sp->ch[IDX_PAP]);
   4521 #else
   4522 	untimeout(pap.TO, (void *)sp
   4523 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4524 	, sp->ch[IDX_PAP]
   4525 #endif
   4526 	);
   4527 #endif /* __NetBSD__ */
   4528 
   4529 #if defined(__NetBSD__)
   4530 	callout_stop(&sp->pap_my_to_ch);
   4531 #else
   4532 	untimeout(sppp_pap_my_TO, (void *)sp
   4533 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4534 	, sp->pap_my_to_ch
   4535 #endif
   4536 	);
   4537 #endif /* __NetBSD__ */
   4538 	sp->lcp.protos &= ~(1 << IDX_PAP);
   4539 
   4540 	lcp.Close(sp);
   4541 }
   4542 
   4543 static void
   4544 sppp_pap_scr(struct sppp *sp)
   4545 {
   4546 	u_char idlen, pwdlen;
   4547 
   4548 	sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
   4549 	pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
   4550 	idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
   4551 
   4552 	sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
   4553 		       sizeof idlen, (const char *)&idlen,
   4554 		       (size_t)idlen, sp->myauth.name,
   4555 		       sizeof pwdlen, (const char *)&pwdlen,
   4556 		       (size_t)pwdlen, sp->myauth.secret,
   4557 		       0);
   4558 }
   4559 /*
   4561  * Random miscellaneous functions.
   4562  */
   4563 
   4564 /*
   4565  * Send a PAP or CHAP proto packet.
   4566  *
   4567  * Varadic function, each of the elements for the ellipsis is of type
   4568  * ``size_t mlen, const u_char *msg''.  Processing will stop iff
   4569  * mlen == 0.
   4570  * NOTE: never declare variadic functions with types subject to type
   4571  * promotion (i.e. u_char). This is asking for big trouble depending
   4572  * on the architecture you are on...
   4573  */
   4574 
   4575 static void
   4576 sppp_auth_send(const struct cp *cp, struct sppp *sp,
   4577                unsigned int type, unsigned int id,
   4578 	       ...)
   4579 {
   4580 	STDDCL;
   4581 	struct lcp_header *lh;
   4582 	struct mbuf *m;
   4583 	u_char *p;
   4584 	int len;
   4585 	size_t pkthdrlen;
   4586 	unsigned int mlen;
   4587 	const char *msg;
   4588 	va_list ap;
   4589 
   4590 	MGETHDR (m, M_DONTWAIT, MT_DATA);
   4591 	if (! m)
   4592 		return;
   4593 	m->m_pkthdr.rcvif = 0;
   4594 
   4595 	if (sp->pp_flags & PP_NOFRAMING) {
   4596 		*mtod(m, u_int16_t*) = htons(cp->proto);
   4597 		pkthdrlen = 2;
   4598 		lh = (struct lcp_header*)(mtod(m, u_int8_t*)+2);
   4599 	} else {
   4600 		struct ppp_header *h;
   4601 		h = mtod (m, struct ppp_header*);
   4602 		h->address = PPP_ALLSTATIONS;		/* broadcast address */
   4603 		h->control = PPP_UI;			/* Unnumbered Info */
   4604 		h->protocol = htons(cp->proto);
   4605 		pkthdrlen = PPP_HEADER_LEN;
   4606 
   4607 		lh = (struct lcp_header*)(h + 1);
   4608 	}
   4609 
   4610 	lh->type = type;
   4611 	lh->ident = id;
   4612 	p = (u_char*) (lh+1);
   4613 
   4614 	va_start(ap, id);
   4615 	len = 0;
   4616 
   4617 	while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
   4618 		msg = va_arg(ap, const char *);
   4619 		len += mlen;
   4620 		if (len > MHLEN - pkthdrlen - LCP_HEADER_LEN) {
   4621 			va_end(ap);
   4622 			m_freem(m);
   4623 			return;
   4624 		}
   4625 
   4626 		bcopy(msg, p, mlen);
   4627 		p += mlen;
   4628 	}
   4629 	va_end(ap);
   4630 
   4631 	m->m_pkthdr.len = m->m_len = pkthdrlen + LCP_HEADER_LEN + len;
   4632 	lh->len = htons (LCP_HEADER_LEN + len);
   4633 
   4634 	if (debug) {
   4635 		log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
   4636 		    SPP_ARGS(ifp), cp->name,
   4637 		    sppp_auth_type_name(cp->proto, lh->type),
   4638 		    lh->ident, ntohs(lh->len));
   4639 		if (len)
   4640 			sppp_print_bytes((u_char*) (lh+1), len);
   4641 		addlog(">\n");
   4642 	}
   4643 	if (IF_QFULL (&sp->pp_cpq)) {
   4644 		IF_DROP (&sp->pp_fastq);
   4645 		IF_DROP (&ifp->if_snd);
   4646 		m_freem (m);
   4647 		++ifp->if_oerrors;
   4648 	} else
   4649 		IF_ENQUEUE (&sp->pp_cpq, m);
   4650 	if (! (ifp->if_flags & IFF_OACTIVE))
   4651 		(*ifp->if_start) (ifp);
   4652 	ifp->if_obytes += m->m_pkthdr.len + 3;
   4653 }
   4654 
   4655 /*
   4656  * Send keepalive packets, every 10 seconds.
   4657  */
   4658 static void
   4659 sppp_keepalive(void *dummy)
   4660 {
   4661 	struct sppp *sp;
   4662 	int s;
   4663 
   4664 	s = splnet();
   4665 	for (sp=spppq; sp; sp=sp->pp_next) {
   4666 		struct ifnet *ifp = &sp->pp_if;
   4667 
   4668 		/* Keepalive mode disabled or channel down? */
   4669 		if (! (sp->pp_flags & PP_KEEPALIVE) ||
   4670 		    ! (ifp->if_flags & IFF_RUNNING))
   4671 			continue;
   4672 
   4673 		/* No keepalive in PPP mode if LCP not opened yet. */
   4674 		if (! (sp->pp_flags & PP_CISCO) &&
   4675 		    sp->pp_phase < PHASE_AUTHENTICATE)
   4676 			continue;
   4677 
   4678 		if (sp->pp_alivecnt == MAXALIVECNT) {
   4679 			/* No keepalive packets got.  Stop the interface. */
   4680 			printf (SPP_FMT "down\n", SPP_ARGS(ifp));
   4681 			if_down (ifp);
   4682 			IFQ_PURGE (&sp->pp_cpq);
   4683 			if (! (sp->pp_flags & PP_CISCO)) {
   4684 				/* XXX */
   4685 				/* Shut down the PPP link. */
   4686 				lcp.Down(sp);
   4687 				/* Initiate negotiation. XXX */
   4688 				lcp.Up(sp);
   4689 			}
   4690 		}
   4691 		if (sp->pp_alivecnt <= MAXALIVECNT)
   4692 			++sp->pp_alivecnt;
   4693 		if (sp->pp_flags & PP_CISCO)
   4694 			sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
   4695 			    ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
   4696 		else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
   4697 			long nmagic = htonl (sp->lcp.magic);
   4698 			sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
   4699 			sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
   4700 				sp->lcp.echoid, 4, &nmagic);
   4701 		}
   4702 	}
   4703 	splx(s);
   4704 #if defined(__NetBSD__)
   4705 	callout_reset(&keepalive_ch, hz * 10, sppp_keepalive, NULL);
   4706 #else
   4707 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4708 	keepalive_ch =
   4709 #endif
   4710 	timeout(sppp_keepalive, 0, hz * 10);
   4711 #endif /* __NetBSD__ */
   4712 }
   4713 
   4714 /*
   4715  * Get both IP addresses.
   4716  */
   4717 static void
   4718 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
   4719 {
   4720 	struct ifnet *ifp = &sp->pp_if;
   4721 	struct ifaddr *ifa;
   4722 	struct sockaddr_in *si, *sm;
   4723 	u_long ssrc, ddst;
   4724 
   4725 	sm = NULL;
   4726 	ssrc = ddst = 0L;
   4727 	/*
   4728 	 * Pick the first AF_INET address from the list,
   4729 	 * aliases don't make any sense on a p2p link anyway.
   4730 	 */
   4731 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4732 	for (ifa = ifp->if_addrhead.tqh_first, si = 0;
   4733 	     ifa;
   4734 	     ifa = ifa->ifa_link.tqe_next)
   4735 #elif defined(__NetBSD__) || defined (__OpenBSD__)
   4736 	for (ifa = ifp->if_addrlist.tqh_first, si = 0;
   4737 	     ifa;
   4738 	     ifa = ifa->ifa_list.tqe_next)
   4739 #else
   4740 	for (ifa = ifp->if_addrlist, si = 0;
   4741 	     ifa;
   4742 	     ifa = ifa->ifa_next)
   4743 #endif
   4744 		if (ifa->ifa_addr->sa_family == AF_INET) {
   4745 			si = (struct sockaddr_in *)ifa->ifa_addr;
   4746 			sm = (struct sockaddr_in *)ifa->ifa_netmask;
   4747 			if (si)
   4748 				break;
   4749 		}
   4750 	if (ifa) {
   4751 		if (si && si->sin_addr.s_addr) {
   4752 			ssrc = si->sin_addr.s_addr;
   4753 			if (srcmask)
   4754 				*srcmask = ntohl(sm->sin_addr.s_addr);
   4755 		}
   4756 
   4757 		si = (struct sockaddr_in *)ifa->ifa_dstaddr;
   4758 		if (si && si->sin_addr.s_addr)
   4759 			ddst = si->sin_addr.s_addr;
   4760 	}
   4761 
   4762 	if (dst) *dst = ntohl(ddst);
   4763 	if (src) *src = ntohl(ssrc);
   4764 }
   4765 
   4766 /*
   4767  * Set my IP address.  Must be called at splnet.
   4768  */
   4769 static void
   4770 sppp_set_ip_addr(struct sppp *sp, u_long src)
   4771 {
   4772 	STDDCL;
   4773 	struct ifaddr *ifa;
   4774 	struct sockaddr_in *si;
   4775 
   4776 	/*
   4777 	 * Pick the first AF_INET address from the list,
   4778 	 * aliases don't make any sense on a p2p link anyway.
   4779 	 */
   4780 
   4781 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4782 	for (ifa = ifp->if_addrhead.tqh_first, si = 0;
   4783 	     ifa;
   4784 	     ifa = ifa->ifa_link.tqe_next)
   4785 #elif defined(__NetBSD__) || defined (__OpenBSD__)
   4786 	for (ifa = ifp->if_addrlist.tqh_first, si = 0;
   4787 	     ifa;
   4788 	     ifa = ifa->ifa_list.tqe_next)
   4789 #else
   4790 	for (ifa = ifp->if_addrlist, si = 0;
   4791 	     ifa;
   4792 	     ifa = ifa->ifa_next)
   4793 #endif
   4794 	{
   4795 		if (ifa->ifa_addr->sa_family == AF_INET)
   4796 		{
   4797 			si = (struct sockaddr_in *)ifa->ifa_addr;
   4798 			if (si)
   4799 				break;
   4800 		}
   4801 	}
   4802 
   4803 	if (ifa && si)
   4804 	{
   4805 		int error;
   4806 #if __NetBSD_Version__ >= 103080000
   4807 		struct sockaddr_in new_sin = *si;
   4808 
   4809 		new_sin.sin_addr.s_addr = htonl(src);
   4810 		error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 1);
   4811 		if(debug && error)
   4812 		{
   4813 			log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: in_ifinit "
   4814 			" failed, error=%d\n", SPP_ARGS(ifp), error);
   4815 		}
   4816 #else
   4817 		/* delete old route */
   4818 		error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
   4819 		if(debug && error)
   4820 		{
   4821 			log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
   4822 		    		SPP_ARGS(ifp), error);
   4823 		}
   4824 
   4825 		/* set new address */
   4826 		si->sin_addr.s_addr = htonl(src);
   4827 
   4828 		/* add new route */
   4829 		error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
   4830 		if (debug && error)
   4831 		{
   4832 			log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
   4833 		    		SPP_ARGS(ifp), error);
   4834 		}
   4835 #endif
   4836 	}
   4837 }
   4838 
   4839 #ifdef INET6
   4840 /*
   4841  * Get both IPv6 addresses.
   4842  */
   4843 static void
   4844 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
   4845 		   struct in6_addr *srcmask)
   4846 {
   4847 	struct ifnet *ifp = &sp->pp_if;
   4848 	struct ifaddr *ifa;
   4849 	struct sockaddr_in6 *si, *sm;
   4850 	struct in6_addr ssrc, ddst;
   4851 
   4852 	sm = NULL;
   4853 	bzero(&ssrc, sizeof(ssrc));
   4854 	bzero(&ddst, sizeof(ddst));
   4855 	/*
   4856 	 * Pick the first link-local AF_INET6 address from the list,
   4857 	 * aliases don't make any sense on a p2p link anyway.
   4858 	 */
   4859 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4860 	for (ifa = ifp->if_addrhead.tqh_first, si = 0;
   4861 	     ifa;
   4862 	     ifa = ifa->ifa_link.tqe_next)
   4863 #elif defined(__NetBSD__) || defined (__OpenBSD__)
   4864 	for (ifa = ifp->if_addrlist.tqh_first, si = 0;
   4865 	     ifa;
   4866 	     ifa = ifa->ifa_list.tqe_next)
   4867 #else
   4868 	for (ifa = ifp->if_addrlist, si = 0;
   4869 	     ifa;
   4870 	     ifa = ifa->ifa_next)
   4871 #endif
   4872 		if (ifa->ifa_addr->sa_family == AF_INET6) {
   4873 			si = (struct sockaddr_in6 *)ifa->ifa_addr;
   4874 			sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
   4875 			if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
   4876 				break;
   4877 		}
   4878 	if (ifa) {
   4879 		if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
   4880 			bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
   4881 			if (srcmask) {
   4882 				bcopy(&sm->sin6_addr, srcmask,
   4883 				    sizeof(*srcmask));
   4884 			}
   4885 		}
   4886 
   4887 		si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
   4888 		if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
   4889 			bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
   4890 	}
   4891 
   4892 	if (dst)
   4893 		bcopy(&ddst, dst, sizeof(*dst));
   4894 	if (src)
   4895 		bcopy(&ssrc, src, sizeof(*src));
   4896 }
   4897 
   4898 #ifdef IPV6CP_MYIFID_DYN
   4899 /*
   4900  * Generate random ifid.
   4901  */
   4902 static void
   4903 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
   4904 {
   4905 	/* TBD */
   4906 }
   4907 
   4908 /*
   4909  * Set my IPv6 address.  Must be called at splnet.
   4910  */
   4911 static void
   4912 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
   4913 {
   4914 	STDDCL;
   4915 	struct ifaddr *ifa;
   4916 	struct sockaddr_in6 *sin6;
   4917 
   4918 	/*
   4919 	 * Pick the first link-local AF_INET6 address from the list,
   4920 	 * aliases don't make any sense on a p2p link anyway.
   4921 	 */
   4922 
   4923 	sin6 = NULL;
   4924 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
   4925 	for (ifa = ifp->if_addrhead.tqh_first;
   4926 	     ifa;
   4927 	     ifa = ifa->ifa_link.tqe_next)
   4928 #elif defined(__NetBSD__) || defined (__OpenBSD__)
   4929 	for (ifa = ifp->if_addrlist.tqh_first;
   4930 	     ifa;
   4931 	     ifa = ifa->ifa_list.tqe_next)
   4932 #else
   4933 	for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
   4934 #endif
   4935 	{
   4936 		if (ifa->ifa_addr->sa_family == AF_INET6)
   4937 		{
   4938 			sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
   4939 			if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
   4940 				break;
   4941 		}
   4942 	}
   4943 
   4944 	if (ifa && sin6)
   4945 	{
   4946 		int error;
   4947 		struct sockaddr_in6 new_sin6 = *sin6;
   4948 
   4949 		bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
   4950 		error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
   4951 		if (debug && error)
   4952 		{
   4953 			log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
   4954 			" failed, error=%d\n", SPP_ARGS(ifp), error);
   4955 		}
   4956 	}
   4957 }
   4958 #endif
   4959 
   4960 /*
   4961  * Suggest a candidate address to be used by peer.
   4962  */
   4963 static void
   4964 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
   4965 {
   4966 	struct in6_addr myaddr;
   4967 	struct timeval tv;
   4968 
   4969 	sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
   4970 
   4971 	myaddr.s6_addr[8] &= ~0x02;	/* u bit to "local" */
   4972 	microtime(&tv);
   4973 	if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
   4974 		myaddr.s6_addr[14] ^= 0xff;
   4975 		myaddr.s6_addr[15] ^= 0xff;
   4976 	} else {
   4977 		myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
   4978 		myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
   4979 	}
   4980 	if (suggest)
   4981 		bcopy(&myaddr, suggest, sizeof(myaddr));
   4982 }
   4983 #endif /*INET6*/
   4984 
   4985 static int
   4986 sppp_params(struct sppp *sp, int cmd, void *data)
   4987 {
   4988 	struct ifreq *ifr = (struct ifreq *)data;
   4989 	struct spppreq spr;
   4990 
   4991 #if 0
   4992 	/*
   4993 	 * ifr->ifr_data is supposed to point to a struct spppreq.
   4994 	 * Check the cmd word first before attempting to fetch all the
   4995 	 * data.
   4996 	 */
   4997 	if ((subcmd = fuword(ifr->ifr_data)) == -1)
   4998 		return EFAULT;
   4999 #endif
   5000 
   5001 	if (copyin((caddr_t)ifr->ifr_data, &spr, sizeof spr) != 0)
   5002 		return EFAULT;
   5003 
   5004 	switch (spr.cmd) {
   5005 	case SPPPIOGDEFS:
   5006 		if (cmd != (int)SIOCGIFGENERIC)
   5007 			return EINVAL;
   5008 		/*
   5009 		 * We copy over the entire current state, but clean
   5010 		 * out some of the stuff we don't wanna pass up.
   5011 		 * Remember, SIOCGIFGENERIC is unprotected, and can be
   5012 		 * called by any user.  No need to ever get PAP or
   5013 		 * CHAP secrets back to userland anyway.
   5014 		 */
   5015 		bcopy(sp, &spr.defs, sizeof(struct sppp));
   5016 		bzero(spr.defs.myauth.secret, AUTHKEYLEN);
   5017 		bzero(spr.defs.myauth.challenge, AUTHKEYLEN);
   5018 		bzero(spr.defs.hisauth.secret, AUTHKEYLEN);
   5019 		bzero(spr.defs.hisauth.challenge, AUTHKEYLEN);
   5020 		return copyout(&spr, (caddr_t)ifr->ifr_data, sizeof spr);
   5021 
   5022 	case SPPPIOSDEFS:
   5023 		if (cmd != (int)SIOCSIFGENERIC)
   5024 			return EINVAL;
   5025 		/*
   5026 		 * We have a very specific idea of which fields we allow
   5027 		 * being passed back from userland, so to not clobber our
   5028 		 * current state.  For one, we only allow setting
   5029 		 * anything if LCP is in dead phase.  Once the LCP
   5030 		 * negotiations started, the authentication settings must
   5031 		 * not be changed again.  (The administrator can force an
   5032 		 * ifconfig down in order to get LCP back into dead
   5033 		 * phase.)
   5034 		 *
   5035 		 * Also, we only allow for authentication parameters to be
   5036 		 * specified.
   5037 		 *
   5038 		 * XXX Should allow to set or clear pp_flags.
   5039 		 *
   5040 		 * Finally, if the respective authentication protocol to
   5041 		 * be used is set differently than 0, but the secret is
   5042 		 * passed as all zeros, we don't trash the existing secret.
   5043 		 * This allows an administrator to change the system name
   5044 		 * only without clobbering the secret (which he didn't get
   5045 		 * back in a previous SPPPIOGDEFS call).  However, the
   5046 		 * secrets are cleared if the authentication protocol is
   5047 		 * reset to 0.
   5048 		 */
   5049 		if (sp->pp_phase != PHASE_DEAD)
   5050 			return EBUSY;
   5051 
   5052 		if ((spr.defs.myauth.proto != 0 && spr.defs.myauth.proto != PPP_PAP &&
   5053 		     spr.defs.myauth.proto != PPP_CHAP) ||
   5054 		    (spr.defs.hisauth.proto != 0 && spr.defs.hisauth.proto != PPP_PAP &&
   5055 		     spr.defs.hisauth.proto != PPP_CHAP))
   5056 			return EINVAL;
   5057 
   5058 		if (spr.defs.myauth.proto == 0)
   5059 			/* resetting myauth */
   5060 			bzero(&sp->myauth, sizeof sp->myauth);
   5061 		else {
   5062 			/* setting/changing myauth */
   5063 			sp->myauth.proto = spr.defs.myauth.proto;
   5064 			bcopy(spr.defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
   5065 			if (spr.defs.myauth.secret[0] != '\0')
   5066 				bcopy(spr.defs.myauth.secret, sp->myauth.secret,
   5067 				      AUTHKEYLEN);
   5068 		}
   5069 		if (spr.defs.hisauth.proto == 0)
   5070 			/* resetting hisauth */
   5071 			bzero(&sp->hisauth, sizeof sp->hisauth);
   5072 		else {
   5073 			/* setting/changing hisauth */
   5074 			sp->hisauth.proto = spr.defs.hisauth.proto;
   5075 			sp->hisauth.flags = spr.defs.hisauth.flags;
   5076 			bcopy(spr.defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
   5077 			if (spr.defs.hisauth.secret[0] != '\0')
   5078 				bcopy(spr.defs.hisauth.secret, sp->hisauth.secret,
   5079 				      AUTHKEYLEN);
   5080 		}
   5081 		break;
   5082 
   5083 	default:
   5084 		return EINVAL;
   5085 	}
   5086 
   5087 	return 0;
   5088 }
   5089 
   5090 static void
   5091 sppp_phase_network(struct sppp *sp)
   5092 {
   5093 	STDDCL;
   5094 	int i;
   5095 	u_long mask;
   5096 
   5097 	sp->pp_phase = PHASE_NETWORK;
   5098 
   5099 	if(debug)
   5100 	{
   5101 		log(LOG_INFO, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
   5102 			sppp_phase_name(sp->pp_phase));
   5103 	}
   5104 
   5105 	/* Notify NCPs now. */
   5106 	for (i = 0; i < IDX_COUNT; i++)
   5107 		if ((cps[i])->flags & CP_NCP)
   5108 			(cps[i])->Open(sp);
   5109 
   5110 	/* Send Up events to all NCPs. */
   5111 	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
   5112 		if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
   5113 			(cps[i])->Up(sp);
   5114 
   5115 	/* if no NCP is starting, all this was in vain, close down */
   5116 	sppp_lcp_check_and_close(sp);
   5117 }
   5118 
   5119 
   5120 static const char *
   5121 sppp_cp_type_name(u_char type)
   5122 {
   5123 	static char buf[12];
   5124 	switch (type) {
   5125 	case CONF_REQ:   return "conf-req";
   5126 	case CONF_ACK:   return "conf-ack";
   5127 	case CONF_NAK:   return "conf-nak";
   5128 	case CONF_REJ:   return "conf-rej";
   5129 	case TERM_REQ:   return "term-req";
   5130 	case TERM_ACK:   return "term-ack";
   5131 	case CODE_REJ:   return "code-rej";
   5132 	case PROTO_REJ:  return "proto-rej";
   5133 	case ECHO_REQ:   return "echo-req";
   5134 	case ECHO_REPLY: return "echo-reply";
   5135 	case DISC_REQ:   return "discard-req";
   5136 	}
   5137 	sprintf (buf, "0x%x", type);
   5138 	return buf;
   5139 }
   5140 
   5141 static const char *
   5142 sppp_auth_type_name(u_short proto, u_char type)
   5143 {
   5144 	static char buf[12];
   5145 	switch (proto) {
   5146 	case PPP_CHAP:
   5147 		switch (type) {
   5148 		case CHAP_CHALLENGE:	return "challenge";
   5149 		case CHAP_RESPONSE:	return "response";
   5150 		case CHAP_SUCCESS:	return "success";
   5151 		case CHAP_FAILURE:	return "failure";
   5152 		}
   5153 	case PPP_PAP:
   5154 		switch (type) {
   5155 		case PAP_REQ:		return "req";
   5156 		case PAP_ACK:		return "ack";
   5157 		case PAP_NAK:		return "nak";
   5158 		}
   5159 	}
   5160 	sprintf (buf, "0x%x", type);
   5161 	return buf;
   5162 }
   5163 
   5164 static const char *
   5165 sppp_lcp_opt_name(u_char opt)
   5166 {
   5167 	static char buf[12];
   5168 	switch (opt) {
   5169 	case LCP_OPT_MRU:		return "mru";
   5170 	case LCP_OPT_ASYNC_MAP:		return "async-map";
   5171 	case LCP_OPT_AUTH_PROTO:	return "auth-proto";
   5172 	case LCP_OPT_QUAL_PROTO:	return "qual-proto";
   5173 	case LCP_OPT_MAGIC:		return "magic";
   5174 	case LCP_OPT_PROTO_COMP:	return "proto-comp";
   5175 	case LCP_OPT_ADDR_COMP:		return "addr-comp";
   5176 	}
   5177 	sprintf (buf, "0x%x", opt);
   5178 	return buf;
   5179 }
   5180 
   5181 static const char *
   5182 sppp_ipcp_opt_name(u_char opt)
   5183 {
   5184 	static char buf[12];
   5185 	switch (opt) {
   5186 	case IPCP_OPT_ADDRESSES:	return "addresses";
   5187 	case IPCP_OPT_COMPRESSION:	return "compression";
   5188 	case IPCP_OPT_ADDRESS:		return "address";
   5189 	}
   5190 	sprintf (buf, "0x%x", opt);
   5191 	return buf;
   5192 }
   5193 
   5194 #ifdef INET6
   5195 static const char *
   5196 sppp_ipv6cp_opt_name(u_char opt)
   5197 {
   5198 	static char buf[12];
   5199 	switch (opt) {
   5200 	case IPV6CP_OPT_IFID:		return "ifid";
   5201 	case IPV6CP_OPT_COMPRESSION:	return "compression";
   5202 	}
   5203 	sprintf (buf, "0x%x", opt);
   5204 	return buf;
   5205 }
   5206 #endif
   5207 
   5208 static const char *
   5209 sppp_state_name(int state)
   5210 {
   5211 	switch (state) {
   5212 	case STATE_INITIAL:	return "initial";
   5213 	case STATE_STARTING:	return "starting";
   5214 	case STATE_CLOSED:	return "closed";
   5215 	case STATE_STOPPED:	return "stopped";
   5216 	case STATE_CLOSING:	return "closing";
   5217 	case STATE_STOPPING:	return "stopping";
   5218 	case STATE_REQ_SENT:	return "req-sent";
   5219 	case STATE_ACK_RCVD:	return "ack-rcvd";
   5220 	case STATE_ACK_SENT:	return "ack-sent";
   5221 	case STATE_OPENED:	return "opened";
   5222 	}
   5223 	return "illegal";
   5224 }
   5225 
   5226 static const char *
   5227 sppp_phase_name(enum ppp_phase phase)
   5228 {
   5229 	switch (phase) {
   5230 	case PHASE_DEAD:	return "dead";
   5231 	case PHASE_ESTABLISH:	return "establish";
   5232 	case PHASE_TERMINATE:	return "terminate";
   5233 	case PHASE_AUTHENTICATE: return "authenticate";
   5234 	case PHASE_NETWORK:	return "network";
   5235 	}
   5236 	return "illegal";
   5237 }
   5238 
   5239 static const char *
   5240 sppp_proto_name(u_short proto)
   5241 {
   5242 	static char buf[12];
   5243 	switch (proto) {
   5244 	case PPP_LCP:	return "lcp";
   5245 	case PPP_IPCP:	return "ipcp";
   5246 	case PPP_PAP:	return "pap";
   5247 	case PPP_CHAP:	return "chap";
   5248 	case PPP_IPV6CP: return "ipv6cp";
   5249 	}
   5250 	sprintf(buf, "0x%x", (unsigned)proto);
   5251 	return buf;
   5252 }
   5253 
   5254 static void
   5255 sppp_print_bytes(const u_char *p, u_short len)
   5256 {
   5257 	addlog(" %02x", *p++);
   5258 	while (--len > 0)
   5259 		addlog("-%02x", *p++);
   5260 }
   5261 
   5262 static void
   5263 sppp_print_string(const char *p, u_short len)
   5264 {
   5265 	u_char c;
   5266 
   5267 	while (len-- > 0) {
   5268 		c = *p++;
   5269 		/*
   5270 		 * Print only ASCII chars directly.  RFC 1994 recommends
   5271 		 * using only them, but we don't rely on it.  */
   5272 		if (c < ' ' || c > '~')
   5273 			addlog("\\x%x", c);
   5274 		else
   5275 			addlog("%c", c);
   5276 	}
   5277 }
   5278 
   5279 static const char *
   5280 sppp_dotted_quad(u_long addr)
   5281 {
   5282 	static char s[16];
   5283 	sprintf(s, "%d.%d.%d.%d",
   5284 		(int)((addr >> 24) & 0xff),
   5285 		(int)((addr >> 16) & 0xff),
   5286 		(int)((addr >> 8) & 0xff),
   5287 		(int)(addr & 0xff));
   5288 	return s;
   5289 }
   5290 
   5291 static int
   5292 sppp_strnlen(u_char *p, int max)
   5293 {
   5294 	int len;
   5295 
   5296 	for (len = 0; len < max && *p; ++p)
   5297 		++len;
   5298 	return len;
   5299 }
   5300 
   5301 /* a dummy, used to drop uninteresting events */
   5302 static void
   5303 sppp_null(struct sppp *unused)
   5304 {
   5305 	/* do just nothing */
   5306 }
   5307 /*
   5308  * This file is large.  Tell emacs to highlight it nevertheless.
   5309  *
   5310  * Local Variables:
   5311  * hilit-auto-highlight-maxout: 120000
   5312  * End:
   5313  */
   5314