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