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