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