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.15 2004/06/04 07:45:46 hmp 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 #if defined(__DragonFly__)
101 # define UNTIMEOUT(fun, arg, handle) untimeout(fun, arg, handle)
102 # define TIMEOUT(fun, arg1, arg2, handle) handle = timeout(fun, arg1, arg2)
103 # define IOCTL_CMD_T u_long
105 # define UNTIMEOUT(fun, arg, handle) untimeout(fun, arg)
106 # define TIMEOUT(fun, arg1, arg2, handle) timeout(fun, arg1, arg2)
107 # define IOCTL_CMD_T int
110 #define MAXALIVECNT 3 /* max. alive packets */
113 * Interface flags that can be set in an ifconfig command.
115 * Setting link0 will make the link passive, i.e. it will be marked
116 * as being administrative openable, but won't be opened to begin
117 * with. Incoming calls will be answered, or subsequent calls with
118 * -link1 will cause the administrative open of the LCP layer.
120 * Setting link1 will cause the link to auto-dial only as packets
123 * Setting IFF_DEBUG will syslog the option negotiation and state
124 * transitions at level kern.debug. Note: all logs consistently look
127 * <if-name><unit>: <proto-name> <additional info...>
129 * with <if-name><unit> being something like "bppp0", and <proto-name>
130 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
133 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
134 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
135 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
137 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
138 #define PPP_UI 0x03 /* Unnumbered Information */
139 #define PPP_IP 0x0021 /* Internet Protocol */
140 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
141 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
142 #define PPP_IPX 0x002b /* Novell IPX Protocol */
143 #define PPP_VJ_COMP 0x002d /* VJ compressed TCP/IP */
144 #define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */
145 #define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */
146 #define PPP_LCP 0xc021 /* Link Control Protocol */
147 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
148 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
149 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
150 #define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */
152 #define CONF_REQ 1 /* PPP configure request */
153 #define CONF_ACK 2 /* PPP configure acknowledge */
154 #define CONF_NAK 3 /* PPP configure negative ack */
155 #define CONF_REJ 4 /* PPP configure reject */
156 #define TERM_REQ 5 /* PPP terminate request */
157 #define TERM_ACK 6 /* PPP terminate acknowledge */
158 #define CODE_REJ 7 /* PPP code reject */
159 #define PROTO_REJ 8 /* PPP protocol reject */
160 #define ECHO_REQ 9 /* PPP echo request */
161 #define ECHO_REPLY 10 /* PPP echo reply */
162 #define DISC_REQ 11 /* PPP discard request */
164 #define LCP_OPT_MRU 1 /* maximum receive unit */
165 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
166 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
167 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
168 #define LCP_OPT_MAGIC 5 /* magic number */
169 #define LCP_OPT_RESERVED 6 /* reserved */
170 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
171 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
173 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
174 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
175 #define IPCP_OPT_ADDRESS 3 /* local IP address */
177 #define IPV6CP_OPT_IFID 1 /* interface identifier */
178 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
180 #define IPCP_COMP_VJ 0x2d /* Code for VJ compression */
182 #define PAP_REQ 1 /* PAP name/password request */
183 #define PAP_ACK 2 /* PAP acknowledge */
184 #define PAP_NAK 3 /* PAP fail */
186 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
187 #define CHAP_RESPONSE 2 /* CHAP challenge response */
188 #define CHAP_SUCCESS 3 /* CHAP response ok */
189 #define CHAP_FAILURE 4 /* CHAP response failed */
191 #define CHAP_MD5 5 /* hash algorithm - MD5 */
193 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
194 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
195 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
196 #define CISCO_ADDR_REQ 0 /* Cisco address request */
197 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
198 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
200 /* states are named and numbered according to RFC 1661 */
201 #define STATE_INITIAL 0
202 #define STATE_STARTING 1
203 #define STATE_CLOSED 2
204 #define STATE_STOPPED 3
205 #define STATE_CLOSING 4
206 #define STATE_STOPPING 5
207 #define STATE_REQ_SENT 6
208 #define STATE_ACK_RCVD 7
209 #define STATE_ACK_SENT 8
210 #define STATE_OPENED 9
216 } __attribute__((__packed__));
217 #define PPP_HEADER_LEN sizeof (struct ppp_header)
223 } __attribute__((__packed__));
224 #define LCP_HEADER_LEN sizeof (struct lcp_header)
226 struct cisco_packet {
233 } __attribute__((__packed__));
234 #define CISCO_PACKET_LEN sizeof (struct cisco_packet)
237 * We follow the spelling and capitalization of RFC 1661 here, to make
238 * it easier comparing with the standard. Please refer to this RFC in
239 * case you can't make sense out of these abbreviation; it will also
240 * explain the semantics related to the various events and actions.
243 u_short proto; /* PPP control protocol number */
244 u_char protoidx; /* index into state table in struct sppp */
246 #define CP_LCP 0x01 /* this is the LCP */
247 #define CP_AUTH 0x02 /* this is an authentication protocol */
248 #define CP_NCP 0x04 /* this is a NCP */
249 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
250 const char *name; /* name of this control protocol */
252 void (*Up)(struct sppp *sp);
253 void (*Down)(struct sppp *sp);
254 void (*Open)(struct sppp *sp);
255 void (*Close)(struct sppp *sp);
256 void (*TO)(void *sp);
257 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
258 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
259 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
261 void (*tlu)(struct sppp *sp);
262 void (*tld)(struct sppp *sp);
263 void (*tls)(struct sppp *sp);
264 void (*tlf)(struct sppp *sp);
265 void (*scr)(struct sppp *sp);
268 static struct sppp *spppq;
269 #if defined(__DragonFly__)
270 static struct callout_handle keepalive_ch;
273 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 && !defined(__DragonFly__)
274 #define SPP_FMT "%s%d: "
275 #define SPP_ARGS(ifp) (ifp)->if_name, (ifp)->if_unit
277 #define SPP_FMT "%s: "
278 #define SPP_ARGS(ifp) (ifp)->if_xname
283 * The following disgusting hack gets around the problem that IP TOS
284 * can't be set yet. We want to put "interactive" traffic on a high
285 * priority queue. To decide if traffic is interactive, we check that
286 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
288 * XXX is this really still necessary? - joerg -
290 static u_short interactive_ports[8] = {
294 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
297 /* almost every function needs these */
299 struct ifnet *ifp = &sp->pp_if; \
300 int debug = ifp->if_flags & IFF_DEBUG
302 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
303 struct sockaddr *dst, struct rtentry *rt);
305 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
306 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
308 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
310 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
311 u_char ident, u_short len, void *data);
312 /* static void sppp_cp_timeout(void *arg); */
313 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
315 static void sppp_auth_send(const struct cp *cp,
316 struct sppp *sp, unsigned int type, unsigned int id,
319 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
320 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
321 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
322 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
323 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
325 static void sppp_null(struct sppp *sp);
327 static void sppp_lcp_init(struct sppp *sp);
328 static void sppp_lcp_up(struct sppp *sp);
329 static void sppp_lcp_down(struct sppp *sp);
330 static void sppp_lcp_open(struct sppp *sp);
331 static void sppp_lcp_close(struct sppp *sp);
332 static void sppp_lcp_TO(void *sp);
333 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
334 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
335 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
336 static void sppp_lcp_tlu(struct sppp *sp);
337 static void sppp_lcp_tld(struct sppp *sp);
338 static void sppp_lcp_tls(struct sppp *sp);
339 static void sppp_lcp_tlf(struct sppp *sp);
340 static void sppp_lcp_scr(struct sppp *sp);
341 static void sppp_lcp_check_and_close(struct sppp *sp);
342 static int sppp_ncp_check(struct sppp *sp);
344 static void sppp_ipcp_init(struct sppp *sp);
345 static void sppp_ipcp_up(struct sppp *sp);
346 static void sppp_ipcp_down(struct sppp *sp);
347 static void sppp_ipcp_open(struct sppp *sp);
348 static void sppp_ipcp_close(struct sppp *sp);
349 static void sppp_ipcp_TO(void *sp);
350 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
351 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
352 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
353 static void sppp_ipcp_tlu(struct sppp *sp);
354 static void sppp_ipcp_tld(struct sppp *sp);
355 static void sppp_ipcp_tls(struct sppp *sp);
356 static void sppp_ipcp_tlf(struct sppp *sp);
357 static void sppp_ipcp_scr(struct sppp *sp);
359 static void sppp_ipv6cp_init(struct sppp *sp);
360 static void sppp_ipv6cp_up(struct sppp *sp);
361 static void sppp_ipv6cp_down(struct sppp *sp);
362 static void sppp_ipv6cp_open(struct sppp *sp);
363 static void sppp_ipv6cp_close(struct sppp *sp);
364 static void sppp_ipv6cp_TO(void *sp);
365 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
366 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
367 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
368 static void sppp_ipv6cp_tlu(struct sppp *sp);
369 static void sppp_ipv6cp_tld(struct sppp *sp);
370 static void sppp_ipv6cp_tls(struct sppp *sp);
371 static void sppp_ipv6cp_tlf(struct sppp *sp);
372 static void sppp_ipv6cp_scr(struct sppp *sp);
374 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
375 static void sppp_pap_init(struct sppp *sp);
376 static void sppp_pap_open(struct sppp *sp);
377 static void sppp_pap_close(struct sppp *sp);
378 static void sppp_pap_TO(void *sp);
379 static void sppp_pap_my_TO(void *sp);
380 static void sppp_pap_tlu(struct sppp *sp);
381 static void sppp_pap_tld(struct sppp *sp);
382 static void sppp_pap_scr(struct sppp *sp);
384 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
385 static void sppp_chap_init(struct sppp *sp);
386 static void sppp_chap_open(struct sppp *sp);
387 static void sppp_chap_close(struct sppp *sp);
388 static void sppp_chap_TO(void *sp);
389 static void sppp_chap_tlu(struct sppp *sp);
390 static void sppp_chap_tld(struct sppp *sp);
391 static void sppp_chap_scr(struct sppp *sp);
393 static const char *sppp_auth_type_name(u_short proto, u_char type);
394 static const char *sppp_cp_type_name(u_char type);
395 static const char *sppp_dotted_quad(u_long addr);
396 static const char *sppp_ipcp_opt_name(u_char opt);
398 static const char *sppp_ipv6cp_opt_name(u_char opt);
400 static const char *sppp_lcp_opt_name(u_char opt);
401 static const char *sppp_phase_name(enum ppp_phase phase);
402 static const char *sppp_proto_name(u_short proto);
403 static const char *sppp_state_name(int state);
404 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
405 static int sppp_strnlen(u_char *p, int max);
406 static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst,
408 static void sppp_keepalive(void *dummy);
409 static void sppp_phase_network(struct sppp *sp);
410 static void sppp_print_bytes(const u_char *p, u_short len);
411 static void sppp_print_string(const char *p, u_short len);
412 static void sppp_qflush(struct ifqueue *ifq);
413 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
415 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
416 struct in6_addr *dst, struct in6_addr *srcmask);
417 #ifdef IPV6CP_MYIFID_DYN
418 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
419 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
421 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
424 /* our control protocol descriptors */
425 static const struct cp lcp = {
426 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
427 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
428 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
429 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
433 static const struct cp ipcp = {
434 PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
435 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
436 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
437 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
441 static const struct cp ipv6cp = {
442 PPP_IPV6CP, IDX_IPV6CP,
443 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
449 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
450 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
451 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
455 static const struct cp pap = {
456 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
457 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
458 sppp_pap_TO, 0, 0, 0,
459 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
463 static const struct cp chap = {
464 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
465 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
466 sppp_chap_TO, 0, 0, 0,
467 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
471 static const struct cp *cps[IDX_COUNT] = {
473 &ipcp, /* IDX_IPCP */
474 &ipv6cp, /* IDX_IPV6CP */
476 &chap, /* IDX_CHAP */
480 sppp_modevent(module_t mod, int type, void *unused)
493 static moduledata_t spppmod = {
498 MODULE_VERSION(sppp, 1);
499 DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
502 * Exported functions, comprising our interface to the lower layer.
506 * Process the received packet.
509 sppp_input(struct ifnet *ifp, struct mbuf *m)
511 struct ppp_header *h;
513 struct sppp *sp = (struct sppp *)ifp;
515 int hlen, vjlen, do_account = 0;
516 int debug = ifp->if_flags & IFF_DEBUG;
518 if (ifp->if_flags & IFF_UP)
519 /* Count received bytes, add FCS and one flag */
520 ifp->if_ibytes += m->m_pkthdr.len + 3;
522 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
523 /* Too small packet, drop it. */
526 SPP_FMT "input packet is too small, %d bytes\n",
527 SPP_ARGS(ifp), m->m_pkthdr.len);
536 /* Get PPP header. */
537 h = mtod (m, struct ppp_header*);
538 m_adj (m, PPP_HEADER_LEN);
540 switch (h->address) {
541 case PPP_ALLSTATIONS:
542 if (h->control != PPP_UI)
544 if (sp->pp_mode == IFF_CISCO) {
547 SPP_FMT "PPP packet in Cisco mode "
548 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
550 h->address, h->control, ntohs(h->protocol));
553 switch (ntohs (h->protocol)) {
557 SPP_FMT "rejecting protocol "
558 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
560 h->address, h->control, ntohs(h->protocol));
561 if (sp->state[IDX_LCP] == STATE_OPENED)
562 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
563 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
568 sppp_cp_input(&lcp, sp, m);
572 if (sp->pp_phase >= PHASE_AUTHENTICATE)
573 sppp_pap_input(sp, m);
577 if (sp->pp_phase >= PHASE_AUTHENTICATE)
578 sppp_chap_input(sp, m);
583 if (sp->pp_phase == PHASE_NETWORK)
584 sppp_cp_input(&ipcp, sp, m);
588 if (sp->state[IDX_IPCP] == STATE_OPENED) {
594 if (sp->state[IDX_IPCP] == STATE_OPENED) {
596 sl_uncompress_tcp_core(mtod(m, u_char *),
600 &iphdr, &hlen)) <= 0) {
603 SPP_FMT "VJ uncompress failed on compressed packet\n",
609 * Trim the VJ header off the packet, and prepend
610 * the uncompressed IP header (which will usually
611 * end up in two chained mbufs since there's not
612 * enough leading space in the existing mbuf).
615 M_PREPEND(m, hlen, MB_DONTWAIT);
618 bcopy(iphdr, mtod(m, u_char *), hlen);
625 if (sp->state[IDX_IPCP] == STATE_OPENED) {
626 if (sl_uncompress_tcp_core(mtod(m, u_char *),
628 TYPE_UNCOMPRESSED_TCP,
630 &iphdr, &hlen) != 0) {
633 SPP_FMT "VJ uncompress failed on uncompressed packet\n",
644 if (sp->pp_phase == PHASE_NETWORK)
645 sppp_cp_input(&ipv6cp, sp, m);
650 if (sp->state[IDX_IPV6CP] == STATE_OPENED) {
658 /* IPX IPXCP not implemented yet */
659 if (sp->pp_phase == PHASE_NETWORK) {
667 /* XNS IDPCP not implemented yet */
668 if (sp->pp_phase == PHASE_NETWORK) {
676 case CISCO_MULTICAST:
678 /* Don't check the control field here (RFC 1547). */
679 if (sp->pp_mode != IFF_CISCO) {
682 SPP_FMT "Cisco packet in PPP mode "
683 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
685 h->address, h->control, ntohs(h->protocol));
688 switch (ntohs (h->protocol)) {
692 case CISCO_KEEPALIVE:
693 sppp_cisco_input ((struct sppp*) ifp, m);
722 default: /* Invalid PPP packet. */
726 SPP_FMT "invalid input packet "
727 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
729 h->address, h->control, ntohs(h->protocol));
733 if (! (ifp->if_flags & IFF_UP) || isr < 0)
738 netisr_dispatch(isr, m);
741 * Do only account for network packets, not for control
742 * packets. This is used by some subsystems to detect
745 sp->pp_last_recv = time_second;
749 * Enqueue transmit packet.
752 sppp_output(struct ifnet *ifp, struct mbuf *m,
753 struct sockaddr *dst, struct rtentry *rt)
755 struct sppp *sp = (struct sppp*) ifp;
756 struct ppp_header *h;
757 struct ifqueue *ifq = NULL;
759 int ipproto = PPP_IP;
760 int debug = ifp->if_flags & IFF_DEBUG;
764 if ((ifp->if_flags & IFF_UP) == 0 ||
765 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
774 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
779 * Hack to prevent the initialization-time generated
780 * IPv6 multicast packet to erroneously cause a
781 * dialout event in case IPv6 has been
782 * administratively disabled on that interface.
784 if (dst->sa_family == AF_INET6 &&
785 !(sp->confflags & CONF_ENABLE_IPV6))
789 * Interface is not yet running, but auto-dial. Need
790 * to start LCP for it.
792 ifp->if_flags |= IFF_RUNNING;
800 if (dst->sa_family == AF_INET) {
801 /* XXX Check mbuf length here? */
802 struct ip *ip = mtod (m, struct ip*);
803 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
806 * When using dynamic local IP address assignment by using
807 * 0.0.0.0 as a local address, the first TCP session will
808 * not connect because the local TCP checksum is computed
809 * using 0.0.0.0 which will later become our real IP address
810 * so the TCP checksum computed at the remote end will
811 * become invalid. So we
812 * - don't let packets with src ip addr 0 thru
813 * - we flag TCP packets with src ip 0 as an error
816 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
820 if(ip->ip_p == IPPROTO_TCP)
821 return(EADDRNOTAVAIL);
827 * Put low delay, telnet, rlogin and ftp control packets
828 * in front of the queue.
830 if (IF_QFULL (&sp->pp_fastq))
832 else if (ip->ip_tos & IPTOS_LOWDELAY)
834 else if (m->m_len < sizeof *ip + sizeof *tcp)
836 else if (ip->ip_p != IPPROTO_TCP)
838 else if (INTERACTIVE (ntohs (tcp->th_sport)))
840 else if (INTERACTIVE (ntohs (tcp->th_dport)))
844 * Do IP Header compression
846 if (sp->pp_mode != IFF_CISCO && (sp->ipcp.flags & IPCP_VJ) &&
847 ip->ip_p == IPPROTO_TCP)
848 switch (sl_compress_tcp(m, ip, sp->pp_comp,
849 sp->ipcp.compress_cid)) {
850 case TYPE_COMPRESSED_TCP:
851 ipproto = PPP_VJ_COMP;
853 case TYPE_UNCOMPRESSED_TCP:
854 ipproto = PPP_VJ_UCOMP;
868 if (dst->sa_family == AF_INET6) {
869 /* XXX do something tricky here? */
874 * Prepend general data packet PPP header. For now, IP only.
876 M_PREPEND (m, PPP_HEADER_LEN, MB_DONTWAIT);
879 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
886 * May want to check size of packet
887 * (albeit due to the implementation it's always enough)
889 h = mtod (m, struct ppp_header*);
890 if (sp->pp_mode == IFF_CISCO) {
891 h->address = CISCO_UNICAST; /* unicast address */
894 h->address = PPP_ALLSTATIONS; /* broadcast address */
895 h->control = PPP_UI; /* Unnumbered Info */
898 switch (dst->sa_family) {
900 case AF_INET: /* Internet Protocol */
901 if (sp->pp_mode == IFF_CISCO)
902 h->protocol = htons (ETHERTYPE_IP);
905 * Don't choke with an ENETDOWN early. It's
906 * possible that we just started dialing out,
907 * so don't drop the packet immediately. If
908 * we notice that we run out of buffer space
909 * below, we will however remember that we are
910 * not ready to carry IP packets, and return
911 * ENETDOWN, as opposed to ENOBUFS.
913 h->protocol = htons(ipproto);
914 if (sp->state[IDX_IPCP] != STATE_OPENED)
920 case AF_INET6: /* Internet Protocol */
921 if (sp->pp_mode == IFF_CISCO)
922 h->protocol = htons (ETHERTYPE_IPV6);
925 * Don't choke with an ENETDOWN early. It's
926 * possible that we just started dialing out,
927 * so don't drop the packet immediately. If
928 * we notice that we run out of buffer space
929 * below, we will however remember that we are
930 * not ready to carry IP packets, and return
931 * ENETDOWN, as opposed to ENOBUFS.
933 h->protocol = htons(PPP_IPV6);
934 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
940 case AF_NS: /* Xerox NS Protocol */
941 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
942 ETHERTYPE_NS : PPP_XNS);
946 case AF_IPX: /* Novell IPX Protocol */
947 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
948 ETHERTYPE_IPX : PPP_IPX);
955 return (EAFNOSUPPORT);
959 * Queue message on interface, and start output if interface
962 if (IF_QFULL (ifq)) {
963 IF_DROP (&ifp->if_snd);
967 return (rv? rv: ENOBUFS);
970 if (! (ifp->if_flags & IFF_OACTIVE))
971 (*ifp->if_start) (ifp);
974 * Count output packets and bytes.
975 * The packet length includes header, FCS and 1 flag,
976 * according to RFC 1333.
978 ifp->if_obytes += m->m_pkthdr.len + 3;
981 * Unlike in sppp_input(), we can always bump the timestamp
982 * here since sppp_output() is only called on behalf of
983 * network-layer traffic; control-layer traffic is handled
986 sp->pp_last_sent = time_second;
991 sppp_attach(struct ifnet *ifp)
993 struct sppp *sp = (struct sppp*) ifp;
995 /* Initialize keepalive handler. */
997 TIMEOUT(sppp_keepalive, 0, hz * 10, keepalive_ch);
999 /* Insert new entry into the keepalive list. */
1000 sp->pp_next = spppq;
1003 sp->pp_if.if_mtu = PP_MTU;
1004 sp->pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
1005 sp->pp_if.if_type = IFT_PPP;
1006 sp->pp_if.if_output = sppp_output;
1008 sp->pp_flags = PP_KEEPALIVE;
1010 sp->pp_if.if_snd.ifq_maxlen = 32;
1011 sp->pp_fastq.ifq_maxlen = 32;
1012 sp->pp_cpq.ifq_maxlen = 20;
1014 sp->pp_alivecnt = 0;
1015 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
1016 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
1017 sp->pp_phase = PHASE_DEAD;
1019 sp->pp_down = lcp.Down;
1020 sp->pp_last_recv = sp->pp_last_sent = time_second;
1023 sp->confflags |= CONF_ENABLE_VJ;
1026 sp->confflags |= CONF_ENABLE_IPV6;
1028 sp->pp_comp = malloc(sizeof(struct slcompress), M_TEMP, M_WAITOK);
1029 sl_compress_init(sp->pp_comp, -1);
1032 sppp_ipv6cp_init(sp);
1038 sppp_detach(struct ifnet *ifp)
1040 struct sppp **q, *p, *sp = (struct sppp*) ifp;
1043 /* Remove the entry from the keepalive list. */
1044 for (q = &spppq; (p = *q); q = &p->pp_next)
1050 /* Stop keepalive handler. */
1052 UNTIMEOUT(sppp_keepalive, 0, keepalive_ch);
1054 for (i = 0; i < IDX_COUNT; i++)
1055 UNTIMEOUT((cps[i])->TO, (void *)sp, sp->ch[i]);
1056 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
1060 * Flush the interface output queue.
1063 sppp_flush(struct ifnet *ifp)
1065 struct sppp *sp = (struct sppp*) ifp;
1067 sppp_qflush (&sp->pp_if.if_snd);
1068 sppp_qflush (&sp->pp_fastq);
1069 sppp_qflush (&sp->pp_cpq);
1073 * Check if the output queue is empty.
1076 sppp_isempty(struct ifnet *ifp)
1078 struct sppp *sp = (struct sppp*) ifp;
1082 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
1083 !sp->pp_if.if_snd.ifq_head;
1089 * Get next packet to send.
1092 sppp_dequeue(struct ifnet *ifp)
1094 struct sppp *sp = (struct sppp*) ifp;
1100 * Process only the control protocol queue until we have at
1101 * least one NCP open.
1103 * Do always serve all three queues in Cisco mode.
1105 IF_DEQUEUE(&sp->pp_cpq, m);
1107 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO)) {
1108 IF_DEQUEUE(&sp->pp_fastq, m);
1110 IF_DEQUEUE (&sp->pp_if.if_snd, m);
1117 * Pick the next packet, do not remove it from the queue.
1120 sppp_pick(struct ifnet *ifp)
1122 struct sppp *sp = (struct sppp*)ifp;
1128 m = sp->pp_cpq.ifq_head;
1130 (sp->pp_phase == PHASE_NETWORK || sp->pp_mode == IFF_CISCO))
1131 if ((m = sp->pp_fastq.ifq_head) == NULL)
1132 m = sp->pp_if.if_snd.ifq_head;
1138 * Process an ioctl request. Called on low priority level.
1141 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1143 struct ifreq *ifr = (struct ifreq*) data;
1144 struct sppp *sp = (struct sppp*) ifp;
1145 int s, rv, going_up, going_down, newmode;
1151 case SIOCSIFDSTADDR:
1155 /* set the interface "up" when assigning an IP address */
1156 ifp->if_flags |= IFF_UP;
1157 /* fall through... */
1160 going_up = ifp->if_flags & IFF_UP &&
1161 (ifp->if_flags & IFF_RUNNING) == 0;
1162 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1163 ifp->if_flags & IFF_RUNNING;
1165 newmode = ifp->if_flags & IFF_PASSIVE;
1167 newmode = ifp->if_flags & IFF_AUTO;
1169 newmode = ifp->if_flags & IFF_CISCO;
1170 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1171 ifp->if_flags |= newmode;
1173 if (newmode != sp->pp_mode) {
1176 going_up = ifp->if_flags & IFF_RUNNING;
1180 if (sp->pp_mode != IFF_CISCO)
1182 else if (sp->pp_tlf)
1185 ifp->if_flags &= ~IFF_RUNNING;
1186 sp->pp_mode = newmode;
1190 if (sp->pp_mode != IFF_CISCO)
1192 sp->pp_mode = newmode;
1193 if (sp->pp_mode == 0) {
1194 ifp->if_flags |= IFF_RUNNING;
1197 if (sp->pp_mode == IFF_CISCO) {
1200 ifp->if_flags |= IFF_RUNNING;
1208 #define ifr_mtu ifr_metric
1211 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1213 ifp->if_mtu = ifr->ifr_mtu;
1218 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1220 ifp->if_mtu = *(short*)data;
1225 ifr->ifr_mtu = ifp->if_mtu;
1230 *(short*)data = ifp->if_mtu;
1237 case SIOCGIFGENERIC:
1238 case SIOCSIFGENERIC:
1239 rv = sppp_params(sp, cmd, data);
1250 * Cisco framing implementation.
1254 * Handle incoming Cisco keepalive protocol packets.
1257 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1260 struct cisco_packet *h;
1263 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1266 SPP_FMT "cisco invalid packet length: %d bytes\n",
1267 SPP_ARGS(ifp), m->m_pkthdr.len);
1270 h = mtod (m, struct cisco_packet*);
1273 SPP_FMT "cisco input: %d bytes "
1274 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1275 SPP_ARGS(ifp), m->m_pkthdr.len,
1276 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1277 (u_int)h->time0, (u_int)h->time1);
1278 switch (ntohl (h->type)) {
1281 addlog(SPP_FMT "cisco unknown packet type: 0x%lx\n",
1282 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1284 case CISCO_ADDR_REPLY:
1285 /* Reply on address request, ignore */
1287 case CISCO_KEEPALIVE_REQ:
1288 sp->pp_alivecnt = 0;
1289 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1290 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1291 /* Local and remote sequence numbers are equal.
1292 * Probably, the line is in loopback mode. */
1293 if (sp->pp_loopcnt >= MAXALIVECNT) {
1294 printf (SPP_FMT "loopback\n",
1297 if (ifp->if_flags & IFF_UP) {
1299 sppp_qflush (&sp->pp_cpq);
1304 /* Generate new local sequence number */
1305 #if defined(__DragonFly__)
1306 sp->pp_seq[IDX_LCP] = random();
1308 sp->pp_seq[IDX_LCP] ^= time.tv_sec ^ time.tv_usec;
1313 if (! (ifp->if_flags & IFF_UP) &&
1314 (ifp->if_flags & IFF_RUNNING)) {
1316 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1319 case CISCO_ADDR_REQ:
1320 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1322 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1328 * Send Cisco keepalive packet.
1331 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1334 struct ppp_header *h;
1335 struct cisco_packet *ch;
1337 #if defined(__DragonFly__)
1340 u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
1343 #if defined(__DragonFly__)
1344 getmicrouptime(&tv);
1347 MGETHDR (m, MB_DONTWAIT, MT_DATA);
1350 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1351 m->m_pkthdr.rcvif = 0;
1353 h = mtod (m, struct ppp_header*);
1354 h->address = CISCO_MULTICAST;
1356 h->protocol = htons (CISCO_KEEPALIVE);
1358 ch = (struct cisco_packet*) (h + 1);
1359 ch->type = htonl (type);
1360 ch->par1 = htonl (par1);
1361 ch->par2 = htonl (par2);
1364 #if defined(__DragonFly__)
1365 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1366 ch->time1 = htons ((u_short) tv.tv_sec);
1368 ch->time0 = htons ((u_short) (t >> 16));
1369 ch->time1 = htons ((u_short) t);
1374 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1375 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1376 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1378 if (IF_QFULL (&sp->pp_cpq)) {
1379 IF_DROP (&sp->pp_fastq);
1380 IF_DROP (&ifp->if_snd);
1383 IF_ENQUEUE (&sp->pp_cpq, m);
1384 if (! (ifp->if_flags & IFF_OACTIVE))
1385 (*ifp->if_start) (ifp);
1386 ifp->if_obytes += m->m_pkthdr.len + 3;
1390 * PPP protocol implementation.
1394 * Send PPP control protocol packet.
1397 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1398 u_char ident, u_short len, void *data)
1401 struct ppp_header *h;
1402 struct lcp_header *lh;
1405 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1406 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1407 MGETHDR (m, MB_DONTWAIT, MT_DATA);
1410 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1411 m->m_pkthdr.rcvif = 0;
1413 h = mtod (m, struct ppp_header*);
1414 h->address = PPP_ALLSTATIONS; /* broadcast address */
1415 h->control = PPP_UI; /* Unnumbered Info */
1416 h->protocol = htons (proto); /* Link Control Protocol */
1418 lh = (struct lcp_header*) (h + 1);
1421 lh->len = htons (LCP_HEADER_LEN + len);
1423 bcopy (data, lh+1, len);
1426 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1428 sppp_proto_name(proto),
1429 sppp_cp_type_name (lh->type), lh->ident,
1431 sppp_print_bytes ((u_char*) (lh+1), len);
1434 if (IF_QFULL (&sp->pp_cpq)) {
1435 IF_DROP (&sp->pp_fastq);
1436 IF_DROP (&ifp->if_snd);
1440 IF_ENQUEUE (&sp->pp_cpq, m);
1441 if (! (ifp->if_flags & IFF_OACTIVE))
1442 (*ifp->if_start) (ifp);
1443 ifp->if_obytes += m->m_pkthdr.len + 3;
1447 * Handle incoming PPP control protocol packets.
1450 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1453 struct lcp_header *h;
1454 int len = m->m_pkthdr.len;
1461 SPP_FMT "%s invalid packet length: %d bytes\n",
1462 SPP_ARGS(ifp), cp->name, len);
1465 h = mtod (m, struct lcp_header*);
1468 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1469 SPP_ARGS(ifp), cp->name,
1470 sppp_state_name(sp->state[cp->protoidx]),
1471 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1472 sppp_print_bytes ((u_char*) (h+1), len-4);
1475 if (len > ntohs (h->len))
1476 len = ntohs (h->len);
1477 p = (u_char *)(h + 1);
1482 addlog(SPP_FMT "%s invalid conf-req length %d\n",
1483 SPP_ARGS(ifp), cp->name,
1488 /* handle states where RCR doesn't get a SCA/SCN */
1489 switch (sp->state[cp->protoidx]) {
1491 case STATE_STOPPING:
1494 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1498 rv = (cp->RCR)(sp, h, len);
1499 switch (sp->state[cp->protoidx]) {
1503 /* fall through... */
1504 case STATE_ACK_SENT:
1505 case STATE_REQ_SENT:
1507 * sppp_cp_change_state() have the side effect of
1508 * restarting the timeouts. We want to avoid that
1509 * if the state don't change, otherwise we won't
1510 * ever timeout and resend a configuration request
1513 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1516 sppp_cp_change_state(cp, sp, rv?
1517 STATE_ACK_SENT: STATE_REQ_SENT);
1520 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1522 sppp_cp_change_state(cp, sp, rv?
1523 STATE_ACK_SENT: STATE_REQ_SENT);
1525 case STATE_ACK_RCVD:
1527 sppp_cp_change_state(cp, sp, STATE_OPENED);
1529 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1534 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1537 printf(SPP_FMT "%s illegal %s in state %s\n",
1538 SPP_ARGS(ifp), cp->name,
1539 sppp_cp_type_name(h->type),
1540 sppp_state_name(sp->state[cp->protoidx]));
1545 if (h->ident != sp->confid[cp->protoidx]) {
1547 addlog(SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1548 SPP_ARGS(ifp), cp->name,
1549 h->ident, sp->confid[cp->protoidx]);
1553 switch (sp->state[cp->protoidx]) {
1556 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1559 case STATE_STOPPING:
1561 case STATE_REQ_SENT:
1562 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1563 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1568 case STATE_ACK_RCVD:
1570 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1572 case STATE_ACK_SENT:
1573 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1574 sppp_cp_change_state(cp, sp, STATE_OPENED);
1576 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1577 SPP_ARGS(ifp), cp->name);
1581 printf(SPP_FMT "%s illegal %s in state %s\n",
1582 SPP_ARGS(ifp), cp->name,
1583 sppp_cp_type_name(h->type),
1584 sppp_state_name(sp->state[cp->protoidx]));
1590 if (h->ident != sp->confid[cp->protoidx]) {
1592 addlog(SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1593 SPP_ARGS(ifp), cp->name,
1594 h->ident, sp->confid[cp->protoidx]);
1598 if (h->type == CONF_NAK)
1599 (cp->RCN_nak)(sp, h, len);
1601 (cp->RCN_rej)(sp, h, len);
1603 switch (sp->state[cp->protoidx]) {
1606 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1608 case STATE_REQ_SENT:
1609 case STATE_ACK_SENT:
1610 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1612 * Slow things down a bit if we think we might be
1613 * in loopback. Depend on the timeout to send the
1614 * next configuration request.
1623 case STATE_ACK_RCVD:
1624 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1628 case STATE_STOPPING:
1631 printf(SPP_FMT "%s illegal %s in state %s\n",
1632 SPP_ARGS(ifp), cp->name,
1633 sppp_cp_type_name(h->type),
1634 sppp_state_name(sp->state[cp->protoidx]));
1640 switch (sp->state[cp->protoidx]) {
1641 case STATE_ACK_RCVD:
1642 case STATE_ACK_SENT:
1643 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1648 case STATE_STOPPING:
1649 case STATE_REQ_SENT:
1651 /* Send Terminate-Ack packet. */
1653 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1654 SPP_ARGS(ifp), cp->name);
1655 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1659 sp->rst_counter[cp->protoidx] = 0;
1660 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1664 printf(SPP_FMT "%s illegal %s in state %s\n",
1665 SPP_ARGS(ifp), cp->name,
1666 sppp_cp_type_name(h->type),
1667 sppp_state_name(sp->state[cp->protoidx]));
1672 switch (sp->state[cp->protoidx]) {
1675 case STATE_REQ_SENT:
1676 case STATE_ACK_SENT:
1679 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1682 case STATE_STOPPING:
1683 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1686 case STATE_ACK_RCVD:
1687 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1692 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1695 printf(SPP_FMT "%s illegal %s in state %s\n",
1696 SPP_ARGS(ifp), cp->name,
1697 sppp_cp_type_name(h->type),
1698 sppp_state_name(sp->state[cp->protoidx]));
1703 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1705 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1706 "danger will robinson\n",
1707 SPP_ARGS(ifp), cp->name,
1708 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1709 switch (sp->state[cp->protoidx]) {
1712 case STATE_REQ_SENT:
1713 case STATE_ACK_SENT:
1715 case STATE_STOPPING:
1718 case STATE_ACK_RCVD:
1719 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1722 printf(SPP_FMT "%s illegal %s in state %s\n",
1723 SPP_ARGS(ifp), cp->name,
1724 sppp_cp_type_name(h->type),
1725 sppp_state_name(sp->state[cp->protoidx]));
1732 const struct cp *upper;
1738 proto = ntohs(*((u_int16_t *)p));
1739 for (i = 0; i < IDX_COUNT; i++) {
1740 if (cps[i]->proto == proto) {
1748 if (catastrophic || debug)
1749 log(catastrophic? LOG_INFO: LOG_DEBUG,
1750 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1751 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1752 sppp_cp_type_name(h->type), proto,
1753 upper ? upper->name : "unknown",
1754 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1757 * if we got RXJ+ against conf-req, the peer does not implement
1758 * this particular protocol type. terminate the protocol.
1760 if (upper && !catastrophic) {
1761 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1767 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1768 switch (sp->state[cp->protoidx]) {
1771 case STATE_REQ_SENT:
1772 case STATE_ACK_SENT:
1774 case STATE_STOPPING:
1777 case STATE_ACK_RCVD:
1778 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1781 printf(SPP_FMT "%s illegal %s in state %s\n",
1782 SPP_ARGS(ifp), cp->name,
1783 sppp_cp_type_name(h->type),
1784 sppp_state_name(sp->state[cp->protoidx]));
1790 if (cp->proto != PPP_LCP)
1792 /* Discard the packet. */
1795 if (cp->proto != PPP_LCP)
1797 if (sp->state[cp->protoidx] != STATE_OPENED) {
1799 addlog(SPP_FMT "lcp echo req but lcp closed\n",
1806 addlog(SPP_FMT "invalid lcp echo request "
1807 "packet length: %d bytes\n",
1808 SPP_ARGS(ifp), len);
1811 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1812 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1813 /* Line loopback mode detected. */
1814 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1815 sp->pp_loopcnt = MAXALIVECNT * 5;
1817 sppp_qflush (&sp->pp_cpq);
1819 /* Shut down the PPP link. */
1825 *(long*)(h+1) = htonl (sp->lcp.magic);
1827 addlog(SPP_FMT "got lcp echo req, sending echo rep\n",
1829 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1832 if (cp->proto != PPP_LCP)
1834 if (h->ident != sp->lcp.echoid) {
1840 addlog(SPP_FMT "lcp invalid echo reply "
1841 "packet length: %d bytes\n",
1842 SPP_ARGS(ifp), len);
1846 addlog(SPP_FMT "lcp got echo rep\n",
1848 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1849 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1850 sp->pp_alivecnt = 0;
1853 /* Unknown packet type -- send Code-Reject packet. */
1856 addlog(SPP_FMT "%s send code-rej for 0x%x\n",
1857 SPP_ARGS(ifp), cp->name, h->type);
1858 sppp_cp_send(sp, cp->proto, CODE_REJ,
1859 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1866 * The generic part of all Up/Down/Open/Close/TO event handlers.
1867 * Basically, the state transition handling in the automaton.
1870 sppp_up_event(const struct cp *cp, struct sppp *sp)
1875 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1876 SPP_ARGS(ifp), cp->name,
1877 sppp_state_name(sp->state[cp->protoidx]));
1879 switch (sp->state[cp->protoidx]) {
1881 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1883 case STATE_STARTING:
1884 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1886 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1889 printf(SPP_FMT "%s illegal up in state %s\n",
1890 SPP_ARGS(ifp), cp->name,
1891 sppp_state_name(sp->state[cp->protoidx]));
1896 sppp_down_event(const struct cp *cp, struct sppp *sp)
1901 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1902 SPP_ARGS(ifp), cp->name,
1903 sppp_state_name(sp->state[cp->protoidx]));
1905 switch (sp->state[cp->protoidx]) {
1908 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1911 sppp_cp_change_state(cp, sp, STATE_STARTING);
1914 case STATE_STOPPING:
1915 case STATE_REQ_SENT:
1916 case STATE_ACK_RCVD:
1917 case STATE_ACK_SENT:
1918 sppp_cp_change_state(cp, sp, STATE_STARTING);
1922 sppp_cp_change_state(cp, sp, STATE_STARTING);
1925 printf(SPP_FMT "%s illegal down in state %s\n",
1926 SPP_ARGS(ifp), cp->name,
1927 sppp_state_name(sp->state[cp->protoidx]));
1933 sppp_open_event(const struct cp *cp, struct sppp *sp)
1938 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1939 SPP_ARGS(ifp), cp->name,
1940 sppp_state_name(sp->state[cp->protoidx]));
1942 switch (sp->state[cp->protoidx]) {
1944 sppp_cp_change_state(cp, sp, STATE_STARTING);
1947 case STATE_STARTING:
1950 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1952 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1956 * Try escaping stopped state. This seems to bite
1957 * people occasionally, in particular for IPCP,
1958 * presumably following previous IPCP negotiation
1959 * aborts. Somehow, we must have missed a Down event
1960 * which would have caused a transition into starting
1961 * state, so as a bandaid we force the Down event now.
1962 * This effectively implements (something like the)
1963 * `restart' option mentioned in the state transition
1964 * table of RFC 1661.
1966 sppp_cp_change_state(cp, sp, STATE_STARTING);
1969 case STATE_STOPPING:
1970 case STATE_REQ_SENT:
1971 case STATE_ACK_RCVD:
1972 case STATE_ACK_SENT:
1976 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1983 sppp_close_event(const struct cp *cp, struct sppp *sp)
1988 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
1989 SPP_ARGS(ifp), cp->name,
1990 sppp_state_name(sp->state[cp->protoidx]));
1992 switch (sp->state[cp->protoidx]) {
1997 case STATE_STARTING:
1998 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2002 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2004 case STATE_STOPPING:
2005 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2010 case STATE_REQ_SENT:
2011 case STATE_ACK_RCVD:
2012 case STATE_ACK_SENT:
2013 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2014 sppp_cp_send(sp, cp->proto, TERM_REQ,
2015 ++sp->pp_seq[cp->protoidx], 0, 0);
2016 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2022 sppp_to_event(const struct cp *cp, struct sppp *sp)
2029 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
2030 SPP_ARGS(ifp), cp->name,
2031 sppp_state_name(sp->state[cp->protoidx]),
2032 sp->rst_counter[cp->protoidx]);
2034 if (--sp->rst_counter[cp->protoidx] < 0)
2036 switch (sp->state[cp->protoidx]) {
2038 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2041 case STATE_STOPPING:
2042 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2045 case STATE_REQ_SENT:
2046 case STATE_ACK_RCVD:
2047 case STATE_ACK_SENT:
2048 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2054 switch (sp->state[cp->protoidx]) {
2056 case STATE_STOPPING:
2057 sppp_cp_send(sp, cp->proto, TERM_REQ,
2058 ++sp->pp_seq[cp->protoidx], 0, 0);
2059 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
2060 sp->ch[cp->protoidx]);
2062 case STATE_REQ_SENT:
2063 case STATE_ACK_RCVD:
2065 /* sppp_cp_change_state() will restart the timer */
2066 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2068 case STATE_ACK_SENT:
2070 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
2071 sp->ch[cp->protoidx]);
2079 * Change the state of a control protocol in the state automaton.
2080 * Takes care of starting/stopping the restart timer.
2083 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2085 sp->state[cp->protoidx] = newstate;
2087 UNTIMEOUT(cp->TO, (void *)sp, sp->ch[cp->protoidx]);
2090 case STATE_STARTING:
2096 case STATE_STOPPING:
2097 case STATE_REQ_SENT:
2098 case STATE_ACK_RCVD:
2099 case STATE_ACK_SENT:
2100 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
2101 sp->ch[cp->protoidx]);
2107 *--------------------------------------------------------------------------*
2109 * The LCP implementation. *
2111 *--------------------------------------------------------------------------*
2114 sppp_lcp_init(struct sppp *sp)
2116 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2118 sp->state[IDX_LCP] = STATE_INITIAL;
2119 sp->fail_counter[IDX_LCP] = 0;
2120 sp->pp_seq[IDX_LCP] = 0;
2121 sp->pp_rseq[IDX_LCP] = 0;
2123 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2125 /* Note that these values are relevant for all control protocols */
2126 sp->lcp.timeout = 3 * hz;
2127 sp->lcp.max_terminate = 2;
2128 sp->lcp.max_configure = 10;
2129 sp->lcp.max_failure = 10;
2130 #if defined(__DragonFly__)
2131 callout_handle_init(&sp->ch[IDX_LCP]);
2136 sppp_lcp_up(struct sppp *sp)
2140 sp->pp_alivecnt = 0;
2141 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2144 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2146 * If this interface is passive or dial-on-demand, and we are
2147 * still in Initial state, it means we've got an incoming
2148 * call. Activate the interface.
2150 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2153 SPP_FMT "Up event", SPP_ARGS(ifp));
2154 ifp->if_flags |= IFF_RUNNING;
2155 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2157 addlog("(incoming call)\n");
2158 sp->pp_flags |= PP_CALLIN;
2162 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2163 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2164 ifp->if_flags |= IFF_RUNNING;
2168 sppp_up_event(&lcp, sp);
2172 sppp_lcp_down(struct sppp *sp)
2176 sppp_down_event(&lcp, sp);
2179 * If this is neither a dial-on-demand nor a passive
2180 * interface, simulate an ``ifconfig down'' action, so the
2181 * administrator can force a redial by another ``ifconfig
2182 * up''. XXX For leased line operation, should we immediately
2183 * try to reopen the connection here?
2185 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2187 SPP_FMT "Down event, taking interface down.\n",
2193 SPP_FMT "Down event (carrier loss)\n",
2195 sp->pp_flags &= ~PP_CALLIN;
2196 if (sp->state[IDX_LCP] != STATE_INITIAL)
2198 ifp->if_flags &= ~IFF_RUNNING;
2203 sppp_lcp_open(struct sppp *sp)
2206 * If we are authenticator, negotiate LCP_AUTH
2208 if (sp->hisauth.proto != 0)
2209 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2211 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2212 sp->pp_flags &= ~PP_NEEDAUTH;
2213 sppp_open_event(&lcp, sp);
2217 sppp_lcp_close(struct sppp *sp)
2219 sppp_close_event(&lcp, sp);
2223 sppp_lcp_TO(void *cookie)
2225 sppp_to_event(&lcp, (struct sppp *)cookie);
2229 * Analyze a configure request. Return true if it was agreeable, and
2230 * caused action sca, false if it has been rejected or nak'ed, and
2231 * caused action scn. (The return value is used to make the state
2232 * transition decision in the state automaton.)
2235 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2238 u_char *buf, *r, *p;
2245 buf = r = malloc (len, M_TEMP, M_INTWAIT);
2248 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2251 /* pass 1: check for things that need to be rejected */
2253 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2255 addlog(" %s ", sppp_lcp_opt_name(*p));
2259 if (len >= 6 && p[1] == 6)
2262 addlog("[invalid] ");
2264 case LCP_OPT_ASYNC_MAP:
2265 /* Async control character map. */
2266 if (len >= 6 && p[1] == 6)
2269 addlog("[invalid] ");
2272 /* Maximum receive unit. */
2273 if (len >= 4 && p[1] == 4)
2276 addlog("[invalid] ");
2278 case LCP_OPT_AUTH_PROTO:
2281 addlog("[invalid] ");
2284 authproto = (p[2] << 8) + p[3];
2285 if (authproto == PPP_CHAP && p[1] != 5) {
2287 addlog("[invalid chap len] ");
2290 if (sp->myauth.proto == 0) {
2291 /* we are not configured to do auth */
2293 addlog("[not configured] ");
2297 * Remote want us to authenticate, remember this,
2298 * so we stay in PHASE_AUTHENTICATE after LCP got
2301 sp->pp_flags |= PP_NEEDAUTH;
2304 /* Others not supported. */
2309 /* Add the option to rejected list. */
2316 addlog(" send conf-rej\n");
2317 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2323 * pass 2: check for option values that are unacceptable and
2324 * thus require to be nak'ed.
2327 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2332 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2334 addlog(" %s ", sppp_lcp_opt_name(*p));
2337 /* Magic number -- extract. */
2338 nmagic = (u_long)p[2] << 24 |
2339 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2340 if (nmagic != sp->lcp.magic) {
2343 addlog("0x%lx ", nmagic);
2346 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2347 addlog("[glitch] ");
2350 * We negate our magic here, and NAK it. If
2351 * we see it later in an NAK packet, we
2352 * suggest a new one.
2354 nmagic = ~sp->lcp.magic;
2356 p[2] = nmagic >> 24;
2357 p[3] = nmagic >> 16;
2362 case LCP_OPT_ASYNC_MAP:
2364 * Async control character map -- just ignore it.
2366 * Quote from RFC 1662, chapter 6:
2367 * To enable this functionality, synchronous PPP
2368 * implementations MUST always respond to the
2369 * Async-Control-Character-Map Configuration
2370 * Option with the LCP Configure-Ack. However,
2371 * acceptance of the Configuration Option does
2372 * not imply that the synchronous implementation
2373 * will do any ACCM mapping. Instead, all such
2374 * octet mapping will be performed by the
2375 * asynchronous-to-synchronous converter.
2381 * Maximum receive unit. Always agreeable,
2382 * but ignored by now.
2384 sp->lcp.their_mru = p[2] * 256 + p[3];
2386 addlog("%lu ", sp->lcp.their_mru);
2389 case LCP_OPT_AUTH_PROTO:
2390 authproto = (p[2] << 8) + p[3];
2391 if (sp->myauth.proto != authproto) {
2392 /* not agreed, nak */
2394 addlog("[mine %s != his %s] ",
2395 sppp_proto_name(sp->hisauth.proto),
2396 sppp_proto_name(authproto));
2397 p[2] = sp->myauth.proto >> 8;
2398 p[3] = sp->myauth.proto;
2401 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2403 addlog("[chap not MD5] ");
2409 /* Add the option to nak'ed list. */
2416 * Local and remote magics equal -- loopback?
2418 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2419 if (sp->pp_loopcnt == MAXALIVECNT*5)
2420 printf (SPP_FMT "loopback\n",
2422 if (ifp->if_flags & IFF_UP) {
2424 sppp_qflush(&sp->pp_cpq);
2429 } else if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2431 addlog(" max_failure (%d) exceeded, "
2433 sp->lcp.max_failure);
2434 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2437 addlog(" send conf-nak\n");
2438 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2442 addlog(" send conf-ack\n");
2443 sp->fail_counter[IDX_LCP] = 0;
2445 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2446 h->ident, origlen, h+1);
2454 * Analyze the LCP Configure-Reject option list, and adjust our
2458 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2464 buf = malloc (len, M_TEMP, M_INTWAIT);
2467 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2471 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2473 addlog(" %s ", sppp_lcp_opt_name(*p));
2476 /* Magic number -- can't use it, use 0 */
2477 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2482 * Should not be rejected anyway, since we only
2483 * negotiate a MRU if explicitly requested by
2486 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2488 case LCP_OPT_AUTH_PROTO:
2490 * Peer doesn't want to authenticate himself,
2491 * deny unless this is a dialout call, and
2492 * AUTHFLAG_NOCALLOUT is set.
2494 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2495 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2497 addlog("[don't insist on auth "
2499 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2503 addlog("[access denied]\n");
2515 * Analyze the LCP Configure-NAK option list, and adjust our
2519 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2526 buf = malloc (len, M_TEMP, M_INTWAIT);
2529 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2533 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2535 addlog(" %s ", sppp_lcp_opt_name(*p));
2538 /* Magic number -- renegotiate */
2539 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2540 len >= 6 && p[1] == 6) {
2541 magic = (u_long)p[2] << 24 |
2542 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2544 * If the remote magic is our negated one,
2545 * this looks like a loopback problem.
2546 * Suggest a new magic to make sure.
2548 if (magic == ~sp->lcp.magic) {
2550 addlog("magic glitch ");
2551 #if defined(__DragonFly__)
2552 sp->lcp.magic = random();
2554 sp->lcp.magic = time.tv_sec + time.tv_usec;
2557 sp->lcp.magic = magic;
2559 addlog("%lu ", magic);
2565 * Peer wants to advise us to negotiate an MRU.
2566 * Agree on it if it's reasonable, or use
2567 * default otherwise.
2569 if (len >= 4 && p[1] == 4) {
2570 u_int mru = p[2] * 256 + p[3];
2573 if (mru < PP_MTU || mru > PP_MAX_MRU)
2576 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2579 case LCP_OPT_AUTH_PROTO:
2581 * Peer doesn't like our authentication method,
2585 addlog("[access denied]\n");
2597 sppp_lcp_tlu(struct sppp *sp)
2604 if (! (ifp->if_flags & IFF_UP) &&
2605 (ifp->if_flags & IFF_RUNNING)) {
2606 /* Coming out of loopback mode. */
2608 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2611 for (i = 0; i < IDX_COUNT; i++)
2612 if ((cps[i])->flags & CP_QUAL)
2615 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2616 (sp->pp_flags & PP_NEEDAUTH) != 0)
2617 sp->pp_phase = PHASE_AUTHENTICATE;
2619 sp->pp_phase = PHASE_NETWORK;
2622 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2623 sppp_phase_name(sp->pp_phase));
2626 * Open all authentication protocols. This is even required
2627 * if we already proceeded to network phase, since it might be
2628 * that remote wants us to authenticate, so we might have to
2629 * send a PAP request. Undesired authentication protocols
2630 * don't do anything when they get an Open event.
2632 for (i = 0; i < IDX_COUNT; i++)
2633 if ((cps[i])->flags & CP_AUTH)
2636 if (sp->pp_phase == PHASE_NETWORK) {
2637 /* Notify all NCPs. */
2638 for (i = 0; i < IDX_COUNT; i++)
2639 if (((cps[i])->flags & CP_NCP) &&
2642 * Hack to administratively disable IPv6 if
2643 * not desired. Perhaps we should have another
2644 * flag for this, but right now, we can make
2645 * all struct cp's read/only.
2647 (cps[i] != &ipv6cp ||
2648 (sp->confflags & CONF_ENABLE_IPV6)))
2652 /* Send Up events to all started protos. */
2653 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2654 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2657 /* notify low-level driver of state change */
2659 sp->pp_chg(sp, (int)sp->pp_phase);
2661 if (sp->pp_phase == PHASE_NETWORK)
2662 /* if no NCP is starting, close down */
2663 sppp_lcp_check_and_close(sp);
2667 sppp_lcp_tld(struct sppp *sp)
2673 sp->pp_phase = PHASE_TERMINATE;
2676 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2677 sppp_phase_name(sp->pp_phase));
2680 * Take upper layers down. We send the Down event first and
2681 * the Close second to prevent the upper layers from sending
2682 * ``a flurry of terminate-request packets'', as the RFC
2685 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2686 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2688 (cps[i])->Close(sp);
2693 sppp_lcp_tls(struct sppp *sp)
2697 sp->pp_phase = PHASE_ESTABLISH;
2700 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2701 sppp_phase_name(sp->pp_phase));
2703 /* Notify lower layer if desired. */
2711 sppp_lcp_tlf(struct sppp *sp)
2715 sp->pp_phase = PHASE_DEAD;
2717 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2718 sppp_phase_name(sp->pp_phase));
2720 /* Notify lower layer if desired. */
2728 sppp_lcp_scr(struct sppp *sp)
2730 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2734 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2735 if (! sp->lcp.magic)
2736 #if defined(__DragonFly__)
2737 sp->lcp.magic = random();
2739 sp->lcp.magic = time.tv_sec + time.tv_usec;
2741 opt[i++] = LCP_OPT_MAGIC;
2743 opt[i++] = sp->lcp.magic >> 24;
2744 opt[i++] = sp->lcp.magic >> 16;
2745 opt[i++] = sp->lcp.magic >> 8;
2746 opt[i++] = sp->lcp.magic;
2749 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2750 opt[i++] = LCP_OPT_MRU;
2752 opt[i++] = sp->lcp.mru >> 8;
2753 opt[i++] = sp->lcp.mru;
2756 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2757 authproto = sp->hisauth.proto;
2758 opt[i++] = LCP_OPT_AUTH_PROTO;
2759 opt[i++] = authproto == PPP_CHAP? 5: 4;
2760 opt[i++] = authproto >> 8;
2761 opt[i++] = authproto;
2762 if (authproto == PPP_CHAP)
2763 opt[i++] = CHAP_MD5;
2766 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2767 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2771 * Check the open NCPs, return true if at least one NCP is open.
2774 sppp_ncp_check(struct sppp *sp)
2778 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2779 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2785 * Re-check the open NCPs and see if we should terminate the link.
2786 * Called by the NCPs during their tlf action handling.
2789 sppp_lcp_check_and_close(struct sppp *sp)
2792 if (sp->pp_phase < PHASE_NETWORK)
2793 /* don't bother, we are already going down */
2796 if (sppp_ncp_check(sp))
2803 *--------------------------------------------------------------------------*
2805 * The IPCP implementation. *
2807 *--------------------------------------------------------------------------*
2811 sppp_ipcp_init(struct sppp *sp)
2815 sp->state[IDX_IPCP] = STATE_INITIAL;
2816 sp->fail_counter[IDX_IPCP] = 0;
2817 sp->pp_seq[IDX_IPCP] = 0;
2818 sp->pp_rseq[IDX_IPCP] = 0;
2819 #if defined(__DragonFly__)
2820 callout_handle_init(&sp->ch[IDX_IPCP]);
2825 sppp_ipcp_up(struct sppp *sp)
2827 sppp_up_event(&ipcp, sp);
2831 sppp_ipcp_down(struct sppp *sp)
2833 sppp_down_event(&ipcp, sp);
2837 sppp_ipcp_open(struct sppp *sp)
2840 u_long myaddr, hisaddr;
2842 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2843 IPCP_MYADDR_DYN | IPCP_VJ);
2846 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2848 * If we don't have his address, this probably means our
2849 * interface doesn't want to talk IP at all. (This could
2850 * be the case if somebody wants to speak only IPX, for
2851 * example.) Don't open IPCP in this case.
2853 if (hisaddr == 0L) {
2854 /* XXX this message should go away */
2856 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2862 * I don't have an assigned address, so i need to
2863 * negotiate my address.
2865 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2866 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2868 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2869 if (sp->confflags & CONF_ENABLE_VJ) {
2870 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2871 sp->ipcp.max_state = MAX_STATES - 1;
2872 sp->ipcp.compress_cid = 1;
2874 sppp_open_event(&ipcp, sp);
2878 sppp_ipcp_close(struct sppp *sp)
2880 sppp_close_event(&ipcp, sp);
2881 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2883 * My address was dynamic, clear it again.
2885 sppp_set_ip_addr(sp, 0L);
2889 sppp_ipcp_TO(void *cookie)
2891 sppp_to_event(&ipcp, (struct sppp *)cookie);
2895 * Analyze a configure request. Return true if it was agreeable, and
2896 * caused action sca, false if it has been rejected or nak'ed, and
2897 * caused action scn. (The return value is used to make the state
2898 * transition decision in the state automaton.)
2901 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2903 u_char *buf, *r, *p;
2904 struct ifnet *ifp = &sp->pp_if;
2905 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2906 u_long hisaddr, desiredaddr;
2913 * Make sure to allocate a buf that can at least hold a
2914 * conf-nak with an `address' option. We might need it below.
2916 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_INTWAIT);
2918 /* pass 1: see if we can recognize them */
2920 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2923 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2925 addlog(" %s ", sppp_ipcp_opt_name(*p));
2927 case IPCP_OPT_COMPRESSION:
2928 if (!(sp->confflags & CONF_ENABLE_VJ)) {
2929 /* VJ compression administratively disabled */
2931 addlog("[locally disabled] ");
2935 * In theory, we should only conf-rej an
2936 * option that is shorter than RFC 1618
2937 * requires (i.e. < 4), and should conf-nak
2938 * anything else that is not VJ. However,
2939 * since our algorithm always uses the
2940 * original option to NAK it with new values,
2941 * things would become more complicated. In
2942 * pratice, the only commonly implemented IP
2943 * compression option is VJ anyway, so the
2944 * difference is negligible.
2946 if (len >= 6 && p[1] == 6) {
2948 * correctly formed compression option
2949 * that could be VJ compression
2954 addlog("optlen %d [invalid/unsupported] ",
2957 case IPCP_OPT_ADDRESS:
2958 if (len >= 6 && p[1] == 6) {
2959 /* correctly formed address option */
2963 addlog("[invalid] ");
2966 /* Others not supported. */
2971 /* Add the option to rejected list. */
2978 addlog(" send conf-rej\n");
2979 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2984 /* pass 2: parse option values */
2985 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
2987 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
2991 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2993 addlog(" %s ", sppp_ipcp_opt_name(*p));
2995 case IPCP_OPT_COMPRESSION:
2996 desiredcomp = p[2] << 8 | p[3];
2997 /* We only support VJ */
2998 if (desiredcomp == IPCP_COMP_VJ) {
3000 addlog("VJ [ack] ");
3001 sp->ipcp.flags |= IPCP_VJ;
3002 sl_compress_init(sp->pp_comp, p[4]);
3003 sp->ipcp.max_state = p[4];
3004 sp->ipcp.compress_cid = p[5];
3008 addlog("compproto %#04x [not supported] ",
3010 p[2] = IPCP_COMP_VJ >> 8;
3011 p[3] = IPCP_COMP_VJ;
3012 p[4] = sp->ipcp.max_state;
3013 p[5] = sp->ipcp.compress_cid;
3015 case IPCP_OPT_ADDRESS:
3016 /* This is the address he wants in his end */
3017 desiredaddr = p[2] << 24 | p[3] << 16 |
3019 if (desiredaddr == hisaddr ||
3020 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3022 * Peer's address is same as our value,
3023 * or we have set it to 0.0.0.* to
3024 * indicate that we do not really care,
3025 * this is agreeable. Gonna conf-ack
3030 sppp_dotted_quad(hisaddr));
3031 /* record that we've seen it already */
3032 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3036 * The address wasn't agreeable. This is either
3037 * he sent us 0.0.0.0, asking to assign him an
3038 * address, or he send us another address not
3039 * matching our value. Either case, we gonna
3040 * conf-nak it with our value.
3041 * XXX: we should "rej" if hisaddr == 0
3044 if (desiredaddr == 0)
3045 addlog("[addr requested] ");
3047 addlog("%s [not agreed] ",
3048 sppp_dotted_quad(desiredaddr));
3051 p[2] = hisaddr >> 24;
3052 p[3] = hisaddr >> 16;
3053 p[4] = hisaddr >> 8;
3057 /* Add the option to nak'ed list. */
3064 * If we are about to conf-ack the request, but haven't seen
3065 * his address so far, gonna conf-nak it instead, with the
3066 * `address' option present and our idea of his address being
3067 * filled in there, to request negotiation of both addresses.
3069 * XXX This can result in an endless req - nak loop if peer
3070 * doesn't want to send us his address. Q: What should we do
3071 * about it? XXX A: implement the max-failure counter.
3073 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3074 buf[0] = IPCP_OPT_ADDRESS;
3076 buf[2] = hisaddr >> 24;
3077 buf[3] = hisaddr >> 16;
3078 buf[4] = hisaddr >> 8;
3082 addlog("still need hisaddr ");
3087 addlog(" send conf-nak\n");
3088 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3091 addlog(" send conf-ack\n");
3092 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3093 h->ident, origlen, h+1);
3101 * Analyze the IPCP Configure-Reject option list, and adjust our
3105 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3108 struct ifnet *ifp = &sp->pp_if;
3109 int debug = ifp->if_flags & IFF_DEBUG;
3112 buf = malloc (len, M_TEMP, M_INTWAIT);
3115 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3119 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3121 addlog(" %s ", sppp_ipcp_opt_name(*p));
3123 case IPCP_OPT_COMPRESSION:
3124 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3126 case IPCP_OPT_ADDRESS:
3128 * Peer doesn't grok address option. This is
3129 * bad. XXX Should we better give up here?
3130 * XXX We could try old "addresses" option...
3132 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3143 * Analyze the IPCP Configure-NAK option list, and adjust our
3147 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3150 struct ifnet *ifp = &sp->pp_if;
3151 int debug = ifp->if_flags & IFF_DEBUG;
3156 buf = malloc (len, M_TEMP, M_INTWAIT);
3159 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3163 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3165 addlog(" %s ", sppp_ipcp_opt_name(*p));
3167 case IPCP_OPT_COMPRESSION:
3168 if (len >= 6 && p[1] == 6) {
3169 desiredcomp = p[2] << 8 | p[3];
3171 addlog("[wantcomp %#04x] ",
3173 if (desiredcomp == IPCP_COMP_VJ) {
3174 sl_compress_init(sp->pp_comp, p[4]);
3175 sp->ipcp.max_state = p[4];
3176 sp->ipcp.compress_cid = p[5];
3181 ~(1 << IPCP_OPT_COMPRESSION);
3184 case IPCP_OPT_ADDRESS:
3186 * Peer doesn't like our local IP address. See
3187 * if we can do something for him. We'll drop
3188 * him our address then.
3190 if (len >= 6 && p[1] == 6) {
3191 wantaddr = p[2] << 24 | p[3] << 16 |
3193 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3195 addlog("[wantaddr %s] ",
3196 sppp_dotted_quad(wantaddr));
3198 * When doing dynamic address assignment,
3199 * we accept his offer. Otherwise, we
3200 * ignore it and thus continue to negotiate
3201 * our already existing value.
3202 * XXX: Bogus, if he said no once, he'll
3203 * just say no again, might as well die.
3205 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3206 sppp_set_ip_addr(sp, wantaddr);
3209 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3222 sppp_ipcp_tlu(struct sppp *sp)
3224 /* we are up - notify isdn daemon */
3230 sppp_ipcp_tld(struct sppp *sp)
3235 sppp_ipcp_tls(struct sppp *sp)
3237 /* indicate to LCP that it must stay alive */
3238 sp->lcp.protos |= (1 << IDX_IPCP);
3242 sppp_ipcp_tlf(struct sppp *sp)
3244 /* we no longer need LCP */
3245 sp->lcp.protos &= ~(1 << IDX_IPCP);
3246 sppp_lcp_check_and_close(sp);
3250 sppp_ipcp_scr(struct sppp *sp)
3252 char opt[6 /* compression */ + 6 /* address */];
3256 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3257 opt[i++] = IPCP_OPT_COMPRESSION;
3259 opt[i++] = IPCP_COMP_VJ >> 8;
3260 opt[i++] = IPCP_COMP_VJ;
3261 opt[i++] = sp->ipcp.max_state;
3262 opt[i++] = sp->ipcp.compress_cid;
3264 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3265 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3266 opt[i++] = IPCP_OPT_ADDRESS;
3268 opt[i++] = ouraddr >> 24;
3269 opt[i++] = ouraddr >> 16;
3270 opt[i++] = ouraddr >> 8;
3274 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3275 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3279 *--------------------------------------------------------------------------*
3281 * The IPv6CP implementation. *
3283 *--------------------------------------------------------------------------*
3288 sppp_ipv6cp_init(struct sppp *sp)
3290 sp->ipv6cp.opts = 0;
3291 sp->ipv6cp.flags = 0;
3292 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3293 sp->fail_counter[IDX_IPV6CP] = 0;
3294 sp->pp_seq[IDX_IPV6CP] = 0;
3295 sp->pp_rseq[IDX_IPV6CP] = 0;
3296 #if defined(__NetBSD__)
3297 callout_init(&sp->ch[IDX_IPV6CP]);
3299 #if defined(__DragonFly__)
3300 callout_handle_init(&sp->ch[IDX_IPV6CP]);
3305 sppp_ipv6cp_up(struct sppp *sp)
3307 sppp_up_event(&ipv6cp, sp);
3311 sppp_ipv6cp_down(struct sppp *sp)
3313 sppp_down_event(&ipv6cp, sp);
3317 sppp_ipv6cp_open(struct sppp *sp)
3320 struct in6_addr myaddr, hisaddr;
3322 #ifdef IPV6CP_MYIFID_DYN
3323 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3325 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3328 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3330 * If we don't have our address, this probably means our
3331 * interface doesn't want to talk IPv6 at all. (This could
3332 * be the case if somebody wants to speak only IPX, for
3333 * example.) Don't open IPv6CP in this case.
3335 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3336 /* XXX this message should go away */
3338 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3343 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3344 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3345 sppp_open_event(&ipv6cp, sp);
3349 sppp_ipv6cp_close(struct sppp *sp)
3351 sppp_close_event(&ipv6cp, sp);
3355 sppp_ipv6cp_TO(void *cookie)
3357 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3361 * Analyze a configure request. Return true if it was agreeable, and
3362 * caused action sca, false if it has been rejected or nak'ed, and
3363 * caused action scn. (The return value is used to make the state
3364 * transition decision in the state automaton.)
3367 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3369 u_char *buf, *r, *p;
3370 struct ifnet *ifp = &sp->pp_if;
3371 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3372 struct in6_addr myaddr, desiredaddr, suggestaddr;
3375 int collision, nohisaddr;
3380 * Make sure to allocate a buf that can at least hold a
3381 * conf-nak with an `address' option. We might need it below.
3383 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_INTWAIT);
3385 /* pass 1: see if we can recognize them */
3387 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3391 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3393 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3395 case IPV6CP_OPT_IFID:
3396 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3397 /* correctly formed address option */
3402 addlog(" [invalid]");
3405 case IPV6CP_OPT_COMPRESSION:
3406 if (len >= 4 && p[1] >= 4) {
3407 /* correctly formed compress option */
3411 addlog(" [invalid]");
3415 /* Others not supported. */
3420 /* Add the option to rejected list. */
3427 addlog(" send conf-rej\n");
3428 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3433 /* pass 2: parse option values */
3434 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3436 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3441 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3443 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3446 case IPV6CP_OPT_COMPRESSION:
3449 case IPV6CP_OPT_IFID:
3450 bzero(&desiredaddr, sizeof(desiredaddr));
3451 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3452 collision = (bcmp(&desiredaddr.s6_addr[8],
3453 &myaddr.s6_addr[8], 8) == 0);
3454 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3456 desiredaddr.s6_addr16[0] = htons(0xfe80);
3457 desiredaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3459 if (!collision && !nohisaddr) {
3460 /* no collision, hisaddr known - Conf-Ack */
3465 ip6_sprintf(&desiredaddr),
3466 sppp_cp_type_name(type));
3471 bzero(&suggestaddr, sizeof(&suggestaddr));
3472 if (collision && nohisaddr) {
3473 /* collision, hisaddr unknown - Conf-Rej */
3478 * - no collision, hisaddr unknown, or
3479 * - collision, hisaddr known
3480 * Conf-Nak, suggest hisaddr
3483 sppp_suggest_ip6_addr(sp, &suggestaddr);
3484 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3487 addlog(" %s [%s]", ip6_sprintf(&desiredaddr),
3488 sppp_cp_type_name(type));
3491 /* Add the option to nak'ed list. */
3497 if (rlen == 0 && type == CONF_ACK) {
3499 addlog(" send %s\n", sppp_cp_type_name(type));
3500 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3503 if (type == CONF_ACK)
3504 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3508 addlog(" send %s suggest %s\n",
3509 sppp_cp_type_name(type), ip6_sprintf(&suggestaddr));
3511 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3520 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3524 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3527 struct ifnet *ifp = &sp->pp_if;
3528 int debug = ifp->if_flags & IFF_DEBUG;
3531 buf = malloc (len, M_TEMP, M_INTWAIT);
3534 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3538 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3540 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3542 case IPV6CP_OPT_IFID:
3544 * Peer doesn't grok address option. This is
3545 * bad. XXX Should we better give up here?
3547 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3550 case IPV6CP_OPT_COMPRESS:
3551 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3563 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3567 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3570 struct ifnet *ifp = &sp->pp_if;
3571 int debug = ifp->if_flags & IFF_DEBUG;
3572 struct in6_addr suggestaddr;
3575 buf = malloc (len, M_TEMP, M_INTWAIT);
3578 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3582 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3584 addlog(" %s", sppp_ipv6cp_opt_name(*p));
3586 case IPV6CP_OPT_IFID:
3588 * Peer doesn't like our local ifid. See
3589 * if we can do something for him. We'll drop
3590 * him our address then.
3592 if (len < 10 || p[1] != 10)
3594 bzero(&suggestaddr, sizeof(suggestaddr));
3595 suggestaddr.s6_addr16[0] = htons(0xfe80);
3596 suggestaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3597 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3599 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3601 addlog(" [suggestaddr %s]",
3602 ip6_sprintf(&suggestaddr));
3603 #ifdef IPV6CP_MYIFID_DYN
3605 * When doing dynamic address assignment,
3606 * we accept his offer.
3608 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3609 struct in6_addr lastsuggest;
3611 * If <suggested myaddr from peer> equals to
3612 * <hisaddr we have suggested last time>,
3613 * we have a collision. generate new random
3616 sppp_suggest_ip6_addr(&lastsuggest);
3617 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3620 addlog(" [random]");
3621 sppp_gen_ip6_addr(sp, &suggestaddr);
3623 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3626 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3630 * Since we do not do dynamic address assignment,
3631 * we ignore it and thus continue to negotiate
3632 * our already existing value. This can possibly
3633 * go into infinite request-reject loop.
3635 * This is not likely because we normally use
3636 * ifid based on MAC-address.
3637 * If you have no ethernet card on the node, too bad.
3638 * XXX should we use fail_counter?
3643 case IPV6CP_OPT_COMPRESS:
3645 * Peer wants different compression parameters.
3657 sppp_ipv6cp_tlu(struct sppp *sp)
3659 /* we are up - notify isdn daemon */
3665 sppp_ipv6cp_tld(struct sppp *sp)
3670 sppp_ipv6cp_tls(struct sppp *sp)
3672 /* indicate to LCP that it must stay alive */
3673 sp->lcp.protos |= (1 << IDX_IPV6CP);
3677 sppp_ipv6cp_tlf(struct sppp *sp)
3680 #if 0 /* need #if 0 to close IPv6CP properly */
3681 /* we no longer need LCP */
3682 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3683 sppp_lcp_check_and_close(sp);
3688 sppp_ipv6cp_scr(struct sppp *sp)
3690 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3691 struct in6_addr ouraddr;
3694 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3695 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3696 opt[i++] = IPV6CP_OPT_IFID;
3698 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3703 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3704 opt[i++] = IPV6CP_OPT_COMPRESSION;
3706 opt[i++] = 0; /* TBD */
3707 opt[i++] = 0; /* TBD */
3708 /* variable length data may follow */
3712 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3713 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3716 static void sppp_ipv6cp_init(struct sppp *sp)
3720 static void sppp_ipv6cp_up(struct sppp *sp)
3724 static void sppp_ipv6cp_down(struct sppp *sp)
3729 static void sppp_ipv6cp_open(struct sppp *sp)
3733 static void sppp_ipv6cp_close(struct sppp *sp)
3737 static void sppp_ipv6cp_TO(void *sp)
3741 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3746 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3750 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3754 static void sppp_ipv6cp_tlu(struct sppp *sp)
3758 static void sppp_ipv6cp_tld(struct sppp *sp)
3762 static void sppp_ipv6cp_tls(struct sppp *sp)
3766 static void sppp_ipv6cp_tlf(struct sppp *sp)
3770 static void sppp_ipv6cp_scr(struct sppp *sp)
3776 *--------------------------------------------------------------------------*
3778 * The CHAP implementation. *
3780 *--------------------------------------------------------------------------*
3784 * The authentication protocols don't employ a full-fledged state machine as
3785 * the control protocols do, since they do have Open and Close events, but
3786 * not Up and Down, nor are they explicitly terminated. Also, use of the
3787 * authentication protocols may be different in both directions (this makes
3788 * sense, think of a machine that never accepts incoming calls but only
3789 * calls out, it doesn't require the called party to authenticate itself).
3791 * Our state machine for the local authentication protocol (we are requesting
3792 * the peer to authenticate) looks like:
3795 * +--------------------------------------------+
3797 * +--------+ Close +---------+ RCA+
3798 * | |<----------------------------------| |------+
3799 * +--->| Closed | TO* | Opened | sca |
3800 * | | |-----+ +-------| |<-----+
3801 * | +--------+ irc | | +---------+
3807 * | | +------->+ | |
3809 * | +--------+ V | |
3810 * | | |<----+<--------------------+ |
3816 * +------+ +------------------------------------------+
3817 * scn,tld sca,irc,ict,tlu
3822 * Open: LCP reached authentication phase
3823 * Close: LCP reached terminate phase
3825 * RCA+: received reply (pap-req, chap-response), acceptable
3826 * RCN: received reply (pap-req, chap-response), not acceptable
3827 * TO+: timeout with restart counter >= 0
3828 * TO-: timeout with restart counter < 0
3829 * TO*: reschedule timeout for CHAP
3831 * scr: send request packet (none for PAP, chap-challenge)
3832 * sca: send ack packet (pap-ack, chap-success)
3833 * scn: send nak packet (pap-nak, chap-failure)
3834 * ict: initialize re-challenge timer (CHAP only)
3836 * tlu: this-layer-up, LCP reaches network phase
3837 * tld: this-layer-down, LCP enters terminate phase
3839 * Note that in CHAP mode, after sending a new challenge, while the state
3840 * automaton falls back into Req-Sent state, it doesn't signal a tld
3841 * event to LCP, so LCP remains in network phase. Only after not getting
3842 * any response (or after getting an unacceptable response), CHAP closes,
3843 * causing LCP to enter terminate phase.
3845 * With PAP, there is no initial request that can be sent. The peer is
3846 * expected to send one based on the successful negotiation of PAP as
3847 * the authentication protocol during the LCP option negotiation.
3849 * Incoming authentication protocol requests (remote requests
3850 * authentication, we are peer) don't employ a state machine at all,
3851 * they are simply answered. Some peers [Ascend P50 firmware rev
3852 * 4.50] react allergically when sending IPCP requests while they are
3853 * still in authentication phase (thereby violating the standard that
3854 * demands that these NCP packets are to be discarded), so we keep
3855 * track of the peer demanding us to authenticate, and only proceed to
3856 * phase network once we've seen a positive acknowledge for the
3861 * Handle incoming CHAP packets.
3864 sppp_chap_input(struct sppp *sp, struct mbuf *m)
3867 struct lcp_header *h;
3869 u_char *value, *name, digest[AUTHKEYLEN], dsize;
3870 int value_len, name_len;
3873 len = m->m_pkthdr.len;
3877 SPP_FMT "chap invalid packet length: %d bytes\n",
3878 SPP_ARGS(ifp), len);
3881 h = mtod (m, struct lcp_header*);
3882 if (len > ntohs (h->len))
3883 len = ntohs (h->len);
3886 /* challenge, failure and success are his authproto */
3887 case CHAP_CHALLENGE:
3888 value = 1 + (u_char*)(h+1);
3889 value_len = value[-1];
3890 name = value + value_len;
3891 name_len = len - value_len - 5;
3895 SPP_FMT "chap corrupted challenge "
3896 "<%s id=0x%x len=%d",
3898 sppp_auth_type_name(PPP_CHAP, h->type),
3899 h->ident, ntohs(h->len));
3900 sppp_print_bytes((u_char*) (h+1), len-4);
3908 SPP_FMT "chap input <%s id=0x%x len=%d name=",
3910 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
3912 sppp_print_string((char*) name, name_len);
3913 addlog(" value-size=%d value=", value_len);
3914 sppp_print_bytes(value, value_len);
3918 /* Compute reply value. */
3920 MD5Update(&ctx, &h->ident, 1);
3921 MD5Update(&ctx, sp->myauth.secret,
3922 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
3923 MD5Update(&ctx, value, value_len);
3924 MD5Final(digest, &ctx);
3925 dsize = sizeof digest;
3927 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
3928 sizeof dsize, (const char *)&dsize,
3929 sizeof digest, digest,
3930 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3937 log(LOG_DEBUG, SPP_FMT "chap success",
3941 sppp_print_string((char*)(h + 1), len - 4);
3946 sp->pp_flags &= ~PP_NEEDAUTH;
3947 if (sp->myauth.proto == PPP_CHAP &&
3948 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
3949 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
3951 * We are authenticator for CHAP but didn't
3952 * complete yet. Leave it to tlu to proceed
3959 sppp_phase_network(sp);
3964 log(LOG_INFO, SPP_FMT "chap failure",
3968 sppp_print_string((char*)(h + 1), len - 4);
3972 log(LOG_INFO, SPP_FMT "chap failure\n",
3974 /* await LCP shutdown by authenticator */
3977 /* response is my authproto */
3979 value = 1 + (u_char*)(h+1);
3980 value_len = value[-1];
3981 name = value + value_len;
3982 name_len = len - value_len - 5;
3986 SPP_FMT "chap corrupted response "
3987 "<%s id=0x%x len=%d",
3989 sppp_auth_type_name(PPP_CHAP, h->type),
3990 h->ident, ntohs(h->len));
3991 sppp_print_bytes((u_char*)(h+1), len-4);
3996 if (h->ident != sp->confid[IDX_CHAP]) {
3999 SPP_FMT "chap dropping response for old ID "
4000 "(got %d, expected %d)\n",
4002 h->ident, sp->confid[IDX_CHAP]);
4005 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4006 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4007 log(LOG_INFO, SPP_FMT "chap response, his name ",
4009 sppp_print_string(name, name_len);
4010 addlog(" != expected ");
4011 sppp_print_string(sp->hisauth.name,
4012 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4016 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4017 "<%s id=0x%x len=%d name=",
4019 sppp_state_name(sp->state[IDX_CHAP]),
4020 sppp_auth_type_name(PPP_CHAP, h->type),
4021 h->ident, ntohs (h->len));
4022 sppp_print_string((char*)name, name_len);
4023 addlog(" value-size=%d value=", value_len);
4024 sppp_print_bytes(value, value_len);
4027 if (value_len != AUTHKEYLEN) {
4030 SPP_FMT "chap bad hash value length: "
4031 "%d bytes, should be %d\n",
4032 SPP_ARGS(ifp), value_len,
4038 MD5Update(&ctx, &h->ident, 1);
4039 MD5Update(&ctx, sp->hisauth.secret,
4040 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4041 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4042 MD5Final(digest, &ctx);
4044 #define FAILMSG "Failed..."
4045 #define SUCCMSG "Welcome!"
4047 if (value_len != sizeof digest ||
4048 bcmp(digest, value, value_len) != 0) {
4049 /* action scn, tld */
4050 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4051 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4056 /* action sca, perhaps tlu */
4057 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4058 sp->state[IDX_CHAP] == STATE_OPENED)
4059 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4060 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4062 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4063 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4069 /* Unknown CHAP packet type -- ignore. */
4071 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4072 "<0x%x id=0x%xh len=%d",
4074 sppp_state_name(sp->state[IDX_CHAP]),
4075 h->type, h->ident, ntohs(h->len));
4076 sppp_print_bytes((u_char*)(h+1), len-4);
4085 sppp_chap_init(struct sppp *sp)
4087 /* Chap doesn't have STATE_INITIAL at all. */
4088 sp->state[IDX_CHAP] = STATE_CLOSED;
4089 sp->fail_counter[IDX_CHAP] = 0;
4090 sp->pp_seq[IDX_CHAP] = 0;
4091 sp->pp_rseq[IDX_CHAP] = 0;
4092 #if defined(__DragonFly__)
4093 callout_handle_init(&sp->ch[IDX_CHAP]);
4098 sppp_chap_open(struct sppp *sp)
4100 if (sp->myauth.proto == PPP_CHAP &&
4101 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4102 /* we are authenticator for CHAP, start it */
4104 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4105 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4107 /* nothing to be done if we are peer, await a challenge */
4111 sppp_chap_close(struct sppp *sp)
4113 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4114 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4118 sppp_chap_TO(void *cookie)
4120 struct sppp *sp = (struct sppp *)cookie;
4126 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4128 sppp_state_name(sp->state[IDX_CHAP]),
4129 sp->rst_counter[IDX_CHAP]);
4131 if (--sp->rst_counter[IDX_CHAP] < 0)
4133 switch (sp->state[IDX_CHAP]) {
4134 case STATE_REQ_SENT:
4136 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4140 /* TO+ (or TO*) event */
4141 switch (sp->state[IDX_CHAP]) {
4144 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4146 case STATE_REQ_SENT:
4148 /* sppp_cp_change_state() will restart the timer */
4149 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4157 sppp_chap_tlu(struct sppp *sp)
4163 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4166 * Some broken CHAP implementations (Conware CoNet, firmware
4167 * 4.0.?) don't want to re-authenticate their CHAP once the
4168 * initial challenge-response exchange has taken place.
4169 * Provide for an option to avoid rechallenges.
4171 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4173 * Compute the re-challenge timeout. This will yield
4174 * a number between 300 and 810 seconds.
4176 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
4177 TIMEOUT(chap.TO, (void *)sp, i * hz, sp->ch[IDX_CHAP]);
4182 SPP_FMT "chap %s, ",
4184 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4185 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4186 addlog("next re-challenge in %d seconds\n", i);
4188 addlog("re-challenging supressed\n");
4192 /* indicate to LCP that we need to be closed down */
4193 sp->lcp.protos |= (1 << IDX_CHAP);
4195 if (sp->pp_flags & PP_NEEDAUTH) {
4197 * Remote is authenticator, but his auth proto didn't
4198 * complete yet. Defer the transition to network
4207 * If we are already in phase network, we are done here. This
4208 * is the case if this is a dummy tlu event after a re-challenge.
4210 if (sp->pp_phase != PHASE_NETWORK)
4211 sppp_phase_network(sp);
4215 sppp_chap_tld(struct sppp *sp)
4220 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4221 UNTIMEOUT(chap.TO, (void *)sp, sp->ch[IDX_CHAP]);
4222 sp->lcp.protos &= ~(1 << IDX_CHAP);
4228 sppp_chap_scr(struct sppp *sp)
4233 /* Compute random challenge. */
4234 ch = (u_long *)sp->myauth.challenge;
4235 #if defined(__DragonFly__)
4236 read_random(&seed, sizeof seed);
4241 seed = tv.tv_sec ^ tv.tv_usec;
4244 ch[0] = seed ^ random();
4245 ch[1] = seed ^ random();
4246 ch[2] = seed ^ random();
4247 ch[3] = seed ^ random();
4250 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4252 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4253 sizeof clen, (const char *)&clen,
4254 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4255 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4261 *--------------------------------------------------------------------------*
4263 * The PAP implementation. *
4265 *--------------------------------------------------------------------------*
4268 * For PAP, we need to keep a little state also if we are the peer, not the
4269 * authenticator. This is since we don't get a request to authenticate, but
4270 * have to repeatedly authenticate ourself until we got a response (or the
4271 * retry counter is expired).
4275 * Handle incoming PAP packets. */
4277 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4280 struct lcp_header *h;
4282 u_char *name, *passwd, mlen;
4283 int name_len, passwd_len;
4285 len = m->m_pkthdr.len;
4289 SPP_FMT "pap invalid packet length: %d bytes\n",
4290 SPP_ARGS(ifp), len);
4293 h = mtod (m, struct lcp_header*);
4294 if (len > ntohs (h->len))
4295 len = ntohs (h->len);
4297 /* PAP request is my authproto */
4299 name = 1 + (u_char*)(h+1);
4300 name_len = name[-1];
4301 passwd = name + name_len + 1;
4302 if (name_len > len - 6 ||
4303 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4305 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4306 "<%s id=0x%x len=%d",
4308 sppp_auth_type_name(PPP_PAP, h->type),
4309 h->ident, ntohs(h->len));
4310 sppp_print_bytes((u_char*)(h+1), len-4);
4316 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4317 "<%s id=0x%x len=%d name=",
4319 sppp_state_name(sp->state[IDX_PAP]),
4320 sppp_auth_type_name(PPP_PAP, h->type),
4321 h->ident, ntohs(h->len));
4322 sppp_print_string((char*)name, name_len);
4324 sppp_print_string((char*)passwd, passwd_len);
4327 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4328 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4329 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4330 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4331 /* action scn, tld */
4332 mlen = sizeof(FAILMSG) - 1;
4333 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4334 sizeof mlen, (const char *)&mlen,
4335 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4340 /* action sca, perhaps tlu */
4341 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4342 sp->state[IDX_PAP] == STATE_OPENED) {
4343 mlen = sizeof(SUCCMSG) - 1;
4344 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4345 sizeof mlen, (const char *)&mlen,
4346 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4349 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4350 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4355 /* ack and nak are his authproto */
4357 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
4359 log(LOG_DEBUG, SPP_FMT "pap success",
4361 name_len = *((char *)h);
4362 if (len > 5 && name_len) {
4364 sppp_print_string((char*)(h+1), name_len);
4369 sp->pp_flags &= ~PP_NEEDAUTH;
4370 if (sp->myauth.proto == PPP_PAP &&
4371 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4372 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4374 * We are authenticator for PAP but didn't
4375 * complete yet. Leave it to tlu to proceed
4382 sppp_phase_network(sp);
4386 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
4388 log(LOG_INFO, SPP_FMT "pap failure",
4390 name_len = *((char *)h);
4391 if (len > 5 && name_len) {
4393 sppp_print_string((char*)(h+1), name_len);
4397 log(LOG_INFO, SPP_FMT "pap failure\n",
4399 /* await LCP shutdown by authenticator */
4403 /* Unknown PAP packet type -- ignore. */
4405 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4406 "<0x%x id=0x%x len=%d",
4408 h->type, h->ident, ntohs(h->len));
4409 sppp_print_bytes((u_char*)(h+1), len-4);
4418 sppp_pap_init(struct sppp *sp)
4420 /* PAP doesn't have STATE_INITIAL at all. */
4421 sp->state[IDX_PAP] = STATE_CLOSED;
4422 sp->fail_counter[IDX_PAP] = 0;
4423 sp->pp_seq[IDX_PAP] = 0;
4424 sp->pp_rseq[IDX_PAP] = 0;
4425 #if defined(__DragonFly__)
4426 callout_handle_init(&sp->ch[IDX_PAP]);
4427 callout_handle_init(&sp->pap_my_to_ch);
4432 sppp_pap_open(struct sppp *sp)
4434 if (sp->hisauth.proto == PPP_PAP &&
4435 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4436 /* we are authenticator for PAP, start our timer */
4437 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4438 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4440 if (sp->myauth.proto == PPP_PAP) {
4441 /* we are peer, send a request, and start a timer */
4443 TIMEOUT(sppp_pap_my_TO, (void *)sp, sp->lcp.timeout,
4449 sppp_pap_close(struct sppp *sp)
4451 if (sp->state[IDX_PAP] != STATE_CLOSED)
4452 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4456 * That's the timeout routine if we are authenticator. Since the
4457 * authenticator is basically passive in PAP, we can't do much here.
4460 sppp_pap_TO(void *cookie)
4462 struct sppp *sp = (struct sppp *)cookie;
4468 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4470 sppp_state_name(sp->state[IDX_PAP]),
4471 sp->rst_counter[IDX_PAP]);
4473 if (--sp->rst_counter[IDX_PAP] < 0)
4475 switch (sp->state[IDX_PAP]) {
4476 case STATE_REQ_SENT:
4478 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4482 /* TO+ event, not very much we could do */
4483 switch (sp->state[IDX_PAP]) {
4484 case STATE_REQ_SENT:
4485 /* sppp_cp_change_state() will restart the timer */
4486 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4494 * That's the timeout handler if we are peer. Since the peer is active,
4495 * we need to retransmit our PAP request since it is apparently lost.
4496 * XXX We should impose a max counter.
4499 sppp_pap_my_TO(void *cookie)
4501 struct sppp *sp = (struct sppp *)cookie;
4505 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4512 sppp_pap_tlu(struct sppp *sp)
4517 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4520 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4521 SPP_ARGS(ifp), pap.name);
4524 /* indicate to LCP that we need to be closed down */
4525 sp->lcp.protos |= (1 << IDX_PAP);
4527 if (sp->pp_flags & PP_NEEDAUTH) {
4529 * Remote is authenticator, but his auth proto didn't
4530 * complete yet. Defer the transition to network
4537 sppp_phase_network(sp);
4541 sppp_pap_tld(struct sppp *sp)
4546 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4547 UNTIMEOUT(pap.TO, (void *)sp, sp->ch[IDX_PAP]);
4548 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
4549 sp->lcp.protos &= ~(1 << IDX_PAP);
4555 sppp_pap_scr(struct sppp *sp)
4557 u_char idlen, pwdlen;
4559 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4560 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4561 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4563 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4564 sizeof idlen, (const char *)&idlen,
4565 (size_t)idlen, sp->myauth.name,
4566 sizeof pwdlen, (const char *)&pwdlen,
4567 (size_t)pwdlen, sp->myauth.secret,
4572 * Random miscellaneous functions.
4576 * Send a PAP or CHAP proto packet.
4578 * Varadic function, each of the elements for the ellipsis is of type
4579 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4581 * NOTE: never declare variadic functions with types subject to type
4582 * promotion (i.e. u_char). This is asking for big trouble depending
4583 * on the architecture you are on...
4587 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4588 unsigned int type, unsigned int id,
4592 struct ppp_header *h;
4593 struct lcp_header *lh;
4601 MGETHDR (m, MB_DONTWAIT, MT_DATA);
4604 m->m_pkthdr.rcvif = 0;
4606 h = mtod (m, struct ppp_header*);
4607 h->address = PPP_ALLSTATIONS; /* broadcast address */
4608 h->control = PPP_UI; /* Unnumbered Info */
4609 h->protocol = htons(cp->proto);
4611 lh = (struct lcp_header*)(h + 1);
4614 p = (u_char*) (lh+1);
4619 while ((mlen = (unsigned int)__va_arg(ap, size_t)) != 0) {
4620 msg = __va_arg(ap, const char *);
4622 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4628 bcopy(msg, p, mlen);
4633 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4634 lh->len = htons (LCP_HEADER_LEN + len);
4637 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4638 SPP_ARGS(ifp), cp->name,
4639 sppp_auth_type_name(cp->proto, lh->type),
4640 lh->ident, ntohs(lh->len));
4641 sppp_print_bytes((u_char*) (lh+1), len);
4644 if (IF_QFULL (&sp->pp_cpq)) {
4645 IF_DROP (&sp->pp_fastq);
4646 IF_DROP (&ifp->if_snd);
4650 IF_ENQUEUE (&sp->pp_cpq, m);
4651 if (! (ifp->if_flags & IFF_OACTIVE))
4652 (*ifp->if_start) (ifp);
4653 ifp->if_obytes += m->m_pkthdr.len + 3;
4657 * Flush interface queue.
4660 sppp_qflush(struct ifqueue *ifq)
4675 * Send keepalive packets, every 10 seconds.
4678 sppp_keepalive(void *dummy)
4684 for (sp=spppq; sp; sp=sp->pp_next) {
4685 struct ifnet *ifp = &sp->pp_if;
4687 /* Keepalive mode disabled or channel down? */
4688 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4689 ! (ifp->if_flags & IFF_RUNNING))
4692 /* No keepalive in PPP mode if LCP not opened yet. */
4693 if (sp->pp_mode != IFF_CISCO &&
4694 sp->pp_phase < PHASE_AUTHENTICATE)
4697 if (sp->pp_alivecnt == MAXALIVECNT) {
4698 /* No keepalive packets got. Stop the interface. */
4699 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
4701 sppp_qflush (&sp->pp_cpq);
4702 if (sp->pp_mode != IFF_CISCO) {
4704 /* Shut down the PPP link. */
4706 /* Initiate negotiation. XXX */
4710 if (sp->pp_alivecnt <= MAXALIVECNT)
4712 if (sp->pp_mode == IFF_CISCO)
4713 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4714 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4715 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4716 long nmagic = htonl (sp->lcp.magic);
4717 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4718 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4719 sp->lcp.echoid, 4, &nmagic);
4723 TIMEOUT(sppp_keepalive, 0, hz * 10, keepalive_ch);
4727 * Get both IP addresses.
4730 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4732 struct ifnet *ifp = &sp->pp_if;
4734 struct sockaddr_in *si, *sm;
4740 * Pick the first AF_INET address from the list,
4741 * aliases don't make any sense on a p2p link anyway.
4744 #if defined(__DragonFly__)
4745 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4746 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4747 for (ifa = ifp->if_addrlist.tqh_first;
4749 ifa = ifa->ifa_list.tqe_next)
4751 for (ifa = ifp->if_addrlist;
4753 ifa = ifa->ifa_next)
4755 if (ifa->ifa_addr->sa_family == AF_INET) {
4756 si = (struct sockaddr_in *)ifa->ifa_addr;
4757 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4762 if (si && si->sin_addr.s_addr) {
4763 ssrc = si->sin_addr.s_addr;
4765 *srcmask = ntohl(sm->sin_addr.s_addr);
4768 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4769 if (si && si->sin_addr.s_addr)
4770 ddst = si->sin_addr.s_addr;
4773 if (dst) *dst = ntohl(ddst);
4774 if (src) *src = ntohl(ssrc);
4778 * Set my IP address. Must be called at splimp.
4781 sppp_set_ip_addr(struct sppp *sp, u_long src)
4785 struct sockaddr_in *si;
4786 struct in_ifaddr *ia;
4789 * Pick the first AF_INET address from the list,
4790 * aliases don't make any sense on a p2p link anyway.
4793 #if defined(__DragonFly__)
4794 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4795 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4796 for (ifa = ifp->if_addrlist.tqh_first;
4798 ifa = ifa->ifa_list.tqe_next)
4800 for (ifa = ifp->if_addrlist;
4802 ifa = ifa->ifa_next)
4805 if (ifa->ifa_addr->sa_family == AF_INET)
4807 si = (struct sockaddr_in *)ifa->ifa_addr;
4816 #if __NetBSD_Version__ >= 103080000
4817 struct sockaddr_in new_sin = *si;
4819 new_sin.sin_addr.s_addr = htonl(src);
4820 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 1);
4823 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: in_ifinit "
4824 " failed, error=%d\n", SPP_ARGS(ifp), error);
4827 /* delete old route */
4828 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4831 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4832 SPP_ARGS(ifp), error);
4835 /* set new address */
4836 si->sin_addr.s_addr = htonl(src);
4838 LIST_REMOVE(ia, ia_hash);
4839 LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash);
4842 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
4845 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
4846 SPP_ARGS(ifp), error);
4854 * Get both IPv6 addresses.
4857 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4858 struct in6_addr *srcmask)
4860 struct ifnet *ifp = &sp->pp_if;
4862 struct sockaddr_in6 *si, *sm;
4863 struct in6_addr ssrc, ddst;
4866 bzero(&ssrc, sizeof(ssrc));
4867 bzero(&ddst, sizeof(ddst));
4869 * Pick the first link-local AF_INET6 address from the list,
4870 * aliases don't make any sense on a p2p link anyway.
4872 #if defined(__DragonFly__)
4873 for (ifa = ifp->if_addrhead.tqh_first, si = 0;
4875 ifa = ifa->ifa_link.tqe_next)
4876 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4877 for (ifa = ifp->if_addrlist.tqh_first, si = 0;
4879 ifa = ifa->ifa_list.tqe_next)
4881 for (ifa = ifp->if_addrlist, si = 0;
4883 ifa = ifa->ifa_next)
4885 if (ifa->ifa_addr->sa_family == AF_INET6) {
4886 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4887 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4888 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
4892 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
4893 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
4895 bcopy(&sm->sin6_addr, srcmask,
4900 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
4901 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
4902 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
4906 bcopy(&ddst, dst, sizeof(*dst));
4908 bcopy(&ssrc, src, sizeof(*src));
4911 #ifdef IPV6CP_MYIFID_DYN
4913 * Generate random ifid.
4916 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
4922 * Set my IPv6 address. Must be called at splimp.
4925 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
4929 struct sockaddr_in6 *sin6;
4932 * Pick the first link-local AF_INET6 address from the list,
4933 * aliases don't make any sense on a p2p link anyway.
4937 #if defined(__DragonFly__)
4938 for (ifa = ifp->if_addrhead.tqh_first;
4940 ifa = ifa->ifa_link.tqe_next)
4941 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4942 for (ifa = ifp->if_addrlist.tqh_first;
4944 ifa = ifa->ifa_list.tqe_next)
4946 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
4949 if (ifa->ifa_addr->sa_family == AF_INET6)
4951 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
4952 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
4960 struct sockaddr_in6 new_sin6 = *sin6;
4962 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
4963 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
4966 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
4967 " failed, error=%d\n", SPP_ARGS(ifp), error);
4974 * Suggest a candidate address to be used by peer.
4977 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
4979 struct in6_addr myaddr;
4982 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
4984 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
4986 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
4987 myaddr.s6_addr[14] ^= 0xff;
4988 myaddr.s6_addr[15] ^= 0xff;
4990 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
4991 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
4994 bcopy(&myaddr, suggest, sizeof(myaddr));
4999 sppp_params(struct sppp *sp, u_long cmd, void *data)
5002 struct ifreq *ifr = (struct ifreq *)data;
5003 struct spppreq *spr;
5006 spr = malloc(sizeof(struct spppreq), M_TEMP, M_INTWAIT);
5009 * ifr->ifr_data is supposed to point to a struct spppreq.
5010 * Check the cmd word first before attempting to fetch all the
5013 if ((subcmd = fuword(ifr->ifr_data)) == -1) {
5018 if (copyin((caddr_t)ifr->ifr_data, spr, sizeof(struct spppreq)) != 0) {
5024 case (int)SPPPIOGDEFS:
5025 if (cmd != SIOCGIFGENERIC) {
5030 * We copy over the entire current state, but clean
5031 * out some of the stuff we don't wanna pass up.
5032 * Remember, SIOCGIFGENERIC is unprotected, and can be
5033 * called by any user. No need to ever get PAP or
5034 * CHAP secrets back to userland anyway.
5036 spr->defs.pp_phase = sp->pp_phase;
5037 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5038 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5039 spr->defs.lcp = sp->lcp;
5040 spr->defs.ipcp = sp->ipcp;
5041 spr->defs.ipv6cp = sp->ipv6cp;
5042 spr->defs.myauth = sp->myauth;
5043 spr->defs.hisauth = sp->hisauth;
5044 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5045 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5046 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5047 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5049 * Fixup the LCP timeout value to milliseconds so
5050 * spppcontrol doesn't need to bother about the value
5051 * of "hz". We do the reverse calculation below when
5054 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5055 rv = copyout(spr, (caddr_t)ifr->ifr_data,
5056 sizeof(struct spppreq));
5059 case (int)SPPPIOSDEFS:
5060 if (cmd != SIOCSIFGENERIC) {
5065 * We have a very specific idea of which fields we
5066 * allow being passed back from userland, so to not
5067 * clobber our current state. For one, we only allow
5068 * setting anything if LCP is in dead or establish
5069 * phase. Once the authentication negotiations
5070 * started, the authentication settings must not be
5071 * changed again. (The administrator can force an
5072 * ifconfig down in order to get LCP back into dead
5075 * Also, we only allow for authentication parameters to be
5078 * XXX Should allow to set or clear pp_flags.
5080 * Finally, if the respective authentication protocol to
5081 * be used is set differently than 0, but the secret is
5082 * passed as all zeros, we don't trash the existing secret.
5083 * This allows an administrator to change the system name
5084 * only without clobbering the secret (which he didn't get
5085 * back in a previous SPPPIOGDEFS call). However, the
5086 * secrets are cleared if the authentication protocol is
5088 if (sp->pp_phase != PHASE_DEAD &&
5089 sp->pp_phase != PHASE_ESTABLISH) {
5094 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5095 spr->defs.myauth.proto != PPP_CHAP) ||
5096 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5097 spr->defs.hisauth.proto != PPP_CHAP)) {
5102 if (spr->defs.myauth.proto == 0)
5103 /* resetting myauth */
5104 bzero(&sp->myauth, sizeof sp->myauth);
5106 /* setting/changing myauth */
5107 sp->myauth.proto = spr->defs.myauth.proto;
5108 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5109 if (spr->defs.myauth.secret[0] != '\0')
5110 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5113 if (spr->defs.hisauth.proto == 0)
5114 /* resetting hisauth */
5115 bzero(&sp->hisauth, sizeof sp->hisauth);
5117 /* setting/changing hisauth */
5118 sp->hisauth.proto = spr->defs.hisauth.proto;
5119 sp->hisauth.flags = spr->defs.hisauth.flags;
5120 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5121 if (spr->defs.hisauth.secret[0] != '\0')
5122 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5125 /* set LCP restart timer timeout */
5126 if (spr->defs.lcp.timeout != 0)
5127 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5128 /* set VJ enable and IPv6 disable flags */
5130 if (spr->defs.enable_vj)
5131 sp->confflags |= CONF_ENABLE_VJ;
5133 sp->confflags &= ~CONF_ENABLE_VJ;
5136 if (spr->defs.enable_ipv6)
5137 sp->confflags |= CONF_ENABLE_IPV6;
5139 sp->confflags &= ~CONF_ENABLE_IPV6;
5154 sppp_phase_network(struct sppp *sp)
5160 sp->pp_phase = PHASE_NETWORK;
5163 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5164 sppp_phase_name(sp->pp_phase));
5166 /* Notify NCPs now. */
5167 for (i = 0; i < IDX_COUNT; i++)
5168 if ((cps[i])->flags & CP_NCP)
5171 /* Send Up events to all NCPs. */
5172 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5173 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5176 /* if no NCP is starting, all this was in vain, close down */
5177 sppp_lcp_check_and_close(sp);
5182 sppp_cp_type_name(u_char type)
5184 static char buf[12];
5186 case CONF_REQ: return "conf-req";
5187 case CONF_ACK: return "conf-ack";
5188 case CONF_NAK: return "conf-nak";
5189 case CONF_REJ: return "conf-rej";
5190 case TERM_REQ: return "term-req";
5191 case TERM_ACK: return "term-ack";
5192 case CODE_REJ: return "code-rej";
5193 case PROTO_REJ: return "proto-rej";
5194 case ECHO_REQ: return "echo-req";
5195 case ECHO_REPLY: return "echo-reply";
5196 case DISC_REQ: return "discard-req";
5198 snprintf (buf, sizeof(buf), "cp/0x%x", type);
5203 sppp_auth_type_name(u_short proto, u_char type)
5205 static char buf[12];
5209 case CHAP_CHALLENGE: return "challenge";
5210 case CHAP_RESPONSE: return "response";
5211 case CHAP_SUCCESS: return "success";
5212 case CHAP_FAILURE: return "failure";
5216 case PAP_REQ: return "req";
5217 case PAP_ACK: return "ack";
5218 case PAP_NAK: return "nak";
5221 snprintf (buf, sizeof(buf), "auth/0x%x", type);
5226 sppp_lcp_opt_name(u_char opt)
5228 static char buf[12];
5230 case LCP_OPT_MRU: return "mru";
5231 case LCP_OPT_ASYNC_MAP: return "async-map";
5232 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5233 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5234 case LCP_OPT_MAGIC: return "magic";
5235 case LCP_OPT_PROTO_COMP: return "proto-comp";
5236 case LCP_OPT_ADDR_COMP: return "addr-comp";
5238 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5243 sppp_ipcp_opt_name(u_char opt)
5245 static char buf[12];
5247 case IPCP_OPT_ADDRESSES: return "addresses";
5248 case IPCP_OPT_COMPRESSION: return "compression";
5249 case IPCP_OPT_ADDRESS: return "address";
5251 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5257 sppp_ipv6cp_opt_name(u_char opt)
5259 static char buf[12];
5261 case IPV6CP_OPT_IFID: return "ifid";
5262 case IPV6CP_OPT_COMPRESSION: return "compression";
5264 sprintf (buf, "0x%x", opt);
5270 sppp_state_name(int state)
5273 case STATE_INITIAL: return "initial";
5274 case STATE_STARTING: return "starting";
5275 case STATE_CLOSED: return "closed";
5276 case STATE_STOPPED: return "stopped";
5277 case STATE_CLOSING: return "closing";
5278 case STATE_STOPPING: return "stopping";
5279 case STATE_REQ_SENT: return "req-sent";
5280 case STATE_ACK_RCVD: return "ack-rcvd";
5281 case STATE_ACK_SENT: return "ack-sent";
5282 case STATE_OPENED: return "opened";
5288 sppp_phase_name(enum ppp_phase phase)
5291 case PHASE_DEAD: return "dead";
5292 case PHASE_ESTABLISH: return "establish";
5293 case PHASE_TERMINATE: return "terminate";
5294 case PHASE_AUTHENTICATE: return "authenticate";
5295 case PHASE_NETWORK: return "network";
5301 sppp_proto_name(u_short proto)
5303 static char buf[12];
5305 case PPP_LCP: return "lcp";
5306 case PPP_IPCP: return "ipcp";
5307 case PPP_PAP: return "pap";
5308 case PPP_CHAP: return "chap";
5309 case PPP_IPV6CP: return "ipv6cp";
5311 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5316 sppp_print_bytes(const u_char *p, u_short len)
5319 addlog(" %*D", len, p, "-");
5323 sppp_print_string(const char *p, u_short len)
5330 * Print only ASCII chars directly. RFC 1994 recommends
5331 * using only them, but we don't rely on it. */
5332 if (c < ' ' || c > '~')
5340 sppp_dotted_quad(u_long addr)
5343 sprintf(s, "%d.%d.%d.%d",
5344 (int)((addr >> 24) & 0xff),
5345 (int)((addr >> 16) & 0xff),
5346 (int)((addr >> 8) & 0xff),
5347 (int)(addr & 0xff));
5352 sppp_strnlen(u_char *p, int max)
5356 for (len = 0; len < max && *p; ++p)
5361 /* a dummy, used to drop uninteresting events */
5363 sppp_null(struct sppp *unused)
5365 /* do just nothing */