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