if_spppsubr.c revision 11819
1109998Smarkm/*
2280304Sjkim * Synchronous PPP/Cisco link level subroutines.
3280304Sjkim * Keepalive protocol implemented in both Cisco and PPP modes.
4280304Sjkim *
5280304Sjkim * Copyright (C) 1994 Cronyx Ltd.
6109998Smarkm * Author: Serge Vakulenko, <vak@zebub.msk.su>
7109998Smarkm *
8109998Smarkm * This software is distributed with NO WARRANTIES, not even the implied
9109998Smarkm * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10109998Smarkm *
11109998Smarkm * Authors grant any other persons or organisations permission to use
12109998Smarkm * or modify this software as long as this message is kept with the software,
13109998Smarkm * all derivative works or modified versions.
14280304Sjkim *
15109998Smarkm * Version 1.9, Wed Oct  4 18:58:15 MSK 1995
16109998Smarkm */
17109998Smarkm#undef DEBUG
18109998Smarkm
19109998Smarkm#include <sys/param.h>
20109998Smarkm#include <sys/systm.h>
21109998Smarkm#include <sys/kernel.h>
22109998Smarkm#include <sys/ioctl.h>
23109998Smarkm#include <sys/socket.h>
24109998Smarkm#include <sys/mbuf.h>
25109998Smarkm
26109998Smarkm#include <net/if.h>
27109998Smarkm#include <net/netisr.h>
28109998Smarkm#include <net/if_types.h>
29109998Smarkm
30109998Smarkm#ifdef INET
31109998Smarkm#include <netinet/in.h>
32109998Smarkm#include <netinet/in_systm.h>
33109998Smarkm#include <netinet/in_var.h>
34109998Smarkm#include <netinet/ip.h>
35109998Smarkm#include <netinet/tcp.h>
36109998Smarkm#include <netinet/if_ether.h>
37109998Smarkm#endif
38109998Smarkm
39109998Smarkm#ifdef IPX
40109998Smarkm#include <netipx/ipx.h>
41109998Smarkm#include <netipx/ipx_if.h>
42109998Smarkm#endif
43109998Smarkm
44109998Smarkm#ifdef NS
45109998Smarkm#include <netns/ns.h>
46109998Smarkm#include <netns/ns_if.h>
47109998Smarkm#endif
48109998Smarkm
49109998Smarkm#ifdef ISO
50109998Smarkm#include <netiso/argo_debug.h>
51109998Smarkm#include <netiso/iso.h>
52109998Smarkm#include <netiso/iso_var.h>
53109998Smarkm#include <netiso/iso_snpac.h>
54109998Smarkm#endif
55109998Smarkm
56109998Smarkm#include <net/if_sppp.h>
57109998Smarkm
58109998Smarkm#ifdef DEBUG
59109998Smarkm#define print(s)        printf s
60109998Smarkm#else
61109998Smarkm#define print(s)        {/*void*/}
62109998Smarkm#endif
63109998Smarkm
64109998Smarkm#define MAXALIVECNT     3               /* max. alive packets */
65280304Sjkim
66280304Sjkim#define PPP_ALLSTATIONS 0xff            /* All-Stations broadcast address */
67280304Sjkim#define PPP_UI          0x03            /* Unnumbered Information */
68109998Smarkm#define PPP_IP          0x0021          /* Internet Protocol */
69109998Smarkm#define PPP_ISO         0x0023          /* ISO OSI Protocol */
70109998Smarkm#define PPP_XNS         0x0025          /* Xerox NS Protocol */
71109998Smarkm#define PPP_LCP         0xc021          /* Link Control Protocol */
72109998Smarkm#define PPP_IPCP        0x8021          /* Internet Protocol Control Protocol */
73109998Smarkm
74280304Sjkim#define LCP_CONF_REQ    1               /* PPP LCP configure request */
75280304Sjkim#define LCP_CONF_ACK    2               /* PPP LCP configure acknowledge */
76109998Smarkm#define LCP_CONF_NAK    3               /* PPP LCP configure negative ack */
77109998Smarkm#define LCP_CONF_REJ    4               /* PPP LCP configure reject */
78109998Smarkm#define LCP_TERM_REQ    5               /* PPP LCP terminate request */
79109998Smarkm#define LCP_TERM_ACK    6               /* PPP LCP terminate acknowledge */
80109998Smarkm#define LCP_CODE_REJ    7               /* PPP LCP code reject */
81109998Smarkm#define LCP_PROTO_REJ   8               /* PPP LCP protocol reject */
82280304Sjkim#define LCP_ECHO_REQ    9               /* PPP LCP echo request */
83280304Sjkim#define LCP_ECHO_REPLY  10              /* PPP LCP echo reply */
84280304Sjkim#define LCP_DISC_REQ    11              /* PPP LCP discard request */
85280304Sjkim
86109998Smarkm#define LCP_OPT_MRU             1       /* maximum receive unit */
87109998Smarkm#define LCP_OPT_ASYNC_MAP       2       /* async control character map */
88109998Smarkm#define LCP_OPT_AUTH_PROTO      3       /* authentication protocol */
89109998Smarkm#define LCP_OPT_QUAL_PROTO      4       /* quality protocol */
90109998Smarkm#define LCP_OPT_MAGIC           5       /* magic number */
91280304Sjkim#define LCP_OPT_RESERVED        6       /* reserved */
92280304Sjkim#define LCP_OPT_PROTO_COMP      7       /* protocol field compression */
93280304Sjkim#define LCP_OPT_ADDR_COMP       8       /* address/control field compression */
94109998Smarkm
95109998Smarkm#define IPCP_CONF_REQ   LCP_CONF_REQ    /* PPP IPCP configure request */
96109998Smarkm#define IPCP_CONF_ACK   LCP_CONF_ACK    /* PPP IPCP configure acknowledge */
97109998Smarkm#define IPCP_CONF_NAK   LCP_CONF_NAK    /* PPP IPCP configure negative ack */
98109998Smarkm#define IPCP_CONF_REJ   LCP_CONF_REJ    /* PPP IPCP configure reject */
99109998Smarkm#define IPCP_TERM_REQ   LCP_TERM_REQ    /* PPP IPCP terminate request */
100280304Sjkim#define IPCP_TERM_ACK   LCP_TERM_ACK    /* PPP IPCP terminate acknowledge */
101280304Sjkim#define IPCP_CODE_REJ   LCP_CODE_REJ    /* PPP IPCP code reject */
102280304Sjkim
103280304Sjkim#define CISCO_MULTICAST         0x8f    /* Cisco multicast address */
104280304Sjkim#define CISCO_UNICAST           0x0f    /* Cisco unicast address */
105109998Smarkm#define CISCO_KEEPALIVE         0x8035  /* Cisco keepalive protocol */
106109998Smarkm#define CISCO_ADDR_REQ          0       /* Cisco address request */
107109998Smarkm#define CISCO_ADDR_REPLY        1       /* Cisco address reply */
108109998Smarkm#define CISCO_KEEPALIVE_REQ     2       /* Cisco keepalive request */
109280304Sjkim
110280304Sjkimstruct ppp_header {
111280304Sjkim	u_char address;
112109998Smarkm	u_char control;
113109998Smarkm	u_short protocol;
114109998Smarkm};
115109998Smarkm#define PPP_HEADER_LEN          sizeof (struct ppp_header)
116280304Sjkim
117109998Smarkmstruct lcp_header {
118109998Smarkm	u_char type;
119280304Sjkim	u_char ident;
120280304Sjkim	u_short len;
121109998Smarkm};
122109998Smarkm#define LCP_HEADER_LEN          sizeof (struct lcp_header)
123109998Smarkm
124109998Smarkmstruct cisco_packet {
125109998Smarkm	u_long type;
126109998Smarkm	u_long par1;
127280304Sjkim	u_long par2;
128280304Sjkim	u_short rel;
129109998Smarkm	u_short time0;
130109998Smarkm	u_short time1;
131109998Smarkm};
132109998Smarkm#define CISCO_PACKET_LEN 18
133109998Smarkm
134109998Smarkmstruct sppp *spppq;
135280304Sjkim
136280304Sjkim/*
137109998Smarkm * The following disgusting hack gets around the problem that IP TOS
138109998Smarkm * can't be set yet.  We want to put "interactive" traffic on a high
139109998Smarkm * priority queue.  To decide if traffic is interactive, we check that
140109998Smarkm * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
141109998Smarkm */
142109998Smarkmstatic u_short interactive_ports[8] = {
143280304Sjkim	0,	513,	0,	0,
144280304Sjkim	0,	21,	0,	23,
145280304Sjkim};
146280304Sjkim#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
147280304Sjkim
148280304Sjkim/*
149280304Sjkim * Timeout routine activation macros.
150280304Sjkim */
151280304Sjkim#define TIMO(p,s) if (! ((p)->pp_flags & PP_TIMO)) { \
152280304Sjkim			timeout (sppp_cp_timeout, (void*) (p), (s)*hz); \
153109998Smarkm			(p)->pp_flags |= PP_TIMO; }
154109998Smarkm#define UNTIMO(p) if ((p)->pp_flags & PP_TIMO) { \
155109998Smarkm			untimeout (sppp_cp_timeout, (void*) (p)); \
156109998Smarkm			(p)->pp_flags &= ~PP_TIMO; }
157280304Sjkim
158280304Sjkimvoid sppp_keepalive (void *dummy);
159280304Sjkimvoid sppp_cp_send (struct sppp *sp, u_short proto, u_char type,
160109998Smarkm	u_char ident, u_short len, void *data);
161109998Smarkmvoid sppp_cisco_send (struct sppp *sp, int type, long par1, long par2);
162109998Smarkmvoid 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	{
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) && ((ip->ip_tos & IPTOS_LOWDELAY) ||
365	    ip->ip_p == IPPROTO_TCP &&
366	    m->m_len >= sizeof (struct ip) + sizeof (struct tcphdr) &&
367	    (INTERACTIVE (ntohs (tcp->th_sport)) ||
368	    INTERACTIVE (ntohs (tcp->th_dport)))))
369		ifq = &sp->pp_fastq;
370	}
371#endif
372
373	/*
374	 * Prepend general data packet PPP header. For now, IP only.
375	 */
376	M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
377	if (! m) {
378		if (ifp->if_flags & IFF_DEBUG)
379			printf ("%s%d: no memory for transmit header\n",
380				ifp->if_name, ifp->if_unit);
381		splx (s);
382		return (ENOBUFS);
383	}
384	h = mtod (m, struct ppp_header*);
385	if (sp->pp_flags & PP_CISCO) {
386		h->address = CISCO_MULTICAST;        /* broadcast address */
387		h->control = 0;
388	} else {
389		h->address = PPP_ALLSTATIONS;        /* broadcast address */
390		h->control = PPP_UI;                 /* Unnumbered Info */
391	}
392
393	switch (dst->sa_family) {
394#ifdef INET
395	case AF_INET:   /* Internet Protocol */
396		if (sp->pp_flags & PP_CISCO)
397			h->protocol = htons (ETHERTYPE_IP);
398		else if (sp->ipcp.state == IPCP_STATE_OPENED)
399			h->protocol = htons (PPP_IP);
400		else {
401			m_freem (m);
402			splx (s);
403			return (ENETDOWN);
404		}
405		break;
406#endif
407#ifdef NS
408	case AF_NS:     /* Xerox NS Protocol */
409		h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
410			ETHERTYPE_NS : PPP_XNS);
411		break;
412#endif
413#ifdef IPX
414	case AF_IPX:     /* Xerox NS Protocol */
415		h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
416			ETHERTYPE_IPX : PPP_XNS);
417		break;
418#endif
419#ifdef ISO
420	case AF_ISO:    /* ISO OSI Protocol */
421		if (sp->pp_flags & PP_CISCO)
422			goto nosupport;
423		h->protocol = htons (PPP_ISO);
424		break;
425#endif
426nosupport:
427	default:
428		m_freem (m);
429		splx (s);
430		return (EAFNOSUPPORT);
431	}
432
433	/*
434	 * Queue message on interface, and start output if interface
435	 * not yet active.
436	 */
437	if (IF_QFULL (ifq)) {
438		IF_DROP (&ifp->if_snd);
439		m_freem (m);
440		splx (s);
441		return (ENOBUFS);
442	}
443	IF_ENQUEUE (ifq, m);
444	if (! (ifp->if_flags & IFF_OACTIVE))
445		(*ifp->if_start) (ifp);
446
447	/*
448	 * Count output packets and bytes.
449	 * The packet length includes header, FCS and 1 flag,
450	 * according to RFC 1333.
451	 */
452	ifp->if_obytes += m->m_pkthdr.len + 3;
453	ifp->if_lastchange = time;
454	splx (s);
455	return (0);
456}
457
458void sppp_attach (struct ifnet *ifp)
459{
460	struct sppp *sp = (struct sppp*) ifp;
461
462	/* Initialize keepalive handler. */
463	if (! spppq)
464		timeout (sppp_keepalive, 0, hz * 10);
465
466	/* Insert new entry into the keepalive list. */
467	sp->pp_next = spppq;
468	spppq = sp;
469
470	sp->pp_if.if_type = IFT_PPP;
471	sp->pp_if.if_output = sppp_output;
472	sp->pp_fastq.ifq_maxlen = 32;
473	sp->pp_loopcnt = 0;
474	sp->pp_alivecnt = 0;
475	sp->pp_seq = 0;
476	sp->pp_rseq = 0;
477	sp->lcp.magic = 0;
478	sp->lcp.state = LCP_STATE_CLOSED;
479	sp->ipcp.state = IPCP_STATE_CLOSED;
480}
481
482void sppp_detach (struct ifnet *ifp)
483{
484	struct sppp **q, *p, *sp = (struct sppp*) ifp;
485
486	/* Remove the entry from the keepalive list. */
487	for (q = &spppq; (p = *q); q = &p->pp_next)
488		if (p == sp) {
489			*q = p->pp_next;
490			break;
491		}
492
493	/* Stop keepalive handler. */
494	if (! spppq)
495		untimeout (sppp_keepalive, 0);
496	UNTIMO (sp);
497}
498
499/*
500 * Flush the interface output queue.
501 */
502void sppp_flush (struct ifnet *ifp)
503{
504	struct sppp *sp = (struct sppp*) ifp;
505
506	qflush (&sp->pp_if.if_snd);
507	qflush (&sp->pp_fastq);
508}
509
510/*
511 * Check if the output queue is empty.
512 */
513int sppp_isempty (struct ifnet *ifp)
514{
515	struct sppp *sp = (struct sppp*) ifp;
516	int empty, s = splimp ();
517
518	empty = !sp->pp_fastq.ifq_head && !sp->pp_if.if_snd.ifq_head;
519	splx (s);
520	return (empty);
521}
522
523/*
524 * Get next packet to send.
525 */
526struct mbuf *sppp_dequeue (struct ifnet *ifp)
527{
528	struct sppp *sp = (struct sppp*) ifp;
529	struct mbuf *m;
530	int s = splimp ();
531
532	IF_DEQUEUE (&sp->pp_fastq, m);
533	if (! m)
534		IF_DEQUEUE (&sp->pp_if.if_snd, m);
535	splx (s);
536	return (m);
537}
538
539/*
540 * Send keepalive packets, every 10 seconds.
541 */
542void sppp_keepalive (void *dummy)
543{
544	struct sppp *sp;
545	int s = splimp ();
546
547	for (sp=spppq; sp; sp=sp->pp_next) {
548		struct ifnet *ifp = &sp->pp_if;
549
550		/* Keepalive mode disabled or channel down? */
551		if (! (sp->pp_flags & PP_KEEPALIVE) ||
552		    ! (ifp->if_flags & IFF_RUNNING))
553			continue;
554
555		/* No keepalive in PPP mode if LCP not opened yet. */
556		if (! (sp->pp_flags & PP_CISCO) &&
557		    sp->lcp.state != LCP_STATE_OPENED)
558			continue;
559
560		if (sp->pp_alivecnt == MAXALIVECNT) {
561			/* No keepalive packets got.  Stop the interface. */
562			printf ("%s%d: down\n", ifp->if_name, ifp->if_unit);
563			if_down (ifp);
564			qflush (&sp->pp_fastq);
565			if (! (sp->pp_flags & PP_CISCO)) {
566				/* Shut down the PPP link. */
567				sp->lcp.state = LCP_STATE_CLOSED;
568				sp->ipcp.state = IPCP_STATE_CLOSED;
569				UNTIMO (sp);
570				/* Initiate negotiation. */
571				sppp_lcp_open (sp);
572			}
573		}
574		if (sp->pp_alivecnt <= MAXALIVECNT)
575			++sp->pp_alivecnt;
576		if (sp->pp_flags & PP_CISCO)
577			sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
578				sp->pp_rseq);
579		else if (sp->lcp.state == LCP_STATE_OPENED) {
580			long nmagic = htonl (sp->lcp.magic);
581			sp->lcp.echoid = ++sp->pp_seq;
582			sppp_cp_send (sp, PPP_LCP, LCP_ECHO_REQ,
583				sp->lcp.echoid, 4, &nmagic);
584		}
585	}
586	splx (s);
587	timeout (sppp_keepalive, 0, hz * 10);
588}
589
590/*
591 * Handle incoming PPP Link Control Protocol packets.
592 */
593void sppp_lcp_input (struct sppp *sp, struct mbuf *m)
594{
595	struct lcp_header *h;
596	struct ifnet *ifp = &sp->pp_if;
597	int len = m->m_pkthdr.len;
598	u_char *p, opt[6];
599	u_long rmagic;
600
601	if (len < 4) {
602		if (ifp->if_flags & IFF_DEBUG)
603			printf ("%s%d: invalid lcp packet length: %d bytes\n",
604				ifp->if_name, ifp->if_unit, len);
605		return;
606	}
607	h = mtod (m, struct lcp_header*);
608	if (ifp->if_flags & IFF_DEBUG) {
609		char state = '?';
610		switch (sp->lcp.state) {
611		case LCP_STATE_CLOSED:   state = 'C'; break;
612		case LCP_STATE_ACK_RCVD: state = 'R'; break;
613		case LCP_STATE_ACK_SENT: state = 'S'; break;
614		case LCP_STATE_OPENED:   state = 'O'; break;
615		}
616		printf ("%s%d: lcp input(%c): %d bytes <%s id=%xh len=%xh",
617			ifp->if_name, ifp->if_unit, state, len,
618			sppp_lcp_type_name (h->type), h->ident, ntohs (h->len));
619		if (len > 4)
620			sppp_print_bytes ((u_char*) (h+1), len-4);
621		printf (">\n");
622	}
623	if (len > ntohs (h->len))
624		len = ntohs (h->len);
625	switch (h->type) {
626	default:
627		/* Unknown packet type -- send Code-Reject packet. */
628		sppp_cp_send (sp, PPP_LCP, LCP_CODE_REJ, ++sp->pp_seq,
629			m->m_pkthdr.len, h);
630		break;
631	case LCP_CONF_REQ:
632		if (len < 4) {
633			if (ifp->if_flags & IFF_DEBUG)
634				printf ("%s%d: invalid lcp configure request packet length: %d bytes\n",
635					ifp->if_name, ifp->if_unit, len);
636			break;
637		}
638		if (len>4 && !sppp_lcp_conf_parse_options (sp, h, len, &rmagic))
639			goto badreq;
640		if (rmagic == sp->lcp.magic) {
641			/* Local and remote magics equal -- loopback? */
642			if (sp->pp_loopcnt >= MAXALIVECNT*5) {
643				printf ("%s%d: loopback\n",
644					ifp->if_name, ifp->if_unit);
645				sp->pp_loopcnt = 0;
646				if (ifp->if_flags & IFF_UP) {
647					if_down (ifp);
648					qflush (&sp->pp_fastq);
649				}
650			} else if (ifp->if_flags & IFF_DEBUG)
651				printf ("%s%d: conf req: magic glitch\n",
652					ifp->if_name, ifp->if_unit);
653			++sp->pp_loopcnt;
654
655			/* MUST send Conf-Nack packet. */
656			rmagic = ~sp->lcp.magic;
657			opt[0] = LCP_OPT_MAGIC;
658			opt[1] = sizeof (opt);
659			opt[2] = rmagic >> 24;
660			opt[3] = rmagic >> 16;
661			opt[4] = rmagic >> 8;
662			opt[5] = rmagic;
663			sppp_cp_send (sp, PPP_LCP, LCP_CONF_NAK,
664				h->ident, sizeof (opt), &opt);
665badreq:
666			switch (sp->lcp.state) {
667			case LCP_STATE_OPENED:
668				/* Initiate renegotiation. */
669				sppp_lcp_open (sp);
670				/* fall through... */
671			case LCP_STATE_ACK_SENT:
672				/* Go to closed state. */
673				sp->lcp.state = LCP_STATE_CLOSED;
674				sp->ipcp.state = IPCP_STATE_CLOSED;
675			}
676			break;
677				}
678				/* Send Configure-Ack packet. */
679				sp->pp_loopcnt = 0;
680				sppp_cp_send (sp, PPP_LCP, LCP_CONF_ACK,
681					h->ident, len-4, h+1);
682				/* Change the state. */
683		switch (sp->lcp.state) {
684		case LCP_STATE_CLOSED:
685			sp->lcp.state = LCP_STATE_ACK_SENT;
686			break;
687		case LCP_STATE_ACK_RCVD:
688					sp->lcp.state = LCP_STATE_OPENED;
689					sppp_ipcp_open (sp);
690			break;
691		case LCP_STATE_OPENED:
692			/* Remote magic changed -- close session. */
693			sp->lcp.state = LCP_STATE_CLOSED;
694			sp->ipcp.state = IPCP_STATE_CLOSED;
695			/* Initiate renegotiation. */
696			sppp_lcp_open (sp);
697			break;
698		}
699		break;
700	case LCP_CONF_ACK:
701		if (h->ident != sp->lcp.confid)
702			break;
703		UNTIMO (sp);
704		if (! (ifp->if_flags & IFF_UP) &&
705		    (ifp->if_flags & IFF_RUNNING)) {
706			/* Coming out of loopback mode. */
707			ifp->if_flags |= IFF_UP;
708			printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
709		}
710		switch (sp->lcp.state) {
711		case LCP_STATE_CLOSED:
712			sp->lcp.state = LCP_STATE_ACK_RCVD;
713			TIMO (sp, 5);
714			break;
715		case LCP_STATE_ACK_SENT:
716			sp->lcp.state = LCP_STATE_OPENED;
717			sppp_ipcp_open (sp);
718			break;
719		}
720		break;
721	case LCP_CONF_NAK:
722		if (h->ident != sp->lcp.confid)
723			break;
724		p = (u_char*) (h+1);
725		if (len>=10 && p[0] == LCP_OPT_MAGIC && p[1] >= 4) {
726			rmagic = (u_long)p[2] << 24 |
727				(u_long)p[3] << 16 | p[4] << 8 | p[5];
728			if (rmagic == ~sp->lcp.magic) {
729				if (ifp->if_flags & IFF_DEBUG)
730					printf ("%s%d: conf nak: magic glitch\n",
731						ifp->if_name, ifp->if_unit);
732				sp->lcp.magic += time.tv_sec + time.tv_usec;
733			} else
734				sp->lcp.magic = rmagic;
735			}
736		if (sp->lcp.state != LCP_STATE_ACK_SENT) {
737			/* Go to closed state. */
738			sp->lcp.state = LCP_STATE_CLOSED;
739			sp->ipcp.state = IPCP_STATE_CLOSED;
740		}
741		/* The link will be renegotiated after timeout,
742		 * to avoid endless req-nack loop. */
743		UNTIMO (sp);
744		TIMO (sp, 2);
745		break;
746	case LCP_CONF_REJ:
747		if (h->ident != sp->lcp.confid)
748			break;
749		UNTIMO (sp);
750		/* Initiate renegotiation. */
751		sppp_lcp_open (sp);
752		if (sp->lcp.state != LCP_STATE_ACK_SENT) {
753			/* Go to closed state. */
754			sp->lcp.state = LCP_STATE_CLOSED;
755			sp->ipcp.state = IPCP_STATE_CLOSED;
756		}
757		break;
758	case LCP_TERM_REQ:
759		UNTIMO (sp);
760		/* Send Terminate-Ack packet. */
761		sppp_cp_send (sp, PPP_LCP, LCP_TERM_ACK, h->ident, 0, 0);
762		/* Go to closed state. */
763		sp->lcp.state = LCP_STATE_CLOSED;
764		sp->ipcp.state = IPCP_STATE_CLOSED;
765			/* Initiate renegotiation. */
766			sppp_lcp_open (sp);
767		break;
768	case LCP_TERM_ACK:
769	case LCP_CODE_REJ:
770	case LCP_PROTO_REJ:
771		/* Ignore for now. */
772		break;
773	case LCP_DISC_REQ:
774		/* Discard the packet. */
775		break;
776	case LCP_ECHO_REQ:
777		if (len < 8) {
778			if (ifp->if_flags & IFF_DEBUG)
779				printf ("%s%d: invalid lcp echo request packet length: %d bytes\n",
780					ifp->if_name, ifp->if_unit, len);
781			break;
782		}
783		if (ntohl (*(long*)(h+1)) == sp->lcp.magic) {
784			/* Line loopback mode detected. */
785			printf ("%s%d: loopback\n", ifp->if_name, ifp->if_unit);
786			if_down (ifp);
787			qflush (&sp->pp_fastq);
788
789			/* Shut down the PPP link. */
790			sp->lcp.state = LCP_STATE_CLOSED;
791			sp->ipcp.state = IPCP_STATE_CLOSED;
792			UNTIMO (sp);
793			/* Initiate negotiation. */
794			sppp_lcp_open (sp);
795			break;
796		}
797		*(long*)(h+1) = htonl (sp->lcp.magic);
798		sppp_cp_send (sp, PPP_LCP, LCP_ECHO_REPLY, h->ident, len-4, h+1);
799		break;
800	case LCP_ECHO_REPLY:
801		if (h->ident != sp->lcp.echoid)
802			break;
803		if (len < 8) {
804			if (ifp->if_flags & IFF_DEBUG)
805				printf ("%s%d: invalid lcp echo reply 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		sp->pp_alivecnt = 0;
811		break;
812	}
813}
814
815/*
816 * Handle incoming Cisco keepalive protocol packets.
817 */
818void sppp_cisco_input (struct sppp *sp, struct mbuf *m)
819{
820	struct cisco_packet *h;
821	struct ifaddr *ifa;
822	struct ifnet *ifp = &sp->pp_if;
823
824	if (m->m_pkthdr.len != CISCO_PACKET_LEN) {
825		if (ifp->if_flags & IFF_DEBUG)
826			printf ("%s%d: invalid cisco packet length: %d bytes\n",
827				ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
828		return;
829	}
830	h = mtod (m, struct cisco_packet*);
831	if (ifp->if_flags & IFF_DEBUG)
832		printf ("%s%d: cisco input: %d bytes <%lxh %lxh %lxh %xh %xh-%xh>\n",
833			ifp->if_name, ifp->if_unit, m->m_pkthdr.len,
834			ntohl (h->type), h->par1, h->par2, h->rel,
835			h->time0, h->time1);
836	switch (ntohl (h->type)) {
837	default:
838		if (ifp->if_flags & IFF_DEBUG)
839			printf ("%s%d: unknown cisco packet type: 0x%lx\n",
840				ifp->if_name, ifp->if_unit, ntohl (h->type));
841		break;
842	case CISCO_ADDR_REPLY:
843		/* Reply on address request, ignore */
844		break;
845	case CISCO_KEEPALIVE_REQ:
846		sp->pp_alivecnt = 0;
847		sp->pp_rseq = ntohl (h->par1);
848		if (sp->pp_seq == sp->pp_rseq) {
849			/* Local and remote sequence numbers are equal.
850			 * Probably, the line is in loopback mode. */
851			if (sp->pp_loopcnt >= MAXALIVECNT) {
852				printf ("%s%d: loopback\n",
853					ifp->if_name, ifp->if_unit);
854				sp->pp_loopcnt = 0;
855				if (ifp->if_flags & IFF_UP) {
856					if_down (ifp);
857					qflush (&sp->pp_fastq);
858				}
859			}
860			++sp->pp_loopcnt;
861
862			/* Generate new local sequence number */
863			sp->pp_seq ^= time.tv_sec ^ time.tv_usec;
864			break;
865		}
866			sp->pp_loopcnt = 0;
867		if (! (ifp->if_flags & IFF_UP) &&
868		    (ifp->if_flags & IFF_RUNNING)) {
869			ifp->if_flags |= IFF_UP;
870			printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
871		}
872		break;
873	case CISCO_ADDR_REQ:
874		for (ifa=ifp->if_addrlist; ifa; ifa=ifa->ifa_next)
875			if (ifa->ifa_addr->sa_family == AF_INET)
876				break;
877		if (! ifa) {
878			if (ifp->if_flags & IFF_DEBUG)
879				printf ("%s%d: unknown address for cisco request\n",
880					ifp->if_name, ifp->if_unit);
881			return;
882		}
883		sppp_cisco_send (sp, CISCO_ADDR_REPLY,
884			ntohl (((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr),
885			ntohl (((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr.s_addr));
886		break;
887	}
888}
889
890/*
891 * Send PPP LCP packet.
892 */
893void sppp_cp_send (struct sppp *sp, u_short proto, u_char type,
894	u_char ident, u_short len, void *data)
895{
896	struct ppp_header *h;
897	struct lcp_header *lh;
898	struct mbuf *m;
899	struct ifnet *ifp = &sp->pp_if;
900
901	if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
902		len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
903	MGETHDR (m, M_DONTWAIT, MT_DATA);
904	if (! m)
905		return;
906	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
907	m->m_pkthdr.rcvif = 0;
908
909	h = mtod (m, struct ppp_header*);
910	h->address = PPP_ALLSTATIONS;        /* broadcast address */
911	h->control = PPP_UI;                 /* Unnumbered Info */
912	h->protocol = htons (proto);         /* Link Control Protocol */
913
914	lh = (struct lcp_header*) (h + 1);
915	lh->type = type;
916	lh->ident = ident;
917	lh->len = htons (LCP_HEADER_LEN + len);
918	if (len)
919		bcopy (data, lh+1, len);
920
921	if (ifp->if_flags & IFF_DEBUG) {
922		printf ("%s%d: %s output <%s id=%xh len=%xh",
923			ifp->if_name, ifp->if_unit,
924			proto==PPP_LCP ? "lcp" : "ipcp",
925			proto==PPP_LCP ? sppp_lcp_type_name (lh->type) :
926			sppp_ipcp_type_name (lh->type), lh->ident,
927			ntohs (lh->len));
928		if (len)
929			sppp_print_bytes ((u_char*) (lh+1), len);
930		printf (">\n");
931	}
932	if (IF_QFULL (&sp->pp_fastq)) {
933		IF_DROP (&ifp->if_snd);
934		m_freem (m);
935	} else
936		IF_ENQUEUE (&sp->pp_fastq, m);
937	if (! (ifp->if_flags & IFF_OACTIVE))
938		(*ifp->if_start) (ifp);
939	ifp->if_obytes += m->m_pkthdr.len + 3;
940}
941
942/*
943 * Send Cisco keepalive packet.
944 */
945void sppp_cisco_send (struct sppp *sp, int type, long par1, long par2)
946{
947	struct ppp_header *h;
948	struct cisco_packet *ch;
949	struct mbuf *m;
950	struct ifnet *ifp = &sp->pp_if;
951	u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
952
953	MGETHDR (m, M_DONTWAIT, MT_DATA);
954	if (! m)
955		return;
956	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
957	m->m_pkthdr.rcvif = 0;
958
959	h = mtod (m, struct ppp_header*);
960	h->address = CISCO_MULTICAST;
961	h->control = 0;
962	h->protocol = htons (CISCO_KEEPALIVE);
963
964	ch = (struct cisco_packet*) (h + 1);
965	ch->type = htonl (type);
966	ch->par1 = htonl (par1);
967	ch->par2 = htonl (par2);
968	ch->rel = -1;
969	ch->time0 = htons ((u_short) (t >> 16));
970	ch->time1 = htons ((u_short) t);
971
972	if (ifp->if_flags & IFF_DEBUG)
973		printf ("%s%d: cisco output: <%lxh %lxh %lxh %xh %xh-%xh>\n",
974			ifp->if_name, ifp->if_unit, ntohl (ch->type), ch->par1,
975			ch->par2, ch->rel, ch->time0, ch->time1);
976
977	if (IF_QFULL (&sp->pp_fastq)) {
978		IF_DROP (&ifp->if_snd);
979		m_freem (m);
980	} else
981		IF_ENQUEUE (&sp->pp_fastq, m);
982	if (! (ifp->if_flags & IFF_OACTIVE))
983		(*ifp->if_start) (ifp);
984	ifp->if_obytes += m->m_pkthdr.len + 3;
985}
986
987/*
988 * Process an ioctl request.  Called on low priority level.
989 */
990int sppp_ioctl (struct ifnet *ifp, int cmd, void *data)
991{
992	struct ifreq *ifr = (struct ifreq*) data;
993	struct sppp *sp = (struct sppp*) ifp;
994	int s, going_up, going_down;
995
996	switch (cmd) {
997	default:
998		return (EINVAL);
999
1000	case SIOCAIFADDR:
1001	case SIOCSIFDSTADDR:
1002		break;
1003
1004	case SIOCSIFADDR:
1005		ifp->if_flags |= IFF_UP;
1006		/* fall through... */
1007
1008	case SIOCSIFFLAGS:
1009		if (sp->pp_flags & PP_CISCO)
1010			break;
1011		s = splimp ();
1012		going_up   = (ifp->if_flags & IFF_UP) &&
1013			     ! (ifp->if_flags & IFF_RUNNING);
1014		going_down = ! (ifp->if_flags & IFF_UP) &&
1015			      (ifp->if_flags & IFF_RUNNING);
1016		if (going_up || going_down) {
1017			/* Shut down the PPP link. */
1018			ifp->if_flags &= ~IFF_RUNNING;
1019			sp->lcp.state = LCP_STATE_CLOSED;
1020			sp->ipcp.state = IPCP_STATE_CLOSED;
1021			UNTIMO (sp);
1022		}
1023		if (going_up) {
1024			/* Interface is starting -- initiate negotiation. */
1025			ifp->if_flags |= IFF_RUNNING;
1026			sppp_lcp_open (sp);
1027		}
1028		splx (s);
1029		break;
1030
1031#ifdef SIOCSIFMTU
1032#ifndef ifr_mtu
1033#define ifr_mtu ifr_metric
1034#endif
1035	case SIOCSIFMTU:
1036		if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > PP_MTU)
1037			return (EINVAL);
1038		ifp->if_mtu = ifr->ifr_mtu;
1039		break;
1040#endif
1041#ifdef SLIOCSETMTU
1042	case SLIOCSETMTU:
1043		if (*(short*)data < 128 || *(short*)data > PP_MTU)
1044			return (EINVAL);
1045		ifp->if_mtu = *(short*)data;
1046		break;
1047#endif
1048#ifdef SIOCGIFMTU
1049	case SIOCGIFMTU:
1050		ifr->ifr_mtu = ifp->if_mtu;
1051		break;
1052#endif
1053#ifdef SLIOCGETMTU
1054	case SLIOCGETMTU:
1055		*(short*)data = ifp->if_mtu;
1056		break;
1057#endif
1058#ifdef MULTICAST
1059	case SIOCADDMULTI:
1060	case SIOCDELMULTI:
1061		break;
1062#endif
1063	}
1064	return (0);
1065}
1066
1067/*
1068 * Analyze the LCP Configure-Request options list
1069 * for the presence of unknown options.
1070 * If the request contains unknown options, build and
1071 * send Configure-reject packet, containing only unknown options.
1072 */
1073int sppp_lcp_conf_parse_options (struct sppp *sp, struct lcp_header *h,
1074	int len, u_long *magic)
1075{
1076	u_char *buf, *r, *p;
1077	int rlen;
1078
1079	len -= 4;
1080	buf = r = malloc (len, M_TEMP, M_NOWAIT);
1081	if (! buf)
1082		return (0);
1083
1084	p = (void*) (h+1);
1085	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1086		switch (*p) {
1087		case LCP_OPT_MAGIC:
1088			/* Magic number -- extract. */
1089			if (len >= 6 && p[1] == 6) {
1090				*magic = (u_long)p[2] << 24 |
1091					(u_long)p[3] << 16 | p[4] << 8 | p[5];
1092				continue;
1093			}
1094			break;
1095		case LCP_OPT_ASYNC_MAP:
1096			/* Async control character map -- check to be zero. */
1097			if (len >= 6 && p[1] == 6 && ! p[2] && ! p[3] &&
1098			    ! p[4] && ! p[5])
1099				continue;
1100			break;
1101		case LCP_OPT_MRU:
1102			/* Maximum receive unit -- always OK. */
1103			continue;
1104		default:
1105			/* Others not supported. */
1106			break;
1107		}
1108		/* Add the option to rejected list. */
1109			bcopy (p, r, p[1]);
1110			r += p[1];
1111		rlen += p[1];
1112		}
1113	if (rlen)
1114	sppp_cp_send (sp, PPP_LCP, LCP_CONF_REJ, h->ident, rlen, buf);
1115	free (buf, M_TEMP);
1116	return (rlen == 0);
1117}
1118
1119void sppp_ipcp_input (struct sppp *sp, struct mbuf *m)
1120{
1121	struct lcp_header *h;
1122	struct ifnet *ifp = &sp->pp_if;
1123	int len = m->m_pkthdr.len;
1124
1125	if (len < 4) {
1126		/* if (ifp->if_flags & IFF_DEBUG) */
1127			printf ("%s%d: invalid ipcp packet length: %d bytes\n",
1128				ifp->if_name, ifp->if_unit, len);
1129		return;
1130	}
1131	h = mtod (m, struct lcp_header*);
1132	if (ifp->if_flags & IFF_DEBUG) {
1133		printf ("%s%d: ipcp input: %d bytes <%s id=%xh len=%xh",
1134			ifp->if_name, ifp->if_unit, len,
1135			sppp_ipcp_type_name (h->type), h->ident, ntohs (h->len));
1136		if (len > 4)
1137			sppp_print_bytes ((u_char*) (h+1), len-4);
1138		printf (">\n");
1139	}
1140	if (len > ntohs (h->len))
1141		len = ntohs (h->len);
1142	switch (h->type) {
1143	default:
1144		/* Unknown packet type -- send Code-Reject packet. */
1145		sppp_cp_send (sp, PPP_IPCP, IPCP_CODE_REJ, ++sp->pp_seq, len, h);
1146		break;
1147	case IPCP_CONF_REQ:
1148		if (len < 4) {
1149			if (ifp->if_flags & IFF_DEBUG)
1150				printf ("%s%d: invalid ipcp configure request packet length: %d bytes\n",
1151					ifp->if_name, ifp->if_unit, len);
1152			return;
1153		}
1154		if (len > 4) {
1155			sppp_cp_send (sp, PPP_IPCP, LCP_CONF_REJ, h->ident,
1156				len-4, h+1);
1157
1158			switch (sp->ipcp.state) {
1159			case IPCP_STATE_OPENED:
1160				/* Initiate renegotiation. */
1161				sppp_ipcp_open (sp);
1162				/* fall through... */
1163			case IPCP_STATE_ACK_SENT:
1164				/* Go to closed state. */
1165				sp->ipcp.state = IPCP_STATE_CLOSED;
1166			}
1167		} else {
1168			/* Send Configure-Ack packet. */
1169			sppp_cp_send (sp, PPP_IPCP, IPCP_CONF_ACK, h->ident,
1170				0, 0);
1171			/* Change the state. */
1172			if (sp->ipcp.state == IPCP_STATE_ACK_RCVD)
1173				sp->ipcp.state = IPCP_STATE_OPENED;
1174			else
1175				sp->ipcp.state = IPCP_STATE_ACK_SENT;
1176		}
1177		break;
1178	case IPCP_CONF_ACK:
1179		if (h->ident != sp->ipcp.confid)
1180			break;
1181		UNTIMO (sp);
1182		switch (sp->ipcp.state) {
1183		case IPCP_STATE_CLOSED:
1184			sp->ipcp.state = IPCP_STATE_ACK_RCVD;
1185			TIMO (sp, 5);
1186			break;
1187		case IPCP_STATE_ACK_SENT:
1188			sp->ipcp.state = IPCP_STATE_OPENED;
1189			break;
1190		}
1191		break;
1192	case IPCP_CONF_NAK:
1193	case IPCP_CONF_REJ:
1194		if (h->ident != sp->ipcp.confid)
1195			break;
1196		UNTIMO (sp);
1197			/* Initiate renegotiation. */
1198			sppp_ipcp_open (sp);
1199		if (sp->ipcp.state != IPCP_STATE_ACK_SENT)
1200			/* Go to closed state. */
1201			sp->ipcp.state = IPCP_STATE_CLOSED;
1202		break;
1203	case IPCP_TERM_REQ:
1204		/* Send Terminate-Ack packet. */
1205		sppp_cp_send (sp, PPP_IPCP, IPCP_TERM_ACK, h->ident, 0, 0);
1206		/* Go to closed state. */
1207		sp->ipcp.state = IPCP_STATE_CLOSED;
1208			/* Initiate renegotiation. */
1209			sppp_ipcp_open (sp);
1210		break;
1211	case IPCP_TERM_ACK:
1212		/* Ignore for now. */
1213	case IPCP_CODE_REJ:
1214		/* Ignore for now. */
1215		break;
1216	}
1217}
1218
1219void sppp_lcp_open (struct sppp *sp)
1220{
1221	char opt[6];
1222
1223	if (! sp->lcp.magic)
1224	sp->lcp.magic = time.tv_sec + time.tv_usec;
1225	opt[0] = LCP_OPT_MAGIC;
1226	opt[1] = sizeof (opt);
1227	opt[2] = sp->lcp.magic >> 24;
1228	opt[3] = sp->lcp.magic >> 16;
1229	opt[4] = sp->lcp.magic >> 8;
1230	opt[5] = sp->lcp.magic;
1231	sp->lcp.confid = ++sp->pp_seq;
1232	sppp_cp_send (sp, PPP_LCP, LCP_CONF_REQ, sp->lcp.confid,
1233		sizeof (opt), &opt);
1234	TIMO (sp, 2);
1235}
1236
1237void sppp_ipcp_open (struct sppp *sp)
1238{
1239	sp->ipcp.confid = ++sp->pp_seq;
1240	sppp_cp_send (sp, PPP_IPCP, IPCP_CONF_REQ, sp->ipcp.confid, 0, 0);
1241	TIMO (sp, 2);
1242}
1243
1244/*
1245 * Process PPP control protocol timeouts.
1246 */
1247void sppp_cp_timeout (void *arg)
1248{
1249	struct sppp *sp = (struct sppp*) arg;
1250	int s = splimp ();
1251
1252	sp->pp_flags &= ~PP_TIMO;
1253	if (! (sp->pp_if.if_flags & IFF_RUNNING) || (sp->pp_flags & PP_CISCO)) {
1254		splx (s);
1255		return;
1256	}
1257	switch (sp->lcp.state) {
1258	case LCP_STATE_CLOSED:
1259		/* No ACK for Configure-Request, retry. */
1260		sppp_lcp_open (sp);
1261		break;
1262	case LCP_STATE_ACK_RCVD:
1263		/* ACK got, but no Configure-Request for peer, retry. */
1264		sppp_lcp_open (sp);
1265		sp->lcp.state = LCP_STATE_CLOSED;
1266		break;
1267	case LCP_STATE_ACK_SENT:
1268		/* ACK sent but no ACK for Configure-Request, retry. */
1269		sppp_lcp_open (sp);
1270		break;
1271	case LCP_STATE_OPENED:
1272		/* LCP is already OK, try IPCP. */
1273		switch (sp->ipcp.state) {
1274		case IPCP_STATE_CLOSED:
1275			/* No ACK for Configure-Request, retry. */
1276			sppp_ipcp_open (sp);
1277			break;
1278		case IPCP_STATE_ACK_RCVD:
1279			/* ACK got, but no Configure-Request for peer, retry. */
1280			sppp_ipcp_open (sp);
1281			sp->ipcp.state = IPCP_STATE_CLOSED;
1282			break;
1283		case IPCP_STATE_ACK_SENT:
1284			/* ACK sent but no ACK for Configure-Request, retry. */
1285			sppp_ipcp_open (sp);
1286			break;
1287		case IPCP_STATE_OPENED:
1288			/* IPCP is OK. */
1289			break;
1290		}
1291		break;
1292	}
1293	splx (s);
1294}
1295
1296char *sppp_lcp_type_name (u_char type)
1297{
1298	static char buf [8];
1299	switch (type) {
1300	case LCP_CONF_REQ:   return ("conf-req");
1301	case LCP_CONF_ACK:   return ("conf-ack");
1302	case LCP_CONF_NAK:   return ("conf-nack");
1303	case LCP_CONF_REJ:   return ("conf-rej");
1304	case LCP_TERM_REQ:   return ("term-req");
1305	case LCP_TERM_ACK:   return ("term-ack");
1306	case LCP_CODE_REJ:   return ("code-rej");
1307	case LCP_PROTO_REJ:  return ("proto-rej");
1308	case LCP_ECHO_REQ:   return ("echo-req");
1309	case LCP_ECHO_REPLY: return ("echo-reply");
1310	case LCP_DISC_REQ:   return ("discard-req");
1311	}
1312	sprintf (buf, "%xh", type);
1313	return (buf);
1314}
1315
1316char *sppp_ipcp_type_name (u_char type)
1317{
1318	static char buf [8];
1319	switch (type) {
1320	case IPCP_CONF_REQ:   return ("conf-req");
1321	case IPCP_CONF_ACK:   return ("conf-ack");
1322	case IPCP_CONF_NAK:   return ("conf-nack");
1323	case IPCP_CONF_REJ:   return ("conf-rej");
1324	case IPCP_TERM_REQ:   return ("term-req");
1325	case IPCP_TERM_ACK:   return ("term-ack");
1326	case IPCP_CODE_REJ:   return ("code-rej");
1327	}
1328	sprintf (buf, "%xh", type);
1329	return (buf);
1330}
1331
1332void sppp_print_bytes (u_char *p, u_short len)
1333{
1334	printf (" %x", *p++);
1335	while (--len > 0)
1336		printf ("-%x", *p++);
1337}
1338