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_NOWAIT);
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;
796 int ipproto = PPP_IP;
798 int debug = ifp->if_flags & IFF_DEBUG;
802 if (!(ifp->if_flags & IFF_UP) ||
803 (!(ifp->if_flags & IFF_AUTO) &&
804 !(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
813 if ((ifp->if_flags & IFF_AUTO) &&
814 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
819 * Hack to prevent the initialization-time generated
820 * IPv6 multicast packet to erroneously cause a
821 * dialout event in case IPv6 has been
822 * administratively disabled on that interface.
824 if (dst->sa_family == AF_INET6 &&
825 !(sp->confflags & CONF_ENABLE_IPV6))
829 * Interface is not yet running, but auto-dial. Need
830 * to start LCP for it.
832 ifp->if_drv_flags |= IFF_DRV_RUNNING;
837 if (dst->sa_family == AF_INET) {
838 /* XXX Check mbuf length here? */
839 struct ip *ip = mtod (m, struct ip*);
840 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
843 * When using dynamic local IP address assignment by using
844 * 0.0.0.0 as a local address, the first TCP session will
845 * not connect because the local TCP checksum is computed
846 * using 0.0.0.0 which will later become our real IP address
847 * so the TCP checksum computed at the remote end will
848 * become invalid. So we
849 * - don't let packets with src ip addr 0 thru
850 * - we flag TCP packets with src ip 0 as an error
853 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
857 if(ip->ip_p == IPPROTO_TCP)
858 return(EADDRNOTAVAIL);
864 * Put low delay, telnet, rlogin and ftp control packets
865 * in front of the queue or let ALTQ take care.
867 if (ALTQ_IS_ENABLED(&ifp->if_snd))
869 else if (_IF_QFULL(&sp->pp_fastq))
871 else if (ip->ip_tos & IPTOS_LOWDELAY)
873 else if (m->m_len < sizeof *ip + sizeof *tcp)
875 else if (ip->ip_p != IPPROTO_TCP)
877 else if (INTERACTIVE (ntohs (tcp->th_sport)))
879 else if (INTERACTIVE (ntohs (tcp->th_dport)))
883 * Do IP Header compression
885 if (sp->pp_mode != IFF_CISCO && sp->pp_mode != PP_FR &&
886 (sp->ipcp.flags & IPCP_VJ) && ip->ip_p == IPPROTO_TCP)
887 switch (sl_compress_tcp(m, ip, sp->pp_comp,
888 sp->ipcp.compress_cid)) {
889 case TYPE_COMPRESSED_TCP:
890 ipproto = PPP_VJ_COMP;
892 case TYPE_UNCOMPRESSED_TCP:
893 ipproto = PPP_VJ_UCOMP;
907 if (dst->sa_family == AF_INET6) {
908 /* XXX do something tricky here? */
912 if (sp->pp_mode == PP_FR) {
913 /* Add frame relay header. */
914 m = sppp_fr_header (sp, m, dst->sa_family);
921 * Prepend general data packet PPP header. For now, IP only.
923 M_PREPEND (m, PPP_HEADER_LEN, M_NOWAIT);
926 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
933 * May want to check size of packet
934 * (albeit due to the implementation it's always enough)
936 h = mtod (m, struct ppp_header*);
937 if (sp->pp_mode == IFF_CISCO) {
938 h->address = CISCO_UNICAST; /* unicast address */
941 h->address = PPP_ALLSTATIONS; /* broadcast address */
942 h->control = PPP_UI; /* Unnumbered Info */
945 switch (dst->sa_family) {
947 case AF_INET: /* Internet Protocol */
948 if (sp->pp_mode == IFF_CISCO)
949 h->protocol = htons (ETHERTYPE_IP);
952 * Don't choke with an ENETDOWN early. It's
953 * possible that we just started dialing out,
954 * so don't drop the packet immediately. If
955 * we notice that we run out of buffer space
956 * below, we will however remember that we are
957 * not ready to carry IP packets, and return
958 * ENETDOWN, as opposed to ENOBUFS.
960 h->protocol = htons(ipproto);
961 if (sp->state[IDX_IPCP] != STATE_OPENED)
967 case AF_INET6: /* Internet Protocol */
968 if (sp->pp_mode == IFF_CISCO)
969 h->protocol = htons (ETHERTYPE_IPV6);
972 * Don't choke with an ENETDOWN early. It's
973 * possible that we just started dialing out,
974 * so don't drop the packet immediately. If
975 * we notice that we run out of buffer space
976 * below, we will however remember that we are
977 * not ready to carry IP packets, and return
978 * ENETDOWN, as opposed to ENOBUFS.
980 h->protocol = htons(PPP_IPV6);
981 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
987 case AF_IPX: /* Novell IPX Protocol */
988 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
989 ETHERTYPE_IPX : PPP_IPX);
996 return (EAFNOSUPPORT);
1000 * Queue message on interface, and start output if interface
1005 error = !(IF_HANDOFF_ADJ(ifq, m, ifp, 3));
1007 IFQ_HANDOFF_ADJ(ifp, m, 3, error);
1011 return (rv? rv: ENOBUFS);
1015 * Unlike in sppp_input(), we can always bump the timestamp
1016 * here since sppp_output() is only called on behalf of
1017 * network-layer traffic; control-layer traffic is handled
1018 * by sppp_cp_send().
1020 sp->pp_last_sent = time_uptime;
1025 sppp_attach(struct ifnet *ifp)
1027 struct sppp *sp = IFP2SP(ifp);
1029 /* Initialize mtx lock */
1030 mtx_init(&sp->mtx, "sppp", MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
1032 /* Initialize keepalive handler. */
1033 callout_init(&sp->keepalive_callout, CALLOUT_MPSAFE);
1034 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
1037 ifp->if_mtu = PP_MTU;
1038 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
1039 ifp->if_output = sppp_output;
1041 sp->pp_flags = PP_KEEPALIVE;
1043 ifp->if_snd.ifq_maxlen = 32;
1044 sp->pp_fastq.ifq_maxlen = 32;
1045 sp->pp_cpq.ifq_maxlen = 20;
1047 sp->pp_alivecnt = 0;
1048 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
1049 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
1050 sp->pp_phase = PHASE_DEAD;
1051 sp->pp_up = sppp_pp_up;
1052 sp->pp_down = sppp_pp_down;
1053 if(!mtx_initialized(&sp->pp_cpq.ifq_mtx))
1054 mtx_init(&sp->pp_cpq.ifq_mtx, "sppp_cpq", NULL, MTX_DEF);
1055 if(!mtx_initialized(&sp->pp_fastq.ifq_mtx))
1056 mtx_init(&sp->pp_fastq.ifq_mtx, "sppp_fastq", NULL, MTX_DEF);
1057 sp->pp_last_recv = sp->pp_last_sent = time_uptime;
1060 sp->confflags |= CONF_ENABLE_VJ;
1063 sp->confflags |= CONF_ENABLE_IPV6;
1065 callout_init(&sp->ifstart_callout, CALLOUT_MPSAFE);
1066 sp->if_start = ifp->if_start;
1067 ifp->if_start = sppp_ifstart;
1068 sp->pp_comp = malloc(sizeof(struct slcompress), M_TEMP, M_WAITOK);
1069 sl_compress_init(sp->pp_comp, -1);
1072 sppp_ipv6cp_init(sp);
1078 sppp_detach(struct ifnet *ifp)
1080 struct sppp *sp = IFP2SP(ifp);
1083 KASSERT(mtx_initialized(&sp->mtx), ("sppp mutex is not initialized"));
1085 /* Stop keepalive handler. */
1086 if (!callout_drain(&sp->keepalive_callout))
1087 callout_stop(&sp->keepalive_callout);
1089 for (i = 0; i < IDX_COUNT; i++) {
1090 if (!callout_drain(&sp->ch[i]))
1091 callout_stop(&sp->ch[i]);
1093 if (!callout_drain(&sp->pap_my_to_ch))
1094 callout_stop(&sp->pap_my_to_ch);
1095 mtx_destroy(&sp->pp_cpq.ifq_mtx);
1096 mtx_destroy(&sp->pp_fastq.ifq_mtx);
1097 mtx_destroy(&sp->mtx);
1101 * Flush the interface output queue.
1104 sppp_flush_unlocked(struct ifnet *ifp)
1106 struct sppp *sp = IFP2SP(ifp);
1108 sppp_qflush ((struct ifqueue *)&SP2IFP(sp)->if_snd);
1109 sppp_qflush (&sp->pp_fastq);
1110 sppp_qflush (&sp->pp_cpq);
1114 sppp_flush(struct ifnet *ifp)
1116 struct sppp *sp = IFP2SP(ifp);
1119 sppp_flush_unlocked (ifp);
1124 * Check if the output queue is empty.
1127 sppp_isempty(struct ifnet *ifp)
1129 struct sppp *sp = IFP2SP(ifp);
1133 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
1134 !SP2IFP(sp)->if_snd.ifq_head;
1140 * Get next packet to send.
1143 sppp_dequeue(struct ifnet *ifp)
1145 struct sppp *sp = IFP2SP(ifp);
1150 * Process only the control protocol queue until we have at
1151 * least one NCP open.
1153 * Do always serve all three queues in Cisco mode.
1155 IF_DEQUEUE(&sp->pp_cpq, m);
1157 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO ||
1158 sp->pp_mode == PP_FR)) {
1159 IF_DEQUEUE(&sp->pp_fastq, m);
1161 IF_DEQUEUE (&SP2IFP(sp)->if_snd, m);
1168 * Pick the next packet, do not remove it from the queue.
1171 sppp_pick(struct ifnet *ifp)
1173 struct sppp *sp = IFP2SP(ifp);
1178 m = sp->pp_cpq.ifq_head;
1180 (sp->pp_phase == PHASE_NETWORK ||
1181 sp->pp_mode == IFF_CISCO ||
1182 sp->pp_mode == PP_FR))
1183 if ((m = sp->pp_fastq.ifq_head) == NULL)
1184 m = SP2IFP(sp)->if_snd.ifq_head;
1190 * Process an ioctl request. Called on low priority level.
1193 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1195 struct ifreq *ifr = (struct ifreq*) data;
1196 struct sppp *sp = IFP2SP(ifp);
1197 int rv, going_up, going_down, newmode;
1203 case SIOCSIFDSTADDR:
1207 /* set the interface "up" when assigning an IP address */
1208 ifp->if_flags |= IFF_UP;
1212 going_up = ifp->if_flags & IFF_UP &&
1213 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0;
1214 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1215 ifp->if_drv_flags & IFF_DRV_RUNNING;
1217 newmode = ifp->if_flags & IFF_PASSIVE;
1219 newmode = ifp->if_flags & IFF_AUTO;
1221 newmode = ifp->if_flags & IFF_CISCO;
1222 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1223 ifp->if_flags |= newmode;
1226 newmode = sp->pp_flags & PP_FR;
1228 if (newmode != sp->pp_mode) {
1231 going_up = ifp->if_drv_flags & IFF_DRV_RUNNING;
1235 if (sp->pp_mode != IFF_CISCO &&
1236 sp->pp_mode != PP_FR)
1238 else if (sp->pp_tlf)
1240 sppp_flush_unlocked(ifp);
1241 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1242 sp->pp_mode = newmode;
1246 if (sp->pp_mode != IFF_CISCO &&
1247 sp->pp_mode != PP_FR)
1249 sp->pp_mode = newmode;
1250 if (sp->pp_mode == 0) {
1251 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1254 if ((sp->pp_mode == IFF_CISCO) ||
1255 (sp->pp_mode == PP_FR)) {
1258 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1266 #define ifr_mtu ifr_metric
1269 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1271 ifp->if_mtu = ifr->ifr_mtu;
1276 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1278 ifp->if_mtu = *(short*)data;
1283 ifr->ifr_mtu = ifp->if_mtu;
1288 *(short*)data = ifp->if_mtu;
1295 case SIOCGIFGENERIC:
1296 case SIOCSIFGENERIC:
1297 rv = sppp_params(sp, cmd, data);
1308 * Cisco framing implementation.
1312 * Handle incoming Cisco keepalive protocol packets.
1315 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1318 struct cisco_packet *h;
1321 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1324 SPP_FMT "cisco invalid packet length: %d bytes\n",
1325 SPP_ARGS(ifp), m->m_pkthdr.len);
1328 h = mtod (m, struct cisco_packet*);
1331 SPP_FMT "cisco input: %d bytes "
1332 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1333 SPP_ARGS(ifp), m->m_pkthdr.len,
1334 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1335 (u_int)h->time0, (u_int)h->time1);
1336 switch (ntohl (h->type)) {
1339 log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n",
1340 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1342 case CISCO_ADDR_REPLY:
1343 /* Reply on address request, ignore */
1345 case CISCO_KEEPALIVE_REQ:
1346 sp->pp_alivecnt = 0;
1347 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1348 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1349 /* Local and remote sequence numbers are equal.
1350 * Probably, the line is in loopback mode. */
1351 if (sp->pp_loopcnt >= MAXALIVECNT) {
1352 printf (SPP_FMT "loopback\n",
1355 if (ifp->if_flags & IFF_UP) {
1357 sppp_qflush (&sp->pp_cpq);
1362 /* Generate new local sequence number */
1363 sp->pp_seq[IDX_LCP] = random();
1367 if (! (ifp->if_flags & IFF_UP) &&
1368 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1370 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1373 case CISCO_ADDR_REQ:
1374 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1376 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1382 * Send Cisco keepalive packet.
1385 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1388 struct ppp_header *h;
1389 struct cisco_packet *ch;
1393 getmicrouptime(&tv);
1395 MGETHDR (m, M_NOWAIT, MT_DATA);
1398 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1399 m->m_pkthdr.rcvif = 0;
1401 h = mtod (m, struct ppp_header*);
1402 h->address = CISCO_MULTICAST;
1404 h->protocol = htons (CISCO_KEEPALIVE);
1406 ch = (struct cisco_packet*) (h + 1);
1407 ch->type = htonl (type);
1408 ch->par1 = htonl (par1);
1409 ch->par2 = htonl (par2);
1412 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1413 ch->time1 = htons ((u_short) tv.tv_sec);
1417 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1418 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1419 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1421 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1426 * PPP protocol implementation.
1430 * Send PPP control protocol packet.
1433 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1434 u_char ident, u_short len, void *data)
1437 struct ppp_header *h;
1438 struct lcp_header *lh;
1441 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1442 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1443 MGETHDR (m, M_NOWAIT, MT_DATA);
1446 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1447 m->m_pkthdr.rcvif = 0;
1449 h = mtod (m, struct ppp_header*);
1450 h->address = PPP_ALLSTATIONS; /* broadcast address */
1451 h->control = PPP_UI; /* Unnumbered Info */
1452 h->protocol = htons (proto); /* Link Control Protocol */
1454 lh = (struct lcp_header*) (h + 1);
1457 lh->len = htons (LCP_HEADER_LEN + len);
1459 bcopy (data, lh+1, len);
1462 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1464 sppp_proto_name(proto),
1465 sppp_cp_type_name (lh->type), lh->ident,
1467 sppp_print_bytes ((u_char*) (lh+1), len);
1470 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1475 * Handle incoming PPP control protocol packets.
1478 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1481 struct lcp_header *h;
1482 int len = m->m_pkthdr.len;
1489 SPP_FMT "%s invalid packet length: %d bytes\n",
1490 SPP_ARGS(ifp), cp->name, len);
1493 h = mtod (m, struct lcp_header*);
1496 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1497 SPP_ARGS(ifp), cp->name,
1498 sppp_state_name(sp->state[cp->protoidx]),
1499 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1500 sppp_print_bytes ((u_char*) (h+1), len-4);
1503 if (len > ntohs (h->len))
1504 len = ntohs (h->len);
1505 p = (u_char *)(h + 1);
1510 log(-1, SPP_FMT "%s invalid conf-req length %d\n",
1511 SPP_ARGS(ifp), cp->name,
1516 /* handle states where RCR doesn't get a SCA/SCN */
1517 switch (sp->state[cp->protoidx]) {
1519 case STATE_STOPPING:
1522 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1526 rv = (cp->RCR)(sp, h, len);
1527 switch (sp->state[cp->protoidx]) {
1532 case STATE_ACK_SENT:
1533 case STATE_REQ_SENT:
1535 * sppp_cp_change_state() have the side effect of
1536 * restarting the timeouts. We want to avoid that
1537 * if the state don't change, otherwise we won't
1538 * ever timeout and resend a configuration request
1541 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1544 sppp_cp_change_state(cp, sp, rv?
1545 STATE_ACK_SENT: STATE_REQ_SENT);
1548 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1550 sppp_cp_change_state(cp, sp, rv?
1551 STATE_ACK_SENT: STATE_REQ_SENT);
1553 case STATE_ACK_RCVD:
1555 sppp_cp_change_state(cp, sp, STATE_OPENED);
1557 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1562 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1565 printf(SPP_FMT "%s illegal %s in state %s\n",
1566 SPP_ARGS(ifp), cp->name,
1567 sppp_cp_type_name(h->type),
1568 sppp_state_name(sp->state[cp->protoidx]));
1573 if (h->ident != sp->confid[cp->protoidx]) {
1575 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1576 SPP_ARGS(ifp), cp->name,
1577 h->ident, sp->confid[cp->protoidx]);
1581 switch (sp->state[cp->protoidx]) {
1584 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1587 case STATE_STOPPING:
1589 case STATE_REQ_SENT:
1590 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1591 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1596 case STATE_ACK_RCVD:
1598 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1600 case STATE_ACK_SENT:
1601 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1602 sppp_cp_change_state(cp, sp, STATE_OPENED);
1604 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1605 SPP_ARGS(ifp), cp->name);
1609 printf(SPP_FMT "%s illegal %s in state %s\n",
1610 SPP_ARGS(ifp), cp->name,
1611 sppp_cp_type_name(h->type),
1612 sppp_state_name(sp->state[cp->protoidx]));
1618 if (h->ident != sp->confid[cp->protoidx]) {
1620 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1621 SPP_ARGS(ifp), cp->name,
1622 h->ident, sp->confid[cp->protoidx]);
1626 if (h->type == CONF_NAK)
1627 (cp->RCN_nak)(sp, h, len);
1629 (cp->RCN_rej)(sp, h, len);
1631 switch (sp->state[cp->protoidx]) {
1634 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1636 case STATE_REQ_SENT:
1637 case STATE_ACK_SENT:
1638 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1640 * Slow things down a bit if we think we might be
1641 * in loopback. Depend on the timeout to send the
1642 * next configuration request.
1651 case STATE_ACK_RCVD:
1652 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1656 case STATE_STOPPING:
1659 printf(SPP_FMT "%s illegal %s in state %s\n",
1660 SPP_ARGS(ifp), cp->name,
1661 sppp_cp_type_name(h->type),
1662 sppp_state_name(sp->state[cp->protoidx]));
1668 switch (sp->state[cp->protoidx]) {
1669 case STATE_ACK_RCVD:
1670 case STATE_ACK_SENT:
1671 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1676 case STATE_STOPPING:
1677 case STATE_REQ_SENT:
1679 /* Send Terminate-Ack packet. */
1681 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1682 SPP_ARGS(ifp), cp->name);
1683 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1687 sp->rst_counter[cp->protoidx] = 0;
1688 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1692 printf(SPP_FMT "%s illegal %s in state %s\n",
1693 SPP_ARGS(ifp), cp->name,
1694 sppp_cp_type_name(h->type),
1695 sppp_state_name(sp->state[cp->protoidx]));
1700 switch (sp->state[cp->protoidx]) {
1703 case STATE_REQ_SENT:
1704 case STATE_ACK_SENT:
1707 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1710 case STATE_STOPPING:
1711 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1714 case STATE_ACK_RCVD:
1715 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1720 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1723 printf(SPP_FMT "%s illegal %s in state %s\n",
1724 SPP_ARGS(ifp), cp->name,
1725 sppp_cp_type_name(h->type),
1726 sppp_state_name(sp->state[cp->protoidx]));
1731 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1733 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1734 "danger will robinson\n",
1735 SPP_ARGS(ifp), cp->name,
1736 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1737 switch (sp->state[cp->protoidx]) {
1740 case STATE_REQ_SENT:
1741 case STATE_ACK_SENT:
1743 case STATE_STOPPING:
1746 case STATE_ACK_RCVD:
1747 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1750 printf(SPP_FMT "%s illegal %s in state %s\n",
1751 SPP_ARGS(ifp), cp->name,
1752 sppp_cp_type_name(h->type),
1753 sppp_state_name(sp->state[cp->protoidx]));
1760 const struct cp *upper;
1766 proto = ntohs(*((u_int16_t *)p));
1767 for (i = 0; i < IDX_COUNT; i++) {
1768 if (cps[i]->proto == proto) {
1776 if (catastrophic || debug)
1777 log(catastrophic? LOG_INFO: LOG_DEBUG,
1778 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1779 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1780 sppp_cp_type_name(h->type), proto,
1781 upper ? upper->name : "unknown",
1782 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1785 * if we got RXJ+ against conf-req, the peer does not implement
1786 * this particular protocol type. terminate the protocol.
1788 if (upper && !catastrophic) {
1789 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1795 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1796 switch (sp->state[cp->protoidx]) {
1799 case STATE_REQ_SENT:
1800 case STATE_ACK_SENT:
1802 case STATE_STOPPING:
1805 case STATE_ACK_RCVD:
1806 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1809 printf(SPP_FMT "%s illegal %s in state %s\n",
1810 SPP_ARGS(ifp), cp->name,
1811 sppp_cp_type_name(h->type),
1812 sppp_state_name(sp->state[cp->protoidx]));
1818 if (cp->proto != PPP_LCP)
1820 /* Discard the packet. */
1823 if (cp->proto != PPP_LCP)
1825 if (sp->state[cp->protoidx] != STATE_OPENED) {
1827 log(-1, SPP_FMT "lcp echo req but lcp closed\n",
1834 log(-1, SPP_FMT "invalid lcp echo request "
1835 "packet length: %d bytes\n",
1836 SPP_ARGS(ifp), len);
1839 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1840 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1841 /* Line loopback mode detected. */
1842 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1843 sp->pp_loopcnt = MAXALIVECNT * 5;
1845 sppp_qflush (&sp->pp_cpq);
1847 /* Shut down the PPP link. */
1853 *(long*)(h+1) = htonl (sp->lcp.magic);
1855 log(-1, SPP_FMT "got lcp echo req, sending echo rep\n",
1857 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1860 if (cp->proto != PPP_LCP)
1862 if (h->ident != sp->lcp.echoid) {
1868 log(-1, SPP_FMT "lcp invalid echo reply "
1869 "packet length: %d bytes\n",
1870 SPP_ARGS(ifp), len);
1874 log(-1, SPP_FMT "lcp got echo rep\n",
1876 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1877 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1878 sp->pp_alivecnt = 0;
1881 /* Unknown packet type -- send Code-Reject packet. */
1884 log(-1, SPP_FMT "%s send code-rej for 0x%x\n",
1885 SPP_ARGS(ifp), cp->name, h->type);
1886 sppp_cp_send(sp, cp->proto, CODE_REJ,
1887 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1894 * The generic part of all Up/Down/Open/Close/TO event handlers.
1895 * Basically, the state transition handling in the automaton.
1898 sppp_up_event(const struct cp *cp, struct sppp *sp)
1903 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1904 SPP_ARGS(ifp), cp->name,
1905 sppp_state_name(sp->state[cp->protoidx]));
1907 switch (sp->state[cp->protoidx]) {
1909 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1911 case STATE_STARTING:
1912 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1914 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1917 printf(SPP_FMT "%s illegal up in state %s\n",
1918 SPP_ARGS(ifp), cp->name,
1919 sppp_state_name(sp->state[cp->protoidx]));
1924 sppp_down_event(const struct cp *cp, struct sppp *sp)
1929 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1930 SPP_ARGS(ifp), cp->name,
1931 sppp_state_name(sp->state[cp->protoidx]));
1933 switch (sp->state[cp->protoidx]) {
1936 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1939 sppp_cp_change_state(cp, sp, STATE_STARTING);
1942 case STATE_STOPPING:
1943 case STATE_REQ_SENT:
1944 case STATE_ACK_RCVD:
1945 case STATE_ACK_SENT:
1946 sppp_cp_change_state(cp, sp, STATE_STARTING);
1950 sppp_cp_change_state(cp, sp, STATE_STARTING);
1953 printf(SPP_FMT "%s illegal down in state %s\n",
1954 SPP_ARGS(ifp), cp->name,
1955 sppp_state_name(sp->state[cp->protoidx]));
1961 sppp_open_event(const struct cp *cp, struct sppp *sp)
1966 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1967 SPP_ARGS(ifp), cp->name,
1968 sppp_state_name(sp->state[cp->protoidx]));
1970 switch (sp->state[cp->protoidx]) {
1972 sppp_cp_change_state(cp, sp, STATE_STARTING);
1975 case STATE_STARTING:
1978 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1980 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1984 * Try escaping stopped state. This seems to bite
1985 * people occasionally, in particular for IPCP,
1986 * presumably following previous IPCP negotiation
1987 * aborts. Somehow, we must have missed a Down event
1988 * which would have caused a transition into starting
1989 * state, so as a bandaid we force the Down event now.
1990 * This effectively implements (something like the)
1991 * `restart' option mentioned in the state transition
1992 * table of RFC 1661.
1994 sppp_cp_change_state(cp, sp, STATE_STARTING);
1997 case STATE_STOPPING:
1998 case STATE_REQ_SENT:
1999 case STATE_ACK_RCVD:
2000 case STATE_ACK_SENT:
2004 sppp_cp_change_state(cp, sp, STATE_STOPPING);
2011 sppp_close_event(const struct cp *cp, struct sppp *sp)
2016 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
2017 SPP_ARGS(ifp), cp->name,
2018 sppp_state_name(sp->state[cp->protoidx]));
2020 switch (sp->state[cp->protoidx]) {
2025 case STATE_STARTING:
2026 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2030 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2032 case STATE_STOPPING:
2033 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2038 case STATE_REQ_SENT:
2039 case STATE_ACK_RCVD:
2040 case STATE_ACK_SENT:
2041 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2042 sppp_cp_send(sp, cp->proto, TERM_REQ,
2043 ++sp->pp_seq[cp->protoidx], 0, 0);
2044 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2050 sppp_to_event(const struct cp *cp, struct sppp *sp)
2056 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
2057 SPP_ARGS(ifp), cp->name,
2058 sppp_state_name(sp->state[cp->protoidx]),
2059 sp->rst_counter[cp->protoidx]);
2061 if (--sp->rst_counter[cp->protoidx] < 0)
2063 switch (sp->state[cp->protoidx]) {
2065 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2068 case STATE_STOPPING:
2069 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2072 case STATE_REQ_SENT:
2073 case STATE_ACK_RCVD:
2074 case STATE_ACK_SENT:
2075 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2081 switch (sp->state[cp->protoidx]) {
2083 case STATE_STOPPING:
2084 sppp_cp_send(sp, cp->proto, TERM_REQ,
2085 ++sp->pp_seq[cp->protoidx], 0, 0);
2086 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2087 cp->TO, (void *)sp);
2089 case STATE_REQ_SENT:
2090 case STATE_ACK_RCVD:
2092 /* sppp_cp_change_state() will restart the timer */
2093 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2095 case STATE_ACK_SENT:
2097 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2098 cp->TO, (void *)sp);
2106 * Change the state of a control protocol in the state automaton.
2107 * Takes care of starting/stopping the restart timer.
2110 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2112 sp->state[cp->protoidx] = newstate;
2114 callout_stop (&sp->ch[cp->protoidx]);
2118 case STATE_STARTING:
2124 case STATE_STOPPING:
2125 case STATE_REQ_SENT:
2126 case STATE_ACK_RCVD:
2127 case STATE_ACK_SENT:
2128 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2129 cp->TO, (void *)sp);
2135 *--------------------------------------------------------------------------*
2137 * The LCP implementation. *
2139 *--------------------------------------------------------------------------*
2142 sppp_pp_up(struct sppp *sp)
2150 sppp_pp_down(struct sppp *sp)
2158 sppp_lcp_init(struct sppp *sp)
2160 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2162 sp->state[IDX_LCP] = STATE_INITIAL;
2163 sp->fail_counter[IDX_LCP] = 0;
2164 sp->pp_seq[IDX_LCP] = 0;
2165 sp->pp_rseq[IDX_LCP] = 0;
2167 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2169 /* Note that these values are relevant for all control protocols */
2170 sp->lcp.timeout = 3 * hz;
2171 sp->lcp.max_terminate = 2;
2172 sp->lcp.max_configure = 10;
2173 sp->lcp.max_failure = 10;
2174 callout_init(&sp->ch[IDX_LCP], CALLOUT_MPSAFE);
2178 sppp_lcp_up(struct sppp *sp)
2182 sp->pp_alivecnt = 0;
2183 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2186 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2188 * If we are authenticator, negotiate LCP_AUTH
2190 if (sp->hisauth.proto != 0)
2191 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2193 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2194 sp->pp_flags &= ~PP_NEEDAUTH;
2196 * If this interface is passive or dial-on-demand, and we are
2197 * still in Initial state, it means we've got an incoming
2198 * call. Activate the interface.
2200 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2203 SPP_FMT "Up event", SPP_ARGS(ifp));
2204 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2205 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2207 log(-1, "(incoming call)\n");
2208 sp->pp_flags |= PP_CALLIN;
2212 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2213 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2214 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2218 sppp_up_event(&lcp, sp);
2222 sppp_lcp_down(struct sppp *sp)
2226 sppp_down_event(&lcp, sp);
2229 * If this is neither a dial-on-demand nor a passive
2230 * interface, simulate an ``ifconfig down'' action, so the
2231 * administrator can force a redial by another ``ifconfig
2232 * up''. XXX For leased line operation, should we immediately
2233 * try to reopen the connection here?
2235 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2237 SPP_FMT "Down event, taking interface down.\n",
2243 SPP_FMT "Down event (carrier loss)\n",
2245 sp->pp_flags &= ~PP_CALLIN;
2246 if (sp->state[IDX_LCP] != STATE_INITIAL)
2248 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2253 sppp_lcp_open(struct sppp *sp)
2255 sppp_open_event(&lcp, sp);
2259 sppp_lcp_close(struct sppp *sp)
2261 sppp_close_event(&lcp, sp);
2265 sppp_lcp_TO(void *cookie)
2267 sppp_to_event(&lcp, (struct sppp *)cookie);
2271 * Analyze a configure request. Return true if it was agreeable, and
2272 * caused action sca, false if it has been rejected or nak'ed, and
2273 * caused action scn. (The return value is used to make the state
2274 * transition decision in the state automaton.)
2277 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2280 u_char *buf, *r, *p;
2287 buf = r = malloc (len, M_TEMP, M_NOWAIT);
2292 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2295 /* pass 1: check for things that need to be rejected */
2297 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2298 len-=p[1], p+=p[1]) {
2300 log(-1, " %s ", sppp_lcp_opt_name(*p));
2304 if (len >= 6 && p[1] == 6)
2307 log(-1, "[invalid] ");
2309 case LCP_OPT_ASYNC_MAP:
2310 /* Async control character map. */
2311 if (len >= 6 && p[1] == 6)
2314 log(-1, "[invalid] ");
2317 /* Maximum receive unit. */
2318 if (len >= 4 && p[1] == 4)
2321 log(-1, "[invalid] ");
2323 case LCP_OPT_AUTH_PROTO:
2326 log(-1, "[invalid] ");
2329 authproto = (p[2] << 8) + p[3];
2330 if (authproto == PPP_CHAP && p[1] != 5) {
2332 log(-1, "[invalid chap len] ");
2335 if (sp->myauth.proto == 0) {
2336 /* we are not configured to do auth */
2338 log(-1, "[not configured] ");
2342 * Remote want us to authenticate, remember this,
2343 * so we stay in PHASE_AUTHENTICATE after LCP got
2346 sp->pp_flags |= PP_NEEDAUTH;
2349 /* Others not supported. */
2354 /* Add the option to rejected list. */
2361 log(-1, " send conf-rej\n");
2362 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2368 * pass 2: check for option values that are unacceptable and
2369 * thus require to be nak'ed.
2372 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2377 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2378 len-=p[1], p+=p[1]) {
2380 log(-1, " %s ", sppp_lcp_opt_name(*p));
2383 /* Magic number -- extract. */
2384 nmagic = (u_long)p[2] << 24 |
2385 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2386 if (nmagic != sp->lcp.magic) {
2389 log(-1, "0x%lx ", nmagic);
2392 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2393 log(-1, "[glitch] ");
2396 * We negate our magic here, and NAK it. If
2397 * we see it later in an NAK packet, we
2398 * suggest a new one.
2400 nmagic = ~sp->lcp.magic;
2402 p[2] = nmagic >> 24;
2403 p[3] = nmagic >> 16;
2408 case LCP_OPT_ASYNC_MAP:
2410 * Async control character map -- just ignore it.
2412 * Quote from RFC 1662, chapter 6:
2413 * To enable this functionality, synchronous PPP
2414 * implementations MUST always respond to the
2415 * Async-Control-Character-Map Configuration
2416 * Option with the LCP Configure-Ack. However,
2417 * acceptance of the Configuration Option does
2418 * not imply that the synchronous implementation
2419 * will do any ACCM mapping. Instead, all such
2420 * octet mapping will be performed by the
2421 * asynchronous-to-synchronous converter.
2427 * Maximum receive unit. Always agreeable,
2428 * but ignored by now.
2430 sp->lcp.their_mru = p[2] * 256 + p[3];
2432 log(-1, "%lu ", sp->lcp.their_mru);
2435 case LCP_OPT_AUTH_PROTO:
2436 authproto = (p[2] << 8) + p[3];
2437 if (sp->myauth.proto != authproto) {
2438 /* not agreed, nak */
2440 log(-1, "[mine %s != his %s] ",
2441 sppp_proto_name(sp->hisauth.proto),
2442 sppp_proto_name(authproto));
2443 p[2] = sp->myauth.proto >> 8;
2444 p[3] = sp->myauth.proto;
2447 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2449 log(-1, "[chap not MD5] ");
2455 /* Add the option to nak'ed list. */
2462 * Local and remote magics equal -- loopback?
2464 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2465 if (sp->pp_loopcnt == MAXALIVECNT*5)
2466 printf (SPP_FMT "loopback\n",
2468 if (ifp->if_flags & IFF_UP) {
2470 sppp_qflush(&sp->pp_cpq);
2475 } else if (!sp->pp_loopcnt &&
2476 ++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2478 log(-1, " max_failure (%d) exceeded, "
2480 sp->lcp.max_failure);
2481 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2484 log(-1, " send conf-nak\n");
2485 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2489 log(-1, " send conf-ack\n");
2490 sp->fail_counter[IDX_LCP] = 0;
2492 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2493 h->ident, origlen, h+1);
2501 * Analyze the LCP Configure-Reject option list, and adjust our
2505 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2511 buf = malloc (len, M_TEMP, M_NOWAIT);
2516 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2520 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2521 len -= p[1], p += p[1]) {
2523 log(-1, " %s ", sppp_lcp_opt_name(*p));
2526 /* Magic number -- can't use it, use 0 */
2527 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2532 * Should not be rejected anyway, since we only
2533 * negotiate a MRU if explicitly requested by
2536 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2538 case LCP_OPT_AUTH_PROTO:
2540 * Peer doesn't want to authenticate himself,
2541 * deny unless this is a dialout call, and
2542 * AUTHFLAG_NOCALLOUT is set.
2544 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2545 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2547 log(-1, "[don't insist on auth "
2549 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2553 log(-1, "[access denied]\n");
2565 * Analyze the LCP Configure-NAK option list, and adjust our
2569 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2576 buf = malloc (len, M_TEMP, M_NOWAIT);
2581 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2585 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2586 len -= p[1], p += p[1]) {
2588 log(-1, " %s ", sppp_lcp_opt_name(*p));
2591 /* Magic number -- renegotiate */
2592 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2593 len >= 6 && p[1] == 6) {
2594 magic = (u_long)p[2] << 24 |
2595 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2597 * If the remote magic is our negated one,
2598 * this looks like a loopback problem.
2599 * Suggest a new magic to make sure.
2601 if (magic == ~sp->lcp.magic) {
2603 log(-1, "magic glitch ");
2604 sp->lcp.magic = random();
2606 sp->lcp.magic = magic;
2608 log(-1, "%lu ", magic);
2614 * Peer wants to advise us to negotiate an MRU.
2615 * Agree on it if it's reasonable, or use
2616 * default otherwise.
2618 if (len >= 4 && p[1] == 4) {
2619 u_int mru = p[2] * 256 + p[3];
2621 log(-1, "%d ", mru);
2622 if (mru < PP_MTU || mru > PP_MAX_MRU)
2625 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2628 case LCP_OPT_AUTH_PROTO:
2630 * Peer doesn't like our authentication method,
2634 log(-1, "[access denied]\n");
2646 sppp_lcp_tlu(struct sppp *sp)
2653 if (! (ifp->if_flags & IFF_UP) &&
2654 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2655 /* Coming out of loopback mode. */
2657 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2660 for (i = 0; i < IDX_COUNT; i++)
2661 if ((cps[i])->flags & CP_QUAL)
2664 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2665 (sp->pp_flags & PP_NEEDAUTH) != 0)
2666 sp->pp_phase = PHASE_AUTHENTICATE;
2668 sp->pp_phase = PHASE_NETWORK;
2671 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2672 sppp_phase_name(sp->pp_phase));
2675 * Open all authentication protocols. This is even required
2676 * if we already proceeded to network phase, since it might be
2677 * that remote wants us to authenticate, so we might have to
2678 * send a PAP request. Undesired authentication protocols
2679 * don't do anything when they get an Open event.
2681 for (i = 0; i < IDX_COUNT; i++)
2682 if ((cps[i])->flags & CP_AUTH)
2685 if (sp->pp_phase == PHASE_NETWORK) {
2686 /* Notify all NCPs. */
2687 for (i = 0; i < IDX_COUNT; i++)
2688 if (((cps[i])->flags & CP_NCP) &&
2691 * Hack to administratively disable IPv6 if
2692 * not desired. Perhaps we should have another
2693 * flag for this, but right now, we can make
2694 * all struct cp's read/only.
2696 (cps[i] != &ipv6cp ||
2697 (sp->confflags & CONF_ENABLE_IPV6)))
2701 /* Send Up events to all started protos. */
2702 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2703 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2706 /* notify low-level driver of state change */
2708 sp->pp_chg(sp, (int)sp->pp_phase);
2710 if (sp->pp_phase == PHASE_NETWORK)
2711 /* if no NCP is starting, close down */
2712 sppp_lcp_check_and_close(sp);
2716 sppp_lcp_tld(struct sppp *sp)
2722 sp->pp_phase = PHASE_TERMINATE;
2725 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2726 sppp_phase_name(sp->pp_phase));
2729 * Take upper layers down. We send the Down event first and
2730 * the Close second to prevent the upper layers from sending
2731 * ``a flurry of terminate-request packets'', as the RFC
2734 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2735 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2737 (cps[i])->Close(sp);
2742 sppp_lcp_tls(struct sppp *sp)
2746 sp->pp_phase = PHASE_ESTABLISH;
2749 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2750 sppp_phase_name(sp->pp_phase));
2752 /* Notify lower layer if desired. */
2760 sppp_lcp_tlf(struct sppp *sp)
2764 sp->pp_phase = PHASE_DEAD;
2766 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2767 sppp_phase_name(sp->pp_phase));
2769 /* Notify lower layer if desired. */
2777 sppp_lcp_scr(struct sppp *sp)
2779 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2783 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2784 if (! sp->lcp.magic)
2785 sp->lcp.magic = random();
2786 opt[i++] = LCP_OPT_MAGIC;
2788 opt[i++] = sp->lcp.magic >> 24;
2789 opt[i++] = sp->lcp.magic >> 16;
2790 opt[i++] = sp->lcp.magic >> 8;
2791 opt[i++] = sp->lcp.magic;
2794 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2795 opt[i++] = LCP_OPT_MRU;
2797 opt[i++] = sp->lcp.mru >> 8;
2798 opt[i++] = sp->lcp.mru;
2801 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2802 authproto = sp->hisauth.proto;
2803 opt[i++] = LCP_OPT_AUTH_PROTO;
2804 opt[i++] = authproto == PPP_CHAP? 5: 4;
2805 opt[i++] = authproto >> 8;
2806 opt[i++] = authproto;
2807 if (authproto == PPP_CHAP)
2808 opt[i++] = CHAP_MD5;
2811 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2812 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2816 * Check the open NCPs, return true if at least one NCP is open.
2819 sppp_ncp_check(struct sppp *sp)
2823 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2824 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2830 * Re-check the open NCPs and see if we should terminate the link.
2831 * Called by the NCPs during their tlf action handling.
2834 sppp_lcp_check_and_close(struct sppp *sp)
2837 if (sp->pp_phase < PHASE_NETWORK)
2838 /* don't bother, we are already going down */
2841 if (sppp_ncp_check(sp))
2848 *--------------------------------------------------------------------------*
2850 * The IPCP implementation. *
2852 *--------------------------------------------------------------------------*
2857 sppp_ipcp_init(struct sppp *sp)
2861 sp->state[IDX_IPCP] = STATE_INITIAL;
2862 sp->fail_counter[IDX_IPCP] = 0;
2863 sp->pp_seq[IDX_IPCP] = 0;
2864 sp->pp_rseq[IDX_IPCP] = 0;
2865 callout_init(&sp->ch[IDX_IPCP], CALLOUT_MPSAFE);
2869 sppp_ipcp_up(struct sppp *sp)
2871 sppp_up_event(&ipcp, sp);
2875 sppp_ipcp_down(struct sppp *sp)
2877 sppp_down_event(&ipcp, sp);
2881 sppp_ipcp_open(struct sppp *sp)
2884 u_long myaddr, hisaddr;
2886 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2887 IPCP_MYADDR_DYN | IPCP_VJ);
2890 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2892 * If we don't have his address, this probably means our
2893 * interface doesn't want to talk IP at all. (This could
2894 * be the case if somebody wants to speak only IPX, for
2895 * example.) Don't open IPCP in this case.
2897 if (hisaddr == 0L) {
2898 /* XXX this message should go away */
2900 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2906 * I don't have an assigned address, so i need to
2907 * negotiate my address.
2909 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2910 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2912 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2913 if (sp->confflags & CONF_ENABLE_VJ) {
2914 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2915 sp->ipcp.max_state = MAX_STATES - 1;
2916 sp->ipcp.compress_cid = 1;
2918 sppp_open_event(&ipcp, sp);
2922 sppp_ipcp_close(struct sppp *sp)
2924 sppp_close_event(&ipcp, sp);
2925 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2927 * My address was dynamic, clear it again.
2929 sppp_set_ip_addr(sp, 0L);
2933 sppp_ipcp_TO(void *cookie)
2935 sppp_to_event(&ipcp, (struct sppp *)cookie);
2939 * Analyze a configure request. Return true if it was agreeable, and
2940 * caused action sca, false if it has been rejected or nak'ed, and
2941 * caused action scn. (The return value is used to make the state
2942 * transition decision in the state automaton.)
2945 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2947 u_char *buf, *r, *p;
2948 struct ifnet *ifp = SP2IFP(sp);
2949 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2950 u_long hisaddr, desiredaddr;
2957 * Make sure to allocate a buf that can at least hold a
2958 * conf-nak with an `address' option. We might need it below.
2960 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2964 /* pass 1: see if we can recognize them */
2966 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2969 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2970 len-=p[1], p+=p[1]) {
2972 log(-1, " %s ", sppp_ipcp_opt_name(*p));
2974 case IPCP_OPT_COMPRESSION:
2975 if (!(sp->confflags & CONF_ENABLE_VJ)) {
2976 /* VJ compression administratively disabled */
2978 log(-1, "[locally disabled] ");
2982 * In theory, we should only conf-rej an
2983 * option that is shorter than RFC 1618
2984 * requires (i.e. < 4), and should conf-nak
2985 * anything else that is not VJ. However,
2986 * since our algorithm always uses the
2987 * original option to NAK it with new values,
2988 * things would become more complicated. In
2989 * pratice, the only commonly implemented IP
2990 * compression option is VJ anyway, so the
2991 * difference is negligible.
2993 if (len >= 6 && p[1] == 6) {
2995 * correctly formed compression option
2996 * that could be VJ compression
3002 "optlen %d [invalid/unsupported] ",
3005 case IPCP_OPT_ADDRESS:
3006 if (len >= 6 && p[1] == 6) {
3007 /* correctly formed address option */
3011 log(-1, "[invalid] ");
3014 /* Others not supported. */
3019 /* Add the option to rejected list. */
3026 log(-1, " send conf-rej\n");
3027 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
3032 /* pass 2: parse option values */
3033 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
3035 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
3039 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3040 len-=p[1], p+=p[1]) {
3042 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3044 case IPCP_OPT_COMPRESSION:
3045 desiredcomp = p[2] << 8 | p[3];
3046 /* We only support VJ */
3047 if (desiredcomp == IPCP_COMP_VJ) {
3049 log(-1, "VJ [ack] ");
3050 sp->ipcp.flags |= IPCP_VJ;
3051 sl_compress_init(sp->pp_comp, p[4]);
3052 sp->ipcp.max_state = p[4];
3053 sp->ipcp.compress_cid = p[5];
3058 "compproto %#04x [not supported] ",
3060 p[2] = IPCP_COMP_VJ >> 8;
3061 p[3] = IPCP_COMP_VJ;
3062 p[4] = sp->ipcp.max_state;
3063 p[5] = sp->ipcp.compress_cid;
3065 case IPCP_OPT_ADDRESS:
3066 /* This is the address he wants in his end */
3067 desiredaddr = p[2] << 24 | p[3] << 16 |
3069 if (desiredaddr == hisaddr ||
3070 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3072 * Peer's address is same as our value,
3073 * or we have set it to 0.0.0.* to
3074 * indicate that we do not really care,
3075 * this is agreeable. Gonna conf-ack
3079 log(-1, "%s [ack] ",
3080 sppp_dotted_quad(hisaddr));
3081 /* record that we've seen it already */
3082 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3086 * The address wasn't agreeable. This is either
3087 * he sent us 0.0.0.0, asking to assign him an
3088 * address, or he send us another address not
3089 * matching our value. Either case, we gonna
3090 * conf-nak it with our value.
3091 * XXX: we should "rej" if hisaddr == 0
3094 if (desiredaddr == 0)
3095 log(-1, "[addr requested] ");
3097 log(-1, "%s [not agreed] ",
3098 sppp_dotted_quad(desiredaddr));
3101 p[2] = hisaddr >> 24;
3102 p[3] = hisaddr >> 16;
3103 p[4] = hisaddr >> 8;
3107 /* Add the option to nak'ed list. */
3114 * If we are about to conf-ack the request, but haven't seen
3115 * his address so far, gonna conf-nak it instead, with the
3116 * `address' option present and our idea of his address being
3117 * filled in there, to request negotiation of both addresses.
3119 * XXX This can result in an endless req - nak loop if peer
3120 * doesn't want to send us his address. Q: What should we do
3121 * about it? XXX A: implement the max-failure counter.
3123 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3124 buf[0] = IPCP_OPT_ADDRESS;
3126 buf[2] = hisaddr >> 24;
3127 buf[3] = hisaddr >> 16;
3128 buf[4] = hisaddr >> 8;
3132 log(-1, "still need hisaddr ");
3137 log(-1, " send conf-nak\n");
3138 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3141 log(-1, " send conf-ack\n");
3142 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3143 h->ident, origlen, h+1);
3151 * Analyze the IPCP Configure-Reject option list, and adjust our
3155 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3158 struct ifnet *ifp = SP2IFP(sp);
3159 int debug = ifp->if_flags & IFF_DEBUG;
3162 buf = malloc (len, M_TEMP, M_NOWAIT);
3167 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3171 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3172 len -= p[1], p += p[1]) {
3174 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3176 case IPCP_OPT_COMPRESSION:
3177 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3179 case IPCP_OPT_ADDRESS:
3181 * Peer doesn't grok address option. This is
3182 * bad. XXX Should we better give up here?
3183 * XXX We could try old "addresses" option...
3185 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3196 * Analyze the IPCP Configure-NAK option list, and adjust our
3200 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3203 struct ifnet *ifp = SP2IFP(sp);
3204 int debug = ifp->if_flags & IFF_DEBUG;
3209 buf = malloc (len, M_TEMP, M_NOWAIT);
3214 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3218 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3219 len -= p[1], p += p[1]) {
3221 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3223 case IPCP_OPT_COMPRESSION:
3224 if (len >= 6 && p[1] == 6) {
3225 desiredcomp = p[2] << 8 | p[3];
3227 log(-1, "[wantcomp %#04x] ",
3229 if (desiredcomp == IPCP_COMP_VJ) {
3230 sl_compress_init(sp->pp_comp, p[4]);
3231 sp->ipcp.max_state = p[4];
3232 sp->ipcp.compress_cid = p[5];
3234 log(-1, "[agree] ");
3237 ~(1 << IPCP_OPT_COMPRESSION);
3240 case IPCP_OPT_ADDRESS:
3242 * Peer doesn't like our local IP address. See
3243 * if we can do something for him. We'll drop
3244 * him our address then.
3246 if (len >= 6 && p[1] == 6) {
3247 wantaddr = p[2] << 24 | p[3] << 16 |
3249 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3251 log(-1, "[wantaddr %s] ",
3252 sppp_dotted_quad(wantaddr));
3254 * When doing dynamic address assignment,
3255 * we accept his offer. Otherwise, we
3256 * ignore it and thus continue to negotiate
3257 * our already existing value.
3258 * XXX: Bogus, if he said no once, he'll
3259 * just say no again, might as well die.
3261 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3262 sppp_set_ip_addr(sp, wantaddr);
3264 log(-1, "[agree] ");
3265 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3278 sppp_ipcp_tlu(struct sppp *sp)
3280 /* we are up - notify isdn daemon */
3286 sppp_ipcp_tld(struct sppp *sp)
3291 sppp_ipcp_tls(struct sppp *sp)
3293 /* indicate to LCP that it must stay alive */
3294 sp->lcp.protos |= (1 << IDX_IPCP);
3298 sppp_ipcp_tlf(struct sppp *sp)
3300 /* we no longer need LCP */
3301 sp->lcp.protos &= ~(1 << IDX_IPCP);
3302 sppp_lcp_check_and_close(sp);
3306 sppp_ipcp_scr(struct sppp *sp)
3308 char opt[6 /* compression */ + 6 /* address */];
3312 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3313 opt[i++] = IPCP_OPT_COMPRESSION;
3315 opt[i++] = IPCP_COMP_VJ >> 8;
3316 opt[i++] = IPCP_COMP_VJ;
3317 opt[i++] = sp->ipcp.max_state;
3318 opt[i++] = sp->ipcp.compress_cid;
3320 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3321 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3322 opt[i++] = IPCP_OPT_ADDRESS;
3324 opt[i++] = ouraddr >> 24;
3325 opt[i++] = ouraddr >> 16;
3326 opt[i++] = ouraddr >> 8;
3330 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3331 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3335 sppp_ipcp_init(struct sppp *sp)
3340 sppp_ipcp_up(struct sppp *sp)
3345 sppp_ipcp_down(struct sppp *sp)
3350 sppp_ipcp_open(struct sppp *sp)
3355 sppp_ipcp_close(struct sppp *sp)
3360 sppp_ipcp_TO(void *cookie)
3365 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3371 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3376 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3381 sppp_ipcp_tlu(struct sppp *sp)
3386 sppp_ipcp_tld(struct sppp *sp)
3391 sppp_ipcp_tls(struct sppp *sp)
3396 sppp_ipcp_tlf(struct sppp *sp)
3401 sppp_ipcp_scr(struct sppp *sp)
3407 *--------------------------------------------------------------------------*
3409 * The IPv6CP implementation. *
3411 *--------------------------------------------------------------------------*
3416 sppp_ipv6cp_init(struct sppp *sp)
3418 sp->ipv6cp.opts = 0;
3419 sp->ipv6cp.flags = 0;
3420 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3421 sp->fail_counter[IDX_IPV6CP] = 0;
3422 sp->pp_seq[IDX_IPV6CP] = 0;
3423 sp->pp_rseq[IDX_IPV6CP] = 0;
3424 callout_init(&sp->ch[IDX_IPV6CP], CALLOUT_MPSAFE);
3428 sppp_ipv6cp_up(struct sppp *sp)
3430 sppp_up_event(&ipv6cp, sp);
3434 sppp_ipv6cp_down(struct sppp *sp)
3436 sppp_down_event(&ipv6cp, sp);
3440 sppp_ipv6cp_open(struct sppp *sp)
3443 struct in6_addr myaddr, hisaddr;
3445 #ifdef IPV6CP_MYIFID_DYN
3446 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3448 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3451 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3453 * If we don't have our address, this probably means our
3454 * interface doesn't want to talk IPv6 at all. (This could
3455 * be the case if somebody wants to speak only IPX, for
3456 * example.) Don't open IPv6CP in this case.
3458 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3459 /* XXX this message should go away */
3461 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3466 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3467 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3468 sppp_open_event(&ipv6cp, sp);
3472 sppp_ipv6cp_close(struct sppp *sp)
3474 sppp_close_event(&ipv6cp, sp);
3478 sppp_ipv6cp_TO(void *cookie)
3480 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3484 * Analyze a configure request. Return true if it was agreeable, and
3485 * caused action sca, false if it has been rejected or nak'ed, and
3486 * caused action scn. (The return value is used to make the state
3487 * transition decision in the state automaton.)
3490 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3492 u_char *buf, *r, *p;
3493 struct ifnet *ifp = SP2IFP(sp);
3494 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3495 struct in6_addr myaddr, desiredaddr, suggestaddr;
3498 int collision, nohisaddr;
3499 char ip6buf[INET6_ADDRSTRLEN];
3504 * Make sure to allocate a buf that can at least hold a
3505 * conf-nak with an `address' option. We might need it below.
3507 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3511 /* pass 1: see if we can recognize them */
3513 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3517 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3518 len-=p[1], p+=p[1]) {
3520 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3522 case IPV6CP_OPT_IFID:
3523 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3524 /* correctly formed address option */
3529 log(-1, " [invalid]");
3532 case IPV6CP_OPT_COMPRESSION:
3533 if (len >= 4 && p[1] >= 4) {
3534 /* correctly formed compress option */
3538 log(-1, " [invalid]");
3542 /* Others not supported. */
3547 /* Add the option to rejected list. */
3554 log(-1, " send conf-rej\n");
3555 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3560 /* pass 2: parse option values */
3561 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3563 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3568 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3569 len-=p[1], p+=p[1]) {
3571 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3574 case IPV6CP_OPT_COMPRESSION:
3577 case IPV6CP_OPT_IFID:
3578 bzero(&desiredaddr, sizeof(desiredaddr));
3579 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3580 collision = (bcmp(&desiredaddr.s6_addr[8],
3581 &myaddr.s6_addr[8], 8) == 0);
3582 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3584 desiredaddr.s6_addr16[0] = htons(0xfe80);
3585 (void)in6_setscope(&desiredaddr, SP2IFP(sp), NULL);
3587 if (!collision && !nohisaddr) {
3588 /* no collision, hisaddr known - Conf-Ack */
3593 ip6_sprintf(ip6buf, &desiredaddr),
3594 sppp_cp_type_name(type));
3599 bzero(&suggestaddr, sizeof(&suggestaddr));
3600 if (collision && nohisaddr) {
3601 /* collision, hisaddr unknown - Conf-Rej */
3606 * - no collision, hisaddr unknown, or
3607 * - collision, hisaddr known
3608 * Conf-Nak, suggest hisaddr
3611 sppp_suggest_ip6_addr(sp, &suggestaddr);
3612 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3616 ip6_sprintf(ip6buf, &desiredaddr),
3617 sppp_cp_type_name(type));
3620 /* Add the option to nak'ed list. */
3626 if (rlen == 0 && type == CONF_ACK) {
3628 log(-1, " send %s\n", sppp_cp_type_name(type));
3629 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3632 if (type == CONF_ACK)
3633 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3637 log(-1, " send %s suggest %s\n",
3638 sppp_cp_type_name(type),
3639 ip6_sprintf(ip6buf, &suggestaddr));
3641 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3650 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3654 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3657 struct ifnet *ifp = SP2IFP(sp);
3658 int debug = ifp->if_flags & IFF_DEBUG;
3661 buf = malloc (len, M_TEMP, M_NOWAIT);
3666 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3670 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3671 len -= p[1], p += p[1]) {
3673 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3675 case IPV6CP_OPT_IFID:
3677 * Peer doesn't grok address option. This is
3678 * bad. XXX Should we better give up here?
3680 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3683 case IPV6CP_OPT_COMPRESS:
3684 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3696 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3700 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3703 struct ifnet *ifp = SP2IFP(sp);
3704 int debug = ifp->if_flags & IFF_DEBUG;
3705 struct in6_addr suggestaddr;
3706 char ip6buf[INET6_ADDRSTRLEN];
3709 buf = malloc (len, M_TEMP, M_NOWAIT);
3714 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3718 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3719 len -= p[1], p += p[1]) {
3721 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3723 case IPV6CP_OPT_IFID:
3725 * Peer doesn't like our local ifid. See
3726 * if we can do something for him. We'll drop
3727 * him our address then.
3729 if (len < 10 || p[1] != 10)
3731 bzero(&suggestaddr, sizeof(suggestaddr));
3732 suggestaddr.s6_addr16[0] = htons(0xfe80);
3733 (void)in6_setscope(&suggestaddr, SP2IFP(sp), NULL);
3734 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3736 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3738 log(-1, " [suggestaddr %s]",
3739 ip6_sprintf(ip6buf, &suggestaddr));
3740 #ifdef IPV6CP_MYIFID_DYN
3742 * When doing dynamic address assignment,
3743 * we accept his offer.
3745 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3746 struct in6_addr lastsuggest;
3748 * If <suggested myaddr from peer> equals to
3749 * <hisaddr we have suggested last time>,
3750 * we have a collision. generate new random
3753 sppp_suggest_ip6_addr(&lastsuggest);
3754 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3757 log(-1, " [random]");
3758 sppp_gen_ip6_addr(sp, &suggestaddr);
3760 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3762 log(-1, " [agree]");
3763 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3767 * Since we do not do dynamic address assignment,
3768 * we ignore it and thus continue to negotiate
3769 * our already existing value. This can possibly
3770 * go into infinite request-reject loop.
3772 * This is not likely because we normally use
3773 * ifid based on MAC-address.
3774 * If you have no ethernet card on the node, too bad.
3775 * XXX should we use fail_counter?
3780 case IPV6CP_OPT_COMPRESS:
3782 * Peer wants different compression parameters.
3794 sppp_ipv6cp_tlu(struct sppp *sp)
3796 /* we are up - notify isdn daemon */
3802 sppp_ipv6cp_tld(struct sppp *sp)
3807 sppp_ipv6cp_tls(struct sppp *sp)
3809 /* indicate to LCP that it must stay alive */
3810 sp->lcp.protos |= (1 << IDX_IPV6CP);
3814 sppp_ipv6cp_tlf(struct sppp *sp)
3817 #if 0 /* need #if 0 to close IPv6CP properly */
3818 /* we no longer need LCP */
3819 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3820 sppp_lcp_check_and_close(sp);
3825 sppp_ipv6cp_scr(struct sppp *sp)
3827 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3828 struct in6_addr ouraddr;
3831 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3832 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3833 opt[i++] = IPV6CP_OPT_IFID;
3835 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3840 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3841 opt[i++] = IPV6CP_OPT_COMPRESSION;
3843 opt[i++] = 0; /* TBD */
3844 opt[i++] = 0; /* TBD */
3845 /* variable length data may follow */
3849 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3850 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3853 static void sppp_ipv6cp_init(struct sppp *sp)
3857 static void sppp_ipv6cp_up(struct sppp *sp)
3861 static void sppp_ipv6cp_down(struct sppp *sp)
3866 static void sppp_ipv6cp_open(struct sppp *sp)
3870 static void sppp_ipv6cp_close(struct sppp *sp)
3874 static void sppp_ipv6cp_TO(void *sp)
3878 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3883 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3887 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3891 static void sppp_ipv6cp_tlu(struct sppp *sp)
3895 static void sppp_ipv6cp_tld(struct sppp *sp)
3899 static void sppp_ipv6cp_tls(struct sppp *sp)
3903 static void sppp_ipv6cp_tlf(struct sppp *sp)
3907 static void sppp_ipv6cp_scr(struct sppp *sp)
3913 *--------------------------------------------------------------------------*
3915 * The CHAP implementation. *
3917 *--------------------------------------------------------------------------*
3921 * The authentication protocols don't employ a full-fledged state machine as
3922 * the control protocols do, since they do have Open and Close events, but
3923 * not Up and Down, nor are they explicitly terminated. Also, use of the
3924 * authentication protocols may be different in both directions (this makes
3925 * sense, think of a machine that never accepts incoming calls but only
3926 * calls out, it doesn't require the called party to authenticate itself).
3928 * Our state machine for the local authentication protocol (we are requesting
3929 * the peer to authenticate) looks like:
3932 * +--------------------------------------------+
3934 * +--------+ Close +---------+ RCA+
3935 * | |<----------------------------------| |------+
3936 * +--->| Closed | TO* | Opened | sca |
3937 * | | |-----+ +-------| |<-----+
3938 * | +--------+ irc | | +---------+
3944 * | | +------->+ | |
3946 * | +--------+ V | |
3947 * | | |<----+<--------------------+ |
3953 * +------+ +------------------------------------------+
3954 * scn,tld sca,irc,ict,tlu
3959 * Open: LCP reached authentication phase
3960 * Close: LCP reached terminate phase
3962 * RCA+: received reply (pap-req, chap-response), acceptable
3963 * RCN: received reply (pap-req, chap-response), not acceptable
3964 * TO+: timeout with restart counter >= 0
3965 * TO-: timeout with restart counter < 0
3966 * TO*: reschedule timeout for CHAP
3968 * scr: send request packet (none for PAP, chap-challenge)
3969 * sca: send ack packet (pap-ack, chap-success)
3970 * scn: send nak packet (pap-nak, chap-failure)
3971 * ict: initialize re-challenge timer (CHAP only)
3973 * tlu: this-layer-up, LCP reaches network phase
3974 * tld: this-layer-down, LCP enters terminate phase
3976 * Note that in CHAP mode, after sending a new challenge, while the state
3977 * automaton falls back into Req-Sent state, it doesn't signal a tld
3978 * event to LCP, so LCP remains in network phase. Only after not getting
3979 * any response (or after getting an unacceptable response), CHAP closes,
3980 * causing LCP to enter terminate phase.
3982 * With PAP, there is no initial request that can be sent. The peer is
3983 * expected to send one based on the successful negotiation of PAP as
3984 * the authentication protocol during the LCP option negotiation.
3986 * Incoming authentication protocol requests (remote requests
3987 * authentication, we are peer) don't employ a state machine at all,
3988 * they are simply answered. Some peers [Ascend P50 firmware rev
3989 * 4.50] react allergically when sending IPCP requests while they are
3990 * still in authentication phase (thereby violating the standard that
3991 * demands that these NCP packets are to be discarded), so we keep
3992 * track of the peer demanding us to authenticate, and only proceed to
3993 * phase network once we've seen a positive acknowledge for the
3998 * Handle incoming CHAP packets.
4001 sppp_chap_input(struct sppp *sp, struct mbuf *m)
4004 struct lcp_header *h;
4006 u_char *value, *name, digest[AUTHKEYLEN], dsize;
4007 int value_len, name_len;
4010 len = m->m_pkthdr.len;
4014 SPP_FMT "chap invalid packet length: %d bytes\n",
4015 SPP_ARGS(ifp), len);
4018 h = mtod (m, struct lcp_header*);
4019 if (len > ntohs (h->len))
4020 len = ntohs (h->len);
4023 /* challenge, failure and success are his authproto */
4024 case CHAP_CHALLENGE:
4025 value = 1 + (u_char*)(h+1);
4026 value_len = value[-1];
4027 name = value + value_len;
4028 name_len = len - value_len - 5;
4032 SPP_FMT "chap corrupted challenge "
4033 "<%s id=0x%x len=%d",
4035 sppp_auth_type_name(PPP_CHAP, h->type),
4036 h->ident, ntohs(h->len));
4037 sppp_print_bytes((u_char*) (h+1), len-4);
4045 SPP_FMT "chap input <%s id=0x%x len=%d name=",
4047 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
4049 sppp_print_string((char*) name, name_len);
4050 log(-1, " value-size=%d value=", value_len);
4051 sppp_print_bytes(value, value_len);
4055 /* Compute reply value. */
4057 MD5Update(&ctx, &h->ident, 1);
4058 MD5Update(&ctx, sp->myauth.secret,
4059 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
4060 MD5Update(&ctx, value, value_len);
4061 MD5Final(digest, &ctx);
4062 dsize = sizeof digest;
4064 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
4065 sizeof dsize, (const char *)&dsize,
4066 sizeof digest, digest,
4067 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4074 log(LOG_DEBUG, SPP_FMT "chap success",
4078 sppp_print_string((char*)(h + 1), len - 4);
4083 sp->pp_flags &= ~PP_NEEDAUTH;
4084 if (sp->myauth.proto == PPP_CHAP &&
4085 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4086 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
4088 * We are authenticator for CHAP but didn't
4089 * complete yet. Leave it to tlu to proceed
4096 sppp_phase_network(sp);
4101 log(LOG_INFO, SPP_FMT "chap failure",
4105 sppp_print_string((char*)(h + 1), len - 4);
4109 log(LOG_INFO, SPP_FMT "chap failure\n",
4111 /* await LCP shutdown by authenticator */
4114 /* response is my authproto */
4116 value = 1 + (u_char*)(h+1);
4117 value_len = value[-1];
4118 name = value + value_len;
4119 name_len = len - value_len - 5;
4123 SPP_FMT "chap corrupted response "
4124 "<%s id=0x%x len=%d",
4126 sppp_auth_type_name(PPP_CHAP, h->type),
4127 h->ident, ntohs(h->len));
4128 sppp_print_bytes((u_char*)(h+1), len-4);
4133 if (h->ident != sp->confid[IDX_CHAP]) {
4136 SPP_FMT "chap dropping response for old ID "
4137 "(got %d, expected %d)\n",
4139 h->ident, sp->confid[IDX_CHAP]);
4142 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4143 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4144 log(LOG_INFO, SPP_FMT "chap response, his name ",
4146 sppp_print_string(name, name_len);
4147 log(-1, " != expected ");
4148 sppp_print_string(sp->hisauth.name,
4149 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4153 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4154 "<%s id=0x%x len=%d name=",
4156 sppp_state_name(sp->state[IDX_CHAP]),
4157 sppp_auth_type_name(PPP_CHAP, h->type),
4158 h->ident, ntohs (h->len));
4159 sppp_print_string((char*)name, name_len);
4160 log(-1, " value-size=%d value=", value_len);
4161 sppp_print_bytes(value, value_len);
4164 if (value_len != AUTHKEYLEN) {
4167 SPP_FMT "chap bad hash value length: "
4168 "%d bytes, should be %d\n",
4169 SPP_ARGS(ifp), value_len,
4175 MD5Update(&ctx, &h->ident, 1);
4176 MD5Update(&ctx, sp->hisauth.secret,
4177 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4178 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4179 MD5Final(digest, &ctx);
4181 #define FAILMSG "Failed..."
4182 #define SUCCMSG "Welcome!"
4184 if (value_len != sizeof digest ||
4185 bcmp(digest, value, value_len) != 0) {
4186 /* action scn, tld */
4187 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4188 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4193 /* action sca, perhaps tlu */
4194 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4195 sp->state[IDX_CHAP] == STATE_OPENED)
4196 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4197 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4199 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4200 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4206 /* Unknown CHAP packet type -- ignore. */
4208 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4209 "<0x%x id=0x%xh len=%d",
4211 sppp_state_name(sp->state[IDX_CHAP]),
4212 h->type, h->ident, ntohs(h->len));
4213 sppp_print_bytes((u_char*)(h+1), len-4);
4222 sppp_chap_init(struct sppp *sp)
4224 /* Chap doesn't have STATE_INITIAL at all. */
4225 sp->state[IDX_CHAP] = STATE_CLOSED;
4226 sp->fail_counter[IDX_CHAP] = 0;
4227 sp->pp_seq[IDX_CHAP] = 0;
4228 sp->pp_rseq[IDX_CHAP] = 0;
4229 callout_init(&sp->ch[IDX_CHAP], CALLOUT_MPSAFE);
4233 sppp_chap_open(struct sppp *sp)
4235 if (sp->myauth.proto == PPP_CHAP &&
4236 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4237 /* we are authenticator for CHAP, start it */
4239 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4240 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4242 /* nothing to be done if we are peer, await a challenge */
4246 sppp_chap_close(struct sppp *sp)
4248 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4249 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4253 sppp_chap_TO(void *cookie)
4255 struct sppp *sp = (struct sppp *)cookie;
4260 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4262 sppp_state_name(sp->state[IDX_CHAP]),
4263 sp->rst_counter[IDX_CHAP]);
4265 if (--sp->rst_counter[IDX_CHAP] < 0)
4267 switch (sp->state[IDX_CHAP]) {
4268 case STATE_REQ_SENT:
4270 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4274 /* TO+ (or TO*) event */
4275 switch (sp->state[IDX_CHAP]) {
4278 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4280 case STATE_REQ_SENT:
4282 /* sppp_cp_change_state() will restart the timer */
4283 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4291 sppp_chap_tlu(struct sppp *sp)
4297 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4300 * Some broken CHAP implementations (Conware CoNet, firmware
4301 * 4.0.?) don't want to re-authenticate their CHAP once the
4302 * initial challenge-response exchange has taken place.
4303 * Provide for an option to avoid rechallenges.
4305 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4307 * Compute the re-challenge timeout. This will yield
4308 * a number between 300 and 810 seconds.
4310 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
4311 callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, (void *)sp);
4316 SPP_FMT "chap %s, ",
4318 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4319 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4320 log(-1, "next re-challenge in %d seconds\n", i);
4322 log(-1, "re-challenging supressed\n");
4326 /* indicate to LCP that we need to be closed down */
4327 sp->lcp.protos |= (1 << IDX_CHAP);
4329 if (sp->pp_flags & PP_NEEDAUTH) {
4331 * Remote is authenticator, but his auth proto didn't
4332 * complete yet. Defer the transition to network
4341 * If we are already in phase network, we are done here. This
4342 * is the case if this is a dummy tlu event after a re-challenge.
4344 if (sp->pp_phase != PHASE_NETWORK)
4345 sppp_phase_network(sp);
4349 sppp_chap_tld(struct sppp *sp)
4354 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4355 callout_stop(&sp->ch[IDX_CHAP]);
4356 sp->lcp.protos &= ~(1 << IDX_CHAP);
4362 sppp_chap_scr(struct sppp *sp)
4367 /* Compute random challenge. */
4368 ch = (u_long *)sp->myauth.challenge;
4369 read_random(&seed, sizeof seed);
4370 ch[0] = seed ^ random();
4371 ch[1] = seed ^ random();
4372 ch[2] = seed ^ random();
4373 ch[3] = seed ^ random();
4376 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4378 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4379 sizeof clen, (const char *)&clen,
4380 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4381 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4387 *--------------------------------------------------------------------------*
4389 * The PAP implementation. *
4391 *--------------------------------------------------------------------------*
4394 * For PAP, we need to keep a little state also if we are the peer, not the
4395 * authenticator. This is since we don't get a request to authenticate, but
4396 * have to repeatedly authenticate ourself until we got a response (or the
4397 * retry counter is expired).
4401 * Handle incoming PAP packets. */
4403 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4406 struct lcp_header *h;
4408 u_char *name, *passwd, mlen;
4409 int name_len, passwd_len;
4411 len = m->m_pkthdr.len;
4415 SPP_FMT "pap invalid packet length: %d bytes\n",
4416 SPP_ARGS(ifp), len);
4419 h = mtod (m, struct lcp_header*);
4420 if (len > ntohs (h->len))
4421 len = ntohs (h->len);
4423 /* PAP request is my authproto */
4425 name = 1 + (u_char*)(h+1);
4426 name_len = name[-1];
4427 passwd = name + name_len + 1;
4428 if (name_len > len - 6 ||
4429 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4431 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4432 "<%s id=0x%x len=%d",
4434 sppp_auth_type_name(PPP_PAP, h->type),
4435 h->ident, ntohs(h->len));
4436 sppp_print_bytes((u_char*)(h+1), len-4);
4442 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4443 "<%s id=0x%x len=%d name=",
4445 sppp_state_name(sp->state[IDX_PAP]),
4446 sppp_auth_type_name(PPP_PAP, h->type),
4447 h->ident, ntohs(h->len));
4448 sppp_print_string((char*)name, name_len);
4449 log(-1, " passwd=");
4450 sppp_print_string((char*)passwd, passwd_len);
4453 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4454 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4455 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4456 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4457 /* action scn, tld */
4458 mlen = sizeof(FAILMSG) - 1;
4459 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4460 sizeof mlen, (const char *)&mlen,
4461 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4466 /* action sca, perhaps tlu */
4467 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4468 sp->state[IDX_PAP] == STATE_OPENED) {
4469 mlen = sizeof(SUCCMSG) - 1;
4470 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4471 sizeof mlen, (const char *)&mlen,
4472 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4475 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4476 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4481 /* ack and nak are his authproto */
4483 callout_stop(&sp->pap_my_to_ch);
4485 log(LOG_DEBUG, SPP_FMT "pap success",
4487 name_len = *((char *)h);
4488 if (len > 5 && name_len) {
4490 sppp_print_string((char*)(h+1), name_len);
4495 sp->pp_flags &= ~PP_NEEDAUTH;
4496 if (sp->myauth.proto == PPP_PAP &&
4497 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4498 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4500 * We are authenticator for PAP but didn't
4501 * complete yet. Leave it to tlu to proceed
4508 sppp_phase_network(sp);
4512 callout_stop (&sp->pap_my_to_ch);
4514 log(LOG_INFO, SPP_FMT "pap failure",
4516 name_len = *((char *)h);
4517 if (len > 5 && name_len) {
4519 sppp_print_string((char*)(h+1), name_len);
4523 log(LOG_INFO, SPP_FMT "pap failure\n",
4525 /* await LCP shutdown by authenticator */
4529 /* Unknown PAP packet type -- ignore. */
4531 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4532 "<0x%x id=0x%x len=%d",
4534 h->type, h->ident, ntohs(h->len));
4535 sppp_print_bytes((u_char*)(h+1), len-4);
4544 sppp_pap_init(struct sppp *sp)
4546 /* PAP doesn't have STATE_INITIAL at all. */
4547 sp->state[IDX_PAP] = STATE_CLOSED;
4548 sp->fail_counter[IDX_PAP] = 0;
4549 sp->pp_seq[IDX_PAP] = 0;
4550 sp->pp_rseq[IDX_PAP] = 0;
4551 callout_init(&sp->ch[IDX_PAP], CALLOUT_MPSAFE);
4552 callout_init(&sp->pap_my_to_ch, CALLOUT_MPSAFE);
4556 sppp_pap_open(struct sppp *sp)
4558 if (sp->hisauth.proto == PPP_PAP &&
4559 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4560 /* we are authenticator for PAP, start our timer */
4561 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4562 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4564 if (sp->myauth.proto == PPP_PAP) {
4565 /* we are peer, send a request, and start a timer */
4567 callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
4568 sppp_pap_my_TO, (void *)sp);
4573 sppp_pap_close(struct sppp *sp)
4575 if (sp->state[IDX_PAP] != STATE_CLOSED)
4576 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4580 * That's the timeout routine if we are authenticator. Since the
4581 * authenticator is basically passive in PAP, we can't do much here.
4584 sppp_pap_TO(void *cookie)
4586 struct sppp *sp = (struct sppp *)cookie;
4591 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4593 sppp_state_name(sp->state[IDX_PAP]),
4594 sp->rst_counter[IDX_PAP]);
4596 if (--sp->rst_counter[IDX_PAP] < 0)
4598 switch (sp->state[IDX_PAP]) {
4599 case STATE_REQ_SENT:
4601 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4605 /* TO+ event, not very much we could do */
4606 switch (sp->state[IDX_PAP]) {
4607 case STATE_REQ_SENT:
4608 /* sppp_cp_change_state() will restart the timer */
4609 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4617 * That's the timeout handler if we are peer. Since the peer is active,
4618 * we need to retransmit our PAP request since it is apparently lost.
4619 * XXX We should impose a max counter.
4622 sppp_pap_my_TO(void *cookie)
4624 struct sppp *sp = (struct sppp *)cookie;
4628 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4637 sppp_pap_tlu(struct sppp *sp)
4641 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4644 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4645 SPP_ARGS(ifp), pap.name);
4648 /* indicate to LCP that we need to be closed down */
4649 sp->lcp.protos |= (1 << IDX_PAP);
4651 if (sp->pp_flags & PP_NEEDAUTH) {
4653 * Remote is authenticator, but his auth proto didn't
4654 * complete yet. Defer the transition to network
4661 sppp_phase_network(sp);
4665 sppp_pap_tld(struct sppp *sp)
4670 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4671 callout_stop (&sp->ch[IDX_PAP]);
4672 callout_stop (&sp->pap_my_to_ch);
4673 sp->lcp.protos &= ~(1 << IDX_PAP);
4679 sppp_pap_scr(struct sppp *sp)
4681 u_char idlen, pwdlen;
4683 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4684 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4685 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4687 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4688 sizeof idlen, (const char *)&idlen,
4689 (size_t)idlen, sp->myauth.name,
4690 sizeof pwdlen, (const char *)&pwdlen,
4691 (size_t)pwdlen, sp->myauth.secret,
4696 * Random miscellaneous functions.
4700 * Send a PAP or CHAP proto packet.
4702 * Varadic function, each of the elements for the ellipsis is of type
4703 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4705 * NOTE: never declare variadic functions with types subject to type
4706 * promotion (i.e. u_char). This is asking for big trouble depending
4707 * on the architecture you are on...
4711 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4712 unsigned int type, unsigned int id,
4716 struct ppp_header *h;
4717 struct lcp_header *lh;
4725 MGETHDR (m, M_NOWAIT, MT_DATA);
4728 m->m_pkthdr.rcvif = 0;
4730 h = mtod (m, struct ppp_header*);
4731 h->address = PPP_ALLSTATIONS; /* broadcast address */
4732 h->control = PPP_UI; /* Unnumbered Info */
4733 h->protocol = htons(cp->proto);
4735 lh = (struct lcp_header*)(h + 1);
4738 p = (u_char*) (lh+1);
4743 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
4744 msg = va_arg(ap, const char *);
4746 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4752 bcopy(msg, p, mlen);
4757 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4758 lh->len = htons (LCP_HEADER_LEN + len);
4761 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4762 SPP_ARGS(ifp), cp->name,
4763 sppp_auth_type_name(cp->proto, lh->type),
4764 lh->ident, ntohs(lh->len));
4765 sppp_print_bytes((u_char*) (lh+1), len);
4768 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
4773 * Flush interface queue.
4776 sppp_qflush(struct ifqueue *ifq)
4791 * Send keepalive packets, every 10 seconds.
4794 sppp_keepalive(void *dummy)
4796 struct sppp *sp = (struct sppp*)dummy;
4797 struct ifnet *ifp = SP2IFP(sp);
4800 /* Keepalive mode disabled or channel down? */
4801 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4802 ! (ifp->if_drv_flags & IFF_DRV_RUNNING))
4805 if (sp->pp_mode == PP_FR) {
4806 sppp_fr_keepalive (sp);
4810 /* No keepalive in PPP mode if LCP not opened yet. */
4811 if (sp->pp_mode != IFF_CISCO &&
4812 sp->pp_phase < PHASE_AUTHENTICATE)
4815 if (sp->pp_alivecnt == MAXALIVECNT) {
4816 /* No keepalive packets got. Stop the interface. */
4817 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
4819 sppp_qflush (&sp->pp_cpq);
4820 if (sp->pp_mode != IFF_CISCO) {
4822 /* Shut down the PPP link. */
4824 /* Initiate negotiation. XXX */
4828 if (sp->pp_alivecnt <= MAXALIVECNT)
4830 if (sp->pp_mode == IFF_CISCO)
4831 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4832 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4833 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4834 long nmagic = htonl (sp->lcp.magic);
4835 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4836 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4837 sp->lcp.echoid, 4, &nmagic);
4841 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
4846 * Get both IP addresses.
4849 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4851 struct ifnet *ifp = SP2IFP(sp);
4853 struct sockaddr_in *si, *sm;
4859 * Pick the first AF_INET address from the list,
4860 * aliases don't make any sense on a p2p link anyway.
4864 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4865 if (ifa->ifa_addr->sa_family == AF_INET) {
4866 si = (struct sockaddr_in *)ifa->ifa_addr;
4867 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4872 if (si && si->sin_addr.s_addr) {
4873 ssrc = si->sin_addr.s_addr;
4875 *srcmask = ntohl(sm->sin_addr.s_addr);
4878 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4879 if (si && si->sin_addr.s_addr)
4880 ddst = si->sin_addr.s_addr;
4882 if_addr_runlock(ifp);
4884 if (dst) *dst = ntohl(ddst);
4885 if (src) *src = ntohl(ssrc);
4890 * Set my IP address.
4893 sppp_set_ip_addr(struct sppp *sp, u_long src)
4897 struct sockaddr_in *si;
4898 struct in_ifaddr *ia;
4901 * Pick the first AF_INET address from the list,
4902 * aliases don't make any sense on a p2p link anyway.
4906 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
4907 if (ifa->ifa_addr->sa_family == AF_INET) {
4908 si = (struct sockaddr_in *)ifa->ifa_addr;
4915 if_addr_runlock(ifp);
4920 /* delete old route */
4921 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4922 if (debug && error) {
4923 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4924 SPP_ARGS(ifp), error);
4927 /* set new address */
4928 si->sin_addr.s_addr = htonl(src);
4931 LIST_REMOVE(ia, ia_hash);
4932 LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash);
4933 IN_IFADDR_WUNLOCK();
4936 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
4937 if (debug && error) {
4938 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
4939 SPP_ARGS(ifp), error);
4948 * Get both IPv6 addresses.
4951 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4952 struct in6_addr *srcmask)
4954 struct ifnet *ifp = SP2IFP(sp);
4956 struct sockaddr_in6 *si, *sm;
4957 struct in6_addr ssrc, ddst;
4960 bzero(&ssrc, sizeof(ssrc));
4961 bzero(&ddst, sizeof(ddst));
4963 * Pick the first link-local AF_INET6 address from the list,
4964 * aliases don't make any sense on a p2p link anyway.
4968 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4969 if (ifa->ifa_addr->sa_family == AF_INET6) {
4970 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4971 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4972 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
4976 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
4977 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
4979 bcopy(&sm->sin6_addr, srcmask,
4984 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
4985 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
4986 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
4990 bcopy(&ddst, dst, sizeof(*dst));
4992 bcopy(&ssrc, src, sizeof(*src));
4993 if_addr_runlock(ifp);
4996 #ifdef IPV6CP_MYIFID_DYN
4998 * Generate random ifid.
5001 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
5007 * Set my IPv6 address.
5010 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
5014 struct sockaddr_in6 *sin6;
5017 * Pick the first link-local AF_INET6 address from the list,
5018 * aliases don't make any sense on a p2p link anyway.
5023 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
5024 if (ifa->ifa_addr->sa_family == AF_INET6) {
5025 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
5026 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) {
5032 if_addr_runlock(ifp);
5036 struct sockaddr_in6 new_sin6 = *sin6;
5038 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
5039 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5040 if (debug && error) {
5041 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
5042 " failed, error=%d\n", SPP_ARGS(ifp), error);
5050 * Suggest a candidate address to be used by peer.
5053 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5055 struct in6_addr myaddr;
5058 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5060 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5062 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5063 myaddr.s6_addr[14] ^= 0xff;
5064 myaddr.s6_addr[15] ^= 0xff;
5066 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5067 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5070 bcopy(&myaddr, suggest, sizeof(myaddr));
5075 sppp_params(struct sppp *sp, u_long cmd, void *data)
5078 struct ifreq *ifr = (struct ifreq *)data;
5079 struct spppreq *spr;
5082 if ((spr = malloc(sizeof(struct spppreq), M_TEMP, M_NOWAIT)) == 0)
5085 * ifr->ifr_data is supposed to point to a struct spppreq.
5086 * Check the cmd word first before attempting to fetch all the
5089 if ((subcmd = fuword(ifr->ifr_data)) == -1) {
5094 if (copyin((caddr_t)ifr->ifr_data, spr, sizeof(struct spppreq)) != 0) {
5100 case (u_long)SPPPIOGDEFS:
5101 if (cmd != SIOCGIFGENERIC) {
5106 * We copy over the entire current state, but clean
5107 * out some of the stuff we don't wanna pass up.
5108 * Remember, SIOCGIFGENERIC is unprotected, and can be
5109 * called by any user. No need to ever get PAP or
5110 * CHAP secrets back to userland anyway.
5112 spr->defs.pp_phase = sp->pp_phase;
5113 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5114 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5115 spr->defs.lcp = sp->lcp;
5116 spr->defs.ipcp = sp->ipcp;
5117 spr->defs.ipv6cp = sp->ipv6cp;
5118 spr->defs.myauth = sp->myauth;
5119 spr->defs.hisauth = sp->hisauth;
5120 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5121 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5122 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5123 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5125 * Fixup the LCP timeout value to milliseconds so
5126 * spppcontrol doesn't need to bother about the value
5127 * of "hz". We do the reverse calculation below when
5130 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5131 rv = copyout(spr, (caddr_t)ifr->ifr_data,
5132 sizeof(struct spppreq));
5135 case (u_long)SPPPIOSDEFS:
5136 if (cmd != SIOCSIFGENERIC) {
5141 * We have a very specific idea of which fields we
5142 * allow being passed back from userland, so to not
5143 * clobber our current state. For one, we only allow
5144 * setting anything if LCP is in dead or establish
5145 * phase. Once the authentication negotiations
5146 * started, the authentication settings must not be
5147 * changed again. (The administrator can force an
5148 * ifconfig down in order to get LCP back into dead
5151 * Also, we only allow for authentication parameters to be
5154 * XXX Should allow to set or clear pp_flags.
5156 * Finally, if the respective authentication protocol to
5157 * be used is set differently than 0, but the secret is
5158 * passed as all zeros, we don't trash the existing secret.
5159 * This allows an administrator to change the system name
5160 * only without clobbering the secret (which he didn't get
5161 * back in a previous SPPPIOGDEFS call). However, the
5162 * secrets are cleared if the authentication protocol is
5164 if (sp->pp_phase != PHASE_DEAD &&
5165 sp->pp_phase != PHASE_ESTABLISH) {
5170 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5171 spr->defs.myauth.proto != PPP_CHAP) ||
5172 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5173 spr->defs.hisauth.proto != PPP_CHAP)) {
5178 if (spr->defs.myauth.proto == 0)
5179 /* resetting myauth */
5180 bzero(&sp->myauth, sizeof sp->myauth);
5182 /* setting/changing myauth */
5183 sp->myauth.proto = spr->defs.myauth.proto;
5184 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5185 if (spr->defs.myauth.secret[0] != '\0')
5186 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5189 if (spr->defs.hisauth.proto == 0)
5190 /* resetting hisauth */
5191 bzero(&sp->hisauth, sizeof sp->hisauth);
5193 /* setting/changing hisauth */
5194 sp->hisauth.proto = spr->defs.hisauth.proto;
5195 sp->hisauth.flags = spr->defs.hisauth.flags;
5196 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5197 if (spr->defs.hisauth.secret[0] != '\0')
5198 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5201 /* set LCP restart timer timeout */
5202 if (spr->defs.lcp.timeout != 0)
5203 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5204 /* set VJ enable and IPv6 disable flags */
5206 if (spr->defs.enable_vj)
5207 sp->confflags |= CONF_ENABLE_VJ;
5209 sp->confflags &= ~CONF_ENABLE_VJ;
5212 if (spr->defs.enable_ipv6)
5213 sp->confflags |= CONF_ENABLE_IPV6;
5215 sp->confflags &= ~CONF_ENABLE_IPV6;
5230 sppp_phase_network(struct sppp *sp)
5236 sp->pp_phase = PHASE_NETWORK;
5239 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5240 sppp_phase_name(sp->pp_phase));
5242 /* Notify NCPs now. */
5243 for (i = 0; i < IDX_COUNT; i++)
5244 if ((cps[i])->flags & CP_NCP)
5247 /* Send Up events to all NCPs. */
5248 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5249 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5252 /* if no NCP is starting, all this was in vain, close down */
5253 sppp_lcp_check_and_close(sp);
5258 sppp_cp_type_name(u_char type)
5260 static char buf[12];
5262 case CONF_REQ: return "conf-req";
5263 case CONF_ACK: return "conf-ack";
5264 case CONF_NAK: return "conf-nak";
5265 case CONF_REJ: return "conf-rej";
5266 case TERM_REQ: return "term-req";
5267 case TERM_ACK: return "term-ack";
5268 case CODE_REJ: return "code-rej";
5269 case PROTO_REJ: return "proto-rej";
5270 case ECHO_REQ: return "echo-req";
5271 case ECHO_REPLY: return "echo-reply";
5272 case DISC_REQ: return "discard-req";
5274 snprintf (buf, sizeof(buf), "cp/0x%x", type);
5279 sppp_auth_type_name(u_short proto, u_char type)
5281 static char buf[12];
5285 case CHAP_CHALLENGE: return "challenge";
5286 case CHAP_RESPONSE: return "response";
5287 case CHAP_SUCCESS: return "success";
5288 case CHAP_FAILURE: return "failure";
5292 case PAP_REQ: return "req";
5293 case PAP_ACK: return "ack";
5294 case PAP_NAK: return "nak";
5297 snprintf (buf, sizeof(buf), "auth/0x%x", type);
5302 sppp_lcp_opt_name(u_char opt)
5304 static char buf[12];
5306 case LCP_OPT_MRU: return "mru";
5307 case LCP_OPT_ASYNC_MAP: return "async-map";
5308 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5309 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5310 case LCP_OPT_MAGIC: return "magic";
5311 case LCP_OPT_PROTO_COMP: return "proto-comp";
5312 case LCP_OPT_ADDR_COMP: return "addr-comp";
5314 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5320 sppp_ipcp_opt_name(u_char opt)
5322 static char buf[12];
5324 case IPCP_OPT_ADDRESSES: return "addresses";
5325 case IPCP_OPT_COMPRESSION: return "compression";
5326 case IPCP_OPT_ADDRESS: return "address";
5328 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5335 sppp_ipv6cp_opt_name(u_char opt)
5337 static char buf[12];
5339 case IPV6CP_OPT_IFID: return "ifid";
5340 case IPV6CP_OPT_COMPRESSION: return "compression";
5342 sprintf (buf, "0x%x", opt);
5348 sppp_state_name(int state)
5351 case STATE_INITIAL: return "initial";
5352 case STATE_STARTING: return "starting";
5353 case STATE_CLOSED: return "closed";
5354 case STATE_STOPPED: return "stopped";
5355 case STATE_CLOSING: return "closing";
5356 case STATE_STOPPING: return "stopping";
5357 case STATE_REQ_SENT: return "req-sent";
5358 case STATE_ACK_RCVD: return "ack-rcvd";
5359 case STATE_ACK_SENT: return "ack-sent";
5360 case STATE_OPENED: return "opened";
5366 sppp_phase_name(enum ppp_phase phase)
5369 case PHASE_DEAD: return "dead";
5370 case PHASE_ESTABLISH: return "establish";
5371 case PHASE_TERMINATE: return "terminate";
5372 case PHASE_AUTHENTICATE: return "authenticate";
5373 case PHASE_NETWORK: return "network";
5379 sppp_proto_name(u_short proto)
5381 static char buf[12];
5383 case PPP_LCP: return "lcp";
5384 case PPP_IPCP: return "ipcp";
5385 case PPP_PAP: return "pap";
5386 case PPP_CHAP: return "chap";
5387 case PPP_IPV6CP: return "ipv6cp";
5389 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5394 sppp_print_bytes(const u_char *p, u_short len)
5397 log(-1, " %*D", len, p, "-");
5401 sppp_print_string(const char *p, u_short len)
5408 * Print only ASCII chars directly. RFC 1994 recommends
5409 * using only them, but we don't rely on it. */
5410 if (c < ' ' || c > '~')
5411 log(-1, "\\x%x", c);
5419 sppp_dotted_quad(u_long addr)
5422 sprintf(s, "%d.%d.%d.%d",
5423 (int)((addr >> 24) & 0xff),
5424 (int)((addr >> 16) & 0xff),
5425 (int)((addr >> 8) & 0xff),
5426 (int)(addr & 0xff));
5432 sppp_strnlen(u_char *p, int max)
5436 for (len = 0; len < max && *p; ++p)
5441 /* a dummy, used to drop uninteresting events */
5443 sppp_null(struct sppp *unused)
5445 /* do just nothing */