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