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 if (!callout_drain(&sp->keepalive_callout))
1064 callout_stop(&sp->keepalive_callout);
1066 for (i = 0; i < IDX_COUNT; i++) {
1067 if (!callout_drain(&sp->ch[i]))
1068 callout_stop(&sp->ch[i]);
1070 if (!callout_drain(&sp->pap_my_to_ch))
1071 callout_stop(&sp->pap_my_to_ch);
1072 mtx_destroy(&sp->pp_cpq.ifq_mtx);
1073 mtx_destroy(&sp->pp_fastq.ifq_mtx);
1074 mtx_destroy(&sp->mtx);
1078 * Flush the interface output queue.
1081 sppp_flush_unlocked(struct ifnet *ifp)
1083 struct sppp *sp = IFP2SP(ifp);
1085 sppp_qflush ((struct ifqueue *)&SP2IFP(sp)->if_snd);
1086 sppp_qflush (&sp->pp_fastq);
1087 sppp_qflush (&sp->pp_cpq);
1091 sppp_flush(struct ifnet *ifp)
1093 struct sppp *sp = IFP2SP(ifp);
1096 sppp_flush_unlocked (ifp);
1101 * Check if the output queue is empty.
1104 sppp_isempty(struct ifnet *ifp)
1106 struct sppp *sp = IFP2SP(ifp);
1110 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
1111 !SP2IFP(sp)->if_snd.ifq_head;
1117 * Get next packet to send.
1120 sppp_dequeue(struct ifnet *ifp)
1122 struct sppp *sp = IFP2SP(ifp);
1127 * Process only the control protocol queue until we have at
1128 * least one NCP open.
1130 * Do always serve all three queues in Cisco mode.
1132 IF_DEQUEUE(&sp->pp_cpq, m);
1134 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO ||
1135 sp->pp_mode == PP_FR)) {
1136 IF_DEQUEUE(&sp->pp_fastq, m);
1138 IF_DEQUEUE (&SP2IFP(sp)->if_snd, m);
1145 * Pick the next packet, do not remove it from the queue.
1148 sppp_pick(struct ifnet *ifp)
1150 struct sppp *sp = IFP2SP(ifp);
1155 m = sp->pp_cpq.ifq_head;
1157 (sp->pp_phase == PHASE_NETWORK ||
1158 sp->pp_mode == IFF_CISCO ||
1159 sp->pp_mode == PP_FR))
1160 if ((m = sp->pp_fastq.ifq_head) == NULL)
1161 m = SP2IFP(sp)->if_snd.ifq_head;
1167 * Process an ioctl request. Called on low priority level.
1170 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1172 struct ifreq *ifr = (struct ifreq*) data;
1173 struct sppp *sp = IFP2SP(ifp);
1174 int rv, going_up, going_down, newmode;
1183 /* set the interface "up" when assigning an IP address */
1184 ifp->if_flags |= IFF_UP;
1188 going_up = ifp->if_flags & IFF_UP &&
1189 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0;
1190 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1191 ifp->if_drv_flags & IFF_DRV_RUNNING;
1193 newmode = ifp->if_flags & IFF_PASSIVE;
1195 newmode = ifp->if_flags & IFF_AUTO;
1197 newmode = ifp->if_flags & IFF_CISCO;
1198 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1199 ifp->if_flags |= newmode;
1202 newmode = sp->pp_flags & PP_FR;
1204 if (newmode != sp->pp_mode) {
1207 going_up = ifp->if_drv_flags & IFF_DRV_RUNNING;
1211 if (sp->pp_mode != IFF_CISCO &&
1212 sp->pp_mode != PP_FR)
1214 else if (sp->pp_tlf)
1216 sppp_flush_unlocked(ifp);
1217 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1218 sp->pp_mode = newmode;
1222 if (sp->pp_mode != IFF_CISCO &&
1223 sp->pp_mode != PP_FR)
1225 sp->pp_mode = newmode;
1226 if (sp->pp_mode == 0) {
1227 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1230 if ((sp->pp_mode == IFF_CISCO) ||
1231 (sp->pp_mode == PP_FR)) {
1234 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1242 #define ifr_mtu ifr_metric
1245 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1247 ifp->if_mtu = ifr->ifr_mtu;
1252 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1254 ifp->if_mtu = *(short*)data;
1259 ifr->ifr_mtu = ifp->if_mtu;
1264 *(short*)data = ifp->if_mtu;
1271 case SIOCGIFGENERIC:
1272 case SIOCSIFGENERIC:
1273 rv = sppp_params(sp, cmd, data);
1284 * Cisco framing implementation.
1288 * Handle incoming Cisco keepalive protocol packets.
1291 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1294 struct cisco_packet *h;
1297 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1300 SPP_FMT "cisco invalid packet length: %d bytes\n",
1301 SPP_ARGS(ifp), m->m_pkthdr.len);
1304 h = mtod (m, struct cisco_packet*);
1307 SPP_FMT "cisco input: %d bytes "
1308 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1309 SPP_ARGS(ifp), m->m_pkthdr.len,
1310 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1311 (u_int)h->time0, (u_int)h->time1);
1312 switch (ntohl (h->type)) {
1315 log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n",
1316 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1318 case CISCO_ADDR_REPLY:
1319 /* Reply on address request, ignore */
1321 case CISCO_KEEPALIVE_REQ:
1322 sp->pp_alivecnt = 0;
1323 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1324 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1325 /* Local and remote sequence numbers are equal.
1326 * Probably, the line is in loopback mode. */
1327 if (sp->pp_loopcnt >= MAXALIVECNT) {
1328 printf (SPP_FMT "loopback\n",
1331 if (ifp->if_flags & IFF_UP) {
1333 sppp_qflush (&sp->pp_cpq);
1338 /* Generate new local sequence number */
1339 sp->pp_seq[IDX_LCP] = random();
1343 if (! (ifp->if_flags & IFF_UP) &&
1344 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1346 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1349 case CISCO_ADDR_REQ:
1350 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1352 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1358 * Send Cisco keepalive packet.
1361 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1364 struct ppp_header *h;
1365 struct cisco_packet *ch;
1369 getmicrouptime(&tv);
1371 MGETHDR (m, M_NOWAIT, MT_DATA);
1374 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1375 m->m_pkthdr.rcvif = 0;
1377 h = mtod (m, struct ppp_header*);
1378 h->address = CISCO_MULTICAST;
1380 h->protocol = htons (CISCO_KEEPALIVE);
1382 ch = (struct cisco_packet*) (h + 1);
1383 ch->type = htonl (type);
1384 ch->par1 = htonl (par1);
1385 ch->par2 = htonl (par2);
1388 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1389 ch->time1 = htons ((u_short) tv.tv_sec);
1393 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1394 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1395 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1397 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1398 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1402 * PPP protocol implementation.
1406 * Send PPP control protocol packet.
1409 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1410 u_char ident, u_short len, void *data)
1413 struct ppp_header *h;
1414 struct lcp_header *lh;
1417 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1418 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1419 MGETHDR (m, M_NOWAIT, MT_DATA);
1422 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1423 m->m_pkthdr.rcvif = 0;
1425 h = mtod (m, struct ppp_header*);
1426 h->address = PPP_ALLSTATIONS; /* broadcast address */
1427 h->control = PPP_UI; /* Unnumbered Info */
1428 h->protocol = htons (proto); /* Link Control Protocol */
1430 lh = (struct lcp_header*) (h + 1);
1433 lh->len = htons (LCP_HEADER_LEN + len);
1435 bcopy (data, lh+1, len);
1438 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1440 sppp_proto_name(proto),
1441 sppp_cp_type_name (lh->type), lh->ident,
1443 sppp_print_bytes ((u_char*) (lh+1), len);
1446 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1447 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
1451 * Handle incoming PPP control protocol packets.
1454 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1457 struct lcp_header *h;
1458 int len = m->m_pkthdr.len;
1465 SPP_FMT "%s invalid packet length: %d bytes\n",
1466 SPP_ARGS(ifp), cp->name, len);
1469 h = mtod (m, struct lcp_header*);
1472 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1473 SPP_ARGS(ifp), cp->name,
1474 sppp_state_name(sp->state[cp->protoidx]),
1475 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1476 sppp_print_bytes ((u_char*) (h+1), len-4);
1479 if (len > ntohs (h->len))
1480 len = ntohs (h->len);
1481 p = (u_char *)(h + 1);
1486 log(-1, SPP_FMT "%s invalid conf-req length %d\n",
1487 SPP_ARGS(ifp), cp->name,
1489 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1492 /* handle states where RCR doesn't get a SCA/SCN */
1493 switch (sp->state[cp->protoidx]) {
1495 case STATE_STOPPING:
1498 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1502 rv = (cp->RCR)(sp, h, len);
1503 switch (sp->state[cp->protoidx]) {
1508 case STATE_ACK_SENT:
1509 case STATE_REQ_SENT:
1511 * sppp_cp_change_state() have the side effect of
1512 * restarting the timeouts. We want to avoid that
1513 * if the state don't change, otherwise we won't
1514 * ever timeout and resend a configuration request
1517 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1520 sppp_cp_change_state(cp, sp, rv?
1521 STATE_ACK_SENT: STATE_REQ_SENT);
1524 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1526 sppp_cp_change_state(cp, sp, rv?
1527 STATE_ACK_SENT: STATE_REQ_SENT);
1529 case STATE_ACK_RCVD:
1531 sppp_cp_change_state(cp, sp, STATE_OPENED);
1533 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1538 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1541 printf(SPP_FMT "%s illegal %s in state %s\n",
1542 SPP_ARGS(ifp), cp->name,
1543 sppp_cp_type_name(h->type),
1544 sppp_state_name(sp->state[cp->protoidx]));
1545 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1549 if (h->ident != sp->confid[cp->protoidx]) {
1551 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1552 SPP_ARGS(ifp), cp->name,
1553 h->ident, sp->confid[cp->protoidx]);
1554 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1557 switch (sp->state[cp->protoidx]) {
1560 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1563 case STATE_STOPPING:
1565 case STATE_REQ_SENT:
1566 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1567 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1572 case STATE_ACK_RCVD:
1574 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1576 case STATE_ACK_SENT:
1577 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1578 sppp_cp_change_state(cp, sp, STATE_OPENED);
1580 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1581 SPP_ARGS(ifp), cp->name);
1585 printf(SPP_FMT "%s illegal %s in state %s\n",
1586 SPP_ARGS(ifp), cp->name,
1587 sppp_cp_type_name(h->type),
1588 sppp_state_name(sp->state[cp->protoidx]));
1589 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1594 if (h->ident != sp->confid[cp->protoidx]) {
1596 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1597 SPP_ARGS(ifp), cp->name,
1598 h->ident, sp->confid[cp->protoidx]);
1599 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1602 if (h->type == CONF_NAK)
1603 (cp->RCN_nak)(sp, h, len);
1605 (cp->RCN_rej)(sp, h, len);
1607 switch (sp->state[cp->protoidx]) {
1610 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1612 case STATE_REQ_SENT:
1613 case STATE_ACK_SENT:
1614 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1616 * Slow things down a bit if we think we might be
1617 * in loopback. Depend on the timeout to send the
1618 * next configuration request.
1627 case STATE_ACK_RCVD:
1628 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1632 case STATE_STOPPING:
1635 printf(SPP_FMT "%s illegal %s in state %s\n",
1636 SPP_ARGS(ifp), cp->name,
1637 sppp_cp_type_name(h->type),
1638 sppp_state_name(sp->state[cp->protoidx]));
1639 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1644 switch (sp->state[cp->protoidx]) {
1645 case STATE_ACK_RCVD:
1646 case STATE_ACK_SENT:
1647 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1652 case STATE_STOPPING:
1653 case STATE_REQ_SENT:
1655 /* Send Terminate-Ack packet. */
1657 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1658 SPP_ARGS(ifp), cp->name);
1659 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1663 sp->rst_counter[cp->protoidx] = 0;
1664 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1668 printf(SPP_FMT "%s illegal %s in state %s\n",
1669 SPP_ARGS(ifp), cp->name,
1670 sppp_cp_type_name(h->type),
1671 sppp_state_name(sp->state[cp->protoidx]));
1672 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1676 switch (sp->state[cp->protoidx]) {
1679 case STATE_REQ_SENT:
1680 case STATE_ACK_SENT:
1683 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1686 case STATE_STOPPING:
1687 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1690 case STATE_ACK_RCVD:
1691 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1696 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1699 printf(SPP_FMT "%s illegal %s in state %s\n",
1700 SPP_ARGS(ifp), cp->name,
1701 sppp_cp_type_name(h->type),
1702 sppp_state_name(sp->state[cp->protoidx]));
1703 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1707 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1709 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1710 "danger will robinson\n",
1711 SPP_ARGS(ifp), cp->name,
1712 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1713 switch (sp->state[cp->protoidx]) {
1716 case STATE_REQ_SENT:
1717 case STATE_ACK_SENT:
1719 case STATE_STOPPING:
1722 case STATE_ACK_RCVD:
1723 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1726 printf(SPP_FMT "%s illegal %s in state %s\n",
1727 SPP_ARGS(ifp), cp->name,
1728 sppp_cp_type_name(h->type),
1729 sppp_state_name(sp->state[cp->protoidx]));
1730 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1736 const struct cp *upper;
1742 proto = ntohs(*((u_int16_t *)p));
1743 for (i = 0; i < IDX_COUNT; i++) {
1744 if (cps[i]->proto == proto) {
1752 if (catastrophic || debug)
1753 log(catastrophic? LOG_INFO: LOG_DEBUG,
1754 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1755 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1756 sppp_cp_type_name(h->type), proto,
1757 upper ? upper->name : "unknown",
1758 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1761 * if we got RXJ+ against conf-req, the peer does not implement
1762 * this particular protocol type. terminate the protocol.
1764 if (upper && !catastrophic) {
1765 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1771 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1772 switch (sp->state[cp->protoidx]) {
1775 case STATE_REQ_SENT:
1776 case STATE_ACK_SENT:
1778 case STATE_STOPPING:
1781 case STATE_ACK_RCVD:
1782 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1785 printf(SPP_FMT "%s illegal %s in state %s\n",
1786 SPP_ARGS(ifp), cp->name,
1787 sppp_cp_type_name(h->type),
1788 sppp_state_name(sp->state[cp->protoidx]));
1789 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1794 if (cp->proto != PPP_LCP)
1796 /* Discard the packet. */
1799 if (cp->proto != PPP_LCP)
1801 if (sp->state[cp->protoidx] != STATE_OPENED) {
1803 log(-1, SPP_FMT "lcp echo req but lcp closed\n",
1805 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1810 log(-1, SPP_FMT "invalid lcp echo request "
1811 "packet length: %d bytes\n",
1812 SPP_ARGS(ifp), len);
1815 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1816 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1817 /* Line loopback mode detected. */
1818 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1819 sp->pp_loopcnt = MAXALIVECNT * 5;
1821 sppp_qflush (&sp->pp_cpq);
1823 /* Shut down the PPP link. */
1829 *(long*)(h+1) = htonl (sp->lcp.magic);
1831 log(-1, SPP_FMT "got lcp echo req, sending echo rep\n",
1833 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1836 if (cp->proto != PPP_LCP)
1838 if (h->ident != sp->lcp.echoid) {
1839 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1844 log(-1, SPP_FMT "lcp invalid echo reply "
1845 "packet length: %d bytes\n",
1846 SPP_ARGS(ifp), len);
1850 log(-1, SPP_FMT "lcp got echo rep\n",
1852 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1853 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1854 sp->pp_alivecnt = 0;
1857 /* Unknown packet type -- send Code-Reject packet. */
1860 log(-1, SPP_FMT "%s send code-rej for 0x%x\n",
1861 SPP_ARGS(ifp), cp->name, h->type);
1862 sppp_cp_send(sp, cp->proto, CODE_REJ,
1863 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1864 if_inc_counter(ifp, IFCOUNTER_IERRORS, 1);
1870 * The generic part of all Up/Down/Open/Close/TO event handlers.
1871 * Basically, the state transition handling in the automaton.
1874 sppp_up_event(const struct cp *cp, struct sppp *sp)
1879 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1880 SPP_ARGS(ifp), cp->name,
1881 sppp_state_name(sp->state[cp->protoidx]));
1883 switch (sp->state[cp->protoidx]) {
1885 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1887 case STATE_STARTING:
1888 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1890 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1893 printf(SPP_FMT "%s illegal up in state %s\n",
1894 SPP_ARGS(ifp), cp->name,
1895 sppp_state_name(sp->state[cp->protoidx]));
1900 sppp_down_event(const struct cp *cp, struct sppp *sp)
1905 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1906 SPP_ARGS(ifp), cp->name,
1907 sppp_state_name(sp->state[cp->protoidx]));
1909 switch (sp->state[cp->protoidx]) {
1912 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1915 sppp_cp_change_state(cp, sp, STATE_STARTING);
1918 case STATE_STOPPING:
1919 case STATE_REQ_SENT:
1920 case STATE_ACK_RCVD:
1921 case STATE_ACK_SENT:
1922 sppp_cp_change_state(cp, sp, STATE_STARTING);
1926 sppp_cp_change_state(cp, sp, STATE_STARTING);
1929 printf(SPP_FMT "%s illegal down in state %s\n",
1930 SPP_ARGS(ifp), cp->name,
1931 sppp_state_name(sp->state[cp->protoidx]));
1937 sppp_open_event(const struct cp *cp, struct sppp *sp)
1942 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1943 SPP_ARGS(ifp), cp->name,
1944 sppp_state_name(sp->state[cp->protoidx]));
1946 switch (sp->state[cp->protoidx]) {
1948 sppp_cp_change_state(cp, sp, STATE_STARTING);
1951 case STATE_STARTING:
1954 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1956 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1960 * Try escaping stopped state. This seems to bite
1961 * people occasionally, in particular for IPCP,
1962 * presumably following previous IPCP negotiation
1963 * aborts. Somehow, we must have missed a Down event
1964 * which would have caused a transition into starting
1965 * state, so as a bandaid we force the Down event now.
1966 * This effectively implements (something like the)
1967 * `restart' option mentioned in the state transition
1968 * table of RFC 1661.
1970 sppp_cp_change_state(cp, sp, STATE_STARTING);
1973 case STATE_STOPPING:
1974 case STATE_REQ_SENT:
1975 case STATE_ACK_RCVD:
1976 case STATE_ACK_SENT:
1980 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1987 sppp_close_event(const struct cp *cp, struct sppp *sp)
1992 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
1993 SPP_ARGS(ifp), cp->name,
1994 sppp_state_name(sp->state[cp->protoidx]));
1996 switch (sp->state[cp->protoidx]) {
2001 case STATE_STARTING:
2002 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2006 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2008 case STATE_STOPPING:
2009 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2014 case STATE_REQ_SENT:
2015 case STATE_ACK_RCVD:
2016 case STATE_ACK_SENT:
2017 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2018 sppp_cp_send(sp, cp->proto, TERM_REQ,
2019 ++sp->pp_seq[cp->protoidx], 0, 0);
2020 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2026 sppp_to_event(const struct cp *cp, struct sppp *sp)
2032 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
2033 SPP_ARGS(ifp), cp->name,
2034 sppp_state_name(sp->state[cp->protoidx]),
2035 sp->rst_counter[cp->protoidx]);
2037 if (--sp->rst_counter[cp->protoidx] < 0)
2039 switch (sp->state[cp->protoidx]) {
2041 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2044 case STATE_STOPPING:
2045 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2048 case STATE_REQ_SENT:
2049 case STATE_ACK_RCVD:
2050 case STATE_ACK_SENT:
2051 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2057 switch (sp->state[cp->protoidx]) {
2059 case STATE_STOPPING:
2060 sppp_cp_send(sp, cp->proto, TERM_REQ,
2061 ++sp->pp_seq[cp->protoidx], 0, 0);
2062 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2063 cp->TO, (void *)sp);
2065 case STATE_REQ_SENT:
2066 case STATE_ACK_RCVD:
2068 /* sppp_cp_change_state() will restart the timer */
2069 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2071 case STATE_ACK_SENT:
2073 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2074 cp->TO, (void *)sp);
2082 * Change the state of a control protocol in the state automaton.
2083 * Takes care of starting/stopping the restart timer.
2086 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2088 sp->state[cp->protoidx] = newstate;
2090 callout_stop (&sp->ch[cp->protoidx]);
2094 case STATE_STARTING:
2100 case STATE_STOPPING:
2101 case STATE_REQ_SENT:
2102 case STATE_ACK_RCVD:
2103 case STATE_ACK_SENT:
2104 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2105 cp->TO, (void *)sp);
2111 *--------------------------------------------------------------------------*
2113 * The LCP implementation. *
2115 *--------------------------------------------------------------------------*
2118 sppp_pp_up(struct sppp *sp)
2126 sppp_pp_down(struct sppp *sp)
2134 sppp_lcp_init(struct sppp *sp)
2136 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2138 sp->state[IDX_LCP] = STATE_INITIAL;
2139 sp->fail_counter[IDX_LCP] = 0;
2140 sp->pp_seq[IDX_LCP] = 0;
2141 sp->pp_rseq[IDX_LCP] = 0;
2143 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2145 /* Note that these values are relevant for all control protocols */
2146 sp->lcp.timeout = 3 * hz;
2147 sp->lcp.max_terminate = 2;
2148 sp->lcp.max_configure = 10;
2149 sp->lcp.max_failure = 10;
2150 callout_init(&sp->ch[IDX_LCP], 1);
2154 sppp_lcp_up(struct sppp *sp)
2158 sp->pp_alivecnt = 0;
2159 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2162 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2164 * If we are authenticator, negotiate LCP_AUTH
2166 if (sp->hisauth.proto != 0)
2167 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2169 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2170 sp->pp_flags &= ~PP_NEEDAUTH;
2172 * If this interface is passive or dial-on-demand, and we are
2173 * still in Initial state, it means we've got an incoming
2174 * call. Activate the interface.
2176 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2179 SPP_FMT "Up event", SPP_ARGS(ifp));
2180 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2181 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2183 log(-1, "(incoming call)\n");
2184 sp->pp_flags |= PP_CALLIN;
2188 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2189 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2190 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2194 sppp_up_event(&lcp, sp);
2198 sppp_lcp_down(struct sppp *sp)
2202 sppp_down_event(&lcp, sp);
2205 * If this is neither a dial-on-demand nor a passive
2206 * interface, simulate an ``ifconfig down'' action, so the
2207 * administrator can force a redial by another ``ifconfig
2208 * up''. XXX For leased line operation, should we immediately
2209 * try to reopen the connection here?
2211 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2213 SPP_FMT "Down event, taking interface down.\n",
2219 SPP_FMT "Down event (carrier loss)\n",
2221 sp->pp_flags &= ~PP_CALLIN;
2222 if (sp->state[IDX_LCP] != STATE_INITIAL)
2224 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2229 sppp_lcp_open(struct sppp *sp)
2231 sppp_open_event(&lcp, sp);
2235 sppp_lcp_close(struct sppp *sp)
2237 sppp_close_event(&lcp, sp);
2241 sppp_lcp_TO(void *cookie)
2243 sppp_to_event(&lcp, (struct sppp *)cookie);
2247 * Analyze a configure request. Return true if it was agreeable, and
2248 * caused action sca, false if it has been rejected or nak'ed, and
2249 * caused action scn. (The return value is used to make the state
2250 * transition decision in the state automaton.)
2253 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2256 u_char *buf, *r, *p;
2263 buf = r = malloc (len, M_TEMP, M_NOWAIT);
2268 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2271 /* pass 1: check for things that need to be rejected */
2273 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2274 len-=p[1], p+=p[1]) {
2276 log(-1, " %s ", sppp_lcp_opt_name(*p));
2280 if (len >= 6 && p[1] == 6)
2283 log(-1, "[invalid] ");
2285 case LCP_OPT_ASYNC_MAP:
2286 /* Async control character map. */
2287 if (len >= 6 && p[1] == 6)
2290 log(-1, "[invalid] ");
2293 /* Maximum receive unit. */
2294 if (len >= 4 && p[1] == 4)
2297 log(-1, "[invalid] ");
2299 case LCP_OPT_AUTH_PROTO:
2302 log(-1, "[invalid] ");
2305 authproto = (p[2] << 8) + p[3];
2306 if (authproto == PPP_CHAP && p[1] != 5) {
2308 log(-1, "[invalid chap len] ");
2311 if (sp->myauth.proto == 0) {
2312 /* we are not configured to do auth */
2314 log(-1, "[not configured] ");
2318 * Remote want us to authenticate, remember this,
2319 * so we stay in PHASE_AUTHENTICATE after LCP got
2322 sp->pp_flags |= PP_NEEDAUTH;
2325 /* Others not supported. */
2330 /* Add the option to rejected list. */
2337 log(-1, " send conf-rej\n");
2338 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2344 * pass 2: check for option values that are unacceptable and
2345 * thus require to be nak'ed.
2348 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2353 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2354 len-=p[1], p+=p[1]) {
2356 log(-1, " %s ", sppp_lcp_opt_name(*p));
2359 /* Magic number -- extract. */
2360 nmagic = (u_long)p[2] << 24 |
2361 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2362 if (nmagic != sp->lcp.magic) {
2365 log(-1, "0x%lx ", nmagic);
2368 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2369 log(-1, "[glitch] ");
2372 * We negate our magic here, and NAK it. If
2373 * we see it later in an NAK packet, we
2374 * suggest a new one.
2376 nmagic = ~sp->lcp.magic;
2378 p[2] = nmagic >> 24;
2379 p[3] = nmagic >> 16;
2384 case LCP_OPT_ASYNC_MAP:
2386 * Async control character map -- just ignore it.
2388 * Quote from RFC 1662, chapter 6:
2389 * To enable this functionality, synchronous PPP
2390 * implementations MUST always respond to the
2391 * Async-Control-Character-Map Configuration
2392 * Option with the LCP Configure-Ack. However,
2393 * acceptance of the Configuration Option does
2394 * not imply that the synchronous implementation
2395 * will do any ACCM mapping. Instead, all such
2396 * octet mapping will be performed by the
2397 * asynchronous-to-synchronous converter.
2403 * Maximum receive unit. Always agreeable,
2404 * but ignored by now.
2406 sp->lcp.their_mru = p[2] * 256 + p[3];
2408 log(-1, "%lu ", sp->lcp.their_mru);
2411 case LCP_OPT_AUTH_PROTO:
2412 authproto = (p[2] << 8) + p[3];
2413 if (sp->myauth.proto != authproto) {
2414 /* not agreed, nak */
2416 log(-1, "[mine %s != his %s] ",
2417 sppp_proto_name(sp->hisauth.proto),
2418 sppp_proto_name(authproto));
2419 p[2] = sp->myauth.proto >> 8;
2420 p[3] = sp->myauth.proto;
2423 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2425 log(-1, "[chap not MD5] ");
2431 /* Add the option to nak'ed list. */
2438 * Local and remote magics equal -- loopback?
2440 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2441 if (sp->pp_loopcnt == MAXALIVECNT*5)
2442 printf (SPP_FMT "loopback\n",
2444 if (ifp->if_flags & IFF_UP) {
2446 sppp_qflush(&sp->pp_cpq);
2451 } else if (!sp->pp_loopcnt &&
2452 ++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2454 log(-1, " max_failure (%d) exceeded, "
2456 sp->lcp.max_failure);
2457 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2460 log(-1, " send conf-nak\n");
2461 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2465 log(-1, " send conf-ack\n");
2466 sp->fail_counter[IDX_LCP] = 0;
2468 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2469 h->ident, origlen, h+1);
2477 * Analyze the LCP Configure-Reject option list, and adjust our
2481 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2487 buf = malloc (len, M_TEMP, M_NOWAIT);
2492 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2496 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2497 len -= p[1], p += p[1]) {
2499 log(-1, " %s ", sppp_lcp_opt_name(*p));
2502 /* Magic number -- can't use it, use 0 */
2503 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2508 * Should not be rejected anyway, since we only
2509 * negotiate a MRU if explicitly requested by
2512 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2514 case LCP_OPT_AUTH_PROTO:
2516 * Peer doesn't want to authenticate himself,
2517 * deny unless this is a dialout call, and
2518 * AUTHFLAG_NOCALLOUT is set.
2520 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2521 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2523 log(-1, "[don't insist on auth "
2525 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2529 log(-1, "[access denied]\n");
2541 * Analyze the LCP Configure-NAK option list, and adjust our
2545 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2552 buf = malloc (len, M_TEMP, M_NOWAIT);
2557 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2561 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2562 len -= p[1], p += p[1]) {
2564 log(-1, " %s ", sppp_lcp_opt_name(*p));
2567 /* Magic number -- renegotiate */
2568 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2569 len >= 6 && p[1] == 6) {
2570 magic = (u_long)p[2] << 24 |
2571 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2573 * If the remote magic is our negated one,
2574 * this looks like a loopback problem.
2575 * Suggest a new magic to make sure.
2577 if (magic == ~sp->lcp.magic) {
2579 log(-1, "magic glitch ");
2580 sp->lcp.magic = random();
2582 sp->lcp.magic = magic;
2584 log(-1, "%lu ", magic);
2590 * Peer wants to advise us to negotiate an MRU.
2591 * Agree on it if it's reasonable, or use
2592 * default otherwise.
2594 if (len >= 4 && p[1] == 4) {
2595 u_int mru = p[2] * 256 + p[3];
2597 log(-1, "%d ", mru);
2598 if (mru < PP_MTU || mru > PP_MAX_MRU)
2601 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2604 case LCP_OPT_AUTH_PROTO:
2606 * Peer doesn't like our authentication method,
2610 log(-1, "[access denied]\n");
2622 sppp_lcp_tlu(struct sppp *sp)
2629 if (! (ifp->if_flags & IFF_UP) &&
2630 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2631 /* Coming out of loopback mode. */
2633 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2636 for (i = 0; i < IDX_COUNT; i++)
2637 if ((cps[i])->flags & CP_QUAL)
2640 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2641 (sp->pp_flags & PP_NEEDAUTH) != 0)
2642 sp->pp_phase = PHASE_AUTHENTICATE;
2644 sp->pp_phase = PHASE_NETWORK;
2647 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2648 sppp_phase_name(sp->pp_phase));
2651 * Open all authentication protocols. This is even required
2652 * if we already proceeded to network phase, since it might be
2653 * that remote wants us to authenticate, so we might have to
2654 * send a PAP request. Undesired authentication protocols
2655 * don't do anything when they get an Open event.
2657 for (i = 0; i < IDX_COUNT; i++)
2658 if ((cps[i])->flags & CP_AUTH)
2661 if (sp->pp_phase == PHASE_NETWORK) {
2662 /* Notify all NCPs. */
2663 for (i = 0; i < IDX_COUNT; i++)
2664 if (((cps[i])->flags & CP_NCP) &&
2667 * Hack to administratively disable IPv6 if
2668 * not desired. Perhaps we should have another
2669 * flag for this, but right now, we can make
2670 * all struct cp's read/only.
2672 (cps[i] != &ipv6cp ||
2673 (sp->confflags & CONF_ENABLE_IPV6)))
2677 /* Send Up events to all started protos. */
2678 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2679 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2682 /* notify low-level driver of state change */
2684 sp->pp_chg(sp, (int)sp->pp_phase);
2686 if (sp->pp_phase == PHASE_NETWORK)
2687 /* if no NCP is starting, close down */
2688 sppp_lcp_check_and_close(sp);
2692 sppp_lcp_tld(struct sppp *sp)
2698 sp->pp_phase = PHASE_TERMINATE;
2701 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2702 sppp_phase_name(sp->pp_phase));
2705 * Take upper layers down. We send the Down event first and
2706 * the Close second to prevent the upper layers from sending
2707 * ``a flurry of terminate-request packets'', as the RFC
2710 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2711 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2713 (cps[i])->Close(sp);
2718 sppp_lcp_tls(struct sppp *sp)
2722 sp->pp_phase = PHASE_ESTABLISH;
2725 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2726 sppp_phase_name(sp->pp_phase));
2728 /* Notify lower layer if desired. */
2736 sppp_lcp_tlf(struct sppp *sp)
2740 sp->pp_phase = PHASE_DEAD;
2742 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2743 sppp_phase_name(sp->pp_phase));
2745 /* Notify lower layer if desired. */
2753 sppp_lcp_scr(struct sppp *sp)
2755 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2759 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2760 if (! sp->lcp.magic)
2761 sp->lcp.magic = random();
2762 opt[i++] = LCP_OPT_MAGIC;
2764 opt[i++] = sp->lcp.magic >> 24;
2765 opt[i++] = sp->lcp.magic >> 16;
2766 opt[i++] = sp->lcp.magic >> 8;
2767 opt[i++] = sp->lcp.magic;
2770 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2771 opt[i++] = LCP_OPT_MRU;
2773 opt[i++] = sp->lcp.mru >> 8;
2774 opt[i++] = sp->lcp.mru;
2777 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2778 authproto = sp->hisauth.proto;
2779 opt[i++] = LCP_OPT_AUTH_PROTO;
2780 opt[i++] = authproto == PPP_CHAP? 5: 4;
2781 opt[i++] = authproto >> 8;
2782 opt[i++] = authproto;
2783 if (authproto == PPP_CHAP)
2784 opt[i++] = CHAP_MD5;
2787 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2788 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2792 * Check the open NCPs, return true if at least one NCP is open.
2795 sppp_ncp_check(struct sppp *sp)
2799 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2800 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2806 * Re-check the open NCPs and see if we should terminate the link.
2807 * Called by the NCPs during their tlf action handling.
2810 sppp_lcp_check_and_close(struct sppp *sp)
2813 if (sp->pp_phase < PHASE_NETWORK)
2814 /* don't bother, we are already going down */
2817 if (sppp_ncp_check(sp))
2824 *--------------------------------------------------------------------------*
2826 * The IPCP implementation. *
2828 *--------------------------------------------------------------------------*
2833 sppp_ipcp_init(struct sppp *sp)
2837 sp->state[IDX_IPCP] = STATE_INITIAL;
2838 sp->fail_counter[IDX_IPCP] = 0;
2839 sp->pp_seq[IDX_IPCP] = 0;
2840 sp->pp_rseq[IDX_IPCP] = 0;
2841 callout_init(&sp->ch[IDX_IPCP], 1);
2845 sppp_ipcp_up(struct sppp *sp)
2847 sppp_up_event(&ipcp, sp);
2851 sppp_ipcp_down(struct sppp *sp)
2853 sppp_down_event(&ipcp, sp);
2857 sppp_ipcp_open(struct sppp *sp)
2860 u_long myaddr, hisaddr;
2862 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2863 IPCP_MYADDR_DYN | IPCP_VJ);
2866 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2868 * If we don't have his address, this probably means our
2869 * interface doesn't want to talk IP at all. (This could
2870 * be the case if somebody wants to speak only IPX, for
2871 * example.) Don't open IPCP in this case.
2873 if (hisaddr == 0L) {
2874 /* XXX this message should go away */
2876 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2882 * I don't have an assigned address, so i need to
2883 * negotiate my address.
2885 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2886 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2888 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2889 if (sp->confflags & CONF_ENABLE_VJ) {
2890 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2891 sp->ipcp.max_state = MAX_STATES - 1;
2892 sp->ipcp.compress_cid = 1;
2894 sppp_open_event(&ipcp, sp);
2898 sppp_ipcp_close(struct sppp *sp)
2900 sppp_close_event(&ipcp, sp);
2901 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2903 * My address was dynamic, clear it again.
2905 sppp_set_ip_addr(sp, 0L);
2909 sppp_ipcp_TO(void *cookie)
2911 sppp_to_event(&ipcp, (struct sppp *)cookie);
2915 * Analyze a configure request. Return true if it was agreeable, and
2916 * caused action sca, false if it has been rejected or nak'ed, and
2917 * caused action scn. (The return value is used to make the state
2918 * transition decision in the state automaton.)
2921 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2923 u_char *buf, *r, *p;
2924 struct ifnet *ifp = SP2IFP(sp);
2925 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2926 u_long hisaddr, desiredaddr;
2933 * Make sure to allocate a buf that can at least hold a
2934 * conf-nak with an `address' option. We might need it below.
2936 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2940 /* pass 1: see if we can recognize them */
2942 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2945 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2946 len-=p[1], p+=p[1]) {
2948 log(-1, " %s ", sppp_ipcp_opt_name(*p));
2950 case IPCP_OPT_COMPRESSION:
2951 if (!(sp->confflags & CONF_ENABLE_VJ)) {
2952 /* VJ compression administratively disabled */
2954 log(-1, "[locally disabled] ");
2958 * In theory, we should only conf-rej an
2959 * option that is shorter than RFC 1618
2960 * requires (i.e. < 4), and should conf-nak
2961 * anything else that is not VJ. However,
2962 * since our algorithm always uses the
2963 * original option to NAK it with new values,
2964 * things would become more complicated. In
2965 * practice, the only commonly implemented IP
2966 * compression option is VJ anyway, so the
2967 * difference is negligible.
2969 if (len >= 6 && p[1] == 6) {
2971 * correctly formed compression option
2972 * that could be VJ compression
2978 "optlen %d [invalid/unsupported] ",
2981 case IPCP_OPT_ADDRESS:
2982 if (len >= 6 && p[1] == 6) {
2983 /* correctly formed address option */
2987 log(-1, "[invalid] ");
2990 /* Others not supported. */
2995 /* Add the option to rejected list. */
3002 log(-1, " send conf-rej\n");
3003 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
3008 /* pass 2: parse option values */
3009 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
3011 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
3015 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3016 len-=p[1], p+=p[1]) {
3018 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3020 case IPCP_OPT_COMPRESSION:
3021 desiredcomp = p[2] << 8 | p[3];
3022 /* We only support VJ */
3023 if (desiredcomp == IPCP_COMP_VJ) {
3025 log(-1, "VJ [ack] ");
3026 sp->ipcp.flags |= IPCP_VJ;
3027 sl_compress_init(sp->pp_comp, p[4]);
3028 sp->ipcp.max_state = p[4];
3029 sp->ipcp.compress_cid = p[5];
3034 "compproto %#04x [not supported] ",
3036 p[2] = IPCP_COMP_VJ >> 8;
3037 p[3] = IPCP_COMP_VJ;
3038 p[4] = sp->ipcp.max_state;
3039 p[5] = sp->ipcp.compress_cid;
3041 case IPCP_OPT_ADDRESS:
3042 /* This is the address he wants in his end */
3043 desiredaddr = p[2] << 24 | p[3] << 16 |
3045 if (desiredaddr == hisaddr ||
3046 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3048 * Peer's address is same as our value,
3049 * or we have set it to 0.0.0.* to
3050 * indicate that we do not really care,
3051 * this is agreeable. Gonna conf-ack
3055 log(-1, "%s [ack] ",
3056 sppp_dotted_quad(hisaddr));
3057 /* record that we've seen it already */
3058 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3062 * The address wasn't agreeable. This is either
3063 * he sent us 0.0.0.0, asking to assign him an
3064 * address, or he send us another address not
3065 * matching our value. Either case, we gonna
3066 * conf-nak it with our value.
3067 * XXX: we should "rej" if hisaddr == 0
3070 if (desiredaddr == 0)
3071 log(-1, "[addr requested] ");
3073 log(-1, "%s [not agreed] ",
3074 sppp_dotted_quad(desiredaddr));
3077 p[2] = hisaddr >> 24;
3078 p[3] = hisaddr >> 16;
3079 p[4] = hisaddr >> 8;
3083 /* Add the option to nak'ed list. */
3090 * If we are about to conf-ack the request, but haven't seen
3091 * his address so far, gonna conf-nak it instead, with the
3092 * `address' option present and our idea of his address being
3093 * filled in there, to request negotiation of both addresses.
3095 * XXX This can result in an endless req - nak loop if peer
3096 * doesn't want to send us his address. Q: What should we do
3097 * about it? XXX A: implement the max-failure counter.
3099 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3100 buf[0] = IPCP_OPT_ADDRESS;
3102 buf[2] = hisaddr >> 24;
3103 buf[3] = hisaddr >> 16;
3104 buf[4] = hisaddr >> 8;
3108 log(-1, "still need hisaddr ");
3113 log(-1, " send conf-nak\n");
3114 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3117 log(-1, " send conf-ack\n");
3118 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3119 h->ident, origlen, h+1);
3127 * Analyze the IPCP Configure-Reject option list, and adjust our
3131 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3134 struct ifnet *ifp = SP2IFP(sp);
3135 int debug = ifp->if_flags & IFF_DEBUG;
3138 buf = malloc (len, M_TEMP, M_NOWAIT);
3143 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3147 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3148 len -= p[1], p += p[1]) {
3150 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3152 case IPCP_OPT_COMPRESSION:
3153 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3155 case IPCP_OPT_ADDRESS:
3157 * Peer doesn't grok address option. This is
3158 * bad. XXX Should we better give up here?
3159 * XXX We could try old "addresses" option...
3161 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3172 * Analyze the IPCP Configure-NAK option list, and adjust our
3176 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3179 struct ifnet *ifp = SP2IFP(sp);
3180 int debug = ifp->if_flags & IFF_DEBUG;
3185 buf = malloc (len, M_TEMP, M_NOWAIT);
3190 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3194 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3195 len -= p[1], p += p[1]) {
3197 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3199 case IPCP_OPT_COMPRESSION:
3200 if (len >= 6 && p[1] == 6) {
3201 desiredcomp = p[2] << 8 | p[3];
3203 log(-1, "[wantcomp %#04x] ",
3205 if (desiredcomp == IPCP_COMP_VJ) {
3206 sl_compress_init(sp->pp_comp, p[4]);
3207 sp->ipcp.max_state = p[4];
3208 sp->ipcp.compress_cid = p[5];
3210 log(-1, "[agree] ");
3213 ~(1 << IPCP_OPT_COMPRESSION);
3216 case IPCP_OPT_ADDRESS:
3218 * Peer doesn't like our local IP address. See
3219 * if we can do something for him. We'll drop
3220 * him our address then.
3222 if (len >= 6 && p[1] == 6) {
3223 wantaddr = p[2] << 24 | p[3] << 16 |
3225 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3227 log(-1, "[wantaddr %s] ",
3228 sppp_dotted_quad(wantaddr));
3230 * When doing dynamic address assignment,
3231 * we accept his offer. Otherwise, we
3232 * ignore it and thus continue to negotiate
3233 * our already existing value.
3234 * XXX: Bogus, if he said no once, he'll
3235 * just say no again, might as well die.
3237 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3238 sppp_set_ip_addr(sp, wantaddr);
3240 log(-1, "[agree] ");
3241 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3254 sppp_ipcp_tlu(struct sppp *sp)
3256 /* we are up - notify isdn daemon */
3262 sppp_ipcp_tld(struct sppp *sp)
3267 sppp_ipcp_tls(struct sppp *sp)
3269 /* indicate to LCP that it must stay alive */
3270 sp->lcp.protos |= (1 << IDX_IPCP);
3274 sppp_ipcp_tlf(struct sppp *sp)
3276 /* we no longer need LCP */
3277 sp->lcp.protos &= ~(1 << IDX_IPCP);
3278 sppp_lcp_check_and_close(sp);
3282 sppp_ipcp_scr(struct sppp *sp)
3284 char opt[6 /* compression */ + 6 /* address */];
3288 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3289 opt[i++] = IPCP_OPT_COMPRESSION;
3291 opt[i++] = IPCP_COMP_VJ >> 8;
3292 opt[i++] = IPCP_COMP_VJ;
3293 opt[i++] = sp->ipcp.max_state;
3294 opt[i++] = sp->ipcp.compress_cid;
3296 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3297 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3298 opt[i++] = IPCP_OPT_ADDRESS;
3300 opt[i++] = ouraddr >> 24;
3301 opt[i++] = ouraddr >> 16;
3302 opt[i++] = ouraddr >> 8;
3306 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3307 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3311 sppp_ipcp_init(struct sppp *sp)
3316 sppp_ipcp_up(struct sppp *sp)
3321 sppp_ipcp_down(struct sppp *sp)
3326 sppp_ipcp_open(struct sppp *sp)
3331 sppp_ipcp_close(struct sppp *sp)
3336 sppp_ipcp_TO(void *cookie)
3341 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3347 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3352 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3357 sppp_ipcp_tlu(struct sppp *sp)
3362 sppp_ipcp_tld(struct sppp *sp)
3367 sppp_ipcp_tls(struct sppp *sp)
3372 sppp_ipcp_tlf(struct sppp *sp)
3377 sppp_ipcp_scr(struct sppp *sp)
3383 *--------------------------------------------------------------------------*
3385 * The IPv6CP implementation. *
3387 *--------------------------------------------------------------------------*
3392 sppp_ipv6cp_init(struct sppp *sp)
3394 sp->ipv6cp.opts = 0;
3395 sp->ipv6cp.flags = 0;
3396 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3397 sp->fail_counter[IDX_IPV6CP] = 0;
3398 sp->pp_seq[IDX_IPV6CP] = 0;
3399 sp->pp_rseq[IDX_IPV6CP] = 0;
3400 callout_init(&sp->ch[IDX_IPV6CP], 1);
3404 sppp_ipv6cp_up(struct sppp *sp)
3406 sppp_up_event(&ipv6cp, sp);
3410 sppp_ipv6cp_down(struct sppp *sp)
3412 sppp_down_event(&ipv6cp, sp);
3416 sppp_ipv6cp_open(struct sppp *sp)
3419 struct in6_addr myaddr, hisaddr;
3421 #ifdef IPV6CP_MYIFID_DYN
3422 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3424 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3427 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3429 * If we don't have our address, this probably means our
3430 * interface doesn't want to talk IPv6 at all. (This could
3431 * be the case if somebody wants to speak only IPX, for
3432 * example.) Don't open IPv6CP in this case.
3434 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3435 /* XXX this message should go away */
3437 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3442 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3443 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3444 sppp_open_event(&ipv6cp, sp);
3448 sppp_ipv6cp_close(struct sppp *sp)
3450 sppp_close_event(&ipv6cp, sp);
3454 sppp_ipv6cp_TO(void *cookie)
3456 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3460 * Analyze a configure request. Return true if it was agreeable, and
3461 * caused action sca, false if it has been rejected or nak'ed, and
3462 * caused action scn. (The return value is used to make the state
3463 * transition decision in the state automaton.)
3466 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3468 u_char *buf, *r, *p;
3469 struct ifnet *ifp = SP2IFP(sp);
3470 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3471 struct in6_addr myaddr, desiredaddr, suggestaddr;
3474 int collision, nohisaddr;
3475 char ip6buf[INET6_ADDRSTRLEN];
3480 * Make sure to allocate a buf that can at least hold a
3481 * conf-nak with an `address' option. We might need it below.
3483 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3487 /* pass 1: see if we can recognize them */
3489 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3493 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3494 len-=p[1], p+=p[1]) {
3496 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3498 case IPV6CP_OPT_IFID:
3499 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3500 /* correctly formed address option */
3505 log(-1, " [invalid]");
3508 case IPV6CP_OPT_COMPRESSION:
3509 if (len >= 4 && p[1] >= 4) {
3510 /* correctly formed compress option */
3514 log(-1, " [invalid]");
3518 /* Others not supported. */
3523 /* Add the option to rejected list. */
3530 log(-1, " send conf-rej\n");
3531 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3536 /* pass 2: parse option values */
3537 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3539 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3544 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3545 len-=p[1], p+=p[1]) {
3547 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3550 case IPV6CP_OPT_COMPRESSION:
3553 case IPV6CP_OPT_IFID:
3554 bzero(&desiredaddr, sizeof(desiredaddr));
3555 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3556 collision = (bcmp(&desiredaddr.s6_addr[8],
3557 &myaddr.s6_addr[8], 8) == 0);
3558 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3560 desiredaddr.s6_addr16[0] = htons(0xfe80);
3561 (void)in6_setscope(&desiredaddr, SP2IFP(sp), NULL);
3563 if (!collision && !nohisaddr) {
3564 /* no collision, hisaddr known - Conf-Ack */
3569 ip6_sprintf(ip6buf, &desiredaddr),
3570 sppp_cp_type_name(type));
3575 bzero(&suggestaddr, sizeof(suggestaddr));
3576 if (collision && nohisaddr) {
3577 /* collision, hisaddr unknown - Conf-Rej */
3582 * - no collision, hisaddr unknown, or
3583 * - collision, hisaddr known
3584 * Conf-Nak, suggest hisaddr
3587 sppp_suggest_ip6_addr(sp, &suggestaddr);
3588 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3592 ip6_sprintf(ip6buf, &desiredaddr),
3593 sppp_cp_type_name(type));
3596 /* Add the option to nak'ed list. */
3602 if (rlen == 0 && type == CONF_ACK) {
3604 log(-1, " send %s\n", sppp_cp_type_name(type));
3605 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3608 if (type == CONF_ACK)
3609 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3613 log(-1, " send %s suggest %s\n",
3614 sppp_cp_type_name(type),
3615 ip6_sprintf(ip6buf, &suggestaddr));
3617 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3626 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3630 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3633 struct ifnet *ifp = SP2IFP(sp);
3634 int debug = ifp->if_flags & IFF_DEBUG;
3637 buf = malloc (len, M_TEMP, M_NOWAIT);
3642 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3646 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3647 len -= p[1], p += p[1]) {
3649 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3651 case IPV6CP_OPT_IFID:
3653 * Peer doesn't grok address option. This is
3654 * bad. XXX Should we better give up here?
3656 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3659 case IPV6CP_OPT_COMPRESS:
3660 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3672 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3676 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3679 struct ifnet *ifp = SP2IFP(sp);
3680 int debug = ifp->if_flags & IFF_DEBUG;
3681 struct in6_addr suggestaddr;
3682 char ip6buf[INET6_ADDRSTRLEN];
3685 buf = malloc (len, M_TEMP, M_NOWAIT);
3690 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3694 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3695 len -= p[1], p += p[1]) {
3697 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3699 case IPV6CP_OPT_IFID:
3701 * Peer doesn't like our local ifid. See
3702 * if we can do something for him. We'll drop
3703 * him our address then.
3705 if (len < 10 || p[1] != 10)
3707 bzero(&suggestaddr, sizeof(suggestaddr));
3708 suggestaddr.s6_addr16[0] = htons(0xfe80);
3709 (void)in6_setscope(&suggestaddr, SP2IFP(sp), NULL);
3710 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3712 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3714 log(-1, " [suggestaddr %s]",
3715 ip6_sprintf(ip6buf, &suggestaddr));
3716 #ifdef IPV6CP_MYIFID_DYN
3718 * When doing dynamic address assignment,
3719 * we accept his offer.
3721 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3722 struct in6_addr lastsuggest;
3724 * If <suggested myaddr from peer> equals to
3725 * <hisaddr we have suggested last time>,
3726 * we have a collision. generate new random
3729 sppp_suggest_ip6_addr(&lastsuggest);
3730 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3733 log(-1, " [random]");
3734 sppp_gen_ip6_addr(sp, &suggestaddr);
3736 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3738 log(-1, " [agree]");
3739 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3743 * Since we do not do dynamic address assignment,
3744 * we ignore it and thus continue to negotiate
3745 * our already existing value. This can possibly
3746 * go into infinite request-reject loop.
3748 * This is not likely because we normally use
3749 * ifid based on MAC-address.
3750 * If you have no ethernet card on the node, too bad.
3751 * XXX should we use fail_counter?
3756 case IPV6CP_OPT_COMPRESS:
3758 * Peer wants different compression parameters.
3770 sppp_ipv6cp_tlu(struct sppp *sp)
3772 /* we are up - notify isdn daemon */
3778 sppp_ipv6cp_tld(struct sppp *sp)
3783 sppp_ipv6cp_tls(struct sppp *sp)
3785 /* indicate to LCP that it must stay alive */
3786 sp->lcp.protos |= (1 << IDX_IPV6CP);
3790 sppp_ipv6cp_tlf(struct sppp *sp)
3793 #if 0 /* need #if 0 to close IPv6CP properly */
3794 /* we no longer need LCP */
3795 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3796 sppp_lcp_check_and_close(sp);
3801 sppp_ipv6cp_scr(struct sppp *sp)
3803 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3804 struct in6_addr ouraddr;
3807 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3808 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3809 opt[i++] = IPV6CP_OPT_IFID;
3811 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3816 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3817 opt[i++] = IPV6CP_OPT_COMPRESSION;
3819 opt[i++] = 0; /* TBD */
3820 opt[i++] = 0; /* TBD */
3821 /* variable length data may follow */
3825 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3826 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3829 static void sppp_ipv6cp_init(struct sppp *sp)
3833 static void sppp_ipv6cp_up(struct sppp *sp)
3837 static void sppp_ipv6cp_down(struct sppp *sp)
3842 static void sppp_ipv6cp_open(struct sppp *sp)
3846 static void sppp_ipv6cp_close(struct sppp *sp)
3850 static void sppp_ipv6cp_TO(void *sp)
3854 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3859 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3863 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3867 static void sppp_ipv6cp_tlu(struct sppp *sp)
3871 static void sppp_ipv6cp_tld(struct sppp *sp)
3875 static void sppp_ipv6cp_tls(struct sppp *sp)
3879 static void sppp_ipv6cp_tlf(struct sppp *sp)
3883 static void sppp_ipv6cp_scr(struct sppp *sp)
3889 *--------------------------------------------------------------------------*
3891 * The CHAP implementation. *
3893 *--------------------------------------------------------------------------*
3897 * The authentication protocols don't employ a full-fledged state machine as
3898 * the control protocols do, since they do have Open and Close events, but
3899 * not Up and Down, nor are they explicitly terminated. Also, use of the
3900 * authentication protocols may be different in both directions (this makes
3901 * sense, think of a machine that never accepts incoming calls but only
3902 * calls out, it doesn't require the called party to authenticate itself).
3904 * Our state machine for the local authentication protocol (we are requesting
3905 * the peer to authenticate) looks like:
3908 * +--------------------------------------------+
3910 * +--------+ Close +---------+ RCA+
3911 * | |<----------------------------------| |------+
3912 * +--->| Closed | TO* | Opened | sca |
3913 * | | |-----+ +-------| |<-----+
3914 * | +--------+ irc | | +---------+
3920 * | | +------->+ | |
3922 * | +--------+ V | |
3923 * | | |<----+<--------------------+ |
3929 * +------+ +------------------------------------------+
3930 * scn,tld sca,irc,ict,tlu
3935 * Open: LCP reached authentication phase
3936 * Close: LCP reached terminate phase
3938 * RCA+: received reply (pap-req, chap-response), acceptable
3939 * RCN: received reply (pap-req, chap-response), not acceptable
3940 * TO+: timeout with restart counter >= 0
3941 * TO-: timeout with restart counter < 0
3942 * TO*: reschedule timeout for CHAP
3944 * scr: send request packet (none for PAP, chap-challenge)
3945 * sca: send ack packet (pap-ack, chap-success)
3946 * scn: send nak packet (pap-nak, chap-failure)
3947 * ict: initialize re-challenge timer (CHAP only)
3949 * tlu: this-layer-up, LCP reaches network phase
3950 * tld: this-layer-down, LCP enters terminate phase
3952 * Note that in CHAP mode, after sending a new challenge, while the state
3953 * automaton falls back into Req-Sent state, it doesn't signal a tld
3954 * event to LCP, so LCP remains in network phase. Only after not getting
3955 * any response (or after getting an unacceptable response), CHAP closes,
3956 * causing LCP to enter terminate phase.
3958 * With PAP, there is no initial request that can be sent. The peer is
3959 * expected to send one based on the successful negotiation of PAP as
3960 * the authentication protocol during the LCP option negotiation.
3962 * Incoming authentication protocol requests (remote requests
3963 * authentication, we are peer) don't employ a state machine at all,
3964 * they are simply answered. Some peers [Ascend P50 firmware rev
3965 * 4.50] react allergically when sending IPCP requests while they are
3966 * still in authentication phase (thereby violating the standard that
3967 * demands that these NCP packets are to be discarded), so we keep
3968 * track of the peer demanding us to authenticate, and only proceed to
3969 * phase network once we've seen a positive acknowledge for the
3974 * Handle incoming CHAP packets.
3977 sppp_chap_input(struct sppp *sp, struct mbuf *m)
3980 struct lcp_header *h;
3982 u_char *value, *name, digest[AUTHKEYLEN], dsize;
3983 int value_len, name_len;
3986 len = m->m_pkthdr.len;
3990 SPP_FMT "chap invalid packet length: %d bytes\n",
3991 SPP_ARGS(ifp), len);
3994 h = mtod (m, struct lcp_header*);
3995 if (len > ntohs (h->len))
3996 len = ntohs (h->len);
3999 /* challenge, failure and success are his authproto */
4000 case CHAP_CHALLENGE:
4001 value = 1 + (u_char*)(h+1);
4002 value_len = value[-1];
4003 name = value + value_len;
4004 name_len = len - value_len - 5;
4008 SPP_FMT "chap corrupted challenge "
4009 "<%s id=0x%x len=%d",
4011 sppp_auth_type_name(PPP_CHAP, h->type),
4012 h->ident, ntohs(h->len));
4013 sppp_print_bytes((u_char*) (h+1), len-4);
4021 SPP_FMT "chap input <%s id=0x%x len=%d name=",
4023 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
4025 sppp_print_string((char*) name, name_len);
4026 log(-1, " value-size=%d value=", value_len);
4027 sppp_print_bytes(value, value_len);
4031 /* Compute reply value. */
4033 MD5Update(&ctx, &h->ident, 1);
4034 MD5Update(&ctx, sp->myauth.secret,
4035 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
4036 MD5Update(&ctx, value, value_len);
4037 MD5Final(digest, &ctx);
4038 dsize = sizeof digest;
4040 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
4041 sizeof dsize, (const char *)&dsize,
4042 sizeof digest, digest,
4043 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4050 log(LOG_DEBUG, SPP_FMT "chap success",
4054 sppp_print_string((char*)(h + 1), len - 4);
4059 sp->pp_flags &= ~PP_NEEDAUTH;
4060 if (sp->myauth.proto == PPP_CHAP &&
4061 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4062 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
4064 * We are authenticator for CHAP but didn't
4065 * complete yet. Leave it to tlu to proceed
4072 sppp_phase_network(sp);
4077 log(LOG_INFO, SPP_FMT "chap failure",
4081 sppp_print_string((char*)(h + 1), len - 4);
4085 log(LOG_INFO, SPP_FMT "chap failure\n",
4087 /* await LCP shutdown by authenticator */
4090 /* response is my authproto */
4092 value = 1 + (u_char*)(h+1);
4093 value_len = value[-1];
4094 name = value + value_len;
4095 name_len = len - value_len - 5;
4099 SPP_FMT "chap corrupted response "
4100 "<%s id=0x%x len=%d",
4102 sppp_auth_type_name(PPP_CHAP, h->type),
4103 h->ident, ntohs(h->len));
4104 sppp_print_bytes((u_char*)(h+1), len-4);
4109 if (h->ident != sp->confid[IDX_CHAP]) {
4112 SPP_FMT "chap dropping response for old ID "
4113 "(got %d, expected %d)\n",
4115 h->ident, sp->confid[IDX_CHAP]);
4118 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4119 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4120 log(LOG_INFO, SPP_FMT "chap response, his name ",
4122 sppp_print_string(name, name_len);
4123 log(-1, " != expected ");
4124 sppp_print_string(sp->hisauth.name,
4125 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4129 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4130 "<%s id=0x%x len=%d name=",
4132 sppp_state_name(sp->state[IDX_CHAP]),
4133 sppp_auth_type_name(PPP_CHAP, h->type),
4134 h->ident, ntohs (h->len));
4135 sppp_print_string((char*)name, name_len);
4136 log(-1, " value-size=%d value=", value_len);
4137 sppp_print_bytes(value, value_len);
4140 if (value_len != AUTHKEYLEN) {
4143 SPP_FMT "chap bad hash value length: "
4144 "%d bytes, should be %d\n",
4145 SPP_ARGS(ifp), value_len,
4151 MD5Update(&ctx, &h->ident, 1);
4152 MD5Update(&ctx, sp->hisauth.secret,
4153 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4154 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4155 MD5Final(digest, &ctx);
4157 #define FAILMSG "Failed..."
4158 #define SUCCMSG "Welcome!"
4160 if (value_len != sizeof digest ||
4161 bcmp(digest, value, value_len) != 0) {
4162 /* action scn, tld */
4163 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4164 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4169 /* action sca, perhaps tlu */
4170 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4171 sp->state[IDX_CHAP] == STATE_OPENED)
4172 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4173 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4175 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4176 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4182 /* Unknown CHAP packet type -- ignore. */
4184 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4185 "<0x%x id=0x%xh len=%d",
4187 sppp_state_name(sp->state[IDX_CHAP]),
4188 h->type, h->ident, ntohs(h->len));
4189 sppp_print_bytes((u_char*)(h+1), len-4);
4198 sppp_chap_init(struct sppp *sp)
4200 /* Chap doesn't have STATE_INITIAL at all. */
4201 sp->state[IDX_CHAP] = STATE_CLOSED;
4202 sp->fail_counter[IDX_CHAP] = 0;
4203 sp->pp_seq[IDX_CHAP] = 0;
4204 sp->pp_rseq[IDX_CHAP] = 0;
4205 callout_init(&sp->ch[IDX_CHAP], 1);
4209 sppp_chap_open(struct sppp *sp)
4211 if (sp->myauth.proto == PPP_CHAP &&
4212 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4213 /* we are authenticator for CHAP, start it */
4215 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4216 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4218 /* nothing to be done if we are peer, await a challenge */
4222 sppp_chap_close(struct sppp *sp)
4224 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4225 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4229 sppp_chap_TO(void *cookie)
4231 struct sppp *sp = (struct sppp *)cookie;
4236 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4238 sppp_state_name(sp->state[IDX_CHAP]),
4239 sp->rst_counter[IDX_CHAP]);
4241 if (--sp->rst_counter[IDX_CHAP] < 0)
4243 switch (sp->state[IDX_CHAP]) {
4244 case STATE_REQ_SENT:
4246 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4250 /* TO+ (or TO*) event */
4251 switch (sp->state[IDX_CHAP]) {
4254 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4256 case STATE_REQ_SENT:
4258 /* sppp_cp_change_state() will restart the timer */
4259 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4267 sppp_chap_tlu(struct sppp *sp)
4273 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4276 * Some broken CHAP implementations (Conware CoNet, firmware
4277 * 4.0.?) don't want to re-authenticate their CHAP once the
4278 * initial challenge-response exchange has taken place.
4279 * Provide for an option to avoid rechallenges.
4281 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4283 * Compute the re-challenge timeout. This will yield
4284 * a number between 300 and 810 seconds.
4286 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
4287 callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, (void *)sp);
4292 SPP_FMT "chap %s, ",
4294 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4295 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4296 log(-1, "next re-challenge in %d seconds\n", i);
4298 log(-1, "re-challenging suppressed\n");
4302 /* indicate to LCP that we need to be closed down */
4303 sp->lcp.protos |= (1 << IDX_CHAP);
4305 if (sp->pp_flags & PP_NEEDAUTH) {
4307 * Remote is authenticator, but his auth proto didn't
4308 * complete yet. Defer the transition to network
4317 * If we are already in phase network, we are done here. This
4318 * is the case if this is a dummy tlu event after a re-challenge.
4320 if (sp->pp_phase != PHASE_NETWORK)
4321 sppp_phase_network(sp);
4325 sppp_chap_tld(struct sppp *sp)
4330 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4331 callout_stop(&sp->ch[IDX_CHAP]);
4332 sp->lcp.protos &= ~(1 << IDX_CHAP);
4338 sppp_chap_scr(struct sppp *sp)
4343 /* Compute random challenge. */
4344 ch = (u_long *)sp->myauth.challenge;
4345 read_random(&seed, sizeof seed);
4346 ch[0] = seed ^ random();
4347 ch[1] = seed ^ random();
4348 ch[2] = seed ^ random();
4349 ch[3] = seed ^ random();
4352 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4354 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4355 sizeof clen, (const char *)&clen,
4356 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4357 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4363 *--------------------------------------------------------------------------*
4365 * The PAP implementation. *
4367 *--------------------------------------------------------------------------*
4370 * For PAP, we need to keep a little state also if we are the peer, not the
4371 * authenticator. This is since we don't get a request to authenticate, but
4372 * have to repeatedly authenticate ourself until we got a response (or the
4373 * retry counter is expired).
4377 * Handle incoming PAP packets. */
4379 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4382 struct lcp_header *h;
4384 u_char *name, *passwd, mlen;
4385 int name_len, passwd_len;
4387 len = m->m_pkthdr.len;
4391 SPP_FMT "pap invalid packet length: %d bytes\n",
4392 SPP_ARGS(ifp), len);
4395 h = mtod (m, struct lcp_header*);
4396 if (len > ntohs (h->len))
4397 len = ntohs (h->len);
4399 /* PAP request is my authproto */
4401 name = 1 + (u_char*)(h+1);
4402 name_len = name[-1];
4403 passwd = name + name_len + 1;
4404 if (name_len > len - 6 ||
4405 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4407 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4408 "<%s id=0x%x len=%d",
4410 sppp_auth_type_name(PPP_PAP, h->type),
4411 h->ident, ntohs(h->len));
4412 sppp_print_bytes((u_char*)(h+1), len-4);
4418 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4419 "<%s id=0x%x len=%d name=",
4421 sppp_state_name(sp->state[IDX_PAP]),
4422 sppp_auth_type_name(PPP_PAP, h->type),
4423 h->ident, ntohs(h->len));
4424 sppp_print_string((char*)name, name_len);
4425 log(-1, " passwd=");
4426 sppp_print_string((char*)passwd, passwd_len);
4429 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4430 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4431 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4432 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4433 /* action scn, tld */
4434 mlen = sizeof(FAILMSG) - 1;
4435 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4436 sizeof mlen, (const char *)&mlen,
4437 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4442 /* action sca, perhaps tlu */
4443 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4444 sp->state[IDX_PAP] == STATE_OPENED) {
4445 mlen = sizeof(SUCCMSG) - 1;
4446 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4447 sizeof mlen, (const char *)&mlen,
4448 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4451 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4452 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4457 /* ack and nak are his authproto */
4459 callout_stop(&sp->pap_my_to_ch);
4461 log(LOG_DEBUG, SPP_FMT "pap success",
4463 name_len = *((char *)h);
4464 if (len > 5 && name_len) {
4466 sppp_print_string((char*)(h+1), name_len);
4471 sp->pp_flags &= ~PP_NEEDAUTH;
4472 if (sp->myauth.proto == PPP_PAP &&
4473 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4474 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4476 * We are authenticator for PAP but didn't
4477 * complete yet. Leave it to tlu to proceed
4484 sppp_phase_network(sp);
4488 callout_stop (&sp->pap_my_to_ch);
4490 log(LOG_INFO, SPP_FMT "pap failure",
4492 name_len = *((char *)h);
4493 if (len > 5 && name_len) {
4495 sppp_print_string((char*)(h+1), name_len);
4499 log(LOG_INFO, SPP_FMT "pap failure\n",
4501 /* await LCP shutdown by authenticator */
4505 /* Unknown PAP packet type -- ignore. */
4507 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4508 "<0x%x id=0x%x len=%d",
4510 h->type, h->ident, ntohs(h->len));
4511 sppp_print_bytes((u_char*)(h+1), len-4);
4520 sppp_pap_init(struct sppp *sp)
4522 /* PAP doesn't have STATE_INITIAL at all. */
4523 sp->state[IDX_PAP] = STATE_CLOSED;
4524 sp->fail_counter[IDX_PAP] = 0;
4525 sp->pp_seq[IDX_PAP] = 0;
4526 sp->pp_rseq[IDX_PAP] = 0;
4527 callout_init(&sp->ch[IDX_PAP], 1);
4528 callout_init(&sp->pap_my_to_ch, 1);
4532 sppp_pap_open(struct sppp *sp)
4534 if (sp->hisauth.proto == PPP_PAP &&
4535 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4536 /* we are authenticator for PAP, start our timer */
4537 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4538 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4540 if (sp->myauth.proto == PPP_PAP) {
4541 /* we are peer, send a request, and start a timer */
4543 callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
4544 sppp_pap_my_TO, (void *)sp);
4549 sppp_pap_close(struct sppp *sp)
4551 if (sp->state[IDX_PAP] != STATE_CLOSED)
4552 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4556 * That's the timeout routine if we are authenticator. Since the
4557 * authenticator is basically passive in PAP, we can't do much here.
4560 sppp_pap_TO(void *cookie)
4562 struct sppp *sp = (struct sppp *)cookie;
4567 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4569 sppp_state_name(sp->state[IDX_PAP]),
4570 sp->rst_counter[IDX_PAP]);
4572 if (--sp->rst_counter[IDX_PAP] < 0)
4574 switch (sp->state[IDX_PAP]) {
4575 case STATE_REQ_SENT:
4577 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4581 /* TO+ event, not very much we could do */
4582 switch (sp->state[IDX_PAP]) {
4583 case STATE_REQ_SENT:
4584 /* sppp_cp_change_state() will restart the timer */
4585 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4593 * That's the timeout handler if we are peer. Since the peer is active,
4594 * we need to retransmit our PAP request since it is apparently lost.
4595 * XXX We should impose a max counter.
4598 sppp_pap_my_TO(void *cookie)
4600 struct sppp *sp = (struct sppp *)cookie;
4604 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4613 sppp_pap_tlu(struct sppp *sp)
4617 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4620 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4621 SPP_ARGS(ifp), pap.name);
4624 /* indicate to LCP that we need to be closed down */
4625 sp->lcp.protos |= (1 << IDX_PAP);
4627 if (sp->pp_flags & PP_NEEDAUTH) {
4629 * Remote is authenticator, but his auth proto didn't
4630 * complete yet. Defer the transition to network
4637 sppp_phase_network(sp);
4641 sppp_pap_tld(struct sppp *sp)
4646 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4647 callout_stop (&sp->ch[IDX_PAP]);
4648 callout_stop (&sp->pap_my_to_ch);
4649 sp->lcp.protos &= ~(1 << IDX_PAP);
4655 sppp_pap_scr(struct sppp *sp)
4657 u_char idlen, pwdlen;
4659 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4660 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4661 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4663 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4664 sizeof idlen, (const char *)&idlen,
4665 (size_t)idlen, sp->myauth.name,
4666 sizeof pwdlen, (const char *)&pwdlen,
4667 (size_t)pwdlen, sp->myauth.secret,
4672 * Random miscellaneous functions.
4676 * Send a PAP or CHAP proto packet.
4678 * Varadic function, each of the elements for the ellipsis is of type
4679 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4681 * NOTE: never declare variadic functions with types subject to type
4682 * promotion (i.e. u_char). This is asking for big trouble depending
4683 * on the architecture you are on...
4687 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4688 unsigned int type, unsigned int id,
4692 struct ppp_header *h;
4693 struct lcp_header *lh;
4701 MGETHDR (m, M_NOWAIT, MT_DATA);
4704 m->m_pkthdr.rcvif = 0;
4706 h = mtod (m, struct ppp_header*);
4707 h->address = PPP_ALLSTATIONS; /* broadcast address */
4708 h->control = PPP_UI; /* Unnumbered Info */
4709 h->protocol = htons(cp->proto);
4711 lh = (struct lcp_header*)(h + 1);
4714 p = (u_char*) (lh+1);
4719 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
4720 msg = va_arg(ap, const char *);
4722 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4728 bcopy(msg, p, mlen);
4733 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4734 lh->len = htons (LCP_HEADER_LEN + len);
4737 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4738 SPP_ARGS(ifp), cp->name,
4739 sppp_auth_type_name(cp->proto, lh->type),
4740 lh->ident, ntohs(lh->len));
4741 sppp_print_bytes((u_char*) (lh+1), len);
4744 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
4745 if_inc_counter(ifp, IFCOUNTER_OERRORS, 1);
4749 * Flush interface queue.
4752 sppp_qflush(struct ifqueue *ifq)
4767 * Send keepalive packets, every 10 seconds.
4770 sppp_keepalive(void *dummy)
4772 struct sppp *sp = (struct sppp*)dummy;
4773 struct ifnet *ifp = SP2IFP(sp);
4776 /* Keepalive mode disabled or channel down? */
4777 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4778 ! (ifp->if_drv_flags & IFF_DRV_RUNNING))
4781 if (sp->pp_mode == PP_FR) {
4782 sppp_fr_keepalive (sp);
4786 /* No keepalive in PPP mode if LCP not opened yet. */
4787 if (sp->pp_mode != IFF_CISCO &&
4788 sp->pp_phase < PHASE_AUTHENTICATE)
4791 if (sp->pp_alivecnt == MAXALIVECNT) {
4792 /* No keepalive packets got. Stop the interface. */
4793 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
4795 sppp_qflush (&sp->pp_cpq);
4796 if (sp->pp_mode != IFF_CISCO) {
4798 /* Shut down the PPP link. */
4800 /* Initiate negotiation. XXX */
4804 if (sp->pp_alivecnt <= MAXALIVECNT)
4806 if (sp->pp_mode == IFF_CISCO)
4807 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4808 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4809 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4810 long nmagic = htonl (sp->lcp.magic);
4811 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4812 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4813 sp->lcp.echoid, 4, &nmagic);
4817 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
4822 * Get both IP addresses.
4825 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4827 struct ifnet *ifp = SP2IFP(sp);
4829 struct sockaddr_in *si, *sm;
4835 * Pick the first AF_INET address from the list,
4836 * aliases don't make any sense on a p2p link anyway.
4840 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4841 if (ifa->ifa_addr->sa_family == AF_INET) {
4842 si = (struct sockaddr_in *)ifa->ifa_addr;
4843 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4848 if (si && si->sin_addr.s_addr) {
4849 ssrc = si->sin_addr.s_addr;
4851 *srcmask = ntohl(sm->sin_addr.s_addr);
4854 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4855 if (si && si->sin_addr.s_addr)
4856 ddst = si->sin_addr.s_addr;
4858 if_addr_runlock(ifp);
4860 if (dst) *dst = ntohl(ddst);
4861 if (src) *src = ntohl(ssrc);
4866 * Set my IP address.
4869 sppp_set_ip_addr(struct sppp *sp, u_long src)
4873 struct sockaddr_in *si;
4874 struct in_ifaddr *ia;
4877 * Pick the first AF_INET address from the list,
4878 * aliases don't make any sense on a p2p link anyway.
4882 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4883 if (ifa->ifa_addr->sa_family == AF_INET) {
4884 si = (struct sockaddr_in *)ifa->ifa_addr;
4891 if_addr_runlock(ifp);
4896 /* delete old route */
4897 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4898 if (debug && error) {
4899 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4900 SPP_ARGS(ifp), error);
4903 /* set new address */
4904 si->sin_addr.s_addr = htonl(src);
4907 LIST_REMOVE(ia, ia_hash);
4908 LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash);
4909 IN_IFADDR_WUNLOCK();
4912 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
4913 if (debug && error) {
4914 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
4915 SPP_ARGS(ifp), error);
4924 * Get both IPv6 addresses.
4927 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4928 struct in6_addr *srcmask)
4930 struct ifnet *ifp = SP2IFP(sp);
4932 struct sockaddr_in6 *si, *sm;
4933 struct in6_addr ssrc, ddst;
4936 bzero(&ssrc, sizeof(ssrc));
4937 bzero(&ddst, sizeof(ddst));
4939 * Pick the first link-local AF_INET6 address from the list,
4940 * aliases don't make any sense on a p2p link anyway.
4944 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4945 if (ifa->ifa_addr->sa_family == AF_INET6) {
4946 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4947 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4948 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
4952 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
4953 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
4955 bcopy(&sm->sin6_addr, srcmask,
4960 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
4961 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
4962 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
4966 bcopy(&ddst, dst, sizeof(*dst));
4968 bcopy(&ssrc, src, sizeof(*src));
4969 if_addr_runlock(ifp);
4972 #ifdef IPV6CP_MYIFID_DYN
4974 * Generate random ifid.
4977 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
4983 * Set my IPv6 address.
4986 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
4990 struct sockaddr_in6 *sin6;
4993 * Pick the first link-local AF_INET6 address from the list,
4994 * aliases don't make any sense on a p2p link anyway.
4999 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
5000 if (ifa->ifa_addr->sa_family == AF_INET6) {
5001 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
5002 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5008 if_addr_runlock(ifp);
5012 struct sockaddr_in6 new_sin6 = *sin6;
5014 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
5015 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5016 if (debug && error) {
5017 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
5018 " failed, error=%d\n", SPP_ARGS(ifp), error);
5026 * Suggest a candidate address to be used by peer.
5029 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5031 struct in6_addr myaddr;
5034 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5036 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5038 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5039 myaddr.s6_addr[14] ^= 0xff;
5040 myaddr.s6_addr[15] ^= 0xff;
5042 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5043 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5046 bcopy(&myaddr, suggest, sizeof(myaddr));
5051 sppp_params(struct sppp *sp, u_long cmd, void *data)
5054 struct ifreq *ifr = (struct ifreq *)data;
5055 struct spppreq *spr;
5058 if ((spr = malloc(sizeof(struct spppreq), M_TEMP, M_NOWAIT)) == NULL)
5061 * ifr->ifr_data is supposed to point to a struct spppreq.
5062 * Check the cmd word first before attempting to fetch all the
5065 rv = fueword(ifr->ifr_data, &subcmd);
5071 if (copyin((caddr_t)ifr->ifr_data, spr, sizeof(struct spppreq)) != 0) {
5077 case (u_long)SPPPIOGDEFS:
5078 if (cmd != SIOCGIFGENERIC) {
5083 * We copy over the entire current state, but clean
5084 * out some of the stuff we don't wanna pass up.
5085 * Remember, SIOCGIFGENERIC is unprotected, and can be
5086 * called by any user. No need to ever get PAP or
5087 * CHAP secrets back to userland anyway.
5089 spr->defs.pp_phase = sp->pp_phase;
5090 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5091 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5092 spr->defs.lcp = sp->lcp;
5093 spr->defs.ipcp = sp->ipcp;
5094 spr->defs.ipv6cp = sp->ipv6cp;
5095 spr->defs.myauth = sp->myauth;
5096 spr->defs.hisauth = sp->hisauth;
5097 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5098 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5099 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5100 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5102 * Fixup the LCP timeout value to milliseconds so
5103 * spppcontrol doesn't need to bother about the value
5104 * of "hz". We do the reverse calculation below when
5107 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5108 rv = copyout(spr, (caddr_t)ifr->ifr_data,
5109 sizeof(struct spppreq));
5112 case (u_long)SPPPIOSDEFS:
5113 if (cmd != SIOCSIFGENERIC) {
5118 * We have a very specific idea of which fields we
5119 * allow being passed back from userland, so to not
5120 * clobber our current state. For one, we only allow
5121 * setting anything if LCP is in dead or establish
5122 * phase. Once the authentication negotiations
5123 * started, the authentication settings must not be
5124 * changed again. (The administrator can force an
5125 * ifconfig down in order to get LCP back into dead
5128 * Also, we only allow for authentication parameters to be
5131 * XXX Should allow to set or clear pp_flags.
5133 * Finally, if the respective authentication protocol to
5134 * be used is set differently than 0, but the secret is
5135 * passed as all zeros, we don't trash the existing secret.
5136 * This allows an administrator to change the system name
5137 * only without clobbering the secret (which he didn't get
5138 * back in a previous SPPPIOGDEFS call). However, the
5139 * secrets are cleared if the authentication protocol is
5141 if (sp->pp_phase != PHASE_DEAD &&
5142 sp->pp_phase != PHASE_ESTABLISH) {
5147 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5148 spr->defs.myauth.proto != PPP_CHAP) ||
5149 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5150 spr->defs.hisauth.proto != PPP_CHAP)) {
5155 if (spr->defs.myauth.proto == 0)
5156 /* resetting myauth */
5157 bzero(&sp->myauth, sizeof sp->myauth);
5159 /* setting/changing myauth */
5160 sp->myauth.proto = spr->defs.myauth.proto;
5161 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5162 if (spr->defs.myauth.secret[0] != '\0')
5163 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5166 if (spr->defs.hisauth.proto == 0)
5167 /* resetting hisauth */
5168 bzero(&sp->hisauth, sizeof sp->hisauth);
5170 /* setting/changing hisauth */
5171 sp->hisauth.proto = spr->defs.hisauth.proto;
5172 sp->hisauth.flags = spr->defs.hisauth.flags;
5173 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5174 if (spr->defs.hisauth.secret[0] != '\0')
5175 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5178 /* set LCP restart timer timeout */
5179 if (spr->defs.lcp.timeout != 0)
5180 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5181 /* set VJ enable and IPv6 disable flags */
5183 if (spr->defs.enable_vj)
5184 sp->confflags |= CONF_ENABLE_VJ;
5186 sp->confflags &= ~CONF_ENABLE_VJ;
5189 if (spr->defs.enable_ipv6)
5190 sp->confflags |= CONF_ENABLE_IPV6;
5192 sp->confflags &= ~CONF_ENABLE_IPV6;
5207 sppp_phase_network(struct sppp *sp)
5213 sp->pp_phase = PHASE_NETWORK;
5216 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5217 sppp_phase_name(sp->pp_phase));
5219 /* Notify NCPs now. */
5220 for (i = 0; i < IDX_COUNT; i++)
5221 if ((cps[i])->flags & CP_NCP)
5224 /* Send Up events to all NCPs. */
5225 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5226 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5229 /* if no NCP is starting, all this was in vain, close down */
5230 sppp_lcp_check_and_close(sp);
5235 sppp_cp_type_name(u_char type)
5237 static char buf[12];
5239 case CONF_REQ: return "conf-req";
5240 case CONF_ACK: return "conf-ack";
5241 case CONF_NAK: return "conf-nak";
5242 case CONF_REJ: return "conf-rej";
5243 case TERM_REQ: return "term-req";
5244 case TERM_ACK: return "term-ack";
5245 case CODE_REJ: return "code-rej";
5246 case PROTO_REJ: return "proto-rej";
5247 case ECHO_REQ: return "echo-req";
5248 case ECHO_REPLY: return "echo-reply";
5249 case DISC_REQ: return "discard-req";
5251 snprintf (buf, sizeof(buf), "cp/0x%x", type);
5256 sppp_auth_type_name(u_short proto, u_char type)
5258 static char buf[12];
5262 case CHAP_CHALLENGE: return "challenge";
5263 case CHAP_RESPONSE: return "response";
5264 case CHAP_SUCCESS: return "success";
5265 case CHAP_FAILURE: return "failure";
5269 case PAP_REQ: return "req";
5270 case PAP_ACK: return "ack";
5271 case PAP_NAK: return "nak";
5274 snprintf (buf, sizeof(buf), "auth/0x%x", type);
5279 sppp_lcp_opt_name(u_char opt)
5281 static char buf[12];
5283 case LCP_OPT_MRU: return "mru";
5284 case LCP_OPT_ASYNC_MAP: return "async-map";
5285 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5286 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5287 case LCP_OPT_MAGIC: return "magic";
5288 case LCP_OPT_PROTO_COMP: return "proto-comp";
5289 case LCP_OPT_ADDR_COMP: return "addr-comp";
5291 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5297 sppp_ipcp_opt_name(u_char opt)
5299 static char buf[12];
5301 case IPCP_OPT_ADDRESSES: return "addresses";
5302 case IPCP_OPT_COMPRESSION: return "compression";
5303 case IPCP_OPT_ADDRESS: return "address";
5305 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5312 sppp_ipv6cp_opt_name(u_char opt)
5314 static char buf[12];
5316 case IPV6CP_OPT_IFID: return "ifid";
5317 case IPV6CP_OPT_COMPRESSION: return "compression";
5319 sprintf (buf, "0x%x", opt);
5325 sppp_state_name(int state)
5328 case STATE_INITIAL: return "initial";
5329 case STATE_STARTING: return "starting";
5330 case STATE_CLOSED: return "closed";
5331 case STATE_STOPPED: return "stopped";
5332 case STATE_CLOSING: return "closing";
5333 case STATE_STOPPING: return "stopping";
5334 case STATE_REQ_SENT: return "req-sent";
5335 case STATE_ACK_RCVD: return "ack-rcvd";
5336 case STATE_ACK_SENT: return "ack-sent";
5337 case STATE_OPENED: return "opened";
5343 sppp_phase_name(enum ppp_phase phase)
5346 case PHASE_DEAD: return "dead";
5347 case PHASE_ESTABLISH: return "establish";
5348 case PHASE_TERMINATE: return "terminate";
5349 case PHASE_AUTHENTICATE: return "authenticate";
5350 case PHASE_NETWORK: return "network";
5356 sppp_proto_name(u_short proto)
5358 static char buf[12];
5360 case PPP_LCP: return "lcp";
5361 case PPP_IPCP: return "ipcp";
5362 case PPP_PAP: return "pap";
5363 case PPP_CHAP: return "chap";
5364 case PPP_IPV6CP: return "ipv6cp";
5366 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5371 sppp_print_bytes(const u_char *p, u_short len)
5374 log(-1, " %*D", len, p, "-");
5378 sppp_print_string(const char *p, u_short len)
5385 * Print only ASCII chars directly. RFC 1994 recommends
5386 * using only them, but we don't rely on it. */
5387 if (c < ' ' || c > '~')
5388 log(-1, "\\x%x", c);
5396 sppp_dotted_quad(u_long addr)
5399 sprintf(s, "%d.%d.%d.%d",
5400 (int)((addr >> 24) & 0xff),
5401 (int)((addr >> 16) & 0xff),
5402 (int)((addr >> 8) & 0xff),
5403 (int)(addr & 0xff));
5409 sppp_strnlen(u_char *p, int max)
5413 for (len = 0; len < max && *p; ++p)
5418 /* a dummy, used to drop uninteresting events */
5420 sppp_null(struct sppp *unused)
5422 /* do just nothing */