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