2 * Synchronous PPP/Cisco link level subroutines.
3 * Keepalive protocol implemented in both Cisco and PPP modes.
5 * Copyright (C) 1994-1996 Cronyx Engineering Ltd.
6 * Author: Serge Vakulenko, <vak@cronyx.ru>
8 * Heavily revamped to conform to RFC 1661.
9 * Copyright (C) 1997, Joerg Wunsch.
11 * This software is distributed with NO WARRANTIES, not even the implied
12 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14 * Authors grant any other persons or organisations permission to use
15 * or modify this software as long as this message is kept with the software,
16 * all derivative works or modified versions.
18 * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997
23 #include <sys/param.h>
25 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
27 #include "opt_inet6.h"
33 # include "opt_inet.h"
34 # include "opt_inet6.h"
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/module.h>
42 #include <sys/sockio.h>
43 #include <sys/socket.h>
44 #include <sys/syslog.h>
45 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
46 #include <sys/random.h>
48 #include <sys/malloc.h>
51 #if defined (__OpenBSD__)
58 #include <net/netisr.h>
59 #include <net/if_types.h>
60 #include <net/route.h>
62 #if defined (__NetBSD__) || defined (__OpenBSD__)
63 #include <machine/cpu.h> /* XXX for softnet */
66 #include <machine/stdarg.h>
69 #include <netinet/in.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/in_var.h>
72 #include <netinet/ip.h>
73 #include <netinet/tcp.h>
74 # if defined (__FreeBSD__) || defined (__OpenBSD__)
75 # include <netinet/if_ether.h>
77 # include <net/ethertypes.h>
80 # error Huh? sppp without INET?
84 #include <netipx/ipx.h>
85 #include <netipx/ipx_if.h>
90 #include <netns/ns_if.h>
93 #include <net/if_sppp.h>
95 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
96 # define UNTIMEOUT(fun, arg, handle) untimeout(fun, arg, handle)
97 # define TIMEOUT(fun, arg1, arg2, handle) handle = timeout(fun, arg1, arg2)
98 # define IOCTL_CMD_T u_long
100 # define UNTIMEOUT(fun, arg, handle) untimeout(fun, arg)
101 # define TIMEOUT(fun, arg1, arg2, handle) timeout(fun, arg1, arg2)
102 # define IOCTL_CMD_T int
105 #define MAXALIVECNT 3 /* max. alive packets */
108 * Interface flags that can be set in an ifconfig command.
110 * Setting link0 will make the link passive, i.e. it will be marked
111 * as being administrative openable, but won't be opened to begin
112 * with. Incoming calls will be answered, or subsequent calls with
113 * -link1 will cause the administrative open of the LCP layer.
115 * Setting link1 will cause the link to auto-dial only as packets
118 * Setting IFF_DEBUG will syslog the option negotiation and state
119 * transitions at level kern.debug. Note: all logs consistently look
122 * <if-name><unit>: <proto-name> <additional info...>
124 * with <if-name><unit> being something like "bppp0", and <proto-name>
125 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
128 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
129 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
130 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
132 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
133 #define PPP_UI 0x03 /* Unnumbered Information */
134 #define PPP_IP 0x0021 /* Internet Protocol */
135 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
136 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
137 #define PPP_IPX 0x002b /* Novell IPX Protocol */
138 #define PPP_LCP 0xc021 /* Link Control Protocol */
139 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
140 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
141 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
143 #define CONF_REQ 1 /* PPP configure request */
144 #define CONF_ACK 2 /* PPP configure acknowledge */
145 #define CONF_NAK 3 /* PPP configure negative ack */
146 #define CONF_REJ 4 /* PPP configure reject */
147 #define TERM_REQ 5 /* PPP terminate request */
148 #define TERM_ACK 6 /* PPP terminate acknowledge */
149 #define CODE_REJ 7 /* PPP code reject */
150 #define PROTO_REJ 8 /* PPP protocol reject */
151 #define ECHO_REQ 9 /* PPP echo request */
152 #define ECHO_REPLY 10 /* PPP echo reply */
153 #define DISC_REQ 11 /* PPP discard request */
155 #define LCP_OPT_MRU 1 /* maximum receive unit */
156 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
157 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
158 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
159 #define LCP_OPT_MAGIC 5 /* magic number */
160 #define LCP_OPT_RESERVED 6 /* reserved */
161 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
162 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
164 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
165 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
166 #define IPCP_OPT_ADDRESS 3 /* local IP address */
168 #define PAP_REQ 1 /* PAP name/password request */
169 #define PAP_ACK 2 /* PAP acknowledge */
170 #define PAP_NAK 3 /* PAP fail */
172 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
173 #define CHAP_RESPONSE 2 /* CHAP challenge response */
174 #define CHAP_SUCCESS 3 /* CHAP response ok */
175 #define CHAP_FAILURE 4 /* CHAP response failed */
177 #define CHAP_MD5 5 /* hash algorithm - MD5 */
179 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
180 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
181 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
182 #define CISCO_ADDR_REQ 0 /* Cisco address request */
183 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
184 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
186 /* states are named and numbered according to RFC 1661 */
187 #define STATE_INITIAL 0
188 #define STATE_STARTING 1
189 #define STATE_CLOSED 2
190 #define STATE_STOPPED 3
191 #define STATE_CLOSING 4
192 #define STATE_STOPPING 5
193 #define STATE_REQ_SENT 6
194 #define STATE_ACK_RCVD 7
195 #define STATE_ACK_SENT 8
196 #define STATE_OPENED 9
203 #define PPP_HEADER_LEN sizeof (struct ppp_header)
210 #define LCP_HEADER_LEN sizeof (struct lcp_header)
212 struct cisco_packet {
220 #define CISCO_PACKET_LEN 18
223 * We follow the spelling and capitalization of RFC 1661 here, to make
224 * it easier comparing with the standard. Please refer to this RFC in
225 * case you can't make sense out of these abbreviation; it will also
226 * explain the semantics related to the various events and actions.
229 u_short proto; /* PPP control protocol number */
230 u_char protoidx; /* index into state table in struct sppp */
232 #define CP_LCP 0x01 /* this is the LCP */
233 #define CP_AUTH 0x02 /* this is an authentication protocol */
234 #define CP_NCP 0x04 /* this is a NCP */
235 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
236 const char *name; /* name of this control protocol */
238 void (*Up)(struct sppp *sp);
239 void (*Down)(struct sppp *sp);
240 void (*Open)(struct sppp *sp);
241 void (*Close)(struct sppp *sp);
242 void (*TO)(void *sp);
243 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
244 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
245 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
247 void (*tlu)(struct sppp *sp);
248 void (*tld)(struct sppp *sp);
249 void (*tls)(struct sppp *sp);
250 void (*tlf)(struct sppp *sp);
251 void (*scr)(struct sppp *sp);
254 static struct sppp *spppq;
255 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
256 static struct callout_handle keepalive_ch;
259 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
260 #define SPP_FMT "%s%d: "
261 #define SPP_ARGS(ifp) (ifp)->if_name, (ifp)->if_unit
263 #define SPP_FMT "%s: "
264 #define SPP_ARGS(ifp) (ifp)->if_xname
268 * The following disgusting hack gets around the problem that IP TOS
269 * can't be set yet. We want to put "interactive" traffic on a high
270 * priority queue. To decide if traffic is interactive, we check that
271 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
273 * XXX is this really still necessary? - joerg -
275 static u_short interactive_ports[8] = {
279 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
281 /* almost every function needs these */
283 struct ifnet *ifp = &sp->pp_if; \
284 int debug = ifp->if_flags & IFF_DEBUG
286 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
287 struct sockaddr *dst, struct rtentry *rt);
289 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
290 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
292 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
294 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
295 u_char ident, u_short len, void *data);
296 /* static void sppp_cp_timeout(void *arg); */
297 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
299 static void sppp_auth_send(const struct cp *cp,
300 struct sppp *sp, unsigned int type, unsigned int id,
303 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
304 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
305 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
306 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
307 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
309 static void sppp_null(struct sppp *sp);
311 static void sppp_lcp_init(struct sppp *sp);
312 static void sppp_lcp_up(struct sppp *sp);
313 static void sppp_lcp_down(struct sppp *sp);
314 static void sppp_lcp_open(struct sppp *sp);
315 static void sppp_lcp_close(struct sppp *sp);
316 static void sppp_lcp_TO(void *sp);
317 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
318 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
319 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
320 static void sppp_lcp_tlu(struct sppp *sp);
321 static void sppp_lcp_tld(struct sppp *sp);
322 static void sppp_lcp_tls(struct sppp *sp);
323 static void sppp_lcp_tlf(struct sppp *sp);
324 static void sppp_lcp_scr(struct sppp *sp);
325 static void sppp_lcp_check_and_close(struct sppp *sp);
326 static int sppp_ncp_check(struct sppp *sp);
328 static void sppp_ipcp_init(struct sppp *sp);
329 static void sppp_ipcp_up(struct sppp *sp);
330 static void sppp_ipcp_down(struct sppp *sp);
331 static void sppp_ipcp_open(struct sppp *sp);
332 static void sppp_ipcp_close(struct sppp *sp);
333 static void sppp_ipcp_TO(void *sp);
334 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
335 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
336 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
337 static void sppp_ipcp_tlu(struct sppp *sp);
338 static void sppp_ipcp_tld(struct sppp *sp);
339 static void sppp_ipcp_tls(struct sppp *sp);
340 static void sppp_ipcp_tlf(struct sppp *sp);
341 static void sppp_ipcp_scr(struct sppp *sp);
343 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
344 static void sppp_pap_init(struct sppp *sp);
345 static void sppp_pap_open(struct sppp *sp);
346 static void sppp_pap_close(struct sppp *sp);
347 static void sppp_pap_TO(void *sp);
348 static void sppp_pap_my_TO(void *sp);
349 static void sppp_pap_tlu(struct sppp *sp);
350 static void sppp_pap_tld(struct sppp *sp);
351 static void sppp_pap_scr(struct sppp *sp);
353 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
354 static void sppp_chap_init(struct sppp *sp);
355 static void sppp_chap_open(struct sppp *sp);
356 static void sppp_chap_close(struct sppp *sp);
357 static void sppp_chap_TO(void *sp);
358 static void sppp_chap_tlu(struct sppp *sp);
359 static void sppp_chap_tld(struct sppp *sp);
360 static void sppp_chap_scr(struct sppp *sp);
362 static const char *sppp_auth_type_name(u_short proto, u_char type);
363 static const char *sppp_cp_type_name(u_char type);
364 static const char *sppp_dotted_quad(u_long addr);
365 static const char *sppp_ipcp_opt_name(u_char opt);
366 static const char *sppp_lcp_opt_name(u_char opt);
367 static const char *sppp_phase_name(enum ppp_phase phase);
368 static const char *sppp_proto_name(u_short proto);
369 static const char *sppp_state_name(int state);
370 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
371 static int sppp_strnlen(u_char *p, int max);
372 static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst,
374 static void sppp_keepalive(void *dummy);
375 static void sppp_phase_network(struct sppp *sp);
376 static void sppp_print_bytes(const u_char *p, u_short len);
377 static void sppp_print_string(const char *p, u_short len);
378 static void sppp_qflush(struct ifqueue *ifq);
379 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
381 /* our control protocol descriptors */
382 static const struct cp lcp = {
383 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
384 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
385 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
386 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
390 static const struct cp ipcp = {
391 PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
392 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
393 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
394 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
398 static const struct cp pap = {
399 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
400 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
401 sppp_pap_TO, 0, 0, 0,
402 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
406 static const struct cp chap = {
407 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
408 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
409 sppp_chap_TO, 0, 0, 0,
410 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
414 static const struct cp *cps[IDX_COUNT] = {
416 &ipcp, /* IDX_IPCP */
418 &chap, /* IDX_CHAP */
422 sppp_modevent(module_t mod, int type, void *unused)
435 static moduledata_t spppmod = {
440 MODULE_VERSION(sppp, 1);
441 DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY);
444 * Exported functions, comprising our interface to the lower layer.
448 * Process the received packet.
451 sppp_input(struct ifnet *ifp, struct mbuf *m)
453 struct ppp_header *h;
454 struct ifqueue *inq = 0;
455 struct sppp *sp = (struct sppp *)ifp;
456 int debug = ifp->if_flags & IFF_DEBUG;
458 if (ifp->if_flags & IFF_UP)
459 /* Count received bytes, add FCS and one flag */
460 ifp->if_ibytes += m->m_pkthdr.len + 3;
462 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
463 /* Too small packet, drop it. */
466 SPP_FMT "input packet is too small, %d bytes\n",
467 SPP_ARGS(ifp), m->m_pkthdr.len);
475 /* Get PPP header. */
476 h = mtod (m, struct ppp_header*);
477 m_adj (m, PPP_HEADER_LEN);
479 switch (h->address) {
480 case PPP_ALLSTATIONS:
481 if (h->control != PPP_UI)
483 if (sp->pp_mode == IFF_CISCO) {
486 SPP_FMT "PPP packet in Cisco mode "
487 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
489 h->address, h->control, ntohs(h->protocol));
492 switch (ntohs (h->protocol)) {
496 SPP_FMT "rejecting protocol "
497 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
499 h->address, h->control, ntohs(h->protocol));
500 if (sp->state[IDX_LCP] == STATE_OPENED)
501 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
502 ++sp->pp_seq, m->m_pkthdr.len + 2,
507 sppp_cp_input(&lcp, sp, m);
511 if (sp->pp_phase >= PHASE_AUTHENTICATE)
512 sppp_pap_input(sp, m);
516 if (sp->pp_phase >= PHASE_AUTHENTICATE)
517 sppp_chap_input(sp, m);
522 if (sp->pp_phase == PHASE_NETWORK)
523 sppp_cp_input(&ipcp, sp, m);
527 if (sp->state[IDX_IPCP] == STATE_OPENED) {
528 schednetisr (NETISR_IP);
535 /* IPX IPXCP not implemented yet */
536 if (sp->pp_phase == PHASE_NETWORK) {
537 schednetisr (NETISR_IPX);
544 /* XNS IDPCP not implemented yet */
545 if (sp->pp_phase == PHASE_NETWORK) {
546 schednetisr (NETISR_NS);
553 case CISCO_MULTICAST:
555 /* Don't check the control field here (RFC 1547). */
556 if (sp->pp_mode != IFF_CISCO) {
559 SPP_FMT "Cisco packet in PPP mode "
560 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
562 h->address, h->control, ntohs(h->protocol));
565 switch (ntohs (h->protocol)) {
569 case CISCO_KEEPALIVE:
570 sppp_cisco_input ((struct sppp*) ifp, m);
575 schednetisr (NETISR_IP);
581 schednetisr (NETISR_IPV6);
587 schednetisr (NETISR_IPX);
593 schednetisr (NETISR_NS);
599 default: /* Invalid PPP packet. */
603 SPP_FMT "invalid input packet "
604 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
606 h->address, h->control, ntohs(h->protocol));
610 if (! (ifp->if_flags & IFF_UP) || ! inq)
614 if (! IF_HANDOFF(inq, m, NULL)) {
616 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
623 * Enqueue transmit packet.
626 sppp_output(struct ifnet *ifp, struct mbuf *m,
627 struct sockaddr *dst, struct rtentry *rt)
629 struct sppp *sp = (struct sppp*) ifp;
630 struct ppp_header *h;
633 int debug = ifp->if_flags & IFF_DEBUG;
637 if ((ifp->if_flags & IFF_UP) == 0 ||
638 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
644 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
646 * Interface is not yet running, but auto-dial. Need
647 * to start LCP for it.
649 ifp->if_flags |= IFF_RUNNING;
657 if (dst->sa_family == AF_INET) {
658 /* XXX Check mbuf length here? */
659 struct ip *ip = mtod (m, struct ip*);
660 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
663 * When using dynamic local IP address assignment by using
664 * 0.0.0.0 as a local address, the first TCP session will
665 * not connect because the local TCP checksum is computed
666 * using 0.0.0.0 which will later become our real IP address
667 * so the TCP checksum computed at the remote end will
668 * become invalid. So we
669 * - don't let packets with src ip addr 0 thru
670 * - we flag TCP packets with src ip 0 as an error
673 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
677 if(ip->ip_p == IPPROTO_TCP)
678 return(EADDRNOTAVAIL);
684 * Put low delay, telnet, rlogin and ftp control packets
685 * in front of the queue.
687 if (_IF_QFULL(&sp->pp_fastq))
689 else if (ip->ip_tos & IPTOS_LOWDELAY)
691 else if (m->m_len < sizeof *ip + sizeof *tcp)
693 else if (ip->ip_p != IPPROTO_TCP)
695 else if (INTERACTIVE (ntohs (tcp->th_sport)))
697 else if (INTERACTIVE (ntohs (tcp->th_dport)))
703 * Prepend general data packet PPP header. For now, IP only.
705 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
708 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
715 * May want to check size of packet
716 * (albeit due to the implementation it's always enough)
718 h = mtod (m, struct ppp_header*);
719 if (sp->pp_mode == IFF_CISCO) {
720 h->address = CISCO_UNICAST; /* unicast address */
723 h->address = PPP_ALLSTATIONS; /* broadcast address */
724 h->control = PPP_UI; /* Unnumbered Info */
727 switch (dst->sa_family) {
729 case AF_INET: /* Internet Protocol */
730 if (sp->pp_mode == IFF_CISCO)
731 h->protocol = htons (ETHERTYPE_IP);
734 * Don't choke with an ENETDOWN early. It's
735 * possible that we just started dialing out,
736 * so don't drop the packet immediately. If
737 * we notice that we run out of buffer space
738 * below, we will however remember that we are
739 * not ready to carry IP packets, and return
740 * ENETDOWN, as opposed to ENOBUFS.
742 h->protocol = htons(PPP_IP);
743 if (sp->state[IDX_IPCP] != STATE_OPENED)
749 case AF_INET6: /* Internet Protocol */
750 if (sp->pp_mode == IFF_CISCO)
751 h->protocol = htons (ETHERTYPE_IPV6);
758 case AF_NS: /* Xerox NS Protocol */
759 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
760 ETHERTYPE_NS : PPP_XNS);
764 case AF_IPX: /* Novell IPX Protocol */
765 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
766 ETHERTYPE_IPX : PPP_IPX);
774 return (EAFNOSUPPORT);
778 * Queue message on interface, and start output if interface
779 * not yet active. Also adjust output byte count.
780 * The packet length includes header, FCS and 1 flag,
781 * according to RFC 1333.
783 if (! IF_HANDOFF_ADJ(ifq, m, ifp, 3)) {
785 return (rv? rv: ENOBUFS);
791 sppp_attach(struct ifnet *ifp)
793 struct sppp *sp = (struct sppp*) ifp;
795 /* Initialize keepalive handler. */
797 TIMEOUT(sppp_keepalive, 0, hz * 10, keepalive_ch);
799 /* Insert new entry into the keepalive list. */
803 sp->pp_if.if_mtu = PP_MTU;
804 sp->pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
805 sp->pp_if.if_type = IFT_PPP;
806 sp->pp_if.if_output = sppp_output;
808 sp->pp_flags = PP_KEEPALIVE;
810 sp->pp_if.if_snd.ifq_maxlen = 32;
811 sp->pp_fastq.ifq_maxlen = 32;
812 sp->pp_cpq.ifq_maxlen = 20;
817 sp->pp_phase = PHASE_DEAD;
819 sp->pp_down = lcp.Down;
820 mtx_init(&sp->pp_cpq.ifq_mtx, "sppp_cpq", MTX_DEF);
821 mtx_init(&sp->pp_fastq.ifq_mtx, "sppp_fastq", MTX_DEF);
830 sppp_detach(struct ifnet *ifp)
832 struct sppp **q, *p, *sp = (struct sppp*) ifp;
835 /* Remove the entry from the keepalive list. */
836 for (q = &spppq; (p = *q); q = &p->pp_next)
842 /* Stop keepalive handler. */
844 UNTIMEOUT(sppp_keepalive, 0, keepalive_ch);
846 for (i = 0; i < IDX_COUNT; i++)
847 UNTIMEOUT((cps[i])->TO, (void *)sp, sp->ch[i]);
848 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
849 mtx_destroy(&sp->pp_cpq.ifq_mtx);
850 mtx_destroy(&sp->pp_fastq.ifq_mtx);
854 * Flush the interface output queue.
857 sppp_flush(struct ifnet *ifp)
859 struct sppp *sp = (struct sppp*) ifp;
861 sppp_qflush (&sp->pp_if.if_snd);
862 sppp_qflush (&sp->pp_fastq);
863 sppp_qflush (&sp->pp_cpq);
867 * Check if the output queue is empty.
870 sppp_isempty(struct ifnet *ifp)
872 struct sppp *sp = (struct sppp*) ifp;
876 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
877 !sp->pp_if.if_snd.ifq_head;
883 * Get next packet to send.
886 sppp_dequeue(struct ifnet *ifp)
888 struct sppp *sp = (struct sppp*) ifp;
894 * Process only the control protocol queue until we have at
895 * least one NCP open.
897 * Do always serve all three queues in Cisco mode.
899 IF_DEQUEUE(&sp->pp_cpq, m);
901 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO)) {
902 IF_DEQUEUE(&sp->pp_fastq, m);
904 IF_DEQUEUE (&sp->pp_if.if_snd, m);
911 * Pick the next packet, do not remove it from the queue.
914 sppp_pick(struct ifnet *ifp)
916 struct sppp *sp = (struct sppp*)ifp;
922 m = sp->pp_cpq.ifq_head;
924 (sp->pp_phase == PHASE_NETWORK || sp->pp_mode == IFF_CISCO))
925 if ((m = sp->pp_fastq.ifq_head) == NULL)
926 m = sp->pp_if.if_snd.ifq_head;
932 * Process an ioctl request. Called on low priority level.
935 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
937 struct ifreq *ifr = (struct ifreq*) data;
938 struct sppp *sp = (struct sppp*) ifp;
939 int s, rv, going_up, going_down, newmode;
950 /* fall through... */
953 going_up = ifp->if_flags & IFF_UP &&
954 (ifp->if_flags & IFF_RUNNING) == 0;
955 going_down = (ifp->if_flags & IFF_UP) == 0 &&
956 ifp->if_flags & IFF_RUNNING;
958 newmode = ifp->if_flags & IFF_PASSIVE;
960 newmode = ifp->if_flags & IFF_AUTO;
962 newmode = ifp->if_flags & IFF_CISCO;
963 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
964 ifp->if_flags |= newmode;
966 if (newmode != sp->pp_mode) {
969 going_up = ifp->if_flags & IFF_RUNNING;
973 if (sp->pp_mode != IFF_CISCO)
978 ifp->if_flags &= ~IFF_RUNNING;
979 sp->pp_mode = newmode;
983 if (sp->pp_mode != IFF_CISCO)
985 sp->pp_mode = newmode;
986 if (sp->pp_mode == 0) {
987 ifp->if_flags |= IFF_RUNNING;
990 if (sp->pp_mode == IFF_CISCO) {
993 ifp->if_flags |= IFF_RUNNING;
1001 #define ifr_mtu ifr_metric
1004 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1006 ifp->if_mtu = ifr->ifr_mtu;
1011 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1013 ifp->if_mtu = *(short*)data;
1018 ifr->ifr_mtu = ifp->if_mtu;
1023 *(short*)data = ifp->if_mtu;
1030 case SIOCGIFGENERIC:
1031 case SIOCSIFGENERIC:
1032 rv = sppp_params(sp, cmd, data);
1043 * Cisco framing implementation.
1047 * Handle incoming Cisco keepalive protocol packets.
1050 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1053 struct cisco_packet *h;
1056 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1059 SPP_FMT "cisco invalid packet length: %d bytes\n",
1060 SPP_ARGS(ifp), m->m_pkthdr.len);
1063 h = mtod (m, struct cisco_packet*);
1066 SPP_FMT "cisco input: %d bytes "
1067 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1068 SPP_ARGS(ifp), m->m_pkthdr.len,
1069 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1070 (u_int)h->time0, (u_int)h->time1);
1071 switch (ntohl (h->type)) {
1074 log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n",
1075 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1077 case CISCO_ADDR_REPLY:
1078 /* Reply on address request, ignore */
1080 case CISCO_KEEPALIVE_REQ:
1081 sp->pp_alivecnt = 0;
1082 sp->pp_rseq = ntohl (h->par1);
1083 if (sp->pp_seq == sp->pp_rseq) {
1084 /* Local and remote sequence numbers are equal.
1085 * Probably, the line is in loopback mode. */
1086 if (sp->pp_loopcnt >= MAXALIVECNT) {
1087 printf (SPP_FMT "loopback\n",
1090 if (ifp->if_flags & IFF_UP) {
1092 sppp_qflush (&sp->pp_cpq);
1097 /* Generate new local sequence number */
1098 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1099 sp->pp_seq = random();
1101 sp->pp_seq ^= time.tv_sec ^ time.tv_usec;
1106 if (! (ifp->if_flags & IFF_UP) &&
1107 (ifp->if_flags & IFF_RUNNING)) {
1109 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1112 case CISCO_ADDR_REQ:
1113 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1115 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1121 * Send Cisco keepalive packet.
1124 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1127 struct ppp_header *h;
1128 struct cisco_packet *ch;
1130 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1133 u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
1136 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1137 getmicrouptime(&tv);
1140 MGETHDR (m, M_DONTWAIT, MT_DATA);
1143 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1144 m->m_pkthdr.rcvif = 0;
1146 h = mtod (m, struct ppp_header*);
1147 h->address = CISCO_MULTICAST;
1149 h->protocol = htons (CISCO_KEEPALIVE);
1151 ch = (struct cisco_packet*) (h + 1);
1152 ch->type = htonl (type);
1153 ch->par1 = htonl (par1);
1154 ch->par2 = htonl (par2);
1157 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1158 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1159 ch->time1 = htons ((u_short) tv.tv_sec);
1161 ch->time0 = htons ((u_short) (t >> 16));
1162 ch->time1 = htons ((u_short) t);
1167 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1168 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1169 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1171 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1176 * PPP protocol implementation.
1180 * Send PPP control protocol packet.
1183 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1184 u_char ident, u_short len, void *data)
1187 struct ppp_header *h;
1188 struct lcp_header *lh;
1191 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1192 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1193 MGETHDR (m, M_DONTWAIT, MT_DATA);
1196 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1197 m->m_pkthdr.rcvif = 0;
1199 h = mtod (m, struct ppp_header*);
1200 h->address = PPP_ALLSTATIONS; /* broadcast address */
1201 h->control = PPP_UI; /* Unnumbered Info */
1202 h->protocol = htons (proto); /* Link Control Protocol */
1204 lh = (struct lcp_header*) (h + 1);
1207 lh->len = htons (LCP_HEADER_LEN + len);
1209 bcopy (data, lh+1, len);
1212 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1214 sppp_proto_name(proto),
1215 sppp_cp_type_name (lh->type), lh->ident,
1217 sppp_print_bytes ((u_char*) (lh+1), len);
1220 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
1225 * Handle incoming PPP control protocol packets.
1228 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1231 struct lcp_header *h;
1232 int len = m->m_pkthdr.len;
1239 SPP_FMT "%s invalid packet length: %d bytes\n",
1240 SPP_ARGS(ifp), cp->name, len);
1243 h = mtod (m, struct lcp_header*);
1246 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1247 SPP_ARGS(ifp), cp->name,
1248 sppp_state_name(sp->state[cp->protoidx]),
1249 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1250 sppp_print_bytes ((u_char*) (h+1), len-4);
1253 if (len > ntohs (h->len))
1254 len = ntohs (h->len);
1255 p = (u_char *)(h + 1);
1260 log(-1, SPP_FMT "%s invalid conf-req length %d\n",
1261 SPP_ARGS(ifp), cp->name,
1266 /* handle states where RCR doesn't get a SCA/SCN */
1267 switch (sp->state[cp->protoidx]) {
1269 case STATE_STOPPING:
1272 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1276 rv = (cp->RCR)(sp, h, len);
1277 switch (sp->state[cp->protoidx]) {
1281 /* fall through... */
1282 case STATE_ACK_SENT:
1283 case STATE_REQ_SENT:
1285 * sppp_cp_change_state() have the side effect of
1286 * restarting the timeouts. We want to avoid that
1287 * if the state don't change, otherwise we won't
1288 * ever timeout and resend a configuration request
1291 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT:
1294 sppp_cp_change_state(cp, sp, rv?
1295 STATE_ACK_SENT: STATE_REQ_SENT);
1298 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1300 sppp_cp_change_state(cp, sp, rv?
1301 STATE_ACK_SENT: STATE_REQ_SENT);
1303 case STATE_ACK_RCVD:
1305 sppp_cp_change_state(cp, sp, STATE_OPENED);
1307 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1312 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1315 printf(SPP_FMT "%s illegal %s in state %s\n",
1316 SPP_ARGS(ifp), cp->name,
1317 sppp_cp_type_name(h->type),
1318 sppp_state_name(sp->state[cp->protoidx]));
1323 if (h->ident != sp->confid[cp->protoidx]) {
1325 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1326 SPP_ARGS(ifp), cp->name,
1327 h->ident, sp->confid[cp->protoidx]);
1331 switch (sp->state[cp->protoidx]) {
1334 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1337 case STATE_STOPPING:
1339 case STATE_REQ_SENT:
1340 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1341 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1346 case STATE_ACK_RCVD:
1348 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1350 case STATE_ACK_SENT:
1351 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1352 sppp_cp_change_state(cp, sp, STATE_OPENED);
1354 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1355 SPP_ARGS(ifp), cp->name);
1359 printf(SPP_FMT "%s illegal %s in state %s\n",
1360 SPP_ARGS(ifp), cp->name,
1361 sppp_cp_type_name(h->type),
1362 sppp_state_name(sp->state[cp->protoidx]));
1368 if (h->ident != sp->confid[cp->protoidx]) {
1370 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1371 SPP_ARGS(ifp), cp->name,
1372 h->ident, sp->confid[cp->protoidx]);
1376 if (h->type == CONF_NAK)
1377 (cp->RCN_nak)(sp, h, len);
1379 (cp->RCN_rej)(sp, h, len);
1381 switch (sp->state[cp->protoidx]) {
1384 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1386 case STATE_REQ_SENT:
1387 case STATE_ACK_SENT:
1388 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1390 * Slow things down a bit if we think we might be
1391 * in loopback. Depend on the timeout to send the
1392 * next configuration request.
1401 case STATE_ACK_RCVD:
1402 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1406 case STATE_STOPPING:
1409 printf(SPP_FMT "%s illegal %s in state %s\n",
1410 SPP_ARGS(ifp), cp->name,
1411 sppp_cp_type_name(h->type),
1412 sppp_state_name(sp->state[cp->protoidx]));
1418 switch (sp->state[cp->protoidx]) {
1419 case STATE_ACK_RCVD:
1420 case STATE_ACK_SENT:
1421 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1426 case STATE_STOPPING:
1427 case STATE_REQ_SENT:
1429 /* Send Terminate-Ack packet. */
1431 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1432 SPP_ARGS(ifp), cp->name);
1433 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1437 sp->rst_counter[cp->protoidx] = 0;
1438 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1442 printf(SPP_FMT "%s illegal %s in state %s\n",
1443 SPP_ARGS(ifp), cp->name,
1444 sppp_cp_type_name(h->type),
1445 sppp_state_name(sp->state[cp->protoidx]));
1450 switch (sp->state[cp->protoidx]) {
1453 case STATE_REQ_SENT:
1454 case STATE_ACK_SENT:
1457 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1460 case STATE_STOPPING:
1461 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1464 case STATE_ACK_RCVD:
1465 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1470 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1473 printf(SPP_FMT "%s illegal %s in state %s\n",
1474 SPP_ARGS(ifp), cp->name,
1475 sppp_cp_type_name(h->type),
1476 sppp_state_name(sp->state[cp->protoidx]));
1482 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1484 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1485 "danger will robinson\n",
1486 SPP_ARGS(ifp), cp->name,
1487 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1488 switch (sp->state[cp->protoidx]) {
1491 case STATE_REQ_SENT:
1492 case STATE_ACK_SENT:
1494 case STATE_STOPPING:
1497 case STATE_ACK_RCVD:
1498 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1501 printf(SPP_FMT "%s illegal %s in state %s\n",
1502 SPP_ARGS(ifp), cp->name,
1503 sppp_cp_type_name(h->type),
1504 sppp_state_name(sp->state[cp->protoidx]));
1509 if (cp->proto != PPP_LCP)
1511 /* Discard the packet. */
1514 if (cp->proto != PPP_LCP)
1516 if (sp->state[cp->protoidx] != STATE_OPENED) {
1518 log(-1, SPP_FMT "lcp echo req but lcp closed\n",
1525 log(-1, SPP_FMT "invalid lcp echo request "
1526 "packet length: %d bytes\n",
1527 SPP_ARGS(ifp), len);
1530 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1531 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1532 /* Line loopback mode detected. */
1533 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1534 sp->pp_loopcnt = MAXALIVECNT * 5;
1536 sppp_qflush (&sp->pp_cpq);
1538 /* Shut down the PPP link. */
1544 *(long*)(h+1) = htonl (sp->lcp.magic);
1546 log(-1, SPP_FMT "got lcp echo req, sending echo rep\n",
1548 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1551 if (cp->proto != PPP_LCP)
1553 if (h->ident != sp->lcp.echoid) {
1559 log(-1, SPP_FMT "lcp invalid echo reply "
1560 "packet length: %d bytes\n",
1561 SPP_ARGS(ifp), len);
1565 log(-1, SPP_FMT "lcp got echo rep\n",
1567 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1568 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1569 sp->pp_alivecnt = 0;
1572 /* Unknown packet type -- send Code-Reject packet. */
1575 log(-1, SPP_FMT "%s send code-rej for 0x%x\n",
1576 SPP_ARGS(ifp), cp->name, h->type);
1577 sppp_cp_send(sp, cp->proto, CODE_REJ, ++sp->pp_seq,
1578 m->m_pkthdr.len, h);
1585 * The generic part of all Up/Down/Open/Close/TO event handlers.
1586 * Basically, the state transition handling in the automaton.
1589 sppp_up_event(const struct cp *cp, struct sppp *sp)
1594 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1595 SPP_ARGS(ifp), cp->name,
1596 sppp_state_name(sp->state[cp->protoidx]));
1598 switch (sp->state[cp->protoidx]) {
1600 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1602 case STATE_STARTING:
1603 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1605 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1608 printf(SPP_FMT "%s illegal up in state %s\n",
1609 SPP_ARGS(ifp), cp->name,
1610 sppp_state_name(sp->state[cp->protoidx]));
1615 sppp_down_event(const struct cp *cp, struct sppp *sp)
1620 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1621 SPP_ARGS(ifp), cp->name,
1622 sppp_state_name(sp->state[cp->protoidx]));
1624 switch (sp->state[cp->protoidx]) {
1627 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1630 sppp_cp_change_state(cp, sp, STATE_STARTING);
1633 case STATE_STOPPING:
1634 case STATE_REQ_SENT:
1635 case STATE_ACK_RCVD:
1636 case STATE_ACK_SENT:
1637 sppp_cp_change_state(cp, sp, STATE_STARTING);
1641 sppp_cp_change_state(cp, sp, STATE_STARTING);
1644 printf(SPP_FMT "%s illegal down in state %s\n",
1645 SPP_ARGS(ifp), cp->name,
1646 sppp_state_name(sp->state[cp->protoidx]));
1652 sppp_open_event(const struct cp *cp, struct sppp *sp)
1657 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1658 SPP_ARGS(ifp), cp->name,
1659 sppp_state_name(sp->state[cp->protoidx]));
1661 switch (sp->state[cp->protoidx]) {
1663 sppp_cp_change_state(cp, sp, STATE_STARTING);
1666 case STATE_STARTING:
1669 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1671 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1674 case STATE_STOPPING:
1675 case STATE_REQ_SENT:
1676 case STATE_ACK_RCVD:
1677 case STATE_ACK_SENT:
1681 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1688 sppp_close_event(const struct cp *cp, struct sppp *sp)
1693 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
1694 SPP_ARGS(ifp), cp->name,
1695 sppp_state_name(sp->state[cp->protoidx]));
1697 switch (sp->state[cp->protoidx]) {
1702 case STATE_STARTING:
1703 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1707 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1709 case STATE_STOPPING:
1710 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1715 case STATE_REQ_SENT:
1716 case STATE_ACK_RCVD:
1717 case STATE_ACK_SENT:
1718 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
1719 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq, 0, 0);
1720 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1726 sppp_to_event(const struct cp *cp, struct sppp *sp)
1733 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
1734 SPP_ARGS(ifp), cp->name,
1735 sppp_state_name(sp->state[cp->protoidx]),
1736 sp->rst_counter[cp->protoidx]);
1738 if (--sp->rst_counter[cp->protoidx] < 0)
1740 switch (sp->state[cp->protoidx]) {
1742 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1745 case STATE_STOPPING:
1746 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1749 case STATE_REQ_SENT:
1750 case STATE_ACK_RCVD:
1751 case STATE_ACK_SENT:
1752 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1758 switch (sp->state[cp->protoidx]) {
1760 case STATE_STOPPING:
1761 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq,
1763 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
1764 sp->ch[cp->protoidx]);
1766 case STATE_REQ_SENT:
1767 case STATE_ACK_RCVD:
1769 /* sppp_cp_change_state() will restart the timer */
1770 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1772 case STATE_ACK_SENT:
1774 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
1775 sp->ch[cp->protoidx]);
1783 * Change the state of a control protocol in the state automaton.
1784 * Takes care of starting/stopping the restart timer.
1787 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
1789 sp->state[cp->protoidx] = newstate;
1791 UNTIMEOUT(cp->TO, (void *)sp, sp->ch[cp->protoidx]);
1794 case STATE_STARTING:
1800 case STATE_STOPPING:
1801 case STATE_REQ_SENT:
1802 case STATE_ACK_RCVD:
1803 case STATE_ACK_SENT:
1804 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
1805 sp->ch[cp->protoidx]);
1811 *--------------------------------------------------------------------------*
1813 * The LCP implementation. *
1815 *--------------------------------------------------------------------------*
1818 sppp_lcp_init(struct sppp *sp)
1820 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
1822 sp->state[IDX_LCP] = STATE_INITIAL;
1823 sp->fail_counter[IDX_LCP] = 0;
1825 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
1827 /* Note that these values are relevant for all control protocols */
1828 sp->lcp.timeout = 3 * hz;
1829 sp->lcp.max_terminate = 2;
1830 sp->lcp.max_configure = 10;
1831 sp->lcp.max_failure = 10;
1832 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1833 callout_handle_init(&sp->ch[IDX_LCP]);
1838 sppp_lcp_up(struct sppp *sp)
1842 sp->pp_alivecnt = 0;
1843 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
1846 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
1848 * If this interface is passive or dial-on-demand, and we are
1849 * still in Initial state, it means we've got an incoming
1850 * call. Activate the interface.
1852 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
1855 SPP_FMT "Up event", SPP_ARGS(ifp));
1856 ifp->if_flags |= IFF_RUNNING;
1857 if (sp->state[IDX_LCP] == STATE_INITIAL) {
1859 log(-1, "(incoming call)\n");
1860 sp->pp_flags |= PP_CALLIN;
1866 sppp_up_event(&lcp, sp);
1870 sppp_lcp_down(struct sppp *sp)
1874 sppp_down_event(&lcp, sp);
1877 * If this is neither a dial-on-demand nor a passive
1878 * interface, simulate an ``ifconfig down'' action, so the
1879 * administrator can force a redial by another ``ifconfig
1880 * up''. XXX For leased line operation, should we immediately
1881 * try to reopen the connection here?
1883 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
1885 SPP_FMT "Down event, taking interface down.\n",
1891 SPP_FMT "Down event (carrier loss)\n",
1893 sp->pp_flags &= ~PP_CALLIN;
1894 if (sp->state[IDX_LCP] != STATE_INITIAL)
1896 ifp->if_flags &= ~IFF_RUNNING;
1901 sppp_lcp_open(struct sppp *sp)
1904 * If we are authenticator, negotiate LCP_AUTH
1906 if (sp->hisauth.proto != 0)
1907 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
1909 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
1910 sp->pp_flags &= ~PP_NEEDAUTH;
1911 sppp_open_event(&lcp, sp);
1915 sppp_lcp_close(struct sppp *sp)
1917 sppp_close_event(&lcp, sp);
1921 sppp_lcp_TO(void *cookie)
1923 sppp_to_event(&lcp, (struct sppp *)cookie);
1927 * Analyze a configure request. Return true if it was agreeable, and
1928 * caused action sca, false if it has been rejected or nak'ed, and
1929 * caused action scn. (The return value is used to make the state
1930 * transition decision in the state automaton.)
1933 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
1936 u_char *buf, *r, *p;
1943 buf = r = malloc (len, M_TEMP, M_NOWAIT);
1948 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
1951 /* pass 1: check for things that need to be rejected */
1953 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1955 log(-1, " %s ", sppp_lcp_opt_name(*p));
1959 if (len >= 6 && p[1] == 6)
1962 log(-1, "[invalid] ");
1964 case LCP_OPT_ASYNC_MAP:
1965 /* Async control character map. */
1966 if (len >= 6 && p[1] == 6)
1969 log(-1, "[invalid] ");
1972 /* Maximum receive unit. */
1973 if (len >= 4 && p[1] == 4)
1976 log(-1, "[invalid] ");
1978 case LCP_OPT_AUTH_PROTO:
1981 log(-1, "[invalid] ");
1984 authproto = (p[2] << 8) + p[3];
1985 if (authproto == PPP_CHAP && p[1] != 5) {
1987 log(-1, "[invalid chap len] ");
1990 if (sp->myauth.proto == 0) {
1991 /* we are not configured to do auth */
1993 log(-1, "[not configured] ");
1997 * Remote want us to authenticate, remember this,
1998 * so we stay in PHASE_AUTHENTICATE after LCP got
2001 sp->pp_flags |= PP_NEEDAUTH;
2004 /* Others not supported. */
2009 /* Add the option to rejected list. */
2016 log(-1, " send conf-rej\n");
2017 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2023 * pass 2: check for option values that are unacceptable and
2024 * thus require to be nak'ed.
2027 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2032 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2034 log(-1, " %s ", sppp_lcp_opt_name(*p));
2037 /* Magic number -- extract. */
2038 nmagic = (u_long)p[2] << 24 |
2039 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2040 if (nmagic != sp->lcp.magic) {
2043 log(-1, "0x%lx ", nmagic);
2046 if (debug && sp->pp_loopcnt < MAXALIVECNT*5)
2047 log(-1, "[glitch] ");
2050 * We negate our magic here, and NAK it. If
2051 * we see it later in an NAK packet, we
2052 * suggest a new one.
2054 nmagic = ~sp->lcp.magic;
2056 p[2] = nmagic >> 24;
2057 p[3] = nmagic >> 16;
2062 case LCP_OPT_ASYNC_MAP:
2063 /* Async control character map -- check to be zero. */
2064 if (! p[2] && ! p[3] && ! p[4] && ! p[5]) {
2066 log(-1, "[empty] ");
2070 log(-1, "[non-empty] ");
2071 /* suggest a zero one */
2072 p[2] = p[3] = p[4] = p[5] = 0;
2077 * Maximum receive unit. Always agreeable,
2078 * but ignored by now.
2080 sp->lcp.their_mru = p[2] * 256 + p[3];
2082 log(-1, "%lu ", sp->lcp.their_mru);
2085 case LCP_OPT_AUTH_PROTO:
2086 authproto = (p[2] << 8) + p[3];
2087 if (sp->myauth.proto != authproto) {
2088 /* not agreed, nak */
2090 log(-1, "[mine %s != his %s] ",
2091 sppp_proto_name(sp->hisauth.proto),
2092 sppp_proto_name(authproto));
2093 p[2] = sp->myauth.proto >> 8;
2094 p[3] = sp->myauth.proto;
2097 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2099 log(-1, "[chap not MD5] ");
2105 /* Add the option to nak'ed list. */
2112 * Local and remote magics equal -- loopback?
2114 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2115 if (sp->pp_loopcnt == MAXALIVECNT*5)
2116 printf (SPP_FMT "loopback\n",
2118 if (ifp->if_flags & IFF_UP) {
2120 sppp_qflush(&sp->pp_cpq);
2125 } else if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2127 log(-1, " max_failure (%d) exceeded, "
2129 sp->lcp.max_failure);
2130 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2133 log(-1, " send conf-nak\n");
2134 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2138 log(-1, " send conf-ack\n");
2139 sp->fail_counter[IDX_LCP] = 0;
2141 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2142 h->ident, origlen, h+1);
2150 * Analyze the LCP Configure-Reject option list, and adjust our
2154 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2160 buf = malloc (len, M_TEMP, M_NOWAIT);
2165 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2169 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2171 log(-1, " %s ", sppp_lcp_opt_name(*p));
2174 /* Magic number -- can't use it, use 0 */
2175 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2180 * Should not be rejected anyway, since we only
2181 * negotiate a MRU if explicitly requested by
2184 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2186 case LCP_OPT_AUTH_PROTO:
2188 * Peer doesn't want to authenticate himself,
2189 * deny unless this is a dialout call, and
2190 * AUTHFLAG_NOCALLOUT is set.
2192 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2193 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2195 log(-1, "[don't insist on auth "
2197 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2201 log(-1, "[access denied]\n");
2213 * Analyze the LCP Configure-NAK option list, and adjust our
2217 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2224 buf = malloc (len, M_TEMP, M_NOWAIT);
2229 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2233 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2235 log(-1, " %s ", sppp_lcp_opt_name(*p));
2238 /* Magic number -- renegotiate */
2239 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2240 len >= 6 && p[1] == 6) {
2241 magic = (u_long)p[2] << 24 |
2242 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2244 * If the remote magic is our negated one,
2245 * this looks like a loopback problem.
2246 * Suggest a new magic to make sure.
2248 if (magic == ~sp->lcp.magic) {
2250 log(-1, "magic glitch ");
2251 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2252 sp->lcp.magic = random();
2254 sp->lcp.magic = time.tv_sec + time.tv_usec;
2257 sp->lcp.magic = magic;
2259 log(-1, "%lu ", magic);
2265 * Peer wants to advise us to negotiate an MRU.
2266 * Agree on it if it's reasonable, or use
2267 * default otherwise.
2269 if (len >= 4 && p[1] == 4) {
2270 u_int mru = p[2] * 256 + p[3];
2272 log(-1, "%d ", mru);
2273 if (mru < PP_MTU || mru > PP_MAX_MRU)
2276 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2279 case LCP_OPT_AUTH_PROTO:
2281 * Peer doesn't like our authentication method,
2285 log(-1, "[access denied]\n");
2297 sppp_lcp_tlu(struct sppp *sp)
2304 if (! (ifp->if_flags & IFF_UP) &&
2305 (ifp->if_flags & IFF_RUNNING)) {
2306 /* Coming out of loopback mode. */
2308 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2311 for (i = 0; i < IDX_COUNT; i++)
2312 if ((cps[i])->flags & CP_QUAL)
2315 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2316 (sp->pp_flags & PP_NEEDAUTH) != 0)
2317 sp->pp_phase = PHASE_AUTHENTICATE;
2319 sp->pp_phase = PHASE_NETWORK;
2322 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2323 sppp_phase_name(sp->pp_phase));
2326 * Open all authentication protocols. This is even required
2327 * if we already proceeded to network phase, since it might be
2328 * that remote wants us to authenticate, so we might have to
2329 * send a PAP request. Undesired authentication protocols
2330 * don't do anything when they get an Open event.
2332 for (i = 0; i < IDX_COUNT; i++)
2333 if ((cps[i])->flags & CP_AUTH)
2336 if (sp->pp_phase == PHASE_NETWORK) {
2337 /* Notify all NCPs. */
2338 for (i = 0; i < IDX_COUNT; i++)
2339 if ((cps[i])->flags & CP_NCP)
2343 /* Send Up events to all started protos. */
2344 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2345 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0)
2348 /* notify low-level driver of state change */
2350 sp->pp_chg(sp, (int)sp->pp_phase);
2352 if (sp->pp_phase == PHASE_NETWORK)
2353 /* if no NCP is starting, close down */
2354 sppp_lcp_check_and_close(sp);
2358 sppp_lcp_tld(struct sppp *sp)
2364 sp->pp_phase = PHASE_TERMINATE;
2367 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2368 sppp_phase_name(sp->pp_phase));
2371 * Take upper layers down. We send the Down event first and
2372 * the Close second to prevent the upper layers from sending
2373 * ``a flurry of terminate-request packets'', as the RFC
2376 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2377 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0) {
2379 (cps[i])->Close(sp);
2384 sppp_lcp_tls(struct sppp *sp)
2388 sp->pp_phase = PHASE_ESTABLISH;
2391 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2392 sppp_phase_name(sp->pp_phase));
2394 /* Notify lower layer if desired. */
2402 sppp_lcp_tlf(struct sppp *sp)
2406 sp->pp_phase = PHASE_DEAD;
2408 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2409 sppp_phase_name(sp->pp_phase));
2411 /* Notify lower layer if desired. */
2419 sppp_lcp_scr(struct sppp *sp)
2421 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2425 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2426 if (! sp->lcp.magic)
2427 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2428 sp->lcp.magic = random();
2430 sp->lcp.magic = time.tv_sec + time.tv_usec;
2432 opt[i++] = LCP_OPT_MAGIC;
2434 opt[i++] = sp->lcp.magic >> 24;
2435 opt[i++] = sp->lcp.magic >> 16;
2436 opt[i++] = sp->lcp.magic >> 8;
2437 opt[i++] = sp->lcp.magic;
2440 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2441 opt[i++] = LCP_OPT_MRU;
2443 opt[i++] = sp->lcp.mru >> 8;
2444 opt[i++] = sp->lcp.mru;
2447 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2448 authproto = sp->hisauth.proto;
2449 opt[i++] = LCP_OPT_AUTH_PROTO;
2450 opt[i++] = authproto == PPP_CHAP? 5: 4;
2451 opt[i++] = authproto >> 8;
2452 opt[i++] = authproto;
2453 if (authproto == PPP_CHAP)
2454 opt[i++] = CHAP_MD5;
2457 sp->confid[IDX_LCP] = ++sp->pp_seq;
2458 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2462 * Check the open NCPs, return true if at least one NCP is open.
2465 sppp_ncp_check(struct sppp *sp)
2469 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2470 if (sp->lcp.protos & mask && (cps[i])->flags & CP_NCP)
2476 * Re-check the open NCPs and see if we should terminate the link.
2477 * Called by the NCPs during their tlf action handling.
2480 sppp_lcp_check_and_close(struct sppp *sp)
2483 if (sp->pp_phase < PHASE_NETWORK)
2484 /* don't bother, we are already going down */
2487 if (sppp_ncp_check(sp))
2494 *--------------------------------------------------------------------------*
2496 * The IPCP implementation. *
2498 *--------------------------------------------------------------------------*
2502 sppp_ipcp_init(struct sppp *sp)
2506 sp->state[IDX_IPCP] = STATE_INITIAL;
2507 sp->fail_counter[IDX_IPCP] = 0;
2508 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2509 callout_handle_init(&sp->ch[IDX_IPCP]);
2514 sppp_ipcp_up(struct sppp *sp)
2516 sppp_up_event(&ipcp, sp);
2520 sppp_ipcp_down(struct sppp *sp)
2522 sppp_down_event(&ipcp, sp);
2526 sppp_ipcp_open(struct sppp *sp)
2529 u_long myaddr, hisaddr;
2531 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN|IPCP_MYADDR_SEEN|IPCP_MYADDR_DYN);
2533 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2535 * If we don't have his address, this probably means our
2536 * interface doesn't want to talk IP at all. (This could
2537 * be the case if somebody wants to speak only IPX, for
2538 * example.) Don't open IPCP in this case.
2540 if (hisaddr == 0L) {
2541 /* XXX this message should go away */
2543 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2550 * I don't have an assigned address, so i need to
2551 * negotiate my address.
2553 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2554 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2556 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2557 sppp_open_event(&ipcp, sp);
2561 sppp_ipcp_close(struct sppp *sp)
2563 sppp_close_event(&ipcp, sp);
2564 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2566 * My address was dynamic, clear it again.
2568 sppp_set_ip_addr(sp, 0L);
2572 sppp_ipcp_TO(void *cookie)
2574 sppp_to_event(&ipcp, (struct sppp *)cookie);
2578 * Analyze a configure request. Return true if it was agreeable, and
2579 * caused action sca, false if it has been rejected or nak'ed, and
2580 * caused action scn. (The return value is used to make the state
2581 * transition decision in the state automaton.)
2584 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2586 u_char *buf, *r, *p;
2587 struct ifnet *ifp = &sp->pp_if;
2588 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2589 u_long hisaddr, desiredaddr;
2595 * Make sure to allocate a buf that can at least hold a
2596 * conf-nak with an `address' option. We might need it below.
2598 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2602 /* pass 1: see if we can recognize them */
2604 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2607 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2609 log(-1, " %s ", sppp_ipcp_opt_name(*p));
2611 case IPCP_OPT_ADDRESS:
2612 if (len >= 6 && p[1] == 6) {
2613 /* correctly formed address option */
2617 log(-1, "[invalid] ");
2620 /* Others not supported. */
2625 /* Add the option to rejected list. */
2632 log(-1, " send conf-rej\n");
2633 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2638 /* pass 2: parse option values */
2639 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
2641 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
2645 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2647 log(-1, " %s ", sppp_ipcp_opt_name(*p));
2649 case IPCP_OPT_ADDRESS:
2650 /* This is the address he wants in his end */
2651 desiredaddr = p[2] << 24 | p[3] << 16 |
2653 if (desiredaddr == hisaddr ||
2654 (hisaddr == 1 && desiredaddr != 0)) {
2656 * Peer's address is same as our value,
2657 * or we have set it to 0.0.0.1 to
2658 * indicate that we do not really care,
2659 * this is agreeable. Gonna conf-ack
2663 log(-1, "%s [ack] ",
2664 sppp_dotted_quad(hisaddr));
2665 /* record that we've seen it already */
2666 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
2670 * The address wasn't agreeable. This is either
2671 * he sent us 0.0.0.0, asking to assign him an
2672 * address, or he send us another address not
2673 * matching our value. Either case, we gonna
2674 * conf-nak it with our value.
2675 * XXX: we should "rej" if hisaddr == 0
2678 if (desiredaddr == 0)
2679 log(-1, "[addr requested] ");
2681 log(-1, "%s [not agreed] ",
2682 sppp_dotted_quad(desiredaddr));
2685 p[2] = hisaddr >> 24;
2686 p[3] = hisaddr >> 16;
2687 p[4] = hisaddr >> 8;
2691 /* Add the option to nak'ed list. */
2698 * If we are about to conf-ack the request, but haven't seen
2699 * his address so far, gonna conf-nak it instead, with the
2700 * `address' option present and our idea of his address being
2701 * filled in there, to request negotiation of both addresses.
2703 * XXX This can result in an endless req - nak loop if peer
2704 * doesn't want to send us his address. Q: What should we do
2705 * about it? XXX A: implement the max-failure counter.
2707 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
2708 buf[0] = IPCP_OPT_ADDRESS;
2710 buf[2] = hisaddr >> 24;
2711 buf[3] = hisaddr >> 16;
2712 buf[4] = hisaddr >> 8;
2716 log(-1, "still need hisaddr ");
2721 log(-1, " send conf-nak\n");
2722 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
2725 log(-1, " send conf-ack\n");
2726 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
2727 h->ident, origlen, h+1);
2735 * Analyze the IPCP Configure-Reject option list, and adjust our
2739 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2742 struct ifnet *ifp = &sp->pp_if;
2743 int debug = ifp->if_flags & IFF_DEBUG;
2746 buf = malloc (len, M_TEMP, M_NOWAIT);
2751 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
2755 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2757 log(-1, " %s ", sppp_ipcp_opt_name(*p));
2759 case IPCP_OPT_ADDRESS:
2761 * Peer doesn't grok address option. This is
2762 * bad. XXX Should we better give up here?
2763 * XXX We could try old "addresses" option...
2765 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
2776 * Analyze the IPCP Configure-NAK option list, and adjust our
2780 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2783 struct ifnet *ifp = &sp->pp_if;
2784 int debug = ifp->if_flags & IFF_DEBUG;
2788 buf = malloc (len, M_TEMP, M_NOWAIT);
2793 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
2797 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2799 log(-1, " %s ", sppp_ipcp_opt_name(*p));
2801 case IPCP_OPT_ADDRESS:
2803 * Peer doesn't like our local IP address. See
2804 * if we can do something for him. We'll drop
2805 * him our address then.
2807 if (len >= 6 && p[1] == 6) {
2808 wantaddr = p[2] << 24 | p[3] << 16 |
2810 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2812 log(-1, "[wantaddr %s] ",
2813 sppp_dotted_quad(wantaddr));
2815 * When doing dynamic address assignment,
2816 * we accept his offer. Otherwise, we
2817 * ignore it and thus continue to negotiate
2818 * our already existing value.
2819 * XXX: Bogus, if he said no once, he'll
2820 * just say no again, might as well die.
2822 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
2823 sppp_set_ip_addr(sp, wantaddr);
2825 log(-1, "[agree] ");
2826 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2839 sppp_ipcp_tlu(struct sppp *sp)
2841 /* we are up - notify isdn daemon */
2847 sppp_ipcp_tld(struct sppp *sp)
2852 sppp_ipcp_tls(struct sppp *sp)
2854 /* indicate to LCP that it must stay alive */
2855 sp->lcp.protos |= (1 << IDX_IPCP);
2859 sppp_ipcp_tlf(struct sppp *sp)
2861 /* we no longer need LCP */
2862 sp->lcp.protos &= ~(1 << IDX_IPCP);
2863 sppp_lcp_check_and_close(sp);
2867 sppp_ipcp_scr(struct sppp *sp)
2869 char opt[6 /* compression */ + 6 /* address */];
2873 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
2874 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
2875 opt[i++] = IPCP_OPT_ADDRESS;
2877 opt[i++] = ouraddr >> 24;
2878 opt[i++] = ouraddr >> 16;
2879 opt[i++] = ouraddr >> 8;
2883 sp->confid[IDX_IPCP] = ++sp->pp_seq;
2884 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
2888 *--------------------------------------------------------------------------*
2890 * The CHAP implementation. *
2892 *--------------------------------------------------------------------------*
2896 * The authentication protocols don't employ a full-fledged state machine as
2897 * the control protocols do, since they do have Open and Close events, but
2898 * not Up and Down, nor are they explicitly terminated. Also, use of the
2899 * authentication protocols may be different in both directions (this makes
2900 * sense, think of a machine that never accepts incoming calls but only
2901 * calls out, it doesn't require the called party to authenticate itself).
2903 * Our state machine for the local authentication protocol (we are requesting
2904 * the peer to authenticate) looks like:
2907 * +--------------------------------------------+
2909 * +--------+ Close +---------+ RCA+
2910 * | |<----------------------------------| |------+
2911 * +--->| Closed | TO* | Opened | sca |
2912 * | | |-----+ +-------| |<-----+
2913 * | +--------+ irc | | +---------+
2919 * | | +------->+ | |
2921 * | +--------+ V | |
2922 * | | |<----+<--------------------+ |
2928 * +------+ +------------------------------------------+
2929 * scn,tld sca,irc,ict,tlu
2934 * Open: LCP reached authentication phase
2935 * Close: LCP reached terminate phase
2937 * RCA+: received reply (pap-req, chap-response), acceptable
2938 * RCN: received reply (pap-req, chap-response), not acceptable
2939 * TO+: timeout with restart counter >= 0
2940 * TO-: timeout with restart counter < 0
2941 * TO*: reschedule timeout for CHAP
2943 * scr: send request packet (none for PAP, chap-challenge)
2944 * sca: send ack packet (pap-ack, chap-success)
2945 * scn: send nak packet (pap-nak, chap-failure)
2946 * ict: initialize re-challenge timer (CHAP only)
2948 * tlu: this-layer-up, LCP reaches network phase
2949 * tld: this-layer-down, LCP enters terminate phase
2951 * Note that in CHAP mode, after sending a new challenge, while the state
2952 * automaton falls back into Req-Sent state, it doesn't signal a tld
2953 * event to LCP, so LCP remains in network phase. Only after not getting
2954 * any response (or after getting an unacceptable response), CHAP closes,
2955 * causing LCP to enter terminate phase.
2957 * With PAP, there is no initial request that can be sent. The peer is
2958 * expected to send one based on the successful negotiation of PAP as
2959 * the authentication protocol during the LCP option negotiation.
2961 * Incoming authentication protocol requests (remote requests
2962 * authentication, we are peer) don't employ a state machine at all,
2963 * they are simply answered. Some peers [Ascend P50 firmware rev
2964 * 4.50] react allergically when sending IPCP requests while they are
2965 * still in authentication phase (thereby violating the standard that
2966 * demands that these NCP packets are to be discarded), so we keep
2967 * track of the peer demanding us to authenticate, and only proceed to
2968 * phase network once we've seen a positive acknowledge for the
2973 * Handle incoming CHAP packets.
2976 sppp_chap_input(struct sppp *sp, struct mbuf *m)
2979 struct lcp_header *h;
2981 u_char *value, *name, digest[AUTHKEYLEN], dsize;
2982 int value_len, name_len;
2985 len = m->m_pkthdr.len;
2989 SPP_FMT "chap invalid packet length: %d bytes\n",
2990 SPP_ARGS(ifp), len);
2993 h = mtod (m, struct lcp_header*);
2994 if (len > ntohs (h->len))
2995 len = ntohs (h->len);
2998 /* challenge, failure and success are his authproto */
2999 case CHAP_CHALLENGE:
3000 value = 1 + (u_char*)(h+1);
3001 value_len = value[-1];
3002 name = value + value_len;
3003 name_len = len - value_len - 5;
3007 SPP_FMT "chap corrupted challenge "
3008 "<%s id=0x%x len=%d",
3010 sppp_auth_type_name(PPP_CHAP, h->type),
3011 h->ident, ntohs(h->len));
3012 sppp_print_bytes((u_char*) (h+1), len-4);
3020 SPP_FMT "chap input <%s id=0x%x len=%d name=",
3022 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
3024 sppp_print_string((char*) name, name_len);
3025 log(-1, " value-size=%d value=", value_len);
3026 sppp_print_bytes(value, value_len);
3030 /* Compute reply value. */
3032 MD5Update(&ctx, &h->ident, 1);
3033 MD5Update(&ctx, sp->myauth.secret,
3034 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
3035 MD5Update(&ctx, value, value_len);
3036 MD5Final(digest, &ctx);
3037 dsize = sizeof digest;
3039 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
3040 sizeof dsize, (const char *)&dsize,
3041 sizeof digest, digest,
3042 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3049 log(LOG_DEBUG, SPP_FMT "chap success",
3053 sppp_print_string((char*)(h + 1), len - 4);
3058 sp->pp_flags &= ~PP_NEEDAUTH;
3059 if (sp->myauth.proto == PPP_CHAP &&
3060 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
3061 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
3063 * We are authenticator for CHAP but didn't
3064 * complete yet. Leave it to tlu to proceed
3071 sppp_phase_network(sp);
3076 log(LOG_INFO, SPP_FMT "chap failure",
3080 sppp_print_string((char*)(h + 1), len - 4);
3084 log(LOG_INFO, SPP_FMT "chap failure\n",
3086 /* await LCP shutdown by authenticator */
3089 /* response is my authproto */
3091 value = 1 + (u_char*)(h+1);
3092 value_len = value[-1];
3093 name = value + value_len;
3094 name_len = len - value_len - 5;
3098 SPP_FMT "chap corrupted response "
3099 "<%s id=0x%x len=%d",
3101 sppp_auth_type_name(PPP_CHAP, h->type),
3102 h->ident, ntohs(h->len));
3103 sppp_print_bytes((u_char*)(h+1), len-4);
3108 if (h->ident != sp->confid[IDX_CHAP]) {
3111 SPP_FMT "chap dropping response for old ID "
3112 "(got %d, expected %d)\n",
3114 h->ident, sp->confid[IDX_CHAP]);
3117 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
3118 || bcmp(name, sp->hisauth.name, name_len) != 0) {
3119 log(LOG_INFO, SPP_FMT "chap response, his name ",
3121 sppp_print_string(name, name_len);
3122 log(-1, " != expected ");
3123 sppp_print_string(sp->hisauth.name,
3124 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
3128 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
3129 "<%s id=0x%x len=%d name=",
3131 sppp_state_name(sp->state[IDX_CHAP]),
3132 sppp_auth_type_name(PPP_CHAP, h->type),
3133 h->ident, ntohs (h->len));
3134 sppp_print_string((char*)name, name_len);
3135 log(-1, " value-size=%d value=", value_len);
3136 sppp_print_bytes(value, value_len);
3139 if (value_len != AUTHKEYLEN) {
3142 SPP_FMT "chap bad hash value length: "
3143 "%d bytes, should be %d\n",
3144 SPP_ARGS(ifp), value_len,
3150 MD5Update(&ctx, &h->ident, 1);
3151 MD5Update(&ctx, sp->hisauth.secret,
3152 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
3153 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
3154 MD5Final(digest, &ctx);
3156 #define FAILMSG "Failed..."
3157 #define SUCCMSG "Welcome!"
3159 if (value_len != sizeof digest ||
3160 bcmp(digest, value, value_len) != 0) {
3161 /* action scn, tld */
3162 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
3163 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3168 /* action sca, perhaps tlu */
3169 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
3170 sp->state[IDX_CHAP] == STATE_OPENED)
3171 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
3172 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3174 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
3175 sppp_cp_change_state(&chap, sp, STATE_OPENED);
3181 /* Unknown CHAP packet type -- ignore. */
3183 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
3184 "<0x%x id=0x%xh len=%d",
3186 sppp_state_name(sp->state[IDX_CHAP]),
3187 h->type, h->ident, ntohs(h->len));
3188 sppp_print_bytes((u_char*)(h+1), len-4);
3197 sppp_chap_init(struct sppp *sp)
3199 /* Chap doesn't have STATE_INITIAL at all. */
3200 sp->state[IDX_CHAP] = STATE_CLOSED;
3201 sp->fail_counter[IDX_CHAP] = 0;
3202 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3203 callout_handle_init(&sp->ch[IDX_CHAP]);
3208 sppp_chap_open(struct sppp *sp)
3210 if (sp->myauth.proto == PPP_CHAP &&
3211 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3212 /* we are authenticator for CHAP, start it */
3214 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3215 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3217 /* nothing to be done if we are peer, await a challenge */
3221 sppp_chap_close(struct sppp *sp)
3223 if (sp->state[IDX_CHAP] != STATE_CLOSED)
3224 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3228 sppp_chap_TO(void *cookie)
3230 struct sppp *sp = (struct sppp *)cookie;
3236 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
3238 sppp_state_name(sp->state[IDX_CHAP]),
3239 sp->rst_counter[IDX_CHAP]);
3241 if (--sp->rst_counter[IDX_CHAP] < 0)
3243 switch (sp->state[IDX_CHAP]) {
3244 case STATE_REQ_SENT:
3246 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3250 /* TO+ (or TO*) event */
3251 switch (sp->state[IDX_CHAP]) {
3254 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3256 case STATE_REQ_SENT:
3258 /* sppp_cp_change_state() will restart the timer */
3259 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3267 sppp_chap_tlu(struct sppp *sp)
3273 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3276 * Some broken CHAP implementations (Conware CoNet, firmware
3277 * 4.0.?) don't want to re-authenticate their CHAP once the
3278 * initial challenge-response exchange has taken place.
3279 * Provide for an option to avoid rechallenges.
3281 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
3283 * Compute the re-challenge timeout. This will yield
3284 * a number between 300 and 810 seconds.
3286 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
3287 TIMEOUT(chap.TO, (void *)sp, i * hz, sp->ch[IDX_CHAP]);
3292 SPP_FMT "chap %s, ",
3294 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
3295 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
3296 log(-1, "next re-challenge in %d seconds\n", i);
3298 log(-1, "re-challenging supressed\n");
3302 /* indicate to LCP that we need to be closed down */
3303 sp->lcp.protos |= (1 << IDX_CHAP);
3305 if (sp->pp_flags & PP_NEEDAUTH) {
3307 * Remote is authenticator, but his auth proto didn't
3308 * complete yet. Defer the transition to network
3317 * If we are already in phase network, we are done here. This
3318 * is the case if this is a dummy tlu event after a re-challenge.
3320 if (sp->pp_phase != PHASE_NETWORK)
3321 sppp_phase_network(sp);
3325 sppp_chap_tld(struct sppp *sp)
3330 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
3331 UNTIMEOUT(chap.TO, (void *)sp, sp->ch[IDX_CHAP]);
3332 sp->lcp.protos &= ~(1 << IDX_CHAP);
3338 sppp_chap_scr(struct sppp *sp)
3343 /* Compute random challenge. */
3344 ch = (u_long *)sp->myauth.challenge;
3345 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3346 read_random(&seed, sizeof seed);
3351 seed = tv.tv_sec ^ tv.tv_usec;
3354 ch[0] = seed ^ random();
3355 ch[1] = seed ^ random();
3356 ch[2] = seed ^ random();
3357 ch[3] = seed ^ random();
3360 sp->confid[IDX_CHAP] = ++sp->pp_seq;
3362 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
3363 sizeof clen, (const char *)&clen,
3364 (size_t)AUTHKEYLEN, sp->myauth.challenge,
3365 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3371 *--------------------------------------------------------------------------*
3373 * The PAP implementation. *
3375 *--------------------------------------------------------------------------*
3378 * For PAP, we need to keep a little state also if we are the peer, not the
3379 * authenticator. This is since we don't get a request to authenticate, but
3380 * have to repeatedly authenticate ourself until we got a response (or the
3381 * retry counter is expired).
3385 * Handle incoming PAP packets. */
3387 sppp_pap_input(struct sppp *sp, struct mbuf *m)
3390 struct lcp_header *h;
3392 u_char *name, *passwd, mlen;
3393 int name_len, passwd_len;
3395 len = m->m_pkthdr.len;
3399 SPP_FMT "pap invalid packet length: %d bytes\n",
3400 SPP_ARGS(ifp), len);
3403 h = mtod (m, struct lcp_header*);
3404 if (len > ntohs (h->len))
3405 len = ntohs (h->len);
3407 /* PAP request is my authproto */
3409 name = 1 + (u_char*)(h+1);
3410 name_len = name[-1];
3411 passwd = name + name_len + 1;
3412 if (name_len > len - 6 ||
3413 (passwd_len = passwd[-1]) > len - 6 - name_len) {
3415 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
3416 "<%s id=0x%x len=%d",
3418 sppp_auth_type_name(PPP_PAP, h->type),
3419 h->ident, ntohs(h->len));
3420 sppp_print_bytes((u_char*)(h+1), len-4);
3426 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
3427 "<%s id=0x%x len=%d name=",
3429 sppp_state_name(sp->state[IDX_PAP]),
3430 sppp_auth_type_name(PPP_PAP, h->type),
3431 h->ident, ntohs(h->len));
3432 sppp_print_string((char*)name, name_len);
3433 log(-1, " passwd=");
3434 sppp_print_string((char*)passwd, passwd_len);
3437 if (name_len > AUTHNAMELEN ||
3438 passwd_len > AUTHKEYLEN ||
3439 bcmp(name, sp->hisauth.name, name_len) != 0 ||
3440 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
3441 /* action scn, tld */
3442 mlen = sizeof(FAILMSG) - 1;
3443 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
3444 sizeof mlen, (const char *)&mlen,
3445 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3450 /* action sca, perhaps tlu */
3451 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
3452 sp->state[IDX_PAP] == STATE_OPENED) {
3453 mlen = sizeof(SUCCMSG) - 1;
3454 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
3455 sizeof mlen, (const char *)&mlen,
3456 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3459 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
3460 sppp_cp_change_state(&pap, sp, STATE_OPENED);
3465 /* ack and nak are his authproto */
3467 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3469 log(LOG_DEBUG, SPP_FMT "pap success",
3471 name_len = *((char *)h);
3472 if (len > 5 && name_len) {
3474 sppp_print_string((char*)(h+1), name_len);
3479 sp->pp_flags &= ~PP_NEEDAUTH;
3480 if (sp->myauth.proto == PPP_PAP &&
3481 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
3482 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
3484 * We are authenticator for PAP but didn't
3485 * complete yet. Leave it to tlu to proceed
3492 sppp_phase_network(sp);
3496 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3498 log(LOG_INFO, SPP_FMT "pap failure",
3500 name_len = *((char *)h);
3501 if (len > 5 && name_len) {
3503 sppp_print_string((char*)(h+1), name_len);
3507 log(LOG_INFO, SPP_FMT "pap failure\n",
3509 /* await LCP shutdown by authenticator */
3513 /* Unknown PAP packet type -- ignore. */
3515 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
3516 "<0x%x id=0x%x len=%d",
3518 h->type, h->ident, ntohs(h->len));
3519 sppp_print_bytes((u_char*)(h+1), len-4);
3528 sppp_pap_init(struct sppp *sp)
3530 /* PAP doesn't have STATE_INITIAL at all. */
3531 sp->state[IDX_PAP] = STATE_CLOSED;
3532 sp->fail_counter[IDX_PAP] = 0;
3533 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3534 callout_handle_init(&sp->ch[IDX_PAP]);
3535 callout_handle_init(&sp->pap_my_to_ch);
3540 sppp_pap_open(struct sppp *sp)
3542 if (sp->hisauth.proto == PPP_PAP &&
3543 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3544 /* we are authenticator for PAP, start our timer */
3545 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
3546 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
3548 if (sp->myauth.proto == PPP_PAP) {
3549 /* we are peer, send a request, and start a timer */
3551 TIMEOUT(sppp_pap_my_TO, (void *)sp, sp->lcp.timeout,
3557 sppp_pap_close(struct sppp *sp)
3559 if (sp->state[IDX_PAP] != STATE_CLOSED)
3560 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
3564 * That's the timeout routine if we are authenticator. Since the
3565 * authenticator is basically passive in PAP, we can't do much here.
3568 sppp_pap_TO(void *cookie)
3570 struct sppp *sp = (struct sppp *)cookie;
3576 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
3578 sppp_state_name(sp->state[IDX_PAP]),
3579 sp->rst_counter[IDX_PAP]);
3581 if (--sp->rst_counter[IDX_PAP] < 0)
3583 switch (sp->state[IDX_PAP]) {
3584 case STATE_REQ_SENT:
3586 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
3590 /* TO+ event, not very much we could do */
3591 switch (sp->state[IDX_PAP]) {
3592 case STATE_REQ_SENT:
3593 /* sppp_cp_change_state() will restart the timer */
3594 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
3602 * That's the timeout handler if we are peer. Since the peer is active,
3603 * we need to retransmit our PAP request since it is apparently lost.
3604 * XXX We should impose a max counter.
3607 sppp_pap_my_TO(void *cookie)
3609 struct sppp *sp = (struct sppp *)cookie;
3613 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
3620 sppp_pap_tlu(struct sppp *sp)
3625 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
3628 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
3629 SPP_ARGS(ifp), pap.name);
3632 /* indicate to LCP that we need to be closed down */
3633 sp->lcp.protos |= (1 << IDX_PAP);
3635 if (sp->pp_flags & PP_NEEDAUTH) {
3637 * Remote is authenticator, but his auth proto didn't
3638 * complete yet. Defer the transition to network
3645 sppp_phase_network(sp);
3649 sppp_pap_tld(struct sppp *sp)
3654 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
3655 UNTIMEOUT(pap.TO, (void *)sp, sp->ch[IDX_PAP]);
3656 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3657 sp->lcp.protos &= ~(1 << IDX_PAP);
3663 sppp_pap_scr(struct sppp *sp)
3665 u_char idlen, pwdlen;
3667 sp->confid[IDX_PAP] = ++sp->pp_seq;
3668 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
3669 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
3671 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
3672 sizeof idlen, (const char *)&idlen,
3673 (size_t)idlen, sp->myauth.name,
3674 sizeof pwdlen, (const char *)&pwdlen,
3675 (size_t)pwdlen, sp->myauth.secret,
3680 * Random miscellaneous functions.
3684 * Send a PAP or CHAP proto packet.
3686 * Varadic function, each of the elements for the ellipsis is of type
3687 * ``size_t mlen, const u_char *msg''. Processing will stop iff
3689 * NOTE: never declare variadic functions with types subject to type
3690 * promotion (i.e. u_char). This is asking for big trouble depending
3691 * on the architecture you are on...
3695 sppp_auth_send(const struct cp *cp, struct sppp *sp,
3696 unsigned int type, unsigned int id,
3700 struct ppp_header *h;
3701 struct lcp_header *lh;
3709 MGETHDR (m, M_DONTWAIT, MT_DATA);
3712 m->m_pkthdr.rcvif = 0;
3714 h = mtod (m, struct ppp_header*);
3715 h->address = PPP_ALLSTATIONS; /* broadcast address */
3716 h->control = PPP_UI; /* Unnumbered Info */
3717 h->protocol = htons(cp->proto);
3719 lh = (struct lcp_header*)(h + 1);
3722 p = (u_char*) (lh+1);
3727 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
3728 msg = va_arg(ap, const char *);
3730 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
3736 bcopy(msg, p, mlen);
3741 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
3742 lh->len = htons (LCP_HEADER_LEN + len);
3745 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
3746 SPP_ARGS(ifp), cp->name,
3747 sppp_auth_type_name(cp->proto, lh->type),
3748 lh->ident, ntohs(lh->len));
3749 sppp_print_bytes((u_char*) (lh+1), len);
3752 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3))
3757 * Flush interface queue.
3760 sppp_qflush(struct ifqueue *ifq)
3775 * Send keepalive packets, every 10 seconds.
3778 sppp_keepalive(void *dummy)
3784 for (sp=spppq; sp; sp=sp->pp_next) {
3785 struct ifnet *ifp = &sp->pp_if;
3787 /* Keepalive mode disabled or channel down? */
3788 if (! (sp->pp_flags & PP_KEEPALIVE) ||
3789 ! (ifp->if_flags & IFF_RUNNING))
3792 /* No keepalive in PPP mode if LCP not opened yet. */
3793 if (sp->pp_mode != IFF_CISCO &&
3794 sp->pp_phase < PHASE_AUTHENTICATE)
3797 if (sp->pp_alivecnt == MAXALIVECNT) {
3798 /* No keepalive packets got. Stop the interface. */
3799 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
3801 sppp_qflush (&sp->pp_cpq);
3802 if (sp->pp_mode != IFF_CISCO) {
3804 /* Shut down the PPP link. */
3806 /* Initiate negotiation. XXX */
3810 if (sp->pp_alivecnt <= MAXALIVECNT)
3812 if (sp->pp_mode == IFF_CISCO)
3813 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
3815 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
3816 long nmagic = htonl (sp->lcp.magic);
3817 sp->lcp.echoid = ++sp->pp_seq;
3818 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
3819 sp->lcp.echoid, 4, &nmagic);
3823 TIMEOUT(sppp_keepalive, 0, hz * 10, keepalive_ch);
3827 * Get both IP addresses.
3830 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
3832 struct ifnet *ifp = &sp->pp_if;
3834 struct sockaddr_in *si, *sm;
3840 * Pick the first AF_INET address from the list,
3841 * aliases don't make any sense on a p2p link anyway.
3844 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3845 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
3846 #elif defined(__NetBSD__) || defined (__OpenBSD__)
3847 for (ifa = ifp->if_addrlist.tqh_first;
3849 ifa = ifa->ifa_list.tqe_next)
3851 for (ifa = ifp->if_addrlist;
3853 ifa = ifa->ifa_next)
3855 if (ifa->ifa_addr->sa_family == AF_INET) {
3856 si = (struct sockaddr_in *)ifa->ifa_addr;
3857 sm = (struct sockaddr_in *)ifa->ifa_netmask;
3862 if (si && si->sin_addr.s_addr) {
3863 ssrc = si->sin_addr.s_addr;
3865 *srcmask = ntohl(sm->sin_addr.s_addr);
3868 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
3869 if (si && si->sin_addr.s_addr)
3870 ddst = si->sin_addr.s_addr;
3873 if (dst) *dst = ntohl(ddst);
3874 if (src) *src = ntohl(ssrc);
3878 * Set my IP address. Must be called at splimp.
3881 sppp_set_ip_addr(struct sppp *sp, u_long src)
3885 struct sockaddr_in *si;
3888 * Pick the first AF_INET address from the list,
3889 * aliases don't make any sense on a p2p link anyway.
3892 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3893 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
3894 #elif defined(__NetBSD__) || defined (__OpenBSD__)
3895 for (ifa = ifp->if_addrlist.tqh_first;
3897 ifa = ifa->ifa_list.tqe_next)
3899 for (ifa = ifp->if_addrlist;
3901 ifa = ifa->ifa_next)
3904 if (ifa->ifa_addr->sa_family == AF_INET)
3906 si = (struct sockaddr_in *)ifa->ifa_addr;
3915 #if __NetBSD_Version__ >= 103080000
3916 struct sockaddr_in new_sin = *si;
3918 new_sin.sin_addr.s_addr = htonl(src);
3919 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 1);
3922 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: in_ifinit "
3923 " failed, error=%d\n", SPP_ARGS(ifp), error);
3926 /* delete old route */
3927 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
3930 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
3931 SPP_ARGS(ifp), error);
3934 /* set new address */
3935 si->sin_addr.s_addr = htonl(src);
3938 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
3941 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
3942 SPP_ARGS(ifp), error);
3949 sppp_params(struct sppp *sp, u_long cmd, void *data)
3952 struct ifreq *ifr = (struct ifreq *)data;
3956 * ifr->ifr_data is supposed to point to a struct spppreq.
3957 * Check the cmd word first before attempting to fetch all the
3960 if ((subcmd = fuword(ifr->ifr_data)) == -1)
3963 if (copyin((caddr_t)ifr->ifr_data, &spr, sizeof spr) != 0)
3968 if (cmd != SIOCGIFGENERIC)
3971 * We copy over the entire current state, but clean
3972 * out some of the stuff we don't wanna pass up.
3973 * Remember, SIOCGIFGENERIC is unprotected, and can be
3974 * called by any user. No need to ever get PAP or
3975 * CHAP secrets back to userland anyway.
3977 bcopy(sp, &spr.defs, sizeof(struct sppp));
3978 bzero(spr.defs.myauth.secret, AUTHKEYLEN);
3979 bzero(spr.defs.myauth.challenge, AUTHKEYLEN);
3980 bzero(spr.defs.hisauth.secret, AUTHKEYLEN);
3981 bzero(spr.defs.hisauth.challenge, AUTHKEYLEN);
3982 return copyout(&spr, (caddr_t)ifr->ifr_data, sizeof spr);
3985 if (cmd != SIOCSIFGENERIC)
3988 * We have a very specific idea of which fields we allow
3989 * being passed back from userland, so to not clobber our
3990 * current state. For one, we only allow setting
3991 * anything if LCP is in dead phase. Once the LCP
3992 * negotiations started, the authentication settings must
3993 * not be changed again. (The administrator can force an
3994 * ifconfig down in order to get LCP back into dead
3997 * Also, we only allow for authentication parameters to be
4000 * XXX Should allow to set or clear pp_flags.
4002 * Finally, if the respective authentication protocol to
4003 * be used is set differently than 0, but the secret is
4004 * passed as all zeros, we don't trash the existing secret.
4005 * This allows an administrator to change the system name
4006 * only without clobbering the secret (which he didn't get
4007 * back in a previous SPPPIOGDEFS call). However, the
4008 * secrets are cleared if the authentication protocol is
4011 if (sp->pp_phase != PHASE_DEAD)
4014 if ((spr.defs.myauth.proto != 0 && spr.defs.myauth.proto != PPP_PAP &&
4015 spr.defs.myauth.proto != PPP_CHAP) ||
4016 (spr.defs.hisauth.proto != 0 && spr.defs.hisauth.proto != PPP_PAP &&
4017 spr.defs.hisauth.proto != PPP_CHAP))
4020 if (spr.defs.myauth.proto == 0)
4021 /* resetting myauth */
4022 bzero(&sp->myauth, sizeof sp->myauth);
4024 /* setting/changing myauth */
4025 sp->myauth.proto = spr.defs.myauth.proto;
4026 bcopy(spr.defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
4027 if (spr.defs.myauth.secret[0] != '\0')
4028 bcopy(spr.defs.myauth.secret, sp->myauth.secret,
4031 if (spr.defs.hisauth.proto == 0)
4032 /* resetting hisauth */
4033 bzero(&sp->hisauth, sizeof sp->hisauth);
4035 /* setting/changing hisauth */
4036 sp->hisauth.proto = spr.defs.hisauth.proto;
4037 sp->hisauth.flags = spr.defs.hisauth.flags;
4038 bcopy(spr.defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
4039 if (spr.defs.hisauth.secret[0] != '\0')
4040 bcopy(spr.defs.hisauth.secret, sp->hisauth.secret,
4053 sppp_phase_network(struct sppp *sp)
4059 sp->pp_phase = PHASE_NETWORK;
4062 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
4063 sppp_phase_name(sp->pp_phase));
4065 /* Notify NCPs now. */
4066 for (i = 0; i < IDX_COUNT; i++)
4067 if ((cps[i])->flags & CP_NCP)
4070 /* Send Up events to all NCPs. */
4071 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
4072 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_NCP))
4075 /* if no NCP is starting, all this was in vain, close down */
4076 sppp_lcp_check_and_close(sp);
4081 sppp_cp_type_name(u_char type)
4083 static char buf[12];
4085 case CONF_REQ: return "conf-req";
4086 case CONF_ACK: return "conf-ack";
4087 case CONF_NAK: return "conf-nak";
4088 case CONF_REJ: return "conf-rej";
4089 case TERM_REQ: return "term-req";
4090 case TERM_ACK: return "term-ack";
4091 case CODE_REJ: return "code-rej";
4092 case PROTO_REJ: return "proto-rej";
4093 case ECHO_REQ: return "echo-req";
4094 case ECHO_REPLY: return "echo-reply";
4095 case DISC_REQ: return "discard-req";
4097 snprintf (buf, sizeof(buf), "cp/0x%x", type);
4102 sppp_auth_type_name(u_short proto, u_char type)
4104 static char buf[12];
4108 case CHAP_CHALLENGE: return "challenge";
4109 case CHAP_RESPONSE: return "response";
4110 case CHAP_SUCCESS: return "success";
4111 case CHAP_FAILURE: return "failure";
4115 case PAP_REQ: return "req";
4116 case PAP_ACK: return "ack";
4117 case PAP_NAK: return "nak";
4120 snprintf (buf, sizeof(buf), "auth/0x%x", type);
4125 sppp_lcp_opt_name(u_char opt)
4127 static char buf[12];
4129 case LCP_OPT_MRU: return "mru";
4130 case LCP_OPT_ASYNC_MAP: return "async-map";
4131 case LCP_OPT_AUTH_PROTO: return "auth-proto";
4132 case LCP_OPT_QUAL_PROTO: return "qual-proto";
4133 case LCP_OPT_MAGIC: return "magic";
4134 case LCP_OPT_PROTO_COMP: return "proto-comp";
4135 case LCP_OPT_ADDR_COMP: return "addr-comp";
4137 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
4142 sppp_ipcp_opt_name(u_char opt)
4144 static char buf[12];
4146 case IPCP_OPT_ADDRESSES: return "addresses";
4147 case IPCP_OPT_COMPRESSION: return "compression";
4148 case IPCP_OPT_ADDRESS: return "address";
4150 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
4155 sppp_state_name(int state)
4158 case STATE_INITIAL: return "initial";
4159 case STATE_STARTING: return "starting";
4160 case STATE_CLOSED: return "closed";
4161 case STATE_STOPPED: return "stopped";
4162 case STATE_CLOSING: return "closing";
4163 case STATE_STOPPING: return "stopping";
4164 case STATE_REQ_SENT: return "req-sent";
4165 case STATE_ACK_RCVD: return "ack-rcvd";
4166 case STATE_ACK_SENT: return "ack-sent";
4167 case STATE_OPENED: return "opened";
4173 sppp_phase_name(enum ppp_phase phase)
4176 case PHASE_DEAD: return "dead";
4177 case PHASE_ESTABLISH: return "establish";
4178 case PHASE_TERMINATE: return "terminate";
4179 case PHASE_AUTHENTICATE: return "authenticate";
4180 case PHASE_NETWORK: return "network";
4186 sppp_proto_name(u_short proto)
4188 static char buf[12];
4190 case PPP_LCP: return "lcp";
4191 case PPP_IPCP: return "ipcp";
4192 case PPP_PAP: return "pap";
4193 case PPP_CHAP: return "chap";
4195 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
4200 sppp_print_bytes(const u_char *p, u_short len)
4203 log(-1, " %*D", len, p, "-");
4207 sppp_print_string(const char *p, u_short len)
4214 * Print only ASCII chars directly. RFC 1994 recommends
4215 * using only them, but we don't rely on it. */
4216 if (c < ' ' || c > '~')
4217 log(-1, "\\x%x", c);
4224 sppp_dotted_quad(u_long addr)
4227 sprintf(s, "%d.%d.%d.%d",
4228 (int)((addr >> 24) & 0xff),
4229 (int)((addr >> 16) & 0xff),
4230 (int)((addr >> 8) & 0xff),
4231 (int)(addr & 0xff));
4236 sppp_strnlen(u_char *p, int max)
4240 for (len = 0; len < max && *p; ++p)
4245 /* a dummy, used to drop uninteresting events */
4247 sppp_null(struct sppp *unused)
4249 /* do just nothing */