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 * Modified by Ahmed Helmy, SGI, June 1996 11 * Modified by George Edmond Eddy (Rusty), ISI, February 1998 12 * Modified by Pavlin Radoslavov, USC/ISI, May 1998, August 1999, October 2000 13 * Modified by Hitoshi Asaeda, WIDE, August 2000 14 * Modified by Pavlin Radoslavov, ICSI, October 2002 15 * 16 * MROUTING Revision: 3.5 17 * and PIM-SMv2 and PIM-DM support, advanced API support, 18 * bandwidth metering and signaling 19 * 20 * $FreeBSD: src/sys/netinet/ip_mroute.c,v 1.56.2.10 2003/08/24 21:37:34 hsu Exp $ 21 * $DragonFly: src/sys/net/ip_mroute/ip_mroute.c,v 1.22 2007/04/22 01:13:13 dillon Exp $ 22 */ 23 24 #include "opt_mrouting.h" 25 26 #ifdef PIM 27 #define _PIM_VT 1 28 #endif 29 30 #include <sys/param.h> 31 #include <sys/kernel.h> 32 #include <sys/malloc.h> 33 #include <sys/mbuf.h> 34 #include <sys/protosw.h> 35 #include <sys/socket.h> 36 #include <sys/socketvar.h> 37 #include <sys/sockio.h> 38 #include <sys/sysctl.h> 39 #include <sys/syslog.h> 40 #include <sys/systm.h> 41 #include <sys/thread2.h> 42 #include <sys/time.h> 43 #include <sys/in_cksum.h> 44 45 #include <machine/stdarg.h> 46 47 #include <net/if.h> 48 #include <net/netisr.h> 49 #include <net/route.h> 50 #include <netinet/in.h> 51 #include <netinet/igmp.h> 52 #include <netinet/in_systm.h> 53 #include <netinet/in_var.h> 54 #include <netinet/ip.h> 55 #include "ip_mroute.h" 56 #include <netinet/ip_var.h> 57 #ifdef PIM 58 #include <netinet/pim.h> 59 #include <netinet/pim_var.h> 60 #endif 61 #ifdef ALTQ 62 #include <netinet/in_pcb.h> 63 #endif 64 #include <netinet/udp.h> 65 66 /* 67 * Control debugging code for rsvp and multicast routing code. 68 * Can only set them with the debugger. 69 */ 70 static u_int rsvpdebug; /* non-zero enables debugging */ 71 72 static u_int mrtdebug; /* any set of the flags below */ 73 74 #define DEBUG_MFC 0x02 75 #define DEBUG_FORWARD 0x04 76 #define DEBUG_EXPIRE 0x08 77 #define DEBUG_XMIT 0x10 78 #define DEBUG_PIM 0x20 79 80 #define VIFI_INVALID ((vifi_t) -1) 81 82 #define M_HASCL(m) ((m)->m_flags & M_EXT) 83 84 static MALLOC_DEFINE(M_MRTABLE, "mroutetbl", "multicast routing tables"); 85 86 static struct mrtstat mrtstat; 87 SYSCTL_STRUCT(_net_inet_ip, OID_AUTO, mrtstat, CTLFLAG_RW, 88 &mrtstat, mrtstat, 89 "Multicast Routing Statistics (struct mrtstat, netinet/ip_mroute.h)"); 90 91 static struct mfc *mfctable[MFCTBLSIZ]; 92 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, mfctable, CTLFLAG_RD, 93 &mfctable, sizeof(mfctable), "S,*mfc[MFCTBLSIZ]", 94 "Multicast Forwarding Table (struct *mfc[MFCTBLSIZ], netinet/ip_mroute.h)"); 95 96 static struct vif viftable[MAXVIFS]; 97 SYSCTL_OPAQUE(_net_inet_ip, OID_AUTO, viftable, CTLFLAG_RD, 98 &viftable, sizeof(viftable), "S,vif[MAXVIFS]", 99 "Multicast Virtual Interfaces (struct vif[MAXVIFS], netinet/ip_mroute.h)"); 100 101 static u_char nexpire[MFCTBLSIZ]; 102 103 static struct callout expire_upcalls_ch; 104 static struct callout tbf_reprocess_q_ch; 105 #define EXPIRE_TIMEOUT (hz / 4) /* 4x / second */ 106 #define UPCALL_EXPIRE 6 /* number of timeouts */ 107 108 /* 109 * Define the token bucket filter structures 110 * tbftable -> each vif has one of these for storing info 111 */ 112 113 static struct tbf tbftable[MAXVIFS]; 114 #define TBF_REPROCESS (hz / 100) /* 100x / second */ 115 116 /* 117 * 'Interfaces' associated with decapsulator (so we can tell 118 * packets that went through it from ones that get reflected 119 * by a broken gateway). These interfaces are never linked into 120 * the system ifnet list & no routes point to them. I.e., packets 121 * can't be sent this way. They only exist as a placeholder for 122 * multicast source verification. 123 */ 124 static struct ifnet multicast_decap_if[MAXVIFS]; 125 126 #define ENCAP_TTL 64 127 #define ENCAP_PROTO IPPROTO_IPIP /* 4 */ 128 129 /* prototype IP hdr for encapsulated packets */ 130 static struct ip multicast_encap_iphdr = { 131 #if BYTE_ORDER == LITTLE_ENDIAN 132 sizeof(struct ip) >> 2, IPVERSION, 133 #else 134 IPVERSION, sizeof(struct ip) >> 2, 135 #endif 136 0, /* tos */ 137 sizeof(struct ip), /* total length */ 138 0, /* id */ 139 0, /* frag offset */ 140 ENCAP_TTL, ENCAP_PROTO, 141 0, /* checksum */ 142 }; 143 144 /* 145 * Bandwidth meter variables and constants 146 */ 147 static MALLOC_DEFINE(M_BWMETER, "bwmeter", "multicast upcall bw meters"); 148 /* 149 * Pending timeouts are stored in a hash table, the key being the 150 * expiration time. Periodically, the entries are analysed and processed. 151 */ 152 #define BW_METER_BUCKETS 1024 153 static struct bw_meter *bw_meter_timers[BW_METER_BUCKETS]; 154 static struct callout bw_meter_ch; 155 #define BW_METER_PERIOD (hz) /* periodical handling of bw meters */ 156 157 /* 158 * Pending upcalls are stored in a vector which is flushed when 159 * full, or periodically 160 */ 161 static struct bw_upcall bw_upcalls[BW_UPCALLS_MAX]; 162 static u_int bw_upcalls_n; /* # of pending upcalls */ 163 static struct callout bw_upcalls_ch; 164 #define BW_UPCALLS_PERIOD (hz) /* periodical flush of bw upcalls */ 165 166 #ifdef PIM 167 static struct pimstat pimstat; 168 SYSCTL_STRUCT(_net_inet_pim, PIMCTL_STATS, stats, CTLFLAG_RD, 169 &pimstat, pimstat, 170 "PIM Statistics (struct pimstat, netinet/pim_var.h)"); 171 172 /* 173 * Note: the PIM Register encapsulation adds the following in front of a 174 * data packet: 175 * 176 * struct pim_encap_hdr { 177 * struct ip ip; 178 * struct pim_encap_pimhdr pim; 179 * } 180 * 181 */ 182 183 struct pim_encap_pimhdr { 184 struct pim pim; 185 uint32_t flags; 186 }; 187 188 static struct ip pim_encap_iphdr = { 189 #if BYTE_ORDER == LITTLE_ENDIAN 190 sizeof(struct ip) >> 2, 191 IPVERSION, 192 #else 193 IPVERSION, 194 sizeof(struct ip) >> 2, 195 #endif 196 0, /* tos */ 197 sizeof(struct ip), /* total length */ 198 0, /* id */ 199 0, /* frag offset */ 200 ENCAP_TTL, 201 IPPROTO_PIM, 202 0, /* checksum */ 203 }; 204 205 static struct pim_encap_pimhdr pim_encap_pimhdr = { 206 { 207 PIM_MAKE_VT(PIM_VERSION, PIM_REGISTER), /* PIM vers and message type */ 208 0, /* reserved */ 209 0, /* checksum */ 210 }, 211 0 /* flags */ 212 }; 213 214 static struct ifnet multicast_register_if; 215 static vifi_t reg_vif_num = VIFI_INVALID; 216 #endif /* PIM */ 217 218 /* 219 * Private variables. 220 */ 221 static vifi_t numvifs; 222 static int have_encap_tunnel; 223 224 /* 225 * one-back cache used by ipip_input to locate a tunnel's vif 226 * given a datagram's src ip address. 227 */ 228 static u_long last_encap_src; 229 static struct vif *last_encap_vif; 230 231 static u_long X_ip_mcast_src(int vifi); 232 static int X_ip_mforward(struct ip *ip, struct ifnet *ifp, 233 struct mbuf *m, struct ip_moptions *imo); 234 static int X_ip_mrouter_done(void); 235 static int X_ip_mrouter_get(struct socket *so, struct sockopt *m); 236 static int X_ip_mrouter_set(struct socket *so, struct sockopt *m); 237 static int X_legal_vif_num(int vif); 238 static int X_mrt_ioctl(int cmd, caddr_t data); 239 240 static int get_sg_cnt(struct sioc_sg_req *); 241 static int get_vif_cnt(struct sioc_vif_req *); 242 static int ip_mrouter_init(struct socket *, int); 243 static int add_vif(struct vifctl *); 244 static int del_vif(vifi_t); 245 static int add_mfc(struct mfcctl2 *); 246 static int del_mfc(struct mfcctl2 *); 247 static int set_api_config(uint32_t *); /* chose API capabilities */ 248 static int socket_send(struct socket *, struct mbuf *, struct sockaddr_in *); 249 static int set_assert(int); 250 static void expire_upcalls(void *); 251 static int ip_mdq(struct mbuf *, struct ifnet *, struct mfc *, vifi_t); 252 static void phyint_send(struct ip *, struct vif *, struct mbuf *); 253 static void encap_send(struct ip *, struct vif *, struct mbuf *); 254 static void tbf_control(struct vif *, struct mbuf *, struct ip *, u_long); 255 static void tbf_queue(struct vif *, struct mbuf *); 256 static void tbf_process_q(struct vif *); 257 static void tbf_reprocess_q(void *); 258 static int tbf_dq_sel(struct vif *, struct ip *); 259 static void tbf_send_packet(struct vif *, struct mbuf *); 260 static void tbf_update_tokens(struct vif *); 261 static int priority(struct vif *, struct ip *); 262 263 /* 264 * Bandwidth monitoring 265 */ 266 static void free_bw_list(struct bw_meter *list); 267 static int add_bw_upcall(struct bw_upcall *); 268 static int del_bw_upcall(struct bw_upcall *); 269 static void bw_meter_receive_packet(struct bw_meter *x, int plen, 270 struct timeval *nowp); 271 static void bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp); 272 static void bw_upcalls_send(void); 273 static void schedule_bw_meter(struct bw_meter *x, struct timeval *nowp); 274 static void unschedule_bw_meter(struct bw_meter *x); 275 static void bw_meter_process(void); 276 static void expire_bw_upcalls_send(void *); 277 static void expire_bw_meter_process(void *); 278 279 #ifdef PIM 280 static int pim_register_send(struct ip *, struct vif *, 281 struct mbuf *, struct mfc *); 282 static int pim_register_send_rp(struct ip *, struct vif *, 283 struct mbuf *, struct mfc *); 284 static int pim_register_send_upcall(struct ip *, struct vif *, 285 struct mbuf *, struct mfc *); 286 static struct mbuf *pim_register_prepare(struct ip *, struct mbuf *); 287 #endif 288 289 /* 290 * whether or not special PIM assert processing is enabled. 291 */ 292 static int pim_assert; 293 /* 294 * Rate limit for assert notification messages, in usec 295 */ 296 #define ASSERT_MSG_TIME 3000000 297 298 /* 299 * Kernel multicast routing API capabilities and setup. 300 * If more API capabilities are added to the kernel, they should be 301 * recorded in `mrt_api_support'. 302 */ 303 static const uint32_t mrt_api_support = (MRT_MFC_FLAGS_DISABLE_WRONGVIF | 304 MRT_MFC_FLAGS_BORDER_VIF | 305 MRT_MFC_RP | 306 MRT_MFC_BW_UPCALL); 307 static uint32_t mrt_api_config = 0; 308 309 /* 310 * Hash function for a source, group entry 311 */ 312 #define MFCHASH(a, g) MFCHASHMOD(((a) >> 20) ^ ((a) >> 10) ^ (a) ^ \ 313 ((g) >> 20) ^ ((g) >> 10) ^ (g)) 314 315 /* 316 * Find a route for a given origin IP address and Multicast group address 317 * Type of service parameter to be added in the future!!! 318 * Statistics are updated by the caller if needed 319 * (mrtstat.mrts_mfc_lookups and mrtstat.mrts_mfc_misses) 320 */ 321 static struct mfc * 322 mfc_find(in_addr_t o, in_addr_t g) 323 { 324 struct mfc *rt; 325 326 for (rt = mfctable[MFCHASH(o,g)]; rt; rt = rt->mfc_next) 327 if ((rt->mfc_origin.s_addr == o) && 328 (rt->mfc_mcastgrp.s_addr == g) && (rt->mfc_stall == NULL)) 329 break; 330 return rt; 331 } 332 333 /* 334 * Macros to compute elapsed time efficiently 335 * Borrowed from Van Jacobson's scheduling code 336 */ 337 #define TV_DELTA(a, b, delta) { \ 338 int xxs; \ 339 delta = (a).tv_usec - (b).tv_usec; \ 340 if ((xxs = (a).tv_sec - (b).tv_sec)) { \ 341 switch (xxs) { \ 342 case 2: \ 343 delta += 1000000; \ 344 /* FALLTHROUGH */ \ 345 case 1: \ 346 delta += 1000000; \ 347 break; \ 348 default: \ 349 delta += (1000000 * xxs); \ 350 } \ 351 } \ 352 } 353 354 #define TV_LT(a, b) (((a).tv_usec < (b).tv_usec && \ 355 (a).tv_sec <= (b).tv_sec) || (a).tv_sec < (b).tv_sec) 356 357 /* 358 * Handle MRT setsockopt commands to modify the multicast routing tables. 359 */ 360 static int 361 X_ip_mrouter_set(struct socket *so, struct sockopt *sopt) 362 { 363 int error, optval; 364 vifi_t vifi; 365 struct vifctl vifc; 366 struct mfcctl2 mfc; 367 struct bw_upcall bw_upcall; 368 uint32_t i; 369 370 if (so != ip_mrouter && sopt->sopt_name != MRT_INIT) 371 return EPERM; 372 373 error = 0; 374 switch (sopt->sopt_name) { 375 case MRT_INIT: 376 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval); 377 if (error) 378 break; 379 error = ip_mrouter_init(so, optval); 380 break; 381 382 case MRT_DONE: 383 error = ip_mrouter_done(); 384 break; 385 386 case MRT_ADD_VIF: 387 error = sooptcopyin(sopt, &vifc, sizeof vifc, sizeof vifc); 388 if (error) 389 break; 390 error = add_vif(&vifc); 391 break; 392 393 case MRT_DEL_VIF: 394 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi); 395 if (error) 396 break; 397 error = del_vif(vifi); 398 break; 399 400 case MRT_ADD_MFC: 401 case MRT_DEL_MFC: 402 /* 403 * select data size depending on API version. 404 */ 405 if (sopt->sopt_name == MRT_ADD_MFC && 406 mrt_api_config & MRT_API_FLAGS_ALL) { 407 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl2), 408 sizeof(struct mfcctl2)); 409 } else { 410 error = sooptcopyin(sopt, &mfc, sizeof(struct mfcctl), 411 sizeof(struct mfcctl)); 412 bzero((caddr_t)&mfc + sizeof(struct mfcctl), 413 sizeof(mfc) - sizeof(struct mfcctl)); 414 } 415 if (error) 416 break; 417 if (sopt->sopt_name == MRT_ADD_MFC) 418 error = add_mfc(&mfc); 419 else 420 error = del_mfc(&mfc); 421 break; 422 423 case MRT_ASSERT: 424 error = sooptcopyin(sopt, &optval, sizeof optval, sizeof optval); 425 if (error) 426 break; 427 set_assert(optval); 428 break; 429 430 case MRT_API_CONFIG: 431 error = sooptcopyin(sopt, &i, sizeof i, sizeof i); 432 if (!error) 433 error = set_api_config(&i); 434 if (!error) 435 error = sooptcopyout(sopt, &i, sizeof i); 436 break; 437 438 case MRT_ADD_BW_UPCALL: 439 case MRT_DEL_BW_UPCALL: 440 error = sooptcopyin(sopt, &bw_upcall, sizeof bw_upcall, 441 sizeof bw_upcall); 442 if (error) 443 break; 444 if (sopt->sopt_name == MRT_ADD_BW_UPCALL) 445 error = add_bw_upcall(&bw_upcall); 446 else 447 error = del_bw_upcall(&bw_upcall); 448 break; 449 450 default: 451 error = EOPNOTSUPP; 452 break; 453 } 454 return error; 455 } 456 457 /* 458 * Handle MRT getsockopt commands 459 */ 460 static int 461 X_ip_mrouter_get(struct socket *so, struct sockopt *sopt) 462 { 463 int error; 464 static int version = 0x0305; /* !!! why is this here? XXX */ 465 466 switch (sopt->sopt_name) { 467 case MRT_VERSION: 468 error = sooptcopyout(sopt, &version, sizeof version); 469 break; 470 471 case MRT_ASSERT: 472 error = sooptcopyout(sopt, &pim_assert, sizeof pim_assert); 473 break; 474 475 case MRT_API_SUPPORT: 476 error = sooptcopyout(sopt, &mrt_api_support, sizeof mrt_api_support); 477 break; 478 479 case MRT_API_CONFIG: 480 error = sooptcopyout(sopt, &mrt_api_config, sizeof mrt_api_config); 481 break; 482 483 default: 484 error = EOPNOTSUPP; 485 break; 486 } 487 return error; 488 } 489 490 /* 491 * Handle ioctl commands to obtain information from the cache 492 */ 493 static int 494 X_mrt_ioctl(int cmd, caddr_t data) 495 { 496 int error = 0; 497 498 switch (cmd) { 499 case SIOCGETVIFCNT: 500 error = get_vif_cnt((struct sioc_vif_req *)data); 501 break; 502 503 case SIOCGETSGCNT: 504 error = get_sg_cnt((struct sioc_sg_req *)data); 505 break; 506 507 default: 508 error = EINVAL; 509 break; 510 } 511 return error; 512 } 513 514 /* 515 * returns the packet, byte, rpf-failure count for the source group provided 516 */ 517 static int 518 get_sg_cnt(struct sioc_sg_req *req) 519 { 520 struct mfc *rt; 521 522 crit_enter(); 523 rt = mfc_find(req->src.s_addr, req->grp.s_addr); 524 crit_exit(); 525 if (rt == NULL) { 526 req->pktcnt = req->bytecnt = req->wrong_if = 0xffffffff; 527 return EADDRNOTAVAIL; 528 } 529 req->pktcnt = rt->mfc_pkt_cnt; 530 req->bytecnt = rt->mfc_byte_cnt; 531 req->wrong_if = rt->mfc_wrong_if; 532 return 0; 533 } 534 535 /* 536 * returns the input and output packet and byte counts on the vif provided 537 */ 538 static int 539 get_vif_cnt(struct sioc_vif_req *req) 540 { 541 vifi_t vifi = req->vifi; 542 543 if (vifi >= numvifs) 544 return EINVAL; 545 546 req->icount = viftable[vifi].v_pkt_in; 547 req->ocount = viftable[vifi].v_pkt_out; 548 req->ibytes = viftable[vifi].v_bytes_in; 549 req->obytes = viftable[vifi].v_bytes_out; 550 551 return 0; 552 } 553 554 /* 555 * Enable multicast routing 556 */ 557 static int 558 ip_mrouter_init(struct socket *so, int version) 559 { 560 if (mrtdebug) 561 log(LOG_DEBUG, "ip_mrouter_init: so_type = %d, pr_protocol = %d\n", 562 so->so_type, so->so_proto->pr_protocol); 563 564 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_IGMP) 565 return EOPNOTSUPP; 566 567 if (version != 1) 568 return ENOPROTOOPT; 569 570 if (ip_mrouter != NULL) 571 return EADDRINUSE; 572 573 ip_mrouter = so; 574 575 bzero((caddr_t)mfctable, sizeof(mfctable)); 576 bzero((caddr_t)nexpire, sizeof(nexpire)); 577 578 pim_assert = 0; 579 bw_upcalls_n = 0; 580 bzero((caddr_t)bw_meter_timers, sizeof(bw_meter_timers)); 581 582 callout_init(&expire_upcalls_ch); 583 callout_init(&bw_upcalls_ch); 584 callout_init(&bw_meter_ch); 585 callout_init(&tbf_reprocess_q_ch); 586 587 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL); 588 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD, 589 expire_bw_upcalls_send, NULL); 590 callout_reset(&bw_meter_ch, BW_METER_PERIOD, expire_bw_meter_process, NULL); 591 592 mrt_api_config = 0; 593 594 if (mrtdebug) 595 log(LOG_DEBUG, "ip_mrouter_init\n"); 596 597 return 0; 598 } 599 600 /* 601 * Disable multicast routing 602 */ 603 static int 604 X_ip_mrouter_done(void) 605 { 606 vifi_t vifi; 607 int i; 608 struct ifnet *ifp; 609 struct ifreq ifr; 610 struct mfc *rt; 611 struct rtdetq *rte; 612 613 crit_enter(); 614 615 /* 616 * For each phyint in use, disable promiscuous reception of all IP 617 * multicasts. 618 */ 619 for (vifi = 0; vifi < numvifs; vifi++) { 620 if (viftable[vifi].v_lcl_addr.s_addr != 0 && 621 !(viftable[vifi].v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) { 622 struct sockaddr_in *so = (struct sockaddr_in *)&(ifr.ifr_addr); 623 624 so->sin_len = sizeof(struct sockaddr_in); 625 so->sin_family = AF_INET; 626 so->sin_addr.s_addr = INADDR_ANY; 627 ifp = viftable[vifi].v_ifp; 628 if_allmulti(ifp, 0); 629 } 630 } 631 bzero((caddr_t)tbftable, sizeof(tbftable)); 632 bzero((caddr_t)viftable, sizeof(viftable)); 633 numvifs = 0; 634 pim_assert = 0; 635 636 callout_stop(&expire_upcalls_ch); 637 638 mrt_api_config = 0; 639 bw_upcalls_n = 0; 640 callout_stop(&bw_upcalls_ch); 641 callout_stop(&bw_meter_ch); 642 callout_stop(&tbf_reprocess_q_ch); 643 644 /* 645 * Free all multicast forwarding cache entries. 646 */ 647 for (i = 0; i < MFCTBLSIZ; i++) { 648 for (rt = mfctable[i]; rt != NULL; ) { 649 struct mfc *nr = rt->mfc_next; 650 651 for (rte = rt->mfc_stall; rte != NULL; ) { 652 struct rtdetq *n = rte->next; 653 654 m_freem(rte->m); 655 kfree(rte, M_MRTABLE); 656 rte = n; 657 } 658 free_bw_list(rt->mfc_bw_meter); 659 kfree(rt, M_MRTABLE); 660 rt = nr; 661 } 662 } 663 664 bzero((caddr_t)mfctable, sizeof(mfctable)); 665 666 bzero(bw_meter_timers, sizeof(bw_meter_timers)); 667 668 /* 669 * Reset de-encapsulation cache 670 */ 671 last_encap_src = INADDR_ANY; 672 last_encap_vif = NULL; 673 #ifdef PIM 674 reg_vif_num = VIFI_INVALID; 675 #endif 676 have_encap_tunnel = 0; 677 678 ip_mrouter = NULL; 679 680 crit_exit(); 681 682 if (mrtdebug) 683 log(LOG_DEBUG, "ip_mrouter_done\n"); 684 685 return 0; 686 } 687 688 /* 689 * Set PIM assert processing global 690 */ 691 static int 692 set_assert(int i) 693 { 694 if ((i != 1) && (i != 0)) 695 return EINVAL; 696 697 pim_assert = i; 698 699 return 0; 700 } 701 702 /* 703 * Configure API capabilities 704 */ 705 int 706 set_api_config(uint32_t *apival) 707 { 708 int i; 709 710 /* 711 * We can set the API capabilities only if it is the first operation 712 * after MRT_INIT. I.e.: 713 * - there are no vifs installed 714 * - pim_assert is not enabled 715 * - the MFC table is empty 716 */ 717 if (numvifs > 0) { 718 *apival = 0; 719 return EPERM; 720 } 721 if (pim_assert) { 722 *apival = 0; 723 return EPERM; 724 } 725 for (i = 0; i < MFCTBLSIZ; i++) { 726 if (mfctable[i] != NULL) { 727 *apival = 0; 728 return EPERM; 729 } 730 } 731 732 mrt_api_config = *apival & mrt_api_support; 733 *apival = mrt_api_config; 734 735 return 0; 736 } 737 738 /* 739 * Add a vif to the vif table 740 */ 741 static int 742 add_vif(struct vifctl *vifcp) 743 { 744 struct vif *vifp = viftable + vifcp->vifc_vifi; 745 struct sockaddr_in sin = {sizeof sin, AF_INET}; 746 struct ifaddr *ifa; 747 struct ifnet *ifp; 748 int error, i; 749 struct tbf *v_tbf = tbftable + vifcp->vifc_vifi; 750 751 if (vifcp->vifc_vifi >= MAXVIFS) 752 return EINVAL; 753 if (vifp->v_lcl_addr.s_addr != INADDR_ANY) 754 return EADDRINUSE; 755 if (vifcp->vifc_lcl_addr.s_addr == INADDR_ANY) 756 return EADDRNOTAVAIL; 757 758 /* Find the interface with an address in AF_INET family */ 759 #ifdef PIM 760 if (vifcp->vifc_flags & VIFF_REGISTER) { 761 /* 762 * XXX: Because VIFF_REGISTER does not really need a valid 763 * local interface (e.g. it could be 127.0.0.2), we don't 764 * check its address. 765 */ 766 ifp = NULL; 767 } else 768 #endif 769 { 770 sin.sin_addr = vifcp->vifc_lcl_addr; 771 ifa = ifa_ifwithaddr((struct sockaddr *)&sin); 772 if (ifa == NULL) 773 return EADDRNOTAVAIL; 774 ifp = ifa->ifa_ifp; 775 } 776 777 if (vifcp->vifc_flags & VIFF_TUNNEL) { 778 if ((vifcp->vifc_flags & VIFF_SRCRT) == 0) { 779 /* 780 * An encapsulating tunnel is wanted. Tell ipip_input() to 781 * start paying attention to encapsulated packets. 782 */ 783 if (have_encap_tunnel == 0) { 784 have_encap_tunnel = 1; 785 for (i = 0; i < MAXVIFS; i++) { 786 if_initname(&multicast_decap_if[i], "mdecap", i); 787 } 788 } 789 /* 790 * Set interface to fake encapsulator interface 791 */ 792 ifp = &multicast_decap_if[vifcp->vifc_vifi]; 793 /* 794 * Prepare cached route entry 795 */ 796 bzero(&vifp->v_route, sizeof(vifp->v_route)); 797 } else { 798 log(LOG_ERR, "source routed tunnels not supported\n"); 799 return EOPNOTSUPP; 800 } 801 #ifdef PIM 802 } else if (vifcp->vifc_flags & VIFF_REGISTER) { 803 ifp = &multicast_register_if; 804 if (mrtdebug) 805 log(LOG_DEBUG, "Adding a register vif, ifp: %p\n", 806 (void *)&multicast_register_if); 807 if (reg_vif_num == VIFI_INVALID) { 808 if_initname(&multicast_register_if, "register_vif", 0); 809 multicast_register_if.if_flags = IFF_LOOPBACK; 810 bzero(&vifp->v_route, sizeof(vifp->v_route)); 811 reg_vif_num = vifcp->vifc_vifi; 812 } 813 #endif 814 } else { /* Make sure the interface supports multicast */ 815 if ((ifp->if_flags & IFF_MULTICAST) == 0) 816 return EOPNOTSUPP; 817 818 /* Enable promiscuous reception of all IP multicasts from the if */ 819 crit_enter(); 820 error = if_allmulti(ifp, 1); 821 crit_exit(); 822 if (error) 823 return error; 824 } 825 826 crit_enter(); 827 /* define parameters for the tbf structure */ 828 vifp->v_tbf = v_tbf; 829 GET_TIME(vifp->v_tbf->tbf_last_pkt_t); 830 vifp->v_tbf->tbf_n_tok = 0; 831 vifp->v_tbf->tbf_q_len = 0; 832 vifp->v_tbf->tbf_max_q_len = MAXQSIZE; 833 vifp->v_tbf->tbf_q = vifp->v_tbf->tbf_t = NULL; 834 835 vifp->v_flags = vifcp->vifc_flags; 836 vifp->v_threshold = vifcp->vifc_threshold; 837 vifp->v_lcl_addr = vifcp->vifc_lcl_addr; 838 vifp->v_rmt_addr = vifcp->vifc_rmt_addr; 839 vifp->v_ifp = ifp; 840 /* scaling up here allows division by 1024 in critical code */ 841 vifp->v_rate_limit= vifcp->vifc_rate_limit * 1024 / 1000; 842 vifp->v_rsvp_on = 0; 843 vifp->v_rsvpd = NULL; 844 /* initialize per vif pkt counters */ 845 vifp->v_pkt_in = 0; 846 vifp->v_pkt_out = 0; 847 vifp->v_bytes_in = 0; 848 vifp->v_bytes_out = 0; 849 crit_exit(); 850 851 /* Adjust numvifs up if the vifi is higher than numvifs */ 852 if (numvifs <= vifcp->vifc_vifi) numvifs = vifcp->vifc_vifi + 1; 853 854 if (mrtdebug) 855 log(LOG_DEBUG, "add_vif #%d, lcladdr %lx, %s %lx, thresh %x, rate %d\n", 856 vifcp->vifc_vifi, 857 (u_long)ntohl(vifcp->vifc_lcl_addr.s_addr), 858 (vifcp->vifc_flags & VIFF_TUNNEL) ? "rmtaddr" : "mask", 859 (u_long)ntohl(vifcp->vifc_rmt_addr.s_addr), 860 vifcp->vifc_threshold, 861 vifcp->vifc_rate_limit); 862 863 return 0; 864 } 865 866 /* 867 * Delete a vif from the vif table 868 */ 869 static int 870 del_vif(vifi_t vifi) 871 { 872 struct vif *vifp; 873 874 if (vifi >= numvifs) 875 return EINVAL; 876 vifp = &viftable[vifi]; 877 if (vifp->v_lcl_addr.s_addr == INADDR_ANY) 878 return EADDRNOTAVAIL; 879 880 crit_enter(); 881 882 if (!(vifp->v_flags & (VIFF_TUNNEL | VIFF_REGISTER))) 883 if_allmulti(vifp->v_ifp, 0); 884 885 if (vifp == last_encap_vif) { 886 last_encap_vif = NULL; 887 last_encap_src = INADDR_ANY; 888 } 889 890 /* 891 * Free packets queued at the interface 892 */ 893 while (vifp->v_tbf->tbf_q) { 894 struct mbuf *m = vifp->v_tbf->tbf_q; 895 896 vifp->v_tbf->tbf_q = m->m_nextpkt; 897 m_freem(m); 898 } 899 900 #ifdef PIM 901 if (vifp->v_flags & VIFF_REGISTER) 902 reg_vif_num = VIFI_INVALID; 903 #endif 904 905 bzero((caddr_t)vifp->v_tbf, sizeof(*(vifp->v_tbf))); 906 bzero((caddr_t)vifp, sizeof (*vifp)); 907 908 if (mrtdebug) 909 log(LOG_DEBUG, "del_vif %d, numvifs %d\n", vifi, numvifs); 910 911 /* Adjust numvifs down */ 912 for (vifi = numvifs; vifi > 0; vifi--) 913 if (viftable[vifi-1].v_lcl_addr.s_addr != INADDR_ANY) 914 break; 915 numvifs = vifi; 916 917 crit_exit(); 918 919 return 0; 920 } 921 922 /* 923 * update an mfc entry without resetting counters and S,G addresses. 924 */ 925 static void 926 update_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp) 927 { 928 int i; 929 930 rt->mfc_parent = mfccp->mfcc_parent; 931 for (i = 0; i < numvifs; i++) { 932 rt->mfc_ttls[i] = mfccp->mfcc_ttls[i]; 933 rt->mfc_flags[i] = mfccp->mfcc_flags[i] & mrt_api_config & 934 MRT_MFC_FLAGS_ALL; 935 } 936 /* set the RP address */ 937 if (mrt_api_config & MRT_MFC_RP) 938 rt->mfc_rp = mfccp->mfcc_rp; 939 else 940 rt->mfc_rp.s_addr = INADDR_ANY; 941 } 942 943 /* 944 * fully initialize an mfc entry from the parameter. 945 */ 946 static void 947 init_mfc_params(struct mfc *rt, struct mfcctl2 *mfccp) 948 { 949 rt->mfc_origin = mfccp->mfcc_origin; 950 rt->mfc_mcastgrp = mfccp->mfcc_mcastgrp; 951 952 update_mfc_params(rt, mfccp); 953 954 /* initialize pkt counters per src-grp */ 955 rt->mfc_pkt_cnt = 0; 956 rt->mfc_byte_cnt = 0; 957 rt->mfc_wrong_if = 0; 958 rt->mfc_last_assert.tv_sec = rt->mfc_last_assert.tv_usec = 0; 959 } 960 961 962 /* 963 * Add an mfc entry 964 */ 965 static int 966 add_mfc(struct mfcctl2 *mfccp) 967 { 968 struct mfc *rt; 969 u_long hash; 970 struct rtdetq *rte; 971 u_short nstl; 972 973 rt = mfc_find(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr); 974 975 /* If an entry already exists, just update the fields */ 976 if (rt) { 977 if (mrtdebug & DEBUG_MFC) 978 log(LOG_DEBUG,"add_mfc update o %lx g %lx p %x\n", 979 (u_long)ntohl(mfccp->mfcc_origin.s_addr), 980 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr), 981 mfccp->mfcc_parent); 982 983 crit_enter(); 984 update_mfc_params(rt, mfccp); 985 crit_exit(); 986 return 0; 987 } 988 989 /* 990 * Find the entry for which the upcall was made and update 991 */ 992 crit_enter(); 993 hash = MFCHASH(mfccp->mfcc_origin.s_addr, mfccp->mfcc_mcastgrp.s_addr); 994 for (rt = mfctable[hash], nstl = 0; rt; rt = rt->mfc_next) { 995 996 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) && 997 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr) && 998 (rt->mfc_stall != NULL)) { 999 1000 if (nstl++) 1001 log(LOG_ERR, "add_mfc %s o %lx g %lx p %x dbx %p\n", 1002 "multiple kernel entries", 1003 (u_long)ntohl(mfccp->mfcc_origin.s_addr), 1004 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr), 1005 mfccp->mfcc_parent, (void *)rt->mfc_stall); 1006 1007 if (mrtdebug & DEBUG_MFC) 1008 log(LOG_DEBUG,"add_mfc o %lx g %lx p %x dbg %p\n", 1009 (u_long)ntohl(mfccp->mfcc_origin.s_addr), 1010 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr), 1011 mfccp->mfcc_parent, (void *)rt->mfc_stall); 1012 1013 init_mfc_params(rt, mfccp); 1014 1015 rt->mfc_expire = 0; /* Don't clean this guy up */ 1016 nexpire[hash]--; 1017 1018 /* free packets Qed at the end of this entry */ 1019 for (rte = rt->mfc_stall; rte != NULL; ) { 1020 struct rtdetq *n = rte->next; 1021 1022 ip_mdq(rte->m, rte->ifp, rt, -1); 1023 m_freem(rte->m); 1024 kfree(rte, M_MRTABLE); 1025 rte = n; 1026 } 1027 rt->mfc_stall = NULL; 1028 } 1029 } 1030 1031 /* 1032 * It is possible that an entry is being inserted without an upcall 1033 */ 1034 if (nstl == 0) { 1035 if (mrtdebug & DEBUG_MFC) 1036 log(LOG_DEBUG,"add_mfc no upcall h %lu o %lx g %lx p %x\n", 1037 hash, (u_long)ntohl(mfccp->mfcc_origin.s_addr), 1038 (u_long)ntohl(mfccp->mfcc_mcastgrp.s_addr), 1039 mfccp->mfcc_parent); 1040 1041 for (rt = mfctable[hash]; rt != NULL; rt = rt->mfc_next) { 1042 if ((rt->mfc_origin.s_addr == mfccp->mfcc_origin.s_addr) && 1043 (rt->mfc_mcastgrp.s_addr == mfccp->mfcc_mcastgrp.s_addr)) { 1044 init_mfc_params(rt, mfccp); 1045 if (rt->mfc_expire) 1046 nexpire[hash]--; 1047 rt->mfc_expire = 0; 1048 break; /* XXX */ 1049 } 1050 } 1051 if (rt == NULL) { /* no upcall, so make a new entry */ 1052 rt = kmalloc(sizeof(*rt), M_MRTABLE, M_INTWAIT | M_NULLOK); 1053 if (rt == NULL) { 1054 crit_exit(); 1055 return ENOBUFS; 1056 } 1057 1058 init_mfc_params(rt, mfccp); 1059 rt->mfc_expire = 0; 1060 rt->mfc_stall = NULL; 1061 1062 rt->mfc_bw_meter = NULL; 1063 /* insert new entry at head of hash chain */ 1064 rt->mfc_next = mfctable[hash]; 1065 mfctable[hash] = rt; 1066 } 1067 } 1068 crit_exit(); 1069 return 0; 1070 } 1071 1072 /* 1073 * Delete an mfc entry 1074 */ 1075 static int 1076 del_mfc(struct mfcctl2 *mfccp) 1077 { 1078 struct in_addr origin; 1079 struct in_addr mcastgrp; 1080 struct mfc *rt; 1081 struct mfc **nptr; 1082 u_long hash; 1083 struct bw_meter *list; 1084 1085 origin = mfccp->mfcc_origin; 1086 mcastgrp = mfccp->mfcc_mcastgrp; 1087 1088 if (mrtdebug & DEBUG_MFC) 1089 log(LOG_DEBUG,"del_mfc orig %lx mcastgrp %lx\n", 1090 (u_long)ntohl(origin.s_addr), (u_long)ntohl(mcastgrp.s_addr)); 1091 1092 crit_enter(); 1093 1094 hash = MFCHASH(origin.s_addr, mcastgrp.s_addr); 1095 for (nptr = &mfctable[hash]; (rt = *nptr) != NULL; nptr = &rt->mfc_next) 1096 if (origin.s_addr == rt->mfc_origin.s_addr && 1097 mcastgrp.s_addr == rt->mfc_mcastgrp.s_addr && 1098 rt->mfc_stall == NULL) 1099 break; 1100 if (rt == NULL) { 1101 crit_exit(); 1102 return EADDRNOTAVAIL; 1103 } 1104 1105 *nptr = rt->mfc_next; 1106 1107 /* 1108 * free the bw_meter entries 1109 */ 1110 list = rt->mfc_bw_meter; 1111 rt->mfc_bw_meter = NULL; 1112 1113 kfree(rt, M_MRTABLE); 1114 1115 crit_exit(); 1116 1117 free_bw_list(list); 1118 1119 return 0; 1120 } 1121 1122 /* 1123 * Send a message to mrouted on the multicast routing socket 1124 */ 1125 static int 1126 socket_send(struct socket *s, struct mbuf *mm, struct sockaddr_in *src) 1127 { 1128 if (s) { 1129 if (ssb_appendaddr(&s->so_rcv, (struct sockaddr *)src, mm, NULL) != 0) { 1130 sorwakeup(s); 1131 return 0; 1132 } 1133 } 1134 m_freem(mm); 1135 return -1; 1136 } 1137 1138 /* 1139 * IP multicast forwarding function. This function assumes that the packet 1140 * pointed to by "ip" has arrived on (or is about to be sent to) the interface 1141 * pointed to by "ifp", and the packet is to be relayed to other networks 1142 * that have members of the packet's destination IP multicast group. 1143 * 1144 * The packet is returned unscathed to the caller, unless it is 1145 * erroneous, in which case a non-zero return value tells the caller to 1146 * discard it. 1147 */ 1148 1149 #define TUNNEL_LEN 12 /* # bytes of IP option for tunnel encapsulation */ 1150 1151 static int 1152 X_ip_mforward(struct ip *ip, struct ifnet *ifp, struct mbuf *m, 1153 struct ip_moptions *imo) 1154 { 1155 struct mfc *rt; 1156 vifi_t vifi; 1157 1158 if (mrtdebug & DEBUG_FORWARD) 1159 log(LOG_DEBUG, "ip_mforward: src %lx, dst %lx, ifp %p\n", 1160 (u_long)ntohl(ip->ip_src.s_addr), (u_long)ntohl(ip->ip_dst.s_addr), 1161 (void *)ifp); 1162 1163 if (ip->ip_hl < (sizeof(struct ip) + TUNNEL_LEN) >> 2 || 1164 ((u_char *)(ip + 1))[1] != IPOPT_LSRR ) { 1165 /* 1166 * Packet arrived via a physical interface or 1167 * an encapsulated tunnel or a register_vif. 1168 */ 1169 } else { 1170 /* 1171 * Packet arrived through a source-route tunnel. 1172 * Source-route tunnels are no longer supported. 1173 */ 1174 static int last_log; 1175 if (last_log != time_second) { 1176 last_log = time_second; 1177 log(LOG_ERR, 1178 "ip_mforward: received source-routed packet from %lx\n", 1179 (u_long)ntohl(ip->ip_src.s_addr)); 1180 } 1181 return 1; 1182 } 1183 1184 if (imo && ((vifi = imo->imo_multicast_vif) < numvifs)) { 1185 if (ip->ip_ttl < 255) 1186 ip->ip_ttl++; /* compensate for -1 in *_send routines */ 1187 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) { 1188 struct vif *vifp = viftable + vifi; 1189 1190 kprintf("Sending IPPROTO_RSVP from %lx to %lx on vif %d (%s%s)\n", 1191 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr), 1192 vifi, 1193 (vifp->v_flags & VIFF_TUNNEL) ? "tunnel on " : "", 1194 vifp->v_ifp->if_xname); 1195 } 1196 return ip_mdq(m, ifp, NULL, vifi); 1197 } 1198 if (rsvpdebug && ip->ip_p == IPPROTO_RSVP) { 1199 kprintf("Warning: IPPROTO_RSVP from %lx to %lx without vif option\n", 1200 (long)ntohl(ip->ip_src.s_addr), (long)ntohl(ip->ip_dst.s_addr)); 1201 if (!imo) 1202 kprintf("In fact, no options were specified at all\n"); 1203 } 1204 1205 /* 1206 * Don't forward a packet with time-to-live of zero or one, 1207 * or a packet destined to a local-only group. 1208 */ 1209 if (ip->ip_ttl <= 1 || ntohl(ip->ip_dst.s_addr) <= INADDR_MAX_LOCAL_GROUP) 1210 return 0; 1211 1212 /* 1213 * Determine forwarding vifs from the forwarding cache table 1214 */ 1215 crit_enter(); 1216 ++mrtstat.mrts_mfc_lookups; 1217 rt = mfc_find(ip->ip_src.s_addr, ip->ip_dst.s_addr); 1218 1219 /* Entry exists, so forward if necessary */ 1220 if (rt != NULL) { 1221 crit_exit(); 1222 return ip_mdq(m, ifp, rt, -1); 1223 } else { 1224 /* 1225 * If we don't have a route for packet's origin, 1226 * Make a copy of the packet & send message to routing daemon 1227 */ 1228 1229 struct mbuf *mb0; 1230 struct rtdetq *rte; 1231 u_long hash; 1232 int hlen = ip->ip_hl << 2; 1233 1234 ++mrtstat.mrts_mfc_misses; 1235 1236 mrtstat.mrts_no_route++; 1237 if (mrtdebug & (DEBUG_FORWARD | DEBUG_MFC)) 1238 log(LOG_DEBUG, "ip_mforward: no rte s %lx g %lx\n", 1239 (u_long)ntohl(ip->ip_src.s_addr), 1240 (u_long)ntohl(ip->ip_dst.s_addr)); 1241 1242 /* 1243 * Allocate mbufs early so that we don't do extra work if we are 1244 * just going to fail anyway. Make sure to pullup the header so 1245 * that other people can't step on it. 1246 */ 1247 rte = kmalloc((sizeof *rte), M_MRTABLE, M_INTWAIT | M_NULLOK); 1248 if (rte == NULL) { 1249 crit_exit(); 1250 return ENOBUFS; 1251 } 1252 1253 mb0 = m_copypacket(m, MB_DONTWAIT); 1254 if (mb0 && (M_HASCL(mb0) || mb0->m_len < hlen)) 1255 mb0 = m_pullup(mb0, hlen); 1256 if (mb0 == NULL) { 1257 kfree(rte, M_MRTABLE); 1258 crit_exit(); 1259 return ENOBUFS; 1260 } 1261 1262 /* is there an upcall waiting for this flow ? */ 1263 hash = MFCHASH(ip->ip_src.s_addr, ip->ip_dst.s_addr); 1264 for (rt = mfctable[hash]; rt; rt = rt->mfc_next) { 1265 if ((ip->ip_src.s_addr == rt->mfc_origin.s_addr) && 1266 (ip->ip_dst.s_addr == rt->mfc_mcastgrp.s_addr) && 1267 (rt->mfc_stall != NULL)) 1268 break; 1269 } 1270 1271 if (rt == NULL) { 1272 int i; 1273 struct igmpmsg *im; 1274 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET }; 1275 struct mbuf *mm; 1276 1277 /* 1278 * Locate the vifi for the incoming interface for this packet. 1279 * If none found, drop packet. 1280 */ 1281 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++) 1282 ; 1283 if (vifi >= numvifs) /* vif not found, drop packet */ 1284 goto non_fatal; 1285 1286 /* no upcall, so make a new entry */ 1287 rt = kmalloc(sizeof(*rt), M_MRTABLE, M_INTWAIT | M_NULLOK); 1288 if (rt == NULL) 1289 goto fail; 1290 1291 /* Make a copy of the header to send to the user level process */ 1292 mm = m_copy(mb0, 0, hlen); 1293 if (mm == NULL) 1294 goto fail1; 1295 1296 /* 1297 * Send message to routing daemon to install 1298 * a route into the kernel table 1299 */ 1300 1301 im = mtod(mm, struct igmpmsg *); 1302 im->im_msgtype = IGMPMSG_NOCACHE; 1303 im->im_mbz = 0; 1304 im->im_vif = vifi; 1305 1306 mrtstat.mrts_upcalls++; 1307 1308 k_igmpsrc.sin_addr = ip->ip_src; 1309 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) { 1310 log(LOG_WARNING, "ip_mforward: ip_mrouter socket queue full\n"); 1311 ++mrtstat.mrts_upq_sockfull; 1312 fail1: 1313 kfree(rt, M_MRTABLE); 1314 fail: 1315 kfree(rte, M_MRTABLE); 1316 m_freem(mb0); 1317 crit_exit(); 1318 return ENOBUFS; 1319 } 1320 1321 /* insert new entry at head of hash chain */ 1322 rt->mfc_origin.s_addr = ip->ip_src.s_addr; 1323 rt->mfc_mcastgrp.s_addr = ip->ip_dst.s_addr; 1324 rt->mfc_expire = UPCALL_EXPIRE; 1325 nexpire[hash]++; 1326 for (i = 0; i < numvifs; i++) { 1327 rt->mfc_ttls[i] = 0; 1328 rt->mfc_flags[i] = 0; 1329 } 1330 rt->mfc_parent = -1; 1331 1332 rt->mfc_rp.s_addr = INADDR_ANY; /* clear the RP address */ 1333 1334 rt->mfc_bw_meter = NULL; 1335 1336 /* link into table */ 1337 rt->mfc_next = mfctable[hash]; 1338 mfctable[hash] = rt; 1339 rt->mfc_stall = rte; 1340 1341 } else { 1342 /* determine if q has overflowed */ 1343 int npkts = 0; 1344 struct rtdetq **p; 1345 1346 /* 1347 * XXX ouch! we need to append to the list, but we 1348 * only have a pointer to the front, so we have to 1349 * scan the entire list every time. 1350 */ 1351 for (p = &rt->mfc_stall; *p != NULL; p = &(*p)->next) 1352 npkts++; 1353 1354 if (npkts > MAX_UPQ) { 1355 mrtstat.mrts_upq_ovflw++; 1356 non_fatal: 1357 kfree(rte, M_MRTABLE); 1358 m_freem(mb0); 1359 crit_exit(); 1360 return 0; 1361 } 1362 1363 /* Add this entry to the end of the queue */ 1364 *p = rte; 1365 } 1366 1367 rte->m = mb0; 1368 rte->ifp = ifp; 1369 rte->next = NULL; 1370 1371 crit_exit(); 1372 return 0; 1373 } 1374 } 1375 1376 /* 1377 * Clean up the cache entry if upcall is not serviced 1378 */ 1379 static void 1380 expire_upcalls(void *unused) 1381 { 1382 struct rtdetq *rte; 1383 struct mfc *mfc, **nptr; 1384 int i; 1385 1386 crit_enter(); 1387 for (i = 0; i < MFCTBLSIZ; i++) { 1388 if (nexpire[i] == 0) 1389 continue; 1390 nptr = &mfctable[i]; 1391 for (mfc = *nptr; mfc != NULL; mfc = *nptr) { 1392 /* 1393 * Skip real cache entries 1394 * Make sure it wasn't marked to not expire (shouldn't happen) 1395 * If it expires now 1396 */ 1397 if (mfc->mfc_stall != NULL && mfc->mfc_expire != 0 && 1398 --mfc->mfc_expire == 0) { 1399 if (mrtdebug & DEBUG_EXPIRE) 1400 log(LOG_DEBUG, "expire_upcalls: expiring (%lx %lx)\n", 1401 (u_long)ntohl(mfc->mfc_origin.s_addr), 1402 (u_long)ntohl(mfc->mfc_mcastgrp.s_addr)); 1403 /* 1404 * drop all the packets 1405 * free the mbuf with the pkt, if, timing info 1406 */ 1407 for (rte = mfc->mfc_stall; rte; ) { 1408 struct rtdetq *n = rte->next; 1409 1410 m_freem(rte->m); 1411 kfree(rte, M_MRTABLE); 1412 rte = n; 1413 } 1414 ++mrtstat.mrts_cache_cleanups; 1415 nexpire[i]--; 1416 1417 /* 1418 * free the bw_meter entries 1419 */ 1420 while (mfc->mfc_bw_meter != NULL) { 1421 struct bw_meter *x = mfc->mfc_bw_meter; 1422 1423 mfc->mfc_bw_meter = x->bm_mfc_next; 1424 kfree(x, M_BWMETER); 1425 } 1426 1427 *nptr = mfc->mfc_next; 1428 kfree(mfc, M_MRTABLE); 1429 } else { 1430 nptr = &mfc->mfc_next; 1431 } 1432 } 1433 } 1434 callout_reset(&expire_upcalls_ch, EXPIRE_TIMEOUT, expire_upcalls, NULL); 1435 crit_exit(); 1436 } 1437 1438 /* 1439 * Packet forwarding routine once entry in the cache is made 1440 */ 1441 static int 1442 ip_mdq(struct mbuf *m, struct ifnet *ifp, struct mfc *rt, vifi_t xmt_vif) 1443 { 1444 struct ip *ip = mtod(m, struct ip *); 1445 vifi_t vifi; 1446 int plen = ip->ip_len; 1447 1448 /* 1449 * Macro to send packet on vif. Since RSVP packets don't get counted on 1450 * input, they shouldn't get counted on output, so statistics keeping is 1451 * separate. 1452 */ 1453 #define MC_SEND(ip,vifp,m) { \ 1454 if ((vifp)->v_flags & VIFF_TUNNEL) \ 1455 encap_send((ip), (vifp), (m)); \ 1456 else \ 1457 phyint_send((ip), (vifp), (m)); \ 1458 } 1459 1460 /* 1461 * If xmt_vif is not -1, send on only the requested vif. 1462 * 1463 * (since vifi_t is u_short, -1 becomes MAXUSHORT, which > numvifs.) 1464 */ 1465 if (xmt_vif < numvifs) { 1466 #ifdef PIM 1467 if (viftable[xmt_vif].v_flags & VIFF_REGISTER) 1468 pim_register_send(ip, viftable + xmt_vif, m, rt); 1469 else 1470 #endif 1471 MC_SEND(ip, viftable + xmt_vif, m); 1472 return 1; 1473 } 1474 1475 /* 1476 * Don't forward if it didn't arrive from the parent vif for its origin. 1477 */ 1478 vifi = rt->mfc_parent; 1479 if ((vifi >= numvifs) || (viftable[vifi].v_ifp != ifp)) { 1480 /* came in the wrong interface */ 1481 if (mrtdebug & DEBUG_FORWARD) 1482 log(LOG_DEBUG, "wrong if: ifp %p vifi %d vififp %p\n", 1483 (void *)ifp, vifi, (void *)viftable[vifi].v_ifp); 1484 ++mrtstat.mrts_wrong_if; 1485 ++rt->mfc_wrong_if; 1486 /* 1487 * If we are doing PIM assert processing, send a message 1488 * to the routing daemon. 1489 * 1490 * XXX: A PIM-SM router needs the WRONGVIF detection so it 1491 * can complete the SPT switch, regardless of the type 1492 * of the iif (broadcast media, GRE tunnel, etc). 1493 */ 1494 if (pim_assert && (vifi < numvifs) && viftable[vifi].v_ifp) { 1495 struct timeval now; 1496 u_long delta; 1497 1498 #ifdef PIM 1499 if (ifp == &multicast_register_if) 1500 pimstat.pims_rcv_registers_wrongiif++; 1501 #endif 1502 1503 /* Get vifi for the incoming packet */ 1504 for (vifi=0; vifi < numvifs && viftable[vifi].v_ifp != ifp; vifi++) 1505 ; 1506 if (vifi >= numvifs) 1507 return 0; /* The iif is not found: ignore the packet. */ 1508 1509 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_DISABLE_WRONGVIF) 1510 return 0; /* WRONGVIF disabled: ignore the packet */ 1511 1512 GET_TIME(now); 1513 1514 TV_DELTA(rt->mfc_last_assert, now, delta); 1515 1516 if (delta > ASSERT_MSG_TIME) { 1517 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET }; 1518 struct igmpmsg *im; 1519 int hlen = ip->ip_hl << 2; 1520 struct mbuf *mm = m_copy(m, 0, hlen); 1521 1522 if (mm && (M_HASCL(mm) || mm->m_len < hlen)) 1523 mm = m_pullup(mm, hlen); 1524 if (mm == NULL) 1525 return ENOBUFS; 1526 1527 rt->mfc_last_assert = now; 1528 1529 im = mtod(mm, struct igmpmsg *); 1530 im->im_msgtype = IGMPMSG_WRONGVIF; 1531 im->im_mbz = 0; 1532 im->im_vif = vifi; 1533 1534 mrtstat.mrts_upcalls++; 1535 1536 k_igmpsrc.sin_addr = im->im_src; 1537 if (socket_send(ip_mrouter, mm, &k_igmpsrc) < 0) { 1538 log(LOG_WARNING, 1539 "ip_mforward: ip_mrouter socket queue full\n"); 1540 ++mrtstat.mrts_upq_sockfull; 1541 return ENOBUFS; 1542 } 1543 } 1544 } 1545 return 0; 1546 } 1547 1548 /* If I sourced this packet, it counts as output, else it was input. */ 1549 if (ip->ip_src.s_addr == viftable[vifi].v_lcl_addr.s_addr) { 1550 viftable[vifi].v_pkt_out++; 1551 viftable[vifi].v_bytes_out += plen; 1552 } else { 1553 viftable[vifi].v_pkt_in++; 1554 viftable[vifi].v_bytes_in += plen; 1555 } 1556 rt->mfc_pkt_cnt++; 1557 rt->mfc_byte_cnt += plen; 1558 1559 /* 1560 * For each vif, decide if a copy of the packet should be forwarded. 1561 * Forward if: 1562 * - the ttl exceeds the vif's threshold 1563 * - there are group members downstream on interface 1564 */ 1565 for (vifi = 0; vifi < numvifs; vifi++) 1566 if ((rt->mfc_ttls[vifi] > 0) && (ip->ip_ttl > rt->mfc_ttls[vifi])) { 1567 viftable[vifi].v_pkt_out++; 1568 viftable[vifi].v_bytes_out += plen; 1569 #ifdef PIM 1570 if (viftable[vifi].v_flags & VIFF_REGISTER) 1571 pim_register_send(ip, viftable + vifi, m, rt); 1572 else 1573 #endif 1574 MC_SEND(ip, viftable+vifi, m); 1575 } 1576 1577 /* 1578 * Perform upcall-related bw measuring. 1579 */ 1580 if (rt->mfc_bw_meter != NULL) { 1581 struct bw_meter *x; 1582 struct timeval now; 1583 1584 GET_TIME(now); 1585 for (x = rt->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) 1586 bw_meter_receive_packet(x, plen, &now); 1587 } 1588 1589 return 0; 1590 } 1591 1592 /* 1593 * check if a vif number is legal/ok. This is used by ip_output. 1594 */ 1595 static int 1596 X_legal_vif_num(int vif) 1597 { 1598 return (vif >= 0 && vif < numvifs); 1599 } 1600 1601 /* 1602 * Return the local address used by this vif 1603 */ 1604 static u_long 1605 X_ip_mcast_src(int vifi) 1606 { 1607 if (vifi >= 0 && vifi < numvifs) 1608 return viftable[vifi].v_lcl_addr.s_addr; 1609 else 1610 return INADDR_ANY; 1611 } 1612 1613 static void 1614 phyint_send(struct ip *ip, struct vif *vifp, struct mbuf *m) 1615 { 1616 struct mbuf *mb_copy; 1617 int hlen = ip->ip_hl << 2; 1618 1619 /* 1620 * Make a new reference to the packet; make sure that 1621 * the IP header is actually copied, not just referenced, 1622 * so that ip_output() only scribbles on the copy. 1623 */ 1624 mb_copy = m_copypacket(m, MB_DONTWAIT); 1625 if (mb_copy && (M_HASCL(mb_copy) || mb_copy->m_len < hlen)) 1626 mb_copy = m_pullup(mb_copy, hlen); 1627 if (mb_copy == NULL) 1628 return; 1629 1630 if (vifp->v_rate_limit == 0) 1631 tbf_send_packet(vifp, mb_copy); 1632 else 1633 tbf_control(vifp, mb_copy, mtod(mb_copy, struct ip *), ip->ip_len); 1634 } 1635 1636 static void 1637 encap_send(struct ip *ip, struct vif *vifp, struct mbuf *m) 1638 { 1639 struct mbuf *mb_copy; 1640 struct ip *ip_copy; 1641 int i, len = ip->ip_len; 1642 1643 /* Take care of delayed checksums */ 1644 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 1645 in_delayed_cksum(m); 1646 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 1647 } 1648 1649 /* 1650 * copy the old packet & pullup its IP header into the 1651 * new mbuf so we can modify it. Try to fill the new 1652 * mbuf since if we don't the ethernet driver will. 1653 */ 1654 MGETHDR(mb_copy, MB_DONTWAIT, MT_HEADER); 1655 if (mb_copy == NULL) 1656 return; 1657 mb_copy->m_data += max_linkhdr; 1658 mb_copy->m_len = sizeof(multicast_encap_iphdr); 1659 1660 if ((mb_copy->m_next = m_copypacket(m, MB_DONTWAIT)) == NULL) { 1661 m_freem(mb_copy); 1662 return; 1663 } 1664 i = MHLEN - M_LEADINGSPACE(mb_copy); 1665 if (i > len) 1666 i = len; 1667 mb_copy = m_pullup(mb_copy, i); 1668 if (mb_copy == NULL) 1669 return; 1670 mb_copy->m_pkthdr.len = len + sizeof(multicast_encap_iphdr); 1671 1672 /* 1673 * fill in the encapsulating IP header. 1674 */ 1675 ip_copy = mtod(mb_copy, struct ip *); 1676 *ip_copy = multicast_encap_iphdr; 1677 ip_copy->ip_id = ip_newid(); 1678 ip_copy->ip_len += len; 1679 ip_copy->ip_src = vifp->v_lcl_addr; 1680 ip_copy->ip_dst = vifp->v_rmt_addr; 1681 1682 /* 1683 * turn the encapsulated IP header back into a valid one. 1684 */ 1685 ip = (struct ip *)((caddr_t)ip_copy + sizeof(multicast_encap_iphdr)); 1686 --ip->ip_ttl; 1687 ip->ip_len = htons(ip->ip_len); 1688 ip->ip_off = htons(ip->ip_off); 1689 ip->ip_sum = 0; 1690 mb_copy->m_data += sizeof(multicast_encap_iphdr); 1691 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2); 1692 mb_copy->m_data -= sizeof(multicast_encap_iphdr); 1693 1694 if (vifp->v_rate_limit == 0) 1695 tbf_send_packet(vifp, mb_copy); 1696 else 1697 tbf_control(vifp, mb_copy, ip, ip_copy->ip_len); 1698 } 1699 1700 /* 1701 * De-encapsulate a packet and feed it back through ip input (this 1702 * routine is called whenever IP gets a packet with proto type 1703 * ENCAP_PROTO and a local destination address). 1704 * 1705 * This is similar to mroute_encapcheck() + mroute_encap_input() in -current. 1706 */ 1707 static void 1708 X_ipip_input(struct mbuf *m, int off, int proto) 1709 { 1710 struct ip *ip = mtod(m, struct ip *); 1711 int hlen = ip->ip_hl << 2; 1712 1713 if (!have_encap_tunnel) { 1714 rip_input(m, off, proto); 1715 return; 1716 } 1717 /* 1718 * dump the packet if it's not to a multicast destination or if 1719 * we don't have an encapsulating tunnel with the source. 1720 * Note: This code assumes that the remote site IP address 1721 * uniquely identifies the tunnel (i.e., that this site has 1722 * at most one tunnel with the remote site). 1723 */ 1724 if (!IN_MULTICAST(ntohl(((struct ip *)((char *)ip+hlen))->ip_dst.s_addr))) { 1725 ++mrtstat.mrts_bad_tunnel; 1726 m_freem(m); 1727 return; 1728 } 1729 if (ip->ip_src.s_addr != last_encap_src) { 1730 struct vif *vifp = viftable; 1731 struct vif *vife = vifp + numvifs; 1732 1733 last_encap_src = ip->ip_src.s_addr; 1734 last_encap_vif = NULL; 1735 for ( ; vifp < vife; ++vifp) 1736 if (vifp->v_rmt_addr.s_addr == ip->ip_src.s_addr) { 1737 if ((vifp->v_flags & (VIFF_TUNNEL|VIFF_SRCRT)) 1738 == VIFF_TUNNEL) 1739 last_encap_vif = vifp; 1740 break; 1741 } 1742 } 1743 if (last_encap_vif == NULL) { 1744 last_encap_src = INADDR_ANY; 1745 mrtstat.mrts_cant_tunnel++; /*XXX*/ 1746 m_freem(m); 1747 if (mrtdebug) 1748 log(LOG_DEBUG, "ip_mforward: no tunnel with %lx\n", 1749 (u_long)ntohl(ip->ip_src.s_addr)); 1750 return; 1751 } 1752 1753 if (hlen > sizeof(struct ip)) 1754 ip_stripoptions(m); 1755 m->m_data += sizeof(struct ip); 1756 m->m_len -= sizeof(struct ip); 1757 m->m_pkthdr.len -= sizeof(struct ip); 1758 m->m_pkthdr.rcvif = last_encap_vif->v_ifp; 1759 1760 netisr_queue(NETISR_IP, m); 1761 } 1762 1763 /* 1764 * Token bucket filter module 1765 */ 1766 1767 static void 1768 tbf_control(struct vif *vifp, struct mbuf *m, struct ip *ip, u_long p_len) 1769 { 1770 struct tbf *t = vifp->v_tbf; 1771 1772 if (p_len > MAX_BKT_SIZE) { /* drop if packet is too large */ 1773 mrtstat.mrts_pkt2large++; 1774 m_freem(m); 1775 return; 1776 } 1777 1778 tbf_update_tokens(vifp); 1779 1780 if (t->tbf_q_len == 0) { /* queue empty... */ 1781 if (p_len <= t->tbf_n_tok) { /* send packet if enough tokens */ 1782 t->tbf_n_tok -= p_len; 1783 tbf_send_packet(vifp, m); 1784 } else { /* no, queue packet and try later */ 1785 tbf_queue(vifp, m); 1786 callout_reset(&tbf_reprocess_q_ch, TBF_REPROCESS, 1787 tbf_reprocess_q, vifp); 1788 } 1789 } else if (t->tbf_q_len < t->tbf_max_q_len) { 1790 /* finite queue length, so queue pkts and process queue */ 1791 tbf_queue(vifp, m); 1792 tbf_process_q(vifp); 1793 } else { 1794 /* queue full, try to dq and queue and process */ 1795 if (!tbf_dq_sel(vifp, ip)) { 1796 mrtstat.mrts_q_overflow++; 1797 m_freem(m); 1798 } else { 1799 tbf_queue(vifp, m); 1800 tbf_process_q(vifp); 1801 } 1802 } 1803 } 1804 1805 /* 1806 * adds a packet to the queue at the interface 1807 */ 1808 static void 1809 tbf_queue(struct vif *vifp, struct mbuf *m) 1810 { 1811 struct tbf *t = vifp->v_tbf; 1812 1813 crit_enter(); 1814 1815 if (t->tbf_t == NULL) /* Queue was empty */ 1816 t->tbf_q = m; 1817 else /* Insert at tail */ 1818 t->tbf_t->m_nextpkt = m; 1819 1820 t->tbf_t = m; /* Set new tail pointer */ 1821 1822 #ifdef DIAGNOSTIC 1823 /* Make sure we didn't get fed a bogus mbuf */ 1824 if (m->m_nextpkt) 1825 panic("tbf_queue: m_nextpkt"); 1826 #endif 1827 m->m_nextpkt = NULL; 1828 1829 t->tbf_q_len++; 1830 1831 crit_exit(); 1832 } 1833 1834 /* 1835 * processes the queue at the interface 1836 */ 1837 static void 1838 tbf_process_q(struct vif *vifp) 1839 { 1840 struct tbf *t = vifp->v_tbf; 1841 1842 crit_enter(); 1843 1844 /* loop through the queue at the interface and send as many packets 1845 * as possible 1846 */ 1847 while (t->tbf_q_len > 0) { 1848 struct mbuf *m = t->tbf_q; 1849 int len = mtod(m, struct ip *)->ip_len; 1850 1851 /* determine if the packet can be sent */ 1852 if (len > t->tbf_n_tok) /* not enough tokens, we are done */ 1853 break; 1854 /* ok, reduce no of tokens, dequeue and send the packet. */ 1855 t->tbf_n_tok -= len; 1856 1857 t->tbf_q = m->m_nextpkt; 1858 if (--t->tbf_q_len == 0) 1859 t->tbf_t = NULL; 1860 1861 m->m_nextpkt = NULL; 1862 tbf_send_packet(vifp, m); 1863 } 1864 crit_exit(); 1865 } 1866 1867 static void 1868 tbf_reprocess_q(void *xvifp) 1869 { 1870 struct vif *vifp = xvifp; 1871 1872 if (ip_mrouter == NULL) 1873 return; 1874 tbf_update_tokens(vifp); 1875 tbf_process_q(vifp); 1876 if (vifp->v_tbf->tbf_q_len) 1877 callout_reset(&tbf_reprocess_q_ch, TBF_REPROCESS, 1878 tbf_reprocess_q, vifp); 1879 } 1880 1881 /* function that will selectively discard a member of the queue 1882 * based on the precedence value and the priority 1883 */ 1884 static int 1885 tbf_dq_sel(struct vif *vifp, struct ip *ip) 1886 { 1887 u_int p; 1888 struct mbuf *m, *last; 1889 struct mbuf **np; 1890 struct tbf *t = vifp->v_tbf; 1891 1892 crit_enter(); 1893 1894 p = priority(vifp, ip); 1895 1896 np = &t->tbf_q; 1897 last = NULL; 1898 while ((m = *np) != NULL) { 1899 if (p > priority(vifp, mtod(m, struct ip *))) { 1900 *np = m->m_nextpkt; 1901 /* If we're removing the last packet, fix the tail pointer */ 1902 if (m == t->tbf_t) 1903 t->tbf_t = last; 1904 m_freem(m); 1905 /* It's impossible for the queue to be empty, but check anyways. */ 1906 if (--t->tbf_q_len == 0) 1907 t->tbf_t = NULL; 1908 crit_exit(); 1909 mrtstat.mrts_drop_sel++; 1910 return 1; 1911 } 1912 np = &m->m_nextpkt; 1913 last = m; 1914 } 1915 crit_exit(); 1916 return 0; 1917 } 1918 1919 static void 1920 tbf_send_packet(struct vif *vifp, struct mbuf *m) 1921 { 1922 crit_enter(); 1923 1924 if (vifp->v_flags & VIFF_TUNNEL) /* If tunnel options */ 1925 ip_output(m, NULL, &vifp->v_route, IP_FORWARDING, NULL, NULL); 1926 else { 1927 struct ip_moptions imo; 1928 int error; 1929 static struct route ro; /* XXX check this */ 1930 1931 imo.imo_multicast_ifp = vifp->v_ifp; 1932 imo.imo_multicast_ttl = mtod(m, struct ip *)->ip_ttl - 1; 1933 imo.imo_multicast_loop = 1; 1934 imo.imo_multicast_vif = -1; 1935 1936 /* 1937 * Re-entrancy should not be a problem here, because 1938 * the packets that we send out and are looped back at us 1939 * should get rejected because they appear to come from 1940 * the loopback interface, thus preventing looping. 1941 */ 1942 error = ip_output(m, NULL, &ro, IP_FORWARDING, &imo, NULL); 1943 1944 if (mrtdebug & DEBUG_XMIT) 1945 log(LOG_DEBUG, "phyint_send on vif %d err %d\n", 1946 (int)(vifp - viftable), error); 1947 } 1948 crit_exit(); 1949 } 1950 1951 /* determine the current time and then 1952 * the elapsed time (between the last time and time now) 1953 * in milliseconds & update the no. of tokens in the bucket 1954 */ 1955 static void 1956 tbf_update_tokens(struct vif *vifp) 1957 { 1958 struct timeval tp; 1959 u_long tm; 1960 struct tbf *t = vifp->v_tbf; 1961 1962 crit_enter(); 1963 1964 GET_TIME(tp); 1965 1966 TV_DELTA(tp, t->tbf_last_pkt_t, tm); 1967 1968 /* 1969 * This formula is actually 1970 * "time in seconds" * "bytes/second". 1971 * 1972 * (tm / 1000000) * (v_rate_limit * 1000 * (1000/1024) / 8) 1973 * 1974 * The (1000/1024) was introduced in add_vif to optimize 1975 * this divide into a shift. 1976 */ 1977 t->tbf_n_tok += tm * vifp->v_rate_limit / 1024 / 8; 1978 t->tbf_last_pkt_t = tp; 1979 1980 if (t->tbf_n_tok > MAX_BKT_SIZE) 1981 t->tbf_n_tok = MAX_BKT_SIZE; 1982 1983 crit_exit(); 1984 } 1985 1986 static int 1987 priority(struct vif *vifp, struct ip *ip) 1988 { 1989 int prio = 50; /* the lowest priority -- default case */ 1990 1991 /* temporary hack; may add general packet classifier some day */ 1992 1993 /* 1994 * The UDP port space is divided up into four priority ranges: 1995 * [0, 16384) : unclassified - lowest priority 1996 * [16384, 32768) : audio - highest priority 1997 * [32768, 49152) : whiteboard - medium priority 1998 * [49152, 65536) : video - low priority 1999 * 2000 * Everything else gets lowest priority. 2001 */ 2002 if (ip->ip_p == IPPROTO_UDP) { 2003 struct udphdr *udp = (struct udphdr *)(((char *)ip) + (ip->ip_hl << 2)); 2004 switch (ntohs(udp->uh_dport) & 0xc000) { 2005 case 0x4000: 2006 prio = 70; 2007 break; 2008 case 0x8000: 2009 prio = 60; 2010 break; 2011 case 0xc000: 2012 prio = 55; 2013 break; 2014 } 2015 } 2016 return prio; 2017 } 2018 2019 /* 2020 * End of token bucket filter modifications 2021 */ 2022 2023 static int 2024 X_ip_rsvp_vif(struct socket *so, struct sockopt *sopt) 2025 { 2026 int error, vifi; 2027 2028 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP) 2029 return EOPNOTSUPP; 2030 2031 error = sooptcopyin(sopt, &vifi, sizeof vifi, sizeof vifi); 2032 if (error) 2033 return error; 2034 2035 crit_enter(); 2036 2037 if (vifi < 0 || vifi >= numvifs) { /* Error if vif is invalid */ 2038 crit_exit(); 2039 return EADDRNOTAVAIL; 2040 } 2041 2042 if (sopt->sopt_name == IP_RSVP_VIF_ON) { 2043 /* Check if socket is available. */ 2044 if (viftable[vifi].v_rsvpd != NULL) { 2045 crit_exit(); 2046 return EADDRINUSE; 2047 } 2048 2049 viftable[vifi].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[vifi].v_rsvp_on) { 2054 viftable[vifi].v_rsvp_on = 1; 2055 rsvp_on++; 2056 } 2057 } else { /* must be VIF_OFF */ 2058 /* 2059 * XXX as an additional consistency check, one could make sure 2060 * that viftable[vifi].v_rsvpd == so, otherwise passing so as 2061 * first parameter is pretty useless. 2062 */ 2063 viftable[vifi].v_rsvpd = NULL; 2064 /* 2065 * This may seem silly, but we need to be sure we don't over-decrement 2066 * the RSVP counter, in case something slips up. 2067 */ 2068 if (viftable[vifi].v_rsvp_on) { 2069 viftable[vifi].v_rsvp_on = 0; 2070 rsvp_on--; 2071 } 2072 } 2073 crit_exit(); 2074 return 0; 2075 } 2076 2077 static void 2078 X_ip_rsvp_force_done(struct socket *so) 2079 { 2080 int vifi; 2081 2082 /* Don't bother if it is not the right type of socket. */ 2083 if (so->so_type != SOCK_RAW || so->so_proto->pr_protocol != IPPROTO_RSVP) 2084 return; 2085 2086 crit_enter(); 2087 2088 /* The socket may be attached to more than one vif...this 2089 * is perfectly legal. 2090 */ 2091 for (vifi = 0; vifi < numvifs; vifi++) { 2092 if (viftable[vifi].v_rsvpd == so) { 2093 viftable[vifi].v_rsvpd = NULL; 2094 /* This may seem silly, but we need to be sure we don't 2095 * over-decrement the RSVP counter, in case something slips up. 2096 */ 2097 if (viftable[vifi].v_rsvp_on) { 2098 viftable[vifi].v_rsvp_on = 0; 2099 rsvp_on--; 2100 } 2101 } 2102 } 2103 2104 crit_exit(); 2105 } 2106 2107 static void 2108 X_rsvp_input(struct mbuf *m, ...) 2109 { 2110 int vifi; 2111 struct ip *ip = mtod(m, struct ip *); 2112 struct sockaddr_in rsvp_src = { sizeof rsvp_src, AF_INET }; 2113 struct ifnet *ifp; 2114 int off, proto; 2115 #ifdef ALTQ 2116 /* support IP_RECVIF used by rsvpd rel4.2a1 */ 2117 struct inpcb *inp; 2118 struct socket *so; 2119 struct mbuf *opts; 2120 #endif 2121 __va_list ap; 2122 2123 __va_start(ap, m); 2124 off = __va_arg(ap, int); 2125 proto = __va_arg(ap, int); 2126 __va_end(ap); 2127 2128 if (rsvpdebug) 2129 kprintf("rsvp_input: rsvp_on %d\n",rsvp_on); 2130 2131 /* Can still get packets with rsvp_on = 0 if there is a local member 2132 * of the group to which the RSVP packet is addressed. But in this 2133 * case we want to throw the packet away. 2134 */ 2135 if (!rsvp_on) { 2136 m_freem(m); 2137 return; 2138 } 2139 2140 crit_enter(); 2141 2142 if (rsvpdebug) 2143 kprintf("rsvp_input: check vifs\n"); 2144 2145 #ifdef DIAGNOSTIC 2146 if (!(m->m_flags & M_PKTHDR)) 2147 panic("rsvp_input no hdr"); 2148 #endif 2149 2150 ifp = m->m_pkthdr.rcvif; 2151 /* Find which vif the packet arrived on. */ 2152 for (vifi = 0; vifi < numvifs; vifi++) 2153 if (viftable[vifi].v_ifp == ifp) 2154 break; 2155 2156 #ifdef ALTQ 2157 if (vifi == numvifs || (so = viftable[vifi].v_rsvpd) == NULL) { 2158 #else 2159 if (vifi == numvifs || viftable[vifi].v_rsvpd == NULL) { 2160 #endif 2161 /* 2162 * If the old-style non-vif-associated socket is set, 2163 * then use it. Otherwise, drop packet since there 2164 * is no specific socket for this vif. 2165 */ 2166 if (ip_rsvpd != NULL) { 2167 if (rsvpdebug) 2168 kprintf("rsvp_input: Sending packet up old-style socket\n"); 2169 rip_input(m, off, proto); /* xxx */ 2170 } else { 2171 if (rsvpdebug && vifi == numvifs) 2172 kprintf("rsvp_input: Can't find vif for packet.\n"); 2173 else if (rsvpdebug && viftable[vifi].v_rsvpd == NULL) 2174 kprintf("rsvp_input: No socket defined for vif %d\n",vifi); 2175 m_freem(m); 2176 } 2177 crit_exit(); 2178 return; 2179 } 2180 rsvp_src.sin_addr = ip->ip_src; 2181 2182 if (rsvpdebug && m) 2183 kprintf("rsvp_input: m->m_len = %d, ssb_space() = %ld\n", 2184 m->m_len,ssb_space(&(viftable[vifi].v_rsvpd->so_rcv))); 2185 2186 #ifdef ALTQ 2187 opts = NULL; 2188 inp = (struct inpcb *)so->so_pcb; 2189 if (inp->inp_flags & INP_CONTROLOPTS || 2190 inp->inp_socket->so_options & SO_TIMESTAMP) 2191 ip_savecontrol(inp, &opts, ip, m); 2192 if (ssb_appendaddr(&so->so_rcv, 2193 (struct sockaddr *)&rsvp_src,m, opts) == 0) { 2194 m_freem(m); 2195 if (opts) 2196 m_freem(opts); 2197 if (rsvpdebug) 2198 kprintf("rsvp_input: Failed to append to socket\n"); 2199 } 2200 else { 2201 sorwakeup(so); 2202 if (rsvpdebug) 2203 kprintf("rsvp_input: send packet up\n"); 2204 } 2205 #else /* !ALTQ */ 2206 if (socket_send(viftable[vifi].v_rsvpd, m, &rsvp_src) < 0) { 2207 if (rsvpdebug) 2208 kprintf("rsvp_input: Failed to append to socket\n"); 2209 } else { 2210 if (rsvpdebug) 2211 kprintf("rsvp_input: send packet up\n"); 2212 } 2213 #endif /* !ALTQ */ 2214 2215 crit_exit(); 2216 } 2217 2218 /* 2219 * Code for bandwidth monitors 2220 */ 2221 2222 /* 2223 * Define common interface for timeval-related methods 2224 */ 2225 #define BW_TIMEVALCMP(tvp, uvp, cmp) timevalcmp((tvp), (uvp), cmp) 2226 #define BW_TIMEVALDECR(vvp, uvp) timevalsub((vvp), (uvp)) 2227 #define BW_TIMEVALADD(vvp, uvp) timevaladd((vvp), (uvp)) 2228 2229 static uint32_t 2230 compute_bw_meter_flags(struct bw_upcall *req) 2231 { 2232 uint32_t flags = 0; 2233 2234 if (req->bu_flags & BW_UPCALL_UNIT_PACKETS) 2235 flags |= BW_METER_UNIT_PACKETS; 2236 if (req->bu_flags & BW_UPCALL_UNIT_BYTES) 2237 flags |= BW_METER_UNIT_BYTES; 2238 if (req->bu_flags & BW_UPCALL_GEQ) 2239 flags |= BW_METER_GEQ; 2240 if (req->bu_flags & BW_UPCALL_LEQ) 2241 flags |= BW_METER_LEQ; 2242 2243 return flags; 2244 } 2245 2246 /* 2247 * Add a bw_meter entry 2248 */ 2249 static int 2250 add_bw_upcall(struct bw_upcall *req) 2251 { 2252 struct mfc *mfc; 2253 struct timeval delta = { BW_UPCALL_THRESHOLD_INTERVAL_MIN_SEC, 2254 BW_UPCALL_THRESHOLD_INTERVAL_MIN_USEC }; 2255 struct timeval now; 2256 struct bw_meter *x; 2257 uint32_t flags; 2258 2259 if (!(mrt_api_config & MRT_MFC_BW_UPCALL)) 2260 return EOPNOTSUPP; 2261 2262 /* Test if the flags are valid */ 2263 if (!(req->bu_flags & (BW_UPCALL_UNIT_PACKETS | BW_UPCALL_UNIT_BYTES))) 2264 return EINVAL; 2265 if (!(req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ))) 2266 return EINVAL; 2267 if ((req->bu_flags & (BW_UPCALL_GEQ | BW_UPCALL_LEQ)) 2268 == (BW_UPCALL_GEQ | BW_UPCALL_LEQ)) 2269 return EINVAL; 2270 2271 /* Test if the threshold time interval is valid */ 2272 if (BW_TIMEVALCMP(&req->bu_threshold.b_time, &delta, <)) 2273 return EINVAL; 2274 2275 flags = compute_bw_meter_flags(req); 2276 2277 /* 2278 * Find if we have already same bw_meter entry 2279 */ 2280 crit_enter(); 2281 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr); 2282 if (mfc == NULL) { 2283 crit_exit(); 2284 return EADDRNOTAVAIL; 2285 } 2286 for (x = mfc->mfc_bw_meter; x != NULL; x = x->bm_mfc_next) { 2287 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time, 2288 &req->bu_threshold.b_time, ==)) && 2289 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) && 2290 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) && 2291 (x->bm_flags & BW_METER_USER_FLAGS) == flags) { 2292 crit_exit(); 2293 return 0; /* XXX Already installed */ 2294 } 2295 } 2296 crit_exit(); 2297 2298 /* Allocate the new bw_meter entry */ 2299 x = kmalloc(sizeof(*x), M_BWMETER, M_INTWAIT); 2300 2301 /* Set the new bw_meter entry */ 2302 x->bm_threshold.b_time = req->bu_threshold.b_time; 2303 GET_TIME(now); 2304 x->bm_start_time = now; 2305 x->bm_threshold.b_packets = req->bu_threshold.b_packets; 2306 x->bm_threshold.b_bytes = req->bu_threshold.b_bytes; 2307 x->bm_measured.b_packets = 0; 2308 x->bm_measured.b_bytes = 0; 2309 x->bm_flags = flags; 2310 x->bm_time_next = NULL; 2311 x->bm_time_hash = BW_METER_BUCKETS; 2312 2313 /* Add the new bw_meter entry to the front of entries for this MFC */ 2314 crit_enter(); 2315 x->bm_mfc = mfc; 2316 x->bm_mfc_next = mfc->mfc_bw_meter; 2317 mfc->mfc_bw_meter = x; 2318 schedule_bw_meter(x, &now); 2319 crit_exit(); 2320 2321 return 0; 2322 } 2323 2324 static void 2325 free_bw_list(struct bw_meter *list) 2326 { 2327 while (list != NULL) { 2328 struct bw_meter *x = list; 2329 2330 list = list->bm_mfc_next; 2331 unschedule_bw_meter(x); 2332 kfree(x, M_BWMETER); 2333 } 2334 } 2335 2336 /* 2337 * Delete one or multiple bw_meter entries 2338 */ 2339 static int 2340 del_bw_upcall(struct bw_upcall *req) 2341 { 2342 struct mfc *mfc; 2343 struct bw_meter *x; 2344 2345 if (!(mrt_api_config & MRT_MFC_BW_UPCALL)) 2346 return EOPNOTSUPP; 2347 2348 crit_enter(); 2349 /* Find the corresponding MFC entry */ 2350 mfc = mfc_find(req->bu_src.s_addr, req->bu_dst.s_addr); 2351 if (mfc == NULL) { 2352 crit_exit(); 2353 return EADDRNOTAVAIL; 2354 } else if (req->bu_flags & BW_UPCALL_DELETE_ALL) { 2355 /* 2356 * Delete all bw_meter entries for this mfc 2357 */ 2358 struct bw_meter *list; 2359 2360 list = mfc->mfc_bw_meter; 2361 mfc->mfc_bw_meter = NULL; 2362 crit_exit(); 2363 free_bw_list(list); 2364 return 0; 2365 } else { /* Delete a single bw_meter entry */ 2366 struct bw_meter *prev; 2367 uint32_t flags = 0; 2368 2369 flags = compute_bw_meter_flags(req); 2370 2371 /* Find the bw_meter entry to delete */ 2372 for (prev = NULL, x = mfc->mfc_bw_meter; x != NULL; 2373 prev = x, x = x->bm_mfc_next) { 2374 if ((BW_TIMEVALCMP(&x->bm_threshold.b_time, 2375 &req->bu_threshold.b_time, ==)) && 2376 (x->bm_threshold.b_packets == req->bu_threshold.b_packets) && 2377 (x->bm_threshold.b_bytes == req->bu_threshold.b_bytes) && 2378 (x->bm_flags & BW_METER_USER_FLAGS) == flags) 2379 break; 2380 } 2381 if (x != NULL) { /* Delete entry from the list for this MFC */ 2382 if (prev != NULL) 2383 prev->bm_mfc_next = x->bm_mfc_next; /* remove from middle*/ 2384 else 2385 x->bm_mfc->mfc_bw_meter = x->bm_mfc_next;/* new head of list */ 2386 crit_exit(); 2387 2388 unschedule_bw_meter(x); 2389 /* Free the bw_meter entry */ 2390 kfree(x, M_BWMETER); 2391 return 0; 2392 } else { 2393 crit_exit(); 2394 return EINVAL; 2395 } 2396 } 2397 /* NOTREACHED */ 2398 } 2399 2400 /* 2401 * Perform bandwidth measurement processing that may result in an upcall 2402 */ 2403 static void 2404 bw_meter_receive_packet(struct bw_meter *x, int plen, struct timeval *nowp) 2405 { 2406 struct timeval delta; 2407 2408 crit_enter(); 2409 delta = *nowp; 2410 BW_TIMEVALDECR(&delta, &x->bm_start_time); 2411 2412 if (x->bm_flags & BW_METER_GEQ) { 2413 /* 2414 * Processing for ">=" type of bw_meter entry 2415 */ 2416 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) { 2417 /* Reset the bw_meter entry */ 2418 x->bm_start_time = *nowp; 2419 x->bm_measured.b_packets = 0; 2420 x->bm_measured.b_bytes = 0; 2421 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED; 2422 } 2423 2424 /* Record that a packet is received */ 2425 x->bm_measured.b_packets++; 2426 x->bm_measured.b_bytes += plen; 2427 2428 /* 2429 * Test if we should deliver an upcall 2430 */ 2431 if (!(x->bm_flags & BW_METER_UPCALL_DELIVERED)) { 2432 if (((x->bm_flags & BW_METER_UNIT_PACKETS) && 2433 (x->bm_measured.b_packets >= x->bm_threshold.b_packets)) || 2434 ((x->bm_flags & BW_METER_UNIT_BYTES) && 2435 (x->bm_measured.b_bytes >= x->bm_threshold.b_bytes))) { 2436 /* Prepare an upcall for delivery */ 2437 bw_meter_prepare_upcall(x, nowp); 2438 x->bm_flags |= BW_METER_UPCALL_DELIVERED; 2439 } 2440 } 2441 } else if (x->bm_flags & BW_METER_LEQ) { 2442 /* 2443 * Processing for "<=" type of bw_meter entry 2444 */ 2445 if (BW_TIMEVALCMP(&delta, &x->bm_threshold.b_time, >)) { 2446 /* 2447 * We are behind time with the multicast forwarding table 2448 * scanning for "<=" type of bw_meter entries, so test now 2449 * if we should deliver an upcall. 2450 */ 2451 if (((x->bm_flags & BW_METER_UNIT_PACKETS) && 2452 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) || 2453 ((x->bm_flags & BW_METER_UNIT_BYTES) && 2454 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) { 2455 /* Prepare an upcall for delivery */ 2456 bw_meter_prepare_upcall(x, nowp); 2457 } 2458 /* Reschedule the bw_meter entry */ 2459 unschedule_bw_meter(x); 2460 schedule_bw_meter(x, nowp); 2461 } 2462 2463 /* Record that a packet is received */ 2464 x->bm_measured.b_packets++; 2465 x->bm_measured.b_bytes += plen; 2466 2467 /* 2468 * Test if we should restart the measuring interval 2469 */ 2470 if ((x->bm_flags & BW_METER_UNIT_PACKETS && 2471 x->bm_measured.b_packets <= x->bm_threshold.b_packets) || 2472 (x->bm_flags & BW_METER_UNIT_BYTES && 2473 x->bm_measured.b_bytes <= x->bm_threshold.b_bytes)) { 2474 /* Don't restart the measuring interval */ 2475 } else { 2476 /* Do restart the measuring interval */ 2477 /* 2478 * XXX: note that we don't unschedule and schedule, because this 2479 * might be too much overhead per packet. Instead, when we process 2480 * all entries for a given timer hash bin, we check whether it is 2481 * really a timeout. If not, we reschedule at that time. 2482 */ 2483 x->bm_start_time = *nowp; 2484 x->bm_measured.b_packets = 0; 2485 x->bm_measured.b_bytes = 0; 2486 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED; 2487 } 2488 } 2489 crit_exit(); 2490 } 2491 2492 /* 2493 * Prepare a bandwidth-related upcall 2494 */ 2495 static void 2496 bw_meter_prepare_upcall(struct bw_meter *x, struct timeval *nowp) 2497 { 2498 struct timeval delta; 2499 struct bw_upcall *u; 2500 2501 crit_enter(); 2502 2503 /* 2504 * Compute the measured time interval 2505 */ 2506 delta = *nowp; 2507 BW_TIMEVALDECR(&delta, &x->bm_start_time); 2508 2509 /* 2510 * If there are too many pending upcalls, deliver them now 2511 */ 2512 if (bw_upcalls_n >= BW_UPCALLS_MAX) 2513 bw_upcalls_send(); 2514 2515 /* 2516 * Set the bw_upcall entry 2517 */ 2518 u = &bw_upcalls[bw_upcalls_n++]; 2519 u->bu_src = x->bm_mfc->mfc_origin; 2520 u->bu_dst = x->bm_mfc->mfc_mcastgrp; 2521 u->bu_threshold.b_time = x->bm_threshold.b_time; 2522 u->bu_threshold.b_packets = x->bm_threshold.b_packets; 2523 u->bu_threshold.b_bytes = x->bm_threshold.b_bytes; 2524 u->bu_measured.b_time = delta; 2525 u->bu_measured.b_packets = x->bm_measured.b_packets; 2526 u->bu_measured.b_bytes = x->bm_measured.b_bytes; 2527 u->bu_flags = 0; 2528 if (x->bm_flags & BW_METER_UNIT_PACKETS) 2529 u->bu_flags |= BW_UPCALL_UNIT_PACKETS; 2530 if (x->bm_flags & BW_METER_UNIT_BYTES) 2531 u->bu_flags |= BW_UPCALL_UNIT_BYTES; 2532 if (x->bm_flags & BW_METER_GEQ) 2533 u->bu_flags |= BW_UPCALL_GEQ; 2534 if (x->bm_flags & BW_METER_LEQ) 2535 u->bu_flags |= BW_UPCALL_LEQ; 2536 2537 crit_exit(); 2538 } 2539 2540 /* 2541 * Send the pending bandwidth-related upcalls 2542 */ 2543 static void 2544 bw_upcalls_send(void) 2545 { 2546 struct mbuf *m; 2547 int len = bw_upcalls_n * sizeof(bw_upcalls[0]); 2548 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET }; 2549 static struct igmpmsg igmpmsg = { 0, /* unused1 */ 2550 0, /* unused2 */ 2551 IGMPMSG_BW_UPCALL,/* im_msgtype */ 2552 0, /* im_mbz */ 2553 0, /* im_vif */ 2554 0, /* unused3 */ 2555 { 0 }, /* im_src */ 2556 { 0 } }; /* im_dst */ 2557 2558 if (bw_upcalls_n == 0) 2559 return; /* No pending upcalls */ 2560 2561 bw_upcalls_n = 0; 2562 2563 /* 2564 * Allocate a new mbuf, initialize it with the header and 2565 * the payload for the pending calls. 2566 */ 2567 MGETHDR(m, MB_DONTWAIT, MT_HEADER); 2568 if (m == NULL) { 2569 log(LOG_WARNING, "bw_upcalls_send: cannot allocate mbuf\n"); 2570 return; 2571 } 2572 2573 m->m_len = m->m_pkthdr.len = 0; 2574 m_copyback(m, 0, sizeof(struct igmpmsg), (caddr_t)&igmpmsg); 2575 m_copyback(m, sizeof(struct igmpmsg), len, (caddr_t)&bw_upcalls[0]); 2576 2577 /* 2578 * Send the upcalls 2579 * XXX do we need to set the address in k_igmpsrc ? 2580 */ 2581 mrtstat.mrts_upcalls++; 2582 if (socket_send(ip_mrouter, m, &k_igmpsrc) < 0) { 2583 log(LOG_WARNING, "bw_upcalls_send: ip_mrouter socket queue full\n"); 2584 ++mrtstat.mrts_upq_sockfull; 2585 } 2586 } 2587 2588 /* 2589 * Compute the timeout hash value for the bw_meter entries 2590 */ 2591 #define BW_METER_TIMEHASH(bw_meter, hash) \ 2592 do { \ 2593 struct timeval next_timeval = (bw_meter)->bm_start_time; \ 2594 \ 2595 BW_TIMEVALADD(&next_timeval, &(bw_meter)->bm_threshold.b_time); \ 2596 (hash) = next_timeval.tv_sec; \ 2597 if (next_timeval.tv_usec) \ 2598 (hash)++; /* XXX: make sure we don't timeout early */ \ 2599 (hash) %= BW_METER_BUCKETS; \ 2600 } while (0) 2601 2602 /* 2603 * Schedule a timer to process periodically bw_meter entry of type "<=" 2604 * by linking the entry in the proper hash bucket. 2605 */ 2606 static void 2607 schedule_bw_meter(struct bw_meter *x, struct timeval *nowp) 2608 { 2609 int time_hash; 2610 2611 if (!(x->bm_flags & BW_METER_LEQ)) 2612 return; /* XXX: we schedule timers only for "<=" entries */ 2613 2614 /* 2615 * Reset the bw_meter entry 2616 */ 2617 crit_enter(); 2618 x->bm_start_time = *nowp; 2619 x->bm_measured.b_packets = 0; 2620 x->bm_measured.b_bytes = 0; 2621 x->bm_flags &= ~BW_METER_UPCALL_DELIVERED; 2622 crit_exit(); 2623 2624 /* 2625 * Compute the timeout hash value and insert the entry 2626 */ 2627 BW_METER_TIMEHASH(x, time_hash); 2628 x->bm_time_next = bw_meter_timers[time_hash]; 2629 bw_meter_timers[time_hash] = x; 2630 x->bm_time_hash = time_hash; 2631 } 2632 2633 /* 2634 * Unschedule the periodic timer that processes bw_meter entry of type "<=" 2635 * by removing the entry from the proper hash bucket. 2636 */ 2637 static void 2638 unschedule_bw_meter(struct bw_meter *x) 2639 { 2640 int time_hash; 2641 struct bw_meter *prev, *tmp; 2642 2643 if (!(x->bm_flags & BW_METER_LEQ)) 2644 return; /* XXX: we schedule timers only for "<=" entries */ 2645 2646 /* 2647 * Compute the timeout hash value and delete the entry 2648 */ 2649 time_hash = x->bm_time_hash; 2650 if (time_hash >= BW_METER_BUCKETS) 2651 return; /* Entry was not scheduled */ 2652 2653 for (prev = NULL, tmp = bw_meter_timers[time_hash]; 2654 tmp != NULL; prev = tmp, tmp = tmp->bm_time_next) 2655 if (tmp == x) 2656 break; 2657 2658 if (tmp == NULL) 2659 panic("unschedule_bw_meter: bw_meter entry not found"); 2660 2661 if (prev != NULL) 2662 prev->bm_time_next = x->bm_time_next; 2663 else 2664 bw_meter_timers[time_hash] = x->bm_time_next; 2665 2666 x->bm_time_next = NULL; 2667 x->bm_time_hash = BW_METER_BUCKETS; 2668 } 2669 2670 2671 /* 2672 * Process all "<=" type of bw_meter that should be processed now, 2673 * and for each entry prepare an upcall if necessary. Each processed 2674 * entry is rescheduled again for the (periodic) processing. 2675 * 2676 * This is run periodically (once per second normally). On each round, 2677 * all the potentially matching entries are in the hash slot that we are 2678 * looking at. 2679 */ 2680 static void 2681 bw_meter_process(void) 2682 { 2683 static uint32_t last_tv_sec; /* last time we processed this */ 2684 2685 uint32_t loops; 2686 int i; 2687 struct timeval now, process_endtime; 2688 2689 GET_TIME(now); 2690 if (last_tv_sec == now.tv_sec) 2691 return; /* nothing to do */ 2692 2693 crit_enter(); 2694 loops = now.tv_sec - last_tv_sec; 2695 last_tv_sec = now.tv_sec; 2696 if (loops > BW_METER_BUCKETS) 2697 loops = BW_METER_BUCKETS; 2698 2699 /* 2700 * Process all bins of bw_meter entries from the one after the last 2701 * processed to the current one. On entry, i points to the last bucket 2702 * visited, so we need to increment i at the beginning of the loop. 2703 */ 2704 for (i = (now.tv_sec - loops) % BW_METER_BUCKETS; loops > 0; loops--) { 2705 struct bw_meter *x, *tmp_list; 2706 2707 if (++i >= BW_METER_BUCKETS) 2708 i = 0; 2709 2710 /* Disconnect the list of bw_meter entries from the bin */ 2711 tmp_list = bw_meter_timers[i]; 2712 bw_meter_timers[i] = NULL; 2713 2714 /* Process the list of bw_meter entries */ 2715 while (tmp_list != NULL) { 2716 x = tmp_list; 2717 tmp_list = tmp_list->bm_time_next; 2718 2719 /* Test if the time interval is over */ 2720 process_endtime = x->bm_start_time; 2721 BW_TIMEVALADD(&process_endtime, &x->bm_threshold.b_time); 2722 if (BW_TIMEVALCMP(&process_endtime, &now, >)) { 2723 /* Not yet: reschedule, but don't reset */ 2724 int time_hash; 2725 2726 BW_METER_TIMEHASH(x, time_hash); 2727 if (time_hash == i && process_endtime.tv_sec == now.tv_sec) { 2728 /* 2729 * XXX: somehow the bin processing is a bit ahead of time. 2730 * Put the entry in the next bin. 2731 */ 2732 if (++time_hash >= BW_METER_BUCKETS) 2733 time_hash = 0; 2734 } 2735 x->bm_time_next = bw_meter_timers[time_hash]; 2736 bw_meter_timers[time_hash] = x; 2737 x->bm_time_hash = time_hash; 2738 2739 continue; 2740 } 2741 2742 /* 2743 * Test if we should deliver an upcall 2744 */ 2745 if (((x->bm_flags & BW_METER_UNIT_PACKETS) && 2746 (x->bm_measured.b_packets <= x->bm_threshold.b_packets)) || 2747 ((x->bm_flags & BW_METER_UNIT_BYTES) && 2748 (x->bm_measured.b_bytes <= x->bm_threshold.b_bytes))) { 2749 /* Prepare an upcall for delivery */ 2750 bw_meter_prepare_upcall(x, &now); 2751 } 2752 2753 /* 2754 * Reschedule for next processing 2755 */ 2756 schedule_bw_meter(x, &now); 2757 } 2758 } 2759 crit_exit(); 2760 2761 /* Send all upcalls that are pending delivery */ 2762 bw_upcalls_send(); 2763 } 2764 2765 /* 2766 * A periodic function for sending all upcalls that are pending delivery 2767 */ 2768 static void 2769 expire_bw_upcalls_send(void *unused) 2770 { 2771 bw_upcalls_send(); 2772 2773 callout_reset(&bw_upcalls_ch, BW_UPCALLS_PERIOD, 2774 expire_bw_upcalls_send, NULL); 2775 } 2776 2777 /* 2778 * A periodic function for periodic scanning of the multicast forwarding 2779 * table for processing all "<=" bw_meter entries. 2780 */ 2781 static void 2782 expire_bw_meter_process(void *unused) 2783 { 2784 if (mrt_api_config & MRT_MFC_BW_UPCALL) 2785 bw_meter_process(); 2786 2787 callout_reset(&bw_meter_ch, BW_METER_PERIOD, 2788 expire_bw_meter_process, NULL); 2789 } 2790 2791 /* 2792 * End of bandwidth monitoring code 2793 */ 2794 2795 #ifdef PIM 2796 /* 2797 * Send the packet up to the user daemon, or eventually do kernel encapsulation 2798 * 2799 */ 2800 static int 2801 pim_register_send(struct ip *ip, struct vif *vifp, 2802 struct mbuf *m, struct mfc *rt) 2803 { 2804 struct mbuf *mb_copy, *mm; 2805 2806 if (mrtdebug & DEBUG_PIM) 2807 log(LOG_DEBUG, "pim_register_send: "); 2808 2809 mb_copy = pim_register_prepare(ip, m); 2810 if (mb_copy == NULL) 2811 return ENOBUFS; 2812 2813 /* 2814 * Send all the fragments. Note that the mbuf for each fragment 2815 * is freed by the sending machinery. 2816 */ 2817 for (mm = mb_copy; mm; mm = mb_copy) { 2818 mb_copy = mm->m_nextpkt; 2819 mm->m_nextpkt = 0; 2820 mm = m_pullup(mm, sizeof(struct ip)); 2821 if (mm != NULL) { 2822 ip = mtod(mm, struct ip *); 2823 if ((mrt_api_config & MRT_MFC_RP) && 2824 (rt->mfc_rp.s_addr != INADDR_ANY)) { 2825 pim_register_send_rp(ip, vifp, mm, rt); 2826 } else { 2827 pim_register_send_upcall(ip, vifp, mm, rt); 2828 } 2829 } 2830 } 2831 2832 return 0; 2833 } 2834 2835 /* 2836 * Return a copy of the data packet that is ready for PIM Register 2837 * encapsulation. 2838 * XXX: Note that in the returned copy the IP header is a valid one. 2839 */ 2840 static struct mbuf * 2841 pim_register_prepare(struct ip *ip, struct mbuf *m) 2842 { 2843 struct mbuf *mb_copy = NULL; 2844 int mtu; 2845 2846 /* Take care of delayed checksums */ 2847 if (m->m_pkthdr.csum_flags & CSUM_DELAY_DATA) { 2848 in_delayed_cksum(m); 2849 m->m_pkthdr.csum_flags &= ~CSUM_DELAY_DATA; 2850 } 2851 2852 /* 2853 * Copy the old packet & pullup its IP header into the 2854 * new mbuf so we can modify it. 2855 */ 2856 mb_copy = m_copypacket(m, MB_DONTWAIT); 2857 if (mb_copy == NULL) 2858 return NULL; 2859 mb_copy = m_pullup(mb_copy, ip->ip_hl << 2); 2860 if (mb_copy == NULL) 2861 return NULL; 2862 2863 /* take care of the TTL */ 2864 ip = mtod(mb_copy, struct ip *); 2865 --ip->ip_ttl; 2866 2867 /* Compute the MTU after the PIM Register encapsulation */ 2868 mtu = 0xffff - sizeof(pim_encap_iphdr) - sizeof(pim_encap_pimhdr); 2869 2870 if (ip->ip_len <= mtu) { 2871 /* Turn the IP header into a valid one */ 2872 ip->ip_len = htons(ip->ip_len); 2873 ip->ip_off = htons(ip->ip_off); 2874 ip->ip_sum = 0; 2875 ip->ip_sum = in_cksum(mb_copy, ip->ip_hl << 2); 2876 } else { 2877 /* Fragment the packet */ 2878 if (ip_fragment(ip, &mb_copy, mtu, 0, CSUM_DELAY_IP) != 0) { 2879 m_freem(mb_copy); 2880 return NULL; 2881 } 2882 } 2883 return mb_copy; 2884 } 2885 2886 /* 2887 * Send an upcall with the data packet to the user-level process. 2888 */ 2889 static int 2890 pim_register_send_upcall(struct ip *ip, struct vif *vifp, 2891 struct mbuf *mb_copy, struct mfc *rt) 2892 { 2893 struct mbuf *mb_first; 2894 int len = ntohs(ip->ip_len); 2895 struct igmpmsg *im; 2896 struct sockaddr_in k_igmpsrc = { sizeof k_igmpsrc, AF_INET }; 2897 2898 /* 2899 * Add a new mbuf with an upcall header 2900 */ 2901 MGETHDR(mb_first, MB_DONTWAIT, MT_HEADER); 2902 if (mb_first == NULL) { 2903 m_freem(mb_copy); 2904 return ENOBUFS; 2905 } 2906 mb_first->m_data += max_linkhdr; 2907 mb_first->m_pkthdr.len = len + sizeof(struct igmpmsg); 2908 mb_first->m_len = sizeof(struct igmpmsg); 2909 mb_first->m_next = mb_copy; 2910 2911 /* Send message to routing daemon */ 2912 im = mtod(mb_first, struct igmpmsg *); 2913 im->im_msgtype = IGMPMSG_WHOLEPKT; 2914 im->im_mbz = 0; 2915 im->im_vif = vifp - viftable; 2916 im->im_src = ip->ip_src; 2917 im->im_dst = ip->ip_dst; 2918 2919 k_igmpsrc.sin_addr = ip->ip_src; 2920 2921 mrtstat.mrts_upcalls++; 2922 2923 if (socket_send(ip_mrouter, mb_first, &k_igmpsrc) < 0) { 2924 if (mrtdebug & DEBUG_PIM) 2925 log(LOG_WARNING, 2926 "mcast: pim_register_send_upcall: ip_mrouter socket queue full"); 2927 ++mrtstat.mrts_upq_sockfull; 2928 return ENOBUFS; 2929 } 2930 2931 /* Keep statistics */ 2932 pimstat.pims_snd_registers_msgs++; 2933 pimstat.pims_snd_registers_bytes += len; 2934 2935 return 0; 2936 } 2937 2938 /* 2939 * Encapsulate the data packet in PIM Register message and send it to the RP. 2940 */ 2941 static int 2942 pim_register_send_rp(struct ip *ip, struct vif *vifp, 2943 struct mbuf *mb_copy, struct mfc *rt) 2944 { 2945 struct mbuf *mb_first; 2946 struct ip *ip_outer; 2947 struct pim_encap_pimhdr *pimhdr; 2948 int len = ntohs(ip->ip_len); 2949 vifi_t vifi = rt->mfc_parent; 2950 2951 if ((vifi >= numvifs) || (viftable[vifi].v_lcl_addr.s_addr == 0)) { 2952 m_freem(mb_copy); 2953 return EADDRNOTAVAIL; /* The iif vif is invalid */ 2954 } 2955 2956 /* 2957 * Add a new mbuf with the encapsulating header 2958 */ 2959 MGETHDR(mb_first, MB_DONTWAIT, MT_HEADER); 2960 if (mb_first == NULL) { 2961 m_freem(mb_copy); 2962 return ENOBUFS; 2963 } 2964 mb_first->m_data += max_linkhdr; 2965 mb_first->m_len = sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr); 2966 mb_first->m_next = mb_copy; 2967 2968 mb_first->m_pkthdr.len = len + mb_first->m_len; 2969 2970 /* 2971 * Fill in the encapsulating IP and PIM header 2972 */ 2973 ip_outer = mtod(mb_first, struct ip *); 2974 *ip_outer = pim_encap_iphdr; 2975 ip_outer->ip_id = ip_newid(); 2976 ip_outer->ip_len = len + sizeof(pim_encap_iphdr) + sizeof(pim_encap_pimhdr); 2977 ip_outer->ip_src = viftable[vifi].v_lcl_addr; 2978 ip_outer->ip_dst = rt->mfc_rp; 2979 /* 2980 * Copy the inner header TOS to the outer header, and take care of the 2981 * IP_DF bit. 2982 */ 2983 ip_outer->ip_tos = ip->ip_tos; 2984 if (ntohs(ip->ip_off) & IP_DF) 2985 ip_outer->ip_off |= IP_DF; 2986 pimhdr = (struct pim_encap_pimhdr *)((caddr_t)ip_outer 2987 + sizeof(pim_encap_iphdr)); 2988 *pimhdr = pim_encap_pimhdr; 2989 /* If the iif crosses a border, set the Border-bit */ 2990 if (rt->mfc_flags[vifi] & MRT_MFC_FLAGS_BORDER_VIF & mrt_api_config) 2991 pimhdr->flags |= htonl(PIM_BORDER_REGISTER); 2992 2993 mb_first->m_data += sizeof(pim_encap_iphdr); 2994 pimhdr->pim.pim_cksum = in_cksum(mb_first, sizeof(pim_encap_pimhdr)); 2995 mb_first->m_data -= sizeof(pim_encap_iphdr); 2996 2997 if (vifp->v_rate_limit == 0) 2998 tbf_send_packet(vifp, mb_first); 2999 else 3000 tbf_control(vifp, mb_first, ip, ip_outer->ip_len); 3001 3002 /* Keep statistics */ 3003 pimstat.pims_snd_registers_msgs++; 3004 pimstat.pims_snd_registers_bytes += len; 3005 3006 return 0; 3007 } 3008 3009 /* 3010 * PIM-SMv2 and PIM-DM messages processing. 3011 * Receives and verifies the PIM control messages, and passes them 3012 * up to the listening socket, using rip_input(). 3013 * The only message with special processing is the PIM_REGISTER message 3014 * (used by PIM-SM): the PIM header is stripped off, and the inner packet 3015 * is passed to if_simloop(). 3016 */ 3017 void 3018 pim_input(struct mbuf *m, ...) 3019 { 3020 int off, proto; 3021 struct ip *ip = mtod(m, struct ip *); 3022 struct pim *pim; 3023 int minlen; 3024 int datalen = ip->ip_len; 3025 int ip_tos; 3026 int iphlen; 3027 __va_list ap; 3028 3029 __va_start(ap, m); 3030 off = __va_arg(ap, int); 3031 proto = __va_arg(ap, int); 3032 __va_end(ap); 3033 3034 iphlen = off; 3035 3036 /* Keep statistics */ 3037 pimstat.pims_rcv_total_msgs++; 3038 pimstat.pims_rcv_total_bytes += datalen; 3039 3040 /* 3041 * Validate lengths 3042 */ 3043 if (datalen < PIM_MINLEN) { 3044 pimstat.pims_rcv_tooshort++; 3045 log(LOG_ERR, "pim_input: packet size too small %d from %lx\n", 3046 datalen, (u_long)ip->ip_src.s_addr); 3047 m_freem(m); 3048 return; 3049 } 3050 3051 /* 3052 * If the packet is at least as big as a REGISTER, go agead 3053 * and grab the PIM REGISTER header size, to avoid another 3054 * possible m_pullup() later. 3055 * 3056 * PIM_MINLEN == pimhdr + u_int32_t == 4 + 4 = 8 3057 * PIM_REG_MINLEN == pimhdr + reghdr + encap_iphdr == 4 + 4 + 20 = 28 3058 */ 3059 minlen = iphlen + (datalen >= PIM_REG_MINLEN ? PIM_REG_MINLEN : PIM_MINLEN); 3060 /* 3061 * Get the IP and PIM headers in contiguous memory, and 3062 * possibly the PIM REGISTER header. 3063 */ 3064 if ((m->m_flags & M_EXT || m->m_len < minlen) && 3065 (m = m_pullup(m, minlen)) == 0) { 3066 log(LOG_ERR, "pim_input: m_pullup failure\n"); 3067 return; 3068 } 3069 /* m_pullup() may have given us a new mbuf so reset ip. */ 3070 ip = mtod(m, struct ip *); 3071 ip_tos = ip->ip_tos; 3072 3073 /* adjust mbuf to point to the PIM header */ 3074 m->m_data += iphlen; 3075 m->m_len -= iphlen; 3076 pim = mtod(m, struct pim *); 3077 3078 /* 3079 * Validate checksum. If PIM REGISTER, exclude the data packet. 3080 * 3081 * XXX: some older PIMv2 implementations don't make this distinction, 3082 * so for compatibility reason perform the checksum over part of the 3083 * message, and if error, then over the whole message. 3084 */ 3085 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER && in_cksum(m, PIM_MINLEN) == 0) { 3086 /* do nothing, checksum okay */ 3087 } else if (in_cksum(m, datalen)) { 3088 pimstat.pims_rcv_badsum++; 3089 if (mrtdebug & DEBUG_PIM) 3090 log(LOG_DEBUG, "pim_input: invalid checksum"); 3091 m_freem(m); 3092 return; 3093 } 3094 3095 /* PIM version check */ 3096 if (PIM_VT_V(pim->pim_vt) < PIM_VERSION) { 3097 pimstat.pims_rcv_badversion++; 3098 log(LOG_ERR, "pim_input: incorrect version %d, expecting %d\n", 3099 PIM_VT_V(pim->pim_vt), PIM_VERSION); 3100 m_freem(m); 3101 return; 3102 } 3103 3104 /* restore mbuf back to the outer IP */ 3105 m->m_data -= iphlen; 3106 m->m_len += iphlen; 3107 3108 if (PIM_VT_T(pim->pim_vt) == PIM_REGISTER) { 3109 /* 3110 * Since this is a REGISTER, we'll make a copy of the register 3111 * headers ip + pim + u_int32 + encap_ip, to be passed up to the 3112 * routing daemon. 3113 */ 3114 struct sockaddr_in dst = { sizeof(dst), AF_INET }; 3115 struct mbuf *mcp; 3116 struct ip *encap_ip; 3117 u_int32_t *reghdr; 3118 3119 if ((reg_vif_num >= numvifs) || (reg_vif_num == VIFI_INVALID)) { 3120 if (mrtdebug & DEBUG_PIM) 3121 log(LOG_DEBUG, 3122 "pim_input: register vif not set: %d\n", reg_vif_num); 3123 m_freem(m); 3124 return; 3125 } 3126 3127 /* 3128 * Validate length 3129 */ 3130 if (datalen < PIM_REG_MINLEN) { 3131 pimstat.pims_rcv_tooshort++; 3132 pimstat.pims_rcv_badregisters++; 3133 log(LOG_ERR, 3134 "pim_input: register packet size too small %d from %lx\n", 3135 datalen, (u_long)ip->ip_src.s_addr); 3136 m_freem(m); 3137 return; 3138 } 3139 3140 reghdr = (u_int32_t *)(pim + 1); 3141 encap_ip = (struct ip *)(reghdr + 1); 3142 3143 if (mrtdebug & DEBUG_PIM) { 3144 log(LOG_DEBUG, 3145 "pim_input[register], encap_ip: %lx -> %lx, encap_ip len %d\n", 3146 (u_long)ntohl(encap_ip->ip_src.s_addr), 3147 (u_long)ntohl(encap_ip->ip_dst.s_addr), 3148 ntohs(encap_ip->ip_len)); 3149 } 3150 3151 /* verify the version number of the inner packet */ 3152 if (encap_ip->ip_v != IPVERSION) { 3153 pimstat.pims_rcv_badregisters++; 3154 if (mrtdebug & DEBUG_PIM) { 3155 log(LOG_DEBUG, "pim_input: invalid IP version (%d) " 3156 "of the inner packet\n", encap_ip->ip_v); 3157 } 3158 m_freem(m); 3159 return; 3160 } 3161 3162 /* verify the inner packet is destined to a mcast group */ 3163 if (!IN_MULTICAST(ntohl(encap_ip->ip_dst.s_addr))) { 3164 pimstat.pims_rcv_badregisters++; 3165 if (mrtdebug & DEBUG_PIM) 3166 log(LOG_DEBUG, 3167 "pim_input: inner packet of register is not " 3168 "multicast %lx\n", 3169 (u_long)ntohl(encap_ip->ip_dst.s_addr)); 3170 m_freem(m); 3171 return; 3172 } 3173 3174 /* If a NULL_REGISTER, pass it to the daemon */ 3175 if ((ntohl(*reghdr) & PIM_NULL_REGISTER)) 3176 goto pim_input_to_daemon; 3177 3178 /* 3179 * Copy the TOS from the outer IP header to the inner IP header. 3180 */ 3181 if (encap_ip->ip_tos != ip_tos) { 3182 /* Outer TOS -> inner TOS */ 3183 encap_ip->ip_tos = ip_tos; 3184 /* Recompute the inner header checksum. Sigh... */ 3185 3186 /* adjust mbuf to point to the inner IP header */ 3187 m->m_data += (iphlen + PIM_MINLEN); 3188 m->m_len -= (iphlen + PIM_MINLEN); 3189 3190 encap_ip->ip_sum = 0; 3191 encap_ip->ip_sum = in_cksum(m, encap_ip->ip_hl << 2); 3192 3193 /* restore mbuf to point back to the outer IP header */ 3194 m->m_data -= (iphlen + PIM_MINLEN); 3195 m->m_len += (iphlen + PIM_MINLEN); 3196 } 3197 3198 /* 3199 * Decapsulate the inner IP packet and loopback to forward it 3200 * as a normal multicast packet. Also, make a copy of the 3201 * outer_iphdr + pimhdr + reghdr + encap_iphdr 3202 * to pass to the daemon later, so it can take the appropriate 3203 * actions (e.g., send back PIM_REGISTER_STOP). 3204 * XXX: here m->m_data points to the outer IP header. 3205 */ 3206 mcp = m_copy(m, 0, iphlen + PIM_REG_MINLEN); 3207 if (mcp == NULL) { 3208 log(LOG_ERR, 3209 "pim_input: pim register: could not copy register head\n"); 3210 m_freem(m); 3211 return; 3212 } 3213 3214 /* Keep statistics */ 3215 /* XXX: registers_bytes include only the encap. mcast pkt */ 3216 pimstat.pims_rcv_registers_msgs++; 3217 pimstat.pims_rcv_registers_bytes += ntohs(encap_ip->ip_len); 3218 3219 /* 3220 * forward the inner ip packet; point m_data at the inner ip. 3221 */ 3222 m_adj(m, iphlen + PIM_MINLEN); 3223 3224 if (mrtdebug & DEBUG_PIM) { 3225 log(LOG_DEBUG, 3226 "pim_input: forwarding decapsulated register: " 3227 "src %lx, dst %lx, vif %d\n", 3228 (u_long)ntohl(encap_ip->ip_src.s_addr), 3229 (u_long)ntohl(encap_ip->ip_dst.s_addr), 3230 reg_vif_num); 3231 } 3232 if_simloop(viftable[reg_vif_num].v_ifp, m, dst.sin_family, 0); 3233 3234 /* prepare the register head to send to the mrouting daemon */ 3235 m = mcp; 3236 } 3237 3238 pim_input_to_daemon: 3239 /* 3240 * Pass the PIM message up to the daemon; if it is a Register message, 3241 * pass the 'head' only up to the daemon. This includes the 3242 * outer IP header, PIM header, PIM-Register header and the 3243 * inner IP header. 3244 * XXX: the outer IP header pkt size of a Register is not adjust to 3245 * reflect the fact that the inner multicast data is truncated. 3246 */ 3247 rip_input(m, iphlen, proto); 3248 3249 return; 3250 } 3251 #endif /* PIM */ 3252 3253 static int 3254 ip_mroute_modevent(module_t mod, int type, void *unused) 3255 { 3256 switch (type) { 3257 case MOD_LOAD: 3258 crit_enter(); 3259 /* XXX Protect against multiple loading */ 3260 ip_mcast_src = X_ip_mcast_src; 3261 ip_mforward = X_ip_mforward; 3262 ip_mrouter_done = X_ip_mrouter_done; 3263 ip_mrouter_get = X_ip_mrouter_get; 3264 ip_mrouter_set = X_ip_mrouter_set; 3265 ip_rsvp_force_done = X_ip_rsvp_force_done; 3266 ip_rsvp_vif = X_ip_rsvp_vif; 3267 ipip_input = X_ipip_input; 3268 legal_vif_num = X_legal_vif_num; 3269 mrt_ioctl = X_mrt_ioctl; 3270 rsvp_input_p = X_rsvp_input; 3271 crit_exit(); 3272 break; 3273 3274 case MOD_UNLOAD: 3275 if (ip_mrouter) 3276 return EINVAL; 3277 3278 crit_enter(); 3279 ip_mcast_src = NULL; 3280 ip_mforward = NULL; 3281 ip_mrouter_done = NULL; 3282 ip_mrouter_get = NULL; 3283 ip_mrouter_set = NULL; 3284 ip_rsvp_force_done = NULL; 3285 ip_rsvp_vif = NULL; 3286 ipip_input = NULL; 3287 legal_vif_num = NULL; 3288 mrt_ioctl = NULL; 3289 rsvp_input_p = NULL; 3290 crit_exit(); 3291 break; 3292 } 3293 return 0; 3294 } 3295 3296 static moduledata_t ip_mroutemod = { 3297 "ip_mroute", 3298 ip_mroute_modevent, 3299 0 3300 }; 3301 DECLARE_MODULE(ip_mroute, ip_mroutemod, SI_SUB_PSEUDO, SI_ORDER_ANY); 3302