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