1 /* $NetBSD: igmp.c,v 1.45 2007/04/25 00:11:18 dyoung 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.45 2007/04/25 00:11:18 dyoung 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 POOL_INIT(igmp_rti_pool, sizeof(struct router_info), 0, 0, 0, "igmppl", NULL, 69 IPL_SOFTNET); 70 struct igmpstat igmpstat; 71 int igmp_timers_are_running; 72 static LIST_HEAD(, router_info) rti_head = LIST_HEAD_INITIALIZER(rti_head); 73 74 void igmp_sendpkt(struct in_multi *, int); 75 static int rti_fill(struct in_multi *); 76 static struct router_info *rti_find(struct ifnet *); 77 static void rti_delete(struct ifnet *); 78 79 static int 80 rti_fill(struct in_multi *inm) 81 { 82 struct router_info *rti; 83 84 /* this function is called at splsoftnet() */ 85 LIST_FOREACH(rti, &rti_head, rti_link) { 86 if (rti->rti_ifp == inm->inm_ifp) { 87 inm->inm_rti = rti; 88 if (rti->rti_type == IGMP_v1_ROUTER) 89 return (IGMP_v1_HOST_MEMBERSHIP_REPORT); 90 else 91 return (IGMP_v2_HOST_MEMBERSHIP_REPORT); 92 } 93 } 94 95 rti = pool_get(&igmp_rti_pool, PR_NOWAIT); 96 if (rti == NULL) 97 return 0; 98 rti->rti_ifp = inm->inm_ifp; 99 rti->rti_type = IGMP_v2_ROUTER; 100 LIST_INSERT_HEAD(&rti_head, rti, rti_link); 101 inm->inm_rti = rti; 102 return (IGMP_v2_HOST_MEMBERSHIP_REPORT); 103 } 104 105 static struct router_info * 106 rti_find(struct ifnet *ifp) 107 { 108 struct router_info *rti; 109 int s = splsoftnet(); 110 111 LIST_FOREACH(rti, &rti_head, rti_link) { 112 if (rti->rti_ifp == ifp) 113 return (rti); 114 } 115 116 rti = pool_get(&igmp_rti_pool, PR_NOWAIT); 117 if (rti == NULL) { 118 splx(s); 119 return NULL; 120 } 121 rti->rti_ifp = ifp; 122 rti->rti_type = IGMP_v2_ROUTER; 123 LIST_INSERT_HEAD(&rti_head, rti, rti_link); 124 splx(s); 125 return (rti); 126 } 127 128 static void 129 rti_delete(struct ifnet *ifp) /* MUST be called at splsoftnet */ 130 { 131 struct router_info *rti; 132 133 LIST_FOREACH(rti, &rti_head, rti_link) { 134 if (rti->rti_ifp == ifp) { 135 LIST_REMOVE(rti, rti_link); 136 pool_put(&igmp_rti_pool, rti); 137 return; 138 } 139 } 140 } 141 142 void 143 igmp_input(struct mbuf *m, ...) 144 { 145 int proto; 146 int iphlen; 147 struct ifnet *ifp = m->m_pkthdr.rcvif; 148 struct ip *ip = mtod(m, struct ip *); 149 struct igmp *igmp; 150 u_int minlen; 151 struct in_multi *inm; 152 struct in_multistep step; 153 struct router_info *rti; 154 struct in_ifaddr *ia; 155 u_int timer; 156 va_list ap; 157 u_int16_t ip_len; 158 159 va_start(ap, m); 160 iphlen = va_arg(ap, int); 161 proto = va_arg(ap, int); 162 va_end(ap); 163 164 ++igmpstat.igps_rcv_total; 165 166 /* 167 * Validate lengths 168 */ 169 minlen = iphlen + IGMP_MINLEN; 170 ip_len = ntohs(ip->ip_len); 171 if (ip_len < minlen) { 172 ++igmpstat.igps_rcv_tooshort; 173 m_freem(m); 174 return; 175 } 176 if (((m->m_flags & M_EXT) && (ip->ip_src.s_addr & IN_CLASSA_NET) == 0) 177 || m->m_len < minlen) { 178 if ((m = m_pullup(m, minlen)) == 0) { 179 ++igmpstat.igps_rcv_tooshort; 180 return; 181 } 182 ip = mtod(m, struct ip *); 183 } 184 185 /* 186 * Validate checksum 187 */ 188 m->m_data += iphlen; 189 m->m_len -= iphlen; 190 igmp = mtod(m, struct igmp *); 191 /* No need to assert alignment here. */ 192 if (in_cksum(m, ip_len - iphlen)) { 193 ++igmpstat.igps_rcv_badsum; 194 m_freem(m); 195 return; 196 } 197 m->m_data -= iphlen; 198 m->m_len += iphlen; 199 200 switch (igmp->igmp_type) { 201 202 case IGMP_HOST_MEMBERSHIP_QUERY: 203 ++igmpstat.igps_rcv_queries; 204 205 if (ifp->if_flags & IFF_LOOPBACK) 206 break; 207 208 if (igmp->igmp_code == 0) { 209 rti = rti_find(ifp); 210 if (rti == NULL) 211 break; 212 rti->rti_type = IGMP_v1_ROUTER; 213 rti->rti_age = 0; 214 215 if (ip->ip_dst.s_addr != INADDR_ALLHOSTS_GROUP) { 216 ++igmpstat.igps_rcv_badqueries; 217 m_freem(m); 218 return; 219 } 220 221 /* 222 * Start the timers in all of our membership records 223 * for the interface on which the query arrived, 224 * except those that are already running and those 225 * that belong to a "local" group (224.0.0.X). 226 */ 227 IN_FIRST_MULTI(step, inm); 228 while (inm != NULL) { 229 if (inm->inm_ifp == ifp && 230 inm->inm_timer == 0 && 231 !IN_LOCAL_GROUP(inm->inm_addr.s_addr)) { 232 inm->inm_state = IGMP_DELAYING_MEMBER; 233 inm->inm_timer = IGMP_RANDOM_DELAY( 234 IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ); 235 igmp_timers_are_running = 1; 236 } 237 IN_NEXT_MULTI(step, inm); 238 } 239 } else { 240 if (!IN_MULTICAST(ip->ip_dst.s_addr)) { 241 ++igmpstat.igps_rcv_badqueries; 242 m_freem(m); 243 return; 244 } 245 246 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE; 247 if (timer == 0) 248 timer =1; 249 250 /* 251 * Start the timers in all of our membership records 252 * for the interface on which the query arrived, 253 * except those that are already running and those 254 * that belong to a "local" group (224.0.0.X). For 255 * timers already running, check if they need to be 256 * reset. 257 */ 258 IN_FIRST_MULTI(step, inm); 259 while (inm != NULL) { 260 if (inm->inm_ifp == ifp && 261 !IN_LOCAL_GROUP(inm->inm_addr.s_addr) && 262 (ip->ip_dst.s_addr == INADDR_ALLHOSTS_GROUP || 263 in_hosteq(ip->ip_dst, inm->inm_addr))) { 264 switch (inm->inm_state) { 265 case IGMP_DELAYING_MEMBER: 266 if (inm->inm_timer <= timer) 267 break; 268 /* FALLTHROUGH */ 269 case IGMP_IDLE_MEMBER: 270 case IGMP_LAZY_MEMBER: 271 case IGMP_AWAKENING_MEMBER: 272 inm->inm_state = 273 IGMP_DELAYING_MEMBER; 274 inm->inm_timer = 275 IGMP_RANDOM_DELAY(timer); 276 igmp_timers_are_running = 1; 277 break; 278 case IGMP_SLEEPING_MEMBER: 279 inm->inm_state = 280 IGMP_AWAKENING_MEMBER; 281 break; 282 } 283 } 284 IN_NEXT_MULTI(step, inm); 285 } 286 } 287 288 break; 289 290 case IGMP_v1_HOST_MEMBERSHIP_REPORT: 291 ++igmpstat.igps_rcv_reports; 292 293 if (ifp->if_flags & IFF_LOOPBACK) 294 break; 295 296 if (!IN_MULTICAST(igmp->igmp_group.s_addr) || 297 !in_hosteq(igmp->igmp_group, ip->ip_dst)) { 298 ++igmpstat.igps_rcv_badreports; 299 m_freem(m); 300 return; 301 } 302 303 /* 304 * KLUDGE: if the IP source address of the report has an 305 * unspecified (i.e., zero) subnet number, as is allowed for 306 * a booting host, replace it with the correct subnet number 307 * so that a process-level multicast routing daemon can 308 * determine which subnet it arrived from. This is necessary 309 * to compensate for the lack of any way for a process to 310 * determine the arrival interface of an incoming packet. 311 */ 312 if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) { 313 IFP_TO_IA(ifp, ia); /* XXX */ 314 if (ia) 315 ip->ip_src.s_addr = ia->ia_subnet; 316 } 317 318 /* 319 * If we belong to the group being reported, stop 320 * our timer for that group. 321 */ 322 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm); 323 if (inm != NULL) { 324 inm->inm_timer = 0; 325 ++igmpstat.igps_rcv_ourreports; 326 327 switch (inm->inm_state) { 328 case IGMP_IDLE_MEMBER: 329 case IGMP_LAZY_MEMBER: 330 case IGMP_AWAKENING_MEMBER: 331 case IGMP_SLEEPING_MEMBER: 332 inm->inm_state = IGMP_SLEEPING_MEMBER; 333 break; 334 case IGMP_DELAYING_MEMBER: 335 if (inm->inm_rti->rti_type == IGMP_v1_ROUTER) 336 inm->inm_state = IGMP_LAZY_MEMBER; 337 else 338 inm->inm_state = IGMP_SLEEPING_MEMBER; 339 break; 340 } 341 } 342 343 break; 344 345 case IGMP_v2_HOST_MEMBERSHIP_REPORT: 346 #ifdef MROUTING 347 /* 348 * Make sure we don't hear our own membership report. Fast 349 * leave requires knowing that we are the only member of a 350 * group. 351 */ 352 IFP_TO_IA(ifp, ia); /* XXX */ 353 if (ia && in_hosteq(ip->ip_src, ia->ia_addr.sin_addr)) 354 break; 355 #endif 356 357 ++igmpstat.igps_rcv_reports; 358 359 if (ifp->if_flags & IFF_LOOPBACK) 360 break; 361 362 if (!IN_MULTICAST(igmp->igmp_group.s_addr) || 363 !in_hosteq(igmp->igmp_group, ip->ip_dst)) { 364 ++igmpstat.igps_rcv_badreports; 365 m_freem(m); 366 return; 367 } 368 369 /* 370 * KLUDGE: if the IP source address of the report has an 371 * unspecified (i.e., zero) subnet number, as is allowed for 372 * a booting host, replace it with the correct subnet number 373 * so that a process-level multicast routing daemon can 374 * determine which subnet it arrived from. This is necessary 375 * to compensate for the lack of any way for a process to 376 * determine the arrival interface of an incoming packet. 377 */ 378 if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) { 379 #ifndef MROUTING 380 IFP_TO_IA(ifp, ia); /* XXX */ 381 #endif 382 if (ia) 383 ip->ip_src.s_addr = ia->ia_subnet; 384 } 385 386 /* 387 * If we belong to the group being reported, stop 388 * our timer for that group. 389 */ 390 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm); 391 if (inm != NULL) { 392 inm->inm_timer = 0; 393 ++igmpstat.igps_rcv_ourreports; 394 395 switch (inm->inm_state) { 396 case IGMP_DELAYING_MEMBER: 397 case IGMP_IDLE_MEMBER: 398 case IGMP_AWAKENING_MEMBER: 399 inm->inm_state = IGMP_LAZY_MEMBER; 400 break; 401 case IGMP_LAZY_MEMBER: 402 case IGMP_SLEEPING_MEMBER: 403 break; 404 } 405 } 406 407 break; 408 409 } 410 411 /* 412 * Pass all valid IGMP packets up to any process(es) listening 413 * on a raw IGMP socket. 414 */ 415 rip_input(m, iphlen, proto); 416 return; 417 } 418 419 int 420 igmp_joingroup(struct in_multi *inm) 421 { 422 int report_type; 423 int s = splsoftnet(); 424 425 inm->inm_state = IGMP_IDLE_MEMBER; 426 427 if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) && 428 (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0) { 429 report_type = rti_fill(inm); 430 if (report_type == 0) { 431 splx(s); 432 return ENOMEM; 433 } 434 igmp_sendpkt(inm, report_type); 435 inm->inm_state = IGMP_DELAYING_MEMBER; 436 inm->inm_timer = IGMP_RANDOM_DELAY( 437 IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ); 438 igmp_timers_are_running = 1; 439 } else 440 inm->inm_timer = 0; 441 splx(s); 442 return 0; 443 } 444 445 void 446 igmp_leavegroup(struct in_multi *inm) 447 { 448 449 switch (inm->inm_state) { 450 case IGMP_DELAYING_MEMBER: 451 case IGMP_IDLE_MEMBER: 452 if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) && 453 (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0) 454 if (inm->inm_rti->rti_type != IGMP_v1_ROUTER) 455 igmp_sendpkt(inm, IGMP_HOST_LEAVE_MESSAGE); 456 break; 457 case IGMP_LAZY_MEMBER: 458 case IGMP_AWAKENING_MEMBER: 459 case IGMP_SLEEPING_MEMBER: 460 break; 461 } 462 } 463 464 void 465 igmp_fasttimo(void) 466 { 467 struct in_multi *inm; 468 struct in_multistep step; 469 int s; 470 471 /* 472 * Quick check to see if any work needs to be done, in order 473 * to minimize the overhead of fasttimo processing. 474 */ 475 if (!igmp_timers_are_running) 476 return; 477 478 s = splsoftnet(); 479 igmp_timers_are_running = 0; 480 IN_FIRST_MULTI(step, inm); 481 while (inm != NULL) { 482 if (inm->inm_timer == 0) { 483 /* do nothing */ 484 } else if (--inm->inm_timer == 0) { 485 if (inm->inm_state == IGMP_DELAYING_MEMBER) { 486 if (inm->inm_rti->rti_type == IGMP_v1_ROUTER) 487 igmp_sendpkt(inm, 488 IGMP_v1_HOST_MEMBERSHIP_REPORT); 489 else 490 igmp_sendpkt(inm, 491 IGMP_v2_HOST_MEMBERSHIP_REPORT); 492 inm->inm_state = IGMP_IDLE_MEMBER; 493 } 494 } else { 495 igmp_timers_are_running = 1; 496 } 497 IN_NEXT_MULTI(step, inm); 498 } 499 splx(s); 500 } 501 502 void 503 igmp_slowtimo(void) 504 { 505 struct router_info *rti; 506 int s; 507 508 s = splsoftnet(); 509 LIST_FOREACH(rti, &rti_head, rti_link) { 510 if (rti->rti_type == IGMP_v1_ROUTER && 511 ++rti->rti_age >= IGMP_AGE_THRESHOLD) { 512 rti->rti_type = IGMP_v2_ROUTER; 513 } 514 } 515 splx(s); 516 } 517 518 void 519 igmp_sendpkt(struct in_multi *inm, int type) 520 { 521 struct mbuf *m; 522 struct igmp *igmp; 523 struct ip *ip; 524 struct ip_moptions imo; 525 #ifdef MROUTING 526 extern struct socket *ip_mrouter; 527 #endif /* MROUTING */ 528 529 MGETHDR(m, M_DONTWAIT, MT_HEADER); 530 if (m == NULL) 531 return; 532 /* 533 * Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN 534 * is smaller than mbuf size returned by MGETHDR. 535 */ 536 m->m_data += max_linkhdr; 537 m->m_len = sizeof(struct ip) + IGMP_MINLEN; 538 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN; 539 540 ip = mtod(m, struct ip *); 541 ip->ip_tos = 0; 542 ip->ip_len = htons(sizeof(struct ip) + IGMP_MINLEN); 543 ip->ip_off = htons(0); 544 ip->ip_p = IPPROTO_IGMP; 545 ip->ip_src = zeroin_addr; 546 ip->ip_dst = inm->inm_addr; 547 548 m->m_data += sizeof(struct ip); 549 m->m_len -= sizeof(struct ip); 550 igmp = mtod(m, struct igmp *); 551 igmp->igmp_type = type; 552 igmp->igmp_code = 0; 553 igmp->igmp_group = inm->inm_addr; 554 igmp->igmp_cksum = 0; 555 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN); 556 m->m_data -= sizeof(struct ip); 557 m->m_len += sizeof(struct ip); 558 559 imo.imo_multicast_ifp = inm->inm_ifp; 560 imo.imo_multicast_ttl = 1; 561 #ifdef RSVP_ISI 562 imo.imo_multicast_vif = -1; 563 #endif 564 /* 565 * Request loopback of the report if we are acting as a multicast 566 * router, so that the process-level routing demon can hear it. 567 */ 568 #ifdef MROUTING 569 imo.imo_multicast_loop = (ip_mrouter != NULL); 570 #else 571 imo.imo_multicast_loop = 0; 572 #endif /* MROUTING */ 573 574 ip_output(m, NULL, NULL, IP_MULTICASTOPTS, &imo, NULL); 575 576 ++igmpstat.igps_snd_reports; 577 } 578 579 void 580 igmp_purgeif(struct ifnet *ifp) /* MUST be called at splsoftnet() */ 581 { 582 rti_delete(ifp); /* manipulates pools */ 583 } 584