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