linprocfs - Introduce /proc/mounts
[dragonfly.git] / sys / netinet / igmp.c
blob7e844cf107d0c505a9c2fbbe7c96ab01f0ed80ba
1 /*
2 * Copyright (c) 1988 Stephen Deering.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
6 * This code is derived from software contributed to Berkeley by
7 * Stephen Deering of Stanford University.
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.
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.
37 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
38 * $FreeBSD: src/sys/netinet/igmp.c,v 1.29.2.2 2003/01/23 21:06:44 sam Exp $
39 * $DragonFly: src/sys/netinet/igmp.c,v 1.14 2008/06/08 08:38:05 sephe Exp $
43 * Internet Group Management Protocol (IGMP) routines.
45 * Written by Steve Deering, Stanford, May 1988.
46 * Modified by Rosen Sharma, Stanford, Aug 1994.
47 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
48 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
50 * MULTICAST Revision: 3.5.1.4
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/socket.h>
58 #include <sys/protosw.h>
59 #include <sys/kernel.h>
60 #include <sys/sysctl.h>
61 #include <sys/in_cksum.h>
62 #include <sys/thread2.h>
64 #include <machine/stdarg.h>
66 #include <net/if.h>
67 #include <net/route.h>
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/igmp.h>
75 #include <netinet/igmp_var.h>
77 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
79 static struct router_info *
80 find_rti (struct ifnet *ifp);
82 static struct igmpstat igmpstat;
84 SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW,
85 &igmpstat, igmpstat, "");
87 static int igmp_timers_are_running;
88 static u_long igmp_all_hosts_group;
89 static u_long igmp_all_rtrs_group;
90 static struct mbuf *router_alert;
91 static struct router_info *Head;
93 static void igmp_sendpkt (struct in_multi *, int, unsigned long);
95 void
96 igmp_init(void)
98 struct ipoption *ra;
101 * To avoid byte-swapping the same value over and over again.
103 igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
104 igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP);
106 igmp_timers_are_running = 0;
109 * Construct a Router Alert option to use in outgoing packets
111 MGET(router_alert, MB_DONTWAIT, MT_DATA);
112 ra = mtod(router_alert, struct ipoption *);
113 ra->ipopt_dst.s_addr = 0;
114 ra->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */
115 ra->ipopt_list[1] = 0x04; /* 4 bytes long */
116 ra->ipopt_list[2] = 0x00;
117 ra->ipopt_list[3] = 0x00;
118 router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1];
120 Head = NULL;
123 static struct router_info *
124 find_rti(struct ifnet *ifp)
126 struct router_info *rti = Head;
128 #ifdef IGMP_DEBUG
129 kprintf("[igmp.c, _find_rti] --> entering \n");
130 #endif
131 while (rti) {
132 if (rti->rti_ifp == ifp) {
133 #ifdef IGMP_DEBUG
134 kprintf("[igmp.c, _find_rti] --> found old entry \n");
135 #endif
136 return rti;
138 rti = rti->rti_next;
140 MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, M_INTWAIT);
141 rti->rti_ifp = ifp;
142 rti->rti_type = IGMP_V2_ROUTER;
143 rti->rti_time = 0;
144 rti->rti_next = Head;
145 Head = rti;
146 #ifdef IGMP_DEBUG
147 kprintf("[igmp.c, _find_rti] --> created an entry \n");
148 #endif
149 return rti;
152 void
153 igmp_input(struct mbuf *m, ...)
155 int iphlen, off, proto;
156 struct igmp *igmp;
157 struct ip *ip;
158 int igmplen;
159 struct ifnet *ifp = m->m_pkthdr.rcvif;
160 int minlen;
161 struct in_multi *inm;
162 struct in_ifaddr *ia;
163 struct in_multistep step;
164 struct router_info *rti;
165 __va_list ap;
167 int timer; /** timer value in the igmp query header **/
169 __va_start(ap, m);
170 off = __va_arg(ap, int);
171 proto = __va_arg(ap, int);
172 __va_end(ap);
174 iphlen = off;
176 ++igmpstat.igps_rcv_total;
178 ip = mtod(m, struct ip *);
179 igmplen = ip->ip_len;
182 * Validate lengths
184 if (igmplen < IGMP_MINLEN) {
185 ++igmpstat.igps_rcv_tooshort;
186 m_freem(m);
187 return;
189 minlen = iphlen + IGMP_MINLEN;
190 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
191 (m = m_pullup(m, minlen)) == 0) {
192 ++igmpstat.igps_rcv_tooshort;
193 return;
197 * Validate checksum
199 m->m_data += iphlen;
200 m->m_len -= iphlen;
201 igmp = mtod(m, struct igmp *);
202 if (in_cksum(m, igmplen)) {
203 ++igmpstat.igps_rcv_badsum;
204 m_freem(m);
205 return;
207 m->m_data -= iphlen;
208 m->m_len += iphlen;
210 ip = mtod(m, struct ip *);
211 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
212 if (timer == 0)
213 timer = 1;
214 rti = find_rti(ifp);
217 * In the IGMPv2 specification, there are 3 states and a flag.
219 * In Non-Member state, we simply don't have a membership record.
220 * In Delaying Member state, our timer is running (inm->inm_timer)
221 * In Idle Member state, our timer is not running (inm->inm_timer==0)
223 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
224 * we have heard a report from another member, or IGMP_IREPORTEDLAST
225 * if I sent the last report.
227 switch (igmp->igmp_type) {
229 case IGMP_MEMBERSHIP_QUERY:
230 ++igmpstat.igps_rcv_queries;
232 if (ifp->if_flags & IFF_LOOPBACK)
233 break;
235 if (igmp->igmp_code == 0) {
237 * Old router. Remember that the querier on this
238 * interface is old, and set the timer to the
239 * value in RFC 1112.
242 rti->rti_type = IGMP_V1_ROUTER;
243 rti->rti_time = 0;
245 timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
247 if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
248 igmp->igmp_group.s_addr != 0) {
249 ++igmpstat.igps_rcv_badqueries;
250 m_freem(m);
251 return;
253 } else {
255 * New router. Simply do the new validity check.
258 if (igmp->igmp_group.s_addr != 0 &&
259 !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
260 ++igmpstat.igps_rcv_badqueries;
261 m_freem(m);
262 return;
267 * - Start the timers in all of our membership records
268 * that the query applies to for the interface on
269 * which the query arrived excl. those that belong
270 * to the "all-hosts" group (224.0.0.1).
271 * - Restart any timer that is already running but has
272 * a value longer than the requested timeout.
273 * - Use the value specified in the query message as
274 * the maximum timeout.
276 IN_FIRST_MULTI(step, inm);
277 while (inm != NULL) {
278 if (inm->inm_ifp == ifp &&
279 inm->inm_addr.s_addr != igmp_all_hosts_group &&
280 (igmp->igmp_group.s_addr == 0 ||
281 igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
282 if (inm->inm_timer == 0 ||
283 inm->inm_timer > timer) {
284 inm->inm_timer =
285 IGMP_RANDOM_DELAY(timer);
286 igmp_timers_are_running = 1;
289 IN_NEXT_MULTI(step, inm);
292 break;
294 case IGMP_V1_MEMBERSHIP_REPORT:
295 case IGMP_V2_MEMBERSHIP_REPORT:
297 * For fast leave to work, we have to know that we are the
298 * last person to send a report for this group. Reports
299 * can potentially get looped back if we are a multicast
300 * router, so discard reports sourced by me.
302 ia = IFP_TO_IA(ifp);
303 if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
304 break;
306 ++igmpstat.igps_rcv_reports;
308 if (ifp->if_flags & IFF_LOOPBACK)
309 break;
311 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
312 ++igmpstat.igps_rcv_badreports;
313 m_freem(m);
314 return;
318 * KLUDGE: if the IP source address of the report has an
319 * unspecified (i.e., zero) subnet number, as is allowed for
320 * a booting host, replace it with the correct subnet number
321 * so that a process-level multicast routing demon can
322 * determine which subnet it arrived from. This is necessary
323 * to compensate for the lack of any way for a process to
324 * determine the arrival interface of an incoming packet.
326 if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0)
327 if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
330 * If we belong to the group being reported, stop
331 * our timer for that group.
333 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
335 if (inm != NULL) {
336 inm->inm_timer = 0;
337 ++igmpstat.igps_rcv_ourreports;
339 inm->inm_state = IGMP_OTHERMEMBER;
342 break;
346 * Pass all valid IGMP packets up to any process(es) listening
347 * on a raw IGMP socket.
349 rip_input(m, off, proto);
352 void
353 igmp_joingroup(struct in_multi *inm)
355 crit_enter();
356 if (inm->inm_addr.s_addr == igmp_all_hosts_group
357 || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
358 inm->inm_timer = 0;
359 inm->inm_state = IGMP_OTHERMEMBER;
360 } else {
361 inm->inm_rti = find_rti(inm->inm_ifp);
362 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
363 inm->inm_timer = IGMP_RANDOM_DELAY(
364 IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
365 inm->inm_state = IGMP_IREPORTEDLAST;
366 igmp_timers_are_running = 1;
368 crit_exit();
371 void
372 igmp_leavegroup(struct in_multi *inm)
374 if (inm->inm_state == IGMP_IREPORTEDLAST &&
375 inm->inm_addr.s_addr != igmp_all_hosts_group &&
376 !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
377 inm->inm_rti->rti_type != IGMP_V1_ROUTER)
378 igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
381 void
382 igmp_fasttimo(void)
384 struct in_multi *inm;
385 struct in_multistep step;
388 * Quick check to see if any work needs to be done, in order
389 * to minimize the overhead of fasttimo processing.
392 if (!igmp_timers_are_running)
393 return;
395 crit_enter();
396 igmp_timers_are_running = 0;
397 IN_FIRST_MULTI(step, inm);
398 while (inm != NULL) {
399 if (inm->inm_timer == 0) {
400 /* do nothing */
401 } else if (--inm->inm_timer == 0) {
402 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
403 inm->inm_state = IGMP_IREPORTEDLAST;
404 } else {
405 igmp_timers_are_running = 1;
407 IN_NEXT_MULTI(step, inm);
409 crit_exit();
412 void
413 igmp_slowtimo(void)
415 struct router_info *rti = Head;
417 crit_enter();
418 #ifdef IGMP_DEBUG
419 kprintf("[igmp.c,_slowtimo] -- > entering \n");
420 #endif
421 while (rti) {
422 if (rti->rti_type == IGMP_V1_ROUTER) {
423 rti->rti_time++;
424 if (rti->rti_time >= IGMP_AGE_THRESHOLD) {
425 rti->rti_type = IGMP_V2_ROUTER;
428 rti = rti->rti_next;
430 #ifdef IGMP_DEBUG
431 kprintf("[igmp.c,_slowtimo] -- > exiting \n");
432 #endif
433 crit_exit();
436 static struct route igmprt;
438 static void
439 igmp_sendpkt(struct in_multi *inm, int type, unsigned long addr)
441 struct mbuf *m;
442 struct igmp *igmp;
443 struct ip *ip;
444 struct ip_moptions imo;
446 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
447 if (m == NULL)
448 return;
450 m->m_pkthdr.rcvif = loif;
451 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
452 MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
453 m->m_data += sizeof(struct ip);
454 m->m_len = IGMP_MINLEN;
455 igmp = mtod(m, struct igmp *);
456 igmp->igmp_type = type;
457 igmp->igmp_code = 0;
458 igmp->igmp_group = inm->inm_addr;
459 igmp->igmp_cksum = 0;
460 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
462 m->m_data -= sizeof(struct ip);
463 m->m_len += sizeof(struct ip);
464 ip = mtod(m, struct ip *);
465 ip->ip_tos = 0;
466 ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
467 ip->ip_off = 0;
468 ip->ip_p = IPPROTO_IGMP;
469 ip->ip_src.s_addr = INADDR_ANY;
470 ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
472 imo.imo_multicast_ifp = inm->inm_ifp;
473 imo.imo_multicast_ttl = 1;
474 imo.imo_multicast_vif = -1;
476 * Request loopback of the report if we are acting as a multicast
477 * router, so that the process-level routing demon can hear it.
479 imo.imo_multicast_loop = (ip_mrouter != NULL);
482 * XXX
483 * Do we have to worry about reentrancy here? Don't think so.
485 ip_output(m, router_alert, &igmprt, 0, &imo, NULL);
487 ++igmpstat.igps_snd_reports;