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