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 $
21 * $DragonFly: src/sys/net/sppp/if_spppsubr.c,v 1.35 2008/06/09 11:24:24 sephe Exp $
24 #include <sys/param.h>
26 #if defined(__DragonFly__)
28 #include "opt_inet6.h"
34 # include "opt_inet.h"
35 # include "opt_inet6.h"
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/sockio.h>
44 #include <sys/socket.h>
45 #include <sys/syslog.h>
46 #if defined(__DragonFly__)
47 #include <sys/random.h>
48 #include <sys/thread2.h>
50 #include <sys/malloc.h>
53 #if defined (__OpenBSD__)
60 #include <net/ifq_var.h>
61 #include <net/netisr.h>
62 #include <net/if_types.h>
63 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <net/slcompress.h>
69 #if defined (__NetBSD__) || defined (__OpenBSD__)
70 #include <machine/cpu.h> /* XXX for softnet */
73 #include <machine/stdarg.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #include <netinet/tcp.h>
84 #if defined (__DragonFly__) || defined (__OpenBSD__)
85 # include <netinet/if_ether.h>
87 # include <net/ethertypes.h>
91 #include <netproto/ipx/ipx.h>
92 #include <netproto/ipx/ipx_if.h>
97 #include <netns/ns_if.h>
102 #define IOCTL_CMD_T u_long
103 #define MAXALIVECNT 3 /* max. alive packets */
106 * Interface flags that can be set in an ifconfig command.
108 * Setting link0 will make the link passive, i.e. it will be marked
109 * as being administrative openable, but won't be opened to begin
110 * with. Incoming calls will be answered, or subsequent calls with
111 * -link1 will cause the administrative open of the LCP layer.
113 * Setting link1 will cause the link to auto-dial only as packets
116 * Setting IFF_DEBUG will syslog the option negotiation and state
117 * transitions at level kern.debug. Note: all logs consistently look
120 * <if-name><unit>: <proto-name> <additional info...>
122 * with <if-name><unit> being something like "bppp0", and <proto-name>
123 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
126 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
127 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
128 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
130 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
131 #define PPP_UI 0x03 /* Unnumbered Information */
132 #define PPP_IP 0x0021 /* Internet Protocol */
133 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
134 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
135 #define PPP_IPX 0x002b /* Novell IPX Protocol */
136 #define PPP_VJ_COMP 0x002d /* VJ compressed TCP/IP */
137 #define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */
138 #define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */
139 #define PPP_LCP 0xc021 /* Link Control Protocol */
140 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
141 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
142 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
143 #define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */
145 #define CONF_REQ 1 /* PPP configure request */
146 #define CONF_ACK 2 /* PPP configure acknowledge */
147 #define CONF_NAK 3 /* PPP configure negative ack */
148 #define CONF_REJ 4 /* PPP configure reject */
149 #define TERM_REQ 5 /* PPP terminate request */
150 #define TERM_ACK 6 /* PPP terminate acknowledge */
151 #define CODE_REJ 7 /* PPP code reject */
152 #define PROTO_REJ 8 /* PPP protocol reject */
153 #define ECHO_REQ 9 /* PPP echo request */
154 #define ECHO_REPLY 10 /* PPP echo reply */
155 #define DISC_REQ 11 /* PPP discard request */
157 #define LCP_OPT_MRU 1 /* maximum receive unit */
158 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
159 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
160 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
161 #define LCP_OPT_MAGIC 5 /* magic number */
162 #define LCP_OPT_RESERVED 6 /* reserved */
163 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
164 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
166 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
167 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
168 #define IPCP_OPT_ADDRESS 3 /* local IP address */
170 #define IPV6CP_OPT_IFID 1 /* interface identifier */
171 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
173 #define IPCP_COMP_VJ 0x2d /* Code for VJ compression */
175 #define PAP_REQ 1 /* PAP name/password request */
176 #define PAP_ACK 2 /* PAP acknowledge */
177 #define PAP_NAK 3 /* PAP fail */
179 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
180 #define CHAP_RESPONSE 2 /* CHAP challenge response */
181 #define CHAP_SUCCESS 3 /* CHAP response ok */
182 #define CHAP_FAILURE 4 /* CHAP response failed */
184 #define CHAP_MD5 5 /* hash algorithm - MD5 */
186 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
187 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
188 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
189 #define CISCO_ADDR_REQ 0 /* Cisco address request */
190 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
191 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
193 /* states are named and numbered according to RFC 1661 */
194 #define STATE_INITIAL 0
195 #define STATE_STARTING 1
196 #define STATE_CLOSED 2
197 #define STATE_STOPPED 3
198 #define STATE_CLOSING 4
199 #define STATE_STOPPING 5
200 #define STATE_REQ_SENT 6
201 #define STATE_ACK_RCVD 7
202 #define STATE_ACK_SENT 8
203 #define STATE_OPENED 9
209 } __attribute__((__packed__));
210 #define PPP_HEADER_LEN sizeof (struct ppp_header)
216 } __attribute__((__packed__));
217 #define LCP_HEADER_LEN sizeof (struct lcp_header)
219 struct cisco_packet {
226 } __attribute__((__packed__));
227 #define CISCO_PACKET_LEN sizeof (struct cisco_packet)
230 * We follow the spelling and capitalization of RFC 1661 here, to make
231 * it easier comparing with the standard. Please refer to this RFC in
232 * case you can't make sense out of these abbreviation; it will also
233 * explain the semantics related to the various events and actions.
236 u_short proto; /* PPP control protocol number */
237 u_char protoidx; /* index into state table in struct sppp */
239 #define CP_LCP 0x01 /* this is the LCP */
240 #define CP_AUTH 0x02 /* this is an authentication protocol */
241 #define CP_NCP 0x04 /* this is a NCP */
242 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
243 const char *name; /* name of this control protocol */
245 void (*Up)(struct sppp *sp);
246 void (*Down)(struct sppp *sp);
247 void (*Open)(struct sppp *sp);
248 void (*Close)(struct sppp *sp);
249 void (*TO)(void *sp);
250 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
251 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
252 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
254 void (*tlu)(struct sppp *sp);
255 void (*tld)(struct sppp *sp);
256 void (*tls)(struct sppp *sp);
257 void (*tlf)(struct sppp *sp);
258 void (*scr)(struct sppp *sp);
261 static struct sppp *spppq;
262 #if defined(__DragonFly__)
263 static struct callout keepalive_timeout;
266 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 && !defined(__DragonFly__)
267 #define SPP_FMT "%s%d: "
268 #define SPP_ARGS(ifp) (ifp)->if_name, (ifp)->if_unit
270 #define SPP_FMT "%s: "
271 #define SPP_ARGS(ifp) (ifp)->if_xname
276 * The following disgusting hack gets around the problem that IP TOS
277 * can't be set yet. We want to put "interactive" traffic on a high
278 * priority queue. To decide if traffic is interactive, we check that
279 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
281 * XXX is this really still necessary? - joerg -
283 static u_short interactive_ports[8] = {
287 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
290 /* almost every function needs these */
292 struct ifnet *ifp = &sp->pp_if; \
293 int debug = ifp->if_flags & IFF_DEBUG
295 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
296 struct sockaddr *dst, struct rtentry *rt);
298 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
299 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
301 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
303 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
304 u_char ident, u_short len, void *data);
305 /* static void sppp_cp_timeout(void *arg); */
306 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
308 static void sppp_auth_send(const struct cp *cp,
309 struct sppp *sp, unsigned int type, unsigned int id,
312 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
313 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
314 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
315 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
316 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
318 static void sppp_null(struct sppp *sp);
320 static void sppp_lcp_init(struct sppp *sp);
321 static void sppp_lcp_up(struct sppp *sp);
322 static void sppp_lcp_down(struct sppp *sp);
323 static void sppp_lcp_open(struct sppp *sp);
324 static void sppp_lcp_close(struct sppp *sp);
325 static void sppp_lcp_TO(void *sp);
326 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
327 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
328 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
329 static void sppp_lcp_tlu(struct sppp *sp);
330 static void sppp_lcp_tld(struct sppp *sp);
331 static void sppp_lcp_tls(struct sppp *sp);
332 static void sppp_lcp_tlf(struct sppp *sp);
333 static void sppp_lcp_scr(struct sppp *sp);
334 static void sppp_lcp_check_and_close(struct sppp *sp);
335 static int sppp_ncp_check(struct sppp *sp);
337 static void sppp_ipcp_init(struct sppp *sp);
338 static void sppp_ipcp_up(struct sppp *sp);
339 static void sppp_ipcp_down(struct sppp *sp);
340 static void sppp_ipcp_open(struct sppp *sp);
341 static void sppp_ipcp_close(struct sppp *sp);
342 static void sppp_ipcp_TO(void *sp);
343 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
344 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
345 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
346 static void sppp_ipcp_tlu(struct sppp *sp);
347 static void sppp_ipcp_tld(struct sppp *sp);
348 static void sppp_ipcp_tls(struct sppp *sp);
349 static void sppp_ipcp_tlf(struct sppp *sp);
350 static void sppp_ipcp_scr(struct sppp *sp);
352 static void sppp_ipv6cp_init(struct sppp *sp);
353 static void sppp_ipv6cp_up(struct sppp *sp);
354 static void sppp_ipv6cp_down(struct sppp *sp);
355 static void sppp_ipv6cp_open(struct sppp *sp);
356 static void sppp_ipv6cp_close(struct sppp *sp);
357 static void sppp_ipv6cp_TO(void *sp);
358 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
359 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
360 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
361 static void sppp_ipv6cp_tlu(struct sppp *sp);
362 static void sppp_ipv6cp_tld(struct sppp *sp);
363 static void sppp_ipv6cp_tls(struct sppp *sp);
364 static void sppp_ipv6cp_tlf(struct sppp *sp);
365 static void sppp_ipv6cp_scr(struct sppp *sp);
367 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
368 static void sppp_pap_init(struct sppp *sp);
369 static void sppp_pap_open(struct sppp *sp);
370 static void sppp_pap_close(struct sppp *sp);
371 static void sppp_pap_TO(void *sp);
372 static void sppp_pap_my_TO(void *sp);
373 static void sppp_pap_tlu(struct sppp *sp);
374 static void sppp_pap_tld(struct sppp *sp);
375 static void sppp_pap_scr(struct sppp *sp);
377 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
378 static void sppp_chap_init(struct sppp *sp);
379 static void sppp_chap_open(struct sppp *sp);
380 static void sppp_chap_close(struct sppp *sp);
381 static void sppp_chap_TO(void *sp);
382 static void sppp_chap_tlu(struct sppp *sp);
383 static void sppp_chap_tld(struct sppp *sp);
384 static void sppp_chap_scr(struct sppp *sp);
386 static const char *sppp_auth_type_name(u_short proto, u_char type);
387 static const char *sppp_cp_type_name(u_char type);
388 static const char *sppp_dotted_quad(u_long addr);
389 static const char *sppp_ipcp_opt_name(u_char opt);
391 static const char *sppp_ipv6cp_opt_name(u_char opt);
393 static const char *sppp_lcp_opt_name(u_char opt);
394 static const char *sppp_phase_name(enum ppp_phase phase);
395 static const char *sppp_proto_name(u_short proto);
396 static const char *sppp_state_name(int state);
397 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
398 static int sppp_strnlen(u_char *p, int max);
399 static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst,
401 static void sppp_keepalive(void *dummy);
402 static void sppp_phase_network(struct sppp *sp);
403 static void sppp_print_bytes(const u_char *p, u_short len);
404 static void sppp_print_string(const char *p, u_short len);
405 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
407 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
408 struct in6_addr *dst, struct in6_addr *srcmask);
409 #ifdef IPV6CP_MYIFID_DYN
410 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
411 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
413 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
416 /* our control protocol descriptors */
417 static const struct cp lcp = {
418 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
419 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
420 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
421 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
425 static const struct cp ipcp = {
426 PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
427 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
428 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
429 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
433 static const struct cp ipv6cp = {
434 PPP_IPV6CP, IDX_IPV6CP,
435 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
441 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
442 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
443 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
447 static const struct cp pap = {
448 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
449 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
450 sppp_pap_TO, 0, 0, 0,
451 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
455 static const struct cp chap = {
456 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
457 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
458 sppp_chap_TO, 0, 0, 0,
459 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
463 static const struct cp *cps[IDX_COUNT] = {
465 &ipcp, /* IDX_IPCP */
466 &ipv6cp, /* IDX_IPV6CP */
468 &chap, /* IDX_CHAP */
472 sppp_modevent(module_t mod, int type, void *unused)
476 callout_init(&keepalive_timeout);
486 static moduledata_t spppmod = {
491 MODULE_VERSION(sppp, 1);
492 DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
495 * Exported functions, comprising our interface to the lower layer.
499 * Process the received packet.
502 sppp_input(struct ifnet *ifp, struct mbuf *m)
504 struct ppp_header *h;
506 struct sppp *sp = (struct sppp *)ifp;
508 int hlen, vjlen, do_account = 0;
509 int debug = ifp->if_flags & IFF_DEBUG;
511 if (ifp->if_flags & IFF_UP)
512 /* Count received bytes, add FCS and one flag */
513 ifp->if_ibytes += m->m_pkthdr.len + 3;
515 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
516 /* Too small packet, drop it. */
519 SPP_FMT "input packet is too small, %d bytes\n",
520 SPP_ARGS(ifp), m->m_pkthdr.len);
529 /* Get PPP header. */
530 h = mtod (m, struct ppp_header*);
531 m_adj (m, PPP_HEADER_LEN);
533 switch (h->address) {
534 case PPP_ALLSTATIONS:
535 if (h->control != PPP_UI)
537 if (sp->pp_mode == IFF_CISCO) {
540 SPP_FMT "PPP packet in Cisco mode "
541 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
543 h->address, h->control, ntohs(h->protocol));
546 switch (ntohs (h->protocol)) {
550 SPP_FMT "rejecting protocol "
551 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
553 h->address, h->control, ntohs(h->protocol));
554 if (sp->state[IDX_LCP] == STATE_OPENED)
555 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
556 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
561 sppp_cp_input(&lcp, sp, m);
565 if (sp->pp_phase >= PHASE_AUTHENTICATE)
566 sppp_pap_input(sp, m);
570 if (sp->pp_phase >= PHASE_AUTHENTICATE)
571 sppp_chap_input(sp, m);
576 if (sp->pp_phase == PHASE_NETWORK)
577 sppp_cp_input(&ipcp, sp, m);
581 if (sp->state[IDX_IPCP] == STATE_OPENED) {
587 if (sp->state[IDX_IPCP] == STATE_OPENED) {
589 sl_uncompress_tcp_core(mtod(m, u_char *),
593 &iphdr, &hlen)) <= 0) {
596 SPP_FMT "VJ uncompress failed on compressed packet\n",
602 * Trim the VJ header off the packet, and prepend
603 * the uncompressed IP header (which will usually
604 * end up in two chained mbufs since there's not
605 * enough leading space in the existing mbuf).
608 M_PREPEND(m, hlen, MB_DONTWAIT);
611 bcopy(iphdr, mtod(m, u_char *), hlen);
618 if (sp->state[IDX_IPCP] == STATE_OPENED) {
619 if (sl_uncompress_tcp_core(mtod(m, u_char *),
621 TYPE_UNCOMPRESSED_TCP,
623 &iphdr, &hlen) != 0) {
626 SPP_FMT "VJ uncompress failed on uncompressed packet\n",
637 if (sp->pp_phase == PHASE_NETWORK)
638 sppp_cp_input(&ipv6cp, sp, m);
643 if (sp->state[IDX_IPV6CP] == STATE_OPENED) {
651 /* IPX IPXCP not implemented yet */
652 if (sp->pp_phase == PHASE_NETWORK) {
660 /* XNS IDPCP not implemented yet */
661 if (sp->pp_phase == PHASE_NETWORK) {
669 case CISCO_MULTICAST:
671 /* Don't check the control field here (RFC 1547). */
672 if (sp->pp_mode != IFF_CISCO) {
675 SPP_FMT "Cisco packet in PPP mode "
676 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
678 h->address, h->control, ntohs(h->protocol));
681 switch (ntohs (h->protocol)) {
685 case CISCO_KEEPALIVE:
686 sppp_cisco_input ((struct sppp*) ifp, m);
715 default: /* Invalid PPP packet. */
719 SPP_FMT "invalid input packet "
720 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
722 h->address, h->control, ntohs(h->protocol));
726 if (! (ifp->if_flags & IFF_UP) || isr < 0)
731 netisr_queue(isr, m);
734 * Do only account for network packets, not for control
735 * packets. This is used by some subsystems to detect
739 sp->pp_last_recv = time_second;
743 * Enqueue transmit packet.
746 sppp_output_serialized(struct ifnet *ifp, struct mbuf *m,
747 struct sockaddr *dst, struct rtentry *rt)
749 struct sppp *sp = (struct sppp*) ifp;
750 struct ppp_header *h;
751 struct ifqueue *ifq = NULL;
753 int ipproto = PPP_IP;
754 int debug = ifp->if_flags & IFF_DEBUG;
755 struct altq_pktattr pktattr;
759 if ((ifp->if_flags & IFF_UP) == 0 ||
760 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
769 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
774 * Hack to prevent the initialization-time generated
775 * IPv6 multicast packet to erroneously cause a
776 * dialout event in case IPv6 has been
777 * administratively disabled on that interface.
779 if (dst->sa_family == AF_INET6 &&
780 !(sp->confflags & CONF_ENABLE_IPV6))
784 * Interface is not yet running, but auto-dial. Need
785 * to start LCP for it.
787 ifp->if_flags |= IFF_RUNNING;
794 * if the queueing discipline needs packet classification,
795 * do it before prepending link headers.
797 ifq_classify(&ifp->if_snd, m, dst->sa_family, &pktattr);
800 if (dst->sa_family == AF_INET) {
801 /* XXX Check mbuf length here? */
802 struct ip *ip = mtod (m, struct ip*);
803 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
806 * When using dynamic local IP address assignment by using
807 * 0.0.0.0 as a local address, the first TCP session will
808 * not connect because the local TCP checksum is computed
809 * using 0.0.0.0 which will later become our real IP address
810 * so the TCP checksum computed at the remote end will
811 * become invalid. So we
812 * - don't let packets with src ip addr 0 thru
813 * - we flag TCP packets with src ip 0 as an error
816 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
820 if(ip->ip_p == IPPROTO_TCP)
821 return(EADDRNOTAVAIL);
827 * Put low delay, telnet, rlogin and ftp control packets
828 * in front of the queue.
830 if (IF_QFULL (&sp->pp_fastq))
832 else if (ip->ip_tos & IPTOS_LOWDELAY)
834 else if (m->m_len < sizeof *ip + sizeof *tcp)
836 else if (ip->ip_p != IPPROTO_TCP)
838 else if (INTERACTIVE (ntohs (tcp->th_sport)))
840 else if (INTERACTIVE (ntohs (tcp->th_dport)))
844 * Do IP Header compression
846 if (sp->pp_mode != IFF_CISCO && (sp->ipcp.flags & IPCP_VJ) &&
847 ip->ip_p == IPPROTO_TCP)
848 switch (sl_compress_tcp(m, ip, sp->pp_comp,
849 sp->ipcp.compress_cid)) {
850 case TYPE_COMPRESSED_TCP:
851 ipproto = PPP_VJ_COMP;
853 case TYPE_UNCOMPRESSED_TCP:
854 ipproto = PPP_VJ_UCOMP;
868 if (dst->sa_family == AF_INET6) {
869 /* XXX do something tricky here? */
874 * Prepend general data packet PPP header. For now, IP only.
876 M_PREPEND (m, PPP_HEADER_LEN, MB_DONTWAIT);
879 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
886 * May want to check size of packet
887 * (albeit due to the implementation it's always enough)
889 h = mtod (m, struct ppp_header*);
890 if (sp->pp_mode == IFF_CISCO) {
891 h->address = CISCO_UNICAST; /* unicast address */
894 h->address = PPP_ALLSTATIONS; /* broadcast address */
895 h->control = PPP_UI; /* Unnumbered Info */
898 switch (dst->sa_family) {
900 case AF_INET: /* Internet Protocol */
901 if (sp->pp_mode == IFF_CISCO)
902 h->protocol = htons (ETHERTYPE_IP);
905 * Don't choke with an ENETDOWN early. It's
906 * possible that we just started dialing out,
907 * so don't drop the packet immediately. If
908 * we notice that we run out of buffer space
909 * below, we will however remember that we are
910 * not ready to carry IP packets, and return
911 * ENETDOWN, as opposed to ENOBUFS.
913 h->protocol = htons(ipproto);
914 if (sp->state[IDX_IPCP] != STATE_OPENED)
920 case AF_INET6: /* Internet Protocol */
921 if (sp->pp_mode == IFF_CISCO)
922 h->protocol = htons (ETHERTYPE_IPV6);
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(PPP_IPV6);
934 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
940 case AF_NS: /* Xerox NS Protocol */
941 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
942 ETHERTYPE_NS : PPP_XNS);
946 case AF_IPX: /* Novell IPX Protocol */
947 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
948 ETHERTYPE_IPX : PPP_IPX);
955 return (EAFNOSUPPORT);
959 * Queue message on interface, and start output if interface
973 rv = ifq_enqueue(&ifp->if_snd, m, &pktattr);
980 if (! (ifp->if_flags & IFF_OACTIVE))
981 (*ifp->if_start) (ifp);
984 * Count output packets and bytes.
985 * The packet length includes header, FCS and 1 flag,
986 * according to RFC 1333.
988 ifp->if_obytes += m->m_pkthdr.len + 3;
991 * Unlike in sppp_input(), we can always bump the timestamp
992 * here since sppp_output() is only called on behalf of
993 * network-layer traffic; control-layer traffic is handled
996 sp->pp_last_sent = time_second;
1003 sppp_output(struct ifnet *ifp, struct mbuf *m,
1004 struct sockaddr *dst, struct rtentry *rt)
1008 ifnet_serialize_tx(ifp);
1009 error = sppp_output_serialized(ifp, m, dst, rt);
1010 ifnet_deserialize_tx(ifp);
1016 sppp_attach(struct ifnet *ifp)
1018 struct sppp *sp = (struct sppp*) ifp;
1020 /* Initialize keepalive handler. */
1022 callout_reset(&keepalive_timeout, hz * 10,
1023 sppp_keepalive, NULL);
1025 /* Insert new entry into the keepalive list. */
1026 sp->pp_next = spppq;
1029 sp->pp_if.if_mtu = PP_MTU;
1030 sp->pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
1031 sp->pp_if.if_type = IFT_PPP;
1032 sp->pp_if.if_output = sppp_output;
1034 sp->pp_flags = PP_KEEPALIVE;
1036 sp->pp_if.if_snd.ifq_maxlen = 32;
1037 sp->pp_fastq.ifq_maxlen = 32;
1038 sp->pp_cpq.ifq_maxlen = 20;
1040 sp->pp_alivecnt = 0;
1041 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
1042 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
1043 sp->pp_phase = PHASE_DEAD;
1045 sp->pp_down = lcp.Down;
1046 sp->pp_last_recv = sp->pp_last_sent = time_second;
1049 sp->confflags |= CONF_ENABLE_VJ;
1052 sp->confflags |= CONF_ENABLE_IPV6;
1054 sp->pp_comp = kmalloc(sizeof(struct slcompress), M_TEMP, M_WAITOK);
1055 sl_compress_init(sp->pp_comp, -1);
1058 sppp_ipv6cp_init(sp);
1064 sppp_detach(struct ifnet *ifp)
1066 struct sppp **q, *p, *sp = (struct sppp*) ifp;
1069 /* Remove the entry from the keepalive list. */
1070 for (q = &spppq; (p = *q); q = &p->pp_next)
1076 /* Stop keepalive handler. */
1078 callout_stop(&keepalive_timeout);
1080 for (i = 0; i < IDX_COUNT; i++)
1081 callout_stop(&sp->timeout[i]);
1082 callout_stop(&sp->pap_my_to);
1086 * Flush the interface output queue.
1089 sppp_flush(struct ifnet *ifp)
1091 struct sppp *sp = (struct sppp*) ifp;
1093 ifq_purge(&sp->pp_if.if_snd);
1094 IF_DRAIN(&sp->pp_fastq);
1095 IF_DRAIN(&sp->pp_cpq);
1099 * Check if the output queue is empty.
1102 sppp_isempty(struct ifnet *ifp)
1104 struct sppp *sp = (struct sppp*) ifp;
1108 empty = IF_QEMPTY(&sp->pp_fastq) && IF_QEMPTY(&sp->pp_cpq) &&
1109 ifq_is_empty(&sp->pp_if.if_snd);
1115 * Get next packet to send.
1118 sppp_dequeue(struct ifnet *ifp)
1120 struct sppp *sp = (struct sppp*) ifp;
1126 * Process only the control protocol queue until we have at
1127 * least one NCP open.
1129 * Do always serve all three queues in Cisco mode.
1131 IF_DEQUEUE(&sp->pp_cpq, m);
1133 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO)) {
1134 IF_DEQUEUE(&sp->pp_fastq, m);
1136 m = ifq_dequeue(&sp->pp_if.if_snd, NULL);
1144 * Pick the next packet, do not remove it from the queue.
1147 sppp_pick(struct ifnet *ifp)
1149 struct sppp *sp = (struct sppp*)ifp;
1154 m = sp->pp_cpq.ifq_head;
1156 (sp->pp_phase == PHASE_NETWORK || sp->pp_mode == IFF_CISCO)) {
1157 if ((m = sp->pp_fastq.ifq_head) == NULL)
1158 m = ifq_poll(&sp->pp_if.if_snd);
1166 * Process an ioctl request. Called on low priority level.
1169 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1171 struct ifreq *ifr = (struct ifreq*) data;
1172 struct sppp *sp = (struct sppp*) ifp;
1173 int rv, going_up, going_down, newmode;
1180 case SIOCSIFDSTADDR:
1184 /* set the interface "up" when assigning an IP address */
1185 ifp->if_flags |= IFF_UP;
1186 /* fall through... */
1189 going_up = ifp->if_flags & IFF_UP &&
1190 (ifp->if_flags & IFF_RUNNING) == 0;
1191 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1192 ifp->if_flags & IFF_RUNNING;
1194 newmode = ifp->if_flags & IFF_PASSIVE;
1196 newmode = ifp->if_flags & IFF_AUTO;
1198 newmode = ifp->if_flags & IFF_CISCO;
1199 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1200 ifp->if_flags |= newmode;
1202 if (newmode != sp->pp_mode) {
1205 going_up = ifp->if_flags & IFF_RUNNING;
1209 if (sp->pp_mode != IFF_CISCO)
1211 else if (sp->pp_tlf)
1214 ifp->if_flags &= ~IFF_RUNNING;
1215 sp->pp_mode = newmode;
1219 if (sp->pp_mode != IFF_CISCO)
1221 sp->pp_mode = newmode;
1222 if (sp->pp_mode == 0) {
1223 ifp->if_flags |= IFF_RUNNING;
1226 if (sp->pp_mode == IFF_CISCO) {
1229 ifp->if_flags |= IFF_RUNNING;
1237 #define ifr_mtu ifr_metric
1240 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1242 ifp->if_mtu = ifr->ifr_mtu;
1247 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1249 ifp->if_mtu = *(short*)data;
1254 ifr->ifr_mtu = ifp->if_mtu;
1259 *(short*)data = ifp->if_mtu;
1266 case SIOCGIFGENERIC:
1267 case SIOCSIFGENERIC:
1268 rv = sppp_params(sp, cmd, data);
1280 * Cisco framing implementation.
1284 * Handle incoming Cisco keepalive protocol packets.
1287 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1290 struct cisco_packet *h;
1293 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1296 SPP_FMT "cisco invalid packet length: %d bytes\n",
1297 SPP_ARGS(ifp), m->m_pkthdr.len);
1300 h = mtod (m, struct cisco_packet*);
1303 SPP_FMT "cisco input: %d bytes "
1304 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1305 SPP_ARGS(ifp), m->m_pkthdr.len,
1306 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1307 (u_int)h->time0, (u_int)h->time1);
1308 switch (ntohl (h->type)) {
1311 log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n",
1312 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1314 case CISCO_ADDR_REPLY:
1315 /* Reply on address request, ignore */
1317 case CISCO_KEEPALIVE_REQ:
1318 sp->pp_alivecnt = 0;
1319 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1320 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1321 /* Local and remote sequence numbers are equal.
1322 * Probably, the line is in loopback mode. */
1323 if (sp->pp_loopcnt >= MAXALIVECNT) {
1324 kprintf (SPP_FMT "loopback\n",
1327 if (ifp->if_flags & IFF_UP) {
1329 IF_DRAIN(&sp->pp_cpq);
1334 /* Generate new local sequence number */
1335 #if defined(__DragonFly__)
1336 sp->pp_seq[IDX_LCP] = krandom();
1338 sp->pp_seq[IDX_LCP] ^= time.tv_sec ^ time.tv_usec;
1343 if (! (ifp->if_flags & IFF_UP) &&
1344 (ifp->if_flags & IFF_RUNNING)) {
1346 kprintf (SPP_FMT "up\n", SPP_ARGS(ifp));
1349 case CISCO_ADDR_REQ:
1350 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1352 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1358 * Send Cisco keepalive packet.
1361 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1364 struct ppp_header *h;
1365 struct cisco_packet *ch;
1367 #if defined(__DragonFly__)
1370 u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
1373 #if defined(__DragonFly__)
1374 getmicrouptime(&tv);
1377 MGETHDR (m, MB_DONTWAIT, MT_DATA);
1380 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1381 m->m_pkthdr.rcvif = 0;
1383 h = mtod (m, struct ppp_header*);
1384 h->address = CISCO_MULTICAST;
1386 h->protocol = htons (CISCO_KEEPALIVE);
1388 ch = (struct cisco_packet*) (h + 1);
1389 ch->type = htonl (type);
1390 ch->par1 = htonl (par1);
1391 ch->par2 = htonl (par2);
1394 #if defined(__DragonFly__)
1395 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1396 ch->time1 = htons ((u_short) tv.tv_sec);
1398 ch->time0 = htons ((u_short) (t >> 16));
1399 ch->time1 = htons ((u_short) t);
1404 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1405 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1406 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1408 if (IF_QFULL (&sp->pp_cpq)) {
1409 IF_DROP (&sp->pp_fastq);
1410 IF_DROP (&ifp->if_snd);
1413 IF_ENQUEUE (&sp->pp_cpq, m);
1414 if (! (ifp->if_flags & IFF_OACTIVE))
1415 (*ifp->if_start) (ifp);
1416 ifp->if_obytes += m->m_pkthdr.len + 3;
1420 * PPP protocol implementation.
1424 * Send PPP control protocol packet.
1427 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1428 u_char ident, u_short len, void *data)
1431 struct ppp_header *h;
1432 struct lcp_header *lh;
1435 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1436 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1437 MGETHDR (m, MB_DONTWAIT, MT_DATA);
1440 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1441 m->m_pkthdr.rcvif = 0;
1443 h = mtod (m, struct ppp_header*);
1444 h->address = PPP_ALLSTATIONS; /* broadcast address */
1445 h->control = PPP_UI; /* Unnumbered Info */
1446 h->protocol = htons (proto); /* Link Control Protocol */
1448 lh = (struct lcp_header*) (h + 1);
1451 lh->len = htons (LCP_HEADER_LEN + len);
1453 bcopy (data, lh+1, len);
1456 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1458 sppp_proto_name(proto),
1459 sppp_cp_type_name (lh->type), lh->ident,
1461 sppp_print_bytes ((u_char*) (lh+1), len);
1464 if (IF_QFULL (&sp->pp_cpq)) {
1465 IF_DROP (&sp->pp_fastq);
1466 IF_DROP (&ifp->if_snd);
1470 IF_ENQUEUE (&sp->pp_cpq, m);
1471 if (! (ifp->if_flags & IFF_OACTIVE))
1472 (*ifp->if_start) (ifp);
1473 ifp->if_obytes += m->m_pkthdr.len + 3;
1477 * Handle incoming PPP control protocol packets.
1480 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1483 struct lcp_header *h;
1484 int printlen, len = m->m_pkthdr.len;
1491 SPP_FMT "%s invalid packet length: %d bytes\n",
1492 SPP_ARGS(ifp), cp->name, len);
1495 h = mtod (m, struct lcp_header*);
1497 printlen = ntohs(h->len);
1499 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1500 SPP_ARGS(ifp), cp->name,
1501 sppp_state_name(sp->state[cp->protoidx]),
1502 sppp_cp_type_name (h->type), h->ident, printlen);
1506 sppp_print_bytes ((u_char*) (h+1), printlen - 4);
1509 if (len > ntohs (h->len))
1510 len = ntohs (h->len);
1511 p = (u_char *)(h + 1);
1516 log(-1, SPP_FMT "%s invalid conf-req length %d\n",
1517 SPP_ARGS(ifp), cp->name,
1522 /* handle states where RCR doesn't get a SCA/SCN */
1523 switch (sp->state[cp->protoidx]) {
1525 case STATE_STOPPING:
1528 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1532 rv = (cp->RCR)(sp, h, len);
1534 /* fatal error, shut down */
1539 switch (sp->state[cp->protoidx]) {
1543 /* fall through... */
1544 case STATE_ACK_SENT:
1545 case STATE_REQ_SENT:
1547 * sppp_cp_change_state() have the side effect of
1548 * restarting the timeouts. We want to avoid that
1549 * if the state don't change, otherwise we won't
1550 * ever timeout and resend a configuration request
1553 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1556 sppp_cp_change_state(cp, sp, rv?
1557 STATE_ACK_SENT: STATE_REQ_SENT);
1560 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1562 sppp_cp_change_state(cp, sp, rv?
1563 STATE_ACK_SENT: STATE_REQ_SENT);
1565 case STATE_ACK_RCVD:
1567 sppp_cp_change_state(cp, sp, STATE_OPENED);
1569 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1574 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1577 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1578 SPP_ARGS(ifp), cp->name,
1579 sppp_cp_type_name(h->type),
1580 sppp_state_name(sp->state[cp->protoidx]));
1585 if (h->ident != sp->confid[cp->protoidx]) {
1587 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1588 SPP_ARGS(ifp), cp->name,
1589 h->ident, sp->confid[cp->protoidx]);
1593 switch (sp->state[cp->protoidx]) {
1596 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1599 case STATE_STOPPING:
1601 case STATE_REQ_SENT:
1602 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1603 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1608 case STATE_ACK_RCVD:
1610 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1612 case STATE_ACK_SENT:
1613 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1614 sppp_cp_change_state(cp, sp, STATE_OPENED);
1616 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1617 SPP_ARGS(ifp), cp->name);
1621 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1622 SPP_ARGS(ifp), cp->name,
1623 sppp_cp_type_name(h->type),
1624 sppp_state_name(sp->state[cp->protoidx]));
1630 if (h->ident != sp->confid[cp->protoidx]) {
1632 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1633 SPP_ARGS(ifp), cp->name,
1634 h->ident, sp->confid[cp->protoidx]);
1638 if (h->type == CONF_NAK)
1639 (cp->RCN_nak)(sp, h, len);
1641 (cp->RCN_rej)(sp, h, len);
1643 switch (sp->state[cp->protoidx]) {
1646 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1648 case STATE_REQ_SENT:
1649 case STATE_ACK_SENT:
1650 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1652 * Slow things down a bit if we think we might be
1653 * in loopback. Depend on the timeout to send the
1654 * next configuration request.
1663 case STATE_ACK_RCVD:
1664 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1668 case STATE_STOPPING:
1671 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1672 SPP_ARGS(ifp), cp->name,
1673 sppp_cp_type_name(h->type),
1674 sppp_state_name(sp->state[cp->protoidx]));
1680 switch (sp->state[cp->protoidx]) {
1681 case STATE_ACK_RCVD:
1682 case STATE_ACK_SENT:
1683 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1688 case STATE_STOPPING:
1689 case STATE_REQ_SENT:
1691 /* Send Terminate-Ack packet. */
1693 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1694 SPP_ARGS(ifp), cp->name);
1695 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1699 sp->rst_counter[cp->protoidx] = 0;
1700 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1704 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1705 SPP_ARGS(ifp), cp->name,
1706 sppp_cp_type_name(h->type),
1707 sppp_state_name(sp->state[cp->protoidx]));
1712 switch (sp->state[cp->protoidx]) {
1715 case STATE_REQ_SENT:
1716 case STATE_ACK_SENT:
1719 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1722 case STATE_STOPPING:
1723 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1726 case STATE_ACK_RCVD:
1727 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1732 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1735 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1736 SPP_ARGS(ifp), cp->name,
1737 sppp_cp_type_name(h->type),
1738 sppp_state_name(sp->state[cp->protoidx]));
1743 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1745 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1746 "danger will robinson\n",
1747 SPP_ARGS(ifp), cp->name,
1748 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1749 switch (sp->state[cp->protoidx]) {
1752 case STATE_REQ_SENT:
1753 case STATE_ACK_SENT:
1755 case STATE_STOPPING:
1758 case STATE_ACK_RCVD:
1759 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1762 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1763 SPP_ARGS(ifp), cp->name,
1764 sppp_cp_type_name(h->type),
1765 sppp_state_name(sp->state[cp->protoidx]));
1772 const struct cp *upper;
1778 proto = ntohs(*((u_int16_t *)p));
1779 for (i = 0; i < IDX_COUNT; i++) {
1780 if (cps[i]->proto == proto) {
1788 if (catastrophic || debug)
1789 log(catastrophic? LOG_INFO: LOG_DEBUG,
1790 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1791 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1792 sppp_cp_type_name(h->type), proto,
1793 upper ? upper->name : "unknown",
1794 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1797 * if we got RXJ+ against conf-req, the peer does not implement
1798 * this particular protocol type. terminate the protocol.
1800 if (upper && !catastrophic) {
1801 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1807 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1808 switch (sp->state[cp->protoidx]) {
1811 case STATE_REQ_SENT:
1812 case STATE_ACK_SENT:
1814 case STATE_STOPPING:
1817 case STATE_ACK_RCVD:
1818 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1821 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1822 SPP_ARGS(ifp), cp->name,
1823 sppp_cp_type_name(h->type),
1824 sppp_state_name(sp->state[cp->protoidx]));
1830 if (cp->proto != PPP_LCP)
1832 /* Discard the packet. */
1835 if (cp->proto != PPP_LCP)
1837 if (sp->state[cp->protoidx] != STATE_OPENED) {
1839 log(-1, SPP_FMT "lcp echo req but lcp closed\n",
1846 log(-1, SPP_FMT "invalid lcp echo request "
1847 "packet length: %d bytes\n",
1848 SPP_ARGS(ifp), len);
1851 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1852 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1853 /* Line loopback mode detected. */
1854 kprintf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1855 sp->pp_loopcnt = MAXALIVECNT * 5;
1857 IF_DRAIN(&sp->pp_cpq);
1859 /* Shut down the PPP link. */
1865 *(long*)(h+1) = htonl (sp->lcp.magic);
1867 log(-1, SPP_FMT "got lcp echo req, sending echo rep\n",
1869 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1872 if (cp->proto != PPP_LCP)
1874 if (h->ident != sp->lcp.echoid) {
1880 log(-1, SPP_FMT "lcp invalid echo reply "
1881 "packet length: %d bytes\n",
1882 SPP_ARGS(ifp), len);
1886 log(-1, SPP_FMT "lcp got echo rep\n",
1888 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1889 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1890 sp->pp_alivecnt = 0;
1893 /* Unknown packet type -- send Code-Reject packet. */
1896 log(-1, SPP_FMT "%s send code-rej for 0x%x\n",
1897 SPP_ARGS(ifp), cp->name, h->type);
1898 sppp_cp_send(sp, cp->proto, CODE_REJ,
1899 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1906 * The generic part of all Up/Down/Open/Close/TO event handlers.
1907 * Basically, the state transition handling in the automaton.
1910 sppp_up_event(const struct cp *cp, struct sppp *sp)
1915 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1916 SPP_ARGS(ifp), cp->name,
1917 sppp_state_name(sp->state[cp->protoidx]));
1919 switch (sp->state[cp->protoidx]) {
1921 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1923 case STATE_STARTING:
1924 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1926 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1929 kprintf(SPP_FMT "%s illegal up in state %s\n",
1930 SPP_ARGS(ifp), cp->name,
1931 sppp_state_name(sp->state[cp->protoidx]));
1936 sppp_down_event(const struct cp *cp, struct sppp *sp)
1941 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1942 SPP_ARGS(ifp), cp->name,
1943 sppp_state_name(sp->state[cp->protoidx]));
1945 switch (sp->state[cp->protoidx]) {
1948 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1951 sppp_cp_change_state(cp, sp, STATE_STARTING);
1954 case STATE_STOPPING:
1955 case STATE_REQ_SENT:
1956 case STATE_ACK_RCVD:
1957 case STATE_ACK_SENT:
1958 sppp_cp_change_state(cp, sp, STATE_STARTING);
1962 sppp_cp_change_state(cp, sp, STATE_STARTING);
1965 kprintf(SPP_FMT "%s illegal down in state %s\n",
1966 SPP_ARGS(ifp), cp->name,
1967 sppp_state_name(sp->state[cp->protoidx]));
1973 sppp_open_event(const struct cp *cp, struct sppp *sp)
1978 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1979 SPP_ARGS(ifp), cp->name,
1980 sppp_state_name(sp->state[cp->protoidx]));
1982 switch (sp->state[cp->protoidx]) {
1984 sppp_cp_change_state(cp, sp, STATE_STARTING);
1987 case STATE_STARTING:
1990 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1992 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1996 * Try escaping stopped state. This seems to bite
1997 * people occasionally, in particular for IPCP,
1998 * presumably following previous IPCP negotiation
1999 * aborts. Somehow, we must have missed a Down event
2000 * which would have caused a transition into starting
2001 * state, so as a bandaid we force the Down event now.
2002 * This effectively implements (something like the)
2003 * `restart' option mentioned in the state transition
2004 * table of RFC 1661.
2006 sppp_cp_change_state(cp, sp, STATE_STARTING);
2009 case STATE_STOPPING:
2010 case STATE_REQ_SENT:
2011 case STATE_ACK_RCVD:
2012 case STATE_ACK_SENT:
2016 sppp_cp_change_state(cp, sp, STATE_STOPPING);
2023 sppp_close_event(const struct cp *cp, struct sppp *sp)
2028 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
2029 SPP_ARGS(ifp), cp->name,
2030 sppp_state_name(sp->state[cp->protoidx]));
2032 switch (sp->state[cp->protoidx]) {
2037 case STATE_STARTING:
2038 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2042 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2044 case STATE_STOPPING:
2045 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2050 case STATE_REQ_SENT:
2051 case STATE_ACK_RCVD:
2052 case STATE_ACK_SENT:
2053 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2054 sppp_cp_send(sp, cp->proto, TERM_REQ,
2055 ++sp->pp_seq[cp->protoidx], 0, 0);
2056 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2062 sppp_to_event(const struct cp *cp, struct sppp *sp)
2069 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
2070 SPP_ARGS(ifp), cp->name,
2071 sppp_state_name(sp->state[cp->protoidx]),
2072 sp->rst_counter[cp->protoidx]);
2074 if (--sp->rst_counter[cp->protoidx] < 0)
2076 switch (sp->state[cp->protoidx]) {
2078 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2081 case STATE_STOPPING:
2082 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2085 case STATE_REQ_SENT:
2086 case STATE_ACK_RCVD:
2087 case STATE_ACK_SENT:
2088 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2094 switch (sp->state[cp->protoidx]) {
2096 case STATE_STOPPING:
2097 sppp_cp_send(sp, cp->proto, TERM_REQ,
2098 ++sp->pp_seq[cp->protoidx], 0, 0);
2099 callout_reset(&sp->timeout[cp->protoidx],
2100 sp->lcp.timeout, cp->TO, sp);
2102 case STATE_REQ_SENT:
2103 case STATE_ACK_RCVD:
2105 /* sppp_cp_change_state() will restart the timer */
2106 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2108 case STATE_ACK_SENT:
2110 callout_reset(&sp->timeout[cp->protoidx],
2111 sp->lcp.timeout, cp->TO, sp);
2119 * Change the state of a control protocol in the state automaton.
2120 * Takes care of starting/stopping the restart timer.
2123 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2125 sp->state[cp->protoidx] = newstate;
2126 callout_stop(&sp->timeout[cp->protoidx]);
2130 case STATE_STARTING:
2136 case STATE_STOPPING:
2137 case STATE_REQ_SENT:
2138 case STATE_ACK_RCVD:
2139 case STATE_ACK_SENT:
2140 callout_reset(&sp->timeout[cp->protoidx],
2141 sp->lcp.timeout, cp->TO, sp);
2147 *--------------------------------------------------------------------------*
2149 * The LCP implementation. *
2151 *--------------------------------------------------------------------------*
2154 sppp_lcp_init(struct sppp *sp)
2156 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2158 sp->state[IDX_LCP] = STATE_INITIAL;
2159 sp->fail_counter[IDX_LCP] = 0;
2160 sp->pp_seq[IDX_LCP] = 0;
2161 sp->pp_rseq[IDX_LCP] = 0;
2163 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2165 /* Note that these values are relevant for all control protocols */
2166 sp->lcp.timeout = 3 * hz;
2167 sp->lcp.max_terminate = 2;
2168 sp->lcp.max_configure = 10;
2169 sp->lcp.max_failure = 10;
2170 #if defined(__DragonFly__)
2171 callout_init(&sp->timeout[IDX_LCP]);
2176 sppp_lcp_up(struct sppp *sp)
2180 sp->pp_alivecnt = 0;
2181 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2184 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2186 * If this interface is passive or dial-on-demand, and we are
2187 * still in Initial state, it means we've got an incoming
2188 * call. Activate the interface.
2190 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2193 SPP_FMT "Up event", SPP_ARGS(ifp));
2194 ifp->if_flags |= IFF_RUNNING;
2195 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2197 log(-1, "(incoming call)\n");
2198 sp->pp_flags |= PP_CALLIN;
2202 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2203 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2204 ifp->if_flags |= IFF_RUNNING;
2208 sppp_up_event(&lcp, sp);
2212 sppp_lcp_down(struct sppp *sp)
2216 sppp_down_event(&lcp, sp);
2219 * If this is neither a dial-on-demand nor a passive
2220 * interface, simulate an ``ifconfig down'' action, so the
2221 * administrator can force a redial by another ``ifconfig
2222 * up''. XXX For leased line operation, should we immediately
2223 * try to reopen the connection here?
2225 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2227 SPP_FMT "Down event, taking interface down.\n",
2233 SPP_FMT "Down event (carrier loss)\n",
2235 sp->pp_flags &= ~PP_CALLIN;
2236 if (sp->state[IDX_LCP] != STATE_INITIAL)
2238 ifp->if_flags &= ~IFF_RUNNING;
2243 sppp_lcp_open(struct sppp *sp)
2246 * If we are authenticator, negotiate LCP_AUTH
2248 if (sp->hisauth.proto != 0)
2249 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2251 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2252 sp->pp_flags &= ~PP_NEEDAUTH;
2253 sppp_open_event(&lcp, sp);
2257 sppp_lcp_close(struct sppp *sp)
2259 sppp_close_event(&lcp, sp);
2263 sppp_lcp_TO(void *cookie)
2265 sppp_to_event(&lcp, (struct sppp *)cookie);
2269 * Analyze a configure request. Return true if it was agreeable, and
2270 * caused action sca, false if it has been rejected or nak'ed, and
2271 * caused action scn. (The return value is used to make the state
2272 * transition decision in the state automaton.)
2275 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2278 u_char *buf, *r, *p;
2285 buf = r = kmalloc (len, M_TEMP, M_INTWAIT);
2288 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2291 /* pass 1: check for things that need to be rejected */
2293 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2294 /* Sanity check option length */
2296 /* Malicious option - drop immediately.
2297 * XXX Maybe we should just RXJ it?
2299 log(-1, "%s: received malicious LCP option 0x%02x, "
2300 "length 0x%02x, (len: 0x%02x) dropping.\n", ifp->if_xname,
2305 log(-1, " %s ", sppp_lcp_opt_name(*p));
2309 if (len >= 6 && p[1] == 6)
2312 log(-1, "[invalid] ");
2314 case LCP_OPT_ASYNC_MAP:
2315 /* Async control character map. */
2316 if (len >= 6 && p[1] == 6)
2319 log(-1, "[invalid] ");
2322 /* Maximum receive unit. */
2323 if (len >= 4 && p[1] == 4)
2326 log(-1, "[invalid] ");
2328 case LCP_OPT_AUTH_PROTO:
2331 log(-1, "[invalid] ");
2334 authproto = (p[2] << 8) + p[3];
2335 if (authproto == PPP_CHAP && p[1] != 5) {
2337 log(-1, "[invalid chap len] ");
2340 if (sp->myauth.proto == 0) {
2341 /* we are not configured to do auth */
2343 log(-1, "[not configured] ");
2347 * Remote want us to authenticate, remember this,
2348 * so we stay in PHASE_AUTHENTICATE after LCP got
2351 sp->pp_flags |= PP_NEEDAUTH;
2354 /* Others not supported. */
2359 /* Add the option to rejected list. */
2366 log(-1, " send conf-rej\n");
2367 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2373 * pass 2: check for option values that are unacceptable and
2374 * thus require to be nak'ed.
2377 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2382 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2384 log(-1, " %s ", sppp_lcp_opt_name(*p));
2387 /* Magic number -- extract. */
2388 nmagic = (u_long)p[2] << 24 |
2389 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2390 if (nmagic != sp->lcp.magic) {
2393 log(-1, "0x%lx ", nmagic);
2396 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2397 log(-1, "[glitch] ");
2400 * We negate our magic here, and NAK it. If
2401 * we see it later in an NAK packet, we
2402 * suggest a new one.
2404 nmagic = ~sp->lcp.magic;
2406 p[2] = nmagic >> 24;
2407 p[3] = nmagic >> 16;
2412 case LCP_OPT_ASYNC_MAP:
2414 * Async control character map -- just ignore it.
2416 * Quote from RFC 1662, chapter 6:
2417 * To enable this functionality, synchronous PPP
2418 * implementations MUST always respond to the
2419 * Async-Control-Character-Map Configuration
2420 * Option with the LCP Configure-Ack. However,
2421 * acceptance of the Configuration Option does
2422 * not imply that the synchronous implementation
2423 * will do any ACCM mapping. Instead, all such
2424 * octet mapping will be performed by the
2425 * asynchronous-to-synchronous converter.
2431 * Maximum receive unit. Always agreeable,
2432 * but ignored by now.
2434 sp->lcp.their_mru = p[2] * 256 + p[3];
2436 log(-1, "%lu ", sp->lcp.their_mru);
2439 case LCP_OPT_AUTH_PROTO:
2440 authproto = (p[2] << 8) + p[3];
2441 if (sp->myauth.proto != authproto) {
2442 /* not agreed, nak */
2444 log(-1, "[mine %s != his %s] ",
2445 sppp_proto_name(sp->hisauth.proto),
2446 sppp_proto_name(authproto));
2447 p[2] = sp->myauth.proto >> 8;
2448 p[3] = sp->myauth.proto;
2451 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2453 log(-1, "[chap not MD5] ");
2459 /* Add the option to nak'ed list. */
2466 * Local and remote magics equal -- loopback?
2468 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2469 if (sp->pp_loopcnt == MAXALIVECNT*5)
2470 kprintf (SPP_FMT "loopback\n",
2472 if (ifp->if_flags & IFF_UP) {
2474 IF_DRAIN(&sp->pp_cpq);
2479 } else if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2481 log(-1, " max_failure (%d) exceeded, "
2483 sp->lcp.max_failure);
2484 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2487 log(-1, " send conf-nak\n");
2488 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2492 log(-1, " send conf-ack\n");
2493 sp->fail_counter[IDX_LCP] = 0;
2495 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2496 h->ident, origlen, h+1);
2499 kfree (buf, M_TEMP);
2508 * Analyze the LCP Configure-Reject option list, and adjust our
2512 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2518 buf = kmalloc (len, M_TEMP, M_INTWAIT);
2521 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2525 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2526 /* Sanity check option length */
2529 * Malicious option - drop immediately.
2530 * XXX Maybe we should just RXJ it?
2532 log(-1, "%s: received malicious LCP option, "
2533 "dropping.\n", ifp->if_xname);
2537 log(-1, " %s ", sppp_lcp_opt_name(*p));
2540 /* Magic number -- can't use it, use 0 */
2541 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2546 * Should not be rejected anyway, since we only
2547 * negotiate a MRU if explicitly requested by
2550 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2552 case LCP_OPT_AUTH_PROTO:
2554 * Peer doesn't want to authenticate himself,
2555 * deny unless this is a dialout call, and
2556 * AUTHFLAG_NOCALLOUT is set.
2558 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2559 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2561 log(-1, "[don't insist on auth "
2563 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2567 log(-1, "[access denied]\n");
2575 kfree (buf, M_TEMP);
2580 * Analyze the LCP Configure-NAK option list, and adjust our
2584 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2591 buf = kmalloc (len, M_TEMP, M_INTWAIT);
2594 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2598 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2599 /* Sanity check option length */
2602 * Malicious option - drop immediately.
2603 * XXX Maybe we should just RXJ it?
2605 log(-1, "%s: received malicious LCP option, "
2606 "dropping.\n", ifp->if_xname);
2610 log(-1, " %s ", sppp_lcp_opt_name(*p));
2613 /* Magic number -- renegotiate */
2614 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2615 len >= 6 && p[1] == 6) {
2616 magic = (u_long)p[2] << 24 |
2617 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2619 * If the remote magic is our negated one,
2620 * this looks like a loopback problem.
2621 * Suggest a new magic to make sure.
2623 if (magic == ~sp->lcp.magic) {
2625 log(-1, "magic glitch ");
2626 #if defined(__DragonFly__)
2627 sp->lcp.magic = krandom();
2629 sp->lcp.magic = time.tv_sec + time.tv_usec;
2632 sp->lcp.magic = magic;
2634 log(-1, "%lu ", magic);
2640 * Peer wants to advise us to negotiate an MRU.
2641 * Agree on it if it's reasonable, or use
2642 * default otherwise.
2644 if (len >= 4 && p[1] == 4) {
2645 u_int mru = p[2] * 256 + p[3];
2647 log(-1, "%d ", mru);
2648 if (mru < PP_MTU || mru > PP_MAX_MRU)
2651 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2654 case LCP_OPT_AUTH_PROTO:
2656 * Peer doesn't like our authentication method,
2660 log(-1, "[access denied]\n");
2668 kfree (buf, M_TEMP);
2673 sppp_lcp_tlu(struct sppp *sp)
2680 if (! (ifp->if_flags & IFF_UP) &&
2681 (ifp->if_flags & IFF_RUNNING)) {
2682 /* Coming out of loopback mode. */
2684 kprintf (SPP_FMT "up\n", SPP_ARGS(ifp));
2687 for (i = 0; i < IDX_COUNT; i++)
2688 if ((cps[i])->flags & CP_QUAL)
2691 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2692 (sp->pp_flags & PP_NEEDAUTH) != 0)
2693 sp->pp_phase = PHASE_AUTHENTICATE;
2695 sp->pp_phase = PHASE_NETWORK;
2698 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2699 sppp_phase_name(sp->pp_phase));
2702 * Open all authentication protocols. This is even required
2703 * if we already proceeded to network phase, since it might be
2704 * that remote wants us to authenticate, so we might have to
2705 * send a PAP request. Undesired authentication protocols
2706 * don't do anything when they get an Open event.
2708 for (i = 0; i < IDX_COUNT; i++)
2709 if ((cps[i])->flags & CP_AUTH)
2712 if (sp->pp_phase == PHASE_NETWORK) {
2713 /* Notify all NCPs. */
2714 for (i = 0; i < IDX_COUNT; i++)
2715 if (((cps[i])->flags & CP_NCP) &&
2718 * Hack to administratively disable IPv6 if
2719 * not desired. Perhaps we should have another
2720 * flag for this, but right now, we can make
2721 * all struct cp's read/only.
2723 (cps[i] != &ipv6cp ||
2724 (sp->confflags & CONF_ENABLE_IPV6)))
2728 /* Send Up events to all started protos. */
2729 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2730 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2733 /* notify low-level driver of state change */
2735 sp->pp_chg(sp, (int)sp->pp_phase);
2737 if (sp->pp_phase == PHASE_NETWORK)
2738 /* if no NCP is starting, close down */
2739 sppp_lcp_check_and_close(sp);
2743 sppp_lcp_tld(struct sppp *sp)
2749 sp->pp_phase = PHASE_TERMINATE;
2752 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2753 sppp_phase_name(sp->pp_phase));
2756 * Take upper layers down. We send the Down event first and
2757 * the Close second to prevent the upper layers from sending
2758 * ``a flurry of terminate-request packets'', as the RFC
2761 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2762 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2764 (cps[i])->Close(sp);
2769 sppp_lcp_tls(struct sppp *sp)
2773 sp->pp_phase = PHASE_ESTABLISH;
2776 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2777 sppp_phase_name(sp->pp_phase));
2779 /* Notify lower layer if desired. */
2787 sppp_lcp_tlf(struct sppp *sp)
2791 sp->pp_phase = PHASE_DEAD;
2793 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2794 sppp_phase_name(sp->pp_phase));
2796 /* Notify lower layer if desired. */
2804 sppp_lcp_scr(struct sppp *sp)
2806 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2810 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2811 if (! sp->lcp.magic)
2812 #if defined(__DragonFly__)
2813 sp->lcp.magic = krandom();
2815 sp->lcp.magic = time.tv_sec + time.tv_usec;
2817 opt[i++] = LCP_OPT_MAGIC;
2819 opt[i++] = sp->lcp.magic >> 24;
2820 opt[i++] = sp->lcp.magic >> 16;
2821 opt[i++] = sp->lcp.magic >> 8;
2822 opt[i++] = sp->lcp.magic;
2825 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2826 opt[i++] = LCP_OPT_MRU;
2828 opt[i++] = sp->lcp.mru >> 8;
2829 opt[i++] = sp->lcp.mru;
2832 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2833 authproto = sp->hisauth.proto;
2834 opt[i++] = LCP_OPT_AUTH_PROTO;
2835 opt[i++] = authproto == PPP_CHAP? 5: 4;
2836 opt[i++] = authproto >> 8;
2837 opt[i++] = authproto;
2838 if (authproto == PPP_CHAP)
2839 opt[i++] = CHAP_MD5;
2842 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2843 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2847 * Check the open NCPs, return true if at least one NCP is open.
2850 sppp_ncp_check(struct sppp *sp)
2854 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2855 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2861 * Re-check the open NCPs and see if we should terminate the link.
2862 * Called by the NCPs during their tlf action handling.
2865 sppp_lcp_check_and_close(struct sppp *sp)
2868 if (sp->pp_phase < PHASE_NETWORK)
2869 /* don't bother, we are already going down */
2872 if (sppp_ncp_check(sp))
2879 *--------------------------------------------------------------------------*
2881 * The IPCP implementation. *
2883 *--------------------------------------------------------------------------*
2887 sppp_ipcp_init(struct sppp *sp)
2891 sp->state[IDX_IPCP] = STATE_INITIAL;
2892 sp->fail_counter[IDX_IPCP] = 0;
2893 sp->pp_seq[IDX_IPCP] = 0;
2894 sp->pp_rseq[IDX_IPCP] = 0;
2895 #if defined(__DragonFly__)
2896 callout_init(&sp->timeout[IDX_IPCP]);
2901 sppp_ipcp_up(struct sppp *sp)
2903 sppp_up_event(&ipcp, sp);
2907 sppp_ipcp_down(struct sppp *sp)
2909 sppp_down_event(&ipcp, sp);
2913 sppp_ipcp_open(struct sppp *sp)
2916 u_long myaddr, hisaddr;
2918 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2919 IPCP_MYADDR_DYN | IPCP_VJ);
2922 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2924 * If we don't have his address, this probably means our
2925 * interface doesn't want to talk IP at all. (This could
2926 * be the case if somebody wants to speak only IPX, for
2927 * example.) Don't open IPCP in this case.
2929 if (hisaddr == 0L) {
2930 /* XXX this message should go away */
2932 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2938 * I don't have an assigned address, so i need to
2939 * negotiate my address.
2941 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2942 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2944 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2945 if (sp->confflags & CONF_ENABLE_VJ) {
2946 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2947 sp->ipcp.max_state = MAX_STATES - 1;
2948 sp->ipcp.compress_cid = 1;
2950 sppp_open_event(&ipcp, sp);
2954 sppp_ipcp_close(struct sppp *sp)
2956 sppp_close_event(&ipcp, sp);
2957 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2959 * My address was dynamic, clear it again.
2961 sppp_set_ip_addr(sp, 0L);
2965 sppp_ipcp_TO(void *cookie)
2967 sppp_to_event(&ipcp, (struct sppp *)cookie);
2971 * Analyze a configure request. Return true if it was agreeable, and
2972 * caused action sca, false if it has been rejected or nak'ed, and
2973 * caused action scn. (The return value is used to make the state
2974 * transition decision in the state automaton.)
2977 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2979 u_char *buf, *r, *p;
2980 struct ifnet *ifp = &sp->pp_if;
2981 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2982 u_long hisaddr, desiredaddr;
2989 * Make sure to allocate a buf that can at least hold a
2990 * conf-nak with an `address' option. We might need it below.
2992 buf = r = kmalloc ((len < 6? 6: len), M_TEMP, M_INTWAIT);
2994 /* pass 1: see if we can recognize them */
2996 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2999 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3000 /* Sanity check option length */
3002 /* XXX should we just RXJ? */
3003 log(-1, "%s: malicious IPCP option received, dropping\n",
3008 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3010 case IPCP_OPT_COMPRESSION:
3011 if (!(sp->confflags & CONF_ENABLE_VJ)) {
3012 /* VJ compression administratively disabled */
3014 log(-1, "[locally disabled] ");
3018 * In theory, we should only conf-rej an
3019 * option that is shorter than RFC 1618
3020 * requires (i.e. < 4), and should conf-nak
3021 * anything else that is not VJ. However,
3022 * since our algorithm always uses the
3023 * original option to NAK it with new values,
3024 * things would become more complicated. In
3025 * pratice, the only commonly implemented IP
3026 * compression option is VJ anyway, so the
3027 * difference is negligible.
3029 if (len >= 6 && p[1] == 6) {
3031 * correctly formed compression option
3032 * that could be VJ compression
3037 log(-1, "optlen %d [invalid/unsupported] ",
3040 case IPCP_OPT_ADDRESS:
3041 if (len >= 6 && p[1] == 6) {
3042 /* correctly formed address option */
3046 log(-1, "[invalid] ");
3049 /* Others not supported. */
3054 /* Add the option to rejected list. */
3061 log(-1, " send conf-rej\n");
3062 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
3067 /* pass 2: parse option values */
3068 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
3070 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
3074 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3076 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3078 case IPCP_OPT_COMPRESSION:
3079 desiredcomp = p[2] << 8 | p[3];
3080 /* We only support VJ */
3081 if (desiredcomp == IPCP_COMP_VJ) {
3083 log(-1, "VJ [ack] ");
3084 sp->ipcp.flags |= IPCP_VJ;
3085 sl_compress_init(sp->pp_comp, p[4]);
3086 sp->ipcp.max_state = p[4];
3087 sp->ipcp.compress_cid = p[5];
3091 log(-1, "compproto %#04x [not supported] ",
3093 p[2] = IPCP_COMP_VJ >> 8;
3094 p[3] = IPCP_COMP_VJ;
3095 p[4] = sp->ipcp.max_state;
3096 p[5] = sp->ipcp.compress_cid;
3098 case IPCP_OPT_ADDRESS:
3099 /* This is the address he wants in his end */
3100 desiredaddr = p[2] << 24 | p[3] << 16 |
3102 if (desiredaddr == hisaddr ||
3103 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3105 * Peer's address is same as our value,
3106 * or we have set it to 0.0.0.* to
3107 * indicate that we do not really care,
3108 * this is agreeable. Gonna conf-ack
3112 log(-1, "%s [ack] ",
3113 sppp_dotted_quad(hisaddr));
3114 /* record that we've seen it already */
3115 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3119 * The address wasn't agreeable. This is either
3120 * he sent us 0.0.0.0, asking to assign him an
3121 * address, or he send us another address not
3122 * matching our value. Either case, we gonna
3123 * conf-nak it with our value.
3124 * XXX: we should "rej" if hisaddr == 0
3127 if (desiredaddr == 0)
3128 log(-1, "[addr requested] ");
3130 log(-1, "%s [not agreed] ",
3131 sppp_dotted_quad(desiredaddr));
3134 p[2] = hisaddr >> 24;
3135 p[3] = hisaddr >> 16;
3136 p[4] = hisaddr >> 8;
3140 /* Add the option to nak'ed list. */
3147 * If we are about to conf-ack the request, but haven't seen
3148 * his address so far, gonna conf-nak it instead, with the
3149 * `address' option present and our idea of his address being
3150 * filled in there, to request negotiation of both addresses.
3152 * XXX This can result in an endless req - nak loop if peer
3153 * doesn't want to send us his address. Q: What should we do
3154 * about it? XXX A: implement the max-failure counter.
3156 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3157 buf[0] = IPCP_OPT_ADDRESS;
3159 buf[2] = hisaddr >> 24;
3160 buf[3] = hisaddr >> 16;
3161 buf[4] = hisaddr >> 8;
3165 log(-1, "still need hisaddr ");
3170 log(-1, " send conf-nak\n");
3171 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3174 log(-1, " send conf-ack\n");
3175 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3176 h->ident, origlen, h+1);
3179 kfree (buf, M_TEMP);
3188 * Analyze the IPCP Configure-Reject option list, and adjust our
3192 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3195 struct ifnet *ifp = &sp->pp_if;
3196 int debug = ifp->if_flags & IFF_DEBUG;
3199 buf = kmalloc (len, M_TEMP, M_INTWAIT);
3202 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3206 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3207 /* Sanity check option length */
3209 /* XXX should we just RXJ? */
3210 log(-1, "%s: malicious IPCP option received, dropping\n",
3215 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3217 case IPCP_OPT_COMPRESSION:
3218 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3220 case IPCP_OPT_ADDRESS:
3222 * Peer doesn't grok address option. This is
3223 * bad. XXX Should we better give up here?
3224 * XXX We could try old "addresses" option...
3226 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3233 kfree (buf, M_TEMP);
3238 * Analyze the IPCP Configure-NAK option list, and adjust our
3242 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3245 struct ifnet *ifp = &sp->pp_if;
3246 int debug = ifp->if_flags & IFF_DEBUG;
3251 buf = kmalloc (len, M_TEMP, M_INTWAIT);
3254 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3258 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3259 /* Sanity check option length */
3261 /* XXX should we just RXJ? */
3262 log(-1, "%s: malicious IPCP option received, dropping\n",
3267 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3269 case IPCP_OPT_COMPRESSION:
3270 if (len >= 6 && p[1] == 6) {
3271 desiredcomp = p[2] << 8 | p[3];
3273 log(-1, "[wantcomp %#04x] ",
3275 if (desiredcomp == IPCP_COMP_VJ) {
3276 sl_compress_init(sp->pp_comp, p[4]);
3277 sp->ipcp.max_state = p[4];
3278 sp->ipcp.compress_cid = p[5];
3280 log(-1, "[agree] ");
3283 ~(1 << IPCP_OPT_COMPRESSION);
3286 case IPCP_OPT_ADDRESS:
3288 * Peer doesn't like our local IP address. See
3289 * if we can do something for him. We'll drop
3290 * him our address then.
3292 if (len >= 6 && p[1] == 6) {
3293 wantaddr = p[2] << 24 | p[3] << 16 |
3295 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3297 log(-1, "[wantaddr %s] ",
3298 sppp_dotted_quad(wantaddr));
3300 * When doing dynamic address assignment,
3301 * we accept his offer. Otherwise, we
3302 * ignore it and thus continue to negotiate
3303 * our already existing value.
3304 * XXX: Bogus, if he said no once, he'll
3305 * just say no again, might as well die.
3307 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3308 sppp_set_ip_addr(sp, wantaddr);
3310 log(-1, "[agree] ");
3311 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3319 kfree (buf, M_TEMP);
3324 sppp_ipcp_tlu(struct sppp *sp)
3326 /* we are up - notify isdn daemon */
3332 sppp_ipcp_tld(struct sppp *sp)
3337 sppp_ipcp_tls(struct sppp *sp)
3339 /* indicate to LCP that it must stay alive */
3340 sp->lcp.protos |= (1 << IDX_IPCP);
3344 sppp_ipcp_tlf(struct sppp *sp)
3346 /* we no longer need LCP */
3347 sp->lcp.protos &= ~(1 << IDX_IPCP);
3348 sppp_lcp_check_and_close(sp);
3352 sppp_ipcp_scr(struct sppp *sp)
3354 char opt[6 /* compression */ + 6 /* address */];
3358 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3359 opt[i++] = IPCP_OPT_COMPRESSION;
3361 opt[i++] = IPCP_COMP_VJ >> 8;
3362 opt[i++] = IPCP_COMP_VJ;
3363 opt[i++] = sp->ipcp.max_state;
3364 opt[i++] = sp->ipcp.compress_cid;
3366 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3367 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3368 opt[i++] = IPCP_OPT_ADDRESS;
3370 opt[i++] = ouraddr >> 24;
3371 opt[i++] = ouraddr >> 16;
3372 opt[i++] = ouraddr >> 8;
3376 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3377 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3381 *--------------------------------------------------------------------------*
3383 * The IPv6CP implementation. *
3385 *--------------------------------------------------------------------------*
3390 sppp_ipv6cp_init(struct sppp *sp)
3392 sp->ipv6cp.opts = 0;
3393 sp->ipv6cp.flags = 0;
3394 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3395 sp->fail_counter[IDX_IPV6CP] = 0;
3396 sp->pp_seq[IDX_IPV6CP] = 0;
3397 sp->pp_rseq[IDX_IPV6CP] = 0;
3398 #if defined(__NetBSD__)
3399 callout_init(&sp->ch[IDX_IPV6CP]);
3401 #if defined(__DragonFly__)
3402 callout_init(&sp->timeout[IDX_IPV6CP]);
3407 sppp_ipv6cp_up(struct sppp *sp)
3409 sppp_up_event(&ipv6cp, sp);
3413 sppp_ipv6cp_down(struct sppp *sp)
3415 sppp_down_event(&ipv6cp, sp);
3419 sppp_ipv6cp_open(struct sppp *sp)
3422 struct in6_addr myaddr, hisaddr;
3424 #ifdef IPV6CP_MYIFID_DYN
3425 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3427 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3430 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3432 * If we don't have our address, this probably means our
3433 * interface doesn't want to talk IPv6 at all. (This could
3434 * be the case if somebody wants to speak only IPX, for
3435 * example.) Don't open IPv6CP in this case.
3437 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3438 /* XXX this message should go away */
3440 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3445 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3446 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3447 sppp_open_event(&ipv6cp, sp);
3451 sppp_ipv6cp_close(struct sppp *sp)
3453 sppp_close_event(&ipv6cp, sp);
3457 sppp_ipv6cp_TO(void *cookie)
3459 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3463 * Analyze a configure request. Return true if it was agreeable, and
3464 * caused action sca, false if it has been rejected or nak'ed, and
3465 * caused action scn. (The return value is used to make the state
3466 * transition decision in the state automaton.)
3469 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3471 u_char *buf, *r, *p;
3472 struct ifnet *ifp = &sp->pp_if;
3473 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3474 struct in6_addr myaddr, desiredaddr, suggestaddr;
3477 int collision, nohisaddr;
3482 * Make sure to allocate a buf that can at least hold a
3483 * conf-nak with an `address' option. We might need it below.
3485 buf = r = kmalloc ((len < 6? 6: len), M_TEMP, M_INTWAIT);
3487 /* pass 1: see if we can recognize them */
3489 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3493 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3494 /* Sanity check option length */
3497 log(-1, "%s: received malicious IPCPv6 option, "
3498 "dropping\n", ifp->if_xname);
3502 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3504 case IPV6CP_OPT_IFID:
3505 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3506 /* correctly formed address option */
3511 log(-1, " [invalid]");
3514 case IPV6CP_OPT_COMPRESSION:
3515 if (len >= 4 && p[1] >= 4) {
3516 /* correctly formed compress option */
3520 log(-1, " [invalid]");
3524 /* Others not supported. */
3529 /* Add the option to rejected list. */
3536 log(-1, " send conf-rej\n");
3537 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3542 /* pass 2: parse option values */
3543 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3545 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3550 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3552 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3555 case IPV6CP_OPT_COMPRESSION:
3558 case IPV6CP_OPT_IFID:
3559 bzero(&desiredaddr, sizeof(desiredaddr));
3560 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3561 collision = (bcmp(&desiredaddr.s6_addr[8],
3562 &myaddr.s6_addr[8], 8) == 0);
3563 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3565 desiredaddr.s6_addr16[0] = htons(0xfe80);
3566 desiredaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3568 if (!collision && !nohisaddr) {
3569 /* no collision, hisaddr known - Conf-Ack */
3574 ip6_sprintf(&desiredaddr),
3575 sppp_cp_type_name(type));
3580 bzero(&suggestaddr, sizeof(&suggestaddr));
3581 if (collision && nohisaddr) {
3582 /* collision, hisaddr unknown - Conf-Rej */
3587 * - no collision, hisaddr unknown, or
3588 * - collision, hisaddr known
3589 * Conf-Nak, suggest hisaddr
3592 sppp_suggest_ip6_addr(sp, &suggestaddr);
3593 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3596 log(-1, " %s [%s]", ip6_sprintf(&desiredaddr),
3597 sppp_cp_type_name(type));
3600 /* Add the option to nak'ed list. */
3606 if (rlen == 0 && type == CONF_ACK) {
3608 log(-1, " send %s\n", sppp_cp_type_name(type));
3609 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3612 if (type == CONF_ACK)
3613 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3617 log(-1, " send %s suggest %s\n",
3618 sppp_cp_type_name(type), ip6_sprintf(&suggestaddr));
3620 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3624 kfree (buf, M_TEMP);
3633 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3637 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3640 struct ifnet *ifp = &sp->pp_if;
3641 int debug = ifp->if_flags & IFF_DEBUG;
3644 buf = kmalloc (len, M_TEMP, M_INTWAIT);
3647 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3651 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3654 log(-1, "%s: received malicious IPCPv6 option, "
3655 "dropping\n", ifp->if_xname);
3659 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3661 case IPV6CP_OPT_IFID:
3663 * Peer doesn't grok address option. This is
3664 * bad. XXX Should we better give up here?
3666 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3669 case IPV6CP_OPT_COMPRESS:
3670 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3678 kfree (buf, M_TEMP);
3683 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3687 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3690 struct ifnet *ifp = &sp->pp_if;
3691 int debug = ifp->if_flags & IFF_DEBUG;
3692 struct in6_addr suggestaddr;
3695 buf = kmalloc (len, M_TEMP, M_INTWAIT);
3698 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3702 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3705 log(-1, "%s: received malicious IPCPv6 option, "
3706 "dropping\n", ifp->if_xname);
3710 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3712 case IPV6CP_OPT_IFID:
3714 * Peer doesn't like our local ifid. See
3715 * if we can do something for him. We'll drop
3716 * him our address then.
3718 if (len < 10 || p[1] != 10)
3720 bzero(&suggestaddr, sizeof(suggestaddr));
3721 suggestaddr.s6_addr16[0] = htons(0xfe80);
3722 suggestaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3723 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3725 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3727 log(-1, " [suggestaddr %s]",
3728 ip6_sprintf(&suggestaddr));
3729 #ifdef IPV6CP_MYIFID_DYN
3731 * When doing dynamic address assignment,
3732 * we accept his offer.
3734 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3735 struct in6_addr lastsuggest;
3737 * If <suggested myaddr from peer> equals to
3738 * <hisaddr we have suggested last time>,
3739 * we have a collision. generate new random
3742 sppp_suggest_ip6_addr(&lastsuggest);
3743 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3746 log(-1, " [random]");
3747 sppp_gen_ip6_addr(sp, &suggestaddr);
3749 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3751 log(-1, " [agree]");
3752 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3756 * Since we do not do dynamic address assignment,
3757 * we ignore it and thus continue to negotiate
3758 * our already existing value. This can possibly
3759 * go into infinite request-reject loop.
3761 * This is not likely because we normally use
3762 * ifid based on MAC-address.
3763 * If you have no ethernet card on the node, too bad.
3764 * XXX should we use fail_counter?
3769 case IPV6CP_OPT_COMPRESS:
3771 * Peer wants different compression parameters.
3780 kfree (buf, M_TEMP);
3784 sppp_ipv6cp_tlu(struct sppp *sp)
3786 /* we are up - notify isdn daemon */
3792 sppp_ipv6cp_tld(struct sppp *sp)
3797 sppp_ipv6cp_tls(struct sppp *sp)
3799 /* indicate to LCP that it must stay alive */
3800 sp->lcp.protos |= (1 << IDX_IPV6CP);
3804 sppp_ipv6cp_tlf(struct sppp *sp)
3807 #if 0 /* need #if 0 to close IPv6CP properly */
3808 /* we no longer need LCP */
3809 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3810 sppp_lcp_check_and_close(sp);
3815 sppp_ipv6cp_scr(struct sppp *sp)
3817 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3818 struct in6_addr ouraddr;
3821 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3822 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3823 opt[i++] = IPV6CP_OPT_IFID;
3825 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3830 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3831 opt[i++] = IPV6CP_OPT_COMPRESSION;
3833 opt[i++] = 0; /* TBD */
3834 opt[i++] = 0; /* TBD */
3835 /* variable length data may follow */
3839 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3840 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3844 sppp_ipv6cp_init(struct sppp *sp)
3849 sppp_ipv6cp_up(struct sppp *sp)
3854 sppp_ipv6cp_down(struct sppp *sp)
3860 sppp_ipv6cp_open(struct sppp *sp)
3865 sppp_ipv6cp_close(struct sppp *sp)
3870 sppp_ipv6cp_TO(void *sp)
3875 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3881 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3886 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3891 sppp_ipv6cp_tlu(struct sppp *sp)
3896 sppp_ipv6cp_tld(struct sppp *sp)
3901 sppp_ipv6cp_tls(struct sppp *sp)
3906 sppp_ipv6cp_tlf(struct sppp *sp)
3911 sppp_ipv6cp_scr(struct sppp *sp)
3917 *--------------------------------------------------------------------------*
3919 * The CHAP implementation. *
3921 *--------------------------------------------------------------------------*
3925 * The authentication protocols don't employ a full-fledged state machine as
3926 * the control protocols do, since they do have Open and Close events, but
3927 * not Up and Down, nor are they explicitly terminated. Also, use of the
3928 * authentication protocols may be different in both directions (this makes
3929 * sense, think of a machine that never accepts incoming calls but only
3930 * calls out, it doesn't require the called party to authenticate itself).
3932 * Our state machine for the local authentication protocol (we are requesting
3933 * the peer to authenticate) looks like:
3936 * +--------------------------------------------+
3938 * +--------+ Close +---------+ RCA+
3939 * | |<----------------------------------| |------+
3940 * +--->| Closed | TO* | Opened | sca |
3941 * | | |-----+ +-------| |<-----+
3942 * | +--------+ irc | | +---------+
3948 * | | +------->+ | |
3950 * | +--------+ V | |
3951 * | | |<----+<--------------------+ |
3957 * +------+ +------------------------------------------+
3958 * scn,tld sca,irc,ict,tlu
3963 * Open: LCP reached authentication phase
3964 * Close: LCP reached terminate phase
3966 * RCA+: received reply (pap-req, chap-response), acceptable
3967 * RCN: received reply (pap-req, chap-response), not acceptable
3968 * TO+: timeout with restart counter >= 0
3969 * TO-: timeout with restart counter < 0
3970 * TO*: reschedule timeout for CHAP
3972 * scr: send request packet (none for PAP, chap-challenge)
3973 * sca: send ack packet (pap-ack, chap-success)
3974 * scn: send nak packet (pap-nak, chap-failure)
3975 * ict: initialize re-challenge timer (CHAP only)
3977 * tlu: this-layer-up, LCP reaches network phase
3978 * tld: this-layer-down, LCP enters terminate phase
3980 * Note that in CHAP mode, after sending a new challenge, while the state
3981 * automaton falls back into Req-Sent state, it doesn't signal a tld
3982 * event to LCP, so LCP remains in network phase. Only after not getting
3983 * any response (or after getting an unacceptable response), CHAP closes,
3984 * causing LCP to enter terminate phase.
3986 * With PAP, there is no initial request that can be sent. The peer is
3987 * expected to send one based on the successful negotiation of PAP as
3988 * the authentication protocol during the LCP option negotiation.
3990 * Incoming authentication protocol requests (remote requests
3991 * authentication, we are peer) don't employ a state machine at all,
3992 * they are simply answered. Some peers [Ascend P50 firmware rev
3993 * 4.50] react allergically when sending IPCP requests while they are
3994 * still in authentication phase (thereby violating the standard that
3995 * demands that these NCP packets are to be discarded), so we keep
3996 * track of the peer demanding us to authenticate, and only proceed to
3997 * phase network once we've seen a positive acknowledge for the
4002 * Handle incoming CHAP packets.
4005 sppp_chap_input(struct sppp *sp, struct mbuf *m)
4008 struct lcp_header *h;
4010 u_char *value, *name, digest[AUTHKEYLEN], dsize;
4011 int value_len, name_len;
4014 len = m->m_pkthdr.len;
4018 SPP_FMT "chap invalid packet length: %d bytes\n",
4019 SPP_ARGS(ifp), len);
4022 h = mtod (m, struct lcp_header*);
4023 if (len > ntohs (h->len))
4024 len = ntohs (h->len);
4027 /* challenge, failure and success are his authproto */
4028 case CHAP_CHALLENGE:
4029 value = 1 + (u_char*)(h+1);
4030 value_len = value[-1];
4031 name = value + value_len;
4032 name_len = len - value_len - 5;
4036 SPP_FMT "chap corrupted challenge "
4037 "<%s id=0x%x len=%d",
4039 sppp_auth_type_name(PPP_CHAP, h->type),
4040 h->ident, ntohs(h->len));
4041 sppp_print_bytes((u_char*) (h+1), len-4);
4049 SPP_FMT "chap input <%s id=0x%x len=%d name=",
4051 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
4053 sppp_print_string((char*) name, name_len);
4054 log(-1, " value-size=%d value=", value_len);
4055 sppp_print_bytes(value, value_len);
4059 /* Compute reply value. */
4061 MD5Update(&ctx, &h->ident, 1);
4062 MD5Update(&ctx, sp->myauth.secret,
4063 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
4064 MD5Update(&ctx, value, value_len);
4065 MD5Final(digest, &ctx);
4066 dsize = sizeof digest;
4068 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
4069 sizeof dsize, (const char *)&dsize,
4070 sizeof digest, digest,
4071 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4078 log(LOG_DEBUG, SPP_FMT "chap success",
4082 sppp_print_string((char*)(h + 1), len - 4);
4089 sp->pp_flags &= ~PP_NEEDAUTH;
4090 if (sp->myauth.proto == PPP_CHAP &&
4091 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4092 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
4094 * We are authenticator for CHAP but didn't
4095 * complete yet. Leave it to tlu to proceed
4102 sppp_phase_network(sp);
4107 log(LOG_INFO, SPP_FMT "chap failure",
4111 sppp_print_string((char*)(h + 1), len - 4);
4115 log(LOG_INFO, SPP_FMT "chap failure\n",
4117 /* await LCP shutdown by authenticator */
4120 /* response is my authproto */
4122 value = 1 + (u_char*)(h+1);
4123 value_len = value[-1];
4124 name = value + value_len;
4125 name_len = len - value_len - 5;
4129 SPP_FMT "chap corrupted response "
4130 "<%s id=0x%x len=%d",
4132 sppp_auth_type_name(PPP_CHAP, h->type),
4133 h->ident, ntohs(h->len));
4134 sppp_print_bytes((u_char*)(h+1), len-4);
4139 if (h->ident != sp->confid[IDX_CHAP]) {
4142 SPP_FMT "chap dropping response for old ID "
4143 "(got %d, expected %d)\n",
4145 h->ident, sp->confid[IDX_CHAP]);
4148 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4149 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4150 log(LOG_INFO, SPP_FMT "chap response, his name ",
4152 sppp_print_string(name, name_len);
4153 log(-1, " != expected ");
4154 sppp_print_string(sp->hisauth.name,
4155 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4159 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4160 "<%s id=0x%x len=%d name=",
4162 sppp_state_name(sp->state[IDX_CHAP]),
4163 sppp_auth_type_name(PPP_CHAP, h->type),
4164 h->ident, ntohs (h->len));
4165 sppp_print_string((char*)name, name_len);
4166 log(-1, " value-size=%d value=", value_len);
4167 sppp_print_bytes(value, value_len);
4170 if (value_len != AUTHKEYLEN) {
4173 SPP_FMT "chap bad hash value length: "
4174 "%d bytes, should be %d\n",
4175 SPP_ARGS(ifp), value_len,
4181 MD5Update(&ctx, &h->ident, 1);
4182 MD5Update(&ctx, sp->hisauth.secret,
4183 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4184 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4185 MD5Final(digest, &ctx);
4187 #define FAILMSG "Failed..."
4188 #define SUCCMSG "Welcome!"
4190 if (value_len != sizeof digest ||
4191 bcmp(digest, value, value_len) != 0) {
4192 /* action scn, tld */
4193 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4194 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4199 /* action sca, perhaps tlu */
4200 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4201 sp->state[IDX_CHAP] == STATE_OPENED)
4202 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4203 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4205 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4206 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4212 /* Unknown CHAP packet type -- ignore. */
4214 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4215 "<0x%x id=0x%xh len=%d",
4217 sppp_state_name(sp->state[IDX_CHAP]),
4218 h->type, h->ident, ntohs(h->len));
4219 sppp_print_bytes((u_char*)(h+1), len-4);
4228 sppp_chap_init(struct sppp *sp)
4230 /* Chap doesn't have STATE_INITIAL at all. */
4231 sp->state[IDX_CHAP] = STATE_CLOSED;
4232 sp->fail_counter[IDX_CHAP] = 0;
4233 sp->pp_seq[IDX_CHAP] = 0;
4234 sp->pp_rseq[IDX_CHAP] = 0;
4235 #if defined(__DragonFly__)
4236 callout_init(&sp->timeout[IDX_CHAP]);
4241 sppp_chap_open(struct sppp *sp)
4243 if (sp->myauth.proto == PPP_CHAP &&
4244 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4245 /* we are authenticator for CHAP, start it */
4247 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4248 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4250 /* nothing to be done if we are peer, await a challenge */
4254 sppp_chap_close(struct sppp *sp)
4256 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4257 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4261 sppp_chap_TO(void *cookie)
4263 struct sppp *sp = (struct sppp *)cookie;
4269 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4271 sppp_state_name(sp->state[IDX_CHAP]),
4272 sp->rst_counter[IDX_CHAP]);
4274 if (--sp->rst_counter[IDX_CHAP] < 0)
4276 switch (sp->state[IDX_CHAP]) {
4277 case STATE_REQ_SENT:
4279 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4283 /* TO+ (or TO*) event */
4284 switch (sp->state[IDX_CHAP]) {
4287 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4289 case STATE_REQ_SENT:
4291 /* sppp_cp_change_state() will restart the timer */
4292 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4300 sppp_chap_tlu(struct sppp *sp)
4306 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4309 * Some broken CHAP implementations (Conware CoNet, firmware
4310 * 4.0.?) don't want to re-authenticate their CHAP once the
4311 * initial challenge-response exchange has taken place.
4312 * Provide for an option to avoid rechallenges.
4314 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4316 * Compute the re-challenge timeout. This will yield
4317 * a number between 300 and 810 seconds.
4319 i = 300 + ((unsigned)(krandom() & 0xff00) >> 7);
4320 callout_reset(&sp->timeout[IDX_CHAP], i * hz, chap.TO, sp);
4325 SPP_FMT "chap %s, ",
4327 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4328 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4329 log(-1, "next re-challenge in %d seconds\n", i);
4331 log(-1, "re-challenging suppressed\n");
4336 /* indicate to LCP that we need to be closed down */
4337 sp->lcp.protos |= (1 << IDX_CHAP);
4339 if (sp->pp_flags & PP_NEEDAUTH) {
4341 * Remote is authenticator, but his auth proto didn't
4342 * complete yet. Defer the transition to network
4352 * If we are already in phase network, we are done here. This
4353 * is the case if this is a dummy tlu event after a re-challenge.
4355 if (sp->pp_phase != PHASE_NETWORK)
4356 sppp_phase_network(sp);
4360 sppp_chap_tld(struct sppp *sp)
4365 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4366 callout_stop(&sp->timeout[IDX_CHAP]);
4367 sp->lcp.protos &= ~(1 << IDX_CHAP);
4373 sppp_chap_scr(struct sppp *sp)
4378 /* Compute random challenge. */
4379 ch = (u_long *)sp->myauth.challenge;
4380 #if defined(__DragonFly__)
4381 read_random(&seed, sizeof seed);
4386 seed = tv.tv_sec ^ tv.tv_usec;
4389 ch[0] = seed ^ krandom();
4390 ch[1] = seed ^ krandom();
4391 ch[2] = seed ^ krandom();
4392 ch[3] = seed ^ krandom();
4395 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4397 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4398 sizeof clen, (const char *)&clen,
4399 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4400 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4406 *--------------------------------------------------------------------------*
4408 * The PAP implementation. *
4410 *--------------------------------------------------------------------------*
4413 * For PAP, we need to keep a little state also if we are the peer, not the
4414 * authenticator. This is since we don't get a request to authenticate, but
4415 * have to repeatedly authenticate ourself until we got a response (or the
4416 * retry counter is expired).
4420 * Handle incoming PAP packets. */
4422 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4425 struct lcp_header *h;
4427 u_char *name, *passwd, mlen;
4428 int name_len, passwd_len;
4431 * Malicious input might leave this uninitialized, so
4432 * init to an impossible value.
4436 len = m->m_pkthdr.len;
4440 SPP_FMT "pap invalid packet length: %d bytes\n",
4441 SPP_ARGS(ifp), len);
4444 h = mtod (m, struct lcp_header*);
4445 if (len > ntohs (h->len))
4446 len = ntohs (h->len);
4448 /* PAP request is my authproto */
4450 name = 1 + (u_char*)(h+1);
4451 name_len = name[-1];
4452 passwd = name + name_len + 1;
4453 if (name_len > len - 6 ||
4454 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4456 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4457 "<%s id=0x%x len=%d",
4459 sppp_auth_type_name(PPP_PAP, h->type),
4460 h->ident, ntohs(h->len));
4461 sppp_print_bytes((u_char*)(h+1), len-4);
4467 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4468 "<%s id=0x%x len=%d name=",
4470 sppp_state_name(sp->state[IDX_PAP]),
4471 sppp_auth_type_name(PPP_PAP, h->type),
4472 h->ident, ntohs(h->len));
4473 sppp_print_string((char*)name, name_len);
4474 log(-1, " passwd=");
4475 sppp_print_string((char*)passwd, passwd_len);
4478 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4479 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4480 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4481 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4482 /* action scn, tld */
4483 mlen = sizeof(FAILMSG) - 1;
4484 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4485 sizeof mlen, (const char *)&mlen,
4486 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4491 /* action sca, perhaps tlu */
4492 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4493 sp->state[IDX_PAP] == STATE_OPENED) {
4494 mlen = sizeof(SUCCMSG) - 1;
4495 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4496 sizeof mlen, (const char *)&mlen,
4497 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4500 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4501 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4506 /* ack and nak are his authproto */
4508 callout_stop(&sp->pap_my_to);
4510 log(LOG_DEBUG, SPP_FMT "pap success",
4512 name = 1 + (u_char *)(h + 1);
4513 name_len = name[-1];
4514 if (len > 5 && name_len < len+4) {
4516 sppp_print_string(name, name_len);
4523 sp->pp_flags &= ~PP_NEEDAUTH;
4524 if (sp->myauth.proto == PPP_PAP &&
4525 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4526 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4528 * We are authenticator for PAP but didn't
4529 * complete yet. Leave it to tlu to proceed
4540 sppp_phase_network(sp);
4544 callout_stop(&sp->pap_my_to);
4546 log(LOG_INFO, SPP_FMT "pap failure",
4548 name = 1 + (u_char *)(h + 1);
4549 name_len = name[-1];
4550 if (len > 5 && name_len < len+4) {
4552 sppp_print_string(name, name_len);
4556 log(LOG_INFO, SPP_FMT "pap failure\n",
4558 /* await LCP shutdown by authenticator */
4562 /* Unknown PAP packet type -- ignore. */
4564 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4565 "<0x%x id=0x%x len=%d",
4567 h->type, h->ident, ntohs(h->len));
4568 sppp_print_bytes((u_char*)(h+1), len-4);
4577 sppp_pap_init(struct sppp *sp)
4579 /* PAP doesn't have STATE_INITIAL at all. */
4580 sp->state[IDX_PAP] = STATE_CLOSED;
4581 sp->fail_counter[IDX_PAP] = 0;
4582 sp->pp_seq[IDX_PAP] = 0;
4583 sp->pp_rseq[IDX_PAP] = 0;
4584 #if defined(__DragonFly__)
4585 callout_init(&sp->timeout[IDX_PAP]);
4586 callout_init(&sp->pap_my_to);
4591 sppp_pap_open(struct sppp *sp)
4593 if (sp->hisauth.proto == PPP_PAP &&
4594 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4595 /* we are authenticator for PAP, start our timer */
4596 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4597 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4599 if (sp->myauth.proto == PPP_PAP) {
4600 /* we are peer, send a request, and start a timer */
4602 callout_reset(&sp->pap_my_to, sp->lcp.timeout,
4603 sppp_pap_my_TO, sp);
4608 sppp_pap_close(struct sppp *sp)
4610 if (sp->state[IDX_PAP] != STATE_CLOSED)
4611 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4615 * That's the timeout routine if we are authenticator. Since the
4616 * authenticator is basically passive in PAP, we can't do much here.
4619 sppp_pap_TO(void *cookie)
4621 struct sppp *sp = (struct sppp *)cookie;
4627 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4629 sppp_state_name(sp->state[IDX_PAP]),
4630 sp->rst_counter[IDX_PAP]);
4632 if (--sp->rst_counter[IDX_PAP] < 0)
4634 switch (sp->state[IDX_PAP]) {
4635 case STATE_REQ_SENT:
4637 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4641 /* TO+ event, not very much we could do */
4642 switch (sp->state[IDX_PAP]) {
4643 case STATE_REQ_SENT:
4644 /* sppp_cp_change_state() will restart the timer */
4645 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4653 * That's the timeout handler if we are peer. Since the peer is active,
4654 * we need to retransmit our PAP request since it is apparently lost.
4655 * XXX We should impose a max counter.
4658 sppp_pap_my_TO(void *cookie)
4660 struct sppp *sp = (struct sppp *)cookie;
4664 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4671 sppp_pap_tlu(struct sppp *sp)
4675 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4678 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4679 SPP_ARGS(ifp), pap.name);
4683 /* indicate to LCP that we need to be closed down */
4684 sp->lcp.protos |= (1 << IDX_PAP);
4686 if (sp->pp_flags & PP_NEEDAUTH) {
4688 * Remote is authenticator, but his auth proto didn't
4689 * complete yet. Defer the transition to network
4696 sppp_phase_network(sp);
4700 sppp_pap_tld(struct sppp *sp)
4705 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4706 callout_stop(&sp->timeout[IDX_PAP]);
4707 callout_stop(&sp->pap_my_to);
4708 sp->lcp.protos &= ~(1 << IDX_PAP);
4714 sppp_pap_scr(struct sppp *sp)
4716 u_char idlen, pwdlen;
4718 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4719 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4720 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4722 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4723 sizeof idlen, (const char *)&idlen,
4724 (size_t)idlen, sp->myauth.name,
4725 sizeof pwdlen, (const char *)&pwdlen,
4726 (size_t)pwdlen, sp->myauth.secret,
4731 * Random miscellaneous functions.
4735 * Send a PAP or CHAP proto packet.
4737 * Varadic function, each of the elements for the ellipsis is of type
4738 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4740 * NOTE: never declare variadic functions with types subject to type
4741 * promotion (i.e. u_char). This is asking for big trouble depending
4742 * on the architecture you are on...
4746 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4747 unsigned int type, unsigned int id,
4751 struct ppp_header *h;
4752 struct lcp_header *lh;
4760 MGETHDR (m, MB_DONTWAIT, MT_DATA);
4763 m->m_pkthdr.rcvif = 0;
4765 h = mtod (m, struct ppp_header*);
4766 h->address = PPP_ALLSTATIONS; /* broadcast address */
4767 h->control = PPP_UI; /* Unnumbered Info */
4768 h->protocol = htons(cp->proto);
4770 lh = (struct lcp_header*)(h + 1);
4773 p = (u_char*) (lh+1);
4778 while ((mlen = (unsigned int)__va_arg(ap, size_t)) != 0) {
4779 msg = __va_arg(ap, const char *);
4781 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4787 bcopy(msg, p, mlen);
4792 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4793 lh->len = htons (LCP_HEADER_LEN + len);
4796 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4797 SPP_ARGS(ifp), cp->name,
4798 sppp_auth_type_name(cp->proto, lh->type),
4799 lh->ident, ntohs(lh->len));
4800 sppp_print_bytes((u_char*) (lh+1), len);
4803 if (IF_QFULL (&sp->pp_cpq)) {
4804 IF_DROP (&sp->pp_fastq);
4805 IF_DROP (&ifp->if_snd);
4809 IF_ENQUEUE (&sp->pp_cpq, m);
4810 if (! (ifp->if_flags & IFF_OACTIVE))
4811 (*ifp->if_start) (ifp);
4812 ifp->if_obytes += m->m_pkthdr.len + 3;
4816 * Send keepalive packets, every 10 seconds.
4819 sppp_keepalive(void *dummy)
4825 for (sp=spppq; sp; sp=sp->pp_next) {
4826 struct ifnet *ifp = &sp->pp_if;
4828 /* Keepalive mode disabled or channel down? */
4829 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4830 ! (ifp->if_flags & IFF_RUNNING))
4833 /* No keepalive in PPP mode if LCP not opened yet. */
4834 if (sp->pp_mode != IFF_CISCO &&
4835 sp->pp_phase < PHASE_AUTHENTICATE)
4838 if (sp->pp_alivecnt == MAXALIVECNT) {
4839 /* No keepalive packets got. Stop the interface. */
4840 kprintf (SPP_FMT "down\n", SPP_ARGS(ifp));
4842 IF_DRAIN(&sp->pp_cpq);
4843 if (sp->pp_mode != IFF_CISCO) {
4845 /* Shut down the PPP link. */
4847 /* Initiate negotiation. XXX */
4851 ifnet_serialize_all(ifp);
4852 if (sp->pp_alivecnt <= MAXALIVECNT)
4854 if (sp->pp_mode == IFF_CISCO)
4855 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4856 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4857 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4858 long nmagic = htonl (sp->lcp.magic);
4859 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4860 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4861 sp->lcp.echoid, 4, &nmagic);
4863 ifnet_deserialize_all(ifp);
4865 callout_reset(&keepalive_timeout, hz * 10, sppp_keepalive, NULL);
4870 * Get both IP addresses.
4873 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4875 struct ifnet *ifp = &sp->pp_if;
4876 struct ifaddr_container *ifac;
4878 struct sockaddr_in *si, *sm;
4884 * Pick the first AF_INET address from the list,
4885 * aliases don't make any sense on a p2p link anyway.
4888 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
4890 if (ifa->ifa_addr->sa_family == AF_INET) {
4891 si = (struct sockaddr_in *)ifa->ifa_addr;
4892 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4898 if (si && si->sin_addr.s_addr) {
4899 ssrc = si->sin_addr.s_addr;
4901 *srcmask = ntohl(sm->sin_addr.s_addr);
4904 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4905 if (si && si->sin_addr.s_addr)
4906 ddst = si->sin_addr.s_addr;
4909 if (dst) *dst = ntohl(ddst);
4910 if (src) *src = ntohl(ssrc);
4914 * Set my IP address. Must be called at splimp.
4917 sppp_set_ip_addr(struct sppp *sp, u_long src)
4920 struct ifaddr_container *ifac;
4921 struct ifaddr *ifa = NULL;
4922 struct sockaddr_in *si;
4923 struct in_ifaddr *ia;
4926 * Pick the first AF_INET address from the list,
4927 * aliases don't make any sense on a p2p link anyway.
4930 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
4932 if (ifa->ifa_addr->sa_family == AF_INET) {
4933 si = (struct sockaddr_in *)ifa->ifa_addr;
4939 if (ifac != NULL && si != NULL) {
4941 #if __NetBSD_Version__ >= 103080000
4942 struct sockaddr_in new_sin = *si;
4944 new_sin.sin_addr.s_addr = htonl(src);
4945 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 1);
4948 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: in_ifinit "
4949 " failed, error=%d\n", SPP_ARGS(ifp), error);
4952 /* delete old route */
4953 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4956 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4957 SPP_ARGS(ifp), error);
4961 in_iahash_remove(ia);
4963 /* set new address */
4964 si->sin_addr.s_addr = htonl(src);
4965 in_iahash_insert(ia);
4968 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
4971 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
4972 SPP_ARGS(ifp), error);
4980 * Get both IPv6 addresses.
4983 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4984 struct in6_addr *srcmask)
4986 struct ifnet *ifp = &sp->pp_if;
4987 struct ifaddr_container *ifac;
4989 struct sockaddr_in6 *si, *sm;
4990 struct in6_addr ssrc, ddst;
4993 bzero(&ssrc, sizeof(ssrc));
4994 bzero(&ddst, sizeof(ddst));
4996 * Pick the first link-local AF_INET6 address from the list,
4997 * aliases don't make any sense on a p2p link anyway.
5000 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
5002 if (ifa->ifa_addr->sa_family == AF_INET6) {
5003 si = (struct sockaddr_in6 *)ifa->ifa_addr;
5004 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
5005 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
5010 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
5011 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
5013 bcopy(&sm->sin6_addr, srcmask,
5018 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
5019 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
5020 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
5024 bcopy(&ddst, dst, sizeof(*dst));
5026 bcopy(&ssrc, src, sizeof(*src));
5029 #ifdef IPV6CP_MYIFID_DYN
5031 * Generate random ifid.
5034 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
5040 * Set my IPv6 address. Must be called at splimp.
5043 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
5046 struct ifaddr_container *ifac;
5048 struct sockaddr_in6 *sin6;
5051 * Pick the first link-local AF_INET6 address from the list,
5052 * aliases don't make any sense on a p2p link anyway.
5056 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
5058 if (ifa->ifa_addr->sa_family == AF_INET6) {
5059 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
5060 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
5065 if (ifac != NULL && sin6 != NULL) {
5067 struct sockaddr_in6 new_sin6 = *sin6;
5069 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
5070 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5071 if (debug && error) {
5072 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
5073 " failed, error=%d\n", SPP_ARGS(ifp), error);
5080 * Suggest a candidate address to be used by peer.
5083 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5085 struct in6_addr myaddr;
5088 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5090 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5092 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5093 myaddr.s6_addr[14] ^= 0xff;
5094 myaddr.s6_addr[15] ^= 0xff;
5096 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5097 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5100 bcopy(&myaddr, suggest, sizeof(myaddr));
5105 sppp_params(struct sppp *sp, u_long cmd, void *data)
5108 struct ifreq *ifr = (struct ifreq *)data;
5109 struct spppreq *spr;
5112 spr = kmalloc(sizeof(struct spppreq), M_TEMP, M_INTWAIT);
5115 * ifr->ifr_data is supposed to point to a struct spppreq.
5116 * Check the cmd word first before attempting to fetch all the
5119 if ((subcmd = fuword(ifr->ifr_data)) == -1) {
5124 if (copyin((caddr_t)ifr->ifr_data, spr, sizeof(struct spppreq)) != 0) {
5130 case (int)SPPPIOGDEFS:
5131 if (cmd != SIOCGIFGENERIC) {
5136 * We copy over the entire current state, but clean
5137 * out some of the stuff we don't wanna pass up.
5138 * Remember, SIOCGIFGENERIC is unprotected, and can be
5139 * called by any user. No need to ever get PAP or
5140 * CHAP secrets back to userland anyway.
5142 spr->defs.pp_phase = sp->pp_phase;
5143 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5144 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5145 spr->defs.lcp = sp->lcp;
5146 spr->defs.ipcp = sp->ipcp;
5147 spr->defs.ipv6cp = sp->ipv6cp;
5148 spr->defs.myauth = sp->myauth;
5149 spr->defs.hisauth = sp->hisauth;
5150 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5151 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5152 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5153 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5155 * Fixup the LCP timeout value to milliseconds so
5156 * spppcontrol doesn't need to bother about the value
5157 * of "hz". We do the reverse calculation below when
5160 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5161 rv = copyout(spr, (caddr_t)ifr->ifr_data,
5162 sizeof(struct spppreq));
5165 case (int)SPPPIOSDEFS:
5166 if (cmd != SIOCSIFGENERIC) {
5171 * We have a very specific idea of which fields we
5172 * allow being passed back from userland, so to not
5173 * clobber our current state. For one, we only allow
5174 * setting anything if LCP is in dead or establish
5175 * phase. Once the authentication negotiations
5176 * started, the authentication settings must not be
5177 * changed again. (The administrator can force an
5178 * ifconfig down in order to get LCP back into dead
5181 * Also, we only allow for authentication parameters to be
5184 * XXX Should allow to set or clear pp_flags.
5186 * Finally, if the respective authentication protocol to
5187 * be used is set differently than 0, but the secret is
5188 * passed as all zeros, we don't trash the existing secret.
5189 * This allows an administrator to change the system name
5190 * only without clobbering the secret (which he didn't get
5191 * back in a previous SPPPIOGDEFS call). However, the
5192 * secrets are cleared if the authentication protocol is
5194 if (sp->pp_phase != PHASE_DEAD &&
5195 sp->pp_phase != PHASE_ESTABLISH) {
5200 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5201 spr->defs.myauth.proto != PPP_CHAP) ||
5202 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5203 spr->defs.hisauth.proto != PPP_CHAP)) {
5208 if (spr->defs.myauth.proto == 0)
5209 /* resetting myauth */
5210 bzero(&sp->myauth, sizeof sp->myauth);
5212 /* setting/changing myauth */
5213 sp->myauth.proto = spr->defs.myauth.proto;
5214 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5215 if (spr->defs.myauth.secret[0] != '\0')
5216 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5219 if (spr->defs.hisauth.proto == 0)
5220 /* resetting hisauth */
5221 bzero(&sp->hisauth, sizeof sp->hisauth);
5223 /* setting/changing hisauth */
5224 sp->hisauth.proto = spr->defs.hisauth.proto;
5225 sp->hisauth.flags = spr->defs.hisauth.flags;
5226 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5227 if (spr->defs.hisauth.secret[0] != '\0')
5228 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5231 /* set LCP restart timer timeout */
5232 if (spr->defs.lcp.timeout != 0)
5233 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5234 /* set VJ enable and IPv6 disable flags */
5236 if (spr->defs.enable_vj)
5237 sp->confflags |= CONF_ENABLE_VJ;
5239 sp->confflags &= ~CONF_ENABLE_VJ;
5242 if (spr->defs.enable_ipv6)
5243 sp->confflags |= CONF_ENABLE_IPV6;
5245 sp->confflags &= ~CONF_ENABLE_IPV6;
5260 sppp_phase_network(struct sppp *sp)
5266 sp->pp_phase = PHASE_NETWORK;
5269 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5270 sppp_phase_name(sp->pp_phase));
5272 /* Notify NCPs now. */
5273 for (i = 0; i < IDX_COUNT; i++)
5274 if ((cps[i])->flags & CP_NCP)
5277 /* Send Up events to all NCPs. */
5278 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5279 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5282 /* if no NCP is starting, all this was in vain, close down */
5283 sppp_lcp_check_and_close(sp);
5288 sppp_cp_type_name(u_char type)
5290 static char buf[12];
5292 case CONF_REQ: return "conf-req";
5293 case CONF_ACK: return "conf-ack";
5294 case CONF_NAK: return "conf-nak";
5295 case CONF_REJ: return "conf-rej";
5296 case TERM_REQ: return "term-req";
5297 case TERM_ACK: return "term-ack";
5298 case CODE_REJ: return "code-rej";
5299 case PROTO_REJ: return "proto-rej";
5300 case ECHO_REQ: return "echo-req";
5301 case ECHO_REPLY: return "echo-reply";
5302 case DISC_REQ: return "discard-req";
5304 ksnprintf (buf, sizeof(buf), "cp/0x%x", type);
5309 sppp_auth_type_name(u_short proto, u_char type)
5311 static char buf[12];
5315 case CHAP_CHALLENGE: return "challenge";
5316 case CHAP_RESPONSE: return "response";
5317 case CHAP_SUCCESS: return "success";
5318 case CHAP_FAILURE: return "failure";
5322 case PAP_REQ: return "req";
5323 case PAP_ACK: return "ack";
5324 case PAP_NAK: return "nak";
5327 ksnprintf (buf, sizeof(buf), "auth/0x%x", type);
5332 sppp_lcp_opt_name(u_char opt)
5334 static char buf[12];
5336 case LCP_OPT_MRU: return "mru";
5337 case LCP_OPT_ASYNC_MAP: return "async-map";
5338 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5339 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5340 case LCP_OPT_MAGIC: return "magic";
5341 case LCP_OPT_PROTO_COMP: return "proto-comp";
5342 case LCP_OPT_ADDR_COMP: return "addr-comp";
5344 ksnprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5349 sppp_ipcp_opt_name(u_char opt)
5351 static char buf[12];
5353 case IPCP_OPT_ADDRESSES: return "addresses";
5354 case IPCP_OPT_COMPRESSION: return "compression";
5355 case IPCP_OPT_ADDRESS: return "address";
5357 ksnprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5363 sppp_ipv6cp_opt_name(u_char opt)
5365 static char buf[12];
5367 case IPV6CP_OPT_IFID: return "ifid";
5368 case IPV6CP_OPT_COMPRESSION: return "compression";
5370 ksprintf (buf, "0x%x", opt);
5376 sppp_state_name(int state)
5379 case STATE_INITIAL: return "initial";
5380 case STATE_STARTING: return "starting";
5381 case STATE_CLOSED: return "closed";
5382 case STATE_STOPPED: return "stopped";
5383 case STATE_CLOSING: return "closing";
5384 case STATE_STOPPING: return "stopping";
5385 case STATE_REQ_SENT: return "req-sent";
5386 case STATE_ACK_RCVD: return "ack-rcvd";
5387 case STATE_ACK_SENT: return "ack-sent";
5388 case STATE_OPENED: return "opened";
5394 sppp_phase_name(enum ppp_phase phase)
5397 case PHASE_DEAD: return "dead";
5398 case PHASE_ESTABLISH: return "establish";
5399 case PHASE_TERMINATE: return "terminate";
5400 case PHASE_AUTHENTICATE: return "authenticate";
5401 case PHASE_NETWORK: return "network";
5407 sppp_proto_name(u_short proto)
5409 static char buf[12];
5411 case PPP_LCP: return "lcp";
5412 case PPP_IPCP: return "ipcp";
5413 case PPP_PAP: return "pap";
5414 case PPP_CHAP: return "chap";
5415 case PPP_IPV6CP: return "ipv6cp";
5417 ksnprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5422 sppp_print_bytes(const u_char *p, u_short len)
5425 log(-1, " %*D", len, p, "-");
5429 sppp_print_string(const char *p, u_short len)
5436 * Print only ASCII chars directly. RFC 1994 recommends
5437 * using only them, but we don't rely on it. */
5438 if (c < ' ' || c > '~')
5439 log(-1, "\\x%x", c);
5446 sppp_dotted_quad(u_long addr)
5449 ksprintf(s, "%d.%d.%d.%d",
5450 (int)((addr >> 24) & 0xff),
5451 (int)((addr >> 16) & 0xff),
5452 (int)((addr >> 8) & 0xff),
5453 (int)(addr & 0xff));
5458 sppp_strnlen(u_char *p, int max)
5462 for (len = 0; len < max && *p; ++p)
5467 /* a dummy, used to drop uninteresting events */
5469 sppp_null(struct sppp *unused)
5471 /* do just nothing */