igmp.c revision 1.44 1 1.44 ad /* $NetBSD: igmp.c,v 1.44 2007/03/12 18:18:35 ad 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.44 ad __KERNEL_RCSID(0, "$NetBSD: igmp.c,v 1.44 2007/03/12 18:18:35 ad 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.44 ad POOL_INIT(igmp_rti_pool, sizeof(struct router_info), 0, 0, 0, "igmppl", NULL,
69 1.44 ad IPL_SOFTNET);
70 1.28 matt struct igmpstat igmpstat;
71 1.25 matt int igmp_timers_are_running;
72 1.33 matt static LIST_HEAD(, router_info) rti_head = LIST_HEAD_INITIALIZER(rti_head);
73 1.1 hpeyerl
74 1.40 perry void igmp_sendpkt(struct in_multi *, int);
75 1.40 perry static int rti_fill(struct in_multi *);
76 1.40 perry static struct router_info *rti_find(struct ifnet *);
77 1.34 itojun static void rti_delete(struct ifnet *);
78 1.1 hpeyerl
79 1.10 mycroft static int
80 1.41 perry rti_fill(struct in_multi *inm)
81 1.10 mycroft {
82 1.24 augustss struct router_info *rti;
83 1.10 mycroft
84 1.43 tls /* this function is called at splsoftnet() */
85 1.33 matt LIST_FOREACH(rti, &rti_head, rti_link) {
86 1.10 mycroft if (rti->rti_ifp == inm->inm_ifp) {
87 1.10 mycroft inm->inm_rti = rti;
88 1.10 mycroft if (rti->rti_type == IGMP_v1_ROUTER)
89 1.10 mycroft return (IGMP_v1_HOST_MEMBERSHIP_REPORT);
90 1.10 mycroft else
91 1.10 mycroft return (IGMP_v2_HOST_MEMBERSHIP_REPORT);
92 1.10 mycroft }
93 1.10 mycroft }
94 1.10 mycroft
95 1.33 matt rti = pool_get(&igmp_rti_pool, PR_NOWAIT);
96 1.33 matt if (rti == NULL)
97 1.33 matt return 0;
98 1.10 mycroft rti->rti_ifp = inm->inm_ifp;
99 1.10 mycroft rti->rti_type = IGMP_v2_ROUTER;
100 1.33 matt LIST_INSERT_HEAD(&rti_head, rti, rti_link);
101 1.10 mycroft inm->inm_rti = rti;
102 1.10 mycroft return (IGMP_v2_HOST_MEMBERSHIP_REPORT);
103 1.10 mycroft }
104 1.10 mycroft
105 1.10 mycroft static struct router_info *
106 1.41 perry rti_find(struct ifnet *ifp)
107 1.10 mycroft {
108 1.24 augustss struct router_info *rti;
109 1.43 tls int s = splsoftnet();
110 1.10 mycroft
111 1.33 matt LIST_FOREACH(rti, &rti_head, rti_link) {
112 1.10 mycroft if (rti->rti_ifp == ifp)
113 1.10 mycroft return (rti);
114 1.10 mycroft }
115 1.10 mycroft
116 1.33 matt rti = pool_get(&igmp_rti_pool, PR_NOWAIT);
117 1.43 tls if (rti == NULL) {
118 1.43 tls splx(s);
119 1.33 matt return NULL;
120 1.43 tls }
121 1.10 mycroft rti->rti_ifp = ifp;
122 1.10 mycroft rti->rti_type = IGMP_v2_ROUTER;
123 1.33 matt LIST_INSERT_HEAD(&rti_head, rti, rti_link);
124 1.43 tls splx(s);
125 1.10 mycroft return (rti);
126 1.1 hpeyerl }
127 1.1 hpeyerl
128 1.34 itojun static void
129 1.43 tls rti_delete(struct ifnet *ifp) /* MUST be called at splsoftnet */
130 1.34 itojun {
131 1.34 itojun struct router_info *rti;
132 1.34 itojun
133 1.34 itojun LIST_FOREACH(rti, &rti_head, rti_link) {
134 1.34 itojun if (rti->rti_ifp == ifp) {
135 1.34 itojun LIST_REMOVE(rti, rti_link);
136 1.34 itojun pool_put(&igmp_rti_pool, rti);
137 1.34 itojun return;
138 1.34 itojun }
139 1.34 itojun }
140 1.34 itojun }
141 1.34 itojun
142 1.1 hpeyerl void
143 1.15 christos igmp_input(struct mbuf *m, ...)
144 1.15 christos {
145 1.21 itojun int proto;
146 1.24 augustss int iphlen;
147 1.24 augustss struct ifnet *ifp = m->m_pkthdr.rcvif;
148 1.24 augustss struct ip *ip = mtod(m, struct ip *);
149 1.24 augustss struct igmp *igmp;
150 1.32 thorpej u_int minlen;
151 1.10 mycroft struct in_multi *inm;
152 1.10 mycroft struct in_multistep step;
153 1.10 mycroft struct router_info *rti;
154 1.24 augustss struct in_ifaddr *ia;
155 1.32 thorpej u_int timer;
156 1.15 christos va_list ap;
157 1.31 itojun u_int16_t ip_len;
158 1.15 christos
159 1.15 christos va_start(ap, m);
160 1.15 christos iphlen = va_arg(ap, int);
161 1.21 itojun proto = va_arg(ap, int);
162 1.15 christos va_end(ap);
163 1.1 hpeyerl
164 1.1 hpeyerl ++igmpstat.igps_rcv_total;
165 1.1 hpeyerl
166 1.1 hpeyerl /*
167 1.1 hpeyerl * Validate lengths
168 1.1 hpeyerl */
169 1.19 mycroft minlen = iphlen + IGMP_MINLEN;
170 1.31 itojun ip_len = ntohs(ip->ip_len);
171 1.31 itojun if (ip_len < minlen) {
172 1.1 hpeyerl ++igmpstat.igps_rcv_tooshort;
173 1.1 hpeyerl m_freem(m);
174 1.1 hpeyerl return;
175 1.1 hpeyerl }
176 1.25 matt if (((m->m_flags & M_EXT) && (ip->ip_src.s_addr & IN_CLASSA_NET) == 0)
177 1.25 matt || m->m_len < minlen) {
178 1.25 matt if ((m = m_pullup(m, minlen)) == 0) {
179 1.25 matt ++igmpstat.igps_rcv_tooshort;
180 1.25 matt return;
181 1.25 matt }
182 1.25 matt ip = mtod(m, struct ip *);
183 1.1 hpeyerl }
184 1.1 hpeyerl
185 1.1 hpeyerl /*
186 1.1 hpeyerl * Validate checksum
187 1.1 hpeyerl */
188 1.1 hpeyerl m->m_data += iphlen;
189 1.1 hpeyerl m->m_len -= iphlen;
190 1.1 hpeyerl igmp = mtod(m, struct igmp *);
191 1.30 thorpej /* No need to assert alignment here. */
192 1.31 itojun if (in_cksum(m, ip_len - iphlen)) {
193 1.1 hpeyerl ++igmpstat.igps_rcv_badsum;
194 1.1 hpeyerl m_freem(m);
195 1.1 hpeyerl return;
196 1.1 hpeyerl }
197 1.1 hpeyerl m->m_data -= iphlen;
198 1.1 hpeyerl m->m_len += iphlen;
199 1.1 hpeyerl
200 1.1 hpeyerl switch (igmp->igmp_type) {
201 1.1 hpeyerl
202 1.1 hpeyerl case IGMP_HOST_MEMBERSHIP_QUERY:
203 1.1 hpeyerl ++igmpstat.igps_rcv_queries;
204 1.1 hpeyerl
205 1.6 mycroft if (ifp->if_flags & IFF_LOOPBACK)
206 1.1 hpeyerl break;
207 1.1 hpeyerl
208 1.10 mycroft if (igmp->igmp_code == 0) {
209 1.10 mycroft rti = rti_find(ifp);
210 1.33 matt if (rti == NULL)
211 1.33 matt break;
212 1.10 mycroft rti->rti_type = IGMP_v1_ROUTER;
213 1.10 mycroft rti->rti_age = 0;
214 1.10 mycroft
215 1.12 mycroft if (ip->ip_dst.s_addr != INADDR_ALLHOSTS_GROUP) {
216 1.10 mycroft ++igmpstat.igps_rcv_badqueries;
217 1.10 mycroft m_freem(m);
218 1.10 mycroft return;
219 1.10 mycroft }
220 1.10 mycroft
221 1.10 mycroft /*
222 1.10 mycroft * Start the timers in all of our membership records
223 1.10 mycroft * for the interface on which the query arrived,
224 1.10 mycroft * except those that are already running and those
225 1.10 mycroft * that belong to a "local" group (224.0.0.X).
226 1.10 mycroft */
227 1.10 mycroft IN_FIRST_MULTI(step, inm);
228 1.10 mycroft while (inm != NULL) {
229 1.10 mycroft if (inm->inm_ifp == ifp &&
230 1.10 mycroft inm->inm_timer == 0 &&
231 1.10 mycroft !IN_LOCAL_GROUP(inm->inm_addr.s_addr)) {
232 1.10 mycroft inm->inm_state = IGMP_DELAYING_MEMBER;
233 1.10 mycroft inm->inm_timer = IGMP_RANDOM_DELAY(
234 1.10 mycroft IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
235 1.10 mycroft igmp_timers_are_running = 1;
236 1.10 mycroft }
237 1.10 mycroft IN_NEXT_MULTI(step, inm);
238 1.10 mycroft }
239 1.10 mycroft } else {
240 1.12 mycroft if (!IN_MULTICAST(ip->ip_dst.s_addr)) {
241 1.10 mycroft ++igmpstat.igps_rcv_badqueries;
242 1.10 mycroft m_freem(m);
243 1.10 mycroft return;
244 1.10 mycroft }
245 1.10 mycroft
246 1.10 mycroft timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
247 1.20 hwr if (timer == 0)
248 1.20 hwr timer =1;
249 1.10 mycroft
250 1.10 mycroft /*
251 1.10 mycroft * Start the timers in all of our membership records
252 1.10 mycroft * for the interface on which the query arrived,
253 1.10 mycroft * except those that are already running and those
254 1.10 mycroft * that belong to a "local" group (224.0.0.X). For
255 1.10 mycroft * timers already running, check if they need to be
256 1.10 mycroft * reset.
257 1.10 mycroft */
258 1.10 mycroft IN_FIRST_MULTI(step, inm);
259 1.10 mycroft while (inm != NULL) {
260 1.10 mycroft if (inm->inm_ifp == ifp &&
261 1.10 mycroft !IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
262 1.12 mycroft (ip->ip_dst.s_addr == INADDR_ALLHOSTS_GROUP ||
263 1.16 mycroft in_hosteq(ip->ip_dst, inm->inm_addr))) {
264 1.10 mycroft switch (inm->inm_state) {
265 1.10 mycroft case IGMP_DELAYING_MEMBER:
266 1.10 mycroft if (inm->inm_timer <= timer)
267 1.10 mycroft break;
268 1.10 mycroft /* FALLTHROUGH */
269 1.10 mycroft case IGMP_IDLE_MEMBER:
270 1.10 mycroft case IGMP_LAZY_MEMBER:
271 1.10 mycroft case IGMP_AWAKENING_MEMBER:
272 1.10 mycroft inm->inm_state =
273 1.10 mycroft IGMP_DELAYING_MEMBER;
274 1.10 mycroft inm->inm_timer =
275 1.10 mycroft IGMP_RANDOM_DELAY(timer);
276 1.10 mycroft igmp_timers_are_running = 1;
277 1.10 mycroft break;
278 1.10 mycroft case IGMP_SLEEPING_MEMBER:
279 1.10 mycroft inm->inm_state =
280 1.10 mycroft IGMP_AWAKENING_MEMBER;
281 1.10 mycroft break;
282 1.10 mycroft }
283 1.10 mycroft }
284 1.10 mycroft IN_NEXT_MULTI(step, inm);
285 1.10 mycroft }
286 1.10 mycroft }
287 1.10 mycroft
288 1.10 mycroft break;
289 1.10 mycroft
290 1.10 mycroft case IGMP_v1_HOST_MEMBERSHIP_REPORT:
291 1.10 mycroft ++igmpstat.igps_rcv_reports;
292 1.10 mycroft
293 1.10 mycroft if (ifp->if_flags & IFF_LOOPBACK)
294 1.10 mycroft break;
295 1.10 mycroft
296 1.12 mycroft if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
297 1.16 mycroft !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
298 1.10 mycroft ++igmpstat.igps_rcv_badreports;
299 1.1 hpeyerl m_freem(m);
300 1.1 hpeyerl return;
301 1.1 hpeyerl }
302 1.1 hpeyerl
303 1.1 hpeyerl /*
304 1.10 mycroft * KLUDGE: if the IP source address of the report has an
305 1.10 mycroft * unspecified (i.e., zero) subnet number, as is allowed for
306 1.10 mycroft * a booting host, replace it with the correct subnet number
307 1.10 mycroft * so that a process-level multicast routing daemon can
308 1.10 mycroft * determine which subnet it arrived from. This is necessary
309 1.10 mycroft * to compensate for the lack of any way for a process to
310 1.10 mycroft * determine the arrival interface of an incoming packet.
311 1.1 hpeyerl */
312 1.12 mycroft if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
313 1.18 tls IFP_TO_IA(ifp, ia); /* XXX */
314 1.10 mycroft if (ia)
315 1.12 mycroft ip->ip_src.s_addr = ia->ia_subnet;
316 1.10 mycroft }
317 1.10 mycroft
318 1.10 mycroft /*
319 1.10 mycroft * If we belong to the group being reported, stop
320 1.10 mycroft * our timer for that group.
321 1.10 mycroft */
322 1.10 mycroft IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
323 1.10 mycroft if (inm != NULL) {
324 1.10 mycroft inm->inm_timer = 0;
325 1.10 mycroft ++igmpstat.igps_rcv_ourreports;
326 1.10 mycroft
327 1.10 mycroft switch (inm->inm_state) {
328 1.10 mycroft case IGMP_IDLE_MEMBER:
329 1.10 mycroft case IGMP_LAZY_MEMBER:
330 1.10 mycroft case IGMP_AWAKENING_MEMBER:
331 1.10 mycroft case IGMP_SLEEPING_MEMBER:
332 1.10 mycroft inm->inm_state = IGMP_SLEEPING_MEMBER;
333 1.10 mycroft break;
334 1.10 mycroft case IGMP_DELAYING_MEMBER:
335 1.10 mycroft if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
336 1.10 mycroft inm->inm_state = IGMP_LAZY_MEMBER;
337 1.10 mycroft else
338 1.10 mycroft inm->inm_state = IGMP_SLEEPING_MEMBER;
339 1.10 mycroft break;
340 1.1 hpeyerl }
341 1.1 hpeyerl }
342 1.1 hpeyerl
343 1.1 hpeyerl break;
344 1.1 hpeyerl
345 1.10 mycroft case IGMP_v2_HOST_MEMBERSHIP_REPORT:
346 1.10 mycroft #ifdef MROUTING
347 1.10 mycroft /*
348 1.10 mycroft * Make sure we don't hear our own membership report. Fast
349 1.10 mycroft * leave requires knowing that we are the only member of a
350 1.10 mycroft * group.
351 1.10 mycroft */
352 1.18 tls IFP_TO_IA(ifp, ia); /* XXX */
353 1.16 mycroft if (ia && in_hosteq(ip->ip_src, ia->ia_addr.sin_addr))
354 1.10 mycroft break;
355 1.10 mycroft #endif
356 1.10 mycroft
357 1.1 hpeyerl ++igmpstat.igps_rcv_reports;
358 1.1 hpeyerl
359 1.6 mycroft if (ifp->if_flags & IFF_LOOPBACK)
360 1.1 hpeyerl break;
361 1.1 hpeyerl
362 1.12 mycroft if (!IN_MULTICAST(igmp->igmp_group.s_addr) ||
363 1.16 mycroft !in_hosteq(igmp->igmp_group, ip->ip_dst)) {
364 1.1 hpeyerl ++igmpstat.igps_rcv_badreports;
365 1.1 hpeyerl m_freem(m);
366 1.1 hpeyerl return;
367 1.1 hpeyerl }
368 1.1 hpeyerl
369 1.1 hpeyerl /*
370 1.1 hpeyerl * KLUDGE: if the IP source address of the report has an
371 1.1 hpeyerl * unspecified (i.e., zero) subnet number, as is allowed for
372 1.1 hpeyerl * a booting host, replace it with the correct subnet number
373 1.10 mycroft * so that a process-level multicast routing daemon can
374 1.1 hpeyerl * determine which subnet it arrived from. This is necessary
375 1.1 hpeyerl * to compensate for the lack of any way for a process to
376 1.1 hpeyerl * determine the arrival interface of an incoming packet.
377 1.1 hpeyerl */
378 1.12 mycroft if ((ip->ip_src.s_addr & IN_CLASSA_NET) == 0) {
379 1.10 mycroft #ifndef MROUTING
380 1.18 tls IFP_TO_IA(ifp, ia); /* XXX */
381 1.10 mycroft #endif
382 1.10 mycroft if (ia)
383 1.12 mycroft ip->ip_src.s_addr = ia->ia_subnet;
384 1.1 hpeyerl }
385 1.1 hpeyerl
386 1.1 hpeyerl /*
387 1.1 hpeyerl * If we belong to the group being reported, stop
388 1.1 hpeyerl * our timer for that group.
389 1.1 hpeyerl */
390 1.1 hpeyerl IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
391 1.1 hpeyerl if (inm != NULL) {
392 1.1 hpeyerl inm->inm_timer = 0;
393 1.1 hpeyerl ++igmpstat.igps_rcv_ourreports;
394 1.10 mycroft
395 1.10 mycroft switch (inm->inm_state) {
396 1.10 mycroft case IGMP_DELAYING_MEMBER:
397 1.10 mycroft case IGMP_IDLE_MEMBER:
398 1.10 mycroft case IGMP_AWAKENING_MEMBER:
399 1.10 mycroft inm->inm_state = IGMP_LAZY_MEMBER;
400 1.10 mycroft break;
401 1.10 mycroft case IGMP_LAZY_MEMBER:
402 1.10 mycroft case IGMP_SLEEPING_MEMBER:
403 1.10 mycroft break;
404 1.10 mycroft }
405 1.1 hpeyerl }
406 1.1 hpeyerl
407 1.1 hpeyerl break;
408 1.10 mycroft
409 1.1 hpeyerl }
410 1.1 hpeyerl
411 1.1 hpeyerl /*
412 1.1 hpeyerl * Pass all valid IGMP packets up to any process(es) listening
413 1.1 hpeyerl * on a raw IGMP socket.
414 1.1 hpeyerl */
415 1.21 itojun rip_input(m, iphlen, proto);
416 1.21 itojun return;
417 1.1 hpeyerl }
418 1.1 hpeyerl
419 1.33 matt int
420 1.41 perry igmp_joingroup(struct in_multi *inm)
421 1.1 hpeyerl {
422 1.33 matt int report_type;
423 1.14 mycroft int s = splsoftnet();
424 1.10 mycroft
425 1.10 mycroft inm->inm_state = IGMP_IDLE_MEMBER;
426 1.1 hpeyerl
427 1.10 mycroft if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
428 1.10 mycroft (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0) {
429 1.33 matt report_type = rti_fill(inm);
430 1.39 christos if (report_type == 0) {
431 1.39 christos splx(s);
432 1.33 matt return ENOMEM;
433 1.39 christos }
434 1.33 matt igmp_sendpkt(inm, report_type);
435 1.10 mycroft inm->inm_state = IGMP_DELAYING_MEMBER;
436 1.10 mycroft inm->inm_timer = IGMP_RANDOM_DELAY(
437 1.10 mycroft IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ);
438 1.10 mycroft igmp_timers_are_running = 1;
439 1.10 mycroft } else
440 1.1 hpeyerl inm->inm_timer = 0;
441 1.1 hpeyerl splx(s);
442 1.33 matt return 0;
443 1.1 hpeyerl }
444 1.1 hpeyerl
445 1.1 hpeyerl void
446 1.41 perry igmp_leavegroup(struct in_multi *inm)
447 1.1 hpeyerl {
448 1.10 mycroft
449 1.10 mycroft switch (inm->inm_state) {
450 1.10 mycroft case IGMP_DELAYING_MEMBER:
451 1.10 mycroft case IGMP_IDLE_MEMBER:
452 1.10 mycroft if (!IN_LOCAL_GROUP(inm->inm_addr.s_addr) &&
453 1.10 mycroft (inm->inm_ifp->if_flags & IFF_LOOPBACK) == 0)
454 1.10 mycroft if (inm->inm_rti->rti_type != IGMP_v1_ROUTER)
455 1.10 mycroft igmp_sendpkt(inm, IGMP_HOST_LEAVE_MESSAGE);
456 1.10 mycroft break;
457 1.10 mycroft case IGMP_LAZY_MEMBER:
458 1.10 mycroft case IGMP_AWAKENING_MEMBER:
459 1.10 mycroft case IGMP_SLEEPING_MEMBER:
460 1.10 mycroft break;
461 1.10 mycroft }
462 1.1 hpeyerl }
463 1.1 hpeyerl
464 1.1 hpeyerl void
465 1.41 perry igmp_fasttimo(void)
466 1.1 hpeyerl {
467 1.24 augustss struct in_multi *inm;
468 1.1 hpeyerl struct in_multistep step;
469 1.10 mycroft int s;
470 1.1 hpeyerl
471 1.1 hpeyerl /*
472 1.1 hpeyerl * Quick check to see if any work needs to be done, in order
473 1.1 hpeyerl * to minimize the overhead of fasttimo processing.
474 1.1 hpeyerl */
475 1.1 hpeyerl if (!igmp_timers_are_running)
476 1.1 hpeyerl return;
477 1.1 hpeyerl
478 1.14 mycroft s = splsoftnet();
479 1.1 hpeyerl igmp_timers_are_running = 0;
480 1.1 hpeyerl IN_FIRST_MULTI(step, inm);
481 1.1 hpeyerl while (inm != NULL) {
482 1.1 hpeyerl if (inm->inm_timer == 0) {
483 1.1 hpeyerl /* do nothing */
484 1.1 hpeyerl } else if (--inm->inm_timer == 0) {
485 1.10 mycroft if (inm->inm_state == IGMP_DELAYING_MEMBER) {
486 1.10 mycroft if (inm->inm_rti->rti_type == IGMP_v1_ROUTER)
487 1.10 mycroft igmp_sendpkt(inm,
488 1.10 mycroft IGMP_v1_HOST_MEMBERSHIP_REPORT);
489 1.10 mycroft else
490 1.10 mycroft igmp_sendpkt(inm,
491 1.10 mycroft IGMP_v2_HOST_MEMBERSHIP_REPORT);
492 1.10 mycroft inm->inm_state = IGMP_IDLE_MEMBER;
493 1.10 mycroft }
494 1.1 hpeyerl } else {
495 1.1 hpeyerl igmp_timers_are_running = 1;
496 1.1 hpeyerl }
497 1.1 hpeyerl IN_NEXT_MULTI(step, inm);
498 1.1 hpeyerl }
499 1.1 hpeyerl splx(s);
500 1.1 hpeyerl }
501 1.1 hpeyerl
502 1.10 mycroft void
503 1.41 perry igmp_slowtimo(void)
504 1.10 mycroft {
505 1.24 augustss struct router_info *rti;
506 1.10 mycroft int s;
507 1.10 mycroft
508 1.14 mycroft s = splsoftnet();
509 1.33 matt LIST_FOREACH(rti, &rti_head, rti_link) {
510 1.10 mycroft if (rti->rti_type == IGMP_v1_ROUTER &&
511 1.10 mycroft ++rti->rti_age >= IGMP_AGE_THRESHOLD) {
512 1.10 mycroft rti->rti_type = IGMP_v2_ROUTER;
513 1.10 mycroft }
514 1.10 mycroft }
515 1.10 mycroft splx(s);
516 1.10 mycroft }
517 1.10 mycroft
518 1.10 mycroft void
519 1.41 perry igmp_sendpkt(struct in_multi *inm, int type)
520 1.1 hpeyerl {
521 1.10 mycroft struct mbuf *m;
522 1.10 mycroft struct igmp *igmp;
523 1.10 mycroft struct ip *ip;
524 1.10 mycroft struct ip_moptions imo;
525 1.10 mycroft #ifdef MROUTING
526 1.10 mycroft extern struct socket *ip_mrouter;
527 1.10 mycroft #endif /* MROUTING */
528 1.1 hpeyerl
529 1.1 hpeyerl MGETHDR(m, M_DONTWAIT, MT_HEADER);
530 1.1 hpeyerl if (m == NULL)
531 1.1 hpeyerl return;
532 1.1 hpeyerl /*
533 1.1 hpeyerl * Assume max_linkhdr + sizeof(struct ip) + IGMP_MINLEN
534 1.1 hpeyerl * is smaller than mbuf size returned by MGETHDR.
535 1.1 hpeyerl */
536 1.1 hpeyerl m->m_data += max_linkhdr;
537 1.1 hpeyerl m->m_len = sizeof(struct ip) + IGMP_MINLEN;
538 1.1 hpeyerl m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
539 1.1 hpeyerl
540 1.1 hpeyerl ip = mtod(m, struct ip *);
541 1.1 hpeyerl ip->ip_tos = 0;
542 1.31 itojun ip->ip_len = htons(sizeof(struct ip) + IGMP_MINLEN);
543 1.31 itojun ip->ip_off = htons(0);
544 1.1 hpeyerl ip->ip_p = IPPROTO_IGMP;
545 1.16 mycroft ip->ip_src = zeroin_addr;
546 1.1 hpeyerl ip->ip_dst = inm->inm_addr;
547 1.1 hpeyerl
548 1.7 brezak m->m_data += sizeof(struct ip);
549 1.7 brezak m->m_len -= sizeof(struct ip);
550 1.7 brezak igmp = mtod(m, struct igmp *);
551 1.10 mycroft igmp->igmp_type = type;
552 1.1 hpeyerl igmp->igmp_code = 0;
553 1.1 hpeyerl igmp->igmp_group = inm->inm_addr;
554 1.1 hpeyerl igmp->igmp_cksum = 0;
555 1.1 hpeyerl igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
556 1.7 brezak m->m_data -= sizeof(struct ip);
557 1.7 brezak m->m_len += sizeof(struct ip);
558 1.1 hpeyerl
559 1.10 mycroft imo.imo_multicast_ifp = inm->inm_ifp;
560 1.10 mycroft imo.imo_multicast_ttl = 1;
561 1.10 mycroft #ifdef RSVP_ISI
562 1.10 mycroft imo.imo_multicast_vif = -1;
563 1.10 mycroft #endif
564 1.1 hpeyerl /*
565 1.1 hpeyerl * Request loopback of the report if we are acting as a multicast
566 1.1 hpeyerl * router, so that the process-level routing demon can hear it.
567 1.1 hpeyerl */
568 1.1 hpeyerl #ifdef MROUTING
569 1.10 mycroft imo.imo_multicast_loop = (ip_mrouter != NULL);
570 1.10 mycroft #else
571 1.10 mycroft imo.imo_multicast_loop = 0;
572 1.10 mycroft #endif /* MROUTING */
573 1.10 mycroft
574 1.36 itojun ip_output(m, (struct mbuf *)NULL, (struct route *)NULL,
575 1.36 itojun IP_MULTICASTOPTS, &imo, (struct socket *)NULL);
576 1.1 hpeyerl
577 1.1 hpeyerl ++igmpstat.igps_snd_reports;
578 1.34 itojun }
579 1.34 itojun
580 1.34 itojun void
581 1.43 tls igmp_purgeif(struct ifnet *ifp) /* MUST be called at splsoftnet() */
582 1.34 itojun {
583 1.43 tls rti_delete(ifp); /* manipulates pools */
584 1.1 hpeyerl }
585