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