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