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