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