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 * 3. All advertising materials mentioning features or use of this software 18 * must display the following acknowledgement: 19 * This product includes software developed by the University of 20 * California, Berkeley and its contributors. 21 * 4. Neither the name of the University nor the names of its contributors 22 * may be used to endorse or promote products derived from this software 23 * without specific prior written permission. 24 * 25 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 26 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35 * SUCH DAMAGE. 36 * 37 * @(#)igmp.c 8.1 (Berkeley) 7/19/93 38 * $FreeBSD: src/sys/netinet/igmp.c,v 1.29.2.2 2003/01/23 21:06:44 sam Exp $ 39 * $DragonFly: src/sys/netinet/igmp.c,v 1.11 2005/06/02 23:52:42 dillon Exp $ 40 */ 41 42 /* 43 * Internet Group Management Protocol (IGMP) routines. 44 * 45 * Written by Steve Deering, Stanford, May 1988. 46 * Modified by Rosen Sharma, Stanford, Aug 1994. 47 * Modified by Bill Fenner, Xerox PARC, Feb 1995. 48 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995. 49 * 50 * MULTICAST Revision: 3.5.1.4 51 */ 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/in_cksum.h> 62 #include <sys/thread2.h> 63 64 #include <machine/stdarg.h> 65 66 #include <net/if.h> 67 #include <net/route.h> 68 69 #include <netinet/in.h> 70 #include <netinet/in_var.h> 71 #include <netinet/in_systm.h> 72 #include <netinet/ip.h> 73 #include <netinet/ip_var.h> 74 #include <netinet/igmp.h> 75 #include <netinet/igmp_var.h> 76 77 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state"); 78 79 static struct router_info * 80 find_rti (struct ifnet *ifp); 81 82 static struct igmpstat igmpstat; 83 84 SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW, 85 &igmpstat, igmpstat, ""); 86 87 static int igmp_timers_are_running; 88 static u_long igmp_all_hosts_group; 89 static u_long igmp_all_rtrs_group; 90 static struct mbuf *router_alert; 91 static struct router_info *Head; 92 93 static void igmp_sendpkt (struct in_multi *, int, unsigned long); 94 95 void 96 igmp_init() 97 { 98 struct ipoption *ra; 99 100 /* 101 * To avoid byte-swapping the same value over and over again. 102 */ 103 igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP); 104 igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP); 105 106 igmp_timers_are_running = 0; 107 108 /* 109 * Construct a Router Alert option to use in outgoing packets 110 */ 111 MGET(router_alert, MB_DONTWAIT, MT_DATA); 112 ra = mtod(router_alert, struct ipoption *); 113 ra->ipopt_dst.s_addr = 0; 114 ra->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */ 115 ra->ipopt_list[1] = 0x04; /* 4 bytes long */ 116 ra->ipopt_list[2] = 0x00; 117 ra->ipopt_list[3] = 0x00; 118 router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1]; 119 120 Head = (struct router_info *) 0; 121 } 122 123 static struct router_info * 124 find_rti(ifp) 125 struct ifnet *ifp; 126 { 127 struct router_info *rti = Head; 128 129 #ifdef IGMP_DEBUG 130 printf("[igmp.c, _find_rti] --> entering \n"); 131 #endif 132 while (rti) { 133 if (rti->rti_ifp == ifp) { 134 #ifdef IGMP_DEBUG 135 printf("[igmp.c, _find_rti] --> found old entry \n"); 136 #endif 137 return rti; 138 } 139 rti = rti->rti_next; 140 } 141 MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, M_INTWAIT); 142 rti->rti_ifp = ifp; 143 rti->rti_type = IGMP_V2_ROUTER; 144 rti->rti_time = 0; 145 rti->rti_next = Head; 146 Head = rti; 147 #ifdef IGMP_DEBUG 148 printf("[igmp.c, _find_rti] --> created an entry \n"); 149 #endif 150 return rti; 151 } 152 153 void 154 igmp_input(struct mbuf *m, ...) 155 { 156 int iphlen, off, proto; 157 struct igmp *igmp; 158 struct ip *ip; 159 int igmplen; 160 struct ifnet *ifp = m->m_pkthdr.rcvif; 161 int minlen; 162 struct in_multi *inm; 163 struct in_ifaddr *ia; 164 struct in_multistep step; 165 struct router_info *rti; 166 __va_list ap; 167 168 int timer; /** timer value in the igmp query header **/ 169 170 __va_start(ap, m); 171 off = __va_arg(ap, int); 172 proto = __va_arg(ap, int); 173 __va_end(ap); 174 175 iphlen = off; 176 177 ++igmpstat.igps_rcv_total; 178 179 ip = mtod(m, struct ip *); 180 igmplen = ip->ip_len; 181 182 /* 183 * Validate lengths 184 */ 185 if (igmplen < IGMP_MINLEN) { 186 ++igmpstat.igps_rcv_tooshort; 187 m_freem(m); 188 return; 189 } 190 minlen = iphlen + IGMP_MINLEN; 191 if ((m->m_flags & M_EXT || m->m_len < minlen) && 192 (m = m_pullup(m, minlen)) == 0) { 193 ++igmpstat.igps_rcv_tooshort; 194 return; 195 } 196 197 /* 198 * Validate checksum 199 */ 200 m->m_data += iphlen; 201 m->m_len -= iphlen; 202 igmp = mtod(m, struct igmp *); 203 if (in_cksum(m, igmplen)) { 204 ++igmpstat.igps_rcv_badsum; 205 m_freem(m); 206 return; 207 } 208 m->m_data -= iphlen; 209 m->m_len += iphlen; 210 211 ip = mtod(m, struct ip *); 212 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE; 213 if (timer == 0) 214 timer = 1; 215 rti = find_rti(ifp); 216 217 /* 218 * In the IGMPv2 specification, there are 3 states and a flag. 219 * 220 * In Non-Member state, we simply don't have a membership record. 221 * In Delaying Member state, our timer is running (inm->inm_timer) 222 * In Idle Member state, our timer is not running (inm->inm_timer==0) 223 * 224 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if 225 * we have heard a report from another member, or IGMP_IREPORTEDLAST 226 * if I sent the last report. 227 */ 228 switch (igmp->igmp_type) { 229 230 case IGMP_MEMBERSHIP_QUERY: 231 ++igmpstat.igps_rcv_queries; 232 233 if (ifp->if_flags & IFF_LOOPBACK) 234 break; 235 236 if (igmp->igmp_code == 0) { 237 /* 238 * Old router. Remember that the querier on this 239 * interface is old, and set the timer to the 240 * value in RFC 1112. 241 */ 242 243 rti->rti_type = IGMP_V1_ROUTER; 244 rti->rti_time = 0; 245 246 timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ; 247 248 if (ip->ip_dst.s_addr != igmp_all_hosts_group || 249 igmp->igmp_group.s_addr != 0) { 250 ++igmpstat.igps_rcv_badqueries; 251 m_freem(m); 252 return; 253 } 254 } else { 255 /* 256 * New router. Simply do the new validity check. 257 */ 258 259 if (igmp->igmp_group.s_addr != 0 && 260 !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) { 261 ++igmpstat.igps_rcv_badqueries; 262 m_freem(m); 263 return; 264 } 265 } 266 267 /* 268 * - Start the timers in all of our membership records 269 * that the query applies to for the interface on 270 * which the query arrived excl. those that belong 271 * to the "all-hosts" group (224.0.0.1). 272 * - Restart any timer that is already running but has 273 * a value longer than the requested timeout. 274 * - Use the value specified in the query message as 275 * the maximum timeout. 276 */ 277 IN_FIRST_MULTI(step, inm); 278 while (inm != NULL) { 279 if (inm->inm_ifp == ifp && 280 inm->inm_addr.s_addr != igmp_all_hosts_group && 281 (igmp->igmp_group.s_addr == 0 || 282 igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) { 283 if (inm->inm_timer == 0 || 284 inm->inm_timer > timer) { 285 inm->inm_timer = 286 IGMP_RANDOM_DELAY(timer); 287 igmp_timers_are_running = 1; 288 } 289 } 290 IN_NEXT_MULTI(step, inm); 291 } 292 293 break; 294 295 case IGMP_V1_MEMBERSHIP_REPORT: 296 case IGMP_V2_MEMBERSHIP_REPORT: 297 /* 298 * For fast leave to work, we have to know that we are the 299 * last person to send a report for this group. Reports 300 * can potentially get looped back if we are a multicast 301 * router, so discard reports sourced by me. 302 */ 303 IFP_TO_IA(ifp, ia); 304 if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr) 305 break; 306 307 ++igmpstat.igps_rcv_reports; 308 309 if (ifp->if_flags & IFF_LOOPBACK) 310 break; 311 312 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) { 313 ++igmpstat.igps_rcv_badreports; 314 m_freem(m); 315 return; 316 } 317 318 /* 319 * KLUDGE: if the IP source address of the report has an 320 * unspecified (i.e., zero) subnet number, as is allowed for 321 * a booting host, replace it with the correct subnet number 322 * so that a process-level multicast routing demon can 323 * determine which subnet it arrived from. This is necessary 324 * to compensate for the lack of any way for a process to 325 * determine the arrival interface of an incoming packet. 326 */ 327 if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0) 328 if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet); 329 330 /* 331 * If we belong to the group being reported, stop 332 * our timer for that group. 333 */ 334 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm); 335 336 if (inm != NULL) { 337 inm->inm_timer = 0; 338 ++igmpstat.igps_rcv_ourreports; 339 340 inm->inm_state = IGMP_OTHERMEMBER; 341 } 342 343 break; 344 } 345 346 /* 347 * Pass all valid IGMP packets up to any process(es) listening 348 * on a raw IGMP socket. 349 */ 350 rip_input(m, off, proto); 351 } 352 353 void 354 igmp_joingroup(inm) 355 struct in_multi *inm; 356 { 357 crit_enter(); 358 if (inm->inm_addr.s_addr == igmp_all_hosts_group 359 || inm->inm_ifp->if_flags & IFF_LOOPBACK) { 360 inm->inm_timer = 0; 361 inm->inm_state = IGMP_OTHERMEMBER; 362 } else { 363 inm->inm_rti = find_rti(inm->inm_ifp); 364 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0); 365 inm->inm_timer = IGMP_RANDOM_DELAY( 366 IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ); 367 inm->inm_state = IGMP_IREPORTEDLAST; 368 igmp_timers_are_running = 1; 369 } 370 crit_exit(); 371 } 372 373 void 374 igmp_leavegroup(inm) 375 struct in_multi *inm; 376 { 377 if (inm->inm_state == IGMP_IREPORTEDLAST && 378 inm->inm_addr.s_addr != igmp_all_hosts_group && 379 !(inm->inm_ifp->if_flags & IFF_LOOPBACK) && 380 inm->inm_rti->rti_type != IGMP_V1_ROUTER) 381 igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group); 382 } 383 384 void 385 igmp_fasttimo() 386 { 387 struct in_multi *inm; 388 struct in_multistep step; 389 390 /* 391 * Quick check to see if any work needs to be done, in order 392 * to minimize the overhead of fasttimo processing. 393 */ 394 395 if (!igmp_timers_are_running) 396 return; 397 398 crit_enter(); 399 igmp_timers_are_running = 0; 400 IN_FIRST_MULTI(step, inm); 401 while (inm != NULL) { 402 if (inm->inm_timer == 0) { 403 /* do nothing */ 404 } else if (--inm->inm_timer == 0) { 405 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0); 406 inm->inm_state = IGMP_IREPORTEDLAST; 407 } else { 408 igmp_timers_are_running = 1; 409 } 410 IN_NEXT_MULTI(step, inm); 411 } 412 crit_exit(); 413 } 414 415 void 416 igmp_slowtimo() 417 { 418 struct router_info *rti = Head; 419 420 crit_enter(); 421 #ifdef IGMP_DEBUG 422 printf("[igmp.c,_slowtimo] -- > entering \n"); 423 #endif 424 while (rti) { 425 if (rti->rti_type == IGMP_V1_ROUTER) { 426 rti->rti_time++; 427 if (rti->rti_time >= IGMP_AGE_THRESHOLD) { 428 rti->rti_type = IGMP_V2_ROUTER; 429 } 430 } 431 rti = rti->rti_next; 432 } 433 #ifdef IGMP_DEBUG 434 printf("[igmp.c,_slowtimo] -- > exiting \n"); 435 #endif 436 crit_exit(); 437 } 438 439 static struct route igmprt; 440 441 static void 442 igmp_sendpkt(inm, type, addr) 443 struct in_multi *inm; 444 int type; 445 unsigned long addr; 446 { 447 struct mbuf *m; 448 struct igmp *igmp; 449 struct ip *ip; 450 struct ip_moptions imo; 451 452 MGETHDR(m, MB_DONTWAIT, MT_HEADER); 453 if (m == NULL) 454 return; 455 456 m->m_pkthdr.rcvif = loif; 457 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN; 458 MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip)); 459 m->m_data += sizeof(struct ip); 460 m->m_len = IGMP_MINLEN; 461 igmp = mtod(m, struct igmp *); 462 igmp->igmp_type = type; 463 igmp->igmp_code = 0; 464 igmp->igmp_group = inm->inm_addr; 465 igmp->igmp_cksum = 0; 466 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN); 467 468 m->m_data -= sizeof(struct ip); 469 m->m_len += sizeof(struct ip); 470 ip = mtod(m, struct ip *); 471 ip->ip_tos = 0; 472 ip->ip_len = sizeof(struct ip) + IGMP_MINLEN; 473 ip->ip_off = 0; 474 ip->ip_p = IPPROTO_IGMP; 475 ip->ip_src.s_addr = INADDR_ANY; 476 ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr; 477 478 imo.imo_multicast_ifp = inm->inm_ifp; 479 imo.imo_multicast_ttl = 1; 480 imo.imo_multicast_vif = -1; 481 /* 482 * Request loopback of the report if we are acting as a multicast 483 * router, so that the process-level routing demon can hear it. 484 */ 485 imo.imo_multicast_loop = (ip_mrouter != NULL); 486 487 /* 488 * XXX 489 * Do we have to worry about reentrancy here? Don't think so. 490 */ 491 ip_output(m, router_alert, &igmprt, 0, &imo, NULL); 492 493 ++igmpstat.igps_snd_reports; 494 } 495