if_spppsubr.c revision 20407
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.13 1996/08/30 16:44:36 jhay 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_addrhead.tqh_first; ifa;
902		     ifa=ifa->ifa_link.tqe_next)
903			if (ifa->ifa_addr->sa_family == AF_INET)
904				break;
905		if (! ifa) {
906			if (ifp->if_flags & IFF_DEBUG)
907				printf ("%s%d: unknown address for cisco request\n",
908					ifp->if_name, ifp->if_unit);
909			return;
910		}
911		sppp_cisco_send (sp, CISCO_ADDR_REPLY,
912			ntohl (((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr),
913			ntohl (((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr.s_addr));
914		break;
915	}
916}
917
918/*
919 * Send PPP LCP packet.
920 */
921static void
922sppp_cp_send (struct sppp *sp, u_short proto, u_char type,
923	u_char ident, u_short len, void *data)
924{
925	struct ppp_header *h;
926	struct lcp_header *lh;
927	struct mbuf *m;
928	struct ifnet *ifp = &sp->pp_if;
929
930	if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
931		len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
932	MGETHDR (m, M_DONTWAIT, MT_DATA);
933	if (! m)
934		return;
935	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
936	m->m_pkthdr.rcvif = 0;
937
938	h = mtod (m, struct ppp_header*);
939	h->address = PPP_ALLSTATIONS;        /* broadcast address */
940	h->control = PPP_UI;                 /* Unnumbered Info */
941	h->protocol = htons (proto);         /* Link Control Protocol */
942
943	lh = (struct lcp_header*) (h + 1);
944	lh->type = type;
945	lh->ident = ident;
946	lh->len = htons (LCP_HEADER_LEN + len);
947	if (len)
948		bcopy (data, lh+1, len);
949
950	if (ifp->if_flags & IFF_DEBUG) {
951		printf ("%s%d: %s output <%s id=%xh len=%xh",
952			ifp->if_name, ifp->if_unit,
953			proto==PPP_LCP ? "lcp" : "ipcp",
954			proto==PPP_LCP ? sppp_lcp_type_name (lh->type) :
955			sppp_ipcp_type_name (lh->type), lh->ident,
956			ntohs (lh->len));
957		if (len)
958			sppp_print_bytes ((u_char*) (lh+1), len);
959		printf (">\n");
960	}
961	if (IF_QFULL (&sp->pp_fastq)) {
962		IF_DROP (&ifp->if_snd);
963		m_freem (m);
964	} else
965		IF_ENQUEUE (&sp->pp_fastq, m);
966	if (! (ifp->if_flags & IFF_OACTIVE))
967		(*ifp->if_start) (ifp);
968	ifp->if_obytes += m->m_pkthdr.len + 3;
969}
970
971/*
972 * Send Cisco keepalive packet.
973 */
974static void
975sppp_cisco_send (struct sppp *sp, int type, long par1, long par2)
976{
977	struct ppp_header *h;
978	struct cisco_packet *ch;
979	struct mbuf *m;
980	struct ifnet *ifp = &sp->pp_if;
981	u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
982
983	MGETHDR (m, M_DONTWAIT, MT_DATA);
984	if (! m)
985		return;
986	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
987	m->m_pkthdr.rcvif = 0;
988
989	h = mtod (m, struct ppp_header*);
990	h->address = CISCO_MULTICAST;
991	h->control = 0;
992	h->protocol = htons (CISCO_KEEPALIVE);
993
994	ch = (struct cisco_packet*) (h + 1);
995	ch->type = htonl (type);
996	ch->par1 = htonl (par1);
997	ch->par2 = htonl (par2);
998	ch->rel = -1;
999	ch->time0 = htons ((u_short) (t >> 16));
1000	ch->time1 = htons ((u_short) t);
1001
1002	if (ifp->if_flags & IFF_DEBUG)
1003		printf ("%s%d: cisco output: <%lxh %lxh %lxh %xh %xh-%xh>\n",
1004			ifp->if_name, ifp->if_unit, ntohl (ch->type), ch->par1,
1005			ch->par2, ch->rel, ch->time0, ch->time1);
1006
1007	if (IF_QFULL (&sp->pp_fastq)) {
1008		IF_DROP (&ifp->if_snd);
1009		m_freem (m);
1010	} else
1011		IF_ENQUEUE (&sp->pp_fastq, m);
1012	if (! (ifp->if_flags & IFF_OACTIVE))
1013		(*ifp->if_start) (ifp);
1014	ifp->if_obytes += m->m_pkthdr.len + 3;
1015}
1016
1017/*
1018 * Process an ioctl request.  Called on low priority level.
1019 */
1020int
1021sppp_ioctl (struct ifnet *ifp, int cmd, void *data)
1022{
1023	struct ifreq *ifr = (struct ifreq*) data;
1024	struct sppp *sp = (struct sppp*) ifp;
1025	int s, going_up, going_down;
1026
1027	switch (cmd) {
1028	default:
1029		return (EINVAL);
1030
1031	case SIOCAIFADDR:
1032	case SIOCSIFDSTADDR:
1033		break;
1034
1035	case SIOCSIFADDR:
1036		ifp->if_flags |= IFF_UP;
1037		/* fall through... */
1038
1039	case SIOCSIFFLAGS:
1040		s = splimp ();
1041		going_up   = (ifp->if_flags & IFF_UP) &&
1042			     ! (ifp->if_flags & IFF_RUNNING);
1043		going_down = ! (ifp->if_flags & IFF_UP) &&
1044			      (ifp->if_flags & IFF_RUNNING);
1045		if (going_up || going_down) {
1046			/* Shut down the PPP link. */
1047			ifp->if_flags &= ~IFF_RUNNING;
1048			sp->lcp.state = LCP_STATE_CLOSED;
1049			sp->ipcp.state = IPCP_STATE_CLOSED;
1050			UNTIMO (sp);
1051		}
1052		if (going_up) {
1053			/* Interface is starting -- initiate negotiation. */
1054			ifp->if_flags |= IFF_RUNNING;
1055			if (!(sp->pp_flags & PP_CISCO))
1056				sppp_lcp_open (sp);
1057		}
1058		splx (s);
1059		break;
1060
1061#ifdef SIOCSIFMTU
1062#ifndef ifr_mtu
1063#define ifr_mtu ifr_metric
1064#endif
1065	case SIOCSIFMTU:
1066		if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > PP_MTU)
1067			return (EINVAL);
1068		ifp->if_mtu = ifr->ifr_mtu;
1069		break;
1070#endif
1071#ifdef SLIOCSETMTU
1072	case SLIOCSETMTU:
1073		if (*(short*)data < 128 || *(short*)data > PP_MTU)
1074			return (EINVAL);
1075		ifp->if_mtu = *(short*)data;
1076		break;
1077#endif
1078#ifdef SIOCGIFMTU
1079	case SIOCGIFMTU:
1080		ifr->ifr_mtu = ifp->if_mtu;
1081		break;
1082#endif
1083#ifdef SLIOCGETMTU
1084	case SLIOCGETMTU:
1085		*(short*)data = ifp->if_mtu;
1086		break;
1087#endif
1088	case SIOCADDMULTI:
1089	case SIOCDELMULTI:
1090		break;
1091	}
1092	return (0);
1093}
1094
1095/*
1096 * Analyze the LCP Configure-Request options list
1097 * for the presence of unknown options.
1098 * If the request contains unknown options, build and
1099 * send Configure-reject packet, containing only unknown options.
1100 */
1101static int
1102sppp_lcp_conf_parse_options (struct sppp *sp, struct lcp_header *h,
1103	int len, u_long *magic)
1104{
1105	u_char *buf, *r, *p;
1106	int rlen;
1107
1108	len -= 4;
1109	buf = r = malloc (len, M_TEMP, M_NOWAIT);
1110	if (! buf)
1111		return (0);
1112
1113	p = (void*) (h+1);
1114	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1115		switch (*p) {
1116		case LCP_OPT_MAGIC:
1117			/* Magic number -- extract. */
1118			if (len >= 6 && p[1] == 6) {
1119				*magic = (u_long)p[2] << 24 |
1120					(u_long)p[3] << 16 | p[4] << 8 | p[5];
1121				continue;
1122			}
1123			break;
1124		case LCP_OPT_ASYNC_MAP:
1125			/* Async control character map -- check to be zero. */
1126			if (len >= 6 && p[1] == 6 && ! p[2] && ! p[3] &&
1127			    ! p[4] && ! p[5])
1128				continue;
1129			break;
1130		case LCP_OPT_MRU:
1131			/* Maximum receive unit -- always OK. */
1132			continue;
1133		default:
1134			/* Others not supported. */
1135			break;
1136		}
1137		/* Add the option to rejected list. */
1138			bcopy (p, r, p[1]);
1139			r += p[1];
1140		rlen += p[1];
1141	}
1142	if (rlen)
1143	sppp_cp_send (sp, PPP_LCP, LCP_CONF_REJ, h->ident, rlen, buf);
1144	free (buf, M_TEMP);
1145	return (rlen == 0);
1146}
1147
1148static void
1149sppp_ipcp_input (struct sppp *sp, struct mbuf *m)
1150{
1151	struct lcp_header *h;
1152	struct ifnet *ifp = &sp->pp_if;
1153	int len = m->m_pkthdr.len;
1154
1155	if (len < 4) {
1156		/* if (ifp->if_flags & IFF_DEBUG) */
1157			printf ("%s%d: invalid ipcp packet length: %d bytes\n",
1158				ifp->if_name, ifp->if_unit, len);
1159		return;
1160	}
1161	h = mtod (m, struct lcp_header*);
1162	if (ifp->if_flags & IFF_DEBUG) {
1163		printf ("%s%d: ipcp input: %d bytes <%s id=%xh len=%xh",
1164			ifp->if_name, ifp->if_unit, len,
1165			sppp_ipcp_type_name (h->type), h->ident, ntohs (h->len));
1166		if (len > 4)
1167			sppp_print_bytes ((u_char*) (h+1), len-4);
1168		printf (">\n");
1169	}
1170	if (len > ntohs (h->len))
1171		len = ntohs (h->len);
1172	switch (h->type) {
1173	default:
1174		/* Unknown packet type -- send Code-Reject packet. */
1175		sppp_cp_send (sp, PPP_IPCP, IPCP_CODE_REJ, ++sp->pp_seq, len, h);
1176		break;
1177	case IPCP_CONF_REQ:
1178		if (len < 4) {
1179			if (ifp->if_flags & IFF_DEBUG)
1180				printf ("%s%d: invalid ipcp configure request packet length: %d bytes\n",
1181					ifp->if_name, ifp->if_unit, len);
1182			return;
1183		}
1184		if (len > 4) {
1185			sppp_cp_send (sp, PPP_IPCP, LCP_CONF_REJ, h->ident,
1186				len-4, h+1);
1187
1188			switch (sp->ipcp.state) {
1189			case IPCP_STATE_OPENED:
1190				/* Initiate renegotiation. */
1191				sppp_ipcp_open (sp);
1192				/* fall through... */
1193			case IPCP_STATE_ACK_SENT:
1194				/* Go to closed state. */
1195				sp->ipcp.state = IPCP_STATE_CLOSED;
1196			}
1197		} else {
1198			/* Send Configure-Ack packet. */
1199			sppp_cp_send (sp, PPP_IPCP, IPCP_CONF_ACK, h->ident,
1200				0, 0);
1201			/* Change the state. */
1202			if (sp->ipcp.state == IPCP_STATE_ACK_RCVD)
1203				sp->ipcp.state = IPCP_STATE_OPENED;
1204			else
1205				sp->ipcp.state = IPCP_STATE_ACK_SENT;
1206		}
1207		break;
1208	case IPCP_CONF_ACK:
1209		if (h->ident != sp->ipcp.confid)
1210			break;
1211		UNTIMO (sp);
1212		switch (sp->ipcp.state) {
1213		case IPCP_STATE_CLOSED:
1214			sp->ipcp.state = IPCP_STATE_ACK_RCVD;
1215			TIMO (sp, 5);
1216			break;
1217		case IPCP_STATE_ACK_SENT:
1218			sp->ipcp.state = IPCP_STATE_OPENED;
1219			break;
1220		}
1221		break;
1222	case IPCP_CONF_NAK:
1223	case IPCP_CONF_REJ:
1224		if (h->ident != sp->ipcp.confid)
1225			break;
1226		UNTIMO (sp);
1227			/* Initiate renegotiation. */
1228			sppp_ipcp_open (sp);
1229		if (sp->ipcp.state != IPCP_STATE_ACK_SENT)
1230			/* Go to closed state. */
1231			sp->ipcp.state = IPCP_STATE_CLOSED;
1232		break;
1233	case IPCP_TERM_REQ:
1234		/* Send Terminate-Ack packet. */
1235		sppp_cp_send (sp, PPP_IPCP, IPCP_TERM_ACK, h->ident, 0, 0);
1236		/* Go to closed state. */
1237		sp->ipcp.state = IPCP_STATE_CLOSED;
1238			/* Initiate renegotiation. */
1239			sppp_ipcp_open (sp);
1240		break;
1241	case IPCP_TERM_ACK:
1242		/* Ignore for now. */
1243	case IPCP_CODE_REJ:
1244		/* Ignore for now. */
1245		break;
1246	}
1247}
1248
1249static void
1250sppp_lcp_open (struct sppp *sp)
1251{
1252	char opt[6];
1253
1254	if (! sp->lcp.magic)
1255	sp->lcp.magic = time.tv_sec + time.tv_usec;
1256	opt[0] = LCP_OPT_MAGIC;
1257	opt[1] = sizeof (opt);
1258	opt[2] = sp->lcp.magic >> 24;
1259	opt[3] = sp->lcp.magic >> 16;
1260	opt[4] = sp->lcp.magic >> 8;
1261	opt[5] = sp->lcp.magic;
1262	sp->lcp.confid = ++sp->pp_seq;
1263	sppp_cp_send (sp, PPP_LCP, LCP_CONF_REQ, sp->lcp.confid,
1264		sizeof (opt), &opt);
1265	TIMO (sp, 2);
1266}
1267
1268static void
1269sppp_ipcp_open (struct sppp *sp)
1270{
1271	sp->ipcp.confid = ++sp->pp_seq;
1272	sppp_cp_send (sp, PPP_IPCP, IPCP_CONF_REQ, sp->ipcp.confid, 0, 0);
1273	TIMO (sp, 2);
1274}
1275
1276/*
1277 * Process PPP control protocol timeouts.
1278 */
1279static void
1280sppp_cp_timeout (void *arg)
1281{
1282	struct sppp *sp = (struct sppp*) arg;
1283	int s = splimp ();
1284
1285	sp->pp_flags &= ~PP_TIMO;
1286	if (! (sp->pp_if.if_flags & IFF_RUNNING) || (sp->pp_flags & PP_CISCO)) {
1287		splx (s);
1288		return;
1289	}
1290	switch (sp->lcp.state) {
1291	case LCP_STATE_CLOSED:
1292		/* No ACK for Configure-Request, retry. */
1293		sppp_lcp_open (sp);
1294		break;
1295	case LCP_STATE_ACK_RCVD:
1296		/* ACK got, but no Configure-Request for peer, retry. */
1297		sppp_lcp_open (sp);
1298		sp->lcp.state = LCP_STATE_CLOSED;
1299		break;
1300	case LCP_STATE_ACK_SENT:
1301		/* ACK sent but no ACK for Configure-Request, retry. */
1302		sppp_lcp_open (sp);
1303		break;
1304	case LCP_STATE_OPENED:
1305		/* LCP is already OK, try IPCP. */
1306		switch (sp->ipcp.state) {
1307		case IPCP_STATE_CLOSED:
1308			/* No ACK for Configure-Request, retry. */
1309			sppp_ipcp_open (sp);
1310			break;
1311		case IPCP_STATE_ACK_RCVD:
1312			/* ACK got, but no Configure-Request for peer, retry. */
1313			sppp_ipcp_open (sp);
1314			sp->ipcp.state = IPCP_STATE_CLOSED;
1315			break;
1316		case IPCP_STATE_ACK_SENT:
1317			/* ACK sent but no ACK for Configure-Request, retry. */
1318			sppp_ipcp_open (sp);
1319			break;
1320		case IPCP_STATE_OPENED:
1321			/* IPCP is OK. */
1322			break;
1323		}
1324		break;
1325	}
1326	splx (s);
1327}
1328
1329static char
1330*sppp_lcp_type_name (u_char type)
1331{
1332	static char buf [8];
1333	switch (type) {
1334	case LCP_CONF_REQ:   return ("conf-req");
1335	case LCP_CONF_ACK:   return ("conf-ack");
1336	case LCP_CONF_NAK:   return ("conf-nack");
1337	case LCP_CONF_REJ:   return ("conf-rej");
1338	case LCP_TERM_REQ:   return ("term-req");
1339	case LCP_TERM_ACK:   return ("term-ack");
1340	case LCP_CODE_REJ:   return ("code-rej");
1341	case LCP_PROTO_REJ:  return ("proto-rej");
1342	case LCP_ECHO_REQ:   return ("echo-req");
1343	case LCP_ECHO_REPLY: return ("echo-reply");
1344	case LCP_DISC_REQ:   return ("discard-req");
1345	}
1346	sprintf (buf, "%xh", type);
1347	return (buf);
1348}
1349
1350static char
1351*sppp_ipcp_type_name (u_char type)
1352{
1353	static char buf [8];
1354	switch (type) {
1355	case IPCP_CONF_REQ:   return ("conf-req");
1356	case IPCP_CONF_ACK:   return ("conf-ack");
1357	case IPCP_CONF_NAK:   return ("conf-nack");
1358	case IPCP_CONF_REJ:   return ("conf-rej");
1359	case IPCP_TERM_REQ:   return ("term-req");
1360	case IPCP_TERM_ACK:   return ("term-ack");
1361	case IPCP_CODE_REJ:   return ("code-rej");
1362	}
1363	sprintf (buf, "%xh", type);
1364	return (buf);
1365}
1366
1367static void
1368sppp_print_bytes (u_char *p, u_short len)
1369{
1370	printf (" %x", *p++);
1371	while (--len > 0)
1372		printf ("-%x", *p++);
1373}
1374