]> git.saurik.com Git - apple/xnu.git/blame - bsd/netinet/igmp.c
xnu-1456.1.26.tar.gz
[apple/xnu.git] / bsd / netinet / igmp.c
CommitLineData
1c79356b 1/*
b0d623f7 2 * Copyright (c) 2000-2008 Apple Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b
A
27 */
28/*
29 * Copyright (c) 1988 Stephen Deering.
30 * Copyright (c) 1992, 1993
31 * The Regents of the University of California. All rights reserved.
32 *
33 * This code is derived from software contributed to Berkeley by
34 * Stephen Deering of Stanford University.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. All advertising materials mentioning features or use of this software
45 * must display the following acknowledgement:
46 * This product includes software developed by the University of
47 * California, Berkeley and its contributors.
48 * 4. Neither the name of the University nor the names of its contributors
49 * may be used to endorse or promote products derived from this software
50 * without specific prior written permission.
51 *
52 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
55 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
62 * SUCH DAMAGE.
63 *
64 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
65 */
2d21ac55
A
66/*
67 * NOTICE: This file was modified by SPARTA, Inc. in 2005 to introduce
68 * support for mandatory and extensible security protections. This notice
69 * is included in support of clause 2.2 (b) of the Apple Public License,
70 * Version 2.0.
71 */
1c79356b
A
72
73/*
74 * Internet Group Management Protocol (IGMP) routines.
75 *
76 * Written by Steve Deering, Stanford, May 1988.
77 * Modified by Rosen Sharma, Stanford, Aug 1994.
78 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
79 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
80 *
81 * MULTICAST Revision: 3.5.1.4
82 */
83
84#include <sys/param.h>
85#include <sys/systm.h>
86#include <sys/malloc.h>
87#include <sys/mbuf.h>
88#include <sys/socket.h>
89#include <sys/protosw.h>
90#include <sys/kernel.h>
91#include <sys/sysctl.h>
92
93#include <net/if.h>
94#include <net/route.h>
95
96#include <netinet/in.h>
97#include <netinet/in_var.h>
98#include <netinet/in_systm.h>
99#include <netinet/ip.h>
100#include <netinet/ip_var.h>
101#include <netinet/igmp.h>
102#include <netinet/igmp_var.h>
103
2d21ac55
A
104#if CONFIG_MACF_NET
105#include <security/mac_framework.h>
106#endif
107
9bccf70c 108#ifndef __APPLE__
1c79356b 109static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
9bccf70c 110#endif
1c79356b
A
111
112static struct router_info *
4a3eedf9 113 find_rti(struct ifnet *ifp, int wait);
1c79356b
A
114
115static struct igmpstat igmpstat;
116
117SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RD,
118 &igmpstat, igmpstat, "");
119
120static int igmp_timers_are_running;
b0d623f7
A
121static uint32_t igmp_all_hosts_group;
122static uint32_t igmp_all_rtrs_group;
1c79356b
A
123static struct mbuf *router_alert;
124static struct router_info *Head;
125
b0d623f7 126static void igmp_sendpkt(struct in_multi *, int, uint32_t);
1c79356b
A
127
128void
2d21ac55 129igmp_init(void)
1c79356b
A
130{
131 struct ipoption *ra;
132
133 /*
134 * To avoid byte-swapping the same value over and over again.
135 */
136 igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
137 igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP);
138
139 igmp_timers_are_running = 0;
140
141 /*
142 * Construct a Router Alert option to use in outgoing packets
143 */
b0d623f7 144 MGET(router_alert, M_WAIT, MT_DATA);
1c79356b
A
145 ra = mtod(router_alert, struct ipoption *);
146 ra->ipopt_dst.s_addr = 0;
147 ra->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */
148 ra->ipopt_list[1] = 0x04; /* 4 bytes long */
149 ra->ipopt_list[2] = 0x00;
150 ra->ipopt_list[3] = 0x00;
151 router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1];
152
153 Head = (struct router_info *) 0;
154}
155
156static struct router_info *
91447636 157find_rti(
4a3eedf9 158 struct ifnet *ifp, int wait)
1c79356b 159{
91447636 160 struct router_info *rti = Head;
55e303ae
A
161
162
1c79356b
A
163#if IGMP_DEBUG
164 printf("[igmp.c, _find_rti] --> entering \n");
165#endif
55e303ae
A
166 while (rti) {
167 if (rti->rti_ifp == ifp) {
1c79356b
A
168#if IGMP_DEBUG
169 printf("[igmp.c, _find_rti] --> found old entry \n");
170#endif
55e303ae
A
171 return rti;
172 }
173 rti = rti->rti_next;
174 }
175
4a3eedf9 176 MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, wait);
55e303ae
A
177 if (rti != NULL)
178 {
179 rti->rti_ifp = ifp;
180 rti->rti_type = IGMP_V2_ROUTER;
181 rti->rti_time = 0;
182 rti->rti_next = Head;
183 Head = rti;
184 }
1c79356b 185#if IGMP_DEBUG
55e303ae 186 if (rti) printf("[igmp.c, _find_rti] --> created an entry \n");
1c79356b 187#endif
55e303ae 188 return rti;
1c79356b
A
189}
190
191void
91447636
A
192igmp_input(
193 struct mbuf *m,
194 int iphlen)
1c79356b 195{
91447636
A
196 struct igmp *igmp;
197 struct ip *ip;
198 int igmplen;
199 struct ifnet *ifp = m->m_pkthdr.rcvif;
200 int minlen;
201 struct in_multi *inm;
202 struct in_ifaddr *ia;
1c79356b
A
203 struct in_multistep step;
204 struct router_info *rti;
205
206 int timer; /** timer value in the igmp query header **/
207
208 ++igmpstat.igps_rcv_total;
209
210 ip = mtod(m, struct ip *);
211 igmplen = ip->ip_len;
212
213 /*
214 * Validate lengths
215 */
216 if (igmplen < IGMP_MINLEN) {
217 ++igmpstat.igps_rcv_tooshort;
218 m_freem(m);
219 return;
220 }
221 minlen = iphlen + IGMP_MINLEN;
222 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
223 (m = m_pullup(m, minlen)) == 0) {
224 ++igmpstat.igps_rcv_tooshort;
225 return;
226 }
227
228 /*
229 * Validate checksum
230 */
231 m->m_data += iphlen;
232 m->m_len -= iphlen;
233 igmp = mtod(m, struct igmp *);
234 if (in_cksum(m, igmplen)) {
235 ++igmpstat.igps_rcv_badsum;
236 m_freem(m);
237 return;
238 }
239 m->m_data -= iphlen;
240 m->m_len += iphlen;
241
242 ip = mtod(m, struct ip *);
243 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
244 if (timer == 0)
245 timer = 1;
4a3eedf9 246 rti = find_rti(ifp, M_NOWAIT);
55e303ae
A
247 if (rti == NULL) {
248 m_freem(m);
249 return;
250 }
1c79356b
A
251
252 /*
253 * In the IGMPv2 specification, there are 3 states and a flag.
254 *
255 * In Non-Member state, we simply don't have a membership record.
256 * In Delaying Member state, our timer is running (inm->inm_timer)
257 * In Idle Member state, our timer is not running (inm->inm_timer==0)
258 *
259 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
260 * we have heard a report from another member, or IGMP_IREPORTEDLAST
261 * if I sent the last report.
262 */
263 switch (igmp->igmp_type) {
264
265 case IGMP_MEMBERSHIP_QUERY:
266 ++igmpstat.igps_rcv_queries;
267
268 if (ifp->if_flags & IFF_LOOPBACK)
269 break;
270
271 if (igmp->igmp_code == 0) {
272 /*
273 * Old router. Remember that the querier on this
274 * interface is old, and set the timer to the
275 * value in RFC 1112.
276 */
277
278 rti->rti_type = IGMP_V1_ROUTER;
279 rti->rti_time = 0;
280
281 timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
282
283 if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
284 igmp->igmp_group.s_addr != 0) {
285 ++igmpstat.igps_rcv_badqueries;
286 m_freem(m);
287 return;
288 }
289 } else {
290 /*
291 * New router. Simply do the new validity check.
292 */
293
294 if (igmp->igmp_group.s_addr != 0 &&
295 !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
296 ++igmpstat.igps_rcv_badqueries;
297 m_freem(m);
298 return;
299 }
300 }
301
302 /*
303 * - Start the timers in all of our membership records
304 * that the query applies to for the interface on
305 * which the query arrived excl. those that belong
306 * to the "all-hosts" group (224.0.0.1).
307 * - Restart any timer that is already running but has
308 * a value longer than the requested timeout.
309 * - Use the value specified in the query message as
310 * the maximum timeout.
311 */
b0d623f7 312 lck_mtx_lock(rnh_lock);
1c79356b
A
313 IN_FIRST_MULTI(step, inm);
314 while (inm != NULL) {
315 if (inm->inm_ifp == ifp &&
316 inm->inm_addr.s_addr != igmp_all_hosts_group &&
317 (igmp->igmp_group.s_addr == 0 ||
318 igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
319 if (inm->inm_timer == 0 ||
320 inm->inm_timer > timer) {
321 inm->inm_timer =
322 IGMP_RANDOM_DELAY(timer);
323 igmp_timers_are_running = 1;
324 }
325 }
326 IN_NEXT_MULTI(step, inm);
327 }
b0d623f7 328 lck_mtx_unlock(rnh_lock);
1c79356b
A
329
330 break;
331
332 case IGMP_V1_MEMBERSHIP_REPORT:
333 case IGMP_V2_MEMBERSHIP_REPORT:
334 /*
335 * For fast leave to work, we have to know that we are the
336 * last person to send a report for this group. Reports
337 * can potentially get looped back if we are a multicast
338 * router, so discard reports sourced by me.
339 */
340 IFP_TO_IA(ifp, ia);
b0d623f7
A
341 if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr) {
342 ifafree(&ia->ia_ifa);
1c79356b 343 break;
b0d623f7 344 }
1c79356b
A
345
346 ++igmpstat.igps_rcv_reports;
347
b0d623f7
A
348 if (ifp->if_flags & IFF_LOOPBACK) {
349 if (ia != NULL)
350 ifafree(&ia->ia_ifa);
1c79356b 351 break;
b0d623f7 352 }
1c79356b
A
353
354 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
355 ++igmpstat.igps_rcv_badreports;
356 m_freem(m);
b0d623f7
A
357 if (ia != NULL)
358 ifafree(&ia->ia_ifa);
1c79356b
A
359 return;
360 }
361
362 /*
363 * KLUDGE: if the IP source address of the report has an
364 * unspecified (i.e., zero) subnet number, as is allowed for
365 * a booting host, replace it with the correct subnet number
366 * so that a process-level multicast routing demon can
367 * determine which subnet it arrived from. This is necessary
368 * to compensate for the lack of any way for a process to
369 * determine the arrival interface of an incoming packet.
370 */
371 if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0)
372 if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
373
374 /*
375 * If we belong to the group being reported, stop
376 * our timer for that group.
377 */
91447636 378 ifnet_lock_shared(ifp);
1c79356b 379 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
91447636 380 ifnet_lock_done(ifp);
1c79356b
A
381
382 if (inm != NULL) {
383 inm->inm_timer = 0;
384 ++igmpstat.igps_rcv_ourreports;
385
386 inm->inm_state = IGMP_OTHERMEMBER;
387 }
388
b0d623f7
A
389 if (ia != NULL)
390 ifafree(&ia->ia_ifa);
1c79356b
A
391 break;
392 }
393
394 /*
395 * Pass all valid IGMP packets up to any process(es) listening
396 * on a raw IGMP socket.
397 */
398 rip_input(m, iphlen);
399}
400
55e303ae 401int
2d21ac55 402igmp_joingroup(struct in_multi *inm)
1c79356b 403{
1c79356b
A
404
405 if (inm->inm_addr.s_addr == igmp_all_hosts_group
406 || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
407 inm->inm_timer = 0;
408 inm->inm_state = IGMP_OTHERMEMBER;
409 } else {
4a3eedf9 410 inm->inm_rti = find_rti(inm->inm_ifp, M_WAITOK);
55e303ae 411 if (inm->inm_rti == NULL) return ENOMEM;
1c79356b
A
412 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
413 inm->inm_timer = IGMP_RANDOM_DELAY(
414 IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
415 inm->inm_state = IGMP_IREPORTEDLAST;
416 igmp_timers_are_running = 1;
417 }
55e303ae 418 return 0;
1c79356b
A
419}
420
421void
2d21ac55 422igmp_leavegroup(struct in_multi *inm)
1c79356b
A
423{
424 if (inm->inm_state == IGMP_IREPORTEDLAST &&
425 inm->inm_addr.s_addr != igmp_all_hosts_group &&
426 !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
427 inm->inm_rti->rti_type != IGMP_V1_ROUTER)
428 igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
429}
430
431void
2d21ac55 432igmp_fasttimo(void)
1c79356b 433{
91447636 434 struct in_multi *inm;
1c79356b 435 struct in_multistep step;
1c79356b
A
436
437 /*
438 * Quick check to see if any work needs to be done, in order
439 * to minimize the overhead of fasttimo processing.
440 */
441
442 if (!igmp_timers_are_running)
443 return;
444
1c79356b
A
445 igmp_timers_are_running = 0;
446 IN_FIRST_MULTI(step, inm);
447 while (inm != NULL) {
448 if (inm->inm_timer == 0) {
449 /* do nothing */
4a3eedf9 450 } else if ((--inm->inm_timer == 0) && (inm->inm_rti != NULL)) {
1c79356b
A
451 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
452 inm->inm_state = IGMP_IREPORTEDLAST;
453 } else {
454 igmp_timers_are_running = 1;
455 }
456 IN_NEXT_MULTI(step, inm);
457 }
1c79356b
A
458}
459
460void
2d21ac55 461igmp_slowtimo(void)
1c79356b 462{
91447636 463 struct router_info *rti = Head;
1c79356b
A
464
465#if IGMP_DEBUG
466 printf("[igmp.c,_slowtimo] -- > entering \n");
467#endif
468 while (rti) {
469 if (rti->rti_type == IGMP_V1_ROUTER) {
470 rti->rti_time++;
471 if (rti->rti_time >= IGMP_AGE_THRESHOLD) {
472 rti->rti_type = IGMP_V2_ROUTER;
473 }
474 }
475 rti = rti->rti_next;
476 }
477#if IGMP_DEBUG
478 printf("[igmp.c,_slowtimo] -- > exiting \n");
479#endif
1c79356b
A
480}
481
482static struct route igmprt;
483
484static void
b0d623f7 485igmp_sendpkt(struct in_multi *inm, int type, uint32_t addr)
1c79356b
A
486{
487 struct mbuf *m;
488 struct igmp *igmp;
489 struct ip *ip;
490 struct ip_moptions imo;
491
2d21ac55 492 MGETHDR(m, M_DONTWAIT, MT_HEADER); /* MAC-OK */
1c79356b
A
493 if (m == NULL)
494 return;
495
2d21ac55
A
496 m->m_pkthdr.rcvif = lo_ifp;
497#if CONFIG_MACF_NET
498 mac_mbuf_label_associate_linklayer(inm->inm_ifp, m);
499#endif
1c79356b
A
500 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
501 MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
502 m->m_data += sizeof(struct ip);
503 m->m_len = IGMP_MINLEN;
0b4e3aa0
A
504 m->m_pkthdr.csum_flags = 0;
505 m->m_pkthdr.csum_data = 0;
1c79356b
A
506 igmp = mtod(m, struct igmp *);
507 igmp->igmp_type = type;
508 igmp->igmp_code = 0;
509 igmp->igmp_group = inm->inm_addr;
510 igmp->igmp_cksum = 0;
511 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
512
513 m->m_data -= sizeof(struct ip);
514 m->m_len += sizeof(struct ip);
515 ip = mtod(m, struct ip *);
516 ip->ip_tos = 0;
517 ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
518 ip->ip_off = 0;
519 ip->ip_p = IPPROTO_IGMP;
520 ip->ip_src.s_addr = INADDR_ANY;
521 ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
522
523 imo.imo_multicast_ifp = inm->inm_ifp;
524 imo.imo_multicast_ttl = 1;
2d21ac55
A
525 imo.imo_multicast_vif = -1;
526#if MROUTING
1c79356b
A
527 /*
528 * Request loopback of the report if we are acting as a multicast
529 * router, so that the process-level routing demon can hear it.
530 */
531 imo.imo_multicast_loop = (ip_mrouter != NULL);
2d21ac55
A
532#else
533 imo.imo_multicast_loop = 0;
534#endif
1c79356b
A
535
536 /*
537 * XXX
538 * Do we have to worry about reentrancy here? Don't think so.
539 */
2d21ac55 540 ip_output(m, router_alert, &igmprt, 0, &imo, NULL);
1c79356b
A
541
542 ++igmpstat.igps_snd_reports;
543}
2d21ac55 544