The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/igmp.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: b409f05f09094ca7bd711f3dfa87ead1


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.