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