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