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