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>
26 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
28 #include "opt_inet6.h"
34 # include "opt_inet.h"
35 # include "opt_inet6.h"
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/sockio.h>
44 #include <sys/socket.h>
45 #include <sys/syslog.h>
46 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
47 #include <sys/random.h>
49 #include <sys/malloc.h>
52 #if defined (__OpenBSD__)
59 #include <net/netisr.h>
60 #include <net/if_types.h>
61 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <net/slcompress.h>
67 #if defined (__NetBSD__) || defined (__OpenBSD__)
68 #include <machine/cpu.h> /* XXX for softnet */
71 #include <machine/stdarg.h>
73 #include <netinet/in_var.h>
76 #include <netinet/ip.h>
77 #include <netinet/tcp.h>
80 #if defined (__FreeBSD__) || defined (__OpenBSD__)
81 # include <netinet/if_ether.h>
83 # include <net/ethertypes.h>
87 #include <netipx/ipx.h>
88 #include <netipx/ipx_if.h>
91 #include <net/if_sppp.h>
93 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
94 # define IOCTL_CMD_T u_long
96 # define IOCTL_CMD_T int
99 #define MAXALIVECNT 3 /* max. alive packets */
102 * Interface flags that can be set in an ifconfig command.
104 * Setting link0 will make the link passive, i.e. it will be marked
105 * as being administrative openable, but won't be opened to begin
106 * with. Incoming calls will be answered, or subsequent calls with
107 * -link1 will cause the administrative open of the LCP layer.
109 * Setting link1 will cause the link to auto-dial only as packets
112 * Setting IFF_DEBUG will syslog the option negotiation and state
113 * transitions at level kern.debug. Note: all logs consistently look
116 * <if-name><unit>: <proto-name> <additional info...>
118 * with <if-name><unit> being something like "bppp0", and <proto-name>
119 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
122 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
123 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
124 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
126 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
127 #define PPP_UI 0x03 /* Unnumbered Information */
128 #define PPP_IP 0x0021 /* Internet Protocol */
129 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
130 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
131 #define PPP_IPX 0x002b /* Novell IPX Protocol */
132 #define PPP_VJ_COMP 0x002d /* VJ compressed TCP/IP */
133 #define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */
134 #define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */
135 #define PPP_LCP 0xc021 /* Link Control Protocol */
136 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
137 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
138 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
139 #define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */
141 #define CONF_REQ 1 /* PPP configure request */
142 #define CONF_ACK 2 /* PPP configure acknowledge */
143 #define CONF_NAK 3 /* PPP configure negative ack */
144 #define CONF_REJ 4 /* PPP configure reject */
145 #define TERM_REQ 5 /* PPP terminate request */
146 #define TERM_ACK 6 /* PPP terminate acknowledge */
147 #define CODE_REJ 7 /* PPP code reject */
148 #define PROTO_REJ 8 /* PPP protocol reject */
149 #define ECHO_REQ 9 /* PPP echo request */
150 #define ECHO_REPLY 10 /* PPP echo reply */
151 #define DISC_REQ 11 /* PPP discard request */
153 #define LCP_OPT_MRU 1 /* maximum receive unit */
154 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
155 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
156 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
157 #define LCP_OPT_MAGIC 5 /* magic number */
158 #define LCP_OPT_RESERVED 6 /* reserved */
159 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
160 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
162 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
163 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
164 #define IPCP_OPT_ADDRESS 3 /* local IP address */
166 #define IPV6CP_OPT_IFID 1 /* interface identifier */
167 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
169 #define IPCP_COMP_VJ 0x2d /* Code for VJ compression */
171 #define PAP_REQ 1 /* PAP name/password request */
172 #define PAP_ACK 2 /* PAP acknowledge */
173 #define PAP_NAK 3 /* PAP fail */
175 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
176 #define CHAP_RESPONSE 2 /* CHAP challenge response */
177 #define CHAP_SUCCESS 3 /* CHAP response ok */
178 #define CHAP_FAILURE 4 /* CHAP response failed */
180 #define CHAP_MD5 5 /* hash algorithm - MD5 */
182 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
183 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
184 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
185 #define CISCO_ADDR_REQ 0 /* Cisco address request */
186 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
187 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
189 /* states are named and numbered according to RFC 1661 */
190 #define STATE_INITIAL 0
191 #define STATE_STARTING 1
192 #define STATE_CLOSED 2
193 #define STATE_STOPPED 3
194 #define STATE_CLOSING 4
195 #define STATE_STOPPING 5
196 #define STATE_REQ_SENT 6
197 #define STATE_ACK_RCVD 7
198 #define STATE_ACK_SENT 8
199 #define STATE_OPENED 9
206 #define PPP_HEADER_LEN sizeof (struct ppp_header)
213 #define LCP_HEADER_LEN sizeof (struct lcp_header)
215 struct cisco_packet {
223 #define CISCO_PACKET_LEN sizeof (struct cisco_packet)
226 * We follow the spelling and capitalization of RFC 1661 here, to make
227 * it easier comparing with the standard. Please refer to this RFC in
228 * case you can't make sense out of these abbreviation; it will also
229 * explain the semantics related to the various events and actions.
232 u_short proto; /* PPP control protocol number */
233 u_char protoidx; /* index into state table in struct sppp */
235 #define CP_LCP 0x01 /* this is the LCP */
236 #define CP_AUTH 0x02 /* this is an authentication protocol */
237 #define CP_NCP 0x04 /* this is a NCP */
238 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
239 const char *name; /* name of this control protocol */
241 void (*Up)(struct sppp *sp);
242 void (*Down)(struct sppp *sp);
243 void (*Open)(struct sppp *sp);
244 void (*Close)(struct sppp *sp);
245 void (*TO)(void *sp);
246 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
247 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
248 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
250 void (*tlu)(struct sppp *sp);
251 void (*tld)(struct sppp *sp);
252 void (*tls)(struct sppp *sp);
253 void (*tlf)(struct sppp *sp);
254 void (*scr)(struct sppp *sp);
257 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 && __FreeBSD_version < 501113
258 #define SPP_FMT "%s%d: "
259 #define SPP_ARGS(ifp) (ifp)->if_name, (ifp)->if_unit
261 #define SPP_FMT "%s: "
262 #define SPP_ARGS(ifp) (ifp)->if_xname
265 #define SPPP_LOCK(sp) \
267 if (!((sp)->pp_if.if_flags & IFF_NEEDSGIANT)) \
268 mtx_lock (&(sp)->mtx); \
270 #define SPPP_UNLOCK(sp) \
272 if (!((sp)->pp_if.if_flags & IFF_NEEDSGIANT)) \
273 mtx_unlock (&(sp)->mtx); \
276 #define SPPP_LOCK_ASSERT(sp) \
278 if (!((sp)->pp_if.if_flags & IFF_NEEDSGIANT)) \
279 mtx_assert (&(sp)->mtx, MA_OWNED); \
281 #define SPPP_LOCK_OWNED(sp) \
282 (!((sp)->pp_if.if_flags & IFF_NEEDSGIANT) && \
283 mtx_owned (&sp->mtx))
287 * The following disgusting hack gets around the problem that IP TOS
288 * can't be set yet. We want to put "interactive" traffic on a high
289 * priority queue. To decide if traffic is interactive, we check that
290 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
292 * XXX is this really still necessary? - joerg -
294 static const u_short interactive_ports[8] = {
298 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
301 /* almost every function needs these */
303 struct ifnet *ifp = &sp->pp_if; \
304 int debug = ifp->if_flags & IFF_DEBUG
306 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
307 struct sockaddr *dst, struct rtentry *rt);
309 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
310 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
312 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
314 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
315 u_char ident, u_short len, void *data);
316 /* static void sppp_cp_timeout(void *arg); */
317 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
319 static void sppp_auth_send(const struct cp *cp,
320 struct sppp *sp, unsigned int type, unsigned int id,
323 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
324 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
325 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
326 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
327 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
329 static void sppp_null(struct sppp *sp);
331 static void sppp_pp_up(struct sppp *sp);
332 static void sppp_pp_down(struct sppp *sp);
334 static void sppp_lcp_init(struct sppp *sp);
335 static void sppp_lcp_up(struct sppp *sp);
336 static void sppp_lcp_down(struct sppp *sp);
337 static void sppp_lcp_open(struct sppp *sp);
338 static void sppp_lcp_close(struct sppp *sp);
339 static void sppp_lcp_TO(void *sp);
340 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
341 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
342 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
343 static void sppp_lcp_tlu(struct sppp *sp);
344 static void sppp_lcp_tld(struct sppp *sp);
345 static void sppp_lcp_tls(struct sppp *sp);
346 static void sppp_lcp_tlf(struct sppp *sp);
347 static void sppp_lcp_scr(struct sppp *sp);
348 static void sppp_lcp_check_and_close(struct sppp *sp);
349 static int sppp_ncp_check(struct sppp *sp);
351 static void sppp_ipcp_init(struct sppp *sp);
352 static void sppp_ipcp_up(struct sppp *sp);
353 static void sppp_ipcp_down(struct sppp *sp);
354 static void sppp_ipcp_open(struct sppp *sp);
355 static void sppp_ipcp_close(struct sppp *sp);
356 static void sppp_ipcp_TO(void *sp);
357 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
358 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
359 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
360 static void sppp_ipcp_tlu(struct sppp *sp);
361 static void sppp_ipcp_tld(struct sppp *sp);
362 static void sppp_ipcp_tls(struct sppp *sp);
363 static void sppp_ipcp_tlf(struct sppp *sp);
364 static void sppp_ipcp_scr(struct sppp *sp);
366 static void sppp_ipv6cp_init(struct sppp *sp);
367 static void sppp_ipv6cp_up(struct sppp *sp);
368 static void sppp_ipv6cp_down(struct sppp *sp);
369 static void sppp_ipv6cp_open(struct sppp *sp);
370 static void sppp_ipv6cp_close(struct sppp *sp);
371 static void sppp_ipv6cp_TO(void *sp);
372 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
373 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
374 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
375 static void sppp_ipv6cp_tlu(struct sppp *sp);
376 static void sppp_ipv6cp_tld(struct sppp *sp);
377 static void sppp_ipv6cp_tls(struct sppp *sp);
378 static void sppp_ipv6cp_tlf(struct sppp *sp);
379 static void sppp_ipv6cp_scr(struct sppp *sp);
381 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
382 static void sppp_pap_init(struct sppp *sp);
383 static void sppp_pap_open(struct sppp *sp);
384 static void sppp_pap_close(struct sppp *sp);
385 static void sppp_pap_TO(void *sp);
386 static void sppp_pap_my_TO(void *sp);
387 static void sppp_pap_tlu(struct sppp *sp);
388 static void sppp_pap_tld(struct sppp *sp);
389 static void sppp_pap_scr(struct sppp *sp);
391 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
392 static void sppp_chap_init(struct sppp *sp);
393 static void sppp_chap_open(struct sppp *sp);
394 static void sppp_chap_close(struct sppp *sp);
395 static void sppp_chap_TO(void *sp);
396 static void sppp_chap_tlu(struct sppp *sp);
397 static void sppp_chap_tld(struct sppp *sp);
398 static void sppp_chap_scr(struct sppp *sp);
400 static const char *sppp_auth_type_name(u_short proto, u_char type);
401 static const char *sppp_cp_type_name(u_char type);
402 static const char *sppp_dotted_quad(u_long addr);
403 static const char *sppp_ipcp_opt_name(u_char opt);
405 static const char *sppp_ipv6cp_opt_name(u_char opt);
407 static const char *sppp_lcp_opt_name(u_char opt);
408 static const char *sppp_phase_name(enum ppp_phase phase);
409 static const char *sppp_proto_name(u_short proto);
410 static const char *sppp_state_name(int state);
411 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
412 static int sppp_strnlen(u_char *p, int max);
413 static void sppp_keepalive(void *dummy);
414 static void sppp_phase_network(struct sppp *sp);
415 static void sppp_print_bytes(const u_char *p, u_short len);
416 static void sppp_print_string(const char *p, u_short len);
417 static void sppp_qflush(struct ifqueue *ifq);
418 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
420 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
421 struct in6_addr *dst, struct in6_addr *srcmask);
422 #ifdef IPV6CP_MYIFID_DYN
423 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
424 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
426 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
429 /* if_start () wrapper */
430 static void sppp_ifstart (struct ifnet *ifp);
432 /* our control protocol descriptors */
433 static const struct cp lcp = {
434 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
435 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
436 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
437 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
441 static const struct cp ipcp = {
443 #ifdef INET /* don't run IPCP if there's no IPv4 support */
449 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
450 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
451 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
455 static const struct cp ipv6cp = {
456 PPP_IPV6CP, IDX_IPV6CP,
457 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
463 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
464 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
465 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
469 static const struct cp pap = {
470 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
471 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
472 sppp_pap_TO, 0, 0, 0,
473 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
477 static const struct cp chap = {
478 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
479 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
480 sppp_chap_TO, 0, 0, 0,
481 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
485 static const struct cp *cps[IDX_COUNT] = {
487 &ipcp, /* IDX_IPCP */
488 &ipv6cp, /* IDX_IPV6CP */
490 &chap, /* IDX_CHAP */
494 sppp_modevent(module_t mod, int type, void *unused)
506 static moduledata_t spppmod = {
511 MODULE_VERSION(sppp, 1);
512 DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
515 * Exported functions, comprising our interface to the lower layer.
519 * Process the received packet.
522 sppp_input(struct ifnet *ifp, struct mbuf *m)
524 struct ppp_header *h;
526 struct sppp *sp = (struct sppp *)ifp;
528 int hlen, vjlen, do_account = 0;
532 debug = ifp->if_flags & IFF_DEBUG;
534 if (ifp->if_flags & IFF_UP)
535 /* Count received bytes, add FCS and one flag */
536 ifp->if_ibytes += m->m_pkthdr.len + 3;
538 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
539 /* Too small packet, drop it. */
542 SPP_FMT "input packet is too small, %d bytes\n",
543 SPP_ARGS(ifp), m->m_pkthdr.len);
553 if (sp->pp_mode == PP_FR) {
554 sppp_fr_input (sp, m);
559 /* Get PPP header. */
560 h = mtod (m, struct ppp_header*);
561 m_adj (m, PPP_HEADER_LEN);
563 switch (h->address) {
564 case PPP_ALLSTATIONS:
565 if (h->control != PPP_UI)
567 if (sp->pp_mode == IFF_CISCO) {
570 SPP_FMT "PPP packet in Cisco mode "
571 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
573 h->address, h->control, ntohs(h->protocol));
576 switch (ntohs (h->protocol)) {
580 SPP_FMT "rejecting protocol "
581 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
583 h->address, h->control, ntohs(h->protocol));
584 if (sp->state[IDX_LCP] == STATE_OPENED)
585 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
586 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
591 sppp_cp_input(&lcp, sp, m);
596 if (sp->pp_phase >= PHASE_AUTHENTICATE)
597 sppp_pap_input(sp, m);
602 if (sp->pp_phase >= PHASE_AUTHENTICATE)
603 sppp_chap_input(sp, m);
609 if (sp->pp_phase == PHASE_NETWORK)
610 sppp_cp_input(&ipcp, sp, m);
615 if (sp->state[IDX_IPCP] == STATE_OPENED) {
621 if (sp->state[IDX_IPCP] == STATE_OPENED) {
623 sl_uncompress_tcp_core(mtod(m, u_char *),
627 &iphdr, &hlen)) <= 0) {
630 SPP_FMT "VJ uncompress failed on compressed packet\n",
636 * Trim the VJ header off the packet, and prepend
637 * the uncompressed IP header (which will usually
638 * end up in two chained mbufs since there's not
639 * enough leading space in the existing mbuf).
642 M_PREPEND(m, hlen, M_DONTWAIT);
647 bcopy(iphdr, mtod(m, u_char *), hlen);
653 if (sp->state[IDX_IPCP] == STATE_OPENED) {
654 if (sl_uncompress_tcp_core(mtod(m, u_char *),
656 TYPE_UNCOMPRESSED_TCP,
658 &iphdr, &hlen) != 0) {
661 SPP_FMT "VJ uncompress failed on uncompressed packet\n",
672 if (sp->pp_phase == PHASE_NETWORK)
673 sppp_cp_input(&ipv6cp, sp, m);
679 if (sp->state[IDX_IPV6CP] == STATE_OPENED)
686 /* IPX IPXCP not implemented yet */
687 if (sp->pp_phase == PHASE_NETWORK)
694 case CISCO_MULTICAST:
696 /* Don't check the control field here (RFC 1547). */
697 if (sp->pp_mode != IFF_CISCO) {
700 SPP_FMT "Cisco packet in PPP mode "
701 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
703 h->address, h->control, ntohs(h->protocol));
706 switch (ntohs (h->protocol)) {
710 case CISCO_KEEPALIVE:
711 sppp_cisco_input ((struct sppp*) ifp, m);
735 default: /* Invalid PPP packet. */
739 SPP_FMT "invalid input packet "
740 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
742 h->address, h->control, ntohs(h->protocol));
746 if (! (ifp->if_flags & IFF_UP) || isr == -1)
751 if (netisr_queue(isr, m)) { /* (0) on success. */
753 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
760 * Do only account for network packets, not for control
761 * packets. This is used by some subsystems to detect
764 sp->pp_last_recv = time_second;
768 sppp_ifstart_sched(void *dummy)
770 struct sppp *sp = dummy;
772 sp->if_start(&sp->pp_if);
775 /* if_start () wrapper function. We use it to schedule real if_start () for
776 * execution. We can't call it directly
779 sppp_ifstart(struct ifnet *ifp)
781 struct sppp *sp = (struct sppp*) ifp;
783 if (SPPP_LOCK_OWNED(sp)) {
784 if (callout_pending(&sp->ifstart_callout))
786 callout_reset(&sp->ifstart_callout, 1, sppp_ifstart_sched,
794 * Enqueue transmit packet.
797 sppp_output(struct ifnet *ifp, struct mbuf *m,
798 struct sockaddr *dst, struct rtentry *rt)
800 struct sppp *sp = (struct sppp*) ifp;
801 struct ppp_header *h;
802 struct ifqueue *ifq = NULL;
803 int s, error, rv = 0;
804 int ipproto = PPP_IP;
805 int debug = ifp->if_flags & IFF_DEBUG;
810 if ((ifp->if_flags & IFF_UP) == 0 ||
811 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
821 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
826 * Hack to prevent the initialization-time generated
827 * IPv6 multicast packet to erroneously cause a
828 * dialout event in case IPv6 has been
829 * administratively disabled on that interface.
831 if (dst->sa_family == AF_INET6 &&
832 !(sp->confflags & CONF_ENABLE_IPV6))
836 * Interface is not yet running, but auto-dial. Need
837 * to start LCP for it.
839 ifp->if_flags |= IFF_RUNNING;
846 if (dst->sa_family == AF_INET) {
847 /* XXX Check mbuf length here? */
848 struct ip *ip = mtod (m, struct ip*);
849 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
852 * When using dynamic local IP address assignment by using
853 * 0.0.0.0 as a local address, the first TCP session will
854 * not connect because the local TCP checksum is computed
855 * using 0.0.0.0 which will later become our real IP address
856 * so the TCP checksum computed at the remote end will
857 * become invalid. So we
858 * - don't let packets with src ip addr 0 thru
859 * - we flag TCP packets with src ip 0 as an error
862 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
867 if(ip->ip_p == IPPROTO_TCP)
868 return(EADDRNOTAVAIL);
874 * Put low delay, telnet, rlogin and ftp control packets
875 * in front of the queue or let ALTQ take care.
877 if (ALTQ_IS_ENABLED(&ifp->if_snd))
879 else if (_IF_QFULL(&sp->pp_fastq))
881 else if (ip->ip_tos & IPTOS_LOWDELAY)
883 else if (m->m_len < sizeof *ip + sizeof *tcp)
885 else if (ip->ip_p != IPPROTO_TCP)
887 else if (INTERACTIVE (ntohs (tcp->th_sport)))
889 else if (INTERACTIVE (ntohs (tcp->th_dport)))
893 * Do IP Header compression
895 if (sp->pp_mode != IFF_CISCO && sp->pp_mode != PP_FR &&
896 (sp->ipcp.flags & IPCP_VJ) && ip->ip_p == IPPROTO_TCP)
897 switch (sl_compress_tcp(m, ip, sp->pp_comp,
898 sp->ipcp.compress_cid)) {
899 case TYPE_COMPRESSED_TCP:
900 ipproto = PPP_VJ_COMP;
902 case TYPE_UNCOMPRESSED_TCP:
903 ipproto = PPP_VJ_UCOMP;
918 if (dst->sa_family == AF_INET6) {
919 /* XXX do something tricky here? */
923 if (sp->pp_mode == PP_FR) {
924 /* Add frame relay header. */
925 m = sppp_fr_header (sp, m, dst->sa_family);
932 * Prepend general data packet PPP header. For now, IP only.
934 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
937 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
945 * May want to check size of packet
946 * (albeit due to the implementation it's always enough)
948 h = mtod (m, struct ppp_header*);
949 if (sp->pp_mode == IFF_CISCO) {
950 h->address = CISCO_UNICAST; /* unicast address */
953 h->address = PPP_ALLSTATIONS; /* broadcast address */
954 h->control = PPP_UI; /* Unnumbered Info */
957 switch (dst->sa_family) {
959 case AF_INET: /* Internet Protocol */
960 if (sp->pp_mode == IFF_CISCO)
961 h->protocol = htons (ETHERTYPE_IP);
964 * Don't choke with an ENETDOWN early. It's
965 * possible that we just started dialing out,
966 * so don't drop the packet immediately. If
967 * we notice that we run out of buffer space
968 * below, we will however remember that we are
969 * not ready to carry IP packets, and return
970 * ENETDOWN, as opposed to ENOBUFS.
972 h->protocol = htons(ipproto);
973 if (sp->state[IDX_IPCP] != STATE_OPENED)
979 case AF_INET6: /* Internet Protocol */
980 if (sp->pp_mode == IFF_CISCO)
981 h->protocol = htons (ETHERTYPE_IPV6);
984 * Don't choke with an ENETDOWN early. It's
985 * possible that we just started dialing out,
986 * so don't drop the packet immediately. If
987 * we notice that we run out of buffer space
988 * below, we will however remember that we are
989 * not ready to carry IP packets, and return
990 * ENETDOWN, as opposed to ENOBUFS.
992 h->protocol = htons(PPP_IPV6);
993 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
999 case AF_IPX: /* Novell IPX Protocol */
1000 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
1001 ETHERTYPE_IPX : PPP_IPX);
1009 return (EAFNOSUPPORT);
1013 * Queue message on interface, and start output if interface
1018 error = !(IF_HANDOFF_ADJ(ifq, m, ifp, 3));
1020 IFQ_HANDOFF_ADJ(ifp, m, 3, error);
1025 return (rv? rv: ENOBUFS);
1030 * Unlike in sppp_input(), we can always bump the timestamp
1031 * here since sppp_output() is only called on behalf of
1032 * network-layer traffic; control-layer traffic is handled
1033 * by sppp_cp_send().
1035 sp->pp_last_sent = time_second;
1040 sppp_attach(struct ifnet *ifp)
1042 struct sppp *sp = (struct sppp*) ifp;
1044 /* Initialize mtx lock */
1045 mtx_init(&sp->mtx, "sppp", MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
1047 /* Initialize keepalive handler. */
1048 callout_init(&sp->keepalive_callout,
1049 (ifp->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
1050 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
1053 sp->pp_if.if_mtu = PP_MTU;
1054 sp->pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
1055 sp->pp_if.if_type = IFT_PPP;
1056 sp->pp_if.if_output = sppp_output;
1058 sp->pp_flags = PP_KEEPALIVE;
1060 sp->pp_if.if_snd.ifq_maxlen = 32;
1061 sp->pp_fastq.ifq_maxlen = 32;
1062 sp->pp_cpq.ifq_maxlen = 20;
1064 sp->pp_alivecnt = 0;
1065 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
1066 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
1067 sp->pp_phase = PHASE_DEAD;
1068 sp->pp_up = sppp_pp_up;
1069 sp->pp_down = sppp_pp_down;
1070 if(!mtx_initialized(&sp->pp_cpq.ifq_mtx))
1071 mtx_init(&sp->pp_cpq.ifq_mtx, "sppp_cpq", NULL, MTX_DEF);
1072 if(!mtx_initialized(&sp->pp_fastq.ifq_mtx))
1073 mtx_init(&sp->pp_fastq.ifq_mtx, "sppp_fastq", NULL, MTX_DEF);
1074 sp->pp_last_recv = sp->pp_last_sent = time_second;
1077 sp->confflags |= CONF_ENABLE_VJ;
1080 sp->confflags |= CONF_ENABLE_IPV6;
1082 callout_init(&sp->ifstart_callout,
1083 (ifp->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
1084 sp->if_start = ifp->if_start;
1085 ifp->if_start = sppp_ifstart;
1086 sp->pp_comp = malloc(sizeof(struct slcompress), M_TEMP, M_WAITOK);
1087 sl_compress_init(sp->pp_comp, -1);
1090 sppp_ipv6cp_init(sp);
1096 sppp_detach(struct ifnet *ifp)
1098 struct sppp *sp = (struct sppp*) ifp;
1101 KASSERT(mtx_initialized(&sp->mtx), ("sppp mutex is not initialized"));
1103 /* Stop keepalive handler. */
1104 if (!callout_drain(&sp->keepalive_callout))
1105 callout_stop(&sp->keepalive_callout);
1107 for (i = 0; i < IDX_COUNT; i++) {
1108 if (!callout_drain(&sp->ch[i]))
1109 callout_stop(&sp->ch[i]);
1111 if (!callout_drain(&sp->pap_my_to_ch))
1112 callout_stop(&sp->pap_my_to_ch);
1113 mtx_destroy(&sp->pp_cpq.ifq_mtx);
1114 mtx_destroy(&sp->pp_fastq.ifq_mtx);
1115 mtx_destroy(&sp->mtx);
1119 * Flush the interface output queue.
1122 sppp_flush_unlocked(struct ifnet *ifp)
1124 struct sppp *sp = (struct sppp*) ifp;
1126 sppp_qflush ((struct ifqueue *)&sp->pp_if.if_snd);
1127 sppp_qflush (&sp->pp_fastq);
1128 sppp_qflush (&sp->pp_cpq);
1132 sppp_flush(struct ifnet *ifp)
1134 struct sppp *sp = (struct sppp*) ifp;
1137 sppp_flush_unlocked (ifp);
1142 * Check if the output queue is empty.
1145 sppp_isempty(struct ifnet *ifp)
1147 struct sppp *sp = (struct sppp*) ifp;
1152 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
1153 !sp->pp_if.if_snd.ifq_head;
1160 * Get next packet to send.
1163 sppp_dequeue(struct ifnet *ifp)
1165 struct sppp *sp = (struct sppp*) ifp;
1172 * Process only the control protocol queue until we have at
1173 * least one NCP open.
1175 * Do always serve all three queues in Cisco mode.
1177 IF_DEQUEUE(&sp->pp_cpq, m);
1179 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO ||
1180 sp->pp_mode == PP_FR)) {
1181 IF_DEQUEUE(&sp->pp_fastq, m);
1183 IF_DEQUEUE (&sp->pp_if.if_snd, m);
1191 * Pick the next packet, do not remove it from the queue.
1194 sppp_pick(struct ifnet *ifp)
1196 struct sppp *sp = (struct sppp*)ifp;
1203 m = sp->pp_cpq.ifq_head;
1205 (sp->pp_phase == PHASE_NETWORK ||
1206 sp->pp_mode == IFF_CISCO ||
1207 sp->pp_mode == PP_FR))
1208 if ((m = sp->pp_fastq.ifq_head) == NULL)
1209 m = sp->pp_if.if_snd.ifq_head;
1216 * Process an ioctl request. Called on low priority level.
1219 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1221 struct ifreq *ifr = (struct ifreq*) data;
1222 struct sppp *sp = (struct sppp*) ifp;
1223 int s, rv, going_up, going_down, newmode;
1230 case SIOCSIFDSTADDR:
1234 /* set the interface "up" when assigning an IP address */
1235 ifp->if_flags |= IFF_UP;
1239 going_up = ifp->if_flags & IFF_UP &&
1240 (ifp->if_flags & IFF_RUNNING) == 0;
1241 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1242 ifp->if_flags & IFF_RUNNING;
1244 newmode = ifp->if_flags & IFF_PASSIVE;
1246 newmode = ifp->if_flags & IFF_AUTO;
1248 newmode = ifp->if_flags & IFF_CISCO;
1249 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1250 ifp->if_flags |= newmode;
1253 newmode = sp->pp_flags & PP_FR;
1255 if (newmode != sp->pp_mode) {
1258 going_up = ifp->if_flags & IFF_RUNNING;
1262 if (sp->pp_mode != IFF_CISCO &&
1263 sp->pp_mode != PP_FR)
1265 else if (sp->pp_tlf)
1267 sppp_flush_unlocked(ifp);
1268 ifp->if_flags &= ~IFF_RUNNING;
1269 sp->pp_mode = newmode;
1273 if (sp->pp_mode != IFF_CISCO &&
1274 sp->pp_mode != PP_FR)
1276 sp->pp_mode = newmode;
1277 if (sp->pp_mode == 0) {
1278 ifp->if_flags |= IFF_RUNNING;
1281 if ((sp->pp_mode == IFF_CISCO) ||
1282 (sp->pp_mode == PP_FR)) {
1285 ifp->if_flags |= IFF_RUNNING;
1293 #define ifr_mtu ifr_metric
1296 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1298 ifp->if_mtu = ifr->ifr_mtu;
1303 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1305 ifp->if_mtu = *(short*)data;
1310 ifr->ifr_mtu = ifp->if_mtu;
1315 *(short*)data = ifp->if_mtu;
1322 case SIOCGIFGENERIC:
1323 case SIOCSIFGENERIC:
1324 rv = sppp_params(sp, cmd, data);
1336 * Cisco framing implementation.
1340 * Handle incoming Cisco keepalive protocol packets.
1343 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1346 struct cisco_packet *h;
1349 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1352 SPP_FMT "cisco invalid packet length: %d bytes\n",
1353 SPP_ARGS(ifp), m->m_pkthdr.len);
1356 h = mtod (m, struct cisco_packet*);
1359 SPP_FMT "cisco input: %d bytes "
1360 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1361 SPP_ARGS(ifp), m->m_pkthdr.len,
1362 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1363 (u_int)h->time0, (u_int)h->time1);
1364 switch (ntohl (h->type)) {
1367 log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n",
1368 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1370 case CISCO_ADDR_REPLY:
1371 /* Reply on address request, ignore */
1373 case CISCO_KEEPALIVE_REQ:
1374 sp->pp_alivecnt = 0;
1375 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1376 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1377 /* Local and remote sequence numbers are equal.
1378 * Probably, the line is in loopback mode. */
1379 if (sp->pp_loopcnt >= MAXALIVECNT) {
1380 printf (SPP_FMT "loopback\n",
1383 if (ifp->if_flags & IFF_UP) {
1385 sppp_qflush (&sp->pp_cpq);
1390 /* Generate new local sequence number */
1391 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1392 sp->pp_seq[IDX_LCP] = random();
1394 sp->pp_seq[IDX_LCP] ^= time.tv_sec ^ time.tv_usec;
1399 if (! (ifp->if_flags & IFF_UP) &&
1400 (ifp->if_flags & IFF_RUNNING)) {
1402 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1405 case CISCO_ADDR_REQ:
1406 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1408 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1414 * Send Cisco keepalive packet.
1417 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1420 struct ppp_header *h;
1421 struct cisco_packet *ch;
1425 getmicrouptime(&tv);
1427 MGETHDR (m, M_DONTWAIT, MT_DATA);
1430 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1431 m->m_pkthdr.rcvif = 0;
1433 h = mtod (m, struct ppp_header*);
1434 h->address = CISCO_MULTICAST;
1436 h->protocol = htons (CISCO_KEEPALIVE);
1438 ch = (struct cisco_packet*) (h + 1);
1439 ch->type = htonl (type);
1440 ch->par1 = htonl (par1);
1441 ch->par2 = htonl (par2);
1444 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1445 ch->time1 = htons ((u_short) tv.tv_sec);
1449 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1450 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1451 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1453 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1458 * PPP protocol implementation.
1462 * Send PPP control protocol packet.
1465 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1466 u_char ident, u_short len, void *data)
1469 struct ppp_header *h;
1470 struct lcp_header *lh;
1473 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1474 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1475 MGETHDR (m, M_DONTWAIT, MT_DATA);
1478 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1479 m->m_pkthdr.rcvif = 0;
1481 h = mtod (m, struct ppp_header*);
1482 h->address = PPP_ALLSTATIONS; /* broadcast address */
1483 h->control = PPP_UI; /* Unnumbered Info */
1484 h->protocol = htons (proto); /* Link Control Protocol */
1486 lh = (struct lcp_header*) (h + 1);
1489 lh->len = htons (LCP_HEADER_LEN + len);
1491 bcopy (data, lh+1, len);
1494 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1496 sppp_proto_name(proto),
1497 sppp_cp_type_name (lh->type), lh->ident,
1499 sppp_print_bytes ((u_char*) (lh+1), len);
1502 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1507 * Handle incoming PPP control protocol packets.
1510 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1513 struct lcp_header *h;
1514 int len = m->m_pkthdr.len;
1521 SPP_FMT "%s invalid packet length: %d bytes\n",
1522 SPP_ARGS(ifp), cp->name, len);
1525 h = mtod (m, struct lcp_header*);
1528 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1529 SPP_ARGS(ifp), cp->name,
1530 sppp_state_name(sp->state[cp->protoidx]),
1531 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1532 sppp_print_bytes ((u_char*) (h+1), len-4);
1535 if (len > ntohs (h->len))
1536 len = ntohs (h->len);
1537 p = (u_char *)(h + 1);
1542 log(-1, SPP_FMT "%s invalid conf-req length %d\n",
1543 SPP_ARGS(ifp), cp->name,
1548 /* handle states where RCR doesn't get a SCA/SCN */
1549 switch (sp->state[cp->protoidx]) {
1551 case STATE_STOPPING:
1554 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1558 rv = (cp->RCR)(sp, h, len);
1559 switch (sp->state[cp->protoidx]) {
1564 case STATE_ACK_SENT:
1565 case STATE_REQ_SENT:
1567 * sppp_cp_change_state() have the side effect of
1568 * restarting the timeouts. We want to avoid that
1569 * if the state don't change, otherwise we won't
1570 * ever timeout and resend a configuration request
1573 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1576 sppp_cp_change_state(cp, sp, rv?
1577 STATE_ACK_SENT: STATE_REQ_SENT);
1580 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1582 sppp_cp_change_state(cp, sp, rv?
1583 STATE_ACK_SENT: STATE_REQ_SENT);
1585 case STATE_ACK_RCVD:
1587 sppp_cp_change_state(cp, sp, STATE_OPENED);
1589 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1594 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1597 printf(SPP_FMT "%s illegal %s in state %s\n",
1598 SPP_ARGS(ifp), cp->name,
1599 sppp_cp_type_name(h->type),
1600 sppp_state_name(sp->state[cp->protoidx]));
1605 if (h->ident != sp->confid[cp->protoidx]) {
1607 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1608 SPP_ARGS(ifp), cp->name,
1609 h->ident, sp->confid[cp->protoidx]);
1613 switch (sp->state[cp->protoidx]) {
1616 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1619 case STATE_STOPPING:
1621 case STATE_REQ_SENT:
1622 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1623 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1628 case STATE_ACK_RCVD:
1630 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1632 case STATE_ACK_SENT:
1633 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1634 sppp_cp_change_state(cp, sp, STATE_OPENED);
1636 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1637 SPP_ARGS(ifp), cp->name);
1641 printf(SPP_FMT "%s illegal %s in state %s\n",
1642 SPP_ARGS(ifp), cp->name,
1643 sppp_cp_type_name(h->type),
1644 sppp_state_name(sp->state[cp->protoidx]));
1650 if (h->ident != sp->confid[cp->protoidx]) {
1652 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1653 SPP_ARGS(ifp), cp->name,
1654 h->ident, sp->confid[cp->protoidx]);
1658 if (h->type == CONF_NAK)
1659 (cp->RCN_nak)(sp, h, len);
1661 (cp->RCN_rej)(sp, h, len);
1663 switch (sp->state[cp->protoidx]) {
1666 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1668 case STATE_REQ_SENT:
1669 case STATE_ACK_SENT:
1670 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1672 * Slow things down a bit if we think we might be
1673 * in loopback. Depend on the timeout to send the
1674 * next configuration request.
1683 case STATE_ACK_RCVD:
1684 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1688 case STATE_STOPPING:
1691 printf(SPP_FMT "%s illegal %s in state %s\n",
1692 SPP_ARGS(ifp), cp->name,
1693 sppp_cp_type_name(h->type),
1694 sppp_state_name(sp->state[cp->protoidx]));
1700 switch (sp->state[cp->protoidx]) {
1701 case STATE_ACK_RCVD:
1702 case STATE_ACK_SENT:
1703 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1708 case STATE_STOPPING:
1709 case STATE_REQ_SENT:
1711 /* Send Terminate-Ack packet. */
1713 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1714 SPP_ARGS(ifp), cp->name);
1715 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1719 sp->rst_counter[cp->protoidx] = 0;
1720 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1724 printf(SPP_FMT "%s illegal %s in state %s\n",
1725 SPP_ARGS(ifp), cp->name,
1726 sppp_cp_type_name(h->type),
1727 sppp_state_name(sp->state[cp->protoidx]));
1732 switch (sp->state[cp->protoidx]) {
1735 case STATE_REQ_SENT:
1736 case STATE_ACK_SENT:
1739 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1742 case STATE_STOPPING:
1743 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1746 case STATE_ACK_RCVD:
1747 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1752 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1755 printf(SPP_FMT "%s illegal %s in state %s\n",
1756 SPP_ARGS(ifp), cp->name,
1757 sppp_cp_type_name(h->type),
1758 sppp_state_name(sp->state[cp->protoidx]));
1763 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1765 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1766 "danger will robinson\n",
1767 SPP_ARGS(ifp), cp->name,
1768 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1769 switch (sp->state[cp->protoidx]) {
1772 case STATE_REQ_SENT:
1773 case STATE_ACK_SENT:
1775 case STATE_STOPPING:
1778 case STATE_ACK_RCVD:
1779 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1782 printf(SPP_FMT "%s illegal %s in state %s\n",
1783 SPP_ARGS(ifp), cp->name,
1784 sppp_cp_type_name(h->type),
1785 sppp_state_name(sp->state[cp->protoidx]));
1792 const struct cp *upper;
1798 proto = ntohs(*((u_int16_t *)p));
1799 for (i = 0; i < IDX_COUNT; i++) {
1800 if (cps[i]->proto == proto) {
1808 if (catastrophic || debug)
1809 log(catastrophic? LOG_INFO: LOG_DEBUG,
1810 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1811 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1812 sppp_cp_type_name(h->type), proto,
1813 upper ? upper->name : "unknown",
1814 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1817 * if we got RXJ+ against conf-req, the peer does not implement
1818 * this particular protocol type. terminate the protocol.
1820 if (upper && !catastrophic) {
1821 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1827 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1828 switch (sp->state[cp->protoidx]) {
1831 case STATE_REQ_SENT:
1832 case STATE_ACK_SENT:
1834 case STATE_STOPPING:
1837 case STATE_ACK_RCVD:
1838 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1841 printf(SPP_FMT "%s illegal %s in state %s\n",
1842 SPP_ARGS(ifp), cp->name,
1843 sppp_cp_type_name(h->type),
1844 sppp_state_name(sp->state[cp->protoidx]));
1850 if (cp->proto != PPP_LCP)
1852 /* Discard the packet. */
1855 if (cp->proto != PPP_LCP)
1857 if (sp->state[cp->protoidx] != STATE_OPENED) {
1859 log(-1, SPP_FMT "lcp echo req but lcp closed\n",
1866 log(-1, SPP_FMT "invalid lcp echo request "
1867 "packet length: %d bytes\n",
1868 SPP_ARGS(ifp), len);
1871 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1872 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1873 /* Line loopback mode detected. */
1874 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1875 sp->pp_loopcnt = MAXALIVECNT * 5;
1877 sppp_qflush (&sp->pp_cpq);
1879 /* Shut down the PPP link. */
1885 *(long*)(h+1) = htonl (sp->lcp.magic);
1887 log(-1, SPP_FMT "got lcp echo req, sending echo rep\n",
1889 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1892 if (cp->proto != PPP_LCP)
1894 if (h->ident != sp->lcp.echoid) {
1900 log(-1, SPP_FMT "lcp invalid echo reply "
1901 "packet length: %d bytes\n",
1902 SPP_ARGS(ifp), len);
1906 log(-1, SPP_FMT "lcp got echo rep\n",
1908 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1909 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1910 sp->pp_alivecnt = 0;
1913 /* Unknown packet type -- send Code-Reject packet. */
1916 log(-1, SPP_FMT "%s send code-rej for 0x%x\n",
1917 SPP_ARGS(ifp), cp->name, h->type);
1918 sppp_cp_send(sp, cp->proto, CODE_REJ,
1919 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1926 * The generic part of all Up/Down/Open/Close/TO event handlers.
1927 * Basically, the state transition handling in the automaton.
1930 sppp_up_event(const struct cp *cp, struct sppp *sp)
1935 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1936 SPP_ARGS(ifp), cp->name,
1937 sppp_state_name(sp->state[cp->protoidx]));
1939 switch (sp->state[cp->protoidx]) {
1941 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1943 case STATE_STARTING:
1944 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1946 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1949 printf(SPP_FMT "%s illegal up in state %s\n",
1950 SPP_ARGS(ifp), cp->name,
1951 sppp_state_name(sp->state[cp->protoidx]));
1956 sppp_down_event(const struct cp *cp, struct sppp *sp)
1961 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1962 SPP_ARGS(ifp), cp->name,
1963 sppp_state_name(sp->state[cp->protoidx]));
1965 switch (sp->state[cp->protoidx]) {
1968 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1971 sppp_cp_change_state(cp, sp, STATE_STARTING);
1974 case STATE_STOPPING:
1975 case STATE_REQ_SENT:
1976 case STATE_ACK_RCVD:
1977 case STATE_ACK_SENT:
1978 sppp_cp_change_state(cp, sp, STATE_STARTING);
1982 sppp_cp_change_state(cp, sp, STATE_STARTING);
1985 printf(SPP_FMT "%s illegal down in state %s\n",
1986 SPP_ARGS(ifp), cp->name,
1987 sppp_state_name(sp->state[cp->protoidx]));
1993 sppp_open_event(const struct cp *cp, struct sppp *sp)
1998 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1999 SPP_ARGS(ifp), cp->name,
2000 sppp_state_name(sp->state[cp->protoidx]));
2002 switch (sp->state[cp->protoidx]) {
2004 sppp_cp_change_state(cp, sp, STATE_STARTING);
2007 case STATE_STARTING:
2010 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
2012 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2016 * Try escaping stopped state. This seems to bite
2017 * people occasionally, in particular for IPCP,
2018 * presumably following previous IPCP negotiation
2019 * aborts. Somehow, we must have missed a Down event
2020 * which would have caused a transition into starting
2021 * state, so as a bandaid we force the Down event now.
2022 * This effectively implements (something like the)
2023 * `restart' option mentioned in the state transition
2024 * table of RFC 1661.
2026 sppp_cp_change_state(cp, sp, STATE_STARTING);
2029 case STATE_STOPPING:
2030 case STATE_REQ_SENT:
2031 case STATE_ACK_RCVD:
2032 case STATE_ACK_SENT:
2036 sppp_cp_change_state(cp, sp, STATE_STOPPING);
2043 sppp_close_event(const struct cp *cp, struct sppp *sp)
2048 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
2049 SPP_ARGS(ifp), cp->name,
2050 sppp_state_name(sp->state[cp->protoidx]));
2052 switch (sp->state[cp->protoidx]) {
2057 case STATE_STARTING:
2058 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2062 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2064 case STATE_STOPPING:
2065 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2070 case STATE_REQ_SENT:
2071 case STATE_ACK_RCVD:
2072 case STATE_ACK_SENT:
2073 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2074 sppp_cp_send(sp, cp->proto, TERM_REQ,
2075 ++sp->pp_seq[cp->protoidx], 0, 0);
2076 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2082 sppp_to_event(const struct cp *cp, struct sppp *sp)
2090 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
2091 SPP_ARGS(ifp), cp->name,
2092 sppp_state_name(sp->state[cp->protoidx]),
2093 sp->rst_counter[cp->protoidx]);
2095 if (--sp->rst_counter[cp->protoidx] < 0)
2097 switch (sp->state[cp->protoidx]) {
2099 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2102 case STATE_STOPPING:
2103 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2106 case STATE_REQ_SENT:
2107 case STATE_ACK_RCVD:
2108 case STATE_ACK_SENT:
2109 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2115 switch (sp->state[cp->protoidx]) {
2117 case STATE_STOPPING:
2118 sppp_cp_send(sp, cp->proto, TERM_REQ,
2119 ++sp->pp_seq[cp->protoidx], 0, 0);
2120 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2121 cp->TO, (void *)sp);
2123 case STATE_REQ_SENT:
2124 case STATE_ACK_RCVD:
2126 /* sppp_cp_change_state() will restart the timer */
2127 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2129 case STATE_ACK_SENT:
2131 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2132 cp->TO, (void *)sp);
2141 * Change the state of a control protocol in the state automaton.
2142 * Takes care of starting/stopping the restart timer.
2145 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2147 sp->state[cp->protoidx] = newstate;
2149 callout_stop (&sp->ch[cp->protoidx]);
2153 case STATE_STARTING:
2159 case STATE_STOPPING:
2160 case STATE_REQ_SENT:
2161 case STATE_ACK_RCVD:
2162 case STATE_ACK_SENT:
2163 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2164 cp->TO, (void *)sp);
2170 *--------------------------------------------------------------------------*
2172 * The LCP implementation. *
2174 *--------------------------------------------------------------------------*
2177 sppp_pp_up(struct sppp *sp)
2185 sppp_pp_down(struct sppp *sp)
2193 sppp_lcp_init(struct sppp *sp)
2195 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2197 sp->state[IDX_LCP] = STATE_INITIAL;
2198 sp->fail_counter[IDX_LCP] = 0;
2199 sp->pp_seq[IDX_LCP] = 0;
2200 sp->pp_rseq[IDX_LCP] = 0;
2202 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2204 /* Note that these values are relevant for all control protocols */
2205 sp->lcp.timeout = 3 * hz;
2206 sp->lcp.max_terminate = 2;
2207 sp->lcp.max_configure = 10;
2208 sp->lcp.max_failure = 10;
2209 callout_init(&sp->ch[IDX_LCP],
2210 (sp->pp_if.if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
2214 sppp_lcp_up(struct sppp *sp)
2218 sp->pp_alivecnt = 0;
2219 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2222 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2224 * If we are authenticator, negotiate LCP_AUTH
2226 if (sp->hisauth.proto != 0)
2227 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2229 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2230 sp->pp_flags &= ~PP_NEEDAUTH;
2232 * If this interface is passive or dial-on-demand, and we are
2233 * still in Initial state, it means we've got an incoming
2234 * call. Activate the interface.
2236 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2239 SPP_FMT "Up event", SPP_ARGS(ifp));
2240 ifp->if_flags |= IFF_RUNNING;
2241 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2243 log(-1, "(incoming call)\n");
2244 sp->pp_flags |= PP_CALLIN;
2248 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2249 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2250 ifp->if_flags |= IFF_RUNNING;
2254 sppp_up_event(&lcp, sp);
2258 sppp_lcp_down(struct sppp *sp)
2262 sppp_down_event(&lcp, sp);
2265 * If this is neither a dial-on-demand nor a passive
2266 * interface, simulate an ``ifconfig down'' action, so the
2267 * administrator can force a redial by another ``ifconfig
2268 * up''. XXX For leased line operation, should we immediately
2269 * try to reopen the connection here?
2271 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2273 SPP_FMT "Down event, taking interface down.\n",
2279 SPP_FMT "Down event (carrier loss)\n",
2281 sp->pp_flags &= ~PP_CALLIN;
2282 if (sp->state[IDX_LCP] != STATE_INITIAL)
2284 ifp->if_flags &= ~IFF_RUNNING;
2289 sppp_lcp_open(struct sppp *sp)
2291 sppp_open_event(&lcp, sp);
2295 sppp_lcp_close(struct sppp *sp)
2297 sppp_close_event(&lcp, sp);
2301 sppp_lcp_TO(void *cookie)
2303 sppp_to_event(&lcp, (struct sppp *)cookie);
2307 * Analyze a configure request. Return true if it was agreeable, and
2308 * caused action sca, false if it has been rejected or nak'ed, and
2309 * caused action scn. (The return value is used to make the state
2310 * transition decision in the state automaton.)
2313 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2316 u_char *buf, *r, *p;
2323 buf = r = malloc (len, M_TEMP, M_NOWAIT);
2328 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2331 /* pass 1: check for things that need to be rejected */
2333 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2335 log(-1, " %s ", sppp_lcp_opt_name(*p));
2339 if (len >= 6 && p[1] == 6)
2342 log(-1, "[invalid] ");
2344 case LCP_OPT_ASYNC_MAP:
2345 /* Async control character map. */
2346 if (len >= 6 && p[1] == 6)
2349 log(-1, "[invalid] ");
2352 /* Maximum receive unit. */
2353 if (len >= 4 && p[1] == 4)
2356 log(-1, "[invalid] ");
2358 case LCP_OPT_AUTH_PROTO:
2361 log(-1, "[invalid] ");
2364 authproto = (p[2] << 8) + p[3];
2365 if (authproto == PPP_CHAP && p[1] != 5) {
2367 log(-1, "[invalid chap len] ");
2370 if (sp->myauth.proto == 0) {
2371 /* we are not configured to do auth */
2373 log(-1, "[not configured] ");
2377 * Remote want us to authenticate, remember this,
2378 * so we stay in PHASE_AUTHENTICATE after LCP got
2381 sp->pp_flags |= PP_NEEDAUTH;
2384 /* Others not supported. */
2389 /* Add the option to rejected list. */
2396 log(-1, " send conf-rej\n");
2397 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2403 * pass 2: check for option values that are unacceptable and
2404 * thus require to be nak'ed.
2407 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2412 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2414 log(-1, " %s ", sppp_lcp_opt_name(*p));
2417 /* Magic number -- extract. */
2418 nmagic = (u_long)p[2] << 24 |
2419 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2420 if (nmagic != sp->lcp.magic) {
2423 log(-1, "0x%lx ", nmagic);
2426 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2427 log(-1, "[glitch] ");
2430 * We negate our magic here, and NAK it. If
2431 * we see it later in an NAK packet, we
2432 * suggest a new one.
2434 nmagic = ~sp->lcp.magic;
2436 p[2] = nmagic >> 24;
2437 p[3] = nmagic >> 16;
2442 case LCP_OPT_ASYNC_MAP:
2444 * Async control character map -- just ignore it.
2446 * Quote from RFC 1662, chapter 6:
2447 * To enable this functionality, synchronous PPP
2448 * implementations MUST always respond to the
2449 * Async-Control-Character-Map Configuration
2450 * Option with the LCP Configure-Ack. However,
2451 * acceptance of the Configuration Option does
2452 * not imply that the synchronous implementation
2453 * will do any ACCM mapping. Instead, all such
2454 * octet mapping will be performed by the
2455 * asynchronous-to-synchronous converter.
2461 * Maximum receive unit. Always agreeable,
2462 * but ignored by now.
2464 sp->lcp.their_mru = p[2] * 256 + p[3];
2466 log(-1, "%lu ", sp->lcp.their_mru);
2469 case LCP_OPT_AUTH_PROTO:
2470 authproto = (p[2] << 8) + p[3];
2471 if (sp->myauth.proto != authproto) {
2472 /* not agreed, nak */
2474 log(-1, "[mine %s != his %s] ",
2475 sppp_proto_name(sp->hisauth.proto),
2476 sppp_proto_name(authproto));
2477 p[2] = sp->myauth.proto >> 8;
2478 p[3] = sp->myauth.proto;
2481 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2483 log(-1, "[chap not MD5] ");
2489 /* Add the option to nak'ed list. */
2496 * Local and remote magics equal -- loopback?
2498 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2499 if (sp->pp_loopcnt == MAXALIVECNT*5)
2500 printf (SPP_FMT "loopback\n",
2502 if (ifp->if_flags & IFF_UP) {
2504 sppp_qflush(&sp->pp_cpq);
2509 } else if (!sp->pp_loopcnt &&
2510 ++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2512 log(-1, " max_failure (%d) exceeded, "
2514 sp->lcp.max_failure);
2515 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2518 log(-1, " send conf-nak\n");
2519 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2523 log(-1, " send conf-ack\n");
2524 sp->fail_counter[IDX_LCP] = 0;
2526 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2527 h->ident, origlen, h+1);
2535 * Analyze the LCP Configure-Reject option list, and adjust our
2539 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2545 buf = malloc (len, M_TEMP, M_NOWAIT);
2550 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2554 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2556 log(-1, " %s ", sppp_lcp_opt_name(*p));
2559 /* Magic number -- can't use it, use 0 */
2560 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2565 * Should not be rejected anyway, since we only
2566 * negotiate a MRU if explicitly requested by
2569 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2571 case LCP_OPT_AUTH_PROTO:
2573 * Peer doesn't want to authenticate himself,
2574 * deny unless this is a dialout call, and
2575 * AUTHFLAG_NOCALLOUT is set.
2577 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2578 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2580 log(-1, "[don't insist on auth "
2582 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2586 log(-1, "[access denied]\n");
2598 * Analyze the LCP Configure-NAK option list, and adjust our
2602 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2609 buf = malloc (len, M_TEMP, M_NOWAIT);
2614 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2618 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2620 log(-1, " %s ", sppp_lcp_opt_name(*p));
2623 /* Magic number -- renegotiate */
2624 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2625 len >= 6 && p[1] == 6) {
2626 magic = (u_long)p[2] << 24 |
2627 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2629 * If the remote magic is our negated one,
2630 * this looks like a loopback problem.
2631 * Suggest a new magic to make sure.
2633 if (magic == ~sp->lcp.magic) {
2635 log(-1, "magic glitch ");
2636 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2637 sp->lcp.magic = random();
2639 sp->lcp.magic = time.tv_sec + time.tv_usec;
2642 sp->lcp.magic = magic;
2644 log(-1, "%lu ", magic);
2650 * Peer wants to advise us to negotiate an MRU.
2651 * Agree on it if it's reasonable, or use
2652 * default otherwise.
2654 if (len >= 4 && p[1] == 4) {
2655 u_int mru = p[2] * 256 + p[3];
2657 log(-1, "%d ", mru);
2658 if (mru < PP_MTU || mru > PP_MAX_MRU)
2661 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2664 case LCP_OPT_AUTH_PROTO:
2666 * Peer doesn't like our authentication method,
2670 log(-1, "[access denied]\n");
2682 sppp_lcp_tlu(struct sppp *sp)
2689 if (! (ifp->if_flags & IFF_UP) &&
2690 (ifp->if_flags & IFF_RUNNING)) {
2691 /* Coming out of loopback mode. */
2693 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2696 for (i = 0; i < IDX_COUNT; i++)
2697 if ((cps[i])->flags & CP_QUAL)
2700 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2701 (sp->pp_flags & PP_NEEDAUTH) != 0)
2702 sp->pp_phase = PHASE_AUTHENTICATE;
2704 sp->pp_phase = PHASE_NETWORK;
2707 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2708 sppp_phase_name(sp->pp_phase));
2711 * Open all authentication protocols. This is even required
2712 * if we already proceeded to network phase, since it might be
2713 * that remote wants us to authenticate, so we might have to
2714 * send a PAP request. Undesired authentication protocols
2715 * don't do anything when they get an Open event.
2717 for (i = 0; i < IDX_COUNT; i++)
2718 if ((cps[i])->flags & CP_AUTH)
2721 if (sp->pp_phase == PHASE_NETWORK) {
2722 /* Notify all NCPs. */
2723 for (i = 0; i < IDX_COUNT; i++)
2724 if (((cps[i])->flags & CP_NCP) &&
2727 * Hack to administratively disable IPv6 if
2728 * not desired. Perhaps we should have another
2729 * flag for this, but right now, we can make
2730 * all struct cp's read/only.
2732 (cps[i] != &ipv6cp ||
2733 (sp->confflags & CONF_ENABLE_IPV6)))
2737 /* Send Up events to all started protos. */
2738 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2739 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2742 /* notify low-level driver of state change */
2744 sp->pp_chg(sp, (int)sp->pp_phase);
2746 if (sp->pp_phase == PHASE_NETWORK)
2747 /* if no NCP is starting, close down */
2748 sppp_lcp_check_and_close(sp);
2752 sppp_lcp_tld(struct sppp *sp)
2758 sp->pp_phase = PHASE_TERMINATE;
2761 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2762 sppp_phase_name(sp->pp_phase));
2765 * Take upper layers down. We send the Down event first and
2766 * the Close second to prevent the upper layers from sending
2767 * ``a flurry of terminate-request packets'', as the RFC
2770 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2771 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2773 (cps[i])->Close(sp);
2778 sppp_lcp_tls(struct sppp *sp)
2782 sp->pp_phase = PHASE_ESTABLISH;
2785 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2786 sppp_phase_name(sp->pp_phase));
2788 /* Notify lower layer if desired. */
2796 sppp_lcp_tlf(struct sppp *sp)
2800 sp->pp_phase = PHASE_DEAD;
2802 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2803 sppp_phase_name(sp->pp_phase));
2805 /* Notify lower layer if desired. */
2813 sppp_lcp_scr(struct sppp *sp)
2815 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2819 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2820 if (! sp->lcp.magic)
2821 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2822 sp->lcp.magic = random();
2824 sp->lcp.magic = time.tv_sec + time.tv_usec;
2826 opt[i++] = LCP_OPT_MAGIC;
2828 opt[i++] = sp->lcp.magic >> 24;
2829 opt[i++] = sp->lcp.magic >> 16;
2830 opt[i++] = sp->lcp.magic >> 8;
2831 opt[i++] = sp->lcp.magic;
2834 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2835 opt[i++] = LCP_OPT_MRU;
2837 opt[i++] = sp->lcp.mru >> 8;
2838 opt[i++] = sp->lcp.mru;
2841 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2842 authproto = sp->hisauth.proto;
2843 opt[i++] = LCP_OPT_AUTH_PROTO;
2844 opt[i++] = authproto == PPP_CHAP? 5: 4;
2845 opt[i++] = authproto >> 8;
2846 opt[i++] = authproto;
2847 if (authproto == PPP_CHAP)
2848 opt[i++] = CHAP_MD5;
2851 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2852 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2856 * Check the open NCPs, return true if at least one NCP is open.
2859 sppp_ncp_check(struct sppp *sp)
2863 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2864 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2870 * Re-check the open NCPs and see if we should terminate the link.
2871 * Called by the NCPs during their tlf action handling.
2874 sppp_lcp_check_and_close(struct sppp *sp)
2877 if (sp->pp_phase < PHASE_NETWORK)
2878 /* don't bother, we are already going down */
2881 if (sppp_ncp_check(sp))
2888 *--------------------------------------------------------------------------*
2890 * The IPCP implementation. *
2892 *--------------------------------------------------------------------------*
2896 sppp_ipcp_init(struct sppp *sp)
2900 sp->state[IDX_IPCP] = STATE_INITIAL;
2901 sp->fail_counter[IDX_IPCP] = 0;
2902 sp->pp_seq[IDX_IPCP] = 0;
2903 sp->pp_rseq[IDX_IPCP] = 0;
2904 callout_init(&sp->ch[IDX_IPCP],
2905 (sp->pp_if.if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
2909 sppp_ipcp_up(struct sppp *sp)
2911 sppp_up_event(&ipcp, sp);
2915 sppp_ipcp_down(struct sppp *sp)
2917 sppp_down_event(&ipcp, sp);
2921 sppp_ipcp_open(struct sppp *sp)
2924 u_long myaddr, hisaddr;
2926 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2927 IPCP_MYADDR_DYN | IPCP_VJ);
2930 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2932 * If we don't have his address, this probably means our
2933 * interface doesn't want to talk IP at all. (This could
2934 * be the case if somebody wants to speak only IPX, for
2935 * example.) Don't open IPCP in this case.
2937 if (hisaddr == 0L) {
2938 /* XXX this message should go away */
2940 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2946 * I don't have an assigned address, so i need to
2947 * negotiate my address.
2949 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2950 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2952 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2953 if (sp->confflags & CONF_ENABLE_VJ) {
2954 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2955 sp->ipcp.max_state = MAX_STATES - 1;
2956 sp->ipcp.compress_cid = 1;
2958 sppp_open_event(&ipcp, sp);
2962 sppp_ipcp_close(struct sppp *sp)
2964 sppp_close_event(&ipcp, sp);
2965 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2967 * My address was dynamic, clear it again.
2969 sppp_set_ip_addr(sp, 0L);
2973 sppp_ipcp_TO(void *cookie)
2975 sppp_to_event(&ipcp, (struct sppp *)cookie);
2979 * Analyze a configure request. Return true if it was agreeable, and
2980 * caused action sca, false if it has been rejected or nak'ed, and
2981 * caused action scn. (The return value is used to make the state
2982 * transition decision in the state automaton.)
2985 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2987 u_char *buf, *r, *p;
2988 struct ifnet *ifp = &sp->pp_if;
2989 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2990 u_long hisaddr, desiredaddr;
2997 * Make sure to allocate a buf that can at least hold a
2998 * conf-nak with an `address' option. We might need it below.
3000 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3004 /* pass 1: see if we can recognize them */
3006 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
3009 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3011 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3013 case IPCP_OPT_COMPRESSION:
3014 if (!(sp->confflags & CONF_ENABLE_VJ)) {
3015 /* VJ compression administratively disabled */
3017 log(-1, "[locally disabled] ");
3021 * In theory, we should only conf-rej an
3022 * option that is shorter than RFC 1618
3023 * requires (i.e. < 4), and should conf-nak
3024 * anything else that is not VJ. However,
3025 * since our algorithm always uses the
3026 * original option to NAK it with new values,
3027 * things would become more complicated. In
3028 * pratice, the only commonly implemented IP
3029 * compression option is VJ anyway, so the
3030 * difference is negligible.
3032 if (len >= 6 && p[1] == 6) {
3034 * correctly formed compression option
3035 * that could be VJ compression
3041 "optlen %d [invalid/unsupported] ",
3044 case IPCP_OPT_ADDRESS:
3045 if (len >= 6 && p[1] == 6) {
3046 /* correctly formed address option */
3050 log(-1, "[invalid] ");
3053 /* Others not supported. */
3058 /* Add the option to rejected list. */
3065 log(-1, " send conf-rej\n");
3066 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
3071 /* pass 2: parse option values */
3072 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
3074 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
3078 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3080 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3082 case IPCP_OPT_COMPRESSION:
3083 desiredcomp = p[2] << 8 | p[3];
3084 /* We only support VJ */
3085 if (desiredcomp == IPCP_COMP_VJ) {
3087 log(-1, "VJ [ack] ");
3088 sp->ipcp.flags |= IPCP_VJ;
3089 sl_compress_init(sp->pp_comp, p[4]);
3090 sp->ipcp.max_state = p[4];
3091 sp->ipcp.compress_cid = p[5];
3096 "compproto %#04x [not supported] ",
3098 p[2] = IPCP_COMP_VJ >> 8;
3099 p[3] = IPCP_COMP_VJ;
3100 p[4] = sp->ipcp.max_state;
3101 p[5] = sp->ipcp.compress_cid;
3103 case IPCP_OPT_ADDRESS:
3104 /* This is the address he wants in his end */
3105 desiredaddr = p[2] << 24 | p[3] << 16 |
3107 if (desiredaddr == hisaddr ||
3108 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3110 * Peer's address is same as our value,
3111 * or we have set it to 0.0.0.* to
3112 * indicate that we do not really care,
3113 * this is agreeable. Gonna conf-ack
3117 log(-1, "%s [ack] ",
3118 sppp_dotted_quad(hisaddr));
3119 /* record that we've seen it already */
3120 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3124 * The address wasn't agreeable. This is either
3125 * he sent us 0.0.0.0, asking to assign him an
3126 * address, or he send us another address not
3127 * matching our value. Either case, we gonna
3128 * conf-nak it with our value.
3129 * XXX: we should "rej" if hisaddr == 0
3132 if (desiredaddr == 0)
3133 log(-1, "[addr requested] ");
3135 log(-1, "%s [not agreed] ",
3136 sppp_dotted_quad(desiredaddr));
3139 p[2] = hisaddr >> 24;
3140 p[3] = hisaddr >> 16;
3141 p[4] = hisaddr >> 8;
3145 /* Add the option to nak'ed list. */
3152 * If we are about to conf-ack the request, but haven't seen
3153 * his address so far, gonna conf-nak it instead, with the
3154 * `address' option present and our idea of his address being
3155 * filled in there, to request negotiation of both addresses.
3157 * XXX This can result in an endless req - nak loop if peer
3158 * doesn't want to send us his address. Q: What should we do
3159 * about it? XXX A: implement the max-failure counter.
3161 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3162 buf[0] = IPCP_OPT_ADDRESS;
3164 buf[2] = hisaddr >> 24;
3165 buf[3] = hisaddr >> 16;
3166 buf[4] = hisaddr >> 8;
3170 log(-1, "still need hisaddr ");
3175 log(-1, " send conf-nak\n");
3176 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3179 log(-1, " send conf-ack\n");
3180 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3181 h->ident, origlen, h+1);
3189 * Analyze the IPCP Configure-Reject option list, and adjust our
3193 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3196 struct ifnet *ifp = &sp->pp_if;
3197 int debug = ifp->if_flags & IFF_DEBUG;
3200 buf = malloc (len, M_TEMP, M_NOWAIT);
3205 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3209 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3211 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3213 case IPCP_OPT_COMPRESSION:
3214 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3216 case IPCP_OPT_ADDRESS:
3218 * Peer doesn't grok address option. This is
3219 * bad. XXX Should we better give up here?
3220 * XXX We could try old "addresses" option...
3222 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3233 * Analyze the IPCP Configure-NAK option list, and adjust our
3237 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3240 struct ifnet *ifp = &sp->pp_if;
3241 int debug = ifp->if_flags & IFF_DEBUG;
3246 buf = malloc (len, M_TEMP, M_NOWAIT);
3251 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3255 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3257 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3259 case IPCP_OPT_COMPRESSION:
3260 if (len >= 6 && p[1] == 6) {
3261 desiredcomp = p[2] << 8 | p[3];
3263 log(-1, "[wantcomp %#04x] ",
3265 if (desiredcomp == IPCP_COMP_VJ) {
3266 sl_compress_init(sp->pp_comp, p[4]);
3267 sp->ipcp.max_state = p[4];
3268 sp->ipcp.compress_cid = p[5];
3270 log(-1, "[agree] ");
3273 ~(1 << IPCP_OPT_COMPRESSION);
3276 case IPCP_OPT_ADDRESS:
3278 * Peer doesn't like our local IP address. See
3279 * if we can do something for him. We'll drop
3280 * him our address then.
3282 if (len >= 6 && p[1] == 6) {
3283 wantaddr = p[2] << 24 | p[3] << 16 |
3285 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3287 log(-1, "[wantaddr %s] ",
3288 sppp_dotted_quad(wantaddr));
3290 * When doing dynamic address assignment,
3291 * we accept his offer. Otherwise, we
3292 * ignore it and thus continue to negotiate
3293 * our already existing value.
3294 * XXX: Bogus, if he said no once, he'll
3295 * just say no again, might as well die.
3297 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3298 sppp_set_ip_addr(sp, wantaddr);
3300 log(-1, "[agree] ");
3301 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3314 sppp_ipcp_tlu(struct sppp *sp)
3316 /* we are up - notify isdn daemon */
3322 sppp_ipcp_tld(struct sppp *sp)
3327 sppp_ipcp_tls(struct sppp *sp)
3329 /* indicate to LCP that it must stay alive */
3330 sp->lcp.protos |= (1 << IDX_IPCP);
3334 sppp_ipcp_tlf(struct sppp *sp)
3336 /* we no longer need LCP */
3337 sp->lcp.protos &= ~(1 << IDX_IPCP);
3338 sppp_lcp_check_and_close(sp);
3342 sppp_ipcp_scr(struct sppp *sp)
3344 char opt[6 /* compression */ + 6 /* address */];
3348 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3349 opt[i++] = IPCP_OPT_COMPRESSION;
3351 opt[i++] = IPCP_COMP_VJ >> 8;
3352 opt[i++] = IPCP_COMP_VJ;
3353 opt[i++] = sp->ipcp.max_state;
3354 opt[i++] = sp->ipcp.compress_cid;
3356 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3357 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3358 opt[i++] = IPCP_OPT_ADDRESS;
3360 opt[i++] = ouraddr >> 24;
3361 opt[i++] = ouraddr >> 16;
3362 opt[i++] = ouraddr >> 8;
3366 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3367 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3371 *--------------------------------------------------------------------------*
3373 * The IPv6CP implementation. *
3375 *--------------------------------------------------------------------------*
3380 sppp_ipv6cp_init(struct sppp *sp)
3382 sp->ipv6cp.opts = 0;
3383 sp->ipv6cp.flags = 0;
3384 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3385 sp->fail_counter[IDX_IPV6CP] = 0;
3386 sp->pp_seq[IDX_IPV6CP] = 0;
3387 sp->pp_rseq[IDX_IPV6CP] = 0;
3388 callout_init(&sp->ch[IDX_IPV6CP],
3389 (sp->pp_if.if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
3393 sppp_ipv6cp_up(struct sppp *sp)
3395 sppp_up_event(&ipv6cp, sp);
3399 sppp_ipv6cp_down(struct sppp *sp)
3401 sppp_down_event(&ipv6cp, sp);
3405 sppp_ipv6cp_open(struct sppp *sp)
3408 struct in6_addr myaddr, hisaddr;
3410 #ifdef IPV6CP_MYIFID_DYN
3411 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3413 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3416 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3418 * If we don't have our address, this probably means our
3419 * interface doesn't want to talk IPv6 at all. (This could
3420 * be the case if somebody wants to speak only IPX, for
3421 * example.) Don't open IPv6CP in this case.
3423 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3424 /* XXX this message should go away */
3426 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3431 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3432 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3433 sppp_open_event(&ipv6cp, sp);
3437 sppp_ipv6cp_close(struct sppp *sp)
3439 sppp_close_event(&ipv6cp, sp);
3443 sppp_ipv6cp_TO(void *cookie)
3445 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3449 * Analyze a configure request. Return true if it was agreeable, and
3450 * caused action sca, false if it has been rejected or nak'ed, and
3451 * caused action scn. (The return value is used to make the state
3452 * transition decision in the state automaton.)
3455 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3457 u_char *buf, *r, *p;
3458 struct ifnet *ifp = &sp->pp_if;
3459 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3460 struct in6_addr myaddr, desiredaddr, suggestaddr;
3463 int collision, nohisaddr;
3468 * Make sure to allocate a buf that can at least hold a
3469 * conf-nak with an `address' option. We might need it below.
3471 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3475 /* pass 1: see if we can recognize them */
3477 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3481 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3483 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3485 case IPV6CP_OPT_IFID:
3486 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3487 /* correctly formed address option */
3492 log(-1, " [invalid]");
3495 case IPV6CP_OPT_COMPRESSION:
3496 if (len >= 4 && p[1] >= 4) {
3497 /* correctly formed compress option */
3501 log(-1, " [invalid]");
3505 /* Others not supported. */
3510 /* Add the option to rejected list. */
3517 log(-1, " send conf-rej\n");
3518 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3523 /* pass 2: parse option values */
3524 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3526 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3531 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3533 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3536 case IPV6CP_OPT_COMPRESSION:
3539 case IPV6CP_OPT_IFID:
3540 bzero(&desiredaddr, sizeof(desiredaddr));
3541 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3542 collision = (bcmp(&desiredaddr.s6_addr[8],
3543 &myaddr.s6_addr[8], 8) == 0);
3544 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3546 desiredaddr.s6_addr16[0] = htons(0xfe80);
3547 desiredaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3549 if (!collision && !nohisaddr) {
3550 /* no collision, hisaddr known - Conf-Ack */
3555 ip6_sprintf(&desiredaddr),
3556 sppp_cp_type_name(type));
3561 bzero(&suggestaddr, sizeof(&suggestaddr));
3562 if (collision && nohisaddr) {
3563 /* collision, hisaddr unknown - Conf-Rej */
3568 * - no collision, hisaddr unknown, or
3569 * - collision, hisaddr known
3570 * Conf-Nak, suggest hisaddr
3573 sppp_suggest_ip6_addr(sp, &suggestaddr);
3574 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3577 log(-1, " %s [%s]", ip6_sprintf(&desiredaddr),
3578 sppp_cp_type_name(type));
3581 /* Add the option to nak'ed list. */
3587 if (rlen == 0 && type == CONF_ACK) {
3589 log(-1, " send %s\n", sppp_cp_type_name(type));
3590 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3593 if (type == CONF_ACK)
3594 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3598 log(-1, " send %s suggest %s\n",
3599 sppp_cp_type_name(type), ip6_sprintf(&suggestaddr));
3601 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3610 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3614 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3617 struct ifnet *ifp = &sp->pp_if;
3618 int debug = ifp->if_flags & IFF_DEBUG;
3621 buf = malloc (len, M_TEMP, M_NOWAIT);
3626 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3630 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3632 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3634 case IPV6CP_OPT_IFID:
3636 * Peer doesn't grok address option. This is
3637 * bad. XXX Should we better give up here?
3639 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3642 case IPV6CP_OPT_COMPRESS:
3643 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3655 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3659 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3662 struct ifnet *ifp = &sp->pp_if;
3663 int debug = ifp->if_flags & IFF_DEBUG;
3664 struct in6_addr suggestaddr;
3667 buf = malloc (len, M_TEMP, M_NOWAIT);
3672 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3676 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3678 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3680 case IPV6CP_OPT_IFID:
3682 * Peer doesn't like our local ifid. See
3683 * if we can do something for him. We'll drop
3684 * him our address then.
3686 if (len < 10 || p[1] != 10)
3688 bzero(&suggestaddr, sizeof(suggestaddr));
3689 suggestaddr.s6_addr16[0] = htons(0xfe80);
3690 suggestaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3691 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3693 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3695 log(-1, " [suggestaddr %s]",
3696 ip6_sprintf(&suggestaddr));
3697 #ifdef IPV6CP_MYIFID_DYN
3699 * When doing dynamic address assignment,
3700 * we accept his offer.
3702 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3703 struct in6_addr lastsuggest;
3705 * If <suggested myaddr from peer> equals to
3706 * <hisaddr we have suggested last time>,
3707 * we have a collision. generate new random
3710 sppp_suggest_ip6_addr(&lastsuggest);
3711 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3714 log(-1, " [random]");
3715 sppp_gen_ip6_addr(sp, &suggestaddr);
3717 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3719 log(-1, " [agree]");
3720 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3724 * Since we do not do dynamic address assignment,
3725 * we ignore it and thus continue to negotiate
3726 * our already existing value. This can possibly
3727 * go into infinite request-reject loop.
3729 * This is not likely because we normally use
3730 * ifid based on MAC-address.
3731 * If you have no ethernet card on the node, too bad.
3732 * XXX should we use fail_counter?
3737 case IPV6CP_OPT_COMPRESS:
3739 * Peer wants different compression parameters.
3751 sppp_ipv6cp_tlu(struct sppp *sp)
3753 /* we are up - notify isdn daemon */
3759 sppp_ipv6cp_tld(struct sppp *sp)
3764 sppp_ipv6cp_tls(struct sppp *sp)
3766 /* indicate to LCP that it must stay alive */
3767 sp->lcp.protos |= (1 << IDX_IPV6CP);
3771 sppp_ipv6cp_tlf(struct sppp *sp)
3774 #if 0 /* need #if 0 to close IPv6CP properly */
3775 /* we no longer need LCP */
3776 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3777 sppp_lcp_check_and_close(sp);
3782 sppp_ipv6cp_scr(struct sppp *sp)
3784 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3785 struct in6_addr ouraddr;
3788 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3789 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3790 opt[i++] = IPV6CP_OPT_IFID;
3792 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3797 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3798 opt[i++] = IPV6CP_OPT_COMPRESSION;
3800 opt[i++] = 0; /* TBD */
3801 opt[i++] = 0; /* TBD */
3802 /* variable length data may follow */
3806 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3807 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3810 static void sppp_ipv6cp_init(struct sppp *sp)
3814 static void sppp_ipv6cp_up(struct sppp *sp)
3818 static void sppp_ipv6cp_down(struct sppp *sp)
3823 static void sppp_ipv6cp_open(struct sppp *sp)
3827 static void sppp_ipv6cp_close(struct sppp *sp)
3831 static void sppp_ipv6cp_TO(void *sp)
3835 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3840 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3844 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3848 static void sppp_ipv6cp_tlu(struct sppp *sp)
3852 static void sppp_ipv6cp_tld(struct sppp *sp)
3856 static void sppp_ipv6cp_tls(struct sppp *sp)
3860 static void sppp_ipv6cp_tlf(struct sppp *sp)
3864 static void sppp_ipv6cp_scr(struct sppp *sp)
3870 *--------------------------------------------------------------------------*
3872 * The CHAP implementation. *
3874 *--------------------------------------------------------------------------*
3878 * The authentication protocols don't employ a full-fledged state machine as
3879 * the control protocols do, since they do have Open and Close events, but
3880 * not Up and Down, nor are they explicitly terminated. Also, use of the
3881 * authentication protocols may be different in both directions (this makes
3882 * sense, think of a machine that never accepts incoming calls but only
3883 * calls out, it doesn't require the called party to authenticate itself).
3885 * Our state machine for the local authentication protocol (we are requesting
3886 * the peer to authenticate) looks like:
3889 * +--------------------------------------------+
3891 * +--------+ Close +---------+ RCA+
3892 * | |<----------------------------------| |------+
3893 * +--->| Closed | TO* | Opened | sca |
3894 * | | |-----+ +-------| |<-----+
3895 * | +--------+ irc | | +---------+
3901 * | | +------->+ | |
3903 * | +--------+ V | |
3904 * | | |<----+<--------------------+ |
3910 * +------+ +------------------------------------------+
3911 * scn,tld sca,irc,ict,tlu
3916 * Open: LCP reached authentication phase
3917 * Close: LCP reached terminate phase
3919 * RCA+: received reply (pap-req, chap-response), acceptable
3920 * RCN: received reply (pap-req, chap-response), not acceptable
3921 * TO+: timeout with restart counter >= 0
3922 * TO-: timeout with restart counter < 0
3923 * TO*: reschedule timeout for CHAP
3925 * scr: send request packet (none for PAP, chap-challenge)
3926 * sca: send ack packet (pap-ack, chap-success)
3927 * scn: send nak packet (pap-nak, chap-failure)
3928 * ict: initialize re-challenge timer (CHAP only)
3930 * tlu: this-layer-up, LCP reaches network phase
3931 * tld: this-layer-down, LCP enters terminate phase
3933 * Note that in CHAP mode, after sending a new challenge, while the state
3934 * automaton falls back into Req-Sent state, it doesn't signal a tld
3935 * event to LCP, so LCP remains in network phase. Only after not getting
3936 * any response (or after getting an unacceptable response), CHAP closes,
3937 * causing LCP to enter terminate phase.
3939 * With PAP, there is no initial request that can be sent. The peer is
3940 * expected to send one based on the successful negotiation of PAP as
3941 * the authentication protocol during the LCP option negotiation.
3943 * Incoming authentication protocol requests (remote requests
3944 * authentication, we are peer) don't employ a state machine at all,
3945 * they are simply answered. Some peers [Ascend P50 firmware rev
3946 * 4.50] react allergically when sending IPCP requests while they are
3947 * still in authentication phase (thereby violating the standard that
3948 * demands that these NCP packets are to be discarded), so we keep
3949 * track of the peer demanding us to authenticate, and only proceed to
3950 * phase network once we've seen a positive acknowledge for the
3955 * Handle incoming CHAP packets.
3958 sppp_chap_input(struct sppp *sp, struct mbuf *m)
3961 struct lcp_header *h;
3963 u_char *value, *name, digest[AUTHKEYLEN], dsize;
3964 int value_len, name_len;
3967 len = m->m_pkthdr.len;
3971 SPP_FMT "chap invalid packet length: %d bytes\n",
3972 SPP_ARGS(ifp), len);
3975 h = mtod (m, struct lcp_header*);
3976 if (len > ntohs (h->len))
3977 len = ntohs (h->len);
3980 /* challenge, failure and success are his authproto */
3981 case CHAP_CHALLENGE:
3982 value = 1 + (u_char*)(h+1);
3983 value_len = value[-1];
3984 name = value + value_len;
3985 name_len = len - value_len - 5;
3989 SPP_FMT "chap corrupted challenge "
3990 "<%s id=0x%x len=%d",
3992 sppp_auth_type_name(PPP_CHAP, h->type),
3993 h->ident, ntohs(h->len));
3994 sppp_print_bytes((u_char*) (h+1), len-4);
4002 SPP_FMT "chap input <%s id=0x%x len=%d name=",
4004 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
4006 sppp_print_string((char*) name, name_len);
4007 log(-1, " value-size=%d value=", value_len);
4008 sppp_print_bytes(value, value_len);
4012 /* Compute reply value. */
4014 MD5Update(&ctx, &h->ident, 1);
4015 MD5Update(&ctx, sp->myauth.secret,
4016 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
4017 MD5Update(&ctx, value, value_len);
4018 MD5Final(digest, &ctx);
4019 dsize = sizeof digest;
4021 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
4022 sizeof dsize, (const char *)&dsize,
4023 sizeof digest, digest,
4024 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4031 log(LOG_DEBUG, SPP_FMT "chap success",
4035 sppp_print_string((char*)(h + 1), len - 4);
4041 sp->pp_flags &= ~PP_NEEDAUTH;
4042 if (sp->myauth.proto == PPP_CHAP &&
4043 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4044 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
4046 * We are authenticator for CHAP but didn't
4047 * complete yet. Leave it to tlu to proceed
4056 sppp_phase_network(sp);
4061 log(LOG_INFO, SPP_FMT "chap failure",
4065 sppp_print_string((char*)(h + 1), len - 4);
4069 log(LOG_INFO, SPP_FMT "chap failure\n",
4071 /* await LCP shutdown by authenticator */
4074 /* response is my authproto */
4076 value = 1 + (u_char*)(h+1);
4077 value_len = value[-1];
4078 name = value + value_len;
4079 name_len = len - value_len - 5;
4083 SPP_FMT "chap corrupted response "
4084 "<%s id=0x%x len=%d",
4086 sppp_auth_type_name(PPP_CHAP, h->type),
4087 h->ident, ntohs(h->len));
4088 sppp_print_bytes((u_char*)(h+1), len-4);
4093 if (h->ident != sp->confid[IDX_CHAP]) {
4096 SPP_FMT "chap dropping response for old ID "
4097 "(got %d, expected %d)\n",
4099 h->ident, sp->confid[IDX_CHAP]);
4102 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4103 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4104 log(LOG_INFO, SPP_FMT "chap response, his name ",
4106 sppp_print_string(name, name_len);
4107 log(-1, " != expected ");
4108 sppp_print_string(sp->hisauth.name,
4109 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4113 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4114 "<%s id=0x%x len=%d name=",
4116 sppp_state_name(sp->state[IDX_CHAP]),
4117 sppp_auth_type_name(PPP_CHAP, h->type),
4118 h->ident, ntohs (h->len));
4119 sppp_print_string((char*)name, name_len);
4120 log(-1, " value-size=%d value=", value_len);
4121 sppp_print_bytes(value, value_len);
4124 if (value_len != AUTHKEYLEN) {
4127 SPP_FMT "chap bad hash value length: "
4128 "%d bytes, should be %d\n",
4129 SPP_ARGS(ifp), value_len,
4135 MD5Update(&ctx, &h->ident, 1);
4136 MD5Update(&ctx, sp->hisauth.secret,
4137 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4138 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4139 MD5Final(digest, &ctx);
4141 #define FAILMSG "Failed..."
4142 #define SUCCMSG "Welcome!"
4144 if (value_len != sizeof digest ||
4145 bcmp(digest, value, value_len) != 0) {
4146 /* action scn, tld */
4147 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4148 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4153 /* action sca, perhaps tlu */
4154 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4155 sp->state[IDX_CHAP] == STATE_OPENED)
4156 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4157 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4159 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4160 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4166 /* Unknown CHAP packet type -- ignore. */
4168 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4169 "<0x%x id=0x%xh len=%d",
4171 sppp_state_name(sp->state[IDX_CHAP]),
4172 h->type, h->ident, ntohs(h->len));
4173 sppp_print_bytes((u_char*)(h+1), len-4);
4182 sppp_chap_init(struct sppp *sp)
4184 /* Chap doesn't have STATE_INITIAL at all. */
4185 sp->state[IDX_CHAP] = STATE_CLOSED;
4186 sp->fail_counter[IDX_CHAP] = 0;
4187 sp->pp_seq[IDX_CHAP] = 0;
4188 sp->pp_rseq[IDX_CHAP] = 0;
4189 callout_init(&sp->ch[IDX_CHAP],
4190 (sp->pp_if.if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
4194 sppp_chap_open(struct sppp *sp)
4196 if (sp->myauth.proto == PPP_CHAP &&
4197 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4198 /* we are authenticator for CHAP, start it */
4200 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4201 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4203 /* nothing to be done if we are peer, await a challenge */
4207 sppp_chap_close(struct sppp *sp)
4209 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4210 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4214 sppp_chap_TO(void *cookie)
4216 struct sppp *sp = (struct sppp *)cookie;
4223 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4225 sppp_state_name(sp->state[IDX_CHAP]),
4226 sp->rst_counter[IDX_CHAP]);
4228 if (--sp->rst_counter[IDX_CHAP] < 0)
4230 switch (sp->state[IDX_CHAP]) {
4231 case STATE_REQ_SENT:
4233 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4237 /* TO+ (or TO*) event */
4238 switch (sp->state[IDX_CHAP]) {
4241 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4243 case STATE_REQ_SENT:
4245 /* sppp_cp_change_state() will restart the timer */
4246 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4255 sppp_chap_tlu(struct sppp *sp)
4261 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4264 * Some broken CHAP implementations (Conware CoNet, firmware
4265 * 4.0.?) don't want to re-authenticate their CHAP once the
4266 * initial challenge-response exchange has taken place.
4267 * Provide for an option to avoid rechallenges.
4269 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4271 * Compute the re-challenge timeout. This will yield
4272 * a number between 300 and 810 seconds.
4274 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
4275 callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, (void *)sp);
4280 SPP_FMT "chap %s, ",
4282 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4283 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4284 log(-1, "next re-challenge in %d seconds\n", i);
4286 log(-1, "re-challenging supressed\n");
4291 /* indicate to LCP that we need to be closed down */
4292 sp->lcp.protos |= (1 << IDX_CHAP);
4294 if (sp->pp_flags & PP_NEEDAUTH) {
4296 * Remote is authenticator, but his auth proto didn't
4297 * complete yet. Defer the transition to network
4308 * If we are already in phase network, we are done here. This
4309 * is the case if this is a dummy tlu event after a re-challenge.
4311 if (sp->pp_phase != PHASE_NETWORK)
4312 sppp_phase_network(sp);
4316 sppp_chap_tld(struct sppp *sp)
4321 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4322 callout_stop(&sp->ch[IDX_CHAP]);
4323 sp->lcp.protos &= ~(1 << IDX_CHAP);
4329 sppp_chap_scr(struct sppp *sp)
4334 /* Compute random challenge. */
4335 ch = (u_long *)sp->myauth.challenge;
4336 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
4337 read_random(&seed, sizeof seed);
4342 seed = tv.tv_sec ^ tv.tv_usec;
4345 ch[0] = seed ^ random();
4346 ch[1] = seed ^ random();
4347 ch[2] = seed ^ random();
4348 ch[3] = seed ^ random();
4351 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4353 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4354 sizeof clen, (const char *)&clen,
4355 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4356 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4362 *--------------------------------------------------------------------------*
4364 * The PAP implementation. *
4366 *--------------------------------------------------------------------------*
4369 * For PAP, we need to keep a little state also if we are the peer, not the
4370 * authenticator. This is since we don't get a request to authenticate, but
4371 * have to repeatedly authenticate ourself until we got a response (or the
4372 * retry counter is expired).
4376 * Handle incoming PAP packets. */
4378 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4381 struct lcp_header *h;
4383 u_char *name, *passwd, mlen;
4384 int name_len, passwd_len;
4386 len = m->m_pkthdr.len;
4390 SPP_FMT "pap invalid packet length: %d bytes\n",
4391 SPP_ARGS(ifp), len);
4394 h = mtod (m, struct lcp_header*);
4395 if (len > ntohs (h->len))
4396 len = ntohs (h->len);
4398 /* PAP request is my authproto */
4400 name = 1 + (u_char*)(h+1);
4401 name_len = name[-1];
4402 passwd = name + name_len + 1;
4403 if (name_len > len - 6 ||
4404 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4406 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4407 "<%s id=0x%x len=%d",
4409 sppp_auth_type_name(PPP_PAP, h->type),
4410 h->ident, ntohs(h->len));
4411 sppp_print_bytes((u_char*)(h+1), len-4);
4417 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4418 "<%s id=0x%x len=%d name=",
4420 sppp_state_name(sp->state[IDX_PAP]),
4421 sppp_auth_type_name(PPP_PAP, h->type),
4422 h->ident, ntohs(h->len));
4423 sppp_print_string((char*)name, name_len);
4424 log(-1, " passwd=");
4425 sppp_print_string((char*)passwd, passwd_len);
4428 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4429 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4430 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4431 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4432 /* action scn, tld */
4433 mlen = sizeof(FAILMSG) - 1;
4434 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4435 sizeof mlen, (const char *)&mlen,
4436 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4441 /* action sca, perhaps tlu */
4442 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4443 sp->state[IDX_PAP] == STATE_OPENED) {
4444 mlen = sizeof(SUCCMSG) - 1;
4445 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4446 sizeof mlen, (const char *)&mlen,
4447 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4450 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4451 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4456 /* ack and nak are his authproto */
4458 callout_stop(&sp->pap_my_to_ch);
4460 log(LOG_DEBUG, SPP_FMT "pap success",
4462 name_len = *((char *)h);
4463 if (len > 5 && name_len) {
4465 sppp_print_string((char*)(h+1), name_len);
4471 sp->pp_flags &= ~PP_NEEDAUTH;
4472 if (sp->myauth.proto == PPP_PAP &&
4473 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4474 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4476 * We are authenticator for PAP but didn't
4477 * complete yet. Leave it to tlu to proceed
4486 sppp_phase_network(sp);
4490 callout_stop (&sp->pap_my_to_ch);
4492 log(LOG_INFO, SPP_FMT "pap failure",
4494 name_len = *((char *)h);
4495 if (len > 5 && name_len) {
4497 sppp_print_string((char*)(h+1), name_len);
4501 log(LOG_INFO, SPP_FMT "pap failure\n",
4503 /* await LCP shutdown by authenticator */
4507 /* Unknown PAP packet type -- ignore. */
4509 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4510 "<0x%x id=0x%x len=%d",
4512 h->type, h->ident, ntohs(h->len));
4513 sppp_print_bytes((u_char*)(h+1), len-4);
4522 sppp_pap_init(struct sppp *sp)
4524 /* PAP doesn't have STATE_INITIAL at all. */
4525 sp->state[IDX_PAP] = STATE_CLOSED;
4526 sp->fail_counter[IDX_PAP] = 0;
4527 sp->pp_seq[IDX_PAP] = 0;
4528 sp->pp_rseq[IDX_PAP] = 0;
4529 callout_init(&sp->ch[IDX_PAP],
4530 (sp->pp_if.if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
4531 callout_init(&sp->pap_my_to_ch,
4532 (sp->pp_if.if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
4536 sppp_pap_open(struct sppp *sp)
4538 if (sp->hisauth.proto == PPP_PAP &&
4539 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4540 /* we are authenticator for PAP, start our timer */
4541 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4542 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4544 if (sp->myauth.proto == PPP_PAP) {
4545 /* we are peer, send a request, and start a timer */
4547 callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
4548 sppp_pap_my_TO, (void *)sp);
4553 sppp_pap_close(struct sppp *sp)
4555 if (sp->state[IDX_PAP] != STATE_CLOSED)
4556 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4560 * That's the timeout routine if we are authenticator. Since the
4561 * authenticator is basically passive in PAP, we can't do much here.
4564 sppp_pap_TO(void *cookie)
4566 struct sppp *sp = (struct sppp *)cookie;
4573 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4575 sppp_state_name(sp->state[IDX_PAP]),
4576 sp->rst_counter[IDX_PAP]);
4578 if (--sp->rst_counter[IDX_PAP] < 0)
4580 switch (sp->state[IDX_PAP]) {
4581 case STATE_REQ_SENT:
4583 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4587 /* TO+ event, not very much we could do */
4588 switch (sp->state[IDX_PAP]) {
4589 case STATE_REQ_SENT:
4590 /* sppp_cp_change_state() will restart the timer */
4591 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4600 * That's the timeout handler if we are peer. Since the peer is active,
4601 * we need to retransmit our PAP request since it is apparently lost.
4602 * XXX We should impose a max counter.
4605 sppp_pap_my_TO(void *cookie)
4607 struct sppp *sp = (struct sppp *)cookie;
4611 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4620 sppp_pap_tlu(struct sppp *sp)
4625 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4628 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4629 SPP_ARGS(ifp), pap.name);
4633 /* indicate to LCP that we need to be closed down */
4634 sp->lcp.protos |= (1 << IDX_PAP);
4636 if (sp->pp_flags & PP_NEEDAUTH) {
4638 * Remote is authenticator, but his auth proto didn't
4639 * complete yet. Defer the transition to network
4648 sppp_phase_network(sp);
4652 sppp_pap_tld(struct sppp *sp)
4657 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4658 callout_stop (&sp->ch[IDX_PAP]);
4659 callout_stop (&sp->pap_my_to_ch);
4660 sp->lcp.protos &= ~(1 << IDX_PAP);
4666 sppp_pap_scr(struct sppp *sp)
4668 u_char idlen, pwdlen;
4670 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4671 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4672 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4674 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4675 sizeof idlen, (const char *)&idlen,
4676 (size_t)idlen, sp->myauth.name,
4677 sizeof pwdlen, (const char *)&pwdlen,
4678 (size_t)pwdlen, sp->myauth.secret,
4683 * Random miscellaneous functions.
4687 * Send a PAP or CHAP proto packet.
4689 * Varadic function, each of the elements for the ellipsis is of type
4690 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4692 * NOTE: never declare variadic functions with types subject to type
4693 * promotion (i.e. u_char). This is asking for big trouble depending
4694 * on the architecture you are on...
4698 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4699 unsigned int type, unsigned int id,
4703 struct ppp_header *h;
4704 struct lcp_header *lh;
4712 MGETHDR (m, M_DONTWAIT, MT_DATA);
4715 m->m_pkthdr.rcvif = 0;
4717 h = mtod (m, struct ppp_header*);
4718 h->address = PPP_ALLSTATIONS; /* broadcast address */
4719 h->control = PPP_UI; /* Unnumbered Info */
4720 h->protocol = htons(cp->proto);
4722 lh = (struct lcp_header*)(h + 1);
4725 p = (u_char*) (lh+1);
4730 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
4731 msg = va_arg(ap, const char *);
4733 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4739 bcopy(msg, p, mlen);
4744 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4745 lh->len = htons (LCP_HEADER_LEN + len);
4748 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4749 SPP_ARGS(ifp), cp->name,
4750 sppp_auth_type_name(cp->proto, lh->type),
4751 lh->ident, ntohs(lh->len));
4752 sppp_print_bytes((u_char*) (lh+1), len);
4755 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
4760 * Flush interface queue.
4763 sppp_qflush(struct ifqueue *ifq)
4778 * Send keepalive packets, every 10 seconds.
4781 sppp_keepalive(void *dummy)
4783 struct sppp *sp = (struct sppp*)dummy;
4784 struct ifnet *ifp = &sp->pp_if;
4789 /* Keepalive mode disabled or channel down? */
4790 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4791 ! (ifp->if_flags & IFF_RUNNING))
4794 if (sp->pp_mode == PP_FR) {
4795 sppp_fr_keepalive (sp);
4799 /* No keepalive in PPP mode if LCP not opened yet. */
4800 if (sp->pp_mode != IFF_CISCO &&
4801 sp->pp_phase < PHASE_AUTHENTICATE)
4804 if (sp->pp_alivecnt == MAXALIVECNT) {
4805 /* No keepalive packets got. Stop the interface. */
4806 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
4808 sppp_qflush (&sp->pp_cpq);
4809 if (sp->pp_mode != IFF_CISCO) {
4811 /* Shut down the PPP link. */
4813 /* Initiate negotiation. XXX */
4817 if (sp->pp_alivecnt <= MAXALIVECNT)
4819 if (sp->pp_mode == IFF_CISCO)
4820 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4821 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4822 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4823 long nmagic = htonl (sp->lcp.magic);
4824 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4825 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4826 sp->lcp.echoid, 4, &nmagic);
4831 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
4836 * Get both IP addresses.
4839 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4841 struct ifnet *ifp = &sp->pp_if;
4843 struct sockaddr_in *si, *sm;
4849 * Pick the first AF_INET address from the list,
4850 * aliases don't make any sense on a p2p link anyway.
4853 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
4854 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4855 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4856 for (ifa = TAILQ_FIRST(&ifp->if_addrlist);
4858 ifa = TAILQ_NEXT(ifa, ifa_list))
4860 for (ifa = ifp->if_addrlist;
4862 ifa = ifa->ifa_next)
4864 if (ifa->ifa_addr->sa_family == AF_INET) {
4865 si = (struct sockaddr_in *)ifa->ifa_addr;
4866 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4871 if (si && si->sin_addr.s_addr) {
4872 ssrc = si->sin_addr.s_addr;
4874 *srcmask = ntohl(sm->sin_addr.s_addr);
4877 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4878 if (si && si->sin_addr.s_addr)
4879 ddst = si->sin_addr.s_addr;
4882 if (dst) *dst = ntohl(ddst);
4883 if (src) *src = ntohl(ssrc);
4887 * Set my IP address. Must be called at splimp.
4890 sppp_set_ip_addr(struct sppp *sp, u_long src)
4894 struct sockaddr_in *si;
4895 struct in_ifaddr *ia;
4898 * Pick the first AF_INET address from the list,
4899 * aliases don't make any sense on a p2p link anyway.
4902 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
4903 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4904 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4905 for (ifa = TAILQ_FIRST(&ifp->if_addrlist);
4907 ifa = TAILQ_NEXT(ifa, ifa_list))
4909 for (ifa = ifp->if_addrlist;
4911 ifa = ifa->ifa_next)
4914 if (ifa->ifa_addr->sa_family == AF_INET)
4916 si = (struct sockaddr_in *)ifa->ifa_addr;
4925 #if __NetBSD_Version__ >= 103080000
4926 struct sockaddr_in new_sin = *si;
4928 new_sin.sin_addr.s_addr = htonl(src);
4929 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 1);
4932 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: in_ifinit "
4933 " failed, error=%d\n", SPP_ARGS(ifp), error);
4936 /* delete old route */
4937 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4940 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4941 SPP_ARGS(ifp), error);
4944 /* set new address */
4945 si->sin_addr.s_addr = htonl(src);
4947 LIST_REMOVE(ia, ia_hash);
4948 LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash);
4951 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
4954 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
4955 SPP_ARGS(ifp), error);
4963 * Get both IPv6 addresses.
4966 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4967 struct in6_addr *srcmask)
4969 struct ifnet *ifp = &sp->pp_if;
4971 struct sockaddr_in6 *si, *sm;
4972 struct in6_addr ssrc, ddst;
4975 bzero(&ssrc, sizeof(ssrc));
4976 bzero(&ddst, sizeof(ddst));
4978 * Pick the first link-local AF_INET6 address from the list,
4979 * aliases don't make any sense on a p2p link anyway.
4981 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
4982 for (ifa = ifp->if_addrhead.tqh_first, si = 0;
4984 ifa = ifa->ifa_link.tqe_next)
4985 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4986 for (ifa = ifp->if_addrlist.tqh_first, si = 0;
4988 ifa = ifa->ifa_list.tqe_next)
4990 for (ifa = ifp->if_addrlist, si = 0;
4992 ifa = ifa->ifa_next)
4994 if (ifa->ifa_addr->sa_family == AF_INET6) {
4995 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4996 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4997 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
5001 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
5002 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
5004 bcopy(&sm->sin6_addr, srcmask,
5009 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
5010 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
5011 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
5015 bcopy(&ddst, dst, sizeof(*dst));
5017 bcopy(&ssrc, src, sizeof(*src));
5020 #ifdef IPV6CP_MYIFID_DYN
5022 * Generate random ifid.
5025 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
5031 * Set my IPv6 address. Must be called at splimp.
5034 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
5038 struct sockaddr_in6 *sin6;
5041 * Pick the first link-local AF_INET6 address from the list,
5042 * aliases don't make any sense on a p2p link anyway.
5046 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
5047 for (ifa = ifp->if_addrhead.tqh_first;
5049 ifa = ifa->ifa_link.tqe_next)
5050 #elif defined(__NetBSD__) || defined (__OpenBSD__)
5051 for (ifa = ifp->if_addrlist.tqh_first;
5053 ifa = ifa->ifa_list.tqe_next)
5055 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
5058 if (ifa->ifa_addr->sa_family == AF_INET6)
5060 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
5061 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
5069 struct sockaddr_in6 new_sin6 = *sin6;
5071 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
5072 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5075 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
5076 " failed, error=%d\n", SPP_ARGS(ifp), error);
5083 * Suggest a candidate address to be used by peer.
5086 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5088 struct in6_addr myaddr;
5091 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5093 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5095 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5096 myaddr.s6_addr[14] ^= 0xff;
5097 myaddr.s6_addr[15] ^= 0xff;
5099 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5100 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5103 bcopy(&myaddr, suggest, sizeof(myaddr));
5108 sppp_params(struct sppp *sp, u_long cmd, void *data)
5111 struct ifreq *ifr = (struct ifreq *)data;
5112 struct spppreq *spr;
5115 if ((spr = malloc(sizeof(struct spppreq), M_TEMP, M_NOWAIT)) == 0)
5118 * ifr->ifr_data is supposed to point to a struct spppreq.
5119 * Check the cmd word first before attempting to fetch all the
5122 if ((subcmd = fuword(ifr->ifr_data)) == -1) {
5127 if (copyin((caddr_t)ifr->ifr_data, spr, sizeof(struct spppreq)) != 0) {
5133 case (int)SPPPIOGDEFS:
5134 if (cmd != SIOCGIFGENERIC) {
5139 * We copy over the entire current state, but clean
5140 * out some of the stuff we don't wanna pass up.
5141 * Remember, SIOCGIFGENERIC is unprotected, and can be
5142 * called by any user. No need to ever get PAP or
5143 * CHAP secrets back to userland anyway.
5145 spr->defs.pp_phase = sp->pp_phase;
5146 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5147 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5148 spr->defs.lcp = sp->lcp;
5149 spr->defs.ipcp = sp->ipcp;
5150 spr->defs.ipv6cp = sp->ipv6cp;
5151 spr->defs.myauth = sp->myauth;
5152 spr->defs.hisauth = sp->hisauth;
5153 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5154 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5155 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5156 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5158 * Fixup the LCP timeout value to milliseconds so
5159 * spppcontrol doesn't need to bother about the value
5160 * of "hz". We do the reverse calculation below when
5163 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5164 rv = copyout(spr, (caddr_t)ifr->ifr_data,
5165 sizeof(struct spppreq));
5168 case (int)SPPPIOSDEFS:
5169 if (cmd != SIOCSIFGENERIC) {
5174 * We have a very specific idea of which fields we
5175 * allow being passed back from userland, so to not
5176 * clobber our current state. For one, we only allow
5177 * setting anything if LCP is in dead or establish
5178 * phase. Once the authentication negotiations
5179 * started, the authentication settings must not be
5180 * changed again. (The administrator can force an
5181 * ifconfig down in order to get LCP back into dead
5184 * Also, we only allow for authentication parameters to be
5187 * XXX Should allow to set or clear pp_flags.
5189 * Finally, if the respective authentication protocol to
5190 * be used is set differently than 0, but the secret is
5191 * passed as all zeros, we don't trash the existing secret.
5192 * This allows an administrator to change the system name
5193 * only without clobbering the secret (which he didn't get
5194 * back in a previous SPPPIOGDEFS call). However, the
5195 * secrets are cleared if the authentication protocol is
5197 if (sp->pp_phase != PHASE_DEAD &&
5198 sp->pp_phase != PHASE_ESTABLISH) {
5203 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5204 spr->defs.myauth.proto != PPP_CHAP) ||
5205 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5206 spr->defs.hisauth.proto != PPP_CHAP)) {
5211 if (spr->defs.myauth.proto == 0)
5212 /* resetting myauth */
5213 bzero(&sp->myauth, sizeof sp->myauth);
5215 /* setting/changing myauth */
5216 sp->myauth.proto = spr->defs.myauth.proto;
5217 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5218 if (spr->defs.myauth.secret[0] != '\0')
5219 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5222 if (spr->defs.hisauth.proto == 0)
5223 /* resetting hisauth */
5224 bzero(&sp->hisauth, sizeof sp->hisauth);
5226 /* setting/changing hisauth */
5227 sp->hisauth.proto = spr->defs.hisauth.proto;
5228 sp->hisauth.flags = spr->defs.hisauth.flags;
5229 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5230 if (spr->defs.hisauth.secret[0] != '\0')
5231 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5234 /* set LCP restart timer timeout */
5235 if (spr->defs.lcp.timeout != 0)
5236 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5237 /* set VJ enable and IPv6 disable flags */
5239 if (spr->defs.enable_vj)
5240 sp->confflags |= CONF_ENABLE_VJ;
5242 sp->confflags &= ~CONF_ENABLE_VJ;
5245 if (spr->defs.enable_ipv6)
5246 sp->confflags |= CONF_ENABLE_IPV6;
5248 sp->confflags &= ~CONF_ENABLE_IPV6;
5263 sppp_phase_network(struct sppp *sp)
5269 sp->pp_phase = PHASE_NETWORK;
5272 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5273 sppp_phase_name(sp->pp_phase));
5275 /* Notify NCPs now. */
5276 for (i = 0; i < IDX_COUNT; i++)
5277 if ((cps[i])->flags & CP_NCP)
5280 /* Send Up events to all NCPs. */
5281 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5282 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5285 /* if no NCP is starting, all this was in vain, close down */
5286 sppp_lcp_check_and_close(sp);
5291 sppp_cp_type_name(u_char type)
5293 static char buf[12];
5295 case CONF_REQ: return "conf-req";
5296 case CONF_ACK: return "conf-ack";
5297 case CONF_NAK: return "conf-nak";
5298 case CONF_REJ: return "conf-rej";
5299 case TERM_REQ: return "term-req";
5300 case TERM_ACK: return "term-ack";
5301 case CODE_REJ: return "code-rej";
5302 case PROTO_REJ: return "proto-rej";
5303 case ECHO_REQ: return "echo-req";
5304 case ECHO_REPLY: return "echo-reply";
5305 case DISC_REQ: return "discard-req";
5307 snprintf (buf, sizeof(buf), "cp/0x%x", type);
5312 sppp_auth_type_name(u_short proto, u_char type)
5314 static char buf[12];
5318 case CHAP_CHALLENGE: return "challenge";
5319 case CHAP_RESPONSE: return "response";
5320 case CHAP_SUCCESS: return "success";
5321 case CHAP_FAILURE: return "failure";
5325 case PAP_REQ: return "req";
5326 case PAP_ACK: return "ack";
5327 case PAP_NAK: return "nak";
5330 snprintf (buf, sizeof(buf), "auth/0x%x", type);
5335 sppp_lcp_opt_name(u_char opt)
5337 static char buf[12];
5339 case LCP_OPT_MRU: return "mru";
5340 case LCP_OPT_ASYNC_MAP: return "async-map";
5341 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5342 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5343 case LCP_OPT_MAGIC: return "magic";
5344 case LCP_OPT_PROTO_COMP: return "proto-comp";
5345 case LCP_OPT_ADDR_COMP: return "addr-comp";
5347 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5352 sppp_ipcp_opt_name(u_char opt)
5354 static char buf[12];
5356 case IPCP_OPT_ADDRESSES: return "addresses";
5357 case IPCP_OPT_COMPRESSION: return "compression";
5358 case IPCP_OPT_ADDRESS: return "address";
5360 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5366 sppp_ipv6cp_opt_name(u_char opt)
5368 static char buf[12];
5370 case IPV6CP_OPT_IFID: return "ifid";
5371 case IPV6CP_OPT_COMPRESSION: return "compression";
5373 sprintf (buf, "0x%x", opt);
5379 sppp_state_name(int state)
5382 case STATE_INITIAL: return "initial";
5383 case STATE_STARTING: return "starting";
5384 case STATE_CLOSED: return "closed";
5385 case STATE_STOPPED: return "stopped";
5386 case STATE_CLOSING: return "closing";
5387 case STATE_STOPPING: return "stopping";
5388 case STATE_REQ_SENT: return "req-sent";
5389 case STATE_ACK_RCVD: return "ack-rcvd";
5390 case STATE_ACK_SENT: return "ack-sent";
5391 case STATE_OPENED: return "opened";
5397 sppp_phase_name(enum ppp_phase phase)
5400 case PHASE_DEAD: return "dead";
5401 case PHASE_ESTABLISH: return "establish";
5402 case PHASE_TERMINATE: return "terminate";
5403 case PHASE_AUTHENTICATE: return "authenticate";
5404 case PHASE_NETWORK: return "network";
5410 sppp_proto_name(u_short proto)
5412 static char buf[12];
5414 case PPP_LCP: return "lcp";
5415 case PPP_IPCP: return "ipcp";
5416 case PPP_PAP: return "pap";
5417 case PPP_CHAP: return "chap";
5418 case PPP_IPV6CP: return "ipv6cp";
5420 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5425 sppp_print_bytes(const u_char *p, u_short len)
5428 log(-1, " %*D", len, p, "-");
5432 sppp_print_string(const char *p, u_short len)
5439 * Print only ASCII chars directly. RFC 1994 recommends
5440 * using only them, but we don't rely on it. */
5441 if (c < ' ' || c > '~')
5442 log(-1, "\\x%x", c);
5449 sppp_dotted_quad(u_long addr)
5452 sprintf(s, "%d.%d.%d.%d",
5453 (int)((addr >> 24) & 0xff),
5454 (int)((addr >> 16) & 0xff),
5455 (int)((addr >> 8) & 0xff),
5456 (int)(addr & 0xff));
5461 sppp_strnlen(u_char *p, int max)
5465 for (len = 0; len < max && *p; ++p)
5470 /* a dummy, used to drop uninteresting events */
5472 sppp_null(struct sppp *unused)
5474 /* do just nothing */