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