if_spppsubr.c revision 6429
1/* 2 * Synchronous PPP/Cisco link level subroutines. 3 * Keepalive protocol implemented in both Cisco and PPP modes. 4 * 5 * Copyright (C) 1994 Cronyx Ltd. 6 * Author: Serge Vakulenko, <vak@zebub.msk.su> 7 * 8 * This software is distributed with NO WARRANTIES, not even the implied 9 * warranties for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 10 * 11 * Authors grant any other persons or organisations permission to use 12 * or modify this software as long as this message is kept with the software, 13 * all derivative works or modified versions. 14 * 15 * Version 1.1, Thu Oct 27 21:13:59 MSK 1994 16 */ 17#undef DEBUG 18 19#include <sys/param.h> 20#include <sys/systm.h> 21#include <sys/kernel.h> 22#include <sys/ioctl.h> 23#include <sys/socket.h> 24#include <sys/mbuf.h> 25 26#include <net/if.h> 27#include <net/netisr.h> 28#include <net/if_types.h> 29 30#ifdef INET 31#include <netinet/in.h> 32#include <netinet/in_systm.h> 33#include <netinet/in_var.h> 34#include <netinet/ip.h> 35#include <netinet/tcp.h> 36#include <netinet/if_ether.h> 37#endif 38 39#ifdef NS 40#include <netns/ns.h> 41#include <netns/ns_if.h> 42#endif 43 44#ifdef ISO 45#include <netiso/argo_debug.h> 46#include <netiso/iso.h> 47#include <netiso/iso_var.h> 48#include <netiso/iso_snpac.h> 49#endif 50 51#include <net/if_sppp.h> 52 53#ifdef DEBUG 54#define print(s) printf s 55#else 56#define print(s) /*void*/ 57#endif 58 59#define MAXALIVECNT 3 /* max. alive packets */ 60 61#define PPP_ALLSTATIONS 0xff /* All-Stations broadcast address */ 62#define PPP_UI 0x03 /* Unnumbered Information */ 63#define PPP_IP 0x0021 /* Internet Protocol */ 64#define PPP_ISO 0x0023 /* ISO OSI Protocol */ 65#define PPP_XNS 0x0025 /* Xerox NS Protocol */ 66#define PPP_LCP 0xc021 /* Link Control Protocol */ 67#define PPP_IPCP 0x8021 /* Internet Protocol Control Protocol */ 68 69#define LCP_CONF_REQ 1 /* PPP LCP configure request */ 70#define LCP_CONF_ACK 2 /* PPP LCP configure acknowledge */ 71#define LCP_CONF_NAK 3 /* PPP LCP configure negative ack */ 72#define LCP_CONF_REJ 4 /* PPP LCP configure reject */ 73#define LCP_TERM_REQ 5 /* PPP LCP terminate request */ 74#define LCP_TERM_ACK 6 /* PPP LCP terminate acknowledge */ 75#define LCP_CODE_REJ 7 /* PPP LCP code reject */ 76#define LCP_PROTO_REJ 8 /* PPP LCP protocol reject */ 77#define LCP_ECHO_REQ 9 /* PPP LCP echo request */ 78#define LCP_ECHO_REPLY 10 /* PPP LCP echo reply */ 79#define LCP_DISC_REQ 11 /* PPP LCP discard request */ 80 81#define LCP_OPT_MRU 1 /* maximum receive unit */ 82#define LCP_OPT_ASYNC_MAP 2 /* async control character map */ 83#define LCP_OPT_AUTH_PROTO 3 /* authentication protocol */ 84#define LCP_OPT_QUAL_PROTO 4 /* quality protocol */ 85#define LCP_OPT_MAGIC 5 /* magic number */ 86#define LCP_OPT_RESERVED 6 /* reserved */ 87#define LCP_OPT_PROTO_COMP 7 /* protocol field compression */ 88#define LCP_OPT_ADDR_COMP 8 /* address/control field compression */ 89 90#define IPCP_CONF_REQ LCP_CONF_REQ /* PPP IPCP configure request */ 91#define IPCP_CONF_ACK LCP_CONF_ACK /* PPP IPCP configure acknowledge */ 92#define IPCP_CONF_NAK LCP_CONF_NAK /* PPP IPCP configure negative ack */ 93#define IPCP_CONF_REJ LCP_CONF_REJ /* PPP IPCP configure reject */ 94#define IPCP_TERM_REQ LCP_TERM_REQ /* PPP IPCP terminate request */ 95#define IPCP_TERM_ACK LCP_TERM_ACK /* PPP IPCP terminate acknowledge */ 96#define IPCP_CODE_REJ LCP_CODE_REJ /* PPP IPCP code reject */ 97 98#define CISCO_MULTICAST 0x8f /* Cisco multicast address */ 99#define CISCO_UNICAST 0x0f /* Cisco unicast address */ 100#define CISCO_KEEPALIVE 0x8035 /* Cisco keepalive protocol */ 101#define CISCO_ADDR_REQ 0 /* Cisco address request */ 102#define CISCO_ADDR_REPLY 1 /* Cisco address reply */ 103#define CISCO_KEEPALIVE_REQ 2 /* Cisco keepalive request */ 104 105struct ppp_header { 106 unsigned char address; 107 unsigned char control; 108 unsigned short protocol; 109}; 110#define PPP_HEADER_LEN sizeof (struct ppp_header) 111 112struct lcp_header { 113 unsigned char type; 114 unsigned char ident; 115 unsigned short len; 116}; 117#define LCP_HEADER_LEN sizeof (struct lcp_header) 118 119struct cisco_packet { 120 unsigned long type; 121 unsigned long par1; 122 unsigned long par2; 123 unsigned short rel; 124 unsigned short time0; 125 unsigned short time1; 126}; 127#define CISCO_PACKET_LEN 18 128 129struct sppp *spppq; 130 131/* 132 * The following disgusting hack gets around the problem that IP TOS 133 * can't be set yet. We want to put "interactive" traffic on a high 134 * priority queue. To decide if traffic is interactive, we check that 135 * a) it is TCP and b) one of its ports is telnet, rlogin or ftp control. 136 */ 137static unsigned short interactive_ports[8] = { 138 0, 513, 0, 0, 139 0, 21, 0, 23, 140}; 141#define INTERACTIVE(p) (interactive_ports[(p) & 7] == (p)) 142 143void sppp_keepalive (void *dummy1); 144void sppp_cp_send (struct sppp *sp, unsigned short proto, unsigned char type, 145 unsigned char ident, unsigned short len, void *data); 146void sppp_cisco_send (struct sppp *sp, int type, long par1, long par2); 147void sppp_lcp_input (struct sppp *sp, struct mbuf *m); 148void sppp_cisco_input (struct sppp *sp, struct mbuf *m); 149void sppp_lcp_conf_rej (struct sppp *sp, struct lcp_header *h); 150void sppp_ipcp_input (struct sppp *sp, struct mbuf *m); 151void sppp_lcp_open (struct sppp *sp); 152void sppp_ipcp_open (struct sppp *sp); 153int sppp_lcp_conf_unknown_options (int len, unsigned char *p); 154void sppp_cp_timeout (void *arg); 155char *sppp_lcp_type_name (unsigned char type); 156char *sppp_ipcp_type_name (unsigned char type); 157void sppp_print_bytes (unsigned char *p, unsigned short len); 158 159/* 160 * Flush interface queue. 161 */ 162static void qflush (struct ifqueue *ifq) 163{ 164 struct mbuf *m, *n; 165 166 n = ifq->ifq_head; 167 while ((m = n)) { 168 n = m->m_act; 169 m_freem (m); 170 } 171 ifq->ifq_head = 0; 172 ifq->ifq_tail = 0; 173 ifq->ifq_len = 0; 174} 175 176/* 177 * Process the received packet. 178 */ 179void sppp_input (struct ifnet *ifp, struct mbuf *m) 180{ 181 struct ppp_header *h; 182 struct sppp *sp; 183 struct ifqueue *inq = 0; 184 185 ifp->if_lastchange = time; 186 if (ifp->if_flags & IFF_UP) 187 /* Count received bytes, add FCS and one flag */ 188 ifp->if_ibytes += m->m_pkthdr.len + 3; 189 190 if (m->m_pkthdr.len <= PPP_HEADER_LEN) { 191 /* Too small packet, drop it. */ 192 if (ifp->if_flags & IFF_DEBUG) 193 printf ("%s%d: input packet is too small, %d bytes\n", 194 ifp->if_name, ifp->if_unit, m->m_pkthdr.len); 195drop: ++ifp->if_iqdrops; 196 m_freem (m); 197 return; 198 } 199 200 /* Get PPP header. */ 201 h = mtod (m, struct ppp_header*); 202 m_adj (m, PPP_HEADER_LEN); 203 204 switch (h->address) { 205 default: /* Invalid PPP packet. */ 206invalid: if (ifp->if_flags & IFF_DEBUG) 207 printf ("%s%d: invalid input packet <0x%x 0x%x 0x%x>\n", 208 ifp->if_name, ifp->if_unit, 209 h->address, h->control, ntohs (h->protocol)); 210 goto drop; 211 case PPP_ALLSTATIONS: 212 if (h->control != PPP_UI) 213 goto invalid; 214 sp = (struct sppp*) ifp; 215 switch (ntohs (h->protocol)) { 216 default: 217 if (sp->lcp.state == LCP_STATE_OPENED) 218 sppp_cp_send (sp, PPP_LCP, LCP_PROTO_REJ, 219 ++sp->pp_seq, m->m_pkthdr.len - 2, 220 &h->protocol); 221 if (ifp->if_flags & IFF_DEBUG) 222 printf ("%s%d: invalid input protocol <0x%x 0x%x 0x%x>\n", 223 ifp->if_name, ifp->if_unit, 224 h->address, h->control, ntohs (h->protocol)); 225 ++ifp->if_noproto; 226 goto drop; 227 case PPP_LCP: 228 sppp_lcp_input ((struct sppp*) ifp, m); 229 m_freem (m); 230 return; 231#ifdef INET 232 case PPP_IPCP: 233 if (sp->lcp.state == LCP_STATE_OPENED) 234 sppp_ipcp_input ((struct sppp*) ifp, m); 235 m_freem (m); 236 return; 237 case PPP_IP: 238 if (sp->ipcp.state == IPCP_STATE_OPENED) { 239 schednetisr (NETISR_IP); 240 inq = &ipintrq; 241 } 242 break; 243#endif 244#ifdef NS 245 case PPP_XNS: 246 /* XNS IDPCP not implemented yet */ 247 if (sp->lcp.state == LCP_STATE_OPENED) { 248 schednetisr (NETISR_NS); 249 inq = &nsintrq; 250 } 251 break; 252#endif 253#ifdef ISO 254 case PPP_ISO: 255 /* OSI NLCP not implemented yet */ 256 if (sp->lcp.state == LCP_STATE_OPENED) { 257 schednetisr (NETISR_ISO); 258 inq = &clnlintrq; 259 } 260 break; 261#endif 262 } 263 break; 264 case CISCO_MULTICAST: 265 case CISCO_UNICAST: 266 /* Don't check the control field here (RFC 1547). */ 267 switch (ntohs (h->protocol)) { 268 default: 269 ++ifp->if_noproto; 270 goto invalid; 271 case CISCO_KEEPALIVE: 272 sppp_cisco_input ((struct sppp*) ifp, m); 273 m_freem (m); 274 return; 275#ifdef INET 276 case ETHERTYPE_IP: 277 schednetisr (NETISR_IP); 278 inq = &ipintrq; 279 break; 280#endif 281#ifdef NS 282 case ETHERTYPE_NS: 283 schednetisr (NETISR_NS); 284 inq = &nsintrq; 285 break; 286#endif 287 } 288 break; 289 } 290 291 if (! (ifp->if_flags & IFF_UP) || ! inq) 292 goto drop; 293 294 /* Check queue. */ 295 if (IF_QFULL (inq)) { 296 /* Queue overflow. */ 297 if (ifp->if_flags & IFF_DEBUG) 298 printf ("%s%d: protocol queue overflow\n", 299 ifp->if_name, ifp->if_unit); 300 IF_DROP (inq); 301 goto drop; 302 } 303 IF_ENQUEUE (inq, m); 304} 305 306/* 307 * Enqueue transmit packet. 308 */ 309int sppp_output (struct ifnet *ifp, struct mbuf *m, struct sockaddr *dst, struct rtentry *rt) 310{ 311 struct sppp *sp = (struct sppp*) ifp; 312 struct ppp_header *h; 313 struct ifqueue *ifq; 314 int s = splimp (); 315 316 if (! (ifp->if_flags & IFF_UP) || ! (ifp->if_flags & IFF_RUNNING)) { 317 m_freem (m); 318 splx (s); 319 return (ENETDOWN); 320 } 321 322 ifq = &ifp->if_snd; 323#ifdef INET 324 /* 325 * Put low delay, telnet, rlogin and ftp control packets 326 * in front of the queue. 327 */ 328 { 329 struct ip *ip = mtod (m, struct ip*); 330 struct tcphdr *tcp = (struct tcphdr*) ((long*)ip + ip->ip_hl); 331 332 if (! IF_QFULL (&sp->pp_fastq) && ((ip->ip_tos & IPTOS_LOWDELAY) || 333 ip->ip_p == IPPROTO_TCP && 334 m->m_len >= sizeof (struct ip) + sizeof (struct tcphdr) && 335 (INTERACTIVE (ntohs (tcp->th_sport)) || 336 INTERACTIVE (ntohs (tcp->th_dport))))) 337 ifq = &sp->pp_fastq; 338 } 339#endif 340 341 /* 342 * Prepend general data packet PPP header. For now, IP only. 343 */ 344 M_PREPEND (m, PPP_HEADER_LEN, M_DONTWAIT); 345 if (! m) { 346 if (ifp->if_flags & IFF_DEBUG) 347 printf ("%s%d: no memory for transmit header\n", 348 ifp->if_name, ifp->if_unit); 349 splx (s); 350 return (ENOBUFS); 351 } 352 h = mtod (m, struct ppp_header*); 353 if (sp->pp_flags & PP_CISCO) { 354 h->address = CISCO_MULTICAST; /* broadcast address */ 355 h->control = 0; 356 } else { 357 h->address = PPP_ALLSTATIONS; /* broadcast address */ 358 h->control = PPP_UI; /* Unnumbered Info */ 359 } 360 361 switch (dst->sa_family) { 362#ifdef INET 363 case AF_INET: /* Internet Protocol */ 364 h->protocol = htons ((sp->pp_flags & PP_CISCO) ? 365 ETHERTYPE_IP : PPP_IP); 366 break; 367#endif 368#ifdef NS 369 case AF_NS: /* Xerox NS Protocol */ 370 h->protocol = htons ((sp->pp_flags & PP_CISCO) ? 371 ETHERTYPE_NS : PPP_XNS); 372 break; 373#endif 374#ifdef ISO 375 case AF_ISO: /* ISO OSI Protocol */ 376 if (sp->pp_flags & PP_CISCO) 377 goto nosupport; 378 h->protocol = htons (PPP_ISO); 379 break; 380#endif 381nosupport: 382 default: 383 m_freem (m); 384 splx (s); 385 return (EAFNOSUPPORT); 386 } 387 388 /* 389 * Queue message on interface, and start output if interface 390 * not yet active. 391 */ 392 if (IF_QFULL (ifq)) { 393 IF_DROP (&ifp->if_snd); 394 m_freem (m); 395 splx (s); 396 return (ENOBUFS); 397 } 398 IF_ENQUEUE (ifq, m); 399 if (! (ifp->if_flags & IFF_OACTIVE)) 400 (*ifp->if_start) (ifp); 401 402 /* 403 * Count output packets and bytes. 404 * The packet length includes header, FCS and 1 flag, 405 * according to RFC 1333. 406 */ 407 ifp->if_obytes += m->m_pkthdr.len + 3; 408 ifp->if_lastchange = time; 409 splx (s); 410 return (0); 411} 412 413void sppp_attach (struct ifnet *ifp) 414{ 415 struct sppp *sp = (struct sppp*) ifp; 416 417 /* Initialize keepalive handler. */ 418 if (! spppq) 419 timeout (sppp_keepalive, (void *)0, hz * 10); 420 421 /* Insert new entry into the keepalive list. */ 422 sp->pp_next = spppq; 423 spppq = sp; 424 425 sp->pp_if.if_type = IFT_PPP; 426 sp->pp_if.if_output = sppp_output; 427 sp->pp_fastq.ifq_maxlen = 32; 428 sp->pp_loopcnt = 0; 429 sp->pp_alivecnt = 0; 430 sp->lcp.magic = time.tv_sec + time.tv_usec; 431 sp->lcp.rmagic = 0; 432 sp->pp_seq = sp->lcp.magic; 433 sp->pp_rseq = 0; 434} 435 436void sppp_detach (struct ifnet *ifp) 437{ 438 struct sppp **q, *p, *sp = (struct sppp*) ifp; 439 440 /* Remove the entry from the keepalive list. */ 441 for (q = &spppq; (p = *q); q = &p->pp_next) 442 if (p == sp) { 443 *q = p->pp_next; 444 break; 445 } 446 447 /* Stop keepalive handler. */ 448 if (! spppq) 449 untimeout (sppp_keepalive, (void *)0); 450 untimeout (sppp_cp_timeout, (void *)sp); 451} 452 453/* 454 * Flush the interface output queue. 455 */ 456void sppp_flush (struct ifnet *ifp) 457{ 458 struct sppp *sp = (struct sppp*) ifp; 459 460 qflush (&sp->pp_if.if_snd); 461 qflush (&sp->pp_fastq); 462} 463 464/* 465 * Get next packet to send. 466 */ 467struct mbuf *sppp_dequeue (struct ifnet *ifp) 468{ 469 struct sppp *sp = (struct sppp*) ifp; 470 struct mbuf *m; 471 int s = splimp (); 472 473 IF_DEQUEUE (&sp->pp_fastq, m); 474 if (! m) 475 IF_DEQUEUE (&sp->pp_if.if_snd, m); 476 splx (s); 477 return (m); 478} 479 480/* 481 * Send keepalive packets, every 10 seconds. 482 */ 483void 484sppp_keepalive (void *dummy1) 485{ 486 struct sppp *sp; 487 int s = splimp (); 488 489 for (sp=spppq; sp; sp=sp->pp_next) { 490 struct ifnet *ifp = &sp->pp_if; 491 492 if (! (sp->pp_flags & PP_KEEPALIVE) || 493 ! (ifp->if_flags & IFF_RUNNING) || 494 sp->lcp.state != LCP_STATE_OPENED) 495 continue; 496 497 if (sp->pp_alivecnt == MAXALIVECNT) { 498 /* No keepalive packets got. Stop the interface. */ 499 printf ("%s%d: down\n", ifp->if_name, ifp->if_unit); 500 if_down (ifp); 501 qflush (&sp->pp_fastq); 502 } 503 if (sp->pp_loopcnt >= MAXALIVECNT) 504 printf ("%s%d: loopback\n", ifp->if_name, ifp->if_unit); 505 506 if (sp->pp_alivecnt <= MAXALIVECNT) 507 ++sp->pp_alivecnt; 508 if (sp->pp_flags & PP_CISCO) 509 sppp_cisco_send (sp, CISCO_KEEPALIVE_REQ, ++sp->pp_seq, 510 sp->pp_rseq); 511 else if (sp->lcp.state == LCP_STATE_OPENED) { 512 long nmagic = htonl (sp->lcp.magic); 513 sp->lcp.lastid = ++sp->pp_seq; 514 sppp_cp_send (sp, PPP_LCP, LCP_ECHO_REQ, 515 sp->lcp.lastid, 4, &nmagic); 516 } 517 } 518 splx (s); 519 timeout (sppp_keepalive, (void *)0, hz * 10); 520} 521 522/* 523 * Handle incoming PPP Link Control Protocol packets. 524 */ 525void sppp_lcp_input (struct sppp *sp, struct mbuf *m) 526{ 527 struct lcp_header *h; 528 struct ifnet *ifp = &sp->pp_if; 529 int len = m->m_pkthdr.len; 530 unsigned char *p; 531 532 if (len < 4) { 533 if (ifp->if_flags & IFF_DEBUG) 534 printf ("%s%d: invalid lcp packet length: %d bytes\n", 535 ifp->if_name, ifp->if_unit, len); 536 return; 537 } 538 h = mtod (m, struct lcp_header*); 539 if (ifp->if_flags & IFF_DEBUG) { 540 printf ("%s%d: lcp input: %d bytes <%s id=%xh len=%xh", 541 ifp->if_name, ifp->if_unit, len, 542 sppp_lcp_type_name (h->type), h->ident, ntohs (h->len)); 543 if (len > 4) 544 sppp_print_bytes ((unsigned char*) (h+1), len-4); 545 printf (">\n"); 546 } 547 if (len > ntohs (h->len)) 548 len = ntohs (h->len); 549 switch (h->type) { 550 default: 551 /* Unknown packet type -- send Code-Reject packet. */ 552 sppp_cp_send (sp, PPP_LCP, LCP_CODE_REJ, ++sp->pp_seq, len, h); 553 break; 554 case LCP_CONF_REQ: 555 if (len < 4) { 556 if (ifp->if_flags & IFF_DEBUG) 557 printf ("%s%d: invalid lcp configure request packet length: %d bytes\n", 558 ifp->if_name, ifp->if_unit, len); 559 return; 560 } 561 if (len>4 && sppp_lcp_conf_unknown_options (len-4, (unsigned char*) (h+1))) { 562 sppp_lcp_conf_rej (sp, h); 563 if (sp->lcp.state == LCP_STATE_OPENED) 564 /* Initiate renegotiation. */ 565 sppp_lcp_open (sp); 566 if (sp->lcp.state != LCP_STATE_ACK_RCVD) { 567 /* Go to closed state. */ 568 sp->lcp.state = LCP_STATE_CLOSED; 569 sp->ipcp.state = IPCP_STATE_CLOSED; 570 } 571 } else { 572 /* Extract remote magic number. */ 573 p = (unsigned char*) (h+1); 574 if (len>=10 && p[0] == LCP_OPT_MAGIC && p[1] >= 4) 575 sp->lcp.rmagic = (unsigned long)p[2] << 24 | 576 (unsigned long)p[3] << 16 | 577 p[4] << 8 | p[5]; 578 if (sp->lcp.rmagic == sp->lcp.magic) { 579 /* Local and remote magics are equal -- loop? */ 580 sp->lcp.rmagic = ~sp->lcp.magic; 581 /* Send Configure-Nack packet. */ 582 p[2] = sp->lcp.rmagic >> 24; 583 p[3] = sp->lcp.rmagic >> 16; 584 p[4] = sp->lcp.rmagic >> 8; 585 p[5] = sp->lcp.rmagic; 586 sppp_cp_send (sp, PPP_LCP, LCP_CONF_NAK, 587 h->ident, len-4, h+1); 588 if (sp->lcp.state != LCP_STATE_ACK_RCVD) { 589 /* Go to closed state. */ 590 sp->lcp.state = LCP_STATE_CLOSED; 591 sp->ipcp.state = IPCP_STATE_CLOSED; 592 } 593 } else { 594 /* Send Configure-Ack packet. */ 595 sp->pp_loopcnt = 0; 596 sppp_cp_send (sp, PPP_LCP, LCP_CONF_ACK, 597 h->ident, len-4, h+1); 598 if (sp->lcp.state == LCP_STATE_OPENED) 599 /* Initiate renegotiation. */ 600 sppp_lcp_open (sp); 601 /* Change the state. */ 602 if (sp->lcp.state == LCP_STATE_ACK_RCVD) { 603 sp->lcp.state = LCP_STATE_OPENED; 604 sppp_ipcp_open (sp); 605 } else 606 sp->lcp.state = LCP_STATE_ACK_SENT; 607 } 608 } 609 break; 610 case LCP_CONF_ACK: 611 if (h->ident != sp->pp_seq) 612 return; 613 untimeout (sppp_cp_timeout, (void *)sp); 614 switch (sp->lcp.state) { 615 case LCP_STATE_CLOSED: 616 sp->lcp.state = LCP_STATE_ACK_RCVD; 617 break; 618 case LCP_STATE_ACK_SENT: 619 sp->lcp.state = LCP_STATE_OPENED; 620 sppp_ipcp_open (sp); 621 break; 622 case LCP_STATE_ACK_RCVD: 623 case LCP_STATE_OPENED: 624 /* Initiate renegotiation. */ 625 sppp_lcp_open (sp); 626 /* Go to closed state. */ 627 sp->lcp.state = LCP_STATE_CLOSED; 628 sp->ipcp.state = IPCP_STATE_CLOSED; 629 break; 630 } 631 break; 632 case LCP_CONF_NAK: 633 if (h->ident != sp->pp_seq) 634 return; 635 p = (unsigned char*) (h+1); 636 if (len>=10 && p[0] == LCP_OPT_MAGIC && p[1] >= 4) { 637 sp->lcp.rmagic = (unsigned long)p[2] << 24 | 638 (unsigned long)p[3] << 16 | 639 p[4] << 8 | p[5]; 640 if (sp->lcp.rmagic == ~sp->lcp.magic) { 641 if (ifp->if_flags & IFF_DEBUG) 642 printf ("%s%d: conf nak: magic glitch\n", 643 ifp->if_name, ifp->if_unit); 644 ++sp->pp_loopcnt; 645 sp->lcp.magic = time.tv_sec + time.tv_usec; 646 } 647 } 648 /* Fall through. */ 649 case LCP_CONF_REJ: 650 if (h->ident != sp->pp_seq) 651 return; 652 untimeout (sppp_cp_timeout, (void *)sp); 653 /* Initiate renegotiation. */ 654 sppp_lcp_open (sp); 655 if (sp->lcp.state != LCP_STATE_ACK_SENT) { 656 /* Go to closed state. */ 657 sp->lcp.state = LCP_STATE_CLOSED; 658 sp->ipcp.state = IPCP_STATE_CLOSED; 659 } 660 break; 661 case LCP_TERM_REQ: 662 /* Send Terminate-Ack packet. */ 663 sppp_cp_send (sp, PPP_LCP, LCP_TERM_ACK, h->ident, 0, 0); 664 if (sp->lcp.state == LCP_STATE_OPENED) 665 /* Initiate renegotiation. */ 666 sppp_lcp_open (sp); 667 /* Go to closed state. */ 668 sp->lcp.state = LCP_STATE_CLOSED; 669 sp->ipcp.state = IPCP_STATE_CLOSED; 670 break; 671 case LCP_TERM_ACK: 672 if (h->ident != sp->pp_seq) 673 return; 674 if (sp->lcp.state == LCP_STATE_OPENED) 675 /* Initiate renegotiation. */ 676 sppp_lcp_open (sp); 677 if (sp->lcp.state != LCP_STATE_ACK_SENT) { 678 /* Go to closed state. */ 679 sp->lcp.state = LCP_STATE_CLOSED; 680 sp->ipcp.state = IPCP_STATE_CLOSED; 681 } 682 break; 683 case LCP_CODE_REJ: 684 case LCP_PROTO_REJ: 685 /* Ignore for now. */ 686 break; 687 case LCP_DISC_REQ: 688 /* Discard the packet. */ 689 break; 690 case LCP_ECHO_REQ: 691 if (len < 8) { 692 if (ifp->if_flags & IFF_DEBUG) 693 printf ("%s%d: invalid lcp echo request packet length: %d bytes\n", 694 ifp->if_name, ifp->if_unit, len); 695 return; 696 } 697 if (ntohl (*(long*)(h+1)) == sp->lcp.magic) { 698 if (ifp->if_flags & IFF_DEBUG) 699 printf ("%s%d: echo reply: magic glitch\n", 700 ifp->if_name, ifp->if_unit); 701 ++sp->pp_loopcnt; 702 } 703 *(long*)(h+1) = htonl (sp->lcp.magic); 704 sppp_cp_send (sp, PPP_LCP, LCP_ECHO_REPLY, h->ident, len-4, h+1); 705 break; 706 case LCP_ECHO_REPLY: 707 if (h->ident != sp->lcp.lastid) 708 return; 709 if (len < 8) { 710 if (ifp->if_flags & IFF_DEBUG) 711 printf ("%s%d: invalid lcp echo reply packet length: %d bytes\n", 712 ifp->if_name, ifp->if_unit, len); 713 return; 714 } 715 if (ntohl (*(long*)(h+1)) == sp->lcp.magic) 716 return; 717 if (! (ifp->if_flags & IFF_UP) && 718 (ifp->if_flags & IFF_RUNNING)) { 719 ifp->if_flags |= IFF_UP; 720 printf ("%s%d: up\n", ifp->if_name, ifp->if_unit); 721 } 722 sp->pp_alivecnt = 0; 723 break; 724 } 725} 726 727/* 728 * Handle incoming Cisco keepalive protocol packets. 729 */ 730void sppp_cisco_input (struct sppp *sp, struct mbuf *m) 731{ 732 struct cisco_packet *h; 733 struct ifaddr *ifa; 734 struct ifnet *ifp = &sp->pp_if; 735 736 if (m->m_pkthdr.len != CISCO_PACKET_LEN) { 737 if (ifp->if_flags & IFF_DEBUG) 738 printf ("%s%d: invalid cisco packet length: %d bytes\n", 739 ifp->if_name, ifp->if_unit, m->m_pkthdr.len); 740 return; 741 } 742 h = mtod (m, struct cisco_packet*); 743 if (ifp->if_flags & IFF_DEBUG) 744 printf ("%s%d: cisco input: %d bytes <%xh %xh %xh %xh %xh-%xh>\n", 745 ifp->if_name, ifp->if_unit, m->m_pkthdr.len, 746 ntohl (h->type), h->par1, h->par2, h->rel, 747 h->time0, h->time1); 748 switch (ntohl (h->type)) { 749 default: 750 if (ifp->if_flags & IFF_DEBUG) 751 printf ("%s%d: unknown cisco packet type: 0x%x\n", 752 ifp->if_name, ifp->if_unit, ntohl (h->type)); 753 break; 754 case CISCO_ADDR_REPLY: 755 /* Reply on address request, ignore */ 756 break; 757 case CISCO_KEEPALIVE_REQ: 758 if (! (ifp->if_flags & IFF_UP) && 759 (ifp->if_flags & IFF_RUNNING)) { 760 ifp->if_flags |= IFF_UP; 761 printf ("%s%d: up\n", ifp->if_name, ifp->if_unit); 762 } 763 sp->pp_alivecnt = 0; 764 sp->pp_rseq = ntohl (h->par1); 765 if (sp->pp_seq == sp->pp_rseq) { 766 /* Local and remote sequence numbers are equal. 767 * Probably, the line is in loopback mode. */ 768 ++sp->pp_loopcnt; 769 770 /* Generate new local sequence number */ 771 sp->pp_seq ^= time.tv_sec ^ time.tv_usec; 772 } else 773 sp->pp_loopcnt = 0; 774 break; 775 case CISCO_ADDR_REQ: 776 for (ifa=ifp->if_addrlist; ifa; ifa=ifa->ifa_next) 777 if (ifa->ifa_addr->sa_family == AF_INET) 778 break; 779 if (! ifa) { 780 if (ifp->if_flags & IFF_DEBUG) 781 printf ("%s%d: unknown address for cisco request\n", 782 ifp->if_name, ifp->if_unit); 783 return; 784 } 785 sppp_cisco_send (sp, CISCO_ADDR_REPLY, 786 ntohl (((struct sockaddr_in*)ifa->ifa_addr)->sin_addr.s_addr), 787 ntohl (((struct sockaddr_in*)ifa->ifa_netmask)->sin_addr.s_addr)); 788 break; 789 } 790} 791 792/* 793 * Send PPP LCP packet. 794 */ 795void sppp_cp_send (struct sppp *sp, unsigned short proto, unsigned char type, 796 unsigned char ident, unsigned short len, void *data) 797{ 798 struct ppp_header *h; 799 struct lcp_header *lh; 800 struct mbuf *m; 801 struct ifnet *ifp = &sp->pp_if; 802 803 if (len > MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN) 804 len = MHLEN - PPP_HEADER_LEN - LCP_HEADER_LEN; 805 MGETHDR (m, M_DONTWAIT, MT_DATA); 806 if (! m) 807 return; 808 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + LCP_HEADER_LEN + len; 809 m->m_pkthdr.rcvif = 0; 810 811 h = mtod (m, struct ppp_header*); 812 h->address = PPP_ALLSTATIONS; /* broadcast address */ 813 h->control = PPP_UI; /* Unnumbered Info */ 814 h->protocol = htons (proto); /* Link Control Protocol */ 815 816 lh = (struct lcp_header*) (h + 1); 817 lh->type = type; 818 lh->ident = ident; 819 lh->len = htons (LCP_HEADER_LEN + len); 820 if (len) 821 bcopy (data, lh+1, len); 822 823 if (ifp->if_flags & IFF_DEBUG) { 824 printf ("%s%d: %s output <%s id=%xh len=%xh", 825 ifp->if_name, ifp->if_unit, 826 proto==PPP_LCP ? "lcp" : "ipcp", 827 proto==PPP_LCP ? sppp_lcp_type_name (lh->type) : 828 sppp_ipcp_type_name (lh->type), lh->ident, 829 ntohs (lh->len)); 830 if (len) 831 sppp_print_bytes ((unsigned char*) (lh+1), len); 832 printf (">\n"); 833 } 834 if (IF_QFULL (&sp->pp_fastq)) { 835 IF_DROP (&ifp->if_snd); 836 m_freem (m); 837 } else 838 IF_ENQUEUE (&sp->pp_fastq, m); 839 if (! (ifp->if_flags & IFF_OACTIVE)) 840 (*ifp->if_start) (ifp); 841 ifp->if_obytes += m->m_pkthdr.len + 3; 842} 843 844/* 845 * Send Cisco keepalive packet. 846 */ 847void sppp_cisco_send (struct sppp *sp, int type, long par1, long par2) 848{ 849 struct ppp_header *h; 850 struct cisco_packet *ch; 851 struct mbuf *m; 852 struct ifnet *ifp = &sp->pp_if; 853 unsigned long t = (time.tv_sec - boottime.tv_sec) * 1000; 854 855 MGETHDR (m, M_DONTWAIT, MT_DATA); 856 if (! m) 857 return; 858 m->m_pkthdr.len = m->m_len = PPP_HEADER_LEN + CISCO_PACKET_LEN; 859 m->m_pkthdr.rcvif = 0; 860 861 h = mtod (m, struct ppp_header*); 862 h->address = CISCO_MULTICAST; 863 h->control = 0; 864 h->protocol = htons (CISCO_KEEPALIVE); 865 866 ch = (struct cisco_packet*) (h + 1); 867 ch->type = htonl (type); 868 ch->par1 = htonl (par1); 869 ch->par2 = htonl (par2); 870 ch->rel = -1; 871 ch->time0 = htons ((unsigned short) (t >> 16)); 872 ch->time1 = htons ((unsigned short) t); 873 874 if (ifp->if_flags & IFF_DEBUG) 875 printf ("%s%d: cisco output: <%xh %xh %xh %xh %xh-%xh>\n", 876 ifp->if_name, ifp->if_unit, ntohl (ch->type), ch->par1, 877 ch->par2, ch->rel, ch->time0, ch->time1); 878 879 if (IF_QFULL (&sp->pp_fastq)) { 880 IF_DROP (&ifp->if_snd); 881 m_freem (m); 882 } else 883 IF_ENQUEUE (&sp->pp_fastq, m); 884 if (! (ifp->if_flags & IFF_OACTIVE)) 885 (*ifp->if_start) (ifp); 886 ifp->if_obytes += m->m_pkthdr.len + 3; 887} 888 889/* 890 * Process an ioctl request. Called on low priority level. 891 */ 892int sppp_ioctl (struct ifnet *ifp, int cmd, caddr_t data) 893{ 894 struct ifreq *ifr = (struct ifreq*) data; 895 struct sppp *sp; 896 int s; 897 898 switch (cmd) { 899 default: 900 return (EINVAL); 901 902 case SIOCSIFADDR: 903 case SIOCAIFADDR: 904 case SIOCSIFDSTADDR: 905 break; 906 907 case SIOCSIFFLAGS: 908 s = splimp (); 909 if (! (ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING)) { 910 /* Interface is stopping. */ 911 sp = (struct sppp*) ifp; 912 sp->lcp.state = LCP_STATE_CLOSED; 913 sp->ipcp.state = IPCP_STATE_CLOSED; 914 sppp_cp_send (sp, PPP_LCP, LCP_TERM_REQ, ++sp->pp_seq, 915 0, 0); 916 } else if ((ifp->if_flags & IFF_UP) && ! (ifp->if_flags & IFF_RUNNING)) { 917 /* Interface is starting. */ 918 sp = (struct sppp*) ifp; 919 sp->lcp.state = LCP_STATE_CLOSED; 920 sp->ipcp.state = IPCP_STATE_CLOSED; 921 sppp_lcp_open (sp); 922 } 923 splx (s); 924 break; 925 926#ifdef SIOCSIFMTU 927#ifndef ifr_mtu 928#define ifr_mtu ifr_metric 929#endif 930 case SIOCSIFMTU: 931 if (ifr->ifr_mtu < 128 || ifr->ifr_mtu > PP_MTU) 932 return (EINVAL); 933 ifp->if_mtu = ifr->ifr_mtu; 934 break; 935#endif 936#ifdef SLIOCSETMTU 937 case SLIOCSETMTU: 938 if (*(short*)data < 128 || *(short*)data > PP_MTU) 939 return (EINVAL); 940 ifp->if_mtu = *(short*)data; 941 break; 942#endif 943#ifdef SIOCGIFMTU 944 case SIOCGIFMTU: 945 ifr->ifr_mtu = ifp->if_mtu; 946 break; 947#endif 948#ifdef SLIOCGETMTU 949 case SLIOCGETMTU: 950 *(short*)data = ifp->if_mtu; 951 break; 952#endif 953#ifdef MULTICAST 954 case SIOCADDMULTI: 955 case SIOCDELMULTI: 956 break; 957#endif 958 } 959 return (0); 960} 961 962int sppp_lcp_conf_unknown_options (int len, unsigned char *p) 963{ 964 /* Analyze the LCP Configure-Request options list 965 * for the presence of unknown options. */ 966 while (len > 0) { 967 if (*p != LCP_OPT_MAGIC) 968 return (1); 969 len -= p[1]; 970 p += p[1]; 971 } 972 return (0); 973} 974 975void sppp_lcp_conf_rej (struct sppp *sp, struct lcp_header *h) 976{ 977 /* The LCP Configure-Request contains unknown options. 978 * Send Configure-reject packet, containing only unknown options. */ 979 unsigned char buf [PP_MTU], *r = buf, *p = (void*) (h+1); 980 unsigned rlen = 0, len = h->len - 4; 981 982 while (len > 0) { 983 if (*p != LCP_OPT_MAGIC) { 984 bcopy (p, r, p[1]); 985 r += p[1]; 986 } 987 len -= p[1]; 988 p += p[1]; 989 } 990 sppp_cp_send (sp, PPP_LCP, LCP_CONF_REJ, h->ident, rlen, buf); 991} 992 993void sppp_ipcp_input (struct sppp *sp, struct mbuf *m) 994{ 995 struct lcp_header *h; 996 struct ifnet *ifp = &sp->pp_if; 997 int len = m->m_pkthdr.len; 998 999 if (len < 4) { 1000 /* if (ifp->if_flags & IFF_DEBUG) */ 1001 printf ("%s%d: invalid ipcp packet length: %d bytes\n", 1002 ifp->if_name, ifp->if_unit, len); 1003 return; 1004 } 1005 h = mtod (m, struct lcp_header*); 1006 if (ifp->if_flags & IFF_DEBUG) { 1007 printf ("%s%d: ipcp input: %d bytes <%s id=%xh len=%xh", 1008 ifp->if_name, ifp->if_unit, len, 1009 sppp_ipcp_type_name (h->type), h->ident, ntohs (h->len)); 1010 if (len > 4) 1011 sppp_print_bytes ((unsigned char*) (h+1), len-4); 1012 printf (">\n"); 1013 } 1014 if (len > ntohs (h->len)) 1015 len = ntohs (h->len); 1016 switch (h->type) { 1017 default: 1018 /* Unknown packet type -- send Code-Reject packet. */ 1019 sppp_cp_send (sp, PPP_IPCP, IPCP_CODE_REJ, ++sp->pp_seq, len, h); 1020 break; 1021 case IPCP_CONF_REQ: 1022 if (len < 4) { 1023 if (ifp->if_flags & IFF_DEBUG) 1024 printf ("%s%d: invalid ipcp configure request packet length: %d bytes\n", 1025 ifp->if_name, ifp->if_unit, len); 1026 return; 1027 } 1028 if (len > 4) { 1029 sppp_cp_send (sp, PPP_IPCP, LCP_CONF_REJ, h->ident, 1030 len-4, h+1); 1031 if (sp->lcp.state == LCP_STATE_OPENED && 1032 sp->ipcp.state == IPCP_STATE_OPENED) 1033 /* Initiate renegotiation. */ 1034 sppp_ipcp_open (sp); 1035 if (sp->ipcp.state != IPCP_STATE_ACK_RCVD) 1036 /* Go to closed state. */ 1037 sp->ipcp.state = IPCP_STATE_CLOSED; 1038 } else { 1039 /* Send Configure-Ack packet. */ 1040 sppp_cp_send (sp, PPP_IPCP, IPCP_CONF_ACK, h->ident, 1041 0, 0); 1042 if (sp->lcp.state == LCP_STATE_OPENED && 1043 sp->ipcp.state == IPCP_STATE_OPENED) 1044 /* Initiate renegotiation. */ 1045 sppp_ipcp_open (sp); 1046 /* Change the state. */ 1047 sp->ipcp.state = (sp->ipcp.state == IPCP_STATE_ACK_RCVD) ? 1048 IPCP_STATE_OPENED : IPCP_STATE_ACK_SENT; 1049 } 1050 break; 1051 case IPCP_CONF_ACK: 1052 untimeout (sppp_cp_timeout, (void *)sp); 1053 switch (sp->ipcp.state) { 1054 case IPCP_STATE_CLOSED: 1055 sp->ipcp.state = IPCP_STATE_ACK_RCVD; 1056 break; 1057 case IPCP_STATE_ACK_SENT: 1058 sp->ipcp.state = IPCP_STATE_OPENED; 1059 break; 1060 case IPCP_STATE_ACK_RCVD: 1061 case IPCP_STATE_OPENED: 1062 if (sp->lcp.state == LCP_STATE_OPENED) 1063 /* Initiate renegotiation. */ 1064 sppp_ipcp_open (sp); 1065 /* Go to closed state. */ 1066 sp->ipcp.state = IPCP_STATE_CLOSED; 1067 break; 1068 } 1069 break; 1070 case IPCP_CONF_NAK: 1071 case IPCP_CONF_REJ: 1072 untimeout (sppp_cp_timeout, (void *)sp); 1073 /* Initiate renegotiation. */ 1074 sppp_ipcp_open (sp); 1075 if (sp->lcp.state == LCP_STATE_OPENED) 1076 /* Initiate renegotiation. */ 1077 sppp_ipcp_open (sp); 1078 if (sp->ipcp.state != IPCP_STATE_ACK_SENT) 1079 /* Go to closed state. */ 1080 sp->ipcp.state = IPCP_STATE_CLOSED; 1081 break; 1082 case IPCP_TERM_REQ: 1083 /* Send Terminate-Ack packet. */ 1084 sppp_cp_send (sp, PPP_IPCP, IPCP_TERM_ACK, h->ident, 0, 0); 1085 if (sp->lcp.state == LCP_STATE_OPENED && 1086 sp->ipcp.state == IPCP_STATE_OPENED) 1087 /* Initiate renegotiation. */ 1088 sppp_ipcp_open (sp); 1089 /* Go to closed state. */ 1090 sp->ipcp.state = IPCP_STATE_CLOSED; 1091 break; 1092 case IPCP_TERM_ACK: 1093 if (sp->lcp.state == LCP_STATE_OPENED && 1094 sp->ipcp.state == IPCP_STATE_OPENED) 1095 /* Initiate renegotiation. */ 1096 sppp_ipcp_open (sp); 1097 if (sp->ipcp.state != IPCP_STATE_ACK_SENT) 1098 /* Go to closed state. */ 1099 sp->ipcp.state = IPCP_STATE_CLOSED; 1100 break; 1101 case IPCP_CODE_REJ: 1102 /* Ignore for now. */ 1103 break; 1104 } 1105} 1106 1107void sppp_lcp_open (struct sppp *sp) 1108{ 1109 char opt[6]; 1110 1111 /* Make new magic number. */ 1112 sp->lcp.magic = time.tv_sec + time.tv_usec; 1113 opt[0] = LCP_OPT_MAGIC; 1114 opt[1] = sizeof (opt); 1115 opt[2] = sp->lcp.magic >> 24; 1116 opt[3] = sp->lcp.magic >> 16; 1117 opt[4] = sp->lcp.magic >> 8; 1118 opt[5] = sp->lcp.magic; 1119 sppp_cp_send (sp, PPP_LCP, LCP_CONF_REQ, ++sp->pp_seq, 1120 sizeof (opt), &opt); 1121 timeout (sppp_cp_timeout, (void *)sp, hz * 5); 1122} 1123 1124void sppp_ipcp_open (struct sppp *sp) 1125{ 1126 sppp_cp_send (sp, PPP_IPCP, IPCP_CONF_REQ, ++sp->pp_seq, 0, 0); 1127 timeout (sppp_cp_timeout, (void *)sp, hz * 5); 1128} 1129 1130/* 1131 * Process PPP control protocol timeouts. 1132 */ 1133void 1134sppp_cp_timeout (void * arg) 1135{ 1136 struct sppp *sp = (struct sppp*) arg; 1137 struct ifnet *ifp = &sp->pp_if; 1138 int s = splimp (); 1139 1140 switch (sp->lcp.state) { 1141 case LCP_STATE_CLOSED: 1142 /* No ACK for Configure-Request, retry. */ 1143 sppp_lcp_open (sp); 1144 break; 1145 case LCP_STATE_ACK_RCVD: 1146 /* ACK got, but no Configure-Request for peer, retry. */ 1147 sppp_lcp_open (sp); 1148 sp->lcp.state = LCP_STATE_CLOSED; 1149 break; 1150 case LCP_STATE_ACK_SENT: 1151 /* ACK sent but no ACK for Configure-Request, retry. */ 1152 sppp_lcp_open (sp); 1153 break; 1154 case LCP_STATE_OPENED: 1155 /* LCP is already OK, try IPCP. */ 1156 switch (sp->ipcp.state) { 1157 case IPCP_STATE_CLOSED: 1158 /* No ACK for Configure-Request, retry. */ 1159 sppp_ipcp_open (sp); 1160 break; 1161 case IPCP_STATE_ACK_RCVD: 1162 /* ACK got, but no Configure-Request for peer, retry. */ 1163 sppp_ipcp_open (sp); 1164 sp->ipcp.state = IPCP_STATE_CLOSED; 1165 break; 1166 case IPCP_STATE_ACK_SENT: 1167 /* ACK sent but no ACK for Configure-Request, retry. */ 1168 sppp_ipcp_open (sp); 1169 break; 1170 case IPCP_STATE_OPENED: 1171 /* IPCP is OK. */ 1172 break; 1173 } 1174 break; 1175 } 1176 splx (s); 1177} 1178 1179char *sppp_lcp_type_name (unsigned char type) 1180{ 1181 static char buf [8]; 1182 switch (type) { 1183 case LCP_CONF_REQ: return ("conf-req"); 1184 case LCP_CONF_ACK: return ("conf-ack"); 1185 case LCP_CONF_NAK: return ("conf-nack"); 1186 case LCP_CONF_REJ: return ("conf-rej"); 1187 case LCP_TERM_REQ: return ("term-req"); 1188 case LCP_TERM_ACK: return ("term-ack"); 1189 case LCP_CODE_REJ: return ("code-rej"); 1190 case LCP_PROTO_REJ: return ("proto-rej"); 1191 case LCP_ECHO_REQ: return ("echo-req"); 1192 case LCP_ECHO_REPLY: return ("echo-reply"); 1193 case LCP_DISC_REQ: return ("discard-req"); 1194 } 1195 sprintf (buf, "%xh", type); 1196 return (buf); 1197} 1198 1199char *sppp_ipcp_type_name (unsigned char type) 1200{ 1201 static char buf [8]; 1202 switch (type) { 1203 case IPCP_CONF_REQ: return ("conf-req"); 1204 case IPCP_CONF_ACK: return ("conf-ack"); 1205 case IPCP_CONF_NAK: return ("conf-nack"); 1206 case IPCP_CONF_REJ: return ("conf-rej"); 1207 case IPCP_TERM_REQ: return ("term-req"); 1208 case IPCP_TERM_ACK: return ("term-ack"); 1209 case IPCP_CODE_REJ: return ("code-rej"); 1210 } 1211 sprintf (buf, "%xh", type); 1212 return (buf); 1213} 1214 1215void sppp_print_bytes (unsigned char *p, unsigned short len) 1216{ 1217 printf (" %x", *p++); 1218 while (--len > 0) 1219 printf ("-%x", *p++); 1220} 1221