if_spppsubr.c revision 25955
1/*
2 * Synchronous PPP/Cisco link level subroutines.
3 * Keepalive protocol implemented in both Cisco and PPP modes.
4 *
5 * Copyright (C) 1994 Cronyx 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 1.9, Wed Oct  4 18:58:15 MSK 1995
19 *
20 * $Id: if_spppsubr.c,v 1.19 1997/05/19 22:03:09 joerg Exp $
21 */
22
23#include <sys/param.h>
24#include <sys/systm.h>
25#include <sys/kernel.h>
26#include <sys/sockio.h>
27#include <sys/socket.h>
28#include <sys/syslog.h>
29#include <sys/mbuf.h>
30
31#include <net/if.h>
32#include <net/netisr.h>
33#include <net/if_types.h>
34
35#ifdef INET
36#include <netinet/in.h>
37#include <netinet/in_systm.h>
38#include <netinet/in_var.h>
39#include <netinet/ip.h>
40#include <netinet/tcp.h>
41#include <netinet/if_ether.h>
42#endif
43
44#ifdef IPX
45#include <netipx/ipx.h>
46#include <netipx/ipx_if.h>
47#endif
48
49#ifdef NS
50#include <netns/ns.h>
51#include <netns/ns_if.h>
52#endif
53
54#ifdef ISO
55#include <netiso/argo_debug.h>
56#include <netiso/iso.h>
57#include <netiso/iso_var.h>
58#include <netiso/iso_snpac.h>
59#endif
60
61#include <net/if_sppp.h>
62
63#define MAXALIVECNT     3               /* max. alive packets */
64
65/*
66 * Interface flags that can be set in an ifconfig command.
67 *
68 * Setting link0 will make the link passive, i.e. it will be marked
69 * as being administrative openable, but won't be opened to begin
70 * with.  Incoming calls will be answered, or subsequent calls with
71 * -link1 will cause the administrative open of the LCP layer.
72 *
73 * Setting link1 will cause the link to auto-dial only as packets
74 * arrive to be sent.
75 */
76
77#define IFF_PASSIVE	IFF_LINK0	/* wait passively for connection */
78#define IFF_AUTO	IFF_LINK1	/* auto-dial on output */
79
80#define PPP_ALLSTATIONS 0xff            /* All-Stations broadcast address */
81#define PPP_UI          0x03            /* Unnumbered Information */
82#define PPP_IP          0x0021          /* Internet Protocol */
83#define PPP_ISO         0x0023          /* ISO OSI Protocol */
84#define PPP_XNS         0x0025          /* Xerox NS Protocol */
85#define PPP_IPX         0x002b          /* Novell IPX Protocol */
86#define PPP_LCP         0xc021          /* Link Control Protocol */
87#define PPP_IPCP        0x8021          /* Internet Protocol Control Protocol */
88
89#define CONF_REQ	1		/* PPP configure request */
90#define CONF_ACK	2		/* PPP configure acknowledge */
91#define CONF_NAK	3		/* PPP configure negative ack */
92#define CONF_REJ	4		/* PPP configure reject */
93#define TERM_REQ	5		/* PPP terminate request */
94#define TERM_ACK	6		/* PPP terminate acknowledge */
95#define CODE_REJ	7		/* PPP code reject */
96#define PROTO_REJ	8		/* PPP protocol reject */
97#define ECHO_REQ	9		/* PPP echo request */
98#define ECHO_REPLY	10		/* PPP echo reply */
99#define DISC_REQ	11		/* PPP discard request */
100
101#define LCP_OPT_MRU             1       /* maximum receive unit */
102#define LCP_OPT_ASYNC_MAP       2       /* async control character map */
103#define LCP_OPT_AUTH_PROTO      3       /* authentication protocol */
104#define LCP_OPT_QUAL_PROTO      4       /* quality protocol */
105#define LCP_OPT_MAGIC           5       /* magic number */
106#define LCP_OPT_RESERVED        6       /* reserved */
107#define LCP_OPT_PROTO_COMP      7       /* protocol field compression */
108#define LCP_OPT_ADDR_COMP       8       /* address/control field compression */
109
110#define IPCP_OPT_ADDRESSES	1	/* both IP addresses; deprecated */
111#define IPCP_OPT_COMPRESSION	2	/* IP compression protocol (VJ) */
112#define IPCP_OPT_ADDRESS	3	/* local IP address */
113
114#define CISCO_MULTICAST         0x8f    /* Cisco multicast address */
115#define CISCO_UNICAST           0x0f    /* Cisco unicast address */
116#define CISCO_KEEPALIVE         0x8035  /* Cisco keepalive protocol */
117#define CISCO_ADDR_REQ          0       /* Cisco address request */
118#define CISCO_ADDR_REPLY        1       /* Cisco address reply */
119#define CISCO_KEEPALIVE_REQ     2       /* Cisco keepalive request */
120
121/* states are named and numbered according to RFC 1661 */
122#define STATE_INITIAL	0
123#define STATE_STARTING	1
124#define STATE_CLOSED	2
125#define STATE_STOPPED	3
126#define STATE_CLOSING	4
127#define STATE_STOPPING	5
128#define STATE_REQ_SENT	6
129#define STATE_ACK_RCVD	7
130#define STATE_ACK_SENT	8
131#define STATE_OPENED	9
132
133struct ppp_header {
134	u_char address;
135	u_char control;
136	u_short protocol;
137};
138#define PPP_HEADER_LEN          sizeof (struct ppp_header)
139
140struct lcp_header {
141	u_char type;
142	u_char ident;
143	u_short len;
144};
145#define LCP_HEADER_LEN          sizeof (struct lcp_header)
146
147struct cisco_packet {
148	u_long type;
149	u_long par1;
150	u_long par2;
151	u_short rel;
152	u_short time0;
153	u_short time1;
154};
155#define CISCO_PACKET_LEN 18
156
157/*
158 * We follow the spelling and capitalization of RFC 1661 here, to make
159 * it easier comparing with the standard.  Please refer to this RFC in
160 * case you can't make sense out of these abbreviation; it will also
161 * explain the semantics related to the various events and actions.
162 */
163struct cp {
164	u_short	proto;		/* PPP control protocol number */
165	u_char protoidx;	/* index into state table in struct sppp */
166	u_char flags;
167#define CP_LCP		0x01	/* this is the LCP */
168#define CP_AUTH		0x02	/* this is an authentication protocol */
169#define CP_NCP		0x04	/* this is a NCP */
170#define CP_QUAL		0x08	/* this is a quality reporting protocol */
171	const char *name;	/* name of this control protocol */
172	/* event handlers */
173	void	(*Up)(struct sppp *sp);
174	void	(*Down)(struct sppp *sp);
175	void	(*Open)(struct sppp *sp);
176	void	(*Close)(struct sppp *sp);
177	void	(*TO)(void *sp);
178	int	(*RCR)(struct sppp *sp, struct lcp_header *h, int len);
179	void	(*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
180	void	(*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
181	/* actions */
182	void	(*tlu)(struct sppp *sp);
183	void	(*tld)(struct sppp *sp);
184	void	(*tls)(struct sppp *sp);
185	void	(*tlf)(struct sppp *sp);
186	void	(*scr)(struct sppp *sp);
187};
188
189static struct sppp *spppq;
190
191/*
192 * The following disgusting hack gets around the problem that IP TOS
193 * can't be set yet.  We want to put "interactive" traffic on a high
194 * priority queue.  To decide if traffic is interactive, we check that
195 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
196 */
197static u_short interactive_ports[8] = {
198	0,	513,	0,	0,
199	0,	21,	0,	23,
200};
201#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
202
203/* almost every function needs these */
204#define STDDCL							\
205	struct ifnet *ifp = &sp->pp_if;				\
206	int debug = ifp->if_flags & IFF_DEBUG
207
208static int sppp_output(struct ifnet *ifp, struct mbuf *m,
209		       struct sockaddr *dst, struct rtentry *rt);
210
211static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
212static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
213
214static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
215			  struct mbuf *m);
216static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
217			 u_char ident, u_short len, void *data);
218static void sppp_cp_timeout(void *arg);
219static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
220				 int newstate);
221
222static void sppp_up_event(const struct cp *cp, struct sppp *sp);
223static void sppp_down_event(const struct cp *cp, struct sppp *sp);
224static void sppp_open_event(const struct cp *cp, struct sppp *sp);
225static void sppp_close_event(const struct cp *cp, struct sppp *sp);
226static void sppp_to_event(const struct cp *cp, struct sppp *sp);
227
228static void sppp_lcp_init(struct sppp *sp);
229static void sppp_lcp_up(struct sppp *sp);
230static void sppp_lcp_down(struct sppp *sp);
231static void sppp_lcp_open(struct sppp *sp);
232static void sppp_lcp_close(struct sppp *sp);
233static void sppp_lcp_TO(void *sp);
234static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
235static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
236static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
237static void sppp_lcp_tlu(struct sppp *sp);
238static void sppp_lcp_tld(struct sppp *sp);
239static void sppp_lcp_tls(struct sppp *sp);
240static void sppp_lcp_tlf(struct sppp *sp);
241static void sppp_lcp_scr(struct sppp *sp);
242static void sppp_lcp_check(struct sppp *sp);
243
244static void sppp_ipcp_init(struct sppp *sp);
245static void sppp_ipcp_up(struct sppp *sp);
246static void sppp_ipcp_down(struct sppp *sp);
247static void sppp_ipcp_open(struct sppp *sp);
248static void sppp_ipcp_close(struct sppp *sp);
249static void sppp_ipcp_TO(void *sp);
250static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
251static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
252static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
253static void sppp_ipcp_tlu(struct sppp *sp);
254static void sppp_ipcp_tld(struct sppp *sp);
255static void sppp_ipcp_tls(struct sppp *sp);
256static void sppp_ipcp_tlf(struct sppp *sp);
257static void sppp_ipcp_scr(struct sppp *sp);
258
259static const char *sppp_cp_type_name(u_char type);
260static const char *sppp_lcp_opt_name(u_char opt);
261static const char *sppp_ipcp_opt_name(u_char opt);
262static const char *sppp_state_name(int state);
263static const char *sppp_phase_name(enum ppp_phase phase);
264static const char *sppp_proto_name(u_short proto);
265
266static void sppp_keepalive(void *dummy);
267static void sppp_qflush(struct ifqueue *ifq);
268
269static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst);
270static void sppp_set_ip_addr(struct sppp *sp, u_long src);
271
272static void sppp_print_bytes(u_char *p, u_short len);
273
274/* our control protocol descriptors */
275const struct cp lcp = {
276	PPP_LCP, IDX_LCP, CP_LCP, "lcp",
277	sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
278	sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
279	sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
280	sppp_lcp_scr
281};
282
283const struct cp ipcp = {
284	PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
285	sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
286	sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
287	sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
288	sppp_ipcp_scr
289};
290
291const struct cp *cps[IDX_COUNT] = {
292	&lcp,			/* IDX_LCP */
293	&ipcp,			/* IDX_IPCP */
294};
295
296
297/*
298 * Exported functions, comprising our interface to the lower layer.
299 */
300
301/*
302 * Process the received packet.
303 */
304void
305sppp_input(struct ifnet *ifp, struct mbuf *m)
306{
307	struct ppp_header *h;
308	struct ifqueue *inq = 0;
309	int s;
310	struct sppp *sp = (struct sppp *)ifp;
311	int debug = ifp->if_flags & IFF_DEBUG;
312
313	if (ifp->if_flags & IFF_UP)
314		/* Count received bytes, add FCS and one flag */
315		ifp->if_ibytes += m->m_pkthdr.len + 3;
316
317	if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
318		/* Too small packet, drop it. */
319		if (debug)
320			log(LOG_DEBUG,
321			    "%s%d: input packet is too small, %d bytes\n",
322			    ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
323	  drop:
324		++ifp->if_ierrors;
325		++ifp->if_iqdrops;
326		m_freem (m);
327		return;
328	}
329
330	/* Get PPP header. */
331	h = mtod (m, struct ppp_header*);
332	m_adj (m, PPP_HEADER_LEN);
333
334	switch (h->address) {
335	case PPP_ALLSTATIONS:
336		if (h->control != PPP_UI)
337			goto invalid;
338		if (sp->pp_flags & PP_CISCO) {
339			if (debug)
340				log(LOG_DEBUG,
341				    "%s%d: PPP packet in Cisco mode "
342				    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
343				    ifp->if_name, ifp->if_unit,
344				    h->address, h->control, ntohs(h->protocol));
345			goto drop;
346		}
347		switch (ntohs (h->protocol)) {
348		default:
349			if (sp->state[IDX_LCP] == STATE_OPENED)
350				sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
351					++sp->pp_seq, m->m_pkthdr.len + 2,
352					&h->protocol);
353			if (debug)
354				log(LOG_DEBUG,
355				    "%s%d: invalid input protocol "
356				    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
357				    ifp->if_name, ifp->if_unit,
358				    h->address, h->control, ntohs(h->protocol));
359			++ifp->if_noproto;
360			goto drop;
361		case PPP_LCP:
362			sppp_cp_input(&lcp, (struct sppp*)ifp, m);
363			m_freem (m);
364			return;
365#ifdef INET
366		case PPP_IPCP:
367			if (sp->pp_phase == PHASE_NETWORK)
368				sppp_cp_input(&ipcp, (struct sppp*) ifp, m);
369			m_freem (m);
370			return;
371		case PPP_IP:
372			if (sp->state[IDX_IPCP] == STATE_OPENED) {
373				schednetisr (NETISR_IP);
374				inq = &ipintrq;
375			}
376			break;
377#endif
378#ifdef IPX
379		case PPP_IPX:
380			/* IPX IPXCP not implemented yet */
381			if (sp->pp_phase == PHASE_NETWORK) {
382				schednetisr (NETISR_IPX);
383				inq = &ipxintrq;
384			}
385			break;
386#endif
387#ifdef NS
388		case PPP_XNS:
389			/* XNS IDPCP not implemented yet */
390			if (sp->pp_phase == PHASE_NETWORK) {
391				schednetisr (NETISR_NS);
392				inq = &nsintrq;
393			}
394			break;
395#endif
396#ifdef ISO
397		case PPP_ISO:
398			/* OSI NLCP not implemented yet */
399			if (sp->pp_phase == PHASE_NETWORK) {
400				schednetisr (NETISR_ISO);
401				inq = &clnlintrq;
402			}
403			break;
404#endif
405		}
406		break;
407	case CISCO_MULTICAST:
408	case CISCO_UNICAST:
409		/* Don't check the control field here (RFC 1547). */
410		if (! (sp->pp_flags & PP_CISCO)) {
411			if (debug)
412				log(LOG_DEBUG,
413				    "%s%d: Cisco packet in PPP mode "
414				    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
415				    ifp->if_name, ifp->if_unit,
416				    h->address, h->control, ntohs(h->protocol));
417			goto drop;
418		}
419		switch (ntohs (h->protocol)) {
420		default:
421			++ifp->if_noproto;
422			goto invalid;
423		case CISCO_KEEPALIVE:
424			sppp_cisco_input ((struct sppp*) ifp, m);
425			m_freem (m);
426			return;
427#ifdef INET
428		case ETHERTYPE_IP:
429			schednetisr (NETISR_IP);
430			inq = &ipintrq;
431			break;
432#endif
433#ifdef IPX
434		case ETHERTYPE_IPX:
435			schednetisr (NETISR_IPX);
436			inq = &ipxintrq;
437			break;
438#endif
439#ifdef NS
440		case ETHERTYPE_NS:
441			schednetisr (NETISR_NS);
442			inq = &nsintrq;
443			break;
444#endif
445		}
446		break;
447	default:        /* Invalid PPP packet. */
448	  invalid:
449		if (debug)
450			log(LOG_DEBUG,
451			    "%s%d: invalid input packet "
452			    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
453			    ifp->if_name, ifp->if_unit,
454			    h->address, h->control, ntohs(h->protocol));
455		goto drop;
456	}
457
458	if (! (ifp->if_flags & IFF_UP) || ! inq)
459		goto drop;
460
461	/* Check queue. */
462	s = splimp();
463	if (IF_QFULL (inq)) {
464		/* Queue overflow. */
465		IF_DROP(inq);
466		splx(s);
467		if (debug)
468			log(LOG_DEBUG, "%s%d: protocol queue overflow\n",
469				ifp->if_name, ifp->if_unit);
470		goto drop;
471	}
472	IF_ENQUEUE(inq, m);
473	splx(s);
474}
475
476/*
477 * Enqueue transmit packet.
478 */
479static int
480sppp_output(struct ifnet *ifp, struct mbuf *m,
481	    struct sockaddr *dst, struct rtentry *rt)
482{
483	struct sppp *sp = (struct sppp*) ifp;
484	struct ppp_header *h;
485	struct ifqueue *ifq;
486	int s, rv = 0;
487
488	s = splimp();
489
490	if ((ifp->if_flags & IFF_UP) == 0 ||
491	    (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
492		m_freem (m);
493		splx (s);
494		return (ENETDOWN);
495	}
496
497	if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
498		/*
499		 * Interface is not yet running, but auto-dial.  Need
500		 * to start LCP for it.
501		 */
502		ifp->if_flags |= IFF_RUNNING;
503		splx(s);
504		lcp.Open(sp);
505		s = splimp();
506	}
507
508	ifq = &ifp->if_snd;
509#ifdef INET
510	/*
511	 * Put low delay, telnet, rlogin and ftp control packets
512	 * in front of the queue.
513	 */
514	if (dst->sa_family == AF_INET) {
515		struct ip *ip = mtod (m, struct ip*);
516		struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
517
518		if (! IF_QFULL (&sp->pp_fastq) &&
519		    ((ip->ip_tos & IPTOS_LOWDELAY) ||
520	    	    ip->ip_p == IPPROTO_TCP &&
521	    	    m->m_len >= sizeof (struct ip) + sizeof (struct tcphdr) &&
522	    	    (INTERACTIVE (ntohs (tcp->th_sport)) ||
523	    	    INTERACTIVE (ntohs (tcp->th_dport)))))
524			ifq = &sp->pp_fastq;
525	}
526#endif
527
528	/*
529	 * Prepend general data packet PPP header. For now, IP only.
530	 */
531	M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
532	if (! m) {
533		if (ifp->if_flags & IFF_DEBUG)
534			log(LOG_DEBUG, "%s%d: no memory for transmit header\n",
535				ifp->if_name, ifp->if_unit);
536		++ifp->if_oerrors;
537		splx (s);
538		return (ENOBUFS);
539	}
540	h = mtod (m, struct ppp_header*);
541	if (sp->pp_flags & PP_CISCO) {
542		h->address = CISCO_MULTICAST;        /* broadcast address */
543		h->control = 0;
544	} else {
545		h->address = PPP_ALLSTATIONS;        /* broadcast address */
546		h->control = PPP_UI;                 /* Unnumbered Info */
547	}
548
549	switch (dst->sa_family) {
550#ifdef INET
551	case AF_INET:   /* Internet Protocol */
552		if (sp->pp_flags & PP_CISCO)
553			h->protocol = htons (ETHERTYPE_IP);
554		else {
555			/*
556			 * Don't choke with an ENETDOWN early.  It's
557			 * possible that we just started dialing out,
558			 * so don't drop the packet immediately.  If
559			 * we notice that we run out of buffer space
560			 * below, we will however remember that we are
561			 * not ready to carry IP packets, and return
562			 * ENETDOWN, as opposed to ENOBUFS.
563			 */
564			h->protocol = htons(PPP_IP);
565			if (sp->state[IDX_IPCP] != STATE_OPENED)
566				rv = ENETDOWN;
567		}
568		break;
569#endif
570#ifdef NS
571	case AF_NS:     /* Xerox NS Protocol */
572		h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
573			ETHERTYPE_NS : PPP_XNS);
574		break;
575#endif
576#ifdef IPX
577	case AF_IPX:     /* Novell IPX Protocol */
578		h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
579			ETHERTYPE_IPX : PPP_IPX);
580		break;
581#endif
582#ifdef ISO
583	case AF_ISO:    /* ISO OSI Protocol */
584		if (sp->pp_flags & PP_CISCO)
585			goto nosupport;
586		h->protocol = htons (PPP_ISO);
587		break;
588nosupport:
589#endif
590	default:
591		m_freem (m);
592		++ifp->if_oerrors;
593		splx (s);
594		return (EAFNOSUPPORT);
595	}
596
597	/*
598	 * Queue message on interface, and start output if interface
599	 * not yet active.
600	 */
601	if (IF_QFULL (ifq)) {
602		IF_DROP (&ifp->if_snd);
603		m_freem (m);
604		++ifp->if_oerrors;
605		splx (s);
606		return (rv? rv: ENOBUFS);
607	}
608	IF_ENQUEUE (ifq, m);
609	if (! (ifp->if_flags & IFF_OACTIVE))
610		(*ifp->if_start) (ifp);
611
612	/*
613	 * Count output packets and bytes.
614	 * The packet length includes header, FCS and 1 flag,
615	 * according to RFC 1333.
616	 */
617	ifp->if_obytes += m->m_pkthdr.len + 3;
618	splx (s);
619	return (0);
620}
621
622void
623sppp_attach(struct ifnet *ifp)
624{
625	struct sppp *sp = (struct sppp*) ifp;
626
627	/* Initialize keepalive handler. */
628	if (! spppq)
629		timeout (sppp_keepalive, 0, hz * 10);
630
631	/* Insert new entry into the keepalive list. */
632	sp->pp_next = spppq;
633	spppq = sp;
634
635	sp->pp_if.if_type = IFT_PPP;
636	sp->pp_if.if_output = sppp_output;
637	sp->pp_fastq.ifq_maxlen = 32;
638	sp->pp_loopcnt = 0;
639	sp->pp_alivecnt = 0;
640	sp->pp_seq = 0;
641	sp->pp_rseq = 0;
642	sp->pp_phase = PHASE_DEAD;
643	sp->pp_up = lcp.Up;
644	sp->pp_down = lcp.Down;
645
646	sppp_lcp_init(sp);
647	sppp_ipcp_init(sp);
648}
649
650void
651sppp_detach(struct ifnet *ifp)
652{
653	struct sppp **q, *p, *sp = (struct sppp*) ifp;
654	int i;
655
656	/* Remove the entry from the keepalive list. */
657	for (q = &spppq; (p = *q); q = &p->pp_next)
658		if (p == sp) {
659			*q = p->pp_next;
660			break;
661		}
662
663	/* Stop keepalive handler. */
664	if (! spppq)
665		untimeout (sppp_keepalive, 0);
666
667	for (i = 0; i < IDX_COUNT; i++)
668		untimeout((cps[i])->TO, (void *)sp);
669}
670
671/*
672 * Flush the interface output queue.
673 */
674void
675sppp_flush(struct ifnet *ifp)
676{
677	struct sppp *sp = (struct sppp*) ifp;
678
679	sppp_qflush (&sp->pp_if.if_snd);
680	sppp_qflush (&sp->pp_fastq);
681}
682
683/*
684 * Check if the output queue is empty.
685 */
686int
687sppp_isempty(struct ifnet *ifp)
688{
689	struct sppp *sp = (struct sppp*) ifp;
690	int empty, s;
691
692	s = splimp();
693	empty = !sp->pp_fastq.ifq_head && !sp->pp_if.if_snd.ifq_head;
694	splx(s);
695	return (empty);
696}
697
698/*
699 * Get next packet to send.
700 */
701struct mbuf *
702sppp_dequeue(struct ifnet *ifp)
703{
704	struct sppp *sp = (struct sppp*) ifp;
705	struct mbuf *m;
706	int s;
707
708	s = splimp();
709	IF_DEQUEUE (&sp->pp_fastq, m);
710	if (! m)
711		IF_DEQUEUE (&sp->pp_if.if_snd, m);
712	splx (s);
713	return (m);
714}
715
716/*
717 * Process an ioctl request.  Called on low priority level.
718 */
719int
720sppp_ioctl(struct ifnet *ifp, int cmd, void *data)
721{
722	struct ifreq *ifr = (struct ifreq*) data;
723	struct sppp *sp = (struct sppp*) ifp;
724	int s, going_up, going_down, newmode;
725
726	s = splimp();
727	switch (cmd) {
728	case SIOCAIFADDR:
729	case SIOCSIFDSTADDR:
730		break;
731
732	case SIOCSIFADDR:
733		if_up(ifp);
734		/* fall through... */
735
736	case SIOCSIFFLAGS:
737		going_up = ifp->if_flags & IFF_UP &&
738			(ifp->if_flags & IFF_RUNNING) == 0;
739		going_down = (ifp->if_flags & IFF_UP) == 0 &&
740			ifp->if_flags & IFF_RUNNING;
741		newmode = ifp->if_flags & (IFF_AUTO | IFF_PASSIVE);
742		if (newmode == (IFF_AUTO | IFF_PASSIVE)) {
743			/* sanity */
744			newmode = IFF_PASSIVE;
745			ifp->if_flags &= ~IFF_AUTO;
746		}
747
748		if (going_up || going_down)
749			lcp.Close(sp);
750		if (going_up && newmode == 0) {
751			/* neither auto-dial nor passive */
752			ifp->if_flags |= IFF_RUNNING;
753			if (!(sp->pp_flags & PP_CISCO))
754				lcp.Open(sp);
755		} else if (going_down)
756			ifp->if_flags &= ~IFF_RUNNING;
757
758		break;
759
760#ifdef SIOCSIFMTU
761#ifndef ifr_mtu
762#define ifr_mtu ifr_metric
763#endif
764	case SIOCSIFMTU:
765		if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
766			return (EINVAL);
767		ifp->if_mtu = ifr->ifr_mtu;
768		break;
769#endif
770#ifdef SLIOCSETMTU
771	case SLIOCSETMTU:
772		if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
773			return (EINVAL);
774		ifp->if_mtu = *(short*)data;
775		break;
776#endif
777#ifdef SIOCGIFMTU
778	case SIOCGIFMTU:
779		ifr->ifr_mtu = ifp->if_mtu;
780		break;
781#endif
782#ifdef SLIOCGETMTU
783	case SLIOCGETMTU:
784		*(short*)data = ifp->if_mtu;
785		break;
786#endif
787	case SIOCADDMULTI:
788	case SIOCDELMULTI:
789		break;
790
791	default:
792		splx(s);
793		return (ENOTTY);
794	}
795	splx(s);
796	return (0);
797}
798
799
800/*
801 * Cisco framing implementation.
802 */
803
804/*
805 * Handle incoming Cisco keepalive protocol packets.
806 */
807static void
808sppp_cisco_input(struct sppp *sp, struct mbuf *m)
809{
810	STDDCL;
811	struct cisco_packet *h;
812	struct ifaddr *ifa;
813
814	if (m->m_pkthdr.len != CISCO_PACKET_LEN) {
815		if (debug)
816			log(LOG_DEBUG,
817			    "%s%d: invalid cisco packet length: %d bytes\n",
818			    ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
819		return;
820	}
821	h = mtod (m, struct cisco_packet*);
822	if (debug)
823		log(LOG_DEBUG,
824		    "%s%d: cisco input: %d bytes "
825		    "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
826		    ifp->if_name, ifp->if_unit, m->m_pkthdr.len,
827		    ntohl (h->type), h->par1, h->par2, h->rel,
828		    h->time0, h->time1);
829	switch (ntohl (h->type)) {
830	default:
831		if (debug)
832			addlog("%s%d: unknown cisco packet type: 0x%lx\n",
833			       ifp->if_name, ifp->if_unit, ntohl (h->type));
834		break;
835	case CISCO_ADDR_REPLY:
836		/* Reply on address request, ignore */
837		break;
838	case CISCO_KEEPALIVE_REQ:
839		sp->pp_alivecnt = 0;
840		sp->pp_rseq = ntohl (h->par1);
841		if (sp->pp_seq == sp->pp_rseq) {
842			/* Local and remote sequence numbers are equal.
843			 * Probably, the line is in loopback mode. */
844			if (sp->pp_loopcnt >= MAXALIVECNT) {
845				printf ("%s%d: loopback\n",
846					ifp->if_name, ifp->if_unit);
847				sp->pp_loopcnt = 0;
848				if (ifp->if_flags & IFF_UP) {
849					if_down (ifp);
850					sppp_qflush (&sp->pp_fastq);
851				}
852			}
853			++sp->pp_loopcnt;
854
855			/* Generate new local sequence number */
856			sp->pp_seq ^= time.tv_sec ^ time.tv_usec;
857			break;
858		}
859			sp->pp_loopcnt = 0;
860		if (! (ifp->if_flags & IFF_UP) &&
861		    (ifp->if_flags & IFF_RUNNING)) {
862			ifp->if_flags |= IFF_UP;
863			printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
864		}
865		break;
866	case CISCO_ADDR_REQ:
867		for (ifa=ifp->if_addrhead.tqh_first; ifa;
868		     ifa=ifa->ifa_link.tqe_next)
869			if (ifa->ifa_addr->sa_family == AF_INET)
870				break;
871		if (! ifa) {
872			if (debug)
873				addlog("%s%d: unknown address for cisco request\n",
874				       ifp->if_name, ifp->if_unit);
875			return;
876		}
877		sppp_cisco_send (sp, CISCO_ADDR_REPLY,
878			ntohl (((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr),
879			ntohl (((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr.s_addr));
880		break;
881	}
882}
883
884/*
885 * Send Cisco keepalive packet.
886 */
887static void
888sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
889{
890	STDDCL;
891	struct ppp_header *h;
892	struct cisco_packet *ch;
893	struct mbuf *m;
894	u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
895
896	MGETHDR (m, M_DONTWAIT, MT_DATA);
897	if (! m)
898		return;
899	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
900	m->m_pkthdr.rcvif = 0;
901
902	h = mtod (m, struct ppp_header*);
903	h->address = CISCO_MULTICAST;
904	h->control = 0;
905	h->protocol = htons (CISCO_KEEPALIVE);
906
907	ch = (struct cisco_packet*) (h + 1);
908	ch->type = htonl (type);
909	ch->par1 = htonl (par1);
910	ch->par2 = htonl (par2);
911	ch->rel = -1;
912	ch->time0 = htons ((u_short) (t >> 16));
913	ch->time1 = htons ((u_short) t);
914
915	if (debug)
916		log(LOG_DEBUG,
917		    "%s%d: cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
918			ifp->if_name, ifp->if_unit, ntohl (ch->type), ch->par1,
919			ch->par2, ch->rel, ch->time0, ch->time1);
920
921	if (IF_QFULL (&sp->pp_fastq)) {
922		IF_DROP (&ifp->if_snd);
923		m_freem (m);
924	} else
925		IF_ENQUEUE (&sp->pp_fastq, m);
926	if (! (ifp->if_flags & IFF_OACTIVE))
927		(*ifp->if_start) (ifp);
928	ifp->if_obytes += m->m_pkthdr.len + 3;
929}
930
931/*
932 * PPP protocol implementation.
933 */
934
935/*
936 * Send PPP control protocol packet.
937 */
938static void
939sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
940	     u_char ident, u_short len, void *data)
941{
942	STDDCL;
943	struct ppp_header *h;
944	struct lcp_header *lh;
945	struct mbuf *m;
946
947	if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
948		len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
949	MGETHDR (m, M_DONTWAIT, MT_DATA);
950	if (! m)
951		return;
952	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
953	m->m_pkthdr.rcvif = 0;
954
955	h = mtod (m, struct ppp_header*);
956	h->address = PPP_ALLSTATIONS;        /* broadcast address */
957	h->control = PPP_UI;                 /* Unnumbered Info */
958	h->protocol = htons (proto);         /* Link Control Protocol */
959
960	lh = (struct lcp_header*) (h + 1);
961	lh->type = type;
962	lh->ident = ident;
963	lh->len = htons (LCP_HEADER_LEN + len);
964	if (len)
965		bcopy (data, lh+1, len);
966
967	if (debug) {
968		log(LOG_DEBUG, "%s%d: %s output <%s id=0x%x len=%d",
969		    ifp->if_name, ifp->if_unit,
970		    sppp_proto_name(proto),
971		    sppp_cp_type_name (lh->type), lh->ident,
972		    ntohs (lh->len));
973		if (len)
974			sppp_print_bytes ((u_char*) (lh+1), len);
975		addlog(">\n");
976	}
977	if (IF_QFULL (&sp->pp_fastq)) {
978		IF_DROP (&ifp->if_snd);
979		m_freem (m);
980		++ifp->if_oerrors;
981	} else
982		IF_ENQUEUE (&sp->pp_fastq, m);
983	if (! (ifp->if_flags & IFF_OACTIVE))
984		(*ifp->if_start) (ifp);
985	ifp->if_obytes += m->m_pkthdr.len + 3;
986}
987
988/*
989 * Handle incoming PPP control protocol packets.
990 */
991static void
992sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
993{
994	STDDCL;
995	struct lcp_header *h;
996	int len = m->m_pkthdr.len;
997	int rv;
998	u_char *p;
999
1000	if (len < 4) {
1001		if (debug)
1002			log(LOG_DEBUG,
1003			    "%s%d: %s invalid packet length: %d bytes\n",
1004			    ifp->if_name, ifp->if_unit, cp->name, len);
1005		return;
1006	}
1007	h = mtod (m, struct lcp_header*);
1008	if (debug) {
1009		log(LOG_DEBUG,
1010		    "%s%d: %s input(%s): <%s id=0x%x len=%d",
1011		    ifp->if_name, ifp->if_unit, cp->name,
1012		    sppp_state_name(sp->state[cp->protoidx]),
1013		    sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1014		if (len > 4)
1015			sppp_print_bytes ((u_char*) (h+1), len-4);
1016		addlog(">\n");
1017	}
1018	if (len > ntohs (h->len))
1019		len = ntohs (h->len);
1020	switch (h->type) {
1021	case CONF_REQ:
1022		if (len < 4) {
1023			if (debug)
1024				addlog("%s%d: %s invalid conf-req length %d\n",
1025				       ifp->if_name, ifp->if_unit, cp->name,
1026				       len);
1027			++ifp->if_ierrors;
1028			break;
1029		}
1030		rv = (cp->RCR)(sp, h, len);
1031		switch (sp->state[cp->protoidx]) {
1032		case STATE_OPENED:
1033			(cp->tld)(sp);
1034			(cp->scr)(sp);
1035			/* fall through... */
1036		case STATE_ACK_SENT:
1037		case STATE_REQ_SENT:
1038			sppp_cp_change_state(cp, sp, rv?
1039					     STATE_ACK_SENT: STATE_REQ_SENT);
1040			break;
1041		case STATE_CLOSING:
1042		case STATE_STOPPING:
1043			break;
1044		case STATE_STOPPED:
1045			sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1046			(cp->scr)(sp);
1047			sppp_cp_change_state(cp, sp, rv?
1048					     STATE_ACK_SENT: STATE_REQ_SENT);
1049			break;
1050		case STATE_CLOSED:
1051			sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1052				     0, 0);
1053			break;
1054		case STATE_ACK_RCVD:
1055			if (rv) {
1056				sppp_cp_change_state(cp, sp, STATE_OPENED);
1057				if (debug)
1058					addlog("%s%d: %s tlu\n",
1059					       ifp->if_name, ifp->if_unit,
1060					       cp->name);
1061				(cp->tlu)(sp);
1062			} else
1063				sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1064			break;
1065		default:
1066			printf("%s%d: %s illegal %s in state %s\n",
1067			       ifp->if_name, ifp->if_unit, cp->name,
1068			       sppp_cp_type_name(h->type),
1069			       sppp_state_name(sp->state[cp->protoidx]));
1070			++ifp->if_ierrors;
1071		}
1072		break;
1073	case CONF_ACK:
1074		if (h->ident != sp->confid[cp->protoidx]) {
1075			if (debug)
1076				addlog("%s%d: %s id mismatch 0x%x != 0x%x\n",
1077				       ifp->if_name, ifp->if_unit, cp->name,
1078				       h->ident, sp->confid[cp->protoidx]);
1079			++ifp->if_ierrors;
1080			break;
1081		}
1082		switch (sp->state[cp->protoidx]) {
1083		case STATE_CLOSED:
1084		case STATE_STOPPED:
1085			sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1086			break;
1087		case STATE_CLOSING:
1088		case STATE_STOPPING:
1089			break;
1090		case STATE_REQ_SENT:
1091			sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1092			sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1093			break;
1094		case STATE_OPENED:
1095			(cp->tld)(sp);
1096			/* fall through */
1097		case STATE_ACK_RCVD:
1098			(cp->scr)(sp);
1099			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1100			break;
1101		case STATE_ACK_SENT:
1102			sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1103			sppp_cp_change_state(cp, sp, STATE_OPENED);
1104			if (debug)
1105				addlog("%s%d: %s tlu\n",
1106				       ifp->if_name, ifp->if_unit, cp->name);
1107			(cp->tlu)(sp);
1108			break;
1109		default:
1110			printf("%s%d: %s illegal %s in state %s\n",
1111			       ifp->if_name, ifp->if_unit, cp->name,
1112			       sppp_cp_type_name(h->type),
1113			       sppp_state_name(sp->state[cp->protoidx]));
1114			++ifp->if_ierrors;
1115		}
1116		break;
1117	case CONF_NAK:
1118	case CONF_REJ:
1119		if (h->ident != sp->confid[cp->protoidx]) {
1120			if (debug)
1121				addlog("%s%d: %s id mismatch 0x%x != 0x%x\n",
1122				       ifp->if_name, ifp->if_unit, cp->name,
1123				       h->ident, sp->confid[cp->protoidx]);
1124			++ifp->if_ierrors;
1125			break;
1126		}
1127		if (h->type == CONF_NAK)
1128			(cp->RCN_nak)(sp, h, len);
1129		else /* CONF_REJ */
1130			(cp->RCN_rej)(sp, h, len);
1131
1132		switch (sp->state[cp->protoidx]) {
1133		case STATE_CLOSED:
1134		case STATE_STOPPED:
1135			sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1136			break;
1137		case STATE_REQ_SENT:
1138		case STATE_ACK_SENT:
1139			sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1140			(cp->scr)(sp);
1141			break;
1142		case STATE_OPENED:
1143			(cp->tld)(sp);
1144			/* fall through */
1145		case STATE_ACK_RCVD:
1146			sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
1147			(cp->scr)(sp);
1148			break;
1149		case STATE_CLOSING:
1150		case STATE_STOPPING:
1151			break;
1152		default:
1153			printf("%s%d: %s illegal %s in state %s\n",
1154			       ifp->if_name, ifp->if_unit, cp->name,
1155			       sppp_cp_type_name(h->type),
1156			       sppp_state_name(sp->state[cp->protoidx]));
1157			++ifp->if_ierrors;
1158		}
1159		break;
1160
1161	case TERM_REQ:
1162		switch (sp->state[cp->protoidx]) {
1163		case STATE_ACK_RCVD:
1164		case STATE_ACK_SENT:
1165			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1166			/* fall through */
1167		case STATE_CLOSED:
1168		case STATE_STOPPED:
1169		case STATE_CLOSING:
1170		case STATE_STOPPING:
1171		case STATE_REQ_SENT:
1172		  sta:
1173			/* Send Terminate-Ack packet. */
1174			if (debug)
1175				log(LOG_DEBUG, "%s%d: %s send terminate-ack\n",
1176				    ifp->if_name, ifp->if_unit, cp->name);
1177			sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1178			break;
1179		case STATE_OPENED:
1180			(cp->tld)(sp);
1181			sp->rst_counter[cp->protoidx] = 0;
1182			sppp_cp_change_state(cp, sp, STATE_STOPPING);
1183			goto sta;
1184			break;
1185		default:
1186			printf("%s%d: %s illegal %s in state %s\n",
1187			       ifp->if_name, ifp->if_unit, cp->name,
1188			       sppp_cp_type_name(h->type),
1189			       sppp_state_name(sp->state[cp->protoidx]));
1190			++ifp->if_ierrors;
1191		}
1192		break;
1193	case TERM_ACK:
1194		switch (sp->state[cp->protoidx]) {
1195		case STATE_CLOSED:
1196		case STATE_STOPPED:
1197		case STATE_REQ_SENT:
1198		case STATE_ACK_SENT:
1199			break;
1200		case STATE_CLOSING:
1201			(cp->tlf)(sp);
1202			sppp_cp_change_state(cp, sp, STATE_CLOSED);
1203			break;
1204		case STATE_STOPPING:
1205			(cp->tlf)(sp);
1206			sppp_cp_change_state(cp, sp, STATE_STOPPED);
1207			break;
1208		case STATE_ACK_RCVD:
1209			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1210			break;
1211		case STATE_OPENED:
1212			(cp->tld)(sp);
1213			(cp->scr)(sp);
1214			sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1215			break;
1216		default:
1217			printf("%s%d: %s illegal %s in state %s\n",
1218			       ifp->if_name, ifp->if_unit, cp->name,
1219			       sppp_cp_type_name(h->type),
1220			       sppp_state_name(sp->state[cp->protoidx]));
1221			++ifp->if_ierrors;
1222		}
1223		break;
1224	case CODE_REJ:
1225	case PROTO_REJ:
1226		/* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1227		switch (sp->state[cp->protoidx]) {
1228		case STATE_CLOSED:
1229		case STATE_STOPPED:
1230		case STATE_REQ_SENT:
1231		case STATE_ACK_SENT:
1232		case STATE_CLOSING:
1233		case STATE_STOPPING:
1234		case STATE_OPENED:
1235			break;
1236		case STATE_ACK_RCVD:
1237			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1238			break;
1239		default:
1240			printf("%s%d: %s illegal %s in state %s\n",
1241			       ifp->if_name, ifp->if_unit, cp->name,
1242			       sppp_cp_type_name(h->type),
1243			       sppp_state_name(sp->state[cp->protoidx]));
1244			++ifp->if_ierrors;
1245		}
1246		break;
1247	case DISC_REQ:
1248		if (cp->proto != PPP_LCP)
1249			goto illegal;
1250		/* Discard the packet. */
1251		break;
1252	case ECHO_REQ:
1253		if (cp->proto != PPP_LCP)
1254			goto illegal;
1255		if (sp->state[cp->protoidx] != STATE_OPENED) {
1256			if (debug)
1257				addlog("%s%d: lcp echo req but lcp closed\n",
1258				       ifp->if_name, ifp->if_unit);
1259			++ifp->if_ierrors;
1260			break;
1261		}
1262		if (len < 8) {
1263			if (debug)
1264				addlog("%s%d: invalid lcp echo request "
1265				       "packet length: %d bytes\n",
1266				       ifp->if_name, ifp->if_unit, len);
1267			break;
1268		}
1269		if (ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1270			/* Line loopback mode detected. */
1271			printf("%s%d: loopback\n", ifp->if_name, ifp->if_unit);
1272			if_down (ifp);
1273			sppp_qflush (&sp->pp_fastq);
1274
1275			/* Shut down the PPP link. */
1276			/* XXX */
1277			lcp.Down(sp);
1278			lcp.Up(sp);
1279			break;
1280		}
1281		*(long*)(h+1) = htonl (sp->lcp.magic);
1282		if (debug)
1283			addlog("%s%d: got lcp echo req, sending echo rep\n",
1284			       ifp->if_name, ifp->if_unit);
1285		sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1286		break;
1287	case ECHO_REPLY:
1288		if (cp->proto != PPP_LCP)
1289			goto illegal;
1290		if (h->ident != sp->lcp.echoid) {
1291			++ifp->if_ierrors;
1292			break;
1293		}
1294		if (len < 8) {
1295			if (debug)
1296				addlog("%s%d: lcp invalid echo reply "
1297				       "packet length: %d bytes\n",
1298				       ifp->if_name, ifp->if_unit, len);
1299			break;
1300		}
1301		if (debug)
1302			addlog("%s%d: lcp got echo rep\n",
1303			       ifp->if_name, ifp->if_unit);
1304		if (ntohl (*(long*)(h+1)) != sp->lcp.magic)
1305			sp->pp_alivecnt = 0;
1306		break;
1307	default:
1308		/* Unknown packet type -- send Code-Reject packet. */
1309	  illegal:
1310		if (debug)
1311			addlog("%s%d: %c send code-rej for 0x%x\n",
1312			       ifp->if_name, ifp->if_unit, cp->name, h->type);
1313		sppp_cp_send(sp, cp->proto, CODE_REJ, ++sp->pp_seq,
1314			     m->m_pkthdr.len, h);
1315		++ifp->if_ierrors;
1316	}
1317}
1318
1319
1320/*
1321 * The generic part of all Up/Down/Open/Close/TO event handlers.
1322 * Basically, the state transition handling in the automaton.
1323 */
1324static void
1325sppp_up_event(const struct cp *cp, struct sppp *sp)
1326{
1327	STDDCL;
1328
1329	if (debug)
1330		log(LOG_DEBUG, "%s%d: %s up(%s)\n",
1331		    ifp->if_name, ifp->if_unit, cp->name,
1332		    sppp_state_name(sp->state[cp->protoidx]));
1333
1334	switch (sp->state[cp->protoidx]) {
1335	case STATE_INITIAL:
1336		sppp_cp_change_state(cp, sp, STATE_CLOSED);
1337		break;
1338	case STATE_STARTING:
1339		sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1340		(cp->scr)(sp);
1341		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1342		break;
1343	default:
1344		printf("%s%d: %s illegal up in state %s\n",
1345		       ifp->if_name, ifp->if_unit, cp->name,
1346		       sppp_state_name(sp->state[cp->protoidx]));
1347	}
1348}
1349
1350static void
1351sppp_down_event(const struct cp *cp, struct sppp *sp)
1352{
1353	STDDCL;
1354
1355	if (debug)
1356		log(LOG_DEBUG, "%s%d: %s down(%s)\n",
1357		    ifp->if_name, ifp->if_unit, cp->name,
1358		    sppp_state_name(sp->state[cp->protoidx]));
1359
1360	switch (sp->state[cp->protoidx]) {
1361	case STATE_CLOSED:
1362	case STATE_CLOSING:
1363		sppp_cp_change_state(cp, sp, STATE_INITIAL);
1364		break;
1365	case STATE_STOPPED:
1366		(cp->tls)(sp);
1367		/* fall through */
1368	case STATE_STOPPING:
1369	case STATE_REQ_SENT:
1370	case STATE_ACK_RCVD:
1371	case STATE_ACK_SENT:
1372		sppp_cp_change_state(cp, sp, STATE_STARTING);
1373		break;
1374	case STATE_OPENED:
1375		(cp->tld)(sp);
1376		sppp_cp_change_state(cp, sp, STATE_STARTING);
1377		break;
1378	default:
1379		printf("%s%d: %s illegal down in state %s\n",
1380		       ifp->if_name, ifp->if_unit, cp->name,
1381		       sppp_state_name(sp->state[cp->protoidx]));
1382	}
1383}
1384
1385
1386static void
1387sppp_open_event(const struct cp *cp, struct sppp *sp)
1388{
1389	STDDCL;
1390
1391	if (debug)
1392		log(LOG_DEBUG, "%s%d: %s open(%s)\n",
1393		    ifp->if_name, ifp->if_unit, cp->name,
1394		    sppp_state_name(sp->state[cp->protoidx]));
1395
1396	switch (sp->state[cp->protoidx]) {
1397	case STATE_INITIAL:
1398		(cp->tls)(sp);
1399		sppp_cp_change_state(cp, sp, STATE_STARTING);
1400		break;
1401	case STATE_STARTING:
1402		break;
1403	case STATE_CLOSED:
1404		sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1405		(cp->scr)(sp);
1406		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1407		break;
1408	case STATE_STOPPED:
1409	case STATE_STOPPING:
1410	case STATE_REQ_SENT:
1411	case STATE_ACK_RCVD:
1412	case STATE_ACK_SENT:
1413	case STATE_OPENED:
1414		break;
1415	case STATE_CLOSING:
1416		sppp_cp_change_state(cp, sp, STATE_STOPPING);
1417		break;
1418	}
1419}
1420
1421
1422static void
1423sppp_close_event(const struct cp *cp, struct sppp *sp)
1424{
1425	STDDCL;
1426
1427	if (debug)
1428		log(LOG_DEBUG, "%s%d: %s close(%s)\n",
1429		    ifp->if_name, ifp->if_unit, cp->name,
1430		    sppp_state_name(sp->state[cp->protoidx]));
1431
1432	switch (sp->state[cp->protoidx]) {
1433	case STATE_INITIAL:
1434	case STATE_CLOSED:
1435	case STATE_CLOSING:
1436		break;
1437	case STATE_STARTING:
1438		(cp->tlf)(sp);
1439		sppp_cp_change_state(cp, sp, STATE_INITIAL);
1440		break;
1441	case STATE_STOPPED:
1442		sppp_cp_change_state(cp, sp, STATE_CLOSED);
1443		break;
1444	case STATE_STOPPING:
1445		sppp_cp_change_state(cp, sp, STATE_CLOSING);
1446		break;
1447	case STATE_OPENED:
1448		(cp->tld)(sp);
1449		/* fall through */
1450	case STATE_REQ_SENT:
1451	case STATE_ACK_RCVD:
1452	case STATE_ACK_SENT:
1453		sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
1454		sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq, 0, 0);
1455		sppp_cp_change_state(cp, sp, STATE_CLOSING);
1456		break;
1457	}
1458}
1459
1460static void
1461sppp_to_event(const struct cp *cp, struct sppp *sp)
1462{
1463	STDDCL;
1464	int s;
1465
1466	s = splimp();
1467	if (debug)
1468		log(LOG_DEBUG, "%s%d: %s TO(%s) rst_counter = %d\n",
1469		    ifp->if_name, ifp->if_unit, cp->name,
1470		    sppp_state_name(sp->state[cp->protoidx]),
1471		    sp->rst_counter[cp->protoidx]);
1472
1473	if (--sp->rst_counter[cp->protoidx] < 0)
1474		/* TO- event */
1475		switch (sp->state[cp->protoidx]) {
1476		case STATE_CLOSING:
1477			(cp->tlf)(sp);
1478			sppp_cp_change_state(cp, sp, STATE_CLOSED);
1479			break;
1480		case STATE_STOPPING:
1481			(cp->tlf)(sp);
1482			sppp_cp_change_state(cp, sp, STATE_STOPPED);
1483			break;
1484		case STATE_REQ_SENT:
1485		case STATE_ACK_RCVD:
1486		case STATE_ACK_SENT:
1487			(cp->tlf)(sp);
1488			sppp_cp_change_state(cp, sp, STATE_STOPPED);
1489			break;
1490		}
1491	else
1492		/* TO+ event */
1493		switch (sp->state[cp->protoidx]) {
1494		case STATE_CLOSING:
1495		case STATE_STOPPING:
1496			sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq,
1497				     0, 0);
1498			timeout(cp->TO, (void *)sp, sp->lcp.timeout);
1499			break;
1500		case STATE_REQ_SENT:
1501		case STATE_ACK_RCVD:
1502			(cp->scr)(sp);
1503			/* sppp_cp_change_state() will restart the timer */
1504			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1505			break;
1506		case STATE_ACK_SENT:
1507			(cp->scr)(sp);
1508			timeout(cp->TO, (void *)sp, sp->lcp.timeout);
1509			break;
1510		}
1511
1512	splx(s);
1513}
1514
1515/*
1516 * Change the state of a control protocol in the state automaton.
1517 * Takes care of starting/stopping the restart timer.
1518 */
1519void
1520sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
1521{
1522	sp->state[cp->protoidx] = newstate;
1523
1524	untimeout(cp->TO, (void *)sp);
1525	switch (newstate) {
1526	case STATE_INITIAL:
1527	case STATE_STARTING:
1528	case STATE_CLOSED:
1529	case STATE_STOPPED:
1530	case STATE_OPENED:
1531		break;
1532	case STATE_CLOSING:
1533	case STATE_STOPPING:
1534	case STATE_REQ_SENT:
1535	case STATE_ACK_RCVD:
1536	case STATE_ACK_SENT:
1537		timeout(cp->TO, (void *)sp, sp->lcp.timeout);
1538		break;
1539	}
1540}
1541/*
1542 *--------------------------------------------------------------------------*
1543 *                                                                          *
1544 *                         The LCP implementation.                          *
1545 *                                                                          *
1546 *--------------------------------------------------------------------------*
1547 */
1548static void
1549sppp_lcp_init(struct sppp *sp)
1550{
1551	sp->lcp.opts = (1 << LCP_OPT_MAGIC);
1552	sp->lcp.magic = 0;
1553	sp->state[IDX_LCP] = STATE_INITIAL;
1554	sp->fail_counter[IDX_LCP] = 0;
1555	sp->lcp.protos = 0;
1556	sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
1557
1558	/*
1559	 * Initialize counters and timeout values.  Note that we don't
1560	 * use the 3 seconds suggested in RFC 1661 since we are likely
1561	 * running on a fast link.  XXX We should probably implement
1562	 * the exponential backoff option.  Note that these values are
1563	 * relevant for all control protocols, not just LCP only.
1564	 */
1565	sp->lcp.timeout = 1 * hz;
1566	sp->lcp.max_terminate = 2;
1567	sp->lcp.max_configure = 10;
1568	sp->lcp.max_failure = 10;
1569}
1570
1571static void
1572sppp_lcp_up(struct sppp *sp)
1573{
1574	STDDCL;
1575
1576	/*
1577	 * If this interface is passive or dial-on-demand, it means
1578	 * we've got in incoming call.  Activate the interface.
1579	 */
1580	if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
1581		if (debug)
1582			log(LOG_DEBUG,
1583			    "%s%d: Up event (incoming call)\n",
1584			    ifp->if_name, ifp->if_unit);
1585		ifp->if_flags |= IFF_RUNNING;
1586		lcp.Open(sp);
1587	}
1588
1589	sppp_up_event(&lcp, sp);
1590}
1591
1592static void
1593sppp_lcp_down(struct sppp *sp)
1594{
1595	STDDCL;
1596
1597	sppp_down_event(&lcp, sp);
1598
1599	/*
1600	 * If this is neither a dial-on-demand nor a passive
1601	 * interface, simulate an ``ifconfig down'' action, so the
1602	 * administrator can force a redial by another ``ifconfig
1603	 * up''.  XXX For leased line operation, should we immediately
1604	 * try to reopen the connection here?
1605	 */
1606	if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
1607		log(LOG_INFO,
1608		    "%s%d: Down event (carrier loss), taking interface down.\n",
1609		    ifp->if_name, ifp->if_unit);
1610		if_down(ifp);
1611	} else {
1612		if (debug)
1613			log(LOG_DEBUG,
1614			    "%s%d: Down event (carrier loss)\n",
1615			    ifp->if_name, ifp->if_unit);
1616	}
1617	lcp.Close(sp);
1618	ifp->if_flags &= ~IFF_RUNNING;
1619}
1620
1621static void
1622sppp_lcp_open(struct sppp *sp)
1623{
1624	sppp_open_event(&lcp, sp);
1625}
1626
1627static void
1628sppp_lcp_close(struct sppp *sp)
1629{
1630	sppp_close_event(&lcp, sp);
1631}
1632
1633static void
1634sppp_lcp_TO(void *cookie)
1635{
1636	sppp_to_event(&lcp, (struct sppp *)cookie);
1637}
1638
1639/*
1640 * Analyze a configure request.  Return true if it was agreeable, and
1641 * caused action sca, false if it has been rejected or nak'ed, and
1642 * caused action scn.  (The return value is used to make the state
1643 * transition decision in the state automaton.)
1644 */
1645static int
1646sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
1647{
1648	STDDCL;
1649	u_char *buf, *r, *p;
1650	int origlen, rlen;
1651	u_long nmagic;
1652
1653	len -= 4;
1654	origlen = len;
1655	buf = r = malloc (len, M_TEMP, M_NOWAIT);
1656	if (! buf)
1657		return (0);
1658
1659	if (debug)
1660		log(LOG_DEBUG, "%s%d: lcp parse opts: ",
1661		    ifp->if_name, ifp->if_unit);
1662
1663	/* pass 1: check for things that need to be rejected */
1664	p = (void*) (h+1);
1665	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1666		if (debug)
1667			addlog(" %s ", sppp_lcp_opt_name(*p));
1668		switch (*p) {
1669		case LCP_OPT_MAGIC:
1670			/* Magic number. */
1671			/* fall through, both are same length */
1672		case LCP_OPT_ASYNC_MAP:
1673			/* Async control character map. */
1674			if (len >= 6 || p[1] == 6)
1675				continue;
1676			if (debug)
1677				addlog("[invalid] ");
1678			break;
1679		case LCP_OPT_MRU:
1680			/* Maximum receive unit. */
1681			if (len >= 4 && p[1] == 4)
1682				continue;
1683			if (debug)
1684				addlog("[invalid] ");
1685			break;
1686		default:
1687			/* Others not supported. */
1688			if (debug)
1689				addlog("[rej] ");
1690			break;
1691		}
1692		/* Add the option to rejected list. */
1693		bcopy (p, r, p[1]);
1694		r += p[1];
1695		rlen += p[1];
1696	}
1697	if (rlen) {
1698		if (debug)
1699			addlog(" send conf-rej\n");
1700		sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
1701		return 0;
1702	} else if (debug)
1703		addlog("\n");
1704
1705	/*
1706	 * pass 2: check for option values that are unacceptable and
1707	 * thus require to be nak'ed.
1708	 */
1709	if (debug)
1710		addlog("%s%d: lcp parse opt values: ",
1711		       ifp->if_name, ifp->if_unit);
1712
1713	p = (void*) (h+1);
1714	len = origlen;
1715	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1716		if (debug)
1717			addlog(" %s ", sppp_lcp_opt_name(*p));
1718		switch (*p) {
1719		case LCP_OPT_MAGIC:
1720			/* Magic number -- extract. */
1721			nmagic = (u_long)p[2] << 24 |
1722				(u_long)p[3] << 16 | p[4] << 8 | p[5];
1723			if (nmagic != sp->lcp.magic) {
1724				if (debug)
1725					addlog("0x%x ", nmagic);
1726				continue;
1727			}
1728			/*
1729			 * Local and remote magics equal -- loopback?
1730			 */
1731			if (sp->pp_loopcnt >= MAXALIVECNT*5) {
1732				printf ("\n%s%d: loopback\n",
1733					ifp->if_name, ifp->if_unit);
1734				sp->pp_loopcnt = 0;
1735				if (ifp->if_flags & IFF_UP) {
1736					if_down(ifp);
1737					sppp_qflush(&sp->pp_fastq);
1738					/* XXX ? */
1739					lcp.Down(sp);
1740					lcp.Up(sp);
1741				}
1742			} else if (debug)
1743				addlog("[glitch] ");
1744			++sp->pp_loopcnt;
1745			/*
1746			 * We negate our magic here, and NAK it.  If
1747			 * we see it later in an NAK packet, we
1748			 * suggest a new one.
1749			 */
1750			nmagic = ~sp->lcp.magic;
1751			/* Gonna NAK it. */
1752			p[2] = nmagic >> 24;
1753			p[3] = nmagic >> 16;
1754			p[4] = nmagic >> 8;
1755			p[5] = nmagic;
1756			break;
1757
1758		case LCP_OPT_ASYNC_MAP:
1759			/* Async control character map -- check to be zero. */
1760			if (! p[2] && ! p[3] && ! p[4] && ! p[5]) {
1761				if (debug)
1762					addlog("[empty] ");
1763				continue;
1764			}
1765			if (debug)
1766				addlog("[non-empty] ");
1767			/* suggest a zero one */
1768			p[2] = p[3] = p[4] = p[5] = 0;
1769			break;
1770
1771		case LCP_OPT_MRU:
1772			/*
1773			 * Maximum receive unit.  Always agreeable,
1774			 * but ignored by now.
1775			 */
1776			sp->lcp.their_mru = p[2] * 256 + p[3];
1777			if (debug)
1778				addlog("%d ", sp->lcp.their_mru);
1779			continue;
1780		}
1781		/* Add the option to nak'ed list. */
1782		bcopy (p, r, p[1]);
1783		r += p[1];
1784		rlen += p[1];
1785	}
1786	if (rlen) {
1787		if (debug)
1788			addlog(" send conf-nak\n");
1789		sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
1790		return 0;
1791	} else {
1792		if (debug)
1793			addlog(" send conf-ack\n");
1794		sp->pp_loopcnt = 0;
1795		sppp_cp_send (sp, PPP_LCP, CONF_ACK,
1796			      h->ident, origlen, h+1);
1797	}
1798
1799	free (buf, M_TEMP);
1800	return (rlen == 0);
1801}
1802
1803/*
1804 * Analyze the LCP Configure-Reject option list, and adjust our
1805 * negotiation.
1806 */
1807static void
1808sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
1809{
1810	STDDCL;
1811	u_char *buf, *p;
1812
1813	len -= 4;
1814	buf = malloc (len, M_TEMP, M_NOWAIT);
1815	if (!buf)
1816		return;
1817
1818	if (debug)
1819		log(LOG_DEBUG, "%s%d: lcp rej opts: ",
1820		    ifp->if_name, ifp->if_unit);
1821
1822	p = (void*) (h+1);
1823	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
1824		if (debug)
1825			addlog(" %s ", sppp_lcp_opt_name(*p));
1826		switch (*p) {
1827		case LCP_OPT_MAGIC:
1828			/* Magic number -- can't use it, use 0 */
1829			sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
1830			sp->lcp.magic = 0;
1831			break;
1832		case LCP_OPT_MRU:
1833			/*
1834			 * Should not be rejected anyway, since we only
1835			 * negotiate a MRU if explicitly requested by
1836			 * peer.
1837			 */
1838			sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
1839			break;
1840		}
1841	}
1842	if (debug)
1843		addlog("\n");
1844	free (buf, M_TEMP);
1845	return;
1846}
1847
1848/*
1849 * Analyze the LCP Configure-NAK option list, and adjust our
1850 * negotiation.
1851 */
1852static void
1853sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
1854{
1855	STDDCL;
1856	u_char *buf, *p;
1857	u_long magic;
1858
1859	len -= 4;
1860	buf = malloc (len, M_TEMP, M_NOWAIT);
1861	if (!buf)
1862		return;
1863
1864	if (debug)
1865		log(LOG_DEBUG, "%s%d: lcp nak opts: ",
1866		    ifp->if_name, ifp->if_unit);
1867
1868	p = (void*) (h+1);
1869	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
1870		if (debug)
1871			addlog(" %s ", sppp_lcp_opt_name(*p));
1872		switch (*p) {
1873		case LCP_OPT_MAGIC:
1874			/* Magic number -- renegotiate */
1875			if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1876			    len >= 6 && p[1] == 6) {
1877				magic = (u_long)p[2] << 24 |
1878					(u_long)p[3] << 16 | p[4] << 8 | p[5];
1879				/*
1880				 * If the remote magic is our negated one,
1881				 * this looks like a loopback problem.
1882				 * Suggest a new magic to make sure.
1883				 */
1884				if (magic == ~sp->lcp.magic) {
1885					if (debug)
1886						addlog("magic glitch ");
1887					sp->lcp.magic += time.tv_sec + time.tv_usec;
1888				} else {
1889					sp->lcp.magic = magic;
1890					if (debug)
1891						addlog("%d ");
1892				}
1893			}
1894			break;
1895		case LCP_OPT_MRU:
1896			/*
1897			 * Peer wants to advise us to negotiate an MRU.
1898			 * Agree on it if it's reasonable, or use
1899			 * default otherwise.
1900			 */
1901			if (len >= 4 && p[1] == 4) {
1902				u_int mru = p[2] * 256 + p[3];
1903				if (debug)
1904					addlog("%d ", mru);
1905				if (mru < PP_MTU || mru > PP_MAX_MRU)
1906					mru = PP_MTU;
1907				sp->lcp.mru = mru;
1908				sp->lcp.opts |= (1 << LCP_OPT_MRU);
1909			}
1910			break;
1911		}
1912	}
1913	if (debug)
1914		addlog("\n");
1915	free (buf, M_TEMP);
1916	return;
1917}
1918
1919static void
1920sppp_lcp_tlu(struct sppp *sp)
1921{
1922	STDDCL;
1923	int i;
1924	u_long mask;
1925
1926	/* XXX ? */
1927	if (! (ifp->if_flags & IFF_UP) &&
1928	    (ifp->if_flags & IFF_RUNNING)) {
1929		/* Coming out of loopback mode. */
1930		if_up(ifp);
1931		printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
1932	}
1933
1934	for (i = 0; i < IDX_COUNT; i++)
1935		if ((cps[i])->flags & CP_QUAL)
1936			(cps[i])->Open(sp);
1937
1938	if (/* require authentication XXX */ 0)
1939		sp->pp_phase = PHASE_AUTHENTICATE;
1940	else
1941		sp->pp_phase = PHASE_NETWORK;
1942
1943	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
1944	    sppp_phase_name(sp->pp_phase));
1945
1946	if (sp->pp_phase == PHASE_AUTHENTICATE) {
1947		for (i = 0; i < IDX_COUNT; i++)
1948			if ((cps[i])->flags & CP_AUTH)
1949				(cps[i])->Open(sp);
1950	} else {
1951		/* Notify all NCPs. */
1952		for (i = 0; i < IDX_COUNT; i++)
1953			if ((cps[i])->flags & CP_NCP)
1954				(cps[i])->Open(sp);
1955	}
1956
1957	/* Send Up events to all started protos. */
1958	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
1959		if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0)
1960			(cps[i])->Up(sp);
1961
1962	if (sp->pp_phase == PHASE_NETWORK)
1963		/* if no NCP is starting, close down */
1964		sppp_lcp_check(sp);
1965}
1966
1967static void
1968sppp_lcp_tld(struct sppp *sp)
1969{
1970	STDDCL;
1971	int i;
1972	u_long mask;
1973
1974	sp->pp_phase = PHASE_TERMINATE;
1975
1976	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
1977	    sppp_phase_name(sp->pp_phase));
1978
1979	/*
1980	 * Take upper layers down.  We send the Down event first and
1981	 * the Close second to prevent the upper layers from sending
1982	 * ``a flurry of terminate-request packets'', as the RFC
1983	 * describes it.
1984	 */
1985	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
1986		if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0) {
1987			(cps[i])->Down(sp);
1988			(cps[i])->Close(sp);
1989		}
1990}
1991
1992static void
1993sppp_lcp_tls(struct sppp *sp)
1994{
1995	STDDCL;
1996
1997	sp->pp_phase = PHASE_ESTABLISH;
1998
1999	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2000	    sppp_phase_name(sp->pp_phase));
2001
2002	/* Notify lower layer if desired. */
2003	if (sp->pp_tls)
2004		(sp->pp_tls)(sp);
2005}
2006
2007static void
2008sppp_lcp_tlf(struct sppp *sp)
2009{
2010	STDDCL;
2011
2012	sp->pp_phase = PHASE_DEAD;
2013	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2014	    sppp_phase_name(sp->pp_phase));
2015
2016	/* Notify lower layer if desired. */
2017	if (sp->pp_tlf)
2018		(sp->pp_tlf)(sp);
2019}
2020
2021static void
2022sppp_lcp_scr(struct sppp *sp)
2023{
2024	char opt[6 /* magicnum */ + 4 /* mru */];
2025	int i = 0;
2026
2027	if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2028		if (! sp->lcp.magic)
2029			sp->lcp.magic = time.tv_sec + time.tv_usec;
2030		opt[i++] = LCP_OPT_MAGIC;
2031		opt[i++] = 6;
2032		opt[i++] = sp->lcp.magic >> 24;
2033		opt[i++] = sp->lcp.magic >> 16;
2034		opt[i++] = sp->lcp.magic >> 8;
2035		opt[i++] = sp->lcp.magic;
2036	}
2037
2038	if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2039		opt[i++] = LCP_OPT_MRU;
2040		opt[i++] = 4;
2041		opt[i++] = sp->lcp.mru >> 8;
2042		opt[i++] = sp->lcp.mru;
2043	}
2044
2045	sp->confid[IDX_LCP] = ++sp->pp_seq;
2046	sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2047}
2048
2049/*
2050 * Re-check the open NCPs and see if we should terminate the link.
2051 * Called by the NCPs during their tlf action handling.
2052 */
2053static void
2054sppp_lcp_check(struct sppp *sp)
2055{
2056	int i, mask;
2057
2058	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2059		if (sp->lcp.protos & mask && (cps[i])->flags & CP_NCP)
2060			return;
2061	lcp.Close(sp);
2062}
2063/*
2064 *--------------------------------------------------------------------------*
2065 *                                                                          *
2066 *                        The IPCP implementation.                          *
2067 *                                                                          *
2068 *--------------------------------------------------------------------------*
2069 */
2070
2071static void
2072sppp_ipcp_init(struct sppp *sp)
2073{
2074	sp->ipcp.opts = 0;
2075	sp->ipcp.flags = 0;
2076	sp->state[IDX_IPCP] = STATE_INITIAL;
2077	sp->fail_counter[IDX_IPCP] = 0;
2078}
2079
2080static void
2081sppp_ipcp_up(struct sppp *sp)
2082{
2083	sppp_up_event(&ipcp, sp);
2084}
2085
2086static void
2087sppp_ipcp_down(struct sppp *sp)
2088{
2089	sppp_down_event(&ipcp, sp);
2090}
2091
2092static void
2093sppp_ipcp_open(struct sppp *sp)
2094{
2095	STDDCL;
2096	u_long myaddr, hisaddr;
2097
2098	sppp_get_ip_addrs(sp, &myaddr, &hisaddr);
2099	/*
2100	 * If we don't have his address, this probably means our
2101	 * interface doesn't want to talk IP at all.  (This could
2102	 * be the case if somebody wants to speak only IPX, for
2103	 * example.)  Don't open IPCP in this case.
2104	 */
2105	if (hisaddr == 0L) {
2106		/* XXX this message should go away */
2107		if (debug)
2108			log(LOG_DEBUG, "%s%d: ipcp_open(): no IP interface\n",
2109			    ifp->if_name, ifp->if_unit);
2110		return;
2111	}
2112
2113	if (myaddr == 0L) {
2114		/*
2115		 * I don't have an assigned address, so i need to
2116		 * negotiate my address.
2117		 */
2118		sp->ipcp.flags |= IPCP_MYADDR_DYN;
2119		sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2120	}
2121	sppp_open_event(&ipcp, sp);
2122}
2123
2124static void
2125sppp_ipcp_close(struct sppp *sp)
2126{
2127	sppp_close_event(&ipcp, sp);
2128	if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2129		/*
2130		 * My address was dynamic, clear it again.
2131		 */
2132		sppp_set_ip_addr(sp, 0L);
2133}
2134
2135static void
2136sppp_ipcp_TO(void *cookie)
2137{
2138	sppp_to_event(&ipcp, (struct sppp *)cookie);
2139}
2140
2141/*
2142 * Analyze a configure request.  Return true if it was agreeable, and
2143 * caused action sca, false if it has been rejected or nak'ed, and
2144 * caused action scn.  (The return value is used to make the state
2145 * transition decision in the state automaton.)
2146 */
2147static int
2148sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2149{
2150	u_char *buf, *r, *p;
2151	struct ifnet *ifp = &sp->pp_if;
2152	int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2153	u_long hisaddr, desiredaddr;
2154
2155	len -= 4;
2156	origlen = len;
2157	/*
2158	 * Make sure to allocate a buf that can at least hold a
2159	 * conf-nak with an `address' option.  We might need it below.
2160	 */
2161	buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2162	if (! buf)
2163		return (0);
2164
2165	/* pass 1: see if we can recognize them */
2166	if (debug)
2167		log(LOG_DEBUG, "%s%d: ipcp parse opts: ",
2168		    ifp->if_name, ifp->if_unit);
2169	p = (void*) (h+1);
2170	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2171		if (debug)
2172			addlog(" %s ", sppp_ipcp_opt_name(*p));
2173		switch (*p) {
2174#ifdef notyet
2175		case IPCP_OPT_COMPRESSION:
2176			if (len >= 6 && p[1] >= 6) {
2177				/* correctly formed compress option */
2178				continue;
2179			}
2180			if (debug)
2181				addlog("[invalid] ");
2182			break;
2183#endif
2184		case IPCP_OPT_ADDRESS:
2185			if (len >= 6 && p[1] == 6) {
2186				/* correctly formed address option */
2187				continue;
2188			}
2189			if (debug)
2190				addlog("[invalid] ");
2191			break;
2192		default:
2193			/* Others not supported. */
2194			if (debug)
2195				addlog("[rej] ");
2196			break;
2197		}
2198		/* Add the option to rejected list. */
2199		bcopy (p, r, p[1]);
2200		r += p[1];
2201		rlen += p[1];
2202	}
2203	if (rlen) {
2204		if (debug)
2205			addlog(" send conf-rej\n");
2206		sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2207		return 0;
2208	} else if (debug)
2209		addlog("\n");
2210
2211	/* pass 2: parse option values */
2212	sppp_get_ip_addrs(sp, 0, &hisaddr);
2213	if (debug)
2214		addlog("%s%d: ipcp parse opt values: ", ifp->if_name, ifp->if_unit);
2215	p = (void*) (h+1);
2216	len = origlen;
2217	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2218		if (debug)
2219			addlog(" %s ", sppp_ipcp_opt_name(*p));
2220		switch (*p) {
2221#ifdef notyet
2222		case IPCP_OPT_COMPRESSION:
2223			continue;
2224#endif
2225		case IPCP_OPT_ADDRESS:
2226			desiredaddr = p[2] << 24 | p[3] << 16 |
2227				p[4] << 8 | p[5];
2228			if (desiredaddr == hisaddr) {
2229				/*
2230				 * Peer's address is same as our value,
2231				 * this is agreeable.  Gonna conf-ack
2232				 * it.
2233				 */
2234				if (debug)
2235					addlog("0x%x [ack] ", hisaddr);
2236				/* record that we've seen it already */
2237				sp->ipcp.flags |= IPCP_HISADDR_SEEN;
2238				continue;
2239			}
2240			/*
2241			 * The address wasn't agreeable.  This is either
2242			 * he sent us 0.0.0.0, asking to assign him an
2243			 * address, or he send us another address not
2244			 * matching our value.  Either case, we gonna
2245			 * conf-nak it with our value.
2246			 */
2247			if (debug) {
2248				if (desiredaddr == 0)
2249					addlog("[addr requested] ");
2250				else
2251					addlog("0x%x [not agreed] ",
2252					       desiredaddr);
2253
2254				p[2] = hisaddr >> 24;
2255				p[3] = hisaddr >> 16;
2256				p[4] = hisaddr >> 8;
2257				p[5] = hisaddr;
2258			}
2259			break;
2260		}
2261		/* Add the option to nak'ed list. */
2262		bcopy (p, r, p[1]);
2263		r += p[1];
2264		rlen += p[1];
2265	}
2266
2267	/*
2268	 * If we are about to conf-ack the request, but haven't seen
2269	 * his address so far, gonna conf-nak it instead, with the
2270	 * `address' option present and our idea of his address being
2271	 * filled in there, to request negotiation of both addresses.
2272	 *
2273	 * XXX This can result in an endless req - nak loop if peer
2274	 * doesn't want to send us his address.  Q: What should we do
2275	 * about it?  XXX  A: implement the max-failure counter.
2276	 */
2277	if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN)) {
2278		buf[0] = IPCP_OPT_ADDRESS;
2279		buf[1] = 6;
2280		buf[2] = hisaddr >> 24;
2281		buf[3] = hisaddr >> 16;
2282		buf[4] = hisaddr >> 8;
2283		buf[5] = hisaddr;
2284		rlen = 6;
2285		if (debug)
2286			addlog("still need hisaddr ");
2287	}
2288
2289	if (rlen) {
2290		if (debug)
2291			addlog(" send conf-nak\n");
2292		sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
2293	} else {
2294		if (debug)
2295			addlog(" send conf-ack\n");
2296		sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
2297			      h->ident, origlen, h+1);
2298	}
2299
2300	free (buf, M_TEMP);
2301	return (rlen == 0);
2302}
2303
2304/*
2305 * Analyze the IPCP Configure-Reject option list, and adjust our
2306 * negotiation.
2307 */
2308static void
2309sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2310{
2311	u_char *buf, *p;
2312	struct ifnet *ifp = &sp->pp_if;
2313	int debug = ifp->if_flags & IFF_DEBUG;
2314
2315	len -= 4;
2316	buf = malloc (len, M_TEMP, M_NOWAIT);
2317	if (!buf)
2318		return;
2319
2320	if (debug)
2321		log(LOG_DEBUG, "%s%d: ipcp rej opts: ",
2322		    ifp->if_name, ifp->if_unit);
2323
2324	p = (void*) (h+1);
2325	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2326		if (debug)
2327			addlog(" %s ", sppp_ipcp_opt_name(*p));
2328		switch (*p) {
2329		case IPCP_OPT_ADDRESS:
2330			/*
2331			 * Peer doesn't grok address option.  This is
2332			 * bad.  XXX  Should we better give up here?
2333			 */
2334			sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
2335			break;
2336#ifdef notyet
2337		case IPCP_OPT_COMPRESS:
2338			sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
2339			break;
2340#endif
2341		}
2342	}
2343	if (debug)
2344		addlog("\n");
2345	free (buf, M_TEMP);
2346	return;
2347}
2348
2349/*
2350 * Analyze the IPCP Configure-NAK option list, and adjust our
2351 * negotiation.
2352 */
2353static void
2354sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2355{
2356	u_char *buf, *p;
2357	struct ifnet *ifp = &sp->pp_if;
2358	int debug = ifp->if_flags & IFF_DEBUG;
2359	u_long wantaddr;
2360
2361	len -= 4;
2362	buf = malloc (len, M_TEMP, M_NOWAIT);
2363	if (!buf)
2364		return;
2365
2366	if (debug)
2367		log(LOG_DEBUG, "%s%d: ipcp nak opts: ",
2368		    ifp->if_name, ifp->if_unit);
2369
2370	p = (void*) (h+1);
2371	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2372		if (debug)
2373			addlog(" %s ", sppp_ipcp_opt_name(*p));
2374		switch (*p) {
2375		case IPCP_OPT_ADDRESS:
2376			/*
2377			 * Peer doesn't like our local IP address.  See
2378			 * if we can do something for him.  We'll drop
2379			 * him our address then.
2380			 */
2381			if (len >= 6 && p[1] == 6) {
2382				wantaddr = p[2] << 24 | p[3] << 16 |
2383					p[4] << 8 | p[5];
2384				sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2385				if (debug)
2386					addlog("[wantaddr 0x%x] ", wantaddr);
2387				/*
2388				 * When doing dynamic address assignment,
2389				 * we accept his offer.  Otherwise, we
2390				 * ignore it and thus continue to negotiate
2391				 * our already existing value.
2392				 */
2393				if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
2394					sppp_set_ip_addr(sp, wantaddr);
2395					if (debug)
2396						addlog("[agree] ");
2397				}
2398			}
2399			break;
2400#ifdef notyet
2401		case IPCP_OPT_COMPRESS:
2402			/*
2403			 * Peer wants different compression parameters.
2404			 */
2405			break;
2406#endif
2407		}
2408	}
2409	if (debug)
2410		addlog("\n");
2411	free (buf, M_TEMP);
2412	return;
2413}
2414
2415static void
2416sppp_ipcp_tlu(struct sppp *sp)
2417{
2418}
2419
2420static void
2421sppp_ipcp_tld(struct sppp *sp)
2422{
2423}
2424
2425static void
2426sppp_ipcp_tls(struct sppp *sp)
2427{
2428	/* indicate to LCP that it must stay alive */
2429	sp->lcp.protos |= (1 << IDX_IPCP);
2430}
2431
2432static void
2433sppp_ipcp_tlf(struct sppp *sp)
2434{
2435	/* we no longer need LCP */
2436	sp->lcp.protos &= ~(1 << IDX_IPCP);
2437	sppp_lcp_check(sp);
2438}
2439
2440static void
2441sppp_ipcp_scr(struct sppp *sp)
2442{
2443	char opt[6 /* compression */ + 6 /* address */];
2444	u_long ouraddr;
2445	int i = 0;
2446
2447#ifdef notyet
2448	if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
2449		opt[i++] = IPCP_OPT_COMPRESSION;
2450		opt[i++] = 6;
2451		opt[i++] = 0;	/* VJ header compression */
2452		opt[i++] = 0x2d; /* VJ header compression */
2453		opt[i++] = max_slot_id;
2454		opt[i++] = comp_slot_id;
2455	}
2456#endif
2457
2458	if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
2459		sppp_get_ip_addrs(sp, &ouraddr, 0);
2460		opt[i++] = IPCP_OPT_ADDRESS;
2461		opt[i++] = 6;
2462		opt[i++] = ouraddr >> 24;
2463		opt[i++] = ouraddr >> 16;
2464		opt[i++] = ouraddr >> 8;
2465		opt[i++] = ouraddr;
2466	}
2467
2468	sp->confid[IDX_IPCP] = ++sp->pp_seq;
2469	sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
2470}
2471
2472
2473/*
2474 * Random miscellaneous functions.
2475 */
2476
2477/*
2478 * Flush interface queue.
2479 */
2480static void
2481sppp_qflush(struct ifqueue *ifq)
2482{
2483	struct mbuf *m, *n;
2484
2485	n = ifq->ifq_head;
2486	while ((m = n)) {
2487		n = m->m_act;
2488		m_freem (m);
2489	}
2490	ifq->ifq_head = 0;
2491	ifq->ifq_tail = 0;
2492	ifq->ifq_len = 0;
2493}
2494
2495/*
2496 * Send keepalive packets, every 10 seconds.
2497 */
2498static void
2499sppp_keepalive(void *dummy)
2500{
2501	struct sppp *sp;
2502	int s;
2503
2504	s = splimp();
2505	for (sp=spppq; sp; sp=sp->pp_next) {
2506		struct ifnet *ifp = &sp->pp_if;
2507
2508		/* Keepalive mode disabled or channel down? */
2509		if (! (sp->pp_flags & PP_KEEPALIVE) ||
2510		    ! (ifp->if_flags & IFF_RUNNING))
2511			continue;
2512
2513		/* No keepalive in PPP mode if LCP not opened yet. */
2514		if (! (sp->pp_flags & PP_CISCO) &&
2515		    sp->pp_phase < PHASE_AUTHENTICATE)
2516			continue;
2517
2518		if (sp->pp_alivecnt == MAXALIVECNT) {
2519			/* No keepalive packets got.  Stop the interface. */
2520			printf ("%s%d: down\n", ifp->if_name, ifp->if_unit);
2521			if_down (ifp);
2522			sppp_qflush (&sp->pp_fastq);
2523			if (! (sp->pp_flags & PP_CISCO)) {
2524				/* XXX */
2525				/* Shut down the PPP link. */
2526				lcp.Down(sp);
2527				/* Initiate negotiation. XXX */
2528				lcp.Up(sp);
2529			}
2530		}
2531		if (sp->pp_alivecnt <= MAXALIVECNT)
2532			++sp->pp_alivecnt;
2533		if (sp->pp_flags & PP_CISCO)
2534			sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
2535				sp->pp_rseq);
2536		else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
2537			long nmagic = htonl (sp->lcp.magic);
2538			sp->lcp.echoid = ++sp->pp_seq;
2539			sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
2540				sp->lcp.echoid, 4, &nmagic);
2541		}
2542	}
2543	splx(s);
2544	timeout(sppp_keepalive, 0, hz * 10);
2545}
2546
2547/*
2548 * Get both IP addresses.
2549 */
2550static void
2551sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst)
2552{
2553	struct ifnet *ifp = &sp->pp_if;
2554	struct ifaddr *ifa;
2555	struct sockaddr_in *si;
2556	u_long ssrc, ddst;
2557
2558	ssrc = ddst = 0L;
2559	/*
2560	 * Pick the first AF_INET address from the list,
2561	 * aliases don't make any sense on a p2p link anyway.
2562	 */
2563	for (ifa = ifp->if_addrhead.tqh_first, si = 0;
2564	     ifa;
2565	     ifa = ifa->ifa_link.tqe_next)
2566		if (ifa->ifa_addr->sa_family == AF_INET) {
2567			si = (struct sockaddr_in *)ifa->ifa_addr;
2568			if (si)
2569				break;
2570		}
2571	if (ifa) {
2572		if (si && si->sin_addr.s_addr)
2573			ssrc = si->sin_addr.s_addr;
2574
2575		si = (struct sockaddr_in *)ifa->ifa_dstaddr;
2576		if (si && si->sin_addr.s_addr)
2577			ddst = si->sin_addr.s_addr;
2578	}
2579
2580	if (dst) *dst = ntohl(ddst);
2581	if (src) *src = ntohl(ssrc);
2582}
2583
2584/*
2585 * Set my IP address.  Must be called at splimp.
2586 */
2587static void
2588sppp_set_ip_addr(struct sppp *sp, u_long src)
2589{
2590	struct ifnet *ifp = &sp->pp_if;
2591	struct ifaddr *ifa;
2592	struct sockaddr_in *si;
2593	u_long ssrc, ddst;
2594
2595	/*
2596	 * Pick the first AF_INET address from the list,
2597	 * aliases don't make any sense on a p2p link anyway.
2598	 */
2599	for (ifa = ifp->if_addrhead.tqh_first, si = 0;
2600	     ifa;
2601	     ifa = ifa->ifa_link.tqe_next)
2602		if (ifa->ifa_addr->sa_family == AF_INET) {
2603			si = (struct sockaddr_in *)ifa->ifa_addr;
2604			if (si)
2605				break;
2606		}
2607	if (ifa && si)
2608		si->sin_addr.s_addr = htonl(src);
2609}
2610
2611static const char *
2612sppp_cp_type_name(u_char type)
2613{
2614	static char buf [12];
2615	switch (type) {
2616	case CONF_REQ:   return ("conf-req");
2617	case CONF_ACK:   return ("conf-ack");
2618	case CONF_NAK:   return ("conf-nak");
2619	case CONF_REJ:   return ("conf-rej");
2620	case TERM_REQ:   return ("term-req");
2621	case TERM_ACK:   return ("term-ack");
2622	case CODE_REJ:   return ("code-rej");
2623	case PROTO_REJ:  return ("proto-rej");
2624	case ECHO_REQ:   return ("echo-req");
2625	case ECHO_REPLY: return ("echo-reply");
2626	case DISC_REQ:   return ("discard-req");
2627	}
2628	sprintf (buf, "0x%x", type);
2629	return (buf);
2630}
2631
2632static const char *
2633sppp_lcp_opt_name(u_char opt)
2634{
2635	static char buf [12];
2636	switch (opt) {
2637	case LCP_OPT_MRU:		return ("mru");
2638	case LCP_OPT_ASYNC_MAP:		return ("async-map");
2639	case LCP_OPT_AUTH_PROTO:	return ("auth-proto");
2640	case LCP_OPT_QUAL_PROTO:	return ("qual-proto");
2641	case LCP_OPT_MAGIC:		return ("magic");
2642	case LCP_OPT_PROTO_COMP:	return ("proto-comp");
2643	case LCP_OPT_ADDR_COMP:		return ("addr-comp");
2644	}
2645	sprintf (buf, "0x%x", opt);
2646	return (buf);
2647}
2648
2649static const char *
2650sppp_ipcp_opt_name(u_char opt)
2651{
2652	static char buf [12];
2653	switch (opt) {
2654	case IPCP_OPT_ADDRESSES:	return ("addresses");
2655	case IPCP_OPT_COMPRESSION:	return ("compression");
2656	case IPCP_OPT_ADDRESS:		return ("address");
2657	}
2658	sprintf (buf, "0x%x", opt);
2659	return (buf);
2660}
2661
2662static const char *
2663sppp_state_name(int state)
2664{
2665	switch (state) {
2666	case STATE_INITIAL:	return "initial";
2667	case STATE_STARTING:	return "starting";
2668	case STATE_CLOSED:	return "closed";
2669	case STATE_STOPPED:	return "stopped";
2670	case STATE_CLOSING:	return "closing";
2671	case STATE_STOPPING:	return "stopping";
2672	case STATE_REQ_SENT:	return "req-sent";
2673	case STATE_ACK_RCVD:	return "ack-rcvd";
2674	case STATE_ACK_SENT:	return "ack-sent";
2675	case STATE_OPENED:	return "opened";
2676	}
2677	return "illegal";
2678}
2679
2680static const char *
2681sppp_phase_name(enum ppp_phase phase)
2682{
2683	switch (phase) {
2684	case PHASE_DEAD:	return "dead";
2685	case PHASE_ESTABLISH:	return "establish";
2686	case PHASE_TERMINATE:	return "terminate";
2687	case PHASE_AUTHENTICATE: return "authenticate";
2688	case PHASE_NETWORK:	return "network";
2689	}
2690	return "illegal";
2691}
2692
2693static const char *
2694sppp_proto_name(u_short proto)
2695{
2696	static char buf[12];
2697	switch (proto) {
2698	case PPP_LCP:	return "lcp";
2699	case PPP_IPCP:	return "ipcp";
2700	}
2701	sprintf(buf, "0x%x", (unsigned)proto);
2702	return buf;
2703}
2704
2705static void
2706sppp_print_bytes(u_char *p, u_short len)
2707{
2708	addlog(" %x", *p++);
2709	while (--len > 0)
2710		addlog("-%x", *p++);
2711}
2712
2713/*
2714 * This file is large.  Tell emacs to highlight it nevertheless.
2715 *
2716 * Local Variables:
2717 * hilit-auto-highlight-maxout: 100000
2718 * End:
2719 */
2720