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