if_spppsubr.c revision 134391
1/* 2 * Synchronous PPP/Cisco link level subroutines. 3 * Keepalive protocol implemented in both Cisco and PPP modes. 4 * 5 * Copyright (C) 1994-1996 Cronyx Engineering Ltd. 6 * Author: Serge Vakulenko, <vak@cronyx.ru> 7 * 8 * Heavily revamped to conform to RFC 1661. 9 * Copyright (C) 1997, 2001 Joerg Wunsch. 10 * 11 * This software is distributed with NO WARRANTIES, not even the implied 12 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 13 * 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. 17 * 18 * From: Version 2.4, Thu Apr 30 17:17:21 MSD 1997 19 * 20 * $FreeBSD: head/sys/net/if_spppsubr.c 134391 2004-08-27 18:33:08Z andre $ 21 */ 22 23#include <sys/param.h> 24 25#if defined(__FreeBSD__) && __FreeBSD__ >= 3 26#include "opt_inet.h" 27#include "opt_inet6.h" 28#include "opt_ipx.h" 29#endif 30 31#ifdef NetBSD1_3 32# if NetBSD1_3 > 6 33# include "opt_inet.h" 34# include "opt_inet6.h" 35# include "opt_iso.h" 36# endif 37#endif 38 39#include <sys/systm.h> 40#include <sys/kernel.h> 41#include <sys/module.h> 42#include <sys/sockio.h> 43#include <sys/socket.h> 44#include <sys/syslog.h> 45#if defined(__FreeBSD__) && __FreeBSD__ >= 3 46#include <sys/random.h> 47#endif 48#include <sys/malloc.h> 49#include <sys/mbuf.h> 50 51#if defined (__OpenBSD__) 52#include <sys/md5k.h> 53#else 54#include <sys/md5.h> 55#endif 56 57#include <net/if.h> 58#include <net/netisr.h> 59#include <net/if_types.h> 60#include <net/route.h> 61#include <netinet/in.h> 62#include <netinet/in_systm.h> 63#include <netinet/ip.h> 64#include <net/slcompress.h> 65 66#if defined (__NetBSD__) || defined (__OpenBSD__) 67#include <machine/cpu.h> /* XXX for softnet */ 68#endif 69 70#include <machine/stdarg.h> 71 72#include <netinet/in.h> 73#include <netinet/in_systm.h> 74#include <netinet/in_var.h> 75 76#ifdef INET 77#include <netinet/ip.h> 78#include <netinet/tcp.h> 79#endif 80 81#if defined (__FreeBSD__) || defined (__OpenBSD__) 82# include <netinet/if_ether.h> 83#else 84# include <net/ethertypes.h> 85#endif 86 87#ifdef IPX 88#include <netipx/ipx.h> 89#include <netipx/ipx_if.h> 90#endif 91 92#include <net/if_sppp.h> 93 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 98#else 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 102#endif 103 104#define MAXALIVECNT 3 /* max. alive packets */ 105 106/* 107 * Interface flags that can be set in an ifconfig command. 108 * 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. 113 * 114 * Setting link1 will cause the link to auto-dial only as packets 115 * arrive to be sent. 116 * 117 * Setting IFF_DEBUG will syslog the option negotiation and state 118 * transitions at level kern.debug. Note: all logs consistently look 119 * like 120 * 121 * <if-name><unit>: <proto-name> <additional info...> 122 * 123 * with <if-name><unit> being something like "bppp0", and <proto-name> 124 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc. 125 */ 126 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 */ 130 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_VJ_COMP 0x002d /* VJ compressed TCP/IP */ 138#define PPP_VJ_UCOMP 0x002f /* VJ uncompressed TCP/IP */ 139#define PPP_IPV6 0x0057 /* Internet Protocol Version 6 */ 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 */ 144#define PPP_IPV6CP 0x8057 /* IPv6 Control Protocol */ 145 146#define CONF_REQ 1 /* PPP configure request */ 147#define CONF_ACK 2 /* PPP configure acknowledge */ 148#define CONF_NAK 3 /* PPP configure negative ack */ 149#define CONF_REJ 4 /* PPP configure reject */ 150#define TERM_REQ 5 /* PPP terminate request */ 151#define TERM_ACK 6 /* PPP terminate acknowledge */ 152#define CODE_REJ 7 /* PPP code reject */ 153#define PROTO_REJ 8 /* PPP protocol reject */ 154#define ECHO_REQ 9 /* PPP echo request */ 155#define ECHO_REPLY 10 /* PPP echo reply */ 156#define DISC_REQ 11 /* PPP discard request */ 157 158#define LCP_OPT_MRU 1 /* maximum receive unit */ 159#define LCP_OPT_ASYNC_MAP 2 /* async control character map */ 160#define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */ 161#define LCP_OPT_QUAL_PROTO 4 /* quality protocol */ 162#define LCP_OPT_MAGIC 5 /* magic number */ 163#define LCP_OPT_RESERVED 6 /* reserved */ 164#define LCP_OPT_PROTO_COMP 7 /* protocol field compression */ 165#define LCP_OPT_ADDR_COMP 8 /* address/control field compression */ 166 167#define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */ 168#define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */ 169#define IPCP_OPT_ADDRESS 3 /* local IP address */ 170 171#define IPV6CP_OPT_IFID 1 /* interface identifier */ 172#define IPV6CP_OPT_COMPRESSION 2 /* IPv6 compression protocol */ 173 174#define IPCP_COMP_VJ 0x2d /* Code for VJ compression */ 175 176#define PAP_REQ 1 /* PAP name/password request */ 177#define PAP_ACK 2 /* PAP acknowledge */ 178#define PAP_NAK 3 /* PAP fail */ 179 180#define CHAP_CHALLENGE 1 /* CHAP challenge request */ 181#define CHAP_RESPONSE 2 /* CHAP challenge response */ 182#define CHAP_SUCCESS 3 /* CHAP response ok */ 183#define CHAP_FAILURE 4 /* CHAP response failed */ 184 185#define CHAP_MD5 5 /* hash algorithm - MD5 */ 186 187#define CISCO_MULTICAST 0x8f /* Cisco multicast address */ 188#define CISCO_UNICAST 0x0f /* Cisco unicast address */ 189#define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */ 190#define CISCO_ADDR_REQ 0 /* Cisco address request */ 191#define CISCO_ADDR_REPLY 1 /* Cisco address reply */ 192#define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */ 193 194/* states are named and numbered according to RFC 1661 */ 195#define STATE_INITIAL 0 196#define STATE_STARTING 1 197#define STATE_CLOSED 2 198#define STATE_STOPPED 3 199#define STATE_CLOSING 4 200#define STATE_STOPPING 5 201#define STATE_REQ_SENT 6 202#define STATE_ACK_RCVD 7 203#define STATE_ACK_SENT 8 204#define STATE_OPENED 9 205 206struct ppp_header { 207 u_char address; 208 u_char control; 209 u_short protocol; 210} __packed; 211#define PPP_HEADER_LEN sizeof (struct ppp_header) 212 213struct lcp_header { 214 u_char type; 215 u_char ident; 216 u_short len; 217} __packed; 218#define LCP_HEADER_LEN sizeof (struct lcp_header) 219 220struct cisco_packet { 221 u_long type; 222 u_long par1; 223 u_long par2; 224 u_short rel; 225 u_short time0; 226 u_short time1; 227} __packed; 228#define CISCO_PACKET_LEN sizeof (struct cisco_packet) 229 230/* 231 * We follow the spelling and capitalization of RFC 1661 here, to make 232 * it easier comparing with the standard. Please refer to this RFC in 233 * case you can't make sense out of these abbreviation; it will also 234 * explain the semantics related to the various events and actions. 235 */ 236struct cp { 237 u_short proto; /* PPP control protocol number */ 238 u_char protoidx; /* index into state table in struct sppp */ 239 u_char flags; 240#define CP_LCP 0x01 /* this is the LCP */ 241#define CP_AUTH 0x02 /* this is an authentication protocol */ 242#define CP_NCP 0x04 /* this is a NCP */ 243#define CP_QUAL 0x08 /* this is a quality reporting protocol */ 244 const char *name; /* name of this control protocol */ 245 /* event handlers */ 246 void (*Up)(struct sppp *sp); 247 void (*Down)(struct sppp *sp); 248 void (*Open)(struct sppp *sp); 249 void (*Close)(struct sppp *sp); 250 void (*TO)(void *sp); 251 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len); 252 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len); 253 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len); 254 /* actions */ 255 void (*tlu)(struct sppp *sp); 256 void (*tld)(struct sppp *sp); 257 void (*tls)(struct sppp *sp); 258 void (*tlf)(struct sppp *sp); 259 void (*scr)(struct sppp *sp); 260}; 261 262static struct sppp *spppq; 263#if defined(__FreeBSD__) && __FreeBSD__ >= 3 264static struct callout_handle keepalive_ch; 265#endif 266 267#if defined(__FreeBSD__) && __FreeBSD__ >= 3 && __FreeBSD_version < 501113 268#define SPP_FMT "%s%d: " 269#define SPP_ARGS(ifp) (ifp)->if_name, (ifp)->if_unit 270#else 271#define SPP_FMT "%s: " 272#define SPP_ARGS(ifp) (ifp)->if_xname 273#endif 274 275#ifdef INET 276/* 277 * The following disgusting hack gets around the problem that IP TOS 278 * can't be set yet. We want to put "interactive" traffic on a high 279 * priority queue. To decide if traffic is interactive, we check that 280 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control. 281 * 282 * XXX is this really still necessary? - joerg - 283 */ 284static const u_short interactive_ports[8] = { 285 0, 513, 0, 0, 286 0, 21, 0, 23, 287}; 288#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p)) 289#endif 290 291/* almost every function needs these */ 292#define STDDCL \ 293 struct ifnet *ifp = &sp->pp_if; \ 294 int debug = ifp->if_flags & IFF_DEBUG 295 296static int sppp_output(struct ifnet *ifp, struct mbuf *m, 297 struct sockaddr *dst, struct rtentry *rt); 298 299static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2); 300static void sppp_cisco_input(struct sppp *sp, struct mbuf *m); 301 302static void sppp_cp_input(const struct cp *cp, struct sppp *sp, 303 struct mbuf *m); 304static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type, 305 u_char ident, u_short len, void *data); 306/* static void sppp_cp_timeout(void *arg); */ 307static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp, 308 int newstate); 309static void sppp_auth_send(const struct cp *cp, 310 struct sppp *sp, unsigned int type, unsigned int id, 311 ...); 312 313static void sppp_up_event(const struct cp *cp, struct sppp *sp); 314static void sppp_down_event(const struct cp *cp, struct sppp *sp); 315static void sppp_open_event(const struct cp *cp, struct sppp *sp); 316static void sppp_close_event(const struct cp *cp, struct sppp *sp); 317static void sppp_to_event(const struct cp *cp, struct sppp *sp); 318 319static void sppp_null(struct sppp *sp); 320 321static void sppp_lcp_init(struct sppp *sp); 322static void sppp_lcp_up(struct sppp *sp); 323static void sppp_lcp_down(struct sppp *sp); 324static void sppp_lcp_open(struct sppp *sp); 325static void sppp_lcp_close(struct sppp *sp); 326static void sppp_lcp_TO(void *sp); 327static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len); 328static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len); 329static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len); 330static void sppp_lcp_tlu(struct sppp *sp); 331static void sppp_lcp_tld(struct sppp *sp); 332static void sppp_lcp_tls(struct sppp *sp); 333static void sppp_lcp_tlf(struct sppp *sp); 334static void sppp_lcp_scr(struct sppp *sp); 335static void sppp_lcp_check_and_close(struct sppp *sp); 336static int sppp_ncp_check(struct sppp *sp); 337 338static void sppp_ipcp_init(struct sppp *sp); 339static void sppp_ipcp_up(struct sppp *sp); 340static void sppp_ipcp_down(struct sppp *sp); 341static void sppp_ipcp_open(struct sppp *sp); 342static void sppp_ipcp_close(struct sppp *sp); 343static void sppp_ipcp_TO(void *sp); 344static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len); 345static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len); 346static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len); 347static void sppp_ipcp_tlu(struct sppp *sp); 348static void sppp_ipcp_tld(struct sppp *sp); 349static void sppp_ipcp_tls(struct sppp *sp); 350static void sppp_ipcp_tlf(struct sppp *sp); 351static void sppp_ipcp_scr(struct sppp *sp); 352 353static void sppp_ipv6cp_init(struct sppp *sp); 354static void sppp_ipv6cp_up(struct sppp *sp); 355static void sppp_ipv6cp_down(struct sppp *sp); 356static void sppp_ipv6cp_open(struct sppp *sp); 357static void sppp_ipv6cp_close(struct sppp *sp); 358static void sppp_ipv6cp_TO(void *sp); 359static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len); 360static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len); 361static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len); 362static void sppp_ipv6cp_tlu(struct sppp *sp); 363static void sppp_ipv6cp_tld(struct sppp *sp); 364static void sppp_ipv6cp_tls(struct sppp *sp); 365static void sppp_ipv6cp_tlf(struct sppp *sp); 366static void sppp_ipv6cp_scr(struct sppp *sp); 367 368static void sppp_pap_input(struct sppp *sp, struct mbuf *m); 369static void sppp_pap_init(struct sppp *sp); 370static void sppp_pap_open(struct sppp *sp); 371static void sppp_pap_close(struct sppp *sp); 372static void sppp_pap_TO(void *sp); 373static void sppp_pap_my_TO(void *sp); 374static void sppp_pap_tlu(struct sppp *sp); 375static void sppp_pap_tld(struct sppp *sp); 376static void sppp_pap_scr(struct sppp *sp); 377 378static void sppp_chap_input(struct sppp *sp, struct mbuf *m); 379static void sppp_chap_init(struct sppp *sp); 380static void sppp_chap_open(struct sppp *sp); 381static void sppp_chap_close(struct sppp *sp); 382static void sppp_chap_TO(void *sp); 383static void sppp_chap_tlu(struct sppp *sp); 384static void sppp_chap_tld(struct sppp *sp); 385static void sppp_chap_scr(struct sppp *sp); 386 387static const char *sppp_auth_type_name(u_short proto, u_char type); 388static const char *sppp_cp_type_name(u_char type); 389static const char *sppp_dotted_quad(u_long addr); 390static const char *sppp_ipcp_opt_name(u_char opt); 391#ifdef INET6 392static const char *sppp_ipv6cp_opt_name(u_char opt); 393#endif 394static const char *sppp_lcp_opt_name(u_char opt); 395static const char *sppp_phase_name(enum ppp_phase phase); 396static const char *sppp_proto_name(u_short proto); 397static const char *sppp_state_name(int state); 398static int sppp_params(struct sppp *sp, u_long cmd, void *data); 399static int sppp_strnlen(u_char *p, int max); 400static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, 401 u_long *srcmask); 402static void sppp_keepalive(void *dummy); 403static void sppp_phase_network(struct sppp *sp); 404static void sppp_print_bytes(const u_char *p, u_short len); 405static void sppp_print_string(const char *p, u_short len); 406static void sppp_qflush(struct ifqueue *ifq); 407static void sppp_set_ip_addr(struct sppp *sp, u_long src); 408#ifdef INET6 409static void sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, 410 struct in6_addr *dst, struct in6_addr *srcmask); 411#ifdef IPV6CP_MYIFID_DYN 412static void sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src); 413static void sppp_gen_ip6_addr(struct sppp *sp, const struct in6_addr *src); 414#endif 415static void sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *src); 416#endif 417 418/* our control protocol descriptors */ 419static const struct cp lcp = { 420 PPP_LCP, IDX_LCP, CP_LCP, "lcp", 421 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close, 422 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak, 423 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf, 424 sppp_lcp_scr 425}; 426 427static const struct cp ipcp = { 428 PPP_IPCP, IDX_IPCP, 429#ifdef INET /* don't run IPCP if there's no IPv4 support */ 430 CP_NCP, 431#else 432 0, 433#endif 434 "ipcp", 435 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close, 436 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak, 437 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf, 438 sppp_ipcp_scr 439}; 440 441static const struct cp ipv6cp = { 442 PPP_IPV6CP, IDX_IPV6CP, 443#ifdef INET6 /*don't run IPv6CP if there's no IPv6 support*/ 444 CP_NCP, 445#else 446 0, 447#endif 448 "ipv6cp", 449 sppp_ipv6cp_up, sppp_ipv6cp_down, sppp_ipv6cp_open, sppp_ipv6cp_close, 450 sppp_ipv6cp_TO, sppp_ipv6cp_RCR, sppp_ipv6cp_RCN_rej, sppp_ipv6cp_RCN_nak, 451 sppp_ipv6cp_tlu, sppp_ipv6cp_tld, sppp_ipv6cp_tls, sppp_ipv6cp_tlf, 452 sppp_ipv6cp_scr 453}; 454 455static const struct cp pap = { 456 PPP_PAP, IDX_PAP, CP_AUTH, "pap", 457 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close, 458 sppp_pap_TO, 0, 0, 0, 459 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null, 460 sppp_pap_scr 461}; 462 463static const struct cp chap = { 464 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap", 465 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close, 466 sppp_chap_TO, 0, 0, 0, 467 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null, 468 sppp_chap_scr 469}; 470 471static const struct cp *cps[IDX_COUNT] = { 472 &lcp, /* IDX_LCP */ 473 &ipcp, /* IDX_IPCP */ 474 &ipv6cp, /* IDX_IPV6CP */ 475 &pap, /* IDX_PAP */ 476 &chap, /* IDX_CHAP */ 477}; 478 479static int 480sppp_modevent(module_t mod, int type, void *unused) 481{ 482 switch (type) { 483 case MOD_LOAD: 484 break; 485 case MOD_UNLOAD: 486 return EACCES; 487 default: 488 return EOPNOTSUPP; 489 } 490 return 0; 491} 492static moduledata_t spppmod = { 493 "sppp", 494 sppp_modevent, 495 0 496}; 497MODULE_VERSION(sppp, 1); 498DECLARE_MODULE(sppp, spppmod, SI_SUB_DRIVERS, SI_ORDER_ANY); 499 500/* 501 * Exported functions, comprising our interface to the lower layer. 502 */ 503 504/* 505 * Process the received packet. 506 */ 507void 508sppp_input(struct ifnet *ifp, struct mbuf *m) 509{ 510 struct ppp_header *h; 511 int isr = -1; 512 struct sppp *sp = (struct sppp *)ifp; 513 u_char *iphdr; 514 int hlen, vjlen, do_account = 0; 515 int debug = ifp->if_flags & IFF_DEBUG; 516 517 if (ifp->if_flags & IFF_UP) 518 /* Count received bytes, add FCS and one flag */ 519 ifp->if_ibytes += m->m_pkthdr.len + 3; 520 521 if (m->m_pkthdr.len <= PPP_HEADER_LEN) { 522 /* Too small packet, drop it. */ 523 if (debug) 524 log(LOG_DEBUG, 525 SPP_FMT "input packet is too small, %d bytes\n", 526 SPP_ARGS(ifp), m->m_pkthdr.len); 527 drop: 528 m_freem (m); 529 drop2: 530 ++ifp->if_ierrors; 531 ++ifp->if_iqdrops; 532 return; 533 } 534 535 /* Get PPP header. */ 536 h = mtod (m, struct ppp_header*); 537 m_adj (m, PPP_HEADER_LEN); 538 539 switch (h->address) { 540 case PPP_ALLSTATIONS: 541 if (h->control != PPP_UI) 542 goto invalid; 543 if (sp->pp_mode == IFF_CISCO) { 544 if (debug) 545 log(LOG_DEBUG, 546 SPP_FMT "PPP packet in Cisco mode " 547 "<addr=0x%x ctrl=0x%x proto=0x%x>\n", 548 SPP_ARGS(ifp), 549 h->address, h->control, ntohs(h->protocol)); 550 goto drop; 551 } 552 switch (ntohs (h->protocol)) { 553 default: 554 if (debug) 555 log(LOG_DEBUG, 556 SPP_FMT "rejecting protocol " 557 "<addr=0x%x ctrl=0x%x proto=0x%x>\n", 558 SPP_ARGS(ifp), 559 h->address, h->control, ntohs(h->protocol)); 560 if (sp->state[IDX_LCP] == STATE_OPENED) 561 sppp_cp_send (sp, PPP_LCP, PROTO_REJ, 562 ++sp->pp_seq[IDX_LCP], m->m_pkthdr.len + 2, 563 &h->protocol); 564 ++ifp->if_noproto; 565 goto drop; 566 case PPP_LCP: 567 sppp_cp_input(&lcp, sp, m); 568 m_freem (m); 569 return; 570 case PPP_PAP: 571 if (sp->pp_phase >= PHASE_AUTHENTICATE) 572 sppp_pap_input(sp, m); 573 m_freem (m); 574 return; 575 case PPP_CHAP: 576 if (sp->pp_phase >= PHASE_AUTHENTICATE) 577 sppp_chap_input(sp, m); 578 m_freem (m); 579 return; 580#ifdef INET 581 case PPP_IPCP: 582 if (sp->pp_phase == PHASE_NETWORK) 583 sppp_cp_input(&ipcp, sp, m); 584 m_freem (m); 585 return; 586 case PPP_IP: 587 if (sp->state[IDX_IPCP] == STATE_OPENED) { 588 isr = NETISR_IP; 589 } 590 do_account++; 591 break; 592 case PPP_VJ_COMP: 593 if (sp->state[IDX_IPCP] == STATE_OPENED) { 594 if ((vjlen = 595 sl_uncompress_tcp_core(mtod(m, u_char *), 596 m->m_len, m->m_len, 597 TYPE_COMPRESSED_TCP, 598 sp->pp_comp, 599 &iphdr, &hlen)) <= 0) { 600 if (debug) 601 log(LOG_INFO, 602 SPP_FMT "VJ uncompress failed on compressed packet\n", 603 SPP_ARGS(ifp)); 604 goto drop; 605 } 606 607 /* 608 * Trim the VJ header off the packet, and prepend 609 * the uncompressed IP header (which will usually 610 * end up in two chained mbufs since there's not 611 * enough leading space in the existing mbuf). 612 */ 613 m_adj(m, vjlen); 614 M_PREPEND(m, hlen, M_DONTWAIT); 615 if (m == NULL) 616 goto drop2; 617 bcopy(iphdr, mtod(m, u_char *), hlen); 618 isr = NETISR_IP; 619 } 620 do_account++; 621 break; 622 case PPP_VJ_UCOMP: 623 if (sp->state[IDX_IPCP] == STATE_OPENED) { 624 if (sl_uncompress_tcp_core(mtod(m, u_char *), 625 m->m_len, m->m_len, 626 TYPE_UNCOMPRESSED_TCP, 627 sp->pp_comp, 628 &iphdr, &hlen) != 0) { 629 if (debug) 630 log(LOG_INFO, 631 SPP_FMT "VJ uncompress failed on uncompressed packet\n", 632 SPP_ARGS(ifp)); 633 goto drop; 634 } 635 isr = NETISR_IP; 636 } 637 do_account++; 638 break; 639#endif 640#ifdef INET6 641 case PPP_IPV6CP: 642 if (sp->pp_phase == PHASE_NETWORK) 643 sppp_cp_input(&ipv6cp, sp, m); 644 m_freem (m); 645 return; 646 647 case PPP_IPV6: 648 if (sp->state[IDX_IPV6CP] == STATE_OPENED) 649 isr = NETISR_IPV6; 650 do_account++; 651 break; 652#endif 653#ifdef IPX 654 case PPP_IPX: 655 /* IPX IPXCP not implemented yet */ 656 if (sp->pp_phase == PHASE_NETWORK) 657 isr = NETISR_IPX; 658 do_account++; 659 break; 660#endif 661 } 662 break; 663 case CISCO_MULTICAST: 664 case CISCO_UNICAST: 665 /* Don't check the control field here (RFC 1547). */ 666 if (sp->pp_mode != IFF_CISCO) { 667 if (debug) 668 log(LOG_DEBUG, 669 SPP_FMT "Cisco packet in PPP mode " 670 "<addr=0x%x ctrl=0x%x proto=0x%x>\n", 671 SPP_ARGS(ifp), 672 h->address, h->control, ntohs(h->protocol)); 673 goto drop; 674 } 675 switch (ntohs (h->protocol)) { 676 default: 677 ++ifp->if_noproto; 678 goto invalid; 679 case CISCO_KEEPALIVE: 680 sppp_cisco_input ((struct sppp*) ifp, m); 681 m_freem (m); 682 return; 683#ifdef INET 684 case ETHERTYPE_IP: 685 isr = NETISR_IP; 686 do_account++; 687 break; 688#endif 689#ifdef INET6 690 case ETHERTYPE_IPV6: 691 isr = NETISR_IPV6; 692 do_account++; 693 break; 694#endif 695#ifdef IPX 696 case ETHERTYPE_IPX: 697 isr = NETISR_IPX; 698 do_account++; 699 break; 700#endif 701 } 702 break; 703 default: /* Invalid PPP packet. */ 704 invalid: 705 if (debug) 706 log(LOG_DEBUG, 707 SPP_FMT "invalid input packet " 708 "<addr=0x%x ctrl=0x%x proto=0x%x>\n", 709 SPP_ARGS(ifp), 710 h->address, h->control, ntohs(h->protocol)); 711 goto drop; 712 } 713 714 if (! (ifp->if_flags & IFF_UP) || isr == -1) 715 goto drop; 716 717 /* Check queue. */ 718 if (netisr_queue(isr, m)) { /* (0) on success. */ 719 if (debug) 720 log(LOG_DEBUG, SPP_FMT "protocol queue overflow\n", 721 SPP_ARGS(ifp)); 722 goto drop2; 723 } 724 if (do_account) 725 /* 726 * Do only account for network packets, not for control 727 * packets. This is used by some subsystems to detect 728 * idle lines. 729 */ 730 sp->pp_last_recv = time_second; 731} 732 733/* 734 * Enqueue transmit packet. 735 */ 736static int 737sppp_output(struct ifnet *ifp, struct mbuf *m, 738 struct sockaddr *dst, struct rtentry *rt) 739{ 740 struct sppp *sp = (struct sppp*) ifp; 741 struct ppp_header *h; 742 struct ifqueue *ifq = NULL; 743 int s, error, rv = 0; 744 int ipproto = PPP_IP; 745 int debug = ifp->if_flags & IFF_DEBUG; 746 747 s = splimp(); 748 749 if ((ifp->if_flags & IFF_UP) == 0 || 750 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) { 751#ifdef INET6 752 drop: 753#endif 754 m_freem (m); 755 splx (s); 756 return (ENETDOWN); 757 } 758 759 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) { 760#ifdef INET6 761 /* 762 * XXX 763 * 764 * Hack to prevent the initialization-time generated 765 * IPv6 multicast packet to erroneously cause a 766 * dialout event in case IPv6 has been 767 * administratively disabled on that interface. 768 */ 769 if (dst->sa_family == AF_INET6 && 770 !(sp->confflags & CONF_ENABLE_IPV6)) 771 goto drop; 772#endif 773 /* 774 * Interface is not yet running, but auto-dial. Need 775 * to start LCP for it. 776 */ 777 ifp->if_flags |= IFF_RUNNING; 778 splx(s); 779 lcp.Open(sp); 780 s = splimp(); 781 } 782 783#ifdef INET 784 if (dst->sa_family == AF_INET) { 785 /* XXX Check mbuf length here? */ 786 struct ip *ip = mtod (m, struct ip*); 787 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl); 788 789 /* 790 * When using dynamic local IP address assignment by using 791 * 0.0.0.0 as a local address, the first TCP session will 792 * not connect because the local TCP checksum is computed 793 * using 0.0.0.0 which will later become our real IP address 794 * so the TCP checksum computed at the remote end will 795 * become invalid. So we 796 * - don't let packets with src ip addr 0 thru 797 * - we flag TCP packets with src ip 0 as an error 798 */ 799 800 if(ip->ip_src.s_addr == INADDR_ANY) /* -hm */ 801 { 802 m_freem(m); 803 splx(s); 804 if(ip->ip_p == IPPROTO_TCP) 805 return(EADDRNOTAVAIL); 806 else 807 return(0); 808 } 809 810 /* 811 * Put low delay, telnet, rlogin and ftp control packets 812 * in front of the queue or let ALTQ take care. 813 */ 814 if (ALTQ_IS_ENABLED(&ifp->if_snd)) 815 ; 816 else if (_IF_QFULL(&sp->pp_fastq)) 817 ; 818 else if (ip->ip_tos & IPTOS_LOWDELAY) 819 ifq = &sp->pp_fastq; 820 else if (m->m_len < sizeof *ip + sizeof *tcp) 821 ; 822 else if (ip->ip_p != IPPROTO_TCP) 823 ; 824 else if (INTERACTIVE (ntohs (tcp->th_sport))) 825 ifq = &sp->pp_fastq; 826 else if (INTERACTIVE (ntohs (tcp->th_dport))) 827 ifq = &sp->pp_fastq; 828 829 /* 830 * Do IP Header compression 831 */ 832 if (sp->pp_mode != IFF_CISCO && (sp->ipcp.flags & IPCP_VJ) && 833 ip->ip_p == IPPROTO_TCP) 834 switch (sl_compress_tcp(m, ip, sp->pp_comp, 835 sp->ipcp.compress_cid)) { 836 case TYPE_COMPRESSED_TCP: 837 ipproto = PPP_VJ_COMP; 838 break; 839 case TYPE_UNCOMPRESSED_TCP: 840 ipproto = PPP_VJ_UCOMP; 841 break; 842 case TYPE_IP: 843 ipproto = PPP_IP; 844 break; 845 default: 846 m_freem(m); 847 splx(s); 848 return (EINVAL); 849 } 850 } 851#endif 852 853#ifdef INET6 854 if (dst->sa_family == AF_INET6) { 855 /* XXX do something tricky here? */ 856 } 857#endif 858 859 /* 860 * Prepend general data packet PPP header. For now, IP only. 861 */ 862 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT); 863 if (! m) { 864 if (debug) 865 log(LOG_DEBUG, SPP_FMT "no memory for transmit header\n", 866 SPP_ARGS(ifp)); 867 ++ifp->if_oerrors; 868 splx (s); 869 return (ENOBUFS); 870 } 871 /* 872 * May want to check size of packet 873 * (albeit due to the implementation it's always enough) 874 */ 875 h = mtod (m, struct ppp_header*); 876 if (sp->pp_mode == IFF_CISCO) { 877 h->address = CISCO_UNICAST; /* unicast address */ 878 h->control = 0; 879 } else { 880 h->address = PPP_ALLSTATIONS; /* broadcast address */ 881 h->control = PPP_UI; /* Unnumbered Info */ 882 } 883 884 switch (dst->sa_family) { 885#ifdef INET 886 case AF_INET: /* Internet Protocol */ 887 if (sp->pp_mode == IFF_CISCO) 888 h->protocol = htons (ETHERTYPE_IP); 889 else { 890 /* 891 * Don't choke with an ENETDOWN early. It's 892 * possible that we just started dialing out, 893 * so don't drop the packet immediately. If 894 * we notice that we run out of buffer space 895 * below, we will however remember that we are 896 * not ready to carry IP packets, and return 897 * ENETDOWN, as opposed to ENOBUFS. 898 */ 899 h->protocol = htons(ipproto); 900 if (sp->state[IDX_IPCP] != STATE_OPENED) 901 rv = ENETDOWN; 902 } 903 break; 904#endif 905#ifdef INET6 906 case AF_INET6: /* Internet Protocol */ 907 if (sp->pp_mode == IFF_CISCO) 908 h->protocol = htons (ETHERTYPE_IPV6); 909 else { 910 /* 911 * Don't choke with an ENETDOWN early. It's 912 * possible that we just started dialing out, 913 * so don't drop the packet immediately. If 914 * we notice that we run out of buffer space 915 * below, we will however remember that we are 916 * not ready to carry IP packets, and return 917 * ENETDOWN, as opposed to ENOBUFS. 918 */ 919 h->protocol = htons(PPP_IPV6); 920 if (sp->state[IDX_IPV6CP] != STATE_OPENED) 921 rv = ENETDOWN; 922 } 923 break; 924#endif 925#ifdef IPX 926 case AF_IPX: /* Novell IPX Protocol */ 927 h->protocol = htons (sp->pp_mode == IFF_CISCO ? 928 ETHERTYPE_IPX : PPP_IPX); 929 break; 930#endif 931 default: 932 m_freem (m); 933 ++ifp->if_oerrors; 934 splx (s); 935 return (EAFNOSUPPORT); 936 } 937 938 /* 939 * Queue message on interface, and start output if interface 940 * not yet active. 941 */ 942 if (ifq != NULL) 943 error = !(IF_HANDOFF_ADJ(ifq, m, ifp, 3)); 944 else 945 IFQ_HANDOFF_ADJ(ifp, m, 3, error); 946 if (error) { 947 ++ifp->if_oerrors; 948 splx (s); 949 return (rv? rv: ENOBUFS); 950 } 951 splx (s); 952 /* 953 * Unlike in sppp_input(), we can always bump the timestamp 954 * here since sppp_output() is only called on behalf of 955 * network-layer traffic; control-layer traffic is handled 956 * by sppp_cp_send(). 957 */ 958 sp->pp_last_sent = time_second; 959 return (0); 960} 961 962void 963sppp_attach(struct ifnet *ifp) 964{ 965 struct sppp *sp = (struct sppp*) ifp; 966 967 /* Initialize keepalive handler. */ 968 if (spppq == NULL) 969 TIMEOUT(sppp_keepalive, 0, hz * 10, keepalive_ch); 970 971 /* Insert new entry into the keepalive list. */ 972 sp->pp_next = spppq; 973 spppq = sp; 974 975 sp->pp_if.if_mtu = PP_MTU; 976 sp->pp_if.if_flags = IFF_POINTOPOINT | IFF_MULTICAST; 977 sp->pp_if.if_type = IFT_PPP; 978 sp->pp_if.if_output = sppp_output; 979#if 0 980 sp->pp_flags = PP_KEEPALIVE; 981#endif 982 sp->pp_if.if_snd.ifq_maxlen = 32; 983 sp->pp_fastq.ifq_maxlen = 32; 984 sp->pp_cpq.ifq_maxlen = 20; 985 sp->pp_loopcnt = 0; 986 sp->pp_alivecnt = 0; 987 bzero(&sp->pp_seq[0], sizeof(sp->pp_seq)); 988 bzero(&sp->pp_rseq[0], sizeof(sp->pp_rseq)); 989 sp->pp_phase = PHASE_DEAD; 990 sp->pp_up = lcp.Up; 991 sp->pp_down = lcp.Down; 992 if(!mtx_initialized(&sp->pp_cpq.ifq_mtx)) 993 mtx_init(&sp->pp_cpq.ifq_mtx, "sppp_cpq", NULL, MTX_DEF); 994 if(!mtx_initialized(&sp->pp_fastq.ifq_mtx)) 995 mtx_init(&sp->pp_fastq.ifq_mtx, "sppp_fastq", NULL, MTX_DEF); 996 sp->pp_last_recv = sp->pp_last_sent = time_second; 997 sp->confflags = 0; 998#ifdef INET 999 sp->confflags |= CONF_ENABLE_VJ; 1000#endif 1001#ifdef INET6 1002 sp->confflags |= CONF_ENABLE_IPV6; 1003#endif 1004 sp->pp_comp = malloc(sizeof(struct slcompress), M_TEMP, M_WAITOK); 1005 sl_compress_init(sp->pp_comp, -1); 1006 sppp_lcp_init(sp); 1007 sppp_ipcp_init(sp); 1008 sppp_ipv6cp_init(sp); 1009 sppp_pap_init(sp); 1010 sppp_chap_init(sp); 1011} 1012 1013void 1014sppp_detach(struct ifnet *ifp) 1015{ 1016 struct sppp **q, *p, *sp = (struct sppp*) ifp; 1017 int i; 1018 1019 /* Remove the entry from the keepalive list. */ 1020 for (q = &spppq; (p = *q); q = &p->pp_next) 1021 if (p == sp) { 1022 *q = p->pp_next; 1023 break; 1024 } 1025 1026 /* Stop keepalive handler. */ 1027 if (spppq == NULL) 1028 UNTIMEOUT(sppp_keepalive, 0, keepalive_ch); 1029 1030 for (i = 0; i < IDX_COUNT; i++) 1031 UNTIMEOUT((cps[i])->TO, (void *)sp, sp->ch[i]); 1032 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch); 1033 mtx_destroy(&sp->pp_cpq.ifq_mtx); 1034 mtx_destroy(&sp->pp_fastq.ifq_mtx); 1035} 1036 1037/* 1038 * Flush the interface output queue. 1039 */ 1040void 1041sppp_flush(struct ifnet *ifp) 1042{ 1043 struct sppp *sp = (struct sppp*) ifp; 1044 1045 sppp_qflush ((struct ifqueue *)&sp->pp_if.if_snd); 1046 sppp_qflush (&sp->pp_fastq); 1047 sppp_qflush (&sp->pp_cpq); 1048} 1049 1050/* 1051 * Check if the output queue is empty. 1052 */ 1053int 1054sppp_isempty(struct ifnet *ifp) 1055{ 1056 struct sppp *sp = (struct sppp*) ifp; 1057 int empty, s; 1058 1059 s = splimp(); 1060 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head && 1061 !sp->pp_if.if_snd.ifq_head; 1062 splx(s); 1063 return (empty); 1064} 1065 1066/* 1067 * Get next packet to send. 1068 */ 1069struct mbuf * 1070sppp_dequeue(struct ifnet *ifp) 1071{ 1072 struct sppp *sp = (struct sppp*) ifp; 1073 struct mbuf *m; 1074 int s; 1075 1076 s = splimp(); 1077 /* 1078 * Process only the control protocol queue until we have at 1079 * least one NCP open. 1080 * 1081 * Do always serve all three queues in Cisco mode. 1082 */ 1083 IF_DEQUEUE(&sp->pp_cpq, m); 1084 if (m == NULL && 1085 (sppp_ncp_check(sp) || sp->pp_mode == IFF_CISCO)) { 1086 IF_DEQUEUE(&sp->pp_fastq, m); 1087 if (m == NULL) 1088 IF_DEQUEUE (&sp->pp_if.if_snd, m); 1089 } 1090 splx(s); 1091 return m; 1092} 1093 1094/* 1095 * Pick the next packet, do not remove it from the queue. 1096 */ 1097struct mbuf * 1098sppp_pick(struct ifnet *ifp) 1099{ 1100 struct sppp *sp = (struct sppp*)ifp; 1101 struct mbuf *m; 1102 int s; 1103 1104 s= splimp (); 1105 1106 m = sp->pp_cpq.ifq_head; 1107 if (m == NULL && 1108 (sp->pp_phase == PHASE_NETWORK || sp->pp_mode == IFF_CISCO)) 1109 if ((m = sp->pp_fastq.ifq_head) == NULL) 1110 m = sp->pp_if.if_snd.ifq_head; 1111 splx (s); 1112 return (m); 1113} 1114 1115/* 1116 * Process an ioctl request. Called on low priority level. 1117 */ 1118int 1119sppp_ioctl(struct ifnet *ifp, IOCTL_CMD_T cmd, void *data) 1120{ 1121 struct ifreq *ifr = (struct ifreq*) data; 1122 struct sppp *sp = (struct sppp*) ifp; 1123 int s, rv, going_up, going_down, newmode; 1124 1125 s = splimp(); 1126 rv = 0; 1127 switch (cmd) { 1128 case SIOCAIFADDR: 1129 case SIOCSIFDSTADDR: 1130 break; 1131 1132 case SIOCSIFADDR: 1133 /* set the interface "up" when assigning an IP address */ 1134 ifp->if_flags |= IFF_UP; 1135 /* FALLTHROUGH */ 1136 1137 case SIOCSIFFLAGS: 1138 going_up = ifp->if_flags & IFF_UP && 1139 (ifp->if_flags & IFF_RUNNING) == 0; 1140 going_down = (ifp->if_flags & IFF_UP) == 0 && 1141 ifp->if_flags & IFF_RUNNING; 1142 1143 newmode = ifp->if_flags & IFF_PASSIVE; 1144 if (!newmode) 1145 newmode = ifp->if_flags & IFF_AUTO; 1146 if (!newmode) 1147 newmode = ifp->if_flags & IFF_CISCO; 1148 ifp->if_flags &= ~(IFF_PASSIVE | IFF_AUTO | IFF_CISCO); 1149 ifp->if_flags |= newmode; 1150 1151 if (newmode != sp->pp_mode) { 1152 going_down = 1; 1153 if (!going_up) 1154 going_up = ifp->if_flags & IFF_RUNNING; 1155 } 1156 1157 if (going_down) { 1158 if (sp->pp_mode != IFF_CISCO) 1159 lcp.Close(sp); 1160 else if (sp->pp_tlf) 1161 (sp->pp_tlf)(sp); 1162 sppp_flush(ifp); 1163 ifp->if_flags &= ~IFF_RUNNING; 1164 sp->pp_mode = newmode; 1165 } 1166 1167 if (going_up) { 1168 if (sp->pp_mode != IFF_CISCO) 1169 lcp.Close(sp); 1170 sp->pp_mode = newmode; 1171 if (sp->pp_mode == 0) { 1172 ifp->if_flags |= IFF_RUNNING; 1173 lcp.Open(sp); 1174 } 1175 if (sp->pp_mode == IFF_CISCO) { 1176 if (sp->pp_tls) 1177 (sp->pp_tls)(sp); 1178 ifp->if_flags |= IFF_RUNNING; 1179 } 1180 } 1181 1182 break; 1183 1184#ifdef SIOCSIFMTU 1185#ifndef ifr_mtu 1186#define ifr_mtu ifr_metric 1187#endif 1188 case SIOCSIFMTU: 1189 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru) 1190 return (EINVAL); 1191 ifp->if_mtu = ifr->ifr_mtu; 1192 break; 1193#endif 1194#ifdef SLIOCSETMTU 1195 case SLIOCSETMTU: 1196 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru) 1197 return (EINVAL); 1198 ifp->if_mtu = *(short*)data; 1199 break; 1200#endif 1201#ifdef SIOCGIFMTU 1202 case SIOCGIFMTU: 1203 ifr->ifr_mtu = ifp->if_mtu; 1204 break; 1205#endif 1206#ifdef SLIOCGETMTU 1207 case SLIOCGETMTU: 1208 *(short*)data = ifp->if_mtu; 1209 break; 1210#endif 1211 case SIOCADDMULTI: 1212 case SIOCDELMULTI: 1213 break; 1214 1215 case SIOCGIFGENERIC: 1216 case SIOCSIFGENERIC: 1217 rv = sppp_params(sp, cmd, data); 1218 break; 1219 1220 default: 1221 rv = ENOTTY; 1222 } 1223 splx(s); 1224 return rv; 1225} 1226 1227/* 1228 * Cisco framing implementation. 1229 */ 1230 1231/* 1232 * Handle incoming Cisco keepalive protocol packets. 1233 */ 1234static void 1235sppp_cisco_input(struct sppp *sp, struct mbuf *m) 1236{ 1237 STDDCL; 1238 struct cisco_packet *h; 1239 u_long me, mymask; 1240 1241 if (m->m_pkthdr.len < CISCO_PACKET_LEN) { 1242 if (debug) 1243 log(LOG_DEBUG, 1244 SPP_FMT "cisco invalid packet length: %d bytes\n", 1245 SPP_ARGS(ifp), m->m_pkthdr.len); 1246 return; 1247 } 1248 h = mtod (m, struct cisco_packet*); 1249 if (debug) 1250 log(LOG_DEBUG, 1251 SPP_FMT "cisco input: %d bytes " 1252 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n", 1253 SPP_ARGS(ifp), m->m_pkthdr.len, 1254 (u_long)ntohl (h->type), (u_long)h->par1, (u_long)h->par2, (u_int)h->rel, 1255 (u_int)h->time0, (u_int)h->time1); 1256 switch (ntohl (h->type)) { 1257 default: 1258 if (debug) 1259 log(-1, SPP_FMT "cisco unknown packet type: 0x%lx\n", 1260 SPP_ARGS(ifp), (u_long)ntohl (h->type)); 1261 break; 1262 case CISCO_ADDR_REPLY: 1263 /* Reply on address request, ignore */ 1264 break; 1265 case CISCO_KEEPALIVE_REQ: 1266 sp->pp_alivecnt = 0; 1267 sp->pp_rseq[IDX_LCP] = ntohl (h->par1); 1268 if (sp->pp_seq[IDX_LCP] == sp->pp_rseq[IDX_LCP]) { 1269 /* Local and remote sequence numbers are equal. 1270 * Probably, the line is in loopback mode. */ 1271 if (sp->pp_loopcnt >= MAXALIVECNT) { 1272 printf (SPP_FMT "loopback\n", 1273 SPP_ARGS(ifp)); 1274 sp->pp_loopcnt = 0; 1275 if (ifp->if_flags & IFF_UP) { 1276 if_down (ifp); 1277 sppp_qflush (&sp->pp_cpq); 1278 } 1279 } 1280 ++sp->pp_loopcnt; 1281 1282 /* Generate new local sequence number */ 1283#if defined(__FreeBSD__) && __FreeBSD__ >= 3 1284 sp->pp_seq[IDX_LCP] = random(); 1285#else 1286 sp->pp_seq[IDX_LCP] ^= time.tv_sec ^ time.tv_usec; 1287#endif 1288 break; 1289 } 1290 sp->pp_loopcnt = 0; 1291 if (! (ifp->if_flags & IFF_UP) && 1292 (ifp->if_flags & IFF_RUNNING)) { 1293 if_up(ifp); 1294 printf (SPP_FMT "up\n", SPP_ARGS(ifp)); 1295 } 1296 break; 1297 case CISCO_ADDR_REQ: 1298 sppp_get_ip_addrs(sp, &me, 0, &mymask); 1299 if (me != 0L) 1300 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask); 1301 break; 1302 } 1303} 1304 1305/* 1306 * Send Cisco keepalive packet. 1307 */ 1308static void 1309sppp_cisco_send(struct sppp *sp, int type, long par1, long par2) 1310{ 1311 STDDCL; 1312 struct ppp_header *h; 1313 struct cisco_packet *ch; 1314 struct mbuf *m; 1315 struct timeval tv; 1316 1317 getmicrouptime(&tv); 1318 1319 MGETHDR (m, M_DONTWAIT, MT_DATA); 1320 if (! m) 1321 return; 1322 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN; 1323 m->m_pkthdr.rcvif = 0; 1324 1325 h = mtod (m, struct ppp_header*); 1326 h->address = CISCO_MULTICAST; 1327 h->control = 0; 1328 h->protocol = htons (CISCO_KEEPALIVE); 1329 1330 ch = (struct cisco_packet*) (h + 1); 1331 ch->type = htonl (type); 1332 ch->par1 = htonl (par1); 1333 ch->par2 = htonl (par2); 1334 ch->rel = -1; 1335 1336 ch->time0 = htons ((u_short) (tv.tv_sec >> 16)); 1337 ch->time1 = htons ((u_short) tv.tv_sec); 1338 1339 if (debug) 1340 log(LOG_DEBUG, 1341 SPP_FMT "cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n", 1342 SPP_ARGS(ifp), (u_long)ntohl (ch->type), (u_long)ch->par1, 1343 (u_long)ch->par2, (u_int)ch->rel, (u_int)ch->time0, (u_int)ch->time1); 1344 1345 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3)) 1346 ifp->if_oerrors++; 1347} 1348 1349/* 1350 * PPP protocol implementation. 1351 */ 1352 1353/* 1354 * Send PPP control protocol packet. 1355 */ 1356static void 1357sppp_cp_send(struct sppp *sp, u_short proto, u_char type, 1358 u_char ident, u_short len, void *data) 1359{ 1360 STDDCL; 1361 struct ppp_header *h; 1362 struct lcp_header *lh; 1363 struct mbuf *m; 1364 1365 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) 1366 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN; 1367 MGETHDR (m, M_DONTWAIT, MT_DATA); 1368 if (! m) 1369 return; 1370 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len; 1371 m->m_pkthdr.rcvif = 0; 1372 1373 h = mtod (m, struct ppp_header*); 1374 h->address = PPP_ALLSTATIONS; /* broadcast address */ 1375 h->control = PPP_UI; /* Unnumbered Info */ 1376 h->protocol = htons (proto); /* Link Control Protocol */ 1377 1378 lh = (struct lcp_header*) (h + 1); 1379 lh->type = type; 1380 lh->ident = ident; 1381 lh->len = htons (LCP_HEADER_LEN + len); 1382 if (len) 1383 bcopy (data, lh+1, len); 1384 1385 if (debug) { 1386 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d", 1387 SPP_ARGS(ifp), 1388 sppp_proto_name(proto), 1389 sppp_cp_type_name (lh->type), lh->ident, 1390 ntohs (lh->len)); 1391 sppp_print_bytes ((u_char*) (lh+1), len); 1392 log(-1, ">\n"); 1393 } 1394 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3)) 1395 ifp->if_oerrors++; 1396} 1397 1398/* 1399 * Handle incoming PPP control protocol packets. 1400 */ 1401static void 1402sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m) 1403{ 1404 STDDCL; 1405 struct lcp_header *h; 1406 int len = m->m_pkthdr.len; 1407 int rv; 1408 u_char *p; 1409 1410 if (len < 4) { 1411 if (debug) 1412 log(LOG_DEBUG, 1413 SPP_FMT "%s invalid packet length: %d bytes\n", 1414 SPP_ARGS(ifp), cp->name, len); 1415 return; 1416 } 1417 h = mtod (m, struct lcp_header*); 1418 if (debug) { 1419 log(LOG_DEBUG, 1420 SPP_FMT "%s input(%s): <%s id=0x%x len=%d", 1421 SPP_ARGS(ifp), cp->name, 1422 sppp_state_name(sp->state[cp->protoidx]), 1423 sppp_cp_type_name (h->type), h->ident, ntohs (h->len)); 1424 sppp_print_bytes ((u_char*) (h+1), len-4); 1425 log(-1, ">\n"); 1426 } 1427 if (len > ntohs (h->len)) 1428 len = ntohs (h->len); 1429 p = (u_char *)(h + 1); 1430 switch (h->type) { 1431 case CONF_REQ: 1432 if (len < 4) { 1433 if (debug) 1434 log(-1, SPP_FMT "%s invalid conf-req length %d\n", 1435 SPP_ARGS(ifp), cp->name, 1436 len); 1437 ++ifp->if_ierrors; 1438 break; 1439 } 1440 /* handle states where RCR doesn't get a SCA/SCN */ 1441 switch (sp->state[cp->protoidx]) { 1442 case STATE_CLOSING: 1443 case STATE_STOPPING: 1444 return; 1445 case STATE_CLOSED: 1446 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 1447 0, 0); 1448 return; 1449 } 1450 rv = (cp->RCR)(sp, h, len); 1451 switch (sp->state[cp->protoidx]) { 1452 case STATE_OPENED: 1453 (cp->tld)(sp); 1454 (cp->scr)(sp); 1455 /* FALLTHROUGH */ 1456 case STATE_ACK_SENT: 1457 case STATE_REQ_SENT: 1458 /* 1459 * sppp_cp_change_state() have the side effect of 1460 * restarting the timeouts. We want to avoid that 1461 * if the state don't change, otherwise we won't 1462 * ever timeout and resend a configuration request 1463 * that got lost. 1464 */ 1465 if (sp->state[cp->protoidx] == (rv ? STATE_ACK_SENT: 1466 STATE_REQ_SENT)) 1467 break; 1468 sppp_cp_change_state(cp, sp, rv? 1469 STATE_ACK_SENT: STATE_REQ_SENT); 1470 break; 1471 case STATE_STOPPED: 1472 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure; 1473 (cp->scr)(sp); 1474 sppp_cp_change_state(cp, sp, rv? 1475 STATE_ACK_SENT: STATE_REQ_SENT); 1476 break; 1477 case STATE_ACK_RCVD: 1478 if (rv) { 1479 sppp_cp_change_state(cp, sp, STATE_OPENED); 1480 if (debug) 1481 log(LOG_DEBUG, SPP_FMT "%s tlu\n", 1482 SPP_ARGS(ifp), 1483 cp->name); 1484 (cp->tlu)(sp); 1485 } else 1486 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD); 1487 break; 1488 default: 1489 printf(SPP_FMT "%s illegal %s in state %s\n", 1490 SPP_ARGS(ifp), cp->name, 1491 sppp_cp_type_name(h->type), 1492 sppp_state_name(sp->state[cp->protoidx])); 1493 ++ifp->if_ierrors; 1494 } 1495 break; 1496 case CONF_ACK: 1497 if (h->ident != sp->confid[cp->protoidx]) { 1498 if (debug) 1499 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n", 1500 SPP_ARGS(ifp), cp->name, 1501 h->ident, sp->confid[cp->protoidx]); 1502 ++ifp->if_ierrors; 1503 break; 1504 } 1505 switch (sp->state[cp->protoidx]) { 1506 case STATE_CLOSED: 1507 case STATE_STOPPED: 1508 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0); 1509 break; 1510 case STATE_CLOSING: 1511 case STATE_STOPPING: 1512 break; 1513 case STATE_REQ_SENT: 1514 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure; 1515 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD); 1516 break; 1517 case STATE_OPENED: 1518 (cp->tld)(sp); 1519 /* FALLTHROUGH */ 1520 case STATE_ACK_RCVD: 1521 (cp->scr)(sp); 1522 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1523 break; 1524 case STATE_ACK_SENT: 1525 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure; 1526 sppp_cp_change_state(cp, sp, STATE_OPENED); 1527 if (debug) 1528 log(LOG_DEBUG, SPP_FMT "%s tlu\n", 1529 SPP_ARGS(ifp), cp->name); 1530 (cp->tlu)(sp); 1531 break; 1532 default: 1533 printf(SPP_FMT "%s illegal %s in state %s\n", 1534 SPP_ARGS(ifp), cp->name, 1535 sppp_cp_type_name(h->type), 1536 sppp_state_name(sp->state[cp->protoidx])); 1537 ++ifp->if_ierrors; 1538 } 1539 break; 1540 case CONF_NAK: 1541 case CONF_REJ: 1542 if (h->ident != sp->confid[cp->protoidx]) { 1543 if (debug) 1544 log(-1, SPP_FMT "%s id mismatch 0x%x != 0x%x\n", 1545 SPP_ARGS(ifp), cp->name, 1546 h->ident, sp->confid[cp->protoidx]); 1547 ++ifp->if_ierrors; 1548 break; 1549 } 1550 if (h->type == CONF_NAK) 1551 (cp->RCN_nak)(sp, h, len); 1552 else /* CONF_REJ */ 1553 (cp->RCN_rej)(sp, h, len); 1554 1555 switch (sp->state[cp->protoidx]) { 1556 case STATE_CLOSED: 1557 case STATE_STOPPED: 1558 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0); 1559 break; 1560 case STATE_REQ_SENT: 1561 case STATE_ACK_SENT: 1562 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure; 1563 /* 1564 * Slow things down a bit if we think we might be 1565 * in loopback. Depend on the timeout to send the 1566 * next configuration request. 1567 */ 1568 if (sp->pp_loopcnt) 1569 break; 1570 (cp->scr)(sp); 1571 break; 1572 case STATE_OPENED: 1573 (cp->tld)(sp); 1574 /* FALLTHROUGH */ 1575 case STATE_ACK_RCVD: 1576 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1577 (cp->scr)(sp); 1578 break; 1579 case STATE_CLOSING: 1580 case STATE_STOPPING: 1581 break; 1582 default: 1583 printf(SPP_FMT "%s illegal %s in state %s\n", 1584 SPP_ARGS(ifp), cp->name, 1585 sppp_cp_type_name(h->type), 1586 sppp_state_name(sp->state[cp->protoidx])); 1587 ++ifp->if_ierrors; 1588 } 1589 break; 1590 1591 case TERM_REQ: 1592 switch (sp->state[cp->protoidx]) { 1593 case STATE_ACK_RCVD: 1594 case STATE_ACK_SENT: 1595 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1596 /* FALLTHROUGH */ 1597 case STATE_CLOSED: 1598 case STATE_STOPPED: 1599 case STATE_CLOSING: 1600 case STATE_STOPPING: 1601 case STATE_REQ_SENT: 1602 sta: 1603 /* Send Terminate-Ack packet. */ 1604 if (debug) 1605 log(LOG_DEBUG, SPP_FMT "%s send terminate-ack\n", 1606 SPP_ARGS(ifp), cp->name); 1607 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0); 1608 break; 1609 case STATE_OPENED: 1610 (cp->tld)(sp); 1611 sp->rst_counter[cp->protoidx] = 0; 1612 sppp_cp_change_state(cp, sp, STATE_STOPPING); 1613 goto sta; 1614 break; 1615 default: 1616 printf(SPP_FMT "%s illegal %s in state %s\n", 1617 SPP_ARGS(ifp), cp->name, 1618 sppp_cp_type_name(h->type), 1619 sppp_state_name(sp->state[cp->protoidx])); 1620 ++ifp->if_ierrors; 1621 } 1622 break; 1623 case TERM_ACK: 1624 switch (sp->state[cp->protoidx]) { 1625 case STATE_CLOSED: 1626 case STATE_STOPPED: 1627 case STATE_REQ_SENT: 1628 case STATE_ACK_SENT: 1629 break; 1630 case STATE_CLOSING: 1631 sppp_cp_change_state(cp, sp, STATE_CLOSED); 1632 (cp->tlf)(sp); 1633 break; 1634 case STATE_STOPPING: 1635 sppp_cp_change_state(cp, sp, STATE_STOPPED); 1636 (cp->tlf)(sp); 1637 break; 1638 case STATE_ACK_RCVD: 1639 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1640 break; 1641 case STATE_OPENED: 1642 (cp->tld)(sp); 1643 (cp->scr)(sp); 1644 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD); 1645 break; 1646 default: 1647 printf(SPP_FMT "%s illegal %s in state %s\n", 1648 SPP_ARGS(ifp), cp->name, 1649 sppp_cp_type_name(h->type), 1650 sppp_state_name(sp->state[cp->protoidx])); 1651 ++ifp->if_ierrors; 1652 } 1653 break; 1654 case CODE_REJ: 1655 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */ 1656 log(LOG_INFO, 1657 SPP_FMT "%s: ignoring RXJ (%s) for proto 0x%x, " 1658 "danger will robinson\n", 1659 SPP_ARGS(ifp), cp->name, 1660 sppp_cp_type_name(h->type), ntohs(*((u_short *)p))); 1661 switch (sp->state[cp->protoidx]) { 1662 case STATE_CLOSED: 1663 case STATE_STOPPED: 1664 case STATE_REQ_SENT: 1665 case STATE_ACK_SENT: 1666 case STATE_CLOSING: 1667 case STATE_STOPPING: 1668 case STATE_OPENED: 1669 break; 1670 case STATE_ACK_RCVD: 1671 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1672 break; 1673 default: 1674 printf(SPP_FMT "%s illegal %s in state %s\n", 1675 SPP_ARGS(ifp), cp->name, 1676 sppp_cp_type_name(h->type), 1677 sppp_state_name(sp->state[cp->protoidx])); 1678 ++ifp->if_ierrors; 1679 } 1680 break; 1681 case PROTO_REJ: 1682 { 1683 int catastrophic; 1684 const struct cp *upper; 1685 int i; 1686 u_int16_t proto; 1687 1688 catastrophic = 0; 1689 upper = NULL; 1690 proto = ntohs(*((u_int16_t *)p)); 1691 for (i = 0; i < IDX_COUNT; i++) { 1692 if (cps[i]->proto == proto) { 1693 upper = cps[i]; 1694 break; 1695 } 1696 } 1697 if (upper == NULL) 1698 catastrophic++; 1699 1700 if (catastrophic || debug) 1701 log(catastrophic? LOG_INFO: LOG_DEBUG, 1702 SPP_FMT "%s: RXJ%c (%s) for proto 0x%x (%s/%s)\n", 1703 SPP_ARGS(ifp), cp->name, catastrophic ? '-' : '+', 1704 sppp_cp_type_name(h->type), proto, 1705 upper ? upper->name : "unknown", 1706 upper ? sppp_state_name(sp->state[upper->protoidx]) : "?"); 1707 1708 /* 1709 * if we got RXJ+ against conf-req, the peer does not implement 1710 * this particular protocol type. terminate the protocol. 1711 */ 1712 if (upper && !catastrophic) { 1713 if (sp->state[upper->protoidx] == STATE_REQ_SENT) { 1714 upper->Close(sp); 1715 break; 1716 } 1717 } 1718 1719 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */ 1720 switch (sp->state[cp->protoidx]) { 1721 case STATE_CLOSED: 1722 case STATE_STOPPED: 1723 case STATE_REQ_SENT: 1724 case STATE_ACK_SENT: 1725 case STATE_CLOSING: 1726 case STATE_STOPPING: 1727 case STATE_OPENED: 1728 break; 1729 case STATE_ACK_RCVD: 1730 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1731 break; 1732 default: 1733 printf(SPP_FMT "%s illegal %s in state %s\n", 1734 SPP_ARGS(ifp), cp->name, 1735 sppp_cp_type_name(h->type), 1736 sppp_state_name(sp->state[cp->protoidx])); 1737 ++ifp->if_ierrors; 1738 } 1739 break; 1740 } 1741 case DISC_REQ: 1742 if (cp->proto != PPP_LCP) 1743 goto illegal; 1744 /* Discard the packet. */ 1745 break; 1746 case ECHO_REQ: 1747 if (cp->proto != PPP_LCP) 1748 goto illegal; 1749 if (sp->state[cp->protoidx] != STATE_OPENED) { 1750 if (debug) 1751 log(-1, SPP_FMT "lcp echo req but lcp closed\n", 1752 SPP_ARGS(ifp)); 1753 ++ifp->if_ierrors; 1754 break; 1755 } 1756 if (len < 8) { 1757 if (debug) 1758 log(-1, SPP_FMT "invalid lcp echo request " 1759 "packet length: %d bytes\n", 1760 SPP_ARGS(ifp), len); 1761 break; 1762 } 1763 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) && 1764 ntohl (*(long*)(h+1)) == sp->lcp.magic) { 1765 /* Line loopback mode detected. */ 1766 printf(SPP_FMT "loopback\n", SPP_ARGS(ifp)); 1767 sp->pp_loopcnt = MAXALIVECNT * 5; 1768 if_down (ifp); 1769 sppp_qflush (&sp->pp_cpq); 1770 1771 /* Shut down the PPP link. */ 1772 /* XXX */ 1773 lcp.Down(sp); 1774 lcp.Up(sp); 1775 break; 1776 } 1777 *(long*)(h+1) = htonl (sp->lcp.magic); 1778 if (debug) 1779 log(-1, SPP_FMT "got lcp echo req, sending echo rep\n", 1780 SPP_ARGS(ifp)); 1781 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1); 1782 break; 1783 case ECHO_REPLY: 1784 if (cp->proto != PPP_LCP) 1785 goto illegal; 1786 if (h->ident != sp->lcp.echoid) { 1787 ++ifp->if_ierrors; 1788 break; 1789 } 1790 if (len < 8) { 1791 if (debug) 1792 log(-1, SPP_FMT "lcp invalid echo reply " 1793 "packet length: %d bytes\n", 1794 SPP_ARGS(ifp), len); 1795 break; 1796 } 1797 if (debug) 1798 log(-1, SPP_FMT "lcp got echo rep\n", 1799 SPP_ARGS(ifp)); 1800 if (!(sp->lcp.opts & (1 << LCP_OPT_MAGIC)) || 1801 ntohl (*(long*)(h+1)) != sp->lcp.magic) 1802 sp->pp_alivecnt = 0; 1803 break; 1804 default: 1805 /* Unknown packet type -- send Code-Reject packet. */ 1806 illegal: 1807 if (debug) 1808 log(-1, SPP_FMT "%s send code-rej for 0x%x\n", 1809 SPP_ARGS(ifp), cp->name, h->type); 1810 sppp_cp_send(sp, cp->proto, CODE_REJ, 1811 ++sp->pp_seq[cp->protoidx], m->m_pkthdr.len, h); 1812 ++ifp->if_ierrors; 1813 } 1814} 1815 1816 1817/* 1818 * The generic part of all Up/Down/Open/Close/TO event handlers. 1819 * Basically, the state transition handling in the automaton. 1820 */ 1821static void 1822sppp_up_event(const struct cp *cp, struct sppp *sp) 1823{ 1824 STDDCL; 1825 1826 if (debug) 1827 log(LOG_DEBUG, SPP_FMT "%s up(%s)\n", 1828 SPP_ARGS(ifp), cp->name, 1829 sppp_state_name(sp->state[cp->protoidx])); 1830 1831 switch (sp->state[cp->protoidx]) { 1832 case STATE_INITIAL: 1833 sppp_cp_change_state(cp, sp, STATE_CLOSED); 1834 break; 1835 case STATE_STARTING: 1836 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure; 1837 (cp->scr)(sp); 1838 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1839 break; 1840 default: 1841 printf(SPP_FMT "%s illegal up in state %s\n", 1842 SPP_ARGS(ifp), cp->name, 1843 sppp_state_name(sp->state[cp->protoidx])); 1844 } 1845} 1846 1847static void 1848sppp_down_event(const struct cp *cp, struct sppp *sp) 1849{ 1850 STDDCL; 1851 1852 if (debug) 1853 log(LOG_DEBUG, SPP_FMT "%s down(%s)\n", 1854 SPP_ARGS(ifp), cp->name, 1855 sppp_state_name(sp->state[cp->protoidx])); 1856 1857 switch (sp->state[cp->protoidx]) { 1858 case STATE_CLOSED: 1859 case STATE_CLOSING: 1860 sppp_cp_change_state(cp, sp, STATE_INITIAL); 1861 break; 1862 case STATE_STOPPED: 1863 sppp_cp_change_state(cp, sp, STATE_STARTING); 1864 (cp->tls)(sp); 1865 break; 1866 case STATE_STOPPING: 1867 case STATE_REQ_SENT: 1868 case STATE_ACK_RCVD: 1869 case STATE_ACK_SENT: 1870 sppp_cp_change_state(cp, sp, STATE_STARTING); 1871 break; 1872 case STATE_OPENED: 1873 (cp->tld)(sp); 1874 sppp_cp_change_state(cp, sp, STATE_STARTING); 1875 break; 1876 default: 1877 printf(SPP_FMT "%s illegal down in state %s\n", 1878 SPP_ARGS(ifp), cp->name, 1879 sppp_state_name(sp->state[cp->protoidx])); 1880 } 1881} 1882 1883 1884static void 1885sppp_open_event(const struct cp *cp, struct sppp *sp) 1886{ 1887 STDDCL; 1888 1889 if (debug) 1890 log(LOG_DEBUG, SPP_FMT "%s open(%s)\n", 1891 SPP_ARGS(ifp), cp->name, 1892 sppp_state_name(sp->state[cp->protoidx])); 1893 1894 switch (sp->state[cp->protoidx]) { 1895 case STATE_INITIAL: 1896 sppp_cp_change_state(cp, sp, STATE_STARTING); 1897 (cp->tls)(sp); 1898 break; 1899 case STATE_STARTING: 1900 break; 1901 case STATE_CLOSED: 1902 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure; 1903 (cp->scr)(sp); 1904 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1905 break; 1906 case STATE_STOPPED: 1907 /* 1908 * Try escaping stopped state. This seems to bite 1909 * people occasionally, in particular for IPCP, 1910 * presumably following previous IPCP negotiation 1911 * aborts. Somehow, we must have missed a Down event 1912 * which would have caused a transition into starting 1913 * state, so as a bandaid we force the Down event now. 1914 * This effectively implements (something like the) 1915 * `restart' option mentioned in the state transition 1916 * table of RFC 1661. 1917 */ 1918 sppp_cp_change_state(cp, sp, STATE_STARTING); 1919 (cp->tls)(sp); 1920 break; 1921 case STATE_STOPPING: 1922 case STATE_REQ_SENT: 1923 case STATE_ACK_RCVD: 1924 case STATE_ACK_SENT: 1925 case STATE_OPENED: 1926 break; 1927 case STATE_CLOSING: 1928 sppp_cp_change_state(cp, sp, STATE_STOPPING); 1929 break; 1930 } 1931} 1932 1933 1934static void 1935sppp_close_event(const struct cp *cp, struct sppp *sp) 1936{ 1937 STDDCL; 1938 1939 if (debug) 1940 log(LOG_DEBUG, SPP_FMT "%s close(%s)\n", 1941 SPP_ARGS(ifp), cp->name, 1942 sppp_state_name(sp->state[cp->protoidx])); 1943 1944 switch (sp->state[cp->protoidx]) { 1945 case STATE_INITIAL: 1946 case STATE_CLOSED: 1947 case STATE_CLOSING: 1948 break; 1949 case STATE_STARTING: 1950 sppp_cp_change_state(cp, sp, STATE_INITIAL); 1951 (cp->tlf)(sp); 1952 break; 1953 case STATE_STOPPED: 1954 sppp_cp_change_state(cp, sp, STATE_CLOSED); 1955 break; 1956 case STATE_STOPPING: 1957 sppp_cp_change_state(cp, sp, STATE_CLOSING); 1958 break; 1959 case STATE_OPENED: 1960 (cp->tld)(sp); 1961 /* FALLTHROUGH */ 1962 case STATE_REQ_SENT: 1963 case STATE_ACK_RCVD: 1964 case STATE_ACK_SENT: 1965 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate; 1966 sppp_cp_send(sp, cp->proto, TERM_REQ, 1967 ++sp->pp_seq[cp->protoidx], 0, 0); 1968 sppp_cp_change_state(cp, sp, STATE_CLOSING); 1969 break; 1970 } 1971} 1972 1973static void 1974sppp_to_event(const struct cp *cp, struct sppp *sp) 1975{ 1976 STDDCL; 1977 int s; 1978 1979 s = splimp(); 1980 if (debug) 1981 log(LOG_DEBUG, SPP_FMT "%s TO(%s) rst_counter = %d\n", 1982 SPP_ARGS(ifp), cp->name, 1983 sppp_state_name(sp->state[cp->protoidx]), 1984 sp->rst_counter[cp->protoidx]); 1985 1986 if (--sp->rst_counter[cp->protoidx] < 0) 1987 /* TO- event */ 1988 switch (sp->state[cp->protoidx]) { 1989 case STATE_CLOSING: 1990 sppp_cp_change_state(cp, sp, STATE_CLOSED); 1991 (cp->tlf)(sp); 1992 break; 1993 case STATE_STOPPING: 1994 sppp_cp_change_state(cp, sp, STATE_STOPPED); 1995 (cp->tlf)(sp); 1996 break; 1997 case STATE_REQ_SENT: 1998 case STATE_ACK_RCVD: 1999 case STATE_ACK_SENT: 2000 sppp_cp_change_state(cp, sp, STATE_STOPPED); 2001 (cp->tlf)(sp); 2002 break; 2003 } 2004 else 2005 /* TO+ event */ 2006 switch (sp->state[cp->protoidx]) { 2007 case STATE_CLOSING: 2008 case STATE_STOPPING: 2009 sppp_cp_send(sp, cp->proto, TERM_REQ, 2010 ++sp->pp_seq[cp->protoidx], 0, 0); 2011 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout, 2012 sp->ch[cp->protoidx]); 2013 break; 2014 case STATE_REQ_SENT: 2015 case STATE_ACK_RCVD: 2016 (cp->scr)(sp); 2017 /* sppp_cp_change_state() will restart the timer */ 2018 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 2019 break; 2020 case STATE_ACK_SENT: 2021 (cp->scr)(sp); 2022 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout, 2023 sp->ch[cp->protoidx]); 2024 break; 2025 } 2026 2027 splx(s); 2028} 2029 2030/* 2031 * Change the state of a control protocol in the state automaton. 2032 * Takes care of starting/stopping the restart timer. 2033 */ 2034static void 2035sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate) 2036{ 2037 sp->state[cp->protoidx] = newstate; 2038 2039 UNTIMEOUT(cp->TO, (void *)sp, sp->ch[cp->protoidx]); 2040 switch (newstate) { 2041 case STATE_INITIAL: 2042 case STATE_STARTING: 2043 case STATE_CLOSED: 2044 case STATE_STOPPED: 2045 case STATE_OPENED: 2046 break; 2047 case STATE_CLOSING: 2048 case STATE_STOPPING: 2049 case STATE_REQ_SENT: 2050 case STATE_ACK_RCVD: 2051 case STATE_ACK_SENT: 2052 TIMEOUT(cp->TO, (void *)sp, sp->lcp.timeout, 2053 sp->ch[cp->protoidx]); 2054 break; 2055 } 2056} 2057 2058/* 2059 *--------------------------------------------------------------------------* 2060 * * 2061 * The LCP implementation. * 2062 * * 2063 *--------------------------------------------------------------------------* 2064 */ 2065static void 2066sppp_lcp_init(struct sppp *sp) 2067{ 2068 sp->lcp.opts = (1 << LCP_OPT_MAGIC); 2069 sp->lcp.magic = 0; 2070 sp->state[IDX_LCP] = STATE_INITIAL; 2071 sp->fail_counter[IDX_LCP] = 0; 2072 sp->pp_seq[IDX_LCP] = 0; 2073 sp->pp_rseq[IDX_LCP] = 0; 2074 sp->lcp.protos = 0; 2075 sp->lcp.mru = sp->lcp.their_mru = PP_MTU; 2076 2077 /* Note that these values are relevant for all control protocols */ 2078 sp->lcp.timeout = 3 * hz; 2079 sp->lcp.max_terminate = 2; 2080 sp->lcp.max_configure = 10; 2081 sp->lcp.max_failure = 10; 2082#if defined(__FreeBSD__) && __FreeBSD__ >= 3 2083 callout_handle_init(&sp->ch[IDX_LCP]); 2084#endif 2085} 2086 2087static void 2088sppp_lcp_up(struct sppp *sp) 2089{ 2090 STDDCL; 2091 2092 sp->pp_alivecnt = 0; 2093 sp->lcp.opts = (1 << LCP_OPT_MAGIC); 2094 sp->lcp.magic = 0; 2095 sp->lcp.protos = 0; 2096 sp->lcp.mru = sp->lcp.their_mru = PP_MTU; 2097 /* 2098 * If we are authenticator, negotiate LCP_AUTH 2099 */ 2100 if (sp->hisauth.proto != 0) 2101 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO); 2102 else 2103 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO); 2104 sp->pp_flags &= ~PP_NEEDAUTH; 2105 /* 2106 * If this interface is passive or dial-on-demand, and we are 2107 * still in Initial state, it means we've got an incoming 2108 * call. Activate the interface. 2109 */ 2110 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) { 2111 if (debug) 2112 log(LOG_DEBUG, 2113 SPP_FMT "Up event", SPP_ARGS(ifp)); 2114 ifp->if_flags |= IFF_RUNNING; 2115 if (sp->state[IDX_LCP] == STATE_INITIAL) { 2116 if (debug) 2117 log(-1, "(incoming call)\n"); 2118 sp->pp_flags |= PP_CALLIN; 2119 lcp.Open(sp); 2120 } else if (debug) 2121 log(-1, "\n"); 2122 } else if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0 && 2123 (sp->state[IDX_LCP] == STATE_INITIAL)) { 2124 ifp->if_flags |= IFF_RUNNING; 2125 lcp.Open(sp); 2126 } 2127 2128 sppp_up_event(&lcp, sp); 2129} 2130 2131static void 2132sppp_lcp_down(struct sppp *sp) 2133{ 2134 STDDCL; 2135 2136 sppp_down_event(&lcp, sp); 2137 2138 /* 2139 * If this is neither a dial-on-demand nor a passive 2140 * interface, simulate an ``ifconfig down'' action, so the 2141 * administrator can force a redial by another ``ifconfig 2142 * up''. XXX For leased line operation, should we immediately 2143 * try to reopen the connection here? 2144 */ 2145 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) { 2146 log(LOG_INFO, 2147 SPP_FMT "Down event, taking interface down.\n", 2148 SPP_ARGS(ifp)); 2149 if_down(ifp); 2150 } else { 2151 if (debug) 2152 log(LOG_DEBUG, 2153 SPP_FMT "Down event (carrier loss)\n", 2154 SPP_ARGS(ifp)); 2155 sp->pp_flags &= ~PP_CALLIN; 2156 if (sp->state[IDX_LCP] != STATE_INITIAL) 2157 lcp.Close(sp); 2158 ifp->if_flags &= ~IFF_RUNNING; 2159 } 2160} 2161 2162static void 2163sppp_lcp_open(struct sppp *sp) 2164{ 2165 sppp_open_event(&lcp, sp); 2166} 2167 2168static void 2169sppp_lcp_close(struct sppp *sp) 2170{ 2171 sppp_close_event(&lcp, sp); 2172} 2173 2174static void 2175sppp_lcp_TO(void *cookie) 2176{ 2177 sppp_to_event(&lcp, (struct sppp *)cookie); 2178} 2179 2180/* 2181 * Analyze a configure request. Return true if it was agreeable, and 2182 * caused action sca, false if it has been rejected or nak'ed, and 2183 * caused action scn. (The return value is used to make the state 2184 * transition decision in the state automaton.) 2185 */ 2186static int 2187sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len) 2188{ 2189 STDDCL; 2190 u_char *buf, *r, *p; 2191 int origlen, rlen; 2192 u_long nmagic; 2193 u_short authproto; 2194 2195 len -= 4; 2196 origlen = len; 2197 buf = r = malloc (len, M_TEMP, M_NOWAIT); 2198 if (! buf) 2199 return (0); 2200 2201 if (debug) 2202 log(LOG_DEBUG, SPP_FMT "lcp parse opts: ", 2203 SPP_ARGS(ifp)); 2204 2205 /* pass 1: check for things that need to be rejected */ 2206 p = (void*) (h+1); 2207 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) { 2208 if (debug) 2209 log(-1, " %s ", sppp_lcp_opt_name(*p)); 2210 switch (*p) { 2211 case LCP_OPT_MAGIC: 2212 /* Magic number. */ 2213 if (len >= 6 && p[1] == 6) 2214 continue; 2215 if (debug) 2216 log(-1, "[invalid] "); 2217 break; 2218 case LCP_OPT_ASYNC_MAP: 2219 /* Async control character map. */ 2220 if (len >= 6 && p[1] == 6) 2221 continue; 2222 if (debug) 2223 log(-1, "[invalid] "); 2224 break; 2225 case LCP_OPT_MRU: 2226 /* Maximum receive unit. */ 2227 if (len >= 4 && p[1] == 4) 2228 continue; 2229 if (debug) 2230 log(-1, "[invalid] "); 2231 break; 2232 case LCP_OPT_AUTH_PROTO: 2233 if (len < 4) { 2234 if (debug) 2235 log(-1, "[invalid] "); 2236 break; 2237 } 2238 authproto = (p[2] << 8) + p[3]; 2239 if (authproto == PPP_CHAP && p[1] != 5) { 2240 if (debug) 2241 log(-1, "[invalid chap len] "); 2242 break; 2243 } 2244 if (sp->myauth.proto == 0) { 2245 /* we are not configured to do auth */ 2246 if (debug) 2247 log(-1, "[not configured] "); 2248 break; 2249 } 2250 /* 2251 * Remote want us to authenticate, remember this, 2252 * so we stay in PHASE_AUTHENTICATE after LCP got 2253 * up. 2254 */ 2255 sp->pp_flags |= PP_NEEDAUTH; 2256 continue; 2257 default: 2258 /* Others not supported. */ 2259 if (debug) 2260 log(-1, "[rej] "); 2261 break; 2262 } 2263 /* Add the option to rejected list. */ 2264 bcopy (p, r, p[1]); 2265 r += p[1]; 2266 rlen += p[1]; 2267 } 2268 if (rlen) { 2269 if (debug) 2270 log(-1, " send conf-rej\n"); 2271 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf); 2272 return 0; 2273 } else if (debug) 2274 log(-1, "\n"); 2275 2276 /* 2277 * pass 2: check for option values that are unacceptable and 2278 * thus require to be nak'ed. 2279 */ 2280 if (debug) 2281 log(LOG_DEBUG, SPP_FMT "lcp parse opt values: ", 2282 SPP_ARGS(ifp)); 2283 2284 p = (void*) (h+1); 2285 len = origlen; 2286 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) { 2287 if (debug) 2288 log(-1, " %s ", sppp_lcp_opt_name(*p)); 2289 switch (*p) { 2290 case LCP_OPT_MAGIC: 2291 /* Magic number -- extract. */ 2292 nmagic = (u_long)p[2] << 24 | 2293 (u_long)p[3] << 16 | p[4] << 8 | p[5]; 2294 if (nmagic != sp->lcp.magic) { 2295 sp->pp_loopcnt = 0; 2296 if (debug) 2297 log(-1, "0x%lx ", nmagic); 2298 continue; 2299 } 2300 if (debug && sp->pp_loopcnt < MAXALIVECNT*5) 2301 log(-1, "[glitch] "); 2302 ++sp->pp_loopcnt; 2303 /* 2304 * We negate our magic here, and NAK it. If 2305 * we see it later in an NAK packet, we 2306 * suggest a new one. 2307 */ 2308 nmagic = ~sp->lcp.magic; 2309 /* Gonna NAK it. */ 2310 p[2] = nmagic >> 24; 2311 p[3] = nmagic >> 16; 2312 p[4] = nmagic >> 8; 2313 p[5] = nmagic; 2314 break; 2315 2316 case LCP_OPT_ASYNC_MAP: 2317 /* 2318 * Async control character map -- just ignore it. 2319 * 2320 * Quote from RFC 1662, chapter 6: 2321 * To enable this functionality, synchronous PPP 2322 * implementations MUST always respond to the 2323 * Async-Control-Character-Map Configuration 2324 * Option with the LCP Configure-Ack. However, 2325 * acceptance of the Configuration Option does 2326 * not imply that the synchronous implementation 2327 * will do any ACCM mapping. Instead, all such 2328 * octet mapping will be performed by the 2329 * asynchronous-to-synchronous converter. 2330 */ 2331 continue; 2332 2333 case LCP_OPT_MRU: 2334 /* 2335 * Maximum receive unit. Always agreeable, 2336 * but ignored by now. 2337 */ 2338 sp->lcp.their_mru = p[2] * 256 + p[3]; 2339 if (debug) 2340 log(-1, "%lu ", sp->lcp.their_mru); 2341 continue; 2342 2343 case LCP_OPT_AUTH_PROTO: 2344 authproto = (p[2] << 8) + p[3]; 2345 if (sp->myauth.proto != authproto) { 2346 /* not agreed, nak */ 2347 if (debug) 2348 log(-1, "[mine %s != his %s] ", 2349 sppp_proto_name(sp->hisauth.proto), 2350 sppp_proto_name(authproto)); 2351 p[2] = sp->myauth.proto >> 8; 2352 p[3] = sp->myauth.proto; 2353 break; 2354 } 2355 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) { 2356 if (debug) 2357 log(-1, "[chap not MD5] "); 2358 p[4] = CHAP_MD5; 2359 break; 2360 } 2361 continue; 2362 } 2363 /* Add the option to nak'ed list. */ 2364 bcopy (p, r, p[1]); 2365 r += p[1]; 2366 rlen += p[1]; 2367 } 2368 if (rlen) { 2369 /* 2370 * Local and remote magics equal -- loopback? 2371 */ 2372 if (sp->pp_loopcnt >= MAXALIVECNT*5) { 2373 if (sp->pp_loopcnt == MAXALIVECNT*5) 2374 printf (SPP_FMT "loopback\n", 2375 SPP_ARGS(ifp)); 2376 if (ifp->if_flags & IFF_UP) { 2377 if_down(ifp); 2378 sppp_qflush(&sp->pp_cpq); 2379 /* XXX ? */ 2380 lcp.Down(sp); 2381 lcp.Up(sp); 2382 } 2383 } else if (!sp->pp_loopcnt && 2384 ++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) { 2385 if (debug) 2386 log(-1, " max_failure (%d) exceeded, " 2387 "send conf-rej\n", 2388 sp->lcp.max_failure); 2389 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf); 2390 } else { 2391 if (debug) 2392 log(-1, " send conf-nak\n"); 2393 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf); 2394 } 2395 } else { 2396 if (debug) 2397 log(-1, " send conf-ack\n"); 2398 sp->fail_counter[IDX_LCP] = 0; 2399 sp->pp_loopcnt = 0; 2400 sppp_cp_send (sp, PPP_LCP, CONF_ACK, 2401 h->ident, origlen, h+1); 2402 } 2403 2404 free (buf, M_TEMP); 2405 return (rlen == 0); 2406} 2407 2408/* 2409 * Analyze the LCP Configure-Reject option list, and adjust our 2410 * negotiation. 2411 */ 2412static void 2413sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len) 2414{ 2415 STDDCL; 2416 u_char *buf, *p; 2417 2418 len -= 4; 2419 buf = malloc (len, M_TEMP, M_NOWAIT); 2420 if (!buf) 2421 return; 2422 2423 if (debug) 2424 log(LOG_DEBUG, SPP_FMT "lcp rej opts: ", 2425 SPP_ARGS(ifp)); 2426 2427 p = (void*) (h+1); 2428 for (; len > 1 && p[1]; len -= p[1], p += p[1]) { 2429 if (debug) 2430 log(-1, " %s ", sppp_lcp_opt_name(*p)); 2431 switch (*p) { 2432 case LCP_OPT_MAGIC: 2433 /* Magic number -- can't use it, use 0 */ 2434 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC); 2435 sp->lcp.magic = 0; 2436 break; 2437 case LCP_OPT_MRU: 2438 /* 2439 * Should not be rejected anyway, since we only 2440 * negotiate a MRU if explicitly requested by 2441 * peer. 2442 */ 2443 sp->lcp.opts &= ~(1 << LCP_OPT_MRU); 2444 break; 2445 case LCP_OPT_AUTH_PROTO: 2446 /* 2447 * Peer doesn't want to authenticate himself, 2448 * deny unless this is a dialout call, and 2449 * AUTHFLAG_NOCALLOUT is set. 2450 */ 2451 if ((sp->pp_flags & PP_CALLIN) == 0 && 2452 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) { 2453 if (debug) 2454 log(-1, "[don't insist on auth " 2455 "for callout]"); 2456 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO); 2457 break; 2458 } 2459 if (debug) 2460 log(-1, "[access denied]\n"); 2461 lcp.Close(sp); 2462 break; 2463 } 2464 } 2465 if (debug) 2466 log(-1, "\n"); 2467 free (buf, M_TEMP); 2468 return; 2469} 2470 2471/* 2472 * Analyze the LCP Configure-NAK option list, and adjust our 2473 * negotiation. 2474 */ 2475static void 2476sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len) 2477{ 2478 STDDCL; 2479 u_char *buf, *p; 2480 u_long magic; 2481 2482 len -= 4; 2483 buf = malloc (len, M_TEMP, M_NOWAIT); 2484 if (!buf) 2485 return; 2486 2487 if (debug) 2488 log(LOG_DEBUG, SPP_FMT "lcp nak opts: ", 2489 SPP_ARGS(ifp)); 2490 2491 p = (void*) (h+1); 2492 for (; len > 1 && p[1]; len -= p[1], p += p[1]) { 2493 if (debug) 2494 log(-1, " %s ", sppp_lcp_opt_name(*p)); 2495 switch (*p) { 2496 case LCP_OPT_MAGIC: 2497 /* Magic number -- renegotiate */ 2498 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) && 2499 len >= 6 && p[1] == 6) { 2500 magic = (u_long)p[2] << 24 | 2501 (u_long)p[3] << 16 | p[4] << 8 | p[5]; 2502 /* 2503 * If the remote magic is our negated one, 2504 * this looks like a loopback problem. 2505 * Suggest a new magic to make sure. 2506 */ 2507 if (magic == ~sp->lcp.magic) { 2508 if (debug) 2509 log(-1, "magic glitch "); 2510#if defined(__FreeBSD__) && __FreeBSD__ >= 3 2511 sp->lcp.magic = random(); 2512#else 2513 sp->lcp.magic = time.tv_sec + time.tv_usec; 2514#endif 2515 } else { 2516 sp->lcp.magic = magic; 2517 if (debug) 2518 log(-1, "%lu ", magic); 2519 } 2520 } 2521 break; 2522 case LCP_OPT_MRU: 2523 /* 2524 * Peer wants to advise us to negotiate an MRU. 2525 * Agree on it if it's reasonable, or use 2526 * default otherwise. 2527 */ 2528 if (len >= 4 && p[1] == 4) { 2529 u_int mru = p[2] * 256 + p[3]; 2530 if (debug) 2531 log(-1, "%d ", mru); 2532 if (mru < PP_MTU || mru > PP_MAX_MRU) 2533 mru = PP_MTU; 2534 sp->lcp.mru = mru; 2535 sp->lcp.opts |= (1 << LCP_OPT_MRU); 2536 } 2537 break; 2538 case LCP_OPT_AUTH_PROTO: 2539 /* 2540 * Peer doesn't like our authentication method, 2541 * deny. 2542 */ 2543 if (debug) 2544 log(-1, "[access denied]\n"); 2545 lcp.Close(sp); 2546 break; 2547 } 2548 } 2549 if (debug) 2550 log(-1, "\n"); 2551 free (buf, M_TEMP); 2552 return; 2553} 2554 2555static void 2556sppp_lcp_tlu(struct sppp *sp) 2557{ 2558 STDDCL; 2559 int i; 2560 u_long mask; 2561 2562 /* XXX ? */ 2563 if (! (ifp->if_flags & IFF_UP) && 2564 (ifp->if_flags & IFF_RUNNING)) { 2565 /* Coming out of loopback mode. */ 2566 if_up(ifp); 2567 printf (SPP_FMT "up\n", SPP_ARGS(ifp)); 2568 } 2569 2570 for (i = 0; i < IDX_COUNT; i++) 2571 if ((cps[i])->flags & CP_QUAL) 2572 (cps[i])->Open(sp); 2573 2574 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 || 2575 (sp->pp_flags & PP_NEEDAUTH) != 0) 2576 sp->pp_phase = PHASE_AUTHENTICATE; 2577 else 2578 sp->pp_phase = PHASE_NETWORK; 2579 2580 if (debug) 2581 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp), 2582 sppp_phase_name(sp->pp_phase)); 2583 2584 /* 2585 * Open all authentication protocols. This is even required 2586 * if we already proceeded to network phase, since it might be 2587 * that remote wants us to authenticate, so we might have to 2588 * send a PAP request. Undesired authentication protocols 2589 * don't do anything when they get an Open event. 2590 */ 2591 for (i = 0; i < IDX_COUNT; i++) 2592 if ((cps[i])->flags & CP_AUTH) 2593 (cps[i])->Open(sp); 2594 2595 if (sp->pp_phase == PHASE_NETWORK) { 2596 /* Notify all NCPs. */ 2597 for (i = 0; i < IDX_COUNT; i++) 2598 if (((cps[i])->flags & CP_NCP) && 2599 /* 2600 * XXX 2601 * Hack to administratively disable IPv6 if 2602 * not desired. Perhaps we should have another 2603 * flag for this, but right now, we can make 2604 * all struct cp's read/only. 2605 */ 2606 (cps[i] != &ipv6cp || 2607 (sp->confflags & CONF_ENABLE_IPV6))) 2608 (cps[i])->Open(sp); 2609 } 2610 2611 /* Send Up events to all started protos. */ 2612 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1) 2613 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) 2614 (cps[i])->Up(sp); 2615 2616 /* notify low-level driver of state change */ 2617 if (sp->pp_chg) 2618 sp->pp_chg(sp, (int)sp->pp_phase); 2619 2620 if (sp->pp_phase == PHASE_NETWORK) 2621 /* if no NCP is starting, close down */ 2622 sppp_lcp_check_and_close(sp); 2623} 2624 2625static void 2626sppp_lcp_tld(struct sppp *sp) 2627{ 2628 STDDCL; 2629 int i; 2630 u_long mask; 2631 2632 sp->pp_phase = PHASE_TERMINATE; 2633 2634 if (debug) 2635 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp), 2636 sppp_phase_name(sp->pp_phase)); 2637 2638 /* 2639 * Take upper layers down. We send the Down event first and 2640 * the Close second to prevent the upper layers from sending 2641 * ``a flurry of terminate-request packets'', as the RFC 2642 * describes it. 2643 */ 2644 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1) 2645 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_LCP) == 0) { 2646 (cps[i])->Down(sp); 2647 (cps[i])->Close(sp); 2648 } 2649} 2650 2651static void 2652sppp_lcp_tls(struct sppp *sp) 2653{ 2654 STDDCL; 2655 2656 sp->pp_phase = PHASE_ESTABLISH; 2657 2658 if (debug) 2659 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp), 2660 sppp_phase_name(sp->pp_phase)); 2661 2662 /* Notify lower layer if desired. */ 2663 if (sp->pp_tls) 2664 (sp->pp_tls)(sp); 2665 else 2666 (sp->pp_up)(sp); 2667} 2668 2669static void 2670sppp_lcp_tlf(struct sppp *sp) 2671{ 2672 STDDCL; 2673 2674 sp->pp_phase = PHASE_DEAD; 2675 if (debug) 2676 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp), 2677 sppp_phase_name(sp->pp_phase)); 2678 2679 /* Notify lower layer if desired. */ 2680 if (sp->pp_tlf) 2681 (sp->pp_tlf)(sp); 2682 else 2683 (sp->pp_down)(sp); 2684} 2685 2686static void 2687sppp_lcp_scr(struct sppp *sp) 2688{ 2689 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */]; 2690 int i = 0; 2691 u_short authproto; 2692 2693 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) { 2694 if (! sp->lcp.magic) 2695#if defined(__FreeBSD__) && __FreeBSD__ >= 3 2696 sp->lcp.magic = random(); 2697#else 2698 sp->lcp.magic = time.tv_sec + time.tv_usec; 2699#endif 2700 opt[i++] = LCP_OPT_MAGIC; 2701 opt[i++] = 6; 2702 opt[i++] = sp->lcp.magic >> 24; 2703 opt[i++] = sp->lcp.magic >> 16; 2704 opt[i++] = sp->lcp.magic >> 8; 2705 opt[i++] = sp->lcp.magic; 2706 } 2707 2708 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) { 2709 opt[i++] = LCP_OPT_MRU; 2710 opt[i++] = 4; 2711 opt[i++] = sp->lcp.mru >> 8; 2712 opt[i++] = sp->lcp.mru; 2713 } 2714 2715 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) { 2716 authproto = sp->hisauth.proto; 2717 opt[i++] = LCP_OPT_AUTH_PROTO; 2718 opt[i++] = authproto == PPP_CHAP? 5: 4; 2719 opt[i++] = authproto >> 8; 2720 opt[i++] = authproto; 2721 if (authproto == PPP_CHAP) 2722 opt[i++] = CHAP_MD5; 2723 } 2724 2725 sp->confid[IDX_LCP] = ++sp->pp_seq[IDX_LCP]; 2726 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt); 2727} 2728 2729/* 2730 * Check the open NCPs, return true if at least one NCP is open. 2731 */ 2732static int 2733sppp_ncp_check(struct sppp *sp) 2734{ 2735 int i, mask; 2736 2737 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1) 2738 if ((sp->lcp.protos & mask) && (cps[i])->flags & CP_NCP) 2739 return 1; 2740 return 0; 2741} 2742 2743/* 2744 * Re-check the open NCPs and see if we should terminate the link. 2745 * Called by the NCPs during their tlf action handling. 2746 */ 2747static void 2748sppp_lcp_check_and_close(struct sppp *sp) 2749{ 2750 2751 if (sp->pp_phase < PHASE_NETWORK) 2752 /* don't bother, we are already going down */ 2753 return; 2754 2755 if (sppp_ncp_check(sp)) 2756 return; 2757 2758 lcp.Close(sp); 2759} 2760 2761/* 2762 *--------------------------------------------------------------------------* 2763 * * 2764 * The IPCP implementation. * 2765 * * 2766 *--------------------------------------------------------------------------* 2767 */ 2768 2769static void 2770sppp_ipcp_init(struct sppp *sp) 2771{ 2772 sp->ipcp.opts = 0; 2773 sp->ipcp.flags = 0; 2774 sp->state[IDX_IPCP] = STATE_INITIAL; 2775 sp->fail_counter[IDX_IPCP] = 0; 2776 sp->pp_seq[IDX_IPCP] = 0; 2777 sp->pp_rseq[IDX_IPCP] = 0; 2778#if defined(__FreeBSD__) && __FreeBSD__ >= 3 2779 callout_handle_init(&sp->ch[IDX_IPCP]); 2780#endif 2781} 2782 2783static void 2784sppp_ipcp_up(struct sppp *sp) 2785{ 2786 sppp_up_event(&ipcp, sp); 2787} 2788 2789static void 2790sppp_ipcp_down(struct sppp *sp) 2791{ 2792 sppp_down_event(&ipcp, sp); 2793} 2794 2795static void 2796sppp_ipcp_open(struct sppp *sp) 2797{ 2798 STDDCL; 2799 u_long myaddr, hisaddr; 2800 2801 sp->ipcp.flags &= ~(IPCP_HISADDR_SEEN | IPCP_MYADDR_SEEN | 2802 IPCP_MYADDR_DYN | IPCP_VJ); 2803 sp->ipcp.opts = 0; 2804 2805 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0); 2806 /* 2807 * If we don't have his address, this probably means our 2808 * interface doesn't want to talk IP at all. (This could 2809 * be the case if somebody wants to speak only IPX, for 2810 * example.) Don't open IPCP in this case. 2811 */ 2812 if (hisaddr == 0L) { 2813 /* XXX this message should go away */ 2814 if (debug) 2815 log(LOG_DEBUG, SPP_FMT "ipcp_open(): no IP interface\n", 2816 SPP_ARGS(ifp)); 2817 return; 2818 } 2819 if (myaddr == 0L) { 2820 /* 2821 * I don't have an assigned address, so i need to 2822 * negotiate my address. 2823 */ 2824 sp->ipcp.flags |= IPCP_MYADDR_DYN; 2825 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS); 2826 } else 2827 sp->ipcp.flags |= IPCP_MYADDR_SEEN; 2828 if (sp->confflags & CONF_ENABLE_VJ) { 2829 sp->ipcp.opts |= (1 << IPCP_OPT_COMPRESSION); 2830 sp->ipcp.max_state = MAX_STATES - 1; 2831 sp->ipcp.compress_cid = 1; 2832 } 2833 sppp_open_event(&ipcp, sp); 2834} 2835 2836static void 2837sppp_ipcp_close(struct sppp *sp) 2838{ 2839 sppp_close_event(&ipcp, sp); 2840 if (sp->ipcp.flags & IPCP_MYADDR_DYN) 2841 /* 2842 * My address was dynamic, clear it again. 2843 */ 2844 sppp_set_ip_addr(sp, 0L); 2845} 2846 2847static void 2848sppp_ipcp_TO(void *cookie) 2849{ 2850 sppp_to_event(&ipcp, (struct sppp *)cookie); 2851} 2852 2853/* 2854 * Analyze a configure request. Return true if it was agreeable, and 2855 * caused action sca, false if it has been rejected or nak'ed, and 2856 * caused action scn. (The return value is used to make the state 2857 * transition decision in the state automaton.) 2858 */ 2859static int 2860sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len) 2861{ 2862 u_char *buf, *r, *p; 2863 struct ifnet *ifp = &sp->pp_if; 2864 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG; 2865 u_long hisaddr, desiredaddr; 2866 int gotmyaddr = 0; 2867 int desiredcomp; 2868 2869 len -= 4; 2870 origlen = len; 2871 /* 2872 * Make sure to allocate a buf that can at least hold a 2873 * conf-nak with an `address' option. We might need it below. 2874 */ 2875 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT); 2876 if (! buf) 2877 return (0); 2878 2879 /* pass 1: see if we can recognize them */ 2880 if (debug) 2881 log(LOG_DEBUG, SPP_FMT "ipcp parse opts: ", 2882 SPP_ARGS(ifp)); 2883 p = (void*) (h+1); 2884 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) { 2885 if (debug) 2886 log(-1, " %s ", sppp_ipcp_opt_name(*p)); 2887 switch (*p) { 2888 case IPCP_OPT_COMPRESSION: 2889 if (!(sp->confflags & CONF_ENABLE_VJ)) { 2890 /* VJ compression administratively disabled */ 2891 if (debug) 2892 log(-1, "[locally disabled] "); 2893 break; 2894 } 2895 /* 2896 * In theory, we should only conf-rej an 2897 * option that is shorter than RFC 1618 2898 * requires (i.e. < 4), and should conf-nak 2899 * anything else that is not VJ. However, 2900 * since our algorithm always uses the 2901 * original option to NAK it with new values, 2902 * things would become more complicated. In 2903 * pratice, the only commonly implemented IP 2904 * compression option is VJ anyway, so the 2905 * difference is negligible. 2906 */ 2907 if (len >= 6 && p[1] == 6) { 2908 /* 2909 * correctly formed compression option 2910 * that could be VJ compression 2911 */ 2912 continue; 2913 } 2914 if (debug) 2915 log(-1, 2916 "optlen %d [invalid/unsupported] ", 2917 p[1]); 2918 break; 2919 case IPCP_OPT_ADDRESS: 2920 if (len >= 6 && p[1] == 6) { 2921 /* correctly formed address option */ 2922 continue; 2923 } 2924 if (debug) 2925 log(-1, "[invalid] "); 2926 break; 2927 default: 2928 /* Others not supported. */ 2929 if (debug) 2930 log(-1, "[rej] "); 2931 break; 2932 } 2933 /* Add the option to rejected list. */ 2934 bcopy (p, r, p[1]); 2935 r += p[1]; 2936 rlen += p[1]; 2937 } 2938 if (rlen) { 2939 if (debug) 2940 log(-1, " send conf-rej\n"); 2941 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf); 2942 return 0; 2943 } else if (debug) 2944 log(-1, "\n"); 2945 2946 /* pass 2: parse option values */ 2947 sppp_get_ip_addrs(sp, 0, &hisaddr, 0); 2948 if (debug) 2949 log(LOG_DEBUG, SPP_FMT "ipcp parse opt values: ", 2950 SPP_ARGS(ifp)); 2951 p = (void*) (h+1); 2952 len = origlen; 2953 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) { 2954 if (debug) 2955 log(-1, " %s ", sppp_ipcp_opt_name(*p)); 2956 switch (*p) { 2957 case IPCP_OPT_COMPRESSION: 2958 desiredcomp = p[2] << 8 | p[3]; 2959 /* We only support VJ */ 2960 if (desiredcomp == IPCP_COMP_VJ) { 2961 if (debug) 2962 log(-1, "VJ [ack] "); 2963 sp->ipcp.flags |= IPCP_VJ; 2964 sl_compress_init(sp->pp_comp, p[4]); 2965 sp->ipcp.max_state = p[4]; 2966 sp->ipcp.compress_cid = p[5]; 2967 continue; 2968 } 2969 if (debug) 2970 log(-1, 2971 "compproto %#04x [not supported] ", 2972 desiredcomp); 2973 p[2] = IPCP_COMP_VJ >> 8; 2974 p[3] = IPCP_COMP_VJ; 2975 p[4] = sp->ipcp.max_state; 2976 p[5] = sp->ipcp.compress_cid; 2977 break; 2978 case IPCP_OPT_ADDRESS: 2979 /* This is the address he wants in his end */ 2980 desiredaddr = p[2] << 24 | p[3] << 16 | 2981 p[4] << 8 | p[5]; 2982 if (desiredaddr == hisaddr || 2983 (hisaddr >= 1 && hisaddr <= 254 && desiredaddr != 0)) { 2984 /* 2985 * Peer's address is same as our value, 2986 * or we have set it to 0.0.0.* to 2987 * indicate that we do not really care, 2988 * this is agreeable. Gonna conf-ack 2989 * it. 2990 */ 2991 if (debug) 2992 log(-1, "%s [ack] ", 2993 sppp_dotted_quad(hisaddr)); 2994 /* record that we've seen it already */ 2995 sp->ipcp.flags |= IPCP_HISADDR_SEEN; 2996 continue; 2997 } 2998 /* 2999 * The address wasn't agreeable. This is either 3000 * he sent us 0.0.0.0, asking to assign him an 3001 * address, or he send us another address not 3002 * matching our value. Either case, we gonna 3003 * conf-nak it with our value. 3004 * XXX: we should "rej" if hisaddr == 0 3005 */ 3006 if (debug) { 3007 if (desiredaddr == 0) 3008 log(-1, "[addr requested] "); 3009 else 3010 log(-1, "%s [not agreed] ", 3011 sppp_dotted_quad(desiredaddr)); 3012 3013 } 3014 p[2] = hisaddr >> 24; 3015 p[3] = hisaddr >> 16; 3016 p[4] = hisaddr >> 8; 3017 p[5] = hisaddr; 3018 break; 3019 } 3020 /* Add the option to nak'ed list. */ 3021 bcopy (p, r, p[1]); 3022 r += p[1]; 3023 rlen += p[1]; 3024 } 3025 3026 /* 3027 * If we are about to conf-ack the request, but haven't seen 3028 * his address so far, gonna conf-nak it instead, with the 3029 * `address' option present and our idea of his address being 3030 * filled in there, to request negotiation of both addresses. 3031 * 3032 * XXX This can result in an endless req - nak loop if peer 3033 * doesn't want to send us his address. Q: What should we do 3034 * about it? XXX A: implement the max-failure counter. 3035 */ 3036 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN) && !gotmyaddr) { 3037 buf[0] = IPCP_OPT_ADDRESS; 3038 buf[1] = 6; 3039 buf[2] = hisaddr >> 24; 3040 buf[3] = hisaddr >> 16; 3041 buf[4] = hisaddr >> 8; 3042 buf[5] = hisaddr; 3043 rlen = 6; 3044 if (debug) 3045 log(-1, "still need hisaddr "); 3046 } 3047 3048 if (rlen) { 3049 if (debug) 3050 log(-1, " send conf-nak\n"); 3051 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf); 3052 } else { 3053 if (debug) 3054 log(-1, " send conf-ack\n"); 3055 sppp_cp_send (sp, PPP_IPCP, CONF_ACK, 3056 h->ident, origlen, h+1); 3057 } 3058 3059 free (buf, M_TEMP); 3060 return (rlen == 0); 3061} 3062 3063/* 3064 * Analyze the IPCP Configure-Reject option list, and adjust our 3065 * negotiation. 3066 */ 3067static void 3068sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len) 3069{ 3070 u_char *buf, *p; 3071 struct ifnet *ifp = &sp->pp_if; 3072 int debug = ifp->if_flags & IFF_DEBUG; 3073 3074 len -= 4; 3075 buf = malloc (len, M_TEMP, M_NOWAIT); 3076 if (!buf) 3077 return; 3078 3079 if (debug) 3080 log(LOG_DEBUG, SPP_FMT "ipcp rej opts: ", 3081 SPP_ARGS(ifp)); 3082 3083 p = (void*) (h+1); 3084 for (; len > 1 && p[1]; len -= p[1], p += p[1]) { 3085 if (debug) 3086 log(-1, " %s ", sppp_ipcp_opt_name(*p)); 3087 switch (*p) { 3088 case IPCP_OPT_COMPRESSION: 3089 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESSION); 3090 break; 3091 case IPCP_OPT_ADDRESS: 3092 /* 3093 * Peer doesn't grok address option. This is 3094 * bad. XXX Should we better give up here? 3095 * XXX We could try old "addresses" option... 3096 */ 3097 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS); 3098 break; 3099 } 3100 } 3101 if (debug) 3102 log(-1, "\n"); 3103 free (buf, M_TEMP); 3104 return; 3105} 3106 3107/* 3108 * Analyze the IPCP Configure-NAK option list, and adjust our 3109 * negotiation. 3110 */ 3111static void 3112sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len) 3113{ 3114 u_char *buf, *p; 3115 struct ifnet *ifp = &sp->pp_if; 3116 int debug = ifp->if_flags & IFF_DEBUG; 3117 int desiredcomp; 3118 u_long wantaddr; 3119 3120 len -= 4; 3121 buf = malloc (len, M_TEMP, M_NOWAIT); 3122 if (!buf) 3123 return; 3124 3125 if (debug) 3126 log(LOG_DEBUG, SPP_FMT "ipcp nak opts: ", 3127 SPP_ARGS(ifp)); 3128 3129 p = (void*) (h+1); 3130 for (; len > 1 && p[1]; len -= p[1], p += p[1]) { 3131 if (debug) 3132 log(-1, " %s ", sppp_ipcp_opt_name(*p)); 3133 switch (*p) { 3134 case IPCP_OPT_COMPRESSION: 3135 if (len >= 6 && p[1] == 6) { 3136 desiredcomp = p[2] << 8 | p[3]; 3137 if (debug) 3138 log(-1, "[wantcomp %#04x] ", 3139 desiredcomp); 3140 if (desiredcomp == IPCP_COMP_VJ) { 3141 sl_compress_init(sp->pp_comp, p[4]); 3142 sp->ipcp.max_state = p[4]; 3143 sp->ipcp.compress_cid = p[5]; 3144 if (debug) 3145 log(-1, "[agree] "); 3146 } else 3147 sp->ipcp.opts &= 3148 ~(1 << IPCP_OPT_COMPRESSION); 3149 } 3150 break; 3151 case IPCP_OPT_ADDRESS: 3152 /* 3153 * Peer doesn't like our local IP address. See 3154 * if we can do something for him. We'll drop 3155 * him our address then. 3156 */ 3157 if (len >= 6 && p[1] == 6) { 3158 wantaddr = p[2] << 24 | p[3] << 16 | 3159 p[4] << 8 | p[5]; 3160 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS); 3161 if (debug) 3162 log(-1, "[wantaddr %s] ", 3163 sppp_dotted_quad(wantaddr)); 3164 /* 3165 * When doing dynamic address assignment, 3166 * we accept his offer. Otherwise, we 3167 * ignore it and thus continue to negotiate 3168 * our already existing value. 3169 * XXX: Bogus, if he said no once, he'll 3170 * just say no again, might as well die. 3171 */ 3172 if (sp->ipcp.flags & IPCP_MYADDR_DYN) { 3173 sppp_set_ip_addr(sp, wantaddr); 3174 if (debug) 3175 log(-1, "[agree] "); 3176 sp->ipcp.flags |= IPCP_MYADDR_SEEN; 3177 } 3178 } 3179 break; 3180 } 3181 } 3182 if (debug) 3183 log(-1, "\n"); 3184 free (buf, M_TEMP); 3185 return; 3186} 3187 3188static void 3189sppp_ipcp_tlu(struct sppp *sp) 3190{ 3191 /* we are up - notify isdn daemon */ 3192 if (sp->pp_con) 3193 sp->pp_con(sp); 3194} 3195 3196static void 3197sppp_ipcp_tld(struct sppp *sp) 3198{ 3199} 3200 3201static void 3202sppp_ipcp_tls(struct sppp *sp) 3203{ 3204 /* indicate to LCP that it must stay alive */ 3205 sp->lcp.protos |= (1 << IDX_IPCP); 3206} 3207 3208static void 3209sppp_ipcp_tlf(struct sppp *sp) 3210{ 3211 /* we no longer need LCP */ 3212 sp->lcp.protos &= ~(1 << IDX_IPCP); 3213 sppp_lcp_check_and_close(sp); 3214} 3215 3216static void 3217sppp_ipcp_scr(struct sppp *sp) 3218{ 3219 char opt[6 /* compression */ + 6 /* address */]; 3220 u_long ouraddr; 3221 int i = 0; 3222 3223 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) { 3224 opt[i++] = IPCP_OPT_COMPRESSION; 3225 opt[i++] = 6; 3226 opt[i++] = IPCP_COMP_VJ >> 8; 3227 opt[i++] = IPCP_COMP_VJ; 3228 opt[i++] = sp->ipcp.max_state; 3229 opt[i++] = sp->ipcp.compress_cid; 3230 } 3231 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) { 3232 sppp_get_ip_addrs(sp, &ouraddr, 0, 0); 3233 opt[i++] = IPCP_OPT_ADDRESS; 3234 opt[i++] = 6; 3235 opt[i++] = ouraddr >> 24; 3236 opt[i++] = ouraddr >> 16; 3237 opt[i++] = ouraddr >> 8; 3238 opt[i++] = ouraddr; 3239 } 3240 3241 sp->confid[IDX_IPCP] = ++sp->pp_seq[IDX_IPCP]; 3242 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt); 3243} 3244 3245/* 3246 *--------------------------------------------------------------------------* 3247 * * 3248 * The IPv6CP implementation. * 3249 * * 3250 *--------------------------------------------------------------------------* 3251 */ 3252 3253#ifdef INET6 3254static void 3255sppp_ipv6cp_init(struct sppp *sp) 3256{ 3257 sp->ipv6cp.opts = 0; 3258 sp->ipv6cp.flags = 0; 3259 sp->state[IDX_IPV6CP] = STATE_INITIAL; 3260 sp->fail_counter[IDX_IPV6CP] = 0; 3261 sp->pp_seq[IDX_IPV6CP] = 0; 3262 sp->pp_rseq[IDX_IPV6CP] = 0; 3263#if defined(__NetBSD__) 3264 callout_init(&sp->ch[IDX_IPV6CP]); 3265#endif 3266#if defined(__FreeBSD__) && __FreeBSD__ >= 3 3267 callout_handle_init(&sp->ch[IDX_IPV6CP]); 3268#endif 3269} 3270 3271static void 3272sppp_ipv6cp_up(struct sppp *sp) 3273{ 3274 sppp_up_event(&ipv6cp, sp); 3275} 3276 3277static void 3278sppp_ipv6cp_down(struct sppp *sp) 3279{ 3280 sppp_down_event(&ipv6cp, sp); 3281} 3282 3283static void 3284sppp_ipv6cp_open(struct sppp *sp) 3285{ 3286 STDDCL; 3287 struct in6_addr myaddr, hisaddr; 3288 3289#ifdef IPV6CP_MYIFID_DYN 3290 sp->ipv6cp.flags &= ~(IPV6CP_MYIFID_SEEN|IPV6CP_MYIFID_DYN); 3291#else 3292 sp->ipv6cp.flags &= ~IPV6CP_MYIFID_SEEN; 3293#endif 3294 3295 sppp_get_ip6_addrs(sp, &myaddr, &hisaddr, 0); 3296 /* 3297 * If we don't have our address, this probably means our 3298 * interface doesn't want to talk IPv6 at all. (This could 3299 * be the case if somebody wants to speak only IPX, for 3300 * example.) Don't open IPv6CP in this case. 3301 */ 3302 if (IN6_IS_ADDR_UNSPECIFIED(&myaddr)) { 3303 /* XXX this message should go away */ 3304 if (debug) 3305 log(LOG_DEBUG, SPP_FMT "ipv6cp_open(): no IPv6 interface\n", 3306 SPP_ARGS(ifp)); 3307 return; 3308 } 3309 3310 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN; 3311 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID); 3312 sppp_open_event(&ipv6cp, sp); 3313} 3314 3315static void 3316sppp_ipv6cp_close(struct sppp *sp) 3317{ 3318 sppp_close_event(&ipv6cp, sp); 3319} 3320 3321static void 3322sppp_ipv6cp_TO(void *cookie) 3323{ 3324 sppp_to_event(&ipv6cp, (struct sppp *)cookie); 3325} 3326 3327/* 3328 * Analyze a configure request. Return true if it was agreeable, and 3329 * caused action sca, false if it has been rejected or nak'ed, and 3330 * caused action scn. (The return value is used to make the state 3331 * transition decision in the state automaton.) 3332 */ 3333static int 3334sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len) 3335{ 3336 u_char *buf, *r, *p; 3337 struct ifnet *ifp = &sp->pp_if; 3338 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG; 3339 struct in6_addr myaddr, desiredaddr, suggestaddr; 3340 int ifidcount; 3341 int type; 3342 int collision, nohisaddr; 3343 3344 len -= 4; 3345 origlen = len; 3346 /* 3347 * Make sure to allocate a buf that can at least hold a 3348 * conf-nak with an `address' option. We might need it below. 3349 */ 3350 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT); 3351 if (! buf) 3352 return (0); 3353 3354 /* pass 1: see if we can recognize them */ 3355 if (debug) 3356 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opts:", 3357 SPP_ARGS(ifp)); 3358 p = (void*) (h+1); 3359 ifidcount = 0; 3360 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) { 3361 if (debug) 3362 log(-1, " %s", sppp_ipv6cp_opt_name(*p)); 3363 switch (*p) { 3364 case IPV6CP_OPT_IFID: 3365 if (len >= 10 && p[1] == 10 && ifidcount == 0) { 3366 /* correctly formed address option */ 3367 ifidcount++; 3368 continue; 3369 } 3370 if (debug) 3371 log(-1, " [invalid]"); 3372 break; 3373#ifdef notyet 3374 case IPV6CP_OPT_COMPRESSION: 3375 if (len >= 4 && p[1] >= 4) { 3376 /* correctly formed compress option */ 3377 continue; 3378 } 3379 if (debug) 3380 log(-1, " [invalid]"); 3381 break; 3382#endif 3383 default: 3384 /* Others not supported. */ 3385 if (debug) 3386 log(-1, " [rej]"); 3387 break; 3388 } 3389 /* Add the option to rejected list. */ 3390 bcopy (p, r, p[1]); 3391 r += p[1]; 3392 rlen += p[1]; 3393 } 3394 if (rlen) { 3395 if (debug) 3396 log(-1, " send conf-rej\n"); 3397 sppp_cp_send (sp, PPP_IPV6CP, CONF_REJ, h->ident, rlen, buf); 3398 goto end; 3399 } else if (debug) 3400 log(-1, "\n"); 3401 3402 /* pass 2: parse option values */ 3403 sppp_get_ip6_addrs(sp, &myaddr, 0, 0); 3404 if (debug) 3405 log(LOG_DEBUG, SPP_FMT "ipv6cp parse opt values: ", 3406 SPP_ARGS(ifp)); 3407 p = (void*) (h+1); 3408 len = origlen; 3409 type = CONF_ACK; 3410 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) { 3411 if (debug) 3412 log(-1, " %s", sppp_ipv6cp_opt_name(*p)); 3413 switch (*p) { 3414#ifdef notyet 3415 case IPV6CP_OPT_COMPRESSION: 3416 continue; 3417#endif 3418 case IPV6CP_OPT_IFID: 3419 bzero(&desiredaddr, sizeof(desiredaddr)); 3420 bcopy(&p[2], &desiredaddr.s6_addr[8], 8); 3421 collision = (bcmp(&desiredaddr.s6_addr[8], 3422 &myaddr.s6_addr[8], 8) == 0); 3423 nohisaddr = IN6_IS_ADDR_UNSPECIFIED(&desiredaddr); 3424 3425 desiredaddr.s6_addr16[0] = htons(0xfe80); 3426 desiredaddr.s6_addr16[1] = htons(sp->pp_if.if_index); 3427 3428 if (!collision && !nohisaddr) { 3429 /* no collision, hisaddr known - Conf-Ack */ 3430 type = CONF_ACK; 3431 3432 if (debug) { 3433 log(-1, " %s [%s]", 3434 ip6_sprintf(&desiredaddr), 3435 sppp_cp_type_name(type)); 3436 } 3437 continue; 3438 } 3439 3440 bzero(&suggestaddr, sizeof(&suggestaddr)); 3441 if (collision && nohisaddr) { 3442 /* collision, hisaddr unknown - Conf-Rej */ 3443 type = CONF_REJ; 3444 bzero(&p[2], 8); 3445 } else { 3446 /* 3447 * - no collision, hisaddr unknown, or 3448 * - collision, hisaddr known 3449 * Conf-Nak, suggest hisaddr 3450 */ 3451 type = CONF_NAK; 3452 sppp_suggest_ip6_addr(sp, &suggestaddr); 3453 bcopy(&suggestaddr.s6_addr[8], &p[2], 8); 3454 } 3455 if (debug) 3456 log(-1, " %s [%s]", ip6_sprintf(&desiredaddr), 3457 sppp_cp_type_name(type)); 3458 break; 3459 } 3460 /* Add the option to nak'ed list. */ 3461 bcopy (p, r, p[1]); 3462 r += p[1]; 3463 rlen += p[1]; 3464 } 3465 3466 if (rlen == 0 && type == CONF_ACK) { 3467 if (debug) 3468 log(-1, " send %s\n", sppp_cp_type_name(type)); 3469 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, origlen, h+1); 3470 } else { 3471#ifdef DIAGNOSTIC 3472 if (type == CONF_ACK) 3473 panic("IPv6CP RCR: CONF_ACK with non-zero rlen"); 3474#endif 3475 3476 if (debug) { 3477 log(-1, " send %s suggest %s\n", 3478 sppp_cp_type_name(type), ip6_sprintf(&suggestaddr)); 3479 } 3480 sppp_cp_send (sp, PPP_IPV6CP, type, h->ident, rlen, buf); 3481 } 3482 3483 end: 3484 free (buf, M_TEMP); 3485 return (rlen == 0); 3486} 3487 3488/* 3489 * Analyze the IPv6CP Configure-Reject option list, and adjust our 3490 * negotiation. 3491 */ 3492static void 3493sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len) 3494{ 3495 u_char *buf, *p; 3496 struct ifnet *ifp = &sp->pp_if; 3497 int debug = ifp->if_flags & IFF_DEBUG; 3498 3499 len -= 4; 3500 buf = malloc (len, M_TEMP, M_NOWAIT); 3501 if (!buf) 3502 return; 3503 3504 if (debug) 3505 log(LOG_DEBUG, SPP_FMT "ipv6cp rej opts:", 3506 SPP_ARGS(ifp)); 3507 3508 p = (void*) (h+1); 3509 for (; len > 1 && p[1]; len -= p[1], p += p[1]) { 3510 if (debug) 3511 log(-1, " %s", sppp_ipv6cp_opt_name(*p)); 3512 switch (*p) { 3513 case IPV6CP_OPT_IFID: 3514 /* 3515 * Peer doesn't grok address option. This is 3516 * bad. XXX Should we better give up here? 3517 */ 3518 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_IFID); 3519 break; 3520#ifdef notyet 3521 case IPV6CP_OPT_COMPRESS: 3522 sp->ipv6cp.opts &= ~(1 << IPV6CP_OPT_COMPRESS); 3523 break; 3524#endif 3525 } 3526 } 3527 if (debug) 3528 log(-1, "\n"); 3529 free (buf, M_TEMP); 3530 return; 3531} 3532 3533/* 3534 * Analyze the IPv6CP Configure-NAK option list, and adjust our 3535 * negotiation. 3536 */ 3537static void 3538sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len) 3539{ 3540 u_char *buf, *p; 3541 struct ifnet *ifp = &sp->pp_if; 3542 int debug = ifp->if_flags & IFF_DEBUG; 3543 struct in6_addr suggestaddr; 3544 3545 len -= 4; 3546 buf = malloc (len, M_TEMP, M_NOWAIT); 3547 if (!buf) 3548 return; 3549 3550 if (debug) 3551 log(LOG_DEBUG, SPP_FMT "ipv6cp nak opts:", 3552 SPP_ARGS(ifp)); 3553 3554 p = (void*) (h+1); 3555 for (; len > 1 && p[1]; len -= p[1], p += p[1]) { 3556 if (debug) 3557 log(-1, " %s", sppp_ipv6cp_opt_name(*p)); 3558 switch (*p) { 3559 case IPV6CP_OPT_IFID: 3560 /* 3561 * Peer doesn't like our local ifid. See 3562 * if we can do something for him. We'll drop 3563 * him our address then. 3564 */ 3565 if (len < 10 || p[1] != 10) 3566 break; 3567 bzero(&suggestaddr, sizeof(suggestaddr)); 3568 suggestaddr.s6_addr16[0] = htons(0xfe80); 3569 suggestaddr.s6_addr16[1] = htons(sp->pp_if.if_index); 3570 bcopy(&p[2], &suggestaddr.s6_addr[8], 8); 3571 3572 sp->ipv6cp.opts |= (1 << IPV6CP_OPT_IFID); 3573 if (debug) 3574 log(-1, " [suggestaddr %s]", 3575 ip6_sprintf(&suggestaddr)); 3576#ifdef IPV6CP_MYIFID_DYN 3577 /* 3578 * When doing dynamic address assignment, 3579 * we accept his offer. 3580 */ 3581 if (sp->ipv6cp.flags & IPV6CP_MYIFID_DYN) { 3582 struct in6_addr lastsuggest; 3583 /* 3584 * If <suggested myaddr from peer> equals to 3585 * <hisaddr we have suggested last time>, 3586 * we have a collision. generate new random 3587 * ifid. 3588 */ 3589 sppp_suggest_ip6_addr(&lastsuggest); 3590 if (IN6_ARE_ADDR_EQUAL(&suggestaddr, 3591 lastsuggest)) { 3592 if (debug) 3593 log(-1, " [random]"); 3594 sppp_gen_ip6_addr(sp, &suggestaddr); 3595 } 3596 sppp_set_ip6_addr(sp, &suggestaddr, 0); 3597 if (debug) 3598 log(-1, " [agree]"); 3599 sp->ipv6cp.flags |= IPV6CP_MYIFID_SEEN; 3600 } 3601#else 3602 /* 3603 * Since we do not do dynamic address assignment, 3604 * we ignore it and thus continue to negotiate 3605 * our already existing value. This can possibly 3606 * go into infinite request-reject loop. 3607 * 3608 * This is not likely because we normally use 3609 * ifid based on MAC-address. 3610 * If you have no ethernet card on the node, too bad. 3611 * XXX should we use fail_counter? 3612 */ 3613#endif 3614 break; 3615#ifdef notyet 3616 case IPV6CP_OPT_COMPRESS: 3617 /* 3618 * Peer wants different compression parameters. 3619 */ 3620 break; 3621#endif 3622 } 3623 } 3624 if (debug) 3625 log(-1, "\n"); 3626 free (buf, M_TEMP); 3627 return; 3628} 3629static void 3630sppp_ipv6cp_tlu(struct sppp *sp) 3631{ 3632 /* we are up - notify isdn daemon */ 3633 if (sp->pp_con) 3634 sp->pp_con(sp); 3635} 3636 3637static void 3638sppp_ipv6cp_tld(struct sppp *sp) 3639{ 3640} 3641 3642static void 3643sppp_ipv6cp_tls(struct sppp *sp) 3644{ 3645 /* indicate to LCP that it must stay alive */ 3646 sp->lcp.protos |= (1 << IDX_IPV6CP); 3647} 3648 3649static void 3650sppp_ipv6cp_tlf(struct sppp *sp) 3651{ 3652 3653#if 0 /* need #if 0 to close IPv6CP properly */ 3654 /* we no longer need LCP */ 3655 sp->lcp.protos &= ~(1 << IDX_IPV6CP); 3656 sppp_lcp_check_and_close(sp); 3657#endif 3658} 3659 3660static void 3661sppp_ipv6cp_scr(struct sppp *sp) 3662{ 3663 char opt[10 /* ifid */ + 4 /* compression, minimum */]; 3664 struct in6_addr ouraddr; 3665 int i = 0; 3666 3667 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_IFID)) { 3668 sppp_get_ip6_addrs(sp, &ouraddr, 0, 0); 3669 opt[i++] = IPV6CP_OPT_IFID; 3670 opt[i++] = 10; 3671 bcopy(&ouraddr.s6_addr[8], &opt[i], 8); 3672 i += 8; 3673 } 3674 3675#ifdef notyet 3676 if (sp->ipv6cp.opts & (1 << IPV6CP_OPT_COMPRESSION)) { 3677 opt[i++] = IPV6CP_OPT_COMPRESSION; 3678 opt[i++] = 4; 3679 opt[i++] = 0; /* TBD */ 3680 opt[i++] = 0; /* TBD */ 3681 /* variable length data may follow */ 3682 } 3683#endif 3684 3685 sp->confid[IDX_IPV6CP] = ++sp->pp_seq[IDX_IPV6CP]; 3686 sppp_cp_send(sp, PPP_IPV6CP, CONF_REQ, sp->confid[IDX_IPV6CP], i, &opt); 3687} 3688#else /*INET6*/ 3689static void sppp_ipv6cp_init(struct sppp *sp) 3690{ 3691} 3692 3693static void sppp_ipv6cp_up(struct sppp *sp) 3694{ 3695} 3696 3697static void sppp_ipv6cp_down(struct sppp *sp) 3698{ 3699} 3700 3701 3702static void sppp_ipv6cp_open(struct sppp *sp) 3703{ 3704} 3705 3706static void sppp_ipv6cp_close(struct sppp *sp) 3707{ 3708} 3709 3710static void sppp_ipv6cp_TO(void *sp) 3711{ 3712} 3713 3714static int sppp_ipv6cp_RCR(struct sppp *sp, struct lcp_header *h, int len) 3715{ 3716 return 0; 3717} 3718 3719static void sppp_ipv6cp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len) 3720{ 3721} 3722 3723static void sppp_ipv6cp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len) 3724{ 3725} 3726 3727static void sppp_ipv6cp_tlu(struct sppp *sp) 3728{ 3729} 3730 3731static void sppp_ipv6cp_tld(struct sppp *sp) 3732{ 3733} 3734 3735static void sppp_ipv6cp_tls(struct sppp *sp) 3736{ 3737} 3738 3739static void sppp_ipv6cp_tlf(struct sppp *sp) 3740{ 3741} 3742 3743static void sppp_ipv6cp_scr(struct sppp *sp) 3744{ 3745} 3746#endif /*INET6*/ 3747 3748/* 3749 *--------------------------------------------------------------------------* 3750 * * 3751 * The CHAP implementation. * 3752 * * 3753 *--------------------------------------------------------------------------* 3754 */ 3755 3756/* 3757 * The authentication protocols don't employ a full-fledged state machine as 3758 * the control protocols do, since they do have Open and Close events, but 3759 * not Up and Down, nor are they explicitly terminated. Also, use of the 3760 * authentication protocols may be different in both directions (this makes 3761 * sense, think of a machine that never accepts incoming calls but only 3762 * calls out, it doesn't require the called party to authenticate itself). 3763 * 3764 * Our state machine for the local authentication protocol (we are requesting 3765 * the peer to authenticate) looks like: 3766 * 3767 * RCA- 3768 * +--------------------------------------------+ 3769 * V scn,tld| 3770 * +--------+ Close +---------+ RCA+ 3771 * | |<----------------------------------| |------+ 3772 * +--->| Closed | TO* | Opened | sca | 3773 * | | |-----+ +-------| |<-----+ 3774 * | +--------+ irc | | +---------+ 3775 * | ^ | | ^ 3776 * | | | | | 3777 * | | | | | 3778 * | TO-| | | | 3779 * | |tld TO+ V | | 3780 * | | +------->+ | | 3781 * | | | | | | 3782 * | +--------+ V | | 3783 * | | |<----+<--------------------+ | 3784 * | | Req- | scr | 3785 * | | Sent | | 3786 * | | | | 3787 * | +--------+ | 3788 * | RCA- | | RCA+ | 3789 * +------+ +------------------------------------------+ 3790 * scn,tld sca,irc,ict,tlu 3791 * 3792 * 3793 * with: 3794 * 3795 * Open: LCP reached authentication phase 3796 * Close: LCP reached terminate phase 3797 * 3798 * RCA+: received reply (pap-req, chap-response), acceptable 3799 * RCN: received reply (pap-req, chap-response), not acceptable 3800 * TO+: timeout with restart counter >= 0 3801 * TO-: timeout with restart counter < 0 3802 * TO*: reschedule timeout for CHAP 3803 * 3804 * scr: send request packet (none for PAP, chap-challenge) 3805 * sca: send ack packet (pap-ack, chap-success) 3806 * scn: send nak packet (pap-nak, chap-failure) 3807 * ict: initialize re-challenge timer (CHAP only) 3808 * 3809 * tlu: this-layer-up, LCP reaches network phase 3810 * tld: this-layer-down, LCP enters terminate phase 3811 * 3812 * Note that in CHAP mode, after sending a new challenge, while the state 3813 * automaton falls back into Req-Sent state, it doesn't signal a tld 3814 * event to LCP, so LCP remains in network phase. Only after not getting 3815 * any response (or after getting an unacceptable response), CHAP closes, 3816 * causing LCP to enter terminate phase. 3817 * 3818 * With PAP, there is no initial request that can be sent. The peer is 3819 * expected to send one based on the successful negotiation of PAP as 3820 * the authentication protocol during the LCP option negotiation. 3821 * 3822 * Incoming authentication protocol requests (remote requests 3823 * authentication, we are peer) don't employ a state machine at all, 3824 * they are simply answered. Some peers [Ascend P50 firmware rev 3825 * 4.50] react allergically when sending IPCP requests while they are 3826 * still in authentication phase (thereby violating the standard that 3827 * demands that these NCP packets are to be discarded), so we keep 3828 * track of the peer demanding us to authenticate, and only proceed to 3829 * phase network once we've seen a positive acknowledge for the 3830 * authentication. 3831 */ 3832 3833/* 3834 * Handle incoming CHAP packets. 3835 */ 3836static void 3837sppp_chap_input(struct sppp *sp, struct mbuf *m) 3838{ 3839 STDDCL; 3840 struct lcp_header *h; 3841 int len, x; 3842 u_char *value, *name, digest[AUTHKEYLEN], dsize; 3843 int value_len, name_len; 3844 MD5_CTX ctx; 3845 3846 len = m->m_pkthdr.len; 3847 if (len < 4) { 3848 if (debug) 3849 log(LOG_DEBUG, 3850 SPP_FMT "chap invalid packet length: %d bytes\n", 3851 SPP_ARGS(ifp), len); 3852 return; 3853 } 3854 h = mtod (m, struct lcp_header*); 3855 if (len > ntohs (h->len)) 3856 len = ntohs (h->len); 3857 3858 switch (h->type) { 3859 /* challenge, failure and success are his authproto */ 3860 case CHAP_CHALLENGE: 3861 value = 1 + (u_char*)(h+1); 3862 value_len = value[-1]; 3863 name = value + value_len; 3864 name_len = len - value_len - 5; 3865 if (name_len < 0) { 3866 if (debug) { 3867 log(LOG_DEBUG, 3868 SPP_FMT "chap corrupted challenge " 3869 "<%s id=0x%x len=%d", 3870 SPP_ARGS(ifp), 3871 sppp_auth_type_name(PPP_CHAP, h->type), 3872 h->ident, ntohs(h->len)); 3873 sppp_print_bytes((u_char*) (h+1), len-4); 3874 log(-1, ">\n"); 3875 } 3876 break; 3877 } 3878 3879 if (debug) { 3880 log(LOG_DEBUG, 3881 SPP_FMT "chap input <%s id=0x%x len=%d name=", 3882 SPP_ARGS(ifp), 3883 sppp_auth_type_name(PPP_CHAP, h->type), h->ident, 3884 ntohs(h->len)); 3885 sppp_print_string((char*) name, name_len); 3886 log(-1, " value-size=%d value=", value_len); 3887 sppp_print_bytes(value, value_len); 3888 log(-1, ">\n"); 3889 } 3890 3891 /* Compute reply value. */ 3892 MD5Init(&ctx); 3893 MD5Update(&ctx, &h->ident, 1); 3894 MD5Update(&ctx, sp->myauth.secret, 3895 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN)); 3896 MD5Update(&ctx, value, value_len); 3897 MD5Final(digest, &ctx); 3898 dsize = sizeof digest; 3899 3900 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident, 3901 sizeof dsize, (const char *)&dsize, 3902 sizeof digest, digest, 3903 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN), 3904 sp->myauth.name, 3905 0); 3906 break; 3907 3908 case CHAP_SUCCESS: 3909 if (debug) { 3910 log(LOG_DEBUG, SPP_FMT "chap success", 3911 SPP_ARGS(ifp)); 3912 if (len > 4) { 3913 log(-1, ": "); 3914 sppp_print_string((char*)(h + 1), len - 4); 3915 } 3916 log(-1, "\n"); 3917 } 3918 x = splimp(); 3919 sp->pp_flags &= ~PP_NEEDAUTH; 3920 if (sp->myauth.proto == PPP_CHAP && 3921 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) && 3922 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) { 3923 /* 3924 * We are authenticator for CHAP but didn't 3925 * complete yet. Leave it to tlu to proceed 3926 * to network phase. 3927 */ 3928 splx(x); 3929 break; 3930 } 3931 splx(x); 3932 sppp_phase_network(sp); 3933 break; 3934 3935 case CHAP_FAILURE: 3936 if (debug) { 3937 log(LOG_INFO, SPP_FMT "chap failure", 3938 SPP_ARGS(ifp)); 3939 if (len > 4) { 3940 log(-1, ": "); 3941 sppp_print_string((char*)(h + 1), len - 4); 3942 } 3943 log(-1, "\n"); 3944 } else 3945 log(LOG_INFO, SPP_FMT "chap failure\n", 3946 SPP_ARGS(ifp)); 3947 /* await LCP shutdown by authenticator */ 3948 break; 3949 3950 /* response is my authproto */ 3951 case CHAP_RESPONSE: 3952 value = 1 + (u_char*)(h+1); 3953 value_len = value[-1]; 3954 name = value + value_len; 3955 name_len = len - value_len - 5; 3956 if (name_len < 0) { 3957 if (debug) { 3958 log(LOG_DEBUG, 3959 SPP_FMT "chap corrupted response " 3960 "<%s id=0x%x len=%d", 3961 SPP_ARGS(ifp), 3962 sppp_auth_type_name(PPP_CHAP, h->type), 3963 h->ident, ntohs(h->len)); 3964 sppp_print_bytes((u_char*)(h+1), len-4); 3965 log(-1, ">\n"); 3966 } 3967 break; 3968 } 3969 if (h->ident != sp->confid[IDX_CHAP]) { 3970 if (debug) 3971 log(LOG_DEBUG, 3972 SPP_FMT "chap dropping response for old ID " 3973 "(got %d, expected %d)\n", 3974 SPP_ARGS(ifp), 3975 h->ident, sp->confid[IDX_CHAP]); 3976 break; 3977 } 3978 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) 3979 || bcmp(name, sp->hisauth.name, name_len) != 0) { 3980 log(LOG_INFO, SPP_FMT "chap response, his name ", 3981 SPP_ARGS(ifp)); 3982 sppp_print_string(name, name_len); 3983 log(-1, " != expected "); 3984 sppp_print_string(sp->hisauth.name, 3985 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)); 3986 log(-1, "\n"); 3987 } 3988 if (debug) { 3989 log(LOG_DEBUG, SPP_FMT "chap input(%s) " 3990 "<%s id=0x%x len=%d name=", 3991 SPP_ARGS(ifp), 3992 sppp_state_name(sp->state[IDX_CHAP]), 3993 sppp_auth_type_name(PPP_CHAP, h->type), 3994 h->ident, ntohs (h->len)); 3995 sppp_print_string((char*)name, name_len); 3996 log(-1, " value-size=%d value=", value_len); 3997 sppp_print_bytes(value, value_len); 3998 log(-1, ">\n"); 3999 } 4000 if (value_len != AUTHKEYLEN) { 4001 if (debug) 4002 log(LOG_DEBUG, 4003 SPP_FMT "chap bad hash value length: " 4004 "%d bytes, should be %d\n", 4005 SPP_ARGS(ifp), value_len, 4006 AUTHKEYLEN); 4007 break; 4008 } 4009 4010 MD5Init(&ctx); 4011 MD5Update(&ctx, &h->ident, 1); 4012 MD5Update(&ctx, sp->hisauth.secret, 4013 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN)); 4014 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN); 4015 MD5Final(digest, &ctx); 4016 4017#define FAILMSG "Failed..." 4018#define SUCCMSG "Welcome!" 4019 4020 if (value_len != sizeof digest || 4021 bcmp(digest, value, value_len) != 0) { 4022 /* action scn, tld */ 4023 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident, 4024 sizeof(FAILMSG) - 1, (u_char *)FAILMSG, 4025 0); 4026 chap.tld(sp); 4027 break; 4028 } 4029 /* action sca, perhaps tlu */ 4030 if (sp->state[IDX_CHAP] == STATE_REQ_SENT || 4031 sp->state[IDX_CHAP] == STATE_OPENED) 4032 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident, 4033 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG, 4034 0); 4035 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) { 4036 sppp_cp_change_state(&chap, sp, STATE_OPENED); 4037 chap.tlu(sp); 4038 } 4039 break; 4040 4041 default: 4042 /* Unknown CHAP packet type -- ignore. */ 4043 if (debug) { 4044 log(LOG_DEBUG, SPP_FMT "chap unknown input(%s) " 4045 "<0x%x id=0x%xh len=%d", 4046 SPP_ARGS(ifp), 4047 sppp_state_name(sp->state[IDX_CHAP]), 4048 h->type, h->ident, ntohs(h->len)); 4049 sppp_print_bytes((u_char*)(h+1), len-4); 4050 log(-1, ">\n"); 4051 } 4052 break; 4053 4054 } 4055} 4056 4057static void 4058sppp_chap_init(struct sppp *sp) 4059{ 4060 /* Chap doesn't have STATE_INITIAL at all. */ 4061 sp->state[IDX_CHAP] = STATE_CLOSED; 4062 sp->fail_counter[IDX_CHAP] = 0; 4063 sp->pp_seq[IDX_CHAP] = 0; 4064 sp->pp_rseq[IDX_CHAP] = 0; 4065#if defined(__FreeBSD__) && __FreeBSD__ >= 3 4066 callout_handle_init(&sp->ch[IDX_CHAP]); 4067#endif 4068} 4069 4070static void 4071sppp_chap_open(struct sppp *sp) 4072{ 4073 if (sp->myauth.proto == PPP_CHAP && 4074 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) { 4075 /* we are authenticator for CHAP, start it */ 4076 chap.scr(sp); 4077 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure; 4078 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT); 4079 } 4080 /* nothing to be done if we are peer, await a challenge */ 4081} 4082 4083static void 4084sppp_chap_close(struct sppp *sp) 4085{ 4086 if (sp->state[IDX_CHAP] != STATE_CLOSED) 4087 sppp_cp_change_state(&chap, sp, STATE_CLOSED); 4088} 4089 4090static void 4091sppp_chap_TO(void *cookie) 4092{ 4093 struct sppp *sp = (struct sppp *)cookie; 4094 STDDCL; 4095 int s; 4096 4097 s = splimp(); 4098 if (debug) 4099 log(LOG_DEBUG, SPP_FMT "chap TO(%s) rst_counter = %d\n", 4100 SPP_ARGS(ifp), 4101 sppp_state_name(sp->state[IDX_CHAP]), 4102 sp->rst_counter[IDX_CHAP]); 4103 4104 if (--sp->rst_counter[IDX_CHAP] < 0) 4105 /* TO- event */ 4106 switch (sp->state[IDX_CHAP]) { 4107 case STATE_REQ_SENT: 4108 chap.tld(sp); 4109 sppp_cp_change_state(&chap, sp, STATE_CLOSED); 4110 break; 4111 } 4112 else 4113 /* TO+ (or TO*) event */ 4114 switch (sp->state[IDX_CHAP]) { 4115 case STATE_OPENED: 4116 /* TO* event */ 4117 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure; 4118 /* FALLTHROUGH */ 4119 case STATE_REQ_SENT: 4120 chap.scr(sp); 4121 /* sppp_cp_change_state() will restart the timer */ 4122 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT); 4123 break; 4124 } 4125 4126 splx(s); 4127} 4128 4129static void 4130sppp_chap_tlu(struct sppp *sp) 4131{ 4132 STDDCL; 4133 int i, x; 4134 4135 i = 0; 4136 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure; 4137 4138 /* 4139 * Some broken CHAP implementations (Conware CoNet, firmware 4140 * 4.0.?) don't want to re-authenticate their CHAP once the 4141 * initial challenge-response exchange has taken place. 4142 * Provide for an option to avoid rechallenges. 4143 */ 4144 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) { 4145 /* 4146 * Compute the re-challenge timeout. This will yield 4147 * a number between 300 and 810 seconds. 4148 */ 4149 i = 300 + ((unsigned)(random() & 0xff00) >> 7); 4150 TIMEOUT(chap.TO, (void *)sp, i * hz, sp->ch[IDX_CHAP]); 4151 } 4152 4153 if (debug) { 4154 log(LOG_DEBUG, 4155 SPP_FMT "chap %s, ", 4156 SPP_ARGS(ifp), 4157 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu"); 4158 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) 4159 log(-1, "next re-challenge in %d seconds\n", i); 4160 else 4161 log(-1, "re-challenging supressed\n"); 4162 } 4163 4164 x = splimp(); 4165 /* indicate to LCP that we need to be closed down */ 4166 sp->lcp.protos |= (1 << IDX_CHAP); 4167 4168 if (sp->pp_flags & PP_NEEDAUTH) { 4169 /* 4170 * Remote is authenticator, but his auth proto didn't 4171 * complete yet. Defer the transition to network 4172 * phase. 4173 */ 4174 splx(x); 4175 return; 4176 } 4177 splx(x); 4178 4179 /* 4180 * If we are already in phase network, we are done here. This 4181 * is the case if this is a dummy tlu event after a re-challenge. 4182 */ 4183 if (sp->pp_phase != PHASE_NETWORK) 4184 sppp_phase_network(sp); 4185} 4186 4187static void 4188sppp_chap_tld(struct sppp *sp) 4189{ 4190 STDDCL; 4191 4192 if (debug) 4193 log(LOG_DEBUG, SPP_FMT "chap tld\n", SPP_ARGS(ifp)); 4194 UNTIMEOUT(chap.TO, (void *)sp, sp->ch[IDX_CHAP]); 4195 sp->lcp.protos &= ~(1 << IDX_CHAP); 4196 4197 lcp.Close(sp); 4198} 4199 4200static void 4201sppp_chap_scr(struct sppp *sp) 4202{ 4203 u_long *ch, seed; 4204 u_char clen; 4205 4206 /* Compute random challenge. */ 4207 ch = (u_long *)sp->myauth.challenge; 4208#if defined(__FreeBSD__) && __FreeBSD__ >= 3 4209 read_random(&seed, sizeof seed); 4210#else 4211 { 4212 struct timeval tv; 4213 microtime(&tv); 4214 seed = tv.tv_sec ^ tv.tv_usec; 4215 } 4216#endif 4217 ch[0] = seed ^ random(); 4218 ch[1] = seed ^ random(); 4219 ch[2] = seed ^ random(); 4220 ch[3] = seed ^ random(); 4221 clen = AUTHKEYLEN; 4222 4223 sp->confid[IDX_CHAP] = ++sp->pp_seq[IDX_CHAP]; 4224 4225 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP], 4226 sizeof clen, (const char *)&clen, 4227 (size_t)AUTHKEYLEN, sp->myauth.challenge, 4228 (size_t)sppp_strnlen(sp->myauth.name, AUTHNAMELEN), 4229 sp->myauth.name, 4230 0); 4231} 4232 4233/* 4234 *--------------------------------------------------------------------------* 4235 * * 4236 * The PAP implementation. * 4237 * * 4238 *--------------------------------------------------------------------------* 4239 */ 4240/* 4241 * For PAP, we need to keep a little state also if we are the peer, not the 4242 * authenticator. This is since we don't get a request to authenticate, but 4243 * have to repeatedly authenticate ourself until we got a response (or the 4244 * retry counter is expired). 4245 */ 4246 4247/* 4248 * Handle incoming PAP packets. */ 4249static void 4250sppp_pap_input(struct sppp *sp, struct mbuf *m) 4251{ 4252 STDDCL; 4253 struct lcp_header *h; 4254 int len, x; 4255 u_char *name, *passwd, mlen; 4256 int name_len, passwd_len; 4257 4258 len = m->m_pkthdr.len; 4259 if (len < 5) { 4260 if (debug) 4261 log(LOG_DEBUG, 4262 SPP_FMT "pap invalid packet length: %d bytes\n", 4263 SPP_ARGS(ifp), len); 4264 return; 4265 } 4266 h = mtod (m, struct lcp_header*); 4267 if (len > ntohs (h->len)) 4268 len = ntohs (h->len); 4269 switch (h->type) { 4270 /* PAP request is my authproto */ 4271 case PAP_REQ: 4272 name = 1 + (u_char*)(h+1); 4273 name_len = name[-1]; 4274 passwd = name + name_len + 1; 4275 if (name_len > len - 6 || 4276 (passwd_len = passwd[-1]) > len - 6 - name_len) { 4277 if (debug) { 4278 log(LOG_DEBUG, SPP_FMT "pap corrupted input " 4279 "<%s id=0x%x len=%d", 4280 SPP_ARGS(ifp), 4281 sppp_auth_type_name(PPP_PAP, h->type), 4282 h->ident, ntohs(h->len)); 4283 sppp_print_bytes((u_char*)(h+1), len-4); 4284 log(-1, ">\n"); 4285 } 4286 break; 4287 } 4288 if (debug) { 4289 log(LOG_DEBUG, SPP_FMT "pap input(%s) " 4290 "<%s id=0x%x len=%d name=", 4291 SPP_ARGS(ifp), 4292 sppp_state_name(sp->state[IDX_PAP]), 4293 sppp_auth_type_name(PPP_PAP, h->type), 4294 h->ident, ntohs(h->len)); 4295 sppp_print_string((char*)name, name_len); 4296 log(-1, " passwd="); 4297 sppp_print_string((char*)passwd, passwd_len); 4298 log(-1, ">\n"); 4299 } 4300 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) || 4301 passwd_len != sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN) || 4302 bcmp(name, sp->hisauth.name, name_len) != 0 || 4303 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) { 4304 /* action scn, tld */ 4305 mlen = sizeof(FAILMSG) - 1; 4306 sppp_auth_send(&pap, sp, PAP_NAK, h->ident, 4307 sizeof mlen, (const char *)&mlen, 4308 sizeof(FAILMSG) - 1, (u_char *)FAILMSG, 4309 0); 4310 pap.tld(sp); 4311 break; 4312 } 4313 /* action sca, perhaps tlu */ 4314 if (sp->state[IDX_PAP] == STATE_REQ_SENT || 4315 sp->state[IDX_PAP] == STATE_OPENED) { 4316 mlen = sizeof(SUCCMSG) - 1; 4317 sppp_auth_send(&pap, sp, PAP_ACK, h->ident, 4318 sizeof mlen, (const char *)&mlen, 4319 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG, 4320 0); 4321 } 4322 if (sp->state[IDX_PAP] == STATE_REQ_SENT) { 4323 sppp_cp_change_state(&pap, sp, STATE_OPENED); 4324 pap.tlu(sp); 4325 } 4326 break; 4327 4328 /* ack and nak are his authproto */ 4329 case PAP_ACK: 4330 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch); 4331 if (debug) { 4332 log(LOG_DEBUG, SPP_FMT "pap success", 4333 SPP_ARGS(ifp)); 4334 name_len = *((char *)h); 4335 if (len > 5 && name_len) { 4336 log(-1, ": "); 4337 sppp_print_string((char*)(h+1), name_len); 4338 } 4339 log(-1, "\n"); 4340 } 4341 x = splimp(); 4342 sp->pp_flags &= ~PP_NEEDAUTH; 4343 if (sp->myauth.proto == PPP_PAP && 4344 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) && 4345 (sp->lcp.protos & (1 << IDX_PAP)) == 0) { 4346 /* 4347 * We are authenticator for PAP but didn't 4348 * complete yet. Leave it to tlu to proceed 4349 * to network phase. 4350 */ 4351 splx(x); 4352 break; 4353 } 4354 splx(x); 4355 sppp_phase_network(sp); 4356 break; 4357 4358 case PAP_NAK: 4359 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch); 4360 if (debug) { 4361 log(LOG_INFO, SPP_FMT "pap failure", 4362 SPP_ARGS(ifp)); 4363 name_len = *((char *)h); 4364 if (len > 5 && name_len) { 4365 log(-1, ": "); 4366 sppp_print_string((char*)(h+1), name_len); 4367 } 4368 log(-1, "\n"); 4369 } else 4370 log(LOG_INFO, SPP_FMT "pap failure\n", 4371 SPP_ARGS(ifp)); 4372 /* await LCP shutdown by authenticator */ 4373 break; 4374 4375 default: 4376 /* Unknown PAP packet type -- ignore. */ 4377 if (debug) { 4378 log(LOG_DEBUG, SPP_FMT "pap corrupted input " 4379 "<0x%x id=0x%x len=%d", 4380 SPP_ARGS(ifp), 4381 h->type, h->ident, ntohs(h->len)); 4382 sppp_print_bytes((u_char*)(h+1), len-4); 4383 log(-1, ">\n"); 4384 } 4385 break; 4386 4387 } 4388} 4389 4390static void 4391sppp_pap_init(struct sppp *sp) 4392{ 4393 /* PAP doesn't have STATE_INITIAL at all. */ 4394 sp->state[IDX_PAP] = STATE_CLOSED; 4395 sp->fail_counter[IDX_PAP] = 0; 4396 sp->pp_seq[IDX_PAP] = 0; 4397 sp->pp_rseq[IDX_PAP] = 0; 4398#if defined(__FreeBSD__) && __FreeBSD__ >= 3 4399 callout_handle_init(&sp->ch[IDX_PAP]); 4400 callout_handle_init(&sp->pap_my_to_ch); 4401#endif 4402} 4403 4404static void 4405sppp_pap_open(struct sppp *sp) 4406{ 4407 if (sp->hisauth.proto == PPP_PAP && 4408 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) { 4409 /* we are authenticator for PAP, start our timer */ 4410 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure; 4411 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT); 4412 } 4413 if (sp->myauth.proto == PPP_PAP) { 4414 /* we are peer, send a request, and start a timer */ 4415 pap.scr(sp); 4416 TIMEOUT(sppp_pap_my_TO, (void *)sp, sp->lcp.timeout, 4417 sp->pap_my_to_ch); 4418 } 4419} 4420 4421static void 4422sppp_pap_close(struct sppp *sp) 4423{ 4424 if (sp->state[IDX_PAP] != STATE_CLOSED) 4425 sppp_cp_change_state(&pap, sp, STATE_CLOSED); 4426} 4427 4428/* 4429 * That's the timeout routine if we are authenticator. Since the 4430 * authenticator is basically passive in PAP, we can't do much here. 4431 */ 4432static void 4433sppp_pap_TO(void *cookie) 4434{ 4435 struct sppp *sp = (struct sppp *)cookie; 4436 STDDCL; 4437 int s; 4438 4439 s = splimp(); 4440 if (debug) 4441 log(LOG_DEBUG, SPP_FMT "pap TO(%s) rst_counter = %d\n", 4442 SPP_ARGS(ifp), 4443 sppp_state_name(sp->state[IDX_PAP]), 4444 sp->rst_counter[IDX_PAP]); 4445 4446 if (--sp->rst_counter[IDX_PAP] < 0) 4447 /* TO- event */ 4448 switch (sp->state[IDX_PAP]) { 4449 case STATE_REQ_SENT: 4450 pap.tld(sp); 4451 sppp_cp_change_state(&pap, sp, STATE_CLOSED); 4452 break; 4453 } 4454 else 4455 /* TO+ event, not very much we could do */ 4456 switch (sp->state[IDX_PAP]) { 4457 case STATE_REQ_SENT: 4458 /* sppp_cp_change_state() will restart the timer */ 4459 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT); 4460 break; 4461 } 4462 4463 splx(s); 4464} 4465 4466/* 4467 * That's the timeout handler if we are peer. Since the peer is active, 4468 * we need to retransmit our PAP request since it is apparently lost. 4469 * XXX We should impose a max counter. 4470 */ 4471static void 4472sppp_pap_my_TO(void *cookie) 4473{ 4474 struct sppp *sp = (struct sppp *)cookie; 4475 STDDCL; 4476 4477 if (debug) 4478 log(LOG_DEBUG, SPP_FMT "pap peer TO\n", 4479 SPP_ARGS(ifp)); 4480 4481 pap.scr(sp); 4482} 4483 4484static void 4485sppp_pap_tlu(struct sppp *sp) 4486{ 4487 STDDCL; 4488 int x; 4489 4490 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure; 4491 4492 if (debug) 4493 log(LOG_DEBUG, SPP_FMT "%s tlu\n", 4494 SPP_ARGS(ifp), pap.name); 4495 4496 x = splimp(); 4497 /* indicate to LCP that we need to be closed down */ 4498 sp->lcp.protos |= (1 << IDX_PAP); 4499 4500 if (sp->pp_flags & PP_NEEDAUTH) { 4501 /* 4502 * Remote is authenticator, but his auth proto didn't 4503 * complete yet. Defer the transition to network 4504 * phase. 4505 */ 4506 splx(x); 4507 return; 4508 } 4509 splx(x); 4510 sppp_phase_network(sp); 4511} 4512 4513static void 4514sppp_pap_tld(struct sppp *sp) 4515{ 4516 STDDCL; 4517 4518 if (debug) 4519 log(LOG_DEBUG, SPP_FMT "pap tld\n", SPP_ARGS(ifp)); 4520 UNTIMEOUT(pap.TO, (void *)sp, sp->ch[IDX_PAP]); 4521 UNTIMEOUT(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch); 4522 sp->lcp.protos &= ~(1 << IDX_PAP); 4523 4524 lcp.Close(sp); 4525} 4526 4527static void 4528sppp_pap_scr(struct sppp *sp) 4529{ 4530 u_char idlen, pwdlen; 4531 4532 sp->confid[IDX_PAP] = ++sp->pp_seq[IDX_PAP]; 4533 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN); 4534 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN); 4535 4536 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP], 4537 sizeof idlen, (const char *)&idlen, 4538 (size_t)idlen, sp->myauth.name, 4539 sizeof pwdlen, (const char *)&pwdlen, 4540 (size_t)pwdlen, sp->myauth.secret, 4541 0); 4542} 4543 4544/* 4545 * Random miscellaneous functions. 4546 */ 4547 4548/* 4549 * Send a PAP or CHAP proto packet. 4550 * 4551 * Varadic function, each of the elements for the ellipsis is of type 4552 * ``size_t mlen, const u_char *msg''. Processing will stop iff 4553 * mlen == 0. 4554 * NOTE: never declare variadic functions with types subject to type 4555 * promotion (i.e. u_char). This is asking for big trouble depending 4556 * on the architecture you are on... 4557 */ 4558 4559static void 4560sppp_auth_send(const struct cp *cp, struct sppp *sp, 4561 unsigned int type, unsigned int id, 4562 ...) 4563{ 4564 STDDCL; 4565 struct ppp_header *h; 4566 struct lcp_header *lh; 4567 struct mbuf *m; 4568 u_char *p; 4569 int len; 4570 unsigned int mlen; 4571 const char *msg; 4572 va_list ap; 4573 4574 MGETHDR (m, M_DONTWAIT, MT_DATA); 4575 if (! m) 4576 return; 4577 m->m_pkthdr.rcvif = 0; 4578 4579 h = mtod (m, struct ppp_header*); 4580 h->address = PPP_ALLSTATIONS; /* broadcast address */ 4581 h->control = PPP_UI; /* Unnumbered Info */ 4582 h->protocol = htons(cp->proto); 4583 4584 lh = (struct lcp_header*)(h + 1); 4585 lh->type = type; 4586 lh->ident = id; 4587 p = (u_char*) (lh+1); 4588 4589 va_start(ap, id); 4590 len = 0; 4591 4592 while ((mlen = (unsigned int)va_arg(ap, size_t)) != 0) { 4593 msg = va_arg(ap, const char *); 4594 len += mlen; 4595 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) { 4596 va_end(ap); 4597 m_freem(m); 4598 return; 4599 } 4600 4601 bcopy(msg, p, mlen); 4602 p += mlen; 4603 } 4604 va_end(ap); 4605 4606 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len; 4607 lh->len = htons (LCP_HEADER_LEN + len); 4608 4609 if (debug) { 4610 log(LOG_DEBUG, SPP_FMT "%s output <%s id=0x%x len=%d", 4611 SPP_ARGS(ifp), cp->name, 4612 sppp_auth_type_name(cp->proto, lh->type), 4613 lh->ident, ntohs(lh->len)); 4614 sppp_print_bytes((u_char*) (lh+1), len); 4615 log(-1, ">\n"); 4616 } 4617 if (! IF_HANDOFF_ADJ(&sp->pp_cpq, m, ifp, 3)) 4618 ifp->if_oerrors++; 4619} 4620 4621/* 4622 * Flush interface queue. 4623 */ 4624static void 4625sppp_qflush(struct ifqueue *ifq) 4626{ 4627 struct mbuf *m, *n; 4628 4629 n = ifq->ifq_head; 4630 while ((m = n)) { 4631 n = m->m_act; 4632 m_freem (m); 4633 } 4634 ifq->ifq_head = 0; 4635 ifq->ifq_tail = 0; 4636 ifq->ifq_len = 0; 4637} 4638 4639/* 4640 * Send keepalive packets, every 10 seconds. 4641 */ 4642static void 4643sppp_keepalive(void *dummy) 4644{ 4645 struct sppp *sp; 4646 int s; 4647 4648 s = splimp(); 4649 for (sp=spppq; sp; sp=sp->pp_next) { 4650 struct ifnet *ifp = &sp->pp_if; 4651 4652 /* Keepalive mode disabled or channel down? */ 4653 if (! (sp->pp_flags & PP_KEEPALIVE) || 4654 ! (ifp->if_flags & IFF_RUNNING)) 4655 continue; 4656 4657 /* No keepalive in PPP mode if LCP not opened yet. */ 4658 if (sp->pp_mode != IFF_CISCO && 4659 sp->pp_phase < PHASE_AUTHENTICATE) 4660 continue; 4661 4662 if (sp->pp_alivecnt == MAXALIVECNT) { 4663 /* No keepalive packets got. Stop the interface. */ 4664 printf (SPP_FMT "down\n", SPP_ARGS(ifp)); 4665 if_down (ifp); 4666 sppp_qflush (&sp->pp_cpq); 4667 if (sp->pp_mode != IFF_CISCO) { 4668 /* XXX */ 4669 /* Shut down the PPP link. */ 4670 lcp.Down(sp); 4671 /* Initiate negotiation. XXX */ 4672 lcp.Up(sp); 4673 } 4674 } 4675 if (sp->pp_alivecnt <= MAXALIVECNT) 4676 ++sp->pp_alivecnt; 4677 if (sp->pp_mode == IFF_CISCO) 4678 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, 4679 ++sp->pp_seq[IDX_LCP], sp->pp_rseq[IDX_LCP]); 4680 else if (sp->pp_phase >= PHASE_AUTHENTICATE) { 4681 long nmagic = htonl (sp->lcp.magic); 4682 sp->lcp.echoid = ++sp->pp_seq[IDX_LCP]; 4683 sppp_cp_send (sp, PPP_LCP, ECHO_REQ, 4684 sp->lcp.echoid, 4, &nmagic); 4685 } 4686 } 4687 splx(s); 4688 TIMEOUT(sppp_keepalive, 0, hz * 10, keepalive_ch); 4689} 4690 4691/* 4692 * Get both IP addresses. 4693 */ 4694static void 4695sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask) 4696{ 4697 struct ifnet *ifp = &sp->pp_if; 4698 struct ifaddr *ifa; 4699 struct sockaddr_in *si, *sm; 4700 u_long ssrc, ddst; 4701 4702 sm = NULL; 4703 ssrc = ddst = 0L; 4704 /* 4705 * Pick the first AF_INET address from the list, 4706 * aliases don't make any sense on a p2p link anyway. 4707 */ 4708 si = 0; 4709#if defined(__FreeBSD__) && __FreeBSD__ >= 3 4710 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 4711#elif defined(__NetBSD__) || defined (__OpenBSD__) 4712 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); 4713 ifa; 4714 ifa = TAILQ_NEXT(ifa, ifa_list)) 4715#else 4716 for (ifa = ifp->if_addrlist; 4717 ifa; 4718 ifa = ifa->ifa_next) 4719#endif 4720 if (ifa->ifa_addr->sa_family == AF_INET) { 4721 si = (struct sockaddr_in *)ifa->ifa_addr; 4722 sm = (struct sockaddr_in *)ifa->ifa_netmask; 4723 if (si) 4724 break; 4725 } 4726 if (ifa) { 4727 if (si && si->sin_addr.s_addr) { 4728 ssrc = si->sin_addr.s_addr; 4729 if (srcmask) 4730 *srcmask = ntohl(sm->sin_addr.s_addr); 4731 } 4732 4733 si = (struct sockaddr_in *)ifa->ifa_dstaddr; 4734 if (si && si->sin_addr.s_addr) 4735 ddst = si->sin_addr.s_addr; 4736 } 4737 4738 if (dst) *dst = ntohl(ddst); 4739 if (src) *src = ntohl(ssrc); 4740} 4741 4742/* 4743 * Set my IP address. Must be called at splimp. 4744 */ 4745static void 4746sppp_set_ip_addr(struct sppp *sp, u_long src) 4747{ 4748 STDDCL; 4749 struct ifaddr *ifa; 4750 struct sockaddr_in *si; 4751 struct in_ifaddr *ia; 4752 4753 /* 4754 * Pick the first AF_INET address from the list, 4755 * aliases don't make any sense on a p2p link anyway. 4756 */ 4757 si = 0; 4758#if defined(__FreeBSD__) && __FreeBSD__ >= 3 4759 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) 4760#elif defined(__NetBSD__) || defined (__OpenBSD__) 4761 for (ifa = TAILQ_FIRST(&ifp->if_addrlist); 4762 ifa; 4763 ifa = TAILQ_NEXT(ifa, ifa_list)) 4764#else 4765 for (ifa = ifp->if_addrlist; 4766 ifa; 4767 ifa = ifa->ifa_next) 4768#endif 4769 { 4770 if (ifa->ifa_addr->sa_family == AF_INET) 4771 { 4772 si = (struct sockaddr_in *)ifa->ifa_addr; 4773 if (si) 4774 break; 4775 } 4776 } 4777 4778 if (ifa && si) 4779 { 4780 int error; 4781#if __NetBSD_Version__ >= 103080000 4782 struct sockaddr_in new_sin = *si; 4783 4784 new_sin.sin_addr.s_addr = htonl(src); 4785 error = in_ifinit(ifp, ifatoia(ifa), &new_sin, 1); 4786 if(debug && error) 4787 { 4788 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: in_ifinit " 4789 " failed, error=%d\n", SPP_ARGS(ifp), error); 4790 } 4791#else 4792 /* delete old route */ 4793 error = rtinit(ifa, (int)RTM_DELETE, RTF_HOST); 4794 if(debug && error) 4795 { 4796 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit DEL failed, error=%d\n", 4797 SPP_ARGS(ifp), error); 4798 } 4799 4800 /* set new address */ 4801 si->sin_addr.s_addr = htonl(src); 4802 ia = ifatoia(ifa); 4803 LIST_REMOVE(ia, ia_hash); 4804 LIST_INSERT_HEAD(INADDR_HASH(si->sin_addr.s_addr), ia, ia_hash); 4805 4806 /* add new route */ 4807 error = rtinit(ifa, (int)RTM_ADD, RTF_HOST); 4808 if (debug && error) 4809 { 4810 log(LOG_DEBUG, SPP_FMT "sppp_set_ip_addr: rtinit ADD failed, error=%d", 4811 SPP_ARGS(ifp), error); 4812 } 4813#endif 4814 } 4815} 4816 4817#ifdef INET6 4818/* 4819 * Get both IPv6 addresses. 4820 */ 4821static void 4822sppp_get_ip6_addrs(struct sppp *sp, struct in6_addr *src, struct in6_addr *dst, 4823 struct in6_addr *srcmask) 4824{ 4825 struct ifnet *ifp = &sp->pp_if; 4826 struct ifaddr *ifa; 4827 struct sockaddr_in6 *si, *sm; 4828 struct in6_addr ssrc, ddst; 4829 4830 sm = NULL; 4831 bzero(&ssrc, sizeof(ssrc)); 4832 bzero(&ddst, sizeof(ddst)); 4833 /* 4834 * Pick the first link-local AF_INET6 address from the list, 4835 * aliases don't make any sense on a p2p link anyway. 4836 */ 4837#if defined(__FreeBSD__) && __FreeBSD__ >= 3 4838 for (ifa = ifp->if_addrhead.tqh_first, si = 0; 4839 ifa; 4840 ifa = ifa->ifa_link.tqe_next) 4841#elif defined(__NetBSD__) || defined (__OpenBSD__) 4842 for (ifa = ifp->if_addrlist.tqh_first, si = 0; 4843 ifa; 4844 ifa = ifa->ifa_list.tqe_next) 4845#else 4846 for (ifa = ifp->if_addrlist, si = 0; 4847 ifa; 4848 ifa = ifa->ifa_next) 4849#endif 4850 if (ifa->ifa_addr->sa_family == AF_INET6) { 4851 si = (struct sockaddr_in6 *)ifa->ifa_addr; 4852 sm = (struct sockaddr_in6 *)ifa->ifa_netmask; 4853 if (si && IN6_IS_ADDR_LINKLOCAL(&si->sin6_addr)) 4854 break; 4855 } 4856 if (ifa) { 4857 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) { 4858 bcopy(&si->sin6_addr, &ssrc, sizeof(ssrc)); 4859 if (srcmask) { 4860 bcopy(&sm->sin6_addr, srcmask, 4861 sizeof(*srcmask)); 4862 } 4863 } 4864 4865 si = (struct sockaddr_in6 *)ifa->ifa_dstaddr; 4866 if (si && !IN6_IS_ADDR_UNSPECIFIED(&si->sin6_addr)) 4867 bcopy(&si->sin6_addr, &ddst, sizeof(ddst)); 4868 } 4869 4870 if (dst) 4871 bcopy(&ddst, dst, sizeof(*dst)); 4872 if (src) 4873 bcopy(&ssrc, src, sizeof(*src)); 4874} 4875 4876#ifdef IPV6CP_MYIFID_DYN 4877/* 4878 * Generate random ifid. 4879 */ 4880static void 4881sppp_gen_ip6_addr(struct sppp *sp, struct in6_addr *addr) 4882{ 4883 /* TBD */ 4884} 4885 4886/* 4887 * Set my IPv6 address. Must be called at splimp. 4888 */ 4889static void 4890sppp_set_ip6_addr(struct sppp *sp, const struct in6_addr *src) 4891{ 4892 STDDCL; 4893 struct ifaddr *ifa; 4894 struct sockaddr_in6 *sin6; 4895 4896 /* 4897 * Pick the first link-local AF_INET6 address from the list, 4898 * aliases don't make any sense on a p2p link anyway. 4899 */ 4900 4901 sin6 = NULL; 4902#if defined(__FreeBSD__) && __FreeBSD__ >= 3 4903 for (ifa = ifp->if_addrhead.tqh_first; 4904 ifa; 4905 ifa = ifa->ifa_link.tqe_next) 4906#elif defined(__NetBSD__) || defined (__OpenBSD__) 4907 for (ifa = ifp->if_addrlist.tqh_first; 4908 ifa; 4909 ifa = ifa->ifa_list.tqe_next) 4910#else 4911 for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) 4912#endif 4913 { 4914 if (ifa->ifa_addr->sa_family == AF_INET6) 4915 { 4916 sin6 = (struct sockaddr_in6 *)ifa->ifa_addr; 4917 if (sin6 && IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr)) 4918 break; 4919 } 4920 } 4921 4922 if (ifa && sin6) 4923 { 4924 int error; 4925 struct sockaddr_in6 new_sin6 = *sin6; 4926 4927 bcopy(src, &new_sin6.sin6_addr, sizeof(new_sin6.sin6_addr)); 4928 error = in6_ifinit(ifp, ifatoia6(ifa), &new_sin6, 1); 4929 if (debug && error) 4930 { 4931 log(LOG_DEBUG, SPP_FMT "sppp_set_ip6_addr: in6_ifinit " 4932 " failed, error=%d\n", SPP_ARGS(ifp), error); 4933 } 4934 } 4935} 4936#endif 4937 4938/* 4939 * Suggest a candidate address to be used by peer. 4940 */ 4941static void 4942sppp_suggest_ip6_addr(struct sppp *sp, struct in6_addr *suggest) 4943{ 4944 struct in6_addr myaddr; 4945 struct timeval tv; 4946 4947 sppp_get_ip6_addrs(sp, &myaddr, 0, 0); 4948 4949 myaddr.s6_addr[8] &= ~0x02; /* u bit to "local" */ 4950 microtime(&tv); 4951 if ((tv.tv_usec & 0xff) == 0 && (tv.tv_sec & 0xff) == 0) { 4952 myaddr.s6_addr[14] ^= 0xff; 4953 myaddr.s6_addr[15] ^= 0xff; 4954 } else { 4955 myaddr.s6_addr[14] ^= (tv.tv_usec & 0xff); 4956 myaddr.s6_addr[15] ^= (tv.tv_sec & 0xff); 4957 } 4958 if (suggest) 4959 bcopy(&myaddr, suggest, sizeof(myaddr)); 4960} 4961#endif /*INET6*/ 4962 4963static int 4964sppp_params(struct sppp *sp, u_long cmd, void *data) 4965{ 4966 u_long subcmd; 4967 struct ifreq *ifr = (struct ifreq *)data; 4968 struct spppreq *spr; 4969 int rv = 0; 4970 4971 if ((spr = malloc(sizeof(struct spppreq), M_TEMP, M_NOWAIT)) == 0) 4972 return (EAGAIN); 4973 /* 4974 * ifr->ifr_data is supposed to point to a struct spppreq. 4975 * Check the cmd word first before attempting to fetch all the 4976 * data. 4977 */ 4978 if ((subcmd = fuword(ifr->ifr_data)) == -1) { 4979 rv = EFAULT; 4980 goto quit; 4981 } 4982 4983 if (copyin((caddr_t)ifr->ifr_data, spr, sizeof(struct spppreq)) != 0) { 4984 rv = EFAULT; 4985 goto quit; 4986 } 4987 4988 switch (subcmd) { 4989 case (int)SPPPIOGDEFS: 4990 if (cmd != SIOCGIFGENERIC) { 4991 rv = EINVAL; 4992 break; 4993 } 4994 /* 4995 * We copy over the entire current state, but clean 4996 * out some of the stuff we don't wanna pass up. 4997 * Remember, SIOCGIFGENERIC is unprotected, and can be 4998 * called by any user. No need to ever get PAP or 4999 * CHAP secrets back to userland anyway. 5000 */ 5001 spr->defs.pp_phase = sp->pp_phase; 5002 spr->defs.enable_vj = (sp->confflags & CONF_ENABLE_VJ) != 0; 5003 spr->defs.enable_ipv6 = (sp->confflags & CONF_ENABLE_IPV6) != 0; 5004 spr->defs.lcp = sp->lcp; 5005 spr->defs.ipcp = sp->ipcp; 5006 spr->defs.ipv6cp = sp->ipv6cp; 5007 spr->defs.myauth = sp->myauth; 5008 spr->defs.hisauth = sp->hisauth; 5009 bzero(spr->defs.myauth.secret, AUTHKEYLEN); 5010 bzero(spr->defs.myauth.challenge, AUTHKEYLEN); 5011 bzero(spr->defs.hisauth.secret, AUTHKEYLEN); 5012 bzero(spr->defs.hisauth.challenge, AUTHKEYLEN); 5013 /* 5014 * Fixup the LCP timeout value to milliseconds so 5015 * spppcontrol doesn't need to bother about the value 5016 * of "hz". We do the reverse calculation below when 5017 * setting it. 5018 */ 5019 spr->defs.lcp.timeout = sp->lcp.timeout * 1000 / hz; 5020 rv = copyout(spr, (caddr_t)ifr->ifr_data, 5021 sizeof(struct spppreq)); 5022 break; 5023 5024 case (int)SPPPIOSDEFS: 5025 if (cmd != SIOCSIFGENERIC) { 5026 rv = EINVAL; 5027 break; 5028 } 5029 /* 5030 * We have a very specific idea of which fields we 5031 * allow being passed back from userland, so to not 5032 * clobber our current state. For one, we only allow 5033 * setting anything if LCP is in dead or establish 5034 * phase. Once the authentication negotiations 5035 * started, the authentication settings must not be 5036 * changed again. (The administrator can force an 5037 * ifconfig down in order to get LCP back into dead 5038 * phase.) 5039 * 5040 * Also, we only allow for authentication parameters to be 5041 * specified. 5042 * 5043 * XXX Should allow to set or clear pp_flags. 5044 * 5045 * Finally, if the respective authentication protocol to 5046 * be used is set differently than 0, but the secret is 5047 * passed as all zeros, we don't trash the existing secret. 5048 * This allows an administrator to change the system name 5049 * only without clobbering the secret (which he didn't get 5050 * back in a previous SPPPIOGDEFS call). However, the 5051 * secrets are cleared if the authentication protocol is 5052 * reset to 0. */ 5053 if (sp->pp_phase != PHASE_DEAD && 5054 sp->pp_phase != PHASE_ESTABLISH) { 5055 rv = EBUSY; 5056 break; 5057 } 5058 5059 if ((spr->defs.myauth.proto != 0 && spr->defs.myauth.proto != PPP_PAP && 5060 spr->defs.myauth.proto != PPP_CHAP) || 5061 (spr->defs.hisauth.proto != 0 && spr->defs.hisauth.proto != PPP_PAP && 5062 spr->defs.hisauth.proto != PPP_CHAP)) { 5063 rv = EINVAL; 5064 break; 5065 } 5066 5067 if (spr->defs.myauth.proto == 0) 5068 /* resetting myauth */ 5069 bzero(&sp->myauth, sizeof sp->myauth); 5070 else { 5071 /* setting/changing myauth */ 5072 sp->myauth.proto = spr->defs.myauth.proto; 5073 bcopy(spr->defs.myauth.name, sp->myauth.name, AUTHNAMELEN); 5074 if (spr->defs.myauth.secret[0] != '\0') 5075 bcopy(spr->defs.myauth.secret, sp->myauth.secret, 5076 AUTHKEYLEN); 5077 } 5078 if (spr->defs.hisauth.proto == 0) 5079 /* resetting hisauth */ 5080 bzero(&sp->hisauth, sizeof sp->hisauth); 5081 else { 5082 /* setting/changing hisauth */ 5083 sp->hisauth.proto = spr->defs.hisauth.proto; 5084 sp->hisauth.flags = spr->defs.hisauth.flags; 5085 bcopy(spr->defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN); 5086 if (spr->defs.hisauth.secret[0] != '\0') 5087 bcopy(spr->defs.hisauth.secret, sp->hisauth.secret, 5088 AUTHKEYLEN); 5089 } 5090 /* set LCP restart timer timeout */ 5091 if (spr->defs.lcp.timeout != 0) 5092 sp->lcp.timeout = spr->defs.lcp.timeout * hz / 1000; 5093 /* set VJ enable and IPv6 disable flags */ 5094#ifdef INET 5095 if (spr->defs.enable_vj) 5096 sp->confflags |= CONF_ENABLE_VJ; 5097 else 5098 sp->confflags &= ~CONF_ENABLE_VJ; 5099#endif 5100#ifdef INET6 5101 if (spr->defs.enable_ipv6) 5102 sp->confflags |= CONF_ENABLE_IPV6; 5103 else 5104 sp->confflags &= ~CONF_ENABLE_IPV6; 5105#endif 5106 break; 5107 5108 default: 5109 rv = EINVAL; 5110 } 5111 5112 quit: 5113 free(spr, M_TEMP); 5114 5115 return (rv); 5116} 5117 5118static void 5119sppp_phase_network(struct sppp *sp) 5120{ 5121 STDDCL; 5122 int i; 5123 u_long mask; 5124 5125 sp->pp_phase = PHASE_NETWORK; 5126 5127 if (debug) 5128 log(LOG_DEBUG, SPP_FMT "phase %s\n", SPP_ARGS(ifp), 5129 sppp_phase_name(sp->pp_phase)); 5130 5131 /* Notify NCPs now. */ 5132 for (i = 0; i < IDX_COUNT; i++) 5133 if ((cps[i])->flags & CP_NCP) 5134 (cps[i])->Open(sp); 5135 5136 /* Send Up events to all NCPs. */ 5137 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1) 5138 if ((sp->lcp.protos & mask) && ((cps[i])->flags & CP_NCP)) 5139 (cps[i])->Up(sp); 5140 5141 /* if no NCP is starting, all this was in vain, close down */ 5142 sppp_lcp_check_and_close(sp); 5143} 5144 5145 5146static const char * 5147sppp_cp_type_name(u_char type) 5148{ 5149 static char buf[12]; 5150 switch (type) { 5151 case CONF_REQ: return "conf-req"; 5152 case CONF_ACK: return "conf-ack"; 5153 case CONF_NAK: return "conf-nak"; 5154 case CONF_REJ: return "conf-rej"; 5155 case TERM_REQ: return "term-req"; 5156 case TERM_ACK: return "term-ack"; 5157 case CODE_REJ: return "code-rej"; 5158 case PROTO_REJ: return "proto-rej"; 5159 case ECHO_REQ: return "echo-req"; 5160 case ECHO_REPLY: return "echo-reply"; 5161 case DISC_REQ: return "discard-req"; 5162 } 5163 snprintf (buf, sizeof(buf), "cp/0x%x", type); 5164 return buf; 5165} 5166 5167static const char * 5168sppp_auth_type_name(u_short proto, u_char type) 5169{ 5170 static char buf[12]; 5171 switch (proto) { 5172 case PPP_CHAP: 5173 switch (type) { 5174 case CHAP_CHALLENGE: return "challenge"; 5175 case CHAP_RESPONSE: return "response"; 5176 case CHAP_SUCCESS: return "success"; 5177 case CHAP_FAILURE: return "failure"; 5178 } 5179 case PPP_PAP: 5180 switch (type) { 5181 case PAP_REQ: return "req"; 5182 case PAP_ACK: return "ack"; 5183 case PAP_NAK: return "nak"; 5184 } 5185 } 5186 snprintf (buf, sizeof(buf), "auth/0x%x", type); 5187 return buf; 5188} 5189 5190static const char * 5191sppp_lcp_opt_name(u_char opt) 5192{ 5193 static char buf[12]; 5194 switch (opt) { 5195 case LCP_OPT_MRU: return "mru"; 5196 case LCP_OPT_ASYNC_MAP: return "async-map"; 5197 case LCP_OPT_AUTH_PROTO: return "auth-proto"; 5198 case LCP_OPT_QUAL_PROTO: return "qual-proto"; 5199 case LCP_OPT_MAGIC: return "magic"; 5200 case LCP_OPT_PROTO_COMP: return "proto-comp"; 5201 case LCP_OPT_ADDR_COMP: return "addr-comp"; 5202 } 5203 snprintf (buf, sizeof(buf), "lcp/0x%x", opt); 5204 return buf; 5205} 5206 5207static const char * 5208sppp_ipcp_opt_name(u_char opt) 5209{ 5210 static char buf[12]; 5211 switch (opt) { 5212 case IPCP_OPT_ADDRESSES: return "addresses"; 5213 case IPCP_OPT_COMPRESSION: return "compression"; 5214 case IPCP_OPT_ADDRESS: return "address"; 5215 } 5216 snprintf (buf, sizeof(buf), "ipcp/0x%x", opt); 5217 return buf; 5218} 5219 5220#ifdef INET6 5221static const char * 5222sppp_ipv6cp_opt_name(u_char opt) 5223{ 5224 static char buf[12]; 5225 switch (opt) { 5226 case IPV6CP_OPT_IFID: return "ifid"; 5227 case IPV6CP_OPT_COMPRESSION: return "compression"; 5228 } 5229 sprintf (buf, "0x%x", opt); 5230 return buf; 5231} 5232#endif 5233 5234static const char * 5235sppp_state_name(int state) 5236{ 5237 switch (state) { 5238 case STATE_INITIAL: return "initial"; 5239 case STATE_STARTING: return "starting"; 5240 case STATE_CLOSED: return "closed"; 5241 case STATE_STOPPED: return "stopped"; 5242 case STATE_CLOSING: return "closing"; 5243 case STATE_STOPPING: return "stopping"; 5244 case STATE_REQ_SENT: return "req-sent"; 5245 case STATE_ACK_RCVD: return "ack-rcvd"; 5246 case STATE_ACK_SENT: return "ack-sent"; 5247 case STATE_OPENED: return "opened"; 5248 } 5249 return "illegal"; 5250} 5251 5252static const char * 5253sppp_phase_name(enum ppp_phase phase) 5254{ 5255 switch (phase) { 5256 case PHASE_DEAD: return "dead"; 5257 case PHASE_ESTABLISH: return "establish"; 5258 case PHASE_TERMINATE: return "terminate"; 5259 case PHASE_AUTHENTICATE: return "authenticate"; 5260 case PHASE_NETWORK: return "network"; 5261 } 5262 return "illegal"; 5263} 5264 5265static const char * 5266sppp_proto_name(u_short proto) 5267{ 5268 static char buf[12]; 5269 switch (proto) { 5270 case PPP_LCP: return "lcp"; 5271 case PPP_IPCP: return "ipcp"; 5272 case PPP_PAP: return "pap"; 5273 case PPP_CHAP: return "chap"; 5274 case PPP_IPV6CP: return "ipv6cp"; 5275 } 5276 snprintf(buf, sizeof(buf), "proto/0x%x", (unsigned)proto); 5277 return buf; 5278} 5279 5280static void 5281sppp_print_bytes(const u_char *p, u_short len) 5282{ 5283 if (len) 5284 log(-1, " %*D", len, p, "-"); 5285} 5286 5287static void 5288sppp_print_string(const char *p, u_short len) 5289{ 5290 u_char c; 5291 5292 while (len-- > 0) { 5293 c = *p++; 5294 /* 5295 * Print only ASCII chars directly. RFC 1994 recommends 5296 * using only them, but we don't rely on it. */ 5297 if (c < ' ' || c > '~') 5298 log(-1, "\\x%x", c); 5299 else 5300 log(-1, "%c", c); 5301 } 5302} 5303 5304static const char * 5305sppp_dotted_quad(u_long addr) 5306{ 5307 static char s[16]; 5308 sprintf(s, "%d.%d.%d.%d", 5309 (int)((addr >> 24) & 0xff), 5310 (int)((addr >> 16) & 0xff), 5311 (int)((addr >> 8) & 0xff), 5312 (int)(addr & 0xff)); 5313 return s; 5314} 5315 5316static int 5317sppp_strnlen(u_char *p, int max) 5318{ 5319 int len; 5320 5321 for (len = 0; len < max && *p; ++p) 5322 ++len; 5323 return len; 5324} 5325 5326/* a dummy, used to drop uninteresting events */ 5327static void 5328sppp_null(struct sppp *unused) 5329{ 5330 /* do just nothing */ 5331} 5332