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