1 /* 2 * IP multicast forwarding procedures 3 * 4 * Written by David Waitzman, BBN Labs, August 1988. 5 * Modified by Steve Deering, Stanford, February 1989. 6 * Modified by Mark J. Steiglitz, Stanford, May, 1991 7 * Modified by Van Jacobson, LBL, January 1993 8 * Modified by Ajit Thyagarajan, PARC, August 1993 9 * Modified by Bill Fenner, PARC, April 1995 10 * 11 * MROUTING Revision: 3.5 12 * $FreeBSD$ 13 */ 14 15 #include "opt_mrouting.h" 16 17 #include <sys/param.h> 18 #include <sys/systm.h> 19 #include <sys/malloc.h> 20 #include <sys/mbuf.h> 21 #include <sys/socket.h> 22 #include <sys/socketvar.h> 23 #include <sys/protosw.h> 24 #include <sys/time.h> 25 #include <sys/kernel.h> 26 #include <sys/sockio.h> 27 #include <sys/syslog.h> 28 #include <net/if.h> 29 #include <net/route.h> 30 #include <netinet/in.h> 31 #include <netinet/in_systm.h> 32 #include <netinet/ip.h> 33 #include <netinet/ip_var.h> 34 #include <netinet/in_var.h> 35 #include <netinet/igmp.h> 36 #include <netinet/ip_mroute.h> 37 #include <netinet/udp.h> 38 #include <machine/in_cksum.h> 39 40 #ifndef NTOHL 41 #if BYTE_ORDER != BIG_ENDIAN 42 #define NTOHL(d) ((d) = ntohl((d))) 43 #define NTOHS(d) ((d) = ntohs((u_short)(d))) 44 #define HTONL(d) ((d) = htonl((d))) 45 #define HTONS(d) ((d) = htons((u_short)(d))) 46 #else 47 #define NTOHL(d) 48 #define NTOHS(d) 49 #define HTONL(d) 50 #define HTONS(d) 51 #endif 52 #endif 53 54 #ifndef MROUTING 55 extern u_long _ip_mcast_src __P((int vifi)); 56 extern int _ip_mforward __P((struct ip *ip, struct ifnet *ifp, 57 struct mbuf *m, struct ip_moptions *imo)); 58 extern int _ip_mrouter_done __P((void)); 59 extern int _ip_mrouter_get __P((struct socket *so, struct sockopt *sopt)); 60 extern int _ip_mrouter_set __P((struct socket *so, struct sockopt *sopt)); 61 extern int _mrt_ioctl __P((int req, caddr_t data, struct proc *p)); 62 63 /* 64 * Dummy routines and globals used when multicast routing is not compiled in. 65 */ 66 67 struct socket *ip_mrouter = NULL; 68 u_int rsvpdebug = 0; 69 70 int 71 _ip_mrouter_set(so, sopt) 72 struct socket *so; 73 struct sockopt *sopt; 74 { 75 return(EOPNOTSUPP); 76 } 77 78 int (*ip_mrouter_set)(struct socket *, struct sockopt *) = _ip_mrouter_set; 79 80 81 int 82 _ip_mrouter_get(so, sopt) 83 struct socket *so; 84 struct sockopt *sopt; 85 { 86 return(EOPNOTSUPP); 87 } 88 89 int (*ip_mrouter_get)(struct socket *, struct sockopt *) = _ip_mrouter_get; 90 91 int 92 _ip_mrouter_done() 93 { 94 return(0); 95 } 96 97 int (*ip_mrouter_done)(void) = _ip_mrouter_done; 98 99 int 100 _ip_mforward(ip, ifp, m, imo) 101 struct ip *ip; 102 struct ifnet *ifp; 103 struct mbuf *m; 104 struct ip_moptions *imo; 105 { 106 return(0); 107 } 108 109 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *, 110 struct ip_moptions *) = _ip_mforward; 111 112 int 113 _mrt_ioctl(int req, caddr_t data, struct proc *p) 114 { 115 return EOPNOTSUPP; 116 } 117 118 int (*mrt_ioctl)(int, caddr_t, struct proc *) = _mrt_ioctl; 119 120 void 121 rsvp_input(m, off, proto) /* XXX must fixup manually */ 122 struct mbuf *m; 123 int off; 124 int proto; 125 { 126 /* Can still get packets with rsvp_on = 0 if there is a local member 127 * of the group to which the RSVP packet is addressed. But in this 128 * case we want to throw the packet away. 129 */ 130 if (!rsvp_on) { 131 m_freem(m); 132 return; 133 } 134 135 if (ip_rsvpd != NULL) { 136 if (rsvpdebug) 137 printf("rsvp_input: Sending packet up old-style socket\n"); 138 rip_input(m, off, proto); 139 return; 140 } 141 /* Drop the packet */ 142 m_freem(m); 143 } 144 145 void ipip_input(struct mbuf *m, int off, int proto) { /* XXX must fixup manually */ 146 rip_input(m, off, proto); 147 } 148 149 int (*legal_vif_num)(int) = 0; 150 151 /* 152 * This should never be called, since IP_MULTICAST_VIF should fail, but 153 * just in case it does get called, the code a little lower in ip_output 154 * will assign the packet a local address. 155 */ 156 u_long 157 _ip_mcast_src(int vifi) { return INADDR_ANY; } 158 u_long (*ip_mcast_src)(int) = _ip_mcast_src; 159 160 int 161 ip_rsvp_vif_init(so, sopt) 162 struct socket *so; 163 struct sockopt *sopt; 164 { 165 return(EINVAL); 166 } 167 168 int 169 ip_rsvp_vif_done(so, sopt) 170 struct socket *so; 171 struct sockopt *sopt; 172 { 173 return(EINVAL); 174 } 175 176 void 177 ip_rsvp_force_done(so) 178 struct socket *so; 179 { 180 return; 181 } 182 183 #else /* MROUTING */ 184 185 #define M_HASCL(m) ((m)->m_flags & M_EXT) 186 187 #define INSIZ sizeof(struct in_addr) 188 #define same(a1, a2) \ 189 (bcmp((caddr_t)(a1), (caddr_t)(a2), INSIZ) == 0) 190 191 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables"); 192 193 /* 194 * Globals. All but ip_mrouter and ip_mrtproto could be static, 195 * except for netstat or debugging purposes. 196 */ 197 #ifndef MROUTE_LKM 198 struct socket *ip_mrouter = NULL; 199 static struct mrtstat mrtstat; 200 #else /* MROUTE_LKM */ 201 extern void X_ipip_input __P((struct mbuf *m, int iphlen)); 202 extern struct mrtstat mrtstat; 203 static int ip_mrtproto; 204 #endif 205 206 #define NO_RTE_FOUND 0x1 207 #define RTE_FOUND 0x2 208 209 static struct mfc *mfctable[MFCTBLSIZ]; 210 static u_char nexpire[MFCTBLSIZ]; 211 static struct vif viftable[MAXVIFS]; 212 static u_int mrtdebug = 0; /* debug level */ 213 #define DEBUG_MFC 0x02 214 #define DEBUG_FORWARD 0x04 215 #define DEBUG_EXPIRE 0x08 216 #define DEBUG_XMIT 0x10 217 static u_int tbfdebug = 0; /* tbf debug level */ 218 static u_int rsvpdebug = 0; /* rsvp debug level */ 219 220 static struct callout_handle expire_upcalls_ch; 221 222 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */ 223 #define UPCALL_EXPIRE 6 /* number of timeouts */ 224 225 /* 226 * Define the token bucket filter structures 227 * tbftable -> each vif has one of these for storing info 228 */ 229 230 static struct tbf tbftable[MAXVIFS]; 231 #define TBF_REPROCESS (hz / 100) /* 100x / second */ 232 233 /* 234 * 'Interfaces' associated with decapsulator (so we can tell 235 * packets that went through it from ones that get reflected 236 * by a broken gateway). These interfaces are never linked into 237 * the system ifnet list & no routes point to them. I.e., packets 238 * can't be sent this way. They only exist as a placeholder for 239 * multicast source verification. 240 */ 241 static struct ifnet multicast_decap_if[MAXVIFS]; 242 243 #define ENCAP_TTL 64 244 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */ 245 246 /* prototype IP hdr for encapsulated packets */ 247 static struct ip multicast_encap_iphdr = { 248 #if BYTE_ORDER == LITTLE_ENDIAN 249 sizeof(struct ip) >> 2, IPVERSION, 250 #else 251 IPVERSION, sizeof(struct ip) >> 2, 252 #endif 253 0, /* tos */ 254 sizeof(struct ip), /* total length */ 255 0, /* id */ 256 0, /* frag offset */ 257 ENCAP_TTL, ENCAP_PROTO, 258 0, /* checksum */ 259 }; 260 261 /* 262 * Private variables. 263 */ 264 static vifi_t numvifs = 0; 265 static int have_encap_tunnel = 0; 266 267 /* 268 * one-back cache used by ipip_input to locate a tunnel's vif 269 * given a datagram's src ip address. 270 */ 271 static u_long last_encap_src; 272 static struct vif *last_encap_vif; 273 274 static u_long X_ip_mcast_src __P((int vifi)); 275 static int X_ip_mforward __P((struct ip *ip, struct ifnet *ifp, struct mbuf *m, struct ip_moptions *imo)); 276 static int X_ip_mrouter_done __P((void)); 277 static int X_ip_mrouter_get __P((struct socket *so, struct sockopt *m)); 278 static int X_ip_mrouter_set __P((struct socket *so, struct sockopt *m)); 279 static int X_legal_vif_num __P((int vif)); 280 static int X_mrt_ioctl __P((int cmd, caddr_t data)); 281 282 static int get_sg_cnt(struct sioc_sg_req *); 283 static int get_vif_cnt(struct sioc_vif_req *); 284 static int ip_mrouter_init(struct socket *, int); 285 static int add_vif(struct vifctl *); 286 static int del_vif(vifi_t); 287 static int add_mfc(struct mfcctl *); 288 static int del_mfc(struct mfcctl *); 289 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *); 290 static int set_assert(int); 291 static void expire_upcalls(void *); 292 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, 293 vifi_t); 294 static void phyint_send(struct ip *, struct vif *, struct mbuf *); 295 static void encap_send(struct ip *, struct vif *, struct mbuf *); 296 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long); 297 static void tbf_queue(struct vif *, struct mbuf *); 298 static void tbf_process_q(struct vif *); 299 static void tbf_reprocess_q(void *); 300 static int tbf_dq_sel(struct vif *, struct ip *); 301 static void tbf_send_packet(struct vif *, struct mbuf *); 302 static void tbf_update_tokens(struct vif *); 303 static int priority(struct vif *, struct ip *); 304 void multiencap_decap(struct mbuf *); 305 306 /* 307 * whether or not special PIM assert processing is enabled. 308 */ 309 static int pim_assert; 310 /* 311 * Rate limit for assert notification messages, in usec 312 */ 313 #define ASSERT_MSG_TIME 3000000 314 315 /* 316 * Hash function for a source, group entry 317 */ 318 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \ 319 ((g) >> 20) ^ ((g) >> 10) ^ (g)) 320 321 /* 322 * Find a route for a given origin IP address and Multicast group address 323 * Type of service parameter to be added in the future!!! 324 */ 325 326 #define MFCFIND(o, g, rt) { \ 327 register struct mfc *_rt = mfctable[MFCHASH(o,g)]; \ 328 rt = NULL; \ 329 ++mrtstat.mrts_mfc_lookups; \ 330 while (_rt) { \ 331 if ((_rt->mfc_origin.s_addr == o) && \ 332 (_rt->mfc_mcastgrp.s_addr == g) && \ 333 (_rt->mfc_stall == NULL)) { \ 334 rt = _rt; \ 335 break; \ 336 } \ 337 _rt = _rt->mfc_next; \ 338 } \ 339 if (rt == NULL) { \ 340 ++mrtstat.mrts_mfc_misses; \ 341 } \ 342 } 343 344 345 /* 346 * Macros to compute elapsed time efficiently 347 * Borrowed from Van Jacobson's scheduling code 348 */ 349 #define TV_DELTA(a, b, delta) { \ 350 register int xxs; \ 351 \ 352 delta = (a).tv_usec - (b).tv_usec; \ 353 if ((xxs = (a).tv_sec - (b).tv_sec)) { \ 354 switch (xxs) { \ 355 case 2: \ 356 delta += 1000000; \ 357 /* fall through */ \ 358 case 1: \ 359 delta += 1000000; \ 360 break; \ 361 default: \ 362 delta += (1000000 * xxs); \ 363 } \ 364 } \ 365 } 366 367 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \ 368 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec) 369 370 #ifdef UPCALL_TIMING 371 u_long upcall_data[51]; 372 static void collate(struct timeval *); 373 #endif /* UPCALL_TIMING */ 374 375 376 /* 377 * Handle MRT setsockopt commands to modify the multicast routing tables. 378 */ 379 static int 380 X_ip_mrouter_set(so, sopt) 381 struct socket *so; 382 struct sockopt *sopt; 383 { 384 int error, optval; 385 vifi_t vifi; 386 struct vifctl vifc; 387 struct mfcctl mfc; 388 389 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT) 390 return (EPERM); 391 392 error = 0; 393 switch (sopt->sopt_name) { 394 case MRT_INIT: 395 error = sooptcopyin(sopt, &optval, sizeof optval, 396 sizeof optval); 397 if (error) 398 break; 399 error = ip_mrouter_init(so, optval); 400 break; 401 402 case MRT_DONE: 403 error = ip_mrouter_done(); 404 break; 405 406 case MRT_ADD_VIF: 407 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc); 408 if (error) 409 break; 410 error = add_vif(&vifc); 411 break; 412 413 case MRT_DEL_VIF: 414 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi); 415 if (error) 416 break; 417 error = del_vif(vifi); 418 break; 419 420 case MRT_ADD_MFC: 421 case MRT_DEL_MFC: 422 error = sooptcopyin(sopt, &mfc, sizeof mfc, sizeof mfc); 423 if (error) 424 break; 425 if (sopt->sopt_name == MRT_ADD_MFC) 426 error = add_mfc(&mfc); 427 else 428 error = del_mfc(&mfc); 429 break; 430 431 case MRT_ASSERT: 432 error = sooptcopyin(sopt, &optval, sizeof optval, 433 sizeof optval); 434 if (error) 435 break; 436 set_assert(optval); 437 break; 438 439 default: 440 error = EOPNOTSUPP; 441 break; 442 } 443 return (error); 444 } 445 446 #ifndef MROUTE_LKM 447 int (*ip_mrouter_set)(struct socket *, struct sockopt *) = X_ip_mrouter_set; 448 #endif 449 450 /* 451 * Handle MRT getsockopt commands 452 */ 453 static int 454 X_ip_mrouter_get(so, sopt) 455 struct socket *so; 456 struct sockopt *sopt; 457 { 458 int error; 459 static int version = 0x0305; /* !!! why is this here? XXX */ 460 461 switch (sopt->sopt_name) { 462 case MRT_VERSION: 463 error = sooptcopyout(sopt, &version, sizeof version); 464 break; 465 466 case MRT_ASSERT: 467 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert); 468 break; 469 default: 470 error = EOPNOTSUPP; 471 break; 472 } 473 return (error); 474 } 475 476 #ifndef MROUTE_LKM 477 int (*ip_mrouter_get)(struct socket *, struct sockopt *) = X_ip_mrouter_get; 478 #endif 479 480 /* 481 * Handle ioctl commands to obtain information from the cache 482 */ 483 static int 484 X_mrt_ioctl(cmd, data) 485 int cmd; 486 caddr_t data; 487 { 488 int error = 0; 489 490 switch (cmd) { 491 case (SIOCGETVIFCNT): 492 return (get_vif_cnt((struct sioc_vif_req *)data)); 493 break; 494 case (SIOCGETSGCNT): 495 return (get_sg_cnt((struct sioc_sg_req *)data)); 496 break; 497 default: 498 return (EINVAL); 499 break; 500 } 501 return error; 502 } 503 504 #ifndef MROUTE_LKM 505 int (*mrt_ioctl)(int, caddr_t) = X_mrt_ioctl; 506 #endif 507 508 /* 509 * returns the packet, byte, rpf-failure count for the source group provided 510 */ 511 static int 512 get_sg_cnt(req) 513 register struct sioc_sg_req *req; 514 { 515 register struct mfc *rt; 516 int s; 517 518 s = splnet(); 519 MFCFIND(req->src.s_addr, req->grp.s_addr, rt); 520 splx(s); 521 if (rt != NULL) { 522 req->pktcnt = rt->mfc_pkt_cnt; 523 req->bytecnt = rt->mfc_byte_cnt; 524 req->wrong_if = rt->mfc_wrong_if; 525 } else 526 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff; 527 528 return 0; 529 } 530 531 /* 532 * returns the input and output packet and byte counts on the vif provided 533 */ 534 static int 535 get_vif_cnt(req) 536 register struct sioc_vif_req *req; 537 { 538 register vifi_t vifi = req->vifi; 539 540 if (vifi >= numvifs) return EINVAL; 541 542 req->icount = viftable[vifi].v_pkt_in; 543 req->ocount = viftable[vifi].v_pkt_out; 544 req->ibytes = viftable[vifi].v_bytes_in; 545 req->obytes = viftable[vifi].v_bytes_out; 546 547 return 0; 548 } 549 550 /* 551 * Enable multicast routing 552 */ 553 static int 554 ip_mrouter_init(so, version) 555 struct socket *so; 556 int version; 557 { 558 if (mrtdebug) 559 log(LOG_DEBUG,"ip_mrouter_init: so_type = %d, pr_protocol = %d\n", 560 so->so_type, so->so_proto->pr_protocol); 561 562 if (so->so_type != SOCK_RAW || 563 so->so_proto->pr_protocol != IPPROTO_IGMP) return EOPNOTSUPP; 564 565 if (version != 1) 566 return ENOPROTOOPT; 567 568 if (ip_mrouter != NULL) return EADDRINUSE; 569 570 ip_mrouter = so; 571 572 bzero((caddr_t)mfctable, sizeof(mfctable)); 573 bzero((caddr_t)nexpire, sizeof(nexpire)); 574 575 pim_assert = 0; 576 577 expire_upcalls_ch = timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT); 578 579 if (mrtdebug) 580 log(LOG_DEBUG, "ip_mrouter_init\n"); 581 582 return 0; 583 } 584 585 /* 586 * Disable multicast routing 587 */ 588 static int 589 X_ip_mrouter_done() 590 { 591 vifi_t vifi; 592 int i; 593 struct ifnet *ifp; 594 struct ifreq ifr; 595 struct mfc *rt; 596 struct rtdetq *rte; 597 int s; 598 599 s = splnet(); 600 601 /* 602 * For each phyint in use, disable promiscuous reception of all IP 603 * multicasts. 604 */ 605 for (vifi = 0; vifi < numvifs; vifi++) { 606 if (viftable[vifi].v_lcl_addr.s_addr != 0 && 607 !(viftable[vifi].v_flags & VIFF_TUNNEL)) { 608 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET; 609 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr 610 = INADDR_ANY; 611 ifp = viftable[vifi].v_ifp; 612 if_allmulti(ifp, 0); 613 } 614 } 615 bzero((caddr_t)tbftable, sizeof(tbftable)); 616 bzero((caddr_t)viftable, sizeof(viftable)); 617 numvifs = 0; 618 pim_assert = 0; 619 620 untimeout(expire_upcalls, (caddr_t)NULL, expire_upcalls_ch); 621 622 /* 623 * Free all multicast forwarding cache entries. 624 */ 625 for (i = 0; i < MFCTBLSIZ; i++) { 626 for (rt = mfctable[i]; rt != NULL; ) { 627 struct mfc *nr = rt->mfc_next; 628 629 for (rte = rt->mfc_stall; rte != NULL; ) { 630 struct rtdetq *n = rte->next; 631 632 m_freem(rte->m); 633 free(rte, M_MRTABLE); 634 rte = n; 635 } 636 free(rt, M_MRTABLE); 637 rt = nr; 638 } 639 } 640 641 bzero((caddr_t)mfctable, sizeof(mfctable)); 642 643 /* 644 * Reset de-encapsulation cache 645 */ 646 last_encap_src = 0; 647 last_encap_vif = NULL; 648 have_encap_tunnel = 0; 649 650 ip_mrouter = NULL; 651 652 splx(s); 653 654 if (mrtdebug) 655 log(LOG_DEBUG, "ip_mrouter_done\n"); 656 657 return 0; 658 } 659 660 #ifndef MROUTE_LKM 661 int (*ip_mrouter_done)(void) = X_ip_mrouter_done; 662 #endif 663 664 /* 665 * Set PIM assert processing global 666 */ 667 static int 668 set_assert(i) 669 int i; 670 { 671 if ((i != 1) && (i != 0)) 672 return EINVAL; 673 674 pim_assert = i; 675 676 return 0; 677 } 678 679 /* 680 * Add a vif to the vif table 681 */ 682 static int 683 add_vif(vifcp) 684 register struct vifctl *vifcp; 685 { 686 register struct vif *vifp = viftable + vifcp->vifc_vifi; 687 static struct sockaddr_in sin = {sizeof sin, AF_INET}; 688 struct ifaddr *ifa; 689 struct ifnet *ifp; 690 int error, s; 691 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi; 692 693 if (vifcp->vifc_vifi >= MAXVIFS) return EINVAL; 694 if (vifp->v_lcl_addr.s_addr != 0) return EADDRINUSE; 695 696 /* Find the interface with an address in AF_INET family */ 697 sin.sin_addr = vifcp->vifc_lcl_addr; 698 ifa = ifa_ifwithaddr((struct sockaddr *)&sin); 699 if (ifa == 0) return EADDRNOTAVAIL; 700 ifp = ifa->ifa_ifp; 701 702 if (vifcp->vifc_flags & VIFF_TUNNEL) { 703 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) { 704 /* 705 * An encapsulating tunnel is wanted. Tell ipip_input() to 706 * start paying attention to encapsulated packets. 707 */ 708 if (have_encap_tunnel == 0) { 709 have_encap_tunnel = 1; 710 for (s = 0; s < MAXVIFS; ++s) { 711 multicast_decap_if[s].if_name = "mdecap"; 712 multicast_decap_if[s].if_unit = s; 713 } 714 } 715 /* 716 * Set interface to fake encapsulator interface 717 */ 718 ifp = &multicast_decap_if[vifcp->vifc_vifi]; 719 /* 720 * Prepare cached route entry 721 */ 722 bzero(&vifp->v_route, sizeof(vifp->v_route)); 723 } else { 724 log(LOG_ERR, "source routed tunnels not supported\n"); 725 return EOPNOTSUPP; 726 } 727 } else { 728 /* Make sure the interface supports multicast */ 729 if ((ifp->if_flags & IFF_MULTICAST) == 0) 730 return EOPNOTSUPP; 731 732 /* Enable promiscuous reception of all IP multicasts from the if */ 733 s = splnet(); 734 error = if_allmulti(ifp, 1); 735 splx(s); 736 if (error) 737 return error; 738 } 739 740 s = splnet(); 741 /* define parameters for the tbf structure */ 742 vifp->v_tbf = v_tbf; 743 GET_TIME(vifp->v_tbf->tbf_last_pkt_t); 744 vifp->v_tbf->tbf_n_tok = 0; 745 vifp->v_tbf->tbf_q_len = 0; 746 vifp->v_tbf->tbf_max_q_len = MAXQSIZE; 747 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL; 748 749 vifp->v_flags = vifcp->vifc_flags; 750 vifp->v_threshold = vifcp->vifc_threshold; 751 vifp->v_lcl_addr = vifcp->vifc_lcl_addr; 752 vifp->v_rmt_addr = vifcp->vifc_rmt_addr; 753 vifp->v_ifp = ifp; 754 /* scaling up here allows division by 1024 in critical code */ 755 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000; 756 vifp->v_rsvp_on = 0; 757 vifp->v_rsvpd = NULL; 758 /* initialize per vif pkt counters */ 759 vifp->v_pkt_in = 0; 760 vifp->v_pkt_out = 0; 761 vifp->v_bytes_in = 0; 762 vifp->v_bytes_out = 0; 763 splx(s); 764 765 /* Adjust numvifs up if the vifi is higher than numvifs */ 766 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1; 767 768 if (mrtdebug) 769 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n", 770 vifcp->vifc_vifi, 771 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr), 772 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask", 773 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr), 774 vifcp->vifc_threshold, 775 vifcp->vifc_rate_limit); 776 777 return 0; 778 } 779 780 /* 781 * Delete a vif from the vif table 782 */ 783 static int 784 del_vif(vifi) 785 vifi_t vifi; 786 { 787 register struct vif *vifp = &viftable[vifi]; 788 register struct mbuf *m; 789 struct ifnet *ifp; 790 struct ifreq ifr; 791 int s; 792 793 if (vifi >= numvifs) return EINVAL; 794 if (vifp->v_lcl_addr.s_addr == 0) return EADDRNOTAVAIL; 795 796 s = splnet(); 797 798 if (!(vifp->v_flags & VIFF_TUNNEL)) { 799 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_family = AF_INET; 800 ((struct sockaddr_in *)&(ifr.ifr_addr))->sin_addr.s_addr = INADDR_ANY; 801 ifp = vifp->v_ifp; 802 if_allmulti(ifp, 0); 803 } 804 805 if (vifp == last_encap_vif) { 806 last_encap_vif = 0; 807 last_encap_src = 0; 808 } 809 810 /* 811 * Free packets queued at the interface 812 */ 813 while (vifp->v_tbf->tbf_q) { 814 m = vifp->v_tbf->tbf_q; 815 vifp->v_tbf->tbf_q = m->m_act; 816 m_freem(m); 817 } 818 819 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf))); 820 bzero((caddr_t)vifp, sizeof (*vifp)); 821 822 if (mrtdebug) 823 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs); 824 825 /* Adjust numvifs down */ 826 for (vifi = numvifs; vifi > 0; vifi--) 827 if (viftable[vifi-1].v_lcl_addr.s_addr != 0) break; 828 numvifs = vifi; 829 830 splx(s); 831 832 return 0; 833 } 834 835 /* 836 * Add an mfc entry 837 */ 838 static int 839 add_mfc(mfccp) 840 struct mfcctl *mfccp; 841 { 842 struct mfc *rt; 843 u_long hash; 844 struct rtdetq *rte; 845 register u_short nstl; 846 int s; 847 int i; 848 849 MFCFIND(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr, rt); 850 851 /* If an entry already exists, just update the fields */ 852 if (rt) { 853 if (mrtdebug & DEBUG_MFC) 854 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n", 855 (u_long)ntohl(mfccp->mfcc_origin.s_addr), 856 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr), 857 mfccp->mfcc_parent); 858 859 s = splnet(); 860 rt->mfc_parent = mfccp->mfcc_parent; 861 for (i = 0; i < numvifs; i++) 862 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i]; 863 splx(s); 864 return 0; 865 } 866 867 /* 868 * Find the entry for which the upcall was made and update 869 */ 870 s = splnet(); 871 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr); 872 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) { 873 874 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) && 875 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) && 876 (rt->mfc_stall != NULL)) { 877 878 if (nstl++) 879 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n", 880 "multiple kernel entries", 881 (u_long)ntohl(mfccp->mfcc_origin.s_addr), 882 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr), 883 mfccp->mfcc_parent, (void *)rt->mfc_stall); 884 885 if (mrtdebug & DEBUG_MFC) 886 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n", 887 (u_long)ntohl(mfccp->mfcc_origin.s_addr), 888 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr), 889 mfccp->mfcc_parent, (void *)rt->mfc_stall); 890 891 rt->mfc_origin = mfccp->mfcc_origin; 892 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp; 893 rt->mfc_parent = mfccp->mfcc_parent; 894 for (i = 0; i < numvifs; i++) 895 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i]; 896 /* initialize pkt counters per src-grp */ 897 rt->mfc_pkt_cnt = 0; 898 rt->mfc_byte_cnt = 0; 899 rt->mfc_wrong_if = 0; 900 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0; 901 902 rt->mfc_expire = 0; /* Don't clean this guy up */ 903 nexpire[hash]--; 904 905 /* free packets Qed at the end of this entry */ 906 for (rte = rt->mfc_stall; rte != NULL; ) { 907 struct rtdetq *n = rte->next; 908 909 ip_mdq(rte->m, rte->ifp, rt, -1); 910 m_freem(rte->m); 911 #ifdef UPCALL_TIMING 912 collate(&(rte->t)); 913 #endif /* UPCALL_TIMING */ 914 free(rte, M_MRTABLE); 915 rte = n; 916 } 917 rt->mfc_stall = NULL; 918 } 919 } 920 921 /* 922 * It is possible that an entry is being inserted without an upcall 923 */ 924 if (nstl == 0) { 925 if (mrtdebug & DEBUG_MFC) 926 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n", 927 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr), 928 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr), 929 mfccp->mfcc_parent); 930 931 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) { 932 933 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) && 934 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) { 935 936 rt->mfc_origin = mfccp->mfcc_origin; 937 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp; 938 rt->mfc_parent = mfccp->mfcc_parent; 939 for (i = 0; i < numvifs; i++) 940 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i]; 941 /* initialize pkt counters per src-grp */ 942 rt->mfc_pkt_cnt = 0; 943 rt->mfc_byte_cnt = 0; 944 rt->mfc_wrong_if = 0; 945 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0; 946 if (rt->mfc_expire) 947 nexpire[hash]--; 948 rt->mfc_expire = 0; 949 } 950 } 951 if (rt == NULL) { 952 /* no upcall, so make a new entry */ 953 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT); 954 if (rt == NULL) { 955 splx(s); 956 return ENOBUFS; 957 } 958 959 /* insert new entry at head of hash chain */ 960 rt->mfc_origin = mfccp->mfcc_origin; 961 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp; 962 rt->mfc_parent = mfccp->mfcc_parent; 963 for (i = 0; i < numvifs; i++) 964 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i]; 965 /* initialize pkt counters per src-grp */ 966 rt->mfc_pkt_cnt = 0; 967 rt->mfc_byte_cnt = 0; 968 rt->mfc_wrong_if = 0; 969 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0; 970 rt->mfc_expire = 0; 971 rt->mfc_stall = NULL; 972 973 /* link into table */ 974 rt->mfc_next = mfctable[hash]; 975 mfctable[hash] = rt; 976 } 977 } 978 splx(s); 979 return 0; 980 } 981 982 #ifdef UPCALL_TIMING 983 /* 984 * collect delay statistics on the upcalls 985 */ 986 static void collate(t) 987 register struct timeval *t; 988 { 989 register u_long d; 990 register struct timeval tp; 991 register u_long delta; 992 993 GET_TIME(tp); 994 995 if (TV_LT(*t, tp)) 996 { 997 TV_DELTA(tp, *t, delta); 998 999 d = delta >> 10; 1000 if (d > 50) 1001 d = 50; 1002 1003 ++upcall_data[d]; 1004 } 1005 } 1006 #endif /* UPCALL_TIMING */ 1007 1008 /* 1009 * Delete an mfc entry 1010 */ 1011 static int 1012 del_mfc(mfccp) 1013 struct mfcctl *mfccp; 1014 { 1015 struct in_addr origin; 1016 struct in_addr mcastgrp; 1017 struct mfc *rt; 1018 struct mfc **nptr; 1019 u_long hash; 1020 int s; 1021 1022 origin = mfccp->mfcc_origin; 1023 mcastgrp = mfccp->mfcc_mcastgrp; 1024 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr); 1025 1026 if (mrtdebug & DEBUG_MFC) 1027 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n", 1028 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr)); 1029 1030 s = splnet(); 1031 1032 nptr = &mfctable[hash]; 1033 while ((rt = *nptr) != NULL) { 1034 if (origin.s_addr == rt->mfc_origin.s_addr && 1035 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr && 1036 rt->mfc_stall == NULL) 1037 break; 1038 1039 nptr = &rt->mfc_next; 1040 } 1041 if (rt == NULL) { 1042 splx(s); 1043 return EADDRNOTAVAIL; 1044 } 1045 1046 *nptr = rt->mfc_next; 1047 free(rt, M_MRTABLE); 1048 1049 splx(s); 1050 1051 return 0; 1052 } 1053 1054 /* 1055 * Send a message to mrouted on the multicast routing socket 1056 */ 1057 static int 1058 socket_send(s, mm, src) 1059 struct socket *s; 1060 struct mbuf *mm; 1061 struct sockaddr_in *src; 1062 { 1063 if (s) { 1064 if (sbappendaddr(&s->so_rcv, 1065 (struct sockaddr *)src, 1066 mm, (struct mbuf *)0) != 0) { 1067 sorwakeup(s); 1068 return 0; 1069 } 1070 } 1071 m_freem(mm); 1072 return -1; 1073 } 1074 1075 /* 1076 * IP multicast forwarding function. This function assumes that the packet 1077 * pointed to by "ip" has arrived on (or is about to be sent to) the interface 1078 * pointed to by "ifp", and the packet is to be relayed to other networks 1079 * that have members of the packet's destination IP multicast group. 1080 * 1081 * The packet is returned unscathed to the caller, unless it is 1082 * erroneous, in which case a non-zero return value tells the caller to 1083 * discard it. 1084 */ 1085 1086 #define IP_HDR_LEN 20 /* # bytes of fixed IP header (excluding options) */ 1087 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */ 1088 1089 static int 1090 X_ip_mforward(ip, ifp, m, imo) 1091 register struct ip *ip; 1092 struct ifnet *ifp; 1093 struct mbuf *m; 1094 struct ip_moptions *imo; 1095 { 1096 register struct mfc *rt; 1097 register u_char *ipoptions; 1098 static struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET }; 1099 static int srctun = 0; 1100 register struct mbuf *mm; 1101 int s; 1102 vifi_t vifi; 1103 struct vif *vifp; 1104 1105 if (mrtdebug & DEBUG_FORWARD) 1106 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n", 1107 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr), 1108 (void *)ifp); 1109 1110 if (ip->ip_hl < (IP_HDR_LEN + TUNNEL_LEN) >> 2 || 1111 (ipoptions = (u_char *)(ip + 1))[1] != IPOPT_LSRR ) { 1112 /* 1113 * Packet arrived via a physical interface or 1114 * an encapsulated tunnel. 1115 */ 1116 } else { 1117 /* 1118 * Packet arrived through a source-route tunnel. 1119 * Source-route tunnels are no longer supported. 1120 */ 1121 if ((srctun++ % 1000) == 0) 1122 log(LOG_ERR, 1123 "ip_mforward: received source-routed packet from %lx\n", 1124 (u_long)ntohl(ip->ip_src.s_addr)); 1125 1126 return 1; 1127 } 1128 1129 if ((imo) && ((vifi = imo->imo_multicast_vif) < numvifs)) { 1130 if (ip->ip_ttl < 255) 1131 ip->ip_ttl++; /* compensate for -1 in *_send routines */ 1132 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) { 1133 vifp = viftable + vifi; 1134 printf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s%d)\n", 1135 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr), vifi, 1136 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "", 1137 vifp->v_ifp->if_name, vifp->v_ifp->if_unit); 1138 } 1139 return (ip_mdq(m, ifp, NULL, vifi)); 1140 } 1141 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) { 1142 printf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n", 1143 ntohl(ip->ip_src.s_addr), ntohl(ip->ip_dst.s_addr)); 1144 if(!imo) 1145 printf("In fact, no options were specified at all\n"); 1146 } 1147 1148 /* 1149 * Don't forward a packet with time-to-live of zero or one, 1150 * or a packet destined to a local-only group. 1151 */ 1152 if (ip->ip_ttl <= 1 || 1153 ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP) 1154 return 0; 1155 1156 /* 1157 * Determine forwarding vifs from the forwarding cache table 1158 */ 1159 s = splnet(); 1160 MFCFIND(ip->ip_src.s_addr, ip->ip_dst.s_addr, rt); 1161 1162 /* Entry exists, so forward if necessary */ 1163 if (rt != NULL) { 1164 splx(s); 1165 return (ip_mdq(m, ifp, rt, -1)); 1166 } else { 1167 /* 1168 * If we don't have a route for packet's origin, 1169 * Make a copy of the packet & 1170 * send message to routing daemon 1171 */ 1172 1173 register struct mbuf *mb0; 1174 register struct rtdetq *rte; 1175 register u_long hash; 1176 int hlen = ip->ip_hl << 2; 1177 #ifdef UPCALL_TIMING 1178 struct timeval tp; 1179 1180 GET_TIME(tp); 1181 #endif 1182 1183 mrtstat.mrts_no_route++; 1184 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC)) 1185 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n", 1186 (u_long)ntohl(ip->ip_src.s_addr), 1187 (u_long)ntohl(ip->ip_dst.s_addr)); 1188 1189 /* 1190 * Allocate mbufs early so that we don't do extra work if we are 1191 * just going to fail anyway. Make sure to pullup the header so 1192 * that other people can't step on it. 1193 */ 1194 rte = (struct rtdetq *)malloc((sizeof *rte), M_MRTABLE, M_NOWAIT); 1195 if (rte == NULL) { 1196 splx(s); 1197 return ENOBUFS; 1198 } 1199 mb0 = m_copy(m, 0, M_COPYALL); 1200 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen)) 1201 mb0 = m_pullup(mb0, hlen); 1202 if (mb0 == NULL) { 1203 free(rte, M_MRTABLE); 1204 splx(s); 1205 return ENOBUFS; 1206 } 1207 1208 /* is there an upcall waiting for this packet? */ 1209 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr); 1210 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) { 1211 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) && 1212 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) && 1213 (rt->mfc_stall != NULL)) 1214 break; 1215 } 1216 1217 if (rt == NULL) { 1218 int i; 1219 struct igmpmsg *im; 1220 1221 /* no upcall, so make a new entry */ 1222 rt = (struct mfc *)malloc(sizeof(*rt), M_MRTABLE, M_NOWAIT); 1223 if (rt == NULL) { 1224 free(rte, M_MRTABLE); 1225 m_freem(mb0); 1226 splx(s); 1227 return ENOBUFS; 1228 } 1229 /* Make a copy of the header to send to the user level process */ 1230 mm = m_copy(mb0, 0, hlen); 1231 if (mm == NULL) { 1232 free(rte, M_MRTABLE); 1233 m_freem(mb0); 1234 free(rt, M_MRTABLE); 1235 splx(s); 1236 return ENOBUFS; 1237 } 1238 1239 /* 1240 * Send message to routing daemon to install 1241 * a route into the kernel table 1242 */ 1243 k_igmpsrc.sin_addr = ip->ip_src; 1244 1245 im = mtod(mm, struct igmpmsg *); 1246 im->im_msgtype = IGMPMSG_NOCACHE; 1247 im->im_mbz = 0; 1248 1249 mrtstat.mrts_upcalls++; 1250 1251 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) { 1252 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n"); 1253 ++mrtstat.mrts_upq_sockfull; 1254 free(rte, M_MRTABLE); 1255 m_freem(mb0); 1256 free(rt, M_MRTABLE); 1257 splx(s); 1258 return ENOBUFS; 1259 } 1260 1261 /* insert new entry at head of hash chain */ 1262 rt->mfc_origin.s_addr = ip->ip_src.s_addr; 1263 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr; 1264 rt->mfc_expire = UPCALL_EXPIRE; 1265 nexpire[hash]++; 1266 for (i = 0; i < numvifs; i++) 1267 rt->mfc_ttls[i] = 0; 1268 rt->mfc_parent = -1; 1269 1270 /* link into table */ 1271 rt->mfc_next = mfctable[hash]; 1272 mfctable[hash] = rt; 1273 rt->mfc_stall = rte; 1274 1275 } else { 1276 /* determine if q has overflowed */ 1277 int npkts = 0; 1278 struct rtdetq **p; 1279 1280 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next) 1281 npkts++; 1282 1283 if (npkts > MAX_UPQ) { 1284 mrtstat.mrts_upq_ovflw++; 1285 free(rte, M_MRTABLE); 1286 m_freem(mb0); 1287 splx(s); 1288 return 0; 1289 } 1290 1291 /* Add this entry to the end of the queue */ 1292 *p = rte; 1293 } 1294 1295 rte->m = mb0; 1296 rte->ifp = ifp; 1297 #ifdef UPCALL_TIMING 1298 rte->t = tp; 1299 #endif 1300 rte->next = NULL; 1301 1302 splx(s); 1303 1304 return 0; 1305 } 1306 } 1307 1308 #ifndef MROUTE_LKM 1309 int (*ip_mforward)(struct ip *, struct ifnet *, struct mbuf *, 1310 struct ip_moptions *) = X_ip_mforward; 1311 #endif 1312 1313 /* 1314 * Clean up the cache entry if upcall is not serviced 1315 */ 1316 static void 1317 expire_upcalls(void *unused) 1318 { 1319 struct rtdetq *rte; 1320 struct mfc *mfc, **nptr; 1321 int i; 1322 int s; 1323 1324 s = splnet(); 1325 for (i = 0; i < MFCTBLSIZ; i++) { 1326 if (nexpire[i] == 0) 1327 continue; 1328 nptr = &mfctable[i]; 1329 for (mfc = *nptr; mfc != NULL; mfc = *nptr) { 1330 /* 1331 * Skip real cache entries 1332 * Make sure it wasn't marked to not expire (shouldn't happen) 1333 * If it expires now 1334 */ 1335 if (mfc->mfc_stall != NULL && 1336 mfc->mfc_expire != 0 && 1337 --mfc->mfc_expire == 0) { 1338 if (mrtdebug & DEBUG_EXPIRE) 1339 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n", 1340 (u_long)ntohl(mfc->mfc_origin.s_addr), 1341 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr)); 1342 /* 1343 * drop all the packets 1344 * free the mbuf with the pkt, if, timing info 1345 */ 1346 for (rte = mfc->mfc_stall; rte; ) { 1347 struct rtdetq *n = rte->next; 1348 1349 m_freem(rte->m); 1350 free(rte, M_MRTABLE); 1351 rte = n; 1352 } 1353 ++mrtstat.mrts_cache_cleanups; 1354 nexpire[i]--; 1355 1356 *nptr = mfc->mfc_next; 1357 free(mfc, M_MRTABLE); 1358 } else { 1359 nptr = &mfc->mfc_next; 1360 } 1361 } 1362 } 1363 splx(s); 1364 expire_upcalls_ch = timeout(expire_upcalls, (caddr_t)NULL, EXPIRE_TIMEOUT); 1365 } 1366 1367 /* 1368 * Packet forwarding routine once entry in the cache is made 1369 */ 1370 static int 1371 ip_mdq(m, ifp, rt, xmt_vif) 1372 register struct mbuf *m; 1373 register struct ifnet *ifp; 1374 register struct mfc *rt; 1375 register vifi_t xmt_vif; 1376 { 1377 register struct ip *ip = mtod(m, struct ip *); 1378 register vifi_t vifi; 1379 register struct vif *vifp; 1380 register int plen = ip->ip_len; 1381 1382 /* 1383 * Macro to send packet on vif. Since RSVP packets don't get counted on 1384 * input, they shouldn't get counted on output, so statistics keeping is 1385 * seperate. 1386 */ 1387 #define MC_SEND(ip,vifp,m) { \ 1388 if ((vifp)->v_flags & VIFF_TUNNEL) \ 1389 encap_send((ip), (vifp), (m)); \ 1390 else \ 1391 phyint_send((ip), (vifp), (m)); \ 1392 } 1393 1394 /* 1395 * If xmt_vif is not -1, send on only the requested vif. 1396 * 1397 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.) 1398 */ 1399 if (xmt_vif < numvifs) { 1400 MC_SEND(ip, viftable + xmt_vif, m); 1401 return 1; 1402 } 1403 1404 /* 1405 * Don't forward if it didn't arrive from the parent vif for its origin. 1406 */ 1407 vifi = rt->mfc_parent; 1408 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) { 1409 /* came in the wrong interface */ 1410 if (mrtdebug & DEBUG_FORWARD) 1411 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n", 1412 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp); 1413 ++mrtstat.mrts_wrong_if; 1414 ++rt->mfc_wrong_if; 1415 /* 1416 * If we are doing PIM assert processing, and we are forwarding 1417 * packets on this interface, and it is a broadcast medium 1418 * interface (and not a tunnel), send a message to the routing daemon. 1419 */ 1420 if (pim_assert && rt->mfc_ttls[vifi] && 1421 (ifp->if_flags & IFF_BROADCAST) && 1422 !(viftable[vifi].v_flags & VIFF_TUNNEL)) { 1423 struct sockaddr_in k_igmpsrc; 1424 struct mbuf *mm; 1425 struct igmpmsg *im; 1426 int hlen = ip->ip_hl << 2; 1427 struct timeval now; 1428 register u_long delta; 1429 1430 GET_TIME(now); 1431 1432 TV_DELTA(rt->mfc_last_assert, now, delta); 1433 1434 if (delta > ASSERT_MSG_TIME) { 1435 mm = m_copy(m, 0, hlen); 1436 if (mm && (M_HASCL(mm) || mm->m_len < hlen)) 1437 mm = m_pullup(mm, hlen); 1438 if (mm == NULL) { 1439 return ENOBUFS; 1440 } 1441 1442 rt->mfc_last_assert = now; 1443 1444 im = mtod(mm, struct igmpmsg *); 1445 im->im_msgtype = IGMPMSG_WRONGVIF; 1446 im->im_mbz = 0; 1447 im->im_vif = vifi; 1448 1449 k_igmpsrc.sin_addr = im->im_src; 1450 1451 socket_send(ip_mrouter, mm, &k_igmpsrc); 1452 } 1453 } 1454 return 0; 1455 } 1456 1457 /* If I sourced this packet, it counts as output, else it was input. */ 1458 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) { 1459 viftable[vifi].v_pkt_out++; 1460 viftable[vifi].v_bytes_out += plen; 1461 } else { 1462 viftable[vifi].v_pkt_in++; 1463 viftable[vifi].v_bytes_in += plen; 1464 } 1465 rt->mfc_pkt_cnt++; 1466 rt->mfc_byte_cnt += plen; 1467 1468 /* 1469 * For each vif, decide if a copy of the packet should be forwarded. 1470 * Forward if: 1471 * - the ttl exceeds the vif's threshold 1472 * - there are group members downstream on interface 1473 */ 1474 for (vifp = viftable, vifi = 0; vifi < numvifs; vifp++, vifi++) 1475 if ((rt->mfc_ttls[vifi] > 0) && 1476 (ip->ip_ttl > rt->mfc_ttls[vifi])) { 1477 vifp->v_pkt_out++; 1478 vifp->v_bytes_out += plen; 1479 MC_SEND(ip, vifp, m); 1480 } 1481 1482 return 0; 1483 } 1484 1485 /* 1486 * check if a vif number is legal/ok. This is used by ip_output, to export 1487 * numvifs there, 1488 */ 1489 static int 1490 X_legal_vif_num(vif) 1491 int vif; 1492 { 1493 if (vif >= 0 && vif < numvifs) 1494 return(1); 1495 else 1496 return(0); 1497 } 1498 1499 #ifndef MROUTE_LKM 1500 int (*legal_vif_num)(int) = X_legal_vif_num; 1501 #endif 1502 1503 /* 1504 * Return the local address used by this vif 1505 */ 1506 static u_long 1507 X_ip_mcast_src(vifi) 1508 int vifi; 1509 { 1510 if (vifi >= 0 && vifi < numvifs) 1511 return viftable[vifi].v_lcl_addr.s_addr; 1512 else 1513 return INADDR_ANY; 1514 } 1515 1516 #ifndef MROUTE_LKM 1517 u_long (*ip_mcast_src)(int) = X_ip_mcast_src; 1518 #endif 1519 1520 static void 1521 phyint_send(ip, vifp, m) 1522 struct ip *ip; 1523 struct vif *vifp; 1524 struct mbuf *m; 1525 { 1526 register struct mbuf *mb_copy; 1527 register int hlen = ip->ip_hl << 2; 1528 1529 /* 1530 * Make a new reference to the packet; make sure that 1531 * the IP header is actually copied, not just referenced, 1532 * so that ip_output() only scribbles on the copy. 1533 */ 1534 mb_copy = m_copy(m, 0, M_COPYALL); 1535 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen)) 1536 mb_copy = m_pullup(mb_copy, hlen); 1537 if (mb_copy == NULL) 1538 return; 1539 1540 if (vifp->v_rate_limit == 0) 1541 tbf_send_packet(vifp, mb_copy); 1542 else 1543 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len); 1544 } 1545 1546 static void 1547 encap_send(ip, vifp, m) 1548 register struct ip *ip; 1549 register struct vif *vifp; 1550 register struct mbuf *m; 1551 { 1552 register struct mbuf *mb_copy; 1553 register struct ip *ip_copy; 1554 register int i, len = ip->ip_len; 1555 1556 /* 1557 * copy the old packet & pullup its IP header into the 1558 * new mbuf so we can modify it. Try to fill the new 1559 * mbuf since if we don't the ethernet driver will. 1560 */ 1561 MGETHDR(mb_copy, M_DONTWAIT, MT_HEADER); 1562 if (mb_copy == NULL) 1563 return; 1564 mb_copy->m_data += max_linkhdr; 1565 mb_copy->m_len = sizeof(multicast_encap_iphdr); 1566 1567 if ((mb_copy->m_next = m_copy(m, 0, M_COPYALL)) == NULL) { 1568 m_freem(mb_copy); 1569 return; 1570 } 1571 i = MHLEN - M_LEADINGSPACE(mb_copy); 1572 if (i > len) 1573 i = len; 1574 mb_copy = m_pullup(mb_copy, i); 1575 if (mb_copy == NULL) 1576 return; 1577 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr); 1578 1579 /* 1580 * fill in the encapsulating IP header. 1581 */ 1582 ip_copy = mtod(mb_copy, struct ip *); 1583 *ip_copy = multicast_encap_iphdr; 1584 ip_copy->ip_id = htons(ip_id++); 1585 ip_copy->ip_len += len; 1586 ip_copy->ip_src = vifp->v_lcl_addr; 1587 ip_copy->ip_dst = vifp->v_rmt_addr; 1588 1589 /* 1590 * turn the encapsulated IP header back into a valid one. 1591 */ 1592 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr)); 1593 --ip->ip_ttl; 1594 HTONS(ip->ip_len); 1595 HTONS(ip->ip_off); 1596 ip->ip_sum = 0; 1597 mb_copy->m_data += sizeof(multicast_encap_iphdr); 1598 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2); 1599 mb_copy->m_data -= sizeof(multicast_encap_iphdr); 1600 1601 if (vifp->v_rate_limit == 0) 1602 tbf_send_packet(vifp, mb_copy); 1603 else 1604 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len); 1605 } 1606 1607 /* 1608 * De-encapsulate a packet and feed it back through ip input (this 1609 * routine is called whenever IP gets a packet with proto type 1610 * ENCAP_PROTO and a local destination address). 1611 */ 1612 void 1613 #ifdef MROUTE_LKM 1614 X_ipip_input(m, off, proto) 1615 #else 1616 ipip_input(m, off, proto) 1617 #endif 1618 register struct mbuf *m; 1619 int off; 1620 int proto; 1621 { 1622 struct ifnet *ifp = m->m_pkthdr.rcvif; 1623 register struct ip *ip = mtod(m, struct ip *); 1624 register int hlen = ip->ip_hl << 2; 1625 register struct vif *vifp; 1626 1627 if (!have_encap_tunnel) { 1628 rip_input(m, off, proto); 1629 return; 1630 } 1631 /* 1632 * dump the packet if it's not to a multicast destination or if 1633 * we don't have an encapsulating tunnel with the source. 1634 * Note: This code assumes that the remote site IP address 1635 * uniquely identifies the tunnel (i.e., that this site has 1636 * at most one tunnel with the remote site). 1637 */ 1638 if (! IN_MULTICAST(ntohl(((struct ip *)((char *)ip + hlen))->ip_dst.s_addr))) { 1639 ++mrtstat.mrts_bad_tunnel; 1640 m_freem(m); 1641 return; 1642 } 1643 if (ip->ip_src.s_addr != last_encap_src) { 1644 register struct vif *vife; 1645 1646 vifp = viftable; 1647 vife = vifp + numvifs; 1648 last_encap_src = ip->ip_src.s_addr; 1649 last_encap_vif = 0; 1650 for ( ; vifp < vife; ++vifp) 1651 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) { 1652 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT)) 1653 == VIFF_TUNNEL) 1654 last_encap_vif = vifp; 1655 break; 1656 } 1657 } 1658 if ((vifp = last_encap_vif) == 0) { 1659 last_encap_src = 0; 1660 mrtstat.mrts_cant_tunnel++; /*XXX*/ 1661 m_freem(m); 1662 if (mrtdebug) 1663 log(LOG_DEBUG, "ip_mforward: no tunnel with %lx\n", 1664 (u_long)ntohl(ip->ip_src.s_addr)); 1665 return; 1666 } 1667 ifp = vifp->v_ifp; 1668 1669 if (hlen > IP_HDR_LEN) 1670 ip_stripoptions(m, (struct mbuf *) 0); 1671 m->m_data += IP_HDR_LEN; 1672 m->m_len -= IP_HDR_LEN; 1673 m->m_pkthdr.len -= IP_HDR_LEN; 1674 m->m_pkthdr.rcvif = ifp; 1675 1676 (void) IF_HANDOFF(&ipintrq, m, NULL); 1677 /* 1678 * normally we would need a "schednetisr(NETISR_IP)" 1679 * here but we were called by ip_input and it is going 1680 * to loop back & try to dequeue the packet we just 1681 * queued as soon as we return so we avoid the 1682 * unnecessary software interrrupt. 1683 */ 1684 } 1685 1686 /* 1687 * Token bucket filter module 1688 */ 1689 1690 static void 1691 tbf_control(vifp, m, ip, p_len) 1692 register struct vif *vifp; 1693 register struct mbuf *m; 1694 register struct ip *ip; 1695 register u_long p_len; 1696 { 1697 register struct tbf *t = vifp->v_tbf; 1698 1699 if (p_len > MAX_BKT_SIZE) { 1700 /* drop if packet is too large */ 1701 mrtstat.mrts_pkt2large++; 1702 m_freem(m); 1703 return; 1704 } 1705 1706 tbf_update_tokens(vifp); 1707 1708 /* if there are enough tokens, 1709 * and the queue is empty, 1710 * send this packet out 1711 */ 1712 1713 if (t->tbf_q_len == 0) { 1714 /* queue empty, send packet if enough tokens */ 1715 if (p_len <= t->tbf_n_tok) { 1716 t->tbf_n_tok -= p_len; 1717 tbf_send_packet(vifp, m); 1718 } else { 1719 /* queue packet and timeout till later */ 1720 tbf_queue(vifp, m); 1721 timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS); 1722 } 1723 } else if (t->tbf_q_len < t->tbf_max_q_len) { 1724 /* finite queue length, so queue pkts and process queue */ 1725 tbf_queue(vifp, m); 1726 tbf_process_q(vifp); 1727 } else { 1728 /* queue length too much, try to dq and queue and process */ 1729 if (!tbf_dq_sel(vifp, ip)) { 1730 mrtstat.mrts_q_overflow++; 1731 m_freem(m); 1732 return; 1733 } else { 1734 tbf_queue(vifp, m); 1735 tbf_process_q(vifp); 1736 } 1737 } 1738 return; 1739 } 1740 1741 /* 1742 * adds a packet to the queue at the interface 1743 */ 1744 static void 1745 tbf_queue(vifp, m) 1746 register struct vif *vifp; 1747 register struct mbuf *m; 1748 { 1749 register int s = splnet(); 1750 register struct tbf *t = vifp->v_tbf; 1751 1752 if (t->tbf_t == NULL) { 1753 /* Queue was empty */ 1754 t->tbf_q = m; 1755 } else { 1756 /* Insert at tail */ 1757 t->tbf_t->m_act = m; 1758 } 1759 1760 /* Set new tail pointer */ 1761 t->tbf_t = m; 1762 1763 #ifdef DIAGNOSTIC 1764 /* Make sure we didn't get fed a bogus mbuf */ 1765 if (m->m_act) 1766 panic("tbf_queue: m_act"); 1767 #endif 1768 m->m_act = NULL; 1769 1770 t->tbf_q_len++; 1771 1772 splx(s); 1773 } 1774 1775 1776 /* 1777 * processes the queue at the interface 1778 */ 1779 static void 1780 tbf_process_q(vifp) 1781 register struct vif *vifp; 1782 { 1783 register struct mbuf *m; 1784 register int len; 1785 register int s = splnet(); 1786 register struct tbf *t = vifp->v_tbf; 1787 1788 /* loop through the queue at the interface and send as many packets 1789 * as possible 1790 */ 1791 while (t->tbf_q_len > 0) { 1792 m = t->tbf_q; 1793 1794 len = mtod(m, struct ip *)->ip_len; 1795 1796 /* determine if the packet can be sent */ 1797 if (len <= t->tbf_n_tok) { 1798 /* if so, 1799 * reduce no of tokens, dequeue the packet, 1800 * send the packet. 1801 */ 1802 t->tbf_n_tok -= len; 1803 1804 t->tbf_q = m->m_act; 1805 if (--t->tbf_q_len == 0) 1806 t->tbf_t = NULL; 1807 1808 m->m_act = NULL; 1809 tbf_send_packet(vifp, m); 1810 1811 } else break; 1812 } 1813 splx(s); 1814 } 1815 1816 static void 1817 tbf_reprocess_q(xvifp) 1818 void *xvifp; 1819 { 1820 register struct vif *vifp = xvifp; 1821 if (ip_mrouter == NULL) 1822 return; 1823 1824 tbf_update_tokens(vifp); 1825 1826 tbf_process_q(vifp); 1827 1828 if (vifp->v_tbf->tbf_q_len) 1829 timeout(tbf_reprocess_q, (caddr_t)vifp, TBF_REPROCESS); 1830 } 1831 1832 /* function that will selectively discard a member of the queue 1833 * based on the precedence value and the priority 1834 */ 1835 static int 1836 tbf_dq_sel(vifp, ip) 1837 register struct vif *vifp; 1838 register struct ip *ip; 1839 { 1840 register int s = splnet(); 1841 register u_int p; 1842 register struct mbuf *m, *last; 1843 register struct mbuf **np; 1844 register struct tbf *t = vifp->v_tbf; 1845 1846 p = priority(vifp, ip); 1847 1848 np = &t->tbf_q; 1849 last = NULL; 1850 while ((m = *np) != NULL) { 1851 if (p > priority(vifp, mtod(m, struct ip *))) { 1852 *np = m->m_act; 1853 /* If we're removing the last packet, fix the tail pointer */ 1854 if (m == t->tbf_t) 1855 t->tbf_t = last; 1856 m_freem(m); 1857 /* it's impossible for the queue to be empty, but 1858 * we check anyway. */ 1859 if (--t->tbf_q_len == 0) 1860 t->tbf_t = NULL; 1861 splx(s); 1862 mrtstat.mrts_drop_sel++; 1863 return(1); 1864 } 1865 np = &m->m_act; 1866 last = m; 1867 } 1868 splx(s); 1869 return(0); 1870 } 1871 1872 static void 1873 tbf_send_packet(vifp, m) 1874 register struct vif *vifp; 1875 register struct mbuf *m; 1876 { 1877 struct ip_moptions imo; 1878 int error; 1879 static struct route ro; 1880 int s = splnet(); 1881 1882 if (vifp->v_flags & VIFF_TUNNEL) { 1883 /* If tunnel options */ 1884 ip_output(m, (struct mbuf *)0, &vifp->v_route, 1885 IP_FORWARDING, (struct ip_moptions *)0); 1886 } else { 1887 imo.imo_multicast_ifp = vifp->v_ifp; 1888 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1; 1889 imo.imo_multicast_loop = 1; 1890 imo.imo_multicast_vif = -1; 1891 1892 /* 1893 * Re-entrancy should not be a problem here, because 1894 * the packets that we send out and are looped back at us 1895 * should get rejected because they appear to come from 1896 * the loopback interface, thus preventing looping. 1897 */ 1898 error = ip_output(m, (struct mbuf *)0, &ro, 1899 IP_FORWARDING, &imo); 1900 1901 if (mrtdebug & DEBUG_XMIT) 1902 log(LOG_DEBUG, "phyint_send on vif %d err %d\n", 1903 vifp - viftable, error); 1904 } 1905 splx(s); 1906 } 1907 1908 /* determine the current time and then 1909 * the elapsed time (between the last time and time now) 1910 * in milliseconds & update the no. of tokens in the bucket 1911 */ 1912 static void 1913 tbf_update_tokens(vifp) 1914 register struct vif *vifp; 1915 { 1916 struct timeval tp; 1917 register u_long tm; 1918 register int s = splnet(); 1919 register struct tbf *t = vifp->v_tbf; 1920 1921 GET_TIME(tp); 1922 1923 TV_DELTA(tp, t->tbf_last_pkt_t, tm); 1924 1925 /* 1926 * This formula is actually 1927 * "time in seconds" * "bytes/second". 1928 * 1929 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8) 1930 * 1931 * The (1000/1024) was introduced in add_vif to optimize 1932 * this divide into a shift. 1933 */ 1934 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8; 1935 t->tbf_last_pkt_t = tp; 1936 1937 if (t->tbf_n_tok > MAX_BKT_SIZE) 1938 t->tbf_n_tok = MAX_BKT_SIZE; 1939 1940 splx(s); 1941 } 1942 1943 static int 1944 priority(vifp, ip) 1945 register struct vif *vifp; 1946 register struct ip *ip; 1947 { 1948 register int prio; 1949 1950 /* temporary hack; may add general packet classifier some day */ 1951 1952 /* 1953 * The UDP port space is divided up into four priority ranges: 1954 * [0, 16384) : unclassified - lowest priority 1955 * [16384, 32768) : audio - highest priority 1956 * [32768, 49152) : whiteboard - medium priority 1957 * [49152, 65536) : video - low priority 1958 */ 1959 if (ip->ip_p == IPPROTO_UDP) { 1960 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2)); 1961 switch (ntohs(udp->uh_dport) & 0xc000) { 1962 case 0x4000: 1963 prio = 70; 1964 break; 1965 case 0x8000: 1966 prio = 60; 1967 break; 1968 case 0xc000: 1969 prio = 55; 1970 break; 1971 default: 1972 prio = 50; 1973 break; 1974 } 1975 if (tbfdebug > 1) 1976 log(LOG_DEBUG, "port %x prio%d\n", ntohs(udp->uh_dport), prio); 1977 } else { 1978 prio = 50; 1979 } 1980 return prio; 1981 } 1982 1983 /* 1984 * End of token bucket filter modifications 1985 */ 1986 1987 int 1988 ip_rsvp_vif_init(so, sopt) 1989 struct socket *so; 1990 struct sockopt *sopt; 1991 { 1992 int error, i, s; 1993 1994 if (rsvpdebug) 1995 printf("ip_rsvp_vif_init: so_type = %d, pr_protocol = %d\n", 1996 so->so_type, so->so_proto->pr_protocol); 1997 1998 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP) 1999 return EOPNOTSUPP; 2000 2001 /* Check mbuf. */ 2002 error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 2003 if (error) 2004 return (error); 2005 2006 if (rsvpdebug) 2007 printf("ip_rsvp_vif_init: vif = %d rsvp_on = %d\n", i, rsvp_on); 2008 2009 s = splnet(); 2010 2011 /* Check vif. */ 2012 if (!legal_vif_num(i)) { 2013 splx(s); 2014 return EADDRNOTAVAIL; 2015 } 2016 2017 /* Check if socket is available. */ 2018 if (viftable[i].v_rsvpd != NULL) { 2019 splx(s); 2020 return EADDRINUSE; 2021 } 2022 2023 viftable[i].v_rsvpd = so; 2024 /* This may seem silly, but we need to be sure we don't over-increment 2025 * the RSVP counter, in case something slips up. 2026 */ 2027 if (!viftable[i].v_rsvp_on) { 2028 viftable[i].v_rsvp_on = 1; 2029 rsvp_on++; 2030 } 2031 2032 splx(s); 2033 return 0; 2034 } 2035 2036 int 2037 ip_rsvp_vif_done(so, sopt) 2038 struct socket *so; 2039 struct sockopt *sopt; 2040 { 2041 int error, i, s; 2042 2043 if (rsvpdebug) 2044 printf("ip_rsvp_vif_done: so_type = %d, pr_protocol = %d\n", 2045 so->so_type, so->so_proto->pr_protocol); 2046 2047 if (so->so_type != SOCK_RAW || 2048 so->so_proto->pr_protocol != IPPROTO_RSVP) 2049 return EOPNOTSUPP; 2050 2051 error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 2052 if (error) 2053 return (error); 2054 2055 s = splnet(); 2056 2057 /* Check vif. */ 2058 if (!legal_vif_num(i)) { 2059 splx(s); 2060 return EADDRNOTAVAIL; 2061 } 2062 2063 if (rsvpdebug) 2064 printf("ip_rsvp_vif_done: v_rsvpd = %p so = %p\n", 2065 viftable[i].v_rsvpd, so); 2066 2067 viftable[i].v_rsvpd = NULL; 2068 /* 2069 * This may seem silly, but we need to be sure we don't over-decrement 2070 * the RSVP counter, in case something slips up. 2071 */ 2072 if (viftable[i].v_rsvp_on) { 2073 viftable[i].v_rsvp_on = 0; 2074 rsvp_on--; 2075 } 2076 2077 splx(s); 2078 return 0; 2079 } 2080 2081 void 2082 ip_rsvp_force_done(so) 2083 struct socket *so; 2084 { 2085 int vifi; 2086 register int s; 2087 2088 /* Don't bother if it is not the right type of socket. */ 2089 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP) 2090 return; 2091 2092 s = splnet(); 2093 2094 /* The socket may be attached to more than one vif...this 2095 * is perfectly legal. 2096 */ 2097 for (vifi = 0; vifi < numvifs; vifi++) { 2098 if (viftable[vifi].v_rsvpd == so) { 2099 viftable[vifi].v_rsvpd = NULL; 2100 /* This may seem silly, but we need to be sure we don't 2101 * over-decrement the RSVP counter, in case something slips up. 2102 */ 2103 if (viftable[vifi].v_rsvp_on) { 2104 viftable[vifi].v_rsvp_on = 0; 2105 rsvp_on--; 2106 } 2107 } 2108 } 2109 2110 splx(s); 2111 return; 2112 } 2113 2114 void 2115 rsvp_input(m, off, proto) 2116 struct mbuf *m; 2117 int off; 2118 int proto; 2119 { 2120 int vifi; 2121 register struct ip *ip = mtod(m, struct ip *); 2122 static struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET }; 2123 register int s; 2124 struct ifnet *ifp; 2125 2126 if (rsvpdebug) 2127 printf("rsvp_input: rsvp_on %d\n",rsvp_on); 2128 2129 /* Can still get packets with rsvp_on = 0 if there is a local member 2130 * of the group to which the RSVP packet is addressed. But in this 2131 * case we want to throw the packet away. 2132 */ 2133 if (!rsvp_on) { 2134 m_freem(m); 2135 return; 2136 } 2137 2138 s = splnet(); 2139 2140 if (rsvpdebug) 2141 printf("rsvp_input: check vifs\n"); 2142 2143 #ifdef DIAGNOSTIC 2144 if (!(m->m_flags & M_PKTHDR)) 2145 panic("rsvp_input no hdr"); 2146 #endif 2147 2148 ifp = m->m_pkthdr.rcvif; 2149 /* Find which vif the packet arrived on. */ 2150 for (vifi = 0; vifi < numvifs; vifi++) 2151 if (viftable[vifi].v_ifp == ifp) 2152 break; 2153 2154 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) { 2155 /* 2156 * If the old-style non-vif-associated socket is set, 2157 * then use it. Otherwise, drop packet since there 2158 * is no specific socket for this vif. 2159 */ 2160 if (ip_rsvpd != NULL) { 2161 if (rsvpdebug) 2162 printf("rsvp_input: Sending packet up old-style socket\n"); 2163 rip_input(m, off, proto); /* xxx */ 2164 } else { 2165 if (rsvpdebug && vifi == numvifs) 2166 printf("rsvp_input: Can't find vif for packet.\n"); 2167 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL) 2168 printf("rsvp_input: No socket defined for vif %d\n",vifi); 2169 m_freem(m); 2170 } 2171 splx(s); 2172 return; 2173 } 2174 rsvp_src.sin_addr = ip->ip_src; 2175 2176 if (rsvpdebug && m) 2177 printf("rsvp_input: m->m_len = %d, sbspace() = %ld\n", 2178 m->m_len,sbspace(&(viftable[vifi].v_rsvpd->so_rcv))); 2179 2180 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) { 2181 if (rsvpdebug) 2182 printf("rsvp_input: Failed to append to socket\n"); 2183 } else { 2184 if (rsvpdebug) 2185 printf("rsvp_input: send packet up\n"); 2186 } 2187 2188 splx(s); 2189 } 2190 2191 #ifdef MROUTE_LKM 2192 #include <sys/conf.h> 2193 #include <sys/exec.h> 2194 #include <sys/sysent.h> 2195 #include <sys/lkm.h> 2196 2197 MOD_MISC("ip_mroute_mod") 2198 2199 static int 2200 ip_mroute_mod_handle(struct lkm_table *lkmtp, int cmd) 2201 { 2202 int i; 2203 struct lkm_misc *args = lkmtp->private.lkm_misc; 2204 int err = 0; 2205 2206 switch(cmd) { 2207 static int (*old_ip_mrouter_cmd)(); 2208 static int (*old_ip_mrouter_done)(); 2209 static int (*old_ip_mforward)(); 2210 static int (*old_mrt_ioctl)(); 2211 static void (*old_proto4_input)(); 2212 static int (*old_legal_vif_num)(); 2213 extern struct protosw inetsw[]; 2214 2215 case LKM_E_LOAD: 2216 if(lkmexists(lkmtp) || ip_mrtproto) 2217 return(EEXIST); 2218 old_ip_mrouter_cmd = ip_mrouter_cmd; 2219 ip_mrouter_cmd = X_ip_mrouter_cmd; 2220 old_ip_mrouter_done = ip_mrouter_done; 2221 ip_mrouter_done = X_ip_mrouter_done; 2222 old_ip_mforward = ip_mforward; 2223 ip_mforward = X_ip_mforward; 2224 old_mrt_ioctl = mrt_ioctl; 2225 mrt_ioctl = X_mrt_ioctl; 2226 old_proto4_input = inetsw[ip_protox[ENCAP_PROTO]].pr_input; 2227 inetsw[ip_protox[ENCAP_PROTO]].pr_input = X_ipip_input; 2228 old_legal_vif_num = legal_vif_num; 2229 legal_vif_num = X_legal_vif_num; 2230 ip_mrtproto = IGMP_DVMRP; 2231 2232 printf("\nIP multicast routing loaded\n"); 2233 break; 2234 2235 case LKM_E_UNLOAD: 2236 if (ip_mrouter) 2237 return EINVAL; 2238 2239 ip_mrouter_cmd = old_ip_mrouter_cmd; 2240 ip_mrouter_done = old_ip_mrouter_done; 2241 ip_mforward = old_ip_mforward; 2242 mrt_ioctl = old_mrt_ioctl; 2243 inetsw[ip_protox[ENCAP_PROTO]].pr_input = old_proto4_input; 2244 legal_vif_num = old_legal_vif_num; 2245 ip_mrtproto = 0; 2246 break; 2247 2248 default: 2249 err = EINVAL; 2250 break; 2251 } 2252 2253 return(err); 2254 } 2255 2256 int 2257 ip_mroute_mod(struct lkm_table *lkmtp, int cmd, int ver) { 2258 DISPATCH(lkmtp, cmd, ver, ip_mroute_mod_handle, ip_mroute_mod_handle, 2259 nosys); 2260 } 2261 2262 #endif /* MROUTE_LKM */ 2263 #endif /* MROUTING */ 2264