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