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