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