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