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