inet6: only mark autoconf addresses tentative if detached
[dragonfly.git] / sys / netinet / igmp.c
blob44084c7397da6a0cca5320490fdab61d70dce46e
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. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
33 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
34 * $FreeBSD: src/sys/netinet/igmp.c,v 1.29.2.2 2003/01/23 21:06:44 sam Exp $
38 * Internet Group Management Protocol (IGMP) routines.
40 * Written by Steve Deering, Stanford, May 1988.
41 * Modified by Rosen Sharma, Stanford, Aug 1994.
42 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
43 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
45 * MULTICAST Revision: 3.5.1.4
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/malloc.h>
51 #include <sys/mbuf.h>
52 #include <sys/socket.h>
53 #include <sys/protosw.h>
54 #include <sys/kernel.h>
55 #include <sys/sysctl.h>
56 #include <sys/in_cksum.h>
57 #include <sys/thread2.h>
59 #include <machine/stdarg.h>
61 #include <net/if.h>
62 #include <net/route.h>
63 #include <net/netmsg2.h>
64 #include <net/netisr2.h>
66 #include <netinet/in.h>
67 #include <netinet/in_var.h>
68 #include <netinet/in_systm.h>
69 #include <netinet/ip.h>
70 #include <netinet/ip_var.h>
71 #include <netinet/igmp.h>
72 #include <netinet/igmp_var.h>
74 #define IGMP_FASTTIMO (hz / PR_FASTHZ)
75 #define IGMP_SLOWTIMO (hz / PR_SLOWHZ)
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, "IGMP statistics");
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);
94 static void igmp_fasttimo_dispatch(netmsg_t);
95 static void igmp_fasttimo(void *);
96 static void igmp_slowtimo_dispatch(netmsg_t);
97 static void igmp_slowtimo(void *);
99 static struct netmsg_base igmp_slowtimo_netmsg;
100 static struct callout igmp_slowtimo_ch;
101 static struct netmsg_base igmp_fasttimo_netmsg;
102 static struct callout igmp_fasttimo_ch;
104 void
105 igmp_init(void)
107 struct ipoption *ra;
110 * To avoid byte-swapping the same value over and over again.
112 igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
113 igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP);
115 igmp_timers_are_running = 0;
118 * Construct a Router Alert option to use in outgoing packets
120 MGET(router_alert, M_NOWAIT, MT_DATA);
121 ra = mtod(router_alert, struct ipoption *);
122 ra->ipopt_dst.s_addr = 0;
123 ra->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */
124 ra->ipopt_list[1] = 0x04; /* 4 bytes long */
125 ra->ipopt_list[2] = 0x00;
126 ra->ipopt_list[3] = 0x00;
127 router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1];
129 Head = NULL;
131 callout_init_mp(&igmp_slowtimo_ch);
132 netmsg_init(&igmp_slowtimo_netmsg, NULL, &netisr_adone_rport,
133 MSGF_PRIORITY, igmp_slowtimo_dispatch);
135 callout_init_mp(&igmp_fasttimo_ch);
136 netmsg_init(&igmp_fasttimo_netmsg, NULL, &netisr_adone_rport,
137 MSGF_PRIORITY, igmp_fasttimo_dispatch);
139 callout_reset_bycpu(&igmp_slowtimo_ch, IGMP_SLOWTIMO,
140 igmp_slowtimo, NULL, 0);
141 callout_reset_bycpu(&igmp_fasttimo_ch, IGMP_FASTTIMO,
142 igmp_fasttimo, NULL, 0);
145 static struct router_info *
146 find_rti(struct ifnet *ifp)
148 struct router_info *rti = Head;
150 #ifdef IGMP_DEBUG
151 kprintf("[igmp.c, _find_rti] --> entering \n");
152 #endif
153 while (rti) {
154 if (rti->rti_ifp == ifp) {
155 #ifdef IGMP_DEBUG
156 kprintf("[igmp.c, _find_rti] --> found old entry \n");
157 #endif
158 return rti;
160 rti = rti->rti_next;
162 rti = kmalloc(sizeof *rti, M_IGMP, M_INTWAIT);
163 rti->rti_ifp = ifp;
164 rti->rti_type = IGMP_V2_ROUTER;
165 rti->rti_time = 0;
166 rti->rti_next = Head;
167 Head = rti;
168 #ifdef IGMP_DEBUG
169 kprintf("[igmp.c, _find_rti] --> created an entry \n");
170 #endif
171 return rti;
175 igmp_input(struct mbuf **mp, int *offp, int proto)
177 struct mbuf *m = *mp;
178 int iphlen;
179 struct igmp *igmp;
180 struct ip *ip;
181 int igmplen;
182 struct ifnet *ifp = m->m_pkthdr.rcvif;
183 int minlen;
184 struct in_multi *inm;
185 struct in_ifaddr *ia;
186 struct in_multistep step;
187 struct router_info *rti;
188 int timer; /** timer value in the igmp query header **/
190 iphlen = *offp;
191 *mp = NULL;
193 ++igmpstat.igps_rcv_total;
195 ip = mtod(m, struct ip *);
196 igmplen = ntohs(ip->ip_len) - iphlen;
199 * Validate lengths
201 if (igmplen < IGMP_MINLEN) {
202 ++igmpstat.igps_rcv_tooshort;
203 m_freem(m);
204 return(IPPROTO_DONE);
206 minlen = iphlen + IGMP_MINLEN;
207 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
208 (m = m_pullup(m, minlen)) == NULL) {
209 ++igmpstat.igps_rcv_tooshort;
210 return(IPPROTO_DONE);
214 * Validate checksum
216 m->m_data += iphlen;
217 m->m_len -= iphlen;
218 igmp = mtod(m, struct igmp *);
219 if (in_cksum(m, igmplen)) {
220 ++igmpstat.igps_rcv_badsum;
221 m_freem(m);
222 return(IPPROTO_DONE);
224 m->m_data -= iphlen;
225 m->m_len += iphlen;
227 ip = mtod(m, struct ip *);
228 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
229 if (timer == 0)
230 timer = 1;
231 rti = find_rti(ifp);
234 * In the IGMPv2 specification, there are 3 states and a flag.
236 * In Non-Member state, we simply don't have a membership record.
237 * In Delaying Member state, our timer is running (inm->inm_timer)
238 * In Idle Member state, our timer is not running (inm->inm_timer==0)
240 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
241 * we have heard a report from another member, or IGMP_IREPORTEDLAST
242 * if I sent the last report.
244 switch (igmp->igmp_type) {
246 case IGMP_MEMBERSHIP_QUERY:
247 ++igmpstat.igps_rcv_queries;
249 if (ifp->if_flags & IFF_LOOPBACK)
250 break;
252 if (igmp->igmp_code == 0) {
254 * Old router. Remember that the querier on this
255 * interface is old, and set the timer to the
256 * value in RFC 1112.
259 rti->rti_type = IGMP_V1_ROUTER;
260 rti->rti_time = 0;
262 timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
264 if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
265 igmp->igmp_group.s_addr != 0) {
266 ++igmpstat.igps_rcv_badqueries;
267 m_freem(m);
268 return(IPPROTO_DONE);
270 } else {
272 * New router. Simply do the new validity check.
275 if (igmp->igmp_group.s_addr != 0 &&
276 !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
277 ++igmpstat.igps_rcv_badqueries;
278 m_freem(m);
279 return(IPPROTO_DONE);
284 * - Start the timers in all of our membership records
285 * that the query applies to for the interface on
286 * which the query arrived excl. those that belong
287 * to the "all-hosts" group (224.0.0.1).
288 * - Restart any timer that is already running but has
289 * a value longer than the requested timeout.
290 * - Use the value specified in the query message as
291 * the maximum timeout.
293 IN_FIRST_MULTI(step, inm);
294 while (inm != NULL) {
295 if (inm->inm_ifp == ifp &&
296 inm->inm_addr.s_addr != igmp_all_hosts_group &&
297 (igmp->igmp_group.s_addr == 0 ||
298 igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
299 if (inm->inm_timer == 0 ||
300 inm->inm_timer > timer) {
301 inm->inm_timer =
302 IGMP_RANDOM_DELAY(timer);
303 igmp_timers_are_running = 1;
306 IN_NEXT_MULTI(step, inm);
309 break;
311 case IGMP_V1_MEMBERSHIP_REPORT:
312 case IGMP_V2_MEMBERSHIP_REPORT:
314 * For fast leave to work, we have to know that we are the
315 * last person to send a report for this group. Reports
316 * can potentially get looped back if we are a multicast
317 * router, so discard reports sourced by me.
319 ia = IFP_TO_IA(ifp);
320 if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
321 break;
323 ++igmpstat.igps_rcv_reports;
325 if (ifp->if_flags & IFF_LOOPBACK)
326 break;
328 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
329 ++igmpstat.igps_rcv_badreports;
330 m_freem(m);
331 return(IPPROTO_DONE);
335 * KLUDGE: if the IP source address of the report has an
336 * unspecified (i.e., zero) subnet number, as is allowed for
337 * a booting host, replace it with the correct subnet number
338 * so that a process-level multicast routing demon can
339 * determine which subnet it arrived from. This is necessary
340 * to compensate for the lack of any way for a process to
341 * determine the arrival interface of an incoming packet.
343 if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0)
344 if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
347 * If we belong to the group being reported, stop
348 * our timer for that group.
350 inm = IN_LOOKUP_MULTI(&igmp->igmp_group, ifp);
352 if (inm != NULL) {
353 inm->inm_timer = 0;
354 ++igmpstat.igps_rcv_ourreports;
356 inm->inm_state = IGMP_OTHERMEMBER;
359 break;
363 * Pass all valid IGMP packets up to any process(es) listening
364 * on a raw IGMP socket.
366 *mp = m;
367 rip_input(mp, offp, proto);
368 return(IPPROTO_DONE);
371 void
372 igmp_joingroup(struct in_multi *inm)
374 crit_enter();
375 if (inm->inm_addr.s_addr == igmp_all_hosts_group
376 || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
377 inm->inm_timer = 0;
378 inm->inm_state = IGMP_OTHERMEMBER;
379 } else {
380 inm->inm_rti = find_rti(inm->inm_ifp);
381 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
382 inm->inm_timer = IGMP_RANDOM_DELAY(
383 IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
384 inm->inm_state = IGMP_IREPORTEDLAST;
385 igmp_timers_are_running = 1;
387 crit_exit();
390 void
391 igmp_leavegroup(struct in_multi *inm)
393 if (inm->inm_state == IGMP_IREPORTEDLAST &&
394 inm->inm_addr.s_addr != igmp_all_hosts_group &&
395 !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
396 inm->inm_rti->rti_type != IGMP_V1_ROUTER)
397 igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
400 static void
401 igmp_fasttimo(void *dummy __unused)
403 struct netmsg_base *msg = &igmp_fasttimo_netmsg;
405 KKASSERT(mycpuid == 0);
407 crit_enter();
408 if (msg->lmsg.ms_flags & MSGF_DONE)
409 netisr_sendmsg_oncpu(msg);
410 crit_exit();
413 static void
414 igmp_fasttimo_dispatch(netmsg_t nmsg)
416 struct in_multi *inm;
417 struct in_multistep step;
419 ASSERT_NETISR0;
421 crit_enter();
422 netisr_replymsg(&nmsg->base, 0); /* reply ASAP */
423 crit_exit();
426 * Quick check to see if any work needs to be done, in order
427 * to minimize the overhead of fasttimo processing.
430 if (!igmp_timers_are_running)
431 goto done;
433 igmp_timers_are_running = 0;
434 IN_FIRST_MULTI(step, inm);
435 while (inm != NULL) {
436 if (inm->inm_timer == 0) {
437 /* do nothing */
438 } else if (--inm->inm_timer == 0) {
439 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
440 inm->inm_state = IGMP_IREPORTEDLAST;
441 } else {
442 igmp_timers_are_running = 1;
444 IN_NEXT_MULTI(step, inm);
446 done:
447 callout_reset(&igmp_fasttimo_ch, IGMP_FASTTIMO, igmp_fasttimo, NULL);
450 static void
451 igmp_slowtimo(void *dummy __unused)
453 struct netmsg_base *msg = &igmp_slowtimo_netmsg;
455 KKASSERT(mycpuid == 0);
457 crit_enter();
458 if (msg->lmsg.ms_flags & MSGF_DONE)
459 netisr_sendmsg_oncpu(msg);
460 crit_exit();
463 static void
464 igmp_slowtimo_dispatch(netmsg_t nmsg)
466 struct router_info *rti = Head;
468 ASSERT_NETISR0;
470 crit_enter();
471 netisr_replymsg(&nmsg->base, 0); /* reply ASAP */
472 crit_exit();
474 #ifdef IGMP_DEBUG
475 kprintf("[igmp.c,_slowtimo] -- > entering \n");
476 #endif
477 while (rti) {
478 if (rti->rti_type == IGMP_V1_ROUTER) {
479 rti->rti_time++;
480 if (rti->rti_time >= IGMP_AGE_THRESHOLD) {
481 rti->rti_type = IGMP_V2_ROUTER;
484 rti = rti->rti_next;
486 #ifdef IGMP_DEBUG
487 kprintf("[igmp.c,_slowtimo] -- > exiting \n");
488 #endif
489 callout_reset(&igmp_slowtimo_ch, IGMP_SLOWTIMO, igmp_slowtimo, NULL);
492 static struct route igmprt;
494 static void
495 igmp_sendpkt(struct in_multi *inm, int type, unsigned long addr)
497 struct mbuf *m;
498 struct igmp *igmp;
499 struct ip *ip;
500 struct ip_moptions imo;
502 MGETHDR(m, M_NOWAIT, MT_HEADER);
503 if (m == NULL)
504 return;
506 m->m_pkthdr.rcvif = loif;
507 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
508 MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
509 m->m_data += sizeof(struct ip);
510 m->m_len = IGMP_MINLEN;
511 igmp = mtod(m, struct igmp *);
512 igmp->igmp_type = type;
513 igmp->igmp_code = 0;
514 igmp->igmp_group = inm->inm_addr;
515 igmp->igmp_cksum = 0;
516 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
518 m->m_data -= sizeof(struct ip);
519 m->m_len += sizeof(struct ip);
520 ip = mtod(m, struct ip *);
521 ip->ip_tos = 0;
522 ip->ip_len = htons(sizeof(struct ip) + IGMP_MINLEN);
523 ip->ip_off = 0;
524 ip->ip_p = IPPROTO_IGMP;
525 ip->ip_src.s_addr = INADDR_ANY;
526 ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
528 imo.imo_multicast_ifp = inm->inm_ifp;
529 imo.imo_multicast_ttl = 1;
530 imo.imo_multicast_vif = -1;
532 * Request loopback of the report if we are acting as a multicast
533 * router, so that the process-level routing demon can hear it.
535 imo.imo_multicast_loop = (ip_mrouter != NULL);
538 * XXX
539 * Do we have to worry about reentrancy here? Don't think so.
541 ip_output(m, router_alert, &igmprt, 0, &imo, NULL);
543 ++igmpstat.igps_snd_reports;