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