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/sockio.h>
42 #include <sys/socket.h>
43 #include <sys/syslog.h>
44 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
45 #include <machine/random.h>
47 #include <sys/malloc.h>
50 #if defined (__OpenBSD__)
57 #include <net/netisr.h>
58 #include <net/if_types.h>
59 #include <net/route.h>
61 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
62 #include <machine/random.h>
64 #if defined (__NetBSD__) || defined (__OpenBSD__)
65 #include <machine/cpu.h> /* XXX for softnet */
68 #include <machine/stdarg.h>
71 #include <netinet/in.h>
72 #include <netinet/in_systm.h>
73 #include <netinet/in_var.h>
74 #include <netinet/ip.h>
75 #include <netinet/tcp.h>
76 # if defined (__FreeBSD__) || defined (__OpenBSD__)
77 # include <netinet/if_ether.h>
79 # include <net/ethertypes.h>
82 # error Huh? sppp without INET?
86 #include <netipx/ipx.h>
87 #include <netipx/ipx_if.h>
92 #include <netns/ns_if.h>
95 #include <net/if_sppp.h>
97 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
98 # define UNTIMEOUT(fun, arg, handle) untimeout(fun, arg, handle)
99 # define TIMEOUT(fun, arg1, arg2, handle) handle = timeout(fun, arg1, arg2)
100 # define IOCTL_CMD_T u_long
102 # define UNTIMEOUT(fun, arg, handle) untimeout(fun, arg)
103 # define TIMEOUT(fun, arg1, arg2, handle) timeout(fun, arg1, arg2)
104 # define IOCTL_CMD_T int
107 #define MAXALIVECNT 3 /* max. alive packets */
110 * Interface flags that can be set in an ifconfig command.
112 * Setting link0 will make the link passive, i.e. it will be marked
113 * as being administrative openable, but won't be opened to begin
114 * with. Incoming calls will be answered, or subsequent calls with
115 * -link1 will cause the administrative open of the LCP layer.
117 * Setting link1 will cause the link to auto-dial only as packets
120 * Setting IFF_DEBUG will syslog the option negotiation and state
121 * transitions at level kern.debug. Note: all logs consistently look
124 * <if-name><unit>: <proto-name> <additional info...>
126 * with <if-name><unit> being something like "bppp0", and <proto-name>
127 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
130 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
131 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
132 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
134 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
135 #define PPP_UI 0x03 /* Unnumbered Information */
136 #define PPP_IP 0x0021 /* Internet Protocol */
137 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
138 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
139 #define PPP_IPX 0x002b /* Novell IPX Protocol */
140 #define PPP_LCP 0xc021 /* Link Control Protocol */
141 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
142 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
143 #define PPP_IPCP 0x8021 /* Internet Protocol 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 PAP_REQ 1 /* PAP name/password request */
171 #define PAP_ACK 2 /* PAP acknowledge */
172 #define PAP_NAK 3 /* PAP fail */
174 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
175 #define CHAP_RESPONSE 2 /* CHAP challenge response */
176 #define CHAP_SUCCESS 3 /* CHAP response ok */
177 #define CHAP_FAILURE 4 /* CHAP response failed */
179 #define CHAP_MD5 5 /* hash algorithm - MD5 */
181 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
182 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
183 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
184 #define CISCO_ADDR_REQ 0 /* Cisco address request */
185 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
186 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
188 /* states are named and numbered according to RFC 1661 */
189 #define STATE_INITIAL 0
190 #define STATE_STARTING 1
191 #define STATE_CLOSED 2
192 #define STATE_STOPPED 3
193 #define STATE_CLOSING 4
194 #define STATE_STOPPING 5
195 #define STATE_REQ_SENT 6
196 #define STATE_ACK_RCVD 7
197 #define STATE_ACK_SENT 8
198 #define STATE_OPENED 9
205 #define PPP_HEADER_LEN sizeof (struct ppp_header)
212 #define LCP_HEADER_LEN sizeof (struct lcp_header)
214 struct cisco_packet {
222 #define CISCO_PACKET_LEN 18
225 * We follow the spelling and capitalization of RFC 1661 here, to make
226 * it easier comparing with the standard. Please refer to this RFC in
227 * case you can't make sense out of these abbreviation; it will also
228 * explain the semantics related to the various events and actions.
231 u_short proto; /* PPP control protocol number */
232 u_char protoidx; /* index into state table in struct sppp */
234 #define CP_LCP 0x01 /* this is the LCP */
235 #define CP_AUTH 0x02 /* this is an authentication protocol */
236 #define CP_NCP 0x04 /* this is a NCP */
237 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
238 const char *name; /* name of this control protocol */
240 void (*Up)(struct sppp *sp);
241 void (*Down)(struct sppp *sp);
242 void (*Open)(struct sppp *sp);
243 void (*Close)(struct sppp *sp);
244 void (*TO)(void *sp);
245 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
246 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
247 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
249 void (*tlu)(struct sppp *sp);
250 void (*tld)(struct sppp *sp);
251 void (*tls)(struct sppp *sp);
252 void (*tlf)(struct sppp *sp);
253 void (*scr)(struct sppp *sp);
256 static struct sppp *spppq;
257 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
258 static struct callout_handle keepalive_ch;
261 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
262 #define SPP_FMT "%s%d: "
263 #define SPP_ARGS(ifp) (ifp)->if_name, (ifp)->if_unit
265 #define SPP_FMT "%s: "
266 #define SPP_ARGS(ifp) (ifp)->if_xname
270 * The following disgusting hack gets around the problem that IP TOS
271 * can't be set yet. We want to put "interactive" traffic on a high
272 * priority queue. To decide if traffic is interactive, we check that
273 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
275 * XXX is this really still necessary? - joerg -
277 static u_short interactive_ports[8] = {
281 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
283 /* almost every function needs these */
285 struct ifnet *ifp = &sp->pp_if; \
286 int debug = ifp->if_flags & IFF_DEBUG
288 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
289 struct sockaddr *dst, struct rtentry *rt);
291 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
292 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
294 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
296 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
297 u_char ident, u_short len, void *data);
298 /* static void sppp_cp_timeout(void *arg); */
299 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
301 static void sppp_auth_send(const struct cp *cp,
302 struct sppp *sp, unsigned int type, unsigned int id,
305 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
306 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
307 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
308 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
309 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
311 static void sppp_null(struct sppp *sp);
313 static void sppp_lcp_init(struct sppp *sp);
314 static void sppp_lcp_up(struct sppp *sp);
315 static void sppp_lcp_down(struct sppp *sp);
316 static void sppp_lcp_open(struct sppp *sp);
317 static void sppp_lcp_close(struct sppp *sp);
318 static void sppp_lcp_TO(void *sp);
319 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
320 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
321 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
322 static void sppp_lcp_tlu(struct sppp *sp);
323 static void sppp_lcp_tld(struct sppp *sp);
324 static void sppp_lcp_tls(struct sppp *sp);
325 static void sppp_lcp_tlf(struct sppp *sp);
326 static void sppp_lcp_scr(struct sppp *sp);
327 static void sppp_lcp_check_and_close(struct sppp *sp);
328 static int sppp_ncp_check(struct sppp *sp);
330 static void sppp_ipcp_init(struct sppp *sp);
331 static void sppp_ipcp_up(struct sppp *sp);
332 static void sppp_ipcp_down(struct sppp *sp);
333 static void sppp_ipcp_open(struct sppp *sp);
334 static void sppp_ipcp_close(struct sppp *sp);
335 static void sppp_ipcp_TO(void *sp);
336 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
337 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
338 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
339 static void sppp_ipcp_tlu(struct sppp *sp);
340 static void sppp_ipcp_tld(struct sppp *sp);
341 static void sppp_ipcp_tls(struct sppp *sp);
342 static void sppp_ipcp_tlf(struct sppp *sp);
343 static void sppp_ipcp_scr(struct sppp *sp);
345 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
346 static void sppp_pap_init(struct sppp *sp);
347 static void sppp_pap_open(struct sppp *sp);
348 static void sppp_pap_close(struct sppp *sp);
349 static void sppp_pap_TO(void *sp);
350 static void sppp_pap_my_TO(void *sp);
351 static void sppp_pap_tlu(struct sppp *sp);
352 static void sppp_pap_tld(struct sppp *sp);
353 static void sppp_pap_scr(struct sppp *sp);
355 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
356 static void sppp_chap_init(struct sppp *sp);
357 static void sppp_chap_open(struct sppp *sp);
358 static void sppp_chap_close(struct sppp *sp);
359 static void sppp_chap_TO(void *sp);
360 static void sppp_chap_tlu(struct sppp *sp);
361 static void sppp_chap_tld(struct sppp *sp);
362 static void sppp_chap_scr(struct sppp *sp);
364 static const char *sppp_auth_type_name(u_short proto, u_char type);
365 static const char *sppp_cp_type_name(u_char type);
366 static const char *sppp_dotted_quad(u_long addr);
367 static const char *sppp_ipcp_opt_name(u_char opt);
368 static const char *sppp_lcp_opt_name(u_char opt);
369 static const char *sppp_phase_name(enum ppp_phase phase);
370 static const char *sppp_proto_name(u_short proto);
371 static const char *sppp_state_name(int state);
372 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
373 static int sppp_strnlen(u_char *p, int max);
374 static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst,
376 static void sppp_keepalive(void *dummy);
377 static void sppp_phase_network(struct sppp *sp);
378 static void sppp_print_bytes(const u_char *p, u_short len);
379 static void sppp_print_string(const char *p, u_short len);
380 static void sppp_qflush(struct ifqueue *ifq);
381 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
383 /* our control protocol descriptors */
384 static const struct cp lcp = {
385 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
386 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
387 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
388 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
392 static const struct cp ipcp = {
393 PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
394 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
395 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
396 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
400 static const struct cp pap = {
401 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
402 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
403 sppp_pap_TO, 0, 0, 0,
404 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
408 static const struct cp chap = {
409 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
410 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
411 sppp_chap_TO, 0, 0, 0,
412 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
416 static const struct cp *cps[IDX_COUNT] = {
418 &ipcp, /* IDX_IPCP */
420 &chap, /* IDX_CHAP */
425 * Exported functions, comprising our interface to the lower layer.
429 * Process the received packet.
432 sppp_input(struct ifnet *ifp, struct mbuf *m)
434 struct ppp_header *h;
435 struct ifqueue *inq = 0;
437 struct sppp *sp = (struct sppp *)ifp;
438 int debug = ifp->if_flags & IFF_DEBUG;
440 if (ifp->if_flags & IFF_UP)
441 /* Count received bytes, add FCS and one flag */
442 ifp->if_ibytes += m->m_pkthdr.len + 3;
444 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
445 /* Too small packet, drop it. */
448 SPP_FMT "input packet is too small, %d bytes\n",
449 SPP_ARGS(ifp), m->m_pkthdr.len);
457 /* Get PPP header. */
458 h = mtod (m, struct ppp_header*);
459 m_adj (m, PPP_HEADER_LEN);
461 switch (h->address) {
462 case PPP_ALLSTATIONS:
463 if (h->control != PPP_UI)
465 if (sp->pp_mode == IFF_CISCO) {
468 SPP_FMT "PPP packet in Cisco mode "
469 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
471 h->address, h->control, ntohs(h->protocol));
474 switch (ntohs (h->protocol)) {
478 SPP_FMT "rejecting protocol "
479 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
481 h->address, h->control, ntohs(h->protocol));
482 if (sp->state[IDX_LCP] == STATE_OPENED)
483 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
484 ++sp->pp_seq, m->m_pkthdr.len + 2,
489 sppp_cp_input(&lcp, sp, m);
493 if (sp->pp_phase >= PHASE_AUTHENTICATE)
494 sppp_pap_input(sp, m);
498 if (sp->pp_phase >= PHASE_AUTHENTICATE)
499 sppp_chap_input(sp, m);
504 if (sp->pp_phase == PHASE_NETWORK)
505 sppp_cp_input(&ipcp, sp, m);
509 if (sp->state[IDX_IPCP] == STATE_OPENED) {
510 schednetisr (NETISR_IP);
517 /* IPX IPXCP not implemented yet */
518 if (sp->pp_phase == PHASE_NETWORK) {
519 schednetisr (NETISR_IPX);
526 /* XNS IDPCP not implemented yet */
527 if (sp->pp_phase == PHASE_NETWORK) {
528 schednetisr (NETISR_NS);
535 case CISCO_MULTICAST:
537 /* Don't check the control field here (RFC 1547). */
538 if (sp->pp_mode != IFF_CISCO) {
541 SPP_FMT "Cisco packet in PPP mode "
542 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
544 h->address, h->control, ntohs(h->protocol));
547 switch (ntohs (h->protocol)) {
551 case CISCO_KEEPALIVE:
552 sppp_cisco_input ((struct sppp*) ifp, m);
557 schednetisr (NETISR_IP);
563 schednetisr (NETISR_IPV6);
569 schednetisr (NETISR_IPX);
575 schednetisr (NETISR_NS);
581 default: /* Invalid PPP packet. */
585 SPP_FMT "invalid input packet "
586 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
588 h->address, h->control, ntohs(h->protocol));
592 if (! (ifp->if_flags & IFF_UP) || ! inq)
597 if (IF_QFULL (inq)) {
598 /* Queue overflow. */
602 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
611 * Enqueue transmit packet.
614 sppp_output(struct ifnet *ifp, struct mbuf *m,
615 struct sockaddr *dst, struct rtentry *rt)
617 struct sppp *sp = (struct sppp*) ifp;
618 struct ppp_header *h;
621 int debug = ifp->if_flags & IFF_DEBUG;
625 if ((ifp->if_flags & IFF_UP) == 0 ||
626 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
632 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
634 * Interface is not yet running, but auto-dial. Need
635 * to start LCP for it.
637 ifp->if_flags |= IFF_RUNNING;
645 if (dst->sa_family == AF_INET) {
646 /* XXX Check mbuf length here? */
647 struct ip *ip = mtod (m, struct ip*);
648 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
651 * When using dynamic local IP address assignment by using
652 * 0.0.0.0 as a local address, the first TCP session will
653 * not connect because the local TCP checksum is computed
654 * using 0.0.0.0 which will later become our real IP address
655 * so the TCP checksum computed at the remote end will
656 * become invalid. So we
657 * - don't let packets with src ip addr 0 thru
658 * - we flag TCP packets with src ip 0 as an error
661 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
665 if(ip->ip_p == IPPROTO_TCP)
666 return(EADDRNOTAVAIL);
672 * Put low delay, telnet, rlogin and ftp control packets
673 * in front of the queue.
675 if (IF_QFULL (&sp->pp_fastq))
677 else if (ip->ip_tos & IPTOS_LOWDELAY)
679 else if (m->m_len < sizeof *ip + sizeof *tcp)
681 else if (ip->ip_p != IPPROTO_TCP)
683 else if (INTERACTIVE (ntohs (tcp->th_sport)))
685 else if (INTERACTIVE (ntohs (tcp->th_dport)))
691 * Prepend general data packet PPP header. For now, IP only.
693 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
696 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
703 * May want to check size of packet
704 * (albeit due to the implementation it's always enough)
706 h = mtod (m, struct ppp_header*);
707 if (sp->pp_mode == IFF_CISCO) {
708 h->address = CISCO_UNICAST; /* unicast address */
711 h->address = PPP_ALLSTATIONS; /* broadcast address */
712 h->control = PPP_UI; /* Unnumbered Info */
715 switch (dst->sa_family) {
717 case AF_INET: /* Internet Protocol */
718 if (sp->pp_mode == IFF_CISCO)
719 h->protocol = htons (ETHERTYPE_IP);
722 * Don't choke with an ENETDOWN early. It's
723 * possible that we just started dialing out,
724 * so don't drop the packet immediately. If
725 * we notice that we run out of buffer space
726 * below, we will however remember that we are
727 * not ready to carry IP packets, and return
728 * ENETDOWN, as opposed to ENOBUFS.
730 h->protocol = htons(PPP_IP);
731 if (sp->state[IDX_IPCP] != STATE_OPENED)
737 case AF_INET6: /* Internet Protocol */
738 if (sp->pp_mode == IFF_CISCO)
739 h->protocol = htons (ETHERTYPE_IPV6);
746 case AF_NS: /* Xerox NS Protocol */
747 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
748 ETHERTYPE_NS : PPP_XNS);
752 case AF_IPX: /* Novell IPX Protocol */
753 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
754 ETHERTYPE_IPX : PPP_IPX);
762 return (EAFNOSUPPORT);
766 * Queue message on interface, and start output if interface
769 if (IF_QFULL (ifq)) {
770 IF_DROP (&ifp->if_snd);
774 return (rv? rv: ENOBUFS);
777 if (! (ifp->if_flags & IFF_OACTIVE))
778 (*ifp->if_start) (ifp);
781 * Count output packets and bytes.
782 * The packet length includes header, FCS and 1 flag,
783 * according to RFC 1333.
785 ifp->if_obytes += m->m_pkthdr.len + 3;
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_fastq.ifq_maxlen = 32;
811 sp->pp_cpq.ifq_maxlen = 20;
816 sp->pp_phase = PHASE_DEAD;
818 sp->pp_down = lcp.Down;
827 sppp_detach(struct ifnet *ifp)
829 struct sppp **q, *p, *sp = (struct sppp*) ifp;
832 /* Remove the entry from the keepalive list. */
833 for (q = &spppq; (p = *q); q = &p->pp_next)
839 /* Stop keepalive handler. */
841 UNTIMEOUT(sppp_keepalive, 0, keepalive_ch);
843 for (i = 0; i < IDX_COUNT; i++)
844 UNTIMEOUT((cps[i])->TO, (void *)sp, sp->ch[i]);
845 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
849 * Flush the interface output queue.
852 sppp_flush(struct ifnet *ifp)
854 struct sppp *sp = (struct sppp*) ifp;
856 sppp_qflush (&sp->pp_if.if_snd);
857 sppp_qflush (&sp->pp_fastq);
858 sppp_qflush (&sp->pp_cpq);
862 * Check if the output queue is empty.
865 sppp_isempty(struct ifnet *ifp)
867 struct sppp *sp = (struct sppp*) ifp;
871 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
872 !sp->pp_if.if_snd.ifq_head;
878 * Get next packet to send.
881 sppp_dequeue(struct ifnet *ifp)
883 struct sppp *sp = (struct sppp*) ifp;
889 * Process only the control protocol queue until we have at
890 * least one NCP open.
892 * Do always serve all three queues in Cisco mode.
894 IF_DEQUEUE(&sp->pp_cpq, m);
896 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO)) {
897 IF_DEQUEUE(&sp->pp_fastq, m);
899 IF_DEQUEUE (&sp->pp_if.if_snd, m);
906 * Pick the next packet, do not remove it from the queue.
909 sppp_pick(struct ifnet *ifp)
911 struct sppp *sp = (struct sppp*)ifp;
917 m = sp->pp_cpq.ifq_head;
919 (sp->pp_phase == PHASE_NETWORK || sp->pp_mode == IFF_CISCO))
920 if ((m = sp->pp_fastq.ifq_head) == NULL)
921 m = sp->pp_if.if_snd.ifq_head;
927 * Process an ioctl request. Called on low priority level.
930 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
932 struct ifreq *ifr = (struct ifreq*) data;
933 struct sppp *sp = (struct sppp*) ifp;
934 int s, rv, going_up, going_down, newmode;
945 /* fall through... */
948 going_up = ifp->if_flags & IFF_UP &&
949 (ifp->if_flags & IFF_RUNNING) == 0;
950 going_down = (ifp->if_flags & IFF_UP) == 0 &&
951 ifp->if_flags & IFF_RUNNING;
953 newmode = ifp->if_flags & IFF_PASSIVE;
955 newmode = ifp->if_flags & IFF_AUTO;
957 newmode = ifp->if_flags & IFF_CISCO;
958 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
959 ifp->if_flags |= newmode;
961 if (newmode != sp->pp_mode) {
964 going_up = ifp->if_flags & IFF_RUNNING;
968 if (sp->pp_mode != IFF_CISCO)
973 ifp->if_flags &= ~IFF_RUNNING;
974 sp->pp_mode = newmode;
978 if (sp->pp_mode != IFF_CISCO)
980 sp->pp_mode = newmode;
981 if (sp->pp_mode == 0) {
982 ifp->if_flags |= IFF_RUNNING;
985 if (sp->pp_mode == IFF_CISCO) {
988 ifp->if_flags |= IFF_RUNNING;
996 #define ifr_mtu ifr_metric
999 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
1001 ifp->if_mtu = ifr->ifr_mtu;
1006 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1008 ifp->if_mtu = *(short*)data;
1013 ifr->ifr_mtu = ifp->if_mtu;
1018 *(short*)data = ifp->if_mtu;
1025 case SIOCGIFGENERIC:
1026 case SIOCSIFGENERIC:
1027 rv = sppp_params(sp, cmd, data);
1039 * Cisco framing implementation.
1043 * Handle incoming Cisco keepalive protocol packets.
1046 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1049 struct cisco_packet *h;
1052 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1055 SPP_FMT "cisco invalid packet length: %d bytes\n",
1056 SPP_ARGS(ifp), m->m_pkthdr.len);
1059 h = mtod (m, struct cisco_packet*);
1062 SPP_FMT "cisco input: %d bytes "
1063 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1064 SPP_ARGS(ifp), m->m_pkthdr.len,
1065 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1066 (u_int)h->time0, (u_int)h->time1);
1067 switch (ntohl (h->type)) {
1070 addlog(SPP_FMT "cisco unknown packet type: 0x%lx\n",
1071 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1073 case CISCO_ADDR_REPLY:
1074 /* Reply on address request, ignore */
1076 case CISCO_KEEPALIVE_REQ:
1077 sp->pp_alivecnt = 0;
1078 sp->pp_rseq = ntohl (h->par1);
1079 if (sp->pp_seq == sp->pp_rseq) {
1080 /* Local and remote sequence numbers are equal.
1081 * Probably, the line is in loopback mode. */
1082 if (sp->pp_loopcnt >= MAXALIVECNT) {
1083 printf (SPP_FMT "loopback\n",
1086 if (ifp->if_flags & IFF_UP) {
1088 sppp_qflush (&sp->pp_cpq);
1093 /* Generate new local sequence number */
1094 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1095 sp->pp_seq = random();
1097 sp->pp_seq ^= time.tv_sec ^ time.tv_usec;
1102 if (! (ifp->if_flags & IFF_UP) &&
1103 (ifp->if_flags & IFF_RUNNING)) {
1105 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1108 case CISCO_ADDR_REQ:
1109 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1111 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1117 * Send Cisco keepalive packet.
1120 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1123 struct ppp_header *h;
1124 struct cisco_packet *ch;
1126 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1129 u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
1132 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1133 getmicrouptime(&tv);
1136 MGETHDR (m, M_DONTWAIT, MT_DATA);
1139 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1140 m->m_pkthdr.rcvif = 0;
1142 h = mtod (m, struct ppp_header*);
1143 h->address = CISCO_MULTICAST;
1145 h->protocol = htons (CISCO_KEEPALIVE);
1147 ch = (struct cisco_packet*) (h + 1);
1148 ch->type = htonl (type);
1149 ch->par1 = htonl (par1);
1150 ch->par2 = htonl (par2);
1153 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1154 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1155 ch->time1 = htons ((u_short) tv.tv_sec);
1157 ch->time0 = htons ((u_short) (t >> 16));
1158 ch->time1 = htons ((u_short) t);
1163 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1164 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1165 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1167 if (IF_QFULL (&sp->pp_cpq)) {
1168 IF_DROP (&sp->pp_fastq);
1169 IF_DROP (&ifp->if_snd);
1172 IF_ENQUEUE (&sp->pp_cpq, m);
1173 if (! (ifp->if_flags & IFF_OACTIVE))
1174 (*ifp->if_start) (ifp);
1175 ifp->if_obytes += m->m_pkthdr.len + 3;
1179 * PPP protocol implementation.
1183 * Send PPP control protocol packet.
1186 sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
1187 u_char ident, u_short len, void *data)
1190 struct ppp_header *h;
1191 struct lcp_header *lh;
1194 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN)
1195 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN;
1196 MGETHDR (m, M_DONTWAIT, MT_DATA);
1199 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
1200 m->m_pkthdr.rcvif = 0;
1202 h = mtod (m, struct ppp_header*);
1203 h->address = PPP_ALLSTATIONS; /* broadcast address */
1204 h->control = PPP_UI; /* Unnumbered Info */
1205 h->protocol = htons (proto); /* Link Control Protocol */
1207 lh = (struct lcp_header*) (h + 1);
1210 lh->len = htons (LCP_HEADER_LEN + len);
1212 bcopy (data, lh+1, len);
1215 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
1217 sppp_proto_name(proto),
1218 sppp_cp_type_name (lh->type), lh->ident,
1220 sppp_print_bytes ((u_char*) (lh+1), len);
1223 if (IF_QFULL (&sp->pp_cpq)) {
1224 IF_DROP (&sp->pp_fastq);
1225 IF_DROP (&ifp->if_snd);
1229 IF_ENQUEUE (&sp->pp_cpq, m);
1230 if (! (ifp->if_flags & IFF_OACTIVE))
1231 (*ifp->if_start) (ifp);
1232 ifp->if_obytes += m->m_pkthdr.len + 3;
1236 * Handle incoming PPP control protocol packets.
1239 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1242 struct lcp_header *h;
1243 int len = m->m_pkthdr.len;
1250 SPP_FMT "%s invalid packet length: %d bytes\n",
1251 SPP_ARGS(ifp), cp->name, len);
1254 h = mtod (m, struct lcp_header*);
1257 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1258 SPP_ARGS(ifp), cp->name,
1259 sppp_state_name(sp->state[cp->protoidx]),
1260 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1261 sppp_print_bytes ((u_char*) (h+1), len-4);
1264 if (len > ntohs (h->len))
1265 len = ntohs (h->len);
1266 p = (u_char *)(h + 1);
1271 addlog(SPP_FMT "%s invalid conf-req length %d\n",
1272 SPP_ARGS(ifp), cp->name,
1277 /* handle states where RCR doesn't get a SCA/SCN */
1278 switch (sp->state[cp->protoidx]) {
1280 case STATE_STOPPING:
1283 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1287 rv = (cp->RCR)(sp, h, len);
1288 switch (sp->state[cp->protoidx]) {
1292 /* fall through... */
1293 case STATE_ACK_SENT:
1294 case STATE_REQ_SENT:
1295 sppp_cp_change_state(cp, sp, rv?
1296 STATE_ACK_SENT: STATE_REQ_SENT);
1299 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1301 sppp_cp_change_state(cp, sp, rv?
1302 STATE_ACK_SENT: STATE_REQ_SENT);
1304 case STATE_ACK_RCVD:
1306 sppp_cp_change_state(cp, sp, STATE_OPENED);
1308 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1313 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1316 printf(SPP_FMT "%s illegal %s in state %s\n",
1317 SPP_ARGS(ifp), cp->name,
1318 sppp_cp_type_name(h->type),
1319 sppp_state_name(sp->state[cp->protoidx]));
1324 if (h->ident != sp->confid[cp->protoidx]) {
1326 addlog(SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1327 SPP_ARGS(ifp), cp->name,
1328 h->ident, sp->confid[cp->protoidx]);
1332 switch (sp->state[cp->protoidx]) {
1335 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1338 case STATE_STOPPING:
1340 case STATE_REQ_SENT:
1341 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1342 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1347 case STATE_ACK_RCVD:
1349 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1351 case STATE_ACK_SENT:
1352 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1353 sppp_cp_change_state(cp, sp, STATE_OPENED);
1355 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1356 SPP_ARGS(ifp), cp->name);
1360 printf(SPP_FMT "%s illegal %s in state %s\n",
1361 SPP_ARGS(ifp), cp->name,
1362 sppp_cp_type_name(h->type),
1363 sppp_state_name(sp->state[cp->protoidx]));
1369 if (h->ident != sp->confid[cp->protoidx]) {
1371 addlog(SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1372 SPP_ARGS(ifp), cp->name,
1373 h->ident, sp->confid[cp->protoidx]);
1377 if (h->type == CONF_NAK)
1378 (cp->RCN_nak)(sp, h, len);
1380 (cp->RCN_rej)(sp, h, len);
1382 switch (sp->state[cp->protoidx]) {
1385 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1387 case STATE_REQ_SENT:
1388 case STATE_ACK_SENT:
1389 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1395 case STATE_ACK_RCVD:
1396 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1400 case STATE_STOPPING:
1403 printf(SPP_FMT "%s illegal %s in state %s\n",
1404 SPP_ARGS(ifp), cp->name,
1405 sppp_cp_type_name(h->type),
1406 sppp_state_name(sp->state[cp->protoidx]));
1412 switch (sp->state[cp->protoidx]) {
1413 case STATE_ACK_RCVD:
1414 case STATE_ACK_SENT:
1415 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1420 case STATE_STOPPING:
1421 case STATE_REQ_SENT:
1423 /* Send Terminate-Ack packet. */
1425 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1426 SPP_ARGS(ifp), cp->name);
1427 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1431 sp->rst_counter[cp->protoidx] = 0;
1432 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1436 printf(SPP_FMT "%s illegal %s in state %s\n",
1437 SPP_ARGS(ifp), cp->name,
1438 sppp_cp_type_name(h->type),
1439 sppp_state_name(sp->state[cp->protoidx]));
1444 switch (sp->state[cp->protoidx]) {
1447 case STATE_REQ_SENT:
1448 case STATE_ACK_SENT:
1451 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1454 case STATE_STOPPING:
1455 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1458 case STATE_ACK_RCVD:
1459 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1464 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1467 printf(SPP_FMT "%s illegal %s in state %s\n",
1468 SPP_ARGS(ifp), cp->name,
1469 sppp_cp_type_name(h->type),
1470 sppp_state_name(sp->state[cp->protoidx]));
1476 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1478 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1479 "danger will robinson\n",
1480 SPP_ARGS(ifp), cp->name,
1481 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1482 switch (sp->state[cp->protoidx]) {
1485 case STATE_REQ_SENT:
1486 case STATE_ACK_SENT:
1488 case STATE_STOPPING:
1491 case STATE_ACK_RCVD:
1492 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1495 printf(SPP_FMT "%s illegal %s in state %s\n",
1496 SPP_ARGS(ifp), cp->name,
1497 sppp_cp_type_name(h->type),
1498 sppp_state_name(sp->state[cp->protoidx]));
1503 if (cp->proto != PPP_LCP)
1505 /* Discard the packet. */
1508 if (cp->proto != PPP_LCP)
1510 if (sp->state[cp->protoidx] != STATE_OPENED) {
1512 addlog(SPP_FMT "lcp echo req but lcp closed\n",
1519 addlog(SPP_FMT "invalid lcp echo request "
1520 "packet length: %d bytes\n",
1521 SPP_ARGS(ifp), len);
1524 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1525 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1526 /* Line loopback mode detected. */
1527 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1529 sppp_qflush (&sp->pp_cpq);
1531 /* Shut down the PPP link. */
1537 *(long*)(h+1) = htonl (sp->lcp.magic);
1539 addlog(SPP_FMT "got lcp echo req, sending echo rep\n",
1541 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1544 if (cp->proto != PPP_LCP)
1546 if (h->ident != sp->lcp.echoid) {
1552 addlog(SPP_FMT "lcp invalid echo reply "
1553 "packet length: %d bytes\n",
1554 SPP_ARGS(ifp), len);
1558 addlog(SPP_FMT "lcp got echo rep\n",
1560 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1561 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1562 sp->pp_alivecnt = 0;
1565 /* Unknown packet type -- send Code-Reject packet. */
1568 addlog(SPP_FMT "%s send code-rej for 0x%x\n",
1569 SPP_ARGS(ifp), cp->name, h->type);
1570 sppp_cp_send(sp, cp->proto, CODE_REJ, ++sp->pp_seq,
1571 m->m_pkthdr.len, h);
1578 * The generic part of all Up/Down/Open/Close/TO event handlers.
1579 * Basically, the state transition handling in the automaton.
1582 sppp_up_event(const struct cp *cp, struct sppp *sp)
1587 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1588 SPP_ARGS(ifp), cp->name,
1589 sppp_state_name(sp->state[cp->protoidx]));
1591 switch (sp->state[cp->protoidx]) {
1593 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1595 case STATE_STARTING:
1596 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1598 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1601 printf(SPP_FMT "%s illegal up in state %s\n",
1602 SPP_ARGS(ifp), cp->name,
1603 sppp_state_name(sp->state[cp->protoidx]));
1608 sppp_down_event(const struct cp *cp, struct sppp *sp)
1613 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1614 SPP_ARGS(ifp), cp->name,
1615 sppp_state_name(sp->state[cp->protoidx]));
1617 switch (sp->state[cp->protoidx]) {
1620 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1623 sppp_cp_change_state(cp, sp, STATE_STARTING);
1626 case STATE_STOPPING:
1627 case STATE_REQ_SENT:
1628 case STATE_ACK_RCVD:
1629 case STATE_ACK_SENT:
1630 sppp_cp_change_state(cp, sp, STATE_STARTING);
1634 sppp_cp_change_state(cp, sp, STATE_STARTING);
1637 printf(SPP_FMT "%s illegal down in state %s\n",
1638 SPP_ARGS(ifp), cp->name,
1639 sppp_state_name(sp->state[cp->protoidx]));
1645 sppp_open_event(const struct cp *cp, struct sppp *sp)
1650 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1651 SPP_ARGS(ifp), cp->name,
1652 sppp_state_name(sp->state[cp->protoidx]));
1654 switch (sp->state[cp->protoidx]) {
1656 sppp_cp_change_state(cp, sp, STATE_STARTING);
1659 case STATE_STARTING:
1662 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1664 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1667 case STATE_STOPPING:
1668 case STATE_REQ_SENT:
1669 case STATE_ACK_RCVD:
1670 case STATE_ACK_SENT:
1674 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1681 sppp_close_event(const struct cp *cp, struct sppp *sp)
1686 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
1687 SPP_ARGS(ifp), cp->name,
1688 sppp_state_name(sp->state[cp->protoidx]));
1690 switch (sp->state[cp->protoidx]) {
1695 case STATE_STARTING:
1696 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1700 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1702 case STATE_STOPPING:
1703 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1708 case STATE_REQ_SENT:
1709 case STATE_ACK_RCVD:
1710 case STATE_ACK_SENT:
1711 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
1712 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq, 0, 0);
1713 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1719 sppp_to_event(const struct cp *cp, struct sppp *sp)
1726 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
1727 SPP_ARGS(ifp), cp->name,
1728 sppp_state_name(sp->state[cp->protoidx]),
1729 sp->rst_counter[cp->protoidx]);
1731 if (--sp->rst_counter[cp->protoidx] < 0)
1733 switch (sp->state[cp->protoidx]) {
1735 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1738 case STATE_STOPPING:
1739 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1742 case STATE_REQ_SENT:
1743 case STATE_ACK_RCVD:
1744 case STATE_ACK_SENT:
1745 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1751 switch (sp->state[cp->protoidx]) {
1753 case STATE_STOPPING:
1754 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq,
1756 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
1757 sp->ch[cp->protoidx]);
1759 case STATE_REQ_SENT:
1760 case STATE_ACK_RCVD:
1762 /* sppp_cp_change_state() will restart the timer */
1763 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1765 case STATE_ACK_SENT:
1767 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
1768 sp->ch[cp->protoidx]);
1776 * Change the state of a control protocol in the state automaton.
1777 * Takes care of starting/stopping the restart timer.
1780 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
1782 sp->state[cp->protoidx] = newstate;
1784 UNTIMEOUT(cp->TO, (void *)sp, sp->ch[cp->protoidx]);
1787 case STATE_STARTING:
1793 case STATE_STOPPING:
1794 case STATE_REQ_SENT:
1795 case STATE_ACK_RCVD:
1796 case STATE_ACK_SENT:
1797 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
1798 sp->ch[cp->protoidx]);
1803 *--------------------------------------------------------------------------*
1805 * The LCP implementation. *
1807 *--------------------------------------------------------------------------*
1810 sppp_lcp_init(struct sppp *sp)
1812 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
1814 sp->state[IDX_LCP] = STATE_INITIAL;
1815 sp->fail_counter[IDX_LCP] = 0;
1817 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
1819 /* Note that these values are relevant for all control protocols */
1820 sp->lcp.timeout = 3 * hz;
1821 sp->lcp.max_terminate = 2;
1822 sp->lcp.max_configure = 10;
1823 sp->lcp.max_failure = 10;
1824 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1825 callout_handle_init(&sp->ch[IDX_LCP]);
1830 sppp_lcp_up(struct sppp *sp)
1835 * If this interface is passive or dial-on-demand, and we are
1836 * still in Initial state, it means we've got an incoming
1837 * call. Activate the interface.
1839 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
1842 SPP_FMT "Up event", SPP_ARGS(ifp));
1843 ifp->if_flags |= IFF_RUNNING;
1844 if (sp->state[IDX_LCP] == STATE_INITIAL) {
1846 addlog("(incoming call)\n");
1847 sp->pp_flags |= PP_CALLIN;
1853 sppp_up_event(&lcp, sp);
1857 sppp_lcp_down(struct sppp *sp)
1861 sppp_down_event(&lcp, sp);
1864 * If this is neither a dial-on-demand nor a passive
1865 * interface, simulate an ``ifconfig down'' action, so the
1866 * administrator can force a redial by another ``ifconfig
1867 * up''. XXX For leased line operation, should we immediately
1868 * try to reopen the connection here?
1870 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
1872 SPP_FMT "Down event, taking interface down.\n",
1878 SPP_FMT "Down event (carrier loss)\n",
1881 sp->pp_flags &= ~PP_CALLIN;
1882 if (sp->state[IDX_LCP] != STATE_INITIAL)
1884 ifp->if_flags &= ~IFF_RUNNING;
1888 sppp_lcp_open(struct sppp *sp)
1891 * If we are authenticator, negotiate LCP_AUTH
1893 if (sp->hisauth.proto != 0)
1894 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
1896 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
1897 sp->pp_flags &= ~PP_NEEDAUTH;
1898 sppp_open_event(&lcp, sp);
1902 sppp_lcp_close(struct sppp *sp)
1904 sppp_close_event(&lcp, sp);
1908 sppp_lcp_TO(void *cookie)
1910 sppp_to_event(&lcp, (struct sppp *)cookie);
1914 * Analyze a configure request. Return true if it was agreeable, and
1915 * caused action sca, false if it has been rejected or nak'ed, and
1916 * caused action scn. (The return value is used to make the state
1917 * transition decision in the state automaton.)
1920 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
1923 u_char *buf, *r, *p;
1930 buf = r = malloc (len, M_TEMP, M_NOWAIT);
1935 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
1938 /* pass 1: check for things that need to be rejected */
1940 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1942 addlog(" %s ", sppp_lcp_opt_name(*p));
1946 /* fall through, both are same length */
1947 case LCP_OPT_ASYNC_MAP:
1948 /* Async control character map. */
1949 if (len >= 6 || p[1] == 6)
1952 addlog("[invalid] ");
1955 /* Maximum receive unit. */
1956 if (len >= 4 && p[1] == 4)
1959 addlog("[invalid] ");
1961 case LCP_OPT_AUTH_PROTO:
1964 addlog("[invalid] ");
1967 authproto = (p[2] << 8) + p[3];
1968 if (authproto == PPP_CHAP && p[1] != 5) {
1970 addlog("[invalid chap len] ");
1973 if (sp->myauth.proto == 0) {
1974 /* we are not configured to do auth */
1976 addlog("[not configured] ");
1980 * Remote want us to authenticate, remember this,
1981 * so we stay in PHASE_AUTHENTICATE after LCP got
1984 sp->pp_flags |= PP_NEEDAUTH;
1987 /* Others not supported. */
1992 /* Add the option to rejected list. */
1999 addlog(" send conf-rej\n");
2000 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2006 * pass 2: check for option values that are unacceptable and
2007 * thus require to be nak'ed.
2010 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2015 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2017 addlog(" %s ", sppp_lcp_opt_name(*p));
2020 /* Magic number -- extract. */
2021 nmagic = (u_long)p[2] << 24 |
2022 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2023 if (nmagic != sp->lcp.magic) {
2025 addlog("0x%lx ", nmagic);
2029 * Local and remote magics equal -- loopback?
2031 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2032 printf (SPP_FMT "loopback\n",
2035 if (ifp->if_flags & IFF_UP) {
2037 sppp_qflush(&sp->pp_cpq);
2043 addlog("[glitch] ");
2046 * We negate our magic here, and NAK it. If
2047 * we see it later in an NAK packet, we
2048 * suggest a new one.
2050 nmagic = ~sp->lcp.magic;
2052 p[2] = nmagic >> 24;
2053 p[3] = nmagic >> 16;
2058 case LCP_OPT_ASYNC_MAP:
2059 /* Async control character map -- check to be zero. */
2060 if (! p[2] && ! p[3] && ! p[4] && ! p[5]) {
2066 addlog("[non-empty] ");
2067 /* suggest a zero one */
2068 p[2] = p[3] = p[4] = p[5] = 0;
2073 * Maximum receive unit. Always agreeable,
2074 * but ignored by now.
2076 sp->lcp.their_mru = p[2] * 256 + p[3];
2078 addlog("%lu ", sp->lcp.their_mru);
2081 case LCP_OPT_AUTH_PROTO:
2082 authproto = (p[2] << 8) + p[3];
2083 if (sp->myauth.proto != authproto) {
2084 /* not agreed, nak */
2086 addlog("[mine %s != his %s] ",
2087 sppp_proto_name(sp->hisauth.proto),
2088 sppp_proto_name(authproto));
2089 p[2] = sp->myauth.proto >> 8;
2090 p[3] = sp->myauth.proto;
2093 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2095 addlog("[chap not MD5] ");
2101 /* Add the option to nak'ed list. */
2107 if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2109 addlog(" max_failure (%d) exceeded, "
2111 sp->lcp.max_failure);
2112 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2115 addlog(" send conf-nak\n");
2116 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2121 addlog(" send conf-ack\n");
2122 sp->fail_counter[IDX_LCP] = 0;
2124 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2125 h->ident, origlen, h+1);
2133 * Analyze the LCP Configure-Reject option list, and adjust our
2137 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2143 buf = malloc (len, M_TEMP, M_NOWAIT);
2148 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2152 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2154 addlog(" %s ", sppp_lcp_opt_name(*p));
2157 /* Magic number -- can't use it, use 0 */
2158 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2163 * Should not be rejected anyway, since we only
2164 * negotiate a MRU if explicitly requested by
2167 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2169 case LCP_OPT_AUTH_PROTO:
2171 * Peer doesn't want to authenticate himself,
2172 * deny unless this is a dialout call, and
2173 * AUTHFLAG_NOCALLOUT is set.
2175 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2176 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2178 addlog("[don't insist on auth "
2180 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2184 addlog("[access denied]\n");
2196 * Analyze the LCP Configure-NAK option list, and adjust our
2200 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2207 buf = malloc (len, M_TEMP, M_NOWAIT);
2212 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2216 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2218 addlog(" %s ", sppp_lcp_opt_name(*p));
2221 /* Magic number -- renegotiate */
2222 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2223 len >= 6 && p[1] == 6) {
2224 magic = (u_long)p[2] << 24 |
2225 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2227 * If the remote magic is our negated one,
2228 * this looks like a loopback problem.
2229 * Suggest a new magic to make sure.
2231 if (magic == ~sp->lcp.magic) {
2233 addlog("magic glitch ");
2234 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2235 sp->lcp.magic = random();
2237 sp->lcp.magic = time.tv_sec + time.tv_usec;
2240 sp->lcp.magic = magic;
2242 addlog("%lu ", magic);
2248 * Peer wants to advise us to negotiate an MRU.
2249 * Agree on it if it's reasonable, or use
2250 * default otherwise.
2252 if (len >= 4 && p[1] == 4) {
2253 u_int mru = p[2] * 256 + p[3];
2256 if (mru < PP_MTU || mru > PP_MAX_MRU)
2259 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2262 case LCP_OPT_AUTH_PROTO:
2264 * Peer doesn't like our authentication method,
2268 addlog("[access denied]\n");
2280 sppp_lcp_tlu(struct sppp *sp)
2287 if (! (ifp->if_flags & IFF_UP) &&
2288 (ifp->if_flags & IFF_RUNNING)) {
2289 /* Coming out of loopback mode. */
2291 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2294 for (i = 0; i < IDX_COUNT; i++)
2295 if ((cps[i])->flags & CP_QUAL)
2298 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2299 (sp->pp_flags & PP_NEEDAUTH) != 0)
2300 sp->pp_phase = PHASE_AUTHENTICATE;
2302 sp->pp_phase = PHASE_NETWORK;
2305 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2306 sppp_phase_name(sp->pp_phase));
2309 * Open all authentication protocols. This is even required
2310 * if we already proceeded to network phase, since it might be
2311 * that remote wants us to authenticate, so we might have to
2312 * send a PAP request. Undesired authentication protocols
2313 * don't do anything when they get an Open event.
2315 for (i = 0; i < IDX_COUNT; i++)
2316 if ((cps[i])->flags & CP_AUTH)
2319 if (sp->pp_phase == PHASE_NETWORK) {
2320 /* Notify all NCPs. */
2321 for (i = 0; i < IDX_COUNT; i++)
2322 if ((cps[i])->flags & CP_NCP)
2326 /* Send Up events to all started protos. */
2327 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2328 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0)
2331 /* notify low-level driver of state change */
2333 sp->pp_chg(sp, (int)sp->pp_phase);
2335 if (sp->pp_phase == PHASE_NETWORK)
2336 /* if no NCP is starting, close down */
2337 sppp_lcp_check_and_close(sp);
2341 sppp_lcp_tld(struct sppp *sp)
2347 sp->pp_phase = PHASE_TERMINATE;
2350 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2351 sppp_phase_name(sp->pp_phase));
2354 * Take upper layers down. We send the Down event first and
2355 * the Close second to prevent the upper layers from sending
2356 * ``a flurry of terminate-request packets'', as the RFC
2359 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2360 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0) {
2362 (cps[i])->Close(sp);
2367 sppp_lcp_tls(struct sppp *sp)
2371 sp->pp_phase = PHASE_ESTABLISH;
2374 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2375 sppp_phase_name(sp->pp_phase));
2377 /* Notify lower layer if desired. */
2385 sppp_lcp_tlf(struct sppp *sp)
2389 sp->pp_phase = PHASE_DEAD;
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_scr(struct sppp *sp)
2404 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2408 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2409 if (! sp->lcp.magic)
2410 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2411 sp->lcp.magic = random();
2413 sp->lcp.magic = time.tv_sec + time.tv_usec;
2415 opt[i++] = LCP_OPT_MAGIC;
2417 opt[i++] = sp->lcp.magic >> 24;
2418 opt[i++] = sp->lcp.magic >> 16;
2419 opt[i++] = sp->lcp.magic >> 8;
2420 opt[i++] = sp->lcp.magic;
2423 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2424 opt[i++] = LCP_OPT_MRU;
2426 opt[i++] = sp->lcp.mru >> 8;
2427 opt[i++] = sp->lcp.mru;
2430 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2431 authproto = sp->hisauth.proto;
2432 opt[i++] = LCP_OPT_AUTH_PROTO;
2433 opt[i++] = authproto == PPP_CHAP? 5: 4;
2434 opt[i++] = authproto >> 8;
2435 opt[i++] = authproto;
2436 if (authproto == PPP_CHAP)
2437 opt[i++] = CHAP_MD5;
2440 sp->confid[IDX_LCP] = ++sp->pp_seq;
2441 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2445 * Check the open NCPs, return true if at least one NCP is open.
2448 sppp_ncp_check(struct sppp *sp)
2452 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2453 if (sp->lcp.protos & mask && (cps[i])->flags & CP_NCP)
2459 * Re-check the open NCPs and see if we should terminate the link.
2460 * Called by the NCPs during their tlf action handling.
2463 sppp_lcp_check_and_close(struct sppp *sp)
2466 if (sp->pp_phase < PHASE_NETWORK)
2467 /* don't bother, we are already going down */
2470 if (sppp_ncp_check(sp))
2476 *--------------------------------------------------------------------------*
2478 * The IPCP implementation. *
2480 *--------------------------------------------------------------------------*
2484 sppp_ipcp_init(struct sppp *sp)
2488 sp->state[IDX_IPCP] = STATE_INITIAL;
2489 sp->fail_counter[IDX_IPCP] = 0;
2490 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2491 callout_handle_init(&sp->ch[IDX_IPCP]);
2496 sppp_ipcp_up(struct sppp *sp)
2498 sppp_up_event(&ipcp, sp);
2502 sppp_ipcp_down(struct sppp *sp)
2504 sppp_down_event(&ipcp, sp);
2508 sppp_ipcp_open(struct sppp *sp)
2511 u_long myaddr, hisaddr;
2513 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN|IPCP_MYADDR_SEEN|IPCP_MYADDR_DYN);
2515 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2517 * If we don't have his address, this probably means our
2518 * interface doesn't want to talk IP at all. (This could
2519 * be the case if somebody wants to speak only IPX, for
2520 * example.) Don't open IPCP in this case.
2522 if (hisaddr == 0L) {
2523 /* XXX this message should go away */
2525 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2532 * I don't have an assigned address, so i need to
2533 * negotiate my address.
2535 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2536 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2538 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2539 sppp_open_event(&ipcp, sp);
2543 sppp_ipcp_close(struct sppp *sp)
2545 sppp_close_event(&ipcp, sp);
2546 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2548 * My address was dynamic, clear it again.
2550 sppp_set_ip_addr(sp, 0L);
2554 sppp_ipcp_TO(void *cookie)
2556 sppp_to_event(&ipcp, (struct sppp *)cookie);
2560 * Analyze a configure request. Return true if it was agreeable, and
2561 * caused action sca, false if it has been rejected or nak'ed, and
2562 * caused action scn. (The return value is used to make the state
2563 * transition decision in the state automaton.)
2566 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2568 u_char *buf, *r, *p;
2569 struct ifnet *ifp = &sp->pp_if;
2570 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2571 u_long hisaddr, desiredaddr;
2577 * Make sure to allocate a buf that can at least hold a
2578 * conf-nak with an `address' option. We might need it below.
2580 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2584 /* pass 1: see if we can recognize them */
2586 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2589 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2591 addlog(" %s ", sppp_ipcp_opt_name(*p));
2593 case IPCP_OPT_ADDRESS:
2594 if (len >= 6 && p[1] == 6) {
2595 /* correctly formed address option */
2599 addlog("[invalid] ");
2602 /* Others not supported. */
2607 /* Add the option to rejected list. */
2614 addlog(" send conf-rej\n");
2615 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2620 /* pass 2: parse option values */
2621 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
2623 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
2627 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2629 addlog(" %s ", sppp_ipcp_opt_name(*p));
2631 case IPCP_OPT_ADDRESS:
2632 /* This is the address he wants in his end */
2633 desiredaddr = p[2] << 24 | p[3] << 16 |
2635 if (desiredaddr == hisaddr ||
2636 (hisaddr == 1 && desiredaddr != 0)) {
2638 * Peer's address is same as our value,
2639 * or we have set it to 0.0.0.1 to
2640 * indicate that we do not really care,
2641 * this is agreeable. Gonna conf-ack
2646 sppp_dotted_quad(hisaddr));
2647 /* record that we've seen it already */
2648 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
2652 * The address wasn't agreeable. This is either
2653 * he sent us 0.0.0.0, asking to assign him an
2654 * address, or he send us another address not
2655 * matching our value. Either case, we gonna
2656 * conf-nak it with our value.
2657 * XXX: we should "rej" if hisaddr == 0
2660 if (desiredaddr == 0)
2661 addlog("[addr requested] ");
2663 addlog("%s [not agreed] ",
2664 sppp_dotted_quad(desiredaddr));
2667 p[2] = hisaddr >> 24;
2668 p[3] = hisaddr >> 16;
2669 p[4] = hisaddr >> 8;
2673 /* Add the option to nak'ed list. */
2680 * If we are about to conf-ack the request, but haven't seen
2681 * his address so far, gonna conf-nak it instead, with the
2682 * `address' option present and our idea of his address being
2683 * filled in there, to request negotiation of both addresses.
2685 * XXX This can result in an endless req - nak loop if peer
2686 * doesn't want to send us his address. Q: What should we do
2687 * about it? XXX A: implement the max-failure counter.
2689 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
2690 buf[0] = IPCP_OPT_ADDRESS;
2692 buf[2] = hisaddr >> 24;
2693 buf[3] = hisaddr >> 16;
2694 buf[4] = hisaddr >> 8;
2698 addlog("still need hisaddr ");
2703 addlog(" send conf-nak\n");
2704 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
2707 addlog(" send conf-ack\n");
2708 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
2709 h->ident, origlen, h+1);
2717 * Analyze the IPCP Configure-Reject option list, and adjust our
2721 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2724 struct ifnet *ifp = &sp->pp_if;
2725 int debug = ifp->if_flags & IFF_DEBUG;
2728 buf = malloc (len, M_TEMP, M_NOWAIT);
2733 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
2737 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2739 addlog(" %s ", sppp_ipcp_opt_name(*p));
2741 case IPCP_OPT_ADDRESS:
2743 * Peer doesn't grok address option. This is
2744 * bad. XXX Should we better give up here?
2745 * XXX We could try old "addresses" option...
2747 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
2758 * Analyze the IPCP Configure-NAK option list, and adjust our
2762 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2765 struct ifnet *ifp = &sp->pp_if;
2766 int debug = ifp->if_flags & IFF_DEBUG;
2770 buf = malloc (len, M_TEMP, M_NOWAIT);
2775 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
2779 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2781 addlog(" %s ", sppp_ipcp_opt_name(*p));
2783 case IPCP_OPT_ADDRESS:
2785 * Peer doesn't like our local IP address. See
2786 * if we can do something for him. We'll drop
2787 * him our address then.
2789 if (len >= 6 && p[1] == 6) {
2790 wantaddr = p[2] << 24 | p[3] << 16 |
2792 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2794 addlog("[wantaddr %s] ",
2795 sppp_dotted_quad(wantaddr));
2797 * When doing dynamic address assignment,
2798 * we accept his offer. Otherwise, we
2799 * ignore it and thus continue to negotiate
2800 * our already existing value.
2801 * XXX: Bogus, if he said no once, he'll
2802 * just say no again, might as well die.
2804 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
2805 sppp_set_ip_addr(sp, wantaddr);
2808 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2821 sppp_ipcp_tlu(struct sppp *sp)
2823 /* we are up - notify isdn daemon */
2829 sppp_ipcp_tld(struct sppp *sp)
2834 sppp_ipcp_tls(struct sppp *sp)
2836 /* indicate to LCP that it must stay alive */
2837 sp->lcp.protos |= (1 << IDX_IPCP);
2841 sppp_ipcp_tlf(struct sppp *sp)
2843 /* we no longer need LCP */
2844 sp->lcp.protos &= ~(1 << IDX_IPCP);
2845 sppp_lcp_check_and_close(sp);
2849 sppp_ipcp_scr(struct sppp *sp)
2851 char opt[6 /* compression */ + 6 /* address */];
2855 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
2856 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
2857 opt[i++] = IPCP_OPT_ADDRESS;
2859 opt[i++] = ouraddr >> 24;
2860 opt[i++] = ouraddr >> 16;
2861 opt[i++] = ouraddr >> 8;
2865 sp->confid[IDX_IPCP] = ++sp->pp_seq;
2866 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
2871 *--------------------------------------------------------------------------*
2873 * The CHAP implementation. *
2875 *--------------------------------------------------------------------------*
2879 * The authentication protocols don't employ a full-fledged state machine as
2880 * the control protocols do, since they do have Open and Close events, but
2881 * not Up and Down, nor are they explicitly terminated. Also, use of the
2882 * authentication protocols may be different in both directions (this makes
2883 * sense, think of a machine that never accepts incoming calls but only
2884 * calls out, it doesn't require the called party to authenticate itself).
2886 * Our state machine for the local authentication protocol (we are requesting
2887 * the peer to authenticate) looks like:
2890 * +--------------------------------------------+
2892 * +--------+ Close +---------+ RCA+
2893 * | |<----------------------------------| |------+
2894 * +--->| Closed | TO* | Opened | sca |
2895 * | | |-----+ +-------| |<-----+
2896 * | +--------+ irc | | +---------+
2902 * | | +------->+ | |
2904 * | +--------+ V | |
2905 * | | |<----+<--------------------+ |
2911 * +------+ +------------------------------------------+
2912 * scn,tld sca,irc,ict,tlu
2917 * Open: LCP reached authentication phase
2918 * Close: LCP reached terminate phase
2920 * RCA+: received reply (pap-req, chap-response), acceptable
2921 * RCN: received reply (pap-req, chap-response), not acceptable
2922 * TO+: timeout with restart counter >= 0
2923 * TO-: timeout with restart counter < 0
2924 * TO*: reschedule timeout for CHAP
2926 * scr: send request packet (none for PAP, chap-challenge)
2927 * sca: send ack packet (pap-ack, chap-success)
2928 * scn: send nak packet (pap-nak, chap-failure)
2929 * ict: initialize re-challenge timer (CHAP only)
2931 * tlu: this-layer-up, LCP reaches network phase
2932 * tld: this-layer-down, LCP enters terminate phase
2934 * Note that in CHAP mode, after sending a new challenge, while the state
2935 * automaton falls back into Req-Sent state, it doesn't signal a tld
2936 * event to LCP, so LCP remains in network phase. Only after not getting
2937 * any response (or after getting an unacceptable response), CHAP closes,
2938 * causing LCP to enter terminate phase.
2940 * With PAP, there is no initial request that can be sent. The peer is
2941 * expected to send one based on the successful negotiation of PAP as
2942 * the authentication protocol during the LCP option negotiation.
2944 * Incoming authentication protocol requests (remote requests
2945 * authentication, we are peer) don't employ a state machine at all,
2946 * they are simply answered. Some peers [Ascend P50 firmware rev
2947 * 4.50] react allergically when sending IPCP requests while they are
2948 * still in authentication phase (thereby violating the standard that
2949 * demands that these NCP packets are to be discarded), so we keep
2950 * track of the peer demanding us to authenticate, and only proceed to
2951 * phase network once we've seen a positive acknowledge for the
2956 * Handle incoming CHAP packets.
2959 sppp_chap_input(struct sppp *sp, struct mbuf *m)
2962 struct lcp_header *h;
2964 u_char *value, *name, digest[AUTHKEYLEN], dsize;
2965 int value_len, name_len;
2968 len = m->m_pkthdr.len;
2972 SPP_FMT "chap invalid packet length: %d bytes\n",
2973 SPP_ARGS(ifp), len);
2976 h = mtod (m, struct lcp_header*);
2977 if (len > ntohs (h->len))
2978 len = ntohs (h->len);
2981 /* challenge, failure and success are his authproto */
2982 case CHAP_CHALLENGE:
2983 value = 1 + (u_char*)(h+1);
2984 value_len = value[-1];
2985 name = value + value_len;
2986 name_len = len - value_len - 5;
2990 SPP_FMT "chap corrupted challenge "
2991 "<%s id=0x%x len=%d",
2993 sppp_auth_type_name(PPP_CHAP, h->type),
2994 h->ident, ntohs(h->len));
2995 sppp_print_bytes((u_char*) (h+1), len-4);
3003 SPP_FMT "chap input <%s id=0x%x len=%d name=",
3005 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
3007 sppp_print_string((char*) name, name_len);
3008 addlog(" value-size=%d value=", value_len);
3009 sppp_print_bytes(value, value_len);
3013 /* Compute reply value. */
3015 MD5Update(&ctx, &h->ident, 1);
3016 MD5Update(&ctx, sp->myauth.secret,
3017 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
3018 MD5Update(&ctx, value, value_len);
3019 MD5Final(digest, &ctx);
3020 dsize = sizeof digest;
3022 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
3023 sizeof dsize, (const char *)&dsize,
3024 sizeof digest, digest,
3025 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3032 log(LOG_DEBUG, SPP_FMT "chap success",
3036 sppp_print_string((char*)(h + 1), len - 4);
3041 sp->pp_flags &= ~PP_NEEDAUTH;
3042 if (sp->myauth.proto == PPP_CHAP &&
3043 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
3044 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
3046 * We are authenticator for CHAP but didn't
3047 * complete yet. Leave it to tlu to proceed
3054 sppp_phase_network(sp);
3059 log(LOG_INFO, SPP_FMT "chap failure",
3063 sppp_print_string((char*)(h + 1), len - 4);
3067 log(LOG_INFO, SPP_FMT "chap failure\n",
3069 /* await LCP shutdown by authenticator */
3072 /* response is my authproto */
3074 value = 1 + (u_char*)(h+1);
3075 value_len = value[-1];
3076 name = value + value_len;
3077 name_len = len - value_len - 5;
3081 SPP_FMT "chap corrupted response "
3082 "<%s id=0x%x len=%d",
3084 sppp_auth_type_name(PPP_CHAP, h->type),
3085 h->ident, ntohs(h->len));
3086 sppp_print_bytes((u_char*)(h+1), len-4);
3091 if (h->ident != sp->confid[IDX_CHAP]) {
3094 SPP_FMT "chap dropping response for old ID "
3095 "(got %d, expected %d)\n",
3097 h->ident, sp->confid[IDX_CHAP]);
3100 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
3101 || bcmp(name, sp->hisauth.name, name_len) != 0) {
3102 log(LOG_INFO, SPP_FMT "chap response, his name ",
3104 sppp_print_string(name, name_len);
3105 addlog(" != expected ");
3106 sppp_print_string(sp->hisauth.name,
3107 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
3111 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
3112 "<%s id=0x%x len=%d name=",
3114 sppp_state_name(sp->state[IDX_CHAP]),
3115 sppp_auth_type_name(PPP_CHAP, h->type),
3116 h->ident, ntohs (h->len));
3117 sppp_print_string((char*)name, name_len);
3118 addlog(" value-size=%d value=", value_len);
3119 sppp_print_bytes(value, value_len);
3122 if (value_len != AUTHKEYLEN) {
3125 SPP_FMT "chap bad hash value length: "
3126 "%d bytes, should be %d\n",
3127 SPP_ARGS(ifp), value_len,
3133 MD5Update(&ctx, &h->ident, 1);
3134 MD5Update(&ctx, sp->hisauth.secret,
3135 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
3136 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
3137 MD5Final(digest, &ctx);
3139 #define FAILMSG "Failed..."
3140 #define SUCCMSG "Welcome!"
3142 if (value_len != sizeof digest ||
3143 bcmp(digest, value, value_len) != 0) {
3144 /* action scn, tld */
3145 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
3146 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3151 /* action sca, perhaps tlu */
3152 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
3153 sp->state[IDX_CHAP] == STATE_OPENED)
3154 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
3155 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3157 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
3158 sppp_cp_change_state(&chap, sp, STATE_OPENED);
3164 /* Unknown CHAP packet type -- ignore. */
3166 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
3167 "<0x%x id=0x%xh len=%d",
3169 sppp_state_name(sp->state[IDX_CHAP]),
3170 h->type, h->ident, ntohs(h->len));
3171 sppp_print_bytes((u_char*)(h+1), len-4);
3180 sppp_chap_init(struct sppp *sp)
3182 /* Chap doesn't have STATE_INITIAL at all. */
3183 sp->state[IDX_CHAP] = STATE_CLOSED;
3184 sp->fail_counter[IDX_CHAP] = 0;
3185 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3186 callout_handle_init(&sp->ch[IDX_CHAP]);
3191 sppp_chap_open(struct sppp *sp)
3193 if (sp->myauth.proto == PPP_CHAP &&
3194 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3195 /* we are authenticator for CHAP, start it */
3197 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3198 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3200 /* nothing to be done if we are peer, await a challenge */
3204 sppp_chap_close(struct sppp *sp)
3206 if (sp->state[IDX_CHAP] != STATE_CLOSED)
3207 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3211 sppp_chap_TO(void *cookie)
3213 struct sppp *sp = (struct sppp *)cookie;
3219 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
3221 sppp_state_name(sp->state[IDX_CHAP]),
3222 sp->rst_counter[IDX_CHAP]);
3224 if (--sp->rst_counter[IDX_CHAP] < 0)
3226 switch (sp->state[IDX_CHAP]) {
3227 case STATE_REQ_SENT:
3229 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3233 /* TO+ (or TO*) event */
3234 switch (sp->state[IDX_CHAP]) {
3237 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3239 case STATE_REQ_SENT:
3241 /* sppp_cp_change_state() will restart the timer */
3242 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3250 sppp_chap_tlu(struct sppp *sp)
3256 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3259 * Some broken CHAP implementations (Conware CoNet, firmware
3260 * 4.0.?) don't want to re-authenticate their CHAP once the
3261 * initial challenge-response exchange has taken place.
3262 * Provide for an option to avoid rechallenges.
3264 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
3266 * Compute the re-challenge timeout. This will yield
3267 * a number between 300 and 810 seconds.
3269 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
3270 TIMEOUT(chap.TO, (void *)sp, i * hz, sp->ch[IDX_CHAP]);
3275 SPP_FMT "chap %s, ",
3277 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
3278 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
3279 addlog("next re-challenge in %d seconds\n", i);
3281 addlog("re-challenging supressed\n");
3285 /* indicate to LCP that we need to be closed down */
3286 sp->lcp.protos |= (1 << IDX_CHAP);
3288 if (sp->pp_flags & PP_NEEDAUTH) {
3290 * Remote is authenticator, but his auth proto didn't
3291 * complete yet. Defer the transition to network
3300 * If we are already in phase network, we are done here. This
3301 * is the case if this is a dummy tlu event after a re-challenge.
3303 if (sp->pp_phase != PHASE_NETWORK)
3304 sppp_phase_network(sp);
3308 sppp_chap_tld(struct sppp *sp)
3313 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
3314 UNTIMEOUT(chap.TO, (void *)sp, sp->ch[IDX_CHAP]);
3315 sp->lcp.protos &= ~(1 << IDX_CHAP);
3321 sppp_chap_scr(struct sppp *sp)
3326 /* Compute random challenge. */
3327 ch = (u_long *)sp->myauth.challenge;
3328 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3329 read_random(&seed, sizeof seed);
3334 seed = tv.tv_sec ^ tv.tv_usec;
3337 ch[0] = seed ^ random();
3338 ch[1] = seed ^ random();
3339 ch[2] = seed ^ random();
3340 ch[3] = seed ^ random();
3343 sp->confid[IDX_CHAP] = ++sp->pp_seq;
3345 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
3346 sizeof clen, (const char *)&clen,
3347 (size_t)AUTHKEYLEN, sp->myauth.challenge,
3348 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3353 *--------------------------------------------------------------------------*
3355 * The PAP implementation. *
3357 *--------------------------------------------------------------------------*
3360 * For PAP, we need to keep a little state also if we are the peer, not the
3361 * authenticator. This is since we don't get a request to authenticate, but
3362 * have to repeatedly authenticate ourself until we got a response (or the
3363 * retry counter is expired).
3367 * Handle incoming PAP packets. */
3369 sppp_pap_input(struct sppp *sp, struct mbuf *m)
3372 struct lcp_header *h;
3374 u_char *name, *passwd, mlen;
3375 int name_len, passwd_len;
3377 len = m->m_pkthdr.len;
3381 SPP_FMT "pap invalid packet length: %d bytes\n",
3382 SPP_ARGS(ifp), len);
3385 h = mtod (m, struct lcp_header*);
3386 if (len > ntohs (h->len))
3387 len = ntohs (h->len);
3389 /* PAP request is my authproto */
3391 name = 1 + (u_char*)(h+1);
3392 name_len = name[-1];
3393 passwd = name + name_len + 1;
3394 if (name_len > len - 6 ||
3395 (passwd_len = passwd[-1]) > len - 6 - name_len) {
3397 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
3398 "<%s id=0x%x len=%d",
3400 sppp_auth_type_name(PPP_PAP, h->type),
3401 h->ident, ntohs(h->len));
3402 sppp_print_bytes((u_char*)(h+1), len-4);
3408 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
3409 "<%s id=0x%x len=%d name=",
3411 sppp_state_name(sp->state[IDX_PAP]),
3412 sppp_auth_type_name(PPP_PAP, h->type),
3413 h->ident, ntohs(h->len));
3414 sppp_print_string((char*)name, name_len);
3416 sppp_print_string((char*)passwd, passwd_len);
3419 if (name_len > AUTHNAMELEN ||
3420 passwd_len > AUTHKEYLEN ||
3421 bcmp(name, sp->hisauth.name, name_len) != 0 ||
3422 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
3423 /* action scn, tld */
3424 mlen = sizeof(FAILMSG) - 1;
3425 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
3426 sizeof mlen, (const char *)&mlen,
3427 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3432 /* action sca, perhaps tlu */
3433 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
3434 sp->state[IDX_PAP] == STATE_OPENED) {
3435 mlen = sizeof(SUCCMSG) - 1;
3436 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
3437 sizeof mlen, (const char *)&mlen,
3438 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3441 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
3442 sppp_cp_change_state(&pap, sp, STATE_OPENED);
3447 /* ack and nak are his authproto */
3449 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3451 log(LOG_DEBUG, SPP_FMT "pap success",
3453 name_len = *((char *)h);
3454 if (len > 5 && name_len) {
3456 sppp_print_string((char*)(h+1), name_len);
3461 sp->pp_flags &= ~PP_NEEDAUTH;
3462 if (sp->myauth.proto == PPP_PAP &&
3463 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
3464 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
3466 * We are authenticator for PAP but didn't
3467 * complete yet. Leave it to tlu to proceed
3474 sppp_phase_network(sp);
3478 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3480 log(LOG_INFO, SPP_FMT "pap failure",
3482 name_len = *((char *)h);
3483 if (len > 5 && name_len) {
3485 sppp_print_string((char*)(h+1), name_len);
3489 log(LOG_INFO, SPP_FMT "pap failure\n",
3491 /* await LCP shutdown by authenticator */
3495 /* Unknown PAP packet type -- ignore. */
3497 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
3498 "<0x%x id=0x%x len=%d",
3500 h->type, h->ident, ntohs(h->len));
3501 sppp_print_bytes((u_char*)(h+1), len-4);
3510 sppp_pap_init(struct sppp *sp)
3512 /* PAP doesn't have STATE_INITIAL at all. */
3513 sp->state[IDX_PAP] = STATE_CLOSED;
3514 sp->fail_counter[IDX_PAP] = 0;
3515 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3516 callout_handle_init(&sp->ch[IDX_PAP]);
3517 callout_handle_init(&sp->pap_my_to_ch);
3522 sppp_pap_open(struct sppp *sp)
3524 if (sp->hisauth.proto == PPP_PAP &&
3525 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3526 /* we are authenticator for PAP, start our timer */
3527 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
3528 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
3530 if (sp->myauth.proto == PPP_PAP) {
3531 /* we are peer, send a request, and start a timer */
3533 TIMEOUT(sppp_pap_my_TO, (void *)sp, sp->lcp.timeout,
3539 sppp_pap_close(struct sppp *sp)
3541 if (sp->state[IDX_PAP] != STATE_CLOSED)
3542 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
3546 * That's the timeout routine if we are authenticator. Since the
3547 * authenticator is basically passive in PAP, we can't do much here.
3550 sppp_pap_TO(void *cookie)
3552 struct sppp *sp = (struct sppp *)cookie;
3558 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
3560 sppp_state_name(sp->state[IDX_PAP]),
3561 sp->rst_counter[IDX_PAP]);
3563 if (--sp->rst_counter[IDX_PAP] < 0)
3565 switch (sp->state[IDX_PAP]) {
3566 case STATE_REQ_SENT:
3568 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
3572 /* TO+ event, not very much we could do */
3573 switch (sp->state[IDX_PAP]) {
3574 case STATE_REQ_SENT:
3575 /* sppp_cp_change_state() will restart the timer */
3576 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
3584 * That's the timeout handler if we are peer. Since the peer is active,
3585 * we need to retransmit our PAP request since it is apparently lost.
3586 * XXX We should impose a max counter.
3589 sppp_pap_my_TO(void *cookie)
3591 struct sppp *sp = (struct sppp *)cookie;
3595 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
3602 sppp_pap_tlu(struct sppp *sp)
3607 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
3610 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
3611 SPP_ARGS(ifp), pap.name);
3614 /* indicate to LCP that we need to be closed down */
3615 sp->lcp.protos |= (1 << IDX_PAP);
3617 if (sp->pp_flags & PP_NEEDAUTH) {
3619 * Remote is authenticator, but his auth proto didn't
3620 * complete yet. Defer the transition to network
3627 sppp_phase_network(sp);
3631 sppp_pap_tld(struct sppp *sp)
3636 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
3637 UNTIMEOUT(pap.TO, (void *)sp, sp->ch[IDX_PAP]);
3638 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3639 sp->lcp.protos &= ~(1 << IDX_PAP);
3645 sppp_pap_scr(struct sppp *sp)
3647 u_char idlen, pwdlen;
3649 sp->confid[IDX_PAP] = ++sp->pp_seq;
3650 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
3651 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
3653 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
3654 sizeof idlen, (const char *)&idlen,
3655 (size_t)idlen, sp->myauth.name,
3656 sizeof pwdlen, (const char *)&pwdlen,
3657 (size_t)pwdlen, sp->myauth.secret,
3661 * Random miscellaneous functions.
3665 * Send a PAP or CHAP proto packet.
3667 * Varadic function, each of the elements for the ellipsis is of type
3668 * ``size_t mlen, const u_char *msg''. Processing will stop iff
3670 * NOTE: never declare variadic functions with types subject to type
3671 * promotion (i.e. u_char). This is asking for big trouble depending
3672 * on the architecture you are on...
3676 sppp_auth_send(const struct cp *cp, struct sppp *sp,
3677 unsigned int type, unsigned int id,
3681 struct ppp_header *h;
3682 struct lcp_header *lh;
3690 MGETHDR (m, M_DONTWAIT, MT_DATA);
3693 m->m_pkthdr.rcvif = 0;
3695 h = mtod (m, struct ppp_header*);
3696 h->address = PPP_ALLSTATIONS; /* broadcast address */
3697 h->control = PPP_UI; /* Unnumbered Info */
3698 h->protocol = htons(cp->proto);
3700 lh = (struct lcp_header*)(h + 1);
3703 p = (u_char*) (lh+1);
3708 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
3709 msg = va_arg(ap, const char *);
3711 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
3717 bcopy(msg, p, mlen);
3722 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
3723 lh->len = htons (LCP_HEADER_LEN + len);
3726 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
3727 SPP_ARGS(ifp), cp->name,
3728 sppp_auth_type_name(cp->proto, lh->type),
3729 lh->ident, ntohs(lh->len));
3730 sppp_print_bytes((u_char*) (lh+1), len);
3733 if (IF_QFULL (&sp->pp_cpq)) {
3734 IF_DROP (&sp->pp_fastq);
3735 IF_DROP (&ifp->if_snd);
3739 IF_ENQUEUE (&sp->pp_cpq, m);
3740 if (! (ifp->if_flags & IFF_OACTIVE))
3741 (*ifp->if_start) (ifp);
3742 ifp->if_obytes += m->m_pkthdr.len + 3;
3746 * Flush interface queue.
3749 sppp_qflush(struct ifqueue *ifq)
3764 * Send keepalive packets, every 10 seconds.
3767 sppp_keepalive(void *dummy)
3773 for (sp=spppq; sp; sp=sp->pp_next) {
3774 struct ifnet *ifp = &sp->pp_if;
3776 /* Keepalive mode disabled or channel down? */
3777 if (! (sp->pp_flags & PP_KEEPALIVE) ||
3778 ! (ifp->if_flags & IFF_RUNNING))
3781 /* No keepalive in PPP mode if LCP not opened yet. */
3782 if (sp->pp_mode != IFF_CISCO &&
3783 sp->pp_phase < PHASE_AUTHENTICATE)
3786 if (sp->pp_alivecnt == MAXALIVECNT) {
3787 /* No keepalive packets got. Stop the interface. */
3788 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
3790 sppp_qflush (&sp->pp_cpq);
3791 if (sp->pp_mode != IFF_CISCO) {
3793 /* Shut down the PPP link. */
3795 /* Initiate negotiation. XXX */
3799 if (sp->pp_alivecnt <= MAXALIVECNT)
3801 if (sp->pp_mode == IFF_CISCO)
3802 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
3804 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
3805 long nmagic = htonl (sp->lcp.magic);
3806 sp->lcp.echoid = ++sp->pp_seq;
3807 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
3808 sp->lcp.echoid, 4, &nmagic);
3812 TIMEOUT(sppp_keepalive, 0, hz * 10, keepalive_ch);
3816 * Get both IP addresses.
3819 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
3821 struct ifnet *ifp = &sp->pp_if;
3823 struct sockaddr_in *si, *sm;
3829 * Pick the first AF_INET address from the list,
3830 * aliases don't make any sense on a p2p link anyway.
3833 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3834 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
3835 #elif defined(__NetBSD__) || defined (__OpenBSD__)
3836 for (ifa = ifp->if_addrlist.tqh_first;
3838 ifa = ifa->ifa_list.tqe_next)
3840 for (ifa = ifp->if_addrlist;
3842 ifa = ifa->ifa_next)
3844 if (ifa->ifa_addr->sa_family == AF_INET) {
3845 si = (struct sockaddr_in *)ifa->ifa_addr;
3846 sm = (struct sockaddr_in *)ifa->ifa_netmask;
3851 if (si && si->sin_addr.s_addr) {
3852 ssrc = si->sin_addr.s_addr;
3854 *srcmask = ntohl(sm->sin_addr.s_addr);
3857 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
3858 if (si && si->sin_addr.s_addr)
3859 ddst = si->sin_addr.s_addr;
3862 if (dst) *dst = ntohl(ddst);
3863 if (src) *src = ntohl(ssrc);
3867 * Set my IP address. Must be called at splimp.
3870 sppp_set_ip_addr(struct sppp *sp, u_long src)
3874 struct sockaddr_in *si;
3877 * Pick the first AF_INET address from the list,
3878 * aliases don't make any sense on a p2p link anyway.
3881 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3882 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
3883 #elif defined(__NetBSD__) || defined (__OpenBSD__)
3884 for (ifa = ifp->if_addrlist.tqh_first;
3886 ifa = ifa->ifa_list.tqe_next)
3888 for (ifa = ifp->if_addrlist;
3890 ifa = ifa->ifa_next)
3893 if (ifa->ifa_addr->sa_family == AF_INET)
3895 si = (struct sockaddr_in *)ifa->ifa_addr;
3904 #if __NetBSD_Version__ >= 103080000
3905 struct sockaddr_in new_sin = *si;
3907 new_sin.sin_addr.s_addr = htonl(src);
3908 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 1);
3911 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: in_ifinit "
3912 " failed, error=%d\n", SPP_ARGS(ifp), error);
3915 /* delete old route */
3916 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
3919 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
3920 SPP_ARGS(ifp), error);
3923 /* set new address */
3924 si->sin_addr.s_addr = htonl(src);
3927 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
3930 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
3931 SPP_ARGS(ifp), error);
3938 sppp_params(struct sppp *sp, u_long cmd, void *data)
3941 struct ifreq *ifr = (struct ifreq *)data;
3945 * ifr->ifr_data is supposed to point to a struct spppreq.
3946 * Check the cmd word first before attempting to fetch all the
3949 if ((subcmd = fuword(ifr->ifr_data)) == -1)
3952 if (copyin((caddr_t)ifr->ifr_data, &spr, sizeof spr) != 0)
3957 if (cmd != SIOCGIFGENERIC)
3960 * We copy over the entire current state, but clean
3961 * out some of the stuff we don't wanna pass up.
3962 * Remember, SIOCGIFGENERIC is unprotected, and can be
3963 * called by any user. No need to ever get PAP or
3964 * CHAP secrets back to userland anyway.
3966 bcopy(sp, &spr.defs, sizeof(struct sppp));
3967 bzero(spr.defs.myauth.secret, AUTHKEYLEN);
3968 bzero(spr.defs.myauth.challenge, AUTHKEYLEN);
3969 bzero(spr.defs.hisauth.secret, AUTHKEYLEN);
3970 bzero(spr.defs.hisauth.challenge, AUTHKEYLEN);
3971 return copyout(&spr, (caddr_t)ifr->ifr_data, sizeof spr);
3974 if (cmd != SIOCSIFGENERIC)
3977 * We have a very specific idea of which fields we allow
3978 * being passed back from userland, so to not clobber our
3979 * current state. For one, we only allow setting
3980 * anything if LCP is in dead phase. Once the LCP
3981 * negotiations started, the authentication settings must
3982 * not be changed again. (The administrator can force an
3983 * ifconfig down in order to get LCP back into dead
3986 * Also, we only allow for authentication parameters to be
3989 * XXX Should allow to set or clear pp_flags.
3991 * Finally, if the respective authentication protocol to
3992 * be used is set differently than 0, but the secret is
3993 * passed as all zeros, we don't trash the existing secret.
3994 * This allows an administrator to change the system name
3995 * only without clobbering the secret (which he didn't get
3996 * back in a previous SPPPIOGDEFS call). However, the
3997 * secrets are cleared if the authentication protocol is
4000 if (sp->pp_phase != PHASE_DEAD)
4003 if ((spr.defs.myauth.proto != 0 && spr.defs.myauth.proto != PPP_PAP &&
4004 spr.defs.myauth.proto != PPP_CHAP) ||
4005 (spr.defs.hisauth.proto != 0 && spr.defs.hisauth.proto != PPP_PAP &&
4006 spr.defs.hisauth.proto != PPP_CHAP))
4009 if (spr.defs.myauth.proto == 0)
4010 /* resetting myauth */
4011 bzero(&sp->myauth, sizeof sp->myauth);
4013 /* setting/changing myauth */
4014 sp->myauth.proto = spr.defs.myauth.proto;
4015 bcopy(spr.defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
4016 if (spr.defs.myauth.secret[0] != '\0')
4017 bcopy(spr.defs.myauth.secret, sp->myauth.secret,
4020 if (spr.defs.hisauth.proto == 0)
4021 /* resetting hisauth */
4022 bzero(&sp->hisauth, sizeof sp->hisauth);
4024 /* setting/changing hisauth */
4025 sp->hisauth.proto = spr.defs.hisauth.proto;
4026 sp->hisauth.flags = spr.defs.hisauth.flags;
4027 bcopy(spr.defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
4028 if (spr.defs.hisauth.secret[0] != '\0')
4029 bcopy(spr.defs.hisauth.secret, sp->hisauth.secret,
4042 sppp_phase_network(struct sppp *sp)
4048 sp->pp_phase = PHASE_NETWORK;
4051 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
4052 sppp_phase_name(sp->pp_phase));
4054 /* Notify NCPs now. */
4055 for (i = 0; i < IDX_COUNT; i++)
4056 if ((cps[i])->flags & CP_NCP)
4059 /* Send Up events to all NCPs. */
4060 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
4061 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_NCP))
4064 /* if no NCP is starting, all this was in vain, close down */
4065 sppp_lcp_check_and_close(sp);
4070 sppp_cp_type_name(u_char type)
4072 static char buf[12];
4074 case CONF_REQ: return "conf-req";
4075 case CONF_ACK: return "conf-ack";
4076 case CONF_NAK: return "conf-nak";
4077 case CONF_REJ: return "conf-rej";
4078 case TERM_REQ: return "term-req";
4079 case TERM_ACK: return "term-ack";
4080 case CODE_REJ: return "code-rej";
4081 case PROTO_REJ: return "proto-rej";
4082 case ECHO_REQ: return "echo-req";
4083 case ECHO_REPLY: return "echo-reply";
4084 case DISC_REQ: return "discard-req";
4086 snprintf (buf, sizeof(buf), "cp/0x%x", type);
4091 sppp_auth_type_name(u_short proto, u_char type)
4093 static char buf[12];
4097 case CHAP_CHALLENGE: return "challenge";
4098 case CHAP_RESPONSE: return "response";
4099 case CHAP_SUCCESS: return "success";
4100 case CHAP_FAILURE: return "failure";
4104 case PAP_REQ: return "req";
4105 case PAP_ACK: return "ack";
4106 case PAP_NAK: return "nak";
4109 snprintf (buf, sizeof(buf), "auth/0x%x", type);
4114 sppp_lcp_opt_name(u_char opt)
4116 static char buf[12];
4118 case LCP_OPT_MRU: return "mru";
4119 case LCP_OPT_ASYNC_MAP: return "async-map";
4120 case LCP_OPT_AUTH_PROTO: return "auth-proto";
4121 case LCP_OPT_QUAL_PROTO: return "qual-proto";
4122 case LCP_OPT_MAGIC: return "magic";
4123 case LCP_OPT_PROTO_COMP: return "proto-comp";
4124 case LCP_OPT_ADDR_COMP: return "addr-comp";
4126 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
4131 sppp_ipcp_opt_name(u_char opt)
4133 static char buf[12];
4135 case IPCP_OPT_ADDRESSES: return "addresses";
4136 case IPCP_OPT_COMPRESSION: return "compression";
4137 case IPCP_OPT_ADDRESS: return "address";
4139 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
4144 sppp_state_name(int state)
4147 case STATE_INITIAL: return "initial";
4148 case STATE_STARTING: return "starting";
4149 case STATE_CLOSED: return "closed";
4150 case STATE_STOPPED: return "stopped";
4151 case STATE_CLOSING: return "closing";
4152 case STATE_STOPPING: return "stopping";
4153 case STATE_REQ_SENT: return "req-sent";
4154 case STATE_ACK_RCVD: return "ack-rcvd";
4155 case STATE_ACK_SENT: return "ack-sent";
4156 case STATE_OPENED: return "opened";
4162 sppp_phase_name(enum ppp_phase phase)
4165 case PHASE_DEAD: return "dead";
4166 case PHASE_ESTABLISH: return "establish";
4167 case PHASE_TERMINATE: return "terminate";
4168 case PHASE_AUTHENTICATE: return "authenticate";
4169 case PHASE_NETWORK: return "network";
4175 sppp_proto_name(u_short proto)
4177 static char buf[12];
4179 case PPP_LCP: return "lcp";
4180 case PPP_IPCP: return "ipcp";
4181 case PPP_PAP: return "pap";
4182 case PPP_CHAP: return "chap";
4184 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
4189 sppp_print_bytes(const u_char *p, u_short len)
4192 addlog(" %*D", len, p, "-");
4196 sppp_print_string(const char *p, u_short len)
4203 * Print only ASCII chars directly. RFC 1994 recommends
4204 * using only them, but we don't rely on it. */
4205 if (c < ' ' || c > '~')
4213 sppp_dotted_quad(u_long addr)
4216 sprintf(s, "%d.%d.%d.%d",
4217 (int)((addr >> 24) & 0xff),
4218 (int)((addr >> 16) & 0xff),
4219 (int)((addr >> 8) & 0xff),
4220 (int)(addr & 0xff));
4225 sppp_strnlen(u_char *p, int max)
4229 for (len = 0; len < max && *p; ++p)
4234 /* a dummy, used to drop uninteresting events */
4236 sppp_null(struct sppp *unused)
4238 /* do just nothing */