if_spppsubr.c revision 30300
1/*
2 * Synchronous PPP/Cisco link level subroutines.
3 * Keepalive protocol implemented in both Cisco and PPP modes.
4 *
5 * Copyright (C) 1994-1996 Cronyx Engineering Ltd.
6 * Author: Serge Vakulenko, <vak@cronyx.ru>
7 *
8 * Heavily revamped to conform to RFC 1661.
9 * Copyright (C) 1997, Joerg Wunsch.
10 *
11 * This software is distributed with NO WARRANTIES, not even the implied
12 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 *
14 * Authors grant any other persons or organisations permission to use
15 * or modify this software as long as this message is kept with the software,
16 * all derivative works or modified versions.
17 *
18 * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997
19 *
20 * $Id: if_spppsubr.c,v 1.26 1997/09/02 01:18:37 bde Exp $
21 */
22
23#include <sys/param.h>
24#include <sys/systm.h>
25#include <sys/kernel.h>
26#include <sys/sockio.h>
27#include <sys/socket.h>
28#include <sys/syslog.h>
29#include <sys/malloc.h>
30#include <sys/mbuf.h>
31#include <sys/md5.h>
32
33#include <net/if.h>
34#include <net/netisr.h>
35#include <net/if_types.h>
36
37#include <machine/stdarg.h>
38
39#ifdef INET
40#include <netinet/in.h>
41#include <netinet/in_systm.h>
42#include <netinet/in_var.h>
43#include <netinet/ip.h>
44#include <netinet/tcp.h>
45#include <netinet/if_ether.h>
46#endif
47
48#ifdef IPX
49#include <netipx/ipx.h>
50#include <netipx/ipx_if.h>
51#endif
52
53#ifdef NS
54#include <netns/ns.h>
55#include <netns/ns_if.h>
56#endif
57
58#ifdef ISO
59#include <netiso/argo_debug.h>
60#include <netiso/iso.h>
61#include <netiso/iso_var.h>
62#include <netiso/iso_snpac.h>
63#endif
64
65#include <net/if_sppp.h>
66
67#define MAXALIVECNT     3               /* max. alive packets */
68
69/*
70 * Interface flags that can be set in an ifconfig command.
71 *
72 * Setting link0 will make the link passive, i.e. it will be marked
73 * as being administrative openable, but won't be opened to begin
74 * with.  Incoming calls will be answered, or subsequent calls with
75 * -link1 will cause the administrative open of the LCP layer.
76 *
77 * Setting link1 will cause the link to auto-dial only as packets
78 * arrive to be sent.
79 *
80 * Setting IFF_DEBUG will syslog the option negotiation and state
81 * transitions at level kern.debug.  Note: all logs consistently look
82 * like
83 *
84 *   <if-name><unit>: <proto-name> <additional info...>
85 *
86 * with <if-name><unit> being something like "bppp0", and <proto-name>
87 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
88 */
89
90#define IFF_PASSIVE	IFF_LINK0	/* wait passively for connection */
91#define IFF_AUTO	IFF_LINK1	/* auto-dial on output */
92
93#define PPP_ALLSTATIONS 0xff		/* All-Stations broadcast address */
94#define PPP_UI		0x03		/* Unnumbered Information */
95#define PPP_IP		0x0021		/* Internet Protocol */
96#define PPP_ISO		0x0023		/* ISO OSI Protocol */
97#define PPP_XNS		0x0025		/* Xerox NS Protocol */
98#define PPP_IPX		0x002b		/* Novell IPX Protocol */
99#define PPP_LCP		0xc021		/* Link Control Protocol */
100#define PPP_PAP		0xc023		/* Password Authentication Protocol */
101#define PPP_CHAP	0xc223		/* Challenge-Handshake Auth Protocol */
102#define PPP_IPCP	0x8021		/* Internet Protocol Control Protocol */
103
104#define CONF_REQ	1		/* PPP configure request */
105#define CONF_ACK	2		/* PPP configure acknowledge */
106#define CONF_NAK	3		/* PPP configure negative ack */
107#define CONF_REJ	4		/* PPP configure reject */
108#define TERM_REQ	5		/* PPP terminate request */
109#define TERM_ACK	6		/* PPP terminate acknowledge */
110#define CODE_REJ	7		/* PPP code reject */
111#define PROTO_REJ	8		/* PPP protocol reject */
112#define ECHO_REQ	9		/* PPP echo request */
113#define ECHO_REPLY	10		/* PPP echo reply */
114#define DISC_REQ	11		/* PPP discard request */
115
116#define LCP_OPT_MRU		1	/* maximum receive unit */
117#define LCP_OPT_ASYNC_MAP	2	/* async control character map */
118#define LCP_OPT_AUTH_PROTO	3	/* authentication protocol */
119#define LCP_OPT_QUAL_PROTO	4	/* quality protocol */
120#define LCP_OPT_MAGIC		5	/* magic number */
121#define LCP_OPT_RESERVED	6	/* reserved */
122#define LCP_OPT_PROTO_COMP	7	/* protocol field compression */
123#define LCP_OPT_ADDR_COMP	8	/* address/control field compression */
124
125#define IPCP_OPT_ADDRESSES	1	/* both IP addresses; deprecated */
126#define IPCP_OPT_COMPRESSION	2	/* IP compression protocol (VJ) */
127#define IPCP_OPT_ADDRESS	3	/* local IP address */
128
129#define PAP_REQ			1	/* PAP name/password request */
130#define PAP_ACK			2	/* PAP acknowledge */
131#define PAP_NAK			3	/* PAP fail */
132
133#define CHAP_CHALLENGE		1	/* CHAP challenge request */
134#define CHAP_RESPONSE		2	/* CHAP challenge response */
135#define CHAP_SUCCESS		3	/* CHAP response ok */
136#define CHAP_FAILURE		4	/* CHAP response failed */
137
138#define CHAP_MD5		5	/* hash algorithm - MD5 */
139
140#define CISCO_MULTICAST		0x8f	/* Cisco multicast address */
141#define CISCO_UNICAST		0x0f	/* Cisco unicast address */
142#define CISCO_KEEPALIVE		0x8035	/* Cisco keepalive protocol */
143#define CISCO_ADDR_REQ		0	/* Cisco address request */
144#define CISCO_ADDR_REPLY	1	/* Cisco address reply */
145#define CISCO_KEEPALIVE_REQ	2	/* Cisco keepalive request */
146
147/* states are named and numbered according to RFC 1661 */
148#define STATE_INITIAL	0
149#define STATE_STARTING	1
150#define STATE_CLOSED	2
151#define STATE_STOPPED	3
152#define STATE_CLOSING	4
153#define STATE_STOPPING	5
154#define STATE_REQ_SENT	6
155#define STATE_ACK_RCVD	7
156#define STATE_ACK_SENT	8
157#define STATE_OPENED	9
158
159struct ppp_header {
160	u_char address;
161	u_char control;
162	u_short protocol;
163};
164#define PPP_HEADER_LEN          sizeof (struct ppp_header)
165
166struct lcp_header {
167	u_char type;
168	u_char ident;
169	u_short len;
170};
171#define LCP_HEADER_LEN          sizeof (struct lcp_header)
172
173struct cisco_packet {
174	u_long type;
175	u_long par1;
176	u_long par2;
177	u_short rel;
178	u_short time0;
179	u_short time1;
180};
181#define CISCO_PACKET_LEN 18
182
183/*
184 * We follow the spelling and capitalization of RFC 1661 here, to make
185 * it easier comparing with the standard.  Please refer to this RFC in
186 * case you can't make sense out of these abbreviation; it will also
187 * explain the semantics related to the various events and actions.
188 */
189struct cp {
190	u_short	proto;		/* PPP control protocol number */
191	u_char protoidx;	/* index into state table in struct sppp */
192	u_char flags;
193#define CP_LCP		0x01	/* this is the LCP */
194#define CP_AUTH		0x02	/* this is an authentication protocol */
195#define CP_NCP		0x04	/* this is a NCP */
196#define CP_QUAL		0x08	/* this is a quality reporting protocol */
197	const char *name;	/* name of this control protocol */
198	/* event handlers */
199	void	(*Up)(struct sppp *sp);
200	void	(*Down)(struct sppp *sp);
201	void	(*Open)(struct sppp *sp);
202	void	(*Close)(struct sppp *sp);
203	void	(*TO)(void *sp);
204	int	(*RCR)(struct sppp *sp, struct lcp_header *h, int len);
205	void	(*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
206	void	(*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
207	/* actions */
208	void	(*tlu)(struct sppp *sp);
209	void	(*tld)(struct sppp *sp);
210	void	(*tls)(struct sppp *sp);
211	void	(*tlf)(struct sppp *sp);
212	void	(*scr)(struct sppp *sp);
213};
214
215static struct sppp *spppq;
216static struct callout_handle keepalive_ch;
217
218/*
219 * The following disgusting hack gets around the problem that IP TOS
220 * can't be set yet.  We want to put "interactive" traffic on a high
221 * priority queue.  To decide if traffic is interactive, we check that
222 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
223 *
224 * XXX is this really still necessary?  - joerg -
225 */
226static u_short interactive_ports[8] = {
227	0,	513,	0,	0,
228	0,	21,	0,	23,
229};
230#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
231
232/* almost every function needs these */
233#define STDDCL							\
234	struct ifnet *ifp = &sp->pp_if;				\
235	int debug = ifp->if_flags & IFF_DEBUG
236
237static int sppp_output(struct ifnet *ifp, struct mbuf *m,
238		       struct sockaddr *dst, struct rtentry *rt);
239
240static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
241static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
242
243static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
244			  struct mbuf *m);
245static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
246			 u_char ident, u_short len, void *data);
247static void sppp_cp_timeout(void *arg);
248static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
249				 int newstate);
250static void sppp_auth_send(const struct cp *cp,
251			   struct sppp *sp, u_char type, u_char id,
252			   ...);
253
254static void sppp_up_event(const struct cp *cp, struct sppp *sp);
255static void sppp_down_event(const struct cp *cp, struct sppp *sp);
256static void sppp_open_event(const struct cp *cp, struct sppp *sp);
257static void sppp_close_event(const struct cp *cp, struct sppp *sp);
258static void sppp_to_event(const struct cp *cp, struct sppp *sp);
259
260static void sppp_null(struct sppp *sp);
261
262static void sppp_lcp_init(struct sppp *sp);
263static void sppp_lcp_up(struct sppp *sp);
264static void sppp_lcp_down(struct sppp *sp);
265static void sppp_lcp_open(struct sppp *sp);
266static void sppp_lcp_close(struct sppp *sp);
267static void sppp_lcp_TO(void *sp);
268static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
269static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
270static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
271static void sppp_lcp_tlu(struct sppp *sp);
272static void sppp_lcp_tld(struct sppp *sp);
273static void sppp_lcp_tls(struct sppp *sp);
274static void sppp_lcp_tlf(struct sppp *sp);
275static void sppp_lcp_scr(struct sppp *sp);
276static void sppp_lcp_check_and_close(struct sppp *sp);
277static int sppp_ncp_check(struct sppp *sp);
278
279static void sppp_ipcp_init(struct sppp *sp);
280static void sppp_ipcp_up(struct sppp *sp);
281static void sppp_ipcp_down(struct sppp *sp);
282static void sppp_ipcp_open(struct sppp *sp);
283static void sppp_ipcp_close(struct sppp *sp);
284static void sppp_ipcp_TO(void *sp);
285static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
286static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
287static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
288static void sppp_ipcp_tlu(struct sppp *sp);
289static void sppp_ipcp_tld(struct sppp *sp);
290static void sppp_ipcp_tls(struct sppp *sp);
291static void sppp_ipcp_tlf(struct sppp *sp);
292static void sppp_ipcp_scr(struct sppp *sp);
293
294static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
295static void sppp_pap_init(struct sppp *sp);
296static void sppp_pap_open(struct sppp *sp);
297static void sppp_pap_close(struct sppp *sp);
298static void sppp_pap_TO(void *sp);
299static void sppp_pap_my_TO(void *sp);
300static void sppp_pap_tlu(struct sppp *sp);
301static void sppp_pap_tld(struct sppp *sp);
302static void sppp_pap_scr(struct sppp *sp);
303
304static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
305static void sppp_chap_init(struct sppp *sp);
306static void sppp_chap_open(struct sppp *sp);
307static void sppp_chap_close(struct sppp *sp);
308static void sppp_chap_TO(void *sp);
309static void sppp_chap_tlu(struct sppp *sp);
310static void sppp_chap_tld(struct sppp *sp);
311static void sppp_chap_scr(struct sppp *sp);
312
313static const char *sppp_auth_type_name(u_short proto, u_char type);
314static const char *sppp_cp_type_name(u_char type);
315static const char *sppp_dotted_quad(u_long addr);
316static const char *sppp_ipcp_opt_name(u_char opt);
317static const char *sppp_lcp_opt_name(u_char opt);
318static const char *sppp_phase_name(enum ppp_phase phase);
319static const char *sppp_proto_name(u_short proto);
320static const char *sppp_state_name(int state);
321static int sppp_params(struct sppp *sp, int cmd, void *data);
322static int sppp_strnlen(u_char *p, int max);
323static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst,
324			      u_long *srcmask);
325static void sppp_keepalive(void *dummy);
326static void sppp_phase_network(struct sppp *sp);
327static void sppp_print_bytes(const u_char *p, u_short len);
328static void sppp_print_string(const char *p, u_short len);
329static void sppp_qflush(struct ifqueue *ifq);
330static void sppp_set_ip_addr(struct sppp *sp, u_long src);
331
332/* our control protocol descriptors */
333const struct cp lcp = {
334	PPP_LCP, IDX_LCP, CP_LCP, "lcp",
335	sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
336	sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
337	sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
338	sppp_lcp_scr
339};
340
341const struct cp ipcp = {
342	PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
343	sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
344	sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
345	sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
346	sppp_ipcp_scr
347};
348
349const struct cp pap = {
350	PPP_PAP, IDX_PAP, CP_AUTH, "pap",
351	sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
352	sppp_pap_TO, 0, 0, 0,
353	sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
354	sppp_pap_scr
355};
356
357const struct cp chap = {
358	PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
359	sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
360	sppp_chap_TO, 0, 0, 0,
361	sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
362	sppp_chap_scr
363};
364
365const struct cp *cps[IDX_COUNT] = {
366	&lcp,			/* IDX_LCP */
367	&ipcp,			/* IDX_IPCP */
368	&pap,			/* IDX_PAP */
369	&chap,			/* IDX_CHAP */
370};
371
372
373/*
374 * Exported functions, comprising our interface to the lower layer.
375 */
376
377/*
378 * Process the received packet.
379 */
380void
381sppp_input(struct ifnet *ifp, struct mbuf *m)
382{
383	struct ppp_header *h;
384	struct ifqueue *inq = 0;
385	int s;
386	struct sppp *sp = (struct sppp *)ifp;
387	int debug = ifp->if_flags & IFF_DEBUG;
388
389	if (ifp->if_flags & IFF_UP)
390		/* Count received bytes, add FCS and one flag */
391		ifp->if_ibytes += m->m_pkthdr.len + 3;
392
393	if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
394		/* Too small packet, drop it. */
395		if (debug)
396			log(LOG_DEBUG,
397			    "%s%d: input packet is too small, %d bytes\n",
398			    ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
399	  drop:
400		++ifp->if_ierrors;
401		++ifp->if_iqdrops;
402		m_freem (m);
403		return;
404	}
405
406	/* Get PPP header. */
407	h = mtod (m, struct ppp_header*);
408	m_adj (m, PPP_HEADER_LEN);
409
410	switch (h->address) {
411	case PPP_ALLSTATIONS:
412		if (h->control != PPP_UI)
413			goto invalid;
414		if (sp->pp_flags & PP_CISCO) {
415			if (debug)
416				log(LOG_DEBUG,
417				    "%s%d: PPP packet in Cisco mode "
418				    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
419				    ifp->if_name, ifp->if_unit,
420				    h->address, h->control, ntohs(h->protocol));
421			goto drop;
422		}
423		switch (ntohs (h->protocol)) {
424		default:
425			if (sp->state[IDX_LCP] == STATE_OPENED)
426				sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
427					++sp->pp_seq, m->m_pkthdr.len + 2,
428					&h->protocol);
429			if (debug)
430				log(LOG_DEBUG,
431				    "%s%d: invalid input protocol "
432				    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
433				    ifp->if_name, ifp->if_unit,
434				    h->address, h->control, ntohs(h->protocol));
435			++ifp->if_noproto;
436			goto drop;
437		case PPP_LCP:
438			sppp_cp_input(&lcp, sp, m);
439			m_freem (m);
440			return;
441		case PPP_PAP:
442			if (sp->pp_phase >= PHASE_AUTHENTICATE)
443				sppp_pap_input(sp, m);
444			m_freem (m);
445			return;
446		case PPP_CHAP:
447			if (sp->pp_phase >= PHASE_AUTHENTICATE)
448				sppp_chap_input(sp, m);
449			m_freem (m);
450			return;
451#ifdef INET
452		case PPP_IPCP:
453			if (sp->pp_phase == PHASE_NETWORK)
454				sppp_cp_input(&ipcp, sp, m);
455			m_freem (m);
456			return;
457		case PPP_IP:
458			if (sp->state[IDX_IPCP] == STATE_OPENED) {
459				schednetisr (NETISR_IP);
460				inq = &ipintrq;
461			}
462			break;
463#endif
464#ifdef IPX
465		case PPP_IPX:
466			/* IPX IPXCP not implemented yet */
467			if (sp->pp_phase == PHASE_NETWORK) {
468				schednetisr (NETISR_IPX);
469				inq = &ipxintrq;
470			}
471			break;
472#endif
473#ifdef NS
474		case PPP_XNS:
475			/* XNS IDPCP not implemented yet */
476			if (sp->pp_phase == PHASE_NETWORK) {
477				schednetisr (NETISR_NS);
478				inq = &nsintrq;
479			}
480			break;
481#endif
482#ifdef ISO
483		case PPP_ISO:
484			/* OSI NLCP not implemented yet */
485			if (sp->pp_phase == PHASE_NETWORK) {
486				schednetisr (NETISR_ISO);
487				inq = &clnlintrq;
488			}
489			break;
490#endif
491		}
492		break;
493	case CISCO_MULTICAST:
494	case CISCO_UNICAST:
495		/* Don't check the control field here (RFC 1547). */
496		if (! (sp->pp_flags & PP_CISCO)) {
497			if (debug)
498				log(LOG_DEBUG,
499				    "%s%d: Cisco packet in PPP mode "
500				    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
501				    ifp->if_name, ifp->if_unit,
502				    h->address, h->control, ntohs(h->protocol));
503			goto drop;
504		}
505		switch (ntohs (h->protocol)) {
506		default:
507			++ifp->if_noproto;
508			goto invalid;
509		case CISCO_KEEPALIVE:
510			sppp_cisco_input ((struct sppp*) ifp, m);
511			m_freem (m);
512			return;
513#ifdef INET
514		case ETHERTYPE_IP:
515			schednetisr (NETISR_IP);
516			inq = &ipintrq;
517			break;
518#endif
519#ifdef IPX
520		case ETHERTYPE_IPX:
521			schednetisr (NETISR_IPX);
522			inq = &ipxintrq;
523			break;
524#endif
525#ifdef NS
526		case ETHERTYPE_NS:
527			schednetisr (NETISR_NS);
528			inq = &nsintrq;
529			break;
530#endif
531		}
532		break;
533	default:        /* Invalid PPP packet. */
534	  invalid:
535		if (debug)
536			log(LOG_DEBUG,
537			    "%s%d: invalid input packet "
538			    "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
539			    ifp->if_name, ifp->if_unit,
540			    h->address, h->control, ntohs(h->protocol));
541		goto drop;
542	}
543
544	if (! (ifp->if_flags & IFF_UP) || ! inq)
545		goto drop;
546
547	/* Check queue. */
548	s = splimp();
549	if (IF_QFULL (inq)) {
550		/* Queue overflow. */
551		IF_DROP(inq);
552		splx(s);
553		if (debug)
554			log(LOG_DEBUG, "%s%d: protocol queue overflow\n",
555				ifp->if_name, ifp->if_unit);
556		goto drop;
557	}
558	IF_ENQUEUE(inq, m);
559	splx(s);
560}
561
562/*
563 * Enqueue transmit packet.
564 */
565static int
566sppp_output(struct ifnet *ifp, struct mbuf *m,
567	    struct sockaddr *dst, struct rtentry *rt)
568{
569	struct sppp *sp = (struct sppp*) ifp;
570	struct ppp_header *h;
571	struct ifqueue *ifq;
572	int s, rv = 0;
573
574	s = splimp();
575
576	if ((ifp->if_flags & IFF_UP) == 0 ||
577	    (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
578		m_freem (m);
579		splx (s);
580		return (ENETDOWN);
581	}
582
583	if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
584		/*
585		 * Interface is not yet running, but auto-dial.  Need
586		 * to start LCP for it.
587		 */
588		ifp->if_flags |= IFF_RUNNING;
589		splx(s);
590		lcp.Open(sp);
591		s = splimp();
592	}
593
594	ifq = &ifp->if_snd;
595#ifdef INET
596	/*
597	 * Put low delay, telnet, rlogin and ftp control packets
598	 * in front of the queue.
599	 */
600	if (dst->sa_family == AF_INET) {
601		struct ip *ip = mtod (m, struct ip*);
602		struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
603
604		if (! IF_QFULL (&sp->pp_fastq) &&
605		    ((ip->ip_tos & IPTOS_LOWDELAY) ||
606	    	    ip->ip_p == IPPROTO_TCP &&
607	    	    m->m_len >= sizeof (struct ip) + sizeof (struct tcphdr) &&
608	    	    (INTERACTIVE (ntohs (tcp->th_sport)) ||
609	    	    INTERACTIVE (ntohs (tcp->th_dport)))))
610			ifq = &sp->pp_fastq;
611	}
612#endif
613
614	/*
615	 * Prepend general data packet PPP header. For now, IP only.
616	 */
617	M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
618	if (! m) {
619		if (ifp->if_flags & IFF_DEBUG)
620			log(LOG_DEBUG, "%s%d: no memory for transmit header\n",
621				ifp->if_name, ifp->if_unit);
622		++ifp->if_oerrors;
623		splx (s);
624		return (ENOBUFS);
625	}
626	h = mtod (m, struct ppp_header*);
627	if (sp->pp_flags & PP_CISCO) {
628		h->address = CISCO_UNICAST;        /* unicast address */
629		h->control = 0;
630	} else {
631		h->address = PPP_ALLSTATIONS;        /* broadcast address */
632		h->control = PPP_UI;                 /* Unnumbered Info */
633	}
634
635	switch (dst->sa_family) {
636#ifdef INET
637	case AF_INET:   /* Internet Protocol */
638		if (sp->pp_flags & PP_CISCO)
639			h->protocol = htons (ETHERTYPE_IP);
640		else {
641			/*
642			 * Don't choke with an ENETDOWN early.  It's
643			 * possible that we just started dialing out,
644			 * so don't drop the packet immediately.  If
645			 * we notice that we run out of buffer space
646			 * below, we will however remember that we are
647			 * not ready to carry IP packets, and return
648			 * ENETDOWN, as opposed to ENOBUFS.
649			 */
650			h->protocol = htons(PPP_IP);
651			if (sp->state[IDX_IPCP] != STATE_OPENED)
652				rv = ENETDOWN;
653		}
654		break;
655#endif
656#ifdef NS
657	case AF_NS:     /* Xerox NS Protocol */
658		h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
659			ETHERTYPE_NS : PPP_XNS);
660		break;
661#endif
662#ifdef IPX
663	case AF_IPX:     /* Novell IPX Protocol */
664		h->protocol = htons ((sp->pp_flags & PP_CISCO) ?
665			ETHERTYPE_IPX : PPP_IPX);
666		break;
667#endif
668#ifdef ISO
669	case AF_ISO:    /* ISO OSI Protocol */
670		if (sp->pp_flags & PP_CISCO)
671			goto nosupport;
672		h->protocol = htons (PPP_ISO);
673		break;
674nosupport:
675#endif
676	default:
677		m_freem (m);
678		++ifp->if_oerrors;
679		splx (s);
680		return (EAFNOSUPPORT);
681	}
682
683	/*
684	 * Queue message on interface, and start output if interface
685	 * not yet active.
686	 */
687	if (IF_QFULL (ifq)) {
688		IF_DROP (&ifp->if_snd);
689		m_freem (m);
690		++ifp->if_oerrors;
691		splx (s);
692		return (rv? rv: ENOBUFS);
693	}
694	IF_ENQUEUE (ifq, m);
695	if (! (ifp->if_flags & IFF_OACTIVE))
696		(*ifp->if_start) (ifp);
697
698	/*
699	 * Count output packets and bytes.
700	 * The packet length includes header, FCS and 1 flag,
701	 * according to RFC 1333.
702	 */
703	ifp->if_obytes += m->m_pkthdr.len + 3;
704	splx (s);
705	return (0);
706}
707
708void
709sppp_attach(struct ifnet *ifp)
710{
711	struct sppp *sp = (struct sppp*) ifp;
712
713	/* Initialize keepalive handler. */
714	if (! spppq)
715		keepalive_ch = timeout(sppp_keepalive, 0, hz * 10);
716
717	/* Insert new entry into the keepalive list. */
718	sp->pp_next = spppq;
719	spppq = sp;
720
721	sp->pp_if.if_type = IFT_PPP;
722	sp->pp_if.if_output = sppp_output;
723	sp->pp_fastq.ifq_maxlen = 32;
724	sp->pp_cpq.ifq_maxlen = 20;
725	sp->pp_loopcnt = 0;
726	sp->pp_alivecnt = 0;
727	sp->pp_seq = 0;
728	sp->pp_rseq = 0;
729	sp->pp_phase = PHASE_DEAD;
730	sp->pp_up = lcp.Up;
731	sp->pp_down = lcp.Down;
732
733	sppp_lcp_init(sp);
734	sppp_ipcp_init(sp);
735	sppp_pap_init(sp);
736	sppp_chap_init(sp);
737}
738
739void
740sppp_detach(struct ifnet *ifp)
741{
742	struct sppp **q, *p, *sp = (struct sppp*) ifp;
743	int i;
744
745	/* Remove the entry from the keepalive list. */
746	for (q = &spppq; (p = *q); q = &p->pp_next)
747		if (p == sp) {
748			*q = p->pp_next;
749			break;
750		}
751
752	/* Stop keepalive handler. */
753	if (! spppq)
754		untimeout(sppp_keepalive, 0, keepalive_ch);
755
756	for (i = 0; i < IDX_COUNT; i++)
757		untimeout((cps[i])->TO, (void *)sp, sp->ch[i]);
758	untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
759}
760
761/*
762 * Flush the interface output queue.
763 */
764void
765sppp_flush(struct ifnet *ifp)
766{
767	struct sppp *sp = (struct sppp*) ifp;
768
769	sppp_qflush (&sp->pp_if.if_snd);
770	sppp_qflush (&sp->pp_fastq);
771	sppp_qflush (&sp->pp_cpq);
772}
773
774/*
775 * Check if the output queue is empty.
776 */
777int
778sppp_isempty(struct ifnet *ifp)
779{
780	struct sppp *sp = (struct sppp*) ifp;
781	int empty, s;
782
783	s = splimp();
784	empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
785		!sp->pp_if.if_snd.ifq_head;
786	splx(s);
787	return (empty);
788}
789
790/*
791 * Get next packet to send.
792 */
793struct mbuf *
794sppp_dequeue(struct ifnet *ifp)
795{
796	struct sppp *sp = (struct sppp*) ifp;
797	struct mbuf *m;
798	int s;
799
800	s = splimp();
801	/*
802	 * Process only the control protocol queue until we have at
803	 * least one NCP open.
804	 *
805	 * Do always serve all three queues in Cisco mode.
806	 */
807	IF_DEQUEUE(&sp->pp_cpq, m);
808	if (m == NULL &&
809	    (sppp_ncp_check(sp) || (sp->pp_flags & PP_CISCO) != 0)) {
810		IF_DEQUEUE(&sp->pp_fastq, m);
811		if (m == NULL)
812			IF_DEQUEUE (&sp->pp_if.if_snd, m);
813	}
814	splx(s);
815	return m;
816}
817
818/*
819 * Pick the next packet, do not remove it from the queue.
820 */
821struct mbuf *
822sppp_pick(struct ifnet *ifp)
823{
824	struct sppp *sp = (struct sppp*)ifp;
825	struct mbuf *m;
826	int s;
827
828	s= splimp ();
829
830	m = sp->pp_cpq.ifq_head;
831	if (m == NULL &&
832	    (sp->pp_phase == PHASE_NETWORK ||
833	     (sp->pp_flags & PP_CISCO) != 0))
834		if ((m = sp->pp_fastq.ifq_head) == NULL)
835			m = sp->pp_if.if_snd.ifq_head;
836	splx (s);
837	return (m);
838}
839
840/*
841 * Process an ioctl request.  Called on low priority level.
842 */
843int
844sppp_ioctl(struct ifnet *ifp, int cmd, void *data)
845{
846	struct ifreq *ifr = (struct ifreq*) data;
847	struct sppp *sp = (struct sppp*) ifp;
848	int s, rv, going_up, going_down, newmode;
849
850	s = splimp();
851	rv = 0;
852	switch (cmd) {
853	case SIOCAIFADDR:
854	case SIOCSIFDSTADDR:
855		break;
856
857	case SIOCSIFADDR:
858		if_up(ifp);
859		/* fall through... */
860
861	case SIOCSIFFLAGS:
862		going_up = ifp->if_flags & IFF_UP &&
863			(ifp->if_flags & IFF_RUNNING) == 0;
864		going_down = (ifp->if_flags & IFF_UP) == 0 &&
865			ifp->if_flags & IFF_RUNNING;
866		newmode = ifp->if_flags & (IFF_AUTO | IFF_PASSIVE);
867		if (newmode == (IFF_AUTO | IFF_PASSIVE)) {
868			/* sanity */
869			newmode = IFF_PASSIVE;
870			ifp->if_flags &= ~IFF_AUTO;
871		}
872
873		if (going_up || going_down)
874			lcp.Close(sp);
875		if (going_up && newmode == 0) {
876			/* neither auto-dial nor passive */
877			ifp->if_flags |= IFF_RUNNING;
878			if (!(sp->pp_flags & PP_CISCO))
879				lcp.Open(sp);
880		} else if (going_down) {
881			sppp_flush(ifp);
882			ifp->if_flags &= ~IFF_RUNNING;
883		}
884
885		break;
886
887#ifdef SIOCSIFMTU
888#ifndef ifr_mtu
889#define ifr_mtu ifr_metric
890#endif
891	case SIOCSIFMTU:
892		if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
893			return (EINVAL);
894		ifp->if_mtu = ifr->ifr_mtu;
895		break;
896#endif
897#ifdef SLIOCSETMTU
898	case SLIOCSETMTU:
899		if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
900			return (EINVAL);
901		ifp->if_mtu = *(short*)data;
902		break;
903#endif
904#ifdef SIOCGIFMTU
905	case SIOCGIFMTU:
906		ifr->ifr_mtu = ifp->if_mtu;
907		break;
908#endif
909#ifdef SLIOCGETMTU
910	case SLIOCGETMTU:
911		*(short*)data = ifp->if_mtu;
912		break;
913#endif
914	case SIOCADDMULTI:
915	case SIOCDELMULTI:
916		break;
917
918	case SIOCGIFGENERIC:
919	case SIOCSIFGENERIC:
920		rv = sppp_params(sp, cmd, data);
921		break;
922
923	default:
924		rv = ENOTTY;
925	}
926	splx(s);
927	return rv;
928}
929
930
931/*
932 * Cisco framing implementation.
933 */
934
935/*
936 * Handle incoming Cisco keepalive protocol packets.
937 */
938static void
939sppp_cisco_input(struct sppp *sp, struct mbuf *m)
940{
941	STDDCL;
942	struct cisco_packet *h;
943	u_long me, mymask;
944
945	if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
946		if (debug)
947			log(LOG_DEBUG,
948			    "%s%d: cisco invalid packet length: %d bytes\n",
949			    ifp->if_name, ifp->if_unit, m->m_pkthdr.len);
950		return;
951	}
952	h = mtod (m, struct cisco_packet*);
953	if (debug)
954		log(LOG_DEBUG,
955		    "%s%d: cisco input: %d bytes "
956		    "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
957		    ifp->if_name, ifp->if_unit, m->m_pkthdr.len,
958		    ntohl (h->type), h->par1, h->par2, h->rel,
959		    h->time0, h->time1);
960	switch (ntohl (h->type)) {
961	default:
962		if (debug)
963			addlog("%s%d: cisco unknown packet type: 0x%lx\n",
964			       ifp->if_name, ifp->if_unit, ntohl (h->type));
965		break;
966	case CISCO_ADDR_REPLY:
967		/* Reply on address request, ignore */
968		break;
969	case CISCO_KEEPALIVE_REQ:
970		sp->pp_alivecnt = 0;
971		sp->pp_rseq = ntohl (h->par1);
972		if (sp->pp_seq == sp->pp_rseq) {
973			/* Local and remote sequence numbers are equal.
974			 * Probably, the line is in loopback mode. */
975			if (sp->pp_loopcnt >= MAXALIVECNT) {
976				printf ("%s%d: loopback\n",
977					ifp->if_name, ifp->if_unit);
978				sp->pp_loopcnt = 0;
979				if (ifp->if_flags & IFF_UP) {
980					if_down (ifp);
981					sppp_qflush (&sp->pp_cpq);
982				}
983			}
984			++sp->pp_loopcnt;
985
986			/* Generate new local sequence number */
987			sp->pp_seq ^= time.tv_sec ^ time.tv_usec;
988			break;
989		}
990		sp->pp_loopcnt = 0;
991		if (! (ifp->if_flags & IFF_UP) &&
992		    (ifp->if_flags & IFF_RUNNING)) {
993			if_up(ifp);
994			printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
995		}
996		break;
997	case CISCO_ADDR_REQ:
998		sppp_get_ip_addrs(sp, &me, 0, &mymask);
999		if (me != 0L)
1000			sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1001		break;
1002	}
1003}
1004
1005/*
1006 * Send Cisco keepalive packet.
1007 */
1008static void
1009sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1010{
1011	STDDCL;
1012	struct ppp_header *h;
1013	struct cisco_packet *ch;
1014	struct mbuf *m;
1015	u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
1016
1017	MGETHDR (m, M_DONTWAIT, MT_DATA);
1018	if (! m)
1019		return;
1020	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1021	m->m_pkthdr.rcvif = 0;
1022
1023	h = mtod (m, struct ppp_header*);
1024	h->address = CISCO_MULTICAST;
1025	h->control = 0;
1026	h->protocol = htons (CISCO_KEEPALIVE);
1027
1028	ch = (struct cisco_packet*) (h + 1);
1029	ch->type = htonl (type);
1030	ch->par1 = htonl (par1);
1031	ch->par2 = htonl (par2);
1032	ch->rel = -1;
1033	ch->time0 = htons ((u_short) (t >> 16));
1034	ch->time1 = htons ((u_short) t);
1035
1036	if (debug)
1037		log(LOG_DEBUG,
1038		    "%s%d: cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1039			ifp->if_name, ifp->if_unit, ntohl (ch->type), ch->par1,
1040			ch->par2, ch->rel, ch->time0, ch->time1);
1041
1042	if (IF_QFULL (&sp->pp_cpq)) {
1043		IF_DROP (&sp->pp_fastq);
1044		IF_DROP (&ifp->if_snd);
1045		m_freem (m);
1046	} else
1047		IF_ENQUEUE (&sp->pp_cpq, m);
1048	if (! (ifp->if_flags & IFF_OACTIVE))
1049		(*ifp->if_start) (ifp);
1050	ifp->if_obytes += m->m_pkthdr.len + 3;
1051}
1052
1053/*
1054 * PPP protocol implementation.
1055 */
1056
1057/*
1058 * Send PPP control protocol packet.
1059 */
1060static void
1061sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1062	     u_char ident, u_short len, void *data)
1063{
1064	STDDCL;
1065	struct ppp_header *h;
1066	struct lcp_header *lh;
1067	struct mbuf *m;
1068
1069	if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1070		len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1071	MGETHDR (m, M_DONTWAIT, MT_DATA);
1072	if (! m)
1073		return;
1074	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1075	m->m_pkthdr.rcvif = 0;
1076
1077	h = mtod (m, struct ppp_header*);
1078	h->address = PPP_ALLSTATIONS;        /* broadcast address */
1079	h->control = PPP_UI;                 /* Unnumbered Info */
1080	h->protocol = htons (proto);         /* Link Control Protocol */
1081
1082	lh = (struct lcp_header*) (h + 1);
1083	lh->type = type;
1084	lh->ident = ident;
1085	lh->len = htons (LCP_HEADER_LEN + len);
1086	if (len)
1087		bcopy (data, lh+1, len);
1088
1089	if (debug) {
1090		log(LOG_DEBUG, "%s%d: %s output <%s id=0x%x len=%d",
1091		    ifp->if_name, ifp->if_unit,
1092		    sppp_proto_name(proto),
1093		    sppp_cp_type_name (lh->type), lh->ident,
1094		    ntohs (lh->len));
1095		if (len)
1096			sppp_print_bytes ((u_char*) (lh+1), len);
1097		addlog(">\n");
1098	}
1099	if (IF_QFULL (&sp->pp_cpq)) {
1100		IF_DROP (&sp->pp_fastq);
1101		IF_DROP (&ifp->if_snd);
1102		m_freem (m);
1103		++ifp->if_oerrors;
1104	} else
1105		IF_ENQUEUE (&sp->pp_cpq, m);
1106	if (! (ifp->if_flags & IFF_OACTIVE))
1107		(*ifp->if_start) (ifp);
1108	ifp->if_obytes += m->m_pkthdr.len + 3;
1109}
1110
1111/*
1112 * Handle incoming PPP control protocol packets.
1113 */
1114static void
1115sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1116{
1117	STDDCL;
1118	struct lcp_header *h;
1119	int len = m->m_pkthdr.len;
1120	int rv;
1121	u_char *p;
1122
1123	if (len < 4) {
1124		if (debug)
1125			log(LOG_DEBUG,
1126			    "%s%d: %s invalid packet length: %d bytes\n",
1127			    ifp->if_name, ifp->if_unit, cp->name, len);
1128		return;
1129	}
1130	h = mtod (m, struct lcp_header*);
1131	if (debug) {
1132		log(LOG_DEBUG,
1133		    "%s%d: %s input(%s): <%s id=0x%x len=%d",
1134		    ifp->if_name, ifp->if_unit, cp->name,
1135		    sppp_state_name(sp->state[cp->protoidx]),
1136		    sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1137		if (len > 4)
1138			sppp_print_bytes ((u_char*) (h+1), len-4);
1139		addlog(">\n");
1140	}
1141	if (len > ntohs (h->len))
1142		len = ntohs (h->len);
1143	p = (u_char *)(h + 1);
1144	switch (h->type) {
1145	case CONF_REQ:
1146		if (len < 4) {
1147			if (debug)
1148				addlog("%s%d: %s invalid conf-req length %d\n",
1149				       ifp->if_name, ifp->if_unit, cp->name,
1150				       len);
1151			++ifp->if_ierrors;
1152			break;
1153		}
1154		/* handle states where RCR doesn't get a SCA/SCN */
1155		switch (sp->state[cp->protoidx]) {
1156		case STATE_CLOSING:
1157		case STATE_STOPPING:
1158			return;
1159		case STATE_CLOSED:
1160			sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1161				     0, 0);
1162			return;
1163		}
1164		rv = (cp->RCR)(sp, h, len);
1165		switch (sp->state[cp->protoidx]) {
1166		case STATE_OPENED:
1167			(cp->tld)(sp);
1168			(cp->scr)(sp);
1169			/* fall through... */
1170		case STATE_ACK_SENT:
1171		case STATE_REQ_SENT:
1172			sppp_cp_change_state(cp, sp, rv?
1173					     STATE_ACK_SENT: STATE_REQ_SENT);
1174			break;
1175		case STATE_STOPPED:
1176			sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1177			(cp->scr)(sp);
1178			sppp_cp_change_state(cp, sp, rv?
1179					     STATE_ACK_SENT: STATE_REQ_SENT);
1180			break;
1181		case STATE_ACK_RCVD:
1182			if (rv) {
1183				sppp_cp_change_state(cp, sp, STATE_OPENED);
1184				if (debug)
1185					log(LOG_DEBUG, "%s%d: %s tlu\n",
1186					    ifp->if_name, ifp->if_unit,
1187					    cp->name);
1188				(cp->tlu)(sp);
1189			} else
1190				sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1191			break;
1192		default:
1193			printf("%s%d: %s illegal %s in state %s\n",
1194			       ifp->if_name, ifp->if_unit, cp->name,
1195			       sppp_cp_type_name(h->type),
1196			       sppp_state_name(sp->state[cp->protoidx]));
1197			++ifp->if_ierrors;
1198		}
1199		break;
1200	case CONF_ACK:
1201		if (h->ident != sp->confid[cp->protoidx]) {
1202			if (debug)
1203				addlog("%s%d: %s id mismatch 0x%x != 0x%x\n",
1204				       ifp->if_name, ifp->if_unit, cp->name,
1205				       h->ident, sp->confid[cp->protoidx]);
1206			++ifp->if_ierrors;
1207			break;
1208		}
1209		switch (sp->state[cp->protoidx]) {
1210		case STATE_CLOSED:
1211		case STATE_STOPPED:
1212			sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1213			break;
1214		case STATE_CLOSING:
1215		case STATE_STOPPING:
1216			break;
1217		case STATE_REQ_SENT:
1218			sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1219			sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1220			break;
1221		case STATE_OPENED:
1222			(cp->tld)(sp);
1223			/* fall through */
1224		case STATE_ACK_RCVD:
1225			(cp->scr)(sp);
1226			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1227			break;
1228		case STATE_ACK_SENT:
1229			sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1230			sppp_cp_change_state(cp, sp, STATE_OPENED);
1231			if (debug)
1232				log(LOG_DEBUG, "%s%d: %s tlu\n",
1233				       ifp->if_name, ifp->if_unit, cp->name);
1234			(cp->tlu)(sp);
1235			break;
1236		default:
1237			printf("%s%d: %s illegal %s in state %s\n",
1238			       ifp->if_name, ifp->if_unit, cp->name,
1239			       sppp_cp_type_name(h->type),
1240			       sppp_state_name(sp->state[cp->protoidx]));
1241			++ifp->if_ierrors;
1242		}
1243		break;
1244	case CONF_NAK:
1245	case CONF_REJ:
1246		if (h->ident != sp->confid[cp->protoidx]) {
1247			if (debug)
1248				addlog("%s%d: %s id mismatch 0x%x != 0x%x\n",
1249				       ifp->if_name, ifp->if_unit, cp->name,
1250				       h->ident, sp->confid[cp->protoidx]);
1251			++ifp->if_ierrors;
1252			break;
1253		}
1254		if (h->type == CONF_NAK)
1255			(cp->RCN_nak)(sp, h, len);
1256		else /* CONF_REJ */
1257			(cp->RCN_rej)(sp, h, len);
1258
1259		switch (sp->state[cp->protoidx]) {
1260		case STATE_CLOSED:
1261		case STATE_STOPPED:
1262			sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1263			break;
1264		case STATE_REQ_SENT:
1265		case STATE_ACK_SENT:
1266			sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1267			(cp->scr)(sp);
1268			break;
1269		case STATE_OPENED:
1270			(cp->tld)(sp);
1271			/* fall through */
1272		case STATE_ACK_RCVD:
1273			sppp_cp_change_state(cp, sp, STATE_ACK_SENT);
1274			(cp->scr)(sp);
1275			break;
1276		case STATE_CLOSING:
1277		case STATE_STOPPING:
1278			break;
1279		default:
1280			printf("%s%d: %s illegal %s in state %s\n",
1281			       ifp->if_name, ifp->if_unit, cp->name,
1282			       sppp_cp_type_name(h->type),
1283			       sppp_state_name(sp->state[cp->protoidx]));
1284			++ifp->if_ierrors;
1285		}
1286		break;
1287
1288	case TERM_REQ:
1289		switch (sp->state[cp->protoidx]) {
1290		case STATE_ACK_RCVD:
1291		case STATE_ACK_SENT:
1292			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1293			/* fall through */
1294		case STATE_CLOSED:
1295		case STATE_STOPPED:
1296		case STATE_CLOSING:
1297		case STATE_STOPPING:
1298		case STATE_REQ_SENT:
1299		  sta:
1300			/* Send Terminate-Ack packet. */
1301			if (debug)
1302				log(LOG_DEBUG, "%s%d: %s send terminate-ack\n",
1303				    ifp->if_name, ifp->if_unit, cp->name);
1304			sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1305			break;
1306		case STATE_OPENED:
1307			(cp->tld)(sp);
1308			sp->rst_counter[cp->protoidx] = 0;
1309			sppp_cp_change_state(cp, sp, STATE_STOPPING);
1310			goto sta;
1311			break;
1312		default:
1313			printf("%s%d: %s illegal %s in state %s\n",
1314			       ifp->if_name, ifp->if_unit, cp->name,
1315			       sppp_cp_type_name(h->type),
1316			       sppp_state_name(sp->state[cp->protoidx]));
1317			++ifp->if_ierrors;
1318		}
1319		break;
1320	case TERM_ACK:
1321		switch (sp->state[cp->protoidx]) {
1322		case STATE_CLOSED:
1323		case STATE_STOPPED:
1324		case STATE_REQ_SENT:
1325		case STATE_ACK_SENT:
1326			break;
1327		case STATE_CLOSING:
1328			(cp->tlf)(sp);
1329			sppp_cp_change_state(cp, sp, STATE_CLOSED);
1330			break;
1331		case STATE_STOPPING:
1332			(cp->tlf)(sp);
1333			sppp_cp_change_state(cp, sp, STATE_STOPPED);
1334			break;
1335		case STATE_ACK_RCVD:
1336			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1337			break;
1338		case STATE_OPENED:
1339			(cp->tld)(sp);
1340			(cp->scr)(sp);
1341			sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1342			break;
1343		default:
1344			printf("%s%d: %s illegal %s in state %s\n",
1345			       ifp->if_name, ifp->if_unit, cp->name,
1346			       sppp_cp_type_name(h->type),
1347			       sppp_state_name(sp->state[cp->protoidx]));
1348			++ifp->if_ierrors;
1349		}
1350		break;
1351	case CODE_REJ:
1352	case PROTO_REJ:
1353		/* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1354		log(LOG_INFO,
1355		    "%s%d: %s: ignoring RXJ (%s) for proto 0x%x, "
1356		    "danger will robinson\n",
1357		    ifp->if_name, ifp->if_unit, cp->name,
1358		    sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1359		switch (sp->state[cp->protoidx]) {
1360		case STATE_CLOSED:
1361		case STATE_STOPPED:
1362		case STATE_REQ_SENT:
1363		case STATE_ACK_SENT:
1364		case STATE_CLOSING:
1365		case STATE_STOPPING:
1366		case STATE_OPENED:
1367			break;
1368		case STATE_ACK_RCVD:
1369			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1370			break;
1371		default:
1372			printf("%s%d: %s illegal %s in state %s\n",
1373			       ifp->if_name, ifp->if_unit, cp->name,
1374			       sppp_cp_type_name(h->type),
1375			       sppp_state_name(sp->state[cp->protoidx]));
1376			++ifp->if_ierrors;
1377		}
1378		break;
1379	case DISC_REQ:
1380		if (cp->proto != PPP_LCP)
1381			goto illegal;
1382		/* Discard the packet. */
1383		break;
1384	case ECHO_REQ:
1385		if (cp->proto != PPP_LCP)
1386			goto illegal;
1387		if (sp->state[cp->protoidx] != STATE_OPENED) {
1388			if (debug)
1389				addlog("%s%d: lcp echo req but lcp closed\n",
1390				       ifp->if_name, ifp->if_unit);
1391			++ifp->if_ierrors;
1392			break;
1393		}
1394		if (len < 8) {
1395			if (debug)
1396				addlog("%s%d: invalid lcp echo request "
1397				       "packet length: %d bytes\n",
1398				       ifp->if_name, ifp->if_unit, len);
1399			break;
1400		}
1401		if (ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1402			/* Line loopback mode detected. */
1403			printf("%s%d: loopback\n", ifp->if_name, ifp->if_unit);
1404			if_down (ifp);
1405			sppp_qflush (&sp->pp_cpq);
1406
1407			/* Shut down the PPP link. */
1408			/* XXX */
1409			lcp.Down(sp);
1410			lcp.Up(sp);
1411			break;
1412		}
1413		*(long*)(h+1) = htonl (sp->lcp.magic);
1414		if (debug)
1415			addlog("%s%d: got lcp echo req, sending echo rep\n",
1416			       ifp->if_name, ifp->if_unit);
1417		sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1418		break;
1419	case ECHO_REPLY:
1420		if (cp->proto != PPP_LCP)
1421			goto illegal;
1422		if (h->ident != sp->lcp.echoid) {
1423			++ifp->if_ierrors;
1424			break;
1425		}
1426		if (len < 8) {
1427			if (debug)
1428				addlog("%s%d: lcp invalid echo reply "
1429				       "packet length: %d bytes\n",
1430				       ifp->if_name, ifp->if_unit, len);
1431			break;
1432		}
1433		if (debug)
1434			addlog("%s%d: lcp got echo rep\n",
1435			       ifp->if_name, ifp->if_unit);
1436		if (ntohl (*(long*)(h+1)) != sp->lcp.magic)
1437			sp->pp_alivecnt = 0;
1438		break;
1439	default:
1440		/* Unknown packet type -- send Code-Reject packet. */
1441	  illegal:
1442		if (debug)
1443			addlog("%s%d: %c send code-rej for 0x%x\n",
1444			       ifp->if_name, ifp->if_unit, cp->name, h->type);
1445		sppp_cp_send(sp, cp->proto, CODE_REJ, ++sp->pp_seq,
1446			     m->m_pkthdr.len, h);
1447		++ifp->if_ierrors;
1448	}
1449}
1450
1451
1452/*
1453 * The generic part of all Up/Down/Open/Close/TO event handlers.
1454 * Basically, the state transition handling in the automaton.
1455 */
1456static void
1457sppp_up_event(const struct cp *cp, struct sppp *sp)
1458{
1459	STDDCL;
1460
1461	if (debug)
1462		log(LOG_DEBUG, "%s%d: %s up(%s)\n",
1463		    ifp->if_name, ifp->if_unit, cp->name,
1464		    sppp_state_name(sp->state[cp->protoidx]));
1465
1466	switch (sp->state[cp->protoidx]) {
1467	case STATE_INITIAL:
1468		sppp_cp_change_state(cp, sp, STATE_CLOSED);
1469		break;
1470	case STATE_STARTING:
1471		sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1472		(cp->scr)(sp);
1473		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1474		break;
1475	default:
1476		printf("%s%d: %s illegal up in state %s\n",
1477		       ifp->if_name, ifp->if_unit, cp->name,
1478		       sppp_state_name(sp->state[cp->protoidx]));
1479	}
1480}
1481
1482static void
1483sppp_down_event(const struct cp *cp, struct sppp *sp)
1484{
1485	STDDCL;
1486
1487	if (debug)
1488		log(LOG_DEBUG, "%s%d: %s down(%s)\n",
1489		    ifp->if_name, ifp->if_unit, cp->name,
1490		    sppp_state_name(sp->state[cp->protoidx]));
1491
1492	switch (sp->state[cp->protoidx]) {
1493	case STATE_CLOSED:
1494	case STATE_CLOSING:
1495		sppp_cp_change_state(cp, sp, STATE_INITIAL);
1496		break;
1497	case STATE_STOPPED:
1498		(cp->tls)(sp);
1499		/* fall through */
1500	case STATE_STOPPING:
1501	case STATE_REQ_SENT:
1502	case STATE_ACK_RCVD:
1503	case STATE_ACK_SENT:
1504		sppp_cp_change_state(cp, sp, STATE_STARTING);
1505		break;
1506	case STATE_OPENED:
1507		(cp->tld)(sp);
1508		sppp_cp_change_state(cp, sp, STATE_STARTING);
1509		break;
1510	default:
1511		printf("%s%d: %s illegal down in state %s\n",
1512		       ifp->if_name, ifp->if_unit, cp->name,
1513		       sppp_state_name(sp->state[cp->protoidx]));
1514	}
1515}
1516
1517
1518static void
1519sppp_open_event(const struct cp *cp, struct sppp *sp)
1520{
1521	STDDCL;
1522
1523	if (debug)
1524		log(LOG_DEBUG, "%s%d: %s open(%s)\n",
1525		    ifp->if_name, ifp->if_unit, cp->name,
1526		    sppp_state_name(sp->state[cp->protoidx]));
1527
1528	switch (sp->state[cp->protoidx]) {
1529	case STATE_INITIAL:
1530		(cp->tls)(sp);
1531		sppp_cp_change_state(cp, sp, STATE_STARTING);
1532		break;
1533	case STATE_STARTING:
1534		break;
1535	case STATE_CLOSED:
1536		sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1537		(cp->scr)(sp);
1538		sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1539		break;
1540	case STATE_STOPPED:
1541	case STATE_STOPPING:
1542	case STATE_REQ_SENT:
1543	case STATE_ACK_RCVD:
1544	case STATE_ACK_SENT:
1545	case STATE_OPENED:
1546		break;
1547	case STATE_CLOSING:
1548		sppp_cp_change_state(cp, sp, STATE_STOPPING);
1549		break;
1550	}
1551}
1552
1553
1554static void
1555sppp_close_event(const struct cp *cp, struct sppp *sp)
1556{
1557	STDDCL;
1558
1559	if (debug)
1560		log(LOG_DEBUG, "%s%d: %s close(%s)\n",
1561		    ifp->if_name, ifp->if_unit, cp->name,
1562		    sppp_state_name(sp->state[cp->protoidx]));
1563
1564	switch (sp->state[cp->protoidx]) {
1565	case STATE_INITIAL:
1566	case STATE_CLOSED:
1567	case STATE_CLOSING:
1568		break;
1569	case STATE_STARTING:
1570		(cp->tlf)(sp);
1571		sppp_cp_change_state(cp, sp, STATE_INITIAL);
1572		break;
1573	case STATE_STOPPED:
1574		sppp_cp_change_state(cp, sp, STATE_CLOSED);
1575		break;
1576	case STATE_STOPPING:
1577		sppp_cp_change_state(cp, sp, STATE_CLOSING);
1578		break;
1579	case STATE_OPENED:
1580		(cp->tld)(sp);
1581		/* fall through */
1582	case STATE_REQ_SENT:
1583	case STATE_ACK_RCVD:
1584	case STATE_ACK_SENT:
1585		sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
1586		sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq, 0, 0);
1587		sppp_cp_change_state(cp, sp, STATE_CLOSING);
1588		break;
1589	}
1590}
1591
1592static void
1593sppp_to_event(const struct cp *cp, struct sppp *sp)
1594{
1595	STDDCL;
1596	int s;
1597
1598	s = splimp();
1599	if (debug)
1600		log(LOG_DEBUG, "%s%d: %s TO(%s) rst_counter = %d\n",
1601		    ifp->if_name, ifp->if_unit, cp->name,
1602		    sppp_state_name(sp->state[cp->protoidx]),
1603		    sp->rst_counter[cp->protoidx]);
1604
1605	if (--sp->rst_counter[cp->protoidx] < 0)
1606		/* TO- event */
1607		switch (sp->state[cp->protoidx]) {
1608		case STATE_CLOSING:
1609			(cp->tlf)(sp);
1610			sppp_cp_change_state(cp, sp, STATE_CLOSED);
1611			break;
1612		case STATE_STOPPING:
1613			(cp->tlf)(sp);
1614			sppp_cp_change_state(cp, sp, STATE_STOPPED);
1615			break;
1616		case STATE_REQ_SENT:
1617		case STATE_ACK_RCVD:
1618		case STATE_ACK_SENT:
1619			(cp->tlf)(sp);
1620			sppp_cp_change_state(cp, sp, STATE_STOPPED);
1621			break;
1622		}
1623	else
1624		/* TO+ event */
1625		switch (sp->state[cp->protoidx]) {
1626		case STATE_CLOSING:
1627		case STATE_STOPPING:
1628			sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq,
1629				     0, 0);
1630			sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp,
1631						       sp->lcp.timeout);
1632			break;
1633		case STATE_REQ_SENT:
1634		case STATE_ACK_RCVD:
1635			(cp->scr)(sp);
1636			/* sppp_cp_change_state() will restart the timer */
1637			sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1638			break;
1639		case STATE_ACK_SENT:
1640			(cp->scr)(sp);
1641			sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp,
1642						       sp->lcp.timeout);
1643			break;
1644		}
1645
1646	splx(s);
1647}
1648
1649/*
1650 * Change the state of a control protocol in the state automaton.
1651 * Takes care of starting/stopping the restart timer.
1652 */
1653void
1654sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
1655{
1656	sp->state[cp->protoidx] = newstate;
1657
1658	untimeout(cp->TO, (void *)sp, sp->ch[cp->protoidx]);
1659	switch (newstate) {
1660	case STATE_INITIAL:
1661	case STATE_STARTING:
1662	case STATE_CLOSED:
1663	case STATE_STOPPED:
1664	case STATE_OPENED:
1665		break;
1666	case STATE_CLOSING:
1667	case STATE_STOPPING:
1668	case STATE_REQ_SENT:
1669	case STATE_ACK_RCVD:
1670	case STATE_ACK_SENT:
1671		sp->ch[cp->protoidx]  = timeout(cp->TO, (void *)sp,
1672						sp->lcp.timeout);
1673		break;
1674	}
1675}
1676/*
1677 *--------------------------------------------------------------------------*
1678 *                                                                          *
1679 *                         The LCP implementation.                          *
1680 *                                                                          *
1681 *--------------------------------------------------------------------------*
1682 */
1683static void
1684sppp_lcp_init(struct sppp *sp)
1685{
1686	sp->lcp.opts = (1 << LCP_OPT_MAGIC);
1687	sp->lcp.magic = 0;
1688	sp->state[IDX_LCP] = STATE_INITIAL;
1689	sp->fail_counter[IDX_LCP] = 0;
1690	sp->lcp.protos = 0;
1691	sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
1692
1693	/*
1694	 * Initialize counters and timeout values.  Note that we don't
1695	 * use the 3 seconds suggested in RFC 1661 since we are likely
1696	 * running on a fast link.  XXX We should probably implement
1697	 * the exponential backoff option.  Note that these values are
1698	 * relevant for all control protocols, not just LCP only.
1699	 */
1700	sp->lcp.timeout = 1 * hz;
1701	sp->lcp.max_terminate = 2;
1702	sp->lcp.max_configure = 10;
1703	sp->lcp.max_failure = 10;
1704	callout_handle_init(&sp->ch[IDX_LCP]);
1705}
1706
1707static void
1708sppp_lcp_up(struct sppp *sp)
1709{
1710	STDDCL;
1711
1712	/*
1713	 * If this interface is passive or dial-on-demand, and we are
1714	 * still in Initial state, it means we've got an incoming
1715	 * call.  Activate the interface.
1716	 */
1717	if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
1718		if (debug)
1719			log(LOG_DEBUG,
1720			    "%s%d: Up event", ifp->if_name, ifp->if_unit);
1721		ifp->if_flags |= IFF_RUNNING;
1722		if (sp->state[IDX_LCP] == STATE_INITIAL) {
1723			if (debug)
1724				addlog("(incoming call)\n");
1725			sp->pp_flags |= PP_CALLIN;
1726			lcp.Open(sp);
1727		} else if (debug)
1728			addlog("\n");
1729	}
1730
1731	sppp_up_event(&lcp, sp);
1732}
1733
1734static void
1735sppp_lcp_down(struct sppp *sp)
1736{
1737	STDDCL;
1738
1739	sppp_down_event(&lcp, sp);
1740
1741	/*
1742	 * If this is neither a dial-on-demand nor a passive
1743	 * interface, simulate an ``ifconfig down'' action, so the
1744	 * administrator can force a redial by another ``ifconfig
1745	 * up''.  XXX For leased line operation, should we immediately
1746	 * try to reopen the connection here?
1747	 */
1748	if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
1749		log(LOG_INFO,
1750		    "%s%d: Down event (carrier loss), taking interface down.\n",
1751		    ifp->if_name, ifp->if_unit);
1752		if_down(ifp);
1753	} else {
1754		if (debug)
1755			log(LOG_DEBUG,
1756			    "%s%d: Down event (carrier loss)\n",
1757			    ifp->if_name, ifp->if_unit);
1758	}
1759	sp->pp_flags &= ~PP_CALLIN;
1760	if (sp->state[IDX_LCP] != STATE_INITIAL)
1761		lcp.Close(sp);
1762	ifp->if_flags &= ~IFF_RUNNING;
1763}
1764
1765static void
1766sppp_lcp_open(struct sppp *sp)
1767{
1768	/*
1769	 * If we are authenticator, negotiate LCP_AUTH
1770	 */
1771	if (sp->hisauth.proto != 0)
1772		sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
1773	else
1774		sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
1775	sp->pp_flags &= ~PP_NEEDAUTH;
1776	sppp_open_event(&lcp, sp);
1777}
1778
1779static void
1780sppp_lcp_close(struct sppp *sp)
1781{
1782	sppp_close_event(&lcp, sp);
1783}
1784
1785static void
1786sppp_lcp_TO(void *cookie)
1787{
1788	sppp_to_event(&lcp, (struct sppp *)cookie);
1789}
1790
1791/*
1792 * Analyze a configure request.  Return true if it was agreeable, and
1793 * caused action sca, false if it has been rejected or nak'ed, and
1794 * caused action scn.  (The return value is used to make the state
1795 * transition decision in the state automaton.)
1796 */
1797static int
1798sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
1799{
1800	STDDCL;
1801	u_char *buf, *r, *p;
1802	int origlen, rlen;
1803	u_long nmagic;
1804	u_short authproto;
1805
1806	len -= 4;
1807	origlen = len;
1808	buf = r = malloc (len, M_TEMP, M_NOWAIT);
1809	if (! buf)
1810		return (0);
1811
1812	if (debug)
1813		log(LOG_DEBUG, "%s%d: lcp parse opts: ",
1814		    ifp->if_name, ifp->if_unit);
1815
1816	/* pass 1: check for things that need to be rejected */
1817	p = (void*) (h+1);
1818	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1819		if (debug)
1820			addlog(" %s ", sppp_lcp_opt_name(*p));
1821		switch (*p) {
1822		case LCP_OPT_MAGIC:
1823			/* Magic number. */
1824			/* fall through, both are same length */
1825		case LCP_OPT_ASYNC_MAP:
1826			/* Async control character map. */
1827			if (len >= 6 || p[1] == 6)
1828				continue;
1829			if (debug)
1830				addlog("[invalid] ");
1831			break;
1832		case LCP_OPT_MRU:
1833			/* Maximum receive unit. */
1834			if (len >= 4 && p[1] == 4)
1835				continue;
1836			if (debug)
1837				addlog("[invalid] ");
1838			break;
1839		case LCP_OPT_AUTH_PROTO:
1840			if (len < 4) {
1841				if (debug)
1842					addlog("[invalid] ");
1843				break;
1844			}
1845			authproto = (p[2] << 8) + p[3];
1846			if (authproto == PPP_CHAP && p[1] != 5) {
1847				if (debug)
1848					addlog("[invalid chap len] ");
1849				break;
1850			}
1851			if (sp->myauth.proto == 0) {
1852				/* we are not configured to do auth */
1853				if (debug)
1854					addlog("[not configured] ");
1855				break;
1856			}
1857			/*
1858			 * Remote want us to authenticate, remember this,
1859			 * so we stay in PHASE_AUTHENTICATE after LCP got
1860			 * up.
1861			 */
1862			sp->pp_flags |= PP_NEEDAUTH;
1863			continue;
1864		default:
1865			/* Others not supported. */
1866			if (debug)
1867				addlog("[rej] ");
1868			break;
1869		}
1870		/* Add the option to rejected list. */
1871		bcopy (p, r, p[1]);
1872		r += p[1];
1873		rlen += p[1];
1874	}
1875	if (rlen) {
1876		if (debug)
1877			addlog(" send conf-rej\n");
1878		sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
1879		return 0;
1880	} else if (debug)
1881		addlog("\n");
1882
1883	/*
1884	 * pass 2: check for option values that are unacceptable and
1885	 * thus require to be nak'ed.
1886	 */
1887	if (debug)
1888		log(LOG_DEBUG, "%s%d: lcp parse opt values: ",
1889		    ifp->if_name, ifp->if_unit);
1890
1891	p = (void*) (h+1);
1892	len = origlen;
1893	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1894		if (debug)
1895			addlog(" %s ", sppp_lcp_opt_name(*p));
1896		switch (*p) {
1897		case LCP_OPT_MAGIC:
1898			/* Magic number -- extract. */
1899			nmagic = (u_long)p[2] << 24 |
1900				(u_long)p[3] << 16 | p[4] << 8 | p[5];
1901			if (nmagic != sp->lcp.magic) {
1902				if (debug)
1903					addlog("0x%x ", nmagic);
1904				continue;
1905			}
1906			/*
1907			 * Local and remote magics equal -- loopback?
1908			 */
1909			if (sp->pp_loopcnt >= MAXALIVECNT*5) {
1910				printf ("%s%d: loopback\n",
1911					ifp->if_name, ifp->if_unit);
1912				sp->pp_loopcnt = 0;
1913				if (ifp->if_flags & IFF_UP) {
1914					if_down(ifp);
1915					sppp_qflush(&sp->pp_cpq);
1916					/* XXX ? */
1917					lcp.Down(sp);
1918					lcp.Up(sp);
1919				}
1920			} else if (debug)
1921				addlog("[glitch] ");
1922			++sp->pp_loopcnt;
1923			/*
1924			 * We negate our magic here, and NAK it.  If
1925			 * we see it later in an NAK packet, we
1926			 * suggest a new one.
1927			 */
1928			nmagic = ~sp->lcp.magic;
1929			/* Gonna NAK it. */
1930			p[2] = nmagic >> 24;
1931			p[3] = nmagic >> 16;
1932			p[4] = nmagic >> 8;
1933			p[5] = nmagic;
1934			break;
1935
1936		case LCP_OPT_ASYNC_MAP:
1937			/* Async control character map -- check to be zero. */
1938			if (! p[2] && ! p[3] && ! p[4] && ! p[5]) {
1939				if (debug)
1940					addlog("[empty] ");
1941				continue;
1942			}
1943			if (debug)
1944				addlog("[non-empty] ");
1945			/* suggest a zero one */
1946			p[2] = p[3] = p[4] = p[5] = 0;
1947			break;
1948
1949		case LCP_OPT_MRU:
1950			/*
1951			 * Maximum receive unit.  Always agreeable,
1952			 * but ignored by now.
1953			 */
1954			sp->lcp.their_mru = p[2] * 256 + p[3];
1955			if (debug)
1956				addlog("%d ", sp->lcp.their_mru);
1957			continue;
1958
1959		case LCP_OPT_AUTH_PROTO:
1960			authproto = (p[2] << 8) + p[3];
1961			if (sp->myauth.proto != authproto) {
1962				/* not agreed, nak */
1963				if (debug)
1964					addlog("[mine %s != his %s] ",
1965					       sppp_proto_name(sp->hisauth.proto),
1966					       sppp_proto_name(authproto));
1967				p[2] = sp->myauth.proto >> 8;
1968				p[3] = sp->myauth.proto;
1969				break;
1970			}
1971			if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
1972				if (debug)
1973					addlog("[chap not MD5] ");
1974				p[4] == CHAP_MD5;
1975				break;
1976			}
1977			continue;
1978		}
1979		/* Add the option to nak'ed list. */
1980		bcopy (p, r, p[1]);
1981		r += p[1];
1982		rlen += p[1];
1983	}
1984	if (rlen) {
1985		if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
1986			if (debug)
1987				addlog(" max_failure (%d) exceeded, "
1988				       "send conf-rej\n",
1989				       sp->lcp.max_failure);
1990			sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
1991		} else {
1992			if (debug)
1993				addlog(" send conf-nak\n");
1994			sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
1995		}
1996		return 0;
1997	} else {
1998		if (debug)
1999			addlog(" send conf-ack\n");
2000		sp->fail_counter[IDX_LCP] = 0;
2001		sp->pp_loopcnt = 0;
2002		sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2003			      h->ident, origlen, h+1);
2004	}
2005
2006	free (buf, M_TEMP);
2007	return (rlen == 0);
2008}
2009
2010/*
2011 * Analyze the LCP Configure-Reject option list, and adjust our
2012 * negotiation.
2013 */
2014static void
2015sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2016{
2017	STDDCL;
2018	u_char *buf, *p;
2019
2020	len -= 4;
2021	buf = malloc (len, M_TEMP, M_NOWAIT);
2022	if (!buf)
2023		return;
2024
2025	if (debug)
2026		log(LOG_DEBUG, "%s%d: lcp rej opts: ",
2027		    ifp->if_name, ifp->if_unit);
2028
2029	p = (void*) (h+1);
2030	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2031		if (debug)
2032			addlog(" %s ", sppp_lcp_opt_name(*p));
2033		switch (*p) {
2034		case LCP_OPT_MAGIC:
2035			/* Magic number -- can't use it, use 0 */
2036			sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2037			sp->lcp.magic = 0;
2038			break;
2039		case LCP_OPT_MRU:
2040			/*
2041			 * Should not be rejected anyway, since we only
2042			 * negotiate a MRU if explicitly requested by
2043			 * peer.
2044			 */
2045			sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2046			break;
2047		case LCP_OPT_AUTH_PROTO:
2048			/*
2049			 * Peer doesn't want to authenticate himself,
2050			 * deny unless this is a dialout call, and
2051			 * AUTHFLAG_NOCALLOUT is set.
2052			 */
2053			if ((sp->pp_flags & PP_CALLIN) == 0 &&
2054			    (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2055				if (debug)
2056					addlog("[don't insist on auth "
2057					       "for callout]");
2058				sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2059				break;
2060			}
2061			if (debug)
2062				addlog("[access denied]\n");
2063			lcp.Close(sp);
2064			break;
2065		}
2066	}
2067	if (debug)
2068		addlog("\n");
2069	free (buf, M_TEMP);
2070	return;
2071}
2072
2073/*
2074 * Analyze the LCP Configure-NAK option list, and adjust our
2075 * negotiation.
2076 */
2077static void
2078sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2079{
2080	STDDCL;
2081	u_char *buf, *p;
2082	u_long magic;
2083
2084	len -= 4;
2085	buf = malloc (len, M_TEMP, M_NOWAIT);
2086	if (!buf)
2087		return;
2088
2089	if (debug)
2090		log(LOG_DEBUG, "%s%d: lcp nak opts: ",
2091		    ifp->if_name, ifp->if_unit);
2092
2093	p = (void*) (h+1);
2094	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2095		if (debug)
2096			addlog(" %s ", sppp_lcp_opt_name(*p));
2097		switch (*p) {
2098		case LCP_OPT_MAGIC:
2099			/* Magic number -- renegotiate */
2100			if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2101			    len >= 6 && p[1] == 6) {
2102				magic = (u_long)p[2] << 24 |
2103					(u_long)p[3] << 16 | p[4] << 8 | p[5];
2104				/*
2105				 * If the remote magic is our negated one,
2106				 * this looks like a loopback problem.
2107				 * Suggest a new magic to make sure.
2108				 */
2109				if (magic == ~sp->lcp.magic) {
2110					if (debug)
2111						addlog("magic glitch ");
2112					sp->lcp.magic += time.tv_sec + time.tv_usec;
2113				} else {
2114					sp->lcp.magic = magic;
2115					if (debug)
2116						addlog("%d ");
2117				}
2118			}
2119			break;
2120		case LCP_OPT_MRU:
2121			/*
2122			 * Peer wants to advise us to negotiate an MRU.
2123			 * Agree on it if it's reasonable, or use
2124			 * default otherwise.
2125			 */
2126			if (len >= 4 && p[1] == 4) {
2127				u_int mru = p[2] * 256 + p[3];
2128				if (debug)
2129					addlog("%d ", mru);
2130				if (mru < PP_MTU || mru > PP_MAX_MRU)
2131					mru = PP_MTU;
2132				sp->lcp.mru = mru;
2133				sp->lcp.opts |= (1 << LCP_OPT_MRU);
2134			}
2135			break;
2136		case LCP_OPT_AUTH_PROTO:
2137			/*
2138			 * Peer doesn't like our authentication method,
2139			 * deny.
2140			 */
2141			if (debug)
2142				addlog("[access denied]\n");
2143			lcp.Close(sp);
2144			break;
2145		}
2146	}
2147	if (debug)
2148		addlog("\n");
2149	free (buf, M_TEMP);
2150	return;
2151}
2152
2153static void
2154sppp_lcp_tlu(struct sppp *sp)
2155{
2156	STDDCL;
2157	int i;
2158	u_long mask;
2159
2160	/* XXX ? */
2161	if (! (ifp->if_flags & IFF_UP) &&
2162	    (ifp->if_flags & IFF_RUNNING)) {
2163		/* Coming out of loopback mode. */
2164		if_up(ifp);
2165		printf ("%s%d: up\n", ifp->if_name, ifp->if_unit);
2166	}
2167
2168	for (i = 0; i < IDX_COUNT; i++)
2169		if ((cps[i])->flags & CP_QUAL)
2170			(cps[i])->Open(sp);
2171
2172	if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2173	    (sp->pp_flags & PP_NEEDAUTH) != 0)
2174		sp->pp_phase = PHASE_AUTHENTICATE;
2175	else
2176		sp->pp_phase = PHASE_NETWORK;
2177
2178	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2179	    sppp_phase_name(sp->pp_phase));
2180
2181	/*
2182	 * Open all authentication protocols.  This is even required
2183	 * if we already proceeded to network phase, since it might be
2184	 * that remote wants us to authenticate, so we might have to
2185	 * send a PAP request.  Undesired authentication protocols
2186	 * don't do anything when they get an Open event.
2187	 */
2188	for (i = 0; i < IDX_COUNT; i++)
2189		if ((cps[i])->flags & CP_AUTH)
2190			(cps[i])->Open(sp);
2191
2192	if (sp->pp_phase == PHASE_NETWORK) {
2193		/* Notify all NCPs. */
2194		for (i = 0; i < IDX_COUNT; i++)
2195			if ((cps[i])->flags & CP_NCP)
2196				(cps[i])->Open(sp);
2197	}
2198
2199	/* Send Up events to all started protos. */
2200	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2201		if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0)
2202			(cps[i])->Up(sp);
2203
2204	if (sp->pp_phase == PHASE_NETWORK)
2205		/* if no NCP is starting, close down */
2206		sppp_lcp_check_and_close(sp);
2207}
2208
2209static void
2210sppp_lcp_tld(struct sppp *sp)
2211{
2212	STDDCL;
2213	int i;
2214	u_long mask;
2215
2216	sp->pp_phase = PHASE_TERMINATE;
2217
2218	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2219	    sppp_phase_name(sp->pp_phase));
2220
2221	/*
2222	 * Take upper layers down.  We send the Down event first and
2223	 * the Close second to prevent the upper layers from sending
2224	 * ``a flurry of terminate-request packets'', as the RFC
2225	 * describes it.
2226	 */
2227	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2228		if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0) {
2229			(cps[i])->Down(sp);
2230			(cps[i])->Close(sp);
2231		}
2232}
2233
2234static void
2235sppp_lcp_tls(struct sppp *sp)
2236{
2237	STDDCL;
2238
2239	sp->pp_phase = PHASE_ESTABLISH;
2240
2241	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2242	    sppp_phase_name(sp->pp_phase));
2243
2244	/* Notify lower layer if desired. */
2245	if (sp->pp_tls)
2246		(sp->pp_tls)(sp);
2247}
2248
2249static void
2250sppp_lcp_tlf(struct sppp *sp)
2251{
2252	STDDCL;
2253
2254	sp->pp_phase = PHASE_DEAD;
2255	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
2256	    sppp_phase_name(sp->pp_phase));
2257
2258	/* Notify lower layer if desired. */
2259	if (sp->pp_tlf)
2260		(sp->pp_tlf)(sp);
2261}
2262
2263static void
2264sppp_lcp_scr(struct sppp *sp)
2265{
2266	char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2267	int i = 0;
2268	u_short authproto;
2269
2270	if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2271		if (! sp->lcp.magic)
2272			sp->lcp.magic = time.tv_sec + time.tv_usec;
2273		opt[i++] = LCP_OPT_MAGIC;
2274		opt[i++] = 6;
2275		opt[i++] = sp->lcp.magic >> 24;
2276		opt[i++] = sp->lcp.magic >> 16;
2277		opt[i++] = sp->lcp.magic >> 8;
2278		opt[i++] = sp->lcp.magic;
2279	}
2280
2281	if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2282		opt[i++] = LCP_OPT_MRU;
2283		opt[i++] = 4;
2284		opt[i++] = sp->lcp.mru >> 8;
2285		opt[i++] = sp->lcp.mru;
2286	}
2287
2288	if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2289		authproto = sp->hisauth.proto;
2290		opt[i++] = LCP_OPT_AUTH_PROTO;
2291		opt[i++] = authproto == PPP_CHAP? 5: 4;
2292		opt[i++] = authproto >> 8;
2293		opt[i++] = authproto;
2294		if (authproto == PPP_CHAP)
2295			opt[i++] = CHAP_MD5;
2296	}
2297
2298	sp->confid[IDX_LCP] = ++sp->pp_seq;
2299	sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2300}
2301
2302/*
2303 * Check the open NCPs, return true if at least one NCP is open.
2304 */
2305static int
2306sppp_ncp_check(struct sppp *sp)
2307{
2308	int i, mask;
2309
2310	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2311		if (sp->lcp.protos & mask && (cps[i])->flags & CP_NCP)
2312			return 1;
2313	return 0;
2314}
2315
2316/*
2317 * Re-check the open NCPs and see if we should terminate the link.
2318 * Called by the NCPs during their tlf action handling.
2319 */
2320static void
2321sppp_lcp_check_and_close(struct sppp *sp)
2322{
2323
2324	if (sp->pp_phase < PHASE_NETWORK)
2325		/* don't bother, we are already going down */
2326		return;
2327
2328	if (sppp_ncp_check(sp))
2329		return;
2330
2331	lcp.Close(sp);
2332}
2333/*
2334 *--------------------------------------------------------------------------*
2335 *                                                                          *
2336 *                        The IPCP implementation.                          *
2337 *                                                                          *
2338 *--------------------------------------------------------------------------*
2339 */
2340
2341static void
2342sppp_ipcp_init(struct sppp *sp)
2343{
2344	sp->ipcp.opts = 0;
2345	sp->ipcp.flags = 0;
2346	sp->state[IDX_IPCP] = STATE_INITIAL;
2347	sp->fail_counter[IDX_IPCP] = 0;
2348	callout_handle_init(&sp->ch[IDX_IPCP]);
2349}
2350
2351static void
2352sppp_ipcp_up(struct sppp *sp)
2353{
2354	sppp_up_event(&ipcp, sp);
2355}
2356
2357static void
2358sppp_ipcp_down(struct sppp *sp)
2359{
2360	sppp_down_event(&ipcp, sp);
2361}
2362
2363static void
2364sppp_ipcp_open(struct sppp *sp)
2365{
2366	STDDCL;
2367	u_long myaddr, hisaddr;
2368
2369	sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2370	/*
2371	 * If we don't have his address, this probably means our
2372	 * interface doesn't want to talk IP at all.  (This could
2373	 * be the case if somebody wants to speak only IPX, for
2374	 * example.)  Don't open IPCP in this case.
2375	 */
2376	if (hisaddr == 0L) {
2377		/* XXX this message should go away */
2378		if (debug)
2379			log(LOG_DEBUG, "%s%d: ipcp_open(): no IP interface\n",
2380			    ifp->if_name, ifp->if_unit);
2381		return;
2382	}
2383
2384	if (myaddr == 0L) {
2385		/*
2386		 * I don't have an assigned address, so i need to
2387		 * negotiate my address.
2388		 */
2389		sp->ipcp.flags |= IPCP_MYADDR_DYN;
2390		sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2391	}
2392	sppp_open_event(&ipcp, sp);
2393}
2394
2395static void
2396sppp_ipcp_close(struct sppp *sp)
2397{
2398	sppp_close_event(&ipcp, sp);
2399	if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2400		/*
2401		 * My address was dynamic, clear it again.
2402		 */
2403		sppp_set_ip_addr(sp, 0L);
2404}
2405
2406static void
2407sppp_ipcp_TO(void *cookie)
2408{
2409	sppp_to_event(&ipcp, (struct sppp *)cookie);
2410}
2411
2412/*
2413 * Analyze a configure request.  Return true if it was agreeable, and
2414 * caused action sca, false if it has been rejected or nak'ed, and
2415 * caused action scn.  (The return value is used to make the state
2416 * transition decision in the state automaton.)
2417 */
2418static int
2419sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2420{
2421	u_char *buf, *r, *p;
2422	struct ifnet *ifp = &sp->pp_if;
2423	int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2424	u_long hisaddr, desiredaddr;
2425
2426	len -= 4;
2427	origlen = len;
2428	/*
2429	 * Make sure to allocate a buf that can at least hold a
2430	 * conf-nak with an `address' option.  We might need it below.
2431	 */
2432	buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2433	if (! buf)
2434		return (0);
2435
2436	/* pass 1: see if we can recognize them */
2437	if (debug)
2438		log(LOG_DEBUG, "%s%d: ipcp parse opts: ",
2439		    ifp->if_name, ifp->if_unit);
2440	p = (void*) (h+1);
2441	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2442		if (debug)
2443			addlog(" %s ", sppp_ipcp_opt_name(*p));
2444		switch (*p) {
2445#ifdef notyet
2446		case IPCP_OPT_COMPRESSION:
2447			if (len >= 6 && p[1] >= 6) {
2448				/* correctly formed compress option */
2449				continue;
2450			}
2451			if (debug)
2452				addlog("[invalid] ");
2453			break;
2454#endif
2455		case IPCP_OPT_ADDRESS:
2456			if (len >= 6 && p[1] == 6) {
2457				/* correctly formed address option */
2458				continue;
2459			}
2460			if (debug)
2461				addlog("[invalid] ");
2462			break;
2463		default:
2464			/* Others not supported. */
2465			if (debug)
2466				addlog("[rej] ");
2467			break;
2468		}
2469		/* Add the option to rejected list. */
2470		bcopy (p, r, p[1]);
2471		r += p[1];
2472		rlen += p[1];
2473	}
2474	if (rlen) {
2475		if (debug)
2476			addlog(" send conf-rej\n");
2477		sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2478		return 0;
2479	} else if (debug)
2480		addlog("\n");
2481
2482	/* pass 2: parse option values */
2483	sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
2484	if (debug)
2485		log(LOG_DEBUG, "%s%d: ipcp parse opt values: ",
2486		       ifp->if_name, ifp->if_unit);
2487	p = (void*) (h+1);
2488	len = origlen;
2489	for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2490		if (debug)
2491			addlog(" %s ", sppp_ipcp_opt_name(*p));
2492		switch (*p) {
2493#ifdef notyet
2494		case IPCP_OPT_COMPRESSION:
2495			continue;
2496#endif
2497		case IPCP_OPT_ADDRESS:
2498			desiredaddr = p[2] << 24 | p[3] << 16 |
2499				p[4] << 8 | p[5];
2500			if (desiredaddr == hisaddr) {
2501				/*
2502				 * Peer's address is same as our value,
2503				 * this is agreeable.  Gonna conf-ack
2504				 * it.
2505				 */
2506				if (debug)
2507					addlog("%s [ack] ",
2508					       sppp_dotted_quad(hisaddr));
2509				/* record that we've seen it already */
2510				sp->ipcp.flags |= IPCP_HISADDR_SEEN;
2511				continue;
2512			}
2513			/*
2514			 * The address wasn't agreeable.  This is either
2515			 * he sent us 0.0.0.0, asking to assign him an
2516			 * address, or he send us another address not
2517			 * matching our value.  Either case, we gonna
2518			 * conf-nak it with our value.
2519			 */
2520			if (debug) {
2521				if (desiredaddr == 0)
2522					addlog("[addr requested] ");
2523				else
2524					addlog("%s [not agreed] ",
2525					       sppp_dotted_quad(desiredaddr));
2526
2527				p[2] = hisaddr >> 24;
2528				p[3] = hisaddr >> 16;
2529				p[4] = hisaddr >> 8;
2530				p[5] = hisaddr;
2531			}
2532			break;
2533		}
2534		/* Add the option to nak'ed list. */
2535		bcopy (p, r, p[1]);
2536		r += p[1];
2537		rlen += p[1];
2538	}
2539
2540	/*
2541	 * If we are about to conf-ack the request, but haven't seen
2542	 * his address so far, gonna conf-nak it instead, with the
2543	 * `address' option present and our idea of his address being
2544	 * filled in there, to request negotiation of both addresses.
2545	 *
2546	 * XXX This can result in an endless req - nak loop if peer
2547	 * doesn't want to send us his address.  Q: What should we do
2548	 * about it?  XXX  A: implement the max-failure counter.
2549	 */
2550	if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN)) {
2551		buf[0] = IPCP_OPT_ADDRESS;
2552		buf[1] = 6;
2553		buf[2] = hisaddr >> 24;
2554		buf[3] = hisaddr >> 16;
2555		buf[4] = hisaddr >> 8;
2556		buf[5] = hisaddr;
2557		rlen = 6;
2558		if (debug)
2559			addlog("still need hisaddr ");
2560	}
2561
2562	if (rlen) {
2563		if (debug)
2564			addlog(" send conf-nak\n");
2565		sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
2566	} else {
2567		if (debug)
2568			addlog(" send conf-ack\n");
2569		sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
2570			      h->ident, origlen, h+1);
2571	}
2572
2573	free (buf, M_TEMP);
2574	return (rlen == 0);
2575}
2576
2577/*
2578 * Analyze the IPCP Configure-Reject option list, and adjust our
2579 * negotiation.
2580 */
2581static void
2582sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2583{
2584	u_char *buf, *p;
2585	struct ifnet *ifp = &sp->pp_if;
2586	int debug = ifp->if_flags & IFF_DEBUG;
2587
2588	len -= 4;
2589	buf = malloc (len, M_TEMP, M_NOWAIT);
2590	if (!buf)
2591		return;
2592
2593	if (debug)
2594		log(LOG_DEBUG, "%s%d: ipcp rej opts: ",
2595		    ifp->if_name, ifp->if_unit);
2596
2597	p = (void*) (h+1);
2598	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2599		if (debug)
2600			addlog(" %s ", sppp_ipcp_opt_name(*p));
2601		switch (*p) {
2602		case IPCP_OPT_ADDRESS:
2603			/*
2604			 * Peer doesn't grok address option.  This is
2605			 * bad.  XXX  Should we better give up here?
2606			 */
2607			sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
2608			break;
2609#ifdef notyet
2610		case IPCP_OPT_COMPRESS:
2611			sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS);
2612			break;
2613#endif
2614		}
2615	}
2616	if (debug)
2617		addlog("\n");
2618	free (buf, M_TEMP);
2619	return;
2620}
2621
2622/*
2623 * Analyze the IPCP Configure-NAK option list, and adjust our
2624 * negotiation.
2625 */
2626static void
2627sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2628{
2629	u_char *buf, *p;
2630	struct ifnet *ifp = &sp->pp_if;
2631	int debug = ifp->if_flags & IFF_DEBUG;
2632	u_long wantaddr;
2633
2634	len -= 4;
2635	buf = malloc (len, M_TEMP, M_NOWAIT);
2636	if (!buf)
2637		return;
2638
2639	if (debug)
2640		log(LOG_DEBUG, "%s%d: ipcp nak opts: ",
2641		    ifp->if_name, ifp->if_unit);
2642
2643	p = (void*) (h+1);
2644	for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2645		if (debug)
2646			addlog(" %s ", sppp_ipcp_opt_name(*p));
2647		switch (*p) {
2648		case IPCP_OPT_ADDRESS:
2649			/*
2650			 * Peer doesn't like our local IP address.  See
2651			 * if we can do something for him.  We'll drop
2652			 * him our address then.
2653			 */
2654			if (len >= 6 && p[1] == 6) {
2655				wantaddr = p[2] << 24 | p[3] << 16 |
2656					p[4] << 8 | p[5];
2657				sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2658				if (debug)
2659					addlog("[wantaddr %s] ",
2660					       sppp_dotted_quad(wantaddr));
2661				/*
2662				 * When doing dynamic address assignment,
2663				 * we accept his offer.  Otherwise, we
2664				 * ignore it and thus continue to negotiate
2665				 * our already existing value.
2666				 */
2667				if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
2668					sppp_set_ip_addr(sp, wantaddr);
2669					if (debug)
2670						addlog("[agree] ");
2671				}
2672			}
2673			break;
2674#ifdef notyet
2675		case IPCP_OPT_COMPRESS:
2676			/*
2677			 * Peer wants different compression parameters.
2678			 */
2679			break;
2680#endif
2681		}
2682	}
2683	if (debug)
2684		addlog("\n");
2685	free (buf, M_TEMP);
2686	return;
2687}
2688
2689static void
2690sppp_ipcp_tlu(struct sppp *sp)
2691{
2692}
2693
2694static void
2695sppp_ipcp_tld(struct sppp *sp)
2696{
2697}
2698
2699static void
2700sppp_ipcp_tls(struct sppp *sp)
2701{
2702	/* indicate to LCP that it must stay alive */
2703	sp->lcp.protos |= (1 << IDX_IPCP);
2704}
2705
2706static void
2707sppp_ipcp_tlf(struct sppp *sp)
2708{
2709	/* we no longer need LCP */
2710	sp->lcp.protos &= ~(1 << IDX_IPCP);
2711	sppp_lcp_check_and_close(sp);
2712}
2713
2714static void
2715sppp_ipcp_scr(struct sppp *sp)
2716{
2717	char opt[6 /* compression */ + 6 /* address */];
2718	u_long ouraddr;
2719	int i = 0;
2720
2721#ifdef notyet
2722	if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
2723		opt[i++] = IPCP_OPT_COMPRESSION;
2724		opt[i++] = 6;
2725		opt[i++] = 0;	/* VJ header compression */
2726		opt[i++] = 0x2d; /* VJ header compression */
2727		opt[i++] = max_slot_id;
2728		opt[i++] = comp_slot_id;
2729	}
2730#endif
2731
2732	if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
2733		sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
2734		opt[i++] = IPCP_OPT_ADDRESS;
2735		opt[i++] = 6;
2736		opt[i++] = ouraddr >> 24;
2737		opt[i++] = ouraddr >> 16;
2738		opt[i++] = ouraddr >> 8;
2739		opt[i++] = ouraddr;
2740	}
2741
2742	sp->confid[IDX_IPCP] = ++sp->pp_seq;
2743	sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
2744}
2745
2746
2747/*
2748 *--------------------------------------------------------------------------*
2749 *                                                                          *
2750 *                        The CHAP implementation.                          *
2751 *                                                                          *
2752 *--------------------------------------------------------------------------*
2753 */
2754
2755/*
2756 * The authentication protocols don't employ a full-fledged state machine as
2757 * the control protocols do, since they do have Open and Close events, but
2758 * not Up and Down, nor are they explicitly terminated.  Also, use of the
2759 * authentication protocols may be different in both directions (this makes
2760 * sense, think of a machine that never accepts incoming calls but only
2761 * calls out, it doesn't require the called party to authenticate itself).
2762 *
2763 * Our state machine for the local authentication protocol (we are requesting
2764 * the peer to authenticate) looks like:
2765 *
2766 *						    RCA-
2767 *	      +--------------------------------------------+
2768 *	      V					    scn,tld|
2769 *	  +--------+			       Close   +---------+ RCA+
2770 *	  |	   |<----------------------------------|	 |------+
2771 *   +--->| Closed |				TO*    | Opened	 | sca	|
2772 *   |	  |	   |-----+		       +-------|	 |<-----+
2773 *   |	  +--------+ irc |		       |       +---------+
2774 *   |	    ^		 |		       |	   ^
2775 *   |	    |		 |		       |	   |
2776 *   |	    |		 |		       |	   |
2777 *   |	 TO-|		 |		       |	   |
2778 *   |	    |tld  TO+	 V		       |	   |
2779 *   |	    |	+------->+		       |	   |
2780 *   |	    |	|	 |		       |	   |
2781 *   |	  +--------+	 V		       |	   |
2782 *   |	  |	   |<----+<--------------------+	   |
2783 *   |	  | Req-   | scr				   |
2784 *   |	  | Sent   |					   |
2785 *   |	  |	   |					   |
2786 *   |	  +--------+					   |
2787 *   | RCA- |	| RCA+					   |
2788 *   +------+	+------------------------------------------+
2789 *   scn,tld	  sca,irc,ict,tlu
2790 *
2791 *
2792 *   with:
2793 *
2794 *	Open:	LCP reached authentication phase
2795 *	Close:	LCP reached terminate phase
2796 *
2797 *	RCA+:	received reply (pap-req, chap-response), acceptable
2798 *	RCN:	received reply (pap-req, chap-response), not acceptable
2799 *	TO+:	timeout with restart counter >= 0
2800 *	TO-:	timeout with restart counter < 0
2801 *	TO*:	reschedule timeout for CHAP
2802 *
2803 *	scr:	send request packet (none for PAP, chap-challenge)
2804 *	sca:	send ack packet (pap-ack, chap-success)
2805 *	scn:	send nak packet (pap-nak, chap-failure)
2806 *	ict:	initialize re-challenge timer (CHAP only)
2807 *
2808 *	tlu:	this-layer-up, LCP reaches network phase
2809 *	tld:	this-layer-down, LCP enters terminate phase
2810 *
2811 * Note that in CHAP mode, after sending a new challenge, while the state
2812 * automaton falls back into Req-Sent state, it doesn't signal a tld
2813 * event to LCP, so LCP remains in network phase.  Only after not getting
2814 * any response (or after getting an unacceptable response), CHAP closes,
2815 * causing LCP to enter terminate phase.
2816 *
2817 * With PAP, there is no initial request that can be sent.  The peer is
2818 * expected to send one based on the successful negotiation of PAP as
2819 * the authentication protocol during the LCP option negotiation.
2820 *
2821 * Incoming authentication protocol requests (remote requests
2822 * authentication, we are peer) don't employ a state machine at all,
2823 * they are simply answered.  Some peers [Ascend P50 firmware rev
2824 * 4.50] react allergically when sending IPCP requests while they are
2825 * still in authentication phase (thereby violating the standard that
2826 * demands that these NCP packets are to be discarded), so we keep
2827 * track of the peer demanding us to authenticate, and only proceed to
2828 * phase network once we've seen a positive acknowledge for the
2829 * authentication.
2830 */
2831
2832/*
2833 * Handle incoming CHAP packets.
2834 */
2835void
2836sppp_chap_input(struct sppp *sp, struct mbuf *m)
2837{
2838	STDDCL;
2839	struct lcp_header *h;
2840	int len, x;
2841	u_char *value, *name, digest[AUTHKEYLEN], dsize;
2842	int value_len, name_len;
2843	MD5_CTX ctx;
2844
2845	len = m->m_pkthdr.len;
2846	if (len < 4) {
2847		if (debug)
2848			log(LOG_DEBUG,
2849			    "%s%d: chap invalid packet length: %d bytes\n",
2850			    ifp->if_name, ifp->if_unit, len);
2851		return;
2852	}
2853	h = mtod (m, struct lcp_header*);
2854	if (len > ntohs (h->len))
2855		len = ntohs (h->len);
2856
2857	switch (h->type) {
2858	/* challenge, failure and success are his authproto */
2859	case CHAP_CHALLENGE:
2860		value = 1 + (u_char*)(h+1);
2861		value_len = value[-1];
2862		name = value + value_len;
2863		name_len = len - value_len - 5;
2864		if (name_len < 0) {
2865			if (debug) {
2866				log(LOG_DEBUG,
2867				    "%s%d: chap corrupted challenge "
2868				    "<%s id=0x%x len=%d",
2869				    ifp->if_name, ifp->if_unit,
2870				    sppp_auth_type_name(PPP_CHAP, h->type),
2871				    h->ident, ntohs(h->len));
2872				if (len > 4)
2873					sppp_print_bytes((u_char*) (h+1), len-4);
2874				addlog(">\n");
2875			}
2876			break;
2877		}
2878		if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
2879		    || bcmp(name, sp->hisauth.name, name_len) != 0) {
2880			log(LOG_INFO, "%s%d: chap challenge, his name ");
2881			sppp_print_string(name, name_len);
2882			addlog(" != expected ");
2883			sppp_print_string(sp->hisauth.name,
2884					  sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
2885			addlog("\n");
2886		}
2887
2888		if (debug) {
2889			log(LOG_DEBUG,
2890			    "%s%d: chap input <%s id=0x%x len=%d name=",
2891			    ifp->if_name, ifp->if_unit,
2892			    sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
2893			    ntohs(h->len));
2894			sppp_print_string((char*) name, name_len);
2895			addlog(" value-size=%d value=", value_len);
2896			sppp_print_bytes(value, value_len);
2897			addlog(">\n");
2898		}
2899
2900		/* Compute reply value. */
2901		MD5Init(&ctx);
2902		MD5Update(&ctx, &h->ident, 1);
2903		MD5Update(&ctx, sp->myauth.secret,
2904			  sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
2905		MD5Update(&ctx, value, value_len);
2906		MD5Final(digest, &ctx);
2907		dsize = sizeof digest;
2908
2909		sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
2910			       sizeof dsize, (const char *)&dsize,
2911			       sizeof digest, digest,
2912			       sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
2913			       sp->myauth.name,
2914			       0);
2915		break;
2916
2917	case CHAP_SUCCESS:
2918		if (debug) {
2919			log(LOG_DEBUG, "%s%d: chap success",
2920			    ifp->if_name, ifp->if_unit);
2921			if (len > 4) {
2922				addlog(": ");
2923				sppp_print_string((char*)(h + 1), len - 4);
2924			}
2925			addlog("\n");
2926		}
2927		x = splimp();
2928		sp->pp_flags &= ~PP_NEEDAUTH;
2929		if (sp->myauth.proto == PPP_CHAP &&
2930		    (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
2931			/*
2932			 * We are authenticator for CHAP but didn't
2933			 * complete yet.  Leave it to tlu to proceed
2934			 * to network phase.
2935			 */
2936			splx(x);
2937			break;
2938		}
2939		splx(x);
2940		sppp_phase_network(sp);
2941		break;
2942
2943	case CHAP_FAILURE:
2944		if (debug) {
2945			log(LOG_INFO, "%s%d: chap failure",
2946			    ifp->if_name, ifp->if_unit);
2947			if (len > 4) {
2948				addlog(": ");
2949				sppp_print_string((char*)(h + 1), len - 4);
2950			}
2951			addlog("\n");
2952		} else
2953			log(LOG_INFO, "%s%d: chap failure\n",
2954			    ifp->if_name, ifp->if_unit);
2955		/* await LCP shutdown by authenticator */
2956		break;
2957
2958	/* response is my authproto */
2959	case CHAP_RESPONSE:
2960		value = 1 + (u_char*)(h+1);
2961		value_len = value[-1];
2962		name = value + value_len;
2963		name_len = len - value_len - 5;
2964		if (name_len < 0) {
2965			if (debug) {
2966				log(LOG_DEBUG,
2967				    "%s%d: chap corrupted response "
2968				    "<%s id=0x%x len=%d",
2969				    ifp->if_name, ifp->if_unit,
2970				    sppp_auth_type_name(PPP_CHAP, h->type),
2971				    h->ident, ntohs(h->len));
2972				if (len > 4)
2973					sppp_print_bytes((u_char*)(h+1), len-4);
2974				addlog(">\n");
2975			}
2976			break;
2977		}
2978		if (h->ident != sp->confid[IDX_CHAP]) {
2979			if (debug)
2980				log(LOG_DEBUG,
2981				    "%s%d: chap dropping response for old ID "
2982				    "(got %d, expected %d)\n",
2983				    h->ident, sp->confid[IDX_CHAP]);
2984			break;
2985		}
2986		if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
2987		    || bcmp(name, sp->hisauth.name, name_len) != 0) {
2988			log(LOG_INFO, "%s%d: chap response, his name ",
2989			    ifp->if_name, ifp->if_unit);
2990			sppp_print_string(name, name_len);
2991			addlog(" != expected ");
2992			sppp_print_string(sp->hisauth.name,
2993					  sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
2994			addlog("\n");
2995		}
2996		if (debug) {
2997			log(LOG_DEBUG, "%s%d: chap input(%s) "
2998			    "<%s id=0x%x len=%d name=",
2999			    ifp->if_name, ifp->if_unit,
3000			    sppp_state_name(sp->state[IDX_CHAP]),
3001			    sppp_auth_type_name(PPP_CHAP, h->type),
3002			    h->ident, ntohs (h->len));
3003			sppp_print_string((char*)name, name_len);
3004			addlog(" value-size=%d value=", value_len);
3005			sppp_print_bytes(value, value_len);
3006			addlog(">\n");
3007		}
3008		if (value_len != AUTHKEYLEN) {
3009			if (debug)
3010				log(LOG_DEBUG,
3011				    "%s%d: chap bad hash value length: "
3012				    "%d bytes, should be %d\n",
3013				    ifp->if_name, ifp->if_unit, value_len,
3014				    AUTHKEYLEN);
3015			break;
3016		}
3017
3018		MD5Init(&ctx);
3019		MD5Update(&ctx, &h->ident, 1);
3020		MD5Update(&ctx, sp->hisauth.secret,
3021			  sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
3022		MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
3023		MD5Final(digest, &ctx);
3024
3025#define FAILMSG "Failed..."
3026#define SUCCMSG "Welcome!"
3027
3028		if (value_len != sizeof digest ||
3029		    bcmp(digest, value, value_len) != 0) {
3030			/* action scn, tld */
3031			sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
3032				       sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3033				       0);
3034			chap.tld(sp);
3035			break;
3036		}
3037		/* action sca, perhaps tlu */
3038		if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
3039		    sp->state[IDX_CHAP] == STATE_OPENED)
3040			sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
3041				       sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3042				       0);
3043		if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
3044			sppp_cp_change_state(&chap, sp, STATE_OPENED);
3045			chap.tlu(sp);
3046		}
3047		break;
3048
3049	default:
3050		/* Unknown CHAP packet type -- ignore. */
3051		if (debug) {
3052			log(LOG_DEBUG, "%s%d: chap unknown input(%s) "
3053			    "<0x%x id=0x%xh len=%d",
3054			    ifp->if_name, ifp->if_unit,
3055			    sppp_state_name(sp->state[IDX_CHAP]),
3056			    h->type, h->ident, ntohs(h->len));
3057			if (len > 4)
3058				sppp_print_bytes((u_char*)(h+1), len-4);
3059			addlog(">\n");
3060		}
3061		break;
3062
3063	}
3064}
3065
3066static void
3067sppp_chap_init(struct sppp *sp)
3068{
3069	/* Chap doesn't have STATE_INITIAL at all. */
3070	sp->state[IDX_CHAP] = STATE_CLOSED;
3071	sp->fail_counter[IDX_CHAP] = 0;
3072	callout_handle_init(&sp->ch[IDX_CHAP]);
3073}
3074
3075static void
3076sppp_chap_open(struct sppp *sp)
3077{
3078	if (sp->myauth.proto == PPP_CHAP &&
3079	    (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3080		/* we are authenticator for CHAP, start it */
3081		chap.scr(sp);
3082		sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3083		sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3084	}
3085	/* nothing to be done if we are peer, await a challenge */
3086}
3087
3088static void
3089sppp_chap_close(struct sppp *sp)
3090{
3091	if (sp->state[IDX_CHAP] != STATE_CLOSED)
3092		sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3093}
3094
3095static void
3096sppp_chap_TO(void *cookie)
3097{
3098	struct sppp *sp = (struct sppp *)cookie;
3099	STDDCL;
3100	int s;
3101
3102	s = splimp();
3103	if (debug)
3104		log(LOG_DEBUG, "%s%d: chap TO(%s) rst_counter = %d\n",
3105		    ifp->if_name, ifp->if_unit,
3106		    sppp_state_name(sp->state[IDX_CHAP]),
3107		    sp->rst_counter[IDX_CHAP]);
3108
3109	if (--sp->rst_counter[IDX_CHAP] < 0)
3110		/* TO- event */
3111		switch (sp->state[IDX_CHAP]) {
3112		case STATE_REQ_SENT:
3113			chap.tld(sp);
3114			sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3115			break;
3116		}
3117	else
3118		/* TO+ (or TO*) event */
3119		switch (sp->state[IDX_CHAP]) {
3120		case STATE_OPENED:
3121			/* TO* event */
3122			sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3123			/* fall through */
3124		case STATE_REQ_SENT:
3125			chap.scr(sp);
3126			/* sppp_cp_change_state() will restart the timer */
3127			sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3128			break;
3129		}
3130
3131	splx(s);
3132}
3133
3134static void
3135sppp_chap_tlu(struct sppp *sp)
3136{
3137	STDDCL;
3138	int i, x;
3139
3140	sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3141
3142	/*
3143	 * Some broken CHAP implementations (Conware CoNet, firmware
3144	 * 4.0.?) don't want to re-authenticate their CHAP once the
3145	 * initial challenge-response exchange has taken place.
3146	 * Provide for an option to avoid rechallenges.
3147	 */
3148	if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
3149		/*
3150		 * Compute the re-challenge timeout.  This will yield
3151		 * a number between 300 and 810 seconds.
3152		 */
3153		i = 300 + ((unsigned)(random() & 0xff00) >> 7);
3154
3155		sp->ch[IDX_CHAP] = timeout(chap.TO, (void *)sp, i * hz);
3156	}
3157
3158	if (debug) {
3159		log(LOG_DEBUG,
3160		    "%s%d: chap %s, ",
3161		    ifp->if_name, ifp->if_unit,
3162		    sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
3163		if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
3164			addlog("next re-challenge in %d seconds\n", i);
3165		else
3166			addlog("re-challenging supressed\n");
3167	}
3168
3169	x = splimp();
3170	/* indicate to LCP that we need to be closed down */
3171	sp->lcp.protos |= (1 << IDX_CHAP);
3172
3173	if (sp->pp_flags & PP_NEEDAUTH) {
3174		/*
3175		 * Remote is authenticator, but his auth proto didn't
3176		 * complete yet.  Defer the transition to network
3177		 * phase.
3178		 */
3179		splx(x);
3180		return;
3181	}
3182	splx(x);
3183
3184	/*
3185	 * If we are already in phase network, we are done here.  This
3186	 * is the case if this is a dummy tlu event after a re-challenge.
3187	 */
3188	if (sp->pp_phase != PHASE_NETWORK)
3189		sppp_phase_network(sp);
3190}
3191
3192static void
3193sppp_chap_tld(struct sppp *sp)
3194{
3195	STDDCL;
3196
3197	if (debug)
3198		log(LOG_DEBUG, "%s%d: chap tld\n", ifp->if_name, ifp->if_unit);
3199	untimeout(chap.TO, (void *)sp, sp->ch[IDX_CHAP]);
3200	sp->lcp.protos &= ~(1 << IDX_CHAP);
3201
3202	lcp.Close(sp);
3203}
3204
3205static void
3206sppp_chap_scr(struct sppp *sp)
3207{
3208	struct timeval tv;
3209	u_long *ch, seed;
3210	u_char clen;
3211
3212	/* Compute random challenge. */
3213	ch = (u_long *)sp->myauth.challenge;
3214	microtime(&tv);
3215	seed = tv.tv_sec ^ tv.tv_usec;
3216	ch[0] = seed ^ random();
3217	ch[1] = seed ^ random();
3218	ch[2] = seed ^ random();
3219	ch[3] = seed ^ random();
3220	clen = AUTHKEYLEN;
3221
3222	sp->confid[IDX_CHAP] = ++sp->pp_seq;
3223
3224	sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
3225		       sizeof clen, (const char *)&clen,
3226		       AUTHKEYLEN, sp->myauth.challenge,
3227		       sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3228		       sp->myauth.name,
3229		       0);
3230}
3231/*
3232 *--------------------------------------------------------------------------*
3233 *                                                                          *
3234 *                        The PAP implementation.                           *
3235 *                                                                          *
3236 *--------------------------------------------------------------------------*
3237 */
3238/*
3239 * For PAP, we need to keep a little state also if we are the peer, not the
3240 * authenticator.  This is since we don't get a request to authenticate, but
3241 * have to repeatedly authenticate ourself until we got a response (or the
3242 * retry counter is expired).
3243 */
3244
3245/*
3246 * Handle incoming PAP packets.  */
3247static void
3248sppp_pap_input(struct sppp *sp, struct mbuf *m)
3249{
3250	STDDCL;
3251	struct lcp_header *h;
3252	int len, x;
3253	u_char *name, *passwd, mlen;
3254	int name_len, passwd_len;
3255
3256	len = m->m_pkthdr.len;
3257	if (len < 5) {
3258		if (debug)
3259			log(LOG_DEBUG,
3260			    "%s%d: pap invalid packet length: %d bytes\n",
3261			    ifp->if_name, ifp->if_unit, len);
3262		return;
3263	}
3264	h = mtod (m, struct lcp_header*);
3265	if (len > ntohs (h->len))
3266		len = ntohs (h->len);
3267	switch (h->type) {
3268	/* PAP request is my authproto */
3269	case PAP_REQ:
3270		name = 1 + (u_char*)(h+1);
3271		name_len = name[-1];
3272		passwd = name + name_len + 1;
3273		if (name_len > len - 6 ||
3274		    (passwd_len = passwd[-1]) > len - 6 - name_len) {
3275			if (debug) {
3276				log(LOG_DEBUG, "%s%d: pap corrupted input "
3277				    "<%s id=0x%x len=%d",
3278				    ifp->if_name, ifp->if_unit,
3279				    sppp_auth_type_name(PPP_PAP, h->type),
3280				    h->ident, ntohs(h->len));
3281				if (len > 4)
3282					sppp_print_bytes((u_char*)(h+1), len-4);
3283				addlog(">\n");
3284			}
3285			break;
3286		}
3287		if (debug) {
3288			log(LOG_DEBUG, "%s%d: pap input(%s) "
3289			    "<%s id=0x%x len=%d name=",
3290			    ifp->if_name, ifp->if_unit,
3291			    sppp_state_name(sp->state[IDX_PAP]),
3292			    sppp_auth_type_name(PPP_PAP, h->type),
3293			    h->ident, ntohs(h->len));
3294			sppp_print_string((char*)name, name_len);
3295			addlog(" passwd=");
3296			sppp_print_string((char*)passwd, passwd_len);
3297			addlog(">\n");
3298		}
3299		if (name_len > AUTHNAMELEN ||
3300		    passwd_len > AUTHKEYLEN ||
3301		    bcmp(name, sp->hisauth.name, name_len) != 0 ||
3302		    bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
3303			/* action scn, tld */
3304			mlen = sizeof(FAILMSG) - 1;
3305			sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
3306				       sizeof mlen, (const char *)&mlen,
3307				       sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3308				       0);
3309			pap.tld(sp);
3310			break;
3311		}
3312		/* action sca, perhaps tlu */
3313		if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
3314		    sp->state[IDX_PAP] == STATE_OPENED) {
3315			mlen = sizeof(SUCCMSG) - 1;
3316			sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
3317				       sizeof mlen, (const char *)&mlen,
3318				       sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3319				       0);
3320		}
3321		if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
3322			sppp_cp_change_state(&pap, sp, STATE_OPENED);
3323			pap.tlu(sp);
3324		}
3325		break;
3326
3327	/* ack and nak are his authproto */
3328	case PAP_ACK:
3329		untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3330		if (debug) {
3331			log(LOG_DEBUG, "%s%d: pap success",
3332			    ifp->if_name, ifp->if_unit);
3333			name_len = *((char *)h);
3334			if (len > 5 && name_len) {
3335				addlog(": ");
3336				sppp_print_string((char*)(h+1), name_len);
3337			}
3338			addlog("\n");
3339		}
3340		x = splimp();
3341		sp->pp_flags &= ~PP_NEEDAUTH;
3342		if (sp->myauth.proto == PPP_PAP &&
3343		    (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
3344			/*
3345			 * We are authenticator for PAP but didn't
3346			 * complete yet.  Leave it to tlu to proceed
3347			 * to network phase.
3348			 */
3349			splx(x);
3350			break;
3351		}
3352		splx(x);
3353		sppp_phase_network(sp);
3354		break;
3355
3356	case PAP_NAK:
3357		untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3358		if (debug) {
3359			log(LOG_INFO, "%s%d: pap failure",
3360			    ifp->if_name, ifp->if_unit);
3361			name_len = *((char *)h);
3362			if (len > 5 && name_len) {
3363				addlog(": ");
3364				sppp_print_string((char*)(h+1), name_len);
3365			}
3366			addlog("\n");
3367		} else
3368			log(LOG_INFO, "%s%d: pap failure\n",
3369			    ifp->if_name, ifp->if_unit);
3370		/* await LCP shutdown by authenticator */
3371		break;
3372
3373	default:
3374		/* Unknown PAP packet type -- ignore. */
3375		if (debug) {
3376			log(LOG_DEBUG, "%s%d: pap corrupted input "
3377			    "<0x%x id=0x%x len=%d",
3378			    ifp->if_name, ifp->if_unit,
3379			    h->type, h->ident, ntohs(h->len));
3380			if (len > 4)
3381				sppp_print_bytes((u_char*)(h+1), len-4);
3382			addlog(">\n");
3383		}
3384		break;
3385
3386	}
3387}
3388
3389static void
3390sppp_pap_init(struct sppp *sp)
3391{
3392	/* PAP doesn't have STATE_INITIAL at all. */
3393	sp->state[IDX_PAP] = STATE_CLOSED;
3394	sp->fail_counter[IDX_PAP] = 0;
3395	callout_handle_init(&sp->ch[IDX_PAP]);
3396	callout_handle_init(&sp->pap_my_to_ch);
3397}
3398
3399static void
3400sppp_pap_open(struct sppp *sp)
3401{
3402	if (sp->hisauth.proto == PPP_PAP &&
3403	    (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3404		/* we are authenticator for PAP, start our timer */
3405		sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
3406		sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
3407	}
3408	if (sp->myauth.proto == PPP_PAP) {
3409		/* we are peer, send a request, and start a timer */
3410		pap.scr(sp);
3411		sp->pap_my_to_ch = timeout(sppp_pap_my_TO, (void *)sp,
3412					   sp->lcp.timeout);
3413	}
3414}
3415
3416static void
3417sppp_pap_close(struct sppp *sp)
3418{
3419	if (sp->state[IDX_PAP] != STATE_CLOSED)
3420		sppp_cp_change_state(&pap, sp, STATE_CLOSED);
3421}
3422
3423/*
3424 * That's the timeout routine if we are authenticator.  Since the
3425 * authenticator is basically passive in PAP, we can't do much here.
3426 */
3427static void
3428sppp_pap_TO(void *cookie)
3429{
3430	struct sppp *sp = (struct sppp *)cookie;
3431	STDDCL;
3432	int s;
3433
3434	s = splimp();
3435	if (debug)
3436		log(LOG_DEBUG, "%s%d: pap TO(%s) rst_counter = %d\n",
3437		    ifp->if_name, ifp->if_unit,
3438		    sppp_state_name(sp->state[IDX_PAP]),
3439		    sp->rst_counter[IDX_PAP]);
3440
3441	if (--sp->rst_counter[IDX_PAP] < 0)
3442		/* TO- event */
3443		switch (sp->state[IDX_PAP]) {
3444		case STATE_REQ_SENT:
3445			pap.tld(sp);
3446			sppp_cp_change_state(&pap, sp, STATE_CLOSED);
3447			break;
3448		}
3449	else
3450		/* TO+ event, not very much we could do */
3451		switch (sp->state[IDX_PAP]) {
3452		case STATE_REQ_SENT:
3453			/* sppp_cp_change_state() will restart the timer */
3454			sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
3455			break;
3456		}
3457
3458	splx(s);
3459}
3460
3461/*
3462 * That's the timeout handler if we are peer.  Since the peer is active,
3463 * we need to retransmit our PAP request since it is apparently lost.
3464 * XXX We should impose a max counter.
3465 */
3466static void
3467sppp_pap_my_TO(void *cookie)
3468{
3469	struct sppp *sp = (struct sppp *)cookie;
3470	STDDCL;
3471
3472	if (debug)
3473		log(LOG_DEBUG, "%s%d: pap peer TO\n",
3474		    ifp->if_name, ifp->if_unit);
3475
3476	pap.scr(sp);
3477}
3478
3479static void
3480sppp_pap_tlu(struct sppp *sp)
3481{
3482	STDDCL;
3483	int x;
3484
3485	sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
3486
3487	if (debug)
3488		log(LOG_DEBUG, "%s%d: %s tlu\n",
3489		    ifp->if_name, ifp->if_unit, pap.name);
3490
3491	x = splimp();
3492	/* indicate to LCP that we need to be closed down */
3493	sp->lcp.protos |= (1 << IDX_PAP);
3494
3495	if (sp->pp_flags & PP_NEEDAUTH) {
3496		/*
3497		 * Remote is authenticator, but his auth proto didn't
3498		 * complete yet.  Defer the transition to network
3499		 * phase.
3500		 */
3501		splx(x);
3502		return;
3503	}
3504	splx(x);
3505	sppp_phase_network(sp);
3506}
3507
3508static void
3509sppp_pap_tld(struct sppp *sp)
3510{
3511	STDDCL;
3512
3513	if (debug)
3514		log(LOG_DEBUG, "%s%d: pap tld\n", ifp->if_name, ifp->if_unit);
3515	untimeout(pap.TO, (void *)sp, sp->ch[IDX_PAP]);
3516	untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3517	sp->lcp.protos &= ~(1 << IDX_PAP);
3518
3519	lcp.Close(sp);
3520}
3521
3522static void
3523sppp_pap_scr(struct sppp *sp)
3524{
3525	STDDCL;
3526	u_char idlen, pwdlen;
3527
3528	sp->confid[IDX_PAP] = ++sp->pp_seq;
3529	pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
3530	idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
3531
3532	sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
3533		       sizeof idlen, (const char *)&idlen,
3534		       (unsigned)idlen, sp->myauth.name,
3535		       sizeof pwdlen, (const char *)&pwdlen,
3536		       (unsigned)pwdlen, sp->myauth.secret,
3537		       0);
3538}
3539/*
3540 * Random miscellaneous functions.
3541 */
3542
3543/*
3544 * Send a PAP or CHAP proto packet.
3545 *
3546 * Varadic function, each of the elements for the ellipsis is of type
3547 * ``unsigned mlen, const u_char *msg''.  Processing will stop iff
3548 * mlen == 0.
3549 */
3550
3551static void
3552sppp_auth_send(const struct cp *cp, struct sppp *sp, u_char type, u_char id,
3553	       ...)
3554{
3555	STDDCL;
3556	struct ppp_header *h;
3557	struct lcp_header *lh;
3558	struct mbuf *m;
3559	u_char *p;
3560	int len;
3561	unsigned mlen;
3562	const char *msg;
3563	va_list ap;
3564
3565	MGETHDR (m, M_DONTWAIT, MT_DATA);
3566	if (! m)
3567		return;
3568	m->m_pkthdr.rcvif = 0;
3569
3570	h = mtod (m, struct ppp_header*);
3571	h->address = PPP_ALLSTATIONS;		/* broadcast address */
3572	h->control = PPP_UI;			/* Unnumbered Info */
3573	h->protocol = htons(cp->proto);
3574
3575	lh = (struct lcp_header*)(h + 1);
3576	lh->type = type;
3577	lh->ident = id;
3578	p = (u_char*) (lh+1);
3579
3580	va_start(ap, id);
3581	len = 0;
3582
3583	while ((mlen = va_arg(ap, unsigned)) != 0) {
3584		msg = va_arg(ap, const char *);
3585		len += mlen;
3586		if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
3587			va_end(ap);
3588			m_freem(m);
3589			return;
3590		}
3591
3592		bcopy(msg, p, mlen);
3593		p += mlen;
3594	}
3595	va_end(ap);
3596
3597	m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
3598	lh->len = htons (LCP_HEADER_LEN + len);
3599
3600	if (debug) {
3601		log(LOG_DEBUG, "%s%d: %s output <%s id=0x%x len=%d",
3602		    ifp->if_name, ifp->if_unit, cp->name,
3603		    sppp_auth_type_name(cp->proto, lh->type),
3604		    lh->ident, ntohs(lh->len));
3605		if (len)
3606			sppp_print_bytes((u_char*) (lh+1), len);
3607		addlog(">\n");
3608	}
3609	if (IF_QFULL (&sp->pp_cpq)) {
3610		IF_DROP (&sp->pp_fastq);
3611		IF_DROP (&ifp->if_snd);
3612		m_freem (m);
3613		++ifp->if_oerrors;
3614	} else
3615		IF_ENQUEUE (&sp->pp_cpq, m);
3616	if (! (ifp->if_flags & IFF_OACTIVE))
3617		(*ifp->if_start) (ifp);
3618	ifp->if_obytes += m->m_pkthdr.len + 3;
3619}
3620
3621/*
3622 * Flush interface queue.
3623 */
3624static void
3625sppp_qflush(struct ifqueue *ifq)
3626{
3627	struct mbuf *m, *n;
3628
3629	n = ifq->ifq_head;
3630	while ((m = n)) {
3631		n = m->m_act;
3632		m_freem (m);
3633	}
3634	ifq->ifq_head = 0;
3635	ifq->ifq_tail = 0;
3636	ifq->ifq_len = 0;
3637}
3638
3639/*
3640 * Send keepalive packets, every 10 seconds.
3641 */
3642static void
3643sppp_keepalive(void *dummy)
3644{
3645	struct sppp *sp;
3646	int s;
3647
3648	s = splimp();
3649	for (sp=spppq; sp; sp=sp->pp_next) {
3650		struct ifnet *ifp = &sp->pp_if;
3651
3652		/* Keepalive mode disabled or channel down? */
3653		if (! (sp->pp_flags & PP_KEEPALIVE) ||
3654		    ! (ifp->if_flags & IFF_RUNNING))
3655			continue;
3656
3657		/* No keepalive in PPP mode if LCP not opened yet. */
3658		if (! (sp->pp_flags & PP_CISCO) &&
3659		    sp->pp_phase < PHASE_AUTHENTICATE)
3660			continue;
3661
3662		if (sp->pp_alivecnt == MAXALIVECNT) {
3663			/* No keepalive packets got.  Stop the interface. */
3664			printf ("%s%d: down\n", ifp->if_name, ifp->if_unit);
3665			if_down (ifp);
3666			sppp_qflush (&sp->pp_cpq);
3667			if (! (sp->pp_flags & PP_CISCO)) {
3668				/* XXX */
3669				/* Shut down the PPP link. */
3670				lcp.Down(sp);
3671				/* Initiate negotiation. XXX */
3672				lcp.Up(sp);
3673			}
3674		}
3675		if (sp->pp_alivecnt <= MAXALIVECNT)
3676			++sp->pp_alivecnt;
3677		if (sp->pp_flags & PP_CISCO)
3678			sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
3679				sp->pp_rseq);
3680		else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
3681			long nmagic = htonl (sp->lcp.magic);
3682			sp->lcp.echoid = ++sp->pp_seq;
3683			sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
3684				sp->lcp.echoid, 4, &nmagic);
3685		}
3686	}
3687	splx(s);
3688	keepalive_ch = timeout(sppp_keepalive, 0, hz * 10);
3689}
3690
3691/*
3692 * Get both IP addresses.
3693 */
3694static void
3695sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
3696{
3697	struct ifnet *ifp = &sp->pp_if;
3698	struct ifaddr *ifa;
3699	struct sockaddr_in *si, *sm;
3700	u_long ssrc, ddst;
3701
3702	ssrc = ddst = 0L;
3703	/*
3704	 * Pick the first AF_INET address from the list,
3705	 * aliases don't make any sense on a p2p link anyway.
3706	 */
3707	for (ifa = ifp->if_addrhead.tqh_first, si = 0;
3708	     ifa;
3709	     ifa = ifa->ifa_link.tqe_next)
3710		if (ifa->ifa_addr->sa_family == AF_INET) {
3711			si = (struct sockaddr_in *)ifa->ifa_addr;
3712			sm = (struct sockaddr_in *)ifa->ifa_netmask;
3713			if (si)
3714				break;
3715		}
3716	if (ifa) {
3717		if (si && si->sin_addr.s_addr) {
3718			ssrc = si->sin_addr.s_addr;
3719			if (srcmask)
3720				*srcmask = ntohl(sm->sin_addr.s_addr);
3721		}
3722
3723		si = (struct sockaddr_in *)ifa->ifa_dstaddr;
3724		if (si && si->sin_addr.s_addr)
3725			ddst = si->sin_addr.s_addr;
3726	}
3727
3728	if (dst) *dst = ntohl(ddst);
3729	if (src) *src = ntohl(ssrc);
3730}
3731
3732/*
3733 * Set my IP address.  Must be called at splimp.
3734 */
3735static void
3736sppp_set_ip_addr(struct sppp *sp, u_long src)
3737{
3738	struct ifnet *ifp = &sp->pp_if;
3739	struct ifaddr *ifa;
3740	struct sockaddr_in *si;
3741	u_long ssrc, ddst;
3742
3743	/*
3744	 * Pick the first AF_INET address from the list,
3745	 * aliases don't make any sense on a p2p link anyway.
3746	 */
3747	for (ifa = ifp->if_addrhead.tqh_first, si = 0;
3748	     ifa;
3749	     ifa = ifa->ifa_link.tqe_next)
3750		if (ifa->ifa_addr->sa_family == AF_INET) {
3751			si = (struct sockaddr_in *)ifa->ifa_addr;
3752			if (si)
3753				break;
3754		}
3755	if (ifa && si)
3756		si->sin_addr.s_addr = htonl(src);
3757}
3758
3759static int
3760sppp_params(struct sppp *sp, int cmd, void *data)
3761{
3762	int subcmd;
3763	struct ifreq *ifr = (struct ifreq *)data;
3764	struct spppreq spr;
3765
3766	/*
3767	 * ifr->ifr_data is supposed to point to a struct spppreq.
3768	 * Check the cmd word first before attempting to fetch all the
3769	 * data.
3770	 */
3771	if ((subcmd = fuword(ifr->ifr_data)) == -1)
3772		return EFAULT;
3773
3774	if (copyin((caddr_t)ifr->ifr_data, &spr, sizeof spr) != 0)
3775		return EFAULT;
3776
3777	switch (subcmd) {
3778	case SPPPIOGDEFS:
3779		if (cmd != SIOCGIFGENERIC)
3780			return EINVAL;
3781		/*
3782		 * We copy over the entire current state, but clean
3783		 * out some of the stuff we don't wanna pass up.
3784		 * Remember, SIOCGIFGENERIC is unprotected, and can be
3785		 * called by any user.  No need to ever get PAP or
3786		 * CHAP secrets back to userland anyway.
3787		 */
3788		bcopy(sp, &spr.defs, sizeof(struct sppp));
3789		bzero(spr.defs.myauth.secret, AUTHKEYLEN);
3790		bzero(spr.defs.myauth.challenge, AUTHKEYLEN);
3791		bzero(spr.defs.hisauth.secret, AUTHKEYLEN);
3792		bzero(spr.defs.hisauth.challenge, AUTHKEYLEN);
3793		return copyout(&spr, (caddr_t)ifr->ifr_data, sizeof spr);
3794
3795	case SPPPIOSDEFS:
3796		if (cmd != SIOCSIFGENERIC)
3797			return EINVAL;
3798		/*
3799		 * We have a very specific idea of which fields we allow
3800		 * being passed back from userland, so to not clobber our
3801		 * current state.  For one, we only allow setting
3802		 * anything if LCP is in dead phase.  Once the LCP
3803		 * negotiations started, the authentication settings must
3804		 * not be changed again.  (The administrator can force an
3805		 * ifconfig down in order to get LCP back into dead
3806		 * phase.)
3807		 *
3808		 * Also, we only allow for authentication parameters to be
3809		 * specified.
3810		 *
3811		 * XXX Should allow to set or clear pp_flags.
3812		 *
3813		 * Finally, if the respective authentication protocol to
3814		 * be used is set differently than 0, but the secret is
3815		 * passed as all zeros, we don't trash the existing secret.
3816		 * This allows an administrator to change the system name
3817		 * only without clobbering the secret (which he didn't get
3818		 * back in a previous SPPPIOGDEFS call).  However, the
3819		 * secrets are cleared if the authentication protocol is
3820		 * reset to 0.
3821		 */
3822		if (sp->pp_phase != PHASE_DEAD)
3823			return EBUSY;
3824
3825		if ((spr.defs.myauth.proto != 0 && spr.defs.myauth.proto != PPP_PAP &&
3826		     spr.defs.myauth.proto != PPP_CHAP) ||
3827		    (spr.defs.hisauth.proto != 0 && spr.defs.hisauth.proto != PPP_PAP &&
3828		     spr.defs.hisauth.proto != PPP_CHAP))
3829			return EINVAL;
3830
3831		if (spr.defs.myauth.proto == 0)
3832			/* resetting myauth */
3833			bzero(&sp->myauth, sizeof sp->myauth);
3834		else {
3835			/* setting/changing myauth */
3836			sp->myauth.proto = spr.defs.myauth.proto;
3837			bcopy(spr.defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
3838			if (spr.defs.myauth.secret[0] != '\0')
3839				bcopy(spr.defs.myauth.secret, sp->myauth.secret,
3840				      AUTHKEYLEN);
3841		}
3842		if (spr.defs.hisauth.proto == 0)
3843			/* resetting hisauth */
3844			bzero(&sp->hisauth, sizeof sp->hisauth);
3845		else {
3846			/* setting/changing hisauth */
3847			sp->hisauth.proto = spr.defs.hisauth.proto;
3848			sp->hisauth.flags = spr.defs.hisauth.flags;
3849			bcopy(spr.defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
3850			if (spr.defs.hisauth.secret[0] != '\0')
3851				bcopy(spr.defs.hisauth.secret, sp->hisauth.secret,
3852				      AUTHKEYLEN);
3853		}
3854		break;
3855
3856	default:
3857		return EINVAL;
3858	}
3859
3860	return 0;
3861}
3862
3863static void
3864sppp_phase_network(struct sppp *sp)
3865{
3866	struct ifnet *ifp = &sp->pp_if;
3867	int i;
3868	u_long mask;
3869
3870	sp->pp_phase = PHASE_NETWORK;
3871
3872	log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit,
3873	    sppp_phase_name(sp->pp_phase));
3874
3875	/* Notify NCPs now. */
3876	for (i = 0; i < IDX_COUNT; i++)
3877		if ((cps[i])->flags & CP_NCP)
3878			(cps[i])->Open(sp);
3879
3880	/* Send Up events to all NCPs. */
3881	for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
3882		if (sp->lcp.protos & mask && ((cps[i])->flags & CP_NCP))
3883			(cps[i])->Up(sp);
3884
3885	/* if no NCP is starting, all this was in vain, close down */
3886	sppp_lcp_check_and_close(sp);
3887}
3888
3889
3890static const char *
3891sppp_cp_type_name(u_char type)
3892{
3893	static char buf[12];
3894	switch (type) {
3895	case CONF_REQ:   return "conf-req";
3896	case CONF_ACK:   return "conf-ack";
3897	case CONF_NAK:   return "conf-nak";
3898	case CONF_REJ:   return "conf-rej";
3899	case TERM_REQ:   return "term-req";
3900	case TERM_ACK:   return "term-ack";
3901	case CODE_REJ:   return "code-rej";
3902	case PROTO_REJ:  return "proto-rej";
3903	case ECHO_REQ:   return "echo-req";
3904	case ECHO_REPLY: return "echo-reply";
3905	case DISC_REQ:   return "discard-req";
3906	}
3907	sprintf (buf, "0x%x", type);
3908	return buf;
3909}
3910
3911static const char *
3912sppp_auth_type_name(u_short proto, u_char type)
3913{
3914	static char buf[12];
3915	switch (proto) {
3916	case PPP_CHAP:
3917		switch (type) {
3918		case CHAP_CHALLENGE:	return "challenge";
3919		case CHAP_RESPONSE:	return "response";
3920		case CHAP_SUCCESS:	return "success";
3921		case CHAP_FAILURE:	return "failure";
3922		}
3923	case PPP_PAP:
3924		switch (type) {
3925		case PAP_REQ:		return "req";
3926		case PAP_ACK:		return "ack";
3927		case PAP_NAK:		return "nak";
3928		}
3929	}
3930	sprintf (buf, "0x%x", type);
3931	return buf;
3932}
3933
3934static const char *
3935sppp_lcp_opt_name(u_char opt)
3936{
3937	static char buf[12];
3938	switch (opt) {
3939	case LCP_OPT_MRU:		return "mru";
3940	case LCP_OPT_ASYNC_MAP:		return "async-map";
3941	case LCP_OPT_AUTH_PROTO:	return "auth-proto";
3942	case LCP_OPT_QUAL_PROTO:	return "qual-proto";
3943	case LCP_OPT_MAGIC:		return "magic";
3944	case LCP_OPT_PROTO_COMP:	return "proto-comp";
3945	case LCP_OPT_ADDR_COMP:		return "addr-comp";
3946	}
3947	sprintf (buf, "0x%x", opt);
3948	return buf;
3949}
3950
3951static const char *
3952sppp_ipcp_opt_name(u_char opt)
3953{
3954	static char buf[12];
3955	switch (opt) {
3956	case IPCP_OPT_ADDRESSES:	return "addresses";
3957	case IPCP_OPT_COMPRESSION:	return "compression";
3958	case IPCP_OPT_ADDRESS:		return "address";
3959	}
3960	sprintf (buf, "0x%x", opt);
3961	return buf;
3962}
3963
3964static const char *
3965sppp_state_name(int state)
3966{
3967	switch (state) {
3968	case STATE_INITIAL:	return "initial";
3969	case STATE_STARTING:	return "starting";
3970	case STATE_CLOSED:	return "closed";
3971	case STATE_STOPPED:	return "stopped";
3972	case STATE_CLOSING:	return "closing";
3973	case STATE_STOPPING:	return "stopping";
3974	case STATE_REQ_SENT:	return "req-sent";
3975	case STATE_ACK_RCVD:	return "ack-rcvd";
3976	case STATE_ACK_SENT:	return "ack-sent";
3977	case STATE_OPENED:	return "opened";
3978	}
3979	return "illegal";
3980}
3981
3982static const char *
3983sppp_phase_name(enum ppp_phase phase)
3984{
3985	switch (phase) {
3986	case PHASE_DEAD:	return "dead";
3987	case PHASE_ESTABLISH:	return "establish";
3988	case PHASE_TERMINATE:	return "terminate";
3989	case PHASE_AUTHENTICATE: return "authenticate";
3990	case PHASE_NETWORK:	return "network";
3991	}
3992	return "illegal";
3993}
3994
3995static const char *
3996sppp_proto_name(u_short proto)
3997{
3998	static char buf[12];
3999	switch (proto) {
4000	case PPP_LCP:	return "lcp";
4001	case PPP_IPCP:	return "ipcp";
4002	case PPP_PAP:	return "pap";
4003	case PPP_CHAP:	return "chap";
4004	}
4005	sprintf(buf, "0x%x", (unsigned)proto);
4006	return buf;
4007}
4008
4009static void
4010sppp_print_bytes(const u_char *p, u_short len)
4011{
4012	addlog(" %x", *p++);
4013	while (--len > 0)
4014		addlog("-%x", *p++);
4015}
4016
4017static void
4018sppp_print_string(const char *p, u_short len)
4019{
4020	u_char c;
4021
4022	while (len-- > 0) {
4023		c = *p++;
4024		/*
4025		 * Print only ASCII chars directly.  RFC 1994 recommends
4026		 * using only them, but we don't rely on it.  */
4027		if (c < ' ' || c > '~')
4028			addlog("\\x%x", c);
4029		else
4030			addlog("%c", c);
4031	}
4032}
4033
4034static const char *
4035sppp_dotted_quad(u_long addr)
4036{
4037	static char s[16];
4038	sprintf(s, "%d.%d.%d.%d",
4039		(addr >> 24) & 0xff,
4040		(addr >> 16) & 0xff,
4041		(addr >> 8) & 0xff,
4042		addr & 0xff);
4043	return s;
4044}
4045
4046static int
4047sppp_strnlen(u_char *p, int max)
4048{
4049	int len;
4050
4051	for (len = 0; len < max && *p; ++p)
4052		++len;
4053	return len;
4054}
4055
4056/* a dummy, used to drop uninteresting events */
4057static void
4058sppp_null(struct sppp *unused)
4059{
4060	/* do just nothing */
4061}
4062/*
4063 * This file is large.  Tell emacs to highlight it nevertheless.
4064 *
4065 * Local Variables:
4066 * hilit-auto-highlight-maxout: 120000
4067 * End:
4068 */
4069