]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/netinet/igmp.c
This commit was generated by cvs2svn to compensate for changes in r170964,
[FreeBSD/FreeBSD.git] / sys / netinet / igmp.c
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$
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/malloc.h>
53 #include <sys/mbuf.h>
54 #include <sys/socket.h>
55 #include <sys/protosw.h>
56 #include <sys/kernel.h>
57 #include <sys/sysctl.h>
58
59 #include <net/if.h>
60 #include <net/route.h>
61
62 #include <netinet/in.h>
63 #include <netinet/in_var.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
66 #include <netinet/ip_var.h>
67 #include <netinet/ip_options.h>
68 #include <netinet/igmp.h>
69 #include <netinet/igmp_var.h>
70
71 #include <machine/in_cksum.h>
72
73 #include <security/mac/mac_framework.h>
74
75 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
76
77 static struct router_info       *find_rti(struct ifnet *ifp);
78 static void     igmp_sendpkt(struct in_multi *, int, unsigned long);
79
80 static struct igmpstat igmpstat;
81
82 SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW, &igmpstat,
83     igmpstat, "");
84
85 /*
86  * igmp_mtx protects all mutable global variables in igmp.c, as well as the
87  * data fields in struct router_info.  In general, a router_info structure
88  * will be valid as long as the referencing struct in_multi is valid, so no
89  * reference counting is used.  We allow unlocked reads of router_info data
90  * when accessed via an in_multi read-only.
91  */
92 static struct mtx igmp_mtx;
93 static SLIST_HEAD(, router_info) router_info_head;
94 static int igmp_timers_are_running;
95
96 /*
97  * XXXRW: can we define these such that these can be made const?  In any
98  * case, these shouldn't be changed after igmp_init() and therefore don't
99  * need locking.
100  */
101 static u_long igmp_all_hosts_group;
102 static u_long igmp_all_rtrs_group;
103
104 static struct mbuf *router_alert;
105 static struct route igmprt;
106
107 #ifdef IGMP_DEBUG
108 #define IGMP_PRINTF(x)  printf(x)
109 #else
110 #define IGMP_PRINTF(x)
111 #endif
112
113 void
114 igmp_init(void)
115 {
116         struct ipoption *ra;
117
118         /*
119          * To avoid byte-swapping the same value over and over again.
120          */
121         igmp_all_hosts_group = htonl(INADDR_ALLHOSTS_GROUP);
122         igmp_all_rtrs_group = htonl(INADDR_ALLRTRS_GROUP);
123
124         igmp_timers_are_running = 0;
125
126         /*
127          * Construct a Router Alert option to use in outgoing packets.
128          */
129         MGET(router_alert, M_DONTWAIT, MT_DATA);
130         ra = mtod(router_alert, struct ipoption *);
131         ra->ipopt_dst.s_addr = 0;
132         ra->ipopt_list[0] = IPOPT_RA;   /* Router Alert Option */
133         ra->ipopt_list[1] = 0x04;       /* 4 bytes long */
134         ra->ipopt_list[2] = 0x00;
135         ra->ipopt_list[3] = 0x00;
136         router_alert->m_len = sizeof(ra->ipopt_dst) + ra->ipopt_list[1];
137
138         mtx_init(&igmp_mtx, "igmp_mtx", NULL, MTX_DEF);
139         SLIST_INIT(&router_info_head);
140 }
141
142 static struct router_info *
143 find_rti(struct ifnet *ifp)
144 {
145         struct router_info *rti;
146
147         mtx_assert(&igmp_mtx, MA_OWNED);
148         IGMP_PRINTF("[igmp.c, _find_rti] --> entering \n");
149         SLIST_FOREACH(rti, &router_info_head, rti_list) {
150                 if (rti->rti_ifp == ifp) {
151                         IGMP_PRINTF(
152                             "[igmp.c, _find_rti] --> found old entry \n");
153                         return (rti);
154                 }
155         }
156         MALLOC(rti, struct router_info *, sizeof *rti, M_IGMP, M_NOWAIT);
157         if (rti == NULL) {
158                 IGMP_PRINTF("[igmp.c, _find_rti] --> no memory for entry\n");
159                 return (NULL);
160         }
161         rti->rti_ifp = ifp;
162         rti->rti_type = IGMP_V2_ROUTER;
163         rti->rti_time = 0;
164         SLIST_INSERT_HEAD(&router_info_head, rti, rti_list);
165         IGMP_PRINTF("[igmp.c, _find_rti] --> created an entry \n");
166         return (rti);
167 }
168
169 void
170 igmp_input(register struct mbuf *m, int off)
171 {
172         register int iphlen = off;
173         register struct igmp *igmp;
174         register struct ip *ip;
175         register int igmplen;
176         register struct ifnet *ifp = m->m_pkthdr.rcvif;
177         register int minlen;
178         register struct in_multi *inm;
179         register struct in_ifaddr *ia;
180         struct in_multistep step;
181         struct router_info *rti;
182         int timer; /** timer value in the igmp query header **/
183
184         ++igmpstat.igps_rcv_total;
185
186         ip = mtod(m, struct ip *);
187         igmplen = ip->ip_len;
188
189         /*
190          * Validate lengths.
191          */
192         if (igmplen < IGMP_MINLEN) {
193                 ++igmpstat.igps_rcv_tooshort;
194                 m_freem(m);
195                 return;
196         }
197         minlen = iphlen + IGMP_MINLEN;
198         if ((m->m_flags & M_EXT || m->m_len < minlen) &&
199             (m = m_pullup(m, minlen)) == 0) {
200                 ++igmpstat.igps_rcv_tooshort;
201                 return;
202         }
203
204         /*
205          * Validate checksum.
206          */
207         m->m_data += iphlen;
208         m->m_len -= iphlen;
209         igmp = mtod(m, struct igmp *);
210         if (in_cksum(m, igmplen)) {
211                 ++igmpstat.igps_rcv_badsum;
212                 m_freem(m);
213                 return;
214         }
215         m->m_data -= iphlen;
216         m->m_len += iphlen;
217
218         ip = mtod(m, struct ip *);
219         timer = igmp->igmp_code * PR_FASTHZ / IGMP_TIMER_SCALE;
220         if (timer == 0)
221                 timer = 1;
222
223         /*
224          * In the IGMPv2 specification, there are 3 states and a flag.
225          *
226          * In Non-Member state, we simply don't have a membership record.
227          * In Delaying Member state, our timer is running (inm->inm_timer).
228          * In Idle Member state, our timer is not running (inm->inm_timer==0).
229          *
230          * The flag is inm->inm_state, it is set to IGMP_OTHERMEMBER if we
231          * have heard a report from another member, or IGMP_IREPORTEDLAST if
232          * I sent the last report.
233          */
234         switch (igmp->igmp_type) {
235         case IGMP_MEMBERSHIP_QUERY:
236                 ++igmpstat.igps_rcv_queries;
237
238                 if (ifp->if_flags & IFF_LOOPBACK)
239                         break;
240
241                 if (igmp->igmp_code == 0) {
242                         /*
243                          * Old router.  Remember that the querier on this
244                          * interface is old, and set the timer to the value
245                          * in RFC 1112.
246                          */
247
248                         mtx_lock(&igmp_mtx);
249                         rti = find_rti(ifp);
250                         if (rti == NULL) {
251                                 mtx_unlock(&igmp_mtx);
252                                 m_freem(m);
253                                 return;
254                         }
255                         rti->rti_type = IGMP_V1_ROUTER;
256                         rti->rti_time = 0;
257                         mtx_unlock(&igmp_mtx);
258
259                         timer = IGMP_MAX_HOST_REPORT_DELAY * PR_FASTHZ;
260
261                         if (ip->ip_dst.s_addr != igmp_all_hosts_group ||
262                             igmp->igmp_group.s_addr != 0) {
263                                 ++igmpstat.igps_rcv_badqueries;
264                                 m_freem(m);
265                                 return;
266                         }
267                 } else {
268                         /*
269                          * New router.  Simply do the new validity check.
270                          */
271                         
272                         if (igmp->igmp_group.s_addr != 0 &&
273                             !IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
274                                 ++igmpstat.igps_rcv_badqueries;
275                                 m_freem(m);
276                                 return;
277                         }
278                 }
279
280                 /*
281                  * - Start the timers in all of our membership records that
282                  *   the query applies to for the interface on which the
283                  *   query arrived excl. those that belong to the "all-hosts"
284                  *   group (224.0.0.1).
285                  * - Restart any timer that is already running but has a
286                  *   value longer than the requested timeout.
287                  * - Use the value specified in the query message as the
288                  *   maximum timeout.
289                  */
290                 IN_MULTI_LOCK();
291                 IN_FIRST_MULTI(step, inm);
292                 while (inm != NULL) {
293                         if (inm->inm_ifp == ifp &&
294                             inm->inm_addr.s_addr != igmp_all_hosts_group &&
295                             (igmp->igmp_group.s_addr == 0 ||
296                              igmp->igmp_group.s_addr == inm->inm_addr.s_addr)) {
297                                 if (inm->inm_timer == 0 ||
298                                     inm->inm_timer > timer) {
299                                         inm->inm_timer =
300                                                 IGMP_RANDOM_DELAY(timer);
301                                         igmp_timers_are_running = 1;
302                                 }
303                         }
304                         IN_NEXT_MULTI(step, inm);
305                 }
306                 IN_MULTI_UNLOCK();
307                 break;
308
309         case IGMP_V1_MEMBERSHIP_REPORT:
310         case IGMP_V2_MEMBERSHIP_REPORT:
311                 /*
312                  * For fast leave to work, we have to know that we are the
313                  * last person to send a report for this group.  Reports can
314                  * potentially get looped back if we are a multicast router,
315                  * so discard reports sourced by me.
316                  */
317                 IFP_TO_IA(ifp, ia);
318                 if (ia != NULL &&
319                     ip->ip_src.s_addr == IA_SIN(ia)->sin_addr.s_addr)
320                         break;
321
322                 ++igmpstat.igps_rcv_reports;
323
324                 if (ifp->if_flags & IFF_LOOPBACK)
325                         break;
326
327                 if (!IN_MULTICAST(ntohl(igmp->igmp_group.s_addr))) {
328                         ++igmpstat.igps_rcv_badreports;
329                         m_freem(m);
330                         return;
331                 }
332
333                 /*
334                  * KLUDGE: if the IP source address of the report has an
335                  * unspecified (i.e., zero) subnet number, as is allowed for
336                  * a booting host, replace it with the correct subnet number
337                  * so that a process-level multicast routing daemon can
338                  * determine which subnet it arrived from.  This is necessary
339                  * to compensate for the lack of any way for a process to
340                  * determine the arrival interface of an incoming packet.
341                  */
342                 if ((ntohl(ip->ip_src.s_addr) & IN_CLASSA_NET) == 0) {
343                         if (ia != NULL)
344                                 ip->ip_src.s_addr = htonl(ia->ia_subnet);
345                 }
346
347                 /*
348                  * If we belong to the group being reported, stop our timer
349                  * for that group.
350                  */
351                 IN_MULTI_LOCK();
352                 IN_LOOKUP_MULTI(igmp->igmp_group, ifp, inm);
353                 if (inm != NULL) {
354                         inm->inm_timer = 0;
355                         ++igmpstat.igps_rcv_ourreports;
356                         inm->inm_state = IGMP_OTHERMEMBER;
357                 }
358                 IN_MULTI_UNLOCK();
359                 break;
360         }
361
362         /*
363          * Pass all valid IGMP packets up to any process(es) listening on a
364          * raw IGMP socket.
365          */
366         rip_input(m, off);
367 }
368
369 void
370 igmp_joingroup(struct in_multi *inm)
371 {
372
373         IN_MULTI_LOCK_ASSERT();
374
375         if (inm->inm_addr.s_addr == igmp_all_hosts_group
376             || inm->inm_ifp->if_flags & IFF_LOOPBACK) {
377                 inm->inm_timer = 0;
378                 inm->inm_state = IGMP_OTHERMEMBER;
379         } else {
380                 mtx_lock(&igmp_mtx);
381                 inm->inm_rti = find_rti(inm->inm_ifp);
382                 mtx_unlock(&igmp_mtx);
383                 if (inm->inm_rti != NULL) {
384                         igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
385                         inm->inm_timer = IGMP_RANDOM_DELAY(
386                                         IGMP_MAX_HOST_REPORT_DELAY*PR_FASTHZ);
387                         inm->inm_state = IGMP_IREPORTEDLAST;
388                         igmp_timers_are_running = 1;
389                 }
390                 /* XXX handling of failure case? */
391         }
392 }
393
394 void
395 igmp_leavegroup(struct in_multi *inm)
396 {
397
398         IN_MULTI_LOCK_ASSERT();
399
400         if (inm->inm_state == IGMP_IREPORTEDLAST &&
401             inm->inm_addr.s_addr != igmp_all_hosts_group &&
402             !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
403             inm->inm_rti->rti_type != IGMP_V1_ROUTER)
404                 igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
405 }
406
407 void
408 igmp_fasttimo(void)
409 {
410         register struct in_multi *inm;
411         struct in_multistep step;
412
413         /*
414          * Quick check to see if any work needs to be done, in order to
415          * minimize the overhead of fasttimo processing.
416          */
417
418         if (!igmp_timers_are_running)
419                 return;
420
421         IN_MULTI_LOCK();
422         igmp_timers_are_running = 0;
423         IN_FIRST_MULTI(step, inm);
424         while (inm != NULL) {
425                 if (inm->inm_timer == 0) {
426                         /* do nothing */
427                 } else if (--inm->inm_timer == 0) {
428                         igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
429                         inm->inm_state = IGMP_IREPORTEDLAST;
430                 } else {
431                         igmp_timers_are_running = 1;
432                 }
433                 IN_NEXT_MULTI(step, inm);
434         }
435         IN_MULTI_UNLOCK();
436 }
437
438 void
439 igmp_slowtimo(void)
440 {
441         struct router_info *rti;
442
443         IGMP_PRINTF("[igmp.c,_slowtimo] -- > entering \n");
444         mtx_lock(&igmp_mtx);
445         SLIST_FOREACH(rti, &router_info_head, rti_list) {
446                 if (rti->rti_type == IGMP_V1_ROUTER) {
447                         rti->rti_time++;
448                         if (rti->rti_time >= IGMP_AGE_THRESHOLD)
449                                 rti->rti_type = IGMP_V2_ROUTER;
450                 }
451         }
452         mtx_unlock(&igmp_mtx);
453         IGMP_PRINTF("[igmp.c,_slowtimo] -- > exiting \n");
454 }
455
456 static void
457 igmp_sendpkt(struct in_multi *inm, int type, unsigned long addr)
458 {
459         struct mbuf *m;
460         struct igmp *igmp;
461         struct ip *ip;
462         struct ip_moptions imo;
463
464         IN_MULTI_LOCK_ASSERT();
465
466         MGETHDR(m, M_DONTWAIT, MT_DATA);
467         if (m == NULL)
468                 return;
469
470         m->m_pkthdr.rcvif = loif;
471 #ifdef MAC
472         mac_create_mbuf_linklayer(inm->inm_ifp, m);
473 #endif
474         m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
475         MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
476         m->m_data += sizeof(struct ip);
477         m->m_len = IGMP_MINLEN;
478         igmp = mtod(m, struct igmp *);
479         igmp->igmp_type = type;
480         igmp->igmp_code = 0;
481         igmp->igmp_group = inm->inm_addr;
482         igmp->igmp_cksum = 0;
483         igmp->igmp_cksum = in_cksum(m, IGMP_MINLEN);
484
485         m->m_data -= sizeof(struct ip);
486         m->m_len += sizeof(struct ip);
487         ip = mtod(m, struct ip *);
488         ip->ip_tos = 0;
489         ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
490         ip->ip_off = 0;
491         ip->ip_p = IPPROTO_IGMP;
492         ip->ip_src.s_addr = INADDR_ANY;
493         ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
494
495         imo.imo_multicast_ifp  = inm->inm_ifp;
496         imo.imo_multicast_ttl  = 1;
497         imo.imo_multicast_vif  = -1;
498         /*
499          * Request loopback of the report if we are acting as a multicast
500          * router, so that the process-level routing daemon can hear it.
501          */
502         imo.imo_multicast_loop = (ip_mrouter != NULL);
503
504         /*
505          * XXX: Do we have to worry about reentrancy here?  Don't think so.
506          */
507         ip_output(m, router_alert, &igmprt, 0, &imo, NULL);
508
509         ++igmpstat.igps_snd_reports;
510 }