source: rtems/cpukit/libnetworking/netinet/igmp.c @ dd967330

4.104.114.95
Last change on this file since dd967330 was dd967330, checked in by Ralf Corsepius <ralf.corsepius@…>, on 09/01/08 at 06:36:17

Stop using old-style function definitions.

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