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_systm.h>
51 #include <netinet/ip.h>
52 #include <net/slcompress.h>
54 #include <machine/stdarg.h>
56 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/tcp.h>
64 #include <netinet6/scope6_var.h>
67 #include <netinet/if_ether.h>
69 #include <net/if_sppp.h>
71 #define IOCTL_CMD_T u_long
72 #define MAXALIVECNT 3 /* max. alive packets */
75 * Interface flags that can be set in an ifconfig command.
77 * Setting link0 will make the link passive, i.e. it will be marked
78 * as being administrative openable, but won't be opened to begin
79 * with. Incoming calls will be answered, or subsequent calls with
80 * -link1 will cause the administrative open of the LCP layer.
82 * Setting link1 will cause the link to auto-dial only as packets
85 * Setting IFF_DEBUG will syslog the option negotiation and state
86 * transitions at level kern.debug. Note: all logs consistently look
89 * <if-name><unit>: <proto-name> <additional info...>
91 * with <if-name><unit> being something like "bppp0", and <proto-name>
92 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
95 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
96 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
97 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
99 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
100 #define PPP_UI 0x03 /* Unnumbered Information */
101 #define PPP_IP 0x0021 /* Internet Protocol */
102 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
103 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
104 #define PPP_IPX 0x002b /* Novell IPX Protocol */
105 #define PPP_VJ_COMP 0x002d /* VJ compressed TCP/IP */
106 #define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */
107 #define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */
108 #define PPP_LCP 0xc021 /* Link Control Protocol */
109 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
110 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
111 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
112 #define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */
114 #define CONF_REQ 1 /* PPP configure request */
115 #define CONF_ACK 2 /* PPP configure acknowledge */
116 #define CONF_NAK 3 /* PPP configure negative ack */
117 #define CONF_REJ 4 /* PPP configure reject */
118 #define TERM_REQ 5 /* PPP terminate request */
119 #define TERM_ACK 6 /* PPP terminate acknowledge */
120 #define CODE_REJ 7 /* PPP code reject */
121 #define PROTO_REJ 8 /* PPP protocol reject */
122 #define ECHO_REQ 9 /* PPP echo request */
123 #define ECHO_REPLY 10 /* PPP echo reply */
124 #define DISC_REQ 11 /* PPP discard request */
126 #define LCP_OPT_MRU 1 /* maximum receive unit */
127 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
128 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
129 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
130 #define LCP_OPT_MAGIC 5 /* magic number */
131 #define LCP_OPT_RESERVED 6 /* reserved */
132 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
133 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
135 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
136 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
137 #define IPCP_OPT_ADDRESS 3 /* local IP address */
139 #define IPV6CP_OPT_IFID 1 /* interface identifier */
140 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
142 #define IPCP_COMP_VJ 0x2d /* Code for VJ compression */
144 #define PAP_REQ 1 /* PAP name/password request */
145 #define PAP_ACK 2 /* PAP acknowledge */
146 #define PAP_NAK 3 /* PAP fail */
148 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
149 #define CHAP_RESPONSE 2 /* CHAP challenge response */
150 #define CHAP_SUCCESS 3 /* CHAP response ok */
151 #define CHAP_FAILURE 4 /* CHAP response failed */
153 #define CHAP_MD5 5 /* hash algorithm - MD5 */
155 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
156 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
157 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
158 #define CISCO_ADDR_REQ 0 /* Cisco address request */
159 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
160 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
162 /* states are named and numbered according to RFC 1661 */
163 #define STATE_INITIAL 0
164 #define STATE_STARTING 1
165 #define STATE_CLOSED 2
166 #define STATE_STOPPED 3
167 #define STATE_CLOSING 4
168 #define STATE_STOPPING 5
169 #define STATE_REQ_SENT 6
170 #define STATE_ACK_RCVD 7
171 #define STATE_ACK_SENT 8
172 #define STATE_OPENED 9
174 static MALLOC_DEFINE(M_SPPP, "sppp", "synchronous PPP interface internals");
181 #define PPP_HEADER_LEN sizeof (struct ppp_header)
188 #define LCP_HEADER_LEN sizeof (struct lcp_header)
190 struct cisco_packet {
198 #define CISCO_PACKET_LEN sizeof (struct cisco_packet)
201 * We follow the spelling and capitalization of RFC 1661 here, to make
202 * it easier comparing with the standard. Please refer to this RFC in
203 * case you can't make sense out of these abbreviation; it will also
204 * explain the semantics related to the various events and actions.
207 u_short proto; /* PPP control protocol number */
208 u_char protoidx; /* index into state table in struct sppp */
210 #define CP_LCP 0x01 /* this is the LCP */
211 #define CP_AUTH 0x02 /* this is an authentication protocol */
212 #define CP_NCP 0x04 /* this is a NCP */
213 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
214 const char *name; /* name of this control protocol */
216 void (*Up)(struct sppp *sp);
217 void (*Down)(struct sppp *sp);
218 void (*Open)(struct sppp *sp);
219 void (*Close)(struct sppp *sp);
220 void (*TO)(void *sp);
221 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
222 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
223 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
225 void (*tlu)(struct sppp *sp);
226 void (*tld)(struct sppp *sp);
227 void (*tls)(struct sppp *sp);
228 void (*tlf)(struct sppp *sp);
229 void (*scr)(struct sppp *sp);
232 #define SPP_FMT "%s: "
233 #define SPP_ARGS(ifp) (ifp)->if_xname
235 #define SPPP_LOCK(sp) mtx_lock (&(sp)->mtx)
236 #define SPPP_UNLOCK(sp) mtx_unlock (&(sp)->mtx)
237 #define SPPP_LOCK_ASSERT(sp) mtx_assert (&(sp)->mtx, MA_OWNED)
238 #define SPPP_LOCK_OWNED(sp) mtx_owned (&(sp)->mtx)
242 * The following disgusting hack gets around the problem that IP TOS
243 * can't be set yet. We want to put "interactive" traffic on a high
244 * priority queue. To decide if traffic is interactive, we check that
245 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
247 * XXX is this really still necessary? - joerg -
249 static const u_short interactive_ports[8] = {
253 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
256 /* almost every function needs these */
258 struct ifnet *ifp = SP2IFP(sp); \
259 int debug = ifp->if_flags & IFF_DEBUG
261 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
262 const struct sockaddr *dst, struct route *ro);
264 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
265 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
267 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
269 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
270 u_char ident, u_short len, void *data);
271 /* static void sppp_cp_timeout(void *arg); */
272 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
274 static void sppp_auth_send(const struct cp *cp,
275 struct sppp *sp, unsigned int type, unsigned int id,
278 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
279 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
280 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
281 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
282 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
284 static void sppp_null(struct sppp *sp);
286 static void sppp_pp_up(struct sppp *sp);
287 static void sppp_pp_down(struct sppp *sp);
289 static void sppp_lcp_init(struct sppp *sp);
290 static void sppp_lcp_up(struct sppp *sp);
291 static void sppp_lcp_down(struct sppp *sp);
292 static void sppp_lcp_open(struct sppp *sp);
293 static void sppp_lcp_close(struct sppp *sp);
294 static void sppp_lcp_TO(void *sp);
295 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
296 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
297 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
298 static void sppp_lcp_tlu(struct sppp *sp);
299 static void sppp_lcp_tld(struct sppp *sp);
300 static void sppp_lcp_tls(struct sppp *sp);
301 static void sppp_lcp_tlf(struct sppp *sp);
302 static void sppp_lcp_scr(struct sppp *sp);
303 static void sppp_lcp_check_and_close(struct sppp *sp);
304 static int sppp_ncp_check(struct sppp *sp);
306 static void sppp_ipcp_init(struct sppp *sp);
307 static void sppp_ipcp_up(struct sppp *sp);
308 static void sppp_ipcp_down(struct sppp *sp);
309 static void sppp_ipcp_open(struct sppp *sp);
310 static void sppp_ipcp_close(struct sppp *sp);
311 static void sppp_ipcp_TO(void *sp);
312 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
313 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
314 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
315 static void sppp_ipcp_tlu(struct sppp *sp);
316 static void sppp_ipcp_tld(struct sppp *sp);
317 static void sppp_ipcp_tls(struct sppp *sp);
318 static void sppp_ipcp_tlf(struct sppp *sp);
319 static void sppp_ipcp_scr(struct sppp *sp);
321 static void sppp_ipv6cp_init(struct sppp *sp);
322 static void sppp_ipv6cp_up(struct sppp *sp);
323 static void sppp_ipv6cp_down(struct sppp *sp);
324 static void sppp_ipv6cp_open(struct sppp *sp);
325 static void sppp_ipv6cp_close(struct sppp *sp);
326 static void sppp_ipv6cp_TO(void *sp);
327 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
328 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
329 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
330 static void sppp_ipv6cp_tlu(struct sppp *sp);
331 static void sppp_ipv6cp_tld(struct sppp *sp);
332 static void sppp_ipv6cp_tls(struct sppp *sp);
333 static void sppp_ipv6cp_tlf(struct sppp *sp);
334 static void sppp_ipv6cp_scr(struct sppp *sp);
336 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
337 static void sppp_pap_init(struct sppp *sp);
338 static void sppp_pap_open(struct sppp *sp);
339 static void sppp_pap_close(struct sppp *sp);
340 static void sppp_pap_TO(void *sp);
341 static void sppp_pap_my_TO(void *sp);
342 static void sppp_pap_tlu(struct sppp *sp);
343 static void sppp_pap_tld(struct sppp *sp);
344 static void sppp_pap_scr(struct sppp *sp);
346 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
347 static void sppp_chap_init(struct sppp *sp);
348 static void sppp_chap_open(struct sppp *sp);
349 static void sppp_chap_close(struct sppp *sp);
350 static void sppp_chap_TO(void *sp);
351 static void sppp_chap_tlu(struct sppp *sp);
352 static void sppp_chap_tld(struct sppp *sp);
353 static void sppp_chap_scr(struct sppp *sp);
355 static const char *sppp_auth_type_name(u_short proto, u_char type);
356 static const char *sppp_cp_type_name(u_char type);
358 static const char *sppp_dotted_quad(u_long addr);
359 static const char *sppp_ipcp_opt_name(u_char opt);
362 static const char *sppp_ipv6cp_opt_name(u_char opt);
364 static const char *sppp_lcp_opt_name(u_char opt);
365 static const char *sppp_phase_name(enum ppp_phase phase);
366 static const char *sppp_proto_name(u_short proto);
367 static const char *sppp_state_name(int state);
368 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
369 static int sppp_strnlen(u_char *p, int max);
370 static void sppp_keepalive(void *dummy);
371 static void sppp_phase_network(struct sppp *sp);
372 static void sppp_print_bytes(const u_char *p, u_short len);
373 static void sppp_print_string(const char *p, u_short len);
374 static void sppp_qflush(struct ifqueue *ifq);
376 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
379 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
380 struct in6_addr *dst, struct in6_addr *srcmask);
381 #ifdef IPV6CP_MYIFID_DYN
382 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
383 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
385 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
388 /* if_start () wrapper */
389 static void sppp_ifstart (struct ifnet *ifp);
391 /* our control protocol descriptors */
392 static const struct cp lcp = {
393 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
394 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
395 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
396 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
400 static const struct cp ipcp = {
402 #ifdef INET /* don't run IPCP if there's no IPv4 support */
408 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
409 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
410 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
414 static const struct cp ipv6cp = {
415 PPP_IPV6CP, IDX_IPV6CP,
416 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
422 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
423 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
424 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
428 static const struct cp pap = {
429 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
430 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
431 sppp_pap_TO, 0, 0, 0,
432 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
436 static const struct cp chap = {
437 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
438 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
439 sppp_chap_TO, 0, 0, 0,
440 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
444 static const struct cp *cps[IDX_COUNT] = {
446 &ipcp, /* IDX_IPCP */
447 &ipv6cp, /* IDX_IPV6CP */
449 &chap, /* IDX_CHAP */
453 sppp_alloc(u_char type, struct ifnet *ifp)
457 sp = malloc(sizeof(struct sppp), M_SPPP, M_WAITOK | M_ZERO);
464 sppp_free(void *com, u_char type)
471 sppp_modevent(module_t mod, int type, void *unused)
476 * XXX: should probably be IFT_SPPP, but it's fairly
477 * harmless to allocate struct sppp's for non-sppp
481 if_register_com_alloc(IFT_PPP, sppp_alloc, sppp_free);
484 /* if_deregister_com_alloc(IFT_PPP); */
491 static moduledata_t spppmod = {
496 MODULE_VERSION(sppp, 1);
497 DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
500 * Exported functions, comprising our interface to the lower layer.
504 * Process the received packet.
507 sppp_input(struct ifnet *ifp, struct mbuf *m)
509 struct ppp_header *h;
511 struct sppp *sp = IFP2SP(ifp);
512 int debug, do_account = 0;
519 debug = ifp->if_flags & IFF_DEBUG;
521 if (ifp->if_flags & IFF_UP)
522 /* Count received bytes, add FCS and one flag */
523 if_inc_counter(ifp, IFCOUNTER_IBYTES, m->m_pkthdr.len + 3);
525 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
526 /* Too small packet, drop it. */
529 SPP_FMT "input packet is too small, %d bytes\n",
530 SPP_ARGS(ifp), m->m_pkthdr.len);
535 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
536 if_inc_counter(ifp, IFCOUNTER_IQDROPS, 1);
540 if (sp->pp_mode == PP_FR) {
541 sppp_fr_input (sp, m);
546 /* Get PPP header. */
547 h = mtod (m, struct ppp_header*);
548 m_adj (m, PPP_HEADER_LEN);
550 switch (h->address) {
551 case PPP_ALLSTATIONS:
552 if (h->control != PPP_UI)
554 if (sp->pp_mode == IFF_CISCO) {
557 SPP_FMT "PPP packet in Cisco mode "
558 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
560 h->address, h->control, ntohs(h->protocol));
563 switch (ntohs (h->protocol)) {
567 SPP_FMT "rejecting protocol "
568 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
570 h->address, h->control, ntohs(h->protocol));
571 if (sp->state[IDX_LCP] == STATE_OPENED)
572 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
573 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
575 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
578 sppp_cp_input(&lcp, sp, m);
583 if (sp->pp_phase >= PHASE_AUTHENTICATE)
584 sppp_pap_input(sp, m);
589 if (sp->pp_phase >= PHASE_AUTHENTICATE)
590 sppp_chap_input(sp, m);
596 if (sp->pp_phase == PHASE_NETWORK)
597 sppp_cp_input(&ipcp, sp, m);
602 if (sp->state[IDX_IPCP] == STATE_OPENED) {
608 if (sp->state[IDX_IPCP] == STATE_OPENED) {
610 sl_uncompress_tcp_core(mtod(m, u_char *),
614 &iphdr, &hlen)) <= 0) {
617 SPP_FMT "VJ uncompress failed on compressed packet\n",
623 * Trim the VJ header off the packet, and prepend
624 * the uncompressed IP header (which will usually
625 * end up in two chained mbufs since there's not
626 * enough leading space in the existing mbuf).
629 M_PREPEND(m, hlen, M_NOWAIT);
634 bcopy(iphdr, mtod(m, u_char *), hlen);
640 if (sp->state[IDX_IPCP] == STATE_OPENED) {
641 if (sl_uncompress_tcp_core(mtod(m, u_char *),
643 TYPE_UNCOMPRESSED_TCP,
645 &iphdr, &hlen) != 0) {
648 SPP_FMT "VJ uncompress failed on uncompressed packet\n",
659 if (sp->pp_phase == PHASE_NETWORK)
660 sppp_cp_input(&ipv6cp, sp, m);
666 if (sp->state[IDX_IPV6CP] == STATE_OPENED)
673 case CISCO_MULTICAST:
675 /* Don't check the control field here (RFC 1547). */
676 if (sp->pp_mode != IFF_CISCO) {
679 SPP_FMT "Cisco packet in PPP mode "
680 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
682 h->address, h->control, ntohs(h->protocol));
685 switch (ntohs (h->protocol)) {
687 if_inc_counter(ifp, IFCOUNTER_NOPROTO, 1);
689 case CISCO_KEEPALIVE:
690 sppp_cisco_input (sp, m);
708 default: /* Invalid PPP packet. */
712 SPP_FMT "invalid input packet "
713 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
715 h->address, h->control, ntohs(h->protocol));
719 if (! (ifp->if_flags & IFF_UP) || isr == -1)
723 M_SETFIB(m, ifp->if_fib);
725 if (netisr_queue(isr, m)) { /* (0) on success. */
727 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
734 * Do only account for network packets, not for control
735 * packets. This is used by some subsystems to detect
738 sp->pp_last_recv = time_uptime;
742 sppp_ifstart_sched(void *dummy)
744 struct sppp *sp = dummy;
746 sp->if_start(SP2IFP(sp));
749 /* if_start () wrapper function. We use it to schedule real if_start () for
750 * execution. We can't call it directly
753 sppp_ifstart(struct ifnet *ifp)
755 struct sppp *sp = IFP2SP(ifp);
757 if (SPPP_LOCK_OWNED(sp)) {
758 if (callout_pending(&sp->ifstart_callout))
760 callout_reset(&sp->ifstart_callout, 1, sppp_ifstart_sched,
768 * Enqueue transmit packet.
771 sppp_output(struct ifnet *ifp, struct mbuf *m, const struct sockaddr *dst,
774 struct sppp *sp = IFP2SP(ifp);
775 struct ppp_header *h;
776 struct ifqueue *ifq = NULL;
779 int ipproto = PPP_IP;
781 int debug = ifp->if_flags & IFF_DEBUG;
785 if (!(ifp->if_flags & IFF_UP) ||
786 (!(ifp->if_flags & IFF_AUTO) &&
787 !(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
796 if ((ifp->if_flags & IFF_AUTO) &&
797 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
802 * Hack to prevent the initialization-time generated
803 * IPv6 multicast packet to erroneously cause a
804 * dialout event in case IPv6 has been
805 * administratively disabled on that interface.
807 if (dst->sa_family == AF_INET6 &&
808 !(sp->confflags & CONF_ENABLE_IPV6))
812 * Interface is not yet running, but auto-dial. Need
813 * to start LCP for it.
815 ifp->if_drv_flags |= IFF_DRV_RUNNING;
820 if (dst->sa_family == AF_INET) {
821 /* XXX Check mbuf length here? */
822 struct ip *ip = mtod (m, struct ip*);
823 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
826 * When using dynamic local IP address assignment by using
827 * 0.0.0.0 as a local address, the first TCP session will
828 * not connect because the local TCP checksum is computed
829 * using 0.0.0.0 which will later become our real IP address
830 * so the TCP checksum computed at the remote end will
831 * become invalid. So we
832 * - don't let packets with src ip addr 0 thru
833 * - we flag TCP packets with src ip 0 as an error
836 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
840 if(ip->ip_p == IPPROTO_TCP)
841 return(EADDRNOTAVAIL);
847 * Put low delay, telnet, rlogin and ftp control packets
848 * in front of the queue or let ALTQ take care.
850 if (ALTQ_IS_ENABLED(&ifp->if_snd))
852 else if (_IF_QFULL(&sp->pp_fastq))
854 else if (ip->ip_tos & IPTOS_LOWDELAY)
856 else if (m->m_len < sizeof *ip + sizeof *tcp)
858 else if (ip->ip_p != IPPROTO_TCP)
860 else if (INTERACTIVE (ntohs (tcp->th_sport)))
862 else if (INTERACTIVE (ntohs (tcp->th_dport)))
866 * Do IP Header compression
868 if (sp->pp_mode != IFF_CISCO && sp->pp_mode != PP_FR &&
869 (sp->ipcp.flags & IPCP_VJ) && ip->ip_p == IPPROTO_TCP)
870 switch (sl_compress_tcp(m, ip, sp->pp_comp,
871 sp->ipcp.compress_cid)) {
872 case TYPE_COMPRESSED_TCP:
873 ipproto = PPP_VJ_COMP;
875 case TYPE_UNCOMPRESSED_TCP:
876 ipproto = PPP_VJ_UCOMP;
890 if (dst->sa_family == AF_INET6) {
891 /* XXX do something tricky here? */
895 if (sp->pp_mode == PP_FR) {
896 /* Add frame relay header. */
897 m = sppp_fr_header (sp, m, dst->sa_family);
904 * Prepend general data packet PPP header. For now, IP only.
906 M_PREPEND (m, PPP_HEADER_LEN, M_NOWAIT);
909 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
911 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
916 * May want to check size of packet
917 * (albeit due to the implementation it's always enough)
919 h = mtod (m, struct ppp_header*);
920 if (sp->pp_mode == IFF_CISCO) {
921 h->address = CISCO_UNICAST; /* unicast address */
924 h->address = PPP_ALLSTATIONS; /* broadcast address */
925 h->control = PPP_UI; /* Unnumbered Info */
928 switch (dst->sa_family) {
930 case AF_INET: /* Internet Protocol */
931 if (sp->pp_mode == IFF_CISCO)
932 h->protocol = htons (ETHERTYPE_IP);
935 * Don't choke with an ENETDOWN early. It's
936 * possible that we just started dialing out,
937 * so don't drop the packet immediately. If
938 * we notice that we run out of buffer space
939 * below, we will however remember that we are
940 * not ready to carry IP packets, and return
941 * ENETDOWN, as opposed to ENOBUFS.
943 h->protocol = htons(ipproto);
944 if (sp->state[IDX_IPCP] != STATE_OPENED)
950 case AF_INET6: /* Internet Protocol */
951 if (sp->pp_mode == IFF_CISCO)
952 h->protocol = htons (ETHERTYPE_IPV6);
955 * Don't choke with an ENETDOWN early. It's
956 * possible that we just started dialing out,
957 * so don't drop the packet immediately. If
958 * we notice that we run out of buffer space
959 * below, we will however remember that we are
960 * not ready to carry IP packets, and return
961 * ENETDOWN, as opposed to ENOBUFS.
963 h->protocol = htons(PPP_IPV6);
964 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
971 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
973 return (EAFNOSUPPORT);
977 * Queue message on interface, and start output if interface
982 error = !(IF_HANDOFF_ADJ(ifq, m, ifp, 3));
984 IFQ_HANDOFF_ADJ(ifp, m, 3, error);
986 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
988 return (rv? rv: ENOBUFS);
992 * Unlike in sppp_input(), we can always bump the timestamp
993 * here since sppp_output() is only called on behalf of
994 * network-layer traffic; control-layer traffic is handled
997 sp->pp_last_sent = time_uptime;
1002 sppp_attach(struct ifnet *ifp)
1004 struct sppp *sp = IFP2SP(ifp);
1006 /* Initialize mtx lock */
1007 mtx_init(&sp->mtx, "sppp", MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
1009 /* Initialize keepalive handler. */
1010 callout_init(&sp->keepalive_callout, 1);
1011 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
1014 ifp->if_mtu = PP_MTU;
1015 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
1016 ifp->if_output = sppp_output;
1018 sp->pp_flags = PP_KEEPALIVE;
1020 ifp->if_snd.ifq_maxlen = 32;
1021 sp->pp_fastq.ifq_maxlen = 32;
1022 sp->pp_cpq.ifq_maxlen = 20;
1024 sp->pp_alivecnt = 0;
1025 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
1026 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
1027 sp->pp_phase = PHASE_DEAD;
1028 sp->pp_up = sppp_pp_up;
1029 sp->pp_down = sppp_pp_down;
1030 if(!mtx_initialized(&sp->pp_cpq.ifq_mtx))
1031 mtx_init(&sp->pp_cpq.ifq_mtx, "sppp_cpq", NULL, MTX_DEF);
1032 if(!mtx_initialized(&sp->pp_fastq.ifq_mtx))
1033 mtx_init(&sp->pp_fastq.ifq_mtx, "sppp_fastq", NULL, MTX_DEF);
1034 sp->pp_last_recv = sp->pp_last_sent = time_uptime;
1037 sp->confflags |= CONF_ENABLE_VJ;
1040 sp->confflags |= CONF_ENABLE_IPV6;
1042 callout_init(&sp->ifstart_callout, 1);
1043 sp->if_start = ifp->if_start;
1044 ifp->if_start = sppp_ifstart;
1045 sp->pp_comp = malloc(sizeof(struct slcompress), M_TEMP, M_WAITOK);
1046 sl_compress_init(sp->pp_comp, -1);
1049 sppp_ipv6cp_init(sp);
1055 sppp_detach(struct ifnet *ifp)
1057 struct sppp *sp = IFP2SP(ifp);
1060 KASSERT(mtx_initialized(&sp->mtx), ("sppp mutex is not initialized"));
1062 /* Stop keepalive handler. */
1063 callout_drain(&sp->keepalive_callout);
1065 for (i = 0; i < IDX_COUNT; i++) {
1066 callout_drain(&sp->ch[i]);
1068 callout_drain(&sp->pap_my_to_ch);
1070 mtx_destroy(&sp->pp_cpq.ifq_mtx);
1071 mtx_destroy(&sp->pp_fastq.ifq_mtx);
1072 mtx_destroy(&sp->mtx);
1076 * Flush the interface output queue.
1079 sppp_flush_unlocked(struct ifnet *ifp)
1081 struct sppp *sp = IFP2SP(ifp);
1083 sppp_qflush ((struct ifqueue *)&SP2IFP(sp)->if_snd);
1084 sppp_qflush (&sp->pp_fastq);
1085 sppp_qflush (&sp->pp_cpq);
1089 sppp_flush(struct ifnet *ifp)
1091 struct sppp *sp = IFP2SP(ifp);
1094 sppp_flush_unlocked (ifp);
1099 * Check if the output queue is empty.
1102 sppp_isempty(struct ifnet *ifp)
1104 struct sppp *sp = IFP2SP(ifp);
1108 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
1109 !SP2IFP(sp)->if_snd.ifq_head;
1115 * Get next packet to send.
1118 sppp_dequeue(struct ifnet *ifp)
1120 struct sppp *sp = IFP2SP(ifp);
1125 * Process only the control protocol queue until we have at
1126 * least one NCP open.
1128 * Do always serve all three queues in Cisco mode.
1130 IF_DEQUEUE(&sp->pp_cpq, m);
1132 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO ||
1133 sp->pp_mode == PP_FR)) {
1134 IF_DEQUEUE(&sp->pp_fastq, m);
1136 IF_DEQUEUE (&SP2IFP(sp)->if_snd, m);
1143 * Pick the next packet, do not remove it from the queue.
1146 sppp_pick(struct ifnet *ifp)
1148 struct sppp *sp = IFP2SP(ifp);
1153 m = sp->pp_cpq.ifq_head;
1155 (sp->pp_phase == PHASE_NETWORK ||
1156 sp->pp_mode == IFF_CISCO ||
1157 sp->pp_mode == PP_FR))
1158 if ((m = sp->pp_fastq.ifq_head) == NULL)
1159 m = SP2IFP(sp)->if_snd.ifq_head;
1165 * Process an ioctl request. Called on low priority level.
1168 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1170 struct ifreq *ifr = (struct ifreq*) data;
1171 struct sppp *sp = IFP2SP(ifp);
1172 int rv, going_up, going_down, newmode;
1181 /* set the interface "up" when assigning an IP address */
1182 ifp->if_flags |= IFF_UP;
1186 going_up = ifp->if_flags & IFF_UP &&
1187 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0;
1188 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1189 ifp->if_drv_flags & IFF_DRV_RUNNING;
1191 newmode = ifp->if_flags & IFF_PASSIVE;
1193 newmode = ifp->if_flags & IFF_AUTO;
1195 newmode = ifp->if_flags & IFF_CISCO;
1196 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1197 ifp->if_flags |= newmode;
1200 newmode = sp->pp_flags & PP_FR;
1202 if (newmode != sp->pp_mode) {
1205 going_up = ifp->if_drv_flags & IFF_DRV_RUNNING;
1209 if (sp->pp_mode != IFF_CISCO &&
1210 sp->pp_mode != PP_FR)
1212 else if (sp->pp_tlf)
1214 sppp_flush_unlocked(ifp);
1215 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1216 sp->pp_mode = newmode;
1220 if (sp->pp_mode != IFF_CISCO &&
1221 sp->pp_mode != PP_FR)
1223 sp->pp_mode = newmode;
1224 if (sp->pp_mode == 0) {
1225 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1228 if ((sp->pp_mode == IFF_CISCO) ||
1229 (sp->pp_mode == PP_FR)) {
1232 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1240 #define ifr_mtu ifr_metric
1243 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1245 ifp->if_mtu = ifr->ifr_mtu;
1250 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1252 ifp->if_mtu = *(short*)data;
1257 ifr->ifr_mtu = ifp->if_mtu;
1262 *(short*)data = ifp->if_mtu;
1269 case SIOCGIFGENERIC:
1270 case SIOCSIFGENERIC:
1271 rv = sppp_params(sp, cmd, data);
1282 * Cisco framing implementation.
1286 * Handle incoming Cisco keepalive protocol packets.
1289 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1292 struct cisco_packet *h;
1295 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1298 SPP_FMT "cisco invalid packet length: %d bytes\n",
1299 SPP_ARGS(ifp), m->m_pkthdr.len);
1302 h = mtod (m, struct cisco_packet*);
1305 SPP_FMT "cisco input: %d bytes "
1306 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1307 SPP_ARGS(ifp), m->m_pkthdr.len,
1308 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1309 (u_int)h->time0, (u_int)h->time1);
1310 switch (ntohl (h->type)) {
1313 log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n",
1314 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1316 case CISCO_ADDR_REPLY:
1317 /* Reply on address request, ignore */
1319 case CISCO_KEEPALIVE_REQ:
1320 sp->pp_alivecnt = 0;
1321 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1322 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1323 /* Local and remote sequence numbers are equal.
1324 * Probably, the line is in loopback mode. */
1325 if (sp->pp_loopcnt >= MAXALIVECNT) {
1326 printf (SPP_FMT "loopback\n",
1329 if (ifp->if_flags & IFF_UP) {
1331 sppp_qflush (&sp->pp_cpq);
1336 /* Generate new local sequence number */
1337 sp->pp_seq[IDX_LCP] = random();
1341 if (! (ifp->if_flags & IFF_UP) &&
1342 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1344 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1347 case CISCO_ADDR_REQ:
1348 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1350 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1356 * Send Cisco keepalive packet.
1359 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1362 struct ppp_header *h;
1363 struct cisco_packet *ch;
1367 getmicrouptime(&tv);
1369 MGETHDR (m, M_NOWAIT, MT_DATA);
1372 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1373 m->m_pkthdr.rcvif = 0;
1375 h = mtod (m, struct ppp_header*);
1376 h->address = CISCO_MULTICAST;
1378 h->protocol = htons (CISCO_KEEPALIVE);
1380 ch = (struct cisco_packet*) (h + 1);
1381 ch->type = htonl (type);
1382 ch->par1 = htonl (par1);
1383 ch->par2 = htonl (par2);
1386 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1387 ch->time1 = htons ((u_short) tv.tv_sec);
1391 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1392 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1393 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1395 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1396 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1400 * PPP protocol implementation.
1404 * Send PPP control protocol packet.
1407 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1408 u_char ident, u_short len, void *data)
1411 struct ppp_header *h;
1412 struct lcp_header *lh;
1415 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1416 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1417 MGETHDR (m, M_NOWAIT, MT_DATA);
1420 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1421 m->m_pkthdr.rcvif = 0;
1423 h = mtod (m, struct ppp_header*);
1424 h->address = PPP_ALLSTATIONS; /* broadcast address */
1425 h->control = PPP_UI; /* Unnumbered Info */
1426 h->protocol = htons (proto); /* Link Control Protocol */
1428 lh = (struct lcp_header*) (h + 1);
1431 lh->len = htons (LCP_HEADER_LEN + len);
1433 bcopy (data, lh+1, len);
1436 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1438 sppp_proto_name(proto),
1439 sppp_cp_type_name (lh->type), lh->ident,
1441 sppp_print_bytes ((u_char*) (lh+1), len);
1444 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1445 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1449 * Handle incoming PPP control protocol packets.
1452 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1455 struct lcp_header *h;
1456 int len = m->m_pkthdr.len;
1463 SPP_FMT "%s invalid packet length: %d bytes\n",
1464 SPP_ARGS(ifp), cp->name, len);
1467 h = mtod (m, struct lcp_header*);
1470 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1471 SPP_ARGS(ifp), cp->name,
1472 sppp_state_name(sp->state[cp->protoidx]),
1473 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1474 sppp_print_bytes ((u_char*) (h+1), len-4);
1477 if (len > ntohs (h->len))
1478 len = ntohs (h->len);
1479 p = (u_char *)(h + 1);
1484 log(-1, SPP_FMT "%s invalid conf-req length %d\n",
1485 SPP_ARGS(ifp), cp->name,
1487 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1490 /* handle states where RCR doesn't get a SCA/SCN */
1491 switch (sp->state[cp->protoidx]) {
1493 case STATE_STOPPING:
1496 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1500 rv = (cp->RCR)(sp, h, len);
1501 switch (sp->state[cp->protoidx]) {
1506 case STATE_ACK_SENT:
1507 case STATE_REQ_SENT:
1509 * sppp_cp_change_state() have the side effect of
1510 * restarting the timeouts. We want to avoid that
1511 * if the state don't change, otherwise we won't
1512 * ever timeout and resend a configuration request
1515 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1518 sppp_cp_change_state(cp, sp, rv?
1519 STATE_ACK_SENT: STATE_REQ_SENT);
1522 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1524 sppp_cp_change_state(cp, sp, rv?
1525 STATE_ACK_SENT: STATE_REQ_SENT);
1527 case STATE_ACK_RCVD:
1529 sppp_cp_change_state(cp, sp, STATE_OPENED);
1531 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1536 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1539 printf(SPP_FMT "%s illegal %s in state %s\n",
1540 SPP_ARGS(ifp), cp->name,
1541 sppp_cp_type_name(h->type),
1542 sppp_state_name(sp->state[cp->protoidx]));
1543 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1547 if (h->ident != sp->confid[cp->protoidx]) {
1549 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1550 SPP_ARGS(ifp), cp->name,
1551 h->ident, sp->confid[cp->protoidx]);
1552 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1555 switch (sp->state[cp->protoidx]) {
1558 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1561 case STATE_STOPPING:
1563 case STATE_REQ_SENT:
1564 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1565 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1570 case STATE_ACK_RCVD:
1572 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1574 case STATE_ACK_SENT:
1575 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1576 sppp_cp_change_state(cp, sp, STATE_OPENED);
1578 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1579 SPP_ARGS(ifp), cp->name);
1583 printf(SPP_FMT "%s illegal %s in state %s\n",
1584 SPP_ARGS(ifp), cp->name,
1585 sppp_cp_type_name(h->type),
1586 sppp_state_name(sp->state[cp->protoidx]));
1587 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1592 if (h->ident != sp->confid[cp->protoidx]) {
1594 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1595 SPP_ARGS(ifp), cp->name,
1596 h->ident, sp->confid[cp->protoidx]);
1597 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1600 if (h->type == CONF_NAK)
1601 (cp->RCN_nak)(sp, h, len);
1603 (cp->RCN_rej)(sp, h, len);
1605 switch (sp->state[cp->protoidx]) {
1608 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1610 case STATE_REQ_SENT:
1611 case STATE_ACK_SENT:
1612 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1614 * Slow things down a bit if we think we might be
1615 * in loopback. Depend on the timeout to send the
1616 * next configuration request.
1625 case STATE_ACK_RCVD:
1626 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1630 case STATE_STOPPING:
1633 printf(SPP_FMT "%s illegal %s in state %s\n",
1634 SPP_ARGS(ifp), cp->name,
1635 sppp_cp_type_name(h->type),
1636 sppp_state_name(sp->state[cp->protoidx]));
1637 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1642 switch (sp->state[cp->protoidx]) {
1643 case STATE_ACK_RCVD:
1644 case STATE_ACK_SENT:
1645 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1650 case STATE_STOPPING:
1651 case STATE_REQ_SENT:
1653 /* Send Terminate-Ack packet. */
1655 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1656 SPP_ARGS(ifp), cp->name);
1657 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1661 sp->rst_counter[cp->protoidx] = 0;
1662 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1666 printf(SPP_FMT "%s illegal %s in state %s\n",
1667 SPP_ARGS(ifp), cp->name,
1668 sppp_cp_type_name(h->type),
1669 sppp_state_name(sp->state[cp->protoidx]));
1670 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1674 switch (sp->state[cp->protoidx]) {
1677 case STATE_REQ_SENT:
1678 case STATE_ACK_SENT:
1681 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1684 case STATE_STOPPING:
1685 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1688 case STATE_ACK_RCVD:
1689 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1694 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1697 printf(SPP_FMT "%s illegal %s in state %s\n",
1698 SPP_ARGS(ifp), cp->name,
1699 sppp_cp_type_name(h->type),
1700 sppp_state_name(sp->state[cp->protoidx]));
1701 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1705 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1707 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1708 "danger will robinson\n",
1709 SPP_ARGS(ifp), cp->name,
1710 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1711 switch (sp->state[cp->protoidx]) {
1714 case STATE_REQ_SENT:
1715 case STATE_ACK_SENT:
1717 case STATE_STOPPING:
1720 case STATE_ACK_RCVD:
1721 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1724 printf(SPP_FMT "%s illegal %s in state %s\n",
1725 SPP_ARGS(ifp), cp->name,
1726 sppp_cp_type_name(h->type),
1727 sppp_state_name(sp->state[cp->protoidx]));
1728 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1734 const struct cp *upper;
1740 proto = ntohs(*((u_int16_t *)p));
1741 for (i = 0; i < IDX_COUNT; i++) {
1742 if (cps[i]->proto == proto) {
1750 if (catastrophic || debug)
1751 log(catastrophic? LOG_INFO: LOG_DEBUG,
1752 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1753 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1754 sppp_cp_type_name(h->type), proto,
1755 upper ? upper->name : "unknown",
1756 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1759 * if we got RXJ+ against conf-req, the peer does not implement
1760 * this particular protocol type. terminate the protocol.
1762 if (upper && !catastrophic) {
1763 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1769 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1770 switch (sp->state[cp->protoidx]) {
1773 case STATE_REQ_SENT:
1774 case STATE_ACK_SENT:
1776 case STATE_STOPPING:
1779 case STATE_ACK_RCVD:
1780 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1783 printf(SPP_FMT "%s illegal %s in state %s\n",
1784 SPP_ARGS(ifp), cp->name,
1785 sppp_cp_type_name(h->type),
1786 sppp_state_name(sp->state[cp->protoidx]));
1787 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1792 if (cp->proto != PPP_LCP)
1794 /* Discard the packet. */
1797 if (cp->proto != PPP_LCP)
1799 if (sp->state[cp->protoidx] != STATE_OPENED) {
1801 log(-1, SPP_FMT "lcp echo req but lcp closed\n",
1803 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1808 log(-1, SPP_FMT "invalid lcp echo request "
1809 "packet length: %d bytes\n",
1810 SPP_ARGS(ifp), len);
1813 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1814 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1815 /* Line loopback mode detected. */
1816 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1817 sp->pp_loopcnt = MAXALIVECNT * 5;
1819 sppp_qflush (&sp->pp_cpq);
1821 /* Shut down the PPP link. */
1827 *(long*)(h+1) = htonl (sp->lcp.magic);
1829 log(-1, SPP_FMT "got lcp echo req, sending echo rep\n",
1831 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1834 if (cp->proto != PPP_LCP)
1836 if (h->ident != sp->lcp.echoid) {
1837 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1842 log(-1, SPP_FMT "lcp invalid echo reply "
1843 "packet length: %d bytes\n",
1844 SPP_ARGS(ifp), len);
1848 log(-1, SPP_FMT "lcp got echo rep\n",
1850 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1851 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1852 sp->pp_alivecnt = 0;
1855 /* Unknown packet type -- send Code-Reject packet. */
1858 log(-1, SPP_FMT "%s send code-rej for 0x%x\n",
1859 SPP_ARGS(ifp), cp->name, h->type);
1860 sppp_cp_send(sp, cp->proto, CODE_REJ,
1861 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1862 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1867 * The generic part of all Up/Down/Open/Close/TO event handlers.
1868 * Basically, the state transition handling in the automaton.
1871 sppp_up_event(const struct cp *cp, struct sppp *sp)
1876 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1877 SPP_ARGS(ifp), cp->name,
1878 sppp_state_name(sp->state[cp->protoidx]));
1880 switch (sp->state[cp->protoidx]) {
1882 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1884 case STATE_STARTING:
1885 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1887 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1890 printf(SPP_FMT "%s illegal up in state %s\n",
1891 SPP_ARGS(ifp), cp->name,
1892 sppp_state_name(sp->state[cp->protoidx]));
1897 sppp_down_event(const struct cp *cp, struct sppp *sp)
1902 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1903 SPP_ARGS(ifp), cp->name,
1904 sppp_state_name(sp->state[cp->protoidx]));
1906 switch (sp->state[cp->protoidx]) {
1909 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1912 sppp_cp_change_state(cp, sp, STATE_STARTING);
1915 case STATE_STOPPING:
1916 case STATE_REQ_SENT:
1917 case STATE_ACK_RCVD:
1918 case STATE_ACK_SENT:
1919 sppp_cp_change_state(cp, sp, STATE_STARTING);
1923 sppp_cp_change_state(cp, sp, STATE_STARTING);
1926 printf(SPP_FMT "%s illegal down in state %s\n",
1927 SPP_ARGS(ifp), cp->name,
1928 sppp_state_name(sp->state[cp->protoidx]));
1933 sppp_open_event(const struct cp *cp, struct sppp *sp)
1938 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1939 SPP_ARGS(ifp), cp->name,
1940 sppp_state_name(sp->state[cp->protoidx]));
1942 switch (sp->state[cp->protoidx]) {
1944 sppp_cp_change_state(cp, sp, STATE_STARTING);
1947 case STATE_STARTING:
1950 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1952 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1956 * Try escaping stopped state. This seems to bite
1957 * people occasionally, in particular for IPCP,
1958 * presumably following previous IPCP negotiation
1959 * aborts. Somehow, we must have missed a Down event
1960 * which would have caused a transition into starting
1961 * state, so as a bandaid we force the Down event now.
1962 * This effectively implements (something like the)
1963 * `restart' option mentioned in the state transition
1964 * table of RFC 1661.
1966 sppp_cp_change_state(cp, sp, STATE_STARTING);
1969 case STATE_STOPPING:
1970 case STATE_REQ_SENT:
1971 case STATE_ACK_RCVD:
1972 case STATE_ACK_SENT:
1976 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1982 sppp_close_event(const struct cp *cp, struct sppp *sp)
1987 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
1988 SPP_ARGS(ifp), cp->name,
1989 sppp_state_name(sp->state[cp->protoidx]));
1991 switch (sp->state[cp->protoidx]) {
1996 case STATE_STARTING:
1997 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2001 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2003 case STATE_STOPPING:
2004 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2009 case STATE_REQ_SENT:
2010 case STATE_ACK_RCVD:
2011 case STATE_ACK_SENT:
2012 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2013 sppp_cp_send(sp, cp->proto, TERM_REQ,
2014 ++sp->pp_seq[cp->protoidx], 0, 0);
2015 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2021 sppp_to_event(const struct cp *cp, struct sppp *sp)
2027 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
2028 SPP_ARGS(ifp), cp->name,
2029 sppp_state_name(sp->state[cp->protoidx]),
2030 sp->rst_counter[cp->protoidx]);
2032 if (--sp->rst_counter[cp->protoidx] < 0)
2034 switch (sp->state[cp->protoidx]) {
2036 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2039 case STATE_STOPPING:
2040 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2043 case STATE_REQ_SENT:
2044 case STATE_ACK_RCVD:
2045 case STATE_ACK_SENT:
2046 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2052 switch (sp->state[cp->protoidx]) {
2054 case STATE_STOPPING:
2055 sppp_cp_send(sp, cp->proto, TERM_REQ,
2056 ++sp->pp_seq[cp->protoidx], 0, 0);
2057 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2058 cp->TO, (void *)sp);
2060 case STATE_REQ_SENT:
2061 case STATE_ACK_RCVD:
2063 /* sppp_cp_change_state() will restart the timer */
2064 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2066 case STATE_ACK_SENT:
2068 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2069 cp->TO, (void *)sp);
2077 * Change the state of a control protocol in the state automaton.
2078 * Takes care of starting/stopping the restart timer.
2081 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2083 sp->state[cp->protoidx] = newstate;
2085 callout_stop (&sp->ch[cp->protoidx]);
2089 case STATE_STARTING:
2095 case STATE_STOPPING:
2096 case STATE_REQ_SENT:
2097 case STATE_ACK_RCVD:
2098 case STATE_ACK_SENT:
2099 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2100 cp->TO, (void *)sp);
2106 *--------------------------------------------------------------------------*
2108 * The LCP implementation. *
2110 *--------------------------------------------------------------------------*
2113 sppp_pp_up(struct sppp *sp)
2121 sppp_pp_down(struct sppp *sp)
2129 sppp_lcp_init(struct sppp *sp)
2131 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2133 sp->state[IDX_LCP] = STATE_INITIAL;
2134 sp->fail_counter[IDX_LCP] = 0;
2135 sp->pp_seq[IDX_LCP] = 0;
2136 sp->pp_rseq[IDX_LCP] = 0;
2138 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2140 /* Note that these values are relevant for all control protocols */
2141 sp->lcp.timeout = 3 * hz;
2142 sp->lcp.max_terminate = 2;
2143 sp->lcp.max_configure = 10;
2144 sp->lcp.max_failure = 10;
2145 callout_init(&sp->ch[IDX_LCP], 1);
2149 sppp_lcp_up(struct sppp *sp)
2153 sp->pp_alivecnt = 0;
2154 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2157 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2159 * If we are authenticator, negotiate LCP_AUTH
2161 if (sp->hisauth.proto != 0)
2162 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2164 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2165 sp->pp_flags &= ~PP_NEEDAUTH;
2167 * If this interface is passive or dial-on-demand, and we are
2168 * still in Initial state, it means we've got an incoming
2169 * call. Activate the interface.
2171 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2174 SPP_FMT "Up event", SPP_ARGS(ifp));
2175 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2176 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2178 log(-1, "(incoming call)\n");
2179 sp->pp_flags |= PP_CALLIN;
2183 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2184 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2185 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2189 sppp_up_event(&lcp, sp);
2193 sppp_lcp_down(struct sppp *sp)
2197 sppp_down_event(&lcp, sp);
2200 * If this is neither a dial-on-demand nor a passive
2201 * interface, simulate an ``ifconfig down'' action, so the
2202 * administrator can force a redial by another ``ifconfig
2203 * up''. XXX For leased line operation, should we immediately
2204 * try to reopen the connection here?
2206 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2208 SPP_FMT "Down event, taking interface down.\n",
2214 SPP_FMT "Down event (carrier loss)\n",
2216 sp->pp_flags &= ~PP_CALLIN;
2217 if (sp->state[IDX_LCP] != STATE_INITIAL)
2219 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2224 sppp_lcp_open(struct sppp *sp)
2226 sppp_open_event(&lcp, sp);
2230 sppp_lcp_close(struct sppp *sp)
2232 sppp_close_event(&lcp, sp);
2236 sppp_lcp_TO(void *cookie)
2238 sppp_to_event(&lcp, (struct sppp *)cookie);
2242 * Analyze a configure request. Return true if it was agreeable, and
2243 * caused action sca, false if it has been rejected or nak'ed, and
2244 * caused action scn. (The return value is used to make the state
2245 * transition decision in the state automaton.)
2248 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2251 u_char *buf, *r, *p;
2258 buf = r = malloc (len, M_TEMP, M_NOWAIT);
2263 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2266 /* pass 1: check for things that need to be rejected */
2268 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2269 len-=p[1], p+=p[1]) {
2271 log(-1, " %s ", sppp_lcp_opt_name(*p));
2275 if (len >= 6 && p[1] == 6)
2278 log(-1, "[invalid] ");
2280 case LCP_OPT_ASYNC_MAP:
2281 /* Async control character map. */
2282 if (len >= 6 && p[1] == 6)
2285 log(-1, "[invalid] ");
2288 /* Maximum receive unit. */
2289 if (len >= 4 && p[1] == 4)
2292 log(-1, "[invalid] ");
2294 case LCP_OPT_AUTH_PROTO:
2297 log(-1, "[invalid] ");
2300 authproto = (p[2] << 8) + p[3];
2301 if (authproto == PPP_CHAP && p[1] != 5) {
2303 log(-1, "[invalid chap len] ");
2306 if (sp->myauth.proto == 0) {
2307 /* we are not configured to do auth */
2309 log(-1, "[not configured] ");
2313 * Remote want us to authenticate, remember this,
2314 * so we stay in PHASE_AUTHENTICATE after LCP got
2317 sp->pp_flags |= PP_NEEDAUTH;
2320 /* Others not supported. */
2325 /* Add the option to rejected list. */
2332 log(-1, " send conf-rej\n");
2333 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2339 * pass 2: check for option values that are unacceptable and
2340 * thus require to be nak'ed.
2343 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2348 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2349 len-=p[1], p+=p[1]) {
2351 log(-1, " %s ", sppp_lcp_opt_name(*p));
2354 /* Magic number -- extract. */
2355 nmagic = (u_long)p[2] << 24 |
2356 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2357 if (nmagic != sp->lcp.magic) {
2360 log(-1, "0x%lx ", nmagic);
2363 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2364 log(-1, "[glitch] ");
2367 * We negate our magic here, and NAK it. If
2368 * we see it later in an NAK packet, we
2369 * suggest a new one.
2371 nmagic = ~sp->lcp.magic;
2373 p[2] = nmagic >> 24;
2374 p[3] = nmagic >> 16;
2379 case LCP_OPT_ASYNC_MAP:
2381 * Async control character map -- just ignore it.
2383 * Quote from RFC 1662, chapter 6:
2384 * To enable this functionality, synchronous PPP
2385 * implementations MUST always respond to the
2386 * Async-Control-Character-Map Configuration
2387 * Option with the LCP Configure-Ack. However,
2388 * acceptance of the Configuration Option does
2389 * not imply that the synchronous implementation
2390 * will do any ACCM mapping. Instead, all such
2391 * octet mapping will be performed by the
2392 * asynchronous-to-synchronous converter.
2398 * Maximum receive unit. Always agreeable,
2399 * but ignored by now.
2401 sp->lcp.their_mru = p[2] * 256 + p[3];
2403 log(-1, "%lu ", sp->lcp.their_mru);
2406 case LCP_OPT_AUTH_PROTO:
2407 authproto = (p[2] << 8) + p[3];
2408 if (sp->myauth.proto != authproto) {
2409 /* not agreed, nak */
2411 log(-1, "[mine %s != his %s] ",
2412 sppp_proto_name(sp->hisauth.proto),
2413 sppp_proto_name(authproto));
2414 p[2] = sp->myauth.proto >> 8;
2415 p[3] = sp->myauth.proto;
2418 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2420 log(-1, "[chap not MD5] ");
2426 /* Add the option to nak'ed list. */
2433 * Local and remote magics equal -- loopback?
2435 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2436 if (sp->pp_loopcnt == MAXALIVECNT*5)
2437 printf (SPP_FMT "loopback\n",
2439 if (ifp->if_flags & IFF_UP) {
2441 sppp_qflush(&sp->pp_cpq);
2446 } else if (!sp->pp_loopcnt &&
2447 ++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2449 log(-1, " max_failure (%d) exceeded, "
2451 sp->lcp.max_failure);
2452 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2455 log(-1, " send conf-nak\n");
2456 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2460 log(-1, " send conf-ack\n");
2461 sp->fail_counter[IDX_LCP] = 0;
2463 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2464 h->ident, origlen, h+1);
2472 * Analyze the LCP Configure-Reject option list, and adjust our
2476 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2482 buf = malloc (len, M_TEMP, M_NOWAIT);
2487 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2491 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2492 len -= p[1], p += p[1]) {
2494 log(-1, " %s ", sppp_lcp_opt_name(*p));
2497 /* Magic number -- can't use it, use 0 */
2498 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2503 * Should not be rejected anyway, since we only
2504 * negotiate a MRU if explicitly requested by
2507 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2509 case LCP_OPT_AUTH_PROTO:
2511 * Peer doesn't want to authenticate himself,
2512 * deny unless this is a dialout call, and
2513 * AUTHFLAG_NOCALLOUT is set.
2515 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2516 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2518 log(-1, "[don't insist on auth "
2520 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2524 log(-1, "[access denied]\n");
2536 * Analyze the LCP Configure-NAK option list, and adjust our
2540 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2547 buf = malloc (len, M_TEMP, M_NOWAIT);
2552 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2556 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2557 len -= p[1], p += p[1]) {
2559 log(-1, " %s ", sppp_lcp_opt_name(*p));
2562 /* Magic number -- renegotiate */
2563 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2564 len >= 6 && p[1] == 6) {
2565 magic = (u_long)p[2] << 24 |
2566 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2568 * If the remote magic is our negated one,
2569 * this looks like a loopback problem.
2570 * Suggest a new magic to make sure.
2572 if (magic == ~sp->lcp.magic) {
2574 log(-1, "magic glitch ");
2575 sp->lcp.magic = random();
2577 sp->lcp.magic = magic;
2579 log(-1, "%lu ", magic);
2585 * Peer wants to advise us to negotiate an MRU.
2586 * Agree on it if it's reasonable, or use
2587 * default otherwise.
2589 if (len >= 4 && p[1] == 4) {
2590 u_int mru = p[2] * 256 + p[3];
2592 log(-1, "%d ", mru);
2593 if (mru < PP_MTU || mru > PP_MAX_MRU)
2596 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2599 case LCP_OPT_AUTH_PROTO:
2601 * Peer doesn't like our authentication method,
2605 log(-1, "[access denied]\n");
2617 sppp_lcp_tlu(struct sppp *sp)
2624 if (! (ifp->if_flags & IFF_UP) &&
2625 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2626 /* Coming out of loopback mode. */
2628 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2631 for (i = 0; i < IDX_COUNT; i++)
2632 if ((cps[i])->flags & CP_QUAL)
2635 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2636 (sp->pp_flags & PP_NEEDAUTH) != 0)
2637 sp->pp_phase = PHASE_AUTHENTICATE;
2639 sp->pp_phase = PHASE_NETWORK;
2642 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2643 sppp_phase_name(sp->pp_phase));
2646 * Open all authentication protocols. This is even required
2647 * if we already proceeded to network phase, since it might be
2648 * that remote wants us to authenticate, so we might have to
2649 * send a PAP request. Undesired authentication protocols
2650 * don't do anything when they get an Open event.
2652 for (i = 0; i < IDX_COUNT; i++)
2653 if ((cps[i])->flags & CP_AUTH)
2656 if (sp->pp_phase == PHASE_NETWORK) {
2657 /* Notify all NCPs. */
2658 for (i = 0; i < IDX_COUNT; i++)
2659 if (((cps[i])->flags & CP_NCP) &&
2662 * Hack to administratively disable IPv6 if
2663 * not desired. Perhaps we should have another
2664 * flag for this, but right now, we can make
2665 * all struct cp's read/only.
2667 (cps[i] != &ipv6cp ||
2668 (sp->confflags & CONF_ENABLE_IPV6)))
2672 /* Send Up events to all started protos. */
2673 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2674 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2677 /* notify low-level driver of state change */
2679 sp->pp_chg(sp, (int)sp->pp_phase);
2681 if (sp->pp_phase == PHASE_NETWORK)
2682 /* if no NCP is starting, close down */
2683 sppp_lcp_check_and_close(sp);
2687 sppp_lcp_tld(struct sppp *sp)
2693 sp->pp_phase = PHASE_TERMINATE;
2696 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2697 sppp_phase_name(sp->pp_phase));
2700 * Take upper layers down. We send the Down event first and
2701 * the Close second to prevent the upper layers from sending
2702 * ``a flurry of terminate-request packets'', as the RFC
2705 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2706 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2708 (cps[i])->Close(sp);
2713 sppp_lcp_tls(struct sppp *sp)
2717 sp->pp_phase = PHASE_ESTABLISH;
2720 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2721 sppp_phase_name(sp->pp_phase));
2723 /* Notify lower layer if desired. */
2731 sppp_lcp_tlf(struct sppp *sp)
2735 sp->pp_phase = PHASE_DEAD;
2737 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2738 sppp_phase_name(sp->pp_phase));
2740 /* Notify lower layer if desired. */
2748 sppp_lcp_scr(struct sppp *sp)
2750 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2754 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2755 if (! sp->lcp.magic)
2756 sp->lcp.magic = random();
2757 opt[i++] = LCP_OPT_MAGIC;
2759 opt[i++] = sp->lcp.magic >> 24;
2760 opt[i++] = sp->lcp.magic >> 16;
2761 opt[i++] = sp->lcp.magic >> 8;
2762 opt[i++] = sp->lcp.magic;
2765 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2766 opt[i++] = LCP_OPT_MRU;
2768 opt[i++] = sp->lcp.mru >> 8;
2769 opt[i++] = sp->lcp.mru;
2772 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2773 authproto = sp->hisauth.proto;
2774 opt[i++] = LCP_OPT_AUTH_PROTO;
2775 opt[i++] = authproto == PPP_CHAP? 5: 4;
2776 opt[i++] = authproto >> 8;
2777 opt[i++] = authproto;
2778 if (authproto == PPP_CHAP)
2779 opt[i++] = CHAP_MD5;
2782 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2783 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2787 * Check the open NCPs, return true if at least one NCP is open.
2790 sppp_ncp_check(struct sppp *sp)
2794 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2795 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2801 * Re-check the open NCPs and see if we should terminate the link.
2802 * Called by the NCPs during their tlf action handling.
2805 sppp_lcp_check_and_close(struct sppp *sp)
2808 if (sp->pp_phase < PHASE_NETWORK)
2809 /* don't bother, we are already going down */
2812 if (sppp_ncp_check(sp))
2819 *--------------------------------------------------------------------------*
2821 * The IPCP implementation. *
2823 *--------------------------------------------------------------------------*
2828 sppp_ipcp_init(struct sppp *sp)
2832 sp->state[IDX_IPCP] = STATE_INITIAL;
2833 sp->fail_counter[IDX_IPCP] = 0;
2834 sp->pp_seq[IDX_IPCP] = 0;
2835 sp->pp_rseq[IDX_IPCP] = 0;
2836 callout_init(&sp->ch[IDX_IPCP], 1);
2840 sppp_ipcp_up(struct sppp *sp)
2842 sppp_up_event(&ipcp, sp);
2846 sppp_ipcp_down(struct sppp *sp)
2848 sppp_down_event(&ipcp, sp);
2852 sppp_ipcp_open(struct sppp *sp)
2855 u_long myaddr, hisaddr;
2857 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2858 IPCP_MYADDR_DYN | IPCP_VJ);
2861 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2863 * If we don't have his address, this probably means our
2864 * interface doesn't want to talk IP at all. (This could
2865 * be the case if somebody wants to speak only IPX, for
2866 * example.) Don't open IPCP in this case.
2868 if (hisaddr == 0L) {
2869 /* XXX this message should go away */
2871 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2877 * I don't have an assigned address, so i need to
2878 * negotiate my address.
2880 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2881 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2883 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2884 if (sp->confflags & CONF_ENABLE_VJ) {
2885 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2886 sp->ipcp.max_state = MAX_STATES - 1;
2887 sp->ipcp.compress_cid = 1;
2889 sppp_open_event(&ipcp, sp);
2893 sppp_ipcp_close(struct sppp *sp)
2895 sppp_close_event(&ipcp, sp);
2896 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2898 * My address was dynamic, clear it again.
2900 sppp_set_ip_addr(sp, 0L);
2904 sppp_ipcp_TO(void *cookie)
2906 sppp_to_event(&ipcp, (struct sppp *)cookie);
2910 * Analyze a configure request. Return true if it was agreeable, and
2911 * caused action sca, false if it has been rejected or nak'ed, and
2912 * caused action scn. (The return value is used to make the state
2913 * transition decision in the state automaton.)
2916 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2918 u_char *buf, *r, *p;
2919 struct ifnet *ifp = SP2IFP(sp);
2920 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2921 u_long hisaddr, desiredaddr;
2928 * Make sure to allocate a buf that can at least hold a
2929 * conf-nak with an `address' option. We might need it below.
2931 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2935 /* pass 1: see if we can recognize them */
2937 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2940 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2941 len-=p[1], p+=p[1]) {
2943 log(-1, " %s ", sppp_ipcp_opt_name(*p));
2945 case IPCP_OPT_COMPRESSION:
2946 if (!(sp->confflags & CONF_ENABLE_VJ)) {
2947 /* VJ compression administratively disabled */
2949 log(-1, "[locally disabled] ");
2953 * In theory, we should only conf-rej an
2954 * option that is shorter than RFC 1618
2955 * requires (i.e. < 4), and should conf-nak
2956 * anything else that is not VJ. However,
2957 * since our algorithm always uses the
2958 * original option to NAK it with new values,
2959 * things would become more complicated. In
2960 * practice, the only commonly implemented IP
2961 * compression option is VJ anyway, so the
2962 * difference is negligible.
2964 if (len >= 6 && p[1] == 6) {
2966 * correctly formed compression option
2967 * that could be VJ compression
2973 "optlen %d [invalid/unsupported] ",
2976 case IPCP_OPT_ADDRESS:
2977 if (len >= 6 && p[1] == 6) {
2978 /* correctly formed address option */
2982 log(-1, "[invalid] ");
2985 /* Others not supported. */
2990 /* Add the option to rejected list. */
2997 log(-1, " send conf-rej\n");
2998 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
3003 /* pass 2: parse option values */
3004 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
3006 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
3010 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3011 len-=p[1], p+=p[1]) {
3013 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3015 case IPCP_OPT_COMPRESSION:
3016 desiredcomp = p[2] << 8 | p[3];
3017 /* We only support VJ */
3018 if (desiredcomp == IPCP_COMP_VJ) {
3020 log(-1, "VJ [ack] ");
3021 sp->ipcp.flags |= IPCP_VJ;
3022 sl_compress_init(sp->pp_comp, p[4]);
3023 sp->ipcp.max_state = p[4];
3024 sp->ipcp.compress_cid = p[5];
3029 "compproto %#04x [not supported] ",
3031 p[2] = IPCP_COMP_VJ >> 8;
3032 p[3] = IPCP_COMP_VJ;
3033 p[4] = sp->ipcp.max_state;
3034 p[5] = sp->ipcp.compress_cid;
3036 case IPCP_OPT_ADDRESS:
3037 /* This is the address he wants in his end */
3038 desiredaddr = p[2] << 24 | p[3] << 16 |
3040 if (desiredaddr == hisaddr ||
3041 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3043 * Peer's address is same as our value,
3044 * or we have set it to 0.0.0.* to
3045 * indicate that we do not really care,
3046 * this is agreeable. Gonna conf-ack
3050 log(-1, "%s [ack] ",
3051 sppp_dotted_quad(hisaddr));
3052 /* record that we've seen it already */
3053 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3057 * The address wasn't agreeable. This is either
3058 * he sent us 0.0.0.0, asking to assign him an
3059 * address, or he send us another address not
3060 * matching our value. Either case, we gonna
3061 * conf-nak it with our value.
3062 * XXX: we should "rej" if hisaddr == 0
3065 if (desiredaddr == 0)
3066 log(-1, "[addr requested] ");
3068 log(-1, "%s [not agreed] ",
3069 sppp_dotted_quad(desiredaddr));
3071 p[2] = hisaddr >> 24;
3072 p[3] = hisaddr >> 16;
3073 p[4] = hisaddr >> 8;
3077 /* Add the option to nak'ed list. */
3084 * If we are about to conf-ack the request, but haven't seen
3085 * his address so far, gonna conf-nak it instead, with the
3086 * `address' option present and our idea of his address being
3087 * filled in there, to request negotiation of both addresses.
3089 * XXX This can result in an endless req - nak loop if peer
3090 * doesn't want to send us his address. Q: What should we do
3091 * about it? XXX A: implement the max-failure counter.
3093 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3094 buf[0] = IPCP_OPT_ADDRESS;
3096 buf[2] = hisaddr >> 24;
3097 buf[3] = hisaddr >> 16;
3098 buf[4] = hisaddr >> 8;
3102 log(-1, "still need hisaddr ");
3107 log(-1, " send conf-nak\n");
3108 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3111 log(-1, " send conf-ack\n");
3112 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3113 h->ident, origlen, h+1);
3121 * Analyze the IPCP Configure-Reject option list, and adjust our
3125 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3128 struct ifnet *ifp = SP2IFP(sp);
3129 int debug = ifp->if_flags & IFF_DEBUG;
3132 buf = malloc (len, M_TEMP, M_NOWAIT);
3137 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3141 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3142 len -= p[1], p += p[1]) {
3144 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3146 case IPCP_OPT_COMPRESSION:
3147 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3149 case IPCP_OPT_ADDRESS:
3151 * Peer doesn't grok address option. This is
3152 * bad. XXX Should we better give up here?
3153 * XXX We could try old "addresses" option...
3155 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3166 * Analyze the IPCP Configure-NAK option list, and adjust our
3170 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3173 struct ifnet *ifp = SP2IFP(sp);
3174 int debug = ifp->if_flags & IFF_DEBUG;
3179 buf = malloc (len, M_TEMP, M_NOWAIT);
3184 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3188 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3189 len -= p[1], p += p[1]) {
3191 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3193 case IPCP_OPT_COMPRESSION:
3194 if (len >= 6 && p[1] == 6) {
3195 desiredcomp = p[2] << 8 | p[3];
3197 log(-1, "[wantcomp %#04x] ",
3199 if (desiredcomp == IPCP_COMP_VJ) {
3200 sl_compress_init(sp->pp_comp, p[4]);
3201 sp->ipcp.max_state = p[4];
3202 sp->ipcp.compress_cid = p[5];
3204 log(-1, "[agree] ");
3207 ~(1 << IPCP_OPT_COMPRESSION);
3210 case IPCP_OPT_ADDRESS:
3212 * Peer doesn't like our local IP address. See
3213 * if we can do something for him. We'll drop
3214 * him our address then.
3216 if (len >= 6 && p[1] == 6) {
3217 wantaddr = p[2] << 24 | p[3] << 16 |
3219 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3221 log(-1, "[wantaddr %s] ",
3222 sppp_dotted_quad(wantaddr));
3224 * When doing dynamic address assignment,
3225 * we accept his offer. Otherwise, we
3226 * ignore it and thus continue to negotiate
3227 * our already existing value.
3228 * XXX: Bogus, if he said no once, he'll
3229 * just say no again, might as well die.
3231 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3232 sppp_set_ip_addr(sp, wantaddr);
3234 log(-1, "[agree] ");
3235 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3248 sppp_ipcp_tlu(struct sppp *sp)
3250 /* we are up - notify isdn daemon */
3256 sppp_ipcp_tld(struct sppp *sp)
3261 sppp_ipcp_tls(struct sppp *sp)
3263 /* indicate to LCP that it must stay alive */
3264 sp->lcp.protos |= (1 << IDX_IPCP);
3268 sppp_ipcp_tlf(struct sppp *sp)
3270 /* we no longer need LCP */
3271 sp->lcp.protos &= ~(1 << IDX_IPCP);
3272 sppp_lcp_check_and_close(sp);
3276 sppp_ipcp_scr(struct sppp *sp)
3278 char opt[6 /* compression */ + 6 /* address */];
3282 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3283 opt[i++] = IPCP_OPT_COMPRESSION;
3285 opt[i++] = IPCP_COMP_VJ >> 8;
3286 opt[i++] = IPCP_COMP_VJ;
3287 opt[i++] = sp->ipcp.max_state;
3288 opt[i++] = sp->ipcp.compress_cid;
3290 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3291 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3292 opt[i++] = IPCP_OPT_ADDRESS;
3294 opt[i++] = ouraddr >> 24;
3295 opt[i++] = ouraddr >> 16;
3296 opt[i++] = ouraddr >> 8;
3300 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3301 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3305 sppp_ipcp_init(struct sppp *sp)
3310 sppp_ipcp_up(struct sppp *sp)
3315 sppp_ipcp_down(struct sppp *sp)
3320 sppp_ipcp_open(struct sppp *sp)
3325 sppp_ipcp_close(struct sppp *sp)
3330 sppp_ipcp_TO(void *cookie)
3335 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3341 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3346 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3351 sppp_ipcp_tlu(struct sppp *sp)
3356 sppp_ipcp_tld(struct sppp *sp)
3361 sppp_ipcp_tls(struct sppp *sp)
3366 sppp_ipcp_tlf(struct sppp *sp)
3371 sppp_ipcp_scr(struct sppp *sp)
3377 *--------------------------------------------------------------------------*
3379 * The IPv6CP implementation. *
3381 *--------------------------------------------------------------------------*
3386 sppp_ipv6cp_init(struct sppp *sp)
3388 sp->ipv6cp.opts = 0;
3389 sp->ipv6cp.flags = 0;
3390 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3391 sp->fail_counter[IDX_IPV6CP] = 0;
3392 sp->pp_seq[IDX_IPV6CP] = 0;
3393 sp->pp_rseq[IDX_IPV6CP] = 0;
3394 callout_init(&sp->ch[IDX_IPV6CP], 1);
3398 sppp_ipv6cp_up(struct sppp *sp)
3400 sppp_up_event(&ipv6cp, sp);
3404 sppp_ipv6cp_down(struct sppp *sp)
3406 sppp_down_event(&ipv6cp, sp);
3410 sppp_ipv6cp_open(struct sppp *sp)
3413 struct in6_addr myaddr, hisaddr;
3415 #ifdef IPV6CP_MYIFID_DYN
3416 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3418 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3421 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3423 * If we don't have our address, this probably means our
3424 * interface doesn't want to talk IPv6 at all. (This could
3425 * be the case if somebody wants to speak only IPX, for
3426 * example.) Don't open IPv6CP in this case.
3428 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3429 /* XXX this message should go away */
3431 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3436 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3437 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3438 sppp_open_event(&ipv6cp, sp);
3442 sppp_ipv6cp_close(struct sppp *sp)
3444 sppp_close_event(&ipv6cp, sp);
3448 sppp_ipv6cp_TO(void *cookie)
3450 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3454 * Analyze a configure request. Return true if it was agreeable, and
3455 * caused action sca, false if it has been rejected or nak'ed, and
3456 * caused action scn. (The return value is used to make the state
3457 * transition decision in the state automaton.)
3460 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3462 u_char *buf, *r, *p;
3463 struct ifnet *ifp = SP2IFP(sp);
3464 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3465 struct in6_addr myaddr, desiredaddr, suggestaddr;
3468 int collision, nohisaddr;
3469 char ip6buf[INET6_ADDRSTRLEN];
3474 * Make sure to allocate a buf that can at least hold a
3475 * conf-nak with an `address' option. We might need it below.
3477 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3481 /* pass 1: see if we can recognize them */
3483 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3487 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3488 len-=p[1], p+=p[1]) {
3490 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3492 case IPV6CP_OPT_IFID:
3493 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3494 /* correctly formed address option */
3499 log(-1, " [invalid]");
3502 case IPV6CP_OPT_COMPRESSION:
3503 if (len >= 4 && p[1] >= 4) {
3504 /* correctly formed compress option */
3508 log(-1, " [invalid]");
3512 /* Others not supported. */
3517 /* Add the option to rejected list. */
3524 log(-1, " send conf-rej\n");
3525 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3530 /* pass 2: parse option values */
3531 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3533 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3538 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3539 len-=p[1], p+=p[1]) {
3541 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3544 case IPV6CP_OPT_COMPRESSION:
3547 case IPV6CP_OPT_IFID:
3548 bzero(&desiredaddr, sizeof(desiredaddr));
3549 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3550 collision = (bcmp(&desiredaddr.s6_addr[8],
3551 &myaddr.s6_addr[8], 8) == 0);
3552 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3554 desiredaddr.s6_addr16[0] = htons(0xfe80);
3555 (void)in6_setscope(&desiredaddr, SP2IFP(sp), NULL);
3557 if (!collision && !nohisaddr) {
3558 /* no collision, hisaddr known - Conf-Ack */
3563 ip6_sprintf(ip6buf, &desiredaddr),
3564 sppp_cp_type_name(type));
3569 bzero(&suggestaddr, sizeof(suggestaddr));
3570 if (collision && nohisaddr) {
3571 /* collision, hisaddr unknown - Conf-Rej */
3576 * - no collision, hisaddr unknown, or
3577 * - collision, hisaddr known
3578 * Conf-Nak, suggest hisaddr
3581 sppp_suggest_ip6_addr(sp, &suggestaddr);
3582 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3586 ip6_sprintf(ip6buf, &desiredaddr),
3587 sppp_cp_type_name(type));
3590 /* Add the option to nak'ed list. */
3596 if (rlen == 0 && type == CONF_ACK) {
3598 log(-1, " send %s\n", sppp_cp_type_name(type));
3599 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3602 if (type == CONF_ACK)
3603 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3607 log(-1, " send %s suggest %s\n",
3608 sppp_cp_type_name(type),
3609 ip6_sprintf(ip6buf, &suggestaddr));
3611 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3620 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3624 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3627 struct ifnet *ifp = SP2IFP(sp);
3628 int debug = ifp->if_flags & IFF_DEBUG;
3631 buf = malloc (len, M_TEMP, M_NOWAIT);
3636 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3640 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3641 len -= p[1], p += p[1]) {
3643 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3645 case IPV6CP_OPT_IFID:
3647 * Peer doesn't grok address option. This is
3648 * bad. XXX Should we better give up here?
3650 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3653 case IPV6CP_OPT_COMPRESS:
3654 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3666 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3670 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3673 struct ifnet *ifp = SP2IFP(sp);
3674 int debug = ifp->if_flags & IFF_DEBUG;
3675 struct in6_addr suggestaddr;
3676 char ip6buf[INET6_ADDRSTRLEN];
3679 buf = malloc (len, M_TEMP, M_NOWAIT);
3684 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3688 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3689 len -= p[1], p += p[1]) {
3691 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3693 case IPV6CP_OPT_IFID:
3695 * Peer doesn't like our local ifid. See
3696 * if we can do something for him. We'll drop
3697 * him our address then.
3699 if (len < 10 || p[1] != 10)
3701 bzero(&suggestaddr, sizeof(suggestaddr));
3702 suggestaddr.s6_addr16[0] = htons(0xfe80);
3703 (void)in6_setscope(&suggestaddr, SP2IFP(sp), NULL);
3704 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3706 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3708 log(-1, " [suggestaddr %s]",
3709 ip6_sprintf(ip6buf, &suggestaddr));
3710 #ifdef IPV6CP_MYIFID_DYN
3712 * When doing dynamic address assignment,
3713 * we accept his offer.
3715 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3716 struct in6_addr lastsuggest;
3718 * If <suggested myaddr from peer> equals to
3719 * <hisaddr we have suggested last time>,
3720 * we have a collision. generate new random
3723 sppp_suggest_ip6_addr(&lastsuggest);
3724 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3727 log(-1, " [random]");
3728 sppp_gen_ip6_addr(sp, &suggestaddr);
3730 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3732 log(-1, " [agree]");
3733 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3737 * Since we do not do dynamic address assignment,
3738 * we ignore it and thus continue to negotiate
3739 * our already existing value. This can possibly
3740 * go into infinite request-reject loop.
3742 * This is not likely because we normally use
3743 * ifid based on MAC-address.
3744 * If you have no ethernet card on the node, too bad.
3745 * XXX should we use fail_counter?
3750 case IPV6CP_OPT_COMPRESS:
3752 * Peer wants different compression parameters.
3764 sppp_ipv6cp_tlu(struct sppp *sp)
3766 /* we are up - notify isdn daemon */
3772 sppp_ipv6cp_tld(struct sppp *sp)
3777 sppp_ipv6cp_tls(struct sppp *sp)
3779 /* indicate to LCP that it must stay alive */
3780 sp->lcp.protos |= (1 << IDX_IPV6CP);
3784 sppp_ipv6cp_tlf(struct sppp *sp)
3787 #if 0 /* need #if 0 to close IPv6CP properly */
3788 /* we no longer need LCP */
3789 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3790 sppp_lcp_check_and_close(sp);
3795 sppp_ipv6cp_scr(struct sppp *sp)
3797 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3798 struct in6_addr ouraddr;
3801 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3802 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3803 opt[i++] = IPV6CP_OPT_IFID;
3805 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3810 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3811 opt[i++] = IPV6CP_OPT_COMPRESSION;
3813 opt[i++] = 0; /* TBD */
3814 opt[i++] = 0; /* TBD */
3815 /* variable length data may follow */
3819 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3820 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3823 static void sppp_ipv6cp_init(struct sppp *sp)
3827 static void sppp_ipv6cp_up(struct sppp *sp)
3831 static void sppp_ipv6cp_down(struct sppp *sp)
3835 static void sppp_ipv6cp_open(struct sppp *sp)
3839 static void sppp_ipv6cp_close(struct sppp *sp)
3843 static void sppp_ipv6cp_TO(void *sp)
3847 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3852 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3856 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3860 static void sppp_ipv6cp_tlu(struct sppp *sp)
3864 static void sppp_ipv6cp_tld(struct sppp *sp)
3868 static void sppp_ipv6cp_tls(struct sppp *sp)
3872 static void sppp_ipv6cp_tlf(struct sppp *sp)
3876 static void sppp_ipv6cp_scr(struct sppp *sp)
3882 *--------------------------------------------------------------------------*
3884 * The CHAP implementation. *
3886 *--------------------------------------------------------------------------*
3890 * The authentication protocols don't employ a full-fledged state machine as
3891 * the control protocols do, since they do have Open and Close events, but
3892 * not Up and Down, nor are they explicitly terminated. Also, use of the
3893 * authentication protocols may be different in both directions (this makes
3894 * sense, think of a machine that never accepts incoming calls but only
3895 * calls out, it doesn't require the called party to authenticate itself).
3897 * Our state machine for the local authentication protocol (we are requesting
3898 * the peer to authenticate) looks like:
3901 * +--------------------------------------------+
3903 * +--------+ Close +---------+ RCA+
3904 * | |<----------------------------------| |------+
3905 * +--->| Closed | TO* | Opened | sca |
3906 * | | |-----+ +-------| |<-----+
3907 * | +--------+ irc | | +---------+
3913 * | | +------->+ | |
3915 * | +--------+ V | |
3916 * | | |<----+<--------------------+ |
3922 * +------+ +------------------------------------------+
3923 * scn,tld sca,irc,ict,tlu
3928 * Open: LCP reached authentication phase
3929 * Close: LCP reached terminate phase
3931 * RCA+: received reply (pap-req, chap-response), acceptable
3932 * RCN: received reply (pap-req, chap-response), not acceptable
3933 * TO+: timeout with restart counter >= 0
3934 * TO-: timeout with restart counter < 0
3935 * TO*: reschedule timeout for CHAP
3937 * scr: send request packet (none for PAP, chap-challenge)
3938 * sca: send ack packet (pap-ack, chap-success)
3939 * scn: send nak packet (pap-nak, chap-failure)
3940 * ict: initialize re-challenge timer (CHAP only)
3942 * tlu: this-layer-up, LCP reaches network phase
3943 * tld: this-layer-down, LCP enters terminate phase
3945 * Note that in CHAP mode, after sending a new challenge, while the state
3946 * automaton falls back into Req-Sent state, it doesn't signal a tld
3947 * event to LCP, so LCP remains in network phase. Only after not getting
3948 * any response (or after getting an unacceptable response), CHAP closes,
3949 * causing LCP to enter terminate phase.
3951 * With PAP, there is no initial request that can be sent. The peer is
3952 * expected to send one based on the successful negotiation of PAP as
3953 * the authentication protocol during the LCP option negotiation.
3955 * Incoming authentication protocol requests (remote requests
3956 * authentication, we are peer) don't employ a state machine at all,
3957 * they are simply answered. Some peers [Ascend P50 firmware rev
3958 * 4.50] react allergically when sending IPCP requests while they are
3959 * still in authentication phase (thereby violating the standard that
3960 * demands that these NCP packets are to be discarded), so we keep
3961 * track of the peer demanding us to authenticate, and only proceed to
3962 * phase network once we've seen a positive acknowledge for the
3967 * Handle incoming CHAP packets.
3970 sppp_chap_input(struct sppp *sp, struct mbuf *m)
3973 struct lcp_header *h;
3975 u_char *value, *name, digest[AUTHKEYLEN], dsize;
3976 int value_len, name_len;
3979 len = m->m_pkthdr.len;
3983 SPP_FMT "chap invalid packet length: %d bytes\n",
3984 SPP_ARGS(ifp), len);
3987 h = mtod (m, struct lcp_header*);
3988 if (len > ntohs (h->len))
3989 len = ntohs (h->len);
3992 /* challenge, failure and success are his authproto */
3993 case CHAP_CHALLENGE:
3994 value = 1 + (u_char*)(h+1);
3995 value_len = value[-1];
3996 name = value + value_len;
3997 name_len = len - value_len - 5;
4001 SPP_FMT "chap corrupted challenge "
4002 "<%s id=0x%x len=%d",
4004 sppp_auth_type_name(PPP_CHAP, h->type),
4005 h->ident, ntohs(h->len));
4006 sppp_print_bytes((u_char*) (h+1), len-4);
4014 SPP_FMT "chap input <%s id=0x%x len=%d name=",
4016 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
4018 sppp_print_string((char*) name, name_len);
4019 log(-1, " value-size=%d value=", value_len);
4020 sppp_print_bytes(value, value_len);
4024 /* Compute reply value. */
4026 MD5Update(&ctx, &h->ident, 1);
4027 MD5Update(&ctx, sp->myauth.secret,
4028 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
4029 MD5Update(&ctx, value, value_len);
4030 MD5Final(digest, &ctx);
4031 dsize = sizeof digest;
4033 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
4034 sizeof dsize, (const char *)&dsize,
4035 sizeof digest, digest,
4036 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4043 log(LOG_DEBUG, SPP_FMT "chap success",
4047 sppp_print_string((char*)(h + 1), len - 4);
4052 sp->pp_flags &= ~PP_NEEDAUTH;
4053 if (sp->myauth.proto == PPP_CHAP &&
4054 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4055 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
4057 * We are authenticator for CHAP but didn't
4058 * complete yet. Leave it to tlu to proceed
4065 sppp_phase_network(sp);
4070 log(LOG_INFO, SPP_FMT "chap failure",
4074 sppp_print_string((char*)(h + 1), len - 4);
4078 log(LOG_INFO, SPP_FMT "chap failure\n",
4080 /* await LCP shutdown by authenticator */
4083 /* response is my authproto */
4085 value = 1 + (u_char*)(h+1);
4086 value_len = value[-1];
4087 name = value + value_len;
4088 name_len = len - value_len - 5;
4092 SPP_FMT "chap corrupted response "
4093 "<%s id=0x%x len=%d",
4095 sppp_auth_type_name(PPP_CHAP, h->type),
4096 h->ident, ntohs(h->len));
4097 sppp_print_bytes((u_char*)(h+1), len-4);
4102 if (h->ident != sp->confid[IDX_CHAP]) {
4105 SPP_FMT "chap dropping response for old ID "
4106 "(got %d, expected %d)\n",
4108 h->ident, sp->confid[IDX_CHAP]);
4111 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4112 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4113 log(LOG_INFO, SPP_FMT "chap response, his name ",
4115 sppp_print_string(name, name_len);
4116 log(-1, " != expected ");
4117 sppp_print_string(sp->hisauth.name,
4118 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4122 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4123 "<%s id=0x%x len=%d name=",
4125 sppp_state_name(sp->state[IDX_CHAP]),
4126 sppp_auth_type_name(PPP_CHAP, h->type),
4127 h->ident, ntohs (h->len));
4128 sppp_print_string((char*)name, name_len);
4129 log(-1, " value-size=%d value=", value_len);
4130 sppp_print_bytes(value, value_len);
4133 if (value_len != AUTHKEYLEN) {
4136 SPP_FMT "chap bad hash value length: "
4137 "%d bytes, should be %d\n",
4138 SPP_ARGS(ifp), value_len,
4144 MD5Update(&ctx, &h->ident, 1);
4145 MD5Update(&ctx, sp->hisauth.secret,
4146 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4147 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4148 MD5Final(digest, &ctx);
4150 #define FAILMSG "Failed..."
4151 #define SUCCMSG "Welcome!"
4153 if (value_len != sizeof digest ||
4154 bcmp(digest, value, value_len) != 0) {
4155 /* action scn, tld */
4156 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4157 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4162 /* action sca, perhaps tlu */
4163 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4164 sp->state[IDX_CHAP] == STATE_OPENED)
4165 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4166 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4168 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4169 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4175 /* Unknown CHAP packet type -- ignore. */
4177 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4178 "<0x%x id=0x%xh len=%d",
4180 sppp_state_name(sp->state[IDX_CHAP]),
4181 h->type, h->ident, ntohs(h->len));
4182 sppp_print_bytes((u_char*)(h+1), len-4);
4190 sppp_chap_init(struct sppp *sp)
4192 /* Chap doesn't have STATE_INITIAL at all. */
4193 sp->state[IDX_CHAP] = STATE_CLOSED;
4194 sp->fail_counter[IDX_CHAP] = 0;
4195 sp->pp_seq[IDX_CHAP] = 0;
4196 sp->pp_rseq[IDX_CHAP] = 0;
4197 callout_init(&sp->ch[IDX_CHAP], 1);
4201 sppp_chap_open(struct sppp *sp)
4203 if (sp->myauth.proto == PPP_CHAP &&
4204 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4205 /* we are authenticator for CHAP, start it */
4207 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4208 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4210 /* nothing to be done if we are peer, await a challenge */
4214 sppp_chap_close(struct sppp *sp)
4216 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4217 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4221 sppp_chap_TO(void *cookie)
4223 struct sppp *sp = (struct sppp *)cookie;
4228 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4230 sppp_state_name(sp->state[IDX_CHAP]),
4231 sp->rst_counter[IDX_CHAP]);
4233 if (--sp->rst_counter[IDX_CHAP] < 0)
4235 switch (sp->state[IDX_CHAP]) {
4236 case STATE_REQ_SENT:
4238 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4242 /* TO+ (or TO*) event */
4243 switch (sp->state[IDX_CHAP]) {
4246 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4248 case STATE_REQ_SENT:
4250 /* sppp_cp_change_state() will restart the timer */
4251 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4259 sppp_chap_tlu(struct sppp *sp)
4265 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4268 * Some broken CHAP implementations (Conware CoNet, firmware
4269 * 4.0.?) don't want to re-authenticate their CHAP once the
4270 * initial challenge-response exchange has taken place.
4271 * Provide for an option to avoid rechallenges.
4273 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4275 * Compute the re-challenge timeout. This will yield
4276 * a number between 300 and 810 seconds.
4278 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
4279 callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, (void *)sp);
4284 SPP_FMT "chap %s, ",
4286 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4287 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4288 log(-1, "next re-challenge in %d seconds\n", i);
4290 log(-1, "re-challenging suppressed\n");
4294 /* indicate to LCP that we need to be closed down */
4295 sp->lcp.protos |= (1 << IDX_CHAP);
4297 if (sp->pp_flags & PP_NEEDAUTH) {
4299 * Remote is authenticator, but his auth proto didn't
4300 * complete yet. Defer the transition to network
4309 * If we are already in phase network, we are done here. This
4310 * is the case if this is a dummy tlu event after a re-challenge.
4312 if (sp->pp_phase != PHASE_NETWORK)
4313 sppp_phase_network(sp);
4317 sppp_chap_tld(struct sppp *sp)
4322 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4323 callout_stop(&sp->ch[IDX_CHAP]);
4324 sp->lcp.protos &= ~(1 << IDX_CHAP);
4330 sppp_chap_scr(struct sppp *sp)
4335 /* Compute random challenge. */
4336 ch = (u_long *)sp->myauth.challenge;
4337 arc4random_buf(ch, 4 * sizeof(*ch));
4340 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4342 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4343 sizeof clen, (const char *)&clen,
4344 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4345 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4351 *--------------------------------------------------------------------------*
4353 * The PAP implementation. *
4355 *--------------------------------------------------------------------------*
4358 * For PAP, we need to keep a little state also if we are the peer, not the
4359 * authenticator. This is since we don't get a request to authenticate, but
4360 * have to repeatedly authenticate ourself until we got a response (or the
4361 * retry counter is expired).
4365 * Handle incoming PAP packets. */
4367 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4370 struct lcp_header *h;
4372 u_char *name, *passwd, mlen;
4373 int name_len, passwd_len;
4375 len = m->m_pkthdr.len;
4379 SPP_FMT "pap invalid packet length: %d bytes\n",
4380 SPP_ARGS(ifp), len);
4383 h = mtod (m, struct lcp_header*);
4384 if (len > ntohs (h->len))
4385 len = ntohs (h->len);
4387 /* PAP request is my authproto */
4389 name = 1 + (u_char*)(h+1);
4390 name_len = name[-1];
4391 passwd = name + name_len + 1;
4392 if (name_len > len - 6 ||
4393 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4395 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4396 "<%s id=0x%x len=%d",
4398 sppp_auth_type_name(PPP_PAP, h->type),
4399 h->ident, ntohs(h->len));
4400 sppp_print_bytes((u_char*)(h+1), len-4);
4406 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4407 "<%s id=0x%x len=%d name=",
4409 sppp_state_name(sp->state[IDX_PAP]),
4410 sppp_auth_type_name(PPP_PAP, h->type),
4411 h->ident, ntohs(h->len));
4412 sppp_print_string((char*)name, name_len);
4413 log(-1, " passwd=");
4414 sppp_print_string((char*)passwd, passwd_len);
4417 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4418 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4419 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4420 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4421 /* action scn, tld */
4422 mlen = sizeof(FAILMSG) - 1;
4423 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4424 sizeof mlen, (const char *)&mlen,
4425 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4430 /* action sca, perhaps tlu */
4431 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4432 sp->state[IDX_PAP] == STATE_OPENED) {
4433 mlen = sizeof(SUCCMSG) - 1;
4434 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4435 sizeof mlen, (const char *)&mlen,
4436 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4439 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4440 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4445 /* ack and nak are his authproto */
4447 callout_stop(&sp->pap_my_to_ch);
4449 log(LOG_DEBUG, SPP_FMT "pap success",
4451 name_len = *((char *)h);
4452 if (len > 5 && name_len) {
4454 sppp_print_string((char*)(h+1), name_len);
4459 sp->pp_flags &= ~PP_NEEDAUTH;
4460 if (sp->myauth.proto == PPP_PAP &&
4461 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4462 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4464 * We are authenticator for PAP but didn't
4465 * complete yet. Leave it to tlu to proceed
4472 sppp_phase_network(sp);
4476 callout_stop (&sp->pap_my_to_ch);
4478 log(LOG_INFO, SPP_FMT "pap failure",
4480 name_len = *((char *)h);
4481 if (len > 5 && name_len) {
4483 sppp_print_string((char*)(h+1), name_len);
4487 log(LOG_INFO, SPP_FMT "pap failure\n",
4489 /* await LCP shutdown by authenticator */
4493 /* Unknown PAP packet type -- ignore. */
4495 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4496 "<0x%x id=0x%x len=%d",
4498 h->type, h->ident, ntohs(h->len));
4499 sppp_print_bytes((u_char*)(h+1), len-4);
4507 sppp_pap_init(struct sppp *sp)
4509 /* PAP doesn't have STATE_INITIAL at all. */
4510 sp->state[IDX_PAP] = STATE_CLOSED;
4511 sp->fail_counter[IDX_PAP] = 0;
4512 sp->pp_seq[IDX_PAP] = 0;
4513 sp->pp_rseq[IDX_PAP] = 0;
4514 callout_init(&sp->ch[IDX_PAP], 1);
4515 callout_init(&sp->pap_my_to_ch, 1);
4519 sppp_pap_open(struct sppp *sp)
4521 if (sp->hisauth.proto == PPP_PAP &&
4522 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4523 /* we are authenticator for PAP, start our timer */
4524 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4525 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4527 if (sp->myauth.proto == PPP_PAP) {
4528 /* we are peer, send a request, and start a timer */
4530 callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
4531 sppp_pap_my_TO, (void *)sp);
4536 sppp_pap_close(struct sppp *sp)
4538 if (sp->state[IDX_PAP] != STATE_CLOSED)
4539 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4543 * That's the timeout routine if we are authenticator. Since the
4544 * authenticator is basically passive in PAP, we can't do much here.
4547 sppp_pap_TO(void *cookie)
4549 struct sppp *sp = (struct sppp *)cookie;
4554 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4556 sppp_state_name(sp->state[IDX_PAP]),
4557 sp->rst_counter[IDX_PAP]);
4559 if (--sp->rst_counter[IDX_PAP] < 0)
4561 switch (sp->state[IDX_PAP]) {
4562 case STATE_REQ_SENT:
4564 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4568 /* TO+ event, not very much we could do */
4569 switch (sp->state[IDX_PAP]) {
4570 case STATE_REQ_SENT:
4571 /* sppp_cp_change_state() will restart the timer */
4572 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4580 * That's the timeout handler if we are peer. Since the peer is active,
4581 * we need to retransmit our PAP request since it is apparently lost.
4582 * XXX We should impose a max counter.
4585 sppp_pap_my_TO(void *cookie)
4587 struct sppp *sp = (struct sppp *)cookie;
4591 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4600 sppp_pap_tlu(struct sppp *sp)
4604 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4607 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4608 SPP_ARGS(ifp), pap.name);
4611 /* indicate to LCP that we need to be closed down */
4612 sp->lcp.protos |= (1 << IDX_PAP);
4614 if (sp->pp_flags & PP_NEEDAUTH) {
4616 * Remote is authenticator, but his auth proto didn't
4617 * complete yet. Defer the transition to network
4624 sppp_phase_network(sp);
4628 sppp_pap_tld(struct sppp *sp)
4633 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4634 callout_stop (&sp->ch[IDX_PAP]);
4635 callout_stop (&sp->pap_my_to_ch);
4636 sp->lcp.protos &= ~(1 << IDX_PAP);
4642 sppp_pap_scr(struct sppp *sp)
4644 u_char idlen, pwdlen;
4646 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4647 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4648 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4650 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4651 sizeof idlen, (const char *)&idlen,
4652 (size_t)idlen, sp->myauth.name,
4653 sizeof pwdlen, (const char *)&pwdlen,
4654 (size_t)pwdlen, sp->myauth.secret,
4659 * Random miscellaneous functions.
4663 * Send a PAP or CHAP proto packet.
4665 * Varadic function, each of the elements for the ellipsis is of type
4666 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4668 * NOTE: never declare variadic functions with types subject to type
4669 * promotion (i.e. u_char). This is asking for big trouble depending
4670 * on the architecture you are on...
4674 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4675 unsigned int type, unsigned int id,
4679 struct ppp_header *h;
4680 struct lcp_header *lh;
4688 MGETHDR (m, M_NOWAIT, MT_DATA);
4691 m->m_pkthdr.rcvif = 0;
4693 h = mtod (m, struct ppp_header*);
4694 h->address = PPP_ALLSTATIONS; /* broadcast address */
4695 h->control = PPP_UI; /* Unnumbered Info */
4696 h->protocol = htons(cp->proto);
4698 lh = (struct lcp_header*)(h + 1);
4701 p = (u_char*) (lh+1);
4706 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
4707 msg = va_arg(ap, const char *);
4709 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4715 bcopy(msg, p, mlen);
4720 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4721 lh->len = htons (LCP_HEADER_LEN + len);
4724 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4725 SPP_ARGS(ifp), cp->name,
4726 sppp_auth_type_name(cp->proto, lh->type),
4727 lh->ident, ntohs(lh->len));
4728 sppp_print_bytes((u_char*) (lh+1), len);
4731 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
4732 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4736 * Flush interface queue.
4739 sppp_qflush(struct ifqueue *ifq)
4754 * Send keepalive packets, every 10 seconds.
4757 sppp_keepalive(void *dummy)
4759 struct sppp *sp = (struct sppp*)dummy;
4760 struct ifnet *ifp = SP2IFP(sp);
4763 /* Keepalive mode disabled or channel down? */
4764 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4765 ! (ifp->if_drv_flags & IFF_DRV_RUNNING))
4768 if (sp->pp_mode == PP_FR) {
4769 sppp_fr_keepalive (sp);
4773 /* No keepalive in PPP mode if LCP not opened yet. */
4774 if (sp->pp_mode != IFF_CISCO &&
4775 sp->pp_phase < PHASE_AUTHENTICATE)
4778 if (sp->pp_alivecnt == MAXALIVECNT) {
4779 /* No keepalive packets got. Stop the interface. */
4780 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
4782 sppp_qflush (&sp->pp_cpq);
4783 if (sp->pp_mode != IFF_CISCO) {
4785 /* Shut down the PPP link. */
4787 /* Initiate negotiation. XXX */
4791 if (sp->pp_alivecnt <= MAXALIVECNT)
4793 if (sp->pp_mode == IFF_CISCO)
4794 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4795 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4796 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4797 uint32_t nmagic = htonl(sp->lcp.magic);
4798 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4799 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4800 sp->lcp.echoid, 4, &nmagic);
4804 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
4809 * Get both IP addresses.
4812 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4814 struct epoch_tracker et;
4815 struct ifnet *ifp = SP2IFP(sp);
4817 struct sockaddr_in *si, *sm;
4823 * Pick the first AF_INET address from the list,
4824 * aliases don't make any sense on a p2p link anyway.
4827 NET_EPOCH_ENTER(et);
4828 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4829 if (ifa->ifa_addr->sa_family == AF_INET) {
4830 si = (struct sockaddr_in *)ifa->ifa_addr;
4831 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4836 if (si && si->sin_addr.s_addr) {
4837 ssrc = si->sin_addr.s_addr;
4839 *srcmask = ntohl(sm->sin_addr.s_addr);
4842 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4843 if (si && si->sin_addr.s_addr)
4844 ddst = si->sin_addr.s_addr;
4848 if (dst) *dst = ntohl(ddst);
4849 if (src) *src = ntohl(ssrc);
4854 * Set my IP address.
4857 sppp_set_ip_addr(struct sppp *sp, u_long src)
4860 struct epoch_tracker et;
4862 struct sockaddr_in *si;
4863 struct in_ifaddr *ia;
4866 * Pick the first AF_INET address from the list,
4867 * aliases don't make any sense on a p2p link anyway.
4870 NET_EPOCH_ENTER(et);
4871 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4872 if (ifa->ifa_addr->sa_family == AF_INET) {
4873 si = (struct sockaddr_in *)ifa->ifa_addr;
4885 /* delete old route */
4886 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4887 if (debug && error) {
4888 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4889 SPP_ARGS(ifp), error);
4892 /* set new address */
4893 si->sin_addr.s_addr = htonl(src);
4896 LIST_REMOVE(ia, ia_hash);
4897 LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash);
4898 IN_IFADDR_WUNLOCK();
4901 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
4902 if (debug && error) {
4903 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
4904 SPP_ARGS(ifp), error);
4913 * Get both IPv6 addresses.
4916 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4917 struct in6_addr *srcmask)
4919 struct epoch_tracker et;
4920 struct ifnet *ifp = SP2IFP(sp);
4922 struct sockaddr_in6 *si, *sm;
4923 struct in6_addr ssrc, ddst;
4926 bzero(&ssrc, sizeof(ssrc));
4927 bzero(&ddst, sizeof(ddst));
4929 * Pick the first link-local AF_INET6 address from the list,
4930 * aliases don't make any sense on a p2p link anyway.
4933 NET_EPOCH_ENTER(et);
4934 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4935 if (ifa->ifa_addr->sa_family == AF_INET6) {
4936 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4937 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4938 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
4942 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
4943 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
4945 bcopy(&sm->sin6_addr, srcmask,
4950 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
4951 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
4952 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
4956 bcopy(&ddst, dst, sizeof(*dst));
4958 bcopy(&ssrc, src, sizeof(*src));
4962 #ifdef IPV6CP_MYIFID_DYN
4964 * Generate random ifid.
4967 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
4973 * Set my IPv6 address.
4976 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
4979 struct epoch_tracker et;
4981 struct sockaddr_in6 *sin6;
4984 * Pick the first link-local AF_INET6 address from the list,
4985 * aliases don't make any sense on a p2p link anyway.
4989 NET_EPOCH_ENTER(et);
4990 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4991 if (ifa->ifa_addr->sa_family == AF_INET6) {
4992 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
4993 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5003 struct sockaddr_in6 new_sin6 = *sin6;
5005 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
5006 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5007 if (debug && error) {
5008 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
5009 " failed, error=%d\n", SPP_ARGS(ifp), error);
5017 * Suggest a candidate address to be used by peer.
5020 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5022 struct in6_addr myaddr;
5025 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5027 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5029 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5030 myaddr.s6_addr[14] ^= 0xff;
5031 myaddr.s6_addr[15] ^= 0xff;
5033 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5034 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5037 bcopy(&myaddr, suggest, sizeof(myaddr));
5042 sppp_params(struct sppp *sp, u_long cmd, void *data)
5045 struct ifreq *ifr = (struct ifreq *)data;
5046 struct spppreq *spr;
5049 if ((spr = malloc(sizeof(struct spppreq), M_TEMP, M_NOWAIT)) == NULL)
5052 * ifr_data_get_ptr(ifr) is supposed to point to a struct spppreq.
5053 * Check the cmd word first before attempting to fetch all the
5056 rv = fueword(ifr_data_get_ptr(ifr), &subcmd);
5062 if (copyin(ifr_data_get_ptr(ifr), spr, sizeof(struct spppreq)) != 0) {
5068 case (u_long)SPPPIOGDEFS:
5069 if (cmd != SIOCGIFGENERIC) {
5074 * We copy over the entire current state, but clean
5075 * out some of the stuff we don't wanna pass up.
5076 * Remember, SIOCGIFGENERIC is unprotected, and can be
5077 * called by any user. No need to ever get PAP or
5078 * CHAP secrets back to userland anyway.
5080 spr->defs.pp_phase = sp->pp_phase;
5081 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5082 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5083 spr->defs.lcp = sp->lcp;
5084 spr->defs.ipcp = sp->ipcp;
5085 spr->defs.ipv6cp = sp->ipv6cp;
5086 spr->defs.myauth = sp->myauth;
5087 spr->defs.hisauth = sp->hisauth;
5088 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5089 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5090 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5091 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5093 * Fixup the LCP timeout value to milliseconds so
5094 * spppcontrol doesn't need to bother about the value
5095 * of "hz". We do the reverse calculation below when
5098 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5099 rv = copyout(spr, ifr_data_get_ptr(ifr),
5100 sizeof(struct spppreq));
5103 case (u_long)SPPPIOSDEFS:
5104 if (cmd != SIOCSIFGENERIC) {
5109 * We have a very specific idea of which fields we
5110 * allow being passed back from userland, so to not
5111 * clobber our current state. For one, we only allow
5112 * setting anything if LCP is in dead or establish
5113 * phase. Once the authentication negotiations
5114 * started, the authentication settings must not be
5115 * changed again. (The administrator can force an
5116 * ifconfig down in order to get LCP back into dead
5119 * Also, we only allow for authentication parameters to be
5122 * XXX Should allow to set or clear pp_flags.
5124 * Finally, if the respective authentication protocol to
5125 * be used is set differently than 0, but the secret is
5126 * passed as all zeros, we don't trash the existing secret.
5127 * This allows an administrator to change the system name
5128 * only without clobbering the secret (which he didn't get
5129 * back in a previous SPPPIOGDEFS call). However, the
5130 * secrets are cleared if the authentication protocol is
5132 if (sp->pp_phase != PHASE_DEAD &&
5133 sp->pp_phase != PHASE_ESTABLISH) {
5138 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5139 spr->defs.myauth.proto != PPP_CHAP) ||
5140 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5141 spr->defs.hisauth.proto != PPP_CHAP)) {
5146 if (spr->defs.myauth.proto == 0)
5147 /* resetting myauth */
5148 bzero(&sp->myauth, sizeof sp->myauth);
5150 /* setting/changing myauth */
5151 sp->myauth.proto = spr->defs.myauth.proto;
5152 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5153 if (spr->defs.myauth.secret[0] != '\0')
5154 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5157 if (spr->defs.hisauth.proto == 0)
5158 /* resetting hisauth */
5159 bzero(&sp->hisauth, sizeof sp->hisauth);
5161 /* setting/changing hisauth */
5162 sp->hisauth.proto = spr->defs.hisauth.proto;
5163 sp->hisauth.flags = spr->defs.hisauth.flags;
5164 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5165 if (spr->defs.hisauth.secret[0] != '\0')
5166 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5169 /* set LCP restart timer timeout */
5170 if (spr->defs.lcp.timeout != 0)
5171 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5172 /* set VJ enable and IPv6 disable flags */
5174 if (spr->defs.enable_vj)
5175 sp->confflags |= CONF_ENABLE_VJ;
5177 sp->confflags &= ~CONF_ENABLE_VJ;
5180 if (spr->defs.enable_ipv6)
5181 sp->confflags |= CONF_ENABLE_IPV6;
5183 sp->confflags &= ~CONF_ENABLE_IPV6;
5198 sppp_phase_network(struct sppp *sp)
5204 sp->pp_phase = PHASE_NETWORK;
5207 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5208 sppp_phase_name(sp->pp_phase));
5210 /* Notify NCPs now. */
5211 for (i = 0; i < IDX_COUNT; i++)
5212 if ((cps[i])->flags & CP_NCP)
5215 /* Send Up events to all NCPs. */
5216 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5217 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5220 /* if no NCP is starting, all this was in vain, close down */
5221 sppp_lcp_check_and_close(sp);
5225 sppp_cp_type_name(u_char type)
5227 static char buf[12];
5229 case CONF_REQ: return "conf-req";
5230 case CONF_ACK: return "conf-ack";
5231 case CONF_NAK: return "conf-nak";
5232 case CONF_REJ: return "conf-rej";
5233 case TERM_REQ: return "term-req";
5234 case TERM_ACK: return "term-ack";
5235 case CODE_REJ: return "code-rej";
5236 case PROTO_REJ: return "proto-rej";
5237 case ECHO_REQ: return "echo-req";
5238 case ECHO_REPLY: return "echo-reply";
5239 case DISC_REQ: return "discard-req";
5241 snprintf (buf, sizeof(buf), "cp/0x%x", type);
5246 sppp_auth_type_name(u_short proto, u_char type)
5248 static char buf[12];
5252 case CHAP_CHALLENGE: return "challenge";
5253 case CHAP_RESPONSE: return "response";
5254 case CHAP_SUCCESS: return "success";
5255 case CHAP_FAILURE: return "failure";
5259 case PAP_REQ: return "req";
5260 case PAP_ACK: return "ack";
5261 case PAP_NAK: return "nak";
5264 snprintf (buf, sizeof(buf), "auth/0x%x", type);
5269 sppp_lcp_opt_name(u_char opt)
5271 static char buf[12];
5273 case LCP_OPT_MRU: return "mru";
5274 case LCP_OPT_ASYNC_MAP: return "async-map";
5275 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5276 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5277 case LCP_OPT_MAGIC: return "magic";
5278 case LCP_OPT_PROTO_COMP: return "proto-comp";
5279 case LCP_OPT_ADDR_COMP: return "addr-comp";
5281 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5287 sppp_ipcp_opt_name(u_char opt)
5289 static char buf[12];
5291 case IPCP_OPT_ADDRESSES: return "addresses";
5292 case IPCP_OPT_COMPRESSION: return "compression";
5293 case IPCP_OPT_ADDRESS: return "address";
5295 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5302 sppp_ipv6cp_opt_name(u_char opt)
5304 static char buf[12];
5306 case IPV6CP_OPT_IFID: return "ifid";
5307 case IPV6CP_OPT_COMPRESSION: return "compression";
5309 sprintf (buf, "0x%x", opt);
5315 sppp_state_name(int state)
5318 case STATE_INITIAL: return "initial";
5319 case STATE_STARTING: return "starting";
5320 case STATE_CLOSED: return "closed";
5321 case STATE_STOPPED: return "stopped";
5322 case STATE_CLOSING: return "closing";
5323 case STATE_STOPPING: return "stopping";
5324 case STATE_REQ_SENT: return "req-sent";
5325 case STATE_ACK_RCVD: return "ack-rcvd";
5326 case STATE_ACK_SENT: return "ack-sent";
5327 case STATE_OPENED: return "opened";
5333 sppp_phase_name(enum ppp_phase phase)
5336 case PHASE_DEAD: return "dead";
5337 case PHASE_ESTABLISH: return "establish";
5338 case PHASE_TERMINATE: return "terminate";
5339 case PHASE_AUTHENTICATE: return "authenticate";
5340 case PHASE_NETWORK: return "network";
5346 sppp_proto_name(u_short proto)
5348 static char buf[12];
5350 case PPP_LCP: return "lcp";
5351 case PPP_IPCP: return "ipcp";
5352 case PPP_PAP: return "pap";
5353 case PPP_CHAP: return "chap";
5354 case PPP_IPV6CP: return "ipv6cp";
5356 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5361 sppp_print_bytes(const u_char *p, u_short len)
5364 log(-1, " %*D", len, p, "-");
5368 sppp_print_string(const char *p, u_short len)
5375 * Print only ASCII chars directly. RFC 1994 recommends
5376 * using only them, but we don't rely on it. */
5377 if (c < ' ' || c > '~')
5378 log(-1, "\\x%x", c);
5386 sppp_dotted_quad(u_long addr)
5389 sprintf(s, "%d.%d.%d.%d",
5390 (int)((addr >> 24) & 0xff),
5391 (int)((addr >> 16) & 0xff),
5392 (int)((addr >> 8) & 0xff),
5393 (int)(addr & 0xff));
5399 sppp_strnlen(u_char *p, int max)
5403 for (len = 0; len < max && *p; ++p)
5408 /* a dummy, used to drop uninteresting events */
5410 sppp_null(struct sppp *unused)
5412 /* do just nothing */