if_spppsubr.c revision 16287
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@zebub.msk.su>
7 *
8 * This software is distributed with NO WARRANTIES, not even the implied
9 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 *
11 * Authors grant any other persons or organisations permission to use
12 * or modify this software as long as this message is kept with the software,
13 * all derivative works or modified versions.
14 *
15 * Version 1.9, Wed Oct  4 18:58:15 MSK 1995
16 */
17#undef DEBUG
18
19#include <sys/param.h>
20#include <sys/systm.h>
21#include <sys/kernel.h>
22#include <sys/ioctl.h>
23#include <sys/socket.h>
24#include <sys/mbuf.h>
25
26#include <net/if.h>
27#include <net/netisr.h>
28#include <net/if_types.h>
29
30#ifdef INET
31#include <netinet/in.h>
32#include <netinet/in_systm.h>
33#include <netinet/in_var.h>
34#include <netinet/ip.h>
35#include <netinet/tcp.h>
36#include <netinet/if_ether.h>
37#endif
38
39#ifdef IPX
40#include <netipx/ipx.h>
41#include <netipx/ipx_if.h>
42#endif
43
44#ifdef NS
45#include <netns/ns.h>
46#include <netns/ns_if.h>
47#endif
48
49#ifdef ISO
50#include <netiso/argo_debug.h>
51#include <netiso/iso.h>
52#include <netiso/iso_var.h>
53#include <netiso/iso_snpac.h>
54#endif
55
56#include <net/if_sppp.h>
57
58#ifdef DEBUG
59#define print(s)        printf s
60#else
61#define print(s)        {/*void*/}
62#endif
63
64#define MAXALIVECNT     3               /* max. alive packets */
65
66#define PPP_ALLSTATIONS 0xff            /* All-Stations broadcast address */
67#define PPP_UI          0x03            /* Unnumbered Information */
68#define PPP_IP          0x0021          /* Internet Protocol */
69#define PPP_ISO         0x0023          /* ISO OSI Protocol */
70#define PPP_XNS         0x0025          /* Xerox NS Protocol */
71#define PPP_IPX         0x002b          /* Novell IPX Protocol */
72#define PPP_LCP         0xc021          /* Link Control Protocol */
73#define PPP_IPCP        0x8021          /* Internet Protocol Control Protocol */
74
75#define LCP_CONF_REQ    1               /* PPP LCP configure request */
76#define LCP_CONF_ACK    2               /* PPP LCP configure acknowledge */
77#define LCP_CONF_NAK    3               /* PPP LCP configure negative ack */
78#define LCP_CONF_REJ    4               /* PPP LCP configure reject */
79#define LCP_TERM_REQ    5               /* PPP LCP terminate request */
80#define LCP_TERM_ACK    6               /* PPP LCP terminate acknowledge */
81#define LCP_CODE_REJ    7               /* PPP LCP code reject */
82#define LCP_PROTO_REJ   8               /* PPP LCP protocol reject */
83#define LCP_ECHO_REQ    9               /* PPP LCP echo request */
84#define LCP_ECHO_REPLY  10              /* PPP LCP echo reply */
85#define LCP_DISC_REQ    11              /* PPP LCP discard request */
86
87#define LCP_OPT_MRU             1       /* maximum receive unit */
88#define LCP_OPT_ASYNC_MAP       2       /* async control character map */
89#define LCP_OPT_AUTH_PROTO      3       /* authentication protocol */
90#define LCP_OPT_QUAL_PROTO      4       /* quality protocol */
91#define LCP_OPT_MAGIC           5       /* magic number */
92#define LCP_OPT_RESERVED        6       /* reserved */
93#define LCP_OPT_PROTO_COMP      7       /* protocol field compression */
94#define LCP_OPT_ADDR_COMP       8       /* address/control field compression */
95
96#define IPCP_CONF_REQ   LCP_CONF_REQ    /* PPP IPCP configure request */
97#define IPCP_CONF_ACK   LCP_CONF_ACK    /* PPP IPCP configure acknowledge */
98#define IPCP_CONF_NAK   LCP_CONF_NAK    /* PPP IPCP configure negative ack */
99#define IPCP_CONF_REJ   LCP_CONF_REJ    /* PPP IPCP configure reject */
100#define IPCP_TERM_REQ   LCP_TERM_REQ    /* PPP IPCP terminate request */
101#define IPCP_TERM_ACK   LCP_TERM_ACK    /* PPP IPCP terminate acknowledge */
102#define IPCP_CODE_REJ   LCP_CODE_REJ    /* PPP IPCP code reject */
103
104#define CISCO_MULTICAST         0x8f    /* Cisco multicast address */
105#define CISCO_UNICAST           0x0f    /* Cisco unicast address */
106#define CISCO_KEEPALIVE         0x8035  /* Cisco keepalive protocol */
107#define CISCO_ADDR_REQ          0       /* Cisco address request */
108#define CISCO_ADDR_REPLY        1       /* Cisco address reply */
109#define CISCO_KEEPALIVE_REQ     2       /* Cisco keepalive request */
110
111struct ppp_header {
112	u_char address;
113	u_char control;
114	u_short protocol;
115};
116#define PPP_HEADER_LEN          sizeof (struct ppp_header)
117
118struct lcp_header {
119	u_char type;
120	u_char ident;
121	u_short len;
122};
123#define LCP_HEADER_LEN          sizeof (struct lcp_header)
124
125struct cisco_packet {
126	u_long type;
127	u_long par1;
128	u_long par2;
129	u_short rel;
130	u_short time0;
131	u_short time1;
132};
133#define CISCO_PACKET_LEN 18
134
135static struct sppp *spppq;
136
137/*
138 * The following disgusting hack gets around the problem that IP TOS
139 * can't be set yet.  We want to put "interactive" traffic on a high
140 * priority queue.  To decide if traffic is interactive, we check that
141 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
142 */
143static u_short interactive_ports[8] = {
144	0,	513,	0,	0,
145	0,	21,	0,	23,
146};
147#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
148
149/*
150 * Timeout routine activation macros.
151 */
152#define TIMO(p,s) if (! ((p)->pp_flags & PP_TIMO)) { \
153			timeout (sppp_cp_timeout, (void*) (p), (s)*hz); \
154			(p)->pp_flags |= PP_TIMO; }
155#define UNTIMO(p) if ((p)->pp_flags & PP_TIMO) { \
156			untimeout (sppp_cp_timeout, (void*) (p)); \
157			(p)->pp_flags &= ~PP_TIMO; }
158
159static void sppp_keepalive (void *dummy);
160static void sppp_cp_send (struct sppp *sp, u_short proto, u_char type,
161	u_char ident, u_short len, void *data);
162static void sppp_cisco_send (struct sppp *sp, int type, long par1, long par2);
163static void sppp_lcp_input (struct sppp *sp, struct mbuf *m);
164static void sppp_cisco_input (struct sppp *sp, struct mbuf *m);
165static void sppp_ipcp_input (struct sppp *sp, struct mbuf *m);
166static void sppp_lcp_open (struct sppp *sp);
167static void sppp_ipcp_open (struct sppp *sp);
168static int sppp_lcp_conf_parse_options (struct sppp *sp, struct lcp_header *h,
169	int len, u_long *magic);
170static void sppp_cp_timeout (void *arg);
171static char *sppp_lcp_type_name (u_char type);
172static char *sppp_ipcp_type_name (u_char type);
173static void sppp_print_bytes (u_char *p, u_short len);
174static int sppp_output (struct ifnet *ifp, struct mbuf *m,
175	struct sockaddr *dst, struct rtentry *rt);
176
177/*
178 * Flush interface queue.
179 */
180static void qflush (struct ifqueue *ifq)
181{
182	struct mbuf *m, *n;
183
184	n = ifq->ifq_head;
185	while ((m = n)) {
186		n = m->m_act;
187		m_freem (m);
188	}
189	ifq->ifq_head = 0;
190	ifq->ifq_tail = 0;
191	ifq->ifq_len = 0;
192}
193
194/*
195 * Process the received packet.
196 */
197void sppp_input (struct ifnet *ifp, struct mbuf *m)
198{
199	struct ppp_header *h;
200	struct sppp *sp = (struct sppp*) ifp;
201	struct ifqueue *inq = 0;
202	int s;
203
204	if (ifp->if_flags & IFF_UP)
205		/* Count received bytes, add FCS and one flag */
206		ifp->if_ibytes += m->m_pkthdr.len + 3;
207
208	if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
209		/* Too small packet, drop it. */
210		if (ifp->if_flags & IFF_DEBUG)
211			printf ("%s%d: input packet is too small, %d bytes\n",
212				ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
213drop:           ++ifp->if_iqdrops;
214		m_freem (m);
215		return;
216	}
217
218	/* Get PPP header. */
219	h = mtod (m, struct ppp_header*);
220	m_adj (m, PPP_HEADER_LEN);
221
222	switch (h->address) {
223	default:        /* Invalid PPP packet. */
224invalid:        if (ifp->if_flags & IFF_DEBUG)
225			printf ("%s%d: invalid input packet <0x%x 0x%x 0x%x>\n",
226				ifp->if_name, ifp->if_unit,
227				h->address, h->control, ntohs (h->protocol));
228		goto drop;
229	case PPP_ALLSTATIONS:
230		if (h->control != PPP_UI)
231			goto invalid;
232		if (sp->pp_flags & PP_CISCO) {
233			if (ifp->if_flags & IFF_DEBUG)
234				printf ("%s%d: PPP packet in Cisco mode <0x%x 0x%x 0x%x>\n",
235					ifp->if_name, ifp->if_unit,
236					h->address, h->control, ntohs (h->protocol));
237			goto drop;
238		}
239		switch (ntohs (h->protocol)) {
240		default:
241			if (sp->lcp.state == LCP_STATE_OPENED)
242				sppp_cp_send (sp, PPP_LCP, LCP_PROTO_REJ,
243					++sp->pp_seq, m->m_pkthdr.len + 2,
244					&h->protocol);
245			if (ifp->if_flags & IFF_DEBUG)
246				printf ("%s%d: invalid input protocol <0x%x 0x%x 0x%x>\n",
247					ifp->if_name, ifp->if_unit,
248					h->address, h->control, ntohs (h->protocol));
249			++ifp->if_noproto;
250			goto drop;
251		case PPP_LCP:
252			sppp_lcp_input ((struct sppp*) ifp, m);
253			m_freem (m);
254			return;
255#ifdef INET
256		case PPP_IPCP:
257			if (sp->lcp.state == LCP_STATE_OPENED)
258				sppp_ipcp_input ((struct sppp*) ifp, m);
259			m_freem (m);
260			return;
261		case PPP_IP:
262			if (sp->ipcp.state == IPCP_STATE_OPENED) {
263				schednetisr (NETISR_IP);
264				inq = &ipintrq;
265			}
266			break;
267#endif
268#ifdef IPX
269		case PPP_IPX:
270			/* IPX IPXCP not implemented yet */
271			if (sp->lcp.state == LCP_STATE_OPENED) {
272				schednetisr (NETISR_IPX);
273				inq = &ipxintrq;
274			}
275			break;
276#endif
277#ifdef NS
278		case PPP_XNS:
279			/* XNS IDPCP not implemented yet */
280			if (sp->lcp.state == LCP_STATE_OPENED) {
281				schednetisr (NETISR_NS);
282				inq = &nsintrq;
283			}
284			break;
285#endif
286#ifdef ISO
287		case PPP_ISO:
288			/* OSI NLCP not implemented yet */
289			if (sp->lcp.state == LCP_STATE_OPENED) {
290				schednetisr (NETISR_ISO);
291				inq = &clnlintrq;
292			}
293			break;
294#endif
295		}
296		break;
297	case CISCO_MULTICAST:
298	case CISCO_UNICAST:
299		/* Don't check the control field here (RFC 1547). */
300		if (! (sp->pp_flags & PP_CISCO)) {
301			if (ifp->if_flags & IFF_DEBUG)
302				printf ("%s%d: Cisco packet in PPP mode <0x%x 0x%x 0x%x>\n",
303					ifp->if_name, ifp->if_unit,
304					h->address, h->control, ntohs (h->protocol));
305			goto drop;
306		}
307		switch (ntohs (h->protocol)) {
308		default:
309			++ifp->if_noproto;
310			goto invalid;
311		case CISCO_KEEPALIVE:
312			sppp_cisco_input ((struct sppp*) ifp, m);
313			m_freem (m);
314			return;
315#ifdef INET
316		case ETHERTYPE_IP:
317			schednetisr (NETISR_IP);
318			inq = &ipintrq;
319			break;
320#endif
321#ifdef IPX
322		case ETHERTYPE_IPX:
323			schednetisr (NETISR_IPX);
324			inq = &ipxintrq;
325			break;
326#endif
327#ifdef NS
328		case ETHERTYPE_NS:
329			schednetisr (NETISR_NS);
330			inq = &nsintrq;
331			break;
332#endif
333		}
334		break;
335	}
336
337	if (! (ifp->if_flags & IFF_UP) || ! inq)
338		goto drop;
339
340	/* Check queue. */
341	s = splimp ();
342	if (IF_QFULL (inq)) {
343		/* Queue overflow. */
344		IF_DROP (inq);
345		splx (s);
346		if (ifp->if_flags & IFF_DEBUG)
347			printf ("%s%d: protocol queue overflow\n",
348				ifp->if_name, ifp->if_unit);
349		goto drop;
350	}
351	IF_ENQUEUE (inq, m);
352	splx (s);
353}
354
355/*
356 * Enqueue transmit packet.
357 */
358static int
359sppp_output (struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, struct rtentry *rt)
360{
361	struct sppp *sp = (struct sppp*) ifp;
362	struct ppp_header *h;
363	struct ifqueue *ifq;
364	int s = splimp ();
365
366	if (! (ifp->if_flags & IFF_UP) || ! (ifp->if_flags & IFF_RUNNING)) {
367		m_freem (m);
368		splx (s);
369		return (ENETDOWN);
370	}
371
372	ifq = &ifp->if_snd;
373#ifdef INET
374	/*
375	 * Put low delay, telnet, rlogin and ftp control packets
376	 * in front of the queue.
377	 */
378	if (dst->sa_family == AF_INET) {
379		struct ip *ip = mtod (m, struct ip*);
380		struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
381
382		if (! IF_QFULL (&sp->pp_fastq) &&
383		    ((ip->ip_tos & IPTOS_LOWDELAY) ||
384	    	    ip->ip_p == IPPROTO_TCP &&
385	    	    m->m_len >= sizeof (struct ip) + sizeof (struct tcphdr) &&
386	    	    (INTERACTIVE (ntohs (tcp->th_sport)) ||
387	    	    INTERACTIVE (ntohs (tcp->th_dport)))))
388			ifq = &sp->pp_fastq;
389	}
390#endif
391
392	/*
393	 * Prepend general data packet PPP header. For now, IP only.
394	 */
395	M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
396	if (! m) {
397		if (ifp->if_flags & IFF_DEBUG)
398			printf ("%s%d: no memory for transmit header\n",
399				ifp->if_name, ifp->if_unit);
400		splx (s);
401		return (ENOBUFS);
402	}
403	h = mtod (m, struct ppp_header*);
404	if (sp->pp_flags & PP_CISCO) {
405		h->address = CISCO_MULTICAST;        /* broadcast address */
406		h->control = 0;
407	} else {
408		h->address = PPP_ALLSTATIONS;        /* broadcast address */
409		h->control = PPP_UI;                 /* Unnumbered Info */
410	}
411
412	switch (dst->sa_family) {
413#ifdef INET
414	case AF_INET:   /* Internet Protocol */
415		if (sp->pp_flags & PP_CISCO)
416			h->protocol = htons (ETHERTYPE_IP);
417		else if (sp->ipcp.state == IPCP_STATE_OPENED)
418			h->protocol = htons (PPP_IP);
419		else {
420			m_freem (m);
421			splx (s);
422			return (ENETDOWN);
423		}
424		break;
425#endif
426#ifdef NS
427	case AF_NS:     /* Xerox NS Protocol */
428		h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
429			ETHERTYPE_NS : PPP_XNS);
430		break;
431#endif
432#ifdef IPX
433	case AF_IPX:     /* Novell IPX Protocol */
434		h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
435			ETHERTYPE_IPX : PPP_IPX);
436		break;
437#endif
438#ifdef ISO
439	case AF_ISO:    /* ISO OSI Protocol */
440		if (sp->pp_flags & PP_CISCO)
441			goto nosupport;
442		h->protocol = htons (PPP_ISO);
443		break;
444nosupport:
445#endif
446	default:
447		m_freem (m);
448		splx (s);
449		return (EAFNOSUPPORT);
450	}
451
452	/*
453	 * Queue message on interface, and start output if interface
454	 * not yet active.
455	 */
456	if (IF_QFULL (ifq)) {
457		IF_DROP (&ifp->if_snd);
458		m_freem (m);
459		splx (s);
460		return (ENOBUFS);
461	}
462	IF_ENQUEUE (ifq, m);
463	if (! (ifp->if_flags & IFF_OACTIVE))
464		(*ifp->if_start) (ifp);
465
466	/*
467	 * Count output packets and bytes.
468	 * The packet length includes header, FCS and 1 flag,
469	 * according to RFC 1333.
470	 */
471	ifp->if_obytes += m->m_pkthdr.len + 3;
472	splx (s);
473	return (0);
474}
475
476void sppp_attach (struct ifnet *ifp)
477{
478	struct sppp *sp = (struct sppp*) ifp;
479
480	/* Initialize keepalive handler. */
481	if (! spppq)
482		timeout (sppp_keepalive, 0, hz * 10);
483
484	/* Insert new entry into the keepalive list. */
485	sp->pp_next = spppq;
486	spppq = sp;
487
488	sp->pp_if.if_type = IFT_PPP;
489	sp->pp_if.if_output = sppp_output;
490	sp->pp_fastq.ifq_maxlen = 32;
491	sp->pp_loopcnt = 0;
492	sp->pp_alivecnt = 0;
493	sp->pp_seq = 0;
494	sp->pp_rseq = 0;
495	sp->lcp.magic = 0;
496	sp->lcp.state = LCP_STATE_CLOSED;
497	sp->ipcp.state = IPCP_STATE_CLOSED;
498}
499
500void
501sppp_detach (struct ifnet *ifp)
502{
503	struct sppp **q, *p, *sp = (struct sppp*) ifp;
504
505	/* Remove the entry from the keepalive list. */
506	for (q = &spppq; (p = *q); q = &p->pp_next)
507		if (p == sp) {
508			*q = p->pp_next;
509			break;
510		}
511
512	/* Stop keepalive handler. */
513	if (! spppq)
514		untimeout (sppp_keepalive, 0);
515	UNTIMO (sp);
516}
517
518/*
519 * Flush the interface output queue.
520 */
521void sppp_flush (struct ifnet *ifp)
522{
523	struct sppp *sp = (struct sppp*) ifp;
524
525	qflush (&sp->pp_if.if_snd);
526	qflush (&sp->pp_fastq);
527}
528
529/*
530 * Check if the output queue is empty.
531 */
532int
533sppp_isempty (struct ifnet *ifp)
534{
535	struct sppp *sp = (struct sppp*) ifp;
536	int empty, s = splimp ();
537
538	empty = !sp->pp_fastq.ifq_head && !sp->pp_if.if_snd.ifq_head;
539	splx (s);
540	return (empty);
541}
542
543/*
544 * Get next packet to send.
545 */
546struct mbuf *sppp_dequeue (struct ifnet *ifp)
547{
548	struct sppp *sp = (struct sppp*) ifp;
549	struct mbuf *m;
550	int s = splimp ();
551
552	IF_DEQUEUE (&sp->pp_fastq, m);
553	if (! m)
554		IF_DEQUEUE (&sp->pp_if.if_snd, m);
555	splx (s);
556	return (m);
557}
558
559/*
560 * Send keepalive packets, every 10 seconds.
561 */
562void sppp_keepalive (void *dummy)
563{
564	struct sppp *sp;
565	int s = splimp ();
566
567	for (sp=spppq; sp; sp=sp->pp_next) {
568		struct ifnet *ifp = &sp->pp_if;
569
570		/* Keepalive mode disabled or channel down? */
571		if (! (sp->pp_flags & PP_KEEPALIVE) ||
572		    ! (ifp->if_flags & IFF_RUNNING))
573			continue;
574
575		/* No keepalive in PPP mode if LCP not opened yet. */
576		if (! (sp->pp_flags & PP_CISCO) &&
577		    sp->lcp.state != LCP_STATE_OPENED)
578			continue;
579
580		if (sp->pp_alivecnt == MAXALIVECNT) {
581			/* No keepalive packets got.  Stop the interface. */
582			printf ("%s%d: down\n", ifp->if_name, ifp->if_unit);
583			if_down (ifp);
584			qflush (&sp->pp_fastq);
585			if (! (sp->pp_flags & PP_CISCO)) {
586				/* Shut down the PPP link. */
587				sp->lcp.state = LCP_STATE_CLOSED;
588				sp->ipcp.state = IPCP_STATE_CLOSED;
589				UNTIMO (sp);
590				/* Initiate negotiation. */
591				sppp_lcp_open (sp);
592			}
593		}
594		if (sp->pp_alivecnt <= MAXALIVECNT)
595			++sp->pp_alivecnt;
596		if (sp->pp_flags & PP_CISCO)
597			sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
598				sp->pp_rseq);
599		else if (sp->lcp.state == LCP_STATE_OPENED) {
600			long nmagic = htonl (sp->lcp.magic);
601			sp->lcp.echoid = ++sp->pp_seq;
602			sppp_cp_send (sp, PPP_LCP, LCP_ECHO_REQ,
603				sp->lcp.echoid, 4, &nmagic);
604		}
605	}
606	splx (s);
607	timeout (sppp_keepalive, 0, hz * 10);
608}
609
610/*
611 * Handle incoming PPP Link Control Protocol packets.
612 */
613void sppp_lcp_input (struct sppp *sp, struct mbuf *m)
614{
615	struct lcp_header *h;
616	struct ifnet *ifp = &sp->pp_if;
617	int len = m->m_pkthdr.len;
618	u_char *p, opt[6];
619	u_long rmagic;
620
621	if (len < 4) {
622		if (ifp->if_flags & IFF_DEBUG)
623			printf ("%s%d: invalid lcp packet length: %d bytes\n",
624				ifp->if_name, ifp->if_unit, len);
625		return;
626	}
627	h = mtod (m, struct lcp_header*);
628	if (ifp->if_flags & IFF_DEBUG) {
629		char state = '?';
630		switch (sp->lcp.state) {
631		case LCP_STATE_CLOSED:   state = 'C'; break;
632		case LCP_STATE_ACK_RCVD: state = 'R'; break;
633		case LCP_STATE_ACK_SENT: state = 'S'; break;
634		case LCP_STATE_OPENED:   state = 'O'; break;
635		}
636		printf ("%s%d: lcp input(%c): %d bytes <%s id=%xh len=%xh",
637			ifp->if_name, ifp->if_unit, state, len,
638			sppp_lcp_type_name (h->type), h->ident, ntohs (h->len));
639		if (len > 4)
640			sppp_print_bytes ((u_char*) (h+1), len-4);
641		printf (">\n");
642	}
643	if (len > ntohs (h->len))
644		len = ntohs (h->len);
645	switch (h->type) {
646	default:
647		/* Unknown packet type -- send Code-Reject packet. */
648		sppp_cp_send (sp, PPP_LCP, LCP_CODE_REJ, ++sp->pp_seq,
649			m->m_pkthdr.len, h);
650		break;
651	case LCP_CONF_REQ:
652		if (len < 4) {
653			if (ifp->if_flags & IFF_DEBUG)
654				printf ("%s%d: invalid lcp configure request packet length: %d bytes\n",
655					ifp->if_name, ifp->if_unit, len);
656			break;
657		}
658		if (len>4 && !sppp_lcp_conf_parse_options (sp, h, len, &rmagic))
659			goto badreq;
660		if (rmagic == sp->lcp.magic) {
661			/* Local and remote magics equal -- loopback? */
662			if (sp->pp_loopcnt >= MAXALIVECNT*5) {
663				printf ("%s%d: loopback\n",
664					ifp->if_name, ifp->if_unit);
665				sp->pp_loopcnt = 0;
666				if (ifp->if_flags & IFF_UP) {
667					if_down (ifp);
668					qflush (&sp->pp_fastq);
669				}
670			} else if (ifp->if_flags & IFF_DEBUG)
671				printf ("%s%d: conf req: magic glitch\n",
672					ifp->if_name, ifp->if_unit);
673			++sp->pp_loopcnt;
674
675			/* MUST send Conf-Nack packet. */
676			rmagic = ~sp->lcp.magic;
677			opt[0] = LCP_OPT_MAGIC;
678			opt[1] = sizeof (opt);
679			opt[2] = rmagic >> 24;
680			opt[3] = rmagic >> 16;
681			opt[4] = rmagic >> 8;
682			opt[5] = rmagic;
683			sppp_cp_send (sp, PPP_LCP, LCP_CONF_NAK,
684				h->ident, sizeof (opt), &opt);
685badreq:
686			switch (sp->lcp.state) {
687			case LCP_STATE_OPENED:
688				/* Initiate renegotiation. */
689				sppp_lcp_open (sp);
690				/* fall through... */
691			case LCP_STATE_ACK_SENT:
692				/* Go to closed state. */
693				sp->lcp.state = LCP_STATE_CLOSED;
694				sp->ipcp.state = IPCP_STATE_CLOSED;
695			}
696			break;
697		}
698		/* Send Configure-Ack packet. */
699		sp->pp_loopcnt = 0;
700		sppp_cp_send (sp, PPP_LCP, LCP_CONF_ACK,
701				h->ident, len-4, h+1);
702		/* Change the state. */
703		switch (sp->lcp.state) {
704		case LCP_STATE_CLOSED:
705			sp->lcp.state = LCP_STATE_ACK_SENT;
706			break;
707		case LCP_STATE_ACK_RCVD:
708			sp->lcp.state = LCP_STATE_OPENED;
709			sppp_ipcp_open (sp);
710			break;
711		case LCP_STATE_OPENED:
712			/* Remote magic changed -- close session. */
713			sp->lcp.state = LCP_STATE_CLOSED;
714			sp->ipcp.state = IPCP_STATE_CLOSED;
715			/* Initiate renegotiation. */
716			sppp_lcp_open (sp);
717			/* An ACK has already been sent. */
718			sp->lcp.state = LCP_STATE_ACK_SENT;
719			break;
720		}
721		break;
722	case LCP_CONF_ACK:
723		if (h->ident != sp->lcp.confid)
724			break;
725		UNTIMO (sp);
726		if (! (ifp->if_flags & IFF_UP) &&
727		    (ifp->if_flags & IFF_RUNNING)) {
728			/* Coming out of loopback mode. */
729			ifp->if_flags |= IFF_UP;
730			printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
731		}
732		switch (sp->lcp.state) {
733		case LCP_STATE_CLOSED:
734			sp->lcp.state = LCP_STATE_ACK_RCVD;
735			TIMO (sp, 5);
736			break;
737		case LCP_STATE_ACK_SENT:
738			sp->lcp.state = LCP_STATE_OPENED;
739			sppp_ipcp_open (sp);
740			break;
741		}
742		break;
743	case LCP_CONF_NAK:
744		if (h->ident != sp->lcp.confid)
745			break;
746		p = (u_char*) (h+1);
747		if (len>=10 && p[0] == LCP_OPT_MAGIC && p[1] >= 4) {
748			rmagic = (u_long)p[2] << 24 |
749				(u_long)p[3] << 16 | p[4] << 8 | p[5];
750			if (rmagic == ~sp->lcp.magic) {
751				if (ifp->if_flags & IFF_DEBUG)
752					printf ("%s%d: conf nak: magic glitch\n",
753						ifp->if_name, ifp->if_unit);
754				sp->lcp.magic += time.tv_sec + time.tv_usec;
755			} else
756				sp->lcp.magic = rmagic;
757			}
758		if (sp->lcp.state != LCP_STATE_ACK_SENT) {
759			/* Go to closed state. */
760			sp->lcp.state = LCP_STATE_CLOSED;
761			sp->ipcp.state = IPCP_STATE_CLOSED;
762		}
763		/* The link will be renegotiated after timeout,
764		 * to avoid endless req-nack loop. */
765		UNTIMO (sp);
766		TIMO (sp, 2);
767		break;
768	case LCP_CONF_REJ:
769		if (h->ident != sp->lcp.confid)
770			break;
771		UNTIMO (sp);
772		/* Initiate renegotiation. */
773		sppp_lcp_open (sp);
774		if (sp->lcp.state != LCP_STATE_ACK_SENT) {
775			/* Go to closed state. */
776			sp->lcp.state = LCP_STATE_CLOSED;
777			sp->ipcp.state = IPCP_STATE_CLOSED;
778		}
779		break;
780	case LCP_TERM_REQ:
781		UNTIMO (sp);
782		/* Send Terminate-Ack packet. */
783		sppp_cp_send (sp, PPP_LCP, LCP_TERM_ACK, h->ident, 0, 0);
784		/* Go to closed state. */
785		sp->lcp.state = LCP_STATE_CLOSED;
786		sp->ipcp.state = IPCP_STATE_CLOSED;
787		/* Initiate renegotiation. */
788		sppp_lcp_open (sp);
789		break;
790	case LCP_TERM_ACK:
791	case LCP_CODE_REJ:
792	case LCP_PROTO_REJ:
793		/* Ignore for now. */
794		break;
795	case LCP_DISC_REQ:
796		/* Discard the packet. */
797		break;
798	case LCP_ECHO_REQ:
799		if (sp->lcp.state != LCP_STATE_OPENED)
800			break;
801		if (len < 8) {
802			if (ifp->if_flags & IFF_DEBUG)
803				printf ("%s%d: invalid lcp echo request packet length: %d bytes\n",
804					ifp->if_name, ifp->if_unit, len);
805			break;
806		}
807		if (ntohl (*(long*)(h+1)) == sp->lcp.magic) {
808			/* Line loopback mode detected. */
809			printf ("%s%d: loopback\n", ifp->if_name, ifp->if_unit);
810			if_down (ifp);
811			qflush (&sp->pp_fastq);
812
813			/* Shut down the PPP link. */
814			sp->lcp.state = LCP_STATE_CLOSED;
815			sp->ipcp.state = IPCP_STATE_CLOSED;
816			UNTIMO (sp);
817			/* Initiate negotiation. */
818			sppp_lcp_open (sp);
819			break;
820		}
821		*(long*)(h+1) = htonl (sp->lcp.magic);
822		sppp_cp_send (sp, PPP_LCP, LCP_ECHO_REPLY, h->ident, len-4, h+1);
823		break;
824	case LCP_ECHO_REPLY:
825		if (h->ident != sp->lcp.echoid)
826			break;
827		if (len < 8) {
828			if (ifp->if_flags & IFF_DEBUG)
829				printf ("%s%d: invalid lcp echo reply packet length: %d bytes\n",
830					ifp->if_name, ifp->if_unit, len);
831			break;
832		}
833		if (ntohl (*(long*)(h+1)) != sp->lcp.magic)
834		sp->pp_alivecnt = 0;
835		break;
836	}
837}
838
839/*
840 * Handle incoming Cisco keepalive protocol packets.
841 */
842static void
843sppp_cisco_input (struct sppp *sp, struct mbuf *m)
844{
845	struct cisco_packet *h;
846	struct ifaddr *ifa;
847	struct ifnet *ifp = &sp->pp_if;
848
849	if (m->m_pkthdr.len != CISCO_PACKET_LEN) {
850		if (ifp->if_flags & IFF_DEBUG)
851			printf ("%s%d: invalid cisco packet length: %d bytes\n",
852				ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
853		return;
854	}
855	h = mtod (m, struct cisco_packet*);
856	if (ifp->if_flags & IFF_DEBUG)
857		printf ("%s%d: cisco input: %d bytes <%lxh %lxh %lxh %xh %xh-%xh>\n",
858			ifp->if_name, ifp->if_unit, m->m_pkthdr.len,
859			ntohl (h->type), h->par1, h->par2, h->rel,
860			h->time0, h->time1);
861	switch (ntohl (h->type)) {
862	default:
863		if (ifp->if_flags & IFF_DEBUG)
864			printf ("%s%d: unknown cisco packet type: 0x%lx\n",
865				ifp->if_name, ifp->if_unit, ntohl (h->type));
866		break;
867	case CISCO_ADDR_REPLY:
868		/* Reply on address request, ignore */
869		break;
870	case CISCO_KEEPALIVE_REQ:
871		sp->pp_alivecnt = 0;
872		sp->pp_rseq = ntohl (h->par1);
873		if (sp->pp_seq == sp->pp_rseq) {
874			/* Local and remote sequence numbers are equal.
875			 * Probably, the line is in loopback mode. */
876			if (sp->pp_loopcnt >= MAXALIVECNT) {
877				printf ("%s%d: loopback\n",
878					ifp->if_name, ifp->if_unit);
879				sp->pp_loopcnt = 0;
880				if (ifp->if_flags & IFF_UP) {
881					if_down (ifp);
882					qflush (&sp->pp_fastq);
883				}
884			}
885			++sp->pp_loopcnt;
886
887			/* Generate new local sequence number */
888			sp->pp_seq ^= time.tv_sec ^ time.tv_usec;
889			break;
890		}
891			sp->pp_loopcnt = 0;
892		if (! (ifp->if_flags & IFF_UP) &&
893		    (ifp->if_flags & IFF_RUNNING)) {
894			ifp->if_flags |= IFF_UP;
895			printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
896		}
897		break;
898	case CISCO_ADDR_REQ:
899		for (ifa=ifp->if_addrlist; ifa; ifa=ifa->ifa_next)
900			if (ifa->ifa_addr->sa_family == AF_INET)
901				break;
902		if (! ifa) {
903			if (ifp->if_flags & IFF_DEBUG)
904				printf ("%s%d: unknown address for cisco request\n",
905					ifp->if_name, ifp->if_unit);
906			return;
907		}
908		sppp_cisco_send (sp, CISCO_ADDR_REPLY,
909			ntohl (((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr),
910			ntohl (((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr.s_addr));
911		break;
912	}
913}
914
915/*
916 * Send PPP LCP packet.
917 */
918static void
919sppp_cp_send (struct sppp *sp, u_short proto, u_char type,
920	u_char ident, u_short len, void *data)
921{
922	struct ppp_header *h;
923	struct lcp_header *lh;
924	struct mbuf *m;
925	struct ifnet *ifp = &sp->pp_if;
926
927	if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
928		len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
929	MGETHDR (m, M_DONTWAIT, MT_DATA);
930	if (! m)
931		return;
932	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
933	m->m_pkthdr.rcvif = 0;
934
935	h = mtod (m, struct ppp_header*);
936	h->address = PPP_ALLSTATIONS;        /* broadcast address */
937	h->control = PPP_UI;                 /* Unnumbered Info */
938	h->protocol = htons (proto);         /* Link Control Protocol */
939
940	lh = (struct lcp_header*) (h + 1);
941	lh->type = type;
942	lh->ident = ident;
943	lh->len = htons (LCP_HEADER_LEN + len);
944	if (len)
945		bcopy (data, lh+1, len);
946
947	if (ifp->if_flags & IFF_DEBUG) {
948		printf ("%s%d: %s output <%s id=%xh len=%xh",
949			ifp->if_name, ifp->if_unit,
950			proto==PPP_LCP ? "lcp" : "ipcp",
951			proto==PPP_LCP ? sppp_lcp_type_name (lh->type) :
952			sppp_ipcp_type_name (lh->type), lh->ident,
953			ntohs (lh->len));
954		if (len)
955			sppp_print_bytes ((u_char*) (lh+1), len);
956		printf (">\n");
957	}
958	if (IF_QFULL (&sp->pp_fastq)) {
959		IF_DROP (&ifp->if_snd);
960		m_freem (m);
961	} else
962		IF_ENQUEUE (&sp->pp_fastq, m);
963	if (! (ifp->if_flags & IFF_OACTIVE))
964		(*ifp->if_start) (ifp);
965	ifp->if_obytes += m->m_pkthdr.len + 3;
966}
967
968/*
969 * Send Cisco keepalive packet.
970 */
971static void
972sppp_cisco_send (struct sppp *sp, int type, long par1, long par2)
973{
974	struct ppp_header *h;
975	struct cisco_packet *ch;
976	struct mbuf *m;
977	struct ifnet *ifp = &sp->pp_if;
978	u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
979
980	MGETHDR (m, M_DONTWAIT, MT_DATA);
981	if (! m)
982		return;
983	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
984	m->m_pkthdr.rcvif = 0;
985
986	h = mtod (m, struct ppp_header*);
987	h->address = CISCO_MULTICAST;
988	h->control = 0;
989	h->protocol = htons (CISCO_KEEPALIVE);
990
991	ch = (struct cisco_packet*) (h + 1);
992	ch->type = htonl (type);
993	ch->par1 = htonl (par1);
994	ch->par2 = htonl (par2);
995	ch->rel = -1;
996	ch->time0 = htons ((u_short) (t >> 16));
997	ch->time1 = htons ((u_short) t);
998
999	if (ifp->if_flags & IFF_DEBUG)
1000		printf ("%s%d: cisco output: <%lxh %lxh %lxh %xh %xh-%xh>\n",
1001			ifp->if_name, ifp->if_unit, ntohl (ch->type), ch->par1,
1002			ch->par2, ch->rel, ch->time0, ch->time1);
1003
1004	if (IF_QFULL (&sp->pp_fastq)) {
1005		IF_DROP (&ifp->if_snd);
1006		m_freem (m);
1007	} else
1008		IF_ENQUEUE (&sp->pp_fastq, 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 * Process an ioctl request.  Called on low priority level.
1016 */
1017int
1018sppp_ioctl (struct ifnet *ifp, int cmd, void *data)
1019{
1020	struct ifreq *ifr = (struct ifreq*) data;
1021	struct sppp *sp = (struct sppp*) ifp;
1022	int s, going_up, going_down;
1023
1024	switch (cmd) {
1025	default:
1026		return (EINVAL);
1027
1028	case SIOCAIFADDR:
1029	case SIOCSIFDSTADDR:
1030		break;
1031
1032	case SIOCSIFADDR:
1033		ifp->if_flags |= IFF_UP;
1034		/* fall through... */
1035
1036	case SIOCSIFFLAGS:
1037		s = splimp ();
1038		going_up   = (ifp->if_flags & IFF_UP) &&
1039			     ! (ifp->if_flags & IFF_RUNNING);
1040		going_down = ! (ifp->if_flags & IFF_UP) &&
1041			      (ifp->if_flags & IFF_RUNNING);
1042		if (going_up || going_down) {
1043			/* Shut down the PPP link. */
1044			ifp->if_flags &= ~IFF_RUNNING;
1045			sp->lcp.state = LCP_STATE_CLOSED;
1046			sp->ipcp.state = IPCP_STATE_CLOSED;
1047			UNTIMO (sp);
1048		}
1049		if (going_up) {
1050			/* Interface is starting -- initiate negotiation. */
1051			ifp->if_flags |= IFF_RUNNING;
1052			if (!(sp->pp_flags & PP_CISCO))
1053				sppp_lcp_open (sp);
1054		}
1055		splx (s);
1056		break;
1057
1058#ifdef SIOCSIFMTU
1059#ifndef ifr_mtu
1060#define ifr_mtu ifr_metric
1061#endif
1062	case SIOCSIFMTU:
1063		if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > PP_MTU)
1064			return (EINVAL);
1065		ifp->if_mtu = ifr->ifr_mtu;
1066		break;
1067#endif
1068#ifdef SLIOCSETMTU
1069	case SLIOCSETMTU:
1070		if (*(short*)data < 128 || *(short*)data > PP_MTU)
1071			return (EINVAL);
1072		ifp->if_mtu = *(short*)data;
1073		break;
1074#endif
1075#ifdef SIOCGIFMTU
1076	case SIOCGIFMTU:
1077		ifr->ifr_mtu = ifp->if_mtu;
1078		break;
1079#endif
1080#ifdef SLIOCGETMTU
1081	case SLIOCGETMTU:
1082		*(short*)data = ifp->if_mtu;
1083		break;
1084#endif
1085#ifdef MULTICAST
1086	case SIOCADDMULTI:
1087	case SIOCDELMULTI:
1088		break;
1089#endif
1090	}
1091	return (0);
1092}
1093
1094/*
1095 * Analyze the LCP Configure-Request options list
1096 * for the presence of unknown options.
1097 * If the request contains unknown options, build and
1098 * send Configure-reject packet, containing only unknown options.
1099 */
1100static int
1101sppp_lcp_conf_parse_options (struct sppp *sp, struct lcp_header *h,
1102	int len, u_long *magic)
1103{
1104	u_char *buf, *r, *p;
1105	int rlen;
1106
1107	len -= 4;
1108	buf = r = malloc (len, M_TEMP, M_NOWAIT);
1109	if (! buf)
1110		return (0);
1111
1112	p = (void*) (h+1);
1113	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1114		switch (*p) {
1115		case LCP_OPT_MAGIC:
1116			/* Magic number -- extract. */
1117			if (len >= 6 && p[1] == 6) {
1118				*magic = (u_long)p[2] << 24 |
1119					(u_long)p[3] << 16 | p[4] << 8 | p[5];
1120				continue;
1121			}
1122			break;
1123		case LCP_OPT_ASYNC_MAP:
1124			/* Async control character map -- check to be zero. */
1125			if (len >= 6 && p[1] == 6 && ! p[2] && ! p[3] &&
1126			    ! p[4] && ! p[5])
1127				continue;
1128			break;
1129		case LCP_OPT_MRU:
1130			/* Maximum receive unit -- always OK. */
1131			continue;
1132		default:
1133			/* Others not supported. */
1134			break;
1135		}
1136		/* Add the option to rejected list. */
1137			bcopy (p, r, p[1]);
1138			r += p[1];
1139		rlen += p[1];
1140	}
1141	if (rlen)
1142	sppp_cp_send (sp, PPP_LCP, LCP_CONF_REJ, h->ident, rlen, buf);
1143	free (buf, M_TEMP);
1144	return (rlen == 0);
1145}
1146
1147static void
1148sppp_ipcp_input (struct sppp *sp, struct mbuf *m)
1149{
1150	struct lcp_header *h;
1151	struct ifnet *ifp = &sp->pp_if;
1152	int len = m->m_pkthdr.len;
1153
1154	if (len < 4) {
1155		/* if (ifp->if_flags & IFF_DEBUG) */
1156			printf ("%s%d: invalid ipcp packet length: %d bytes\n",
1157				ifp->if_name, ifp->if_unit, len);
1158		return;
1159	}
1160	h = mtod (m, struct lcp_header*);
1161	if (ifp->if_flags & IFF_DEBUG) {
1162		printf ("%s%d: ipcp input: %d bytes <%s id=%xh len=%xh",
1163			ifp->if_name, ifp->if_unit, len,
1164			sppp_ipcp_type_name (h->type), h->ident, ntohs (h->len));
1165		if (len > 4)
1166			sppp_print_bytes ((u_char*) (h+1), len-4);
1167		printf (">\n");
1168	}
1169	if (len > ntohs (h->len))
1170		len = ntohs (h->len);
1171	switch (h->type) {
1172	default:
1173		/* Unknown packet type -- send Code-Reject packet. */
1174		sppp_cp_send (sp, PPP_IPCP, IPCP_CODE_REJ, ++sp->pp_seq, len, h);
1175		break;
1176	case IPCP_CONF_REQ:
1177		if (len < 4) {
1178			if (ifp->if_flags & IFF_DEBUG)
1179				printf ("%s%d: invalid ipcp configure request packet length: %d bytes\n",
1180					ifp->if_name, ifp->if_unit, len);
1181			return;
1182		}
1183		if (len > 4) {
1184			sppp_cp_send (sp, PPP_IPCP, LCP_CONF_REJ, h->ident,
1185				len-4, h+1);
1186
1187			switch (sp->ipcp.state) {
1188			case IPCP_STATE_OPENED:
1189				/* Initiate renegotiation. */
1190				sppp_ipcp_open (sp);
1191				/* fall through... */
1192			case IPCP_STATE_ACK_SENT:
1193				/* Go to closed state. */
1194				sp->ipcp.state = IPCP_STATE_CLOSED;
1195			}
1196		} else {
1197			/* Send Configure-Ack packet. */
1198			sppp_cp_send (sp, PPP_IPCP, IPCP_CONF_ACK, h->ident,
1199				0, 0);
1200			/* Change the state. */
1201			if (sp->ipcp.state == IPCP_STATE_ACK_RCVD)
1202				sp->ipcp.state = IPCP_STATE_OPENED;
1203			else
1204				sp->ipcp.state = IPCP_STATE_ACK_SENT;
1205		}
1206		break;
1207	case IPCP_CONF_ACK:
1208		if (h->ident != sp->ipcp.confid)
1209			break;
1210		UNTIMO (sp);
1211		switch (sp->ipcp.state) {
1212		case IPCP_STATE_CLOSED:
1213			sp->ipcp.state = IPCP_STATE_ACK_RCVD;
1214			TIMO (sp, 5);
1215			break;
1216		case IPCP_STATE_ACK_SENT:
1217			sp->ipcp.state = IPCP_STATE_OPENED;
1218			break;
1219		}
1220		break;
1221	case IPCP_CONF_NAK:
1222	case IPCP_CONF_REJ:
1223		if (h->ident != sp->ipcp.confid)
1224			break;
1225		UNTIMO (sp);
1226			/* Initiate renegotiation. */
1227			sppp_ipcp_open (sp);
1228		if (sp->ipcp.state != IPCP_STATE_ACK_SENT)
1229			/* Go to closed state. */
1230			sp->ipcp.state = IPCP_STATE_CLOSED;
1231		break;
1232	case IPCP_TERM_REQ:
1233		/* Send Terminate-Ack packet. */
1234		sppp_cp_send (sp, PPP_IPCP, IPCP_TERM_ACK, h->ident, 0, 0);
1235		/* Go to closed state. */
1236		sp->ipcp.state = IPCP_STATE_CLOSED;
1237			/* Initiate renegotiation. */
1238			sppp_ipcp_open (sp);
1239		break;
1240	case IPCP_TERM_ACK:
1241		/* Ignore for now. */
1242	case IPCP_CODE_REJ:
1243		/* Ignore for now. */
1244		break;
1245	}
1246}
1247
1248static void
1249sppp_lcp_open (struct sppp *sp)
1250{
1251	char opt[6];
1252
1253	if (! sp->lcp.magic)
1254	sp->lcp.magic = time.tv_sec + time.tv_usec;
1255	opt[0] = LCP_OPT_MAGIC;
1256	opt[1] = sizeof (opt);
1257	opt[2] = sp->lcp.magic >> 24;
1258	opt[3] = sp->lcp.magic >> 16;
1259	opt[4] = sp->lcp.magic >> 8;
1260	opt[5] = sp->lcp.magic;
1261	sp->lcp.confid = ++sp->pp_seq;
1262	sppp_cp_send (sp, PPP_LCP, LCP_CONF_REQ, sp->lcp.confid,
1263		sizeof (opt), &opt);
1264	TIMO (sp, 2);
1265}
1266
1267static void
1268sppp_ipcp_open (struct sppp *sp)
1269{
1270	sp->ipcp.confid = ++sp->pp_seq;
1271	sppp_cp_send (sp, PPP_IPCP, IPCP_CONF_REQ, sp->ipcp.confid, 0, 0);
1272	TIMO (sp, 2);
1273}
1274
1275/*
1276 * Process PPP control protocol timeouts.
1277 */
1278static void
1279sppp_cp_timeout (void *arg)
1280{
1281	struct sppp *sp = (struct sppp*) arg;
1282	int s = splimp ();
1283
1284	sp->pp_flags &= ~PP_TIMO;
1285	if (! (sp->pp_if.if_flags & IFF_RUNNING) || (sp->pp_flags & PP_CISCO)) {
1286		splx (s);
1287		return;
1288	}
1289	switch (sp->lcp.state) {
1290	case LCP_STATE_CLOSED:
1291		/* No ACK for Configure-Request, retry. */
1292		sppp_lcp_open (sp);
1293		break;
1294	case LCP_STATE_ACK_RCVD:
1295		/* ACK got, but no Configure-Request for peer, retry. */
1296		sppp_lcp_open (sp);
1297		sp->lcp.state = LCP_STATE_CLOSED;
1298		break;
1299	case LCP_STATE_ACK_SENT:
1300		/* ACK sent but no ACK for Configure-Request, retry. */
1301		sppp_lcp_open (sp);
1302		break;
1303	case LCP_STATE_OPENED:
1304		/* LCP is already OK, try IPCP. */
1305		switch (sp->ipcp.state) {
1306		case IPCP_STATE_CLOSED:
1307			/* No ACK for Configure-Request, retry. */
1308			sppp_ipcp_open (sp);
1309			break;
1310		case IPCP_STATE_ACK_RCVD:
1311			/* ACK got, but no Configure-Request for peer, retry. */
1312			sppp_ipcp_open (sp);
1313			sp->ipcp.state = IPCP_STATE_CLOSED;
1314			break;
1315		case IPCP_STATE_ACK_SENT:
1316			/* ACK sent but no ACK for Configure-Request, retry. */
1317			sppp_ipcp_open (sp);
1318			break;
1319		case IPCP_STATE_OPENED:
1320			/* IPCP is OK. */
1321			break;
1322		}
1323		break;
1324	}
1325	splx (s);
1326}
1327
1328static char
1329*sppp_lcp_type_name (u_char type)
1330{
1331	static char buf [8];
1332	switch (type) {
1333	case LCP_CONF_REQ:   return ("conf-req");
1334	case LCP_CONF_ACK:   return ("conf-ack");
1335	case LCP_CONF_NAK:   return ("conf-nack");
1336	case LCP_CONF_REJ:   return ("conf-rej");
1337	case LCP_TERM_REQ:   return ("term-req");
1338	case LCP_TERM_ACK:   return ("term-ack");
1339	case LCP_CODE_REJ:   return ("code-rej");
1340	case LCP_PROTO_REJ:  return ("proto-rej");
1341	case LCP_ECHO_REQ:   return ("echo-req");
1342	case LCP_ECHO_REPLY: return ("echo-reply");
1343	case LCP_DISC_REQ:   return ("discard-req");
1344	}
1345	sprintf (buf, "%xh", type);
1346	return (buf);
1347}
1348
1349static char
1350*sppp_ipcp_type_name (u_char type)
1351{
1352	static char buf [8];
1353	switch (type) {
1354	case IPCP_CONF_REQ:   return ("conf-req");
1355	case IPCP_CONF_ACK:   return ("conf-ack");
1356	case IPCP_CONF_NAK:   return ("conf-nack");
1357	case IPCP_CONF_REJ:   return ("conf-rej");
1358	case IPCP_TERM_REQ:   return ("term-req");
1359	case IPCP_TERM_ACK:   return ("term-ack");
1360	case IPCP_CODE_REJ:   return ("code-rej");
1361	}
1362	sprintf (buf, "%xh", type);
1363	return (buf);
1364}
1365
1366static void
1367sppp_print_bytes (u_char *p, u_short len)
1368{
1369	printf (" %x", *p++);
1370	while (--len > 0)
1371		printf ("-%x", *p++);
1372}
1373