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