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