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