Deleted Added
full compact
igmp.c (128019) igmp.c (130333)
1/*
2 * Copyright (c) 1988 Stephen Deering.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Stephen Deering of Stanford University.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
1/*
2 * Copyright (c) 1988 Stephen Deering.
3 * Copyright (c) 1992, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * This code is derived from software contributed to Berkeley by
7 * Stephen Deering of Stanford University.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)igmp.c 8.1 (Berkeley) 7/19/93
34 * $FreeBSD: head/sys/netinet/igmp.c 128019 2004-04-07 20:46:16Z imp $
34 * $FreeBSD: head/sys/netinet/igmp.c 130333 2004-06-11 03:42:37Z rwatson $
35 */
36
37/*
38 * Internet Group Management Protocol (IGMP) routines.
39 *
40 * Written by Steve Deering, Stanford, May 1988.
41 * Modified by Rosen Sharma, Stanford, Aug 1994.
42 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
43 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
44 *
45 * MULTICAST Revision: 3.5.1.4
46 */
47
48#include "opt_mac.h"
49
50#include <sys/param.h>
51#include <sys/systm.h>
52#include <sys/mac.h>
53#include <sys/malloc.h>
54#include <sys/mbuf.h>
55#include <sys/socket.h>
56#include <sys/protosw.h>
57#include <sys/kernel.h>
58#include <sys/sysctl.h>
59
60#include <net/if.h>
61#include <net/route.h>
62
63#include <netinet/in.h>
64#include <netinet/in_var.h>
65#include <netinet/in_systm.h>
66#include <netinet/ip.h>
67#include <netinet/ip_var.h>
68#include <netinet/igmp.h>
69#include <netinet/igmp_var.h>
70
71#include <machine/in_cksum.h>
72
73static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
74
75static struct router_info *find_rti(struct ifnet *ifp);
76static void igmp_sendpkt(struct in_multi *, int, unsigned long);
77
78static struct igmpstat igmpstat;
79
80SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW, &igmpstat,
81 igmpstat, "");
82
35 */
36
37/*
38 * Internet Group Management Protocol (IGMP) routines.
39 *
40 * Written by Steve Deering, Stanford, May 1988.
41 * Modified by Rosen Sharma, Stanford, Aug 1994.
42 * Modified by Bill Fenner, Xerox PARC, Feb 1995.
43 * Modified to fully comply to IGMPv2 by Bill Fenner, Oct 1995.
44 *
45 * MULTICAST Revision: 3.5.1.4
46 */
47
48#include "opt_mac.h"
49
50#include <sys/param.h>
51#include <sys/systm.h>
52#include <sys/mac.h>
53#include <sys/malloc.h>
54#include <sys/mbuf.h>
55#include <sys/socket.h>
56#include <sys/protosw.h>
57#include <sys/kernel.h>
58#include <sys/sysctl.h>
59
60#include <net/if.h>
61#include <net/route.h>
62
63#include <netinet/in.h>
64#include <netinet/in_var.h>
65#include <netinet/in_systm.h>
66#include <netinet/ip.h>
67#include <netinet/ip_var.h>
68#include <netinet/igmp.h>
69#include <netinet/igmp_var.h>
70
71#include <machine/in_cksum.h>
72
73static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
74
75static struct router_info *find_rti(struct ifnet *ifp);
76static void igmp_sendpkt(struct in_multi *, int, unsigned long);
77
78static struct igmpstat igmpstat;
79
80SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW, &igmpstat,
81 igmpstat, "");
82
83/*
84 * igmp_mtx protects all mutable global variables in igmp.c, as well as
85 * the data fields in struct router_info. In general, a router_info
86 * structure will be valid as long as the referencing struct in_multi is
87 * valid, so no reference counting is used. We allow unlocked reads of
88 * router_info data when accessed via an in_multi read-only.
89 */
90static struct mtx igmp_mtx;
83static SLIST_HEAD(, router_info) router_info_head;
84static int igmp_timers_are_running;
91static SLIST_HEAD(, router_info) router_info_head;
92static int igmp_timers_are_running;
93
94/*
95 * XXXRW: can we define these such that these can be made const? In any
96 * case, these shouldn't be changed after igmp_init() and therefore don't
97 * need locking.
98 */
85static u_long igmp_all_hosts_group;
86static u_long igmp_all_rtrs_group;
99static u_long igmp_all_hosts_group;
100static u_long igmp_all_rtrs_group;
101
87static struct mbuf *router_alert;
88static struct route igmprt;
89
90#ifdef IGMP_DEBUG
91#define IGMP_PRINTF(x) printf(x)
92#else
93#define IGMP_PRINTF(x)
94#endif
95
96void
97igmp_init(void)
98{
99 struct ipoption *ra;
100
101 /*
102 * To avoid byte-swapping the same value over and over again.
103 */
104 igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
105 igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP);
106
107 igmp_timers_are_running = 0;
108
109 /*
110 * Construct a Router Alert option to use in outgoing packets
111 */
112 MGET(router_alert, M_DONTWAIT, MT_DATA);
113 ra = mtod(router_alert, struct ipoption *);
114 ra->ipopt_dst.s_addr = 0;
115 ra->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */
116 ra->ipopt_list[1] = 0x04; /* 4 bytes long */
117 ra->ipopt_list[2] = 0x00;
118 ra->ipopt_list[3] = 0x00;
119 router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1];
120
102static struct mbuf *router_alert;
103static struct route igmprt;
104
105#ifdef IGMP_DEBUG
106#define IGMP_PRINTF(x) printf(x)
107#else
108#define IGMP_PRINTF(x)
109#endif
110
111void
112igmp_init(void)
113{
114 struct ipoption *ra;
115
116 /*
117 * To avoid byte-swapping the same value over and over again.
118 */
119 igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
120 igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP);
121
122 igmp_timers_are_running = 0;
123
124 /*
125 * Construct a Router Alert option to use in outgoing packets
126 */
127 MGET(router_alert, M_DONTWAIT, MT_DATA);
128 ra = mtod(router_alert, struct ipoption *);
129 ra->ipopt_dst.s_addr = 0;
130 ra->ipopt_list[0] = IPOPT_RA; /* Router Alert Option */
131 ra->ipopt_list[1] = 0x04; /* 4 bytes long */
132 ra->ipopt_list[2] = 0x00;
133 ra->ipopt_list[3] = 0x00;
134 router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1];
135
136 mtx_init(&igmp_mtx, "igmp_mtx", NULL, MTX_DEF);
121 SLIST_INIT(&router_info_head);
122}
123
124static struct router_info *
125find_rti(struct ifnet *ifp)
126{
127 struct router_info *rti;
128
137 SLIST_INIT(&router_info_head);
138}
139
140static struct router_info *
141find_rti(struct ifnet *ifp)
142{
143 struct router_info *rti;
144
145 mtx_assert(&igmp_mtx, MA_OWNED);
129 IGMP_PRINTF("[igmp.c, _find_rti] --> entering \n");
130 SLIST_FOREACH(rti, &router_info_head, rti_list) {
131 if (rti->rti_ifp == ifp) {
132 IGMP_PRINTF(
133 "[igmp.c, _find_rti] --> found old entry \n");
134 return rti;
135 }
136 }
146 IGMP_PRINTF("[igmp.c, _find_rti] --> entering \n");
147 SLIST_FOREACH(rti, &router_info_head, rti_list) {
148 if (rti->rti_ifp == ifp) {
149 IGMP_PRINTF(
150 "[igmp.c, _find_rti] --> found old entry \n");
151 return rti;
152 }
153 }
154 /*
155 * XXXRW: return value of malloc not checked, despite M_NOWAIT.
156 */
137 MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, M_NOWAIT);
138 rti->rti_ifp = ifp;
139 rti->rti_type = IGMP_V2_ROUTER;
140 rti->rti_time = 0;
141 SLIST_INSERT_HEAD(&router_info_head, rti, rti_list);
142
143 IGMP_PRINTF("[igmp.c, _find_rti] --> created an entry \n");
144 return rti;
145}
146
147void
148igmp_input(register struct mbuf *m, int off)
149{
150 register int iphlen = off;
151 register struct igmp *igmp;
152 register struct ip *ip;
153 register int igmplen;
154 register struct ifnet *ifp = m->m_pkthdr.rcvif;
155 register int minlen;
156 register struct in_multi *inm;
157 register struct in_ifaddr *ia;
158 struct in_multistep step;
159 struct router_info *rti;
160 int timer; /** timer value in the igmp query header **/
161
162 ++igmpstat.igps_rcv_total;
163
164 ip = mtod(m, struct ip *);
165 igmplen = ip->ip_len;
166
167 /*
168 * Validate lengths
169 */
170 if (igmplen < IGMP_MINLEN) {
171 ++igmpstat.igps_rcv_tooshort;
172 m_freem(m);
173 return;
174 }
175 minlen = iphlen + IGMP_MINLEN;
176 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
177 (m = m_pullup(m, minlen)) == 0) {
178 ++igmpstat.igps_rcv_tooshort;
179 return;
180 }
181
182 /*
183 * Validate checksum
184 */
185 m->m_data += iphlen;
186 m->m_len -= iphlen;
187 igmp = mtod(m, struct igmp *);
188 if (in_cksum(m, igmplen)) {
189 ++igmpstat.igps_rcv_badsum;
190 m_freem(m);
191 return;
192 }
193 m->m_data -= iphlen;
194 m->m_len += iphlen;
195
196 ip = mtod(m, struct ip *);
197 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
198 if (timer == 0)
199 timer = 1;
157 MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, M_NOWAIT);
158 rti->rti_ifp = ifp;
159 rti->rti_type = IGMP_V2_ROUTER;
160 rti->rti_time = 0;
161 SLIST_INSERT_HEAD(&router_info_head, rti, rti_list);
162
163 IGMP_PRINTF("[igmp.c, _find_rti] --> created an entry \n");
164 return rti;
165}
166
167void
168igmp_input(register struct mbuf *m, int off)
169{
170 register int iphlen = off;
171 register struct igmp *igmp;
172 register struct ip *ip;
173 register int igmplen;
174 register struct ifnet *ifp = m->m_pkthdr.rcvif;
175 register int minlen;
176 register struct in_multi *inm;
177 register struct in_ifaddr *ia;
178 struct in_multistep step;
179 struct router_info *rti;
180 int timer; /** timer value in the igmp query header **/
181
182 ++igmpstat.igps_rcv_total;
183
184 ip = mtod(m, struct ip *);
185 igmplen = ip->ip_len;
186
187 /*
188 * Validate lengths
189 */
190 if (igmplen < IGMP_MINLEN) {
191 ++igmpstat.igps_rcv_tooshort;
192 m_freem(m);
193 return;
194 }
195 minlen = iphlen + IGMP_MINLEN;
196 if ((m->m_flags & M_EXT || m->m_len < minlen) &&
197 (m = m_pullup(m, minlen)) == 0) {
198 ++igmpstat.igps_rcv_tooshort;
199 return;
200 }
201
202 /*
203 * Validate checksum
204 */
205 m->m_data += iphlen;
206 m->m_len -= iphlen;
207 igmp = mtod(m, struct igmp *);
208 if (in_cksum(m, igmplen)) {
209 ++igmpstat.igps_rcv_badsum;
210 m_freem(m);
211 return;
212 }
213 m->m_data -= iphlen;
214 m->m_len += iphlen;
215
216 ip = mtod(m, struct ip *);
217 timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
218 if (timer == 0)
219 timer = 1;
200 rti = find_rti(ifp);
201
202 /*
203 * In the IGMPv2 specification, there are 3 states and a flag.
204 *
205 * In Non-Member state, we simply don't have a membership record.
206 * In Delaying Member state, our timer is running (inm->inm_timer)
207 * In Idle Member state, our timer is not running (inm->inm_timer==0)
208 *
209 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
210 * we have heard a report from another member, or IGMP_IREPORTEDLAST
211 * if I sent the last report.
212 */
213 switch (igmp->igmp_type) {
214 case IGMP_MEMBERSHIP_QUERY:
215 ++igmpstat.igps_rcv_queries;
216
217 if (ifp->if_flags & IFF_LOOPBACK)
218 break;
219
220 if (igmp->igmp_code == 0) {
221 /*
222 * Old router. Remember that the querier on this
223 * interface is old, and set the timer to the
224 * value in RFC 1112.
225 */
226
220
221 /*
222 * In the IGMPv2 specification, there are 3 states and a flag.
223 *
224 * In Non-Member state, we simply don't have a membership record.
225 * In Delaying Member state, our timer is running (inm->inm_timer)
226 * In Idle Member state, our timer is not running (inm->inm_timer==0)
227 *
228 * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if
229 * we have heard a report from another member, or IGMP_IREPORTEDLAST
230 * if I sent the last report.
231 */
232 switch (igmp->igmp_type) {
233 case IGMP_MEMBERSHIP_QUERY:
234 ++igmpstat.igps_rcv_queries;
235
236 if (ifp->if_flags & IFF_LOOPBACK)
237 break;
238
239 if (igmp->igmp_code == 0) {
240 /*
241 * Old router. Remember that the querier on this
242 * interface is old, and set the timer to the
243 * value in RFC 1112.
244 */
245
246 mtx_lock(&igmp_mtx);
247 rti = find_rti(ifp);
227 rti->rti_type = IGMP_V1_ROUTER;
228 rti->rti_time = 0;
248 rti->rti_type = IGMP_V1_ROUTER;
249 rti->rti_time = 0;
250 mtx_unlock(&igmp_mtx);
229
230 timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
231
232 if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
233 igmp->igmp_group.s_addr != 0) {
234 ++igmpstat.igps_rcv_badqueries;
235 m_freem(m);
236 return;
237 }
238 } else {
239 /*
240 * New router. Simply do the new validity check.
241 */
242
243 if (igmp->igmp_group.s_addr != 0 &&
244 !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
245 ++igmpstat.igps_rcv_badqueries;
246 m_freem(m);
247 return;
248 }
249 }
250
251 /*
252 * - Start the timers in all of our membership records
253 * that the query applies to for the interface on
254 * which the query arrived excl. those that belong
255 * to the "all-hosts" group (224.0.0.1).
256 * - Restart any timer that is already running but has
257 * a value longer than the requested timeout.
258 * - Use the value specified in the query message as
259 * the maximum timeout.
260 */
261 IN_FIRST_MULTI(step, inm);
262 while (inm != NULL) {
263 if (inm->inm_ifp == ifp &&
264 inm->inm_addr.s_addr != igmp_all_hosts_group &&
265 (igmp->igmp_group.s_addr == 0 ||
266 igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
267 if (inm->inm_timer == 0 ||
268 inm->inm_timer > timer) {
269 inm->inm_timer =
270 IGMP_RANDOM_DELAY(timer);
271 igmp_timers_are_running = 1;
272 }
273 }
274 IN_NEXT_MULTI(step, inm);
275 }
276
277 break;
278
279 case IGMP_V1_MEMBERSHIP_REPORT:
280 case IGMP_V2_MEMBERSHIP_REPORT:
281 /*
282 * For fast leave to work, we have to know that we are the
283 * last person to send a report for this group. Reports
284 * can potentially get looped back if we are a multicast
285 * router, so discard reports sourced by me.
286 */
287 IFP_TO_IA(ifp, ia);
288 if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
289 break;
290
291 ++igmpstat.igps_rcv_reports;
292
293 if (ifp->if_flags & IFF_LOOPBACK)
294 break;
295
296 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
297 ++igmpstat.igps_rcv_badreports;
298 m_freem(m);
299 return;
300 }
301
302 /*
303 * KLUDGE: if the IP source address of the report has an
304 * unspecified (i.e., zero) subnet number, as is allowed for
305 * a booting host, replace it with the correct subnet number
306 * so that a process-level multicast routing daemon can
307 * determine which subnet it arrived from. This is necessary
308 * to compensate for the lack of any way for a process to
309 * determine the arrival interface of an incoming packet.
310 */
311 if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0)
312 if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
313
314 /*
315 * If we belong to the group being reported, stop
316 * our timer for that group.
317 */
318 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
319
320 if (inm != NULL) {
321 inm->inm_timer = 0;
322 ++igmpstat.igps_rcv_ourreports;
323
324 inm->inm_state = IGMP_OTHERMEMBER;
325 }
326
327 break;
328 }
329
330 /*
331 * Pass all valid IGMP packets up to any process(es) listening
332 * on a raw IGMP socket.
333 */
334 rip_input(m, off);
335}
336
337void
338igmp_joingroup(struct in_multi *inm)
339{
340 int s = splnet();
341
342 if (inm->inm_addr.s_addr == igmp_all_hosts_group
343 || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
344 inm->inm_timer = 0;
345 inm->inm_state = IGMP_OTHERMEMBER;
346 } else {
251
252 timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
253
254 if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
255 igmp->igmp_group.s_addr != 0) {
256 ++igmpstat.igps_rcv_badqueries;
257 m_freem(m);
258 return;
259 }
260 } else {
261 /*
262 * New router. Simply do the new validity check.
263 */
264
265 if (igmp->igmp_group.s_addr != 0 &&
266 !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
267 ++igmpstat.igps_rcv_badqueries;
268 m_freem(m);
269 return;
270 }
271 }
272
273 /*
274 * - Start the timers in all of our membership records
275 * that the query applies to for the interface on
276 * which the query arrived excl. those that belong
277 * to the "all-hosts" group (224.0.0.1).
278 * - Restart any timer that is already running but has
279 * a value longer than the requested timeout.
280 * - Use the value specified in the query message as
281 * the maximum timeout.
282 */
283 IN_FIRST_MULTI(step, inm);
284 while (inm != NULL) {
285 if (inm->inm_ifp == ifp &&
286 inm->inm_addr.s_addr != igmp_all_hosts_group &&
287 (igmp->igmp_group.s_addr == 0 ||
288 igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
289 if (inm->inm_timer == 0 ||
290 inm->inm_timer > timer) {
291 inm->inm_timer =
292 IGMP_RANDOM_DELAY(timer);
293 igmp_timers_are_running = 1;
294 }
295 }
296 IN_NEXT_MULTI(step, inm);
297 }
298
299 break;
300
301 case IGMP_V1_MEMBERSHIP_REPORT:
302 case IGMP_V2_MEMBERSHIP_REPORT:
303 /*
304 * For fast leave to work, we have to know that we are the
305 * last person to send a report for this group. Reports
306 * can potentially get looped back if we are a multicast
307 * router, so discard reports sourced by me.
308 */
309 IFP_TO_IA(ifp, ia);
310 if (ia && ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
311 break;
312
313 ++igmpstat.igps_rcv_reports;
314
315 if (ifp->if_flags & IFF_LOOPBACK)
316 break;
317
318 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
319 ++igmpstat.igps_rcv_badreports;
320 m_freem(m);
321 return;
322 }
323
324 /*
325 * KLUDGE: if the IP source address of the report has an
326 * unspecified (i.e., zero) subnet number, as is allowed for
327 * a booting host, replace it with the correct subnet number
328 * so that a process-level multicast routing daemon can
329 * determine which subnet it arrived from. This is necessary
330 * to compensate for the lack of any way for a process to
331 * determine the arrival interface of an incoming packet.
332 */
333 if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0)
334 if (ia) ip->ip_src.s_addr = htonl(ia->ia_subnet);
335
336 /*
337 * If we belong to the group being reported, stop
338 * our timer for that group.
339 */
340 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
341
342 if (inm != NULL) {
343 inm->inm_timer = 0;
344 ++igmpstat.igps_rcv_ourreports;
345
346 inm->inm_state = IGMP_OTHERMEMBER;
347 }
348
349 break;
350 }
351
352 /*
353 * Pass all valid IGMP packets up to any process(es) listening
354 * on a raw IGMP socket.
355 */
356 rip_input(m, off);
357}
358
359void
360igmp_joingroup(struct in_multi *inm)
361{
362 int s = splnet();
363
364 if (inm->inm_addr.s_addr == igmp_all_hosts_group
365 || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
366 inm->inm_timer = 0;
367 inm->inm_state = IGMP_OTHERMEMBER;
368 } else {
369 mtx_lock(&igmp_mtx);
347 inm->inm_rti = find_rti(inm->inm_ifp);
370 inm->inm_rti = find_rti(inm->inm_ifp);
371 mtx_unlock(&igmp_mtx);
348 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
349 inm->inm_timer = IGMP_RANDOM_DELAY(
350 IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
351 inm->inm_state = IGMP_IREPORTEDLAST;
352 igmp_timers_are_running = 1;
353 }
354 splx(s);
355}
356
357void
358igmp_leavegroup(struct in_multi *inm)
359{
360
361 if (inm->inm_state == IGMP_IREPORTEDLAST &&
362 inm->inm_addr.s_addr != igmp_all_hosts_group &&
363 !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
364 inm->inm_rti->rti_type != IGMP_V1_ROUTER)
365 igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
366}
367
368void
369igmp_fasttimo(void)
370{
371 register struct in_multi *inm;
372 struct in_multistep step;
373 int s;
374
375 /*
376 * Quick check to see if any work needs to be done, in order
377 * to minimize the overhead of fasttimo processing.
378 */
379
380 if (!igmp_timers_are_running)
381 return;
382
383 s = splnet();
384 igmp_timers_are_running = 0;
385 IN_FIRST_MULTI(step, inm);
386 while (inm != NULL) {
387 if (inm->inm_timer == 0) {
388 /* do nothing */
389 } else if (--inm->inm_timer == 0) {
390 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
391 inm->inm_state = IGMP_IREPORTEDLAST;
392 } else {
393 igmp_timers_are_running = 1;
394 }
395 IN_NEXT_MULTI(step, inm);
396 }
397 splx(s);
398}
399
400void
401igmp_slowtimo(void)
402{
403 int s = splnet();
404 struct router_info *rti;
405
406 IGMP_PRINTF("[igmp.c,_slowtimo] -- > entering \n");
372 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
373 inm->inm_timer = IGMP_RANDOM_DELAY(
374 IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
375 inm->inm_state = IGMP_IREPORTEDLAST;
376 igmp_timers_are_running = 1;
377 }
378 splx(s);
379}
380
381void
382igmp_leavegroup(struct in_multi *inm)
383{
384
385 if (inm->inm_state == IGMP_IREPORTEDLAST &&
386 inm->inm_addr.s_addr != igmp_all_hosts_group &&
387 !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
388 inm->inm_rti->rti_type != IGMP_V1_ROUTER)
389 igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
390}
391
392void
393igmp_fasttimo(void)
394{
395 register struct in_multi *inm;
396 struct in_multistep step;
397 int s;
398
399 /*
400 * Quick check to see if any work needs to be done, in order
401 * to minimize the overhead of fasttimo processing.
402 */
403
404 if (!igmp_timers_are_running)
405 return;
406
407 s = splnet();
408 igmp_timers_are_running = 0;
409 IN_FIRST_MULTI(step, inm);
410 while (inm != NULL) {
411 if (inm->inm_timer == 0) {
412 /* do nothing */
413 } else if (--inm->inm_timer == 0) {
414 igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
415 inm->inm_state = IGMP_IREPORTEDLAST;
416 } else {
417 igmp_timers_are_running = 1;
418 }
419 IN_NEXT_MULTI(step, inm);
420 }
421 splx(s);
422}
423
424void
425igmp_slowtimo(void)
426{
427 int s = splnet();
428 struct router_info *rti;
429
430 IGMP_PRINTF("[igmp.c,_slowtimo] -- > entering \n");
431 mtx_lock(&igmp_mtx);
407 SLIST_FOREACH(rti, &router_info_head, rti_list) {
408 if (rti->rti_type == IGMP_V1_ROUTER) {
409 rti->rti_time++;
410 if (rti->rti_time >= IGMP_AGE_THRESHOLD)
411 rti->rti_type = IGMP_V2_ROUTER;
412 }
413 }
432 SLIST_FOREACH(rti, &router_info_head, rti_list) {
433 if (rti->rti_type == IGMP_V1_ROUTER) {
434 rti->rti_time++;
435 if (rti->rti_time >= IGMP_AGE_THRESHOLD)
436 rti->rti_type = IGMP_V2_ROUTER;
437 }
438 }
439 mtx_unlock(&igmp_mtx);
414 IGMP_PRINTF("[igmp.c,_slowtimo] -- > exiting \n");
415 splx(s);
416}
417
418static void
419igmp_sendpkt(struct in_multi *inm, int type, unsigned long addr)
420{
421 struct mbuf *m;
422 struct igmp *igmp;
423 struct ip *ip;
424 struct ip_moptions imo;
425
426 MGETHDR(m, M_DONTWAIT, MT_HEADER);
427 if (m == NULL)
428 return;
429
430 m->m_pkthdr.rcvif = loif;
431#ifdef MAC
432 mac_create_mbuf_linklayer(inm->inm_ifp, m);
433#endif
434 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
435 MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
436 m->m_data += sizeof(struct ip);
437 m->m_len = IGMP_MINLEN;
438 igmp = mtod(m, struct igmp *);
439 igmp->igmp_type = type;
440 igmp->igmp_code = 0;
441 igmp->igmp_group = inm->inm_addr;
442 igmp->igmp_cksum = 0;
443 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
444
445 m->m_data -= sizeof(struct ip);
446 m->m_len += sizeof(struct ip);
447 ip = mtod(m, struct ip *);
448 ip->ip_tos = 0;
449 ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
450 ip->ip_off = 0;
451 ip->ip_p = IPPROTO_IGMP;
452 ip->ip_src.s_addr = INADDR_ANY;
453 ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
454
455 imo.imo_multicast_ifp = inm->inm_ifp;
456 imo.imo_multicast_ttl = 1;
457 imo.imo_multicast_vif = -1;
458 /*
459 * Request loopback of the report if we are acting as a multicast
460 * router, so that the process-level routing daemon can hear it.
461 */
462 imo.imo_multicast_loop = (ip_mrouter != NULL);
463
464 /*
465 * XXX
466 * Do we have to worry about reentrancy here? Don't think so.
467 */
468 ip_output(m, router_alert, &igmprt, 0, &imo, NULL);
469
470 ++igmpstat.igps_snd_reports;
471}
440 IGMP_PRINTF("[igmp.c,_slowtimo] -- > exiting \n");
441 splx(s);
442}
443
444static void
445igmp_sendpkt(struct in_multi *inm, int type, unsigned long addr)
446{
447 struct mbuf *m;
448 struct igmp *igmp;
449 struct ip *ip;
450 struct ip_moptions imo;
451
452 MGETHDR(m, M_DONTWAIT, MT_HEADER);
453 if (m == NULL)
454 return;
455
456 m->m_pkthdr.rcvif = loif;
457#ifdef MAC
458 mac_create_mbuf_linklayer(inm->inm_ifp, m);
459#endif
460 m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
461 MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
462 m->m_data += sizeof(struct ip);
463 m->m_len = IGMP_MINLEN;
464 igmp = mtod(m, struct igmp *);
465 igmp->igmp_type = type;
466 igmp->igmp_code = 0;
467 igmp->igmp_group = inm->inm_addr;
468 igmp->igmp_cksum = 0;
469 igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
470
471 m->m_data -= sizeof(struct ip);
472 m->m_len += sizeof(struct ip);
473 ip = mtod(m, struct ip *);
474 ip->ip_tos = 0;
475 ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
476 ip->ip_off = 0;
477 ip->ip_p = IPPROTO_IGMP;
478 ip->ip_src.s_addr = INADDR_ANY;
479 ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
480
481 imo.imo_multicast_ifp = inm->inm_ifp;
482 imo.imo_multicast_ttl = 1;
483 imo.imo_multicast_vif = -1;
484 /*
485 * Request loopback of the report if we are acting as a multicast
486 * router, so that the process-level routing daemon can hear it.
487 */
488 imo.imo_multicast_loop = (ip_mrouter != NULL);
489
490 /*
491 * XXX
492 * Do we have to worry about reentrancy here? Don't think so.
493 */
494 ip_output(m, router_alert, &igmprt, 0, &imo, NULL);
495
496 ++igmpstat.igps_snd_reports;
497}