1 /* $NetBSD: if_spppsubr.c,v 1.117 2009/03/18 17:06:51 cegger Exp $ */
4 * Synchronous PPP/Cisco link level subroutines.
5 * Keepalive protocol implemented in both Cisco and PPP modes.
7 * Copyright (C) 1994-1996 Cronyx Engineering Ltd.
8 * Author: Serge Vakulenko, <vak@cronyx.ru>
10 * Heavily revamped to conform to RFC 1661.
11 * Copyright (C) 1997, Joerg Wunsch.
13 * RFC2472 IPv6CP support.
14 * Copyright (C) 2000, Jun-ichiro itojun Hagino <itojun@iijlab.net>.
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions are met:
18 * 1. Redistributions of source code must retain the above copyright notice,
19 * this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright notice,
21 * this list of conditions and the following disclaimer in the documentation
22 * and/or other materials provided with the distribution.
24 * THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT ``AS IS'' AND ANY
25 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
36 * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997
38 * From: if_spppsubr.c,v 1.39 1998/04/04 13:26:03 phk Exp
40 * From: Id: if_spppsubr.c,v 1.23 1999/02/23 14:47:50 hm Exp
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: if_spppsubr.c,v 1.117 2009/03/18 17:06:51 cegger Exp $");
49 #include "opt_pfil_hooks.h"
51 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/kernel.h>
55 #include <sys/sockio.h>
56 #include <sys/socket.h>
57 #include <sys/syslog.h>
58 #include <sys/malloc.h>
60 #include <sys/callout.h>
62 #include <sys/inttypes.h>
63 #include <sys/kauth.h>
66 #include <net/netisr.h>
67 #include <net/if_types.h>
68 #include <net/route.h>
69 #include <net/ppp_defs.h>
71 #include <machine/stdarg.h>
73 #include <netinet/in.h>
74 #include <netinet/in_systm.h>
75 #include <netinet/in_var.h>
77 #include <netinet/ip.h>
78 #include <netinet/tcp.h>
80 #include <net/ethertypes.h>
83 #include <netinet6/scope6_var.h>
87 #include <netipx/ipx.h>
88 #include <netipx/ipx_if.h>
93 #include <netiso/argo_debug.h>
94 #include <netiso/iso.h>
95 #include <netiso/iso_var.h>
96 #include <netiso/iso_snpac.h>
99 #include <net/if_sppp.h>
100 #include <net/if_spppvar.h>
102 #define LCP_KEEPALIVE_INTERVAL 10 /* seconds between checks */
103 #define LOOPALIVECNT 3 /* loopback detection tries */
104 #define DEFAULT_MAXALIVECNT 3 /* max. missed alive packets */
105 #define DEFAULT_NORECV_TIME 15 /* before we get worried */
106 #define DEFAULT_MAX_AUTH_FAILURES 5 /* max. auth. failures */
109 * Interface flags that can be set in an ifconfig command.
111 * Setting link0 will make the link passive, i.e. it will be marked
112 * as being administrative openable, but won't be opened to begin
113 * with. Incoming calls will be answered, or subsequent calls with
114 * -link1 will cause the administrative open of the LCP layer.
116 * Setting link1 will cause the link to auto-dial only as packets
119 * Setting IFF_DEBUG will syslog the option negotiation and state
120 * transitions at level kern.debug. Note: all logs consistently look
123 * <if-name><unit>: <proto-name> <additional info...>
125 * with <if-name><unit> being something like "bppp0", and <proto-name>
126 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
129 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
130 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
132 #define CONF_REQ 1 /* PPP configure request */
133 #define CONF_ACK 2 /* PPP configure acknowledge */
134 #define CONF_NAK 3 /* PPP configure negative ack */
135 #define CONF_REJ 4 /* PPP configure reject */
136 #define TERM_REQ 5 /* PPP terminate request */
137 #define TERM_ACK 6 /* PPP terminate acknowledge */
138 #define CODE_REJ 7 /* PPP code reject */
139 #define PROTO_REJ 8 /* PPP protocol reject */
140 #define ECHO_REQ 9 /* PPP echo request */
141 #define ECHO_REPLY 10 /* PPP echo reply */
142 #define DISC_REQ 11 /* PPP discard request */
144 #define LCP_OPT_MRU 1 /* maximum receive unit */
145 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
146 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
147 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
148 #define LCP_OPT_MAGIC 5 /* magic number */
149 #define LCP_OPT_RESERVED 6 /* reserved */
150 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
151 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
153 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
154 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol */
155 #define IPCP_OPT_ADDRESS 3 /* local IP address */
156 #define IPCP_OPT_PRIMDNS 129 /* primary remote dns address */
157 #define IPCP_OPT_SECDNS 131 /* secondary remote dns address */
159 #define IPV6CP_OPT_IFID 1 /* interface identifier */
160 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
162 #define PAP_REQ 1 /* PAP name/password request */
163 #define PAP_ACK 2 /* PAP acknowledge */
164 #define PAP_NAK 3 /* PAP fail */
166 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
167 #define CHAP_RESPONSE 2 /* CHAP challenge response */
168 #define CHAP_SUCCESS 3 /* CHAP response ok */
169 #define CHAP_FAILURE 4 /* CHAP response failed */
171 #define CHAP_MD5 5 /* hash algorithm - MD5 */
173 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
174 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
175 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
176 #define CISCO_ADDR_REQ 0 /* Cisco address request */
177 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
178 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
180 /* states are named and numbered according to RFC 1661 */
181 #define STATE_INITIAL 0
182 #define STATE_STARTING 1
183 #define STATE_CLOSED 2
184 #define STATE_STOPPED 3
185 #define STATE_CLOSING 4
186 #define STATE_STOPPING 5
187 #define STATE_REQ_SENT 6
188 #define STATE_ACK_RCVD 7
189 #define STATE_ACK_SENT 8
190 #define STATE_OPENED 9
197 #define PPP_HEADER_LEN sizeof (struct ppp_header)
204 #define LCP_HEADER_LEN sizeof (struct lcp_header)
206 struct cisco_packet
{
214 #define CISCO_PACKET_LEN 18
217 * We follow the spelling and capitalization of RFC 1661 here, to make
218 * it easier comparing with the standard. Please refer to this RFC in
219 * case you can't make sense out of these abbreviation; it will also
220 * explain the semantics related to the various events and actions.
223 u_short proto
; /* PPP control protocol number */
224 u_char protoidx
; /* index into state table in struct sppp */
226 #define CP_LCP 0x01 /* this is the LCP */
227 #define CP_AUTH 0x02 /* this is an authentication protocol */
228 #define CP_NCP 0x04 /* this is a NCP */
229 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
230 const char *name
; /* name of this control protocol */
232 void (*Up
)(struct sppp
*sp
);
233 void (*Down
)(struct sppp
*sp
);
234 void (*Open
)(struct sppp
*sp
);
235 void (*Close
)(struct sppp
*sp
);
236 void (*TO
)(void *sp
);
237 int (*RCR
)(struct sppp
*sp
, struct lcp_header
*h
, int len
);
238 void (*RCN_rej
)(struct sppp
*sp
, struct lcp_header
*h
, int len
);
239 void (*RCN_nak
)(struct sppp
*sp
, struct lcp_header
*h
, int len
);
241 void (*tlu
)(struct sppp
*sp
);
242 void (*tld
)(struct sppp
*sp
);
243 void (*tls
)(struct sppp
*sp
);
244 void (*tlf
)(struct sppp
*sp
);
245 void (*scr
)(struct sppp
*sp
);
248 static struct sppp
*spppq
;
249 static callout_t keepalive_ch
;
253 * The following disgusting hack gets around the problem that IP TOS
254 * can't be set yet. We want to put "interactive" traffic on a high
255 * priority queue. To decide if traffic is interactive, we check that
256 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
258 * XXX is this really still necessary? - joerg -
260 static u_short interactive_ports
[8] = {
264 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
267 /* almost every function needs these */
269 struct ifnet *ifp = &sp->pp_if; \
270 int debug = ifp->if_flags & IFF_DEBUG
272 static int sppp_output(struct ifnet
*ifp
, struct mbuf
*m
,
273 const struct sockaddr
*dst
, struct rtentry
*rt
);
275 static void sppp_cisco_send(struct sppp
*sp
, int type
, int32_t par1
, int32_t par2
);
276 static void sppp_cisco_input(struct sppp
*sp
, struct mbuf
*m
);
278 static void sppp_cp_input(const struct cp
*cp
, struct sppp
*sp
,
280 static void sppp_cp_send(struct sppp
*sp
, u_short proto
, u_char type
,
281 u_char ident
, u_short len
, void *data
);
282 /* static void sppp_cp_timeout(void *arg); */
283 static void sppp_cp_change_state(const struct cp
*cp
, struct sppp
*sp
,
285 static void sppp_auth_send(const struct cp
*cp
,
286 struct sppp
*sp
, unsigned int type
, unsigned int id
,
289 static void sppp_up_event(const struct cp
*cp
, struct sppp
*sp
);
290 static void sppp_down_event(const struct cp
*cp
, struct sppp
*sp
);
291 static void sppp_open_event(const struct cp
*cp
, struct sppp
*sp
);
292 static void sppp_close_event(const struct cp
*cp
, struct sppp
*sp
);
293 static void sppp_to_event(const struct cp
*cp
, struct sppp
*sp
);
295 static void sppp_null(struct sppp
*sp
);
297 static void sppp_lcp_init(struct sppp
*sp
);
298 static void sppp_lcp_up(struct sppp
*sp
);
299 static void sppp_lcp_down(struct sppp
*sp
);
300 static void sppp_lcp_open(struct sppp
*sp
);
301 static void sppp_lcp_close(struct sppp
*sp
);
302 static void sppp_lcp_TO(void *sp
);
303 static int sppp_lcp_RCR(struct sppp
*sp
, struct lcp_header
*h
, int len
);
304 static void sppp_lcp_RCN_rej(struct sppp
*sp
, struct lcp_header
*h
, int len
);
305 static void sppp_lcp_RCN_nak(struct sppp
*sp
, struct lcp_header
*h
, int len
);
306 static void sppp_lcp_tlu(struct sppp
*sp
);
307 static void sppp_lcp_tld(struct sppp
*sp
);
308 static void sppp_lcp_tls(struct sppp
*sp
);
309 static void sppp_lcp_tlf(struct sppp
*sp
);
310 static void sppp_lcp_scr(struct sppp
*sp
);
311 static void sppp_lcp_check_and_close(struct sppp
*sp
);
312 static int sppp_ncp_check(struct sppp
*sp
);
314 static void sppp_ipcp_init(struct sppp
*sp
);
315 static void sppp_ipcp_up(struct sppp
*sp
);
316 static void sppp_ipcp_down(struct sppp
*sp
);
317 static void sppp_ipcp_open(struct sppp
*sp
);
318 static void sppp_ipcp_close(struct sppp
*sp
);
319 static void sppp_ipcp_TO(void *sp
);
320 static int sppp_ipcp_RCR(struct sppp
*sp
, struct lcp_header
*h
, int len
);
321 static void sppp_ipcp_RCN_rej(struct sppp
*sp
, struct lcp_header
*h
, int len
);
322 static void sppp_ipcp_RCN_nak(struct sppp
*sp
, struct lcp_header
*h
, int len
);
323 static void sppp_ipcp_tlu(struct sppp
*sp
);
324 static void sppp_ipcp_tld(struct sppp
*sp
);
325 static void sppp_ipcp_tls(struct sppp
*sp
);
326 static void sppp_ipcp_tlf(struct sppp
*sp
);
327 static void sppp_ipcp_scr(struct sppp
*sp
);
329 static void sppp_ipv6cp_init(struct sppp
*sp
);
330 static void sppp_ipv6cp_up(struct sppp
*sp
);
331 static void sppp_ipv6cp_down(struct sppp
*sp
);
332 static void sppp_ipv6cp_open(struct sppp
*sp
);
333 static void sppp_ipv6cp_close(struct sppp
*sp
);
334 static void sppp_ipv6cp_TO(void *sp
);
335 static int sppp_ipv6cp_RCR(struct sppp
*sp
, struct lcp_header
*h
, int len
);
336 static void sppp_ipv6cp_RCN_rej(struct sppp
*sp
, struct lcp_header
*h
, int len
);
337 static void sppp_ipv6cp_RCN_nak(struct sppp
*sp
, struct lcp_header
*h
, int len
);
338 static void sppp_ipv6cp_tlu(struct sppp
*sp
);
339 static void sppp_ipv6cp_tld(struct sppp
*sp
);
340 static void sppp_ipv6cp_tls(struct sppp
*sp
);
341 static void sppp_ipv6cp_tlf(struct sppp
*sp
);
342 static void sppp_ipv6cp_scr(struct sppp
*sp
);
344 static void sppp_pap_input(struct sppp
*sp
, struct mbuf
*m
);
345 static void sppp_pap_init(struct sppp
*sp
);
346 static void sppp_pap_open(struct sppp
*sp
);
347 static void sppp_pap_close(struct sppp
*sp
);
348 static void sppp_pap_TO(void *sp
);
349 static void sppp_pap_my_TO(void *sp
);
350 static void sppp_pap_tlu(struct sppp
*sp
);
351 static void sppp_pap_tld(struct sppp
*sp
);
352 static void sppp_pap_scr(struct sppp
*sp
);
354 static void sppp_chap_input(struct sppp
*sp
, struct mbuf
*m
);
355 static void sppp_chap_init(struct sppp
*sp
);
356 static void sppp_chap_open(struct sppp
*sp
);
357 static void sppp_chap_close(struct sppp
*sp
);
358 static void sppp_chap_TO(void *sp
);
359 static void sppp_chap_tlu(struct sppp
*sp
);
360 static void sppp_chap_tld(struct sppp
*sp
);
361 static void sppp_chap_scr(struct sppp
*sp
);
363 static const char *sppp_auth_type_name(u_short proto
, u_char type
);
364 static const char *sppp_cp_type_name(u_char type
);
365 static const char *sppp_dotted_quad(uint32_t addr
);
366 static const char *sppp_ipcp_opt_name(u_char opt
);
368 static const char *sppp_ipv6cp_opt_name(u_char opt
);
370 static const char *sppp_lcp_opt_name(u_char opt
);
371 static const char *sppp_phase_name(int phase
);
372 static const char *sppp_proto_name(u_short proto
);
373 static const char *sppp_state_name(int state
);
374 static int sppp_params(struct sppp
*sp
, u_long cmd
, void *data
);
376 static void sppp_get_ip_addrs(struct sppp
*sp
, uint32_t *src
, uint32_t *dst
,
378 static void sppp_set_ip_addrs(struct sppp
*sp
, uint32_t myaddr
, uint32_t hisaddr
);
379 static void sppp_clear_ip_addrs(struct sppp
*sp
);
381 static void sppp_keepalive(void *dummy
);
382 static void sppp_phase_network(struct sppp
*sp
);
383 static void sppp_print_bytes(const u_char
*p
, u_short len
);
384 static void sppp_print_string(const char *p
, u_short len
);
386 static void sppp_get_ip6_addrs(struct sppp
*sp
, struct in6_addr
*src
,
387 struct in6_addr
*dst
, struct in6_addr
*srcmask
);
388 #ifdef IPV6CP_MYIFID_DYN
389 static void sppp_set_ip6_addr(struct sppp
*sp
, const struct in6_addr
*src
);
390 static void sppp_gen_ip6_addr(struct sppp
*sp
, const struct in6_addr
*src
);
392 static void sppp_suggest_ip6_addr(struct sppp
*sp
, struct in6_addr
*src
);
395 /* our control protocol descriptors */
396 static const struct cp lcp
= {
397 PPP_LCP
, IDX_LCP
, CP_LCP
, "lcp",
398 sppp_lcp_up
, sppp_lcp_down
, sppp_lcp_open
, sppp_lcp_close
,
399 sppp_lcp_TO
, sppp_lcp_RCR
, sppp_lcp_RCN_rej
, sppp_lcp_RCN_nak
,
400 sppp_lcp_tlu
, sppp_lcp_tld
, sppp_lcp_tls
, sppp_lcp_tlf
,
404 static const struct cp ipcp
= {
407 CP_NCP
, /*don't run IPCP if there's no IPv4 support*/
412 sppp_ipcp_up
, sppp_ipcp_down
, sppp_ipcp_open
, sppp_ipcp_close
,
413 sppp_ipcp_TO
, sppp_ipcp_RCR
, sppp_ipcp_RCN_rej
, sppp_ipcp_RCN_nak
,
414 sppp_ipcp_tlu
, sppp_ipcp_tld
, sppp_ipcp_tls
, sppp_ipcp_tlf
,
418 static const struct cp ipv6cp
= {
419 PPP_IPV6CP
, IDX_IPV6CP
,
420 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
426 sppp_ipv6cp_up
, sppp_ipv6cp_down
, sppp_ipv6cp_open
, sppp_ipv6cp_close
,
427 sppp_ipv6cp_TO
, sppp_ipv6cp_RCR
, sppp_ipv6cp_RCN_rej
, sppp_ipv6cp_RCN_nak
,
428 sppp_ipv6cp_tlu
, sppp_ipv6cp_tld
, sppp_ipv6cp_tls
, sppp_ipv6cp_tlf
,
432 static const struct cp pap
= {
433 PPP_PAP
, IDX_PAP
, CP_AUTH
, "pap",
434 sppp_null
, sppp_null
, sppp_pap_open
, sppp_pap_close
,
435 sppp_pap_TO
, 0, 0, 0,
436 sppp_pap_tlu
, sppp_pap_tld
, sppp_null
, sppp_null
,
440 static const struct cp chap
= {
441 PPP_CHAP
, IDX_CHAP
, CP_AUTH
, "chap",
442 sppp_null
, sppp_null
, sppp_chap_open
, sppp_chap_close
,
443 sppp_chap_TO
, 0, 0, 0,
444 sppp_chap_tlu
, sppp_chap_tld
, sppp_null
, sppp_null
,
448 static const struct cp
*cps
[IDX_COUNT
] = {
450 &ipcp
, /* IDX_IPCP */
451 &ipv6cp
, /* IDX_IPV6CP */
453 &chap
, /* IDX_CHAP */
457 void spppattach(int);
460 spppattach(int count
)
465 * Exported functions, comprising our interface to the lower layer.
469 * Process the received packet.
472 sppp_input(struct ifnet
*ifp
, struct mbuf
*m
)
474 struct ppp_header
*h
= NULL
;
475 struct ifqueue
*inq
= 0;
478 struct sppp
*sp
= (struct sppp
*)ifp
;
479 int debug
= ifp
->if_flags
& IFF_DEBUG
;
481 if (ifp
->if_flags
& IFF_UP
) {
482 /* Count received bytes, add hardware framing */
483 ifp
->if_ibytes
+= m
->m_pkthdr
.len
+ sp
->pp_framebytes
;
484 /* Note time of last receive */
485 sp
->pp_last_receive
= time_uptime
;
488 if (m
->m_pkthdr
.len
<= PPP_HEADER_LEN
) {
489 /* Too small packet, drop it. */
492 "%s: input packet is too small, %d bytes\n",
493 ifp
->if_xname
, m
->m_pkthdr
.len
);
501 if (sp
->pp_flags
& PP_NOFRAMING
) {
502 memcpy(&protocol
, mtod(m
, void *), 2);
503 protocol
= ntohs(protocol
);
507 /* Get PPP header. */
508 h
= mtod(m
, struct ppp_header
*);
509 m_adj(m
, PPP_HEADER_LEN
);
511 switch (h
->address
) {
512 case PPP_ALLSTATIONS
:
513 if (h
->control
!= PPP_UI
)
515 if (sp
->pp_flags
& PP_CISCO
) {
518 "%s: PPP packet in Cisco mode "
519 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
521 h
->address
, h
->control
, ntohs(h
->protocol
));
525 case CISCO_MULTICAST
:
527 /* Don't check the control field here (RFC 1547). */
528 if (! (sp
->pp_flags
& PP_CISCO
)) {
531 "%s: Cisco packet in PPP mode "
532 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
534 h
->address
, h
->control
, ntohs(h
->protocol
));
537 switch (ntohs(h
->protocol
)) {
541 case CISCO_KEEPALIVE
:
542 sppp_cisco_input((struct sppp
*) ifp
, m
);
547 schednetisr(NETISR_IP
);
553 schednetisr(NETISR_IPV6
);
559 schednetisr(NETISR_IPX
);
565 default: /* Invalid PPP packet. */
569 "%s: invalid input packet "
570 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
572 h
->address
, h
->control
, ntohs(h
->protocol
));
575 protocol
= ntohs(h
->protocol
);
580 if (sp
->state
[IDX_LCP
] == STATE_OPENED
) {
581 uint16_t prot
= htons(protocol
);
582 sppp_cp_send(sp
, PPP_LCP
, PROTO_REJ
,
583 ++sp
->pp_seq
[IDX_LCP
], m
->m_pkthdr
.len
+ 2,
588 "%s: invalid input protocol "
589 "<proto=0x%x>\n", ifp
->if_xname
, ntohs(protocol
));
593 sppp_cp_input(&lcp
, sp
, m
);
597 if (sp
->pp_phase
>= SPPP_PHASE_AUTHENTICATE
)
598 sppp_pap_input(sp
, m
);
602 if (sp
->pp_phase
>= SPPP_PHASE_AUTHENTICATE
)
603 sppp_chap_input(sp
, m
);
608 if (sp
->pp_phase
== SPPP_PHASE_NETWORK
)
609 sppp_cp_input(&ipcp
, sp
, m
);
613 if (sp
->state
[IDX_IPCP
] == STATE_OPENED
) {
614 schednetisr(NETISR_IP
);
616 sp
->pp_last_activity
= time_uptime
;
622 if (sp
->pp_phase
== SPPP_PHASE_NETWORK
)
623 sppp_cp_input(&ipv6cp
, sp
, m
);
628 if (sp
->state
[IDX_IPV6CP
] == STATE_OPENED
) {
629 schednetisr(NETISR_IPV6
);
631 sp
->pp_last_activity
= time_uptime
;
637 /* IPX IPXCP not implemented yet */
638 if (sp
->pp_phase
== SPPP_PHASE_NETWORK
) {
639 schednetisr(NETISR_IPX
);
646 /* OSI NLCP not implemented yet */
647 if (sp
->pp_phase
== SPPP_PHASE_NETWORK
) {
648 schednetisr(NETISR_ISO
);
656 if (! (ifp
->if_flags
& IFF_UP
) || ! inq
)
662 /* Queue overflow. */
666 log(LOG_DEBUG
, "%s: protocol queue overflow\n",
675 * Enqueue transmit packet.
678 sppp_output(struct ifnet
*ifp
, struct mbuf
*m
,
679 const struct sockaddr
*dst
, struct rtentry
*rt
)
681 struct sppp
*sp
= (struct sppp
*) ifp
;
682 struct ppp_header
*h
= NULL
;
683 struct ifqueue
*ifq
= NULL
; /* XXX */
686 ALTQ_DECL(struct altq_pktattr pktattr
;)
690 sp
->pp_last_activity
= time_uptime
;
692 if ((ifp
->if_flags
& IFF_UP
) == 0 ||
693 (ifp
->if_flags
& (IFF_RUNNING
| IFF_AUTO
)) == 0) {
699 if ((ifp
->if_flags
& (IFF_RUNNING
| IFF_AUTO
)) == IFF_AUTO
) {
701 * Interface is not yet running, but auto-dial. Need
702 * to start LCP for it.
704 ifp
->if_flags
|= IFF_RUNNING
;
711 * If the queueing discipline needs packet classification,
712 * do it before prepending link headers.
714 IFQ_CLASSIFY(&ifp
->if_snd
, m
, dst
->sa_family
, &pktattr
);
717 if (dst
->sa_family
== AF_INET
) {
718 struct ip
*ip
= NULL
;
719 struct tcphdr
*th
= NULL
;
721 if (m
->m_len
>= sizeof(struct ip
)) {
722 ip
= mtod(m
, struct ip
*);
723 if (ip
->ip_p
== IPPROTO_TCP
&&
724 m
->m_len
>= sizeof(struct ip
) + (ip
->ip_hl
<< 2) +
725 sizeof(struct tcphdr
)) {
726 th
= (struct tcphdr
*)
727 ((char *)ip
+ (ip
->ip_hl
<< 2));
733 * When using dynamic local IP address assignment by using
734 * 0.0.0.0 as a local address, the first TCP session will
735 * not connect because the local TCP checksum is computed
736 * using 0.0.0.0 which will later become our real IP address
737 * so the TCP checksum computed at the remote end will
738 * become invalid. So we
739 * - don't let packets with src ip addr 0 thru
740 * - we flag TCP packets with src ip 0 as an error
742 if (ip
&& ip
->ip_src
.s_addr
== INADDR_ANY
) {
743 uint8_t proto
= ip
->ip_p
;
747 if (proto
== IPPROTO_TCP
)
748 return (EADDRNOTAVAIL
);
754 * Put low delay, telnet, rlogin and ftp control packets
755 * in front of the queue.
758 if (!IF_QFULL(&sp
->pp_fastq
) &&
759 ((ip
&& (ip
->ip_tos
& IPTOS_LOWDELAY
)) ||
760 (th
&& (INTERACTIVE(ntohs(th
->th_sport
)) ||
761 INTERACTIVE(ntohs(th
->th_dport
))))))
767 if (dst
->sa_family
== AF_INET6
) {
768 /* XXX do something tricky here? */
772 if ((sp
->pp_flags
& PP_NOFRAMING
) == 0) {
774 * Prepend general data packet PPP header. For now, IP only.
776 M_PREPEND(m
, PPP_HEADER_LEN
, M_DONTWAIT
);
778 if (ifp
->if_flags
& IFF_DEBUG
)
779 log(LOG_DEBUG
, "%s: no memory for transmit header\n",
786 * May want to check size of packet
787 * (albeit due to the implementation it's always enough)
789 h
= mtod(m
, struct ppp_header
*);
790 if (sp
->pp_flags
& PP_CISCO
) {
791 h
->address
= CISCO_UNICAST
; /* unicast address */
794 h
->address
= PPP_ALLSTATIONS
; /* broadcast address */
795 h
->control
= PPP_UI
; /* Unnumbered Info */
799 switch (dst
->sa_family
) {
801 case AF_INET
: /* Internet Protocol */
802 if (sp
->pp_flags
& PP_CISCO
)
803 protocol
= htons(ETHERTYPE_IP
);
806 * Don't choke with an ENETDOWN early. It's
807 * possible that we just started dialing out,
808 * so don't drop the packet immediately. If
809 * we notice that we run out of buffer space
810 * below, we will however remember that we are
811 * not ready to carry IP packets, and return
812 * ENETDOWN, as opposed to ENOBUFS.
814 protocol
= htons(PPP_IP
);
815 if (sp
->state
[IDX_IPCP
] != STATE_OPENED
)
821 case AF_INET6
: /* Internet Protocol version 6 */
822 if (sp
->pp_flags
& PP_CISCO
)
823 protocol
= htons(ETHERTYPE_IPV6
);
826 * Don't choke with an ENETDOWN early. It's
827 * possible that we just started dialing out,
828 * so don't drop the packet immediately. If
829 * we notice that we run out of buffer space
830 * below, we will however remember that we are
831 * not ready to carry IP packets, and return
832 * ENETDOWN, as opposed to ENOBUFS.
834 protocol
= htons(PPP_IPV6
);
835 if (sp
->state
[IDX_IPV6CP
] != STATE_OPENED
)
841 case AF_IPX
: /* Novell IPX Protocol */
842 protocol
= htons((sp
->pp_flags
& PP_CISCO
) ?
843 ETHERTYPE_IPX
: PPP_IPX
);
847 case AF_ISO
: /* ISO OSI Protocol */
848 if (sp
->pp_flags
& PP_CISCO
)
850 protocol
= htons(PPP_ISO
);
858 return (EAFNOSUPPORT
);
861 if (sp
->pp_flags
& PP_NOFRAMING
) {
862 M_PREPEND(m
, 2, M_DONTWAIT
);
864 if (ifp
->if_flags
& IFF_DEBUG
)
865 log(LOG_DEBUG
, "%s: no memory for transmit header\n",
871 *mtod(m
, uint16_t *) = protocol
;
873 h
->protocol
= protocol
;
877 error
= ifq_enqueue2(ifp
, ifq
, m ALTQ_COMMA
ALTQ_DECL(&pktattr
));
881 * Count output packets and bytes.
882 * The packet length includes header + additional hardware
883 * framing according to RFC 1333.
885 if (!(ifp
->if_flags
& IFF_OACTIVE
))
886 (*ifp
->if_start
)(ifp
);
887 ifp
->if_obytes
+= m
->m_pkthdr
.len
+ sp
->pp_framebytes
;
894 sppp_attach(struct ifnet
*ifp
)
896 struct sppp
*sp
= (struct sppp
*) ifp
;
898 /* Initialize keepalive handler. */
900 callout_init(&keepalive_ch
, 0);
901 callout_reset(&keepalive_ch
, hz
* LCP_KEEPALIVE_INTERVAL
, sppp_keepalive
, NULL
);
904 /* Insert new entry into the keepalive list. */
908 sp
->pp_if
.if_type
= IFT_PPP
;
909 sp
->pp_if
.if_output
= sppp_output
;
910 sp
->pp_fastq
.ifq_maxlen
= 32;
911 sp
->pp_cpq
.ifq_maxlen
= 20;
914 sp
->pp_last_activity
= 0;
915 sp
->pp_last_receive
= 0;
916 sp
->pp_maxalive
= DEFAULT_MAXALIVECNT
;
917 sp
->pp_max_noreceive
= DEFAULT_NORECV_TIME
;
918 sp
->pp_idle_timeout
= 0;
919 memset(&sp
->pp_seq
[0], 0, sizeof(sp
->pp_seq
));
920 memset(&sp
->pp_rseq
[0], 0, sizeof(sp
->pp_rseq
));
921 sp
->pp_auth_failures
= 0;
922 sp
->pp_max_auth_fail
= DEFAULT_MAX_AUTH_FAILURES
;
923 sp
->pp_phase
= SPPP_PHASE_DEAD
;
925 sp
->pp_down
= lcp
.Down
;
929 memset(&sp
->myauth
, 0, sizeof sp
->myauth
);
930 memset(&sp
->hisauth
, 0, sizeof sp
->hisauth
);
933 sppp_ipv6cp_init(sp
);
939 sppp_detach(struct ifnet
*ifp
)
941 struct sppp
**q
, *p
, *sp
= (struct sppp
*) ifp
;
943 /* Remove the entry from the keepalive list. */
944 for (q
= &spppq
; (p
= *q
); q
= &p
->pp_next
)
950 /* Stop keepalive handler. */
952 callout_stop(&keepalive_ch
);
955 callout_stop(&sp
->ch
[IDX_LCP
]);
956 callout_stop(&sp
->ch
[IDX_IPCP
]);
957 callout_stop(&sp
->ch
[IDX_PAP
]);
958 callout_stop(&sp
->ch
[IDX_CHAP
]);
960 callout_stop(&sp
->ch
[IDX_IPV6CP
]);
962 callout_stop(&sp
->pap_my_to_ch
);
964 /* free authentication info */
965 if (sp
->myauth
.name
) free(sp
->myauth
.name
, M_DEVBUF
);
966 if (sp
->myauth
.secret
) free(sp
->myauth
.secret
, M_DEVBUF
);
967 if (sp
->hisauth
.name
) free(sp
->hisauth
.name
, M_DEVBUF
);
968 if (sp
->hisauth
.secret
) free(sp
->hisauth
.secret
, M_DEVBUF
);
970 #if 0 /* done in if_detach() */
976 * Flush the interface output queue.
979 sppp_flush(struct ifnet
*ifp
)
981 struct sppp
*sp
= (struct sppp
*) ifp
;
983 IFQ_PURGE(&sp
->pp_if
.if_snd
);
984 IF_PURGE(&sp
->pp_fastq
);
985 IF_PURGE(&sp
->pp_cpq
);
989 * Check if the output queue is empty.
992 sppp_isempty(struct ifnet
*ifp
)
994 struct sppp
*sp
= (struct sppp
*) ifp
;
998 empty
= IF_IS_EMPTY(&sp
->pp_fastq
) && IF_IS_EMPTY(&sp
->pp_cpq
) &&
999 IFQ_IS_EMPTY(&sp
->pp_if
.if_snd
);
1005 * Get next packet to send.
1008 sppp_dequeue(struct ifnet
*ifp
)
1010 struct sppp
*sp
= (struct sppp
*) ifp
;
1016 * Process only the control protocol queue until we have at
1017 * least one NCP open.
1019 * Do always serve all three queues in Cisco mode.
1021 IF_DEQUEUE(&sp
->pp_cpq
, m
);
1023 (sppp_ncp_check(sp
) || (sp
->pp_flags
& PP_CISCO
) != 0)) {
1024 IF_DEQUEUE(&sp
->pp_fastq
, m
);
1026 IFQ_DEQUEUE(&sp
->pp_if
.if_snd
, m
);
1033 * Process an ioctl request. Called on low priority level.
1036 sppp_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
1038 struct lwp
*l
= curlwp
; /* XXX */
1039 struct ifreq
*ifr
= (struct ifreq
*) data
;
1040 struct sppp
*sp
= (struct sppp
*) ifp
;
1041 int s
, error
=0, going_up
, going_down
, newmode
;
1045 case SIOCSIFDSTADDR
:
1046 case SIOCINITIFADDR
:
1050 if ((error
= ifioctl_common(ifp
, cmd
, data
)) != 0)
1052 going_up
= ifp
->if_flags
& IFF_UP
&&
1053 (ifp
->if_flags
& IFF_RUNNING
) == 0;
1054 going_down
= (ifp
->if_flags
& IFF_UP
) == 0 &&
1055 ifp
->if_flags
& IFF_RUNNING
;
1056 newmode
= ifp
->if_flags
& (IFF_AUTO
| IFF_PASSIVE
);
1057 if (newmode
== (IFF_AUTO
| IFF_PASSIVE
)) {
1059 newmode
= IFF_PASSIVE
;
1060 ifp
->if_flags
&= ~IFF_AUTO
;
1063 if (going_up
|| going_down
)
1065 if (going_up
&& newmode
== 0) {
1066 /* neither auto-dial nor passive */
1067 ifp
->if_flags
|= IFF_RUNNING
;
1068 if (!(sp
->pp_flags
& PP_CISCO
))
1070 } else if (going_down
) {
1072 ifp
->if_flags
&= ~IFF_RUNNING
;
1078 if (ifr
->ifr_mtu
< PPP_MINMRU
||
1079 ifr
->ifr_mtu
> sp
->lcp
.their_mru
) {
1085 if ((error
= ifioctl_common(ifp
, cmd
, data
)) == ENETRESET
)
1092 case SPPPSETAUTHCFG
:
1095 case SPPPSETAUTHFAILURE
:
1096 case SPPPSETDNSOPTS
:
1097 case SPPPSETKEEPALIVE
:
1098 error
= kauth_authorize_network(l
->l_cred
,
1099 KAUTH_NETWORK_INTERFACE
,
1100 KAUTH_REQ_NETWORK_INTERFACE_SETPRIV
, ifp
, (void *)cmd
,
1104 error
= sppp_params(sp
, cmd
, data
);
1107 case SPPPGETAUTHCFG
:
1109 case SPPPGETAUTHFAILURES
:
1110 error
= kauth_authorize_network(l
->l_cred
,
1111 KAUTH_NETWORK_INTERFACE
,
1112 KAUTH_REQ_NETWORK_INTERFACE_GETPRIV
, ifp
, (void *)cmd
,
1116 error
= sppp_params(sp
, cmd
, data
);
1120 case SPPPGETSTATUSNCP
:
1122 case SPPPGETDNSOPTS
:
1123 case SPPPGETDNSADDRS
:
1124 case SPPPGETKEEPALIVE
:
1125 error
= sppp_params(sp
, cmd
, data
);
1129 error
= ifioctl_common(ifp
, cmd
, data
);
1138 * Cisco framing implementation.
1142 * Handle incoming Cisco keepalive protocol packets.
1145 sppp_cisco_input(struct sppp
*sp
, struct mbuf
*m
)
1148 struct cisco_packet
*h
;
1150 uint32_t me
, mymask
= 0; /* XXX: GCC */
1153 if (m
->m_pkthdr
.len
< CISCO_PACKET_LEN
) {
1156 "%s: cisco invalid packet length: %d bytes\n",
1157 ifp
->if_xname
, m
->m_pkthdr
.len
);
1160 h
= mtod(m
, struct cisco_packet
*);
1163 "%s: cisco input: %d bytes "
1164 "<0x%x 0x%x 0x%x 0x%x 0x%x-0x%x>\n",
1165 ifp
->if_xname
, m
->m_pkthdr
.len
,
1166 ntohl(h
->type
), h
->par1
, h
->par2
, (u_int
)h
->rel
,
1167 (u_int
)h
->time0
, (u_int
)h
->time1
);
1168 switch (ntohl(h
->type
)) {
1171 addlog("%s: cisco unknown packet type: 0x%x\n",
1172 ifp
->if_xname
, ntohl(h
->type
));
1174 case CISCO_ADDR_REPLY
:
1175 /* Reply on address request, ignore */
1177 case CISCO_KEEPALIVE_REQ
:
1178 sp
->pp_alivecnt
= 0;
1179 sp
->pp_rseq
[IDX_LCP
] = ntohl(h
->par1
);
1180 if (sp
->pp_seq
[IDX_LCP
] == sp
->pp_rseq
[IDX_LCP
]) {
1181 /* Local and remote sequence numbers are equal.
1182 * Probably, the line is in loopback mode. */
1183 if (sp
->pp_loopcnt
>= LOOPALIVECNT
) {
1184 printf ("%s: loopback\n",
1187 if (ifp
->if_flags
& IFF_UP
) {
1189 IF_PURGE(&sp
->pp_cpq
);
1194 /* Generate new local sequence number */
1195 sp
->pp_seq
[IDX_LCP
] = arc4random();
1199 if (! (ifp
->if_flags
& IFF_UP
) &&
1200 (ifp
->if_flags
& IFF_RUNNING
)) {
1204 case CISCO_ADDR_REQ
:
1206 sppp_get_ip_addrs(sp
, &me
, 0, &mymask
);
1208 sppp_cisco_send(sp
, CISCO_ADDR_REPLY
, me
, mymask
);
1215 * Send Cisco keepalive packet.
1218 sppp_cisco_send(struct sppp
*sp
, int type
, int32_t par1
, int32_t par2
)
1221 struct ppp_header
*h
;
1222 struct cisco_packet
*ch
;
1226 t
= time_uptime
* 1000;
1227 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1230 m
->m_pkthdr
.len
= m
->m_len
= PPP_HEADER_LEN
+ CISCO_PACKET_LEN
;
1231 m
->m_pkthdr
.rcvif
= 0;
1233 h
= mtod(m
, struct ppp_header
*);
1234 h
->address
= CISCO_MULTICAST
;
1236 h
->protocol
= htons(CISCO_KEEPALIVE
);
1238 ch
= (struct cisco_packet
*)(h
+ 1);
1239 ch
->type
= htonl(type
);
1240 ch
->par1
= htonl(par1
);
1241 ch
->par2
= htonl(par2
);
1244 ch
->time0
= htons((u_short
)(t
>> 16));
1245 ch
->time1
= htons((u_short
) t
);
1249 "%s: cisco output: <0x%x 0x%x 0x%x 0x%x 0x%x-0x%x>\n",
1250 ifp
->if_xname
, ntohl(ch
->type
), ch
->par1
,
1251 ch
->par2
, (u_int
)ch
->rel
, (u_int
)ch
->time0
,
1254 if (IF_QFULL(&sp
->pp_cpq
)) {
1255 IF_DROP(&sp
->pp_fastq
);
1256 IF_DROP(&ifp
->if_snd
);
1261 IF_ENQUEUE(&sp
->pp_cpq
, m
);
1262 if (! (ifp
->if_flags
& IFF_OACTIVE
))
1263 (*ifp
->if_start
)(ifp
);
1264 ifp
->if_obytes
+= m
->m_pkthdr
.len
+ sp
->pp_framebytes
;
1268 * PPP protocol implementation.
1272 * Send PPP control protocol packet.
1275 sppp_cp_send(struct sppp
*sp
, u_short proto
, u_char type
,
1276 u_char ident
, u_short len
, void *data
)
1279 struct lcp_header
*lh
;
1283 pkthdrlen
= (sp
->pp_flags
& PP_NOFRAMING
) ? 2 : PPP_HEADER_LEN
;
1285 if (len
> MHLEN
- pkthdrlen
- LCP_HEADER_LEN
)
1286 len
= MHLEN
- pkthdrlen
- LCP_HEADER_LEN
;
1287 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1290 m
->m_pkthdr
.len
= m
->m_len
= pkthdrlen
+ LCP_HEADER_LEN
+ len
;
1291 m
->m_pkthdr
.rcvif
= 0;
1293 if (sp
->pp_flags
& PP_NOFRAMING
) {
1294 *mtod(m
, uint16_t *) = htons(proto
);
1295 lh
= (struct lcp_header
*)(mtod(m
, uint8_t *) + 2);
1297 struct ppp_header
*h
;
1298 h
= mtod(m
, struct ppp_header
*);
1299 h
->address
= PPP_ALLSTATIONS
; /* broadcast address */
1300 h
->control
= PPP_UI
; /* Unnumbered Info */
1301 h
->protocol
= htons(proto
); /* Link Control Protocol */
1302 lh
= (struct lcp_header
*)(h
+ 1);
1306 lh
->len
= htons(LCP_HEADER_LEN
+ len
);
1308 bcopy (data
, lh
+ 1, len
);
1311 log(LOG_DEBUG
, "%s: %s output <%s id=0x%x len=%d",
1313 sppp_proto_name(proto
),
1314 sppp_cp_type_name(lh
->type
), lh
->ident
, ntohs(lh
->len
));
1316 sppp_print_bytes((u_char
*)(lh
+ 1), len
);
1319 if (IF_QFULL(&sp
->pp_cpq
)) {
1320 IF_DROP(&sp
->pp_fastq
);
1321 IF_DROP(&ifp
->if_snd
);
1326 IF_ENQUEUE(&sp
->pp_cpq
, m
);
1327 if (! (ifp
->if_flags
& IFF_OACTIVE
))
1328 (*ifp
->if_start
)(ifp
);
1329 ifp
->if_obytes
+= m
->m_pkthdr
.len
+ sp
->pp_framebytes
;
1333 * Handle incoming PPP control protocol packets.
1336 sppp_cp_input(const struct cp
*cp
, struct sppp
*sp
, struct mbuf
*m
)
1339 struct lcp_header
*h
;
1340 int printlen
, len
= m
->m_pkthdr
.len
;
1348 "%s: %s invalid packet length: %d bytes\n",
1349 ifp
->if_xname
, cp
->name
, len
);
1352 h
= mtod(m
, struct lcp_header
*);
1354 printlen
= ntohs(h
->len
);
1356 "%s: %s input(%s): <%s id=0x%x len=%d",
1357 ifp
->if_xname
, cp
->name
,
1358 sppp_state_name(sp
->state
[cp
->protoidx
]),
1359 sppp_cp_type_name(h
->type
), h
->ident
, printlen
);
1363 sppp_print_bytes((u_char
*)(h
+ 1), printlen
- 4);
1366 if (len
> ntohs(h
->len
))
1367 len
= ntohs(h
->len
);
1368 p
= (u_char
*)(h
+ 1);
1373 addlog("%s: %s invalid conf-req length %d\n",
1374 ifp
->if_xname
, cp
->name
,
1379 /* handle states where RCR doesn't get a SCA/SCN */
1380 switch (sp
->state
[cp
->protoidx
]) {
1382 case STATE_STOPPING
:
1385 sppp_cp_send(sp
, cp
->proto
, TERM_ACK
, h
->ident
,
1389 rv
= (cp
->RCR
)(sp
, h
, len
);
1391 /* fatal error, shut down */
1396 switch (sp
->state
[cp
->protoidx
]) {
1400 /* fall through... */
1401 case STATE_ACK_SENT
:
1402 case STATE_REQ_SENT
:
1403 sppp_cp_change_state(cp
, sp
, rv
?
1404 STATE_ACK_SENT
: STATE_REQ_SENT
);
1407 sp
->rst_counter
[cp
->protoidx
] = sp
->lcp
.max_configure
;
1409 sppp_cp_change_state(cp
, sp
, rv
?
1410 STATE_ACK_SENT
: STATE_REQ_SENT
);
1412 case STATE_ACK_RCVD
:
1414 sppp_cp_change_state(cp
, sp
, STATE_OPENED
);
1416 log(LOG_DEBUG
, "%s: %s tlu\n",
1421 sppp_cp_change_state(cp
, sp
, STATE_ACK_RCVD
);
1424 printf("%s: %s illegal %s in state %s\n",
1425 ifp
->if_xname
, cp
->name
,
1426 sppp_cp_type_name(h
->type
),
1427 sppp_state_name(sp
->state
[cp
->protoidx
]));
1432 if (h
->ident
!= sp
->confid
[cp
->protoidx
]) {
1434 addlog("%s: %s id mismatch 0x%x != 0x%x\n",
1435 ifp
->if_xname
, cp
->name
,
1436 h
->ident
, sp
->confid
[cp
->protoidx
]);
1440 switch (sp
->state
[cp
->protoidx
]) {
1443 sppp_cp_send(sp
, cp
->proto
, TERM_ACK
, h
->ident
, 0, 0);
1446 case STATE_STOPPING
:
1448 case STATE_REQ_SENT
:
1449 sp
->rst_counter
[cp
->protoidx
] = sp
->lcp
.max_configure
;
1450 sppp_cp_change_state(cp
, sp
, STATE_ACK_RCVD
);
1455 case STATE_ACK_RCVD
:
1457 sppp_cp_change_state(cp
, sp
, STATE_REQ_SENT
);
1459 case STATE_ACK_SENT
:
1460 sp
->rst_counter
[cp
->protoidx
] = sp
->lcp
.max_configure
;
1461 sppp_cp_change_state(cp
, sp
, STATE_OPENED
);
1463 log(LOG_DEBUG
, "%s: %s tlu\n",
1464 ifp
->if_xname
, cp
->name
);
1468 printf("%s: %s illegal %s in state %s\n",
1469 ifp
->if_xname
, cp
->name
,
1470 sppp_cp_type_name(h
->type
),
1471 sppp_state_name(sp
->state
[cp
->protoidx
]));
1477 if (h
->ident
!= sp
->confid
[cp
->protoidx
]) {
1479 addlog("%s: %s id mismatch 0x%x != 0x%x\n",
1480 ifp
->if_xname
, cp
->name
,
1481 h
->ident
, sp
->confid
[cp
->protoidx
]);
1485 if (h
->type
== CONF_NAK
)
1486 (cp
->RCN_nak
)(sp
, h
, len
);
1488 (cp
->RCN_rej
)(sp
, h
, len
);
1490 switch (sp
->state
[cp
->protoidx
]) {
1493 sppp_cp_send(sp
, cp
->proto
, TERM_ACK
, h
->ident
, 0, 0);
1495 case STATE_REQ_SENT
:
1496 case STATE_ACK_SENT
:
1497 sp
->rst_counter
[cp
->protoidx
] = sp
->lcp
.max_configure
;
1503 case STATE_ACK_RCVD
:
1504 sppp_cp_change_state(cp
, sp
, STATE_ACK_SENT
);
1508 case STATE_STOPPING
:
1511 printf("%s: %s illegal %s in state %s\n",
1512 ifp
->if_xname
, cp
->name
,
1513 sppp_cp_type_name(h
->type
),
1514 sppp_state_name(sp
->state
[cp
->protoidx
]));
1520 switch (sp
->state
[cp
->protoidx
]) {
1521 case STATE_ACK_RCVD
:
1522 case STATE_ACK_SENT
:
1523 sppp_cp_change_state(cp
, sp
, STATE_REQ_SENT
);
1528 case STATE_STOPPING
:
1529 case STATE_REQ_SENT
:
1531 /* Send Terminate-Ack packet. */
1533 log(LOG_DEBUG
, "%s: %s send terminate-ack\n",
1534 ifp
->if_xname
, cp
->name
);
1535 sppp_cp_send(sp
, cp
->proto
, TERM_ACK
, h
->ident
, 0, 0);
1539 sp
->rst_counter
[cp
->protoidx
] = 0;
1540 sppp_cp_change_state(cp
, sp
, STATE_STOPPING
);
1543 printf("%s: %s illegal %s in state %s\n",
1544 ifp
->if_xname
, cp
->name
,
1545 sppp_cp_type_name(h
->type
),
1546 sppp_state_name(sp
->state
[cp
->protoidx
]));
1551 switch (sp
->state
[cp
->protoidx
]) {
1554 case STATE_REQ_SENT
:
1555 case STATE_ACK_SENT
:
1559 sppp_cp_change_state(cp
, sp
, STATE_CLOSED
);
1560 sppp_lcp_check_and_close(sp
);
1562 case STATE_STOPPING
:
1564 sppp_cp_change_state(cp
, sp
, STATE_STOPPED
);
1565 sppp_lcp_check_and_close(sp
);
1567 case STATE_ACK_RCVD
:
1568 sppp_cp_change_state(cp
, sp
, STATE_REQ_SENT
);
1573 sppp_cp_change_state(cp
, sp
, STATE_ACK_RCVD
);
1576 printf("%s: %s illegal %s in state %s\n",
1577 ifp
->if_xname
, cp
->name
,
1578 sppp_cp_type_name(h
->type
),
1579 sppp_state_name(sp
->state
[cp
->protoidx
]));
1584 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1586 "%s: %s: ignoring RXJ (%s) for code ?, "
1587 "danger will robinson\n",
1588 ifp
->if_xname
, cp
->name
,
1589 sppp_cp_type_name(h
->type
));
1590 switch (sp
->state
[cp
->protoidx
]) {
1593 case STATE_REQ_SENT
:
1594 case STATE_ACK_SENT
:
1596 case STATE_STOPPING
:
1599 case STATE_ACK_RCVD
:
1600 sppp_cp_change_state(cp
, sp
, STATE_REQ_SENT
);
1603 printf("%s: %s illegal %s in state %s\n",
1604 ifp
->if_xname
, cp
->name
,
1605 sppp_cp_type_name(h
->type
),
1606 sppp_state_name(sp
->state
[cp
->protoidx
]));
1613 const struct cp
*upper
;
1619 proto
= p
[0] << 8 | p
[1];
1620 for (i
= 0; i
< IDX_COUNT
; i
++) {
1621 if (cps
[i
]->proto
== proto
) {
1631 "%s: %s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1632 ifp
->if_xname
, cp
->name
, catastrophic
? '-' : '+',
1633 sppp_cp_type_name(h
->type
), proto
,
1634 upper
? upper
->name
: "unknown",
1635 upper
? sppp_state_name(sp
->state
[upper
->protoidx
]) : "?");
1638 * if we got RXJ+ against conf-req, the peer does not implement
1639 * this particular protocol type. terminate the protocol.
1641 if (upper
&& !catastrophic
) {
1642 if (sp
->state
[upper
->protoidx
] == STATE_REQ_SENT
) {
1648 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1649 switch (sp
->state
[cp
->protoidx
]) {
1652 case STATE_REQ_SENT
:
1653 case STATE_ACK_SENT
:
1655 case STATE_STOPPING
:
1658 case STATE_ACK_RCVD
:
1659 sppp_cp_change_state(cp
, sp
, STATE_REQ_SENT
);
1662 printf("%s: %s illegal %s in state %s\n",
1663 ifp
->if_xname
, cp
->name
,
1664 sppp_cp_type_name(h
->type
),
1665 sppp_state_name(sp
->state
[cp
->protoidx
]));
1671 if (cp
->proto
!= PPP_LCP
)
1673 /* Discard the packet. */
1676 if (cp
->proto
!= PPP_LCP
)
1678 if (sp
->state
[cp
->protoidx
] != STATE_OPENED
) {
1680 addlog("%s: lcp echo req but lcp closed\n",
1687 addlog("%s: invalid lcp echo request "
1688 "packet length: %d bytes\n",
1689 ifp
->if_xname
, len
);
1692 memcpy(&u32
, h
+ 1, sizeof u32
);
1693 if (ntohl(u32
) == sp
->lcp
.magic
) {
1694 /* Line loopback mode detected. */
1695 printf("%s: loopback\n", ifp
->if_xname
);
1697 IF_PURGE(&sp
->pp_cpq
);
1699 /* Shut down the PPP link. */
1705 u32
= htonl(sp
->lcp
.magic
);
1706 memcpy(h
+ 1, &u32
, sizeof u32
);
1708 addlog("%s: got lcp echo req, sending echo rep\n",
1710 sppp_cp_send(sp
, PPP_LCP
, ECHO_REPLY
, h
->ident
, len
- 4,
1714 if (cp
->proto
!= PPP_LCP
)
1716 if (h
->ident
!= sp
->lcp
.echoid
) {
1722 addlog("%s: lcp invalid echo reply "
1723 "packet length: %d bytes\n",
1724 ifp
->if_xname
, len
);
1728 addlog("%s: lcp got echo rep\n",
1730 memcpy(&u32
, h
+ 1, sizeof u32
);
1731 if (ntohl(u32
) != sp
->lcp
.magic
)
1732 sp
->pp_alivecnt
= 0;
1735 /* Unknown packet type -- send Code-Reject packet. */
1738 addlog("%s: %s send code-rej for 0x%x\n",
1739 ifp
->if_xname
, cp
->name
, h
->type
);
1740 sppp_cp_send(sp
, cp
->proto
, CODE_REJ
,
1741 ++sp
->pp_seq
[cp
->protoidx
], m
->m_pkthdr
.len
, h
);
1748 * The generic part of all Up/Down/Open/Close/TO event handlers.
1749 * Basically, the state transition handling in the automaton.
1752 sppp_up_event(const struct cp
*cp
, struct sppp
*sp
)
1757 log(LOG_DEBUG
, "%s: %s up(%s)\n",
1758 ifp
->if_xname
, cp
->name
,
1759 sppp_state_name(sp
->state
[cp
->protoidx
]));
1761 switch (sp
->state
[cp
->protoidx
]) {
1763 sppp_cp_change_state(cp
, sp
, STATE_CLOSED
);
1765 case STATE_STARTING
:
1766 sp
->rst_counter
[cp
->protoidx
] = sp
->lcp
.max_configure
;
1768 sppp_cp_change_state(cp
, sp
, STATE_REQ_SENT
);
1771 printf("%s: %s illegal up in state %s\n",
1772 ifp
->if_xname
, cp
->name
,
1773 sppp_state_name(sp
->state
[cp
->protoidx
]));
1778 sppp_down_event(const struct cp
*cp
, struct sppp
*sp
)
1783 log(LOG_DEBUG
, "%s: %s down(%s)\n",
1784 ifp
->if_xname
, cp
->name
,
1785 sppp_state_name(sp
->state
[cp
->protoidx
]));
1787 switch (sp
->state
[cp
->protoidx
]) {
1790 sppp_cp_change_state(cp
, sp
, STATE_INITIAL
);
1795 case STATE_STOPPING
:
1796 case STATE_REQ_SENT
:
1797 case STATE_ACK_RCVD
:
1798 case STATE_ACK_SENT
:
1799 sppp_cp_change_state(cp
, sp
, STATE_STARTING
);
1803 sppp_cp_change_state(cp
, sp
, STATE_STARTING
);
1806 printf("%s: %s illegal down in state %s\n",
1807 ifp
->if_xname
, cp
->name
,
1808 sppp_state_name(sp
->state
[cp
->protoidx
]));
1814 sppp_open_event(const struct cp
*cp
, struct sppp
*sp
)
1819 log(LOG_DEBUG
, "%s: %s open(%s)\n",
1820 ifp
->if_xname
, cp
->name
,
1821 sppp_state_name(sp
->state
[cp
->protoidx
]));
1823 switch (sp
->state
[cp
->protoidx
]) {
1825 sppp_cp_change_state(cp
, sp
, STATE_STARTING
);
1828 case STATE_STARTING
:
1831 sp
->rst_counter
[cp
->protoidx
] = sp
->lcp
.max_configure
;
1833 sppp_cp_change_state(cp
, sp
, STATE_REQ_SENT
);
1836 case STATE_STOPPING
:
1837 case STATE_REQ_SENT
:
1838 case STATE_ACK_RCVD
:
1839 case STATE_ACK_SENT
:
1843 sppp_cp_change_state(cp
, sp
, STATE_STOPPING
);
1850 sppp_close_event(const struct cp
*cp
, struct sppp
*sp
)
1855 log(LOG_DEBUG
, "%s: %s close(%s)\n",
1856 ifp
->if_xname
, cp
->name
,
1857 sppp_state_name(sp
->state
[cp
->protoidx
]));
1859 switch (sp
->state
[cp
->protoidx
]) {
1864 case STATE_STARTING
:
1865 sppp_cp_change_state(cp
, sp
, STATE_INITIAL
);
1869 sppp_cp_change_state(cp
, sp
, STATE_CLOSED
);
1871 case STATE_STOPPING
:
1872 sppp_cp_change_state(cp
, sp
, STATE_CLOSING
);
1877 case STATE_REQ_SENT
:
1878 case STATE_ACK_RCVD
:
1879 case STATE_ACK_SENT
:
1880 sp
->rst_counter
[cp
->protoidx
] = sp
->lcp
.max_terminate
;
1881 sppp_cp_send(sp
, cp
->proto
, TERM_REQ
,
1882 ++sp
->pp_seq
[cp
->protoidx
], 0, 0);
1883 sppp_cp_change_state(cp
, sp
, STATE_CLOSING
);
1889 sppp_to_event(const struct cp
*cp
, struct sppp
*sp
)
1896 log(LOG_DEBUG
, "%s: %s TO(%s) rst_counter = %d\n",
1897 ifp
->if_xname
, cp
->name
,
1898 sppp_state_name(sp
->state
[cp
->protoidx
]),
1899 sp
->rst_counter
[cp
->protoidx
]);
1901 if (--sp
->rst_counter
[cp
->protoidx
] < 0)
1903 switch (sp
->state
[cp
->protoidx
]) {
1906 sppp_cp_change_state(cp
, sp
, STATE_CLOSED
);
1907 sppp_lcp_check_and_close(sp
);
1909 case STATE_STOPPING
:
1911 sppp_cp_change_state(cp
, sp
, STATE_STOPPED
);
1912 sppp_lcp_check_and_close(sp
);
1914 case STATE_REQ_SENT
:
1915 case STATE_ACK_RCVD
:
1916 case STATE_ACK_SENT
:
1918 sppp_cp_change_state(cp
, sp
, STATE_STOPPED
);
1919 sppp_lcp_check_and_close(sp
);
1924 switch (sp
->state
[cp
->protoidx
]) {
1926 case STATE_STOPPING
:
1927 sppp_cp_send(sp
, cp
->proto
, TERM_REQ
,
1928 ++sp
->pp_seq
[cp
->protoidx
], 0, 0);
1929 callout_reset(&sp
->ch
[cp
->protoidx
], sp
->lcp
.timeout
,
1932 case STATE_REQ_SENT
:
1933 case STATE_ACK_RCVD
:
1935 /* sppp_cp_change_state() will restart the timer */
1936 sppp_cp_change_state(cp
, sp
, STATE_REQ_SENT
);
1938 case STATE_ACK_SENT
:
1940 callout_reset(&sp
->ch
[cp
->protoidx
], sp
->lcp
.timeout
,
1949 * Change the state of a control protocol in the state automaton.
1950 * Takes care of starting/stopping the restart timer.
1953 sppp_cp_change_state(const struct cp
*cp
, struct sppp
*sp
, int newstate
)
1955 sp
->state
[cp
->protoidx
] = newstate
;
1956 callout_stop(&sp
->ch
[cp
->protoidx
]);
1959 case STATE_STARTING
:
1965 case STATE_STOPPING
:
1966 case STATE_REQ_SENT
:
1967 case STATE_ACK_RCVD
:
1968 case STATE_ACK_SENT
:
1969 callout_reset(&sp
->ch
[cp
->protoidx
], sp
->lcp
.timeout
,
1976 *--------------------------------------------------------------------------*
1978 * The LCP implementation. *
1980 *--------------------------------------------------------------------------*
1983 sppp_lcp_init(struct sppp
*sp
)
1985 sp
->lcp
.opts
= (1 << LCP_OPT_MAGIC
);
1987 sp
->state
[IDX_LCP
] = STATE_INITIAL
;
1988 sp
->fail_counter
[IDX_LCP
] = 0;
1989 sp
->pp_seq
[IDX_LCP
] = 0;
1990 sp
->pp_rseq
[IDX_LCP
] = 0;
1992 if (sp
->pp_if
.if_mtu
< PP_MTU
) {
1993 sp
->lcp
.mru
= sp
->pp_if
.if_mtu
;
1994 sp
->lcp
.opts
|= (1 << LCP_OPT_MRU
);
1996 sp
->lcp
.mru
= PP_MTU
;
1997 sp
->lcp
.their_mru
= PP_MTU
;
2000 * Initialize counters and timeout values. Note that we don't
2001 * use the 3 seconds suggested in RFC 1661 since we are likely
2002 * running on a fast link. XXX We should probably implement
2003 * the exponential backoff option. Note that these values are
2004 * relevant for all control protocols, not just LCP only.
2006 sp
->lcp
.timeout
= 1 * hz
;
2007 sp
->lcp
.max_terminate
= 2;
2008 sp
->lcp
.max_configure
= 10;
2009 sp
->lcp
.max_failure
= 10;
2010 callout_init(&sp
->ch
[IDX_LCP
], 0);
2014 sppp_lcp_up(struct sppp
*sp
)
2018 /* Initialize activity timestamp: opening a connection is an activity */
2019 sp
->pp_last_receive
= sp
->pp_last_activity
= time_uptime
;
2022 * If this interface is passive or dial-on-demand, and we are
2023 * still in Initial state, it means we've got an incoming
2024 * call. Activate the interface.
2026 if ((ifp
->if_flags
& (IFF_AUTO
| IFF_PASSIVE
)) != 0) {
2029 "%s: Up event", ifp
->if_xname
);
2030 ifp
->if_flags
|= IFF_RUNNING
;
2031 if (sp
->state
[IDX_LCP
] == STATE_INITIAL
) {
2033 addlog("(incoming call)\n");
2034 sp
->pp_flags
|= PP_CALLIN
;
2038 } else if ((ifp
->if_flags
& (IFF_AUTO
| IFF_PASSIVE
)) == 0 &&
2039 (sp
->state
[IDX_LCP
] == STATE_INITIAL
)) {
2040 ifp
->if_flags
|= IFF_RUNNING
;
2044 sppp_up_event(&lcp
, sp
);
2048 sppp_lcp_down(struct sppp
*sp
)
2052 sppp_down_event(&lcp
, sp
);
2055 * If this is neither a dial-on-demand nor a passive
2056 * interface, simulate an ``ifconfig down'' action, so the
2057 * administrator can force a redial by another ``ifconfig
2058 * up''. XXX For leased line operation, should we immediately
2059 * try to reopen the connection here?
2061 if ((ifp
->if_flags
& (IFF_AUTO
| IFF_PASSIVE
)) == 0) {
2064 "%s: Down event (carrier loss), taking interface down.\n",
2070 "%s: Down event (carrier loss)\n",
2073 sp
->pp_flags
&= ~PP_CALLIN
;
2074 if (sp
->state
[IDX_LCP
] != STATE_INITIAL
)
2076 ifp
->if_flags
&= ~IFF_RUNNING
;
2080 sppp_lcp_open(struct sppp
*sp
)
2083 * If we are authenticator, negotiate LCP_AUTH
2085 if (sp
->hisauth
.proto
!= 0)
2086 sp
->lcp
.opts
|= (1 << LCP_OPT_AUTH_PROTO
);
2088 sp
->lcp
.opts
&= ~(1 << LCP_OPT_AUTH_PROTO
);
2089 sp
->pp_flags
&= ~PP_NEEDAUTH
;
2090 sppp_open_event(&lcp
, sp
);
2094 sppp_lcp_close(struct sppp
*sp
)
2096 sppp_close_event(&lcp
, sp
);
2100 sppp_lcp_TO(void *cookie
)
2102 sppp_to_event(&lcp
, (struct sppp
*)cookie
);
2106 * Analyze a configure request. Return true if it was agreeable, and
2107 * caused action sca, false if it has been rejected or nak'ed, and
2108 * caused action scn. (The return value is used to make the state
2109 * transition decision in the state automaton.)
2112 sppp_lcp_RCR(struct sppp
*sp
, struct lcp_header
*h
, int len
)
2115 u_char
*buf
, *r
, *p
;
2122 buf
= r
= malloc (len
, M_TEMP
, M_NOWAIT
);
2127 log(LOG_DEBUG
, "%s: lcp parse opts:",
2130 /* pass 1: check for things that need to be rejected */
2131 p
= (void *)(h
+ 1);
2132 for (rlen
=0; len
>1 && p
[1]; len
-=p
[1], p
+=p
[1]) {
2133 /* Sanity check option length */
2136 * Malicious option - drop immediately.
2137 * XXX Maybe we should just RXJ it?
2139 addlog("%s: received malicious LCP option 0x%02x, "
2140 "length 0x%02x, (len: 0x%02x) dropping.\n", ifp
->if_xname
,
2145 addlog(" %s", sppp_lcp_opt_name(*p
));
2149 /* fall through, both are same length */
2150 case LCP_OPT_ASYNC_MAP
:
2151 /* Async control character map. */
2152 if (len
>= 6 || p
[1] == 6)
2155 addlog(" [invalid]");
2158 /* Maximum receive unit. */
2159 if (len
>= 4 && p
[1] == 4)
2162 addlog(" [invalid]");
2164 case LCP_OPT_AUTH_PROTO
:
2167 addlog(" [invalid]");
2170 authproto
= (p
[2] << 8) + p
[3];
2171 if (authproto
== PPP_CHAP
&& p
[1] != 5) {
2173 addlog(" [invalid chap len]");
2176 if (sp
->myauth
.proto
== 0) {
2177 /* we are not configured to do auth */
2179 addlog(" [not configured]");
2183 * Remote want us to authenticate, remember this,
2184 * so we stay in SPPP_PHASE_AUTHENTICATE after LCP got
2187 sp
->pp_flags
|= PP_NEEDAUTH
;
2190 /* Others not supported. */
2195 /* Add the option to rejected list. */
2202 addlog(" send conf-rej\n");
2203 sppp_cp_send(sp
, PPP_LCP
, CONF_REJ
, h
->ident
, rlen
, buf
);
2209 * pass 2: check for option values that are unacceptable and
2210 * thus require to be nak'ed.
2213 log(LOG_DEBUG
, "%s: lcp parse opt values: ",
2216 p
= (void *)(h
+ 1);
2218 for (rlen
=0; len
>1 && p
[1]; len
-=p
[1], p
+=p
[1]) {
2220 addlog(" %s", sppp_lcp_opt_name(*p
));
2223 /* Magic number -- extract. */
2224 nmagic
= (uint32_t)p
[2] << 24 |
2225 (uint32_t)p
[3] << 16 | p
[4] << 8 | p
[5];
2226 if (nmagic
!= sp
->lcp
.magic
) {
2228 addlog(" 0x%x", nmagic
);
2232 * Local and remote magics equal -- loopback?
2234 if (sp
->pp_loopcnt
>= LOOPALIVECNT
*5) {
2235 printf ("%s: loopback\n",
2238 if (ifp
->if_flags
& IFF_UP
) {
2240 IF_PURGE(&sp
->pp_cpq
);
2246 addlog(" [glitch]");
2249 * We negate our magic here, and NAK it. If
2250 * we see it later in an NAK packet, we
2251 * suggest a new one.
2253 nmagic
= ~sp
->lcp
.magic
;
2255 p
[2] = nmagic
>> 24;
2256 p
[3] = nmagic
>> 16;
2261 case LCP_OPT_ASYNC_MAP
:
2263 * Async control character map -- just ignore it.
2265 * Quote from RFC 1662, chapter 6:
2266 * To enable this functionality, synchronous PPP
2267 * implementations MUST always respond to the
2268 * Async-Control-Character-Map Configuration
2269 * Option with the LCP Configure-Ack. However,
2270 * acceptance of the Configuration Option does
2271 * not imply that the synchronous implementation
2272 * will do any ACCM mapping. Instead, all such
2273 * octet mapping will be performed by the
2274 * asynchronous-to-synchronous converter.
2280 * Maximum receive unit. Always agreeable,
2281 * but ignored by now.
2283 sp
->lcp
.their_mru
= p
[2] * 256 + p
[3];
2285 addlog(" %ld", sp
->lcp
.their_mru
);
2288 case LCP_OPT_AUTH_PROTO
:
2289 authproto
= (p
[2] << 8) + p
[3];
2290 if (sp
->myauth
.proto
!= authproto
) {
2291 /* not agreed, nak */
2293 addlog(" [mine %s != his %s]",
2294 sppp_proto_name(sp
->myauth
.proto
),
2295 sppp_proto_name(authproto
));
2296 p
[2] = sp
->myauth
.proto
>> 8;
2297 p
[3] = sp
->myauth
.proto
;
2300 if (authproto
== PPP_CHAP
&& p
[4] != CHAP_MD5
) {
2302 addlog(" [chap not MD5]");
2308 /* Add the option to nak'ed list. */
2314 if (++sp
->fail_counter
[IDX_LCP
] >= sp
->lcp
.max_failure
) {
2316 addlog(" max_failure (%d) exceeded, "
2318 sp
->lcp
.max_failure
);
2319 sppp_cp_send(sp
, PPP_LCP
, CONF_REJ
, h
->ident
, rlen
, buf
);
2322 addlog(" send conf-nak\n");
2323 sppp_cp_send(sp
, PPP_LCP
, CONF_NAK
, h
->ident
, rlen
, buf
);
2328 addlog(" send conf-ack\n");
2329 sp
->fail_counter
[IDX_LCP
] = 0;
2331 sppp_cp_send(sp
, PPP_LCP
, CONF_ACK
, h
->ident
, origlen
, h
+ 1);
2344 * Analyze the LCP Configure-Reject option list, and adjust our
2348 sppp_lcp_RCN_rej(struct sppp
*sp
, struct lcp_header
*h
, int len
)
2354 buf
= malloc (len
, M_TEMP
, M_NOWAIT
);
2359 log(LOG_DEBUG
, "%s: lcp rej opts:",
2362 p
= (void *)(h
+ 1);
2363 for (; len
> 1 && p
[1]; len
-= p
[1], p
+= p
[1]) {
2364 /* Sanity check option length */
2367 * Malicious option - drop immediately.
2368 * XXX Maybe we should just RXJ it?
2370 addlog("%s: received malicious LCP option, "
2371 "dropping.\n", ifp
->if_xname
);
2375 addlog(" %s", sppp_lcp_opt_name(*p
));
2378 /* Magic number -- can't use it, use 0 */
2379 sp
->lcp
.opts
&= ~(1 << LCP_OPT_MAGIC
);
2384 * We try to negotiate a lower MRU if the underlying
2385 * link's MTU is less than PP_MTU (e.g. PPPoE). If the
2386 * peer rejects this lower rate, fallback to the
2390 addlog("%s: warning: peer rejected our MRU of "
2391 "%ld bytes. Defaulting to %d bytes\n",
2392 ifp
->if_xname
, sp
->lcp
.mru
, PP_MTU
);
2394 sp
->lcp
.opts
&= ~(1 << LCP_OPT_MRU
);
2395 sp
->lcp
.mru
= PP_MTU
;
2397 case LCP_OPT_AUTH_PROTO
:
2399 * Peer doesn't want to authenticate himself,
2400 * deny unless this is a dialout call, and
2401 * SPPP_AUTHFLAG_NOCALLOUT is set.
2403 if ((sp
->pp_flags
& PP_CALLIN
) == 0 &&
2404 (sp
->hisauth
.flags
& SPPP_AUTHFLAG_NOCALLOUT
) != 0) {
2406 addlog(" [don't insist on auth "
2408 sp
->lcp
.opts
&= ~(1 << LCP_OPT_AUTH_PROTO
);
2412 addlog("[access denied]\n");
2425 * Analyze the LCP Configure-NAK option list, and adjust our
2429 sppp_lcp_RCN_nak(struct sppp
*sp
, struct lcp_header
*h
, int len
)
2436 buf
= malloc (len
, M_TEMP
, M_NOWAIT
);
2441 log(LOG_DEBUG
, "%s: lcp nak opts:",
2444 p
= (void *)(h
+ 1);
2445 for (; len
> 1 && p
[1]; len
-= p
[1], p
+= p
[1]) {
2446 /* Sanity check option length */
2449 * Malicious option - drop immediately.
2450 * XXX Maybe we should just RXJ it?
2452 addlog("%s: received malicious LCP option, "
2453 "dropping.\n", ifp
->if_xname
);
2457 addlog(" %s", sppp_lcp_opt_name(*p
));
2460 /* Magic number -- renegotiate */
2461 if ((sp
->lcp
.opts
& (1 << LCP_OPT_MAGIC
)) &&
2462 len
>= 6 && p
[1] == 6) {
2463 magic
= (uint32_t)p
[2] << 24 |
2464 (uint32_t)p
[3] << 16 | p
[4] << 8 | p
[5];
2466 * If the remote magic is our negated one,
2467 * this looks like a loopback problem.
2468 * Suggest a new magic to make sure.
2470 if (magic
== ~sp
->lcp
.magic
) {
2472 addlog(" magic glitch");
2473 sp
->lcp
.magic
= arc4random();
2475 sp
->lcp
.magic
= magic
;
2477 addlog(" %d", magic
);
2483 * Peer wants to advise us to negotiate an MRU.
2484 * Agree on it if it's reasonable, or use
2485 * default otherwise.
2487 if (len
>= 4 && p
[1] == 4) {
2488 u_int mru
= p
[2] * 256 + p
[3];
2491 if (mru
< PPP_MINMRU
|| mru
> sp
->pp_if
.if_mtu
)
2492 mru
= sp
->pp_if
.if_mtu
;
2494 sp
->lcp
.opts
|= (1 << LCP_OPT_MRU
);
2497 case LCP_OPT_AUTH_PROTO
:
2499 * Peer doesn't like our authentication method,
2503 addlog("[access denied]\n");
2516 sppp_lcp_tlu(struct sppp
*sp
)
2523 if (! (ifp
->if_flags
& IFF_UP
) &&
2524 (ifp
->if_flags
& IFF_RUNNING
)) {
2525 /* Coming out of loopback mode. */
2529 for (i
= 0; i
< IDX_COUNT
; i
++)
2530 if ((cps
[i
])->flags
& CP_QUAL
)
2533 if ((sp
->lcp
.opts
& (1 << LCP_OPT_AUTH_PROTO
)) != 0 ||
2534 (sp
->pp_flags
& PP_NEEDAUTH
) != 0)
2535 sp
->pp_phase
= SPPP_PHASE_AUTHENTICATE
;
2537 sp
->pp_phase
= SPPP_PHASE_NETWORK
;
2541 log(LOG_INFO
, "%s: phase %s\n", ifp
->if_xname
,
2542 sppp_phase_name(sp
->pp_phase
));
2546 * Open all authentication protocols. This is even required
2547 * if we already proceeded to network phase, since it might be
2548 * that remote wants us to authenticate, so we might have to
2549 * send a PAP request. Undesired authentication protocols
2550 * don't do anything when they get an Open event.
2552 for (i
= 0; i
< IDX_COUNT
; i
++)
2553 if ((cps
[i
])->flags
& CP_AUTH
)
2556 if (sp
->pp_phase
== SPPP_PHASE_NETWORK
) {
2557 /* Notify all NCPs. */
2558 for (i
= 0; i
< IDX_COUNT
; i
++)
2559 if ((cps
[i
])->flags
& CP_NCP
)
2563 /* Send Up events to all started protos. */
2564 for (i
= 0, mask
= 1; i
< IDX_COUNT
; i
++, mask
<<= 1)
2565 if ((sp
->lcp
.protos
& mask
) && ((cps
[i
])->flags
& CP_LCP
) == 0)
2568 /* notify low-level driver of state change */
2570 sp
->pp_chg(sp
, (int)sp
->pp_phase
);
2572 if (sp
->pp_phase
== SPPP_PHASE_NETWORK
)
2573 /* if no NCP is starting, close down */
2574 sppp_lcp_check_and_close(sp
);
2578 sppp_lcp_tld(struct sppp
*sp
)
2584 sp
->pp_phase
= SPPP_PHASE_TERMINATE
;
2588 log(LOG_INFO
, "%s: phase %s\n", ifp
->if_xname
,
2589 sppp_phase_name(sp
->pp_phase
));
2593 * Take upper layers down. We send the Down event first and
2594 * the Close second to prevent the upper layers from sending
2595 * ``a flurry of terminate-request packets'', as the RFC
2598 for (i
= 0, mask
= 1; i
< IDX_COUNT
; i
++, mask
<<= 1)
2599 if ((sp
->lcp
.protos
& mask
) && ((cps
[i
])->flags
& CP_LCP
) == 0) {
2601 (cps
[i
])->Close(sp
);
2606 sppp_lcp_tls(struct sppp
*sp
)
2610 if (sp
->pp_max_auth_fail
!= 0 && sp
->pp_auth_failures
>= sp
->pp_max_auth_fail
) {
2611 printf("%s: authentication failed %d times, not retrying again\n",
2612 sp
->pp_if
.if_xname
, sp
->pp_auth_failures
);
2613 if_down(&sp
->pp_if
);
2617 sp
->pp_phase
= SPPP_PHASE_ESTABLISH
;
2621 log(LOG_INFO
, "%s: phase %s\n", ifp
->if_xname
,
2622 sppp_phase_name(sp
->pp_phase
));
2625 /* Notify lower layer if desired. */
2631 sppp_lcp_tlf(struct sppp
*sp
)
2635 sp
->pp_phase
= SPPP_PHASE_DEAD
;
2639 log(LOG_INFO
, "%s: phase %s\n", ifp
->if_xname
,
2640 sppp_phase_name(sp
->pp_phase
));
2643 /* Notify lower layer if desired. */
2649 sppp_lcp_scr(struct sppp
*sp
)
2651 char opt
[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2655 if (sp
->lcp
.opts
& (1 << LCP_OPT_MAGIC
)) {
2656 if (! sp
->lcp
.magic
)
2657 sp
->lcp
.magic
= arc4random();
2658 opt
[i
++] = LCP_OPT_MAGIC
;
2660 opt
[i
++] = sp
->lcp
.magic
>> 24;
2661 opt
[i
++] = sp
->lcp
.magic
>> 16;
2662 opt
[i
++] = sp
->lcp
.magic
>> 8;
2663 opt
[i
++] = sp
->lcp
.magic
;
2666 if (sp
->lcp
.opts
& (1 << LCP_OPT_MRU
)) {
2667 opt
[i
++] = LCP_OPT_MRU
;
2669 opt
[i
++] = sp
->lcp
.mru
>> 8;
2670 opt
[i
++] = sp
->lcp
.mru
;
2673 if (sp
->lcp
.opts
& (1 << LCP_OPT_AUTH_PROTO
)) {
2674 authproto
= sp
->hisauth
.proto
;
2675 opt
[i
++] = LCP_OPT_AUTH_PROTO
;
2676 opt
[i
++] = authproto
== PPP_CHAP
? 5: 4;
2677 opt
[i
++] = authproto
>> 8;
2678 opt
[i
++] = authproto
;
2679 if (authproto
== PPP_CHAP
)
2680 opt
[i
++] = CHAP_MD5
;
2683 sp
->confid
[IDX_LCP
] = ++sp
->pp_seq
[IDX_LCP
];
2684 sppp_cp_send(sp
, PPP_LCP
, CONF_REQ
, sp
->confid
[IDX_LCP
], i
, &opt
);
2688 * Check the open NCPs, return true if at least one NCP is open.
2691 sppp_ncp_check(struct sppp
*sp
)
2695 for (i
= 0, mask
= 1; i
< IDX_COUNT
; i
++, mask
<<= 1)
2696 if ((sp
->lcp
.protos
& mask
) && (cps
[i
])->flags
& CP_NCP
)
2702 * Re-check the open NCPs and see if we should terminate the link.
2703 * Called by the NCPs during their tlf action handling.
2706 sppp_lcp_check_and_close(struct sppp
*sp
)
2709 if (sp
->pp_phase
< SPPP_PHASE_NETWORK
)
2710 /* don't bother, we are already going down */
2713 if (sppp_ncp_check(sp
))
2721 *--------------------------------------------------------------------------*
2723 * The IPCP implementation. *
2725 *--------------------------------------------------------------------------*
2729 sppp_ipcp_init(struct sppp
*sp
)
2733 sp
->state
[IDX_IPCP
] = STATE_INITIAL
;
2734 sp
->fail_counter
[IDX_IPCP
] = 0;
2735 sp
->pp_seq
[IDX_IPCP
] = 0;
2736 sp
->pp_rseq
[IDX_IPCP
] = 0;
2737 callout_init(&sp
->ch
[IDX_IPCP
], 0);
2741 sppp_ipcp_up(struct sppp
*sp
)
2743 sppp_up_event(&ipcp
, sp
);
2747 sppp_ipcp_down(struct sppp
*sp
)
2749 sppp_down_event(&ipcp
, sp
);
2753 sppp_ipcp_open(struct sppp
*sp
)
2756 uint32_t myaddr
, hisaddr
;
2758 sp
->ipcp
.flags
&= ~(IPCP_HISADDR_SEEN
|IPCP_MYADDR_SEEN
|IPCP_MYADDR_DYN
|IPCP_HISADDR_DYN
);
2759 sp
->ipcp
.req_myaddr
= 0;
2760 sp
->ipcp
.req_hisaddr
= 0;
2761 memset(&sp
->dns_addrs
, 0, sizeof sp
->dns_addrs
);
2764 sppp_get_ip_addrs(sp
, &myaddr
, &hisaddr
, 0);
2766 myaddr
= hisaddr
= 0;
2769 * If we don't have his address, this probably means our
2770 * interface doesn't want to talk IP at all. (This could
2771 * be the case if somebody wants to speak only IPX, for
2772 * example.) Don't open IPCP in this case.
2775 /* XXX this message should go away */
2777 log(LOG_DEBUG
, "%s: ipcp_open(): no IP interface\n",
2784 * I don't have an assigned address, so i need to
2785 * negotiate my address.
2787 sp
->ipcp
.flags
|= IPCP_MYADDR_DYN
;
2788 sp
->ipcp
.opts
|= (1 << IPCP_OPT_ADDRESS
);
2792 * XXX - remove this hack!
2793 * remote has no valid address, we need to get one assigned.
2795 sp
->ipcp
.flags
|= IPCP_HISADDR_DYN
;
2797 sppp_open_event(&ipcp
, sp
);
2801 sppp_ipcp_close(struct sppp
*sp
)
2805 sppp_close_event(&ipcp
, sp
);
2807 if (sp
->ipcp
.flags
& (IPCP_MYADDR_DYN
|IPCP_HISADDR_DYN
))
2809 * Some address was dynamic, clear it again.
2811 sppp_clear_ip_addrs(sp
);
2814 if (sp
->pp_saved_mtu
> 0) {
2815 ifp
->if_mtu
= sp
->pp_saved_mtu
;
2816 sp
->pp_saved_mtu
= 0;
2819 "%s: resetting MTU to %" PRIu64
" bytes\n",
2820 ifp
->if_xname
, ifp
->if_mtu
);
2825 sppp_ipcp_TO(void *cookie
)
2827 sppp_to_event(&ipcp
, (struct sppp
*)cookie
);
2831 * Analyze a configure request. Return true if it was agreeable, and
2832 * caused action sca, false if it has been rejected or nak'ed, and
2833 * caused action scn. (The return value is used to make the state
2834 * transition decision in the state automaton.)
2837 sppp_ipcp_RCR(struct sppp
*sp
, struct lcp_header
*h
, int len
)
2839 u_char
*buf
, *r
, *p
;
2840 struct ifnet
*ifp
= &sp
->pp_if
;
2841 int rlen
, origlen
, debug
= ifp
->if_flags
& IFF_DEBUG
;
2842 uint32_t hisaddr
, desiredaddr
;
2847 * Make sure to allocate a buf that can at least hold a
2848 * conf-nak with an `address' option. We might need it below.
2850 buf
= r
= malloc ((len
< 6? 6: len
), M_TEMP
, M_NOWAIT
);
2854 /* pass 1: see if we can recognize them */
2856 log(LOG_DEBUG
, "%s: ipcp parse opts:",
2858 p
= (void *)(h
+ 1);
2859 for (rlen
=0; len
>1 && p
[1]; len
-=p
[1], p
+=p
[1]) {
2860 /* Sanity check option length */
2862 /* XXX should we just RXJ? */
2863 addlog("%s: malicious IPCP option received, dropping\n",
2868 addlog(" %s", sppp_ipcp_opt_name(*p
));
2871 case IPCP_OPT_COMPRESSION
:
2872 if (len
>= 6 && p
[1] >= 6) {
2873 /* correctly formed compress option */
2877 addlog(" [invalid]");
2880 case IPCP_OPT_ADDRESS
:
2881 if (len
>= 6 && p
[1] == 6) {
2882 /* correctly formed address option */
2886 addlog(" [invalid]");
2889 /* Others not supported. */
2894 /* Add the option to rejected list. */
2901 addlog(" send conf-rej\n");
2902 sppp_cp_send(sp
, PPP_IPCP
, CONF_REJ
, h
->ident
, rlen
, buf
);
2907 /* pass 2: parse option values */
2908 if (sp
->ipcp
.flags
& IPCP_HISADDR_SEEN
)
2909 hisaddr
= sp
->ipcp
.req_hisaddr
; /* we already aggreed on that */
2912 sppp_get_ip_addrs(sp
, 0, &hisaddr
, 0); /* user configuration */
2917 log(LOG_DEBUG
, "%s: ipcp parse opt values: ",
2919 p
= (void *)(h
+ 1);
2921 for (rlen
=0; len
>1 && p
[1]; len
-=p
[1], p
+=p
[1]) {
2923 addlog(" %s", sppp_ipcp_opt_name(*p
));
2926 case IPCP_OPT_COMPRESSION
:
2929 case IPCP_OPT_ADDRESS
:
2930 desiredaddr
= p
[2] << 24 | p
[3] << 16 |
2932 if (desiredaddr
== hisaddr
||
2933 ((sp
->ipcp
.flags
& IPCP_HISADDR_DYN
) && desiredaddr
!= 0)) {
2935 * Peer's address is same as our value,
2936 * this is agreeable. Gonna conf-ack
2941 sppp_dotted_quad(hisaddr
));
2942 /* record that we've seen it already */
2943 sp
->ipcp
.flags
|= IPCP_HISADDR_SEEN
;
2944 sp
->ipcp
.req_hisaddr
= desiredaddr
;
2945 hisaddr
= desiredaddr
;
2949 * The address wasn't agreeable. This is either
2950 * he sent us 0.0.0.0, asking to assign him an
2951 * address, or he send us another address not
2952 * matching our value. Either case, we gonna
2953 * conf-nak it with our value.
2956 if (desiredaddr
== 0)
2957 addlog(" [addr requested]");
2959 addlog(" %s [not agreed]",
2960 sppp_dotted_quad(desiredaddr
));
2963 p
[2] = hisaddr
>> 24;
2964 p
[3] = hisaddr
>> 16;
2965 p
[4] = hisaddr
>> 8;
2969 /* Add the option to nak'ed list. */
2976 * If we are about to conf-ack the request, but haven't seen
2977 * his address so far, gonna conf-nak it instead, with the
2978 * `address' option present and our idea of his address being
2979 * filled in there, to request negotiation of both addresses.
2981 * XXX This can result in an endless req - nak loop if peer
2982 * doesn't want to send us his address. Q: What should we do
2983 * about it? XXX A: implement the max-failure counter.
2985 if (rlen
== 0 && !(sp
->ipcp
.flags
& IPCP_HISADDR_SEEN
)) {
2986 buf
[0] = IPCP_OPT_ADDRESS
;
2988 buf
[2] = hisaddr
>> 24;
2989 buf
[3] = hisaddr
>> 16;
2990 buf
[4] = hisaddr
>> 8;
2994 addlog(" still need hisaddr");
2999 addlog(" send conf-nak\n");
3000 sppp_cp_send(sp
, PPP_IPCP
, CONF_NAK
, h
->ident
, rlen
, buf
);
3003 addlog(" send conf-ack\n");
3004 sppp_cp_send(sp
, PPP_IPCP
, CONF_ACK
, h
->ident
, origlen
, h
+ 1);
3017 * Analyze the IPCP Configure-Reject option list, and adjust our
3021 sppp_ipcp_RCN_rej(struct sppp
*sp
, struct lcp_header
*h
, int len
)
3024 struct ifnet
*ifp
= &sp
->pp_if
;
3025 int debug
= ifp
->if_flags
& IFF_DEBUG
;
3028 buf
= malloc (len
, M_TEMP
, M_NOWAIT
);
3033 log(LOG_DEBUG
, "%s: ipcp rej opts:",
3036 p
= (void *)(h
+ 1);
3037 for (; len
> 1 && p
[1]; len
-= p
[1], p
+= p
[1]) {
3038 /* Sanity check option length */
3040 /* XXX should we just RXJ? */
3041 addlog("%s: malicious IPCP option received, dropping\n",
3046 addlog(" %s", sppp_ipcp_opt_name(*p
));
3048 case IPCP_OPT_ADDRESS
:
3050 * Peer doesn't grok address option. This is
3051 * bad. XXX Should we better give up here?
3053 sp
->ipcp
.opts
&= ~(1 << IPCP_OPT_ADDRESS
);
3056 case IPCP_OPT_COMPRESS
:
3057 sp
->ipcp
.opts
&= ~(1 << IPCP_OPT_COMPRESS
);
3070 * Analyze the IPCP Configure-NAK option list, and adjust our
3074 sppp_ipcp_RCN_nak(struct sppp
*sp
, struct lcp_header
*h
, int len
)
3077 struct ifnet
*ifp
= &sp
->pp_if
;
3078 int debug
= ifp
->if_flags
& IFF_DEBUG
;
3084 log(LOG_DEBUG
, "%s: ipcp nak opts:",
3087 p
= (void *)(h
+ 1);
3088 for (; len
> 1 && p
[1]; len
-= p
[1], p
+= p
[1]) {
3089 /* Sanity check option length */
3091 /* XXX should we just RXJ? */
3092 addlog("%s: malicious IPCP option received, dropping\n",
3097 addlog(" %s", sppp_ipcp_opt_name(*p
));
3099 case IPCP_OPT_ADDRESS
:
3101 * Peer doesn't like our local IP address. See
3102 * if we can do something for him. We'll drop
3103 * him our address then.
3105 if (len
>= 6 && p
[1] == 6) {
3106 wantaddr
= p
[2] << 24 | p
[3] << 16 |
3108 sp
->ipcp
.opts
|= (1 << IPCP_OPT_ADDRESS
);
3110 addlog(" [wantaddr %s]",
3111 sppp_dotted_quad(wantaddr
));
3113 * When doing dynamic address assignment,
3114 * we accept his offer. Otherwise, we
3115 * ignore it and thus continue to negotiate
3116 * our already existing value.
3118 if (sp
->ipcp
.flags
& IPCP_MYADDR_DYN
) {
3121 sp
->ipcp
.flags
|= IPCP_MYADDR_SEEN
;
3122 sp
->ipcp
.req_myaddr
= wantaddr
;
3127 case IPCP_OPT_PRIMDNS
:
3128 if (len
>= 6 && p
[1] == 6) {
3129 sp
->dns_addrs
[0] = p
[2] << 24 | p
[3] << 16 |
3134 case IPCP_OPT_SECDNS
:
3135 if (len
>= 6 && p
[1] == 6) {
3136 sp
->dns_addrs
[1] = p
[2] << 24 | p
[3] << 16 |
3141 case IPCP_OPT_COMPRESS
:
3143 * Peer wants different compression parameters.
3154 sppp_ipcp_tlu(struct sppp
*sp
)
3157 /* we are up. Set addresses and notify anyone interested */
3159 uint32_t myaddr
, hisaddr
;
3161 sppp_get_ip_addrs(sp
, &myaddr
, &hisaddr
, 0);
3162 if ((sp
->ipcp
.flags
& IPCP_MYADDR_DYN
) && (sp
->ipcp
.flags
& IPCP_MYADDR_SEEN
))
3163 myaddr
= sp
->ipcp
.req_myaddr
;
3164 if ((sp
->ipcp
.flags
& IPCP_HISADDR_DYN
) && (sp
->ipcp
.flags
& IPCP_HISADDR_SEEN
))
3165 hisaddr
= sp
->ipcp
.req_hisaddr
;
3166 sppp_set_ip_addrs(sp
, myaddr
, hisaddr
);
3168 if (ifp
->if_mtu
> sp
->lcp
.their_mru
) {
3169 sp
->pp_saved_mtu
= ifp
->if_mtu
;
3170 ifp
->if_mtu
= sp
->lcp
.their_mru
;
3173 "%s: setting MTU to %" PRIu64
" bytes\n",
3174 ifp
->if_xname
, ifp
->if_mtu
);
3183 sppp_ipcp_tld(struct sppp
*sp
)
3188 sppp_ipcp_tls(struct sppp
*sp
)
3190 /* indicate to LCP that it must stay alive */
3191 sp
->lcp
.protos
|= (1 << IDX_IPCP
);
3195 sppp_ipcp_tlf(struct sppp
*sp
)
3197 /* we no longer need LCP */
3198 sp
->lcp
.protos
&= ~(1 << IDX_IPCP
);
3202 sppp_ipcp_scr(struct sppp
*sp
)
3204 char opt
[6 /* compression */ + 6 /* address */ + 12 /* dns addresses */];
3211 if (sp
->ipcp
.opts
& (1 << IPCP_OPT_COMPRESSION
)) {
3212 opt
[i
++] = IPCP_OPT_COMPRESSION
;
3214 opt
[i
++] = 0; /* VJ header compression */
3215 opt
[i
++] = 0x2d; /* VJ header compression */
3216 opt
[i
++] = max_slot_id
;
3217 opt
[i
++] = comp_slot_id
;
3222 if (sp
->ipcp
.opts
& (1 << IPCP_OPT_ADDRESS
)) {
3223 if (sp
->ipcp
.flags
& IPCP_MYADDR_SEEN
)
3224 ouraddr
= sp
->ipcp
.req_myaddr
; /* not sure if this can ever happen */
3226 sppp_get_ip_addrs(sp
, &ouraddr
, 0, 0);
3227 opt
[i
++] = IPCP_OPT_ADDRESS
;
3229 opt
[i
++] = ouraddr
>> 24;
3230 opt
[i
++] = ouraddr
>> 16;
3231 opt
[i
++] = ouraddr
>> 8;
3236 if (sp
->query_dns
& 1) {
3237 opt
[i
++] = IPCP_OPT_PRIMDNS
;
3239 opt
[i
++] = sp
->dns_addrs
[0] >> 24;
3240 opt
[i
++] = sp
->dns_addrs
[0] >> 16;
3241 opt
[i
++] = sp
->dns_addrs
[0] >> 8;
3242 opt
[i
++] = sp
->dns_addrs
[0];
3244 if (sp
->query_dns
& 2) {
3245 opt
[i
++] = IPCP_OPT_SECDNS
;
3247 opt
[i
++] = sp
->dns_addrs
[1] >> 24;
3248 opt
[i
++] = sp
->dns_addrs
[1] >> 16;
3249 opt
[i
++] = sp
->dns_addrs
[1] >> 8;
3250 opt
[i
++] = sp
->dns_addrs
[1];
3253 sp
->confid
[IDX_IPCP
] = ++sp
->pp_seq
[IDX_IPCP
];
3254 sppp_cp_send(sp
, PPP_IPCP
, CONF_REQ
, sp
->confid
[IDX_IPCP
], i
, &opt
);
3259 *--------------------------------------------------------------------------*
3261 * The IPv6CP implementation. *
3263 *--------------------------------------------------------------------------*
3268 sppp_ipv6cp_init(struct sppp
*sp
)
3270 sp
->ipv6cp
.opts
= 0;
3271 sp
->ipv6cp
.flags
= 0;
3272 sp
->state
[IDX_IPV6CP
] = STATE_INITIAL
;
3273 sp
->fail_counter
[IDX_IPV6CP
] = 0;
3274 sp
->pp_seq
[IDX_IPV6CP
] = 0;
3275 sp
->pp_rseq
[IDX_IPV6CP
] = 0;
3276 callout_init(&sp
->ch
[IDX_IPV6CP
], 0);
3280 sppp_ipv6cp_up(struct sppp
*sp
)
3282 sppp_up_event(&ipv6cp
, sp
);
3286 sppp_ipv6cp_down(struct sppp
*sp
)
3288 sppp_down_event(&ipv6cp
, sp
);
3292 sppp_ipv6cp_open(struct sppp
*sp
)
3295 struct in6_addr myaddr
, hisaddr
;
3297 #ifdef IPV6CP_MYIFID_DYN
3298 sp
->ipv6cp
.flags
&= ~(IPV6CP_MYIFID_SEEN
|IPV6CP_MYIFID_DYN
);
3300 sp
->ipv6cp
.flags
&= ~IPV6CP_MYIFID_SEEN
;
3303 sppp_get_ip6_addrs(sp
, &myaddr
, &hisaddr
, 0);
3305 * If we don't have our address, this probably means our
3306 * interface doesn't want to talk IPv6 at all. (This could
3307 * be the case if somebody wants to speak only IPX, for
3308 * example.) Don't open IPv6CP in this case.
3310 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr
)) {
3311 /* XXX this message should go away */
3313 log(LOG_DEBUG
, "%s: ipv6cp_open(): no IPv6 interface\n",
3318 sp
->ipv6cp
.flags
|= IPV6CP_MYIFID_SEEN
;
3319 sp
->ipv6cp
.opts
|= (1 << IPV6CP_OPT_IFID
);
3320 sppp_open_event(&ipv6cp
, sp
);
3324 sppp_ipv6cp_close(struct sppp
*sp
)
3326 sppp_close_event(&ipv6cp
, sp
);
3330 sppp_ipv6cp_TO(void *cookie
)
3332 sppp_to_event(&ipv6cp
, (struct sppp
*)cookie
);
3336 * Analyze a configure request. Return true if it was agreeable, and
3337 * caused action sca, false if it has been rejected or nak'ed, and
3338 * caused action scn. (The return value is used to make the state
3339 * transition decision in the state automaton.)
3342 sppp_ipv6cp_RCR(struct sppp
*sp
, struct lcp_header
*h
, int len
)
3344 u_char
*buf
, *r
, *p
;
3345 struct ifnet
*ifp
= &sp
->pp_if
;
3346 int rlen
, origlen
, debug
= ifp
->if_flags
& IFF_DEBUG
;
3347 struct in6_addr myaddr
, desiredaddr
, suggestaddr
;
3350 int collision
, nohisaddr
;
3355 * Make sure to allocate a buf that can at least hold a
3356 * conf-nak with an `address' option. We might need it below.
3358 buf
= r
= malloc ((len
< 6? 6: len
), M_TEMP
, M_NOWAIT
);
3362 /* pass 1: see if we can recognize them */
3364 log(LOG_DEBUG
, "%s: ipv6cp parse opts:",
3366 p
= (void *)(h
+ 1);
3368 for (rlen
=0; len
>1 && p
[1]; len
-=p
[1], p
+=p
[1]) {
3369 /* Sanity check option length */
3372 addlog("%s: received malicious IPCPv6 option, "
3373 "dropping\n", ifp
->if_xname
);
3377 addlog(" %s", sppp_ipv6cp_opt_name(*p
));
3379 case IPV6CP_OPT_IFID
:
3380 if (len
>= 10 && p
[1] == 10 && ifidcount
== 0) {
3381 /* correctly formed address option */
3386 addlog(" [invalid]");
3389 case IPV6CP_OPT_COMPRESSION
:
3390 if (len
>= 4 && p
[1] >= 4) {
3391 /* correctly formed compress option */
3395 addlog(" [invalid]");
3399 /* Others not supported. */
3404 /* Add the option to rejected list. */
3411 addlog(" send conf-rej\n");
3412 sppp_cp_send(sp
, PPP_IPV6CP
, CONF_REJ
, h
->ident
, rlen
, buf
);
3417 /* pass 2: parse option values */
3418 sppp_get_ip6_addrs(sp
, &myaddr
, 0, 0);
3420 log(LOG_DEBUG
, "%s: ipv6cp parse opt values: ",
3422 p
= (void *)(h
+ 1);
3425 for (rlen
=0; len
>1 && p
[1]; len
-=p
[1], p
+=p
[1]) {
3427 addlog(" %s", sppp_ipv6cp_opt_name(*p
));
3430 case IPV6CP_OPT_COMPRESSION
:
3433 case IPV6CP_OPT_IFID
:
3434 memset(&desiredaddr
, 0, sizeof(desiredaddr
));
3435 memcpy(&desiredaddr
.s6_addr
[8], &p
[2], 8);
3436 collision
= (memcmp(&desiredaddr
.s6_addr
[8],
3437 &myaddr
.s6_addr
[8], 8) == 0);
3438 nohisaddr
= IN6_IS_ADDR_UNSPECIFIED(&desiredaddr
);
3440 desiredaddr
.s6_addr16
[0] = htons(0xfe80);
3441 (void)in6_setscope(&desiredaddr
, &sp
->pp_if
, NULL
);
3443 if (!collision
&& !nohisaddr
) {
3444 /* no collision, hisaddr known - Conf-Ack */
3449 ip6_sprintf(&desiredaddr
),
3450 sppp_cp_type_name(type
));
3455 memset(&suggestaddr
, 0, sizeof(suggestaddr
));
3456 if (collision
&& nohisaddr
) {
3457 /* collision, hisaddr unknown - Conf-Rej */
3459 memset(&p
[2], 0, 8);
3462 * - no collision, hisaddr unknown, or
3463 * - collision, hisaddr known
3464 * Conf-Nak, suggest hisaddr
3467 sppp_suggest_ip6_addr(sp
, &suggestaddr
);
3468 memcpy(&p
[2], &suggestaddr
.s6_addr
[8], 8);
3471 addlog(" %s [%s]", ip6_sprintf(&desiredaddr
),
3472 sppp_cp_type_name(type
));
3475 /* Add the option to nak'ed list. */
3481 if (rlen
== 0 && type
== CONF_ACK
) {
3483 addlog(" send %s\n", sppp_cp_type_name(type
));
3484 sppp_cp_send(sp
, PPP_IPV6CP
, type
, h
->ident
, origlen
, h
+ 1);
3487 if (type
== CONF_ACK
)
3488 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3492 addlog(" send %s suggest %s\n",
3493 sppp_cp_type_name(type
), ip6_sprintf(&suggestaddr
));
3495 sppp_cp_send(sp
, PPP_IPV6CP
, type
, h
->ident
, rlen
, buf
);
3508 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3512 sppp_ipv6cp_RCN_rej(struct sppp
*sp
, struct lcp_header
*h
, int len
)
3515 struct ifnet
*ifp
= &sp
->pp_if
;
3516 int debug
= ifp
->if_flags
& IFF_DEBUG
;
3519 buf
= malloc (len
, M_TEMP
, M_NOWAIT
);
3524 log(LOG_DEBUG
, "%s: ipv6cp rej opts:",
3527 p
= (void *)(h
+ 1);
3528 for (; len
> 1 && p
[1]; len
-= p
[1], p
+= p
[1]) {
3531 addlog("%s: received malicious IPCPv6 option, "
3532 "dropping\n", ifp
->if_xname
);
3536 addlog(" %s", sppp_ipv6cp_opt_name(*p
));
3538 case IPV6CP_OPT_IFID
:
3540 * Peer doesn't grok address option. This is
3541 * bad. XXX Should we better give up here?
3543 sp
->ipv6cp
.opts
&= ~(1 << IPV6CP_OPT_IFID
);
3546 case IPV6CP_OPT_COMPRESS
:
3547 sp
->ipv6cp
.opts
&= ~(1 << IPV6CP_OPT_COMPRESS
);
3560 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3564 sppp_ipv6cp_RCN_nak(struct sppp
*sp
, struct lcp_header
*h
, int len
)
3567 struct ifnet
*ifp
= &sp
->pp_if
;
3568 int debug
= ifp
->if_flags
& IFF_DEBUG
;
3569 struct in6_addr suggestaddr
;
3572 buf
= malloc (len
, M_TEMP
, M_NOWAIT
);
3577 log(LOG_DEBUG
, "%s: ipv6cp nak opts:",
3580 p
= (void *)(h
+ 1);
3581 for (; len
> 1 && p
[1]; len
-= p
[1], p
+= p
[1]) {
3584 addlog("%s: received malicious IPCPv6 option, "
3585 "dropping\n", ifp
->if_xname
);
3589 addlog(" %s", sppp_ipv6cp_opt_name(*p
));
3591 case IPV6CP_OPT_IFID
:
3593 * Peer doesn't like our local ifid. See
3594 * if we can do something for him. We'll drop
3595 * him our address then.
3597 if (len
< 10 || p
[1] != 10)
3599 memset(&suggestaddr
, 0, sizeof(suggestaddr
));
3600 suggestaddr
.s6_addr16
[0] = htons(0xfe80);
3601 (void)in6_setscope(&suggestaddr
, &sp
->pp_if
, NULL
);
3602 memcpy(&suggestaddr
.s6_addr
[8], &p
[2], 8);
3604 sp
->ipv6cp
.opts
|= (1 << IPV6CP_OPT_IFID
);
3606 addlog(" [suggestaddr %s]",
3607 ip6_sprintf(&suggestaddr
));
3608 #ifdef IPV6CP_MYIFID_DYN
3610 * When doing dynamic address assignment,
3611 * we accept his offer.
3613 if (sp
->ipv6cp
.flags
& IPV6CP_MYIFID_DYN
) {
3614 struct in6_addr lastsuggest
;
3616 * If <suggested myaddr from peer> equals to
3617 * <hisaddr we have suggested last time>,
3618 * we have a collision. generate new random
3621 sppp_suggest_ip6_addr(&lastsuggest
);
3622 if (IN6_ARE_ADDR_EQUAL(&suggestaddr
,
3625 addlog(" [random]");
3626 sppp_gen_ip6_addr(sp
, &suggestaddr
);
3628 sppp_set_ip6_addr(sp
, &suggestaddr
, 0);
3631 sp
->ipv6cp
.flags
|= IPV6CP_MYIFID_SEEN
;
3635 * Since we do not do dynamic address assignment,
3636 * we ignore it and thus continue to negotiate
3637 * our already existing value. This can possibly
3638 * go into infinite request-reject loop.
3640 * This is not likely because we normally use
3641 * ifid based on MAC-address.
3642 * If you have no ethernet card on the node, too bad.
3643 * XXX should we use fail_counter?
3648 case IPV6CP_OPT_COMPRESS
:
3650 * Peer wants different compression parameters.
3664 sppp_ipv6cp_tlu(struct sppp
*sp
)
3666 /* we are up - notify isdn daemon */
3672 sppp_ipv6cp_tld(struct sppp
*sp
)
3677 sppp_ipv6cp_tls(struct sppp
*sp
)
3679 /* indicate to LCP that it must stay alive */
3680 sp
->lcp
.protos
|= (1 << IDX_IPV6CP
);
3684 sppp_ipv6cp_tlf(struct sppp
*sp
)
3686 /* we no longer need LCP */
3687 sp
->lcp
.protos
&= ~(1 << IDX_IPV6CP
);
3691 sppp_ipv6cp_scr(struct sppp
*sp
)
3693 char opt
[10 /* ifid */ + 4 /* compression, minimum */];
3694 struct in6_addr ouraddr
;
3697 if (sp
->ipv6cp
.opts
& (1 << IPV6CP_OPT_IFID
)) {
3698 sppp_get_ip6_addrs(sp
, &ouraddr
, 0, 0);
3699 opt
[i
++] = IPV6CP_OPT_IFID
;
3701 memcpy(&opt
[i
], &ouraddr
.s6_addr
[8], 8);
3706 if (sp
->ipv6cp
.opts
& (1 << IPV6CP_OPT_COMPRESSION
)) {
3707 opt
[i
++] = IPV6CP_OPT_COMPRESSION
;
3709 opt
[i
++] = 0; /* TBD */
3710 opt
[i
++] = 0; /* TBD */
3711 /* variable length data may follow */
3715 sp
->confid
[IDX_IPV6CP
] = ++sp
->pp_seq
[IDX_IPV6CP
];
3716 sppp_cp_send(sp
, PPP_IPV6CP
, CONF_REQ
, sp
->confid
[IDX_IPV6CP
], i
, &opt
);
3720 sppp_ipv6cp_init(struct sppp
*sp
)
3725 sppp_ipv6cp_up(struct sppp
*sp
)
3730 sppp_ipv6cp_down(struct sppp
*sp
)
3735 sppp_ipv6cp_open(struct sppp
*sp
)
3740 sppp_ipv6cp_close(struct sppp
*sp
)
3745 sppp_ipv6cp_TO(void *sp
)
3750 sppp_ipv6cp_RCR(struct sppp
*sp
, struct lcp_header
*h
,
3757 sppp_ipv6cp_RCN_rej(struct sppp
*sp
, struct lcp_header
*h
,
3763 sppp_ipv6cp_RCN_nak(struct sppp
*sp
, struct lcp_header
*h
,
3769 sppp_ipv6cp_tlu(struct sppp
*sp
)
3774 sppp_ipv6cp_tld(struct sppp
*sp
)
3779 sppp_ipv6cp_tls(struct sppp
*sp
)
3784 sppp_ipv6cp_tlf(struct sppp
*sp
)
3789 sppp_ipv6cp_scr(struct sppp
*sp
)
3796 *--------------------------------------------------------------------------*
3798 * The CHAP implementation. *
3800 *--------------------------------------------------------------------------*
3804 * The authentication protocols don't employ a full-fledged state machine as
3805 * the control protocols do, since they do have Open and Close events, but
3806 * not Up and Down, nor are they explicitly terminated. Also, use of the
3807 * authentication protocols may be different in both directions (this makes
3808 * sense, think of a machine that never accepts incoming calls but only
3809 * calls out, it doesn't require the called party to authenticate itself).
3811 * Our state machine for the local authentication protocol (we are requesting
3812 * the peer to authenticate) looks like:
3815 * +--------------------------------------------+
3817 * +--------+ Close +---------+ RCA+
3818 * | |<----------------------------------| |------+
3819 * +--->| Closed | TO* | Opened | sca |
3820 * | | |-----+ +-------| |<-----+
3821 * | +--------+ irc | | +---------+
3827 * | | +------->+ | |
3829 * | +--------+ V | |
3830 * | | |<----+<--------------------+ |
3836 * +------+ +------------------------------------------+
3837 * scn,tld sca,irc,ict,tlu
3842 * Open: LCP reached authentication phase
3843 * Close: LCP reached terminate phase
3845 * RCA+: received reply (pap-req, chap-response), acceptable
3846 * RCN: received reply (pap-req, chap-response), not acceptable
3847 * TO+: timeout with restart counter >= 0
3848 * TO-: timeout with restart counter < 0
3849 * TO*: reschedule timeout for CHAP
3851 * scr: send request packet (none for PAP, chap-challenge)
3852 * sca: send ack packet (pap-ack, chap-success)
3853 * scn: send nak packet (pap-nak, chap-failure)
3854 * ict: initialize re-challenge timer (CHAP only)
3856 * tlu: this-layer-up, LCP reaches network phase
3857 * tld: this-layer-down, LCP enters terminate phase
3859 * Note that in CHAP mode, after sending a new challenge, while the state
3860 * automaton falls back into Req-Sent state, it doesn't signal a tld
3861 * event to LCP, so LCP remains in network phase. Only after not getting
3862 * any response (or after getting an unacceptable response), CHAP closes,
3863 * causing LCP to enter terminate phase.
3865 * With PAP, there is no initial request that can be sent. The peer is
3866 * expected to send one based on the successful negotiation of PAP as
3867 * the authentication protocol during the LCP option negotiation.
3869 * Incoming authentication protocol requests (remote requests
3870 * authentication, we are peer) don't employ a state machine at all,
3871 * they are simply answered. Some peers [Ascend P50 firmware rev
3872 * 4.50] react allergically when sending IPCP/IPv6CP requests while they are
3873 * still in authentication phase (thereby violating the standard that
3874 * demands that these NCP packets are to be discarded), so we keep
3875 * track of the peer demanding us to authenticate, and only proceed to
3876 * phase network once we've seen a positive acknowledge for the
3881 * Handle incoming CHAP packets.
3884 sppp_chap_input(struct sppp
*sp
, struct mbuf
*m
)
3887 struct lcp_header
*h
;
3889 u_char
*value
, *name
, digest
[sizeof(sp
->myauth
.challenge
)], dsize
;
3890 int value_len
, name_len
;
3893 len
= m
->m_pkthdr
.len
;
3897 "%s: chap invalid packet length: %d bytes\n",
3898 ifp
->if_xname
, len
);
3901 h
= mtod(m
, struct lcp_header
*);
3902 if (len
> ntohs(h
->len
))
3903 len
= ntohs(h
->len
);
3906 /* challenge, failure and success are his authproto */
3907 case CHAP_CHALLENGE
:
3908 if (sp
->myauth
.secret
== NULL
|| sp
->myauth
.name
== NULL
) {
3909 /* can't do anything usefull */
3910 sp
->pp_auth_failures
++;
3911 printf("%s: chap input without my name and my secret being set\n",
3915 value
= 1 + (u_char
*)(h
+ 1);
3916 value_len
= value
[-1];
3917 name
= value
+ value_len
;
3918 name_len
= len
- value_len
- 5;
3922 "%s: chap corrupted challenge "
3923 "<%s id=0x%x len=%d",
3925 sppp_auth_type_name(PPP_CHAP
, h
->type
),
3926 h
->ident
, ntohs(h
->len
));
3928 sppp_print_bytes((u_char
*)(h
+ 1),
3937 "%s: chap input <%s id=0x%x len=%d name=",
3939 sppp_auth_type_name(PPP_CHAP
, h
->type
), h
->ident
,
3941 sppp_print_string((char *) name
, name_len
);
3942 addlog(" value-size=%d value=", value_len
);
3943 sppp_print_bytes(value
, value_len
);
3947 /* Compute reply value. */
3949 MD5Update(&ctx
, &h
->ident
, 1);
3950 MD5Update(&ctx
, sp
->myauth
.secret
, sp
->myauth
.secret_len
);
3951 MD5Update(&ctx
, value
, value_len
);
3952 MD5Final(digest
, &ctx
);
3953 dsize
= sizeof digest
;
3955 sppp_auth_send(&chap
, sp
, CHAP_RESPONSE
, h
->ident
,
3956 sizeof dsize
, (const char *)&dsize
,
3957 sizeof digest
, digest
,
3958 sp
->myauth
.name_len
,
3965 log(LOG_DEBUG
, "%s: chap success",
3969 sppp_print_string((char *)(h
+ 1), len
- 4);
3974 sp
->pp_auth_failures
= 0;
3975 sp
->pp_flags
&= ~PP_NEEDAUTH
;
3976 if (sp
->myauth
.proto
== PPP_CHAP
&&
3977 (sp
->lcp
.opts
& (1 << LCP_OPT_AUTH_PROTO
)) &&
3978 (sp
->lcp
.protos
& (1 << IDX_CHAP
)) == 0) {
3980 * We are authenticator for CHAP but didn't
3981 * complete yet. Leave it to tlu to proceed
3988 sppp_phase_network(sp
);
3993 sp
->pp_auth_failures
++;
3996 log(LOG_INFO
, "%s: chap failure",
4000 sppp_print_string((char *)(h
+ 1), len
- 4);
4004 log(LOG_INFO
, "%s: chap failure\n",
4006 /* await LCP shutdown by authenticator */
4009 /* response is my authproto */
4011 if (sp
->hisauth
.secret
== NULL
) {
4012 /* can't do anything usefull */
4013 printf("%s: chap input without his secret being set\n",
4017 value
= 1 + (u_char
*)(h
+ 1);
4018 value_len
= value
[-1];
4019 name
= value
+ value_len
;
4020 name_len
= len
- value_len
- 5;
4024 "%s: chap corrupted response "
4025 "<%s id=0x%x len=%d",
4027 sppp_auth_type_name(PPP_CHAP
, h
->type
),
4028 h
->ident
, ntohs(h
->len
));
4030 sppp_print_bytes((u_char
*)(h
+ 1),
4036 if (h
->ident
!= sp
->confid
[IDX_CHAP
]) {
4039 "%s: chap dropping response for old ID "
4040 "(got %d, expected %d)\n",
4042 h
->ident
, sp
->confid
[IDX_CHAP
]);
4045 if (sp
->hisauth
.name
!= NULL
&&
4046 (name_len
!= sp
->hisauth
.name_len
4047 || memcmp(name
, sp
->hisauth
.name
, name_len
) != 0)) {
4048 log(LOG_INFO
, "%s: chap response, his name ",
4050 sppp_print_string(name
, name_len
);
4051 addlog(" != expected ");
4052 sppp_print_string(sp
->hisauth
.name
,
4053 sp
->hisauth
.name_len
);
4058 log(LOG_DEBUG
, "%s: chap input(%s) "
4059 "<%s id=0x%x len=%d name=",
4061 sppp_state_name(sp
->state
[IDX_CHAP
]),
4062 sppp_auth_type_name(PPP_CHAP
, h
->type
),
4063 h
->ident
, ntohs(h
->len
));
4064 sppp_print_string((char *)name
, name_len
);
4065 addlog(" value-size=%d value=", value_len
);
4066 sppp_print_bytes(value
, value_len
);
4069 if (value_len
!= sizeof(sp
->myauth
.challenge
)) {
4072 "%s: chap bad hash value length: "
4073 "%d bytes, should be %ld\n",
4074 ifp
->if_xname
, value_len
,
4075 (long) sizeof(sp
->myauth
.challenge
));
4080 MD5Update(&ctx
, &h
->ident
, 1);
4081 MD5Update(&ctx
, sp
->hisauth
.secret
, sp
->hisauth
.secret_len
);
4082 MD5Update(&ctx
, sp
->myauth
.challenge
, sizeof(sp
->myauth
.challenge
));
4083 MD5Final(digest
, &ctx
);
4085 #define FAILMSG "Failed..."
4086 #define SUCCMSG "Welcome!"
4088 if (value_len
!= sizeof digest
||
4089 memcmp(digest
, value
, value_len
) != 0) {
4091 /* action scn, tld */
4093 sp
->pp_auth_failures
++;
4095 sppp_auth_send(&chap
, sp
, CHAP_FAILURE
, h
->ident
,
4096 sizeof(FAILMSG
) - 1, (const u_char
*)FAILMSG
,
4101 sp
->pp_auth_failures
= 0;
4102 /* action sca, perhaps tlu */
4103 if (sp
->state
[IDX_CHAP
] == STATE_REQ_SENT
||
4104 sp
->state
[IDX_CHAP
] == STATE_OPENED
)
4105 sppp_auth_send(&chap
, sp
, CHAP_SUCCESS
, h
->ident
,
4106 sizeof(SUCCMSG
) - 1, (const u_char
*)SUCCMSG
,
4108 if (sp
->state
[IDX_CHAP
] == STATE_REQ_SENT
) {
4109 sppp_cp_change_state(&chap
, sp
, STATE_OPENED
);
4115 /* Unknown CHAP packet type -- ignore. */
4117 log(LOG_DEBUG
, "%s: chap unknown input(%s) "
4118 "<0x%x id=0x%xh len=%d",
4120 sppp_state_name(sp
->state
[IDX_CHAP
]),
4121 h
->type
, h
->ident
, ntohs(h
->len
));
4123 sppp_print_bytes((u_char
*)(h
+ 1), len
- 4);
4132 sppp_chap_init(struct sppp
*sp
)
4134 /* Chap doesn't have STATE_INITIAL at all. */
4135 sp
->state
[IDX_CHAP
] = STATE_CLOSED
;
4136 sp
->fail_counter
[IDX_CHAP
] = 0;
4137 sp
->pp_seq
[IDX_CHAP
] = 0;
4138 sp
->pp_rseq
[IDX_CHAP
] = 0;
4139 callout_init(&sp
->ch
[IDX_CHAP
], 0);
4143 sppp_chap_open(struct sppp
*sp
)
4145 if (sp
->myauth
.proto
== PPP_CHAP
&&
4146 (sp
->lcp
.opts
& (1 << LCP_OPT_AUTH_PROTO
)) != 0) {
4147 /* we are authenticator for CHAP, start it */
4149 sp
->rst_counter
[IDX_CHAP
] = sp
->lcp
.max_configure
;
4150 sppp_cp_change_state(&chap
, sp
, STATE_REQ_SENT
);
4152 /* nothing to be done if we are peer, await a challenge */
4156 sppp_chap_close(struct sppp
*sp
)
4158 if (sp
->state
[IDX_CHAP
] != STATE_CLOSED
)
4159 sppp_cp_change_state(&chap
, sp
, STATE_CLOSED
);
4163 sppp_chap_TO(void *cookie
)
4165 struct sppp
*sp
= (struct sppp
*)cookie
;
4171 log(LOG_DEBUG
, "%s: chap TO(%s) rst_counter = %d\n",
4173 sppp_state_name(sp
->state
[IDX_CHAP
]),
4174 sp
->rst_counter
[IDX_CHAP
]);
4176 if (--sp
->rst_counter
[IDX_CHAP
] < 0)
4178 switch (sp
->state
[IDX_CHAP
]) {
4179 case STATE_REQ_SENT
:
4181 sppp_cp_change_state(&chap
, sp
, STATE_CLOSED
);
4185 /* TO+ (or TO*) event */
4186 switch (sp
->state
[IDX_CHAP
]) {
4189 sp
->rst_counter
[IDX_CHAP
] = sp
->lcp
.max_configure
;
4191 case STATE_REQ_SENT
:
4193 /* sppp_cp_change_state() will restart the timer */
4194 sppp_cp_change_state(&chap
, sp
, STATE_REQ_SENT
);
4202 sppp_chap_tlu(struct sppp
*sp
)
4208 sp
->rst_counter
[IDX_CHAP
] = sp
->lcp
.max_configure
;
4211 * Some broken CHAP implementations (Conware CoNet, firmware
4212 * 4.0.?) don't want to re-authenticate their CHAP once the
4213 * initial challenge-response exchange has taken place.
4214 * Provide for an option to avoid rechallenges.
4216 if ((sp
->hisauth
.flags
& SPPP_AUTHFLAG_NORECHALLENGE
) == 0) {
4218 * Compute the re-challenge timeout. This will yield
4219 * a number between 300 and 810 seconds.
4221 i
= 300 + ((unsigned)(arc4random() & 0xff00) >> 7);
4223 callout_reset(&sp
->ch
[IDX_CHAP
], i
* hz
, chap
.TO
, sp
);
4230 sp
->pp_phase
== SPPP_PHASE_NETWORK
? "reconfirmed": "tlu");
4231 if ((sp
->hisauth
.flags
& SPPP_AUTHFLAG_NORECHALLENGE
) == 0)
4232 addlog("next re-challenge in %d seconds\n", i
);
4234 addlog("re-challenging supressed\n");
4238 sp
->pp_auth_failures
= 0;
4239 /* indicate to LCP that we need to be closed down */
4240 sp
->lcp
.protos
|= (1 << IDX_CHAP
);
4242 if (sp
->pp_flags
& PP_NEEDAUTH
) {
4244 * Remote is authenticator, but his auth proto didn't
4245 * complete yet. Defer the transition to network
4254 * If we are already in phase network, we are done here. This
4255 * is the case if this is a dummy tlu event after a re-challenge.
4257 if (sp
->pp_phase
!= SPPP_PHASE_NETWORK
)
4258 sppp_phase_network(sp
);
4262 sppp_chap_tld(struct sppp
*sp
)
4267 log(LOG_DEBUG
, "%s: chap tld\n", ifp
->if_xname
);
4268 callout_stop(&sp
->ch
[IDX_CHAP
]);
4269 sp
->lcp
.protos
&= ~(1 << IDX_CHAP
);
4275 sppp_chap_scr(struct sppp
*sp
)
4280 if (sp
->myauth
.name
== NULL
) {
4281 /* can't do anything usefull */
4282 printf("%s: chap starting without my name being set\n",
4283 sp
->pp_if
.if_xname
);
4287 /* Compute random challenge. */
4288 ch
= (uint32_t *)sp
->myauth
.challenge
;
4289 ch
[0] = arc4random();
4290 ch
[1] = arc4random();
4291 ch
[2] = arc4random();
4292 ch
[3] = arc4random();
4293 clen
= 16; /* 4 * sizeof(uint32_t) */
4295 sp
->confid
[IDX_CHAP
] = ++sp
->pp_seq
[IDX_CHAP
];
4297 sppp_auth_send(&chap
, sp
, CHAP_CHALLENGE
, sp
->confid
[IDX_CHAP
],
4298 sizeof clen
, (const char *)&clen
,
4299 sizeof(sp
->myauth
.challenge
), sp
->myauth
.challenge
,
4300 sp
->myauth
.name_len
,
4306 *--------------------------------------------------------------------------*
4308 * The PAP implementation. *
4310 *--------------------------------------------------------------------------*
4313 * For PAP, we need to keep a little state also if we are the peer, not the
4314 * authenticator. This is since we don't get a request to authenticate, but
4315 * have to repeatedly authenticate ourself until we got a response (or the
4316 * retry counter is expired).
4320 * Handle incoming PAP packets. */
4322 sppp_pap_input(struct sppp
*sp
, struct mbuf
*m
)
4325 struct lcp_header
*h
;
4328 char *name
, *secret
;
4329 int name_len
, secret_len
;
4332 * Malicious input might leave this uninitialized, so
4333 * init to an impossible value.
4337 len
= m
->m_pkthdr
.len
;
4341 "%s: pap invalid packet length: %d bytes\n",
4342 ifp
->if_xname
, len
);
4345 h
= mtod(m
, struct lcp_header
*);
4346 if (len
> ntohs(h
->len
))
4347 len
= ntohs(h
->len
);
4349 /* PAP request is my authproto */
4351 if (sp
->hisauth
.name
== NULL
|| sp
->hisauth
.secret
== NULL
) {
4352 /* can't do anything usefull */
4353 printf("%s: pap request without his name and his secret being set\n",
4357 name
= 1 + (u_char
*)(h
+ 1);
4358 name_len
= name
[-1];
4359 secret
= name
+ name_len
+ 1;
4360 if (name_len
> len
- 6 ||
4361 (secret_len
= secret
[-1]) > len
- 6 - name_len
) {
4363 log(LOG_DEBUG
, "%s: pap corrupted input "
4364 "<%s id=0x%x len=%d",
4366 sppp_auth_type_name(PPP_PAP
, h
->type
),
4367 h
->ident
, ntohs(h
->len
));
4369 sppp_print_bytes((u_char
*)(h
+ 1),
4376 log(LOG_DEBUG
, "%s: pap input(%s) "
4377 "<%s id=0x%x len=%d name=",
4379 sppp_state_name(sp
->state
[IDX_PAP
]),
4380 sppp_auth_type_name(PPP_PAP
, h
->type
),
4381 h
->ident
, ntohs(h
->len
));
4382 sppp_print_string((char *)name
, name_len
);
4384 sppp_print_string((char *)secret
, secret_len
);
4387 if (name_len
!= sp
->hisauth
.name_len
||
4388 secret_len
!= sp
->hisauth
.secret_len
||
4389 memcmp(name
, sp
->hisauth
.name
, name_len
) != 0 ||
4390 memcmp(secret
, sp
->hisauth
.secret
, secret_len
) != 0) {
4391 /* action scn, tld */
4392 sp
->pp_auth_failures
++;
4393 mlen
= sizeof(FAILMSG
) - 1;
4394 sppp_auth_send(&pap
, sp
, PAP_NAK
, h
->ident
,
4395 sizeof mlen
, (const char *)&mlen
,
4396 sizeof(FAILMSG
) - 1, (const u_char
*)FAILMSG
,
4401 /* action sca, perhaps tlu */
4402 if (sp
->state
[IDX_PAP
] == STATE_REQ_SENT
||
4403 sp
->state
[IDX_PAP
] == STATE_OPENED
) {
4404 mlen
= sizeof(SUCCMSG
) - 1;
4405 sppp_auth_send(&pap
, sp
, PAP_ACK
, h
->ident
,
4406 sizeof mlen
, (const char *)&mlen
,
4407 sizeof(SUCCMSG
) - 1, (const u_char
*)SUCCMSG
,
4410 if (sp
->state
[IDX_PAP
] == STATE_REQ_SENT
) {
4411 sppp_cp_change_state(&pap
, sp
, STATE_OPENED
);
4416 /* ack and nak are his authproto */
4418 callout_stop(&sp
->pap_my_to_ch
);
4420 log(LOG_DEBUG
, "%s: pap success",
4422 name
= 1 + (u_char
*)(h
+ 1);
4423 name_len
= name
[-1];
4424 if (len
> 5 && name_len
< len
+4) {
4426 sppp_print_string(name
, name_len
);
4431 sp
->pp_auth_failures
= 0;
4432 sp
->pp_flags
&= ~PP_NEEDAUTH
;
4433 if (sp
->myauth
.proto
== PPP_PAP
&&
4434 (sp
->lcp
.opts
& (1 << LCP_OPT_AUTH_PROTO
)) &&
4435 (sp
->lcp
.protos
& (1 << IDX_PAP
)) == 0) {
4437 * We are authenticator for PAP but didn't
4438 * complete yet. Leave it to tlu to proceed
4445 sppp_phase_network(sp
);
4449 callout_stop(&sp
->pap_my_to_ch
);
4450 sp
->pp_auth_failures
++;
4452 log(LOG_INFO
, "%s: pap failure",
4454 name
= 1 + (u_char
*)(h
+ 1);
4455 name_len
= name
[-1];
4456 if (len
> 5 && name_len
< len
+4) {
4458 sppp_print_string(name
, name_len
);
4462 log(LOG_INFO
, "%s: pap failure\n",
4464 /* await LCP shutdown by authenticator */
4468 /* Unknown PAP packet type -- ignore. */
4470 log(LOG_DEBUG
, "%s: pap corrupted input "
4471 "<0x%x id=0x%x len=%d",
4473 h
->type
, h
->ident
, ntohs(h
->len
));
4475 sppp_print_bytes((u_char
*)(h
+ 1), len
- 4);
4484 sppp_pap_init(struct sppp
*sp
)
4486 /* PAP doesn't have STATE_INITIAL at all. */
4487 sp
->state
[IDX_PAP
] = STATE_CLOSED
;
4488 sp
->fail_counter
[IDX_PAP
] = 0;
4489 sp
->pp_seq
[IDX_PAP
] = 0;
4490 sp
->pp_rseq
[IDX_PAP
] = 0;
4491 callout_init(&sp
->ch
[IDX_PAP
], 0);
4492 callout_init(&sp
->pap_my_to_ch
, 0);
4496 sppp_pap_open(struct sppp
*sp
)
4498 if (sp
->hisauth
.proto
== PPP_PAP
&&
4499 (sp
->lcp
.opts
& (1 << LCP_OPT_AUTH_PROTO
)) != 0) {
4500 /* we are authenticator for PAP, start our timer */
4501 sp
->rst_counter
[IDX_PAP
] = sp
->lcp
.max_configure
;
4502 sppp_cp_change_state(&pap
, sp
, STATE_REQ_SENT
);
4504 if (sp
->myauth
.proto
== PPP_PAP
) {
4505 /* we are peer, send a request, and start a timer */
4507 callout_reset(&sp
->pap_my_to_ch
, sp
->lcp
.timeout
,
4508 sppp_pap_my_TO
, sp
);
4513 sppp_pap_close(struct sppp
*sp
)
4515 if (sp
->state
[IDX_PAP
] != STATE_CLOSED
)
4516 sppp_cp_change_state(&pap
, sp
, STATE_CLOSED
);
4520 * That's the timeout routine if we are authenticator. Since the
4521 * authenticator is basically passive in PAP, we can't do much here.
4524 sppp_pap_TO(void *cookie
)
4526 struct sppp
*sp
= (struct sppp
*)cookie
;
4532 log(LOG_DEBUG
, "%s: pap TO(%s) rst_counter = %d\n",
4534 sppp_state_name(sp
->state
[IDX_PAP
]),
4535 sp
->rst_counter
[IDX_PAP
]);
4537 if (--sp
->rst_counter
[IDX_PAP
] < 0)
4539 switch (sp
->state
[IDX_PAP
]) {
4540 case STATE_REQ_SENT
:
4542 sppp_cp_change_state(&pap
, sp
, STATE_CLOSED
);
4546 /* TO+ event, not very much we could do */
4547 switch (sp
->state
[IDX_PAP
]) {
4548 case STATE_REQ_SENT
:
4549 /* sppp_cp_change_state() will restart the timer */
4550 sppp_cp_change_state(&pap
, sp
, STATE_REQ_SENT
);
4558 * That's the timeout handler if we are peer. Since the peer is active,
4559 * we need to retransmit our PAP request since it is apparently lost.
4560 * XXX We should impose a max counter.
4563 sppp_pap_my_TO(void *cookie
)
4565 struct sppp
*sp
= (struct sppp
*)cookie
;
4569 log(LOG_DEBUG
, "%s: pap peer TO\n",
4576 sppp_pap_tlu(struct sppp
*sp
)
4581 sp
->rst_counter
[IDX_PAP
] = sp
->lcp
.max_configure
;
4584 log(LOG_DEBUG
, "%s: %s tlu\n",
4585 ifp
->if_xname
, pap
.name
);
4588 sp
->pp_auth_failures
= 0;
4589 /* indicate to LCP that we need to be closed down */
4590 sp
->lcp
.protos
|= (1 << IDX_PAP
);
4592 if (sp
->pp_flags
& PP_NEEDAUTH
) {
4594 * Remote is authenticator, but his auth proto didn't
4595 * complete yet. Defer the transition to network
4602 sppp_phase_network(sp
);
4606 sppp_pap_tld(struct sppp
*sp
)
4611 log(LOG_DEBUG
, "%s: pap tld\n", ifp
->if_xname
);
4612 callout_stop(&sp
->ch
[IDX_PAP
]);
4613 callout_stop(&sp
->pap_my_to_ch
);
4614 sp
->lcp
.protos
&= ~(1 << IDX_PAP
);
4620 sppp_pap_scr(struct sppp
*sp
)
4622 u_char idlen
, pwdlen
;
4624 if (sp
->myauth
.secret
== NULL
|| sp
->myauth
.name
== NULL
) {
4625 /* can't do anything usefull */
4626 printf("%s: pap starting without my name and secret being set\n",
4627 sp
->pp_if
.if_xname
);
4631 sp
->confid
[IDX_PAP
] = ++sp
->pp_seq
[IDX_PAP
];
4632 pwdlen
= sp
->myauth
.secret_len
;
4633 idlen
= sp
->myauth
.name_len
;
4635 sppp_auth_send(&pap
, sp
, PAP_REQ
, sp
->confid
[IDX_PAP
],
4636 sizeof idlen
, (const char *)&idlen
,
4637 idlen
, sp
->myauth
.name
,
4638 sizeof pwdlen
, (const char *)&pwdlen
,
4639 pwdlen
, sp
->myauth
.secret
,
4644 * Random miscellaneous functions.
4648 * Send a PAP or CHAP proto packet.
4650 * Varadic function, each of the elements for the ellipsis is of type
4651 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4653 * NOTE: never declare variadic functions with types subject to type
4654 * promotion (i.e. u_char). This is asking for big trouble depending
4655 * on the architecture you are on...
4659 sppp_auth_send(const struct cp
*cp
, struct sppp
*sp
,
4660 unsigned int type
, unsigned int id
,
4664 struct lcp_header
*lh
;
4673 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
4676 m
->m_pkthdr
.rcvif
= 0;
4678 if (sp
->pp_flags
& PP_NOFRAMING
) {
4679 *mtod(m
, uint16_t *) = htons(cp
->proto
);
4681 lh
= (struct lcp_header
*)(mtod(m
, uint8_t *)+2);
4683 struct ppp_header
*h
;
4684 h
= mtod(m
, struct ppp_header
*);
4685 h
->address
= PPP_ALLSTATIONS
; /* broadcast address */
4686 h
->control
= PPP_UI
; /* Unnumbered Info */
4687 h
->protocol
= htons(cp
->proto
);
4688 pkthdrlen
= PPP_HEADER_LEN
;
4690 lh
= (struct lcp_header
*)(h
+ 1);
4695 p
= (u_char
*)(lh
+ 1);
4700 while ((mlen
= (unsigned int)va_arg(ap
, size_t)) != 0) {
4701 msg
= va_arg(ap
, const char *);
4703 if (len
> MHLEN
- pkthdrlen
- LCP_HEADER_LEN
) {
4709 memcpy(p
, msg
, mlen
);
4714 m
->m_pkthdr
.len
= m
->m_len
= pkthdrlen
+ LCP_HEADER_LEN
+ len
;
4715 lh
->len
= htons(LCP_HEADER_LEN
+ len
);
4718 log(LOG_DEBUG
, "%s: %s output <%s id=0x%x len=%d",
4719 ifp
->if_xname
, cp
->name
,
4720 sppp_auth_type_name(cp
->proto
, lh
->type
),
4721 lh
->ident
, ntohs(lh
->len
));
4723 sppp_print_bytes((u_char
*)(lh
+ 1), len
);
4726 if (IF_QFULL(&sp
->pp_cpq
)) {
4727 IF_DROP(&sp
->pp_fastq
);
4728 IF_DROP(&ifp
->if_snd
);
4733 IF_ENQUEUE(&sp
->pp_cpq
, m
);
4734 if (! (ifp
->if_flags
& IFF_OACTIVE
))
4735 (*ifp
->if_start
)(ifp
);
4736 ifp
->if_obytes
+= m
->m_pkthdr
.len
+ 3;
4740 * Send keepalive packets, every 10 seconds.
4743 sppp_keepalive(void *dummy
)
4751 for (sp
=spppq
; sp
; sp
=sp
->pp_next
) {
4752 struct ifnet
*ifp
= &sp
->pp_if
;
4754 /* check idle timeout */
4755 if ((sp
->pp_idle_timeout
!= 0) && (ifp
->if_flags
& IFF_RUNNING
)
4756 && (sp
->pp_phase
== SPPP_PHASE_NETWORK
)) {
4757 /* idle timeout is enabled for this interface */
4758 if ((now
-sp
->pp_last_activity
) >= sp
->pp_idle_timeout
) {
4759 if (ifp
->if_flags
& IFF_DEBUG
)
4760 printf("%s: no activity for %lu seconds\n",
4762 (unsigned long)(now
-sp
->pp_last_activity
));
4768 /* Keepalive mode disabled or channel down? */
4769 if (! (sp
->pp_flags
& PP_KEEPALIVE
) ||
4770 ! (ifp
->if_flags
& IFF_RUNNING
))
4773 /* No keepalive in PPP mode if LCP not opened yet. */
4774 if (! (sp
->pp_flags
& PP_CISCO
) &&
4775 sp
->pp_phase
< SPPP_PHASE_AUTHENTICATE
)
4778 /* No echo reply, but maybe user data passed through? */
4779 if ((now
- sp
->pp_last_receive
) < sp
->pp_max_noreceive
) {
4780 sp
->pp_alivecnt
= 0;
4784 if (sp
->pp_alivecnt
>= sp
->pp_maxalive
) {
4785 /* No keepalive packets got. Stop the interface. */
4787 IF_PURGE(&sp
->pp_cpq
);
4788 if (! (sp
->pp_flags
& PP_CISCO
)) {
4789 printf("%s: LCP keepalive timed out, going to restart the connection\n",
4791 sp
->pp_alivecnt
= 0;
4793 /* we are down, close all open protocols */
4796 /* And now prepare LCP to reestablish the link, if configured to do so. */
4797 sppp_cp_change_state(&lcp
, sp
, STATE_STOPPED
);
4799 /* Close connection immediately, completition of this
4800 * will summon the magic needed to reestablish it. */
4806 if (sp
->pp_alivecnt
< sp
->pp_maxalive
)
4808 if (sp
->pp_flags
& PP_CISCO
)
4809 sppp_cisco_send(sp
, CISCO_KEEPALIVE_REQ
,
4810 ++sp
->pp_seq
[IDX_LCP
], sp
->pp_rseq
[IDX_LCP
]);
4811 else if (sp
->pp_phase
>= SPPP_PHASE_AUTHENTICATE
) {
4812 int32_t nmagic
= htonl(sp
->lcp
.magic
);
4813 sp
->lcp
.echoid
= ++sp
->pp_seq
[IDX_LCP
];
4814 sppp_cp_send(sp
, PPP_LCP
, ECHO_REQ
,
4815 sp
->lcp
.echoid
, 4, &nmagic
);
4819 callout_reset(&keepalive_ch
, hz
* LCP_KEEPALIVE_INTERVAL
, sppp_keepalive
, NULL
);
4824 * Get both IP addresses.
4827 sppp_get_ip_addrs(struct sppp
*sp
, uint32_t *src
, uint32_t *dst
, uint32_t *srcmask
)
4829 struct ifnet
*ifp
= &sp
->pp_if
;
4831 struct sockaddr_in
*si
, *sm
;
4832 uint32_t ssrc
, ddst
;
4837 * Pick the first AF_INET address from the list,
4838 * aliases don't make any sense on a p2p link anyway.
4841 IFADDR_FOREACH(ifa
, ifp
) {
4842 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
4843 si
= (struct sockaddr_in
*)ifa
->ifa_addr
;
4844 sm
= (struct sockaddr_in
*)ifa
->ifa_netmask
;
4850 if (si
&& si
->sin_addr
.s_addr
) {
4851 ssrc
= si
->sin_addr
.s_addr
;
4853 *srcmask
= ntohl(sm
->sin_addr
.s_addr
);
4856 si
= (struct sockaddr_in
*)ifa
->ifa_dstaddr
;
4857 if (si
&& si
->sin_addr
.s_addr
)
4858 ddst
= si
->sin_addr
.s_addr
;
4861 if (dst
) *dst
= ntohl(ddst
);
4862 if (src
) *src
= ntohl(ssrc
);
4866 * Set IP addresses. Must be called at splnet.
4867 * If an address is 0, leave it the way it is.
4870 sppp_set_ip_addrs(struct sppp
*sp
, uint32_t myaddr
, uint32_t hisaddr
)
4874 struct sockaddr_in
*si
, *dest
;
4877 * Pick the first AF_INET address from the list,
4878 * aliases don't make any sense on a p2p link anyway.
4881 IFADDR_FOREACH(ifa
, ifp
) {
4882 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
4883 si
= (struct sockaddr_in
*)ifa
->ifa_addr
;
4884 dest
= (struct sockaddr_in
*)ifa
->ifa_dstaddr
;
4893 struct sockaddr_in new_sin
= *si
;
4894 struct sockaddr_in new_dst
= *dest
;
4897 * Scrub old routes now instead of calling in_ifinit with
4898 * scrub=1, because we may change the dstaddr
4899 * before the call to in_ifinit.
4901 in_ifscrub(ifp
, ifatoia(ifa
));
4904 new_sin
.sin_addr
.s_addr
= htonl(myaddr
);
4906 new_dst
.sin_addr
.s_addr
= htonl(hisaddr
);
4907 if (new_dst
.sin_addr
.s_addr
!= dest
->sin_addr
.s_addr
) {
4908 sp
->ipcp
.saved_hisaddr
= dest
->sin_addr
.s_addr
;
4909 *dest
= new_dst
; /* fix dstaddr in place */
4912 error
= in_ifinit(ifp
, ifatoia(ifa
), &new_sin
, 0);
4915 log(LOG_DEBUG
, "%s: sppp_set_ip_addrs: in_ifinit "
4916 " failed, error=%d\n", ifp
->if_xname
, error
);
4920 (void)pfil_run_hooks(&if_pfil
,
4921 (struct mbuf
**)SIOCAIFADDR
, ifp
, PFIL_IFADDR
);
4927 * Clear IP addresses. Must be called at splnet.
4930 sppp_clear_ip_addrs(struct sppp
*sp
)
4932 struct ifnet
*ifp
= &sp
->pp_if
;
4934 struct sockaddr_in
*si
, *dest
;
4937 if (sp
->ipcp
.flags
& IPCP_HISADDR_DYN
)
4938 remote
= sp
->ipcp
.saved_hisaddr
;
4940 sppp_get_ip_addrs(sp
, 0, &remote
, 0);
4943 * Pick the first AF_INET address from the list,
4944 * aliases don't make any sense on a p2p link anyway.
4947 IFADDR_FOREACH(ifa
, ifp
) {
4948 if (ifa
->ifa_addr
->sa_family
== AF_INET
) {
4949 si
= (struct sockaddr_in
*)ifa
->ifa_addr
;
4950 dest
= (struct sockaddr_in
*)ifa
->ifa_dstaddr
;
4958 struct sockaddr_in new_sin
= *si
;
4960 in_ifscrub(ifp
, ifatoia(ifa
));
4961 if (sp
->ipcp
.flags
& IPCP_MYADDR_DYN
)
4962 new_sin
.sin_addr
.s_addr
= 0;
4963 if (sp
->ipcp
.flags
& IPCP_HISADDR_DYN
)
4964 /* replace peer addr in place */
4965 dest
->sin_addr
.s_addr
= sp
->ipcp
.saved_hisaddr
;
4966 in_ifinit(ifp
, ifatoia(ifa
), &new_sin
, 0);
4968 (void)pfil_run_hooks(&if_pfil
,
4969 (struct mbuf
**)SIOCDIFADDR
, ifp
, PFIL_IFADDR
);
4977 * Get both IPv6 addresses.
4980 sppp_get_ip6_addrs(struct sppp
*sp
, struct in6_addr
*src
, struct in6_addr
*dst
,
4981 struct in6_addr
*srcmask
)
4983 struct ifnet
*ifp
= &sp
->pp_if
;
4985 struct sockaddr_in6
*si
, *sm
;
4986 struct in6_addr ssrc
, ddst
;
4989 memset(&ssrc
, 0, sizeof(ssrc
));
4990 memset(&ddst
, 0, sizeof(ddst
));
4992 * Pick the first link-local AF_INET6 address from the list,
4993 * aliases don't make any sense on a p2p link anyway.
4996 IFADDR_FOREACH(ifa
, ifp
)
4997 if (ifa
->ifa_addr
->sa_family
== AF_INET6
) {
4998 si
= (struct sockaddr_in6
*)ifa
->ifa_addr
;
4999 sm
= (struct sockaddr_in6
*)ifa
->ifa_netmask
;
5000 if (si
&& IN6_IS_ADDR_LINKLOCAL(&si
->sin6_addr
))
5004 if (si
&& !IN6_IS_ADDR_UNSPECIFIED(&si
->sin6_addr
)) {
5005 memcpy(&ssrc
, &si
->sin6_addr
, sizeof(ssrc
));
5007 memcpy(srcmask
, &sm
->sin6_addr
,
5012 si
= (struct sockaddr_in6
*)ifa
->ifa_dstaddr
;
5013 if (si
&& !IN6_IS_ADDR_UNSPECIFIED(&si
->sin6_addr
))
5014 memcpy(&ddst
, &si
->sin6_addr
, sizeof(ddst
));
5018 memcpy(dst
, &ddst
, sizeof(*dst
));
5020 memcpy(src
, &ssrc
, sizeof(*src
));
5023 #ifdef IPV6CP_MYIFID_DYN
5025 * Generate random ifid.
5028 sppp_gen_ip6_addr(struct sppp
*sp
, struct in6_addr
*addr
)
5034 * Set my IPv6 address. Must be called at splnet.
5037 sppp_set_ip6_addr(struct sppp
*sp
, const struct in6_addr
*src
)
5041 struct sockaddr_in6
*sin6
;
5044 * Pick the first link-local AF_INET6 address from the list,
5045 * aliases don't make any sense on a p2p link anyway.
5049 IFADDR_FOREACH(ifa
, ifp
)
5051 if (ifa
->ifa_addr
->sa_family
== AF_INET6
)
5053 sin6
= (struct sockaddr_in6
*)ifa
->ifa_addr
;
5054 if (sin6
&& IN6_IS_ADDR_LINKLOCAL(&sin6
->sin6_addr
))
5062 struct sockaddr_in6 new_sin6
= *sin6
;
5064 memcpy(&new_sin6
.sin6_addr
, src
, sizeof(new_sin6
.sin6_addr
));
5065 error
= in6_ifinit(ifp
, ifatoia6(ifa
), &new_sin6
, 1);
5068 log(LOG_DEBUG
, "%s: sppp_set_ip6_addr: in6_ifinit "
5069 " failed, error=%d\n", ifp
->if_xname
, error
);
5073 (void)pfil_run_hooks(&if_pfil
,
5074 (struct mbuf
**)SIOCAIFADDR_IN6
, ifp
, PFIL_IFADDR
);
5081 * Suggest a candidate address to be used by peer.
5084 sppp_suggest_ip6_addr(struct sppp
*sp
, struct in6_addr
*suggest
)
5086 struct in6_addr myaddr
;
5089 sppp_get_ip6_addrs(sp
, &myaddr
, 0, 0);
5091 myaddr
.s6_addr
[8] &= ~0x02; /* u bit to "local" */
5093 if ((tv
.tv_usec
& 0xff) == 0 && (tv
.tv_sec
& 0xff) == 0) {
5094 myaddr
.s6_addr
[14] ^= 0xff;
5095 myaddr
.s6_addr
[15] ^= 0xff;
5097 myaddr
.s6_addr
[14] ^= (tv
.tv_usec
& 0xff);
5098 myaddr
.s6_addr
[15] ^= (tv
.tv_sec
& 0xff);
5101 memcpy(suggest
, &myaddr
, sizeof(myaddr
));
5106 * Process ioctl requests specific to the PPP interface.
5107 * Permissions have already been checked.
5110 sppp_params(struct sppp
*sp
, u_long cmd
, void *data
)
5113 case SPPPGETAUTHCFG
:
5115 struct spppauthcfg
*cfg
= (struct spppauthcfg
*)data
;
5119 cfg
->myauthflags
= sp
->myauth
.flags
;
5120 cfg
->hisauthflags
= sp
->hisauth
.flags
;
5121 strncpy(cfg
->ifname
, sp
->pp_if
.if_xname
, IFNAMSIZ
);
5123 if (sp
->hisauth
.proto
)
5124 cfg
->hisauth
= (sp
->hisauth
.proto
== PPP_PAP
) ? SPPP_AUTHPROTO_PAP
: SPPP_AUTHPROTO_CHAP
;
5126 if (sp
->myauth
.proto
)
5127 cfg
->myauth
= (sp
->myauth
.proto
== PPP_PAP
) ? SPPP_AUTHPROTO_PAP
: SPPP_AUTHPROTO_CHAP
;
5128 if (cfg
->myname_length
== 0) {
5129 if (sp
->myauth
.name
!= NULL
)
5130 cfg
->myname_length
= sp
->myauth
.name_len
+ 1;
5132 if (sp
->myauth
.name
== NULL
) {
5133 cfg
->myname_length
= 0;
5135 len
= sp
->myauth
.name_len
+ 1;
5136 if (cfg
->myname_length
< len
)
5137 return (ENAMETOOLONG
);
5138 error
= copyout(sp
->myauth
.name
, cfg
->myname
, len
);
5139 if (error
) return error
;
5142 if (cfg
->hisname_length
== 0) {
5143 if (sp
->hisauth
.name
!= NULL
)
5144 cfg
->hisname_length
= sp
->hisauth
.name_len
+ 1;
5146 if (sp
->hisauth
.name
== NULL
) {
5147 cfg
->hisname_length
= 0;
5149 len
= sp
->hisauth
.name_len
+ 1;
5150 if (cfg
->hisname_length
< len
)
5151 return (ENAMETOOLONG
);
5152 error
= copyout(sp
->hisauth
.name
, cfg
->hisname
, len
);
5153 if (error
) return error
;
5158 case SPPPSETAUTHCFG
:
5160 struct spppauthcfg
*cfg
= (struct spppauthcfg
*)data
;
5163 if (sp
->myauth
.name
) {
5164 free(sp
->myauth
.name
, M_DEVBUF
);
5165 sp
->myauth
.name
= NULL
;
5167 if (sp
->myauth
.secret
) {
5168 free(sp
->myauth
.secret
, M_DEVBUF
);
5169 sp
->myauth
.secret
= NULL
;
5171 if (sp
->hisauth
.name
) {
5172 free(sp
->hisauth
.name
, M_DEVBUF
);
5173 sp
->hisauth
.name
= NULL
;
5175 if (sp
->hisauth
.secret
) {
5176 free(sp
->hisauth
.secret
, M_DEVBUF
);
5177 sp
->hisauth
.secret
= NULL
;
5180 if (cfg
->hisname
!= NULL
&& cfg
->hisname_length
> 0) {
5181 if (cfg
->hisname_length
>= MCLBYTES
)
5182 return (ENAMETOOLONG
);
5183 sp
->hisauth
.name
= malloc(cfg
->hisname_length
, M_DEVBUF
, M_WAITOK
);
5184 error
= copyin(cfg
->hisname
, sp
->hisauth
.name
, cfg
->hisname_length
);
5186 free(sp
->hisauth
.name
, M_DEVBUF
);
5187 sp
->hisauth
.name
= NULL
;
5190 sp
->hisauth
.name_len
= cfg
->hisname_length
- 1;
5191 sp
->hisauth
.name
[sp
->hisauth
.name_len
] = 0;
5193 if (cfg
->hissecret
!= NULL
&& cfg
->hissecret_length
> 0) {
5194 if (cfg
->hissecret_length
>= MCLBYTES
)
5195 return (ENAMETOOLONG
);
5196 sp
->hisauth
.secret
= malloc(cfg
->hissecret_length
, M_DEVBUF
, M_WAITOK
);
5197 error
= copyin(cfg
->hissecret
, sp
->hisauth
.secret
, cfg
->hissecret_length
);
5199 free(sp
->hisauth
.secret
, M_DEVBUF
);
5200 sp
->hisauth
.secret
= NULL
;
5203 sp
->hisauth
.secret_len
= cfg
->hissecret_length
- 1;
5204 sp
->hisauth
.secret
[sp
->hisauth
.secret_len
] = 0;
5206 if (cfg
->myname
!= NULL
&& cfg
->myname_length
> 0) {
5207 if (cfg
->myname_length
>= MCLBYTES
)
5208 return (ENAMETOOLONG
);
5209 sp
->myauth
.name
= malloc(cfg
->myname_length
, M_DEVBUF
, M_WAITOK
);
5210 error
= copyin(cfg
->myname
, sp
->myauth
.name
, cfg
->myname_length
);
5212 free(sp
->myauth
.name
, M_DEVBUF
);
5213 sp
->myauth
.name
= NULL
;
5216 sp
->myauth
.name_len
= cfg
->myname_length
- 1;
5217 sp
->myauth
.name
[sp
->myauth
.name_len
] = 0;
5219 if (cfg
->mysecret
!= NULL
&& cfg
->mysecret_length
> 0) {
5220 if (cfg
->mysecret_length
>= MCLBYTES
)
5221 return (ENAMETOOLONG
);
5222 sp
->myauth
.secret
= malloc(cfg
->mysecret_length
, M_DEVBUF
, M_WAITOK
);
5223 error
= copyin(cfg
->mysecret
, sp
->myauth
.secret
, cfg
->mysecret_length
);
5225 free(sp
->myauth
.secret
, M_DEVBUF
);
5226 sp
->myauth
.secret
= NULL
;
5229 sp
->myauth
.secret_len
= cfg
->mysecret_length
- 1;
5230 sp
->myauth
.secret
[sp
->myauth
.secret_len
] = 0;
5232 sp
->myauth
.flags
= cfg
->myauthflags
;
5234 sp
->myauth
.proto
= (cfg
->myauth
== SPPP_AUTHPROTO_PAP
) ? PPP_PAP
: PPP_CHAP
;
5235 sp
->hisauth
.flags
= cfg
->hisauthflags
;
5237 sp
->hisauth
.proto
= (cfg
->hisauth
== SPPP_AUTHPROTO_PAP
) ? PPP_PAP
: PPP_CHAP
;
5238 sp
->pp_auth_failures
= 0;
5239 if (sp
->hisauth
.proto
!= 0)
5240 sp
->lcp
.opts
|= (1 << LCP_OPT_AUTH_PROTO
);
5242 sp
->lcp
.opts
&= ~(1 << LCP_OPT_AUTH_PROTO
);
5247 struct sppplcpcfg
*lcpp
= (struct sppplcpcfg
*)data
;
5248 lcpp
->lcp_timeout
= sp
->lcp
.timeout
;
5253 struct sppplcpcfg
*lcpp
= (struct sppplcpcfg
*)data
;
5254 sp
->lcp
.timeout
= lcpp
->lcp_timeout
;
5259 struct spppstatus
*status
= (struct spppstatus
*)data
;
5260 status
->phase
= sp
->pp_phase
;
5263 case SPPPGETSTATUSNCP
:
5265 struct spppstatusncp
*status
= (struct spppstatusncp
*)data
;
5266 status
->phase
= sp
->pp_phase
;
5267 status
->ncpup
= sppp_ncp_check(sp
);
5272 struct spppidletimeout
*to
= (struct spppidletimeout
*)data
;
5273 to
->idle_seconds
= sp
->pp_idle_timeout
;
5278 struct spppidletimeout
*to
= (struct spppidletimeout
*)data
;
5279 sp
->pp_idle_timeout
= to
->idle_seconds
;
5282 case SPPPSETAUTHFAILURE
:
5284 struct spppauthfailuresettings
*afsettings
= (struct spppauthfailuresettings
*)data
;
5285 sp
->pp_max_auth_fail
= afsettings
->max_failures
;
5286 sp
->pp_auth_failures
= 0;
5289 case SPPPGETAUTHFAILURES
:
5291 struct spppauthfailurestats
*stats
= (struct spppauthfailurestats
*)data
;
5292 stats
->auth_failures
= sp
->pp_auth_failures
;
5293 stats
->max_failures
= sp
->pp_max_auth_fail
;
5296 case SPPPSETDNSOPTS
:
5298 struct spppdnssettings
*req
= (struct spppdnssettings
*)data
;
5299 sp
->query_dns
= req
->query_dns
& 3;
5302 case SPPPGETDNSOPTS
:
5304 struct spppdnssettings
*req
= (struct spppdnssettings
*)data
;
5305 req
->query_dns
= sp
->query_dns
;
5308 case SPPPGETDNSADDRS
:
5310 struct spppdnsaddrs
*addrs
= (struct spppdnsaddrs
*)data
;
5311 memcpy(&addrs
->dns
, &sp
->dns_addrs
, sizeof addrs
->dns
);
5314 case SPPPGETKEEPALIVE
:
5316 struct spppkeepalivesettings
*settings
=
5317 (struct spppkeepalivesettings
*)data
;
5318 settings
->maxalive
= sp
->pp_maxalive
;
5319 settings
->max_noreceive
= sp
->pp_max_noreceive
;
5322 case SPPPSETKEEPALIVE
:
5324 struct spppkeepalivesettings
*settings
=
5325 (struct spppkeepalivesettings
*)data
;
5326 sp
->pp_maxalive
= settings
->maxalive
;
5327 sp
->pp_max_noreceive
= settings
->max_noreceive
;
5338 sppp_phase_network(struct sppp
*sp
)
5344 sp
->pp_phase
= SPPP_PHASE_NETWORK
;
5348 log(LOG_INFO
, "%s: phase %s\n", ifp
->if_xname
,
5349 sppp_phase_name(sp
->pp_phase
));
5352 /* Notify NCPs now. */
5353 for (i
= 0; i
< IDX_COUNT
; i
++)
5354 if ((cps
[i
])->flags
& CP_NCP
)
5357 /* Send Up events to all NCPs. */
5358 for (i
= 0, mask
= 1; i
< IDX_COUNT
; i
++, mask
<<= 1)
5359 if ((sp
->lcp
.protos
& mask
) && ((cps
[i
])->flags
& CP_NCP
))
5362 /* if no NCP is starting, all this was in vain, close down */
5363 sppp_lcp_check_and_close(sp
);
5368 sppp_cp_type_name(u_char type
)
5370 static char buf
[12];
5372 case CONF_REQ
: return "conf-req";
5373 case CONF_ACK
: return "conf-ack";
5374 case CONF_NAK
: return "conf-nak";
5375 case CONF_REJ
: return "conf-rej";
5376 case TERM_REQ
: return "term-req";
5377 case TERM_ACK
: return "term-ack";
5378 case CODE_REJ
: return "code-rej";
5379 case PROTO_REJ
: return "proto-rej";
5380 case ECHO_REQ
: return "echo-req";
5381 case ECHO_REPLY
: return "echo-reply";
5382 case DISC_REQ
: return "discard-req";
5384 snprintf(buf
, sizeof(buf
), "0x%x", type
);
5389 sppp_auth_type_name(u_short proto
, u_char type
)
5391 static char buf
[12];
5395 case CHAP_CHALLENGE
: return "challenge";
5396 case CHAP_RESPONSE
: return "response";
5397 case CHAP_SUCCESS
: return "success";
5398 case CHAP_FAILURE
: return "failure";
5402 case PAP_REQ
: return "req";
5403 case PAP_ACK
: return "ack";
5404 case PAP_NAK
: return "nak";
5407 snprintf(buf
, sizeof(buf
), "0x%x", type
);
5412 sppp_lcp_opt_name(u_char opt
)
5414 static char buf
[12];
5416 case LCP_OPT_MRU
: return "mru";
5417 case LCP_OPT_ASYNC_MAP
: return "async-map";
5418 case LCP_OPT_AUTH_PROTO
: return "auth-proto";
5419 case LCP_OPT_QUAL_PROTO
: return "qual-proto";
5420 case LCP_OPT_MAGIC
: return "magic";
5421 case LCP_OPT_PROTO_COMP
: return "proto-comp";
5422 case LCP_OPT_ADDR_COMP
: return "addr-comp";
5424 snprintf(buf
, sizeof(buf
), "0x%x", opt
);
5429 sppp_ipcp_opt_name(u_char opt
)
5431 static char buf
[12];
5433 case IPCP_OPT_ADDRESSES
: return "addresses";
5434 case IPCP_OPT_COMPRESSION
: return "compression";
5435 case IPCP_OPT_ADDRESS
: return "address";
5437 snprintf(buf
, sizeof(buf
), "0x%x", opt
);
5443 sppp_ipv6cp_opt_name(u_char opt
)
5445 static char buf
[12];
5447 case IPV6CP_OPT_IFID
: return "ifid";
5448 case IPV6CP_OPT_COMPRESSION
: return "compression";
5450 snprintf(buf
, sizeof(buf
), "0x%x", opt
);
5456 sppp_state_name(int state
)
5459 case STATE_INITIAL
: return "initial";
5460 case STATE_STARTING
: return "starting";
5461 case STATE_CLOSED
: return "closed";
5462 case STATE_STOPPED
: return "stopped";
5463 case STATE_CLOSING
: return "closing";
5464 case STATE_STOPPING
: return "stopping";
5465 case STATE_REQ_SENT
: return "req-sent";
5466 case STATE_ACK_RCVD
: return "ack-rcvd";
5467 case STATE_ACK_SENT
: return "ack-sent";
5468 case STATE_OPENED
: return "opened";
5474 sppp_phase_name(int phase
)
5477 case SPPP_PHASE_DEAD
: return "dead";
5478 case SPPP_PHASE_ESTABLISH
: return "establish";
5479 case SPPP_PHASE_TERMINATE
: return "terminate";
5480 case SPPP_PHASE_AUTHENTICATE
: return "authenticate";
5481 case SPPP_PHASE_NETWORK
: return "network";
5487 sppp_proto_name(u_short proto
)
5489 static char buf
[12];
5491 case PPP_LCP
: return "lcp";
5492 case PPP_IPCP
: return "ipcp";
5493 case PPP_PAP
: return "pap";
5494 case PPP_CHAP
: return "chap";
5495 case PPP_IPV6CP
: return "ipv6cp";
5497 snprintf(buf
, sizeof(buf
), "0x%x", (unsigned)proto
);
5502 sppp_print_bytes(const u_char
*p
, u_short len
)
5504 addlog(" %02x", *p
++);
5506 addlog("-%02x", *p
++);
5510 sppp_print_string(const char *p
, u_short len
)
5517 * Print only ASCII chars directly. RFC 1994 recommends
5518 * using only them, but we don't rely on it. */
5519 if (c
< ' ' || c
> '~')
5527 sppp_dotted_quad(uint32_t addr
)
5530 snprintf(s
, sizeof(s
), "%d.%d.%d.%d",
5531 (int)((addr
>> 24) & 0xff),
5532 (int)((addr
>> 16) & 0xff),
5533 (int)((addr
>> 8) & 0xff),
5534 (int)(addr
& 0xff));
5538 /* a dummy, used to drop uninteresting events */
5540 sppp_null(struct sppp
*unused
)
5542 /* do just nothing */
5545 * This file is large. Tell emacs to highlight it nevertheless.
5548 * hilit-auto-highlight-maxout: 120000