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>
26 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
28 #include "opt_inet6.h"
34 # include "opt_inet.h"
35 # include "opt_inet6.h"
40 #include <sys/systm.h>
41 #include <sys/kernel.h>
42 #include <sys/module.h>
43 #include <sys/sockio.h>
44 #include <sys/socket.h>
45 #include <sys/syslog.h>
46 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
47 #include <sys/random.h>
49 #include <sys/malloc.h>
52 #if defined (__OpenBSD__)
59 #include <net/netisr.h>
60 #include <net/if_types.h>
61 #include <net/route.h>
62 #include <netinet/in.h>
63 #include <netinet/in_systm.h>
64 #include <netinet/ip.h>
65 #include <net/slcompress.h>
67 #if defined (__NetBSD__) || defined (__OpenBSD__)
68 #include <machine/cpu.h> /* XXX for softnet */
71 #include <machine/stdarg.h>
73 #include <netinet/in_var.h>
76 #include <netinet/ip.h>
77 #include <netinet/tcp.h>
81 #include <netinet6/scope6_var.h>
84 #if defined (__FreeBSD__) || defined (__OpenBSD__)
85 # include <netinet/if_ether.h>
87 # include <net/ethertypes.h>
91 #include <netipx/ipx.h>
92 #include <netipx/ipx_if.h>
95 #include <net/if_sppp.h>
97 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
98 # define IOCTL_CMD_T u_long
100 # define IOCTL_CMD_T int
103 #define MAXALIVECNT 3 /* max. alive packets */
106 * Interface flags that can be set in an ifconfig command.
108 * Setting link0 will make the link passive, i.e. it will be marked
109 * as being administrative openable, but won't be opened to begin
110 * with. Incoming calls will be answered, or subsequent calls with
111 * -link1 will cause the administrative open of the LCP layer.
113 * Setting link1 will cause the link to auto-dial only as packets
116 * Setting IFF_DEBUG will syslog the option negotiation and state
117 * transitions at level kern.debug. Note: all logs consistently look
120 * <if-name><unit>: <proto-name> <additional info...>
122 * with <if-name><unit> being something like "bppp0", and <proto-name>
123 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
126 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
127 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
128 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
130 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
131 #define PPP_UI 0x03 /* Unnumbered Information */
132 #define PPP_IP 0x0021 /* Internet Protocol */
133 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
134 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
135 #define PPP_IPX 0x002b /* Novell IPX Protocol */
136 #define PPP_VJ_COMP 0x002d /* VJ compressed TCP/IP */
137 #define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */
138 #define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */
139 #define PPP_LCP 0xc021 /* Link Control Protocol */
140 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
141 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
142 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
143 #define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */
145 #define CONF_REQ 1 /* PPP configure request */
146 #define CONF_ACK 2 /* PPP configure acknowledge */
147 #define CONF_NAK 3 /* PPP configure negative ack */
148 #define CONF_REJ 4 /* PPP configure reject */
149 #define TERM_REQ 5 /* PPP terminate request */
150 #define TERM_ACK 6 /* PPP terminate acknowledge */
151 #define CODE_REJ 7 /* PPP code reject */
152 #define PROTO_REJ 8 /* PPP protocol reject */
153 #define ECHO_REQ 9 /* PPP echo request */
154 #define ECHO_REPLY 10 /* PPP echo reply */
155 #define DISC_REQ 11 /* PPP discard request */
157 #define LCP_OPT_MRU 1 /* maximum receive unit */
158 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
159 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
160 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
161 #define LCP_OPT_MAGIC 5 /* magic number */
162 #define LCP_OPT_RESERVED 6 /* reserved */
163 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
164 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
166 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
167 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
168 #define IPCP_OPT_ADDRESS 3 /* local IP address */
170 #define IPV6CP_OPT_IFID 1 /* interface identifier */
171 #define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */
173 #define IPCP_COMP_VJ 0x2d /* Code for VJ compression */
175 #define PAP_REQ 1 /* PAP name/password request */
176 #define PAP_ACK 2 /* PAP acknowledge */
177 #define PAP_NAK 3 /* PAP fail */
179 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
180 #define CHAP_RESPONSE 2 /* CHAP challenge response */
181 #define CHAP_SUCCESS 3 /* CHAP response ok */
182 #define CHAP_FAILURE 4 /* CHAP response failed */
184 #define CHAP_MD5 5 /* hash algorithm - MD5 */
186 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
187 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
188 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
189 #define CISCO_ADDR_REQ 0 /* Cisco address request */
190 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
191 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
193 /* states are named and numbered according to RFC 1661 */
194 #define STATE_INITIAL 0
195 #define STATE_STARTING 1
196 #define STATE_CLOSED 2
197 #define STATE_STOPPED 3
198 #define STATE_CLOSING 4
199 #define STATE_STOPPING 5
200 #define STATE_REQ_SENT 6
201 #define STATE_ACK_RCVD 7
202 #define STATE_ACK_SENT 8
203 #define STATE_OPENED 9
205 MALLOC_DEFINE(M_SPPP, "sppp", "synchronous PPP interface internals");
212 #define PPP_HEADER_LEN sizeof (struct ppp_header)
219 #define LCP_HEADER_LEN sizeof (struct lcp_header)
221 struct cisco_packet {
229 #define CISCO_PACKET_LEN sizeof (struct cisco_packet)
232 * We follow the spelling and capitalization of RFC 1661 here, to make
233 * it easier comparing with the standard. Please refer to this RFC in
234 * case you can't make sense out of these abbreviation; it will also
235 * explain the semantics related to the various events and actions.
238 u_short proto; /* PPP control protocol number */
239 u_char protoidx; /* index into state table in struct sppp */
241 #define CP_LCP 0x01 /* this is the LCP */
242 #define CP_AUTH 0x02 /* this is an authentication protocol */
243 #define CP_NCP 0x04 /* this is a NCP */
244 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
245 const char *name; /* name of this control protocol */
247 void (*Up)(struct sppp *sp);
248 void (*Down)(struct sppp *sp);
249 void (*Open)(struct sppp *sp);
250 void (*Close)(struct sppp *sp);
251 void (*TO)(void *sp);
252 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
253 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
254 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
256 void (*tlu)(struct sppp *sp);
257 void (*tld)(struct sppp *sp);
258 void (*tls)(struct sppp *sp);
259 void (*tlf)(struct sppp *sp);
260 void (*scr)(struct sppp *sp);
263 #if defined(__FreeBSD__) && __FreeBSD__ >= 3 && __FreeBSD_version < 501113
264 #define SPP_FMT "%s%d: "
265 #define SPP_ARGS(ifp) (ifp)->if_name, (ifp)->if_unit
267 #define SPP_FMT "%s: "
268 #define SPP_ARGS(ifp) (ifp)->if_xname
271 #define SPPP_LOCK(sp) \
273 if (!(SP2IFP(sp)->if_flags & IFF_NEEDSGIANT)) \
274 mtx_lock (&(sp)->mtx); \
276 #define SPPP_UNLOCK(sp) \
278 if (!(SP2IFP(sp)->if_flags & IFF_NEEDSGIANT)) \
279 mtx_unlock (&(sp)->mtx); \
282 #define SPPP_LOCK_ASSERT(sp) \
284 if (!(SP2IFP(sp)->if_flags & IFF_NEEDSGIANT)) \
285 mtx_assert (&(sp)->mtx, MA_OWNED); \
287 #define SPPP_LOCK_OWNED(sp) \
288 (!(SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) && \
289 mtx_owned (&sp->mtx))
293 * The following disgusting hack gets around the problem that IP TOS
294 * can't be set yet. We want to put "interactive" traffic on a high
295 * priority queue. To decide if traffic is interactive, we check that
296 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
298 * XXX is this really still necessary? - joerg -
300 static const u_short interactive_ports[8] = {
304 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
307 /* almost every function needs these */
309 struct ifnet *ifp = SP2IFP(sp); \
310 int debug = ifp->if_flags & IFF_DEBUG
312 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
313 struct sockaddr *dst, struct rtentry *rt);
315 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
316 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
318 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
320 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
321 u_char ident, u_short len, void *data);
322 /* static void sppp_cp_timeout(void *arg); */
323 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
325 static void sppp_auth_send(const struct cp *cp,
326 struct sppp *sp, unsigned int type, unsigned int id,
329 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
330 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
331 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
332 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
333 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
335 static void sppp_null(struct sppp *sp);
337 static void sppp_pp_up(struct sppp *sp);
338 static void sppp_pp_down(struct sppp *sp);
340 static void sppp_lcp_init(struct sppp *sp);
341 static void sppp_lcp_up(struct sppp *sp);
342 static void sppp_lcp_down(struct sppp *sp);
343 static void sppp_lcp_open(struct sppp *sp);
344 static void sppp_lcp_close(struct sppp *sp);
345 static void sppp_lcp_TO(void *sp);
346 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
347 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
348 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
349 static void sppp_lcp_tlu(struct sppp *sp);
350 static void sppp_lcp_tld(struct sppp *sp);
351 static void sppp_lcp_tls(struct sppp *sp);
352 static void sppp_lcp_tlf(struct sppp *sp);
353 static void sppp_lcp_scr(struct sppp *sp);
354 static void sppp_lcp_check_and_close(struct sppp *sp);
355 static int sppp_ncp_check(struct sppp *sp);
357 static void sppp_ipcp_init(struct sppp *sp);
358 static void sppp_ipcp_up(struct sppp *sp);
359 static void sppp_ipcp_down(struct sppp *sp);
360 static void sppp_ipcp_open(struct sppp *sp);
361 static void sppp_ipcp_close(struct sppp *sp);
362 static void sppp_ipcp_TO(void *sp);
363 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
364 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
365 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
366 static void sppp_ipcp_tlu(struct sppp *sp);
367 static void sppp_ipcp_tld(struct sppp *sp);
368 static void sppp_ipcp_tls(struct sppp *sp);
369 static void sppp_ipcp_tlf(struct sppp *sp);
370 static void sppp_ipcp_scr(struct sppp *sp);
372 static void sppp_ipv6cp_init(struct sppp *sp);
373 static void sppp_ipv6cp_up(struct sppp *sp);
374 static void sppp_ipv6cp_down(struct sppp *sp);
375 static void sppp_ipv6cp_open(struct sppp *sp);
376 static void sppp_ipv6cp_close(struct sppp *sp);
377 static void sppp_ipv6cp_TO(void *sp);
378 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len);
379 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
380 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
381 static void sppp_ipv6cp_tlu(struct sppp *sp);
382 static void sppp_ipv6cp_tld(struct sppp *sp);
383 static void sppp_ipv6cp_tls(struct sppp *sp);
384 static void sppp_ipv6cp_tlf(struct sppp *sp);
385 static void sppp_ipv6cp_scr(struct sppp *sp);
387 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
388 static void sppp_pap_init(struct sppp *sp);
389 static void sppp_pap_open(struct sppp *sp);
390 static void sppp_pap_close(struct sppp *sp);
391 static void sppp_pap_TO(void *sp);
392 static void sppp_pap_my_TO(void *sp);
393 static void sppp_pap_tlu(struct sppp *sp);
394 static void sppp_pap_tld(struct sppp *sp);
395 static void sppp_pap_scr(struct sppp *sp);
397 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
398 static void sppp_chap_init(struct sppp *sp);
399 static void sppp_chap_open(struct sppp *sp);
400 static void sppp_chap_close(struct sppp *sp);
401 static void sppp_chap_TO(void *sp);
402 static void sppp_chap_tlu(struct sppp *sp);
403 static void sppp_chap_tld(struct sppp *sp);
404 static void sppp_chap_scr(struct sppp *sp);
406 static const char *sppp_auth_type_name(u_short proto, u_char type);
407 static const char *sppp_cp_type_name(u_char type);
408 static const char *sppp_dotted_quad(u_long addr);
409 static const char *sppp_ipcp_opt_name(u_char opt);
411 static const char *sppp_ipv6cp_opt_name(u_char opt);
413 static const char *sppp_lcp_opt_name(u_char opt);
414 static const char *sppp_phase_name(enum ppp_phase phase);
415 static const char *sppp_proto_name(u_short proto);
416 static const char *sppp_state_name(int state);
417 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
418 static int sppp_strnlen(u_char *p, int max);
419 static void sppp_keepalive(void *dummy);
420 static void sppp_phase_network(struct sppp *sp);
421 static void sppp_print_bytes(const u_char *p, u_short len);
422 static void sppp_print_string(const char *p, u_short len);
423 static void sppp_qflush(struct ifqueue *ifq);
424 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
426 static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src,
427 struct in6_addr *dst, struct in6_addr *srcmask);
428 #ifdef IPV6CP_MYIFID_DYN
429 static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src);
430 static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src);
432 static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src);
435 /* if_start () wrapper */
436 static void sppp_ifstart (struct ifnet *ifp);
438 /* our control protocol descriptors */
439 static const struct cp lcp = {
440 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
441 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
442 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
443 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
447 static const struct cp ipcp = {
449 #ifdef INET /* don't run IPCP if there's no IPv4 support */
455 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
456 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
457 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
461 static const struct cp ipv6cp = {
462 PPP_IPV6CP, IDX_IPV6CP,
463 #ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/
469 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close,
470 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak,
471 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf,
475 static const struct cp pap = {
476 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
477 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
478 sppp_pap_TO, 0, 0, 0,
479 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
483 static const struct cp chap = {
484 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
485 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
486 sppp_chap_TO, 0, 0, 0,
487 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
491 static const struct cp *cps[IDX_COUNT] = {
493 &ipcp, /* IDX_IPCP */
494 &ipv6cp, /* IDX_IPV6CP */
496 &chap, /* IDX_CHAP */
500 sppp_alloc(u_char type, struct ifnet *ifp)
504 sp = malloc(sizeof(struct sppp), M_SPPP, M_WAITOK | M_ZERO);
511 sppp_free(void *com, u_char type)
518 sppp_modevent(module_t mod, int type, void *unused)
523 * XXX: should probably be IFT_SPPP, but it's fairly
524 * harmless to allocate struct sppp's for non-sppp
528 if_register_com_alloc(IFT_PPP, sppp_alloc, sppp_free);
531 /* if_deregister_com_alloc(IFT_PPP); */
538 static moduledata_t spppmod = {
543 MODULE_VERSION(sppp, 1);
544 DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
547 * Exported functions, comprising our interface to the lower layer.
551 * Process the received packet.
554 sppp_input(struct ifnet *ifp, struct mbuf *m)
556 struct ppp_header *h;
558 struct sppp *sp = IFP2SP(ifp);
560 int hlen, vjlen, do_account = 0;
564 debug = ifp->if_flags & IFF_DEBUG;
566 if (ifp->if_flags & IFF_UP)
567 /* Count received bytes, add FCS and one flag */
568 ifp->if_ibytes += m->m_pkthdr.len + 3;
570 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
571 /* Too small packet, drop it. */
574 SPP_FMT "input packet is too small, %d bytes\n",
575 SPP_ARGS(ifp), m->m_pkthdr.len);
585 if (sp->pp_mode == PP_FR) {
586 sppp_fr_input (sp, m);
591 /* Get PPP header. */
592 h = mtod (m, struct ppp_header*);
593 m_adj (m, PPP_HEADER_LEN);
595 switch (h->address) {
596 case PPP_ALLSTATIONS:
597 if (h->control != PPP_UI)
599 if (sp->pp_mode == IFF_CISCO) {
602 SPP_FMT "PPP packet in Cisco mode "
603 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
605 h->address, h->control, ntohs(h->protocol));
608 switch (ntohs (h->protocol)) {
612 SPP_FMT "rejecting protocol "
613 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
615 h->address, h->control, ntohs(h->protocol));
616 if (sp->state[IDX_LCP] == STATE_OPENED)
617 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
618 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2,
623 sppp_cp_input(&lcp, sp, m);
628 if (sp->pp_phase >= PHASE_AUTHENTICATE)
629 sppp_pap_input(sp, m);
634 if (sp->pp_phase >= PHASE_AUTHENTICATE)
635 sppp_chap_input(sp, m);
641 if (sp->pp_phase == PHASE_NETWORK)
642 sppp_cp_input(&ipcp, sp, m);
647 if (sp->state[IDX_IPCP] == STATE_OPENED) {
653 if (sp->state[IDX_IPCP] == STATE_OPENED) {
655 sl_uncompress_tcp_core(mtod(m, u_char *),
659 &iphdr, &hlen)) <= 0) {
662 SPP_FMT "VJ uncompress failed on compressed packet\n",
668 * Trim the VJ header off the packet, and prepend
669 * the uncompressed IP header (which will usually
670 * end up in two chained mbufs since there's not
671 * enough leading space in the existing mbuf).
674 M_PREPEND(m, hlen, M_DONTWAIT);
679 bcopy(iphdr, mtod(m, u_char *), hlen);
685 if (sp->state[IDX_IPCP] == STATE_OPENED) {
686 if (sl_uncompress_tcp_core(mtod(m, u_char *),
688 TYPE_UNCOMPRESSED_TCP,
690 &iphdr, &hlen) != 0) {
693 SPP_FMT "VJ uncompress failed on uncompressed packet\n",
704 if (sp->pp_phase == PHASE_NETWORK)
705 sppp_cp_input(&ipv6cp, sp, m);
711 if (sp->state[IDX_IPV6CP] == STATE_OPENED)
718 /* IPX IPXCP not implemented yet */
719 if (sp->pp_phase == PHASE_NETWORK)
726 case CISCO_MULTICAST:
728 /* Don't check the control field here (RFC 1547). */
729 if (sp->pp_mode != IFF_CISCO) {
732 SPP_FMT "Cisco packet in PPP mode "
733 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
735 h->address, h->control, ntohs(h->protocol));
738 switch (ntohs (h->protocol)) {
742 case CISCO_KEEPALIVE:
743 sppp_cisco_input (sp, m);
767 default: /* Invalid PPP packet. */
771 SPP_FMT "invalid input packet "
772 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
774 h->address, h->control, ntohs(h->protocol));
778 if (! (ifp->if_flags & IFF_UP) || isr == -1)
783 if (netisr_queue(isr, m)) { /* (0) on success. */
785 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
792 * Do only account for network packets, not for control
793 * packets. This is used by some subsystems to detect
796 sp->pp_last_recv = time_uptime;
800 sppp_ifstart_sched(void *dummy)
802 struct sppp *sp = dummy;
804 sp->if_start(SP2IFP(sp));
807 /* if_start () wrapper function. We use it to schedule real if_start () for
808 * execution. We can't call it directly
811 sppp_ifstart(struct ifnet *ifp)
813 struct sppp *sp = IFP2SP(ifp);
815 if (SPPP_LOCK_OWNED(sp)) {
816 if (callout_pending(&sp->ifstart_callout))
818 callout_reset(&sp->ifstart_callout, 1, sppp_ifstart_sched,
826 * Enqueue transmit packet.
829 sppp_output(struct ifnet *ifp, struct mbuf *m,
830 struct sockaddr *dst, struct rtentry *rt)
832 struct sppp *sp = IFP2SP(ifp);
833 struct ppp_header *h;
834 struct ifqueue *ifq = NULL;
835 int s, error, rv = 0;
836 int ipproto = PPP_IP;
837 int debug = ifp->if_flags & IFF_DEBUG;
842 if (!(ifp->if_flags & IFF_UP) ||
843 (!(ifp->if_flags & IFF_AUTO) &&
844 !(ifp->if_drv_flags & IFF_DRV_RUNNING))) {
854 if ((ifp->if_flags & IFF_AUTO) &&
855 !(ifp->if_drv_flags & IFF_DRV_RUNNING)) {
860 * Hack to prevent the initialization-time generated
861 * IPv6 multicast packet to erroneously cause a
862 * dialout event in case IPv6 has been
863 * administratively disabled on that interface.
865 if (dst->sa_family == AF_INET6 &&
866 !(sp->confflags & CONF_ENABLE_IPV6))
870 * Interface is not yet running, but auto-dial. Need
871 * to start LCP for it.
873 ifp->if_drv_flags |= IFF_DRV_RUNNING;
880 if (dst->sa_family == AF_INET) {
881 /* XXX Check mbuf length here? */
882 struct ip *ip = mtod (m, struct ip*);
883 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
886 * When using dynamic local IP address assignment by using
887 * 0.0.0.0 as a local address, the first TCP session will
888 * not connect because the local TCP checksum is computed
889 * using 0.0.0.0 which will later become our real IP address
890 * so the TCP checksum computed at the remote end will
891 * become invalid. So we
892 * - don't let packets with src ip addr 0 thru
893 * - we flag TCP packets with src ip 0 as an error
896 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
901 if(ip->ip_p == IPPROTO_TCP)
902 return(EADDRNOTAVAIL);
908 * Put low delay, telnet, rlogin and ftp control packets
909 * in front of the queue or let ALTQ take care.
911 if (ALTQ_IS_ENABLED(&ifp->if_snd))
913 else if (_IF_QFULL(&sp->pp_fastq))
915 else if (ip->ip_tos & IPTOS_LOWDELAY)
917 else if (m->m_len < sizeof *ip + sizeof *tcp)
919 else if (ip->ip_p != IPPROTO_TCP)
921 else if (INTERACTIVE (ntohs (tcp->th_sport)))
923 else if (INTERACTIVE (ntohs (tcp->th_dport)))
927 * Do IP Header compression
929 if (sp->pp_mode != IFF_CISCO && sp->pp_mode != PP_FR &&
930 (sp->ipcp.flags & IPCP_VJ) && ip->ip_p == IPPROTO_TCP)
931 switch (sl_compress_tcp(m, ip, sp->pp_comp,
932 sp->ipcp.compress_cid)) {
933 case TYPE_COMPRESSED_TCP:
934 ipproto = PPP_VJ_COMP;
936 case TYPE_UNCOMPRESSED_TCP:
937 ipproto = PPP_VJ_UCOMP;
952 if (dst->sa_family == AF_INET6) {
953 /* XXX do something tricky here? */
957 if (sp->pp_mode == PP_FR) {
958 /* Add frame relay header. */
959 m = sppp_fr_header (sp, m, dst->sa_family);
966 * Prepend general data packet PPP header. For now, IP only.
968 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
971 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
979 * May want to check size of packet
980 * (albeit due to the implementation it's always enough)
982 h = mtod (m, struct ppp_header*);
983 if (sp->pp_mode == IFF_CISCO) {
984 h->address = CISCO_UNICAST; /* unicast address */
987 h->address = PPP_ALLSTATIONS; /* broadcast address */
988 h->control = PPP_UI; /* Unnumbered Info */
991 switch (dst->sa_family) {
993 case AF_INET: /* Internet Protocol */
994 if (sp->pp_mode == IFF_CISCO)
995 h->protocol = htons (ETHERTYPE_IP);
998 * Don't choke with an ENETDOWN early. It's
999 * possible that we just started dialing out,
1000 * so don't drop the packet immediately. If
1001 * we notice that we run out of buffer space
1002 * below, we will however remember that we are
1003 * not ready to carry IP packets, and return
1004 * ENETDOWN, as opposed to ENOBUFS.
1006 h->protocol = htons(ipproto);
1007 if (sp->state[IDX_IPCP] != STATE_OPENED)
1013 case AF_INET6: /* Internet Protocol */
1014 if (sp->pp_mode == IFF_CISCO)
1015 h->protocol = htons (ETHERTYPE_IPV6);
1018 * Don't choke with an ENETDOWN early. It's
1019 * possible that we just started dialing out,
1020 * so don't drop the packet immediately. If
1021 * we notice that we run out of buffer space
1022 * below, we will however remember that we are
1023 * not ready to carry IP packets, and return
1024 * ENETDOWN, as opposed to ENOBUFS.
1026 h->protocol = htons(PPP_IPV6);
1027 if (sp->state[IDX_IPV6CP] != STATE_OPENED)
1033 case AF_IPX: /* Novell IPX Protocol */
1034 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
1035 ETHERTYPE_IPX : PPP_IPX);
1043 return (EAFNOSUPPORT);
1047 * Queue message on interface, and start output if interface
1052 error = !(IF_HANDOFF_ADJ(ifq, m, ifp, 3));
1054 IFQ_HANDOFF_ADJ(ifp, m, 3, error);
1059 return (rv? rv: ENOBUFS);
1064 * Unlike in sppp_input(), we can always bump the timestamp
1065 * here since sppp_output() is only called on behalf of
1066 * network-layer traffic; control-layer traffic is handled
1067 * by sppp_cp_send().
1069 sp->pp_last_sent = time_uptime;
1074 sppp_attach(struct ifnet *ifp)
1076 struct sppp *sp = IFP2SP(ifp);
1078 /* Initialize mtx lock */
1079 mtx_init(&sp->mtx, "sppp", MTX_NETWORK_LOCK, MTX_DEF | MTX_RECURSE);
1081 /* Initialize keepalive handler. */
1082 callout_init(&sp->keepalive_callout,
1083 (ifp->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
1084 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
1087 ifp->if_mtu = PP_MTU;
1088 ifp->if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
1089 ifp->if_output = sppp_output;
1091 sp->pp_flags = PP_KEEPALIVE;
1093 ifp->if_snd.ifq_maxlen = 32;
1094 sp->pp_fastq.ifq_maxlen = 32;
1095 sp->pp_cpq.ifq_maxlen = 20;
1097 sp->pp_alivecnt = 0;
1098 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq));
1099 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq));
1100 sp->pp_phase = PHASE_DEAD;
1101 sp->pp_up = sppp_pp_up;
1102 sp->pp_down = sppp_pp_down;
1103 if(!mtx_initialized(&sp->pp_cpq.ifq_mtx))
1104 mtx_init(&sp->pp_cpq.ifq_mtx, "sppp_cpq", NULL, MTX_DEF);
1105 if(!mtx_initialized(&sp->pp_fastq.ifq_mtx))
1106 mtx_init(&sp->pp_fastq.ifq_mtx, "sppp_fastq", NULL, MTX_DEF);
1107 sp->pp_last_recv = sp->pp_last_sent = time_uptime;
1110 sp->confflags |= CONF_ENABLE_VJ;
1113 sp->confflags |= CONF_ENABLE_IPV6;
1115 callout_init(&sp->ifstart_callout,
1116 (ifp->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
1117 sp->if_start = ifp->if_start;
1118 ifp->if_start = sppp_ifstart;
1119 sp->pp_comp = malloc(sizeof(struct slcompress), M_TEMP, M_WAITOK);
1120 sl_compress_init(sp->pp_comp, -1);
1123 sppp_ipv6cp_init(sp);
1129 sppp_detach(struct ifnet *ifp)
1131 struct sppp *sp = IFP2SP(ifp);
1134 KASSERT(mtx_initialized(&sp->mtx), ("sppp mutex is not initialized"));
1136 /* Stop keepalive handler. */
1137 if (!callout_drain(&sp->keepalive_callout))
1138 callout_stop(&sp->keepalive_callout);
1140 for (i = 0; i < IDX_COUNT; i++) {
1141 if (!callout_drain(&sp->ch[i]))
1142 callout_stop(&sp->ch[i]);
1144 if (!callout_drain(&sp->pap_my_to_ch))
1145 callout_stop(&sp->pap_my_to_ch);
1146 mtx_destroy(&sp->pp_cpq.ifq_mtx);
1147 mtx_destroy(&sp->pp_fastq.ifq_mtx);
1148 mtx_destroy(&sp->mtx);
1152 * Flush the interface output queue.
1155 sppp_flush_unlocked(struct ifnet *ifp)
1157 struct sppp *sp = IFP2SP(ifp);
1159 sppp_qflush ((struct ifqueue *)&SP2IFP(sp)->if_snd);
1160 sppp_qflush (&sp->pp_fastq);
1161 sppp_qflush (&sp->pp_cpq);
1165 sppp_flush(struct ifnet *ifp)
1167 struct sppp *sp = IFP2SP(ifp);
1170 sppp_flush_unlocked (ifp);
1175 * Check if the output queue is empty.
1178 sppp_isempty(struct ifnet *ifp)
1180 struct sppp *sp = IFP2SP(ifp);
1185 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
1186 !SP2IFP(sp)->if_snd.ifq_head;
1193 * Get next packet to send.
1196 sppp_dequeue(struct ifnet *ifp)
1198 struct sppp *sp = IFP2SP(ifp);
1205 * Process only the control protocol queue until we have at
1206 * least one NCP open.
1208 * Do always serve all three queues in Cisco mode.
1210 IF_DEQUEUE(&sp->pp_cpq, m);
1212 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO ||
1213 sp->pp_mode == PP_FR)) {
1214 IF_DEQUEUE(&sp->pp_fastq, m);
1216 IF_DEQUEUE (&SP2IFP(sp)->if_snd, m);
1224 * Pick the next packet, do not remove it from the queue.
1227 sppp_pick(struct ifnet *ifp)
1229 struct sppp *sp = IFP2SP(ifp);
1236 m = sp->pp_cpq.ifq_head;
1238 (sp->pp_phase == PHASE_NETWORK ||
1239 sp->pp_mode == IFF_CISCO ||
1240 sp->pp_mode == PP_FR))
1241 if ((m = sp->pp_fastq.ifq_head) == NULL)
1242 m = SP2IFP(sp)->if_snd.ifq_head;
1249 * Process an ioctl request. Called on low priority level.
1252 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
1254 struct ifreq *ifr = (struct ifreq*) data;
1255 struct sppp *sp = IFP2SP(ifp);
1256 int s, rv, going_up, going_down, newmode;
1263 case SIOCSIFDSTADDR:
1267 /* set the interface "up" when assigning an IP address */
1268 ifp->if_flags |= IFF_UP;
1272 going_up = ifp->if_flags & IFF_UP &&
1273 (ifp->if_drv_flags & IFF_DRV_RUNNING) == 0;
1274 going_down = (ifp->if_flags & IFF_UP) == 0 &&
1275 ifp->if_drv_flags & IFF_DRV_RUNNING;
1277 newmode = ifp->if_flags & IFF_PASSIVE;
1279 newmode = ifp->if_flags & IFF_AUTO;
1281 newmode = ifp->if_flags & IFF_CISCO;
1282 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
1283 ifp->if_flags |= newmode;
1286 newmode = sp->pp_flags & PP_FR;
1288 if (newmode != sp->pp_mode) {
1291 going_up = ifp->if_drv_flags & IFF_DRV_RUNNING;
1295 if (sp->pp_mode != IFF_CISCO &&
1296 sp->pp_mode != PP_FR)
1298 else if (sp->pp_tlf)
1300 sppp_flush_unlocked(ifp);
1301 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
1302 sp->pp_mode = newmode;
1306 if (sp->pp_mode != IFF_CISCO &&
1307 sp->pp_mode != PP_FR)
1309 sp->pp_mode = newmode;
1310 if (sp->pp_mode == 0) {
1311 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1314 if ((sp->pp_mode == IFF_CISCO) ||
1315 (sp->pp_mode == PP_FR)) {
1318 ifp->if_drv_flags |= IFF_DRV_RUNNING;
1326 #define ifr_mtu ifr_metric
1329 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1331 ifp->if_mtu = ifr->ifr_mtu;
1336 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1338 ifp->if_mtu = *(short*)data;
1343 ifr->ifr_mtu = ifp->if_mtu;
1348 *(short*)data = ifp->if_mtu;
1355 case SIOCGIFGENERIC:
1356 case SIOCSIFGENERIC:
1357 rv = sppp_params(sp, cmd, data);
1369 * Cisco framing implementation.
1373 * Handle incoming Cisco keepalive protocol packets.
1376 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1379 struct cisco_packet *h;
1382 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1385 SPP_FMT "cisco invalid packet length: %d bytes\n",
1386 SPP_ARGS(ifp), m->m_pkthdr.len);
1389 h = mtod (m, struct cisco_packet*);
1392 SPP_FMT "cisco input: %d bytes "
1393 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1394 SPP_ARGS(ifp), m->m_pkthdr.len,
1395 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1396 (u_int)h->time0, (u_int)h->time1);
1397 switch (ntohl (h->type)) {
1400 log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n",
1401 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1403 case CISCO_ADDR_REPLY:
1404 /* Reply on address request, ignore */
1406 case CISCO_KEEPALIVE_REQ:
1407 sp->pp_alivecnt = 0;
1408 sp->pp_rseq[IDX_LCP] = ntohl (h->par1);
1409 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) {
1410 /* Local and remote sequence numbers are equal.
1411 * Probably, the line is in loopback mode. */
1412 if (sp->pp_loopcnt >= MAXALIVECNT) {
1413 printf (SPP_FMT "loopback\n",
1416 if (ifp->if_flags & IFF_UP) {
1418 sppp_qflush (&sp->pp_cpq);
1423 /* Generate new local sequence number */
1424 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1425 sp->pp_seq[IDX_LCP] = random();
1427 sp->pp_seq[IDX_LCP] ^= time.tv_sec ^ time.tv_usec;
1432 if (! (ifp->if_flags & IFF_UP) &&
1433 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
1435 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1438 case CISCO_ADDR_REQ:
1439 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1441 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1447 * Send Cisco keepalive packet.
1450 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1453 struct ppp_header *h;
1454 struct cisco_packet *ch;
1458 getmicrouptime(&tv);
1460 MGETHDR (m, M_DONTWAIT, MT_DATA);
1463 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1464 m->m_pkthdr.rcvif = 0;
1466 h = mtod (m, struct ppp_header*);
1467 h->address = CISCO_MULTICAST;
1469 h->protocol = htons (CISCO_KEEPALIVE);
1471 ch = (struct cisco_packet*) (h + 1);
1472 ch->type = htonl (type);
1473 ch->par1 = htonl (par1);
1474 ch->par2 = htonl (par2);
1477 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1478 ch->time1 = htons ((u_short) tv.tv_sec);
1482 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1483 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1484 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1486 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1491 * PPP protocol implementation.
1495 * Send PPP control protocol packet.
1498 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1499 u_char ident, u_short len, void *data)
1502 struct ppp_header *h;
1503 struct lcp_header *lh;
1506 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1507 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1508 MGETHDR (m, M_DONTWAIT, MT_DATA);
1511 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1512 m->m_pkthdr.rcvif = 0;
1514 h = mtod (m, struct ppp_header*);
1515 h->address = PPP_ALLSTATIONS; /* broadcast address */
1516 h->control = PPP_UI; /* Unnumbered Info */
1517 h->protocol = htons (proto); /* Link Control Protocol */
1519 lh = (struct lcp_header*) (h + 1);
1522 lh->len = htons (LCP_HEADER_LEN + len);
1524 bcopy (data, lh+1, len);
1527 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1529 sppp_proto_name(proto),
1530 sppp_cp_type_name (lh->type), lh->ident,
1532 sppp_print_bytes ((u_char*) (lh+1), len);
1535 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1540 * Handle incoming PPP control protocol packets.
1543 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1546 struct lcp_header *h;
1547 int len = m->m_pkthdr.len;
1554 SPP_FMT "%s invalid packet length: %d bytes\n",
1555 SPP_ARGS(ifp), cp->name, len);
1558 h = mtod (m, struct lcp_header*);
1561 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1562 SPP_ARGS(ifp), cp->name,
1563 sppp_state_name(sp->state[cp->protoidx]),
1564 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1565 sppp_print_bytes ((u_char*) (h+1), len-4);
1568 if (len > ntohs (h->len))
1569 len = ntohs (h->len);
1570 p = (u_char *)(h + 1);
1575 log(-1, SPP_FMT "%s invalid conf-req length %d\n",
1576 SPP_ARGS(ifp), cp->name,
1581 /* handle states where RCR doesn't get a SCA/SCN */
1582 switch (sp->state[cp->protoidx]) {
1584 case STATE_STOPPING:
1587 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1591 rv = (cp->RCR)(sp, h, len);
1592 switch (sp->state[cp->protoidx]) {
1597 case STATE_ACK_SENT:
1598 case STATE_REQ_SENT:
1600 * sppp_cp_change_state() have the side effect of
1601 * restarting the timeouts. We want to avoid that
1602 * if the state don't change, otherwise we won't
1603 * ever timeout and resend a configuration request
1606 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1609 sppp_cp_change_state(cp, sp, rv?
1610 STATE_ACK_SENT: STATE_REQ_SENT);
1613 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1615 sppp_cp_change_state(cp, sp, rv?
1616 STATE_ACK_SENT: STATE_REQ_SENT);
1618 case STATE_ACK_RCVD:
1620 sppp_cp_change_state(cp, sp, STATE_OPENED);
1622 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1627 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1630 printf(SPP_FMT "%s illegal %s in state %s\n",
1631 SPP_ARGS(ifp), cp->name,
1632 sppp_cp_type_name(h->type),
1633 sppp_state_name(sp->state[cp->protoidx]));
1638 if (h->ident != sp->confid[cp->protoidx]) {
1640 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1641 SPP_ARGS(ifp), cp->name,
1642 h->ident, sp->confid[cp->protoidx]);
1646 switch (sp->state[cp->protoidx]) {
1649 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1652 case STATE_STOPPING:
1654 case STATE_REQ_SENT:
1655 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1656 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1661 case STATE_ACK_RCVD:
1663 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1665 case STATE_ACK_SENT:
1666 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1667 sppp_cp_change_state(cp, sp, STATE_OPENED);
1669 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1670 SPP_ARGS(ifp), cp->name);
1674 printf(SPP_FMT "%s illegal %s in state %s\n",
1675 SPP_ARGS(ifp), cp->name,
1676 sppp_cp_type_name(h->type),
1677 sppp_state_name(sp->state[cp->protoidx]));
1683 if (h->ident != sp->confid[cp->protoidx]) {
1685 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1686 SPP_ARGS(ifp), cp->name,
1687 h->ident, sp->confid[cp->protoidx]);
1691 if (h->type == CONF_NAK)
1692 (cp->RCN_nak)(sp, h, len);
1694 (cp->RCN_rej)(sp, h, len);
1696 switch (sp->state[cp->protoidx]) {
1699 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1701 case STATE_REQ_SENT:
1702 case STATE_ACK_SENT:
1703 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1705 * Slow things down a bit if we think we might be
1706 * in loopback. Depend on the timeout to send the
1707 * next configuration request.
1716 case STATE_ACK_RCVD:
1717 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1721 case STATE_STOPPING:
1724 printf(SPP_FMT "%s illegal %s in state %s\n",
1725 SPP_ARGS(ifp), cp->name,
1726 sppp_cp_type_name(h->type),
1727 sppp_state_name(sp->state[cp->protoidx]));
1733 switch (sp->state[cp->protoidx]) {
1734 case STATE_ACK_RCVD:
1735 case STATE_ACK_SENT:
1736 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1741 case STATE_STOPPING:
1742 case STATE_REQ_SENT:
1744 /* Send Terminate-Ack packet. */
1746 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1747 SPP_ARGS(ifp), cp->name);
1748 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1752 sp->rst_counter[cp->protoidx] = 0;
1753 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1757 printf(SPP_FMT "%s illegal %s in state %s\n",
1758 SPP_ARGS(ifp), cp->name,
1759 sppp_cp_type_name(h->type),
1760 sppp_state_name(sp->state[cp->protoidx]));
1765 switch (sp->state[cp->protoidx]) {
1768 case STATE_REQ_SENT:
1769 case STATE_ACK_SENT:
1772 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1775 case STATE_STOPPING:
1776 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1779 case STATE_ACK_RCVD:
1780 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1785 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1788 printf(SPP_FMT "%s illegal %s in state %s\n",
1789 SPP_ARGS(ifp), cp->name,
1790 sppp_cp_type_name(h->type),
1791 sppp_state_name(sp->state[cp->protoidx]));
1796 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1798 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1799 "danger will robinson\n",
1800 SPP_ARGS(ifp), cp->name,
1801 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1802 switch (sp->state[cp->protoidx]) {
1805 case STATE_REQ_SENT:
1806 case STATE_ACK_SENT:
1808 case STATE_STOPPING:
1811 case STATE_ACK_RCVD:
1812 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1815 printf(SPP_FMT "%s illegal %s in state %s\n",
1816 SPP_ARGS(ifp), cp->name,
1817 sppp_cp_type_name(h->type),
1818 sppp_state_name(sp->state[cp->protoidx]));
1825 const struct cp *upper;
1831 proto = ntohs(*((u_int16_t *)p));
1832 for (i = 0; i < IDX_COUNT; i++) {
1833 if (cps[i]->proto == proto) {
1841 if (catastrophic || debug)
1842 log(catastrophic? LOG_INFO: LOG_DEBUG,
1843 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n",
1844 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+',
1845 sppp_cp_type_name(h->type), proto,
1846 upper ? upper->name : "unknown",
1847 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?");
1850 * if we got RXJ+ against conf-req, the peer does not implement
1851 * this particular protocol type. terminate the protocol.
1853 if (upper && !catastrophic) {
1854 if (sp->state[upper->protoidx] == STATE_REQ_SENT) {
1860 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1861 switch (sp->state[cp->protoidx]) {
1864 case STATE_REQ_SENT:
1865 case STATE_ACK_SENT:
1867 case STATE_STOPPING:
1870 case STATE_ACK_RCVD:
1871 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1874 printf(SPP_FMT "%s illegal %s in state %s\n",
1875 SPP_ARGS(ifp), cp->name,
1876 sppp_cp_type_name(h->type),
1877 sppp_state_name(sp->state[cp->protoidx]));
1883 if (cp->proto != PPP_LCP)
1885 /* Discard the packet. */
1888 if (cp->proto != PPP_LCP)
1890 if (sp->state[cp->protoidx] != STATE_OPENED) {
1892 log(-1, SPP_FMT "lcp echo req but lcp closed\n",
1899 log(-1, SPP_FMT "invalid lcp echo request "
1900 "packet length: %d bytes\n",
1901 SPP_ARGS(ifp), len);
1904 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1905 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1906 /* Line loopback mode detected. */
1907 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1908 sp->pp_loopcnt = MAXALIVECNT * 5;
1910 sppp_qflush (&sp->pp_cpq);
1912 /* Shut down the PPP link. */
1918 *(long*)(h+1) = htonl (sp->lcp.magic);
1920 log(-1, SPP_FMT "got lcp echo req, sending echo rep\n",
1922 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1925 if (cp->proto != PPP_LCP)
1927 if (h->ident != sp->lcp.echoid) {
1933 log(-1, SPP_FMT "lcp invalid echo reply "
1934 "packet length: %d bytes\n",
1935 SPP_ARGS(ifp), len);
1939 log(-1, SPP_FMT "lcp got echo rep\n",
1941 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1942 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1943 sp->pp_alivecnt = 0;
1946 /* Unknown packet type -- send Code-Reject packet. */
1949 log(-1, SPP_FMT "%s send code-rej for 0x%x\n",
1950 SPP_ARGS(ifp), cp->name, h->type);
1951 sppp_cp_send(sp, cp->proto, CODE_REJ,
1952 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h);
1959 * The generic part of all Up/Down/Open/Close/TO event handlers.
1960 * Basically, the state transition handling in the automaton.
1963 sppp_up_event(const struct cp *cp, struct sppp *sp)
1968 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1969 SPP_ARGS(ifp), cp->name,
1970 sppp_state_name(sp->state[cp->protoidx]));
1972 switch (sp->state[cp->protoidx]) {
1974 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1976 case STATE_STARTING:
1977 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1979 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1982 printf(SPP_FMT "%s illegal up in state %s\n",
1983 SPP_ARGS(ifp), cp->name,
1984 sppp_state_name(sp->state[cp->protoidx]));
1989 sppp_down_event(const struct cp *cp, struct sppp *sp)
1994 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1995 SPP_ARGS(ifp), cp->name,
1996 sppp_state_name(sp->state[cp->protoidx]));
1998 switch (sp->state[cp->protoidx]) {
2001 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2004 sppp_cp_change_state(cp, sp, STATE_STARTING);
2007 case STATE_STOPPING:
2008 case STATE_REQ_SENT:
2009 case STATE_ACK_RCVD:
2010 case STATE_ACK_SENT:
2011 sppp_cp_change_state(cp, sp, STATE_STARTING);
2015 sppp_cp_change_state(cp, sp, STATE_STARTING);
2018 printf(SPP_FMT "%s illegal down in state %s\n",
2019 SPP_ARGS(ifp), cp->name,
2020 sppp_state_name(sp->state[cp->protoidx]));
2026 sppp_open_event(const struct cp *cp, struct sppp *sp)
2031 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
2032 SPP_ARGS(ifp), cp->name,
2033 sppp_state_name(sp->state[cp->protoidx]));
2035 switch (sp->state[cp->protoidx]) {
2037 sppp_cp_change_state(cp, sp, STATE_STARTING);
2040 case STATE_STARTING:
2043 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
2045 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2049 * Try escaping stopped state. This seems to bite
2050 * people occasionally, in particular for IPCP,
2051 * presumably following previous IPCP negotiation
2052 * aborts. Somehow, we must have missed a Down event
2053 * which would have caused a transition into starting
2054 * state, so as a bandaid we force the Down event now.
2055 * This effectively implements (something like the)
2056 * `restart' option mentioned in the state transition
2057 * table of RFC 1661.
2059 sppp_cp_change_state(cp, sp, STATE_STARTING);
2062 case STATE_STOPPING:
2063 case STATE_REQ_SENT:
2064 case STATE_ACK_RCVD:
2065 case STATE_ACK_SENT:
2069 sppp_cp_change_state(cp, sp, STATE_STOPPING);
2076 sppp_close_event(const struct cp *cp, struct sppp *sp)
2081 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
2082 SPP_ARGS(ifp), cp->name,
2083 sppp_state_name(sp->state[cp->protoidx]));
2085 switch (sp->state[cp->protoidx]) {
2090 case STATE_STARTING:
2091 sppp_cp_change_state(cp, sp, STATE_INITIAL);
2095 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2097 case STATE_STOPPING:
2098 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2103 case STATE_REQ_SENT:
2104 case STATE_ACK_RCVD:
2105 case STATE_ACK_SENT:
2106 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
2107 sppp_cp_send(sp, cp->proto, TERM_REQ,
2108 ++sp->pp_seq[cp->protoidx], 0, 0);
2109 sppp_cp_change_state(cp, sp, STATE_CLOSING);
2115 sppp_to_event(const struct cp *cp, struct sppp *sp)
2123 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
2124 SPP_ARGS(ifp), cp->name,
2125 sppp_state_name(sp->state[cp->protoidx]),
2126 sp->rst_counter[cp->protoidx]);
2128 if (--sp->rst_counter[cp->protoidx] < 0)
2130 switch (sp->state[cp->protoidx]) {
2132 sppp_cp_change_state(cp, sp, STATE_CLOSED);
2135 case STATE_STOPPING:
2136 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2139 case STATE_REQ_SENT:
2140 case STATE_ACK_RCVD:
2141 case STATE_ACK_SENT:
2142 sppp_cp_change_state(cp, sp, STATE_STOPPED);
2148 switch (sp->state[cp->protoidx]) {
2150 case STATE_STOPPING:
2151 sppp_cp_send(sp, cp->proto, TERM_REQ,
2152 ++sp->pp_seq[cp->protoidx], 0, 0);
2153 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2154 cp->TO, (void *)sp);
2156 case STATE_REQ_SENT:
2157 case STATE_ACK_RCVD:
2159 /* sppp_cp_change_state() will restart the timer */
2160 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
2162 case STATE_ACK_SENT:
2164 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2165 cp->TO, (void *)sp);
2174 * Change the state of a control protocol in the state automaton.
2175 * Takes care of starting/stopping the restart timer.
2178 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
2180 sp->state[cp->protoidx] = newstate;
2182 callout_stop (&sp->ch[cp->protoidx]);
2186 case STATE_STARTING:
2192 case STATE_STOPPING:
2193 case STATE_REQ_SENT:
2194 case STATE_ACK_RCVD:
2195 case STATE_ACK_SENT:
2196 callout_reset(&sp->ch[cp->protoidx], sp->lcp.timeout,
2197 cp->TO, (void *)sp);
2203 *--------------------------------------------------------------------------*
2205 * The LCP implementation. *
2207 *--------------------------------------------------------------------------*
2210 sppp_pp_up(struct sppp *sp)
2218 sppp_pp_down(struct sppp *sp)
2226 sppp_lcp_init(struct sppp *sp)
2228 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2230 sp->state[IDX_LCP] = STATE_INITIAL;
2231 sp->fail_counter[IDX_LCP] = 0;
2232 sp->pp_seq[IDX_LCP] = 0;
2233 sp->pp_rseq[IDX_LCP] = 0;
2235 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2237 /* Note that these values are relevant for all control protocols */
2238 sp->lcp.timeout = 3 * hz;
2239 sp->lcp.max_terminate = 2;
2240 sp->lcp.max_configure = 10;
2241 sp->lcp.max_failure = 10;
2242 callout_init(&sp->ch[IDX_LCP],
2243 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
2247 sppp_lcp_up(struct sppp *sp)
2251 sp->pp_alivecnt = 0;
2252 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
2255 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
2257 * If we are authenticator, negotiate LCP_AUTH
2259 if (sp->hisauth.proto != 0)
2260 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
2262 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2263 sp->pp_flags &= ~PP_NEEDAUTH;
2265 * If this interface is passive or dial-on-demand, and we are
2266 * still in Initial state, it means we've got an incoming
2267 * call. Activate the interface.
2269 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
2272 SPP_FMT "Up event", SPP_ARGS(ifp));
2273 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2274 if (sp->state[IDX_LCP] == STATE_INITIAL) {
2276 log(-1, "(incoming call)\n");
2277 sp->pp_flags |= PP_CALLIN;
2281 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 &&
2282 (sp->state[IDX_LCP] == STATE_INITIAL)) {
2283 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2287 sppp_up_event(&lcp, sp);
2291 sppp_lcp_down(struct sppp *sp)
2295 sppp_down_event(&lcp, sp);
2298 * If this is neither a dial-on-demand nor a passive
2299 * interface, simulate an ``ifconfig down'' action, so the
2300 * administrator can force a redial by another ``ifconfig
2301 * up''. XXX For leased line operation, should we immediately
2302 * try to reopen the connection here?
2304 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
2306 SPP_FMT "Down event, taking interface down.\n",
2312 SPP_FMT "Down event (carrier loss)\n",
2314 sp->pp_flags &= ~PP_CALLIN;
2315 if (sp->state[IDX_LCP] != STATE_INITIAL)
2317 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2322 sppp_lcp_open(struct sppp *sp)
2324 sppp_open_event(&lcp, sp);
2328 sppp_lcp_close(struct sppp *sp)
2330 sppp_close_event(&lcp, sp);
2334 sppp_lcp_TO(void *cookie)
2336 sppp_to_event(&lcp, (struct sppp *)cookie);
2340 * Analyze a configure request. Return true if it was agreeable, and
2341 * caused action sca, false if it has been rejected or nak'ed, and
2342 * caused action scn. (The return value is used to make the state
2343 * transition decision in the state automaton.)
2346 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2349 u_char *buf, *r, *p;
2356 buf = r = malloc (len, M_TEMP, M_NOWAIT);
2361 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
2364 /* pass 1: check for things that need to be rejected */
2366 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2367 len-=p[1], p+=p[1]) {
2369 log(-1, " %s ", sppp_lcp_opt_name(*p));
2373 if (len >= 6 && p[1] == 6)
2376 log(-1, "[invalid] ");
2378 case LCP_OPT_ASYNC_MAP:
2379 /* Async control character map. */
2380 if (len >= 6 && p[1] == 6)
2383 log(-1, "[invalid] ");
2386 /* Maximum receive unit. */
2387 if (len >= 4 && p[1] == 4)
2390 log(-1, "[invalid] ");
2392 case LCP_OPT_AUTH_PROTO:
2395 log(-1, "[invalid] ");
2398 authproto = (p[2] << 8) + p[3];
2399 if (authproto == PPP_CHAP && p[1] != 5) {
2401 log(-1, "[invalid chap len] ");
2404 if (sp->myauth.proto == 0) {
2405 /* we are not configured to do auth */
2407 log(-1, "[not configured] ");
2411 * Remote want us to authenticate, remember this,
2412 * so we stay in PHASE_AUTHENTICATE after LCP got
2415 sp->pp_flags |= PP_NEEDAUTH;
2418 /* Others not supported. */
2423 /* Add the option to rejected list. */
2430 log(-1, " send conf-rej\n");
2431 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2437 * pass 2: check for option values that are unacceptable and
2438 * thus require to be nak'ed.
2441 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2446 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
2447 len-=p[1], p+=p[1]) {
2449 log(-1, " %s ", sppp_lcp_opt_name(*p));
2452 /* Magic number -- extract. */
2453 nmagic = (u_long)p[2] << 24 |
2454 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2455 if (nmagic != sp->lcp.magic) {
2458 log(-1, "0x%lx ", nmagic);
2461 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2462 log(-1, "[glitch] ");
2465 * We negate our magic here, and NAK it. If
2466 * we see it later in an NAK packet, we
2467 * suggest a new one.
2469 nmagic = ~sp->lcp.magic;
2471 p[2] = nmagic >> 24;
2472 p[3] = nmagic >> 16;
2477 case LCP_OPT_ASYNC_MAP:
2479 * Async control character map -- just ignore it.
2481 * Quote from RFC 1662, chapter 6:
2482 * To enable this functionality, synchronous PPP
2483 * implementations MUST always respond to the
2484 * Async-Control-Character-Map Configuration
2485 * Option with the LCP Configure-Ack. However,
2486 * acceptance of the Configuration Option does
2487 * not imply that the synchronous implementation
2488 * will do any ACCM mapping. Instead, all such
2489 * octet mapping will be performed by the
2490 * asynchronous-to-synchronous converter.
2496 * Maximum receive unit. Always agreeable,
2497 * but ignored by now.
2499 sp->lcp.their_mru = p[2] * 256 + p[3];
2501 log(-1, "%lu ", sp->lcp.their_mru);
2504 case LCP_OPT_AUTH_PROTO:
2505 authproto = (p[2] << 8) + p[3];
2506 if (sp->myauth.proto != authproto) {
2507 /* not agreed, nak */
2509 log(-1, "[mine %s != his %s] ",
2510 sppp_proto_name(sp->hisauth.proto),
2511 sppp_proto_name(authproto));
2512 p[2] = sp->myauth.proto >> 8;
2513 p[3] = sp->myauth.proto;
2516 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2518 log(-1, "[chap not MD5] ");
2524 /* Add the option to nak'ed list. */
2531 * Local and remote magics equal -- loopback?
2533 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2534 if (sp->pp_loopcnt == MAXALIVECNT*5)
2535 printf (SPP_FMT "loopback\n",
2537 if (ifp->if_flags & IFF_UP) {
2539 sppp_qflush(&sp->pp_cpq);
2544 } else if (!sp->pp_loopcnt &&
2545 ++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2547 log(-1, " max_failure (%d) exceeded, "
2549 sp->lcp.max_failure);
2550 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2553 log(-1, " send conf-nak\n");
2554 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2558 log(-1, " send conf-ack\n");
2559 sp->fail_counter[IDX_LCP] = 0;
2561 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2562 h->ident, origlen, h+1);
2570 * Analyze the LCP Configure-Reject option list, and adjust our
2574 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2580 buf = malloc (len, M_TEMP, M_NOWAIT);
2585 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2589 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2590 len -= p[1], p += p[1]) {
2592 log(-1, " %s ", sppp_lcp_opt_name(*p));
2595 /* Magic number -- can't use it, use 0 */
2596 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2601 * Should not be rejected anyway, since we only
2602 * negotiate a MRU if explicitly requested by
2605 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2607 case LCP_OPT_AUTH_PROTO:
2609 * Peer doesn't want to authenticate himself,
2610 * deny unless this is a dialout call, and
2611 * AUTHFLAG_NOCALLOUT is set.
2613 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2614 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2616 log(-1, "[don't insist on auth "
2618 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2622 log(-1, "[access denied]\n");
2634 * Analyze the LCP Configure-NAK option list, and adjust our
2638 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2645 buf = malloc (len, M_TEMP, M_NOWAIT);
2650 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2654 for (; len >= 2 && p[1] >= 2 && len >= p[1];
2655 len -= p[1], p += p[1]) {
2657 log(-1, " %s ", sppp_lcp_opt_name(*p));
2660 /* Magic number -- renegotiate */
2661 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2662 len >= 6 && p[1] == 6) {
2663 magic = (u_long)p[2] << 24 |
2664 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2666 * If the remote magic is our negated one,
2667 * this looks like a loopback problem.
2668 * Suggest a new magic to make sure.
2670 if (magic == ~sp->lcp.magic) {
2672 log(-1, "magic glitch ");
2673 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2674 sp->lcp.magic = random();
2676 sp->lcp.magic = time.tv_sec + time.tv_usec;
2679 sp->lcp.magic = magic;
2681 log(-1, "%lu ", magic);
2687 * Peer wants to advise us to negotiate an MRU.
2688 * Agree on it if it's reasonable, or use
2689 * default otherwise.
2691 if (len >= 4 && p[1] == 4) {
2692 u_int mru = p[2] * 256 + p[3];
2694 log(-1, "%d ", mru);
2695 if (mru < PP_MTU || mru > PP_MAX_MRU)
2698 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2701 case LCP_OPT_AUTH_PROTO:
2703 * Peer doesn't like our authentication method,
2707 log(-1, "[access denied]\n");
2719 sppp_lcp_tlu(struct sppp *sp)
2726 if (! (ifp->if_flags & IFF_UP) &&
2727 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2728 /* Coming out of loopback mode. */
2730 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2733 for (i = 0; i < IDX_COUNT; i++)
2734 if ((cps[i])->flags & CP_QUAL)
2737 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2738 (sp->pp_flags & PP_NEEDAUTH) != 0)
2739 sp->pp_phase = PHASE_AUTHENTICATE;
2741 sp->pp_phase = PHASE_NETWORK;
2744 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2745 sppp_phase_name(sp->pp_phase));
2748 * Open all authentication protocols. This is even required
2749 * if we already proceeded to network phase, since it might be
2750 * that remote wants us to authenticate, so we might have to
2751 * send a PAP request. Undesired authentication protocols
2752 * don't do anything when they get an Open event.
2754 for (i = 0; i < IDX_COUNT; i++)
2755 if ((cps[i])->flags & CP_AUTH)
2758 if (sp->pp_phase == PHASE_NETWORK) {
2759 /* Notify all NCPs. */
2760 for (i = 0; i < IDX_COUNT; i++)
2761 if (((cps[i])->flags & CP_NCP) &&
2764 * Hack to administratively disable IPv6 if
2765 * not desired. Perhaps we should have another
2766 * flag for this, but right now, we can make
2767 * all struct cp's read/only.
2769 (cps[i] != &ipv6cp ||
2770 (sp->confflags & CONF_ENABLE_IPV6)))
2774 /* Send Up events to all started protos. */
2775 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2776 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0)
2779 /* notify low-level driver of state change */
2781 sp->pp_chg(sp, (int)sp->pp_phase);
2783 if (sp->pp_phase == PHASE_NETWORK)
2784 /* if no NCP is starting, close down */
2785 sppp_lcp_check_and_close(sp);
2789 sppp_lcp_tld(struct sppp *sp)
2795 sp->pp_phase = PHASE_TERMINATE;
2798 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2799 sppp_phase_name(sp->pp_phase));
2802 * Take upper layers down. We send the Down event first and
2803 * the Close second to prevent the upper layers from sending
2804 * ``a flurry of terminate-request packets'', as the RFC
2807 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2808 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) {
2810 (cps[i])->Close(sp);
2815 sppp_lcp_tls(struct sppp *sp)
2819 sp->pp_phase = PHASE_ESTABLISH;
2822 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2823 sppp_phase_name(sp->pp_phase));
2825 /* Notify lower layer if desired. */
2833 sppp_lcp_tlf(struct sppp *sp)
2837 sp->pp_phase = PHASE_DEAD;
2839 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2840 sppp_phase_name(sp->pp_phase));
2842 /* Notify lower layer if desired. */
2850 sppp_lcp_scr(struct sppp *sp)
2852 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2856 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2857 if (! sp->lcp.magic)
2858 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2859 sp->lcp.magic = random();
2861 sp->lcp.magic = time.tv_sec + time.tv_usec;
2863 opt[i++] = LCP_OPT_MAGIC;
2865 opt[i++] = sp->lcp.magic >> 24;
2866 opt[i++] = sp->lcp.magic >> 16;
2867 opt[i++] = sp->lcp.magic >> 8;
2868 opt[i++] = sp->lcp.magic;
2871 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2872 opt[i++] = LCP_OPT_MRU;
2874 opt[i++] = sp->lcp.mru >> 8;
2875 opt[i++] = sp->lcp.mru;
2878 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2879 authproto = sp->hisauth.proto;
2880 opt[i++] = LCP_OPT_AUTH_PROTO;
2881 opt[i++] = authproto == PPP_CHAP? 5: 4;
2882 opt[i++] = authproto >> 8;
2883 opt[i++] = authproto;
2884 if (authproto == PPP_CHAP)
2885 opt[i++] = CHAP_MD5;
2888 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP];
2889 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2893 * Check the open NCPs, return true if at least one NCP is open.
2896 sppp_ncp_check(struct sppp *sp)
2900 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2901 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP)
2907 * Re-check the open NCPs and see if we should terminate the link.
2908 * Called by the NCPs during their tlf action handling.
2911 sppp_lcp_check_and_close(struct sppp *sp)
2914 if (sp->pp_phase < PHASE_NETWORK)
2915 /* don't bother, we are already going down */
2918 if (sppp_ncp_check(sp))
2925 *--------------------------------------------------------------------------*
2927 * The IPCP implementation. *
2929 *--------------------------------------------------------------------------*
2933 sppp_ipcp_init(struct sppp *sp)
2937 sp->state[IDX_IPCP] = STATE_INITIAL;
2938 sp->fail_counter[IDX_IPCP] = 0;
2939 sp->pp_seq[IDX_IPCP] = 0;
2940 sp->pp_rseq[IDX_IPCP] = 0;
2941 callout_init(&sp->ch[IDX_IPCP],
2942 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
2946 sppp_ipcp_up(struct sppp *sp)
2948 sppp_up_event(&ipcp, sp);
2952 sppp_ipcp_down(struct sppp *sp)
2954 sppp_down_event(&ipcp, sp);
2958 sppp_ipcp_open(struct sppp *sp)
2961 u_long myaddr, hisaddr;
2963 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN |
2964 IPCP_MYADDR_DYN | IPCP_VJ);
2967 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2969 * If we don't have his address, this probably means our
2970 * interface doesn't want to talk IP at all. (This could
2971 * be the case if somebody wants to speak only IPX, for
2972 * example.) Don't open IPCP in this case.
2974 if (hisaddr == 0L) {
2975 /* XXX this message should go away */
2977 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2983 * I don't have an assigned address, so i need to
2984 * negotiate my address.
2986 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2987 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2989 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2990 if (sp->confflags & CONF_ENABLE_VJ) {
2991 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION);
2992 sp->ipcp.max_state = MAX_STATES - 1;
2993 sp->ipcp.compress_cid = 1;
2995 sppp_open_event(&ipcp, sp);
2999 sppp_ipcp_close(struct sppp *sp)
3001 sppp_close_event(&ipcp, sp);
3002 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
3004 * My address was dynamic, clear it again.
3006 sppp_set_ip_addr(sp, 0L);
3010 sppp_ipcp_TO(void *cookie)
3012 sppp_to_event(&ipcp, (struct sppp *)cookie);
3016 * Analyze a configure request. Return true if it was agreeable, and
3017 * caused action sca, false if it has been rejected or nak'ed, and
3018 * caused action scn. (The return value is used to make the state
3019 * transition decision in the state automaton.)
3022 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3024 u_char *buf, *r, *p;
3025 struct ifnet *ifp = SP2IFP(sp);
3026 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3027 u_long hisaddr, desiredaddr;
3034 * Make sure to allocate a buf that can at least hold a
3035 * conf-nak with an `address' option. We might need it below.
3037 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3041 /* pass 1: see if we can recognize them */
3043 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
3046 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3047 len-=p[1], p+=p[1]) {
3049 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3051 case IPCP_OPT_COMPRESSION:
3052 if (!(sp->confflags & CONF_ENABLE_VJ)) {
3053 /* VJ compression administratively disabled */
3055 log(-1, "[locally disabled] ");
3059 * In theory, we should only conf-rej an
3060 * option that is shorter than RFC 1618
3061 * requires (i.e. < 4), and should conf-nak
3062 * anything else that is not VJ. However,
3063 * since our algorithm always uses the
3064 * original option to NAK it with new values,
3065 * things would become more complicated. In
3066 * pratice, the only commonly implemented IP
3067 * compression option is VJ anyway, so the
3068 * difference is negligible.
3070 if (len >= 6 && p[1] == 6) {
3072 * correctly formed compression option
3073 * that could be VJ compression
3079 "optlen %d [invalid/unsupported] ",
3082 case IPCP_OPT_ADDRESS:
3083 if (len >= 6 && p[1] == 6) {
3084 /* correctly formed address option */
3088 log(-1, "[invalid] ");
3091 /* Others not supported. */
3096 /* Add the option to rejected list. */
3103 log(-1, " send conf-rej\n");
3104 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
3109 /* pass 2: parse option values */
3110 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
3112 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
3116 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3117 len-=p[1], p+=p[1]) {
3119 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3121 case IPCP_OPT_COMPRESSION:
3122 desiredcomp = p[2] << 8 | p[3];
3123 /* We only support VJ */
3124 if (desiredcomp == IPCP_COMP_VJ) {
3126 log(-1, "VJ [ack] ");
3127 sp->ipcp.flags |= IPCP_VJ;
3128 sl_compress_init(sp->pp_comp, p[4]);
3129 sp->ipcp.max_state = p[4];
3130 sp->ipcp.compress_cid = p[5];
3135 "compproto %#04x [not supported] ",
3137 p[2] = IPCP_COMP_VJ >> 8;
3138 p[3] = IPCP_COMP_VJ;
3139 p[4] = sp->ipcp.max_state;
3140 p[5] = sp->ipcp.compress_cid;
3142 case IPCP_OPT_ADDRESS:
3143 /* This is the address he wants in his end */
3144 desiredaddr = p[2] << 24 | p[3] << 16 |
3146 if (desiredaddr == hisaddr ||
3147 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) {
3149 * Peer's address is same as our value,
3150 * or we have set it to 0.0.0.* to
3151 * indicate that we do not really care,
3152 * this is agreeable. Gonna conf-ack
3156 log(-1, "%s [ack] ",
3157 sppp_dotted_quad(hisaddr));
3158 /* record that we've seen it already */
3159 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
3163 * The address wasn't agreeable. This is either
3164 * he sent us 0.0.0.0, asking to assign him an
3165 * address, or he send us another address not
3166 * matching our value. Either case, we gonna
3167 * conf-nak it with our value.
3168 * XXX: we should "rej" if hisaddr == 0
3171 if (desiredaddr == 0)
3172 log(-1, "[addr requested] ");
3174 log(-1, "%s [not agreed] ",
3175 sppp_dotted_quad(desiredaddr));
3178 p[2] = hisaddr >> 24;
3179 p[3] = hisaddr >> 16;
3180 p[4] = hisaddr >> 8;
3184 /* Add the option to nak'ed list. */
3191 * If we are about to conf-ack the request, but haven't seen
3192 * his address so far, gonna conf-nak it instead, with the
3193 * `address' option present and our idea of his address being
3194 * filled in there, to request negotiation of both addresses.
3196 * XXX This can result in an endless req - nak loop if peer
3197 * doesn't want to send us his address. Q: What should we do
3198 * about it? XXX A: implement the max-failure counter.
3200 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
3201 buf[0] = IPCP_OPT_ADDRESS;
3203 buf[2] = hisaddr >> 24;
3204 buf[3] = hisaddr >> 16;
3205 buf[4] = hisaddr >> 8;
3209 log(-1, "still need hisaddr ");
3214 log(-1, " send conf-nak\n");
3215 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
3218 log(-1, " send conf-ack\n");
3219 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
3220 h->ident, origlen, h+1);
3228 * Analyze the IPCP Configure-Reject option list, and adjust our
3232 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3235 struct ifnet *ifp = SP2IFP(sp);
3236 int debug = ifp->if_flags & IFF_DEBUG;
3239 buf = malloc (len, M_TEMP, M_NOWAIT);
3244 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
3248 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3249 len -= p[1], p += p[1]) {
3251 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3253 case IPCP_OPT_COMPRESSION:
3254 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION);
3256 case IPCP_OPT_ADDRESS:
3258 * Peer doesn't grok address option. This is
3259 * bad. XXX Should we better give up here?
3260 * XXX We could try old "addresses" option...
3262 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
3273 * Analyze the IPCP Configure-NAK option list, and adjust our
3277 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3280 struct ifnet *ifp = SP2IFP(sp);
3281 int debug = ifp->if_flags & IFF_DEBUG;
3286 buf = malloc (len, M_TEMP, M_NOWAIT);
3291 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
3295 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3296 len -= p[1], p += p[1]) {
3298 log(-1, " %s ", sppp_ipcp_opt_name(*p));
3300 case IPCP_OPT_COMPRESSION:
3301 if (len >= 6 && p[1] == 6) {
3302 desiredcomp = p[2] << 8 | p[3];
3304 log(-1, "[wantcomp %#04x] ",
3306 if (desiredcomp == IPCP_COMP_VJ) {
3307 sl_compress_init(sp->pp_comp, p[4]);
3308 sp->ipcp.max_state = p[4];
3309 sp->ipcp.compress_cid = p[5];
3311 log(-1, "[agree] ");
3314 ~(1 << IPCP_OPT_COMPRESSION);
3317 case IPCP_OPT_ADDRESS:
3319 * Peer doesn't like our local IP address. See
3320 * if we can do something for him. We'll drop
3321 * him our address then.
3323 if (len >= 6 && p[1] == 6) {
3324 wantaddr = p[2] << 24 | p[3] << 16 |
3326 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
3328 log(-1, "[wantaddr %s] ",
3329 sppp_dotted_quad(wantaddr));
3331 * When doing dynamic address assignment,
3332 * we accept his offer. Otherwise, we
3333 * ignore it and thus continue to negotiate
3334 * our already existing value.
3335 * XXX: Bogus, if he said no once, he'll
3336 * just say no again, might as well die.
3338 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
3339 sppp_set_ip_addr(sp, wantaddr);
3341 log(-1, "[agree] ");
3342 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
3355 sppp_ipcp_tlu(struct sppp *sp)
3357 /* we are up - notify isdn daemon */
3363 sppp_ipcp_tld(struct sppp *sp)
3368 sppp_ipcp_tls(struct sppp *sp)
3370 /* indicate to LCP that it must stay alive */
3371 sp->lcp.protos |= (1 << IDX_IPCP);
3375 sppp_ipcp_tlf(struct sppp *sp)
3377 /* we no longer need LCP */
3378 sp->lcp.protos &= ~(1 << IDX_IPCP);
3379 sppp_lcp_check_and_close(sp);
3383 sppp_ipcp_scr(struct sppp *sp)
3385 char opt[6 /* compression */ + 6 /* address */];
3389 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) {
3390 opt[i++] = IPCP_OPT_COMPRESSION;
3392 opt[i++] = IPCP_COMP_VJ >> 8;
3393 opt[i++] = IPCP_COMP_VJ;
3394 opt[i++] = sp->ipcp.max_state;
3395 opt[i++] = sp->ipcp.compress_cid;
3397 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
3398 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
3399 opt[i++] = IPCP_OPT_ADDRESS;
3401 opt[i++] = ouraddr >> 24;
3402 opt[i++] = ouraddr >> 16;
3403 opt[i++] = ouraddr >> 8;
3407 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP];
3408 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
3412 *--------------------------------------------------------------------------*
3414 * The IPv6CP implementation. *
3416 *--------------------------------------------------------------------------*
3421 sppp_ipv6cp_init(struct sppp *sp)
3423 sp->ipv6cp.opts = 0;
3424 sp->ipv6cp.flags = 0;
3425 sp->state[IDX_IPV6CP] = STATE_INITIAL;
3426 sp->fail_counter[IDX_IPV6CP] = 0;
3427 sp->pp_seq[IDX_IPV6CP] = 0;
3428 sp->pp_rseq[IDX_IPV6CP] = 0;
3429 callout_init(&sp->ch[IDX_IPV6CP],
3430 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
3434 sppp_ipv6cp_up(struct sppp *sp)
3436 sppp_up_event(&ipv6cp, sp);
3440 sppp_ipv6cp_down(struct sppp *sp)
3442 sppp_down_event(&ipv6cp, sp);
3446 sppp_ipv6cp_open(struct sppp *sp)
3449 struct in6_addr myaddr, hisaddr;
3451 #ifdef IPV6CP_MYIFID_DYN
3452 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN);
3454 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN;
3457 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0);
3459 * If we don't have our address, this probably means our
3460 * interface doesn't want to talk IPv6 at all. (This could
3461 * be the case if somebody wants to speak only IPX, for
3462 * example.) Don't open IPv6CP in this case.
3464 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) {
3465 /* XXX this message should go away */
3467 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n",
3472 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3473 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3474 sppp_open_event(&ipv6cp, sp);
3478 sppp_ipv6cp_close(struct sppp *sp)
3480 sppp_close_event(&ipv6cp, sp);
3484 sppp_ipv6cp_TO(void *cookie)
3486 sppp_to_event(&ipv6cp, (struct sppp *)cookie);
3490 * Analyze a configure request. Return true if it was agreeable, and
3491 * caused action sca, false if it has been rejected or nak'ed, and
3492 * caused action scn. (The return value is used to make the state
3493 * transition decision in the state automaton.)
3496 sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3498 u_char *buf, *r, *p;
3499 struct ifnet *ifp = SP2IFP(sp);
3500 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
3501 struct in6_addr myaddr, desiredaddr, suggestaddr;
3504 int collision, nohisaddr;
3505 char ip6buf[INET6_ADDRSTRLEN];
3510 * Make sure to allocate a buf that can at least hold a
3511 * conf-nak with an `address' option. We might need it below.
3513 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
3517 /* pass 1: see if we can recognize them */
3519 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:",
3523 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3524 len-=p[1], p+=p[1]) {
3526 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3528 case IPV6CP_OPT_IFID:
3529 if (len >= 10 && p[1] == 10 && ifidcount == 0) {
3530 /* correctly formed address option */
3535 log(-1, " [invalid]");
3538 case IPV6CP_OPT_COMPRESSION:
3539 if (len >= 4 && p[1] >= 4) {
3540 /* correctly formed compress option */
3544 log(-1, " [invalid]");
3548 /* Others not supported. */
3553 /* Add the option to rejected list. */
3560 log(-1, " send conf-rej\n");
3561 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf);
3566 /* pass 2: parse option values */
3567 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
3569 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ",
3574 for (rlen=0; len >= 2 && p[1] >= 2 && len >= p[1];
3575 len-=p[1], p+=p[1]) {
3577 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3580 case IPV6CP_OPT_COMPRESSION:
3583 case IPV6CP_OPT_IFID:
3584 bzero(&desiredaddr, sizeof(desiredaddr));
3585 bcopy(&p[2], &desiredaddr.s6_addr[8], 8);
3586 collision = (bcmp(&desiredaddr.s6_addr[8],
3587 &myaddr.s6_addr[8], 8) == 0);
3588 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr);
3590 desiredaddr.s6_addr16[0] = htons(0xfe80);
3591 (void)in6_setscope(&desiredaddr, SP2IFP(sp), NULL);
3593 if (!collision && !nohisaddr) {
3594 /* no collision, hisaddr known - Conf-Ack */
3599 ip6_sprintf(ip6buf, &desiredaddr),
3600 sppp_cp_type_name(type));
3605 bzero(&suggestaddr, sizeof(&suggestaddr));
3606 if (collision && nohisaddr) {
3607 /* collision, hisaddr unknown - Conf-Rej */
3612 * - no collision, hisaddr unknown, or
3613 * - collision, hisaddr known
3614 * Conf-Nak, suggest hisaddr
3617 sppp_suggest_ip6_addr(sp, &suggestaddr);
3618 bcopy(&suggestaddr.s6_addr[8], &p[2], 8);
3622 ip6_sprintf(ip6buf, &desiredaddr),
3623 sppp_cp_type_name(type));
3626 /* Add the option to nak'ed list. */
3632 if (rlen == 0 && type == CONF_ACK) {
3634 log(-1, " send %s\n", sppp_cp_type_name(type));
3635 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1);
3638 if (type == CONF_ACK)
3639 panic("IPv6CP RCR: CONF_ACK with non-zero rlen");
3643 log(-1, " send %s suggest %s\n",
3644 sppp_cp_type_name(type),
3645 ip6_sprintf(ip6buf, &suggestaddr));
3647 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf);
3656 * Analyze the IPv6CP Configure-Reject option list, and adjust our
3660 sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3663 struct ifnet *ifp = SP2IFP(sp);
3664 int debug = ifp->if_flags & IFF_DEBUG;
3667 buf = malloc (len, M_TEMP, M_NOWAIT);
3672 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:",
3676 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3677 len -= p[1], p += p[1]) {
3679 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3681 case IPV6CP_OPT_IFID:
3683 * Peer doesn't grok address option. This is
3684 * bad. XXX Should we better give up here?
3686 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID);
3689 case IPV6CP_OPT_COMPRESS:
3690 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS);
3702 * Analyze the IPv6CP Configure-NAK option list, and adjust our
3706 sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3709 struct ifnet *ifp = SP2IFP(sp);
3710 int debug = ifp->if_flags & IFF_DEBUG;
3711 struct in6_addr suggestaddr;
3712 char ip6buf[INET6_ADDRSTRLEN];
3715 buf = malloc (len, M_TEMP, M_NOWAIT);
3720 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:",
3724 for (; len >= 2 && p[1] >= 2 && len >= p[1];
3725 len -= p[1], p += p[1]) {
3727 log(-1, " %s", sppp_ipv6cp_opt_name(*p));
3729 case IPV6CP_OPT_IFID:
3731 * Peer doesn't like our local ifid. See
3732 * if we can do something for him. We'll drop
3733 * him our address then.
3735 if (len < 10 || p[1] != 10)
3737 bzero(&suggestaddr, sizeof(suggestaddr));
3738 suggestaddr.s6_addr16[0] = htons(0xfe80);
3739 (void)in6_setscope(&suggestaddr, SP2IFP(sp), NULL);
3740 bcopy(&p[2], &suggestaddr.s6_addr[8], 8);
3742 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID);
3744 log(-1, " [suggestaddr %s]",
3745 ip6_sprintf(ip6buf, &suggestaddr));
3746 #ifdef IPV6CP_MYIFID_DYN
3748 * When doing dynamic address assignment,
3749 * we accept his offer.
3751 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) {
3752 struct in6_addr lastsuggest;
3754 * If <suggested myaddr from peer> equals to
3755 * <hisaddr we have suggested last time>,
3756 * we have a collision. generate new random
3759 sppp_suggest_ip6_addr(&lastsuggest);
3760 if (IN6_ARE_ADDR_EQUAL(&suggestaddr,
3763 log(-1, " [random]");
3764 sppp_gen_ip6_addr(sp, &suggestaddr);
3766 sppp_set_ip6_addr(sp, &suggestaddr, 0);
3768 log(-1, " [agree]");
3769 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN;
3773 * Since we do not do dynamic address assignment,
3774 * we ignore it and thus continue to negotiate
3775 * our already existing value. This can possibly
3776 * go into infinite request-reject loop.
3778 * This is not likely because we normally use
3779 * ifid based on MAC-address.
3780 * If you have no ethernet card on the node, too bad.
3781 * XXX should we use fail_counter?
3786 case IPV6CP_OPT_COMPRESS:
3788 * Peer wants different compression parameters.
3800 sppp_ipv6cp_tlu(struct sppp *sp)
3802 /* we are up - notify isdn daemon */
3808 sppp_ipv6cp_tld(struct sppp *sp)
3813 sppp_ipv6cp_tls(struct sppp *sp)
3815 /* indicate to LCP that it must stay alive */
3816 sp->lcp.protos |= (1 << IDX_IPV6CP);
3820 sppp_ipv6cp_tlf(struct sppp *sp)
3823 #if 0 /* need #if 0 to close IPv6CP properly */
3824 /* we no longer need LCP */
3825 sp->lcp.protos &= ~(1 << IDX_IPV6CP);
3826 sppp_lcp_check_and_close(sp);
3831 sppp_ipv6cp_scr(struct sppp *sp)
3833 char opt[10 /* ifid */ + 4 /* compression, minimum */];
3834 struct in6_addr ouraddr;
3837 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) {
3838 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0);
3839 opt[i++] = IPV6CP_OPT_IFID;
3841 bcopy(&ouraddr.s6_addr[8], &opt[i], 8);
3846 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) {
3847 opt[i++] = IPV6CP_OPT_COMPRESSION;
3849 opt[i++] = 0; /* TBD */
3850 opt[i++] = 0; /* TBD */
3851 /* variable length data may follow */
3855 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP];
3856 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt);
3859 static void sppp_ipv6cp_init(struct sppp *sp)
3863 static void sppp_ipv6cp_up(struct sppp *sp)
3867 static void sppp_ipv6cp_down(struct sppp *sp)
3872 static void sppp_ipv6cp_open(struct sppp *sp)
3876 static void sppp_ipv6cp_close(struct sppp *sp)
3880 static void sppp_ipv6cp_TO(void *sp)
3884 static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len)
3889 static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
3893 static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
3897 static void sppp_ipv6cp_tlu(struct sppp *sp)
3901 static void sppp_ipv6cp_tld(struct sppp *sp)
3905 static void sppp_ipv6cp_tls(struct sppp *sp)
3909 static void sppp_ipv6cp_tlf(struct sppp *sp)
3913 static void sppp_ipv6cp_scr(struct sppp *sp)
3919 *--------------------------------------------------------------------------*
3921 * The CHAP implementation. *
3923 *--------------------------------------------------------------------------*
3927 * The authentication protocols don't employ a full-fledged state machine as
3928 * the control protocols do, since they do have Open and Close events, but
3929 * not Up and Down, nor are they explicitly terminated. Also, use of the
3930 * authentication protocols may be different in both directions (this makes
3931 * sense, think of a machine that never accepts incoming calls but only
3932 * calls out, it doesn't require the called party to authenticate itself).
3934 * Our state machine for the local authentication protocol (we are requesting
3935 * the peer to authenticate) looks like:
3938 * +--------------------------------------------+
3940 * +--------+ Close +---------+ RCA+
3941 * | |<----------------------------------| |------+
3942 * +--->| Closed | TO* | Opened | sca |
3943 * | | |-----+ +-------| |<-----+
3944 * | +--------+ irc | | +---------+
3950 * | | +------->+ | |
3952 * | +--------+ V | |
3953 * | | |<----+<--------------------+ |
3959 * +------+ +------------------------------------------+
3960 * scn,tld sca,irc,ict,tlu
3965 * Open: LCP reached authentication phase
3966 * Close: LCP reached terminate phase
3968 * RCA+: received reply (pap-req, chap-response), acceptable
3969 * RCN: received reply (pap-req, chap-response), not acceptable
3970 * TO+: timeout with restart counter >= 0
3971 * TO-: timeout with restart counter < 0
3972 * TO*: reschedule timeout for CHAP
3974 * scr: send request packet (none for PAP, chap-challenge)
3975 * sca: send ack packet (pap-ack, chap-success)
3976 * scn: send nak packet (pap-nak, chap-failure)
3977 * ict: initialize re-challenge timer (CHAP only)
3979 * tlu: this-layer-up, LCP reaches network phase
3980 * tld: this-layer-down, LCP enters terminate phase
3982 * Note that in CHAP mode, after sending a new challenge, while the state
3983 * automaton falls back into Req-Sent state, it doesn't signal a tld
3984 * event to LCP, so LCP remains in network phase. Only after not getting
3985 * any response (or after getting an unacceptable response), CHAP closes,
3986 * causing LCP to enter terminate phase.
3988 * With PAP, there is no initial request that can be sent. The peer is
3989 * expected to send one based on the successful negotiation of PAP as
3990 * the authentication protocol during the LCP option negotiation.
3992 * Incoming authentication protocol requests (remote requests
3993 * authentication, we are peer) don't employ a state machine at all,
3994 * they are simply answered. Some peers [Ascend P50 firmware rev
3995 * 4.50] react allergically when sending IPCP requests while they are
3996 * still in authentication phase (thereby violating the standard that
3997 * demands that these NCP packets are to be discarded), so we keep
3998 * track of the peer demanding us to authenticate, and only proceed to
3999 * phase network once we've seen a positive acknowledge for the
4004 * Handle incoming CHAP packets.
4007 sppp_chap_input(struct sppp *sp, struct mbuf *m)
4010 struct lcp_header *h;
4012 u_char *value, *name, digest[AUTHKEYLEN], dsize;
4013 int value_len, name_len;
4016 len = m->m_pkthdr.len;
4020 SPP_FMT "chap invalid packet length: %d bytes\n",
4021 SPP_ARGS(ifp), len);
4024 h = mtod (m, struct lcp_header*);
4025 if (len > ntohs (h->len))
4026 len = ntohs (h->len);
4029 /* challenge, failure and success are his authproto */
4030 case CHAP_CHALLENGE:
4031 value = 1 + (u_char*)(h+1);
4032 value_len = value[-1];
4033 name = value + value_len;
4034 name_len = len - value_len - 5;
4038 SPP_FMT "chap corrupted challenge "
4039 "<%s id=0x%x len=%d",
4041 sppp_auth_type_name(PPP_CHAP, h->type),
4042 h->ident, ntohs(h->len));
4043 sppp_print_bytes((u_char*) (h+1), len-4);
4051 SPP_FMT "chap input <%s id=0x%x len=%d name=",
4053 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
4055 sppp_print_string((char*) name, name_len);
4056 log(-1, " value-size=%d value=", value_len);
4057 sppp_print_bytes(value, value_len);
4061 /* Compute reply value. */
4063 MD5Update(&ctx, &h->ident, 1);
4064 MD5Update(&ctx, sp->myauth.secret,
4065 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
4066 MD5Update(&ctx, value, value_len);
4067 MD5Final(digest, &ctx);
4068 dsize = sizeof digest;
4070 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
4071 sizeof dsize, (const char *)&dsize,
4072 sizeof digest, digest,
4073 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4080 log(LOG_DEBUG, SPP_FMT "chap success",
4084 sppp_print_string((char*)(h + 1), len - 4);
4090 sp->pp_flags &= ~PP_NEEDAUTH;
4091 if (sp->myauth.proto == PPP_CHAP &&
4092 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4093 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
4095 * We are authenticator for CHAP but didn't
4096 * complete yet. Leave it to tlu to proceed
4105 sppp_phase_network(sp);
4110 log(LOG_INFO, SPP_FMT "chap failure",
4114 sppp_print_string((char*)(h + 1), len - 4);
4118 log(LOG_INFO, SPP_FMT "chap failure\n",
4120 /* await LCP shutdown by authenticator */
4123 /* response is my authproto */
4125 value = 1 + (u_char*)(h+1);
4126 value_len = value[-1];
4127 name = value + value_len;
4128 name_len = len - value_len - 5;
4132 SPP_FMT "chap corrupted response "
4133 "<%s id=0x%x len=%d",
4135 sppp_auth_type_name(PPP_CHAP, h->type),
4136 h->ident, ntohs(h->len));
4137 sppp_print_bytes((u_char*)(h+1), len-4);
4142 if (h->ident != sp->confid[IDX_CHAP]) {
4145 SPP_FMT "chap dropping response for old ID "
4146 "(got %d, expected %d)\n",
4148 h->ident, sp->confid[IDX_CHAP]);
4151 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
4152 || bcmp(name, sp->hisauth.name, name_len) != 0) {
4153 log(LOG_INFO, SPP_FMT "chap response, his name ",
4155 sppp_print_string(name, name_len);
4156 log(-1, " != expected ");
4157 sppp_print_string(sp->hisauth.name,
4158 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
4162 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
4163 "<%s id=0x%x len=%d name=",
4165 sppp_state_name(sp->state[IDX_CHAP]),
4166 sppp_auth_type_name(PPP_CHAP, h->type),
4167 h->ident, ntohs (h->len));
4168 sppp_print_string((char*)name, name_len);
4169 log(-1, " value-size=%d value=", value_len);
4170 sppp_print_bytes(value, value_len);
4173 if (value_len != AUTHKEYLEN) {
4176 SPP_FMT "chap bad hash value length: "
4177 "%d bytes, should be %d\n",
4178 SPP_ARGS(ifp), value_len,
4184 MD5Update(&ctx, &h->ident, 1);
4185 MD5Update(&ctx, sp->hisauth.secret,
4186 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
4187 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
4188 MD5Final(digest, &ctx);
4190 #define FAILMSG "Failed..."
4191 #define SUCCMSG "Welcome!"
4193 if (value_len != sizeof digest ||
4194 bcmp(digest, value, value_len) != 0) {
4195 /* action scn, tld */
4196 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
4197 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4202 /* action sca, perhaps tlu */
4203 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
4204 sp->state[IDX_CHAP] == STATE_OPENED)
4205 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
4206 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4208 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
4209 sppp_cp_change_state(&chap, sp, STATE_OPENED);
4215 /* Unknown CHAP packet type -- ignore. */
4217 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
4218 "<0x%x id=0x%xh len=%d",
4220 sppp_state_name(sp->state[IDX_CHAP]),
4221 h->type, h->ident, ntohs(h->len));
4222 sppp_print_bytes((u_char*)(h+1), len-4);
4231 sppp_chap_init(struct sppp *sp)
4233 /* Chap doesn't have STATE_INITIAL at all. */
4234 sp->state[IDX_CHAP] = STATE_CLOSED;
4235 sp->fail_counter[IDX_CHAP] = 0;
4236 sp->pp_seq[IDX_CHAP] = 0;
4237 sp->pp_rseq[IDX_CHAP] = 0;
4238 callout_init(&sp->ch[IDX_CHAP],
4239 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
4243 sppp_chap_open(struct sppp *sp)
4245 if (sp->myauth.proto == PPP_CHAP &&
4246 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4247 /* we are authenticator for CHAP, start it */
4249 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4250 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4252 /* nothing to be done if we are peer, await a challenge */
4256 sppp_chap_close(struct sppp *sp)
4258 if (sp->state[IDX_CHAP] != STATE_CLOSED)
4259 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4263 sppp_chap_TO(void *cookie)
4265 struct sppp *sp = (struct sppp *)cookie;
4272 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
4274 sppp_state_name(sp->state[IDX_CHAP]),
4275 sp->rst_counter[IDX_CHAP]);
4277 if (--sp->rst_counter[IDX_CHAP] < 0)
4279 switch (sp->state[IDX_CHAP]) {
4280 case STATE_REQ_SENT:
4282 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
4286 /* TO+ (or TO*) event */
4287 switch (sp->state[IDX_CHAP]) {
4290 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4292 case STATE_REQ_SENT:
4294 /* sppp_cp_change_state() will restart the timer */
4295 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
4304 sppp_chap_tlu(struct sppp *sp)
4310 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
4313 * Some broken CHAP implementations (Conware CoNet, firmware
4314 * 4.0.?) don't want to re-authenticate their CHAP once the
4315 * initial challenge-response exchange has taken place.
4316 * Provide for an option to avoid rechallenges.
4318 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
4320 * Compute the re-challenge timeout. This will yield
4321 * a number between 300 and 810 seconds.
4323 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
4324 callout_reset(&sp->ch[IDX_CHAP], i * hz, chap.TO, (void *)sp);
4329 SPP_FMT "chap %s, ",
4331 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
4332 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
4333 log(-1, "next re-challenge in %d seconds\n", i);
4335 log(-1, "re-challenging supressed\n");
4340 /* indicate to LCP that we need to be closed down */
4341 sp->lcp.protos |= (1 << IDX_CHAP);
4343 if (sp->pp_flags & PP_NEEDAUTH) {
4345 * Remote is authenticator, but his auth proto didn't
4346 * complete yet. Defer the transition to network
4357 * If we are already in phase network, we are done here. This
4358 * is the case if this is a dummy tlu event after a re-challenge.
4360 if (sp->pp_phase != PHASE_NETWORK)
4361 sppp_phase_network(sp);
4365 sppp_chap_tld(struct sppp *sp)
4370 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
4371 callout_stop(&sp->ch[IDX_CHAP]);
4372 sp->lcp.protos &= ~(1 << IDX_CHAP);
4378 sppp_chap_scr(struct sppp *sp)
4383 /* Compute random challenge. */
4384 ch = (u_long *)sp->myauth.challenge;
4385 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
4386 read_random(&seed, sizeof seed);
4391 seed = tv.tv_sec ^ tv.tv_usec;
4394 ch[0] = seed ^ random();
4395 ch[1] = seed ^ random();
4396 ch[2] = seed ^ random();
4397 ch[3] = seed ^ random();
4400 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP];
4402 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
4403 sizeof clen, (const char *)&clen,
4404 (size_t)AUTHKEYLEN, sp->myauth.challenge,
4405 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
4411 *--------------------------------------------------------------------------*
4413 * The PAP implementation. *
4415 *--------------------------------------------------------------------------*
4418 * For PAP, we need to keep a little state also if we are the peer, not the
4419 * authenticator. This is since we don't get a request to authenticate, but
4420 * have to repeatedly authenticate ourself until we got a response (or the
4421 * retry counter is expired).
4425 * Handle incoming PAP packets. */
4427 sppp_pap_input(struct sppp *sp, struct mbuf *m)
4430 struct lcp_header *h;
4432 u_char *name, *passwd, mlen;
4433 int name_len, passwd_len;
4435 len = m->m_pkthdr.len;
4439 SPP_FMT "pap invalid packet length: %d bytes\n",
4440 SPP_ARGS(ifp), len);
4443 h = mtod (m, struct lcp_header*);
4444 if (len > ntohs (h->len))
4445 len = ntohs (h->len);
4447 /* PAP request is my authproto */
4449 name = 1 + (u_char*)(h+1);
4450 name_len = name[-1];
4451 passwd = name + name_len + 1;
4452 if (name_len > len - 6 ||
4453 (passwd_len = passwd[-1]) > len - 6 - name_len) {
4455 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4456 "<%s id=0x%x len=%d",
4458 sppp_auth_type_name(PPP_PAP, h->type),
4459 h->ident, ntohs(h->len));
4460 sppp_print_bytes((u_char*)(h+1), len-4);
4466 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
4467 "<%s id=0x%x len=%d name=",
4469 sppp_state_name(sp->state[IDX_PAP]),
4470 sppp_auth_type_name(PPP_PAP, h->type),
4471 h->ident, ntohs(h->len));
4472 sppp_print_string((char*)name, name_len);
4473 log(-1, " passwd=");
4474 sppp_print_string((char*)passwd, passwd_len);
4477 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) ||
4478 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) ||
4479 bcmp(name, sp->hisauth.name, name_len) != 0 ||
4480 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
4481 /* action scn, tld */
4482 mlen = sizeof(FAILMSG) - 1;
4483 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
4484 sizeof mlen, (const char *)&mlen,
4485 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
4490 /* action sca, perhaps tlu */
4491 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
4492 sp->state[IDX_PAP] == STATE_OPENED) {
4493 mlen = sizeof(SUCCMSG) - 1;
4494 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
4495 sizeof mlen, (const char *)&mlen,
4496 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
4499 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
4500 sppp_cp_change_state(&pap, sp, STATE_OPENED);
4505 /* ack and nak are his authproto */
4507 callout_stop(&sp->pap_my_to_ch);
4509 log(LOG_DEBUG, SPP_FMT "pap success",
4511 name_len = *((char *)h);
4512 if (len > 5 && name_len) {
4514 sppp_print_string((char*)(h+1), name_len);
4520 sp->pp_flags &= ~PP_NEEDAUTH;
4521 if (sp->myauth.proto == PPP_PAP &&
4522 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
4523 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
4525 * We are authenticator for PAP but didn't
4526 * complete yet. Leave it to tlu to proceed
4535 sppp_phase_network(sp);
4539 callout_stop (&sp->pap_my_to_ch);
4541 log(LOG_INFO, SPP_FMT "pap failure",
4543 name_len = *((char *)h);
4544 if (len > 5 && name_len) {
4546 sppp_print_string((char*)(h+1), name_len);
4550 log(LOG_INFO, SPP_FMT "pap failure\n",
4552 /* await LCP shutdown by authenticator */
4556 /* Unknown PAP packet type -- ignore. */
4558 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
4559 "<0x%x id=0x%x len=%d",
4561 h->type, h->ident, ntohs(h->len));
4562 sppp_print_bytes((u_char*)(h+1), len-4);
4571 sppp_pap_init(struct sppp *sp)
4573 /* PAP doesn't have STATE_INITIAL at all. */
4574 sp->state[IDX_PAP] = STATE_CLOSED;
4575 sp->fail_counter[IDX_PAP] = 0;
4576 sp->pp_seq[IDX_PAP] = 0;
4577 sp->pp_rseq[IDX_PAP] = 0;
4578 callout_init(&sp->ch[IDX_PAP],
4579 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
4580 callout_init(&sp->pap_my_to_ch,
4581 (SP2IFP(sp)->if_flags & IFF_NEEDSGIANT) ? 0 : CALLOUT_MPSAFE);
4585 sppp_pap_open(struct sppp *sp)
4587 if (sp->hisauth.proto == PPP_PAP &&
4588 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
4589 /* we are authenticator for PAP, start our timer */
4590 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4591 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4593 if (sp->myauth.proto == PPP_PAP) {
4594 /* we are peer, send a request, and start a timer */
4596 callout_reset(&sp->pap_my_to_ch, sp->lcp.timeout,
4597 sppp_pap_my_TO, (void *)sp);
4602 sppp_pap_close(struct sppp *sp)
4604 if (sp->state[IDX_PAP] != STATE_CLOSED)
4605 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4609 * That's the timeout routine if we are authenticator. Since the
4610 * authenticator is basically passive in PAP, we can't do much here.
4613 sppp_pap_TO(void *cookie)
4615 struct sppp *sp = (struct sppp *)cookie;
4622 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
4624 sppp_state_name(sp->state[IDX_PAP]),
4625 sp->rst_counter[IDX_PAP]);
4627 if (--sp->rst_counter[IDX_PAP] < 0)
4629 switch (sp->state[IDX_PAP]) {
4630 case STATE_REQ_SENT:
4632 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
4636 /* TO+ event, not very much we could do */
4637 switch (sp->state[IDX_PAP]) {
4638 case STATE_REQ_SENT:
4639 /* sppp_cp_change_state() will restart the timer */
4640 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
4649 * That's the timeout handler if we are peer. Since the peer is active,
4650 * we need to retransmit our PAP request since it is apparently lost.
4651 * XXX We should impose a max counter.
4654 sppp_pap_my_TO(void *cookie)
4656 struct sppp *sp = (struct sppp *)cookie;
4660 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
4669 sppp_pap_tlu(struct sppp *sp)
4674 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
4677 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
4678 SPP_ARGS(ifp), pap.name);
4682 /* indicate to LCP that we need to be closed down */
4683 sp->lcp.protos |= (1 << IDX_PAP);
4685 if (sp->pp_flags & PP_NEEDAUTH) {
4687 * Remote is authenticator, but his auth proto didn't
4688 * complete yet. Defer the transition to network
4697 sppp_phase_network(sp);
4701 sppp_pap_tld(struct sppp *sp)
4706 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
4707 callout_stop (&sp->ch[IDX_PAP]);
4708 callout_stop (&sp->pap_my_to_ch);
4709 sp->lcp.protos &= ~(1 << IDX_PAP);
4715 sppp_pap_scr(struct sppp *sp)
4717 u_char idlen, pwdlen;
4719 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP];
4720 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
4721 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
4723 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
4724 sizeof idlen, (const char *)&idlen,
4725 (size_t)idlen, sp->myauth.name,
4726 sizeof pwdlen, (const char *)&pwdlen,
4727 (size_t)pwdlen, sp->myauth.secret,
4732 * Random miscellaneous functions.
4736 * Send a PAP or CHAP proto packet.
4738 * Varadic function, each of the elements for the ellipsis is of type
4739 * ``size_t mlen, const u_char *msg''. Processing will stop iff
4741 * NOTE: never declare variadic functions with types subject to type
4742 * promotion (i.e. u_char). This is asking for big trouble depending
4743 * on the architecture you are on...
4747 sppp_auth_send(const struct cp *cp, struct sppp *sp,
4748 unsigned int type, unsigned int id,
4752 struct ppp_header *h;
4753 struct lcp_header *lh;
4761 MGETHDR (m, M_DONTWAIT, MT_DATA);
4764 m->m_pkthdr.rcvif = 0;
4766 h = mtod (m, struct ppp_header*);
4767 h->address = PPP_ALLSTATIONS; /* broadcast address */
4768 h->control = PPP_UI; /* Unnumbered Info */
4769 h->protocol = htons(cp->proto);
4771 lh = (struct lcp_header*)(h + 1);
4774 p = (u_char*) (lh+1);
4779 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
4780 msg = va_arg(ap, const char *);
4782 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
4788 bcopy(msg, p, mlen);
4793 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
4794 lh->len = htons (LCP_HEADER_LEN + len);
4797 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
4798 SPP_ARGS(ifp), cp->name,
4799 sppp_auth_type_name(cp->proto, lh->type),
4800 lh->ident, ntohs(lh->len));
4801 sppp_print_bytes((u_char*) (lh+1), len);
4804 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
4809 * Flush interface queue.
4812 sppp_qflush(struct ifqueue *ifq)
4827 * Send keepalive packets, every 10 seconds.
4830 sppp_keepalive(void *dummy)
4832 struct sppp *sp = (struct sppp*)dummy;
4833 struct ifnet *ifp = SP2IFP(sp);
4838 /* Keepalive mode disabled or channel down? */
4839 if (! (sp->pp_flags & PP_KEEPALIVE) ||
4840 ! (ifp->if_drv_flags & IFF_DRV_RUNNING))
4843 if (sp->pp_mode == PP_FR) {
4844 sppp_fr_keepalive (sp);
4848 /* No keepalive in PPP mode if LCP not opened yet. */
4849 if (sp->pp_mode != IFF_CISCO &&
4850 sp->pp_phase < PHASE_AUTHENTICATE)
4853 if (sp->pp_alivecnt == MAXALIVECNT) {
4854 /* No keepalive packets got. Stop the interface. */
4855 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
4857 sppp_qflush (&sp->pp_cpq);
4858 if (sp->pp_mode != IFF_CISCO) {
4860 /* Shut down the PPP link. */
4862 /* Initiate negotiation. XXX */
4866 if (sp->pp_alivecnt <= MAXALIVECNT)
4868 if (sp->pp_mode == IFF_CISCO)
4869 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ,
4870 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]);
4871 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
4872 long nmagic = htonl (sp->lcp.magic);
4873 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP];
4874 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
4875 sp->lcp.echoid, 4, &nmagic);
4880 callout_reset(&sp->keepalive_callout, hz * 10, sppp_keepalive,
4885 * Get both IP addresses.
4888 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
4890 struct ifnet *ifp = SP2IFP(sp);
4892 struct sockaddr_in *si, *sm;
4898 * Pick the first AF_INET address from the list,
4899 * aliases don't make any sense on a p2p link anyway.
4902 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
4903 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4904 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4905 for (ifa = TAILQ_FIRST(&ifp->if_addrlist);
4907 ifa = TAILQ_NEXT(ifa, ifa_list))
4909 for (ifa = ifp->if_addrlist;
4911 ifa = ifa->ifa_next)
4913 if (ifa->ifa_addr->sa_family == AF_INET) {
4914 si = (struct sockaddr_in *)ifa->ifa_addr;
4915 sm = (struct sockaddr_in *)ifa->ifa_netmask;
4920 if (si && si->sin_addr.s_addr) {
4921 ssrc = si->sin_addr.s_addr;
4923 *srcmask = ntohl(sm->sin_addr.s_addr);
4926 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
4927 if (si && si->sin_addr.s_addr)
4928 ddst = si->sin_addr.s_addr;
4931 if (dst) *dst = ntohl(ddst);
4932 if (src) *src = ntohl(ssrc);
4936 * Set my IP address. Must be called at splimp.
4939 sppp_set_ip_addr(struct sppp *sp, u_long src)
4943 struct sockaddr_in *si;
4944 struct in_ifaddr *ia;
4947 * Pick the first AF_INET address from the list,
4948 * aliases don't make any sense on a p2p link anyway.
4951 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
4952 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
4953 #elif defined(__NetBSD__) || defined (__OpenBSD__)
4954 for (ifa = TAILQ_FIRST(&ifp->if_addrlist);
4956 ifa = TAILQ_NEXT(ifa, ifa_list))
4958 for (ifa = ifp->if_addrlist;
4960 ifa = ifa->ifa_next)
4963 if (ifa->ifa_addr->sa_family == AF_INET)
4965 si = (struct sockaddr_in *)ifa->ifa_addr;
4974 #if defined(__NetBSD__) && __NetBSD_Version__ >= 103080000
4975 struct sockaddr_in new_sin = *si;
4977 new_sin.sin_addr.s_addr = htonl(src);
4978 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 1);
4981 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: in_ifinit "
4982 " failed, error=%d\n", SPP_ARGS(ifp), error);
4985 /* delete old route */
4986 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
4989 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
4990 SPP_ARGS(ifp), error);
4993 /* set new address */
4994 si->sin_addr.s_addr = htonl(src);
4996 LIST_REMOVE(ia, ia_hash);
4997 LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash);
5000 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
5003 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
5004 SPP_ARGS(ifp), error);
5012 * Get both IPv6 addresses.
5015 sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst,
5016 struct in6_addr *srcmask)
5018 struct ifnet *ifp = SP2IFP(sp);
5020 struct sockaddr_in6 *si, *sm;
5021 struct in6_addr ssrc, ddst;
5024 bzero(&ssrc, sizeof(ssrc));
5025 bzero(&ddst, sizeof(ddst));
5027 * Pick the first link-local AF_INET6 address from the list,
5028 * aliases don't make any sense on a p2p link anyway.
5031 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
5032 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
5033 #elif defined(__NetBSD__) || defined (__OpenBSD__)
5034 for (ifa = ifp->if_addrlist.tqh_first;
5036 ifa = ifa->ifa_list.tqe_next)
5038 for (ifa = ifp->if_addrlist;
5040 ifa = ifa->ifa_next)
5042 if (ifa->ifa_addr->sa_family == AF_INET6) {
5043 si = (struct sockaddr_in6 *)ifa->ifa_addr;
5044 sm = (struct sockaddr_in6 *)ifa->ifa_netmask;
5045 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr))
5049 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) {
5050 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc));
5052 bcopy(&sm->sin6_addr, srcmask,
5057 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr;
5058 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr))
5059 bcopy(&si->sin6_addr, &ddst, sizeof(ddst));
5063 bcopy(&ddst, dst, sizeof(*dst));
5065 bcopy(&ssrc, src, sizeof(*src));
5068 #ifdef IPV6CP_MYIFID_DYN
5070 * Generate random ifid.
5073 sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr)
5079 * Set my IPv6 address. Must be called at splimp.
5082 sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src)
5086 struct sockaddr_in6 *sin6;
5089 * Pick the first link-local AF_INET6 address from the list,
5090 * aliases don't make any sense on a p2p link anyway.
5094 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
5095 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
5096 #elif defined(__NetBSD__) || defined (__OpenBSD__)
5097 for (ifa = ifp->if_addrlist.tqh_first;
5099 ifa = ifa->ifa_list.tqe_next)
5101 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next)
5104 if (ifa->ifa_addr->sa_family == AF_INET6)
5106 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr;
5107 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
5115 struct sockaddr_in6 new_sin6 = *sin6;
5117 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr));
5118 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1);
5121 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit "
5122 " failed, error=%d\n", SPP_ARGS(ifp), error);
5129 * Suggest a candidate address to be used by peer.
5132 sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest)
5134 struct in6_addr myaddr;
5137 sppp_get_ip6_addrs(sp, &myaddr, 0, 0);
5139 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */
5141 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) {
5142 myaddr.s6_addr[14] ^= 0xff;
5143 myaddr.s6_addr[15] ^= 0xff;
5145 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff);
5146 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff);
5149 bcopy(&myaddr, suggest, sizeof(myaddr));
5154 sppp_params(struct sppp *sp, u_long cmd, void *data)
5157 struct ifreq *ifr = (struct ifreq *)data;
5158 struct spppreq *spr;
5161 if ((spr = malloc(sizeof(struct spppreq), M_TEMP, M_NOWAIT)) == 0)
5164 * ifr->ifr_data is supposed to point to a struct spppreq.
5165 * Check the cmd word first before attempting to fetch all the
5168 if ((subcmd = fuword(ifr->ifr_data)) == -1) {
5173 if (copyin((caddr_t)ifr->ifr_data, spr, sizeof(struct spppreq)) != 0) {
5179 case (u_long)SPPPIOGDEFS:
5180 if (cmd != SIOCGIFGENERIC) {
5185 * We copy over the entire current state, but clean
5186 * out some of the stuff we don't wanna pass up.
5187 * Remember, SIOCGIFGENERIC is unprotected, and can be
5188 * called by any user. No need to ever get PAP or
5189 * CHAP secrets back to userland anyway.
5191 spr->defs.pp_phase = sp->pp_phase;
5192 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0;
5193 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0;
5194 spr->defs.lcp = sp->lcp;
5195 spr->defs.ipcp = sp->ipcp;
5196 spr->defs.ipv6cp = sp->ipv6cp;
5197 spr->defs.myauth = sp->myauth;
5198 spr->defs.hisauth = sp->hisauth;
5199 bzero(spr->defs.myauth.secret, AUTHKEYLEN);
5200 bzero(spr->defs.myauth.challenge, AUTHKEYLEN);
5201 bzero(spr->defs.hisauth.secret, AUTHKEYLEN);
5202 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN);
5204 * Fixup the LCP timeout value to milliseconds so
5205 * spppcontrol doesn't need to bother about the value
5206 * of "hz". We do the reverse calculation below when
5209 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz;
5210 rv = copyout(spr, (caddr_t)ifr->ifr_data,
5211 sizeof(struct spppreq));
5214 case (u_long)SPPPIOSDEFS:
5215 if (cmd != SIOCSIFGENERIC) {
5220 * We have a very specific idea of which fields we
5221 * allow being passed back from userland, so to not
5222 * clobber our current state. For one, we only allow
5223 * setting anything if LCP is in dead or establish
5224 * phase. Once the authentication negotiations
5225 * started, the authentication settings must not be
5226 * changed again. (The administrator can force an
5227 * ifconfig down in order to get LCP back into dead
5230 * Also, we only allow for authentication parameters to be
5233 * XXX Should allow to set or clear pp_flags.
5235 * Finally, if the respective authentication protocol to
5236 * be used is set differently than 0, but the secret is
5237 * passed as all zeros, we don't trash the existing secret.
5238 * This allows an administrator to change the system name
5239 * only without clobbering the secret (which he didn't get
5240 * back in a previous SPPPIOGDEFS call). However, the
5241 * secrets are cleared if the authentication protocol is
5243 if (sp->pp_phase != PHASE_DEAD &&
5244 sp->pp_phase != PHASE_ESTABLISH) {
5249 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP &&
5250 spr->defs.myauth.proto != PPP_CHAP) ||
5251 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP &&
5252 spr->defs.hisauth.proto != PPP_CHAP)) {
5257 if (spr->defs.myauth.proto == 0)
5258 /* resetting myauth */
5259 bzero(&sp->myauth, sizeof sp->myauth);
5261 /* setting/changing myauth */
5262 sp->myauth.proto = spr->defs.myauth.proto;
5263 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
5264 if (spr->defs.myauth.secret[0] != '\0')
5265 bcopy(spr->defs.myauth.secret, sp->myauth.secret,
5268 if (spr->defs.hisauth.proto == 0)
5269 /* resetting hisauth */
5270 bzero(&sp->hisauth, sizeof sp->hisauth);
5272 /* setting/changing hisauth */
5273 sp->hisauth.proto = spr->defs.hisauth.proto;
5274 sp->hisauth.flags = spr->defs.hisauth.flags;
5275 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
5276 if (spr->defs.hisauth.secret[0] != '\0')
5277 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret,
5280 /* set LCP restart timer timeout */
5281 if (spr->defs.lcp.timeout != 0)
5282 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000;
5283 /* set VJ enable and IPv6 disable flags */
5285 if (spr->defs.enable_vj)
5286 sp->confflags |= CONF_ENABLE_VJ;
5288 sp->confflags &= ~CONF_ENABLE_VJ;
5291 if (spr->defs.enable_ipv6)
5292 sp->confflags |= CONF_ENABLE_IPV6;
5294 sp->confflags &= ~CONF_ENABLE_IPV6;
5309 sppp_phase_network(struct sppp *sp)
5315 sp->pp_phase = PHASE_NETWORK;
5318 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
5319 sppp_phase_name(sp->pp_phase));
5321 /* Notify NCPs now. */
5322 for (i = 0; i < IDX_COUNT; i++)
5323 if ((cps[i])->flags & CP_NCP)
5326 /* Send Up events to all NCPs. */
5327 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
5328 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP))
5331 /* if no NCP is starting, all this was in vain, close down */
5332 sppp_lcp_check_and_close(sp);
5337 sppp_cp_type_name(u_char type)
5339 static char buf[12];
5341 case CONF_REQ: return "conf-req";
5342 case CONF_ACK: return "conf-ack";
5343 case CONF_NAK: return "conf-nak";
5344 case CONF_REJ: return "conf-rej";
5345 case TERM_REQ: return "term-req";
5346 case TERM_ACK: return "term-ack";
5347 case CODE_REJ: return "code-rej";
5348 case PROTO_REJ: return "proto-rej";
5349 case ECHO_REQ: return "echo-req";
5350 case ECHO_REPLY: return "echo-reply";
5351 case DISC_REQ: return "discard-req";
5353 snprintf (buf, sizeof(buf), "cp/0x%x", type);
5358 sppp_auth_type_name(u_short proto, u_char type)
5360 static char buf[12];
5364 case CHAP_CHALLENGE: return "challenge";
5365 case CHAP_RESPONSE: return "response";
5366 case CHAP_SUCCESS: return "success";
5367 case CHAP_FAILURE: return "failure";
5371 case PAP_REQ: return "req";
5372 case PAP_ACK: return "ack";
5373 case PAP_NAK: return "nak";
5376 snprintf (buf, sizeof(buf), "auth/0x%x", type);
5381 sppp_lcp_opt_name(u_char opt)
5383 static char buf[12];
5385 case LCP_OPT_MRU: return "mru";
5386 case LCP_OPT_ASYNC_MAP: return "async-map";
5387 case LCP_OPT_AUTH_PROTO: return "auth-proto";
5388 case LCP_OPT_QUAL_PROTO: return "qual-proto";
5389 case LCP_OPT_MAGIC: return "magic";
5390 case LCP_OPT_PROTO_COMP: return "proto-comp";
5391 case LCP_OPT_ADDR_COMP: return "addr-comp";
5393 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
5398 sppp_ipcp_opt_name(u_char opt)
5400 static char buf[12];
5402 case IPCP_OPT_ADDRESSES: return "addresses";
5403 case IPCP_OPT_COMPRESSION: return "compression";
5404 case IPCP_OPT_ADDRESS: return "address";
5406 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
5412 sppp_ipv6cp_opt_name(u_char opt)
5414 static char buf[12];
5416 case IPV6CP_OPT_IFID: return "ifid";
5417 case IPV6CP_OPT_COMPRESSION: return "compression";
5419 sprintf (buf, "0x%x", opt);
5425 sppp_state_name(int state)
5428 case STATE_INITIAL: return "initial";
5429 case STATE_STARTING: return "starting";
5430 case STATE_CLOSED: return "closed";
5431 case STATE_STOPPED: return "stopped";
5432 case STATE_CLOSING: return "closing";
5433 case STATE_STOPPING: return "stopping";
5434 case STATE_REQ_SENT: return "req-sent";
5435 case STATE_ACK_RCVD: return "ack-rcvd";
5436 case STATE_ACK_SENT: return "ack-sent";
5437 case STATE_OPENED: return "opened";
5443 sppp_phase_name(enum ppp_phase phase)
5446 case PHASE_DEAD: return "dead";
5447 case PHASE_ESTABLISH: return "establish";
5448 case PHASE_TERMINATE: return "terminate";
5449 case PHASE_AUTHENTICATE: return "authenticate";
5450 case PHASE_NETWORK: return "network";
5456 sppp_proto_name(u_short proto)
5458 static char buf[12];
5460 case PPP_LCP: return "lcp";
5461 case PPP_IPCP: return "ipcp";
5462 case PPP_PAP: return "pap";
5463 case PPP_CHAP: return "chap";
5464 case PPP_IPV6CP: return "ipv6cp";
5466 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
5471 sppp_print_bytes(const u_char *p, u_short len)
5474 log(-1, " %*D", len, p, "-");
5478 sppp_print_string(const char *p, u_short len)
5485 * Print only ASCII chars directly. RFC 1994 recommends
5486 * using only them, but we don't rely on it. */
5487 if (c < ' ' || c > '~')
5488 log(-1, "\\x%x", c);
5495 sppp_dotted_quad(u_long addr)
5498 sprintf(s, "%d.%d.%d.%d",
5499 (int)((addr >> 24) & 0xff),
5500 (int)((addr >> 16) & 0xff),
5501 (int)((addr >> 8) & 0xff),
5502 (int)(addr & 0xff));
5507 sppp_strnlen(u_char *p, int max)
5511 for (len = 0; len < max && *p; ++p)
5516 /* a dummy, used to drop uninteresting events */
5518 sppp_null(struct sppp *unused)
5520 /* do just nothing */