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