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