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