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