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.18 2004/09/16 14:56:32 joerg 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>
49 #include <sys/malloc.h>
52 #if defined (__OpenBSD__)
59 #include <net/netisr.h>
60 #include <net/if_types.h>
61 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <net/slcompress.h>
67 #if defined (__NetBSD__) || defined (__OpenBSD__)
68 #include <machine/cpu.h> /* XXX for softnet */
71 #include <machine/stdarg.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/in_var.h>
78 #include <netinet/ip.h>
79 #include <netinet/tcp.h>
82 #if defined (__DragonFly__) || defined (__OpenBSD__)
83 # include <netinet/if_ether.h>
85 # include <net/ethertypes.h>
89 #include <netproto/ipx/ipx.h>
90 #include <netproto/ipx/ipx_if.h>
95 #include <netns/ns_if.h>
100 #define IOCTL_CMD_T u_long
101 #define MAXALIVECNT 3 /* max. alive packets */
104 * Interface flags that can be set in an ifconfig command.
106 * Setting link0 will make the link passive, i.e. it will be marked
107 * as being administrative openable, but won't be opened to begin
108 * with. Incoming calls will be answered, or subsequent calls with
109 * -link1 will cause the administrative open of the LCP layer.
111 * Setting link1 will cause the link to auto-dial only as packets
114 * Setting IFF_DEBUG will syslog the option negotiation and state
115 * transitions at level kern.debug. Note: all logs consistently look
118 * <if-name><unit>: <proto-name> <additional info...>
120 * with <if-name><unit> being something like "bppp0", and <proto-name>
121 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
124 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
125 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
126 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
128 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
129 #define PPP_UI 0x03 /* Unnumbered Information */
130 #define PPP_IP 0x0021 /* Internet Protocol */
131 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
132 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
133 #define PPP_IPX 0x002b /* Novell IPX Protocol */
134 #define PPP_VJ_COMP 0x002d /* VJ compressed TCP/IP */
135 #define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */
136 #define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */
137 #define PPP_LCP 0xc021 /* Link Control Protocol */
138 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
139 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
140 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
141 #define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */
143 #define CONF_REQ 1 /* PPP configure request */
144 #define CONF_ACK 2 /* PPP configure acknowledge */
145 #define CONF_NAK 3 /* PPP configure negative ack */
146 #define CONF_REJ 4 /* PPP configure reject */
147 #define TERM_REQ 5 /* PPP terminate request */
148 #define TERM_ACK 6 /* PPP terminate acknowledge */
149 #define CODE_REJ 7 /* PPP code reject */
150 #define PROTO_REJ 8 /* PPP protocol reject */
151 #define ECHO_REQ 9 /* PPP echo request */
152 #define ECHO_REPLY 10 /* PPP echo reply */
153 #define DISC_REQ 11 /* PPP discard request */
155 #define LCP_OPT_MRU 1 /* maximum receive unit */
156 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
157 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
158 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
159 #define LCP_OPT_MAGIC 5 /* magic number */
160 #define LCP_OPT_RESERVED 6 /* reserved */
161 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
162 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
164 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
165 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
166 #define IPCP_OPT_ADDRESS 3 /* local IP address */
168 #define IPV6CP_OPT_IFID 1 /* interface identifier */
169 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
171 #define IPCP_COMP_VJ 0x2d /* Code for VJ compression */
173 #define PAP_REQ 1 /* PAP name/password request */
174 #define PAP_ACK 2 /* PAP acknowledge */
175 #define PAP_NAK 3 /* PAP fail */
177 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
178 #define CHAP_RESPONSE 2 /* CHAP challenge response */
179 #define CHAP_SUCCESS 3 /* CHAP response ok */
180 #define CHAP_FAILURE 4 /* CHAP response failed */
182 #define CHAP_MD5 5 /* hash algorithm - MD5 */
184 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
185 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
186 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
187 #define CISCO_ADDR_REQ 0 /* Cisco address request */
188 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
189 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
191 /* states are named and numbered according to RFC 1661 */
192 #define STATE_INITIAL 0
193 #define STATE_STARTING 1
194 #define STATE_CLOSED 2
195 #define STATE_STOPPED 3
196 #define STATE_CLOSING 4
197 #define STATE_STOPPING 5
198 #define STATE_REQ_SENT 6
199 #define STATE_ACK_RCVD 7
200 #define STATE_ACK_SENT 8
201 #define STATE_OPENED 9
207 } __attribute__((__packed__));
208 #define PPP_HEADER_LEN sizeof (struct ppp_header)
214 } __attribute__((__packed__));
215 #define LCP_HEADER_LEN sizeof (struct lcp_header)
217 struct cisco_packet {
224 } __attribute__((__packed__));
225 #define CISCO_PACKET_LEN sizeof (struct cisco_packet)
228 * We follow the spelling and capitalization of RFC 1661 here, to make
229 * it easier comparing with the standard. Please refer to this RFC in
230 * case you can't make sense out of these abbreviation; it will also
231 * explain the semantics related to the various events and actions.
234 u_short proto; /* PPP control protocol number */
235 u_char protoidx; /* index into state table in struct sppp */
237 #define CP_LCP 0x01 /* this is the LCP */
238 #define CP_AUTH 0x02 /* this is an authentication protocol */
239 #define CP_NCP 0x04 /* this is a NCP */
240 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
241 const char *name; /* name of this control protocol */
243 void (*Up)(struct sppp *sp);
244 void (*Down)(struct sppp *sp);
245 void (*Open)(struct sppp *sp);
246 void (*Close)(struct sppp *sp);
247 void (*TO)(void *sp);
248 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
249 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
250 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
252 void (*tlu)(struct sppp *sp);
253 void (*tld)(struct sppp *sp);
254 void (*tls)(struct sppp *sp);
255 void (*tlf)(struct sppp *sp);
256 void (*scr)(struct sppp *sp);
259 static struct sppp *spppq;
260 #if defined(__DragonFly__)
261 static struct callout keepalive_timeout;
264 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 && !defined(__DragonFly__)
265 #define SPP_FMT "%s%d: "
266 #define SPP_ARGS(ifp) (ifp)->if_name, (ifp)->if_unit
268 #define SPP_FMT "%s: "
269 #define SPP_ARGS(ifp) (ifp)->if_xname
274 * The following disgusting hack gets around the problem that IP TOS
275 * can't be set yet. We want to put "interactive" traffic on a high
276 * priority queue. To decide if traffic is interactive, we check that
277 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
279 * XXX is this really still necessary? - joerg -
281 static u_short interactive_ports[8] = {
285 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
288 /* almost every function needs these */
290 struct ifnet *ifp = &sp->pp_if; \
291 int debug = ifp->if_flags & IFF_DEBUG
293 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
294 struct sockaddr *dst, struct rtentry *rt);
296 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
297 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
299 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
301 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
302 u_char ident, u_short len, void *data);
303 /* static void sppp_cp_timeout(void *arg); */
304 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
306 static void sppp_auth_send(const struct cp *cp,
307 struct sppp *sp, unsigned int type, unsigned int id,
310 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
311 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
312 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
313 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
314 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
316 static void sppp_null(struct sppp *sp);
318 static void sppp_lcp_init(struct sppp *sp);
319 static void sppp_lcp_up(struct sppp *sp);
320 static void sppp_lcp_down(struct sppp *sp);
321 static void sppp_lcp_open(struct sppp *sp);
322 static void sppp_lcp_close(struct sppp *sp);
323 static void sppp_lcp_TO(void *sp);
324 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
325 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
326 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
327 static void sppp_lcp_tlu(struct sppp *sp);
328 static void sppp_lcp_tld(struct sppp *sp);
329 static void sppp_lcp_tls(struct sppp *sp);
330 static void sppp_lcp_tlf(struct sppp *sp);
331 static void sppp_lcp_scr(struct sppp *sp);
332 static void sppp_lcp_check_and_close(struct sppp *sp);
333 static int sppp_ncp_check(struct sppp *sp);
335 static void sppp_ipcp_init(struct sppp *sp);
336 static void sppp_ipcp_up(struct sppp *sp);
337 static void sppp_ipcp_down(struct sppp *sp);
338 static void sppp_ipcp_open(struct sppp *sp);
339 static void sppp_ipcp_close(struct sppp *sp);
340 static void sppp_ipcp_TO(void *sp);
341 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
342 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
343 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
344 static void sppp_ipcp_tlu(struct sppp *sp);
345 static void sppp_ipcp_tld(struct sppp *sp);
346 static void sppp_ipcp_tls(struct sppp *sp);
347 static void sppp_ipcp_tlf(struct sppp *sp);
348 static void sppp_ipcp_scr(struct sppp *sp);
350 static void sppp_ipv6cp_init(struct sppp *sp);
351 static void sppp_ipv6cp_up(struct sppp *sp);
352 static void sppp_ipv6cp_down(struct sppp *sp);
353 static void sppp_ipv6cp_open(struct sppp *sp);
354 static void sppp_ipv6cp_close(struct sppp *sp);
355 static void sppp_ipv6cp_TO(void *sp);
356 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
357 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
358 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
359 static void sppp_ipv6cp_tlu(struct sppp *sp);
360 static void sppp_ipv6cp_tld(struct sppp *sp);
361 static void sppp_ipv6cp_tls(struct sppp *sp);
362 static void sppp_ipv6cp_tlf(struct sppp *sp);
363 static void sppp_ipv6cp_scr(struct sppp *sp);
365 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
366 static void sppp_pap_init(struct sppp *sp);
367 static void sppp_pap_open(struct sppp *sp);
368 static void sppp_pap_close(struct sppp *sp);
369 static void sppp_pap_TO(void *sp);
370 static void sppp_pap_my_TO(void *sp);
371 static void sppp_pap_tlu(struct sppp *sp);
372 static void sppp_pap_tld(struct sppp *sp);
373 static void sppp_pap_scr(struct sppp *sp);
375 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
376 static void sppp_chap_init(struct sppp *sp);
377 static void sppp_chap_open(struct sppp *sp);
378 static void sppp_chap_close(struct sppp *sp);
379 static void sppp_chap_TO(void *sp);
380 static void sppp_chap_tlu(struct sppp *sp);
381 static void sppp_chap_tld(struct sppp *sp);
382 static void sppp_chap_scr(struct sppp *sp);
384 static const char *sppp_auth_type_name(u_short proto, u_char type);
385 static const char *sppp_cp_type_name(u_char type);
386 static const char *sppp_dotted_quad(u_long addr);
387 static const char *sppp_ipcp_opt_name(u_char opt);
389 static const char *sppp_ipv6cp_opt_name(u_char opt);
391 static const char *sppp_lcp_opt_name(u_char opt);
392 static const char *sppp_phase_name(enum ppp_phase phase);
393 static const char *sppp_proto_name(u_short proto);
394 static const char *sppp_state_name(int state);
395 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
396 static int sppp_strnlen(u_char *p, int max);
397 static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst,
399 static void sppp_keepalive(void *dummy);
400 static void sppp_phase_network(struct sppp *sp);
401 static void sppp_print_bytes(const u_char *p, u_short len);
402 static void sppp_print_string(const char *p, u_short len);
403 static void sppp_qflush(struct ifqueue *ifq);
404 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
406 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
407 struct in6_addr *dst, struct in6_addr *srcmask);
408 #ifdef IPV6CP_MYIFID_DYN
409 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
410 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
412 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
415 /* our control protocol descriptors */
416 static const struct cp lcp = {
417 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
418 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
419 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
420 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
424 static const struct cp ipcp = {
425 PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
426 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
427 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
428 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
432 static const struct cp ipv6cp = {
433 PPP_IPV6CP, IDX_IPV6CP,
434 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
440 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
441 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
442 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
446 static const struct cp pap = {
447 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
448 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
449 sppp_pap_TO, 0, 0, 0,
450 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
454 static const struct cp chap = {
455 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
456 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
457 sppp_chap_TO, 0, 0, 0,
458 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
462 static const struct cp *cps[IDX_COUNT] = {
464 &ipcp, /* IDX_IPCP */
465 &ipv6cp, /* IDX_IPV6CP */
467 &chap, /* IDX_CHAP */
471 sppp_modevent(module_t mod, int type, void *unused)
484 static moduledata_t spppmod = {
489 MODULE_VERSION(sppp, 1);
490 DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
493 * Exported functions, comprising our interface to the lower layer.
497 * Process the received packet.
500 sppp_input(struct ifnet *ifp, struct mbuf *m)
502 struct ppp_header *h;
504 struct sppp *sp = (struct sppp *)ifp;
506 int hlen, vjlen, do_account = 0;
507 int debug = ifp->if_flags & IFF_DEBUG;
509 if (ifp->if_flags & IFF_UP)
510 /* Count received bytes, add FCS and one flag */
511 ifp->if_ibytes += m->m_pkthdr.len + 3;
513 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
514 /* Too small packet, drop it. */
517 SPP_FMT "input packet is too small, %d bytes\n",
518 SPP_ARGS(ifp), m->m_pkthdr.len);
527 /* Get PPP header. */
528 h = mtod (m, struct ppp_header*);
529 m_adj (m, PPP_HEADER_LEN);
531 switch (h->address) {
532 case PPP_ALLSTATIONS:
533 if (h->control != PPP_UI)
535 if (sp->pp_mode == IFF_CISCO) {
538 SPP_FMT "PPP packet in Cisco mode "
539 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
541 h->address, h->control, ntohs(h->protocol));
544 switch (ntohs (h->protocol)) {
548 SPP_FMT "rejecting protocol "
549 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
551 h->address, h->control, ntohs(h->protocol));
552 if (sp->state[IDX_LCP] == STATE_OPENED)
553 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
554 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
559 sppp_cp_input(&lcp, sp, m);
563 if (sp->pp_phase >= PHASE_AUTHENTICATE)
564 sppp_pap_input(sp, m);
568 if (sp->pp_phase >= PHASE_AUTHENTICATE)
569 sppp_chap_input(sp, m);
574 if (sp->pp_phase == PHASE_NETWORK)
575 sppp_cp_input(&ipcp, sp, m);
579 if (sp->state[IDX_IPCP] == STATE_OPENED) {
585 if (sp->state[IDX_IPCP] == STATE_OPENED) {
587 sl_uncompress_tcp_core(mtod(m, u_char *),
591 &iphdr, &hlen)) <= 0) {
594 SPP_FMT "VJ uncompress failed on compressed packet\n",
600 * Trim the VJ header off the packet, and prepend
601 * the uncompressed IP header (which will usually
602 * end up in two chained mbufs since there's not
603 * enough leading space in the existing mbuf).
606 M_PREPEND(m, hlen, MB_DONTWAIT);
609 bcopy(iphdr, mtod(m, u_char *), hlen);
616 if (sp->state[IDX_IPCP] == STATE_OPENED) {
617 if (sl_uncompress_tcp_core(mtod(m, u_char *),
619 TYPE_UNCOMPRESSED_TCP,
621 &iphdr, &hlen) != 0) {
624 SPP_FMT "VJ uncompress failed on uncompressed packet\n",
635 if (sp->pp_phase == PHASE_NETWORK)
636 sppp_cp_input(&ipv6cp, sp, m);
641 if (sp->state[IDX_IPV6CP] == STATE_OPENED) {
649 /* IPX IPXCP not implemented yet */
650 if (sp->pp_phase == PHASE_NETWORK) {
658 /* XNS IDPCP not implemented yet */
659 if (sp->pp_phase == PHASE_NETWORK) {
667 case CISCO_MULTICAST:
669 /* Don't check the control field here (RFC 1547). */
670 if (sp->pp_mode != IFF_CISCO) {
673 SPP_FMT "Cisco packet in PPP mode "
674 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
676 h->address, h->control, ntohs(h->protocol));
679 switch (ntohs (h->protocol)) {
683 case CISCO_KEEPALIVE:
684 sppp_cisco_input ((struct sppp*) ifp, m);
713 default: /* Invalid PPP packet. */
717 SPP_FMT "invalid input packet "
718 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
720 h->address, h->control, ntohs(h->protocol));
724 if (! (ifp->if_flags & IFF_UP) || isr < 0)
729 netisr_dispatch(isr, m);
732 * Do only account for network packets, not for control
733 * packets. This is used by some subsystems to detect
736 sp->pp_last_recv = time_second;
740 * Enqueue transmit packet.
743 sppp_output(struct ifnet *ifp, struct mbuf *m,
744 struct sockaddr *dst, struct rtentry *rt)
746 struct sppp *sp = (struct sppp*) ifp;
747 struct ppp_header *h;
748 struct ifqueue *ifq = NULL;
750 int ipproto = PPP_IP;
751 int debug = ifp->if_flags & IFF_DEBUG;
755 if ((ifp->if_flags & IFF_UP) == 0 ||
756 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
765 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
770 * Hack to prevent the initialization-time generated
771 * IPv6 multicast packet to erroneously cause a
772 * dialout event in case IPv6 has been
773 * administratively disabled on that interface.
775 if (dst->sa_family == AF_INET6 &&
776 !(sp->confflags & CONF_ENABLE_IPV6))
780 * Interface is not yet running, but auto-dial. Need
781 * to start LCP for it.
783 ifp->if_flags |= IFF_RUNNING;
791 if (dst->sa_family == AF_INET) {
792 /* XXX Check mbuf length here? */
793 struct ip *ip = mtod (m, struct ip*);
794 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
797 * When using dynamic local IP address assignment by using
798 * 0.0.0.0 as a local address, the first TCP session will
799 * not connect because the local TCP checksum is computed
800 * using 0.0.0.0 which will later become our real IP address
801 * so the TCP checksum computed at the remote end will
802 * become invalid. So we
803 * - don't let packets with src ip addr 0 thru
804 * - we flag TCP packets with src ip 0 as an error
807 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
811 if(ip->ip_p == IPPROTO_TCP)
812 return(EADDRNOTAVAIL);
818 * Put low delay, telnet, rlogin and ftp control packets
819 * in front of the queue.
821 if (IF_QFULL (&sp->pp_fastq))
823 else if (ip->ip_tos & IPTOS_LOWDELAY)
825 else if (m->m_len < sizeof *ip + sizeof *tcp)
827 else if (ip->ip_p != IPPROTO_TCP)
829 else if (INTERACTIVE (ntohs (tcp->th_sport)))
831 else if (INTERACTIVE (ntohs (tcp->th_dport)))
835 * Do IP Header compression
837 if (sp->pp_mode != IFF_CISCO && (sp->ipcp.flags & IPCP_VJ) &&
838 ip->ip_p == IPPROTO_TCP)
839 switch (sl_compress_tcp(m, ip, sp->pp_comp,
840 sp->ipcp.compress_cid)) {
841 case TYPE_COMPRESSED_TCP:
842 ipproto = PPP_VJ_COMP;
844 case TYPE_UNCOMPRESSED_TCP:
845 ipproto = PPP_VJ_UCOMP;
859 if (dst->sa_family == AF_INET6) {
860 /* XXX do something tricky here? */
865 * Prepend general data packet PPP header. For now, IP only.
867 M_PREPEND (m, PPP_HEADER_LEN, MB_DONTWAIT);
870 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
877 * May want to check size of packet
878 * (albeit due to the implementation it's always enough)
880 h = mtod (m, struct ppp_header*);
881 if (sp->pp_mode == IFF_CISCO) {
882 h->address = CISCO_UNICAST; /* unicast address */
885 h->address = PPP_ALLSTATIONS; /* broadcast address */
886 h->control = PPP_UI; /* Unnumbered Info */
889 switch (dst->sa_family) {
891 case AF_INET: /* Internet Protocol */
892 if (sp->pp_mode == IFF_CISCO)
893 h->protocol = htons (ETHERTYPE_IP);
896 * Don't choke with an ENETDOWN early. It's
897 * possible that we just started dialing out,
898 * so don't drop the packet immediately. If
899 * we notice that we run out of buffer space
900 * below, we will however remember that we are
901 * not ready to carry IP packets, and return
902 * ENETDOWN, as opposed to ENOBUFS.
904 h->protocol = htons(ipproto);
905 if (sp->state[IDX_IPCP] != STATE_OPENED)
911 case AF_INET6: /* Internet Protocol */
912 if (sp->pp_mode == IFF_CISCO)
913 h->protocol = htons (ETHERTYPE_IPV6);
916 * Don't choke with an ENETDOWN early. It's
917 * possible that we just started dialing out,
918 * so don't drop the packet immediately. If
919 * we notice that we run out of buffer space
920 * below, we will however remember that we are
921 * not ready to carry IP packets, and return
922 * ENETDOWN, as opposed to ENOBUFS.
924 h->protocol = htons(PPP_IPV6);
925 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
931 case AF_NS: /* Xerox NS Protocol */
932 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
933 ETHERTYPE_NS : PPP_XNS);
937 case AF_IPX: /* Novell IPX Protocol */
938 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
939 ETHERTYPE_IPX : PPP_IPX);
946 return (EAFNOSUPPORT);
950 * Queue message on interface, and start output if interface
953 if (IF_QFULL (ifq)) {
954 IF_DROP (&ifp->if_snd);
958 return (rv? rv: ENOBUFS);
961 if (! (ifp->if_flags & IFF_OACTIVE))
962 (*ifp->if_start) (ifp);
965 * Count output packets and bytes.
966 * The packet length includes header, FCS and 1 flag,
967 * according to RFC 1333.
969 ifp->if_obytes += m->m_pkthdr.len + 3;
972 * Unlike in sppp_input(), we can always bump the timestamp
973 * here since sppp_output() is only called on behalf of
974 * network-layer traffic; control-layer traffic is handled
977 sp->pp_last_sent = time_second;
982 sppp_attach(struct ifnet *ifp)
984 struct sppp *sp = (struct sppp*) ifp;
986 /* Initialize keepalive handler. */
988 callout_reset(&keepalive_timeout, hz * 10,
989 sppp_keepalive, NULL);
991 /* Insert new entry into the keepalive list. */
995 sp->pp_if.if_mtu = PP_MTU;
996 sp->pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
997 sp->pp_if.if_type = IFT_PPP;
998 sp->pp_if.if_output = sppp_output;
1000 sp->pp_flags = PP_KEEPALIVE;
1002 sp->pp_if.if_snd.ifq_maxlen = 32;
1003 sp->pp_fastq.ifq_maxlen = 32;
1004 sp->pp_cpq.ifq_maxlen = 20;
1006 sp->pp_alivecnt = 0;
1007 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
1008 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
1009 sp->pp_phase = PHASE_DEAD;
1011 sp->pp_down = lcp.Down;
1012 sp->pp_last_recv = sp->pp_last_sent = time_second;
1015 sp->confflags |= CONF_ENABLE_VJ;
1018 sp->confflags |= CONF_ENABLE_IPV6;
1020 sp->pp_comp = malloc(sizeof(struct slcompress), M_TEMP, M_WAITOK);
1021 sl_compress_init(sp->pp_comp, -1);
1024 sppp_ipv6cp_init(sp);
1030 sppp_detach(struct ifnet *ifp)
1032 struct sppp **q, *p, *sp = (struct sppp*) ifp;
1035 /* Remove the entry from the keepalive list. */
1036 for (q = &spppq; (p = *q); q = &p->pp_next)
1042 /* Stop keepalive handler. */
1044 callout_stop(&keepalive_timeout);
1046 for (i = 0; i < IDX_COUNT; i++)
1047 callout_stop(&sp->timeout[i]);
1048 callout_stop(&sp->pap_my_to);
1052 * Flush the interface output queue.
1055 sppp_flush(struct ifnet *ifp)
1057 struct sppp *sp = (struct sppp*) ifp;
1059 sppp_qflush (&sp->pp_if.if_snd);
1060 sppp_qflush (&sp->pp_fastq);
1061 sppp_qflush (&sp->pp_cpq);
1065 * Check if the output queue is empty.
1068 sppp_isempty(struct ifnet *ifp)
1070 struct sppp *sp = (struct sppp*) ifp;
1074 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
1075 !sp->pp_if.if_snd.ifq_head;
1081 * Get next packet to send.
1084 sppp_dequeue(struct ifnet *ifp)
1086 struct sppp *sp = (struct sppp*) ifp;
1092 * Process only the control protocol queue until we have at
1093 * least one NCP open.
1095 * Do always serve all three queues in Cisco mode.
1097 IF_DEQUEUE(&sp->pp_cpq, m);
1099 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO)) {
1100 IF_DEQUEUE(&sp->pp_fastq, m);
1102 IF_DEQUEUE (&sp->pp_if.if_snd, m);
1109 * Pick the next packet, do not remove it from the queue.
1112 sppp_pick(struct ifnet *ifp)
1114 struct sppp *sp = (struct sppp*)ifp;
1120 m = sp->pp_cpq.ifq_head;
1122 (sp->pp_phase == PHASE_NETWORK || sp->pp_mode == IFF_CISCO))
1123 if ((m = sp->pp_fastq.ifq_head) == NULL)
1124 m = sp->pp_if.if_snd.ifq_head;
1130 * Process an ioctl request. Called on low priority level.
1133 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1135 struct ifreq *ifr = (struct ifreq*) data;
1136 struct sppp *sp = (struct sppp*) ifp;
1137 int s, rv, going_up, going_down, newmode;
1143 case SIOCSIFDSTADDR:
1147 /* set the interface "up" when assigning an IP address */
1148 ifp->if_flags |= IFF_UP;
1149 /* fall through... */
1152 going_up = ifp->if_flags & IFF_UP &&
1153 (ifp->if_flags & IFF_RUNNING) == 0;
1154 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1155 ifp->if_flags & IFF_RUNNING;
1157 newmode = ifp->if_flags & IFF_PASSIVE;
1159 newmode = ifp->if_flags & IFF_AUTO;
1161 newmode = ifp->if_flags & IFF_CISCO;
1162 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1163 ifp->if_flags |= newmode;
1165 if (newmode != sp->pp_mode) {
1168 going_up = ifp->if_flags & IFF_RUNNING;
1172 if (sp->pp_mode != IFF_CISCO)
1174 else if (sp->pp_tlf)
1177 ifp->if_flags &= ~IFF_RUNNING;
1178 sp->pp_mode = newmode;
1182 if (sp->pp_mode != IFF_CISCO)
1184 sp->pp_mode = newmode;
1185 if (sp->pp_mode == 0) {
1186 ifp->if_flags |= IFF_RUNNING;
1189 if (sp->pp_mode == IFF_CISCO) {
1192 ifp->if_flags |= IFF_RUNNING;
1200 #define ifr_mtu ifr_metric
1203 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1205 ifp->if_mtu = ifr->ifr_mtu;
1210 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1212 ifp->if_mtu = *(short*)data;
1217 ifr->ifr_mtu = ifp->if_mtu;
1222 *(short*)data = ifp->if_mtu;
1229 case SIOCGIFGENERIC:
1230 case SIOCSIFGENERIC:
1231 rv = sppp_params(sp, cmd, data);
1242 * Cisco framing implementation.
1246 * Handle incoming Cisco keepalive protocol packets.
1249 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1252 struct cisco_packet *h;
1255 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1258 SPP_FMT "cisco invalid packet length: %d bytes\n",
1259 SPP_ARGS(ifp), m->m_pkthdr.len);
1262 h = mtod (m, struct cisco_packet*);
1265 SPP_FMT "cisco input: %d bytes "
1266 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1267 SPP_ARGS(ifp), m->m_pkthdr.len,
1268 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1269 (u_int)h->time0, (u_int)h->time1);
1270 switch (ntohl (h->type)) {
1273 addlog(SPP_FMT "cisco unknown packet type: 0x%lx\n",
1274 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1276 case CISCO_ADDR_REPLY:
1277 /* Reply on address request, ignore */
1279 case CISCO_KEEPALIVE_REQ:
1280 sp->pp_alivecnt = 0;
1281 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1282 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1283 /* Local and remote sequence numbers are equal.
1284 * Probably, the line is in loopback mode. */
1285 if (sp->pp_loopcnt >= MAXALIVECNT) {
1286 printf (SPP_FMT "loopback\n",
1289 if (ifp->if_flags & IFF_UP) {
1291 sppp_qflush (&sp->pp_cpq);
1296 /* Generate new local sequence number */
1297 #if defined(__DragonFly__)
1298 sp->pp_seq[IDX_LCP] = random();
1300 sp->pp_seq[IDX_LCP] ^= time.tv_sec ^ time.tv_usec;
1305 if (! (ifp->if_flags & IFF_UP) &&
1306 (ifp->if_flags & IFF_RUNNING)) {
1308 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1311 case CISCO_ADDR_REQ:
1312 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1314 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1320 * Send Cisco keepalive packet.
1323 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1326 struct ppp_header *h;
1327 struct cisco_packet *ch;
1329 #if defined(__DragonFly__)
1332 u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
1335 #if defined(__DragonFly__)
1336 getmicrouptime(&tv);
1339 MGETHDR (m, MB_DONTWAIT, MT_DATA);
1342 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1343 m->m_pkthdr.rcvif = 0;
1345 h = mtod (m, struct ppp_header*);
1346 h->address = CISCO_MULTICAST;
1348 h->protocol = htons (CISCO_KEEPALIVE);
1350 ch = (struct cisco_packet*) (h + 1);
1351 ch->type = htonl (type);
1352 ch->par1 = htonl (par1);
1353 ch->par2 = htonl (par2);
1356 #if defined(__DragonFly__)
1357 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1358 ch->time1 = htons ((u_short) tv.tv_sec);
1360 ch->time0 = htons ((u_short) (t >> 16));
1361 ch->time1 = htons ((u_short) t);
1366 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1367 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1368 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1370 if (IF_QFULL (&sp->pp_cpq)) {
1371 IF_DROP (&sp->pp_fastq);
1372 IF_DROP (&ifp->if_snd);
1375 IF_ENQUEUE (&sp->pp_cpq, m);
1376 if (! (ifp->if_flags & IFF_OACTIVE))
1377 (*ifp->if_start) (ifp);
1378 ifp->if_obytes += m->m_pkthdr.len + 3;
1382 * PPP protocol implementation.
1386 * Send PPP control protocol packet.
1389 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1390 u_char ident, u_short len, void *data)
1393 struct ppp_header *h;
1394 struct lcp_header *lh;
1397 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1398 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1399 MGETHDR (m, MB_DONTWAIT, MT_DATA);
1402 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1403 m->m_pkthdr.rcvif = 0;
1405 h = mtod (m, struct ppp_header*);
1406 h->address = PPP_ALLSTATIONS; /* broadcast address */
1407 h->control = PPP_UI; /* Unnumbered Info */
1408 h->protocol = htons (proto); /* Link Control Protocol */
1410 lh = (struct lcp_header*) (h + 1);
1413 lh->len = htons (LCP_HEADER_LEN + len);
1415 bcopy (data, lh+1, len);
1418 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1420 sppp_proto_name(proto),
1421 sppp_cp_type_name (lh->type), lh->ident,
1423 sppp_print_bytes ((u_char*) (lh+1), len);
1426 if (IF_QFULL (&sp->pp_cpq)) {
1427 IF_DROP (&sp->pp_fastq);
1428 IF_DROP (&ifp->if_snd);
1432 IF_ENQUEUE (&sp->pp_cpq, m);
1433 if (! (ifp->if_flags & IFF_OACTIVE))
1434 (*ifp->if_start) (ifp);
1435 ifp->if_obytes += m->m_pkthdr.len + 3;
1439 * Handle incoming PPP control protocol packets.
1442 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1445 struct lcp_header *h;
1446 int len = m->m_pkthdr.len;
1453 SPP_FMT "%s invalid packet length: %d bytes\n",
1454 SPP_ARGS(ifp), cp->name, len);
1457 h = mtod (m, struct lcp_header*);
1460 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1461 SPP_ARGS(ifp), cp->name,
1462 sppp_state_name(sp->state[cp->protoidx]),
1463 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1464 sppp_print_bytes ((u_char*) (h+1), len-4);
1467 if (len > ntohs (h->len))
1468 len = ntohs (h->len);
1469 p = (u_char *)(h + 1);
1474 addlog(SPP_FMT "%s invalid conf-req length %d\n",
1475 SPP_ARGS(ifp), cp->name,
1480 /* handle states where RCR doesn't get a SCA/SCN */
1481 switch (sp->state[cp->protoidx]) {
1483 case STATE_STOPPING:
1486 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1490 rv = (cp->RCR)(sp, h, len);
1491 switch (sp->state[cp->protoidx]) {
1495 /* fall through... */
1496 case STATE_ACK_SENT:
1497 case STATE_REQ_SENT:
1499 * sppp_cp_change_state() have the side effect of
1500 * restarting the timeouts. We want to avoid that
1501 * if the state don't change, otherwise we won't
1502 * ever timeout and resend a configuration request
1505 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1508 sppp_cp_change_state(cp, sp, rv?
1509 STATE_ACK_SENT: STATE_REQ_SENT);
1512 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1514 sppp_cp_change_state(cp, sp, rv?
1515 STATE_ACK_SENT: STATE_REQ_SENT);
1517 case STATE_ACK_RCVD:
1519 sppp_cp_change_state(cp, sp, STATE_OPENED);
1521 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1526 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1529 printf(SPP_FMT "%s illegal %s in state %s\n",
1530 SPP_ARGS(ifp), cp->name,
1531 sppp_cp_type_name(h->type),
1532 sppp_state_name(sp->state[cp->protoidx]));
1537 if (h->ident != sp->confid[cp->protoidx]) {
1539 addlog(SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1540 SPP_ARGS(ifp), cp->name,
1541 h->ident, sp->confid[cp->protoidx]);
1545 switch (sp->state[cp->protoidx]) {
1548 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1551 case STATE_STOPPING:
1553 case STATE_REQ_SENT:
1554 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1555 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1560 case STATE_ACK_RCVD:
1562 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1564 case STATE_ACK_SENT:
1565 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1566 sppp_cp_change_state(cp, sp, STATE_OPENED);
1568 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1569 SPP_ARGS(ifp), cp->name);
1573 printf(SPP_FMT "%s illegal %s in state %s\n",
1574 SPP_ARGS(ifp), cp->name,
1575 sppp_cp_type_name(h->type),
1576 sppp_state_name(sp->state[cp->protoidx]));
1582 if (h->ident != sp->confid[cp->protoidx]) {
1584 addlog(SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1585 SPP_ARGS(ifp), cp->name,
1586 h->ident, sp->confid[cp->protoidx]);
1590 if (h->type == CONF_NAK)
1591 (cp->RCN_nak)(sp, h, len);
1593 (cp->RCN_rej)(sp, h, len);
1595 switch (sp->state[cp->protoidx]) {
1598 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1600 case STATE_REQ_SENT:
1601 case STATE_ACK_SENT:
1602 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1604 * Slow things down a bit if we think we might be
1605 * in loopback. Depend on the timeout to send the
1606 * next configuration request.
1615 case STATE_ACK_RCVD:
1616 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1620 case STATE_STOPPING:
1623 printf(SPP_FMT "%s illegal %s in state %s\n",
1624 SPP_ARGS(ifp), cp->name,
1625 sppp_cp_type_name(h->type),
1626 sppp_state_name(sp->state[cp->protoidx]));
1632 switch (sp->state[cp->protoidx]) {
1633 case STATE_ACK_RCVD:
1634 case STATE_ACK_SENT:
1635 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1640 case STATE_STOPPING:
1641 case STATE_REQ_SENT:
1643 /* Send Terminate-Ack packet. */
1645 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1646 SPP_ARGS(ifp), cp->name);
1647 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1651 sp->rst_counter[cp->protoidx] = 0;
1652 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1656 printf(SPP_FMT "%s illegal %s in state %s\n",
1657 SPP_ARGS(ifp), cp->name,
1658 sppp_cp_type_name(h->type),
1659 sppp_state_name(sp->state[cp->protoidx]));
1664 switch (sp->state[cp->protoidx]) {
1667 case STATE_REQ_SENT:
1668 case STATE_ACK_SENT:
1671 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1674 case STATE_STOPPING:
1675 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1678 case STATE_ACK_RCVD:
1679 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1684 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1687 printf(SPP_FMT "%s illegal %s in state %s\n",
1688 SPP_ARGS(ifp), cp->name,
1689 sppp_cp_type_name(h->type),
1690 sppp_state_name(sp->state[cp->protoidx]));
1695 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1697 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1698 "danger will robinson\n",
1699 SPP_ARGS(ifp), cp->name,
1700 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1701 switch (sp->state[cp->protoidx]) {
1704 case STATE_REQ_SENT:
1705 case STATE_ACK_SENT:
1707 case STATE_STOPPING:
1710 case STATE_ACK_RCVD:
1711 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1714 printf(SPP_FMT "%s illegal %s in state %s\n",
1715 SPP_ARGS(ifp), cp->name,
1716 sppp_cp_type_name(h->type),
1717 sppp_state_name(sp->state[cp->protoidx]));
1724 const struct cp *upper;
1730 proto = ntohs(*((u_int16_t *)p));
1731 for (i = 0; i < IDX_COUNT; i++) {
1732 if (cps[i]->proto == proto) {
1740 if (catastrophic || debug)
1741 log(catastrophic? LOG_INFO: LOG_DEBUG,
1742 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1743 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1744 sppp_cp_type_name(h->type), proto,
1745 upper ? upper->name : "unknown",
1746 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1749 * if we got RXJ+ against conf-req, the peer does not implement
1750 * this particular protocol type. terminate the protocol.
1752 if (upper && !catastrophic) {
1753 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1759 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1760 switch (sp->state[cp->protoidx]) {
1763 case STATE_REQ_SENT:
1764 case STATE_ACK_SENT:
1766 case STATE_STOPPING:
1769 case STATE_ACK_RCVD:
1770 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1773 printf(SPP_FMT "%s illegal %s in state %s\n",
1774 SPP_ARGS(ifp), cp->name,
1775 sppp_cp_type_name(h->type),
1776 sppp_state_name(sp->state[cp->protoidx]));
1782 if (cp->proto != PPP_LCP)
1784 /* Discard the packet. */
1787 if (cp->proto != PPP_LCP)
1789 if (sp->state[cp->protoidx] != STATE_OPENED) {
1791 addlog(SPP_FMT "lcp echo req but lcp closed\n",
1798 addlog(SPP_FMT "invalid lcp echo request "
1799 "packet length: %d bytes\n",
1800 SPP_ARGS(ifp), len);
1803 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1804 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1805 /* Line loopback mode detected. */
1806 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1807 sp->pp_loopcnt = MAXALIVECNT * 5;
1809 sppp_qflush (&sp->pp_cpq);
1811 /* Shut down the PPP link. */
1817 *(long*)(h+1) = htonl (sp->lcp.magic);
1819 addlog(SPP_FMT "got lcp echo req, sending echo rep\n",
1821 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1824 if (cp->proto != PPP_LCP)
1826 if (h->ident != sp->lcp.echoid) {
1832 addlog(SPP_FMT "lcp invalid echo reply "
1833 "packet length: %d bytes\n",
1834 SPP_ARGS(ifp), len);
1838 addlog(SPP_FMT "lcp got echo rep\n",
1840 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1841 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1842 sp->pp_alivecnt = 0;
1845 /* Unknown packet type -- send Code-Reject packet. */
1848 addlog(SPP_FMT "%s send code-rej for 0x%x\n",
1849 SPP_ARGS(ifp), cp->name, h->type);
1850 sppp_cp_send(sp, cp->proto, CODE_REJ,
1851 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1858 * The generic part of all Up/Down/Open/Close/TO event handlers.
1859 * Basically, the state transition handling in the automaton.
1862 sppp_up_event(const struct cp *cp, struct sppp *sp)
1867 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1868 SPP_ARGS(ifp), cp->name,
1869 sppp_state_name(sp->state[cp->protoidx]));
1871 switch (sp->state[cp->protoidx]) {
1873 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1875 case STATE_STARTING:
1876 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1878 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1881 printf(SPP_FMT "%s illegal up in state %s\n",
1882 SPP_ARGS(ifp), cp->name,
1883 sppp_state_name(sp->state[cp->protoidx]));
1888 sppp_down_event(const struct cp *cp, struct sppp *sp)
1893 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1894 SPP_ARGS(ifp), cp->name,
1895 sppp_state_name(sp->state[cp->protoidx]));
1897 switch (sp->state[cp->protoidx]) {
1900 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1903 sppp_cp_change_state(cp, sp, STATE_STARTING);
1906 case STATE_STOPPING:
1907 case STATE_REQ_SENT:
1908 case STATE_ACK_RCVD:
1909 case STATE_ACK_SENT:
1910 sppp_cp_change_state(cp, sp, STATE_STARTING);
1914 sppp_cp_change_state(cp, sp, STATE_STARTING);
1917 printf(SPP_FMT "%s illegal down in state %s\n",
1918 SPP_ARGS(ifp), cp->name,
1919 sppp_state_name(sp->state[cp->protoidx]));
1925 sppp_open_event(const struct cp *cp, struct sppp *sp)
1930 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1931 SPP_ARGS(ifp), cp->name,
1932 sppp_state_name(sp->state[cp->protoidx]));
1934 switch (sp->state[cp->protoidx]) {
1936 sppp_cp_change_state(cp, sp, STATE_STARTING);
1939 case STATE_STARTING:
1942 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1944 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1948 * Try escaping stopped state. This seems to bite
1949 * people occasionally, in particular for IPCP,
1950 * presumably following previous IPCP negotiation
1951 * aborts. Somehow, we must have missed a Down event
1952 * which would have caused a transition into starting
1953 * state, so as a bandaid we force the Down event now.
1954 * This effectively implements (something like the)
1955 * `restart' option mentioned in the state transition
1956 * table of RFC 1661.
1958 sppp_cp_change_state(cp, sp, STATE_STARTING);
1961 case STATE_STOPPING:
1962 case STATE_REQ_SENT:
1963 case STATE_ACK_RCVD:
1964 case STATE_ACK_SENT:
1968 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1975 sppp_close_event(const struct cp *cp, struct sppp *sp)
1980 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
1981 SPP_ARGS(ifp), cp->name,
1982 sppp_state_name(sp->state[cp->protoidx]));
1984 switch (sp->state[cp->protoidx]) {
1989 case STATE_STARTING:
1990 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1994 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1996 case STATE_STOPPING:
1997 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2002 case STATE_REQ_SENT:
2003 case STATE_ACK_RCVD:
2004 case STATE_ACK_SENT:
2005 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2006 sppp_cp_send(sp, cp->proto, TERM_REQ,
2007 ++sp->pp_seq[cp->protoidx], 0, 0);
2008 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2014 sppp_to_event(const struct cp *cp, struct sppp *sp)
2021 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
2022 SPP_ARGS(ifp), cp->name,
2023 sppp_state_name(sp->state[cp->protoidx]),
2024 sp->rst_counter[cp->protoidx]);
2026 if (--sp->rst_counter[cp->protoidx] < 0)
2028 switch (sp->state[cp->protoidx]) {
2030 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2033 case STATE_STOPPING:
2034 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2037 case STATE_REQ_SENT:
2038 case STATE_ACK_RCVD:
2039 case STATE_ACK_SENT:
2040 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2046 switch (sp->state[cp->protoidx]) {
2048 case STATE_STOPPING:
2049 sppp_cp_send(sp, cp->proto, TERM_REQ,
2050 ++sp->pp_seq[cp->protoidx], 0, 0);
2051 callout_reset(&sp->timeout[cp->protoidx],
2052 sp->lcp.timeout, cp->TO, sp);
2054 case STATE_REQ_SENT:
2055 case STATE_ACK_RCVD:
2057 /* sppp_cp_change_state() will restart the timer */
2058 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2060 case STATE_ACK_SENT:
2062 callout_reset(&sp->timeout[cp->protoidx],
2063 sp->lcp.timeout, cp->TO, sp);
2071 * Change the state of a control protocol in the state automaton.
2072 * Takes care of starting/stopping the restart timer.
2075 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2077 sp->state[cp->protoidx] = newstate;
2078 callout_stop(&sp->timeout[cp->protoidx]);
2082 case STATE_STARTING:
2088 case STATE_STOPPING:
2089 case STATE_REQ_SENT:
2090 case STATE_ACK_RCVD:
2091 case STATE_ACK_SENT:
2092 callout_reset(&sp->timeout[cp->protoidx],
2093 sp->lcp.timeout, cp->TO, sp);
2099 *--------------------------------------------------------------------------*
2101 * The LCP implementation. *
2103 *--------------------------------------------------------------------------*
2106 sppp_lcp_init(struct sppp *sp)
2108 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2110 sp->state[IDX_LCP] = STATE_INITIAL;
2111 sp->fail_counter[IDX_LCP] = 0;
2112 sp->pp_seq[IDX_LCP] = 0;
2113 sp->pp_rseq[IDX_LCP] = 0;
2115 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2117 /* Note that these values are relevant for all control protocols */
2118 sp->lcp.timeout = 3 * hz;
2119 sp->lcp.max_terminate = 2;
2120 sp->lcp.max_configure = 10;
2121 sp->lcp.max_failure = 10;
2122 #if defined(__DragonFly__)
2123 callout_init(&sp->timeout[IDX_LCP]);
2128 sppp_lcp_up(struct sppp *sp)
2132 sp->pp_alivecnt = 0;
2133 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2136 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2138 * If this interface is passive or dial-on-demand, and we are
2139 * still in Initial state, it means we've got an incoming
2140 * call. Activate the interface.
2142 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2145 SPP_FMT "Up event", SPP_ARGS(ifp));
2146 ifp->if_flags |= IFF_RUNNING;
2147 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2149 addlog("(incoming call)\n");
2150 sp->pp_flags |= PP_CALLIN;
2154 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2155 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2156 ifp->if_flags |= IFF_RUNNING;
2160 sppp_up_event(&lcp, sp);
2164 sppp_lcp_down(struct sppp *sp)
2168 sppp_down_event(&lcp, sp);
2171 * If this is neither a dial-on-demand nor a passive
2172 * interface, simulate an ``ifconfig down'' action, so the
2173 * administrator can force a redial by another ``ifconfig
2174 * up''. XXX For leased line operation, should we immediately
2175 * try to reopen the connection here?
2177 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2179 SPP_FMT "Down event, taking interface down.\n",
2185 SPP_FMT "Down event (carrier loss)\n",
2187 sp->pp_flags &= ~PP_CALLIN;
2188 if (sp->state[IDX_LCP] != STATE_INITIAL)
2190 ifp->if_flags &= ~IFF_RUNNING;
2195 sppp_lcp_open(struct sppp *sp)
2198 * If we are authenticator, negotiate LCP_AUTH
2200 if (sp->hisauth.proto != 0)
2201 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2203 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2204 sp->pp_flags &= ~PP_NEEDAUTH;
2205 sppp_open_event(&lcp, sp);
2209 sppp_lcp_close(struct sppp *sp)
2211 sppp_close_event(&lcp, sp);
2215 sppp_lcp_TO(void *cookie)
2217 sppp_to_event(&lcp, (struct sppp *)cookie);
2221 * Analyze a configure request. Return true if it was agreeable, and
2222 * caused action sca, false if it has been rejected or nak'ed, and
2223 * caused action scn. (The return value is used to make the state
2224 * transition decision in the state automaton.)
2227 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2230 u_char *buf, *r, *p;
2237 buf = r = malloc (len, M_TEMP, M_INTWAIT);
2240 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2243 /* pass 1: check for things that need to be rejected */
2245 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2247 addlog(" %s ", sppp_lcp_opt_name(*p));
2251 if (len >= 6 && p[1] == 6)
2254 addlog("[invalid] ");
2256 case LCP_OPT_ASYNC_MAP:
2257 /* Async control character map. */
2258 if (len >= 6 && p[1] == 6)
2261 addlog("[invalid] ");
2264 /* Maximum receive unit. */
2265 if (len >= 4 && p[1] == 4)
2268 addlog("[invalid] ");
2270 case LCP_OPT_AUTH_PROTO:
2273 addlog("[invalid] ");
2276 authproto = (p[2] << 8) + p[3];
2277 if (authproto == PPP_CHAP && p[1] != 5) {
2279 addlog("[invalid chap len] ");
2282 if (sp->myauth.proto == 0) {
2283 /* we are not configured to do auth */
2285 addlog("[not configured] ");
2289 * Remote want us to authenticate, remember this,
2290 * so we stay in PHASE_AUTHENTICATE after LCP got
2293 sp->pp_flags |= PP_NEEDAUTH;
2296 /* Others not supported. */
2301 /* Add the option to rejected list. */
2308 addlog(" send conf-rej\n");
2309 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2315 * pass 2: check for option values that are unacceptable and
2316 * thus require to be nak'ed.
2319 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2324 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2326 addlog(" %s ", sppp_lcp_opt_name(*p));
2329 /* Magic number -- extract. */
2330 nmagic = (u_long)p[2] << 24 |
2331 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2332 if (nmagic != sp->lcp.magic) {
2335 addlog("0x%lx ", nmagic);
2338 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2339 addlog("[glitch] ");
2342 * We negate our magic here, and NAK it. If
2343 * we see it later in an NAK packet, we
2344 * suggest a new one.
2346 nmagic = ~sp->lcp.magic;
2348 p[2] = nmagic >> 24;
2349 p[3] = nmagic >> 16;
2354 case LCP_OPT_ASYNC_MAP:
2356 * Async control character map -- just ignore it.
2358 * Quote from RFC 1662, chapter 6:
2359 * To enable this functionality, synchronous PPP
2360 * implementations MUST always respond to the
2361 * Async-Control-Character-Map Configuration
2362 * Option with the LCP Configure-Ack. However,
2363 * acceptance of the Configuration Option does
2364 * not imply that the synchronous implementation
2365 * will do any ACCM mapping. Instead, all such
2366 * octet mapping will be performed by the
2367 * asynchronous-to-synchronous converter.
2373 * Maximum receive unit. Always agreeable,
2374 * but ignored by now.
2376 sp->lcp.their_mru = p[2] * 256 + p[3];
2378 addlog("%lu ", sp->lcp.their_mru);
2381 case LCP_OPT_AUTH_PROTO:
2382 authproto = (p[2] << 8) + p[3];
2383 if (sp->myauth.proto != authproto) {
2384 /* not agreed, nak */
2386 addlog("[mine %s != his %s] ",
2387 sppp_proto_name(sp->hisauth.proto),
2388 sppp_proto_name(authproto));
2389 p[2] = sp->myauth.proto >> 8;
2390 p[3] = sp->myauth.proto;
2393 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2395 addlog("[chap not MD5] ");
2401 /* Add the option to nak'ed list. */
2408 * Local and remote magics equal -- loopback?
2410 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2411 if (sp->pp_loopcnt == MAXALIVECNT*5)
2412 printf (SPP_FMT "loopback\n",
2414 if (ifp->if_flags & IFF_UP) {
2416 sppp_qflush(&sp->pp_cpq);
2421 } else if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2423 addlog(" max_failure (%d) exceeded, "
2425 sp->lcp.max_failure);
2426 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2429 addlog(" send conf-nak\n");
2430 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2434 addlog(" send conf-ack\n");
2435 sp->fail_counter[IDX_LCP] = 0;
2437 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2438 h->ident, origlen, h+1);
2446 * Analyze the LCP Configure-Reject option list, and adjust our
2450 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2456 buf = malloc (len, M_TEMP, M_INTWAIT);
2459 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2463 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2465 addlog(" %s ", sppp_lcp_opt_name(*p));
2468 /* Magic number -- can't use it, use 0 */
2469 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2474 * Should not be rejected anyway, since we only
2475 * negotiate a MRU if explicitly requested by
2478 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2480 case LCP_OPT_AUTH_PROTO:
2482 * Peer doesn't want to authenticate himself,
2483 * deny unless this is a dialout call, and
2484 * AUTHFLAG_NOCALLOUT is set.
2486 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2487 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2489 addlog("[don't insist on auth "
2491 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2495 addlog("[access denied]\n");
2507 * Analyze the LCP Configure-NAK option list, and adjust our
2511 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2518 buf = malloc (len, M_TEMP, M_INTWAIT);
2521 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2525 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2527 addlog(" %s ", sppp_lcp_opt_name(*p));
2530 /* Magic number -- renegotiate */
2531 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2532 len >= 6 && p[1] == 6) {
2533 magic = (u_long)p[2] << 24 |
2534 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2536 * If the remote magic is our negated one,
2537 * this looks like a loopback problem.
2538 * Suggest a new magic to make sure.
2540 if (magic == ~sp->lcp.magic) {
2542 addlog("magic glitch ");
2543 #if defined(__DragonFly__)
2544 sp->lcp.magic = random();
2546 sp->lcp.magic = time.tv_sec + time.tv_usec;
2549 sp->lcp.magic = magic;
2551 addlog("%lu ", magic);
2557 * Peer wants to advise us to negotiate an MRU.
2558 * Agree on it if it's reasonable, or use
2559 * default otherwise.
2561 if (len >= 4 && p[1] == 4) {
2562 u_int mru = p[2] * 256 + p[3];
2565 if (mru < PP_MTU || mru > PP_MAX_MRU)
2568 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2571 case LCP_OPT_AUTH_PROTO:
2573 * Peer doesn't like our authentication method,
2577 addlog("[access denied]\n");
2589 sppp_lcp_tlu(struct sppp *sp)
2596 if (! (ifp->if_flags & IFF_UP) &&
2597 (ifp->if_flags & IFF_RUNNING)) {
2598 /* Coming out of loopback mode. */
2600 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2603 for (i = 0; i < IDX_COUNT; i++)
2604 if ((cps[i])->flags & CP_QUAL)
2607 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2608 (sp->pp_flags & PP_NEEDAUTH) != 0)
2609 sp->pp_phase = PHASE_AUTHENTICATE;
2611 sp->pp_phase = PHASE_NETWORK;
2614 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2615 sppp_phase_name(sp->pp_phase));
2618 * Open all authentication protocols. This is even required
2619 * if we already proceeded to network phase, since it might be
2620 * that remote wants us to authenticate, so we might have to
2621 * send a PAP request. Undesired authentication protocols
2622 * don't do anything when they get an Open event.
2624 for (i = 0; i < IDX_COUNT; i++)
2625 if ((cps[i])->flags & CP_AUTH)
2628 if (sp->pp_phase == PHASE_NETWORK) {
2629 /* Notify all NCPs. */
2630 for (i = 0; i < IDX_COUNT; i++)
2631 if (((cps[i])->flags & CP_NCP) &&
2634 * Hack to administratively disable IPv6 if
2635 * not desired. Perhaps we should have another
2636 * flag for this, but right now, we can make
2637 * all struct cp's read/only.
2639 (cps[i] != &ipv6cp ||
2640 (sp->confflags & CONF_ENABLE_IPV6)))
2644 /* Send Up events to all started protos. */
2645 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2646 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2649 /* notify low-level driver of state change */
2651 sp->pp_chg(sp, (int)sp->pp_phase);
2653 if (sp->pp_phase == PHASE_NETWORK)
2654 /* if no NCP is starting, close down */
2655 sppp_lcp_check_and_close(sp);
2659 sppp_lcp_tld(struct sppp *sp)
2665 sp->pp_phase = PHASE_TERMINATE;
2668 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2669 sppp_phase_name(sp->pp_phase));
2672 * Take upper layers down. We send the Down event first and
2673 * the Close second to prevent the upper layers from sending
2674 * ``a flurry of terminate-request packets'', as the RFC
2677 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2678 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2680 (cps[i])->Close(sp);
2685 sppp_lcp_tls(struct sppp *sp)
2689 sp->pp_phase = PHASE_ESTABLISH;
2692 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2693 sppp_phase_name(sp->pp_phase));
2695 /* Notify lower layer if desired. */
2703 sppp_lcp_tlf(struct sppp *sp)
2707 sp->pp_phase = PHASE_DEAD;
2709 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2710 sppp_phase_name(sp->pp_phase));
2712 /* Notify lower layer if desired. */
2720 sppp_lcp_scr(struct sppp *sp)
2722 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2726 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2727 if (! sp->lcp.magic)
2728 #if defined(__DragonFly__)
2729 sp->lcp.magic = random();
2731 sp->lcp.magic = time.tv_sec + time.tv_usec;
2733 opt[i++] = LCP_OPT_MAGIC;
2735 opt[i++] = sp->lcp.magic >> 24;
2736 opt[i++] = sp->lcp.magic >> 16;
2737 opt[i++] = sp->lcp.magic >> 8;
2738 opt[i++] = sp->lcp.magic;
2741 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2742 opt[i++] = LCP_OPT_MRU;
2744 opt[i++] = sp->lcp.mru >> 8;
2745 opt[i++] = sp->lcp.mru;
2748 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2749 authproto = sp->hisauth.proto;
2750 opt[i++] = LCP_OPT_AUTH_PROTO;
2751 opt[i++] = authproto == PPP_CHAP? 5: 4;
2752 opt[i++] = authproto >> 8;
2753 opt[i++] = authproto;
2754 if (authproto == PPP_CHAP)
2755 opt[i++] = CHAP_MD5;
2758 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2759 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2763 * Check the open NCPs, return true if at least one NCP is open.
2766 sppp_ncp_check(struct sppp *sp)
2770 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2771 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2777 * Re-check the open NCPs and see if we should terminate the link.
2778 * Called by the NCPs during their tlf action handling.
2781 sppp_lcp_check_and_close(struct sppp *sp)
2784 if (sp->pp_phase < PHASE_NETWORK)
2785 /* don't bother, we are already going down */
2788 if (sppp_ncp_check(sp))
2795 *--------------------------------------------------------------------------*
2797 * The IPCP implementation. *
2799 *--------------------------------------------------------------------------*
2803 sppp_ipcp_init(struct sppp *sp)
2807 sp->state[IDX_IPCP] = STATE_INITIAL;
2808 sp->fail_counter[IDX_IPCP] = 0;
2809 sp->pp_seq[IDX_IPCP] = 0;
2810 sp->pp_rseq[IDX_IPCP] = 0;
2811 #if defined(__DragonFly__)
2812 callout_init(&sp->timeout[IDX_IPCP]);
2817 sppp_ipcp_up(struct sppp *sp)
2819 sppp_up_event(&ipcp, sp);
2823 sppp_ipcp_down(struct sppp *sp)
2825 sppp_down_event(&ipcp, sp);
2829 sppp_ipcp_open(struct sppp *sp)
2832 u_long myaddr, hisaddr;
2834 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2835 IPCP_MYADDR_DYN | IPCP_VJ);
2838 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2840 * If we don't have his address, this probably means our
2841 * interface doesn't want to talk IP at all. (This could
2842 * be the case if somebody wants to speak only IPX, for
2843 * example.) Don't open IPCP in this case.
2845 if (hisaddr == 0L) {
2846 /* XXX this message should go away */
2848 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2854 * I don't have an assigned address, so i need to
2855 * negotiate my address.
2857 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2858 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2860 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2861 if (sp->confflags & CONF_ENABLE_VJ) {
2862 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2863 sp->ipcp.max_state = MAX_STATES - 1;
2864 sp->ipcp.compress_cid = 1;
2866 sppp_open_event(&ipcp, sp);
2870 sppp_ipcp_close(struct sppp *sp)
2872 sppp_close_event(&ipcp, sp);
2873 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2875 * My address was dynamic, clear it again.
2877 sppp_set_ip_addr(sp, 0L);
2881 sppp_ipcp_TO(void *cookie)
2883 sppp_to_event(&ipcp, (struct sppp *)cookie);
2887 * Analyze a configure request. Return true if it was agreeable, and
2888 * caused action sca, false if it has been rejected or nak'ed, and
2889 * caused action scn. (The return value is used to make the state
2890 * transition decision in the state automaton.)
2893 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2895 u_char *buf, *r, *p;
2896 struct ifnet *ifp = &sp->pp_if;
2897 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2898 u_long hisaddr, desiredaddr;
2905 * Make sure to allocate a buf that can at least hold a
2906 * conf-nak with an `address' option. We might need it below.
2908 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_INTWAIT);
2910 /* pass 1: see if we can recognize them */
2912 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2915 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2917 addlog(" %s ", sppp_ipcp_opt_name(*p));
2919 case IPCP_OPT_COMPRESSION:
2920 if (!(sp->confflags & CONF_ENABLE_VJ)) {
2921 /* VJ compression administratively disabled */
2923 addlog("[locally disabled] ");
2927 * In theory, we should only conf-rej an
2928 * option that is shorter than RFC 1618
2929 * requires (i.e. < 4), and should conf-nak
2930 * anything else that is not VJ. However,
2931 * since our algorithm always uses the
2932 * original option to NAK it with new values,
2933 * things would become more complicated. In
2934 * pratice, the only commonly implemented IP
2935 * compression option is VJ anyway, so the
2936 * difference is negligible.
2938 if (len >= 6 && p[1] == 6) {
2940 * correctly formed compression option
2941 * that could be VJ compression
2946 addlog("optlen %d [invalid/unsupported] ",
2949 case IPCP_OPT_ADDRESS:
2950 if (len >= 6 && p[1] == 6) {
2951 /* correctly formed address option */
2955 addlog("[invalid] ");
2958 /* Others not supported. */
2963 /* Add the option to rejected list. */
2970 addlog(" send conf-rej\n");
2971 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2976 /* pass 2: parse option values */
2977 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
2979 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
2983 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2985 addlog(" %s ", sppp_ipcp_opt_name(*p));
2987 case IPCP_OPT_COMPRESSION:
2988 desiredcomp = p[2] << 8 | p[3];
2989 /* We only support VJ */
2990 if (desiredcomp == IPCP_COMP_VJ) {
2992 addlog("VJ [ack] ");
2993 sp->ipcp.flags |= IPCP_VJ;
2994 sl_compress_init(sp->pp_comp, p[4]);
2995 sp->ipcp.max_state = p[4];
2996 sp->ipcp.compress_cid = p[5];
3000 addlog("compproto %#04x [not supported] ",
3002 p[2] = IPCP_COMP_VJ >> 8;
3003 p[3] = IPCP_COMP_VJ;
3004 p[4] = sp->ipcp.max_state;
3005 p[5] = sp->ipcp.compress_cid;
3007 case IPCP_OPT_ADDRESS:
3008 /* This is the address he wants in his end */
3009 desiredaddr = p[2] << 24 | p[3] << 16 |
3011 if (desiredaddr == hisaddr ||
3012 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3014 * Peer's address is same as our value,
3015 * or we have set it to 0.0.0.* to
3016 * indicate that we do not really care,
3017 * this is agreeable. Gonna conf-ack
3022 sppp_dotted_quad(hisaddr));
3023 /* record that we've seen it already */
3024 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3028 * The address wasn't agreeable. This is either
3029 * he sent us 0.0.0.0, asking to assign him an
3030 * address, or he send us another address not
3031 * matching our value. Either case, we gonna
3032 * conf-nak it with our value.
3033 * XXX: we should "rej" if hisaddr == 0
3036 if (desiredaddr == 0)
3037 addlog("[addr requested] ");
3039 addlog("%s [not agreed] ",
3040 sppp_dotted_quad(desiredaddr));
3043 p[2] = hisaddr >> 24;
3044 p[3] = hisaddr >> 16;
3045 p[4] = hisaddr >> 8;
3049 /* Add the option to nak'ed list. */
3056 * If we are about to conf-ack the request, but haven't seen
3057 * his address so far, gonna conf-nak it instead, with the
3058 * `address' option present and our idea of his address being
3059 * filled in there, to request negotiation of both addresses.
3061 * XXX This can result in an endless req - nak loop if peer
3062 * doesn't want to send us his address. Q: What should we do
3063 * about it? XXX A: implement the max-failure counter.
3065 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3066 buf[0] = IPCP_OPT_ADDRESS;
3068 buf[2] = hisaddr >> 24;
3069 buf[3] = hisaddr >> 16;
3070 buf[4] = hisaddr >> 8;
3074 addlog("still need hisaddr ");
3079 addlog(" send conf-nak\n");
3080 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3083 addlog(" send conf-ack\n");
3084 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3085 h->ident, origlen, h+1);
3093 * Analyze the IPCP Configure-Reject option list, and adjust our
3097 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3100 struct ifnet *ifp = &sp->pp_if;
3101 int debug = ifp->if_flags & IFF_DEBUG;
3104 buf = malloc (len, M_TEMP, M_INTWAIT);
3107 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3111 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3113 addlog(" %s ", sppp_ipcp_opt_name(*p));
3115 case IPCP_OPT_COMPRESSION:
3116 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3118 case IPCP_OPT_ADDRESS:
3120 * Peer doesn't grok address option. This is
3121 * bad. XXX Should we better give up here?
3122 * XXX We could try old "addresses" option...
3124 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3135 * Analyze the IPCP Configure-NAK option list, and adjust our
3139 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3142 struct ifnet *ifp = &sp->pp_if;
3143 int debug = ifp->if_flags & IFF_DEBUG;
3148 buf = malloc (len, M_TEMP, M_INTWAIT);
3151 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3155 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3157 addlog(" %s ", sppp_ipcp_opt_name(*p));
3159 case IPCP_OPT_COMPRESSION:
3160 if (len >= 6 && p[1] == 6) {
3161 desiredcomp = p[2] << 8 | p[3];
3163 addlog("[wantcomp %#04x] ",
3165 if (desiredcomp == IPCP_COMP_VJ) {
3166 sl_compress_init(sp->pp_comp, p[4]);
3167 sp->ipcp.max_state = p[4];
3168 sp->ipcp.compress_cid = p[5];
3173 ~(1 << IPCP_OPT_COMPRESSION);
3176 case IPCP_OPT_ADDRESS:
3178 * Peer doesn't like our local IP address. See
3179 * if we can do something for him. We'll drop
3180 * him our address then.
3182 if (len >= 6 && p[1] == 6) {
3183 wantaddr = p[2] << 24 | p[3] << 16 |
3185 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3187 addlog("[wantaddr %s] ",
3188 sppp_dotted_quad(wantaddr));
3190 * When doing dynamic address assignment,
3191 * we accept his offer. Otherwise, we
3192 * ignore it and thus continue to negotiate
3193 * our already existing value.
3194 * XXX: Bogus, if he said no once, he'll
3195 * just say no again, might as well die.
3197 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3198 sppp_set_ip_addr(sp, wantaddr);
3201 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3214 sppp_ipcp_tlu(struct sppp *sp)
3216 /* we are up - notify isdn daemon */
3222 sppp_ipcp_tld(struct sppp *sp)
3227 sppp_ipcp_tls(struct sppp *sp)
3229 /* indicate to LCP that it must stay alive */
3230 sp->lcp.protos |= (1 << IDX_IPCP);
3234 sppp_ipcp_tlf(struct sppp *sp)
3236 /* we no longer need LCP */
3237 sp->lcp.protos &= ~(1 << IDX_IPCP);
3238 sppp_lcp_check_and_close(sp);
3242 sppp_ipcp_scr(struct sppp *sp)
3244 char opt[6 /* compression */ + 6 /* address */];
3248 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3249 opt[i++] = IPCP_OPT_COMPRESSION;
3251 opt[i++] = IPCP_COMP_VJ >> 8;
3252 opt[i++] = IPCP_COMP_VJ;
3253 opt[i++] = sp->ipcp.max_state;
3254 opt[i++] = sp->ipcp.compress_cid;
3256 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3257 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3258 opt[i++] = IPCP_OPT_ADDRESS;
3260 opt[i++] = ouraddr >> 24;
3261 opt[i++] = ouraddr >> 16;
3262 opt[i++] = ouraddr >> 8;
3266 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3267 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3271 *--------------------------------------------------------------------------*
3273 * The IPv6CP implementation. *
3275 *--------------------------------------------------------------------------*
3280 sppp_ipv6cp_init(struct sppp *sp)
3282 sp->ipv6cp.opts = 0;
3283 sp->ipv6cp.flags = 0;
3284 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3285 sp->fail_counter[IDX_IPV6CP] = 0;
3286 sp->pp_seq[IDX_IPV6CP] = 0;
3287 sp->pp_rseq[IDX_IPV6CP] = 0;
3288 #if defined(__NetBSD__)
3289 callout_init(&sp->ch[IDX_IPV6CP]);
3291 #if defined(__DragonFly__)
3292 callout_init(&sp->timeout[IDX_IPV6CP]);
3297 sppp_ipv6cp_up(struct sppp *sp)
3299 sppp_up_event(&ipv6cp, sp);
3303 sppp_ipv6cp_down(struct sppp *sp)
3305 sppp_down_event(&ipv6cp, sp);
3309 sppp_ipv6cp_open(struct sppp *sp)
3312 struct in6_addr myaddr, hisaddr;
3314 #ifdef IPV6CP_MYIFID_DYN
3315 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3317 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3320 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3322 * If we don't have our address, this probably means our
3323 * interface doesn't want to talk IPv6 at all. (This could
3324 * be the case if somebody wants to speak only IPX, for
3325 * example.) Don't open IPv6CP in this case.
3327 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3328 /* XXX this message should go away */
3330 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3335 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3336 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3337 sppp_open_event(&ipv6cp, sp);
3341 sppp_ipv6cp_close(struct sppp *sp)
3343 sppp_close_event(&ipv6cp, sp);
3347 sppp_ipv6cp_TO(void *cookie)
3349 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3353 * Analyze a configure request. Return true if it was agreeable, and
3354 * caused action sca, false if it has been rejected or nak'ed, and
3355 * caused action scn. (The return value is used to make the state
3356 * transition decision in the state automaton.)
3359 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3361 u_char *buf, *r, *p;
3362 struct ifnet *ifp = &sp->pp_if;
3363 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3364 struct in6_addr myaddr, desiredaddr, suggestaddr;
3367 int collision, nohisaddr;
3372 * Make sure to allocate a buf that can at least hold a
3373 * conf-nak with an `address' option. We might need it below.
3375 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_INTWAIT);
3377 /* pass 1: see if we can recognize them */
3379 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3383 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3385 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3387 case IPV6CP_OPT_IFID:
3388 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3389 /* correctly formed address option */
3394 addlog(" [invalid]");
3397 case IPV6CP_OPT_COMPRESSION:
3398 if (len >= 4 && p[1] >= 4) {
3399 /* correctly formed compress option */
3403 addlog(" [invalid]");
3407 /* Others not supported. */
3412 /* Add the option to rejected list. */
3419 addlog(" send conf-rej\n");
3420 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3425 /* pass 2: parse option values */
3426 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3428 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3433 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3435 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3438 case IPV6CP_OPT_COMPRESSION:
3441 case IPV6CP_OPT_IFID:
3442 bzero(&desiredaddr, sizeof(desiredaddr));
3443 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3444 collision = (bcmp(&desiredaddr.s6_addr[8],
3445 &myaddr.s6_addr[8], 8) == 0);
3446 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3448 desiredaddr.s6_addr16[0] = htons(0xfe80);
3449 desiredaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3451 if (!collision && !nohisaddr) {
3452 /* no collision, hisaddr known - Conf-Ack */
3457 ip6_sprintf(&desiredaddr),
3458 sppp_cp_type_name(type));
3463 bzero(&suggestaddr, sizeof(&suggestaddr));
3464 if (collision && nohisaddr) {
3465 /* collision, hisaddr unknown - Conf-Rej */
3470 * - no collision, hisaddr unknown, or
3471 * - collision, hisaddr known
3472 * Conf-Nak, suggest hisaddr
3475 sppp_suggest_ip6_addr(sp, &suggestaddr);
3476 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3479 addlog(" %s [%s]", ip6_sprintf(&desiredaddr),
3480 sppp_cp_type_name(type));
3483 /* Add the option to nak'ed list. */
3489 if (rlen == 0 && type == CONF_ACK) {
3491 addlog(" send %s\n", sppp_cp_type_name(type));
3492 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3495 if (type == CONF_ACK)
3496 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3500 addlog(" send %s suggest %s\n",
3501 sppp_cp_type_name(type), ip6_sprintf(&suggestaddr));
3503 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3512 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3516 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3519 struct ifnet *ifp = &sp->pp_if;
3520 int debug = ifp->if_flags & IFF_DEBUG;
3523 buf = malloc (len, M_TEMP, M_INTWAIT);
3526 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3530 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3532 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3534 case IPV6CP_OPT_IFID:
3536 * Peer doesn't grok address option. This is
3537 * bad. XXX Should we better give up here?
3539 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3542 case IPV6CP_OPT_COMPRESS:
3543 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3555 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3559 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3562 struct ifnet *ifp = &sp->pp_if;
3563 int debug = ifp->if_flags & IFF_DEBUG;
3564 struct in6_addr suggestaddr;
3567 buf = malloc (len, M_TEMP, M_INTWAIT);
3570 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3574 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3576 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3578 case IPV6CP_OPT_IFID:
3580 * Peer doesn't like our local ifid. See
3581 * if we can do something for him. We'll drop
3582 * him our address then.
3584 if (len < 10 || p[1] != 10)
3586 bzero(&suggestaddr, sizeof(suggestaddr));
3587 suggestaddr.s6_addr16[0] = htons(0xfe80);
3588 suggestaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3589 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3591 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3593 addlog(" [suggestaddr %s]",
3594 ip6_sprintf(&suggestaddr));
3595 #ifdef IPV6CP_MYIFID_DYN
3597 * When doing dynamic address assignment,
3598 * we accept his offer.
3600 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3601 struct in6_addr lastsuggest;
3603 * If <suggested myaddr from peer> equals to
3604 * <hisaddr we have suggested last time>,
3605 * we have a collision. generate new random
3608 sppp_suggest_ip6_addr(&lastsuggest);
3609 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3612 addlog(" [random]");
3613 sppp_gen_ip6_addr(sp, &suggestaddr);
3615 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3618 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3622 * Since we do not do dynamic address assignment,
3623 * we ignore it and thus continue to negotiate
3624 * our already existing value. This can possibly
3625 * go into infinite request-reject loop.
3627 * This is not likely because we normally use
3628 * ifid based on MAC-address.
3629 * If you have no ethernet card on the node, too bad.
3630 * XXX should we use fail_counter?
3635 case IPV6CP_OPT_COMPRESS:
3637 * Peer wants different compression parameters.
3649 sppp_ipv6cp_tlu(struct sppp *sp)
3651 /* we are up - notify isdn daemon */
3657 sppp_ipv6cp_tld(struct sppp *sp)
3662 sppp_ipv6cp_tls(struct sppp *sp)
3664 /* indicate to LCP that it must stay alive */
3665 sp->lcp.protos |= (1 << IDX_IPV6CP);
3669 sppp_ipv6cp_tlf(struct sppp *sp)
3672 #if 0 /* need #if 0 to close IPv6CP properly */
3673 /* we no longer need LCP */
3674 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3675 sppp_lcp_check_and_close(sp);
3680 sppp_ipv6cp_scr(struct sppp *sp)
3682 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3683 struct in6_addr ouraddr;
3686 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3687 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3688 opt[i++] = IPV6CP_OPT_IFID;
3690 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3695 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3696 opt[i++] = IPV6CP_OPT_COMPRESSION;
3698 opt[i++] = 0; /* TBD */
3699 opt[i++] = 0; /* TBD */
3700 /* variable length data may follow */
3704 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3705 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3708 static void sppp_ipv6cp_init(struct sppp *sp)
3712 static void sppp_ipv6cp_up(struct sppp *sp)
3716 static void sppp_ipv6cp_down(struct sppp *sp)
3721 static void sppp_ipv6cp_open(struct sppp *sp)
3725 static void sppp_ipv6cp_close(struct sppp *sp)
3729 static void sppp_ipv6cp_TO(void *sp)
3733 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3738 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3742 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3746 static void sppp_ipv6cp_tlu(struct sppp *sp)
3750 static void sppp_ipv6cp_tld(struct sppp *sp)
3754 static void sppp_ipv6cp_tls(struct sppp *sp)
3758 static void sppp_ipv6cp_tlf(struct sppp *sp)
3762 static void sppp_ipv6cp_scr(struct sppp *sp)
3768 *--------------------------------------------------------------------------*
3770 * The CHAP implementation. *
3772 *--------------------------------------------------------------------------*
3776 * The authentication protocols don't employ a full-fledged state machine as
3777 * the control protocols do, since they do have Open and Close events, but
3778 * not Up and Down, nor are they explicitly terminated. Also, use of the
3779 * authentication protocols may be different in both directions (this makes
3780 * sense, think of a machine that never accepts incoming calls but only
3781 * calls out, it doesn't require the called party to authenticate itself).
3783 * Our state machine for the local authentication protocol (we are requesting
3784 * the peer to authenticate) looks like:
3787 * +--------------------------------------------+
3789 * +--------+ Close +---------+ RCA+
3790 * | |<----------------------------------| |------+
3791 * +--->| Closed | TO* | Opened | sca |
3792 * | | |-----+ +-------| |<-----+
3793 * | +--------+ irc | | +---------+
3799 * | | +------->+ | |
3801 * | +--------+ V | |
3802 * | | |<----+<--------------------+ |
3808 * +------+ +------------------------------------------+
3809 * scn,tld sca,irc,ict,tlu
3814 * Open: LCP reached authentication phase
3815 * Close: LCP reached terminate phase
3817 * RCA+: received reply (pap-req, chap-response), acceptable
3818 * RCN: received reply (pap-req, chap-response), not acceptable
3819 * TO+: timeout with restart counter >= 0
3820 * TO-: timeout with restart counter < 0
3821 * TO*: reschedule timeout for CHAP
3823 * scr: send request packet (none for PAP, chap-challenge)
3824 * sca: send ack packet (pap-ack, chap-success)
3825 * scn: send nak packet (pap-nak, chap-failure)
3826 * ict: initialize re-challenge timer (CHAP only)
3828 * tlu: this-layer-up, LCP reaches network phase
3829 * tld: this-layer-down, LCP enters terminate phase
3831 * Note that in CHAP mode, after sending a new challenge, while the state
3832 * automaton falls back into Req-Sent state, it doesn't signal a tld
3833 * event to LCP, so LCP remains in network phase. Only after not getting
3834 * any response (or after getting an unacceptable response), CHAP closes,
3835 * causing LCP to enter terminate phase.
3837 * With PAP, there is no initial request that can be sent. The peer is
3838 * expected to send one based on the successful negotiation of PAP as
3839 * the authentication protocol during the LCP option negotiation.
3841 * Incoming authentication protocol requests (remote requests
3842 * authentication, we are peer) don't employ a state machine at all,
3843 * they are simply answered. Some peers [Ascend P50 firmware rev
3844 * 4.50] react allergically when sending IPCP requests while they are
3845 * still in authentication phase (thereby violating the standard that
3846 * demands that these NCP packets are to be discarded), so we keep
3847 * track of the peer demanding us to authenticate, and only proceed to
3848 * phase network once we've seen a positive acknowledge for the
3853 * Handle incoming CHAP packets.
3856 sppp_chap_input(struct sppp *sp, struct mbuf *m)
3859 struct lcp_header *h;
3861 u_char *value, *name, digest[AUTHKEYLEN], dsize;
3862 int value_len, name_len;
3865 len = m->m_pkthdr.len;
3869 SPP_FMT "chap invalid packet length: %d bytes\n",
3870 SPP_ARGS(ifp), len);
3873 h = mtod (m, struct lcp_header*);
3874 if (len > ntohs (h->len))
3875 len = ntohs (h->len);
3878 /* challenge, failure and success are his authproto */
3879 case CHAP_CHALLENGE:
3880 value = 1 + (u_char*)(h+1);
3881 value_len = value[-1];
3882 name = value + value_len;
3883 name_len = len - value_len - 5;
3887 SPP_FMT "chap corrupted challenge "
3888 "<%s id=0x%x len=%d",
3890 sppp_auth_type_name(PPP_CHAP, h->type),
3891 h->ident, ntohs(h->len));
3892 sppp_print_bytes((u_char*) (h+1), len-4);
3900 SPP_FMT "chap input <%s id=0x%x len=%d name=",
3902 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
3904 sppp_print_string((char*) name, name_len);
3905 addlog(" value-size=%d value=", value_len);
3906 sppp_print_bytes(value, value_len);
3910 /* Compute reply value. */
3912 MD5Update(&ctx, &h->ident, 1);
3913 MD5Update(&ctx, sp->myauth.secret,
3914 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
3915 MD5Update(&ctx, value, value_len);
3916 MD5Final(digest, &ctx);
3917 dsize = sizeof digest;
3919 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
3920 sizeof dsize, (const char *)&dsize,
3921 sizeof digest, digest,
3922 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3929 log(LOG_DEBUG, SPP_FMT "chap success",
3933 sppp_print_string((char*)(h + 1), len - 4);
3938 sp->pp_flags &= ~PP_NEEDAUTH;
3939 if (sp->myauth.proto == PPP_CHAP &&
3940 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
3941 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
3943 * We are authenticator for CHAP but didn't
3944 * complete yet. Leave it to tlu to proceed
3951 sppp_phase_network(sp);
3956 log(LOG_INFO, SPP_FMT "chap failure",
3960 sppp_print_string((char*)(h + 1), len - 4);
3964 log(LOG_INFO, SPP_FMT "chap failure\n",
3966 /* await LCP shutdown by authenticator */
3969 /* response is my authproto */
3971 value = 1 + (u_char*)(h+1);
3972 value_len = value[-1];
3973 name = value + value_len;
3974 name_len = len - value_len - 5;
3978 SPP_FMT "chap corrupted response "
3979 "<%s id=0x%x len=%d",
3981 sppp_auth_type_name(PPP_CHAP, h->type),
3982 h->ident, ntohs(h->len));
3983 sppp_print_bytes((u_char*)(h+1), len-4);
3988 if (h->ident != sp->confid[IDX_CHAP]) {
3991 SPP_FMT "chap dropping response for old ID "
3992 "(got %d, expected %d)\n",
3994 h->ident, sp->confid[IDX_CHAP]);
3997 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
3998 || bcmp(name, sp->hisauth.name, name_len) != 0) {
3999 log(LOG_INFO, SPP_FMT "chap response, his name ",
4001 sppp_print_string(name, name_len);
4002 addlog(" != expected ");
4003 sppp_print_string(sp->hisauth.name,
4004 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4008 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4009 "<%s id=0x%x len=%d name=",
4011 sppp_state_name(sp->state[IDX_CHAP]),
4012 sppp_auth_type_name(PPP_CHAP, h->type),
4013 h->ident, ntohs (h->len));
4014 sppp_print_string((char*)name, name_len);
4015 addlog(" value-size=%d value=", value_len);
4016 sppp_print_bytes(value, value_len);
4019 if (value_len != AUTHKEYLEN) {
4022 SPP_FMT "chap bad hash value length: "
4023 "%d bytes, should be %d\n",
4024 SPP_ARGS(ifp), value_len,
4030 MD5Update(&ctx, &h->ident, 1);
4031 MD5Update(&ctx, sp->hisauth.secret,
4032 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4033 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4034 MD5Final(digest, &ctx);
4036 #define FAILMSG "Failed..."
4037 #define SUCCMSG "Welcome!"
4039 if (value_len != sizeof digest ||
4040 bcmp(digest, value, value_len) != 0) {
4041 /* action scn, tld */
4042 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4043 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4048 /* action sca, perhaps tlu */
4049 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4050 sp->state[IDX_CHAP] == STATE_OPENED)
4051 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4052 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4054 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4055 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4061 /* Unknown CHAP packet type -- ignore. */
4063 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4064 "<0x%x id=0x%xh len=%d",
4066 sppp_state_name(sp->state[IDX_CHAP]),
4067 h->type, h->ident, ntohs(h->len));
4068 sppp_print_bytes((u_char*)(h+1), len-4);
4077 sppp_chap_init(struct sppp *sp)
4079 /* Chap doesn't have STATE_INITIAL at all. */
4080 sp->state[IDX_CHAP] = STATE_CLOSED;
4081 sp->fail_counter[IDX_CHAP] = 0;
4082 sp->pp_seq[IDX_CHAP] = 0;
4083 sp->pp_rseq[IDX_CHAP] = 0;
4084 #if defined(__DragonFly__)
4085 callout_init(&sp->timeout[IDX_CHAP]);
4090 sppp_chap_open(struct sppp *sp)
4092 if (sp->myauth.proto == PPP_CHAP &&
4093 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4094 /* we are authenticator for CHAP, start it */
4096 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4097 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4099 /* nothing to be done if we are peer, await a challenge */
4103 sppp_chap_close(struct sppp *sp)
4105 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4106 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4110 sppp_chap_TO(void *cookie)
4112 struct sppp *sp = (struct sppp *)cookie;
4118 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4120 sppp_state_name(sp->state[IDX_CHAP]),
4121 sp->rst_counter[IDX_CHAP]);
4123 if (--sp->rst_counter[IDX_CHAP] < 0)
4125 switch (sp->state[IDX_CHAP]) {
4126 case STATE_REQ_SENT:
4128 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4132 /* TO+ (or TO*) event */
4133 switch (sp->state[IDX_CHAP]) {
4136 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4138 case STATE_REQ_SENT:
4140 /* sppp_cp_change_state() will restart the timer */
4141 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4149 sppp_chap_tlu(struct sppp *sp)
4155 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4158 * Some broken CHAP implementations (Conware CoNet, firmware
4159 * 4.0.?) don't want to re-authenticate their CHAP once the
4160 * initial challenge-response exchange has taken place.
4161 * Provide for an option to avoid rechallenges.
4163 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4165 * Compute the re-challenge timeout. This will yield
4166 * a number between 300 and 810 seconds.
4168 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
4169 callout_reset(&sp->timeout[IDX_CHAP], i * hz, chap.TO, sp);
4174 SPP_FMT "chap %s, ",
4176 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4177 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4178 addlog("next re-challenge in %d seconds\n", i);
4180 addlog("re-challenging supressed\n");
4184 /* indicate to LCP that we need to be closed down */
4185 sp->lcp.protos |= (1 << IDX_CHAP);
4187 if (sp->pp_flags & PP_NEEDAUTH) {
4189 * Remote is authenticator, but his auth proto didn't
4190 * complete yet. Defer the transition to network
4199 * If we are already in phase network, we are done here. This
4200 * is the case if this is a dummy tlu event after a re-challenge.
4202 if (sp->pp_phase != PHASE_NETWORK)
4203 sppp_phase_network(sp);
4207 sppp_chap_tld(struct sppp *sp)
4212 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4213 callout_stop(&sp->timeout[IDX_CHAP]);
4214 sp->lcp.protos &= ~(1 << IDX_CHAP);
4220 sppp_chap_scr(struct sppp *sp)
4225 /* Compute random challenge. */
4226 ch = (u_long *)sp->myauth.challenge;
4227 #if defined(__DragonFly__)
4228 read_random(&seed, sizeof seed);
4233 seed = tv.tv_sec ^ tv.tv_usec;
4236 ch[0] = seed ^ random();
4237 ch[1] = seed ^ random();
4238 ch[2] = seed ^ random();
4239 ch[3] = seed ^ random();
4242 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4244 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4245 sizeof clen, (const char *)&clen,
4246 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4247 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4253 *--------------------------------------------------------------------------*
4255 * The PAP implementation. *
4257 *--------------------------------------------------------------------------*
4260 * For PAP, we need to keep a little state also if we are the peer, not the
4261 * authenticator. This is since we don't get a request to authenticate, but
4262 * have to repeatedly authenticate ourself until we got a response (or the
4263 * retry counter is expired).
4267 * Handle incoming PAP packets. */
4269 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4272 struct lcp_header *h;
4274 u_char *name, *passwd, mlen;
4275 int name_len, passwd_len;
4277 len = m->m_pkthdr.len;
4281 SPP_FMT "pap invalid packet length: %d bytes\n",
4282 SPP_ARGS(ifp), len);
4285 h = mtod (m, struct lcp_header*);
4286 if (len > ntohs (h->len))
4287 len = ntohs (h->len);
4289 /* PAP request is my authproto */
4291 name = 1 + (u_char*)(h+1);
4292 name_len = name[-1];
4293 passwd = name + name_len + 1;
4294 if (name_len > len - 6 ||
4295 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4297 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4298 "<%s id=0x%x len=%d",
4300 sppp_auth_type_name(PPP_PAP, h->type),
4301 h->ident, ntohs(h->len));
4302 sppp_print_bytes((u_char*)(h+1), len-4);
4308 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4309 "<%s id=0x%x len=%d name=",
4311 sppp_state_name(sp->state[IDX_PAP]),
4312 sppp_auth_type_name(PPP_PAP, h->type),
4313 h->ident, ntohs(h->len));
4314 sppp_print_string((char*)name, name_len);
4316 sppp_print_string((char*)passwd, passwd_len);
4319 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4320 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4321 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4322 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4323 /* action scn, tld */
4324 mlen = sizeof(FAILMSG) - 1;
4325 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4326 sizeof mlen, (const char *)&mlen,
4327 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4332 /* action sca, perhaps tlu */
4333 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4334 sp->state[IDX_PAP] == STATE_OPENED) {
4335 mlen = sizeof(SUCCMSG) - 1;
4336 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4337 sizeof mlen, (const char *)&mlen,
4338 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4341 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4342 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4347 /* ack and nak are his authproto */
4349 callout_stop(&sp->pap_my_to);
4351 log(LOG_DEBUG, SPP_FMT "pap success",
4353 name_len = *((char *)h);
4354 if (len > 5 && name_len) {
4356 sppp_print_string((char*)(h+1), name_len);
4361 sp->pp_flags &= ~PP_NEEDAUTH;
4362 if (sp->myauth.proto == PPP_PAP &&
4363 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4364 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4366 * We are authenticator for PAP but didn't
4367 * complete yet. Leave it to tlu to proceed
4374 sppp_phase_network(sp);
4378 callout_stop(&sp->pap_my_to);
4380 log(LOG_INFO, SPP_FMT "pap failure",
4382 name_len = *((char *)h);
4383 if (len > 5 && name_len) {
4385 sppp_print_string((char*)(h+1), name_len);
4389 log(LOG_INFO, SPP_FMT "pap failure\n",
4391 /* await LCP shutdown by authenticator */
4395 /* Unknown PAP packet type -- ignore. */
4397 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4398 "<0x%x id=0x%x len=%d",
4400 h->type, h->ident, ntohs(h->len));
4401 sppp_print_bytes((u_char*)(h+1), len-4);
4410 sppp_pap_init(struct sppp *sp)
4412 /* PAP doesn't have STATE_INITIAL at all. */
4413 sp->state[IDX_PAP] = STATE_CLOSED;
4414 sp->fail_counter[IDX_PAP] = 0;
4415 sp->pp_seq[IDX_PAP] = 0;
4416 sp->pp_rseq[IDX_PAP] = 0;
4417 #if defined(__DragonFly__)
4418 callout_init(&sp->timeout[IDX_PAP]);
4419 callout_init(&sp->pap_my_to);
4424 sppp_pap_open(struct sppp *sp)
4426 if (sp->hisauth.proto == PPP_PAP &&
4427 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4428 /* we are authenticator for PAP, start our timer */
4429 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4430 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4432 if (sp->myauth.proto == PPP_PAP) {
4433 /* we are peer, send a request, and start a timer */
4435 callout_reset(&sp->pap_my_to, sp->lcp.timeout,
4436 sppp_pap_my_TO, sp);
4441 sppp_pap_close(struct sppp *sp)
4443 if (sp->state[IDX_PAP] != STATE_CLOSED)
4444 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4448 * That's the timeout routine if we are authenticator. Since the
4449 * authenticator is basically passive in PAP, we can't do much here.
4452 sppp_pap_TO(void *cookie)
4454 struct sppp *sp = (struct sppp *)cookie;
4460 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4462 sppp_state_name(sp->state[IDX_PAP]),
4463 sp->rst_counter[IDX_PAP]);
4465 if (--sp->rst_counter[IDX_PAP] < 0)
4467 switch (sp->state[IDX_PAP]) {
4468 case STATE_REQ_SENT:
4470 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4474 /* TO+ event, not very much we could do */
4475 switch (sp->state[IDX_PAP]) {
4476 case STATE_REQ_SENT:
4477 /* sppp_cp_change_state() will restart the timer */
4478 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4486 * That's the timeout handler if we are peer. Since the peer is active,
4487 * we need to retransmit our PAP request since it is apparently lost.
4488 * XXX We should impose a max counter.
4491 sppp_pap_my_TO(void *cookie)
4493 struct sppp *sp = (struct sppp *)cookie;
4497 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4504 sppp_pap_tlu(struct sppp *sp)
4509 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4512 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4513 SPP_ARGS(ifp), pap.name);
4516 /* indicate to LCP that we need to be closed down */
4517 sp->lcp.protos |= (1 << IDX_PAP);
4519 if (sp->pp_flags & PP_NEEDAUTH) {
4521 * Remote is authenticator, but his auth proto didn't
4522 * complete yet. Defer the transition to network
4529 sppp_phase_network(sp);
4533 sppp_pap_tld(struct sppp *sp)
4538 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4539 callout_stop(&sp->timeout[IDX_PAP]);
4540 callout_stop(&sp->pap_my_to);
4541 sp->lcp.protos &= ~(1 << IDX_PAP);
4547 sppp_pap_scr(struct sppp *sp)
4549 u_char idlen, pwdlen;
4551 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4552 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4553 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4555 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4556 sizeof idlen, (const char *)&idlen,
4557 (size_t)idlen, sp->myauth.name,
4558 sizeof pwdlen, (const char *)&pwdlen,
4559 (size_t)pwdlen, sp->myauth.secret,
4564 * Random miscellaneous functions.
4568 * Send a PAP or CHAP proto packet.
4570 * Varadic function, each of the elements for the ellipsis is of type
4571 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4573 * NOTE: never declare variadic functions with types subject to type
4574 * promotion (i.e. u_char). This is asking for big trouble depending
4575 * on the architecture you are on...
4579 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4580 unsigned int type, unsigned int id,
4584 struct ppp_header *h;
4585 struct lcp_header *lh;
4593 MGETHDR (m, MB_DONTWAIT, MT_DATA);
4596 m->m_pkthdr.rcvif = 0;
4598 h = mtod (m, struct ppp_header*);
4599 h->address = PPP_ALLSTATIONS; /* broadcast address */
4600 h->control = PPP_UI; /* Unnumbered Info */
4601 h->protocol = htons(cp->proto);
4603 lh = (struct lcp_header*)(h + 1);
4606 p = (u_char*) (lh+1);
4611 while ((mlen = (unsigned int)__va_arg(ap, size_t)) != 0) {
4612 msg = __va_arg(ap, const char *);
4614 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4620 bcopy(msg, p, mlen);
4625 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4626 lh->len = htons (LCP_HEADER_LEN + len);
4629 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4630 SPP_ARGS(ifp), cp->name,
4631 sppp_auth_type_name(cp->proto, lh->type),
4632 lh->ident, ntohs(lh->len));
4633 sppp_print_bytes((u_char*) (lh+1), len);
4636 if (IF_QFULL (&sp->pp_cpq)) {
4637 IF_DROP (&sp->pp_fastq);
4638 IF_DROP (&ifp->if_snd);
4642 IF_ENQUEUE (&sp->pp_cpq, m);
4643 if (! (ifp->if_flags & IFF_OACTIVE))
4644 (*ifp->if_start) (ifp);
4645 ifp->if_obytes += m->m_pkthdr.len + 3;
4649 * Flush interface queue.
4652 sppp_qflush(struct ifqueue *ifq)
4667 * Send keepalive packets, every 10 seconds.
4670 sppp_keepalive(void *dummy)
4676 for (sp=spppq; sp; sp=sp->pp_next) {
4677 struct ifnet *ifp = &sp->pp_if;
4679 /* Keepalive mode disabled or channel down? */
4680 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4681 ! (ifp->if_flags & IFF_RUNNING))
4684 /* No keepalive in PPP mode if LCP not opened yet. */
4685 if (sp->pp_mode != IFF_CISCO &&
4686 sp->pp_phase < PHASE_AUTHENTICATE)
4689 if (sp->pp_alivecnt == MAXALIVECNT) {
4690 /* No keepalive packets got. Stop the interface. */
4691 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
4693 sppp_qflush (&sp->pp_cpq);
4694 if (sp->pp_mode != IFF_CISCO) {
4696 /* Shut down the PPP link. */
4698 /* Initiate negotiation. XXX */
4702 if (sp->pp_alivecnt <= MAXALIVECNT)
4704 if (sp->pp_mode == IFF_CISCO)
4705 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4706 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4707 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4708 long nmagic = htonl (sp->lcp.magic);
4709 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4710 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4711 sp->lcp.echoid, 4, &nmagic);
4715 callout_reset(&keepalive_timeout, hz * 10, sppp_keepalive, NULL);
4719 * Get both IP addresses.
4722 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4724 struct ifnet *ifp = &sp->pp_if;
4726 struct sockaddr_in *si, *sm;
4732 * Pick the first AF_INET address from the list,
4733 * aliases don't make any sense on a p2p link anyway.
4736 #if defined(__DragonFly__)
4737 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4738 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4739 for (ifa = ifp->if_addrlist.tqh_first;
4741 ifa = ifa->ifa_list.tqe_next)
4743 for (ifa = ifp->if_addrlist;
4745 ifa = ifa->ifa_next)
4747 if (ifa->ifa_addr->sa_family == AF_INET) {
4748 si = (struct sockaddr_in *)ifa->ifa_addr;
4749 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4754 if (si && si->sin_addr.s_addr) {
4755 ssrc = si->sin_addr.s_addr;
4757 *srcmask = ntohl(sm->sin_addr.s_addr);
4760 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4761 if (si && si->sin_addr.s_addr)
4762 ddst = si->sin_addr.s_addr;
4765 if (dst) *dst = ntohl(ddst);
4766 if (src) *src = ntohl(ssrc);
4770 * Set my IP address. Must be called at splimp.
4773 sppp_set_ip_addr(struct sppp *sp, u_long src)
4777 struct sockaddr_in *si;
4778 struct in_ifaddr *ia;
4781 * Pick the first AF_INET address from the list,
4782 * aliases don't make any sense on a p2p link anyway.
4785 #if defined(__DragonFly__)
4786 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4787 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4788 for (ifa = ifp->if_addrlist.tqh_first;
4790 ifa = ifa->ifa_list.tqe_next)
4792 for (ifa = ifp->if_addrlist;
4794 ifa = ifa->ifa_next)
4797 if (ifa->ifa_addr->sa_family == AF_INET)
4799 si = (struct sockaddr_in *)ifa->ifa_addr;
4808 #if __NetBSD_Version__ >= 103080000
4809 struct sockaddr_in new_sin = *si;
4811 new_sin.sin_addr.s_addr = htonl(src);
4812 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 1);
4815 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: in_ifinit "
4816 " failed, error=%d\n", SPP_ARGS(ifp), error);
4819 /* delete old route */
4820 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4823 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4824 SPP_ARGS(ifp), error);
4827 /* set new address */
4828 si->sin_addr.s_addr = htonl(src);
4830 LIST_REMOVE(ia, ia_hash);
4831 LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash);
4834 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
4837 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
4838 SPP_ARGS(ifp), error);
4846 * Get both IPv6 addresses.
4849 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4850 struct in6_addr *srcmask)
4852 struct ifnet *ifp = &sp->pp_if;
4854 struct sockaddr_in6 *si, *sm;
4855 struct in6_addr ssrc, ddst;
4858 bzero(&ssrc, sizeof(ssrc));
4859 bzero(&ddst, sizeof(ddst));
4861 * Pick the first link-local AF_INET6 address from the list,
4862 * aliases don't make any sense on a p2p link anyway.
4864 #if defined(__DragonFly__)
4866 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4867 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4868 for (ifa = ifp->if_addrlist.tqh_first, si = 0;
4870 ifa = ifa->ifa_list.tqe_next)
4872 for (ifa = ifp->if_addrlist, si = 0;
4874 ifa = ifa->ifa_next)
4876 if (ifa->ifa_addr->sa_family == AF_INET6) {
4877 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4878 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4879 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
4883 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
4884 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
4886 bcopy(&sm->sin6_addr, srcmask,
4891 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
4892 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
4893 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
4897 bcopy(&ddst, dst, sizeof(*dst));
4899 bcopy(&ssrc, src, sizeof(*src));
4902 #ifdef IPV6CP_MYIFID_DYN
4904 * Generate random ifid.
4907 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
4913 * Set my IPv6 address. Must be called at splimp.
4916 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
4920 struct sockaddr_in6 *sin6;
4923 * Pick the first link-local AF_INET6 address from the list,
4924 * aliases don't make any sense on a p2p link anyway.
4928 #if defined(__DragonFly__)
4929 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4930 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4931 for (ifa = ifp->if_addrlist.tqh_first;
4933 ifa = ifa->ifa_list.tqe_next)
4935 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
4938 if (ifa->ifa_addr->sa_family == AF_INET6)
4940 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
4941 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
4949 struct sockaddr_in6 new_sin6 = *sin6;
4951 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
4952 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
4955 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
4956 " failed, error=%d\n", SPP_ARGS(ifp), error);
4963 * Suggest a candidate address to be used by peer.
4966 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
4968 struct in6_addr myaddr;
4971 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
4973 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
4975 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
4976 myaddr.s6_addr[14] ^= 0xff;
4977 myaddr.s6_addr[15] ^= 0xff;
4979 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
4980 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
4983 bcopy(&myaddr, suggest, sizeof(myaddr));
4988 sppp_params(struct sppp *sp, u_long cmd, void *data)
4991 struct ifreq *ifr = (struct ifreq *)data;
4992 struct spppreq *spr;
4995 spr = malloc(sizeof(struct spppreq), M_TEMP, M_INTWAIT);
4998 * ifr->ifr_data is supposed to point to a struct spppreq.
4999 * Check the cmd word first before attempting to fetch all the
5002 if ((subcmd = fuword(ifr->ifr_data)) == -1) {
5007 if (copyin((caddr_t)ifr->ifr_data, spr, sizeof(struct spppreq)) != 0) {
5013 case (int)SPPPIOGDEFS:
5014 if (cmd != SIOCGIFGENERIC) {
5019 * We copy over the entire current state, but clean
5020 * out some of the stuff we don't wanna pass up.
5021 * Remember, SIOCGIFGENERIC is unprotected, and can be
5022 * called by any user. No need to ever get PAP or
5023 * CHAP secrets back to userland anyway.
5025 spr->defs.pp_phase = sp->pp_phase;
5026 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5027 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5028 spr->defs.lcp = sp->lcp;
5029 spr->defs.ipcp = sp->ipcp;
5030 spr->defs.ipv6cp = sp->ipv6cp;
5031 spr->defs.myauth = sp->myauth;
5032 spr->defs.hisauth = sp->hisauth;
5033 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5034 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5035 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5036 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5038 * Fixup the LCP timeout value to milliseconds so
5039 * spppcontrol doesn't need to bother about the value
5040 * of "hz". We do the reverse calculation below when
5043 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5044 rv = copyout(spr, (caddr_t)ifr->ifr_data,
5045 sizeof(struct spppreq));
5048 case (int)SPPPIOSDEFS:
5049 if (cmd != SIOCSIFGENERIC) {
5054 * We have a very specific idea of which fields we
5055 * allow being passed back from userland, so to not
5056 * clobber our current state. For one, we only allow
5057 * setting anything if LCP is in dead or establish
5058 * phase. Once the authentication negotiations
5059 * started, the authentication settings must not be
5060 * changed again. (The administrator can force an
5061 * ifconfig down in order to get LCP back into dead
5064 * Also, we only allow for authentication parameters to be
5067 * XXX Should allow to set or clear pp_flags.
5069 * Finally, if the respective authentication protocol to
5070 * be used is set differently than 0, but the secret is
5071 * passed as all zeros, we don't trash the existing secret.
5072 * This allows an administrator to change the system name
5073 * only without clobbering the secret (which he didn't get
5074 * back in a previous SPPPIOGDEFS call). However, the
5075 * secrets are cleared if the authentication protocol is
5077 if (sp->pp_phase != PHASE_DEAD &&
5078 sp->pp_phase != PHASE_ESTABLISH) {
5083 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5084 spr->defs.myauth.proto != PPP_CHAP) ||
5085 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5086 spr->defs.hisauth.proto != PPP_CHAP)) {
5091 if (spr->defs.myauth.proto == 0)
5092 /* resetting myauth */
5093 bzero(&sp->myauth, sizeof sp->myauth);
5095 /* setting/changing myauth */
5096 sp->myauth.proto = spr->defs.myauth.proto;
5097 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5098 if (spr->defs.myauth.secret[0] != '\0')
5099 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5102 if (spr->defs.hisauth.proto == 0)
5103 /* resetting hisauth */
5104 bzero(&sp->hisauth, sizeof sp->hisauth);
5106 /* setting/changing hisauth */
5107 sp->hisauth.proto = spr->defs.hisauth.proto;
5108 sp->hisauth.flags = spr->defs.hisauth.flags;
5109 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5110 if (spr->defs.hisauth.secret[0] != '\0')
5111 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5114 /* set LCP restart timer timeout */
5115 if (spr->defs.lcp.timeout != 0)
5116 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5117 /* set VJ enable and IPv6 disable flags */
5119 if (spr->defs.enable_vj)
5120 sp->confflags |= CONF_ENABLE_VJ;
5122 sp->confflags &= ~CONF_ENABLE_VJ;
5125 if (spr->defs.enable_ipv6)
5126 sp->confflags |= CONF_ENABLE_IPV6;
5128 sp->confflags &= ~CONF_ENABLE_IPV6;
5143 sppp_phase_network(struct sppp *sp)
5149 sp->pp_phase = PHASE_NETWORK;
5152 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5153 sppp_phase_name(sp->pp_phase));
5155 /* Notify NCPs now. */
5156 for (i = 0; i < IDX_COUNT; i++)
5157 if ((cps[i])->flags & CP_NCP)
5160 /* Send Up events to all NCPs. */
5161 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5162 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5165 /* if no NCP is starting, all this was in vain, close down */
5166 sppp_lcp_check_and_close(sp);
5171 sppp_cp_type_name(u_char type)
5173 static char buf[12];
5175 case CONF_REQ: return "conf-req";
5176 case CONF_ACK: return "conf-ack";
5177 case CONF_NAK: return "conf-nak";
5178 case CONF_REJ: return "conf-rej";
5179 case TERM_REQ: return "term-req";
5180 case TERM_ACK: return "term-ack";
5181 case CODE_REJ: return "code-rej";
5182 case PROTO_REJ: return "proto-rej";
5183 case ECHO_REQ: return "echo-req";
5184 case ECHO_REPLY: return "echo-reply";
5185 case DISC_REQ: return "discard-req";
5187 snprintf (buf, sizeof(buf), "cp/0x%x", type);
5192 sppp_auth_type_name(u_short proto, u_char type)
5194 static char buf[12];
5198 case CHAP_CHALLENGE: return "challenge";
5199 case CHAP_RESPONSE: return "response";
5200 case CHAP_SUCCESS: return "success";
5201 case CHAP_FAILURE: return "failure";
5205 case PAP_REQ: return "req";
5206 case PAP_ACK: return "ack";
5207 case PAP_NAK: return "nak";
5210 snprintf (buf, sizeof(buf), "auth/0x%x", type);
5215 sppp_lcp_opt_name(u_char opt)
5217 static char buf[12];
5219 case LCP_OPT_MRU: return "mru";
5220 case LCP_OPT_ASYNC_MAP: return "async-map";
5221 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5222 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5223 case LCP_OPT_MAGIC: return "magic";
5224 case LCP_OPT_PROTO_COMP: return "proto-comp";
5225 case LCP_OPT_ADDR_COMP: return "addr-comp";
5227 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5232 sppp_ipcp_opt_name(u_char opt)
5234 static char buf[12];
5236 case IPCP_OPT_ADDRESSES: return "addresses";
5237 case IPCP_OPT_COMPRESSION: return "compression";
5238 case IPCP_OPT_ADDRESS: return "address";
5240 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5246 sppp_ipv6cp_opt_name(u_char opt)
5248 static char buf[12];
5250 case IPV6CP_OPT_IFID: return "ifid";
5251 case IPV6CP_OPT_COMPRESSION: return "compression";
5253 sprintf (buf, "0x%x", opt);
5259 sppp_state_name(int state)
5262 case STATE_INITIAL: return "initial";
5263 case STATE_STARTING: return "starting";
5264 case STATE_CLOSED: return "closed";
5265 case STATE_STOPPED: return "stopped";
5266 case STATE_CLOSING: return "closing";
5267 case STATE_STOPPING: return "stopping";
5268 case STATE_REQ_SENT: return "req-sent";
5269 case STATE_ACK_RCVD: return "ack-rcvd";
5270 case STATE_ACK_SENT: return "ack-sent";
5271 case STATE_OPENED: return "opened";
5277 sppp_phase_name(enum ppp_phase phase)
5280 case PHASE_DEAD: return "dead";
5281 case PHASE_ESTABLISH: return "establish";
5282 case PHASE_TERMINATE: return "terminate";
5283 case PHASE_AUTHENTICATE: return "authenticate";
5284 case PHASE_NETWORK: return "network";
5290 sppp_proto_name(u_short proto)
5292 static char buf[12];
5294 case PPP_LCP: return "lcp";
5295 case PPP_IPCP: return "ipcp";
5296 case PPP_PAP: return "pap";
5297 case PPP_CHAP: return "chap";
5298 case PPP_IPV6CP: return "ipv6cp";
5300 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5305 sppp_print_bytes(const u_char *p, u_short len)
5308 addlog(" %*D", len, p, "-");
5312 sppp_print_string(const char *p, u_short len)
5319 * Print only ASCII chars directly. RFC 1994 recommends
5320 * using only them, but we don't rely on it. */
5321 if (c < ' ' || c > '~')
5329 sppp_dotted_quad(u_long addr)
5332 sprintf(s, "%d.%d.%d.%d",
5333 (int)((addr >> 24) & 0xff),
5334 (int)((addr >> 16) & 0xff),
5335 (int)((addr >> 8) & 0xff),
5336 (int)(addr & 0xff));
5341 sppp_strnlen(u_char *p, int max)
5345 for (len = 0; len < max && *p; ++p)
5350 /* a dummy, used to drop uninteresting events */
5352 sppp_null(struct sppp *unused)
5354 /* do just nothing */