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>
62 #include <netinet6/scope6_var.h>
65 #include <netinet/if_ether.h>
68 #include <netipx/ipx.h>
69 #include <netipx/ipx_if.h>
72 #include <net/if_sppp.h>
74 #define IOCTL_CMD_T u_long
75 #define MAXALIVECNT 3 /* max. alive packets */
78 * Interface flags that can be set in an ifconfig command.
80 * Setting link0 will make the link passive, i.e. it will be marked
81 * as being administrative openable, but won't be opened to begin
82 * with. Incoming calls will be answered, or subsequent calls with
83 * -link1 will cause the administrative open of the LCP layer.
85 * Setting link1 will cause the link to auto-dial only as packets
88 * Setting IFF_DEBUG will syslog the option negotiation and state
89 * transitions at level kern.debug. Note: all logs consistently look
92 * <if-name><unit>: <proto-name> <additional info...>
94 * with <if-name><unit> being something like "bppp0", and <proto-name>
95 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
98 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
99 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
100 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
102 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
103 #define PPP_UI 0x03 /* Unnumbered Information */
104 #define PPP_IP 0x0021 /* Internet Protocol */
105 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
106 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
107 #define PPP_IPX 0x002b /* Novell IPX Protocol */
108 #define PPP_VJ_COMP 0x002d /* VJ compressed TCP/IP */
109 #define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */
110 #define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */
111 #define PPP_LCP 0xc021 /* Link Control Protocol */
112 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
113 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
114 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
115 #define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */
117 #define CONF_REQ 1 /* PPP configure request */
118 #define CONF_ACK 2 /* PPP configure acknowledge */
119 #define CONF_NAK 3 /* PPP configure negative ack */
120 #define CONF_REJ 4 /* PPP configure reject */
121 #define TERM_REQ 5 /* PPP terminate request */
122 #define TERM_ACK 6 /* PPP terminate acknowledge */
123 #define CODE_REJ 7 /* PPP code reject */
124 #define PROTO_REJ 8 /* PPP protocol reject */
125 #define ECHO_REQ 9 /* PPP echo request */
126 #define ECHO_REPLY 10 /* PPP echo reply */
127 #define DISC_REQ 11 /* PPP discard request */
129 #define LCP_OPT_MRU 1 /* maximum receive unit */
130 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
131 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
132 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
133 #define LCP_OPT_MAGIC 5 /* magic number */
134 #define LCP_OPT_RESERVED 6 /* reserved */
135 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
136 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
138 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
139 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
140 #define IPCP_OPT_ADDRESS 3 /* local IP address */
142 #define IPV6CP_OPT_IFID 1 /* interface identifier */
143 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
145 #define IPCP_COMP_VJ 0x2d /* Code for VJ compression */
147 #define PAP_REQ 1 /* PAP name/password request */
148 #define PAP_ACK 2 /* PAP acknowledge */
149 #define PAP_NAK 3 /* PAP fail */
151 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
152 #define CHAP_RESPONSE 2 /* CHAP challenge response */
153 #define CHAP_SUCCESS 3 /* CHAP response ok */
154 #define CHAP_FAILURE 4 /* CHAP response failed */
156 #define CHAP_MD5 5 /* hash algorithm - MD5 */
158 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
159 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
160 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
161 #define CISCO_ADDR_REQ 0 /* Cisco address request */
162 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
163 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
165 /* states are named and numbered according to RFC 1661 */
166 #define STATE_INITIAL 0
167 #define STATE_STARTING 1
168 #define STATE_CLOSED 2
169 #define STATE_STOPPED 3
170 #define STATE_CLOSING 4
171 #define STATE_STOPPING 5
172 #define STATE_REQ_SENT 6
173 #define STATE_ACK_RCVD 7
174 #define STATE_ACK_SENT 8
175 #define STATE_OPENED 9
177 MALLOC_DEFINE(M_SPPP, "sppp", "synchronous PPP interface internals");
184 #define PPP_HEADER_LEN sizeof (struct ppp_header)
191 #define LCP_HEADER_LEN sizeof (struct lcp_header)
193 struct cisco_packet {
201 #define CISCO_PACKET_LEN sizeof (struct cisco_packet)
204 * We follow the spelling and capitalization of RFC 1661 here, to make
205 * it easier comparing with the standard. Please refer to this RFC in
206 * case you can't make sense out of these abbreviation; it will also
207 * explain the semantics related to the various events and actions.
210 u_short proto; /* PPP control protocol number */
211 u_char protoidx; /* index into state table in struct sppp */
213 #define CP_LCP 0x01 /* this is the LCP */
214 #define CP_AUTH 0x02 /* this is an authentication protocol */
215 #define CP_NCP 0x04 /* this is a NCP */
216 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
217 const char *name; /* name of this control protocol */
219 void (*Up)(struct sppp *sp);
220 void (*Down)(struct sppp *sp);
221 void (*Open)(struct sppp *sp);
222 void (*Close)(struct sppp *sp);
223 void (*TO)(void *sp);
224 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
225 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
226 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
228 void (*tlu)(struct sppp *sp);
229 void (*tld)(struct sppp *sp);
230 void (*tls)(struct sppp *sp);
231 void (*tlf)(struct sppp *sp);
232 void (*scr)(struct sppp *sp);
235 #define SPP_FMT "%s: "
236 #define SPP_ARGS(ifp) (ifp)->if_xname
238 #define SPPP_LOCK(sp) \
240 if (!(SP2IFP(sp)->if_flags & IFF_NEEDSGIANT)) \
241 mtx_lock (&(sp)->mtx); \
243 #define SPPP_UNLOCK(sp) \
245 if (!(SP2IFP(sp)->if_flags & IFF_NEEDSGIANT)) \
246 mtx_unlock (&(sp)->mtx); \
249 #define SPPP_LOCK_ASSERT(sp) \
251 if (!(SP2IFP(sp)->if_flags & IFF_NEEDSGIANT)) \
252 mtx_assert (&(sp)->mtx, MA_OWNED); \
254 #define SPPP_LOCK_OWNED(sp) \
255 (!(SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) && \
256 mtx_owned (&sp->mtx))
260 * The following disgusting hack gets around the problem that IP TOS
261 * can't be set yet. We want to put "interactive" traffic on a high
262 * priority queue. To decide if traffic is interactive, we check that
263 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
265 * XXX is this really still necessary? - joerg -
267 static const u_short interactive_ports[8] = {
271 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
274 /* almost every function needs these */
276 struct ifnet *ifp = SP2IFP(sp); \
277 int debug = ifp->if_flags & IFF_DEBUG
279 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
280 struct sockaddr *dst, struct rtentry *rt);
282 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
283 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
285 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
287 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
288 u_char ident, u_short len, void *data);
289 /* static void sppp_cp_timeout(void *arg); */
290 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
292 static void sppp_auth_send(const struct cp *cp,
293 struct sppp *sp, unsigned int type, unsigned int id,
296 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
297 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
298 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
299 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
300 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
302 static void sppp_null(struct sppp *sp);
304 static void sppp_pp_up(struct sppp *sp);
305 static void sppp_pp_down(struct sppp *sp);
307 static void sppp_lcp_init(struct sppp *sp);
308 static void sppp_lcp_up(struct sppp *sp);
309 static void sppp_lcp_down(struct sppp *sp);
310 static void sppp_lcp_open(struct sppp *sp);
311 static void sppp_lcp_close(struct sppp *sp);
312 static void sppp_lcp_TO(void *sp);
313 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
314 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
315 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
316 static void sppp_lcp_tlu(struct sppp *sp);
317 static void sppp_lcp_tld(struct sppp *sp);
318 static void sppp_lcp_tls(struct sppp *sp);
319 static void sppp_lcp_tlf(struct sppp *sp);
320 static void sppp_lcp_scr(struct sppp *sp);
321 static void sppp_lcp_check_and_close(struct sppp *sp);
322 static int sppp_ncp_check(struct sppp *sp);
324 static void sppp_ipcp_init(struct sppp *sp);
325 static void sppp_ipcp_up(struct sppp *sp);
326 static void sppp_ipcp_down(struct sppp *sp);
327 static void sppp_ipcp_open(struct sppp *sp);
328 static void sppp_ipcp_close(struct sppp *sp);
329 static void sppp_ipcp_TO(void *sp);
330 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
331 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
332 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
333 static void sppp_ipcp_tlu(struct sppp *sp);
334 static void sppp_ipcp_tld(struct sppp *sp);
335 static void sppp_ipcp_tls(struct sppp *sp);
336 static void sppp_ipcp_tlf(struct sppp *sp);
337 static void sppp_ipcp_scr(struct sppp *sp);
339 static void sppp_ipv6cp_init(struct sppp *sp);
340 static void sppp_ipv6cp_up(struct sppp *sp);
341 static void sppp_ipv6cp_down(struct sppp *sp);
342 static void sppp_ipv6cp_open(struct sppp *sp);
343 static void sppp_ipv6cp_close(struct sppp *sp);
344 static void sppp_ipv6cp_TO(void *sp);
345 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
346 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
347 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
348 static void sppp_ipv6cp_tlu(struct sppp *sp);
349 static void sppp_ipv6cp_tld(struct sppp *sp);
350 static void sppp_ipv6cp_tls(struct sppp *sp);
351 static void sppp_ipv6cp_tlf(struct sppp *sp);
352 static void sppp_ipv6cp_scr(struct sppp *sp);
354 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
355 static void sppp_pap_init(struct sppp *sp);
356 static void sppp_pap_open(struct sppp *sp);
357 static void sppp_pap_close(struct sppp *sp);
358 static void sppp_pap_TO(void *sp);
359 static void sppp_pap_my_TO(void *sp);
360 static void sppp_pap_tlu(struct sppp *sp);
361 static void sppp_pap_tld(struct sppp *sp);
362 static void sppp_pap_scr(struct sppp *sp);
364 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
365 static void sppp_chap_init(struct sppp *sp);
366 static void sppp_chap_open(struct sppp *sp);
367 static void sppp_chap_close(struct sppp *sp);
368 static void sppp_chap_TO(void *sp);
369 static void sppp_chap_tlu(struct sppp *sp);
370 static void sppp_chap_tld(struct sppp *sp);
371 static void sppp_chap_scr(struct sppp *sp);
373 static const char *sppp_auth_type_name(u_short proto, u_char type);
374 static const char *sppp_cp_type_name(u_char type);
375 static const char *sppp_dotted_quad(u_long addr);
376 static const char *sppp_ipcp_opt_name(u_char opt);
378 static const char *sppp_ipv6cp_opt_name(u_char opt);
380 static const char *sppp_lcp_opt_name(u_char opt);
381 static const char *sppp_phase_name(enum ppp_phase phase);
382 static const char *sppp_proto_name(u_short proto);
383 static const char *sppp_state_name(int state);
384 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
385 static int sppp_strnlen(u_char *p, int max);
386 static void sppp_keepalive(void *dummy);
387 static void sppp_phase_network(struct sppp *sp);
388 static void sppp_print_bytes(const u_char *p, u_short len);
389 static void sppp_print_string(const char *p, u_short len);
390 static void sppp_qflush(struct ifqueue *ifq);
391 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
393 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
394 struct in6_addr *dst, struct in6_addr *srcmask);
395 #ifdef IPV6CP_MYIFID_DYN
396 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
397 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
399 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
402 /* if_start () wrapper */
403 static void sppp_ifstart (struct ifnet *ifp);
405 /* our control protocol descriptors */
406 static const struct cp lcp = {
407 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
408 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
409 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
410 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
414 static const struct cp ipcp = {
416 #ifdef INET /* don't run IPCP if there's no IPv4 support */
422 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
423 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
424 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
428 static const struct cp ipv6cp = {
429 PPP_IPV6CP, IDX_IPV6CP,
430 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
436 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
437 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
438 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
442 static const struct cp pap = {
443 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
444 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
445 sppp_pap_TO, 0, 0, 0,
446 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
450 static const struct cp chap = {
451 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
452 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
453 sppp_chap_TO, 0, 0, 0,
454 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
458 static const struct cp *cps[IDX_COUNT] = {
460 &ipcp, /* IDX_IPCP */
461 &ipv6cp, /* IDX_IPV6CP */
463 &chap, /* IDX_CHAP */
467 sppp_alloc(u_char type, struct ifnet *ifp)
471 sp = malloc(sizeof(struct sppp), M_SPPP, M_WAITOK | M_ZERO);
478 sppp_free(void *com, u_char type)
485 sppp_modevent(module_t mod, int type, void *unused)
490 * XXX: should probably be IFT_SPPP, but it's fairly
491 * harmless to allocate struct sppp's for non-sppp
495 if_register_com_alloc(IFT_PPP, sppp_alloc, sppp_free);
498 /* if_deregister_com_alloc(IFT_PPP); */
505 static moduledata_t spppmod = {
510 MODULE_VERSION(sppp, 1);
511 DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
514 * Exported functions, comprising our interface to the lower layer.
518 * Process the received packet.
521 sppp_input(struct ifnet *ifp, struct mbuf *m)
523 struct ppp_header *h;
525 struct sppp *sp = IFP2SP(ifp);
527 int hlen, vjlen, do_account = 0;
531 debug = ifp->if_flags & IFF_DEBUG;
533 if (ifp->if_flags & IFF_UP)
534 /* Count received bytes, add FCS and one flag */
535 ifp->if_ibytes += m->m_pkthdr.len + 3;
537 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
538 /* Too small packet, drop it. */
541 SPP_FMT "input packet is too small, %d bytes\n",
542 SPP_ARGS(ifp), m->m_pkthdr.len);
552 if (sp->pp_mode == PP_FR) {
553 sppp_fr_input (sp, m);
558 /* Get PPP header. */
559 h = mtod (m, struct ppp_header*);
560 m_adj (m, PPP_HEADER_LEN);
562 switch (h->address) {
563 case PPP_ALLSTATIONS:
564 if (h->control != PPP_UI)
566 if (sp->pp_mode == IFF_CISCO) {
569 SPP_FMT "PPP packet in Cisco mode "
570 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
572 h->address, h->control, ntohs(h->protocol));
575 switch (ntohs (h->protocol)) {
579 SPP_FMT "rejecting protocol "
580 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
582 h->address, h->control, ntohs(h->protocol));
583 if (sp->state[IDX_LCP] == STATE_OPENED)
584 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
585 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
590 sppp_cp_input(&lcp, sp, m);
595 if (sp->pp_phase >= PHASE_AUTHENTICATE)
596 sppp_pap_input(sp, m);
601 if (sp->pp_phase >= PHASE_AUTHENTICATE)
602 sppp_chap_input(sp, m);
608 if (sp->pp_phase == PHASE_NETWORK)
609 sppp_cp_input(&ipcp, sp, m);
614 if (sp->state[IDX_IPCP] == STATE_OPENED) {
620 if (sp->state[IDX_IPCP] == STATE_OPENED) {
622 sl_uncompress_tcp_core(mtod(m, u_char *),
626 &iphdr, &hlen)) <= 0) {
629 SPP_FMT "VJ uncompress failed on compressed packet\n",
635 * Trim the VJ header off the packet, and prepend
636 * the uncompressed IP header (which will usually
637 * end up in two chained mbufs since there's not
638 * enough leading space in the existing mbuf).
641 M_PREPEND(m, hlen, M_DONTWAIT);
646 bcopy(iphdr, mtod(m, u_char *), hlen);
652 if (sp->state[IDX_IPCP] == STATE_OPENED) {
653 if (sl_uncompress_tcp_core(mtod(m, u_char *),
655 TYPE_UNCOMPRESSED_TCP,
657 &iphdr, &hlen) != 0) {
660 SPP_FMT "VJ uncompress failed on uncompressed packet\n",
671 if (sp->pp_phase == PHASE_NETWORK)
672 sppp_cp_input(&ipv6cp, sp, m);
678 if (sp->state[IDX_IPV6CP] == STATE_OPENED)
685 /* IPX IPXCP not implemented yet */
686 if (sp->pp_phase == PHASE_NETWORK)
693 case CISCO_MULTICAST:
695 /* Don't check the control field here (RFC 1547). */
696 if (sp->pp_mode != IFF_CISCO) {
699 SPP_FMT "Cisco packet in PPP mode "
700 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
702 h->address, h->control, ntohs(h->protocol));
705 switch (ntohs (h->protocol)) {
709 case CISCO_KEEPALIVE:
710 sppp_cisco_input (sp, m);
734 default: /* Invalid PPP packet. */
738 SPP_FMT "invalid input packet "
739 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
741 h->address, h->control, ntohs(h->protocol));
745 if (! (ifp->if_flags & IFF_UP) || isr == -1)
750 if (netisr_queue(isr, m)) { /* (0) on success. */
752 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
759 * Do only account for network packets, not for control
760 * packets. This is used by some subsystems to detect
763 sp->pp_last_recv = time_uptime;
767 sppp_ifstart_sched(void *dummy)
769 struct sppp *sp = dummy;
771 sp->if_start(SP2IFP(sp));
774 /* if_start () wrapper function. We use it to schedule real if_start () for
775 * execution. We can't call it directly
778 sppp_ifstart(struct ifnet *ifp)
780 struct sppp *sp = IFP2SP(ifp);
782 if (SPPP_LOCK_OWNED(sp)) {
783 if (callout_pending(&sp->ifstart_callout))
785 callout_reset(&sp->ifstart_callout, 1, sppp_ifstart_sched,
793 * Enqueue transmit packet.
796 sppp_output(struct ifnet *ifp, struct mbuf *m,
797 struct sockaddr *dst, struct rtentry *rt)
799 struct sppp *sp = IFP2SP(ifp);
800 struct ppp_header *h;
801 struct ifqueue *ifq = NULL;
802 int s, error, rv = 0;
803 int ipproto = PPP_IP;
804 int debug = ifp->if_flags & IFF_DEBUG;
809 if (!(ifp->if_flags & IFF_UP) ||
810 (!(ifp->if_flags & IFF_AUTO) &&
811 !(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
821 if ((ifp->if_flags & IFF_AUTO) &&
822 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
827 * Hack to prevent the initialization-time generated
828 * IPv6 multicast packet to erroneously cause a
829 * dialout event in case IPv6 has been
830 * administratively disabled on that interface.
832 if (dst->sa_family == AF_INET6 &&
833 !(sp->confflags & CONF_ENABLE_IPV6))
837 * Interface is not yet running, but auto-dial. Need
838 * to start LCP for it.
840 ifp->if_drv_flags |= IFF_DRV_RUNNING;
847 if (dst->sa_family == AF_INET) {
848 /* XXX Check mbuf length here? */
849 struct ip *ip = mtod (m, struct ip*);
850 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
853 * When using dynamic local IP address assignment by using
854 * 0.0.0.0 as a local address, the first TCP session will
855 * not connect because the local TCP checksum is computed
856 * using 0.0.0.0 which will later become our real IP address
857 * so the TCP checksum computed at the remote end will
858 * become invalid. So we
859 * - don't let packets with src ip addr 0 thru
860 * - we flag TCP packets with src ip 0 as an error
863 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
868 if(ip->ip_p == IPPROTO_TCP)
869 return(EADDRNOTAVAIL);
875 * Put low delay, telnet, rlogin and ftp control packets
876 * in front of the queue or let ALTQ take care.
878 if (ALTQ_IS_ENABLED(&ifp->if_snd))
880 else if (_IF_QFULL(&sp->pp_fastq))
882 else if (ip->ip_tos & IPTOS_LOWDELAY)
884 else if (m->m_len < sizeof *ip + sizeof *tcp)
886 else if (ip->ip_p != IPPROTO_TCP)
888 else if (INTERACTIVE (ntohs (tcp->th_sport)))
890 else if (INTERACTIVE (ntohs (tcp->th_dport)))
894 * Do IP Header compression
896 if (sp->pp_mode != IFF_CISCO && sp->pp_mode != PP_FR &&
897 (sp->ipcp.flags & IPCP_VJ) && ip->ip_p == IPPROTO_TCP)
898 switch (sl_compress_tcp(m, ip, sp->pp_comp,
899 sp->ipcp.compress_cid)) {
900 case TYPE_COMPRESSED_TCP:
901 ipproto = PPP_VJ_COMP;
903 case TYPE_UNCOMPRESSED_TCP:
904 ipproto = PPP_VJ_UCOMP;
919 if (dst->sa_family == AF_INET6) {
920 /* XXX do something tricky here? */
924 if (sp->pp_mode == PP_FR) {
925 /* Add frame relay header. */
926 m = sppp_fr_header (sp, m, dst->sa_family);
933 * Prepend general data packet PPP header. For now, IP only.
935 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
938 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
946 * May want to check size of packet
947 * (albeit due to the implementation it's always enough)
949 h = mtod (m, struct ppp_header*);
950 if (sp->pp_mode == IFF_CISCO) {
951 h->address = CISCO_UNICAST; /* unicast address */
954 h->address = PPP_ALLSTATIONS; /* broadcast address */
955 h->control = PPP_UI; /* Unnumbered Info */
958 switch (dst->sa_family) {
960 case AF_INET: /* Internet Protocol */
961 if (sp->pp_mode == IFF_CISCO)
962 h->protocol = htons (ETHERTYPE_IP);
965 * Don't choke with an ENETDOWN early. It's
966 * possible that we just started dialing out,
967 * so don't drop the packet immediately. If
968 * we notice that we run out of buffer space
969 * below, we will however remember that we are
970 * not ready to carry IP packets, and return
971 * ENETDOWN, as opposed to ENOBUFS.
973 h->protocol = htons(ipproto);
974 if (sp->state[IDX_IPCP] != STATE_OPENED)
980 case AF_INET6: /* Internet Protocol */
981 if (sp->pp_mode == IFF_CISCO)
982 h->protocol = htons (ETHERTYPE_IPV6);
985 * Don't choke with an ENETDOWN early. It's
986 * possible that we just started dialing out,
987 * so don't drop the packet immediately. If
988 * we notice that we run out of buffer space
989 * below, we will however remember that we are
990 * not ready to carry IP packets, and return
991 * ENETDOWN, as opposed to ENOBUFS.
993 h->protocol = htons(PPP_IPV6);
994 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
1000 case AF_IPX: /* Novell IPX Protocol */
1001 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
1002 ETHERTYPE_IPX : PPP_IPX);
1010 return (EAFNOSUPPORT);
1014 * Queue message on interface, and start output if interface
1019 error = !(IF_HANDOFF_ADJ(ifq, m, ifp, 3));
1021 IFQ_HANDOFF_ADJ(ifp, m, 3, error);
1026 return (rv? rv: ENOBUFS);
1031 * Unlike in sppp_input(), we can always bump the timestamp
1032 * here since sppp_output() is only called on behalf of
1033 * network-layer traffic; control-layer traffic is handled
1034 * by sppp_cp_send().
1036 sp->pp_last_sent = time_uptime;
1041 sppp_attach(struct ifnet *ifp)
1043 struct sppp *sp = IFP2SP(ifp);
1045 /* Initialize mtx lock */
1046 mtx_init(&sp->mtx, "sppp", MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
1048 /* Initialize keepalive handler. */
1049 callout_init(&sp->keepalive_callout,
1050 (ifp->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
1051 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
1054 ifp->if_mtu = PP_MTU;
1055 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
1056 ifp->if_output = sppp_output;
1058 sp->pp_flags = PP_KEEPALIVE;
1060 ifp->if_snd.ifq_maxlen = 32;
1061 sp->pp_fastq.ifq_maxlen = 32;
1062 sp->pp_cpq.ifq_maxlen = 20;
1064 sp->pp_alivecnt = 0;
1065 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
1066 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
1067 sp->pp_phase = PHASE_DEAD;
1068 sp->pp_up = sppp_pp_up;
1069 sp->pp_down = sppp_pp_down;
1070 if(!mtx_initialized(&sp->pp_cpq.ifq_mtx))
1071 mtx_init(&sp->pp_cpq.ifq_mtx, "sppp_cpq", NULL, MTX_DEF);
1072 if(!mtx_initialized(&sp->pp_fastq.ifq_mtx))
1073 mtx_init(&sp->pp_fastq.ifq_mtx, "sppp_fastq", NULL, MTX_DEF);
1074 sp->pp_last_recv = sp->pp_last_sent = time_uptime;
1077 sp->confflags |= CONF_ENABLE_VJ;
1080 sp->confflags |= CONF_ENABLE_IPV6;
1082 callout_init(&sp->ifstart_callout,
1083 (ifp->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
1084 sp->if_start = ifp->if_start;
1085 ifp->if_start = sppp_ifstart;
1086 sp->pp_comp = malloc(sizeof(struct slcompress), M_TEMP, M_WAITOK);
1087 sl_compress_init(sp->pp_comp, -1);
1090 sppp_ipv6cp_init(sp);
1096 sppp_detach(struct ifnet *ifp)
1098 struct sppp *sp = IFP2SP(ifp);
1101 KASSERT(mtx_initialized(&sp->mtx), ("sppp mutex is not initialized"));
1103 /* Stop keepalive handler. */
1104 if (!callout_drain(&sp->keepalive_callout))
1105 callout_stop(&sp->keepalive_callout);
1107 for (i = 0; i < IDX_COUNT; i++) {
1108 if (!callout_drain(&sp->ch[i]))
1109 callout_stop(&sp->ch[i]);
1111 if (!callout_drain(&sp->pap_my_to_ch))
1112 callout_stop(&sp->pap_my_to_ch);
1113 mtx_destroy(&sp->pp_cpq.ifq_mtx);
1114 mtx_destroy(&sp->pp_fastq.ifq_mtx);
1115 mtx_destroy(&sp->mtx);
1119 * Flush the interface output queue.
1122 sppp_flush_unlocked(struct ifnet *ifp)
1124 struct sppp *sp = IFP2SP(ifp);
1126 sppp_qflush ((struct ifqueue *)&SP2IFP(sp)->if_snd);
1127 sppp_qflush (&sp->pp_fastq);
1128 sppp_qflush (&sp->pp_cpq);
1132 sppp_flush(struct ifnet *ifp)
1134 struct sppp *sp = IFP2SP(ifp);
1137 sppp_flush_unlocked (ifp);
1142 * Check if the output queue is empty.
1145 sppp_isempty(struct ifnet *ifp)
1147 struct sppp *sp = IFP2SP(ifp);
1152 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
1153 !SP2IFP(sp)->if_snd.ifq_head;
1160 * Get next packet to send.
1163 sppp_dequeue(struct ifnet *ifp)
1165 struct sppp *sp = IFP2SP(ifp);
1172 * Process only the control protocol queue until we have at
1173 * least one NCP open.
1175 * Do always serve all three queues in Cisco mode.
1177 IF_DEQUEUE(&sp->pp_cpq, m);
1179 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO ||
1180 sp->pp_mode == PP_FR)) {
1181 IF_DEQUEUE(&sp->pp_fastq, m);
1183 IF_DEQUEUE (&SP2IFP(sp)->if_snd, m);
1191 * Pick the next packet, do not remove it from the queue.
1194 sppp_pick(struct ifnet *ifp)
1196 struct sppp *sp = IFP2SP(ifp);
1203 m = sp->pp_cpq.ifq_head;
1205 (sp->pp_phase == PHASE_NETWORK ||
1206 sp->pp_mode == IFF_CISCO ||
1207 sp->pp_mode == PP_FR))
1208 if ((m = sp->pp_fastq.ifq_head) == NULL)
1209 m = SP2IFP(sp)->if_snd.ifq_head;
1216 * Process an ioctl request. Called on low priority level.
1219 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1221 struct ifreq *ifr = (struct ifreq*) data;
1222 struct sppp *sp = IFP2SP(ifp);
1223 int s, rv, going_up, going_down, newmode;
1230 case SIOCSIFDSTADDR:
1234 /* set the interface "up" when assigning an IP address */
1235 ifp->if_flags |= IFF_UP;
1239 going_up = ifp->if_flags & IFF_UP &&
1240 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0;
1241 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1242 ifp->if_drv_flags & IFF_DRV_RUNNING;
1244 newmode = ifp->if_flags & IFF_PASSIVE;
1246 newmode = ifp->if_flags & IFF_AUTO;
1248 newmode = ifp->if_flags & IFF_CISCO;
1249 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1250 ifp->if_flags |= newmode;
1253 newmode = sp->pp_flags & PP_FR;
1255 if (newmode != sp->pp_mode) {
1258 going_up = ifp->if_drv_flags & IFF_DRV_RUNNING;
1262 if (sp->pp_mode != IFF_CISCO &&
1263 sp->pp_mode != PP_FR)
1265 else if (sp->pp_tlf)
1267 sppp_flush_unlocked(ifp);
1268 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1269 sp->pp_mode = newmode;
1273 if (sp->pp_mode != IFF_CISCO &&
1274 sp->pp_mode != PP_FR)
1276 sp->pp_mode = newmode;
1277 if (sp->pp_mode == 0) {
1278 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1281 if ((sp->pp_mode == IFF_CISCO) ||
1282 (sp->pp_mode == PP_FR)) {
1285 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1293 #define ifr_mtu ifr_metric
1296 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1298 ifp->if_mtu = ifr->ifr_mtu;
1303 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1305 ifp->if_mtu = *(short*)data;
1310 ifr->ifr_mtu = ifp->if_mtu;
1315 *(short*)data = ifp->if_mtu;
1322 case SIOCGIFGENERIC:
1323 case SIOCSIFGENERIC:
1324 rv = sppp_params(sp, cmd, data);
1336 * Cisco framing implementation.
1340 * Handle incoming Cisco keepalive protocol packets.
1343 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1346 struct cisco_packet *h;
1349 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1352 SPP_FMT "cisco invalid packet length: %d bytes\n",
1353 SPP_ARGS(ifp), m->m_pkthdr.len);
1356 h = mtod (m, struct cisco_packet*);
1359 SPP_FMT "cisco input: %d bytes "
1360 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1361 SPP_ARGS(ifp), m->m_pkthdr.len,
1362 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1363 (u_int)h->time0, (u_int)h->time1);
1364 switch (ntohl (h->type)) {
1367 log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n",
1368 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1370 case CISCO_ADDR_REPLY:
1371 /* Reply on address request, ignore */
1373 case CISCO_KEEPALIVE_REQ:
1374 sp->pp_alivecnt = 0;
1375 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1376 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1377 /* Local and remote sequence numbers are equal.
1378 * Probably, the line is in loopback mode. */
1379 if (sp->pp_loopcnt >= MAXALIVECNT) {
1380 printf (SPP_FMT "loopback\n",
1383 if (ifp->if_flags & IFF_UP) {
1385 sppp_qflush (&sp->pp_cpq);
1390 /* Generate new local sequence number */
1391 sp->pp_seq[IDX_LCP] = random();
1395 if (! (ifp->if_flags & IFF_UP) &&
1396 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1398 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1401 case CISCO_ADDR_REQ:
1402 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1404 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1410 * Send Cisco keepalive packet.
1413 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1416 struct ppp_header *h;
1417 struct cisco_packet *ch;
1421 getmicrouptime(&tv);
1423 MGETHDR (m, M_DONTWAIT, MT_DATA);
1426 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1427 m->m_pkthdr.rcvif = 0;
1429 h = mtod (m, struct ppp_header*);
1430 h->address = CISCO_MULTICAST;
1432 h->protocol = htons (CISCO_KEEPALIVE);
1434 ch = (struct cisco_packet*) (h + 1);
1435 ch->type = htonl (type);
1436 ch->par1 = htonl (par1);
1437 ch->par2 = htonl (par2);
1440 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1441 ch->time1 = htons ((u_short) tv.tv_sec);
1445 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1446 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1447 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1449 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1454 * PPP protocol implementation.
1458 * Send PPP control protocol packet.
1461 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1462 u_char ident, u_short len, void *data)
1465 struct ppp_header *h;
1466 struct lcp_header *lh;
1469 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1470 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1471 MGETHDR (m, M_DONTWAIT, MT_DATA);
1474 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1475 m->m_pkthdr.rcvif = 0;
1477 h = mtod (m, struct ppp_header*);
1478 h->address = PPP_ALLSTATIONS; /* broadcast address */
1479 h->control = PPP_UI; /* Unnumbered Info */
1480 h->protocol = htons (proto); /* Link Control Protocol */
1482 lh = (struct lcp_header*) (h + 1);
1485 lh->len = htons (LCP_HEADER_LEN + len);
1487 bcopy (data, lh+1, len);
1490 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1492 sppp_proto_name(proto),
1493 sppp_cp_type_name (lh->type), lh->ident,
1495 sppp_print_bytes ((u_char*) (lh+1), len);
1498 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1503 * Handle incoming PPP control protocol packets.
1506 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1509 struct lcp_header *h;
1510 int len = m->m_pkthdr.len;
1517 SPP_FMT "%s invalid packet length: %d bytes\n",
1518 SPP_ARGS(ifp), cp->name, len);
1521 h = mtod (m, struct lcp_header*);
1524 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1525 SPP_ARGS(ifp), cp->name,
1526 sppp_state_name(sp->state[cp->protoidx]),
1527 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1528 sppp_print_bytes ((u_char*) (h+1), len-4);
1531 if (len > ntohs (h->len))
1532 len = ntohs (h->len);
1533 p = (u_char *)(h + 1);
1538 log(-1, SPP_FMT "%s invalid conf-req length %d\n",
1539 SPP_ARGS(ifp), cp->name,
1544 /* handle states where RCR doesn't get a SCA/SCN */
1545 switch (sp->state[cp->protoidx]) {
1547 case STATE_STOPPING:
1550 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1554 rv = (cp->RCR)(sp, h, len);
1555 switch (sp->state[cp->protoidx]) {
1560 case STATE_ACK_SENT:
1561 case STATE_REQ_SENT:
1563 * sppp_cp_change_state() have the side effect of
1564 * restarting the timeouts. We want to avoid that
1565 * if the state don't change, otherwise we won't
1566 * ever timeout and resend a configuration request
1569 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1572 sppp_cp_change_state(cp, sp, rv?
1573 STATE_ACK_SENT: STATE_REQ_SENT);
1576 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1578 sppp_cp_change_state(cp, sp, rv?
1579 STATE_ACK_SENT: STATE_REQ_SENT);
1581 case STATE_ACK_RCVD:
1583 sppp_cp_change_state(cp, sp, STATE_OPENED);
1585 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1590 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1593 printf(SPP_FMT "%s illegal %s in state %s\n",
1594 SPP_ARGS(ifp), cp->name,
1595 sppp_cp_type_name(h->type),
1596 sppp_state_name(sp->state[cp->protoidx]));
1601 if (h->ident != sp->confid[cp->protoidx]) {
1603 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1604 SPP_ARGS(ifp), cp->name,
1605 h->ident, sp->confid[cp->protoidx]);
1609 switch (sp->state[cp->protoidx]) {
1612 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1615 case STATE_STOPPING:
1617 case STATE_REQ_SENT:
1618 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1619 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1624 case STATE_ACK_RCVD:
1626 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1628 case STATE_ACK_SENT:
1629 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1630 sppp_cp_change_state(cp, sp, STATE_OPENED);
1632 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1633 SPP_ARGS(ifp), cp->name);
1637 printf(SPP_FMT "%s illegal %s in state %s\n",
1638 SPP_ARGS(ifp), cp->name,
1639 sppp_cp_type_name(h->type),
1640 sppp_state_name(sp->state[cp->protoidx]));
1646 if (h->ident != sp->confid[cp->protoidx]) {
1648 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1649 SPP_ARGS(ifp), cp->name,
1650 h->ident, sp->confid[cp->protoidx]);
1654 if (h->type == CONF_NAK)
1655 (cp->RCN_nak)(sp, h, len);
1657 (cp->RCN_rej)(sp, h, len);
1659 switch (sp->state[cp->protoidx]) {
1662 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1664 case STATE_REQ_SENT:
1665 case STATE_ACK_SENT:
1666 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1668 * Slow things down a bit if we think we might be
1669 * in loopback. Depend on the timeout to send the
1670 * next configuration request.
1679 case STATE_ACK_RCVD:
1680 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1684 case STATE_STOPPING:
1687 printf(SPP_FMT "%s illegal %s in state %s\n",
1688 SPP_ARGS(ifp), cp->name,
1689 sppp_cp_type_name(h->type),
1690 sppp_state_name(sp->state[cp->protoidx]));
1696 switch (sp->state[cp->protoidx]) {
1697 case STATE_ACK_RCVD:
1698 case STATE_ACK_SENT:
1699 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1704 case STATE_STOPPING:
1705 case STATE_REQ_SENT:
1707 /* Send Terminate-Ack packet. */
1709 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1710 SPP_ARGS(ifp), cp->name);
1711 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1715 sp->rst_counter[cp->protoidx] = 0;
1716 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1720 printf(SPP_FMT "%s illegal %s in state %s\n",
1721 SPP_ARGS(ifp), cp->name,
1722 sppp_cp_type_name(h->type),
1723 sppp_state_name(sp->state[cp->protoidx]));
1728 switch (sp->state[cp->protoidx]) {
1731 case STATE_REQ_SENT:
1732 case STATE_ACK_SENT:
1735 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1738 case STATE_STOPPING:
1739 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1742 case STATE_ACK_RCVD:
1743 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1748 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1751 printf(SPP_FMT "%s illegal %s in state %s\n",
1752 SPP_ARGS(ifp), cp->name,
1753 sppp_cp_type_name(h->type),
1754 sppp_state_name(sp->state[cp->protoidx]));
1759 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1761 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1762 "danger will robinson\n",
1763 SPP_ARGS(ifp), cp->name,
1764 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1765 switch (sp->state[cp->protoidx]) {
1768 case STATE_REQ_SENT:
1769 case STATE_ACK_SENT:
1771 case STATE_STOPPING:
1774 case STATE_ACK_RCVD:
1775 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1778 printf(SPP_FMT "%s illegal %s in state %s\n",
1779 SPP_ARGS(ifp), cp->name,
1780 sppp_cp_type_name(h->type),
1781 sppp_state_name(sp->state[cp->protoidx]));
1788 const struct cp *upper;
1794 proto = ntohs(*((u_int16_t *)p));
1795 for (i = 0; i < IDX_COUNT; i++) {
1796 if (cps[i]->proto == proto) {
1804 if (catastrophic || debug)
1805 log(catastrophic? LOG_INFO: LOG_DEBUG,
1806 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1807 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1808 sppp_cp_type_name(h->type), proto,
1809 upper ? upper->name : "unknown",
1810 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1813 * if we got RXJ+ against conf-req, the peer does not implement
1814 * this particular protocol type. terminate the protocol.
1816 if (upper && !catastrophic) {
1817 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1823 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1824 switch (sp->state[cp->protoidx]) {
1827 case STATE_REQ_SENT:
1828 case STATE_ACK_SENT:
1830 case STATE_STOPPING:
1833 case STATE_ACK_RCVD:
1834 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1837 printf(SPP_FMT "%s illegal %s in state %s\n",
1838 SPP_ARGS(ifp), cp->name,
1839 sppp_cp_type_name(h->type),
1840 sppp_state_name(sp->state[cp->protoidx]));
1846 if (cp->proto != PPP_LCP)
1848 /* Discard the packet. */
1851 if (cp->proto != PPP_LCP)
1853 if (sp->state[cp->protoidx] != STATE_OPENED) {
1855 log(-1, SPP_FMT "lcp echo req but lcp closed\n",
1862 log(-1, SPP_FMT "invalid lcp echo request "
1863 "packet length: %d bytes\n",
1864 SPP_ARGS(ifp), len);
1867 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1868 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1869 /* Line loopback mode detected. */
1870 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1871 sp->pp_loopcnt = MAXALIVECNT * 5;
1873 sppp_qflush (&sp->pp_cpq);
1875 /* Shut down the PPP link. */
1881 *(long*)(h+1) = htonl (sp->lcp.magic);
1883 log(-1, SPP_FMT "got lcp echo req, sending echo rep\n",
1885 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1888 if (cp->proto != PPP_LCP)
1890 if (h->ident != sp->lcp.echoid) {
1896 log(-1, SPP_FMT "lcp invalid echo reply "
1897 "packet length: %d bytes\n",
1898 SPP_ARGS(ifp), len);
1902 log(-1, SPP_FMT "lcp got echo rep\n",
1904 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1905 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1906 sp->pp_alivecnt = 0;
1909 /* Unknown packet type -- send Code-Reject packet. */
1912 log(-1, SPP_FMT "%s send code-rej for 0x%x\n",
1913 SPP_ARGS(ifp), cp->name, h->type);
1914 sppp_cp_send(sp, cp->proto, CODE_REJ,
1915 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1922 * The generic part of all Up/Down/Open/Close/TO event handlers.
1923 * Basically, the state transition handling in the automaton.
1926 sppp_up_event(const struct cp *cp, struct sppp *sp)
1931 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1932 SPP_ARGS(ifp), cp->name,
1933 sppp_state_name(sp->state[cp->protoidx]));
1935 switch (sp->state[cp->protoidx]) {
1937 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1939 case STATE_STARTING:
1940 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1942 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1945 printf(SPP_FMT "%s illegal up in state %s\n",
1946 SPP_ARGS(ifp), cp->name,
1947 sppp_state_name(sp->state[cp->protoidx]));
1952 sppp_down_event(const struct cp *cp, struct sppp *sp)
1957 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1958 SPP_ARGS(ifp), cp->name,
1959 sppp_state_name(sp->state[cp->protoidx]));
1961 switch (sp->state[cp->protoidx]) {
1964 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1967 sppp_cp_change_state(cp, sp, STATE_STARTING);
1970 case STATE_STOPPING:
1971 case STATE_REQ_SENT:
1972 case STATE_ACK_RCVD:
1973 case STATE_ACK_SENT:
1974 sppp_cp_change_state(cp, sp, STATE_STARTING);
1978 sppp_cp_change_state(cp, sp, STATE_STARTING);
1981 printf(SPP_FMT "%s illegal down in state %s\n",
1982 SPP_ARGS(ifp), cp->name,
1983 sppp_state_name(sp->state[cp->protoidx]));
1989 sppp_open_event(const struct cp *cp, struct sppp *sp)
1994 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1995 SPP_ARGS(ifp), cp->name,
1996 sppp_state_name(sp->state[cp->protoidx]));
1998 switch (sp->state[cp->protoidx]) {
2000 sppp_cp_change_state(cp, sp, STATE_STARTING);
2003 case STATE_STARTING:
2006 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
2008 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2012 * Try escaping stopped state. This seems to bite
2013 * people occasionally, in particular for IPCP,
2014 * presumably following previous IPCP negotiation
2015 * aborts. Somehow, we must have missed a Down event
2016 * which would have caused a transition into starting
2017 * state, so as a bandaid we force the Down event now.
2018 * This effectively implements (something like the)
2019 * `restart' option mentioned in the state transition
2020 * table of RFC 1661.
2022 sppp_cp_change_state(cp, sp, STATE_STARTING);
2025 case STATE_STOPPING:
2026 case STATE_REQ_SENT:
2027 case STATE_ACK_RCVD:
2028 case STATE_ACK_SENT:
2032 sppp_cp_change_state(cp, sp, STATE_STOPPING);
2039 sppp_close_event(const struct cp *cp, struct sppp *sp)
2044 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
2045 SPP_ARGS(ifp), cp->name,
2046 sppp_state_name(sp->state[cp->protoidx]));
2048 switch (sp->state[cp->protoidx]) {
2053 case STATE_STARTING:
2054 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2058 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2060 case STATE_STOPPING:
2061 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2066 case STATE_REQ_SENT:
2067 case STATE_ACK_RCVD:
2068 case STATE_ACK_SENT:
2069 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2070 sppp_cp_send(sp, cp->proto, TERM_REQ,
2071 ++sp->pp_seq[cp->protoidx], 0, 0);
2072 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2078 sppp_to_event(const struct cp *cp, struct sppp *sp)
2086 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
2087 SPP_ARGS(ifp), cp->name,
2088 sppp_state_name(sp->state[cp->protoidx]),
2089 sp->rst_counter[cp->protoidx]);
2091 if (--sp->rst_counter[cp->protoidx] < 0)
2093 switch (sp->state[cp->protoidx]) {
2095 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2098 case STATE_STOPPING:
2099 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2102 case STATE_REQ_SENT:
2103 case STATE_ACK_RCVD:
2104 case STATE_ACK_SENT:
2105 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2111 switch (sp->state[cp->protoidx]) {
2113 case STATE_STOPPING:
2114 sppp_cp_send(sp, cp->proto, TERM_REQ,
2115 ++sp->pp_seq[cp->protoidx], 0, 0);
2116 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2117 cp->TO, (void *)sp);
2119 case STATE_REQ_SENT:
2120 case STATE_ACK_RCVD:
2122 /* sppp_cp_change_state() will restart the timer */
2123 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2125 case STATE_ACK_SENT:
2127 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2128 cp->TO, (void *)sp);
2137 * Change the state of a control protocol in the state automaton.
2138 * Takes care of starting/stopping the restart timer.
2141 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2143 sp->state[cp->protoidx] = newstate;
2145 callout_stop (&sp->ch[cp->protoidx]);
2149 case STATE_STARTING:
2155 case STATE_STOPPING:
2156 case STATE_REQ_SENT:
2157 case STATE_ACK_RCVD:
2158 case STATE_ACK_SENT:
2159 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2160 cp->TO, (void *)sp);
2166 *--------------------------------------------------------------------------*
2168 * The LCP implementation. *
2170 *--------------------------------------------------------------------------*
2173 sppp_pp_up(struct sppp *sp)
2181 sppp_pp_down(struct sppp *sp)
2189 sppp_lcp_init(struct sppp *sp)
2191 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2193 sp->state[IDX_LCP] = STATE_INITIAL;
2194 sp->fail_counter[IDX_LCP] = 0;
2195 sp->pp_seq[IDX_LCP] = 0;
2196 sp->pp_rseq[IDX_LCP] = 0;
2198 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2200 /* Note that these values are relevant for all control protocols */
2201 sp->lcp.timeout = 3 * hz;
2202 sp->lcp.max_terminate = 2;
2203 sp->lcp.max_configure = 10;
2204 sp->lcp.max_failure = 10;
2205 callout_init(&sp->ch[IDX_LCP],
2206 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
2210 sppp_lcp_up(struct sppp *sp)
2214 sp->pp_alivecnt = 0;
2215 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2218 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2220 * If we are authenticator, negotiate LCP_AUTH
2222 if (sp->hisauth.proto != 0)
2223 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2225 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2226 sp->pp_flags &= ~PP_NEEDAUTH;
2228 * If this interface is passive or dial-on-demand, and we are
2229 * still in Initial state, it means we've got an incoming
2230 * call. Activate the interface.
2232 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2235 SPP_FMT "Up event", SPP_ARGS(ifp));
2236 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2237 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2239 log(-1, "(incoming call)\n");
2240 sp->pp_flags |= PP_CALLIN;
2244 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2245 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2246 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2250 sppp_up_event(&lcp, sp);
2254 sppp_lcp_down(struct sppp *sp)
2258 sppp_down_event(&lcp, sp);
2261 * If this is neither a dial-on-demand nor a passive
2262 * interface, simulate an ``ifconfig down'' action, so the
2263 * administrator can force a redial by another ``ifconfig
2264 * up''. XXX For leased line operation, should we immediately
2265 * try to reopen the connection here?
2267 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2269 SPP_FMT "Down event, taking interface down.\n",
2275 SPP_FMT "Down event (carrier loss)\n",
2277 sp->pp_flags &= ~PP_CALLIN;
2278 if (sp->state[IDX_LCP] != STATE_INITIAL)
2280 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2285 sppp_lcp_open(struct sppp *sp)
2287 sppp_open_event(&lcp, sp);
2291 sppp_lcp_close(struct sppp *sp)
2293 sppp_close_event(&lcp, sp);
2297 sppp_lcp_TO(void *cookie)
2299 sppp_to_event(&lcp, (struct sppp *)cookie);
2303 * Analyze a configure request. Return true if it was agreeable, and
2304 * caused action sca, false if it has been rejected or nak'ed, and
2305 * caused action scn. (The return value is used to make the state
2306 * transition decision in the state automaton.)
2309 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2312 u_char *buf, *r, *p;
2319 buf = r = malloc (len, M_TEMP, M_NOWAIT);
2324 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2327 /* pass 1: check for things that need to be rejected */
2329 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2330 len-=p[1], p+=p[1]) {
2332 log(-1, " %s ", sppp_lcp_opt_name(*p));
2336 if (len >= 6 && p[1] == 6)
2339 log(-1, "[invalid] ");
2341 case LCP_OPT_ASYNC_MAP:
2342 /* Async control character map. */
2343 if (len >= 6 && p[1] == 6)
2346 log(-1, "[invalid] ");
2349 /* Maximum receive unit. */
2350 if (len >= 4 && p[1] == 4)
2353 log(-1, "[invalid] ");
2355 case LCP_OPT_AUTH_PROTO:
2358 log(-1, "[invalid] ");
2361 authproto = (p[2] << 8) + p[3];
2362 if (authproto == PPP_CHAP && p[1] != 5) {
2364 log(-1, "[invalid chap len] ");
2367 if (sp->myauth.proto == 0) {
2368 /* we are not configured to do auth */
2370 log(-1, "[not configured] ");
2374 * Remote want us to authenticate, remember this,
2375 * so we stay in PHASE_AUTHENTICATE after LCP got
2378 sp->pp_flags |= PP_NEEDAUTH;
2381 /* Others not supported. */
2386 /* Add the option to rejected list. */
2393 log(-1, " send conf-rej\n");
2394 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2400 * pass 2: check for option values that are unacceptable and
2401 * thus require to be nak'ed.
2404 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2409 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2410 len-=p[1], p+=p[1]) {
2412 log(-1, " %s ", sppp_lcp_opt_name(*p));
2415 /* Magic number -- extract. */
2416 nmagic = (u_long)p[2] << 24 |
2417 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2418 if (nmagic != sp->lcp.magic) {
2421 log(-1, "0x%lx ", nmagic);
2424 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2425 log(-1, "[glitch] ");
2428 * We negate our magic here, and NAK it. If
2429 * we see it later in an NAK packet, we
2430 * suggest a new one.
2432 nmagic = ~sp->lcp.magic;
2434 p[2] = nmagic >> 24;
2435 p[3] = nmagic >> 16;
2440 case LCP_OPT_ASYNC_MAP:
2442 * Async control character map -- just ignore it.
2444 * Quote from RFC 1662, chapter 6:
2445 * To enable this functionality, synchronous PPP
2446 * implementations MUST always respond to the
2447 * Async-Control-Character-Map Configuration
2448 * Option with the LCP Configure-Ack. However,
2449 * acceptance of the Configuration Option does
2450 * not imply that the synchronous implementation
2451 * will do any ACCM mapping. Instead, all such
2452 * octet mapping will be performed by the
2453 * asynchronous-to-synchronous converter.
2459 * Maximum receive unit. Always agreeable,
2460 * but ignored by now.
2462 sp->lcp.their_mru = p[2] * 256 + p[3];
2464 log(-1, "%lu ", sp->lcp.their_mru);
2467 case LCP_OPT_AUTH_PROTO:
2468 authproto = (p[2] << 8) + p[3];
2469 if (sp->myauth.proto != authproto) {
2470 /* not agreed, nak */
2472 log(-1, "[mine %s != his %s] ",
2473 sppp_proto_name(sp->hisauth.proto),
2474 sppp_proto_name(authproto));
2475 p[2] = sp->myauth.proto >> 8;
2476 p[3] = sp->myauth.proto;
2479 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2481 log(-1, "[chap not MD5] ");
2487 /* Add the option to nak'ed list. */
2494 * Local and remote magics equal -- loopback?
2496 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2497 if (sp->pp_loopcnt == MAXALIVECNT*5)
2498 printf (SPP_FMT "loopback\n",
2500 if (ifp->if_flags & IFF_UP) {
2502 sppp_qflush(&sp->pp_cpq);
2507 } else if (!sp->pp_loopcnt &&
2508 ++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2510 log(-1, " max_failure (%d) exceeded, "
2512 sp->lcp.max_failure);
2513 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2516 log(-1, " send conf-nak\n");
2517 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2521 log(-1, " send conf-ack\n");
2522 sp->fail_counter[IDX_LCP] = 0;
2524 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2525 h->ident, origlen, h+1);
2533 * Analyze the LCP Configure-Reject option list, and adjust our
2537 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2543 buf = malloc (len, M_TEMP, M_NOWAIT);
2548 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2552 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2553 len -= p[1], p += p[1]) {
2555 log(-1, " %s ", sppp_lcp_opt_name(*p));
2558 /* Magic number -- can't use it, use 0 */
2559 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2564 * Should not be rejected anyway, since we only
2565 * negotiate a MRU if explicitly requested by
2568 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2570 case LCP_OPT_AUTH_PROTO:
2572 * Peer doesn't want to authenticate himself,
2573 * deny unless this is a dialout call, and
2574 * AUTHFLAG_NOCALLOUT is set.
2576 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2577 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2579 log(-1, "[don't insist on auth "
2581 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2585 log(-1, "[access denied]\n");
2597 * Analyze the LCP Configure-NAK option list, and adjust our
2601 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2608 buf = malloc (len, M_TEMP, M_NOWAIT);
2613 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2617 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2618 len -= p[1], p += p[1]) {
2620 log(-1, " %s ", sppp_lcp_opt_name(*p));
2623 /* Magic number -- renegotiate */
2624 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2625 len >= 6 && p[1] == 6) {
2626 magic = (u_long)p[2] << 24 |
2627 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2629 * If the remote magic is our negated one,
2630 * this looks like a loopback problem.
2631 * Suggest a new magic to make sure.
2633 if (magic == ~sp->lcp.magic) {
2635 log(-1, "magic glitch ");
2636 sp->lcp.magic = random();
2638 sp->lcp.magic = magic;
2640 log(-1, "%lu ", magic);
2646 * Peer wants to advise us to negotiate an MRU.
2647 * Agree on it if it's reasonable, or use
2648 * default otherwise.
2650 if (len >= 4 && p[1] == 4) {
2651 u_int mru = p[2] * 256 + p[3];
2653 log(-1, "%d ", mru);
2654 if (mru < PP_MTU || mru > PP_MAX_MRU)
2657 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2660 case LCP_OPT_AUTH_PROTO:
2662 * Peer doesn't like our authentication method,
2666 log(-1, "[access denied]\n");
2678 sppp_lcp_tlu(struct sppp *sp)
2685 if (! (ifp->if_flags & IFF_UP) &&
2686 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2687 /* Coming out of loopback mode. */
2689 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2692 for (i = 0; i < IDX_COUNT; i++)
2693 if ((cps[i])->flags & CP_QUAL)
2696 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2697 (sp->pp_flags & PP_NEEDAUTH) != 0)
2698 sp->pp_phase = PHASE_AUTHENTICATE;
2700 sp->pp_phase = PHASE_NETWORK;
2703 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2704 sppp_phase_name(sp->pp_phase));
2707 * Open all authentication protocols. This is even required
2708 * if we already proceeded to network phase, since it might be
2709 * that remote wants us to authenticate, so we might have to
2710 * send a PAP request. Undesired authentication protocols
2711 * don't do anything when they get an Open event.
2713 for (i = 0; i < IDX_COUNT; i++)
2714 if ((cps[i])->flags & CP_AUTH)
2717 if (sp->pp_phase == PHASE_NETWORK) {
2718 /* Notify all NCPs. */
2719 for (i = 0; i < IDX_COUNT; i++)
2720 if (((cps[i])->flags & CP_NCP) &&
2723 * Hack to administratively disable IPv6 if
2724 * not desired. Perhaps we should have another
2725 * flag for this, but right now, we can make
2726 * all struct cp's read/only.
2728 (cps[i] != &ipv6cp ||
2729 (sp->confflags & CONF_ENABLE_IPV6)))
2733 /* Send Up events to all started protos. */
2734 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2735 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2738 /* notify low-level driver of state change */
2740 sp->pp_chg(sp, (int)sp->pp_phase);
2742 if (sp->pp_phase == PHASE_NETWORK)
2743 /* if no NCP is starting, close down */
2744 sppp_lcp_check_and_close(sp);
2748 sppp_lcp_tld(struct sppp *sp)
2754 sp->pp_phase = PHASE_TERMINATE;
2757 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2758 sppp_phase_name(sp->pp_phase));
2761 * Take upper layers down. We send the Down event first and
2762 * the Close second to prevent the upper layers from sending
2763 * ``a flurry of terminate-request packets'', as the RFC
2766 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2767 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2769 (cps[i])->Close(sp);
2774 sppp_lcp_tls(struct sppp *sp)
2778 sp->pp_phase = PHASE_ESTABLISH;
2781 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2782 sppp_phase_name(sp->pp_phase));
2784 /* Notify lower layer if desired. */
2792 sppp_lcp_tlf(struct sppp *sp)
2796 sp->pp_phase = PHASE_DEAD;
2798 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2799 sppp_phase_name(sp->pp_phase));
2801 /* Notify lower layer if desired. */
2809 sppp_lcp_scr(struct sppp *sp)
2811 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2815 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2816 if (! sp->lcp.magic)
2817 sp->lcp.magic = random();
2818 opt[i++] = LCP_OPT_MAGIC;
2820 opt[i++] = sp->lcp.magic >> 24;
2821 opt[i++] = sp->lcp.magic >> 16;
2822 opt[i++] = sp->lcp.magic >> 8;
2823 opt[i++] = sp->lcp.magic;
2826 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2827 opt[i++] = LCP_OPT_MRU;
2829 opt[i++] = sp->lcp.mru >> 8;
2830 opt[i++] = sp->lcp.mru;
2833 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2834 authproto = sp->hisauth.proto;
2835 opt[i++] = LCP_OPT_AUTH_PROTO;
2836 opt[i++] = authproto == PPP_CHAP? 5: 4;
2837 opt[i++] = authproto >> 8;
2838 opt[i++] = authproto;
2839 if (authproto == PPP_CHAP)
2840 opt[i++] = CHAP_MD5;
2843 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2844 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2848 * Check the open NCPs, return true if at least one NCP is open.
2851 sppp_ncp_check(struct sppp *sp)
2855 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2856 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2862 * Re-check the open NCPs and see if we should terminate the link.
2863 * Called by the NCPs during their tlf action handling.
2866 sppp_lcp_check_and_close(struct sppp *sp)
2869 if (sp->pp_phase < PHASE_NETWORK)
2870 /* don't bother, we are already going down */
2873 if (sppp_ncp_check(sp))
2880 *--------------------------------------------------------------------------*
2882 * The IPCP implementation. *
2884 *--------------------------------------------------------------------------*
2888 sppp_ipcp_init(struct sppp *sp)
2892 sp->state[IDX_IPCP] = STATE_INITIAL;
2893 sp->fail_counter[IDX_IPCP] = 0;
2894 sp->pp_seq[IDX_IPCP] = 0;
2895 sp->pp_rseq[IDX_IPCP] = 0;
2896 callout_init(&sp->ch[IDX_IPCP],
2897 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
2901 sppp_ipcp_up(struct sppp *sp)
2903 sppp_up_event(&ipcp, sp);
2907 sppp_ipcp_down(struct sppp *sp)
2909 sppp_down_event(&ipcp, sp);
2913 sppp_ipcp_open(struct sppp *sp)
2916 u_long myaddr, hisaddr;
2918 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2919 IPCP_MYADDR_DYN | IPCP_VJ);
2922 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2924 * If we don't have his address, this probably means our
2925 * interface doesn't want to talk IP at all. (This could
2926 * be the case if somebody wants to speak only IPX, for
2927 * example.) Don't open IPCP in this case.
2929 if (hisaddr == 0L) {
2930 /* XXX this message should go away */
2932 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2938 * I don't have an assigned address, so i need to
2939 * negotiate my address.
2941 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2942 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2944 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2945 if (sp->confflags & CONF_ENABLE_VJ) {
2946 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2947 sp->ipcp.max_state = MAX_STATES - 1;
2948 sp->ipcp.compress_cid = 1;
2950 sppp_open_event(&ipcp, sp);
2954 sppp_ipcp_close(struct sppp *sp)
2956 sppp_close_event(&ipcp, sp);
2957 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2959 * My address was dynamic, clear it again.
2961 sppp_set_ip_addr(sp, 0L);
2965 sppp_ipcp_TO(void *cookie)
2967 sppp_to_event(&ipcp, (struct sppp *)cookie);
2971 * Analyze a configure request. Return true if it was agreeable, and
2972 * caused action sca, false if it has been rejected or nak'ed, and
2973 * caused action scn. (The return value is used to make the state
2974 * transition decision in the state automaton.)
2977 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2979 u_char *buf, *r, *p;
2980 struct ifnet *ifp = SP2IFP(sp);
2981 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2982 u_long hisaddr, desiredaddr;
2989 * Make sure to allocate a buf that can at least hold a
2990 * conf-nak with an `address' option. We might need it below.
2992 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2996 /* pass 1: see if we can recognize them */
2998 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
3001 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3002 len-=p[1], p+=p[1]) {
3004 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3006 case IPCP_OPT_COMPRESSION:
3007 if (!(sp->confflags & CONF_ENABLE_VJ)) {
3008 /* VJ compression administratively disabled */
3010 log(-1, "[locally disabled] ");
3014 * In theory, we should only conf-rej an
3015 * option that is shorter than RFC 1618
3016 * requires (i.e. < 4), and should conf-nak
3017 * anything else that is not VJ. However,
3018 * since our algorithm always uses the
3019 * original option to NAK it with new values,
3020 * things would become more complicated. In
3021 * pratice, the only commonly implemented IP
3022 * compression option is VJ anyway, so the
3023 * difference is negligible.
3025 if (len >= 6 && p[1] == 6) {
3027 * correctly formed compression option
3028 * that could be VJ compression
3034 "optlen %d [invalid/unsupported] ",
3037 case IPCP_OPT_ADDRESS:
3038 if (len >= 6 && p[1] == 6) {
3039 /* correctly formed address option */
3043 log(-1, "[invalid] ");
3046 /* Others not supported. */
3051 /* Add the option to rejected list. */
3058 log(-1, " send conf-rej\n");
3059 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
3064 /* pass 2: parse option values */
3065 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
3067 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
3071 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3072 len-=p[1], p+=p[1]) {
3074 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3076 case IPCP_OPT_COMPRESSION:
3077 desiredcomp = p[2] << 8 | p[3];
3078 /* We only support VJ */
3079 if (desiredcomp == IPCP_COMP_VJ) {
3081 log(-1, "VJ [ack] ");
3082 sp->ipcp.flags |= IPCP_VJ;
3083 sl_compress_init(sp->pp_comp, p[4]);
3084 sp->ipcp.max_state = p[4];
3085 sp->ipcp.compress_cid = p[5];
3090 "compproto %#04x [not supported] ",
3092 p[2] = IPCP_COMP_VJ >> 8;
3093 p[3] = IPCP_COMP_VJ;
3094 p[4] = sp->ipcp.max_state;
3095 p[5] = sp->ipcp.compress_cid;
3097 case IPCP_OPT_ADDRESS:
3098 /* This is the address he wants in his end */
3099 desiredaddr = p[2] << 24 | p[3] << 16 |
3101 if (desiredaddr == hisaddr ||
3102 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3104 * Peer's address is same as our value,
3105 * or we have set it to 0.0.0.* to
3106 * indicate that we do not really care,
3107 * this is agreeable. Gonna conf-ack
3111 log(-1, "%s [ack] ",
3112 sppp_dotted_quad(hisaddr));
3113 /* record that we've seen it already */
3114 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3118 * The address wasn't agreeable. This is either
3119 * he sent us 0.0.0.0, asking to assign him an
3120 * address, or he send us another address not
3121 * matching our value. Either case, we gonna
3122 * conf-nak it with our value.
3123 * XXX: we should "rej" if hisaddr == 0
3126 if (desiredaddr == 0)
3127 log(-1, "[addr requested] ");
3129 log(-1, "%s [not agreed] ",
3130 sppp_dotted_quad(desiredaddr));
3133 p[2] = hisaddr >> 24;
3134 p[3] = hisaddr >> 16;
3135 p[4] = hisaddr >> 8;
3139 /* Add the option to nak'ed list. */
3146 * If we are about to conf-ack the request, but haven't seen
3147 * his address so far, gonna conf-nak it instead, with the
3148 * `address' option present and our idea of his address being
3149 * filled in there, to request negotiation of both addresses.
3151 * XXX This can result in an endless req - nak loop if peer
3152 * doesn't want to send us his address. Q: What should we do
3153 * about it? XXX A: implement the max-failure counter.
3155 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3156 buf[0] = IPCP_OPT_ADDRESS;
3158 buf[2] = hisaddr >> 24;
3159 buf[3] = hisaddr >> 16;
3160 buf[4] = hisaddr >> 8;
3164 log(-1, "still need hisaddr ");
3169 log(-1, " send conf-nak\n");
3170 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3173 log(-1, " send conf-ack\n");
3174 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3175 h->ident, origlen, h+1);
3183 * Analyze the IPCP Configure-Reject option list, and adjust our
3187 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3190 struct ifnet *ifp = SP2IFP(sp);
3191 int debug = ifp->if_flags & IFF_DEBUG;
3194 buf = malloc (len, M_TEMP, M_NOWAIT);
3199 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3203 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3204 len -= p[1], p += p[1]) {
3206 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3208 case IPCP_OPT_COMPRESSION:
3209 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3211 case IPCP_OPT_ADDRESS:
3213 * Peer doesn't grok address option. This is
3214 * bad. XXX Should we better give up here?
3215 * XXX We could try old "addresses" option...
3217 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3228 * Analyze the IPCP Configure-NAK option list, and adjust our
3232 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3235 struct ifnet *ifp = SP2IFP(sp);
3236 int debug = ifp->if_flags & IFF_DEBUG;
3241 buf = malloc (len, M_TEMP, M_NOWAIT);
3246 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3250 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3251 len -= p[1], p += p[1]) {
3253 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3255 case IPCP_OPT_COMPRESSION:
3256 if (len >= 6 && p[1] == 6) {
3257 desiredcomp = p[2] << 8 | p[3];
3259 log(-1, "[wantcomp %#04x] ",
3261 if (desiredcomp == IPCP_COMP_VJ) {
3262 sl_compress_init(sp->pp_comp, p[4]);
3263 sp->ipcp.max_state = p[4];
3264 sp->ipcp.compress_cid = p[5];
3266 log(-1, "[agree] ");
3269 ~(1 << IPCP_OPT_COMPRESSION);
3272 case IPCP_OPT_ADDRESS:
3274 * Peer doesn't like our local IP address. See
3275 * if we can do something for him. We'll drop
3276 * him our address then.
3278 if (len >= 6 && p[1] == 6) {
3279 wantaddr = p[2] << 24 | p[3] << 16 |
3281 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3283 log(-1, "[wantaddr %s] ",
3284 sppp_dotted_quad(wantaddr));
3286 * When doing dynamic address assignment,
3287 * we accept his offer. Otherwise, we
3288 * ignore it and thus continue to negotiate
3289 * our already existing value.
3290 * XXX: Bogus, if he said no once, he'll
3291 * just say no again, might as well die.
3293 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3294 sppp_set_ip_addr(sp, wantaddr);
3296 log(-1, "[agree] ");
3297 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3310 sppp_ipcp_tlu(struct sppp *sp)
3312 /* we are up - notify isdn daemon */
3318 sppp_ipcp_tld(struct sppp *sp)
3323 sppp_ipcp_tls(struct sppp *sp)
3325 /* indicate to LCP that it must stay alive */
3326 sp->lcp.protos |= (1 << IDX_IPCP);
3330 sppp_ipcp_tlf(struct sppp *sp)
3332 /* we no longer need LCP */
3333 sp->lcp.protos &= ~(1 << IDX_IPCP);
3334 sppp_lcp_check_and_close(sp);
3338 sppp_ipcp_scr(struct sppp *sp)
3340 char opt[6 /* compression */ + 6 /* address */];
3344 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3345 opt[i++] = IPCP_OPT_COMPRESSION;
3347 opt[i++] = IPCP_COMP_VJ >> 8;
3348 opt[i++] = IPCP_COMP_VJ;
3349 opt[i++] = sp->ipcp.max_state;
3350 opt[i++] = sp->ipcp.compress_cid;
3352 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3353 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3354 opt[i++] = IPCP_OPT_ADDRESS;
3356 opt[i++] = ouraddr >> 24;
3357 opt[i++] = ouraddr >> 16;
3358 opt[i++] = ouraddr >> 8;
3362 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3363 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3367 *--------------------------------------------------------------------------*
3369 * The IPv6CP implementation. *
3371 *--------------------------------------------------------------------------*
3376 sppp_ipv6cp_init(struct sppp *sp)
3378 sp->ipv6cp.opts = 0;
3379 sp->ipv6cp.flags = 0;
3380 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3381 sp->fail_counter[IDX_IPV6CP] = 0;
3382 sp->pp_seq[IDX_IPV6CP] = 0;
3383 sp->pp_rseq[IDX_IPV6CP] = 0;
3384 callout_init(&sp->ch[IDX_IPV6CP],
3385 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
3389 sppp_ipv6cp_up(struct sppp *sp)
3391 sppp_up_event(&ipv6cp, sp);
3395 sppp_ipv6cp_down(struct sppp *sp)
3397 sppp_down_event(&ipv6cp, sp);
3401 sppp_ipv6cp_open(struct sppp *sp)
3404 struct in6_addr myaddr, hisaddr;
3406 #ifdef IPV6CP_MYIFID_DYN
3407 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3409 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3412 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3414 * If we don't have our address, this probably means our
3415 * interface doesn't want to talk IPv6 at all. (This could
3416 * be the case if somebody wants to speak only IPX, for
3417 * example.) Don't open IPv6CP in this case.
3419 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3420 /* XXX this message should go away */
3422 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3427 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3428 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3429 sppp_open_event(&ipv6cp, sp);
3433 sppp_ipv6cp_close(struct sppp *sp)
3435 sppp_close_event(&ipv6cp, sp);
3439 sppp_ipv6cp_TO(void *cookie)
3441 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3445 * Analyze a configure request. Return true if it was agreeable, and
3446 * caused action sca, false if it has been rejected or nak'ed, and
3447 * caused action scn. (The return value is used to make the state
3448 * transition decision in the state automaton.)
3451 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3453 u_char *buf, *r, *p;
3454 struct ifnet *ifp = SP2IFP(sp);
3455 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3456 struct in6_addr myaddr, desiredaddr, suggestaddr;
3459 int collision, nohisaddr;
3460 char ip6buf[INET6_ADDRSTRLEN];
3465 * Make sure to allocate a buf that can at least hold a
3466 * conf-nak with an `address' option. We might need it below.
3468 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3472 /* pass 1: see if we can recognize them */
3474 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3478 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3479 len-=p[1], p+=p[1]) {
3481 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3483 case IPV6CP_OPT_IFID:
3484 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3485 /* correctly formed address option */
3490 log(-1, " [invalid]");
3493 case IPV6CP_OPT_COMPRESSION:
3494 if (len >= 4 && p[1] >= 4) {
3495 /* correctly formed compress option */
3499 log(-1, " [invalid]");
3503 /* Others not supported. */
3508 /* Add the option to rejected list. */
3515 log(-1, " send conf-rej\n");
3516 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3521 /* pass 2: parse option values */
3522 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3524 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3529 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3530 len-=p[1], p+=p[1]) {
3532 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3535 case IPV6CP_OPT_COMPRESSION:
3538 case IPV6CP_OPT_IFID:
3539 bzero(&desiredaddr, sizeof(desiredaddr));
3540 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3541 collision = (bcmp(&desiredaddr.s6_addr[8],
3542 &myaddr.s6_addr[8], 8) == 0);
3543 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3545 desiredaddr.s6_addr16[0] = htons(0xfe80);
3546 (void)in6_setscope(&desiredaddr, SP2IFP(sp), NULL);
3548 if (!collision && !nohisaddr) {
3549 /* no collision, hisaddr known - Conf-Ack */
3554 ip6_sprintf(ip6buf, &desiredaddr),
3555 sppp_cp_type_name(type));
3560 bzero(&suggestaddr, sizeof(&suggestaddr));
3561 if (collision && nohisaddr) {
3562 /* collision, hisaddr unknown - Conf-Rej */
3567 * - no collision, hisaddr unknown, or
3568 * - collision, hisaddr known
3569 * Conf-Nak, suggest hisaddr
3572 sppp_suggest_ip6_addr(sp, &suggestaddr);
3573 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3577 ip6_sprintf(ip6buf, &desiredaddr),
3578 sppp_cp_type_name(type));
3581 /* Add the option to nak'ed list. */
3587 if (rlen == 0 && type == CONF_ACK) {
3589 log(-1, " send %s\n", sppp_cp_type_name(type));
3590 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3593 if (type == CONF_ACK)
3594 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3598 log(-1, " send %s suggest %s\n",
3599 sppp_cp_type_name(type),
3600 ip6_sprintf(ip6buf, &suggestaddr));
3602 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3611 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3615 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3618 struct ifnet *ifp = SP2IFP(sp);
3619 int debug = ifp->if_flags & IFF_DEBUG;
3622 buf = malloc (len, M_TEMP, M_NOWAIT);
3627 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3631 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3632 len -= p[1], p += p[1]) {
3634 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3636 case IPV6CP_OPT_IFID:
3638 * Peer doesn't grok address option. This is
3639 * bad. XXX Should we better give up here?
3641 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3644 case IPV6CP_OPT_COMPRESS:
3645 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3657 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3661 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3664 struct ifnet *ifp = SP2IFP(sp);
3665 int debug = ifp->if_flags & IFF_DEBUG;
3666 struct in6_addr suggestaddr;
3667 char ip6buf[INET6_ADDRSTRLEN];
3670 buf = malloc (len, M_TEMP, M_NOWAIT);
3675 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3679 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3680 len -= p[1], p += p[1]) {
3682 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3684 case IPV6CP_OPT_IFID:
3686 * Peer doesn't like our local ifid. See
3687 * if we can do something for him. We'll drop
3688 * him our address then.
3690 if (len < 10 || p[1] != 10)
3692 bzero(&suggestaddr, sizeof(suggestaddr));
3693 suggestaddr.s6_addr16[0] = htons(0xfe80);
3694 (void)in6_setscope(&suggestaddr, SP2IFP(sp), NULL);
3695 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3697 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3699 log(-1, " [suggestaddr %s]",
3700 ip6_sprintf(ip6buf, &suggestaddr));
3701 #ifdef IPV6CP_MYIFID_DYN
3703 * When doing dynamic address assignment,
3704 * we accept his offer.
3706 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3707 struct in6_addr lastsuggest;
3709 * If <suggested myaddr from peer> equals to
3710 * <hisaddr we have suggested last time>,
3711 * we have a collision. generate new random
3714 sppp_suggest_ip6_addr(&lastsuggest);
3715 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3718 log(-1, " [random]");
3719 sppp_gen_ip6_addr(sp, &suggestaddr);
3721 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3723 log(-1, " [agree]");
3724 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3728 * Since we do not do dynamic address assignment,
3729 * we ignore it and thus continue to negotiate
3730 * our already existing value. This can possibly
3731 * go into infinite request-reject loop.
3733 * This is not likely because we normally use
3734 * ifid based on MAC-address.
3735 * If you have no ethernet card on the node, too bad.
3736 * XXX should we use fail_counter?
3741 case IPV6CP_OPT_COMPRESS:
3743 * Peer wants different compression parameters.
3755 sppp_ipv6cp_tlu(struct sppp *sp)
3757 /* we are up - notify isdn daemon */
3763 sppp_ipv6cp_tld(struct sppp *sp)
3768 sppp_ipv6cp_tls(struct sppp *sp)
3770 /* indicate to LCP that it must stay alive */
3771 sp->lcp.protos |= (1 << IDX_IPV6CP);
3775 sppp_ipv6cp_tlf(struct sppp *sp)
3778 #if 0 /* need #if 0 to close IPv6CP properly */
3779 /* we no longer need LCP */
3780 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3781 sppp_lcp_check_and_close(sp);
3786 sppp_ipv6cp_scr(struct sppp *sp)
3788 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3789 struct in6_addr ouraddr;
3792 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3793 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3794 opt[i++] = IPV6CP_OPT_IFID;
3796 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3801 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3802 opt[i++] = IPV6CP_OPT_COMPRESSION;
3804 opt[i++] = 0; /* TBD */
3805 opt[i++] = 0; /* TBD */
3806 /* variable length data may follow */
3810 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3811 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3814 static void sppp_ipv6cp_init(struct sppp *sp)
3818 static void sppp_ipv6cp_up(struct sppp *sp)
3822 static void sppp_ipv6cp_down(struct sppp *sp)
3827 static void sppp_ipv6cp_open(struct sppp *sp)
3831 static void sppp_ipv6cp_close(struct sppp *sp)
3835 static void sppp_ipv6cp_TO(void *sp)
3839 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3844 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3848 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3852 static void sppp_ipv6cp_tlu(struct sppp *sp)
3856 static void sppp_ipv6cp_tld(struct sppp *sp)
3860 static void sppp_ipv6cp_tls(struct sppp *sp)
3864 static void sppp_ipv6cp_tlf(struct sppp *sp)
3868 static void sppp_ipv6cp_scr(struct sppp *sp)
3874 *--------------------------------------------------------------------------*
3876 * The CHAP implementation. *
3878 *--------------------------------------------------------------------------*
3882 * The authentication protocols don't employ a full-fledged state machine as
3883 * the control protocols do, since they do have Open and Close events, but
3884 * not Up and Down, nor are they explicitly terminated. Also, use of the
3885 * authentication protocols may be different in both directions (this makes
3886 * sense, think of a machine that never accepts incoming calls but only
3887 * calls out, it doesn't require the called party to authenticate itself).
3889 * Our state machine for the local authentication protocol (we are requesting
3890 * the peer to authenticate) looks like:
3893 * +--------------------------------------------+
3895 * +--------+ Close +---------+ RCA+
3896 * | |<----------------------------------| |------+
3897 * +--->| Closed | TO* | Opened | sca |
3898 * | | |-----+ +-------| |<-----+
3899 * | +--------+ irc | | +---------+
3905 * | | +------->+ | |
3907 * | +--------+ V | |
3908 * | | |<----+<--------------------+ |
3914 * +------+ +------------------------------------------+
3915 * scn,tld sca,irc,ict,tlu
3920 * Open: LCP reached authentication phase
3921 * Close: LCP reached terminate phase
3923 * RCA+: received reply (pap-req, chap-response), acceptable
3924 * RCN: received reply (pap-req, chap-response), not acceptable
3925 * TO+: timeout with restart counter >= 0
3926 * TO-: timeout with restart counter < 0
3927 * TO*: reschedule timeout for CHAP
3929 * scr: send request packet (none for PAP, chap-challenge)
3930 * sca: send ack packet (pap-ack, chap-success)
3931 * scn: send nak packet (pap-nak, chap-failure)
3932 * ict: initialize re-challenge timer (CHAP only)
3934 * tlu: this-layer-up, LCP reaches network phase
3935 * tld: this-layer-down, LCP enters terminate phase
3937 * Note that in CHAP mode, after sending a new challenge, while the state
3938 * automaton falls back into Req-Sent state, it doesn't signal a tld
3939 * event to LCP, so LCP remains in network phase. Only after not getting
3940 * any response (or after getting an unacceptable response), CHAP closes,
3941 * causing LCP to enter terminate phase.
3943 * With PAP, there is no initial request that can be sent. The peer is
3944 * expected to send one based on the successful negotiation of PAP as
3945 * the authentication protocol during the LCP option negotiation.
3947 * Incoming authentication protocol requests (remote requests
3948 * authentication, we are peer) don't employ a state machine at all,
3949 * they are simply answered. Some peers [Ascend P50 firmware rev
3950 * 4.50] react allergically when sending IPCP requests while they are
3951 * still in authentication phase (thereby violating the standard that
3952 * demands that these NCP packets are to be discarded), so we keep
3953 * track of the peer demanding us to authenticate, and only proceed to
3954 * phase network once we've seen a positive acknowledge for the
3959 * Handle incoming CHAP packets.
3962 sppp_chap_input(struct sppp *sp, struct mbuf *m)
3965 struct lcp_header *h;
3967 u_char *value, *name, digest[AUTHKEYLEN], dsize;
3968 int value_len, name_len;
3971 len = m->m_pkthdr.len;
3975 SPP_FMT "chap invalid packet length: %d bytes\n",
3976 SPP_ARGS(ifp), len);
3979 h = mtod (m, struct lcp_header*);
3980 if (len > ntohs (h->len))
3981 len = ntohs (h->len);
3984 /* challenge, failure and success are his authproto */
3985 case CHAP_CHALLENGE:
3986 value = 1 + (u_char*)(h+1);
3987 value_len = value[-1];
3988 name = value + value_len;
3989 name_len = len - value_len - 5;
3993 SPP_FMT "chap corrupted challenge "
3994 "<%s id=0x%x len=%d",
3996 sppp_auth_type_name(PPP_CHAP, h->type),
3997 h->ident, ntohs(h->len));
3998 sppp_print_bytes((u_char*) (h+1), len-4);
4006 SPP_FMT "chap input <%s id=0x%x len=%d name=",
4008 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
4010 sppp_print_string((char*) name, name_len);
4011 log(-1, " value-size=%d value=", value_len);
4012 sppp_print_bytes(value, value_len);
4016 /* Compute reply value. */
4018 MD5Update(&ctx, &h->ident, 1);
4019 MD5Update(&ctx, sp->myauth.secret,
4020 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
4021 MD5Update(&ctx, value, value_len);
4022 MD5Final(digest, &ctx);
4023 dsize = sizeof digest;
4025 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
4026 sizeof dsize, (const char *)&dsize,
4027 sizeof digest, digest,
4028 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4035 log(LOG_DEBUG, SPP_FMT "chap success",
4039 sppp_print_string((char*)(h + 1), len - 4);
4045 sp->pp_flags &= ~PP_NEEDAUTH;
4046 if (sp->myauth.proto == PPP_CHAP &&
4047 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4048 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
4050 * We are authenticator for CHAP but didn't
4051 * complete yet. Leave it to tlu to proceed
4060 sppp_phase_network(sp);
4065 log(LOG_INFO, SPP_FMT "chap failure",
4069 sppp_print_string((char*)(h + 1), len - 4);
4073 log(LOG_INFO, SPP_FMT "chap failure\n",
4075 /* await LCP shutdown by authenticator */
4078 /* response is my authproto */
4080 value = 1 + (u_char*)(h+1);
4081 value_len = value[-1];
4082 name = value + value_len;
4083 name_len = len - value_len - 5;
4087 SPP_FMT "chap corrupted response "
4088 "<%s id=0x%x len=%d",
4090 sppp_auth_type_name(PPP_CHAP, h->type),
4091 h->ident, ntohs(h->len));
4092 sppp_print_bytes((u_char*)(h+1), len-4);
4097 if (h->ident != sp->confid[IDX_CHAP]) {
4100 SPP_FMT "chap dropping response for old ID "
4101 "(got %d, expected %d)\n",
4103 h->ident, sp->confid[IDX_CHAP]);
4106 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4107 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4108 log(LOG_INFO, SPP_FMT "chap response, his name ",
4110 sppp_print_string(name, name_len);
4111 log(-1, " != expected ");
4112 sppp_print_string(sp->hisauth.name,
4113 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4117 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4118 "<%s id=0x%x len=%d name=",
4120 sppp_state_name(sp->state[IDX_CHAP]),
4121 sppp_auth_type_name(PPP_CHAP, h->type),
4122 h->ident, ntohs (h->len));
4123 sppp_print_string((char*)name, name_len);
4124 log(-1, " value-size=%d value=", value_len);
4125 sppp_print_bytes(value, value_len);
4128 if (value_len != AUTHKEYLEN) {
4131 SPP_FMT "chap bad hash value length: "
4132 "%d bytes, should be %d\n",
4133 SPP_ARGS(ifp), value_len,
4139 MD5Update(&ctx, &h->ident, 1);
4140 MD5Update(&ctx, sp->hisauth.secret,
4141 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4142 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4143 MD5Final(digest, &ctx);
4145 #define FAILMSG "Failed..."
4146 #define SUCCMSG "Welcome!"
4148 if (value_len != sizeof digest ||
4149 bcmp(digest, value, value_len) != 0) {
4150 /* action scn, tld */
4151 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4152 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4157 /* action sca, perhaps tlu */
4158 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4159 sp->state[IDX_CHAP] == STATE_OPENED)
4160 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4161 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4163 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4164 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4170 /* Unknown CHAP packet type -- ignore. */
4172 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4173 "<0x%x id=0x%xh len=%d",
4175 sppp_state_name(sp->state[IDX_CHAP]),
4176 h->type, h->ident, ntohs(h->len));
4177 sppp_print_bytes((u_char*)(h+1), len-4);
4186 sppp_chap_init(struct sppp *sp)
4188 /* Chap doesn't have STATE_INITIAL at all. */
4189 sp->state[IDX_CHAP] = STATE_CLOSED;
4190 sp->fail_counter[IDX_CHAP] = 0;
4191 sp->pp_seq[IDX_CHAP] = 0;
4192 sp->pp_rseq[IDX_CHAP] = 0;
4193 callout_init(&sp->ch[IDX_CHAP],
4194 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
4198 sppp_chap_open(struct sppp *sp)
4200 if (sp->myauth.proto == PPP_CHAP &&
4201 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4202 /* we are authenticator for CHAP, start it */
4204 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4205 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4207 /* nothing to be done if we are peer, await a challenge */
4211 sppp_chap_close(struct sppp *sp)
4213 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4214 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4218 sppp_chap_TO(void *cookie)
4220 struct sppp *sp = (struct sppp *)cookie;
4227 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4229 sppp_state_name(sp->state[IDX_CHAP]),
4230 sp->rst_counter[IDX_CHAP]);
4232 if (--sp->rst_counter[IDX_CHAP] < 0)
4234 switch (sp->state[IDX_CHAP]) {
4235 case STATE_REQ_SENT:
4237 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4241 /* TO+ (or TO*) event */
4242 switch (sp->state[IDX_CHAP]) {
4245 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4247 case STATE_REQ_SENT:
4249 /* sppp_cp_change_state() will restart the timer */
4250 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4259 sppp_chap_tlu(struct sppp *sp)
4265 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4268 * Some broken CHAP implementations (Conware CoNet, firmware
4269 * 4.0.?) don't want to re-authenticate their CHAP once the
4270 * initial challenge-response exchange has taken place.
4271 * Provide for an option to avoid rechallenges.
4273 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4275 * Compute the re-challenge timeout. This will yield
4276 * a number between 300 and 810 seconds.
4278 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
4279 callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, (void *)sp);
4284 SPP_FMT "chap %s, ",
4286 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4287 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4288 log(-1, "next re-challenge in %d seconds\n", i);
4290 log(-1, "re-challenging supressed\n");
4295 /* indicate to LCP that we need to be closed down */
4296 sp->lcp.protos |= (1 << IDX_CHAP);
4298 if (sp->pp_flags & PP_NEEDAUTH) {
4300 * Remote is authenticator, but his auth proto didn't
4301 * complete yet. Defer the transition to network
4312 * If we are already in phase network, we are done here. This
4313 * is the case if this is a dummy tlu event after a re-challenge.
4315 if (sp->pp_phase != PHASE_NETWORK)
4316 sppp_phase_network(sp);
4320 sppp_chap_tld(struct sppp *sp)
4325 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4326 callout_stop(&sp->ch[IDX_CHAP]);
4327 sp->lcp.protos &= ~(1 << IDX_CHAP);
4333 sppp_chap_scr(struct sppp *sp)
4338 /* Compute random challenge. */
4339 ch = (u_long *)sp->myauth.challenge;
4340 read_random(&seed, sizeof seed);
4341 ch[0] = seed ^ random();
4342 ch[1] = seed ^ random();
4343 ch[2] = seed ^ random();
4344 ch[3] = seed ^ random();
4347 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4349 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4350 sizeof clen, (const char *)&clen,
4351 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4352 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4358 *--------------------------------------------------------------------------*
4360 * The PAP implementation. *
4362 *--------------------------------------------------------------------------*
4365 * For PAP, we need to keep a little state also if we are the peer, not the
4366 * authenticator. This is since we don't get a request to authenticate, but
4367 * have to repeatedly authenticate ourself until we got a response (or the
4368 * retry counter is expired).
4372 * Handle incoming PAP packets. */
4374 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4377 struct lcp_header *h;
4379 u_char *name, *passwd, mlen;
4380 int name_len, passwd_len;
4382 len = m->m_pkthdr.len;
4386 SPP_FMT "pap invalid packet length: %d bytes\n",
4387 SPP_ARGS(ifp), len);
4390 h = mtod (m, struct lcp_header*);
4391 if (len > ntohs (h->len))
4392 len = ntohs (h->len);
4394 /* PAP request is my authproto */
4396 name = 1 + (u_char*)(h+1);
4397 name_len = name[-1];
4398 passwd = name + name_len + 1;
4399 if (name_len > len - 6 ||
4400 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4402 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4403 "<%s id=0x%x len=%d",
4405 sppp_auth_type_name(PPP_PAP, h->type),
4406 h->ident, ntohs(h->len));
4407 sppp_print_bytes((u_char*)(h+1), len-4);
4413 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4414 "<%s id=0x%x len=%d name=",
4416 sppp_state_name(sp->state[IDX_PAP]),
4417 sppp_auth_type_name(PPP_PAP, h->type),
4418 h->ident, ntohs(h->len));
4419 sppp_print_string((char*)name, name_len);
4420 log(-1, " passwd=");
4421 sppp_print_string((char*)passwd, passwd_len);
4424 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4425 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4426 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4427 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4428 /* action scn, tld */
4429 mlen = sizeof(FAILMSG) - 1;
4430 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4431 sizeof mlen, (const char *)&mlen,
4432 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4437 /* action sca, perhaps tlu */
4438 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4439 sp->state[IDX_PAP] == STATE_OPENED) {
4440 mlen = sizeof(SUCCMSG) - 1;
4441 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4442 sizeof mlen, (const char *)&mlen,
4443 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4446 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4447 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4452 /* ack and nak are his authproto */
4454 callout_stop(&sp->pap_my_to_ch);
4456 log(LOG_DEBUG, SPP_FMT "pap success",
4458 name_len = *((char *)h);
4459 if (len > 5 && name_len) {
4461 sppp_print_string((char*)(h+1), name_len);
4467 sp->pp_flags &= ~PP_NEEDAUTH;
4468 if (sp->myauth.proto == PPP_PAP &&
4469 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4470 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4472 * We are authenticator for PAP but didn't
4473 * complete yet. Leave it to tlu to proceed
4482 sppp_phase_network(sp);
4486 callout_stop (&sp->pap_my_to_ch);
4488 log(LOG_INFO, SPP_FMT "pap failure",
4490 name_len = *((char *)h);
4491 if (len > 5 && name_len) {
4493 sppp_print_string((char*)(h+1), name_len);
4497 log(LOG_INFO, SPP_FMT "pap failure\n",
4499 /* await LCP shutdown by authenticator */
4503 /* Unknown PAP packet type -- ignore. */
4505 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4506 "<0x%x id=0x%x len=%d",
4508 h->type, h->ident, ntohs(h->len));
4509 sppp_print_bytes((u_char*)(h+1), len-4);
4518 sppp_pap_init(struct sppp *sp)
4520 /* PAP doesn't have STATE_INITIAL at all. */
4521 sp->state[IDX_PAP] = STATE_CLOSED;
4522 sp->fail_counter[IDX_PAP] = 0;
4523 sp->pp_seq[IDX_PAP] = 0;
4524 sp->pp_rseq[IDX_PAP] = 0;
4525 callout_init(&sp->ch[IDX_PAP],
4526 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
4527 callout_init(&sp->pap_my_to_ch,
4528 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
4532 sppp_pap_open(struct sppp *sp)
4534 if (sp->hisauth.proto == PPP_PAP &&
4535 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4536 /* we are authenticator for PAP, start our timer */
4537 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4538 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4540 if (sp->myauth.proto == PPP_PAP) {
4541 /* we are peer, send a request, and start a timer */
4543 callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
4544 sppp_pap_my_TO, (void *)sp);
4549 sppp_pap_close(struct sppp *sp)
4551 if (sp->state[IDX_PAP] != STATE_CLOSED)
4552 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4556 * That's the timeout routine if we are authenticator. Since the
4557 * authenticator is basically passive in PAP, we can't do much here.
4560 sppp_pap_TO(void *cookie)
4562 struct sppp *sp = (struct sppp *)cookie;
4569 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4571 sppp_state_name(sp->state[IDX_PAP]),
4572 sp->rst_counter[IDX_PAP]);
4574 if (--sp->rst_counter[IDX_PAP] < 0)
4576 switch (sp->state[IDX_PAP]) {
4577 case STATE_REQ_SENT:
4579 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4583 /* TO+ event, not very much we could do */
4584 switch (sp->state[IDX_PAP]) {
4585 case STATE_REQ_SENT:
4586 /* sppp_cp_change_state() will restart the timer */
4587 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4596 * That's the timeout handler if we are peer. Since the peer is active,
4597 * we need to retransmit our PAP request since it is apparently lost.
4598 * XXX We should impose a max counter.
4601 sppp_pap_my_TO(void *cookie)
4603 struct sppp *sp = (struct sppp *)cookie;
4607 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4616 sppp_pap_tlu(struct sppp *sp)
4621 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4624 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4625 SPP_ARGS(ifp), pap.name);
4629 /* indicate to LCP that we need to be closed down */
4630 sp->lcp.protos |= (1 << IDX_PAP);
4632 if (sp->pp_flags & PP_NEEDAUTH) {
4634 * Remote is authenticator, but his auth proto didn't
4635 * complete yet. Defer the transition to network
4644 sppp_phase_network(sp);
4648 sppp_pap_tld(struct sppp *sp)
4653 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4654 callout_stop (&sp->ch[IDX_PAP]);
4655 callout_stop (&sp->pap_my_to_ch);
4656 sp->lcp.protos &= ~(1 << IDX_PAP);
4662 sppp_pap_scr(struct sppp *sp)
4664 u_char idlen, pwdlen;
4666 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4667 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4668 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4670 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4671 sizeof idlen, (const char *)&idlen,
4672 (size_t)idlen, sp->myauth.name,
4673 sizeof pwdlen, (const char *)&pwdlen,
4674 (size_t)pwdlen, sp->myauth.secret,
4679 * Random miscellaneous functions.
4683 * Send a PAP or CHAP proto packet.
4685 * Varadic function, each of the elements for the ellipsis is of type
4686 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4688 * NOTE: never declare variadic functions with types subject to type
4689 * promotion (i.e. u_char). This is asking for big trouble depending
4690 * on the architecture you are on...
4694 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4695 unsigned int type, unsigned int id,
4699 struct ppp_header *h;
4700 struct lcp_header *lh;
4708 MGETHDR (m, M_DONTWAIT, MT_DATA);
4711 m->m_pkthdr.rcvif = 0;
4713 h = mtod (m, struct ppp_header*);
4714 h->address = PPP_ALLSTATIONS; /* broadcast address */
4715 h->control = PPP_UI; /* Unnumbered Info */
4716 h->protocol = htons(cp->proto);
4718 lh = (struct lcp_header*)(h + 1);
4721 p = (u_char*) (lh+1);
4726 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
4727 msg = va_arg(ap, const char *);
4729 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4735 bcopy(msg, p, mlen);
4740 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4741 lh->len = htons (LCP_HEADER_LEN + len);
4744 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4745 SPP_ARGS(ifp), cp->name,
4746 sppp_auth_type_name(cp->proto, lh->type),
4747 lh->ident, ntohs(lh->len));
4748 sppp_print_bytes((u_char*) (lh+1), len);
4751 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
4756 * Flush interface queue.
4759 sppp_qflush(struct ifqueue *ifq)
4774 * Send keepalive packets, every 10 seconds.
4777 sppp_keepalive(void *dummy)
4779 struct sppp *sp = (struct sppp*)dummy;
4780 struct ifnet *ifp = SP2IFP(sp);
4785 /* Keepalive mode disabled or channel down? */
4786 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4787 ! (ifp->if_drv_flags & IFF_DRV_RUNNING))
4790 if (sp->pp_mode == PP_FR) {
4791 sppp_fr_keepalive (sp);
4795 /* No keepalive in PPP mode if LCP not opened yet. */
4796 if (sp->pp_mode != IFF_CISCO &&
4797 sp->pp_phase < PHASE_AUTHENTICATE)
4800 if (sp->pp_alivecnt == MAXALIVECNT) {
4801 /* No keepalive packets got. Stop the interface. */
4802 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
4804 sppp_qflush (&sp->pp_cpq);
4805 if (sp->pp_mode != IFF_CISCO) {
4807 /* Shut down the PPP link. */
4809 /* Initiate negotiation. XXX */
4813 if (sp->pp_alivecnt <= MAXALIVECNT)
4815 if (sp->pp_mode == IFF_CISCO)
4816 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4817 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4818 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4819 long nmagic = htonl (sp->lcp.magic);
4820 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4821 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4822 sp->lcp.echoid, 4, &nmagic);
4827 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
4832 * Get both IP addresses.
4835 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4837 struct ifnet *ifp = SP2IFP(sp);
4839 struct sockaddr_in *si, *sm;
4845 * Pick the first AF_INET address from the list,
4846 * aliases don't make any sense on a p2p link anyway.
4849 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4850 if (ifa->ifa_addr->sa_family == AF_INET) {
4851 si = (struct sockaddr_in *)ifa->ifa_addr;
4852 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4857 if (si && si->sin_addr.s_addr) {
4858 ssrc = si->sin_addr.s_addr;
4860 *srcmask = ntohl(sm->sin_addr.s_addr);
4863 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4864 if (si && si->sin_addr.s_addr)
4865 ddst = si->sin_addr.s_addr;
4868 if (dst) *dst = ntohl(ddst);
4869 if (src) *src = ntohl(ssrc);
4873 * Set my IP address. Must be called at splimp.
4876 sppp_set_ip_addr(struct sppp *sp, u_long src)
4880 struct sockaddr_in *si;
4881 struct in_ifaddr *ia;
4884 * Pick the first AF_INET address from the list,
4885 * aliases don't make any sense on a p2p link anyway.
4888 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4890 if (ifa->ifa_addr->sa_family == AF_INET)
4892 si = (struct sockaddr_in *)ifa->ifa_addr;
4901 /* delete old route */
4902 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4905 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4906 SPP_ARGS(ifp), error);
4909 /* set new address */
4910 si->sin_addr.s_addr = htonl(src);
4912 LIST_REMOVE(ia, ia_hash);
4913 LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash);
4916 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
4919 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
4920 SPP_ARGS(ifp), error);
4927 * Get both IPv6 addresses.
4930 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
4931 struct in6_addr *srcmask)
4933 struct ifnet *ifp = SP2IFP(sp);
4935 struct sockaddr_in6 *si, *sm;
4936 struct in6_addr ssrc, ddst;
4939 bzero(&ssrc, sizeof(ssrc));
4940 bzero(&ddst, sizeof(ddst));
4942 * Pick the first link-local AF_INET6 address from the list,
4943 * aliases don't make any sense on a p2p link anyway.
4946 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4947 if (ifa->ifa_addr->sa_family == AF_INET6) {
4948 si = (struct sockaddr_in6 *)ifa->ifa_addr;
4949 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
4950 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
4954 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
4955 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
4957 bcopy(&sm->sin6_addr, srcmask,
4962 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
4963 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
4964 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
4968 bcopy(&ddst, dst, sizeof(*dst));
4970 bcopy(&ssrc, src, sizeof(*src));
4973 #ifdef IPV6CP_MYIFID_DYN
4975 * Generate random ifid.
4978 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
4984 * Set my IPv6 address. Must be called at splimp.
4987 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
4991 struct sockaddr_in6 *sin6;
4994 * Pick the first link-local AF_INET6 address from the list,
4995 * aliases don't make any sense on a p2p link anyway.
4999 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
5001 if (ifa->ifa_addr->sa_family == AF_INET6)
5003 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
5004 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
5012 struct sockaddr_in6 new_sin6 = *sin6;
5014 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
5015 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5018 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
5019 " failed, error=%d\n", SPP_ARGS(ifp), error);
5026 * Suggest a candidate address to be used by peer.
5029 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5031 struct in6_addr myaddr;
5034 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5036 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5038 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5039 myaddr.s6_addr[14] ^= 0xff;
5040 myaddr.s6_addr[15] ^= 0xff;
5042 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5043 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5046 bcopy(&myaddr, suggest, sizeof(myaddr));
5051 sppp_params(struct sppp *sp, u_long cmd, void *data)
5054 struct ifreq *ifr = (struct ifreq *)data;
5055 struct spppreq *spr;
5058 if ((spr = malloc(sizeof(struct spppreq), M_TEMP, M_NOWAIT)) == 0)
5061 * ifr->ifr_data is supposed to point to a struct spppreq.
5062 * Check the cmd word first before attempting to fetch all the
5065 if ((subcmd = fuword(ifr->ifr_data)) == -1) {
5070 if (copyin((caddr_t)ifr->ifr_data, spr, sizeof(struct spppreq)) != 0) {
5076 case (u_long)SPPPIOGDEFS:
5077 if (cmd != SIOCGIFGENERIC) {
5082 * We copy over the entire current state, but clean
5083 * out some of the stuff we don't wanna pass up.
5084 * Remember, SIOCGIFGENERIC is unprotected, and can be
5085 * called by any user. No need to ever get PAP or
5086 * CHAP secrets back to userland anyway.
5088 spr->defs.pp_phase = sp->pp_phase;
5089 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5090 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5091 spr->defs.lcp = sp->lcp;
5092 spr->defs.ipcp = sp->ipcp;
5093 spr->defs.ipv6cp = sp->ipv6cp;
5094 spr->defs.myauth = sp->myauth;
5095 spr->defs.hisauth = sp->hisauth;
5096 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5097 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5098 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5099 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5101 * Fixup the LCP timeout value to milliseconds so
5102 * spppcontrol doesn't need to bother about the value
5103 * of "hz". We do the reverse calculation below when
5106 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5107 rv = copyout(spr, (caddr_t)ifr->ifr_data,
5108 sizeof(struct spppreq));
5111 case (u_long)SPPPIOSDEFS:
5112 if (cmd != SIOCSIFGENERIC) {
5117 * We have a very specific idea of which fields we
5118 * allow being passed back from userland, so to not
5119 * clobber our current state. For one, we only allow
5120 * setting anything if LCP is in dead or establish
5121 * phase. Once the authentication negotiations
5122 * started, the authentication settings must not be
5123 * changed again. (The administrator can force an
5124 * ifconfig down in order to get LCP back into dead
5127 * Also, we only allow for authentication parameters to be
5130 * XXX Should allow to set or clear pp_flags.
5132 * Finally, if the respective authentication protocol to
5133 * be used is set differently than 0, but the secret is
5134 * passed as all zeros, we don't trash the existing secret.
5135 * This allows an administrator to change the system name
5136 * only without clobbering the secret (which he didn't get
5137 * back in a previous SPPPIOGDEFS call). However, the
5138 * secrets are cleared if the authentication protocol is
5140 if (sp->pp_phase != PHASE_DEAD &&
5141 sp->pp_phase != PHASE_ESTABLISH) {
5146 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5147 spr->defs.myauth.proto != PPP_CHAP) ||
5148 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5149 spr->defs.hisauth.proto != PPP_CHAP)) {
5154 if (spr->defs.myauth.proto == 0)
5155 /* resetting myauth */
5156 bzero(&sp->myauth, sizeof sp->myauth);
5158 /* setting/changing myauth */
5159 sp->myauth.proto = spr->defs.myauth.proto;
5160 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5161 if (spr->defs.myauth.secret[0] != '\0')
5162 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5165 if (spr->defs.hisauth.proto == 0)
5166 /* resetting hisauth */
5167 bzero(&sp->hisauth, sizeof sp->hisauth);
5169 /* setting/changing hisauth */
5170 sp->hisauth.proto = spr->defs.hisauth.proto;
5171 sp->hisauth.flags = spr->defs.hisauth.flags;
5172 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5173 if (spr->defs.hisauth.secret[0] != '\0')
5174 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5177 /* set LCP restart timer timeout */
5178 if (spr->defs.lcp.timeout != 0)
5179 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5180 /* set VJ enable and IPv6 disable flags */
5182 if (spr->defs.enable_vj)
5183 sp->confflags |= CONF_ENABLE_VJ;
5185 sp->confflags &= ~CONF_ENABLE_VJ;
5188 if (spr->defs.enable_ipv6)
5189 sp->confflags |= CONF_ENABLE_IPV6;
5191 sp->confflags &= ~CONF_ENABLE_IPV6;
5206 sppp_phase_network(struct sppp *sp)
5212 sp->pp_phase = PHASE_NETWORK;
5215 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5216 sppp_phase_name(sp->pp_phase));
5218 /* Notify NCPs now. */
5219 for (i = 0; i < IDX_COUNT; i++)
5220 if ((cps[i])->flags & CP_NCP)
5223 /* Send Up events to all NCPs. */
5224 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5225 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5228 /* if no NCP is starting, all this was in vain, close down */
5229 sppp_lcp_check_and_close(sp);
5234 sppp_cp_type_name(u_char type)
5236 static char buf[12];
5238 case CONF_REQ: return "conf-req";
5239 case CONF_ACK: return "conf-ack";
5240 case CONF_NAK: return "conf-nak";
5241 case CONF_REJ: return "conf-rej";
5242 case TERM_REQ: return "term-req";
5243 case TERM_ACK: return "term-ack";
5244 case CODE_REJ: return "code-rej";
5245 case PROTO_REJ: return "proto-rej";
5246 case ECHO_REQ: return "echo-req";
5247 case ECHO_REPLY: return "echo-reply";
5248 case DISC_REQ: return "discard-req";
5250 snprintf (buf, sizeof(buf), "cp/0x%x", type);
5255 sppp_auth_type_name(u_short proto, u_char type)
5257 static char buf[12];
5261 case CHAP_CHALLENGE: return "challenge";
5262 case CHAP_RESPONSE: return "response";
5263 case CHAP_SUCCESS: return "success";
5264 case CHAP_FAILURE: return "failure";
5268 case PAP_REQ: return "req";
5269 case PAP_ACK: return "ack";
5270 case PAP_NAK: return "nak";
5273 snprintf (buf, sizeof(buf), "auth/0x%x", type);
5278 sppp_lcp_opt_name(u_char opt)
5280 static char buf[12];
5282 case LCP_OPT_MRU: return "mru";
5283 case LCP_OPT_ASYNC_MAP: return "async-map";
5284 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5285 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5286 case LCP_OPT_MAGIC: return "magic";
5287 case LCP_OPT_PROTO_COMP: return "proto-comp";
5288 case LCP_OPT_ADDR_COMP: return "addr-comp";
5290 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5295 sppp_ipcp_opt_name(u_char opt)
5297 static char buf[12];
5299 case IPCP_OPT_ADDRESSES: return "addresses";
5300 case IPCP_OPT_COMPRESSION: return "compression";
5301 case IPCP_OPT_ADDRESS: return "address";
5303 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5309 sppp_ipv6cp_opt_name(u_char opt)
5311 static char buf[12];
5313 case IPV6CP_OPT_IFID: return "ifid";
5314 case IPV6CP_OPT_COMPRESSION: return "compression";
5316 sprintf (buf, "0x%x", opt);
5322 sppp_state_name(int state)
5325 case STATE_INITIAL: return "initial";
5326 case STATE_STARTING: return "starting";
5327 case STATE_CLOSED: return "closed";
5328 case STATE_STOPPED: return "stopped";
5329 case STATE_CLOSING: return "closing";
5330 case STATE_STOPPING: return "stopping";
5331 case STATE_REQ_SENT: return "req-sent";
5332 case STATE_ACK_RCVD: return "ack-rcvd";
5333 case STATE_ACK_SENT: return "ack-sent";
5334 case STATE_OPENED: return "opened";
5340 sppp_phase_name(enum ppp_phase phase)
5343 case PHASE_DEAD: return "dead";
5344 case PHASE_ESTABLISH: return "establish";
5345 case PHASE_TERMINATE: return "terminate";
5346 case PHASE_AUTHENTICATE: return "authenticate";
5347 case PHASE_NETWORK: return "network";
5353 sppp_proto_name(u_short proto)
5355 static char buf[12];
5357 case PPP_LCP: return "lcp";
5358 case PPP_IPCP: return "ipcp";
5359 case PPP_PAP: return "pap";
5360 case PPP_CHAP: return "chap";
5361 case PPP_IPV6CP: return "ipv6cp";
5363 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5368 sppp_print_bytes(const u_char *p, u_short len)
5371 log(-1, " %*D", len, p, "-");
5375 sppp_print_string(const char *p, u_short len)
5382 * Print only ASCII chars directly. RFC 1994 recommends
5383 * using only them, but we don't rely on it. */
5384 if (c < ' ' || c > '~')
5385 log(-1, "\\x%x", c);
5392 sppp_dotted_quad(u_long addr)
5395 sprintf(s, "%d.%d.%d.%d",
5396 (int)((addr >> 24) & 0xff),
5397 (int)((addr >> 16) & 0xff),
5398 (int)((addr >> 8) & 0xff),
5399 (int)(addr & 0xff));
5404 sppp_strnlen(u_char *p, int max)
5408 for (len = 0; len < max && *p; ++p)
5413 /* a dummy, used to drop uninteresting events */
5415 sppp_null(struct sppp *unused)
5417 /* do just nothing */