tcp: Allow per-tcpcb keepintvl and keepcnt
[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.14 2008/06/08 08:38:05 sephe 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 #include <sys/thread2.h>
63
64 #include <machine/stdarg.h>
65
66 #include <net/if.h>
67 #include <net/route.h>
68
69 #include <netinet/in.h>
70 #include <netinet/in_var.h>
71 #include <netinet/in_systm.h>
72 #include <netinet/ip.h>
73 #include <netinet/ip_var.h>
74 #include <netinet/igmp.h>
75 #include <netinet/igmp_var.h>
76
77 static MALLOC_DEFINE(M_IGMP, "igmp", "igmp state");
78
79 static struct router_info *
80                 find_rti (struct ifnet *ifp);
81
82 static struct igmpstat igmpstat;
83
84 SYSCTL_STRUCT(_net_inet_igmp, IGMPCTL_STATS, stats, CTLFLAG_RW,
85         &igmpstat, igmpstat, "IGMP statistics");
86
87 static int igmp_timers_are_running;
88 static u_long igmp_all_hosts_group;
89 static u_long igmp_all_rtrs_group;
90 static struct mbuf *router_alert;
91 static struct router_info *Head;
92
93 static void igmp_sendpkt (struct in_multi *, int, unsigned long);
94
95 void
96 igmp_init(void)
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, MB_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 = NULL;
121 }
122
123 static struct router_info *
124 find_rti(struct ifnet *ifp)
125 {
126         struct router_info *rti = Head;
127
128 #ifdef IGMP_DEBUG
129         kprintf("[igmp.c, _find_rti] --> entering \n");
130 #endif
131         while (rti) {
132                 if (rti->rti_ifp == ifp) {
133 #ifdef IGMP_DEBUG
134                         kprintf("[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         kprintf("[igmp.c, _find_rti] --> created an entry \n");
148 #endif
149         return rti;
150 }
151
152 int
153 igmp_input(struct mbuf **mp, int *offp, int proto)
154 {
155         struct mbuf *m = *mp;
156         int iphlen;
157         struct igmp *igmp;
158         struct ip *ip;
159         int igmplen;
160         struct ifnet *ifp = m->m_pkthdr.rcvif;
161         int minlen;
162         struct in_multi *inm;
163         struct in_ifaddr *ia;
164         struct in_multistep step;
165         struct router_info *rti;
166         int timer; /** timer value in the igmp query header **/
167
168         iphlen = *offp;
169         *mp = NULL;
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(IPPROTO_DONE);
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(IPPROTO_DONE);
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(IPPROTO_DONE);
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(IPPROTO_DONE);
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(IPPROTO_DONE);
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                 ia = IFP_TO_IA(ifp);
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(IPPROTO_DONE);
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 demon 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         *mp = m;
345         rip_input(mp, offp, proto);
346         return(IPPROTO_DONE);
347 }
348
349 void
350 igmp_joingroup(struct in_multi *inm)
351 {
352         crit_enter();
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         crit_exit();
366 }
367
368 void
369 igmp_leavegroup(struct in_multi *inm)
370 {
371         if (inm->inm_state == IGMP_IREPORTEDLAST &&
372             inm->inm_addr.s_addr != igmp_all_hosts_group &&
373             !(inm->inm_ifp->if_flags & IFF_LOOPBACK) &&
374             inm->inm_rti->rti_type != IGMP_V1_ROUTER)
375                 igmp_sendpkt(inm, IGMP_V2_LEAVE_GROUP, igmp_all_rtrs_group);
376 }
377
378 void
379 igmp_fasttimo(void)
380 {
381         struct in_multi *inm;
382         struct in_multistep step;
383
384         /*
385          * Quick check to see if any work needs to be done, in order
386          * to minimize the overhead of fasttimo processing.
387          */
388
389         if (!igmp_timers_are_running)
390                 return;
391
392         crit_enter();
393         igmp_timers_are_running = 0;
394         IN_FIRST_MULTI(step, inm);
395         while (inm != NULL) {
396                 if (inm->inm_timer == 0) {
397                         /* do nothing */
398                 } else if (--inm->inm_timer == 0) {
399                         igmp_sendpkt(inm, inm->inm_rti->rti_type, 0);
400                         inm->inm_state = IGMP_IREPORTEDLAST;
401                 } else {
402                         igmp_timers_are_running = 1;
403                 }
404                 IN_NEXT_MULTI(step, inm);
405         }
406         crit_exit();
407 }
408
409 void
410 igmp_slowtimo(void)
411 {
412         struct router_info *rti =  Head;
413
414         crit_enter();
415 #ifdef IGMP_DEBUG
416         kprintf("[igmp.c,_slowtimo] -- > entering \n");
417 #endif
418         while (rti) {
419             if (rti->rti_type == IGMP_V1_ROUTER) {
420                 rti->rti_time++;
421                 if (rti->rti_time >= IGMP_AGE_THRESHOLD) {
422                         rti->rti_type = IGMP_V2_ROUTER;
423                 }
424             }
425             rti = rti->rti_next;
426         }
427 #ifdef IGMP_DEBUG       
428         kprintf("[igmp.c,_slowtimo] -- > exiting \n");
429 #endif
430         crit_exit();
431 }
432
433 static struct route igmprt;
434
435 static void
436 igmp_sendpkt(struct in_multi *inm, int type, unsigned long addr)
437 {
438         struct mbuf *m;
439         struct igmp *igmp;
440         struct ip *ip;
441         struct ip_moptions imo;
442
443         MGETHDR(m, MB_DONTWAIT, MT_HEADER);
444         if (m == NULL)
445                 return;
446
447         m->m_pkthdr.rcvif = loif;
448         m->m_pkthdr.len = sizeof(struct ip) + IGMP_MINLEN;
449         MH_ALIGN(m, IGMP_MINLEN + sizeof(struct ip));
450         m->m_data += sizeof(struct ip);
451         m->m_len = IGMP_MINLEN;
452         igmp = mtod(m, struct igmp *);
453         igmp->igmp_type   = type;
454         igmp->igmp_code   = 0;
455         igmp->igmp_group  = inm->inm_addr;
456         igmp->igmp_cksum  = 0;
457         igmp->igmp_cksum  = in_cksum(m, IGMP_MINLEN);
458
459         m->m_data -= sizeof(struct ip);
460         m->m_len += sizeof(struct ip);
461         ip = mtod(m, struct ip *);
462         ip->ip_tos = 0;
463         ip->ip_len = sizeof(struct ip) + IGMP_MINLEN;
464         ip->ip_off = 0;
465         ip->ip_p = IPPROTO_IGMP;
466         ip->ip_src.s_addr = INADDR_ANY;
467         ip->ip_dst.s_addr = addr ? addr : igmp->igmp_group.s_addr;
468
469         imo.imo_multicast_ifp = inm->inm_ifp;
470         imo.imo_multicast_ttl = 1;
471         imo.imo_multicast_vif = -1;
472         /*
473          * Request loopback of the report if we are acting as a multicast
474          * router, so that the process-level routing demon can hear it.
475          */
476         imo.imo_multicast_loop = (ip_mrouter != NULL);
477
478         /*
479          * XXX
480          * Do we have to worry about reentrancy here?  Don't think so.
481          */
482         ip_output(m, router_alert, &igmprt, 0, &imo, NULL);
483
484         ++igmpstat.igps_snd_reports;
485 }