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

Cache object: 15a17b685abfc478780705022e9f44a6


[ 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.