2 * Synchronous PPP/Cisco/Frame Relay link level subroutines.
3 * Keepalive protocol implemented in both Cisco and PPP modes.
6 * Copyright (C) 1994-2000 Cronyx Engineering.
7 * Author: Serge Vakulenko, <vak@cronyx.ru>
9 * Heavily revamped to conform to RFC 1661.
10 * Copyright (C) 1997, 2001 Joerg Wunsch.
12 * This software is distributed with NO WARRANTIES, not even the implied
13 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 * Authors grant any other persons or organisations permission to use
16 * or modify this software as long as this message is kept with the software,
17 * all derivative works or modified versions.
19 * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997
24 #include <sys/param.h>
27 #include "opt_inet6.h"
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/sockio.h>
34 #include <sys/socket.h>
35 #include <sys/syslog.h>
36 #include <sys/random.h>
37 #include <sys/malloc.h>
39 #include <sys/vimage.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/ip.h>
58 #include <netinet/tcp.h>
59 #include <netinet/vinet.h>
63 #include <netinet6/scope6_var.h>
66 #include <netinet/if_ether.h>
69 #include <netipx/ipx.h>
70 #include <netipx/ipx_if.h>
73 #include <net/if_sppp.h>
75 #define IOCTL_CMD_T u_long
76 #define MAXALIVECNT 3 /* max. alive packets */
79 * Interface flags that can be set in an ifconfig command.
81 * Setting link0 will make the link passive, i.e. it will be marked
82 * as being administrative openable, but won't be opened to begin
83 * with. Incoming calls will be answered, or subsequent calls with
84 * -link1 will cause the administrative open of the LCP layer.
86 * Setting link1 will cause the link to auto-dial only as packets
89 * Setting IFF_DEBUG will syslog the option negotiation and state
90 * transitions at level kern.debug. Note: all logs consistently look
93 * <if-name><unit>: <proto-name> <additional info...>
95 * with <if-name><unit> being something like "bppp0", and <proto-name>
96 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
99 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
100 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
101 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
103 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
104 #define PPP_UI 0x03 /* Unnumbered Information */
105 #define PPP_IP 0x0021 /* Internet Protocol */
106 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
107 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
108 #define PPP_IPX 0x002b /* Novell IPX Protocol */
109 #define PPP_VJ_COMP 0x002d /* VJ compressed TCP/IP */
110 #define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */
111 #define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */
112 #define PPP_LCP 0xc021 /* Link Control Protocol */
113 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
114 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
115 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
116 #define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */
118 #define CONF_REQ 1 /* PPP configure request */
119 #define CONF_ACK 2 /* PPP configure acknowledge */
120 #define CONF_NAK 3 /* PPP configure negative ack */
121 #define CONF_REJ 4 /* PPP configure reject */
122 #define TERM_REQ 5 /* PPP terminate request */
123 #define TERM_ACK 6 /* PPP terminate acknowledge */
124 #define CODE_REJ 7 /* PPP code reject */
125 #define PROTO_REJ 8 /* PPP protocol reject */
126 #define ECHO_REQ 9 /* PPP echo request */
127 #define ECHO_REPLY 10 /* PPP echo reply */
128 #define DISC_REQ 11 /* PPP discard request */
130 #define LCP_OPT_MRU 1 /* maximum receive unit */
131 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
132 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
133 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
134 #define LCP_OPT_MAGIC 5 /* magic number */
135 #define LCP_OPT_RESERVED 6 /* reserved */
136 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
137 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
139 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
140 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
141 #define IPCP_OPT_ADDRESS 3 /* local IP address */
143 #define IPV6CP_OPT_IFID 1 /* interface identifier */
144 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
146 #define IPCP_COMP_VJ 0x2d /* Code for VJ compression */
148 #define PAP_REQ 1 /* PAP name/password request */
149 #define PAP_ACK 2 /* PAP acknowledge */
150 #define PAP_NAK 3 /* PAP fail */
152 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
153 #define CHAP_RESPONSE 2 /* CHAP challenge response */
154 #define CHAP_SUCCESS 3 /* CHAP response ok */
155 #define CHAP_FAILURE 4 /* CHAP response failed */
157 #define CHAP_MD5 5 /* hash algorithm - MD5 */
159 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
160 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
161 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
162 #define CISCO_ADDR_REQ 0 /* Cisco address request */
163 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
164 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
166 /* states are named and numbered according to RFC 1661 */
167 #define STATE_INITIAL 0
168 #define STATE_STARTING 1
169 #define STATE_CLOSED 2
170 #define STATE_STOPPED 3
171 #define STATE_CLOSING 4
172 #define STATE_STOPPING 5
173 #define STATE_REQ_SENT 6
174 #define STATE_ACK_RCVD 7
175 #define STATE_ACK_SENT 8
176 #define STATE_OPENED 9
178 MALLOC_DEFINE(M_SPPP, "sppp", "synchronous PPP interface internals");
185 #define PPP_HEADER_LEN sizeof (struct ppp_header)
192 #define LCP_HEADER_LEN sizeof (struct lcp_header)
194 struct cisco_packet {
202 #define CISCO_PACKET_LEN sizeof (struct cisco_packet)
205 * We follow the spelling and capitalization of RFC 1661 here, to make
206 * it easier comparing with the standard. Please refer to this RFC in
207 * case you can't make sense out of these abbreviation; it will also
208 * explain the semantics related to the various events and actions.
211 u_short proto; /* PPP control protocol number */
212 u_char protoidx; /* index into state table in struct sppp */
214 #define CP_LCP 0x01 /* this is the LCP */
215 #define CP_AUTH 0x02 /* this is an authentication protocol */
216 #define CP_NCP 0x04 /* this is a NCP */
217 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
218 const char *name; /* name of this control protocol */
220 void (*Up)(struct sppp *sp);
221 void (*Down)(struct sppp *sp);
222 void (*Open)(struct sppp *sp);
223 void (*Close)(struct sppp *sp);
224 void (*TO)(void *sp);
225 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
226 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
227 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
229 void (*tlu)(struct sppp *sp);
230 void (*tld)(struct sppp *sp);
231 void (*tls)(struct sppp *sp);
232 void (*tlf)(struct sppp *sp);
233 void (*scr)(struct sppp *sp);
236 #define SPP_FMT "%s: "
237 #define SPP_ARGS(ifp) (ifp)->if_xname
239 #define SPPP_LOCK(sp) \
241 if (!(SP2IFP(sp)->if_flags & IFF_NEEDSGIANT)) \
242 mtx_lock (&(sp)->mtx); \
244 #define SPPP_UNLOCK(sp) \
246 if (!(SP2IFP(sp)->if_flags & IFF_NEEDSGIANT)) \
247 mtx_unlock (&(sp)->mtx); \
250 #define SPPP_LOCK_ASSERT(sp) \
252 if (!(SP2IFP(sp)->if_flags & IFF_NEEDSGIANT)) \
253 mtx_assert (&(sp)->mtx, MA_OWNED); \
255 #define SPPP_LOCK_OWNED(sp) \
256 (!(SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) && \
257 mtx_owned (&sp->mtx))
261 * The following disgusting hack gets around the problem that IP TOS
262 * can't be set yet. We want to put "interactive" traffic on a high
263 * priority queue. To decide if traffic is interactive, we check that
264 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
266 * XXX is this really still necessary? - joerg -
268 static const u_short interactive_ports[8] = {
272 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
275 /* almost every function needs these */
277 struct ifnet *ifp = SP2IFP(sp); \
278 int debug = ifp->if_flags & IFF_DEBUG
280 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
281 struct sockaddr *dst, struct rtentry *rt);
283 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
284 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
286 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
288 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
289 u_char ident, u_short len, void *data);
290 /* static void sppp_cp_timeout(void *arg); */
291 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
293 static void sppp_auth_send(const struct cp *cp,
294 struct sppp *sp, unsigned int type, unsigned int id,
297 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
298 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
299 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
300 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
301 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
303 static void sppp_null(struct sppp *sp);
305 static void sppp_pp_up(struct sppp *sp);
306 static void sppp_pp_down(struct sppp *sp);
308 static void sppp_lcp_init(struct sppp *sp);
309 static void sppp_lcp_up(struct sppp *sp);
310 static void sppp_lcp_down(struct sppp *sp);
311 static void sppp_lcp_open(struct sppp *sp);
312 static void sppp_lcp_close(struct sppp *sp);
313 static void sppp_lcp_TO(void *sp);
314 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
315 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
316 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
317 static void sppp_lcp_tlu(struct sppp *sp);
318 static void sppp_lcp_tld(struct sppp *sp);
319 static void sppp_lcp_tls(struct sppp *sp);
320 static void sppp_lcp_tlf(struct sppp *sp);
321 static void sppp_lcp_scr(struct sppp *sp);
322 static void sppp_lcp_check_and_close(struct sppp *sp);
323 static int sppp_ncp_check(struct sppp *sp);
325 static void sppp_ipcp_init(struct sppp *sp);
326 static void sppp_ipcp_up(struct sppp *sp);
327 static void sppp_ipcp_down(struct sppp *sp);
328 static void sppp_ipcp_open(struct sppp *sp);
329 static void sppp_ipcp_close(struct sppp *sp);
330 static void sppp_ipcp_TO(void *sp);
331 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
332 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
333 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
334 static void sppp_ipcp_tlu(struct sppp *sp);
335 static void sppp_ipcp_tld(struct sppp *sp);
336 static void sppp_ipcp_tls(struct sppp *sp);
337 static void sppp_ipcp_tlf(struct sppp *sp);
338 static void sppp_ipcp_scr(struct sppp *sp);
340 static void sppp_ipv6cp_init(struct sppp *sp);
341 static void sppp_ipv6cp_up(struct sppp *sp);
342 static void sppp_ipv6cp_down(struct sppp *sp);
343 static void sppp_ipv6cp_open(struct sppp *sp);
344 static void sppp_ipv6cp_close(struct sppp *sp);
345 static void sppp_ipv6cp_TO(void *sp);
346 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
347 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
348 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
349 static void sppp_ipv6cp_tlu(struct sppp *sp);
350 static void sppp_ipv6cp_tld(struct sppp *sp);
351 static void sppp_ipv6cp_tls(struct sppp *sp);
352 static void sppp_ipv6cp_tlf(struct sppp *sp);
353 static void sppp_ipv6cp_scr(struct sppp *sp);
355 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
356 static void sppp_pap_init(struct sppp *sp);
357 static void sppp_pap_open(struct sppp *sp);
358 static void sppp_pap_close(struct sppp *sp);
359 static void sppp_pap_TO(void *sp);
360 static void sppp_pap_my_TO(void *sp);
361 static void sppp_pap_tlu(struct sppp *sp);
362 static void sppp_pap_tld(struct sppp *sp);
363 static void sppp_pap_scr(struct sppp *sp);
365 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
366 static void sppp_chap_init(struct sppp *sp);
367 static void sppp_chap_open(struct sppp *sp);
368 static void sppp_chap_close(struct sppp *sp);
369 static void sppp_chap_TO(void *sp);
370 static void sppp_chap_tlu(struct sppp *sp);
371 static void sppp_chap_tld(struct sppp *sp);
372 static void sppp_chap_scr(struct sppp *sp);
374 static const char *sppp_auth_type_name(u_short proto, u_char type);
375 static const char *sppp_cp_type_name(u_char type);
377 static const char *sppp_dotted_quad(u_long addr);
378 static const char *sppp_ipcp_opt_name(u_char opt);
381 static const char *sppp_ipv6cp_opt_name(u_char opt);
383 static const char *sppp_lcp_opt_name(u_char opt);
384 static const char *sppp_phase_name(enum ppp_phase phase);
385 static const char *sppp_proto_name(u_short proto);
386 static const char *sppp_state_name(int state);
387 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
388 static int sppp_strnlen(u_char *p, int max);
389 static void sppp_keepalive(void *dummy);
390 static void sppp_phase_network(struct sppp *sp);
391 static void sppp_print_bytes(const u_char *p, u_short len);
392 static void sppp_print_string(const char *p, u_short len);
393 static void sppp_qflush(struct ifqueue *ifq);
395 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
398 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
399 struct in6_addr *dst, struct in6_addr *srcmask);
400 #ifdef IPV6CP_MYIFID_DYN
401 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
402 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
404 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
407 /* if_start () wrapper */
408 static void sppp_ifstart (struct ifnet *ifp);
410 /* our control protocol descriptors */
411 static const struct cp lcp = {
412 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
413 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
414 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
415 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
419 static const struct cp ipcp = {
421 #ifdef INET /* don't run IPCP if there's no IPv4 support */
427 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
428 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
429 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
433 static const struct cp ipv6cp = {
434 PPP_IPV6CP, IDX_IPV6CP,
435 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
441 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
442 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
443 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
447 static const struct cp pap = {
448 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
449 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
450 sppp_pap_TO, 0, 0, 0,
451 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
455 static const struct cp chap = {
456 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
457 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
458 sppp_chap_TO, 0, 0, 0,
459 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
463 static const struct cp *cps[IDX_COUNT] = {
465 &ipcp, /* IDX_IPCP */
466 &ipv6cp, /* IDX_IPV6CP */
468 &chap, /* IDX_CHAP */
472 sppp_alloc(u_char type, struct ifnet *ifp)
476 sp = malloc(sizeof(struct sppp), M_SPPP, M_WAITOK | M_ZERO);
483 sppp_free(void *com, u_char type)
490 sppp_modevent(module_t mod, int type, void *unused)
495 * XXX: should probably be IFT_SPPP, but it's fairly
496 * harmless to allocate struct sppp's for non-sppp
500 if_register_com_alloc(IFT_PPP, sppp_alloc, sppp_free);
503 /* if_deregister_com_alloc(IFT_PPP); */
510 static moduledata_t spppmod = {
515 MODULE_VERSION(sppp, 1);
516 DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
519 * Exported functions, comprising our interface to the lower layer.
523 * Process the received packet.
526 sppp_input(struct ifnet *ifp, struct mbuf *m)
528 struct ppp_header *h;
530 struct sppp *sp = IFP2SP(ifp);
531 int debug, do_account = 0;
538 debug = ifp->if_flags & IFF_DEBUG;
540 if (ifp->if_flags & IFF_UP)
541 /* Count received bytes, add FCS and one flag */
542 ifp->if_ibytes += m->m_pkthdr.len + 3;
544 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
545 /* Too small packet, drop it. */
548 SPP_FMT "input packet is too small, %d bytes\n",
549 SPP_ARGS(ifp), m->m_pkthdr.len);
559 if (sp->pp_mode == PP_FR) {
560 sppp_fr_input (sp, m);
565 /* Get PPP header. */
566 h = mtod (m, struct ppp_header*);
567 m_adj (m, PPP_HEADER_LEN);
569 switch (h->address) {
570 case PPP_ALLSTATIONS:
571 if (h->control != PPP_UI)
573 if (sp->pp_mode == IFF_CISCO) {
576 SPP_FMT "PPP packet in Cisco mode "
577 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
579 h->address, h->control, ntohs(h->protocol));
582 switch (ntohs (h->protocol)) {
586 SPP_FMT "rejecting protocol "
587 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
589 h->address, h->control, ntohs(h->protocol));
590 if (sp->state[IDX_LCP] == STATE_OPENED)
591 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
592 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
597 sppp_cp_input(&lcp, sp, m);
602 if (sp->pp_phase >= PHASE_AUTHENTICATE)
603 sppp_pap_input(sp, m);
608 if (sp->pp_phase >= PHASE_AUTHENTICATE)
609 sppp_chap_input(sp, m);
615 if (sp->pp_phase == PHASE_NETWORK)
616 sppp_cp_input(&ipcp, sp, m);
621 if (sp->state[IDX_IPCP] == STATE_OPENED) {
627 if (sp->state[IDX_IPCP] == STATE_OPENED) {
629 sl_uncompress_tcp_core(mtod(m, u_char *),
633 &iphdr, &hlen)) <= 0) {
636 SPP_FMT "VJ uncompress failed on compressed packet\n",
642 * Trim the VJ header off the packet, and prepend
643 * the uncompressed IP header (which will usually
644 * end up in two chained mbufs since there's not
645 * enough leading space in the existing mbuf).
648 M_PREPEND(m, hlen, M_DONTWAIT);
653 bcopy(iphdr, mtod(m, u_char *), hlen);
659 if (sp->state[IDX_IPCP] == STATE_OPENED) {
660 if (sl_uncompress_tcp_core(mtod(m, u_char *),
662 TYPE_UNCOMPRESSED_TCP,
664 &iphdr, &hlen) != 0) {
667 SPP_FMT "VJ uncompress failed on uncompressed packet\n",
678 if (sp->pp_phase == PHASE_NETWORK)
679 sppp_cp_input(&ipv6cp, sp, m);
685 if (sp->state[IDX_IPV6CP] == STATE_OPENED)
692 /* IPX IPXCP not implemented yet */
693 if (sp->pp_phase == PHASE_NETWORK)
700 case CISCO_MULTICAST:
702 /* Don't check the control field here (RFC 1547). */
703 if (sp->pp_mode != IFF_CISCO) {
706 SPP_FMT "Cisco packet in PPP mode "
707 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
709 h->address, h->control, ntohs(h->protocol));
712 switch (ntohs (h->protocol)) {
716 case CISCO_KEEPALIVE:
717 sppp_cisco_input (sp, m);
741 default: /* Invalid PPP packet. */
745 SPP_FMT "invalid input packet "
746 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
748 h->address, h->control, ntohs(h->protocol));
752 if (! (ifp->if_flags & IFF_UP) || isr == -1)
757 if (netisr_queue(isr, m)) { /* (0) on success. */
759 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
766 * Do only account for network packets, not for control
767 * packets. This is used by some subsystems to detect
770 sp->pp_last_recv = time_uptime;
774 sppp_ifstart_sched(void *dummy)
776 struct sppp *sp = dummy;
778 sp->if_start(SP2IFP(sp));
781 /* if_start () wrapper function. We use it to schedule real if_start () for
782 * execution. We can't call it directly
785 sppp_ifstart(struct ifnet *ifp)
787 struct sppp *sp = IFP2SP(ifp);
789 if (SPPP_LOCK_OWNED(sp)) {
790 if (callout_pending(&sp->ifstart_callout))
792 callout_reset(&sp->ifstart_callout, 1, sppp_ifstart_sched,
800 * Enqueue transmit packet.
803 sppp_output(struct ifnet *ifp, struct mbuf *m,
804 struct sockaddr *dst, struct rtentry *rt)
806 struct sppp *sp = IFP2SP(ifp);
807 struct ppp_header *h;
808 struct ifqueue *ifq = NULL;
809 int s, error, rv = 0;
811 int ipproto = PPP_IP;
813 int debug = ifp->if_flags & IFF_DEBUG;
818 if (!(ifp->if_flags & IFF_UP) ||
819 (!(ifp->if_flags & IFF_AUTO) &&
820 !(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
830 if ((ifp->if_flags & IFF_AUTO) &&
831 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
836 * Hack to prevent the initialization-time generated
837 * IPv6 multicast packet to erroneously cause a
838 * dialout event in case IPv6 has been
839 * administratively disabled on that interface.
841 if (dst->sa_family == AF_INET6 &&
842 !(sp->confflags & CONF_ENABLE_IPV6))
846 * Interface is not yet running, but auto-dial. Need
847 * to start LCP for it.
849 ifp->if_drv_flags |= IFF_DRV_RUNNING;
856 if (dst->sa_family == AF_INET) {
857 /* XXX Check mbuf length here? */
858 struct ip *ip = mtod (m, struct ip*);
859 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
862 * When using dynamic local IP address assignment by using
863 * 0.0.0.0 as a local address, the first TCP session will
864 * not connect because the local TCP checksum is computed
865 * using 0.0.0.0 which will later become our real IP address
866 * so the TCP checksum computed at the remote end will
867 * become invalid. So we
868 * - don't let packets with src ip addr 0 thru
869 * - we flag TCP packets with src ip 0 as an error
872 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
877 if(ip->ip_p == IPPROTO_TCP)
878 return(EADDRNOTAVAIL);
884 * Put low delay, telnet, rlogin and ftp control packets
885 * in front of the queue or let ALTQ take care.
887 if (ALTQ_IS_ENABLED(&ifp->if_snd))
889 else if (_IF_QFULL(&sp->pp_fastq))
891 else if (ip->ip_tos & IPTOS_LOWDELAY)
893 else if (m->m_len < sizeof *ip + sizeof *tcp)
895 else if (ip->ip_p != IPPROTO_TCP)
897 else if (INTERACTIVE (ntohs (tcp->th_sport)))
899 else if (INTERACTIVE (ntohs (tcp->th_dport)))
903 * Do IP Header compression
905 if (sp->pp_mode != IFF_CISCO && sp->pp_mode != PP_FR &&
906 (sp->ipcp.flags & IPCP_VJ) && ip->ip_p == IPPROTO_TCP)
907 switch (sl_compress_tcp(m, ip, sp->pp_comp,
908 sp->ipcp.compress_cid)) {
909 case TYPE_COMPRESSED_TCP:
910 ipproto = PPP_VJ_COMP;
912 case TYPE_UNCOMPRESSED_TCP:
913 ipproto = PPP_VJ_UCOMP;
928 if (dst->sa_family == AF_INET6) {
929 /* XXX do something tricky here? */
933 if (sp->pp_mode == PP_FR) {
934 /* Add frame relay header. */
935 m = sppp_fr_header (sp, m, dst->sa_family);
942 * Prepend general data packet PPP header. For now, IP only.
944 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
947 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
955 * May want to check size of packet
956 * (albeit due to the implementation it's always enough)
958 h = mtod (m, struct ppp_header*);
959 if (sp->pp_mode == IFF_CISCO) {
960 h->address = CISCO_UNICAST; /* unicast address */
963 h->address = PPP_ALLSTATIONS; /* broadcast address */
964 h->control = PPP_UI; /* Unnumbered Info */
967 switch (dst->sa_family) {
969 case AF_INET: /* Internet Protocol */
970 if (sp->pp_mode == IFF_CISCO)
971 h->protocol = htons (ETHERTYPE_IP);
974 * Don't choke with an ENETDOWN early. It's
975 * possible that we just started dialing out,
976 * so don't drop the packet immediately. If
977 * we notice that we run out of buffer space
978 * below, we will however remember that we are
979 * not ready to carry IP packets, and return
980 * ENETDOWN, as opposed to ENOBUFS.
982 h->protocol = htons(ipproto);
983 if (sp->state[IDX_IPCP] != STATE_OPENED)
989 case AF_INET6: /* Internet Protocol */
990 if (sp->pp_mode == IFF_CISCO)
991 h->protocol = htons (ETHERTYPE_IPV6);
994 * Don't choke with an ENETDOWN early. It's
995 * possible that we just started dialing out,
996 * so don't drop the packet immediately. If
997 * we notice that we run out of buffer space
998 * below, we will however remember that we are
999 * not ready to carry IP packets, and return
1000 * ENETDOWN, as opposed to ENOBUFS.
1002 h->protocol = htons(PPP_IPV6);
1003 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
1009 case AF_IPX: /* Novell IPX Protocol */
1010 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
1011 ETHERTYPE_IPX : PPP_IPX);
1019 return (EAFNOSUPPORT);
1023 * Queue message on interface, and start output if interface
1028 error = !(IF_HANDOFF_ADJ(ifq, m, ifp, 3));
1030 IFQ_HANDOFF_ADJ(ifp, m, 3, error);
1035 return (rv? rv: ENOBUFS);
1040 * Unlike in sppp_input(), we can always bump the timestamp
1041 * here since sppp_output() is only called on behalf of
1042 * network-layer traffic; control-layer traffic is handled
1043 * by sppp_cp_send().
1045 sp->pp_last_sent = time_uptime;
1050 sppp_attach(struct ifnet *ifp)
1052 struct sppp *sp = IFP2SP(ifp);
1054 /* Initialize mtx lock */
1055 mtx_init(&sp->mtx, "sppp", MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
1057 /* Initialize keepalive handler. */
1058 callout_init(&sp->keepalive_callout,
1059 (ifp->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
1060 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
1063 ifp->if_mtu = PP_MTU;
1064 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
1065 ifp->if_output = sppp_output;
1067 sp->pp_flags = PP_KEEPALIVE;
1069 ifp->if_snd.ifq_maxlen = 32;
1070 sp->pp_fastq.ifq_maxlen = 32;
1071 sp->pp_cpq.ifq_maxlen = 20;
1073 sp->pp_alivecnt = 0;
1074 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
1075 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
1076 sp->pp_phase = PHASE_DEAD;
1077 sp->pp_up = sppp_pp_up;
1078 sp->pp_down = sppp_pp_down;
1079 if(!mtx_initialized(&sp->pp_cpq.ifq_mtx))
1080 mtx_init(&sp->pp_cpq.ifq_mtx, "sppp_cpq", NULL, MTX_DEF);
1081 if(!mtx_initialized(&sp->pp_fastq.ifq_mtx))
1082 mtx_init(&sp->pp_fastq.ifq_mtx, "sppp_fastq", NULL, MTX_DEF);
1083 sp->pp_last_recv = sp->pp_last_sent = time_uptime;
1086 sp->confflags |= CONF_ENABLE_VJ;
1089 sp->confflags |= CONF_ENABLE_IPV6;
1091 callout_init(&sp->ifstart_callout,
1092 (ifp->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
1093 sp->if_start = ifp->if_start;
1094 ifp->if_start = sppp_ifstart;
1095 sp->pp_comp = malloc(sizeof(struct slcompress), M_TEMP, M_WAITOK);
1096 sl_compress_init(sp->pp_comp, -1);
1099 sppp_ipv6cp_init(sp);
1105 sppp_detach(struct ifnet *ifp)
1107 struct sppp *sp = IFP2SP(ifp);
1110 KASSERT(mtx_initialized(&sp->mtx), ("sppp mutex is not initialized"));
1112 /* Stop keepalive handler. */
1113 if (!callout_drain(&sp->keepalive_callout))
1114 callout_stop(&sp->keepalive_callout);
1116 for (i = 0; i < IDX_COUNT; i++) {
1117 if (!callout_drain(&sp->ch[i]))
1118 callout_stop(&sp->ch[i]);
1120 if (!callout_drain(&sp->pap_my_to_ch))
1121 callout_stop(&sp->pap_my_to_ch);
1122 mtx_destroy(&sp->pp_cpq.ifq_mtx);
1123 mtx_destroy(&sp->pp_fastq.ifq_mtx);
1124 mtx_destroy(&sp->mtx);
1128 * Flush the interface output queue.
1131 sppp_flush_unlocked(struct ifnet *ifp)
1133 struct sppp *sp = IFP2SP(ifp);
1135 sppp_qflush ((struct ifqueue *)&SP2IFP(sp)->if_snd);
1136 sppp_qflush (&sp->pp_fastq);
1137 sppp_qflush (&sp->pp_cpq);
1141 sppp_flush(struct ifnet *ifp)
1143 struct sppp *sp = IFP2SP(ifp);
1146 sppp_flush_unlocked (ifp);
1151 * Check if the output queue is empty.
1154 sppp_isempty(struct ifnet *ifp)
1156 struct sppp *sp = IFP2SP(ifp);
1161 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
1162 !SP2IFP(sp)->if_snd.ifq_head;
1169 * Get next packet to send.
1172 sppp_dequeue(struct ifnet *ifp)
1174 struct sppp *sp = IFP2SP(ifp);
1181 * Process only the control protocol queue until we have at
1182 * least one NCP open.
1184 * Do always serve all three queues in Cisco mode.
1186 IF_DEQUEUE(&sp->pp_cpq, m);
1188 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO ||
1189 sp->pp_mode == PP_FR)) {
1190 IF_DEQUEUE(&sp->pp_fastq, m);
1192 IF_DEQUEUE (&SP2IFP(sp)->if_snd, m);
1200 * Pick the next packet, do not remove it from the queue.
1203 sppp_pick(struct ifnet *ifp)
1205 struct sppp *sp = IFP2SP(ifp);
1212 m = sp->pp_cpq.ifq_head;
1214 (sp->pp_phase == PHASE_NETWORK ||
1215 sp->pp_mode == IFF_CISCO ||
1216 sp->pp_mode == PP_FR))
1217 if ((m = sp->pp_fastq.ifq_head) == NULL)
1218 m = SP2IFP(sp)->if_snd.ifq_head;
1225 * Process an ioctl request. Called on low priority level.
1228 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1230 struct ifreq *ifr = (struct ifreq*) data;
1231 struct sppp *sp = IFP2SP(ifp);
1232 int s, rv, going_up, going_down, newmode;
1239 case SIOCSIFDSTADDR:
1243 /* set the interface "up" when assigning an IP address */
1244 ifp->if_flags |= IFF_UP;
1248 going_up = ifp->if_flags & IFF_UP &&
1249 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0;
1250 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1251 ifp->if_drv_flags & IFF_DRV_RUNNING;
1253 newmode = ifp->if_flags & IFF_PASSIVE;
1255 newmode = ifp->if_flags & IFF_AUTO;
1257 newmode = ifp->if_flags & IFF_CISCO;
1258 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1259 ifp->if_flags |= newmode;
1262 newmode = sp->pp_flags & PP_FR;
1264 if (newmode != sp->pp_mode) {
1267 going_up = ifp->if_drv_flags & IFF_DRV_RUNNING;
1271 if (sp->pp_mode != IFF_CISCO &&
1272 sp->pp_mode != PP_FR)
1274 else if (sp->pp_tlf)
1276 sppp_flush_unlocked(ifp);
1277 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1278 sp->pp_mode = newmode;
1282 if (sp->pp_mode != IFF_CISCO &&
1283 sp->pp_mode != PP_FR)
1285 sp->pp_mode = newmode;
1286 if (sp->pp_mode == 0) {
1287 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1290 if ((sp->pp_mode == IFF_CISCO) ||
1291 (sp->pp_mode == PP_FR)) {
1294 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1302 #define ifr_mtu ifr_metric
1305 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1307 ifp->if_mtu = ifr->ifr_mtu;
1312 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1314 ifp->if_mtu = *(short*)data;
1319 ifr->ifr_mtu = ifp->if_mtu;
1324 *(short*)data = ifp->if_mtu;
1331 case SIOCGIFGENERIC:
1332 case SIOCSIFGENERIC:
1333 rv = sppp_params(sp, cmd, data);
1345 * Cisco framing implementation.
1349 * Handle incoming Cisco keepalive protocol packets.
1352 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1355 struct cisco_packet *h;
1358 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1361 SPP_FMT "cisco invalid packet length: %d bytes\n",
1362 SPP_ARGS(ifp), m->m_pkthdr.len);
1365 h = mtod (m, struct cisco_packet*);
1368 SPP_FMT "cisco input: %d bytes "
1369 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1370 SPP_ARGS(ifp), m->m_pkthdr.len,
1371 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1372 (u_int)h->time0, (u_int)h->time1);
1373 switch (ntohl (h->type)) {
1376 log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n",
1377 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1379 case CISCO_ADDR_REPLY:
1380 /* Reply on address request, ignore */
1382 case CISCO_KEEPALIVE_REQ:
1383 sp->pp_alivecnt = 0;
1384 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1385 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1386 /* Local and remote sequence numbers are equal.
1387 * Probably, the line is in loopback mode. */
1388 if (sp->pp_loopcnt >= MAXALIVECNT) {
1389 printf (SPP_FMT "loopback\n",
1392 if (ifp->if_flags & IFF_UP) {
1394 sppp_qflush (&sp->pp_cpq);
1399 /* Generate new local sequence number */
1400 sp->pp_seq[IDX_LCP] = random();
1404 if (! (ifp->if_flags & IFF_UP) &&
1405 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1407 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1410 case CISCO_ADDR_REQ:
1411 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1413 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1419 * Send Cisco keepalive packet.
1422 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1425 struct ppp_header *h;
1426 struct cisco_packet *ch;
1430 getmicrouptime(&tv);
1432 MGETHDR (m, M_DONTWAIT, MT_DATA);
1435 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1436 m->m_pkthdr.rcvif = 0;
1438 h = mtod (m, struct ppp_header*);
1439 h->address = CISCO_MULTICAST;
1441 h->protocol = htons (CISCO_KEEPALIVE);
1443 ch = (struct cisco_packet*) (h + 1);
1444 ch->type = htonl (type);
1445 ch->par1 = htonl (par1);
1446 ch->par2 = htonl (par2);
1449 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1450 ch->time1 = htons ((u_short) tv.tv_sec);
1454 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1455 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1456 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1458 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1463 * PPP protocol implementation.
1467 * Send PPP control protocol packet.
1470 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1471 u_char ident, u_short len, void *data)
1474 struct ppp_header *h;
1475 struct lcp_header *lh;
1478 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1479 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1480 MGETHDR (m, M_DONTWAIT, MT_DATA);
1483 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1484 m->m_pkthdr.rcvif = 0;
1486 h = mtod (m, struct ppp_header*);
1487 h->address = PPP_ALLSTATIONS; /* broadcast address */
1488 h->control = PPP_UI; /* Unnumbered Info */
1489 h->protocol = htons (proto); /* Link Control Protocol */
1491 lh = (struct lcp_header*) (h + 1);
1494 lh->len = htons (LCP_HEADER_LEN + len);
1496 bcopy (data, lh+1, len);
1499 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1501 sppp_proto_name(proto),
1502 sppp_cp_type_name (lh->type), lh->ident,
1504 sppp_print_bytes ((u_char*) (lh+1), len);
1507 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1512 * Handle incoming PPP control protocol packets.
1515 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1518 struct lcp_header *h;
1519 int len = m->m_pkthdr.len;
1526 SPP_FMT "%s invalid packet length: %d bytes\n",
1527 SPP_ARGS(ifp), cp->name, len);
1530 h = mtod (m, struct lcp_header*);
1533 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1534 SPP_ARGS(ifp), cp->name,
1535 sppp_state_name(sp->state[cp->protoidx]),
1536 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1537 sppp_print_bytes ((u_char*) (h+1), len-4);
1540 if (len > ntohs (h->len))
1541 len = ntohs (h->len);
1542 p = (u_char *)(h + 1);
1547 log(-1, SPP_FMT "%s invalid conf-req length %d\n",
1548 SPP_ARGS(ifp), cp->name,
1553 /* handle states where RCR doesn't get a SCA/SCN */
1554 switch (sp->state[cp->protoidx]) {
1556 case STATE_STOPPING:
1559 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1563 rv = (cp->RCR)(sp, h, len);
1564 switch (sp->state[cp->protoidx]) {
1569 case STATE_ACK_SENT:
1570 case STATE_REQ_SENT:
1572 * sppp_cp_change_state() have the side effect of
1573 * restarting the timeouts. We want to avoid that
1574 * if the state don't change, otherwise we won't
1575 * ever timeout and resend a configuration request
1578 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1581 sppp_cp_change_state(cp, sp, rv?
1582 STATE_ACK_SENT: STATE_REQ_SENT);
1585 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1587 sppp_cp_change_state(cp, sp, rv?
1588 STATE_ACK_SENT: STATE_REQ_SENT);
1590 case STATE_ACK_RCVD:
1592 sppp_cp_change_state(cp, sp, STATE_OPENED);
1594 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1599 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1602 printf(SPP_FMT "%s illegal %s in state %s\n",
1603 SPP_ARGS(ifp), cp->name,
1604 sppp_cp_type_name(h->type),
1605 sppp_state_name(sp->state[cp->protoidx]));
1610 if (h->ident != sp->confid[cp->protoidx]) {
1612 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1613 SPP_ARGS(ifp), cp->name,
1614 h->ident, sp->confid[cp->protoidx]);
1618 switch (sp->state[cp->protoidx]) {
1621 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1624 case STATE_STOPPING:
1626 case STATE_REQ_SENT:
1627 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1628 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1633 case STATE_ACK_RCVD:
1635 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1637 case STATE_ACK_SENT:
1638 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1639 sppp_cp_change_state(cp, sp, STATE_OPENED);
1641 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1642 SPP_ARGS(ifp), cp->name);
1646 printf(SPP_FMT "%s illegal %s in state %s\n",
1647 SPP_ARGS(ifp), cp->name,
1648 sppp_cp_type_name(h->type),
1649 sppp_state_name(sp->state[cp->protoidx]));
1655 if (h->ident != sp->confid[cp->protoidx]) {
1657 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1658 SPP_ARGS(ifp), cp->name,
1659 h->ident, sp->confid[cp->protoidx]);
1663 if (h->type == CONF_NAK)
1664 (cp->RCN_nak)(sp, h, len);
1666 (cp->RCN_rej)(sp, h, len);
1668 switch (sp->state[cp->protoidx]) {
1671 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1673 case STATE_REQ_SENT:
1674 case STATE_ACK_SENT:
1675 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1677 * Slow things down a bit if we think we might be
1678 * in loopback. Depend on the timeout to send the
1679 * next configuration request.
1688 case STATE_ACK_RCVD:
1689 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1693 case STATE_STOPPING:
1696 printf(SPP_FMT "%s illegal %s in state %s\n",
1697 SPP_ARGS(ifp), cp->name,
1698 sppp_cp_type_name(h->type),
1699 sppp_state_name(sp->state[cp->protoidx]));
1705 switch (sp->state[cp->protoidx]) {
1706 case STATE_ACK_RCVD:
1707 case STATE_ACK_SENT:
1708 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1713 case STATE_STOPPING:
1714 case STATE_REQ_SENT:
1716 /* Send Terminate-Ack packet. */
1718 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1719 SPP_ARGS(ifp), cp->name);
1720 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1724 sp->rst_counter[cp->protoidx] = 0;
1725 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1729 printf(SPP_FMT "%s illegal %s in state %s\n",
1730 SPP_ARGS(ifp), cp->name,
1731 sppp_cp_type_name(h->type),
1732 sppp_state_name(sp->state[cp->protoidx]));
1737 switch (sp->state[cp->protoidx]) {
1740 case STATE_REQ_SENT:
1741 case STATE_ACK_SENT:
1744 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1747 case STATE_STOPPING:
1748 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1751 case STATE_ACK_RCVD:
1752 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1757 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1760 printf(SPP_FMT "%s illegal %s in state %s\n",
1761 SPP_ARGS(ifp), cp->name,
1762 sppp_cp_type_name(h->type),
1763 sppp_state_name(sp->state[cp->protoidx]));
1768 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1770 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1771 "danger will robinson\n",
1772 SPP_ARGS(ifp), cp->name,
1773 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1774 switch (sp->state[cp->protoidx]) {
1777 case STATE_REQ_SENT:
1778 case STATE_ACK_SENT:
1780 case STATE_STOPPING:
1783 case STATE_ACK_RCVD:
1784 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1787 printf(SPP_FMT "%s illegal %s in state %s\n",
1788 SPP_ARGS(ifp), cp->name,
1789 sppp_cp_type_name(h->type),
1790 sppp_state_name(sp->state[cp->protoidx]));
1797 const struct cp *upper;
1803 proto = ntohs(*((u_int16_t *)p));
1804 for (i = 0; i < IDX_COUNT; i++) {
1805 if (cps[i]->proto == proto) {
1813 if (catastrophic || debug)
1814 log(catastrophic? LOG_INFO: LOG_DEBUG,
1815 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1816 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1817 sppp_cp_type_name(h->type), proto,
1818 upper ? upper->name : "unknown",
1819 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1822 * if we got RXJ+ against conf-req, the peer does not implement
1823 * this particular protocol type. terminate the protocol.
1825 if (upper && !catastrophic) {
1826 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1832 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1833 switch (sp->state[cp->protoidx]) {
1836 case STATE_REQ_SENT:
1837 case STATE_ACK_SENT:
1839 case STATE_STOPPING:
1842 case STATE_ACK_RCVD:
1843 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1846 printf(SPP_FMT "%s illegal %s in state %s\n",
1847 SPP_ARGS(ifp), cp->name,
1848 sppp_cp_type_name(h->type),
1849 sppp_state_name(sp->state[cp->protoidx]));
1855 if (cp->proto != PPP_LCP)
1857 /* Discard the packet. */
1860 if (cp->proto != PPP_LCP)
1862 if (sp->state[cp->protoidx] != STATE_OPENED) {
1864 log(-1, SPP_FMT "lcp echo req but lcp closed\n",
1871 log(-1, SPP_FMT "invalid lcp echo request "
1872 "packet length: %d bytes\n",
1873 SPP_ARGS(ifp), len);
1876 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1877 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1878 /* Line loopback mode detected. */
1879 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1880 sp->pp_loopcnt = MAXALIVECNT * 5;
1882 sppp_qflush (&sp->pp_cpq);
1884 /* Shut down the PPP link. */
1890 *(long*)(h+1) = htonl (sp->lcp.magic);
1892 log(-1, SPP_FMT "got lcp echo req, sending echo rep\n",
1894 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1897 if (cp->proto != PPP_LCP)
1899 if (h->ident != sp->lcp.echoid) {
1905 log(-1, SPP_FMT "lcp invalid echo reply "
1906 "packet length: %d bytes\n",
1907 SPP_ARGS(ifp), len);
1911 log(-1, SPP_FMT "lcp got echo rep\n",
1913 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1914 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1915 sp->pp_alivecnt = 0;
1918 /* Unknown packet type -- send Code-Reject packet. */
1921 log(-1, SPP_FMT "%s send code-rej for 0x%x\n",
1922 SPP_ARGS(ifp), cp->name, h->type);
1923 sppp_cp_send(sp, cp->proto, CODE_REJ,
1924 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1931 * The generic part of all Up/Down/Open/Close/TO event handlers.
1932 * Basically, the state transition handling in the automaton.
1935 sppp_up_event(const struct cp *cp, struct sppp *sp)
1940 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1941 SPP_ARGS(ifp), cp->name,
1942 sppp_state_name(sp->state[cp->protoidx]));
1944 switch (sp->state[cp->protoidx]) {
1946 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1948 case STATE_STARTING:
1949 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1951 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1954 printf(SPP_FMT "%s illegal up in state %s\n",
1955 SPP_ARGS(ifp), cp->name,
1956 sppp_state_name(sp->state[cp->protoidx]));
1961 sppp_down_event(const struct cp *cp, struct sppp *sp)
1966 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1967 SPP_ARGS(ifp), cp->name,
1968 sppp_state_name(sp->state[cp->protoidx]));
1970 switch (sp->state[cp->protoidx]) {
1973 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1976 sppp_cp_change_state(cp, sp, STATE_STARTING);
1979 case STATE_STOPPING:
1980 case STATE_REQ_SENT:
1981 case STATE_ACK_RCVD:
1982 case STATE_ACK_SENT:
1983 sppp_cp_change_state(cp, sp, STATE_STARTING);
1987 sppp_cp_change_state(cp, sp, STATE_STARTING);
1990 printf(SPP_FMT "%s illegal down in state %s\n",
1991 SPP_ARGS(ifp), cp->name,
1992 sppp_state_name(sp->state[cp->protoidx]));
1998 sppp_open_event(const struct cp *cp, struct sppp *sp)
2003 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
2004 SPP_ARGS(ifp), cp->name,
2005 sppp_state_name(sp->state[cp->protoidx]));
2007 switch (sp->state[cp->protoidx]) {
2009 sppp_cp_change_state(cp, sp, STATE_STARTING);
2012 case STATE_STARTING:
2015 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
2017 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2021 * Try escaping stopped state. This seems to bite
2022 * people occasionally, in particular for IPCP,
2023 * presumably following previous IPCP negotiation
2024 * aborts. Somehow, we must have missed a Down event
2025 * which would have caused a transition into starting
2026 * state, so as a bandaid we force the Down event now.
2027 * This effectively implements (something like the)
2028 * `restart' option mentioned in the state transition
2029 * table of RFC 1661.
2031 sppp_cp_change_state(cp, sp, STATE_STARTING);
2034 case STATE_STOPPING:
2035 case STATE_REQ_SENT:
2036 case STATE_ACK_RCVD:
2037 case STATE_ACK_SENT:
2041 sppp_cp_change_state(cp, sp, STATE_STOPPING);
2048 sppp_close_event(const struct cp *cp, struct sppp *sp)
2053 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
2054 SPP_ARGS(ifp), cp->name,
2055 sppp_state_name(sp->state[cp->protoidx]));
2057 switch (sp->state[cp->protoidx]) {
2062 case STATE_STARTING:
2063 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2067 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2069 case STATE_STOPPING:
2070 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2075 case STATE_REQ_SENT:
2076 case STATE_ACK_RCVD:
2077 case STATE_ACK_SENT:
2078 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2079 sppp_cp_send(sp, cp->proto, TERM_REQ,
2080 ++sp->pp_seq[cp->protoidx], 0, 0);
2081 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2087 sppp_to_event(const struct cp *cp, struct sppp *sp)
2095 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
2096 SPP_ARGS(ifp), cp->name,
2097 sppp_state_name(sp->state[cp->protoidx]),
2098 sp->rst_counter[cp->protoidx]);
2100 if (--sp->rst_counter[cp->protoidx] < 0)
2102 switch (sp->state[cp->protoidx]) {
2104 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2107 case STATE_STOPPING:
2108 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2111 case STATE_REQ_SENT:
2112 case STATE_ACK_RCVD:
2113 case STATE_ACK_SENT:
2114 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2120 switch (sp->state[cp->protoidx]) {
2122 case STATE_STOPPING:
2123 sppp_cp_send(sp, cp->proto, TERM_REQ,
2124 ++sp->pp_seq[cp->protoidx], 0, 0);
2125 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2126 cp->TO, (void *)sp);
2128 case STATE_REQ_SENT:
2129 case STATE_ACK_RCVD:
2131 /* sppp_cp_change_state() will restart the timer */
2132 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2134 case STATE_ACK_SENT:
2136 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2137 cp->TO, (void *)sp);
2146 * Change the state of a control protocol in the state automaton.
2147 * Takes care of starting/stopping the restart timer.
2150 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2152 sp->state[cp->protoidx] = newstate;
2154 callout_stop (&sp->ch[cp->protoidx]);
2158 case STATE_STARTING:
2164 case STATE_STOPPING:
2165 case STATE_REQ_SENT:
2166 case STATE_ACK_RCVD:
2167 case STATE_ACK_SENT:
2168 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2169 cp->TO, (void *)sp);
2175 *--------------------------------------------------------------------------*
2177 * The LCP implementation. *
2179 *--------------------------------------------------------------------------*
2182 sppp_pp_up(struct sppp *sp)
2190 sppp_pp_down(struct sppp *sp)
2198 sppp_lcp_init(struct sppp *sp)
2200 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2202 sp->state[IDX_LCP] = STATE_INITIAL;
2203 sp->fail_counter[IDX_LCP] = 0;
2204 sp->pp_seq[IDX_LCP] = 0;
2205 sp->pp_rseq[IDX_LCP] = 0;
2207 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2209 /* Note that these values are relevant for all control protocols */
2210 sp->lcp.timeout = 3 * hz;
2211 sp->lcp.max_terminate = 2;
2212 sp->lcp.max_configure = 10;
2213 sp->lcp.max_failure = 10;
2214 callout_init(&sp->ch[IDX_LCP],
2215 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
2219 sppp_lcp_up(struct sppp *sp)
2223 sp->pp_alivecnt = 0;
2224 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2227 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2229 * If we are authenticator, negotiate LCP_AUTH
2231 if (sp->hisauth.proto != 0)
2232 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2234 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2235 sp->pp_flags &= ~PP_NEEDAUTH;
2237 * If this interface is passive or dial-on-demand, and we are
2238 * still in Initial state, it means we've got an incoming
2239 * call. Activate the interface.
2241 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2244 SPP_FMT "Up event", SPP_ARGS(ifp));
2245 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2246 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2248 log(-1, "(incoming call)\n");
2249 sp->pp_flags |= PP_CALLIN;
2253 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2254 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2255 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2259 sppp_up_event(&lcp, sp);
2263 sppp_lcp_down(struct sppp *sp)
2267 sppp_down_event(&lcp, sp);
2270 * If this is neither a dial-on-demand nor a passive
2271 * interface, simulate an ``ifconfig down'' action, so the
2272 * administrator can force a redial by another ``ifconfig
2273 * up''. XXX For leased line operation, should we immediately
2274 * try to reopen the connection here?
2276 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2278 SPP_FMT "Down event, taking interface down.\n",
2284 SPP_FMT "Down event (carrier loss)\n",
2286 sp->pp_flags &= ~PP_CALLIN;
2287 if (sp->state[IDX_LCP] != STATE_INITIAL)
2289 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2294 sppp_lcp_open(struct sppp *sp)
2296 sppp_open_event(&lcp, sp);
2300 sppp_lcp_close(struct sppp *sp)
2302 sppp_close_event(&lcp, sp);
2306 sppp_lcp_TO(void *cookie)
2308 sppp_to_event(&lcp, (struct sppp *)cookie);
2312 * Analyze a configure request. Return true if it was agreeable, and
2313 * caused action sca, false if it has been rejected or nak'ed, and
2314 * caused action scn. (The return value is used to make the state
2315 * transition decision in the state automaton.)
2318 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2321 u_char *buf, *r, *p;
2328 buf = r = malloc (len, M_TEMP, M_NOWAIT);
2333 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2336 /* pass 1: check for things that need to be rejected */
2338 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2339 len-=p[1], p+=p[1]) {
2341 log(-1, " %s ", sppp_lcp_opt_name(*p));
2345 if (len >= 6 && p[1] == 6)
2348 log(-1, "[invalid] ");
2350 case LCP_OPT_ASYNC_MAP:
2351 /* Async control character map. */
2352 if (len >= 6 && p[1] == 6)
2355 log(-1, "[invalid] ");
2358 /* Maximum receive unit. */
2359 if (len >= 4 && p[1] == 4)
2362 log(-1, "[invalid] ");
2364 case LCP_OPT_AUTH_PROTO:
2367 log(-1, "[invalid] ");
2370 authproto = (p[2] << 8) + p[3];
2371 if (authproto == PPP_CHAP && p[1] != 5) {
2373 log(-1, "[invalid chap len] ");
2376 if (sp->myauth.proto == 0) {
2377 /* we are not configured to do auth */
2379 log(-1, "[not configured] ");
2383 * Remote want us to authenticate, remember this,
2384 * so we stay in PHASE_AUTHENTICATE after LCP got
2387 sp->pp_flags |= PP_NEEDAUTH;
2390 /* Others not supported. */
2395 /* Add the option to rejected list. */
2402 log(-1, " send conf-rej\n");
2403 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2409 * pass 2: check for option values that are unacceptable and
2410 * thus require to be nak'ed.
2413 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2418 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2419 len-=p[1], p+=p[1]) {
2421 log(-1, " %s ", sppp_lcp_opt_name(*p));
2424 /* Magic number -- extract. */
2425 nmagic = (u_long)p[2] << 24 |
2426 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2427 if (nmagic != sp->lcp.magic) {
2430 log(-1, "0x%lx ", nmagic);
2433 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2434 log(-1, "[glitch] ");
2437 * We negate our magic here, and NAK it. If
2438 * we see it later in an NAK packet, we
2439 * suggest a new one.
2441 nmagic = ~sp->lcp.magic;
2443 p[2] = nmagic >> 24;
2444 p[3] = nmagic >> 16;
2449 case LCP_OPT_ASYNC_MAP:
2451 * Async control character map -- just ignore it.
2453 * Quote from RFC 1662, chapter 6:
2454 * To enable this functionality, synchronous PPP
2455 * implementations MUST always respond to the
2456 * Async-Control-Character-Map Configuration
2457 * Option with the LCP Configure-Ack. However,
2458 * acceptance of the Configuration Option does
2459 * not imply that the synchronous implementation
2460 * will do any ACCM mapping. Instead, all such
2461 * octet mapping will be performed by the
2462 * asynchronous-to-synchronous converter.
2468 * Maximum receive unit. Always agreeable,
2469 * but ignored by now.
2471 sp->lcp.their_mru = p[2] * 256 + p[3];
2473 log(-1, "%lu ", sp->lcp.their_mru);
2476 case LCP_OPT_AUTH_PROTO:
2477 authproto = (p[2] << 8) + p[3];
2478 if (sp->myauth.proto != authproto) {
2479 /* not agreed, nak */
2481 log(-1, "[mine %s != his %s] ",
2482 sppp_proto_name(sp->hisauth.proto),
2483 sppp_proto_name(authproto));
2484 p[2] = sp->myauth.proto >> 8;
2485 p[3] = sp->myauth.proto;
2488 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2490 log(-1, "[chap not MD5] ");
2496 /* Add the option to nak'ed list. */
2503 * Local and remote magics equal -- loopback?
2505 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2506 if (sp->pp_loopcnt == MAXALIVECNT*5)
2507 printf (SPP_FMT "loopback\n",
2509 if (ifp->if_flags & IFF_UP) {
2511 sppp_qflush(&sp->pp_cpq);
2516 } else if (!sp->pp_loopcnt &&
2517 ++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2519 log(-1, " max_failure (%d) exceeded, "
2521 sp->lcp.max_failure);
2522 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2525 log(-1, " send conf-nak\n");
2526 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2530 log(-1, " send conf-ack\n");
2531 sp->fail_counter[IDX_LCP] = 0;
2533 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2534 h->ident, origlen, h+1);
2542 * Analyze the LCP Configure-Reject option list, and adjust our
2546 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2552 buf = malloc (len, M_TEMP, M_NOWAIT);
2557 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2561 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2562 len -= p[1], p += p[1]) {
2564 log(-1, " %s ", sppp_lcp_opt_name(*p));
2567 /* Magic number -- can't use it, use 0 */
2568 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2573 * Should not be rejected anyway, since we only
2574 * negotiate a MRU if explicitly requested by
2577 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2579 case LCP_OPT_AUTH_PROTO:
2581 * Peer doesn't want to authenticate himself,
2582 * deny unless this is a dialout call, and
2583 * AUTHFLAG_NOCALLOUT is set.
2585 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2586 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2588 log(-1, "[don't insist on auth "
2590 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2594 log(-1, "[access denied]\n");
2606 * Analyze the LCP Configure-NAK option list, and adjust our
2610 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2617 buf = malloc (len, M_TEMP, M_NOWAIT);
2622 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2626 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2627 len -= p[1], p += p[1]) {
2629 log(-1, " %s ", sppp_lcp_opt_name(*p));
2632 /* Magic number -- renegotiate */
2633 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2634 len >= 6 && p[1] == 6) {
2635 magic = (u_long)p[2] << 24 |
2636 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2638 * If the remote magic is our negated one,
2639 * this looks like a loopback problem.
2640 * Suggest a new magic to make sure.
2642 if (magic == ~sp->lcp.magic) {
2644 log(-1, "magic glitch ");
2645 sp->lcp.magic = random();
2647 sp->lcp.magic = magic;
2649 log(-1, "%lu ", magic);
2655 * Peer wants to advise us to negotiate an MRU.
2656 * Agree on it if it's reasonable, or use
2657 * default otherwise.
2659 if (len >= 4 && p[1] == 4) {
2660 u_int mru = p[2] * 256 + p[3];
2662 log(-1, "%d ", mru);
2663 if (mru < PP_MTU || mru > PP_MAX_MRU)
2666 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2669 case LCP_OPT_AUTH_PROTO:
2671 * Peer doesn't like our authentication method,
2675 log(-1, "[access denied]\n");
2687 sppp_lcp_tlu(struct sppp *sp)
2694 if (! (ifp->if_flags & IFF_UP) &&
2695 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2696 /* Coming out of loopback mode. */
2698 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2701 for (i = 0; i < IDX_COUNT; i++)
2702 if ((cps[i])->flags & CP_QUAL)
2705 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2706 (sp->pp_flags & PP_NEEDAUTH) != 0)
2707 sp->pp_phase = PHASE_AUTHENTICATE;
2709 sp->pp_phase = PHASE_NETWORK;
2712 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2713 sppp_phase_name(sp->pp_phase));
2716 * Open all authentication protocols. This is even required
2717 * if we already proceeded to network phase, since it might be
2718 * that remote wants us to authenticate, so we might have to
2719 * send a PAP request. Undesired authentication protocols
2720 * don't do anything when they get an Open event.
2722 for (i = 0; i < IDX_COUNT; i++)
2723 if ((cps[i])->flags & CP_AUTH)
2726 if (sp->pp_phase == PHASE_NETWORK) {
2727 /* Notify all NCPs. */
2728 for (i = 0; i < IDX_COUNT; i++)
2729 if (((cps[i])->flags & CP_NCP) &&
2732 * Hack to administratively disable IPv6 if
2733 * not desired. Perhaps we should have another
2734 * flag for this, but right now, we can make
2735 * all struct cp's read/only.
2737 (cps[i] != &ipv6cp ||
2738 (sp->confflags & CONF_ENABLE_IPV6)))
2742 /* Send Up events to all started protos. */
2743 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2744 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2747 /* notify low-level driver of state change */
2749 sp->pp_chg(sp, (int)sp->pp_phase);
2751 if (sp->pp_phase == PHASE_NETWORK)
2752 /* if no NCP is starting, close down */
2753 sppp_lcp_check_and_close(sp);
2757 sppp_lcp_tld(struct sppp *sp)
2763 sp->pp_phase = PHASE_TERMINATE;
2766 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2767 sppp_phase_name(sp->pp_phase));
2770 * Take upper layers down. We send the Down event first and
2771 * the Close second to prevent the upper layers from sending
2772 * ``a flurry of terminate-request packets'', as the RFC
2775 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2776 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2778 (cps[i])->Close(sp);
2783 sppp_lcp_tls(struct sppp *sp)
2787 sp->pp_phase = PHASE_ESTABLISH;
2790 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2791 sppp_phase_name(sp->pp_phase));
2793 /* Notify lower layer if desired. */
2801 sppp_lcp_tlf(struct sppp *sp)
2805 sp->pp_phase = PHASE_DEAD;
2807 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2808 sppp_phase_name(sp->pp_phase));
2810 /* Notify lower layer if desired. */
2818 sppp_lcp_scr(struct sppp *sp)
2820 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2824 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2825 if (! sp->lcp.magic)
2826 sp->lcp.magic = random();
2827 opt[i++] = LCP_OPT_MAGIC;
2829 opt[i++] = sp->lcp.magic >> 24;
2830 opt[i++] = sp->lcp.magic >> 16;
2831 opt[i++] = sp->lcp.magic >> 8;
2832 opt[i++] = sp->lcp.magic;
2835 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2836 opt[i++] = LCP_OPT_MRU;
2838 opt[i++] = sp->lcp.mru >> 8;
2839 opt[i++] = sp->lcp.mru;
2842 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2843 authproto = sp->hisauth.proto;
2844 opt[i++] = LCP_OPT_AUTH_PROTO;
2845 opt[i++] = authproto == PPP_CHAP? 5: 4;
2846 opt[i++] = authproto >> 8;
2847 opt[i++] = authproto;
2848 if (authproto == PPP_CHAP)
2849 opt[i++] = CHAP_MD5;
2852 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2853 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2857 * Check the open NCPs, return true if at least one NCP is open.
2860 sppp_ncp_check(struct sppp *sp)
2864 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2865 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2871 * Re-check the open NCPs and see if we should terminate the link.
2872 * Called by the NCPs during their tlf action handling.
2875 sppp_lcp_check_and_close(struct sppp *sp)
2878 if (sp->pp_phase < PHASE_NETWORK)
2879 /* don't bother, we are already going down */
2882 if (sppp_ncp_check(sp))
2889 *--------------------------------------------------------------------------*
2891 * The IPCP implementation. *
2893 *--------------------------------------------------------------------------*
2898 sppp_ipcp_init(struct sppp *sp)
2902 sp->state[IDX_IPCP] = STATE_INITIAL;
2903 sp->fail_counter[IDX_IPCP] = 0;
2904 sp->pp_seq[IDX_IPCP] = 0;
2905 sp->pp_rseq[IDX_IPCP] = 0;
2906 callout_init(&sp->ch[IDX_IPCP],
2907 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
2911 sppp_ipcp_up(struct sppp *sp)
2913 sppp_up_event(&ipcp, sp);
2917 sppp_ipcp_down(struct sppp *sp)
2919 sppp_down_event(&ipcp, sp);
2923 sppp_ipcp_open(struct sppp *sp)
2926 u_long myaddr, hisaddr;
2928 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2929 IPCP_MYADDR_DYN | IPCP_VJ);
2932 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2934 * If we don't have his address, this probably means our
2935 * interface doesn't want to talk IP at all. (This could
2936 * be the case if somebody wants to speak only IPX, for
2937 * example.) Don't open IPCP in this case.
2939 if (hisaddr == 0L) {
2940 /* XXX this message should go away */
2942 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2948 * I don't have an assigned address, so i need to
2949 * negotiate my address.
2951 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2952 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2954 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2955 if (sp->confflags & CONF_ENABLE_VJ) {
2956 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2957 sp->ipcp.max_state = MAX_STATES - 1;
2958 sp->ipcp.compress_cid = 1;
2960 sppp_open_event(&ipcp, sp);
2964 sppp_ipcp_close(struct sppp *sp)
2966 sppp_close_event(&ipcp, sp);
2967 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2969 * My address was dynamic, clear it again.
2971 sppp_set_ip_addr(sp, 0L);
2975 sppp_ipcp_TO(void *cookie)
2977 sppp_to_event(&ipcp, (struct sppp *)cookie);
2981 * Analyze a configure request. Return true if it was agreeable, and
2982 * caused action sca, false if it has been rejected or nak'ed, and
2983 * caused action scn. (The return value is used to make the state
2984 * transition decision in the state automaton.)
2987 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2989 u_char *buf, *r, *p;
2990 struct ifnet *ifp = SP2IFP(sp);
2991 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2992 u_long hisaddr, desiredaddr;
2999 * Make sure to allocate a buf that can at least hold a
3000 * conf-nak with an `address' option. We might need it below.
3002 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3006 /* pass 1: see if we can recognize them */
3008 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
3011 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3012 len-=p[1], p+=p[1]) {
3014 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3016 case IPCP_OPT_COMPRESSION:
3017 if (!(sp->confflags & CONF_ENABLE_VJ)) {
3018 /* VJ compression administratively disabled */
3020 log(-1, "[locally disabled] ");
3024 * In theory, we should only conf-rej an
3025 * option that is shorter than RFC 1618
3026 * requires (i.e. < 4), and should conf-nak
3027 * anything else that is not VJ. However,
3028 * since our algorithm always uses the
3029 * original option to NAK it with new values,
3030 * things would become more complicated. In
3031 * pratice, the only commonly implemented IP
3032 * compression option is VJ anyway, so the
3033 * difference is negligible.
3035 if (len >= 6 && p[1] == 6) {
3037 * correctly formed compression option
3038 * that could be VJ compression
3044 "optlen %d [invalid/unsupported] ",
3047 case IPCP_OPT_ADDRESS:
3048 if (len >= 6 && p[1] == 6) {
3049 /* correctly formed address option */
3053 log(-1, "[invalid] ");
3056 /* Others not supported. */
3061 /* Add the option to rejected list. */
3068 log(-1, " send conf-rej\n");
3069 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
3074 /* pass 2: parse option values */
3075 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
3077 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
3081 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3082 len-=p[1], p+=p[1]) {
3084 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3086 case IPCP_OPT_COMPRESSION:
3087 desiredcomp = p[2] << 8 | p[3];
3088 /* We only support VJ */
3089 if (desiredcomp == IPCP_COMP_VJ) {
3091 log(-1, "VJ [ack] ");
3092 sp->ipcp.flags |= IPCP_VJ;
3093 sl_compress_init(sp->pp_comp, p[4]);
3094 sp->ipcp.max_state = p[4];
3095 sp->ipcp.compress_cid = p[5];
3100 "compproto %#04x [not supported] ",
3102 p[2] = IPCP_COMP_VJ >> 8;
3103 p[3] = IPCP_COMP_VJ;
3104 p[4] = sp->ipcp.max_state;
3105 p[5] = sp->ipcp.compress_cid;
3107 case IPCP_OPT_ADDRESS:
3108 /* This is the address he wants in his end */
3109 desiredaddr = p[2] << 24 | p[3] << 16 |
3111 if (desiredaddr == hisaddr ||
3112 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3114 * Peer's address is same as our value,
3115 * or we have set it to 0.0.0.* to
3116 * indicate that we do not really care,
3117 * this is agreeable. Gonna conf-ack
3121 log(-1, "%s [ack] ",
3122 sppp_dotted_quad(hisaddr));
3123 /* record that we've seen it already */
3124 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3128 * The address wasn't agreeable. This is either
3129 * he sent us 0.0.0.0, asking to assign him an
3130 * address, or he send us another address not
3131 * matching our value. Either case, we gonna
3132 * conf-nak it with our value.
3133 * XXX: we should "rej" if hisaddr == 0
3136 if (desiredaddr == 0)
3137 log(-1, "[addr requested] ");
3139 log(-1, "%s [not agreed] ",
3140 sppp_dotted_quad(desiredaddr));
3143 p[2] = hisaddr >> 24;
3144 p[3] = hisaddr >> 16;
3145 p[4] = hisaddr >> 8;
3149 /* Add the option to nak'ed list. */
3156 * If we are about to conf-ack the request, but haven't seen
3157 * his address so far, gonna conf-nak it instead, with the
3158 * `address' option present and our idea of his address being
3159 * filled in there, to request negotiation of both addresses.
3161 * XXX This can result in an endless req - nak loop if peer
3162 * doesn't want to send us his address. Q: What should we do
3163 * about it? XXX A: implement the max-failure counter.
3165 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3166 buf[0] = IPCP_OPT_ADDRESS;
3168 buf[2] = hisaddr >> 24;
3169 buf[3] = hisaddr >> 16;
3170 buf[4] = hisaddr >> 8;
3174 log(-1, "still need hisaddr ");
3179 log(-1, " send conf-nak\n");
3180 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3183 log(-1, " send conf-ack\n");
3184 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3185 h->ident, origlen, h+1);
3193 * Analyze the IPCP Configure-Reject option list, and adjust our
3197 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3200 struct ifnet *ifp = SP2IFP(sp);
3201 int debug = ifp->if_flags & IFF_DEBUG;
3204 buf = malloc (len, M_TEMP, M_NOWAIT);
3209 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3213 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3214 len -= p[1], p += p[1]) {
3216 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3218 case IPCP_OPT_COMPRESSION:
3219 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3221 case IPCP_OPT_ADDRESS:
3223 * Peer doesn't grok address option. This is
3224 * bad. XXX Should we better give up here?
3225 * XXX We could try old "addresses" option...
3227 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3238 * Analyze the IPCP Configure-NAK option list, and adjust our
3242 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3245 struct ifnet *ifp = SP2IFP(sp);
3246 int debug = ifp->if_flags & IFF_DEBUG;
3251 buf = malloc (len, M_TEMP, M_NOWAIT);
3256 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3260 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3261 len -= p[1], p += p[1]) {
3263 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3265 case IPCP_OPT_COMPRESSION:
3266 if (len >= 6 && p[1] == 6) {
3267 desiredcomp = p[2] << 8 | p[3];
3269 log(-1, "[wantcomp %#04x] ",
3271 if (desiredcomp == IPCP_COMP_VJ) {
3272 sl_compress_init(sp->pp_comp, p[4]);
3273 sp->ipcp.max_state = p[4];
3274 sp->ipcp.compress_cid = p[5];
3276 log(-1, "[agree] ");
3279 ~(1 << IPCP_OPT_COMPRESSION);
3282 case IPCP_OPT_ADDRESS:
3284 * Peer doesn't like our local IP address. See
3285 * if we can do something for him. We'll drop
3286 * him our address then.
3288 if (len >= 6 && p[1] == 6) {
3289 wantaddr = p[2] << 24 | p[3] << 16 |
3291 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3293 log(-1, "[wantaddr %s] ",
3294 sppp_dotted_quad(wantaddr));
3296 * When doing dynamic address assignment,
3297 * we accept his offer. Otherwise, we
3298 * ignore it and thus continue to negotiate
3299 * our already existing value.
3300 * XXX: Bogus, if he said no once, he'll
3301 * just say no again, might as well die.
3303 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3304 sppp_set_ip_addr(sp, wantaddr);
3306 log(-1, "[agree] ");
3307 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3320 sppp_ipcp_tlu(struct sppp *sp)
3322 /* we are up - notify isdn daemon */
3328 sppp_ipcp_tld(struct sppp *sp)
3333 sppp_ipcp_tls(struct sppp *sp)
3335 /* indicate to LCP that it must stay alive */
3336 sp->lcp.protos |= (1 << IDX_IPCP);
3340 sppp_ipcp_tlf(struct sppp *sp)
3342 /* we no longer need LCP */
3343 sp->lcp.protos &= ~(1 << IDX_IPCP);
3344 sppp_lcp_check_and_close(sp);
3348 sppp_ipcp_scr(struct sppp *sp)
3350 char opt[6 /* compression */ + 6 /* address */];
3354 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3355 opt[i++] = IPCP_OPT_COMPRESSION;
3357 opt[i++] = IPCP_COMP_VJ >> 8;
3358 opt[i++] = IPCP_COMP_VJ;
3359 opt[i++] = sp->ipcp.max_state;
3360 opt[i++] = sp->ipcp.compress_cid;
3362 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3363 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3364 opt[i++] = IPCP_OPT_ADDRESS;
3366 opt[i++] = ouraddr >> 24;
3367 opt[i++] = ouraddr >> 16;
3368 opt[i++] = ouraddr >> 8;
3372 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3373 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3377 sppp_ipcp_init(struct sppp *sp)
3382 sppp_ipcp_up(struct sppp *sp)
3387 sppp_ipcp_down(struct sppp *sp)
3392 sppp_ipcp_open(struct sppp *sp)
3397 sppp_ipcp_close(struct sppp *sp)
3402 sppp_ipcp_TO(void *cookie)
3407 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3413 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3418 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3423 sppp_ipcp_tlu(struct sppp *sp)
3428 sppp_ipcp_tld(struct sppp *sp)
3433 sppp_ipcp_tls(struct sppp *sp)
3438 sppp_ipcp_tlf(struct sppp *sp)
3443 sppp_ipcp_scr(struct sppp *sp)
3449 *--------------------------------------------------------------------------*
3451 * The IPv6CP implementation. *
3453 *--------------------------------------------------------------------------*
3458 sppp_ipv6cp_init(struct sppp *sp)
3460 sp->ipv6cp.opts = 0;
3461 sp->ipv6cp.flags = 0;
3462 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3463 sp->fail_counter[IDX_IPV6CP] = 0;
3464 sp->pp_seq[IDX_IPV6CP] = 0;
3465 sp->pp_rseq[IDX_IPV6CP] = 0;
3466 callout_init(&sp->ch[IDX_IPV6CP],
3467 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
3471 sppp_ipv6cp_up(struct sppp *sp)
3473 sppp_up_event(&ipv6cp, sp);
3477 sppp_ipv6cp_down(struct sppp *sp)
3479 sppp_down_event(&ipv6cp, sp);
3483 sppp_ipv6cp_open(struct sppp *sp)
3486 struct in6_addr myaddr, hisaddr;
3488 #ifdef IPV6CP_MYIFID_DYN
3489 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3491 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3494 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3496 * If we don't have our address, this probably means our
3497 * interface doesn't want to talk IPv6 at all. (This could
3498 * be the case if somebody wants to speak only IPX, for
3499 * example.) Don't open IPv6CP in this case.
3501 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3502 /* XXX this message should go away */
3504 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3509 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3510 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3511 sppp_open_event(&ipv6cp, sp);
3515 sppp_ipv6cp_close(struct sppp *sp)
3517 sppp_close_event(&ipv6cp, sp);
3521 sppp_ipv6cp_TO(void *cookie)
3523 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3527 * Analyze a configure request. Return true if it was agreeable, and
3528 * caused action sca, false if it has been rejected or nak'ed, and
3529 * caused action scn. (The return value is used to make the state
3530 * transition decision in the state automaton.)
3533 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3535 u_char *buf, *r, *p;
3536 struct ifnet *ifp = SP2IFP(sp);
3537 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3538 struct in6_addr myaddr, desiredaddr, suggestaddr;
3541 int collision, nohisaddr;
3542 char ip6buf[INET6_ADDRSTRLEN];
3547 * Make sure to allocate a buf that can at least hold a
3548 * conf-nak with an `address' option. We might need it below.
3550 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3554 /* pass 1: see if we can recognize them */
3556 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3560 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3561 len-=p[1], p+=p[1]) {
3563 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3565 case IPV6CP_OPT_IFID:
3566 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3567 /* correctly formed address option */
3572 log(-1, " [invalid]");
3575 case IPV6CP_OPT_COMPRESSION:
3576 if (len >= 4 && p[1] >= 4) {
3577 /* correctly formed compress option */
3581 log(-1, " [invalid]");
3585 /* Others not supported. */
3590 /* Add the option to rejected list. */
3597 log(-1, " send conf-rej\n");
3598 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3603 /* pass 2: parse option values */
3604 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3606 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3611 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3612 len-=p[1], p+=p[1]) {
3614 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3617 case IPV6CP_OPT_COMPRESSION:
3620 case IPV6CP_OPT_IFID:
3621 bzero(&desiredaddr, sizeof(desiredaddr));
3622 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3623 collision = (bcmp(&desiredaddr.s6_addr[8],
3624 &myaddr.s6_addr[8], 8) == 0);
3625 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3627 desiredaddr.s6_addr16[0] = htons(0xfe80);
3628 (void)in6_setscope(&desiredaddr, SP2IFP(sp), NULL);
3630 if (!collision && !nohisaddr) {
3631 /* no collision, hisaddr known - Conf-Ack */
3636 ip6_sprintf(ip6buf, &desiredaddr),
3637 sppp_cp_type_name(type));
3642 bzero(&suggestaddr, sizeof(&suggestaddr));
3643 if (collision && nohisaddr) {
3644 /* collision, hisaddr unknown - Conf-Rej */
3649 * - no collision, hisaddr unknown, or
3650 * - collision, hisaddr known
3651 * Conf-Nak, suggest hisaddr
3654 sppp_suggest_ip6_addr(sp, &suggestaddr);
3655 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3659 ip6_sprintf(ip6buf, &desiredaddr),
3660 sppp_cp_type_name(type));
3663 /* Add the option to nak'ed list. */
3669 if (rlen == 0 && type == CONF_ACK) {
3671 log(-1, " send %s\n", sppp_cp_type_name(type));
3672 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3675 if (type == CONF_ACK)
3676 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3680 log(-1, " send %s suggest %s\n",
3681 sppp_cp_type_name(type),
3682 ip6_sprintf(ip6buf, &suggestaddr));
3684 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3693 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3697 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3700 struct ifnet *ifp = SP2IFP(sp);
3701 int debug = ifp->if_flags & IFF_DEBUG;
3704 buf = malloc (len, M_TEMP, M_NOWAIT);
3709 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3713 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3714 len -= p[1], p += p[1]) {
3716 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3718 case IPV6CP_OPT_IFID:
3720 * Peer doesn't grok address option. This is
3721 * bad. XXX Should we better give up here?
3723 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3726 case IPV6CP_OPT_COMPRESS:
3727 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3739 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3743 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3746 struct ifnet *ifp = SP2IFP(sp);
3747 int debug = ifp->if_flags & IFF_DEBUG;
3748 struct in6_addr suggestaddr;
3749 char ip6buf[INET6_ADDRSTRLEN];
3752 buf = malloc (len, M_TEMP, M_NOWAIT);
3757 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3761 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3762 len -= p[1], p += p[1]) {
3764 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3766 case IPV6CP_OPT_IFID:
3768 * Peer doesn't like our local ifid. See
3769 * if we can do something for him. We'll drop
3770 * him our address then.
3772 if (len < 10 || p[1] != 10)
3774 bzero(&suggestaddr, sizeof(suggestaddr));
3775 suggestaddr.s6_addr16[0] = htons(0xfe80);
3776 (void)in6_setscope(&suggestaddr, SP2IFP(sp), NULL);
3777 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3779 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3781 log(-1, " [suggestaddr %s]",
3782 ip6_sprintf(ip6buf, &suggestaddr));
3783 #ifdef IPV6CP_MYIFID_DYN
3785 * When doing dynamic address assignment,
3786 * we accept his offer.
3788 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3789 struct in6_addr lastsuggest;
3791 * If <suggested myaddr from peer> equals to
3792 * <hisaddr we have suggested last time>,
3793 * we have a collision. generate new random
3796 sppp_suggest_ip6_addr(&lastsuggest);
3797 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3800 log(-1, " [random]");
3801 sppp_gen_ip6_addr(sp, &suggestaddr);
3803 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3805 log(-1, " [agree]");
3806 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3810 * Since we do not do dynamic address assignment,
3811 * we ignore it and thus continue to negotiate
3812 * our already existing value. This can possibly
3813 * go into infinite request-reject loop.
3815 * This is not likely because we normally use
3816 * ifid based on MAC-address.
3817 * If you have no ethernet card on the node, too bad.
3818 * XXX should we use fail_counter?
3823 case IPV6CP_OPT_COMPRESS:
3825 * Peer wants different compression parameters.
3837 sppp_ipv6cp_tlu(struct sppp *sp)
3839 /* we are up - notify isdn daemon */
3845 sppp_ipv6cp_tld(struct sppp *sp)
3850 sppp_ipv6cp_tls(struct sppp *sp)
3852 /* indicate to LCP that it must stay alive */
3853 sp->lcp.protos |= (1 << IDX_IPV6CP);
3857 sppp_ipv6cp_tlf(struct sppp *sp)
3860 #if 0 /* need #if 0 to close IPv6CP properly */
3861 /* we no longer need LCP */
3862 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3863 sppp_lcp_check_and_close(sp);
3868 sppp_ipv6cp_scr(struct sppp *sp)
3870 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3871 struct in6_addr ouraddr;
3874 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3875 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3876 opt[i++] = IPV6CP_OPT_IFID;
3878 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3883 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3884 opt[i++] = IPV6CP_OPT_COMPRESSION;
3886 opt[i++] = 0; /* TBD */
3887 opt[i++] = 0; /* TBD */
3888 /* variable length data may follow */
3892 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3893 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3896 static void sppp_ipv6cp_init(struct sppp *sp)
3900 static void sppp_ipv6cp_up(struct sppp *sp)
3904 static void sppp_ipv6cp_down(struct sppp *sp)
3909 static void sppp_ipv6cp_open(struct sppp *sp)
3913 static void sppp_ipv6cp_close(struct sppp *sp)
3917 static void sppp_ipv6cp_TO(void *sp)
3921 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3926 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3930 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3934 static void sppp_ipv6cp_tlu(struct sppp *sp)
3938 static void sppp_ipv6cp_tld(struct sppp *sp)
3942 static void sppp_ipv6cp_tls(struct sppp *sp)
3946 static void sppp_ipv6cp_tlf(struct sppp *sp)
3950 static void sppp_ipv6cp_scr(struct sppp *sp)
3956 *--------------------------------------------------------------------------*
3958 * The CHAP implementation. *
3960 *--------------------------------------------------------------------------*
3964 * The authentication protocols don't employ a full-fledged state machine as
3965 * the control protocols do, since they do have Open and Close events, but
3966 * not Up and Down, nor are they explicitly terminated. Also, use of the
3967 * authentication protocols may be different in both directions (this makes
3968 * sense, think of a machine that never accepts incoming calls but only
3969 * calls out, it doesn't require the called party to authenticate itself).
3971 * Our state machine for the local authentication protocol (we are requesting
3972 * the peer to authenticate) looks like:
3975 * +--------------------------------------------+
3977 * +--------+ Close +---------+ RCA+
3978 * | |<----------------------------------| |------+
3979 * +--->| Closed | TO* | Opened | sca |
3980 * | | |-----+ +-------| |<-----+
3981 * | +--------+ irc | | +---------+
3987 * | | +------->+ | |
3989 * | +--------+ V | |
3990 * | | |<----+<--------------------+ |
3996 * +------+ +------------------------------------------+
3997 * scn,tld sca,irc,ict,tlu
4002 * Open: LCP reached authentication phase
4003 * Close: LCP reached terminate phase
4005 * RCA+: received reply (pap-req, chap-response), acceptable
4006 * RCN: received reply (pap-req, chap-response), not acceptable
4007 * TO+: timeout with restart counter >= 0
4008 * TO-: timeout with restart counter < 0
4009 * TO*: reschedule timeout for CHAP
4011 * scr: send request packet (none for PAP, chap-challenge)
4012 * sca: send ack packet (pap-ack, chap-success)
4013 * scn: send nak packet (pap-nak, chap-failure)
4014 * ict: initialize re-challenge timer (CHAP only)
4016 * tlu: this-layer-up, LCP reaches network phase
4017 * tld: this-layer-down, LCP enters terminate phase
4019 * Note that in CHAP mode, after sending a new challenge, while the state
4020 * automaton falls back into Req-Sent state, it doesn't signal a tld
4021 * event to LCP, so LCP remains in network phase. Only after not getting
4022 * any response (or after getting an unacceptable response), CHAP closes,
4023 * causing LCP to enter terminate phase.
4025 * With PAP, there is no initial request that can be sent. The peer is
4026 * expected to send one based on the successful negotiation of PAP as
4027 * the authentication protocol during the LCP option negotiation.
4029 * Incoming authentication protocol requests (remote requests
4030 * authentication, we are peer) don't employ a state machine at all,
4031 * they are simply answered. Some peers [Ascend P50 firmware rev
4032 * 4.50] react allergically when sending IPCP requests while they are
4033 * still in authentication phase (thereby violating the standard that
4034 * demands that these NCP packets are to be discarded), so we keep
4035 * track of the peer demanding us to authenticate, and only proceed to
4036 * phase network once we've seen a positive acknowledge for the
4041 * Handle incoming CHAP packets.
4044 sppp_chap_input(struct sppp *sp, struct mbuf *m)
4047 struct lcp_header *h;
4049 u_char *value, *name, digest[AUTHKEYLEN], dsize;
4050 int value_len, name_len;
4053 len = m->m_pkthdr.len;
4057 SPP_FMT "chap invalid packet length: %d bytes\n",
4058 SPP_ARGS(ifp), len);
4061 h = mtod (m, struct lcp_header*);
4062 if (len > ntohs (h->len))
4063 len = ntohs (h->len);
4066 /* challenge, failure and success are his authproto */
4067 case CHAP_CHALLENGE:
4068 value = 1 + (u_char*)(h+1);
4069 value_len = value[-1];
4070 name = value + value_len;
4071 name_len = len - value_len - 5;
4075 SPP_FMT "chap corrupted challenge "
4076 "<%s id=0x%x len=%d",
4078 sppp_auth_type_name(PPP_CHAP, h->type),
4079 h->ident, ntohs(h->len));
4080 sppp_print_bytes((u_char*) (h+1), len-4);
4088 SPP_FMT "chap input <%s id=0x%x len=%d name=",
4090 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
4092 sppp_print_string((char*) name, name_len);
4093 log(-1, " value-size=%d value=", value_len);
4094 sppp_print_bytes(value, value_len);
4098 /* Compute reply value. */
4100 MD5Update(&ctx, &h->ident, 1);
4101 MD5Update(&ctx, sp->myauth.secret,
4102 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
4103 MD5Update(&ctx, value, value_len);
4104 MD5Final(digest, &ctx);
4105 dsize = sizeof digest;
4107 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
4108 sizeof dsize, (const char *)&dsize,
4109 sizeof digest, digest,
4110 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4117 log(LOG_DEBUG, SPP_FMT "chap success",
4121 sppp_print_string((char*)(h + 1), len - 4);
4127 sp->pp_flags &= ~PP_NEEDAUTH;
4128 if (sp->myauth.proto == PPP_CHAP &&
4129 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4130 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
4132 * We are authenticator for CHAP but didn't
4133 * complete yet. Leave it to tlu to proceed
4142 sppp_phase_network(sp);
4147 log(LOG_INFO, SPP_FMT "chap failure",
4151 sppp_print_string((char*)(h + 1), len - 4);
4155 log(LOG_INFO, SPP_FMT "chap failure\n",
4157 /* await LCP shutdown by authenticator */
4160 /* response is my authproto */
4162 value = 1 + (u_char*)(h+1);
4163 value_len = value[-1];
4164 name = value + value_len;
4165 name_len = len - value_len - 5;
4169 SPP_FMT "chap corrupted response "
4170 "<%s id=0x%x len=%d",
4172 sppp_auth_type_name(PPP_CHAP, h->type),
4173 h->ident, ntohs(h->len));
4174 sppp_print_bytes((u_char*)(h+1), len-4);
4179 if (h->ident != sp->confid[IDX_CHAP]) {
4182 SPP_FMT "chap dropping response for old ID "
4183 "(got %d, expected %d)\n",
4185 h->ident, sp->confid[IDX_CHAP]);
4188 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4189 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4190 log(LOG_INFO, SPP_FMT "chap response, his name ",
4192 sppp_print_string(name, name_len);
4193 log(-1, " != expected ");
4194 sppp_print_string(sp->hisauth.name,
4195 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4199 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4200 "<%s id=0x%x len=%d name=",
4202 sppp_state_name(sp->state[IDX_CHAP]),
4203 sppp_auth_type_name(PPP_CHAP, h->type),
4204 h->ident, ntohs (h->len));
4205 sppp_print_string((char*)name, name_len);
4206 log(-1, " value-size=%d value=", value_len);
4207 sppp_print_bytes(value, value_len);
4210 if (value_len != AUTHKEYLEN) {
4213 SPP_FMT "chap bad hash value length: "
4214 "%d bytes, should be %d\n",
4215 SPP_ARGS(ifp), value_len,
4221 MD5Update(&ctx, &h->ident, 1);
4222 MD5Update(&ctx, sp->hisauth.secret,
4223 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4224 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4225 MD5Final(digest, &ctx);
4227 #define FAILMSG "Failed..."
4228 #define SUCCMSG "Welcome!"
4230 if (value_len != sizeof digest ||
4231 bcmp(digest, value, value_len) != 0) {
4232 /* action scn, tld */
4233 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4234 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4239 /* action sca, perhaps tlu */
4240 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4241 sp->state[IDX_CHAP] == STATE_OPENED)
4242 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4243 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4245 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4246 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4252 /* Unknown CHAP packet type -- ignore. */
4254 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4255 "<0x%x id=0x%xh len=%d",
4257 sppp_state_name(sp->state[IDX_CHAP]),
4258 h->type, h->ident, ntohs(h->len));
4259 sppp_print_bytes((u_char*)(h+1), len-4);
4268 sppp_chap_init(struct sppp *sp)
4270 /* Chap doesn't have STATE_INITIAL at all. */
4271 sp->state[IDX_CHAP] = STATE_CLOSED;
4272 sp->fail_counter[IDX_CHAP] = 0;
4273 sp->pp_seq[IDX_CHAP] = 0;
4274 sp->pp_rseq[IDX_CHAP] = 0;
4275 callout_init(&sp->ch[IDX_CHAP],
4276 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
4280 sppp_chap_open(struct sppp *sp)
4282 if (sp->myauth.proto == PPP_CHAP &&
4283 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4284 /* we are authenticator for CHAP, start it */
4286 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4287 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4289 /* nothing to be done if we are peer, await a challenge */
4293 sppp_chap_close(struct sppp *sp)
4295 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4296 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4300 sppp_chap_TO(void *cookie)
4302 struct sppp *sp = (struct sppp *)cookie;
4309 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4311 sppp_state_name(sp->state[IDX_CHAP]),
4312 sp->rst_counter[IDX_CHAP]);
4314 if (--sp->rst_counter[IDX_CHAP] < 0)
4316 switch (sp->state[IDX_CHAP]) {
4317 case STATE_REQ_SENT:
4319 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4323 /* TO+ (or TO*) event */
4324 switch (sp->state[IDX_CHAP]) {
4327 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4329 case STATE_REQ_SENT:
4331 /* sppp_cp_change_state() will restart the timer */
4332 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4341 sppp_chap_tlu(struct sppp *sp)
4347 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4350 * Some broken CHAP implementations (Conware CoNet, firmware
4351 * 4.0.?) don't want to re-authenticate their CHAP once the
4352 * initial challenge-response exchange has taken place.
4353 * Provide for an option to avoid rechallenges.
4355 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4357 * Compute the re-challenge timeout. This will yield
4358 * a number between 300 and 810 seconds.
4360 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
4361 callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, (void *)sp);
4366 SPP_FMT "chap %s, ",
4368 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4369 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4370 log(-1, "next re-challenge in %d seconds\n", i);
4372 log(-1, "re-challenging supressed\n");
4377 /* indicate to LCP that we need to be closed down */
4378 sp->lcp.protos |= (1 << IDX_CHAP);
4380 if (sp->pp_flags & PP_NEEDAUTH) {
4382 * Remote is authenticator, but his auth proto didn't
4383 * complete yet. Defer the transition to network
4394 * If we are already in phase network, we are done here. This
4395 * is the case if this is a dummy tlu event after a re-challenge.
4397 if (sp->pp_phase != PHASE_NETWORK)
4398 sppp_phase_network(sp);
4402 sppp_chap_tld(struct sppp *sp)
4407 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4408 callout_stop(&sp->ch[IDX_CHAP]);
4409 sp->lcp.protos &= ~(1 << IDX_CHAP);
4415 sppp_chap_scr(struct sppp *sp)
4420 /* Compute random challenge. */
4421 ch = (u_long *)sp->myauth.challenge;
4422 read_random(&seed, sizeof seed);
4423 ch[0] = seed ^ random();
4424 ch[1] = seed ^ random();
4425 ch[2] = seed ^ random();
4426 ch[3] = seed ^ random();
4429 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4431 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4432 sizeof clen, (const char *)&clen,
4433 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4434 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4440 *--------------------------------------------------------------------------*
4442 * The PAP implementation. *
4444 *--------------------------------------------------------------------------*
4447 * For PAP, we need to keep a little state also if we are the peer, not the
4448 * authenticator. This is since we don't get a request to authenticate, but
4449 * have to repeatedly authenticate ourself until we got a response (or the
4450 * retry counter is expired).
4454 * Handle incoming PAP packets. */
4456 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4459 struct lcp_header *h;
4461 u_char *name, *passwd, mlen;
4462 int name_len, passwd_len;
4464 len = m->m_pkthdr.len;
4468 SPP_FMT "pap invalid packet length: %d bytes\n",
4469 SPP_ARGS(ifp), len);
4472 h = mtod (m, struct lcp_header*);
4473 if (len > ntohs (h->len))
4474 len = ntohs (h->len);
4476 /* PAP request is my authproto */
4478 name = 1 + (u_char*)(h+1);
4479 name_len = name[-1];
4480 passwd = name + name_len + 1;
4481 if (name_len > len - 6 ||
4482 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4484 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4485 "<%s id=0x%x len=%d",
4487 sppp_auth_type_name(PPP_PAP, h->type),
4488 h->ident, ntohs(h->len));
4489 sppp_print_bytes((u_char*)(h+1), len-4);
4495 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4496 "<%s id=0x%x len=%d name=",
4498 sppp_state_name(sp->state[IDX_PAP]),
4499 sppp_auth_type_name(PPP_PAP, h->type),
4500 h->ident, ntohs(h->len));
4501 sppp_print_string((char*)name, name_len);
4502 log(-1, " passwd=");
4503 sppp_print_string((char*)passwd, passwd_len);
4506 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4507 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4508 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4509 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4510 /* action scn, tld */
4511 mlen = sizeof(FAILMSG) - 1;
4512 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4513 sizeof mlen, (const char *)&mlen,
4514 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4519 /* action sca, perhaps tlu */
4520 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4521 sp->state[IDX_PAP] == STATE_OPENED) {
4522 mlen = sizeof(SUCCMSG) - 1;
4523 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4524 sizeof mlen, (const char *)&mlen,
4525 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4528 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4529 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4534 /* ack and nak are his authproto */
4536 callout_stop(&sp->pap_my_to_ch);
4538 log(LOG_DEBUG, SPP_FMT "pap success",
4540 name_len = *((char *)h);
4541 if (len > 5 && name_len) {
4543 sppp_print_string((char*)(h+1), name_len);
4549 sp->pp_flags &= ~PP_NEEDAUTH;
4550 if (sp->myauth.proto == PPP_PAP &&
4551 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4552 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4554 * We are authenticator for PAP but didn't
4555 * complete yet. Leave it to tlu to proceed
4564 sppp_phase_network(sp);
4568 callout_stop (&sp->pap_my_to_ch);
4570 log(LOG_INFO, SPP_FMT "pap failure",
4572 name_len = *((char *)h);
4573 if (len > 5 && name_len) {
4575 sppp_print_string((char*)(h+1), name_len);
4579 log(LOG_INFO, SPP_FMT "pap failure\n",
4581 /* await LCP shutdown by authenticator */
4585 /* Unknown PAP packet type -- ignore. */
4587 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4588 "<0x%x id=0x%x len=%d",
4590 h->type, h->ident, ntohs(h->len));
4591 sppp_print_bytes((u_char*)(h+1), len-4);
4600 sppp_pap_init(struct sppp *sp)
4602 /* PAP doesn't have STATE_INITIAL at all. */
4603 sp->state[IDX_PAP] = STATE_CLOSED;
4604 sp->fail_counter[IDX_PAP] = 0;
4605 sp->pp_seq[IDX_PAP] = 0;
4606 sp->pp_rseq[IDX_PAP] = 0;
4607 callout_init(&sp->ch[IDX_PAP],
4608 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
4609 callout_init(&sp->pap_my_to_ch,
4610 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
4614 sppp_pap_open(struct sppp *sp)
4616 if (sp->hisauth.proto == PPP_PAP &&
4617 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4618 /* we are authenticator for PAP, start our timer */
4619 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4620 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4622 if (sp->myauth.proto == PPP_PAP) {
4623 /* we are peer, send a request, and start a timer */
4625 callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
4626 sppp_pap_my_TO, (void *)sp);
4631 sppp_pap_close(struct sppp *sp)
4633 if (sp->state[IDX_PAP] != STATE_CLOSED)
4634 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4638 * That's the timeout routine if we are authenticator. Since the
4639 * authenticator is basically passive in PAP, we can't do much here.
4642 sppp_pap_TO(void *cookie)
4644 struct sppp *sp = (struct sppp *)cookie;
4651 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4653 sppp_state_name(sp->state[IDX_PAP]),
4654 sp->rst_counter[IDX_PAP]);
4656 if (--sp->rst_counter[IDX_PAP] < 0)
4658 switch (sp->state[IDX_PAP]) {
4659 case STATE_REQ_SENT:
4661 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4665 /* TO+ event, not very much we could do */
4666 switch (sp->state[IDX_PAP]) {
4667 case STATE_REQ_SENT:
4668 /* sppp_cp_change_state() will restart the timer */
4669 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4678 * That's the timeout handler if we are peer. Since the peer is active,
4679 * we need to retransmit our PAP request since it is apparently lost.
4680 * XXX We should impose a max counter.
4683 sppp_pap_my_TO(void *cookie)
4685 struct sppp *sp = (struct sppp *)cookie;
4689 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4698 sppp_pap_tlu(struct sppp *sp)
4703 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4706 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4707 SPP_ARGS(ifp), pap.name);
4711 /* indicate to LCP that we need to be closed down */
4712 sp->lcp.protos |= (1 << IDX_PAP);
4714 if (sp->pp_flags & PP_NEEDAUTH) {
4716 * Remote is authenticator, but his auth proto didn't
4717 * complete yet. Defer the transition to network
4726 sppp_phase_network(sp);
4730 sppp_pap_tld(struct sppp *sp)
4735 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4736 callout_stop (&sp->ch[IDX_PAP]);
4737 callout_stop (&sp->pap_my_to_ch);
4738 sp->lcp.protos &= ~(1 << IDX_PAP);
4744 sppp_pap_scr(struct sppp *sp)
4746 u_char idlen, pwdlen;
4748 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4749 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4750 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4752 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4753 sizeof idlen, (const char *)&idlen,
4754 (size_t)idlen, sp->myauth.name,
4755 sizeof pwdlen, (const char *)&pwdlen,
4756 (size_t)pwdlen, sp->myauth.secret,
4761 * Random miscellaneous functions.
4765 * Send a PAP or CHAP proto packet.
4767 * Varadic function, each of the elements for the ellipsis is of type
4768 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4770 * NOTE: never declare variadic functions with types subject to type
4771 * promotion (i.e. u_char). This is asking for big trouble depending
4772 * on the architecture you are on...
4776 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4777 unsigned int type, unsigned int id,
4781 struct ppp_header *h;
4782 struct lcp_header *lh;
4790 MGETHDR (m, M_DONTWAIT, MT_DATA);
4793 m->m_pkthdr.rcvif = 0;
4795 h = mtod (m, struct ppp_header*);
4796 h->address = PPP_ALLSTATIONS; /* broadcast address */
4797 h->control = PPP_UI; /* Unnumbered Info */
4798 h->protocol = htons(cp->proto);
4800 lh = (struct lcp_header*)(h + 1);
4803 p = (u_char*) (lh+1);
4808 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
4809 msg = va_arg(ap, const char *);
4811 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4817 bcopy(msg, p, mlen);
4822 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4823 lh->len = htons (LCP_HEADER_LEN + len);
4826 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4827 SPP_ARGS(ifp), cp->name,
4828 sppp_auth_type_name(cp->proto, lh->type),
4829 lh->ident, ntohs(lh->len));
4830 sppp_print_bytes((u_char*) (lh+1), len);
4833 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
4838 * Flush interface queue.
4841 sppp_qflush(struct ifqueue *ifq)
4856 * Send keepalive packets, every 10 seconds.
4859 sppp_keepalive(void *dummy)
4861 struct sppp *sp = (struct sppp*)dummy;
4862 struct ifnet *ifp = SP2IFP(sp);
4867 /* Keepalive mode disabled or channel down? */
4868 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4869 ! (ifp->if_drv_flags & IFF_DRV_RUNNING))
4872 if (sp->pp_mode == PP_FR) {
4873 sppp_fr_keepalive (sp);
4877 /* No keepalive in PPP mode if LCP not opened yet. */
4878 if (sp->pp_mode != IFF_CISCO &&
4879 sp->pp_phase < PHASE_AUTHENTICATE)
4882 if (sp->pp_alivecnt == MAXALIVECNT) {
4883 /* No keepalive packets got. Stop the interface. */
4884 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
4886 sppp_qflush (&sp->pp_cpq);
4887 if (sp->pp_mode != IFF_CISCO) {
4889 /* Shut down the PPP link. */
4891 /* Initiate negotiation. XXX */
4895 if (sp->pp_alivecnt <= MAXALIVECNT)
4897 if (sp->pp_mode == IFF_CISCO)
4898 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4899 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4900 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4901 long nmagic = htonl (sp->lcp.magic);
4902 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4903 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4904 sp->lcp.echoid, 4, &nmagic);
4909 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
4914 * Get both IP addresses.
4917 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4919 struct ifnet *ifp = SP2IFP(sp);
4921 struct sockaddr_in *si, *sm;
4927 * Pick the first AF_INET address from the list,
4928 * aliases don't make any sense on a p2p link anyway.
4931 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4932 if (ifa->ifa_addr->sa_family == AF_INET) {
4933 si = (struct sockaddr_in *)ifa->ifa_addr;
4934 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4939 if (si && si->sin_addr.s_addr) {
4940 ssrc = si->sin_addr.s_addr;
4942 *srcmask = ntohl(sm->sin_addr.s_addr);
4945 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4946 if (si && si->sin_addr.s_addr)
4947 ddst = si->sin_addr.s_addr;
4950 if (dst) *dst = ntohl(ddst);
4951 if (src) *src = ntohl(ssrc);
4956 * Set my IP address. Must be called at splimp.
4959 sppp_set_ip_addr(struct sppp *sp, u_long src)
4961 INIT_VNET_INET(curvnet);
4964 struct sockaddr_in *si;
4965 struct in_ifaddr *ia;
4968 * Pick the first AF_INET address from the list,
4969 * aliases don't make any sense on a p2p link anyway.
4972 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4974 if (ifa->ifa_addr->sa_family == AF_INET)
4976 si = (struct sockaddr_in *)ifa->ifa_addr;
4985 /* delete old route */
4986 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4989 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4990 SPP_ARGS(ifp), error);
4993 /* set new address */
4994 si->sin_addr.s_addr = htonl(src);
4996 LIST_REMOVE(ia, ia_hash);
4997 LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash);
5000 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
5003 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
5004 SPP_ARGS(ifp), error);
5012 * Get both IPv6 addresses.
5015 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
5016 struct in6_addr *srcmask)
5018 struct ifnet *ifp = SP2IFP(sp);
5020 struct sockaddr_in6 *si, *sm;
5021 struct in6_addr ssrc, ddst;
5024 bzero(&ssrc, sizeof(ssrc));
5025 bzero(&ddst, sizeof(ddst));
5027 * Pick the first link-local AF_INET6 address from the list,
5028 * aliases don't make any sense on a p2p link anyway.
5031 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
5032 if (ifa->ifa_addr->sa_family == AF_INET6) {
5033 si = (struct sockaddr_in6 *)ifa->ifa_addr;
5034 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
5035 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
5039 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
5040 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
5042 bcopy(&sm->sin6_addr, srcmask,
5047 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
5048 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
5049 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
5053 bcopy(&ddst, dst, sizeof(*dst));
5055 bcopy(&ssrc, src, sizeof(*src));
5058 #ifdef IPV6CP_MYIFID_DYN
5060 * Generate random ifid.
5063 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
5069 * Set my IPv6 address. Must be called at splimp.
5072 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
5076 struct sockaddr_in6 *sin6;
5079 * Pick the first link-local AF_INET6 address from the list,
5080 * aliases don't make any sense on a p2p link anyway.
5084 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
5086 if (ifa->ifa_addr->sa_family == AF_INET6)
5088 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
5089 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
5097 struct sockaddr_in6 new_sin6 = *sin6;
5099 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
5100 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5103 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
5104 " failed, error=%d\n", SPP_ARGS(ifp), error);
5111 * Suggest a candidate address to be used by peer.
5114 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5116 struct in6_addr myaddr;
5119 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5121 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5123 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5124 myaddr.s6_addr[14] ^= 0xff;
5125 myaddr.s6_addr[15] ^= 0xff;
5127 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5128 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5131 bcopy(&myaddr, suggest, sizeof(myaddr));
5136 sppp_params(struct sppp *sp, u_long cmd, void *data)
5139 struct ifreq *ifr = (struct ifreq *)data;
5140 struct spppreq *spr;
5143 if ((spr = malloc(sizeof(struct spppreq), M_TEMP, M_NOWAIT)) == 0)
5146 * ifr->ifr_data is supposed to point to a struct spppreq.
5147 * Check the cmd word first before attempting to fetch all the
5150 if ((subcmd = fuword(ifr->ifr_data)) == -1) {
5155 if (copyin((caddr_t)ifr->ifr_data, spr, sizeof(struct spppreq)) != 0) {
5161 case (u_long)SPPPIOGDEFS:
5162 if (cmd != SIOCGIFGENERIC) {
5167 * We copy over the entire current state, but clean
5168 * out some of the stuff we don't wanna pass up.
5169 * Remember, SIOCGIFGENERIC is unprotected, and can be
5170 * called by any user. No need to ever get PAP or
5171 * CHAP secrets back to userland anyway.
5173 spr->defs.pp_phase = sp->pp_phase;
5174 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5175 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5176 spr->defs.lcp = sp->lcp;
5177 spr->defs.ipcp = sp->ipcp;
5178 spr->defs.ipv6cp = sp->ipv6cp;
5179 spr->defs.myauth = sp->myauth;
5180 spr->defs.hisauth = sp->hisauth;
5181 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5182 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5183 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5184 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5186 * Fixup the LCP timeout value to milliseconds so
5187 * spppcontrol doesn't need to bother about the value
5188 * of "hz". We do the reverse calculation below when
5191 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5192 rv = copyout(spr, (caddr_t)ifr->ifr_data,
5193 sizeof(struct spppreq));
5196 case (u_long)SPPPIOSDEFS:
5197 if (cmd != SIOCSIFGENERIC) {
5202 * We have a very specific idea of which fields we
5203 * allow being passed back from userland, so to not
5204 * clobber our current state. For one, we only allow
5205 * setting anything if LCP is in dead or establish
5206 * phase. Once the authentication negotiations
5207 * started, the authentication settings must not be
5208 * changed again. (The administrator can force an
5209 * ifconfig down in order to get LCP back into dead
5212 * Also, we only allow for authentication parameters to be
5215 * XXX Should allow to set or clear pp_flags.
5217 * Finally, if the respective authentication protocol to
5218 * be used is set differently than 0, but the secret is
5219 * passed as all zeros, we don't trash the existing secret.
5220 * This allows an administrator to change the system name
5221 * only without clobbering the secret (which he didn't get
5222 * back in a previous SPPPIOGDEFS call). However, the
5223 * secrets are cleared if the authentication protocol is
5225 if (sp->pp_phase != PHASE_DEAD &&
5226 sp->pp_phase != PHASE_ESTABLISH) {
5231 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5232 spr->defs.myauth.proto != PPP_CHAP) ||
5233 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5234 spr->defs.hisauth.proto != PPP_CHAP)) {
5239 if (spr->defs.myauth.proto == 0)
5240 /* resetting myauth */
5241 bzero(&sp->myauth, sizeof sp->myauth);
5243 /* setting/changing myauth */
5244 sp->myauth.proto = spr->defs.myauth.proto;
5245 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5246 if (spr->defs.myauth.secret[0] != '\0')
5247 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5250 if (spr->defs.hisauth.proto == 0)
5251 /* resetting hisauth */
5252 bzero(&sp->hisauth, sizeof sp->hisauth);
5254 /* setting/changing hisauth */
5255 sp->hisauth.proto = spr->defs.hisauth.proto;
5256 sp->hisauth.flags = spr->defs.hisauth.flags;
5257 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5258 if (spr->defs.hisauth.secret[0] != '\0')
5259 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5262 /* set LCP restart timer timeout */
5263 if (spr->defs.lcp.timeout != 0)
5264 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5265 /* set VJ enable and IPv6 disable flags */
5267 if (spr->defs.enable_vj)
5268 sp->confflags |= CONF_ENABLE_VJ;
5270 sp->confflags &= ~CONF_ENABLE_VJ;
5273 if (spr->defs.enable_ipv6)
5274 sp->confflags |= CONF_ENABLE_IPV6;
5276 sp->confflags &= ~CONF_ENABLE_IPV6;
5291 sppp_phase_network(struct sppp *sp)
5297 sp->pp_phase = PHASE_NETWORK;
5300 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5301 sppp_phase_name(sp->pp_phase));
5303 /* Notify NCPs now. */
5304 for (i = 0; i < IDX_COUNT; i++)
5305 if ((cps[i])->flags & CP_NCP)
5308 /* Send Up events to all NCPs. */
5309 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5310 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5313 /* if no NCP is starting, all this was in vain, close down */
5314 sppp_lcp_check_and_close(sp);
5319 sppp_cp_type_name(u_char type)
5321 static char buf[12];
5323 case CONF_REQ: return "conf-req";
5324 case CONF_ACK: return "conf-ack";
5325 case CONF_NAK: return "conf-nak";
5326 case CONF_REJ: return "conf-rej";
5327 case TERM_REQ: return "term-req";
5328 case TERM_ACK: return "term-ack";
5329 case CODE_REJ: return "code-rej";
5330 case PROTO_REJ: return "proto-rej";
5331 case ECHO_REQ: return "echo-req";
5332 case ECHO_REPLY: return "echo-reply";
5333 case DISC_REQ: return "discard-req";
5335 snprintf (buf, sizeof(buf), "cp/0x%x", type);
5340 sppp_auth_type_name(u_short proto, u_char type)
5342 static char buf[12];
5346 case CHAP_CHALLENGE: return "challenge";
5347 case CHAP_RESPONSE: return "response";
5348 case CHAP_SUCCESS: return "success";
5349 case CHAP_FAILURE: return "failure";
5353 case PAP_REQ: return "req";
5354 case PAP_ACK: return "ack";
5355 case PAP_NAK: return "nak";
5358 snprintf (buf, sizeof(buf), "auth/0x%x", type);
5363 sppp_lcp_opt_name(u_char opt)
5365 static char buf[12];
5367 case LCP_OPT_MRU: return "mru";
5368 case LCP_OPT_ASYNC_MAP: return "async-map";
5369 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5370 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5371 case LCP_OPT_MAGIC: return "magic";
5372 case LCP_OPT_PROTO_COMP: return "proto-comp";
5373 case LCP_OPT_ADDR_COMP: return "addr-comp";
5375 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5381 sppp_ipcp_opt_name(u_char opt)
5383 static char buf[12];
5385 case IPCP_OPT_ADDRESSES: return "addresses";
5386 case IPCP_OPT_COMPRESSION: return "compression";
5387 case IPCP_OPT_ADDRESS: return "address";
5389 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5396 sppp_ipv6cp_opt_name(u_char opt)
5398 static char buf[12];
5400 case IPV6CP_OPT_IFID: return "ifid";
5401 case IPV6CP_OPT_COMPRESSION: return "compression";
5403 sprintf (buf, "0x%x", opt);
5409 sppp_state_name(int state)
5412 case STATE_INITIAL: return "initial";
5413 case STATE_STARTING: return "starting";
5414 case STATE_CLOSED: return "closed";
5415 case STATE_STOPPED: return "stopped";
5416 case STATE_CLOSING: return "closing";
5417 case STATE_STOPPING: return "stopping";
5418 case STATE_REQ_SENT: return "req-sent";
5419 case STATE_ACK_RCVD: return "ack-rcvd";
5420 case STATE_ACK_SENT: return "ack-sent";
5421 case STATE_OPENED: return "opened";
5427 sppp_phase_name(enum ppp_phase phase)
5430 case PHASE_DEAD: return "dead";
5431 case PHASE_ESTABLISH: return "establish";
5432 case PHASE_TERMINATE: return "terminate";
5433 case PHASE_AUTHENTICATE: return "authenticate";
5434 case PHASE_NETWORK: return "network";
5440 sppp_proto_name(u_short proto)
5442 static char buf[12];
5444 case PPP_LCP: return "lcp";
5445 case PPP_IPCP: return "ipcp";
5446 case PPP_PAP: return "pap";
5447 case PPP_CHAP: return "chap";
5448 case PPP_IPV6CP: return "ipv6cp";
5450 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5455 sppp_print_bytes(const u_char *p, u_short len)
5458 log(-1, " %*D", len, p, "-");
5462 sppp_print_string(const char *p, u_short len)
5469 * Print only ASCII chars directly. RFC 1994 recommends
5470 * using only them, but we don't rely on it. */
5471 if (c < ' ' || c > '~')
5472 log(-1, "\\x%x", c);
5480 sppp_dotted_quad(u_long addr)
5483 sprintf(s, "%d.%d.%d.%d",
5484 (int)((addr >> 24) & 0xff),
5485 (int)((addr >> 16) & 0xff),
5486 (int)((addr >> 8) & 0xff),
5487 (int)(addr & 0xff));
5493 sppp_strnlen(u_char *p, int max)
5497 for (len = 0; len < max && *p; ++p)
5502 /* a dummy, used to drop uninteresting events */
5504 sppp_null(struct sppp *unused)
5506 /* do just nothing */