igmp.c revision 1.30 1 1.30 thorpej /* $NetBSD: igmp.c,v 1.30 2002/06/30 22:40:33 thorpej Exp $ */
2 1.21 itojun
3 1.21 itojun /*
4 1.21 itojun * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
5 1.21 itojun * All rights reserved.
6 1.29 itojun *
7 1.21 itojun * Redistribution and use in source and binary forms, with or without
8 1.21 itojun * modification, are permitted provided that the following conditions
9 1.21 itojun * are met:
10 1.21 itojun * 1. Redistributions of source code must retain the above copyright
11 1.21 itojun * notice, this list of conditions and the following disclaimer.
12 1.21 itojun * 2. Redistributions in binary form must reproduce the above copyright
13 1.21 itojun * notice, this list of conditions and the following disclaimer in the
14 1.21 itojun * documentation and/or other materials provided with the distribution.
15 1.21 itojun * 3. Neither the name of the project nor the names of its contributors
16 1.21 itojun * may be used to endorse or promote products derived from this software
17 1.21 itojun * without specific prior written permission.
18 1.29 itojun *
19 1.21 itojun * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20 1.21 itojun * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.21 itojun * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.21 itojun * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23 1.21 itojun * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.21 itojun * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.21 itojun * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.21 itojun * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.21 itojun * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.21 itojun * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.21 itojun * SUCH DAMAGE.
30 1.21 itojun */
31 1.8 cgd
32 1.1 hpeyerl /*
33 1.10 mycroft * Internet Group Management Protocol (IGMP) routines.
34 1.1 hpeyerl *
35 1.10 mycroft * Written by Steve Deering, Stanford, May 1988.
36 1.10 mycroft * Modified by Rosen Sharma, Stanford, Aug 1994.
37 1.10 mycroft * Modified by Bill Fenner, Xerox PARC, Feb 1995.
38 1.1 hpeyerl *
39 1.10 mycroft * MULTICAST Revision: 1.3
40 1.1 hpeyerl */
41 1.27 lukem
42 1.27 lukem #include <sys/cdefs.h>
43 1.30 thorpej __KERNEL_RCSID(0, "$NetBSD: igmp.c,v 1.30 2002/06/30 22:40:33 thorpej Exp $");
44 1.17 scottr
45 1.17 scottr #include "opt_mrouting.h"
46 1.1 hpeyerl
47 1.1 hpeyerl #include <sys/param.h>
48 1.1 hpeyerl #include <sys/mbuf.h>
49 1.1 hpeyerl #include <sys/socket.h>
50 1.1 hpeyerl #include <sys/protosw.h>
51 1.15 christos #include <sys/systm.h>
52 1.1 hpeyerl
53 1.1 hpeyerl #include <net/if.h>
54 1.1 hpeyerl #include <net/route.h>
55 1.1 hpeyerl
56 1.1 hpeyerl #include <netinet/in.h>
57 1.1 hpeyerl #include <netinet/in_var.h>
58 1.1 hpeyerl #include <netinet/in_systm.h>
59 1.1 hpeyerl #include <netinet/ip.h>
60 1.1 hpeyerl #include <netinet/ip_var.h>
61 1.1 hpeyerl #include <netinet/igmp.h>
62 1.1 hpeyerl #include <netinet/igmp_var.h>
63 1.1 hpeyerl
64 1.15 christos #include <machine/stdarg.h>
65 1.15 christos
66 1.10 mycroft #define IP_MULTICASTOPTS 0
67 1.10 mycroft
68 1.28 matt struct igmpstat igmpstat;
69 1.25 matt int igmp_timers_are_running;
70 1.10 mycroft static struct router_info *rti_head;
71 1.1 hpeyerl
72 1.10 mycroft void igmp_sendpkt __P((struct in_multi *, int));
73 1.15 christos static int rti_fill __P((struct in_multi *));
74 1.15 christos static struct router_info * rti_find __P((struct ifnet *));
75 1.1 hpeyerl
76 1.1 hpeyerl void
77 1.1 hpeyerl igmp_init()
78 1.1 hpeyerl {
79 1.10 mycroft
80 1.10 mycroft igmp_timers_are_running = 0;
81 1.10 mycroft rti_head = 0;
82 1.10 mycroft }
83 1.10 mycroft
84 1.10 mycroft static int
85 1.10 mycroft rti_fill(inm)
86 1.10 mycroft struct in_multi *inm;
87 1.10 mycroft {
88 1.24 augustss struct router_info *rti;
89 1.10 mycroft
90 1.10 mycroft for (rti = rti_head; rti != 0; rti = rti->rti_next) {
91 1.10 mycroft if (rti->rti_ifp == inm->inm_ifp) {
92 1.10 mycroft inm->inm_rti = rti;
93 1.10 mycroft if (rti->rti_type == IGMP_v1_ROUTER)
94 1.10 mycroft return (IGMP_v1_HOST_MEMBERSHIP_REPORT);
95 1.10 mycroft else
96 1.10 mycroft return (IGMP_v2_HOST_MEMBERSHIP_REPORT);
97 1.10 mycroft }
98 1.10 mycroft }
99 1.10 mycroft
100 1.10 mycroft rti = (struct router_info *)malloc(sizeof(struct router_info),
101 1.10 mycroft M_MRTABLE, M_NOWAIT);
102 1.10 mycroft rti->rti_ifp = inm->inm_ifp;
103 1.10 mycroft rti->rti_type = IGMP_v2_ROUTER;
104 1.10 mycroft rti->rti_next = rti_head;
105 1.10 mycroft rti_head = rti;
106 1.10 mycroft inm->inm_rti = rti;
107 1.10 mycroft return (IGMP_v2_HOST_MEMBERSHIP_REPORT);
108 1.10 mycroft }
109 1.10 mycroft
110 1.10 mycroft static struct router_info *
111 1.10 mycroft rti_find(ifp)
112 1.10 mycroft struct ifnet *ifp;
113 1.10 mycroft {
114 1.24 augustss struct router_info *rti;
115 1.10 mycroft
116 1.10 mycroft for (rti = rti_head; rti != 0; rti = rti->rti_next) {
117 1.10 mycroft if (rti->rti_ifp == ifp)
118 1.10 mycroft return (rti);
119 1.10 mycroft }
120 1.10 mycroft
121 1.10 mycroft rti = (struct router_info *)malloc(sizeof(struct router_info),
122 1.10 mycroft M_MRTABLE, M_NOWAIT);
123 1.10 mycroft rti->rti_ifp = ifp;
124 1.10 mycroft rti->rti_type = IGMP_v2_ROUTER;
125 1.10 mycroft rti->rti_next = rti_head;
126 1.10 mycroft rti_head = rti;
127 1.10 mycroft return (rti);
128 1.1 hpeyerl }
129 1.1 hpeyerl
130 1.1 hpeyerl void
131 1.15 christos #if __STDC__
132 1.15 christos igmp_input(struct mbuf *m, ...)
133 1.15 christos #else
134 1.15 christos igmp_input(m, va_alist)
135 1.15 christos struct mbuf *m;
136 1.15 christos va_dcl
137 1.15 christos #endif
138 1.15 christos {
139 1.21 itojun int proto;
140 1.24 augustss int iphlen;
141 1.24 augustss struct ifnet *ifp = m->m_pkthdr.rcvif;
142 1.24 augustss struct ip *ip = mtod(m, struct ip *);
143 1.24 augustss struct igmp *igmp;
144 1.24 augustss int minlen;
145 1.10 mycroft struct in_multi *inm;
146 1.10 mycroft struct in_multistep step;
147 1.10 mycroft struct router_info *rti;
148 1.24 augustss struct in_ifaddr *ia;
149 1.10 mycroft int timer;
150 1.15 christos va_list ap;
151 1.15 christos
152 1.15 christos va_start(ap, m);
153 1.15 christos iphlen = va_arg(ap, int);
154 1.21 itojun proto = va_arg(ap, int);
155 1.15 christos va_end(ap);
156 1.1 hpeyerl
157 1.1 hpeyerl ++igmpstat.igps_rcv_total;
158 1.1 hpeyerl
159 1.1 hpeyerl /*
160 1.1 hpeyerl * Validate lengths
161 1.1 hpeyerl */
162 1.19 mycroft minlen = iphlen + IGMP_MINLEN;
163 1.19 mycroft if (ip->ip_len < minlen) {
164 1.1 hpeyerl ++igmpstat.igps_rcv_tooshort;
165 1.1 hpeyerl m_freem(m);
166 1.1 hpeyerl return;
167 1.1 hpeyerl }
168 1.25 matt if (((m->m_flags & M_EXT) && (ip->ip_src.s_addr & IN_CLASSA_NET) == 0)
169 1.25 matt || m->m_len < minlen) {
170 1.25 matt if ((m = m_pullup(m, minlen)) == 0) {
171 1.25 matt ++igmpstat.igps_rcv_tooshort;
172 1.25 matt return;
173 1.25 matt }
174 1.25 matt ip = mtod(m, struct ip *);
175 1.1 hpeyerl }
176 1.1 hpeyerl
177 1.1 hpeyerl /*
178 1.1 hpeyerl * Validate checksum
179 1.1 hpeyerl */
180 1.1 hpeyerl m->m_data += iphlen;
181 1.1 hpeyerl m->m_len -= iphlen;
182 1.1 hpeyerl igmp = mtod(m, struct igmp *);
183 1.30 thorpej /* No need to assert alignment here. */
184 1.19 mycroft if (in_cksum(m, ip->ip_len - iphlen)) {
185 1.1 hpeyerl ++igmpstat.igps_rcv_badsum;
186 1.1 hpeyerl m_freem(m);
187 1.1 hpeyerl return;
188 1.1 hpeyerl }
189 1.1 hpeyerl m->m_data -= iphlen;
190 1.1 hpeyerl m->m_len += iphlen;
191 1.1 hpeyerl
192 1.1 hpeyerl switch (igmp->igmp_type) {
193 1.1 hpeyerl
194 1.1 hpeyerl case IGMP_HOST_MEMBERSHIP_QUERY:
195 1.1 hpeyerl ++igmpstat.igps_rcv_queries;
196 1.1 hpeyerl
197 1.6 mycroft if (ifp->if_flags & IFF_LOOPBACK)
198 1.1 hpeyerl break;
199 1.1 hpeyerl
200 1.10 mycroft if (igmp->igmp_code == 0) {
201 1.10 mycroft rti = rti_find(ifp);
202 1.10 mycroft rti->rti_type = IGMP_v1_ROUTER;
203 1.10 mycroft rti->rti_age = 0;
204 1.10 mycroft
205 1.12 mycroft if (ip->ip_dst.s_addr != INADDR_ALLHOSTS_GROUP) {
206 1.10 mycroft ++igmpstat.igps_rcv_badqueries;
207 1.10 mycroft m_freem(m);
208 1.10 mycroft return;
209 1.10 mycroft }
210 1.10 mycroft
211 1.10 mycroft /*
212 1.10 mycroft * Start the timers in all of our membership records
213 1.10 mycroft * for the interface on which the query arrived,
214 1.10 mycroft * except those that are already running and those
215 1.10 mycroft * that belong to a "local" group (224.0.0.X).
216 1.10 mycroft */
217 1.10 mycroft IN_FIRST_MULTI(step, inm);
218 1.10 mycroft while (inm != NULL) {
219 1.10 mycroft if (inm->inm_ifp == ifp &&
220 1.10 mycroft inm->inm_timer == 0 &&
221 1.10 mycroft !IN_LOCAL_GROUP(inm->inm_addr.s_addr)) {
222 1.10 mycroft inm->inm_state = IGMP_DELAYING_MEMBER;
223 1.10 mycroft inm->inm_timer = IGMP_RANDOM_DELAY(
224 1.10 mycroft IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
225 1.10 mycroft igmp_timers_are_running = 1;
226 1.10 mycroft }
227 1.10 mycroft IN_NEXT_MULTI(step, inm);
228 1.10 mycroft }
229 1.10 mycroft } else {
230 1.12 mycroft if (!IN_MULTICAST(ip->ip_dst.s_addr)) {
231 1.10 mycroft ++igmpstat.igps_rcv_badqueries;
232 1.10 mycroft m_freem(m);
233 1.10 mycroft return;
234 1.10 mycroft }
235 1.10 mycroft
236 1.10 mycroft timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
237 1.20 hwr if (timer == 0)
238 1.20 hwr timer =1;
239 1.10 mycroft
240 1.10 mycroft /*
241 1.10 mycroft * Start the timers in all of our membership records
242 1.10 mycroft * for the interface on which the query arrived,
243 1.10 mycroft * except those that are already running and those
244 1.10 mycroft * that belong to a "local" group (224.0.0.X). For
245 1.10 mycroft * timers already running, check if they need to be
246 1.10 mycroft * reset.
247 1.10 mycroft */
248 1.10 mycroft IN_FIRST_MULTI(step, inm);
249 1.10 mycroft while (inm != NULL) {
250 1.10 mycroft if (inm->inm_ifp == ifp &&
251 1.10 mycroft !IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
252 1.12 mycroft (ip->ip_dst.s_addr == INADDR_ALLHOSTS_GROUP ||
253 1.16 mycroft in_hosteq(ip->ip_dst, inm->inm_addr))) {
254 1.10 mycroft switch (inm->inm_state) {
255 1.10 mycroft case IGMP_DELAYING_MEMBER:
256 1.10 mycroft if (inm->inm_timer <= timer)
257 1.10 mycroft break;
258 1.10 mycroft /* FALLTHROUGH */
259 1.10 mycroft case IGMP_IDLE_MEMBER:
260 1.10 mycroft case IGMP_LAZY_MEMBER:
261 1.10 mycroft case IGMP_AWAKENING_MEMBER:
262 1.10 mycroft inm->inm_state =
263 1.10 mycroft IGMP_DELAYING_MEMBER;
264 1.10 mycroft inm->inm_timer =
265 1.10 mycroft IGMP_RANDOM_DELAY(timer);
266 1.10 mycroft igmp_timers_are_running = 1;
267 1.10 mycroft break;
268 1.10 mycroft case IGMP_SLEEPING_MEMBER:
269 1.10 mycroft inm->inm_state =
270 1.10 mycroft IGMP_AWAKENING_MEMBER;
271 1.10 mycroft break;
272 1.10 mycroft }
273 1.10 mycroft }
274 1.10 mycroft IN_NEXT_MULTI(step, inm);
275 1.10 mycroft }
276 1.10 mycroft }
277 1.10 mycroft
278 1.10 mycroft break;
279 1.10 mycroft
280 1.10 mycroft case IGMP_v1_HOST_MEMBERSHIP_REPORT:
281 1.10 mycroft ++igmpstat.igps_rcv_reports;
282 1.10 mycroft
283 1.10 mycroft if (ifp->if_flags & IFF_LOOPBACK)
284 1.10 mycroft break;
285 1.10 mycroft
286 1.12 mycroft if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
287 1.16 mycroft !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
288 1.10 mycroft ++igmpstat.igps_rcv_badreports;
289 1.1 hpeyerl m_freem(m);
290 1.1 hpeyerl return;
291 1.1 hpeyerl }
292 1.1 hpeyerl
293 1.1 hpeyerl /*
294 1.10 mycroft * KLUDGE: if the IP source address of the report has an
295 1.10 mycroft * unspecified (i.e., zero) subnet number, as is allowed for
296 1.10 mycroft * a booting host, replace it with the correct subnet number
297 1.10 mycroft * so that a process-level multicast routing daemon can
298 1.10 mycroft * determine which subnet it arrived from. This is necessary
299 1.10 mycroft * to compensate for the lack of any way for a process to
300 1.10 mycroft * determine the arrival interface of an incoming packet.
301 1.1 hpeyerl */
302 1.12 mycroft if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
303 1.18 tls IFP_TO_IA(ifp, ia); /* XXX */
304 1.10 mycroft if (ia)
305 1.12 mycroft ip->ip_src.s_addr = ia->ia_subnet;
306 1.10 mycroft }
307 1.10 mycroft
308 1.10 mycroft /*
309 1.10 mycroft * If we belong to the group being reported, stop
310 1.10 mycroft * our timer for that group.
311 1.10 mycroft */
312 1.10 mycroft IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
313 1.10 mycroft if (inm != NULL) {
314 1.10 mycroft inm->inm_timer = 0;
315 1.10 mycroft ++igmpstat.igps_rcv_ourreports;
316 1.10 mycroft
317 1.10 mycroft switch (inm->inm_state) {
318 1.10 mycroft case IGMP_IDLE_MEMBER:
319 1.10 mycroft case IGMP_LAZY_MEMBER:
320 1.10 mycroft case IGMP_AWAKENING_MEMBER:
321 1.10 mycroft case IGMP_SLEEPING_MEMBER:
322 1.10 mycroft inm->inm_state = IGMP_SLEEPING_MEMBER;
323 1.10 mycroft break;
324 1.10 mycroft case IGMP_DELAYING_MEMBER:
325 1.10 mycroft if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
326 1.10 mycroft inm->inm_state = IGMP_LAZY_MEMBER;
327 1.10 mycroft else
328 1.10 mycroft inm->inm_state = IGMP_SLEEPING_MEMBER;
329 1.10 mycroft break;
330 1.1 hpeyerl }
331 1.1 hpeyerl }
332 1.1 hpeyerl
333 1.1 hpeyerl break;
334 1.1 hpeyerl
335 1.10 mycroft case IGMP_v2_HOST_MEMBERSHIP_REPORT:
336 1.10 mycroft #ifdef MROUTING
337 1.10 mycroft /*
338 1.10 mycroft * Make sure we don't hear our own membership report. Fast
339 1.10 mycroft * leave requires knowing that we are the only member of a
340 1.10 mycroft * group.
341 1.10 mycroft */
342 1.18 tls IFP_TO_IA(ifp, ia); /* XXX */
343 1.16 mycroft if (ia && in_hosteq(ip->ip_src, ia->ia_addr.sin_addr))
344 1.10 mycroft break;
345 1.10 mycroft #endif
346 1.10 mycroft
347 1.1 hpeyerl ++igmpstat.igps_rcv_reports;
348 1.1 hpeyerl
349 1.6 mycroft if (ifp->if_flags & IFF_LOOPBACK)
350 1.1 hpeyerl break;
351 1.1 hpeyerl
352 1.12 mycroft if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
353 1.16 mycroft !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
354 1.1 hpeyerl ++igmpstat.igps_rcv_badreports;
355 1.1 hpeyerl m_freem(m);
356 1.1 hpeyerl return;
357 1.1 hpeyerl }
358 1.1 hpeyerl
359 1.1 hpeyerl /*
360 1.1 hpeyerl * KLUDGE: if the IP source address of the report has an
361 1.1 hpeyerl * unspecified (i.e., zero) subnet number, as is allowed for
362 1.1 hpeyerl * a booting host, replace it with the correct subnet number
363 1.10 mycroft * so that a process-level multicast routing daemon can
364 1.1 hpeyerl * determine which subnet it arrived from. This is necessary
365 1.1 hpeyerl * to compensate for the lack of any way for a process to
366 1.1 hpeyerl * determine the arrival interface of an incoming packet.
367 1.1 hpeyerl */
368 1.12 mycroft if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
369 1.10 mycroft #ifndef MROUTING
370 1.18 tls IFP_TO_IA(ifp, ia); /* XXX */
371 1.10 mycroft #endif
372 1.10 mycroft if (ia)
373 1.12 mycroft ip->ip_src.s_addr = ia->ia_subnet;
374 1.1 hpeyerl }
375 1.1 hpeyerl
376 1.1 hpeyerl /*
377 1.1 hpeyerl * If we belong to the group being reported, stop
378 1.1 hpeyerl * our timer for that group.
379 1.1 hpeyerl */
380 1.1 hpeyerl IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
381 1.1 hpeyerl if (inm != NULL) {
382 1.1 hpeyerl inm->inm_timer = 0;
383 1.1 hpeyerl ++igmpstat.igps_rcv_ourreports;
384 1.10 mycroft
385 1.10 mycroft switch (inm->inm_state) {
386 1.10 mycroft case IGMP_DELAYING_MEMBER:
387 1.10 mycroft case IGMP_IDLE_MEMBER:
388 1.10 mycroft case IGMP_AWAKENING_MEMBER:
389 1.10 mycroft inm->inm_state = IGMP_LAZY_MEMBER;
390 1.10 mycroft break;
391 1.10 mycroft case IGMP_LAZY_MEMBER:
392 1.10 mycroft case IGMP_SLEEPING_MEMBER:
393 1.10 mycroft break;
394 1.10 mycroft }
395 1.1 hpeyerl }
396 1.1 hpeyerl
397 1.1 hpeyerl break;
398 1.10 mycroft
399 1.1 hpeyerl }
400 1.1 hpeyerl
401 1.1 hpeyerl /*
402 1.1 hpeyerl * Pass all valid IGMP packets up to any process(es) listening
403 1.1 hpeyerl * on a raw IGMP socket.
404 1.1 hpeyerl */
405 1.21 itojun rip_input(m, iphlen, proto);
406 1.21 itojun return;
407 1.1 hpeyerl }
408 1.1 hpeyerl
409 1.1 hpeyerl void
410 1.1 hpeyerl igmp_joingroup(inm)
411 1.1 hpeyerl struct in_multi *inm;
412 1.1 hpeyerl {
413 1.14 mycroft int s = splsoftnet();
414 1.10 mycroft
415 1.10 mycroft inm->inm_state = IGMP_IDLE_MEMBER;
416 1.1 hpeyerl
417 1.10 mycroft if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
418 1.10 mycroft (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0) {
419 1.10 mycroft igmp_sendpkt(inm, rti_fill(inm));
420 1.10 mycroft inm->inm_state = IGMP_DELAYING_MEMBER;
421 1.10 mycroft inm->inm_timer = IGMP_RANDOM_DELAY(
422 1.10 mycroft IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
423 1.10 mycroft igmp_timers_are_running = 1;
424 1.10 mycroft } else
425 1.1 hpeyerl inm->inm_timer = 0;
426 1.1 hpeyerl splx(s);
427 1.1 hpeyerl }
428 1.1 hpeyerl
429 1.1 hpeyerl void
430 1.1 hpeyerl igmp_leavegroup(inm)
431 1.1 hpeyerl struct in_multi *inm;
432 1.1 hpeyerl {
433 1.10 mycroft
434 1.10 mycroft switch (inm->inm_state) {
435 1.10 mycroft case IGMP_DELAYING_MEMBER:
436 1.10 mycroft case IGMP_IDLE_MEMBER:
437 1.10 mycroft if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
438 1.10 mycroft (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0)
439 1.10 mycroft if (inm->inm_rti->rti_type != IGMP_v1_ROUTER)
440 1.10 mycroft igmp_sendpkt(inm, IGMP_HOST_LEAVE_MESSAGE);
441 1.10 mycroft break;
442 1.10 mycroft case IGMP_LAZY_MEMBER:
443 1.10 mycroft case IGMP_AWAKENING_MEMBER:
444 1.10 mycroft case IGMP_SLEEPING_MEMBER:
445 1.10 mycroft break;
446 1.10 mycroft }
447 1.1 hpeyerl }
448 1.1 hpeyerl
449 1.1 hpeyerl void
450 1.1 hpeyerl igmp_fasttimo()
451 1.1 hpeyerl {
452 1.24 augustss struct in_multi *inm;
453 1.1 hpeyerl struct in_multistep step;
454 1.10 mycroft int s;
455 1.1 hpeyerl
456 1.1 hpeyerl /*
457 1.1 hpeyerl * Quick check to see if any work needs to be done, in order
458 1.1 hpeyerl * to minimize the overhead of fasttimo processing.
459 1.1 hpeyerl */
460 1.1 hpeyerl if (!igmp_timers_are_running)
461 1.1 hpeyerl return;
462 1.1 hpeyerl
463 1.14 mycroft s = splsoftnet();
464 1.1 hpeyerl igmp_timers_are_running = 0;
465 1.1 hpeyerl IN_FIRST_MULTI(step, inm);
466 1.1 hpeyerl while (inm != NULL) {
467 1.1 hpeyerl if (inm->inm_timer == 0) {
468 1.1 hpeyerl /* do nothing */
469 1.1 hpeyerl } else if (--inm->inm_timer == 0) {
470 1.10 mycroft if (inm->inm_state == IGMP_DELAYING_MEMBER) {
471 1.10 mycroft if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
472 1.10 mycroft igmp_sendpkt(inm,
473 1.10 mycroft IGMP_v1_HOST_MEMBERSHIP_REPORT);
474 1.10 mycroft else
475 1.10 mycroft igmp_sendpkt(inm,
476 1.10 mycroft IGMP_v2_HOST_MEMBERSHIP_REPORT);
477 1.10 mycroft inm->inm_state = IGMP_IDLE_MEMBER;
478 1.10 mycroft }
479 1.1 hpeyerl } else {
480 1.1 hpeyerl igmp_timers_are_running = 1;
481 1.1 hpeyerl }
482 1.1 hpeyerl IN_NEXT_MULTI(step, inm);
483 1.1 hpeyerl }
484 1.1 hpeyerl splx(s);
485 1.1 hpeyerl }
486 1.1 hpeyerl
487 1.10 mycroft void
488 1.10 mycroft igmp_slowtimo()
489 1.10 mycroft {
490 1.24 augustss struct router_info *rti;
491 1.10 mycroft int s;
492 1.10 mycroft
493 1.14 mycroft s = splsoftnet();
494 1.10 mycroft for (rti = rti_head; rti != 0; rti = rti->rti_next) {
495 1.10 mycroft if (rti->rti_type == IGMP_v1_ROUTER &&
496 1.10 mycroft ++rti->rti_age >= IGMP_AGE_THRESHOLD) {
497 1.10 mycroft rti->rti_type = IGMP_v2_ROUTER;
498 1.10 mycroft }
499 1.10 mycroft }
500 1.10 mycroft splx(s);
501 1.10 mycroft }
502 1.10 mycroft
503 1.10 mycroft void
504 1.10 mycroft igmp_sendpkt(inm, type)
505 1.10 mycroft struct in_multi *inm;
506 1.10 mycroft int type;
507 1.1 hpeyerl {
508 1.10 mycroft struct mbuf *m;
509 1.10 mycroft struct igmp *igmp;
510 1.10 mycroft struct ip *ip;
511 1.10 mycroft struct ip_moptions imo;
512 1.10 mycroft #ifdef MROUTING
513 1.10 mycroft extern struct socket *ip_mrouter;
514 1.10 mycroft #endif /* MROUTING */
515 1.1 hpeyerl
516 1.1 hpeyerl MGETHDR(m, M_DONTWAIT, MT_HEADER);
517 1.1 hpeyerl if (m == NULL)
518 1.1 hpeyerl return;
519 1.1 hpeyerl /*
520 1.1 hpeyerl * Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN
521 1.1 hpeyerl * is smaller than mbuf size returned by MGETHDR.
522 1.1 hpeyerl */
523 1.1 hpeyerl m->m_data += max_linkhdr;
524 1.1 hpeyerl m->m_len = sizeof(struct ip) + IGMP_MINLEN;
525 1.1 hpeyerl m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
526 1.1 hpeyerl
527 1.1 hpeyerl ip = mtod(m, struct ip *);
528 1.1 hpeyerl ip->ip_tos = 0;
529 1.1 hpeyerl ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
530 1.1 hpeyerl ip->ip_off = 0;
531 1.1 hpeyerl ip->ip_p = IPPROTO_IGMP;
532 1.16 mycroft ip->ip_src = zeroin_addr;
533 1.1 hpeyerl ip->ip_dst = inm->inm_addr;
534 1.1 hpeyerl
535 1.7 brezak m->m_data += sizeof(struct ip);
536 1.7 brezak m->m_len -= sizeof(struct ip);
537 1.7 brezak igmp = mtod(m, struct igmp *);
538 1.10 mycroft igmp->igmp_type = type;
539 1.1 hpeyerl igmp->igmp_code = 0;
540 1.1 hpeyerl igmp->igmp_group = inm->inm_addr;
541 1.1 hpeyerl igmp->igmp_cksum = 0;
542 1.1 hpeyerl igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
543 1.7 brezak m->m_data -= sizeof(struct ip);
544 1.7 brezak m->m_len += sizeof(struct ip);
545 1.1 hpeyerl
546 1.10 mycroft imo.imo_multicast_ifp = inm->inm_ifp;
547 1.10 mycroft imo.imo_multicast_ttl = 1;
548 1.10 mycroft #ifdef RSVP_ISI
549 1.10 mycroft imo.imo_multicast_vif = -1;
550 1.10 mycroft #endif
551 1.1 hpeyerl /*
552 1.1 hpeyerl * Request loopback of the report if we are acting as a multicast
553 1.1 hpeyerl * router, so that the process-level routing demon can hear it.
554 1.1 hpeyerl */
555 1.1 hpeyerl #ifdef MROUTING
556 1.10 mycroft imo.imo_multicast_loop = (ip_mrouter != NULL);
557 1.10 mycroft #else
558 1.10 mycroft imo.imo_multicast_loop = 0;
559 1.10 mycroft #endif /* MROUTING */
560 1.10 mycroft
561 1.10 mycroft ip_output(m, (struct mbuf *)0, (struct route *)0, IP_MULTICASTOPTS,
562 1.10 mycroft &imo);
563 1.1 hpeyerl
564 1.1 hpeyerl ++igmpstat.igps_snd_reports;
565 1.1 hpeyerl }
566