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