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