2 * Synchronous PPP/Cisco link level subroutines.
3 * Keepalive protocol implemented in both Cisco and PPP modes.
5 * Copyright (C) 1994-1996 Cronyx Engineering Ltd.
6 * Author: Serge Vakulenko, <vak@cronyx.ru>
8 * Heavily revamped to conform to RFC 1661.
9 * Copyright (C) 1997, 2001 Joerg Wunsch.
11 * This software is distributed with NO WARRANTIES, not even the implied
12 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * Authors grant any other persons or organisations permission to use
15 * or modify this software as long as this message is kept with the software,
16 * all derivative works or modified versions.
18 * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997
20 * $FreeBSD: src/sys/net/if_spppsubr.c,v 1.59.2.13 2002/07/03 15:44:41 joerg Exp $
23 #include <sys/param.h>
24 #include <sys/libkern.h>
26 #if defined(__DragonFly__)
28 #include "opt_inet6.h"
34 # include "opt_inet.h"
35 # include "opt_inet6.h"
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/sockio.h>
44 #include <sys/socket.h>
45 #include <sys/syslog.h>
46 #if defined(__DragonFly__)
47 #include <sys/random.h>
48 #include <sys/thread2.h>
50 #include <sys/malloc.h>
53 #if defined (__OpenBSD__)
60 #include <net/ifq_var.h>
61 #include <net/netisr.h>
62 #include <net/if_types.h>
63 #include <net/route.h>
64 #include <netinet/in.h>
65 #include <netinet/in_systm.h>
66 #include <netinet/ip.h>
67 #include <net/slcompress.h>
69 #if defined (__NetBSD__) || defined (__OpenBSD__)
70 #include <machine/cpu.h> /* XXX for softnet */
73 #include <machine/stdarg.h>
75 #include <netinet/in.h>
76 #include <netinet/in_systm.h>
77 #include <netinet/in_var.h>
80 #include <netinet/ip.h>
81 #include <netinet/tcp.h>
84 #if defined (__DragonFly__) || defined (__OpenBSD__)
85 # include <netinet/if_ether.h>
87 # include <net/ethertypes.h>
91 #include <netproto/ipx/ipx.h>
92 #include <netproto/ipx/ipx_if.h>
97 #define IOCTL_CMD_T u_long
98 #define MAXALIVECNT 3 /* max. alive packets */
101 * Interface flags that can be set in an ifconfig command.
103 * Setting link0 will make the link passive, i.e. it will be marked
104 * as being administrative openable, but won't be opened to begin
105 * with. Incoming calls will be answered, or subsequent calls with
106 * -link1 will cause the administrative open of the LCP layer.
108 * Setting link1 will cause the link to auto-dial only as packets
111 * Setting IFF_DEBUG will syslog the option negotiation and state
112 * transitions at level kern.debug. Note: all logs consistently look
115 * <if-name><unit>: <proto-name> <additional info...>
117 * with <if-name><unit> being something like "bppp0", and <proto-name>
118 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
121 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
122 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
123 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
125 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
126 #define PPP_UI 0x03 /* Unnumbered Information */
127 #define PPP_IP 0x0021 /* Internet Protocol */
128 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
129 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
130 #define PPP_IPX 0x002b /* Novell IPX Protocol */
131 #define PPP_VJ_COMP 0x002d /* VJ compressed TCP/IP */
132 #define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */
133 #define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */
134 #define PPP_LCP 0xc021 /* Link Control Protocol */
135 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
136 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
137 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
138 #define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */
140 #define CONF_REQ 1 /* PPP configure request */
141 #define CONF_ACK 2 /* PPP configure acknowledge */
142 #define CONF_NAK 3 /* PPP configure negative ack */
143 #define CONF_REJ 4 /* PPP configure reject */
144 #define TERM_REQ 5 /* PPP terminate request */
145 #define TERM_ACK 6 /* PPP terminate acknowledge */
146 #define CODE_REJ 7 /* PPP code reject */
147 #define PROTO_REJ 8 /* PPP protocol reject */
148 #define ECHO_REQ 9 /* PPP echo request */
149 #define ECHO_REPLY 10 /* PPP echo reply */
150 #define DISC_REQ 11 /* PPP discard request */
152 #define LCP_OPT_MRU 1 /* maximum receive unit */
153 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
154 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
155 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
156 #define LCP_OPT_MAGIC 5 /* magic number */
157 #define LCP_OPT_RESERVED 6 /* reserved */
158 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
159 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
161 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
162 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
163 #define IPCP_OPT_ADDRESS 3 /* local IP address */
165 #define IPV6CP_OPT_IFID 1 /* interface identifier */
166 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
168 #define IPCP_COMP_VJ 0x2d /* Code for VJ compression */
170 #define PAP_REQ 1 /* PAP name/password request */
171 #define PAP_ACK 2 /* PAP acknowledge */
172 #define PAP_NAK 3 /* PAP fail */
174 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
175 #define CHAP_RESPONSE 2 /* CHAP challenge response */
176 #define CHAP_SUCCESS 3 /* CHAP response ok */
177 #define CHAP_FAILURE 4 /* CHAP response failed */
179 #define CHAP_MD5 5 /* hash algorithm - MD5 */
181 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
182 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
183 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
184 #define CISCO_ADDR_REQ 0 /* Cisco address request */
185 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
186 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
188 /* states are named and numbered according to RFC 1661 */
189 #define STATE_INITIAL 0
190 #define STATE_STARTING 1
191 #define STATE_CLOSED 2
192 #define STATE_STOPPED 3
193 #define STATE_CLOSING 4
194 #define STATE_STOPPING 5
195 #define STATE_REQ_SENT 6
196 #define STATE_ACK_RCVD 7
197 #define STATE_ACK_SENT 8
198 #define STATE_OPENED 9
204 } __attribute__((__packed__));
205 #define PPP_HEADER_LEN sizeof (struct ppp_header)
211 } __attribute__((__packed__));
212 #define LCP_HEADER_LEN sizeof (struct lcp_header)
214 struct cisco_packet {
221 } __attribute__((__packed__));
222 #define CISCO_PACKET_LEN sizeof (struct cisco_packet)
225 * We follow the spelling and capitalization of RFC 1661 here, to make
226 * it easier comparing with the standard. Please refer to this RFC in
227 * case you can't make sense out of these abbreviation; it will also
228 * explain the semantics related to the various events and actions.
231 u_short proto; /* PPP control protocol number */
232 u_char protoidx; /* index into state table in struct sppp */
234 #define CP_LCP 0x01 /* this is the LCP */
235 #define CP_AUTH 0x02 /* this is an authentication protocol */
236 #define CP_NCP 0x04 /* this is a NCP */
237 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
238 const char *name; /* name of this control protocol */
240 void (*Up)(struct sppp *sp);
241 void (*Down)(struct sppp *sp);
242 void (*Open)(struct sppp *sp);
243 void (*Close)(struct sppp *sp);
244 void (*TO)(void *sp);
245 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
246 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
247 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
249 void (*tlu)(struct sppp *sp);
250 void (*tld)(struct sppp *sp);
251 void (*tls)(struct sppp *sp);
252 void (*tlf)(struct sppp *sp);
253 void (*scr)(struct sppp *sp);
256 static struct sppp *spppq;
257 #if defined(__DragonFly__)
258 static struct callout keepalive_timeout;
261 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 && !defined(__DragonFly__)
262 #define SPP_FMT "%s%d: "
263 #define SPP_ARGS(ifp) (ifp)->if_name, (ifp)->if_unit
265 #define SPP_FMT "%s: "
266 #define SPP_ARGS(ifp) (ifp)->if_xname
271 * The following disgusting hack gets around the problem that IP TOS
272 * can't be set yet. We want to put "interactive" traffic on a high
273 * priority queue. To decide if traffic is interactive, we check that
274 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
276 * XXX is this really still necessary? - joerg -
278 static u_short interactive_ports[8] = {
282 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
285 /* almost every function needs these */
287 struct ifnet *ifp = &sp->pp_if; \
288 int debug = ifp->if_flags & IFF_DEBUG
290 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
291 struct sockaddr *dst, struct rtentry *rt);
293 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
294 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
296 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
298 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
299 u_char ident, u_short len, void *data);
300 /* static void sppp_cp_timeout(void *arg); */
301 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
303 static void sppp_auth_send(const struct cp *cp,
304 struct sppp *sp, unsigned int type, unsigned int id,
307 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
308 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
309 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
310 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
311 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
313 static void sppp_null(struct sppp *sp);
315 static void sppp_lcp_init(struct sppp *sp);
316 static void sppp_lcp_up(struct sppp *sp);
317 static void sppp_lcp_down(struct sppp *sp);
318 static void sppp_lcp_open(struct sppp *sp);
319 static void sppp_lcp_close(struct sppp *sp);
320 static void sppp_lcp_TO(void *sp);
321 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
322 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
323 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
324 static void sppp_lcp_tlu(struct sppp *sp);
325 static void sppp_lcp_tld(struct sppp *sp);
326 static void sppp_lcp_tls(struct sppp *sp);
327 static void sppp_lcp_tlf(struct sppp *sp);
328 static void sppp_lcp_scr(struct sppp *sp);
329 static void sppp_lcp_check_and_close(struct sppp *sp);
330 static int sppp_ncp_check(struct sppp *sp);
332 static void sppp_ipcp_init(struct sppp *sp);
333 static void sppp_ipcp_up(struct sppp *sp);
334 static void sppp_ipcp_down(struct sppp *sp);
335 static void sppp_ipcp_open(struct sppp *sp);
336 static void sppp_ipcp_close(struct sppp *sp);
337 static void sppp_ipcp_TO(void *sp);
338 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
339 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
340 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
341 static void sppp_ipcp_tlu(struct sppp *sp);
342 static void sppp_ipcp_tld(struct sppp *sp);
343 static void sppp_ipcp_tls(struct sppp *sp);
344 static void sppp_ipcp_tlf(struct sppp *sp);
345 static void sppp_ipcp_scr(struct sppp *sp);
347 static void sppp_ipv6cp_init(struct sppp *sp);
348 static void sppp_ipv6cp_up(struct sppp *sp);
349 static void sppp_ipv6cp_down(struct sppp *sp);
350 static void sppp_ipv6cp_open(struct sppp *sp);
351 static void sppp_ipv6cp_close(struct sppp *sp);
352 static void sppp_ipv6cp_TO(void *sp);
353 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
354 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
355 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
356 static void sppp_ipv6cp_tlu(struct sppp *sp);
357 static void sppp_ipv6cp_tld(struct sppp *sp);
358 static void sppp_ipv6cp_tls(struct sppp *sp);
359 static void sppp_ipv6cp_tlf(struct sppp *sp);
360 static void sppp_ipv6cp_scr(struct sppp *sp);
362 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
363 static void sppp_pap_init(struct sppp *sp);
364 static void sppp_pap_open(struct sppp *sp);
365 static void sppp_pap_close(struct sppp *sp);
366 static void sppp_pap_TO(void *sp);
367 static void sppp_pap_my_TO(void *sp);
368 static void sppp_pap_tlu(struct sppp *sp);
369 static void sppp_pap_tld(struct sppp *sp);
370 static void sppp_pap_scr(struct sppp *sp);
372 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
373 static void sppp_chap_init(struct sppp *sp);
374 static void sppp_chap_open(struct sppp *sp);
375 static void sppp_chap_close(struct sppp *sp);
376 static void sppp_chap_TO(void *sp);
377 static void sppp_chap_tlu(struct sppp *sp);
378 static void sppp_chap_tld(struct sppp *sp);
379 static void sppp_chap_scr(struct sppp *sp);
381 static const char *sppp_auth_type_name(u_short proto, u_char type);
382 static const char *sppp_cp_type_name(u_char type);
383 static const char *sppp_dotted_quad(u_long addr);
384 static const char *sppp_ipcp_opt_name(u_char opt);
386 static const char *sppp_ipv6cp_opt_name(u_char opt);
388 static const char *sppp_lcp_opt_name(u_char opt);
389 static const char *sppp_phase_name(enum ppp_phase phase);
390 static const char *sppp_proto_name(u_short proto);
391 static const char *sppp_state_name(int state);
392 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
393 static int sppp_strnlen(u_char *p, int max);
394 static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst,
396 static void sppp_keepalive(void *dummy);
397 static void sppp_phase_network(struct sppp *sp);
398 static void sppp_print_bytes(const u_char *p, u_short len);
399 static void sppp_print_string(const char *p, u_short len);
400 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
402 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
403 struct in6_addr *dst, struct in6_addr *srcmask);
404 #ifdef IPV6CP_MYIFID_DYN
405 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
406 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
408 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
411 /* our control protocol descriptors */
412 static const struct cp lcp = {
413 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
414 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
415 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
416 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
420 static const struct cp ipcp = {
421 PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
422 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
423 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
424 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
428 static const struct cp ipv6cp = {
429 PPP_IPV6CP, IDX_IPV6CP,
430 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
436 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
437 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
438 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
442 static const struct cp pap = {
443 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
444 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
445 sppp_pap_TO, 0, 0, 0,
446 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
450 static const struct cp chap = {
451 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
452 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
453 sppp_chap_TO, 0, 0, 0,
454 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
458 static const struct cp *cps[IDX_COUNT] = {
460 &ipcp, /* IDX_IPCP */
461 &ipv6cp, /* IDX_IPV6CP */
463 &chap, /* IDX_CHAP */
467 sppp_modevent(module_t mod, int type, void *unused)
471 callout_init(&keepalive_timeout);
481 static moduledata_t spppmod = {
486 MODULE_VERSION(sppp, 1);
487 DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
490 * Exported functions, comprising our interface to the lower layer.
494 * Process the received packet.
497 sppp_input(struct ifnet *ifp, struct mbuf *m)
499 struct ppp_header *h;
501 struct sppp *sp = (struct sppp *)ifp;
503 int hlen, vjlen, do_account = 0;
504 int debug = ifp->if_flags & IFF_DEBUG;
506 if (ifp->if_flags & IFF_UP)
507 /* Count received bytes, add FCS and one flag */
508 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len + 3);
510 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
511 /* Too small packet, drop it. */
514 SPP_FMT "input packet is too small, %d bytes\n",
515 SPP_ARGS(ifp), m->m_pkthdr.len);
519 IFNET_STAT_INC(ifp, ierrors, 1);
520 IFNET_STAT_INC(ifp, iqdrops, 1);
524 /* Get PPP header. */
525 h = mtod (m, struct ppp_header*);
526 m_adj (m, PPP_HEADER_LEN);
528 switch (h->address) {
529 case PPP_ALLSTATIONS:
530 if (h->control != PPP_UI)
532 if (sp->pp_mode == IFF_CISCO) {
535 SPP_FMT "PPP packet in Cisco mode "
536 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
538 h->address, h->control, ntohs(h->protocol));
541 switch (ntohs (h->protocol)) {
545 SPP_FMT "rejecting protocol "
546 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
548 h->address, h->control, ntohs(h->protocol));
549 if (sp->state[IDX_LCP] == STATE_OPENED)
550 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
551 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
553 IFNET_STAT_INC(ifp, noproto, 1);
556 sppp_cp_input(&lcp, sp, m);
560 if (sp->pp_phase >= PHASE_AUTHENTICATE)
561 sppp_pap_input(sp, m);
565 if (sp->pp_phase >= PHASE_AUTHENTICATE)
566 sppp_chap_input(sp, m);
571 if (sp->pp_phase == PHASE_NETWORK)
572 sppp_cp_input(&ipcp, sp, m);
576 if (sp->state[IDX_IPCP] == STATE_OPENED) {
582 if (sp->state[IDX_IPCP] == STATE_OPENED) {
584 sl_uncompress_tcp_core(mtod(m, u_char *),
588 &iphdr, &hlen)) <= 0) {
591 SPP_FMT "VJ uncompress failed on compressed packet\n",
597 * Trim the VJ header off the packet, and prepend
598 * the uncompressed IP header (which will usually
599 * end up in two chained mbufs since there's not
600 * enough leading space in the existing mbuf).
603 M_PREPEND(m, hlen, MB_DONTWAIT);
606 bcopy(iphdr, mtod(m, u_char *), hlen);
613 if (sp->state[IDX_IPCP] == STATE_OPENED) {
614 if (sl_uncompress_tcp_core(mtod(m, u_char *),
616 TYPE_UNCOMPRESSED_TCP,
618 &iphdr, &hlen) != 0) {
621 SPP_FMT "VJ uncompress failed on uncompressed packet\n",
632 if (sp->pp_phase == PHASE_NETWORK)
633 sppp_cp_input(&ipv6cp, sp, m);
638 if (sp->state[IDX_IPV6CP] == STATE_OPENED) {
646 /* IPX IPXCP not implemented yet */
647 if (sp->pp_phase == PHASE_NETWORK) {
655 case CISCO_MULTICAST:
657 /* Don't check the control field here (RFC 1547). */
658 if (sp->pp_mode != IFF_CISCO) {
661 SPP_FMT "Cisco packet in PPP mode "
662 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
664 h->address, h->control, ntohs(h->protocol));
667 switch (ntohs (h->protocol)) {
669 IFNET_STAT_INC(ifp, noproto, 1);
671 case CISCO_KEEPALIVE:
672 sppp_cisco_input ((struct sppp*) ifp, m);
695 default: /* Invalid PPP packet. */
699 SPP_FMT "invalid input packet "
700 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
702 h->address, h->control, ntohs(h->protocol));
706 if (! (ifp->if_flags & IFF_UP) || isr < 0)
711 netisr_queue(isr, m);
714 * Do only account for network packets, not for control
715 * packets. This is used by some subsystems to detect
719 sp->pp_last_recv = time_second;
723 * Enqueue transmit packet.
726 sppp_output_serialized(struct ifnet *ifp, struct ifaltq_subque *ifsq,
727 struct mbuf *m, struct sockaddr *dst, struct rtentry *rt)
729 struct sppp *sp = (struct sppp*) ifp;
730 struct ppp_header *h;
731 struct ifqueue *ifq = NULL;
733 int ipproto = PPP_IP;
734 int debug = ifp->if_flags & IFF_DEBUG;
735 struct altq_pktattr pktattr;
739 if ((ifp->if_flags & IFF_UP) == 0 ||
740 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
749 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
754 * Hack to prevent the initialization-time generated
755 * IPv6 multicast packet to erroneously cause a
756 * dialout event in case IPv6 has been
757 * administratively disabled on that interface.
759 if (dst->sa_family == AF_INET6 &&
760 !(sp->confflags & CONF_ENABLE_IPV6))
764 * Interface is not yet running, but auto-dial. Need
765 * to start LCP for it.
767 ifp->if_flags |= IFF_RUNNING;
774 * if the queueing discipline needs packet classification,
775 * do it before prepending link headers.
777 ifq_classify(&ifp->if_snd, m, dst->sa_family, &pktattr);
780 if (dst->sa_family == AF_INET) {
781 /* XXX Check mbuf length here? */
782 struct ip *ip = mtod (m, struct ip*);
783 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
786 * When using dynamic local IP address assignment by using
787 * 0.0.0.0 as a local address, the first TCP session will
788 * not connect because the local TCP checksum is computed
789 * using 0.0.0.0 which will later become our real IP address
790 * so the TCP checksum computed at the remote end will
791 * become invalid. So we
792 * - don't let packets with src ip addr 0 thru
793 * - we flag TCP packets with src ip 0 as an error
796 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
800 if(ip->ip_p == IPPROTO_TCP)
801 return(EADDRNOTAVAIL);
807 * Put low delay, telnet, rlogin and ftp control packets
808 * in front of the queue.
810 if (IF_QFULL (&sp->pp_fastq))
812 else if (ip->ip_tos & IPTOS_LOWDELAY)
814 else if (m->m_len < sizeof *ip + sizeof *tcp)
816 else if (ip->ip_p != IPPROTO_TCP)
818 else if (INTERACTIVE (ntohs (tcp->th_sport)))
820 else if (INTERACTIVE (ntohs (tcp->th_dport)))
824 * Do IP Header compression
826 if (sp->pp_mode != IFF_CISCO && (sp->ipcp.flags & IPCP_VJ) &&
827 ip->ip_p == IPPROTO_TCP)
828 switch (sl_compress_tcp(m, ip, sp->pp_comp,
829 sp->ipcp.compress_cid)) {
830 case TYPE_COMPRESSED_TCP:
831 ipproto = PPP_VJ_COMP;
833 case TYPE_UNCOMPRESSED_TCP:
834 ipproto = PPP_VJ_UCOMP;
848 if (dst->sa_family == AF_INET6) {
849 /* XXX do something tricky here? */
854 * Prepend general data packet PPP header. For now, IP only.
856 M_PREPEND (m, PPP_HEADER_LEN, MB_DONTWAIT);
859 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
861 IFNET_STAT_INC(ifp, oerrors, 1);
866 * May want to check size of packet
867 * (albeit due to the implementation it's always enough)
869 h = mtod (m, struct ppp_header*);
870 if (sp->pp_mode == IFF_CISCO) {
871 h->address = CISCO_UNICAST; /* unicast address */
874 h->address = PPP_ALLSTATIONS; /* broadcast address */
875 h->control = PPP_UI; /* Unnumbered Info */
878 switch (dst->sa_family) {
880 case AF_INET: /* Internet Protocol */
881 if (sp->pp_mode == IFF_CISCO)
882 h->protocol = htons (ETHERTYPE_IP);
885 * Don't choke with an ENETDOWN early. It's
886 * possible that we just started dialing out,
887 * so don't drop the packet immediately. If
888 * we notice that we run out of buffer space
889 * below, we will however remember that we are
890 * not ready to carry IP packets, and return
891 * ENETDOWN, as opposed to ENOBUFS.
893 h->protocol = htons(ipproto);
894 if (sp->state[IDX_IPCP] != STATE_OPENED)
900 case AF_INET6: /* Internet Protocol */
901 if (sp->pp_mode == IFF_CISCO)
902 h->protocol = htons (ETHERTYPE_IPV6);
905 * Don't choke with an ENETDOWN early. It's
906 * possible that we just started dialing out,
907 * so don't drop the packet immediately. If
908 * we notice that we run out of buffer space
909 * below, we will however remember that we are
910 * not ready to carry IP packets, and return
911 * ENETDOWN, as opposed to ENOBUFS.
913 h->protocol = htons(PPP_IPV6);
914 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
920 case AF_IPX: /* Novell IPX Protocol */
921 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
922 ETHERTYPE_IPX : PPP_IPX);
927 IFNET_STAT_INC(ifp, oerrors, 1);
929 return (EAFNOSUPPORT);
933 * Queue message on interface, and start output if interface
946 rv = ifsq_enqueue(ifsq, m, &pktattr);
949 IFNET_STAT_INC(ifp, oerrors, 1);
953 if (!ifsq_is_oactive(ifsq))
954 (*ifp->if_start) (ifp, ifsq);
957 * Count output packets and bytes.
958 * The packet length includes header, FCS and 1 flag,
959 * according to RFC 1333.
961 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len + 3);
964 * Unlike in sppp_input(), we can always bump the timestamp
965 * here since sppp_output() is only called on behalf of
966 * network-layer traffic; control-layer traffic is handled
969 sp->pp_last_sent = time_second;
976 sppp_output(struct ifnet *ifp, struct mbuf *m,
977 struct sockaddr *dst, struct rtentry *rt)
979 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
982 ifnet_serialize_tx(ifp, ifsq);
983 error = sppp_output_serialized(ifp, ifsq, m, dst, rt);
984 ifnet_deserialize_tx(ifp, ifsq);
990 sppp_attach(struct ifnet *ifp)
992 struct sppp *sp = (struct sppp*) ifp;
994 /* Initialize keepalive handler. */
996 callout_reset(&keepalive_timeout, hz * 10,
997 sppp_keepalive, NULL);
999 /* Insert new entry into the keepalive list. */
1000 sp->pp_next = spppq;
1003 sp->pp_if.if_mtu = PP_MTU;
1004 sp->pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
1005 sp->pp_if.if_type = IFT_PPP;
1006 sp->pp_if.if_output = sppp_output;
1008 sp->pp_flags = PP_KEEPALIVE;
1010 ifq_set_maxlen(&sp->pp_if.if_snd, 32);
1011 sp->pp_fastq.ifq_maxlen = 32;
1012 sp->pp_cpq.ifq_maxlen = 20;
1014 sp->pp_alivecnt = 0;
1015 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
1016 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
1017 sp->pp_phase = PHASE_DEAD;
1019 sp->pp_down = lcp.Down;
1020 sp->pp_last_recv = sp->pp_last_sent = time_second;
1023 sp->confflags |= CONF_ENABLE_VJ;
1026 sp->confflags |= CONF_ENABLE_IPV6;
1028 sp->pp_comp = kmalloc(sizeof(struct slcompress), M_TEMP, M_WAITOK);
1029 sl_compress_init(sp->pp_comp, -1);
1032 sppp_ipv6cp_init(sp);
1038 sppp_detach(struct ifnet *ifp)
1040 struct sppp **q, *p, *sp = (struct sppp*) ifp;
1043 /* Remove the entry from the keepalive list. */
1044 for (q = &spppq; (p = *q); q = &p->pp_next)
1050 /* Stop keepalive handler. */
1052 callout_stop(&keepalive_timeout);
1054 for (i = 0; i < IDX_COUNT; i++)
1055 callout_stop(&sp->timeout[i]);
1056 callout_stop(&sp->pap_my_to);
1060 * Flush the interface output queue.
1063 sppp_flush(struct ifnet *ifp)
1065 struct sppp *sp = (struct sppp*) ifp;
1067 ifq_purge_all(&sp->pp_if.if_snd);
1068 IF_DRAIN(&sp->pp_fastq);
1069 IF_DRAIN(&sp->pp_cpq);
1073 * Check if the output queue is empty.
1076 sppp_isempty(struct ifnet *ifp)
1078 struct sppp *sp = (struct sppp*) ifp;
1082 empty = IF_QEMPTY(&sp->pp_fastq) && IF_QEMPTY(&sp->pp_cpq) &&
1083 ifsq_is_empty(ifq_get_subq_default(&sp->pp_if.if_snd));
1089 * Get next packet to send.
1092 sppp_dequeue(struct ifnet *ifp)
1094 struct sppp *sp = (struct sppp*) ifp;
1100 * Process only the control protocol queue until we have at
1101 * least one NCP open.
1103 * Do always serve all three queues in Cisco mode.
1105 IF_DEQUEUE(&sp->pp_cpq, m);
1107 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO)) {
1108 IF_DEQUEUE(&sp->pp_fastq, m);
1111 ifq_get_subq_default(&sp->pp_if.if_snd), NULL);
1120 * Pick the next packet, do not remove it from the queue.
1123 sppp_pick(struct ifnet *ifp)
1125 struct sppp *sp = (struct sppp*)ifp;
1130 m = sp->pp_cpq.ifq_head;
1132 (sp->pp_phase == PHASE_NETWORK || sp->pp_mode == IFF_CISCO)) {
1133 if ((m = sp->pp_fastq.ifq_head) == NULL)
1134 m = ifsq_poll(ifq_get_subq_default(&sp->pp_if.if_snd));
1142 * Process an ioctl request. Called on low priority level.
1145 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1147 struct ifreq *ifr = (struct ifreq*) data;
1148 struct sppp *sp = (struct sppp*) ifp;
1149 int rv, going_up, going_down, newmode;
1156 case SIOCSIFDSTADDR:
1160 /* set the interface "up" when assigning an IP address */
1161 ifp->if_flags |= IFF_UP;
1162 /* fall through... */
1165 going_up = ifp->if_flags & IFF_UP &&
1166 (ifp->if_flags & IFF_RUNNING) == 0;
1167 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1168 ifp->if_flags & IFF_RUNNING;
1170 newmode = ifp->if_flags & IFF_PASSIVE;
1172 newmode = ifp->if_flags & IFF_AUTO;
1174 newmode = ifp->if_flags & IFF_CISCO;
1175 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1176 ifp->if_flags |= newmode;
1178 if (newmode != sp->pp_mode) {
1181 going_up = ifp->if_flags & IFF_RUNNING;
1185 if (sp->pp_mode != IFF_CISCO)
1187 else if (sp->pp_tlf)
1190 ifp->if_flags &= ~IFF_RUNNING;
1191 sp->pp_mode = newmode;
1195 if (sp->pp_mode != IFF_CISCO)
1197 sp->pp_mode = newmode;
1198 if (sp->pp_mode == 0) {
1199 ifp->if_flags |= IFF_RUNNING;
1202 if (sp->pp_mode == IFF_CISCO) {
1205 ifp->if_flags |= IFF_RUNNING;
1213 #define ifr_mtu ifr_metric
1216 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1218 ifp->if_mtu = ifr->ifr_mtu;
1223 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1225 ifp->if_mtu = *(short*)data;
1230 ifr->ifr_mtu = ifp->if_mtu;
1235 *(short*)data = ifp->if_mtu;
1242 case SIOCGIFGENERIC:
1243 case SIOCSIFGENERIC:
1244 rv = sppp_params(sp, cmd, data);
1256 * Cisco framing implementation.
1260 * Handle incoming Cisco keepalive protocol packets.
1263 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1266 struct cisco_packet *h;
1269 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1272 SPP_FMT "cisco invalid packet length: %d bytes\n",
1273 SPP_ARGS(ifp), m->m_pkthdr.len);
1276 h = mtod (m, struct cisco_packet*);
1279 SPP_FMT "cisco input: %d bytes "
1280 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1281 SPP_ARGS(ifp), m->m_pkthdr.len,
1282 (u_long)ntohl (h->type), h->par1, h->par2, (u_int)h->rel,
1283 (u_int)h->time0, (u_int)h->time1);
1284 switch (ntohl (h->type)) {
1287 log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n",
1288 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1290 case CISCO_ADDR_REPLY:
1291 /* Reply on address request, ignore */
1293 case CISCO_KEEPALIVE_REQ:
1294 sp->pp_alivecnt = 0;
1295 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1296 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1297 /* Local and remote sequence numbers are equal.
1298 * Probably, the line is in loopback mode. */
1299 if (sp->pp_loopcnt >= MAXALIVECNT) {
1300 kprintf (SPP_FMT "loopback\n",
1303 if (ifp->if_flags & IFF_UP) {
1305 IF_DRAIN(&sp->pp_cpq);
1310 /* Generate new local sequence number */
1311 #if defined(__DragonFly__)
1312 sp->pp_seq[IDX_LCP] = krandom();
1314 sp->pp_seq[IDX_LCP] ^= time.tv_sec ^ time.tv_usec;
1319 if (! (ifp->if_flags & IFF_UP) &&
1320 (ifp->if_flags & IFF_RUNNING)) {
1322 kprintf (SPP_FMT "up\n", SPP_ARGS(ifp));
1325 case CISCO_ADDR_REQ:
1326 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1328 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1334 * Send Cisco keepalive packet.
1337 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1340 struct ppp_header *h;
1341 struct cisco_packet *ch;
1343 #if defined(__DragonFly__)
1346 u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
1348 struct ifaltq_subque *ifsq;
1350 #if defined(__DragonFly__)
1351 getmicrouptime(&tv);
1354 MGETHDR (m, MB_DONTWAIT, MT_DATA);
1357 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1358 m->m_pkthdr.rcvif = 0;
1360 h = mtod (m, struct ppp_header*);
1361 h->address = CISCO_MULTICAST;
1363 h->protocol = htons (CISCO_KEEPALIVE);
1365 ch = (struct cisco_packet*) (h + 1);
1366 ch->type = htonl (type);
1367 ch->par1 = htonl (par1);
1368 ch->par2 = htonl (par2);
1371 #if defined(__DragonFly__)
1372 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1373 ch->time1 = htons ((u_short) tv.tv_sec);
1375 ch->time0 = htons ((u_short) (t >> 16));
1376 ch->time1 = htons ((u_short) t);
1381 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1382 SPP_ARGS(ifp), (u_long)ntohl (ch->type), ch->par1,
1383 ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1385 if (IF_QFULL (&sp->pp_cpq)) {
1386 IF_DROP (&sp->pp_fastq);
1389 IF_ENQUEUE (&sp->pp_cpq, m);
1390 ifsq = ifq_get_subq_default(&ifp->if_snd);
1391 if (!ifsq_is_oactive(ifsq))
1392 (*ifp->if_start) (ifp, ifsq);
1393 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len + 3);
1397 * PPP protocol implementation.
1401 * Send PPP control protocol packet.
1404 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1405 u_char ident, u_short len, void *data)
1408 struct ppp_header *h;
1409 struct lcp_header *lh;
1411 struct ifaltq_subque *ifsq;
1413 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1414 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1415 MGETHDR (m, MB_DONTWAIT, MT_DATA);
1418 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1419 m->m_pkthdr.rcvif = 0;
1421 h = mtod (m, struct ppp_header*);
1422 h->address = PPP_ALLSTATIONS; /* broadcast address */
1423 h->control = PPP_UI; /* Unnumbered Info */
1424 h->protocol = htons (proto); /* Link Control Protocol */
1426 lh = (struct lcp_header*) (h + 1);
1429 lh->len = htons (LCP_HEADER_LEN + len);
1431 bcopy (data, lh+1, len);
1434 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1436 sppp_proto_name(proto),
1437 sppp_cp_type_name (lh->type), lh->ident,
1439 sppp_print_bytes ((u_char*) (lh+1), len);
1442 if (IF_QFULL (&sp->pp_cpq)) {
1443 IF_DROP (&sp->pp_fastq);
1445 IFNET_STAT_INC(ifp, oerrors, 1);
1447 IF_ENQUEUE (&sp->pp_cpq, m);
1448 ifsq = ifq_get_subq_default(&ifp->if_snd);
1449 if (!ifsq_is_oactive(ifsq))
1450 (*ifp->if_start) (ifp, ifsq);
1451 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len + 3);
1455 * Handle incoming PPP control protocol packets.
1458 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1461 struct lcp_header *h;
1462 int printlen, len = m->m_pkthdr.len;
1469 SPP_FMT "%s invalid packet length: %d bytes\n",
1470 SPP_ARGS(ifp), cp->name, len);
1473 h = mtod (m, struct lcp_header*);
1475 printlen = ntohs(h->len);
1477 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1478 SPP_ARGS(ifp), cp->name,
1479 sppp_state_name(sp->state[cp->protoidx]),
1480 sppp_cp_type_name (h->type), h->ident, printlen);
1484 sppp_print_bytes ((u_char*) (h+1), printlen - 4);
1487 if (len > ntohs (h->len))
1488 len = ntohs (h->len);
1489 p = (u_char *)(h + 1);
1494 log(-1, SPP_FMT "%s invalid conf-req length %d\n",
1495 SPP_ARGS(ifp), cp->name,
1497 IFNET_STAT_INC(ifp, ierrors, 1);
1500 /* handle states where RCR doesn't get a SCA/SCN */
1501 switch (sp->state[cp->protoidx]) {
1503 case STATE_STOPPING:
1506 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1510 rv = (cp->RCR)(sp, h, len);
1512 /* fatal error, shut down */
1517 switch (sp->state[cp->protoidx]) {
1521 /* fall through... */
1522 case STATE_ACK_SENT:
1523 case STATE_REQ_SENT:
1525 * sppp_cp_change_state() have the side effect of
1526 * restarting the timeouts. We want to avoid that
1527 * if the state don't change, otherwise we won't
1528 * ever timeout and resend a configuration request
1531 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1534 sppp_cp_change_state(cp, sp, rv?
1535 STATE_ACK_SENT: STATE_REQ_SENT);
1538 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1540 sppp_cp_change_state(cp, sp, rv?
1541 STATE_ACK_SENT: STATE_REQ_SENT);
1543 case STATE_ACK_RCVD:
1545 sppp_cp_change_state(cp, sp, STATE_OPENED);
1547 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1552 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1555 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1556 SPP_ARGS(ifp), cp->name,
1557 sppp_cp_type_name(h->type),
1558 sppp_state_name(sp->state[cp->protoidx]));
1559 IFNET_STAT_INC(ifp, ierrors, 1);
1563 if (h->ident != sp->confid[cp->protoidx]) {
1565 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1566 SPP_ARGS(ifp), cp->name,
1567 h->ident, sp->confid[cp->protoidx]);
1568 IFNET_STAT_INC(ifp, ierrors, 1);
1571 switch (sp->state[cp->protoidx]) {
1574 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1577 case STATE_STOPPING:
1579 case STATE_REQ_SENT:
1580 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1581 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1586 case STATE_ACK_RCVD:
1588 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1590 case STATE_ACK_SENT:
1591 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1592 sppp_cp_change_state(cp, sp, STATE_OPENED);
1594 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1595 SPP_ARGS(ifp), cp->name);
1599 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1600 SPP_ARGS(ifp), cp->name,
1601 sppp_cp_type_name(h->type),
1602 sppp_state_name(sp->state[cp->protoidx]));
1603 IFNET_STAT_INC(ifp, ierrors, 1);
1608 if (h->ident != sp->confid[cp->protoidx]) {
1610 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1611 SPP_ARGS(ifp), cp->name,
1612 h->ident, sp->confid[cp->protoidx]);
1613 IFNET_STAT_INC(ifp, ierrors, 1);
1616 if (h->type == CONF_NAK)
1617 (cp->RCN_nak)(sp, h, len);
1619 (cp->RCN_rej)(sp, h, len);
1621 switch (sp->state[cp->protoidx]) {
1624 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1626 case STATE_REQ_SENT:
1627 case STATE_ACK_SENT:
1628 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1630 * Slow things down a bit if we think we might be
1631 * in loopback. Depend on the timeout to send the
1632 * next configuration request.
1641 case STATE_ACK_RCVD:
1642 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1646 case STATE_STOPPING:
1649 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1650 SPP_ARGS(ifp), cp->name,
1651 sppp_cp_type_name(h->type),
1652 sppp_state_name(sp->state[cp->protoidx]));
1653 IFNET_STAT_INC(ifp, ierrors, 1);
1658 switch (sp->state[cp->protoidx]) {
1659 case STATE_ACK_RCVD:
1660 case STATE_ACK_SENT:
1661 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1666 case STATE_STOPPING:
1667 case STATE_REQ_SENT:
1669 /* Send Terminate-Ack packet. */
1671 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1672 SPP_ARGS(ifp), cp->name);
1673 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1677 sp->rst_counter[cp->protoidx] = 0;
1678 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1682 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1683 SPP_ARGS(ifp), cp->name,
1684 sppp_cp_type_name(h->type),
1685 sppp_state_name(sp->state[cp->protoidx]));
1686 IFNET_STAT_INC(ifp, ierrors, 1);
1690 switch (sp->state[cp->protoidx]) {
1693 case STATE_REQ_SENT:
1694 case STATE_ACK_SENT:
1697 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1700 case STATE_STOPPING:
1701 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1704 case STATE_ACK_RCVD:
1705 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1710 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1713 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1714 SPP_ARGS(ifp), cp->name,
1715 sppp_cp_type_name(h->type),
1716 sppp_state_name(sp->state[cp->protoidx]));
1717 IFNET_STAT_INC(ifp, ierrors, 1);
1721 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1723 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1724 "danger will robinson\n",
1725 SPP_ARGS(ifp), cp->name,
1726 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1727 switch (sp->state[cp->protoidx]) {
1730 case STATE_REQ_SENT:
1731 case STATE_ACK_SENT:
1733 case STATE_STOPPING:
1736 case STATE_ACK_RCVD:
1737 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1740 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1741 SPP_ARGS(ifp), cp->name,
1742 sppp_cp_type_name(h->type),
1743 sppp_state_name(sp->state[cp->protoidx]));
1744 IFNET_STAT_INC(ifp, ierrors, 1);
1750 const struct cp *upper;
1756 proto = ntohs(*((u_int16_t *)p));
1757 for (i = 0; i < IDX_COUNT; i++) {
1758 if (cps[i]->proto == proto) {
1766 if (catastrophic || debug)
1767 log(catastrophic? LOG_INFO: LOG_DEBUG,
1768 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1769 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1770 sppp_cp_type_name(h->type), proto,
1771 upper ? upper->name : "unknown",
1772 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1775 * if we got RXJ+ against conf-req, the peer does not implement
1776 * this particular protocol type. terminate the protocol.
1778 if (upper && !catastrophic) {
1779 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1785 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1786 switch (sp->state[cp->protoidx]) {
1789 case STATE_REQ_SENT:
1790 case STATE_ACK_SENT:
1792 case STATE_STOPPING:
1795 case STATE_ACK_RCVD:
1796 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1799 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1800 SPP_ARGS(ifp), cp->name,
1801 sppp_cp_type_name(h->type),
1802 sppp_state_name(sp->state[cp->protoidx]));
1803 IFNET_STAT_INC(ifp, ierrors, 1);
1808 if (cp->proto != PPP_LCP)
1810 /* Discard the packet. */
1813 if (cp->proto != PPP_LCP)
1815 if (sp->state[cp->protoidx] != STATE_OPENED) {
1817 log(-1, SPP_FMT "lcp echo req but lcp closed\n",
1819 IFNET_STAT_INC(ifp, ierrors, 1);
1824 log(-1, SPP_FMT "invalid lcp echo request "
1825 "packet length: %d bytes\n",
1826 SPP_ARGS(ifp), len);
1829 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1830 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1831 /* Line loopback mode detected. */
1832 kprintf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1833 sp->pp_loopcnt = MAXALIVECNT * 5;
1835 IF_DRAIN(&sp->pp_cpq);
1837 /* Shut down the PPP link. */
1843 *(long*)(h+1) = htonl (sp->lcp.magic);
1845 log(-1, SPP_FMT "got lcp echo req, sending echo rep\n",
1847 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1850 if (cp->proto != PPP_LCP)
1852 if (h->ident != sp->lcp.echoid) {
1853 IFNET_STAT_INC(ifp, ierrors, 1);
1858 log(-1, SPP_FMT "lcp invalid echo reply "
1859 "packet length: %d bytes\n",
1860 SPP_ARGS(ifp), len);
1864 log(-1, SPP_FMT "lcp got echo rep\n",
1866 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1867 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1868 sp->pp_alivecnt = 0;
1871 /* Unknown packet type -- send Code-Reject packet. */
1874 log(-1, SPP_FMT "%s send code-rej for 0x%x\n",
1875 SPP_ARGS(ifp), cp->name, h->type);
1876 sppp_cp_send(sp, cp->proto, CODE_REJ,
1877 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1878 IFNET_STAT_INC(ifp, ierrors, 1);
1884 * The generic part of all Up/Down/Open/Close/TO event handlers.
1885 * Basically, the state transition handling in the automaton.
1888 sppp_up_event(const struct cp *cp, struct sppp *sp)
1893 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1894 SPP_ARGS(ifp), cp->name,
1895 sppp_state_name(sp->state[cp->protoidx]));
1897 switch (sp->state[cp->protoidx]) {
1899 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1901 case STATE_STARTING:
1902 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1904 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1907 kprintf(SPP_FMT "%s illegal up in state %s\n",
1908 SPP_ARGS(ifp), cp->name,
1909 sppp_state_name(sp->state[cp->protoidx]));
1914 sppp_down_event(const struct cp *cp, struct sppp *sp)
1919 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1920 SPP_ARGS(ifp), cp->name,
1921 sppp_state_name(sp->state[cp->protoidx]));
1923 switch (sp->state[cp->protoidx]) {
1926 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1929 sppp_cp_change_state(cp, sp, STATE_STARTING);
1932 case STATE_STOPPING:
1933 case STATE_REQ_SENT:
1934 case STATE_ACK_RCVD:
1935 case STATE_ACK_SENT:
1936 sppp_cp_change_state(cp, sp, STATE_STARTING);
1940 sppp_cp_change_state(cp, sp, STATE_STARTING);
1943 kprintf(SPP_FMT "%s illegal down in state %s\n",
1944 SPP_ARGS(ifp), cp->name,
1945 sppp_state_name(sp->state[cp->protoidx]));
1951 sppp_open_event(const struct cp *cp, struct sppp *sp)
1956 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1957 SPP_ARGS(ifp), cp->name,
1958 sppp_state_name(sp->state[cp->protoidx]));
1960 switch (sp->state[cp->protoidx]) {
1962 sppp_cp_change_state(cp, sp, STATE_STARTING);
1965 case STATE_STARTING:
1968 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1970 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1974 * Try escaping stopped state. This seems to bite
1975 * people occasionally, in particular for IPCP,
1976 * presumably following previous IPCP negotiation
1977 * aborts. Somehow, we must have missed a Down event
1978 * which would have caused a transition into starting
1979 * state, so as a bandaid we force the Down event now.
1980 * This effectively implements (something like the)
1981 * `restart' option mentioned in the state transition
1982 * table of RFC 1661.
1984 sppp_cp_change_state(cp, sp, STATE_STARTING);
1987 case STATE_STOPPING:
1988 case STATE_REQ_SENT:
1989 case STATE_ACK_RCVD:
1990 case STATE_ACK_SENT:
1994 sppp_cp_change_state(cp, sp, STATE_STOPPING);
2001 sppp_close_event(const struct cp *cp, struct sppp *sp)
2006 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
2007 SPP_ARGS(ifp), cp->name,
2008 sppp_state_name(sp->state[cp->protoidx]));
2010 switch (sp->state[cp->protoidx]) {
2015 case STATE_STARTING:
2016 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2020 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2022 case STATE_STOPPING:
2023 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2028 case STATE_REQ_SENT:
2029 case STATE_ACK_RCVD:
2030 case STATE_ACK_SENT:
2031 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2032 sppp_cp_send(sp, cp->proto, TERM_REQ,
2033 ++sp->pp_seq[cp->protoidx], 0, 0);
2034 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2040 sppp_to_event(const struct cp *cp, struct sppp *sp)
2047 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
2048 SPP_ARGS(ifp), cp->name,
2049 sppp_state_name(sp->state[cp->protoidx]),
2050 sp->rst_counter[cp->protoidx]);
2052 if (--sp->rst_counter[cp->protoidx] < 0)
2054 switch (sp->state[cp->protoidx]) {
2056 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2059 case STATE_STOPPING:
2060 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2063 case STATE_REQ_SENT:
2064 case STATE_ACK_RCVD:
2065 case STATE_ACK_SENT:
2066 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2072 switch (sp->state[cp->protoidx]) {
2074 case STATE_STOPPING:
2075 sppp_cp_send(sp, cp->proto, TERM_REQ,
2076 ++sp->pp_seq[cp->protoidx], 0, 0);
2077 callout_reset(&sp->timeout[cp->protoidx],
2078 sp->lcp.timeout, cp->TO, sp);
2080 case STATE_REQ_SENT:
2081 case STATE_ACK_RCVD:
2083 /* sppp_cp_change_state() will restart the timer */
2084 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2086 case STATE_ACK_SENT:
2088 callout_reset(&sp->timeout[cp->protoidx],
2089 sp->lcp.timeout, cp->TO, sp);
2097 * Change the state of a control protocol in the state automaton.
2098 * Takes care of starting/stopping the restart timer.
2101 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2103 sp->state[cp->protoidx] = newstate;
2104 callout_stop(&sp->timeout[cp->protoidx]);
2108 case STATE_STARTING:
2114 case STATE_STOPPING:
2115 case STATE_REQ_SENT:
2116 case STATE_ACK_RCVD:
2117 case STATE_ACK_SENT:
2118 callout_reset(&sp->timeout[cp->protoidx],
2119 sp->lcp.timeout, cp->TO, sp);
2125 *--------------------------------------------------------------------------*
2127 * The LCP implementation. *
2129 *--------------------------------------------------------------------------*
2132 sppp_lcp_init(struct sppp *sp)
2134 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2136 sp->state[IDX_LCP] = STATE_INITIAL;
2137 sp->fail_counter[IDX_LCP] = 0;
2138 sp->pp_seq[IDX_LCP] = 0;
2139 sp->pp_rseq[IDX_LCP] = 0;
2141 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2143 /* Note that these values are relevant for all control protocols */
2144 sp->lcp.timeout = 3 * hz;
2145 sp->lcp.max_terminate = 2;
2146 sp->lcp.max_configure = 10;
2147 sp->lcp.max_failure = 10;
2148 #if defined(__DragonFly__)
2149 callout_init(&sp->timeout[IDX_LCP]);
2154 sppp_lcp_up(struct sppp *sp)
2158 sp->pp_alivecnt = 0;
2159 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2162 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2164 * If this interface is passive or dial-on-demand, and we are
2165 * still in Initial state, it means we've got an incoming
2166 * call. Activate the interface.
2168 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2171 SPP_FMT "Up event", SPP_ARGS(ifp));
2172 ifp->if_flags |= IFF_RUNNING;
2173 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2175 log(-1, "(incoming call)\n");
2176 sp->pp_flags |= PP_CALLIN;
2180 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2181 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2182 ifp->if_flags |= IFF_RUNNING;
2186 sppp_up_event(&lcp, sp);
2190 sppp_lcp_down(struct sppp *sp)
2194 sppp_down_event(&lcp, sp);
2197 * If this is neither a dial-on-demand nor a passive
2198 * interface, simulate an ``ifconfig down'' action, so the
2199 * administrator can force a redial by another ``ifconfig
2200 * up''. XXX For leased line operation, should we immediately
2201 * try to reopen the connection here?
2203 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2205 SPP_FMT "Down event, taking interface down.\n",
2211 SPP_FMT "Down event (carrier loss)\n",
2213 sp->pp_flags &= ~PP_CALLIN;
2214 if (sp->state[IDX_LCP] != STATE_INITIAL)
2216 ifp->if_flags &= ~IFF_RUNNING;
2221 sppp_lcp_open(struct sppp *sp)
2224 * If we are authenticator, negotiate LCP_AUTH
2226 if (sp->hisauth.proto != 0)
2227 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2229 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2230 sp->pp_flags &= ~PP_NEEDAUTH;
2231 sppp_open_event(&lcp, sp);
2235 sppp_lcp_close(struct sppp *sp)
2237 sppp_close_event(&lcp, sp);
2241 sppp_lcp_TO(void *cookie)
2243 sppp_to_event(&lcp, (struct sppp *)cookie);
2247 * Analyze a configure request. Return true if it was agreeable, and
2248 * caused action sca, false if it has been rejected or nak'ed, and
2249 * caused action scn. (The return value is used to make the state
2250 * transition decision in the state automaton.)
2253 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2256 u_char *buf, *r, *p;
2263 buf = r = kmalloc (len, M_TEMP, M_INTWAIT);
2266 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2269 /* pass 1: check for things that need to be rejected */
2271 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2272 /* Sanity check option length */
2274 /* Malicious option - drop immediately.
2275 * XXX Maybe we should just RXJ it?
2277 log(-1, "%s: received malicious LCP option 0x%02x, "
2278 "length 0x%02x, (len: 0x%02x) dropping.\n", ifp->if_xname,
2283 log(-1, " %s ", sppp_lcp_opt_name(*p));
2287 if (len >= 6 && p[1] == 6)
2290 log(-1, "[invalid] ");
2292 case LCP_OPT_ASYNC_MAP:
2293 /* Async control character map. */
2294 if (len >= 6 && p[1] == 6)
2297 log(-1, "[invalid] ");
2300 /* Maximum receive unit. */
2301 if (len >= 4 && p[1] == 4)
2304 log(-1, "[invalid] ");
2306 case LCP_OPT_AUTH_PROTO:
2309 log(-1, "[invalid] ");
2312 authproto = (p[2] << 8) + p[3];
2313 if (authproto == PPP_CHAP && p[1] != 5) {
2315 log(-1, "[invalid chap len] ");
2318 if (sp->myauth.proto == 0) {
2319 /* we are not configured to do auth */
2321 log(-1, "[not configured] ");
2325 * Remote want us to authenticate, remember this,
2326 * so we stay in PHASE_AUTHENTICATE after LCP got
2329 sp->pp_flags |= PP_NEEDAUTH;
2332 /* Others not supported. */
2337 /* Add the option to rejected list. */
2344 log(-1, " send conf-rej\n");
2345 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2351 * pass 2: check for option values that are unacceptable and
2352 * thus require to be nak'ed.
2355 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2360 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2362 log(-1, " %s ", sppp_lcp_opt_name(*p));
2365 /* Magic number -- extract. */
2366 nmagic = (u_long)p[2] << 24 |
2367 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2368 if (nmagic != sp->lcp.magic) {
2371 log(-1, "0x%lx ", nmagic);
2374 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2375 log(-1, "[glitch] ");
2378 * We negate our magic here, and NAK it. If
2379 * we see it later in an NAK packet, we
2380 * suggest a new one.
2382 nmagic = ~sp->lcp.magic;
2384 p[2] = nmagic >> 24;
2385 p[3] = nmagic >> 16;
2390 case LCP_OPT_ASYNC_MAP:
2392 * Async control character map -- just ignore it.
2394 * Quote from RFC 1662, chapter 6:
2395 * To enable this functionality, synchronous PPP
2396 * implementations MUST always respond to the
2397 * Async-Control-Character-Map Configuration
2398 * Option with the LCP Configure-Ack. However,
2399 * acceptance of the Configuration Option does
2400 * not imply that the synchronous implementation
2401 * will do any ACCM mapping. Instead, all such
2402 * octet mapping will be performed by the
2403 * asynchronous-to-synchronous converter.
2409 * Maximum receive unit. Always agreeable,
2410 * but ignored by now.
2412 sp->lcp.their_mru = p[2] * 256 + p[3];
2414 log(-1, "%lu ", sp->lcp.their_mru);
2417 case LCP_OPT_AUTH_PROTO:
2418 authproto = (p[2] << 8) + p[3];
2419 if (sp->myauth.proto != authproto) {
2420 /* not agreed, nak */
2422 log(-1, "[mine %s != his %s] ",
2423 sppp_proto_name(sp->hisauth.proto),
2424 sppp_proto_name(authproto));
2425 p[2] = sp->myauth.proto >> 8;
2426 p[3] = sp->myauth.proto;
2429 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2431 log(-1, "[chap not MD5] ");
2437 /* Add the option to nak'ed list. */
2444 * Local and remote magics equal -- loopback?
2446 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2447 if (sp->pp_loopcnt == MAXALIVECNT*5)
2448 kprintf (SPP_FMT "loopback\n",
2450 if (ifp->if_flags & IFF_UP) {
2452 IF_DRAIN(&sp->pp_cpq);
2457 } else if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2459 log(-1, " max_failure (%d) exceeded, "
2461 sp->lcp.max_failure);
2462 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2465 log(-1, " send conf-nak\n");
2466 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2470 log(-1, " send conf-ack\n");
2471 sp->fail_counter[IDX_LCP] = 0;
2473 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2474 h->ident, origlen, h+1);
2477 kfree (buf, M_TEMP);
2486 * Analyze the LCP Configure-Reject option list, and adjust our
2490 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2496 buf = kmalloc (len, M_TEMP, M_INTWAIT);
2499 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2503 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2504 /* Sanity check option length */
2507 * Malicious option - drop immediately.
2508 * XXX Maybe we should just RXJ it?
2510 log(-1, "%s: received malicious LCP option, "
2511 "dropping.\n", ifp->if_xname);
2515 log(-1, " %s ", sppp_lcp_opt_name(*p));
2518 /* Magic number -- can't use it, use 0 */
2519 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2524 * Should not be rejected anyway, since we only
2525 * negotiate a MRU if explicitly requested by
2528 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2530 case LCP_OPT_AUTH_PROTO:
2532 * Peer doesn't want to authenticate himself,
2533 * deny unless this is a dialout call, and
2534 * AUTHFLAG_NOCALLOUT is set.
2536 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2537 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2539 log(-1, "[don't insist on auth "
2541 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2545 log(-1, "[access denied]\n");
2553 kfree (buf, M_TEMP);
2558 * Analyze the LCP Configure-NAK option list, and adjust our
2562 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2569 buf = kmalloc (len, M_TEMP, M_INTWAIT);
2572 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2576 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2577 /* Sanity check option length */
2580 * Malicious option - drop immediately.
2581 * XXX Maybe we should just RXJ it?
2583 log(-1, "%s: received malicious LCP option, "
2584 "dropping.\n", ifp->if_xname);
2588 log(-1, " %s ", sppp_lcp_opt_name(*p));
2591 /* Magic number -- renegotiate */
2592 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2593 len >= 6 && p[1] == 6) {
2594 magic = (u_long)p[2] << 24 |
2595 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2597 * If the remote magic is our negated one,
2598 * this looks like a loopback problem.
2599 * Suggest a new magic to make sure.
2601 if (magic == ~sp->lcp.magic) {
2603 log(-1, "magic glitch ");
2604 #if defined(__DragonFly__)
2605 sp->lcp.magic = krandom();
2607 sp->lcp.magic = time.tv_sec + time.tv_usec;
2610 sp->lcp.magic = magic;
2612 log(-1, "%lu ", magic);
2618 * Peer wants to advise us to negotiate an MRU.
2619 * Agree on it if it's reasonable, or use
2620 * default otherwise.
2622 if (len >= 4 && p[1] == 4) {
2623 u_int mru = p[2] * 256 + p[3];
2625 log(-1, "%d ", mru);
2626 if (mru < PP_MTU || mru > PP_MAX_MRU)
2629 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2632 case LCP_OPT_AUTH_PROTO:
2634 * Peer doesn't like our authentication method,
2638 log(-1, "[access denied]\n");
2646 kfree (buf, M_TEMP);
2651 sppp_lcp_tlu(struct sppp *sp)
2658 if (! (ifp->if_flags & IFF_UP) &&
2659 (ifp->if_flags & IFF_RUNNING)) {
2660 /* Coming out of loopback mode. */
2662 kprintf (SPP_FMT "up\n", SPP_ARGS(ifp));
2665 for (i = 0; i < IDX_COUNT; i++)
2666 if ((cps[i])->flags & CP_QUAL)
2669 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2670 (sp->pp_flags & PP_NEEDAUTH) != 0)
2671 sp->pp_phase = PHASE_AUTHENTICATE;
2673 sp->pp_phase = PHASE_NETWORK;
2676 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2677 sppp_phase_name(sp->pp_phase));
2680 * Open all authentication protocols. This is even required
2681 * if we already proceeded to network phase, since it might be
2682 * that remote wants us to authenticate, so we might have to
2683 * send a PAP request. Undesired authentication protocols
2684 * don't do anything when they get an Open event.
2686 for (i = 0; i < IDX_COUNT; i++)
2687 if ((cps[i])->flags & CP_AUTH)
2690 if (sp->pp_phase == PHASE_NETWORK) {
2691 /* Notify all NCPs. */
2692 for (i = 0; i < IDX_COUNT; i++)
2693 if (((cps[i])->flags & CP_NCP) &&
2696 * Hack to administratively disable IPv6 if
2697 * not desired. Perhaps we should have another
2698 * flag for this, but right now, we can make
2699 * all struct cp's read/only.
2701 (cps[i] != &ipv6cp ||
2702 (sp->confflags & CONF_ENABLE_IPV6)))
2706 /* Send Up events to all started protos. */
2707 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2708 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2711 /* notify low-level driver of state change */
2713 sp->pp_chg(sp, (int)sp->pp_phase);
2715 if (sp->pp_phase == PHASE_NETWORK)
2716 /* if no NCP is starting, close down */
2717 sppp_lcp_check_and_close(sp);
2721 sppp_lcp_tld(struct sppp *sp)
2727 sp->pp_phase = PHASE_TERMINATE;
2730 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2731 sppp_phase_name(sp->pp_phase));
2734 * Take upper layers down. We send the Down event first and
2735 * the Close second to prevent the upper layers from sending
2736 * ``a flurry of terminate-request packets'', as the RFC
2739 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2740 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2742 (cps[i])->Close(sp);
2747 sppp_lcp_tls(struct sppp *sp)
2751 sp->pp_phase = PHASE_ESTABLISH;
2754 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2755 sppp_phase_name(sp->pp_phase));
2757 /* Notify lower layer if desired. */
2765 sppp_lcp_tlf(struct sppp *sp)
2769 sp->pp_phase = PHASE_DEAD;
2771 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2772 sppp_phase_name(sp->pp_phase));
2774 /* Notify lower layer if desired. */
2782 sppp_lcp_scr(struct sppp *sp)
2784 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2788 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2789 if (! sp->lcp.magic)
2790 #if defined(__DragonFly__)
2791 sp->lcp.magic = krandom();
2793 sp->lcp.magic = time.tv_sec + time.tv_usec;
2795 opt[i++] = LCP_OPT_MAGIC;
2797 opt[i++] = sp->lcp.magic >> 24;
2798 opt[i++] = sp->lcp.magic >> 16;
2799 opt[i++] = sp->lcp.magic >> 8;
2800 opt[i++] = sp->lcp.magic;
2803 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2804 opt[i++] = LCP_OPT_MRU;
2806 opt[i++] = sp->lcp.mru >> 8;
2807 opt[i++] = sp->lcp.mru;
2810 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2811 authproto = sp->hisauth.proto;
2812 opt[i++] = LCP_OPT_AUTH_PROTO;
2813 opt[i++] = authproto == PPP_CHAP? 5: 4;
2814 opt[i++] = authproto >> 8;
2815 opt[i++] = authproto;
2816 if (authproto == PPP_CHAP)
2817 opt[i++] = CHAP_MD5;
2820 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2821 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2825 * Check the open NCPs, return true if at least one NCP is open.
2828 sppp_ncp_check(struct sppp *sp)
2832 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2833 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2839 * Re-check the open NCPs and see if we should terminate the link.
2840 * Called by the NCPs during their tlf action handling.
2843 sppp_lcp_check_and_close(struct sppp *sp)
2846 if (sp->pp_phase < PHASE_NETWORK)
2847 /* don't bother, we are already going down */
2850 if (sppp_ncp_check(sp))
2857 *--------------------------------------------------------------------------*
2859 * The IPCP implementation. *
2861 *--------------------------------------------------------------------------*
2865 sppp_ipcp_init(struct sppp *sp)
2869 sp->state[IDX_IPCP] = STATE_INITIAL;
2870 sp->fail_counter[IDX_IPCP] = 0;
2871 sp->pp_seq[IDX_IPCP] = 0;
2872 sp->pp_rseq[IDX_IPCP] = 0;
2873 #if defined(__DragonFly__)
2874 callout_init(&sp->timeout[IDX_IPCP]);
2879 sppp_ipcp_up(struct sppp *sp)
2881 sppp_up_event(&ipcp, sp);
2885 sppp_ipcp_down(struct sppp *sp)
2887 sppp_down_event(&ipcp, sp);
2891 sppp_ipcp_open(struct sppp *sp)
2894 u_long myaddr, hisaddr;
2896 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2897 IPCP_MYADDR_DYN | IPCP_VJ);
2900 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2902 * If we don't have his address, this probably means our
2903 * interface doesn't want to talk IP at all. (This could
2904 * be the case if somebody wants to speak only IPX, for
2905 * example.) Don't open IPCP in this case.
2907 if (hisaddr == 0L) {
2908 /* XXX this message should go away */
2910 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2916 * I don't have an assigned address, so i need to
2917 * negotiate my address.
2919 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2920 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2922 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2923 if (sp->confflags & CONF_ENABLE_VJ) {
2924 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2925 sp->ipcp.max_state = MAX_STATES - 1;
2926 sp->ipcp.compress_cid = 1;
2928 sppp_open_event(&ipcp, sp);
2932 sppp_ipcp_close(struct sppp *sp)
2934 sppp_close_event(&ipcp, sp);
2935 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2937 * My address was dynamic, clear it again.
2939 sppp_set_ip_addr(sp, 0L);
2943 sppp_ipcp_TO(void *cookie)
2945 sppp_to_event(&ipcp, (struct sppp *)cookie);
2949 * Analyze a configure request. Return true if it was agreeable, and
2950 * caused action sca, false if it has been rejected or nak'ed, and
2951 * caused action scn. (The return value is used to make the state
2952 * transition decision in the state automaton.)
2955 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2957 u_char *buf, *r, *p;
2958 struct ifnet *ifp = &sp->pp_if;
2959 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2960 u_long hisaddr, desiredaddr;
2967 * Make sure to allocate a buf that can at least hold a
2968 * conf-nak with an `address' option. We might need it below.
2970 buf = r = kmalloc ((len < 6? 6: len), M_TEMP, M_INTWAIT);
2972 /* pass 1: see if we can recognize them */
2974 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2977 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2978 /* Sanity check option length */
2980 /* XXX should we just RXJ? */
2981 log(-1, "%s: malicious IPCP option received, dropping\n",
2986 log(-1, " %s ", sppp_ipcp_opt_name(*p));
2988 case IPCP_OPT_COMPRESSION:
2989 if (!(sp->confflags & CONF_ENABLE_VJ)) {
2990 /* VJ compression administratively disabled */
2992 log(-1, "[locally disabled] ");
2996 * In theory, we should only conf-rej an
2997 * option that is shorter than RFC 1618
2998 * requires (i.e. < 4), and should conf-nak
2999 * anything else that is not VJ. However,
3000 * since our algorithm always uses the
3001 * original option to NAK it with new values,
3002 * things would become more complicated. In
3003 * pratice, the only commonly implemented IP
3004 * compression option is VJ anyway, so the
3005 * difference is negligible.
3007 if (len >= 6 && p[1] == 6) {
3009 * correctly formed compression option
3010 * that could be VJ compression
3015 log(-1, "optlen %d [invalid/unsupported] ",
3018 case IPCP_OPT_ADDRESS:
3019 if (len >= 6 && p[1] == 6) {
3020 /* correctly formed address option */
3024 log(-1, "[invalid] ");
3027 /* Others not supported. */
3032 /* Add the option to rejected list. */
3039 log(-1, " send conf-rej\n");
3040 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
3045 /* pass 2: parse option values */
3046 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
3048 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
3052 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3054 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3056 case IPCP_OPT_COMPRESSION:
3057 desiredcomp = p[2] << 8 | p[3];
3058 /* We only support VJ */
3059 if (desiredcomp == IPCP_COMP_VJ) {
3061 log(-1, "VJ [ack] ");
3062 sp->ipcp.flags |= IPCP_VJ;
3063 sl_compress_init(sp->pp_comp, p[4]);
3064 sp->ipcp.max_state = p[4];
3065 sp->ipcp.compress_cid = p[5];
3069 log(-1, "compproto %#04x [not supported] ",
3071 p[2] = IPCP_COMP_VJ >> 8;
3072 p[3] = IPCP_COMP_VJ;
3073 p[4] = sp->ipcp.max_state;
3074 p[5] = sp->ipcp.compress_cid;
3076 case IPCP_OPT_ADDRESS:
3077 /* This is the address he wants in his end */
3078 desiredaddr = p[2] << 24 | p[3] << 16 |
3080 if (desiredaddr == hisaddr ||
3081 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3083 * Peer's address is same as our value,
3084 * or we have set it to 0.0.0.* to
3085 * indicate that we do not really care,
3086 * this is agreeable. Gonna conf-ack
3090 log(-1, "%s [ack] ",
3091 sppp_dotted_quad(hisaddr));
3092 /* record that we've seen it already */
3093 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3097 * The address wasn't agreeable. This is either
3098 * he sent us 0.0.0.0, asking to assign him an
3099 * address, or he send us another address not
3100 * matching our value. Either case, we gonna
3101 * conf-nak it with our value.
3102 * XXX: we should "rej" if hisaddr == 0
3105 if (desiredaddr == 0)
3106 log(-1, "[addr requested] ");
3108 log(-1, "%s [not agreed] ",
3109 sppp_dotted_quad(desiredaddr));
3112 p[2] = hisaddr >> 24;
3113 p[3] = hisaddr >> 16;
3114 p[4] = hisaddr >> 8;
3118 /* Add the option to nak'ed list. */
3125 * If we are about to conf-ack the request, but haven't seen
3126 * his address so far, gonna conf-nak it instead, with the
3127 * `address' option present and our idea of his address being
3128 * filled in there, to request negotiation of both addresses.
3130 * XXX This can result in an endless req - nak loop if peer
3131 * doesn't want to send us his address. Q: What should we do
3132 * about it? XXX A: implement the max-failure counter.
3134 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3135 buf[0] = IPCP_OPT_ADDRESS;
3137 buf[2] = hisaddr >> 24;
3138 buf[3] = hisaddr >> 16;
3139 buf[4] = hisaddr >> 8;
3143 log(-1, "still need hisaddr ");
3148 log(-1, " send conf-nak\n");
3149 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3152 log(-1, " send conf-ack\n");
3153 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3154 h->ident, origlen, h+1);
3157 kfree (buf, M_TEMP);
3166 * Analyze the IPCP Configure-Reject option list, and adjust our
3170 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3173 struct ifnet *ifp = &sp->pp_if;
3174 int debug = ifp->if_flags & IFF_DEBUG;
3177 buf = kmalloc (len, M_TEMP, M_INTWAIT);
3180 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3184 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3185 /* Sanity check option length */
3187 /* XXX should we just RXJ? */
3188 log(-1, "%s: malicious IPCP option received, dropping\n",
3193 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3195 case IPCP_OPT_COMPRESSION:
3196 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3198 case IPCP_OPT_ADDRESS:
3200 * Peer doesn't grok address option. This is
3201 * bad. XXX Should we better give up here?
3202 * XXX We could try old "addresses" option...
3204 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3211 kfree (buf, M_TEMP);
3216 * Analyze the IPCP Configure-NAK option list, and adjust our
3220 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3223 struct ifnet *ifp = &sp->pp_if;
3224 int debug = ifp->if_flags & IFF_DEBUG;
3229 buf = kmalloc (len, M_TEMP, M_INTWAIT);
3232 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3236 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3237 /* Sanity check option length */
3239 /* XXX should we just RXJ? */
3240 log(-1, "%s: malicious IPCP option received, dropping\n",
3245 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3247 case IPCP_OPT_COMPRESSION:
3248 if (len >= 6 && p[1] == 6) {
3249 desiredcomp = p[2] << 8 | p[3];
3251 log(-1, "[wantcomp %#04x] ",
3253 if (desiredcomp == IPCP_COMP_VJ) {
3254 sl_compress_init(sp->pp_comp, p[4]);
3255 sp->ipcp.max_state = p[4];
3256 sp->ipcp.compress_cid = p[5];
3258 log(-1, "[agree] ");
3261 ~(1 << IPCP_OPT_COMPRESSION);
3264 case IPCP_OPT_ADDRESS:
3266 * Peer doesn't like our local IP address. See
3267 * if we can do something for him. We'll drop
3268 * him our address then.
3270 if (len >= 6 && p[1] == 6) {
3271 wantaddr = p[2] << 24 | p[3] << 16 |
3273 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3275 log(-1, "[wantaddr %s] ",
3276 sppp_dotted_quad(wantaddr));
3278 * When doing dynamic address assignment,
3279 * we accept his offer. Otherwise, we
3280 * ignore it and thus continue to negotiate
3281 * our already existing value.
3282 * XXX: Bogus, if he said no once, he'll
3283 * just say no again, might as well die.
3285 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3286 sppp_set_ip_addr(sp, wantaddr);
3288 log(-1, "[agree] ");
3289 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3297 kfree (buf, M_TEMP);
3302 sppp_ipcp_tlu(struct sppp *sp)
3304 /* we are up - notify isdn daemon */
3310 sppp_ipcp_tld(struct sppp *sp)
3315 sppp_ipcp_tls(struct sppp *sp)
3317 /* indicate to LCP that it must stay alive */
3318 sp->lcp.protos |= (1 << IDX_IPCP);
3322 sppp_ipcp_tlf(struct sppp *sp)
3324 /* we no longer need LCP */
3325 sp->lcp.protos &= ~(1 << IDX_IPCP);
3326 sppp_lcp_check_and_close(sp);
3330 sppp_ipcp_scr(struct sppp *sp)
3332 char opt[6 /* compression */ + 6 /* address */];
3336 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3337 opt[i++] = IPCP_OPT_COMPRESSION;
3339 opt[i++] = IPCP_COMP_VJ >> 8;
3340 opt[i++] = IPCP_COMP_VJ;
3341 opt[i++] = sp->ipcp.max_state;
3342 opt[i++] = sp->ipcp.compress_cid;
3344 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3345 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3346 opt[i++] = IPCP_OPT_ADDRESS;
3348 opt[i++] = ouraddr >> 24;
3349 opt[i++] = ouraddr >> 16;
3350 opt[i++] = ouraddr >> 8;
3354 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3355 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3359 *--------------------------------------------------------------------------*
3361 * The IPv6CP implementation. *
3363 *--------------------------------------------------------------------------*
3368 sppp_ipv6cp_init(struct sppp *sp)
3370 sp->ipv6cp.opts = 0;
3371 sp->ipv6cp.flags = 0;
3372 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3373 sp->fail_counter[IDX_IPV6CP] = 0;
3374 sp->pp_seq[IDX_IPV6CP] = 0;
3375 sp->pp_rseq[IDX_IPV6CP] = 0;
3376 #if defined(__NetBSD__)
3377 callout_init(&sp->ch[IDX_IPV6CP]);
3379 #if defined(__DragonFly__)
3380 callout_init(&sp->timeout[IDX_IPV6CP]);
3385 sppp_ipv6cp_up(struct sppp *sp)
3387 sppp_up_event(&ipv6cp, sp);
3391 sppp_ipv6cp_down(struct sppp *sp)
3393 sppp_down_event(&ipv6cp, sp);
3397 sppp_ipv6cp_open(struct sppp *sp)
3400 struct in6_addr myaddr, hisaddr;
3402 #ifdef IPV6CP_MYIFID_DYN
3403 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3405 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3408 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3410 * If we don't have our address, this probably means our
3411 * interface doesn't want to talk IPv6 at all. (This could
3412 * be the case if somebody wants to speak only IPX, for
3413 * example.) Don't open IPv6CP in this case.
3415 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3416 /* XXX this message should go away */
3418 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3423 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3424 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3425 sppp_open_event(&ipv6cp, sp);
3429 sppp_ipv6cp_close(struct sppp *sp)
3431 sppp_close_event(&ipv6cp, sp);
3435 sppp_ipv6cp_TO(void *cookie)
3437 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3441 * Analyze a configure request. Return true if it was agreeable, and
3442 * caused action sca, false if it has been rejected or nak'ed, and
3443 * caused action scn. (The return value is used to make the state
3444 * transition decision in the state automaton.)
3447 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3449 u_char *buf, *r, *p;
3450 struct ifnet *ifp = &sp->pp_if;
3451 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3452 struct in6_addr myaddr, desiredaddr, suggestaddr;
3455 int collision, nohisaddr;
3460 * Make sure to allocate a buf that can at least hold a
3461 * conf-nak with an `address' option. We might need it below.
3463 buf = r = kmalloc ((len < 6? 6: len), M_TEMP, M_INTWAIT);
3465 /* pass 1: see if we can recognize them */
3467 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3471 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3472 /* Sanity check option length */
3475 log(-1, "%s: received malicious IPCPv6 option, "
3476 "dropping\n", ifp->if_xname);
3480 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3482 case IPV6CP_OPT_IFID:
3483 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3484 /* correctly formed address option */
3489 log(-1, " [invalid]");
3492 case IPV6CP_OPT_COMPRESSION:
3493 if (len >= 4 && p[1] >= 4) {
3494 /* correctly formed compress option */
3498 log(-1, " [invalid]");
3502 /* Others not supported. */
3507 /* Add the option to rejected list. */
3514 log(-1, " send conf-rej\n");
3515 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3520 /* pass 2: parse option values */
3521 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3523 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3528 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3530 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3533 case IPV6CP_OPT_COMPRESSION:
3536 case IPV6CP_OPT_IFID:
3537 bzero(&desiredaddr, sizeof(desiredaddr));
3538 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3539 collision = (bcmp(&desiredaddr.s6_addr[8],
3540 &myaddr.s6_addr[8], 8) == 0);
3541 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3543 desiredaddr.s6_addr16[0] = htons(0xfe80);
3544 desiredaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3546 if (!collision && !nohisaddr) {
3547 /* no collision, hisaddr known - Conf-Ack */
3552 ip6_sprintf(&desiredaddr),
3553 sppp_cp_type_name(type));
3558 bzero(&suggestaddr, sizeof(suggestaddr));
3559 if (collision && nohisaddr) {
3560 /* collision, hisaddr unknown - Conf-Rej */
3565 * - no collision, hisaddr unknown, or
3566 * - collision, hisaddr known
3567 * Conf-Nak, suggest hisaddr
3570 sppp_suggest_ip6_addr(sp, &suggestaddr);
3571 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3574 log(-1, " %s [%s]", ip6_sprintf(&desiredaddr),
3575 sppp_cp_type_name(type));
3578 /* Add the option to nak'ed list. */
3584 if (rlen == 0 && type == CONF_ACK) {
3586 log(-1, " send %s\n", sppp_cp_type_name(type));
3587 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3590 if (type == CONF_ACK)
3591 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3595 log(-1, " send %s suggest %s\n",
3596 sppp_cp_type_name(type), ip6_sprintf(&suggestaddr));
3598 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3602 kfree (buf, M_TEMP);
3611 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3615 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3618 struct ifnet *ifp = &sp->pp_if;
3619 int debug = ifp->if_flags & IFF_DEBUG;
3622 buf = kmalloc (len, M_TEMP, M_INTWAIT);
3625 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3629 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3632 log(-1, "%s: received malicious IPCPv6 option, "
3633 "dropping\n", ifp->if_xname);
3637 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3639 case IPV6CP_OPT_IFID:
3641 * Peer doesn't grok address option. This is
3642 * bad. XXX Should we better give up here?
3644 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3647 case IPV6CP_OPT_COMPRESS:
3648 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3656 kfree (buf, M_TEMP);
3661 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3665 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3668 struct ifnet *ifp = &sp->pp_if;
3669 int debug = ifp->if_flags & IFF_DEBUG;
3670 struct in6_addr suggestaddr;
3673 buf = kmalloc (len, M_TEMP, M_INTWAIT);
3676 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3680 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3683 log(-1, "%s: received malicious IPCPv6 option, "
3684 "dropping\n", ifp->if_xname);
3688 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3690 case IPV6CP_OPT_IFID:
3692 * Peer doesn't like our local ifid. See
3693 * if we can do something for him. We'll drop
3694 * him our address then.
3696 if (len < 10 || p[1] != 10)
3698 bzero(&suggestaddr, sizeof(suggestaddr));
3699 suggestaddr.s6_addr16[0] = htons(0xfe80);
3700 suggestaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3701 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3703 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3705 log(-1, " [suggestaddr %s]",
3706 ip6_sprintf(&suggestaddr));
3707 #ifdef IPV6CP_MYIFID_DYN
3709 * When doing dynamic address assignment,
3710 * we accept his offer.
3712 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3713 struct in6_addr lastsuggest;
3715 * If <suggested myaddr from peer> equals to
3716 * <hisaddr we have suggested last time>,
3717 * we have a collision. generate new random
3720 sppp_suggest_ip6_addr(&lastsuggest);
3721 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3724 log(-1, " [random]");
3725 sppp_gen_ip6_addr(sp, &suggestaddr);
3727 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3729 log(-1, " [agree]");
3730 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3734 * Since we do not do dynamic address assignment,
3735 * we ignore it and thus continue to negotiate
3736 * our already existing value. This can possibly
3737 * go into infinite request-reject loop.
3739 * This is not likely because we normally use
3740 * ifid based on MAC-address.
3741 * If you have no ethernet card on the node, too bad.
3742 * XXX should we use fail_counter?
3747 case IPV6CP_OPT_COMPRESS:
3749 * Peer wants different compression parameters.
3758 kfree (buf, M_TEMP);
3762 sppp_ipv6cp_tlu(struct sppp *sp)
3764 /* we are up - notify isdn daemon */
3770 sppp_ipv6cp_tld(struct sppp *sp)
3775 sppp_ipv6cp_tls(struct sppp *sp)
3777 /* indicate to LCP that it must stay alive */
3778 sp->lcp.protos |= (1 << IDX_IPV6CP);
3782 sppp_ipv6cp_tlf(struct sppp *sp)
3785 #if 0 /* need #if 0 to close IPv6CP properly */
3786 /* we no longer need LCP */
3787 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3788 sppp_lcp_check_and_close(sp);
3793 sppp_ipv6cp_scr(struct sppp *sp)
3795 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3796 struct in6_addr ouraddr;
3799 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3800 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3801 opt[i++] = IPV6CP_OPT_IFID;
3803 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3808 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3809 opt[i++] = IPV6CP_OPT_COMPRESSION;
3811 opt[i++] = 0; /* TBD */
3812 opt[i++] = 0; /* TBD */
3813 /* variable length data may follow */
3817 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3818 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3822 sppp_ipv6cp_init(struct sppp *sp)
3827 sppp_ipv6cp_up(struct sppp *sp)
3832 sppp_ipv6cp_down(struct sppp *sp)
3838 sppp_ipv6cp_open(struct sppp *sp)
3843 sppp_ipv6cp_close(struct sppp *sp)
3848 sppp_ipv6cp_TO(void *sp)
3853 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3859 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3864 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3869 sppp_ipv6cp_tlu(struct sppp *sp)
3874 sppp_ipv6cp_tld(struct sppp *sp)
3879 sppp_ipv6cp_tls(struct sppp *sp)
3884 sppp_ipv6cp_tlf(struct sppp *sp)
3889 sppp_ipv6cp_scr(struct sppp *sp)
3895 *--------------------------------------------------------------------------*
3897 * The CHAP implementation. *
3899 *--------------------------------------------------------------------------*
3903 * The authentication protocols don't employ a full-fledged state machine as
3904 * the control protocols do, since they do have Open and Close events, but
3905 * not Up and Down, nor are they explicitly terminated. Also, use of the
3906 * authentication protocols may be different in both directions (this makes
3907 * sense, think of a machine that never accepts incoming calls but only
3908 * calls out, it doesn't require the called party to authenticate itself).
3910 * Our state machine for the local authentication protocol (we are requesting
3911 * the peer to authenticate) looks like:
3914 * +--------------------------------------------+
3916 * +--------+ Close +---------+ RCA+
3917 * | |<----------------------------------| |------+
3918 * +--->| Closed | TO* | Opened | sca |
3919 * | | |-----+ +-------| |<-----+
3920 * | +--------+ irc | | +---------+
3926 * | | +------->+ | |
3928 * | +--------+ V | |
3929 * | | |<----+<--------------------+ |
3935 * +------+ +------------------------------------------+
3936 * scn,tld sca,irc,ict,tlu
3941 * Open: LCP reached authentication phase
3942 * Close: LCP reached terminate phase
3944 * RCA+: received reply (pap-req, chap-response), acceptable
3945 * RCN: received reply (pap-req, chap-response), not acceptable
3946 * TO+: timeout with restart counter >= 0
3947 * TO-: timeout with restart counter < 0
3948 * TO*: reschedule timeout for CHAP
3950 * scr: send request packet (none for PAP, chap-challenge)
3951 * sca: send ack packet (pap-ack, chap-success)
3952 * scn: send nak packet (pap-nak, chap-failure)
3953 * ict: initialize re-challenge timer (CHAP only)
3955 * tlu: this-layer-up, LCP reaches network phase
3956 * tld: this-layer-down, LCP enters terminate phase
3958 * Note that in CHAP mode, after sending a new challenge, while the state
3959 * automaton falls back into Req-Sent state, it doesn't signal a tld
3960 * event to LCP, so LCP remains in network phase. Only after not getting
3961 * any response (or after getting an unacceptable response), CHAP closes,
3962 * causing LCP to enter terminate phase.
3964 * With PAP, there is no initial request that can be sent. The peer is
3965 * expected to send one based on the successful negotiation of PAP as
3966 * the authentication protocol during the LCP option negotiation.
3968 * Incoming authentication protocol requests (remote requests
3969 * authentication, we are peer) don't employ a state machine at all,
3970 * they are simply answered. Some peers [Ascend P50 firmware rev
3971 * 4.50] react allergically when sending IPCP requests while they are
3972 * still in authentication phase (thereby violating the standard that
3973 * demands that these NCP packets are to be discarded), so we keep
3974 * track of the peer demanding us to authenticate, and only proceed to
3975 * phase network once we've seen a positive acknowledge for the
3980 * Handle incoming CHAP packets.
3983 sppp_chap_input(struct sppp *sp, struct mbuf *m)
3986 struct lcp_header *h;
3988 u_char *value, *name, digest[AUTHKEYLEN], dsize;
3989 int value_len, name_len;
3992 len = m->m_pkthdr.len;
3996 SPP_FMT "chap invalid packet length: %d bytes\n",
3997 SPP_ARGS(ifp), len);
4000 h = mtod (m, struct lcp_header*);
4001 if (len > ntohs (h->len))
4002 len = ntohs (h->len);
4005 /* challenge, failure and success are his authproto */
4006 case CHAP_CHALLENGE:
4007 value = 1 + (u_char*)(h+1);
4008 value_len = value[-1];
4009 name = value + value_len;
4010 name_len = len - value_len - 5;
4014 SPP_FMT "chap corrupted challenge "
4015 "<%s id=0x%x len=%d",
4017 sppp_auth_type_name(PPP_CHAP, h->type),
4018 h->ident, ntohs(h->len));
4019 sppp_print_bytes((u_char*) (h+1), len-4);
4027 SPP_FMT "chap input <%s id=0x%x len=%d name=",
4029 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
4031 sppp_print_string((char*) name, name_len);
4032 log(-1, " value-size=%d value=", value_len);
4033 sppp_print_bytes(value, value_len);
4037 /* Compute reply value. */
4039 MD5Update(&ctx, &h->ident, 1);
4040 MD5Update(&ctx, sp->myauth.secret,
4041 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
4042 MD5Update(&ctx, value, value_len);
4043 MD5Final(digest, &ctx);
4044 dsize = sizeof digest;
4046 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
4047 sizeof dsize, (const char *)&dsize,
4048 sizeof digest, digest,
4049 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4056 log(LOG_DEBUG, SPP_FMT "chap success",
4060 sppp_print_string((char*)(h + 1), len - 4);
4067 sp->pp_flags &= ~PP_NEEDAUTH;
4068 if (sp->myauth.proto == PPP_CHAP &&
4069 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4070 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
4072 * We are authenticator for CHAP but didn't
4073 * complete yet. Leave it to tlu to proceed
4080 sppp_phase_network(sp);
4085 log(LOG_INFO, SPP_FMT "chap failure",
4089 sppp_print_string((char*)(h + 1), len - 4);
4093 log(LOG_INFO, SPP_FMT "chap failure\n",
4095 /* await LCP shutdown by authenticator */
4098 /* response is my authproto */
4100 value = 1 + (u_char*)(h+1);
4101 value_len = value[-1];
4102 name = value + value_len;
4103 name_len = len - value_len - 5;
4107 SPP_FMT "chap corrupted response "
4108 "<%s id=0x%x len=%d",
4110 sppp_auth_type_name(PPP_CHAP, h->type),
4111 h->ident, ntohs(h->len));
4112 sppp_print_bytes((u_char*)(h+1), len-4);
4117 if (h->ident != sp->confid[IDX_CHAP]) {
4120 SPP_FMT "chap dropping response for old ID "
4121 "(got %d, expected %d)\n",
4123 h->ident, sp->confid[IDX_CHAP]);
4126 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4127 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4128 log(LOG_INFO, SPP_FMT "chap response, his name ",
4130 sppp_print_string(name, name_len);
4131 log(-1, " != expected ");
4132 sppp_print_string(sp->hisauth.name,
4133 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4137 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4138 "<%s id=0x%x len=%d name=",
4140 sppp_state_name(sp->state[IDX_CHAP]),
4141 sppp_auth_type_name(PPP_CHAP, h->type),
4142 h->ident, ntohs (h->len));
4143 sppp_print_string((char*)name, name_len);
4144 log(-1, " value-size=%d value=", value_len);
4145 sppp_print_bytes(value, value_len);
4148 if (value_len != AUTHKEYLEN) {
4151 SPP_FMT "chap bad hash value length: "
4152 "%d bytes, should be %d\n",
4153 SPP_ARGS(ifp), value_len,
4159 MD5Update(&ctx, &h->ident, 1);
4160 MD5Update(&ctx, sp->hisauth.secret,
4161 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4162 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4163 MD5Final(digest, &ctx);
4165 #define FAILMSG "Failed..."
4166 #define SUCCMSG "Welcome!"
4168 if (value_len != sizeof digest ||
4169 bcmp(digest, value, value_len) != 0) {
4170 /* action scn, tld */
4171 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4172 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4177 /* action sca, perhaps tlu */
4178 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4179 sp->state[IDX_CHAP] == STATE_OPENED)
4180 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4181 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4183 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4184 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4190 /* Unknown CHAP packet type -- ignore. */
4192 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4193 "<0x%x id=0x%xh len=%d",
4195 sppp_state_name(sp->state[IDX_CHAP]),
4196 h->type, h->ident, ntohs(h->len));
4197 sppp_print_bytes((u_char*)(h+1), len-4);
4206 sppp_chap_init(struct sppp *sp)
4208 /* Chap doesn't have STATE_INITIAL at all. */
4209 sp->state[IDX_CHAP] = STATE_CLOSED;
4210 sp->fail_counter[IDX_CHAP] = 0;
4211 sp->pp_seq[IDX_CHAP] = 0;
4212 sp->pp_rseq[IDX_CHAP] = 0;
4213 #if defined(__DragonFly__)
4214 callout_init(&sp->timeout[IDX_CHAP]);
4219 sppp_chap_open(struct sppp *sp)
4221 if (sp->myauth.proto == PPP_CHAP &&
4222 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4223 /* we are authenticator for CHAP, start it */
4225 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4226 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4228 /* nothing to be done if we are peer, await a challenge */
4232 sppp_chap_close(struct sppp *sp)
4234 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4235 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4239 sppp_chap_TO(void *cookie)
4241 struct sppp *sp = (struct sppp *)cookie;
4247 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4249 sppp_state_name(sp->state[IDX_CHAP]),
4250 sp->rst_counter[IDX_CHAP]);
4252 if (--sp->rst_counter[IDX_CHAP] < 0)
4254 switch (sp->state[IDX_CHAP]) {
4255 case STATE_REQ_SENT:
4257 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4261 /* TO+ (or TO*) event */
4262 switch (sp->state[IDX_CHAP]) {
4265 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4267 case STATE_REQ_SENT:
4269 /* sppp_cp_change_state() will restart the timer */
4270 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4278 sppp_chap_tlu(struct sppp *sp)
4284 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4287 * Some broken CHAP implementations (Conware CoNet, firmware
4288 * 4.0.?) don't want to re-authenticate their CHAP once the
4289 * initial challenge-response exchange has taken place.
4290 * Provide for an option to avoid rechallenges.
4292 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4294 * Compute the re-challenge timeout. This will yield
4295 * a number between 300 and 810 seconds.
4297 i = 300 + ((unsigned)(krandom() & 0xff00) >> 7);
4298 callout_reset(&sp->timeout[IDX_CHAP], i * hz, chap.TO, sp);
4303 SPP_FMT "chap %s, ",
4305 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4306 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4307 log(-1, "next re-challenge in %d seconds\n", i);
4309 log(-1, "re-challenging suppressed\n");
4314 /* indicate to LCP that we need to be closed down */
4315 sp->lcp.protos |= (1 << IDX_CHAP);
4317 if (sp->pp_flags & PP_NEEDAUTH) {
4319 * Remote is authenticator, but his auth proto didn't
4320 * complete yet. Defer the transition to network
4330 * If we are already in phase network, we are done here. This
4331 * is the case if this is a dummy tlu event after a re-challenge.
4333 if (sp->pp_phase != PHASE_NETWORK)
4334 sppp_phase_network(sp);
4338 sppp_chap_tld(struct sppp *sp)
4343 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4344 callout_stop(&sp->timeout[IDX_CHAP]);
4345 sp->lcp.protos &= ~(1 << IDX_CHAP);
4351 sppp_chap_scr(struct sppp *sp)
4356 /* Compute random challenge. */
4357 ch = (u_long *)sp->myauth.challenge;
4358 #if defined(__DragonFly__)
4359 read_random(&seed, sizeof seed);
4364 seed = tv.tv_sec ^ tv.tv_usec;
4367 ch[0] = seed ^ krandom();
4368 ch[1] = seed ^ krandom();
4369 ch[2] = seed ^ krandom();
4370 ch[3] = seed ^ krandom();
4373 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4375 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4376 sizeof clen, (const char *)&clen,
4377 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4378 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4384 *--------------------------------------------------------------------------*
4386 * The PAP implementation. *
4388 *--------------------------------------------------------------------------*
4391 * For PAP, we need to keep a little state also if we are the peer, not the
4392 * authenticator. This is since we don't get a request to authenticate, but
4393 * have to repeatedly authenticate ourself until we got a response (or the
4394 * retry counter is expired).
4398 * Handle incoming PAP packets. */
4400 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4403 struct lcp_header *h;
4405 u_char *name, *passwd, mlen;
4406 int name_len, passwd_len;
4409 * Malicious input might leave this uninitialized, so
4410 * init to an impossible value.
4414 len = m->m_pkthdr.len;
4418 SPP_FMT "pap invalid packet length: %d bytes\n",
4419 SPP_ARGS(ifp), len);
4422 h = mtod (m, struct lcp_header*);
4423 if (len > ntohs (h->len))
4424 len = ntohs (h->len);
4426 /* PAP request is my authproto */
4428 name = 1 + (u_char*)(h+1);
4429 name_len = name[-1];
4430 passwd = name + name_len + 1;
4431 if (name_len > len - 6 ||
4432 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4434 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4435 "<%s id=0x%x len=%d",
4437 sppp_auth_type_name(PPP_PAP, h->type),
4438 h->ident, ntohs(h->len));
4439 sppp_print_bytes((u_char*)(h+1), len-4);
4445 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4446 "<%s id=0x%x len=%d name=",
4448 sppp_state_name(sp->state[IDX_PAP]),
4449 sppp_auth_type_name(PPP_PAP, h->type),
4450 h->ident, ntohs(h->len));
4451 sppp_print_string((char*)name, name_len);
4452 log(-1, " passwd=");
4453 sppp_print_string((char*)passwd, passwd_len);
4456 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4457 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4458 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4459 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4460 /* action scn, tld */
4461 mlen = sizeof(FAILMSG) - 1;
4462 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4463 sizeof mlen, (const char *)&mlen,
4464 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4469 /* action sca, perhaps tlu */
4470 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4471 sp->state[IDX_PAP] == STATE_OPENED) {
4472 mlen = sizeof(SUCCMSG) - 1;
4473 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4474 sizeof mlen, (const char *)&mlen,
4475 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4478 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4479 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4484 /* ack and nak are his authproto */
4486 callout_stop(&sp->pap_my_to);
4488 log(LOG_DEBUG, SPP_FMT "pap success",
4490 name = 1 + (u_char *)(h + 1);
4491 name_len = name[-1];
4492 if (len > 5 && name_len < len+4) {
4494 sppp_print_string(name, name_len);
4501 sp->pp_flags &= ~PP_NEEDAUTH;
4502 if (sp->myauth.proto == PPP_PAP &&
4503 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4504 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4506 * We are authenticator for PAP but didn't
4507 * complete yet. Leave it to tlu to proceed
4518 sppp_phase_network(sp);
4522 callout_stop(&sp->pap_my_to);
4524 log(LOG_INFO, SPP_FMT "pap failure",
4526 name = 1 + (u_char *)(h + 1);
4527 name_len = name[-1];
4528 if (len > 5 && name_len < len+4) {
4530 sppp_print_string(name, name_len);
4534 log(LOG_INFO, SPP_FMT "pap failure\n",
4536 /* await LCP shutdown by authenticator */
4540 /* Unknown PAP packet type -- ignore. */
4542 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4543 "<0x%x id=0x%x len=%d",
4545 h->type, h->ident, ntohs(h->len));
4546 sppp_print_bytes((u_char*)(h+1), len-4);
4555 sppp_pap_init(struct sppp *sp)
4557 /* PAP doesn't have STATE_INITIAL at all. */
4558 sp->state[IDX_PAP] = STATE_CLOSED;
4559 sp->fail_counter[IDX_PAP] = 0;
4560 sp->pp_seq[IDX_PAP] = 0;
4561 sp->pp_rseq[IDX_PAP] = 0;
4562 #if defined(__DragonFly__)
4563 callout_init(&sp->timeout[IDX_PAP]);
4564 callout_init(&sp->pap_my_to);
4569 sppp_pap_open(struct sppp *sp)
4571 if (sp->hisauth.proto == PPP_PAP &&
4572 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4573 /* we are authenticator for PAP, start our timer */
4574 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4575 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4577 if (sp->myauth.proto == PPP_PAP) {
4578 /* we are peer, send a request, and start a timer */
4580 callout_reset(&sp->pap_my_to, sp->lcp.timeout,
4581 sppp_pap_my_TO, sp);
4586 sppp_pap_close(struct sppp *sp)
4588 if (sp->state[IDX_PAP] != STATE_CLOSED)
4589 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4593 * That's the timeout routine if we are authenticator. Since the
4594 * authenticator is basically passive in PAP, we can't do much here.
4597 sppp_pap_TO(void *cookie)
4599 struct sppp *sp = (struct sppp *)cookie;
4605 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4607 sppp_state_name(sp->state[IDX_PAP]),
4608 sp->rst_counter[IDX_PAP]);
4610 if (--sp->rst_counter[IDX_PAP] < 0)
4612 switch (sp->state[IDX_PAP]) {
4613 case STATE_REQ_SENT:
4615 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4619 /* TO+ event, not very much we could do */
4620 switch (sp->state[IDX_PAP]) {
4621 case STATE_REQ_SENT:
4622 /* sppp_cp_change_state() will restart the timer */
4623 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4631 * That's the timeout handler if we are peer. Since the peer is active,
4632 * we need to retransmit our PAP request since it is apparently lost.
4633 * XXX We should impose a max counter.
4636 sppp_pap_my_TO(void *cookie)
4638 struct sppp *sp = (struct sppp *)cookie;
4642 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4649 sppp_pap_tlu(struct sppp *sp)
4653 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4656 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4657 SPP_ARGS(ifp), pap.name);
4661 /* indicate to LCP that we need to be closed down */
4662 sp->lcp.protos |= (1 << IDX_PAP);
4664 if (sp->pp_flags & PP_NEEDAUTH) {
4666 * Remote is authenticator, but his auth proto didn't
4667 * complete yet. Defer the transition to network
4674 sppp_phase_network(sp);
4678 sppp_pap_tld(struct sppp *sp)
4683 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4684 callout_stop(&sp->timeout[IDX_PAP]);
4685 callout_stop(&sp->pap_my_to);
4686 sp->lcp.protos &= ~(1 << IDX_PAP);
4692 sppp_pap_scr(struct sppp *sp)
4694 u_char idlen, pwdlen;
4696 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4697 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4698 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4700 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4701 sizeof idlen, (const char *)&idlen,
4702 (size_t)idlen, sp->myauth.name,
4703 sizeof pwdlen, (const char *)&pwdlen,
4704 (size_t)pwdlen, sp->myauth.secret,
4709 * Random miscellaneous functions.
4713 * Send a PAP or CHAP proto packet.
4715 * Varadic function, each of the elements for the ellipsis is of type
4716 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4718 * NOTE: never declare variadic functions with types subject to type
4719 * promotion (i.e. u_char). This is asking for big trouble depending
4720 * on the architecture you are on...
4724 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4725 unsigned int type, unsigned int id,
4729 struct ppp_header *h;
4730 struct lcp_header *lh;
4736 struct ifaltq_subque *ifsq;
4739 MGETHDR (m, MB_DONTWAIT, MT_DATA);
4742 m->m_pkthdr.rcvif = 0;
4744 h = mtod (m, struct ppp_header*);
4745 h->address = PPP_ALLSTATIONS; /* broadcast address */
4746 h->control = PPP_UI; /* Unnumbered Info */
4747 h->protocol = htons(cp->proto);
4749 lh = (struct lcp_header*)(h + 1);
4752 p = (u_char*) (lh+1);
4757 while ((mlen = (unsigned int)__va_arg(ap, size_t)) != 0) {
4758 msg = __va_arg(ap, const char *);
4760 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4766 bcopy(msg, p, mlen);
4771 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4772 lh->len = htons (LCP_HEADER_LEN + len);
4775 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4776 SPP_ARGS(ifp), cp->name,
4777 sppp_auth_type_name(cp->proto, lh->type),
4778 lh->ident, ntohs(lh->len));
4779 sppp_print_bytes((u_char*) (lh+1), len);
4782 if (IF_QFULL (&sp->pp_cpq)) {
4783 IF_DROP (&sp->pp_fastq);
4785 IFNET_STAT_INC(ifp, oerrors, 1);
4787 IF_ENQUEUE (&sp->pp_cpq, m);
4788 ifsq = ifq_get_subq_default(&ifp->if_snd);
4789 if (!ifsq_is_oactive(ifsq))
4790 (*ifp->if_start) (ifp, ifsq);
4791 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len + 3);
4795 * Send keepalive packets, every 10 seconds.
4798 sppp_keepalive(void *dummy)
4804 for (sp=spppq; sp; sp=sp->pp_next) {
4805 struct ifnet *ifp = &sp->pp_if;
4807 /* Keepalive mode disabled or channel down? */
4808 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4809 ! (ifp->if_flags & IFF_RUNNING))
4812 /* No keepalive in PPP mode if LCP not opened yet. */
4813 if (sp->pp_mode != IFF_CISCO &&
4814 sp->pp_phase < PHASE_AUTHENTICATE)
4817 if (sp->pp_alivecnt == MAXALIVECNT) {
4818 /* No keepalive packets got. Stop the interface. */
4819 kprintf (SPP_FMT "down\n", SPP_ARGS(ifp));
4821 IF_DRAIN(&sp->pp_cpq);
4822 if (sp->pp_mode != IFF_CISCO) {
4824 /* Shut down the PPP link. */
4826 /* Initiate negotiation. XXX */
4830 ifnet_serialize_all(ifp);
4831 if (sp->pp_alivecnt <= MAXALIVECNT)
4833 if (sp->pp_mode == IFF_CISCO)
4834 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4835 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4836 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4837 long nmagic = htonl (sp->lcp.magic);
4838 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4839 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4840 sp->lcp.echoid, 4, &nmagic);
4842 ifnet_deserialize_all(ifp);
4844 callout_reset(&keepalive_timeout, hz * 10, sppp_keepalive, NULL);
4849 * Get both IP addresses.
4852 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4854 struct ifnet *ifp = &sp->pp_if;
4855 struct ifaddr_container *ifac;
4857 struct sockaddr_in *si, *sm;
4863 * Pick the first AF_INET address from the list,
4864 * aliases don't make any sense on a p2p link anyway.
4867 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
4869 if (ifa->ifa_addr->sa_family == AF_INET) {
4870 si = (struct sockaddr_in *)ifa->ifa_addr;
4871 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4877 if (si && si->sin_addr.s_addr) {
4878 ssrc = si->sin_addr.s_addr;
4880 *srcmask = ntohl(sm->sin_addr.s_addr);
4883 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4884 if (si && si->sin_addr.s_addr)
4885 ddst = si->sin_addr.s_addr;
4888 if (dst) *dst = ntohl(ddst);
4889 if (src) *src = ntohl(ssrc);
4893 * Set my IP address. Must be called at splimp.
4896 sppp_set_ip_addr(struct sppp *sp, u_long src)
4899 struct ifaddr_container *ifac;
4900 struct ifaddr *ifa = NULL;
4901 struct sockaddr_in *si;
4902 struct in_ifaddr *ia;
4905 * Pick the first AF_INET address from the list,
4906 * aliases don't make any sense on a p2p link anyway.
4909 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
4911 if (ifa->ifa_addr->sa_family == AF_INET) {
4912 si = (struct sockaddr_in *)ifa->ifa_addr;
4918 if (ifac != NULL && si != NULL) {
4920 #if __NetBSD_Version__ >= 103080000
4921 struct sockaddr_in new_sin = *si;
4923 new_sin.sin_addr.s_addr = htonl(src);
4924 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 1);
4927 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: in_ifinit "
4928 " failed, error=%d\n", SPP_ARGS(ifp), error);
4931 /* delete old route */
4932 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4935 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4936 SPP_ARGS(ifp), error);
4940 in_iahash_remove(ia);
4942 /* set new address */
4943 si->sin_addr.s_addr = htonl(src);
4944 in_iahash_insert(ia);
4947 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
4950 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
4951 SPP_ARGS(ifp), error);
4959 * Get both IPv6 addresses.
4962 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4963 struct in6_addr *srcmask)
4965 struct ifnet *ifp = &sp->pp_if;
4966 struct ifaddr_container *ifac;
4968 struct sockaddr_in6 *si, *sm;
4969 struct in6_addr ssrc, ddst;
4972 bzero(&ssrc, sizeof(ssrc));
4973 bzero(&ddst, sizeof(ddst));
4975 * Pick the first link-local AF_INET6 address from the list,
4976 * aliases don't make any sense on a p2p link anyway.
4979 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
4981 if (ifa->ifa_addr->sa_family == AF_INET6) {
4982 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4983 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4984 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
4989 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
4990 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
4992 bcopy(&sm->sin6_addr, srcmask,
4997 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
4998 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
4999 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
5003 bcopy(&ddst, dst, sizeof(*dst));
5005 bcopy(&ssrc, src, sizeof(*src));
5008 #ifdef IPV6CP_MYIFID_DYN
5010 * Generate random ifid.
5013 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
5019 * Set my IPv6 address. Must be called at splimp.
5022 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
5025 struct ifaddr_container *ifac;
5027 struct sockaddr_in6 *sin6;
5030 * Pick the first link-local AF_INET6 address from the list,
5031 * aliases don't make any sense on a p2p link anyway.
5035 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
5037 if (ifa->ifa_addr->sa_family == AF_INET6) {
5038 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
5039 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
5044 if (ifac != NULL && sin6 != NULL) {
5046 struct sockaddr_in6 new_sin6 = *sin6;
5048 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
5049 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5050 if (debug && error) {
5051 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
5052 " failed, error=%d\n", SPP_ARGS(ifp), error);
5059 * Suggest a candidate address to be used by peer.
5062 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5064 struct in6_addr myaddr;
5067 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5069 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5071 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5072 myaddr.s6_addr[14] ^= 0xff;
5073 myaddr.s6_addr[15] ^= 0xff;
5075 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5076 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5079 bcopy(&myaddr, suggest, sizeof(myaddr));
5084 sppp_params(struct sppp *sp, u_long cmd, void *data)
5087 struct ifreq *ifr = (struct ifreq *)data;
5088 struct spppreq *spr;
5091 spr = kmalloc(sizeof(struct spppreq), M_TEMP, M_INTWAIT);
5094 * ifr->ifr_data is supposed to point to a struct spppreq.
5095 * Check the cmd word first before attempting to fetch all the
5098 if ((subcmd = fuword(ifr->ifr_data)) == -1) {
5103 if (copyin((caddr_t)ifr->ifr_data, spr, sizeof(struct spppreq)) != 0) {
5109 case (u_long)SPPPIOGDEFS:
5110 if (cmd != SIOCGIFGENERIC) {
5115 * We copy over the entire current state, but clean
5116 * out some of the stuff we don't wanna pass up.
5117 * Remember, SIOCGIFGENERIC is unprotected, and can be
5118 * called by any user. No need to ever get PAP or
5119 * CHAP secrets back to userland anyway.
5121 spr->defs.pp_phase = sp->pp_phase;
5122 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5123 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5124 spr->defs.lcp = sp->lcp;
5125 spr->defs.ipcp = sp->ipcp;
5126 spr->defs.ipv6cp = sp->ipv6cp;
5127 spr->defs.myauth = sp->myauth;
5128 spr->defs.hisauth = sp->hisauth;
5129 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5130 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5131 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5132 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5134 * Fixup the LCP timeout value to milliseconds so
5135 * spppcontrol doesn't need to bother about the value
5136 * of "hz". We do the reverse calculation below when
5139 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5140 rv = copyout(spr, (caddr_t)ifr->ifr_data,
5141 sizeof(struct spppreq));
5144 case (u_long)SPPPIOSDEFS:
5145 if (cmd != SIOCSIFGENERIC) {
5150 * We have a very specific idea of which fields we
5151 * allow being passed back from userland, so to not
5152 * clobber our current state. For one, we only allow
5153 * setting anything if LCP is in dead or establish
5154 * phase. Once the authentication negotiations
5155 * started, the authentication settings must not be
5156 * changed again. (The administrator can force an
5157 * ifconfig down in order to get LCP back into dead
5160 * Also, we only allow for authentication parameters to be
5163 * XXX Should allow to set or clear pp_flags.
5165 * Finally, if the respective authentication protocol to
5166 * be used is set differently than 0, but the secret is
5167 * passed as all zeros, we don't trash the existing secret.
5168 * This allows an administrator to change the system name
5169 * only without clobbering the secret (which he didn't get
5170 * back in a previous SPPPIOGDEFS call). However, the
5171 * secrets are cleared if the authentication protocol is
5173 if (sp->pp_phase != PHASE_DEAD &&
5174 sp->pp_phase != PHASE_ESTABLISH) {
5179 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5180 spr->defs.myauth.proto != PPP_CHAP) ||
5181 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5182 spr->defs.hisauth.proto != PPP_CHAP)) {
5187 if (spr->defs.myauth.proto == 0)
5188 /* resetting myauth */
5189 bzero(&sp->myauth, sizeof sp->myauth);
5191 /* setting/changing myauth */
5192 sp->myauth.proto = spr->defs.myauth.proto;
5193 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5194 if (spr->defs.myauth.secret[0] != '\0')
5195 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5198 if (spr->defs.hisauth.proto == 0)
5199 /* resetting hisauth */
5200 bzero(&sp->hisauth, sizeof sp->hisauth);
5202 /* setting/changing hisauth */
5203 sp->hisauth.proto = spr->defs.hisauth.proto;
5204 sp->hisauth.flags = spr->defs.hisauth.flags;
5205 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5206 if (spr->defs.hisauth.secret[0] != '\0')
5207 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5210 /* set LCP restart timer timeout */
5211 if (spr->defs.lcp.timeout != 0)
5212 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5213 /* set VJ enable and IPv6 disable flags */
5215 if (spr->defs.enable_vj)
5216 sp->confflags |= CONF_ENABLE_VJ;
5218 sp->confflags &= ~CONF_ENABLE_VJ;
5221 if (spr->defs.enable_ipv6)
5222 sp->confflags |= CONF_ENABLE_IPV6;
5224 sp->confflags &= ~CONF_ENABLE_IPV6;
5239 sppp_phase_network(struct sppp *sp)
5245 sp->pp_phase = PHASE_NETWORK;
5248 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5249 sppp_phase_name(sp->pp_phase));
5251 /* Notify NCPs now. */
5252 for (i = 0; i < IDX_COUNT; i++)
5253 if ((cps[i])->flags & CP_NCP)
5256 /* Send Up events to all NCPs. */
5257 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5258 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5261 /* if no NCP is starting, all this was in vain, close down */
5262 sppp_lcp_check_and_close(sp);
5267 sppp_cp_type_name(u_char type)
5269 static char buf[12];
5271 case CONF_REQ: return "conf-req";
5272 case CONF_ACK: return "conf-ack";
5273 case CONF_NAK: return "conf-nak";
5274 case CONF_REJ: return "conf-rej";
5275 case TERM_REQ: return "term-req";
5276 case TERM_ACK: return "term-ack";
5277 case CODE_REJ: return "code-rej";
5278 case PROTO_REJ: return "proto-rej";
5279 case ECHO_REQ: return "echo-req";
5280 case ECHO_REPLY: return "echo-reply";
5281 case DISC_REQ: return "discard-req";
5283 ksnprintf (buf, sizeof(buf), "cp/0x%x", type);
5288 sppp_auth_type_name(u_short proto, u_char type)
5290 static char buf[12];
5294 case CHAP_CHALLENGE: return "challenge";
5295 case CHAP_RESPONSE: return "response";
5296 case CHAP_SUCCESS: return "success";
5297 case CHAP_FAILURE: return "failure";
5301 case PAP_REQ: return "req";
5302 case PAP_ACK: return "ack";
5303 case PAP_NAK: return "nak";
5306 ksnprintf (buf, sizeof(buf), "auth/0x%x", type);
5311 sppp_lcp_opt_name(u_char opt)
5313 static char buf[12];
5315 case LCP_OPT_MRU: return "mru";
5316 case LCP_OPT_ASYNC_MAP: return "async-map";
5317 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5318 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5319 case LCP_OPT_MAGIC: return "magic";
5320 case LCP_OPT_PROTO_COMP: return "proto-comp";
5321 case LCP_OPT_ADDR_COMP: return "addr-comp";
5323 ksnprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5328 sppp_ipcp_opt_name(u_char opt)
5330 static char buf[12];
5332 case IPCP_OPT_ADDRESSES: return "addresses";
5333 case IPCP_OPT_COMPRESSION: return "compression";
5334 case IPCP_OPT_ADDRESS: return "address";
5336 ksnprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5342 sppp_ipv6cp_opt_name(u_char opt)
5344 static char buf[12];
5346 case IPV6CP_OPT_IFID: return "ifid";
5347 case IPV6CP_OPT_COMPRESSION: return "compression";
5349 ksprintf (buf, "0x%x", opt);
5355 sppp_state_name(int state)
5358 case STATE_INITIAL: return "initial";
5359 case STATE_STARTING: return "starting";
5360 case STATE_CLOSED: return "closed";
5361 case STATE_STOPPED: return "stopped";
5362 case STATE_CLOSING: return "closing";
5363 case STATE_STOPPING: return "stopping";
5364 case STATE_REQ_SENT: return "req-sent";
5365 case STATE_ACK_RCVD: return "ack-rcvd";
5366 case STATE_ACK_SENT: return "ack-sent";
5367 case STATE_OPENED: return "opened";
5373 sppp_phase_name(enum ppp_phase phase)
5376 case PHASE_DEAD: return "dead";
5377 case PHASE_ESTABLISH: return "establish";
5378 case PHASE_TERMINATE: return "terminate";
5379 case PHASE_AUTHENTICATE: return "authenticate";
5380 case PHASE_NETWORK: return "network";
5386 sppp_proto_name(u_short proto)
5388 static char buf[12];
5390 case PPP_LCP: return "lcp";
5391 case PPP_IPCP: return "ipcp";
5392 case PPP_PAP: return "pap";
5393 case PPP_CHAP: return "chap";
5394 case PPP_IPV6CP: return "ipv6cp";
5396 ksnprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5401 sppp_print_bytes(const u_char *p, u_short len)
5405 log(-1, " %s", hexncpy(p, len, hexstr, HEX_NCPYLEN(len), "-"));
5409 sppp_print_string(const char *p, u_short len)
5416 * Print only ASCII chars directly. RFC 1994 recommends
5417 * using only them, but we don't rely on it. */
5418 if (c < ' ' || c > '~')
5419 log(-1, "\\x%x", c);
5426 sppp_dotted_quad(u_long addr)
5429 ksprintf(s, "%d.%d.%d.%d",
5430 (int)((addr >> 24) & 0xff),
5431 (int)((addr >> 16) & 0xff),
5432 (int)((addr >> 8) & 0xff),
5433 (int)(addr & 0xff));
5438 sppp_strnlen(u_char *p, int max)
5442 for (len = 0; len < max && *p; ++p)
5447 /* a dummy, used to drop uninteresting events */
5449 sppp_null(struct sppp *unused)
5451 /* do just nothing */