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