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