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