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