2 * Synchronous PPP/Cisco/Frame Relay link level subroutines.
3 * Keepalive protocol implemented in both Cisco and PPP modes.
6 * Copyright (C) 1994-2000 Cronyx Engineering.
7 * Author: Serge Vakulenko, <vak@cronyx.ru>
9 * Heavily revamped to conform to RFC 1661.
10 * Copyright (C) 1997, 2001 Joerg Wunsch.
12 * This software is distributed with NO WARRANTIES, not even the implied
13 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * Authors grant any other persons or organisations permission to use
16 * or modify this software as long as this message is kept with the software,
17 * all derivative works or modified versions.
19 * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997
24 #include <sys/param.h>
27 #include "opt_inet6.h"
29 #include <sys/systm.h>
30 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/rmlock.h>
34 #include <sys/sockio.h>
35 #include <sys/socket.h>
36 #include <sys/syslog.h>
37 #include <sys/random.h>
38 #include <sys/malloc.h>
44 #include <net/if_var.h>
45 #include <net/netisr.h>
46 #include <net/if_types.h>
47 #include <net/route.h>
49 #include <netinet/in.h>
50 #include <netinet/in_var.h>
51 #include <netinet/in_systm.h>
52 #include <netinet/ip.h>
53 #include <net/slcompress.h>
55 #include <machine/stdarg.h>
57 #include <netinet/in_var.h>
60 #include <netinet/ip.h>
61 #include <netinet/tcp.h>
65 #include <netinet6/scope6_var.h>
68 #include <netinet/if_ether.h>
70 #include <net/if_sppp.h>
72 #define IOCTL_CMD_T u_long
73 #define MAXALIVECNT 3 /* max. alive packets */
76 * Interface flags that can be set in an ifconfig command.
78 * Setting link0 will make the link passive, i.e. it will be marked
79 * as being administrative openable, but won't be opened to begin
80 * with. Incoming calls will be answered, or subsequent calls with
81 * -link1 will cause the administrative open of the LCP layer.
83 * Setting link1 will cause the link to auto-dial only as packets
86 * Setting IFF_DEBUG will syslog the option negotiation and state
87 * transitions at level kern.debug. Note: all logs consistently look
90 * <if-name><unit>: <proto-name> <additional info...>
92 * with <if-name><unit> being something like "bppp0", and <proto-name>
93 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
96 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
97 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
98 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
100 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
101 #define PPP_UI 0x03 /* Unnumbered Information */
102 #define PPP_IP 0x0021 /* Internet Protocol */
103 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
104 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
105 #define PPP_IPX 0x002b /* Novell IPX Protocol */
106 #define PPP_VJ_COMP 0x002d /* VJ compressed TCP/IP */
107 #define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */
108 #define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */
109 #define PPP_LCP 0xc021 /* Link Control Protocol */
110 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
111 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
112 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
113 #define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */
115 #define CONF_REQ 1 /* PPP configure request */
116 #define CONF_ACK 2 /* PPP configure acknowledge */
117 #define CONF_NAK 3 /* PPP configure negative ack */
118 #define CONF_REJ 4 /* PPP configure reject */
119 #define TERM_REQ 5 /* PPP terminate request */
120 #define TERM_ACK 6 /* PPP terminate acknowledge */
121 #define CODE_REJ 7 /* PPP code reject */
122 #define PROTO_REJ 8 /* PPP protocol reject */
123 #define ECHO_REQ 9 /* PPP echo request */
124 #define ECHO_REPLY 10 /* PPP echo reply */
125 #define DISC_REQ 11 /* PPP discard request */
127 #define LCP_OPT_MRU 1 /* maximum receive unit */
128 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
129 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
130 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
131 #define LCP_OPT_MAGIC 5 /* magic number */
132 #define LCP_OPT_RESERVED 6 /* reserved */
133 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
134 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
136 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
137 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
138 #define IPCP_OPT_ADDRESS 3 /* local IP address */
140 #define IPV6CP_OPT_IFID 1 /* interface identifier */
141 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
143 #define IPCP_COMP_VJ 0x2d /* Code for VJ compression */
145 #define PAP_REQ 1 /* PAP name/password request */
146 #define PAP_ACK 2 /* PAP acknowledge */
147 #define PAP_NAK 3 /* PAP fail */
149 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
150 #define CHAP_RESPONSE 2 /* CHAP challenge response */
151 #define CHAP_SUCCESS 3 /* CHAP response ok */
152 #define CHAP_FAILURE 4 /* CHAP response failed */
154 #define CHAP_MD5 5 /* hash algorithm - MD5 */
156 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
157 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
158 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
159 #define CISCO_ADDR_REQ 0 /* Cisco address request */
160 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
161 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
163 /* states are named and numbered according to RFC 1661 */
164 #define STATE_INITIAL 0
165 #define STATE_STARTING 1
166 #define STATE_CLOSED 2
167 #define STATE_STOPPED 3
168 #define STATE_CLOSING 4
169 #define STATE_STOPPING 5
170 #define STATE_REQ_SENT 6
171 #define STATE_ACK_RCVD 7
172 #define STATE_ACK_SENT 8
173 #define STATE_OPENED 9
175 static MALLOC_DEFINE(M_SPPP, "sppp", "synchronous PPP interface internals");
182 #define PPP_HEADER_LEN sizeof (struct ppp_header)
189 #define LCP_HEADER_LEN sizeof (struct lcp_header)
191 struct cisco_packet {
199 #define CISCO_PACKET_LEN sizeof (struct cisco_packet)
202 * We follow the spelling and capitalization of RFC 1661 here, to make
203 * it easier comparing with the standard. Please refer to this RFC in
204 * case you can't make sense out of these abbreviation; it will also
205 * explain the semantics related to the various events and actions.
208 u_short proto; /* PPP control protocol number */
209 u_char protoidx; /* index into state table in struct sppp */
211 #define CP_LCP 0x01 /* this is the LCP */
212 #define CP_AUTH 0x02 /* this is an authentication protocol */
213 #define CP_NCP 0x04 /* this is a NCP */
214 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
215 const char *name; /* name of this control protocol */
217 void (*Up)(struct sppp *sp);
218 void (*Down)(struct sppp *sp);
219 void (*Open)(struct sppp *sp);
220 void (*Close)(struct sppp *sp);
221 void (*TO)(void *sp);
222 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
223 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
224 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
226 void (*tlu)(struct sppp *sp);
227 void (*tld)(struct sppp *sp);
228 void (*tls)(struct sppp *sp);
229 void (*tlf)(struct sppp *sp);
230 void (*scr)(struct sppp *sp);
233 #define SPP_FMT "%s: "
234 #define SPP_ARGS(ifp) (ifp)->if_xname
236 #define SPPP_LOCK(sp) mtx_lock (&(sp)->mtx)
237 #define SPPP_UNLOCK(sp) mtx_unlock (&(sp)->mtx)
238 #define SPPP_LOCK_ASSERT(sp) mtx_assert (&(sp)->mtx, MA_OWNED)
239 #define SPPP_LOCK_OWNED(sp) mtx_owned (&(sp)->mtx)
243 * The following disgusting hack gets around the problem that IP TOS
244 * can't be set yet. We want to put "interactive" traffic on a high
245 * priority queue. To decide if traffic is interactive, we check that
246 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
248 * XXX is this really still necessary? - joerg -
250 static const u_short interactive_ports[8] = {
254 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
257 /* almost every function needs these */
259 struct ifnet *ifp = SP2IFP(sp); \
260 int debug = ifp->if_flags & IFF_DEBUG
262 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
263 const struct sockaddr *dst, struct route *ro);
265 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
266 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
268 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
270 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
271 u_char ident, u_short len, void *data);
272 /* static void sppp_cp_timeout(void *arg); */
273 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
275 static void sppp_auth_send(const struct cp *cp,
276 struct sppp *sp, unsigned int type, unsigned int id,
279 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
280 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
281 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
282 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
283 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
285 static void sppp_null(struct sppp *sp);
287 static void sppp_pp_up(struct sppp *sp);
288 static void sppp_pp_down(struct sppp *sp);
290 static void sppp_lcp_init(struct sppp *sp);
291 static void sppp_lcp_up(struct sppp *sp);
292 static void sppp_lcp_down(struct sppp *sp);
293 static void sppp_lcp_open(struct sppp *sp);
294 static void sppp_lcp_close(struct sppp *sp);
295 static void sppp_lcp_TO(void *sp);
296 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
297 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
298 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
299 static void sppp_lcp_tlu(struct sppp *sp);
300 static void sppp_lcp_tld(struct sppp *sp);
301 static void sppp_lcp_tls(struct sppp *sp);
302 static void sppp_lcp_tlf(struct sppp *sp);
303 static void sppp_lcp_scr(struct sppp *sp);
304 static void sppp_lcp_check_and_close(struct sppp *sp);
305 static int sppp_ncp_check(struct sppp *sp);
307 static void sppp_ipcp_init(struct sppp *sp);
308 static void sppp_ipcp_up(struct sppp *sp);
309 static void sppp_ipcp_down(struct sppp *sp);
310 static void sppp_ipcp_open(struct sppp *sp);
311 static void sppp_ipcp_close(struct sppp *sp);
312 static void sppp_ipcp_TO(void *sp);
313 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
314 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
315 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
316 static void sppp_ipcp_tlu(struct sppp *sp);
317 static void sppp_ipcp_tld(struct sppp *sp);
318 static void sppp_ipcp_tls(struct sppp *sp);
319 static void sppp_ipcp_tlf(struct sppp *sp);
320 static void sppp_ipcp_scr(struct sppp *sp);
322 static void sppp_ipv6cp_init(struct sppp *sp);
323 static void sppp_ipv6cp_up(struct sppp *sp);
324 static void sppp_ipv6cp_down(struct sppp *sp);
325 static void sppp_ipv6cp_open(struct sppp *sp);
326 static void sppp_ipv6cp_close(struct sppp *sp);
327 static void sppp_ipv6cp_TO(void *sp);
328 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
329 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
330 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
331 static void sppp_ipv6cp_tlu(struct sppp *sp);
332 static void sppp_ipv6cp_tld(struct sppp *sp);
333 static void sppp_ipv6cp_tls(struct sppp *sp);
334 static void sppp_ipv6cp_tlf(struct sppp *sp);
335 static void sppp_ipv6cp_scr(struct sppp *sp);
337 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
338 static void sppp_pap_init(struct sppp *sp);
339 static void sppp_pap_open(struct sppp *sp);
340 static void sppp_pap_close(struct sppp *sp);
341 static void sppp_pap_TO(void *sp);
342 static void sppp_pap_my_TO(void *sp);
343 static void sppp_pap_tlu(struct sppp *sp);
344 static void sppp_pap_tld(struct sppp *sp);
345 static void sppp_pap_scr(struct sppp *sp);
347 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
348 static void sppp_chap_init(struct sppp *sp);
349 static void sppp_chap_open(struct sppp *sp);
350 static void sppp_chap_close(struct sppp *sp);
351 static void sppp_chap_TO(void *sp);
352 static void sppp_chap_tlu(struct sppp *sp);
353 static void sppp_chap_tld(struct sppp *sp);
354 static void sppp_chap_scr(struct sppp *sp);
356 static const char *sppp_auth_type_name(u_short proto, u_char type);
357 static const char *sppp_cp_type_name(u_char type);
359 static const char *sppp_dotted_quad(u_long addr);
360 static const char *sppp_ipcp_opt_name(u_char opt);
363 static const char *sppp_ipv6cp_opt_name(u_char opt);
365 static const char *sppp_lcp_opt_name(u_char opt);
366 static const char *sppp_phase_name(enum ppp_phase phase);
367 static const char *sppp_proto_name(u_short proto);
368 static const char *sppp_state_name(int state);
369 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
370 static int sppp_strnlen(u_char *p, int max);
371 static void sppp_keepalive(void *dummy);
372 static void sppp_phase_network(struct sppp *sp);
373 static void sppp_print_bytes(const u_char *p, u_short len);
374 static void sppp_print_string(const char *p, u_short len);
375 static void sppp_qflush(struct ifqueue *ifq);
377 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
380 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
381 struct in6_addr *dst, struct in6_addr *srcmask);
382 #ifdef IPV6CP_MYIFID_DYN
383 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
384 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
386 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
389 /* if_start () wrapper */
390 static void sppp_ifstart (struct ifnet *ifp);
392 /* our control protocol descriptors */
393 static const struct cp lcp = {
394 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
395 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
396 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
397 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
401 static const struct cp ipcp = {
403 #ifdef INET /* don't run IPCP if there's no IPv4 support */
409 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
410 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
411 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
415 static const struct cp ipv6cp = {
416 PPP_IPV6CP, IDX_IPV6CP,
417 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
423 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
424 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
425 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
429 static const struct cp pap = {
430 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
431 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
432 sppp_pap_TO, 0, 0, 0,
433 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
437 static const struct cp chap = {
438 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
439 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
440 sppp_chap_TO, 0, 0, 0,
441 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
445 static const struct cp *cps[IDX_COUNT] = {
447 &ipcp, /* IDX_IPCP */
448 &ipv6cp, /* IDX_IPV6CP */
450 &chap, /* IDX_CHAP */
454 sppp_alloc(u_char type, struct ifnet *ifp)
458 sp = malloc(sizeof(struct sppp), M_SPPP, M_WAITOK | M_ZERO);
465 sppp_free(void *com, u_char type)
472 sppp_modevent(module_t mod, int type, void *unused)
477 * XXX: should probably be IFT_SPPP, but it's fairly
478 * harmless to allocate struct sppp's for non-sppp
482 if_register_com_alloc(IFT_PPP, sppp_alloc, sppp_free);
485 /* if_deregister_com_alloc(IFT_PPP); */
492 static moduledata_t spppmod = {
497 MODULE_VERSION(sppp, 1);
498 DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
501 * Exported functions, comprising our interface to the lower layer.
505 * Process the received packet.
508 sppp_input(struct ifnet *ifp, struct mbuf *m)
510 struct ppp_header *h;
512 struct sppp *sp = IFP2SP(ifp);
513 int debug, do_account = 0;
520 debug = ifp->if_flags & IFF_DEBUG;
522 if (ifp->if_flags & IFF_UP)
523 /* Count received bytes, add FCS and one flag */
524 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len + 3);
526 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
527 /* Too small packet, drop it. */
530 SPP_FMT "input packet is too small, %d bytes\n",
531 SPP_ARGS(ifp), m->m_pkthdr.len);
536 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
537 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
541 if (sp->pp_mode == PP_FR) {
542 sppp_fr_input (sp, m);
547 /* Get PPP header. */
548 h = mtod (m, struct ppp_header*);
549 m_adj (m, PPP_HEADER_LEN);
551 switch (h->address) {
552 case PPP_ALLSTATIONS:
553 if (h->control != PPP_UI)
555 if (sp->pp_mode == IFF_CISCO) {
558 SPP_FMT "PPP packet in Cisco mode "
559 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
561 h->address, h->control, ntohs(h->protocol));
564 switch (ntohs (h->protocol)) {
568 SPP_FMT "rejecting protocol "
569 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
571 h->address, h->control, ntohs(h->protocol));
572 if (sp->state[IDX_LCP] == STATE_OPENED)
573 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
574 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
576 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
579 sppp_cp_input(&lcp, sp, m);
584 if (sp->pp_phase >= PHASE_AUTHENTICATE)
585 sppp_pap_input(sp, m);
590 if (sp->pp_phase >= PHASE_AUTHENTICATE)
591 sppp_chap_input(sp, m);
597 if (sp->pp_phase == PHASE_NETWORK)
598 sppp_cp_input(&ipcp, sp, m);
603 if (sp->state[IDX_IPCP] == STATE_OPENED) {
609 if (sp->state[IDX_IPCP] == STATE_OPENED) {
611 sl_uncompress_tcp_core(mtod(m, u_char *),
615 &iphdr, &hlen)) <= 0) {
618 SPP_FMT "VJ uncompress failed on compressed packet\n",
624 * Trim the VJ header off the packet, and prepend
625 * the uncompressed IP header (which will usually
626 * end up in two chained mbufs since there's not
627 * enough leading space in the existing mbuf).
630 M_PREPEND(m, hlen, M_NOWAIT);
635 bcopy(iphdr, mtod(m, u_char *), hlen);
641 if (sp->state[IDX_IPCP] == STATE_OPENED) {
642 if (sl_uncompress_tcp_core(mtod(m, u_char *),
644 TYPE_UNCOMPRESSED_TCP,
646 &iphdr, &hlen) != 0) {
649 SPP_FMT "VJ uncompress failed on uncompressed packet\n",
660 if (sp->pp_phase == PHASE_NETWORK)
661 sppp_cp_input(&ipv6cp, sp, m);
667 if (sp->state[IDX_IPV6CP] == STATE_OPENED)
674 case CISCO_MULTICAST:
676 /* Don't check the control field here (RFC 1547). */
677 if (sp->pp_mode != IFF_CISCO) {
680 SPP_FMT "Cisco packet in PPP mode "
681 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
683 h->address, h->control, ntohs(h->protocol));
686 switch (ntohs (h->protocol)) {
688 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
690 case CISCO_KEEPALIVE:
691 sppp_cisco_input (sp, m);
709 default: /* Invalid PPP packet. */
713 SPP_FMT "invalid input packet "
714 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
716 h->address, h->control, ntohs(h->protocol));
720 if (! (ifp->if_flags & IFF_UP) || isr == -1)
724 M_SETFIB(m, ifp->if_fib);
726 if (netisr_queue(isr, m)) { /* (0) on success. */
728 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
735 * Do only account for network packets, not for control
736 * packets. This is used by some subsystems to detect
739 sp->pp_last_recv = time_uptime;
743 sppp_ifstart_sched(void *dummy)
745 struct sppp *sp = dummy;
747 sp->if_start(SP2IFP(sp));
750 /* if_start () wrapper function. We use it to schedule real if_start () for
751 * execution. We can't call it directly
754 sppp_ifstart(struct ifnet *ifp)
756 struct sppp *sp = IFP2SP(ifp);
758 if (SPPP_LOCK_OWNED(sp)) {
759 if (callout_pending(&sp->ifstart_callout))
761 callout_reset(&sp->ifstart_callout, 1, sppp_ifstart_sched,
769 * Enqueue transmit packet.
772 sppp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
775 struct sppp *sp = IFP2SP(ifp);
776 struct ppp_header *h;
777 struct ifqueue *ifq = NULL;
780 int ipproto = PPP_IP;
782 int debug = ifp->if_flags & IFF_DEBUG;
783 int af = RO_GET_FAMILY(ro, dst);
787 if (!(ifp->if_flags & IFF_UP) ||
788 (!(ifp->if_flags & IFF_AUTO) &&
789 !(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
798 if ((ifp->if_flags & IFF_AUTO) &&
799 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
804 * Hack to prevent the initialization-time generated
805 * IPv6 multicast packet to erroneously cause a
806 * dialout event in case IPv6 has been
807 * administratively disabled on that interface.
809 if (af == AF_INET6 &&
810 !(sp->confflags & CONF_ENABLE_IPV6))
814 * Interface is not yet running, but auto-dial. Need
815 * to start LCP for it.
817 ifp->if_drv_flags |= IFF_DRV_RUNNING;
823 /* XXX Check mbuf length here? */
824 struct ip *ip = mtod (m, struct ip*);
825 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
828 * When using dynamic local IP address assignment by using
829 * 0.0.0.0 as a local address, the first TCP session will
830 * not connect because the local TCP checksum is computed
831 * using 0.0.0.0 which will later become our real IP address
832 * so the TCP checksum computed at the remote end will
833 * become invalid. So we
834 * - don't let packets with src ip addr 0 thru
835 * - we flag TCP packets with src ip 0 as an error
838 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
842 if(ip->ip_p == IPPROTO_TCP)
843 return(EADDRNOTAVAIL);
849 * Put low delay, telnet, rlogin and ftp control packets
850 * in front of the queue or let ALTQ take care.
852 if (ALTQ_IS_ENABLED(&ifp->if_snd))
854 else if (_IF_QFULL(&sp->pp_fastq))
856 else if (ip->ip_tos & IPTOS_LOWDELAY)
858 else if (m->m_len < sizeof *ip + sizeof *tcp)
860 else if (ip->ip_p != IPPROTO_TCP)
862 else if (INTERACTIVE (ntohs (tcp->th_sport)))
864 else if (INTERACTIVE (ntohs (tcp->th_dport)))
868 * Do IP Header compression
870 if (sp->pp_mode != IFF_CISCO && sp->pp_mode != PP_FR &&
871 (sp->ipcp.flags & IPCP_VJ) && ip->ip_p == IPPROTO_TCP)
872 switch (sl_compress_tcp(m, ip, sp->pp_comp,
873 sp->ipcp.compress_cid)) {
874 case TYPE_COMPRESSED_TCP:
875 ipproto = PPP_VJ_COMP;
877 case TYPE_UNCOMPRESSED_TCP:
878 ipproto = PPP_VJ_UCOMP;
892 if (af == AF_INET6) {
893 /* XXX do something tricky here? */
897 if (sp->pp_mode == PP_FR) {
898 /* Add frame relay header. */
899 m = sppp_fr_header (sp, m, af);
906 * Prepend general data packet PPP header. For now, IP only.
908 M_PREPEND (m, PPP_HEADER_LEN, M_NOWAIT);
911 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
913 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
918 * May want to check size of packet
919 * (albeit due to the implementation it's always enough)
921 h = mtod (m, struct ppp_header*);
922 if (sp->pp_mode == IFF_CISCO) {
923 h->address = CISCO_UNICAST; /* unicast address */
926 h->address = PPP_ALLSTATIONS; /* broadcast address */
927 h->control = PPP_UI; /* Unnumbered Info */
932 case AF_INET: /* Internet Protocol */
933 if (sp->pp_mode == IFF_CISCO)
934 h->protocol = htons (ETHERTYPE_IP);
937 * Don't choke with an ENETDOWN early. It's
938 * possible that we just started dialing out,
939 * so don't drop the packet immediately. If
940 * we notice that we run out of buffer space
941 * below, we will however remember that we are
942 * not ready to carry IP packets, and return
943 * ENETDOWN, as opposed to ENOBUFS.
945 h->protocol = htons(ipproto);
946 if (sp->state[IDX_IPCP] != STATE_OPENED)
952 case AF_INET6: /* Internet Protocol */
953 if (sp->pp_mode == IFF_CISCO)
954 h->protocol = htons (ETHERTYPE_IPV6);
957 * Don't choke with an ENETDOWN early. It's
958 * possible that we just started dialing out,
959 * so don't drop the packet immediately. If
960 * we notice that we run out of buffer space
961 * below, we will however remember that we are
962 * not ready to carry IP packets, and return
963 * ENETDOWN, as opposed to ENOBUFS.
965 h->protocol = htons(PPP_IPV6);
966 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
973 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
975 return (EAFNOSUPPORT);
979 * Queue message on interface, and start output if interface
984 error = !(IF_HANDOFF_ADJ(ifq, m, ifp, 3));
986 IFQ_HANDOFF_ADJ(ifp, m, 3, error);
988 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
990 return (rv? rv: ENOBUFS);
994 * Unlike in sppp_input(), we can always bump the timestamp
995 * here since sppp_output() is only called on behalf of
996 * network-layer traffic; control-layer traffic is handled
999 sp->pp_last_sent = time_uptime;
1004 sppp_attach(struct ifnet *ifp)
1006 struct sppp *sp = IFP2SP(ifp);
1008 /* Initialize mtx lock */
1009 mtx_init(&sp->mtx, "sppp", MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
1011 /* Initialize keepalive handler. */
1012 callout_init(&sp->keepalive_callout, 1);
1013 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
1016 ifp->if_mtu = PP_MTU;
1017 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
1018 ifp->if_output = sppp_output;
1020 sp->pp_flags = PP_KEEPALIVE;
1022 ifp->if_snd.ifq_maxlen = 32;
1023 sp->pp_fastq.ifq_maxlen = 32;
1024 sp->pp_cpq.ifq_maxlen = 20;
1026 sp->pp_alivecnt = 0;
1027 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
1028 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
1029 sp->pp_phase = PHASE_DEAD;
1030 sp->pp_up = sppp_pp_up;
1031 sp->pp_down = sppp_pp_down;
1032 if(!mtx_initialized(&sp->pp_cpq.ifq_mtx))
1033 mtx_init(&sp->pp_cpq.ifq_mtx, "sppp_cpq", NULL, MTX_DEF);
1034 if(!mtx_initialized(&sp->pp_fastq.ifq_mtx))
1035 mtx_init(&sp->pp_fastq.ifq_mtx, "sppp_fastq", NULL, MTX_DEF);
1036 sp->pp_last_recv = sp->pp_last_sent = time_uptime;
1039 sp->confflags |= CONF_ENABLE_VJ;
1042 sp->confflags |= CONF_ENABLE_IPV6;
1044 callout_init(&sp->ifstart_callout, 1);
1045 sp->if_start = ifp->if_start;
1046 ifp->if_start = sppp_ifstart;
1047 sp->pp_comp = malloc(sizeof(struct slcompress), M_TEMP, M_WAITOK);
1048 sl_compress_init(sp->pp_comp, -1);
1051 sppp_ipv6cp_init(sp);
1057 sppp_detach(struct ifnet *ifp)
1059 struct sppp *sp = IFP2SP(ifp);
1062 KASSERT(mtx_initialized(&sp->mtx), ("sppp mutex is not initialized"));
1064 /* Stop keepalive handler. */
1065 callout_drain(&sp->keepalive_callout);
1067 for (i = 0; i < IDX_COUNT; i++) {
1068 callout_drain(&sp->ch[i]);
1070 callout_drain(&sp->pap_my_to_ch);
1072 mtx_destroy(&sp->pp_cpq.ifq_mtx);
1073 mtx_destroy(&sp->pp_fastq.ifq_mtx);
1074 mtx_destroy(&sp->mtx);
1078 * Flush the interface output queue.
1081 sppp_flush_unlocked(struct ifnet *ifp)
1083 struct sppp *sp = IFP2SP(ifp);
1085 sppp_qflush ((struct ifqueue *)&SP2IFP(sp)->if_snd);
1086 sppp_qflush (&sp->pp_fastq);
1087 sppp_qflush (&sp->pp_cpq);
1091 sppp_flush(struct ifnet *ifp)
1093 struct sppp *sp = IFP2SP(ifp);
1096 sppp_flush_unlocked (ifp);
1101 * Check if the output queue is empty.
1104 sppp_isempty(struct ifnet *ifp)
1106 struct sppp *sp = IFP2SP(ifp);
1110 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
1111 !SP2IFP(sp)->if_snd.ifq_head;
1117 * Get next packet to send.
1120 sppp_dequeue(struct ifnet *ifp)
1122 struct sppp *sp = IFP2SP(ifp);
1127 * Process only the control protocol queue until we have at
1128 * least one NCP open.
1130 * Do always serve all three queues in Cisco mode.
1132 IF_DEQUEUE(&sp->pp_cpq, m);
1134 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO ||
1135 sp->pp_mode == PP_FR)) {
1136 IF_DEQUEUE(&sp->pp_fastq, m);
1138 IF_DEQUEUE (&SP2IFP(sp)->if_snd, m);
1145 * Pick the next packet, do not remove it from the queue.
1148 sppp_pick(struct ifnet *ifp)
1150 struct sppp *sp = IFP2SP(ifp);
1155 m = sp->pp_cpq.ifq_head;
1157 (sp->pp_phase == PHASE_NETWORK ||
1158 sp->pp_mode == IFF_CISCO ||
1159 sp->pp_mode == PP_FR))
1160 if ((m = sp->pp_fastq.ifq_head) == NULL)
1161 m = SP2IFP(sp)->if_snd.ifq_head;
1167 * Process an ioctl request. Called on low priority level.
1170 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1172 struct ifreq *ifr = (struct ifreq*) data;
1173 struct sppp *sp = IFP2SP(ifp);
1174 int rv, going_up, going_down, newmode;
1183 /* set the interface "up" when assigning an IP address */
1184 ifp->if_flags |= IFF_UP;
1188 going_up = ifp->if_flags & IFF_UP &&
1189 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0;
1190 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1191 ifp->if_drv_flags & IFF_DRV_RUNNING;
1193 newmode = ifp->if_flags & IFF_PASSIVE;
1195 newmode = ifp->if_flags & IFF_AUTO;
1197 newmode = ifp->if_flags & IFF_CISCO;
1198 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1199 ifp->if_flags |= newmode;
1202 newmode = sp->pp_flags & PP_FR;
1204 if (newmode != sp->pp_mode) {
1207 going_up = ifp->if_drv_flags & IFF_DRV_RUNNING;
1211 if (sp->pp_mode != IFF_CISCO &&
1212 sp->pp_mode != PP_FR)
1214 else if (sp->pp_tlf)
1216 sppp_flush_unlocked(ifp);
1217 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1218 sp->pp_mode = newmode;
1222 if (sp->pp_mode != IFF_CISCO &&
1223 sp->pp_mode != PP_FR)
1225 sp->pp_mode = newmode;
1226 if (sp->pp_mode == 0) {
1227 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1230 if ((sp->pp_mode == IFF_CISCO) ||
1231 (sp->pp_mode == PP_FR)) {
1234 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1242 #define ifr_mtu ifr_metric
1245 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1247 ifp->if_mtu = ifr->ifr_mtu;
1252 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1254 ifp->if_mtu = *(short*)data;
1259 ifr->ifr_mtu = ifp->if_mtu;
1264 *(short*)data = ifp->if_mtu;
1271 case SIOCGIFGENERIC:
1272 case SIOCSIFGENERIC:
1273 rv = sppp_params(sp, cmd, data);
1284 * Cisco framing implementation.
1288 * Handle incoming Cisco keepalive protocol packets.
1291 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1294 struct cisco_packet *h;
1297 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1300 SPP_FMT "cisco invalid packet length: %d bytes\n",
1301 SPP_ARGS(ifp), m->m_pkthdr.len);
1304 h = mtod (m, struct cisco_packet*);
1307 SPP_FMT "cisco input: %d bytes "
1308 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1309 SPP_ARGS(ifp), m->m_pkthdr.len,
1310 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1311 (u_int)h->time0, (u_int)h->time1);
1312 switch (ntohl (h->type)) {
1315 log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n",
1316 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1318 case CISCO_ADDR_REPLY:
1319 /* Reply on address request, ignore */
1321 case CISCO_KEEPALIVE_REQ:
1322 sp->pp_alivecnt = 0;
1323 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1324 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1325 /* Local and remote sequence numbers are equal.
1326 * Probably, the line is in loopback mode. */
1327 if (sp->pp_loopcnt >= MAXALIVECNT) {
1328 printf (SPP_FMT "loopback\n",
1331 if (ifp->if_flags & IFF_UP) {
1333 sppp_qflush (&sp->pp_cpq);
1338 /* Generate new local sequence number */
1339 sp->pp_seq[IDX_LCP] = random();
1343 if (! (ifp->if_flags & IFF_UP) &&
1344 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1346 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1349 case CISCO_ADDR_REQ:
1350 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1352 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1358 * Send Cisco keepalive packet.
1361 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1364 struct ppp_header *h;
1365 struct cisco_packet *ch;
1369 getmicrouptime(&tv);
1371 MGETHDR (m, M_NOWAIT, MT_DATA);
1374 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1375 m->m_pkthdr.rcvif = 0;
1377 h = mtod (m, struct ppp_header*);
1378 h->address = CISCO_MULTICAST;
1380 h->protocol = htons (CISCO_KEEPALIVE);
1382 ch = (struct cisco_packet*) (h + 1);
1383 ch->type = htonl (type);
1384 ch->par1 = htonl (par1);
1385 ch->par2 = htonl (par2);
1388 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1389 ch->time1 = htons ((u_short) tv.tv_sec);
1393 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1394 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1395 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1397 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1398 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1402 * PPP protocol implementation.
1406 * Send PPP control protocol packet.
1409 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1410 u_char ident, u_short len, void *data)
1413 struct ppp_header *h;
1414 struct lcp_header *lh;
1417 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1418 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1419 MGETHDR (m, M_NOWAIT, MT_DATA);
1422 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1423 m->m_pkthdr.rcvif = 0;
1425 h = mtod (m, struct ppp_header*);
1426 h->address = PPP_ALLSTATIONS; /* broadcast address */
1427 h->control = PPP_UI; /* Unnumbered Info */
1428 h->protocol = htons (proto); /* Link Control Protocol */
1430 lh = (struct lcp_header*) (h + 1);
1433 lh->len = htons (LCP_HEADER_LEN + len);
1435 bcopy (data, lh+1, len);
1438 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1440 sppp_proto_name(proto),
1441 sppp_cp_type_name (lh->type), lh->ident,
1443 sppp_print_bytes ((u_char*) (lh+1), len);
1446 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1447 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1451 * Handle incoming PPP control protocol packets.
1454 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1457 struct lcp_header *h;
1458 int len = m->m_pkthdr.len;
1465 SPP_FMT "%s invalid packet length: %d bytes\n",
1466 SPP_ARGS(ifp), cp->name, len);
1469 h = mtod (m, struct lcp_header*);
1472 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1473 SPP_ARGS(ifp), cp->name,
1474 sppp_state_name(sp->state[cp->protoidx]),
1475 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1476 sppp_print_bytes ((u_char*) (h+1), len-4);
1479 if (len > ntohs (h->len))
1480 len = ntohs (h->len);
1481 p = (u_char *)(h + 1);
1486 log(-1, SPP_FMT "%s invalid conf-req length %d\n",
1487 SPP_ARGS(ifp), cp->name,
1489 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1492 /* handle states where RCR doesn't get a SCA/SCN */
1493 switch (sp->state[cp->protoidx]) {
1495 case STATE_STOPPING:
1498 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1502 rv = (cp->RCR)(sp, h, len);
1503 switch (sp->state[cp->protoidx]) {
1508 case STATE_ACK_SENT:
1509 case STATE_REQ_SENT:
1511 * sppp_cp_change_state() have the side effect of
1512 * restarting the timeouts. We want to avoid that
1513 * if the state don't change, otherwise we won't
1514 * ever timeout and resend a configuration request
1517 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1520 sppp_cp_change_state(cp, sp, rv?
1521 STATE_ACK_SENT: STATE_REQ_SENT);
1524 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1526 sppp_cp_change_state(cp, sp, rv?
1527 STATE_ACK_SENT: STATE_REQ_SENT);
1529 case STATE_ACK_RCVD:
1531 sppp_cp_change_state(cp, sp, STATE_OPENED);
1533 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1538 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1541 printf(SPP_FMT "%s illegal %s in state %s\n",
1542 SPP_ARGS(ifp), cp->name,
1543 sppp_cp_type_name(h->type),
1544 sppp_state_name(sp->state[cp->protoidx]));
1545 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1549 if (h->ident != sp->confid[cp->protoidx]) {
1551 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1552 SPP_ARGS(ifp), cp->name,
1553 h->ident, sp->confid[cp->protoidx]);
1554 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1557 switch (sp->state[cp->protoidx]) {
1560 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1563 case STATE_STOPPING:
1565 case STATE_REQ_SENT:
1566 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1567 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1572 case STATE_ACK_RCVD:
1574 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1576 case STATE_ACK_SENT:
1577 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1578 sppp_cp_change_state(cp, sp, STATE_OPENED);
1580 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1581 SPP_ARGS(ifp), cp->name);
1585 printf(SPP_FMT "%s illegal %s in state %s\n",
1586 SPP_ARGS(ifp), cp->name,
1587 sppp_cp_type_name(h->type),
1588 sppp_state_name(sp->state[cp->protoidx]));
1589 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1594 if (h->ident != sp->confid[cp->protoidx]) {
1596 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1597 SPP_ARGS(ifp), cp->name,
1598 h->ident, sp->confid[cp->protoidx]);
1599 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1602 if (h->type == CONF_NAK)
1603 (cp->RCN_nak)(sp, h, len);
1605 (cp->RCN_rej)(sp, h, len);
1607 switch (sp->state[cp->protoidx]) {
1610 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1612 case STATE_REQ_SENT:
1613 case STATE_ACK_SENT:
1614 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1616 * Slow things down a bit if we think we might be
1617 * in loopback. Depend on the timeout to send the
1618 * next configuration request.
1627 case STATE_ACK_RCVD:
1628 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1632 case STATE_STOPPING:
1635 printf(SPP_FMT "%s illegal %s in state %s\n",
1636 SPP_ARGS(ifp), cp->name,
1637 sppp_cp_type_name(h->type),
1638 sppp_state_name(sp->state[cp->protoidx]));
1639 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1644 switch (sp->state[cp->protoidx]) {
1645 case STATE_ACK_RCVD:
1646 case STATE_ACK_SENT:
1647 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1652 case STATE_STOPPING:
1653 case STATE_REQ_SENT:
1655 /* Send Terminate-Ack packet. */
1657 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1658 SPP_ARGS(ifp), cp->name);
1659 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1663 sp->rst_counter[cp->protoidx] = 0;
1664 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1668 printf(SPP_FMT "%s illegal %s in state %s\n",
1669 SPP_ARGS(ifp), cp->name,
1670 sppp_cp_type_name(h->type),
1671 sppp_state_name(sp->state[cp->protoidx]));
1672 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1676 switch (sp->state[cp->protoidx]) {
1679 case STATE_REQ_SENT:
1680 case STATE_ACK_SENT:
1683 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1686 case STATE_STOPPING:
1687 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1690 case STATE_ACK_RCVD:
1691 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1696 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1699 printf(SPP_FMT "%s illegal %s in state %s\n",
1700 SPP_ARGS(ifp), cp->name,
1701 sppp_cp_type_name(h->type),
1702 sppp_state_name(sp->state[cp->protoidx]));
1703 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1707 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1709 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1710 "danger will robinson\n",
1711 SPP_ARGS(ifp), cp->name,
1712 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1713 switch (sp->state[cp->protoidx]) {
1716 case STATE_REQ_SENT:
1717 case STATE_ACK_SENT:
1719 case STATE_STOPPING:
1722 case STATE_ACK_RCVD:
1723 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1726 printf(SPP_FMT "%s illegal %s in state %s\n",
1727 SPP_ARGS(ifp), cp->name,
1728 sppp_cp_type_name(h->type),
1729 sppp_state_name(sp->state[cp->protoidx]));
1730 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1736 const struct cp *upper;
1742 proto = ntohs(*((u_int16_t *)p));
1743 for (i = 0; i < IDX_COUNT; i++) {
1744 if (cps[i]->proto == proto) {
1752 if (catastrophic || debug)
1753 log(catastrophic? LOG_INFO: LOG_DEBUG,
1754 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1755 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1756 sppp_cp_type_name(h->type), proto,
1757 upper ? upper->name : "unknown",
1758 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1761 * if we got RXJ+ against conf-req, the peer does not implement
1762 * this particular protocol type. terminate the protocol.
1764 if (upper && !catastrophic) {
1765 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1771 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1772 switch (sp->state[cp->protoidx]) {
1775 case STATE_REQ_SENT:
1776 case STATE_ACK_SENT:
1778 case STATE_STOPPING:
1781 case STATE_ACK_RCVD:
1782 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1785 printf(SPP_FMT "%s illegal %s in state %s\n",
1786 SPP_ARGS(ifp), cp->name,
1787 sppp_cp_type_name(h->type),
1788 sppp_state_name(sp->state[cp->protoidx]));
1789 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1794 if (cp->proto != PPP_LCP)
1796 /* Discard the packet. */
1799 if (cp->proto != PPP_LCP)
1801 if (sp->state[cp->protoidx] != STATE_OPENED) {
1803 log(-1, SPP_FMT "lcp echo req but lcp closed\n",
1805 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1810 log(-1, SPP_FMT "invalid lcp echo request "
1811 "packet length: %d bytes\n",
1812 SPP_ARGS(ifp), len);
1815 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1816 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1817 /* Line loopback mode detected. */
1818 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1819 sp->pp_loopcnt = MAXALIVECNT * 5;
1821 sppp_qflush (&sp->pp_cpq);
1823 /* Shut down the PPP link. */
1829 *(long*)(h+1) = htonl (sp->lcp.magic);
1831 log(-1, SPP_FMT "got lcp echo req, sending echo rep\n",
1833 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1836 if (cp->proto != PPP_LCP)
1838 if (h->ident != sp->lcp.echoid) {
1839 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1844 log(-1, SPP_FMT "lcp invalid echo reply "
1845 "packet length: %d bytes\n",
1846 SPP_ARGS(ifp), len);
1850 log(-1, SPP_FMT "lcp got echo rep\n",
1852 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1853 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1854 sp->pp_alivecnt = 0;
1857 /* Unknown packet type -- send Code-Reject packet. */
1860 log(-1, SPP_FMT "%s send code-rej for 0x%x\n",
1861 SPP_ARGS(ifp), cp->name, h->type);
1862 sppp_cp_send(sp, cp->proto, CODE_REJ,
1863 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1864 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1869 * The generic part of all Up/Down/Open/Close/TO event handlers.
1870 * Basically, the state transition handling in the automaton.
1873 sppp_up_event(const struct cp *cp, struct sppp *sp)
1878 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1879 SPP_ARGS(ifp), cp->name,
1880 sppp_state_name(sp->state[cp->protoidx]));
1882 switch (sp->state[cp->protoidx]) {
1884 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1886 case STATE_STARTING:
1887 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1889 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1892 printf(SPP_FMT "%s illegal up in state %s\n",
1893 SPP_ARGS(ifp), cp->name,
1894 sppp_state_name(sp->state[cp->protoidx]));
1899 sppp_down_event(const struct cp *cp, struct sppp *sp)
1904 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1905 SPP_ARGS(ifp), cp->name,
1906 sppp_state_name(sp->state[cp->protoidx]));
1908 switch (sp->state[cp->protoidx]) {
1911 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1914 sppp_cp_change_state(cp, sp, STATE_STARTING);
1917 case STATE_STOPPING:
1918 case STATE_REQ_SENT:
1919 case STATE_ACK_RCVD:
1920 case STATE_ACK_SENT:
1921 sppp_cp_change_state(cp, sp, STATE_STARTING);
1925 sppp_cp_change_state(cp, sp, STATE_STARTING);
1928 printf(SPP_FMT "%s illegal down in state %s\n",
1929 SPP_ARGS(ifp), cp->name,
1930 sppp_state_name(sp->state[cp->protoidx]));
1935 sppp_open_event(const struct cp *cp, struct sppp *sp)
1940 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1941 SPP_ARGS(ifp), cp->name,
1942 sppp_state_name(sp->state[cp->protoidx]));
1944 switch (sp->state[cp->protoidx]) {
1946 sppp_cp_change_state(cp, sp, STATE_STARTING);
1949 case STATE_STARTING:
1952 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1954 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1958 * Try escaping stopped state. This seems to bite
1959 * people occasionally, in particular for IPCP,
1960 * presumably following previous IPCP negotiation
1961 * aborts. Somehow, we must have missed a Down event
1962 * which would have caused a transition into starting
1963 * state, so as a bandaid we force the Down event now.
1964 * This effectively implements (something like the)
1965 * `restart' option mentioned in the state transition
1966 * table of RFC 1661.
1968 sppp_cp_change_state(cp, sp, STATE_STARTING);
1971 case STATE_STOPPING:
1972 case STATE_REQ_SENT:
1973 case STATE_ACK_RCVD:
1974 case STATE_ACK_SENT:
1978 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1984 sppp_close_event(const struct cp *cp, struct sppp *sp)
1989 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
1990 SPP_ARGS(ifp), cp->name,
1991 sppp_state_name(sp->state[cp->protoidx]));
1993 switch (sp->state[cp->protoidx]) {
1998 case STATE_STARTING:
1999 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2003 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2005 case STATE_STOPPING:
2006 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2011 case STATE_REQ_SENT:
2012 case STATE_ACK_RCVD:
2013 case STATE_ACK_SENT:
2014 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2015 sppp_cp_send(sp, cp->proto, TERM_REQ,
2016 ++sp->pp_seq[cp->protoidx], 0, 0);
2017 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2023 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 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2060 cp->TO, (void *)sp);
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 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2071 cp->TO, (void *)sp);
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 callout_stop (&sp->ch[cp->protoidx]);
2091 case STATE_STARTING:
2097 case STATE_STOPPING:
2098 case STATE_REQ_SENT:
2099 case STATE_ACK_RCVD:
2100 case STATE_ACK_SENT:
2101 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2102 cp->TO, (void *)sp);
2108 *--------------------------------------------------------------------------*
2110 * The LCP implementation. *
2112 *--------------------------------------------------------------------------*
2115 sppp_pp_up(struct sppp *sp)
2123 sppp_pp_down(struct sppp *sp)
2131 sppp_lcp_init(struct sppp *sp)
2133 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2135 sp->state[IDX_LCP] = STATE_INITIAL;
2136 sp->fail_counter[IDX_LCP] = 0;
2137 sp->pp_seq[IDX_LCP] = 0;
2138 sp->pp_rseq[IDX_LCP] = 0;
2140 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2142 /* Note that these values are relevant for all control protocols */
2143 sp->lcp.timeout = 3 * hz;
2144 sp->lcp.max_terminate = 2;
2145 sp->lcp.max_configure = 10;
2146 sp->lcp.max_failure = 10;
2147 callout_init(&sp->ch[IDX_LCP], 1);
2151 sppp_lcp_up(struct sppp *sp)
2155 sp->pp_alivecnt = 0;
2156 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2159 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2161 * If we are authenticator, negotiate LCP_AUTH
2163 if (sp->hisauth.proto != 0)
2164 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2166 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2167 sp->pp_flags &= ~PP_NEEDAUTH;
2169 * If this interface is passive or dial-on-demand, and we are
2170 * still in Initial state, it means we've got an incoming
2171 * call. Activate the interface.
2173 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2176 SPP_FMT "Up event", SPP_ARGS(ifp));
2177 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2178 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2180 log(-1, "(incoming call)\n");
2181 sp->pp_flags |= PP_CALLIN;
2185 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2186 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2187 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2191 sppp_up_event(&lcp, sp);
2195 sppp_lcp_down(struct sppp *sp)
2199 sppp_down_event(&lcp, sp);
2202 * If this is neither a dial-on-demand nor a passive
2203 * interface, simulate an ``ifconfig down'' action, so the
2204 * administrator can force a redial by another ``ifconfig
2205 * up''. XXX For leased line operation, should we immediately
2206 * try to reopen the connection here?
2208 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2210 SPP_FMT "Down event, taking interface down.\n",
2216 SPP_FMT "Down event (carrier loss)\n",
2218 sp->pp_flags &= ~PP_CALLIN;
2219 if (sp->state[IDX_LCP] != STATE_INITIAL)
2221 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2226 sppp_lcp_open(struct sppp *sp)
2228 sppp_open_event(&lcp, sp);
2232 sppp_lcp_close(struct sppp *sp)
2234 sppp_close_event(&lcp, sp);
2238 sppp_lcp_TO(void *cookie)
2240 sppp_to_event(&lcp, (struct sppp *)cookie);
2244 * Analyze a configure request. Return true if it was agreeable, and
2245 * caused action sca, false if it has been rejected or nak'ed, and
2246 * caused action scn. (The return value is used to make the state
2247 * transition decision in the state automaton.)
2250 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2253 u_char *buf, *r, *p;
2260 buf = r = malloc (len, M_TEMP, M_NOWAIT);
2265 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2268 /* pass 1: check for things that need to be rejected */
2270 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2271 len-=p[1], p+=p[1]) {
2273 log(-1, " %s ", sppp_lcp_opt_name(*p));
2277 if (len >= 6 && p[1] == 6)
2280 log(-1, "[invalid] ");
2282 case LCP_OPT_ASYNC_MAP:
2283 /* Async control character map. */
2284 if (len >= 6 && p[1] == 6)
2287 log(-1, "[invalid] ");
2290 /* Maximum receive unit. */
2291 if (len >= 4 && p[1] == 4)
2294 log(-1, "[invalid] ");
2296 case LCP_OPT_AUTH_PROTO:
2299 log(-1, "[invalid] ");
2302 authproto = (p[2] << 8) + p[3];
2303 if (authproto == PPP_CHAP && p[1] != 5) {
2305 log(-1, "[invalid chap len] ");
2308 if (sp->myauth.proto == 0) {
2309 /* we are not configured to do auth */
2311 log(-1, "[not configured] ");
2315 * Remote want us to authenticate, remember this,
2316 * so we stay in PHASE_AUTHENTICATE after LCP got
2319 sp->pp_flags |= PP_NEEDAUTH;
2322 /* Others not supported. */
2327 /* Add the option to rejected list. */
2334 log(-1, " send conf-rej\n");
2335 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2341 * pass 2: check for option values that are unacceptable and
2342 * thus require to be nak'ed.
2345 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2350 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2351 len-=p[1], p+=p[1]) {
2353 log(-1, " %s ", sppp_lcp_opt_name(*p));
2356 /* Magic number -- extract. */
2357 nmagic = (u_long)p[2] << 24 |
2358 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2359 if (nmagic != sp->lcp.magic) {
2362 log(-1, "0x%lx ", nmagic);
2365 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2366 log(-1, "[glitch] ");
2369 * We negate our magic here, and NAK it. If
2370 * we see it later in an NAK packet, we
2371 * suggest a new one.
2373 nmagic = ~sp->lcp.magic;
2375 p[2] = nmagic >> 24;
2376 p[3] = nmagic >> 16;
2381 case LCP_OPT_ASYNC_MAP:
2383 * Async control character map -- just ignore it.
2385 * Quote from RFC 1662, chapter 6:
2386 * To enable this functionality, synchronous PPP
2387 * implementations MUST always respond to the
2388 * Async-Control-Character-Map Configuration
2389 * Option with the LCP Configure-Ack. However,
2390 * acceptance of the Configuration Option does
2391 * not imply that the synchronous implementation
2392 * will do any ACCM mapping. Instead, all such
2393 * octet mapping will be performed by the
2394 * asynchronous-to-synchronous converter.
2400 * Maximum receive unit. Always agreeable,
2401 * but ignored by now.
2403 sp->lcp.their_mru = p[2] * 256 + p[3];
2405 log(-1, "%lu ", sp->lcp.their_mru);
2408 case LCP_OPT_AUTH_PROTO:
2409 authproto = (p[2] << 8) + p[3];
2410 if (sp->myauth.proto != authproto) {
2411 /* not agreed, nak */
2413 log(-1, "[mine %s != his %s] ",
2414 sppp_proto_name(sp->hisauth.proto),
2415 sppp_proto_name(authproto));
2416 p[2] = sp->myauth.proto >> 8;
2417 p[3] = sp->myauth.proto;
2420 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2422 log(-1, "[chap not MD5] ");
2428 /* Add the option to nak'ed list. */
2435 * Local and remote magics equal -- loopback?
2437 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2438 if (sp->pp_loopcnt == MAXALIVECNT*5)
2439 printf (SPP_FMT "loopback\n",
2441 if (ifp->if_flags & IFF_UP) {
2443 sppp_qflush(&sp->pp_cpq);
2448 } else if (!sp->pp_loopcnt &&
2449 ++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2451 log(-1, " max_failure (%d) exceeded, "
2453 sp->lcp.max_failure);
2454 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2457 log(-1, " send conf-nak\n");
2458 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2462 log(-1, " send conf-ack\n");
2463 sp->fail_counter[IDX_LCP] = 0;
2465 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2466 h->ident, origlen, h+1);
2474 * Analyze the LCP Configure-Reject option list, and adjust our
2478 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2484 buf = malloc (len, M_TEMP, M_NOWAIT);
2489 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2493 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2494 len -= p[1], p += p[1]) {
2496 log(-1, " %s ", sppp_lcp_opt_name(*p));
2499 /* Magic number -- can't use it, use 0 */
2500 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2505 * Should not be rejected anyway, since we only
2506 * negotiate a MRU if explicitly requested by
2509 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2511 case LCP_OPT_AUTH_PROTO:
2513 * Peer doesn't want to authenticate himself,
2514 * deny unless this is a dialout call, and
2515 * AUTHFLAG_NOCALLOUT is set.
2517 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2518 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2520 log(-1, "[don't insist on auth "
2522 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2526 log(-1, "[access denied]\n");
2538 * Analyze the LCP Configure-NAK option list, and adjust our
2542 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2549 buf = malloc (len, M_TEMP, M_NOWAIT);
2554 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2558 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2559 len -= p[1], p += p[1]) {
2561 log(-1, " %s ", sppp_lcp_opt_name(*p));
2564 /* Magic number -- renegotiate */
2565 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2566 len >= 6 && p[1] == 6) {
2567 magic = (u_long)p[2] << 24 |
2568 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2570 * If the remote magic is our negated one,
2571 * this looks like a loopback problem.
2572 * Suggest a new magic to make sure.
2574 if (magic == ~sp->lcp.magic) {
2576 log(-1, "magic glitch ");
2577 sp->lcp.magic = random();
2579 sp->lcp.magic = magic;
2581 log(-1, "%lu ", magic);
2587 * Peer wants to advise us to negotiate an MRU.
2588 * Agree on it if it's reasonable, or use
2589 * default otherwise.
2591 if (len >= 4 && p[1] == 4) {
2592 u_int mru = p[2] * 256 + p[3];
2594 log(-1, "%d ", mru);
2595 if (mru < PP_MTU || mru > PP_MAX_MRU)
2598 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2601 case LCP_OPT_AUTH_PROTO:
2603 * Peer doesn't like our authentication method,
2607 log(-1, "[access denied]\n");
2619 sppp_lcp_tlu(struct sppp *sp)
2626 if (! (ifp->if_flags & IFF_UP) &&
2627 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2628 /* Coming out of loopback mode. */
2630 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2633 for (i = 0; i < IDX_COUNT; i++)
2634 if ((cps[i])->flags & CP_QUAL)
2637 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2638 (sp->pp_flags & PP_NEEDAUTH) != 0)
2639 sp->pp_phase = PHASE_AUTHENTICATE;
2641 sp->pp_phase = PHASE_NETWORK;
2644 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2645 sppp_phase_name(sp->pp_phase));
2648 * Open all authentication protocols. This is even required
2649 * if we already proceeded to network phase, since it might be
2650 * that remote wants us to authenticate, so we might have to
2651 * send a PAP request. Undesired authentication protocols
2652 * don't do anything when they get an Open event.
2654 for (i = 0; i < IDX_COUNT; i++)
2655 if ((cps[i])->flags & CP_AUTH)
2658 if (sp->pp_phase == PHASE_NETWORK) {
2659 /* Notify all NCPs. */
2660 for (i = 0; i < IDX_COUNT; i++)
2661 if (((cps[i])->flags & CP_NCP) &&
2664 * Hack to administratively disable IPv6 if
2665 * not desired. Perhaps we should have another
2666 * flag for this, but right now, we can make
2667 * all struct cp's read/only.
2669 (cps[i] != &ipv6cp ||
2670 (sp->confflags & CONF_ENABLE_IPV6)))
2674 /* Send Up events to all started protos. */
2675 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2676 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2679 /* notify low-level driver of state change */
2681 sp->pp_chg(sp, (int)sp->pp_phase);
2683 if (sp->pp_phase == PHASE_NETWORK)
2684 /* if no NCP is starting, close down */
2685 sppp_lcp_check_and_close(sp);
2689 sppp_lcp_tld(struct sppp *sp)
2695 sp->pp_phase = PHASE_TERMINATE;
2698 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2699 sppp_phase_name(sp->pp_phase));
2702 * Take upper layers down. We send the Down event first and
2703 * the Close second to prevent the upper layers from sending
2704 * ``a flurry of terminate-request packets'', as the RFC
2707 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2708 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2710 (cps[i])->Close(sp);
2715 sppp_lcp_tls(struct sppp *sp)
2719 sp->pp_phase = PHASE_ESTABLISH;
2722 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2723 sppp_phase_name(sp->pp_phase));
2725 /* Notify lower layer if desired. */
2733 sppp_lcp_tlf(struct sppp *sp)
2737 sp->pp_phase = PHASE_DEAD;
2739 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2740 sppp_phase_name(sp->pp_phase));
2742 /* Notify lower layer if desired. */
2750 sppp_lcp_scr(struct sppp *sp)
2752 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2756 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2757 if (! sp->lcp.magic)
2758 sp->lcp.magic = random();
2759 opt[i++] = LCP_OPT_MAGIC;
2761 opt[i++] = sp->lcp.magic >> 24;
2762 opt[i++] = sp->lcp.magic >> 16;
2763 opt[i++] = sp->lcp.magic >> 8;
2764 opt[i++] = sp->lcp.magic;
2767 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2768 opt[i++] = LCP_OPT_MRU;
2770 opt[i++] = sp->lcp.mru >> 8;
2771 opt[i++] = sp->lcp.mru;
2774 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2775 authproto = sp->hisauth.proto;
2776 opt[i++] = LCP_OPT_AUTH_PROTO;
2777 opt[i++] = authproto == PPP_CHAP? 5: 4;
2778 opt[i++] = authproto >> 8;
2779 opt[i++] = authproto;
2780 if (authproto == PPP_CHAP)
2781 opt[i++] = CHAP_MD5;
2784 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2785 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2789 * Check the open NCPs, return true if at least one NCP is open.
2792 sppp_ncp_check(struct sppp *sp)
2796 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2797 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2803 * Re-check the open NCPs and see if we should terminate the link.
2804 * Called by the NCPs during their tlf action handling.
2807 sppp_lcp_check_and_close(struct sppp *sp)
2810 if (sp->pp_phase < PHASE_NETWORK)
2811 /* don't bother, we are already going down */
2814 if (sppp_ncp_check(sp))
2821 *--------------------------------------------------------------------------*
2823 * The IPCP implementation. *
2825 *--------------------------------------------------------------------------*
2830 sppp_ipcp_init(struct sppp *sp)
2834 sp->state[IDX_IPCP] = STATE_INITIAL;
2835 sp->fail_counter[IDX_IPCP] = 0;
2836 sp->pp_seq[IDX_IPCP] = 0;
2837 sp->pp_rseq[IDX_IPCP] = 0;
2838 callout_init(&sp->ch[IDX_IPCP], 1);
2842 sppp_ipcp_up(struct sppp *sp)
2844 sppp_up_event(&ipcp, sp);
2848 sppp_ipcp_down(struct sppp *sp)
2850 sppp_down_event(&ipcp, sp);
2854 sppp_ipcp_open(struct sppp *sp)
2857 u_long myaddr, hisaddr;
2859 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2860 IPCP_MYADDR_DYN | IPCP_VJ);
2863 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2865 * If we don't have his address, this probably means our
2866 * interface doesn't want to talk IP at all. (This could
2867 * be the case if somebody wants to speak only IPX, for
2868 * example.) Don't open IPCP in this case.
2870 if (hisaddr == 0L) {
2871 /* XXX this message should go away */
2873 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2879 * I don't have an assigned address, so i need to
2880 * negotiate my address.
2882 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2883 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2885 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2886 if (sp->confflags & CONF_ENABLE_VJ) {
2887 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2888 sp->ipcp.max_state = MAX_STATES - 1;
2889 sp->ipcp.compress_cid = 1;
2891 sppp_open_event(&ipcp, sp);
2895 sppp_ipcp_close(struct sppp *sp)
2897 sppp_close_event(&ipcp, sp);
2898 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2900 * My address was dynamic, clear it again.
2902 sppp_set_ip_addr(sp, 0L);
2906 sppp_ipcp_TO(void *cookie)
2908 sppp_to_event(&ipcp, (struct sppp *)cookie);
2912 * Analyze a configure request. Return true if it was agreeable, and
2913 * caused action sca, false if it has been rejected or nak'ed, and
2914 * caused action scn. (The return value is used to make the state
2915 * transition decision in the state automaton.)
2918 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2920 u_char *buf, *r, *p;
2921 struct ifnet *ifp = SP2IFP(sp);
2922 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2923 u_long hisaddr, desiredaddr;
2930 * Make sure to allocate a buf that can at least hold a
2931 * conf-nak with an `address' option. We might need it below.
2933 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2937 /* pass 1: see if we can recognize them */
2939 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2942 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2943 len-=p[1], p+=p[1]) {
2945 log(-1, " %s ", sppp_ipcp_opt_name(*p));
2947 case IPCP_OPT_COMPRESSION:
2948 if (!(sp->confflags & CONF_ENABLE_VJ)) {
2949 /* VJ compression administratively disabled */
2951 log(-1, "[locally disabled] ");
2955 * In theory, we should only conf-rej an
2956 * option that is shorter than RFC 1618
2957 * requires (i.e. < 4), and should conf-nak
2958 * anything else that is not VJ. However,
2959 * since our algorithm always uses the
2960 * original option to NAK it with new values,
2961 * things would become more complicated. In
2962 * practice, the only commonly implemented IP
2963 * compression option is VJ anyway, so the
2964 * difference is negligible.
2966 if (len >= 6 && p[1] == 6) {
2968 * correctly formed compression option
2969 * that could be VJ compression
2975 "optlen %d [invalid/unsupported] ",
2978 case IPCP_OPT_ADDRESS:
2979 if (len >= 6 && p[1] == 6) {
2980 /* correctly formed address option */
2984 log(-1, "[invalid] ");
2987 /* Others not supported. */
2992 /* Add the option to rejected list. */
2999 log(-1, " send conf-rej\n");
3000 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
3005 /* pass 2: parse option values */
3006 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
3008 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
3012 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3013 len-=p[1], p+=p[1]) {
3015 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3017 case IPCP_OPT_COMPRESSION:
3018 desiredcomp = p[2] << 8 | p[3];
3019 /* We only support VJ */
3020 if (desiredcomp == IPCP_COMP_VJ) {
3022 log(-1, "VJ [ack] ");
3023 sp->ipcp.flags |= IPCP_VJ;
3024 sl_compress_init(sp->pp_comp, p[4]);
3025 sp->ipcp.max_state = p[4];
3026 sp->ipcp.compress_cid = p[5];
3031 "compproto %#04x [not supported] ",
3033 p[2] = IPCP_COMP_VJ >> 8;
3034 p[3] = IPCP_COMP_VJ;
3035 p[4] = sp->ipcp.max_state;
3036 p[5] = sp->ipcp.compress_cid;
3038 case IPCP_OPT_ADDRESS:
3039 /* This is the address he wants in his end */
3040 desiredaddr = p[2] << 24 | p[3] << 16 |
3042 if (desiredaddr == hisaddr ||
3043 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3045 * Peer's address is same as our value,
3046 * or we have set it to 0.0.0.* to
3047 * indicate that we do not really care,
3048 * this is agreeable. Gonna conf-ack
3052 log(-1, "%s [ack] ",
3053 sppp_dotted_quad(hisaddr));
3054 /* record that we've seen it already */
3055 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3059 * The address wasn't agreeable. This is either
3060 * he sent us 0.0.0.0, asking to assign him an
3061 * address, or he send us another address not
3062 * matching our value. Either case, we gonna
3063 * conf-nak it with our value.
3064 * XXX: we should "rej" if hisaddr == 0
3067 if (desiredaddr == 0)
3068 log(-1, "[addr requested] ");
3070 log(-1, "%s [not agreed] ",
3071 sppp_dotted_quad(desiredaddr));
3073 p[2] = hisaddr >> 24;
3074 p[3] = hisaddr >> 16;
3075 p[4] = hisaddr >> 8;
3079 /* Add the option to nak'ed list. */
3086 * If we are about to conf-ack the request, but haven't seen
3087 * his address so far, gonna conf-nak it instead, with the
3088 * `address' option present and our idea of his address being
3089 * filled in there, to request negotiation of both addresses.
3091 * XXX This can result in an endless req - nak loop if peer
3092 * doesn't want to send us his address. Q: What should we do
3093 * about it? XXX A: implement the max-failure counter.
3095 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3096 buf[0] = IPCP_OPT_ADDRESS;
3098 buf[2] = hisaddr >> 24;
3099 buf[3] = hisaddr >> 16;
3100 buf[4] = hisaddr >> 8;
3104 log(-1, "still need hisaddr ");
3109 log(-1, " send conf-nak\n");
3110 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3113 log(-1, " send conf-ack\n");
3114 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3115 h->ident, origlen, h+1);
3123 * Analyze the IPCP Configure-Reject option list, and adjust our
3127 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3130 struct ifnet *ifp = SP2IFP(sp);
3131 int debug = ifp->if_flags & IFF_DEBUG;
3134 buf = malloc (len, M_TEMP, M_NOWAIT);
3139 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3143 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3144 len -= p[1], p += p[1]) {
3146 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3148 case IPCP_OPT_COMPRESSION:
3149 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3151 case IPCP_OPT_ADDRESS:
3153 * Peer doesn't grok address option. This is
3154 * bad. XXX Should we better give up here?
3155 * XXX We could try old "addresses" option...
3157 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3168 * Analyze the IPCP Configure-NAK option list, and adjust our
3172 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3175 struct ifnet *ifp = SP2IFP(sp);
3176 int debug = ifp->if_flags & IFF_DEBUG;
3181 buf = malloc (len, M_TEMP, M_NOWAIT);
3186 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3190 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3191 len -= p[1], p += p[1]) {
3193 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3195 case IPCP_OPT_COMPRESSION:
3196 if (len >= 6 && p[1] == 6) {
3197 desiredcomp = p[2] << 8 | p[3];
3199 log(-1, "[wantcomp %#04x] ",
3201 if (desiredcomp == IPCP_COMP_VJ) {
3202 sl_compress_init(sp->pp_comp, p[4]);
3203 sp->ipcp.max_state = p[4];
3204 sp->ipcp.compress_cid = p[5];
3206 log(-1, "[agree] ");
3209 ~(1 << IPCP_OPT_COMPRESSION);
3212 case IPCP_OPT_ADDRESS:
3214 * Peer doesn't like our local IP address. See
3215 * if we can do something for him. We'll drop
3216 * him our address then.
3218 if (len >= 6 && p[1] == 6) {
3219 wantaddr = p[2] << 24 | p[3] << 16 |
3221 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3223 log(-1, "[wantaddr %s] ",
3224 sppp_dotted_quad(wantaddr));
3226 * When doing dynamic address assignment,
3227 * we accept his offer. Otherwise, we
3228 * ignore it and thus continue to negotiate
3229 * our already existing value.
3230 * XXX: Bogus, if he said no once, he'll
3231 * just say no again, might as well die.
3233 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3234 sppp_set_ip_addr(sp, wantaddr);
3236 log(-1, "[agree] ");
3237 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3250 sppp_ipcp_tlu(struct sppp *sp)
3252 /* we are up - notify isdn daemon */
3258 sppp_ipcp_tld(struct sppp *sp)
3263 sppp_ipcp_tls(struct sppp *sp)
3265 /* indicate to LCP that it must stay alive */
3266 sp->lcp.protos |= (1 << IDX_IPCP);
3270 sppp_ipcp_tlf(struct sppp *sp)
3272 /* we no longer need LCP */
3273 sp->lcp.protos &= ~(1 << IDX_IPCP);
3274 sppp_lcp_check_and_close(sp);
3278 sppp_ipcp_scr(struct sppp *sp)
3280 char opt[6 /* compression */ + 6 /* address */];
3284 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3285 opt[i++] = IPCP_OPT_COMPRESSION;
3287 opt[i++] = IPCP_COMP_VJ >> 8;
3288 opt[i++] = IPCP_COMP_VJ;
3289 opt[i++] = sp->ipcp.max_state;
3290 opt[i++] = sp->ipcp.compress_cid;
3292 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3293 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3294 opt[i++] = IPCP_OPT_ADDRESS;
3296 opt[i++] = ouraddr >> 24;
3297 opt[i++] = ouraddr >> 16;
3298 opt[i++] = ouraddr >> 8;
3302 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3303 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3307 sppp_ipcp_init(struct sppp *sp)
3312 sppp_ipcp_up(struct sppp *sp)
3317 sppp_ipcp_down(struct sppp *sp)
3322 sppp_ipcp_open(struct sppp *sp)
3327 sppp_ipcp_close(struct sppp *sp)
3332 sppp_ipcp_TO(void *cookie)
3337 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3343 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3348 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3353 sppp_ipcp_tlu(struct sppp *sp)
3358 sppp_ipcp_tld(struct sppp *sp)
3363 sppp_ipcp_tls(struct sppp *sp)
3368 sppp_ipcp_tlf(struct sppp *sp)
3373 sppp_ipcp_scr(struct sppp *sp)
3379 *--------------------------------------------------------------------------*
3381 * The IPv6CP implementation. *
3383 *--------------------------------------------------------------------------*
3388 sppp_ipv6cp_init(struct sppp *sp)
3390 sp->ipv6cp.opts = 0;
3391 sp->ipv6cp.flags = 0;
3392 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3393 sp->fail_counter[IDX_IPV6CP] = 0;
3394 sp->pp_seq[IDX_IPV6CP] = 0;
3395 sp->pp_rseq[IDX_IPV6CP] = 0;
3396 callout_init(&sp->ch[IDX_IPV6CP], 1);
3400 sppp_ipv6cp_up(struct sppp *sp)
3402 sppp_up_event(&ipv6cp, sp);
3406 sppp_ipv6cp_down(struct sppp *sp)
3408 sppp_down_event(&ipv6cp, sp);
3412 sppp_ipv6cp_open(struct sppp *sp)
3415 struct in6_addr myaddr, hisaddr;
3417 #ifdef IPV6CP_MYIFID_DYN
3418 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3420 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3423 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3425 * If we don't have our address, this probably means our
3426 * interface doesn't want to talk IPv6 at all. (This could
3427 * be the case if somebody wants to speak only IPX, for
3428 * example.) Don't open IPv6CP in this case.
3430 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3431 /* XXX this message should go away */
3433 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3438 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3439 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3440 sppp_open_event(&ipv6cp, sp);
3444 sppp_ipv6cp_close(struct sppp *sp)
3446 sppp_close_event(&ipv6cp, sp);
3450 sppp_ipv6cp_TO(void *cookie)
3452 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3456 * Analyze a configure request. Return true if it was agreeable, and
3457 * caused action sca, false if it has been rejected or nak'ed, and
3458 * caused action scn. (The return value is used to make the state
3459 * transition decision in the state automaton.)
3462 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3464 u_char *buf, *r, *p;
3465 struct ifnet *ifp = SP2IFP(sp);
3466 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3467 struct in6_addr myaddr, desiredaddr, suggestaddr;
3470 int collision, nohisaddr;
3471 char ip6buf[INET6_ADDRSTRLEN];
3476 * Make sure to allocate a buf that can at least hold a
3477 * conf-nak with an `address' option. We might need it below.
3479 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3483 /* pass 1: see if we can recognize them */
3485 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3489 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3490 len-=p[1], p+=p[1]) {
3492 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3494 case IPV6CP_OPT_IFID:
3495 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3496 /* correctly formed address option */
3501 log(-1, " [invalid]");
3504 case IPV6CP_OPT_COMPRESSION:
3505 if (len >= 4 && p[1] >= 4) {
3506 /* correctly formed compress option */
3510 log(-1, " [invalid]");
3514 /* Others not supported. */
3519 /* Add the option to rejected list. */
3526 log(-1, " send conf-rej\n");
3527 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3532 /* pass 2: parse option values */
3533 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3535 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3540 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3541 len-=p[1], p+=p[1]) {
3543 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3546 case IPV6CP_OPT_COMPRESSION:
3549 case IPV6CP_OPT_IFID:
3550 bzero(&desiredaddr, sizeof(desiredaddr));
3551 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3552 collision = (bcmp(&desiredaddr.s6_addr[8],
3553 &myaddr.s6_addr[8], 8) == 0);
3554 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3556 desiredaddr.s6_addr16[0] = htons(0xfe80);
3557 (void)in6_setscope(&desiredaddr, SP2IFP(sp), NULL);
3559 if (!collision && !nohisaddr) {
3560 /* no collision, hisaddr known - Conf-Ack */
3565 ip6_sprintf(ip6buf, &desiredaddr),
3566 sppp_cp_type_name(type));
3571 bzero(&suggestaddr, sizeof(suggestaddr));
3572 if (collision && nohisaddr) {
3573 /* collision, hisaddr unknown - Conf-Rej */
3578 * - no collision, hisaddr unknown, or
3579 * - collision, hisaddr known
3580 * Conf-Nak, suggest hisaddr
3583 sppp_suggest_ip6_addr(sp, &suggestaddr);
3584 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3588 ip6_sprintf(ip6buf, &desiredaddr),
3589 sppp_cp_type_name(type));
3592 /* Add the option to nak'ed list. */
3598 if (rlen == 0 && type == CONF_ACK) {
3600 log(-1, " send %s\n", sppp_cp_type_name(type));
3601 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3604 if (type == CONF_ACK)
3605 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3609 log(-1, " send %s suggest %s\n",
3610 sppp_cp_type_name(type),
3611 ip6_sprintf(ip6buf, &suggestaddr));
3613 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3622 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3626 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3629 struct ifnet *ifp = SP2IFP(sp);
3630 int debug = ifp->if_flags & IFF_DEBUG;
3633 buf = malloc (len, M_TEMP, M_NOWAIT);
3638 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3642 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3643 len -= p[1], p += p[1]) {
3645 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3647 case IPV6CP_OPT_IFID:
3649 * Peer doesn't grok address option. This is
3650 * bad. XXX Should we better give up here?
3652 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3655 case IPV6CP_OPT_COMPRESS:
3656 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3668 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3672 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3675 struct ifnet *ifp = SP2IFP(sp);
3676 int debug = ifp->if_flags & IFF_DEBUG;
3677 struct in6_addr suggestaddr;
3678 char ip6buf[INET6_ADDRSTRLEN];
3681 buf = malloc (len, M_TEMP, M_NOWAIT);
3686 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3690 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3691 len -= p[1], p += p[1]) {
3693 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3695 case IPV6CP_OPT_IFID:
3697 * Peer doesn't like our local ifid. See
3698 * if we can do something for him. We'll drop
3699 * him our address then.
3701 if (len < 10 || p[1] != 10)
3703 bzero(&suggestaddr, sizeof(suggestaddr));
3704 suggestaddr.s6_addr16[0] = htons(0xfe80);
3705 (void)in6_setscope(&suggestaddr, SP2IFP(sp), NULL);
3706 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3708 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3710 log(-1, " [suggestaddr %s]",
3711 ip6_sprintf(ip6buf, &suggestaddr));
3712 #ifdef IPV6CP_MYIFID_DYN
3714 * When doing dynamic address assignment,
3715 * we accept his offer.
3717 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3718 struct in6_addr lastsuggest;
3720 * If <suggested myaddr from peer> equals to
3721 * <hisaddr we have suggested last time>,
3722 * we have a collision. generate new random
3725 sppp_suggest_ip6_addr(&lastsuggest);
3726 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3729 log(-1, " [random]");
3730 sppp_gen_ip6_addr(sp, &suggestaddr);
3732 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3734 log(-1, " [agree]");
3735 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3739 * Since we do not do dynamic address assignment,
3740 * we ignore it and thus continue to negotiate
3741 * our already existing value. This can possibly
3742 * go into infinite request-reject loop.
3744 * This is not likely because we normally use
3745 * ifid based on MAC-address.
3746 * If you have no ethernet card on the node, too bad.
3747 * XXX should we use fail_counter?
3752 case IPV6CP_OPT_COMPRESS:
3754 * Peer wants different compression parameters.
3766 sppp_ipv6cp_tlu(struct sppp *sp)
3768 /* we are up - notify isdn daemon */
3774 sppp_ipv6cp_tld(struct sppp *sp)
3779 sppp_ipv6cp_tls(struct sppp *sp)
3781 /* indicate to LCP that it must stay alive */
3782 sp->lcp.protos |= (1 << IDX_IPV6CP);
3786 sppp_ipv6cp_tlf(struct sppp *sp)
3789 #if 0 /* need #if 0 to close IPv6CP properly */
3790 /* we no longer need LCP */
3791 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3792 sppp_lcp_check_and_close(sp);
3797 sppp_ipv6cp_scr(struct sppp *sp)
3799 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3800 struct in6_addr ouraddr;
3803 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3804 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3805 opt[i++] = IPV6CP_OPT_IFID;
3807 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3812 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3813 opt[i++] = IPV6CP_OPT_COMPRESSION;
3815 opt[i++] = 0; /* TBD */
3816 opt[i++] = 0; /* TBD */
3817 /* variable length data may follow */
3821 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3822 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3825 static void sppp_ipv6cp_init(struct sppp *sp)
3829 static void sppp_ipv6cp_up(struct sppp *sp)
3833 static void sppp_ipv6cp_down(struct sppp *sp)
3837 static void sppp_ipv6cp_open(struct sppp *sp)
3841 static void sppp_ipv6cp_close(struct sppp *sp)
3845 static void sppp_ipv6cp_TO(void *sp)
3849 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3854 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3858 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3862 static void sppp_ipv6cp_tlu(struct sppp *sp)
3866 static void sppp_ipv6cp_tld(struct sppp *sp)
3870 static void sppp_ipv6cp_tls(struct sppp *sp)
3874 static void sppp_ipv6cp_tlf(struct sppp *sp)
3878 static void sppp_ipv6cp_scr(struct sppp *sp)
3884 *--------------------------------------------------------------------------*
3886 * The CHAP implementation. *
3888 *--------------------------------------------------------------------------*
3892 * The authentication protocols don't employ a full-fledged state machine as
3893 * the control protocols do, since they do have Open and Close events, but
3894 * not Up and Down, nor are they explicitly terminated. Also, use of the
3895 * authentication protocols may be different in both directions (this makes
3896 * sense, think of a machine that never accepts incoming calls but only
3897 * calls out, it doesn't require the called party to authenticate itself).
3899 * Our state machine for the local authentication protocol (we are requesting
3900 * the peer to authenticate) looks like:
3903 * +--------------------------------------------+
3905 * +--------+ Close +---------+ RCA+
3906 * | |<----------------------------------| |------+
3907 * +--->| Closed | TO* | Opened | sca |
3908 * | | |-----+ +-------| |<-----+
3909 * | +--------+ irc | | +---------+
3915 * | | +------->+ | |
3917 * | +--------+ V | |
3918 * | | |<----+<--------------------+ |
3924 * +------+ +------------------------------------------+
3925 * scn,tld sca,irc,ict,tlu
3930 * Open: LCP reached authentication phase
3931 * Close: LCP reached terminate phase
3933 * RCA+: received reply (pap-req, chap-response), acceptable
3934 * RCN: received reply (pap-req, chap-response), not acceptable
3935 * TO+: timeout with restart counter >= 0
3936 * TO-: timeout with restart counter < 0
3937 * TO*: reschedule timeout for CHAP
3939 * scr: send request packet (none for PAP, chap-challenge)
3940 * sca: send ack packet (pap-ack, chap-success)
3941 * scn: send nak packet (pap-nak, chap-failure)
3942 * ict: initialize re-challenge timer (CHAP only)
3944 * tlu: this-layer-up, LCP reaches network phase
3945 * tld: this-layer-down, LCP enters terminate phase
3947 * Note that in CHAP mode, after sending a new challenge, while the state
3948 * automaton falls back into Req-Sent state, it doesn't signal a tld
3949 * event to LCP, so LCP remains in network phase. Only after not getting
3950 * any response (or after getting an unacceptable response), CHAP closes,
3951 * causing LCP to enter terminate phase.
3953 * With PAP, there is no initial request that can be sent. The peer is
3954 * expected to send one based on the successful negotiation of PAP as
3955 * the authentication protocol during the LCP option negotiation.
3957 * Incoming authentication protocol requests (remote requests
3958 * authentication, we are peer) don't employ a state machine at all,
3959 * they are simply answered. Some peers [Ascend P50 firmware rev
3960 * 4.50] react allergically when sending IPCP requests while they are
3961 * still in authentication phase (thereby violating the standard that
3962 * demands that these NCP packets are to be discarded), so we keep
3963 * track of the peer demanding us to authenticate, and only proceed to
3964 * phase network once we've seen a positive acknowledge for the
3969 * Handle incoming CHAP packets.
3972 sppp_chap_input(struct sppp *sp, struct mbuf *m)
3975 struct lcp_header *h;
3977 u_char *value, *name, digest[AUTHKEYLEN], dsize;
3978 int value_len, name_len;
3981 len = m->m_pkthdr.len;
3985 SPP_FMT "chap invalid packet length: %d bytes\n",
3986 SPP_ARGS(ifp), len);
3989 h = mtod (m, struct lcp_header*);
3990 if (len > ntohs (h->len))
3991 len = ntohs (h->len);
3994 /* challenge, failure and success are his authproto */
3995 case CHAP_CHALLENGE:
3996 value = 1 + (u_char*)(h+1);
3997 value_len = value[-1];
3998 name = value + value_len;
3999 name_len = len - value_len - 5;
4003 SPP_FMT "chap corrupted challenge "
4004 "<%s id=0x%x len=%d",
4006 sppp_auth_type_name(PPP_CHAP, h->type),
4007 h->ident, ntohs(h->len));
4008 sppp_print_bytes((u_char*) (h+1), len-4);
4016 SPP_FMT "chap input <%s id=0x%x len=%d name=",
4018 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
4020 sppp_print_string((char*) name, name_len);
4021 log(-1, " value-size=%d value=", value_len);
4022 sppp_print_bytes(value, value_len);
4026 /* Compute reply value. */
4028 MD5Update(&ctx, &h->ident, 1);
4029 MD5Update(&ctx, sp->myauth.secret,
4030 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
4031 MD5Update(&ctx, value, value_len);
4032 MD5Final(digest, &ctx);
4033 dsize = sizeof digest;
4035 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
4036 sizeof dsize, (const char *)&dsize,
4037 sizeof digest, digest,
4038 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4045 log(LOG_DEBUG, SPP_FMT "chap success",
4049 sppp_print_string((char*)(h + 1), len - 4);
4054 sp->pp_flags &= ~PP_NEEDAUTH;
4055 if (sp->myauth.proto == PPP_CHAP &&
4056 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4057 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
4059 * We are authenticator for CHAP but didn't
4060 * complete yet. Leave it to tlu to proceed
4067 sppp_phase_network(sp);
4072 log(LOG_INFO, SPP_FMT "chap failure",
4076 sppp_print_string((char*)(h + 1), len - 4);
4080 log(LOG_INFO, SPP_FMT "chap failure\n",
4082 /* await LCP shutdown by authenticator */
4085 /* response is my authproto */
4087 value = 1 + (u_char*)(h+1);
4088 value_len = value[-1];
4089 name = value + value_len;
4090 name_len = len - value_len - 5;
4094 SPP_FMT "chap corrupted response "
4095 "<%s id=0x%x len=%d",
4097 sppp_auth_type_name(PPP_CHAP, h->type),
4098 h->ident, ntohs(h->len));
4099 sppp_print_bytes((u_char*)(h+1), len-4);
4104 if (h->ident != sp->confid[IDX_CHAP]) {
4107 SPP_FMT "chap dropping response for old ID "
4108 "(got %d, expected %d)\n",
4110 h->ident, sp->confid[IDX_CHAP]);
4113 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4114 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4115 log(LOG_INFO, SPP_FMT "chap response, his name ",
4117 sppp_print_string(name, name_len);
4118 log(-1, " != expected ");
4119 sppp_print_string(sp->hisauth.name,
4120 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4124 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4125 "<%s id=0x%x len=%d name=",
4127 sppp_state_name(sp->state[IDX_CHAP]),
4128 sppp_auth_type_name(PPP_CHAP, h->type),
4129 h->ident, ntohs (h->len));
4130 sppp_print_string((char*)name, name_len);
4131 log(-1, " value-size=%d value=", value_len);
4132 sppp_print_bytes(value, value_len);
4135 if (value_len != AUTHKEYLEN) {
4138 SPP_FMT "chap bad hash value length: "
4139 "%d bytes, should be %d\n",
4140 SPP_ARGS(ifp), value_len,
4146 MD5Update(&ctx, &h->ident, 1);
4147 MD5Update(&ctx, sp->hisauth.secret,
4148 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4149 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4150 MD5Final(digest, &ctx);
4152 #define FAILMSG "Failed..."
4153 #define SUCCMSG "Welcome!"
4155 if (value_len != sizeof digest ||
4156 bcmp(digest, value, value_len) != 0) {
4157 /* action scn, tld */
4158 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4159 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4164 /* action sca, perhaps tlu */
4165 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4166 sp->state[IDX_CHAP] == STATE_OPENED)
4167 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4168 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4170 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4171 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4177 /* Unknown CHAP packet type -- ignore. */
4179 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4180 "<0x%x id=0x%xh len=%d",
4182 sppp_state_name(sp->state[IDX_CHAP]),
4183 h->type, h->ident, ntohs(h->len));
4184 sppp_print_bytes((u_char*)(h+1), len-4);
4192 sppp_chap_init(struct sppp *sp)
4194 /* Chap doesn't have STATE_INITIAL at all. */
4195 sp->state[IDX_CHAP] = STATE_CLOSED;
4196 sp->fail_counter[IDX_CHAP] = 0;
4197 sp->pp_seq[IDX_CHAP] = 0;
4198 sp->pp_rseq[IDX_CHAP] = 0;
4199 callout_init(&sp->ch[IDX_CHAP], 1);
4203 sppp_chap_open(struct sppp *sp)
4205 if (sp->myauth.proto == PPP_CHAP &&
4206 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4207 /* we are authenticator for CHAP, start it */
4209 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4210 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4212 /* nothing to be done if we are peer, await a challenge */
4216 sppp_chap_close(struct sppp *sp)
4218 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4219 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4223 sppp_chap_TO(void *cookie)
4225 struct sppp *sp = (struct sppp *)cookie;
4230 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4232 sppp_state_name(sp->state[IDX_CHAP]),
4233 sp->rst_counter[IDX_CHAP]);
4235 if (--sp->rst_counter[IDX_CHAP] < 0)
4237 switch (sp->state[IDX_CHAP]) {
4238 case STATE_REQ_SENT:
4240 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4244 /* TO+ (or TO*) event */
4245 switch (sp->state[IDX_CHAP]) {
4248 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4250 case STATE_REQ_SENT:
4252 /* sppp_cp_change_state() will restart the timer */
4253 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4261 sppp_chap_tlu(struct sppp *sp)
4267 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4270 * Some broken CHAP implementations (Conware CoNet, firmware
4271 * 4.0.?) don't want to re-authenticate their CHAP once the
4272 * initial challenge-response exchange has taken place.
4273 * Provide for an option to avoid rechallenges.
4275 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4277 * Compute the re-challenge timeout. This will yield
4278 * a number between 300 and 810 seconds.
4280 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
4281 callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, (void *)sp);
4286 SPP_FMT "chap %s, ",
4288 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4289 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4290 log(-1, "next re-challenge in %d seconds\n", i);
4292 log(-1, "re-challenging suppressed\n");
4296 /* indicate to LCP that we need to be closed down */
4297 sp->lcp.protos |= (1 << IDX_CHAP);
4299 if (sp->pp_flags & PP_NEEDAUTH) {
4301 * Remote is authenticator, but his auth proto didn't
4302 * complete yet. Defer the transition to network
4311 * If we are already in phase network, we are done here. This
4312 * is the case if this is a dummy tlu event after a re-challenge.
4314 if (sp->pp_phase != PHASE_NETWORK)
4315 sppp_phase_network(sp);
4319 sppp_chap_tld(struct sppp *sp)
4324 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4325 callout_stop(&sp->ch[IDX_CHAP]);
4326 sp->lcp.protos &= ~(1 << IDX_CHAP);
4332 sppp_chap_scr(struct sppp *sp)
4337 /* Compute random challenge. */
4338 ch = (u_long *)sp->myauth.challenge;
4339 arc4random_buf(ch, 4 * sizeof(*ch));
4342 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4344 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4345 sizeof clen, (const char *)&clen,
4346 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4347 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4353 *--------------------------------------------------------------------------*
4355 * The PAP implementation. *
4357 *--------------------------------------------------------------------------*
4360 * For PAP, we need to keep a little state also if we are the peer, not the
4361 * authenticator. This is since we don't get a request to authenticate, but
4362 * have to repeatedly authenticate ourself until we got a response (or the
4363 * retry counter is expired).
4367 * Handle incoming PAP packets. */
4369 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4372 struct lcp_header *h;
4374 u_char *name, *passwd, mlen;
4375 int name_len, passwd_len;
4377 len = m->m_pkthdr.len;
4381 SPP_FMT "pap invalid packet length: %d bytes\n",
4382 SPP_ARGS(ifp), len);
4385 h = mtod (m, struct lcp_header*);
4386 if (len > ntohs (h->len))
4387 len = ntohs (h->len);
4389 /* PAP request is my authproto */
4391 name = 1 + (u_char*)(h+1);
4392 name_len = name[-1];
4393 passwd = name + name_len + 1;
4394 if (name_len > len - 6 ||
4395 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4397 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4398 "<%s id=0x%x len=%d",
4400 sppp_auth_type_name(PPP_PAP, h->type),
4401 h->ident, ntohs(h->len));
4402 sppp_print_bytes((u_char*)(h+1), len-4);
4408 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4409 "<%s id=0x%x len=%d name=",
4411 sppp_state_name(sp->state[IDX_PAP]),
4412 sppp_auth_type_name(PPP_PAP, h->type),
4413 h->ident, ntohs(h->len));
4414 sppp_print_string((char*)name, name_len);
4415 log(-1, " passwd=");
4416 sppp_print_string((char*)passwd, passwd_len);
4419 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4420 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4421 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4422 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4423 /* action scn, tld */
4424 mlen = sizeof(FAILMSG) - 1;
4425 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4426 sizeof mlen, (const char *)&mlen,
4427 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4432 /* action sca, perhaps tlu */
4433 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4434 sp->state[IDX_PAP] == STATE_OPENED) {
4435 mlen = sizeof(SUCCMSG) - 1;
4436 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4437 sizeof mlen, (const char *)&mlen,
4438 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4441 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4442 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4447 /* ack and nak are his authproto */
4449 callout_stop(&sp->pap_my_to_ch);
4451 log(LOG_DEBUG, SPP_FMT "pap success",
4453 name_len = *((char *)h);
4454 if (len > 5 && name_len) {
4456 sppp_print_string((char*)(h+1), name_len);
4461 sp->pp_flags &= ~PP_NEEDAUTH;
4462 if (sp->myauth.proto == PPP_PAP &&
4463 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4464 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4466 * We are authenticator for PAP but didn't
4467 * complete yet. Leave it to tlu to proceed
4474 sppp_phase_network(sp);
4478 callout_stop (&sp->pap_my_to_ch);
4480 log(LOG_INFO, SPP_FMT "pap failure",
4482 name_len = *((char *)h);
4483 if (len > 5 && name_len) {
4485 sppp_print_string((char*)(h+1), name_len);
4489 log(LOG_INFO, SPP_FMT "pap failure\n",
4491 /* await LCP shutdown by authenticator */
4495 /* Unknown PAP packet type -- ignore. */
4497 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4498 "<0x%x id=0x%x len=%d",
4500 h->type, h->ident, ntohs(h->len));
4501 sppp_print_bytes((u_char*)(h+1), len-4);
4509 sppp_pap_init(struct sppp *sp)
4511 /* PAP doesn't have STATE_INITIAL at all. */
4512 sp->state[IDX_PAP] = STATE_CLOSED;
4513 sp->fail_counter[IDX_PAP] = 0;
4514 sp->pp_seq[IDX_PAP] = 0;
4515 sp->pp_rseq[IDX_PAP] = 0;
4516 callout_init(&sp->ch[IDX_PAP], 1);
4517 callout_init(&sp->pap_my_to_ch, 1);
4521 sppp_pap_open(struct sppp *sp)
4523 if (sp->hisauth.proto == PPP_PAP &&
4524 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4525 /* we are authenticator for PAP, start our timer */
4526 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4527 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4529 if (sp->myauth.proto == PPP_PAP) {
4530 /* we are peer, send a request, and start a timer */
4532 callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
4533 sppp_pap_my_TO, (void *)sp);
4538 sppp_pap_close(struct sppp *sp)
4540 if (sp->state[IDX_PAP] != STATE_CLOSED)
4541 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4545 * That's the timeout routine if we are authenticator. Since the
4546 * authenticator is basically passive in PAP, we can't do much here.
4549 sppp_pap_TO(void *cookie)
4551 struct sppp *sp = (struct sppp *)cookie;
4556 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4558 sppp_state_name(sp->state[IDX_PAP]),
4559 sp->rst_counter[IDX_PAP]);
4561 if (--sp->rst_counter[IDX_PAP] < 0)
4563 switch (sp->state[IDX_PAP]) {
4564 case STATE_REQ_SENT:
4566 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4570 /* TO+ event, not very much we could do */
4571 switch (sp->state[IDX_PAP]) {
4572 case STATE_REQ_SENT:
4573 /* sppp_cp_change_state() will restart the timer */
4574 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4582 * That's the timeout handler if we are peer. Since the peer is active,
4583 * we need to retransmit our PAP request since it is apparently lost.
4584 * XXX We should impose a max counter.
4587 sppp_pap_my_TO(void *cookie)
4589 struct sppp *sp = (struct sppp *)cookie;
4593 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4602 sppp_pap_tlu(struct sppp *sp)
4606 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4609 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4610 SPP_ARGS(ifp), pap.name);
4613 /* indicate to LCP that we need to be closed down */
4614 sp->lcp.protos |= (1 << IDX_PAP);
4616 if (sp->pp_flags & PP_NEEDAUTH) {
4618 * Remote is authenticator, but his auth proto didn't
4619 * complete yet. Defer the transition to network
4626 sppp_phase_network(sp);
4630 sppp_pap_tld(struct sppp *sp)
4635 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4636 callout_stop (&sp->ch[IDX_PAP]);
4637 callout_stop (&sp->pap_my_to_ch);
4638 sp->lcp.protos &= ~(1 << IDX_PAP);
4644 sppp_pap_scr(struct sppp *sp)
4646 u_char idlen, pwdlen;
4648 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4649 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4650 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4652 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4653 sizeof idlen, (const char *)&idlen,
4654 (size_t)idlen, sp->myauth.name,
4655 sizeof pwdlen, (const char *)&pwdlen,
4656 (size_t)pwdlen, sp->myauth.secret,
4661 * Random miscellaneous functions.
4665 * Send a PAP or CHAP proto packet.
4667 * Varadic function, each of the elements for the ellipsis is of type
4668 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4670 * NOTE: never declare variadic functions with types subject to type
4671 * promotion (i.e. u_char). This is asking for big trouble depending
4672 * on the architecture you are on...
4676 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4677 unsigned int type, unsigned int id,
4681 struct ppp_header *h;
4682 struct lcp_header *lh;
4690 MGETHDR (m, M_NOWAIT, MT_DATA);
4693 m->m_pkthdr.rcvif = 0;
4695 h = mtod (m, struct ppp_header*);
4696 h->address = PPP_ALLSTATIONS; /* broadcast address */
4697 h->control = PPP_UI; /* Unnumbered Info */
4698 h->protocol = htons(cp->proto);
4700 lh = (struct lcp_header*)(h + 1);
4703 p = (u_char*) (lh+1);
4708 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
4709 msg = va_arg(ap, const char *);
4711 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4717 bcopy(msg, p, mlen);
4722 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4723 lh->len = htons (LCP_HEADER_LEN + len);
4726 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4727 SPP_ARGS(ifp), cp->name,
4728 sppp_auth_type_name(cp->proto, lh->type),
4729 lh->ident, ntohs(lh->len));
4730 sppp_print_bytes((u_char*) (lh+1), len);
4733 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
4734 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4738 * Flush interface queue.
4741 sppp_qflush(struct ifqueue *ifq)
4756 * Send keepalive packets, every 10 seconds.
4759 sppp_keepalive(void *dummy)
4761 struct sppp *sp = (struct sppp*)dummy;
4762 struct ifnet *ifp = SP2IFP(sp);
4765 /* Keepalive mode disabled or channel down? */
4766 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4767 ! (ifp->if_drv_flags & IFF_DRV_RUNNING))
4770 if (sp->pp_mode == PP_FR) {
4771 sppp_fr_keepalive (sp);
4775 /* No keepalive in PPP mode if LCP not opened yet. */
4776 if (sp->pp_mode != IFF_CISCO &&
4777 sp->pp_phase < PHASE_AUTHENTICATE)
4780 if (sp->pp_alivecnt == MAXALIVECNT) {
4781 /* No keepalive packets got. Stop the interface. */
4782 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
4784 sppp_qflush (&sp->pp_cpq);
4785 if (sp->pp_mode != IFF_CISCO) {
4787 /* Shut down the PPP link. */
4789 /* Initiate negotiation. XXX */
4793 if (sp->pp_alivecnt <= MAXALIVECNT)
4795 if (sp->pp_mode == IFF_CISCO)
4796 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4797 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4798 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4799 uint32_t nmagic = htonl(sp->lcp.magic);
4800 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4801 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4802 sp->lcp.echoid, 4, &nmagic);
4806 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
4811 * Get both IP addresses.
4814 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4816 struct epoch_tracker et;
4817 struct ifnet *ifp = SP2IFP(sp);
4819 struct sockaddr_in *si, *sm;
4825 * Pick the first AF_INET address from the list,
4826 * aliases don't make any sense on a p2p link anyway.
4829 NET_EPOCH_ENTER(et);
4830 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4831 if (ifa->ifa_addr->sa_family == AF_INET) {
4832 si = (struct sockaddr_in *)ifa->ifa_addr;
4833 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4838 if (si && si->sin_addr.s_addr) {
4839 ssrc = si->sin_addr.s_addr;
4841 *srcmask = ntohl(sm->sin_addr.s_addr);
4844 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4845 if (si && si->sin_addr.s_addr)
4846 ddst = si->sin_addr.s_addr;
4850 if (dst) *dst = ntohl(ddst);
4851 if (src) *src = ntohl(ssrc);
4856 * Set my IP address.
4859 sppp_set_ip_addr(struct sppp *sp, u_long src)
4862 struct epoch_tracker et;
4864 struct sockaddr_in *si;
4865 struct in_ifaddr *ia;
4868 * Pick the first AF_INET address from the list,
4869 * aliases don't make any sense on a p2p link anyway.
4872 NET_EPOCH_ENTER(et);
4873 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4874 if (ifa->ifa_addr->sa_family == AF_INET) {
4875 si = (struct sockaddr_in *)ifa->ifa_addr;
4886 int fibnum = ifp->if_fib;
4888 rt_addrmsg(RTM_DELETE, ifa, fibnum);
4889 /* delete old route */
4891 error = in_handle_ifaddr_route(RTM_DELETE, ia);
4892 if (debug && error) {
4893 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4894 SPP_ARGS(ifp), error);
4897 /* set new address */
4898 si->sin_addr.s_addr = htonl(src);
4900 LIST_REMOVE(ia, ia_hash);
4901 LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash);
4902 IN_IFADDR_WUNLOCK();
4904 rt_addrmsg(RTM_ADD, ifa, fibnum);
4906 error = in_handle_ifaddr_route(RTM_ADD, ia);
4907 if (debug && error) {
4908 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
4909 SPP_ARGS(ifp), error);
4918 * Get both IPv6 addresses.
4921 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4922 struct in6_addr *srcmask)
4924 struct epoch_tracker et;
4925 struct ifnet *ifp = SP2IFP(sp);
4927 struct sockaddr_in6 *si, *sm;
4928 struct in6_addr ssrc, ddst;
4931 bzero(&ssrc, sizeof(ssrc));
4932 bzero(&ddst, sizeof(ddst));
4934 * Pick the first link-local AF_INET6 address from the list,
4935 * aliases don't make any sense on a p2p link anyway.
4938 NET_EPOCH_ENTER(et);
4939 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4940 if (ifa->ifa_addr->sa_family == AF_INET6) {
4941 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4942 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4943 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
4947 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
4948 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
4950 bcopy(&sm->sin6_addr, srcmask,
4955 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
4956 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
4957 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
4961 bcopy(&ddst, dst, sizeof(*dst));
4963 bcopy(&ssrc, src, sizeof(*src));
4967 #ifdef IPV6CP_MYIFID_DYN
4969 * Generate random ifid.
4972 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
4978 * Set my IPv6 address.
4981 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
4984 struct epoch_tracker et;
4986 struct sockaddr_in6 *sin6;
4989 * Pick the first link-local AF_INET6 address from the list,
4990 * aliases don't make any sense on a p2p link anyway.
4994 NET_EPOCH_ENTER(et);
4995 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4996 if (ifa->ifa_addr->sa_family == AF_INET6) {
4997 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
4998 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5008 struct sockaddr_in6 new_sin6 = *sin6;
5010 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
5011 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5012 if (debug && error) {
5013 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
5014 " failed, error=%d\n", SPP_ARGS(ifp), error);
5022 * Suggest a candidate address to be used by peer.
5025 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5027 struct in6_addr myaddr;
5030 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5032 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5034 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5035 myaddr.s6_addr[14] ^= 0xff;
5036 myaddr.s6_addr[15] ^= 0xff;
5038 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5039 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5042 bcopy(&myaddr, suggest, sizeof(myaddr));
5047 sppp_params(struct sppp *sp, u_long cmd, void *data)
5050 struct ifreq *ifr = (struct ifreq *)data;
5051 struct spppreq *spr;
5054 if ((spr = malloc(sizeof(struct spppreq), M_TEMP, M_NOWAIT)) == NULL)
5057 * ifr_data_get_ptr(ifr) is supposed to point to a struct spppreq.
5058 * Check the cmd word first before attempting to fetch all the
5061 rv = fueword(ifr_data_get_ptr(ifr), &subcmd);
5067 if (copyin(ifr_data_get_ptr(ifr), spr, sizeof(struct spppreq)) != 0) {
5073 case (u_long)SPPPIOGDEFS:
5074 if (cmd != SIOCGIFGENERIC) {
5079 * We copy over the entire current state, but clean
5080 * out some of the stuff we don't wanna pass up.
5081 * Remember, SIOCGIFGENERIC is unprotected, and can be
5082 * called by any user. No need to ever get PAP or
5083 * CHAP secrets back to userland anyway.
5085 spr->defs.pp_phase = sp->pp_phase;
5086 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5087 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5088 spr->defs.lcp = sp->lcp;
5089 spr->defs.ipcp = sp->ipcp;
5090 spr->defs.ipv6cp = sp->ipv6cp;
5091 spr->defs.myauth = sp->myauth;
5092 spr->defs.hisauth = sp->hisauth;
5093 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5094 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5095 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5096 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5098 * Fixup the LCP timeout value to milliseconds so
5099 * spppcontrol doesn't need to bother about the value
5100 * of "hz". We do the reverse calculation below when
5103 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5104 rv = copyout(spr, ifr_data_get_ptr(ifr),
5105 sizeof(struct spppreq));
5108 case (u_long)SPPPIOSDEFS:
5109 if (cmd != SIOCSIFGENERIC) {
5114 * We have a very specific idea of which fields we
5115 * allow being passed back from userland, so to not
5116 * clobber our current state. For one, we only allow
5117 * setting anything if LCP is in dead or establish
5118 * phase. Once the authentication negotiations
5119 * started, the authentication settings must not be
5120 * changed again. (The administrator can force an
5121 * ifconfig down in order to get LCP back into dead
5124 * Also, we only allow for authentication parameters to be
5127 * XXX Should allow to set or clear pp_flags.
5129 * Finally, if the respective authentication protocol to
5130 * be used is set differently than 0, but the secret is
5131 * passed as all zeros, we don't trash the existing secret.
5132 * This allows an administrator to change the system name
5133 * only without clobbering the secret (which he didn't get
5134 * back in a previous SPPPIOGDEFS call). However, the
5135 * secrets are cleared if the authentication protocol is
5137 if (sp->pp_phase != PHASE_DEAD &&
5138 sp->pp_phase != PHASE_ESTABLISH) {
5143 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5144 spr->defs.myauth.proto != PPP_CHAP) ||
5145 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5146 spr->defs.hisauth.proto != PPP_CHAP)) {
5151 if (spr->defs.myauth.proto == 0)
5152 /* resetting myauth */
5153 bzero(&sp->myauth, sizeof sp->myauth);
5155 /* setting/changing myauth */
5156 sp->myauth.proto = spr->defs.myauth.proto;
5157 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5158 if (spr->defs.myauth.secret[0] != '\0')
5159 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5162 if (spr->defs.hisauth.proto == 0)
5163 /* resetting hisauth */
5164 bzero(&sp->hisauth, sizeof sp->hisauth);
5166 /* setting/changing hisauth */
5167 sp->hisauth.proto = spr->defs.hisauth.proto;
5168 sp->hisauth.flags = spr->defs.hisauth.flags;
5169 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5170 if (spr->defs.hisauth.secret[0] != '\0')
5171 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5174 /* set LCP restart timer timeout */
5175 if (spr->defs.lcp.timeout != 0)
5176 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5177 /* set VJ enable and IPv6 disable flags */
5179 if (spr->defs.enable_vj)
5180 sp->confflags |= CONF_ENABLE_VJ;
5182 sp->confflags &= ~CONF_ENABLE_VJ;
5185 if (spr->defs.enable_ipv6)
5186 sp->confflags |= CONF_ENABLE_IPV6;
5188 sp->confflags &= ~CONF_ENABLE_IPV6;
5203 sppp_phase_network(struct sppp *sp)
5209 sp->pp_phase = PHASE_NETWORK;
5212 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5213 sppp_phase_name(sp->pp_phase));
5215 /* Notify NCPs now. */
5216 for (i = 0; i < IDX_COUNT; i++)
5217 if ((cps[i])->flags & CP_NCP)
5220 /* Send Up events to all NCPs. */
5221 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5222 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5225 /* if no NCP is starting, all this was in vain, close down */
5226 sppp_lcp_check_and_close(sp);
5230 sppp_cp_type_name(u_char type)
5232 static char buf[12];
5234 case CONF_REQ: return "conf-req";
5235 case CONF_ACK: return "conf-ack";
5236 case CONF_NAK: return "conf-nak";
5237 case CONF_REJ: return "conf-rej";
5238 case TERM_REQ: return "term-req";
5239 case TERM_ACK: return "term-ack";
5240 case CODE_REJ: return "code-rej";
5241 case PROTO_REJ: return "proto-rej";
5242 case ECHO_REQ: return "echo-req";
5243 case ECHO_REPLY: return "echo-reply";
5244 case DISC_REQ: return "discard-req";
5246 snprintf (buf, sizeof(buf), "cp/0x%x", type);
5251 sppp_auth_type_name(u_short proto, u_char type)
5253 static char buf[12];
5257 case CHAP_CHALLENGE: return "challenge";
5258 case CHAP_RESPONSE: return "response";
5259 case CHAP_SUCCESS: return "success";
5260 case CHAP_FAILURE: return "failure";
5264 case PAP_REQ: return "req";
5265 case PAP_ACK: return "ack";
5266 case PAP_NAK: return "nak";
5269 snprintf (buf, sizeof(buf), "auth/0x%x", type);
5274 sppp_lcp_opt_name(u_char opt)
5276 static char buf[12];
5278 case LCP_OPT_MRU: return "mru";
5279 case LCP_OPT_ASYNC_MAP: return "async-map";
5280 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5281 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5282 case LCP_OPT_MAGIC: return "magic";
5283 case LCP_OPT_PROTO_COMP: return "proto-comp";
5284 case LCP_OPT_ADDR_COMP: return "addr-comp";
5286 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5292 sppp_ipcp_opt_name(u_char opt)
5294 static char buf[12];
5296 case IPCP_OPT_ADDRESSES: return "addresses";
5297 case IPCP_OPT_COMPRESSION: return "compression";
5298 case IPCP_OPT_ADDRESS: return "address";
5300 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5307 sppp_ipv6cp_opt_name(u_char opt)
5309 static char buf[12];
5311 case IPV6CP_OPT_IFID: return "ifid";
5312 case IPV6CP_OPT_COMPRESSION: return "compression";
5314 sprintf (buf, "0x%x", opt);
5320 sppp_state_name(int state)
5323 case STATE_INITIAL: return "initial";
5324 case STATE_STARTING: return "starting";
5325 case STATE_CLOSED: return "closed";
5326 case STATE_STOPPED: return "stopped";
5327 case STATE_CLOSING: return "closing";
5328 case STATE_STOPPING: return "stopping";
5329 case STATE_REQ_SENT: return "req-sent";
5330 case STATE_ACK_RCVD: return "ack-rcvd";
5331 case STATE_ACK_SENT: return "ack-sent";
5332 case STATE_OPENED: return "opened";
5338 sppp_phase_name(enum ppp_phase phase)
5341 case PHASE_DEAD: return "dead";
5342 case PHASE_ESTABLISH: return "establish";
5343 case PHASE_TERMINATE: return "terminate";
5344 case PHASE_AUTHENTICATE: return "authenticate";
5345 case PHASE_NETWORK: return "network";
5351 sppp_proto_name(u_short proto)
5353 static char buf[12];
5355 case PPP_LCP: return "lcp";
5356 case PPP_IPCP: return "ipcp";
5357 case PPP_PAP: return "pap";
5358 case PPP_CHAP: return "chap";
5359 case PPP_IPV6CP: return "ipv6cp";
5361 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5366 sppp_print_bytes(const u_char *p, u_short len)
5369 log(-1, " %*D", len, p, "-");
5373 sppp_print_string(const char *p, u_short len)
5380 * Print only ASCII chars directly. RFC 1994 recommends
5381 * using only them, but we don't rely on it. */
5382 if (c < ' ' || c > '~')
5383 log(-1, "\\x%x", c);
5391 sppp_dotted_quad(u_long addr)
5394 sprintf(s, "%d.%d.%d.%d",
5395 (int)((addr >> 24) & 0xff),
5396 (int)((addr >> 16) & 0xff),
5397 (int)((addr >> 8) & 0xff),
5398 (int)(addr & 0xff));
5404 sppp_strnlen(u_char *p, int max)
5408 for (len = 0; len < max && *p; ++p)
5413 /* a dummy, used to drop uninteresting events */
5415 sppp_null(struct sppp *unused)
5417 /* do just nothing */