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