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);
1868 * The generic part of all Up/Down/Open/Close/TO event handlers.
1869 * Basically, the state transition handling in the automaton.
1872 sppp_up_event(const struct cp *cp, struct sppp *sp)
1877 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1878 SPP_ARGS(ifp), cp->name,
1879 sppp_state_name(sp->state[cp->protoidx]));
1881 switch (sp->state[cp->protoidx]) {
1883 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1885 case STATE_STARTING:
1886 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1888 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1891 printf(SPP_FMT "%s illegal up in state %s\n",
1892 SPP_ARGS(ifp), cp->name,
1893 sppp_state_name(sp->state[cp->protoidx]));
1898 sppp_down_event(const struct cp *cp, struct sppp *sp)
1903 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1904 SPP_ARGS(ifp), cp->name,
1905 sppp_state_name(sp->state[cp->protoidx]));
1907 switch (sp->state[cp->protoidx]) {
1910 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1913 sppp_cp_change_state(cp, sp, STATE_STARTING);
1916 case STATE_STOPPING:
1917 case STATE_REQ_SENT:
1918 case STATE_ACK_RCVD:
1919 case STATE_ACK_SENT:
1920 sppp_cp_change_state(cp, sp, STATE_STARTING);
1924 sppp_cp_change_state(cp, sp, STATE_STARTING);
1927 printf(SPP_FMT "%s illegal down in state %s\n",
1928 SPP_ARGS(ifp), cp->name,
1929 sppp_state_name(sp->state[cp->protoidx]));
1935 sppp_open_event(const struct cp *cp, struct sppp *sp)
1940 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1941 SPP_ARGS(ifp), cp->name,
1942 sppp_state_name(sp->state[cp->protoidx]));
1944 switch (sp->state[cp->protoidx]) {
1946 sppp_cp_change_state(cp, sp, STATE_STARTING);
1949 case STATE_STARTING:
1952 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1954 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1958 * Try escaping stopped state. This seems to bite
1959 * people occasionally, in particular for IPCP,
1960 * presumably following previous IPCP negotiation
1961 * aborts. Somehow, we must have missed a Down event
1962 * which would have caused a transition into starting
1963 * state, so as a bandaid we force the Down event now.
1964 * This effectively implements (something like the)
1965 * `restart' option mentioned in the state transition
1966 * table of RFC 1661.
1968 sppp_cp_change_state(cp, sp, STATE_STARTING);
1971 case STATE_STOPPING:
1972 case STATE_REQ_SENT:
1973 case STATE_ACK_RCVD:
1974 case STATE_ACK_SENT:
1978 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1985 sppp_close_event(const struct cp *cp, struct sppp *sp)
1990 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
1991 SPP_ARGS(ifp), cp->name,
1992 sppp_state_name(sp->state[cp->protoidx]));
1994 switch (sp->state[cp->protoidx]) {
1999 case STATE_STARTING:
2000 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2004 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2006 case STATE_STOPPING:
2007 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2012 case STATE_REQ_SENT:
2013 case STATE_ACK_RCVD:
2014 case STATE_ACK_SENT:
2015 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2016 sppp_cp_send(sp, cp->proto, TERM_REQ,
2017 ++sp->pp_seq[cp->protoidx], 0, 0);
2018 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2024 sppp_to_event(const struct cp *cp, struct sppp *sp)
2030 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
2031 SPP_ARGS(ifp), cp->name,
2032 sppp_state_name(sp->state[cp->protoidx]),
2033 sp->rst_counter[cp->protoidx]);
2035 if (--sp->rst_counter[cp->protoidx] < 0)
2037 switch (sp->state[cp->protoidx]) {
2039 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2042 case STATE_STOPPING:
2043 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2046 case STATE_REQ_SENT:
2047 case STATE_ACK_RCVD:
2048 case STATE_ACK_SENT:
2049 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2055 switch (sp->state[cp->protoidx]) {
2057 case STATE_STOPPING:
2058 sppp_cp_send(sp, cp->proto, TERM_REQ,
2059 ++sp->pp_seq[cp->protoidx], 0, 0);
2060 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2061 cp->TO, (void *)sp);
2063 case STATE_REQ_SENT:
2064 case STATE_ACK_RCVD:
2066 /* sppp_cp_change_state() will restart the timer */
2067 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2069 case STATE_ACK_SENT:
2071 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2072 cp->TO, (void *)sp);
2080 * Change the state of a control protocol in the state automaton.
2081 * Takes care of starting/stopping the restart timer.
2084 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2086 sp->state[cp->protoidx] = newstate;
2088 callout_stop (&sp->ch[cp->protoidx]);
2092 case STATE_STARTING:
2098 case STATE_STOPPING:
2099 case STATE_REQ_SENT:
2100 case STATE_ACK_RCVD:
2101 case STATE_ACK_SENT:
2102 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2103 cp->TO, (void *)sp);
2109 *--------------------------------------------------------------------------*
2111 * The LCP implementation. *
2113 *--------------------------------------------------------------------------*
2116 sppp_pp_up(struct sppp *sp)
2124 sppp_pp_down(struct sppp *sp)
2132 sppp_lcp_init(struct sppp *sp)
2134 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2136 sp->state[IDX_LCP] = STATE_INITIAL;
2137 sp->fail_counter[IDX_LCP] = 0;
2138 sp->pp_seq[IDX_LCP] = 0;
2139 sp->pp_rseq[IDX_LCP] = 0;
2141 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2143 /* Note that these values are relevant for all control protocols */
2144 sp->lcp.timeout = 3 * hz;
2145 sp->lcp.max_terminate = 2;
2146 sp->lcp.max_configure = 10;
2147 sp->lcp.max_failure = 10;
2148 callout_init(&sp->ch[IDX_LCP], 1);
2152 sppp_lcp_up(struct sppp *sp)
2156 sp->pp_alivecnt = 0;
2157 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2160 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2162 * If we are authenticator, negotiate LCP_AUTH
2164 if (sp->hisauth.proto != 0)
2165 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2167 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2168 sp->pp_flags &= ~PP_NEEDAUTH;
2170 * If this interface is passive or dial-on-demand, and we are
2171 * still in Initial state, it means we've got an incoming
2172 * call. Activate the interface.
2174 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2177 SPP_FMT "Up event", SPP_ARGS(ifp));
2178 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2179 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2181 log(-1, "(incoming call)\n");
2182 sp->pp_flags |= PP_CALLIN;
2186 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2187 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2188 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2192 sppp_up_event(&lcp, sp);
2196 sppp_lcp_down(struct sppp *sp)
2200 sppp_down_event(&lcp, sp);
2203 * If this is neither a dial-on-demand nor a passive
2204 * interface, simulate an ``ifconfig down'' action, so the
2205 * administrator can force a redial by another ``ifconfig
2206 * up''. XXX For leased line operation, should we immediately
2207 * try to reopen the connection here?
2209 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2211 SPP_FMT "Down event, taking interface down.\n",
2217 SPP_FMT "Down event (carrier loss)\n",
2219 sp->pp_flags &= ~PP_CALLIN;
2220 if (sp->state[IDX_LCP] != STATE_INITIAL)
2222 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2227 sppp_lcp_open(struct sppp *sp)
2229 sppp_open_event(&lcp, sp);
2233 sppp_lcp_close(struct sppp *sp)
2235 sppp_close_event(&lcp, sp);
2239 sppp_lcp_TO(void *cookie)
2241 sppp_to_event(&lcp, (struct sppp *)cookie);
2245 * Analyze a configure request. Return true if it was agreeable, and
2246 * caused action sca, false if it has been rejected or nak'ed, and
2247 * caused action scn. (The return value is used to make the state
2248 * transition decision in the state automaton.)
2251 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2254 u_char *buf, *r, *p;
2261 buf = r = malloc (len, M_TEMP, M_NOWAIT);
2266 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2269 /* pass 1: check for things that need to be rejected */
2271 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2272 len-=p[1], p+=p[1]) {
2274 log(-1, " %s ", sppp_lcp_opt_name(*p));
2278 if (len >= 6 && p[1] == 6)
2281 log(-1, "[invalid] ");
2283 case LCP_OPT_ASYNC_MAP:
2284 /* Async control character map. */
2285 if (len >= 6 && p[1] == 6)
2288 log(-1, "[invalid] ");
2291 /* Maximum receive unit. */
2292 if (len >= 4 && p[1] == 4)
2295 log(-1, "[invalid] ");
2297 case LCP_OPT_AUTH_PROTO:
2300 log(-1, "[invalid] ");
2303 authproto = (p[2] << 8) + p[3];
2304 if (authproto == PPP_CHAP && p[1] != 5) {
2306 log(-1, "[invalid chap len] ");
2309 if (sp->myauth.proto == 0) {
2310 /* we are not configured to do auth */
2312 log(-1, "[not configured] ");
2316 * Remote want us to authenticate, remember this,
2317 * so we stay in PHASE_AUTHENTICATE after LCP got
2320 sp->pp_flags |= PP_NEEDAUTH;
2323 /* Others not supported. */
2328 /* Add the option to rejected list. */
2335 log(-1, " send conf-rej\n");
2336 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2342 * pass 2: check for option values that are unacceptable and
2343 * thus require to be nak'ed.
2346 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2351 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2352 len-=p[1], p+=p[1]) {
2354 log(-1, " %s ", sppp_lcp_opt_name(*p));
2357 /* Magic number -- extract. */
2358 nmagic = (u_long)p[2] << 24 |
2359 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2360 if (nmagic != sp->lcp.magic) {
2363 log(-1, "0x%lx ", nmagic);
2366 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2367 log(-1, "[glitch] ");
2370 * We negate our magic here, and NAK it. If
2371 * we see it later in an NAK packet, we
2372 * suggest a new one.
2374 nmagic = ~sp->lcp.magic;
2376 p[2] = nmagic >> 24;
2377 p[3] = nmagic >> 16;
2382 case LCP_OPT_ASYNC_MAP:
2384 * Async control character map -- just ignore it.
2386 * Quote from RFC 1662, chapter 6:
2387 * To enable this functionality, synchronous PPP
2388 * implementations MUST always respond to the
2389 * Async-Control-Character-Map Configuration
2390 * Option with the LCP Configure-Ack. However,
2391 * acceptance of the Configuration Option does
2392 * not imply that the synchronous implementation
2393 * will do any ACCM mapping. Instead, all such
2394 * octet mapping will be performed by the
2395 * asynchronous-to-synchronous converter.
2401 * Maximum receive unit. Always agreeable,
2402 * but ignored by now.
2404 sp->lcp.their_mru = p[2] * 256 + p[3];
2406 log(-1, "%lu ", sp->lcp.their_mru);
2409 case LCP_OPT_AUTH_PROTO:
2410 authproto = (p[2] << 8) + p[3];
2411 if (sp->myauth.proto != authproto) {
2412 /* not agreed, nak */
2414 log(-1, "[mine %s != his %s] ",
2415 sppp_proto_name(sp->hisauth.proto),
2416 sppp_proto_name(authproto));
2417 p[2] = sp->myauth.proto >> 8;
2418 p[3] = sp->myauth.proto;
2421 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2423 log(-1, "[chap not MD5] ");
2429 /* Add the option to nak'ed list. */
2436 * Local and remote magics equal -- loopback?
2438 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2439 if (sp->pp_loopcnt == MAXALIVECNT*5)
2440 printf (SPP_FMT "loopback\n",
2442 if (ifp->if_flags & IFF_UP) {
2444 sppp_qflush(&sp->pp_cpq);
2449 } else if (!sp->pp_loopcnt &&
2450 ++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2452 log(-1, " max_failure (%d) exceeded, "
2454 sp->lcp.max_failure);
2455 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2458 log(-1, " send conf-nak\n");
2459 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2463 log(-1, " send conf-ack\n");
2464 sp->fail_counter[IDX_LCP] = 0;
2466 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2467 h->ident, origlen, h+1);
2475 * Analyze the LCP Configure-Reject option list, and adjust our
2479 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2485 buf = malloc (len, M_TEMP, M_NOWAIT);
2490 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2494 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2495 len -= p[1], p += p[1]) {
2497 log(-1, " %s ", sppp_lcp_opt_name(*p));
2500 /* Magic number -- can't use it, use 0 */
2501 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2506 * Should not be rejected anyway, since we only
2507 * negotiate a MRU if explicitly requested by
2510 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2512 case LCP_OPT_AUTH_PROTO:
2514 * Peer doesn't want to authenticate himself,
2515 * deny unless this is a dialout call, and
2516 * AUTHFLAG_NOCALLOUT is set.
2518 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2519 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2521 log(-1, "[don't insist on auth "
2523 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2527 log(-1, "[access denied]\n");
2539 * Analyze the LCP Configure-NAK option list, and adjust our
2543 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2550 buf = malloc (len, M_TEMP, M_NOWAIT);
2555 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2559 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2560 len -= p[1], p += p[1]) {
2562 log(-1, " %s ", sppp_lcp_opt_name(*p));
2565 /* Magic number -- renegotiate */
2566 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2567 len >= 6 && p[1] == 6) {
2568 magic = (u_long)p[2] << 24 |
2569 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2571 * If the remote magic is our negated one,
2572 * this looks like a loopback problem.
2573 * Suggest a new magic to make sure.
2575 if (magic == ~sp->lcp.magic) {
2577 log(-1, "magic glitch ");
2578 sp->lcp.magic = random();
2580 sp->lcp.magic = magic;
2582 log(-1, "%lu ", magic);
2588 * Peer wants to advise us to negotiate an MRU.
2589 * Agree on it if it's reasonable, or use
2590 * default otherwise.
2592 if (len >= 4 && p[1] == 4) {
2593 u_int mru = p[2] * 256 + p[3];
2595 log(-1, "%d ", mru);
2596 if (mru < PP_MTU || mru > PP_MAX_MRU)
2599 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2602 case LCP_OPT_AUTH_PROTO:
2604 * Peer doesn't like our authentication method,
2608 log(-1, "[access denied]\n");
2620 sppp_lcp_tlu(struct sppp *sp)
2627 if (! (ifp->if_flags & IFF_UP) &&
2628 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2629 /* Coming out of loopback mode. */
2631 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2634 for (i = 0; i < IDX_COUNT; i++)
2635 if ((cps[i])->flags & CP_QUAL)
2638 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2639 (sp->pp_flags & PP_NEEDAUTH) != 0)
2640 sp->pp_phase = PHASE_AUTHENTICATE;
2642 sp->pp_phase = PHASE_NETWORK;
2645 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2646 sppp_phase_name(sp->pp_phase));
2649 * Open all authentication protocols. This is even required
2650 * if we already proceeded to network phase, since it might be
2651 * that remote wants us to authenticate, so we might have to
2652 * send a PAP request. Undesired authentication protocols
2653 * don't do anything when they get an Open event.
2655 for (i = 0; i < IDX_COUNT; i++)
2656 if ((cps[i])->flags & CP_AUTH)
2659 if (sp->pp_phase == PHASE_NETWORK) {
2660 /* Notify all NCPs. */
2661 for (i = 0; i < IDX_COUNT; i++)
2662 if (((cps[i])->flags & CP_NCP) &&
2665 * Hack to administratively disable IPv6 if
2666 * not desired. Perhaps we should have another
2667 * flag for this, but right now, we can make
2668 * all struct cp's read/only.
2670 (cps[i] != &ipv6cp ||
2671 (sp->confflags & CONF_ENABLE_IPV6)))
2675 /* Send Up events to all started protos. */
2676 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2677 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2680 /* notify low-level driver of state change */
2682 sp->pp_chg(sp, (int)sp->pp_phase);
2684 if (sp->pp_phase == PHASE_NETWORK)
2685 /* if no NCP is starting, close down */
2686 sppp_lcp_check_and_close(sp);
2690 sppp_lcp_tld(struct sppp *sp)
2696 sp->pp_phase = PHASE_TERMINATE;
2699 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2700 sppp_phase_name(sp->pp_phase));
2703 * Take upper layers down. We send the Down event first and
2704 * the Close second to prevent the upper layers from sending
2705 * ``a flurry of terminate-request packets'', as the RFC
2708 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2709 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2711 (cps[i])->Close(sp);
2716 sppp_lcp_tls(struct sppp *sp)
2720 sp->pp_phase = PHASE_ESTABLISH;
2723 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2724 sppp_phase_name(sp->pp_phase));
2726 /* Notify lower layer if desired. */
2734 sppp_lcp_tlf(struct sppp *sp)
2738 sp->pp_phase = PHASE_DEAD;
2740 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2741 sppp_phase_name(sp->pp_phase));
2743 /* Notify lower layer if desired. */
2751 sppp_lcp_scr(struct sppp *sp)
2753 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2757 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2758 if (! sp->lcp.magic)
2759 sp->lcp.magic = random();
2760 opt[i++] = LCP_OPT_MAGIC;
2762 opt[i++] = sp->lcp.magic >> 24;
2763 opt[i++] = sp->lcp.magic >> 16;
2764 opt[i++] = sp->lcp.magic >> 8;
2765 opt[i++] = sp->lcp.magic;
2768 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2769 opt[i++] = LCP_OPT_MRU;
2771 opt[i++] = sp->lcp.mru >> 8;
2772 opt[i++] = sp->lcp.mru;
2775 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2776 authproto = sp->hisauth.proto;
2777 opt[i++] = LCP_OPT_AUTH_PROTO;
2778 opt[i++] = authproto == PPP_CHAP? 5: 4;
2779 opt[i++] = authproto >> 8;
2780 opt[i++] = authproto;
2781 if (authproto == PPP_CHAP)
2782 opt[i++] = CHAP_MD5;
2785 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2786 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2790 * Check the open NCPs, return true if at least one NCP is open.
2793 sppp_ncp_check(struct sppp *sp)
2797 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2798 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2804 * Re-check the open NCPs and see if we should terminate the link.
2805 * Called by the NCPs during their tlf action handling.
2808 sppp_lcp_check_and_close(struct sppp *sp)
2811 if (sp->pp_phase < PHASE_NETWORK)
2812 /* don't bother, we are already going down */
2815 if (sppp_ncp_check(sp))
2822 *--------------------------------------------------------------------------*
2824 * The IPCP implementation. *
2826 *--------------------------------------------------------------------------*
2831 sppp_ipcp_init(struct sppp *sp)
2835 sp->state[IDX_IPCP] = STATE_INITIAL;
2836 sp->fail_counter[IDX_IPCP] = 0;
2837 sp->pp_seq[IDX_IPCP] = 0;
2838 sp->pp_rseq[IDX_IPCP] = 0;
2839 callout_init(&sp->ch[IDX_IPCP], 1);
2843 sppp_ipcp_up(struct sppp *sp)
2845 sppp_up_event(&ipcp, sp);
2849 sppp_ipcp_down(struct sppp *sp)
2851 sppp_down_event(&ipcp, sp);
2855 sppp_ipcp_open(struct sppp *sp)
2858 u_long myaddr, hisaddr;
2860 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2861 IPCP_MYADDR_DYN | IPCP_VJ);
2864 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2866 * If we don't have his address, this probably means our
2867 * interface doesn't want to talk IP at all. (This could
2868 * be the case if somebody wants to speak only IPX, for
2869 * example.) Don't open IPCP in this case.
2871 if (hisaddr == 0L) {
2872 /* XXX this message should go away */
2874 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2880 * I don't have an assigned address, so i need to
2881 * negotiate my address.
2883 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2884 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2886 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2887 if (sp->confflags & CONF_ENABLE_VJ) {
2888 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2889 sp->ipcp.max_state = MAX_STATES - 1;
2890 sp->ipcp.compress_cid = 1;
2892 sppp_open_event(&ipcp, sp);
2896 sppp_ipcp_close(struct sppp *sp)
2898 sppp_close_event(&ipcp, sp);
2899 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2901 * My address was dynamic, clear it again.
2903 sppp_set_ip_addr(sp, 0L);
2907 sppp_ipcp_TO(void *cookie)
2909 sppp_to_event(&ipcp, (struct sppp *)cookie);
2913 * Analyze a configure request. Return true if it was agreeable, and
2914 * caused action sca, false if it has been rejected or nak'ed, and
2915 * caused action scn. (The return value is used to make the state
2916 * transition decision in the state automaton.)
2919 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2921 u_char *buf, *r, *p;
2922 struct ifnet *ifp = SP2IFP(sp);
2923 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2924 u_long hisaddr, desiredaddr;
2931 * Make sure to allocate a buf that can at least hold a
2932 * conf-nak with an `address' option. We might need it below.
2934 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2938 /* pass 1: see if we can recognize them */
2940 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2943 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2944 len-=p[1], p+=p[1]) {
2946 log(-1, " %s ", sppp_ipcp_opt_name(*p));
2948 case IPCP_OPT_COMPRESSION:
2949 if (!(sp->confflags & CONF_ENABLE_VJ)) {
2950 /* VJ compression administratively disabled */
2952 log(-1, "[locally disabled] ");
2956 * In theory, we should only conf-rej an
2957 * option that is shorter than RFC 1618
2958 * requires (i.e. < 4), and should conf-nak
2959 * anything else that is not VJ. However,
2960 * since our algorithm always uses the
2961 * original option to NAK it with new values,
2962 * things would become more complicated. In
2963 * practice, the only commonly implemented IP
2964 * compression option is VJ anyway, so the
2965 * difference is negligible.
2967 if (len >= 6 && p[1] == 6) {
2969 * correctly formed compression option
2970 * that could be VJ compression
2976 "optlen %d [invalid/unsupported] ",
2979 case IPCP_OPT_ADDRESS:
2980 if (len >= 6 && p[1] == 6) {
2981 /* correctly formed address option */
2985 log(-1, "[invalid] ");
2988 /* Others not supported. */
2993 /* Add the option to rejected list. */
3000 log(-1, " send conf-rej\n");
3001 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
3006 /* pass 2: parse option values */
3007 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
3009 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
3013 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3014 len-=p[1], p+=p[1]) {
3016 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3018 case IPCP_OPT_COMPRESSION:
3019 desiredcomp = p[2] << 8 | p[3];
3020 /* We only support VJ */
3021 if (desiredcomp == IPCP_COMP_VJ) {
3023 log(-1, "VJ [ack] ");
3024 sp->ipcp.flags |= IPCP_VJ;
3025 sl_compress_init(sp->pp_comp, p[4]);
3026 sp->ipcp.max_state = p[4];
3027 sp->ipcp.compress_cid = p[5];
3032 "compproto %#04x [not supported] ",
3034 p[2] = IPCP_COMP_VJ >> 8;
3035 p[3] = IPCP_COMP_VJ;
3036 p[4] = sp->ipcp.max_state;
3037 p[5] = sp->ipcp.compress_cid;
3039 case IPCP_OPT_ADDRESS:
3040 /* This is the address he wants in his end */
3041 desiredaddr = p[2] << 24 | p[3] << 16 |
3043 if (desiredaddr == hisaddr ||
3044 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3046 * Peer's address is same as our value,
3047 * or we have set it to 0.0.0.* to
3048 * indicate that we do not really care,
3049 * this is agreeable. Gonna conf-ack
3053 log(-1, "%s [ack] ",
3054 sppp_dotted_quad(hisaddr));
3055 /* record that we've seen it already */
3056 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3060 * The address wasn't agreeable. This is either
3061 * he sent us 0.0.0.0, asking to assign him an
3062 * address, or he send us another address not
3063 * matching our value. Either case, we gonna
3064 * conf-nak it with our value.
3065 * XXX: we should "rej" if hisaddr == 0
3068 if (desiredaddr == 0)
3069 log(-1, "[addr requested] ");
3071 log(-1, "%s [not agreed] ",
3072 sppp_dotted_quad(desiredaddr));
3075 p[2] = hisaddr >> 24;
3076 p[3] = hisaddr >> 16;
3077 p[4] = hisaddr >> 8;
3081 /* Add the option to nak'ed list. */
3088 * If we are about to conf-ack the request, but haven't seen
3089 * his address so far, gonna conf-nak it instead, with the
3090 * `address' option present and our idea of his address being
3091 * filled in there, to request negotiation of both addresses.
3093 * XXX This can result in an endless req - nak loop if peer
3094 * doesn't want to send us his address. Q: What should we do
3095 * about it? XXX A: implement the max-failure counter.
3097 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3098 buf[0] = IPCP_OPT_ADDRESS;
3100 buf[2] = hisaddr >> 24;
3101 buf[3] = hisaddr >> 16;
3102 buf[4] = hisaddr >> 8;
3106 log(-1, "still need hisaddr ");
3111 log(-1, " send conf-nak\n");
3112 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3115 log(-1, " send conf-ack\n");
3116 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3117 h->ident, origlen, h+1);
3125 * Analyze the IPCP Configure-Reject option list, and adjust our
3129 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3132 struct ifnet *ifp = SP2IFP(sp);
3133 int debug = ifp->if_flags & IFF_DEBUG;
3136 buf = malloc (len, M_TEMP, M_NOWAIT);
3141 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3145 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3146 len -= p[1], p += p[1]) {
3148 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3150 case IPCP_OPT_COMPRESSION:
3151 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3153 case IPCP_OPT_ADDRESS:
3155 * Peer doesn't grok address option. This is
3156 * bad. XXX Should we better give up here?
3157 * XXX We could try old "addresses" option...
3159 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3170 * Analyze the IPCP Configure-NAK option list, and adjust our
3174 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3177 struct ifnet *ifp = SP2IFP(sp);
3178 int debug = ifp->if_flags & IFF_DEBUG;
3183 buf = malloc (len, M_TEMP, M_NOWAIT);
3188 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3192 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3193 len -= p[1], p += p[1]) {
3195 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3197 case IPCP_OPT_COMPRESSION:
3198 if (len >= 6 && p[1] == 6) {
3199 desiredcomp = p[2] << 8 | p[3];
3201 log(-1, "[wantcomp %#04x] ",
3203 if (desiredcomp == IPCP_COMP_VJ) {
3204 sl_compress_init(sp->pp_comp, p[4]);
3205 sp->ipcp.max_state = p[4];
3206 sp->ipcp.compress_cid = p[5];
3208 log(-1, "[agree] ");
3211 ~(1 << IPCP_OPT_COMPRESSION);
3214 case IPCP_OPT_ADDRESS:
3216 * Peer doesn't like our local IP address. See
3217 * if we can do something for him. We'll drop
3218 * him our address then.
3220 if (len >= 6 && p[1] == 6) {
3221 wantaddr = p[2] << 24 | p[3] << 16 |
3223 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3225 log(-1, "[wantaddr %s] ",
3226 sppp_dotted_quad(wantaddr));
3228 * When doing dynamic address assignment,
3229 * we accept his offer. Otherwise, we
3230 * ignore it and thus continue to negotiate
3231 * our already existing value.
3232 * XXX: Bogus, if he said no once, he'll
3233 * just say no again, might as well die.
3235 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3236 sppp_set_ip_addr(sp, wantaddr);
3238 log(-1, "[agree] ");
3239 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3252 sppp_ipcp_tlu(struct sppp *sp)
3254 /* we are up - notify isdn daemon */
3260 sppp_ipcp_tld(struct sppp *sp)
3265 sppp_ipcp_tls(struct sppp *sp)
3267 /* indicate to LCP that it must stay alive */
3268 sp->lcp.protos |= (1 << IDX_IPCP);
3272 sppp_ipcp_tlf(struct sppp *sp)
3274 /* we no longer need LCP */
3275 sp->lcp.protos &= ~(1 << IDX_IPCP);
3276 sppp_lcp_check_and_close(sp);
3280 sppp_ipcp_scr(struct sppp *sp)
3282 char opt[6 /* compression */ + 6 /* address */];
3286 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3287 opt[i++] = IPCP_OPT_COMPRESSION;
3289 opt[i++] = IPCP_COMP_VJ >> 8;
3290 opt[i++] = IPCP_COMP_VJ;
3291 opt[i++] = sp->ipcp.max_state;
3292 opt[i++] = sp->ipcp.compress_cid;
3294 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3295 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3296 opt[i++] = IPCP_OPT_ADDRESS;
3298 opt[i++] = ouraddr >> 24;
3299 opt[i++] = ouraddr >> 16;
3300 opt[i++] = ouraddr >> 8;
3304 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3305 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3309 sppp_ipcp_init(struct sppp *sp)
3314 sppp_ipcp_up(struct sppp *sp)
3319 sppp_ipcp_down(struct sppp *sp)
3324 sppp_ipcp_open(struct sppp *sp)
3329 sppp_ipcp_close(struct sppp *sp)
3334 sppp_ipcp_TO(void *cookie)
3339 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3345 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3350 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3355 sppp_ipcp_tlu(struct sppp *sp)
3360 sppp_ipcp_tld(struct sppp *sp)
3365 sppp_ipcp_tls(struct sppp *sp)
3370 sppp_ipcp_tlf(struct sppp *sp)
3375 sppp_ipcp_scr(struct sppp *sp)
3381 *--------------------------------------------------------------------------*
3383 * The IPv6CP implementation. *
3385 *--------------------------------------------------------------------------*
3390 sppp_ipv6cp_init(struct sppp *sp)
3392 sp->ipv6cp.opts = 0;
3393 sp->ipv6cp.flags = 0;
3394 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3395 sp->fail_counter[IDX_IPV6CP] = 0;
3396 sp->pp_seq[IDX_IPV6CP] = 0;
3397 sp->pp_rseq[IDX_IPV6CP] = 0;
3398 callout_init(&sp->ch[IDX_IPV6CP], 1);
3402 sppp_ipv6cp_up(struct sppp *sp)
3404 sppp_up_event(&ipv6cp, sp);
3408 sppp_ipv6cp_down(struct sppp *sp)
3410 sppp_down_event(&ipv6cp, sp);
3414 sppp_ipv6cp_open(struct sppp *sp)
3417 struct in6_addr myaddr, hisaddr;
3419 #ifdef IPV6CP_MYIFID_DYN
3420 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3422 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3425 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3427 * If we don't have our address, this probably means our
3428 * interface doesn't want to talk IPv6 at all. (This could
3429 * be the case if somebody wants to speak only IPX, for
3430 * example.) Don't open IPv6CP in this case.
3432 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3433 /* XXX this message should go away */
3435 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3440 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3441 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3442 sppp_open_event(&ipv6cp, sp);
3446 sppp_ipv6cp_close(struct sppp *sp)
3448 sppp_close_event(&ipv6cp, sp);
3452 sppp_ipv6cp_TO(void *cookie)
3454 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3458 * Analyze a configure request. Return true if it was agreeable, and
3459 * caused action sca, false if it has been rejected or nak'ed, and
3460 * caused action scn. (The return value is used to make the state
3461 * transition decision in the state automaton.)
3464 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3466 u_char *buf, *r, *p;
3467 struct ifnet *ifp = SP2IFP(sp);
3468 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3469 struct in6_addr myaddr, desiredaddr, suggestaddr;
3472 int collision, nohisaddr;
3473 char ip6buf[INET6_ADDRSTRLEN];
3478 * Make sure to allocate a buf that can at least hold a
3479 * conf-nak with an `address' option. We might need it below.
3481 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3485 /* pass 1: see if we can recognize them */
3487 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3491 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3492 len-=p[1], p+=p[1]) {
3494 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3496 case IPV6CP_OPT_IFID:
3497 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3498 /* correctly formed address option */
3503 log(-1, " [invalid]");
3506 case IPV6CP_OPT_COMPRESSION:
3507 if (len >= 4 && p[1] >= 4) {
3508 /* correctly formed compress option */
3512 log(-1, " [invalid]");
3516 /* Others not supported. */
3521 /* Add the option to rejected list. */
3528 log(-1, " send conf-rej\n");
3529 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3534 /* pass 2: parse option values */
3535 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3537 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3542 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3543 len-=p[1], p+=p[1]) {
3545 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3548 case IPV6CP_OPT_COMPRESSION:
3551 case IPV6CP_OPT_IFID:
3552 bzero(&desiredaddr, sizeof(desiredaddr));
3553 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3554 collision = (bcmp(&desiredaddr.s6_addr[8],
3555 &myaddr.s6_addr[8], 8) == 0);
3556 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3558 desiredaddr.s6_addr16[0] = htons(0xfe80);
3559 (void)in6_setscope(&desiredaddr, SP2IFP(sp), NULL);
3561 if (!collision && !nohisaddr) {
3562 /* no collision, hisaddr known - Conf-Ack */
3567 ip6_sprintf(ip6buf, &desiredaddr),
3568 sppp_cp_type_name(type));
3573 bzero(&suggestaddr, sizeof(suggestaddr));
3574 if (collision && nohisaddr) {
3575 /* collision, hisaddr unknown - Conf-Rej */
3580 * - no collision, hisaddr unknown, or
3581 * - collision, hisaddr known
3582 * Conf-Nak, suggest hisaddr
3585 sppp_suggest_ip6_addr(sp, &suggestaddr);
3586 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3590 ip6_sprintf(ip6buf, &desiredaddr),
3591 sppp_cp_type_name(type));
3594 /* Add the option to nak'ed list. */
3600 if (rlen == 0 && type == CONF_ACK) {
3602 log(-1, " send %s\n", sppp_cp_type_name(type));
3603 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3606 if (type == CONF_ACK)
3607 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3611 log(-1, " send %s suggest %s\n",
3612 sppp_cp_type_name(type),
3613 ip6_sprintf(ip6buf, &suggestaddr));
3615 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3624 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3628 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3631 struct ifnet *ifp = SP2IFP(sp);
3632 int debug = ifp->if_flags & IFF_DEBUG;
3635 buf = malloc (len, M_TEMP, M_NOWAIT);
3640 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3644 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3645 len -= p[1], p += p[1]) {
3647 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3649 case IPV6CP_OPT_IFID:
3651 * Peer doesn't grok address option. This is
3652 * bad. XXX Should we better give up here?
3654 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3657 case IPV6CP_OPT_COMPRESS:
3658 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3670 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3674 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3677 struct ifnet *ifp = SP2IFP(sp);
3678 int debug = ifp->if_flags & IFF_DEBUG;
3679 struct in6_addr suggestaddr;
3680 char ip6buf[INET6_ADDRSTRLEN];
3683 buf = malloc (len, M_TEMP, M_NOWAIT);
3688 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3692 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3693 len -= p[1], p += p[1]) {
3695 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3697 case IPV6CP_OPT_IFID:
3699 * Peer doesn't like our local ifid. See
3700 * if we can do something for him. We'll drop
3701 * him our address then.
3703 if (len < 10 || p[1] != 10)
3705 bzero(&suggestaddr, sizeof(suggestaddr));
3706 suggestaddr.s6_addr16[0] = htons(0xfe80);
3707 (void)in6_setscope(&suggestaddr, SP2IFP(sp), NULL);
3708 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3710 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3712 log(-1, " [suggestaddr %s]",
3713 ip6_sprintf(ip6buf, &suggestaddr));
3714 #ifdef IPV6CP_MYIFID_DYN
3716 * When doing dynamic address assignment,
3717 * we accept his offer.
3719 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3720 struct in6_addr lastsuggest;
3722 * If <suggested myaddr from peer> equals to
3723 * <hisaddr we have suggested last time>,
3724 * we have a collision. generate new random
3727 sppp_suggest_ip6_addr(&lastsuggest);
3728 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3731 log(-1, " [random]");
3732 sppp_gen_ip6_addr(sp, &suggestaddr);
3734 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3736 log(-1, " [agree]");
3737 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3741 * Since we do not do dynamic address assignment,
3742 * we ignore it and thus continue to negotiate
3743 * our already existing value. This can possibly
3744 * go into infinite request-reject loop.
3746 * This is not likely because we normally use
3747 * ifid based on MAC-address.
3748 * If you have no ethernet card on the node, too bad.
3749 * XXX should we use fail_counter?
3754 case IPV6CP_OPT_COMPRESS:
3756 * Peer wants different compression parameters.
3768 sppp_ipv6cp_tlu(struct sppp *sp)
3770 /* we are up - notify isdn daemon */
3776 sppp_ipv6cp_tld(struct sppp *sp)
3781 sppp_ipv6cp_tls(struct sppp *sp)
3783 /* indicate to LCP that it must stay alive */
3784 sp->lcp.protos |= (1 << IDX_IPV6CP);
3788 sppp_ipv6cp_tlf(struct sppp *sp)
3791 #if 0 /* need #if 0 to close IPv6CP properly */
3792 /* we no longer need LCP */
3793 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3794 sppp_lcp_check_and_close(sp);
3799 sppp_ipv6cp_scr(struct sppp *sp)
3801 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3802 struct in6_addr ouraddr;
3805 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3806 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3807 opt[i++] = IPV6CP_OPT_IFID;
3809 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3814 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3815 opt[i++] = IPV6CP_OPT_COMPRESSION;
3817 opt[i++] = 0; /* TBD */
3818 opt[i++] = 0; /* TBD */
3819 /* variable length data may follow */
3823 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3824 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3827 static void sppp_ipv6cp_init(struct sppp *sp)
3831 static void sppp_ipv6cp_up(struct sppp *sp)
3835 static void sppp_ipv6cp_down(struct sppp *sp)
3840 static void sppp_ipv6cp_open(struct sppp *sp)
3844 static void sppp_ipv6cp_close(struct sppp *sp)
3848 static void sppp_ipv6cp_TO(void *sp)
3852 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3857 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3861 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3865 static void sppp_ipv6cp_tlu(struct sppp *sp)
3869 static void sppp_ipv6cp_tld(struct sppp *sp)
3873 static void sppp_ipv6cp_tls(struct sppp *sp)
3877 static void sppp_ipv6cp_tlf(struct sppp *sp)
3881 static void sppp_ipv6cp_scr(struct sppp *sp)
3887 *--------------------------------------------------------------------------*
3889 * The CHAP implementation. *
3891 *--------------------------------------------------------------------------*
3895 * The authentication protocols don't employ a full-fledged state machine as
3896 * the control protocols do, since they do have Open and Close events, but
3897 * not Up and Down, nor are they explicitly terminated. Also, use of the
3898 * authentication protocols may be different in both directions (this makes
3899 * sense, think of a machine that never accepts incoming calls but only
3900 * calls out, it doesn't require the called party to authenticate itself).
3902 * Our state machine for the local authentication protocol (we are requesting
3903 * the peer to authenticate) looks like:
3906 * +--------------------------------------------+
3908 * +--------+ Close +---------+ RCA+
3909 * | |<----------------------------------| |------+
3910 * +--->| Closed | TO* | Opened | sca |
3911 * | | |-----+ +-------| |<-----+
3912 * | +--------+ irc | | +---------+
3918 * | | +------->+ | |
3920 * | +--------+ V | |
3921 * | | |<----+<--------------------+ |
3927 * +------+ +------------------------------------------+
3928 * scn,tld sca,irc,ict,tlu
3933 * Open: LCP reached authentication phase
3934 * Close: LCP reached terminate phase
3936 * RCA+: received reply (pap-req, chap-response), acceptable
3937 * RCN: received reply (pap-req, chap-response), not acceptable
3938 * TO+: timeout with restart counter >= 0
3939 * TO-: timeout with restart counter < 0
3940 * TO*: reschedule timeout for CHAP
3942 * scr: send request packet (none for PAP, chap-challenge)
3943 * sca: send ack packet (pap-ack, chap-success)
3944 * scn: send nak packet (pap-nak, chap-failure)
3945 * ict: initialize re-challenge timer (CHAP only)
3947 * tlu: this-layer-up, LCP reaches network phase
3948 * tld: this-layer-down, LCP enters terminate phase
3950 * Note that in CHAP mode, after sending a new challenge, while the state
3951 * automaton falls back into Req-Sent state, it doesn't signal a tld
3952 * event to LCP, so LCP remains in network phase. Only after not getting
3953 * any response (or after getting an unacceptable response), CHAP closes,
3954 * causing LCP to enter terminate phase.
3956 * With PAP, there is no initial request that can be sent. The peer is
3957 * expected to send one based on the successful negotiation of PAP as
3958 * the authentication protocol during the LCP option negotiation.
3960 * Incoming authentication protocol requests (remote requests
3961 * authentication, we are peer) don't employ a state machine at all,
3962 * they are simply answered. Some peers [Ascend P50 firmware rev
3963 * 4.50] react allergically when sending IPCP requests while they are
3964 * still in authentication phase (thereby violating the standard that
3965 * demands that these NCP packets are to be discarded), so we keep
3966 * track of the peer demanding us to authenticate, and only proceed to
3967 * phase network once we've seen a positive acknowledge for the
3972 * Handle incoming CHAP packets.
3975 sppp_chap_input(struct sppp *sp, struct mbuf *m)
3978 struct lcp_header *h;
3980 u_char *value, *name, digest[AUTHKEYLEN], dsize;
3981 int value_len, name_len;
3984 len = m->m_pkthdr.len;
3988 SPP_FMT "chap invalid packet length: %d bytes\n",
3989 SPP_ARGS(ifp), len);
3992 h = mtod (m, struct lcp_header*);
3993 if (len > ntohs (h->len))
3994 len = ntohs (h->len);
3997 /* challenge, failure and success are his authproto */
3998 case CHAP_CHALLENGE:
3999 value = 1 + (u_char*)(h+1);
4000 value_len = value[-1];
4001 name = value + value_len;
4002 name_len = len - value_len - 5;
4006 SPP_FMT "chap corrupted challenge "
4007 "<%s id=0x%x len=%d",
4009 sppp_auth_type_name(PPP_CHAP, h->type),
4010 h->ident, ntohs(h->len));
4011 sppp_print_bytes((u_char*) (h+1), len-4);
4019 SPP_FMT "chap input <%s id=0x%x len=%d name=",
4021 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
4023 sppp_print_string((char*) name, name_len);
4024 log(-1, " value-size=%d value=", value_len);
4025 sppp_print_bytes(value, value_len);
4029 /* Compute reply value. */
4031 MD5Update(&ctx, &h->ident, 1);
4032 MD5Update(&ctx, sp->myauth.secret,
4033 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
4034 MD5Update(&ctx, value, value_len);
4035 MD5Final(digest, &ctx);
4036 dsize = sizeof digest;
4038 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
4039 sizeof dsize, (const char *)&dsize,
4040 sizeof digest, digest,
4041 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4048 log(LOG_DEBUG, SPP_FMT "chap success",
4052 sppp_print_string((char*)(h + 1), len - 4);
4057 sp->pp_flags &= ~PP_NEEDAUTH;
4058 if (sp->myauth.proto == PPP_CHAP &&
4059 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4060 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
4062 * We are authenticator for CHAP but didn't
4063 * complete yet. Leave it to tlu to proceed
4070 sppp_phase_network(sp);
4075 log(LOG_INFO, SPP_FMT "chap failure",
4079 sppp_print_string((char*)(h + 1), len - 4);
4083 log(LOG_INFO, SPP_FMT "chap failure\n",
4085 /* await LCP shutdown by authenticator */
4088 /* response is my authproto */
4090 value = 1 + (u_char*)(h+1);
4091 value_len = value[-1];
4092 name = value + value_len;
4093 name_len = len - value_len - 5;
4097 SPP_FMT "chap corrupted response "
4098 "<%s id=0x%x len=%d",
4100 sppp_auth_type_name(PPP_CHAP, h->type),
4101 h->ident, ntohs(h->len));
4102 sppp_print_bytes((u_char*)(h+1), len-4);
4107 if (h->ident != sp->confid[IDX_CHAP]) {
4110 SPP_FMT "chap dropping response for old ID "
4111 "(got %d, expected %d)\n",
4113 h->ident, sp->confid[IDX_CHAP]);
4116 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4117 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4118 log(LOG_INFO, SPP_FMT "chap response, his name ",
4120 sppp_print_string(name, name_len);
4121 log(-1, " != expected ");
4122 sppp_print_string(sp->hisauth.name,
4123 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4127 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4128 "<%s id=0x%x len=%d name=",
4130 sppp_state_name(sp->state[IDX_CHAP]),
4131 sppp_auth_type_name(PPP_CHAP, h->type),
4132 h->ident, ntohs (h->len));
4133 sppp_print_string((char*)name, name_len);
4134 log(-1, " value-size=%d value=", value_len);
4135 sppp_print_bytes(value, value_len);
4138 if (value_len != AUTHKEYLEN) {
4141 SPP_FMT "chap bad hash value length: "
4142 "%d bytes, should be %d\n",
4143 SPP_ARGS(ifp), value_len,
4149 MD5Update(&ctx, &h->ident, 1);
4150 MD5Update(&ctx, sp->hisauth.secret,
4151 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4152 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4153 MD5Final(digest, &ctx);
4155 #define FAILMSG "Failed..."
4156 #define SUCCMSG "Welcome!"
4158 if (value_len != sizeof digest ||
4159 bcmp(digest, value, value_len) != 0) {
4160 /* action scn, tld */
4161 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4162 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4167 /* action sca, perhaps tlu */
4168 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4169 sp->state[IDX_CHAP] == STATE_OPENED)
4170 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4171 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4173 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4174 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4180 /* Unknown CHAP packet type -- ignore. */
4182 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4183 "<0x%x id=0x%xh len=%d",
4185 sppp_state_name(sp->state[IDX_CHAP]),
4186 h->type, h->ident, ntohs(h->len));
4187 sppp_print_bytes((u_char*)(h+1), len-4);
4196 sppp_chap_init(struct sppp *sp)
4198 /* Chap doesn't have STATE_INITIAL at all. */
4199 sp->state[IDX_CHAP] = STATE_CLOSED;
4200 sp->fail_counter[IDX_CHAP] = 0;
4201 sp->pp_seq[IDX_CHAP] = 0;
4202 sp->pp_rseq[IDX_CHAP] = 0;
4203 callout_init(&sp->ch[IDX_CHAP], 1);
4207 sppp_chap_open(struct sppp *sp)
4209 if (sp->myauth.proto == PPP_CHAP &&
4210 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4211 /* we are authenticator for CHAP, start it */
4213 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4214 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4216 /* nothing to be done if we are peer, await a challenge */
4220 sppp_chap_close(struct sppp *sp)
4222 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4223 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4227 sppp_chap_TO(void *cookie)
4229 struct sppp *sp = (struct sppp *)cookie;
4234 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4236 sppp_state_name(sp->state[IDX_CHAP]),
4237 sp->rst_counter[IDX_CHAP]);
4239 if (--sp->rst_counter[IDX_CHAP] < 0)
4241 switch (sp->state[IDX_CHAP]) {
4242 case STATE_REQ_SENT:
4244 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4248 /* TO+ (or TO*) event */
4249 switch (sp->state[IDX_CHAP]) {
4252 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4254 case STATE_REQ_SENT:
4256 /* sppp_cp_change_state() will restart the timer */
4257 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4265 sppp_chap_tlu(struct sppp *sp)
4271 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4274 * Some broken CHAP implementations (Conware CoNet, firmware
4275 * 4.0.?) don't want to re-authenticate their CHAP once the
4276 * initial challenge-response exchange has taken place.
4277 * Provide for an option to avoid rechallenges.
4279 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4281 * Compute the re-challenge timeout. This will yield
4282 * a number between 300 and 810 seconds.
4284 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
4285 callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, (void *)sp);
4290 SPP_FMT "chap %s, ",
4292 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4293 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4294 log(-1, "next re-challenge in %d seconds\n", i);
4296 log(-1, "re-challenging suppressed\n");
4300 /* indicate to LCP that we need to be closed down */
4301 sp->lcp.protos |= (1 << IDX_CHAP);
4303 if (sp->pp_flags & PP_NEEDAUTH) {
4305 * Remote is authenticator, but his auth proto didn't
4306 * complete yet. Defer the transition to network
4315 * If we are already in phase network, we are done here. This
4316 * is the case if this is a dummy tlu event after a re-challenge.
4318 if (sp->pp_phase != PHASE_NETWORK)
4319 sppp_phase_network(sp);
4323 sppp_chap_tld(struct sppp *sp)
4328 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4329 callout_stop(&sp->ch[IDX_CHAP]);
4330 sp->lcp.protos &= ~(1 << IDX_CHAP);
4336 sppp_chap_scr(struct sppp *sp)
4341 /* Compute random challenge. */
4342 ch = (u_long *)sp->myauth.challenge;
4343 arc4random_buf(ch, 4 * sizeof(*ch));
4346 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4348 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4349 sizeof clen, (const char *)&clen,
4350 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4351 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4357 *--------------------------------------------------------------------------*
4359 * The PAP implementation. *
4361 *--------------------------------------------------------------------------*
4364 * For PAP, we need to keep a little state also if we are the peer, not the
4365 * authenticator. This is since we don't get a request to authenticate, but
4366 * have to repeatedly authenticate ourself until we got a response (or the
4367 * retry counter is expired).
4371 * Handle incoming PAP packets. */
4373 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4376 struct lcp_header *h;
4378 u_char *name, *passwd, mlen;
4379 int name_len, passwd_len;
4381 len = m->m_pkthdr.len;
4385 SPP_FMT "pap invalid packet length: %d bytes\n",
4386 SPP_ARGS(ifp), len);
4389 h = mtod (m, struct lcp_header*);
4390 if (len > ntohs (h->len))
4391 len = ntohs (h->len);
4393 /* PAP request is my authproto */
4395 name = 1 + (u_char*)(h+1);
4396 name_len = name[-1];
4397 passwd = name + name_len + 1;
4398 if (name_len > len - 6 ||
4399 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4401 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4402 "<%s id=0x%x len=%d",
4404 sppp_auth_type_name(PPP_PAP, h->type),
4405 h->ident, ntohs(h->len));
4406 sppp_print_bytes((u_char*)(h+1), len-4);
4412 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4413 "<%s id=0x%x len=%d name=",
4415 sppp_state_name(sp->state[IDX_PAP]),
4416 sppp_auth_type_name(PPP_PAP, h->type),
4417 h->ident, ntohs(h->len));
4418 sppp_print_string((char*)name, name_len);
4419 log(-1, " passwd=");
4420 sppp_print_string((char*)passwd, passwd_len);
4423 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4424 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4425 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4426 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4427 /* action scn, tld */
4428 mlen = sizeof(FAILMSG) - 1;
4429 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4430 sizeof mlen, (const char *)&mlen,
4431 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4436 /* action sca, perhaps tlu */
4437 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4438 sp->state[IDX_PAP] == STATE_OPENED) {
4439 mlen = sizeof(SUCCMSG) - 1;
4440 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4441 sizeof mlen, (const char *)&mlen,
4442 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4445 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4446 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4451 /* ack and nak are his authproto */
4453 callout_stop(&sp->pap_my_to_ch);
4455 log(LOG_DEBUG, SPP_FMT "pap success",
4457 name_len = *((char *)h);
4458 if (len > 5 && name_len) {
4460 sppp_print_string((char*)(h+1), name_len);
4465 sp->pp_flags &= ~PP_NEEDAUTH;
4466 if (sp->myauth.proto == PPP_PAP &&
4467 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4468 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4470 * We are authenticator for PAP but didn't
4471 * complete yet. Leave it to tlu to proceed
4478 sppp_phase_network(sp);
4482 callout_stop (&sp->pap_my_to_ch);
4484 log(LOG_INFO, SPP_FMT "pap failure",
4486 name_len = *((char *)h);
4487 if (len > 5 && name_len) {
4489 sppp_print_string((char*)(h+1), name_len);
4493 log(LOG_INFO, SPP_FMT "pap failure\n",
4495 /* await LCP shutdown by authenticator */
4499 /* Unknown PAP packet type -- ignore. */
4501 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4502 "<0x%x id=0x%x len=%d",
4504 h->type, h->ident, ntohs(h->len));
4505 sppp_print_bytes((u_char*)(h+1), len-4);
4514 sppp_pap_init(struct sppp *sp)
4516 /* PAP doesn't have STATE_INITIAL at all. */
4517 sp->state[IDX_PAP] = STATE_CLOSED;
4518 sp->fail_counter[IDX_PAP] = 0;
4519 sp->pp_seq[IDX_PAP] = 0;
4520 sp->pp_rseq[IDX_PAP] = 0;
4521 callout_init(&sp->ch[IDX_PAP], 1);
4522 callout_init(&sp->pap_my_to_ch, 1);
4526 sppp_pap_open(struct sppp *sp)
4528 if (sp->hisauth.proto == PPP_PAP &&
4529 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4530 /* we are authenticator for PAP, start our timer */
4531 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4532 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4534 if (sp->myauth.proto == PPP_PAP) {
4535 /* we are peer, send a request, and start a timer */
4537 callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
4538 sppp_pap_my_TO, (void *)sp);
4543 sppp_pap_close(struct sppp *sp)
4545 if (sp->state[IDX_PAP] != STATE_CLOSED)
4546 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4550 * That's the timeout routine if we are authenticator. Since the
4551 * authenticator is basically passive in PAP, we can't do much here.
4554 sppp_pap_TO(void *cookie)
4556 struct sppp *sp = (struct sppp *)cookie;
4561 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4563 sppp_state_name(sp->state[IDX_PAP]),
4564 sp->rst_counter[IDX_PAP]);
4566 if (--sp->rst_counter[IDX_PAP] < 0)
4568 switch (sp->state[IDX_PAP]) {
4569 case STATE_REQ_SENT:
4571 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4575 /* TO+ event, not very much we could do */
4576 switch (sp->state[IDX_PAP]) {
4577 case STATE_REQ_SENT:
4578 /* sppp_cp_change_state() will restart the timer */
4579 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4587 * That's the timeout handler if we are peer. Since the peer is active,
4588 * we need to retransmit our PAP request since it is apparently lost.
4589 * XXX We should impose a max counter.
4592 sppp_pap_my_TO(void *cookie)
4594 struct sppp *sp = (struct sppp *)cookie;
4598 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4607 sppp_pap_tlu(struct sppp *sp)
4611 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4614 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4615 SPP_ARGS(ifp), pap.name);
4618 /* indicate to LCP that we need to be closed down */
4619 sp->lcp.protos |= (1 << IDX_PAP);
4621 if (sp->pp_flags & PP_NEEDAUTH) {
4623 * Remote is authenticator, but his auth proto didn't
4624 * complete yet. Defer the transition to network
4631 sppp_phase_network(sp);
4635 sppp_pap_tld(struct sppp *sp)
4640 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4641 callout_stop (&sp->ch[IDX_PAP]);
4642 callout_stop (&sp->pap_my_to_ch);
4643 sp->lcp.protos &= ~(1 << IDX_PAP);
4649 sppp_pap_scr(struct sppp *sp)
4651 u_char idlen, pwdlen;
4653 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4654 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4655 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4657 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4658 sizeof idlen, (const char *)&idlen,
4659 (size_t)idlen, sp->myauth.name,
4660 sizeof pwdlen, (const char *)&pwdlen,
4661 (size_t)pwdlen, sp->myauth.secret,
4666 * Random miscellaneous functions.
4670 * Send a PAP or CHAP proto packet.
4672 * Varadic function, each of the elements for the ellipsis is of type
4673 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4675 * NOTE: never declare variadic functions with types subject to type
4676 * promotion (i.e. u_char). This is asking for big trouble depending
4677 * on the architecture you are on...
4681 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4682 unsigned int type, unsigned int id,
4686 struct ppp_header *h;
4687 struct lcp_header *lh;
4695 MGETHDR (m, M_NOWAIT, MT_DATA);
4698 m->m_pkthdr.rcvif = 0;
4700 h = mtod (m, struct ppp_header*);
4701 h->address = PPP_ALLSTATIONS; /* broadcast address */
4702 h->control = PPP_UI; /* Unnumbered Info */
4703 h->protocol = htons(cp->proto);
4705 lh = (struct lcp_header*)(h + 1);
4708 p = (u_char*) (lh+1);
4713 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
4714 msg = va_arg(ap, const char *);
4716 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4722 bcopy(msg, p, mlen);
4727 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4728 lh->len = htons (LCP_HEADER_LEN + len);
4731 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4732 SPP_ARGS(ifp), cp->name,
4733 sppp_auth_type_name(cp->proto, lh->type),
4734 lh->ident, ntohs(lh->len));
4735 sppp_print_bytes((u_char*) (lh+1), len);
4738 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
4739 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4743 * Flush interface queue.
4746 sppp_qflush(struct ifqueue *ifq)
4761 * Send keepalive packets, every 10 seconds.
4764 sppp_keepalive(void *dummy)
4766 struct sppp *sp = (struct sppp*)dummy;
4767 struct ifnet *ifp = SP2IFP(sp);
4770 /* Keepalive mode disabled or channel down? */
4771 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4772 ! (ifp->if_drv_flags & IFF_DRV_RUNNING))
4775 if (sp->pp_mode == PP_FR) {
4776 sppp_fr_keepalive (sp);
4780 /* No keepalive in PPP mode if LCP not opened yet. */
4781 if (sp->pp_mode != IFF_CISCO &&
4782 sp->pp_phase < PHASE_AUTHENTICATE)
4785 if (sp->pp_alivecnt == MAXALIVECNT) {
4786 /* No keepalive packets got. Stop the interface. */
4787 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
4789 sppp_qflush (&sp->pp_cpq);
4790 if (sp->pp_mode != IFF_CISCO) {
4792 /* Shut down the PPP link. */
4794 /* Initiate negotiation. XXX */
4798 if (sp->pp_alivecnt <= MAXALIVECNT)
4800 if (sp->pp_mode == IFF_CISCO)
4801 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4802 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4803 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4804 uint32_t nmagic = htonl(sp->lcp.magic);
4805 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4806 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4807 sp->lcp.echoid, 4, &nmagic);
4811 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
4816 * Get both IP addresses.
4819 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4821 struct epoch_tracker et;
4822 struct ifnet *ifp = SP2IFP(sp);
4824 struct sockaddr_in *si, *sm;
4830 * Pick the first AF_INET address from the list,
4831 * aliases don't make any sense on a p2p link anyway.
4834 NET_EPOCH_ENTER(et);
4835 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4836 if (ifa->ifa_addr->sa_family == AF_INET) {
4837 si = (struct sockaddr_in *)ifa->ifa_addr;
4838 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4843 if (si && si->sin_addr.s_addr) {
4844 ssrc = si->sin_addr.s_addr;
4846 *srcmask = ntohl(sm->sin_addr.s_addr);
4849 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4850 if (si && si->sin_addr.s_addr)
4851 ddst = si->sin_addr.s_addr;
4855 if (dst) *dst = ntohl(ddst);
4856 if (src) *src = ntohl(ssrc);
4861 * Set my IP address.
4864 sppp_set_ip_addr(struct sppp *sp, u_long src)
4867 struct epoch_tracker et;
4869 struct sockaddr_in *si;
4870 struct in_ifaddr *ia;
4873 * Pick the first AF_INET address from the list,
4874 * aliases don't make any sense on a p2p link anyway.
4877 NET_EPOCH_ENTER(et);
4878 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4879 if (ifa->ifa_addr->sa_family == AF_INET) {
4880 si = (struct sockaddr_in *)ifa->ifa_addr;
4892 /* delete old route */
4893 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4894 if (debug && error) {
4895 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4896 SPP_ARGS(ifp), error);
4899 /* set new address */
4900 si->sin_addr.s_addr = htonl(src);
4903 LIST_REMOVE(ia, ia_hash);
4904 LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash);
4905 IN_IFADDR_WUNLOCK();
4908 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
4909 if (debug && error) {
4910 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
4911 SPP_ARGS(ifp), error);
4920 * Get both IPv6 addresses.
4923 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4924 struct in6_addr *srcmask)
4926 struct epoch_tracker et;
4927 struct ifnet *ifp = SP2IFP(sp);
4929 struct sockaddr_in6 *si, *sm;
4930 struct in6_addr ssrc, ddst;
4933 bzero(&ssrc, sizeof(ssrc));
4934 bzero(&ddst, sizeof(ddst));
4936 * Pick the first link-local AF_INET6 address from the list,
4937 * aliases don't make any sense on a p2p link anyway.
4940 NET_EPOCH_ENTER(et);
4941 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4942 if (ifa->ifa_addr->sa_family == AF_INET6) {
4943 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4944 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4945 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
4949 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
4950 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
4952 bcopy(&sm->sin6_addr, srcmask,
4957 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
4958 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
4959 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
4963 bcopy(&ddst, dst, sizeof(*dst));
4965 bcopy(&ssrc, src, sizeof(*src));
4969 #ifdef IPV6CP_MYIFID_DYN
4971 * Generate random ifid.
4974 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
4980 * Set my IPv6 address.
4983 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
4986 struct epoch_tracker et;
4988 struct sockaddr_in6 *sin6;
4991 * Pick the first link-local AF_INET6 address from the list,
4992 * aliases don't make any sense on a p2p link anyway.
4996 NET_EPOCH_ENTER(et);
4997 CK_STAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4998 if (ifa->ifa_addr->sa_family == AF_INET6) {
4999 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
5000 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5010 struct sockaddr_in6 new_sin6 = *sin6;
5012 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
5013 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5014 if (debug && error) {
5015 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
5016 " failed, error=%d\n", SPP_ARGS(ifp), error);
5024 * Suggest a candidate address to be used by peer.
5027 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5029 struct in6_addr myaddr;
5032 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5034 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5036 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5037 myaddr.s6_addr[14] ^= 0xff;
5038 myaddr.s6_addr[15] ^= 0xff;
5040 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5041 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5044 bcopy(&myaddr, suggest, sizeof(myaddr));
5049 sppp_params(struct sppp *sp, u_long cmd, void *data)
5052 struct ifreq *ifr = (struct ifreq *)data;
5053 struct spppreq *spr;
5056 if ((spr = malloc(sizeof(struct spppreq), M_TEMP, M_NOWAIT)) == NULL)
5059 * ifr_data_get_ptr(ifr) is supposed to point to a struct spppreq.
5060 * Check the cmd word first before attempting to fetch all the
5063 rv = fueword(ifr_data_get_ptr(ifr), &subcmd);
5069 if (copyin(ifr_data_get_ptr(ifr), spr, sizeof(struct spppreq)) != 0) {
5075 case (u_long)SPPPIOGDEFS:
5076 if (cmd != SIOCGIFGENERIC) {
5081 * We copy over the entire current state, but clean
5082 * out some of the stuff we don't wanna pass up.
5083 * Remember, SIOCGIFGENERIC is unprotected, and can be
5084 * called by any user. No need to ever get PAP or
5085 * CHAP secrets back to userland anyway.
5087 spr->defs.pp_phase = sp->pp_phase;
5088 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5089 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5090 spr->defs.lcp = sp->lcp;
5091 spr->defs.ipcp = sp->ipcp;
5092 spr->defs.ipv6cp = sp->ipv6cp;
5093 spr->defs.myauth = sp->myauth;
5094 spr->defs.hisauth = sp->hisauth;
5095 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5096 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5097 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5098 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5100 * Fixup the LCP timeout value to milliseconds so
5101 * spppcontrol doesn't need to bother about the value
5102 * of "hz". We do the reverse calculation below when
5105 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5106 rv = copyout(spr, ifr_data_get_ptr(ifr),
5107 sizeof(struct spppreq));
5110 case (u_long)SPPPIOSDEFS:
5111 if (cmd != SIOCSIFGENERIC) {
5116 * We have a very specific idea of which fields we
5117 * allow being passed back from userland, so to not
5118 * clobber our current state. For one, we only allow
5119 * setting anything if LCP is in dead or establish
5120 * phase. Once the authentication negotiations
5121 * started, the authentication settings must not be
5122 * changed again. (The administrator can force an
5123 * ifconfig down in order to get LCP back into dead
5126 * Also, we only allow for authentication parameters to be
5129 * XXX Should allow to set or clear pp_flags.
5131 * Finally, if the respective authentication protocol to
5132 * be used is set differently than 0, but the secret is
5133 * passed as all zeros, we don't trash the existing secret.
5134 * This allows an administrator to change the system name
5135 * only without clobbering the secret (which he didn't get
5136 * back in a previous SPPPIOGDEFS call). However, the
5137 * secrets are cleared if the authentication protocol is
5139 if (sp->pp_phase != PHASE_DEAD &&
5140 sp->pp_phase != PHASE_ESTABLISH) {
5145 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5146 spr->defs.myauth.proto != PPP_CHAP) ||
5147 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5148 spr->defs.hisauth.proto != PPP_CHAP)) {
5153 if (spr->defs.myauth.proto == 0)
5154 /* resetting myauth */
5155 bzero(&sp->myauth, sizeof sp->myauth);
5157 /* setting/changing myauth */
5158 sp->myauth.proto = spr->defs.myauth.proto;
5159 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5160 if (spr->defs.myauth.secret[0] != '\0')
5161 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5164 if (spr->defs.hisauth.proto == 0)
5165 /* resetting hisauth */
5166 bzero(&sp->hisauth, sizeof sp->hisauth);
5168 /* setting/changing hisauth */
5169 sp->hisauth.proto = spr->defs.hisauth.proto;
5170 sp->hisauth.flags = spr->defs.hisauth.flags;
5171 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5172 if (spr->defs.hisauth.secret[0] != '\0')
5173 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5176 /* set LCP restart timer timeout */
5177 if (spr->defs.lcp.timeout != 0)
5178 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5179 /* set VJ enable and IPv6 disable flags */
5181 if (spr->defs.enable_vj)
5182 sp->confflags |= CONF_ENABLE_VJ;
5184 sp->confflags &= ~CONF_ENABLE_VJ;
5187 if (spr->defs.enable_ipv6)
5188 sp->confflags |= CONF_ENABLE_IPV6;
5190 sp->confflags &= ~CONF_ENABLE_IPV6;
5205 sppp_phase_network(struct sppp *sp)
5211 sp->pp_phase = PHASE_NETWORK;
5214 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5215 sppp_phase_name(sp->pp_phase));
5217 /* Notify NCPs now. */
5218 for (i = 0; i < IDX_COUNT; i++)
5219 if ((cps[i])->flags & CP_NCP)
5222 /* Send Up events to all NCPs. */
5223 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5224 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5227 /* if no NCP is starting, all this was in vain, close down */
5228 sppp_lcp_check_and_close(sp);
5233 sppp_cp_type_name(u_char type)
5235 static char buf[12];
5237 case CONF_REQ: return "conf-req";
5238 case CONF_ACK: return "conf-ack";
5239 case CONF_NAK: return "conf-nak";
5240 case CONF_REJ: return "conf-rej";
5241 case TERM_REQ: return "term-req";
5242 case TERM_ACK: return "term-ack";
5243 case CODE_REJ: return "code-rej";
5244 case PROTO_REJ: return "proto-rej";
5245 case ECHO_REQ: return "echo-req";
5246 case ECHO_REPLY: return "echo-reply";
5247 case DISC_REQ: return "discard-req";
5249 snprintf (buf, sizeof(buf), "cp/0x%x", type);
5254 sppp_auth_type_name(u_short proto, u_char type)
5256 static char buf[12];
5260 case CHAP_CHALLENGE: return "challenge";
5261 case CHAP_RESPONSE: return "response";
5262 case CHAP_SUCCESS: return "success";
5263 case CHAP_FAILURE: return "failure";
5267 case PAP_REQ: return "req";
5268 case PAP_ACK: return "ack";
5269 case PAP_NAK: return "nak";
5272 snprintf (buf, sizeof(buf), "auth/0x%x", type);
5277 sppp_lcp_opt_name(u_char opt)
5279 static char buf[12];
5281 case LCP_OPT_MRU: return "mru";
5282 case LCP_OPT_ASYNC_MAP: return "async-map";
5283 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5284 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5285 case LCP_OPT_MAGIC: return "magic";
5286 case LCP_OPT_PROTO_COMP: return "proto-comp";
5287 case LCP_OPT_ADDR_COMP: return "addr-comp";
5289 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5295 sppp_ipcp_opt_name(u_char opt)
5297 static char buf[12];
5299 case IPCP_OPT_ADDRESSES: return "addresses";
5300 case IPCP_OPT_COMPRESSION: return "compression";
5301 case IPCP_OPT_ADDRESS: return "address";
5303 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5310 sppp_ipv6cp_opt_name(u_char opt)
5312 static char buf[12];
5314 case IPV6CP_OPT_IFID: return "ifid";
5315 case IPV6CP_OPT_COMPRESSION: return "compression";
5317 sprintf (buf, "0x%x", opt);
5323 sppp_state_name(int state)
5326 case STATE_INITIAL: return "initial";
5327 case STATE_STARTING: return "starting";
5328 case STATE_CLOSED: return "closed";
5329 case STATE_STOPPED: return "stopped";
5330 case STATE_CLOSING: return "closing";
5331 case STATE_STOPPING: return "stopping";
5332 case STATE_REQ_SENT: return "req-sent";
5333 case STATE_ACK_RCVD: return "ack-rcvd";
5334 case STATE_ACK_SENT: return "ack-sent";
5335 case STATE_OPENED: return "opened";
5341 sppp_phase_name(enum ppp_phase phase)
5344 case PHASE_DEAD: return "dead";
5345 case PHASE_ESTABLISH: return "establish";
5346 case PHASE_TERMINATE: return "terminate";
5347 case PHASE_AUTHENTICATE: return "authenticate";
5348 case PHASE_NETWORK: return "network";
5354 sppp_proto_name(u_short proto)
5356 static char buf[12];
5358 case PPP_LCP: return "lcp";
5359 case PPP_IPCP: return "ipcp";
5360 case PPP_PAP: return "pap";
5361 case PPP_CHAP: return "chap";
5362 case PPP_IPV6CP: return "ipv6cp";
5364 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5369 sppp_print_bytes(const u_char *p, u_short len)
5372 log(-1, " %*D", len, p, "-");
5376 sppp_print_string(const char *p, u_short len)
5383 * Print only ASCII chars directly. RFC 1994 recommends
5384 * using only them, but we don't rely on it. */
5385 if (c < ' ' || c > '~')
5386 log(-1, "\\x%x", c);
5394 sppp_dotted_quad(u_long addr)
5397 sprintf(s, "%d.%d.%d.%d",
5398 (int)((addr >> 24) & 0xff),
5399 (int)((addr >> 16) & 0xff),
5400 (int)((addr >> 8) & 0xff),
5401 (int)(addr & 0xff));
5407 sppp_strnlen(u_char *p, int max)
5411 for (len = 0; len < max && *p; ++p)
5416 /* a dummy, used to drop uninteresting events */
5418 sppp_null(struct sppp *unused)
5420 /* do just nothing */