if_spppsubr.c revision 35029
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, 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 * $Id: if_spppsubr.c,v 1.35 1998/03/30 09:52:06 phk Exp $ 21 */ 22 23#include "opt_inet.h" 24#include "opt_ipx.h" 25 26#include <sys/param.h> 27#include <sys/systm.h> 28#include <sys/kernel.h> 29#include <sys/sockio.h> 30#include <sys/socket.h> 31#include <sys/syslog.h> 32#include <machine/random.h> 33#include <sys/malloc.h> 34#include <sys/mbuf.h> 35#include <sys/md5.h> 36 37#include <net/if.h> 38#include <net/netisr.h> 39#include <net/if_types.h> 40 41#include <machine/stdarg.h> 42 43#ifdef INET 44#include <netinet/in.h> 45#include <netinet/in_systm.h> 46#include <netinet/in_var.h> 47#include <netinet/ip.h> 48#include <netinet/tcp.h> 49#include <netinet/if_ether.h> 50#else 51#error Huh? sppp without INET? 52#endif 53 54#ifdef IPX 55#include <netipx/ipx.h> 56#include <netipx/ipx_if.h> 57#endif 58 59#ifdef NS 60#include <netns/ns.h> 61#include <netns/ns_if.h> 62#endif 63 64#ifdef ISO 65#include <netiso/argo_debug.h> 66#include <netiso/iso.h> 67#include <netiso/iso_var.h> 68#include <netiso/iso_snpac.h> 69#endif 70 71#include <net/if_sppp.h> 72 73#define MAXALIVECNT 3 /* max. alive packets */ 74 75/* 76 * Interface flags that can be set in an ifconfig command. 77 * 78 * Setting link0 will make the link passive, i.e. it will be marked 79 * as being administrative openable, but won't be opened to begin 80 * with. Incoming calls will be answered, or subsequent calls with 81 * -link1 will cause the administrative open of the LCP layer. 82 * 83 * Setting link1 will cause the link to auto-dial only as packets 84 * arrive to be sent. 85 * 86 * Setting IFF_DEBUG will syslog the option negotiation and state 87 * transitions at level kern.debug. Note: all logs consistently look 88 * like 89 * 90 * <if-name><unit>: <proto-name> <additional info...> 91 * 92 * with <if-name><unit> being something like "bppp0", and <proto-name> 93 * being one of "lcp", "ipcp", "cisco", "chap", "pap", etc. 94 */ 95 96#define IFF_PASSIVE IFF_LINK0 /* wait passively for connection */ 97#define IFF_AUTO IFF_LINK1 /* auto-dial on output */ 98 99#define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */ 100#define PPP_UI 0x03 /* Unnumbered Information */ 101#define PPP_IP 0x0021 /* Internet Protocol */ 102#define PPP_ISO 0x0023 /* ISO OSI Protocol */ 103#define PPP_XNS 0x0025 /* Xerox NS Protocol */ 104#define PPP_IPX 0x002b /* Novell IPX Protocol */ 105#define PPP_LCP 0xc021 /* Link Control Protocol */ 106#define PPP_PAP 0xc023 /* Password Authentication Protocol */ 107#define PPP_CHAP 0xc223 /* Challenge-Handshake Auth Protocol */ 108#define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */ 109 110#define CONF_REQ 1 /* PPP configure request */ 111#define CONF_ACK 2 /* PPP configure acknowledge */ 112#define CONF_NAK 3 /* PPP configure negative ack */ 113#define CONF_REJ 4 /* PPP configure reject */ 114#define TERM_REQ 5 /* PPP terminate request */ 115#define TERM_ACK 6 /* PPP terminate acknowledge */ 116#define CODE_REJ 7 /* PPP code reject */ 117#define PROTO_REJ 8 /* PPP protocol reject */ 118#define ECHO_REQ 9 /* PPP echo request */ 119#define ECHO_REPLY 10 /* PPP echo reply */ 120#define DISC_REQ 11 /* PPP discard request */ 121 122#define LCP_OPT_MRU 1 /* maximum receive unit */ 123#define LCP_OPT_ASYNC_MAP 2 /* async control character map */ 124#define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */ 125#define LCP_OPT_QUAL_PROTO 4 /* quality protocol */ 126#define LCP_OPT_MAGIC 5 /* magic number */ 127#define LCP_OPT_RESERVED 6 /* reserved */ 128#define LCP_OPT_PROTO_COMP 7 /* protocol field compression */ 129#define LCP_OPT_ADDR_COMP 8 /* address/control field compression */ 130 131#define IPCP_OPT_ADDRESSES 1 /* both IP addresses; deprecated */ 132#define IPCP_OPT_COMPRESSION 2 /* IP compression protocol (VJ) */ 133#define IPCP_OPT_ADDRESS 3 /* local IP address */ 134 135#define PAP_REQ 1 /* PAP name/password request */ 136#define PAP_ACK 2 /* PAP acknowledge */ 137#define PAP_NAK 3 /* PAP fail */ 138 139#define CHAP_CHALLENGE 1 /* CHAP challenge request */ 140#define CHAP_RESPONSE 2 /* CHAP challenge response */ 141#define CHAP_SUCCESS 3 /* CHAP response ok */ 142#define CHAP_FAILURE 4 /* CHAP response failed */ 143 144#define CHAP_MD5 5 /* hash algorithm - MD5 */ 145 146#define CISCO_MULTICAST 0x8f /* Cisco multicast address */ 147#define CISCO_UNICAST 0x0f /* Cisco unicast address */ 148#define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */ 149#define CISCO_ADDR_REQ 0 /* Cisco address request */ 150#define CISCO_ADDR_REPLY 1 /* Cisco address reply */ 151#define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */ 152 153/* states are named and numbered according to RFC 1661 */ 154#define STATE_INITIAL 0 155#define STATE_STARTING 1 156#define STATE_CLOSED 2 157#define STATE_STOPPED 3 158#define STATE_CLOSING 4 159#define STATE_STOPPING 5 160#define STATE_REQ_SENT 6 161#define STATE_ACK_RCVD 7 162#define STATE_ACK_SENT 8 163#define STATE_OPENED 9 164 165struct ppp_header { 166 u_char address; 167 u_char control; 168 u_short protocol; 169}; 170#define PPP_HEADER_LEN sizeof (struct ppp_header) 171 172struct lcp_header { 173 u_char type; 174 u_char ident; 175 u_short len; 176}; 177#define LCP_HEADER_LEN sizeof (struct lcp_header) 178 179struct cisco_packet { 180 u_long type; 181 u_long par1; 182 u_long par2; 183 u_short rel; 184 u_short time0; 185 u_short time1; 186}; 187#define CISCO_PACKET_LEN 18 188 189/* 190 * We follow the spelling and capitalization of RFC 1661 here, to make 191 * it easier comparing with the standard. Please refer to this RFC in 192 * case you can't make sense out of these abbreviation; it will also 193 * explain the semantics related to the various events and actions. 194 */ 195struct cp { 196 u_short proto; /* PPP control protocol number */ 197 u_char protoidx; /* index into state table in struct sppp */ 198 u_char flags; 199#define CP_LCP 0x01 /* this is the LCP */ 200#define CP_AUTH 0x02 /* this is an authentication protocol */ 201#define CP_NCP 0x04 /* this is a NCP */ 202#define CP_QUAL 0x08 /* this is a quality reporting protocol */ 203 const char *name; /* name of this control protocol */ 204 /* event handlers */ 205 void (*Up)(struct sppp *sp); 206 void (*Down)(struct sppp *sp); 207 void (*Open)(struct sppp *sp); 208 void (*Close)(struct sppp *sp); 209 void (*TO)(void *sp); 210 int (*RCR)(struct sppp *sp, struct lcp_header *h, int len); 211 void (*RCN_rej)(struct sppp *sp, struct lcp_header *h, int len); 212 void (*RCN_nak)(struct sppp *sp, struct lcp_header *h, int len); 213 /* actions */ 214 void (*tlu)(struct sppp *sp); 215 void (*tld)(struct sppp *sp); 216 void (*tls)(struct sppp *sp); 217 void (*tlf)(struct sppp *sp); 218 void (*scr)(struct sppp *sp); 219}; 220 221static struct sppp *spppq; 222static struct callout_handle keepalive_ch; 223 224/* 225 * The following disgusting hack gets around the problem that IP TOS 226 * can't be set yet. We want to put "interactive" traffic on a high 227 * priority queue. To decide if traffic is interactive, we check that 228 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control. 229 * 230 * XXX is this really still necessary? - joerg - 231 */ 232static u_short interactive_ports[8] = { 233 0, 513, 0, 0, 234 0, 21, 0, 23, 235}; 236#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p)) 237 238/* almost every function needs these */ 239#define STDDCL \ 240 struct ifnet *ifp = &sp->pp_if; \ 241 int debug = ifp->if_flags & IFF_DEBUG 242 243static int sppp_output(struct ifnet *ifp, struct mbuf *m, 244 struct sockaddr *dst, struct rtentry *rt); 245 246static void sppp_cisco_send(struct sppp *sp, int type, long par1, long par2); 247static void sppp_cisco_input(struct sppp *sp, struct mbuf *m); 248 249static void sppp_cp_input(const struct cp *cp, struct sppp *sp, 250 struct mbuf *m); 251static void sppp_cp_send(struct sppp *sp, u_short proto, u_char type, 252 u_char ident, u_short len, void *data); 253static void sppp_cp_timeout(void *arg); 254static void sppp_cp_change_state(const struct cp *cp, struct sppp *sp, 255 int newstate); 256static void sppp_auth_send(const struct cp *cp, 257 struct sppp *sp, u_char type, u_char id, 258 ...); 259 260static void sppp_up_event(const struct cp *cp, struct sppp *sp); 261static void sppp_down_event(const struct cp *cp, struct sppp *sp); 262static void sppp_open_event(const struct cp *cp, struct sppp *sp); 263static void sppp_close_event(const struct cp *cp, struct sppp *sp); 264static void sppp_to_event(const struct cp *cp, struct sppp *sp); 265 266static void sppp_null(struct sppp *sp); 267 268static void sppp_lcp_init(struct sppp *sp); 269static void sppp_lcp_up(struct sppp *sp); 270static void sppp_lcp_down(struct sppp *sp); 271static void sppp_lcp_open(struct sppp *sp); 272static void sppp_lcp_close(struct sppp *sp); 273static void sppp_lcp_TO(void *sp); 274static int sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len); 275static void sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len); 276static void sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len); 277static void sppp_lcp_tlu(struct sppp *sp); 278static void sppp_lcp_tld(struct sppp *sp); 279static void sppp_lcp_tls(struct sppp *sp); 280static void sppp_lcp_tlf(struct sppp *sp); 281static void sppp_lcp_scr(struct sppp *sp); 282static void sppp_lcp_check_and_close(struct sppp *sp); 283static int sppp_ncp_check(struct sppp *sp); 284 285static void sppp_ipcp_init(struct sppp *sp); 286static void sppp_ipcp_up(struct sppp *sp); 287static void sppp_ipcp_down(struct sppp *sp); 288static void sppp_ipcp_open(struct sppp *sp); 289static void sppp_ipcp_close(struct sppp *sp); 290static void sppp_ipcp_TO(void *sp); 291static int sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len); 292static void sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len); 293static void sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len); 294static void sppp_ipcp_tlu(struct sppp *sp); 295static void sppp_ipcp_tld(struct sppp *sp); 296static void sppp_ipcp_tls(struct sppp *sp); 297static void sppp_ipcp_tlf(struct sppp *sp); 298static void sppp_ipcp_scr(struct sppp *sp); 299 300static void sppp_pap_input(struct sppp *sp, struct mbuf *m); 301static void sppp_pap_init(struct sppp *sp); 302static void sppp_pap_open(struct sppp *sp); 303static void sppp_pap_close(struct sppp *sp); 304static void sppp_pap_TO(void *sp); 305static void sppp_pap_my_TO(void *sp); 306static void sppp_pap_tlu(struct sppp *sp); 307static void sppp_pap_tld(struct sppp *sp); 308static void sppp_pap_scr(struct sppp *sp); 309 310static void sppp_chap_input(struct sppp *sp, struct mbuf *m); 311static void sppp_chap_init(struct sppp *sp); 312static void sppp_chap_open(struct sppp *sp); 313static void sppp_chap_close(struct sppp *sp); 314static void sppp_chap_TO(void *sp); 315static void sppp_chap_tlu(struct sppp *sp); 316static void sppp_chap_tld(struct sppp *sp); 317static void sppp_chap_scr(struct sppp *sp); 318 319static const char *sppp_auth_type_name(u_short proto, u_char type); 320static const char *sppp_cp_type_name(u_char type); 321static const char *sppp_dotted_quad(u_long addr); 322static const char *sppp_ipcp_opt_name(u_char opt); 323static const char *sppp_lcp_opt_name(u_char opt); 324static const char *sppp_phase_name(enum ppp_phase phase); 325static const char *sppp_proto_name(u_short proto); 326static const char *sppp_state_name(int state); 327static int sppp_params(struct sppp *sp, int cmd, void *data); 328static int sppp_strnlen(u_char *p, int max); 329static void sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, 330 u_long *srcmask); 331static void sppp_keepalive(void *dummy); 332static void sppp_phase_network(struct sppp *sp); 333static void sppp_print_bytes(const u_char *p, u_short len); 334static void sppp_print_string(const char *p, u_short len); 335static void sppp_qflush(struct ifqueue *ifq); 336static void sppp_set_ip_addr(struct sppp *sp, u_long src); 337 338/* our control protocol descriptors */ 339static const struct cp lcp = { 340 PPP_LCP, IDX_LCP, CP_LCP, "lcp", 341 sppp_lcp_up, sppp_lcp_down, sppp_lcp_open, sppp_lcp_close, 342 sppp_lcp_TO, sppp_lcp_RCR, sppp_lcp_RCN_rej, sppp_lcp_RCN_nak, 343 sppp_lcp_tlu, sppp_lcp_tld, sppp_lcp_tls, sppp_lcp_tlf, 344 sppp_lcp_scr 345}; 346 347static const struct cp ipcp = { 348 PPP_IPCP, IDX_IPCP, CP_NCP, "ipcp", 349 sppp_ipcp_up, sppp_ipcp_down, sppp_ipcp_open, sppp_ipcp_close, 350 sppp_ipcp_TO, sppp_ipcp_RCR, sppp_ipcp_RCN_rej, sppp_ipcp_RCN_nak, 351 sppp_ipcp_tlu, sppp_ipcp_tld, sppp_ipcp_tls, sppp_ipcp_tlf, 352 sppp_ipcp_scr 353}; 354 355static const struct cp pap = { 356 PPP_PAP, IDX_PAP, CP_AUTH, "pap", 357 sppp_null, sppp_null, sppp_pap_open, sppp_pap_close, 358 sppp_pap_TO, 0, 0, 0, 359 sppp_pap_tlu, sppp_pap_tld, sppp_null, sppp_null, 360 sppp_pap_scr 361}; 362 363static const struct cp chap = { 364 PPP_CHAP, IDX_CHAP, CP_AUTH, "chap", 365 sppp_null, sppp_null, sppp_chap_open, sppp_chap_close, 366 sppp_chap_TO, 0, 0, 0, 367 sppp_chap_tlu, sppp_chap_tld, sppp_null, sppp_null, 368 sppp_chap_scr 369}; 370 371static const struct cp *cps[IDX_COUNT] = { 372 &lcp, /* IDX_LCP */ 373 &ipcp, /* IDX_IPCP */ 374 &pap, /* IDX_PAP */ 375 &chap, /* IDX_CHAP */ 376}; 377 378 379/* 380 * Exported functions, comprising our interface to the lower layer. 381 */ 382 383/* 384 * Process the received packet. 385 */ 386void 387sppp_input(struct ifnet *ifp, struct mbuf *m) 388{ 389 struct ppp_header *h; 390 struct ifqueue *inq = 0; 391 int s; 392 struct sppp *sp = (struct sppp *)ifp; 393 int debug = ifp->if_flags & IFF_DEBUG; 394 395 if (ifp->if_flags & IFF_UP) 396 /* Count received bytes, add FCS and one flag */ 397 ifp->if_ibytes += m->m_pkthdr.len + 3; 398 399 if (m->m_pkthdr.len <= PPP_HEADER_LEN) { 400 /* Too small packet, drop it. */ 401 if (debug) 402 log(LOG_DEBUG, 403 "%s%d: input packet is too small, %d bytes\n", 404 ifp->if_name, ifp->if_unit, m->m_pkthdr.len); 405 drop: 406 ++ifp->if_ierrors; 407 ++ifp->if_iqdrops; 408 m_freem (m); 409 return; 410 } 411 412 /* Get PPP header. */ 413 h = mtod (m, struct ppp_header*); 414 m_adj (m, PPP_HEADER_LEN); 415 416 switch (h->address) { 417 case PPP_ALLSTATIONS: 418 if (h->control != PPP_UI) 419 goto invalid; 420 if (sp->pp_flags & PP_CISCO) { 421 if (debug) 422 log(LOG_DEBUG, 423 "%s%d: PPP packet in Cisco mode " 424 "<addr=0x%x ctrl=0x%x proto=0x%x>\n", 425 ifp->if_name, ifp->if_unit, 426 h->address, h->control, ntohs(h->protocol)); 427 goto drop; 428 } 429 switch (ntohs (h->protocol)) { 430 default: 431 if (sp->state[IDX_LCP] == STATE_OPENED) 432 sppp_cp_send (sp, PPP_LCP, PROTO_REJ, 433 ++sp->pp_seq, m->m_pkthdr.len + 2, 434 &h->protocol); 435 if (debug) 436 log(LOG_DEBUG, 437 "%s%d: invalid input protocol " 438 "<addr=0x%x ctrl=0x%x proto=0x%x>\n", 439 ifp->if_name, ifp->if_unit, 440 h->address, h->control, ntohs(h->protocol)); 441 ++ifp->if_noproto; 442 goto drop; 443 case PPP_LCP: 444 sppp_cp_input(&lcp, sp, m); 445 m_freem (m); 446 return; 447 case PPP_PAP: 448 if (sp->pp_phase >= PHASE_AUTHENTICATE) 449 sppp_pap_input(sp, m); 450 m_freem (m); 451 return; 452 case PPP_CHAP: 453 if (sp->pp_phase >= PHASE_AUTHENTICATE) 454 sppp_chap_input(sp, m); 455 m_freem (m); 456 return; 457#ifdef INET 458 case PPP_IPCP: 459 if (sp->pp_phase == PHASE_NETWORK) 460 sppp_cp_input(&ipcp, sp, m); 461 m_freem (m); 462 return; 463 case PPP_IP: 464 if (sp->state[IDX_IPCP] == STATE_OPENED) { 465 schednetisr (NETISR_IP); 466 inq = &ipintrq; 467 } 468 break; 469#endif 470#ifdef IPX 471 case PPP_IPX: 472 /* IPX IPXCP not implemented yet */ 473 if (sp->pp_phase == PHASE_NETWORK) { 474 schednetisr (NETISR_IPX); 475 inq = &ipxintrq; 476 } 477 break; 478#endif 479#ifdef NS 480 case PPP_XNS: 481 /* XNS IDPCP not implemented yet */ 482 if (sp->pp_phase == PHASE_NETWORK) { 483 schednetisr (NETISR_NS); 484 inq = &nsintrq; 485 } 486 break; 487#endif 488#ifdef ISO 489 case PPP_ISO: 490 /* OSI NLCP not implemented yet */ 491 if (sp->pp_phase == PHASE_NETWORK) { 492 schednetisr (NETISR_ISO); 493 inq = &clnlintrq; 494 } 495 break; 496#endif 497 } 498 break; 499 case CISCO_MULTICAST: 500 case CISCO_UNICAST: 501 /* Don't check the control field here (RFC 1547). */ 502 if (! (sp->pp_flags & PP_CISCO)) { 503 if (debug) 504 log(LOG_DEBUG, 505 "%s%d: Cisco packet in PPP mode " 506 "<addr=0x%x ctrl=0x%x proto=0x%x>\n", 507 ifp->if_name, ifp->if_unit, 508 h->address, h->control, ntohs(h->protocol)); 509 goto drop; 510 } 511 switch (ntohs (h->protocol)) { 512 default: 513 ++ifp->if_noproto; 514 goto invalid; 515 case CISCO_KEEPALIVE: 516 sppp_cisco_input ((struct sppp*) ifp, m); 517 m_freem (m); 518 return; 519#ifdef INET 520 case ETHERTYPE_IP: 521 schednetisr (NETISR_IP); 522 inq = &ipintrq; 523 break; 524#endif 525#ifdef IPX 526 case ETHERTYPE_IPX: 527 schednetisr (NETISR_IPX); 528 inq = &ipxintrq; 529 break; 530#endif 531#ifdef NS 532 case ETHERTYPE_NS: 533 schednetisr (NETISR_NS); 534 inq = &nsintrq; 535 break; 536#endif 537 } 538 break; 539 default: /* Invalid PPP packet. */ 540 invalid: 541 if (debug) 542 log(LOG_DEBUG, 543 "%s%d: invalid input packet " 544 "<addr=0x%x ctrl=0x%x proto=0x%x>\n", 545 ifp->if_name, ifp->if_unit, 546 h->address, h->control, ntohs(h->protocol)); 547 goto drop; 548 } 549 550 if (! (ifp->if_flags & IFF_UP) || ! inq) 551 goto drop; 552 553 /* Check queue. */ 554 s = splimp(); 555 if (IF_QFULL (inq)) { 556 /* Queue overflow. */ 557 IF_DROP(inq); 558 splx(s); 559 if (debug) 560 log(LOG_DEBUG, "%s%d: protocol queue overflow\n", 561 ifp->if_name, ifp->if_unit); 562 goto drop; 563 } 564 IF_ENQUEUE(inq, m); 565 splx(s); 566} 567 568/* 569 * Enqueue transmit packet. 570 */ 571static int 572sppp_output(struct ifnet *ifp, struct mbuf *m, 573 struct sockaddr *dst, struct rtentry *rt) 574{ 575 struct sppp *sp = (struct sppp*) ifp; 576 struct ppp_header *h; 577 struct ifqueue *ifq; 578 int s, rv = 0; 579 580 s = splimp(); 581 582 if ((ifp->if_flags & IFF_UP) == 0 || 583 (ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == 0) { 584 m_freem (m); 585 splx (s); 586 return (ENETDOWN); 587 } 588 589 if ((ifp->if_flags & (IFF_RUNNING | IFF_AUTO)) == IFF_AUTO) { 590 /* 591 * Interface is not yet running, but auto-dial. Need 592 * to start LCP for it. 593 */ 594 ifp->if_flags |= IFF_RUNNING; 595 splx(s); 596 lcp.Open(sp); 597 s = splimp(); 598 } 599 600 ifq = &ifp->if_snd; 601#ifdef INET 602 /* 603 * Put low delay, telnet, rlogin and ftp control packets 604 * in front of the queue. 605 */ 606 if (dst->sa_family == AF_INET) { 607 struct ip *ip = mtod (m, struct ip*); 608 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl); 609 610 if (! IF_QFULL (&sp->pp_fastq) && 611 ((ip->ip_tos & IPTOS_LOWDELAY) || 612 ip->ip_p == IPPROTO_TCP && 613 m->m_len >= sizeof (struct ip) + sizeof (struct tcphdr) && 614 (INTERACTIVE (ntohs (tcp->th_sport)) || 615 INTERACTIVE (ntohs (tcp->th_dport))))) 616 ifq = &sp->pp_fastq; 617 } 618#endif 619 620 /* 621 * Prepend general data packet PPP header. For now, IP only. 622 */ 623 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT); 624 if (! m) { 625 if (ifp->if_flags & IFF_DEBUG) 626 log(LOG_DEBUG, "%s%d: no memory for transmit header\n", 627 ifp->if_name, ifp->if_unit); 628 ++ifp->if_oerrors; 629 splx (s); 630 return (ENOBUFS); 631 } 632 h = mtod (m, struct ppp_header*); 633 if (sp->pp_flags & PP_CISCO) { 634 h->address = CISCO_UNICAST; /* unicast address */ 635 h->control = 0; 636 } else { 637 h->address = PPP_ALLSTATIONS; /* broadcast address */ 638 h->control = PPP_UI; /* Unnumbered Info */ 639 } 640 641 switch (dst->sa_family) { 642#ifdef INET 643 case AF_INET: /* Internet Protocol */ 644 if (sp->pp_flags & PP_CISCO) 645 h->protocol = htons (ETHERTYPE_IP); 646 else { 647 /* 648 * Don't choke with an ENETDOWN early. It's 649 * possible that we just started dialing out, 650 * so don't drop the packet immediately. If 651 * we notice that we run out of buffer space 652 * below, we will however remember that we are 653 * not ready to carry IP packets, and return 654 * ENETDOWN, as opposed to ENOBUFS. 655 */ 656 h->protocol = htons(PPP_IP); 657 if (sp->state[IDX_IPCP] != STATE_OPENED) 658 rv = ENETDOWN; 659 } 660 break; 661#endif 662#ifdef NS 663 case AF_NS: /* Xerox NS Protocol */ 664 h->protocol = htons ((sp->pp_flags & PP_CISCO) ? 665 ETHERTYPE_NS : PPP_XNS); 666 break; 667#endif 668#ifdef IPX 669 case AF_IPX: /* Novell IPX Protocol */ 670 h->protocol = htons ((sp->pp_flags & PP_CISCO) ? 671 ETHERTYPE_IPX : PPP_IPX); 672 break; 673#endif 674#ifdef ISO 675 case AF_ISO: /* ISO OSI Protocol */ 676 if (sp->pp_flags & PP_CISCO) 677 goto nosupport; 678 h->protocol = htons (PPP_ISO); 679 break; 680nosupport: 681#endif 682 default: 683 m_freem (m); 684 ++ifp->if_oerrors; 685 splx (s); 686 return (EAFNOSUPPORT); 687 } 688 689 /* 690 * Queue message on interface, and start output if interface 691 * not yet active. 692 */ 693 if (IF_QFULL (ifq)) { 694 IF_DROP (&ifp->if_snd); 695 m_freem (m); 696 ++ifp->if_oerrors; 697 splx (s); 698 return (rv? rv: ENOBUFS); 699 } 700 IF_ENQUEUE (ifq, m); 701 if (! (ifp->if_flags & IFF_OACTIVE)) 702 (*ifp->if_start) (ifp); 703 704 /* 705 * Count output packets and bytes. 706 * The packet length includes header, FCS and 1 flag, 707 * according to RFC 1333. 708 */ 709 ifp->if_obytes += m->m_pkthdr.len + 3; 710 splx (s); 711 return (0); 712} 713 714void 715sppp_attach(struct ifnet *ifp) 716{ 717 struct sppp *sp = (struct sppp*) ifp; 718 719 /* Initialize keepalive handler. */ 720 if (! spppq) 721 keepalive_ch = timeout(sppp_keepalive, 0, hz * 10); 722 723 /* Insert new entry into the keepalive list. */ 724 sp->pp_next = spppq; 725 spppq = sp; 726 727 sp->pp_if.if_type = IFT_PPP; 728 sp->pp_if.if_output = sppp_output; 729 sp->pp_fastq.ifq_maxlen = 32; 730 sp->pp_cpq.ifq_maxlen = 20; 731 sp->pp_loopcnt = 0; 732 sp->pp_alivecnt = 0; 733 sp->pp_seq = 0; 734 sp->pp_rseq = 0; 735 sp->pp_phase = PHASE_DEAD; 736 sp->pp_up = lcp.Up; 737 sp->pp_down = lcp.Down; 738 739 sppp_lcp_init(sp); 740 sppp_ipcp_init(sp); 741 sppp_pap_init(sp); 742 sppp_chap_init(sp); 743} 744 745void 746sppp_detach(struct ifnet *ifp) 747{ 748 struct sppp **q, *p, *sp = (struct sppp*) ifp; 749 int i; 750 751 /* Remove the entry from the keepalive list. */ 752 for (q = &spppq; (p = *q); q = &p->pp_next) 753 if (p == sp) { 754 *q = p->pp_next; 755 break; 756 } 757 758 /* Stop keepalive handler. */ 759 if (! spppq) 760 untimeout(sppp_keepalive, 0, keepalive_ch); 761 762 for (i = 0; i < IDX_COUNT; i++) 763 untimeout((cps[i])->TO, (void *)sp, sp->ch[i]); 764 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch); 765} 766 767/* 768 * Flush the interface output queue. 769 */ 770void 771sppp_flush(struct ifnet *ifp) 772{ 773 struct sppp *sp = (struct sppp*) ifp; 774 775 sppp_qflush (&sp->pp_if.if_snd); 776 sppp_qflush (&sp->pp_fastq); 777 sppp_qflush (&sp->pp_cpq); 778} 779 780/* 781 * Check if the output queue is empty. 782 */ 783int 784sppp_isempty(struct ifnet *ifp) 785{ 786 struct sppp *sp = (struct sppp*) ifp; 787 int empty, s; 788 789 s = splimp(); 790 empty = !sp->pp_fastq.ifq_head && !sp->pp_cpq.ifq_head && 791 !sp->pp_if.if_snd.ifq_head; 792 splx(s); 793 return (empty); 794} 795 796/* 797 * Get next packet to send. 798 */ 799struct mbuf * 800sppp_dequeue(struct ifnet *ifp) 801{ 802 struct sppp *sp = (struct sppp*) ifp; 803 struct mbuf *m; 804 int s; 805 806 s = splimp(); 807 /* 808 * Process only the control protocol queue until we have at 809 * least one NCP open. 810 * 811 * Do always serve all three queues in Cisco mode. 812 */ 813 IF_DEQUEUE(&sp->pp_cpq, m); 814 if (m == NULL && 815 (sppp_ncp_check(sp) || (sp->pp_flags & PP_CISCO) != 0)) { 816 IF_DEQUEUE(&sp->pp_fastq, m); 817 if (m == NULL) 818 IF_DEQUEUE (&sp->pp_if.if_snd, m); 819 } 820 splx(s); 821 return m; 822} 823 824/* 825 * Pick the next packet, do not remove it from the queue. 826 */ 827struct mbuf * 828sppp_pick(struct ifnet *ifp) 829{ 830 struct sppp *sp = (struct sppp*)ifp; 831 struct mbuf *m; 832 int s; 833 834 s= splimp (); 835 836 m = sp->pp_cpq.ifq_head; 837 if (m == NULL && 838 (sp->pp_phase == PHASE_NETWORK || 839 (sp->pp_flags & PP_CISCO) != 0)) 840 if ((m = sp->pp_fastq.ifq_head) == NULL) 841 m = sp->pp_if.if_snd.ifq_head; 842 splx (s); 843 return (m); 844} 845 846/* 847 * Process an ioctl request. Called on low priority level. 848 */ 849int 850sppp_ioctl(struct ifnet *ifp, int cmd, void *data) 851{ 852 struct ifreq *ifr = (struct ifreq*) data; 853 struct sppp *sp = (struct sppp*) ifp; 854 int s, rv, going_up, going_down, newmode; 855 856 s = splimp(); 857 rv = 0; 858 switch (cmd) { 859 case SIOCAIFADDR: 860 case SIOCSIFDSTADDR: 861 break; 862 863 case SIOCSIFADDR: 864 if_up(ifp); 865 /* fall through... */ 866 867 case SIOCSIFFLAGS: 868 going_up = ifp->if_flags & IFF_UP && 869 (ifp->if_flags & IFF_RUNNING) == 0; 870 going_down = (ifp->if_flags & IFF_UP) == 0 && 871 ifp->if_flags & IFF_RUNNING; 872 newmode = ifp->if_flags & (IFF_AUTO | IFF_PASSIVE); 873 if (newmode == (IFF_AUTO | IFF_PASSIVE)) { 874 /* sanity */ 875 newmode = IFF_PASSIVE; 876 ifp->if_flags &= ~IFF_AUTO; 877 } 878 879 if (going_up || going_down) 880 lcp.Close(sp); 881 if (going_up && newmode == 0) { 882 /* neither auto-dial nor passive */ 883 ifp->if_flags |= IFF_RUNNING; 884 if (!(sp->pp_flags & PP_CISCO)) 885 lcp.Open(sp); 886 } else if (going_down) { 887 sppp_flush(ifp); 888 ifp->if_flags &= ~IFF_RUNNING; 889 } 890 891 break; 892 893#ifdef SIOCSIFMTU 894#ifndef ifr_mtu 895#define ifr_mtu ifr_metric 896#endif 897 case SIOCSIFMTU: 898 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > sp->lcp.their_mru) 899 return (EINVAL); 900 ifp->if_mtu = ifr->ifr_mtu; 901 break; 902#endif 903#ifdef SLIOCSETMTU 904 case SLIOCSETMTU: 905 if (*(short*)data < 128 || *(short*)data > sp->lcp.their_mru) 906 return (EINVAL); 907 ifp->if_mtu = *(short*)data; 908 break; 909#endif 910#ifdef SIOCGIFMTU 911 case SIOCGIFMTU: 912 ifr->ifr_mtu = ifp->if_mtu; 913 break; 914#endif 915#ifdef SLIOCGETMTU 916 case SLIOCGETMTU: 917 *(short*)data = ifp->if_mtu; 918 break; 919#endif 920 case SIOCADDMULTI: 921 case SIOCDELMULTI: 922 break; 923 924 case SIOCGIFGENERIC: 925 case SIOCSIFGENERIC: 926 rv = sppp_params(sp, cmd, data); 927 break; 928 929 default: 930 rv = ENOTTY; 931 } 932 splx(s); 933 return rv; 934} 935 936 937/* 938 * Cisco framing implementation. 939 */ 940 941/* 942 * Handle incoming Cisco keepalive protocol packets. 943 */ 944static void 945sppp_cisco_input(struct sppp *sp, struct mbuf *m) 946{ 947 STDDCL; 948 struct cisco_packet *h; 949 u_long me, mymask; 950 951 if (m->m_pkthdr.len < CISCO_PACKET_LEN) { 952 if (debug) 953 log(LOG_DEBUG, 954 "%s%d: cisco invalid packet length: %d bytes\n", 955 ifp->if_name, ifp->if_unit, m->m_pkthdr.len); 956 return; 957 } 958 h = mtod (m, struct cisco_packet*); 959 if (debug) 960 log(LOG_DEBUG, 961 "%s%d: cisco input: %d bytes " 962 "<0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n", 963 ifp->if_name, ifp->if_unit, m->m_pkthdr.len, 964 ntohl (h->type), h->par1, h->par2, h->rel, 965 h->time0, h->time1); 966 switch (ntohl (h->type)) { 967 default: 968 if (debug) 969 addlog("%s%d: cisco unknown packet type: 0x%lx\n", 970 ifp->if_name, ifp->if_unit, ntohl (h->type)); 971 break; 972 case CISCO_ADDR_REPLY: 973 /* Reply on address request, ignore */ 974 break; 975 case CISCO_KEEPALIVE_REQ: 976 sp->pp_alivecnt = 0; 977 sp->pp_rseq = ntohl (h->par1); 978 if (sp->pp_seq == sp->pp_rseq) { 979 /* Local and remote sequence numbers are equal. 980 * Probably, the line is in loopback mode. */ 981 if (sp->pp_loopcnt >= MAXALIVECNT) { 982 printf ("%s%d: loopback\n", 983 ifp->if_name, ifp->if_unit); 984 sp->pp_loopcnt = 0; 985 if (ifp->if_flags & IFF_UP) { 986 if_down (ifp); 987 sppp_qflush (&sp->pp_cpq); 988 } 989 } 990 ++sp->pp_loopcnt; 991 992 /* Generate new local sequence number */ 993 read_random((char*)&sp->pp_seq, sizeof sp->pp_seq); 994 break; 995 } 996 sp->pp_loopcnt = 0; 997 if (! (ifp->if_flags & IFF_UP) && 998 (ifp->if_flags & IFF_RUNNING)) { 999 if_up(ifp); 1000 printf ("%s%d: up\n", ifp->if_name, ifp->if_unit); 1001 } 1002 break; 1003 case CISCO_ADDR_REQ: 1004 sppp_get_ip_addrs(sp, &me, 0, &mymask); 1005 if (me != 0L) 1006 sppp_cisco_send(sp, CISCO_ADDR_REPLY, me, mymask); 1007 break; 1008 } 1009} 1010 1011/* 1012 * Send Cisco keepalive packet. 1013 */ 1014static void 1015sppp_cisco_send(struct sppp *sp, int type, long par1, long par2) 1016{ 1017 STDDCL; 1018 struct ppp_header *h; 1019 struct cisco_packet *ch; 1020 struct mbuf *m; 1021 struct timeval tv; 1022 1023 getmicroruntime(&tv); 1024 1025 MGETHDR (m, M_DONTWAIT, MT_DATA); 1026 if (! m) 1027 return; 1028 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN; 1029 m->m_pkthdr.rcvif = 0; 1030 1031 h = mtod (m, struct ppp_header*); 1032 h->address = CISCO_MULTICAST; 1033 h->control = 0; 1034 h->protocol = htons (CISCO_KEEPALIVE); 1035 1036 ch = (struct cisco_packet*) (h + 1); 1037 ch->type = htonl (type); 1038 ch->par1 = htonl (par1); 1039 ch->par2 = htonl (par2); 1040 ch->rel = -1; 1041 ch->time0 = htons ((u_short) (tv.tv_sec >> 16)); 1042 ch->time1 = htons ((u_short) tv.tv_sec); 1043 1044 if (debug) 1045 log(LOG_DEBUG, 1046 "%s%d: cisco output: <0x%lx 0x%lx 0x%lx 0x%x 0x%x-0x%x>\n", 1047 ifp->if_name, ifp->if_unit, ntohl (ch->type), ch->par1, 1048 ch->par2, ch->rel, ch->time0, ch->time1); 1049 1050 if (IF_QFULL (&sp->pp_cpq)) { 1051 IF_DROP (&sp->pp_fastq); 1052 IF_DROP (&ifp->if_snd); 1053 m_freem (m); 1054 } else 1055 IF_ENQUEUE (&sp->pp_cpq, m); 1056 if (! (ifp->if_flags & IFF_OACTIVE)) 1057 (*ifp->if_start) (ifp); 1058 ifp->if_obytes += m->m_pkthdr.len + 3; 1059} 1060 1061/* 1062 * PPP protocol implementation. 1063 */ 1064 1065/* 1066 * Send PPP control protocol packet. 1067 */ 1068static void 1069sppp_cp_send(struct sppp *sp, u_short proto, u_char type, 1070 u_char ident, u_short len, void *data) 1071{ 1072 STDDCL; 1073 struct ppp_header *h; 1074 struct lcp_header *lh; 1075 struct mbuf *m; 1076 1077 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) 1078 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN; 1079 MGETHDR (m, M_DONTWAIT, MT_DATA); 1080 if (! m) 1081 return; 1082 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len; 1083 m->m_pkthdr.rcvif = 0; 1084 1085 h = mtod (m, struct ppp_header*); 1086 h->address = PPP_ALLSTATIONS; /* broadcast address */ 1087 h->control = PPP_UI; /* Unnumbered Info */ 1088 h->protocol = htons (proto); /* Link Control Protocol */ 1089 1090 lh = (struct lcp_header*) (h + 1); 1091 lh->type = type; 1092 lh->ident = ident; 1093 lh->len = htons (LCP_HEADER_LEN + len); 1094 if (len) 1095 bcopy (data, lh+1, len); 1096 1097 if (debug) { 1098 log(LOG_DEBUG, "%s%d: %s output <%s id=0x%x len=%d", 1099 ifp->if_name, ifp->if_unit, 1100 sppp_proto_name(proto), 1101 sppp_cp_type_name (lh->type), lh->ident, 1102 ntohs (lh->len)); 1103 if (len) 1104 sppp_print_bytes ((u_char*) (lh+1), len); 1105 addlog(">\n"); 1106 } 1107 if (IF_QFULL (&sp->pp_cpq)) { 1108 IF_DROP (&sp->pp_fastq); 1109 IF_DROP (&ifp->if_snd); 1110 m_freem (m); 1111 ++ifp->if_oerrors; 1112 } else 1113 IF_ENQUEUE (&sp->pp_cpq, m); 1114 if (! (ifp->if_flags & IFF_OACTIVE)) 1115 (*ifp->if_start) (ifp); 1116 ifp->if_obytes += m->m_pkthdr.len + 3; 1117} 1118 1119/* 1120 * Handle incoming PPP control protocol packets. 1121 */ 1122static void 1123sppp_cp_input(const struct cp *cp, struct sppp *sp, struct mbuf *m) 1124{ 1125 STDDCL; 1126 struct lcp_header *h; 1127 int len = m->m_pkthdr.len; 1128 int rv; 1129 u_char *p; 1130 1131 if (len < 4) { 1132 if (debug) 1133 log(LOG_DEBUG, 1134 "%s%d: %s invalid packet length: %d bytes\n", 1135 ifp->if_name, ifp->if_unit, cp->name, len); 1136 return; 1137 } 1138 h = mtod (m, struct lcp_header*); 1139 if (debug) { 1140 log(LOG_DEBUG, 1141 "%s%d: %s input(%s): <%s id=0x%x len=%d", 1142 ifp->if_name, ifp->if_unit, cp->name, 1143 sppp_state_name(sp->state[cp->protoidx]), 1144 sppp_cp_type_name (h->type), h->ident, ntohs (h->len)); 1145 if (len > 4) 1146 sppp_print_bytes ((u_char*) (h+1), len-4); 1147 addlog(">\n"); 1148 } 1149 if (len > ntohs (h->len)) 1150 len = ntohs (h->len); 1151 p = (u_char *)(h + 1); 1152 switch (h->type) { 1153 case CONF_REQ: 1154 if (len < 4) { 1155 if (debug) 1156 addlog("%s%d: %s invalid conf-req length %d\n", 1157 ifp->if_name, ifp->if_unit, cp->name, 1158 len); 1159 ++ifp->if_ierrors; 1160 break; 1161 } 1162 /* handle states where RCR doesn't get a SCA/SCN */ 1163 switch (sp->state[cp->protoidx]) { 1164 case STATE_CLOSING: 1165 case STATE_STOPPING: 1166 return; 1167 case STATE_CLOSED: 1168 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 1169 0, 0); 1170 return; 1171 } 1172 rv = (cp->RCR)(sp, h, len); 1173 switch (sp->state[cp->protoidx]) { 1174 case STATE_OPENED: 1175 (cp->tld)(sp); 1176 (cp->scr)(sp); 1177 /* fall through... */ 1178 case STATE_ACK_SENT: 1179 case STATE_REQ_SENT: 1180 sppp_cp_change_state(cp, sp, rv? 1181 STATE_ACK_SENT: STATE_REQ_SENT); 1182 break; 1183 case STATE_STOPPED: 1184 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure; 1185 (cp->scr)(sp); 1186 sppp_cp_change_state(cp, sp, rv? 1187 STATE_ACK_SENT: STATE_REQ_SENT); 1188 break; 1189 case STATE_ACK_RCVD: 1190 if (rv) { 1191 sppp_cp_change_state(cp, sp, STATE_OPENED); 1192 if (debug) 1193 log(LOG_DEBUG, "%s%d: %s tlu\n", 1194 ifp->if_name, ifp->if_unit, 1195 cp->name); 1196 (cp->tlu)(sp); 1197 } else 1198 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD); 1199 break; 1200 default: 1201 printf("%s%d: %s illegal %s in state %s\n", 1202 ifp->if_name, ifp->if_unit, cp->name, 1203 sppp_cp_type_name(h->type), 1204 sppp_state_name(sp->state[cp->protoidx])); 1205 ++ifp->if_ierrors; 1206 } 1207 break; 1208 case CONF_ACK: 1209 if (h->ident != sp->confid[cp->protoidx]) { 1210 if (debug) 1211 addlog("%s%d: %s id mismatch 0x%x != 0x%x\n", 1212 ifp->if_name, ifp->if_unit, cp->name, 1213 h->ident, sp->confid[cp->protoidx]); 1214 ++ifp->if_ierrors; 1215 break; 1216 } 1217 switch (sp->state[cp->protoidx]) { 1218 case STATE_CLOSED: 1219 case STATE_STOPPED: 1220 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0); 1221 break; 1222 case STATE_CLOSING: 1223 case STATE_STOPPING: 1224 break; 1225 case STATE_REQ_SENT: 1226 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure; 1227 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD); 1228 break; 1229 case STATE_OPENED: 1230 (cp->tld)(sp); 1231 /* fall through */ 1232 case STATE_ACK_RCVD: 1233 (cp->scr)(sp); 1234 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1235 break; 1236 case STATE_ACK_SENT: 1237 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure; 1238 sppp_cp_change_state(cp, sp, STATE_OPENED); 1239 if (debug) 1240 log(LOG_DEBUG, "%s%d: %s tlu\n", 1241 ifp->if_name, ifp->if_unit, cp->name); 1242 (cp->tlu)(sp); 1243 break; 1244 default: 1245 printf("%s%d: %s illegal %s in state %s\n", 1246 ifp->if_name, ifp->if_unit, cp->name, 1247 sppp_cp_type_name(h->type), 1248 sppp_state_name(sp->state[cp->protoidx])); 1249 ++ifp->if_ierrors; 1250 } 1251 break; 1252 case CONF_NAK: 1253 case CONF_REJ: 1254 if (h->ident != sp->confid[cp->protoidx]) { 1255 if (debug) 1256 addlog("%s%d: %s id mismatch 0x%x != 0x%x\n", 1257 ifp->if_name, ifp->if_unit, cp->name, 1258 h->ident, sp->confid[cp->protoidx]); 1259 ++ifp->if_ierrors; 1260 break; 1261 } 1262 if (h->type == CONF_NAK) 1263 (cp->RCN_nak)(sp, h, len); 1264 else /* CONF_REJ */ 1265 (cp->RCN_rej)(sp, h, len); 1266 1267 switch (sp->state[cp->protoidx]) { 1268 case STATE_CLOSED: 1269 case STATE_STOPPED: 1270 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0); 1271 break; 1272 case STATE_REQ_SENT: 1273 case STATE_ACK_SENT: 1274 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure; 1275 (cp->scr)(sp); 1276 break; 1277 case STATE_OPENED: 1278 (cp->tld)(sp); 1279 /* fall through */ 1280 case STATE_ACK_RCVD: 1281 sppp_cp_change_state(cp, sp, STATE_ACK_SENT); 1282 (cp->scr)(sp); 1283 break; 1284 case STATE_CLOSING: 1285 case STATE_STOPPING: 1286 break; 1287 default: 1288 printf("%s%d: %s illegal %s in state %s\n", 1289 ifp->if_name, ifp->if_unit, cp->name, 1290 sppp_cp_type_name(h->type), 1291 sppp_state_name(sp->state[cp->protoidx])); 1292 ++ifp->if_ierrors; 1293 } 1294 break; 1295 1296 case TERM_REQ: 1297 switch (sp->state[cp->protoidx]) { 1298 case STATE_ACK_RCVD: 1299 case STATE_ACK_SENT: 1300 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1301 /* fall through */ 1302 case STATE_CLOSED: 1303 case STATE_STOPPED: 1304 case STATE_CLOSING: 1305 case STATE_STOPPING: 1306 case STATE_REQ_SENT: 1307 sta: 1308 /* Send Terminate-Ack packet. */ 1309 if (debug) 1310 log(LOG_DEBUG, "%s%d: %s send terminate-ack\n", 1311 ifp->if_name, ifp->if_unit, cp->name); 1312 sppp_cp_send(sp, cp->proto, TERM_ACK, h->ident, 0, 0); 1313 break; 1314 case STATE_OPENED: 1315 (cp->tld)(sp); 1316 sp->rst_counter[cp->protoidx] = 0; 1317 sppp_cp_change_state(cp, sp, STATE_STOPPING); 1318 goto sta; 1319 break; 1320 default: 1321 printf("%s%d: %s illegal %s in state %s\n", 1322 ifp->if_name, ifp->if_unit, cp->name, 1323 sppp_cp_type_name(h->type), 1324 sppp_state_name(sp->state[cp->protoidx])); 1325 ++ifp->if_ierrors; 1326 } 1327 break; 1328 case TERM_ACK: 1329 switch (sp->state[cp->protoidx]) { 1330 case STATE_CLOSED: 1331 case STATE_STOPPED: 1332 case STATE_REQ_SENT: 1333 case STATE_ACK_SENT: 1334 break; 1335 case STATE_CLOSING: 1336 (cp->tlf)(sp); 1337 sppp_cp_change_state(cp, sp, STATE_CLOSED); 1338 break; 1339 case STATE_STOPPING: 1340 (cp->tlf)(sp); 1341 sppp_cp_change_state(cp, sp, STATE_STOPPED); 1342 break; 1343 case STATE_ACK_RCVD: 1344 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1345 break; 1346 case STATE_OPENED: 1347 (cp->tld)(sp); 1348 (cp->scr)(sp); 1349 sppp_cp_change_state(cp, sp, STATE_ACK_RCVD); 1350 break; 1351 default: 1352 printf("%s%d: %s illegal %s in state %s\n", 1353 ifp->if_name, ifp->if_unit, cp->name, 1354 sppp_cp_type_name(h->type), 1355 sppp_state_name(sp->state[cp->protoidx])); 1356 ++ifp->if_ierrors; 1357 } 1358 break; 1359 case CODE_REJ: 1360 case PROTO_REJ: 1361 /* XXX catastrophic rejects (RXJ-) aren't handled yet. */ 1362 log(LOG_INFO, 1363 "%s%d: %s: ignoring RXJ (%s) for proto 0x%x, " 1364 "danger will robinson\n", 1365 ifp->if_name, ifp->if_unit, cp->name, 1366 sppp_cp_type_name(h->type), ntohs(*((u_short *)p))); 1367 switch (sp->state[cp->protoidx]) { 1368 case STATE_CLOSED: 1369 case STATE_STOPPED: 1370 case STATE_REQ_SENT: 1371 case STATE_ACK_SENT: 1372 case STATE_CLOSING: 1373 case STATE_STOPPING: 1374 case STATE_OPENED: 1375 break; 1376 case STATE_ACK_RCVD: 1377 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1378 break; 1379 default: 1380 printf("%s%d: %s illegal %s in state %s\n", 1381 ifp->if_name, ifp->if_unit, cp->name, 1382 sppp_cp_type_name(h->type), 1383 sppp_state_name(sp->state[cp->protoidx])); 1384 ++ifp->if_ierrors; 1385 } 1386 break; 1387 case DISC_REQ: 1388 if (cp->proto != PPP_LCP) 1389 goto illegal; 1390 /* Discard the packet. */ 1391 break; 1392 case ECHO_REQ: 1393 if (cp->proto != PPP_LCP) 1394 goto illegal; 1395 if (sp->state[cp->protoidx] != STATE_OPENED) { 1396 if (debug) 1397 addlog("%s%d: lcp echo req but lcp closed\n", 1398 ifp->if_name, ifp->if_unit); 1399 ++ifp->if_ierrors; 1400 break; 1401 } 1402 if (len < 8) { 1403 if (debug) 1404 addlog("%s%d: invalid lcp echo request " 1405 "packet length: %d bytes\n", 1406 ifp->if_name, ifp->if_unit, len); 1407 break; 1408 } 1409 if (ntohl (*(long*)(h+1)) == sp->lcp.magic) { 1410 /* Line loopback mode detected. */ 1411 printf("%s%d: loopback\n", ifp->if_name, ifp->if_unit); 1412 if_down (ifp); 1413 sppp_qflush (&sp->pp_cpq); 1414 1415 /* Shut down the PPP link. */ 1416 /* XXX */ 1417 lcp.Down(sp); 1418 lcp.Up(sp); 1419 break; 1420 } 1421 *(long*)(h+1) = htonl (sp->lcp.magic); 1422 if (debug) 1423 addlog("%s%d: got lcp echo req, sending echo rep\n", 1424 ifp->if_name, ifp->if_unit); 1425 sppp_cp_send (sp, PPP_LCP, ECHO_REPLY, h->ident, len-4, h+1); 1426 break; 1427 case ECHO_REPLY: 1428 if (cp->proto != PPP_LCP) 1429 goto illegal; 1430 if (h->ident != sp->lcp.echoid) { 1431 ++ifp->if_ierrors; 1432 break; 1433 } 1434 if (len < 8) { 1435 if (debug) 1436 addlog("%s%d: lcp invalid echo reply " 1437 "packet length: %d bytes\n", 1438 ifp->if_name, ifp->if_unit, len); 1439 break; 1440 } 1441 if (debug) 1442 addlog("%s%d: lcp got echo rep\n", 1443 ifp->if_name, ifp->if_unit); 1444 if (ntohl (*(long*)(h+1)) != sp->lcp.magic) 1445 sp->pp_alivecnt = 0; 1446 break; 1447 default: 1448 /* Unknown packet type -- send Code-Reject packet. */ 1449 illegal: 1450 if (debug) 1451 addlog("%s%d: %c send code-rej for 0x%x\n", 1452 ifp->if_name, ifp->if_unit, cp->name, h->type); 1453 sppp_cp_send(sp, cp->proto, CODE_REJ, ++sp->pp_seq, 1454 m->m_pkthdr.len, h); 1455 ++ifp->if_ierrors; 1456 } 1457} 1458 1459 1460/* 1461 * The generic part of all Up/Down/Open/Close/TO event handlers. 1462 * Basically, the state transition handling in the automaton. 1463 */ 1464static void 1465sppp_up_event(const struct cp *cp, struct sppp *sp) 1466{ 1467 STDDCL; 1468 1469 if (debug) 1470 log(LOG_DEBUG, "%s%d: %s up(%s)\n", 1471 ifp->if_name, ifp->if_unit, cp->name, 1472 sppp_state_name(sp->state[cp->protoidx])); 1473 1474 switch (sp->state[cp->protoidx]) { 1475 case STATE_INITIAL: 1476 sppp_cp_change_state(cp, sp, STATE_CLOSED); 1477 break; 1478 case STATE_STARTING: 1479 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure; 1480 (cp->scr)(sp); 1481 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1482 break; 1483 default: 1484 printf("%s%d: %s illegal up in state %s\n", 1485 ifp->if_name, ifp->if_unit, cp->name, 1486 sppp_state_name(sp->state[cp->protoidx])); 1487 } 1488} 1489 1490static void 1491sppp_down_event(const struct cp *cp, struct sppp *sp) 1492{ 1493 STDDCL; 1494 1495 if (debug) 1496 log(LOG_DEBUG, "%s%d: %s down(%s)\n", 1497 ifp->if_name, ifp->if_unit, cp->name, 1498 sppp_state_name(sp->state[cp->protoidx])); 1499 1500 switch (sp->state[cp->protoidx]) { 1501 case STATE_CLOSED: 1502 case STATE_CLOSING: 1503 sppp_cp_change_state(cp, sp, STATE_INITIAL); 1504 break; 1505 case STATE_STOPPED: 1506 (cp->tls)(sp); 1507 /* fall through */ 1508 case STATE_STOPPING: 1509 case STATE_REQ_SENT: 1510 case STATE_ACK_RCVD: 1511 case STATE_ACK_SENT: 1512 sppp_cp_change_state(cp, sp, STATE_STARTING); 1513 break; 1514 case STATE_OPENED: 1515 (cp->tld)(sp); 1516 sppp_cp_change_state(cp, sp, STATE_STARTING); 1517 break; 1518 default: 1519 printf("%s%d: %s illegal down in state %s\n", 1520 ifp->if_name, ifp->if_unit, cp->name, 1521 sppp_state_name(sp->state[cp->protoidx])); 1522 } 1523} 1524 1525 1526static void 1527sppp_open_event(const struct cp *cp, struct sppp *sp) 1528{ 1529 STDDCL; 1530 1531 if (debug) 1532 log(LOG_DEBUG, "%s%d: %s open(%s)\n", 1533 ifp->if_name, ifp->if_unit, cp->name, 1534 sppp_state_name(sp->state[cp->protoidx])); 1535 1536 switch (sp->state[cp->protoidx]) { 1537 case STATE_INITIAL: 1538 (cp->tls)(sp); 1539 sppp_cp_change_state(cp, sp, STATE_STARTING); 1540 break; 1541 case STATE_STARTING: 1542 break; 1543 case STATE_CLOSED: 1544 sp->rst_counter[cp->protoidx] = sp->lcp.max_configure; 1545 (cp->scr)(sp); 1546 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1547 break; 1548 case STATE_STOPPED: 1549 case STATE_STOPPING: 1550 case STATE_REQ_SENT: 1551 case STATE_ACK_RCVD: 1552 case STATE_ACK_SENT: 1553 case STATE_OPENED: 1554 break; 1555 case STATE_CLOSING: 1556 sppp_cp_change_state(cp, sp, STATE_STOPPING); 1557 break; 1558 } 1559} 1560 1561 1562static void 1563sppp_close_event(const struct cp *cp, struct sppp *sp) 1564{ 1565 STDDCL; 1566 1567 if (debug) 1568 log(LOG_DEBUG, "%s%d: %s close(%s)\n", 1569 ifp->if_name, ifp->if_unit, cp->name, 1570 sppp_state_name(sp->state[cp->protoidx])); 1571 1572 switch (sp->state[cp->protoidx]) { 1573 case STATE_INITIAL: 1574 case STATE_CLOSED: 1575 case STATE_CLOSING: 1576 break; 1577 case STATE_STARTING: 1578 (cp->tlf)(sp); 1579 sppp_cp_change_state(cp, sp, STATE_INITIAL); 1580 break; 1581 case STATE_STOPPED: 1582 sppp_cp_change_state(cp, sp, STATE_CLOSED); 1583 break; 1584 case STATE_STOPPING: 1585 sppp_cp_change_state(cp, sp, STATE_CLOSING); 1586 break; 1587 case STATE_OPENED: 1588 (cp->tld)(sp); 1589 /* fall through */ 1590 case STATE_REQ_SENT: 1591 case STATE_ACK_RCVD: 1592 case STATE_ACK_SENT: 1593 sp->rst_counter[cp->protoidx] = sp->lcp.max_terminate; 1594 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq, 0, 0); 1595 sppp_cp_change_state(cp, sp, STATE_CLOSING); 1596 break; 1597 } 1598} 1599 1600static void 1601sppp_to_event(const struct cp *cp, struct sppp *sp) 1602{ 1603 STDDCL; 1604 int s; 1605 1606 s = splimp(); 1607 if (debug) 1608 log(LOG_DEBUG, "%s%d: %s TO(%s) rst_counter = %d\n", 1609 ifp->if_name, ifp->if_unit, cp->name, 1610 sppp_state_name(sp->state[cp->protoidx]), 1611 sp->rst_counter[cp->protoidx]); 1612 1613 if (--sp->rst_counter[cp->protoidx] < 0) 1614 /* TO- event */ 1615 switch (sp->state[cp->protoidx]) { 1616 case STATE_CLOSING: 1617 (cp->tlf)(sp); 1618 sppp_cp_change_state(cp, sp, STATE_CLOSED); 1619 break; 1620 case STATE_STOPPING: 1621 (cp->tlf)(sp); 1622 sppp_cp_change_state(cp, sp, STATE_STOPPED); 1623 break; 1624 case STATE_REQ_SENT: 1625 case STATE_ACK_RCVD: 1626 case STATE_ACK_SENT: 1627 (cp->tlf)(sp); 1628 sppp_cp_change_state(cp, sp, STATE_STOPPED); 1629 break; 1630 } 1631 else 1632 /* TO+ event */ 1633 switch (sp->state[cp->protoidx]) { 1634 case STATE_CLOSING: 1635 case STATE_STOPPING: 1636 sppp_cp_send(sp, cp->proto, TERM_REQ, ++sp->pp_seq, 1637 0, 0); 1638 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp, 1639 sp->lcp.timeout); 1640 break; 1641 case STATE_REQ_SENT: 1642 case STATE_ACK_RCVD: 1643 (cp->scr)(sp); 1644 /* sppp_cp_change_state() will restart the timer */ 1645 sppp_cp_change_state(cp, sp, STATE_REQ_SENT); 1646 break; 1647 case STATE_ACK_SENT: 1648 (cp->scr)(sp); 1649 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp, 1650 sp->lcp.timeout); 1651 break; 1652 } 1653 1654 splx(s); 1655} 1656 1657/* 1658 * Change the state of a control protocol in the state automaton. 1659 * Takes care of starting/stopping the restart timer. 1660 */ 1661void 1662sppp_cp_change_state(const struct cp *cp, struct sppp *sp, int newstate) 1663{ 1664 sp->state[cp->protoidx] = newstate; 1665 1666 untimeout(cp->TO, (void *)sp, sp->ch[cp->protoidx]); 1667 switch (newstate) { 1668 case STATE_INITIAL: 1669 case STATE_STARTING: 1670 case STATE_CLOSED: 1671 case STATE_STOPPED: 1672 case STATE_OPENED: 1673 break; 1674 case STATE_CLOSING: 1675 case STATE_STOPPING: 1676 case STATE_REQ_SENT: 1677 case STATE_ACK_RCVD: 1678 case STATE_ACK_SENT: 1679 sp->ch[cp->protoidx] = timeout(cp->TO, (void *)sp, 1680 sp->lcp.timeout); 1681 break; 1682 } 1683} 1684/* 1685 *--------------------------------------------------------------------------* 1686 * * 1687 * The LCP implementation. * 1688 * * 1689 *--------------------------------------------------------------------------* 1690 */ 1691static void 1692sppp_lcp_init(struct sppp *sp) 1693{ 1694 sp->lcp.opts = (1 << LCP_OPT_MAGIC); 1695 sp->lcp.magic = 0; 1696 sp->state[IDX_LCP] = STATE_INITIAL; 1697 sp->fail_counter[IDX_LCP] = 0; 1698 sp->lcp.protos = 0; 1699 sp->lcp.mru = sp->lcp.their_mru = PP_MTU; 1700 1701 /* 1702 * Initialize counters and timeout values. Note that we don't 1703 * use the 3 seconds suggested in RFC 1661 since we are likely 1704 * running on a fast link. XXX We should probably implement 1705 * the exponential backoff option. Note that these values are 1706 * relevant for all control protocols, not just LCP only. 1707 */ 1708 sp->lcp.timeout = 1 * hz; 1709 sp->lcp.max_terminate = 2; 1710 sp->lcp.max_configure = 10; 1711 sp->lcp.max_failure = 10; 1712 callout_handle_init(&sp->ch[IDX_LCP]); 1713} 1714 1715static void 1716sppp_lcp_up(struct sppp *sp) 1717{ 1718 STDDCL; 1719 1720 /* 1721 * If this interface is passive or dial-on-demand, and we are 1722 * still in Initial state, it means we've got an incoming 1723 * call. Activate the interface. 1724 */ 1725 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) != 0) { 1726 if (debug) 1727 log(LOG_DEBUG, 1728 "%s%d: Up event", ifp->if_name, ifp->if_unit); 1729 ifp->if_flags |= IFF_RUNNING; 1730 if (sp->state[IDX_LCP] == STATE_INITIAL) { 1731 if (debug) 1732 addlog("(incoming call)\n"); 1733 sp->pp_flags |= PP_CALLIN; 1734 lcp.Open(sp); 1735 } else if (debug) 1736 addlog("\n"); 1737 } 1738 1739 sppp_up_event(&lcp, sp); 1740} 1741 1742static void 1743sppp_lcp_down(struct sppp *sp) 1744{ 1745 STDDCL; 1746 1747 sppp_down_event(&lcp, sp); 1748 1749 /* 1750 * If this is neither a dial-on-demand nor a passive 1751 * interface, simulate an ``ifconfig down'' action, so the 1752 * administrator can force a redial by another ``ifconfig 1753 * up''. XXX For leased line operation, should we immediately 1754 * try to reopen the connection here? 1755 */ 1756 if ((ifp->if_flags & (IFF_AUTO | IFF_PASSIVE)) == 0) { 1757 log(LOG_INFO, 1758 "%s%d: Down event (carrier loss), taking interface down.\n", 1759 ifp->if_name, ifp->if_unit); 1760 if_down(ifp); 1761 } else { 1762 if (debug) 1763 log(LOG_DEBUG, 1764 "%s%d: Down event (carrier loss)\n", 1765 ifp->if_name, ifp->if_unit); 1766 } 1767 sp->pp_flags &= ~PP_CALLIN; 1768 if (sp->state[IDX_LCP] != STATE_INITIAL) 1769 lcp.Close(sp); 1770 ifp->if_flags &= ~IFF_RUNNING; 1771} 1772 1773static void 1774sppp_lcp_open(struct sppp *sp) 1775{ 1776 /* 1777 * If we are authenticator, negotiate LCP_AUTH 1778 */ 1779 if (sp->hisauth.proto != 0) 1780 sp->lcp.opts |= (1 << LCP_OPT_AUTH_PROTO); 1781 else 1782 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO); 1783 sp->pp_flags &= ~PP_NEEDAUTH; 1784 sppp_open_event(&lcp, sp); 1785} 1786 1787static void 1788sppp_lcp_close(struct sppp *sp) 1789{ 1790 sppp_close_event(&lcp, sp); 1791} 1792 1793static void 1794sppp_lcp_TO(void *cookie) 1795{ 1796 sppp_to_event(&lcp, (struct sppp *)cookie); 1797} 1798 1799/* 1800 * Analyze a configure request. Return true if it was agreeable, and 1801 * caused action sca, false if it has been rejected or nak'ed, and 1802 * caused action scn. (The return value is used to make the state 1803 * transition decision in the state automaton.) 1804 */ 1805static int 1806sppp_lcp_RCR(struct sppp *sp, struct lcp_header *h, int len) 1807{ 1808 STDDCL; 1809 u_char *buf, *r, *p; 1810 int origlen, rlen; 1811 u_long nmagic; 1812 u_short authproto; 1813 1814 len -= 4; 1815 origlen = len; 1816 buf = r = malloc (len, M_TEMP, M_NOWAIT); 1817 if (! buf) 1818 return (0); 1819 1820 if (debug) 1821 log(LOG_DEBUG, "%s%d: lcp parse opts: ", 1822 ifp->if_name, ifp->if_unit); 1823 1824 /* pass 1: check for things that need to be rejected */ 1825 p = (void*) (h+1); 1826 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) { 1827 if (debug) 1828 addlog(" %s ", sppp_lcp_opt_name(*p)); 1829 switch (*p) { 1830 case LCP_OPT_MAGIC: 1831 /* Magic number. */ 1832 /* fall through, both are same length */ 1833 case LCP_OPT_ASYNC_MAP: 1834 /* Async control character map. */ 1835 if (len >= 6 || p[1] == 6) 1836 continue; 1837 if (debug) 1838 addlog("[invalid] "); 1839 break; 1840 case LCP_OPT_MRU: 1841 /* Maximum receive unit. */ 1842 if (len >= 4 && p[1] == 4) 1843 continue; 1844 if (debug) 1845 addlog("[invalid] "); 1846 break; 1847 case LCP_OPT_AUTH_PROTO: 1848 if (len < 4) { 1849 if (debug) 1850 addlog("[invalid] "); 1851 break; 1852 } 1853 authproto = (p[2] << 8) + p[3]; 1854 if (authproto == PPP_CHAP && p[1] != 5) { 1855 if (debug) 1856 addlog("[invalid chap len] "); 1857 break; 1858 } 1859 if (sp->myauth.proto == 0) { 1860 /* we are not configured to do auth */ 1861 if (debug) 1862 addlog("[not configured] "); 1863 break; 1864 } 1865 /* 1866 * Remote want us to authenticate, remember this, 1867 * so we stay in PHASE_AUTHENTICATE after LCP got 1868 * up. 1869 */ 1870 sp->pp_flags |= PP_NEEDAUTH; 1871 continue; 1872 default: 1873 /* Others not supported. */ 1874 if (debug) 1875 addlog("[rej] "); 1876 break; 1877 } 1878 /* Add the option to rejected list. */ 1879 bcopy (p, r, p[1]); 1880 r += p[1]; 1881 rlen += p[1]; 1882 } 1883 if (rlen) { 1884 if (debug) 1885 addlog(" send conf-rej\n"); 1886 sppp_cp_send (sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf); 1887 return 0; 1888 } else if (debug) 1889 addlog("\n"); 1890 1891 /* 1892 * pass 2: check for option values that are unacceptable and 1893 * thus require to be nak'ed. 1894 */ 1895 if (debug) 1896 log(LOG_DEBUG, "%s%d: lcp parse opt values: ", 1897 ifp->if_name, ifp->if_unit); 1898 1899 p = (void*) (h+1); 1900 len = origlen; 1901 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) { 1902 if (debug) 1903 addlog(" %s ", sppp_lcp_opt_name(*p)); 1904 switch (*p) { 1905 case LCP_OPT_MAGIC: 1906 /* Magic number -- extract. */ 1907 nmagic = (u_long)p[2] << 24 | 1908 (u_long)p[3] << 16 | p[4] << 8 | p[5]; 1909 if (nmagic != sp->lcp.magic) { 1910 if (debug) 1911 addlog("0x%x ", nmagic); 1912 continue; 1913 } 1914 /* 1915 * Local and remote magics equal -- loopback? 1916 */ 1917 if (sp->pp_loopcnt >= MAXALIVECNT*5) { 1918 printf ("%s%d: loopback\n", 1919 ifp->if_name, ifp->if_unit); 1920 sp->pp_loopcnt = 0; 1921 if (ifp->if_flags & IFF_UP) { 1922 if_down(ifp); 1923 sppp_qflush(&sp->pp_cpq); 1924 /* XXX ? */ 1925 lcp.Down(sp); 1926 lcp.Up(sp); 1927 } 1928 } else if (debug) 1929 addlog("[glitch] "); 1930 ++sp->pp_loopcnt; 1931 /* 1932 * We negate our magic here, and NAK it. If 1933 * we see it later in an NAK packet, we 1934 * suggest a new one. 1935 */ 1936 nmagic = ~sp->lcp.magic; 1937 /* Gonna NAK it. */ 1938 p[2] = nmagic >> 24; 1939 p[3] = nmagic >> 16; 1940 p[4] = nmagic >> 8; 1941 p[5] = nmagic; 1942 break; 1943 1944 case LCP_OPT_ASYNC_MAP: 1945 /* Async control character map -- check to be zero. */ 1946 if (! p[2] && ! p[3] && ! p[4] && ! p[5]) { 1947 if (debug) 1948 addlog("[empty] "); 1949 continue; 1950 } 1951 if (debug) 1952 addlog("[non-empty] "); 1953 /* suggest a zero one */ 1954 p[2] = p[3] = p[4] = p[5] = 0; 1955 break; 1956 1957 case LCP_OPT_MRU: 1958 /* 1959 * Maximum receive unit. Always agreeable, 1960 * but ignored by now. 1961 */ 1962 sp->lcp.their_mru = p[2] * 256 + p[3]; 1963 if (debug) 1964 addlog("%d ", sp->lcp.their_mru); 1965 continue; 1966 1967 case LCP_OPT_AUTH_PROTO: 1968 authproto = (p[2] << 8) + p[3]; 1969 if (sp->myauth.proto != authproto) { 1970 /* not agreed, nak */ 1971 if (debug) 1972 addlog("[mine %s != his %s] ", 1973 sppp_proto_name(sp->hisauth.proto), 1974 sppp_proto_name(authproto)); 1975 p[2] = sp->myauth.proto >> 8; 1976 p[3] = sp->myauth.proto; 1977 break; 1978 } 1979 if (authproto == PPP_CHAP && p[4] != CHAP_MD5) { 1980 if (debug) 1981 addlog("[chap not MD5] "); 1982 p[4] == CHAP_MD5; 1983 break; 1984 } 1985 continue; 1986 } 1987 /* Add the option to nak'ed list. */ 1988 bcopy (p, r, p[1]); 1989 r += p[1]; 1990 rlen += p[1]; 1991 } 1992 if (rlen) { 1993 if (++sp->fail_counter[IDX_LCP] >= sp->lcp.max_failure) { 1994 if (debug) 1995 addlog(" max_failure (%d) exceeded, " 1996 "send conf-rej\n", 1997 sp->lcp.max_failure); 1998 sppp_cp_send(sp, PPP_LCP, CONF_REJ, h->ident, rlen, buf); 1999 } else { 2000 if (debug) 2001 addlog(" send conf-nak\n"); 2002 sppp_cp_send (sp, PPP_LCP, CONF_NAK, h->ident, rlen, buf); 2003 } 2004 return 0; 2005 } else { 2006 if (debug) 2007 addlog(" send conf-ack\n"); 2008 sp->fail_counter[IDX_LCP] = 0; 2009 sp->pp_loopcnt = 0; 2010 sppp_cp_send (sp, PPP_LCP, CONF_ACK, 2011 h->ident, origlen, h+1); 2012 } 2013 2014 free (buf, M_TEMP); 2015 return (rlen == 0); 2016} 2017 2018/* 2019 * Analyze the LCP Configure-Reject option list, and adjust our 2020 * negotiation. 2021 */ 2022static void 2023sppp_lcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len) 2024{ 2025 STDDCL; 2026 u_char *buf, *p; 2027 2028 len -= 4; 2029 buf = malloc (len, M_TEMP, M_NOWAIT); 2030 if (!buf) 2031 return; 2032 2033 if (debug) 2034 log(LOG_DEBUG, "%s%d: lcp rej opts: ", 2035 ifp->if_name, ifp->if_unit); 2036 2037 p = (void*) (h+1); 2038 for (; len > 1 && p[1]; len -= p[1], p += p[1]) { 2039 if (debug) 2040 addlog(" %s ", sppp_lcp_opt_name(*p)); 2041 switch (*p) { 2042 case LCP_OPT_MAGIC: 2043 /* Magic number -- can't use it, use 0 */ 2044 sp->lcp.opts &= ~(1 << LCP_OPT_MAGIC); 2045 sp->lcp.magic = 0; 2046 break; 2047 case LCP_OPT_MRU: 2048 /* 2049 * Should not be rejected anyway, since we only 2050 * negotiate a MRU if explicitly requested by 2051 * peer. 2052 */ 2053 sp->lcp.opts &= ~(1 << LCP_OPT_MRU); 2054 break; 2055 case LCP_OPT_AUTH_PROTO: 2056 /* 2057 * Peer doesn't want to authenticate himself, 2058 * deny unless this is a dialout call, and 2059 * AUTHFLAG_NOCALLOUT is set. 2060 */ 2061 if ((sp->pp_flags & PP_CALLIN) == 0 && 2062 (sp->hisauth.flags & AUTHFLAG_NOCALLOUT) != 0) { 2063 if (debug) 2064 addlog("[don't insist on auth " 2065 "for callout]"); 2066 sp->lcp.opts &= ~(1 << LCP_OPT_AUTH_PROTO); 2067 break; 2068 } 2069 if (debug) 2070 addlog("[access denied]\n"); 2071 lcp.Close(sp); 2072 break; 2073 } 2074 } 2075 if (debug) 2076 addlog("\n"); 2077 free (buf, M_TEMP); 2078 return; 2079} 2080 2081/* 2082 * Analyze the LCP Configure-NAK option list, and adjust our 2083 * negotiation. 2084 */ 2085static void 2086sppp_lcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len) 2087{ 2088 STDDCL; 2089 u_char *buf, *p; 2090 u_long magic; 2091 2092 len -= 4; 2093 buf = malloc (len, M_TEMP, M_NOWAIT); 2094 if (!buf) 2095 return; 2096 2097 if (debug) 2098 log(LOG_DEBUG, "%s%d: lcp nak opts: ", 2099 ifp->if_name, ifp->if_unit); 2100 2101 p = (void*) (h+1); 2102 for (; len > 1 && p[1]; len -= p[1], p += p[1]) { 2103 if (debug) 2104 addlog(" %s ", sppp_lcp_opt_name(*p)); 2105 switch (*p) { 2106 case LCP_OPT_MAGIC: 2107 /* Magic number -- renegotiate */ 2108 if ((sp->lcp.opts & (1 << LCP_OPT_MAGIC)) && 2109 len >= 6 && p[1] == 6) { 2110 magic = (u_long)p[2] << 24 | 2111 (u_long)p[3] << 16 | p[4] << 8 | p[5]; 2112 /* 2113 * If the remote magic is our negated one, 2114 * this looks like a loopback problem. 2115 * Suggest a new magic to make sure. 2116 */ 2117 if (magic == ~sp->lcp.magic) { 2118 if (debug) 2119 addlog("magic glitch "); 2120 read_random((char*)&sp->lcp.magic, sizeof sp->lcp.magic); 2121 } else { 2122 sp->lcp.magic = magic; 2123 if (debug) 2124 addlog("%d "); 2125 } 2126 } 2127 break; 2128 case LCP_OPT_MRU: 2129 /* 2130 * Peer wants to advise us to negotiate an MRU. 2131 * Agree on it if it's reasonable, or use 2132 * default otherwise. 2133 */ 2134 if (len >= 4 && p[1] == 4) { 2135 u_int mru = p[2] * 256 + p[3]; 2136 if (debug) 2137 addlog("%d ", mru); 2138 if (mru < PP_MTU || mru > PP_MAX_MRU) 2139 mru = PP_MTU; 2140 sp->lcp.mru = mru; 2141 sp->lcp.opts |= (1 << LCP_OPT_MRU); 2142 } 2143 break; 2144 case LCP_OPT_AUTH_PROTO: 2145 /* 2146 * Peer doesn't like our authentication method, 2147 * deny. 2148 */ 2149 if (debug) 2150 addlog("[access denied]\n"); 2151 lcp.Close(sp); 2152 break; 2153 } 2154 } 2155 if (debug) 2156 addlog("\n"); 2157 free (buf, M_TEMP); 2158 return; 2159} 2160 2161static void 2162sppp_lcp_tlu(struct sppp *sp) 2163{ 2164 STDDCL; 2165 int i; 2166 u_long mask; 2167 2168 /* XXX ? */ 2169 if (! (ifp->if_flags & IFF_UP) && 2170 (ifp->if_flags & IFF_RUNNING)) { 2171 /* Coming out of loopback mode. */ 2172 if_up(ifp); 2173 printf ("%s%d: up\n", ifp->if_name, ifp->if_unit); 2174 } 2175 2176 for (i = 0; i < IDX_COUNT; i++) 2177 if ((cps[i])->flags & CP_QUAL) 2178 (cps[i])->Open(sp); 2179 2180 if ((sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0 || 2181 (sp->pp_flags & PP_NEEDAUTH) != 0) 2182 sp->pp_phase = PHASE_AUTHENTICATE; 2183 else 2184 sp->pp_phase = PHASE_NETWORK; 2185 2186 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit, 2187 sppp_phase_name(sp->pp_phase)); 2188 2189 /* 2190 * Open all authentication protocols. This is even required 2191 * if we already proceeded to network phase, since it might be 2192 * that remote wants us to authenticate, so we might have to 2193 * send a PAP request. Undesired authentication protocols 2194 * don't do anything when they get an Open event. 2195 */ 2196 for (i = 0; i < IDX_COUNT; i++) 2197 if ((cps[i])->flags & CP_AUTH) 2198 (cps[i])->Open(sp); 2199 2200 if (sp->pp_phase == PHASE_NETWORK) { 2201 /* Notify all NCPs. */ 2202 for (i = 0; i < IDX_COUNT; i++) 2203 if ((cps[i])->flags & CP_NCP) 2204 (cps[i])->Open(sp); 2205 } 2206 2207 /* Send Up events to all started protos. */ 2208 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1) 2209 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0) 2210 (cps[i])->Up(sp); 2211 2212 if (sp->pp_phase == PHASE_NETWORK) 2213 /* if no NCP is starting, close down */ 2214 sppp_lcp_check_and_close(sp); 2215} 2216 2217static void 2218sppp_lcp_tld(struct sppp *sp) 2219{ 2220 STDDCL; 2221 int i; 2222 u_long mask; 2223 2224 sp->pp_phase = PHASE_TERMINATE; 2225 2226 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit, 2227 sppp_phase_name(sp->pp_phase)); 2228 2229 /* 2230 * Take upper layers down. We send the Down event first and 2231 * the Close second to prevent the upper layers from sending 2232 * ``a flurry of terminate-request packets'', as the RFC 2233 * describes it. 2234 */ 2235 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1) 2236 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_LCP) == 0) { 2237 (cps[i])->Down(sp); 2238 (cps[i])->Close(sp); 2239 } 2240} 2241 2242static void 2243sppp_lcp_tls(struct sppp *sp) 2244{ 2245 STDDCL; 2246 2247 sp->pp_phase = PHASE_ESTABLISH; 2248 2249 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit, 2250 sppp_phase_name(sp->pp_phase)); 2251 2252 /* Notify lower layer if desired. */ 2253 if (sp->pp_tls) 2254 (sp->pp_tls)(sp); 2255} 2256 2257static void 2258sppp_lcp_tlf(struct sppp *sp) 2259{ 2260 STDDCL; 2261 2262 sp->pp_phase = PHASE_DEAD; 2263 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit, 2264 sppp_phase_name(sp->pp_phase)); 2265 2266 /* Notify lower layer if desired. */ 2267 if (sp->pp_tlf) 2268 (sp->pp_tlf)(sp); 2269} 2270 2271static void 2272sppp_lcp_scr(struct sppp *sp) 2273{ 2274 char opt[6 /* magicnum */ + 4 /* mru */ + 5 /* chap */]; 2275 int i = 0; 2276 u_short authproto; 2277 2278 if (sp->lcp.opts & (1 << LCP_OPT_MAGIC)) { 2279 if (! sp->lcp.magic) 2280 read_random((char*)&sp->lcp.magic, sizeof sp->lcp.magic); 2281 opt[i++] = LCP_OPT_MAGIC; 2282 opt[i++] = 6; 2283 opt[i++] = sp->lcp.magic >> 24; 2284 opt[i++] = sp->lcp.magic >> 16; 2285 opt[i++] = sp->lcp.magic >> 8; 2286 opt[i++] = sp->lcp.magic; 2287 } 2288 2289 if (sp->lcp.opts & (1 << LCP_OPT_MRU)) { 2290 opt[i++] = LCP_OPT_MRU; 2291 opt[i++] = 4; 2292 opt[i++] = sp->lcp.mru >> 8; 2293 opt[i++] = sp->lcp.mru; 2294 } 2295 2296 if (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) { 2297 authproto = sp->hisauth.proto; 2298 opt[i++] = LCP_OPT_AUTH_PROTO; 2299 opt[i++] = authproto == PPP_CHAP? 5: 4; 2300 opt[i++] = authproto >> 8; 2301 opt[i++] = authproto; 2302 if (authproto == PPP_CHAP) 2303 opt[i++] = CHAP_MD5; 2304 } 2305 2306 sp->confid[IDX_LCP] = ++sp->pp_seq; 2307 sppp_cp_send (sp, PPP_LCP, CONF_REQ, sp->confid[IDX_LCP], i, &opt); 2308} 2309 2310/* 2311 * Check the open NCPs, return true if at least one NCP is open. 2312 */ 2313static int 2314sppp_ncp_check(struct sppp *sp) 2315{ 2316 int i, mask; 2317 2318 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1) 2319 if (sp->lcp.protos & mask && (cps[i])->flags & CP_NCP) 2320 return 1; 2321 return 0; 2322} 2323 2324/* 2325 * Re-check the open NCPs and see if we should terminate the link. 2326 * Called by the NCPs during their tlf action handling. 2327 */ 2328static void 2329sppp_lcp_check_and_close(struct sppp *sp) 2330{ 2331 2332 if (sp->pp_phase < PHASE_NETWORK) 2333 /* don't bother, we are already going down */ 2334 return; 2335 2336 if (sppp_ncp_check(sp)) 2337 return; 2338 2339 lcp.Close(sp); 2340} 2341/* 2342 *--------------------------------------------------------------------------* 2343 * * 2344 * The IPCP implementation. * 2345 * * 2346 *--------------------------------------------------------------------------* 2347 */ 2348 2349static void 2350sppp_ipcp_init(struct sppp *sp) 2351{ 2352 sp->ipcp.opts = 0; 2353 sp->ipcp.flags = 0; 2354 sp->state[IDX_IPCP] = STATE_INITIAL; 2355 sp->fail_counter[IDX_IPCP] = 0; 2356 callout_handle_init(&sp->ch[IDX_IPCP]); 2357} 2358 2359static void 2360sppp_ipcp_up(struct sppp *sp) 2361{ 2362 sppp_up_event(&ipcp, sp); 2363} 2364 2365static void 2366sppp_ipcp_down(struct sppp *sp) 2367{ 2368 sppp_down_event(&ipcp, sp); 2369} 2370 2371static void 2372sppp_ipcp_open(struct sppp *sp) 2373{ 2374 STDDCL; 2375 u_long myaddr, hisaddr; 2376 2377 sppp_get_ip_addrs(sp, &myaddr, &hisaddr, 0); 2378 /* 2379 * If we don't have his address, this probably means our 2380 * interface doesn't want to talk IP at all. (This could 2381 * be the case if somebody wants to speak only IPX, for 2382 * example.) Don't open IPCP in this case. 2383 */ 2384 if (hisaddr == 0L) { 2385 /* XXX this message should go away */ 2386 if (debug) 2387 log(LOG_DEBUG, "%s%d: ipcp_open(): no IP interface\n", 2388 ifp->if_name, ifp->if_unit); 2389 return; 2390 } 2391 2392 if (myaddr == 0L) { 2393 /* 2394 * I don't have an assigned address, so i need to 2395 * negotiate my address. 2396 */ 2397 sp->ipcp.flags |= IPCP_MYADDR_DYN; 2398 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS); 2399 } 2400 sppp_open_event(&ipcp, sp); 2401} 2402 2403static void 2404sppp_ipcp_close(struct sppp *sp) 2405{ 2406 sppp_close_event(&ipcp, sp); 2407 if (sp->ipcp.flags & IPCP_MYADDR_DYN) 2408 /* 2409 * My address was dynamic, clear it again. 2410 */ 2411 sppp_set_ip_addr(sp, 0L); 2412} 2413 2414static void 2415sppp_ipcp_TO(void *cookie) 2416{ 2417 sppp_to_event(&ipcp, (struct sppp *)cookie); 2418} 2419 2420/* 2421 * Analyze a configure request. Return true if it was agreeable, and 2422 * caused action sca, false if it has been rejected or nak'ed, and 2423 * caused action scn. (The return value is used to make the state 2424 * transition decision in the state automaton.) 2425 */ 2426static int 2427sppp_ipcp_RCR(struct sppp *sp, struct lcp_header *h, int len) 2428{ 2429 u_char *buf, *r, *p; 2430 struct ifnet *ifp = &sp->pp_if; 2431 int rlen, origlen, debug = ifp->if_flags & IFF_DEBUG; 2432 u_long hisaddr, desiredaddr; 2433 2434 len -= 4; 2435 origlen = len; 2436 /* 2437 * Make sure to allocate a buf that can at least hold a 2438 * conf-nak with an `address' option. We might need it below. 2439 */ 2440 buf = r = malloc ((len < 6? 6: len), M_TEMP, M_NOWAIT); 2441 if (! buf) 2442 return (0); 2443 2444 /* pass 1: see if we can recognize them */ 2445 if (debug) 2446 log(LOG_DEBUG, "%s%d: ipcp parse opts: ", 2447 ifp->if_name, ifp->if_unit); 2448 p = (void*) (h+1); 2449 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) { 2450 if (debug) 2451 addlog(" %s ", sppp_ipcp_opt_name(*p)); 2452 switch (*p) { 2453#ifdef notyet 2454 case IPCP_OPT_COMPRESSION: 2455 if (len >= 6 && p[1] >= 6) { 2456 /* correctly formed compress option */ 2457 continue; 2458 } 2459 if (debug) 2460 addlog("[invalid] "); 2461 break; 2462#endif 2463 case IPCP_OPT_ADDRESS: 2464 if (len >= 6 && p[1] == 6) { 2465 /* correctly formed address option */ 2466 continue; 2467 } 2468 if (debug) 2469 addlog("[invalid] "); 2470 break; 2471 default: 2472 /* Others not supported. */ 2473 if (debug) 2474 addlog("[rej] "); 2475 break; 2476 } 2477 /* Add the option to rejected list. */ 2478 bcopy (p, r, p[1]); 2479 r += p[1]; 2480 rlen += p[1]; 2481 } 2482 if (rlen) { 2483 if (debug) 2484 addlog(" send conf-rej\n"); 2485 sppp_cp_send (sp, PPP_IPCP, CONF_REJ, h->ident, rlen, buf); 2486 return 0; 2487 } else if (debug) 2488 addlog("\n"); 2489 2490 /* pass 2: parse option values */ 2491 sppp_get_ip_addrs(sp, 0, &hisaddr, 0); 2492 if (debug) 2493 log(LOG_DEBUG, "%s%d: ipcp parse opt values: ", 2494 ifp->if_name, ifp->if_unit); 2495 p = (void*) (h+1); 2496 len = origlen; 2497 for (rlen=0; len>1 && p[1]; len-=p[1], p+=p[1]) { 2498 if (debug) 2499 addlog(" %s ", sppp_ipcp_opt_name(*p)); 2500 switch (*p) { 2501#ifdef notyet 2502 case IPCP_OPT_COMPRESSION: 2503 continue; 2504#endif 2505 case IPCP_OPT_ADDRESS: 2506 desiredaddr = p[2] << 24 | p[3] << 16 | 2507 p[4] << 8 | p[5]; 2508 if (desiredaddr == hisaddr || 2509 hisaddr == 1 && desiredaddr != 0) { 2510 /* 2511 * Peer's address is same as our value, 2512 * or we have set it to 0.0.0.1 to 2513 * indicate that we do not really care, 2514 * this is agreeable. Gonna conf-ack 2515 * it. 2516 */ 2517 if (debug) 2518 addlog("%s [ack] ", 2519 sppp_dotted_quad(desiredaddr)); 2520 /* record that we've seen it already */ 2521 sp->ipcp.flags |= IPCP_HISADDR_SEEN; 2522 continue; 2523 } 2524 /* 2525 * The address wasn't agreeable. This is either 2526 * he sent us 0.0.0.0, asking to assign him an 2527 * address, or he send us another address not 2528 * matching our value. Either case, we gonna 2529 * conf-nak it with our value. 2530 */ 2531 if (debug) { 2532 if (desiredaddr == 0) 2533 addlog("[addr requested] "); 2534 else 2535 addlog("%s [not agreed] ", 2536 sppp_dotted_quad(desiredaddr)); 2537 2538 p[2] = hisaddr >> 24; 2539 p[3] = hisaddr >> 16; 2540 p[4] = hisaddr >> 8; 2541 p[5] = hisaddr; 2542 } 2543 break; 2544 } 2545 /* Add the option to nak'ed list. */ 2546 bcopy (p, r, p[1]); 2547 r += p[1]; 2548 rlen += p[1]; 2549 } 2550 2551 /* 2552 * If we are about to conf-ack the request, but haven't seen 2553 * his address so far, gonna conf-nak it instead, with the 2554 * `address' option present and our idea of his address being 2555 * filled in there, to request negotiation of both addresses. 2556 * 2557 * XXX This can result in an endless req - nak loop if peer 2558 * doesn't want to send us his address. Q: What should we do 2559 * about it? XXX A: implement the max-failure counter. 2560 */ 2561 if (rlen == 0 && !(sp->ipcp.flags & IPCP_HISADDR_SEEN)) { 2562 buf[0] = IPCP_OPT_ADDRESS; 2563 buf[1] = 6; 2564 buf[2] = hisaddr >> 24; 2565 buf[3] = hisaddr >> 16; 2566 buf[4] = hisaddr >> 8; 2567 buf[5] = hisaddr; 2568 rlen = 6; 2569 if (debug) 2570 addlog("still need hisaddr "); 2571 } 2572 2573 if (rlen) { 2574 if (debug) 2575 addlog(" send conf-nak\n"); 2576 sppp_cp_send (sp, PPP_IPCP, CONF_NAK, h->ident, rlen, buf); 2577 } else { 2578 if (debug) 2579 addlog(" send conf-ack\n"); 2580 sppp_cp_send (sp, PPP_IPCP, CONF_ACK, 2581 h->ident, origlen, h+1); 2582 } 2583 2584 free (buf, M_TEMP); 2585 return (rlen == 0); 2586} 2587 2588/* 2589 * Analyze the IPCP Configure-Reject option list, and adjust our 2590 * negotiation. 2591 */ 2592static void 2593sppp_ipcp_RCN_rej(struct sppp *sp, struct lcp_header *h, int len) 2594{ 2595 u_char *buf, *p; 2596 struct ifnet *ifp = &sp->pp_if; 2597 int debug = ifp->if_flags & IFF_DEBUG; 2598 2599 len -= 4; 2600 buf = malloc (len, M_TEMP, M_NOWAIT); 2601 if (!buf) 2602 return; 2603 2604 if (debug) 2605 log(LOG_DEBUG, "%s%d: ipcp rej opts: ", 2606 ifp->if_name, ifp->if_unit); 2607 2608 p = (void*) (h+1); 2609 for (; len > 1 && p[1]; len -= p[1], p += p[1]) { 2610 if (debug) 2611 addlog(" %s ", sppp_ipcp_opt_name(*p)); 2612 switch (*p) { 2613 case IPCP_OPT_ADDRESS: 2614 /* 2615 * Peer doesn't grok address option. This is 2616 * bad. XXX Should we better give up here? 2617 */ 2618 sp->ipcp.opts &= ~(1 << IPCP_OPT_ADDRESS); 2619 break; 2620#ifdef notyet 2621 case IPCP_OPT_COMPRESS: 2622 sp->ipcp.opts &= ~(1 << IPCP_OPT_COMPRESS); 2623 break; 2624#endif 2625 } 2626 } 2627 if (debug) 2628 addlog("\n"); 2629 free (buf, M_TEMP); 2630 return; 2631} 2632 2633/* 2634 * Analyze the IPCP Configure-NAK option list, and adjust our 2635 * negotiation. 2636 */ 2637static void 2638sppp_ipcp_RCN_nak(struct sppp *sp, struct lcp_header *h, int len) 2639{ 2640 u_char *buf, *p; 2641 struct ifnet *ifp = &sp->pp_if; 2642 int debug = ifp->if_flags & IFF_DEBUG; 2643 u_long wantaddr; 2644 2645 len -= 4; 2646 buf = malloc (len, M_TEMP, M_NOWAIT); 2647 if (!buf) 2648 return; 2649 2650 if (debug) 2651 log(LOG_DEBUG, "%s%d: ipcp nak opts: ", 2652 ifp->if_name, ifp->if_unit); 2653 2654 p = (void*) (h+1); 2655 for (; len > 1 && p[1]; len -= p[1], p += p[1]) { 2656 if (debug) 2657 addlog(" %s ", sppp_ipcp_opt_name(*p)); 2658 switch (*p) { 2659 case IPCP_OPT_ADDRESS: 2660 /* 2661 * Peer doesn't like our local IP address. See 2662 * if we can do something for him. We'll drop 2663 * him our address then. 2664 */ 2665 if (len >= 6 && p[1] == 6) { 2666 wantaddr = p[2] << 24 | p[3] << 16 | 2667 p[4] << 8 | p[5]; 2668 sp->ipcp.opts |= (1 << IPCP_OPT_ADDRESS); 2669 if (debug) 2670 addlog("[wantaddr %s] ", 2671 sppp_dotted_quad(wantaddr)); 2672 /* 2673 * When doing dynamic address assignment, 2674 * we accept his offer. Otherwise, we 2675 * ignore it and thus continue to negotiate 2676 * our already existing value. 2677 */ 2678 if (sp->ipcp.flags & IPCP_MYADDR_DYN) { 2679 sppp_set_ip_addr(sp, wantaddr); 2680 if (debug) 2681 addlog("[agree] "); 2682 } 2683 } 2684 break; 2685#ifdef notyet 2686 case IPCP_OPT_COMPRESS: 2687 /* 2688 * Peer wants different compression parameters. 2689 */ 2690 break; 2691#endif 2692 } 2693 } 2694 if (debug) 2695 addlog("\n"); 2696 free (buf, M_TEMP); 2697 return; 2698} 2699 2700static void 2701sppp_ipcp_tlu(struct sppp *sp) 2702{ 2703} 2704 2705static void 2706sppp_ipcp_tld(struct sppp *sp) 2707{ 2708} 2709 2710static void 2711sppp_ipcp_tls(struct sppp *sp) 2712{ 2713 /* indicate to LCP that it must stay alive */ 2714 sp->lcp.protos |= (1 << IDX_IPCP); 2715} 2716 2717static void 2718sppp_ipcp_tlf(struct sppp *sp) 2719{ 2720 /* we no longer need LCP */ 2721 sp->lcp.protos &= ~(1 << IDX_IPCP); 2722 sppp_lcp_check_and_close(sp); 2723} 2724 2725static void 2726sppp_ipcp_scr(struct sppp *sp) 2727{ 2728 char opt[6 /* compression */ + 6 /* address */]; 2729 u_long ouraddr; 2730 int i = 0; 2731 2732#ifdef notyet 2733 if (sp->ipcp.opts & (1 << IPCP_OPT_COMPRESSION)) { 2734 opt[i++] = IPCP_OPT_COMPRESSION; 2735 opt[i++] = 6; 2736 opt[i++] = 0; /* VJ header compression */ 2737 opt[i++] = 0x2d; /* VJ header compression */ 2738 opt[i++] = max_slot_id; 2739 opt[i++] = comp_slot_id; 2740 } 2741#endif 2742 2743 if (sp->ipcp.opts & (1 << IPCP_OPT_ADDRESS)) { 2744 sppp_get_ip_addrs(sp, &ouraddr, 0, 0); 2745 opt[i++] = IPCP_OPT_ADDRESS; 2746 opt[i++] = 6; 2747 opt[i++] = ouraddr >> 24; 2748 opt[i++] = ouraddr >> 16; 2749 opt[i++] = ouraddr >> 8; 2750 opt[i++] = ouraddr; 2751 } 2752 2753 sp->confid[IDX_IPCP] = ++sp->pp_seq; 2754 sppp_cp_send(sp, PPP_IPCP, CONF_REQ, sp->confid[IDX_IPCP], i, &opt); 2755} 2756 2757 2758/* 2759 *--------------------------------------------------------------------------* 2760 * * 2761 * The CHAP implementation. * 2762 * * 2763 *--------------------------------------------------------------------------* 2764 */ 2765 2766/* 2767 * The authentication protocols don't employ a full-fledged state machine as 2768 * the control protocols do, since they do have Open and Close events, but 2769 * not Up and Down, nor are they explicitly terminated. Also, use of the 2770 * authentication protocols may be different in both directions (this makes 2771 * sense, think of a machine that never accepts incoming calls but only 2772 * calls out, it doesn't require the called party to authenticate itself). 2773 * 2774 * Our state machine for the local authentication protocol (we are requesting 2775 * the peer to authenticate) looks like: 2776 * 2777 * RCA- 2778 * +--------------------------------------------+ 2779 * V scn,tld| 2780 * +--------+ Close +---------+ RCA+ 2781 * | |<----------------------------------| |------+ 2782 * +--->| Closed | TO* | Opened | sca | 2783 * | | |-----+ +-------| |<-----+ 2784 * | +--------+ irc | | +---------+ 2785 * | ^ | | ^ 2786 * | | | | | 2787 * | | | | | 2788 * | TO-| | | | 2789 * | |tld TO+ V | | 2790 * | | +------->+ | | 2791 * | | | | | | 2792 * | +--------+ V | | 2793 * | | |<----+<--------------------+ | 2794 * | | Req- | scr | 2795 * | | Sent | | 2796 * | | | | 2797 * | +--------+ | 2798 * | RCA- | | RCA+ | 2799 * +------+ +------------------------------------------+ 2800 * scn,tld sca,irc,ict,tlu 2801 * 2802 * 2803 * with: 2804 * 2805 * Open: LCP reached authentication phase 2806 * Close: LCP reached terminate phase 2807 * 2808 * RCA+: received reply (pap-req, chap-response), acceptable 2809 * RCN: received reply (pap-req, chap-response), not acceptable 2810 * TO+: timeout with restart counter >= 0 2811 * TO-: timeout with restart counter < 0 2812 * TO*: reschedule timeout for CHAP 2813 * 2814 * scr: send request packet (none for PAP, chap-challenge) 2815 * sca: send ack packet (pap-ack, chap-success) 2816 * scn: send nak packet (pap-nak, chap-failure) 2817 * ict: initialize re-challenge timer (CHAP only) 2818 * 2819 * tlu: this-layer-up, LCP reaches network phase 2820 * tld: this-layer-down, LCP enters terminate phase 2821 * 2822 * Note that in CHAP mode, after sending a new challenge, while the state 2823 * automaton falls back into Req-Sent state, it doesn't signal a tld 2824 * event to LCP, so LCP remains in network phase. Only after not getting 2825 * any response (or after getting an unacceptable response), CHAP closes, 2826 * causing LCP to enter terminate phase. 2827 * 2828 * With PAP, there is no initial request that can be sent. The peer is 2829 * expected to send one based on the successful negotiation of PAP as 2830 * the authentication protocol during the LCP option negotiation. 2831 * 2832 * Incoming authentication protocol requests (remote requests 2833 * authentication, we are peer) don't employ a state machine at all, 2834 * they are simply answered. Some peers [Ascend P50 firmware rev 2835 * 4.50] react allergically when sending IPCP requests while they are 2836 * still in authentication phase (thereby violating the standard that 2837 * demands that these NCP packets are to be discarded), so we keep 2838 * track of the peer demanding us to authenticate, and only proceed to 2839 * phase network once we've seen a positive acknowledge for the 2840 * authentication. 2841 */ 2842 2843/* 2844 * Handle incoming CHAP packets. 2845 */ 2846void 2847sppp_chap_input(struct sppp *sp, struct mbuf *m) 2848{ 2849 STDDCL; 2850 struct lcp_header *h; 2851 int len, x; 2852 u_char *value, *name, digest[AUTHKEYLEN], dsize; 2853 int value_len, name_len; 2854 MD5_CTX ctx; 2855 2856 len = m->m_pkthdr.len; 2857 if (len < 4) { 2858 if (debug) 2859 log(LOG_DEBUG, 2860 "%s%d: chap invalid packet length: %d bytes\n", 2861 ifp->if_name, ifp->if_unit, len); 2862 return; 2863 } 2864 h = mtod (m, struct lcp_header*); 2865 if (len > ntohs (h->len)) 2866 len = ntohs (h->len); 2867 2868 switch (h->type) { 2869 /* challenge, failure and success are his authproto */ 2870 case CHAP_CHALLENGE: 2871 value = 1 + (u_char*)(h+1); 2872 value_len = value[-1]; 2873 name = value + value_len; 2874 name_len = len - value_len - 5; 2875 if (name_len < 0) { 2876 if (debug) { 2877 log(LOG_DEBUG, 2878 "%s%d: chap corrupted challenge " 2879 "<%s id=0x%x len=%d", 2880 ifp->if_name, ifp->if_unit, 2881 sppp_auth_type_name(PPP_CHAP, h->type), 2882 h->ident, ntohs(h->len)); 2883 if (len > 4) 2884 sppp_print_bytes((u_char*) (h+1), len-4); 2885 addlog(">\n"); 2886 } 2887 break; 2888 } 2889 2890 if (debug) { 2891 log(LOG_DEBUG, 2892 "%s%d: chap input <%s id=0x%x len=%d name=", 2893 ifp->if_name, ifp->if_unit, 2894 sppp_auth_type_name(PPP_CHAP, h->type), h->ident, 2895 ntohs(h->len)); 2896 sppp_print_string((char*) name, name_len); 2897 addlog(" value-size=%d value=", value_len); 2898 sppp_print_bytes(value, value_len); 2899 addlog(">\n"); 2900 } 2901 2902 /* Compute reply value. */ 2903 MD5Init(&ctx); 2904 MD5Update(&ctx, &h->ident, 1); 2905 MD5Update(&ctx, sp->myauth.secret, 2906 sppp_strnlen(sp->myauth.secret, AUTHKEYLEN)); 2907 MD5Update(&ctx, value, value_len); 2908 MD5Final(digest, &ctx); 2909 dsize = sizeof digest; 2910 2911 sppp_auth_send(&chap, sp, CHAP_RESPONSE, h->ident, 2912 sizeof dsize, (const char *)&dsize, 2913 sizeof digest, digest, 2914 sppp_strnlen(sp->myauth.name, AUTHNAMELEN), 2915 sp->myauth.name, 2916 0); 2917 break; 2918 2919 case CHAP_SUCCESS: 2920 if (debug) { 2921 log(LOG_DEBUG, "%s%d: chap success", 2922 ifp->if_name, ifp->if_unit); 2923 if (len > 4) { 2924 addlog(": "); 2925 sppp_print_string((char*)(h + 1), len - 4); 2926 } 2927 addlog("\n"); 2928 } 2929 x = splimp(); 2930 sp->pp_flags &= ~PP_NEEDAUTH; 2931 if (sp->myauth.proto == PPP_CHAP && 2932 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) && 2933 (sp->lcp.protos & (1 << IDX_CHAP)) == 0) { 2934 /* 2935 * We are authenticator for CHAP but didn't 2936 * complete yet. Leave it to tlu to proceed 2937 * to network phase. 2938 */ 2939 splx(x); 2940 break; 2941 } 2942 splx(x); 2943 sppp_phase_network(sp); 2944 break; 2945 2946 case CHAP_FAILURE: 2947 if (debug) { 2948 log(LOG_INFO, "%s%d: chap failure", 2949 ifp->if_name, ifp->if_unit); 2950 if (len > 4) { 2951 addlog(": "); 2952 sppp_print_string((char*)(h + 1), len - 4); 2953 } 2954 addlog("\n"); 2955 } else 2956 log(LOG_INFO, "%s%d: chap failure\n", 2957 ifp->if_name, ifp->if_unit); 2958 /* await LCP shutdown by authenticator */ 2959 break; 2960 2961 /* response is my authproto */ 2962 case CHAP_RESPONSE: 2963 value = 1 + (u_char*)(h+1); 2964 value_len = value[-1]; 2965 name = value + value_len; 2966 name_len = len - value_len - 5; 2967 if (name_len < 0) { 2968 if (debug) { 2969 log(LOG_DEBUG, 2970 "%s%d: chap corrupted response " 2971 "<%s id=0x%x len=%d", 2972 ifp->if_name, ifp->if_unit, 2973 sppp_auth_type_name(PPP_CHAP, h->type), 2974 h->ident, ntohs(h->len)); 2975 if (len > 4) 2976 sppp_print_bytes((u_char*)(h+1), len-4); 2977 addlog(">\n"); 2978 } 2979 break; 2980 } 2981 if (h->ident != sp->confid[IDX_CHAP]) { 2982 if (debug) 2983 log(LOG_DEBUG, 2984 "%s%d: chap dropping response for old ID " 2985 "(got %d, expected %d)\n", 2986 h->ident, sp->confid[IDX_CHAP]); 2987 break; 2988 } 2989 if (name_len != sppp_strnlen(sp->hisauth.name, AUTHNAMELEN) 2990 || bcmp(name, sp->hisauth.name, name_len) != 0) { 2991 log(LOG_INFO, "%s%d: chap response, his name ", 2992 ifp->if_name, ifp->if_unit); 2993 sppp_print_string(name, name_len); 2994 addlog(" != expected "); 2995 sppp_print_string(sp->hisauth.name, 2996 sppp_strnlen(sp->hisauth.name, AUTHNAMELEN)); 2997 addlog("\n"); 2998 } 2999 if (debug) { 3000 log(LOG_DEBUG, "%s%d: chap input(%s) " 3001 "<%s id=0x%x len=%d name=", 3002 ifp->if_name, ifp->if_unit, 3003 sppp_state_name(sp->state[IDX_CHAP]), 3004 sppp_auth_type_name(PPP_CHAP, h->type), 3005 h->ident, ntohs (h->len)); 3006 sppp_print_string((char*)name, name_len); 3007 addlog(" value-size=%d value=", value_len); 3008 sppp_print_bytes(value, value_len); 3009 addlog(">\n"); 3010 } 3011 if (value_len != AUTHKEYLEN) { 3012 if (debug) 3013 log(LOG_DEBUG, 3014 "%s%d: chap bad hash value length: " 3015 "%d bytes, should be %d\n", 3016 ifp->if_name, ifp->if_unit, value_len, 3017 AUTHKEYLEN); 3018 break; 3019 } 3020 3021 MD5Init(&ctx); 3022 MD5Update(&ctx, &h->ident, 1); 3023 MD5Update(&ctx, sp->hisauth.secret, 3024 sppp_strnlen(sp->hisauth.secret, AUTHKEYLEN)); 3025 MD5Update(&ctx, sp->myauth.challenge, AUTHKEYLEN); 3026 MD5Final(digest, &ctx); 3027 3028#define FAILMSG "Failed..." 3029#define SUCCMSG "Welcome!" 3030 3031 if (value_len != sizeof digest || 3032 bcmp(digest, value, value_len) != 0) { 3033 /* action scn, tld */ 3034 sppp_auth_send(&chap, sp, CHAP_FAILURE, h->ident, 3035 sizeof(FAILMSG) - 1, (u_char *)FAILMSG, 3036 0); 3037 chap.tld(sp); 3038 break; 3039 } 3040 /* action sca, perhaps tlu */ 3041 if (sp->state[IDX_CHAP] == STATE_REQ_SENT || 3042 sp->state[IDX_CHAP] == STATE_OPENED) 3043 sppp_auth_send(&chap, sp, CHAP_SUCCESS, h->ident, 3044 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG, 3045 0); 3046 if (sp->state[IDX_CHAP] == STATE_REQ_SENT) { 3047 sppp_cp_change_state(&chap, sp, STATE_OPENED); 3048 chap.tlu(sp); 3049 } 3050 break; 3051 3052 default: 3053 /* Unknown CHAP packet type -- ignore. */ 3054 if (debug) { 3055 log(LOG_DEBUG, "%s%d: chap unknown input(%s) " 3056 "<0x%x id=0x%xh len=%d", 3057 ifp->if_name, ifp->if_unit, 3058 sppp_state_name(sp->state[IDX_CHAP]), 3059 h->type, h->ident, ntohs(h->len)); 3060 if (len > 4) 3061 sppp_print_bytes((u_char*)(h+1), len-4); 3062 addlog(">\n"); 3063 } 3064 break; 3065 3066 } 3067} 3068 3069static void 3070sppp_chap_init(struct sppp *sp) 3071{ 3072 /* Chap doesn't have STATE_INITIAL at all. */ 3073 sp->state[IDX_CHAP] = STATE_CLOSED; 3074 sp->fail_counter[IDX_CHAP] = 0; 3075 callout_handle_init(&sp->ch[IDX_CHAP]); 3076} 3077 3078static void 3079sppp_chap_open(struct sppp *sp) 3080{ 3081 if (sp->myauth.proto == PPP_CHAP && 3082 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) { 3083 /* we are authenticator for CHAP, start it */ 3084 chap.scr(sp); 3085 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure; 3086 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT); 3087 } 3088 /* nothing to be done if we are peer, await a challenge */ 3089} 3090 3091static void 3092sppp_chap_close(struct sppp *sp) 3093{ 3094 if (sp->state[IDX_CHAP] != STATE_CLOSED) 3095 sppp_cp_change_state(&chap, sp, STATE_CLOSED); 3096} 3097 3098static void 3099sppp_chap_TO(void *cookie) 3100{ 3101 struct sppp *sp = (struct sppp *)cookie; 3102 STDDCL; 3103 int s; 3104 3105 s = splimp(); 3106 if (debug) 3107 log(LOG_DEBUG, "%s%d: chap TO(%s) rst_counter = %d\n", 3108 ifp->if_name, ifp->if_unit, 3109 sppp_state_name(sp->state[IDX_CHAP]), 3110 sp->rst_counter[IDX_CHAP]); 3111 3112 if (--sp->rst_counter[IDX_CHAP] < 0) 3113 /* TO- event */ 3114 switch (sp->state[IDX_CHAP]) { 3115 case STATE_REQ_SENT: 3116 chap.tld(sp); 3117 sppp_cp_change_state(&chap, sp, STATE_CLOSED); 3118 break; 3119 } 3120 else 3121 /* TO+ (or TO*) event */ 3122 switch (sp->state[IDX_CHAP]) { 3123 case STATE_OPENED: 3124 /* TO* event */ 3125 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure; 3126 /* fall through */ 3127 case STATE_REQ_SENT: 3128 chap.scr(sp); 3129 /* sppp_cp_change_state() will restart the timer */ 3130 sppp_cp_change_state(&chap, sp, STATE_REQ_SENT); 3131 break; 3132 } 3133 3134 splx(s); 3135} 3136 3137static void 3138sppp_chap_tlu(struct sppp *sp) 3139{ 3140 STDDCL; 3141 int i, x; 3142 3143 sp->rst_counter[IDX_CHAP] = sp->lcp.max_configure; 3144 3145 /* 3146 * Some broken CHAP implementations (Conware CoNet, firmware 3147 * 4.0.?) don't want to re-authenticate their CHAP once the 3148 * initial challenge-response exchange has taken place. 3149 * Provide for an option to avoid rechallenges. 3150 */ 3151 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) { 3152 /* 3153 * Compute the re-challenge timeout. This will yield 3154 * a number between 300 and 810 seconds. 3155 */ 3156 i = 300 + ((unsigned)(random() & 0xff00) >> 7); 3157 3158 sp->ch[IDX_CHAP] = timeout(chap.TO, (void *)sp, i * hz); 3159 } 3160 3161 if (debug) { 3162 log(LOG_DEBUG, 3163 "%s%d: chap %s, ", 3164 ifp->if_name, ifp->if_unit, 3165 sp->pp_phase == PHASE_NETWORK? "reconfirmed": "tlu"); 3166 if ((sp->hisauth.flags & AUTHFLAG_NORECHALLENGE) == 0) 3167 addlog("next re-challenge in %d seconds\n", i); 3168 else 3169 addlog("re-challenging supressed\n"); 3170 } 3171 3172 x = splimp(); 3173 /* indicate to LCP that we need to be closed down */ 3174 sp->lcp.protos |= (1 << IDX_CHAP); 3175 3176 if (sp->pp_flags & PP_NEEDAUTH) { 3177 /* 3178 * Remote is authenticator, but his auth proto didn't 3179 * complete yet. Defer the transition to network 3180 * phase. 3181 */ 3182 splx(x); 3183 return; 3184 } 3185 splx(x); 3186 3187 /* 3188 * If we are already in phase network, we are done here. This 3189 * is the case if this is a dummy tlu event after a re-challenge. 3190 */ 3191 if (sp->pp_phase != PHASE_NETWORK) 3192 sppp_phase_network(sp); 3193} 3194 3195static void 3196sppp_chap_tld(struct sppp *sp) 3197{ 3198 STDDCL; 3199 3200 if (debug) 3201 log(LOG_DEBUG, "%s%d: chap tld\n", ifp->if_name, ifp->if_unit); 3202 untimeout(chap.TO, (void *)sp, sp->ch[IDX_CHAP]); 3203 sp->lcp.protos &= ~(1 << IDX_CHAP); 3204 3205 lcp.Close(sp); 3206} 3207 3208static void 3209sppp_chap_scr(struct sppp *sp) 3210{ 3211 struct timeval tv; 3212 u_long *ch, seed; 3213 u_char clen; 3214 3215 /* Compute random challenge. */ 3216 ch = (u_long *)sp->myauth.challenge; 3217 microtime(&tv); 3218 seed = tv.tv_sec ^ tv.tv_usec; 3219 ch[0] = seed ^ random(); 3220 ch[1] = seed ^ random(); 3221 ch[2] = seed ^ random(); 3222 ch[3] = seed ^ random(); 3223 clen = AUTHKEYLEN; 3224 3225 sp->confid[IDX_CHAP] = ++sp->pp_seq; 3226 3227 sppp_auth_send(&chap, sp, CHAP_CHALLENGE, sp->confid[IDX_CHAP], 3228 sizeof clen, (const char *)&clen, 3229 AUTHKEYLEN, sp->myauth.challenge, 3230 sppp_strnlen(sp->myauth.name, AUTHNAMELEN), 3231 sp->myauth.name, 3232 0); 3233} 3234/* 3235 *--------------------------------------------------------------------------* 3236 * * 3237 * The PAP implementation. * 3238 * * 3239 *--------------------------------------------------------------------------* 3240 */ 3241/* 3242 * For PAP, we need to keep a little state also if we are the peer, not the 3243 * authenticator. This is since we don't get a request to authenticate, but 3244 * have to repeatedly authenticate ourself until we got a response (or the 3245 * retry counter is expired). 3246 */ 3247 3248/* 3249 * Handle incoming PAP packets. */ 3250static void 3251sppp_pap_input(struct sppp *sp, struct mbuf *m) 3252{ 3253 STDDCL; 3254 struct lcp_header *h; 3255 int len, x; 3256 u_char *name, *passwd, mlen; 3257 int name_len, passwd_len; 3258 3259 len = m->m_pkthdr.len; 3260 if (len < 5) { 3261 if (debug) 3262 log(LOG_DEBUG, 3263 "%s%d: pap invalid packet length: %d bytes\n", 3264 ifp->if_name, ifp->if_unit, len); 3265 return; 3266 } 3267 h = mtod (m, struct lcp_header*); 3268 if (len > ntohs (h->len)) 3269 len = ntohs (h->len); 3270 switch (h->type) { 3271 /* PAP request is my authproto */ 3272 case PAP_REQ: 3273 name = 1 + (u_char*)(h+1); 3274 name_len = name[-1]; 3275 passwd = name + name_len + 1; 3276 if (name_len > len - 6 || 3277 (passwd_len = passwd[-1]) > len - 6 - name_len) { 3278 if (debug) { 3279 log(LOG_DEBUG, "%s%d: pap corrupted input " 3280 "<%s id=0x%x len=%d", 3281 ifp->if_name, ifp->if_unit, 3282 sppp_auth_type_name(PPP_PAP, h->type), 3283 h->ident, ntohs(h->len)); 3284 if (len > 4) 3285 sppp_print_bytes((u_char*)(h+1), len-4); 3286 addlog(">\n"); 3287 } 3288 break; 3289 } 3290 if (debug) { 3291 log(LOG_DEBUG, "%s%d: pap input(%s) " 3292 "<%s id=0x%x len=%d name=", 3293 ifp->if_name, ifp->if_unit, 3294 sppp_state_name(sp->state[IDX_PAP]), 3295 sppp_auth_type_name(PPP_PAP, h->type), 3296 h->ident, ntohs(h->len)); 3297 sppp_print_string((char*)name, name_len); 3298 addlog(" passwd="); 3299 sppp_print_string((char*)passwd, passwd_len); 3300 addlog(">\n"); 3301 } 3302 if (name_len > AUTHNAMELEN || 3303 passwd_len > AUTHKEYLEN || 3304 bcmp(name, sp->hisauth.name, name_len) != 0 || 3305 bcmp(passwd, sp->hisauth.secret, passwd_len) != 0) { 3306 /* action scn, tld */ 3307 mlen = sizeof(FAILMSG) - 1; 3308 sppp_auth_send(&pap, sp, PAP_NAK, h->ident, 3309 sizeof mlen, (const char *)&mlen, 3310 sizeof(FAILMSG) - 1, (u_char *)FAILMSG, 3311 0); 3312 pap.tld(sp); 3313 break; 3314 } 3315 /* action sca, perhaps tlu */ 3316 if (sp->state[IDX_PAP] == STATE_REQ_SENT || 3317 sp->state[IDX_PAP] == STATE_OPENED) { 3318 mlen = sizeof(SUCCMSG) - 1; 3319 sppp_auth_send(&pap, sp, PAP_ACK, h->ident, 3320 sizeof mlen, (const char *)&mlen, 3321 sizeof(SUCCMSG) - 1, (u_char *)SUCCMSG, 3322 0); 3323 } 3324 if (sp->state[IDX_PAP] == STATE_REQ_SENT) { 3325 sppp_cp_change_state(&pap, sp, STATE_OPENED); 3326 pap.tlu(sp); 3327 } 3328 break; 3329 3330 /* ack and nak are his authproto */ 3331 case PAP_ACK: 3332 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch); 3333 if (debug) { 3334 log(LOG_DEBUG, "%s%d: pap success", 3335 ifp->if_name, ifp->if_unit); 3336 name_len = *((char *)h); 3337 if (len > 5 && name_len) { 3338 addlog(": "); 3339 sppp_print_string((char*)(h+1), name_len); 3340 } 3341 addlog("\n"); 3342 } 3343 x = splimp(); 3344 sp->pp_flags &= ~PP_NEEDAUTH; 3345 if (sp->myauth.proto == PPP_PAP && 3346 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) && 3347 (sp->lcp.protos & (1 << IDX_PAP)) == 0) { 3348 /* 3349 * We are authenticator for PAP but didn't 3350 * complete yet. Leave it to tlu to proceed 3351 * to network phase. 3352 */ 3353 splx(x); 3354 break; 3355 } 3356 splx(x); 3357 sppp_phase_network(sp); 3358 break; 3359 3360 case PAP_NAK: 3361 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch); 3362 if (debug) { 3363 log(LOG_INFO, "%s%d: pap failure", 3364 ifp->if_name, ifp->if_unit); 3365 name_len = *((char *)h); 3366 if (len > 5 && name_len) { 3367 addlog(": "); 3368 sppp_print_string((char*)(h+1), name_len); 3369 } 3370 addlog("\n"); 3371 } else 3372 log(LOG_INFO, "%s%d: pap failure\n", 3373 ifp->if_name, ifp->if_unit); 3374 /* await LCP shutdown by authenticator */ 3375 break; 3376 3377 default: 3378 /* Unknown PAP packet type -- ignore. */ 3379 if (debug) { 3380 log(LOG_DEBUG, "%s%d: pap corrupted input " 3381 "<0x%x id=0x%x len=%d", 3382 ifp->if_name, ifp->if_unit, 3383 h->type, h->ident, ntohs(h->len)); 3384 if (len > 4) 3385 sppp_print_bytes((u_char*)(h+1), len-4); 3386 addlog(">\n"); 3387 } 3388 break; 3389 3390 } 3391} 3392 3393static void 3394sppp_pap_init(struct sppp *sp) 3395{ 3396 /* PAP doesn't have STATE_INITIAL at all. */ 3397 sp->state[IDX_PAP] = STATE_CLOSED; 3398 sp->fail_counter[IDX_PAP] = 0; 3399 callout_handle_init(&sp->ch[IDX_PAP]); 3400 callout_handle_init(&sp->pap_my_to_ch); 3401} 3402 3403static void 3404sppp_pap_open(struct sppp *sp) 3405{ 3406 if (sp->hisauth.proto == PPP_PAP && 3407 (sp->lcp.opts & (1 << LCP_OPT_AUTH_PROTO)) != 0) { 3408 /* we are authenticator for PAP, start our timer */ 3409 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure; 3410 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT); 3411 } 3412 if (sp->myauth.proto == PPP_PAP) { 3413 /* we are peer, send a request, and start a timer */ 3414 pap.scr(sp); 3415 sp->pap_my_to_ch = timeout(sppp_pap_my_TO, (void *)sp, 3416 sp->lcp.timeout); 3417 } 3418} 3419 3420static void 3421sppp_pap_close(struct sppp *sp) 3422{ 3423 if (sp->state[IDX_PAP] != STATE_CLOSED) 3424 sppp_cp_change_state(&pap, sp, STATE_CLOSED); 3425} 3426 3427/* 3428 * That's the timeout routine if we are authenticator. Since the 3429 * authenticator is basically passive in PAP, we can't do much here. 3430 */ 3431static void 3432sppp_pap_TO(void *cookie) 3433{ 3434 struct sppp *sp = (struct sppp *)cookie; 3435 STDDCL; 3436 int s; 3437 3438 s = splimp(); 3439 if (debug) 3440 log(LOG_DEBUG, "%s%d: pap TO(%s) rst_counter = %d\n", 3441 ifp->if_name, ifp->if_unit, 3442 sppp_state_name(sp->state[IDX_PAP]), 3443 sp->rst_counter[IDX_PAP]); 3444 3445 if (--sp->rst_counter[IDX_PAP] < 0) 3446 /* TO- event */ 3447 switch (sp->state[IDX_PAP]) { 3448 case STATE_REQ_SENT: 3449 pap.tld(sp); 3450 sppp_cp_change_state(&pap, sp, STATE_CLOSED); 3451 break; 3452 } 3453 else 3454 /* TO+ event, not very much we could do */ 3455 switch (sp->state[IDX_PAP]) { 3456 case STATE_REQ_SENT: 3457 /* sppp_cp_change_state() will restart the timer */ 3458 sppp_cp_change_state(&pap, sp, STATE_REQ_SENT); 3459 break; 3460 } 3461 3462 splx(s); 3463} 3464 3465/* 3466 * That's the timeout handler if we are peer. Since the peer is active, 3467 * we need to retransmit our PAP request since it is apparently lost. 3468 * XXX We should impose a max counter. 3469 */ 3470static void 3471sppp_pap_my_TO(void *cookie) 3472{ 3473 struct sppp *sp = (struct sppp *)cookie; 3474 STDDCL; 3475 3476 if (debug) 3477 log(LOG_DEBUG, "%s%d: pap peer TO\n", 3478 ifp->if_name, ifp->if_unit); 3479 3480 pap.scr(sp); 3481} 3482 3483static void 3484sppp_pap_tlu(struct sppp *sp) 3485{ 3486 STDDCL; 3487 int x; 3488 3489 sp->rst_counter[IDX_PAP] = sp->lcp.max_configure; 3490 3491 if (debug) 3492 log(LOG_DEBUG, "%s%d: %s tlu\n", 3493 ifp->if_name, ifp->if_unit, pap.name); 3494 3495 x = splimp(); 3496 /* indicate to LCP that we need to be closed down */ 3497 sp->lcp.protos |= (1 << IDX_PAP); 3498 3499 if (sp->pp_flags & PP_NEEDAUTH) { 3500 /* 3501 * Remote is authenticator, but his auth proto didn't 3502 * complete yet. Defer the transition to network 3503 * phase. 3504 */ 3505 splx(x); 3506 return; 3507 } 3508 splx(x); 3509 sppp_phase_network(sp); 3510} 3511 3512static void 3513sppp_pap_tld(struct sppp *sp) 3514{ 3515 STDDCL; 3516 3517 if (debug) 3518 log(LOG_DEBUG, "%s%d: pap tld\n", ifp->if_name, ifp->if_unit); 3519 untimeout(pap.TO, (void *)sp, sp->ch[IDX_PAP]); 3520 untimeout(sppp_pap_my_TO, (void *)sp, sp->pap_my_to_ch); 3521 sp->lcp.protos &= ~(1 << IDX_PAP); 3522 3523 lcp.Close(sp); 3524} 3525 3526static void 3527sppp_pap_scr(struct sppp *sp) 3528{ 3529 STDDCL; 3530 u_char idlen, pwdlen; 3531 3532 sp->confid[IDX_PAP] = ++sp->pp_seq; 3533 pwdlen = sppp_strnlen(sp->myauth.secret, AUTHKEYLEN); 3534 idlen = sppp_strnlen(sp->myauth.name, AUTHNAMELEN); 3535 3536 sppp_auth_send(&pap, sp, PAP_REQ, sp->confid[IDX_PAP], 3537 sizeof idlen, (const char *)&idlen, 3538 (unsigned)idlen, sp->myauth.name, 3539 sizeof pwdlen, (const char *)&pwdlen, 3540 (unsigned)pwdlen, sp->myauth.secret, 3541 0); 3542} 3543/* 3544 * Random miscellaneous functions. 3545 */ 3546 3547/* 3548 * Send a PAP or CHAP proto packet. 3549 * 3550 * Varadic function, each of the elements for the ellipsis is of type 3551 * ``unsigned mlen, const u_char *msg''. Processing will stop iff 3552 * mlen == 0. 3553 */ 3554 3555static void 3556sppp_auth_send(const struct cp *cp, struct sppp *sp, u_char type, u_char id, 3557 ...) 3558{ 3559 STDDCL; 3560 struct ppp_header *h; 3561 struct lcp_header *lh; 3562 struct mbuf *m; 3563 u_char *p; 3564 int len; 3565 unsigned mlen; 3566 const char *msg; 3567 va_list ap; 3568 3569 MGETHDR (m, M_DONTWAIT, MT_DATA); 3570 if (! m) 3571 return; 3572 m->m_pkthdr.rcvif = 0; 3573 3574 h = mtod (m, struct ppp_header*); 3575 h->address = PPP_ALLSTATIONS; /* broadcast address */ 3576 h->control = PPP_UI; /* Unnumbered Info */ 3577 h->protocol = htons(cp->proto); 3578 3579 lh = (struct lcp_header*)(h + 1); 3580 lh->type = type; 3581 lh->ident = id; 3582 p = (u_char*) (lh+1); 3583 3584 va_start(ap, id); 3585 len = 0; 3586 3587 while ((mlen = va_arg(ap, unsigned)) != 0) { 3588 msg = va_arg(ap, const char *); 3589 len += mlen; 3590 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) { 3591 va_end(ap); 3592 m_freem(m); 3593 return; 3594 } 3595 3596 bcopy(msg, p, mlen); 3597 p += mlen; 3598 } 3599 va_end(ap); 3600 3601 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len; 3602 lh->len = htons (LCP_HEADER_LEN + len); 3603 3604 if (debug) { 3605 log(LOG_DEBUG, "%s%d: %s output <%s id=0x%x len=%d", 3606 ifp->if_name, ifp->if_unit, cp->name, 3607 sppp_auth_type_name(cp->proto, lh->type), 3608 lh->ident, ntohs(lh->len)); 3609 if (len) 3610 sppp_print_bytes((u_char*) (lh+1), len); 3611 addlog(">\n"); 3612 } 3613 if (IF_QFULL (&sp->pp_cpq)) { 3614 IF_DROP (&sp->pp_fastq); 3615 IF_DROP (&ifp->if_snd); 3616 m_freem (m); 3617 ++ifp->if_oerrors; 3618 } else 3619 IF_ENQUEUE (&sp->pp_cpq, m); 3620 if (! (ifp->if_flags & IFF_OACTIVE)) 3621 (*ifp->if_start) (ifp); 3622 ifp->if_obytes += m->m_pkthdr.len + 3; 3623} 3624 3625/* 3626 * Flush interface queue. 3627 */ 3628static void 3629sppp_qflush(struct ifqueue *ifq) 3630{ 3631 struct mbuf *m, *n; 3632 3633 n = ifq->ifq_head; 3634 while ((m = n)) { 3635 n = m->m_act; 3636 m_freem (m); 3637 } 3638 ifq->ifq_head = 0; 3639 ifq->ifq_tail = 0; 3640 ifq->ifq_len = 0; 3641} 3642 3643/* 3644 * Send keepalive packets, every 10 seconds. 3645 */ 3646static void 3647sppp_keepalive(void *dummy) 3648{ 3649 struct sppp *sp; 3650 int s; 3651 3652 s = splimp(); 3653 for (sp=spppq; sp; sp=sp->pp_next) { 3654 struct ifnet *ifp = &sp->pp_if; 3655 3656 /* Keepalive mode disabled or channel down? */ 3657 if (! (sp->pp_flags & PP_KEEPALIVE) || 3658 ! (ifp->if_flags & IFF_RUNNING)) 3659 continue; 3660 3661 /* No keepalive in PPP mode if LCP not opened yet. */ 3662 if (! (sp->pp_flags & PP_CISCO) && 3663 sp->pp_phase < PHASE_AUTHENTICATE) 3664 continue; 3665 3666 if (sp->pp_alivecnt == MAXALIVECNT) { 3667 /* No keepalive packets got. Stop the interface. */ 3668 printf ("%s%d: down\n", ifp->if_name, ifp->if_unit); 3669 if_down (ifp); 3670 sppp_qflush (&sp->pp_cpq); 3671 if (! (sp->pp_flags & PP_CISCO)) { 3672 /* XXX */ 3673 /* Shut down the PPP link. */ 3674 lcp.Down(sp); 3675 /* Initiate negotiation. XXX */ 3676 lcp.Up(sp); 3677 } 3678 } 3679 if (sp->pp_alivecnt <= MAXALIVECNT) 3680 ++sp->pp_alivecnt; 3681 if (sp->pp_flags & PP_CISCO) 3682 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq, 3683 sp->pp_rseq); 3684 else if (sp->pp_phase >= PHASE_AUTHENTICATE) { 3685 long nmagic = htonl (sp->lcp.magic); 3686 sp->lcp.echoid = ++sp->pp_seq; 3687 sppp_cp_send (sp, PPP_LCP, ECHO_REQ, 3688 sp->lcp.echoid, 4, &nmagic); 3689 } 3690 } 3691 splx(s); 3692 keepalive_ch = timeout(sppp_keepalive, 0, hz * 10); 3693} 3694 3695/* 3696 * Get both IP addresses. 3697 */ 3698static void 3699sppp_get_ip_addrs(struct sppp *sp, u_long *src, u_long *dst, u_long *srcmask) 3700{ 3701 struct ifnet *ifp = &sp->pp_if; 3702 struct ifaddr *ifa; 3703 struct sockaddr_in *si, *sm; 3704 u_long ssrc, ddst; 3705 3706 ssrc = ddst = 0L; 3707 /* 3708 * Pick the first AF_INET address from the list, 3709 * aliases don't make any sense on a p2p link anyway. 3710 */ 3711 for (ifa = ifp->if_addrhead.tqh_first, si = 0; 3712 ifa; 3713 ifa = ifa->ifa_link.tqe_next) 3714 if (ifa->ifa_addr->sa_family == AF_INET) { 3715 si = (struct sockaddr_in *)ifa->ifa_addr; 3716 sm = (struct sockaddr_in *)ifa->ifa_netmask; 3717 if (si) 3718 break; 3719 } 3720 if (ifa) { 3721 if (si && si->sin_addr.s_addr) { 3722 ssrc = si->sin_addr.s_addr; 3723 if (srcmask) 3724 *srcmask = ntohl(sm->sin_addr.s_addr); 3725 } 3726 3727 si = (struct sockaddr_in *)ifa->ifa_dstaddr; 3728 if (si && si->sin_addr.s_addr) 3729 ddst = si->sin_addr.s_addr; 3730 } 3731 3732 if (dst) *dst = ntohl(ddst); 3733 if (src) *src = ntohl(ssrc); 3734} 3735 3736/* 3737 * Set my IP address. Must be called at splimp. 3738 */ 3739static void 3740sppp_set_ip_addr(struct sppp *sp, u_long src) 3741{ 3742 struct ifnet *ifp = &sp->pp_if; 3743 struct ifaddr *ifa; 3744 struct sockaddr_in *si; 3745 u_long ssrc, ddst; 3746 3747 /* 3748 * Pick the first AF_INET address from the list, 3749 * aliases don't make any sense on a p2p link anyway. 3750 */ 3751 for (ifa = ifp->if_addrhead.tqh_first, si = 0; 3752 ifa; 3753 ifa = ifa->ifa_link.tqe_next) 3754 if (ifa->ifa_addr->sa_family == AF_INET) { 3755 si = (struct sockaddr_in *)ifa->ifa_addr; 3756 if (si) 3757 break; 3758 } 3759 if (ifa && si) 3760 si->sin_addr.s_addr = htonl(src); 3761} 3762 3763static int 3764sppp_params(struct sppp *sp, int cmd, void *data) 3765{ 3766 int subcmd; 3767 struct ifreq *ifr = (struct ifreq *)data; 3768 struct spppreq spr; 3769 3770 /* 3771 * ifr->ifr_data is supposed to point to a struct spppreq. 3772 * Check the cmd word first before attempting to fetch all the 3773 * data. 3774 */ 3775 if ((subcmd = fuword(ifr->ifr_data)) == -1) 3776 return EFAULT; 3777 3778 if (copyin((caddr_t)ifr->ifr_data, &spr, sizeof spr) != 0) 3779 return EFAULT; 3780 3781 switch (subcmd) { 3782 case SPPPIOGDEFS: 3783 if (cmd != SIOCGIFGENERIC) 3784 return EINVAL; 3785 /* 3786 * We copy over the entire current state, but clean 3787 * out some of the stuff we don't wanna pass up. 3788 * Remember, SIOCGIFGENERIC is unprotected, and can be 3789 * called by any user. No need to ever get PAP or 3790 * CHAP secrets back to userland anyway. 3791 */ 3792 bcopy(sp, &spr.defs, sizeof(struct sppp)); 3793 bzero(spr.defs.myauth.secret, AUTHKEYLEN); 3794 bzero(spr.defs.myauth.challenge, AUTHKEYLEN); 3795 bzero(spr.defs.hisauth.secret, AUTHKEYLEN); 3796 bzero(spr.defs.hisauth.challenge, AUTHKEYLEN); 3797 return copyout(&spr, (caddr_t)ifr->ifr_data, sizeof spr); 3798 3799 case SPPPIOSDEFS: 3800 if (cmd != SIOCSIFGENERIC) 3801 return EINVAL; 3802 /* 3803 * We have a very specific idea of which fields we allow 3804 * being passed back from userland, so to not clobber our 3805 * current state. For one, we only allow setting 3806 * anything if LCP is in dead phase. Once the LCP 3807 * negotiations started, the authentication settings must 3808 * not be changed again. (The administrator can force an 3809 * ifconfig down in order to get LCP back into dead 3810 * phase.) 3811 * 3812 * Also, we only allow for authentication parameters to be 3813 * specified. 3814 * 3815 * XXX Should allow to set or clear pp_flags. 3816 * 3817 * Finally, if the respective authentication protocol to 3818 * be used is set differently than 0, but the secret is 3819 * passed as all zeros, we don't trash the existing secret. 3820 * This allows an administrator to change the system name 3821 * only without clobbering the secret (which he didn't get 3822 * back in a previous SPPPIOGDEFS call). However, the 3823 * secrets are cleared if the authentication protocol is 3824 * reset to 0. 3825 */ 3826 if (sp->pp_phase != PHASE_DEAD) 3827 return EBUSY; 3828 3829 if ((spr.defs.myauth.proto != 0 && spr.defs.myauth.proto != PPP_PAP && 3830 spr.defs.myauth.proto != PPP_CHAP) || 3831 (spr.defs.hisauth.proto != 0 && spr.defs.hisauth.proto != PPP_PAP && 3832 spr.defs.hisauth.proto != PPP_CHAP)) 3833 return EINVAL; 3834 3835 if (spr.defs.myauth.proto == 0) 3836 /* resetting myauth */ 3837 bzero(&sp->myauth, sizeof sp->myauth); 3838 else { 3839 /* setting/changing myauth */ 3840 sp->myauth.proto = spr.defs.myauth.proto; 3841 bcopy(spr.defs.myauth.name, sp->myauth.name, AUTHNAMELEN); 3842 if (spr.defs.myauth.secret[0] != '\0') 3843 bcopy(spr.defs.myauth.secret, sp->myauth.secret, 3844 AUTHKEYLEN); 3845 } 3846 if (spr.defs.hisauth.proto == 0) 3847 /* resetting hisauth */ 3848 bzero(&sp->hisauth, sizeof sp->hisauth); 3849 else { 3850 /* setting/changing hisauth */ 3851 sp->hisauth.proto = spr.defs.hisauth.proto; 3852 sp->hisauth.flags = spr.defs.hisauth.flags; 3853 bcopy(spr.defs.hisauth.name, sp->hisauth.name, AUTHNAMELEN); 3854 if (spr.defs.hisauth.secret[0] != '\0') 3855 bcopy(spr.defs.hisauth.secret, sp->hisauth.secret, 3856 AUTHKEYLEN); 3857 } 3858 break; 3859 3860 default: 3861 return EINVAL; 3862 } 3863 3864 return 0; 3865} 3866 3867static void 3868sppp_phase_network(struct sppp *sp) 3869{ 3870 struct ifnet *ifp = &sp->pp_if; 3871 int i; 3872 u_long mask; 3873 3874 sp->pp_phase = PHASE_NETWORK; 3875 3876 log(LOG_INFO, "%s%d: phase %s\n", ifp->if_name, ifp->if_unit, 3877 sppp_phase_name(sp->pp_phase)); 3878 3879 /* Notify NCPs now. */ 3880 for (i = 0; i < IDX_COUNT; i++) 3881 if ((cps[i])->flags & CP_NCP) 3882 (cps[i])->Open(sp); 3883 3884 /* Send Up events to all NCPs. */ 3885 for (i = 0, mask = 1; i < IDX_COUNT; i++, mask <<= 1) 3886 if (sp->lcp.protos & mask && ((cps[i])->flags & CP_NCP)) 3887 (cps[i])->Up(sp); 3888 3889 /* if no NCP is starting, all this was in vain, close down */ 3890 sppp_lcp_check_and_close(sp); 3891} 3892 3893 3894static const char * 3895sppp_cp_type_name(u_char type) 3896{ 3897 static char buf[12]; 3898 switch (type) { 3899 case CONF_REQ: return "conf-req"; 3900 case CONF_ACK: return "conf-ack"; 3901 case CONF_NAK: return "conf-nak"; 3902 case CONF_REJ: return "conf-rej"; 3903 case TERM_REQ: return "term-req"; 3904 case TERM_ACK: return "term-ack"; 3905 case CODE_REJ: return "code-rej"; 3906 case PROTO_REJ: return "proto-rej"; 3907 case ECHO_REQ: return "echo-req"; 3908 case ECHO_REPLY: return "echo-reply"; 3909 case DISC_REQ: return "discard-req"; 3910 } 3911 sprintf (buf, "0x%x", type); 3912 return buf; 3913} 3914 3915static const char * 3916sppp_auth_type_name(u_short proto, u_char type) 3917{ 3918 static char buf[12]; 3919 switch (proto) { 3920 case PPP_CHAP: 3921 switch (type) { 3922 case CHAP_CHALLENGE: return "challenge"; 3923 case CHAP_RESPONSE: return "response"; 3924 case CHAP_SUCCESS: return "success"; 3925 case CHAP_FAILURE: return "failure"; 3926 } 3927 case PPP_PAP: 3928 switch (type) { 3929 case PAP_REQ: return "req"; 3930 case PAP_ACK: return "ack"; 3931 case PAP_NAK: return "nak"; 3932 } 3933 } 3934 sprintf (buf, "0x%x", type); 3935 return buf; 3936} 3937 3938static const char * 3939sppp_lcp_opt_name(u_char opt) 3940{ 3941 static char buf[12]; 3942 switch (opt) { 3943 case LCP_OPT_MRU: return "mru"; 3944 case LCP_OPT_ASYNC_MAP: return "async-map"; 3945 case LCP_OPT_AUTH_PROTO: return "auth-proto"; 3946 case LCP_OPT_QUAL_PROTO: return "qual-proto"; 3947 case LCP_OPT_MAGIC: return "magic"; 3948 case LCP_OPT_PROTO_COMP: return "proto-comp"; 3949 case LCP_OPT_ADDR_COMP: return "addr-comp"; 3950 } 3951 sprintf (buf, "0x%x", opt); 3952 return buf; 3953} 3954 3955static const char * 3956sppp_ipcp_opt_name(u_char opt) 3957{ 3958 static char buf[12]; 3959 switch (opt) { 3960 case IPCP_OPT_ADDRESSES: return "addresses"; 3961 case IPCP_OPT_COMPRESSION: return "compression"; 3962 case IPCP_OPT_ADDRESS: return "address"; 3963 } 3964 sprintf (buf, "0x%x", opt); 3965 return buf; 3966} 3967 3968static const char * 3969sppp_state_name(int state) 3970{ 3971 switch (state) { 3972 case STATE_INITIAL: return "initial"; 3973 case STATE_STARTING: return "starting"; 3974 case STATE_CLOSED: return "closed"; 3975 case STATE_STOPPED: return "stopped"; 3976 case STATE_CLOSING: return "closing"; 3977 case STATE_STOPPING: return "stopping"; 3978 case STATE_REQ_SENT: return "req-sent"; 3979 case STATE_ACK_RCVD: return "ack-rcvd"; 3980 case STATE_ACK_SENT: return "ack-sent"; 3981 case STATE_OPENED: return "opened"; 3982 } 3983 return "illegal"; 3984} 3985 3986static const char * 3987sppp_phase_name(enum ppp_phase phase) 3988{ 3989 switch (phase) { 3990 case PHASE_DEAD: return "dead"; 3991 case PHASE_ESTABLISH: return "establish"; 3992 case PHASE_TERMINATE: return "terminate"; 3993 case PHASE_AUTHENTICATE: return "authenticate"; 3994 case PHASE_NETWORK: return "network"; 3995 } 3996 return "illegal"; 3997} 3998 3999static const char * 4000sppp_proto_name(u_short proto) 4001{ 4002 static char buf[12]; 4003 switch (proto) { 4004 case PPP_LCP: return "lcp"; 4005 case PPP_IPCP: return "ipcp"; 4006 case PPP_PAP: return "pap"; 4007 case PPP_CHAP: return "chap"; 4008 } 4009 sprintf(buf, "0x%x", (unsigned)proto); 4010 return buf; 4011} 4012 4013static void 4014sppp_print_bytes(const u_char *p, u_short len) 4015{ 4016 addlog(" %x", *p++); 4017 while (--len > 0) 4018 addlog("-%x", *p++); 4019} 4020 4021static void 4022sppp_print_string(const char *p, u_short len) 4023{ 4024 u_char c; 4025 4026 while (len-- > 0) { 4027 c = *p++; 4028 /* 4029 * Print only ASCII chars directly. RFC 1994 recommends 4030 * using only them, but we don't rely on it. */ 4031 if (c < ' ' || c > '~') 4032 addlog("\\x%x", c); 4033 else 4034 addlog("%c", c); 4035 } 4036} 4037 4038static const char * 4039sppp_dotted_quad(u_long addr) 4040{ 4041 static char s[16]; 4042 sprintf(s, "%d.%d.%d.%d", 4043 (addr >> 24) & 0xff, 4044 (addr >> 16) & 0xff, 4045 (addr >> 8) & 0xff, 4046 addr & 0xff); 4047 return s; 4048} 4049 4050static int 4051sppp_strnlen(u_char *p, int max) 4052{ 4053 int len; 4054 4055 for (len = 0; len < max && *p; ++p) 4056 ++len; 4057 return len; 4058} 4059 4060/* a dummy, used to drop uninteresting events */ 4061static void 4062sppp_null(struct sppp *unused) 4063{ 4064 /* do just nothing */ 4065} 4066/* 4067 * This file is large. Tell emacs to highlight it nevertheless. 4068 * 4069 * Local Variables: 4070 * hilit-auto-highlight-maxout: 120000 4071 * End: 4072 */ 4073