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