1 /* $NetBSD: igmp.c,v 1.32 2002/11/07 07:15:19 thorpej Exp $ */ 2 3 /* 4 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project. 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. Neither the name of the project nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 /* 33 * Internet Group Management Protocol (IGMP) routines. 34 * 35 * Written by Steve Deering, Stanford, May 1988. 36 * Modified by Rosen Sharma, Stanford, Aug 1994. 37 * Modified by Bill Fenner, Xerox PARC, Feb 1995. 38 * 39 * MULTICAST Revision: 1.3 40 */ 41 42 #include <sys/cdefs.h> 43 __KERNEL_RCSID(0, "$NetBSD: igmp.c,v 1.32 2002/11/07 07:15:19 thorpej Exp $"); 44 45 #include "opt_mrouting.h" 46 47 #include <sys/param.h> 48 #include <sys/mbuf.h> 49 #include <sys/socket.h> 50 #include <sys/protosw.h> 51 #include <sys/systm.h> 52 53 #include <net/if.h> 54 #include <net/route.h> 55 56 #include <netinet/in.h> 57 #include <netinet/in_var.h> 58 #include <netinet/in_systm.h> 59 #include <netinet/ip.h> 60 #include <netinet/ip_var.h> 61 #include <netinet/igmp.h> 62 #include <netinet/igmp_var.h> 63 64 #include <machine/stdarg.h> 65 66 #define IP_MULTICASTOPTS 0 67 68 struct igmpstat igmpstat; 69 int igmp_timers_are_running; 70 static struct router_info *rti_head; 71 72 void igmp_sendpkt __P((struct in_multi *, int)); 73 static int rti_fill __P((struct in_multi *)); 74 static struct router_info * rti_find __P((struct ifnet *)); 75 76 void 77 igmp_init() 78 { 79 80 igmp_timers_are_running = 0; 81 rti_head = 0; 82 } 83 84 static int 85 rti_fill(inm) 86 struct in_multi *inm; 87 { 88 struct router_info *rti; 89 90 for (rti = rti_head; rti != 0; rti = rti->rti_next) { 91 if (rti->rti_ifp == inm->inm_ifp) { 92 inm->inm_rti = rti; 93 if (rti->rti_type == IGMP_v1_ROUTER) 94 return (IGMP_v1_HOST_MEMBERSHIP_REPORT); 95 else 96 return (IGMP_v2_HOST_MEMBERSHIP_REPORT); 97 } 98 } 99 100 rti = (struct router_info *)malloc(sizeof(struct router_info), 101 M_MRTABLE, M_NOWAIT); 102 rti->rti_ifp = inm->inm_ifp; 103 rti->rti_type = IGMP_v2_ROUTER; 104 rti->rti_next = rti_head; 105 rti_head = rti; 106 inm->inm_rti = rti; 107 return (IGMP_v2_HOST_MEMBERSHIP_REPORT); 108 } 109 110 static struct router_info * 111 rti_find(ifp) 112 struct ifnet *ifp; 113 { 114 struct router_info *rti; 115 116 for (rti = rti_head; rti != 0; rti = rti->rti_next) { 117 if (rti->rti_ifp == ifp) 118 return (rti); 119 } 120 121 rti = (struct router_info *)malloc(sizeof(struct router_info), 122 M_MRTABLE, M_NOWAIT); 123 rti->rti_ifp = ifp; 124 rti->rti_type = IGMP_v2_ROUTER; 125 rti->rti_next = rti_head; 126 rti_head = rti; 127 return (rti); 128 } 129 130 void 131 #if __STDC__ 132 igmp_input(struct mbuf *m, ...) 133 #else 134 igmp_input(m, va_alist) 135 struct mbuf *m; 136 va_dcl 137 #endif 138 { 139 int proto; 140 int iphlen; 141 struct ifnet *ifp = m->m_pkthdr.rcvif; 142 struct ip *ip = mtod(m, struct ip *); 143 struct igmp *igmp; 144 u_int minlen; 145 struct in_multi *inm; 146 struct in_multistep step; 147 struct router_info *rti; 148 struct in_ifaddr *ia; 149 u_int timer; 150 va_list ap; 151 u_int16_t ip_len; 152 153 va_start(ap, m); 154 iphlen = va_arg(ap, int); 155 proto = va_arg(ap, int); 156 va_end(ap); 157 158 ++igmpstat.igps_rcv_total; 159 160 /* 161 * Validate lengths 162 */ 163 minlen = iphlen + IGMP_MINLEN; 164 ip_len = ntohs(ip->ip_len); 165 if (ip_len < minlen) { 166 ++igmpstat.igps_rcv_tooshort; 167 m_freem(m); 168 return; 169 } 170 if (((m->m_flags & M_EXT) && (ip->ip_src.s_addr & IN_CLASSA_NET) == 0) 171 || m->m_len < minlen) { 172 if ((m = m_pullup(m, minlen)) == 0) { 173 ++igmpstat.igps_rcv_tooshort; 174 return; 175 } 176 ip = mtod(m, struct ip *); 177 } 178 179 /* 180 * Validate checksum 181 */ 182 m->m_data += iphlen; 183 m->m_len -= iphlen; 184 igmp = mtod(m, struct igmp *); 185 /* No need to assert alignment here. */ 186 if (in_cksum(m, ip_len - iphlen)) { 187 ++igmpstat.igps_rcv_badsum; 188 m_freem(m); 189 return; 190 } 191 m->m_data -= iphlen; 192 m->m_len += iphlen; 193 194 switch (igmp->igmp_type) { 195 196 case IGMP_HOST_MEMBERSHIP_QUERY: 197 ++igmpstat.igps_rcv_queries; 198 199 if (ifp->if_flags & IFF_LOOPBACK) 200 break; 201 202 if (igmp->igmp_code == 0) { 203 rti = rti_find(ifp); 204 rti->rti_type = IGMP_v1_ROUTER; 205 rti->rti_age = 0; 206 207 if (ip->ip_dst.s_addr != INADDR_ALLHOSTS_GROUP) { 208 ++igmpstat.igps_rcv_badqueries; 209 m_freem(m); 210 return; 211 } 212 213 /* 214 * Start the timers in all of our membership records 215 * for the interface on which the query arrived, 216 * except those that are already running and those 217 * that belong to a "local" group (224.0.0.X). 218 */ 219 IN_FIRST_MULTI(step, inm); 220 while (inm != NULL) { 221 if (inm->inm_ifp == ifp && 222 inm->inm_timer == 0 && 223 !IN_LOCAL_GROUP(inm->inm_addr.s_addr)) { 224 inm->inm_state = IGMP_DELAYING_MEMBER; 225 inm->inm_timer = IGMP_RANDOM_DELAY( 226 IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ); 227 igmp_timers_are_running = 1; 228 } 229 IN_NEXT_MULTI(step, inm); 230 } 231 } else { 232 if (!IN_MULTICAST(ip->ip_dst.s_addr)) { 233 ++igmpstat.igps_rcv_badqueries; 234 m_freem(m); 235 return; 236 } 237 238 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE; 239 if (timer == 0) 240 timer =1; 241 242 /* 243 * Start the timers in all of our membership records 244 * for the interface on which the query arrived, 245 * except those that are already running and those 246 * that belong to a "local" group (224.0.0.X). For 247 * timers already running, check if they need to be 248 * reset. 249 */ 250 IN_FIRST_MULTI(step, inm); 251 while (inm != NULL) { 252 if (inm->inm_ifp == ifp && 253 !IN_LOCAL_GROUP(inm->inm_addr.s_addr) && 254 (ip->ip_dst.s_addr == INADDR_ALLHOSTS_GROUP || 255 in_hosteq(ip->ip_dst, inm->inm_addr))) { 256 switch (inm->inm_state) { 257 case IGMP_DELAYING_MEMBER: 258 if (inm->inm_timer <= timer) 259 break; 260 /* FALLTHROUGH */ 261 case IGMP_IDLE_MEMBER: 262 case IGMP_LAZY_MEMBER: 263 case IGMP_AWAKENING_MEMBER: 264 inm->inm_state = 265 IGMP_DELAYING_MEMBER; 266 inm->inm_timer = 267 IGMP_RANDOM_DELAY(timer); 268 igmp_timers_are_running = 1; 269 break; 270 case IGMP_SLEEPING_MEMBER: 271 inm->inm_state = 272 IGMP_AWAKENING_MEMBER; 273 break; 274 } 275 } 276 IN_NEXT_MULTI(step, inm); 277 } 278 } 279 280 break; 281 282 case IGMP_v1_HOST_MEMBERSHIP_REPORT: 283 ++igmpstat.igps_rcv_reports; 284 285 if (ifp->if_flags & IFF_LOOPBACK) 286 break; 287 288 if (!IN_MULTICAST(igmp->igmp_group.s_addr) || 289 !in_hosteq(igmp->igmp_group, ip->ip_dst)) { 290 ++igmpstat.igps_rcv_badreports; 291 m_freem(m); 292 return; 293 } 294 295 /* 296 * KLUDGE: if the IP source address of the report has an 297 * unspecified (i.e., zero) subnet number, as is allowed for 298 * a booting host, replace it with the correct subnet number 299 * so that a process-level multicast routing daemon can 300 * determine which subnet it arrived from. This is necessary 301 * to compensate for the lack of any way for a process to 302 * determine the arrival interface of an incoming packet. 303 */ 304 if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) { 305 IFP_TO_IA(ifp, ia); /* XXX */ 306 if (ia) 307 ip->ip_src.s_addr = ia->ia_subnet; 308 } 309 310 /* 311 * If we belong to the group being reported, stop 312 * our timer for that group. 313 */ 314 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm); 315 if (inm != NULL) { 316 inm->inm_timer = 0; 317 ++igmpstat.igps_rcv_ourreports; 318 319 switch (inm->inm_state) { 320 case IGMP_IDLE_MEMBER: 321 case IGMP_LAZY_MEMBER: 322 case IGMP_AWAKENING_MEMBER: 323 case IGMP_SLEEPING_MEMBER: 324 inm->inm_state = IGMP_SLEEPING_MEMBER; 325 break; 326 case IGMP_DELAYING_MEMBER: 327 if (inm->inm_rti->rti_type == IGMP_v1_ROUTER) 328 inm->inm_state = IGMP_LAZY_MEMBER; 329 else 330 inm->inm_state = IGMP_SLEEPING_MEMBER; 331 break; 332 } 333 } 334 335 break; 336 337 case IGMP_v2_HOST_MEMBERSHIP_REPORT: 338 #ifdef MROUTING 339 /* 340 * Make sure we don't hear our own membership report. Fast 341 * leave requires knowing that we are the only member of a 342 * group. 343 */ 344 IFP_TO_IA(ifp, ia); /* XXX */ 345 if (ia && in_hosteq(ip->ip_src, ia->ia_addr.sin_addr)) 346 break; 347 #endif 348 349 ++igmpstat.igps_rcv_reports; 350 351 if (ifp->if_flags & IFF_LOOPBACK) 352 break; 353 354 if (!IN_MULTICAST(igmp->igmp_group.s_addr) || 355 !in_hosteq(igmp->igmp_group, ip->ip_dst)) { 356 ++igmpstat.igps_rcv_badreports; 357 m_freem(m); 358 return; 359 } 360 361 /* 362 * KLUDGE: if the IP source address of the report has an 363 * unspecified (i.e., zero) subnet number, as is allowed for 364 * a booting host, replace it with the correct subnet number 365 * so that a process-level multicast routing daemon can 366 * determine which subnet it arrived from. This is necessary 367 * to compensate for the lack of any way for a process to 368 * determine the arrival interface of an incoming packet. 369 */ 370 if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) { 371 #ifndef MROUTING 372 IFP_TO_IA(ifp, ia); /* XXX */ 373 #endif 374 if (ia) 375 ip->ip_src.s_addr = ia->ia_subnet; 376 } 377 378 /* 379 * If we belong to the group being reported, stop 380 * our timer for that group. 381 */ 382 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm); 383 if (inm != NULL) { 384 inm->inm_timer = 0; 385 ++igmpstat.igps_rcv_ourreports; 386 387 switch (inm->inm_state) { 388 case IGMP_DELAYING_MEMBER: 389 case IGMP_IDLE_MEMBER: 390 case IGMP_AWAKENING_MEMBER: 391 inm->inm_state = IGMP_LAZY_MEMBER; 392 break; 393 case IGMP_LAZY_MEMBER: 394 case IGMP_SLEEPING_MEMBER: 395 break; 396 } 397 } 398 399 break; 400 401 } 402 403 /* 404 * Pass all valid IGMP packets up to any process(es) listening 405 * on a raw IGMP socket. 406 */ 407 rip_input(m, iphlen, proto); 408 return; 409 } 410 411 void 412 igmp_joingroup(inm) 413 struct in_multi *inm; 414 { 415 int s = splsoftnet(); 416 417 inm->inm_state = IGMP_IDLE_MEMBER; 418 419 if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) && 420 (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0) { 421 igmp_sendpkt(inm, rti_fill(inm)); 422 inm->inm_state = IGMP_DELAYING_MEMBER; 423 inm->inm_timer = IGMP_RANDOM_DELAY( 424 IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ); 425 igmp_timers_are_running = 1; 426 } else 427 inm->inm_timer = 0; 428 splx(s); 429 } 430 431 void 432 igmp_leavegroup(inm) 433 struct in_multi *inm; 434 { 435 436 switch (inm->inm_state) { 437 case IGMP_DELAYING_MEMBER: 438 case IGMP_IDLE_MEMBER: 439 if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) && 440 (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0) 441 if (inm->inm_rti->rti_type != IGMP_v1_ROUTER) 442 igmp_sendpkt(inm, IGMP_HOST_LEAVE_MESSAGE); 443 break; 444 case IGMP_LAZY_MEMBER: 445 case IGMP_AWAKENING_MEMBER: 446 case IGMP_SLEEPING_MEMBER: 447 break; 448 } 449 } 450 451 void 452 igmp_fasttimo() 453 { 454 struct in_multi *inm; 455 struct in_multistep step; 456 int s; 457 458 /* 459 * Quick check to see if any work needs to be done, in order 460 * to minimize the overhead of fasttimo processing. 461 */ 462 if (!igmp_timers_are_running) 463 return; 464 465 s = splsoftnet(); 466 igmp_timers_are_running = 0; 467 IN_FIRST_MULTI(step, inm); 468 while (inm != NULL) { 469 if (inm->inm_timer == 0) { 470 /* do nothing */ 471 } else if (--inm->inm_timer == 0) { 472 if (inm->inm_state == IGMP_DELAYING_MEMBER) { 473 if (inm->inm_rti->rti_type == IGMP_v1_ROUTER) 474 igmp_sendpkt(inm, 475 IGMP_v1_HOST_MEMBERSHIP_REPORT); 476 else 477 igmp_sendpkt(inm, 478 IGMP_v2_HOST_MEMBERSHIP_REPORT); 479 inm->inm_state = IGMP_IDLE_MEMBER; 480 } 481 } else { 482 igmp_timers_are_running = 1; 483 } 484 IN_NEXT_MULTI(step, inm); 485 } 486 splx(s); 487 } 488 489 void 490 igmp_slowtimo() 491 { 492 struct router_info *rti; 493 int s; 494 495 s = splsoftnet(); 496 for (rti = rti_head; rti != 0; rti = rti->rti_next) { 497 if (rti->rti_type == IGMP_v1_ROUTER && 498 ++rti->rti_age >= IGMP_AGE_THRESHOLD) { 499 rti->rti_type = IGMP_v2_ROUTER; 500 } 501 } 502 splx(s); 503 } 504 505 void 506 igmp_sendpkt(inm, type) 507 struct in_multi *inm; 508 int type; 509 { 510 struct mbuf *m; 511 struct igmp *igmp; 512 struct ip *ip; 513 struct ip_moptions imo; 514 #ifdef MROUTING 515 extern struct socket *ip_mrouter; 516 #endif /* MROUTING */ 517 518 MGETHDR(m, M_DONTWAIT, MT_HEADER); 519 if (m == NULL) 520 return; 521 /* 522 * Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN 523 * is smaller than mbuf size returned by MGETHDR. 524 */ 525 m->m_data += max_linkhdr; 526 m->m_len = sizeof(struct ip) + IGMP_MINLEN; 527 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN; 528 529 ip = mtod(m, struct ip *); 530 ip->ip_tos = 0; 531 ip->ip_len = htons(sizeof(struct ip) + IGMP_MINLEN); 532 ip->ip_off = htons(0); 533 ip->ip_p = IPPROTO_IGMP; 534 ip->ip_src = zeroin_addr; 535 ip->ip_dst = inm->inm_addr; 536 537 m->m_data += sizeof(struct ip); 538 m->m_len -= sizeof(struct ip); 539 igmp = mtod(m, struct igmp *); 540 igmp->igmp_type = type; 541 igmp->igmp_code = 0; 542 igmp->igmp_group = inm->inm_addr; 543 igmp->igmp_cksum = 0; 544 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN); 545 m->m_data -= sizeof(struct ip); 546 m->m_len += sizeof(struct ip); 547 548 imo.imo_multicast_ifp = inm->inm_ifp; 549 imo.imo_multicast_ttl = 1; 550 #ifdef RSVP_ISI 551 imo.imo_multicast_vif = -1; 552 #endif 553 /* 554 * Request loopback of the report if we are acting as a multicast 555 * router, so that the process-level routing demon can hear it. 556 */ 557 #ifdef MROUTING 558 imo.imo_multicast_loop = (ip_mrouter != NULL); 559 #else 560 imo.imo_multicast_loop = 0; 561 #endif /* MROUTING */ 562 563 ip_output(m, (struct mbuf *)0, (struct route *)0, IP_MULTICASTOPTS, 564 &imo); 565 566 ++igmpstat.igps_snd_reports; 567 } 568