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