if_spppsubr.c revision 28088
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.24 1997/08/10 14:28:16 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_UNICAST;        /* unicast 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 (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
1814			if (debug)
1815				addlog(" max_failure (%d) exceeded, "
1816				       "send conf-rej\n",
1817				       sp->lcp.max_failure);
1818			sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
1819		} else {
1820			if (debug)
1821				addlog(" send conf-nak\n");
1822			sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
1823		}
1824		return 0;
1825	} else {
1826		if (debug)
1827			addlog(" send conf-ack\n");
1828		sp->fail_counter[IDX_LCP] = 0;
1829		sp->pp_loopcnt = 0;
1830		sppp_cp_send (sp, PPP_LCP, CONF_ACK,
1831			      h->ident, origlen, h+1);
1832	}
1833
1834	free (buf, M_TEMP);
1835	return (rlen == 0);
1836}
1837
1838/*
1839 * Analyze the LCP Configure-Reject option list, and adjust our
1840 * negotiation.
1841 */
1842static void
1843sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
1844{
1845	STDDCL;
1846	u_char *buf, *p;
1847
1848	len -= 4;
1849	buf = malloc (len, M_TEMP, M_NOWAIT);
1850	if (!buf)
1851		return;
1852
1853	if (debug)
1854		log(LOG_DEBUG, "%s%d: lcp rej opts: ",
1855		    ifp->if_name, ifp->if_unit);
1856
1857	p = (void*) (h+1);
1858	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
1859		if (debug)
1860			addlog(" %s ", sppp_lcp_opt_name(*p));
1861		switch (*p) {
1862		case LCP_OPT_MAGIC:
1863			/* Magic number -- can't use it, use 0 */
1864			sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
1865			sp->lcp.magic = 0;
1866			break;
1867		case LCP_OPT_MRU:
1868			/*
1869			 * Should not be rejected anyway, since we only
1870			 * negotiate a MRU if explicitly requested by
1871			 * peer.
1872			 */
1873			sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
1874			break;
1875		}
1876	}
1877	if (debug)
1878		addlog("\n");
1879	free (buf, M_TEMP);
1880	return;
1881}
1882
1883/*
1884 * Analyze the LCP Configure-NAK option list, and adjust our
1885 * negotiation.
1886 */
1887static void
1888sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
1889{
1890	STDDCL;
1891	u_char *buf, *p;
1892	u_long magic;
1893
1894	len -= 4;
1895	buf = malloc (len, M_TEMP, M_NOWAIT);
1896	if (!buf)
1897		return;
1898
1899	if (debug)
1900		log(LOG_DEBUG, "%s%d: lcp nak opts: ",
1901		    ifp->if_name, ifp->if_unit);
1902
1903	p = (void*) (h+1);
1904	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
1905		if (debug)
1906			addlog(" %s ", sppp_lcp_opt_name(*p));
1907		switch (*p) {
1908		case LCP_OPT_MAGIC:
1909			/* Magic number -- renegotiate */
1910			if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1911			    len >= 6 && p[1] == 6) {
1912				magic = (u_long)p[2] << 24 |
1913					(u_long)p[3] << 16 | p[4] << 8 | p[5];
1914				/*
1915				 * If the remote magic is our negated one,
1916				 * this looks like a loopback problem.
1917				 * Suggest a new magic to make sure.
1918				 */
1919				if (magic == ~sp->lcp.magic) {
1920					if (debug)
1921						addlog("magic glitch ");
1922					sp->lcp.magic += time.tv_sec + time.tv_usec;
1923				} else {
1924					sp->lcp.magic = magic;
1925					if (debug)
1926						addlog("%d ");
1927				}
1928			}
1929			break;
1930		case LCP_OPT_MRU:
1931			/*
1932			 * Peer wants to advise us to negotiate an MRU.
1933			 * Agree on it if it's reasonable, or use
1934			 * default otherwise.
1935			 */
1936			if (len >= 4 && p[1] == 4) {
1937				u_int mru = p[2] * 256 + p[3];
1938				if (debug)
1939					addlog("%d ", mru);
1940				if (mru < PP_MTU || mru > PP_MAX_MRU)
1941					mru = PP_MTU;
1942				sp->lcp.mru = mru;
1943				sp->lcp.opts |= (1 << LCP_OPT_MRU);
1944			}
1945			break;
1946		}
1947	}
1948	if (debug)
1949		addlog("\n");
1950	free (buf, M_TEMP);
1951	return;
1952}
1953
1954static void
1955sppp_lcp_tlu(struct sppp *sp)
1956{
1957	STDDCL;
1958	int i;
1959	u_long mask;
1960
1961	/* XXX ? */
1962	if (! (ifp->if_flags & IFF_UP) &&
1963	    (ifp->if_flags & IFF_RUNNING)) {
1964		/* Coming out of loopback mode. */
1965		if_up(ifp);
1966		printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
1967	}
1968
1969	for (i = 0; i < IDX_COUNT; i++)
1970		if ((cps[i])->flags & CP_QUAL)
1971			(cps[i])->Open(sp);
1972
1973	if (/* require authentication XXX */ 0)
1974		sp->pp_phase = PHASE_AUTHENTICATE;
1975	else
1976		sp->pp_phase = PHASE_NETWORK;
1977
1978	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
1979	    sppp_phase_name(sp->pp_phase));
1980
1981	if (sp->pp_phase == PHASE_AUTHENTICATE) {
1982		for (i = 0; i < IDX_COUNT; i++)
1983			if ((cps[i])->flags & CP_AUTH)
1984				(cps[i])->Open(sp);
1985	} else {
1986		/* Notify all NCPs. */
1987		for (i = 0; i < IDX_COUNT; i++)
1988			if ((cps[i])->flags & CP_NCP)
1989				(cps[i])->Open(sp);
1990	}
1991
1992	/* Send Up events to all started protos. */
1993	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
1994		if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0)
1995			(cps[i])->Up(sp);
1996
1997	if (sp->pp_phase == PHASE_NETWORK)
1998		/* if no NCP is starting, close down */
1999		sppp_lcp_check(sp);
2000}
2001
2002static void
2003sppp_lcp_tld(struct sppp *sp)
2004{
2005	STDDCL;
2006	int i;
2007	u_long mask;
2008
2009	sp->pp_phase = PHASE_TERMINATE;
2010
2011	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2012	    sppp_phase_name(sp->pp_phase));
2013
2014	/*
2015	 * Take upper layers down.  We send the Down event first and
2016	 * the Close second to prevent the upper layers from sending
2017	 * ``a flurry of terminate-request packets'', as the RFC
2018	 * describes it.
2019	 */
2020	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2021		if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0) {
2022			(cps[i])->Down(sp);
2023			(cps[i])->Close(sp);
2024		}
2025}
2026
2027static void
2028sppp_lcp_tls(struct sppp *sp)
2029{
2030	STDDCL;
2031
2032	sp->pp_phase = PHASE_ESTABLISH;
2033
2034	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2035	    sppp_phase_name(sp->pp_phase));
2036
2037	/* Notify lower layer if desired. */
2038	if (sp->pp_tls)
2039		(sp->pp_tls)(sp);
2040}
2041
2042static void
2043sppp_lcp_tlf(struct sppp *sp)
2044{
2045	STDDCL;
2046
2047	sp->pp_phase = PHASE_DEAD;
2048	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2049	    sppp_phase_name(sp->pp_phase));
2050
2051	/* Notify lower layer if desired. */
2052	if (sp->pp_tlf)
2053		(sp->pp_tlf)(sp);
2054}
2055
2056static void
2057sppp_lcp_scr(struct sppp *sp)
2058{
2059	char opt[6 /* magicnum */ + 4 /* mru */];
2060	int i = 0;
2061
2062	if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2063		if (! sp->lcp.magic)
2064			sp->lcp.magic = time.tv_sec + time.tv_usec;
2065		opt[i++] = LCP_OPT_MAGIC;
2066		opt[i++] = 6;
2067		opt[i++] = sp->lcp.magic >> 24;
2068		opt[i++] = sp->lcp.magic >> 16;
2069		opt[i++] = sp->lcp.magic >> 8;
2070		opt[i++] = sp->lcp.magic;
2071	}
2072
2073	if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2074		opt[i++] = LCP_OPT_MRU;
2075		opt[i++] = 4;
2076		opt[i++] = sp->lcp.mru >> 8;
2077		opt[i++] = sp->lcp.mru;
2078	}
2079
2080	sp->confid[IDX_LCP] = ++sp->pp_seq;
2081	sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2082}
2083
2084/*
2085 * Re-check the open NCPs and see if we should terminate the link.
2086 * Called by the NCPs during their tlf action handling.
2087 */
2088static void
2089sppp_lcp_check(struct sppp *sp)
2090{
2091	int i, mask;
2092
2093	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2094		if (sp->lcp.protos & mask && (cps[i])->flags & CP_NCP)
2095			return;
2096	lcp.Close(sp);
2097}
2098/*
2099 *--------------------------------------------------------------------------*
2100 *                                                                          *
2101 *                        The IPCP implementation.                          *
2102 *                                                                          *
2103 *--------------------------------------------------------------------------*
2104 */
2105
2106static void
2107sppp_ipcp_init(struct sppp *sp)
2108{
2109	sp->ipcp.opts = 0;
2110	sp->ipcp.flags = 0;
2111	sp->state[IDX_IPCP] = STATE_INITIAL;
2112	sp->fail_counter[IDX_IPCP] = 0;
2113}
2114
2115static void
2116sppp_ipcp_up(struct sppp *sp)
2117{
2118	sppp_up_event(&ipcp, sp);
2119}
2120
2121static void
2122sppp_ipcp_down(struct sppp *sp)
2123{
2124	sppp_down_event(&ipcp, sp);
2125}
2126
2127static void
2128sppp_ipcp_open(struct sppp *sp)
2129{
2130	STDDCL;
2131	u_long myaddr, hisaddr;
2132
2133	sppp_get_ip_addrs(sp, &myaddr, &hisaddr);
2134	/*
2135	 * If we don't have his address, this probably means our
2136	 * interface doesn't want to talk IP at all.  (This could
2137	 * be the case if somebody wants to speak only IPX, for
2138	 * example.)  Don't open IPCP in this case.
2139	 */
2140	if (hisaddr == 0L) {
2141		/* XXX this message should go away */
2142		if (debug)
2143			log(LOG_DEBUG, "%s%d: ipcp_open(): no IP interface\n",
2144			    ifp->if_name, ifp->if_unit);
2145		return;
2146	}
2147
2148	if (myaddr == 0L) {
2149		/*
2150		 * I don't have an assigned address, so i need to
2151		 * negotiate my address.
2152		 */
2153		sp->ipcp.flags |= IPCP_MYADDR_DYN;
2154		sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2155	}
2156	sppp_open_event(&ipcp, sp);
2157}
2158
2159static void
2160sppp_ipcp_close(struct sppp *sp)
2161{
2162	sppp_close_event(&ipcp, sp);
2163	if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2164		/*
2165		 * My address was dynamic, clear it again.
2166		 */
2167		sppp_set_ip_addr(sp, 0L);
2168}
2169
2170static void
2171sppp_ipcp_TO(void *cookie)
2172{
2173	sppp_to_event(&ipcp, (struct sppp *)cookie);
2174}
2175
2176/*
2177 * Analyze a configure request.  Return true if it was agreeable, and
2178 * caused action sca, false if it has been rejected or nak'ed, and
2179 * caused action scn.  (The return value is used to make the state
2180 * transition decision in the state automaton.)
2181 */
2182static int
2183sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2184{
2185	u_char *buf, *r, *p;
2186	struct ifnet *ifp = &sp->pp_if;
2187	int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2188	u_long hisaddr, desiredaddr;
2189
2190	len -= 4;
2191	origlen = len;
2192	/*
2193	 * Make sure to allocate a buf that can at least hold a
2194	 * conf-nak with an `address' option.  We might need it below.
2195	 */
2196	buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2197	if (! buf)
2198		return (0);
2199
2200	/* pass 1: see if we can recognize them */
2201	if (debug)
2202		log(LOG_DEBUG, "%s%d: ipcp parse opts: ",
2203		    ifp->if_name, ifp->if_unit);
2204	p = (void*) (h+1);
2205	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2206		if (debug)
2207			addlog(" %s ", sppp_ipcp_opt_name(*p));
2208		switch (*p) {
2209#ifdef notyet
2210		case IPCP_OPT_COMPRESSION:
2211			if (len >= 6 && p[1] >= 6) {
2212				/* correctly formed compress option */
2213				continue;
2214			}
2215			if (debug)
2216				addlog("[invalid] ");
2217			break;
2218#endif
2219		case IPCP_OPT_ADDRESS:
2220			if (len >= 6 && p[1] == 6) {
2221				/* correctly formed address option */
2222				continue;
2223			}
2224			if (debug)
2225				addlog("[invalid] ");
2226			break;
2227		default:
2228			/* Others not supported. */
2229			if (debug)
2230				addlog("[rej] ");
2231			break;
2232		}
2233		/* Add the option to rejected list. */
2234		bcopy (p, r, p[1]);
2235		r += p[1];
2236		rlen += p[1];
2237	}
2238	if (rlen) {
2239		if (debug)
2240			addlog(" send conf-rej\n");
2241		sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2242		return 0;
2243	} else if (debug)
2244		addlog("\n");
2245
2246	/* pass 2: parse option values */
2247	sppp_get_ip_addrs(sp, 0, &hisaddr);
2248	if (debug)
2249		addlog("%s%d: ipcp parse opt values: ", ifp->if_name, ifp->if_unit);
2250	p = (void*) (h+1);
2251	len = origlen;
2252	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2253		if (debug)
2254			addlog(" %s ", sppp_ipcp_opt_name(*p));
2255		switch (*p) {
2256#ifdef notyet
2257		case IPCP_OPT_COMPRESSION:
2258			continue;
2259#endif
2260		case IPCP_OPT_ADDRESS:
2261			desiredaddr = p[2] << 24 | p[3] << 16 |
2262				p[4] << 8 | p[5];
2263			if (desiredaddr == hisaddr) {
2264				/*
2265				 * Peer's address is same as our value,
2266				 * this is agreeable.  Gonna conf-ack
2267				 * it.
2268				 */
2269				if (debug)
2270					addlog("0x%x [ack] ", hisaddr);
2271				/* record that we've seen it already */
2272				sp->ipcp.flags |= IPCP_HISADDR_SEEN;
2273				continue;
2274			}
2275			/*
2276			 * The address wasn't agreeable.  This is either
2277			 * he sent us 0.0.0.0, asking to assign him an
2278			 * address, or he send us another address not
2279			 * matching our value.  Either case, we gonna
2280			 * conf-nak it with our value.
2281			 */
2282			if (debug) {
2283				if (desiredaddr == 0)
2284					addlog("[addr requested] ");
2285				else
2286					addlog("0x%x [not agreed] ",
2287					       desiredaddr);
2288
2289				p[2] = hisaddr >> 24;
2290				p[3] = hisaddr >> 16;
2291				p[4] = hisaddr >> 8;
2292				p[5] = hisaddr;
2293			}
2294			break;
2295		}
2296		/* Add the option to nak'ed list. */
2297		bcopy (p, r, p[1]);
2298		r += p[1];
2299		rlen += p[1];
2300	}
2301
2302	/*
2303	 * If we are about to conf-ack the request, but haven't seen
2304	 * his address so far, gonna conf-nak it instead, with the
2305	 * `address' option present and our idea of his address being
2306	 * filled in there, to request negotiation of both addresses.
2307	 *
2308	 * XXX This can result in an endless req - nak loop if peer
2309	 * doesn't want to send us his address.  Q: What should we do
2310	 * about it?  XXX  A: implement the max-failure counter.
2311	 */
2312	if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN)) {
2313		buf[0] = IPCP_OPT_ADDRESS;
2314		buf[1] = 6;
2315		buf[2] = hisaddr >> 24;
2316		buf[3] = hisaddr >> 16;
2317		buf[4] = hisaddr >> 8;
2318		buf[5] = hisaddr;
2319		rlen = 6;
2320		if (debug)
2321			addlog("still need hisaddr ");
2322	}
2323
2324	if (rlen) {
2325		if (debug)
2326			addlog(" send conf-nak\n");
2327		sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
2328	} else {
2329		if (debug)
2330			addlog(" send conf-ack\n");
2331		sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
2332			      h->ident, origlen, h+1);
2333	}
2334
2335	free (buf, M_TEMP);
2336	return (rlen == 0);
2337}
2338
2339/*
2340 * Analyze the IPCP Configure-Reject option list, and adjust our
2341 * negotiation.
2342 */
2343static void
2344sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2345{
2346	u_char *buf, *p;
2347	struct ifnet *ifp = &sp->pp_if;
2348	int debug = ifp->if_flags & IFF_DEBUG;
2349
2350	len -= 4;
2351	buf = malloc (len, M_TEMP, M_NOWAIT);
2352	if (!buf)
2353		return;
2354
2355	if (debug)
2356		log(LOG_DEBUG, "%s%d: ipcp rej opts: ",
2357		    ifp->if_name, ifp->if_unit);
2358
2359	p = (void*) (h+1);
2360	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2361		if (debug)
2362			addlog(" %s ", sppp_ipcp_opt_name(*p));
2363		switch (*p) {
2364		case IPCP_OPT_ADDRESS:
2365			/*
2366			 * Peer doesn't grok address option.  This is
2367			 * bad.  XXX  Should we better give up here?
2368			 */
2369			sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
2370			break;
2371#ifdef notyet
2372		case IPCP_OPT_COMPRESS:
2373			sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
2374			break;
2375#endif
2376		}
2377	}
2378	if (debug)
2379		addlog("\n");
2380	free (buf, M_TEMP);
2381	return;
2382}
2383
2384/*
2385 * Analyze the IPCP Configure-NAK option list, and adjust our
2386 * negotiation.
2387 */
2388static void
2389sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2390{
2391	u_char *buf, *p;
2392	struct ifnet *ifp = &sp->pp_if;
2393	int debug = ifp->if_flags & IFF_DEBUG;
2394	u_long wantaddr;
2395
2396	len -= 4;
2397	buf = malloc (len, M_TEMP, M_NOWAIT);
2398	if (!buf)
2399		return;
2400
2401	if (debug)
2402		log(LOG_DEBUG, "%s%d: ipcp nak opts: ",
2403		    ifp->if_name, ifp->if_unit);
2404
2405	p = (void*) (h+1);
2406	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2407		if (debug)
2408			addlog(" %s ", sppp_ipcp_opt_name(*p));
2409		switch (*p) {
2410		case IPCP_OPT_ADDRESS:
2411			/*
2412			 * Peer doesn't like our local IP address.  See
2413			 * if we can do something for him.  We'll drop
2414			 * him our address then.
2415			 */
2416			if (len >= 6 && p[1] == 6) {
2417				wantaddr = p[2] << 24 | p[3] << 16 |
2418					p[4] << 8 | p[5];
2419				sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2420				if (debug)
2421					addlog("[wantaddr 0x%x] ", wantaddr);
2422				/*
2423				 * When doing dynamic address assignment,
2424				 * we accept his offer.  Otherwise, we
2425				 * ignore it and thus continue to negotiate
2426				 * our already existing value.
2427				 */
2428				if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
2429					sppp_set_ip_addr(sp, wantaddr);
2430					if (debug)
2431						addlog("[agree] ");
2432				}
2433			}
2434			break;
2435#ifdef notyet
2436		case IPCP_OPT_COMPRESS:
2437			/*
2438			 * Peer wants different compression parameters.
2439			 */
2440			break;
2441#endif
2442		}
2443	}
2444	if (debug)
2445		addlog("\n");
2446	free (buf, M_TEMP);
2447	return;
2448}
2449
2450static void
2451sppp_ipcp_tlu(struct sppp *sp)
2452{
2453}
2454
2455static void
2456sppp_ipcp_tld(struct sppp *sp)
2457{
2458}
2459
2460static void
2461sppp_ipcp_tls(struct sppp *sp)
2462{
2463	/* indicate to LCP that it must stay alive */
2464	sp->lcp.protos |= (1 << IDX_IPCP);
2465}
2466
2467static void
2468sppp_ipcp_tlf(struct sppp *sp)
2469{
2470	/* we no longer need LCP */
2471	sp->lcp.protos &= ~(1 << IDX_IPCP);
2472	sppp_lcp_check(sp);
2473}
2474
2475static void
2476sppp_ipcp_scr(struct sppp *sp)
2477{
2478	char opt[6 /* compression */ + 6 /* address */];
2479	u_long ouraddr;
2480	int i = 0;
2481
2482#ifdef notyet
2483	if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
2484		opt[i++] = IPCP_OPT_COMPRESSION;
2485		opt[i++] = 6;
2486		opt[i++] = 0;	/* VJ header compression */
2487		opt[i++] = 0x2d; /* VJ header compression */
2488		opt[i++] = max_slot_id;
2489		opt[i++] = comp_slot_id;
2490	}
2491#endif
2492
2493	if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
2494		sppp_get_ip_addrs(sp, &ouraddr, 0);
2495		opt[i++] = IPCP_OPT_ADDRESS;
2496		opt[i++] = 6;
2497		opt[i++] = ouraddr >> 24;
2498		opt[i++] = ouraddr >> 16;
2499		opt[i++] = ouraddr >> 8;
2500		opt[i++] = ouraddr;
2501	}
2502
2503	sp->confid[IDX_IPCP] = ++sp->pp_seq;
2504	sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
2505}
2506
2507
2508/*
2509 * Random miscellaneous functions.
2510 */
2511
2512/*
2513 * Flush interface queue.
2514 */
2515static void
2516sppp_qflush(struct ifqueue *ifq)
2517{
2518	struct mbuf *m, *n;
2519
2520	n = ifq->ifq_head;
2521	while ((m = n)) {
2522		n = m->m_act;
2523		m_freem (m);
2524	}
2525	ifq->ifq_head = 0;
2526	ifq->ifq_tail = 0;
2527	ifq->ifq_len = 0;
2528}
2529
2530/*
2531 * Send keepalive packets, every 10 seconds.
2532 */
2533static void
2534sppp_keepalive(void *dummy)
2535{
2536	struct sppp *sp;
2537	int s;
2538
2539	s = splimp();
2540	for (sp=spppq; sp; sp=sp->pp_next) {
2541		struct ifnet *ifp = &sp->pp_if;
2542
2543		/* Keepalive mode disabled or channel down? */
2544		if (! (sp->pp_flags & PP_KEEPALIVE) ||
2545		    ! (ifp->if_flags & IFF_RUNNING))
2546			continue;
2547
2548		/* No keepalive in PPP mode if LCP not opened yet. */
2549		if (! (sp->pp_flags & PP_CISCO) &&
2550		    sp->pp_phase < PHASE_AUTHENTICATE)
2551			continue;
2552
2553		if (sp->pp_alivecnt == MAXALIVECNT) {
2554			/* No keepalive packets got.  Stop the interface. */
2555			printf ("%s%d: down\n", ifp->if_name, ifp->if_unit);
2556			if_down (ifp);
2557			sppp_qflush (&sp->pp_cpq);
2558			if (! (sp->pp_flags & PP_CISCO)) {
2559				/* XXX */
2560				/* Shut down the PPP link. */
2561				lcp.Down(sp);
2562				/* Initiate negotiation. XXX */
2563				lcp.Up(sp);
2564			}
2565		}
2566		if (sp->pp_alivecnt <= MAXALIVECNT)
2567			++sp->pp_alivecnt;
2568		if (sp->pp_flags & PP_CISCO)
2569			sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
2570				sp->pp_rseq);
2571		else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
2572			long nmagic = htonl (sp->lcp.magic);
2573			sp->lcp.echoid = ++sp->pp_seq;
2574			sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
2575				sp->lcp.echoid, 4, &nmagic);
2576		}
2577	}
2578	splx(s);
2579	timeout(sppp_keepalive, 0, hz * 10);
2580}
2581
2582/*
2583 * Get both IP addresses.
2584 */
2585static void
2586sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst)
2587{
2588	struct ifnet *ifp = &sp->pp_if;
2589	struct ifaddr *ifa;
2590	struct sockaddr_in *si;
2591	u_long ssrc, ddst;
2592
2593	ssrc = ddst = 0L;
2594	/*
2595	 * Pick the first AF_INET address from the list,
2596	 * aliases don't make any sense on a p2p link anyway.
2597	 */
2598	for (ifa = ifp->if_addrhead.tqh_first, si = 0;
2599	     ifa;
2600	     ifa = ifa->ifa_link.tqe_next)
2601		if (ifa->ifa_addr->sa_family == AF_INET) {
2602			si = (struct sockaddr_in *)ifa->ifa_addr;
2603			if (si)
2604				break;
2605		}
2606	if (ifa) {
2607		if (si && si->sin_addr.s_addr)
2608			ssrc = si->sin_addr.s_addr;
2609
2610		si = (struct sockaddr_in *)ifa->ifa_dstaddr;
2611		if (si && si->sin_addr.s_addr)
2612			ddst = si->sin_addr.s_addr;
2613	}
2614
2615	if (dst) *dst = ntohl(ddst);
2616	if (src) *src = ntohl(ssrc);
2617}
2618
2619/*
2620 * Set my IP address.  Must be called at splimp.
2621 */
2622static void
2623sppp_set_ip_addr(struct sppp *sp, u_long src)
2624{
2625	struct ifnet *ifp = &sp->pp_if;
2626	struct ifaddr *ifa;
2627	struct sockaddr_in *si;
2628	u_long ssrc, ddst;
2629
2630	/*
2631	 * Pick the first AF_INET address from the list,
2632	 * aliases don't make any sense on a p2p link anyway.
2633	 */
2634	for (ifa = ifp->if_addrhead.tqh_first, si = 0;
2635	     ifa;
2636	     ifa = ifa->ifa_link.tqe_next)
2637		if (ifa->ifa_addr->sa_family == AF_INET) {
2638			si = (struct sockaddr_in *)ifa->ifa_addr;
2639			if (si)
2640				break;
2641		}
2642	if (ifa && si)
2643		si->sin_addr.s_addr = htonl(src);
2644}
2645
2646static const char *
2647sppp_cp_type_name(u_char type)
2648{
2649	static char buf [12];
2650	switch (type) {
2651	case CONF_REQ:   return ("conf-req");
2652	case CONF_ACK:   return ("conf-ack");
2653	case CONF_NAK:   return ("conf-nak");
2654	case CONF_REJ:   return ("conf-rej");
2655	case TERM_REQ:   return ("term-req");
2656	case TERM_ACK:   return ("term-ack");
2657	case CODE_REJ:   return ("code-rej");
2658	case PROTO_REJ:  return ("proto-rej");
2659	case ECHO_REQ:   return ("echo-req");
2660	case ECHO_REPLY: return ("echo-reply");
2661	case DISC_REQ:   return ("discard-req");
2662	}
2663	sprintf (buf, "0x%x", type);
2664	return (buf);
2665}
2666
2667static const char *
2668sppp_lcp_opt_name(u_char opt)
2669{
2670	static char buf [12];
2671	switch (opt) {
2672	case LCP_OPT_MRU:		return ("mru");
2673	case LCP_OPT_ASYNC_MAP:		return ("async-map");
2674	case LCP_OPT_AUTH_PROTO:	return ("auth-proto");
2675	case LCP_OPT_QUAL_PROTO:	return ("qual-proto");
2676	case LCP_OPT_MAGIC:		return ("magic");
2677	case LCP_OPT_PROTO_COMP:	return ("proto-comp");
2678	case LCP_OPT_ADDR_COMP:		return ("addr-comp");
2679	}
2680	sprintf (buf, "0x%x", opt);
2681	return (buf);
2682}
2683
2684static const char *
2685sppp_ipcp_opt_name(u_char opt)
2686{
2687	static char buf [12];
2688	switch (opt) {
2689	case IPCP_OPT_ADDRESSES:	return ("addresses");
2690	case IPCP_OPT_COMPRESSION:	return ("compression");
2691	case IPCP_OPT_ADDRESS:		return ("address");
2692	}
2693	sprintf (buf, "0x%x", opt);
2694	return (buf);
2695}
2696
2697static const char *
2698sppp_state_name(int state)
2699{
2700	switch (state) {
2701	case STATE_INITIAL:	return "initial";
2702	case STATE_STARTING:	return "starting";
2703	case STATE_CLOSED:	return "closed";
2704	case STATE_STOPPED:	return "stopped";
2705	case STATE_CLOSING:	return "closing";
2706	case STATE_STOPPING:	return "stopping";
2707	case STATE_REQ_SENT:	return "req-sent";
2708	case STATE_ACK_RCVD:	return "ack-rcvd";
2709	case STATE_ACK_SENT:	return "ack-sent";
2710	case STATE_OPENED:	return "opened";
2711	}
2712	return "illegal";
2713}
2714
2715static const char *
2716sppp_phase_name(enum ppp_phase phase)
2717{
2718	switch (phase) {
2719	case PHASE_DEAD:	return "dead";
2720	case PHASE_ESTABLISH:	return "establish";
2721	case PHASE_TERMINATE:	return "terminate";
2722	case PHASE_AUTHENTICATE: return "authenticate";
2723	case PHASE_NETWORK:	return "network";
2724	}
2725	return "illegal";
2726}
2727
2728static const char *
2729sppp_proto_name(u_short proto)
2730{
2731	static char buf[12];
2732	switch (proto) {
2733	case PPP_LCP:	return "lcp";
2734	case PPP_IPCP:	return "ipcp";
2735	}
2736	sprintf(buf, "0x%x", (unsigned)proto);
2737	return buf;
2738}
2739
2740static void
2741sppp_print_bytes(u_char *p, u_short len)
2742{
2743	addlog(" %x", *p++);
2744	while (--len > 0)
2745		addlog("-%x", *p++);
2746}
2747
2748/*
2749 * This file is large.  Tell emacs to highlight it nevertheless.
2750 *
2751 * Local Variables:
2752 * hilit-auto-highlight-maxout: 100000
2753 * End:
2754 */
2755