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 <sys/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 (__NetBSD__) || defined (__OpenBSD__)
62 #include <machine/cpu.h> /* XXX for softnet */
65 #include <machine/stdarg.h>
68 #include <netinet/in.h>
69 #include <netinet/in_systm.h>
70 #include <netinet/in_var.h>
71 #include <netinet/ip.h>
72 #include <netinet/tcp.h>
73 # if defined (__FreeBSD__) || defined (__OpenBSD__)
74 # include <netinet/if_ether.h>
76 # include <net/ethertypes.h>
79 # error Huh? sppp without INET?
83 #include <netipx/ipx.h>
84 #include <netipx/ipx_if.h>
89 #include <netns/ns_if.h>
92 #include <net/if_sppp.h>
94 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
95 # define UNTIMEOUT(fun, arg, handle) untimeout(fun, arg, handle)
96 # define TIMEOUT(fun, arg1, arg2, handle) handle = timeout(fun, arg1, arg2)
97 # define IOCTL_CMD_T u_long
99 # define UNTIMEOUT(fun, arg, handle) untimeout(fun, arg)
100 # define TIMEOUT(fun, arg1, arg2, handle) timeout(fun, arg1, arg2)
101 # define IOCTL_CMD_T int
104 #define MAXALIVECNT 3 /* max. alive packets */
107 * Interface flags that can be set in an ifconfig command.
109 * Setting link0 will make the link passive, i.e. it will be marked
110 * as being administrative openable, but won't be opened to begin
111 * with. Incoming calls will be answered, or subsequent calls with
112 * -link1 will cause the administrative open of the LCP layer.
114 * Setting link1 will cause the link to auto-dial only as packets
117 * Setting IFF_DEBUG will syslog the option negotiation and state
118 * transitions at level kern.debug. Note: all logs consistently look
121 * <if-name><unit>: <proto-name> <additional info...>
123 * with <if-name><unit> being something like "bppp0", and <proto-name>
124 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc.
127 #define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */
128 #define IFF_AUTO IFF_LINK1 /* auto-dial on output */
129 #define IFF_CISCO IFF_LINK2 /* auto-dial on output */
131 #define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */
132 #define PPP_UI 0x03 /* Unnumbered Information */
133 #define PPP_IP 0x0021 /* Internet Protocol */
134 #define PPP_ISO 0x0023 /* ISO OSI Protocol */
135 #define PPP_XNS 0x0025 /* Xerox NS Protocol */
136 #define PPP_IPX 0x002b /* Novell IPX Protocol */
137 #define PPP_LCP 0xc021 /* Link Control Protocol */
138 #define PPP_PAP 0xc023 /* Password Authentication Protocol */
139 #define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */
140 #define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */
142 #define CONF_REQ 1 /* PPP configure request */
143 #define CONF_ACK 2 /* PPP configure acknowledge */
144 #define CONF_NAK 3 /* PPP configure negative ack */
145 #define CONF_REJ 4 /* PPP configure reject */
146 #define TERM_REQ 5 /* PPP terminate request */
147 #define TERM_ACK 6 /* PPP terminate acknowledge */
148 #define CODE_REJ 7 /* PPP code reject */
149 #define PROTO_REJ 8 /* PPP protocol reject */
150 #define ECHO_REQ 9 /* PPP echo request */
151 #define ECHO_REPLY 10 /* PPP echo reply */
152 #define DISC_REQ 11 /* PPP discard request */
154 #define LCP_OPT_MRU 1 /* maximum receive unit */
155 #define LCP_OPT_ASYNC_MAP 2 /* async control character map */
156 #define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */
157 #define LCP_OPT_QUAL_PROTO 4 /* quality protocol */
158 #define LCP_OPT_MAGIC 5 /* magic number */
159 #define LCP_OPT_RESERVED 6 /* reserved */
160 #define LCP_OPT_PROTO_COMP 7 /* protocol field compression */
161 #define LCP_OPT_ADDR_COMP 8 /* address/control field compression */
163 #define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */
164 #define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */
165 #define IPCP_OPT_ADDRESS 3 /* local IP address */
167 #define PAP_REQ 1 /* PAP name/password request */
168 #define PAP_ACK 2 /* PAP acknowledge */
169 #define PAP_NAK 3 /* PAP fail */
171 #define CHAP_CHALLENGE 1 /* CHAP challenge request */
172 #define CHAP_RESPONSE 2 /* CHAP challenge response */
173 #define CHAP_SUCCESS 3 /* CHAP response ok */
174 #define CHAP_FAILURE 4 /* CHAP response failed */
176 #define CHAP_MD5 5 /* hash algorithm - MD5 */
178 #define CISCO_MULTICAST 0x8f /* Cisco multicast address */
179 #define CISCO_UNICAST 0x0f /* Cisco unicast address */
180 #define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */
181 #define CISCO_ADDR_REQ 0 /* Cisco address request */
182 #define CISCO_ADDR_REPLY 1 /* Cisco address reply */
183 #define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */
185 /* states are named and numbered according to RFC 1661 */
186 #define STATE_INITIAL 0
187 #define STATE_STARTING 1
188 #define STATE_CLOSED 2
189 #define STATE_STOPPED 3
190 #define STATE_CLOSING 4
191 #define STATE_STOPPING 5
192 #define STATE_REQ_SENT 6
193 #define STATE_ACK_RCVD 7
194 #define STATE_ACK_SENT 8
195 #define STATE_OPENED 9
202 #define PPP_HEADER_LEN sizeof (struct ppp_header)
209 #define LCP_HEADER_LEN sizeof (struct lcp_header)
211 struct cisco_packet {
219 #define CISCO_PACKET_LEN 18
222 * We follow the spelling and capitalization of RFC 1661 here, to make
223 * it easier comparing with the standard. Please refer to this RFC in
224 * case you can't make sense out of these abbreviation; it will also
225 * explain the semantics related to the various events and actions.
228 u_short proto; /* PPP control protocol number */
229 u_char protoidx; /* index into state table in struct sppp */
231 #define CP_LCP 0x01 /* this is the LCP */
232 #define CP_AUTH 0x02 /* this is an authentication protocol */
233 #define CP_NCP 0x04 /* this is a NCP */
234 #define CP_QUAL 0x08 /* this is a quality reporting protocol */
235 const char *name; /* name of this control protocol */
237 void (*Up)(struct sppp *sp);
238 void (*Down)(struct sppp *sp);
239 void (*Open)(struct sppp *sp);
240 void (*Close)(struct sppp *sp);
241 void (*TO)(void *sp);
242 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len);
243 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len);
244 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len);
246 void (*tlu)(struct sppp *sp);
247 void (*tld)(struct sppp *sp);
248 void (*tls)(struct sppp *sp);
249 void (*tlf)(struct sppp *sp);
250 void (*scr)(struct sppp *sp);
253 static struct sppp *spppq;
254 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
255 static struct callout_handle keepalive_ch;
258 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
259 #define SPP_FMT "%s%d: "
260 #define SPP_ARGS(ifp) (ifp)->if_name, (ifp)->if_unit
262 #define SPP_FMT "%s: "
263 #define SPP_ARGS(ifp) (ifp)->if_xname
267 * The following disgusting hack gets around the problem that IP TOS
268 * can't be set yet. We want to put "interactive" traffic on a high
269 * priority queue. To decide if traffic is interactive, we check that
270 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control.
272 * XXX is this really still necessary? - joerg -
274 static u_short interactive_ports[8] = {
278 #define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p))
280 /* almost every function needs these */
282 struct ifnet *ifp = &sp->pp_if; \
283 int debug = ifp->if_flags & IFF_DEBUG
285 static int sppp_output(struct ifnet *ifp, struct mbuf *m,
286 struct sockaddr *dst, struct rtentry *rt);
288 static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2);
289 static void sppp_cisco_input(struct sppp *sp, struct mbuf *m);
291 static void sppp_cp_input(const struct cp *cp, struct sppp *sp,
293 static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type,
294 u_char ident, u_short len, void *data);
295 /* static void sppp_cp_timeout(void *arg); */
296 static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp,
298 static void sppp_auth_send(const struct cp *cp,
299 struct sppp *sp, unsigned int type, unsigned int id,
302 static void sppp_up_event(const struct cp *cp, struct sppp *sp);
303 static void sppp_down_event(const struct cp *cp, struct sppp *sp);
304 static void sppp_open_event(const struct cp *cp, struct sppp *sp);
305 static void sppp_close_event(const struct cp *cp, struct sppp *sp);
306 static void sppp_to_event(const struct cp *cp, struct sppp *sp);
308 static void sppp_null(struct sppp *sp);
310 static void sppp_lcp_init(struct sppp *sp);
311 static void sppp_lcp_up(struct sppp *sp);
312 static void sppp_lcp_down(struct sppp *sp);
313 static void sppp_lcp_open(struct sppp *sp);
314 static void sppp_lcp_close(struct sppp *sp);
315 static void sppp_lcp_TO(void *sp);
316 static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
317 static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
318 static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
319 static void sppp_lcp_tlu(struct sppp *sp);
320 static void sppp_lcp_tld(struct sppp *sp);
321 static void sppp_lcp_tls(struct sppp *sp);
322 static void sppp_lcp_tlf(struct sppp *sp);
323 static void sppp_lcp_scr(struct sppp *sp);
324 static void sppp_lcp_check_and_close(struct sppp *sp);
325 static int sppp_ncp_check(struct sppp *sp);
327 static void sppp_ipcp_init(struct sppp *sp);
328 static void sppp_ipcp_up(struct sppp *sp);
329 static void sppp_ipcp_down(struct sppp *sp);
330 static void sppp_ipcp_open(struct sppp *sp);
331 static void sppp_ipcp_close(struct sppp *sp);
332 static void sppp_ipcp_TO(void *sp);
333 static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len);
334 static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len);
335 static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len);
336 static void sppp_ipcp_tlu(struct sppp *sp);
337 static void sppp_ipcp_tld(struct sppp *sp);
338 static void sppp_ipcp_tls(struct sppp *sp);
339 static void sppp_ipcp_tlf(struct sppp *sp);
340 static void sppp_ipcp_scr(struct sppp *sp);
342 static void sppp_pap_input(struct sppp *sp, struct mbuf *m);
343 static void sppp_pap_init(struct sppp *sp);
344 static void sppp_pap_open(struct sppp *sp);
345 static void sppp_pap_close(struct sppp *sp);
346 static void sppp_pap_TO(void *sp);
347 static void sppp_pap_my_TO(void *sp);
348 static void sppp_pap_tlu(struct sppp *sp);
349 static void sppp_pap_tld(struct sppp *sp);
350 static void sppp_pap_scr(struct sppp *sp);
352 static void sppp_chap_input(struct sppp *sp, struct mbuf *m);
353 static void sppp_chap_init(struct sppp *sp);
354 static void sppp_chap_open(struct sppp *sp);
355 static void sppp_chap_close(struct sppp *sp);
356 static void sppp_chap_TO(void *sp);
357 static void sppp_chap_tlu(struct sppp *sp);
358 static void sppp_chap_tld(struct sppp *sp);
359 static void sppp_chap_scr(struct sppp *sp);
361 static const char *sppp_auth_type_name(u_short proto, u_char type);
362 static const char *sppp_cp_type_name(u_char type);
363 static const char *sppp_dotted_quad(u_long addr);
364 static const char *sppp_ipcp_opt_name(u_char opt);
365 static const char *sppp_lcp_opt_name(u_char opt);
366 static const char *sppp_phase_name(enum ppp_phase phase);
367 static const char *sppp_proto_name(u_short proto);
368 static const char *sppp_state_name(int state);
369 static int sppp_params(struct sppp *sp, u_long cmd, void *data);
370 static int sppp_strnlen(u_char *p, int max);
371 static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst,
373 static void sppp_keepalive(void *dummy);
374 static void sppp_phase_network(struct sppp *sp);
375 static void sppp_print_bytes(const u_char *p, u_short len);
376 static void sppp_print_string(const char *p, u_short len);
377 static void sppp_qflush(struct ifqueue *ifq);
378 static void sppp_set_ip_addr(struct sppp *sp, u_long src);
380 /* our control protocol descriptors */
381 static const struct cp lcp = {
382 PPP_LCP, IDX_LCP, CP_LCP, "lcp",
383 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close,
384 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak,
385 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf,
389 static const struct cp ipcp = {
390 PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp",
391 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close,
392 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak,
393 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf,
397 static const struct cp pap = {
398 PPP_PAP, IDX_PAP, CP_AUTH, "pap",
399 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close,
400 sppp_pap_TO, 0, 0, 0,
401 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null,
405 static const struct cp chap = {
406 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap",
407 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close,
408 sppp_chap_TO, 0, 0, 0,
409 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null,
413 static const struct cp *cps[IDX_COUNT] = {
415 &ipcp, /* IDX_IPCP */
417 &chap, /* IDX_CHAP */
422 * Exported functions, comprising our interface to the lower layer.
426 * Process the received packet.
429 sppp_input(struct ifnet *ifp, struct mbuf *m)
431 struct ppp_header *h;
432 struct ifqueue *inq = 0;
434 struct sppp *sp = (struct sppp *)ifp;
435 int debug = ifp->if_flags & IFF_DEBUG;
437 if (ifp->if_flags & IFF_UP)
438 /* Count received bytes, add FCS and one flag */
439 ifp->if_ibytes += m->m_pkthdr.len + 3;
441 if (m->m_pkthdr.len <= PPP_HEADER_LEN) {
442 /* Too small packet, drop it. */
445 SPP_FMT "input packet is too small, %d bytes\n",
446 SPP_ARGS(ifp), m->m_pkthdr.len);
454 /* Get PPP header. */
455 h = mtod (m, struct ppp_header*);
456 m_adj (m, PPP_HEADER_LEN);
458 switch (h->address) {
459 case PPP_ALLSTATIONS:
460 if (h->control != PPP_UI)
462 if (sp->pp_mode == IFF_CISCO) {
465 SPP_FMT "PPP packet in Cisco mode "
466 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
468 h->address, h->control, ntohs(h->protocol));
471 switch (ntohs (h->protocol)) {
475 SPP_FMT "rejecting protocol "
476 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
478 h->address, h->control, ntohs(h->protocol));
479 if (sp->state[IDX_LCP] == STATE_OPENED)
480 sppp_cp_send (sp, PPP_LCP, PROTO_REJ,
481 ++sp->pp_seq, m->m_pkthdr.len + 2,
486 sppp_cp_input(&lcp, sp, m);
490 if (sp->pp_phase >= PHASE_AUTHENTICATE)
491 sppp_pap_input(sp, m);
495 if (sp->pp_phase >= PHASE_AUTHENTICATE)
496 sppp_chap_input(sp, m);
501 if (sp->pp_phase == PHASE_NETWORK)
502 sppp_cp_input(&ipcp, sp, m);
506 if (sp->state[IDX_IPCP] == STATE_OPENED) {
507 schednetisr (NETISR_IP);
514 /* IPX IPXCP not implemented yet */
515 if (sp->pp_phase == PHASE_NETWORK) {
516 schednetisr (NETISR_IPX);
523 /* XNS IDPCP not implemented yet */
524 if (sp->pp_phase == PHASE_NETWORK) {
525 schednetisr (NETISR_NS);
532 case CISCO_MULTICAST:
534 /* Don't check the control field here (RFC 1547). */
535 if (sp->pp_mode != IFF_CISCO) {
538 SPP_FMT "Cisco packet in PPP mode "
539 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
541 h->address, h->control, ntohs(h->protocol));
544 switch (ntohs (h->protocol)) {
548 case CISCO_KEEPALIVE:
549 sppp_cisco_input ((struct sppp*) ifp, m);
554 schednetisr (NETISR_IP);
560 schednetisr (NETISR_IPV6);
566 schednetisr (NETISR_IPX);
572 schednetisr (NETISR_NS);
578 default: /* Invalid PPP packet. */
582 SPP_FMT "invalid input packet "
583 "<addr=0x%x ctrl=0x%x proto=0x%x>\n",
585 h->address, h->control, ntohs(h->protocol));
589 if (! (ifp->if_flags & IFF_UP) || ! inq)
594 if (IF_QFULL (inq)) {
595 /* Queue overflow. */
599 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n",
608 * Enqueue transmit packet.
611 sppp_output(struct ifnet *ifp, struct mbuf *m,
612 struct sockaddr *dst, struct rtentry *rt)
614 struct sppp *sp = (struct sppp*) ifp;
615 struct ppp_header *h;
618 int debug = ifp->if_flags & IFF_DEBUG;
622 if ((ifp->if_flags & IFF_UP) == 0 ||
623 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) {
629 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) {
631 * Interface is not yet running, but auto-dial. Need
632 * to start LCP for it.
634 ifp->if_flags |= IFF_RUNNING;
642 if (dst->sa_family == AF_INET) {
643 /* XXX Check mbuf length here? */
644 struct ip *ip = mtod (m, struct ip*);
645 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl);
648 * When using dynamic local IP address assignment by using
649 * 0.0.0.0 as a local address, the first TCP session will
650 * not connect because the local TCP checksum is computed
651 * using 0.0.0.0 which will later become our real IP address
652 * so the TCP checksum computed at the remote end will
653 * become invalid. So we
654 * - don't let packets with src ip addr 0 thru
655 * - we flag TCP packets with src ip 0 as an error
658 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */
662 if(ip->ip_p == IPPROTO_TCP)
663 return(EADDRNOTAVAIL);
669 * Put low delay, telnet, rlogin and ftp control packets
670 * in front of the queue.
672 if (IF_QFULL (&sp->pp_fastq))
674 else if (ip->ip_tos & IPTOS_LOWDELAY)
676 else if (m->m_len < sizeof *ip + sizeof *tcp)
678 else if (ip->ip_p != IPPROTO_TCP)
680 else if (INTERACTIVE (ntohs (tcp->th_sport)))
682 else if (INTERACTIVE (ntohs (tcp->th_dport)))
688 * Prepend general data packet PPP header. For now, IP only.
690 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT);
693 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n",
700 * May want to check size of packet
701 * (albeit due to the implementation it's always enough)
703 h = mtod (m, struct ppp_header*);
704 if (sp->pp_mode == IFF_CISCO) {
705 h->address = CISCO_UNICAST; /* unicast address */
708 h->address = PPP_ALLSTATIONS; /* broadcast address */
709 h->control = PPP_UI; /* Unnumbered Info */
712 switch (dst->sa_family) {
714 case AF_INET: /* Internet Protocol */
715 if (sp->pp_mode == IFF_CISCO)
716 h->protocol = htons (ETHERTYPE_IP);
719 * Don't choke with an ENETDOWN early. It's
720 * possible that we just started dialing out,
721 * so don't drop the packet immediately. If
722 * we notice that we run out of buffer space
723 * below, we will however remember that we are
724 * not ready to carry IP packets, and return
725 * ENETDOWN, as opposed to ENOBUFS.
727 h->protocol = htons(PPP_IP);
728 if (sp->state[IDX_IPCP] != STATE_OPENED)
734 case AF_INET6: /* Internet Protocol */
735 if (sp->pp_mode == IFF_CISCO)
736 h->protocol = htons (ETHERTYPE_IPV6);
743 case AF_NS: /* Xerox NS Protocol */
744 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
745 ETHERTYPE_NS : PPP_XNS);
749 case AF_IPX: /* Novell IPX Protocol */
750 h->protocol = htons (sp->pp_mode == IFF_CISCO ?
751 ETHERTYPE_IPX : PPP_IPX);
759 return (EAFNOSUPPORT);
763 * Queue message on interface, and start output if interface
766 if (IF_QFULL (ifq)) {
767 IF_DROP (&ifp->if_snd);
771 return (rv? rv: ENOBUFS);
774 if (! (ifp->if_flags & IFF_OACTIVE))
775 (*ifp->if_start) (ifp);
778 * Count output packets and bytes.
779 * The packet length includes header, FCS and 1 flag,
780 * according to RFC 1333.
782 ifp->if_obytes += m->m_pkthdr.len + 3;
788 sppp_attach(struct ifnet *ifp)
790 struct sppp *sp = (struct sppp*) ifp;
792 /* Initialize keepalive handler. */
794 TIMEOUT(sppp_keepalive, 0, hz * 10, keepalive_ch);
796 /* Insert new entry into the keepalive list. */
800 sp->pp_if.if_mtu = PP_MTU;
801 sp->pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST;
802 sp->pp_if.if_type = IFT_PPP;
803 sp->pp_if.if_output = sppp_output;
805 sp->pp_flags = PP_KEEPALIVE;
807 sp->pp_fastq.ifq_maxlen = 32;
808 sp->pp_cpq.ifq_maxlen = 20;
813 sp->pp_phase = PHASE_DEAD;
815 sp->pp_down = lcp.Down;
824 sppp_detach(struct ifnet *ifp)
826 struct sppp **q, *p, *sp = (struct sppp*) ifp;
829 /* Remove the entry from the keepalive list. */
830 for (q = &spppq; (p = *q); q = &p->pp_next)
836 /* Stop keepalive handler. */
838 UNTIMEOUT(sppp_keepalive, 0, keepalive_ch);
840 for (i = 0; i < IDX_COUNT; i++)
841 UNTIMEOUT((cps[i])->TO, (void *)sp, sp->ch[i]);
842 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
846 * Flush the interface output queue.
849 sppp_flush(struct ifnet *ifp)
851 struct sppp *sp = (struct sppp*) ifp;
853 sppp_qflush (&sp->pp_if.if_snd);
854 sppp_qflush (&sp->pp_fastq);
855 sppp_qflush (&sp->pp_cpq);
859 * Check if the output queue is empty.
862 sppp_isempty(struct ifnet *ifp)
864 struct sppp *sp = (struct sppp*) ifp;
868 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head &&
869 !sp->pp_if.if_snd.ifq_head;
875 * Get next packet to send.
878 sppp_dequeue(struct ifnet *ifp)
880 struct sppp *sp = (struct sppp*) ifp;
886 * Process only the control protocol queue until we have at
887 * least one NCP open.
889 * Do always serve all three queues in Cisco mode.
891 IF_DEQUEUE(&sp->pp_cpq, m);
893 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO)) {
894 IF_DEQUEUE(&sp->pp_fastq, m);
896 IF_DEQUEUE (&sp->pp_if.if_snd, m);
903 * Pick the next packet, do not remove it from the queue.
906 sppp_pick(struct ifnet *ifp)
908 struct sppp *sp = (struct sppp*)ifp;
914 m = sp->pp_cpq.ifq_head;
916 (sp->pp_phase == PHASE_NETWORK || sp->pp_mode == IFF_CISCO))
917 if ((m = sp->pp_fastq.ifq_head) == NULL)
918 m = sp->pp_if.if_snd.ifq_head;
924 * Process an ioctl request. Called on low priority level.
927 sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data)
929 struct ifreq *ifr = (struct ifreq*) data;
930 struct sppp *sp = (struct sppp*) ifp;
931 int s, rv, going_up, going_down, newmode;
942 /* fall through... */
945 going_up = ifp->if_flags & IFF_UP &&
946 (ifp->if_flags & IFF_RUNNING) == 0;
947 going_down = (ifp->if_flags & IFF_UP) == 0 &&
948 ifp->if_flags & IFF_RUNNING;
950 newmode = ifp->if_flags & IFF_PASSIVE;
952 newmode = ifp->if_flags & IFF_AUTO;
954 newmode = ifp->if_flags & IFF_CISCO;
955 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO);
956 ifp->if_flags |= newmode;
958 if (newmode != sp->pp_mode) {
961 going_up = ifp->if_flags & IFF_RUNNING;
965 if (sp->pp_mode != IFF_CISCO)
970 ifp->if_flags &= ~IFF_RUNNING;
971 sp->pp_mode = newmode;
975 if (sp->pp_mode != IFF_CISCO)
977 sp->pp_mode = newmode;
978 if (sp->pp_mode == 0) {
979 ifp->if_flags |= IFF_RUNNING;
982 if (sp->pp_mode == IFF_CISCO) {
985 ifp->if_flags |= IFF_RUNNING;
993 #define ifr_mtu ifr_metric
996 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru)
998 ifp->if_mtu = ifr->ifr_mtu;
1003 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru)
1005 ifp->if_mtu = *(short*)data;
1010 ifr->ifr_mtu = ifp->if_mtu;
1015 *(short*)data = ifp->if_mtu;
1022 case SIOCGIFGENERIC:
1023 case SIOCSIFGENERIC:
1024 rv = sppp_params(sp, cmd, data);
1036 * Cisco framing implementation.
1040 * Handle incoming Cisco keepalive protocol packets.
1043 sppp_cisco_input(struct sppp *sp, struct mbuf *m)
1046 struct cisco_packet *h;
1049 if (m->m_pkthdr.len < CISCO_PACKET_LEN) {
1052 SPP_FMT "cisco invalid packet length: %d bytes\n",
1053 SPP_ARGS(ifp), m->m_pkthdr.len);
1056 h = mtod (m, struct cisco_packet*);
1059 SPP_FMT "cisco input: %d bytes "
1060 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1061 SPP_ARGS(ifp), m->m_pkthdr.len,
1062 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel,
1063 (u_int)h->time0, (u_int)h->time1);
1064 switch (ntohl (h->type)) {
1067 addlog(SPP_FMT "cisco unknown packet type: 0x%lx\n",
1068 SPP_ARGS(ifp), (u_long)ntohl (h->type));
1070 case CISCO_ADDR_REPLY:
1071 /* Reply on address request, ignore */
1073 case CISCO_KEEPALIVE_REQ:
1074 sp->pp_alivecnt = 0;
1075 sp->pp_rseq = ntohl (h->par1);
1076 if (sp->pp_seq == sp->pp_rseq) {
1077 /* Local and remote sequence numbers are equal.
1078 * Probably, the line is in loopback mode. */
1079 if (sp->pp_loopcnt >= MAXALIVECNT) {
1080 printf (SPP_FMT "loopback\n",
1083 if (ifp->if_flags & IFF_UP) {
1085 sppp_qflush (&sp->pp_cpq);
1090 /* Generate new local sequence number */
1091 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1092 sp->pp_seq = random();
1094 sp->pp_seq ^= time.tv_sec ^ time.tv_usec;
1099 if (! (ifp->if_flags & IFF_UP) &&
1100 (ifp->if_flags & IFF_RUNNING)) {
1102 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
1105 case CISCO_ADDR_REQ:
1106 sppp_get_ip_addrs(sp, &me, 0, &mymask);
1108 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask);
1114 * Send Cisco keepalive packet.
1117 sppp_cisco_send(struct sppp *sp, int type, long par1, long par2)
1120 struct ppp_header *h;
1121 struct cisco_packet *ch;
1123 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1126 u_long t = (time.tv_sec - boottime.tv_sec) * 1000;
1129 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1130 getmicrouptime(&tv);
1133 MGETHDR (m, M_DONTWAIT, MT_DATA);
1136 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN;
1137 m->m_pkthdr.rcvif = 0;
1139 h = mtod (m, struct ppp_header*);
1140 h->address = CISCO_MULTICAST;
1142 h->protocol = htons (CISCO_KEEPALIVE);
1144 ch = (struct cisco_packet*) (h + 1);
1145 ch->type = htonl (type);
1146 ch->par1 = htonl (par1);
1147 ch->par2 = htonl (par2);
1150 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1151 ch->time0 = htons ((u_short) (tv.tv_sec >> 16));
1152 ch->time1 = htons ((u_short) tv.tv_sec);
1154 ch->time0 = htons ((u_short) (t >> 16));
1155 ch->time1 = htons ((u_short) t);
1160 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n",
1161 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1,
1162 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1);
1164 if (IF_QFULL (&sp->pp_cpq)) {
1165 IF_DROP (&sp->pp_fastq);
1166 IF_DROP (&ifp->if_snd);
1169 IF_ENQUEUE (&sp->pp_cpq, m);
1170 if (! (ifp->if_flags & IFF_OACTIVE))
1171 (*ifp->if_start) (ifp);
1172 ifp->if_obytes += m->m_pkthdr.len + 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_QFULL (&sp->pp_cpq)) {
1221 IF_DROP (&sp->pp_fastq);
1222 IF_DROP (&ifp->if_snd);
1226 IF_ENQUEUE (&sp->pp_cpq, m);
1227 if (! (ifp->if_flags & IFF_OACTIVE))
1228 (*ifp->if_start) (ifp);
1229 ifp->if_obytes += m->m_pkthdr.len + 3;
1233 * Handle incoming PPP control protocol packets.
1236 sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m)
1239 struct lcp_header *h;
1240 int len = m->m_pkthdr.len;
1247 SPP_FMT "%s invalid packet length: %d bytes\n",
1248 SPP_ARGS(ifp), cp->name, len);
1251 h = mtod (m, struct lcp_header*);
1254 SPP_FMT "%s input(%s): <%s id=0x%x len=%d",
1255 SPP_ARGS(ifp), cp->name,
1256 sppp_state_name(sp->state[cp->protoidx]),
1257 sppp_cp_type_name (h->type), h->ident, ntohs (h->len));
1258 sppp_print_bytes ((u_char*) (h+1), len-4);
1261 if (len > ntohs (h->len))
1262 len = ntohs (h->len);
1263 p = (u_char *)(h + 1);
1268 addlog(SPP_FMT "%s invalid conf-req length %d\n",
1269 SPP_ARGS(ifp), cp->name,
1274 /* handle states where RCR doesn't get a SCA/SCN */
1275 switch (sp->state[cp->protoidx]) {
1277 case STATE_STOPPING:
1280 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident,
1284 rv = (cp->RCR)(sp, h, len);
1285 switch (sp->state[cp->protoidx]) {
1289 /* fall through... */
1290 case STATE_ACK_SENT:
1291 case STATE_REQ_SENT:
1292 sppp_cp_change_state(cp, sp, rv?
1293 STATE_ACK_SENT: STATE_REQ_SENT);
1296 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1298 sppp_cp_change_state(cp, sp, rv?
1299 STATE_ACK_SENT: STATE_REQ_SENT);
1301 case STATE_ACK_RCVD:
1303 sppp_cp_change_state(cp, sp, STATE_OPENED);
1305 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1310 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1313 printf(SPP_FMT "%s illegal %s in state %s\n",
1314 SPP_ARGS(ifp), cp->name,
1315 sppp_cp_type_name(h->type),
1316 sppp_state_name(sp->state[cp->protoidx]));
1321 if (h->ident != sp->confid[cp->protoidx]) {
1323 addlog(SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1324 SPP_ARGS(ifp), cp->name,
1325 h->ident, sp->confid[cp->protoidx]);
1329 switch (sp->state[cp->protoidx]) {
1332 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1335 case STATE_STOPPING:
1337 case STATE_REQ_SENT:
1338 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1339 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1344 case STATE_ACK_RCVD:
1346 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1348 case STATE_ACK_SENT:
1349 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1350 sppp_cp_change_state(cp, sp, STATE_OPENED);
1352 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
1353 SPP_ARGS(ifp), cp->name);
1357 printf(SPP_FMT "%s illegal %s in state %s\n",
1358 SPP_ARGS(ifp), cp->name,
1359 sppp_cp_type_name(h->type),
1360 sppp_state_name(sp->state[cp->protoidx]));
1366 if (h->ident != sp->confid[cp->protoidx]) {
1368 addlog(SPP_FMT "%s id mismatch 0x%x != 0x%x\n",
1369 SPP_ARGS(ifp), cp->name,
1370 h->ident, sp->confid[cp->protoidx]);
1374 if (h->type == CONF_NAK)
1375 (cp->RCN_nak)(sp, h, len);
1377 (cp->RCN_rej)(sp, h, len);
1379 switch (sp->state[cp->protoidx]) {
1382 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1384 case STATE_REQ_SENT:
1385 case STATE_ACK_SENT:
1386 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1392 case STATE_ACK_RCVD:
1393 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1397 case STATE_STOPPING:
1400 printf(SPP_FMT "%s illegal %s in state %s\n",
1401 SPP_ARGS(ifp), cp->name,
1402 sppp_cp_type_name(h->type),
1403 sppp_state_name(sp->state[cp->protoidx]));
1409 switch (sp->state[cp->protoidx]) {
1410 case STATE_ACK_RCVD:
1411 case STATE_ACK_SENT:
1412 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1417 case STATE_STOPPING:
1418 case STATE_REQ_SENT:
1420 /* Send Terminate-Ack packet. */
1422 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n",
1423 SPP_ARGS(ifp), cp->name);
1424 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0);
1428 sp->rst_counter[cp->protoidx] = 0;
1429 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1433 printf(SPP_FMT "%s illegal %s in state %s\n",
1434 SPP_ARGS(ifp), cp->name,
1435 sppp_cp_type_name(h->type),
1436 sppp_state_name(sp->state[cp->protoidx]));
1441 switch (sp->state[cp->protoidx]) {
1444 case STATE_REQ_SENT:
1445 case STATE_ACK_SENT:
1448 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1451 case STATE_STOPPING:
1452 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1455 case STATE_ACK_RCVD:
1456 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1461 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD);
1464 printf(SPP_FMT "%s illegal %s in state %s\n",
1465 SPP_ARGS(ifp), cp->name,
1466 sppp_cp_type_name(h->type),
1467 sppp_state_name(sp->state[cp->protoidx]));
1473 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */
1475 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, "
1476 "danger will robinson\n",
1477 SPP_ARGS(ifp), cp->name,
1478 sppp_cp_type_name(h->type), ntohs(*((u_short *)p)));
1479 switch (sp->state[cp->protoidx]) {
1482 case STATE_REQ_SENT:
1483 case STATE_ACK_SENT:
1485 case STATE_STOPPING:
1488 case STATE_ACK_RCVD:
1489 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1492 printf(SPP_FMT "%s illegal %s in state %s\n",
1493 SPP_ARGS(ifp), cp->name,
1494 sppp_cp_type_name(h->type),
1495 sppp_state_name(sp->state[cp->protoidx]));
1500 if (cp->proto != PPP_LCP)
1502 /* Discard the packet. */
1505 if (cp->proto != PPP_LCP)
1507 if (sp->state[cp->protoidx] != STATE_OPENED) {
1509 addlog(SPP_FMT "lcp echo req but lcp closed\n",
1516 addlog(SPP_FMT "invalid lcp echo request "
1517 "packet length: %d bytes\n",
1518 SPP_ARGS(ifp), len);
1521 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
1522 ntohl (*(long*)(h+1)) == sp->lcp.magic) {
1523 /* Line loopback mode detected. */
1524 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp));
1526 sppp_qflush (&sp->pp_cpq);
1528 /* Shut down the PPP link. */
1534 *(long*)(h+1) = htonl (sp->lcp.magic);
1536 addlog(SPP_FMT "got lcp echo req, sending echo rep\n",
1538 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1);
1541 if (cp->proto != PPP_LCP)
1543 if (h->ident != sp->lcp.echoid) {
1549 addlog(SPP_FMT "lcp invalid echo reply "
1550 "packet length: %d bytes\n",
1551 SPP_ARGS(ifp), len);
1555 addlog(SPP_FMT "lcp got echo rep\n",
1557 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) ||
1558 ntohl (*(long*)(h+1)) != sp->lcp.magic)
1559 sp->pp_alivecnt = 0;
1562 /* Unknown packet type -- send Code-Reject packet. */
1565 addlog(SPP_FMT "%s send code-rej for 0x%x\n",
1566 SPP_ARGS(ifp), cp->name, h->type);
1567 sppp_cp_send(sp, cp->proto, CODE_REJ, ++sp->pp_seq,
1568 m->m_pkthdr.len, h);
1575 * The generic part of all Up/Down/Open/Close/TO event handlers.
1576 * Basically, the state transition handling in the automaton.
1579 sppp_up_event(const struct cp *cp, struct sppp *sp)
1584 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n",
1585 SPP_ARGS(ifp), cp->name,
1586 sppp_state_name(sp->state[cp->protoidx]));
1588 switch (sp->state[cp->protoidx]) {
1590 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1592 case STATE_STARTING:
1593 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1595 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1598 printf(SPP_FMT "%s illegal up in state %s\n",
1599 SPP_ARGS(ifp), cp->name,
1600 sppp_state_name(sp->state[cp->protoidx]));
1605 sppp_down_event(const struct cp *cp, struct sppp *sp)
1610 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n",
1611 SPP_ARGS(ifp), cp->name,
1612 sppp_state_name(sp->state[cp->protoidx]));
1614 switch (sp->state[cp->protoidx]) {
1617 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1620 sppp_cp_change_state(cp, sp, STATE_STARTING);
1623 case STATE_STOPPING:
1624 case STATE_REQ_SENT:
1625 case STATE_ACK_RCVD:
1626 case STATE_ACK_SENT:
1627 sppp_cp_change_state(cp, sp, STATE_STARTING);
1631 sppp_cp_change_state(cp, sp, STATE_STARTING);
1634 printf(SPP_FMT "%s illegal down in state %s\n",
1635 SPP_ARGS(ifp), cp->name,
1636 sppp_state_name(sp->state[cp->protoidx]));
1642 sppp_open_event(const struct cp *cp, struct sppp *sp)
1647 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n",
1648 SPP_ARGS(ifp), cp->name,
1649 sppp_state_name(sp->state[cp->protoidx]));
1651 switch (sp->state[cp->protoidx]) {
1653 sppp_cp_change_state(cp, sp, STATE_STARTING);
1656 case STATE_STARTING:
1659 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure;
1661 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1664 case STATE_STOPPING:
1665 case STATE_REQ_SENT:
1666 case STATE_ACK_RCVD:
1667 case STATE_ACK_SENT:
1671 sppp_cp_change_state(cp, sp, STATE_STOPPING);
1678 sppp_close_event(const struct cp *cp, struct sppp *sp)
1683 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n",
1684 SPP_ARGS(ifp), cp->name,
1685 sppp_state_name(sp->state[cp->protoidx]));
1687 switch (sp->state[cp->protoidx]) {
1692 case STATE_STARTING:
1693 sppp_cp_change_state(cp, sp, STATE_INITIAL);
1697 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1699 case STATE_STOPPING:
1700 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1705 case STATE_REQ_SENT:
1706 case STATE_ACK_RCVD:
1707 case STATE_ACK_SENT:
1708 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate;
1709 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq, 0, 0);
1710 sppp_cp_change_state(cp, sp, STATE_CLOSING);
1716 sppp_to_event(const struct cp *cp, struct sppp *sp)
1723 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n",
1724 SPP_ARGS(ifp), cp->name,
1725 sppp_state_name(sp->state[cp->protoidx]),
1726 sp->rst_counter[cp->protoidx]);
1728 if (--sp->rst_counter[cp->protoidx] < 0)
1730 switch (sp->state[cp->protoidx]) {
1732 sppp_cp_change_state(cp, sp, STATE_CLOSED);
1735 case STATE_STOPPING:
1736 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1739 case STATE_REQ_SENT:
1740 case STATE_ACK_RCVD:
1741 case STATE_ACK_SENT:
1742 sppp_cp_change_state(cp, sp, STATE_STOPPED);
1748 switch (sp->state[cp->protoidx]) {
1750 case STATE_STOPPING:
1751 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq,
1753 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
1754 sp->ch[cp->protoidx]);
1756 case STATE_REQ_SENT:
1757 case STATE_ACK_RCVD:
1759 /* sppp_cp_change_state() will restart the timer */
1760 sppp_cp_change_state(cp, sp, STATE_REQ_SENT);
1762 case STATE_ACK_SENT:
1764 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
1765 sp->ch[cp->protoidx]);
1773 * Change the state of a control protocol in the state automaton.
1774 * Takes care of starting/stopping the restart timer.
1777 sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate)
1779 sp->state[cp->protoidx] = newstate;
1781 UNTIMEOUT(cp->TO, (void *)sp, sp->ch[cp->protoidx]);
1784 case STATE_STARTING:
1790 case STATE_STOPPING:
1791 case STATE_REQ_SENT:
1792 case STATE_ACK_RCVD:
1793 case STATE_ACK_SENT:
1794 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout,
1795 sp->ch[cp->protoidx]);
1800 *--------------------------------------------------------------------------*
1802 * The LCP implementation. *
1804 *--------------------------------------------------------------------------*
1807 sppp_lcp_init(struct sppp *sp)
1809 sp->lcp.opts = (1 << LCP_OPT_MAGIC);
1811 sp->state[IDX_LCP] = STATE_INITIAL;
1812 sp->fail_counter[IDX_LCP] = 0;
1814 sp->lcp.mru = sp->lcp.their_mru = PP_MTU;
1816 /* Note that these values are relevant for all control protocols */
1817 sp->lcp.timeout = 3 * hz;
1818 sp->lcp.max_terminate = 2;
1819 sp->lcp.max_configure = 10;
1820 sp->lcp.max_failure = 10;
1821 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
1822 callout_handle_init(&sp->ch[IDX_LCP]);
1827 sppp_lcp_up(struct sppp *sp)
1832 * If this interface is passive or dial-on-demand, and we are
1833 * still in Initial state, it means we've got an incoming
1834 * call. Activate the interface.
1836 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) {
1839 SPP_FMT "Up event", SPP_ARGS(ifp));
1840 ifp->if_flags |= IFF_RUNNING;
1841 if (sp->state[IDX_LCP] == STATE_INITIAL) {
1843 addlog("(incoming call)\n");
1844 sp->pp_flags |= PP_CALLIN;
1850 sppp_up_event(&lcp, sp);
1854 sppp_lcp_down(struct sppp *sp)
1858 sppp_down_event(&lcp, sp);
1861 * If this is neither a dial-on-demand nor a passive
1862 * interface, simulate an ``ifconfig down'' action, so the
1863 * administrator can force a redial by another ``ifconfig
1864 * up''. XXX For leased line operation, should we immediately
1865 * try to reopen the connection here?
1867 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) {
1869 SPP_FMT "Down event, taking interface down.\n",
1875 SPP_FMT "Down event (carrier loss)\n",
1878 sp->pp_flags &= ~PP_CALLIN;
1879 if (sp->state[IDX_LCP] != STATE_INITIAL)
1881 ifp->if_flags &= ~IFF_RUNNING;
1885 sppp_lcp_open(struct sppp *sp)
1888 * If we are authenticator, negotiate LCP_AUTH
1890 if (sp->hisauth.proto != 0)
1891 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO);
1893 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
1894 sp->pp_flags &= ~PP_NEEDAUTH;
1895 sppp_open_event(&lcp, sp);
1899 sppp_lcp_close(struct sppp *sp)
1901 sppp_close_event(&lcp, sp);
1905 sppp_lcp_TO(void *cookie)
1907 sppp_to_event(&lcp, (struct sppp *)cookie);
1911 * Analyze a configure request. Return true if it was agreeable, and
1912 * caused action sca, false if it has been rejected or nak'ed, and
1913 * caused action scn. (The return value is used to make the state
1914 * transition decision in the state automaton.)
1917 sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
1920 u_char *buf, *r, *p;
1927 buf = r = malloc (len, M_TEMP, M_NOWAIT);
1932 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ",
1935 /* pass 1: check for things that need to be rejected */
1937 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
1939 addlog(" %s ", sppp_lcp_opt_name(*p));
1943 /* fall through, both are same length */
1944 case LCP_OPT_ASYNC_MAP:
1945 /* Async control character map. */
1946 if (len >= 6 || p[1] == 6)
1949 addlog("[invalid] ");
1952 /* Maximum receive unit. */
1953 if (len >= 4 && p[1] == 4)
1956 addlog("[invalid] ");
1958 case LCP_OPT_AUTH_PROTO:
1961 addlog("[invalid] ");
1964 authproto = (p[2] << 8) + p[3];
1965 if (authproto == PPP_CHAP && p[1] != 5) {
1967 addlog("[invalid chap len] ");
1970 if (sp->myauth.proto == 0) {
1971 /* we are not configured to do auth */
1973 addlog("[not configured] ");
1977 * Remote want us to authenticate, remember this,
1978 * so we stay in PHASE_AUTHENTICATE after LCP got
1981 sp->pp_flags |= PP_NEEDAUTH;
1984 /* Others not supported. */
1989 /* Add the option to rejected list. */
1996 addlog(" send conf-rej\n");
1997 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2003 * pass 2: check for option values that are unacceptable and
2004 * thus require to be nak'ed.
2007 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ",
2012 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2014 addlog(" %s ", sppp_lcp_opt_name(*p));
2017 /* Magic number -- extract. */
2018 nmagic = (u_long)p[2] << 24 |
2019 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2020 if (nmagic != sp->lcp.magic) {
2022 addlog("0x%lx ", nmagic);
2026 * Local and remote magics equal -- loopback?
2028 if (sp->pp_loopcnt >= MAXALIVECNT*5) {
2029 printf (SPP_FMT "loopback\n",
2032 if (ifp->if_flags & IFF_UP) {
2034 sppp_qflush(&sp->pp_cpq);
2040 addlog("[glitch] ");
2043 * We negate our magic here, and NAK it. If
2044 * we see it later in an NAK packet, we
2045 * suggest a new one.
2047 nmagic = ~sp->lcp.magic;
2049 p[2] = nmagic >> 24;
2050 p[3] = nmagic >> 16;
2055 case LCP_OPT_ASYNC_MAP:
2056 /* Async control character map -- check to be zero. */
2057 if (! p[2] && ! p[3] && ! p[4] && ! p[5]) {
2063 addlog("[non-empty] ");
2064 /* suggest a zero one */
2065 p[2] = p[3] = p[4] = p[5] = 0;
2070 * Maximum receive unit. Always agreeable,
2071 * but ignored by now.
2073 sp->lcp.their_mru = p[2] * 256 + p[3];
2075 addlog("%lu ", sp->lcp.their_mru);
2078 case LCP_OPT_AUTH_PROTO:
2079 authproto = (p[2] << 8) + p[3];
2080 if (sp->myauth.proto != authproto) {
2081 /* not agreed, nak */
2083 addlog("[mine %s != his %s] ",
2084 sppp_proto_name(sp->hisauth.proto),
2085 sppp_proto_name(authproto));
2086 p[2] = sp->myauth.proto >> 8;
2087 p[3] = sp->myauth.proto;
2090 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) {
2092 addlog("[chap not MD5] ");
2098 /* Add the option to nak'ed list. */
2104 if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) {
2106 addlog(" max_failure (%d) exceeded, "
2108 sp->lcp.max_failure);
2109 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf);
2112 addlog(" send conf-nak\n");
2113 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf);
2118 addlog(" send conf-ack\n");
2119 sp->fail_counter[IDX_LCP] = 0;
2121 sppp_cp_send (sp, PPP_LCP, CONF_ACK,
2122 h->ident, origlen, h+1);
2130 * Analyze the LCP Configure-Reject option list, and adjust our
2134 sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2140 buf = malloc (len, M_TEMP, M_NOWAIT);
2145 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ",
2149 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2151 addlog(" %s ", sppp_lcp_opt_name(*p));
2154 /* Magic number -- can't use it, use 0 */
2155 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC);
2160 * Should not be rejected anyway, since we only
2161 * negotiate a MRU if explicitly requested by
2164 sp->lcp.opts &= ~(1 << LCP_OPT_MRU);
2166 case LCP_OPT_AUTH_PROTO:
2168 * Peer doesn't want to authenticate himself,
2169 * deny unless this is a dialout call, and
2170 * AUTHFLAG_NOCALLOUT is set.
2172 if ((sp->pp_flags & PP_CALLIN) == 0 &&
2173 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) {
2175 addlog("[don't insist on auth "
2177 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO);
2181 addlog("[access denied]\n");
2193 * Analyze the LCP Configure-NAK option list, and adjust our
2197 sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2204 buf = malloc (len, M_TEMP, M_NOWAIT);
2209 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ",
2213 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2215 addlog(" %s ", sppp_lcp_opt_name(*p));
2218 /* Magic number -- renegotiate */
2219 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) &&
2220 len >= 6 && p[1] == 6) {
2221 magic = (u_long)p[2] << 24 |
2222 (u_long)p[3] << 16 | p[4] << 8 | p[5];
2224 * If the remote magic is our negated one,
2225 * this looks like a loopback problem.
2226 * Suggest a new magic to make sure.
2228 if (magic == ~sp->lcp.magic) {
2230 addlog("magic glitch ");
2231 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2232 sp->lcp.magic = random();
2234 sp->lcp.magic = time.tv_sec + time.tv_usec;
2237 sp->lcp.magic = magic;
2239 addlog("%lu ", magic);
2245 * Peer wants to advise us to negotiate an MRU.
2246 * Agree on it if it's reasonable, or use
2247 * default otherwise.
2249 if (len >= 4 && p[1] == 4) {
2250 u_int mru = p[2] * 256 + p[3];
2253 if (mru < PP_MTU || mru > PP_MAX_MRU)
2256 sp->lcp.opts |= (1 << LCP_OPT_MRU);
2259 case LCP_OPT_AUTH_PROTO:
2261 * Peer doesn't like our authentication method,
2265 addlog("[access denied]\n");
2277 sppp_lcp_tlu(struct sppp *sp)
2284 if (! (ifp->if_flags & IFF_UP) &&
2285 (ifp->if_flags & IFF_RUNNING)) {
2286 /* Coming out of loopback mode. */
2288 printf (SPP_FMT "up\n", SPP_ARGS(ifp));
2291 for (i = 0; i < IDX_COUNT; i++)
2292 if ((cps[i])->flags & CP_QUAL)
2295 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 ||
2296 (sp->pp_flags & PP_NEEDAUTH) != 0)
2297 sp->pp_phase = PHASE_AUTHENTICATE;
2299 sp->pp_phase = PHASE_NETWORK;
2302 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2303 sppp_phase_name(sp->pp_phase));
2306 * Open all authentication protocols. This is even required
2307 * if we already proceeded to network phase, since it might be
2308 * that remote wants us to authenticate, so we might have to
2309 * send a PAP request. Undesired authentication protocols
2310 * don't do anything when they get an Open event.
2312 for (i = 0; i < IDX_COUNT; i++)
2313 if ((cps[i])->flags & CP_AUTH)
2316 if (sp->pp_phase == PHASE_NETWORK) {
2317 /* Notify all NCPs. */
2318 for (i = 0; i < IDX_COUNT; i++)
2319 if ((cps[i])->flags & CP_NCP)
2323 /* Send Up events to all started protos. */
2324 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2325 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0)
2328 /* notify low-level driver of state change */
2330 sp->pp_chg(sp, (int)sp->pp_phase);
2332 if (sp->pp_phase == PHASE_NETWORK)
2333 /* if no NCP is starting, close down */
2334 sppp_lcp_check_and_close(sp);
2338 sppp_lcp_tld(struct sppp *sp)
2344 sp->pp_phase = PHASE_TERMINATE;
2347 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2348 sppp_phase_name(sp->pp_phase));
2351 * Take upper layers down. We send the Down event first and
2352 * the Close second to prevent the upper layers from sending
2353 * ``a flurry of terminate-request packets'', as the RFC
2356 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2357 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0) {
2359 (cps[i])->Close(sp);
2364 sppp_lcp_tls(struct sppp *sp)
2368 sp->pp_phase = PHASE_ESTABLISH;
2371 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2372 sppp_phase_name(sp->pp_phase));
2374 /* Notify lower layer if desired. */
2382 sppp_lcp_tlf(struct sppp *sp)
2386 sp->pp_phase = PHASE_DEAD;
2388 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
2389 sppp_phase_name(sp->pp_phase));
2391 /* Notify lower layer if desired. */
2399 sppp_lcp_scr(struct sppp *sp)
2401 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */];
2405 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) {
2406 if (! sp->lcp.magic)
2407 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2408 sp->lcp.magic = random();
2410 sp->lcp.magic = time.tv_sec + time.tv_usec;
2412 opt[i++] = LCP_OPT_MAGIC;
2414 opt[i++] = sp->lcp.magic >> 24;
2415 opt[i++] = sp->lcp.magic >> 16;
2416 opt[i++] = sp->lcp.magic >> 8;
2417 opt[i++] = sp->lcp.magic;
2420 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) {
2421 opt[i++] = LCP_OPT_MRU;
2423 opt[i++] = sp->lcp.mru >> 8;
2424 opt[i++] = sp->lcp.mru;
2427 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) {
2428 authproto = sp->hisauth.proto;
2429 opt[i++] = LCP_OPT_AUTH_PROTO;
2430 opt[i++] = authproto == PPP_CHAP? 5: 4;
2431 opt[i++] = authproto >> 8;
2432 opt[i++] = authproto;
2433 if (authproto == PPP_CHAP)
2434 opt[i++] = CHAP_MD5;
2437 sp->confid[IDX_LCP] = ++sp->pp_seq;
2438 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt);
2442 * Check the open NCPs, return true if at least one NCP is open.
2445 sppp_ncp_check(struct sppp *sp)
2449 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
2450 if (sp->lcp.protos & mask && (cps[i])->flags & CP_NCP)
2456 * Re-check the open NCPs and see if we should terminate the link.
2457 * Called by the NCPs during their tlf action handling.
2460 sppp_lcp_check_and_close(struct sppp *sp)
2463 if (sp->pp_phase < PHASE_NETWORK)
2464 /* don't bother, we are already going down */
2467 if (sppp_ncp_check(sp))
2473 *--------------------------------------------------------------------------*
2475 * The IPCP implementation. *
2477 *--------------------------------------------------------------------------*
2481 sppp_ipcp_init(struct sppp *sp)
2485 sp->state[IDX_IPCP] = STATE_INITIAL;
2486 sp->fail_counter[IDX_IPCP] = 0;
2487 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
2488 callout_handle_init(&sp->ch[IDX_IPCP]);
2493 sppp_ipcp_up(struct sppp *sp)
2495 sppp_up_event(&ipcp, sp);
2499 sppp_ipcp_down(struct sppp *sp)
2501 sppp_down_event(&ipcp, sp);
2505 sppp_ipcp_open(struct sppp *sp)
2508 u_long myaddr, hisaddr;
2510 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN|IPCP_MYADDR_SEEN|IPCP_MYADDR_DYN);
2512 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0);
2514 * If we don't have his address, this probably means our
2515 * interface doesn't want to talk IP at all. (This could
2516 * be the case if somebody wants to speak only IPX, for
2517 * example.) Don't open IPCP in this case.
2519 if (hisaddr == 0L) {
2520 /* XXX this message should go away */
2522 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n",
2529 * I don't have an assigned address, so i need to
2530 * negotiate my address.
2532 sp->ipcp.flags |= IPCP_MYADDR_DYN;
2533 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2535 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2536 sppp_open_event(&ipcp, sp);
2540 sppp_ipcp_close(struct sppp *sp)
2542 sppp_close_event(&ipcp, sp);
2543 if (sp->ipcp.flags & IPCP_MYADDR_DYN)
2545 * My address was dynamic, clear it again.
2547 sppp_set_ip_addr(sp, 0L);
2551 sppp_ipcp_TO(void *cookie)
2553 sppp_to_event(&ipcp, (struct sppp *)cookie);
2557 * Analyze a configure request. Return true if it was agreeable, and
2558 * caused action sca, false if it has been rejected or nak'ed, and
2559 * caused action scn. (The return value is used to make the state
2560 * transition decision in the state automaton.)
2563 sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len)
2565 u_char *buf, *r, *p;
2566 struct ifnet *ifp = &sp->pp_if;
2567 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG;
2568 u_long hisaddr, desiredaddr;
2574 * Make sure to allocate a buf that can at least hold a
2575 * conf-nak with an `address' option. We might need it below.
2577 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT);
2581 /* pass 1: see if we can recognize them */
2583 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ",
2586 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2588 addlog(" %s ", sppp_ipcp_opt_name(*p));
2590 case IPCP_OPT_ADDRESS:
2591 if (len >= 6 && p[1] == 6) {
2592 /* correctly formed address option */
2596 addlog("[invalid] ");
2599 /* Others not supported. */
2604 /* Add the option to rejected list. */
2611 addlog(" send conf-rej\n");
2612 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf);
2617 /* pass 2: parse option values */
2618 sppp_get_ip_addrs(sp, 0, &hisaddr, 0);
2620 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ",
2624 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) {
2626 addlog(" %s ", sppp_ipcp_opt_name(*p));
2628 case IPCP_OPT_ADDRESS:
2629 /* This is the address he wants in his end */
2630 desiredaddr = p[2] << 24 | p[3] << 16 |
2632 if (desiredaddr == hisaddr ||
2633 (hisaddr == 1 && desiredaddr != 0)) {
2635 * Peer's address is same as our value,
2636 * or we have set it to 0.0.0.1 to
2637 * indicate that we do not really care,
2638 * this is agreeable. Gonna conf-ack
2643 sppp_dotted_quad(hisaddr));
2644 /* record that we've seen it already */
2645 sp->ipcp.flags |= IPCP_HISADDR_SEEN;
2649 * The address wasn't agreeable. This is either
2650 * he sent us 0.0.0.0, asking to assign him an
2651 * address, or he send us another address not
2652 * matching our value. Either case, we gonna
2653 * conf-nak it with our value.
2654 * XXX: we should "rej" if hisaddr == 0
2657 if (desiredaddr == 0)
2658 addlog("[addr requested] ");
2660 addlog("%s [not agreed] ",
2661 sppp_dotted_quad(desiredaddr));
2664 p[2] = hisaddr >> 24;
2665 p[3] = hisaddr >> 16;
2666 p[4] = hisaddr >> 8;
2670 /* Add the option to nak'ed list. */
2677 * If we are about to conf-ack the request, but haven't seen
2678 * his address so far, gonna conf-nak it instead, with the
2679 * `address' option present and our idea of his address being
2680 * filled in there, to request negotiation of both addresses.
2682 * XXX This can result in an endless req - nak loop if peer
2683 * doesn't want to send us his address. Q: What should we do
2684 * about it? XXX A: implement the max-failure counter.
2686 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) {
2687 buf[0] = IPCP_OPT_ADDRESS;
2689 buf[2] = hisaddr >> 24;
2690 buf[3] = hisaddr >> 16;
2691 buf[4] = hisaddr >> 8;
2695 addlog("still need hisaddr ");
2700 addlog(" send conf-nak\n");
2701 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf);
2704 addlog(" send conf-ack\n");
2705 sppp_cp_send (sp, PPP_IPCP, CONF_ACK,
2706 h->ident, origlen, h+1);
2714 * Analyze the IPCP Configure-Reject option list, and adjust our
2718 sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len)
2721 struct ifnet *ifp = &sp->pp_if;
2722 int debug = ifp->if_flags & IFF_DEBUG;
2725 buf = malloc (len, M_TEMP, M_NOWAIT);
2730 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ",
2734 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2736 addlog(" %s ", sppp_ipcp_opt_name(*p));
2738 case IPCP_OPT_ADDRESS:
2740 * Peer doesn't grok address option. This is
2741 * bad. XXX Should we better give up here?
2742 * XXX We could try old "addresses" option...
2744 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS);
2755 * Analyze the IPCP Configure-NAK option list, and adjust our
2759 sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len)
2762 struct ifnet *ifp = &sp->pp_if;
2763 int debug = ifp->if_flags & IFF_DEBUG;
2767 buf = malloc (len, M_TEMP, M_NOWAIT);
2772 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ",
2776 for (; len > 1 && p[1]; len -= p[1], p += p[1]) {
2778 addlog(" %s ", sppp_ipcp_opt_name(*p));
2780 case IPCP_OPT_ADDRESS:
2782 * Peer doesn't like our local IP address. See
2783 * if we can do something for him. We'll drop
2784 * him our address then.
2786 if (len >= 6 && p[1] == 6) {
2787 wantaddr = p[2] << 24 | p[3] << 16 |
2789 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS);
2791 addlog("[wantaddr %s] ",
2792 sppp_dotted_quad(wantaddr));
2794 * When doing dynamic address assignment,
2795 * we accept his offer. Otherwise, we
2796 * ignore it and thus continue to negotiate
2797 * our already existing value.
2798 * XXX: Bogus, if he said no once, he'll
2799 * just say no again, might as well die.
2801 if (sp->ipcp.flags & IPCP_MYADDR_DYN) {
2802 sppp_set_ip_addr(sp, wantaddr);
2805 sp->ipcp.flags |= IPCP_MYADDR_SEEN;
2818 sppp_ipcp_tlu(struct sppp *sp)
2820 /* we are up - notify isdn daemon */
2826 sppp_ipcp_tld(struct sppp *sp)
2831 sppp_ipcp_tls(struct sppp *sp)
2833 /* indicate to LCP that it must stay alive */
2834 sp->lcp.protos |= (1 << IDX_IPCP);
2838 sppp_ipcp_tlf(struct sppp *sp)
2840 /* we no longer need LCP */
2841 sp->lcp.protos &= ~(1 << IDX_IPCP);
2842 sppp_lcp_check_and_close(sp);
2846 sppp_ipcp_scr(struct sppp *sp)
2848 char opt[6 /* compression */ + 6 /* address */];
2852 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) {
2853 sppp_get_ip_addrs(sp, &ouraddr, 0, 0);
2854 opt[i++] = IPCP_OPT_ADDRESS;
2856 opt[i++] = ouraddr >> 24;
2857 opt[i++] = ouraddr >> 16;
2858 opt[i++] = ouraddr >> 8;
2862 sp->confid[IDX_IPCP] = ++sp->pp_seq;
2863 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt);
2868 *--------------------------------------------------------------------------*
2870 * The CHAP implementation. *
2872 *--------------------------------------------------------------------------*
2876 * The authentication protocols don't employ a full-fledged state machine as
2877 * the control protocols do, since they do have Open and Close events, but
2878 * not Up and Down, nor are they explicitly terminated. Also, use of the
2879 * authentication protocols may be different in both directions (this makes
2880 * sense, think of a machine that never accepts incoming calls but only
2881 * calls out, it doesn't require the called party to authenticate itself).
2883 * Our state machine for the local authentication protocol (we are requesting
2884 * the peer to authenticate) looks like:
2887 * +--------------------------------------------+
2889 * +--------+ Close +---------+ RCA+
2890 * | |<----------------------------------| |------+
2891 * +--->| Closed | TO* | Opened | sca |
2892 * | | |-----+ +-------| |<-----+
2893 * | +--------+ irc | | +---------+
2899 * | | +------->+ | |
2901 * | +--------+ V | |
2902 * | | |<----+<--------------------+ |
2908 * +------+ +------------------------------------------+
2909 * scn,tld sca,irc,ict,tlu
2914 * Open: LCP reached authentication phase
2915 * Close: LCP reached terminate phase
2917 * RCA+: received reply (pap-req, chap-response), acceptable
2918 * RCN: received reply (pap-req, chap-response), not acceptable
2919 * TO+: timeout with restart counter >= 0
2920 * TO-: timeout with restart counter < 0
2921 * TO*: reschedule timeout for CHAP
2923 * scr: send request packet (none for PAP, chap-challenge)
2924 * sca: send ack packet (pap-ack, chap-success)
2925 * scn: send nak packet (pap-nak, chap-failure)
2926 * ict: initialize re-challenge timer (CHAP only)
2928 * tlu: this-layer-up, LCP reaches network phase
2929 * tld: this-layer-down, LCP enters terminate phase
2931 * Note that in CHAP mode, after sending a new challenge, while the state
2932 * automaton falls back into Req-Sent state, it doesn't signal a tld
2933 * event to LCP, so LCP remains in network phase. Only after not getting
2934 * any response (or after getting an unacceptable response), CHAP closes,
2935 * causing LCP to enter terminate phase.
2937 * With PAP, there is no initial request that can be sent. The peer is
2938 * expected to send one based on the successful negotiation of PAP as
2939 * the authentication protocol during the LCP option negotiation.
2941 * Incoming authentication protocol requests (remote requests
2942 * authentication, we are peer) don't employ a state machine at all,
2943 * they are simply answered. Some peers [Ascend P50 firmware rev
2944 * 4.50] react allergically when sending IPCP requests while they are
2945 * still in authentication phase (thereby violating the standard that
2946 * demands that these NCP packets are to be discarded), so we keep
2947 * track of the peer demanding us to authenticate, and only proceed to
2948 * phase network once we've seen a positive acknowledge for the
2953 * Handle incoming CHAP packets.
2956 sppp_chap_input(struct sppp *sp, struct mbuf *m)
2959 struct lcp_header *h;
2961 u_char *value, *name, digest[AUTHKEYLEN], dsize;
2962 int value_len, name_len;
2965 len = m->m_pkthdr.len;
2969 SPP_FMT "chap invalid packet length: %d bytes\n",
2970 SPP_ARGS(ifp), len);
2973 h = mtod (m, struct lcp_header*);
2974 if (len > ntohs (h->len))
2975 len = ntohs (h->len);
2978 /* challenge, failure and success are his authproto */
2979 case CHAP_CHALLENGE:
2980 value = 1 + (u_char*)(h+1);
2981 value_len = value[-1];
2982 name = value + value_len;
2983 name_len = len - value_len - 5;
2987 SPP_FMT "chap corrupted challenge "
2988 "<%s id=0x%x len=%d",
2990 sppp_auth_type_name(PPP_CHAP, h->type),
2991 h->ident, ntohs(h->len));
2992 sppp_print_bytes((u_char*) (h+1), len-4);
3000 SPP_FMT "chap input <%s id=0x%x len=%d name=",
3002 sppp_auth_type_name(PPP_CHAP, h->type), h->ident,
3004 sppp_print_string((char*) name, name_len);
3005 addlog(" value-size=%d value=", value_len);
3006 sppp_print_bytes(value, value_len);
3010 /* Compute reply value. */
3012 MD5Update(&ctx, &h->ident, 1);
3013 MD5Update(&ctx, sp->myauth.secret,
3014 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN));
3015 MD5Update(&ctx, value, value_len);
3016 MD5Final(digest, &ctx);
3017 dsize = sizeof digest;
3019 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident,
3020 sizeof dsize, (const char *)&dsize,
3021 sizeof digest, digest,
3022 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3029 log(LOG_DEBUG, SPP_FMT "chap success",
3033 sppp_print_string((char*)(h + 1), len - 4);
3038 sp->pp_flags &= ~PP_NEEDAUTH;
3039 if (sp->myauth.proto == PPP_CHAP &&
3040 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
3041 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) {
3043 * We are authenticator for CHAP but didn't
3044 * complete yet. Leave it to tlu to proceed
3051 sppp_phase_network(sp);
3056 log(LOG_INFO, SPP_FMT "chap failure",
3060 sppp_print_string((char*)(h + 1), len - 4);
3064 log(LOG_INFO, SPP_FMT "chap failure\n",
3066 /* await LCP shutdown by authenticator */
3069 /* response is my authproto */
3071 value = 1 + (u_char*)(h+1);
3072 value_len = value[-1];
3073 name = value + value_len;
3074 name_len = len - value_len - 5;
3078 SPP_FMT "chap corrupted response "
3079 "<%s id=0x%x len=%d",
3081 sppp_auth_type_name(PPP_CHAP, h->type),
3082 h->ident, ntohs(h->len));
3083 sppp_print_bytes((u_char*)(h+1), len-4);
3088 if (h->ident != sp->confid[IDX_CHAP]) {
3091 SPP_FMT "chap dropping response for old ID "
3092 "(got %d, expected %d)\n",
3094 h->ident, sp->confid[IDX_CHAP]);
3097 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)
3098 || bcmp(name, sp->hisauth.name, name_len) != 0) {
3099 log(LOG_INFO, SPP_FMT "chap response, his name ",
3101 sppp_print_string(name, name_len);
3102 addlog(" != expected ");
3103 sppp_print_string(sp->hisauth.name,
3104 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN));
3108 log(LOG_DEBUG, SPP_FMT "chap input(%s) "
3109 "<%s id=0x%x len=%d name=",
3111 sppp_state_name(sp->state[IDX_CHAP]),
3112 sppp_auth_type_name(PPP_CHAP, h->type),
3113 h->ident, ntohs (h->len));
3114 sppp_print_string((char*)name, name_len);
3115 addlog(" value-size=%d value=", value_len);
3116 sppp_print_bytes(value, value_len);
3119 if (value_len != AUTHKEYLEN) {
3122 SPP_FMT "chap bad hash value length: "
3123 "%d bytes, should be %d\n",
3124 SPP_ARGS(ifp), value_len,
3130 MD5Update(&ctx, &h->ident, 1);
3131 MD5Update(&ctx, sp->hisauth.secret,
3132 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN));
3133 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN);
3134 MD5Final(digest, &ctx);
3136 #define FAILMSG "Failed..."
3137 #define SUCCMSG "Welcome!"
3139 if (value_len != sizeof digest ||
3140 bcmp(digest, value, value_len) != 0) {
3141 /* action scn, tld */
3142 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident,
3143 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3148 /* action sca, perhaps tlu */
3149 if (sp->state[IDX_CHAP] == STATE_REQ_SENT ||
3150 sp->state[IDX_CHAP] == STATE_OPENED)
3151 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident,
3152 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3154 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) {
3155 sppp_cp_change_state(&chap, sp, STATE_OPENED);
3161 /* Unknown CHAP packet type -- ignore. */
3163 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) "
3164 "<0x%x id=0x%xh len=%d",
3166 sppp_state_name(sp->state[IDX_CHAP]),
3167 h->type, h->ident, ntohs(h->len));
3168 sppp_print_bytes((u_char*)(h+1), len-4);
3177 sppp_chap_init(struct sppp *sp)
3179 /* Chap doesn't have STATE_INITIAL at all. */
3180 sp->state[IDX_CHAP] = STATE_CLOSED;
3181 sp->fail_counter[IDX_CHAP] = 0;
3182 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3183 callout_handle_init(&sp->ch[IDX_CHAP]);
3188 sppp_chap_open(struct sppp *sp)
3190 if (sp->myauth.proto == PPP_CHAP &&
3191 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3192 /* we are authenticator for CHAP, start it */
3194 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3195 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3197 /* nothing to be done if we are peer, await a challenge */
3201 sppp_chap_close(struct sppp *sp)
3203 if (sp->state[IDX_CHAP] != STATE_CLOSED)
3204 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3208 sppp_chap_TO(void *cookie)
3210 struct sppp *sp = (struct sppp *)cookie;
3216 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n",
3218 sppp_state_name(sp->state[IDX_CHAP]),
3219 sp->rst_counter[IDX_CHAP]);
3221 if (--sp->rst_counter[IDX_CHAP] < 0)
3223 switch (sp->state[IDX_CHAP]) {
3224 case STATE_REQ_SENT:
3226 sppp_cp_change_state(&chap, sp, STATE_CLOSED);
3230 /* TO+ (or TO*) event */
3231 switch (sp->state[IDX_CHAP]) {
3234 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3236 case STATE_REQ_SENT:
3238 /* sppp_cp_change_state() will restart the timer */
3239 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT);
3247 sppp_chap_tlu(struct sppp *sp)
3253 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure;
3256 * Some broken CHAP implementations (Conware CoNet, firmware
3257 * 4.0.?) don't want to re-authenticate their CHAP once the
3258 * initial challenge-response exchange has taken place.
3259 * Provide for an option to avoid rechallenges.
3261 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) {
3263 * Compute the re-challenge timeout. This will yield
3264 * a number between 300 and 810 seconds.
3266 i = 300 + ((unsigned)(random() & 0xff00) >> 7);
3267 TIMEOUT(chap.TO, (void *)sp, i * hz, sp->ch[IDX_CHAP]);
3272 SPP_FMT "chap %s, ",
3274 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu");
3275 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0)
3276 addlog("next re-challenge in %d seconds\n", i);
3278 addlog("re-challenging supressed\n");
3282 /* indicate to LCP that we need to be closed down */
3283 sp->lcp.protos |= (1 << IDX_CHAP);
3285 if (sp->pp_flags & PP_NEEDAUTH) {
3287 * Remote is authenticator, but his auth proto didn't
3288 * complete yet. Defer the transition to network
3297 * If we are already in phase network, we are done here. This
3298 * is the case if this is a dummy tlu event after a re-challenge.
3300 if (sp->pp_phase != PHASE_NETWORK)
3301 sppp_phase_network(sp);
3305 sppp_chap_tld(struct sppp *sp)
3310 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp));
3311 UNTIMEOUT(chap.TO, (void *)sp, sp->ch[IDX_CHAP]);
3312 sp->lcp.protos &= ~(1 << IDX_CHAP);
3318 sppp_chap_scr(struct sppp *sp)
3323 /* Compute random challenge. */
3324 ch = (u_long *)sp->myauth.challenge;
3325 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3326 read_random(&seed, sizeof seed);
3331 seed = tv.tv_sec ^ tv.tv_usec;
3334 ch[0] = seed ^ random();
3335 ch[1] = seed ^ random();
3336 ch[2] = seed ^ random();
3337 ch[3] = seed ^ random();
3340 sp->confid[IDX_CHAP] = ++sp->pp_seq;
3342 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP],
3343 sizeof clen, (const char *)&clen,
3344 (size_t)AUTHKEYLEN, sp->myauth.challenge,
3345 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN),
3350 *--------------------------------------------------------------------------*
3352 * The PAP implementation. *
3354 *--------------------------------------------------------------------------*
3357 * For PAP, we need to keep a little state also if we are the peer, not the
3358 * authenticator. This is since we don't get a request to authenticate, but
3359 * have to repeatedly authenticate ourself until we got a response (or the
3360 * retry counter is expired).
3364 * Handle incoming PAP packets. */
3366 sppp_pap_input(struct sppp *sp, struct mbuf *m)
3369 struct lcp_header *h;
3371 u_char *name, *passwd, mlen;
3372 int name_len, passwd_len;
3374 len = m->m_pkthdr.len;
3378 SPP_FMT "pap invalid packet length: %d bytes\n",
3379 SPP_ARGS(ifp), len);
3382 h = mtod (m, struct lcp_header*);
3383 if (len > ntohs (h->len))
3384 len = ntohs (h->len);
3386 /* PAP request is my authproto */
3388 name = 1 + (u_char*)(h+1);
3389 name_len = name[-1];
3390 passwd = name + name_len + 1;
3391 if (name_len > len - 6 ||
3392 (passwd_len = passwd[-1]) > len - 6 - name_len) {
3394 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
3395 "<%s id=0x%x len=%d",
3397 sppp_auth_type_name(PPP_PAP, h->type),
3398 h->ident, ntohs(h->len));
3399 sppp_print_bytes((u_char*)(h+1), len-4);
3405 log(LOG_DEBUG, SPP_FMT "pap input(%s) "
3406 "<%s id=0x%x len=%d name=",
3408 sppp_state_name(sp->state[IDX_PAP]),
3409 sppp_auth_type_name(PPP_PAP, h->type),
3410 h->ident, ntohs(h->len));
3411 sppp_print_string((char*)name, name_len);
3413 sppp_print_string((char*)passwd, passwd_len);
3416 if (name_len > AUTHNAMELEN ||
3417 passwd_len > AUTHKEYLEN ||
3418 bcmp(name, sp->hisauth.name, name_len) != 0 ||
3419 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) {
3420 /* action scn, tld */
3421 mlen = sizeof(FAILMSG) - 1;
3422 sppp_auth_send(&pap, sp, PAP_NAK, h->ident,
3423 sizeof mlen, (const char *)&mlen,
3424 sizeof(FAILMSG) - 1, (u_char *)FAILMSG,
3429 /* action sca, perhaps tlu */
3430 if (sp->state[IDX_PAP] == STATE_REQ_SENT ||
3431 sp->state[IDX_PAP] == STATE_OPENED) {
3432 mlen = sizeof(SUCCMSG) - 1;
3433 sppp_auth_send(&pap, sp, PAP_ACK, h->ident,
3434 sizeof mlen, (const char *)&mlen,
3435 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG,
3438 if (sp->state[IDX_PAP] == STATE_REQ_SENT) {
3439 sppp_cp_change_state(&pap, sp, STATE_OPENED);
3444 /* ack and nak are his authproto */
3446 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3448 log(LOG_DEBUG, SPP_FMT "pap success",
3450 name_len = *((char *)h);
3451 if (len > 5 && name_len) {
3453 sppp_print_string((char*)(h+1), name_len);
3458 sp->pp_flags &= ~PP_NEEDAUTH;
3459 if (sp->myauth.proto == PPP_PAP &&
3460 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) &&
3461 (sp->lcp.protos & (1 << IDX_PAP)) == 0) {
3463 * We are authenticator for PAP but didn't
3464 * complete yet. Leave it to tlu to proceed
3471 sppp_phase_network(sp);
3475 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3477 log(LOG_INFO, SPP_FMT "pap failure",
3479 name_len = *((char *)h);
3480 if (len > 5 && name_len) {
3482 sppp_print_string((char*)(h+1), name_len);
3486 log(LOG_INFO, SPP_FMT "pap failure\n",
3488 /* await LCP shutdown by authenticator */
3492 /* Unknown PAP packet type -- ignore. */
3494 log(LOG_DEBUG, SPP_FMT "pap corrupted input "
3495 "<0x%x id=0x%x len=%d",
3497 h->type, h->ident, ntohs(h->len));
3498 sppp_print_bytes((u_char*)(h+1), len-4);
3507 sppp_pap_init(struct sppp *sp)
3509 /* PAP doesn't have STATE_INITIAL at all. */
3510 sp->state[IDX_PAP] = STATE_CLOSED;
3511 sp->fail_counter[IDX_PAP] = 0;
3512 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3513 callout_handle_init(&sp->ch[IDX_PAP]);
3514 callout_handle_init(&sp->pap_my_to_ch);
3519 sppp_pap_open(struct sppp *sp)
3521 if (sp->hisauth.proto == PPP_PAP &&
3522 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) {
3523 /* we are authenticator for PAP, start our timer */
3524 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
3525 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
3527 if (sp->myauth.proto == PPP_PAP) {
3528 /* we are peer, send a request, and start a timer */
3530 TIMEOUT(sppp_pap_my_TO, (void *)sp, sp->lcp.timeout,
3536 sppp_pap_close(struct sppp *sp)
3538 if (sp->state[IDX_PAP] != STATE_CLOSED)
3539 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
3543 * That's the timeout routine if we are authenticator. Since the
3544 * authenticator is basically passive in PAP, we can't do much here.
3547 sppp_pap_TO(void *cookie)
3549 struct sppp *sp = (struct sppp *)cookie;
3555 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n",
3557 sppp_state_name(sp->state[IDX_PAP]),
3558 sp->rst_counter[IDX_PAP]);
3560 if (--sp->rst_counter[IDX_PAP] < 0)
3562 switch (sp->state[IDX_PAP]) {
3563 case STATE_REQ_SENT:
3565 sppp_cp_change_state(&pap, sp, STATE_CLOSED);
3569 /* TO+ event, not very much we could do */
3570 switch (sp->state[IDX_PAP]) {
3571 case STATE_REQ_SENT:
3572 /* sppp_cp_change_state() will restart the timer */
3573 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT);
3581 * That's the timeout handler if we are peer. Since the peer is active,
3582 * we need to retransmit our PAP request since it is apparently lost.
3583 * XXX We should impose a max counter.
3586 sppp_pap_my_TO(void *cookie)
3588 struct sppp *sp = (struct sppp *)cookie;
3592 log(LOG_DEBUG, SPP_FMT "pap peer TO\n",
3599 sppp_pap_tlu(struct sppp *sp)
3604 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure;
3607 log(LOG_DEBUG, SPP_FMT "%s tlu\n",
3608 SPP_ARGS(ifp), pap.name);
3611 /* indicate to LCP that we need to be closed down */
3612 sp->lcp.protos |= (1 << IDX_PAP);
3614 if (sp->pp_flags & PP_NEEDAUTH) {
3616 * Remote is authenticator, but his auth proto didn't
3617 * complete yet. Defer the transition to network
3624 sppp_phase_network(sp);
3628 sppp_pap_tld(struct sppp *sp)
3633 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp));
3634 UNTIMEOUT(pap.TO, (void *)sp, sp->ch[IDX_PAP]);
3635 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch);
3636 sp->lcp.protos &= ~(1 << IDX_PAP);
3642 sppp_pap_scr(struct sppp *sp)
3644 u_char idlen, pwdlen;
3646 sp->confid[IDX_PAP] = ++sp->pp_seq;
3647 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN);
3648 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN);
3650 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP],
3651 sizeof idlen, (const char *)&idlen,
3652 (size_t)idlen, sp->myauth.name,
3653 sizeof pwdlen, (const char *)&pwdlen,
3654 (size_t)pwdlen, sp->myauth.secret,
3658 * Random miscellaneous functions.
3662 * Send a PAP or CHAP proto packet.
3664 * Varadic function, each of the elements for the ellipsis is of type
3665 * ``size_t mlen, const u_char *msg''. Processing will stop iff
3667 * NOTE: never declare variadic functions with types subject to type
3668 * promotion (i.e. u_char). This is asking for big trouble depending
3669 * on the architecture you are on...
3673 sppp_auth_send(const struct cp *cp, struct sppp *sp,
3674 unsigned int type, unsigned int id,
3678 struct ppp_header *h;
3679 struct lcp_header *lh;
3687 MGETHDR (m, M_DONTWAIT, MT_DATA);
3690 m->m_pkthdr.rcvif = 0;
3692 h = mtod (m, struct ppp_header*);
3693 h->address = PPP_ALLSTATIONS; /* broadcast address */
3694 h->control = PPP_UI; /* Unnumbered Info */
3695 h->protocol = htons(cp->proto);
3697 lh = (struct lcp_header*)(h + 1);
3700 p = (u_char*) (lh+1);
3705 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) {
3706 msg = va_arg(ap, const char *);
3708 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) {
3714 bcopy(msg, p, mlen);
3719 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len;
3720 lh->len = htons (LCP_HEADER_LEN + len);
3723 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d",
3724 SPP_ARGS(ifp), cp->name,
3725 sppp_auth_type_name(cp->proto, lh->type),
3726 lh->ident, ntohs(lh->len));
3727 sppp_print_bytes((u_char*) (lh+1), len);
3730 if (IF_QFULL (&sp->pp_cpq)) {
3731 IF_DROP (&sp->pp_fastq);
3732 IF_DROP (&ifp->if_snd);
3736 IF_ENQUEUE (&sp->pp_cpq, m);
3737 if (! (ifp->if_flags & IFF_OACTIVE))
3738 (*ifp->if_start) (ifp);
3739 ifp->if_obytes += m->m_pkthdr.len + 3;
3743 * Flush interface queue.
3746 sppp_qflush(struct ifqueue *ifq)
3761 * Send keepalive packets, every 10 seconds.
3764 sppp_keepalive(void *dummy)
3770 for (sp=spppq; sp; sp=sp->pp_next) {
3771 struct ifnet *ifp = &sp->pp_if;
3773 /* Keepalive mode disabled or channel down? */
3774 if (! (sp->pp_flags & PP_KEEPALIVE) ||
3775 ! (ifp->if_flags & IFF_RUNNING))
3778 /* No keepalive in PPP mode if LCP not opened yet. */
3779 if (sp->pp_mode != IFF_CISCO &&
3780 sp->pp_phase < PHASE_AUTHENTICATE)
3783 if (sp->pp_alivecnt == MAXALIVECNT) {
3784 /* No keepalive packets got. Stop the interface. */
3785 printf (SPP_FMT "down\n", SPP_ARGS(ifp));
3787 sppp_qflush (&sp->pp_cpq);
3788 if (sp->pp_mode != IFF_CISCO) {
3790 /* Shut down the PPP link. */
3792 /* Initiate negotiation. XXX */
3796 if (sp->pp_alivecnt <= MAXALIVECNT)
3798 if (sp->pp_mode == IFF_CISCO)
3799 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq,
3801 else if (sp->pp_phase >= PHASE_AUTHENTICATE) {
3802 long nmagic = htonl (sp->lcp.magic);
3803 sp->lcp.echoid = ++sp->pp_seq;
3804 sppp_cp_send (sp, PPP_LCP, ECHO_REQ,
3805 sp->lcp.echoid, 4, &nmagic);
3809 TIMEOUT(sppp_keepalive, 0, hz * 10, keepalive_ch);
3813 * Get both IP addresses.
3816 sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask)
3818 struct ifnet *ifp = &sp->pp_if;
3820 struct sockaddr_in *si, *sm;
3826 * Pick the first AF_INET address from the list,
3827 * aliases don't make any sense on a p2p link anyway.
3830 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3831 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
3832 #elif defined(__NetBSD__) || defined (__OpenBSD__)
3833 for (ifa = ifp->if_addrlist.tqh_first;
3835 ifa = ifa->ifa_list.tqe_next)
3837 for (ifa = ifp->if_addrlist;
3839 ifa = ifa->ifa_next)
3841 if (ifa->ifa_addr->sa_family == AF_INET) {
3842 si = (struct sockaddr_in *)ifa->ifa_addr;
3843 sm = (struct sockaddr_in *)ifa->ifa_netmask;
3848 if (si && si->sin_addr.s_addr) {
3849 ssrc = si->sin_addr.s_addr;
3851 *srcmask = ntohl(sm->sin_addr.s_addr);
3854 si = (struct sockaddr_in *)ifa->ifa_dstaddr;
3855 if (si && si->sin_addr.s_addr)
3856 ddst = si->sin_addr.s_addr;
3859 if (dst) *dst = ntohl(ddst);
3860 if (src) *src = ntohl(ssrc);
3864 * Set my IP address. Must be called at splimp.
3867 sppp_set_ip_addr(struct sppp *sp, u_long src)
3871 struct sockaddr_in *si;
3874 * Pick the first AF_INET address from the list,
3875 * aliases don't make any sense on a p2p link anyway.
3878 #if defined(__FreeBSD__) && __FreeBSD__ >= 3
3879 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link)
3880 #elif defined(__NetBSD__) || defined (__OpenBSD__)
3881 for (ifa = ifp->if_addrlist.tqh_first;
3883 ifa = ifa->ifa_list.tqe_next)
3885 for (ifa = ifp->if_addrlist;
3887 ifa = ifa->ifa_next)
3890 if (ifa->ifa_addr->sa_family == AF_INET)
3892 si = (struct sockaddr_in *)ifa->ifa_addr;
3901 #if __NetBSD_Version__ >= 103080000
3902 struct sockaddr_in new_sin = *si;
3904 new_sin.sin_addr.s_addr = htonl(src);
3905 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 1);
3908 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: in_ifinit "
3909 " failed, error=%d\n", SPP_ARGS(ifp), error);
3912 /* delete old route */
3913 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST);
3916 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n",
3917 SPP_ARGS(ifp), error);
3920 /* set new address */
3921 si->sin_addr.s_addr = htonl(src);
3924 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST);
3927 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d",
3928 SPP_ARGS(ifp), error);
3935 sppp_params(struct sppp *sp, u_long cmd, void *data)
3938 struct ifreq *ifr = (struct ifreq *)data;
3942 * ifr->ifr_data is supposed to point to a struct spppreq.
3943 * Check the cmd word first before attempting to fetch all the
3946 if ((subcmd = fuword(ifr->ifr_data)) == -1)
3949 if (copyin((caddr_t)ifr->ifr_data, &spr, sizeof spr) != 0)
3954 if (cmd != SIOCGIFGENERIC)
3957 * We copy over the entire current state, but clean
3958 * out some of the stuff we don't wanna pass up.
3959 * Remember, SIOCGIFGENERIC is unprotected, and can be
3960 * called by any user. No need to ever get PAP or
3961 * CHAP secrets back to userland anyway.
3963 bcopy(sp, &spr.defs, sizeof(struct sppp));
3964 bzero(spr.defs.myauth.secret, AUTHKEYLEN);
3965 bzero(spr.defs.myauth.challenge, AUTHKEYLEN);
3966 bzero(spr.defs.hisauth.secret, AUTHKEYLEN);
3967 bzero(spr.defs.hisauth.challenge, AUTHKEYLEN);
3968 return copyout(&spr, (caddr_t)ifr->ifr_data, sizeof spr);
3971 if (cmd != SIOCSIFGENERIC)
3974 * We have a very specific idea of which fields we allow
3975 * being passed back from userland, so to not clobber our
3976 * current state. For one, we only allow setting
3977 * anything if LCP is in dead phase. Once the LCP
3978 * negotiations started, the authentication settings must
3979 * not be changed again. (The administrator can force an
3980 * ifconfig down in order to get LCP back into dead
3983 * Also, we only allow for authentication parameters to be
3986 * XXX Should allow to set or clear pp_flags.
3988 * Finally, if the respective authentication protocol to
3989 * be used is set differently than 0, but the secret is
3990 * passed as all zeros, we don't trash the existing secret.
3991 * This allows an administrator to change the system name
3992 * only without clobbering the secret (which he didn't get
3993 * back in a previous SPPPIOGDEFS call). However, the
3994 * secrets are cleared if the authentication protocol is
3997 if (sp->pp_phase != PHASE_DEAD)
4000 if ((spr.defs.myauth.proto != 0 && spr.defs.myauth.proto != PPP_PAP &&
4001 spr.defs.myauth.proto != PPP_CHAP) ||
4002 (spr.defs.hisauth.proto != 0 && spr.defs.hisauth.proto != PPP_PAP &&
4003 spr.defs.hisauth.proto != PPP_CHAP))
4006 if (spr.defs.myauth.proto == 0)
4007 /* resetting myauth */
4008 bzero(&sp->myauth, sizeof sp->myauth);
4010 /* setting/changing myauth */
4011 sp->myauth.proto = spr.defs.myauth.proto;
4012 bcopy(spr.defs.myauth.name, sp->myauth.name, AUTHNAMELEN);
4013 if (spr.defs.myauth.secret[0] != '\0')
4014 bcopy(spr.defs.myauth.secret, sp->myauth.secret,
4017 if (spr.defs.hisauth.proto == 0)
4018 /* resetting hisauth */
4019 bzero(&sp->hisauth, sizeof sp->hisauth);
4021 /* setting/changing hisauth */
4022 sp->hisauth.proto = spr.defs.hisauth.proto;
4023 sp->hisauth.flags = spr.defs.hisauth.flags;
4024 bcopy(spr.defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN);
4025 if (spr.defs.hisauth.secret[0] != '\0')
4026 bcopy(spr.defs.hisauth.secret, sp->hisauth.secret,
4039 sppp_phase_network(struct sppp *sp)
4045 sp->pp_phase = PHASE_NETWORK;
4048 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp),
4049 sppp_phase_name(sp->pp_phase));
4051 /* Notify NCPs now. */
4052 for (i = 0; i < IDX_COUNT; i++)
4053 if ((cps[i])->flags & CP_NCP)
4056 /* Send Up events to all NCPs. */
4057 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1)
4058 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_NCP))
4061 /* if no NCP is starting, all this was in vain, close down */
4062 sppp_lcp_check_and_close(sp);
4067 sppp_cp_type_name(u_char type)
4069 static char buf[12];
4071 case CONF_REQ: return "conf-req";
4072 case CONF_ACK: return "conf-ack";
4073 case CONF_NAK: return "conf-nak";
4074 case CONF_REJ: return "conf-rej";
4075 case TERM_REQ: return "term-req";
4076 case TERM_ACK: return "term-ack";
4077 case CODE_REJ: return "code-rej";
4078 case PROTO_REJ: return "proto-rej";
4079 case ECHO_REQ: return "echo-req";
4080 case ECHO_REPLY: return "echo-reply";
4081 case DISC_REQ: return "discard-req";
4083 snprintf (buf, sizeof(buf), "cp/0x%x", type);
4088 sppp_auth_type_name(u_short proto, u_char type)
4090 static char buf[12];
4094 case CHAP_CHALLENGE: return "challenge";
4095 case CHAP_RESPONSE: return "response";
4096 case CHAP_SUCCESS: return "success";
4097 case CHAP_FAILURE: return "failure";
4101 case PAP_REQ: return "req";
4102 case PAP_ACK: return "ack";
4103 case PAP_NAK: return "nak";
4106 snprintf (buf, sizeof(buf), "auth/0x%x", type);
4111 sppp_lcp_opt_name(u_char opt)
4113 static char buf[12];
4115 case LCP_OPT_MRU: return "mru";
4116 case LCP_OPT_ASYNC_MAP: return "async-map";
4117 case LCP_OPT_AUTH_PROTO: return "auth-proto";
4118 case LCP_OPT_QUAL_PROTO: return "qual-proto";
4119 case LCP_OPT_MAGIC: return "magic";
4120 case LCP_OPT_PROTO_COMP: return "proto-comp";
4121 case LCP_OPT_ADDR_COMP: return "addr-comp";
4123 snprintf (buf, sizeof(buf), "lcp/0x%x", opt);
4128 sppp_ipcp_opt_name(u_char opt)
4130 static char buf[12];
4132 case IPCP_OPT_ADDRESSES: return "addresses";
4133 case IPCP_OPT_COMPRESSION: return "compression";
4134 case IPCP_OPT_ADDRESS: return "address";
4136 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt);
4141 sppp_state_name(int state)
4144 case STATE_INITIAL: return "initial";
4145 case STATE_STARTING: return "starting";
4146 case STATE_CLOSED: return "closed";
4147 case STATE_STOPPED: return "stopped";
4148 case STATE_CLOSING: return "closing";
4149 case STATE_STOPPING: return "stopping";
4150 case STATE_REQ_SENT: return "req-sent";
4151 case STATE_ACK_RCVD: return "ack-rcvd";
4152 case STATE_ACK_SENT: return "ack-sent";
4153 case STATE_OPENED: return "opened";
4159 sppp_phase_name(enum ppp_phase phase)
4162 case PHASE_DEAD: return "dead";
4163 case PHASE_ESTABLISH: return "establish";
4164 case PHASE_TERMINATE: return "terminate";
4165 case PHASE_AUTHENTICATE: return "authenticate";
4166 case PHASE_NETWORK: return "network";
4172 sppp_proto_name(u_short proto)
4174 static char buf[12];
4176 case PPP_LCP: return "lcp";
4177 case PPP_IPCP: return "ipcp";
4178 case PPP_PAP: return "pap";
4179 case PPP_CHAP: return "chap";
4181 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto);
4186 sppp_print_bytes(const u_char *p, u_short len)
4189 addlog(" %*D", len, p, "-");
4193 sppp_print_string(const char *p, u_short len)
4200 * Print only ASCII chars directly. RFC 1994 recommends
4201 * using only them, but we don't rely on it. */
4202 if (c < ' ' || c > '~')
4210 sppp_dotted_quad(u_long addr)
4213 sprintf(s, "%d.%d.%d.%d",
4214 (int)((addr >> 24) & 0xff),
4215 (int)((addr >> 16) & 0xff),
4216 (int)((addr >> 8) & 0xff),
4217 (int)(addr & 0xff));
4222 sppp_strnlen(u_char *p, int max)
4226 for (len = 0; len < max && *p; ++p)
4231 /* a dummy, used to drop uninteresting events */
4233 sppp_null(struct sppp *unused)
4235 /* do just nothing */