1 /*- 2 * Copyright (c) 1988 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 * @(#)igmp.c 8.1 (Berkeley) 7/19/93 34 */ 35 36 /* 37 * Internet Group Management Protocol (IGMP) routines. 38 * 39 * Written by Steve Deering, Stanford, May 1988. 40 * Modified by Rosen Sharma, Stanford, Aug 1994. 41 * Modified by Bill Fenner, Xerox PARC, Feb 1995. 42 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995. 43 * 44 * MULTICAST Revision: 3.5.1.4 45 */ 46 47 #include <sys/cdefs.h> 48 __FBSDID("$FreeBSD$"); 49 50 #include "opt_mac.h" 51 #include "opt_route.h" 52 53 #include <sys/param.h> 54 #include <sys/systm.h> 55 #include <sys/malloc.h> 56 #include <sys/mbuf.h> 57 #include <sys/socket.h> 58 #include <sys/protosw.h> 59 #include <sys/kernel.h> 60 #include <sys/sysctl.h> 61 #include <sys/vimage.h> 62 63 #include <net/if.h> 64 #include <net/route.h> 65 #include <net/vnet.h> 66 67 #include <netinet/in.h> 68 #include <netinet/in_var.h> 69 #include <netinet/in_systm.h> 70 #include <netinet/ip.h> 71 #include <netinet/ip_var.h> 72 #include <netinet/ip_options.h> 73 #include <netinet/igmp.h> 74 #include <netinet/igmp_var.h> 75 #include <netinet/vinet.h> 76 77 #include <machine/in_cksum.h> 78 79 #include <security/mac/mac_framework.h> 80 81 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state"); 82 83 static struct router_info *find_rti(struct ifnet *ifp); 84 static void igmp_sendpkt(struct in_multi *, int, unsigned long); 85 86 #ifdef VIMAGE_GLOBALS 87 static struct igmpstat igmpstat; 88 #endif 89 90 SYSCTL_V_STRUCT(V_NET, vnet_inet, _net_inet_igmp, IGMPCTL_STATS, 91 stats, CTLFLAG_RW, igmpstat, igmpstat, ""); 92 93 /* 94 * igmp_mtx protects all mutable global variables in igmp.c, as well as the 95 * data fields in struct router_info. In general, a router_info structure 96 * will be valid as long as the referencing struct in_multi is valid, so no 97 * reference counting is used. We allow unlocked reads of router_info data 98 * when accessed via an in_multi read-only. 99 */ 100 #ifdef VIMAGE_GLOBALS 101 static SLIST_HEAD(, router_info) router_info_head; 102 #endif 103 static struct mtx igmp_mtx; 104 static int igmp_timers_are_running; 105 106 /* 107 * XXXRW: can we define these such that these can be made const? In any 108 * case, these shouldn't be changed after igmp_init() and therefore don't 109 * need locking. 110 */ 111 static u_long igmp_all_hosts_group; 112 static u_long igmp_all_rtrs_group; 113 114 static struct mbuf *router_alert; 115 static struct route igmprt; 116 117 #ifdef IGMP_DEBUG 118 #define IGMP_PRINTF(x) printf(x) 119 #else 120 #define IGMP_PRINTF(x) 121 #endif 122 123 void 124 igmp_init(void) 125 { 126 INIT_VNET_INET(curvnet); 127 struct ipoption *ra; 128 129 /* 130 * To avoid byte-swapping the same value over and over again. 131 */ 132 igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP); 133 igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP); 134 135 igmp_timers_are_running = 0; 136 137 /* 138 * Construct a Router Alert option to use in outgoing packets. 139 */ 140 MGET(router_alert, M_DONTWAIT, MT_DATA); 141 ra = mtod(router_alert, struct ipoption *); 142 ra->ipopt_dst.s_addr = 0; 143 ra->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */ 144 ra->ipopt_list[1] = 0x04; /* 4 bytes long */ 145 ra->ipopt_list[2] = 0x00; 146 ra->ipopt_list[3] = 0x00; 147 router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1]; 148 149 mtx_init(&igmp_mtx, "igmp_mtx", NULL, MTX_DEF); 150 SLIST_INIT(&V_router_info_head); 151 } 152 153 static struct router_info * 154 find_rti(struct ifnet *ifp) 155 { 156 INIT_VNET_INET(ifp->if_vnet); 157 struct router_info *rti; 158 159 mtx_assert(&igmp_mtx, MA_OWNED); 160 IGMP_PRINTF("[igmp.c, _find_rti] --> entering \n"); 161 SLIST_FOREACH(rti, &V_router_info_head, rti_list) { 162 if (rti->rti_ifp == ifp) { 163 IGMP_PRINTF( 164 "[igmp.c, _find_rti] --> found old entry \n"); 165 return (rti); 166 } 167 } 168 rti = malloc(sizeof *rti, M_IGMP, M_NOWAIT); 169 if (rti == NULL) { 170 IGMP_PRINTF("[igmp.c, _find_rti] --> no memory for entry\n"); 171 return (NULL); 172 } 173 rti->rti_ifp = ifp; 174 rti->rti_type = IGMP_V2_ROUTER; 175 rti->rti_time = 0; 176 SLIST_INSERT_HEAD(&V_router_info_head, rti, rti_list); 177 IGMP_PRINTF("[igmp.c, _find_rti] --> created an entry \n"); 178 return (rti); 179 } 180 181 void 182 igmp_input(register struct mbuf *m, int off) 183 { 184 register int iphlen = off; 185 register struct igmp *igmp; 186 register struct ip *ip; 187 register int igmplen; 188 register struct ifnet *ifp = m->m_pkthdr.rcvif; 189 register int minlen; 190 register struct in_multi *inm; 191 register struct in_ifaddr *ia; 192 struct in_multistep step; 193 struct router_info *rti; 194 int timer; /** timer value in the igmp query header **/ 195 INIT_VNET_INET(ifp->if_vnet); 196 197 ++V_igmpstat.igps_rcv_total; 198 199 ip = mtod(m, struct ip *); 200 igmplen = ip->ip_len; 201 202 /* 203 * Validate lengths. 204 */ 205 if (igmplen < IGMP_MINLEN) { 206 ++V_igmpstat.igps_rcv_tooshort; 207 m_freem(m); 208 return; 209 } 210 minlen = iphlen + IGMP_MINLEN; 211 if ((m->m_flags & M_EXT || m->m_len < minlen) && 212 (m = m_pullup(m, minlen)) == 0) { 213 ++V_igmpstat.igps_rcv_tooshort; 214 return; 215 } 216 217 /* 218 * Validate checksum. 219 */ 220 m->m_data += iphlen; 221 m->m_len -= iphlen; 222 igmp = mtod(m, struct igmp *); 223 if (in_cksum(m, igmplen)) { 224 ++V_igmpstat.igps_rcv_badsum; 225 m_freem(m); 226 return; 227 } 228 m->m_data -= iphlen; 229 m->m_len += iphlen; 230 231 ip = mtod(m, struct ip *); 232 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE; 233 if (timer == 0) 234 timer = 1; 235 236 /* 237 * In the IGMPv2 specification, there are 3 states and a flag. 238 * 239 * In Non-Member state, we simply don't have a membership record. 240 * In Delaying Member state, our timer is running (inm->inm_timer). 241 * In Idle Member state, our timer is not running (inm->inm_timer==0). 242 * 243 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if we 244 * have heard a report from another member, or IGMP_IREPORTEDLAST if 245 * I sent the last report. 246 */ 247 switch (igmp->igmp_type) { 248 case IGMP_MEMBERSHIP_QUERY: 249 ++V_igmpstat.igps_rcv_queries; 250 251 if (ifp->if_flags & IFF_LOOPBACK) 252 break; 253 254 if (igmp->igmp_code == 0) { 255 /* 256 * Old router. Remember that the querier on this 257 * interface is old, and set the timer to the value 258 * in RFC 1112. 259 */ 260 261 mtx_lock(&igmp_mtx); 262 rti = find_rti(ifp); 263 if (rti == NULL) { 264 mtx_unlock(&igmp_mtx); 265 m_freem(m); 266 return; 267 } 268 rti->rti_type = IGMP_V1_ROUTER; 269 rti->rti_time = 0; 270 mtx_unlock(&igmp_mtx); 271 272 timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ; 273 274 if (ip->ip_dst.s_addr != igmp_all_hosts_group || 275 igmp->igmp_group.s_addr != 0) { 276 ++V_igmpstat.igps_rcv_badqueries; 277 m_freem(m); 278 return; 279 } 280 } else { 281 /* 282 * New router. Simply do the new validity check. 283 */ 284 285 if (igmp->igmp_group.s_addr != 0 && 286 !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) { 287 ++V_igmpstat.igps_rcv_badqueries; 288 m_freem(m); 289 return; 290 } 291 } 292 293 /* 294 * - Start the timers in all of our membership records that 295 * the query applies to for the interface on which the 296 * query arrived excl. those that belong to the "all-hosts" 297 * group (224.0.0.1). 298 * - Restart any timer that is already running but has a 299 * value longer than the requested timeout. 300 * - Use the value specified in the query message as the 301 * maximum timeout. 302 */ 303 IN_MULTI_LOCK(); 304 IN_FIRST_MULTI(step, inm); 305 while (inm != NULL) { 306 if (inm->inm_ifp == ifp && 307 inm->inm_addr.s_addr != igmp_all_hosts_group && 308 (igmp->igmp_group.s_addr == 0 || 309 igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) { 310 if (inm->inm_timer == 0 || 311 inm->inm_timer > timer) { 312 inm->inm_timer = 313 IGMP_RANDOM_DELAY(timer); 314 igmp_timers_are_running = 1; 315 } 316 } 317 IN_NEXT_MULTI(step, inm); 318 } 319 IN_MULTI_UNLOCK(); 320 break; 321 322 case IGMP_V1_MEMBERSHIP_REPORT: 323 case IGMP_V2_MEMBERSHIP_REPORT: 324 /* 325 * For fast leave to work, we have to know that we are the 326 * last person to send a report for this group. Reports can 327 * potentially get looped back if we are a multicast router, 328 * so discard reports sourced by me. 329 */ 330 IFP_TO_IA(ifp, ia); 331 if (ia != NULL && 332 ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr) 333 break; 334 335 ++V_igmpstat.igps_rcv_reports; 336 337 if (ifp->if_flags & IFF_LOOPBACK) 338 break; 339 340 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) { 341 ++V_igmpstat.igps_rcv_badreports; 342 m_freem(m); 343 return; 344 } 345 346 /* 347 * KLUDGE: if the IP source address of the report has an 348 * unspecified (i.e., zero) subnet number, as is allowed for 349 * a booting host, replace it with the correct subnet number 350 * so that a process-level multicast routing daemon can 351 * determine which subnet it arrived from. This is necessary 352 * to compensate for the lack of any way for a process to 353 * determine the arrival interface of an incoming packet. 354 */ 355 if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0) { 356 if (ia != NULL) 357 ip->ip_src.s_addr = htonl(ia->ia_subnet); 358 } 359 360 /* 361 * If we belong to the group being reported, stop our timer 362 * for that group. 363 */ 364 IN_MULTI_LOCK(); 365 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm); 366 if (inm != NULL) { 367 inm->inm_timer = 0; 368 ++V_igmpstat.igps_rcv_ourreports; 369 inm->inm_state = IGMP_OTHERMEMBER; 370 } 371 IN_MULTI_UNLOCK(); 372 break; 373 } 374 375 /* 376 * Pass all valid IGMP packets up to any process(es) listening on a 377 * raw IGMP socket. 378 */ 379 rip_input(m, off); 380 } 381 382 void 383 igmp_joingroup(struct in_multi *inm) 384 { 385 386 IN_MULTI_LOCK_ASSERT(); 387 388 if (inm->inm_addr.s_addr == igmp_all_hosts_group 389 || inm->inm_ifp->if_flags & IFF_LOOPBACK) { 390 inm->inm_timer = 0; 391 inm->inm_state = IGMP_OTHERMEMBER; 392 } else { 393 mtx_lock(&igmp_mtx); 394 inm->inm_rti = find_rti(inm->inm_ifp); 395 mtx_unlock(&igmp_mtx); 396 if (inm->inm_rti != NULL) { 397 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0); 398 inm->inm_timer = IGMP_RANDOM_DELAY( 399 IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ); 400 inm->inm_state = IGMP_IREPORTEDLAST; 401 igmp_timers_are_running = 1; 402 } 403 /* XXX handling of failure case? */ 404 } 405 } 406 407 void 408 igmp_leavegroup(struct in_multi *inm) 409 { 410 411 IN_MULTI_LOCK_ASSERT(); 412 413 if (inm->inm_state == IGMP_IREPORTEDLAST && 414 inm->inm_addr.s_addr != igmp_all_hosts_group && 415 !(inm->inm_ifp->if_flags & IFF_LOOPBACK) && 416 inm->inm_rti->rti_type != IGMP_V1_ROUTER) 417 igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group); 418 } 419 420 void 421 igmp_fasttimo(void) 422 { 423 VNET_ITERATOR_DECL(vnet_iter); 424 register struct in_multi *inm; 425 struct in_multistep step; 426 427 /* 428 * Quick check to see if any work needs to be done, in order to 429 * minimize the overhead of fasttimo processing. 430 */ 431 432 if (!igmp_timers_are_running) 433 return; 434 435 IN_MULTI_LOCK(); 436 igmp_timers_are_running = 0; 437 VNET_LIST_RLOCK(); 438 VNET_FOREACH(vnet_iter) { 439 CURVNET_SET(vnet_iter); 440 INIT_VNET_INET(vnet_iter); 441 IN_FIRST_MULTI(step, inm); 442 while (inm != NULL) { 443 if (inm->inm_timer == 0) { 444 /* do nothing */ 445 } else if (--inm->inm_timer == 0) { 446 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0); 447 inm->inm_state = IGMP_IREPORTEDLAST; 448 } else { 449 igmp_timers_are_running = 1; 450 } 451 IN_NEXT_MULTI(step, inm); 452 } 453 CURVNET_RESTORE(); 454 } 455 VNET_LIST_RUNLOCK(); 456 IN_MULTI_UNLOCK(); 457 } 458 459 void 460 igmp_slowtimo(void) 461 { 462 VNET_ITERATOR_DECL(vnet_iter); 463 struct router_info *rti; 464 465 IGMP_PRINTF("[igmp.c,_slowtimo] -- > entering \n"); 466 mtx_lock(&igmp_mtx); 467 VNET_LIST_RLOCK(); 468 VNET_FOREACH(vnet_iter) { 469 CURVNET_SET(vnet_iter); 470 INIT_VNET_INET(vnet_iter); 471 SLIST_FOREACH(rti, &V_router_info_head, rti_list) { 472 if (rti->rti_type == IGMP_V1_ROUTER) { 473 rti->rti_time++; 474 if (rti->rti_time >= IGMP_AGE_THRESHOLD) 475 rti->rti_type = IGMP_V2_ROUTER; 476 } 477 } 478 CURVNET_RESTORE(); 479 } 480 VNET_LIST_RUNLOCK(); 481 mtx_unlock(&igmp_mtx); 482 IGMP_PRINTF("[igmp.c,_slowtimo] -- > exiting \n"); 483 } 484 485 static void 486 igmp_sendpkt(struct in_multi *inm, int type, unsigned long addr) 487 { 488 INIT_VNET_NET(curvnet); 489 INIT_VNET_INET(curvnet); 490 struct mbuf *m; 491 struct igmp *igmp; 492 struct ip *ip; 493 struct ip_moptions imo; 494 495 IN_MULTI_LOCK_ASSERT(); 496 497 MGETHDR(m, M_DONTWAIT, MT_DATA); 498 if (m == NULL) 499 return; 500 501 m->m_pkthdr.rcvif = V_loif; 502 #ifdef MAC 503 mac_netinet_igmp_send(inm->inm_ifp, m); 504 #endif 505 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN; 506 MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip)); 507 m->m_data += sizeof(struct ip); 508 m->m_len = IGMP_MINLEN; 509 igmp = mtod(m, struct igmp *); 510 igmp->igmp_type = type; 511 igmp->igmp_code = 0; 512 igmp->igmp_group = inm->inm_addr; 513 igmp->igmp_cksum = 0; 514 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN); 515 516 m->m_data -= sizeof(struct ip); 517 m->m_len += sizeof(struct ip); 518 ip = mtod(m, struct ip *); 519 ip->ip_tos = 0; 520 ip->ip_len = sizeof(struct ip) + IGMP_MINLEN; 521 ip->ip_off = 0; 522 ip->ip_p = IPPROTO_IGMP; 523 ip->ip_src.s_addr = INADDR_ANY; 524 ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr; 525 526 imo.imo_multicast_ifp = inm->inm_ifp; 527 imo.imo_multicast_ttl = 1; 528 imo.imo_multicast_vif = -1; 529 /* 530 * Request loopback of the report if we are acting as a multicast 531 * router, so that the process-level routing daemon can hear it. 532 */ 533 imo.imo_multicast_loop = (V_ip_mrouter != NULL); 534 535 /* 536 * XXX: Do we have to worry about reentrancy here? Don't think so. 537 */ 538 ip_output(m, router_alert, &igmprt, 0, &imo, NULL); 539 540 ++V_igmpstat.igps_snd_reports; 541 } 542