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