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