if_spppsubr.c revision 29024
1131087Smarcel/*
2131087Smarcel * Synchronous PPP/Cisco link level subroutines.
3215082Simp * Keepalive protocol implemented in both Cisco and PPP modes.
4215082Simp *
5138215Smarcel * Copyright (C) 1994 Cronyx Ltd.
6215082Simp * Author: Serge Vakulenko, <vak@cronyx.ru>
7239272Sgonzo *
8215082Simp * Heavily revamped to conform to RFC 1661.
9215082Simp * Copyright (C) 1997, Joerg Wunsch.
10215082Simp *
11215082Simp * This software is distributed with NO WARRANTIES, not even the implied
12215082Simp * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13131087Smarcel *
14136910Sru * Authors grant any other persons or organisations permission to use
15137440Smarcel * or modify this software as long as this message is kept with the software,
16137440Smarcel * all derivative works or modified versions.
17137440Smarcel *
18137440Smarcel * From: Version 1.9, Wed Oct  4 18:58:15 MSK 1995
19137440Smarcel *
20137440Smarcel * $Id: if_spppsubr.c,v 1.25 1997/08/12 05:22:54 kjc Exp $
21137440Smarcel */
22137440Smarcel
23137440Smarcel#include <sys/param.h>
24137440Smarcel#include <sys/systm.h>
25137440Smarcel#include <sys/kernel.h>
26137440Smarcel#include <sys/sockio.h>
27137440Smarcel#include <sys/socket.h>
28137440Smarcel#include <sys/syslog.h>
29251858Semaste#include <sys/malloc.h>
30137440Smarcel#include <sys/mbuf.h>
31137440Smarcel
32138215Smarcel#include <net/if.h>
33138215Smarcel#include <net/netisr.h>
34137440Smarcel#include <net/if_types.h>
35137440Smarcel
36137440Smarcel#ifdef INET
37137440Smarcel#include <netinet/in.h>
38137440Smarcel#include <netinet/in_systm.h>
39137440Smarcel#include <netinet/in_var.h>
40137440Smarcel#include <netinet/ip.h>
41137440Smarcel#include <netinet/tcp.h>
42137440Smarcel#include <netinet/if_ether.h>
43137440Smarcel#endif
44137440Smarcel
45137440Smarcel#ifdef IPX
46137440Smarcel#include <netipx/ipx.h>
47137440Smarcel#include <netipx/ipx_if.h>
48138383Smarcel#endif
49138383Smarcel
50137440Smarcel#ifdef NS
51137440Smarcel#include <netns/ns.h>
52137440Smarcel#include <netns/ns_if.h>
53137440Smarcel#endif
54137440Smarcel
55137440Smarcel#ifdef ISO
56137440Smarcel#include <netiso/argo_debug.h>
57134154Sdavidxu#include <netiso/iso.h>
58131087Smarcel#include <netiso/iso_var.h>
59141911Sobrien#include <netiso/iso_snpac.h>
60141911Sobrien#endif
61141911Sobrien
62141911Sobrien#include <net/if_sppp.h>
63141911Sobrien
64138380Smarcel#define MAXALIVECNT     3               /* max. alive packets */
65138215Smarcel
66138215Smarcel/*
67138215Smarcel * Interface flags that can be set in an ifconfig command.
68134154Sdavidxu *
69134154Sdavidxu * Setting link0 will make the link passive, i.e. it will be marked
70251858Semaste * as being administrative openable, but won't be opened to begin
71138215Smarcel * with.  Incoming calls will be answered, or subsequent calls with
72131087Smarcel * -link1 will cause the administrative open of the LCP layer.
73131087Smarcel *
74131087Smarcel * Setting link1 will cause the link to auto-dial only as packets
75131087Smarcel * arrive to be sent.
76131087Smarcel */
77131087Smarcel
78131087Smarcel#define IFF_PASSIVE	IFF_LINK0	/* wait passively for connection */
79131087Smarcel#define IFF_AUTO	IFF_LINK1	/* auto-dial on output */
80131087Smarcel
81#define PPP_ALLSTATIONS 0xff            /* All-Stations broadcast address */
82#define PPP_UI          0x03            /* Unnumbered Information */
83#define PPP_IP          0x0021          /* Internet Protocol */
84#define PPP_ISO         0x0023          /* ISO OSI Protocol */
85#define PPP_XNS         0x0025          /* Xerox NS Protocol */
86#define PPP_IPX         0x002b          /* Novell IPX Protocol */
87#define PPP_LCP         0xc021          /* Link Control Protocol */
88#define PPP_IPCP        0x8021          /* Internet Protocol Control Protocol */
89
90#define CONF_REQ	1		/* PPP configure request */
91#define CONF_ACK	2		/* PPP configure acknowledge */
92#define CONF_NAK	3		/* PPP configure negative ack */
93#define CONF_REJ	4		/* PPP configure reject */
94#define TERM_REQ	5		/* PPP terminate request */
95#define TERM_ACK	6		/* PPP terminate acknowledge */
96#define CODE_REJ	7		/* PPP code reject */
97#define PROTO_REJ	8		/* PPP protocol reject */
98#define ECHO_REQ	9		/* PPP echo request */
99#define ECHO_REPLY	10		/* PPP echo reply */
100#define DISC_REQ	11		/* PPP discard request */
101
102#define LCP_OPT_MRU             1       /* maximum receive unit */
103#define LCP_OPT_ASYNC_MAP       2       /* async control character map */
104#define LCP_OPT_AUTH_PROTO      3       /* authentication protocol */
105#define LCP_OPT_QUAL_PROTO      4       /* quality protocol */
106#define LCP_OPT_MAGIC           5       /* magic number */
107#define LCP_OPT_RESERVED        6       /* reserved */
108#define LCP_OPT_PROTO_COMP      7       /* protocol field compression */
109#define LCP_OPT_ADDR_COMP       8       /* address/control field compression */
110
111#define IPCP_OPT_ADDRESSES	1	/* both IP addresses; deprecated */
112#define IPCP_OPT_COMPRESSION	2	/* IP compression protocol (VJ) */
113#define IPCP_OPT_ADDRESS	3	/* local IP address */
114
115#define CISCO_MULTICAST         0x8f    /* Cisco multicast address */
116#define CISCO_UNICAST           0x0f    /* Cisco unicast address */
117#define CISCO_KEEPALIVE         0x8035  /* Cisco keepalive protocol */
118#define CISCO_ADDR_REQ          0       /* Cisco address request */
119#define CISCO_ADDR_REPLY        1       /* Cisco address reply */
120#define CISCO_KEEPALIVE_REQ     2       /* Cisco keepalive request */
121
122/* states are named and numbered according to RFC 1661 */
123#define STATE_INITIAL	0
124#define STATE_STARTING	1
125#define STATE_CLOSED	2
126#define STATE_STOPPED	3
127#define STATE_CLOSING	4
128#define STATE_STOPPING	5
129#define STATE_REQ_SENT	6
130#define STATE_ACK_RCVD	7
131#define STATE_ACK_SENT	8
132#define STATE_OPENED	9
133
134struct ppp_header {
135	u_char address;
136	u_char control;
137	u_short protocol;
138};
139#define PPP_HEADER_LEN          sizeof (struct ppp_header)
140
141struct lcp_header {
142	u_char type;
143	u_char ident;
144	u_short len;
145};
146#define LCP_HEADER_LEN          sizeof (struct lcp_header)
147
148struct cisco_packet {
149	u_long type;
150	u_long par1;
151	u_long par2;
152	u_short rel;
153	u_short time0;
154	u_short time1;
155};
156#define CISCO_PACKET_LEN 18
157
158/*
159 * We follow the spelling and capitalization of RFC 1661 here, to make
160 * it easier comparing with the standard.  Please refer to this RFC in
161 * case you can't make sense out of these abbreviation; it will also
162 * explain the semantics related to the various events and actions.
163 */
164struct cp {
165	u_short	proto;		/* PPP control protocol number */
166	u_char protoidx;	/* index into state table in struct sppp */
167	u_char flags;
168#define CP_LCP		0x01	/* this is the LCP */
169#define CP_AUTH		0x02	/* this is an authentication protocol */
170#define CP_NCP		0x04	/* this is a NCP */
171#define CP_QUAL		0x08	/* this is a quality reporting protocol */
172	const char *name;	/* name of this control protocol */
173	/* event handlers */
174	void	(*Up)(struct sppp *sp);
175	void	(*Down)(struct sppp *sp);
176	void	(*Open)(struct sppp *sp);
177	void	(*Close)(struct sppp *sp);
178	void	(*TO)(void *sp);
179	int	(*RCR)(struct sppp *sp, struct lcp_header *h, int len);
180	void	(*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
181	void	(*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
182	/* actions */
183	void	(*tlu)(struct sppp *sp);
184	void	(*tld)(struct sppp *sp);
185	void	(*tls)(struct sppp *sp);
186	void	(*tlf)(struct sppp *sp);
187	void	(*scr)(struct sppp *sp);
188};
189
190static struct sppp *spppq;
191
192/*
193 * The following disgusting hack gets around the problem that IP TOS
194 * can't be set yet.  We want to put "interactive" traffic on a high
195 * priority queue.  To decide if traffic is interactive, we check that
196 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
197 */
198static u_short interactive_ports[8] = {
199	0,	513,	0,	0,
200	0,	21,	0,	23,
201};
202#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
203
204/* almost every function needs these */
205#define STDDCL							\
206	struct ifnet *ifp = &sp->pp_if;				\
207	int debug = ifp->if_flags & IFF_DEBUG
208
209static int sppp_output(struct ifnet *ifp, struct mbuf *m,
210		       struct sockaddr *dst, struct rtentry *rt);
211
212static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
213static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
214
215static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
216			  struct mbuf *m);
217static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
218			 u_char ident, u_short len, void *data);
219static void sppp_cp_timeout(void *arg);
220static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
221				 int newstate);
222
223static void sppp_up_event(const struct cp *cp, struct sppp *sp);
224static void sppp_down_event(const struct cp *cp, struct sppp *sp);
225static void sppp_open_event(const struct cp *cp, struct sppp *sp);
226static void sppp_close_event(const struct cp *cp, struct sppp *sp);
227static void sppp_to_event(const struct cp *cp, struct sppp *sp);
228
229static void sppp_lcp_init(struct sppp *sp);
230static void sppp_lcp_up(struct sppp *sp);
231static void sppp_lcp_down(struct sppp *sp);
232static void sppp_lcp_open(struct sppp *sp);
233static void sppp_lcp_close(struct sppp *sp);
234static void sppp_lcp_TO(void *sp);
235static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
236static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
237static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
238static void sppp_lcp_tlu(struct sppp *sp);
239static void sppp_lcp_tld(struct sppp *sp);
240static void sppp_lcp_tls(struct sppp *sp);
241static void sppp_lcp_tlf(struct sppp *sp);
242static void sppp_lcp_scr(struct sppp *sp);
243static void sppp_lcp_check(struct sppp *sp);
244
245static void sppp_ipcp_init(struct sppp *sp);
246static void sppp_ipcp_up(struct sppp *sp);
247static void sppp_ipcp_down(struct sppp *sp);
248static void sppp_ipcp_open(struct sppp *sp);
249static void sppp_ipcp_close(struct sppp *sp);
250static void sppp_ipcp_TO(void *sp);
251static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
252static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
253static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
254static void sppp_ipcp_tlu(struct sppp *sp);
255static void sppp_ipcp_tld(struct sppp *sp);
256static void sppp_ipcp_tls(struct sppp *sp);
257static void sppp_ipcp_tlf(struct sppp *sp);
258static void sppp_ipcp_scr(struct sppp *sp);
259
260static const char *sppp_cp_type_name(u_char type);
261static const char *sppp_lcp_opt_name(u_char opt);
262static const char *sppp_ipcp_opt_name(u_char opt);
263static const char *sppp_state_name(int state);
264static const char *sppp_phase_name(enum ppp_phase phase);
265static const char *sppp_proto_name(u_short proto);
266
267static void sppp_keepalive(void *dummy);
268static void sppp_qflush(struct ifqueue *ifq);
269
270static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst);
271static void sppp_set_ip_addr(struct sppp *sp, u_long src);
272
273static void sppp_print_bytes(u_char *p, u_short len);
274
275/* our control protocol descriptors */
276const struct cp lcp = {
277	PPP_LCP, IDX_LCP, CP_LCP, "lcp",
278	sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
279	sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
280	sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
281	sppp_lcp_scr
282};
283
284const struct cp ipcp = {
285	PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
286	sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
287	sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
288	sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
289	sppp_ipcp_scr
290};
291
292const struct cp *cps[IDX_COUNT] = {
293	&lcp,			/* IDX_LCP */
294	&ipcp,			/* IDX_IPCP */
295};
296
297
298/*
299 * Exported functions, comprising our interface to the lower layer.
300 */
301
302/*
303 * Process the received packet.
304 */
305void
306sppp_input(struct ifnet *ifp, struct mbuf *m)
307{
308	struct ppp_header *h;
309	struct ifqueue *inq = 0;
310	int s;
311	struct sppp *sp = (struct sppp *)ifp;
312	int debug = ifp->if_flags & IFF_DEBUG;
313
314	if (ifp->if_flags & IFF_UP)
315		/* Count received bytes, add FCS and one flag */
316		ifp->if_ibytes += m->m_pkthdr.len + 3;
317
318	if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
319		/* Too small packet, drop it. */
320		if (debug)
321			log(LOG_DEBUG,
322			    "%s%d: input packet is too small, %d bytes\n",
323			    ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
324	  drop:
325		++ifp->if_ierrors;
326		++ifp->if_iqdrops;
327		m_freem (m);
328		return;
329	}
330
331	/* Get PPP header. */
332	h = mtod (m, struct ppp_header*);
333	m_adj (m, PPP_HEADER_LEN);
334
335	switch (h->address) {
336	case PPP_ALLSTATIONS:
337		if (h->control != PPP_UI)
338			goto invalid;
339		if (sp->pp_flags & PP_CISCO) {
340			if (debug)
341				log(LOG_DEBUG,
342				    "%s%d: PPP packet in Cisco mode "
343				    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
344				    ifp->if_name, ifp->if_unit,
345				    h->address, h->control, ntohs(h->protocol));
346			goto drop;
347		}
348		switch (ntohs (h->protocol)) {
349		default:
350			if (sp->state[IDX_LCP] == STATE_OPENED)
351				sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
352					++sp->pp_seq, m->m_pkthdr.len + 2,
353					&h->protocol);
354			if (debug)
355				log(LOG_DEBUG,
356				    "%s%d: invalid input protocol "
357				    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
358				    ifp->if_name, ifp->if_unit,
359				    h->address, h->control, ntohs(h->protocol));
360			++ifp->if_noproto;
361			goto drop;
362		case PPP_LCP:
363			sppp_cp_input(&lcp, (struct sppp*)ifp, m);
364			m_freem (m);
365			return;
366#ifdef INET
367		case PPP_IPCP:
368			if (sp->pp_phase == PHASE_NETWORK)
369				sppp_cp_input(&ipcp, (struct sppp*) ifp, m);
370			m_freem (m);
371			return;
372		case PPP_IP:
373			if (sp->state[IDX_IPCP] == STATE_OPENED) {
374				schednetisr (NETISR_IP);
375				inq = &ipintrq;
376			}
377			break;
378#endif
379#ifdef IPX
380		case PPP_IPX:
381			/* IPX IPXCP not implemented yet */
382			if (sp->pp_phase == PHASE_NETWORK) {
383				schednetisr (NETISR_IPX);
384				inq = &ipxintrq;
385			}
386			break;
387#endif
388#ifdef NS
389		case PPP_XNS:
390			/* XNS IDPCP not implemented yet */
391			if (sp->pp_phase == PHASE_NETWORK) {
392				schednetisr (NETISR_NS);
393				inq = &nsintrq;
394			}
395			break;
396#endif
397#ifdef ISO
398		case PPP_ISO:
399			/* OSI NLCP not implemented yet */
400			if (sp->pp_phase == PHASE_NETWORK) {
401				schednetisr (NETISR_ISO);
402				inq = &clnlintrq;
403			}
404			break;
405#endif
406		}
407		break;
408	case CISCO_MULTICAST:
409	case CISCO_UNICAST:
410		/* Don't check the control field here (RFC 1547). */
411		if (! (sp->pp_flags & PP_CISCO)) {
412			if (debug)
413				log(LOG_DEBUG,
414				    "%s%d: Cisco packet in PPP mode "
415				    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
416				    ifp->if_name, ifp->if_unit,
417				    h->address, h->control, ntohs(h->protocol));
418			goto drop;
419		}
420		switch (ntohs (h->protocol)) {
421		default:
422			++ifp->if_noproto;
423			goto invalid;
424		case CISCO_KEEPALIVE:
425			sppp_cisco_input ((struct sppp*) ifp, m);
426			m_freem (m);
427			return;
428#ifdef INET
429		case ETHERTYPE_IP:
430			schednetisr (NETISR_IP);
431			inq = &ipintrq;
432			break;
433#endif
434#ifdef IPX
435		case ETHERTYPE_IPX:
436			schednetisr (NETISR_IPX);
437			inq = &ipxintrq;
438			break;
439#endif
440#ifdef NS
441		case ETHERTYPE_NS:
442			schednetisr (NETISR_NS);
443			inq = &nsintrq;
444			break;
445#endif
446		}
447		break;
448	default:        /* Invalid PPP packet. */
449	  invalid:
450		if (debug)
451			log(LOG_DEBUG,
452			    "%s%d: invalid input packet "
453			    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
454			    ifp->if_name, ifp->if_unit,
455			    h->address, h->control, ntohs(h->protocol));
456		goto drop;
457	}
458
459	if (! (ifp->if_flags & IFF_UP) || ! inq)
460		goto drop;
461
462	/* Check queue. */
463	s = splimp();
464	if (IF_QFULL (inq)) {
465		/* Queue overflow. */
466		IF_DROP(inq);
467		splx(s);
468		if (debug)
469			log(LOG_DEBUG, "%s%d: protocol queue overflow\n",
470				ifp->if_name, ifp->if_unit);
471		goto drop;
472	}
473	IF_ENQUEUE(inq, m);
474	splx(s);
475}
476
477/*
478 * Enqueue transmit packet.
479 */
480static int
481sppp_output(struct ifnet *ifp, struct mbuf *m,
482	    struct sockaddr *dst, struct rtentry *rt)
483{
484	struct sppp *sp = (struct sppp*) ifp;
485	struct ppp_header *h;
486	struct ifqueue *ifq;
487	int s, rv = 0;
488
489	s = splimp();
490
491	if ((ifp->if_flags & IFF_UP) == 0 ||
492	    (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
493		m_freem (m);
494		splx (s);
495		return (ENETDOWN);
496	}
497
498	if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
499		/*
500		 * Interface is not yet running, but auto-dial.  Need
501		 * to start LCP for it.
502		 */
503		ifp->if_flags |= IFF_RUNNING;
504		splx(s);
505		lcp.Open(sp);
506		s = splimp();
507	}
508
509	ifq = &ifp->if_snd;
510#ifdef INET
511	/*
512	 * Put low delay, telnet, rlogin and ftp control packets
513	 * in front of the queue.
514	 */
515	if (dst->sa_family == AF_INET) {
516		struct ip *ip = mtod (m, struct ip*);
517		struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
518
519		if (! IF_QFULL (&sp->pp_fastq) &&
520		    ((ip->ip_tos & IPTOS_LOWDELAY) ||
521	    	    ip->ip_p == IPPROTO_TCP &&
522	    	    m->m_len >= sizeof (struct ip) + sizeof (struct tcphdr) &&
523	    	    (INTERACTIVE (ntohs (tcp->th_sport)) ||
524	    	    INTERACTIVE (ntohs (tcp->th_dport)))))
525			ifq = &sp->pp_fastq;
526	}
527#endif
528
529	/*
530	 * Prepend general data packet PPP header. For now, IP only.
531	 */
532	M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
533	if (! m) {
534		if (ifp->if_flags & IFF_DEBUG)
535			log(LOG_DEBUG, "%s%d: no memory for transmit header\n",
536				ifp->if_name, ifp->if_unit);
537		++ifp->if_oerrors;
538		splx (s);
539		return (ENOBUFS);
540	}
541	h = mtod (m, struct ppp_header*);
542	if (sp->pp_flags & PP_CISCO) {
543		h->address = CISCO_UNICAST;        /* unicast address */
544		h->control = 0;
545	} else {
546		h->address = PPP_ALLSTATIONS;        /* broadcast address */
547		h->control = PPP_UI;                 /* Unnumbered Info */
548	}
549
550	switch (dst->sa_family) {
551#ifdef INET
552	case AF_INET:   /* Internet Protocol */
553		if (sp->pp_flags & PP_CISCO)
554			h->protocol = htons (ETHERTYPE_IP);
555		else {
556			/*
557			 * Don't choke with an ENETDOWN early.  It's
558			 * possible that we just started dialing out,
559			 * so don't drop the packet immediately.  If
560			 * we notice that we run out of buffer space
561			 * below, we will however remember that we are
562			 * not ready to carry IP packets, and return
563			 * ENETDOWN, as opposed to ENOBUFS.
564			 */
565			h->protocol = htons(PPP_IP);
566			if (sp->state[IDX_IPCP] != STATE_OPENED)
567				rv = ENETDOWN;
568		}
569		break;
570#endif
571#ifdef NS
572	case AF_NS:     /* Xerox NS Protocol */
573		h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
574			ETHERTYPE_NS : PPP_XNS);
575		break;
576#endif
577#ifdef IPX
578	case AF_IPX:     /* Novell IPX Protocol */
579		h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
580			ETHERTYPE_IPX : PPP_IPX);
581		break;
582#endif
583#ifdef ISO
584	case AF_ISO:    /* ISO OSI Protocol */
585		if (sp->pp_flags & PP_CISCO)
586			goto nosupport;
587		h->protocol = htons (PPP_ISO);
588		break;
589nosupport:
590#endif
591	default:
592		m_freem (m);
593		++ifp->if_oerrors;
594		splx (s);
595		return (EAFNOSUPPORT);
596	}
597
598	/*
599	 * Queue message on interface, and start output if interface
600	 * not yet active.
601	 */
602	if (IF_QFULL (ifq)) {
603		IF_DROP (&ifp->if_snd);
604		m_freem (m);
605		++ifp->if_oerrors;
606		splx (s);
607		return (rv? rv: ENOBUFS);
608	}
609	IF_ENQUEUE (ifq, m);
610	if (! (ifp->if_flags & IFF_OACTIVE))
611		(*ifp->if_start) (ifp);
612
613	/*
614	 * Count output packets and bytes.
615	 * The packet length includes header, FCS and 1 flag,
616	 * according to RFC 1333.
617	 */
618	ifp->if_obytes += m->m_pkthdr.len + 3;
619	splx (s);
620	return (0);
621}
622
623void
624sppp_attach(struct ifnet *ifp)
625{
626	struct sppp *sp = (struct sppp*) ifp;
627
628	/* Initialize keepalive handler. */
629	if (! spppq)
630		timeout (sppp_keepalive, 0, hz * 10);
631
632	/* Insert new entry into the keepalive list. */
633	sp->pp_next = spppq;
634	spppq = sp;
635
636	sp->pp_if.if_type = IFT_PPP;
637	sp->pp_if.if_output = sppp_output;
638	sp->pp_fastq.ifq_maxlen = 32;
639	sp->pp_cpq.ifq_maxlen = 20;
640	sp->pp_loopcnt = 0;
641	sp->pp_alivecnt = 0;
642	sp->pp_seq = 0;
643	sp->pp_rseq = 0;
644	sp->pp_phase = PHASE_DEAD;
645	sp->pp_up = lcp.Up;
646	sp->pp_down = lcp.Down;
647
648	sppp_lcp_init(sp);
649	sppp_ipcp_init(sp);
650}
651
652void
653sppp_detach(struct ifnet *ifp)
654{
655	struct sppp **q, *p, *sp = (struct sppp*) ifp;
656	int i;
657
658	/* Remove the entry from the keepalive list. */
659	for (q = &spppq; (p = *q); q = &p->pp_next)
660		if (p == sp) {
661			*q = p->pp_next;
662			break;
663		}
664
665	/* Stop keepalive handler. */
666	if (! spppq)
667		untimeout (sppp_keepalive, 0);
668
669	for (i = 0; i < IDX_COUNT; i++)
670		untimeout((cps[i])->TO, (void *)sp);
671}
672
673/*
674 * Flush the interface output queue.
675 */
676void
677sppp_flush(struct ifnet *ifp)
678{
679	struct sppp *sp = (struct sppp*) ifp;
680
681	sppp_qflush (&sp->pp_if.if_snd);
682	sppp_qflush (&sp->pp_fastq);
683	sppp_qflush (&sp->pp_cpq);
684}
685
686/*
687 * Check if the output queue is empty.
688 */
689int
690sppp_isempty(struct ifnet *ifp)
691{
692	struct sppp *sp = (struct sppp*) ifp;
693	int empty, s;
694
695	s = splimp();
696	empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
697		!sp->pp_if.if_snd.ifq_head;
698	splx(s);
699	return (empty);
700}
701
702/*
703 * Get next packet to send.
704 */
705struct mbuf *
706sppp_dequeue(struct ifnet *ifp)
707{
708	struct sppp *sp = (struct sppp*) ifp;
709	struct mbuf *m;
710	int s;
711
712	s = splimp();
713	/*
714	 * Process only the control protocol queue until we are in
715	 * network phase.
716	 *
717	 * XXX Network phase itself is still not a sufficient test, we
718	 * normally should keep a separate queue for each supported
719	 * protocol family, and only serve these queues as the
720	 * respective NCPs were opened.  The simplistic logic used
721	 * here might cause some loss of network traffic while the
722	 * NCPs are being negotiated, in particular if the NCPs take a
723	 * long time to negotiate.
724	 *
725	 * Do always serve all three queues in Cisco mode.
726	 */
727	IF_DEQUEUE(&sp->pp_cpq, m);
728	if (m == NULL &&
729	    (sp->pp_phase == PHASE_NETWORK ||
730	     (sp->pp_flags & PP_CISCO) != 0)) {
731		IF_DEQUEUE(&sp->pp_fastq, m);
732		if (m == NULL)
733			IF_DEQUEUE (&sp->pp_if.if_snd, m);
734	}
735	splx(s);
736	return m;
737}
738
739/*
740 * Process an ioctl request.  Called on low priority level.
741 */
742int
743sppp_ioctl(struct ifnet *ifp, int cmd, void *data)
744{
745	struct ifreq *ifr = (struct ifreq*) data;
746	struct sppp *sp = (struct sppp*) ifp;
747	int s, going_up, going_down, newmode;
748
749	s = splimp();
750	switch (cmd) {
751	case SIOCAIFADDR:
752	case SIOCSIFDSTADDR:
753		break;
754
755	case SIOCSIFADDR:
756		if_up(ifp);
757		/* fall through... */
758
759	case SIOCSIFFLAGS:
760		going_up = ifp->if_flags & IFF_UP &&
761			(ifp->if_flags & IFF_RUNNING) == 0;
762		going_down = (ifp->if_flags & IFF_UP) == 0 &&
763			ifp->if_flags & IFF_RUNNING;
764		newmode = ifp->if_flags & (IFF_AUTO | IFF_PASSIVE);
765		if (newmode == (IFF_AUTO | IFF_PASSIVE)) {
766			/* sanity */
767			newmode = IFF_PASSIVE;
768			ifp->if_flags &= ~IFF_AUTO;
769		}
770
771		if (going_up || going_down)
772			lcp.Close(sp);
773		if (going_up && newmode == 0) {
774			/* neither auto-dial nor passive */
775			ifp->if_flags |= IFF_RUNNING;
776			if (!(sp->pp_flags & PP_CISCO))
777				lcp.Open(sp);
778		} else if (going_down) {
779			sppp_flush(ifp);
780			ifp->if_flags &= ~IFF_RUNNING;
781		}
782
783		break;
784
785#ifdef SIOCSIFMTU
786#ifndef ifr_mtu
787#define ifr_mtu ifr_metric
788#endif
789	case SIOCSIFMTU:
790		if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
791			return (EINVAL);
792		ifp->if_mtu = ifr->ifr_mtu;
793		break;
794#endif
795#ifdef SLIOCSETMTU
796	case SLIOCSETMTU:
797		if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
798			return (EINVAL);
799		ifp->if_mtu = *(short*)data;
800		break;
801#endif
802#ifdef SIOCGIFMTU
803	case SIOCGIFMTU:
804		ifr->ifr_mtu = ifp->if_mtu;
805		break;
806#endif
807#ifdef SLIOCGETMTU
808	case SLIOCGETMTU:
809		*(short*)data = ifp->if_mtu;
810		break;
811#endif
812	case SIOCADDMULTI:
813	case SIOCDELMULTI:
814		break;
815
816	default:
817		splx(s);
818		return (ENOTTY);
819	}
820	splx(s);
821	return (0);
822}
823
824
825/*
826 * Cisco framing implementation.
827 */
828
829/*
830 * Handle incoming Cisco keepalive protocol packets.
831 */
832static void
833sppp_cisco_input(struct sppp *sp, struct mbuf *m)
834{
835	STDDCL;
836	struct cisco_packet *h;
837	struct ifaddr *ifa;
838
839	if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
840		if (debug)
841			log(LOG_DEBUG,
842			    "%s%d: invalid cisco packet length: %d bytes\n",
843			    ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
844		return;
845	}
846	h = mtod (m, struct cisco_packet*);
847	if (debug)
848		log(LOG_DEBUG,
849		    "%s%d: cisco input: %d bytes "
850		    "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
851		    ifp->if_name, ifp->if_unit, m->m_pkthdr.len,
852		    ntohl (h->type), h->par1, h->par2, h->rel,
853		    h->time0, h->time1);
854	switch (ntohl (h->type)) {
855	default:
856		if (debug)
857			addlog("%s%d: unknown cisco packet type: 0x%lx\n",
858			       ifp->if_name, ifp->if_unit, ntohl (h->type));
859		break;
860	case CISCO_ADDR_REPLY:
861		/* Reply on address request, ignore */
862		break;
863	case CISCO_KEEPALIVE_REQ:
864		sp->pp_alivecnt = 0;
865		sp->pp_rseq = ntohl (h->par1);
866		if (sp->pp_seq == sp->pp_rseq) {
867			/* Local and remote sequence numbers are equal.
868			 * Probably, the line is in loopback mode. */
869			if (sp->pp_loopcnt >= MAXALIVECNT) {
870				printf ("%s%d: loopback\n",
871					ifp->if_name, ifp->if_unit);
872				sp->pp_loopcnt = 0;
873				if (ifp->if_flags & IFF_UP) {
874					if_down (ifp);
875					sppp_qflush (&sp->pp_cpq);
876				}
877			}
878			++sp->pp_loopcnt;
879
880			/* Generate new local sequence number */
881			sp->pp_seq ^= time.tv_sec ^ time.tv_usec;
882			break;
883		}
884			sp->pp_loopcnt = 0;
885		if (! (ifp->if_flags & IFF_UP) &&
886		    (ifp->if_flags & IFF_RUNNING)) {
887			ifp->if_flags |= IFF_UP;
888			printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
889		}
890		break;
891	case CISCO_ADDR_REQ:
892		for (ifa=ifp->if_addrhead.tqh_first; ifa;
893		     ifa=ifa->ifa_link.tqe_next)
894			if (ifa->ifa_addr->sa_family == AF_INET)
895				break;
896		if (! ifa) {
897			if (debug)
898				addlog("%s%d: unknown address for cisco request\n",
899				       ifp->if_name, ifp->if_unit);
900			return;
901		}
902		sppp_cisco_send (sp, CISCO_ADDR_REPLY,
903			ntohl (((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr),
904			ntohl (((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr.s_addr));
905		break;
906	}
907}
908
909/*
910 * Send Cisco keepalive packet.
911 */
912static void
913sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
914{
915	STDDCL;
916	struct ppp_header *h;
917	struct cisco_packet *ch;
918	struct mbuf *m;
919	u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
920
921	MGETHDR (m, M_DONTWAIT, MT_DATA);
922	if (! m)
923		return;
924	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
925	m->m_pkthdr.rcvif = 0;
926
927	h = mtod (m, struct ppp_header*);
928	h->address = CISCO_MULTICAST;
929	h->control = 0;
930	h->protocol = htons (CISCO_KEEPALIVE);
931
932	ch = (struct cisco_packet*) (h + 1);
933	ch->type = htonl (type);
934	ch->par1 = htonl (par1);
935	ch->par2 = htonl (par2);
936	ch->rel = -1;
937	ch->time0 = htons ((u_short) (t >> 16));
938	ch->time1 = htons ((u_short) t);
939
940	if (debug)
941		log(LOG_DEBUG,
942		    "%s%d: cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
943			ifp->if_name, ifp->if_unit, ntohl (ch->type), ch->par1,
944			ch->par2, ch->rel, ch->time0, ch->time1);
945
946	if (IF_QFULL (&sp->pp_cpq)) {
947		IF_DROP (&sp->pp_fastq);
948		IF_DROP (&ifp->if_snd);
949		m_freem (m);
950	} else
951		IF_ENQUEUE (&sp->pp_cpq, m);
952	if (! (ifp->if_flags & IFF_OACTIVE))
953		(*ifp->if_start) (ifp);
954	ifp->if_obytes += m->m_pkthdr.len + 3;
955}
956
957/*
958 * PPP protocol implementation.
959 */
960
961/*
962 * Send PPP control protocol packet.
963 */
964static void
965sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
966	     u_char ident, u_short len, void *data)
967{
968	STDDCL;
969	struct ppp_header *h;
970	struct lcp_header *lh;
971	struct mbuf *m;
972
973	if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
974		len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
975	MGETHDR (m, M_DONTWAIT, MT_DATA);
976	if (! m)
977		return;
978	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
979	m->m_pkthdr.rcvif = 0;
980
981	h = mtod (m, struct ppp_header*);
982	h->address = PPP_ALLSTATIONS;        /* broadcast address */
983	h->control = PPP_UI;                 /* Unnumbered Info */
984	h->protocol = htons (proto);         /* Link Control Protocol */
985
986	lh = (struct lcp_header*) (h + 1);
987	lh->type = type;
988	lh->ident = ident;
989	lh->len = htons (LCP_HEADER_LEN + len);
990	if (len)
991		bcopy (data, lh+1, len);
992
993	if (debug) {
994		log(LOG_DEBUG, "%s%d: %s output <%s id=0x%x len=%d",
995		    ifp->if_name, ifp->if_unit,
996		    sppp_proto_name(proto),
997		    sppp_cp_type_name (lh->type), lh->ident,
998		    ntohs (lh->len));
999		if (len)
1000			sppp_print_bytes ((u_char*) (lh+1), len);
1001		addlog(">\n");
1002	}
1003	if (IF_QFULL (&sp->pp_cpq)) {
1004		IF_DROP (&sp->pp_fastq);
1005		IF_DROP (&ifp->if_snd);
1006		m_freem (m);
1007		++ifp->if_oerrors;
1008	} else
1009		IF_ENQUEUE (&sp->pp_cpq, m);
1010	if (! (ifp->if_flags & IFF_OACTIVE))
1011		(*ifp->if_start) (ifp);
1012	ifp->if_obytes += m->m_pkthdr.len + 3;
1013}
1014
1015/*
1016 * Handle incoming PPP control protocol packets.
1017 */
1018static void
1019sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1020{
1021	STDDCL;
1022	struct lcp_header *h;
1023	int len = m->m_pkthdr.len;
1024	int rv;
1025	u_char *p;
1026
1027	if (len < 4) {
1028		if (debug)
1029			log(LOG_DEBUG,
1030			    "%s%d: %s invalid packet length: %d bytes\n",
1031			    ifp->if_name, ifp->if_unit, cp->name, len);
1032		return;
1033	}
1034	h = mtod (m, struct lcp_header*);
1035	if (debug) {
1036		log(LOG_DEBUG,
1037		    "%s%d: %s input(%s): <%s id=0x%x len=%d",
1038		    ifp->if_name, ifp->if_unit, cp->name,
1039		    sppp_state_name(sp->state[cp->protoidx]),
1040		    sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1041		if (len > 4)
1042			sppp_print_bytes ((u_char*) (h+1), len-4);
1043		addlog(">\n");
1044	}
1045	if (len > ntohs (h->len))
1046		len = ntohs (h->len);
1047	switch (h->type) {
1048	case CONF_REQ:
1049		if (len < 4) {
1050			if (debug)
1051				addlog("%s%d: %s invalid conf-req length %d\n",
1052				       ifp->if_name, ifp->if_unit, cp->name,
1053				       len);
1054			++ifp->if_ierrors;
1055			break;
1056		}
1057		rv = (cp->RCR)(sp, h, len);
1058		switch (sp->state[cp->protoidx]) {
1059		case STATE_OPENED:
1060			(cp->tld)(sp);
1061			(cp->scr)(sp);
1062			/* fall through... */
1063		case STATE_ACK_SENT:
1064		case STATE_REQ_SENT:
1065			sppp_cp_change_state(cp, sp, rv?
1066					     STATE_ACK_SENT: STATE_REQ_SENT);
1067			break;
1068		case STATE_CLOSING:
1069		case STATE_STOPPING:
1070			break;
1071		case STATE_STOPPED:
1072			sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1073			(cp->scr)(sp);
1074			sppp_cp_change_state(cp, sp, rv?
1075					     STATE_ACK_SENT: STATE_REQ_SENT);
1076			break;
1077		case STATE_CLOSED:
1078			sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1079				     0, 0);
1080			break;
1081		case STATE_ACK_RCVD:
1082			if (rv) {
1083				sppp_cp_change_state(cp, sp, STATE_OPENED);
1084				if (debug)
1085					log(LOG_DEBUG, "%s%d: %s tlu\n",
1086					    ifp->if_name, ifp->if_unit,
1087					    cp->name);
1088				(cp->tlu)(sp);
1089			} else
1090				sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1091			break;
1092		default:
1093			printf("%s%d: %s illegal %s in state %s\n",
1094			       ifp->if_name, ifp->if_unit, cp->name,
1095			       sppp_cp_type_name(h->type),
1096			       sppp_state_name(sp->state[cp->protoidx]));
1097			++ifp->if_ierrors;
1098		}
1099		break;
1100	case CONF_ACK:
1101		if (h->ident != sp->confid[cp->protoidx]) {
1102			if (debug)
1103				addlog("%s%d: %s id mismatch 0x%x != 0x%x\n",
1104				       ifp->if_name, ifp->if_unit, cp->name,
1105				       h->ident, sp->confid[cp->protoidx]);
1106			++ifp->if_ierrors;
1107			break;
1108		}
1109		switch (sp->state[cp->protoidx]) {
1110		case STATE_CLOSED:
1111		case STATE_STOPPED:
1112			sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1113			break;
1114		case STATE_CLOSING:
1115		case STATE_STOPPING:
1116			break;
1117		case STATE_REQ_SENT:
1118			sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1119			sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1120			break;
1121		case STATE_OPENED:
1122			(cp->tld)(sp);
1123			/* fall through */
1124		case STATE_ACK_RCVD:
1125			(cp->scr)(sp);
1126			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1127			break;
1128		case STATE_ACK_SENT:
1129			sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1130			sppp_cp_change_state(cp, sp, STATE_OPENED);
1131			if (debug)
1132				addlog("%s%d: %s tlu\n",
1133				       ifp->if_name, ifp->if_unit, cp->name);
1134			(cp->tlu)(sp);
1135			break;
1136		default:
1137			printf("%s%d: %s illegal %s in state %s\n",
1138			       ifp->if_name, ifp->if_unit, cp->name,
1139			       sppp_cp_type_name(h->type),
1140			       sppp_state_name(sp->state[cp->protoidx]));
1141			++ifp->if_ierrors;
1142		}
1143		break;
1144	case CONF_NAK:
1145	case CONF_REJ:
1146		if (h->ident != sp->confid[cp->protoidx]) {
1147			if (debug)
1148				addlog("%s%d: %s id mismatch 0x%x != 0x%x\n",
1149				       ifp->if_name, ifp->if_unit, cp->name,
1150				       h->ident, sp->confid[cp->protoidx]);
1151			++ifp->if_ierrors;
1152			break;
1153		}
1154		if (h->type == CONF_NAK)
1155			(cp->RCN_nak)(sp, h, len);
1156		else /* CONF_REJ */
1157			(cp->RCN_rej)(sp, h, len);
1158
1159		switch (sp->state[cp->protoidx]) {
1160		case STATE_CLOSED:
1161		case STATE_STOPPED:
1162			sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1163			break;
1164		case STATE_REQ_SENT:
1165		case STATE_ACK_SENT:
1166			sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1167			(cp->scr)(sp);
1168			break;
1169		case STATE_OPENED:
1170			(cp->tld)(sp);
1171			/* fall through */
1172		case STATE_ACK_RCVD:
1173			sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
1174			(cp->scr)(sp);
1175			break;
1176		case STATE_CLOSING:
1177		case STATE_STOPPING:
1178			break;
1179		default:
1180			printf("%s%d: %s illegal %s in state %s\n",
1181			       ifp->if_name, ifp->if_unit, cp->name,
1182			       sppp_cp_type_name(h->type),
1183			       sppp_state_name(sp->state[cp->protoidx]));
1184			++ifp->if_ierrors;
1185		}
1186		break;
1187
1188	case TERM_REQ:
1189		switch (sp->state[cp->protoidx]) {
1190		case STATE_ACK_RCVD:
1191		case STATE_ACK_SENT:
1192			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1193			/* fall through */
1194		case STATE_CLOSED:
1195		case STATE_STOPPED:
1196		case STATE_CLOSING:
1197		case STATE_STOPPING:
1198		case STATE_REQ_SENT:
1199		  sta:
1200			/* Send Terminate-Ack packet. */
1201			if (debug)
1202				log(LOG_DEBUG, "%s%d: %s send terminate-ack\n",
1203				    ifp->if_name, ifp->if_unit, cp->name);
1204			sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1205			break;
1206		case STATE_OPENED:
1207			(cp->tld)(sp);
1208			sp->rst_counter[cp->protoidx] = 0;
1209			sppp_cp_change_state(cp, sp, STATE_STOPPING);
1210			goto sta;
1211			break;
1212		default:
1213			printf("%s%d: %s illegal %s in state %s\n",
1214			       ifp->if_name, ifp->if_unit, cp->name,
1215			       sppp_cp_type_name(h->type),
1216			       sppp_state_name(sp->state[cp->protoidx]));
1217			++ifp->if_ierrors;
1218		}
1219		break;
1220	case TERM_ACK:
1221		switch (sp->state[cp->protoidx]) {
1222		case STATE_CLOSED:
1223		case STATE_STOPPED:
1224		case STATE_REQ_SENT:
1225		case STATE_ACK_SENT:
1226			break;
1227		case STATE_CLOSING:
1228			(cp->tlf)(sp);
1229			sppp_cp_change_state(cp, sp, STATE_CLOSED);
1230			break;
1231		case STATE_STOPPING:
1232			(cp->tlf)(sp);
1233			sppp_cp_change_state(cp, sp, STATE_STOPPED);
1234			break;
1235		case STATE_ACK_RCVD:
1236			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1237			break;
1238		case STATE_OPENED:
1239			(cp->tld)(sp);
1240			(cp->scr)(sp);
1241			sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1242			break;
1243		default:
1244			printf("%s%d: %s illegal %s in state %s\n",
1245			       ifp->if_name, ifp->if_unit, cp->name,
1246			       sppp_cp_type_name(h->type),
1247			       sppp_state_name(sp->state[cp->protoidx]));
1248			++ifp->if_ierrors;
1249		}
1250		break;
1251	case CODE_REJ:
1252	case PROTO_REJ:
1253		/* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1254		switch (sp->state[cp->protoidx]) {
1255		case STATE_CLOSED:
1256		case STATE_STOPPED:
1257		case STATE_REQ_SENT:
1258		case STATE_ACK_SENT:
1259		case STATE_CLOSING:
1260		case STATE_STOPPING:
1261		case STATE_OPENED:
1262			break;
1263		case STATE_ACK_RCVD:
1264			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1265			break;
1266		default:
1267			printf("%s%d: %s illegal %s in state %s\n",
1268			       ifp->if_name, ifp->if_unit, cp->name,
1269			       sppp_cp_type_name(h->type),
1270			       sppp_state_name(sp->state[cp->protoidx]));
1271			++ifp->if_ierrors;
1272		}
1273		break;
1274	case DISC_REQ:
1275		if (cp->proto != PPP_LCP)
1276			goto illegal;
1277		/* Discard the packet. */
1278		break;
1279	case ECHO_REQ:
1280		if (cp->proto != PPP_LCP)
1281			goto illegal;
1282		if (sp->state[cp->protoidx] != STATE_OPENED) {
1283			if (debug)
1284				addlog("%s%d: lcp echo req but lcp closed\n",
1285				       ifp->if_name, ifp->if_unit);
1286			++ifp->if_ierrors;
1287			break;
1288		}
1289		if (len < 8) {
1290			if (debug)
1291				addlog("%s%d: invalid lcp echo request "
1292				       "packet length: %d bytes\n",
1293				       ifp->if_name, ifp->if_unit, len);
1294			break;
1295		}
1296		if (ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1297			/* Line loopback mode detected. */
1298			printf("%s%d: loopback\n", ifp->if_name, ifp->if_unit);
1299			if_down (ifp);
1300			sppp_qflush (&sp->pp_cpq);
1301
1302			/* Shut down the PPP link. */
1303			/* XXX */
1304			lcp.Down(sp);
1305			lcp.Up(sp);
1306			break;
1307		}
1308		*(long*)(h+1) = htonl (sp->lcp.magic);
1309		if (debug)
1310			addlog("%s%d: got lcp echo req, sending echo rep\n",
1311			       ifp->if_name, ifp->if_unit);
1312		sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1313		break;
1314	case ECHO_REPLY:
1315		if (cp->proto != PPP_LCP)
1316			goto illegal;
1317		if (h->ident != sp->lcp.echoid) {
1318			++ifp->if_ierrors;
1319			break;
1320		}
1321		if (len < 8) {
1322			if (debug)
1323				addlog("%s%d: lcp invalid echo reply "
1324				       "packet length: %d bytes\n",
1325				       ifp->if_name, ifp->if_unit, len);
1326			break;
1327		}
1328		if (debug)
1329			addlog("%s%d: lcp got echo rep\n",
1330			       ifp->if_name, ifp->if_unit);
1331		if (ntohl (*(long*)(h+1)) != sp->lcp.magic)
1332			sp->pp_alivecnt = 0;
1333		break;
1334	default:
1335		/* Unknown packet type -- send Code-Reject packet. */
1336	  illegal:
1337		if (debug)
1338			addlog("%s%d: %c send code-rej for 0x%x\n",
1339			       ifp->if_name, ifp->if_unit, cp->name, h->type);
1340		sppp_cp_send(sp, cp->proto, CODE_REJ, ++sp->pp_seq,
1341			     m->m_pkthdr.len, h);
1342		++ifp->if_ierrors;
1343	}
1344}
1345
1346
1347/*
1348 * The generic part of all Up/Down/Open/Close/TO event handlers.
1349 * Basically, the state transition handling in the automaton.
1350 */
1351static void
1352sppp_up_event(const struct cp *cp, struct sppp *sp)
1353{
1354	STDDCL;
1355
1356	if (debug)
1357		log(LOG_DEBUG, "%s%d: %s up(%s)\n",
1358		    ifp->if_name, ifp->if_unit, cp->name,
1359		    sppp_state_name(sp->state[cp->protoidx]));
1360
1361	switch (sp->state[cp->protoidx]) {
1362	case STATE_INITIAL:
1363		sppp_cp_change_state(cp, sp, STATE_CLOSED);
1364		break;
1365	case STATE_STARTING:
1366		sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1367		(cp->scr)(sp);
1368		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1369		break;
1370	default:
1371		printf("%s%d: %s illegal up in state %s\n",
1372		       ifp->if_name, ifp->if_unit, cp->name,
1373		       sppp_state_name(sp->state[cp->protoidx]));
1374	}
1375}
1376
1377static void
1378sppp_down_event(const struct cp *cp, struct sppp *sp)
1379{
1380	STDDCL;
1381
1382	if (debug)
1383		log(LOG_DEBUG, "%s%d: %s down(%s)\n",
1384		    ifp->if_name, ifp->if_unit, cp->name,
1385		    sppp_state_name(sp->state[cp->protoidx]));
1386
1387	switch (sp->state[cp->protoidx]) {
1388	case STATE_CLOSED:
1389	case STATE_CLOSING:
1390		sppp_cp_change_state(cp, sp, STATE_INITIAL);
1391		break;
1392	case STATE_STOPPED:
1393		(cp->tls)(sp);
1394		/* fall through */
1395	case STATE_STOPPING:
1396	case STATE_REQ_SENT:
1397	case STATE_ACK_RCVD:
1398	case STATE_ACK_SENT:
1399		sppp_cp_change_state(cp, sp, STATE_STARTING);
1400		break;
1401	case STATE_OPENED:
1402		(cp->tld)(sp);
1403		sppp_cp_change_state(cp, sp, STATE_STARTING);
1404		break;
1405	default:
1406		printf("%s%d: %s illegal down in state %s\n",
1407		       ifp->if_name, ifp->if_unit, cp->name,
1408		       sppp_state_name(sp->state[cp->protoidx]));
1409	}
1410}
1411
1412
1413static void
1414sppp_open_event(const struct cp *cp, struct sppp *sp)
1415{
1416	STDDCL;
1417
1418	if (debug)
1419		log(LOG_DEBUG, "%s%d: %s open(%s)\n",
1420		    ifp->if_name, ifp->if_unit, cp->name,
1421		    sppp_state_name(sp->state[cp->protoidx]));
1422
1423	switch (sp->state[cp->protoidx]) {
1424	case STATE_INITIAL:
1425		(cp->tls)(sp);
1426		sppp_cp_change_state(cp, sp, STATE_STARTING);
1427		break;
1428	case STATE_STARTING:
1429		break;
1430	case STATE_CLOSED:
1431		sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1432		(cp->scr)(sp);
1433		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1434		break;
1435	case STATE_STOPPED:
1436	case STATE_STOPPING:
1437	case STATE_REQ_SENT:
1438	case STATE_ACK_RCVD:
1439	case STATE_ACK_SENT:
1440	case STATE_OPENED:
1441		break;
1442	case STATE_CLOSING:
1443		sppp_cp_change_state(cp, sp, STATE_STOPPING);
1444		break;
1445	}
1446}
1447
1448
1449static void
1450sppp_close_event(const struct cp *cp, struct sppp *sp)
1451{
1452	STDDCL;
1453
1454	if (debug)
1455		log(LOG_DEBUG, "%s%d: %s close(%s)\n",
1456		    ifp->if_name, ifp->if_unit, cp->name,
1457		    sppp_state_name(sp->state[cp->protoidx]));
1458
1459	switch (sp->state[cp->protoidx]) {
1460	case STATE_INITIAL:
1461	case STATE_CLOSED:
1462	case STATE_CLOSING:
1463		break;
1464	case STATE_STARTING:
1465		(cp->tlf)(sp);
1466		sppp_cp_change_state(cp, sp, STATE_INITIAL);
1467		break;
1468	case STATE_STOPPED:
1469		sppp_cp_change_state(cp, sp, STATE_CLOSED);
1470		break;
1471	case STATE_STOPPING:
1472		sppp_cp_change_state(cp, sp, STATE_CLOSING);
1473		break;
1474	case STATE_OPENED:
1475		(cp->tld)(sp);
1476		/* fall through */
1477	case STATE_REQ_SENT:
1478	case STATE_ACK_RCVD:
1479	case STATE_ACK_SENT:
1480		sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
1481		sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq, 0, 0);
1482		sppp_cp_change_state(cp, sp, STATE_CLOSING);
1483		break;
1484	}
1485}
1486
1487static void
1488sppp_to_event(const struct cp *cp, struct sppp *sp)
1489{
1490	STDDCL;
1491	int s;
1492
1493	s = splimp();
1494	if (debug)
1495		log(LOG_DEBUG, "%s%d: %s TO(%s) rst_counter = %d\n",
1496		    ifp->if_name, ifp->if_unit, cp->name,
1497		    sppp_state_name(sp->state[cp->protoidx]),
1498		    sp->rst_counter[cp->protoidx]);
1499
1500	if (--sp->rst_counter[cp->protoidx] < 0)
1501		/* TO- event */
1502		switch (sp->state[cp->protoidx]) {
1503		case STATE_CLOSING:
1504			(cp->tlf)(sp);
1505			sppp_cp_change_state(cp, sp, STATE_CLOSED);
1506			break;
1507		case STATE_STOPPING:
1508			(cp->tlf)(sp);
1509			sppp_cp_change_state(cp, sp, STATE_STOPPED);
1510			break;
1511		case STATE_REQ_SENT:
1512		case STATE_ACK_RCVD:
1513		case STATE_ACK_SENT:
1514			(cp->tlf)(sp);
1515			sppp_cp_change_state(cp, sp, STATE_STOPPED);
1516			break;
1517		}
1518	else
1519		/* TO+ event */
1520		switch (sp->state[cp->protoidx]) {
1521		case STATE_CLOSING:
1522		case STATE_STOPPING:
1523			sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq,
1524				     0, 0);
1525			timeout(cp->TO, (void *)sp, sp->lcp.timeout);
1526			break;
1527		case STATE_REQ_SENT:
1528		case STATE_ACK_RCVD:
1529			(cp->scr)(sp);
1530			/* sppp_cp_change_state() will restart the timer */
1531			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1532			break;
1533		case STATE_ACK_SENT:
1534			(cp->scr)(sp);
1535			timeout(cp->TO, (void *)sp, sp->lcp.timeout);
1536			break;
1537		}
1538
1539	splx(s);
1540}
1541
1542/*
1543 * Change the state of a control protocol in the state automaton.
1544 * Takes care of starting/stopping the restart timer.
1545 */
1546void
1547sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
1548{
1549	sp->state[cp->protoidx] = newstate;
1550
1551	untimeout(cp->TO, (void *)sp);
1552	switch (newstate) {
1553	case STATE_INITIAL:
1554	case STATE_STARTING:
1555	case STATE_CLOSED:
1556	case STATE_STOPPED:
1557	case STATE_OPENED:
1558		break;
1559	case STATE_CLOSING:
1560	case STATE_STOPPING:
1561	case STATE_REQ_SENT:
1562	case STATE_ACK_RCVD:
1563	case STATE_ACK_SENT:
1564		timeout(cp->TO, (void *)sp, sp->lcp.timeout);
1565		break;
1566	}
1567}
1568/*
1569 *--------------------------------------------------------------------------*
1570 *                                                                          *
1571 *                         The LCP implementation.                          *
1572 *                                                                          *
1573 *--------------------------------------------------------------------------*
1574 */
1575static void
1576sppp_lcp_init(struct sppp *sp)
1577{
1578	sp->lcp.opts = (1 << LCP_OPT_MAGIC);
1579	sp->lcp.magic = 0;
1580	sp->state[IDX_LCP] = STATE_INITIAL;
1581	sp->fail_counter[IDX_LCP] = 0;
1582	sp->lcp.protos = 0;
1583	sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
1584
1585	/*
1586	 * Initialize counters and timeout values.  Note that we don't
1587	 * use the 3 seconds suggested in RFC 1661 since we are likely
1588	 * running on a fast link.  XXX We should probably implement
1589	 * the exponential backoff option.  Note that these values are
1590	 * relevant for all control protocols, not just LCP only.
1591	 */
1592	sp->lcp.timeout = 1 * hz;
1593	sp->lcp.max_terminate = 2;
1594	sp->lcp.max_configure = 10;
1595	sp->lcp.max_failure = 10;
1596}
1597
1598static void
1599sppp_lcp_up(struct sppp *sp)
1600{
1601	STDDCL;
1602
1603	/*
1604	 * If this interface is passive or dial-on-demand, it means
1605	 * we've got in incoming call.  Activate the interface.
1606	 */
1607	if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
1608		if (debug)
1609			log(LOG_DEBUG,
1610			    "%s%d: Up event (incoming call)\n",
1611			    ifp->if_name, ifp->if_unit);
1612		ifp->if_flags |= IFF_RUNNING;
1613		lcp.Open(sp);
1614	}
1615
1616	sppp_up_event(&lcp, sp);
1617}
1618
1619static void
1620sppp_lcp_down(struct sppp *sp)
1621{
1622	STDDCL;
1623
1624	sppp_down_event(&lcp, sp);
1625
1626	/*
1627	 * If this is neither a dial-on-demand nor a passive
1628	 * interface, simulate an ``ifconfig down'' action, so the
1629	 * administrator can force a redial by another ``ifconfig
1630	 * up''.  XXX For leased line operation, should we immediately
1631	 * try to reopen the connection here?
1632	 */
1633	if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
1634		log(LOG_INFO,
1635		    "%s%d: Down event (carrier loss), taking interface down.\n",
1636		    ifp->if_name, ifp->if_unit);
1637		if_down(ifp);
1638	} else {
1639		if (debug)
1640			log(LOG_DEBUG,
1641			    "%s%d: Down event (carrier loss)\n",
1642			    ifp->if_name, ifp->if_unit);
1643	}
1644	lcp.Close(sp);
1645	ifp->if_flags &= ~IFF_RUNNING;
1646}
1647
1648static void
1649sppp_lcp_open(struct sppp *sp)
1650{
1651	sppp_open_event(&lcp, sp);
1652}
1653
1654static void
1655sppp_lcp_close(struct sppp *sp)
1656{
1657	sppp_close_event(&lcp, sp);
1658}
1659
1660static void
1661sppp_lcp_TO(void *cookie)
1662{
1663	sppp_to_event(&lcp, (struct sppp *)cookie);
1664}
1665
1666/*
1667 * Analyze a configure request.  Return true if it was agreeable, and
1668 * caused action sca, false if it has been rejected or nak'ed, and
1669 * caused action scn.  (The return value is used to make the state
1670 * transition decision in the state automaton.)
1671 */
1672static int
1673sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
1674{
1675	STDDCL;
1676	u_char *buf, *r, *p;
1677	int origlen, rlen;
1678	u_long nmagic;
1679
1680	len -= 4;
1681	origlen = len;
1682	buf = r = malloc (len, M_TEMP, M_NOWAIT);
1683	if (! buf)
1684		return (0);
1685
1686	if (debug)
1687		log(LOG_DEBUG, "%s%d: lcp parse opts: ",
1688		    ifp->if_name, ifp->if_unit);
1689
1690	/* pass 1: check for things that need to be rejected */
1691	p = (void*) (h+1);
1692	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1693		if (debug)
1694			addlog(" %s ", sppp_lcp_opt_name(*p));
1695		switch (*p) {
1696		case LCP_OPT_MAGIC:
1697			/* Magic number. */
1698			/* fall through, both are same length */
1699		case LCP_OPT_ASYNC_MAP:
1700			/* Async control character map. */
1701			if (len >= 6 || p[1] == 6)
1702				continue;
1703			if (debug)
1704				addlog("[invalid] ");
1705			break;
1706		case LCP_OPT_MRU:
1707			/* Maximum receive unit. */
1708			if (len >= 4 && p[1] == 4)
1709				continue;
1710			if (debug)
1711				addlog("[invalid] ");
1712			break;
1713		default:
1714			/* Others not supported. */
1715			if (debug)
1716				addlog("[rej] ");
1717			break;
1718		}
1719		/* Add the option to rejected list. */
1720		bcopy (p, r, p[1]);
1721		r += p[1];
1722		rlen += p[1];
1723	}
1724	if (rlen) {
1725		if (debug)
1726			addlog(" send conf-rej\n");
1727		sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
1728		return 0;
1729	} else if (debug)
1730		addlog("\n");
1731
1732	/*
1733	 * pass 2: check for option values that are unacceptable and
1734	 * thus require to be nak'ed.
1735	 */
1736	if (debug)
1737		log(LOG_DEBUG, "%s%d: lcp parse opt values: ",
1738		    ifp->if_name, ifp->if_unit);
1739
1740	p = (void*) (h+1);
1741	len = origlen;
1742	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1743		if (debug)
1744			addlog(" %s ", sppp_lcp_opt_name(*p));
1745		switch (*p) {
1746		case LCP_OPT_MAGIC:
1747			/* Magic number -- extract. */
1748			nmagic = (u_long)p[2] << 24 |
1749				(u_long)p[3] << 16 | p[4] << 8 | p[5];
1750			if (nmagic != sp->lcp.magic) {
1751				if (debug)
1752					addlog("0x%x ", nmagic);
1753				continue;
1754			}
1755			/*
1756			 * Local and remote magics equal -- loopback?
1757			 */
1758			if (sp->pp_loopcnt >= MAXALIVECNT*5) {
1759				printf ("\n%s%d: loopback\n",
1760					ifp->if_name, ifp->if_unit);
1761				sp->pp_loopcnt = 0;
1762				if (ifp->if_flags & IFF_UP) {
1763					if_down(ifp);
1764					sppp_qflush(&sp->pp_cpq);
1765					/* XXX ? */
1766					lcp.Down(sp);
1767					lcp.Up(sp);
1768				}
1769			} else if (debug)
1770				addlog("[glitch] ");
1771			++sp->pp_loopcnt;
1772			/*
1773			 * We negate our magic here, and NAK it.  If
1774			 * we see it later in an NAK packet, we
1775			 * suggest a new one.
1776			 */
1777			nmagic = ~sp->lcp.magic;
1778			/* Gonna NAK it. */
1779			p[2] = nmagic >> 24;
1780			p[3] = nmagic >> 16;
1781			p[4] = nmagic >> 8;
1782			p[5] = nmagic;
1783			break;
1784
1785		case LCP_OPT_ASYNC_MAP:
1786			/* Async control character map -- check to be zero. */
1787			if (! p[2] && ! p[3] && ! p[4] && ! p[5]) {
1788				if (debug)
1789					addlog("[empty] ");
1790				continue;
1791			}
1792			if (debug)
1793				addlog("[non-empty] ");
1794			/* suggest a zero one */
1795			p[2] = p[3] = p[4] = p[5] = 0;
1796			break;
1797
1798		case LCP_OPT_MRU:
1799			/*
1800			 * Maximum receive unit.  Always agreeable,
1801			 * but ignored by now.
1802			 */
1803			sp->lcp.their_mru = p[2] * 256 + p[3];
1804			if (debug)
1805				addlog("%d ", sp->lcp.their_mru);
1806			continue;
1807		}
1808		/* Add the option to nak'ed list. */
1809		bcopy (p, r, p[1]);
1810		r += p[1];
1811		rlen += p[1];
1812	}
1813	if (rlen) {
1814		if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
1815			if (debug)
1816				addlog(" max_failure (%d) exceeded, "
1817				       "send conf-rej\n",
1818				       sp->lcp.max_failure);
1819			sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
1820		} else {
1821			if (debug)
1822				addlog(" send conf-nak\n");
1823			sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
1824		}
1825		return 0;
1826	} else {
1827		if (debug)
1828			addlog(" send conf-ack\n");
1829		sp->fail_counter[IDX_LCP] = 0;
1830		sp->pp_loopcnt = 0;
1831		sppp_cp_send (sp, PPP_LCP, CONF_ACK,
1832			      h->ident, origlen, h+1);
1833	}
1834
1835	free (buf, M_TEMP);
1836	return (rlen == 0);
1837}
1838
1839/*
1840 * Analyze the LCP Configure-Reject option list, and adjust our
1841 * negotiation.
1842 */
1843static void
1844sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
1845{
1846	STDDCL;
1847	u_char *buf, *p;
1848
1849	len -= 4;
1850	buf = malloc (len, M_TEMP, M_NOWAIT);
1851	if (!buf)
1852		return;
1853
1854	if (debug)
1855		log(LOG_DEBUG, "%s%d: lcp rej opts: ",
1856		    ifp->if_name, ifp->if_unit);
1857
1858	p = (void*) (h+1);
1859	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
1860		if (debug)
1861			addlog(" %s ", sppp_lcp_opt_name(*p));
1862		switch (*p) {
1863		case LCP_OPT_MAGIC:
1864			/* Magic number -- can't use it, use 0 */
1865			sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
1866			sp->lcp.magic = 0;
1867			break;
1868		case LCP_OPT_MRU:
1869			/*
1870			 * Should not be rejected anyway, since we only
1871			 * negotiate a MRU if explicitly requested by
1872			 * peer.
1873			 */
1874			sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
1875			break;
1876		}
1877	}
1878	if (debug)
1879		addlog("\n");
1880	free (buf, M_TEMP);
1881	return;
1882}
1883
1884/*
1885 * Analyze the LCP Configure-NAK option list, and adjust our
1886 * negotiation.
1887 */
1888static void
1889sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
1890{
1891	STDDCL;
1892	u_char *buf, *p;
1893	u_long magic;
1894
1895	len -= 4;
1896	buf = malloc (len, M_TEMP, M_NOWAIT);
1897	if (!buf)
1898		return;
1899
1900	if (debug)
1901		log(LOG_DEBUG, "%s%d: lcp nak opts: ",
1902		    ifp->if_name, ifp->if_unit);
1903
1904	p = (void*) (h+1);
1905	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
1906		if (debug)
1907			addlog(" %s ", sppp_lcp_opt_name(*p));
1908		switch (*p) {
1909		case LCP_OPT_MAGIC:
1910			/* Magic number -- renegotiate */
1911			if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1912			    len >= 6 && p[1] == 6) {
1913				magic = (u_long)p[2] << 24 |
1914					(u_long)p[3] << 16 | p[4] << 8 | p[5];
1915				/*
1916				 * If the remote magic is our negated one,
1917				 * this looks like a loopback problem.
1918				 * Suggest a new magic to make sure.
1919				 */
1920				if (magic == ~sp->lcp.magic) {
1921					if (debug)
1922						addlog("magic glitch ");
1923					sp->lcp.magic += time.tv_sec + time.tv_usec;
1924				} else {
1925					sp->lcp.magic = magic;
1926					if (debug)
1927						addlog("%d ");
1928				}
1929			}
1930			break;
1931		case LCP_OPT_MRU:
1932			/*
1933			 * Peer wants to advise us to negotiate an MRU.
1934			 * Agree on it if it's reasonable, or use
1935			 * default otherwise.
1936			 */
1937			if (len >= 4 && p[1] == 4) {
1938				u_int mru = p[2] * 256 + p[3];
1939				if (debug)
1940					addlog("%d ", mru);
1941				if (mru < PP_MTU || mru > PP_MAX_MRU)
1942					mru = PP_MTU;
1943				sp->lcp.mru = mru;
1944				sp->lcp.opts |= (1 << LCP_OPT_MRU);
1945			}
1946			break;
1947		}
1948	}
1949	if (debug)
1950		addlog("\n");
1951	free (buf, M_TEMP);
1952	return;
1953}
1954
1955static void
1956sppp_lcp_tlu(struct sppp *sp)
1957{
1958	STDDCL;
1959	int i;
1960	u_long mask;
1961
1962	/* XXX ? */
1963	if (! (ifp->if_flags & IFF_UP) &&
1964	    (ifp->if_flags & IFF_RUNNING)) {
1965		/* Coming out of loopback mode. */
1966		if_up(ifp);
1967		printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
1968	}
1969
1970	for (i = 0; i < IDX_COUNT; i++)
1971		if ((cps[i])->flags & CP_QUAL)
1972			(cps[i])->Open(sp);
1973
1974	if (/* require authentication XXX */ 0)
1975		sp->pp_phase = PHASE_AUTHENTICATE;
1976	else
1977		sp->pp_phase = PHASE_NETWORK;
1978
1979	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
1980	    sppp_phase_name(sp->pp_phase));
1981
1982	if (sp->pp_phase == PHASE_AUTHENTICATE) {
1983		for (i = 0; i < IDX_COUNT; i++)
1984			if ((cps[i])->flags & CP_AUTH)
1985				(cps[i])->Open(sp);
1986	} else {
1987		/* Notify all NCPs. */
1988		for (i = 0; i < IDX_COUNT; i++)
1989			if ((cps[i])->flags & CP_NCP)
1990				(cps[i])->Open(sp);
1991	}
1992
1993	/* Send Up events to all started protos. */
1994	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
1995		if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0)
1996			(cps[i])->Up(sp);
1997
1998	if (sp->pp_phase == PHASE_NETWORK)
1999		/* if no NCP is starting, close down */
2000		sppp_lcp_check(sp);
2001}
2002
2003static void
2004sppp_lcp_tld(struct sppp *sp)
2005{
2006	STDDCL;
2007	int i;
2008	u_long mask;
2009
2010	sp->pp_phase = PHASE_TERMINATE;
2011
2012	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2013	    sppp_phase_name(sp->pp_phase));
2014
2015	/*
2016	 * Take upper layers down.  We send the Down event first and
2017	 * the Close second to prevent the upper layers from sending
2018	 * ``a flurry of terminate-request packets'', as the RFC
2019	 * describes it.
2020	 */
2021	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2022		if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0) {
2023			(cps[i])->Down(sp);
2024			(cps[i])->Close(sp);
2025		}
2026}
2027
2028static void
2029sppp_lcp_tls(struct sppp *sp)
2030{
2031	STDDCL;
2032
2033	sp->pp_phase = PHASE_ESTABLISH;
2034
2035	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2036	    sppp_phase_name(sp->pp_phase));
2037
2038	/* Notify lower layer if desired. */
2039	if (sp->pp_tls)
2040		(sp->pp_tls)(sp);
2041}
2042
2043static void
2044sppp_lcp_tlf(struct sppp *sp)
2045{
2046	STDDCL;
2047
2048	sp->pp_phase = PHASE_DEAD;
2049	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2050	    sppp_phase_name(sp->pp_phase));
2051
2052	/* Notify lower layer if desired. */
2053	if (sp->pp_tlf)
2054		(sp->pp_tlf)(sp);
2055}
2056
2057static void
2058sppp_lcp_scr(struct sppp *sp)
2059{
2060	char opt[6 /* magicnum */ + 4 /* mru */];
2061	int i = 0;
2062
2063	if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2064		if (! sp->lcp.magic)
2065			sp->lcp.magic = time.tv_sec + time.tv_usec;
2066		opt[i++] = LCP_OPT_MAGIC;
2067		opt[i++] = 6;
2068		opt[i++] = sp->lcp.magic >> 24;
2069		opt[i++] = sp->lcp.magic >> 16;
2070		opt[i++] = sp->lcp.magic >> 8;
2071		opt[i++] = sp->lcp.magic;
2072	}
2073
2074	if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2075		opt[i++] = LCP_OPT_MRU;
2076		opt[i++] = 4;
2077		opt[i++] = sp->lcp.mru >> 8;
2078		opt[i++] = sp->lcp.mru;
2079	}
2080
2081	sp->confid[IDX_LCP] = ++sp->pp_seq;
2082	sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2083}
2084
2085/*
2086 * Re-check the open NCPs and see if we should terminate the link.
2087 * Called by the NCPs during their tlf action handling.
2088 */
2089static void
2090sppp_lcp_check(struct sppp *sp)
2091{
2092	int i, mask;
2093
2094	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2095		if (sp->lcp.protos & mask && (cps[i])->flags & CP_NCP)
2096			return;
2097	lcp.Close(sp);
2098}
2099/*
2100 *--------------------------------------------------------------------------*
2101 *                                                                          *
2102 *                        The IPCP implementation.                          *
2103 *                                                                          *
2104 *--------------------------------------------------------------------------*
2105 */
2106
2107static void
2108sppp_ipcp_init(struct sppp *sp)
2109{
2110	sp->ipcp.opts = 0;
2111	sp->ipcp.flags = 0;
2112	sp->state[IDX_IPCP] = STATE_INITIAL;
2113	sp->fail_counter[IDX_IPCP] = 0;
2114}
2115
2116static void
2117sppp_ipcp_up(struct sppp *sp)
2118{
2119	sppp_up_event(&ipcp, sp);
2120}
2121
2122static void
2123sppp_ipcp_down(struct sppp *sp)
2124{
2125	sppp_down_event(&ipcp, sp);
2126}
2127
2128static void
2129sppp_ipcp_open(struct sppp *sp)
2130{
2131	STDDCL;
2132	u_long myaddr, hisaddr;
2133
2134	sppp_get_ip_addrs(sp, &myaddr, &hisaddr);
2135	/*
2136	 * If we don't have his address, this probably means our
2137	 * interface doesn't want to talk IP at all.  (This could
2138	 * be the case if somebody wants to speak only IPX, for
2139	 * example.)  Don't open IPCP in this case.
2140	 */
2141	if (hisaddr == 0L) {
2142		/* XXX this message should go away */
2143		if (debug)
2144			log(LOG_DEBUG, "%s%d: ipcp_open(): no IP interface\n",
2145			    ifp->if_name, ifp->if_unit);
2146		return;
2147	}
2148
2149	if (myaddr == 0L) {
2150		/*
2151		 * I don't have an assigned address, so i need to
2152		 * negotiate my address.
2153		 */
2154		sp->ipcp.flags |= IPCP_MYADDR_DYN;
2155		sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2156	}
2157	sppp_open_event(&ipcp, sp);
2158}
2159
2160static void
2161sppp_ipcp_close(struct sppp *sp)
2162{
2163	sppp_close_event(&ipcp, sp);
2164	if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2165		/*
2166		 * My address was dynamic, clear it again.
2167		 */
2168		sppp_set_ip_addr(sp, 0L);
2169}
2170
2171static void
2172sppp_ipcp_TO(void *cookie)
2173{
2174	sppp_to_event(&ipcp, (struct sppp *)cookie);
2175}
2176
2177/*
2178 * Analyze a configure request.  Return true if it was agreeable, and
2179 * caused action sca, false if it has been rejected or nak'ed, and
2180 * caused action scn.  (The return value is used to make the state
2181 * transition decision in the state automaton.)
2182 */
2183static int
2184sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2185{
2186	u_char *buf, *r, *p;
2187	struct ifnet *ifp = &sp->pp_if;
2188	int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2189	u_long hisaddr, desiredaddr;
2190
2191	len -= 4;
2192	origlen = len;
2193	/*
2194	 * Make sure to allocate a buf that can at least hold a
2195	 * conf-nak with an `address' option.  We might need it below.
2196	 */
2197	buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2198	if (! buf)
2199		return (0);
2200
2201	/* pass 1: see if we can recognize them */
2202	if (debug)
2203		log(LOG_DEBUG, "%s%d: ipcp parse opts: ",
2204		    ifp->if_name, ifp->if_unit);
2205	p = (void*) (h+1);
2206	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2207		if (debug)
2208			addlog(" %s ", sppp_ipcp_opt_name(*p));
2209		switch (*p) {
2210#ifdef notyet
2211		case IPCP_OPT_COMPRESSION:
2212			if (len >= 6 && p[1] >= 6) {
2213				/* correctly formed compress option */
2214				continue;
2215			}
2216			if (debug)
2217				addlog("[invalid] ");
2218			break;
2219#endif
2220		case IPCP_OPT_ADDRESS:
2221			if (len >= 6 && p[1] == 6) {
2222				/* correctly formed address option */
2223				continue;
2224			}
2225			if (debug)
2226				addlog("[invalid] ");
2227			break;
2228		default:
2229			/* Others not supported. */
2230			if (debug)
2231				addlog("[rej] ");
2232			break;
2233		}
2234		/* Add the option to rejected list. */
2235		bcopy (p, r, p[1]);
2236		r += p[1];
2237		rlen += p[1];
2238	}
2239	if (rlen) {
2240		if (debug)
2241			addlog(" send conf-rej\n");
2242		sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2243		return 0;
2244	} else if (debug)
2245		addlog("\n");
2246
2247	/* pass 2: parse option values */
2248	sppp_get_ip_addrs(sp, 0, &hisaddr);
2249	if (debug)
2250		addlog("%s%d: ipcp parse opt values: ", ifp->if_name, ifp->if_unit);
2251	p = (void*) (h+1);
2252	len = origlen;
2253	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2254		if (debug)
2255			addlog(" %s ", sppp_ipcp_opt_name(*p));
2256		switch (*p) {
2257#ifdef notyet
2258		case IPCP_OPT_COMPRESSION:
2259			continue;
2260#endif
2261		case IPCP_OPT_ADDRESS:
2262			desiredaddr = p[2] << 24 | p[3] << 16 |
2263				p[4] << 8 | p[5];
2264			if (desiredaddr == hisaddr) {
2265				/*
2266				 * Peer's address is same as our value,
2267				 * this is agreeable.  Gonna conf-ack
2268				 * it.
2269				 */
2270				if (debug)
2271					addlog("0x%x [ack] ", hisaddr);
2272				/* record that we've seen it already */
2273				sp->ipcp.flags |= IPCP_HISADDR_SEEN;
2274				continue;
2275			}
2276			/*
2277			 * The address wasn't agreeable.  This is either
2278			 * he sent us 0.0.0.0, asking to assign him an
2279			 * address, or he send us another address not
2280			 * matching our value.  Either case, we gonna
2281			 * conf-nak it with our value.
2282			 */
2283			if (debug) {
2284				if (desiredaddr == 0)
2285					addlog("[addr requested] ");
2286				else
2287					addlog("0x%x [not agreed] ",
2288					       desiredaddr);
2289
2290				p[2] = hisaddr >> 24;
2291				p[3] = hisaddr >> 16;
2292				p[4] = hisaddr >> 8;
2293				p[5] = hisaddr;
2294			}
2295			break;
2296		}
2297		/* Add the option to nak'ed list. */
2298		bcopy (p, r, p[1]);
2299		r += p[1];
2300		rlen += p[1];
2301	}
2302
2303	/*
2304	 * If we are about to conf-ack the request, but haven't seen
2305	 * his address so far, gonna conf-nak it instead, with the
2306	 * `address' option present and our idea of his address being
2307	 * filled in there, to request negotiation of both addresses.
2308	 *
2309	 * XXX This can result in an endless req - nak loop if peer
2310	 * doesn't want to send us his address.  Q: What should we do
2311	 * about it?  XXX  A: implement the max-failure counter.
2312	 */
2313	if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN)) {
2314		buf[0] = IPCP_OPT_ADDRESS;
2315		buf[1] = 6;
2316		buf[2] = hisaddr >> 24;
2317		buf[3] = hisaddr >> 16;
2318		buf[4] = hisaddr >> 8;
2319		buf[5] = hisaddr;
2320		rlen = 6;
2321		if (debug)
2322			addlog("still need hisaddr ");
2323	}
2324
2325	if (rlen) {
2326		if (debug)
2327			addlog(" send conf-nak\n");
2328		sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
2329	} else {
2330		if (debug)
2331			addlog(" send conf-ack\n");
2332		sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
2333			      h->ident, origlen, h+1);
2334	}
2335
2336	free (buf, M_TEMP);
2337	return (rlen == 0);
2338}
2339
2340/*
2341 * Analyze the IPCP Configure-Reject option list, and adjust our
2342 * negotiation.
2343 */
2344static void
2345sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2346{
2347	u_char *buf, *p;
2348	struct ifnet *ifp = &sp->pp_if;
2349	int debug = ifp->if_flags & IFF_DEBUG;
2350
2351	len -= 4;
2352	buf = malloc (len, M_TEMP, M_NOWAIT);
2353	if (!buf)
2354		return;
2355
2356	if (debug)
2357		log(LOG_DEBUG, "%s%d: ipcp rej opts: ",
2358		    ifp->if_name, ifp->if_unit);
2359
2360	p = (void*) (h+1);
2361	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2362		if (debug)
2363			addlog(" %s ", sppp_ipcp_opt_name(*p));
2364		switch (*p) {
2365		case IPCP_OPT_ADDRESS:
2366			/*
2367			 * Peer doesn't grok address option.  This is
2368			 * bad.  XXX  Should we better give up here?
2369			 */
2370			sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
2371			break;
2372#ifdef notyet
2373		case IPCP_OPT_COMPRESS:
2374			sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
2375			break;
2376#endif
2377		}
2378	}
2379	if (debug)
2380		addlog("\n");
2381	free (buf, M_TEMP);
2382	return;
2383}
2384
2385/*
2386 * Analyze the IPCP Configure-NAK option list, and adjust our
2387 * negotiation.
2388 */
2389static void
2390sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2391{
2392	u_char *buf, *p;
2393	struct ifnet *ifp = &sp->pp_if;
2394	int debug = ifp->if_flags & IFF_DEBUG;
2395	u_long wantaddr;
2396
2397	len -= 4;
2398	buf = malloc (len, M_TEMP, M_NOWAIT);
2399	if (!buf)
2400		return;
2401
2402	if (debug)
2403		log(LOG_DEBUG, "%s%d: ipcp nak opts: ",
2404		    ifp->if_name, ifp->if_unit);
2405
2406	p = (void*) (h+1);
2407	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2408		if (debug)
2409			addlog(" %s ", sppp_ipcp_opt_name(*p));
2410		switch (*p) {
2411		case IPCP_OPT_ADDRESS:
2412			/*
2413			 * Peer doesn't like our local IP address.  See
2414			 * if we can do something for him.  We'll drop
2415			 * him our address then.
2416			 */
2417			if (len >= 6 && p[1] == 6) {
2418				wantaddr = p[2] << 24 | p[3] << 16 |
2419					p[4] << 8 | p[5];
2420				sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2421				if (debug)
2422					addlog("[wantaddr 0x%x] ", wantaddr);
2423				/*
2424				 * When doing dynamic address assignment,
2425				 * we accept his offer.  Otherwise, we
2426				 * ignore it and thus continue to negotiate
2427				 * our already existing value.
2428				 */
2429				if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
2430					sppp_set_ip_addr(sp, wantaddr);
2431					if (debug)
2432						addlog("[agree] ");
2433				}
2434			}
2435			break;
2436#ifdef notyet
2437		case IPCP_OPT_COMPRESS:
2438			/*
2439			 * Peer wants different compression parameters.
2440			 */
2441			break;
2442#endif
2443		}
2444	}
2445	if (debug)
2446		addlog("\n");
2447	free (buf, M_TEMP);
2448	return;
2449}
2450
2451static void
2452sppp_ipcp_tlu(struct sppp *sp)
2453{
2454}
2455
2456static void
2457sppp_ipcp_tld(struct sppp *sp)
2458{
2459}
2460
2461static void
2462sppp_ipcp_tls(struct sppp *sp)
2463{
2464	/* indicate to LCP that it must stay alive */
2465	sp->lcp.protos |= (1 << IDX_IPCP);
2466}
2467
2468static void
2469sppp_ipcp_tlf(struct sppp *sp)
2470{
2471	/* we no longer need LCP */
2472	sp->lcp.protos &= ~(1 << IDX_IPCP);
2473	sppp_lcp_check(sp);
2474}
2475
2476static void
2477sppp_ipcp_scr(struct sppp *sp)
2478{
2479	char opt[6 /* compression */ + 6 /* address */];
2480	u_long ouraddr;
2481	int i = 0;
2482
2483#ifdef notyet
2484	if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
2485		opt[i++] = IPCP_OPT_COMPRESSION;
2486		opt[i++] = 6;
2487		opt[i++] = 0;	/* VJ header compression */
2488		opt[i++] = 0x2d; /* VJ header compression */
2489		opt[i++] = max_slot_id;
2490		opt[i++] = comp_slot_id;
2491	}
2492#endif
2493
2494	if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
2495		sppp_get_ip_addrs(sp, &ouraddr, 0);
2496		opt[i++] = IPCP_OPT_ADDRESS;
2497		opt[i++] = 6;
2498		opt[i++] = ouraddr >> 24;
2499		opt[i++] = ouraddr >> 16;
2500		opt[i++] = ouraddr >> 8;
2501		opt[i++] = ouraddr;
2502	}
2503
2504	sp->confid[IDX_IPCP] = ++sp->pp_seq;
2505	sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
2506}
2507
2508
2509/*
2510 * Random miscellaneous functions.
2511 */
2512
2513/*
2514 * Flush interface queue.
2515 */
2516static void
2517sppp_qflush(struct ifqueue *ifq)
2518{
2519	struct mbuf *m, *n;
2520
2521	n = ifq->ifq_head;
2522	while ((m = n)) {
2523		n = m->m_act;
2524		m_freem (m);
2525	}
2526	ifq->ifq_head = 0;
2527	ifq->ifq_tail = 0;
2528	ifq->ifq_len = 0;
2529}
2530
2531/*
2532 * Send keepalive packets, every 10 seconds.
2533 */
2534static void
2535sppp_keepalive(void *dummy)
2536{
2537	struct sppp *sp;
2538	int s;
2539
2540	s = splimp();
2541	for (sp=spppq; sp; sp=sp->pp_next) {
2542		struct ifnet *ifp = &sp->pp_if;
2543
2544		/* Keepalive mode disabled or channel down? */
2545		if (! (sp->pp_flags & PP_KEEPALIVE) ||
2546		    ! (ifp->if_flags & IFF_RUNNING))
2547			continue;
2548
2549		/* No keepalive in PPP mode if LCP not opened yet. */
2550		if (! (sp->pp_flags & PP_CISCO) &&
2551		    sp->pp_phase < PHASE_AUTHENTICATE)
2552			continue;
2553
2554		if (sp->pp_alivecnt == MAXALIVECNT) {
2555			/* No keepalive packets got.  Stop the interface. */
2556			printf ("%s%d: down\n", ifp->if_name, ifp->if_unit);
2557			if_down (ifp);
2558			sppp_qflush (&sp->pp_cpq);
2559			if (! (sp->pp_flags & PP_CISCO)) {
2560				/* XXX */
2561				/* Shut down the PPP link. */
2562				lcp.Down(sp);
2563				/* Initiate negotiation. XXX */
2564				lcp.Up(sp);
2565			}
2566		}
2567		if (sp->pp_alivecnt <= MAXALIVECNT)
2568			++sp->pp_alivecnt;
2569		if (sp->pp_flags & PP_CISCO)
2570			sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
2571				sp->pp_rseq);
2572		else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
2573			long nmagic = htonl (sp->lcp.magic);
2574			sp->lcp.echoid = ++sp->pp_seq;
2575			sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
2576				sp->lcp.echoid, 4, &nmagic);
2577		}
2578	}
2579	splx(s);
2580	timeout(sppp_keepalive, 0, hz * 10);
2581}
2582
2583/*
2584 * Get both IP addresses.
2585 */
2586static void
2587sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst)
2588{
2589	struct ifnet *ifp = &sp->pp_if;
2590	struct ifaddr *ifa;
2591	struct sockaddr_in *si;
2592	u_long ssrc, ddst;
2593
2594	ssrc = ddst = 0L;
2595	/*
2596	 * Pick the first AF_INET address from the list,
2597	 * aliases don't make any sense on a p2p link anyway.
2598	 */
2599	for (ifa = ifp->if_addrhead.tqh_first, si = 0;
2600	     ifa;
2601	     ifa = ifa->ifa_link.tqe_next)
2602		if (ifa->ifa_addr->sa_family == AF_INET) {
2603			si = (struct sockaddr_in *)ifa->ifa_addr;
2604			if (si)
2605				break;
2606		}
2607	if (ifa) {
2608		if (si && si->sin_addr.s_addr)
2609			ssrc = si->sin_addr.s_addr;
2610
2611		si = (struct sockaddr_in *)ifa->ifa_dstaddr;
2612		if (si && si->sin_addr.s_addr)
2613			ddst = si->sin_addr.s_addr;
2614	}
2615
2616	if (dst) *dst = ntohl(ddst);
2617	if (src) *src = ntohl(ssrc);
2618}
2619
2620/*
2621 * Set my IP address.  Must be called at splimp.
2622 */
2623static void
2624sppp_set_ip_addr(struct sppp *sp, u_long src)
2625{
2626	struct ifnet *ifp = &sp->pp_if;
2627	struct ifaddr *ifa;
2628	struct sockaddr_in *si;
2629	u_long ssrc, ddst;
2630
2631	/*
2632	 * Pick the first AF_INET address from the list,
2633	 * aliases don't make any sense on a p2p link anyway.
2634	 */
2635	for (ifa = ifp->if_addrhead.tqh_first, si = 0;
2636	     ifa;
2637	     ifa = ifa->ifa_link.tqe_next)
2638		if (ifa->ifa_addr->sa_family == AF_INET) {
2639			si = (struct sockaddr_in *)ifa->ifa_addr;
2640			if (si)
2641				break;
2642		}
2643	if (ifa && si)
2644		si->sin_addr.s_addr = htonl(src);
2645}
2646
2647static const char *
2648sppp_cp_type_name(u_char type)
2649{
2650	static char buf [12];
2651	switch (type) {
2652	case CONF_REQ:   return ("conf-req");
2653	case CONF_ACK:   return ("conf-ack");
2654	case CONF_NAK:   return ("conf-nak");
2655	case CONF_REJ:   return ("conf-rej");
2656	case TERM_REQ:   return ("term-req");
2657	case TERM_ACK:   return ("term-ack");
2658	case CODE_REJ:   return ("code-rej");
2659	case PROTO_REJ:  return ("proto-rej");
2660	case ECHO_REQ:   return ("echo-req");
2661	case ECHO_REPLY: return ("echo-reply");
2662	case DISC_REQ:   return ("discard-req");
2663	}
2664	sprintf (buf, "0x%x", type);
2665	return (buf);
2666}
2667
2668static const char *
2669sppp_lcp_opt_name(u_char opt)
2670{
2671	static char buf [12];
2672	switch (opt) {
2673	case LCP_OPT_MRU:		return ("mru");
2674	case LCP_OPT_ASYNC_MAP:		return ("async-map");
2675	case LCP_OPT_AUTH_PROTO:	return ("auth-proto");
2676	case LCP_OPT_QUAL_PROTO:	return ("qual-proto");
2677	case LCP_OPT_MAGIC:		return ("magic");
2678	case LCP_OPT_PROTO_COMP:	return ("proto-comp");
2679	case LCP_OPT_ADDR_COMP:		return ("addr-comp");
2680	}
2681	sprintf (buf, "0x%x", opt);
2682	return (buf);
2683}
2684
2685static const char *
2686sppp_ipcp_opt_name(u_char opt)
2687{
2688	static char buf [12];
2689	switch (opt) {
2690	case IPCP_OPT_ADDRESSES:	return ("addresses");
2691	case IPCP_OPT_COMPRESSION:	return ("compression");
2692	case IPCP_OPT_ADDRESS:		return ("address");
2693	}
2694	sprintf (buf, "0x%x", opt);
2695	return (buf);
2696}
2697
2698static const char *
2699sppp_state_name(int state)
2700{
2701	switch (state) {
2702	case STATE_INITIAL:	return "initial";
2703	case STATE_STARTING:	return "starting";
2704	case STATE_CLOSED:	return "closed";
2705	case STATE_STOPPED:	return "stopped";
2706	case STATE_CLOSING:	return "closing";
2707	case STATE_STOPPING:	return "stopping";
2708	case STATE_REQ_SENT:	return "req-sent";
2709	case STATE_ACK_RCVD:	return "ack-rcvd";
2710	case STATE_ACK_SENT:	return "ack-sent";
2711	case STATE_OPENED:	return "opened";
2712	}
2713	return "illegal";
2714}
2715
2716static const char *
2717sppp_phase_name(enum ppp_phase phase)
2718{
2719	switch (phase) {
2720	case PHASE_DEAD:	return "dead";
2721	case PHASE_ESTABLISH:	return "establish";
2722	case PHASE_TERMINATE:	return "terminate";
2723	case PHASE_AUTHENTICATE: return "authenticate";
2724	case PHASE_NETWORK:	return "network";
2725	}
2726	return "illegal";
2727}
2728
2729static const char *
2730sppp_proto_name(u_short proto)
2731{
2732	static char buf[12];
2733	switch (proto) {
2734	case PPP_LCP:	return "lcp";
2735	case PPP_IPCP:	return "ipcp";
2736	}
2737	sprintf(buf, "0x%x", (unsigned)proto);
2738	return buf;
2739}
2740
2741static void
2742sppp_print_bytes(u_char *p, u_short len)
2743{
2744	addlog(" %x", *p++);
2745	while (--len > 0)
2746		addlog("-%x", *p++);
2747}
2748
2749/*
2750 * This file is large.  Tell emacs to highlight it nevertheless.
2751 *
2752 * Local Variables:
2753 * hilit-auto-highlight-maxout: 100000
2754 * End:
2755 */
2756