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