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>
27 #include "opt_inet6.h"
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/sockio.h>
34 #include <sys/socket.h>
35 #include <sys/syslog.h>
36 #include <sys/random.h>
37 #include <sys/thread2.h>
38 #include <sys/malloc.h>
43 #include <net/ifq_var.h>
44 #include <net/netisr.h>
45 #include <net/if_types.h>
46 #include <net/route.h>
47 #include <netinet/in.h>
48 #include <netinet/in_systm.h>
49 #include <netinet/ip.h>
50 #include <net/slcompress.h>
52 #include <machine/stdarg.h>
54 #include <netinet/in_var.h>
57 #include <netinet/tcp.h>
60 #include <netinet/if_ether.h>
63 #include <netproto/ipx/ipx.h>
64 #include <netproto/ipx/ipx_if.h>
69 #define IOCTL_CMD_T u_long
70 #define MAXALIVECNT 3 /* max. alive packets */
73 * Interface flags that can be set in an ifconfig command.
75 * Setting link0 will make the link passive, i.e. it will be marked
76 * as being administrative openable, but won't be opened to begin
77 * with. Incoming calls will be answered, or subsequent calls with
78 * -link1 will cause the administrative open of the LCP layer.
80 * Setting link1 will cause the link to auto-dial only as packets
83 * Setting IFF_DEBUG will syslog the option negotiation and state
84 * transitions at level kern.debug. Note: all logs consistently look
87 * <if-name><unit>: <proto-name> <additional info...>
89 * with <if-name><unit> being something like "bppp0", and <proto-name>
90 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
93 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
94 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
95 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
97 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
98 #define PPP_UI 0x03 /* Unnumbered Information */
99 #define PPP_IP 0x0021 /* Internet Protocol */
100 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
101 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
102 #define PPP_IPX 0x002b /* Novell IPX Protocol */
103 #define PPP_VJ_COMP 0x002d /* VJ compressed TCP/IP */
104 #define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */
105 #define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */
106 #define PPP_LCP 0xc021 /* Link Control Protocol */
107 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
108 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
109 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
110 #define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */
112 #define CONF_REQ 1 /* PPP configure request */
113 #define CONF_ACK 2 /* PPP configure acknowledge */
114 #define CONF_NAK 3 /* PPP configure negative ack */
115 #define CONF_REJ 4 /* PPP configure reject */
116 #define TERM_REQ 5 /* PPP terminate request */
117 #define TERM_ACK 6 /* PPP terminate acknowledge */
118 #define CODE_REJ 7 /* PPP code reject */
119 #define PROTO_REJ 8 /* PPP protocol reject */
120 #define ECHO_REQ 9 /* PPP echo request */
121 #define ECHO_REPLY 10 /* PPP echo reply */
122 #define DISC_REQ 11 /* PPP discard request */
124 #define LCP_OPT_MRU 1 /* maximum receive unit */
125 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
126 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
127 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
128 #define LCP_OPT_MAGIC 5 /* magic number */
129 #define LCP_OPT_RESERVED 6 /* reserved */
130 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
131 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
133 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
134 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
135 #define IPCP_OPT_ADDRESS 3 /* local IP address */
137 #define IPV6CP_OPT_IFID 1 /* interface identifier */
138 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
140 #define IPCP_COMP_VJ 0x2d /* Code for VJ compression */
142 #define PAP_REQ 1 /* PAP name/password request */
143 #define PAP_ACK 2 /* PAP acknowledge */
144 #define PAP_NAK 3 /* PAP fail */
146 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
147 #define CHAP_RESPONSE 2 /* CHAP challenge response */
148 #define CHAP_SUCCESS 3 /* CHAP response ok */
149 #define CHAP_FAILURE 4 /* CHAP response failed */
151 #define CHAP_MD5 5 /* hash algorithm - MD5 */
153 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
154 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
155 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
156 #define CISCO_ADDR_REQ 0 /* Cisco address request */
157 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
158 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
160 /* states are named and numbered according to RFC 1661 */
161 #define STATE_INITIAL 0
162 #define STATE_STARTING 1
163 #define STATE_CLOSED 2
164 #define STATE_STOPPED 3
165 #define STATE_CLOSING 4
166 #define STATE_STOPPING 5
167 #define STATE_REQ_SENT 6
168 #define STATE_ACK_RCVD 7
169 #define STATE_ACK_SENT 8
170 #define STATE_OPENED 9
176 } __attribute__((__packed__));
177 #define PPP_HEADER_LEN sizeof (struct ppp_header)
183 } __attribute__((__packed__));
184 #define LCP_HEADER_LEN sizeof (struct lcp_header)
186 struct cisco_packet {
193 } __attribute__((__packed__));
194 #define CISCO_PACKET_LEN sizeof (struct cisco_packet)
197 * We follow the spelling and capitalization of RFC 1661 here, to make
198 * it easier comparing with the standard. Please refer to this RFC in
199 * case you can't make sense out of these abbreviation; it will also
200 * explain the semantics related to the various events and actions.
203 u_short proto; /* PPP control protocol number */
204 u_char protoidx; /* index into state table in struct sppp */
206 #define CP_LCP 0x01 /* this is the LCP */
207 #define CP_AUTH 0x02 /* this is an authentication protocol */
208 #define CP_NCP 0x04 /* this is a NCP */
209 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
210 const char *name; /* name of this control protocol */
212 void (*Up)(struct sppp *sp);
213 void (*Down)(struct sppp *sp);
214 void (*Open)(struct sppp *sp);
215 void (*Close)(struct sppp *sp);
216 void (*TO)(void *sp);
217 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
218 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
219 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
221 void (*tlu)(struct sppp *sp);
222 void (*tld)(struct sppp *sp);
223 void (*tls)(struct sppp *sp);
224 void (*tlf)(struct sppp *sp);
225 void (*scr)(struct sppp *sp);
228 static struct sppp *spppq;
229 static struct callout keepalive_timeout;
231 #define SPP_FMT "%s: "
232 #define SPP_ARGS(ifp) (ifp)->if_xname
236 * The following disgusting hack gets around the problem that IP TOS
237 * can't be set yet. We want to put "interactive" traffic on a high
238 * priority queue. To decide if traffic is interactive, we check that
239 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
241 * XXX is this really still necessary? - joerg -
243 static u_short interactive_ports[8] = {
247 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
250 /* almost every function needs these */
252 struct ifnet *ifp = &sp->pp_if; \
253 int debug = ifp->if_flags & IFF_DEBUG
255 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
256 struct sockaddr *dst, struct rtentry *rt);
258 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
259 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
261 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
263 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
264 u_char ident, u_short len, void *data);
265 /* static void sppp_cp_timeout(void *arg); */
266 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
268 static void sppp_auth_send(const struct cp *cp,
269 struct sppp *sp, unsigned int type, unsigned int id,
272 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
273 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
274 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
275 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
276 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
278 static void sppp_null(struct sppp *sp);
280 static void sppp_lcp_init(struct sppp *sp);
281 static void sppp_lcp_up(struct sppp *sp);
282 static void sppp_lcp_down(struct sppp *sp);
283 static void sppp_lcp_open(struct sppp *sp);
284 static void sppp_lcp_close(struct sppp *sp);
285 static void sppp_lcp_TO(void *sp);
286 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
287 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
288 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
289 static void sppp_lcp_tlu(struct sppp *sp);
290 static void sppp_lcp_tld(struct sppp *sp);
291 static void sppp_lcp_tls(struct sppp *sp);
292 static void sppp_lcp_tlf(struct sppp *sp);
293 static void sppp_lcp_scr(struct sppp *sp);
294 static void sppp_lcp_check_and_close(struct sppp *sp);
295 static int sppp_ncp_check(struct sppp *sp);
297 static void sppp_ipcp_init(struct sppp *sp);
298 static void sppp_ipcp_up(struct sppp *sp);
299 static void sppp_ipcp_down(struct sppp *sp);
300 static void sppp_ipcp_open(struct sppp *sp);
301 static void sppp_ipcp_close(struct sppp *sp);
302 static void sppp_ipcp_TO(void *sp);
303 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
304 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
305 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
306 static void sppp_ipcp_tlu(struct sppp *sp);
307 static void sppp_ipcp_tld(struct sppp *sp);
308 static void sppp_ipcp_tls(struct sppp *sp);
309 static void sppp_ipcp_tlf(struct sppp *sp);
310 static void sppp_ipcp_scr(struct sppp *sp);
312 static void sppp_ipv6cp_init(struct sppp *sp);
313 static void sppp_ipv6cp_up(struct sppp *sp);
314 static void sppp_ipv6cp_down(struct sppp *sp);
315 static void sppp_ipv6cp_open(struct sppp *sp);
316 static void sppp_ipv6cp_close(struct sppp *sp);
317 static void sppp_ipv6cp_TO(void *sp);
318 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
319 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
320 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
321 static void sppp_ipv6cp_tlu(struct sppp *sp);
322 static void sppp_ipv6cp_tld(struct sppp *sp);
323 static void sppp_ipv6cp_tls(struct sppp *sp);
324 static void sppp_ipv6cp_tlf(struct sppp *sp);
325 static void sppp_ipv6cp_scr(struct sppp *sp);
327 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
328 static void sppp_pap_init(struct sppp *sp);
329 static void sppp_pap_open(struct sppp *sp);
330 static void sppp_pap_close(struct sppp *sp);
331 static void sppp_pap_TO(void *sp);
332 static void sppp_pap_my_TO(void *sp);
333 static void sppp_pap_tlu(struct sppp *sp);
334 static void sppp_pap_tld(struct sppp *sp);
335 static void sppp_pap_scr(struct sppp *sp);
337 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
338 static void sppp_chap_init(struct sppp *sp);
339 static void sppp_chap_open(struct sppp *sp);
340 static void sppp_chap_close(struct sppp *sp);
341 static void sppp_chap_TO(void *sp);
342 static void sppp_chap_tlu(struct sppp *sp);
343 static void sppp_chap_tld(struct sppp *sp);
344 static void sppp_chap_scr(struct sppp *sp);
346 static const char *sppp_auth_type_name(u_short proto, u_char type);
347 static const char *sppp_cp_type_name(u_char type);
348 static const char *sppp_dotted_quad(u_long addr);
349 static const char *sppp_ipcp_opt_name(u_char opt);
351 static const char *sppp_ipv6cp_opt_name(u_char opt);
353 static const char *sppp_lcp_opt_name(u_char opt);
354 static const char *sppp_phase_name(enum ppp_phase phase);
355 static const char *sppp_proto_name(u_short proto);
356 static const char *sppp_state_name(int state);
357 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
358 static int sppp_strnlen(u_char *p, int max);
359 static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst,
361 static void sppp_keepalive(void *dummy);
362 static void sppp_phase_network(struct sppp *sp);
363 static void sppp_print_bytes(const u_char *p, u_short len);
364 static void sppp_print_string(const char *p, u_short len);
365 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
367 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
368 struct in6_addr *dst, struct in6_addr *srcmask);
369 #ifdef IPV6CP_MYIFID_DYN
370 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
371 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
373 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
376 /* our control protocol descriptors */
377 static const struct cp lcp = {
378 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
379 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
380 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
381 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
385 static const struct cp ipcp = {
386 PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
387 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
388 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
389 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
393 static const struct cp ipv6cp = {
394 PPP_IPV6CP, IDX_IPV6CP,
395 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
401 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
402 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
403 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
407 static const struct cp pap = {
408 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
409 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
410 sppp_pap_TO, 0, 0, 0,
411 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
415 static const struct cp chap = {
416 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
417 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
418 sppp_chap_TO, 0, 0, 0,
419 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
423 static const struct cp *cps[IDX_COUNT] = {
425 &ipcp, /* IDX_IPCP */
426 &ipv6cp, /* IDX_IPV6CP */
428 &chap, /* IDX_CHAP */
432 sppp_modevent(module_t mod, int type, void *unused)
436 callout_init(&keepalive_timeout);
446 static moduledata_t spppmod = {
451 MODULE_VERSION(sppp, 1);
452 DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
455 * Exported functions, comprising our interface to the lower layer.
459 * Process the received packet.
462 sppp_input(struct ifnet *ifp, struct mbuf *m)
464 struct ppp_header *h;
466 struct sppp *sp = (struct sppp *)ifp;
468 int hlen, vjlen, do_account = 0;
469 int debug = ifp->if_flags & IFF_DEBUG;
471 if (ifp->if_flags & IFF_UP)
472 /* Count received bytes, add FCS and one flag */
473 IFNET_STAT_INC(ifp, ibytes, m->m_pkthdr.len + 3);
475 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
476 /* Too small packet, drop it. */
479 SPP_FMT "input packet is too small, %d bytes\n",
480 SPP_ARGS(ifp), m->m_pkthdr.len);
484 IFNET_STAT_INC(ifp, ierrors, 1);
485 IFNET_STAT_INC(ifp, iqdrops, 1);
489 /* Get PPP header. */
490 h = mtod (m, struct ppp_header*);
491 m_adj (m, PPP_HEADER_LEN);
493 switch (h->address) {
494 case PPP_ALLSTATIONS:
495 if (h->control != PPP_UI)
497 if (sp->pp_mode == IFF_CISCO) {
500 SPP_FMT "PPP packet in Cisco mode "
501 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
503 h->address, h->control, ntohs(h->protocol));
506 switch (ntohs (h->protocol)) {
510 SPP_FMT "rejecting protocol "
511 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
513 h->address, h->control, ntohs(h->protocol));
514 if (sp->state[IDX_LCP] == STATE_OPENED)
515 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
516 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
518 IFNET_STAT_INC(ifp, noproto, 1);
521 sppp_cp_input(&lcp, sp, m);
525 if (sp->pp_phase >= PHASE_AUTHENTICATE)
526 sppp_pap_input(sp, m);
530 if (sp->pp_phase >= PHASE_AUTHENTICATE)
531 sppp_chap_input(sp, m);
536 if (sp->pp_phase == PHASE_NETWORK)
537 sppp_cp_input(&ipcp, sp, m);
541 if (sp->state[IDX_IPCP] == STATE_OPENED) {
547 if (sp->state[IDX_IPCP] == STATE_OPENED) {
549 sl_uncompress_tcp_core(mtod(m, u_char *),
553 &iphdr, &hlen)) <= 0) {
556 SPP_FMT "VJ uncompress failed on compressed packet\n",
562 * Trim the VJ header off the packet, and prepend
563 * the uncompressed IP header (which will usually
564 * end up in two chained mbufs since there's not
565 * enough leading space in the existing mbuf).
568 M_PREPEND(m, hlen, MB_DONTWAIT);
571 bcopy(iphdr, mtod(m, u_char *), hlen);
578 if (sp->state[IDX_IPCP] == STATE_OPENED) {
579 if (sl_uncompress_tcp_core(mtod(m, u_char *),
581 TYPE_UNCOMPRESSED_TCP,
583 &iphdr, &hlen) != 0) {
586 SPP_FMT "VJ uncompress failed on uncompressed packet\n",
597 if (sp->pp_phase == PHASE_NETWORK)
598 sppp_cp_input(&ipv6cp, sp, m);
603 if (sp->state[IDX_IPV6CP] == STATE_OPENED) {
611 /* IPX IPXCP not implemented yet */
612 if (sp->pp_phase == PHASE_NETWORK) {
620 case CISCO_MULTICAST:
622 /* Don't check the control field here (RFC 1547). */
623 if (sp->pp_mode != IFF_CISCO) {
626 SPP_FMT "Cisco packet in PPP mode "
627 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
629 h->address, h->control, ntohs(h->protocol));
632 switch (ntohs (h->protocol)) {
634 IFNET_STAT_INC(ifp, noproto, 1);
636 case CISCO_KEEPALIVE:
637 sppp_cisco_input ((struct sppp*) ifp, m);
660 default: /* Invalid PPP packet. */
664 SPP_FMT "invalid input packet "
665 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
667 h->address, h->control, ntohs(h->protocol));
671 if (! (ifp->if_flags & IFF_UP) || isr < 0)
676 netisr_queue(isr, m);
679 * Do only account for network packets, not for control
680 * packets. This is used by some subsystems to detect
684 sp->pp_last_recv = time_uptime;
688 * Enqueue transmit packet.
691 sppp_output_serialized(struct ifnet *ifp, struct ifaltq_subque *ifsq,
692 struct mbuf *m, struct sockaddr *dst, struct rtentry *rt)
694 struct sppp *sp = (struct sppp*) ifp;
695 struct ppp_header *h;
696 struct ifqueue *ifq = NULL;
698 int ipproto = PPP_IP;
699 int debug = ifp->if_flags & IFF_DEBUG;
700 struct altq_pktattr pktattr;
704 if ((ifp->if_flags & IFF_UP) == 0 ||
705 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
714 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
719 * Hack to prevent the initialization-time generated
720 * IPv6 multicast packet to erroneously cause a
721 * dialout event in case IPv6 has been
722 * administratively disabled on that interface.
724 if (dst->sa_family == AF_INET6 &&
725 !(sp->confflags & CONF_ENABLE_IPV6))
729 * Interface is not yet running, but auto-dial. Need
730 * to start LCP for it.
732 ifp->if_flags |= IFF_RUNNING;
739 * if the queueing discipline needs packet classification,
740 * do it before prepending link headers.
742 ifq_classify(&ifp->if_snd, m, dst->sa_family, &pktattr);
745 if (dst->sa_family == AF_INET) {
746 /* XXX Check mbuf length here? */
747 struct ip *ip = mtod (m, struct ip*);
748 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
751 * When using dynamic local IP address assignment by using
752 * 0.0.0.0 as a local address, the first TCP session will
753 * not connect because the local TCP checksum is computed
754 * using 0.0.0.0 which will later become our real IP address
755 * so the TCP checksum computed at the remote end will
756 * become invalid. So we
757 * - don't let packets with src ip addr 0 thru
758 * - we flag TCP packets with src ip 0 as an error
761 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
765 if(ip->ip_p == IPPROTO_TCP)
766 return(EADDRNOTAVAIL);
772 * Put low delay, telnet, rlogin and ftp control packets
773 * in front of the queue.
775 if (IF_QFULL (&sp->pp_fastq))
777 else if (ip->ip_tos & IPTOS_LOWDELAY)
779 else if (m->m_len < sizeof *ip + sizeof *tcp)
781 else if (ip->ip_p != IPPROTO_TCP)
783 else if (INTERACTIVE (ntohs (tcp->th_sport)))
785 else if (INTERACTIVE (ntohs (tcp->th_dport)))
789 * Do IP Header compression
791 if (sp->pp_mode != IFF_CISCO && (sp->ipcp.flags & IPCP_VJ) &&
792 ip->ip_p == IPPROTO_TCP)
793 switch (sl_compress_tcp(m, ip, sp->pp_comp,
794 sp->ipcp.compress_cid)) {
795 case TYPE_COMPRESSED_TCP:
796 ipproto = PPP_VJ_COMP;
798 case TYPE_UNCOMPRESSED_TCP:
799 ipproto = PPP_VJ_UCOMP;
813 if (dst->sa_family == AF_INET6) {
814 /* XXX do something tricky here? */
819 * Prepend general data packet PPP header. For now, IP only.
821 M_PREPEND (m, PPP_HEADER_LEN, MB_DONTWAIT);
824 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
826 IFNET_STAT_INC(ifp, oerrors, 1);
831 * May want to check size of packet
832 * (albeit due to the implementation it's always enough)
834 h = mtod (m, struct ppp_header*);
835 if (sp->pp_mode == IFF_CISCO) {
836 h->address = CISCO_UNICAST; /* unicast address */
839 h->address = PPP_ALLSTATIONS; /* broadcast address */
840 h->control = PPP_UI; /* Unnumbered Info */
843 switch (dst->sa_family) {
845 case AF_INET: /* Internet Protocol */
846 if (sp->pp_mode == IFF_CISCO)
847 h->protocol = htons (ETHERTYPE_IP);
850 * Don't choke with an ENETDOWN early. It's
851 * possible that we just started dialing out,
852 * so don't drop the packet immediately. If
853 * we notice that we run out of buffer space
854 * below, we will however remember that we are
855 * not ready to carry IP packets, and return
856 * ENETDOWN, as opposed to ENOBUFS.
858 h->protocol = htons(ipproto);
859 if (sp->state[IDX_IPCP] != STATE_OPENED)
865 case AF_INET6: /* Internet Protocol */
866 if (sp->pp_mode == IFF_CISCO)
867 h->protocol = htons (ETHERTYPE_IPV6);
870 * Don't choke with an ENETDOWN early. It's
871 * possible that we just started dialing out,
872 * so don't drop the packet immediately. If
873 * we notice that we run out of buffer space
874 * below, we will however remember that we are
875 * not ready to carry IP packets, and return
876 * ENETDOWN, as opposed to ENOBUFS.
878 h->protocol = htons(PPP_IPV6);
879 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
885 case AF_IPX: /* Novell IPX Protocol */
886 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
887 ETHERTYPE_IPX : PPP_IPX);
892 IFNET_STAT_INC(ifp, oerrors, 1);
894 return (EAFNOSUPPORT);
898 * Queue message on interface, and start output if interface
911 rv = ifsq_enqueue(ifsq, m, &pktattr);
914 IFNET_STAT_INC(ifp, oerrors, 1);
918 if (!ifsq_is_oactive(ifsq))
919 (*ifp->if_start) (ifp, ifsq);
922 * Count output packets and bytes.
923 * The packet length includes header, FCS and 1 flag,
924 * according to RFC 1333.
926 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len + 3);
929 * Unlike in sppp_input(), we can always bump the timestamp
930 * here since sppp_output() is only called on behalf of
931 * network-layer traffic; control-layer traffic is handled
934 sp->pp_last_sent = time_uptime;
941 sppp_output(struct ifnet *ifp, struct mbuf *m,
942 struct sockaddr *dst, struct rtentry *rt)
944 struct ifaltq_subque *ifsq = ifq_get_subq_default(&ifp->if_snd);
947 ifsq_serialize_hw(ifsq);
948 error = sppp_output_serialized(ifp, ifsq, m, dst, rt);
949 ifsq_deserialize_hw(ifsq);
955 sppp_attach(struct ifnet *ifp)
957 struct sppp *sp = (struct sppp*) ifp;
959 /* Initialize keepalive handler. */
961 callout_reset(&keepalive_timeout, hz * 10,
962 sppp_keepalive, NULL);
964 /* Insert new entry into the keepalive list. */
968 sp->pp_if.if_mtu = PP_MTU;
969 sp->pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
970 sp->pp_if.if_type = IFT_PPP;
971 sp->pp_if.if_output = sppp_output;
973 sp->pp_flags = PP_KEEPALIVE;
975 ifq_set_maxlen(&sp->pp_if.if_snd, 32);
976 sp->pp_fastq.ifq_maxlen = 32;
977 sp->pp_cpq.ifq_maxlen = 20;
980 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
981 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
982 sp->pp_phase = PHASE_DEAD;
984 sp->pp_down = lcp.Down;
985 sp->pp_last_recv = sp->pp_last_sent = time_uptime;
988 sp->confflags |= CONF_ENABLE_VJ;
991 sp->confflags |= CONF_ENABLE_IPV6;
993 sp->pp_comp = kmalloc(sizeof(struct slcompress), M_TEMP, M_WAITOK);
994 sl_compress_init(sp->pp_comp, -1);
997 sppp_ipv6cp_init(sp);
1003 sppp_detach(struct ifnet *ifp)
1005 struct sppp **q, *p, *sp = (struct sppp*) ifp;
1008 /* Remove the entry from the keepalive list. */
1009 for (q = &spppq; (p = *q); q = &p->pp_next)
1015 /* Stop keepalive handler. */
1017 callout_stop(&keepalive_timeout);
1019 for (i = 0; i < IDX_COUNT; i++)
1020 callout_stop(&sp->timeout[i]);
1021 callout_stop(&sp->pap_my_to);
1025 * Flush the interface output queue.
1028 sppp_flush(struct ifnet *ifp)
1030 struct sppp *sp = (struct sppp*) ifp;
1032 ifq_purge_all(&sp->pp_if.if_snd);
1033 IF_DRAIN(&sp->pp_fastq);
1034 IF_DRAIN(&sp->pp_cpq);
1038 * Check if the output queue is empty.
1041 sppp_isempty(struct ifnet *ifp)
1043 struct sppp *sp = (struct sppp*) ifp;
1047 empty = IF_QEMPTY(&sp->pp_fastq) && IF_QEMPTY(&sp->pp_cpq) &&
1048 ifsq_is_empty(ifq_get_subq_default(&sp->pp_if.if_snd));
1054 * Get next packet to send.
1057 sppp_dequeue(struct ifnet *ifp)
1059 struct sppp *sp = (struct sppp*) ifp;
1065 * Process only the control protocol queue until we have at
1066 * least one NCP open.
1068 * Do always serve all three queues in Cisco mode.
1070 IF_DEQUEUE(&sp->pp_cpq, m);
1072 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO)) {
1073 IF_DEQUEUE(&sp->pp_fastq, m);
1076 ifq_get_subq_default(&sp->pp_if.if_snd));
1085 * Pick the next packet, do not remove it from the queue.
1088 sppp_pick(struct ifnet *ifp)
1090 struct sppp *sp = (struct sppp*)ifp;
1095 m = sp->pp_cpq.ifq_head;
1097 (sp->pp_phase == PHASE_NETWORK || sp->pp_mode == IFF_CISCO)) {
1098 if ((m = sp->pp_fastq.ifq_head) == NULL)
1099 m = ifsq_poll(ifq_get_subq_default(&sp->pp_if.if_snd));
1107 * Process an ioctl request. Called on low priority level.
1110 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1112 struct ifreq *ifr = (struct ifreq*) data;
1113 struct sppp *sp = (struct sppp*) ifp;
1114 int rv, going_up, going_down, newmode;
1121 case SIOCSIFDSTADDR:
1125 /* set the interface "up" when assigning an IP address */
1126 ifp->if_flags |= IFF_UP;
1127 /* fall through... */
1130 going_up = ifp->if_flags & IFF_UP &&
1131 (ifp->if_flags & IFF_RUNNING) == 0;
1132 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1133 ifp->if_flags & IFF_RUNNING;
1135 newmode = ifp->if_flags & IFF_PASSIVE;
1137 newmode = ifp->if_flags & IFF_AUTO;
1139 newmode = ifp->if_flags & IFF_CISCO;
1140 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1141 ifp->if_flags |= newmode;
1143 if (newmode != sp->pp_mode) {
1146 going_up = ifp->if_flags & IFF_RUNNING;
1150 if (sp->pp_mode != IFF_CISCO)
1152 else if (sp->pp_tlf)
1155 ifp->if_flags &= ~IFF_RUNNING;
1156 sp->pp_mode = newmode;
1160 if (sp->pp_mode != IFF_CISCO)
1162 sp->pp_mode = newmode;
1163 if (sp->pp_mode == 0) {
1164 ifp->if_flags |= IFF_RUNNING;
1167 if (sp->pp_mode == IFF_CISCO) {
1170 ifp->if_flags |= IFF_RUNNING;
1178 #define ifr_mtu ifr_metric
1181 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1183 ifp->if_mtu = ifr->ifr_mtu;
1188 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1190 ifp->if_mtu = *(short*)data;
1195 ifr->ifr_mtu = ifp->if_mtu;
1200 *(short*)data = ifp->if_mtu;
1207 case SIOCGIFGENERIC:
1208 case SIOCSIFGENERIC:
1209 rv = sppp_params(sp, cmd, data);
1221 * Cisco framing implementation.
1225 * Handle incoming Cisco keepalive protocol packets.
1228 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1231 struct cisco_packet *h;
1234 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1237 SPP_FMT "cisco invalid packet length: %d bytes\n",
1238 SPP_ARGS(ifp), m->m_pkthdr.len);
1241 h = mtod (m, struct cisco_packet*);
1244 SPP_FMT "cisco input: %d bytes "
1245 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1246 SPP_ARGS(ifp), m->m_pkthdr.len,
1247 (u_long)ntohl (h->type), h->par1, h->par2, (u_int)h->rel,
1248 (u_int)h->time0, (u_int)h->time1);
1249 switch (ntohl (h->type)) {
1252 log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n",
1253 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1255 case CISCO_ADDR_REPLY:
1256 /* Reply on address request, ignore */
1258 case CISCO_KEEPALIVE_REQ:
1259 sp->pp_alivecnt = 0;
1260 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1261 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1262 /* Local and remote sequence numbers are equal.
1263 * Probably, the line is in loopback mode. */
1264 if (sp->pp_loopcnt >= MAXALIVECNT) {
1265 kprintf (SPP_FMT "loopback\n",
1268 if (ifp->if_flags & IFF_UP) {
1270 IF_DRAIN(&sp->pp_cpq);
1275 /* Generate new local sequence number */
1276 sp->pp_seq[IDX_LCP] = krandom();
1280 if (! (ifp->if_flags & IFF_UP) &&
1281 (ifp->if_flags & IFF_RUNNING)) {
1283 kprintf (SPP_FMT "up\n", SPP_ARGS(ifp));
1286 case CISCO_ADDR_REQ:
1287 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1289 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1295 * Send Cisco keepalive packet.
1298 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1301 struct ppp_header *h;
1302 struct cisco_packet *ch;
1305 struct ifaltq_subque *ifsq;
1307 getmicrouptime(&tv);
1309 MGETHDR (m, MB_DONTWAIT, MT_DATA);
1312 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1313 m->m_pkthdr.rcvif = 0;
1315 h = mtod (m, struct ppp_header*);
1316 h->address = CISCO_MULTICAST;
1318 h->protocol = htons (CISCO_KEEPALIVE);
1320 ch = (struct cisco_packet*) (h + 1);
1321 ch->type = htonl (type);
1322 ch->par1 = htonl (par1);
1323 ch->par2 = htonl (par2);
1326 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1327 ch->time1 = htons ((u_short) tv.tv_sec);
1331 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1332 SPP_ARGS(ifp), (u_long)ntohl (ch->type), ch->par1,
1333 ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1335 if (IF_QFULL (&sp->pp_cpq)) {
1336 IF_DROP (&sp->pp_fastq);
1339 IF_ENQUEUE (&sp->pp_cpq, m);
1340 ifsq = ifq_get_subq_default(&ifp->if_snd);
1341 if (!ifsq_is_oactive(ifsq))
1342 (*ifp->if_start) (ifp, ifsq);
1343 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len + 3);
1347 * PPP protocol implementation.
1351 * Send PPP control protocol packet.
1354 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1355 u_char ident, u_short len, void *data)
1358 struct ppp_header *h;
1359 struct lcp_header *lh;
1361 struct ifaltq_subque *ifsq;
1363 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1364 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1365 MGETHDR (m, MB_DONTWAIT, MT_DATA);
1368 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1369 m->m_pkthdr.rcvif = 0;
1371 h = mtod (m, struct ppp_header*);
1372 h->address = PPP_ALLSTATIONS; /* broadcast address */
1373 h->control = PPP_UI; /* Unnumbered Info */
1374 h->protocol = htons (proto); /* Link Control Protocol */
1376 lh = (struct lcp_header*) (h + 1);
1379 lh->len = htons (LCP_HEADER_LEN + len);
1381 bcopy (data, lh+1, len);
1384 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1386 sppp_proto_name(proto),
1387 sppp_cp_type_name (lh->type), lh->ident,
1389 sppp_print_bytes ((u_char*) (lh+1), len);
1392 if (IF_QFULL (&sp->pp_cpq)) {
1393 IF_DROP (&sp->pp_fastq);
1395 IFNET_STAT_INC(ifp, oerrors, 1);
1397 IF_ENQUEUE (&sp->pp_cpq, m);
1398 ifsq = ifq_get_subq_default(&ifp->if_snd);
1399 if (!ifsq_is_oactive(ifsq))
1400 (*ifp->if_start) (ifp, ifsq);
1401 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len + 3);
1405 * Handle incoming PPP control protocol packets.
1408 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1411 struct lcp_header *h;
1412 int printlen, len = m->m_pkthdr.len;
1419 SPP_FMT "%s invalid packet length: %d bytes\n",
1420 SPP_ARGS(ifp), cp->name, len);
1423 h = mtod (m, struct lcp_header*);
1425 printlen = ntohs(h->len);
1427 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1428 SPP_ARGS(ifp), cp->name,
1429 sppp_state_name(sp->state[cp->protoidx]),
1430 sppp_cp_type_name (h->type), h->ident, printlen);
1434 sppp_print_bytes ((u_char*) (h+1), printlen - 4);
1437 if (len > ntohs (h->len))
1438 len = ntohs (h->len);
1439 p = (u_char *)(h + 1);
1444 log(-1, SPP_FMT "%s invalid conf-req length %d\n",
1445 SPP_ARGS(ifp), cp->name,
1447 IFNET_STAT_INC(ifp, ierrors, 1);
1450 /* handle states where RCR doesn't get a SCA/SCN */
1451 switch (sp->state[cp->protoidx]) {
1453 case STATE_STOPPING:
1456 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1460 rv = (cp->RCR)(sp, h, len);
1462 /* fatal error, shut down */
1467 switch (sp->state[cp->protoidx]) {
1471 /* fall through... */
1472 case STATE_ACK_SENT:
1473 case STATE_REQ_SENT:
1475 * sppp_cp_change_state() have the side effect of
1476 * restarting the timeouts. We want to avoid that
1477 * if the state don't change, otherwise we won't
1478 * ever timeout and resend a configuration request
1481 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1484 sppp_cp_change_state(cp, sp, rv?
1485 STATE_ACK_SENT: STATE_REQ_SENT);
1488 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1490 sppp_cp_change_state(cp, sp, rv?
1491 STATE_ACK_SENT: STATE_REQ_SENT);
1493 case STATE_ACK_RCVD:
1495 sppp_cp_change_state(cp, sp, STATE_OPENED);
1497 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1502 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1505 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1506 SPP_ARGS(ifp), cp->name,
1507 sppp_cp_type_name(h->type),
1508 sppp_state_name(sp->state[cp->protoidx]));
1509 IFNET_STAT_INC(ifp, ierrors, 1);
1513 if (h->ident != sp->confid[cp->protoidx]) {
1515 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1516 SPP_ARGS(ifp), cp->name,
1517 h->ident, sp->confid[cp->protoidx]);
1518 IFNET_STAT_INC(ifp, ierrors, 1);
1521 switch (sp->state[cp->protoidx]) {
1524 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1527 case STATE_STOPPING:
1529 case STATE_REQ_SENT:
1530 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1531 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1536 case STATE_ACK_RCVD:
1538 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1540 case STATE_ACK_SENT:
1541 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1542 sppp_cp_change_state(cp, sp, STATE_OPENED);
1544 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1545 SPP_ARGS(ifp), cp->name);
1549 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1550 SPP_ARGS(ifp), cp->name,
1551 sppp_cp_type_name(h->type),
1552 sppp_state_name(sp->state[cp->protoidx]));
1553 IFNET_STAT_INC(ifp, ierrors, 1);
1558 if (h->ident != sp->confid[cp->protoidx]) {
1560 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1561 SPP_ARGS(ifp), cp->name,
1562 h->ident, sp->confid[cp->protoidx]);
1563 IFNET_STAT_INC(ifp, ierrors, 1);
1566 if (h->type == CONF_NAK)
1567 (cp->RCN_nak)(sp, h, len);
1569 (cp->RCN_rej)(sp, h, len);
1571 switch (sp->state[cp->protoidx]) {
1574 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1576 case STATE_REQ_SENT:
1577 case STATE_ACK_SENT:
1578 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1580 * Slow things down a bit if we think we might be
1581 * in loopback. Depend on the timeout to send the
1582 * next configuration request.
1591 case STATE_ACK_RCVD:
1592 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1596 case STATE_STOPPING:
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 switch (sp->state[cp->protoidx]) {
1609 case STATE_ACK_RCVD:
1610 case STATE_ACK_SENT:
1611 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1616 case STATE_STOPPING:
1617 case STATE_REQ_SENT:
1619 /* Send Terminate-Ack packet. */
1621 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1622 SPP_ARGS(ifp), cp->name);
1623 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1627 sp->rst_counter[cp->protoidx] = 0;
1628 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1632 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1633 SPP_ARGS(ifp), cp->name,
1634 sppp_cp_type_name(h->type),
1635 sppp_state_name(sp->state[cp->protoidx]));
1636 IFNET_STAT_INC(ifp, ierrors, 1);
1640 switch (sp->state[cp->protoidx]) {
1643 case STATE_REQ_SENT:
1644 case STATE_ACK_SENT:
1647 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1650 case STATE_STOPPING:
1651 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1654 case STATE_ACK_RCVD:
1655 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1660 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1663 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1664 SPP_ARGS(ifp), cp->name,
1665 sppp_cp_type_name(h->type),
1666 sppp_state_name(sp->state[cp->protoidx]));
1667 IFNET_STAT_INC(ifp, ierrors, 1);
1671 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1673 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1674 "danger will robinson\n",
1675 SPP_ARGS(ifp), cp->name,
1676 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1677 switch (sp->state[cp->protoidx]) {
1680 case STATE_REQ_SENT:
1681 case STATE_ACK_SENT:
1683 case STATE_STOPPING:
1686 case STATE_ACK_RCVD:
1687 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1690 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1691 SPP_ARGS(ifp), cp->name,
1692 sppp_cp_type_name(h->type),
1693 sppp_state_name(sp->state[cp->protoidx]));
1694 IFNET_STAT_INC(ifp, ierrors, 1);
1700 const struct cp *upper;
1706 proto = ntohs(*((u_int16_t *)p));
1707 for (i = 0; i < IDX_COUNT; i++) {
1708 if (cps[i]->proto == proto) {
1716 if (catastrophic || debug)
1717 log(catastrophic? LOG_INFO: LOG_DEBUG,
1718 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1719 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1720 sppp_cp_type_name(h->type), proto,
1721 upper ? upper->name : "unknown",
1722 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1725 * if we got RXJ+ against conf-req, the peer does not implement
1726 * this particular protocol type. terminate the protocol.
1728 if (upper && !catastrophic) {
1729 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1735 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1736 switch (sp->state[cp->protoidx]) {
1739 case STATE_REQ_SENT:
1740 case STATE_ACK_SENT:
1742 case STATE_STOPPING:
1745 case STATE_ACK_RCVD:
1746 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1749 kprintf(SPP_FMT "%s illegal %s in state %s\n",
1750 SPP_ARGS(ifp), cp->name,
1751 sppp_cp_type_name(h->type),
1752 sppp_state_name(sp->state[cp->protoidx]));
1753 IFNET_STAT_INC(ifp, ierrors, 1);
1758 if (cp->proto != PPP_LCP)
1760 /* Discard the packet. */
1763 if (cp->proto != PPP_LCP)
1765 if (sp->state[cp->protoidx] != STATE_OPENED) {
1767 log(-1, SPP_FMT "lcp echo req but lcp closed\n",
1769 IFNET_STAT_INC(ifp, ierrors, 1);
1774 log(-1, SPP_FMT "invalid lcp echo request "
1775 "packet length: %d bytes\n",
1776 SPP_ARGS(ifp), len);
1779 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1780 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1781 /* Line loopback mode detected. */
1782 kprintf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1783 sp->pp_loopcnt = MAXALIVECNT * 5;
1785 IF_DRAIN(&sp->pp_cpq);
1787 /* Shut down the PPP link. */
1793 *(long*)(h+1) = htonl (sp->lcp.magic);
1795 log(-1, SPP_FMT "got lcp echo req, sending echo rep\n",
1797 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1800 if (cp->proto != PPP_LCP)
1802 if (h->ident != sp->lcp.echoid) {
1803 IFNET_STAT_INC(ifp, ierrors, 1);
1808 log(-1, SPP_FMT "lcp invalid echo reply "
1809 "packet length: %d bytes\n",
1810 SPP_ARGS(ifp), len);
1814 log(-1, SPP_FMT "lcp got echo rep\n",
1816 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1817 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1818 sp->pp_alivecnt = 0;
1821 /* Unknown packet type -- send Code-Reject packet. */
1824 log(-1, SPP_FMT "%s send code-rej for 0x%x\n",
1825 SPP_ARGS(ifp), cp->name, h->type);
1826 sppp_cp_send(sp, cp->proto, CODE_REJ,
1827 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1828 IFNET_STAT_INC(ifp, ierrors, 1);
1834 * The generic part of all Up/Down/Open/Close/TO event handlers.
1835 * Basically, the state transition handling in the automaton.
1838 sppp_up_event(const struct cp *cp, struct sppp *sp)
1843 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1844 SPP_ARGS(ifp), cp->name,
1845 sppp_state_name(sp->state[cp->protoidx]));
1847 switch (sp->state[cp->protoidx]) {
1849 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1851 case STATE_STARTING:
1852 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1854 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1857 kprintf(SPP_FMT "%s illegal up in state %s\n",
1858 SPP_ARGS(ifp), cp->name,
1859 sppp_state_name(sp->state[cp->protoidx]));
1864 sppp_down_event(const struct cp *cp, struct sppp *sp)
1869 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1870 SPP_ARGS(ifp), cp->name,
1871 sppp_state_name(sp->state[cp->protoidx]));
1873 switch (sp->state[cp->protoidx]) {
1876 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1879 sppp_cp_change_state(cp, sp, STATE_STARTING);
1882 case STATE_STOPPING:
1883 case STATE_REQ_SENT:
1884 case STATE_ACK_RCVD:
1885 case STATE_ACK_SENT:
1886 sppp_cp_change_state(cp, sp, STATE_STARTING);
1890 sppp_cp_change_state(cp, sp, STATE_STARTING);
1893 kprintf(SPP_FMT "%s illegal down in state %s\n",
1894 SPP_ARGS(ifp), cp->name,
1895 sppp_state_name(sp->state[cp->protoidx]));
1901 sppp_open_event(const struct cp *cp, struct sppp *sp)
1906 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1907 SPP_ARGS(ifp), cp->name,
1908 sppp_state_name(sp->state[cp->protoidx]));
1910 switch (sp->state[cp->protoidx]) {
1912 sppp_cp_change_state(cp, sp, STATE_STARTING);
1915 case STATE_STARTING:
1918 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1920 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1924 * Try escaping stopped state. This seems to bite
1925 * people occasionally, in particular for IPCP,
1926 * presumably following previous IPCP negotiation
1927 * aborts. Somehow, we must have missed a Down event
1928 * which would have caused a transition into starting
1929 * state, so as a bandaid we force the Down event now.
1930 * This effectively implements (something like the)
1931 * `restart' option mentioned in the state transition
1932 * table of RFC 1661.
1934 sppp_cp_change_state(cp, sp, STATE_STARTING);
1937 case STATE_STOPPING:
1938 case STATE_REQ_SENT:
1939 case STATE_ACK_RCVD:
1940 case STATE_ACK_SENT:
1944 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1951 sppp_close_event(const struct cp *cp, struct sppp *sp)
1956 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
1957 SPP_ARGS(ifp), cp->name,
1958 sppp_state_name(sp->state[cp->protoidx]));
1960 switch (sp->state[cp->protoidx]) {
1965 case STATE_STARTING:
1966 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1970 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1972 case STATE_STOPPING:
1973 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1978 case STATE_REQ_SENT:
1979 case STATE_ACK_RCVD:
1980 case STATE_ACK_SENT:
1981 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
1982 sppp_cp_send(sp, cp->proto, TERM_REQ,
1983 ++sp->pp_seq[cp->protoidx], 0, 0);
1984 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1990 sppp_to_event(const struct cp *cp, struct sppp *sp)
1997 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
1998 SPP_ARGS(ifp), cp->name,
1999 sppp_state_name(sp->state[cp->protoidx]),
2000 sp->rst_counter[cp->protoidx]);
2002 if (--sp->rst_counter[cp->protoidx] < 0)
2004 switch (sp->state[cp->protoidx]) {
2006 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2009 case STATE_STOPPING:
2010 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2013 case STATE_REQ_SENT:
2014 case STATE_ACK_RCVD:
2015 case STATE_ACK_SENT:
2016 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2022 switch (sp->state[cp->protoidx]) {
2024 case STATE_STOPPING:
2025 sppp_cp_send(sp, cp->proto, TERM_REQ,
2026 ++sp->pp_seq[cp->protoidx], 0, 0);
2027 callout_reset(&sp->timeout[cp->protoidx],
2028 sp->lcp.timeout, cp->TO, sp);
2030 case STATE_REQ_SENT:
2031 case STATE_ACK_RCVD:
2033 /* sppp_cp_change_state() will restart the timer */
2034 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2036 case STATE_ACK_SENT:
2038 callout_reset(&sp->timeout[cp->protoidx],
2039 sp->lcp.timeout, cp->TO, sp);
2047 * Change the state of a control protocol in the state automaton.
2048 * Takes care of starting/stopping the restart timer.
2051 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2053 sp->state[cp->protoidx] = newstate;
2054 callout_stop(&sp->timeout[cp->protoidx]);
2058 case STATE_STARTING:
2064 case STATE_STOPPING:
2065 case STATE_REQ_SENT:
2066 case STATE_ACK_RCVD:
2067 case STATE_ACK_SENT:
2068 callout_reset(&sp->timeout[cp->protoidx],
2069 sp->lcp.timeout, cp->TO, sp);
2075 *--------------------------------------------------------------------------*
2077 * The LCP implementation. *
2079 *--------------------------------------------------------------------------*
2082 sppp_lcp_init(struct sppp *sp)
2084 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2086 sp->state[IDX_LCP] = STATE_INITIAL;
2087 sp->fail_counter[IDX_LCP] = 0;
2088 sp->pp_seq[IDX_LCP] = 0;
2089 sp->pp_rseq[IDX_LCP] = 0;
2091 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2093 /* Note that these values are relevant for all control protocols */
2094 sp->lcp.timeout = 3 * hz;
2095 sp->lcp.max_terminate = 2;
2096 sp->lcp.max_configure = 10;
2097 sp->lcp.max_failure = 10;
2098 callout_init(&sp->timeout[IDX_LCP]);
2102 sppp_lcp_up(struct sppp *sp)
2106 sp->pp_alivecnt = 0;
2107 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2110 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2112 * If this interface is passive or dial-on-demand, and we are
2113 * still in Initial state, it means we've got an incoming
2114 * call. Activate the interface.
2116 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2119 SPP_FMT "Up event", SPP_ARGS(ifp));
2120 ifp->if_flags |= IFF_RUNNING;
2121 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2123 log(-1, "(incoming call)\n");
2124 sp->pp_flags |= PP_CALLIN;
2128 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2129 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2130 ifp->if_flags |= IFF_RUNNING;
2134 sppp_up_event(&lcp, sp);
2138 sppp_lcp_down(struct sppp *sp)
2142 sppp_down_event(&lcp, sp);
2145 * If this is neither a dial-on-demand nor a passive
2146 * interface, simulate an ``ifconfig down'' action, so the
2147 * administrator can force a redial by another ``ifconfig
2148 * up''. XXX For leased line operation, should we immediately
2149 * try to reopen the connection here?
2151 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2153 SPP_FMT "Down event, taking interface down.\n",
2159 SPP_FMT "Down event (carrier loss)\n",
2161 sp->pp_flags &= ~PP_CALLIN;
2162 if (sp->state[IDX_LCP] != STATE_INITIAL)
2164 ifp->if_flags &= ~IFF_RUNNING;
2169 sppp_lcp_open(struct sppp *sp)
2172 * If we are authenticator, negotiate LCP_AUTH
2174 if (sp->hisauth.proto != 0)
2175 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2177 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2178 sp->pp_flags &= ~PP_NEEDAUTH;
2179 sppp_open_event(&lcp, sp);
2183 sppp_lcp_close(struct sppp *sp)
2185 sppp_close_event(&lcp, sp);
2189 sppp_lcp_TO(void *cookie)
2191 sppp_to_event(&lcp, (struct sppp *)cookie);
2195 * Analyze a configure request. Return true if it was agreeable, and
2196 * caused action sca, false if it has been rejected or nak'ed, and
2197 * caused action scn. (The return value is used to make the state
2198 * transition decision in the state automaton.)
2201 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2204 u_char *buf, *r, *p;
2211 buf = r = kmalloc (len, M_TEMP, M_INTWAIT);
2214 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2217 /* pass 1: check for things that need to be rejected */
2219 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2220 /* Sanity check option length */
2222 /* Malicious option - drop immediately.
2223 * XXX Maybe we should just RXJ it?
2225 log(-1, "%s: received malicious LCP option 0x%02x, "
2226 "length 0x%02x, (len: 0x%02x) dropping.\n", ifp->if_xname,
2231 log(-1, " %s ", sppp_lcp_opt_name(*p));
2235 if (len >= 6 && p[1] == 6)
2238 log(-1, "[invalid] ");
2240 case LCP_OPT_ASYNC_MAP:
2241 /* Async control character map. */
2242 if (len >= 6 && p[1] == 6)
2245 log(-1, "[invalid] ");
2248 /* Maximum receive unit. */
2249 if (len >= 4 && p[1] == 4)
2252 log(-1, "[invalid] ");
2254 case LCP_OPT_AUTH_PROTO:
2257 log(-1, "[invalid] ");
2260 authproto = (p[2] << 8) + p[3];
2261 if (authproto == PPP_CHAP && p[1] != 5) {
2263 log(-1, "[invalid chap len] ");
2266 if (sp->myauth.proto == 0) {
2267 /* we are not configured to do auth */
2269 log(-1, "[not configured] ");
2273 * Remote want us to authenticate, remember this,
2274 * so we stay in PHASE_AUTHENTICATE after LCP got
2277 sp->pp_flags |= PP_NEEDAUTH;
2280 /* Others not supported. */
2285 /* Add the option to rejected list. */
2292 log(-1, " send conf-rej\n");
2293 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2299 * pass 2: check for option values that are unacceptable and
2300 * thus require to be nak'ed.
2303 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2308 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2310 log(-1, " %s ", sppp_lcp_opt_name(*p));
2313 /* Magic number -- extract. */
2314 nmagic = (u_long)p[2] << 24 |
2315 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2316 if (nmagic != sp->lcp.magic) {
2319 log(-1, "0x%lx ", nmagic);
2322 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2323 log(-1, "[glitch] ");
2326 * We negate our magic here, and NAK it. If
2327 * we see it later in an NAK packet, we
2328 * suggest a new one.
2330 nmagic = ~sp->lcp.magic;
2332 p[2] = nmagic >> 24;
2333 p[3] = nmagic >> 16;
2338 case LCP_OPT_ASYNC_MAP:
2340 * Async control character map -- just ignore it.
2342 * Quote from RFC 1662, chapter 6:
2343 * To enable this functionality, synchronous PPP
2344 * implementations MUST always respond to the
2345 * Async-Control-Character-Map Configuration
2346 * Option with the LCP Configure-Ack. However,
2347 * acceptance of the Configuration Option does
2348 * not imply that the synchronous implementation
2349 * will do any ACCM mapping. Instead, all such
2350 * octet mapping will be performed by the
2351 * asynchronous-to-synchronous converter.
2357 * Maximum receive unit. Always agreeable,
2358 * but ignored by now.
2360 sp->lcp.their_mru = p[2] * 256 + p[3];
2362 log(-1, "%lu ", sp->lcp.their_mru);
2365 case LCP_OPT_AUTH_PROTO:
2366 authproto = (p[2] << 8) + p[3];
2367 if (sp->myauth.proto != authproto) {
2368 /* not agreed, nak */
2370 log(-1, "[mine %s != his %s] ",
2371 sppp_proto_name(sp->hisauth.proto),
2372 sppp_proto_name(authproto));
2373 p[2] = sp->myauth.proto >> 8;
2374 p[3] = sp->myauth.proto;
2377 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2379 log(-1, "[chap not MD5] ");
2385 /* Add the option to nak'ed list. */
2392 * Local and remote magics equal -- loopback?
2394 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2395 if (sp->pp_loopcnt == MAXALIVECNT*5)
2396 kprintf (SPP_FMT "loopback\n",
2398 if (ifp->if_flags & IFF_UP) {
2400 IF_DRAIN(&sp->pp_cpq);
2405 } else if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2407 log(-1, " max_failure (%d) exceeded, "
2409 sp->lcp.max_failure);
2410 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2413 log(-1, " send conf-nak\n");
2414 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2418 log(-1, " send conf-ack\n");
2419 sp->fail_counter[IDX_LCP] = 0;
2421 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2422 h->ident, origlen, h+1);
2425 kfree (buf, M_TEMP);
2434 * Analyze the LCP Configure-Reject option list, and adjust our
2438 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2444 buf = kmalloc (len, M_TEMP, M_INTWAIT);
2447 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2451 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2452 /* Sanity check option length */
2455 * Malicious option - drop immediately.
2456 * XXX Maybe we should just RXJ it?
2458 log(-1, "%s: received malicious LCP option, "
2459 "dropping.\n", ifp->if_xname);
2463 log(-1, " %s ", sppp_lcp_opt_name(*p));
2466 /* Magic number -- can't use it, use 0 */
2467 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2472 * Should not be rejected anyway, since we only
2473 * negotiate a MRU if explicitly requested by
2476 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2478 case LCP_OPT_AUTH_PROTO:
2480 * Peer doesn't want to authenticate himself,
2481 * deny unless this is a dialout call, and
2482 * AUTHFLAG_NOCALLOUT is set.
2484 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2485 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2487 log(-1, "[don't insist on auth "
2489 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2493 log(-1, "[access denied]\n");
2501 kfree (buf, M_TEMP);
2506 * Analyze the LCP Configure-NAK option list, and adjust our
2510 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2517 buf = kmalloc (len, M_TEMP, M_INTWAIT);
2520 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2524 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2525 /* Sanity check option length */
2528 * Malicious option - drop immediately.
2529 * XXX Maybe we should just RXJ it?
2531 log(-1, "%s: received malicious LCP option, "
2532 "dropping.\n", ifp->if_xname);
2536 log(-1, " %s ", sppp_lcp_opt_name(*p));
2539 /* Magic number -- renegotiate */
2540 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2541 len >= 6 && p[1] == 6) {
2542 magic = (u_long)p[2] << 24 |
2543 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2545 * If the remote magic is our negated one,
2546 * this looks like a loopback problem.
2547 * Suggest a new magic to make sure.
2549 if (magic == ~sp->lcp.magic) {
2551 log(-1, "magic glitch ");
2552 sp->lcp.magic = krandom();
2554 sp->lcp.magic = magic;
2556 log(-1, "%lu ", magic);
2562 * Peer wants to advise us to negotiate an MRU.
2563 * Agree on it if it's reasonable, or use
2564 * default otherwise.
2566 if (len >= 4 && p[1] == 4) {
2567 u_int mru = p[2] * 256 + p[3];
2569 log(-1, "%d ", mru);
2570 if (mru < PP_MTU || mru > PP_MAX_MRU)
2573 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2576 case LCP_OPT_AUTH_PROTO:
2578 * Peer doesn't like our authentication method,
2582 log(-1, "[access denied]\n");
2590 kfree (buf, M_TEMP);
2595 sppp_lcp_tlu(struct sppp *sp)
2602 if (! (ifp->if_flags & IFF_UP) &&
2603 (ifp->if_flags & IFF_RUNNING)) {
2604 /* Coming out of loopback mode. */
2606 kprintf (SPP_FMT "up\n", SPP_ARGS(ifp));
2609 for (i = 0; i < IDX_COUNT; i++)
2610 if ((cps[i])->flags & CP_QUAL)
2613 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2614 (sp->pp_flags & PP_NEEDAUTH) != 0)
2615 sp->pp_phase = PHASE_AUTHENTICATE;
2617 sp->pp_phase = PHASE_NETWORK;
2620 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2621 sppp_phase_name(sp->pp_phase));
2624 * Open all authentication protocols. This is even required
2625 * if we already proceeded to network phase, since it might be
2626 * that remote wants us to authenticate, so we might have to
2627 * send a PAP request. Undesired authentication protocols
2628 * don't do anything when they get an Open event.
2630 for (i = 0; i < IDX_COUNT; i++)
2631 if ((cps[i])->flags & CP_AUTH)
2634 if (sp->pp_phase == PHASE_NETWORK) {
2635 /* Notify all NCPs. */
2636 for (i = 0; i < IDX_COUNT; i++)
2637 if (((cps[i])->flags & CP_NCP) &&
2640 * Hack to administratively disable IPv6 if
2641 * not desired. Perhaps we should have another
2642 * flag for this, but right now, we can make
2643 * all struct cp's read/only.
2645 (cps[i] != &ipv6cp ||
2646 (sp->confflags & CONF_ENABLE_IPV6)))
2650 /* Send Up events to all started protos. */
2651 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2652 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2655 /* notify low-level driver of state change */
2657 sp->pp_chg(sp, (int)sp->pp_phase);
2659 if (sp->pp_phase == PHASE_NETWORK)
2660 /* if no NCP is starting, close down */
2661 sppp_lcp_check_and_close(sp);
2665 sppp_lcp_tld(struct sppp *sp)
2671 sp->pp_phase = PHASE_TERMINATE;
2674 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2675 sppp_phase_name(sp->pp_phase));
2678 * Take upper layers down. We send the Down event first and
2679 * the Close second to prevent the upper layers from sending
2680 * ``a flurry of terminate-request packets'', as the RFC
2683 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2684 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2686 (cps[i])->Close(sp);
2691 sppp_lcp_tls(struct sppp *sp)
2695 sp->pp_phase = PHASE_ESTABLISH;
2698 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2699 sppp_phase_name(sp->pp_phase));
2701 /* Notify lower layer if desired. */
2709 sppp_lcp_tlf(struct sppp *sp)
2713 sp->pp_phase = PHASE_DEAD;
2715 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2716 sppp_phase_name(sp->pp_phase));
2718 /* Notify lower layer if desired. */
2726 sppp_lcp_scr(struct sppp *sp)
2728 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2732 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2733 if (! sp->lcp.magic)
2734 sp->lcp.magic = krandom();
2735 opt[i++] = LCP_OPT_MAGIC;
2737 opt[i++] = sp->lcp.magic >> 24;
2738 opt[i++] = sp->lcp.magic >> 16;
2739 opt[i++] = sp->lcp.magic >> 8;
2740 opt[i++] = sp->lcp.magic;
2743 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2744 opt[i++] = LCP_OPT_MRU;
2746 opt[i++] = sp->lcp.mru >> 8;
2747 opt[i++] = sp->lcp.mru;
2750 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2751 authproto = sp->hisauth.proto;
2752 opt[i++] = LCP_OPT_AUTH_PROTO;
2753 opt[i++] = authproto == PPP_CHAP? 5: 4;
2754 opt[i++] = authproto >> 8;
2755 opt[i++] = authproto;
2756 if (authproto == PPP_CHAP)
2757 opt[i++] = CHAP_MD5;
2760 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2761 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2765 * Check the open NCPs, return true if at least one NCP is open.
2768 sppp_ncp_check(struct sppp *sp)
2772 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2773 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2779 * Re-check the open NCPs and see if we should terminate the link.
2780 * Called by the NCPs during their tlf action handling.
2783 sppp_lcp_check_and_close(struct sppp *sp)
2786 if (sp->pp_phase < PHASE_NETWORK)
2787 /* don't bother, we are already going down */
2790 if (sppp_ncp_check(sp))
2797 *--------------------------------------------------------------------------*
2799 * The IPCP implementation. *
2801 *--------------------------------------------------------------------------*
2805 sppp_ipcp_init(struct sppp *sp)
2809 sp->state[IDX_IPCP] = STATE_INITIAL;
2810 sp->fail_counter[IDX_IPCP] = 0;
2811 sp->pp_seq[IDX_IPCP] = 0;
2812 sp->pp_rseq[IDX_IPCP] = 0;
2813 callout_init(&sp->timeout[IDX_IPCP]);
2817 sppp_ipcp_up(struct sppp *sp)
2819 sppp_up_event(&ipcp, sp);
2823 sppp_ipcp_down(struct sppp *sp)
2825 sppp_down_event(&ipcp, sp);
2829 sppp_ipcp_open(struct sppp *sp)
2832 u_long myaddr, hisaddr;
2834 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2835 IPCP_MYADDR_DYN | IPCP_VJ);
2838 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2840 * If we don't have his address, this probably means our
2841 * interface doesn't want to talk IP at all. (This could
2842 * be the case if somebody wants to speak only IPX, for
2843 * example.) Don't open IPCP in this case.
2845 if (hisaddr == 0L) {
2846 /* XXX this message should go away */
2848 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2854 * I don't have an assigned address, so i need to
2855 * negotiate my address.
2857 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2858 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2860 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2861 if (sp->confflags & CONF_ENABLE_VJ) {
2862 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2863 sp->ipcp.max_state = MAX_STATES - 1;
2864 sp->ipcp.compress_cid = 1;
2866 sppp_open_event(&ipcp, sp);
2870 sppp_ipcp_close(struct sppp *sp)
2872 sppp_close_event(&ipcp, sp);
2873 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2875 * My address was dynamic, clear it again.
2877 sppp_set_ip_addr(sp, 0L);
2881 sppp_ipcp_TO(void *cookie)
2883 sppp_to_event(&ipcp, (struct sppp *)cookie);
2887 * Analyze a configure request. Return true if it was agreeable, and
2888 * caused action sca, false if it has been rejected or nak'ed, and
2889 * caused action scn. (The return value is used to make the state
2890 * transition decision in the state automaton.)
2893 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2895 u_char *buf, *r, *p;
2896 struct ifnet *ifp = &sp->pp_if;
2897 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2898 u_long hisaddr, desiredaddr;
2905 * Make sure to allocate a buf that can at least hold a
2906 * conf-nak with an `address' option. We might need it below.
2908 buf = r = kmalloc ((len < 6? 6: len), M_TEMP, M_INTWAIT);
2910 /* pass 1: see if we can recognize them */
2912 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2915 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2916 /* Sanity check option length */
2918 /* XXX should we just RXJ? */
2919 log(-1, "%s: malicious IPCP option received, dropping\n",
2924 log(-1, " %s ", sppp_ipcp_opt_name(*p));
2926 case IPCP_OPT_COMPRESSION:
2927 if (!(sp->confflags & CONF_ENABLE_VJ)) {
2928 /* VJ compression administratively disabled */
2930 log(-1, "[locally disabled] ");
2934 * In theory, we should only conf-rej an
2935 * option that is shorter than RFC 1618
2936 * requires (i.e. < 4), and should conf-nak
2937 * anything else that is not VJ. However,
2938 * since our algorithm always uses the
2939 * original option to NAK it with new values,
2940 * things would become more complicated. In
2941 * pratice, the only commonly implemented IP
2942 * compression option is VJ anyway, so the
2943 * difference is negligible.
2945 if (len >= 6 && p[1] == 6) {
2947 * correctly formed compression option
2948 * that could be VJ compression
2953 log(-1, "optlen %d [invalid/unsupported] ",
2956 case IPCP_OPT_ADDRESS:
2957 if (len >= 6 && p[1] == 6) {
2958 /* correctly formed address option */
2962 log(-1, "[invalid] ");
2965 /* Others not supported. */
2970 /* Add the option to rejected list. */
2977 log(-1, " send conf-rej\n");
2978 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2983 /* pass 2: parse option values */
2984 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
2986 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
2990 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2992 log(-1, " %s ", sppp_ipcp_opt_name(*p));
2994 case IPCP_OPT_COMPRESSION:
2995 desiredcomp = p[2] << 8 | p[3];
2996 /* We only support VJ */
2997 if (desiredcomp == IPCP_COMP_VJ) {
2999 log(-1, "VJ [ack] ");
3000 sp->ipcp.flags |= IPCP_VJ;
3001 sl_compress_init(sp->pp_comp, p[4]);
3002 sp->ipcp.max_state = p[4];
3003 sp->ipcp.compress_cid = p[5];
3007 log(-1, "compproto %#04x [not supported] ",
3009 p[2] = IPCP_COMP_VJ >> 8;
3010 p[3] = IPCP_COMP_VJ;
3011 p[4] = sp->ipcp.max_state;
3012 p[5] = sp->ipcp.compress_cid;
3014 case IPCP_OPT_ADDRESS:
3015 /* This is the address he wants in his end */
3016 desiredaddr = p[2] << 24 | p[3] << 16 |
3018 if (desiredaddr == hisaddr ||
3019 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3021 * Peer's address is same as our value,
3022 * or we have set it to 0.0.0.* to
3023 * indicate that we do not really care,
3024 * this is agreeable. Gonna conf-ack
3028 log(-1, "%s [ack] ",
3029 sppp_dotted_quad(hisaddr));
3030 /* record that we've seen it already */
3031 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3035 * The address wasn't agreeable. This is either
3036 * he sent us 0.0.0.0, asking to assign him an
3037 * address, or he send us another address not
3038 * matching our value. Either case, we gonna
3039 * conf-nak it with our value.
3040 * XXX: we should "rej" if hisaddr == 0
3043 if (desiredaddr == 0)
3044 log(-1, "[addr requested] ");
3046 log(-1, "%s [not agreed] ",
3047 sppp_dotted_quad(desiredaddr));
3050 p[2] = hisaddr >> 24;
3051 p[3] = hisaddr >> 16;
3052 p[4] = hisaddr >> 8;
3056 /* Add the option to nak'ed list. */
3063 * If we are about to conf-ack the request, but haven't seen
3064 * his address so far, gonna conf-nak it instead, with the
3065 * `address' option present and our idea of his address being
3066 * filled in there, to request negotiation of both addresses.
3068 * XXX This can result in an endless req - nak loop if peer
3069 * doesn't want to send us his address. Q: What should we do
3070 * about it? XXX A: implement the max-failure counter.
3072 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3073 buf[0] = IPCP_OPT_ADDRESS;
3075 buf[2] = hisaddr >> 24;
3076 buf[3] = hisaddr >> 16;
3077 buf[4] = hisaddr >> 8;
3081 log(-1, "still need hisaddr ");
3086 log(-1, " send conf-nak\n");
3087 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3090 log(-1, " send conf-ack\n");
3091 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3092 h->ident, origlen, h+1);
3095 kfree (buf, M_TEMP);
3104 * Analyze the IPCP Configure-Reject option list, and adjust our
3108 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3111 struct ifnet *ifp = &sp->pp_if;
3112 int debug = ifp->if_flags & IFF_DEBUG;
3115 buf = kmalloc (len, M_TEMP, M_INTWAIT);
3118 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3122 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3123 /* Sanity check option length */
3125 /* XXX should we just RXJ? */
3126 log(-1, "%s: malicious IPCP option received, dropping\n",
3131 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3133 case IPCP_OPT_COMPRESSION:
3134 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3136 case IPCP_OPT_ADDRESS:
3138 * Peer doesn't grok address option. This is
3139 * bad. XXX Should we better give up here?
3140 * XXX We could try old "addresses" option...
3142 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3149 kfree (buf, M_TEMP);
3154 * Analyze the IPCP Configure-NAK option list, and adjust our
3158 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3161 struct ifnet *ifp = &sp->pp_if;
3162 int debug = ifp->if_flags & IFF_DEBUG;
3167 buf = kmalloc (len, M_TEMP, M_INTWAIT);
3170 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3174 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3175 /* Sanity check option length */
3177 /* XXX should we just RXJ? */
3178 log(-1, "%s: malicious IPCP option received, dropping\n",
3183 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3185 case IPCP_OPT_COMPRESSION:
3186 if (len >= 6 && p[1] == 6) {
3187 desiredcomp = p[2] << 8 | p[3];
3189 log(-1, "[wantcomp %#04x] ",
3191 if (desiredcomp == IPCP_COMP_VJ) {
3192 sl_compress_init(sp->pp_comp, p[4]);
3193 sp->ipcp.max_state = p[4];
3194 sp->ipcp.compress_cid = p[5];
3196 log(-1, "[agree] ");
3199 ~(1 << IPCP_OPT_COMPRESSION);
3202 case IPCP_OPT_ADDRESS:
3204 * Peer doesn't like our local IP address. See
3205 * if we can do something for him. We'll drop
3206 * him our address then.
3208 if (len >= 6 && p[1] == 6) {
3209 wantaddr = p[2] << 24 | p[3] << 16 |
3211 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3213 log(-1, "[wantaddr %s] ",
3214 sppp_dotted_quad(wantaddr));
3216 * When doing dynamic address assignment,
3217 * we accept his offer. Otherwise, we
3218 * ignore it and thus continue to negotiate
3219 * our already existing value.
3220 * XXX: Bogus, if he said no once, he'll
3221 * just say no again, might as well die.
3223 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3224 sppp_set_ip_addr(sp, wantaddr);
3226 log(-1, "[agree] ");
3227 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3235 kfree (buf, M_TEMP);
3240 sppp_ipcp_tlu(struct sppp *sp)
3242 /* we are up - notify isdn daemon */
3248 sppp_ipcp_tld(struct sppp *sp)
3253 sppp_ipcp_tls(struct sppp *sp)
3255 /* indicate to LCP that it must stay alive */
3256 sp->lcp.protos |= (1 << IDX_IPCP);
3260 sppp_ipcp_tlf(struct sppp *sp)
3262 /* we no longer need LCP */
3263 sp->lcp.protos &= ~(1 << IDX_IPCP);
3264 sppp_lcp_check_and_close(sp);
3268 sppp_ipcp_scr(struct sppp *sp)
3270 char opt[6 /* compression */ + 6 /* address */];
3274 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3275 opt[i++] = IPCP_OPT_COMPRESSION;
3277 opt[i++] = IPCP_COMP_VJ >> 8;
3278 opt[i++] = IPCP_COMP_VJ;
3279 opt[i++] = sp->ipcp.max_state;
3280 opt[i++] = sp->ipcp.compress_cid;
3282 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3283 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3284 opt[i++] = IPCP_OPT_ADDRESS;
3286 opt[i++] = ouraddr >> 24;
3287 opt[i++] = ouraddr >> 16;
3288 opt[i++] = ouraddr >> 8;
3292 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3293 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3297 *--------------------------------------------------------------------------*
3299 * The IPv6CP implementation. *
3301 *--------------------------------------------------------------------------*
3306 sppp_ipv6cp_init(struct sppp *sp)
3308 sp->ipv6cp.opts = 0;
3309 sp->ipv6cp.flags = 0;
3310 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3311 sp->fail_counter[IDX_IPV6CP] = 0;
3312 sp->pp_seq[IDX_IPV6CP] = 0;
3313 sp->pp_rseq[IDX_IPV6CP] = 0;
3314 callout_init(&sp->timeout[IDX_IPV6CP]);
3318 sppp_ipv6cp_up(struct sppp *sp)
3320 sppp_up_event(&ipv6cp, sp);
3324 sppp_ipv6cp_down(struct sppp *sp)
3326 sppp_down_event(&ipv6cp, sp);
3330 sppp_ipv6cp_open(struct sppp *sp)
3333 struct in6_addr myaddr, hisaddr;
3335 #ifdef IPV6CP_MYIFID_DYN
3336 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3338 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3341 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3343 * If we don't have our address, this probably means our
3344 * interface doesn't want to talk IPv6 at all. (This could
3345 * be the case if somebody wants to speak only IPX, for
3346 * example.) Don't open IPv6CP in this case.
3348 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3349 /* XXX this message should go away */
3351 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3356 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3357 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3358 sppp_open_event(&ipv6cp, sp);
3362 sppp_ipv6cp_close(struct sppp *sp)
3364 sppp_close_event(&ipv6cp, sp);
3368 sppp_ipv6cp_TO(void *cookie)
3370 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3374 * Analyze a configure request. Return true if it was agreeable, and
3375 * caused action sca, false if it has been rejected or nak'ed, and
3376 * caused action scn. (The return value is used to make the state
3377 * transition decision in the state automaton.)
3380 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3382 u_char *buf, *r, *p;
3383 struct ifnet *ifp = &sp->pp_if;
3384 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3385 struct in6_addr myaddr, desiredaddr, suggestaddr;
3388 int collision, nohisaddr;
3393 * Make sure to allocate a buf that can at least hold a
3394 * conf-nak with an `address' option. We might need it below.
3396 buf = r = kmalloc ((len < 6? 6: len), M_TEMP, M_INTWAIT);
3398 /* pass 1: see if we can recognize them */
3400 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3404 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3405 /* Sanity check option length */
3408 log(-1, "%s: received malicious IPCPv6 option, "
3409 "dropping\n", ifp->if_xname);
3413 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3415 case IPV6CP_OPT_IFID:
3416 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3417 /* correctly formed address option */
3422 log(-1, " [invalid]");
3425 case IPV6CP_OPT_COMPRESSION:
3426 if (len >= 4 && p[1] >= 4) {
3427 /* correctly formed compress option */
3431 log(-1, " [invalid]");
3435 /* Others not supported. */
3440 /* Add the option to rejected list. */
3447 log(-1, " send conf-rej\n");
3448 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3453 /* pass 2: parse option values */
3454 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3456 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3461 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
3463 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3466 case IPV6CP_OPT_COMPRESSION:
3469 case IPV6CP_OPT_IFID:
3470 bzero(&desiredaddr, sizeof(desiredaddr));
3471 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3472 collision = (bcmp(&desiredaddr.s6_addr[8],
3473 &myaddr.s6_addr[8], 8) == 0);
3474 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3476 desiredaddr.s6_addr16[0] = htons(0xfe80);
3477 desiredaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3479 if (!collision && !nohisaddr) {
3480 /* no collision, hisaddr known - Conf-Ack */
3485 ip6_sprintf(&desiredaddr),
3486 sppp_cp_type_name(type));
3491 bzero(&suggestaddr, sizeof(suggestaddr));
3492 if (collision && nohisaddr) {
3493 /* collision, hisaddr unknown - Conf-Rej */
3498 * - no collision, hisaddr unknown, or
3499 * - collision, hisaddr known
3500 * Conf-Nak, suggest hisaddr
3503 sppp_suggest_ip6_addr(sp, &suggestaddr);
3504 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3507 log(-1, " %s [%s]", ip6_sprintf(&desiredaddr),
3508 sppp_cp_type_name(type));
3511 /* Add the option to nak'ed list. */
3517 if (rlen == 0 && type == CONF_ACK) {
3519 log(-1, " send %s\n", sppp_cp_type_name(type));
3520 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3523 if (type == CONF_ACK)
3524 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3528 log(-1, " send %s suggest %s\n",
3529 sppp_cp_type_name(type), ip6_sprintf(&suggestaddr));
3531 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3535 kfree (buf, M_TEMP);
3544 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3548 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3551 struct ifnet *ifp = &sp->pp_if;
3552 int debug = ifp->if_flags & IFF_DEBUG;
3555 buf = kmalloc (len, M_TEMP, M_INTWAIT);
3558 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3562 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3565 log(-1, "%s: received malicious IPCPv6 option, "
3566 "dropping\n", ifp->if_xname);
3570 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3572 case IPV6CP_OPT_IFID:
3574 * Peer doesn't grok address option. This is
3575 * bad. XXX Should we better give up here?
3577 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3580 case IPV6CP_OPT_COMPRESS:
3581 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3589 kfree (buf, M_TEMP);
3594 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3598 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3601 struct ifnet *ifp = &sp->pp_if;
3602 int debug = ifp->if_flags & IFF_DEBUG;
3603 struct in6_addr suggestaddr;
3606 buf = kmalloc (len, M_TEMP, M_INTWAIT);
3609 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3613 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
3616 log(-1, "%s: received malicious IPCPv6 option, "
3617 "dropping\n", ifp->if_xname);
3621 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3623 case IPV6CP_OPT_IFID:
3625 * Peer doesn't like our local ifid. See
3626 * if we can do something for him. We'll drop
3627 * him our address then.
3629 if (len < 10 || p[1] != 10)
3631 bzero(&suggestaddr, sizeof(suggestaddr));
3632 suggestaddr.s6_addr16[0] = htons(0xfe80);
3633 suggestaddr.s6_addr16[1] = htons(sp->pp_if.if_index);
3634 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3636 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3638 log(-1, " [suggestaddr %s]",
3639 ip6_sprintf(&suggestaddr));
3640 #ifdef IPV6CP_MYIFID_DYN
3642 * When doing dynamic address assignment,
3643 * we accept his offer.
3645 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3646 struct in6_addr lastsuggest;
3648 * If <suggested myaddr from peer> equals to
3649 * <hisaddr we have suggested last time>,
3650 * we have a collision. generate new random
3653 sppp_suggest_ip6_addr(&lastsuggest);
3654 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3657 log(-1, " [random]");
3658 sppp_gen_ip6_addr(sp, &suggestaddr);
3660 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3662 log(-1, " [agree]");
3663 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3667 * Since we do not do dynamic address assignment,
3668 * we ignore it and thus continue to negotiate
3669 * our already existing value. This can possibly
3670 * go into infinite request-reject loop.
3672 * This is not likely because we normally use
3673 * ifid based on MAC-address.
3674 * If you have no ethernet card on the node, too bad.
3675 * XXX should we use fail_counter?
3680 case IPV6CP_OPT_COMPRESS:
3682 * Peer wants different compression parameters.
3691 kfree (buf, M_TEMP);
3695 sppp_ipv6cp_tlu(struct sppp *sp)
3697 /* we are up - notify isdn daemon */
3703 sppp_ipv6cp_tld(struct sppp *sp)
3708 sppp_ipv6cp_tls(struct sppp *sp)
3710 /* indicate to LCP that it must stay alive */
3711 sp->lcp.protos |= (1 << IDX_IPV6CP);
3715 sppp_ipv6cp_tlf(struct sppp *sp)
3718 #if 0 /* need #if 0 to close IPv6CP properly */
3719 /* we no longer need LCP */
3720 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3721 sppp_lcp_check_and_close(sp);
3726 sppp_ipv6cp_scr(struct sppp *sp)
3728 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3729 struct in6_addr ouraddr;
3732 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3733 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3734 opt[i++] = IPV6CP_OPT_IFID;
3736 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3741 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3742 opt[i++] = IPV6CP_OPT_COMPRESSION;
3744 opt[i++] = 0; /* TBD */
3745 opt[i++] = 0; /* TBD */
3746 /* variable length data may follow */
3750 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3751 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3755 sppp_ipv6cp_init(struct sppp *sp)
3760 sppp_ipv6cp_up(struct sppp *sp)
3765 sppp_ipv6cp_down(struct sppp *sp)
3771 sppp_ipv6cp_open(struct sppp *sp)
3776 sppp_ipv6cp_close(struct sppp *sp)
3781 sppp_ipv6cp_TO(void *sp)
3786 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3792 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3797 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3802 sppp_ipv6cp_tlu(struct sppp *sp)
3807 sppp_ipv6cp_tld(struct sppp *sp)
3812 sppp_ipv6cp_tls(struct sppp *sp)
3817 sppp_ipv6cp_tlf(struct sppp *sp)
3822 sppp_ipv6cp_scr(struct sppp *sp)
3828 *--------------------------------------------------------------------------*
3830 * The CHAP implementation. *
3832 *--------------------------------------------------------------------------*
3836 * The authentication protocols don't employ a full-fledged state machine as
3837 * the control protocols do, since they do have Open and Close events, but
3838 * not Up and Down, nor are they explicitly terminated. Also, use of the
3839 * authentication protocols may be different in both directions (this makes
3840 * sense, think of a machine that never accepts incoming calls but only
3841 * calls out, it doesn't require the called party to authenticate itself).
3843 * Our state machine for the local authentication protocol (we are requesting
3844 * the peer to authenticate) looks like:
3847 * +--------------------------------------------+
3849 * +--------+ Close +---------+ RCA+
3850 * | |<----------------------------------| |------+
3851 * +--->| Closed | TO* | Opened | sca |
3852 * | | |-----+ +-------| |<-----+
3853 * | +--------+ irc | | +---------+
3859 * | | +------->+ | |
3861 * | +--------+ V | |
3862 * | | |<----+<--------------------+ |
3868 * +------+ +------------------------------------------+
3869 * scn,tld sca,irc,ict,tlu
3874 * Open: LCP reached authentication phase
3875 * Close: LCP reached terminate phase
3877 * RCA+: received reply (pap-req, chap-response), acceptable
3878 * RCN: received reply (pap-req, chap-response), not acceptable
3879 * TO+: timeout with restart counter >= 0
3880 * TO-: timeout with restart counter < 0
3881 * TO*: reschedule timeout for CHAP
3883 * scr: send request packet (none for PAP, chap-challenge)
3884 * sca: send ack packet (pap-ack, chap-success)
3885 * scn: send nak packet (pap-nak, chap-failure)
3886 * ict: initialize re-challenge timer (CHAP only)
3888 * tlu: this-layer-up, LCP reaches network phase
3889 * tld: this-layer-down, LCP enters terminate phase
3891 * Note that in CHAP mode, after sending a new challenge, while the state
3892 * automaton falls back into Req-Sent state, it doesn't signal a tld
3893 * event to LCP, so LCP remains in network phase. Only after not getting
3894 * any response (or after getting an unacceptable response), CHAP closes,
3895 * causing LCP to enter terminate phase.
3897 * With PAP, there is no initial request that can be sent. The peer is
3898 * expected to send one based on the successful negotiation of PAP as
3899 * the authentication protocol during the LCP option negotiation.
3901 * Incoming authentication protocol requests (remote requests
3902 * authentication, we are peer) don't employ a state machine at all,
3903 * they are simply answered. Some peers [Ascend P50 firmware rev
3904 * 4.50] react allergically when sending IPCP requests while they are
3905 * still in authentication phase (thereby violating the standard that
3906 * demands that these NCP packets are to be discarded), so we keep
3907 * track of the peer demanding us to authenticate, and only proceed to
3908 * phase network once we've seen a positive acknowledge for the
3913 * Handle incoming CHAP packets.
3916 sppp_chap_input(struct sppp *sp, struct mbuf *m)
3919 struct lcp_header *h;
3921 u_char *value, *name, digest[AUTHKEYLEN], dsize;
3922 int value_len, name_len;
3925 len = m->m_pkthdr.len;
3929 SPP_FMT "chap invalid packet length: %d bytes\n",
3930 SPP_ARGS(ifp), len);
3933 h = mtod (m, struct lcp_header*);
3934 if (len > ntohs (h->len))
3935 len = ntohs (h->len);
3938 /* challenge, failure and success are his authproto */
3939 case CHAP_CHALLENGE:
3940 value = 1 + (u_char*)(h+1);
3941 value_len = value[-1];
3942 name = value + value_len;
3943 name_len = len - value_len - 5;
3947 SPP_FMT "chap corrupted challenge "
3948 "<%s id=0x%x len=%d",
3950 sppp_auth_type_name(PPP_CHAP, h->type),
3951 h->ident, ntohs(h->len));
3952 sppp_print_bytes((u_char*) (h+1), len-4);
3960 SPP_FMT "chap input <%s id=0x%x len=%d name=",
3962 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
3964 sppp_print_string((char*) name, name_len);
3965 log(-1, " value-size=%d value=", value_len);
3966 sppp_print_bytes(value, value_len);
3970 /* Compute reply value. */
3972 MD5Update(&ctx, &h->ident, 1);
3973 MD5Update(&ctx, sp->myauth.secret,
3974 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
3975 MD5Update(&ctx, value, value_len);
3976 MD5Final(digest, &ctx);
3977 dsize = sizeof digest;
3979 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
3980 sizeof dsize, (const char *)&dsize,
3981 sizeof digest, digest,
3982 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3989 log(LOG_DEBUG, SPP_FMT "chap success",
3993 sppp_print_string((char*)(h + 1), len - 4);
4000 sp->pp_flags &= ~PP_NEEDAUTH;
4001 if (sp->myauth.proto == PPP_CHAP &&
4002 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4003 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
4005 * We are authenticator for CHAP but didn't
4006 * complete yet. Leave it to tlu to proceed
4013 sppp_phase_network(sp);
4018 log(LOG_INFO, SPP_FMT "chap failure",
4022 sppp_print_string((char*)(h + 1), len - 4);
4026 log(LOG_INFO, SPP_FMT "chap failure\n",
4028 /* await LCP shutdown by authenticator */
4031 /* response is my authproto */
4033 value = 1 + (u_char*)(h+1);
4034 value_len = value[-1];
4035 name = value + value_len;
4036 name_len = len - value_len - 5;
4040 SPP_FMT "chap corrupted response "
4041 "<%s id=0x%x len=%d",
4043 sppp_auth_type_name(PPP_CHAP, h->type),
4044 h->ident, ntohs(h->len));
4045 sppp_print_bytes((u_char*)(h+1), len-4);
4050 if (h->ident != sp->confid[IDX_CHAP]) {
4053 SPP_FMT "chap dropping response for old ID "
4054 "(got %d, expected %d)\n",
4056 h->ident, sp->confid[IDX_CHAP]);
4059 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4060 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4061 log(LOG_INFO, SPP_FMT "chap response, his name ",
4063 sppp_print_string(name, name_len);
4064 log(-1, " != expected ");
4065 sppp_print_string(sp->hisauth.name,
4066 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4070 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4071 "<%s id=0x%x len=%d name=",
4073 sppp_state_name(sp->state[IDX_CHAP]),
4074 sppp_auth_type_name(PPP_CHAP, h->type),
4075 h->ident, ntohs (h->len));
4076 sppp_print_string((char*)name, name_len);
4077 log(-1, " value-size=%d value=", value_len);
4078 sppp_print_bytes(value, value_len);
4081 if (value_len != AUTHKEYLEN) {
4084 SPP_FMT "chap bad hash value length: "
4085 "%d bytes, should be %d\n",
4086 SPP_ARGS(ifp), value_len,
4092 MD5Update(&ctx, &h->ident, 1);
4093 MD5Update(&ctx, sp->hisauth.secret,
4094 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4095 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4096 MD5Final(digest, &ctx);
4098 #define FAILMSG "Failed..."
4099 #define SUCCMSG "Welcome!"
4101 if (value_len != sizeof digest ||
4102 bcmp(digest, value, value_len) != 0) {
4103 /* action scn, tld */
4104 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4105 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4110 /* action sca, perhaps tlu */
4111 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4112 sp->state[IDX_CHAP] == STATE_OPENED)
4113 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4114 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4116 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4117 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4123 /* Unknown CHAP packet type -- ignore. */
4125 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4126 "<0x%x id=0x%xh len=%d",
4128 sppp_state_name(sp->state[IDX_CHAP]),
4129 h->type, h->ident, ntohs(h->len));
4130 sppp_print_bytes((u_char*)(h+1), len-4);
4139 sppp_chap_init(struct sppp *sp)
4141 /* Chap doesn't have STATE_INITIAL at all. */
4142 sp->state[IDX_CHAP] = STATE_CLOSED;
4143 sp->fail_counter[IDX_CHAP] = 0;
4144 sp->pp_seq[IDX_CHAP] = 0;
4145 sp->pp_rseq[IDX_CHAP] = 0;
4146 callout_init(&sp->timeout[IDX_CHAP]);
4150 sppp_chap_open(struct sppp *sp)
4152 if (sp->myauth.proto == PPP_CHAP &&
4153 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4154 /* we are authenticator for CHAP, start it */
4156 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4157 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4159 /* nothing to be done if we are peer, await a challenge */
4163 sppp_chap_close(struct sppp *sp)
4165 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4166 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4170 sppp_chap_TO(void *cookie)
4172 struct sppp *sp = (struct sppp *)cookie;
4178 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4180 sppp_state_name(sp->state[IDX_CHAP]),
4181 sp->rst_counter[IDX_CHAP]);
4183 if (--sp->rst_counter[IDX_CHAP] < 0)
4185 switch (sp->state[IDX_CHAP]) {
4186 case STATE_REQ_SENT:
4188 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4192 /* TO+ (or TO*) event */
4193 switch (sp->state[IDX_CHAP]) {
4196 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4198 case STATE_REQ_SENT:
4200 /* sppp_cp_change_state() will restart the timer */
4201 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4209 sppp_chap_tlu(struct sppp *sp)
4215 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4218 * Some broken CHAP implementations (Conware CoNet, firmware
4219 * 4.0.?) don't want to re-authenticate their CHAP once the
4220 * initial challenge-response exchange has taken place.
4221 * Provide for an option to avoid rechallenges.
4223 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4225 * Compute the re-challenge timeout. This will yield
4226 * a number between 300 and 810 seconds.
4228 i = 300 + ((unsigned)(krandom() & 0xff00) >> 7);
4229 callout_reset(&sp->timeout[IDX_CHAP], i * hz, chap.TO, sp);
4234 SPP_FMT "chap %s, ",
4236 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4237 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4238 log(-1, "next re-challenge in %d seconds\n", i);
4240 log(-1, "re-challenging suppressed\n");
4245 /* indicate to LCP that we need to be closed down */
4246 sp->lcp.protos |= (1 << IDX_CHAP);
4248 if (sp->pp_flags & PP_NEEDAUTH) {
4250 * Remote is authenticator, but his auth proto didn't
4251 * complete yet. Defer the transition to network
4261 * If we are already in phase network, we are done here. This
4262 * is the case if this is a dummy tlu event after a re-challenge.
4264 if (sp->pp_phase != PHASE_NETWORK)
4265 sppp_phase_network(sp);
4269 sppp_chap_tld(struct sppp *sp)
4274 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4275 callout_stop(&sp->timeout[IDX_CHAP]);
4276 sp->lcp.protos &= ~(1 << IDX_CHAP);
4282 sppp_chap_scr(struct sppp *sp)
4287 /* Compute random challenge. */
4288 ch = (u_long *)sp->myauth.challenge;
4289 read_random(&seed, sizeof seed);
4290 ch[0] = seed ^ krandom();
4291 ch[1] = seed ^ krandom();
4292 ch[2] = seed ^ krandom();
4293 ch[3] = seed ^ krandom();
4296 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4298 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4299 sizeof clen, (const char *)&clen,
4300 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4301 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4307 *--------------------------------------------------------------------------*
4309 * The PAP implementation. *
4311 *--------------------------------------------------------------------------*
4314 * For PAP, we need to keep a little state also if we are the peer, not the
4315 * authenticator. This is since we don't get a request to authenticate, but
4316 * have to repeatedly authenticate ourself until we got a response (or the
4317 * retry counter is expired).
4321 * Handle incoming PAP packets. */
4323 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4326 struct lcp_header *h;
4328 u_char *name, *passwd, mlen;
4329 int name_len, passwd_len;
4332 * Malicious input might leave this uninitialized, so
4333 * init to an impossible value.
4337 len = m->m_pkthdr.len;
4341 SPP_FMT "pap invalid packet length: %d bytes\n",
4342 SPP_ARGS(ifp), len);
4345 h = mtod (m, struct lcp_header*);
4346 if (len > ntohs (h->len))
4347 len = ntohs (h->len);
4349 /* PAP request is my authproto */
4351 name = 1 + (u_char*)(h+1);
4352 name_len = name[-1];
4353 passwd = name + name_len + 1;
4354 if (name_len > len - 6 ||
4355 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4357 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4358 "<%s id=0x%x len=%d",
4360 sppp_auth_type_name(PPP_PAP, h->type),
4361 h->ident, ntohs(h->len));
4362 sppp_print_bytes((u_char*)(h+1), len-4);
4368 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4369 "<%s id=0x%x len=%d name=",
4371 sppp_state_name(sp->state[IDX_PAP]),
4372 sppp_auth_type_name(PPP_PAP, h->type),
4373 h->ident, ntohs(h->len));
4374 sppp_print_string((char*)name, name_len);
4375 log(-1, " passwd=");
4376 sppp_print_string((char*)passwd, passwd_len);
4379 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4380 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4381 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4382 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4383 /* action scn, tld */
4384 mlen = sizeof(FAILMSG) - 1;
4385 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4386 sizeof mlen, (const char *)&mlen,
4387 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4392 /* action sca, perhaps tlu */
4393 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4394 sp->state[IDX_PAP] == STATE_OPENED) {
4395 mlen = sizeof(SUCCMSG) - 1;
4396 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4397 sizeof mlen, (const char *)&mlen,
4398 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4401 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4402 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4407 /* ack and nak are his authproto */
4409 callout_stop(&sp->pap_my_to);
4411 log(LOG_DEBUG, SPP_FMT "pap success",
4413 name = 1 + (u_char *)(h + 1);
4414 name_len = name[-1];
4415 if (len > 5 && name_len < len+4) {
4417 sppp_print_string(name, name_len);
4424 sp->pp_flags &= ~PP_NEEDAUTH;
4425 if (sp->myauth.proto == PPP_PAP &&
4426 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4427 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4429 * We are authenticator for PAP but didn't
4430 * complete yet. Leave it to tlu to proceed
4441 sppp_phase_network(sp);
4445 callout_stop(&sp->pap_my_to);
4447 log(LOG_INFO, SPP_FMT "pap failure",
4449 name = 1 + (u_char *)(h + 1);
4450 name_len = name[-1];
4451 if (len > 5 && name_len < len+4) {
4453 sppp_print_string(name, name_len);
4457 log(LOG_INFO, SPP_FMT "pap failure\n",
4459 /* await LCP shutdown by authenticator */
4463 /* Unknown PAP packet type -- ignore. */
4465 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4466 "<0x%x id=0x%x len=%d",
4468 h->type, h->ident, ntohs(h->len));
4469 sppp_print_bytes((u_char*)(h+1), len-4);
4478 sppp_pap_init(struct sppp *sp)
4480 /* PAP doesn't have STATE_INITIAL at all. */
4481 sp->state[IDX_PAP] = STATE_CLOSED;
4482 sp->fail_counter[IDX_PAP] = 0;
4483 sp->pp_seq[IDX_PAP] = 0;
4484 sp->pp_rseq[IDX_PAP] = 0;
4485 callout_init(&sp->timeout[IDX_PAP]);
4486 callout_init(&sp->pap_my_to);
4490 sppp_pap_open(struct sppp *sp)
4492 if (sp->hisauth.proto == PPP_PAP &&
4493 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4494 /* we are authenticator for PAP, start our timer */
4495 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4496 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4498 if (sp->myauth.proto == PPP_PAP) {
4499 /* we are peer, send a request, and start a timer */
4501 callout_reset(&sp->pap_my_to, sp->lcp.timeout,
4502 sppp_pap_my_TO, sp);
4507 sppp_pap_close(struct sppp *sp)
4509 if (sp->state[IDX_PAP] != STATE_CLOSED)
4510 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4514 * That's the timeout routine if we are authenticator. Since the
4515 * authenticator is basically passive in PAP, we can't do much here.
4518 sppp_pap_TO(void *cookie)
4520 struct sppp *sp = (struct sppp *)cookie;
4526 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4528 sppp_state_name(sp->state[IDX_PAP]),
4529 sp->rst_counter[IDX_PAP]);
4531 if (--sp->rst_counter[IDX_PAP] < 0)
4533 switch (sp->state[IDX_PAP]) {
4534 case STATE_REQ_SENT:
4536 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4540 /* TO+ event, not very much we could do */
4541 switch (sp->state[IDX_PAP]) {
4542 case STATE_REQ_SENT:
4543 /* sppp_cp_change_state() will restart the timer */
4544 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4552 * That's the timeout handler if we are peer. Since the peer is active,
4553 * we need to retransmit our PAP request since it is apparently lost.
4554 * XXX We should impose a max counter.
4557 sppp_pap_my_TO(void *cookie)
4559 struct sppp *sp = (struct sppp *)cookie;
4563 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4570 sppp_pap_tlu(struct sppp *sp)
4574 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4577 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4578 SPP_ARGS(ifp), pap.name);
4582 /* indicate to LCP that we need to be closed down */
4583 sp->lcp.protos |= (1 << IDX_PAP);
4585 if (sp->pp_flags & PP_NEEDAUTH) {
4587 * Remote is authenticator, but his auth proto didn't
4588 * complete yet. Defer the transition to network
4595 sppp_phase_network(sp);
4599 sppp_pap_tld(struct sppp *sp)
4604 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4605 callout_stop(&sp->timeout[IDX_PAP]);
4606 callout_stop(&sp->pap_my_to);
4607 sp->lcp.protos &= ~(1 << IDX_PAP);
4613 sppp_pap_scr(struct sppp *sp)
4615 u_char idlen, pwdlen;
4617 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4618 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4619 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4621 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4622 sizeof idlen, (const char *)&idlen,
4623 (size_t)idlen, sp->myauth.name,
4624 sizeof pwdlen, (const char *)&pwdlen,
4625 (size_t)pwdlen, sp->myauth.secret,
4630 * Random miscellaneous functions.
4634 * Send a PAP or CHAP proto packet.
4636 * Varadic function, each of the elements for the ellipsis is of type
4637 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4639 * NOTE: never declare variadic functions with types subject to type
4640 * promotion (i.e. u_char). This is asking for big trouble depending
4641 * on the architecture you are on...
4645 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4646 unsigned int type, unsigned int id,
4650 struct ppp_header *h;
4651 struct lcp_header *lh;
4657 struct ifaltq_subque *ifsq;
4660 MGETHDR (m, MB_DONTWAIT, MT_DATA);
4663 m->m_pkthdr.rcvif = 0;
4665 h = mtod (m, struct ppp_header*);
4666 h->address = PPP_ALLSTATIONS; /* broadcast address */
4667 h->control = PPP_UI; /* Unnumbered Info */
4668 h->protocol = htons(cp->proto);
4670 lh = (struct lcp_header*)(h + 1);
4673 p = (u_char*) (lh+1);
4678 while ((mlen = (unsigned int)__va_arg(ap, size_t)) != 0) {
4679 msg = __va_arg(ap, const char *);
4681 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4687 bcopy(msg, p, mlen);
4692 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4693 lh->len = htons (LCP_HEADER_LEN + len);
4696 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4697 SPP_ARGS(ifp), cp->name,
4698 sppp_auth_type_name(cp->proto, lh->type),
4699 lh->ident, ntohs(lh->len));
4700 sppp_print_bytes((u_char*) (lh+1), len);
4703 if (IF_QFULL (&sp->pp_cpq)) {
4704 IF_DROP (&sp->pp_fastq);
4706 IFNET_STAT_INC(ifp, oerrors, 1);
4708 IF_ENQUEUE (&sp->pp_cpq, m);
4709 ifsq = ifq_get_subq_default(&ifp->if_snd);
4710 if (!ifsq_is_oactive(ifsq))
4711 (*ifp->if_start) (ifp, ifsq);
4712 IFNET_STAT_INC(ifp, obytes, m->m_pkthdr.len + 3);
4716 * Send keepalive packets, every 10 seconds.
4719 sppp_keepalive(void *dummy)
4725 for (sp=spppq; sp; sp=sp->pp_next) {
4726 struct ifnet *ifp = &sp->pp_if;
4728 /* Keepalive mode disabled or channel down? */
4729 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4730 ! (ifp->if_flags & IFF_RUNNING))
4733 /* No keepalive in PPP mode if LCP not opened yet. */
4734 if (sp->pp_mode != IFF_CISCO &&
4735 sp->pp_phase < PHASE_AUTHENTICATE)
4738 if (sp->pp_alivecnt == MAXALIVECNT) {
4739 /* No keepalive packets got. Stop the interface. */
4740 kprintf (SPP_FMT "down\n", SPP_ARGS(ifp));
4742 IF_DRAIN(&sp->pp_cpq);
4743 if (sp->pp_mode != IFF_CISCO) {
4745 /* Shut down the PPP link. */
4747 /* Initiate negotiation. XXX */
4751 ifnet_serialize_all(ifp);
4752 if (sp->pp_alivecnt <= MAXALIVECNT)
4754 if (sp->pp_mode == IFF_CISCO)
4755 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4756 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4757 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4758 long nmagic = htonl (sp->lcp.magic);
4759 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4760 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4761 sp->lcp.echoid, 4, &nmagic);
4763 ifnet_deserialize_all(ifp);
4765 callout_reset(&keepalive_timeout, hz * 10, sppp_keepalive, NULL);
4770 * Get both IP addresses.
4773 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4775 struct ifnet *ifp = &sp->pp_if;
4776 struct ifaddr_container *ifac;
4778 struct sockaddr_in *si, *sm;
4784 * Pick the first AF_INET address from the list,
4785 * aliases don't make any sense on a p2p link anyway.
4788 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
4790 if (ifa->ifa_addr->sa_family == AF_INET) {
4791 si = (struct sockaddr_in *)ifa->ifa_addr;
4792 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4798 if (si && si->sin_addr.s_addr) {
4799 ssrc = si->sin_addr.s_addr;
4801 *srcmask = ntohl(sm->sin_addr.s_addr);
4804 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4805 if (si && si->sin_addr.s_addr)
4806 ddst = si->sin_addr.s_addr;
4809 if (dst) *dst = ntohl(ddst);
4810 if (src) *src = ntohl(ssrc);
4814 * Set my IP address. Must be called at splimp.
4817 sppp_set_ip_addr(struct sppp *sp, u_long src)
4820 struct ifaddr_container *ifac;
4821 struct ifaddr *ifa = NULL;
4822 struct sockaddr_in *si;
4823 struct in_ifaddr *ia;
4826 * Pick the first AF_INET address from the list,
4827 * aliases don't make any sense on a p2p link anyway.
4830 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
4832 if (ifa->ifa_addr->sa_family == AF_INET) {
4833 si = (struct sockaddr_in *)ifa->ifa_addr;
4839 if (ifac != NULL && si != NULL) {
4842 /* delete old route */
4843 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4846 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4847 SPP_ARGS(ifp), error);
4851 in_iahash_remove(ia);
4853 /* set new address */
4854 si->sin_addr.s_addr = htonl(src);
4855 in_iahash_insert(ia);
4858 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
4861 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
4862 SPP_ARGS(ifp), error);
4869 * Get both IPv6 addresses.
4872 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4873 struct in6_addr *srcmask)
4875 struct ifnet *ifp = &sp->pp_if;
4876 struct ifaddr_container *ifac;
4878 struct sockaddr_in6 *si, *sm;
4879 struct in6_addr ssrc, ddst;
4882 bzero(&ssrc, sizeof(ssrc));
4883 bzero(&ddst, sizeof(ddst));
4885 * Pick the first link-local AF_INET6 address from the list,
4886 * aliases don't make any sense on a p2p link anyway.
4889 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
4891 if (ifa->ifa_addr->sa_family == AF_INET6) {
4892 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4893 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4894 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
4899 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
4900 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
4902 bcopy(&sm->sin6_addr, srcmask,
4907 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
4908 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
4909 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
4913 bcopy(&ddst, dst, sizeof(*dst));
4915 bcopy(&ssrc, src, sizeof(*src));
4918 #ifdef IPV6CP_MYIFID_DYN
4920 * Generate random ifid.
4923 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
4929 * Set my IPv6 address. Must be called at splimp.
4932 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
4935 struct ifaddr_container *ifac;
4937 struct sockaddr_in6 *sin6;
4940 * Pick the first link-local AF_INET6 address from the list,
4941 * aliases don't make any sense on a p2p link anyway.
4945 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
4947 if (ifa->ifa_addr->sa_family == AF_INET6) {
4948 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
4949 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
4954 if (ifac != NULL && sin6 != NULL) {
4956 struct sockaddr_in6 new_sin6 = *sin6;
4958 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
4959 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
4960 if (debug && error) {
4961 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
4962 " failed, error=%d\n", SPP_ARGS(ifp), error);
4969 * Suggest a candidate address to be used by peer.
4972 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
4974 struct in6_addr myaddr;
4977 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
4979 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
4981 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
4982 myaddr.s6_addr[14] ^= 0xff;
4983 myaddr.s6_addr[15] ^= 0xff;
4985 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
4986 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
4989 bcopy(&myaddr, suggest, sizeof(myaddr));
4994 sppp_params(struct sppp *sp, u_long cmd, void *data)
4997 struct ifreq *ifr = (struct ifreq *)data;
4998 struct spppreq *spr;
5001 spr = kmalloc(sizeof(struct spppreq), M_TEMP, M_INTWAIT);
5004 * ifr->ifr_data is supposed to point to a struct spppreq.
5005 * Check the cmd word first before attempting to fetch all the
5008 if ((subcmd = fuword(ifr->ifr_data)) == -1) {
5013 if (copyin((caddr_t)ifr->ifr_data, spr, sizeof(struct spppreq)) != 0) {
5019 case (u_long)SPPPIOGDEFS:
5020 if (cmd != SIOCGIFGENERIC) {
5025 * We copy over the entire current state, but clean
5026 * out some of the stuff we don't wanna pass up.
5027 * Remember, SIOCGIFGENERIC is unprotected, and can be
5028 * called by any user. No need to ever get PAP or
5029 * CHAP secrets back to userland anyway.
5031 spr->defs.pp_phase = sp->pp_phase;
5032 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5033 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5034 spr->defs.lcp = sp->lcp;
5035 spr->defs.ipcp = sp->ipcp;
5036 spr->defs.ipv6cp = sp->ipv6cp;
5037 spr->defs.myauth = sp->myauth;
5038 spr->defs.hisauth = sp->hisauth;
5039 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5040 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5041 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5042 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5044 * Fixup the LCP timeout value to milliseconds so
5045 * spppcontrol doesn't need to bother about the value
5046 * of "hz". We do the reverse calculation below when
5049 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5050 rv = copyout(spr, (caddr_t)ifr->ifr_data,
5051 sizeof(struct spppreq));
5054 case (u_long)SPPPIOSDEFS:
5055 if (cmd != SIOCSIFGENERIC) {
5060 * We have a very specific idea of which fields we
5061 * allow being passed back from userland, so to not
5062 * clobber our current state. For one, we only allow
5063 * setting anything if LCP is in dead or establish
5064 * phase. Once the authentication negotiations
5065 * started, the authentication settings must not be
5066 * changed again. (The administrator can force an
5067 * ifconfig down in order to get LCP back into dead
5070 * Also, we only allow for authentication parameters to be
5073 * XXX Should allow to set or clear pp_flags.
5075 * Finally, if the respective authentication protocol to
5076 * be used is set differently than 0, but the secret is
5077 * passed as all zeros, we don't trash the existing secret.
5078 * This allows an administrator to change the system name
5079 * only without clobbering the secret (which he didn't get
5080 * back in a previous SPPPIOGDEFS call). However, the
5081 * secrets are cleared if the authentication protocol is
5083 if (sp->pp_phase != PHASE_DEAD &&
5084 sp->pp_phase != PHASE_ESTABLISH) {
5089 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5090 spr->defs.myauth.proto != PPP_CHAP) ||
5091 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5092 spr->defs.hisauth.proto != PPP_CHAP)) {
5097 if (spr->defs.myauth.proto == 0)
5098 /* resetting myauth */
5099 bzero(&sp->myauth, sizeof sp->myauth);
5101 /* setting/changing myauth */
5102 sp->myauth.proto = spr->defs.myauth.proto;
5103 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5104 if (spr->defs.myauth.secret[0] != '\0')
5105 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5108 if (spr->defs.hisauth.proto == 0)
5109 /* resetting hisauth */
5110 bzero(&sp->hisauth, sizeof sp->hisauth);
5112 /* setting/changing hisauth */
5113 sp->hisauth.proto = spr->defs.hisauth.proto;
5114 sp->hisauth.flags = spr->defs.hisauth.flags;
5115 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5116 if (spr->defs.hisauth.secret[0] != '\0')
5117 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5120 /* set LCP restart timer timeout */
5121 if (spr->defs.lcp.timeout != 0)
5122 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5123 /* set VJ enable and IPv6 disable flags */
5125 if (spr->defs.enable_vj)
5126 sp->confflags |= CONF_ENABLE_VJ;
5128 sp->confflags &= ~CONF_ENABLE_VJ;
5131 if (spr->defs.enable_ipv6)
5132 sp->confflags |= CONF_ENABLE_IPV6;
5134 sp->confflags &= ~CONF_ENABLE_IPV6;
5149 sppp_phase_network(struct sppp *sp)
5155 sp->pp_phase = PHASE_NETWORK;
5158 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5159 sppp_phase_name(sp->pp_phase));
5161 /* Notify NCPs now. */
5162 for (i = 0; i < IDX_COUNT; i++)
5163 if ((cps[i])->flags & CP_NCP)
5166 /* Send Up events to all NCPs. */
5167 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5168 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5171 /* if no NCP is starting, all this was in vain, close down */
5172 sppp_lcp_check_and_close(sp);
5177 sppp_cp_type_name(u_char type)
5179 static char buf[12];
5181 case CONF_REQ: return "conf-req";
5182 case CONF_ACK: return "conf-ack";
5183 case CONF_NAK: return "conf-nak";
5184 case CONF_REJ: return "conf-rej";
5185 case TERM_REQ: return "term-req";
5186 case TERM_ACK: return "term-ack";
5187 case CODE_REJ: return "code-rej";
5188 case PROTO_REJ: return "proto-rej";
5189 case ECHO_REQ: return "echo-req";
5190 case ECHO_REPLY: return "echo-reply";
5191 case DISC_REQ: return "discard-req";
5193 ksnprintf (buf, sizeof(buf), "cp/0x%x", type);
5198 sppp_auth_type_name(u_short proto, u_char type)
5200 static char buf[12];
5204 case CHAP_CHALLENGE: return "challenge";
5205 case CHAP_RESPONSE: return "response";
5206 case CHAP_SUCCESS: return "success";
5207 case CHAP_FAILURE: return "failure";
5211 case PAP_REQ: return "req";
5212 case PAP_ACK: return "ack";
5213 case PAP_NAK: return "nak";
5216 ksnprintf (buf, sizeof(buf), "auth/0x%x", type);
5221 sppp_lcp_opt_name(u_char opt)
5223 static char buf[12];
5225 case LCP_OPT_MRU: return "mru";
5226 case LCP_OPT_ASYNC_MAP: return "async-map";
5227 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5228 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5229 case LCP_OPT_MAGIC: return "magic";
5230 case LCP_OPT_PROTO_COMP: return "proto-comp";
5231 case LCP_OPT_ADDR_COMP: return "addr-comp";
5233 ksnprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5238 sppp_ipcp_opt_name(u_char opt)
5240 static char buf[12];
5242 case IPCP_OPT_ADDRESSES: return "addresses";
5243 case IPCP_OPT_COMPRESSION: return "compression";
5244 case IPCP_OPT_ADDRESS: return "address";
5246 ksnprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5252 sppp_ipv6cp_opt_name(u_char opt)
5254 static char buf[12];
5256 case IPV6CP_OPT_IFID: return "ifid";
5257 case IPV6CP_OPT_COMPRESSION: return "compression";
5259 ksprintf (buf, "0x%x", opt);
5265 sppp_state_name(int state)
5268 case STATE_INITIAL: return "initial";
5269 case STATE_STARTING: return "starting";
5270 case STATE_CLOSED: return "closed";
5271 case STATE_STOPPED: return "stopped";
5272 case STATE_CLOSING: return "closing";
5273 case STATE_STOPPING: return "stopping";
5274 case STATE_REQ_SENT: return "req-sent";
5275 case STATE_ACK_RCVD: return "ack-rcvd";
5276 case STATE_ACK_SENT: return "ack-sent";
5277 case STATE_OPENED: return "opened";
5283 sppp_phase_name(enum ppp_phase phase)
5286 case PHASE_DEAD: return "dead";
5287 case PHASE_ESTABLISH: return "establish";
5288 case PHASE_TERMINATE: return "terminate";
5289 case PHASE_AUTHENTICATE: return "authenticate";
5290 case PHASE_NETWORK: return "network";
5296 sppp_proto_name(u_short proto)
5298 static char buf[12];
5300 case PPP_LCP: return "lcp";
5301 case PPP_IPCP: return "ipcp";
5302 case PPP_PAP: return "pap";
5303 case PPP_CHAP: return "chap";
5304 case PPP_IPV6CP: return "ipv6cp";
5306 ksnprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5311 sppp_print_bytes(const u_char *p, u_short len)
5315 log(-1, " %s", hexncpy(p, len, hexstr, HEX_NCPYLEN(len), "-"));
5319 sppp_print_string(const char *p, u_short len)
5326 * Print only ASCII chars directly. RFC 1994 recommends
5327 * using only them, but we don't rely on it. */
5328 if (c < ' ' || c > '~')
5329 log(-1, "\\x%x", c);
5336 sppp_dotted_quad(u_long addr)
5339 ksprintf(s, "%d.%d.%d.%d",
5340 (int)((addr >> 24) & 0xff),
5341 (int)((addr >> 16) & 0xff),
5342 (int)((addr >> 8) & 0xff),
5343 (int)(addr & 0xff));
5348 sppp_strnlen(u_char *p, int max)
5352 for (len = 0; len < max && *p; ++p)
5357 /* a dummy, used to drop uninteresting events */
5359 sppp_null(struct sppp *unused)
5361 /* do just nothing */