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