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