linux-bk/net/ipv4/igmp.c
<<
>>
Prefs
   1/*
   2 *      Linux NET3:     Internet Group Management Protocol  [IGMP]
   3 *
   4 *      This code implements the IGMP protocol as defined in RFC1112. There has
   5 *      been a further revision of this protocol since which is now supported.
   6 *
   7 *      If you have trouble with this module be careful what gcc you have used,
   8 *      the older version didn't come out right using gcc 2.5.8, the newer one
   9 *      seems to fall out with gcc 2.6.2.
  10 *
  11 *      Version: $Id: igmp.c,v 1.47 2002/02/01 22:01:03 davem Exp $
  12 *
  13 *      Authors:
  14 *              Alan Cox <Alan.Cox@linux.org>
  15 *
  16 *      This program is free software; you can redistribute it and/or
  17 *      modify it under the terms of the GNU General Public License
  18 *      as published by the Free Software Foundation; either version
  19 *      2 of the License, or (at your option) any later version.
  20 *
  21 *      Fixes:
  22 *
  23 *              Alan Cox        :       Added lots of __inline__ to optimise
  24 *                                      the memory usage of all the tiny little
  25 *                                      functions.
  26 *              Alan Cox        :       Dumped the header building experiment.
  27 *              Alan Cox        :       Minor tweaks ready for multicast routing
  28 *                                      and extended IGMP protocol.
  29 *              Alan Cox        :       Removed a load of inline directives. Gcc 2.5.8
  30 *                                      writes utterly bogus code otherwise (sigh)
  31 *                                      fixed IGMP loopback to behave in the manner
  32 *                                      desired by mrouted, fixed the fact it has been
  33 *                                      broken since 1.3.6 and cleaned up a few minor
  34 *                                      points.
  35 *
  36 *              Chih-Jen Chang  :       Tried to revise IGMP to Version 2
  37 *              Tsu-Sheng Tsao          E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu
  38 *                                      The enhancements are mainly based on Steve Deering's 
  39 *                                      ipmulti-3.5 source code.
  40 *              Chih-Jen Chang  :       Added the igmp_get_mrouter_info and
  41 *              Tsu-Sheng Tsao          igmp_set_mrouter_info to keep track of
  42 *                                      the mrouted version on that device.
  43 *              Chih-Jen Chang  :       Added the max_resp_time parameter to
  44 *              Tsu-Sheng Tsao          igmp_heard_query(). Using this parameter
  45 *                                      to identify the multicast router version
  46 *                                      and do what the IGMP version 2 specified.
  47 *              Chih-Jen Chang  :       Added a timer to revert to IGMP V2 router
  48 *              Tsu-Sheng Tsao          if the specified time expired.
  49 *              Alan Cox        :       Stop IGMP from 0.0.0.0 being accepted.
  50 *              Alan Cox        :       Use GFP_ATOMIC in the right places.
  51 *              Christian Daudt :       igmp timer wasn't set for local group
  52 *                                      memberships but was being deleted, 
  53 *                                      which caused a "del_timer() called 
  54 *                                      from %p with timer not initialized\n"
  55 *                                      message (960131).
  56 *              Christian Daudt :       removed del_timer from 
  57 *                                      igmp_timer_expire function (960205).
  58 *             Christian Daudt :       igmp_heard_report now only calls
  59 *                                     igmp_timer_expire if tm->running is
  60 *                                     true (960216).
  61 *              Malcolm Beattie :       ttl comparison wrong in igmp_rcv made
  62 *                                      igmp_heard_query never trigger. Expiry
  63 *                                      miscalculation fixed in igmp_heard_query
  64 *                                      and random() made to return unsigned to
  65 *                                      prevent negative expiry times.
  66 *              Alexey Kuznetsov:       Wrong group leaving behaviour, backport
  67 *                                      fix from pending 2.1.x patches.
  68 *              Alan Cox:               Forget to enable FDDI support earlier.
  69 *              Alexey Kuznetsov:       Fixed leaving groups on device down.
  70 *              Alexey Kuznetsov:       Accordance to igmp-v2-06 draft.
  71 *              David L Stevens:        IGMPv3 support, with help from
  72 *                                      Vinay Kulkarni
  73 */
  74
  75#include <linux/config.h>
  76#include <linux/module.h>
  77#include <asm/uaccess.h>
  78#include <asm/system.h>
  79#include <linux/types.h>
  80#include <linux/kernel.h>
  81#include <linux/jiffies.h>
  82#include <linux/string.h>
  83#include <linux/socket.h>
  84#include <linux/sockios.h>
  85#include <linux/in.h>
  86#include <linux/inet.h>
  87#include <linux/netdevice.h>
  88#include <linux/skbuff.h>
  89#include <linux/inetdevice.h>
  90#include <linux/igmp.h>
  91#include <linux/if_arp.h>
  92#include <linux/rtnetlink.h>
  93#include <linux/times.h>
  94#include <net/ip.h>
  95#include <net/protocol.h>
  96#include <net/route.h>
  97#include <net/sock.h>
  98#include <net/checksum.h>
  99#include <linux/netfilter_ipv4.h>
 100#ifdef CONFIG_IP_MROUTE
 101#include <linux/mroute.h>
 102#endif
 103#ifdef CONFIG_PROC_FS
 104#include <linux/proc_fs.h>
 105#include <linux/seq_file.h>
 106#endif
 107
 108#define IP_MAX_MEMBERSHIPS 20
 109
 110#ifdef CONFIG_IP_MULTICAST
 111/* Parameter names and values are taken from igmp-v2-06 draft */
 112
 113#define IGMP_V1_Router_Present_Timeout          (400*HZ)
 114#define IGMP_V2_Router_Present_Timeout          (400*HZ)
 115#define IGMP_Unsolicited_Report_Interval        (10*HZ)
 116#define IGMP_Query_Response_Interval            (10*HZ)
 117#define IGMP_Unsolicited_Report_Count           2
 118
 119
 120#define IGMP_Initial_Report_Delay               (1)
 121
 122/* IGMP_Initial_Report_Delay is not from IGMP specs!
 123 * IGMP specs require to report membership immediately after
 124 * joining a group, but we delay the first report by a
 125 * small interval. It seems more natural and still does not
 126 * contradict to specs provided this delay is small enough.
 127 */
 128
 129#define IGMP_V1_SEEN(in_dev) ((in_dev)->mr_v1_seen && \
 130                time_before(jiffies, (in_dev)->mr_v1_seen))
 131#define IGMP_V2_SEEN(in_dev) ((in_dev)->mr_v2_seen && \
 132                time_before(jiffies, (in_dev)->mr_v2_seen))
 133
 134static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im);
 135static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr);
 136static void igmpv3_clear_delrec(struct in_device *in_dev);
 137static int sf_setstate(struct ip_mc_list *pmc);
 138static void sf_markstate(struct ip_mc_list *pmc);
 139#endif
 140static void ip_mc_clear_src(struct ip_mc_list *pmc);
 141int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
 142        int sfcount, __u32 *psfsrc, int delta);
 143
 144static void ip_ma_put(struct ip_mc_list *im)
 145{
 146        if (atomic_dec_and_test(&im->refcnt)) {
 147                in_dev_put(im->interface);
 148                kfree(im);
 149        }
 150}
 151
 152#ifdef CONFIG_IP_MULTICAST
 153
 154/*
 155 *      Timer management
 156 */
 157
 158static __inline__ void igmp_stop_timer(struct ip_mc_list *im)
 159{
 160        spin_lock_bh(&im->lock);
 161        if (del_timer(&im->timer))
 162                atomic_dec(&im->refcnt);
 163        im->tm_running=0;
 164        im->reporter = 0;
 165        im->unsolicit_count = 0;
 166        spin_unlock_bh(&im->lock);
 167}
 168
 169/* It must be called with locked im->lock */
 170static void igmp_start_timer(struct ip_mc_list *im, int max_delay)
 171{
 172        int tv=net_random() % max_delay;
 173
 174        im->tm_running=1;
 175        if (!mod_timer(&im->timer, jiffies+tv+2))
 176                atomic_inc(&im->refcnt);
 177}
 178
 179static void igmp_gq_start_timer(struct in_device *in_dev)
 180{
 181        int tv = net_random() % in_dev->mr_maxdelay;
 182
 183        in_dev->mr_gq_running = 1;
 184        if (!mod_timer(&in_dev->mr_gq_timer, jiffies+tv+2))
 185                in_dev_hold(in_dev);
 186}
 187
 188static void igmp_ifc_start_timer(struct in_device *in_dev, int delay)
 189{
 190        int tv = net_random() % delay;
 191
 192        if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2))
 193                in_dev_hold(in_dev);
 194}
 195
 196static void igmp_mod_timer(struct ip_mc_list *im, int max_delay)
 197{
 198        spin_lock_bh(&im->lock);
 199        im->unsolicit_count = 0;
 200        if (del_timer(&im->timer)) {
 201                if ((long)(im->timer.expires-jiffies) < max_delay) {
 202                        add_timer(&im->timer);
 203                        im->tm_running=1;
 204                        spin_unlock_bh(&im->lock);
 205                        return;
 206                }
 207                atomic_dec(&im->refcnt);
 208        }
 209        igmp_start_timer(im, max_delay);
 210        spin_unlock_bh(&im->lock);
 211}
 212
 213
 214/*
 215 *      Send an IGMP report.
 216 */
 217
 218#define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4)
 219
 220
 221static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type,
 222        int gdeleted, int sdeleted)
 223{
 224        switch (type) {
 225        case IGMPV3_MODE_IS_INCLUDE:
 226        case IGMPV3_MODE_IS_EXCLUDE:
 227                if (gdeleted || sdeleted)
 228                        return 0;
 229                return !(pmc->gsquery && !psf->sf_gsresp);
 230        case IGMPV3_CHANGE_TO_INCLUDE:
 231                if (gdeleted || sdeleted)
 232                        return 0;
 233                return psf->sf_count[MCAST_INCLUDE] != 0;
 234        case IGMPV3_CHANGE_TO_EXCLUDE:
 235                if (gdeleted || sdeleted)
 236                        return 0;
 237                if (pmc->sfcount[MCAST_EXCLUDE] == 0 ||
 238                    psf->sf_count[MCAST_INCLUDE])
 239                        return 0;
 240                return pmc->sfcount[MCAST_EXCLUDE] ==
 241                        psf->sf_count[MCAST_EXCLUDE];
 242        case IGMPV3_ALLOW_NEW_SOURCES:
 243                if (gdeleted || !psf->sf_crcount)
 244                        return 0;
 245                return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted;
 246        case IGMPV3_BLOCK_OLD_SOURCES:
 247                if (pmc->sfmode == MCAST_INCLUDE)
 248                        return gdeleted || (psf->sf_crcount && sdeleted);
 249                return psf->sf_crcount && !gdeleted && !sdeleted;
 250        }
 251        return 0;
 252}
 253
 254static int
 255igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted)
 256{
 257        struct ip_sf_list *psf;
 258        int scount = 0;
 259
 260        for (psf=pmc->sources; psf; psf=psf->sf_next) {
 261                if (!is_in(pmc, psf, type, gdeleted, sdeleted))
 262                        continue;
 263                scount++;
 264        }
 265        return scount;
 266}
 267
 268static struct sk_buff *igmpv3_newpack(struct net_device *dev, int size)
 269{
 270        struct sk_buff *skb;
 271        struct rtable *rt;
 272        struct iphdr *pip;
 273        struct igmpv3_report *pig;
 274
 275        skb = alloc_skb(size + dev->hard_header_len + 15, GFP_ATOMIC);
 276        if (skb == NULL)
 277                return 0;
 278
 279        {
 280                struct flowi fl = { .oif = dev->ifindex,
 281                                    .nl_u = { .ip4_u = {
 282                                    .daddr = IGMPV3_ALL_MCR } },
 283                                    .proto = IPPROTO_IGMP };
 284                if (ip_route_output_key(&rt, &fl)) {
 285                        kfree_skb(skb);
 286                        return 0;
 287                }
 288        }
 289        if (rt->rt_src == 0) {
 290                ip_rt_put(rt);
 291                return 0;
 292        }
 293
 294        skb->dst = &rt->u.dst;
 295        skb->dev = dev;
 296
 297        skb_reserve(skb, (dev->hard_header_len+15)&~15);
 298
 299        skb->nh.iph = pip =(struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
 300
 301        pip->version  = 4;
 302        pip->ihl      = (sizeof(struct iphdr)+4)>>2;
 303        pip->tos      = 0xc0;
 304        pip->frag_off = htons(IP_DF);
 305        pip->ttl      = 1;
 306        pip->daddr    = rt->rt_dst;
 307        pip->saddr    = rt->rt_src;
 308        pip->protocol = IPPROTO_IGMP;
 309        pip->tot_len  = 0;      /* filled in later */
 310        ip_select_ident(pip, &rt->u.dst, NULL);
 311        ((u8*)&pip[1])[0] = IPOPT_RA;
 312        ((u8*)&pip[1])[1] = 4;
 313        ((u8*)&pip[1])[2] = 0;
 314        ((u8*)&pip[1])[3] = 0;
 315
 316        pig =(struct igmpv3_report *)skb_put(skb, sizeof(*pig));
 317        skb->h.igmph = (struct igmphdr *)pig;
 318        pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT;
 319        pig->resv1 = 0;
 320        pig->csum = 0;
 321        pig->resv2 = 0;
 322        pig->ngrec = 0;
 323        return skb;
 324}
 325
 326static int igmpv3_sendpack(struct sk_buff *skb)
 327{
 328        struct iphdr *pip = skb->nh.iph;
 329        struct igmphdr *pig = skb->h.igmph;
 330        int iplen, igmplen;
 331
 332        iplen = skb->tail - (unsigned char *)skb->nh.iph;
 333        pip->tot_len = htons(iplen);
 334        ip_send_check(pip);
 335
 336        igmplen = skb->tail - (unsigned char *)skb->h.igmph;
 337        pig->csum = ip_compute_csum((void *)skb->h.igmph, igmplen);
 338
 339        return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, skb->dev,
 340                       dst_output);
 341}
 342
 343static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel)
 344{
 345        return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc,type,gdel,sdel);
 346}
 347
 348static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc,
 349        int type, struct igmpv3_grec **ppgr)
 350{
 351        struct net_device *dev = pmc->interface->dev;
 352        struct igmpv3_report *pih;
 353        struct igmpv3_grec *pgr;
 354
 355        if (!skb)
 356                skb = igmpv3_newpack(dev, dev->mtu);
 357        if (!skb)
 358                return 0;
 359        pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec));
 360        pgr->grec_type = type;
 361        pgr->grec_auxwords = 0;
 362        pgr->grec_nsrcs = 0;
 363        pgr->grec_mca = pmc->multiaddr;
 364        pih = (struct igmpv3_report *)skb->h.igmph;
 365        pih->ngrec = htons(ntohs(pih->ngrec)+1);
 366        *ppgr = pgr;
 367        return skb;
 368}
 369
 370#define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
 371        skb_tailroom(skb)) : 0)
 372
 373static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc,
 374        int type, int gdeleted, int sdeleted)
 375{
 376        struct net_device *dev = pmc->interface->dev;
 377        struct igmpv3_report *pih;
 378        struct igmpv3_grec *pgr = 0;
 379        struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list;
 380        int scount, first, isquery, truncate;
 381
 382        if (pmc->multiaddr == IGMP_ALL_HOSTS)
 383                return skb;
 384
 385        isquery = type == IGMPV3_MODE_IS_INCLUDE ||
 386                  type == IGMPV3_MODE_IS_EXCLUDE;
 387        truncate = type == IGMPV3_MODE_IS_EXCLUDE ||
 388                    type == IGMPV3_CHANGE_TO_EXCLUDE;
 389
 390        psf_list = sdeleted ? &pmc->tomb : &pmc->sources;
 391
 392        if (!*psf_list) {
 393                if (type == IGMPV3_ALLOW_NEW_SOURCES ||
 394                    type == IGMPV3_BLOCK_OLD_SOURCES)
 395                        return skb;
 396                if (pmc->crcount || isquery) {
 397                        /* make sure we have room for group header and at
 398                         * least one source.
 399                         */
 400                        if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)+
 401                            sizeof(__u32)) {
 402                                igmpv3_sendpack(skb);
 403                                skb = 0; /* add_grhead will get a new one */
 404                        }
 405                        skb = add_grhead(skb, pmc, type, &pgr);
 406                }
 407                return skb;
 408        }
 409        pih = skb ? (struct igmpv3_report *)skb->h.igmph : 0;
 410
 411        /* EX and TO_EX get a fresh packet, if needed */
 412        if (truncate) {
 413                if (pih && pih->ngrec &&
 414                    AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
 415                        if (skb)
 416                                igmpv3_sendpack(skb);
 417                        skb = igmpv3_newpack(dev, dev->mtu);
 418                }
 419        }
 420        first = 1;
 421        scount = 0;
 422        psf_prev = 0;
 423        for (psf=*psf_list; psf; psf=psf_next) {
 424                u32 *psrc;
 425
 426                psf_next = psf->sf_next;
 427
 428                if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
 429                        psf_prev = psf;
 430                        continue;
 431                }
 432
 433                /* clear marks on query responses */
 434                if (isquery)
 435                        psf->sf_gsresp = 0;
 436
 437                if (AVAILABLE(skb) < sizeof(u32) +
 438                    first*sizeof(struct igmpv3_grec)) {
 439                        if (truncate && !first)
 440                                break;   /* truncate these */
 441                        if (pgr)
 442                                pgr->grec_nsrcs = htons(scount);
 443                        if (skb)
 444                                igmpv3_sendpack(skb);
 445                        skb = igmpv3_newpack(dev, dev->mtu);
 446                        first = 1;
 447                        scount = 0;
 448                }
 449                if (first) {
 450                        skb = add_grhead(skb, pmc, type, &pgr);
 451                        first = 0;
 452                }
 453                psrc = (u32 *)skb_put(skb, sizeof(u32));
 454                *psrc = psf->sf_inaddr;
 455                scount++;
 456                if ((type == IGMPV3_ALLOW_NEW_SOURCES ||
 457                     type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) {
 458                        psf->sf_crcount--;
 459                        if ((sdeleted || gdeleted) && psf->sf_crcount == 0) {
 460                                if (psf_prev)
 461                                        psf_prev->sf_next = psf->sf_next;
 462                                else
 463                                        *psf_list = psf->sf_next;
 464                                kfree(psf);
 465                                continue;
 466                        }
 467                }
 468                psf_prev = psf;
 469        }
 470        if (pgr)
 471                pgr->grec_nsrcs = htons(scount);
 472
 473        if (isquery)
 474                pmc->gsquery = 0;       /* clear query state on report */
 475        return skb;
 476}
 477
 478static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc)
 479{
 480        struct sk_buff *skb = 0;
 481        int type;
 482
 483        if (!pmc) {
 484                read_lock(&in_dev->lock);
 485                for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
 486                        if (pmc->multiaddr == IGMP_ALL_HOSTS)
 487                                continue;
 488                        spin_lock_bh(&pmc->lock);
 489                        if (pmc->sfcount[MCAST_EXCLUDE])
 490                                type = IGMPV3_MODE_IS_EXCLUDE;
 491                        else
 492                                type = IGMPV3_MODE_IS_INCLUDE;
 493                        skb = add_grec(skb, pmc, type, 0, 0);
 494                        spin_unlock_bh(&pmc->lock);
 495                }
 496                read_unlock(&in_dev->lock);
 497        } else {
 498                spin_lock_bh(&pmc->lock);
 499                if (pmc->sfcount[MCAST_EXCLUDE])
 500                        type = IGMPV3_MODE_IS_EXCLUDE;
 501                else
 502                        type = IGMPV3_MODE_IS_INCLUDE;
 503                skb = add_grec(skb, pmc, type, 0, 0);
 504                spin_unlock_bh(&pmc->lock);
 505        }
 506        if (!skb)
 507                return 0;
 508        return igmpv3_sendpack(skb);
 509}
 510
 511/*
 512 * remove zero-count source records from a source filter list
 513 */
 514static void igmpv3_clear_zeros(struct ip_sf_list **ppsf)
 515{
 516        struct ip_sf_list *psf_prev, *psf_next, *psf;
 517
 518        psf_prev = 0;
 519        for (psf=*ppsf; psf; psf = psf_next) {
 520                psf_next = psf->sf_next;
 521                if (psf->sf_crcount == 0) {
 522                        if (psf_prev)
 523                                psf_prev->sf_next = psf->sf_next;
 524                        else
 525                                *ppsf = psf->sf_next;
 526                        kfree(psf);
 527                } else
 528                        psf_prev = psf;
 529        }
 530}
 531
 532static void igmpv3_send_cr(struct in_device *in_dev)
 533{
 534        struct ip_mc_list *pmc, *pmc_prev, *pmc_next;
 535        struct sk_buff *skb = 0;
 536        int type, dtype;
 537
 538        read_lock(&in_dev->lock);
 539        write_lock_bh(&in_dev->mc_lock);
 540
 541        /* deleted MCA's */
 542        pmc_prev = 0;
 543        for (pmc=in_dev->mc_tomb; pmc; pmc=pmc_next) {
 544                pmc_next = pmc->next;
 545                if (pmc->sfmode == MCAST_INCLUDE) {
 546                        type = IGMPV3_BLOCK_OLD_SOURCES;
 547                        dtype = IGMPV3_BLOCK_OLD_SOURCES;
 548                        skb = add_grec(skb, pmc, type, 1, 0);
 549                        skb = add_grec(skb, pmc, dtype, 1, 1);
 550                }
 551                if (pmc->crcount) {
 552                        pmc->crcount--;
 553                        if (pmc->sfmode == MCAST_EXCLUDE) {
 554                                type = IGMPV3_CHANGE_TO_INCLUDE;
 555                                skb = add_grec(skb, pmc, type, 1, 0);
 556                        }
 557                        if (pmc->crcount == 0) {
 558                                igmpv3_clear_zeros(&pmc->tomb);
 559                                igmpv3_clear_zeros(&pmc->sources);
 560                        }
 561                }
 562                if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) {
 563                        if (pmc_prev)
 564                                pmc_prev->next = pmc_next;
 565                        else
 566                                in_dev->mc_tomb = pmc_next;
 567                        in_dev_put(pmc->interface);
 568                        kfree(pmc);
 569                } else
 570                        pmc_prev = pmc;
 571        }
 572        write_unlock_bh(&in_dev->mc_lock);
 573
 574        /* change recs */
 575        for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
 576                spin_lock_bh(&pmc->lock);
 577                if (pmc->sfcount[MCAST_EXCLUDE]) {
 578                        type = IGMPV3_BLOCK_OLD_SOURCES;
 579                        dtype = IGMPV3_ALLOW_NEW_SOURCES;
 580                } else {
 581                        type = IGMPV3_ALLOW_NEW_SOURCES;
 582                        dtype = IGMPV3_BLOCK_OLD_SOURCES;
 583                }
 584                skb = add_grec(skb, pmc, type, 0, 0);
 585                skb = add_grec(skb, pmc, dtype, 0, 1);  /* deleted sources */
 586
 587                /* filter mode changes */
 588                if (pmc->crcount) {
 589                        pmc->crcount--;
 590                        if (pmc->sfmode == MCAST_EXCLUDE)
 591                                type = IGMPV3_CHANGE_TO_EXCLUDE;
 592                        else
 593                                type = IGMPV3_CHANGE_TO_INCLUDE;
 594                        skb = add_grec(skb, pmc, type, 0, 0);
 595                }
 596                spin_unlock_bh(&pmc->lock);
 597        }
 598        read_unlock(&in_dev->lock);
 599        if (!skb)
 600                return;
 601        (void) igmpv3_sendpack(skb);
 602}
 603
 604static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc,
 605        int type)
 606{
 607        struct sk_buff *skb;
 608        struct iphdr *iph;
 609        struct igmphdr *ih;
 610        struct rtable *rt;
 611        struct net_device *dev = in_dev->dev;
 612        u32     group = pmc ? pmc->multiaddr : 0;
 613        u32     dst;
 614
 615        if (type == IGMPV3_HOST_MEMBERSHIP_REPORT)
 616                return igmpv3_send_report(in_dev, pmc);
 617        else if (type == IGMP_HOST_LEAVE_MESSAGE)
 618                dst = IGMP_ALL_ROUTER;
 619        else
 620                dst = group;
 621
 622        {
 623                struct flowi fl = { .oif = dev->ifindex,
 624                                    .nl_u = { .ip4_u = { .daddr = dst } },
 625                                    .proto = IPPROTO_IGMP };
 626                if (ip_route_output_key(&rt, &fl))
 627                        return -1;
 628        }
 629        if (rt->rt_src == 0) {
 630                ip_rt_put(rt);
 631                return -1;
 632        }
 633
 634        skb=alloc_skb(IGMP_SIZE+LL_RESERVED_SPACE(dev), GFP_ATOMIC);
 635        if (skb == NULL) {
 636                ip_rt_put(rt);
 637                return -1;
 638        }
 639
 640        skb->dst = &rt->u.dst;
 641
 642        skb_reserve(skb, LL_RESERVED_SPACE(dev));
 643
 644        skb->nh.iph = iph = (struct iphdr *)skb_put(skb, sizeof(struct iphdr)+4);
 645
 646        iph->version  = 4;
 647        iph->ihl      = (sizeof(struct iphdr)+4)>>2;
 648        iph->tos      = 0xc0;
 649        iph->frag_off = htons(IP_DF);
 650        iph->ttl      = 1;
 651        iph->daddr    = dst;
 652        iph->saddr    = rt->rt_src;
 653        iph->protocol = IPPROTO_IGMP;
 654        iph->tot_len  = htons(IGMP_SIZE);
 655        ip_select_ident(iph, &rt->u.dst, NULL);
 656        ((u8*)&iph[1])[0] = IPOPT_RA;
 657        ((u8*)&iph[1])[1] = 4;
 658        ((u8*)&iph[1])[2] = 0;
 659        ((u8*)&iph[1])[3] = 0;
 660        ip_send_check(iph);
 661
 662        ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
 663        ih->type=type;
 664        ih->code=0;
 665        ih->csum=0;
 666        ih->group=group;
 667        ih->csum=ip_compute_csum((void *)ih, sizeof(struct igmphdr));
 668
 669        return NF_HOOK(PF_INET, NF_IP_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
 670                       dst_output);
 671}
 672
 673static void igmp_gq_timer_expire(unsigned long data)
 674{
 675        struct in_device *in_dev = (struct in_device *)data;
 676
 677        in_dev->mr_gq_running = 0;
 678        igmpv3_send_report(in_dev, 0);
 679        __in_dev_put(in_dev);
 680}
 681
 682static void igmp_ifc_timer_expire(unsigned long data)
 683{
 684        struct in_device *in_dev = (struct in_device *)data;
 685
 686        igmpv3_send_cr(in_dev);
 687        if (in_dev->mr_ifc_count) {
 688                in_dev->mr_ifc_count--;
 689                igmp_ifc_start_timer(in_dev, IGMP_Unsolicited_Report_Interval);
 690        }
 691        __in_dev_put(in_dev);
 692}
 693
 694static void igmp_ifc_event(struct in_device *in_dev)
 695{
 696        if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev))
 697                return;
 698        in_dev->mr_ifc_count = in_dev->mr_qrv ? in_dev->mr_qrv : 
 699                IGMP_Unsolicited_Report_Count;
 700        igmp_ifc_start_timer(in_dev, 1);
 701}
 702
 703
 704static void igmp_timer_expire(unsigned long data)
 705{
 706        struct ip_mc_list *im=(struct ip_mc_list *)data;
 707        struct in_device *in_dev = im->interface;
 708
 709        spin_lock(&im->lock);
 710        im->tm_running=0;
 711
 712        if (im->unsolicit_count) {
 713                im->unsolicit_count--;
 714                igmp_start_timer(im, IGMP_Unsolicited_Report_Interval);
 715        }
 716        im->reporter = 1;
 717        spin_unlock(&im->lock);
 718
 719        if (IGMP_V1_SEEN(in_dev))
 720                igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT);
 721        else if (IGMP_V2_SEEN(in_dev))
 722                igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT);
 723        else
 724                igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT);
 725
 726        ip_ma_put(im);
 727}
 728
 729static void igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __u32 *srcs)
 730{
 731        struct ip_sf_list *psf;
 732        int i, scount;
 733
 734        scount = 0;
 735        for (psf=pmc->sources; psf; psf=psf->sf_next) {
 736                if (scount == nsrcs)
 737                        break;
 738                for (i=0; i<nsrcs; i++)
 739                        if (srcs[i] == psf->sf_inaddr) {
 740                                psf->sf_gsresp = 1;
 741                                scount++;
 742                                break;
 743                        }
 744        }
 745}
 746
 747static void igmp_heard_report(struct in_device *in_dev, u32 group)
 748{
 749        struct ip_mc_list *im;
 750
 751        /* Timers are only set for non-local groups */
 752
 753        if (group == IGMP_ALL_HOSTS)
 754                return;
 755
 756        read_lock(&in_dev->lock);
 757        for (im=in_dev->mc_list; im!=NULL; im=im->next) {
 758                if (im->multiaddr == group) {
 759                        igmp_stop_timer(im);
 760                        break;
 761                }
 762        }
 763        read_unlock(&in_dev->lock);
 764}
 765
 766static void igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb,
 767        int len)
 768{
 769        struct igmphdr          *ih = skb->h.igmph;
 770        struct igmpv3_query *ih3 = (struct igmpv3_query *)ih;
 771        struct ip_mc_list       *im;
 772        u32                     group = ih->group;
 773        int                     max_delay;
 774        int                     mark = 0;
 775
 776
 777        if (len == 8) {
 778                if (ih->code == 0) {
 779                        /* Alas, old v1 router presents here. */
 780        
 781                        max_delay = IGMP_Query_Response_Interval;
 782                        in_dev->mr_v1_seen = jiffies +
 783                                IGMP_V1_Router_Present_Timeout;
 784                        group = 0;
 785                } else {
 786                        /* v2 router present */
 787                        max_delay = ih->code*(HZ/IGMP_TIMER_SCALE);
 788                        in_dev->mr_v2_seen = jiffies +
 789                                IGMP_V2_Router_Present_Timeout;
 790                }
 791                /* cancel the interface change timer */
 792                in_dev->mr_ifc_count = 0;
 793                if (del_timer(&in_dev->mr_ifc_timer))
 794                        __in_dev_put(in_dev);
 795                /* clear deleted report items */
 796                igmpv3_clear_delrec(in_dev);
 797        } else if (len < 12) {
 798                return; /* ignore bogus packet; freed by caller */
 799        } else { /* v3 */
 800                if (!pskb_may_pull(skb, sizeof(struct igmpv3_query)))
 801                        return;
 802                
 803                ih3 = (struct igmpv3_query *) skb->h.raw;
 804                if (ih3->nsrcs) {
 805                        if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) 
 806                                           + ntohs(ih3->nsrcs)*sizeof(__u32)))
 807                                return;
 808                        ih3 = (struct igmpv3_query *) skb->h.raw;
 809                }
 810
 811                max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE);
 812                if (!max_delay)
 813                        max_delay = 1;  /* can't mod w/ 0 */
 814                in_dev->mr_maxdelay = max_delay;
 815                if (ih3->qrv)
 816                        in_dev->mr_qrv = ih3->qrv;
 817                if (!group) { /* general query */
 818                        if (ih3->nsrcs)
 819                                return; /* no sources allowed */
 820                        igmp_gq_start_timer(in_dev);
 821                        return;
 822                }
 823                /* mark sources to include, if group & source-specific */
 824                mark = ih3->nsrcs != 0;
 825        }
 826
 827        /*
 828         * - Start the timers in all of our membership records
 829         *   that the query applies to for the interface on
 830         *   which the query arrived excl. those that belong
 831         *   to a "local" group (224.0.0.X)
 832         * - For timers already running check if they need to
 833         *   be reset.
 834         * - Use the igmp->igmp_code field as the maximum
 835         *   delay possible
 836         */
 837        read_lock(&in_dev->lock);
 838        for (im=in_dev->mc_list; im!=NULL; im=im->next) {
 839                if (group && group != im->multiaddr)
 840                        continue;
 841                if (im->multiaddr == IGMP_ALL_HOSTS)
 842                        continue;
 843                spin_lock_bh(&im->lock);
 844                if (im->tm_running)
 845                        im->gsquery = im->gsquery && mark;
 846                else
 847                        im->gsquery = mark;
 848                if (im->gsquery)
 849                        igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs);
 850                spin_unlock_bh(&im->lock);
 851                igmp_mod_timer(im, max_delay);
 852        }
 853        read_unlock(&in_dev->lock);
 854}
 855
 856int igmp_rcv(struct sk_buff *skb)
 857{
 858        /* This basically follows the spec line by line -- see RFC1112 */
 859        struct igmphdr *ih;
 860        struct in_device *in_dev = in_dev_get(skb->dev);
 861        int len = skb->len;
 862
 863        if (in_dev==NULL) {
 864                kfree_skb(skb);
 865                return 0;
 866        }
 867
 868        if (!pskb_may_pull(skb, sizeof(struct igmphdr)) || 
 869            (u16)csum_fold(skb_checksum(skb, 0, len, 0))) {
 870                in_dev_put(in_dev);
 871                kfree_skb(skb);
 872                return 0;
 873        }
 874
 875        ih = skb->h.igmph;
 876        switch (ih->type) {
 877        case IGMP_HOST_MEMBERSHIP_QUERY:
 878                igmp_heard_query(in_dev, skb, len);
 879                break;
 880        case IGMP_HOST_MEMBERSHIP_REPORT:
 881        case IGMPV2_HOST_MEMBERSHIP_REPORT:
 882        case IGMPV3_HOST_MEMBERSHIP_REPORT:
 883                /* Is it our report looped back? */
 884                if (((struct rtable*)skb->dst)->fl.iif == 0)
 885                        break;
 886                igmp_heard_report(in_dev, ih->group);
 887                break;
 888        case IGMP_PIM:
 889#ifdef CONFIG_IP_PIMSM_V1
 890                in_dev_put(in_dev);
 891                return pim_rcv_v1(skb);
 892#endif
 893        case IGMP_DVMRP:
 894        case IGMP_TRACE:
 895        case IGMP_HOST_LEAVE_MESSAGE:
 896        case IGMP_MTRACE:
 897        case IGMP_MTRACE_RESP:
 898                break;
 899        default:
 900                NETDEBUG(printk(KERN_DEBUG "New IGMP type=%d, why we do not know about it?\n", ih->type));
 901        }
 902        in_dev_put(in_dev);
 903        kfree_skb(skb);
 904        return 0;
 905}
 906
 907#endif
 908
 909
 910/*
 911 *      Add a filter to a device
 912 */
 913
 914static void ip_mc_filter_add(struct in_device *in_dev, u32 addr)
 915{
 916        char buf[MAX_ADDR_LEN];
 917        struct net_device *dev = in_dev->dev;
 918
 919        /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG.
 920           We will get multicast token leakage, when IFF_MULTICAST
 921           is changed. This check should be done in dev->set_multicast_list
 922           routine. Something sort of:
 923           if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; }
 924           --ANK
 925           */
 926        if (arp_mc_map(addr, buf, dev, 0) == 0)
 927                dev_mc_add(dev,buf,dev->addr_len,0);
 928}
 929
 930/*
 931 *      Remove a filter from a device
 932 */
 933
 934static void ip_mc_filter_del(struct in_device *in_dev, u32 addr)
 935{
 936        char buf[MAX_ADDR_LEN];
 937        struct net_device *dev = in_dev->dev;
 938
 939        if (arp_mc_map(addr, buf, dev, 0) == 0)
 940                dev_mc_delete(dev,buf,dev->addr_len,0);
 941}
 942
 943#ifdef CONFIG_IP_MULTICAST
 944/*
 945 * deleted ip_mc_list manipulation
 946 */
 947static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im)
 948{
 949        struct ip_mc_list *pmc;
 950
 951        /* this is an "ip_mc_list" for convenience; only the fields below
 952         * are actually used. In particular, the refcnt and users are not
 953         * used for management of the delete list. Using the same structure
 954         * for deleted items allows change reports to use common code with
 955         * non-deleted or query-response MCA's.
 956         */
 957        pmc = (struct ip_mc_list *)kmalloc(sizeof(*pmc), GFP_KERNEL);
 958        if (!pmc)
 959                return;
 960        memset(pmc, 0, sizeof(*pmc));
 961        spin_lock_bh(&im->lock);
 962        pmc->interface = im->interface;
 963        in_dev_hold(in_dev);
 964        pmc->multiaddr = im->multiaddr;
 965        pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
 966                IGMP_Unsolicited_Report_Count;
 967        pmc->sfmode = im->sfmode;
 968        if (pmc->sfmode == MCAST_INCLUDE) {
 969                struct ip_sf_list *psf;
 970
 971                pmc->tomb = im->tomb;
 972                pmc->sources = im->sources;
 973                im->tomb = im->sources = 0;
 974                for (psf=pmc->sources; psf; psf=psf->sf_next)
 975                        psf->sf_crcount = pmc->crcount;
 976        }
 977        spin_unlock_bh(&im->lock);
 978
 979        write_lock_bh(&in_dev->mc_lock);
 980        pmc->next = in_dev->mc_tomb;
 981        in_dev->mc_tomb = pmc;
 982        write_unlock_bh(&in_dev->mc_lock);
 983}
 984
 985static void igmpv3_del_delrec(struct in_device *in_dev, __u32 multiaddr)
 986{
 987        struct ip_mc_list *pmc, *pmc_prev;
 988        struct ip_sf_list *psf, *psf_next;
 989
 990        write_lock_bh(&in_dev->mc_lock);
 991        pmc_prev = 0;
 992        for (pmc=in_dev->mc_tomb; pmc; pmc=pmc->next) {
 993                if (pmc->multiaddr == multiaddr)
 994                        break;
 995                pmc_prev = pmc;
 996        }
 997        if (pmc) {
 998                if (pmc_prev)
 999                        pmc_prev->next = pmc->next;
1000                else
1001                        in_dev->mc_tomb = pmc->next;
1002        }
1003        write_unlock_bh(&in_dev->mc_lock);
1004        if (pmc) {
1005                for (psf=pmc->tomb; psf; psf=psf_next) {
1006                        psf_next = psf->sf_next;
1007                        kfree(psf);
1008                }
1009                in_dev_put(pmc->interface);
1010                kfree(pmc);
1011        }
1012}
1013
1014static void igmpv3_clear_delrec(struct in_device *in_dev)
1015{
1016        struct ip_mc_list *pmc, *nextpmc;
1017
1018        write_lock_bh(&in_dev->mc_lock);
1019        pmc = in_dev->mc_tomb;
1020        in_dev->mc_tomb = 0;
1021        write_unlock_bh(&in_dev->mc_lock);
1022
1023        for (; pmc; pmc = nextpmc) {
1024                nextpmc = pmc->next;
1025                ip_mc_clear_src(pmc);
1026                in_dev_put(pmc->interface);
1027                kfree(pmc);
1028        }
1029        /* clear dead sources, too */
1030        read_lock(&in_dev->lock);
1031        for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1032                struct ip_sf_list *psf, *psf_next;
1033
1034                spin_lock_bh(&pmc->lock);
1035                psf = pmc->tomb;
1036                pmc->tomb = 0;
1037                spin_unlock_bh(&pmc->lock);
1038                for (; psf; psf=psf_next) {
1039                        psf_next = psf->sf_next;
1040                        kfree(psf);
1041                }
1042        }
1043        read_unlock(&in_dev->lock);
1044}
1045#endif
1046
1047static void igmp_group_dropped(struct ip_mc_list *im)
1048{
1049        struct in_device *in_dev = im->interface;
1050#ifdef CONFIG_IP_MULTICAST
1051        int reporter;
1052#endif
1053
1054        if (im->loaded) {
1055                im->loaded = 0;
1056                ip_mc_filter_del(in_dev, im->multiaddr);
1057        }
1058
1059#ifdef CONFIG_IP_MULTICAST
1060        if (im->multiaddr == IGMP_ALL_HOSTS)
1061                return;
1062
1063        reporter = im->reporter;
1064        igmp_stop_timer(im);
1065
1066        if (in_dev->dev->flags & IFF_UP) {
1067                if (IGMP_V1_SEEN(in_dev))
1068                        goto done;
1069                if (IGMP_V2_SEEN(in_dev)) {
1070                        if (reporter)
1071                                igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE);
1072                        goto done;
1073                }
1074                /* IGMPv3 */
1075                igmpv3_add_delrec(in_dev, im);
1076
1077                igmp_ifc_event(in_dev);
1078        }
1079done:
1080#endif
1081        ip_mc_clear_src(im);
1082}
1083
1084static void igmp_group_added(struct ip_mc_list *im)
1085{
1086        struct in_device *in_dev = im->interface;
1087
1088        if (im->loaded == 0) {
1089                im->loaded = 1;
1090                ip_mc_filter_add(in_dev, im->multiaddr);
1091        }
1092
1093#ifdef CONFIG_IP_MULTICAST
1094        if (im->multiaddr == IGMP_ALL_HOSTS)
1095                return;
1096
1097        if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) {
1098                spin_lock_bh(&im->lock);
1099                igmp_start_timer(im, IGMP_Initial_Report_Delay);
1100                spin_unlock_bh(&im->lock);
1101                return;
1102        }
1103        /* else, v3 */
1104
1105        im->crcount = in_dev->mr_qrv ? in_dev->mr_qrv :
1106                IGMP_Unsolicited_Report_Count;
1107        igmp_ifc_event(in_dev);
1108#endif
1109}
1110
1111
1112/*
1113 *      Multicast list managers
1114 */
1115
1116
1117/*
1118 *      A socket has joined a multicast group on device dev.
1119 */
1120
1121void ip_mc_inc_group(struct in_device *in_dev, u32 addr)
1122{
1123        struct ip_mc_list *im;
1124
1125        ASSERT_RTNL();
1126
1127        for (im=in_dev->mc_list; im; im=im->next) {
1128                if (im->multiaddr == addr) {
1129                        im->users++;
1130                        ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, 0, 0);
1131                        goto out;
1132                }
1133        }
1134
1135        im = (struct ip_mc_list *)kmalloc(sizeof(*im), GFP_KERNEL);
1136        if (!im)
1137                goto out;
1138
1139        im->users=1;
1140        im->interface=in_dev;
1141        in_dev_hold(in_dev);
1142        im->multiaddr=addr;
1143        /* initial mode is (EX, empty) */
1144        im->sfmode = MCAST_EXCLUDE;
1145        im->sfcount[MCAST_INCLUDE] = 0;
1146        im->sfcount[MCAST_EXCLUDE] = 1;
1147        im->sources = 0;
1148        im->tomb = 0;
1149        im->crcount = 0;
1150        atomic_set(&im->refcnt, 1);
1151        spin_lock_init(&im->lock);
1152#ifdef CONFIG_IP_MULTICAST
1153        im->tm_running=0;
1154        init_timer(&im->timer);
1155        im->timer.data=(unsigned long)im;
1156        im->timer.function=&igmp_timer_expire;
1157        im->unsolicit_count = IGMP_Unsolicited_Report_Count;
1158        im->reporter = 0;
1159        im->gsquery = 0;
1160#endif
1161        im->loaded = 0;
1162        write_lock_bh(&in_dev->lock);
1163        im->next=in_dev->mc_list;
1164        in_dev->mc_list=im;
1165        write_unlock_bh(&in_dev->lock);
1166#ifdef CONFIG_IP_MULTICAST
1167        igmpv3_del_delrec(in_dev, im->multiaddr);
1168#endif
1169        igmp_group_added(im);
1170        if (in_dev->dev->flags & IFF_UP)
1171                ip_rt_multicast_event(in_dev);
1172out:
1173        return;
1174}
1175
1176/*
1177 *      A socket has left a multicast group on device dev
1178 */
1179
1180void ip_mc_dec_group(struct in_device *in_dev, u32 addr)
1181{
1182        struct ip_mc_list *i, **ip;
1183        
1184        ASSERT_RTNL();
1185        
1186        for (ip=&in_dev->mc_list; (i=*ip)!=NULL; ip=&i->next) {
1187                if (i->multiaddr==addr) {
1188                        if (--i->users == 0) {
1189                                write_lock_bh(&in_dev->lock);
1190                                *ip = i->next;
1191                                write_unlock_bh(&in_dev->lock);
1192                                igmp_group_dropped(i);
1193
1194                                if (in_dev->dev->flags & IFF_UP)
1195                                        ip_rt_multicast_event(in_dev);
1196
1197                                ip_ma_put(i);
1198                                return;
1199                        }
1200                        break;
1201                }
1202        }
1203}
1204
1205/* Device going down */
1206
1207void ip_mc_down(struct in_device *in_dev)
1208{
1209        struct ip_mc_list *i;
1210
1211        ASSERT_RTNL();
1212
1213#ifdef CONFIG_IP_MULTICAST
1214        in_dev->mr_ifc_count = 0;
1215        if (del_timer(&in_dev->mr_ifc_timer))
1216                __in_dev_put(in_dev);
1217        in_dev->mr_gq_running = 0;
1218        if (del_timer(&in_dev->mr_gq_timer))
1219                __in_dev_put(in_dev);
1220#endif
1221
1222        for (i=in_dev->mc_list; i; i=i->next)
1223                igmp_group_dropped(i);
1224
1225#ifdef CONFIG_IP_MULTICAST
1226        igmpv3_clear_delrec(in_dev);
1227#endif
1228
1229        ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS);
1230}
1231
1232/* Device going up */
1233
1234void ip_mc_up(struct in_device *in_dev)
1235{
1236        struct ip_mc_list *i;
1237
1238        ASSERT_RTNL();
1239
1240        in_dev->mc_tomb = 0;
1241#ifdef CONFIG_IP_MULTICAST
1242        in_dev->mr_gq_running = 0;
1243        init_timer(&in_dev->mr_gq_timer);
1244        in_dev->mr_gq_timer.data=(unsigned long) in_dev;
1245        in_dev->mr_gq_timer.function=&igmp_gq_timer_expire;
1246        in_dev->mr_ifc_count = 0;
1247        init_timer(&in_dev->mr_ifc_timer);
1248        in_dev->mr_ifc_timer.data=(unsigned long) in_dev;
1249        in_dev->mr_ifc_timer.function=&igmp_ifc_timer_expire;
1250        in_dev->mr_qrv = IGMP_Unsolicited_Report_Count;
1251#endif
1252
1253        in_dev->mc_lock = RW_LOCK_UNLOCKED;
1254        ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS);
1255
1256        for (i=in_dev->mc_list; i; i=i->next)
1257                igmp_group_added(i);
1258}
1259
1260/*
1261 *      Device is about to be destroyed: clean up.
1262 */
1263
1264void ip_mc_destroy_dev(struct in_device *in_dev)
1265{
1266        struct ip_mc_list *i;
1267
1268        ASSERT_RTNL();
1269
1270        write_lock_bh(&in_dev->lock);
1271        while ((i = in_dev->mc_list) != NULL) {
1272                in_dev->mc_list = i->next;
1273                write_unlock_bh(&in_dev->lock);
1274
1275                igmp_group_dropped(i);
1276                ip_ma_put(i);
1277
1278                write_lock_bh(&in_dev->lock);
1279        }
1280        write_unlock_bh(&in_dev->lock);
1281}
1282
1283static struct in_device * ip_mc_find_dev(struct ip_mreqn *imr)
1284{
1285        struct flowi fl = { .nl_u = { .ip4_u =
1286                                      { .daddr = imr->imr_multiaddr.s_addr } } };
1287        struct rtable *rt;
1288        struct net_device *dev = NULL;
1289        struct in_device *idev = NULL;
1290
1291        if (imr->imr_ifindex) {
1292                idev = inetdev_by_index(imr->imr_ifindex);
1293                if (idev)
1294                        __in_dev_put(idev);
1295                return idev;
1296        }
1297        if (imr->imr_address.s_addr) {
1298                dev = ip_dev_find(imr->imr_address.s_addr);
1299                if (!dev)
1300                        return NULL;
1301                __dev_put(dev);
1302        }
1303
1304        if (!dev && !ip_route_output_key(&rt, &fl)) {
1305                dev = rt->u.dst.dev;
1306                ip_rt_put(rt);
1307        }
1308        if (dev) {
1309                imr->imr_ifindex = dev->ifindex;
1310                idev = __in_dev_get(dev);
1311        }
1312        return idev;
1313}
1314
1315/*
1316 *      Join a socket to a group
1317 */
1318int sysctl_igmp_max_memberships = IP_MAX_MEMBERSHIPS;
1319
1320
1321static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode,
1322        __u32 *psfsrc)
1323{
1324        struct ip_sf_list *psf, *psf_prev;
1325        int rv = 0;
1326
1327        psf_prev = 0;
1328        for (psf=pmc->sources; psf; psf=psf->sf_next) {
1329                if (psf->sf_inaddr == *psfsrc)
1330                        break;
1331                psf_prev = psf;
1332        }
1333        if (!psf || psf->sf_count[sfmode] == 0) {
1334                /* source filter not found, or count wrong =>  bug */
1335                return -ESRCH;
1336        }
1337        psf->sf_count[sfmode]--;
1338        if (psf->sf_count[sfmode] == 0) {
1339                ip_rt_multicast_event(pmc->interface);
1340        }
1341        if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) {
1342#ifdef CONFIG_IP_MULTICAST
1343                struct in_device *in_dev = pmc->interface;
1344#endif
1345
1346                /* no more filters for this source */
1347                if (psf_prev)
1348                        psf_prev->sf_next = psf->sf_next;
1349                else
1350                        pmc->sources = psf->sf_next;
1351#ifdef CONFIG_IP_MULTICAST
1352                if (psf->sf_oldin &&
1353                    !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) {
1354                        psf->sf_crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 
1355                                IGMP_Unsolicited_Report_Count;
1356                        psf->sf_next = pmc->tomb;
1357                        pmc->tomb = psf;
1358                        rv = 1;
1359                } else
1360#endif
1361                        kfree(psf);
1362        }
1363        return rv;
1364}
1365
1366#ifndef CONFIG_IP_MULTICAST
1367#define igmp_ifc_event(x)       do { } while (0)
1368#endif
1369
1370int ip_mc_del_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1371        int sfcount, __u32 *psfsrc, int delta)
1372{
1373        struct ip_mc_list *pmc;
1374        int     changerec = 0;
1375        int     i, err;
1376
1377        if (!in_dev)
1378                return -ENODEV;
1379        read_lock(&in_dev->lock);
1380        for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1381                if (*pmca == pmc->multiaddr)
1382                        break;
1383        }
1384        if (!pmc) {
1385                /* MCA not found?? bug */
1386                read_unlock(&in_dev->lock);
1387                return -ESRCH;
1388        }
1389        spin_lock_bh(&pmc->lock);
1390        read_unlock(&in_dev->lock);
1391#ifdef CONFIG_IP_MULTICAST
1392        sf_markstate(pmc);
1393#endif
1394        if (!delta) {
1395                err = -EINVAL;
1396                if (!pmc->sfcount[sfmode])
1397                        goto out_unlock;
1398                pmc->sfcount[sfmode]--;
1399        }
1400        err = 0;
1401        for (i=0; i<sfcount; i++) {
1402                int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1403
1404                changerec |= rv > 0;
1405                if (!err && rv < 0)
1406                        err = rv;
1407        }
1408        if (pmc->sfmode == MCAST_EXCLUDE &&
1409            pmc->sfcount[MCAST_EXCLUDE] == 0 &&
1410            pmc->sfcount[MCAST_INCLUDE]) {
1411#ifdef CONFIG_IP_MULTICAST
1412                struct ip_sf_list *psf;
1413#endif
1414
1415                /* filter mode change */
1416                pmc->sfmode = MCAST_INCLUDE;
1417#ifdef CONFIG_IP_MULTICAST
1418                pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 
1419                        IGMP_Unsolicited_Report_Count;
1420                in_dev->mr_ifc_count = pmc->crcount;
1421                for (psf=pmc->sources; psf; psf = psf->sf_next)
1422                        psf->sf_crcount = 0;
1423                igmp_ifc_event(pmc->interface);
1424        } else if (sf_setstate(pmc) || changerec) {
1425                igmp_ifc_event(pmc->interface);
1426#endif
1427        }
1428out_unlock:
1429        spin_unlock_bh(&pmc->lock);
1430        return err;
1431}
1432
1433/*
1434 * Add multicast single-source filter to the interface list
1435 */
1436static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode,
1437        __u32 *psfsrc, int delta)
1438{
1439        struct ip_sf_list *psf, *psf_prev;
1440
1441        psf_prev = 0;
1442        for (psf=pmc->sources; psf; psf=psf->sf_next) {
1443                if (psf->sf_inaddr == *psfsrc)
1444                        break;
1445                psf_prev = psf;
1446        }
1447        if (!psf) {
1448                psf = (struct ip_sf_list *)kmalloc(sizeof(*psf), GFP_ATOMIC);
1449                if (!psf)
1450                        return -ENOBUFS;
1451                memset(psf, 0, sizeof(*psf));
1452                psf->sf_inaddr = *psfsrc;
1453                if (psf_prev) {
1454                        psf_prev->sf_next = psf;
1455                } else
1456                        pmc->sources = psf;
1457        }
1458        psf->sf_count[sfmode]++;
1459        if (psf->sf_count[sfmode] == 1) {
1460                ip_rt_multicast_event(pmc->interface);
1461        }
1462        return 0;
1463}
1464
1465#ifdef CONFIG_IP_MULTICAST
1466static void sf_markstate(struct ip_mc_list *pmc)
1467{
1468        struct ip_sf_list *psf;
1469        int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1470
1471        for (psf=pmc->sources; psf; psf=psf->sf_next)
1472                if (pmc->sfcount[MCAST_EXCLUDE]) {
1473                        psf->sf_oldin = mca_xcount ==
1474                                psf->sf_count[MCAST_EXCLUDE] &&
1475                                !psf->sf_count[MCAST_INCLUDE];
1476                } else
1477                        psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0;
1478}
1479
1480static int sf_setstate(struct ip_mc_list *pmc)
1481{
1482        struct ip_sf_list *psf;
1483        int mca_xcount = pmc->sfcount[MCAST_EXCLUDE];
1484        int qrv = pmc->interface->mr_qrv;
1485        int new_in, rv;
1486
1487        rv = 0;
1488        for (psf=pmc->sources; psf; psf=psf->sf_next) {
1489                if (pmc->sfcount[MCAST_EXCLUDE]) {
1490                        new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] &&
1491                                !psf->sf_count[MCAST_INCLUDE];
1492                } else
1493                        new_in = psf->sf_count[MCAST_INCLUDE] != 0;
1494                if (new_in != psf->sf_oldin) {
1495                        psf->sf_crcount = qrv;
1496                        rv++;
1497                }
1498        }
1499        return rv;
1500}
1501#endif
1502
1503/*
1504 * Add multicast source filter list to the interface list
1505 */
1506int ip_mc_add_src(struct in_device *in_dev, __u32 *pmca, int sfmode,
1507        int sfcount, __u32 *psfsrc, int delta)
1508{
1509        struct ip_mc_list *pmc;
1510        int     isexclude;
1511        int     i, err;
1512
1513        if (!in_dev)
1514                return -ENODEV;
1515        read_lock(&in_dev->lock);
1516        for (pmc=in_dev->mc_list; pmc; pmc=pmc->next) {
1517                if (*pmca == pmc->multiaddr)
1518                        break;
1519        }
1520        if (!pmc) {
1521                /* MCA not found?? bug */
1522                read_unlock(&in_dev->lock);
1523                return -ESRCH;
1524        }
1525        spin_lock_bh(&pmc->lock);
1526        read_unlock(&in_dev->lock);
1527
1528#ifdef CONFIG_IP_MULTICAST
1529        sf_markstate(pmc);
1530#endif
1531        isexclude = pmc->sfmode == MCAST_EXCLUDE;
1532        if (!delta)
1533                pmc->sfcount[sfmode]++;
1534        err = 0;
1535        for (i=0; i<sfcount; i++) {
1536                err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i], delta);
1537                if (err)
1538                        break;
1539        }
1540        if (err) {
1541                int j;
1542
1543                pmc->sfcount[sfmode]--;
1544                for (j=0; j<i; j++)
1545                        (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1546        } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) {
1547#ifdef CONFIG_IP_MULTICAST
1548                struct in_device *in_dev = pmc->interface;
1549                struct ip_sf_list *psf;
1550#endif
1551
1552                /* filter mode change */
1553                if (pmc->sfcount[MCAST_EXCLUDE])
1554                        pmc->sfmode = MCAST_EXCLUDE;
1555                else if (pmc->sfcount[MCAST_INCLUDE])
1556                        pmc->sfmode = MCAST_INCLUDE;
1557#ifdef CONFIG_IP_MULTICAST
1558                /* else no filters; keep old mode for reports */
1559
1560                pmc->crcount = in_dev->mr_qrv ? in_dev->mr_qrv : 
1561                        IGMP_Unsolicited_Report_Count;
1562                in_dev->mr_ifc_count = pmc->crcount;
1563                for (psf=pmc->sources; psf; psf = psf->sf_next)
1564                        psf->sf_crcount = 0;
1565                igmp_ifc_event(in_dev);
1566        } else if (sf_setstate(pmc)) {
1567                igmp_ifc_event(in_dev);
1568#endif
1569        }
1570        spin_unlock_bh(&pmc->lock);
1571        return err;
1572}
1573
1574static void ip_mc_clear_src(struct ip_mc_list *pmc)
1575{
1576        struct ip_sf_list *psf, *nextpsf;
1577
1578        for (psf=pmc->tomb; psf; psf=nextpsf) {
1579                nextpsf = psf->sf_next;
1580                kfree(psf);
1581        }
1582        pmc->tomb = 0;
1583        for (psf=pmc->sources; psf; psf=nextpsf) {
1584                nextpsf = psf->sf_next;
1585                kfree(psf);
1586        }
1587        pmc->sources = 0;
1588        pmc->sfmode = MCAST_EXCLUDE;
1589        pmc->sfcount[MCAST_EXCLUDE] = 0;
1590        pmc->sfcount[MCAST_EXCLUDE] = 1;
1591}
1592
1593
1594/*
1595 * Join a multicast group
1596 */
1597int ip_mc_join_group(struct sock *sk , struct ip_mreqn *imr)
1598{
1599        int err;
1600        u32 addr = imr->imr_multiaddr.s_addr;
1601        struct ip_mc_socklist *iml, *i;
1602        struct in_device *in_dev;
1603        struct inet_opt *inet = inet_sk(sk);
1604        int count = 0;
1605
1606        if (!MULTICAST(addr))
1607                return -EINVAL;
1608
1609        rtnl_shlock();
1610
1611        in_dev = ip_mc_find_dev(imr);
1612
1613        if (!in_dev) {
1614                iml = NULL;
1615                err = -ENODEV;
1616                goto done;
1617        }
1618
1619        iml = (struct ip_mc_socklist *)sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL);
1620
1621        err = -EADDRINUSE;
1622        for (i = inet->mc_list; i; i = i->next) {
1623                if (memcmp(&i->multi, imr, sizeof(*imr)) == 0) {
1624                        /* New style additions are reference counted */
1625                        if (imr->imr_address.s_addr == 0) {
1626                                i->count++;
1627                                err = 0;
1628                        }
1629                        goto done;
1630                }
1631                count++;
1632        }
1633        err = -ENOBUFS;
1634        if (iml == NULL || count >= sysctl_igmp_max_memberships)
1635                goto done;
1636        memcpy(&iml->multi, imr, sizeof(*imr));
1637        iml->next = inet->mc_list;
1638        iml->count = 1;
1639        iml->sflist = NULL;
1640        iml->sfmode = MCAST_EXCLUDE;
1641        inet->mc_list = iml;
1642        ip_mc_inc_group(in_dev, addr);
1643        iml = NULL;
1644        err = 0;
1645
1646done:
1647        rtnl_shunlock();
1648        if (iml)
1649                sock_kfree_s(sk, iml, sizeof(*iml));
1650        return err;
1651}
1652
1653int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml,
1654        struct in_device *in_dev)
1655{
1656        int err;
1657
1658        if (iml->sflist == 0) {
1659                /* any-source empty exclude case */
1660                return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1661                        iml->sfmode, 0, 0, 0);
1662        }
1663        err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr,
1664                        iml->sfmode, iml->sflist->sl_count,
1665                        iml->sflist->sl_addr, 0);
1666        sock_kfree_s(sk, iml->sflist, IP_SFLSIZE(iml->sflist->sl_max));
1667        iml->sflist = 0;
1668        return err;
1669}
1670
1671/*
1672 *      Ask a socket to leave a group.
1673 */
1674
1675int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr)
1676{
1677        struct inet_opt *inet = inet_sk(sk);
1678        struct ip_mc_socklist *iml, **imlp;
1679
1680        rtnl_lock();
1681        for (imlp = &inet->mc_list; (iml = *imlp) != NULL; imlp = &iml->next) {
1682                if (iml->multi.imr_multiaddr.s_addr==imr->imr_multiaddr.s_addr &&
1683                    iml->multi.imr_address.s_addr==imr->imr_address.s_addr &&
1684                    (!imr->imr_ifindex || iml->multi.imr_ifindex==imr->imr_ifindex)) {
1685                        struct in_device *in_dev;
1686
1687                        in_dev = inetdev_by_index(iml->multi.imr_ifindex);
1688                        if (in_dev)
1689                                (void) ip_mc_leave_src(sk, iml, in_dev);
1690                        if (--iml->count) {
1691                                rtnl_unlock();
1692                                if (in_dev)
1693                                        in_dev_put(in_dev);
1694                                return 0;
1695                        }
1696
1697                        *imlp = iml->next;
1698
1699                        if (in_dev) {
1700                                ip_mc_dec_group(in_dev, imr->imr_multiaddr.s_addr);
1701                                in_dev_put(in_dev);
1702                        }
1703                        rtnl_unlock();
1704                        sock_kfree_s(sk, iml, sizeof(*iml));
1705                        return 0;
1706                }
1707        }
1708        rtnl_unlock();
1709        return -EADDRNOTAVAIL;
1710}
1711
1712int ip_mc_source(int add, int omode, struct sock *sk, struct
1713        ip_mreq_source *mreqs, int ifindex)
1714{
1715        int err;
1716        struct ip_mreqn imr;
1717        u32 addr = mreqs->imr_multiaddr;
1718        struct ip_mc_socklist *pmc;
1719        struct in_device *in_dev = 0;
1720        struct inet_opt *inet = inet_sk(sk);
1721        struct ip_sf_socklist *psl;
1722        int i, j, rv;
1723
1724        if (!MULTICAST(addr))
1725                return -EINVAL;
1726
1727        rtnl_shlock();
1728
1729        imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr;
1730        imr.imr_address.s_addr = mreqs->imr_interface;
1731        imr.imr_ifindex = ifindex;
1732        in_dev = ip_mc_find_dev(&imr);
1733
1734        if (!in_dev) {
1735                err = -ENODEV;
1736                goto done;
1737        }
1738        err = -EADDRNOTAVAIL;
1739
1740        for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1741                if (memcmp(&pmc->multi, mreqs, 2*sizeof(__u32)) == 0)
1742                        break;
1743        }
1744        if (!pmc)               /* must have a prior join */
1745                goto done;
1746        /* if a source filter was set, must be the same mode as before */
1747        if (pmc->sflist) {
1748                if (pmc->sfmode != omode)
1749                        goto done;
1750        } else if (pmc->sfmode != omode) {
1751                /* allow mode switches for empty-set filters */
1752                ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, 
1753                        0, 0);
1754                pmc->sfmode = omode;
1755                ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, 
1756                        0, 0);
1757        }
1758
1759        psl = pmc->sflist;
1760        if (!add) {
1761                if (!psl)
1762                        goto done;
1763                rv = !0;
1764                for (i=0; i<psl->sl_count; i++) {
1765                        rv = memcmp(&psl->sl_addr, &mreqs->imr_multiaddr,
1766                                sizeof(__u32));
1767                        if (rv >= 0)
1768                                break;
1769                }
1770                if (!rv)        /* source not found */
1771                        goto done;
1772
1773                /* update the interface filter */
1774                ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 
1775                        &mreqs->imr_sourceaddr, 1);
1776
1777                for (j=i+1; j<psl->sl_count; j++)
1778                        psl->sl_addr[j-1] = psl->sl_addr[j];
1779                psl->sl_count--;
1780                err = 0;
1781                goto done;
1782        }
1783        /* else, add a new source to the filter */
1784
1785        if (!psl || psl->sl_count == psl->sl_max) {
1786                struct ip_sf_socklist *newpsl;
1787                int count = IP_SFBLOCK;
1788
1789                if (psl)
1790                        count += psl->sl_max;
1791                newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1792                        IP_SFLSIZE(count), GFP_KERNEL);
1793                if (!newpsl) {
1794                        err = -ENOBUFS;
1795                        goto done;
1796                }
1797                newpsl->sl_max = count;
1798                newpsl->sl_count = count - IP_SFBLOCK;
1799                if (psl) {
1800                        for (i=0; i<psl->sl_count; i++)
1801                                newpsl->sl_addr[i] = psl->sl_addr[i];
1802                        sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1803                }
1804                pmc->sflist = psl = newpsl;
1805        }
1806        rv = 1; /* > 0 for insert logic below if sl_count is 0 */
1807        for (i=0; i<psl->sl_count; i++) {
1808                rv = memcmp(&psl->sl_addr, &mreqs->imr_multiaddr,
1809                        sizeof(__u32));
1810                if (rv >= 0)
1811                        break;
1812        }
1813        if (rv == 0)            /* address already there is an error */
1814                goto done;
1815        for (j=psl->sl_count-1; j>=i; j--)
1816                psl->sl_addr[j+1] = psl->sl_addr[j];
1817        psl->sl_addr[i] = mreqs->imr_sourceaddr;
1818        psl->sl_count++;
1819        err = 0;
1820        /* update the interface list */
1821        ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, 
1822                &mreqs->imr_sourceaddr, 1);
1823done:
1824        rtnl_shunlock();
1825        return err;
1826}
1827
1828int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex)
1829{
1830        int err;
1831        struct ip_mreqn imr;
1832        u32 addr = msf->imsf_multiaddr;
1833        struct ip_mc_socklist *pmc;
1834        struct in_device *in_dev;
1835        struct inet_opt *inet = inet_sk(sk);
1836        struct ip_sf_socklist *newpsl, *psl;
1837
1838        if (!MULTICAST(addr))
1839                return -EINVAL;
1840        if (msf->imsf_fmode != MCAST_INCLUDE &&
1841            msf->imsf_fmode != MCAST_EXCLUDE)
1842                return -EINVAL;
1843
1844        rtnl_shlock();
1845
1846        imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1847        imr.imr_address.s_addr = msf->imsf_interface;
1848        imr.imr_ifindex = ifindex;
1849        in_dev = ip_mc_find_dev(&imr);
1850
1851        if (!in_dev) {
1852                err = -ENODEV;
1853                goto done;
1854        }
1855        err = -EADDRNOTAVAIL;
1856
1857        for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1858                if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1859                    pmc->multi.imr_ifindex == imr.imr_ifindex)
1860                        break;
1861        }
1862        if (!pmc)               /* must have a prior join */
1863                goto done;
1864        if (msf->imsf_numsrc) {
1865                newpsl = (struct ip_sf_socklist *)sock_kmalloc(sk,
1866                                IP_SFLSIZE(msf->imsf_numsrc), GFP_KERNEL);
1867                if (!newpsl) {
1868                        err = -ENOBUFS;
1869                        goto done;
1870                }
1871                newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc;
1872                memcpy(newpsl->sl_addr, msf->imsf_slist,
1873                        msf->imsf_numsrc * sizeof(msf->imsf_slist[0]));
1874                err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr,
1875                        msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0);
1876                if (err) {
1877                        sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max));
1878                        goto done;
1879                }
1880        } else
1881                newpsl = 0;
1882        psl = pmc->sflist;
1883        if (psl) {
1884                (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1885                        psl->sl_count, psl->sl_addr, 0);
1886                sock_kfree_s(sk, psl, IP_SFLSIZE(psl->sl_max));
1887        } else
1888                (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode,
1889                        0, 0, 0);
1890        pmc->sflist = newpsl;
1891        pmc->sfmode = msf->imsf_fmode;
1892done:
1893        rtnl_shunlock();
1894        return err;
1895}
1896
1897int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf,
1898        struct ip_msfilter *optval, int *optlen)
1899{
1900        int err, len, count, copycount;
1901        struct ip_mreqn imr;
1902        u32 addr = msf->imsf_multiaddr;
1903        struct ip_mc_socklist *pmc;
1904        struct in_device *in_dev;
1905        struct inet_opt *inet = inet_sk(sk);
1906        struct ip_sf_socklist *psl;
1907
1908        if (!MULTICAST(addr))
1909                return -EINVAL;
1910
1911        rtnl_shlock();
1912
1913        imr.imr_multiaddr.s_addr = msf->imsf_multiaddr;
1914        imr.imr_address.s_addr = msf->imsf_interface;
1915        imr.imr_ifindex = 0;
1916        in_dev = ip_mc_find_dev(&imr);
1917
1918        if (!in_dev) {
1919                err = -ENODEV;
1920                goto done;
1921        }
1922        err = -EADDRNOTAVAIL;
1923
1924        for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1925                if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr &&
1926                    pmc->multi.imr_ifindex == imr.imr_ifindex)
1927                        break;
1928        }
1929        if (!pmc)               /* must have a prior join */
1930                goto done;
1931        msf->imsf_fmode = pmc->sfmode;
1932        psl = pmc->sflist;
1933        rtnl_shunlock();
1934        if (!psl) {
1935                len = 0;
1936                count = 0;
1937        } else {
1938                count = psl->sl_count;
1939        }
1940        copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc;
1941        len = copycount * sizeof(psl->sl_addr[0]);
1942        msf->imsf_numsrc = count;
1943        if (put_user(IP_MSFILTER_SIZE(copycount), optlen) ||
1944            copy_to_user((void *)optval, msf, IP_MSFILTER_SIZE(0))) {
1945                return -EFAULT;
1946        }
1947        if (len &&
1948            copy_to_user((void *)&optval->imsf_slist[0], psl->sl_addr, len))
1949                return -EFAULT;
1950        return 0;
1951done:
1952        rtnl_shunlock();
1953        return err;
1954}
1955
1956int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf,
1957        struct group_filter *optval, int *optlen)
1958{
1959        int err, i, count, copycount;
1960        struct sockaddr_in *psin;
1961        u32 addr;
1962        struct ip_mc_socklist *pmc;
1963        struct inet_opt *inet = inet_sk(sk);
1964        struct ip_sf_socklist *psl;
1965
1966        psin = (struct sockaddr_in *)&gsf->gf_group;
1967        if (psin->sin_family != AF_INET)
1968                return -EINVAL;
1969        addr = psin->sin_addr.s_addr;
1970        if (!MULTICAST(addr))
1971                return -EINVAL;
1972
1973        rtnl_shlock();
1974
1975        err = -EADDRNOTAVAIL;
1976
1977        for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
1978                if (pmc->multi.imr_multiaddr.s_addr == addr &&
1979                    pmc->multi.imr_ifindex == gsf->gf_interface)
1980                        break;
1981        }
1982        if (!pmc)               /* must have a prior join */
1983                goto done;
1984        gsf->gf_fmode = pmc->sfmode;
1985        psl = pmc->sflist;
1986        rtnl_shunlock();
1987        count = psl ? psl->sl_count : 0;
1988        copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc;
1989        gsf->gf_numsrc = count;
1990        if (put_user(GROUP_FILTER_SIZE(copycount), optlen) ||
1991            copy_to_user((void *)optval, gsf, GROUP_FILTER_SIZE(0))) {
1992                return -EFAULT;
1993        }
1994        for (i=0; i<copycount; i++) {
1995                struct sockaddr_in *psin;
1996                struct sockaddr_storage ss;
1997
1998                psin = (struct sockaddr_in *)&ss;
1999                memset(&ss, 0, sizeof(ss));
2000                psin->sin_family = AF_INET;
2001                psin->sin_addr.s_addr = psl->sl_addr[i];
2002                if (copy_to_user((void *)&optval->gf_slist[i], &ss, sizeof(ss)))
2003                        return -EFAULT;
2004        }
2005        return 0;
2006done:
2007        rtnl_shunlock();
2008        return err;
2009}
2010
2011/*
2012 * check if a multicast source filter allows delivery for a given <src,dst,intf>
2013 */
2014int ip_mc_sf_allow(struct sock *sk, u32 loc_addr, u32 rmt_addr, int dif)
2015{
2016        struct inet_opt *inet = inet_sk(sk);
2017        struct ip_mc_socklist *pmc;
2018        struct ip_sf_socklist *psl;
2019        int i;
2020
2021        if (!MULTICAST(loc_addr))
2022                return 1;
2023
2024        for (pmc=inet->mc_list; pmc; pmc=pmc->next) {
2025                if (pmc->multi.imr_multiaddr.s_addr == loc_addr &&
2026                    pmc->multi.imr_ifindex == dif)
2027                        break;
2028        }
2029        if (!pmc)
2030                return 1;
2031        psl = pmc->sflist;
2032        if (!psl)
2033                return pmc->sfmode == MCAST_EXCLUDE;
2034
2035        for (i=0; i<psl->sl_count; i++) {
2036                if (psl->sl_addr[i] == rmt_addr)
2037                        break;
2038        }
2039        if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count)
2040                return 0;
2041        if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count)
2042                return 0;
2043        return 1;
2044}
2045
2046/*
2047 *      A socket is closing.
2048 */
2049
2050void ip_mc_drop_socket(struct sock *sk)
2051{
2052        struct inet_opt *inet = inet_sk(sk);
2053        struct ip_mc_socklist *iml;
2054
2055        if (inet->mc_list == NULL)
2056                return;
2057
2058        rtnl_lock();
2059        while ((iml = inet->mc_list) != NULL) {
2060                struct in_device *in_dev;
2061                inet->mc_list = iml->next;
2062
2063                if ((in_dev = inetdev_by_index(iml->multi.imr_ifindex)) != NULL) {
2064                        (void) ip_mc_leave_src(sk, iml, in_dev);
2065                        ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr);
2066                        in_dev_put(in_dev);
2067                }
2068                sock_kfree_s(sk, iml, sizeof(*iml));
2069
2070        }
2071        rtnl_unlock();
2072}
2073
2074int ip_check_mc(struct in_device *in_dev, u32 mc_addr, u32 src_addr, u16 proto)
2075{
2076        struct ip_mc_list *im;
2077        struct ip_sf_list *psf;
2078        int rv = 0;
2079
2080        read_lock(&in_dev->lock);
2081        for (im=in_dev->mc_list; im; im=im->next) {
2082                if (im->multiaddr == mc_addr)
2083                        break;
2084        }
2085        if (im && proto == IPPROTO_IGMP) {
2086                rv = 1;
2087        } else if (im) {
2088                for (psf=im->sources; psf; psf=psf->sf_next) {
2089                        if (psf->sf_inaddr == src_addr)
2090                                break;
2091                }
2092                if (psf)
2093                        rv = psf->sf_count[MCAST_INCLUDE] ||
2094                                psf->sf_count[MCAST_EXCLUDE] !=
2095                                im->sfcount[MCAST_EXCLUDE];
2096                else
2097                        rv = im->sfcount[MCAST_EXCLUDE] != 0;
2098        }
2099        read_unlock(&in_dev->lock);
2100        return rv;
2101}
2102
2103#if defined(CONFIG_PROC_FS)
2104struct igmp_mc_iter_state {
2105        struct net_device *dev;
2106        struct in_device *in_dev;
2107};
2108
2109#define igmp_mc_seq_private(seq)        ((struct igmp_mc_iter_state *)(seq)->private)
2110
2111static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq)
2112{
2113        struct ip_mc_list *im = NULL;
2114        struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2115
2116        for (state->dev = dev_base, state->in_dev = NULL;
2117             state->dev; 
2118             state->dev = state->dev->next) {
2119                struct in_device *in_dev;
2120                in_dev = in_dev_get(state->dev);
2121                if (!in_dev)
2122                        continue;
2123                read_lock(&in_dev->lock);
2124                im = in_dev->mc_list;
2125                if (im) {
2126                        state->in_dev = in_dev;
2127                        break;
2128                }
2129                read_unlock(&in_dev->lock);
2130                in_dev_put(in_dev);
2131        }
2132        return im;
2133}
2134
2135static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im)
2136{
2137        struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2138        im = im->next;
2139        while (!im) {
2140                if (likely(state->in_dev != NULL)) {
2141                        read_unlock(&state->in_dev->lock);
2142                        in_dev_put(state->in_dev);
2143                }
2144                state->dev = state->dev->next;
2145                if (!state->dev) {
2146                        state->in_dev = NULL;
2147                        break;
2148                }
2149                state->in_dev = in_dev_get(state->dev);
2150                if (!state->in_dev)
2151                        continue;
2152                read_lock(&state->in_dev->lock);
2153                im = state->in_dev->mc_list;
2154        }
2155        return im;
2156}
2157
2158static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos)
2159{
2160        struct ip_mc_list *im = igmp_mc_get_first(seq);
2161        if (im)
2162                while (pos && (im = igmp_mc_get_next(seq, im)) != NULL)
2163                        --pos;
2164        return pos ? NULL : im;
2165}
2166
2167static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos)
2168{
2169        read_lock(&dev_base_lock);
2170        return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2171}
2172
2173static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2174{
2175        struct ip_mc_list *im;
2176        if (v == SEQ_START_TOKEN)
2177                im = igmp_mc_get_first(seq);
2178        else
2179                im = igmp_mc_get_next(seq, v);
2180        ++*pos;
2181        return im;
2182}
2183
2184static void igmp_mc_seq_stop(struct seq_file *seq, void *v)
2185{
2186        struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2187        if (likely(state->in_dev != NULL)) {
2188                read_unlock(&state->in_dev->lock);
2189                in_dev_put(state->in_dev);
2190                state->in_dev = NULL;
2191        }
2192        state->dev = NULL;
2193        read_unlock(&dev_base_lock);
2194}
2195
2196static int igmp_mc_seq_show(struct seq_file *seq, void *v)
2197{
2198        if (v == SEQ_START_TOKEN)
2199                seq_printf(seq, 
2200                           "Idx\tDevice    : Count Querier\tGroup    Users Timer\tReporter\n");
2201        else {
2202                struct ip_mc_list *im = (struct ip_mc_list *)v;
2203                struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq);
2204                char   *querier;
2205#ifdef CONFIG_IP_MULTICAST
2206                querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : "V2";
2207#else
2208                querier = "NONE";
2209#endif
2210
2211                if (state->in_dev->mc_list == im) {
2212                        seq_printf(seq, "%d\t%-10s: %5d %7s\n",
2213                                   state->dev->ifindex, state->dev->name, state->dev->mc_count, querier);
2214                }
2215
2216                seq_printf(seq,
2217                           "\t\t\t\t%08lX %5d %d:%08lX\t\t%d\n",
2218                           im->multiaddr, im->users,
2219                           im->tm_running, jiffies_to_clock_t(im->timer.expires-jiffies), im->reporter);
2220        }
2221        return 0;
2222}
2223
2224static struct seq_operations igmp_mc_seq_ops = {
2225        .start  =       igmp_mc_seq_start,
2226        .next   =       igmp_mc_seq_next,
2227        .stop   =       igmp_mc_seq_stop,
2228        .show   =       igmp_mc_seq_show,
2229};
2230
2231static int igmp_mc_seq_open(struct inode *inode, struct file *file)
2232{
2233        struct seq_file *seq;
2234        int rc = -ENOMEM;
2235        struct igmp_mc_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2236
2237        if (!s)
2238                goto out;
2239        rc = seq_open(file, &igmp_mc_seq_ops);
2240        if (rc)
2241                goto out_kfree;
2242
2243        seq = file->private_data;
2244        seq->private = s;
2245        memset(s, 0, sizeof(*s));
2246out:
2247        return rc;
2248out_kfree:
2249        kfree(s);
2250        goto out;
2251}
2252
2253static struct file_operations igmp_mc_seq_fops = {
2254        .owner          =       THIS_MODULE,
2255        .open           =       igmp_mc_seq_open,
2256        .read           =       seq_read,
2257        .llseek         =       seq_lseek,
2258        .release        =       seq_release_private,
2259};
2260
2261struct igmp_mcf_iter_state {
2262        struct net_device *dev;
2263        struct in_device *idev;
2264        struct ip_mc_list *im;
2265};
2266
2267#define igmp_mcf_seq_private(seq)       ((struct igmp_mcf_iter_state *)(seq)->private)
2268
2269static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq)
2270{
2271        struct ip_sf_list *psf = NULL;
2272        struct ip_mc_list *im = NULL;
2273        struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2274
2275        for (state->dev = dev_base, state->idev = NULL, state->im = NULL;
2276             state->dev; 
2277             state->dev = state->dev->next) {
2278                struct in_device *idev;
2279                idev = in_dev_get(state->dev);
2280                if (unlikely(idev == NULL))
2281                        continue;
2282                read_lock_bh(&idev->lock);
2283                im = idev->mc_list;
2284                if (likely(im != NULL)) {
2285                        spin_lock_bh(&im->lock);
2286                        psf = im->sources;
2287                        if (likely(psf != NULL)) {
2288                                state->im = im;
2289                                state->idev = idev;
2290                                break;
2291                        }
2292                        spin_unlock_bh(&im->lock);
2293                }
2294                read_unlock_bh(&idev->lock);
2295                in_dev_put(idev);
2296        }
2297        return psf;
2298}
2299
2300static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf)
2301{
2302        struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2303
2304        psf = psf->sf_next;
2305        while (!psf) {
2306                spin_unlock_bh(&state->im->lock);
2307                state->im = state->im->next;
2308                while (!state->im) {
2309                        if (likely(state->idev != NULL)) {
2310                                read_unlock_bh(&state->idev->lock);
2311                                in_dev_put(state->idev);
2312                        }
2313                        state->dev = state->dev->next;
2314                        if (!state->dev) {
2315                                state->idev = NULL;
2316                                goto out;
2317                        }
2318                        state->idev = in_dev_get(state->dev);
2319                        if (!state->idev)
2320                                continue;
2321                        read_lock_bh(&state->idev->lock);
2322                        state->im = state->idev->mc_list;
2323                }
2324                if (!state->im)
2325                        break;
2326                spin_lock_bh(&state->im->lock);
2327                psf = state->im->sources;
2328        }
2329out:
2330        return psf;
2331}
2332
2333static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos)
2334{
2335        struct ip_sf_list *psf = igmp_mcf_get_first(seq);
2336        if (psf)
2337                while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL)
2338                        --pos;
2339        return pos ? NULL : psf;
2340}
2341
2342static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos)
2343{
2344        read_lock(&dev_base_lock);
2345        return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
2346}
2347
2348static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2349{
2350        struct ip_sf_list *psf;
2351        if (v == SEQ_START_TOKEN)
2352                psf = igmp_mcf_get_first(seq);
2353        else
2354                psf = igmp_mcf_get_next(seq, v);
2355        ++*pos;
2356        return psf;
2357}
2358
2359static void igmp_mcf_seq_stop(struct seq_file *seq, void *v)
2360{
2361        struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2362        if (likely(state->im != NULL)) {
2363                spin_unlock_bh(&state->im->lock);
2364                state->im = NULL;
2365        }
2366        if (likely(state->idev != NULL)) {
2367                read_unlock_bh(&state->idev->lock);
2368                in_dev_put(state->idev);
2369                state->idev = NULL;
2370        }
2371        state->dev = NULL;
2372        read_unlock(&dev_base_lock);
2373}
2374
2375static int igmp_mcf_seq_show(struct seq_file *seq, void *v)
2376{
2377        struct ip_sf_list *psf = (struct ip_sf_list *)v;
2378        struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq);
2379
2380        if (v == SEQ_START_TOKEN) {
2381                seq_printf(seq, 
2382                           "%3s %6s "
2383                           "%10s %10s %6s %6s\n", "Idx",
2384                           "Device", "MCA",
2385                           "SRC", "INC", "EXC");
2386        } else {
2387                seq_printf(seq,
2388                           "%3d %6.6s 0x%08x "
2389                           "0x%08x %6lu %6lu\n", 
2390                           state->dev->ifindex, state->dev->name, 
2391                           ntohl(state->im->multiaddr),
2392                           ntohl(psf->sf_inaddr),
2393                           psf->sf_count[MCAST_INCLUDE],
2394                           psf->sf_count[MCAST_EXCLUDE]);
2395        }
2396        return 0;
2397}
2398
2399static struct seq_operations igmp_mcf_seq_ops = {
2400        .start  =       igmp_mcf_seq_start,
2401        .next   =       igmp_mcf_seq_next,
2402        .stop   =       igmp_mcf_seq_stop,
2403        .show   =       igmp_mcf_seq_show,
2404};
2405
2406static int igmp_mcf_seq_open(struct inode *inode, struct file *file)
2407{
2408        struct seq_file *seq;
2409        int rc = -ENOMEM;
2410        struct igmp_mcf_iter_state *s = kmalloc(sizeof(*s), GFP_KERNEL);
2411
2412        if (!s)
2413                goto out;
2414        rc = seq_open(file, &igmp_mcf_seq_ops);
2415        if (rc)
2416                goto out_kfree;
2417
2418        seq = file->private_data;
2419        seq->private = s;
2420        memset(s, 0, sizeof(*s));
2421out:
2422        return rc;
2423out_kfree:
2424        kfree(s);
2425        goto out;
2426}
2427
2428static struct file_operations igmp_mcf_seq_fops = {
2429        .owner          =       THIS_MODULE,
2430        .open           =       igmp_mcf_seq_open,
2431        .read           =       seq_read,
2432        .llseek         =       seq_lseek,
2433        .release        =       seq_release_private,
2434};
2435
2436int __init igmp_mc_proc_init(void)
2437{
2438        proc_net_fops_create("igmp", S_IRUGO, &igmp_mc_seq_fops);
2439        proc_net_fops_create("mcfilter", S_IRUGO, &igmp_mcf_seq_fops);
2440        return 0;
2441}
2442#endif
2443
2444EXPORT_SYMBOL(ip_mc_dec_group);
2445EXPORT_SYMBOL(ip_mc_inc_group);
2446EXPORT_SYMBOL(ip_mc_join_group);
2447
lxr.linux.no kindly hosted by Redpill Linpro AS, provider of Linux consulting and operations services since 1995.