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"
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/sockio.h>
34 #include <sys/socket.h>
35 #include <sys/syslog.h>
36 #include <sys/random.h>
37 #include <sys/malloc.h>
43 #include <net/netisr.h>
44 #include <net/if_types.h>
45 #include <net/route.h>
47 #include <netinet/in.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/ip.h>
50 #include <net/slcompress.h>
52 #include <machine/stdarg.h>
54 #include <netinet/in_var.h>
57 #include <netinet/ip.h>
58 #include <netinet/tcp.h>
62 #include <netinet6/scope6_var.h>
65 #include <netinet/if_ether.h>
68 #include <netipx/ipx.h>
69 #include <netipx/ipx_if.h>
72 #include <net/if_sppp.h>
74 #define IOCTL_CMD_T u_long
75 #define MAXALIVECNT 3 /* max. alive packets */
78 * Interface flags that can be set in an ifconfig command.
80 * Setting link0 will make the link passive, i.e. it will be marked
81 * as being administrative openable, but won't be opened to begin
82 * with. Incoming calls will be answered, or subsequent calls with
83 * -link1 will cause the administrative open of the LCP layer.
85 * Setting link1 will cause the link to auto-dial only as packets
88 * Setting IFF_DEBUG will syslog the option negotiation and state
89 * transitions at level kern.debug. Note: all logs consistently look
92 * <if-name><unit>: <proto-name> <additional info...>
94 * with <if-name><unit> being something like "bppp0", and <proto-name>
95 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
98 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
99 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
100 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
102 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
103 #define PPP_UI 0x03 /* Unnumbered Information */
104 #define PPP_IP 0x0021 /* Internet Protocol */
105 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
106 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
107 #define PPP_IPX 0x002b /* Novell IPX Protocol */
108 #define PPP_VJ_COMP 0x002d /* VJ compressed TCP/IP */
109 #define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */
110 #define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */
111 #define PPP_LCP 0xc021 /* Link Control Protocol */
112 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
113 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
114 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
115 #define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */
117 #define CONF_REQ 1 /* PPP configure request */
118 #define CONF_ACK 2 /* PPP configure acknowledge */
119 #define CONF_NAK 3 /* PPP configure negative ack */
120 #define CONF_REJ 4 /* PPP configure reject */
121 #define TERM_REQ 5 /* PPP terminate request */
122 #define TERM_ACK 6 /* PPP terminate acknowledge */
123 #define CODE_REJ 7 /* PPP code reject */
124 #define PROTO_REJ 8 /* PPP protocol reject */
125 #define ECHO_REQ 9 /* PPP echo request */
126 #define ECHO_REPLY 10 /* PPP echo reply */
127 #define DISC_REQ 11 /* PPP discard request */
129 #define LCP_OPT_MRU 1 /* maximum receive unit */
130 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
131 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
132 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
133 #define LCP_OPT_MAGIC 5 /* magic number */
134 #define LCP_OPT_RESERVED 6 /* reserved */
135 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
136 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
138 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
139 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
140 #define IPCP_OPT_ADDRESS 3 /* local IP address */
142 #define IPV6CP_OPT_IFID 1 /* interface identifier */
143 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
145 #define IPCP_COMP_VJ 0x2d /* Code for VJ compression */
147 #define PAP_REQ 1 /* PAP name/password request */
148 #define PAP_ACK 2 /* PAP acknowledge */
149 #define PAP_NAK 3 /* PAP fail */
151 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
152 #define CHAP_RESPONSE 2 /* CHAP challenge response */
153 #define CHAP_SUCCESS 3 /* CHAP response ok */
154 #define CHAP_FAILURE 4 /* CHAP response failed */
156 #define CHAP_MD5 5 /* hash algorithm - MD5 */
158 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
159 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
160 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
161 #define CISCO_ADDR_REQ 0 /* Cisco address request */
162 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
163 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
165 /* states are named and numbered according to RFC 1661 */
166 #define STATE_INITIAL 0
167 #define STATE_STARTING 1
168 #define STATE_CLOSED 2
169 #define STATE_STOPPED 3
170 #define STATE_CLOSING 4
171 #define STATE_STOPPING 5
172 #define STATE_REQ_SENT 6
173 #define STATE_ACK_RCVD 7
174 #define STATE_ACK_SENT 8
175 #define STATE_OPENED 9
177 static MALLOC_DEFINE(M_SPPP, "sppp", "synchronous PPP interface internals");
184 #define PPP_HEADER_LEN sizeof (struct ppp_header)
191 #define LCP_HEADER_LEN sizeof (struct lcp_header)
193 struct cisco_packet {
201 #define CISCO_PACKET_LEN sizeof (struct cisco_packet)
204 * We follow the spelling and capitalization of RFC 1661 here, to make
205 * it easier comparing with the standard. Please refer to this RFC in
206 * case you can't make sense out of these abbreviation; it will also
207 * explain the semantics related to the various events and actions.
210 u_short proto; /* PPP control protocol number */
211 u_char protoidx; /* index into state table in struct sppp */
213 #define CP_LCP 0x01 /* this is the LCP */
214 #define CP_AUTH 0x02 /* this is an authentication protocol */
215 #define CP_NCP 0x04 /* this is a NCP */
216 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
217 const char *name; /* name of this control protocol */
219 void (*Up)(struct sppp *sp);
220 void (*Down)(struct sppp *sp);
221 void (*Open)(struct sppp *sp);
222 void (*Close)(struct sppp *sp);
223 void (*TO)(void *sp);
224 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
225 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
226 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
228 void (*tlu)(struct sppp *sp);
229 void (*tld)(struct sppp *sp);
230 void (*tls)(struct sppp *sp);
231 void (*tlf)(struct sppp *sp);
232 void (*scr)(struct sppp *sp);
235 #define SPP_FMT "%s: "
236 #define SPP_ARGS(ifp) (ifp)->if_xname
238 #define SPPP_LOCK(sp) mtx_lock (&(sp)->mtx)
239 #define SPPP_UNLOCK(sp) mtx_unlock (&(sp)->mtx)
240 #define SPPP_LOCK_ASSERT(sp) mtx_assert (&(sp)->mtx, MA_OWNED)
241 #define SPPP_LOCK_OWNED(sp) mtx_owned (&(sp)->mtx)
245 * The following disgusting hack gets around the problem that IP TOS
246 * can't be set yet. We want to put "interactive" traffic on a high
247 * priority queue. To decide if traffic is interactive, we check that
248 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
250 * XXX is this really still necessary? - joerg -
252 static const u_short interactive_ports[8] = {
256 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
259 /* almost every function needs these */
261 struct ifnet *ifp = SP2IFP(sp); \
262 int debug = ifp->if_flags & IFF_DEBUG
264 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
265 struct sockaddr *dst, struct route *ro);
267 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
268 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
270 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
272 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
273 u_char ident, u_short len, void *data);
274 /* static void sppp_cp_timeout(void *arg); */
275 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
277 static void sppp_auth_send(const struct cp *cp,
278 struct sppp *sp, unsigned int type, unsigned int id,
281 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
282 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
283 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
284 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
285 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
287 static void sppp_null(struct sppp *sp);
289 static void sppp_pp_up(struct sppp *sp);
290 static void sppp_pp_down(struct sppp *sp);
292 static void sppp_lcp_init(struct sppp *sp);
293 static void sppp_lcp_up(struct sppp *sp);
294 static void sppp_lcp_down(struct sppp *sp);
295 static void sppp_lcp_open(struct sppp *sp);
296 static void sppp_lcp_close(struct sppp *sp);
297 static void sppp_lcp_TO(void *sp);
298 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
299 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
300 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
301 static void sppp_lcp_tlu(struct sppp *sp);
302 static void sppp_lcp_tld(struct sppp *sp);
303 static void sppp_lcp_tls(struct sppp *sp);
304 static void sppp_lcp_tlf(struct sppp *sp);
305 static void sppp_lcp_scr(struct sppp *sp);
306 static void sppp_lcp_check_and_close(struct sppp *sp);
307 static int sppp_ncp_check(struct sppp *sp);
309 static void sppp_ipcp_init(struct sppp *sp);
310 static void sppp_ipcp_up(struct sppp *sp);
311 static void sppp_ipcp_down(struct sppp *sp);
312 static void sppp_ipcp_open(struct sppp *sp);
313 static void sppp_ipcp_close(struct sppp *sp);
314 static void sppp_ipcp_TO(void *sp);
315 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
316 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
317 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
318 static void sppp_ipcp_tlu(struct sppp *sp);
319 static void sppp_ipcp_tld(struct sppp *sp);
320 static void sppp_ipcp_tls(struct sppp *sp);
321 static void sppp_ipcp_tlf(struct sppp *sp);
322 static void sppp_ipcp_scr(struct sppp *sp);
324 static void sppp_ipv6cp_init(struct sppp *sp);
325 static void sppp_ipv6cp_up(struct sppp *sp);
326 static void sppp_ipv6cp_down(struct sppp *sp);
327 static void sppp_ipv6cp_open(struct sppp *sp);
328 static void sppp_ipv6cp_close(struct sppp *sp);
329 static void sppp_ipv6cp_TO(void *sp);
330 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
331 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
332 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
333 static void sppp_ipv6cp_tlu(struct sppp *sp);
334 static void sppp_ipv6cp_tld(struct sppp *sp);
335 static void sppp_ipv6cp_tls(struct sppp *sp);
336 static void sppp_ipv6cp_tlf(struct sppp *sp);
337 static void sppp_ipv6cp_scr(struct sppp *sp);
339 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
340 static void sppp_pap_init(struct sppp *sp);
341 static void sppp_pap_open(struct sppp *sp);
342 static void sppp_pap_close(struct sppp *sp);
343 static void sppp_pap_TO(void *sp);
344 static void sppp_pap_my_TO(void *sp);
345 static void sppp_pap_tlu(struct sppp *sp);
346 static void sppp_pap_tld(struct sppp *sp);
347 static void sppp_pap_scr(struct sppp *sp);
349 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
350 static void sppp_chap_init(struct sppp *sp);
351 static void sppp_chap_open(struct sppp *sp);
352 static void sppp_chap_close(struct sppp *sp);
353 static void sppp_chap_TO(void *sp);
354 static void sppp_chap_tlu(struct sppp *sp);
355 static void sppp_chap_tld(struct sppp *sp);
356 static void sppp_chap_scr(struct sppp *sp);
358 static const char *sppp_auth_type_name(u_short proto, u_char type);
359 static const char *sppp_cp_type_name(u_char type);
361 static const char *sppp_dotted_quad(u_long addr);
362 static const char *sppp_ipcp_opt_name(u_char opt);
365 static const char *sppp_ipv6cp_opt_name(u_char opt);
367 static const char *sppp_lcp_opt_name(u_char opt);
368 static const char *sppp_phase_name(enum ppp_phase phase);
369 static const char *sppp_proto_name(u_short proto);
370 static const char *sppp_state_name(int state);
371 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
372 static int sppp_strnlen(u_char *p, int max);
373 static void sppp_keepalive(void *dummy);
374 static void sppp_phase_network(struct sppp *sp);
375 static void sppp_print_bytes(const u_char *p, u_short len);
376 static void sppp_print_string(const char *p, u_short len);
377 static void sppp_qflush(struct ifqueue *ifq);
379 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
382 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
383 struct in6_addr *dst, struct in6_addr *srcmask);
384 #ifdef IPV6CP_MYIFID_DYN
385 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
386 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
388 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
391 /* if_start () wrapper */
392 static void sppp_ifstart (struct ifnet *ifp);
394 /* our control protocol descriptors */
395 static const struct cp lcp = {
396 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
397 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
398 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
399 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
403 static const struct cp ipcp = {
405 #ifdef INET /* don't run IPCP if there's no IPv4 support */
411 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
412 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
413 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
417 static const struct cp ipv6cp = {
418 PPP_IPV6CP, IDX_IPV6CP,
419 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
425 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
426 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
427 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
431 static const struct cp pap = {
432 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
433 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
434 sppp_pap_TO, 0, 0, 0,
435 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
439 static const struct cp chap = {
440 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
441 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
442 sppp_chap_TO, 0, 0, 0,
443 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
447 static const struct cp *cps[IDX_COUNT] = {
449 &ipcp, /* IDX_IPCP */
450 &ipv6cp, /* IDX_IPV6CP */
452 &chap, /* IDX_CHAP */
456 sppp_alloc(u_char type, struct ifnet *ifp)
460 sp = malloc(sizeof(struct sppp), M_SPPP, M_WAITOK | M_ZERO);
467 sppp_free(void *com, u_char type)
474 sppp_modevent(module_t mod, int type, void *unused)
479 * XXX: should probably be IFT_SPPP, but it's fairly
480 * harmless to allocate struct sppp's for non-sppp
484 if_register_com_alloc(IFT_PPP, sppp_alloc, sppp_free);
487 /* if_deregister_com_alloc(IFT_PPP); */
494 static moduledata_t spppmod = {
499 MODULE_VERSION(sppp, 1);
500 DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
503 * Exported functions, comprising our interface to the lower layer.
507 * Process the received packet.
510 sppp_input(struct ifnet *ifp, struct mbuf *m)
512 struct ppp_header *h;
514 struct sppp *sp = IFP2SP(ifp);
515 int debug, do_account = 0;
522 debug = ifp->if_flags & IFF_DEBUG;
524 if (ifp->if_flags & IFF_UP)
525 /* Count received bytes, add FCS and one flag */
526 ifp->if_ibytes += m->m_pkthdr.len + 3;
528 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
529 /* Too small packet, drop it. */
532 SPP_FMT "input packet is too small, %d bytes\n",
533 SPP_ARGS(ifp), m->m_pkthdr.len);
543 if (sp->pp_mode == PP_FR) {
544 sppp_fr_input (sp, m);
549 /* Get PPP header. */
550 h = mtod (m, struct ppp_header*);
551 m_adj (m, PPP_HEADER_LEN);
553 switch (h->address) {
554 case PPP_ALLSTATIONS:
555 if (h->control != PPP_UI)
557 if (sp->pp_mode == IFF_CISCO) {
560 SPP_FMT "PPP packet in Cisco mode "
561 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
563 h->address, h->control, ntohs(h->protocol));
566 switch (ntohs (h->protocol)) {
570 SPP_FMT "rejecting protocol "
571 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
573 h->address, h->control, ntohs(h->protocol));
574 if (sp->state[IDX_LCP] == STATE_OPENED)
575 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
576 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
581 sppp_cp_input(&lcp, sp, m);
586 if (sp->pp_phase >= PHASE_AUTHENTICATE)
587 sppp_pap_input(sp, m);
592 if (sp->pp_phase >= PHASE_AUTHENTICATE)
593 sppp_chap_input(sp, m);
599 if (sp->pp_phase == PHASE_NETWORK)
600 sppp_cp_input(&ipcp, sp, m);
605 if (sp->state[IDX_IPCP] == STATE_OPENED) {
611 if (sp->state[IDX_IPCP] == STATE_OPENED) {
613 sl_uncompress_tcp_core(mtod(m, u_char *),
617 &iphdr, &hlen)) <= 0) {
620 SPP_FMT "VJ uncompress failed on compressed packet\n",
626 * Trim the VJ header off the packet, and prepend
627 * the uncompressed IP header (which will usually
628 * end up in two chained mbufs since there's not
629 * enough leading space in the existing mbuf).
632 M_PREPEND(m, hlen, M_DONTWAIT);
637 bcopy(iphdr, mtod(m, u_char *), hlen);
643 if (sp->state[IDX_IPCP] == STATE_OPENED) {
644 if (sl_uncompress_tcp_core(mtod(m, u_char *),
646 TYPE_UNCOMPRESSED_TCP,
648 &iphdr, &hlen) != 0) {
651 SPP_FMT "VJ uncompress failed on uncompressed packet\n",
662 if (sp->pp_phase == PHASE_NETWORK)
663 sppp_cp_input(&ipv6cp, sp, m);
669 if (sp->state[IDX_IPV6CP] == STATE_OPENED)
676 /* IPX IPXCP not implemented yet */
677 if (sp->pp_phase == PHASE_NETWORK)
684 case CISCO_MULTICAST:
686 /* Don't check the control field here (RFC 1547). */
687 if (sp->pp_mode != IFF_CISCO) {
690 SPP_FMT "Cisco packet in PPP mode "
691 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
693 h->address, h->control, ntohs(h->protocol));
696 switch (ntohs (h->protocol)) {
700 case CISCO_KEEPALIVE:
701 sppp_cisco_input (sp, m);
725 default: /* Invalid PPP packet. */
729 SPP_FMT "invalid input packet "
730 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
732 h->address, h->control, ntohs(h->protocol));
736 if (! (ifp->if_flags & IFF_UP) || isr == -1)
740 M_SETFIB(m, ifp->if_fib);
742 if (netisr_queue(isr, m)) { /* (0) on success. */
744 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
751 * Do only account for network packets, not for control
752 * packets. This is used by some subsystems to detect
755 sp->pp_last_recv = time_uptime;
759 sppp_ifstart_sched(void *dummy)
761 struct sppp *sp = dummy;
763 sp->if_start(SP2IFP(sp));
766 /* if_start () wrapper function. We use it to schedule real if_start () for
767 * execution. We can't call it directly
770 sppp_ifstart(struct ifnet *ifp)
772 struct sppp *sp = IFP2SP(ifp);
774 if (SPPP_LOCK_OWNED(sp)) {
775 if (callout_pending(&sp->ifstart_callout))
777 callout_reset(&sp->ifstart_callout, 1, sppp_ifstart_sched,
785 * Enqueue transmit packet.
788 sppp_output(struct ifnet *ifp, struct mbuf *m,
789 struct sockaddr *dst, struct route *ro)
791 struct sppp *sp = IFP2SP(ifp);
792 struct ppp_header *h;
793 struct ifqueue *ifq = NULL;
794 int s, error, rv = 0;
796 int ipproto = PPP_IP;
798 int debug = ifp->if_flags & IFF_DEBUG;
803 if (!(ifp->if_flags & IFF_UP) ||
804 (!(ifp->if_flags & IFF_AUTO) &&
805 !(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
815 if ((ifp->if_flags & IFF_AUTO) &&
816 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
821 * Hack to prevent the initialization-time generated
822 * IPv6 multicast packet to erroneously cause a
823 * dialout event in case IPv6 has been
824 * administratively disabled on that interface.
826 if (dst->sa_family == AF_INET6 &&
827 !(sp->confflags & CONF_ENABLE_IPV6))
831 * Interface is not yet running, but auto-dial. Need
832 * to start LCP for it.
834 ifp->if_drv_flags |= IFF_DRV_RUNNING;
841 if (dst->sa_family == AF_INET) {
842 /* XXX Check mbuf length here? */
843 struct ip *ip = mtod (m, struct ip*);
844 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
847 * When using dynamic local IP address assignment by using
848 * 0.0.0.0 as a local address, the first TCP session will
849 * not connect because the local TCP checksum is computed
850 * using 0.0.0.0 which will later become our real IP address
851 * so the TCP checksum computed at the remote end will
852 * become invalid. So we
853 * - don't let packets with src ip addr 0 thru
854 * - we flag TCP packets with src ip 0 as an error
857 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
862 if(ip->ip_p == IPPROTO_TCP)
863 return(EADDRNOTAVAIL);
869 * Put low delay, telnet, rlogin and ftp control packets
870 * in front of the queue or let ALTQ take care.
872 if (ALTQ_IS_ENABLED(&ifp->if_snd))
874 else if (_IF_QFULL(&sp->pp_fastq))
876 else if (ip->ip_tos & IPTOS_LOWDELAY)
878 else if (m->m_len < sizeof *ip + sizeof *tcp)
880 else if (ip->ip_p != IPPROTO_TCP)
882 else if (INTERACTIVE (ntohs (tcp->th_sport)))
884 else if (INTERACTIVE (ntohs (tcp->th_dport)))
888 * Do IP Header compression
890 if (sp->pp_mode != IFF_CISCO && sp->pp_mode != PP_FR &&
891 (sp->ipcp.flags & IPCP_VJ) && ip->ip_p == IPPROTO_TCP)
892 switch (sl_compress_tcp(m, ip, sp->pp_comp,
893 sp->ipcp.compress_cid)) {
894 case TYPE_COMPRESSED_TCP:
895 ipproto = PPP_VJ_COMP;
897 case TYPE_UNCOMPRESSED_TCP:
898 ipproto = PPP_VJ_UCOMP;
913 if (dst->sa_family == AF_INET6) {
914 /* XXX do something tricky here? */
918 if (sp->pp_mode == PP_FR) {
919 /* Add frame relay header. */
920 m = sppp_fr_header (sp, m, dst->sa_family);
927 * Prepend general data packet PPP header. For now, IP only.
929 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
932 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
940 * May want to check size of packet
941 * (albeit due to the implementation it's always enough)
943 h = mtod (m, struct ppp_header*);
944 if (sp->pp_mode == IFF_CISCO) {
945 h->address = CISCO_UNICAST; /* unicast address */
948 h->address = PPP_ALLSTATIONS; /* broadcast address */
949 h->control = PPP_UI; /* Unnumbered Info */
952 switch (dst->sa_family) {
954 case AF_INET: /* Internet Protocol */
955 if (sp->pp_mode == IFF_CISCO)
956 h->protocol = htons (ETHERTYPE_IP);
959 * Don't choke with an ENETDOWN early. It's
960 * possible that we just started dialing out,
961 * so don't drop the packet immediately. If
962 * we notice that we run out of buffer space
963 * below, we will however remember that we are
964 * not ready to carry IP packets, and return
965 * ENETDOWN, as opposed to ENOBUFS.
967 h->protocol = htons(ipproto);
968 if (sp->state[IDX_IPCP] != STATE_OPENED)
974 case AF_INET6: /* Internet Protocol */
975 if (sp->pp_mode == IFF_CISCO)
976 h->protocol = htons (ETHERTYPE_IPV6);
979 * Don't choke with an ENETDOWN early. It's
980 * possible that we just started dialing out,
981 * so don't drop the packet immediately. If
982 * we notice that we run out of buffer space
983 * below, we will however remember that we are
984 * not ready to carry IP packets, and return
985 * ENETDOWN, as opposed to ENOBUFS.
987 h->protocol = htons(PPP_IPV6);
988 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
994 case AF_IPX: /* Novell IPX Protocol */
995 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
996 ETHERTYPE_IPX : PPP_IPX);
1004 return (EAFNOSUPPORT);
1008 * Queue message on interface, and start output if interface
1013 error = !(IF_HANDOFF_ADJ(ifq, m, ifp, 3));
1015 IFQ_HANDOFF_ADJ(ifp, m, 3, error);
1020 return (rv? rv: ENOBUFS);
1025 * Unlike in sppp_input(), we can always bump the timestamp
1026 * here since sppp_output() is only called on behalf of
1027 * network-layer traffic; control-layer traffic is handled
1028 * by sppp_cp_send().
1030 sp->pp_last_sent = time_uptime;
1035 sppp_attach(struct ifnet *ifp)
1037 struct sppp *sp = IFP2SP(ifp);
1039 /* Initialize mtx lock */
1040 mtx_init(&sp->mtx, "sppp", MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
1042 /* Initialize keepalive handler. */
1043 callout_init(&sp->keepalive_callout, CALLOUT_MPSAFE);
1044 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
1047 ifp->if_mtu = PP_MTU;
1048 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
1049 ifp->if_output = sppp_output;
1051 sp->pp_flags = PP_KEEPALIVE;
1053 ifp->if_snd.ifq_maxlen = 32;
1054 sp->pp_fastq.ifq_maxlen = 32;
1055 sp->pp_cpq.ifq_maxlen = 20;
1057 sp->pp_alivecnt = 0;
1058 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
1059 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
1060 sp->pp_phase = PHASE_DEAD;
1061 sp->pp_up = sppp_pp_up;
1062 sp->pp_down = sppp_pp_down;
1063 if(!mtx_initialized(&sp->pp_cpq.ifq_mtx))
1064 mtx_init(&sp->pp_cpq.ifq_mtx, "sppp_cpq", NULL, MTX_DEF);
1065 if(!mtx_initialized(&sp->pp_fastq.ifq_mtx))
1066 mtx_init(&sp->pp_fastq.ifq_mtx, "sppp_fastq", NULL, MTX_DEF);
1067 sp->pp_last_recv = sp->pp_last_sent = time_uptime;
1070 sp->confflags |= CONF_ENABLE_VJ;
1073 sp->confflags |= CONF_ENABLE_IPV6;
1075 callout_init(&sp->ifstart_callout, CALLOUT_MPSAFE);
1076 sp->if_start = ifp->if_start;
1077 ifp->if_start = sppp_ifstart;
1078 sp->pp_comp = malloc(sizeof(struct slcompress), M_TEMP, M_WAITOK);
1079 sl_compress_init(sp->pp_comp, -1);
1082 sppp_ipv6cp_init(sp);
1088 sppp_detach(struct ifnet *ifp)
1090 struct sppp *sp = IFP2SP(ifp);
1093 KASSERT(mtx_initialized(&sp->mtx), ("sppp mutex is not initialized"));
1095 /* Stop keepalive handler. */
1096 if (!callout_drain(&sp->keepalive_callout))
1097 callout_stop(&sp->keepalive_callout);
1099 for (i = 0; i < IDX_COUNT; i++) {
1100 if (!callout_drain(&sp->ch[i]))
1101 callout_stop(&sp->ch[i]);
1103 if (!callout_drain(&sp->pap_my_to_ch))
1104 callout_stop(&sp->pap_my_to_ch);
1105 mtx_destroy(&sp->pp_cpq.ifq_mtx);
1106 mtx_destroy(&sp->pp_fastq.ifq_mtx);
1107 mtx_destroy(&sp->mtx);
1111 * Flush the interface output queue.
1114 sppp_flush_unlocked(struct ifnet *ifp)
1116 struct sppp *sp = IFP2SP(ifp);
1118 sppp_qflush ((struct ifqueue *)&SP2IFP(sp)->if_snd);
1119 sppp_qflush (&sp->pp_fastq);
1120 sppp_qflush (&sp->pp_cpq);
1124 sppp_flush(struct ifnet *ifp)
1126 struct sppp *sp = IFP2SP(ifp);
1129 sppp_flush_unlocked (ifp);
1134 * Check if the output queue is empty.
1137 sppp_isempty(struct ifnet *ifp)
1139 struct sppp *sp = IFP2SP(ifp);
1144 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
1145 !SP2IFP(sp)->if_snd.ifq_head;
1152 * Get next packet to send.
1155 sppp_dequeue(struct ifnet *ifp)
1157 struct sppp *sp = IFP2SP(ifp);
1164 * Process only the control protocol queue until we have at
1165 * least one NCP open.
1167 * Do always serve all three queues in Cisco mode.
1169 IF_DEQUEUE(&sp->pp_cpq, m);
1171 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO ||
1172 sp->pp_mode == PP_FR)) {
1173 IF_DEQUEUE(&sp->pp_fastq, m);
1175 IF_DEQUEUE (&SP2IFP(sp)->if_snd, m);
1183 * Pick the next packet, do not remove it from the queue.
1186 sppp_pick(struct ifnet *ifp)
1188 struct sppp *sp = IFP2SP(ifp);
1195 m = sp->pp_cpq.ifq_head;
1197 (sp->pp_phase == PHASE_NETWORK ||
1198 sp->pp_mode == IFF_CISCO ||
1199 sp->pp_mode == PP_FR))
1200 if ((m = sp->pp_fastq.ifq_head) == NULL)
1201 m = SP2IFP(sp)->if_snd.ifq_head;
1208 * Process an ioctl request. Called on low priority level.
1211 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1213 struct ifreq *ifr = (struct ifreq*) data;
1214 struct sppp *sp = IFP2SP(ifp);
1215 int s, rv, going_up, going_down, newmode;
1222 case SIOCSIFDSTADDR:
1226 /* set the interface "up" when assigning an IP address */
1227 ifp->if_flags |= IFF_UP;
1231 going_up = ifp->if_flags & IFF_UP &&
1232 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0;
1233 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1234 ifp->if_drv_flags & IFF_DRV_RUNNING;
1236 newmode = ifp->if_flags & IFF_PASSIVE;
1238 newmode = ifp->if_flags & IFF_AUTO;
1240 newmode = ifp->if_flags & IFF_CISCO;
1241 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1242 ifp->if_flags |= newmode;
1245 newmode = sp->pp_flags & PP_FR;
1247 if (newmode != sp->pp_mode) {
1250 going_up = ifp->if_drv_flags & IFF_DRV_RUNNING;
1254 if (sp->pp_mode != IFF_CISCO &&
1255 sp->pp_mode != PP_FR)
1257 else if (sp->pp_tlf)
1259 sppp_flush_unlocked(ifp);
1260 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1261 sp->pp_mode = newmode;
1265 if (sp->pp_mode != IFF_CISCO &&
1266 sp->pp_mode != PP_FR)
1268 sp->pp_mode = newmode;
1269 if (sp->pp_mode == 0) {
1270 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1273 if ((sp->pp_mode == IFF_CISCO) ||
1274 (sp->pp_mode == PP_FR)) {
1277 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1285 #define ifr_mtu ifr_metric
1288 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1290 ifp->if_mtu = ifr->ifr_mtu;
1295 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1297 ifp->if_mtu = *(short*)data;
1302 ifr->ifr_mtu = ifp->if_mtu;
1307 *(short*)data = ifp->if_mtu;
1314 case SIOCGIFGENERIC:
1315 case SIOCSIFGENERIC:
1316 rv = sppp_params(sp, cmd, data);
1328 * Cisco framing implementation.
1332 * Handle incoming Cisco keepalive protocol packets.
1335 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1338 struct cisco_packet *h;
1341 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1344 SPP_FMT "cisco invalid packet length: %d bytes\n",
1345 SPP_ARGS(ifp), m->m_pkthdr.len);
1348 h = mtod (m, struct cisco_packet*);
1351 SPP_FMT "cisco input: %d bytes "
1352 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1353 SPP_ARGS(ifp), m->m_pkthdr.len,
1354 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1355 (u_int)h->time0, (u_int)h->time1);
1356 switch (ntohl (h->type)) {
1359 log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n",
1360 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1362 case CISCO_ADDR_REPLY:
1363 /* Reply on address request, ignore */
1365 case CISCO_KEEPALIVE_REQ:
1366 sp->pp_alivecnt = 0;
1367 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1368 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1369 /* Local and remote sequence numbers are equal.
1370 * Probably, the line is in loopback mode. */
1371 if (sp->pp_loopcnt >= MAXALIVECNT) {
1372 printf (SPP_FMT "loopback\n",
1375 if (ifp->if_flags & IFF_UP) {
1377 sppp_qflush (&sp->pp_cpq);
1382 /* Generate new local sequence number */
1383 sp->pp_seq[IDX_LCP] = random();
1387 if (! (ifp->if_flags & IFF_UP) &&
1388 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1390 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1393 case CISCO_ADDR_REQ:
1394 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1396 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1402 * Send Cisco keepalive packet.
1405 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1408 struct ppp_header *h;
1409 struct cisco_packet *ch;
1413 getmicrouptime(&tv);
1415 MGETHDR (m, M_DONTWAIT, MT_DATA);
1418 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1419 m->m_pkthdr.rcvif = 0;
1421 h = mtod (m, struct ppp_header*);
1422 h->address = CISCO_MULTICAST;
1424 h->protocol = htons (CISCO_KEEPALIVE);
1426 ch = (struct cisco_packet*) (h + 1);
1427 ch->type = htonl (type);
1428 ch->par1 = htonl (par1);
1429 ch->par2 = htonl (par2);
1432 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1433 ch->time1 = htons ((u_short) tv.tv_sec);
1437 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1438 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1439 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1441 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1446 * PPP protocol implementation.
1450 * Send PPP control protocol packet.
1453 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1454 u_char ident, u_short len, void *data)
1457 struct ppp_header *h;
1458 struct lcp_header *lh;
1461 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1462 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1463 MGETHDR (m, M_DONTWAIT, MT_DATA);
1466 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1467 m->m_pkthdr.rcvif = 0;
1469 h = mtod (m, struct ppp_header*);
1470 h->address = PPP_ALLSTATIONS; /* broadcast address */
1471 h->control = PPP_UI; /* Unnumbered Info */
1472 h->protocol = htons (proto); /* Link Control Protocol */
1474 lh = (struct lcp_header*) (h + 1);
1477 lh->len = htons (LCP_HEADER_LEN + len);
1479 bcopy (data, lh+1, len);
1482 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1484 sppp_proto_name(proto),
1485 sppp_cp_type_name (lh->type), lh->ident,
1487 sppp_print_bytes ((u_char*) (lh+1), len);
1490 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1495 * Handle incoming PPP control protocol packets.
1498 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1501 struct lcp_header *h;
1502 int len = m->m_pkthdr.len;
1509 SPP_FMT "%s invalid packet length: %d bytes\n",
1510 SPP_ARGS(ifp), cp->name, len);
1513 h = mtod (m, struct lcp_header*);
1516 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1517 SPP_ARGS(ifp), cp->name,
1518 sppp_state_name(sp->state[cp->protoidx]),
1519 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1520 sppp_print_bytes ((u_char*) (h+1), len-4);
1523 if (len > ntohs (h->len))
1524 len = ntohs (h->len);
1525 p = (u_char *)(h + 1);
1530 log(-1, SPP_FMT "%s invalid conf-req length %d\n",
1531 SPP_ARGS(ifp), cp->name,
1536 /* handle states where RCR doesn't get a SCA/SCN */
1537 switch (sp->state[cp->protoidx]) {
1539 case STATE_STOPPING:
1542 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1546 rv = (cp->RCR)(sp, h, len);
1547 switch (sp->state[cp->protoidx]) {
1552 case STATE_ACK_SENT:
1553 case STATE_REQ_SENT:
1555 * sppp_cp_change_state() have the side effect of
1556 * restarting the timeouts. We want to avoid that
1557 * if the state don't change, otherwise we won't
1558 * ever timeout and resend a configuration request
1561 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1564 sppp_cp_change_state(cp, sp, rv?
1565 STATE_ACK_SENT: STATE_REQ_SENT);
1568 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1570 sppp_cp_change_state(cp, sp, rv?
1571 STATE_ACK_SENT: STATE_REQ_SENT);
1573 case STATE_ACK_RCVD:
1575 sppp_cp_change_state(cp, sp, STATE_OPENED);
1577 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1582 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
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]));
1593 if (h->ident != sp->confid[cp->protoidx]) {
1595 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1596 SPP_ARGS(ifp), cp->name,
1597 h->ident, sp->confid[cp->protoidx]);
1601 switch (sp->state[cp->protoidx]) {
1604 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1607 case STATE_STOPPING:
1609 case STATE_REQ_SENT:
1610 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1611 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1616 case STATE_ACK_RCVD:
1618 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1620 case STATE_ACK_SENT:
1621 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1622 sppp_cp_change_state(cp, sp, STATE_OPENED);
1624 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1625 SPP_ARGS(ifp), cp->name);
1629 printf(SPP_FMT "%s illegal %s in state %s\n",
1630 SPP_ARGS(ifp), cp->name,
1631 sppp_cp_type_name(h->type),
1632 sppp_state_name(sp->state[cp->protoidx]));
1638 if (h->ident != sp->confid[cp->protoidx]) {
1640 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1641 SPP_ARGS(ifp), cp->name,
1642 h->ident, sp->confid[cp->protoidx]);
1646 if (h->type == CONF_NAK)
1647 (cp->RCN_nak)(sp, h, len);
1649 (cp->RCN_rej)(sp, h, len);
1651 switch (sp->state[cp->protoidx]) {
1654 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1656 case STATE_REQ_SENT:
1657 case STATE_ACK_SENT:
1658 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1660 * Slow things down a bit if we think we might be
1661 * in loopback. Depend on the timeout to send the
1662 * next configuration request.
1671 case STATE_ACK_RCVD:
1672 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1676 case STATE_STOPPING:
1679 printf(SPP_FMT "%s illegal %s in state %s\n",
1680 SPP_ARGS(ifp), cp->name,
1681 sppp_cp_type_name(h->type),
1682 sppp_state_name(sp->state[cp->protoidx]));
1688 switch (sp->state[cp->protoidx]) {
1689 case STATE_ACK_RCVD:
1690 case STATE_ACK_SENT:
1691 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1696 case STATE_STOPPING:
1697 case STATE_REQ_SENT:
1699 /* Send Terminate-Ack packet. */
1701 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1702 SPP_ARGS(ifp), cp->name);
1703 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1707 sp->rst_counter[cp->protoidx] = 0;
1708 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1712 printf(SPP_FMT "%s illegal %s in state %s\n",
1713 SPP_ARGS(ifp), cp->name,
1714 sppp_cp_type_name(h->type),
1715 sppp_state_name(sp->state[cp->protoidx]));
1720 switch (sp->state[cp->protoidx]) {
1723 case STATE_REQ_SENT:
1724 case STATE_ACK_SENT:
1727 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1730 case STATE_STOPPING:
1731 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1734 case STATE_ACK_RCVD:
1735 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1740 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1743 printf(SPP_FMT "%s illegal %s in state %s\n",
1744 SPP_ARGS(ifp), cp->name,
1745 sppp_cp_type_name(h->type),
1746 sppp_state_name(sp->state[cp->protoidx]));
1751 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1753 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1754 "danger will robinson\n",
1755 SPP_ARGS(ifp), cp->name,
1756 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1757 switch (sp->state[cp->protoidx]) {
1760 case STATE_REQ_SENT:
1761 case STATE_ACK_SENT:
1763 case STATE_STOPPING:
1766 case STATE_ACK_RCVD:
1767 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1770 printf(SPP_FMT "%s illegal %s in state %s\n",
1771 SPP_ARGS(ifp), cp->name,
1772 sppp_cp_type_name(h->type),
1773 sppp_state_name(sp->state[cp->protoidx]));
1780 const struct cp *upper;
1786 proto = ntohs(*((u_int16_t *)p));
1787 for (i = 0; i < IDX_COUNT; i++) {
1788 if (cps[i]->proto == proto) {
1796 if (catastrophic || debug)
1797 log(catastrophic? LOG_INFO: LOG_DEBUG,
1798 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1799 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1800 sppp_cp_type_name(h->type), proto,
1801 upper ? upper->name : "unknown",
1802 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1805 * if we got RXJ+ against conf-req, the peer does not implement
1806 * this particular protocol type. terminate the protocol.
1808 if (upper && !catastrophic) {
1809 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1815 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1816 switch (sp->state[cp->protoidx]) {
1819 case STATE_REQ_SENT:
1820 case STATE_ACK_SENT:
1822 case STATE_STOPPING:
1825 case STATE_ACK_RCVD:
1826 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1829 printf(SPP_FMT "%s illegal %s in state %s\n",
1830 SPP_ARGS(ifp), cp->name,
1831 sppp_cp_type_name(h->type),
1832 sppp_state_name(sp->state[cp->protoidx]));
1838 if (cp->proto != PPP_LCP)
1840 /* Discard the packet. */
1843 if (cp->proto != PPP_LCP)
1845 if (sp->state[cp->protoidx] != STATE_OPENED) {
1847 log(-1, SPP_FMT "lcp echo req but lcp closed\n",
1854 log(-1, SPP_FMT "invalid lcp echo request "
1855 "packet length: %d bytes\n",
1856 SPP_ARGS(ifp), len);
1859 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1860 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1861 /* Line loopback mode detected. */
1862 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1863 sp->pp_loopcnt = MAXALIVECNT * 5;
1865 sppp_qflush (&sp->pp_cpq);
1867 /* Shut down the PPP link. */
1873 *(long*)(h+1) = htonl (sp->lcp.magic);
1875 log(-1, SPP_FMT "got lcp echo req, sending echo rep\n",
1877 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1880 if (cp->proto != PPP_LCP)
1882 if (h->ident != sp->lcp.echoid) {
1888 log(-1, SPP_FMT "lcp invalid echo reply "
1889 "packet length: %d bytes\n",
1890 SPP_ARGS(ifp), len);
1894 log(-1, SPP_FMT "lcp got echo rep\n",
1896 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1897 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1898 sp->pp_alivecnt = 0;
1901 /* Unknown packet type -- send Code-Reject packet. */
1904 log(-1, SPP_FMT "%s send code-rej for 0x%x\n",
1905 SPP_ARGS(ifp), cp->name, h->type);
1906 sppp_cp_send(sp, cp->proto, CODE_REJ,
1907 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1914 * The generic part of all Up/Down/Open/Close/TO event handlers.
1915 * Basically, the state transition handling in the automaton.
1918 sppp_up_event(const struct cp *cp, struct sppp *sp)
1923 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1924 SPP_ARGS(ifp), cp->name,
1925 sppp_state_name(sp->state[cp->protoidx]));
1927 switch (sp->state[cp->protoidx]) {
1929 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1931 case STATE_STARTING:
1932 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1934 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1937 printf(SPP_FMT "%s illegal up in state %s\n",
1938 SPP_ARGS(ifp), cp->name,
1939 sppp_state_name(sp->state[cp->protoidx]));
1944 sppp_down_event(const struct cp *cp, struct sppp *sp)
1949 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1950 SPP_ARGS(ifp), cp->name,
1951 sppp_state_name(sp->state[cp->protoidx]));
1953 switch (sp->state[cp->protoidx]) {
1956 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1959 sppp_cp_change_state(cp, sp, STATE_STARTING);
1962 case STATE_STOPPING:
1963 case STATE_REQ_SENT:
1964 case STATE_ACK_RCVD:
1965 case STATE_ACK_SENT:
1966 sppp_cp_change_state(cp, sp, STATE_STARTING);
1970 sppp_cp_change_state(cp, sp, STATE_STARTING);
1973 printf(SPP_FMT "%s illegal down in state %s\n",
1974 SPP_ARGS(ifp), cp->name,
1975 sppp_state_name(sp->state[cp->protoidx]));
1981 sppp_open_event(const struct cp *cp, struct sppp *sp)
1986 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1987 SPP_ARGS(ifp), cp->name,
1988 sppp_state_name(sp->state[cp->protoidx]));
1990 switch (sp->state[cp->protoidx]) {
1992 sppp_cp_change_state(cp, sp, STATE_STARTING);
1995 case STATE_STARTING:
1998 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
2000 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2004 * Try escaping stopped state. This seems to bite
2005 * people occasionally, in particular for IPCP,
2006 * presumably following previous IPCP negotiation
2007 * aborts. Somehow, we must have missed a Down event
2008 * which would have caused a transition into starting
2009 * state, so as a bandaid we force the Down event now.
2010 * This effectively implements (something like the)
2011 * `restart' option mentioned in the state transition
2012 * table of RFC 1661.
2014 sppp_cp_change_state(cp, sp, STATE_STARTING);
2017 case STATE_STOPPING:
2018 case STATE_REQ_SENT:
2019 case STATE_ACK_RCVD:
2020 case STATE_ACK_SENT:
2024 sppp_cp_change_state(cp, sp, STATE_STOPPING);
2031 sppp_close_event(const struct cp *cp, struct sppp *sp)
2036 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
2037 SPP_ARGS(ifp), cp->name,
2038 sppp_state_name(sp->state[cp->protoidx]));
2040 switch (sp->state[cp->protoidx]) {
2045 case STATE_STARTING:
2046 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2050 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2052 case STATE_STOPPING:
2053 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2058 case STATE_REQ_SENT:
2059 case STATE_ACK_RCVD:
2060 case STATE_ACK_SENT:
2061 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2062 sppp_cp_send(sp, cp->proto, TERM_REQ,
2063 ++sp->pp_seq[cp->protoidx], 0, 0);
2064 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2070 sppp_to_event(const struct cp *cp, struct sppp *sp)
2078 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
2079 SPP_ARGS(ifp), cp->name,
2080 sppp_state_name(sp->state[cp->protoidx]),
2081 sp->rst_counter[cp->protoidx]);
2083 if (--sp->rst_counter[cp->protoidx] < 0)
2085 switch (sp->state[cp->protoidx]) {
2087 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2090 case STATE_STOPPING:
2091 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2094 case STATE_REQ_SENT:
2095 case STATE_ACK_RCVD:
2096 case STATE_ACK_SENT:
2097 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2103 switch (sp->state[cp->protoidx]) {
2105 case STATE_STOPPING:
2106 sppp_cp_send(sp, cp->proto, TERM_REQ,
2107 ++sp->pp_seq[cp->protoidx], 0, 0);
2108 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2109 cp->TO, (void *)sp);
2111 case STATE_REQ_SENT:
2112 case STATE_ACK_RCVD:
2114 /* sppp_cp_change_state() will restart the timer */
2115 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2117 case STATE_ACK_SENT:
2119 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2120 cp->TO, (void *)sp);
2129 * Change the state of a control protocol in the state automaton.
2130 * Takes care of starting/stopping the restart timer.
2133 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2135 sp->state[cp->protoidx] = newstate;
2137 callout_stop (&sp->ch[cp->protoidx]);
2141 case STATE_STARTING:
2147 case STATE_STOPPING:
2148 case STATE_REQ_SENT:
2149 case STATE_ACK_RCVD:
2150 case STATE_ACK_SENT:
2151 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2152 cp->TO, (void *)sp);
2158 *--------------------------------------------------------------------------*
2160 * The LCP implementation. *
2162 *--------------------------------------------------------------------------*
2165 sppp_pp_up(struct sppp *sp)
2173 sppp_pp_down(struct sppp *sp)
2181 sppp_lcp_init(struct sppp *sp)
2183 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2185 sp->state[IDX_LCP] = STATE_INITIAL;
2186 sp->fail_counter[IDX_LCP] = 0;
2187 sp->pp_seq[IDX_LCP] = 0;
2188 sp->pp_rseq[IDX_LCP] = 0;
2190 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2192 /* Note that these values are relevant for all control protocols */
2193 sp->lcp.timeout = 3 * hz;
2194 sp->lcp.max_terminate = 2;
2195 sp->lcp.max_configure = 10;
2196 sp->lcp.max_failure = 10;
2197 callout_init(&sp->ch[IDX_LCP], CALLOUT_MPSAFE);
2201 sppp_lcp_up(struct sppp *sp)
2205 sp->pp_alivecnt = 0;
2206 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2209 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2211 * If we are authenticator, negotiate LCP_AUTH
2213 if (sp->hisauth.proto != 0)
2214 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2216 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2217 sp->pp_flags &= ~PP_NEEDAUTH;
2219 * If this interface is passive or dial-on-demand, and we are
2220 * still in Initial state, it means we've got an incoming
2221 * call. Activate the interface.
2223 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2226 SPP_FMT "Up event", SPP_ARGS(ifp));
2227 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2228 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2230 log(-1, "(incoming call)\n");
2231 sp->pp_flags |= PP_CALLIN;
2235 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2236 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2237 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2241 sppp_up_event(&lcp, sp);
2245 sppp_lcp_down(struct sppp *sp)
2249 sppp_down_event(&lcp, sp);
2252 * If this is neither a dial-on-demand nor a passive
2253 * interface, simulate an ``ifconfig down'' action, so the
2254 * administrator can force a redial by another ``ifconfig
2255 * up''. XXX For leased line operation, should we immediately
2256 * try to reopen the connection here?
2258 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2260 SPP_FMT "Down event, taking interface down.\n",
2266 SPP_FMT "Down event (carrier loss)\n",
2268 sp->pp_flags &= ~PP_CALLIN;
2269 if (sp->state[IDX_LCP] != STATE_INITIAL)
2271 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2276 sppp_lcp_open(struct sppp *sp)
2278 sppp_open_event(&lcp, sp);
2282 sppp_lcp_close(struct sppp *sp)
2284 sppp_close_event(&lcp, sp);
2288 sppp_lcp_TO(void *cookie)
2290 sppp_to_event(&lcp, (struct sppp *)cookie);
2294 * Analyze a configure request. Return true if it was agreeable, and
2295 * caused action sca, false if it has been rejected or nak'ed, and
2296 * caused action scn. (The return value is used to make the state
2297 * transition decision in the state automaton.)
2300 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2303 u_char *buf, *r, *p;
2310 buf = r = malloc (len, M_TEMP, M_NOWAIT);
2315 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2318 /* pass 1: check for things that need to be rejected */
2320 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2321 len-=p[1], p+=p[1]) {
2323 log(-1, " %s ", sppp_lcp_opt_name(*p));
2327 if (len >= 6 && p[1] == 6)
2330 log(-1, "[invalid] ");
2332 case LCP_OPT_ASYNC_MAP:
2333 /* Async control character map. */
2334 if (len >= 6 && p[1] == 6)
2337 log(-1, "[invalid] ");
2340 /* Maximum receive unit. */
2341 if (len >= 4 && p[1] == 4)
2344 log(-1, "[invalid] ");
2346 case LCP_OPT_AUTH_PROTO:
2349 log(-1, "[invalid] ");
2352 authproto = (p[2] << 8) + p[3];
2353 if (authproto == PPP_CHAP && p[1] != 5) {
2355 log(-1, "[invalid chap len] ");
2358 if (sp->myauth.proto == 0) {
2359 /* we are not configured to do auth */
2361 log(-1, "[not configured] ");
2365 * Remote want us to authenticate, remember this,
2366 * so we stay in PHASE_AUTHENTICATE after LCP got
2369 sp->pp_flags |= PP_NEEDAUTH;
2372 /* Others not supported. */
2377 /* Add the option to rejected list. */
2384 log(-1, " send conf-rej\n");
2385 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2391 * pass 2: check for option values that are unacceptable and
2392 * thus require to be nak'ed.
2395 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2400 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2401 len-=p[1], p+=p[1]) {
2403 log(-1, " %s ", sppp_lcp_opt_name(*p));
2406 /* Magic number -- extract. */
2407 nmagic = (u_long)p[2] << 24 |
2408 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2409 if (nmagic != sp->lcp.magic) {
2412 log(-1, "0x%lx ", nmagic);
2415 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2416 log(-1, "[glitch] ");
2419 * We negate our magic here, and NAK it. If
2420 * we see it later in an NAK packet, we
2421 * suggest a new one.
2423 nmagic = ~sp->lcp.magic;
2425 p[2] = nmagic >> 24;
2426 p[3] = nmagic >> 16;
2431 case LCP_OPT_ASYNC_MAP:
2433 * Async control character map -- just ignore it.
2435 * Quote from RFC 1662, chapter 6:
2436 * To enable this functionality, synchronous PPP
2437 * implementations MUST always respond to the
2438 * Async-Control-Character-Map Configuration
2439 * Option with the LCP Configure-Ack. However,
2440 * acceptance of the Configuration Option does
2441 * not imply that the synchronous implementation
2442 * will do any ACCM mapping. Instead, all such
2443 * octet mapping will be performed by the
2444 * asynchronous-to-synchronous converter.
2450 * Maximum receive unit. Always agreeable,
2451 * but ignored by now.
2453 sp->lcp.their_mru = p[2] * 256 + p[3];
2455 log(-1, "%lu ", sp->lcp.their_mru);
2458 case LCP_OPT_AUTH_PROTO:
2459 authproto = (p[2] << 8) + p[3];
2460 if (sp->myauth.proto != authproto) {
2461 /* not agreed, nak */
2463 log(-1, "[mine %s != his %s] ",
2464 sppp_proto_name(sp->hisauth.proto),
2465 sppp_proto_name(authproto));
2466 p[2] = sp->myauth.proto >> 8;
2467 p[3] = sp->myauth.proto;
2470 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2472 log(-1, "[chap not MD5] ");
2478 /* Add the option to nak'ed list. */
2485 * Local and remote magics equal -- loopback?
2487 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2488 if (sp->pp_loopcnt == MAXALIVECNT*5)
2489 printf (SPP_FMT "loopback\n",
2491 if (ifp->if_flags & IFF_UP) {
2493 sppp_qflush(&sp->pp_cpq);
2498 } else if (!sp->pp_loopcnt &&
2499 ++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2501 log(-1, " max_failure (%d) exceeded, "
2503 sp->lcp.max_failure);
2504 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2507 log(-1, " send conf-nak\n");
2508 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2512 log(-1, " send conf-ack\n");
2513 sp->fail_counter[IDX_LCP] = 0;
2515 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2516 h->ident, origlen, h+1);
2524 * Analyze the LCP Configure-Reject option list, and adjust our
2528 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2534 buf = malloc (len, M_TEMP, M_NOWAIT);
2539 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2543 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2544 len -= p[1], p += p[1]) {
2546 log(-1, " %s ", sppp_lcp_opt_name(*p));
2549 /* Magic number -- can't use it, use 0 */
2550 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2555 * Should not be rejected anyway, since we only
2556 * negotiate a MRU if explicitly requested by
2559 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2561 case LCP_OPT_AUTH_PROTO:
2563 * Peer doesn't want to authenticate himself,
2564 * deny unless this is a dialout call, and
2565 * AUTHFLAG_NOCALLOUT is set.
2567 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2568 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2570 log(-1, "[don't insist on auth "
2572 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2576 log(-1, "[access denied]\n");
2588 * Analyze the LCP Configure-NAK option list, and adjust our
2592 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2599 buf = malloc (len, M_TEMP, M_NOWAIT);
2604 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2608 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2609 len -= p[1], p += p[1]) {
2611 log(-1, " %s ", sppp_lcp_opt_name(*p));
2614 /* Magic number -- renegotiate */
2615 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2616 len >= 6 && p[1] == 6) {
2617 magic = (u_long)p[2] << 24 |
2618 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2620 * If the remote magic is our negated one,
2621 * this looks like a loopback problem.
2622 * Suggest a new magic to make sure.
2624 if (magic == ~sp->lcp.magic) {
2626 log(-1, "magic glitch ");
2627 sp->lcp.magic = random();
2629 sp->lcp.magic = magic;
2631 log(-1, "%lu ", magic);
2637 * Peer wants to advise us to negotiate an MRU.
2638 * Agree on it if it's reasonable, or use
2639 * default otherwise.
2641 if (len >= 4 && p[1] == 4) {
2642 u_int mru = p[2] * 256 + p[3];
2644 log(-1, "%d ", mru);
2645 if (mru < PP_MTU || mru > PP_MAX_MRU)
2648 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2651 case LCP_OPT_AUTH_PROTO:
2653 * Peer doesn't like our authentication method,
2657 log(-1, "[access denied]\n");
2669 sppp_lcp_tlu(struct sppp *sp)
2676 if (! (ifp->if_flags & IFF_UP) &&
2677 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2678 /* Coming out of loopback mode. */
2680 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2683 for (i = 0; i < IDX_COUNT; i++)
2684 if ((cps[i])->flags & CP_QUAL)
2687 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2688 (sp->pp_flags & PP_NEEDAUTH) != 0)
2689 sp->pp_phase = PHASE_AUTHENTICATE;
2691 sp->pp_phase = PHASE_NETWORK;
2694 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2695 sppp_phase_name(sp->pp_phase));
2698 * Open all authentication protocols. This is even required
2699 * if we already proceeded to network phase, since it might be
2700 * that remote wants us to authenticate, so we might have to
2701 * send a PAP request. Undesired authentication protocols
2702 * don't do anything when they get an Open event.
2704 for (i = 0; i < IDX_COUNT; i++)
2705 if ((cps[i])->flags & CP_AUTH)
2708 if (sp->pp_phase == PHASE_NETWORK) {
2709 /* Notify all NCPs. */
2710 for (i = 0; i < IDX_COUNT; i++)
2711 if (((cps[i])->flags & CP_NCP) &&
2714 * Hack to administratively disable IPv6 if
2715 * not desired. Perhaps we should have another
2716 * flag for this, but right now, we can make
2717 * all struct cp's read/only.
2719 (cps[i] != &ipv6cp ||
2720 (sp->confflags & CONF_ENABLE_IPV6)))
2724 /* Send Up events to all started protos. */
2725 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2726 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2729 /* notify low-level driver of state change */
2731 sp->pp_chg(sp, (int)sp->pp_phase);
2733 if (sp->pp_phase == PHASE_NETWORK)
2734 /* if no NCP is starting, close down */
2735 sppp_lcp_check_and_close(sp);
2739 sppp_lcp_tld(struct sppp *sp)
2745 sp->pp_phase = PHASE_TERMINATE;
2748 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2749 sppp_phase_name(sp->pp_phase));
2752 * Take upper layers down. We send the Down event first and
2753 * the Close second to prevent the upper layers from sending
2754 * ``a flurry of terminate-request packets'', as the RFC
2757 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2758 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2760 (cps[i])->Close(sp);
2765 sppp_lcp_tls(struct sppp *sp)
2769 sp->pp_phase = PHASE_ESTABLISH;
2772 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2773 sppp_phase_name(sp->pp_phase));
2775 /* Notify lower layer if desired. */
2783 sppp_lcp_tlf(struct sppp *sp)
2787 sp->pp_phase = PHASE_DEAD;
2789 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2790 sppp_phase_name(sp->pp_phase));
2792 /* Notify lower layer if desired. */
2800 sppp_lcp_scr(struct sppp *sp)
2802 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2806 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2807 if (! sp->lcp.magic)
2808 sp->lcp.magic = random();
2809 opt[i++] = LCP_OPT_MAGIC;
2811 opt[i++] = sp->lcp.magic >> 24;
2812 opt[i++] = sp->lcp.magic >> 16;
2813 opt[i++] = sp->lcp.magic >> 8;
2814 opt[i++] = sp->lcp.magic;
2817 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2818 opt[i++] = LCP_OPT_MRU;
2820 opt[i++] = sp->lcp.mru >> 8;
2821 opt[i++] = sp->lcp.mru;
2824 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2825 authproto = sp->hisauth.proto;
2826 opt[i++] = LCP_OPT_AUTH_PROTO;
2827 opt[i++] = authproto == PPP_CHAP? 5: 4;
2828 opt[i++] = authproto >> 8;
2829 opt[i++] = authproto;
2830 if (authproto == PPP_CHAP)
2831 opt[i++] = CHAP_MD5;
2834 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2835 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2839 * Check the open NCPs, return true if at least one NCP is open.
2842 sppp_ncp_check(struct sppp *sp)
2846 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2847 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2853 * Re-check the open NCPs and see if we should terminate the link.
2854 * Called by the NCPs during their tlf action handling.
2857 sppp_lcp_check_and_close(struct sppp *sp)
2860 if (sp->pp_phase < PHASE_NETWORK)
2861 /* don't bother, we are already going down */
2864 if (sppp_ncp_check(sp))
2871 *--------------------------------------------------------------------------*
2873 * The IPCP implementation. *
2875 *--------------------------------------------------------------------------*
2880 sppp_ipcp_init(struct sppp *sp)
2884 sp->state[IDX_IPCP] = STATE_INITIAL;
2885 sp->fail_counter[IDX_IPCP] = 0;
2886 sp->pp_seq[IDX_IPCP] = 0;
2887 sp->pp_rseq[IDX_IPCP] = 0;
2888 callout_init(&sp->ch[IDX_IPCP], CALLOUT_MPSAFE);
2892 sppp_ipcp_up(struct sppp *sp)
2894 sppp_up_event(&ipcp, sp);
2898 sppp_ipcp_down(struct sppp *sp)
2900 sppp_down_event(&ipcp, sp);
2904 sppp_ipcp_open(struct sppp *sp)
2907 u_long myaddr, hisaddr;
2909 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2910 IPCP_MYADDR_DYN | IPCP_VJ);
2913 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2915 * If we don't have his address, this probably means our
2916 * interface doesn't want to talk IP at all. (This could
2917 * be the case if somebody wants to speak only IPX, for
2918 * example.) Don't open IPCP in this case.
2920 if (hisaddr == 0L) {
2921 /* XXX this message should go away */
2923 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2929 * I don't have an assigned address, so i need to
2930 * negotiate my address.
2932 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2933 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2935 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2936 if (sp->confflags & CONF_ENABLE_VJ) {
2937 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2938 sp->ipcp.max_state = MAX_STATES - 1;
2939 sp->ipcp.compress_cid = 1;
2941 sppp_open_event(&ipcp, sp);
2945 sppp_ipcp_close(struct sppp *sp)
2947 sppp_close_event(&ipcp, sp);
2948 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2950 * My address was dynamic, clear it again.
2952 sppp_set_ip_addr(sp, 0L);
2956 sppp_ipcp_TO(void *cookie)
2958 sppp_to_event(&ipcp, (struct sppp *)cookie);
2962 * Analyze a configure request. Return true if it was agreeable, and
2963 * caused action sca, false if it has been rejected or nak'ed, and
2964 * caused action scn. (The return value is used to make the state
2965 * transition decision in the state automaton.)
2968 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2970 u_char *buf, *r, *p;
2971 struct ifnet *ifp = SP2IFP(sp);
2972 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2973 u_long hisaddr, desiredaddr;
2980 * Make sure to allocate a buf that can at least hold a
2981 * conf-nak with an `address' option. We might need it below.
2983 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2987 /* pass 1: see if we can recognize them */
2989 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2992 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2993 len-=p[1], p+=p[1]) {
2995 log(-1, " %s ", sppp_ipcp_opt_name(*p));
2997 case IPCP_OPT_COMPRESSION:
2998 if (!(sp->confflags & CONF_ENABLE_VJ)) {
2999 /* VJ compression administratively disabled */
3001 log(-1, "[locally disabled] ");
3005 * In theory, we should only conf-rej an
3006 * option that is shorter than RFC 1618
3007 * requires (i.e. < 4), and should conf-nak
3008 * anything else that is not VJ. However,
3009 * since our algorithm always uses the
3010 * original option to NAK it with new values,
3011 * things would become more complicated. In
3012 * pratice, the only commonly implemented IP
3013 * compression option is VJ anyway, so the
3014 * difference is negligible.
3016 if (len >= 6 && p[1] == 6) {
3018 * correctly formed compression option
3019 * that could be VJ compression
3025 "optlen %d [invalid/unsupported] ",
3028 case IPCP_OPT_ADDRESS:
3029 if (len >= 6 && p[1] == 6) {
3030 /* correctly formed address option */
3034 log(-1, "[invalid] ");
3037 /* Others not supported. */
3042 /* Add the option to rejected list. */
3049 log(-1, " send conf-rej\n");
3050 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
3055 /* pass 2: parse option values */
3056 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
3058 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
3062 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3063 len-=p[1], p+=p[1]) {
3065 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3067 case IPCP_OPT_COMPRESSION:
3068 desiredcomp = p[2] << 8 | p[3];
3069 /* We only support VJ */
3070 if (desiredcomp == IPCP_COMP_VJ) {
3072 log(-1, "VJ [ack] ");
3073 sp->ipcp.flags |= IPCP_VJ;
3074 sl_compress_init(sp->pp_comp, p[4]);
3075 sp->ipcp.max_state = p[4];
3076 sp->ipcp.compress_cid = p[5];
3081 "compproto %#04x [not supported] ",
3083 p[2] = IPCP_COMP_VJ >> 8;
3084 p[3] = IPCP_COMP_VJ;
3085 p[4] = sp->ipcp.max_state;
3086 p[5] = sp->ipcp.compress_cid;
3088 case IPCP_OPT_ADDRESS:
3089 /* This is the address he wants in his end */
3090 desiredaddr = p[2] << 24 | p[3] << 16 |
3092 if (desiredaddr == hisaddr ||
3093 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3095 * Peer's address is same as our value,
3096 * or we have set it to 0.0.0.* to
3097 * indicate that we do not really care,
3098 * this is agreeable. Gonna conf-ack
3102 log(-1, "%s [ack] ",
3103 sppp_dotted_quad(hisaddr));
3104 /* record that we've seen it already */
3105 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3109 * The address wasn't agreeable. This is either
3110 * he sent us 0.0.0.0, asking to assign him an
3111 * address, or he send us another address not
3112 * matching our value. Either case, we gonna
3113 * conf-nak it with our value.
3114 * XXX: we should "rej" if hisaddr == 0
3117 if (desiredaddr == 0)
3118 log(-1, "[addr requested] ");
3120 log(-1, "%s [not agreed] ",
3121 sppp_dotted_quad(desiredaddr));
3124 p[2] = hisaddr >> 24;
3125 p[3] = hisaddr >> 16;
3126 p[4] = hisaddr >> 8;
3130 /* Add the option to nak'ed list. */
3137 * If we are about to conf-ack the request, but haven't seen
3138 * his address so far, gonna conf-nak it instead, with the
3139 * `address' option present and our idea of his address being
3140 * filled in there, to request negotiation of both addresses.
3142 * XXX This can result in an endless req - nak loop if peer
3143 * doesn't want to send us his address. Q: What should we do
3144 * about it? XXX A: implement the max-failure counter.
3146 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3147 buf[0] = IPCP_OPT_ADDRESS;
3149 buf[2] = hisaddr >> 24;
3150 buf[3] = hisaddr >> 16;
3151 buf[4] = hisaddr >> 8;
3155 log(-1, "still need hisaddr ");
3160 log(-1, " send conf-nak\n");
3161 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3164 log(-1, " send conf-ack\n");
3165 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3166 h->ident, origlen, h+1);
3174 * Analyze the IPCP Configure-Reject option list, and adjust our
3178 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3181 struct ifnet *ifp = SP2IFP(sp);
3182 int debug = ifp->if_flags & IFF_DEBUG;
3185 buf = malloc (len, M_TEMP, M_NOWAIT);
3190 log(LOG_DEBUG, SPP_FMT "ipcp rej 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 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3202 case IPCP_OPT_ADDRESS:
3204 * Peer doesn't grok address option. This is
3205 * bad. XXX Should we better give up here?
3206 * XXX We could try old "addresses" option...
3208 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3219 * Analyze the IPCP Configure-NAK option list, and adjust our
3223 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3226 struct ifnet *ifp = SP2IFP(sp);
3227 int debug = ifp->if_flags & IFF_DEBUG;
3232 buf = malloc (len, M_TEMP, M_NOWAIT);
3237 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3241 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3242 len -= p[1], p += p[1]) {
3244 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3246 case IPCP_OPT_COMPRESSION:
3247 if (len >= 6 && p[1] == 6) {
3248 desiredcomp = p[2] << 8 | p[3];
3250 log(-1, "[wantcomp %#04x] ",
3252 if (desiredcomp == IPCP_COMP_VJ) {
3253 sl_compress_init(sp->pp_comp, p[4]);
3254 sp->ipcp.max_state = p[4];
3255 sp->ipcp.compress_cid = p[5];
3257 log(-1, "[agree] ");
3260 ~(1 << IPCP_OPT_COMPRESSION);
3263 case IPCP_OPT_ADDRESS:
3265 * Peer doesn't like our local IP address. See
3266 * if we can do something for him. We'll drop
3267 * him our address then.
3269 if (len >= 6 && p[1] == 6) {
3270 wantaddr = p[2] << 24 | p[3] << 16 |
3272 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3274 log(-1, "[wantaddr %s] ",
3275 sppp_dotted_quad(wantaddr));
3277 * When doing dynamic address assignment,
3278 * we accept his offer. Otherwise, we
3279 * ignore it and thus continue to negotiate
3280 * our already existing value.
3281 * XXX: Bogus, if he said no once, he'll
3282 * just say no again, might as well die.
3284 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3285 sppp_set_ip_addr(sp, wantaddr);
3287 log(-1, "[agree] ");
3288 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3301 sppp_ipcp_tlu(struct sppp *sp)
3303 /* we are up - notify isdn daemon */
3309 sppp_ipcp_tld(struct sppp *sp)
3314 sppp_ipcp_tls(struct sppp *sp)
3316 /* indicate to LCP that it must stay alive */
3317 sp->lcp.protos |= (1 << IDX_IPCP);
3321 sppp_ipcp_tlf(struct sppp *sp)
3323 /* we no longer need LCP */
3324 sp->lcp.protos &= ~(1 << IDX_IPCP);
3325 sppp_lcp_check_and_close(sp);
3329 sppp_ipcp_scr(struct sppp *sp)
3331 char opt[6 /* compression */ + 6 /* address */];
3335 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3336 opt[i++] = IPCP_OPT_COMPRESSION;
3338 opt[i++] = IPCP_COMP_VJ >> 8;
3339 opt[i++] = IPCP_COMP_VJ;
3340 opt[i++] = sp->ipcp.max_state;
3341 opt[i++] = sp->ipcp.compress_cid;
3343 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3344 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3345 opt[i++] = IPCP_OPT_ADDRESS;
3347 opt[i++] = ouraddr >> 24;
3348 opt[i++] = ouraddr >> 16;
3349 opt[i++] = ouraddr >> 8;
3353 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3354 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3358 sppp_ipcp_init(struct sppp *sp)
3363 sppp_ipcp_up(struct sppp *sp)
3368 sppp_ipcp_down(struct sppp *sp)
3373 sppp_ipcp_open(struct sppp *sp)
3378 sppp_ipcp_close(struct sppp *sp)
3383 sppp_ipcp_TO(void *cookie)
3388 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3394 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3399 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3404 sppp_ipcp_tlu(struct sppp *sp)
3409 sppp_ipcp_tld(struct sppp *sp)
3414 sppp_ipcp_tls(struct sppp *sp)
3419 sppp_ipcp_tlf(struct sppp *sp)
3424 sppp_ipcp_scr(struct sppp *sp)
3430 *--------------------------------------------------------------------------*
3432 * The IPv6CP implementation. *
3434 *--------------------------------------------------------------------------*
3439 sppp_ipv6cp_init(struct sppp *sp)
3441 sp->ipv6cp.opts = 0;
3442 sp->ipv6cp.flags = 0;
3443 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3444 sp->fail_counter[IDX_IPV6CP] = 0;
3445 sp->pp_seq[IDX_IPV6CP] = 0;
3446 sp->pp_rseq[IDX_IPV6CP] = 0;
3447 callout_init(&sp->ch[IDX_IPV6CP], CALLOUT_MPSAFE);
3451 sppp_ipv6cp_up(struct sppp *sp)
3453 sppp_up_event(&ipv6cp, sp);
3457 sppp_ipv6cp_down(struct sppp *sp)
3459 sppp_down_event(&ipv6cp, sp);
3463 sppp_ipv6cp_open(struct sppp *sp)
3466 struct in6_addr myaddr, hisaddr;
3468 #ifdef IPV6CP_MYIFID_DYN
3469 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3471 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3474 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3476 * If we don't have our address, this probably means our
3477 * interface doesn't want to talk IPv6 at all. (This could
3478 * be the case if somebody wants to speak only IPX, for
3479 * example.) Don't open IPv6CP in this case.
3481 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3482 /* XXX this message should go away */
3484 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3489 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3490 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3491 sppp_open_event(&ipv6cp, sp);
3495 sppp_ipv6cp_close(struct sppp *sp)
3497 sppp_close_event(&ipv6cp, sp);
3501 sppp_ipv6cp_TO(void *cookie)
3503 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3507 * Analyze a configure request. Return true if it was agreeable, and
3508 * caused action sca, false if it has been rejected or nak'ed, and
3509 * caused action scn. (The return value is used to make the state
3510 * transition decision in the state automaton.)
3513 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3515 u_char *buf, *r, *p;
3516 struct ifnet *ifp = SP2IFP(sp);
3517 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3518 struct in6_addr myaddr, desiredaddr, suggestaddr;
3521 int collision, nohisaddr;
3522 char ip6buf[INET6_ADDRSTRLEN];
3527 * Make sure to allocate a buf that can at least hold a
3528 * conf-nak with an `address' option. We might need it below.
3530 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3534 /* pass 1: see if we can recognize them */
3536 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3540 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3541 len-=p[1], p+=p[1]) {
3543 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3545 case IPV6CP_OPT_IFID:
3546 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3547 /* correctly formed address option */
3552 log(-1, " [invalid]");
3555 case IPV6CP_OPT_COMPRESSION:
3556 if (len >= 4 && p[1] >= 4) {
3557 /* correctly formed compress option */
3561 log(-1, " [invalid]");
3565 /* Others not supported. */
3570 /* Add the option to rejected list. */
3577 log(-1, " send conf-rej\n");
3578 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3583 /* pass 2: parse option values */
3584 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3586 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3591 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3592 len-=p[1], p+=p[1]) {
3594 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3597 case IPV6CP_OPT_COMPRESSION:
3600 case IPV6CP_OPT_IFID:
3601 bzero(&desiredaddr, sizeof(desiredaddr));
3602 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3603 collision = (bcmp(&desiredaddr.s6_addr[8],
3604 &myaddr.s6_addr[8], 8) == 0);
3605 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3607 desiredaddr.s6_addr16[0] = htons(0xfe80);
3608 (void)in6_setscope(&desiredaddr, SP2IFP(sp), NULL);
3610 if (!collision && !nohisaddr) {
3611 /* no collision, hisaddr known - Conf-Ack */
3616 ip6_sprintf(ip6buf, &desiredaddr),
3617 sppp_cp_type_name(type));
3622 bzero(&suggestaddr, sizeof(&suggestaddr));
3623 if (collision && nohisaddr) {
3624 /* collision, hisaddr unknown - Conf-Rej */
3629 * - no collision, hisaddr unknown, or
3630 * - collision, hisaddr known
3631 * Conf-Nak, suggest hisaddr
3634 sppp_suggest_ip6_addr(sp, &suggestaddr);
3635 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3639 ip6_sprintf(ip6buf, &desiredaddr),
3640 sppp_cp_type_name(type));
3643 /* Add the option to nak'ed list. */
3649 if (rlen == 0 && type == CONF_ACK) {
3651 log(-1, " send %s\n", sppp_cp_type_name(type));
3652 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3655 if (type == CONF_ACK)
3656 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3660 log(-1, " send %s suggest %s\n",
3661 sppp_cp_type_name(type),
3662 ip6_sprintf(ip6buf, &suggestaddr));
3664 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3673 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3677 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3680 struct ifnet *ifp = SP2IFP(sp);
3681 int debug = ifp->if_flags & IFF_DEBUG;
3684 buf = malloc (len, M_TEMP, M_NOWAIT);
3689 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3693 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3694 len -= p[1], p += p[1]) {
3696 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3698 case IPV6CP_OPT_IFID:
3700 * Peer doesn't grok address option. This is
3701 * bad. XXX Should we better give up here?
3703 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3706 case IPV6CP_OPT_COMPRESS:
3707 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3719 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3723 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3726 struct ifnet *ifp = SP2IFP(sp);
3727 int debug = ifp->if_flags & IFF_DEBUG;
3728 struct in6_addr suggestaddr;
3729 char ip6buf[INET6_ADDRSTRLEN];
3732 buf = malloc (len, M_TEMP, M_NOWAIT);
3737 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3741 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3742 len -= p[1], p += p[1]) {
3744 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3746 case IPV6CP_OPT_IFID:
3748 * Peer doesn't like our local ifid. See
3749 * if we can do something for him. We'll drop
3750 * him our address then.
3752 if (len < 10 || p[1] != 10)
3754 bzero(&suggestaddr, sizeof(suggestaddr));
3755 suggestaddr.s6_addr16[0] = htons(0xfe80);
3756 (void)in6_setscope(&suggestaddr, SP2IFP(sp), NULL);
3757 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3759 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3761 log(-1, " [suggestaddr %s]",
3762 ip6_sprintf(ip6buf, &suggestaddr));
3763 #ifdef IPV6CP_MYIFID_DYN
3765 * When doing dynamic address assignment,
3766 * we accept his offer.
3768 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3769 struct in6_addr lastsuggest;
3771 * If <suggested myaddr from peer> equals to
3772 * <hisaddr we have suggested last time>,
3773 * we have a collision. generate new random
3776 sppp_suggest_ip6_addr(&lastsuggest);
3777 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3780 log(-1, " [random]");
3781 sppp_gen_ip6_addr(sp, &suggestaddr);
3783 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3785 log(-1, " [agree]");
3786 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3790 * Since we do not do dynamic address assignment,
3791 * we ignore it and thus continue to negotiate
3792 * our already existing value. This can possibly
3793 * go into infinite request-reject loop.
3795 * This is not likely because we normally use
3796 * ifid based on MAC-address.
3797 * If you have no ethernet card on the node, too bad.
3798 * XXX should we use fail_counter?
3803 case IPV6CP_OPT_COMPRESS:
3805 * Peer wants different compression parameters.
3817 sppp_ipv6cp_tlu(struct sppp *sp)
3819 /* we are up - notify isdn daemon */
3825 sppp_ipv6cp_tld(struct sppp *sp)
3830 sppp_ipv6cp_tls(struct sppp *sp)
3832 /* indicate to LCP that it must stay alive */
3833 sp->lcp.protos |= (1 << IDX_IPV6CP);
3837 sppp_ipv6cp_tlf(struct sppp *sp)
3840 #if 0 /* need #if 0 to close IPv6CP properly */
3841 /* we no longer need LCP */
3842 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3843 sppp_lcp_check_and_close(sp);
3848 sppp_ipv6cp_scr(struct sppp *sp)
3850 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3851 struct in6_addr ouraddr;
3854 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3855 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3856 opt[i++] = IPV6CP_OPT_IFID;
3858 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3863 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3864 opt[i++] = IPV6CP_OPT_COMPRESSION;
3866 opt[i++] = 0; /* TBD */
3867 opt[i++] = 0; /* TBD */
3868 /* variable length data may follow */
3872 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3873 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3876 static void sppp_ipv6cp_init(struct sppp *sp)
3880 static void sppp_ipv6cp_up(struct sppp *sp)
3884 static void sppp_ipv6cp_down(struct sppp *sp)
3889 static void sppp_ipv6cp_open(struct sppp *sp)
3893 static void sppp_ipv6cp_close(struct sppp *sp)
3897 static void sppp_ipv6cp_TO(void *sp)
3901 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3906 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3910 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3914 static void sppp_ipv6cp_tlu(struct sppp *sp)
3918 static void sppp_ipv6cp_tld(struct sppp *sp)
3922 static void sppp_ipv6cp_tls(struct sppp *sp)
3926 static void sppp_ipv6cp_tlf(struct sppp *sp)
3930 static void sppp_ipv6cp_scr(struct sppp *sp)
3936 *--------------------------------------------------------------------------*
3938 * The CHAP implementation. *
3940 *--------------------------------------------------------------------------*
3944 * The authentication protocols don't employ a full-fledged state machine as
3945 * the control protocols do, since they do have Open and Close events, but
3946 * not Up and Down, nor are they explicitly terminated. Also, use of the
3947 * authentication protocols may be different in both directions (this makes
3948 * sense, think of a machine that never accepts incoming calls but only
3949 * calls out, it doesn't require the called party to authenticate itself).
3951 * Our state machine for the local authentication protocol (we are requesting
3952 * the peer to authenticate) looks like:
3955 * +--------------------------------------------+
3957 * +--------+ Close +---------+ RCA+
3958 * | |<----------------------------------| |------+
3959 * +--->| Closed | TO* | Opened | sca |
3960 * | | |-----+ +-------| |<-----+
3961 * | +--------+ irc | | +---------+
3967 * | | +------->+ | |
3969 * | +--------+ V | |
3970 * | | |<----+<--------------------+ |
3976 * +------+ +------------------------------------------+
3977 * scn,tld sca,irc,ict,tlu
3982 * Open: LCP reached authentication phase
3983 * Close: LCP reached terminate phase
3985 * RCA+: received reply (pap-req, chap-response), acceptable
3986 * RCN: received reply (pap-req, chap-response), not acceptable
3987 * TO+: timeout with restart counter >= 0
3988 * TO-: timeout with restart counter < 0
3989 * TO*: reschedule timeout for CHAP
3991 * scr: send request packet (none for PAP, chap-challenge)
3992 * sca: send ack packet (pap-ack, chap-success)
3993 * scn: send nak packet (pap-nak, chap-failure)
3994 * ict: initialize re-challenge timer (CHAP only)
3996 * tlu: this-layer-up, LCP reaches network phase
3997 * tld: this-layer-down, LCP enters terminate phase
3999 * Note that in CHAP mode, after sending a new challenge, while the state
4000 * automaton falls back into Req-Sent state, it doesn't signal a tld
4001 * event to LCP, so LCP remains in network phase. Only after not getting
4002 * any response (or after getting an unacceptable response), CHAP closes,
4003 * causing LCP to enter terminate phase.
4005 * With PAP, there is no initial request that can be sent. The peer is
4006 * expected to send one based on the successful negotiation of PAP as
4007 * the authentication protocol during the LCP option negotiation.
4009 * Incoming authentication protocol requests (remote requests
4010 * authentication, we are peer) don't employ a state machine at all,
4011 * they are simply answered. Some peers [Ascend P50 firmware rev
4012 * 4.50] react allergically when sending IPCP requests while they are
4013 * still in authentication phase (thereby violating the standard that
4014 * demands that these NCP packets are to be discarded), so we keep
4015 * track of the peer demanding us to authenticate, and only proceed to
4016 * phase network once we've seen a positive acknowledge for the
4021 * Handle incoming CHAP packets.
4024 sppp_chap_input(struct sppp *sp, struct mbuf *m)
4027 struct lcp_header *h;
4029 u_char *value, *name, digest[AUTHKEYLEN], dsize;
4030 int value_len, name_len;
4033 len = m->m_pkthdr.len;
4037 SPP_FMT "chap invalid packet length: %d bytes\n",
4038 SPP_ARGS(ifp), len);
4041 h = mtod (m, struct lcp_header*);
4042 if (len > ntohs (h->len))
4043 len = ntohs (h->len);
4046 /* challenge, failure and success are his authproto */
4047 case CHAP_CHALLENGE:
4048 value = 1 + (u_char*)(h+1);
4049 value_len = value[-1];
4050 name = value + value_len;
4051 name_len = len - value_len - 5;
4055 SPP_FMT "chap corrupted challenge "
4056 "<%s id=0x%x len=%d",
4058 sppp_auth_type_name(PPP_CHAP, h->type),
4059 h->ident, ntohs(h->len));
4060 sppp_print_bytes((u_char*) (h+1), len-4);
4068 SPP_FMT "chap input <%s id=0x%x len=%d name=",
4070 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
4072 sppp_print_string((char*) name, name_len);
4073 log(-1, " value-size=%d value=", value_len);
4074 sppp_print_bytes(value, value_len);
4078 /* Compute reply value. */
4080 MD5Update(&ctx, &h->ident, 1);
4081 MD5Update(&ctx, sp->myauth.secret,
4082 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
4083 MD5Update(&ctx, value, value_len);
4084 MD5Final(digest, &ctx);
4085 dsize = sizeof digest;
4087 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
4088 sizeof dsize, (const char *)&dsize,
4089 sizeof digest, digest,
4090 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4097 log(LOG_DEBUG, SPP_FMT "chap success",
4101 sppp_print_string((char*)(h + 1), len - 4);
4107 sp->pp_flags &= ~PP_NEEDAUTH;
4108 if (sp->myauth.proto == PPP_CHAP &&
4109 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4110 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
4112 * We are authenticator for CHAP but didn't
4113 * complete yet. Leave it to tlu to proceed
4122 sppp_phase_network(sp);
4127 log(LOG_INFO, SPP_FMT "chap failure",
4131 sppp_print_string((char*)(h + 1), len - 4);
4135 log(LOG_INFO, SPP_FMT "chap failure\n",
4137 /* await LCP shutdown by authenticator */
4140 /* response is my authproto */
4142 value = 1 + (u_char*)(h+1);
4143 value_len = value[-1];
4144 name = value + value_len;
4145 name_len = len - value_len - 5;
4149 SPP_FMT "chap corrupted response "
4150 "<%s id=0x%x len=%d",
4152 sppp_auth_type_name(PPP_CHAP, h->type),
4153 h->ident, ntohs(h->len));
4154 sppp_print_bytes((u_char*)(h+1), len-4);
4159 if (h->ident != sp->confid[IDX_CHAP]) {
4162 SPP_FMT "chap dropping response for old ID "
4163 "(got %d, expected %d)\n",
4165 h->ident, sp->confid[IDX_CHAP]);
4168 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4169 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4170 log(LOG_INFO, SPP_FMT "chap response, his name ",
4172 sppp_print_string(name, name_len);
4173 log(-1, " != expected ");
4174 sppp_print_string(sp->hisauth.name,
4175 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4179 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4180 "<%s id=0x%x len=%d name=",
4182 sppp_state_name(sp->state[IDX_CHAP]),
4183 sppp_auth_type_name(PPP_CHAP, h->type),
4184 h->ident, ntohs (h->len));
4185 sppp_print_string((char*)name, name_len);
4186 log(-1, " value-size=%d value=", value_len);
4187 sppp_print_bytes(value, value_len);
4190 if (value_len != AUTHKEYLEN) {
4193 SPP_FMT "chap bad hash value length: "
4194 "%d bytes, should be %d\n",
4195 SPP_ARGS(ifp), value_len,
4201 MD5Update(&ctx, &h->ident, 1);
4202 MD5Update(&ctx, sp->hisauth.secret,
4203 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4204 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4205 MD5Final(digest, &ctx);
4207 #define FAILMSG "Failed..."
4208 #define SUCCMSG "Welcome!"
4210 if (value_len != sizeof digest ||
4211 bcmp(digest, value, value_len) != 0) {
4212 /* action scn, tld */
4213 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4214 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4219 /* action sca, perhaps tlu */
4220 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4221 sp->state[IDX_CHAP] == STATE_OPENED)
4222 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4223 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4225 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4226 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4232 /* Unknown CHAP packet type -- ignore. */
4234 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4235 "<0x%x id=0x%xh len=%d",
4237 sppp_state_name(sp->state[IDX_CHAP]),
4238 h->type, h->ident, ntohs(h->len));
4239 sppp_print_bytes((u_char*)(h+1), len-4);
4248 sppp_chap_init(struct sppp *sp)
4250 /* Chap doesn't have STATE_INITIAL at all. */
4251 sp->state[IDX_CHAP] = STATE_CLOSED;
4252 sp->fail_counter[IDX_CHAP] = 0;
4253 sp->pp_seq[IDX_CHAP] = 0;
4254 sp->pp_rseq[IDX_CHAP] = 0;
4255 callout_init(&sp->ch[IDX_CHAP], CALLOUT_MPSAFE);
4259 sppp_chap_open(struct sppp *sp)
4261 if (sp->myauth.proto == PPP_CHAP &&
4262 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4263 /* we are authenticator for CHAP, start it */
4265 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4266 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4268 /* nothing to be done if we are peer, await a challenge */
4272 sppp_chap_close(struct sppp *sp)
4274 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4275 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4279 sppp_chap_TO(void *cookie)
4281 struct sppp *sp = (struct sppp *)cookie;
4288 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4290 sppp_state_name(sp->state[IDX_CHAP]),
4291 sp->rst_counter[IDX_CHAP]);
4293 if (--sp->rst_counter[IDX_CHAP] < 0)
4295 switch (sp->state[IDX_CHAP]) {
4296 case STATE_REQ_SENT:
4298 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4302 /* TO+ (or TO*) event */
4303 switch (sp->state[IDX_CHAP]) {
4306 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4308 case STATE_REQ_SENT:
4310 /* sppp_cp_change_state() will restart the timer */
4311 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4320 sppp_chap_tlu(struct sppp *sp)
4326 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4329 * Some broken CHAP implementations (Conware CoNet, firmware
4330 * 4.0.?) don't want to re-authenticate their CHAP once the
4331 * initial challenge-response exchange has taken place.
4332 * Provide for an option to avoid rechallenges.
4334 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4336 * Compute the re-challenge timeout. This will yield
4337 * a number between 300 and 810 seconds.
4339 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
4340 callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, (void *)sp);
4345 SPP_FMT "chap %s, ",
4347 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4348 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4349 log(-1, "next re-challenge in %d seconds\n", i);
4351 log(-1, "re-challenging supressed\n");
4356 /* indicate to LCP that we need to be closed down */
4357 sp->lcp.protos |= (1 << IDX_CHAP);
4359 if (sp->pp_flags & PP_NEEDAUTH) {
4361 * Remote is authenticator, but his auth proto didn't
4362 * complete yet. Defer the transition to network
4373 * If we are already in phase network, we are done here. This
4374 * is the case if this is a dummy tlu event after a re-challenge.
4376 if (sp->pp_phase != PHASE_NETWORK)
4377 sppp_phase_network(sp);
4381 sppp_chap_tld(struct sppp *sp)
4386 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4387 callout_stop(&sp->ch[IDX_CHAP]);
4388 sp->lcp.protos &= ~(1 << IDX_CHAP);
4394 sppp_chap_scr(struct sppp *sp)
4399 /* Compute random challenge. */
4400 ch = (u_long *)sp->myauth.challenge;
4401 read_random(&seed, sizeof seed);
4402 ch[0] = seed ^ random();
4403 ch[1] = seed ^ random();
4404 ch[2] = seed ^ random();
4405 ch[3] = seed ^ random();
4408 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4410 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4411 sizeof clen, (const char *)&clen,
4412 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4413 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4419 *--------------------------------------------------------------------------*
4421 * The PAP implementation. *
4423 *--------------------------------------------------------------------------*
4426 * For PAP, we need to keep a little state also if we are the peer, not the
4427 * authenticator. This is since we don't get a request to authenticate, but
4428 * have to repeatedly authenticate ourself until we got a response (or the
4429 * retry counter is expired).
4433 * Handle incoming PAP packets. */
4435 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4438 struct lcp_header *h;
4440 u_char *name, *passwd, mlen;
4441 int name_len, passwd_len;
4443 len = m->m_pkthdr.len;
4447 SPP_FMT "pap invalid packet length: %d bytes\n",
4448 SPP_ARGS(ifp), len);
4451 h = mtod (m, struct lcp_header*);
4452 if (len > ntohs (h->len))
4453 len = ntohs (h->len);
4455 /* PAP request is my authproto */
4457 name = 1 + (u_char*)(h+1);
4458 name_len = name[-1];
4459 passwd = name + name_len + 1;
4460 if (name_len > len - 6 ||
4461 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4463 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4464 "<%s id=0x%x len=%d",
4466 sppp_auth_type_name(PPP_PAP, h->type),
4467 h->ident, ntohs(h->len));
4468 sppp_print_bytes((u_char*)(h+1), len-4);
4474 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4475 "<%s id=0x%x len=%d name=",
4477 sppp_state_name(sp->state[IDX_PAP]),
4478 sppp_auth_type_name(PPP_PAP, h->type),
4479 h->ident, ntohs(h->len));
4480 sppp_print_string((char*)name, name_len);
4481 log(-1, " passwd=");
4482 sppp_print_string((char*)passwd, passwd_len);
4485 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4486 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4487 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4488 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4489 /* action scn, tld */
4490 mlen = sizeof(FAILMSG) - 1;
4491 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4492 sizeof mlen, (const char *)&mlen,
4493 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4498 /* action sca, perhaps tlu */
4499 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4500 sp->state[IDX_PAP] == STATE_OPENED) {
4501 mlen = sizeof(SUCCMSG) - 1;
4502 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4503 sizeof mlen, (const char *)&mlen,
4504 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4507 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4508 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4513 /* ack and nak are his authproto */
4515 callout_stop(&sp->pap_my_to_ch);
4517 log(LOG_DEBUG, SPP_FMT "pap success",
4519 name_len = *((char *)h);
4520 if (len > 5 && name_len) {
4522 sppp_print_string((char*)(h+1), name_len);
4528 sp->pp_flags &= ~PP_NEEDAUTH;
4529 if (sp->myauth.proto == PPP_PAP &&
4530 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4531 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4533 * We are authenticator for PAP but didn't
4534 * complete yet. Leave it to tlu to proceed
4543 sppp_phase_network(sp);
4547 callout_stop (&sp->pap_my_to_ch);
4549 log(LOG_INFO, SPP_FMT "pap failure",
4551 name_len = *((char *)h);
4552 if (len > 5 && name_len) {
4554 sppp_print_string((char*)(h+1), name_len);
4558 log(LOG_INFO, SPP_FMT "pap failure\n",
4560 /* await LCP shutdown by authenticator */
4564 /* Unknown PAP packet type -- ignore. */
4566 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4567 "<0x%x id=0x%x len=%d",
4569 h->type, h->ident, ntohs(h->len));
4570 sppp_print_bytes((u_char*)(h+1), len-4);
4579 sppp_pap_init(struct sppp *sp)
4581 /* PAP doesn't have STATE_INITIAL at all. */
4582 sp->state[IDX_PAP] = STATE_CLOSED;
4583 sp->fail_counter[IDX_PAP] = 0;
4584 sp->pp_seq[IDX_PAP] = 0;
4585 sp->pp_rseq[IDX_PAP] = 0;
4586 callout_init(&sp->ch[IDX_PAP], CALLOUT_MPSAFE);
4587 callout_init(&sp->pap_my_to_ch, CALLOUT_MPSAFE);
4591 sppp_pap_open(struct sppp *sp)
4593 if (sp->hisauth.proto == PPP_PAP &&
4594 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4595 /* we are authenticator for PAP, start our timer */
4596 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4597 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4599 if (sp->myauth.proto == PPP_PAP) {
4600 /* we are peer, send a request, and start a timer */
4602 callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
4603 sppp_pap_my_TO, (void *)sp);
4608 sppp_pap_close(struct sppp *sp)
4610 if (sp->state[IDX_PAP] != STATE_CLOSED)
4611 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4615 * That's the timeout routine if we are authenticator. Since the
4616 * authenticator is basically passive in PAP, we can't do much here.
4619 sppp_pap_TO(void *cookie)
4621 struct sppp *sp = (struct sppp *)cookie;
4628 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4630 sppp_state_name(sp->state[IDX_PAP]),
4631 sp->rst_counter[IDX_PAP]);
4633 if (--sp->rst_counter[IDX_PAP] < 0)
4635 switch (sp->state[IDX_PAP]) {
4636 case STATE_REQ_SENT:
4638 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4642 /* TO+ event, not very much we could do */
4643 switch (sp->state[IDX_PAP]) {
4644 case STATE_REQ_SENT:
4645 /* sppp_cp_change_state() will restart the timer */
4646 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4655 * That's the timeout handler if we are peer. Since the peer is active,
4656 * we need to retransmit our PAP request since it is apparently lost.
4657 * XXX We should impose a max counter.
4660 sppp_pap_my_TO(void *cookie)
4662 struct sppp *sp = (struct sppp *)cookie;
4666 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4675 sppp_pap_tlu(struct sppp *sp)
4680 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4683 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4684 SPP_ARGS(ifp), pap.name);
4688 /* indicate to LCP that we need to be closed down */
4689 sp->lcp.protos |= (1 << IDX_PAP);
4691 if (sp->pp_flags & PP_NEEDAUTH) {
4693 * Remote is authenticator, but his auth proto didn't
4694 * complete yet. Defer the transition to network
4703 sppp_phase_network(sp);
4707 sppp_pap_tld(struct sppp *sp)
4712 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4713 callout_stop (&sp->ch[IDX_PAP]);
4714 callout_stop (&sp->pap_my_to_ch);
4715 sp->lcp.protos &= ~(1 << IDX_PAP);
4721 sppp_pap_scr(struct sppp *sp)
4723 u_char idlen, pwdlen;
4725 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4726 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4727 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4729 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4730 sizeof idlen, (const char *)&idlen,
4731 (size_t)idlen, sp->myauth.name,
4732 sizeof pwdlen, (const char *)&pwdlen,
4733 (size_t)pwdlen, sp->myauth.secret,
4738 * Random miscellaneous functions.
4742 * Send a PAP or CHAP proto packet.
4744 * Varadic function, each of the elements for the ellipsis is of type
4745 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4747 * NOTE: never declare variadic functions with types subject to type
4748 * promotion (i.e. u_char). This is asking for big trouble depending
4749 * on the architecture you are on...
4753 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4754 unsigned int type, unsigned int id,
4758 struct ppp_header *h;
4759 struct lcp_header *lh;
4767 MGETHDR (m, M_DONTWAIT, MT_DATA);
4770 m->m_pkthdr.rcvif = 0;
4772 h = mtod (m, struct ppp_header*);
4773 h->address = PPP_ALLSTATIONS; /* broadcast address */
4774 h->control = PPP_UI; /* Unnumbered Info */
4775 h->protocol = htons(cp->proto);
4777 lh = (struct lcp_header*)(h + 1);
4780 p = (u_char*) (lh+1);
4785 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
4786 msg = va_arg(ap, const char *);
4788 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4794 bcopy(msg, p, mlen);
4799 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4800 lh->len = htons (LCP_HEADER_LEN + len);
4803 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4804 SPP_ARGS(ifp), cp->name,
4805 sppp_auth_type_name(cp->proto, lh->type),
4806 lh->ident, ntohs(lh->len));
4807 sppp_print_bytes((u_char*) (lh+1), len);
4810 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
4815 * Flush interface queue.
4818 sppp_qflush(struct ifqueue *ifq)
4833 * Send keepalive packets, every 10 seconds.
4836 sppp_keepalive(void *dummy)
4838 struct sppp *sp = (struct sppp*)dummy;
4839 struct ifnet *ifp = SP2IFP(sp);
4844 /* Keepalive mode disabled or channel down? */
4845 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4846 ! (ifp->if_drv_flags & IFF_DRV_RUNNING))
4849 if (sp->pp_mode == PP_FR) {
4850 sppp_fr_keepalive (sp);
4854 /* No keepalive in PPP mode if LCP not opened yet. */
4855 if (sp->pp_mode != IFF_CISCO &&
4856 sp->pp_phase < PHASE_AUTHENTICATE)
4859 if (sp->pp_alivecnt == MAXALIVECNT) {
4860 /* No keepalive packets got. Stop the interface. */
4861 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
4863 sppp_qflush (&sp->pp_cpq);
4864 if (sp->pp_mode != IFF_CISCO) {
4866 /* Shut down the PPP link. */
4868 /* Initiate negotiation. XXX */
4872 if (sp->pp_alivecnt <= MAXALIVECNT)
4874 if (sp->pp_mode == IFF_CISCO)
4875 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4876 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4877 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4878 long nmagic = htonl (sp->lcp.magic);
4879 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4880 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4881 sp->lcp.echoid, 4, &nmagic);
4886 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
4891 * Get both IP addresses.
4894 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4896 struct ifnet *ifp = SP2IFP(sp);
4898 struct sockaddr_in *si, *sm;
4904 * Pick the first AF_INET address from the list,
4905 * aliases don't make any sense on a p2p link anyway.
4909 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4910 if (ifa->ifa_addr->sa_family == AF_INET) {
4911 si = (struct sockaddr_in *)ifa->ifa_addr;
4912 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4917 if (si && si->sin_addr.s_addr) {
4918 ssrc = si->sin_addr.s_addr;
4920 *srcmask = ntohl(sm->sin_addr.s_addr);
4923 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4924 if (si && si->sin_addr.s_addr)
4925 ddst = si->sin_addr.s_addr;
4927 if_addr_runlock(ifp);
4929 if (dst) *dst = ntohl(ddst);
4930 if (src) *src = ntohl(ssrc);
4935 * Set my IP address. Must be called at splimp.
4938 sppp_set_ip_addr(struct sppp *sp, u_long src)
4942 struct sockaddr_in *si;
4943 struct in_ifaddr *ia;
4946 * Pick the first AF_INET address from the list,
4947 * aliases don't make any sense on a p2p link anyway.
4951 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4952 if (ifa->ifa_addr->sa_family == AF_INET) {
4953 si = (struct sockaddr_in *)ifa->ifa_addr;
4960 if_addr_runlock(ifp);
4965 /* delete old route */
4966 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4967 if (debug && error) {
4968 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4969 SPP_ARGS(ifp), error);
4972 /* set new address */
4973 si->sin_addr.s_addr = htonl(src);
4976 LIST_REMOVE(ia, ia_hash);
4977 LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash);
4978 IN_IFADDR_WUNLOCK();
4981 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
4982 if (debug && error) {
4983 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
4984 SPP_ARGS(ifp), error);
4993 * Get both IPv6 addresses.
4996 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4997 struct in6_addr *srcmask)
4999 struct ifnet *ifp = SP2IFP(sp);
5001 struct sockaddr_in6 *si, *sm;
5002 struct in6_addr ssrc, ddst;
5005 bzero(&ssrc, sizeof(ssrc));
5006 bzero(&ddst, sizeof(ddst));
5008 * Pick the first link-local AF_INET6 address from the list,
5009 * aliases don't make any sense on a p2p link anyway.
5013 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
5014 if (ifa->ifa_addr->sa_family == AF_INET6) {
5015 si = (struct sockaddr_in6 *)ifa->ifa_addr;
5016 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
5017 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
5021 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
5022 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
5024 bcopy(&sm->sin6_addr, srcmask,
5029 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
5030 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
5031 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
5035 bcopy(&ddst, dst, sizeof(*dst));
5037 bcopy(&ssrc, src, sizeof(*src));
5038 if_addr_runlock(ifp);
5041 #ifdef IPV6CP_MYIFID_DYN
5043 * Generate random ifid.
5046 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
5052 * Set my IPv6 address. Must be called at splimp.
5055 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
5059 struct sockaddr_in6 *sin6;
5062 * Pick the first link-local AF_INET6 address from the list,
5063 * aliases don't make any sense on a p2p link anyway.
5068 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
5069 if (ifa->ifa_addr->sa_family == AF_INET6) {
5070 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
5071 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5077 if_addr_runlock(ifp);
5081 struct sockaddr_in6 new_sin6 = *sin6;
5083 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
5084 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5085 if (debug && error) {
5086 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
5087 " failed, error=%d\n", SPP_ARGS(ifp), error);
5095 * Suggest a candidate address to be used by peer.
5098 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5100 struct in6_addr myaddr;
5103 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5105 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5107 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5108 myaddr.s6_addr[14] ^= 0xff;
5109 myaddr.s6_addr[15] ^= 0xff;
5111 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5112 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5115 bcopy(&myaddr, suggest, sizeof(myaddr));
5120 sppp_params(struct sppp *sp, u_long cmd, void *data)
5123 struct ifreq *ifr = (struct ifreq *)data;
5124 struct spppreq *spr;
5127 if ((spr = malloc(sizeof(struct spppreq), M_TEMP, M_NOWAIT)) == 0)
5130 * ifr->ifr_data is supposed to point to a struct spppreq.
5131 * Check the cmd word first before attempting to fetch all the
5134 if ((subcmd = fuword(ifr->ifr_data)) == -1) {
5139 if (copyin((caddr_t)ifr->ifr_data, spr, sizeof(struct spppreq)) != 0) {
5145 case (u_long)SPPPIOGDEFS:
5146 if (cmd != SIOCGIFGENERIC) {
5151 * We copy over the entire current state, but clean
5152 * out some of the stuff we don't wanna pass up.
5153 * Remember, SIOCGIFGENERIC is unprotected, and can be
5154 * called by any user. No need to ever get PAP or
5155 * CHAP secrets back to userland anyway.
5157 spr->defs.pp_phase = sp->pp_phase;
5158 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5159 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5160 spr->defs.lcp = sp->lcp;
5161 spr->defs.ipcp = sp->ipcp;
5162 spr->defs.ipv6cp = sp->ipv6cp;
5163 spr->defs.myauth = sp->myauth;
5164 spr->defs.hisauth = sp->hisauth;
5165 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5166 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5167 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5168 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5170 * Fixup the LCP timeout value to milliseconds so
5171 * spppcontrol doesn't need to bother about the value
5172 * of "hz". We do the reverse calculation below when
5175 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5176 rv = copyout(spr, (caddr_t)ifr->ifr_data,
5177 sizeof(struct spppreq));
5180 case (u_long)SPPPIOSDEFS:
5181 if (cmd != SIOCSIFGENERIC) {
5186 * We have a very specific idea of which fields we
5187 * allow being passed back from userland, so to not
5188 * clobber our current state. For one, we only allow
5189 * setting anything if LCP is in dead or establish
5190 * phase. Once the authentication negotiations
5191 * started, the authentication settings must not be
5192 * changed again. (The administrator can force an
5193 * ifconfig down in order to get LCP back into dead
5196 * Also, we only allow for authentication parameters to be
5199 * XXX Should allow to set or clear pp_flags.
5201 * Finally, if the respective authentication protocol to
5202 * be used is set differently than 0, but the secret is
5203 * passed as all zeros, we don't trash the existing secret.
5204 * This allows an administrator to change the system name
5205 * only without clobbering the secret (which he didn't get
5206 * back in a previous SPPPIOGDEFS call). However, the
5207 * secrets are cleared if the authentication protocol is
5209 if (sp->pp_phase != PHASE_DEAD &&
5210 sp->pp_phase != PHASE_ESTABLISH) {
5215 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5216 spr->defs.myauth.proto != PPP_CHAP) ||
5217 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5218 spr->defs.hisauth.proto != PPP_CHAP)) {
5223 if (spr->defs.myauth.proto == 0)
5224 /* resetting myauth */
5225 bzero(&sp->myauth, sizeof sp->myauth);
5227 /* setting/changing myauth */
5228 sp->myauth.proto = spr->defs.myauth.proto;
5229 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5230 if (spr->defs.myauth.secret[0] != '\0')
5231 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5234 if (spr->defs.hisauth.proto == 0)
5235 /* resetting hisauth */
5236 bzero(&sp->hisauth, sizeof sp->hisauth);
5238 /* setting/changing hisauth */
5239 sp->hisauth.proto = spr->defs.hisauth.proto;
5240 sp->hisauth.flags = spr->defs.hisauth.flags;
5241 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5242 if (spr->defs.hisauth.secret[0] != '\0')
5243 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5246 /* set LCP restart timer timeout */
5247 if (spr->defs.lcp.timeout != 0)
5248 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5249 /* set VJ enable and IPv6 disable flags */
5251 if (spr->defs.enable_vj)
5252 sp->confflags |= CONF_ENABLE_VJ;
5254 sp->confflags &= ~CONF_ENABLE_VJ;
5257 if (spr->defs.enable_ipv6)
5258 sp->confflags |= CONF_ENABLE_IPV6;
5260 sp->confflags &= ~CONF_ENABLE_IPV6;
5275 sppp_phase_network(struct sppp *sp)
5281 sp->pp_phase = PHASE_NETWORK;
5284 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5285 sppp_phase_name(sp->pp_phase));
5287 /* Notify NCPs now. */
5288 for (i = 0; i < IDX_COUNT; i++)
5289 if ((cps[i])->flags & CP_NCP)
5292 /* Send Up events to all NCPs. */
5293 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5294 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5297 /* if no NCP is starting, all this was in vain, close down */
5298 sppp_lcp_check_and_close(sp);
5303 sppp_cp_type_name(u_char type)
5305 static char buf[12];
5307 case CONF_REQ: return "conf-req";
5308 case CONF_ACK: return "conf-ack";
5309 case CONF_NAK: return "conf-nak";
5310 case CONF_REJ: return "conf-rej";
5311 case TERM_REQ: return "term-req";
5312 case TERM_ACK: return "term-ack";
5313 case CODE_REJ: return "code-rej";
5314 case PROTO_REJ: return "proto-rej";
5315 case ECHO_REQ: return "echo-req";
5316 case ECHO_REPLY: return "echo-reply";
5317 case DISC_REQ: return "discard-req";
5319 snprintf (buf, sizeof(buf), "cp/0x%x", type);
5324 sppp_auth_type_name(u_short proto, u_char type)
5326 static char buf[12];
5330 case CHAP_CHALLENGE: return "challenge";
5331 case CHAP_RESPONSE: return "response";
5332 case CHAP_SUCCESS: return "success";
5333 case CHAP_FAILURE: return "failure";
5337 case PAP_REQ: return "req";
5338 case PAP_ACK: return "ack";
5339 case PAP_NAK: return "nak";
5342 snprintf (buf, sizeof(buf), "auth/0x%x", type);
5347 sppp_lcp_opt_name(u_char opt)
5349 static char buf[12];
5351 case LCP_OPT_MRU: return "mru";
5352 case LCP_OPT_ASYNC_MAP: return "async-map";
5353 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5354 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5355 case LCP_OPT_MAGIC: return "magic";
5356 case LCP_OPT_PROTO_COMP: return "proto-comp";
5357 case LCP_OPT_ADDR_COMP: return "addr-comp";
5359 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5365 sppp_ipcp_opt_name(u_char opt)
5367 static char buf[12];
5369 case IPCP_OPT_ADDRESSES: return "addresses";
5370 case IPCP_OPT_COMPRESSION: return "compression";
5371 case IPCP_OPT_ADDRESS: return "address";
5373 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5380 sppp_ipv6cp_opt_name(u_char opt)
5382 static char buf[12];
5384 case IPV6CP_OPT_IFID: return "ifid";
5385 case IPV6CP_OPT_COMPRESSION: return "compression";
5387 sprintf (buf, "0x%x", opt);
5393 sppp_state_name(int state)
5396 case STATE_INITIAL: return "initial";
5397 case STATE_STARTING: return "starting";
5398 case STATE_CLOSED: return "closed";
5399 case STATE_STOPPED: return "stopped";
5400 case STATE_CLOSING: return "closing";
5401 case STATE_STOPPING: return "stopping";
5402 case STATE_REQ_SENT: return "req-sent";
5403 case STATE_ACK_RCVD: return "ack-rcvd";
5404 case STATE_ACK_SENT: return "ack-sent";
5405 case STATE_OPENED: return "opened";
5411 sppp_phase_name(enum ppp_phase phase)
5414 case PHASE_DEAD: return "dead";
5415 case PHASE_ESTABLISH: return "establish";
5416 case PHASE_TERMINATE: return "terminate";
5417 case PHASE_AUTHENTICATE: return "authenticate";
5418 case PHASE_NETWORK: return "network";
5424 sppp_proto_name(u_short proto)
5426 static char buf[12];
5428 case PPP_LCP: return "lcp";
5429 case PPP_IPCP: return "ipcp";
5430 case PPP_PAP: return "pap";
5431 case PPP_CHAP: return "chap";
5432 case PPP_IPV6CP: return "ipv6cp";
5434 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5439 sppp_print_bytes(const u_char *p, u_short len)
5442 log(-1, " %*D", len, p, "-");
5446 sppp_print_string(const char *p, u_short len)
5453 * Print only ASCII chars directly. RFC 1994 recommends
5454 * using only them, but we don't rely on it. */
5455 if (c < ' ' || c > '~')
5456 log(-1, "\\x%x", c);
5464 sppp_dotted_quad(u_long addr)
5467 sprintf(s, "%d.%d.%d.%d",
5468 (int)((addr >> 24) & 0xff),
5469 (int)((addr >> 16) & 0xff),
5470 (int)((addr >> 8) & 0xff),
5471 (int)(addr & 0xff));
5477 sppp_strnlen(u_char *p, int max)
5481 for (len = 0; len < max && *p; ++p)
5486 /* a dummy, used to drop uninteresting events */
5488 sppp_null(struct sppp *unused)
5490 /* do just nothing */