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