arcmsr(4): Use MSI if it is supported by the device.
[dragonfly.git] / sys / netinet / igmp.c
blobf754eca041bf05d3c369b7b7534998cb282b94ae
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 $
42 * Internet Group Management Protocol (IGMP) routines.
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.
49 * MULTICAST Revision: 3.5.1.4
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/malloc.h>
55 #include <sys/mbuf.h>
56 #include <sys/socket.h>
57 #include <sys/protosw.h>
58 #include <sys/kernel.h>
59 #include <sys/sysctl.h>
60 #include <sys/in_cksum.h>
61 #include <sys/thread2.h>
63 #include <machine/stdarg.h>
65 #include <net/if.h>
66 #include <net/route.h>
68 #include <netinet/in.h>
69 #include <netinet/in_var.h>
70 #include <netinet/in_systm.h>
71 #include <netinet/ip.h>
72 #include <netinet/ip_var.h>
73 #include <netinet/igmp.h>
74 #include <netinet/igmp_var.h>
76 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
78 static struct router_info *
79 find_rti (struct ifnet *ifp);
81 static struct igmpstat igmpstat;
83 SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW,
84 &igmpstat, igmpstat, "IGMP statistics");
86 static int igmp_timers_are_running;
87 static u_long igmp_all_hosts_group;
88 static u_long igmp_all_rtrs_group;
89 static struct mbuf *router_alert;
90 static struct router_info *Head;
92 static void igmp_sendpkt (struct in_multi *, int, unsigned long);
94 void
95 igmp_init(void)
97 struct ipoption *ra;
100 * To avoid byte-swapping the same value over and over again.
102 igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
103 igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP);
105 igmp_timers_are_running = 0;
108 * Construct a Router Alert option to use in outgoing packets
110 MGET(router_alert, MB_DONTWAIT, MT_DATA);
111 ra = mtod(router_alert, struct ipoption *);
112 ra->ipopt_dst.s_addr = 0;
113 ra->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */
114 ra->ipopt_list[1] = 0x04; /* 4 bytes long */
115 ra->ipopt_list[2] = 0x00;
116 ra->ipopt_list[3] = 0x00;
117 router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1];
119 Head = NULL;
122 static struct router_info *
123 find_rti(struct ifnet *ifp)
125 struct router_info *rti = Head;
127 #ifdef IGMP_DEBUG
128 kprintf("[igmp.c, _find_rti] --> entering \n");
129 #endif
130 while (rti) {
131 if (rti->rti_ifp == ifp) {
132 #ifdef IGMP_DEBUG
133 kprintf("[igmp.c, _find_rti] --> found old entry \n");
134 #endif
135 return rti;
137 rti = rti->rti_next;
139 rti = kmalloc(sizeof *rti, M_IGMP, M_INTWAIT);
140 rti->rti_ifp = ifp;
141 rti->rti_type = IGMP_V2_ROUTER;
142 rti->rti_time = 0;
143 rti->rti_next = Head;
144 Head = rti;
145 #ifdef IGMP_DEBUG
146 kprintf("[igmp.c, _find_rti] --> created an entry \n");
147 #endif
148 return rti;
152 igmp_input(struct mbuf **mp, int *offp, int proto)
154 struct mbuf *m = *mp;
155 int iphlen;
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 int timer; /** timer value in the igmp query header **/
167 iphlen = *offp;
168 *mp = NULL;
170 ++igmpstat.igps_rcv_total;
172 ip = mtod(m, struct ip *);
173 igmplen = ip->ip_len;
176 * Validate lengths
178 if (igmplen < IGMP_MINLEN) {
179 ++igmpstat.igps_rcv_tooshort;
180 m_freem(m);
181 return(IPPROTO_DONE);
183 minlen = iphlen + IGMP_MINLEN;
184 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
185 (m = m_pullup(m, minlen)) == NULL) {
186 ++igmpstat.igps_rcv_tooshort;
187 return(IPPROTO_DONE);
191 * Validate checksum
193 m->m_data += iphlen;
194 m->m_len -= iphlen;
195 igmp = mtod(m, struct igmp *);
196 if (in_cksum(m, igmplen)) {
197 ++igmpstat.igps_rcv_badsum;
198 m_freem(m);
199 return(IPPROTO_DONE);
201 m->m_data -= iphlen;
202 m->m_len += iphlen;
204 ip = mtod(m, struct ip *);
205 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
206 if (timer == 0)
207 timer = 1;
208 rti = find_rti(ifp);
211 * In the IGMPv2 specification, there are 3 states and a flag.
213 * In Non-Member state, we simply don't have a membership record.
214 * In Delaying Member state, our timer is running (inm->inm_timer)
215 * In Idle Member state, our timer is not running (inm->inm_timer==0)
217 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
218 * we have heard a report from another member, or IGMP_IREPORTEDLAST
219 * if I sent the last report.
221 switch (igmp->igmp_type) {
223 case IGMP_MEMBERSHIP_QUERY:
224 ++igmpstat.igps_rcv_queries;
226 if (ifp->if_flags & IFF_LOOPBACK)
227 break;
229 if (igmp->igmp_code == 0) {
231 * Old router. Remember that the querier on this
232 * interface is old, and set the timer to the
233 * value in RFC 1112.
236 rti->rti_type = IGMP_V1_ROUTER;
237 rti->rti_time = 0;
239 timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
241 if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
242 igmp->igmp_group.s_addr != 0) {
243 ++igmpstat.igps_rcv_badqueries;
244 m_freem(m);
245 return(IPPROTO_DONE);
247 } else {
249 * New router. Simply do the new validity check.
252 if (igmp->igmp_group.s_addr != 0 &&
253 !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
254 ++igmpstat.igps_rcv_badqueries;
255 m_freem(m);
256 return(IPPROTO_DONE);
261 * - Start the timers in all of our membership records
262 * that the query applies to for the interface on
263 * which the query arrived excl. those that belong
264 * to the "all-hosts" group (224.0.0.1).
265 * - Restart any timer that is already running but has
266 * a value longer than the requested timeout.
267 * - Use the value specified in the query message as
268 * the maximum timeout.
270 IN_FIRST_MULTI(step, inm);
271 while (inm != NULL) {
272 if (inm->inm_ifp == ifp &&
273 inm->inm_addr.s_addr != igmp_all_hosts_group &&
274 (igmp->igmp_group.s_addr == 0 ||
275 igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
276 if (inm->inm_timer == 0 ||
277 inm->inm_timer > timer) {
278 inm->inm_timer =
279 IGMP_RANDOM_DELAY(timer);
280 igmp_timers_are_running = 1;
283 IN_NEXT_MULTI(step, inm);
286 break;
288 case IGMP_V1_MEMBERSHIP_REPORT:
289 case IGMP_V2_MEMBERSHIP_REPORT:
291 * For fast leave to work, we have to know that we are the
292 * last person to send a report for this group. Reports
293 * can potentially get looped back if we are a multicast
294 * router, so discard reports sourced by me.
296 ia = IFP_TO_IA(ifp);
297 if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
298 break;
300 ++igmpstat.igps_rcv_reports;
302 if (ifp->if_flags & IFF_LOOPBACK)
303 break;
305 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
306 ++igmpstat.igps_rcv_badreports;
307 m_freem(m);
308 return(IPPROTO_DONE);
312 * KLUDGE: if the IP source address of the report has an
313 * unspecified (i.e., zero) subnet number, as is allowed for
314 * a booting host, replace it with the correct subnet number
315 * so that a process-level multicast routing demon can
316 * determine which subnet it arrived from. This is necessary
317 * to compensate for the lack of any way for a process to
318 * determine the arrival interface of an incoming packet.
320 if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0)
321 if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
324 * If we belong to the group being reported, stop
325 * our timer for that group.
327 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
329 if (inm != NULL) {
330 inm->inm_timer = 0;
331 ++igmpstat.igps_rcv_ourreports;
333 inm->inm_state = IGMP_OTHERMEMBER;
336 break;
340 * Pass all valid IGMP packets up to any process(es) listening
341 * on a raw IGMP socket.
343 *mp = m;
344 rip_input(mp, offp, proto);
345 return(IPPROTO_DONE);
348 void
349 igmp_joingroup(struct in_multi *inm)
351 crit_enter();
352 if (inm->inm_addr.s_addr == igmp_all_hosts_group
353 || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
354 inm->inm_timer = 0;
355 inm->inm_state = IGMP_OTHERMEMBER;
356 } else {
357 inm->inm_rti = find_rti(inm->inm_ifp);
358 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
359 inm->inm_timer = IGMP_RANDOM_DELAY(
360 IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
361 inm->inm_state = IGMP_IREPORTEDLAST;
362 igmp_timers_are_running = 1;
364 crit_exit();
367 void
368 igmp_leavegroup(struct in_multi *inm)
370 if (inm->inm_state == IGMP_IREPORTEDLAST &&
371 inm->inm_addr.s_addr != igmp_all_hosts_group &&
372 !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
373 inm->inm_rti->rti_type != IGMP_V1_ROUTER)
374 igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
377 void
378 igmp_fasttimo(void)
380 struct in_multi *inm;
381 struct in_multistep step;
384 * Quick check to see if any work needs to be done, in order
385 * to minimize the overhead of fasttimo processing.
388 if (!igmp_timers_are_running)
389 return;
391 crit_enter();
392 igmp_timers_are_running = 0;
393 IN_FIRST_MULTI(step, inm);
394 while (inm != NULL) {
395 if (inm->inm_timer == 0) {
396 /* do nothing */
397 } else if (--inm->inm_timer == 0) {
398 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
399 inm->inm_state = IGMP_IREPORTEDLAST;
400 } else {
401 igmp_timers_are_running = 1;
403 IN_NEXT_MULTI(step, inm);
405 crit_exit();
408 void
409 igmp_slowtimo(void)
411 struct router_info *rti = Head;
413 crit_enter();
414 #ifdef IGMP_DEBUG
415 kprintf("[igmp.c,_slowtimo] -- > entering \n");
416 #endif
417 while (rti) {
418 if (rti->rti_type == IGMP_V1_ROUTER) {
419 rti->rti_time++;
420 if (rti->rti_time >= IGMP_AGE_THRESHOLD) {
421 rti->rti_type = IGMP_V2_ROUTER;
424 rti = rti->rti_next;
426 #ifdef IGMP_DEBUG
427 kprintf("[igmp.c,_slowtimo] -- > exiting \n");
428 #endif
429 crit_exit();
432 static struct route igmprt;
434 static void
435 igmp_sendpkt(struct in_multi *inm, int type, unsigned long addr)
437 struct mbuf *m;
438 struct igmp *igmp;
439 struct ip *ip;
440 struct ip_moptions imo;
442 MGETHDR(m, MB_DONTWAIT, MT_HEADER);
443 if (m == NULL)
444 return;
446 m->m_pkthdr.rcvif = loif;
447 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
448 MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
449 m->m_data += sizeof(struct ip);
450 m->m_len = IGMP_MINLEN;
451 igmp = mtod(m, struct igmp *);
452 igmp->igmp_type = type;
453 igmp->igmp_code = 0;
454 igmp->igmp_group = inm->inm_addr;
455 igmp->igmp_cksum = 0;
456 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
458 m->m_data -= sizeof(struct ip);
459 m->m_len += sizeof(struct ip);
460 ip = mtod(m, struct ip *);
461 ip->ip_tos = 0;
462 ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
463 ip->ip_off = 0;
464 ip->ip_p = IPPROTO_IGMP;
465 ip->ip_src.s_addr = INADDR_ANY;
466 ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
468 imo.imo_multicast_ifp = inm->inm_ifp;
469 imo.imo_multicast_ttl = 1;
470 imo.imo_multicast_vif = -1;
472 * Request loopback of the report if we are acting as a multicast
473 * router, so that the process-level routing demon can hear it.
475 imo.imo_multicast_loop = (ip_mrouter != NULL);
478 * XXX
479 * Do we have to worry about reentrancy here? Don't think so.
481 ip_output(m, router_alert, &igmprt, 0, &imo, NULL);
483 ++igmpstat.igps_snd_reports;