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