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