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