2 * Synchronous PPP/Cisco link level subroutines.
3 * Keepalive protocol implemented in both Cisco and PPP modes.
5 * Copyright (C) 1994-1996 Cronyx Engineering Ltd.
6 * Author: Serge Vakulenko, <vak@cronyx.ru>
8 * Heavily revamped to conform to RFC 1661.
9 * Copyright (C) 1997, 2001 Joerg Wunsch.
11 * This software is distributed with NO WARRANTIES, not even the implied
12 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
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.
18 * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997
20 * $FreeBSD: src/sys/net/if_spppsubr.c,v 1.59.2.13 2002/07/03 15:44:41 joerg Exp $
23 #include <sys/param.h>
25 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
27 #include "opt_inet6.h"
33 # include "opt_inet.h"
34 # include "opt_inet6.h"
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/sockio.h>
43 #include <sys/socket.h>
44 #include <sys/syslog.h>
45 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
46 #include <sys/random.h>
48 #include <sys/malloc.h>
51 #if defined (__OpenBSD__)
58 #include <net/netisr.h>
59 #include <net/if_types.h>
60 #include <net/route.h>
61 #include <netinet/in.h>
62 #include <netinet/in_systm.h>
63 #include <netinet/ip.h>
64 #include <net/slcompress.h>
66 #if defined (__NetBSD__) || defined (__OpenBSD__)
67 #include <machine/cpu.h> /* XXX for softnet */
70 #include <machine/stdarg.h>
72 #include <netinet/in.h>
73 #include <netinet/in_systm.h>
74 #include <netinet/in_var.h>
77 #include <netinet/ip.h>
78 #include <netinet/tcp.h>
81 #if defined (__FreeBSD__) || defined (__OpenBSD__)
82 # include <netinet/if_ether.h>
84 # include <net/ethertypes.h>
88 #include <netipx/ipx.h>
89 #include <netipx/ipx_if.h>
94 #include <netns/ns_if.h>
97 #include <net/if_sppp.h>
99 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
100 # define UNTIMEOUT(fun, arg, handle) untimeout(fun, arg, handle)
101 # define TIMEOUT(fun, arg1, arg2, handle) handle = timeout(fun, arg1, arg2)
102 # define IOCTL_CMD_T u_long
104 # define UNTIMEOUT(fun, arg, handle) untimeout(fun, arg)
105 # define TIMEOUT(fun, arg1, arg2, handle) timeout(fun, arg1, arg2)
106 # define IOCTL_CMD_T int
109 #define MAXALIVECNT 3 /* max. alive packets */
112 * Interface flags that can be set in an ifconfig command.
114 * Setting link0 will make the link passive, i.e. it will be marked
115 * as being administrative openable, but won't be opened to begin
116 * with. Incoming calls will be answered, or subsequent calls with
117 * -link1 will cause the administrative open of the LCP layer.
119 * Setting link1 will cause the link to auto-dial only as packets
122 * Setting IFF_DEBUG will syslog the option negotiation and state
123 * transitions at level kern.debug. Note: all logs consistently look
126 * <if-name><unit>: <proto-name> <additional info...>
128 * with <if-name><unit> being something like "bppp0", and <proto-name>
129 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
132 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
133 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
134 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
136 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
137 #define PPP_UI 0x03 /* Unnumbered Information */
138 #define PPP_IP 0x0021 /* Internet Protocol */
139 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
140 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
141 #define PPP_IPX 0x002b /* Novell IPX Protocol */
142 #define PPP_VJ_COMP 0x002d /* VJ compressed TCP/IP */
143 #define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */
144 #define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */
145 #define PPP_LCP 0xc021 /* Link Control Protocol */
146 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
147 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
148 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
149 #define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */
151 #define CONF_REQ 1 /* PPP configure request */
152 #define CONF_ACK 2 /* PPP configure acknowledge */
153 #define CONF_NAK 3 /* PPP configure negative ack */
154 #define CONF_REJ 4 /* PPP configure reject */
155 #define TERM_REQ 5 /* PPP terminate request */
156 #define TERM_ACK 6 /* PPP terminate acknowledge */
157 #define CODE_REJ 7 /* PPP code reject */
158 #define PROTO_REJ 8 /* PPP protocol reject */
159 #define ECHO_REQ 9 /* PPP echo request */
160 #define ECHO_REPLY 10 /* PPP echo reply */
161 #define DISC_REQ 11 /* PPP discard request */
163 #define LCP_OPT_MRU 1 /* maximum receive unit */
164 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
165 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
166 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
167 #define LCP_OPT_MAGIC 5 /* magic number */
168 #define LCP_OPT_RESERVED 6 /* reserved */
169 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
170 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
172 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
173 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
174 #define IPCP_OPT_ADDRESS 3 /* local IP address */
176 #define IPV6CP_OPT_IFID 1 /* interface identifier */
177 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
179 #define IPCP_COMP_VJ 0x2d /* Code for VJ compression */
181 #define PAP_REQ 1 /* PAP name/password request */
182 #define PAP_ACK 2 /* PAP acknowledge */
183 #define PAP_NAK 3 /* PAP fail */
185 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
186 #define CHAP_RESPONSE 2 /* CHAP challenge response */
187 #define CHAP_SUCCESS 3 /* CHAP response ok */
188 #define CHAP_FAILURE 4 /* CHAP response failed */
190 #define CHAP_MD5 5 /* hash algorithm - MD5 */
192 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
193 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
194 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
195 #define CISCO_ADDR_REQ 0 /* Cisco address request */
196 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
197 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
199 /* states are named and numbered according to RFC 1661 */
200 #define STATE_INITIAL 0
201 #define STATE_STARTING 1
202 #define STATE_CLOSED 2
203 #define STATE_STOPPED 3
204 #define STATE_CLOSING 4
205 #define STATE_STOPPING 5
206 #define STATE_REQ_SENT 6
207 #define STATE_ACK_RCVD 7
208 #define STATE_ACK_SENT 8
209 #define STATE_OPENED 9
215 } __attribute__((__packed__));
216 #define PPP_HEADER_LEN sizeof (struct ppp_header)
222 } __attribute__((__packed__));
223 #define LCP_HEADER_LEN sizeof (struct lcp_header)
225 struct cisco_packet {
232 } __attribute__((__packed__));
233 #define CISCO_PACKET_LEN sizeof (struct cisco_packet)
236 * We follow the spelling and capitalization of RFC 1661 here, to make
237 * it easier comparing with the standard. Please refer to this RFC in
238 * case you can't make sense out of these abbreviation; it will also
239 * explain the semantics related to the various events and actions.
242 u_short proto; /* PPP control protocol number */
243 u_char protoidx; /* index into state table in struct sppp */
245 #define CP_LCP 0x01 /* this is the LCP */
246 #define CP_AUTH 0x02 /* this is an authentication protocol */
247 #define CP_NCP 0x04 /* this is a NCP */
248 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
249 const char *name; /* name of this control protocol */
251 void (*Up)(struct sppp *sp);
252 void (*Down)(struct sppp *sp);
253 void (*Open)(struct sppp *sp);
254 void (*Close)(struct sppp *sp);
255 void (*TO)(void *sp);
256 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
257 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
258 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
260 void (*tlu)(struct sppp *sp);
261 void (*tld)(struct sppp *sp);
262 void (*tls)(struct sppp *sp);
263 void (*tlf)(struct sppp *sp);
264 void (*scr)(struct sppp *sp);
267 static struct sppp *spppq;
268 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
269 static struct callout_handle keepalive_ch;
272 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
273 #define SPP_FMT "%s%d: "
274 #define SPP_ARGS(ifp) (ifp)->if_name, (ifp)->if_unit
276 #define SPP_FMT "%s: "
277 #define SPP_ARGS(ifp) (ifp)->if_xname
282 * The following disgusting hack gets around the problem that IP TOS
283 * can't be set yet. We want to put "interactive" traffic on a high
284 * priority queue. To decide if traffic is interactive, we check that
285 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
287 * XXX is this really still necessary? - joerg -
289 static u_short interactive_ports[8] = {
293 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
296 /* almost every function needs these */
298 struct ifnet *ifp = &sp->pp_if; \
299 int debug = ifp->if_flags & IFF_DEBUG
301 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
302 struct sockaddr *dst, struct rtentry *rt);
304 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
305 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
307 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
309 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
310 u_char ident, u_short len, void *data);
311 /* static void sppp_cp_timeout(void *arg); */
312 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
314 static void sppp_auth_send(const struct cp *cp,
315 struct sppp *sp, unsigned int type, unsigned int id,
318 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
319 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
320 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
321 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
322 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
324 static void sppp_null(struct sppp *sp);
326 static void sppp_lcp_init(struct sppp *sp);
327 static void sppp_lcp_up(struct sppp *sp);
328 static void sppp_lcp_down(struct sppp *sp);
329 static void sppp_lcp_open(struct sppp *sp);
330 static void sppp_lcp_close(struct sppp *sp);
331 static void sppp_lcp_TO(void *sp);
332 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
333 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
334 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
335 static void sppp_lcp_tlu(struct sppp *sp);
336 static void sppp_lcp_tld(struct sppp *sp);
337 static void sppp_lcp_tls(struct sppp *sp);
338 static void sppp_lcp_tlf(struct sppp *sp);
339 static void sppp_lcp_scr(struct sppp *sp);
340 static void sppp_lcp_check_and_close(struct sppp *sp);
341 static int sppp_ncp_check(struct sppp *sp);
343 static void sppp_ipcp_init(struct sppp *sp);
344 static void sppp_ipcp_up(struct sppp *sp);
345 static void sppp_ipcp_down(struct sppp *sp);
346 static void sppp_ipcp_open(struct sppp *sp);
347 static void sppp_ipcp_close(struct sppp *sp);
348 static void sppp_ipcp_TO(void *sp);
349 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
350 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
351 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
352 static void sppp_ipcp_tlu(struct sppp *sp);
353 static void sppp_ipcp_tld(struct sppp *sp);
354 static void sppp_ipcp_tls(struct sppp *sp);
355 static void sppp_ipcp_tlf(struct sppp *sp);
356 static void sppp_ipcp_scr(struct sppp *sp);
358 static void sppp_ipv6cp_init(struct sppp *sp);
359 static void sppp_ipv6cp_up(struct sppp *sp);
360 static void sppp_ipv6cp_down(struct sppp *sp);
361 static void sppp_ipv6cp_open(struct sppp *sp);
362 static void sppp_ipv6cp_close(struct sppp *sp);
363 static void sppp_ipv6cp_TO(void *sp);
364 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
365 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
366 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
367 static void sppp_ipv6cp_tlu(struct sppp *sp);
368 static void sppp_ipv6cp_tld(struct sppp *sp);
369 static void sppp_ipv6cp_tls(struct sppp *sp);
370 static void sppp_ipv6cp_tlf(struct sppp *sp);
371 static void sppp_ipv6cp_scr(struct sppp *sp);
373 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
374 static void sppp_pap_init(struct sppp *sp);
375 static void sppp_pap_open(struct sppp *sp);
376 static void sppp_pap_close(struct sppp *sp);
377 static void sppp_pap_TO(void *sp);
378 static void sppp_pap_my_TO(void *sp);
379 static void sppp_pap_tlu(struct sppp *sp);
380 static void sppp_pap_tld(struct sppp *sp);
381 static void sppp_pap_scr(struct sppp *sp);
383 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
384 static void sppp_chap_init(struct sppp *sp);
385 static void sppp_chap_open(struct sppp *sp);
386 static void sppp_chap_close(struct sppp *sp);
387 static void sppp_chap_TO(void *sp);
388 static void sppp_chap_tlu(struct sppp *sp);
389 static void sppp_chap_tld(struct sppp *sp);
390 static void sppp_chap_scr(struct sppp *sp);
392 static const char *sppp_auth_type_name(u_short proto, u_char type);
393 static const char *sppp_cp_type_name(u_char type);
394 static const char *sppp_dotted_quad(u_long addr);
395 static const char *sppp_ipcp_opt_name(u_char opt);
397 static const char *sppp_ipv6cp_opt_name(u_char opt);
399 static const char *sppp_lcp_opt_name(u_char opt);
400 static const char *sppp_phase_name(enum ppp_phase phase);
401 static const char *sppp_proto_name(u_short proto);
402 static const char *sppp_state_name(int state);
403 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
404 static int sppp_strnlen(u_char *p, int max);
405 static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst,
407 static void sppp_keepalive(void *dummy);
408 static void sppp_phase_network(struct sppp *sp);
409 static void sppp_print_bytes(const u_char *p, u_short len);
410 static void sppp_print_string(const char *p, u_short len);
411 static void sppp_qflush(struct ifqueue *ifq);
412 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
414 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
415 struct in6_addr *dst, struct in6_addr *srcmask);
416 #ifdef IPV6CP_MYIFID_DYN
417 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
418 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
420 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
423 /* our control protocol descriptors */
424 static const struct cp lcp = {
425 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
426 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
427 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
428 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
432 static const struct cp ipcp = {
433 PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
434 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
435 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
436 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
440 static const struct cp ipv6cp = {
441 PPP_IPV6CP, IDX_IPV6CP,
442 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
448 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
449 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
450 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
454 static const struct cp pap = {
455 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
456 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
457 sppp_pap_TO, 0, 0, 0,
458 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
462 static const struct cp chap = {
463 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
464 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
465 sppp_chap_TO, 0, 0, 0,
466 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
470 static const struct cp *cps[IDX_COUNT] = {
472 &ipcp, /* IDX_IPCP */
473 &ipv6cp, /* IDX_IPV6CP */
475 &chap, /* IDX_CHAP */
479 sppp_modevent(module_t mod, int type, void *unused)
492 static moduledata_t spppmod = {
497 MODULE_VERSION(sppp, 1);
498 DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
501 * Exported functions, comprising our interface to the lower layer.
505 * Process the received packet.
508 sppp_input(struct ifnet *ifp, struct mbuf *m)
510 struct ppp_header *h;
511 struct ifqueue *inq = 0;
513 struct sppp *sp = (struct sppp *)ifp;
515 int hlen, vjlen, do_account = 0;
516 int debug = ifp->if_flags & IFF_DEBUG;
518 if (ifp->if_flags & IFF_UP)
519 /* Count received bytes, add FCS and one flag */
520 ifp->if_ibytes += m->m_pkthdr.len + 3;
522 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
523 /* Too small packet, drop it. */
526 SPP_FMT "input packet is too small, %d bytes\n",
527 SPP_ARGS(ifp), m->m_pkthdr.len);
536 /* Get PPP header. */
537 h = mtod (m, struct ppp_header*);
538 m_adj (m, PPP_HEADER_LEN);
540 switch (h->address) {
541 case PPP_ALLSTATIONS:
542 if (h->control != PPP_UI)
544 if (sp->pp_mode == IFF_CISCO) {
547 SPP_FMT "PPP packet in Cisco mode "
548 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
550 h->address, h->control, ntohs(h->protocol));
553 switch (ntohs (h->protocol)) {
557 SPP_FMT "rejecting protocol "
558 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
560 h->address, h->control, ntohs(h->protocol));
561 if (sp->state[IDX_LCP] == STATE_OPENED)
562 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
563 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
568 sppp_cp_input(&lcp, sp, m);
572 if (sp->pp_phase >= PHASE_AUTHENTICATE)
573 sppp_pap_input(sp, m);
577 if (sp->pp_phase >= PHASE_AUTHENTICATE)
578 sppp_chap_input(sp, m);
583 if (sp->pp_phase == PHASE_NETWORK)
584 sppp_cp_input(&ipcp, sp, m);
588 if (sp->state[IDX_IPCP] == STATE_OPENED) {
589 schednetisr (NETISR_IP);
595 if (sp->state[IDX_IPCP] == STATE_OPENED) {
597 sl_uncompress_tcp_core(mtod(m, u_char *),
601 &iphdr, &hlen)) <= 0) {
604 SPP_FMT "VJ uncompress failed on compressed packet\n",
610 * Trim the VJ header off the packet, and prepend
611 * the uncompressed IP header (which will usually
612 * end up in two chained mbufs since there's not
613 * enough leading space in the existing mbuf).
616 M_PREPEND(m, hlen, M_DONTWAIT);
619 bcopy(iphdr, mtod(m, u_char *), hlen);
621 schednetisr (NETISR_IP);
627 if (sp->state[IDX_IPCP] == STATE_OPENED) {
628 if (sl_uncompress_tcp_core(mtod(m, u_char *),
630 TYPE_UNCOMPRESSED_TCP,
632 &iphdr, &hlen) != 0) {
635 SPP_FMT "VJ uncompress failed on uncompressed packet\n",
639 schednetisr (NETISR_IP);
647 if (sp->pp_phase == PHASE_NETWORK)
648 sppp_cp_input(&ipv6cp, sp, m);
653 if (sp->state[IDX_IPV6CP] == STATE_OPENED) {
654 schednetisr (NETISR_IPV6);
662 /* IPX IPXCP not implemented yet */
663 if (sp->pp_phase == PHASE_NETWORK) {
664 schednetisr (NETISR_IPX);
672 /* XNS IDPCP not implemented yet */
673 if (sp->pp_phase == PHASE_NETWORK) {
674 schednetisr (NETISR_NS);
682 case CISCO_MULTICAST:
684 /* Don't check the control field here (RFC 1547). */
685 if (sp->pp_mode != IFF_CISCO) {
688 SPP_FMT "Cisco packet in PPP mode "
689 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
691 h->address, h->control, ntohs(h->protocol));
694 switch (ntohs (h->protocol)) {
698 case CISCO_KEEPALIVE:
699 sppp_cisco_input ((struct sppp*) ifp, m);
704 schednetisr (NETISR_IP);
711 schednetisr (NETISR_IPV6);
718 schednetisr (NETISR_IPX);
725 schednetisr (NETISR_NS);
732 default: /* Invalid PPP packet. */
736 SPP_FMT "invalid input packet "
737 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
739 h->address, h->control, ntohs(h->protocol));
743 if (! (ifp->if_flags & IFF_UP) || ! inq)
748 if (IF_QFULL (inq)) {
749 /* Queue overflow. */
753 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
761 * Do only account for network packets, not for control
762 * packets. This is used by some subsystems to detect
765 sp->pp_last_recv = time_second;
769 * Enqueue transmit packet.
772 sppp_output(struct ifnet *ifp, struct mbuf *m,
773 struct sockaddr *dst, struct rtentry *rt)
775 struct sppp *sp = (struct sppp*) ifp;
776 struct ppp_header *h;
777 struct ifqueue *ifq = NULL;
779 int ipproto = PPP_IP;
780 int debug = ifp->if_flags & IFF_DEBUG;
784 if ((ifp->if_flags & IFF_UP) == 0 ||
785 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
794 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
799 * Hack to prevent the initialization-time generated
800 * IPv6 multicast packet to erroneously cause a
801 * dialout event in case IPv6 has been
802 * administratively disabled on that interface.
804 if (dst->sa_family == AF_INET6 &&
805 !(sp->confflags & CONF_ENABLE_IPV6))
809 * Interface is not yet running, but auto-dial. Need
810 * to start LCP for it.
812 ifp->if_flags |= IFF_RUNNING;
820 if (dst->sa_family == AF_INET) {
821 /* XXX Check mbuf length here? */
822 struct ip *ip = mtod (m, struct ip*);
823 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
826 * When using dynamic local IP address assignment by using
827 * 0.0.0.0 as a local address, the first TCP session will
828 * not connect because the local TCP checksum is computed
829 * using 0.0.0.0 which will later become our real IP address
830 * so the TCP checksum computed at the remote end will
831 * become invalid. So we
832 * - don't let packets with src ip addr 0 thru
833 * - we flag TCP packets with src ip 0 as an error
836 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
840 if(ip->ip_p == IPPROTO_TCP)
841 return(EADDRNOTAVAIL);
847 * Put low delay, telnet, rlogin and ftp control packets
848 * in front of the queue.
850 if (IF_QFULL (&sp->pp_fastq))
852 else if (ip->ip_tos & IPTOS_LOWDELAY)
854 else if (m->m_len < sizeof *ip + sizeof *tcp)
856 else if (ip->ip_p != IPPROTO_TCP)
858 else if (INTERACTIVE (ntohs (tcp->th_sport)))
860 else if (INTERACTIVE (ntohs (tcp->th_dport)))
864 * Do IP Header compression
866 if (sp->pp_mode != IFF_CISCO && (sp->ipcp.flags & IPCP_VJ) &&
867 ip->ip_p == IPPROTO_TCP)
868 switch (sl_compress_tcp(m, ip, sp->pp_comp,
869 sp->ipcp.compress_cid)) {
870 case TYPE_COMPRESSED_TCP:
871 ipproto = PPP_VJ_COMP;
873 case TYPE_UNCOMPRESSED_TCP:
874 ipproto = PPP_VJ_UCOMP;
888 if (dst->sa_family == AF_INET6) {
889 /* XXX do something tricky here? */
894 * Prepend general data packet PPP header. For now, IP only.
896 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
899 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
906 * May want to check size of packet
907 * (albeit due to the implementation it's always enough)
909 h = mtod (m, struct ppp_header*);
910 if (sp->pp_mode == IFF_CISCO) {
911 h->address = CISCO_UNICAST; /* unicast address */
914 h->address = PPP_ALLSTATIONS; /* broadcast address */
915 h->control = PPP_UI; /* Unnumbered Info */
918 switch (dst->sa_family) {
920 case AF_INET: /* Internet Protocol */
921 if (sp->pp_mode == IFF_CISCO)
922 h->protocol = htons (ETHERTYPE_IP);
925 * Don't choke with an ENETDOWN early. It's
926 * possible that we just started dialing out,
927 * so don't drop the packet immediately. If
928 * we notice that we run out of buffer space
929 * below, we will however remember that we are
930 * not ready to carry IP packets, and return
931 * ENETDOWN, as opposed to ENOBUFS.
933 h->protocol = htons(ipproto);
934 if (sp->state[IDX_IPCP] != STATE_OPENED)
940 case AF_INET6: /* Internet Protocol */
941 if (sp->pp_mode == IFF_CISCO)
942 h->protocol = htons (ETHERTYPE_IPV6);
945 * Don't choke with an ENETDOWN early. It's
946 * possible that we just started dialing out,
947 * so don't drop the packet immediately. If
948 * we notice that we run out of buffer space
949 * below, we will however remember that we are
950 * not ready to carry IP packets, and return
951 * ENETDOWN, as opposed to ENOBUFS.
953 h->protocol = htons(PPP_IPV6);
954 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
960 case AF_NS: /* Xerox NS Protocol */
961 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
962 ETHERTYPE_NS : PPP_XNS);
966 case AF_IPX: /* Novell IPX Protocol */
967 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
968 ETHERTYPE_IPX : PPP_IPX);
975 return (EAFNOSUPPORT);
979 * Queue message on interface, and start output if interface
982 if (IF_QFULL (ifq)) {
983 IF_DROP (&ifp->if_snd);
987 return (rv? rv: ENOBUFS);
990 if (! (ifp->if_flags & IFF_OACTIVE))
991 (*ifp->if_start) (ifp);
994 * Count output packets and bytes.
995 * The packet length includes header, FCS and 1 flag,
996 * according to RFC 1333.
998 ifp->if_obytes += m->m_pkthdr.len + 3;
1001 * Unlike in sppp_input(), we can always bump the timestamp
1002 * here since sppp_output() is only called on behalf of
1003 * network-layer traffic; control-layer traffic is handled
1004 * by sppp_cp_send().
1006 sp->pp_last_sent = time_second;
1011 sppp_attach(struct ifnet *ifp)
1013 struct sppp *sp = (struct sppp*) ifp;
1015 /* Initialize keepalive handler. */
1017 TIMEOUT(sppp_keepalive, 0, hz * 10, keepalive_ch);
1019 /* Insert new entry into the keepalive list. */
1020 sp->pp_next = spppq;
1023 sp->pp_if.if_mtu = PP_MTU;
1024 sp->pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
1025 sp->pp_if.if_type = IFT_PPP;
1026 sp->pp_if.if_output = sppp_output;
1028 sp->pp_flags = PP_KEEPALIVE;
1030 sp->pp_if.if_snd.ifq_maxlen = 32;
1031 sp->pp_fastq.ifq_maxlen = 32;
1032 sp->pp_cpq.ifq_maxlen = 20;
1034 sp->pp_alivecnt = 0;
1035 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
1036 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
1037 sp->pp_phase = PHASE_DEAD;
1039 sp->pp_down = lcp.Down;
1040 sp->pp_last_recv = sp->pp_last_sent = time_second;
1043 sp->confflags |= CONF_ENABLE_VJ;
1046 sp->confflags |= CONF_ENABLE_IPV6;
1048 sp->pp_comp = malloc(sizeof(struct slcompress), M_TEMP, M_WAIT);
1049 sl_compress_init(sp->pp_comp, -1);
1052 sppp_ipv6cp_init(sp);
1058 sppp_detach(struct ifnet *ifp)
1060 struct sppp **q, *p, *sp = (struct sppp*) ifp;
1063 /* Remove the entry from the keepalive list. */
1064 for (q = &spppq; (p = *q); q = &p->pp_next)
1070 /* Stop keepalive handler. */
1072 UNTIMEOUT(sppp_keepalive, 0, keepalive_ch);
1074 for (i = 0; i < IDX_COUNT; i++)
1075 UNTIMEOUT((cps[i])->TO, (void *)sp, sp->ch[i]);
1076 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
1080 * Flush the interface output queue.
1083 sppp_flush(struct ifnet *ifp)
1085 struct sppp *sp = (struct sppp*) ifp;
1087 sppp_qflush (&sp->pp_if.if_snd);
1088 sppp_qflush (&sp->pp_fastq);
1089 sppp_qflush (&sp->pp_cpq);
1093 * Check if the output queue is empty.
1096 sppp_isempty(struct ifnet *ifp)
1098 struct sppp *sp = (struct sppp*) ifp;
1102 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
1103 !sp->pp_if.if_snd.ifq_head;
1109 * Get next packet to send.
1112 sppp_dequeue(struct ifnet *ifp)
1114 struct sppp *sp = (struct sppp*) ifp;
1120 * Process only the control protocol queue until we have at
1121 * least one NCP open.
1123 * Do always serve all three queues in Cisco mode.
1125 IF_DEQUEUE(&sp->pp_cpq, m);
1127 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO)) {
1128 IF_DEQUEUE(&sp->pp_fastq, m);
1130 IF_DEQUEUE (&sp->pp_if.if_snd, m);
1137 * Pick the next packet, do not remove it from the queue.
1140 sppp_pick(struct ifnet *ifp)
1142 struct sppp *sp = (struct sppp*)ifp;
1148 m = sp->pp_cpq.ifq_head;
1150 (sp->pp_phase == PHASE_NETWORK || sp->pp_mode == IFF_CISCO))
1151 if ((m = sp->pp_fastq.ifq_head) == NULL)
1152 m = sp->pp_if.if_snd.ifq_head;
1158 * Process an ioctl request. Called on low priority level.
1161 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1163 struct ifreq *ifr = (struct ifreq*) data;
1164 struct sppp *sp = (struct sppp*) ifp;
1165 int s, rv, going_up, going_down, newmode;
1171 case SIOCSIFDSTADDR:
1175 /* set the interface "up" when assigning an IP address */
1176 ifp->if_flags |= IFF_UP;
1177 /* fall through... */
1180 going_up = ifp->if_flags & IFF_UP &&
1181 (ifp->if_flags & IFF_RUNNING) == 0;
1182 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1183 ifp->if_flags & IFF_RUNNING;
1185 newmode = ifp->if_flags & IFF_PASSIVE;
1187 newmode = ifp->if_flags & IFF_AUTO;
1189 newmode = ifp->if_flags & IFF_CISCO;
1190 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1191 ifp->if_flags |= newmode;
1193 if (newmode != sp->pp_mode) {
1196 going_up = ifp->if_flags & IFF_RUNNING;
1200 if (sp->pp_mode != IFF_CISCO)
1202 else if (sp->pp_tlf)
1205 ifp->if_flags &= ~IFF_RUNNING;
1206 sp->pp_mode = newmode;
1210 if (sp->pp_mode != IFF_CISCO)
1212 sp->pp_mode = newmode;
1213 if (sp->pp_mode == 0) {
1214 ifp->if_flags |= IFF_RUNNING;
1217 if (sp->pp_mode == IFF_CISCO) {
1220 ifp->if_flags |= IFF_RUNNING;
1228 #define ifr_mtu ifr_metric
1231 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1233 ifp->if_mtu = ifr->ifr_mtu;
1238 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1240 ifp->if_mtu = *(short*)data;
1245 ifr->ifr_mtu = ifp->if_mtu;
1250 *(short*)data = ifp->if_mtu;
1257 case SIOCGIFGENERIC:
1258 case SIOCSIFGENERIC:
1259 rv = sppp_params(sp, cmd, data);
1270 * Cisco framing implementation.
1274 * Handle incoming Cisco keepalive protocol packets.
1277 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1280 struct cisco_packet *h;
1283 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1286 SPP_FMT "cisco invalid packet length: %d bytes\n",
1287 SPP_ARGS(ifp), m->m_pkthdr.len);
1290 h = mtod (m, struct cisco_packet*);
1293 SPP_FMT "cisco input: %d bytes "
1294 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1295 SPP_ARGS(ifp), m->m_pkthdr.len,
1296 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1297 (u_int)h->time0, (u_int)h->time1);
1298 switch (ntohl (h->type)) {
1301 addlog(SPP_FMT "cisco unknown packet type: 0x%lx\n",
1302 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1304 case CISCO_ADDR_REPLY:
1305 /* Reply on address request, ignore */
1307 case CISCO_KEEPALIVE_REQ:
1308 sp->pp_alivecnt = 0;
1309 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1310 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1311 /* Local and remote sequence numbers are equal.
1312 * Probably, the line is in loopback mode. */
1313 if (sp->pp_loopcnt >= MAXALIVECNT) {
1314 printf (SPP_FMT "loopback\n",
1317 if (ifp->if_flags & IFF_UP) {
1319 sppp_qflush (&sp->pp_cpq);
1324 /* Generate new local sequence number */
1325 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1326 sp->pp_seq[IDX_LCP] = random();
1328 sp->pp_seq[IDX_LCP] ^= time.tv_sec ^ time.tv_usec;
1333 if (! (ifp->if_flags & IFF_UP) &&
1334 (ifp->if_flags & IFF_RUNNING)) {
1336 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1339 case CISCO_ADDR_REQ:
1340 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1342 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1348 * Send Cisco keepalive packet.
1351 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1354 struct ppp_header *h;
1355 struct cisco_packet *ch;
1357 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1360 u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
1363 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1364 getmicrouptime(&tv);
1367 MGETHDR (m, M_DONTWAIT, MT_DATA);
1370 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1371 m->m_pkthdr.rcvif = 0;
1373 h = mtod (m, struct ppp_header*);
1374 h->address = CISCO_MULTICAST;
1376 h->protocol = htons (CISCO_KEEPALIVE);
1378 ch = (struct cisco_packet*) (h + 1);
1379 ch->type = htonl (type);
1380 ch->par1 = htonl (par1);
1381 ch->par2 = htonl (par2);
1384 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1385 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1386 ch->time1 = htons ((u_short) tv.tv_sec);
1388 ch->time0 = htons ((u_short) (t >> 16));
1389 ch->time1 = htons ((u_short) t);
1394 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1395 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1396 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1398 if (IF_QFULL (&sp->pp_cpq)) {
1399 IF_DROP (&sp->pp_fastq);
1400 IF_DROP (&ifp->if_snd);
1403 IF_ENQUEUE (&sp->pp_cpq, m);
1404 if (! (ifp->if_flags & IFF_OACTIVE))
1405 (*ifp->if_start) (ifp);
1406 ifp->if_obytes += m->m_pkthdr.len + 3;
1410 * PPP protocol implementation.
1414 * Send PPP control protocol packet.
1417 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1418 u_char ident, u_short len, void *data)
1421 struct ppp_header *h;
1422 struct lcp_header *lh;
1425 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1426 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1427 MGETHDR (m, M_DONTWAIT, MT_DATA);
1430 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1431 m->m_pkthdr.rcvif = 0;
1433 h = mtod (m, struct ppp_header*);
1434 h->address = PPP_ALLSTATIONS; /* broadcast address */
1435 h->control = PPP_UI; /* Unnumbered Info */
1436 h->protocol = htons (proto); /* Link Control Protocol */
1438 lh = (struct lcp_header*) (h + 1);
1441 lh->len = htons (LCP_HEADER_LEN + len);
1443 bcopy (data, lh+1, len);
1446 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1448 sppp_proto_name(proto),
1449 sppp_cp_type_name (lh->type), lh->ident,
1451 sppp_print_bytes ((u_char*) (lh+1), len);
1454 if (IF_QFULL (&sp->pp_cpq)) {
1455 IF_DROP (&sp->pp_fastq);
1456 IF_DROP (&ifp->if_snd);
1460 IF_ENQUEUE (&sp->pp_cpq, m);
1461 if (! (ifp->if_flags & IFF_OACTIVE))
1462 (*ifp->if_start) (ifp);
1463 ifp->if_obytes += m->m_pkthdr.len + 3;
1467 * Handle incoming PPP control protocol packets.
1470 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1473 struct lcp_header *h;
1474 int len = m->m_pkthdr.len;
1481 SPP_FMT "%s invalid packet length: %d bytes\n",
1482 SPP_ARGS(ifp), cp->name, len);
1485 h = mtod (m, struct lcp_header*);
1488 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1489 SPP_ARGS(ifp), cp->name,
1490 sppp_state_name(sp->state[cp->protoidx]),
1491 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1492 sppp_print_bytes ((u_char*) (h+1), len-4);
1495 if (len > ntohs (h->len))
1496 len = ntohs (h->len);
1497 p = (u_char *)(h + 1);
1502 addlog(SPP_FMT "%s invalid conf-req length %d\n",
1503 SPP_ARGS(ifp), cp->name,
1508 /* handle states where RCR doesn't get a SCA/SCN */
1509 switch (sp->state[cp->protoidx]) {
1511 case STATE_STOPPING:
1514 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1518 rv = (cp->RCR)(sp, h, len);
1519 switch (sp->state[cp->protoidx]) {
1523 /* fall through... */
1524 case STATE_ACK_SENT:
1525 case STATE_REQ_SENT:
1527 * sppp_cp_change_state() have the side effect of
1528 * restarting the timeouts. We want to avoid that
1529 * if the state don't change, otherwise we won't
1530 * ever timeout and resend a configuration request
1533 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1536 sppp_cp_change_state(cp, sp, rv?
1537 STATE_ACK_SENT: STATE_REQ_SENT);
1540 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1542 sppp_cp_change_state(cp, sp, rv?
1543 STATE_ACK_SENT: STATE_REQ_SENT);
1545 case STATE_ACK_RCVD:
1547 sppp_cp_change_state(cp, sp, STATE_OPENED);
1549 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1554 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1557 printf(SPP_FMT "%s illegal %s in state %s\n",
1558 SPP_ARGS(ifp), cp->name,
1559 sppp_cp_type_name(h->type),
1560 sppp_state_name(sp->state[cp->protoidx]));
1565 if (h->ident != sp->confid[cp->protoidx]) {
1567 addlog(SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1568 SPP_ARGS(ifp), cp->name,
1569 h->ident, sp->confid[cp->protoidx]);
1573 switch (sp->state[cp->protoidx]) {
1576 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1579 case STATE_STOPPING:
1581 case STATE_REQ_SENT:
1582 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1583 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1588 case STATE_ACK_RCVD:
1590 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1592 case STATE_ACK_SENT:
1593 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1594 sppp_cp_change_state(cp, sp, STATE_OPENED);
1596 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1597 SPP_ARGS(ifp), cp->name);
1601 printf(SPP_FMT "%s illegal %s in state %s\n",
1602 SPP_ARGS(ifp), cp->name,
1603 sppp_cp_type_name(h->type),
1604 sppp_state_name(sp->state[cp->protoidx]));
1610 if (h->ident != sp->confid[cp->protoidx]) {
1612 addlog(SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1613 SPP_ARGS(ifp), cp->name,
1614 h->ident, sp->confid[cp->protoidx]);
1618 if (h->type == CONF_NAK)
1619 (cp->RCN_nak)(sp, h, len);
1621 (cp->RCN_rej)(sp, h, len);
1623 switch (sp->state[cp->protoidx]) {
1626 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1628 case STATE_REQ_SENT:
1629 case STATE_ACK_SENT:
1630 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1632 * Slow things down a bit if we think we might be
1633 * in loopback. Depend on the timeout to send the
1634 * next configuration request.
1643 case STATE_ACK_RCVD:
1644 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1648 case STATE_STOPPING:
1651 printf(SPP_FMT "%s illegal %s in state %s\n",
1652 SPP_ARGS(ifp), cp->name,
1653 sppp_cp_type_name(h->type),
1654 sppp_state_name(sp->state[cp->protoidx]));
1660 switch (sp->state[cp->protoidx]) {
1661 case STATE_ACK_RCVD:
1662 case STATE_ACK_SENT:
1663 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1668 case STATE_STOPPING:
1669 case STATE_REQ_SENT:
1671 /* Send Terminate-Ack packet. */
1673 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1674 SPP_ARGS(ifp), cp->name);
1675 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1679 sp->rst_counter[cp->protoidx] = 0;
1680 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1684 printf(SPP_FMT "%s illegal %s in state %s\n",
1685 SPP_ARGS(ifp), cp->name,
1686 sppp_cp_type_name(h->type),
1687 sppp_state_name(sp->state[cp->protoidx]));
1692 switch (sp->state[cp->protoidx]) {
1695 case STATE_REQ_SENT:
1696 case STATE_ACK_SENT:
1699 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1702 case STATE_STOPPING:
1703 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1706 case STATE_ACK_RCVD:
1707 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1712 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1715 printf(SPP_FMT "%s illegal %s in state %s\n",
1716 SPP_ARGS(ifp), cp->name,
1717 sppp_cp_type_name(h->type),
1718 sppp_state_name(sp->state[cp->protoidx]));
1723 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1725 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1726 "danger will robinson\n",
1727 SPP_ARGS(ifp), cp->name,
1728 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1729 switch (sp->state[cp->protoidx]) {
1732 case STATE_REQ_SENT:
1733 case STATE_ACK_SENT:
1735 case STATE_STOPPING:
1738 case STATE_ACK_RCVD:
1739 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1742 printf(SPP_FMT "%s illegal %s in state %s\n",
1743 SPP_ARGS(ifp), cp->name,
1744 sppp_cp_type_name(h->type),
1745 sppp_state_name(sp->state[cp->protoidx]));
1752 const struct cp *upper;
1758 proto = ntohs(*((u_int16_t *)p));
1759 for (i = 0; i < IDX_COUNT; i++) {
1760 if (cps[i]->proto == proto) {
1768 if (catastrophic || debug)
1769 log(catastrophic? LOG_INFO: LOG_DEBUG,
1770 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1771 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1772 sppp_cp_type_name(h->type), proto,
1773 upper ? upper->name : "unknown",
1774 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1777 * if we got RXJ+ against conf-req, the peer does not implement
1778 * this particular protocol type. terminate the protocol.
1780 if (upper && !catastrophic) {
1781 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1787 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1788 switch (sp->state[cp->protoidx]) {
1791 case STATE_REQ_SENT:
1792 case STATE_ACK_SENT:
1794 case STATE_STOPPING:
1797 case STATE_ACK_RCVD:
1798 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1801 printf(SPP_FMT "%s illegal %s in state %s\n",
1802 SPP_ARGS(ifp), cp->name,
1803 sppp_cp_type_name(h->type),
1804 sppp_state_name(sp->state[cp->protoidx]));
1810 if (cp->proto != PPP_LCP)
1812 /* Discard the packet. */
1815 if (cp->proto != PPP_LCP)
1817 if (sp->state[cp->protoidx] != STATE_OPENED) {
1819 addlog(SPP_FMT "lcp echo req but lcp closed\n",
1826 addlog(SPP_FMT "invalid lcp echo request "
1827 "packet length: %d bytes\n",
1828 SPP_ARGS(ifp), len);
1831 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1832 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1833 /* Line loopback mode detected. */
1834 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1835 sp->pp_loopcnt = MAXALIVECNT * 5;
1837 sppp_qflush (&sp->pp_cpq);
1839 /* Shut down the PPP link. */
1845 *(long*)(h+1) = htonl (sp->lcp.magic);
1847 addlog(SPP_FMT "got lcp echo req, sending echo rep\n",
1849 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1852 if (cp->proto != PPP_LCP)
1854 if (h->ident != sp->lcp.echoid) {
1860 addlog(SPP_FMT "lcp invalid echo reply "
1861 "packet length: %d bytes\n",
1862 SPP_ARGS(ifp), len);
1866 addlog(SPP_FMT "lcp got echo rep\n",
1868 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1869 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1870 sp->pp_alivecnt = 0;
1873 /* Unknown packet type -- send Code-Reject packet. */
1876 addlog(SPP_FMT "%s send code-rej for 0x%x\n",
1877 SPP_ARGS(ifp), cp->name, h->type);
1878 sppp_cp_send(sp, cp->proto, CODE_REJ,
1879 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1886 * The generic part of all Up/Down/Open/Close/TO event handlers.
1887 * Basically, the state transition handling in the automaton.
1890 sppp_up_event(const struct cp *cp, struct sppp *sp)
1895 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1896 SPP_ARGS(ifp), cp->name,
1897 sppp_state_name(sp->state[cp->protoidx]));
1899 switch (sp->state[cp->protoidx]) {
1901 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1903 case STATE_STARTING:
1904 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1906 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1909 printf(SPP_FMT "%s illegal up in state %s\n",
1910 SPP_ARGS(ifp), cp->name,
1911 sppp_state_name(sp->state[cp->protoidx]));
1916 sppp_down_event(const struct cp *cp, struct sppp *sp)
1921 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1922 SPP_ARGS(ifp), cp->name,
1923 sppp_state_name(sp->state[cp->protoidx]));
1925 switch (sp->state[cp->protoidx]) {
1928 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1931 sppp_cp_change_state(cp, sp, STATE_STARTING);
1934 case STATE_STOPPING:
1935 case STATE_REQ_SENT:
1936 case STATE_ACK_RCVD:
1937 case STATE_ACK_SENT:
1938 sppp_cp_change_state(cp, sp, STATE_STARTING);
1942 sppp_cp_change_state(cp, sp, STATE_STARTING);
1945 printf(SPP_FMT "%s illegal down in state %s\n",
1946 SPP_ARGS(ifp), cp->name,
1947 sppp_state_name(sp->state[cp->protoidx]));
1953 sppp_open_event(const struct cp *cp, struct sppp *sp)
1958 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1959 SPP_ARGS(ifp), cp->name,
1960 sppp_state_name(sp->state[cp->protoidx]));
1962 switch (sp->state[cp->protoidx]) {
1964 sppp_cp_change_state(cp, sp, STATE_STARTING);
1967 case STATE_STARTING:
1970 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1972 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1976 * Try escaping stopped state. This seems to bite
1977 * people occasionally, in particular for IPCP,
1978 * presumably following previous IPCP negotiation
1979 * aborts. Somehow, we must have missed a Down event
1980 * which would have caused a transition into starting
1981 * state, so as a bandaid we force the Down event now.
1982 * This effectively implements (something like the)
1983 * `restart' option mentioned in the state transition
1984 * table of RFC 1661.
1986 sppp_cp_change_state(cp, sp, STATE_STARTING);
1989 case STATE_STOPPING:
1990 case STATE_REQ_SENT:
1991 case STATE_ACK_RCVD:
1992 case STATE_ACK_SENT:
1996 sppp_cp_change_state(cp, sp, STATE_STOPPING);
2003 sppp_close_event(const struct cp *cp, struct sppp *sp)
2008 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
2009 SPP_ARGS(ifp), cp->name,
2010 sppp_state_name(sp->state[cp->protoidx]));
2012 switch (sp->state[cp->protoidx]) {
2017 case STATE_STARTING:
2018 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2022 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2024 case STATE_STOPPING:
2025 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2030 case STATE_REQ_SENT:
2031 case STATE_ACK_RCVD:
2032 case STATE_ACK_SENT:
2033 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2034 sppp_cp_send(sp, cp->proto, TERM_REQ,
2035 ++sp->pp_seq[cp->protoidx], 0, 0);
2036 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2042 sppp_to_event(const struct cp *cp, struct sppp *sp)
2049 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
2050 SPP_ARGS(ifp), cp->name,
2051 sppp_state_name(sp->state[cp->protoidx]),
2052 sp->rst_counter[cp->protoidx]);
2054 if (--sp->rst_counter[cp->protoidx] < 0)
2056 switch (sp->state[cp->protoidx]) {
2058 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2061 case STATE_STOPPING:
2062 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2065 case STATE_REQ_SENT:
2066 case STATE_ACK_RCVD:
2067 case STATE_ACK_SENT:
2068 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2074 switch (sp->state[cp->protoidx]) {
2076 case STATE_STOPPING:
2077 sppp_cp_send(sp, cp->proto, TERM_REQ,
2078 ++sp->pp_seq[cp->protoidx], 0, 0);
2079 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
2080 sp->ch[cp->protoidx]);
2082 case STATE_REQ_SENT:
2083 case STATE_ACK_RCVD:
2085 /* sppp_cp_change_state() will restart the timer */
2086 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2088 case STATE_ACK_SENT:
2090 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
2091 sp->ch[cp->protoidx]);
2099 * Change the state of a control protocol in the state automaton.
2100 * Takes care of starting/stopping the restart timer.
2103 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2105 sp->state[cp->protoidx] = newstate;
2107 UNTIMEOUT(cp->TO, (void *)sp, sp->ch[cp->protoidx]);
2110 case STATE_STARTING:
2116 case STATE_STOPPING:
2117 case STATE_REQ_SENT:
2118 case STATE_ACK_RCVD:
2119 case STATE_ACK_SENT:
2120 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
2121 sp->ch[cp->protoidx]);
2127 *--------------------------------------------------------------------------*
2129 * The LCP implementation. *
2131 *--------------------------------------------------------------------------*
2134 sppp_lcp_init(struct sppp *sp)
2136 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2138 sp->state[IDX_LCP] = STATE_INITIAL;
2139 sp->fail_counter[IDX_LCP] = 0;
2140 sp->pp_seq[IDX_LCP] = 0;
2141 sp->pp_rseq[IDX_LCP] = 0;
2143 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2145 /* Note that these values are relevant for all control protocols */
2146 sp->lcp.timeout = 3 * hz;
2147 sp->lcp.max_terminate = 2;
2148 sp->lcp.max_configure = 10;
2149 sp->lcp.max_failure = 10;
2150 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2151 callout_handle_init(&sp->ch[IDX_LCP]);
2156 sppp_lcp_up(struct sppp *sp)
2160 sp->pp_alivecnt = 0;
2161 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2164 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2166 * If this interface is passive or dial-on-demand, and we are
2167 * still in Initial state, it means we've got an incoming
2168 * call. Activate the interface.
2170 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2173 SPP_FMT "Up event", SPP_ARGS(ifp));
2174 ifp->if_flags |= IFF_RUNNING;
2175 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2177 addlog("(incoming call)\n");
2178 sp->pp_flags |= PP_CALLIN;
2182 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2183 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2184 ifp->if_flags |= IFF_RUNNING;
2188 sppp_up_event(&lcp, sp);
2192 sppp_lcp_down(struct sppp *sp)
2196 sppp_down_event(&lcp, sp);
2199 * If this is neither a dial-on-demand nor a passive
2200 * interface, simulate an ``ifconfig down'' action, so the
2201 * administrator can force a redial by another ``ifconfig
2202 * up''. XXX For leased line operation, should we immediately
2203 * try to reopen the connection here?
2205 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2207 SPP_FMT "Down event, taking interface down.\n",
2213 SPP_FMT "Down event (carrier loss)\n",
2215 sp->pp_flags &= ~PP_CALLIN;
2216 if (sp->state[IDX_LCP] != STATE_INITIAL)
2218 ifp->if_flags &= ~IFF_RUNNING;
2223 sppp_lcp_open(struct sppp *sp)
2226 * If we are authenticator, negotiate LCP_AUTH
2228 if (sp->hisauth.proto != 0)
2229 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2231 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2232 sp->pp_flags &= ~PP_NEEDAUTH;
2233 sppp_open_event(&lcp, sp);
2237 sppp_lcp_close(struct sppp *sp)
2239 sppp_close_event(&lcp, sp);
2243 sppp_lcp_TO(void *cookie)
2245 sppp_to_event(&lcp, (struct sppp *)cookie);
2249 * Analyze a configure request. Return true if it was agreeable, and
2250 * caused action sca, false if it has been rejected or nak'ed, and
2251 * caused action scn. (The return value is used to make the state
2252 * transition decision in the state automaton.)
2255 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2258 u_char *buf, *r, *p;
2265 buf = r = malloc (len, M_TEMP, M_NOWAIT);
2270 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2273 /* pass 1: check for things that need to be rejected */
2275 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2277 addlog(" %s ", sppp_lcp_opt_name(*p));
2281 if (len >= 6 && p[1] == 6)
2284 addlog("[invalid] ");
2286 case LCP_OPT_ASYNC_MAP:
2287 /* Async control character map. */
2288 if (len >= 6 && p[1] == 6)
2291 addlog("[invalid] ");
2294 /* Maximum receive unit. */
2295 if (len >= 4 && p[1] == 4)
2298 addlog("[invalid] ");
2300 case LCP_OPT_AUTH_PROTO:
2303 addlog("[invalid] ");
2306 authproto = (p[2] << 8) + p[3];
2307 if (authproto == PPP_CHAP && p[1] != 5) {
2309 addlog("[invalid chap len] ");
2312 if (sp->myauth.proto == 0) {
2313 /* we are not configured to do auth */
2315 addlog("[not configured] ");
2319 * Remote want us to authenticate, remember this,
2320 * so we stay in PHASE_AUTHENTICATE after LCP got
2323 sp->pp_flags |= PP_NEEDAUTH;
2326 /* Others not supported. */
2331 /* Add the option to rejected list. */
2338 addlog(" send conf-rej\n");
2339 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2345 * pass 2: check for option values that are unacceptable and
2346 * thus require to be nak'ed.
2349 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2354 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2356 addlog(" %s ", sppp_lcp_opt_name(*p));
2359 /* Magic number -- extract. */
2360 nmagic = (u_long)p[2] << 24 |
2361 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2362 if (nmagic != sp->lcp.magic) {
2365 addlog("0x%lx ", nmagic);
2368 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2369 addlog("[glitch] ");
2372 * We negate our magic here, and NAK it. If
2373 * we see it later in an NAK packet, we
2374 * suggest a new one.
2376 nmagic = ~sp->lcp.magic;
2378 p[2] = nmagic >> 24;
2379 p[3] = nmagic >> 16;
2384 case LCP_OPT_ASYNC_MAP:
2386 * Async control character map -- just ignore it.
2388 * Quote from RFC 1662, chapter 6:
2389 * To enable this functionality, synchronous PPP
2390 * implementations MUST always respond to the
2391 * Async-Control-Character-Map Configuration
2392 * Option with the LCP Configure-Ack. However,
2393 * acceptance of the Configuration Option does
2394 * not imply that the synchronous implementation
2395 * will do any ACCM mapping. Instead, all such
2396 * octet mapping will be performed by the
2397 * asynchronous-to-synchronous converter.
2403 * Maximum receive unit. Always agreeable,
2404 * but ignored by now.
2406 sp->lcp.their_mru = p[2] * 256 + p[3];
2408 addlog("%lu ", sp->lcp.their_mru);
2411 case LCP_OPT_AUTH_PROTO:
2412 authproto = (p[2] << 8) + p[3];
2413 if (sp->myauth.proto != authproto) {
2414 /* not agreed, nak */
2416 addlog("[mine %s != his %s] ",
2417 sppp_proto_name(sp->hisauth.proto),
2418 sppp_proto_name(authproto));
2419 p[2] = sp->myauth.proto >> 8;
2420 p[3] = sp->myauth.proto;
2423 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2425 addlog("[chap not MD5] ");
2431 /* Add the option to nak'ed list. */
2438 * Local and remote magics equal -- loopback?
2440 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2441 if (sp->pp_loopcnt == MAXALIVECNT*5)
2442 printf (SPP_FMT "loopback\n",
2444 if (ifp->if_flags & IFF_UP) {
2446 sppp_qflush(&sp->pp_cpq);
2451 } else if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2453 addlog(" max_failure (%d) exceeded, "
2455 sp->lcp.max_failure);
2456 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2459 addlog(" send conf-nak\n");
2460 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2464 addlog(" send conf-ack\n");
2465 sp->fail_counter[IDX_LCP] = 0;
2467 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2468 h->ident, origlen, h+1);
2476 * Analyze the LCP Configure-Reject option list, and adjust our
2480 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2486 buf = malloc (len, M_TEMP, M_NOWAIT);
2491 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2495 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2497 addlog(" %s ", sppp_lcp_opt_name(*p));
2500 /* Magic number -- can't use it, use 0 */
2501 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2506 * Should not be rejected anyway, since we only
2507 * negotiate a MRU if explicitly requested by
2510 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2512 case LCP_OPT_AUTH_PROTO:
2514 * Peer doesn't want to authenticate himself,
2515 * deny unless this is a dialout call, and
2516 * AUTHFLAG_NOCALLOUT is set.
2518 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2519 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2521 addlog("[don't insist on auth "
2523 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2527 addlog("[access denied]\n");
2539 * Analyze the LCP Configure-NAK option list, and adjust our
2543 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2550 buf = malloc (len, M_TEMP, M_NOWAIT);
2555 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2559 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2561 addlog(" %s ", sppp_lcp_opt_name(*p));
2564 /* Magic number -- renegotiate */
2565 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2566 len >= 6 && p[1] == 6) {
2567 magic = (u_long)p[2] << 24 |
2568 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2570 * If the remote magic is our negated one,
2571 * this looks like a loopback problem.
2572 * Suggest a new magic to make sure.
2574 if (magic == ~sp->lcp.magic) {
2576 addlog("magic glitch ");
2577 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2578 sp->lcp.magic = random();
2580 sp->lcp.magic = time.tv_sec + time.tv_usec;
2583 sp->lcp.magic = magic;
2585 addlog("%lu ", magic);
2591 * Peer wants to advise us to negotiate an MRU.
2592 * Agree on it if it's reasonable, or use
2593 * default otherwise.
2595 if (len >= 4 && p[1] == 4) {
2596 u_int mru = p[2] * 256 + p[3];
2599 if (mru < PP_MTU || mru > PP_MAX_MRU)
2602 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2605 case LCP_OPT_AUTH_PROTO:
2607 * Peer doesn't like our authentication method,
2611 addlog("[access denied]\n");
2623 sppp_lcp_tlu(struct sppp *sp)
2630 if (! (ifp->if_flags & IFF_UP) &&
2631 (ifp->if_flags & IFF_RUNNING)) {
2632 /* Coming out of loopback mode. */
2634 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2637 for (i = 0; i < IDX_COUNT; i++)
2638 if ((cps[i])->flags & CP_QUAL)
2641 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2642 (sp->pp_flags & PP_NEEDAUTH) != 0)
2643 sp->pp_phase = PHASE_AUTHENTICATE;
2645 sp->pp_phase = PHASE_NETWORK;
2648 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2649 sppp_phase_name(sp->pp_phase));
2652 * Open all authentication protocols. This is even required
2653 * if we already proceeded to network phase, since it might be
2654 * that remote wants us to authenticate, so we might have to
2655 * send a PAP request. Undesired authentication protocols
2656 * don't do anything when they get an Open event.
2658 for (i = 0; i < IDX_COUNT; i++)
2659 if ((cps[i])->flags & CP_AUTH)
2662 if (sp->pp_phase == PHASE_NETWORK) {
2663 /* Notify all NCPs. */
2664 for (i = 0; i < IDX_COUNT; i++)
2665 if (((cps[i])->flags & CP_NCP) &&
2668 * Hack to administratively disable IPv6 if
2669 * not desired. Perhaps we should have another
2670 * flag for this, but right now, we can make
2671 * all struct cp's read/only.
2673 (cps[i] != &ipv6cp ||
2674 (sp->confflags & CONF_ENABLE_IPV6)))
2678 /* Send Up events to all started protos. */
2679 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2680 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2683 /* notify low-level driver of state change */
2685 sp->pp_chg(sp, (int)sp->pp_phase);
2687 if (sp->pp_phase == PHASE_NETWORK)
2688 /* if no NCP is starting, close down */
2689 sppp_lcp_check_and_close(sp);
2693 sppp_lcp_tld(struct sppp *sp)
2699 sp->pp_phase = PHASE_TERMINATE;
2702 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2703 sppp_phase_name(sp->pp_phase));
2706 * Take upper layers down. We send the Down event first and
2707 * the Close second to prevent the upper layers from sending
2708 * ``a flurry of terminate-request packets'', as the RFC
2711 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2712 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2714 (cps[i])->Close(sp);
2719 sppp_lcp_tls(struct sppp *sp)
2723 sp->pp_phase = PHASE_ESTABLISH;
2726 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2727 sppp_phase_name(sp->pp_phase));
2729 /* Notify lower layer if desired. */
2737 sppp_lcp_tlf(struct sppp *sp)
2741 sp->pp_phase = PHASE_DEAD;
2743 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2744 sppp_phase_name(sp->pp_phase));
2746 /* Notify lower layer if desired. */
2754 sppp_lcp_scr(struct sppp *sp)
2756 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2760 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2761 if (! sp->lcp.magic)
2762 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2763 sp->lcp.magic = random();
2765 sp->lcp.magic = time.tv_sec + time.tv_usec;
2767 opt[i++] = LCP_OPT_MAGIC;
2769 opt[i++] = sp->lcp.magic >> 24;
2770 opt[i++] = sp->lcp.magic >> 16;
2771 opt[i++] = sp->lcp.magic >> 8;
2772 opt[i++] = sp->lcp.magic;
2775 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2776 opt[i++] = LCP_OPT_MRU;
2778 opt[i++] = sp->lcp.mru >> 8;
2779 opt[i++] = sp->lcp.mru;
2782 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2783 authproto = sp->hisauth.proto;
2784 opt[i++] = LCP_OPT_AUTH_PROTO;
2785 opt[i++] = authproto == PPP_CHAP? 5: 4;
2786 opt[i++] = authproto >> 8;
2787 opt[i++] = authproto;
2788 if (authproto == PPP_CHAP)
2789 opt[i++] = CHAP_MD5;
2792 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2793 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2797 * Check the open NCPs, return true if at least one NCP is open.
2800 sppp_ncp_check(struct sppp *sp)
2804 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2805 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2811 * Re-check the open NCPs and see if we should terminate the link.
2812 * Called by the NCPs during their tlf action handling.
2815 sppp_lcp_check_and_close(struct sppp *sp)
2818 if (sp->pp_phase < PHASE_NETWORK)
2819 /* don't bother, we are already going down */
2822 if (sppp_ncp_check(sp))
2829 *--------------------------------------------------------------------------*
2831 * The IPCP implementation. *
2833 *--------------------------------------------------------------------------*
2837 sppp_ipcp_init(struct sppp *sp)
2841 sp->state[IDX_IPCP] = STATE_INITIAL;
2842 sp->fail_counter[IDX_IPCP] = 0;
2843 sp->pp_seq[IDX_IPCP] = 0;
2844 sp->pp_rseq[IDX_IPCP] = 0;
2845 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2846 callout_handle_init(&sp->ch[IDX_IPCP]);
2851 sppp_ipcp_up(struct sppp *sp)
2853 sppp_up_event(&ipcp, sp);
2857 sppp_ipcp_down(struct sppp *sp)
2859 sppp_down_event(&ipcp, sp);
2863 sppp_ipcp_open(struct sppp *sp)
2866 u_long myaddr, hisaddr;
2868 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2869 IPCP_MYADDR_DYN | IPCP_VJ);
2872 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2874 * If we don't have his address, this probably means our
2875 * interface doesn't want to talk IP at all. (This could
2876 * be the case if somebody wants to speak only IPX, for
2877 * example.) Don't open IPCP in this case.
2879 if (hisaddr == 0L) {
2880 /* XXX this message should go away */
2882 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2888 * I don't have an assigned address, so i need to
2889 * negotiate my address.
2891 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2892 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2894 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2895 if (sp->confflags & CONF_ENABLE_VJ) {
2896 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2897 sp->ipcp.max_state = MAX_STATES - 1;
2898 sp->ipcp.compress_cid = 1;
2900 sppp_open_event(&ipcp, sp);
2904 sppp_ipcp_close(struct sppp *sp)
2906 sppp_close_event(&ipcp, sp);
2907 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2909 * My address was dynamic, clear it again.
2911 sppp_set_ip_addr(sp, 0L);
2915 sppp_ipcp_TO(void *cookie)
2917 sppp_to_event(&ipcp, (struct sppp *)cookie);
2921 * Analyze a configure request. Return true if it was agreeable, and
2922 * caused action sca, false if it has been rejected or nak'ed, and
2923 * caused action scn. (The return value is used to make the state
2924 * transition decision in the state automaton.)
2927 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2929 u_char *buf, *r, *p;
2930 struct ifnet *ifp = &sp->pp_if;
2931 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2932 u_long hisaddr, desiredaddr;
2939 * Make sure to allocate a buf that can at least hold a
2940 * conf-nak with an `address' option. We might need it below.
2942 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2946 /* pass 1: see if we can recognize them */
2948 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2951 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2953 addlog(" %s ", sppp_ipcp_opt_name(*p));
2955 case IPCP_OPT_COMPRESSION:
2956 if (!(sp->confflags & CONF_ENABLE_VJ)) {
2957 /* VJ compression administratively disabled */
2959 addlog("[locally disabled] ");
2963 * In theory, we should only conf-rej an
2964 * option that is shorter than RFC 1618
2965 * requires (i.e. < 4), and should conf-nak
2966 * anything else that is not VJ. However,
2967 * since our algorithm always uses the
2968 * original option to NAK it with new values,
2969 * things would become more complicated. In
2970 * pratice, the only commonly implemented IP
2971 * compression option is VJ anyway, so the
2972 * difference is negligible.
2974 if (len >= 6 && p[1] == 6) {
2976 * correctly formed compression option
2977 * that could be VJ compression
2982 addlog("optlen %d [invalid/unsupported] ",
2985 case IPCP_OPT_ADDRESS:
2986 if (len >= 6 && p[1] == 6) {
2987 /* correctly formed address option */
2991 addlog("[invalid] ");
2994 /* Others not supported. */
2999 /* Add the option to rejected list. */
3006 addlog(" send conf-rej\n");
3007 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
3012 /* pass 2: parse option values */
3013 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
3015 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
3019 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3021 addlog(" %s ", sppp_ipcp_opt_name(*p));
3023 case IPCP_OPT_COMPRESSION:
3024 desiredcomp = p[2] << 8 | p[3];
3025 /* We only support VJ */
3026 if (desiredcomp == IPCP_COMP_VJ) {
3028 addlog("VJ [ack] ");
3029 sp->ipcp.flags |= IPCP_VJ;
3030 sl_compress_init(sp->pp_comp, p[4]);
3031 sp->ipcp.max_state = p[4];
3032 sp->ipcp.compress_cid = p[5];
3036 addlog("compproto %#04x [not supported] ",
3038 p[2] = IPCP_COMP_VJ >> 8;
3039 p[3] = IPCP_COMP_VJ;
3040 p[4] = sp->ipcp.max_state;
3041 p[5] = sp->ipcp.compress_cid;
3043 case IPCP_OPT_ADDRESS:
3044 /* This is the address he wants in his end */
3045 desiredaddr = p[2] << 24 | p[3] << 16 |
3047 if (desiredaddr == hisaddr ||
3048 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3050 * Peer's address is same as our value,
3051 * or we have set it to 0.0.0.* to
3052 * indicate that we do not really care,
3053 * this is agreeable. Gonna conf-ack
3058 sppp_dotted_quad(hisaddr));
3059 /* record that we've seen it already */
3060 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3064 * The address wasn't agreeable. This is either
3065 * he sent us 0.0.0.0, asking to assign him an
3066 * address, or he send us another address not
3067 * matching our value. Either case, we gonna
3068 * conf-nak it with our value.
3069 * XXX: we should "rej" if hisaddr == 0
3072 if (desiredaddr == 0)
3073 addlog("[addr requested] ");
3075 addlog("%s [not agreed] ",
3076 sppp_dotted_quad(desiredaddr));
3079 p[2] = hisaddr >> 24;
3080 p[3] = hisaddr >> 16;
3081 p[4] = hisaddr >> 8;
3085 /* Add the option to nak'ed list. */
3092 * If we are about to conf-ack the request, but haven't seen
3093 * his address so far, gonna conf-nak it instead, with the
3094 * `address' option present and our idea of his address being
3095 * filled in there, to request negotiation of both addresses.
3097 * XXX This can result in an endless req - nak loop if peer
3098 * doesn't want to send us his address. Q: What should we do
3099 * about it? XXX A: implement the max-failure counter.
3101 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3102 buf[0] = IPCP_OPT_ADDRESS;
3104 buf[2] = hisaddr >> 24;
3105 buf[3] = hisaddr >> 16;
3106 buf[4] = hisaddr >> 8;
3110 addlog("still need hisaddr ");
3115 addlog(" send conf-nak\n");
3116 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3119 addlog(" send conf-ack\n");
3120 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3121 h->ident, origlen, h+1);
3129 * Analyze the IPCP Configure-Reject option list, and adjust our
3133 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3136 struct ifnet *ifp = &sp->pp_if;
3137 int debug = ifp->if_flags & IFF_DEBUG;
3140 buf = malloc (len, M_TEMP, M_NOWAIT);
3145 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3149 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3151 addlog(" %s ", sppp_ipcp_opt_name(*p));
3153 case IPCP_OPT_COMPRESSION:
3154 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3156 case IPCP_OPT_ADDRESS:
3158 * Peer doesn't grok address option. This is
3159 * bad. XXX Should we better give up here?
3160 * XXX We could try old "addresses" option...
3162 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3173 * Analyze the IPCP Configure-NAK option list, and adjust our
3177 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3180 struct ifnet *ifp = &sp->pp_if;
3181 int debug = ifp->if_flags & IFF_DEBUG;
3186 buf = malloc (len, M_TEMP, M_NOWAIT);
3191 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3195 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3197 addlog(" %s ", sppp_ipcp_opt_name(*p));
3199 case IPCP_OPT_COMPRESSION:
3200 if (len >= 6 && p[1] == 6) {
3201 desiredcomp = p[2] << 8 | p[3];
3203 addlog("[wantcomp %#04x] ",
3205 if (desiredcomp == IPCP_COMP_VJ) {
3206 sl_compress_init(sp->pp_comp, p[4]);
3207 sp->ipcp.max_state = p[4];
3208 sp->ipcp.compress_cid = p[5];
3213 ~(1 << IPCP_OPT_COMPRESSION);
3216 case IPCP_OPT_ADDRESS:
3218 * Peer doesn't like our local IP address. See
3219 * if we can do something for him. We'll drop
3220 * him our address then.
3222 if (len >= 6 && p[1] == 6) {
3223 wantaddr = p[2] << 24 | p[3] << 16 |
3225 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3227 addlog("[wantaddr %s] ",
3228 sppp_dotted_quad(wantaddr));
3230 * When doing dynamic address assignment,
3231 * we accept his offer. Otherwise, we
3232 * ignore it and thus continue to negotiate
3233 * our already existing value.
3234 * XXX: Bogus, if he said no once, he'll
3235 * just say no again, might as well die.
3237 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3238 sppp_set_ip_addr(sp, wantaddr);
3241 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3254 sppp_ipcp_tlu(struct sppp *sp)
3256 /* we are up - notify isdn daemon */
3262 sppp_ipcp_tld(struct sppp *sp)
3267 sppp_ipcp_tls(struct sppp *sp)
3269 /* indicate to LCP that it must stay alive */
3270 sp->lcp.protos |= (1 << IDX_IPCP);
3274 sppp_ipcp_tlf(struct sppp *sp)
3276 /* we no longer need LCP */
3277 sp->lcp.protos &= ~(1 << IDX_IPCP);
3278 sppp_lcp_check_and_close(sp);
3282 sppp_ipcp_scr(struct sppp *sp)
3284 char opt[6 /* compression */ + 6 /* address */];
3288 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3289 opt[i++] = IPCP_OPT_COMPRESSION;
3291 opt[i++] = IPCP_COMP_VJ >> 8;
3292 opt[i++] = IPCP_COMP_VJ;
3293 opt[i++] = sp->ipcp.max_state;
3294 opt[i++] = sp->ipcp.compress_cid;
3296 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3297 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3298 opt[i++] = IPCP_OPT_ADDRESS;
3300 opt[i++] = ouraddr >> 24;
3301 opt[i++] = ouraddr >> 16;
3302 opt[i++] = ouraddr >> 8;
3306 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3307 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3311 *--------------------------------------------------------------------------*
3313 * The IPv6CP implementation. *
3315 *--------------------------------------------------------------------------*
3320 sppp_ipv6cp_init(struct sppp *sp)
3322 sp->ipv6cp.opts = 0;
3323 sp->ipv6cp.flags = 0;
3324 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3325 sp->fail_counter[IDX_IPV6CP] = 0;
3326 sp->pp_seq[IDX_IPV6CP] = 0;
3327 sp->pp_rseq[IDX_IPV6CP] = 0;
3328 #if defined(__NetBSD__)
3329 callout_init(&sp->ch[IDX_IPV6CP]);
3331 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3332 callout_handle_init(&sp->ch[IDX_IPV6CP]);
3337 sppp_ipv6cp_up(struct sppp *sp)
3339 sppp_up_event(&ipv6cp, sp);
3343 sppp_ipv6cp_down(struct sppp *sp)
3345 sppp_down_event(&ipv6cp, sp);
3349 sppp_ipv6cp_open(struct sppp *sp)
3352 struct in6_addr myaddr, hisaddr;
3354 #ifdef IPV6CP_MYIFID_DYN
3355 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3357 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3360 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3362 * If we don't have our address, this probably means our
3363 * interface doesn't want to talk IPv6 at all. (This could
3364 * be the case if somebody wants to speak only IPX, for
3365 * example.) Don't open IPv6CP in this case.
3367 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3368 /* XXX this message should go away */
3370 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3375 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3376 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3377 sppp_open_event(&ipv6cp, sp);
3381 sppp_ipv6cp_close(struct sppp *sp)
3383 sppp_close_event(&ipv6cp, sp);
3387 sppp_ipv6cp_TO(void *cookie)
3389 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3393 * Analyze a configure request. Return true if it was agreeable, and
3394 * caused action sca, false if it has been rejected or nak'ed, and
3395 * caused action scn. (The return value is used to make the state
3396 * transition decision in the state automaton.)
3399 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3401 u_char *buf, *r, *p;
3402 struct ifnet *ifp = &sp->pp_if;
3403 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3404 struct in6_addr myaddr, desiredaddr, suggestaddr;
3407 int collision, nohisaddr;
3412 * Make sure to allocate a buf that can at least hold a
3413 * conf-nak with an `address' option. We might need it below.
3415 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3419 /* pass 1: see if we can recognize them */
3421 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3425 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3427 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3429 case IPV6CP_OPT_IFID:
3430 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3431 /* correctly formed address option */
3436 addlog(" [invalid]");
3439 case IPV6CP_OPT_COMPRESSION:
3440 if (len >= 4 && p[1] >= 4) {
3441 /* correctly formed compress option */
3445 addlog(" [invalid]");
3449 /* Others not supported. */
3454 /* Add the option to rejected list. */
3461 addlog(" send conf-rej\n");
3462 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3467 /* pass 2: parse option values */
3468 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3470 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3475 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3477 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3480 case IPV6CP_OPT_COMPRESSION:
3483 case IPV6CP_OPT_IFID:
3484 bzero(&desiredaddr, sizeof(desiredaddr));
3485 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3486 collision = (bcmp(&desiredaddr.s6_addr[8],
3487 &myaddr.s6_addr[8], 8) == 0);
3488 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3490 desiredaddr.s6_addr16[0] = htons(0xfe80);
3491 desiredaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3493 if (!collision && !nohisaddr) {
3494 /* no collision, hisaddr known - Conf-Ack */
3499 ip6_sprintf(&desiredaddr),
3500 sppp_cp_type_name(type));
3505 bzero(&suggestaddr, sizeof(&suggestaddr));
3506 if (collision && nohisaddr) {
3507 /* collision, hisaddr unknown - Conf-Rej */
3512 * - no collision, hisaddr unknown, or
3513 * - collision, hisaddr known
3514 * Conf-Nak, suggest hisaddr
3517 sppp_suggest_ip6_addr(sp, &suggestaddr);
3518 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3521 addlog(" %s [%s]", ip6_sprintf(&desiredaddr),
3522 sppp_cp_type_name(type));
3525 /* Add the option to nak'ed list. */
3531 if (rlen == 0 && type == CONF_ACK) {
3533 addlog(" send %s\n", sppp_cp_type_name(type));
3534 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3537 if (type == CONF_ACK)
3538 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3542 addlog(" send %s suggest %s\n",
3543 sppp_cp_type_name(type), ip6_sprintf(&suggestaddr));
3545 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3554 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3558 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3561 struct ifnet *ifp = &sp->pp_if;
3562 int debug = ifp->if_flags & IFF_DEBUG;
3565 buf = malloc (len, M_TEMP, M_NOWAIT);
3570 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3574 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3576 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3578 case IPV6CP_OPT_IFID:
3580 * Peer doesn't grok address option. This is
3581 * bad. XXX Should we better give up here?
3583 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3586 case IPV6CP_OPT_COMPRESS:
3587 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3599 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3603 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3606 struct ifnet *ifp = &sp->pp_if;
3607 int debug = ifp->if_flags & IFF_DEBUG;
3608 struct in6_addr suggestaddr;
3611 buf = malloc (len, M_TEMP, M_NOWAIT);
3616 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3620 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3622 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3624 case IPV6CP_OPT_IFID:
3626 * Peer doesn't like our local ifid. See
3627 * if we can do something for him. We'll drop
3628 * him our address then.
3630 if (len < 10 || p[1] != 10)
3632 bzero(&suggestaddr, sizeof(suggestaddr));
3633 suggestaddr.s6_addr16[0] = htons(0xfe80);
3634 suggestaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3635 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3637 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3639 addlog(" [suggestaddr %s]",
3640 ip6_sprintf(&suggestaddr));
3641 #ifdef IPV6CP_MYIFID_DYN
3643 * When doing dynamic address assignment,
3644 * we accept his offer.
3646 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3647 struct in6_addr lastsuggest;
3649 * If <suggested myaddr from peer> equals to
3650 * <hisaddr we have suggested last time>,
3651 * we have a collision. generate new random
3654 sppp_suggest_ip6_addr(&lastsuggest);
3655 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3658 addlog(" [random]");
3659 sppp_gen_ip6_addr(sp, &suggestaddr);
3661 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3664 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3668 * Since we do not do dynamic address assignment,
3669 * we ignore it and thus continue to negotiate
3670 * our already existing value. This can possibly
3671 * go into infinite request-reject loop.
3673 * This is not likely because we normally use
3674 * ifid based on MAC-address.
3675 * If you have no ethernet card on the node, too bad.
3676 * XXX should we use fail_counter?
3681 case IPV6CP_OPT_COMPRESS:
3683 * Peer wants different compression parameters.
3695 sppp_ipv6cp_tlu(struct sppp *sp)
3697 /* we are up - notify isdn daemon */
3703 sppp_ipv6cp_tld(struct sppp *sp)
3708 sppp_ipv6cp_tls(struct sppp *sp)
3710 /* indicate to LCP that it must stay alive */
3711 sp->lcp.protos |= (1 << IDX_IPV6CP);
3715 sppp_ipv6cp_tlf(struct sppp *sp)
3718 #if 0 /* need #if 0 to close IPv6CP properly */
3719 /* we no longer need LCP */
3720 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3721 sppp_lcp_check_and_close(sp);
3726 sppp_ipv6cp_scr(struct sppp *sp)
3728 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3729 struct in6_addr ouraddr;
3732 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3733 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3734 opt[i++] = IPV6CP_OPT_IFID;
3736 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3741 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3742 opt[i++] = IPV6CP_OPT_COMPRESSION;
3744 opt[i++] = 0; /* TBD */
3745 opt[i++] = 0; /* TBD */
3746 /* variable length data may follow */
3750 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3751 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3754 static void sppp_ipv6cp_init(struct sppp *sp)
3758 static void sppp_ipv6cp_up(struct sppp *sp)
3762 static void sppp_ipv6cp_down(struct sppp *sp)
3767 static void sppp_ipv6cp_open(struct sppp *sp)
3771 static void sppp_ipv6cp_close(struct sppp *sp)
3775 static void sppp_ipv6cp_TO(void *sp)
3779 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3784 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3788 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3792 static void sppp_ipv6cp_tlu(struct sppp *sp)
3796 static void sppp_ipv6cp_tld(struct sppp *sp)
3800 static void sppp_ipv6cp_tls(struct sppp *sp)
3804 static void sppp_ipv6cp_tlf(struct sppp *sp)
3808 static void sppp_ipv6cp_scr(struct sppp *sp)
3814 *--------------------------------------------------------------------------*
3816 * The CHAP implementation. *
3818 *--------------------------------------------------------------------------*
3822 * The authentication protocols don't employ a full-fledged state machine as
3823 * the control protocols do, since they do have Open and Close events, but
3824 * not Up and Down, nor are they explicitly terminated. Also, use of the
3825 * authentication protocols may be different in both directions (this makes
3826 * sense, think of a machine that never accepts incoming calls but only
3827 * calls out, it doesn't require the called party to authenticate itself).
3829 * Our state machine for the local authentication protocol (we are requesting
3830 * the peer to authenticate) looks like:
3833 * +--------------------------------------------+
3835 * +--------+ Close +---------+ RCA+
3836 * | |<----------------------------------| |------+
3837 * +--->| Closed | TO* | Opened | sca |
3838 * | | |-----+ +-------| |<-----+
3839 * | +--------+ irc | | +---------+
3845 * | | +------->+ | |
3847 * | +--------+ V | |
3848 * | | |<----+<--------------------+ |
3854 * +------+ +------------------------------------------+
3855 * scn,tld sca,irc,ict,tlu
3860 * Open: LCP reached authentication phase
3861 * Close: LCP reached terminate phase
3863 * RCA+: received reply (pap-req, chap-response), acceptable
3864 * RCN: received reply (pap-req, chap-response), not acceptable
3865 * TO+: timeout with restart counter >= 0
3866 * TO-: timeout with restart counter < 0
3867 * TO*: reschedule timeout for CHAP
3869 * scr: send request packet (none for PAP, chap-challenge)
3870 * sca: send ack packet (pap-ack, chap-success)
3871 * scn: send nak packet (pap-nak, chap-failure)
3872 * ict: initialize re-challenge timer (CHAP only)
3874 * tlu: this-layer-up, LCP reaches network phase
3875 * tld: this-layer-down, LCP enters terminate phase
3877 * Note that in CHAP mode, after sending a new challenge, while the state
3878 * automaton falls back into Req-Sent state, it doesn't signal a tld
3879 * event to LCP, so LCP remains in network phase. Only after not getting
3880 * any response (or after getting an unacceptable response), CHAP closes,
3881 * causing LCP to enter terminate phase.
3883 * With PAP, there is no initial request that can be sent. The peer is
3884 * expected to send one based on the successful negotiation of PAP as
3885 * the authentication protocol during the LCP option negotiation.
3887 * Incoming authentication protocol requests (remote requests
3888 * authentication, we are peer) don't employ a state machine at all,
3889 * they are simply answered. Some peers [Ascend P50 firmware rev
3890 * 4.50] react allergically when sending IPCP requests while they are
3891 * still in authentication phase (thereby violating the standard that
3892 * demands that these NCP packets are to be discarded), so we keep
3893 * track of the peer demanding us to authenticate, and only proceed to
3894 * phase network once we've seen a positive acknowledge for the
3899 * Handle incoming CHAP packets.
3902 sppp_chap_input(struct sppp *sp, struct mbuf *m)
3905 struct lcp_header *h;
3907 u_char *value, *name, digest[AUTHKEYLEN], dsize;
3908 int value_len, name_len;
3911 len = m->m_pkthdr.len;
3915 SPP_FMT "chap invalid packet length: %d bytes\n",
3916 SPP_ARGS(ifp), len);
3919 h = mtod (m, struct lcp_header*);
3920 if (len > ntohs (h->len))
3921 len = ntohs (h->len);
3924 /* challenge, failure and success are his authproto */
3925 case CHAP_CHALLENGE:
3926 value = 1 + (u_char*)(h+1);
3927 value_len = value[-1];
3928 name = value + value_len;
3929 name_len = len - value_len - 5;
3933 SPP_FMT "chap corrupted challenge "
3934 "<%s id=0x%x len=%d",
3936 sppp_auth_type_name(PPP_CHAP, h->type),
3937 h->ident, ntohs(h->len));
3938 sppp_print_bytes((u_char*) (h+1), len-4);
3946 SPP_FMT "chap input <%s id=0x%x len=%d name=",
3948 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
3950 sppp_print_string((char*) name, name_len);
3951 addlog(" value-size=%d value=", value_len);
3952 sppp_print_bytes(value, value_len);
3956 /* Compute reply value. */
3958 MD5Update(&ctx, &h->ident, 1);
3959 MD5Update(&ctx, sp->myauth.secret,
3960 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
3961 MD5Update(&ctx, value, value_len);
3962 MD5Final(digest, &ctx);
3963 dsize = sizeof digest;
3965 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
3966 sizeof dsize, (const char *)&dsize,
3967 sizeof digest, digest,
3968 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3975 log(LOG_DEBUG, SPP_FMT "chap success",
3979 sppp_print_string((char*)(h + 1), len - 4);
3984 sp->pp_flags &= ~PP_NEEDAUTH;
3985 if (sp->myauth.proto == PPP_CHAP &&
3986 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
3987 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
3989 * We are authenticator for CHAP but didn't
3990 * complete yet. Leave it to tlu to proceed
3997 sppp_phase_network(sp);
4002 log(LOG_INFO, SPP_FMT "chap failure",
4006 sppp_print_string((char*)(h + 1), len - 4);
4010 log(LOG_INFO, SPP_FMT "chap failure\n",
4012 /* await LCP shutdown by authenticator */
4015 /* response is my authproto */
4017 value = 1 + (u_char*)(h+1);
4018 value_len = value[-1];
4019 name = value + value_len;
4020 name_len = len - value_len - 5;
4024 SPP_FMT "chap corrupted response "
4025 "<%s id=0x%x len=%d",
4027 sppp_auth_type_name(PPP_CHAP, h->type),
4028 h->ident, ntohs(h->len));
4029 sppp_print_bytes((u_char*)(h+1), len-4);
4034 if (h->ident != sp->confid[IDX_CHAP]) {
4037 SPP_FMT "chap dropping response for old ID "
4038 "(got %d, expected %d)\n",
4040 h->ident, sp->confid[IDX_CHAP]);
4043 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4044 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4045 log(LOG_INFO, SPP_FMT "chap response, his name ",
4047 sppp_print_string(name, name_len);
4048 addlog(" != expected ");
4049 sppp_print_string(sp->hisauth.name,
4050 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4054 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4055 "<%s id=0x%x len=%d name=",
4057 sppp_state_name(sp->state[IDX_CHAP]),
4058 sppp_auth_type_name(PPP_CHAP, h->type),
4059 h->ident, ntohs (h->len));
4060 sppp_print_string((char*)name, name_len);
4061 addlog(" value-size=%d value=", value_len);
4062 sppp_print_bytes(value, value_len);
4065 if (value_len != AUTHKEYLEN) {
4068 SPP_FMT "chap bad hash value length: "
4069 "%d bytes, should be %d\n",
4070 SPP_ARGS(ifp), value_len,
4076 MD5Update(&ctx, &h->ident, 1);
4077 MD5Update(&ctx, sp->hisauth.secret,
4078 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4079 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4080 MD5Final(digest, &ctx);
4082 #define FAILMSG "Failed..."
4083 #define SUCCMSG "Welcome!"
4085 if (value_len != sizeof digest ||
4086 bcmp(digest, value, value_len) != 0) {
4087 /* action scn, tld */
4088 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4089 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4094 /* action sca, perhaps tlu */
4095 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4096 sp->state[IDX_CHAP] == STATE_OPENED)
4097 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4098 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4100 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4101 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4107 /* Unknown CHAP packet type -- ignore. */
4109 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4110 "<0x%x id=0x%xh len=%d",
4112 sppp_state_name(sp->state[IDX_CHAP]),
4113 h->type, h->ident, ntohs(h->len));
4114 sppp_print_bytes((u_char*)(h+1), len-4);
4123 sppp_chap_init(struct sppp *sp)
4125 /* Chap doesn't have STATE_INITIAL at all. */
4126 sp->state[IDX_CHAP] = STATE_CLOSED;
4127 sp->fail_counter[IDX_CHAP] = 0;
4128 sp->pp_seq[IDX_CHAP] = 0;
4129 sp->pp_rseq[IDX_CHAP] = 0;
4130 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
4131 callout_handle_init(&sp->ch[IDX_CHAP]);
4136 sppp_chap_open(struct sppp *sp)
4138 if (sp->myauth.proto == PPP_CHAP &&
4139 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4140 /* we are authenticator for CHAP, start it */
4142 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4143 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4145 /* nothing to be done if we are peer, await a challenge */
4149 sppp_chap_close(struct sppp *sp)
4151 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4152 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4156 sppp_chap_TO(void *cookie)
4158 struct sppp *sp = (struct sppp *)cookie;
4164 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4166 sppp_state_name(sp->state[IDX_CHAP]),
4167 sp->rst_counter[IDX_CHAP]);
4169 if (--sp->rst_counter[IDX_CHAP] < 0)
4171 switch (sp->state[IDX_CHAP]) {
4172 case STATE_REQ_SENT:
4174 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4178 /* TO+ (or TO*) event */
4179 switch (sp->state[IDX_CHAP]) {
4182 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4184 case STATE_REQ_SENT:
4186 /* sppp_cp_change_state() will restart the timer */
4187 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4195 sppp_chap_tlu(struct sppp *sp)
4201 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4204 * Some broken CHAP implementations (Conware CoNet, firmware
4205 * 4.0.?) don't want to re-authenticate their CHAP once the
4206 * initial challenge-response exchange has taken place.
4207 * Provide for an option to avoid rechallenges.
4209 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4211 * Compute the re-challenge timeout. This will yield
4212 * a number between 300 and 810 seconds.
4214 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
4215 TIMEOUT(chap.TO, (void *)sp, i * hz, sp->ch[IDX_CHAP]);
4220 SPP_FMT "chap %s, ",
4222 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4223 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4224 addlog("next re-challenge in %d seconds\n", i);
4226 addlog("re-challenging supressed\n");
4230 /* indicate to LCP that we need to be closed down */
4231 sp->lcp.protos |= (1 << IDX_CHAP);
4233 if (sp->pp_flags & PP_NEEDAUTH) {
4235 * Remote is authenticator, but his auth proto didn't
4236 * complete yet. Defer the transition to network
4245 * If we are already in phase network, we are done here. This
4246 * is the case if this is a dummy tlu event after a re-challenge.
4248 if (sp->pp_phase != PHASE_NETWORK)
4249 sppp_phase_network(sp);
4253 sppp_chap_tld(struct sppp *sp)
4258 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4259 UNTIMEOUT(chap.TO, (void *)sp, sp->ch[IDX_CHAP]);
4260 sp->lcp.protos &= ~(1 << IDX_CHAP);
4266 sppp_chap_scr(struct sppp *sp)
4271 /* Compute random challenge. */
4272 ch = (u_long *)sp->myauth.challenge;
4273 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
4274 read_random(&seed, sizeof seed);
4279 seed = tv.tv_sec ^ tv.tv_usec;
4282 ch[0] = seed ^ random();
4283 ch[1] = seed ^ random();
4284 ch[2] = seed ^ random();
4285 ch[3] = seed ^ random();
4288 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4290 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4291 sizeof clen, (const char *)&clen,
4292 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4293 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4299 *--------------------------------------------------------------------------*
4301 * The PAP implementation. *
4303 *--------------------------------------------------------------------------*
4306 * For PAP, we need to keep a little state also if we are the peer, not the
4307 * authenticator. This is since we don't get a request to authenticate, but
4308 * have to repeatedly authenticate ourself until we got a response (or the
4309 * retry counter is expired).
4313 * Handle incoming PAP packets. */
4315 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4318 struct lcp_header *h;
4320 u_char *name, *passwd, mlen;
4321 int name_len, passwd_len;
4323 len = m->m_pkthdr.len;
4327 SPP_FMT "pap invalid packet length: %d bytes\n",
4328 SPP_ARGS(ifp), len);
4331 h = mtod (m, struct lcp_header*);
4332 if (len > ntohs (h->len))
4333 len = ntohs (h->len);
4335 /* PAP request is my authproto */
4337 name = 1 + (u_char*)(h+1);
4338 name_len = name[-1];
4339 passwd = name + name_len + 1;
4340 if (name_len > len - 6 ||
4341 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4343 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4344 "<%s id=0x%x len=%d",
4346 sppp_auth_type_name(PPP_PAP, h->type),
4347 h->ident, ntohs(h->len));
4348 sppp_print_bytes((u_char*)(h+1), len-4);
4354 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4355 "<%s id=0x%x len=%d name=",
4357 sppp_state_name(sp->state[IDX_PAP]),
4358 sppp_auth_type_name(PPP_PAP, h->type),
4359 h->ident, ntohs(h->len));
4360 sppp_print_string((char*)name, name_len);
4362 sppp_print_string((char*)passwd, passwd_len);
4365 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4366 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4367 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4368 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4369 /* action scn, tld */
4370 mlen = sizeof(FAILMSG) - 1;
4371 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4372 sizeof mlen, (const char *)&mlen,
4373 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4378 /* action sca, perhaps tlu */
4379 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4380 sp->state[IDX_PAP] == STATE_OPENED) {
4381 mlen = sizeof(SUCCMSG) - 1;
4382 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4383 sizeof mlen, (const char *)&mlen,
4384 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4387 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4388 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4393 /* ack and nak are his authproto */
4395 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
4397 log(LOG_DEBUG, SPP_FMT "pap success",
4399 name_len = *((char *)h);
4400 if (len > 5 && name_len) {
4402 sppp_print_string((char*)(h+1), name_len);
4407 sp->pp_flags &= ~PP_NEEDAUTH;
4408 if (sp->myauth.proto == PPP_PAP &&
4409 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4410 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4412 * We are authenticator for PAP but didn't
4413 * complete yet. Leave it to tlu to proceed
4420 sppp_phase_network(sp);
4424 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
4426 log(LOG_INFO, SPP_FMT "pap failure",
4428 name_len = *((char *)h);
4429 if (len > 5 && name_len) {
4431 sppp_print_string((char*)(h+1), name_len);
4435 log(LOG_INFO, SPP_FMT "pap failure\n",
4437 /* await LCP shutdown by authenticator */
4441 /* Unknown PAP packet type -- ignore. */
4443 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4444 "<0x%x id=0x%x len=%d",
4446 h->type, h->ident, ntohs(h->len));
4447 sppp_print_bytes((u_char*)(h+1), len-4);
4456 sppp_pap_init(struct sppp *sp)
4458 /* PAP doesn't have STATE_INITIAL at all. */
4459 sp->state[IDX_PAP] = STATE_CLOSED;
4460 sp->fail_counter[IDX_PAP] = 0;
4461 sp->pp_seq[IDX_PAP] = 0;
4462 sp->pp_rseq[IDX_PAP] = 0;
4463 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
4464 callout_handle_init(&sp->ch[IDX_PAP]);
4465 callout_handle_init(&sp->pap_my_to_ch);
4470 sppp_pap_open(struct sppp *sp)
4472 if (sp->hisauth.proto == PPP_PAP &&
4473 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4474 /* we are authenticator for PAP, start our timer */
4475 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4476 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4478 if (sp->myauth.proto == PPP_PAP) {
4479 /* we are peer, send a request, and start a timer */
4481 TIMEOUT(sppp_pap_my_TO, (void *)sp, sp->lcp.timeout,
4487 sppp_pap_close(struct sppp *sp)
4489 if (sp->state[IDX_PAP] != STATE_CLOSED)
4490 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4494 * That's the timeout routine if we are authenticator. Since the
4495 * authenticator is basically passive in PAP, we can't do much here.
4498 sppp_pap_TO(void *cookie)
4500 struct sppp *sp = (struct sppp *)cookie;
4506 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4508 sppp_state_name(sp->state[IDX_PAP]),
4509 sp->rst_counter[IDX_PAP]);
4511 if (--sp->rst_counter[IDX_PAP] < 0)
4513 switch (sp->state[IDX_PAP]) {
4514 case STATE_REQ_SENT:
4516 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4520 /* TO+ event, not very much we could do */
4521 switch (sp->state[IDX_PAP]) {
4522 case STATE_REQ_SENT:
4523 /* sppp_cp_change_state() will restart the timer */
4524 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4532 * That's the timeout handler if we are peer. Since the peer is active,
4533 * we need to retransmit our PAP request since it is apparently lost.
4534 * XXX We should impose a max counter.
4537 sppp_pap_my_TO(void *cookie)
4539 struct sppp *sp = (struct sppp *)cookie;
4543 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4550 sppp_pap_tlu(struct sppp *sp)
4555 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4558 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4559 SPP_ARGS(ifp), pap.name);
4562 /* indicate to LCP that we need to be closed down */
4563 sp->lcp.protos |= (1 << IDX_PAP);
4565 if (sp->pp_flags & PP_NEEDAUTH) {
4567 * Remote is authenticator, but his auth proto didn't
4568 * complete yet. Defer the transition to network
4575 sppp_phase_network(sp);
4579 sppp_pap_tld(struct sppp *sp)
4584 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4585 UNTIMEOUT(pap.TO, (void *)sp, sp->ch[IDX_PAP]);
4586 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
4587 sp->lcp.protos &= ~(1 << IDX_PAP);
4593 sppp_pap_scr(struct sppp *sp)
4595 u_char idlen, pwdlen;
4597 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4598 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4599 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4601 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4602 sizeof idlen, (const char *)&idlen,
4603 (size_t)idlen, sp->myauth.name,
4604 sizeof pwdlen, (const char *)&pwdlen,
4605 (size_t)pwdlen, sp->myauth.secret,
4610 * Random miscellaneous functions.
4614 * Send a PAP or CHAP proto packet.
4616 * Varadic function, each of the elements for the ellipsis is of type
4617 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4619 * NOTE: never declare variadic functions with types subject to type
4620 * promotion (i.e. u_char). This is asking for big trouble depending
4621 * on the architecture you are on...
4625 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4626 unsigned int type, unsigned int id,
4630 struct ppp_header *h;
4631 struct lcp_header *lh;
4639 MGETHDR (m, M_DONTWAIT, MT_DATA);
4642 m->m_pkthdr.rcvif = 0;
4644 h = mtod (m, struct ppp_header*);
4645 h->address = PPP_ALLSTATIONS; /* broadcast address */
4646 h->control = PPP_UI; /* Unnumbered Info */
4647 h->protocol = htons(cp->proto);
4649 lh = (struct lcp_header*)(h + 1);
4652 p = (u_char*) (lh+1);
4657 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
4658 msg = va_arg(ap, const char *);
4660 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4666 bcopy(msg, p, mlen);
4671 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4672 lh->len = htons (LCP_HEADER_LEN + len);
4675 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4676 SPP_ARGS(ifp), cp->name,
4677 sppp_auth_type_name(cp->proto, lh->type),
4678 lh->ident, ntohs(lh->len));
4679 sppp_print_bytes((u_char*) (lh+1), len);
4682 if (IF_QFULL (&sp->pp_cpq)) {
4683 IF_DROP (&sp->pp_fastq);
4684 IF_DROP (&ifp->if_snd);
4688 IF_ENQUEUE (&sp->pp_cpq, m);
4689 if (! (ifp->if_flags & IFF_OACTIVE))
4690 (*ifp->if_start) (ifp);
4691 ifp->if_obytes += m->m_pkthdr.len + 3;
4695 * Flush interface queue.
4698 sppp_qflush(struct ifqueue *ifq)
4713 * Send keepalive packets, every 10 seconds.
4716 sppp_keepalive(void *dummy)
4722 for (sp=spppq; sp; sp=sp->pp_next) {
4723 struct ifnet *ifp = &sp->pp_if;
4725 /* Keepalive mode disabled or channel down? */
4726 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4727 ! (ifp->if_flags & IFF_RUNNING))
4730 /* No keepalive in PPP mode if LCP not opened yet. */
4731 if (sp->pp_mode != IFF_CISCO &&
4732 sp->pp_phase < PHASE_AUTHENTICATE)
4735 if (sp->pp_alivecnt == MAXALIVECNT) {
4736 /* No keepalive packets got. Stop the interface. */
4737 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
4739 sppp_qflush (&sp->pp_cpq);
4740 if (sp->pp_mode != IFF_CISCO) {
4742 /* Shut down the PPP link. */
4744 /* Initiate negotiation. XXX */
4748 if (sp->pp_alivecnt <= MAXALIVECNT)
4750 if (sp->pp_mode == IFF_CISCO)
4751 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4752 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4753 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4754 long nmagic = htonl (sp->lcp.magic);
4755 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4756 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4757 sp->lcp.echoid, 4, &nmagic);
4761 TIMEOUT(sppp_keepalive, 0, hz * 10, keepalive_ch);
4765 * Get both IP addresses.
4768 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4770 struct ifnet *ifp = &sp->pp_if;
4772 struct sockaddr_in *si, *sm;
4778 * Pick the first AF_INET address from the list,
4779 * aliases don't make any sense on a p2p link anyway.
4782 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
4783 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4784 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4785 for (ifa = ifp->if_addrlist.tqh_first;
4787 ifa = ifa->ifa_list.tqe_next)
4789 for (ifa = ifp->if_addrlist;
4791 ifa = ifa->ifa_next)
4793 if (ifa->ifa_addr->sa_family == AF_INET) {
4794 si = (struct sockaddr_in *)ifa->ifa_addr;
4795 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4800 if (si && si->sin_addr.s_addr) {
4801 ssrc = si->sin_addr.s_addr;
4803 *srcmask = ntohl(sm->sin_addr.s_addr);
4806 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4807 if (si && si->sin_addr.s_addr)
4808 ddst = si->sin_addr.s_addr;
4811 if (dst) *dst = ntohl(ddst);
4812 if (src) *src = ntohl(ssrc);
4816 * Set my IP address. Must be called at splimp.
4819 sppp_set_ip_addr(struct sppp *sp, u_long src)
4823 struct sockaddr_in *si;
4824 struct in_ifaddr *ia;
4827 * Pick the first AF_INET address from the list,
4828 * aliases don't make any sense on a p2p link anyway.
4831 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
4832 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4833 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4834 for (ifa = ifp->if_addrlist.tqh_first;
4836 ifa = ifa->ifa_list.tqe_next)
4838 for (ifa = ifp->if_addrlist;
4840 ifa = ifa->ifa_next)
4843 if (ifa->ifa_addr->sa_family == AF_INET)
4845 si = (struct sockaddr_in *)ifa->ifa_addr;
4854 #if __NetBSD_Version__ >= 103080000
4855 struct sockaddr_in new_sin = *si;
4857 new_sin.sin_addr.s_addr = htonl(src);
4858 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 1);
4861 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: in_ifinit "
4862 " failed, error=%d\n", SPP_ARGS(ifp), error);
4865 /* delete old route */
4866 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4869 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4870 SPP_ARGS(ifp), error);
4873 /* set new address */
4874 si->sin_addr.s_addr = htonl(src);
4876 LIST_REMOVE(ia, ia_hash);
4877 LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash);
4880 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
4883 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
4884 SPP_ARGS(ifp), error);
4892 * Get both IPv6 addresses.
4895 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4896 struct in6_addr *srcmask)
4898 struct ifnet *ifp = &sp->pp_if;
4900 struct sockaddr_in6 *si, *sm;
4901 struct in6_addr ssrc, ddst;
4904 bzero(&ssrc, sizeof(ssrc));
4905 bzero(&ddst, sizeof(ddst));
4907 * Pick the first link-local AF_INET6 address from the list,
4908 * aliases don't make any sense on a p2p link anyway.
4910 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
4911 for (ifa = ifp->if_addrhead.tqh_first, si = 0;
4913 ifa = ifa->ifa_link.tqe_next)
4914 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4915 for (ifa = ifp->if_addrlist.tqh_first, si = 0;
4917 ifa = ifa->ifa_list.tqe_next)
4919 for (ifa = ifp->if_addrlist, si = 0;
4921 ifa = ifa->ifa_next)
4923 if (ifa->ifa_addr->sa_family == AF_INET6) {
4924 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4925 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4926 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
4930 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
4931 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
4933 bcopy(&sm->sin6_addr, srcmask,
4938 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
4939 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
4940 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
4944 bcopy(&ddst, dst, sizeof(*dst));
4946 bcopy(&ssrc, src, sizeof(*src));
4949 #ifdef IPV6CP_MYIFID_DYN
4951 * Generate random ifid.
4954 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
4960 * Set my IPv6 address. Must be called at splimp.
4963 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
4967 struct sockaddr_in6 *sin6;
4970 * Pick the first link-local AF_INET6 address from the list,
4971 * aliases don't make any sense on a p2p link anyway.
4975 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
4976 for (ifa = ifp->if_addrhead.tqh_first;
4978 ifa = ifa->ifa_link.tqe_next)
4979 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4980 for (ifa = ifp->if_addrlist.tqh_first;
4982 ifa = ifa->ifa_list.tqe_next)
4984 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
4987 if (ifa->ifa_addr->sa_family == AF_INET6)
4989 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
4990 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
4998 struct sockaddr_in6 new_sin6 = *sin6;
5000 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
5001 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5004 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
5005 " failed, error=%d\n", SPP_ARGS(ifp), error);
5012 * Suggest a candidate address to be used by peer.
5015 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5017 struct in6_addr myaddr;
5020 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5022 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5024 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5025 myaddr.s6_addr[14] ^= 0xff;
5026 myaddr.s6_addr[15] ^= 0xff;
5028 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5029 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5032 bcopy(&myaddr, suggest, sizeof(myaddr));
5037 sppp_params(struct sppp *sp, u_long cmd, void *data)
5040 struct ifreq *ifr = (struct ifreq *)data;
5041 struct spppreq *spr;
5044 if ((spr = malloc(sizeof(struct spppreq), M_TEMP, M_NOWAIT)) == 0)
5047 * ifr->ifr_data is supposed to point to a struct spppreq.
5048 * Check the cmd word first before attempting to fetch all the
5051 if ((subcmd = fuword(ifr->ifr_data)) == -1) {
5056 if (copyin((caddr_t)ifr->ifr_data, spr, sizeof(struct spppreq)) != 0) {
5063 if (cmd != SIOCGIFGENERIC) {
5068 * We copy over the entire current state, but clean
5069 * out some of the stuff we don't wanna pass up.
5070 * Remember, SIOCGIFGENERIC is unprotected, and can be
5071 * called by any user. No need to ever get PAP or
5072 * CHAP secrets back to userland anyway.
5074 spr->defs.pp_phase = sp->pp_phase;
5075 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5076 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5077 spr->defs.lcp = sp->lcp;
5078 spr->defs.ipcp = sp->ipcp;
5079 spr->defs.ipv6cp = sp->ipv6cp;
5080 spr->defs.myauth = sp->myauth;
5081 spr->defs.hisauth = sp->hisauth;
5082 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5083 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5084 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5085 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5087 * Fixup the LCP timeout value to milliseconds so
5088 * spppcontrol doesn't need to bother about the value
5089 * of "hz". We do the reverse calculation below when
5092 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5093 rv = copyout(spr, (caddr_t)ifr->ifr_data,
5094 sizeof(struct spppreq));
5098 if (cmd != SIOCSIFGENERIC) {
5103 * We have a very specific idea of which fields we
5104 * allow being passed back from userland, so to not
5105 * clobber our current state. For one, we only allow
5106 * setting anything if LCP is in dead or establish
5107 * phase. Once the authentication negotiations
5108 * started, the authentication settings must not be
5109 * changed again. (The administrator can force an
5110 * ifconfig down in order to get LCP back into dead
5113 * Also, we only allow for authentication parameters to be
5116 * XXX Should allow to set or clear pp_flags.
5118 * Finally, if the respective authentication protocol to
5119 * be used is set differently than 0, but the secret is
5120 * passed as all zeros, we don't trash the existing secret.
5121 * This allows an administrator to change the system name
5122 * only without clobbering the secret (which he didn't get
5123 * back in a previous SPPPIOGDEFS call). However, the
5124 * secrets are cleared if the authentication protocol is
5126 if (sp->pp_phase != PHASE_DEAD &&
5127 sp->pp_phase != PHASE_ESTABLISH) {
5132 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5133 spr->defs.myauth.proto != PPP_CHAP) ||
5134 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5135 spr->defs.hisauth.proto != PPP_CHAP)) {
5140 if (spr->defs.myauth.proto == 0)
5141 /* resetting myauth */
5142 bzero(&sp->myauth, sizeof sp->myauth);
5144 /* setting/changing myauth */
5145 sp->myauth.proto = spr->defs.myauth.proto;
5146 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5147 if (spr->defs.myauth.secret[0] != '\0')
5148 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5151 if (spr->defs.hisauth.proto == 0)
5152 /* resetting hisauth */
5153 bzero(&sp->hisauth, sizeof sp->hisauth);
5155 /* setting/changing hisauth */
5156 sp->hisauth.proto = spr->defs.hisauth.proto;
5157 sp->hisauth.flags = spr->defs.hisauth.flags;
5158 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5159 if (spr->defs.hisauth.secret[0] != '\0')
5160 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5163 /* set LCP restart timer timeout */
5164 if (spr->defs.lcp.timeout != 0)
5165 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5166 /* set VJ enable and IPv6 disable flags */
5168 if (spr->defs.enable_vj)
5169 sp->confflags |= CONF_ENABLE_VJ;
5171 sp->confflags &= ~CONF_ENABLE_VJ;
5174 if (spr->defs.enable_ipv6)
5175 sp->confflags |= CONF_ENABLE_IPV6;
5177 sp->confflags &= ~CONF_ENABLE_IPV6;
5192 sppp_phase_network(struct sppp *sp)
5198 sp->pp_phase = PHASE_NETWORK;
5201 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5202 sppp_phase_name(sp->pp_phase));
5204 /* Notify NCPs now. */
5205 for (i = 0; i < IDX_COUNT; i++)
5206 if ((cps[i])->flags & CP_NCP)
5209 /* Send Up events to all NCPs. */
5210 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5211 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5214 /* if no NCP is starting, all this was in vain, close down */
5215 sppp_lcp_check_and_close(sp);
5220 sppp_cp_type_name(u_char type)
5222 static char buf[12];
5224 case CONF_REQ: return "conf-req";
5225 case CONF_ACK: return "conf-ack";
5226 case CONF_NAK: return "conf-nak";
5227 case CONF_REJ: return "conf-rej";
5228 case TERM_REQ: return "term-req";
5229 case TERM_ACK: return "term-ack";
5230 case CODE_REJ: return "code-rej";
5231 case PROTO_REJ: return "proto-rej";
5232 case ECHO_REQ: return "echo-req";
5233 case ECHO_REPLY: return "echo-reply";
5234 case DISC_REQ: return "discard-req";
5236 snprintf (buf, sizeof(buf), "cp/0x%x", type);
5241 sppp_auth_type_name(u_short proto, u_char type)
5243 static char buf[12];
5247 case CHAP_CHALLENGE: return "challenge";
5248 case CHAP_RESPONSE: return "response";
5249 case CHAP_SUCCESS: return "success";
5250 case CHAP_FAILURE: return "failure";
5254 case PAP_REQ: return "req";
5255 case PAP_ACK: return "ack";
5256 case PAP_NAK: return "nak";
5259 snprintf (buf, sizeof(buf), "auth/0x%x", type);
5264 sppp_lcp_opt_name(u_char opt)
5266 static char buf[12];
5268 case LCP_OPT_MRU: return "mru";
5269 case LCP_OPT_ASYNC_MAP: return "async-map";
5270 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5271 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5272 case LCP_OPT_MAGIC: return "magic";
5273 case LCP_OPT_PROTO_COMP: return "proto-comp";
5274 case LCP_OPT_ADDR_COMP: return "addr-comp";
5276 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5281 sppp_ipcp_opt_name(u_char opt)
5283 static char buf[12];
5285 case IPCP_OPT_ADDRESSES: return "addresses";
5286 case IPCP_OPT_COMPRESSION: return "compression";
5287 case IPCP_OPT_ADDRESS: return "address";
5289 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5295 sppp_ipv6cp_opt_name(u_char opt)
5297 static char buf[12];
5299 case IPV6CP_OPT_IFID: return "ifid";
5300 case IPV6CP_OPT_COMPRESSION: return "compression";
5302 sprintf (buf, "0x%x", opt);
5308 sppp_state_name(int state)
5311 case STATE_INITIAL: return "initial";
5312 case STATE_STARTING: return "starting";
5313 case STATE_CLOSED: return "closed";
5314 case STATE_STOPPED: return "stopped";
5315 case STATE_CLOSING: return "closing";
5316 case STATE_STOPPING: return "stopping";
5317 case STATE_REQ_SENT: return "req-sent";
5318 case STATE_ACK_RCVD: return "ack-rcvd";
5319 case STATE_ACK_SENT: return "ack-sent";
5320 case STATE_OPENED: return "opened";
5326 sppp_phase_name(enum ppp_phase phase)
5329 case PHASE_DEAD: return "dead";
5330 case PHASE_ESTABLISH: return "establish";
5331 case PHASE_TERMINATE: return "terminate";
5332 case PHASE_AUTHENTICATE: return "authenticate";
5333 case PHASE_NETWORK: return "network";
5339 sppp_proto_name(u_short proto)
5341 static char buf[12];
5343 case PPP_LCP: return "lcp";
5344 case PPP_IPCP: return "ipcp";
5345 case PPP_PAP: return "pap";
5346 case PPP_CHAP: return "chap";
5347 case PPP_IPV6CP: return "ipv6cp";
5349 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5354 sppp_print_bytes(const u_char *p, u_short len)
5357 addlog(" %*D", len, p, "-");
5361 sppp_print_string(const char *p, u_short len)
5368 * Print only ASCII chars directly. RFC 1994 recommends
5369 * using only them, but we don't rely on it. */
5370 if (c < ' ' || c > '~')
5378 sppp_dotted_quad(u_long addr)
5381 sprintf(s, "%d.%d.%d.%d",
5382 (int)((addr >> 24) & 0xff),
5383 (int)((addr >> 16) & 0xff),
5384 (int)((addr >> 8) & 0xff),
5385 (int)(addr & 0xff));
5390 sppp_strnlen(u_char *p, int max)
5394 for (len = 0; len < max && *p; ++p)
5399 /* a dummy, used to drop uninteresting events */
5401 sppp_null(struct sppp *unused)
5403 /* do just nothing */