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