if_spppsubr.c revision 17949
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 * $Id: if_spppsubr.c,v 1.12 1996/06/10 23:17:45 gpalmer Exp $
18 */
19#undef DEBUG
20
21#include <sys/param.h>
22#include <sys/systm.h>
23#include <sys/kernel.h>
24#include <sys/ioctl.h>
25#include <sys/socket.h>
26#include <sys/mbuf.h>
27
28#include <net/if.h>
29#include <net/netisr.h>
30#include <net/if_types.h>
31
32#ifdef INET
33#include <netinet/in.h>
34#include <netinet/in_systm.h>
35#include <netinet/in_var.h>
36#include <netinet/ip.h>
37#include <netinet/tcp.h>
38#include <netinet/if_ether.h>
39#endif
40
41#ifdef IPX
42#include <netipx/ipx.h>
43#include <netipx/ipx_if.h>
44#endif
45
46#ifdef NS
47#include <netns/ns.h>
48#include <netns/ns_if.h>
49#endif
50
51#ifdef ISO
52#include <netiso/argo_debug.h>
53#include <netiso/iso.h>
54#include <netiso/iso_var.h>
55#include <netiso/iso_snpac.h>
56#endif
57
58#include <net/if_sppp.h>
59
60#ifdef DEBUG
61#define print(s)        printf s
62#else
63#define print(s)        {/*void*/}
64#endif
65
66#define MAXALIVECNT     3               /* max. alive packets */
67
68#define PPP_ALLSTATIONS 0xff            /* All-Stations broadcast address */
69#define PPP_UI          0x03            /* Unnumbered Information */
70#define PPP_IP          0x0021          /* Internet Protocol */
71#define PPP_ISO         0x0023          /* ISO OSI Protocol */
72#define PPP_XNS         0x0025          /* Xerox NS Protocol */
73#define PPP_IPX         0x002b          /* Novell IPX Protocol */
74#define PPP_LCP         0xc021          /* Link Control Protocol */
75#define PPP_IPCP        0x8021          /* Internet Protocol Control Protocol */
76
77#define LCP_CONF_REQ    1               /* PPP LCP configure request */
78#define LCP_CONF_ACK    2               /* PPP LCP configure acknowledge */
79#define LCP_CONF_NAK    3               /* PPP LCP configure negative ack */
80#define LCP_CONF_REJ    4               /* PPP LCP configure reject */
81#define LCP_TERM_REQ    5               /* PPP LCP terminate request */
82#define LCP_TERM_ACK    6               /* PPP LCP terminate acknowledge */
83#define LCP_CODE_REJ    7               /* PPP LCP code reject */
84#define LCP_PROTO_REJ   8               /* PPP LCP protocol reject */
85#define LCP_ECHO_REQ    9               /* PPP LCP echo request */
86#define LCP_ECHO_REPLY  10              /* PPP LCP echo reply */
87#define LCP_DISC_REQ    11              /* PPP LCP discard request */
88
89#define LCP_OPT_MRU             1       /* maximum receive unit */
90#define LCP_OPT_ASYNC_MAP       2       /* async control character map */
91#define LCP_OPT_AUTH_PROTO      3       /* authentication protocol */
92#define LCP_OPT_QUAL_PROTO      4       /* quality protocol */
93#define LCP_OPT_MAGIC           5       /* magic number */
94#define LCP_OPT_RESERVED        6       /* reserved */
95#define LCP_OPT_PROTO_COMP      7       /* protocol field compression */
96#define LCP_OPT_ADDR_COMP       8       /* address/control field compression */
97
98#define IPCP_CONF_REQ   LCP_CONF_REQ    /* PPP IPCP configure request */
99#define IPCP_CONF_ACK   LCP_CONF_ACK    /* PPP IPCP configure acknowledge */
100#define IPCP_CONF_NAK   LCP_CONF_NAK    /* PPP IPCP configure negative ack */
101#define IPCP_CONF_REJ   LCP_CONF_REJ    /* PPP IPCP configure reject */
102#define IPCP_TERM_REQ   LCP_TERM_REQ    /* PPP IPCP terminate request */
103#define IPCP_TERM_ACK   LCP_TERM_ACK    /* PPP IPCP terminate acknowledge */
104#define IPCP_CODE_REJ   LCP_CODE_REJ    /* PPP IPCP code reject */
105
106#define CISCO_MULTICAST         0x8f    /* Cisco multicast address */
107#define CISCO_UNICAST           0x0f    /* Cisco unicast address */
108#define CISCO_KEEPALIVE         0x8035  /* Cisco keepalive protocol */
109#define CISCO_ADDR_REQ          0       /* Cisco address request */
110#define CISCO_ADDR_REPLY        1       /* Cisco address reply */
111#define CISCO_KEEPALIVE_REQ     2       /* Cisco keepalive request */
112
113struct ppp_header {
114	u_char address;
115	u_char control;
116	u_short protocol;
117};
118#define PPP_HEADER_LEN          sizeof (struct ppp_header)
119
120struct lcp_header {
121	u_char type;
122	u_char ident;
123	u_short len;
124};
125#define LCP_HEADER_LEN          sizeof (struct lcp_header)
126
127struct cisco_packet {
128	u_long type;
129	u_long par1;
130	u_long par2;
131	u_short rel;
132	u_short time0;
133	u_short time1;
134};
135#define CISCO_PACKET_LEN 18
136
137static struct sppp *spppq;
138
139/*
140 * The following disgusting hack gets around the problem that IP TOS
141 * can't be set yet.  We want to put "interactive" traffic on a high
142 * priority queue.  To decide if traffic is interactive, we check that
143 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
144 */
145static u_short interactive_ports[8] = {
146	0,	513,	0,	0,
147	0,	21,	0,	23,
148};
149#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
150
151/*
152 * Timeout routine activation macros.
153 */
154#define TIMO(p,s) if (! ((p)->pp_flags & PP_TIMO)) { \
155			timeout (sppp_cp_timeout, (void*) (p), (s)*hz); \
156			(p)->pp_flags |= PP_TIMO; }
157#define UNTIMO(p) if ((p)->pp_flags & PP_TIMO) { \
158			untimeout (sppp_cp_timeout, (void*) (p)); \
159			(p)->pp_flags &= ~PP_TIMO; }
160
161static void sppp_keepalive (void *dummy);
162static void sppp_cp_send (struct sppp *sp, u_short proto, u_char type,
163	u_char ident, u_short len, void *data);
164static void sppp_cisco_send (struct sppp *sp, int type, long par1, long par2);
165static void sppp_lcp_input (struct sppp *sp, struct mbuf *m);
166static void sppp_cisco_input (struct sppp *sp, struct mbuf *m);
167static void sppp_ipcp_input (struct sppp *sp, struct mbuf *m);
168static void sppp_lcp_open (struct sppp *sp);
169static void sppp_ipcp_open (struct sppp *sp);
170static int sppp_lcp_conf_parse_options (struct sppp *sp, struct lcp_header *h,
171	int len, u_long *magic);
172static void sppp_cp_timeout (void *arg);
173static char *sppp_lcp_type_name (u_char type);
174static char *sppp_ipcp_type_name (u_char type);
175static void sppp_print_bytes (u_char *p, u_short len);
176static int sppp_output (struct ifnet *ifp, struct mbuf *m,
177	struct sockaddr *dst, struct rtentry *rt);
178
179/*
180 * Flush interface queue.
181 */
182static void qflush (struct ifqueue *ifq)
183{
184	struct mbuf *m, *n;
185
186	n = ifq->ifq_head;
187	while ((m = n)) {
188		n = m->m_act;
189		m_freem (m);
190	}
191	ifq->ifq_head = 0;
192	ifq->ifq_tail = 0;
193	ifq->ifq_len = 0;
194}
195
196/*
197 * Process the received packet.
198 */
199void sppp_input (struct ifnet *ifp, struct mbuf *m)
200{
201	struct ppp_header *h;
202	struct sppp *sp = (struct sppp*) ifp;
203	struct ifqueue *inq = 0;
204	int s;
205
206	if (ifp->if_flags & IFF_UP)
207		/* Count received bytes, add FCS and one flag */
208		ifp->if_ibytes += m->m_pkthdr.len + 3;
209
210	if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
211		/* Too small packet, drop it. */
212		if (ifp->if_flags & IFF_DEBUG)
213			printf ("%s%d: input packet is too small, %d bytes\n",
214				ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
215drop:           ++ifp->if_iqdrops;
216		m_freem (m);
217		return;
218	}
219
220	/* Get PPP header. */
221	h = mtod (m, struct ppp_header*);
222	m_adj (m, PPP_HEADER_LEN);
223
224	switch (h->address) {
225	default:        /* Invalid PPP packet. */
226invalid:        if (ifp->if_flags & IFF_DEBUG)
227			printf ("%s%d: invalid input packet <0x%x 0x%x 0x%x>\n",
228				ifp->if_name, ifp->if_unit,
229				h->address, h->control, ntohs (h->protocol));
230		goto drop;
231	case PPP_ALLSTATIONS:
232		if (h->control != PPP_UI)
233			goto invalid;
234		if (sp->pp_flags & PP_CISCO) {
235			if (ifp->if_flags & IFF_DEBUG)
236				printf ("%s%d: PPP packet in Cisco mode <0x%x 0x%x 0x%x>\n",
237					ifp->if_name, ifp->if_unit,
238					h->address, h->control, ntohs (h->protocol));
239			goto drop;
240		}
241		switch (ntohs (h->protocol)) {
242		default:
243			if (sp->lcp.state == LCP_STATE_OPENED)
244				sppp_cp_send (sp, PPP_LCP, LCP_PROTO_REJ,
245					++sp->pp_seq, m->m_pkthdr.len + 2,
246					&h->protocol);
247			if (ifp->if_flags & IFF_DEBUG)
248				printf ("%s%d: invalid input protocol <0x%x 0x%x 0x%x>\n",
249					ifp->if_name, ifp->if_unit,
250					h->address, h->control, ntohs (h->protocol));
251			++ifp->if_noproto;
252			goto drop;
253		case PPP_LCP:
254			sppp_lcp_input ((struct sppp*) ifp, m);
255			m_freem (m);
256			return;
257#ifdef INET
258		case PPP_IPCP:
259			if (sp->lcp.state == LCP_STATE_OPENED)
260				sppp_ipcp_input ((struct sppp*) ifp, m);
261			m_freem (m);
262			return;
263		case PPP_IP:
264			if (sp->ipcp.state == IPCP_STATE_OPENED) {
265				schednetisr (NETISR_IP);
266				inq = &ipintrq;
267			}
268			break;
269#endif
270#ifdef IPX
271		case PPP_IPX:
272			/* IPX IPXCP not implemented yet */
273			if (sp->lcp.state == LCP_STATE_OPENED) {
274				schednetisr (NETISR_IPX);
275				inq = &ipxintrq;
276			}
277			break;
278#endif
279#ifdef NS
280		case PPP_XNS:
281			/* XNS IDPCP not implemented yet */
282			if (sp->lcp.state == LCP_STATE_OPENED) {
283				schednetisr (NETISR_NS);
284				inq = &nsintrq;
285			}
286			break;
287#endif
288#ifdef ISO
289		case PPP_ISO:
290			/* OSI NLCP not implemented yet */
291			if (sp->lcp.state == LCP_STATE_OPENED) {
292				schednetisr (NETISR_ISO);
293				inq = &clnlintrq;
294			}
295			break;
296#endif
297		}
298		break;
299	case CISCO_MULTICAST:
300	case CISCO_UNICAST:
301		/* Don't check the control field here (RFC 1547). */
302		if (! (sp->pp_flags & PP_CISCO)) {
303			if (ifp->if_flags & IFF_DEBUG)
304				printf ("%s%d: Cisco packet in PPP mode <0x%x 0x%x 0x%x>\n",
305					ifp->if_name, ifp->if_unit,
306					h->address, h->control, ntohs (h->protocol));
307			goto drop;
308		}
309		switch (ntohs (h->protocol)) {
310		default:
311			++ifp->if_noproto;
312			goto invalid;
313		case CISCO_KEEPALIVE:
314			sppp_cisco_input ((struct sppp*) ifp, m);
315			m_freem (m);
316			return;
317#ifdef INET
318		case ETHERTYPE_IP:
319			schednetisr (NETISR_IP);
320			inq = &ipintrq;
321			break;
322#endif
323#ifdef IPX
324		case ETHERTYPE_IPX:
325			schednetisr (NETISR_IPX);
326			inq = &ipxintrq;
327			break;
328#endif
329#ifdef NS
330		case ETHERTYPE_NS:
331			schednetisr (NETISR_NS);
332			inq = &nsintrq;
333			break;
334#endif
335		}
336		break;
337	}
338
339	if (! (ifp->if_flags & IFF_UP) || ! inq)
340		goto drop;
341
342	/* Check queue. */
343	s = splimp ();
344	if (IF_QFULL (inq)) {
345		/* Queue overflow. */
346		IF_DROP (inq);
347		splx (s);
348		if (ifp->if_flags & IFF_DEBUG)
349			printf ("%s%d: protocol queue overflow\n",
350				ifp->if_name, ifp->if_unit);
351		goto drop;
352	}
353	IF_ENQUEUE (inq, m);
354	splx (s);
355}
356
357/*
358 * Enqueue transmit packet.
359 */
360static int
361sppp_output (struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, struct rtentry *rt)
362{
363	struct sppp *sp = (struct sppp*) ifp;
364	struct ppp_header *h;
365	struct ifqueue *ifq;
366	int s = splimp ();
367
368	if (! (ifp->if_flags & IFF_UP) || ! (ifp->if_flags & IFF_RUNNING)) {
369		m_freem (m);
370		splx (s);
371		return (ENETDOWN);
372	}
373
374	ifq = &ifp->if_snd;
375#ifdef INET
376	/*
377	 * Put low delay, telnet, rlogin and ftp control packets
378	 * in front of the queue.
379	 */
380	if (dst->sa_family == AF_INET) {
381		struct ip *ip = mtod (m, struct ip*);
382		struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
383
384		if (! IF_QFULL (&sp->pp_fastq) &&
385		    ((ip->ip_tos & IPTOS_LOWDELAY) ||
386	    	    ip->ip_p == IPPROTO_TCP &&
387	    	    m->m_len >= sizeof (struct ip) + sizeof (struct tcphdr) &&
388	    	    (INTERACTIVE (ntohs (tcp->th_sport)) ||
389	    	    INTERACTIVE (ntohs (tcp->th_dport)))))
390			ifq = &sp->pp_fastq;
391	}
392#endif
393
394	/*
395	 * Prepend general data packet PPP header. For now, IP only.
396	 */
397	M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
398	if (! m) {
399		if (ifp->if_flags & IFF_DEBUG)
400			printf ("%s%d: no memory for transmit header\n",
401				ifp->if_name, ifp->if_unit);
402		splx (s);
403		return (ENOBUFS);
404	}
405	h = mtod (m, struct ppp_header*);
406	if (sp->pp_flags & PP_CISCO) {
407		h->address = CISCO_MULTICAST;        /* broadcast address */
408		h->control = 0;
409	} else {
410		h->address = PPP_ALLSTATIONS;        /* broadcast address */
411		h->control = PPP_UI;                 /* Unnumbered Info */
412	}
413
414	switch (dst->sa_family) {
415#ifdef INET
416	case AF_INET:   /* Internet Protocol */
417		if (sp->pp_flags & PP_CISCO)
418			h->protocol = htons (ETHERTYPE_IP);
419		else if (sp->ipcp.state == IPCP_STATE_OPENED)
420			h->protocol = htons (PPP_IP);
421		else {
422			m_freem (m);
423			splx (s);
424			return (ENETDOWN);
425		}
426		break;
427#endif
428#ifdef NS
429	case AF_NS:     /* Xerox NS Protocol */
430		h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
431			ETHERTYPE_NS : PPP_XNS);
432		break;
433#endif
434#ifdef IPX
435	case AF_IPX:     /* Novell IPX Protocol */
436		h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
437			ETHERTYPE_IPX : PPP_IPX);
438		break;
439#endif
440#ifdef ISO
441	case AF_ISO:    /* ISO OSI Protocol */
442		if (sp->pp_flags & PP_CISCO)
443			goto nosupport;
444		h->protocol = htons (PPP_ISO);
445		break;
446nosupport:
447#endif
448	default:
449		m_freem (m);
450		splx (s);
451		return (EAFNOSUPPORT);
452	}
453
454	/*
455	 * Queue message on interface, and start output if interface
456	 * not yet active.
457	 */
458	if (IF_QFULL (ifq)) {
459		IF_DROP (&ifp->if_snd);
460		m_freem (m);
461		splx (s);
462		return (ENOBUFS);
463	}
464	IF_ENQUEUE (ifq, m);
465	if (! (ifp->if_flags & IFF_OACTIVE))
466		(*ifp->if_start) (ifp);
467
468	/*
469	 * Count output packets and bytes.
470	 * The packet length includes header, FCS and 1 flag,
471	 * according to RFC 1333.
472	 */
473	ifp->if_obytes += m->m_pkthdr.len + 3;
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	case SIOCADDMULTI:
1088	case SIOCDELMULTI:
1089		break;
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