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