Kyle Swenson | 8d8f654 | 2021-03-15 11:02:55 -0600 | [diff] [blame^] | 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 | * Authors: |
| 12 | * Alan Cox <alan@lxorguk.ukuu.org.uk> |
| 13 | * |
| 14 | * This program is free software; you can redistribute it and/or |
| 15 | * modify it under the terms of the GNU General Public License |
| 16 | * as published by the Free Software Foundation; either version |
| 17 | * 2 of the License, or (at your option) any later version. |
| 18 | * |
| 19 | * Fixes: |
| 20 | * |
| 21 | * Alan Cox : Added lots of __inline__ to optimise |
| 22 | * the memory usage of all the tiny little |
| 23 | * functions. |
| 24 | * Alan Cox : Dumped the header building experiment. |
| 25 | * Alan Cox : Minor tweaks ready for multicast routing |
| 26 | * and extended IGMP protocol. |
| 27 | * Alan Cox : Removed a load of inline directives. Gcc 2.5.8 |
| 28 | * writes utterly bogus code otherwise (sigh) |
| 29 | * fixed IGMP loopback to behave in the manner |
| 30 | * desired by mrouted, fixed the fact it has been |
| 31 | * broken since 1.3.6 and cleaned up a few minor |
| 32 | * points. |
| 33 | * |
| 34 | * Chih-Jen Chang : Tried to revise IGMP to Version 2 |
| 35 | * Tsu-Sheng Tsao E-mail: chihjenc@scf.usc.edu and tsusheng@scf.usc.edu |
| 36 | * The enhancements are mainly based on Steve Deering's |
| 37 | * ipmulti-3.5 source code. |
| 38 | * Chih-Jen Chang : Added the igmp_get_mrouter_info and |
| 39 | * Tsu-Sheng Tsao igmp_set_mrouter_info to keep track of |
| 40 | * the mrouted version on that device. |
| 41 | * Chih-Jen Chang : Added the max_resp_time parameter to |
| 42 | * Tsu-Sheng Tsao igmp_heard_query(). Using this parameter |
| 43 | * to identify the multicast router version |
| 44 | * and do what the IGMP version 2 specified. |
| 45 | * Chih-Jen Chang : Added a timer to revert to IGMP V2 router |
| 46 | * Tsu-Sheng Tsao if the specified time expired. |
| 47 | * Alan Cox : Stop IGMP from 0.0.0.0 being accepted. |
| 48 | * Alan Cox : Use GFP_ATOMIC in the right places. |
| 49 | * Christian Daudt : igmp timer wasn't set for local group |
| 50 | * memberships but was being deleted, |
| 51 | * which caused a "del_timer() called |
| 52 | * from %p with timer not initialized\n" |
| 53 | * message (960131). |
| 54 | * Christian Daudt : removed del_timer from |
| 55 | * igmp_timer_expire function (960205). |
| 56 | * Christian Daudt : igmp_heard_report now only calls |
| 57 | * igmp_timer_expire if tm->running is |
| 58 | * true (960216). |
| 59 | * Malcolm Beattie : ttl comparison wrong in igmp_rcv made |
| 60 | * igmp_heard_query never trigger. Expiry |
| 61 | * miscalculation fixed in igmp_heard_query |
| 62 | * and random() made to return unsigned to |
| 63 | * prevent negative expiry times. |
| 64 | * Alexey Kuznetsov: Wrong group leaving behaviour, backport |
| 65 | * fix from pending 2.1.x patches. |
| 66 | * Alan Cox: Forget to enable FDDI support earlier. |
| 67 | * Alexey Kuznetsov: Fixed leaving groups on device down. |
| 68 | * Alexey Kuznetsov: Accordance to igmp-v2-06 draft. |
| 69 | * David L Stevens: IGMPv3 support, with help from |
| 70 | * Vinay Kulkarni |
| 71 | */ |
| 72 | |
| 73 | #include <linux/module.h> |
| 74 | #include <linux/slab.h> |
| 75 | #include <asm/uaccess.h> |
| 76 | #include <linux/types.h> |
| 77 | #include <linux/kernel.h> |
| 78 | #include <linux/jiffies.h> |
| 79 | #include <linux/string.h> |
| 80 | #include <linux/socket.h> |
| 81 | #include <linux/sockios.h> |
| 82 | #include <linux/in.h> |
| 83 | #include <linux/inet.h> |
| 84 | #include <linux/netdevice.h> |
| 85 | #include <linux/skbuff.h> |
| 86 | #include <linux/inetdevice.h> |
| 87 | #include <linux/igmp.h> |
| 88 | #include <linux/if_arp.h> |
| 89 | #include <linux/rtnetlink.h> |
| 90 | #include <linux/times.h> |
| 91 | #include <linux/pkt_sched.h> |
| 92 | |
| 93 | #include <net/net_namespace.h> |
| 94 | #include <net/arp.h> |
| 95 | #include <net/ip.h> |
| 96 | #include <net/protocol.h> |
| 97 | #include <net/route.h> |
| 98 | #include <net/sock.h> |
| 99 | #include <net/checksum.h> |
| 100 | #include <net/inet_common.h> |
| 101 | #include <linux/netfilter_ipv4.h> |
| 102 | #ifdef CONFIG_IP_MROUTE |
| 103 | #include <linux/mroute.h> |
| 104 | #endif |
| 105 | #ifdef CONFIG_PROC_FS |
| 106 | #include <linux/proc_fs.h> |
| 107 | #include <linux/seq_file.h> |
| 108 | #endif |
| 109 | |
| 110 | #define IP_MAX_MEMBERSHIPS 20 |
| 111 | #define IP_MAX_MSF 10 |
| 112 | |
| 113 | /* IGMP reports for link-local multicast groups are enabled by default */ |
| 114 | int sysctl_igmp_llm_reports __read_mostly = 1; |
| 115 | |
| 116 | #ifdef CONFIG_IP_MULTICAST |
| 117 | /* Parameter names and values are taken from igmp-v2-06 draft */ |
| 118 | |
| 119 | #define IGMP_V1_ROUTER_PRESENT_TIMEOUT (400*HZ) |
| 120 | #define IGMP_V2_ROUTER_PRESENT_TIMEOUT (400*HZ) |
| 121 | #define IGMP_V2_UNSOLICITED_REPORT_INTERVAL (10*HZ) |
| 122 | #define IGMP_V3_UNSOLICITED_REPORT_INTERVAL (1*HZ) |
| 123 | #define IGMP_QUERY_RESPONSE_INTERVAL (10*HZ) |
| 124 | #define IGMP_QUERY_ROBUSTNESS_VARIABLE 2 |
| 125 | |
| 126 | |
| 127 | #define IGMP_INITIAL_REPORT_DELAY (1) |
| 128 | |
| 129 | /* IGMP_INITIAL_REPORT_DELAY is not from IGMP specs! |
| 130 | * IGMP specs require to report membership immediately after |
| 131 | * joining a group, but we delay the first report by a |
| 132 | * small interval. It seems more natural and still does not |
| 133 | * contradict to specs provided this delay is small enough. |
| 134 | */ |
| 135 | |
| 136 | #define IGMP_V1_SEEN(in_dev) \ |
| 137 | (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 1 || \ |
| 138 | IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 1 || \ |
| 139 | ((in_dev)->mr_v1_seen && \ |
| 140 | time_before(jiffies, (in_dev)->mr_v1_seen))) |
| 141 | #define IGMP_V2_SEEN(in_dev) \ |
| 142 | (IPV4_DEVCONF_ALL(dev_net(in_dev->dev), FORCE_IGMP_VERSION) == 2 || \ |
| 143 | IN_DEV_CONF_GET((in_dev), FORCE_IGMP_VERSION) == 2 || \ |
| 144 | ((in_dev)->mr_v2_seen && \ |
| 145 | time_before(jiffies, (in_dev)->mr_v2_seen))) |
| 146 | |
| 147 | static int unsolicited_report_interval(struct in_device *in_dev) |
| 148 | { |
| 149 | int interval_ms, interval_jiffies; |
| 150 | |
| 151 | if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) |
| 152 | interval_ms = IN_DEV_CONF_GET( |
| 153 | in_dev, |
| 154 | IGMPV2_UNSOLICITED_REPORT_INTERVAL); |
| 155 | else /* v3 */ |
| 156 | interval_ms = IN_DEV_CONF_GET( |
| 157 | in_dev, |
| 158 | IGMPV3_UNSOLICITED_REPORT_INTERVAL); |
| 159 | |
| 160 | interval_jiffies = msecs_to_jiffies(interval_ms); |
| 161 | |
| 162 | /* _timer functions can't handle a delay of 0 jiffies so ensure |
| 163 | * we always return a positive value. |
| 164 | */ |
| 165 | if (interval_jiffies <= 0) |
| 166 | interval_jiffies = 1; |
| 167 | return interval_jiffies; |
| 168 | } |
| 169 | |
| 170 | static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im); |
| 171 | static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr); |
| 172 | static void igmpv3_clear_delrec(struct in_device *in_dev); |
| 173 | static int sf_setstate(struct ip_mc_list *pmc); |
| 174 | static void sf_markstate(struct ip_mc_list *pmc); |
| 175 | #endif |
| 176 | static void ip_mc_clear_src(struct ip_mc_list *pmc); |
| 177 | static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, |
| 178 | int sfcount, __be32 *psfsrc, int delta); |
| 179 | |
| 180 | static void ip_ma_put(struct ip_mc_list *im) |
| 181 | { |
| 182 | if (atomic_dec_and_test(&im->refcnt)) { |
| 183 | in_dev_put(im->interface); |
| 184 | kfree_rcu(im, rcu); |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | #define for_each_pmc_rcu(in_dev, pmc) \ |
| 189 | for (pmc = rcu_dereference(in_dev->mc_list); \ |
| 190 | pmc != NULL; \ |
| 191 | pmc = rcu_dereference(pmc->next_rcu)) |
| 192 | |
| 193 | #define for_each_pmc_rtnl(in_dev, pmc) \ |
| 194 | for (pmc = rtnl_dereference(in_dev->mc_list); \ |
| 195 | pmc != NULL; \ |
| 196 | pmc = rtnl_dereference(pmc->next_rcu)) |
| 197 | |
| 198 | #ifdef CONFIG_IP_MULTICAST |
| 199 | |
| 200 | /* |
| 201 | * Timer management |
| 202 | */ |
| 203 | |
| 204 | static void igmp_stop_timer(struct ip_mc_list *im) |
| 205 | { |
| 206 | spin_lock_bh(&im->lock); |
| 207 | if (del_timer(&im->timer)) |
| 208 | atomic_dec(&im->refcnt); |
| 209 | im->tm_running = 0; |
| 210 | im->reporter = 0; |
| 211 | im->unsolicit_count = 0; |
| 212 | spin_unlock_bh(&im->lock); |
| 213 | } |
| 214 | |
| 215 | /* It must be called with locked im->lock */ |
| 216 | static void igmp_start_timer(struct ip_mc_list *im, int max_delay) |
| 217 | { |
| 218 | int tv = prandom_u32() % max_delay; |
| 219 | |
| 220 | im->tm_running = 1; |
| 221 | if (!mod_timer(&im->timer, jiffies+tv+2)) |
| 222 | atomic_inc(&im->refcnt); |
| 223 | } |
| 224 | |
| 225 | static void igmp_gq_start_timer(struct in_device *in_dev) |
| 226 | { |
| 227 | int tv = prandom_u32() % in_dev->mr_maxdelay; |
| 228 | unsigned long exp = jiffies + tv + 2; |
| 229 | |
| 230 | if (in_dev->mr_gq_running && |
| 231 | time_after_eq(exp, (in_dev->mr_gq_timer).expires)) |
| 232 | return; |
| 233 | |
| 234 | in_dev->mr_gq_running = 1; |
| 235 | if (!mod_timer(&in_dev->mr_gq_timer, exp)) |
| 236 | in_dev_hold(in_dev); |
| 237 | } |
| 238 | |
| 239 | static void igmp_ifc_start_timer(struct in_device *in_dev, int delay) |
| 240 | { |
| 241 | int tv = prandom_u32() % delay; |
| 242 | |
| 243 | if (!mod_timer(&in_dev->mr_ifc_timer, jiffies+tv+2)) |
| 244 | in_dev_hold(in_dev); |
| 245 | } |
| 246 | |
| 247 | static void igmp_mod_timer(struct ip_mc_list *im, int max_delay) |
| 248 | { |
| 249 | spin_lock_bh(&im->lock); |
| 250 | im->unsolicit_count = 0; |
| 251 | if (del_timer(&im->timer)) { |
| 252 | if ((long)(im->timer.expires-jiffies) < max_delay) { |
| 253 | add_timer(&im->timer); |
| 254 | im->tm_running = 1; |
| 255 | spin_unlock_bh(&im->lock); |
| 256 | return; |
| 257 | } |
| 258 | atomic_dec(&im->refcnt); |
| 259 | } |
| 260 | igmp_start_timer(im, max_delay); |
| 261 | spin_unlock_bh(&im->lock); |
| 262 | } |
| 263 | |
| 264 | |
| 265 | /* |
| 266 | * Send an IGMP report. |
| 267 | */ |
| 268 | |
| 269 | #define IGMP_SIZE (sizeof(struct igmphdr)+sizeof(struct iphdr)+4) |
| 270 | |
| 271 | |
| 272 | static int is_in(struct ip_mc_list *pmc, struct ip_sf_list *psf, int type, |
| 273 | int gdeleted, int sdeleted) |
| 274 | { |
| 275 | switch (type) { |
| 276 | case IGMPV3_MODE_IS_INCLUDE: |
| 277 | case IGMPV3_MODE_IS_EXCLUDE: |
| 278 | if (gdeleted || sdeleted) |
| 279 | return 0; |
| 280 | if (!(pmc->gsquery && !psf->sf_gsresp)) { |
| 281 | if (pmc->sfmode == MCAST_INCLUDE) |
| 282 | return 1; |
| 283 | /* don't include if this source is excluded |
| 284 | * in all filters |
| 285 | */ |
| 286 | if (psf->sf_count[MCAST_INCLUDE]) |
| 287 | return type == IGMPV3_MODE_IS_INCLUDE; |
| 288 | return pmc->sfcount[MCAST_EXCLUDE] == |
| 289 | psf->sf_count[MCAST_EXCLUDE]; |
| 290 | } |
| 291 | return 0; |
| 292 | case IGMPV3_CHANGE_TO_INCLUDE: |
| 293 | if (gdeleted || sdeleted) |
| 294 | return 0; |
| 295 | return psf->sf_count[MCAST_INCLUDE] != 0; |
| 296 | case IGMPV3_CHANGE_TO_EXCLUDE: |
| 297 | if (gdeleted || sdeleted) |
| 298 | return 0; |
| 299 | if (pmc->sfcount[MCAST_EXCLUDE] == 0 || |
| 300 | psf->sf_count[MCAST_INCLUDE]) |
| 301 | return 0; |
| 302 | return pmc->sfcount[MCAST_EXCLUDE] == |
| 303 | psf->sf_count[MCAST_EXCLUDE]; |
| 304 | case IGMPV3_ALLOW_NEW_SOURCES: |
| 305 | if (gdeleted || !psf->sf_crcount) |
| 306 | return 0; |
| 307 | return (pmc->sfmode == MCAST_INCLUDE) ^ sdeleted; |
| 308 | case IGMPV3_BLOCK_OLD_SOURCES: |
| 309 | if (pmc->sfmode == MCAST_INCLUDE) |
| 310 | return gdeleted || (psf->sf_crcount && sdeleted); |
| 311 | return psf->sf_crcount && !gdeleted && !sdeleted; |
| 312 | } |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | static int |
| 317 | igmp_scount(struct ip_mc_list *pmc, int type, int gdeleted, int sdeleted) |
| 318 | { |
| 319 | struct ip_sf_list *psf; |
| 320 | int scount = 0; |
| 321 | |
| 322 | for (psf = pmc->sources; psf; psf = psf->sf_next) { |
| 323 | if (!is_in(pmc, psf, type, gdeleted, sdeleted)) |
| 324 | continue; |
| 325 | scount++; |
| 326 | } |
| 327 | return scount; |
| 328 | } |
| 329 | |
| 330 | static struct sk_buff *igmpv3_newpack(struct net_device *dev, unsigned int mtu) |
| 331 | { |
| 332 | struct sk_buff *skb; |
| 333 | struct rtable *rt; |
| 334 | struct iphdr *pip; |
| 335 | struct igmpv3_report *pig; |
| 336 | struct net *net = dev_net(dev); |
| 337 | struct flowi4 fl4; |
| 338 | int hlen = LL_RESERVED_SPACE(dev); |
| 339 | int tlen = dev->needed_tailroom; |
| 340 | unsigned int size = mtu; |
| 341 | |
| 342 | while (1) { |
| 343 | skb = alloc_skb(size + hlen + tlen, |
| 344 | GFP_ATOMIC | __GFP_NOWARN); |
| 345 | if (skb) |
| 346 | break; |
| 347 | size >>= 1; |
| 348 | if (size < 256) |
| 349 | return NULL; |
| 350 | } |
| 351 | skb->priority = TC_PRIO_CONTROL; |
| 352 | |
| 353 | rt = ip_route_output_ports(net, &fl4, NULL, IGMPV3_ALL_MCR, 0, |
| 354 | 0, 0, |
| 355 | IPPROTO_IGMP, 0, dev->ifindex); |
| 356 | if (IS_ERR(rt)) { |
| 357 | kfree_skb(skb); |
| 358 | return NULL; |
| 359 | } |
| 360 | |
| 361 | skb_dst_set(skb, &rt->dst); |
| 362 | skb->dev = dev; |
| 363 | |
| 364 | skb_reserve(skb, hlen); |
| 365 | skb_tailroom_reserve(skb, mtu, tlen); |
| 366 | |
| 367 | skb_reset_network_header(skb); |
| 368 | pip = ip_hdr(skb); |
| 369 | skb_put(skb, sizeof(struct iphdr) + 4); |
| 370 | |
| 371 | pip->version = 4; |
| 372 | pip->ihl = (sizeof(struct iphdr)+4)>>2; |
| 373 | pip->tos = 0xc0; |
| 374 | pip->frag_off = htons(IP_DF); |
| 375 | pip->ttl = 1; |
| 376 | pip->daddr = fl4.daddr; |
| 377 | pip->saddr = fl4.saddr; |
| 378 | pip->protocol = IPPROTO_IGMP; |
| 379 | pip->tot_len = 0; /* filled in later */ |
| 380 | ip_select_ident(net, skb, NULL); |
| 381 | ((u8 *)&pip[1])[0] = IPOPT_RA; |
| 382 | ((u8 *)&pip[1])[1] = 4; |
| 383 | ((u8 *)&pip[1])[2] = 0; |
| 384 | ((u8 *)&pip[1])[3] = 0; |
| 385 | |
| 386 | skb->transport_header = skb->network_header + sizeof(struct iphdr) + 4; |
| 387 | skb_put(skb, sizeof(*pig)); |
| 388 | pig = igmpv3_report_hdr(skb); |
| 389 | pig->type = IGMPV3_HOST_MEMBERSHIP_REPORT; |
| 390 | pig->resv1 = 0; |
| 391 | pig->csum = 0; |
| 392 | pig->resv2 = 0; |
| 393 | pig->ngrec = 0; |
| 394 | return skb; |
| 395 | } |
| 396 | |
| 397 | static int igmpv3_sendpack(struct sk_buff *skb) |
| 398 | { |
| 399 | struct igmphdr *pig = igmp_hdr(skb); |
| 400 | const int igmplen = skb_tail_pointer(skb) - skb_transport_header(skb); |
| 401 | |
| 402 | pig->csum = ip_compute_csum(igmp_hdr(skb), igmplen); |
| 403 | |
| 404 | return ip_local_out(dev_net(skb_dst(skb)->dev), skb->sk, skb); |
| 405 | } |
| 406 | |
| 407 | static int grec_size(struct ip_mc_list *pmc, int type, int gdel, int sdel) |
| 408 | { |
| 409 | return sizeof(struct igmpv3_grec) + 4*igmp_scount(pmc, type, gdel, sdel); |
| 410 | } |
| 411 | |
| 412 | static struct sk_buff *add_grhead(struct sk_buff *skb, struct ip_mc_list *pmc, |
| 413 | int type, struct igmpv3_grec **ppgr) |
| 414 | { |
| 415 | struct net_device *dev = pmc->interface->dev; |
| 416 | struct igmpv3_report *pih; |
| 417 | struct igmpv3_grec *pgr; |
| 418 | |
| 419 | if (!skb) |
| 420 | skb = igmpv3_newpack(dev, dev->mtu); |
| 421 | if (!skb) |
| 422 | return NULL; |
| 423 | pgr = (struct igmpv3_grec *)skb_put(skb, sizeof(struct igmpv3_grec)); |
| 424 | pgr->grec_type = type; |
| 425 | pgr->grec_auxwords = 0; |
| 426 | pgr->grec_nsrcs = 0; |
| 427 | pgr->grec_mca = pmc->multiaddr; |
| 428 | pih = igmpv3_report_hdr(skb); |
| 429 | pih->ngrec = htons(ntohs(pih->ngrec)+1); |
| 430 | *ppgr = pgr; |
| 431 | return skb; |
| 432 | } |
| 433 | |
| 434 | #define AVAILABLE(skb) ((skb) ? skb_availroom(skb) : 0) |
| 435 | |
| 436 | static struct sk_buff *add_grec(struct sk_buff *skb, struct ip_mc_list *pmc, |
| 437 | int type, int gdeleted, int sdeleted) |
| 438 | { |
| 439 | struct net_device *dev = pmc->interface->dev; |
| 440 | struct igmpv3_report *pih; |
| 441 | struct igmpv3_grec *pgr = NULL; |
| 442 | struct ip_sf_list *psf, *psf_next, *psf_prev, **psf_list; |
| 443 | int scount, stotal, first, isquery, truncate; |
| 444 | |
| 445 | if (pmc->multiaddr == IGMP_ALL_HOSTS) |
| 446 | return skb; |
| 447 | if (ipv4_is_local_multicast(pmc->multiaddr) && !sysctl_igmp_llm_reports) |
| 448 | return skb; |
| 449 | |
| 450 | isquery = type == IGMPV3_MODE_IS_INCLUDE || |
| 451 | type == IGMPV3_MODE_IS_EXCLUDE; |
| 452 | truncate = type == IGMPV3_MODE_IS_EXCLUDE || |
| 453 | type == IGMPV3_CHANGE_TO_EXCLUDE; |
| 454 | |
| 455 | stotal = scount = 0; |
| 456 | |
| 457 | psf_list = sdeleted ? &pmc->tomb : &pmc->sources; |
| 458 | |
| 459 | if (!*psf_list) |
| 460 | goto empty_source; |
| 461 | |
| 462 | pih = skb ? igmpv3_report_hdr(skb) : NULL; |
| 463 | |
| 464 | /* EX and TO_EX get a fresh packet, if needed */ |
| 465 | if (truncate) { |
| 466 | if (pih && pih->ngrec && |
| 467 | AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) { |
| 468 | if (skb) |
| 469 | igmpv3_sendpack(skb); |
| 470 | skb = igmpv3_newpack(dev, dev->mtu); |
| 471 | } |
| 472 | } |
| 473 | first = 1; |
| 474 | psf_prev = NULL; |
| 475 | for (psf = *psf_list; psf; psf = psf_next) { |
| 476 | __be32 *psrc; |
| 477 | |
| 478 | psf_next = psf->sf_next; |
| 479 | |
| 480 | if (!is_in(pmc, psf, type, gdeleted, sdeleted)) { |
| 481 | psf_prev = psf; |
| 482 | continue; |
| 483 | } |
| 484 | |
| 485 | /* clear marks on query responses */ |
| 486 | if (isquery) |
| 487 | psf->sf_gsresp = 0; |
| 488 | |
| 489 | if (AVAILABLE(skb) < sizeof(__be32) + |
| 490 | first*sizeof(struct igmpv3_grec)) { |
| 491 | if (truncate && !first) |
| 492 | break; /* truncate these */ |
| 493 | if (pgr) |
| 494 | pgr->grec_nsrcs = htons(scount); |
| 495 | if (skb) |
| 496 | igmpv3_sendpack(skb); |
| 497 | skb = igmpv3_newpack(dev, dev->mtu); |
| 498 | first = 1; |
| 499 | scount = 0; |
| 500 | } |
| 501 | if (first) { |
| 502 | skb = add_grhead(skb, pmc, type, &pgr); |
| 503 | first = 0; |
| 504 | } |
| 505 | if (!skb) |
| 506 | return NULL; |
| 507 | psrc = (__be32 *)skb_put(skb, sizeof(__be32)); |
| 508 | *psrc = psf->sf_inaddr; |
| 509 | scount++; stotal++; |
| 510 | if ((type == IGMPV3_ALLOW_NEW_SOURCES || |
| 511 | type == IGMPV3_BLOCK_OLD_SOURCES) && psf->sf_crcount) { |
| 512 | psf->sf_crcount--; |
| 513 | if ((sdeleted || gdeleted) && psf->sf_crcount == 0) { |
| 514 | if (psf_prev) |
| 515 | psf_prev->sf_next = psf->sf_next; |
| 516 | else |
| 517 | *psf_list = psf->sf_next; |
| 518 | kfree(psf); |
| 519 | continue; |
| 520 | } |
| 521 | } |
| 522 | psf_prev = psf; |
| 523 | } |
| 524 | |
| 525 | empty_source: |
| 526 | if (!stotal) { |
| 527 | if (type == IGMPV3_ALLOW_NEW_SOURCES || |
| 528 | type == IGMPV3_BLOCK_OLD_SOURCES) |
| 529 | return skb; |
| 530 | if (pmc->crcount || isquery) { |
| 531 | /* make sure we have room for group header */ |
| 532 | if (skb && AVAILABLE(skb) < sizeof(struct igmpv3_grec)) { |
| 533 | igmpv3_sendpack(skb); |
| 534 | skb = NULL; /* add_grhead will get a new one */ |
| 535 | } |
| 536 | skb = add_grhead(skb, pmc, type, &pgr); |
| 537 | } |
| 538 | } |
| 539 | if (pgr) |
| 540 | pgr->grec_nsrcs = htons(scount); |
| 541 | |
| 542 | if (isquery) |
| 543 | pmc->gsquery = 0; /* clear query state on report */ |
| 544 | return skb; |
| 545 | } |
| 546 | |
| 547 | static int igmpv3_send_report(struct in_device *in_dev, struct ip_mc_list *pmc) |
| 548 | { |
| 549 | struct sk_buff *skb = NULL; |
| 550 | int type; |
| 551 | |
| 552 | if (!pmc) { |
| 553 | rcu_read_lock(); |
| 554 | for_each_pmc_rcu(in_dev, pmc) { |
| 555 | if (pmc->multiaddr == IGMP_ALL_HOSTS) |
| 556 | continue; |
| 557 | if (ipv4_is_local_multicast(pmc->multiaddr) && |
| 558 | !sysctl_igmp_llm_reports) |
| 559 | continue; |
| 560 | spin_lock_bh(&pmc->lock); |
| 561 | if (pmc->sfcount[MCAST_EXCLUDE]) |
| 562 | type = IGMPV3_MODE_IS_EXCLUDE; |
| 563 | else |
| 564 | type = IGMPV3_MODE_IS_INCLUDE; |
| 565 | skb = add_grec(skb, pmc, type, 0, 0); |
| 566 | spin_unlock_bh(&pmc->lock); |
| 567 | } |
| 568 | rcu_read_unlock(); |
| 569 | } else { |
| 570 | spin_lock_bh(&pmc->lock); |
| 571 | if (pmc->sfcount[MCAST_EXCLUDE]) |
| 572 | type = IGMPV3_MODE_IS_EXCLUDE; |
| 573 | else |
| 574 | type = IGMPV3_MODE_IS_INCLUDE; |
| 575 | skb = add_grec(skb, pmc, type, 0, 0); |
| 576 | spin_unlock_bh(&pmc->lock); |
| 577 | } |
| 578 | if (!skb) |
| 579 | return 0; |
| 580 | return igmpv3_sendpack(skb); |
| 581 | } |
| 582 | |
| 583 | /* |
| 584 | * remove zero-count source records from a source filter list |
| 585 | */ |
| 586 | static void igmpv3_clear_zeros(struct ip_sf_list **ppsf) |
| 587 | { |
| 588 | struct ip_sf_list *psf_prev, *psf_next, *psf; |
| 589 | |
| 590 | psf_prev = NULL; |
| 591 | for (psf = *ppsf; psf; psf = psf_next) { |
| 592 | psf_next = psf->sf_next; |
| 593 | if (psf->sf_crcount == 0) { |
| 594 | if (psf_prev) |
| 595 | psf_prev->sf_next = psf->sf_next; |
| 596 | else |
| 597 | *ppsf = psf->sf_next; |
| 598 | kfree(psf); |
| 599 | } else |
| 600 | psf_prev = psf; |
| 601 | } |
| 602 | } |
| 603 | |
| 604 | static void igmpv3_send_cr(struct in_device *in_dev) |
| 605 | { |
| 606 | struct ip_mc_list *pmc, *pmc_prev, *pmc_next; |
| 607 | struct sk_buff *skb = NULL; |
| 608 | int type, dtype; |
| 609 | |
| 610 | rcu_read_lock(); |
| 611 | spin_lock_bh(&in_dev->mc_tomb_lock); |
| 612 | |
| 613 | /* deleted MCA's */ |
| 614 | pmc_prev = NULL; |
| 615 | for (pmc = in_dev->mc_tomb; pmc; pmc = pmc_next) { |
| 616 | pmc_next = pmc->next; |
| 617 | if (pmc->sfmode == MCAST_INCLUDE) { |
| 618 | type = IGMPV3_BLOCK_OLD_SOURCES; |
| 619 | dtype = IGMPV3_BLOCK_OLD_SOURCES; |
| 620 | skb = add_grec(skb, pmc, type, 1, 0); |
| 621 | skb = add_grec(skb, pmc, dtype, 1, 1); |
| 622 | } |
| 623 | if (pmc->crcount) { |
| 624 | if (pmc->sfmode == MCAST_EXCLUDE) { |
| 625 | type = IGMPV3_CHANGE_TO_INCLUDE; |
| 626 | skb = add_grec(skb, pmc, type, 1, 0); |
| 627 | } |
| 628 | pmc->crcount--; |
| 629 | if (pmc->crcount == 0) { |
| 630 | igmpv3_clear_zeros(&pmc->tomb); |
| 631 | igmpv3_clear_zeros(&pmc->sources); |
| 632 | } |
| 633 | } |
| 634 | if (pmc->crcount == 0 && !pmc->tomb && !pmc->sources) { |
| 635 | if (pmc_prev) |
| 636 | pmc_prev->next = pmc_next; |
| 637 | else |
| 638 | in_dev->mc_tomb = pmc_next; |
| 639 | in_dev_put(pmc->interface); |
| 640 | kfree(pmc); |
| 641 | } else |
| 642 | pmc_prev = pmc; |
| 643 | } |
| 644 | spin_unlock_bh(&in_dev->mc_tomb_lock); |
| 645 | |
| 646 | /* change recs */ |
| 647 | for_each_pmc_rcu(in_dev, pmc) { |
| 648 | spin_lock_bh(&pmc->lock); |
| 649 | if (pmc->sfcount[MCAST_EXCLUDE]) { |
| 650 | type = IGMPV3_BLOCK_OLD_SOURCES; |
| 651 | dtype = IGMPV3_ALLOW_NEW_SOURCES; |
| 652 | } else { |
| 653 | type = IGMPV3_ALLOW_NEW_SOURCES; |
| 654 | dtype = IGMPV3_BLOCK_OLD_SOURCES; |
| 655 | } |
| 656 | skb = add_grec(skb, pmc, type, 0, 0); |
| 657 | skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */ |
| 658 | |
| 659 | /* filter mode changes */ |
| 660 | if (pmc->crcount) { |
| 661 | if (pmc->sfmode == MCAST_EXCLUDE) |
| 662 | type = IGMPV3_CHANGE_TO_EXCLUDE; |
| 663 | else |
| 664 | type = IGMPV3_CHANGE_TO_INCLUDE; |
| 665 | skb = add_grec(skb, pmc, type, 0, 0); |
| 666 | pmc->crcount--; |
| 667 | } |
| 668 | spin_unlock_bh(&pmc->lock); |
| 669 | } |
| 670 | rcu_read_unlock(); |
| 671 | |
| 672 | if (!skb) |
| 673 | return; |
| 674 | (void) igmpv3_sendpack(skb); |
| 675 | } |
| 676 | |
| 677 | static int igmp_send_report(struct in_device *in_dev, struct ip_mc_list *pmc, |
| 678 | int type) |
| 679 | { |
| 680 | struct sk_buff *skb; |
| 681 | struct iphdr *iph; |
| 682 | struct igmphdr *ih; |
| 683 | struct rtable *rt; |
| 684 | struct net_device *dev = in_dev->dev; |
| 685 | struct net *net = dev_net(dev); |
| 686 | __be32 group = pmc ? pmc->multiaddr : 0; |
| 687 | struct flowi4 fl4; |
| 688 | __be32 dst; |
| 689 | int hlen, tlen; |
| 690 | |
| 691 | if (type == IGMPV3_HOST_MEMBERSHIP_REPORT) |
| 692 | return igmpv3_send_report(in_dev, pmc); |
| 693 | |
| 694 | if (ipv4_is_local_multicast(group) && !sysctl_igmp_llm_reports) |
| 695 | return 0; |
| 696 | |
| 697 | if (type == IGMP_HOST_LEAVE_MESSAGE) |
| 698 | dst = IGMP_ALL_ROUTER; |
| 699 | else |
| 700 | dst = group; |
| 701 | |
| 702 | rt = ip_route_output_ports(net, &fl4, NULL, dst, 0, |
| 703 | 0, 0, |
| 704 | IPPROTO_IGMP, 0, dev->ifindex); |
| 705 | if (IS_ERR(rt)) |
| 706 | return -1; |
| 707 | |
| 708 | hlen = LL_RESERVED_SPACE(dev); |
| 709 | tlen = dev->needed_tailroom; |
| 710 | skb = alloc_skb(IGMP_SIZE + hlen + tlen, GFP_ATOMIC); |
| 711 | if (!skb) { |
| 712 | ip_rt_put(rt); |
| 713 | return -1; |
| 714 | } |
| 715 | skb->priority = TC_PRIO_CONTROL; |
| 716 | |
| 717 | skb_dst_set(skb, &rt->dst); |
| 718 | |
| 719 | skb_reserve(skb, hlen); |
| 720 | |
| 721 | skb_reset_network_header(skb); |
| 722 | iph = ip_hdr(skb); |
| 723 | skb_put(skb, sizeof(struct iphdr) + 4); |
| 724 | |
| 725 | iph->version = 4; |
| 726 | iph->ihl = (sizeof(struct iphdr)+4)>>2; |
| 727 | iph->tos = 0xc0; |
| 728 | iph->frag_off = htons(IP_DF); |
| 729 | iph->ttl = 1; |
| 730 | iph->daddr = dst; |
| 731 | iph->saddr = fl4.saddr; |
| 732 | iph->protocol = IPPROTO_IGMP; |
| 733 | ip_select_ident(net, skb, NULL); |
| 734 | ((u8 *)&iph[1])[0] = IPOPT_RA; |
| 735 | ((u8 *)&iph[1])[1] = 4; |
| 736 | ((u8 *)&iph[1])[2] = 0; |
| 737 | ((u8 *)&iph[1])[3] = 0; |
| 738 | |
| 739 | ih = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr)); |
| 740 | ih->type = type; |
| 741 | ih->code = 0; |
| 742 | ih->csum = 0; |
| 743 | ih->group = group; |
| 744 | ih->csum = ip_compute_csum((void *)ih, sizeof(struct igmphdr)); |
| 745 | |
| 746 | return ip_local_out(net, skb->sk, skb); |
| 747 | } |
| 748 | |
| 749 | static void igmp_gq_timer_expire(unsigned long data) |
| 750 | { |
| 751 | struct in_device *in_dev = (struct in_device *)data; |
| 752 | |
| 753 | in_dev->mr_gq_running = 0; |
| 754 | igmpv3_send_report(in_dev, NULL); |
| 755 | in_dev_put(in_dev); |
| 756 | } |
| 757 | |
| 758 | static void igmp_ifc_timer_expire(unsigned long data) |
| 759 | { |
| 760 | struct in_device *in_dev = (struct in_device *)data; |
| 761 | |
| 762 | igmpv3_send_cr(in_dev); |
| 763 | if (in_dev->mr_ifc_count) { |
| 764 | in_dev->mr_ifc_count--; |
| 765 | igmp_ifc_start_timer(in_dev, |
| 766 | unsolicited_report_interval(in_dev)); |
| 767 | } |
| 768 | in_dev_put(in_dev); |
| 769 | } |
| 770 | |
| 771 | static void igmp_ifc_event(struct in_device *in_dev) |
| 772 | { |
| 773 | if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) |
| 774 | return; |
| 775 | in_dev->mr_ifc_count = in_dev->mr_qrv ?: sysctl_igmp_qrv; |
| 776 | igmp_ifc_start_timer(in_dev, 1); |
| 777 | } |
| 778 | |
| 779 | |
| 780 | static void igmp_timer_expire(unsigned long data) |
| 781 | { |
| 782 | struct ip_mc_list *im = (struct ip_mc_list *)data; |
| 783 | struct in_device *in_dev = im->interface; |
| 784 | |
| 785 | spin_lock(&im->lock); |
| 786 | im->tm_running = 0; |
| 787 | |
| 788 | if (im->unsolicit_count) { |
| 789 | im->unsolicit_count--; |
| 790 | igmp_start_timer(im, unsolicited_report_interval(in_dev)); |
| 791 | } |
| 792 | im->reporter = 1; |
| 793 | spin_unlock(&im->lock); |
| 794 | |
| 795 | if (IGMP_V1_SEEN(in_dev)) |
| 796 | igmp_send_report(in_dev, im, IGMP_HOST_MEMBERSHIP_REPORT); |
| 797 | else if (IGMP_V2_SEEN(in_dev)) |
| 798 | igmp_send_report(in_dev, im, IGMPV2_HOST_MEMBERSHIP_REPORT); |
| 799 | else |
| 800 | igmp_send_report(in_dev, im, IGMPV3_HOST_MEMBERSHIP_REPORT); |
| 801 | |
| 802 | ip_ma_put(im); |
| 803 | } |
| 804 | |
| 805 | /* mark EXCLUDE-mode sources */ |
| 806 | static int igmp_xmarksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) |
| 807 | { |
| 808 | struct ip_sf_list *psf; |
| 809 | int i, scount; |
| 810 | |
| 811 | scount = 0; |
| 812 | for (psf = pmc->sources; psf; psf = psf->sf_next) { |
| 813 | if (scount == nsrcs) |
| 814 | break; |
| 815 | for (i = 0; i < nsrcs; i++) { |
| 816 | /* skip inactive filters */ |
| 817 | if (psf->sf_count[MCAST_INCLUDE] || |
| 818 | pmc->sfcount[MCAST_EXCLUDE] != |
| 819 | psf->sf_count[MCAST_EXCLUDE]) |
| 820 | break; |
| 821 | if (srcs[i] == psf->sf_inaddr) { |
| 822 | scount++; |
| 823 | break; |
| 824 | } |
| 825 | } |
| 826 | } |
| 827 | pmc->gsquery = 0; |
| 828 | if (scount == nsrcs) /* all sources excluded */ |
| 829 | return 0; |
| 830 | return 1; |
| 831 | } |
| 832 | |
| 833 | static int igmp_marksources(struct ip_mc_list *pmc, int nsrcs, __be32 *srcs) |
| 834 | { |
| 835 | struct ip_sf_list *psf; |
| 836 | int i, scount; |
| 837 | |
| 838 | if (pmc->sfmode == MCAST_EXCLUDE) |
| 839 | return igmp_xmarksources(pmc, nsrcs, srcs); |
| 840 | |
| 841 | /* mark INCLUDE-mode sources */ |
| 842 | scount = 0; |
| 843 | for (psf = pmc->sources; psf; psf = psf->sf_next) { |
| 844 | if (scount == nsrcs) |
| 845 | break; |
| 846 | for (i = 0; i < nsrcs; i++) |
| 847 | if (srcs[i] == psf->sf_inaddr) { |
| 848 | psf->sf_gsresp = 1; |
| 849 | scount++; |
| 850 | break; |
| 851 | } |
| 852 | } |
| 853 | if (!scount) { |
| 854 | pmc->gsquery = 0; |
| 855 | return 0; |
| 856 | } |
| 857 | pmc->gsquery = 1; |
| 858 | return 1; |
| 859 | } |
| 860 | |
| 861 | /* return true if packet was dropped */ |
| 862 | static bool igmp_heard_report(struct in_device *in_dev, __be32 group) |
| 863 | { |
| 864 | struct ip_mc_list *im; |
| 865 | |
| 866 | /* Timers are only set for non-local groups */ |
| 867 | |
| 868 | if (group == IGMP_ALL_HOSTS) |
| 869 | return false; |
| 870 | if (ipv4_is_local_multicast(group) && !sysctl_igmp_llm_reports) |
| 871 | return false; |
| 872 | |
| 873 | rcu_read_lock(); |
| 874 | for_each_pmc_rcu(in_dev, im) { |
| 875 | if (im->multiaddr == group) { |
| 876 | igmp_stop_timer(im); |
| 877 | break; |
| 878 | } |
| 879 | } |
| 880 | rcu_read_unlock(); |
| 881 | return false; |
| 882 | } |
| 883 | |
| 884 | /* return true if packet was dropped */ |
| 885 | static bool igmp_heard_query(struct in_device *in_dev, struct sk_buff *skb, |
| 886 | int len) |
| 887 | { |
| 888 | struct igmphdr *ih = igmp_hdr(skb); |
| 889 | struct igmpv3_query *ih3 = igmpv3_query_hdr(skb); |
| 890 | struct ip_mc_list *im; |
| 891 | __be32 group = ih->group; |
| 892 | int max_delay; |
| 893 | int mark = 0; |
| 894 | |
| 895 | |
| 896 | if (len == 8) { |
| 897 | if (ih->code == 0) { |
| 898 | /* Alas, old v1 router presents here. */ |
| 899 | |
| 900 | max_delay = IGMP_QUERY_RESPONSE_INTERVAL; |
| 901 | in_dev->mr_v1_seen = jiffies + |
| 902 | IGMP_V1_ROUTER_PRESENT_TIMEOUT; |
| 903 | group = 0; |
| 904 | } else { |
| 905 | /* v2 router present */ |
| 906 | max_delay = ih->code*(HZ/IGMP_TIMER_SCALE); |
| 907 | in_dev->mr_v2_seen = jiffies + |
| 908 | IGMP_V2_ROUTER_PRESENT_TIMEOUT; |
| 909 | } |
| 910 | /* cancel the interface change timer */ |
| 911 | in_dev->mr_ifc_count = 0; |
| 912 | if (del_timer(&in_dev->mr_ifc_timer)) |
| 913 | __in_dev_put(in_dev); |
| 914 | /* clear deleted report items */ |
| 915 | igmpv3_clear_delrec(in_dev); |
| 916 | } else if (len < 12) { |
| 917 | return true; /* ignore bogus packet; freed by caller */ |
| 918 | } else if (IGMP_V1_SEEN(in_dev)) { |
| 919 | /* This is a v3 query with v1 queriers present */ |
| 920 | max_delay = IGMP_QUERY_RESPONSE_INTERVAL; |
| 921 | group = 0; |
| 922 | } else if (IGMP_V2_SEEN(in_dev)) { |
| 923 | /* this is a v3 query with v2 queriers present; |
| 924 | * Interpretation of the max_delay code is problematic here. |
| 925 | * A real v2 host would use ih_code directly, while v3 has a |
| 926 | * different encoding. We use the v3 encoding as more likely |
| 927 | * to be intended in a v3 query. |
| 928 | */ |
| 929 | max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); |
| 930 | if (!max_delay) |
| 931 | max_delay = 1; /* can't mod w/ 0 */ |
| 932 | } else { /* v3 */ |
| 933 | if (!pskb_may_pull(skb, sizeof(struct igmpv3_query))) |
| 934 | return true; |
| 935 | |
| 936 | ih3 = igmpv3_query_hdr(skb); |
| 937 | if (ih3->nsrcs) { |
| 938 | if (!pskb_may_pull(skb, sizeof(struct igmpv3_query) |
| 939 | + ntohs(ih3->nsrcs)*sizeof(__be32))) |
| 940 | return true; |
| 941 | ih3 = igmpv3_query_hdr(skb); |
| 942 | } |
| 943 | |
| 944 | max_delay = IGMPV3_MRC(ih3->code)*(HZ/IGMP_TIMER_SCALE); |
| 945 | if (!max_delay) |
| 946 | max_delay = 1; /* can't mod w/ 0 */ |
| 947 | in_dev->mr_maxdelay = max_delay; |
| 948 | if (ih3->qrv) |
| 949 | in_dev->mr_qrv = ih3->qrv; |
| 950 | if (!group) { /* general query */ |
| 951 | if (ih3->nsrcs) |
| 952 | return true; /* no sources allowed */ |
| 953 | igmp_gq_start_timer(in_dev); |
| 954 | return false; |
| 955 | } |
| 956 | /* mark sources to include, if group & source-specific */ |
| 957 | mark = ih3->nsrcs != 0; |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * - Start the timers in all of our membership records |
| 962 | * that the query applies to for the interface on |
| 963 | * which the query arrived excl. those that belong |
| 964 | * to a "local" group (224.0.0.X) |
| 965 | * - For timers already running check if they need to |
| 966 | * be reset. |
| 967 | * - Use the igmp->igmp_code field as the maximum |
| 968 | * delay possible |
| 969 | */ |
| 970 | rcu_read_lock(); |
| 971 | for_each_pmc_rcu(in_dev, im) { |
| 972 | int changed; |
| 973 | |
| 974 | if (group && group != im->multiaddr) |
| 975 | continue; |
| 976 | if (im->multiaddr == IGMP_ALL_HOSTS) |
| 977 | continue; |
| 978 | if (ipv4_is_local_multicast(im->multiaddr) && |
| 979 | !sysctl_igmp_llm_reports) |
| 980 | continue; |
| 981 | spin_lock_bh(&im->lock); |
| 982 | if (im->tm_running) |
| 983 | im->gsquery = im->gsquery && mark; |
| 984 | else |
| 985 | im->gsquery = mark; |
| 986 | changed = !im->gsquery || |
| 987 | igmp_marksources(im, ntohs(ih3->nsrcs), ih3->srcs); |
| 988 | spin_unlock_bh(&im->lock); |
| 989 | if (changed) |
| 990 | igmp_mod_timer(im, max_delay); |
| 991 | } |
| 992 | rcu_read_unlock(); |
| 993 | return false; |
| 994 | } |
| 995 | |
| 996 | /* called in rcu_read_lock() section */ |
| 997 | int igmp_rcv(struct sk_buff *skb) |
| 998 | { |
| 999 | /* This basically follows the spec line by line -- see RFC1112 */ |
| 1000 | struct igmphdr *ih; |
| 1001 | struct in_device *in_dev = __in_dev_get_rcu(skb->dev); |
| 1002 | int len = skb->len; |
| 1003 | bool dropped = true; |
| 1004 | |
| 1005 | if (!in_dev) |
| 1006 | goto drop; |
| 1007 | |
| 1008 | if (!pskb_may_pull(skb, sizeof(struct igmphdr))) |
| 1009 | goto drop; |
| 1010 | |
| 1011 | if (skb_checksum_simple_validate(skb)) |
| 1012 | goto drop; |
| 1013 | |
| 1014 | ih = igmp_hdr(skb); |
| 1015 | switch (ih->type) { |
| 1016 | case IGMP_HOST_MEMBERSHIP_QUERY: |
| 1017 | dropped = igmp_heard_query(in_dev, skb, len); |
| 1018 | break; |
| 1019 | case IGMP_HOST_MEMBERSHIP_REPORT: |
| 1020 | case IGMPV2_HOST_MEMBERSHIP_REPORT: |
| 1021 | /* Is it our report looped back? */ |
| 1022 | if (rt_is_output_route(skb_rtable(skb))) |
| 1023 | break; |
| 1024 | /* don't rely on MC router hearing unicast reports */ |
| 1025 | if (skb->pkt_type == PACKET_MULTICAST || |
| 1026 | skb->pkt_type == PACKET_BROADCAST) |
| 1027 | dropped = igmp_heard_report(in_dev, ih->group); |
| 1028 | break; |
| 1029 | case IGMP_PIM: |
| 1030 | #ifdef CONFIG_IP_PIMSM_V1 |
| 1031 | return pim_rcv_v1(skb); |
| 1032 | #endif |
| 1033 | case IGMPV3_HOST_MEMBERSHIP_REPORT: |
| 1034 | case IGMP_DVMRP: |
| 1035 | case IGMP_TRACE: |
| 1036 | case IGMP_HOST_LEAVE_MESSAGE: |
| 1037 | case IGMP_MTRACE: |
| 1038 | case IGMP_MTRACE_RESP: |
| 1039 | break; |
| 1040 | default: |
| 1041 | break; |
| 1042 | } |
| 1043 | |
| 1044 | drop: |
| 1045 | if (dropped) |
| 1046 | kfree_skb(skb); |
| 1047 | else |
| 1048 | consume_skb(skb); |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
| 1052 | #endif |
| 1053 | |
| 1054 | |
| 1055 | /* |
| 1056 | * Add a filter to a device |
| 1057 | */ |
| 1058 | |
| 1059 | static void ip_mc_filter_add(struct in_device *in_dev, __be32 addr) |
| 1060 | { |
| 1061 | char buf[MAX_ADDR_LEN]; |
| 1062 | struct net_device *dev = in_dev->dev; |
| 1063 | |
| 1064 | /* Checking for IFF_MULTICAST here is WRONG-WRONG-WRONG. |
| 1065 | We will get multicast token leakage, when IFF_MULTICAST |
| 1066 | is changed. This check should be done in ndo_set_rx_mode |
| 1067 | routine. Something sort of: |
| 1068 | if (dev->mc_list && dev->flags&IFF_MULTICAST) { do it; } |
| 1069 | --ANK |
| 1070 | */ |
| 1071 | if (arp_mc_map(addr, buf, dev, 0) == 0) |
| 1072 | dev_mc_add(dev, buf); |
| 1073 | } |
| 1074 | |
| 1075 | /* |
| 1076 | * Remove a filter from a device |
| 1077 | */ |
| 1078 | |
| 1079 | static void ip_mc_filter_del(struct in_device *in_dev, __be32 addr) |
| 1080 | { |
| 1081 | char buf[MAX_ADDR_LEN]; |
| 1082 | struct net_device *dev = in_dev->dev; |
| 1083 | |
| 1084 | if (arp_mc_map(addr, buf, dev, 0) == 0) |
| 1085 | dev_mc_del(dev, buf); |
| 1086 | } |
| 1087 | |
| 1088 | #ifdef CONFIG_IP_MULTICAST |
| 1089 | /* |
| 1090 | * deleted ip_mc_list manipulation |
| 1091 | */ |
| 1092 | static void igmpv3_add_delrec(struct in_device *in_dev, struct ip_mc_list *im) |
| 1093 | { |
| 1094 | struct ip_mc_list *pmc; |
| 1095 | |
| 1096 | /* this is an "ip_mc_list" for convenience; only the fields below |
| 1097 | * are actually used. In particular, the refcnt and users are not |
| 1098 | * used for management of the delete list. Using the same structure |
| 1099 | * for deleted items allows change reports to use common code with |
| 1100 | * non-deleted or query-response MCA's. |
| 1101 | */ |
| 1102 | pmc = kzalloc(sizeof(*pmc), GFP_KERNEL); |
| 1103 | if (!pmc) |
| 1104 | return; |
| 1105 | spin_lock_init(&pmc->lock); |
| 1106 | spin_lock_bh(&im->lock); |
| 1107 | pmc->interface = im->interface; |
| 1108 | in_dev_hold(in_dev); |
| 1109 | pmc->multiaddr = im->multiaddr; |
| 1110 | pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv; |
| 1111 | pmc->sfmode = im->sfmode; |
| 1112 | if (pmc->sfmode == MCAST_INCLUDE) { |
| 1113 | struct ip_sf_list *psf; |
| 1114 | |
| 1115 | pmc->tomb = im->tomb; |
| 1116 | pmc->sources = im->sources; |
| 1117 | im->tomb = im->sources = NULL; |
| 1118 | for (psf = pmc->sources; psf; psf = psf->sf_next) |
| 1119 | psf->sf_crcount = pmc->crcount; |
| 1120 | } |
| 1121 | spin_unlock_bh(&im->lock); |
| 1122 | |
| 1123 | spin_lock_bh(&in_dev->mc_tomb_lock); |
| 1124 | pmc->next = in_dev->mc_tomb; |
| 1125 | in_dev->mc_tomb = pmc; |
| 1126 | spin_unlock_bh(&in_dev->mc_tomb_lock); |
| 1127 | } |
| 1128 | |
| 1129 | static void igmpv3_del_delrec(struct in_device *in_dev, __be32 multiaddr) |
| 1130 | { |
| 1131 | struct ip_mc_list *pmc, *pmc_prev; |
| 1132 | struct ip_sf_list *psf, *psf_next; |
| 1133 | |
| 1134 | spin_lock_bh(&in_dev->mc_tomb_lock); |
| 1135 | pmc_prev = NULL; |
| 1136 | for (pmc = in_dev->mc_tomb; pmc; pmc = pmc->next) { |
| 1137 | if (pmc->multiaddr == multiaddr) |
| 1138 | break; |
| 1139 | pmc_prev = pmc; |
| 1140 | } |
| 1141 | if (pmc) { |
| 1142 | if (pmc_prev) |
| 1143 | pmc_prev->next = pmc->next; |
| 1144 | else |
| 1145 | in_dev->mc_tomb = pmc->next; |
| 1146 | } |
| 1147 | spin_unlock_bh(&in_dev->mc_tomb_lock); |
| 1148 | if (pmc) { |
| 1149 | for (psf = pmc->tomb; psf; psf = psf_next) { |
| 1150 | psf_next = psf->sf_next; |
| 1151 | kfree(psf); |
| 1152 | } |
| 1153 | in_dev_put(pmc->interface); |
| 1154 | kfree(pmc); |
| 1155 | } |
| 1156 | } |
| 1157 | |
| 1158 | static void igmpv3_clear_delrec(struct in_device *in_dev) |
| 1159 | { |
| 1160 | struct ip_mc_list *pmc, *nextpmc; |
| 1161 | |
| 1162 | spin_lock_bh(&in_dev->mc_tomb_lock); |
| 1163 | pmc = in_dev->mc_tomb; |
| 1164 | in_dev->mc_tomb = NULL; |
| 1165 | spin_unlock_bh(&in_dev->mc_tomb_lock); |
| 1166 | |
| 1167 | for (; pmc; pmc = nextpmc) { |
| 1168 | nextpmc = pmc->next; |
| 1169 | ip_mc_clear_src(pmc); |
| 1170 | in_dev_put(pmc->interface); |
| 1171 | kfree(pmc); |
| 1172 | } |
| 1173 | /* clear dead sources, too */ |
| 1174 | rcu_read_lock(); |
| 1175 | for_each_pmc_rcu(in_dev, pmc) { |
| 1176 | struct ip_sf_list *psf, *psf_next; |
| 1177 | |
| 1178 | spin_lock_bh(&pmc->lock); |
| 1179 | psf = pmc->tomb; |
| 1180 | pmc->tomb = NULL; |
| 1181 | spin_unlock_bh(&pmc->lock); |
| 1182 | for (; psf; psf = psf_next) { |
| 1183 | psf_next = psf->sf_next; |
| 1184 | kfree(psf); |
| 1185 | } |
| 1186 | } |
| 1187 | rcu_read_unlock(); |
| 1188 | } |
| 1189 | #endif |
| 1190 | |
| 1191 | static void igmp_group_dropped(struct ip_mc_list *im) |
| 1192 | { |
| 1193 | struct in_device *in_dev = im->interface; |
| 1194 | #ifdef CONFIG_IP_MULTICAST |
| 1195 | int reporter; |
| 1196 | #endif |
| 1197 | |
| 1198 | if (im->loaded) { |
| 1199 | im->loaded = 0; |
| 1200 | ip_mc_filter_del(in_dev, im->multiaddr); |
| 1201 | } |
| 1202 | |
| 1203 | #ifdef CONFIG_IP_MULTICAST |
| 1204 | if (im->multiaddr == IGMP_ALL_HOSTS) |
| 1205 | return; |
| 1206 | if (ipv4_is_local_multicast(im->multiaddr) && !sysctl_igmp_llm_reports) |
| 1207 | return; |
| 1208 | |
| 1209 | reporter = im->reporter; |
| 1210 | igmp_stop_timer(im); |
| 1211 | |
| 1212 | if (!in_dev->dead) { |
| 1213 | if (IGMP_V1_SEEN(in_dev)) |
| 1214 | return; |
| 1215 | if (IGMP_V2_SEEN(in_dev)) { |
| 1216 | if (reporter) |
| 1217 | igmp_send_report(in_dev, im, IGMP_HOST_LEAVE_MESSAGE); |
| 1218 | return; |
| 1219 | } |
| 1220 | /* IGMPv3 */ |
| 1221 | igmpv3_add_delrec(in_dev, im); |
| 1222 | |
| 1223 | igmp_ifc_event(in_dev); |
| 1224 | } |
| 1225 | #endif |
| 1226 | } |
| 1227 | |
| 1228 | static void igmp_group_added(struct ip_mc_list *im) |
| 1229 | { |
| 1230 | struct in_device *in_dev = im->interface; |
| 1231 | |
| 1232 | if (im->loaded == 0) { |
| 1233 | im->loaded = 1; |
| 1234 | ip_mc_filter_add(in_dev, im->multiaddr); |
| 1235 | } |
| 1236 | |
| 1237 | #ifdef CONFIG_IP_MULTICAST |
| 1238 | if (im->multiaddr == IGMP_ALL_HOSTS) |
| 1239 | return; |
| 1240 | if (ipv4_is_local_multicast(im->multiaddr) && !sysctl_igmp_llm_reports) |
| 1241 | return; |
| 1242 | |
| 1243 | if (in_dev->dead) |
| 1244 | return; |
| 1245 | if (IGMP_V1_SEEN(in_dev) || IGMP_V2_SEEN(in_dev)) { |
| 1246 | spin_lock_bh(&im->lock); |
| 1247 | igmp_start_timer(im, IGMP_INITIAL_REPORT_DELAY); |
| 1248 | spin_unlock_bh(&im->lock); |
| 1249 | return; |
| 1250 | } |
| 1251 | /* else, v3 */ |
| 1252 | |
| 1253 | im->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv; |
| 1254 | igmp_ifc_event(in_dev); |
| 1255 | #endif |
| 1256 | } |
| 1257 | |
| 1258 | |
| 1259 | /* |
| 1260 | * Multicast list managers |
| 1261 | */ |
| 1262 | |
| 1263 | static u32 ip_mc_hash(const struct ip_mc_list *im) |
| 1264 | { |
| 1265 | return hash_32((__force u32)im->multiaddr, MC_HASH_SZ_LOG); |
| 1266 | } |
| 1267 | |
| 1268 | static void ip_mc_hash_add(struct in_device *in_dev, |
| 1269 | struct ip_mc_list *im) |
| 1270 | { |
| 1271 | struct ip_mc_list __rcu **mc_hash; |
| 1272 | u32 hash; |
| 1273 | |
| 1274 | mc_hash = rtnl_dereference(in_dev->mc_hash); |
| 1275 | if (mc_hash) { |
| 1276 | hash = ip_mc_hash(im); |
| 1277 | im->next_hash = mc_hash[hash]; |
| 1278 | rcu_assign_pointer(mc_hash[hash], im); |
| 1279 | return; |
| 1280 | } |
| 1281 | |
| 1282 | /* do not use a hash table for small number of items */ |
| 1283 | if (in_dev->mc_count < 4) |
| 1284 | return; |
| 1285 | |
| 1286 | mc_hash = kzalloc(sizeof(struct ip_mc_list *) << MC_HASH_SZ_LOG, |
| 1287 | GFP_KERNEL); |
| 1288 | if (!mc_hash) |
| 1289 | return; |
| 1290 | |
| 1291 | for_each_pmc_rtnl(in_dev, im) { |
| 1292 | hash = ip_mc_hash(im); |
| 1293 | im->next_hash = mc_hash[hash]; |
| 1294 | RCU_INIT_POINTER(mc_hash[hash], im); |
| 1295 | } |
| 1296 | |
| 1297 | rcu_assign_pointer(in_dev->mc_hash, mc_hash); |
| 1298 | } |
| 1299 | |
| 1300 | static void ip_mc_hash_remove(struct in_device *in_dev, |
| 1301 | struct ip_mc_list *im) |
| 1302 | { |
| 1303 | struct ip_mc_list __rcu **mc_hash = rtnl_dereference(in_dev->mc_hash); |
| 1304 | struct ip_mc_list *aux; |
| 1305 | |
| 1306 | if (!mc_hash) |
| 1307 | return; |
| 1308 | mc_hash += ip_mc_hash(im); |
| 1309 | while ((aux = rtnl_dereference(*mc_hash)) != im) |
| 1310 | mc_hash = &aux->next_hash; |
| 1311 | *mc_hash = im->next_hash; |
| 1312 | } |
| 1313 | |
| 1314 | |
| 1315 | /* |
| 1316 | * A socket has joined a multicast group on device dev. |
| 1317 | */ |
| 1318 | |
| 1319 | void ip_mc_inc_group(struct in_device *in_dev, __be32 addr) |
| 1320 | { |
| 1321 | struct ip_mc_list *im; |
| 1322 | |
| 1323 | ASSERT_RTNL(); |
| 1324 | |
| 1325 | for_each_pmc_rtnl(in_dev, im) { |
| 1326 | if (im->multiaddr == addr) { |
| 1327 | im->users++; |
| 1328 | ip_mc_add_src(in_dev, &addr, MCAST_EXCLUDE, 0, NULL, 0); |
| 1329 | goto out; |
| 1330 | } |
| 1331 | } |
| 1332 | |
| 1333 | im = kzalloc(sizeof(*im), GFP_KERNEL); |
| 1334 | if (!im) |
| 1335 | goto out; |
| 1336 | |
| 1337 | im->users = 1; |
| 1338 | im->interface = in_dev; |
| 1339 | in_dev_hold(in_dev); |
| 1340 | im->multiaddr = addr; |
| 1341 | /* initial mode is (EX, empty) */ |
| 1342 | im->sfmode = MCAST_EXCLUDE; |
| 1343 | im->sfcount[MCAST_EXCLUDE] = 1; |
| 1344 | atomic_set(&im->refcnt, 1); |
| 1345 | spin_lock_init(&im->lock); |
| 1346 | #ifdef CONFIG_IP_MULTICAST |
| 1347 | setup_timer(&im->timer, igmp_timer_expire, (unsigned long)im); |
| 1348 | im->unsolicit_count = sysctl_igmp_qrv; |
| 1349 | #endif |
| 1350 | |
| 1351 | im->next_rcu = in_dev->mc_list; |
| 1352 | in_dev->mc_count++; |
| 1353 | rcu_assign_pointer(in_dev->mc_list, im); |
| 1354 | |
| 1355 | ip_mc_hash_add(in_dev, im); |
| 1356 | |
| 1357 | #ifdef CONFIG_IP_MULTICAST |
| 1358 | igmpv3_del_delrec(in_dev, im->multiaddr); |
| 1359 | #endif |
| 1360 | igmp_group_added(im); |
| 1361 | if (!in_dev->dead) |
| 1362 | ip_rt_multicast_event(in_dev); |
| 1363 | out: |
| 1364 | return; |
| 1365 | } |
| 1366 | EXPORT_SYMBOL(ip_mc_inc_group); |
| 1367 | |
| 1368 | static int ip_mc_check_iphdr(struct sk_buff *skb) |
| 1369 | { |
| 1370 | const struct iphdr *iph; |
| 1371 | unsigned int len; |
| 1372 | unsigned int offset = skb_network_offset(skb) + sizeof(*iph); |
| 1373 | |
| 1374 | if (!pskb_may_pull(skb, offset)) |
| 1375 | return -EINVAL; |
| 1376 | |
| 1377 | iph = ip_hdr(skb); |
| 1378 | |
| 1379 | if (iph->version != 4 || ip_hdrlen(skb) < sizeof(*iph)) |
| 1380 | return -EINVAL; |
| 1381 | |
| 1382 | offset += ip_hdrlen(skb) - sizeof(*iph); |
| 1383 | |
| 1384 | if (!pskb_may_pull(skb, offset)) |
| 1385 | return -EINVAL; |
| 1386 | |
| 1387 | iph = ip_hdr(skb); |
| 1388 | |
| 1389 | if (unlikely(ip_fast_csum((u8 *)iph, iph->ihl))) |
| 1390 | return -EINVAL; |
| 1391 | |
| 1392 | len = skb_network_offset(skb) + ntohs(iph->tot_len); |
| 1393 | if (skb->len < len || len < offset) |
| 1394 | return -EINVAL; |
| 1395 | |
| 1396 | skb_set_transport_header(skb, offset); |
| 1397 | |
| 1398 | return 0; |
| 1399 | } |
| 1400 | |
| 1401 | static int ip_mc_check_igmp_reportv3(struct sk_buff *skb) |
| 1402 | { |
| 1403 | unsigned int len = skb_transport_offset(skb); |
| 1404 | |
| 1405 | len += sizeof(struct igmpv3_report); |
| 1406 | |
| 1407 | return pskb_may_pull(skb, len) ? 0 : -EINVAL; |
| 1408 | } |
| 1409 | |
| 1410 | static int ip_mc_check_igmp_query(struct sk_buff *skb) |
| 1411 | { |
| 1412 | unsigned int len = skb_transport_offset(skb); |
| 1413 | |
| 1414 | len += sizeof(struct igmphdr); |
| 1415 | if (skb->len < len) |
| 1416 | return -EINVAL; |
| 1417 | |
| 1418 | /* IGMPv{1,2}? */ |
| 1419 | if (skb->len != len) { |
| 1420 | /* or IGMPv3? */ |
| 1421 | len += sizeof(struct igmpv3_query) - sizeof(struct igmphdr); |
| 1422 | if (skb->len < len || !pskb_may_pull(skb, len)) |
| 1423 | return -EINVAL; |
| 1424 | } |
| 1425 | |
| 1426 | /* RFC2236+RFC3376 (IGMPv2+IGMPv3) require the multicast link layer |
| 1427 | * all-systems destination addresses (224.0.0.1) for general queries |
| 1428 | */ |
| 1429 | if (!igmp_hdr(skb)->group && |
| 1430 | ip_hdr(skb)->daddr != htonl(INADDR_ALLHOSTS_GROUP)) |
| 1431 | return -EINVAL; |
| 1432 | |
| 1433 | return 0; |
| 1434 | } |
| 1435 | |
| 1436 | static int ip_mc_check_igmp_msg(struct sk_buff *skb) |
| 1437 | { |
| 1438 | switch (igmp_hdr(skb)->type) { |
| 1439 | case IGMP_HOST_LEAVE_MESSAGE: |
| 1440 | case IGMP_HOST_MEMBERSHIP_REPORT: |
| 1441 | case IGMPV2_HOST_MEMBERSHIP_REPORT: |
| 1442 | /* fall through */ |
| 1443 | return 0; |
| 1444 | case IGMPV3_HOST_MEMBERSHIP_REPORT: |
| 1445 | return ip_mc_check_igmp_reportv3(skb); |
| 1446 | case IGMP_HOST_MEMBERSHIP_QUERY: |
| 1447 | return ip_mc_check_igmp_query(skb); |
| 1448 | default: |
| 1449 | return -ENOMSG; |
| 1450 | } |
| 1451 | } |
| 1452 | |
| 1453 | static inline __sum16 ip_mc_validate_checksum(struct sk_buff *skb) |
| 1454 | { |
| 1455 | return skb_checksum_simple_validate(skb); |
| 1456 | } |
| 1457 | |
| 1458 | static int __ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed) |
| 1459 | |
| 1460 | { |
| 1461 | struct sk_buff *skb_chk; |
| 1462 | unsigned int transport_len; |
| 1463 | unsigned int len = skb_transport_offset(skb) + sizeof(struct igmphdr); |
| 1464 | int ret = -EINVAL; |
| 1465 | |
| 1466 | transport_len = ntohs(ip_hdr(skb)->tot_len) - ip_hdrlen(skb); |
| 1467 | |
| 1468 | skb_chk = skb_checksum_trimmed(skb, transport_len, |
| 1469 | ip_mc_validate_checksum); |
| 1470 | if (!skb_chk) |
| 1471 | goto err; |
| 1472 | |
| 1473 | if (!pskb_may_pull(skb_chk, len)) |
| 1474 | goto err; |
| 1475 | |
| 1476 | ret = ip_mc_check_igmp_msg(skb_chk); |
| 1477 | if (ret) |
| 1478 | goto err; |
| 1479 | |
| 1480 | if (skb_trimmed) |
| 1481 | *skb_trimmed = skb_chk; |
| 1482 | /* free now unneeded clone */ |
| 1483 | else if (skb_chk != skb) |
| 1484 | kfree_skb(skb_chk); |
| 1485 | |
| 1486 | ret = 0; |
| 1487 | |
| 1488 | err: |
| 1489 | if (ret && skb_chk && skb_chk != skb) |
| 1490 | kfree_skb(skb_chk); |
| 1491 | |
| 1492 | return ret; |
| 1493 | } |
| 1494 | |
| 1495 | /** |
| 1496 | * ip_mc_check_igmp - checks whether this is a sane IGMP packet |
| 1497 | * @skb: the skb to validate |
| 1498 | * @skb_trimmed: to store an skb pointer trimmed to IPv4 packet tail (optional) |
| 1499 | * |
| 1500 | * Checks whether an IPv4 packet is a valid IGMP packet. If so sets |
| 1501 | * skb transport header accordingly and returns zero. |
| 1502 | * |
| 1503 | * -EINVAL: A broken packet was detected, i.e. it violates some internet |
| 1504 | * standard |
| 1505 | * -ENOMSG: IP header validation succeeded but it is not an IGMP packet. |
| 1506 | * -ENOMEM: A memory allocation failure happened. |
| 1507 | * |
| 1508 | * Optionally, an skb pointer might be provided via skb_trimmed (or set it |
| 1509 | * to NULL): After parsing an IGMP packet successfully it will point to |
| 1510 | * an skb which has its tail aligned to the IP packet end. This might |
| 1511 | * either be the originally provided skb or a trimmed, cloned version if |
| 1512 | * the skb frame had data beyond the IP packet. A cloned skb allows us |
| 1513 | * to leave the original skb and its full frame unchanged (which might be |
| 1514 | * desirable for layer 2 frame jugglers). |
| 1515 | * |
| 1516 | * Caller needs to set the skb network header and free any returned skb if it |
| 1517 | * differs from the provided skb. |
| 1518 | */ |
| 1519 | int ip_mc_check_igmp(struct sk_buff *skb, struct sk_buff **skb_trimmed) |
| 1520 | { |
| 1521 | int ret = ip_mc_check_iphdr(skb); |
| 1522 | |
| 1523 | if (ret < 0) |
| 1524 | return ret; |
| 1525 | |
| 1526 | if (ip_hdr(skb)->protocol != IPPROTO_IGMP) |
| 1527 | return -ENOMSG; |
| 1528 | |
| 1529 | return __ip_mc_check_igmp(skb, skb_trimmed); |
| 1530 | } |
| 1531 | EXPORT_SYMBOL(ip_mc_check_igmp); |
| 1532 | |
| 1533 | /* |
| 1534 | * Resend IGMP JOIN report; used by netdev notifier. |
| 1535 | */ |
| 1536 | static void ip_mc_rejoin_groups(struct in_device *in_dev) |
| 1537 | { |
| 1538 | #ifdef CONFIG_IP_MULTICAST |
| 1539 | struct ip_mc_list *im; |
| 1540 | int type; |
| 1541 | |
| 1542 | ASSERT_RTNL(); |
| 1543 | |
| 1544 | for_each_pmc_rtnl(in_dev, im) { |
| 1545 | if (im->multiaddr == IGMP_ALL_HOSTS) |
| 1546 | continue; |
| 1547 | if (ipv4_is_local_multicast(im->multiaddr) && |
| 1548 | !sysctl_igmp_llm_reports) |
| 1549 | continue; |
| 1550 | |
| 1551 | /* a failover is happening and switches |
| 1552 | * must be notified immediately |
| 1553 | */ |
| 1554 | if (IGMP_V1_SEEN(in_dev)) |
| 1555 | type = IGMP_HOST_MEMBERSHIP_REPORT; |
| 1556 | else if (IGMP_V2_SEEN(in_dev)) |
| 1557 | type = IGMPV2_HOST_MEMBERSHIP_REPORT; |
| 1558 | else |
| 1559 | type = IGMPV3_HOST_MEMBERSHIP_REPORT; |
| 1560 | igmp_send_report(in_dev, im, type); |
| 1561 | } |
| 1562 | #endif |
| 1563 | } |
| 1564 | |
| 1565 | /* |
| 1566 | * A socket has left a multicast group on device dev |
| 1567 | */ |
| 1568 | |
| 1569 | void ip_mc_dec_group(struct in_device *in_dev, __be32 addr) |
| 1570 | { |
| 1571 | struct ip_mc_list *i; |
| 1572 | struct ip_mc_list __rcu **ip; |
| 1573 | |
| 1574 | ASSERT_RTNL(); |
| 1575 | |
| 1576 | for (ip = &in_dev->mc_list; |
| 1577 | (i = rtnl_dereference(*ip)) != NULL; |
| 1578 | ip = &i->next_rcu) { |
| 1579 | if (i->multiaddr == addr) { |
| 1580 | if (--i->users == 0) { |
| 1581 | ip_mc_hash_remove(in_dev, i); |
| 1582 | *ip = i->next_rcu; |
| 1583 | in_dev->mc_count--; |
| 1584 | igmp_group_dropped(i); |
| 1585 | ip_mc_clear_src(i); |
| 1586 | |
| 1587 | if (!in_dev->dead) |
| 1588 | ip_rt_multicast_event(in_dev); |
| 1589 | |
| 1590 | ip_ma_put(i); |
| 1591 | return; |
| 1592 | } |
| 1593 | break; |
| 1594 | } |
| 1595 | } |
| 1596 | } |
| 1597 | EXPORT_SYMBOL(ip_mc_dec_group); |
| 1598 | |
| 1599 | /* Device changing type */ |
| 1600 | |
| 1601 | void ip_mc_unmap(struct in_device *in_dev) |
| 1602 | { |
| 1603 | struct ip_mc_list *pmc; |
| 1604 | |
| 1605 | ASSERT_RTNL(); |
| 1606 | |
| 1607 | for_each_pmc_rtnl(in_dev, pmc) |
| 1608 | igmp_group_dropped(pmc); |
| 1609 | } |
| 1610 | |
| 1611 | void ip_mc_remap(struct in_device *in_dev) |
| 1612 | { |
| 1613 | struct ip_mc_list *pmc; |
| 1614 | |
| 1615 | ASSERT_RTNL(); |
| 1616 | |
| 1617 | for_each_pmc_rtnl(in_dev, pmc) |
| 1618 | igmp_group_added(pmc); |
| 1619 | } |
| 1620 | |
| 1621 | /* Device going down */ |
| 1622 | |
| 1623 | void ip_mc_down(struct in_device *in_dev) |
| 1624 | { |
| 1625 | struct ip_mc_list *pmc; |
| 1626 | |
| 1627 | ASSERT_RTNL(); |
| 1628 | |
| 1629 | for_each_pmc_rtnl(in_dev, pmc) |
| 1630 | igmp_group_dropped(pmc); |
| 1631 | |
| 1632 | #ifdef CONFIG_IP_MULTICAST |
| 1633 | in_dev->mr_ifc_count = 0; |
| 1634 | if (del_timer(&in_dev->mr_ifc_timer)) |
| 1635 | __in_dev_put(in_dev); |
| 1636 | in_dev->mr_gq_running = 0; |
| 1637 | if (del_timer(&in_dev->mr_gq_timer)) |
| 1638 | __in_dev_put(in_dev); |
| 1639 | igmpv3_clear_delrec(in_dev); |
| 1640 | #endif |
| 1641 | |
| 1642 | ip_mc_dec_group(in_dev, IGMP_ALL_HOSTS); |
| 1643 | } |
| 1644 | |
| 1645 | void ip_mc_init_dev(struct in_device *in_dev) |
| 1646 | { |
| 1647 | ASSERT_RTNL(); |
| 1648 | |
| 1649 | #ifdef CONFIG_IP_MULTICAST |
| 1650 | setup_timer(&in_dev->mr_gq_timer, igmp_gq_timer_expire, |
| 1651 | (unsigned long)in_dev); |
| 1652 | setup_timer(&in_dev->mr_ifc_timer, igmp_ifc_timer_expire, |
| 1653 | (unsigned long)in_dev); |
| 1654 | in_dev->mr_qrv = sysctl_igmp_qrv; |
| 1655 | #endif |
| 1656 | |
| 1657 | spin_lock_init(&in_dev->mc_tomb_lock); |
| 1658 | } |
| 1659 | |
| 1660 | /* Device going up */ |
| 1661 | |
| 1662 | void ip_mc_up(struct in_device *in_dev) |
| 1663 | { |
| 1664 | struct ip_mc_list *pmc; |
| 1665 | |
| 1666 | ASSERT_RTNL(); |
| 1667 | |
| 1668 | #ifdef CONFIG_IP_MULTICAST |
| 1669 | in_dev->mr_qrv = sysctl_igmp_qrv; |
| 1670 | #endif |
| 1671 | ip_mc_inc_group(in_dev, IGMP_ALL_HOSTS); |
| 1672 | |
| 1673 | for_each_pmc_rtnl(in_dev, pmc) |
| 1674 | igmp_group_added(pmc); |
| 1675 | } |
| 1676 | |
| 1677 | /* |
| 1678 | * Device is about to be destroyed: clean up. |
| 1679 | */ |
| 1680 | |
| 1681 | void ip_mc_destroy_dev(struct in_device *in_dev) |
| 1682 | { |
| 1683 | struct ip_mc_list *i; |
| 1684 | |
| 1685 | ASSERT_RTNL(); |
| 1686 | |
| 1687 | /* Deactivate timers */ |
| 1688 | ip_mc_down(in_dev); |
| 1689 | |
| 1690 | while ((i = rtnl_dereference(in_dev->mc_list)) != NULL) { |
| 1691 | in_dev->mc_list = i->next_rcu; |
| 1692 | in_dev->mc_count--; |
| 1693 | |
| 1694 | /* We've dropped the groups in ip_mc_down already */ |
| 1695 | ip_mc_clear_src(i); |
| 1696 | ip_ma_put(i); |
| 1697 | } |
| 1698 | } |
| 1699 | |
| 1700 | /* RTNL is locked */ |
| 1701 | static struct in_device *ip_mc_find_dev(struct net *net, struct ip_mreqn *imr) |
| 1702 | { |
| 1703 | struct net_device *dev = NULL; |
| 1704 | struct in_device *idev = NULL; |
| 1705 | |
| 1706 | if (imr->imr_ifindex) { |
| 1707 | idev = inetdev_by_index(net, imr->imr_ifindex); |
| 1708 | return idev; |
| 1709 | } |
| 1710 | if (imr->imr_address.s_addr) { |
| 1711 | dev = __ip_dev_find(net, imr->imr_address.s_addr, false); |
| 1712 | if (!dev) |
| 1713 | return NULL; |
| 1714 | } |
| 1715 | |
| 1716 | if (!dev) { |
| 1717 | struct rtable *rt = ip_route_output(net, |
| 1718 | imr->imr_multiaddr.s_addr, |
| 1719 | 0, 0, 0); |
| 1720 | if (!IS_ERR(rt)) { |
| 1721 | dev = rt->dst.dev; |
| 1722 | ip_rt_put(rt); |
| 1723 | } |
| 1724 | } |
| 1725 | if (dev) { |
| 1726 | imr->imr_ifindex = dev->ifindex; |
| 1727 | idev = __in_dev_get_rtnl(dev); |
| 1728 | } |
| 1729 | return idev; |
| 1730 | } |
| 1731 | |
| 1732 | /* |
| 1733 | * Join a socket to a group |
| 1734 | */ |
| 1735 | int sysctl_igmp_max_memberships __read_mostly = IP_MAX_MEMBERSHIPS; |
| 1736 | int sysctl_igmp_max_msf __read_mostly = IP_MAX_MSF; |
| 1737 | #ifdef CONFIG_IP_MULTICAST |
| 1738 | int sysctl_igmp_qrv __read_mostly = IGMP_QUERY_ROBUSTNESS_VARIABLE; |
| 1739 | #endif |
| 1740 | |
| 1741 | static int ip_mc_del1_src(struct ip_mc_list *pmc, int sfmode, |
| 1742 | __be32 *psfsrc) |
| 1743 | { |
| 1744 | struct ip_sf_list *psf, *psf_prev; |
| 1745 | int rv = 0; |
| 1746 | |
| 1747 | psf_prev = NULL; |
| 1748 | for (psf = pmc->sources; psf; psf = psf->sf_next) { |
| 1749 | if (psf->sf_inaddr == *psfsrc) |
| 1750 | break; |
| 1751 | psf_prev = psf; |
| 1752 | } |
| 1753 | if (!psf || psf->sf_count[sfmode] == 0) { |
| 1754 | /* source filter not found, or count wrong => bug */ |
| 1755 | return -ESRCH; |
| 1756 | } |
| 1757 | psf->sf_count[sfmode]--; |
| 1758 | if (psf->sf_count[sfmode] == 0) { |
| 1759 | ip_rt_multicast_event(pmc->interface); |
| 1760 | } |
| 1761 | if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) { |
| 1762 | #ifdef CONFIG_IP_MULTICAST |
| 1763 | struct in_device *in_dev = pmc->interface; |
| 1764 | #endif |
| 1765 | |
| 1766 | /* no more filters for this source */ |
| 1767 | if (psf_prev) |
| 1768 | psf_prev->sf_next = psf->sf_next; |
| 1769 | else |
| 1770 | pmc->sources = psf->sf_next; |
| 1771 | #ifdef CONFIG_IP_MULTICAST |
| 1772 | if (psf->sf_oldin && |
| 1773 | !IGMP_V1_SEEN(in_dev) && !IGMP_V2_SEEN(in_dev)) { |
| 1774 | psf->sf_crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv; |
| 1775 | psf->sf_next = pmc->tomb; |
| 1776 | pmc->tomb = psf; |
| 1777 | rv = 1; |
| 1778 | } else |
| 1779 | #endif |
| 1780 | kfree(psf); |
| 1781 | } |
| 1782 | return rv; |
| 1783 | } |
| 1784 | |
| 1785 | #ifndef CONFIG_IP_MULTICAST |
| 1786 | #define igmp_ifc_event(x) do { } while (0) |
| 1787 | #endif |
| 1788 | |
| 1789 | static int ip_mc_del_src(struct in_device *in_dev, __be32 *pmca, int sfmode, |
| 1790 | int sfcount, __be32 *psfsrc, int delta) |
| 1791 | { |
| 1792 | struct ip_mc_list *pmc; |
| 1793 | int changerec = 0; |
| 1794 | int i, err; |
| 1795 | |
| 1796 | if (!in_dev) |
| 1797 | return -ENODEV; |
| 1798 | rcu_read_lock(); |
| 1799 | for_each_pmc_rcu(in_dev, pmc) { |
| 1800 | if (*pmca == pmc->multiaddr) |
| 1801 | break; |
| 1802 | } |
| 1803 | if (!pmc) { |
| 1804 | /* MCA not found?? bug */ |
| 1805 | rcu_read_unlock(); |
| 1806 | return -ESRCH; |
| 1807 | } |
| 1808 | spin_lock_bh(&pmc->lock); |
| 1809 | rcu_read_unlock(); |
| 1810 | #ifdef CONFIG_IP_MULTICAST |
| 1811 | sf_markstate(pmc); |
| 1812 | #endif |
| 1813 | if (!delta) { |
| 1814 | err = -EINVAL; |
| 1815 | if (!pmc->sfcount[sfmode]) |
| 1816 | goto out_unlock; |
| 1817 | pmc->sfcount[sfmode]--; |
| 1818 | } |
| 1819 | err = 0; |
| 1820 | for (i = 0; i < sfcount; i++) { |
| 1821 | int rv = ip_mc_del1_src(pmc, sfmode, &psfsrc[i]); |
| 1822 | |
| 1823 | changerec |= rv > 0; |
| 1824 | if (!err && rv < 0) |
| 1825 | err = rv; |
| 1826 | } |
| 1827 | if (pmc->sfmode == MCAST_EXCLUDE && |
| 1828 | pmc->sfcount[MCAST_EXCLUDE] == 0 && |
| 1829 | pmc->sfcount[MCAST_INCLUDE]) { |
| 1830 | #ifdef CONFIG_IP_MULTICAST |
| 1831 | struct ip_sf_list *psf; |
| 1832 | #endif |
| 1833 | |
| 1834 | /* filter mode change */ |
| 1835 | pmc->sfmode = MCAST_INCLUDE; |
| 1836 | #ifdef CONFIG_IP_MULTICAST |
| 1837 | pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv; |
| 1838 | in_dev->mr_ifc_count = pmc->crcount; |
| 1839 | for (psf = pmc->sources; psf; psf = psf->sf_next) |
| 1840 | psf->sf_crcount = 0; |
| 1841 | igmp_ifc_event(pmc->interface); |
| 1842 | } else if (sf_setstate(pmc) || changerec) { |
| 1843 | igmp_ifc_event(pmc->interface); |
| 1844 | #endif |
| 1845 | } |
| 1846 | out_unlock: |
| 1847 | spin_unlock_bh(&pmc->lock); |
| 1848 | return err; |
| 1849 | } |
| 1850 | |
| 1851 | /* |
| 1852 | * Add multicast single-source filter to the interface list |
| 1853 | */ |
| 1854 | static int ip_mc_add1_src(struct ip_mc_list *pmc, int sfmode, |
| 1855 | __be32 *psfsrc) |
| 1856 | { |
| 1857 | struct ip_sf_list *psf, *psf_prev; |
| 1858 | |
| 1859 | psf_prev = NULL; |
| 1860 | for (psf = pmc->sources; psf; psf = psf->sf_next) { |
| 1861 | if (psf->sf_inaddr == *psfsrc) |
| 1862 | break; |
| 1863 | psf_prev = psf; |
| 1864 | } |
| 1865 | if (!psf) { |
| 1866 | psf = kzalloc(sizeof(*psf), GFP_ATOMIC); |
| 1867 | if (!psf) |
| 1868 | return -ENOBUFS; |
| 1869 | psf->sf_inaddr = *psfsrc; |
| 1870 | if (psf_prev) { |
| 1871 | psf_prev->sf_next = psf; |
| 1872 | } else |
| 1873 | pmc->sources = psf; |
| 1874 | } |
| 1875 | psf->sf_count[sfmode]++; |
| 1876 | if (psf->sf_count[sfmode] == 1) { |
| 1877 | ip_rt_multicast_event(pmc->interface); |
| 1878 | } |
| 1879 | return 0; |
| 1880 | } |
| 1881 | |
| 1882 | #ifdef CONFIG_IP_MULTICAST |
| 1883 | static void sf_markstate(struct ip_mc_list *pmc) |
| 1884 | { |
| 1885 | struct ip_sf_list *psf; |
| 1886 | int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; |
| 1887 | |
| 1888 | for (psf = pmc->sources; psf; psf = psf->sf_next) |
| 1889 | if (pmc->sfcount[MCAST_EXCLUDE]) { |
| 1890 | psf->sf_oldin = mca_xcount == |
| 1891 | psf->sf_count[MCAST_EXCLUDE] && |
| 1892 | !psf->sf_count[MCAST_INCLUDE]; |
| 1893 | } else |
| 1894 | psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0; |
| 1895 | } |
| 1896 | |
| 1897 | static int sf_setstate(struct ip_mc_list *pmc) |
| 1898 | { |
| 1899 | struct ip_sf_list *psf, *dpsf; |
| 1900 | int mca_xcount = pmc->sfcount[MCAST_EXCLUDE]; |
| 1901 | int qrv = pmc->interface->mr_qrv; |
| 1902 | int new_in, rv; |
| 1903 | |
| 1904 | rv = 0; |
| 1905 | for (psf = pmc->sources; psf; psf = psf->sf_next) { |
| 1906 | if (pmc->sfcount[MCAST_EXCLUDE]) { |
| 1907 | new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] && |
| 1908 | !psf->sf_count[MCAST_INCLUDE]; |
| 1909 | } else |
| 1910 | new_in = psf->sf_count[MCAST_INCLUDE] != 0; |
| 1911 | if (new_in) { |
| 1912 | if (!psf->sf_oldin) { |
| 1913 | struct ip_sf_list *prev = NULL; |
| 1914 | |
| 1915 | for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) { |
| 1916 | if (dpsf->sf_inaddr == psf->sf_inaddr) |
| 1917 | break; |
| 1918 | prev = dpsf; |
| 1919 | } |
| 1920 | if (dpsf) { |
| 1921 | if (prev) |
| 1922 | prev->sf_next = dpsf->sf_next; |
| 1923 | else |
| 1924 | pmc->tomb = dpsf->sf_next; |
| 1925 | kfree(dpsf); |
| 1926 | } |
| 1927 | psf->sf_crcount = qrv; |
| 1928 | rv++; |
| 1929 | } |
| 1930 | } else if (psf->sf_oldin) { |
| 1931 | |
| 1932 | psf->sf_crcount = 0; |
| 1933 | /* |
| 1934 | * add or update "delete" records if an active filter |
| 1935 | * is now inactive |
| 1936 | */ |
| 1937 | for (dpsf = pmc->tomb; dpsf; dpsf = dpsf->sf_next) |
| 1938 | if (dpsf->sf_inaddr == psf->sf_inaddr) |
| 1939 | break; |
| 1940 | if (!dpsf) { |
| 1941 | dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC); |
| 1942 | if (!dpsf) |
| 1943 | continue; |
| 1944 | *dpsf = *psf; |
| 1945 | /* pmc->lock held by callers */ |
| 1946 | dpsf->sf_next = pmc->tomb; |
| 1947 | pmc->tomb = dpsf; |
| 1948 | } |
| 1949 | dpsf->sf_crcount = qrv; |
| 1950 | rv++; |
| 1951 | } |
| 1952 | } |
| 1953 | return rv; |
| 1954 | } |
| 1955 | #endif |
| 1956 | |
| 1957 | /* |
| 1958 | * Add multicast source filter list to the interface list |
| 1959 | */ |
| 1960 | static int ip_mc_add_src(struct in_device *in_dev, __be32 *pmca, int sfmode, |
| 1961 | int sfcount, __be32 *psfsrc, int delta) |
| 1962 | { |
| 1963 | struct ip_mc_list *pmc; |
| 1964 | int isexclude; |
| 1965 | int i, err; |
| 1966 | |
| 1967 | if (!in_dev) |
| 1968 | return -ENODEV; |
| 1969 | rcu_read_lock(); |
| 1970 | for_each_pmc_rcu(in_dev, pmc) { |
| 1971 | if (*pmca == pmc->multiaddr) |
| 1972 | break; |
| 1973 | } |
| 1974 | if (!pmc) { |
| 1975 | /* MCA not found?? bug */ |
| 1976 | rcu_read_unlock(); |
| 1977 | return -ESRCH; |
| 1978 | } |
| 1979 | spin_lock_bh(&pmc->lock); |
| 1980 | rcu_read_unlock(); |
| 1981 | |
| 1982 | #ifdef CONFIG_IP_MULTICAST |
| 1983 | sf_markstate(pmc); |
| 1984 | #endif |
| 1985 | isexclude = pmc->sfmode == MCAST_EXCLUDE; |
| 1986 | if (!delta) |
| 1987 | pmc->sfcount[sfmode]++; |
| 1988 | err = 0; |
| 1989 | for (i = 0; i < sfcount; i++) { |
| 1990 | err = ip_mc_add1_src(pmc, sfmode, &psfsrc[i]); |
| 1991 | if (err) |
| 1992 | break; |
| 1993 | } |
| 1994 | if (err) { |
| 1995 | int j; |
| 1996 | |
| 1997 | if (!delta) |
| 1998 | pmc->sfcount[sfmode]--; |
| 1999 | for (j = 0; j < i; j++) |
| 2000 | (void) ip_mc_del1_src(pmc, sfmode, &psfsrc[j]); |
| 2001 | } else if (isexclude != (pmc->sfcount[MCAST_EXCLUDE] != 0)) { |
| 2002 | #ifdef CONFIG_IP_MULTICAST |
| 2003 | struct ip_sf_list *psf; |
| 2004 | in_dev = pmc->interface; |
| 2005 | #endif |
| 2006 | |
| 2007 | /* filter mode change */ |
| 2008 | if (pmc->sfcount[MCAST_EXCLUDE]) |
| 2009 | pmc->sfmode = MCAST_EXCLUDE; |
| 2010 | else if (pmc->sfcount[MCAST_INCLUDE]) |
| 2011 | pmc->sfmode = MCAST_INCLUDE; |
| 2012 | #ifdef CONFIG_IP_MULTICAST |
| 2013 | /* else no filters; keep old mode for reports */ |
| 2014 | |
| 2015 | pmc->crcount = in_dev->mr_qrv ?: sysctl_igmp_qrv; |
| 2016 | in_dev->mr_ifc_count = pmc->crcount; |
| 2017 | for (psf = pmc->sources; psf; psf = psf->sf_next) |
| 2018 | psf->sf_crcount = 0; |
| 2019 | igmp_ifc_event(in_dev); |
| 2020 | } else if (sf_setstate(pmc)) { |
| 2021 | igmp_ifc_event(in_dev); |
| 2022 | #endif |
| 2023 | } |
| 2024 | spin_unlock_bh(&pmc->lock); |
| 2025 | return err; |
| 2026 | } |
| 2027 | |
| 2028 | static void ip_mc_clear_src(struct ip_mc_list *pmc) |
| 2029 | { |
| 2030 | struct ip_sf_list *psf, *nextpsf, *tomb, *sources; |
| 2031 | |
| 2032 | spin_lock_bh(&pmc->lock); |
| 2033 | tomb = pmc->tomb; |
| 2034 | pmc->tomb = NULL; |
| 2035 | sources = pmc->sources; |
| 2036 | pmc->sources = NULL; |
| 2037 | pmc->sfmode = MCAST_EXCLUDE; |
| 2038 | pmc->sfcount[MCAST_INCLUDE] = 0; |
| 2039 | pmc->sfcount[MCAST_EXCLUDE] = 1; |
| 2040 | spin_unlock_bh(&pmc->lock); |
| 2041 | |
| 2042 | for (psf = tomb; psf; psf = nextpsf) { |
| 2043 | nextpsf = psf->sf_next; |
| 2044 | kfree(psf); |
| 2045 | } |
| 2046 | for (psf = sources; psf; psf = nextpsf) { |
| 2047 | nextpsf = psf->sf_next; |
| 2048 | kfree(psf); |
| 2049 | } |
| 2050 | } |
| 2051 | |
| 2052 | /* Join a multicast group |
| 2053 | */ |
| 2054 | |
| 2055 | int ip_mc_join_group(struct sock *sk, struct ip_mreqn *imr) |
| 2056 | { |
| 2057 | __be32 addr = imr->imr_multiaddr.s_addr; |
| 2058 | struct ip_mc_socklist *iml, *i; |
| 2059 | struct in_device *in_dev; |
| 2060 | struct inet_sock *inet = inet_sk(sk); |
| 2061 | struct net *net = sock_net(sk); |
| 2062 | int ifindex; |
| 2063 | int count = 0; |
| 2064 | int err; |
| 2065 | |
| 2066 | ASSERT_RTNL(); |
| 2067 | |
| 2068 | if (!ipv4_is_multicast(addr)) |
| 2069 | return -EINVAL; |
| 2070 | |
| 2071 | in_dev = ip_mc_find_dev(net, imr); |
| 2072 | |
| 2073 | if (!in_dev) { |
| 2074 | err = -ENODEV; |
| 2075 | goto done; |
| 2076 | } |
| 2077 | |
| 2078 | err = -EADDRINUSE; |
| 2079 | ifindex = imr->imr_ifindex; |
| 2080 | for_each_pmc_rtnl(inet, i) { |
| 2081 | if (i->multi.imr_multiaddr.s_addr == addr && |
| 2082 | i->multi.imr_ifindex == ifindex) |
| 2083 | goto done; |
| 2084 | count++; |
| 2085 | } |
| 2086 | err = -ENOBUFS; |
| 2087 | if (count >= sysctl_igmp_max_memberships) |
| 2088 | goto done; |
| 2089 | iml = sock_kmalloc(sk, sizeof(*iml), GFP_KERNEL); |
| 2090 | if (!iml) |
| 2091 | goto done; |
| 2092 | |
| 2093 | memcpy(&iml->multi, imr, sizeof(*imr)); |
| 2094 | iml->next_rcu = inet->mc_list; |
| 2095 | iml->sflist = NULL; |
| 2096 | iml->sfmode = MCAST_EXCLUDE; |
| 2097 | rcu_assign_pointer(inet->mc_list, iml); |
| 2098 | ip_mc_inc_group(in_dev, addr); |
| 2099 | err = 0; |
| 2100 | done: |
| 2101 | return err; |
| 2102 | } |
| 2103 | EXPORT_SYMBOL(ip_mc_join_group); |
| 2104 | |
| 2105 | static int ip_mc_leave_src(struct sock *sk, struct ip_mc_socklist *iml, |
| 2106 | struct in_device *in_dev) |
| 2107 | { |
| 2108 | struct ip_sf_socklist *psf = rtnl_dereference(iml->sflist); |
| 2109 | int err; |
| 2110 | |
| 2111 | if (!psf) { |
| 2112 | /* any-source empty exclude case */ |
| 2113 | return ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, |
| 2114 | iml->sfmode, 0, NULL, 0); |
| 2115 | } |
| 2116 | err = ip_mc_del_src(in_dev, &iml->multi.imr_multiaddr.s_addr, |
| 2117 | iml->sfmode, psf->sl_count, psf->sl_addr, 0); |
| 2118 | RCU_INIT_POINTER(iml->sflist, NULL); |
| 2119 | /* decrease mem now to avoid the memleak warning */ |
| 2120 | atomic_sub(IP_SFLSIZE(psf->sl_max), &sk->sk_omem_alloc); |
| 2121 | kfree_rcu(psf, rcu); |
| 2122 | return err; |
| 2123 | } |
| 2124 | |
| 2125 | int ip_mc_leave_group(struct sock *sk, struct ip_mreqn *imr) |
| 2126 | { |
| 2127 | struct inet_sock *inet = inet_sk(sk); |
| 2128 | struct ip_mc_socklist *iml; |
| 2129 | struct ip_mc_socklist __rcu **imlp; |
| 2130 | struct in_device *in_dev; |
| 2131 | struct net *net = sock_net(sk); |
| 2132 | __be32 group = imr->imr_multiaddr.s_addr; |
| 2133 | u32 ifindex; |
| 2134 | int ret = -EADDRNOTAVAIL; |
| 2135 | |
| 2136 | ASSERT_RTNL(); |
| 2137 | |
| 2138 | in_dev = ip_mc_find_dev(net, imr); |
| 2139 | if (!imr->imr_ifindex && !imr->imr_address.s_addr && !in_dev) { |
| 2140 | ret = -ENODEV; |
| 2141 | goto out; |
| 2142 | } |
| 2143 | ifindex = imr->imr_ifindex; |
| 2144 | for (imlp = &inet->mc_list; |
| 2145 | (iml = rtnl_dereference(*imlp)) != NULL; |
| 2146 | imlp = &iml->next_rcu) { |
| 2147 | if (iml->multi.imr_multiaddr.s_addr != group) |
| 2148 | continue; |
| 2149 | if (ifindex) { |
| 2150 | if (iml->multi.imr_ifindex != ifindex) |
| 2151 | continue; |
| 2152 | } else if (imr->imr_address.s_addr && imr->imr_address.s_addr != |
| 2153 | iml->multi.imr_address.s_addr) |
| 2154 | continue; |
| 2155 | |
| 2156 | (void) ip_mc_leave_src(sk, iml, in_dev); |
| 2157 | |
| 2158 | *imlp = iml->next_rcu; |
| 2159 | |
| 2160 | if (in_dev) |
| 2161 | ip_mc_dec_group(in_dev, group); |
| 2162 | |
| 2163 | /* decrease mem now to avoid the memleak warning */ |
| 2164 | atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); |
| 2165 | kfree_rcu(iml, rcu); |
| 2166 | return 0; |
| 2167 | } |
| 2168 | out: |
| 2169 | return ret; |
| 2170 | } |
| 2171 | EXPORT_SYMBOL(ip_mc_leave_group); |
| 2172 | |
| 2173 | int ip_mc_source(int add, int omode, struct sock *sk, struct |
| 2174 | ip_mreq_source *mreqs, int ifindex) |
| 2175 | { |
| 2176 | int err; |
| 2177 | struct ip_mreqn imr; |
| 2178 | __be32 addr = mreqs->imr_multiaddr; |
| 2179 | struct ip_mc_socklist *pmc; |
| 2180 | struct in_device *in_dev = NULL; |
| 2181 | struct inet_sock *inet = inet_sk(sk); |
| 2182 | struct ip_sf_socklist *psl; |
| 2183 | struct net *net = sock_net(sk); |
| 2184 | int leavegroup = 0; |
| 2185 | int i, j, rv; |
| 2186 | |
| 2187 | if (!ipv4_is_multicast(addr)) |
| 2188 | return -EINVAL; |
| 2189 | |
| 2190 | ASSERT_RTNL(); |
| 2191 | |
| 2192 | imr.imr_multiaddr.s_addr = mreqs->imr_multiaddr; |
| 2193 | imr.imr_address.s_addr = mreqs->imr_interface; |
| 2194 | imr.imr_ifindex = ifindex; |
| 2195 | in_dev = ip_mc_find_dev(net, &imr); |
| 2196 | |
| 2197 | if (!in_dev) { |
| 2198 | err = -ENODEV; |
| 2199 | goto done; |
| 2200 | } |
| 2201 | err = -EADDRNOTAVAIL; |
| 2202 | |
| 2203 | for_each_pmc_rtnl(inet, pmc) { |
| 2204 | if ((pmc->multi.imr_multiaddr.s_addr == |
| 2205 | imr.imr_multiaddr.s_addr) && |
| 2206 | (pmc->multi.imr_ifindex == imr.imr_ifindex)) |
| 2207 | break; |
| 2208 | } |
| 2209 | if (!pmc) { /* must have a prior join */ |
| 2210 | err = -EINVAL; |
| 2211 | goto done; |
| 2212 | } |
| 2213 | /* if a source filter was set, must be the same mode as before */ |
| 2214 | if (pmc->sflist) { |
| 2215 | if (pmc->sfmode != omode) { |
| 2216 | err = -EINVAL; |
| 2217 | goto done; |
| 2218 | } |
| 2219 | } else if (pmc->sfmode != omode) { |
| 2220 | /* allow mode switches for empty-set filters */ |
| 2221 | ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 0, NULL, 0); |
| 2222 | ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, pmc->sfmode, 0, |
| 2223 | NULL, 0); |
| 2224 | pmc->sfmode = omode; |
| 2225 | } |
| 2226 | |
| 2227 | psl = rtnl_dereference(pmc->sflist); |
| 2228 | if (!add) { |
| 2229 | if (!psl) |
| 2230 | goto done; /* err = -EADDRNOTAVAIL */ |
| 2231 | rv = !0; |
| 2232 | for (i = 0; i < psl->sl_count; i++) { |
| 2233 | rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, |
| 2234 | sizeof(__be32)); |
| 2235 | if (rv == 0) |
| 2236 | break; |
| 2237 | } |
| 2238 | if (rv) /* source not found */ |
| 2239 | goto done; /* err = -EADDRNOTAVAIL */ |
| 2240 | |
| 2241 | /* special case - (INCLUDE, empty) == LEAVE_GROUP */ |
| 2242 | if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { |
| 2243 | leavegroup = 1; |
| 2244 | goto done; |
| 2245 | } |
| 2246 | |
| 2247 | /* update the interface filter */ |
| 2248 | ip_mc_del_src(in_dev, &mreqs->imr_multiaddr, omode, 1, |
| 2249 | &mreqs->imr_sourceaddr, 1); |
| 2250 | |
| 2251 | for (j = i+1; j < psl->sl_count; j++) |
| 2252 | psl->sl_addr[j-1] = psl->sl_addr[j]; |
| 2253 | psl->sl_count--; |
| 2254 | err = 0; |
| 2255 | goto done; |
| 2256 | } |
| 2257 | /* else, add a new source to the filter */ |
| 2258 | |
| 2259 | if (psl && psl->sl_count >= sysctl_igmp_max_msf) { |
| 2260 | err = -ENOBUFS; |
| 2261 | goto done; |
| 2262 | } |
| 2263 | if (!psl || psl->sl_count == psl->sl_max) { |
| 2264 | struct ip_sf_socklist *newpsl; |
| 2265 | int count = IP_SFBLOCK; |
| 2266 | |
| 2267 | if (psl) |
| 2268 | count += psl->sl_max; |
| 2269 | newpsl = sock_kmalloc(sk, IP_SFLSIZE(count), GFP_KERNEL); |
| 2270 | if (!newpsl) { |
| 2271 | err = -ENOBUFS; |
| 2272 | goto done; |
| 2273 | } |
| 2274 | newpsl->sl_max = count; |
| 2275 | newpsl->sl_count = count - IP_SFBLOCK; |
| 2276 | if (psl) { |
| 2277 | for (i = 0; i < psl->sl_count; i++) |
| 2278 | newpsl->sl_addr[i] = psl->sl_addr[i]; |
| 2279 | /* decrease mem now to avoid the memleak warning */ |
| 2280 | atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); |
| 2281 | kfree_rcu(psl, rcu); |
| 2282 | } |
| 2283 | rcu_assign_pointer(pmc->sflist, newpsl); |
| 2284 | psl = newpsl; |
| 2285 | } |
| 2286 | rv = 1; /* > 0 for insert logic below if sl_count is 0 */ |
| 2287 | for (i = 0; i < psl->sl_count; i++) { |
| 2288 | rv = memcmp(&psl->sl_addr[i], &mreqs->imr_sourceaddr, |
| 2289 | sizeof(__be32)); |
| 2290 | if (rv == 0) |
| 2291 | break; |
| 2292 | } |
| 2293 | if (rv == 0) /* address already there is an error */ |
| 2294 | goto done; |
| 2295 | for (j = psl->sl_count-1; j >= i; j--) |
| 2296 | psl->sl_addr[j+1] = psl->sl_addr[j]; |
| 2297 | psl->sl_addr[i] = mreqs->imr_sourceaddr; |
| 2298 | psl->sl_count++; |
| 2299 | err = 0; |
| 2300 | /* update the interface list */ |
| 2301 | ip_mc_add_src(in_dev, &mreqs->imr_multiaddr, omode, 1, |
| 2302 | &mreqs->imr_sourceaddr, 1); |
| 2303 | done: |
| 2304 | if (leavegroup) |
| 2305 | err = ip_mc_leave_group(sk, &imr); |
| 2306 | return err; |
| 2307 | } |
| 2308 | |
| 2309 | int ip_mc_msfilter(struct sock *sk, struct ip_msfilter *msf, int ifindex) |
| 2310 | { |
| 2311 | int err = 0; |
| 2312 | struct ip_mreqn imr; |
| 2313 | __be32 addr = msf->imsf_multiaddr; |
| 2314 | struct ip_mc_socklist *pmc; |
| 2315 | struct in_device *in_dev; |
| 2316 | struct inet_sock *inet = inet_sk(sk); |
| 2317 | struct ip_sf_socklist *newpsl, *psl; |
| 2318 | struct net *net = sock_net(sk); |
| 2319 | int leavegroup = 0; |
| 2320 | |
| 2321 | if (!ipv4_is_multicast(addr)) |
| 2322 | return -EINVAL; |
| 2323 | if (msf->imsf_fmode != MCAST_INCLUDE && |
| 2324 | msf->imsf_fmode != MCAST_EXCLUDE) |
| 2325 | return -EINVAL; |
| 2326 | |
| 2327 | ASSERT_RTNL(); |
| 2328 | |
| 2329 | imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; |
| 2330 | imr.imr_address.s_addr = msf->imsf_interface; |
| 2331 | imr.imr_ifindex = ifindex; |
| 2332 | in_dev = ip_mc_find_dev(net, &imr); |
| 2333 | |
| 2334 | if (!in_dev) { |
| 2335 | err = -ENODEV; |
| 2336 | goto done; |
| 2337 | } |
| 2338 | |
| 2339 | /* special case - (INCLUDE, empty) == LEAVE_GROUP */ |
| 2340 | if (msf->imsf_fmode == MCAST_INCLUDE && msf->imsf_numsrc == 0) { |
| 2341 | leavegroup = 1; |
| 2342 | goto done; |
| 2343 | } |
| 2344 | |
| 2345 | for_each_pmc_rtnl(inet, pmc) { |
| 2346 | if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && |
| 2347 | pmc->multi.imr_ifindex == imr.imr_ifindex) |
| 2348 | break; |
| 2349 | } |
| 2350 | if (!pmc) { /* must have a prior join */ |
| 2351 | err = -EINVAL; |
| 2352 | goto done; |
| 2353 | } |
| 2354 | if (msf->imsf_numsrc) { |
| 2355 | newpsl = sock_kmalloc(sk, IP_SFLSIZE(msf->imsf_numsrc), |
| 2356 | GFP_KERNEL); |
| 2357 | if (!newpsl) { |
| 2358 | err = -ENOBUFS; |
| 2359 | goto done; |
| 2360 | } |
| 2361 | newpsl->sl_max = newpsl->sl_count = msf->imsf_numsrc; |
| 2362 | memcpy(newpsl->sl_addr, msf->imsf_slist, |
| 2363 | msf->imsf_numsrc * sizeof(msf->imsf_slist[0])); |
| 2364 | err = ip_mc_add_src(in_dev, &msf->imsf_multiaddr, |
| 2365 | msf->imsf_fmode, newpsl->sl_count, newpsl->sl_addr, 0); |
| 2366 | if (err) { |
| 2367 | sock_kfree_s(sk, newpsl, IP_SFLSIZE(newpsl->sl_max)); |
| 2368 | goto done; |
| 2369 | } |
| 2370 | } else { |
| 2371 | newpsl = NULL; |
| 2372 | (void) ip_mc_add_src(in_dev, &msf->imsf_multiaddr, |
| 2373 | msf->imsf_fmode, 0, NULL, 0); |
| 2374 | } |
| 2375 | psl = rtnl_dereference(pmc->sflist); |
| 2376 | if (psl) { |
| 2377 | (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, |
| 2378 | psl->sl_count, psl->sl_addr, 0); |
| 2379 | /* decrease mem now to avoid the memleak warning */ |
| 2380 | atomic_sub(IP_SFLSIZE(psl->sl_max), &sk->sk_omem_alloc); |
| 2381 | kfree_rcu(psl, rcu); |
| 2382 | } else |
| 2383 | (void) ip_mc_del_src(in_dev, &msf->imsf_multiaddr, pmc->sfmode, |
| 2384 | 0, NULL, 0); |
| 2385 | rcu_assign_pointer(pmc->sflist, newpsl); |
| 2386 | pmc->sfmode = msf->imsf_fmode; |
| 2387 | err = 0; |
| 2388 | done: |
| 2389 | if (leavegroup) |
| 2390 | err = ip_mc_leave_group(sk, &imr); |
| 2391 | return err; |
| 2392 | } |
| 2393 | |
| 2394 | int ip_mc_msfget(struct sock *sk, struct ip_msfilter *msf, |
| 2395 | struct ip_msfilter __user *optval, int __user *optlen) |
| 2396 | { |
| 2397 | int err, len, count, copycount; |
| 2398 | struct ip_mreqn imr; |
| 2399 | __be32 addr = msf->imsf_multiaddr; |
| 2400 | struct ip_mc_socklist *pmc; |
| 2401 | struct in_device *in_dev; |
| 2402 | struct inet_sock *inet = inet_sk(sk); |
| 2403 | struct ip_sf_socklist *psl; |
| 2404 | struct net *net = sock_net(sk); |
| 2405 | |
| 2406 | ASSERT_RTNL(); |
| 2407 | |
| 2408 | if (!ipv4_is_multicast(addr)) |
| 2409 | return -EINVAL; |
| 2410 | |
| 2411 | imr.imr_multiaddr.s_addr = msf->imsf_multiaddr; |
| 2412 | imr.imr_address.s_addr = msf->imsf_interface; |
| 2413 | imr.imr_ifindex = 0; |
| 2414 | in_dev = ip_mc_find_dev(net, &imr); |
| 2415 | |
| 2416 | if (!in_dev) { |
| 2417 | err = -ENODEV; |
| 2418 | goto done; |
| 2419 | } |
| 2420 | err = -EADDRNOTAVAIL; |
| 2421 | |
| 2422 | for_each_pmc_rtnl(inet, pmc) { |
| 2423 | if (pmc->multi.imr_multiaddr.s_addr == msf->imsf_multiaddr && |
| 2424 | pmc->multi.imr_ifindex == imr.imr_ifindex) |
| 2425 | break; |
| 2426 | } |
| 2427 | if (!pmc) /* must have a prior join */ |
| 2428 | goto done; |
| 2429 | msf->imsf_fmode = pmc->sfmode; |
| 2430 | psl = rtnl_dereference(pmc->sflist); |
| 2431 | if (!psl) { |
| 2432 | len = 0; |
| 2433 | count = 0; |
| 2434 | } else { |
| 2435 | count = psl->sl_count; |
| 2436 | } |
| 2437 | copycount = count < msf->imsf_numsrc ? count : msf->imsf_numsrc; |
| 2438 | len = copycount * sizeof(psl->sl_addr[0]); |
| 2439 | msf->imsf_numsrc = count; |
| 2440 | if (put_user(IP_MSFILTER_SIZE(copycount), optlen) || |
| 2441 | copy_to_user(optval, msf, IP_MSFILTER_SIZE(0))) { |
| 2442 | return -EFAULT; |
| 2443 | } |
| 2444 | if (len && |
| 2445 | copy_to_user(&optval->imsf_slist[0], psl->sl_addr, len)) |
| 2446 | return -EFAULT; |
| 2447 | return 0; |
| 2448 | done: |
| 2449 | return err; |
| 2450 | } |
| 2451 | |
| 2452 | int ip_mc_gsfget(struct sock *sk, struct group_filter *gsf, |
| 2453 | struct group_filter __user *optval, int __user *optlen) |
| 2454 | { |
| 2455 | int err, i, count, copycount; |
| 2456 | struct sockaddr_in *psin; |
| 2457 | __be32 addr; |
| 2458 | struct ip_mc_socklist *pmc; |
| 2459 | struct inet_sock *inet = inet_sk(sk); |
| 2460 | struct ip_sf_socklist *psl; |
| 2461 | |
| 2462 | ASSERT_RTNL(); |
| 2463 | |
| 2464 | psin = (struct sockaddr_in *)&gsf->gf_group; |
| 2465 | if (psin->sin_family != AF_INET) |
| 2466 | return -EINVAL; |
| 2467 | addr = psin->sin_addr.s_addr; |
| 2468 | if (!ipv4_is_multicast(addr)) |
| 2469 | return -EINVAL; |
| 2470 | |
| 2471 | err = -EADDRNOTAVAIL; |
| 2472 | |
| 2473 | for_each_pmc_rtnl(inet, pmc) { |
| 2474 | if (pmc->multi.imr_multiaddr.s_addr == addr && |
| 2475 | pmc->multi.imr_ifindex == gsf->gf_interface) |
| 2476 | break; |
| 2477 | } |
| 2478 | if (!pmc) /* must have a prior join */ |
| 2479 | goto done; |
| 2480 | gsf->gf_fmode = pmc->sfmode; |
| 2481 | psl = rtnl_dereference(pmc->sflist); |
| 2482 | count = psl ? psl->sl_count : 0; |
| 2483 | copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; |
| 2484 | gsf->gf_numsrc = count; |
| 2485 | if (put_user(GROUP_FILTER_SIZE(copycount), optlen) || |
| 2486 | copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) { |
| 2487 | return -EFAULT; |
| 2488 | } |
| 2489 | for (i = 0; i < copycount; i++) { |
| 2490 | struct sockaddr_storage ss; |
| 2491 | |
| 2492 | psin = (struct sockaddr_in *)&ss; |
| 2493 | memset(&ss, 0, sizeof(ss)); |
| 2494 | psin->sin_family = AF_INET; |
| 2495 | psin->sin_addr.s_addr = psl->sl_addr[i]; |
| 2496 | if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss))) |
| 2497 | return -EFAULT; |
| 2498 | } |
| 2499 | return 0; |
| 2500 | done: |
| 2501 | return err; |
| 2502 | } |
| 2503 | |
| 2504 | /* |
| 2505 | * check if a multicast source filter allows delivery for a given <src,dst,intf> |
| 2506 | */ |
| 2507 | int ip_mc_sf_allow(struct sock *sk, __be32 loc_addr, __be32 rmt_addr, int dif) |
| 2508 | { |
| 2509 | struct inet_sock *inet = inet_sk(sk); |
| 2510 | struct ip_mc_socklist *pmc; |
| 2511 | struct ip_sf_socklist *psl; |
| 2512 | int i; |
| 2513 | int ret; |
| 2514 | |
| 2515 | ret = 1; |
| 2516 | if (!ipv4_is_multicast(loc_addr)) |
| 2517 | goto out; |
| 2518 | |
| 2519 | rcu_read_lock(); |
| 2520 | for_each_pmc_rcu(inet, pmc) { |
| 2521 | if (pmc->multi.imr_multiaddr.s_addr == loc_addr && |
| 2522 | pmc->multi.imr_ifindex == dif) |
| 2523 | break; |
| 2524 | } |
| 2525 | ret = inet->mc_all; |
| 2526 | if (!pmc) |
| 2527 | goto unlock; |
| 2528 | psl = rcu_dereference(pmc->sflist); |
| 2529 | ret = (pmc->sfmode == MCAST_EXCLUDE); |
| 2530 | if (!psl) |
| 2531 | goto unlock; |
| 2532 | |
| 2533 | for (i = 0; i < psl->sl_count; i++) { |
| 2534 | if (psl->sl_addr[i] == rmt_addr) |
| 2535 | break; |
| 2536 | } |
| 2537 | ret = 0; |
| 2538 | if (pmc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) |
| 2539 | goto unlock; |
| 2540 | if (pmc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) |
| 2541 | goto unlock; |
| 2542 | ret = 1; |
| 2543 | unlock: |
| 2544 | rcu_read_unlock(); |
| 2545 | out: |
| 2546 | return ret; |
| 2547 | } |
| 2548 | |
| 2549 | /* |
| 2550 | * A socket is closing. |
| 2551 | */ |
| 2552 | |
| 2553 | void ip_mc_drop_socket(struct sock *sk) |
| 2554 | { |
| 2555 | struct inet_sock *inet = inet_sk(sk); |
| 2556 | struct ip_mc_socklist *iml; |
| 2557 | struct net *net = sock_net(sk); |
| 2558 | |
| 2559 | if (!inet->mc_list) |
| 2560 | return; |
| 2561 | |
| 2562 | rtnl_lock(); |
| 2563 | while ((iml = rtnl_dereference(inet->mc_list)) != NULL) { |
| 2564 | struct in_device *in_dev; |
| 2565 | |
| 2566 | inet->mc_list = iml->next_rcu; |
| 2567 | in_dev = inetdev_by_index(net, iml->multi.imr_ifindex); |
| 2568 | (void) ip_mc_leave_src(sk, iml, in_dev); |
| 2569 | if (in_dev) |
| 2570 | ip_mc_dec_group(in_dev, iml->multi.imr_multiaddr.s_addr); |
| 2571 | /* decrease mem now to avoid the memleak warning */ |
| 2572 | atomic_sub(sizeof(*iml), &sk->sk_omem_alloc); |
| 2573 | kfree_rcu(iml, rcu); |
| 2574 | } |
| 2575 | rtnl_unlock(); |
| 2576 | } |
| 2577 | |
| 2578 | /* called with rcu_read_lock() */ |
| 2579 | int ip_check_mc_rcu(struct in_device *in_dev, __be32 mc_addr, __be32 src_addr, u8 proto) |
| 2580 | { |
| 2581 | struct ip_mc_list *im; |
| 2582 | struct ip_mc_list __rcu **mc_hash; |
| 2583 | struct ip_sf_list *psf; |
| 2584 | int rv = 0; |
| 2585 | |
| 2586 | mc_hash = rcu_dereference(in_dev->mc_hash); |
| 2587 | if (mc_hash) { |
| 2588 | u32 hash = hash_32((__force u32)mc_addr, MC_HASH_SZ_LOG); |
| 2589 | |
| 2590 | for (im = rcu_dereference(mc_hash[hash]); |
| 2591 | im != NULL; |
| 2592 | im = rcu_dereference(im->next_hash)) { |
| 2593 | if (im->multiaddr == mc_addr) |
| 2594 | break; |
| 2595 | } |
| 2596 | } else { |
| 2597 | for_each_pmc_rcu(in_dev, im) { |
| 2598 | if (im->multiaddr == mc_addr) |
| 2599 | break; |
| 2600 | } |
| 2601 | } |
| 2602 | if (im && proto == IPPROTO_IGMP) { |
| 2603 | rv = 1; |
| 2604 | } else if (im) { |
| 2605 | if (src_addr) { |
| 2606 | for (psf = im->sources; psf; psf = psf->sf_next) { |
| 2607 | if (psf->sf_inaddr == src_addr) |
| 2608 | break; |
| 2609 | } |
| 2610 | if (psf) |
| 2611 | rv = psf->sf_count[MCAST_INCLUDE] || |
| 2612 | psf->sf_count[MCAST_EXCLUDE] != |
| 2613 | im->sfcount[MCAST_EXCLUDE]; |
| 2614 | else |
| 2615 | rv = im->sfcount[MCAST_EXCLUDE] != 0; |
| 2616 | } else |
| 2617 | rv = 1; /* unspecified source; tentatively allow */ |
| 2618 | } |
| 2619 | return rv; |
| 2620 | } |
| 2621 | |
| 2622 | #if defined(CONFIG_PROC_FS) |
| 2623 | struct igmp_mc_iter_state { |
| 2624 | struct seq_net_private p; |
| 2625 | struct net_device *dev; |
| 2626 | struct in_device *in_dev; |
| 2627 | }; |
| 2628 | |
| 2629 | #define igmp_mc_seq_private(seq) ((struct igmp_mc_iter_state *)(seq)->private) |
| 2630 | |
| 2631 | static inline struct ip_mc_list *igmp_mc_get_first(struct seq_file *seq) |
| 2632 | { |
| 2633 | struct net *net = seq_file_net(seq); |
| 2634 | struct ip_mc_list *im = NULL; |
| 2635 | struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); |
| 2636 | |
| 2637 | state->in_dev = NULL; |
| 2638 | for_each_netdev_rcu(net, state->dev) { |
| 2639 | struct in_device *in_dev; |
| 2640 | |
| 2641 | in_dev = __in_dev_get_rcu(state->dev); |
| 2642 | if (!in_dev) |
| 2643 | continue; |
| 2644 | im = rcu_dereference(in_dev->mc_list); |
| 2645 | if (im) { |
| 2646 | state->in_dev = in_dev; |
| 2647 | break; |
| 2648 | } |
| 2649 | } |
| 2650 | return im; |
| 2651 | } |
| 2652 | |
| 2653 | static struct ip_mc_list *igmp_mc_get_next(struct seq_file *seq, struct ip_mc_list *im) |
| 2654 | { |
| 2655 | struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); |
| 2656 | |
| 2657 | im = rcu_dereference(im->next_rcu); |
| 2658 | while (!im) { |
| 2659 | state->dev = next_net_device_rcu(state->dev); |
| 2660 | if (!state->dev) { |
| 2661 | state->in_dev = NULL; |
| 2662 | break; |
| 2663 | } |
| 2664 | state->in_dev = __in_dev_get_rcu(state->dev); |
| 2665 | if (!state->in_dev) |
| 2666 | continue; |
| 2667 | im = rcu_dereference(state->in_dev->mc_list); |
| 2668 | } |
| 2669 | return im; |
| 2670 | } |
| 2671 | |
| 2672 | static struct ip_mc_list *igmp_mc_get_idx(struct seq_file *seq, loff_t pos) |
| 2673 | { |
| 2674 | struct ip_mc_list *im = igmp_mc_get_first(seq); |
| 2675 | if (im) |
| 2676 | while (pos && (im = igmp_mc_get_next(seq, im)) != NULL) |
| 2677 | --pos; |
| 2678 | return pos ? NULL : im; |
| 2679 | } |
| 2680 | |
| 2681 | static void *igmp_mc_seq_start(struct seq_file *seq, loff_t *pos) |
| 2682 | __acquires(rcu) |
| 2683 | { |
| 2684 | rcu_read_lock(); |
| 2685 | return *pos ? igmp_mc_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 2686 | } |
| 2687 | |
| 2688 | static void *igmp_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2689 | { |
| 2690 | struct ip_mc_list *im; |
| 2691 | if (v == SEQ_START_TOKEN) |
| 2692 | im = igmp_mc_get_first(seq); |
| 2693 | else |
| 2694 | im = igmp_mc_get_next(seq, v); |
| 2695 | ++*pos; |
| 2696 | return im; |
| 2697 | } |
| 2698 | |
| 2699 | static void igmp_mc_seq_stop(struct seq_file *seq, void *v) |
| 2700 | __releases(rcu) |
| 2701 | { |
| 2702 | struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); |
| 2703 | |
| 2704 | state->in_dev = NULL; |
| 2705 | state->dev = NULL; |
| 2706 | rcu_read_unlock(); |
| 2707 | } |
| 2708 | |
| 2709 | static int igmp_mc_seq_show(struct seq_file *seq, void *v) |
| 2710 | { |
| 2711 | if (v == SEQ_START_TOKEN) |
| 2712 | seq_puts(seq, |
| 2713 | "Idx\tDevice : Count Querier\tGroup Users Timer\tReporter\n"); |
| 2714 | else { |
| 2715 | struct ip_mc_list *im = (struct ip_mc_list *)v; |
| 2716 | struct igmp_mc_iter_state *state = igmp_mc_seq_private(seq); |
| 2717 | char *querier; |
| 2718 | long delta; |
| 2719 | |
| 2720 | #ifdef CONFIG_IP_MULTICAST |
| 2721 | querier = IGMP_V1_SEEN(state->in_dev) ? "V1" : |
| 2722 | IGMP_V2_SEEN(state->in_dev) ? "V2" : |
| 2723 | "V3"; |
| 2724 | #else |
| 2725 | querier = "NONE"; |
| 2726 | #endif |
| 2727 | |
| 2728 | if (rcu_access_pointer(state->in_dev->mc_list) == im) { |
| 2729 | seq_printf(seq, "%d\t%-10s: %5d %7s\n", |
| 2730 | state->dev->ifindex, state->dev->name, state->in_dev->mc_count, querier); |
| 2731 | } |
| 2732 | |
| 2733 | delta = im->timer.expires - jiffies; |
| 2734 | seq_printf(seq, |
| 2735 | "\t\t\t\t%08X %5d %d:%08lX\t\t%d\n", |
| 2736 | im->multiaddr, im->users, |
| 2737 | im->tm_running, |
| 2738 | im->tm_running ? jiffies_delta_to_clock_t(delta) : 0, |
| 2739 | im->reporter); |
| 2740 | } |
| 2741 | return 0; |
| 2742 | } |
| 2743 | |
| 2744 | static const struct seq_operations igmp_mc_seq_ops = { |
| 2745 | .start = igmp_mc_seq_start, |
| 2746 | .next = igmp_mc_seq_next, |
| 2747 | .stop = igmp_mc_seq_stop, |
| 2748 | .show = igmp_mc_seq_show, |
| 2749 | }; |
| 2750 | |
| 2751 | static int igmp_mc_seq_open(struct inode *inode, struct file *file) |
| 2752 | { |
| 2753 | return seq_open_net(inode, file, &igmp_mc_seq_ops, |
| 2754 | sizeof(struct igmp_mc_iter_state)); |
| 2755 | } |
| 2756 | |
| 2757 | static const struct file_operations igmp_mc_seq_fops = { |
| 2758 | .owner = THIS_MODULE, |
| 2759 | .open = igmp_mc_seq_open, |
| 2760 | .read = seq_read, |
| 2761 | .llseek = seq_lseek, |
| 2762 | .release = seq_release_net, |
| 2763 | }; |
| 2764 | |
| 2765 | struct igmp_mcf_iter_state { |
| 2766 | struct seq_net_private p; |
| 2767 | struct net_device *dev; |
| 2768 | struct in_device *idev; |
| 2769 | struct ip_mc_list *im; |
| 2770 | }; |
| 2771 | |
| 2772 | #define igmp_mcf_seq_private(seq) ((struct igmp_mcf_iter_state *)(seq)->private) |
| 2773 | |
| 2774 | static inline struct ip_sf_list *igmp_mcf_get_first(struct seq_file *seq) |
| 2775 | { |
| 2776 | struct net *net = seq_file_net(seq); |
| 2777 | struct ip_sf_list *psf = NULL; |
| 2778 | struct ip_mc_list *im = NULL; |
| 2779 | struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); |
| 2780 | |
| 2781 | state->idev = NULL; |
| 2782 | state->im = NULL; |
| 2783 | for_each_netdev_rcu(net, state->dev) { |
| 2784 | struct in_device *idev; |
| 2785 | idev = __in_dev_get_rcu(state->dev); |
| 2786 | if (unlikely(!idev)) |
| 2787 | continue; |
| 2788 | im = rcu_dereference(idev->mc_list); |
| 2789 | if (likely(im)) { |
| 2790 | spin_lock_bh(&im->lock); |
| 2791 | psf = im->sources; |
| 2792 | if (likely(psf)) { |
| 2793 | state->im = im; |
| 2794 | state->idev = idev; |
| 2795 | break; |
| 2796 | } |
| 2797 | spin_unlock_bh(&im->lock); |
| 2798 | } |
| 2799 | } |
| 2800 | return psf; |
| 2801 | } |
| 2802 | |
| 2803 | static struct ip_sf_list *igmp_mcf_get_next(struct seq_file *seq, struct ip_sf_list *psf) |
| 2804 | { |
| 2805 | struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); |
| 2806 | |
| 2807 | psf = psf->sf_next; |
| 2808 | while (!psf) { |
| 2809 | spin_unlock_bh(&state->im->lock); |
| 2810 | state->im = state->im->next; |
| 2811 | while (!state->im) { |
| 2812 | state->dev = next_net_device_rcu(state->dev); |
| 2813 | if (!state->dev) { |
| 2814 | state->idev = NULL; |
| 2815 | goto out; |
| 2816 | } |
| 2817 | state->idev = __in_dev_get_rcu(state->dev); |
| 2818 | if (!state->idev) |
| 2819 | continue; |
| 2820 | state->im = rcu_dereference(state->idev->mc_list); |
| 2821 | } |
| 2822 | if (!state->im) |
| 2823 | break; |
| 2824 | spin_lock_bh(&state->im->lock); |
| 2825 | psf = state->im->sources; |
| 2826 | } |
| 2827 | out: |
| 2828 | return psf; |
| 2829 | } |
| 2830 | |
| 2831 | static struct ip_sf_list *igmp_mcf_get_idx(struct seq_file *seq, loff_t pos) |
| 2832 | { |
| 2833 | struct ip_sf_list *psf = igmp_mcf_get_first(seq); |
| 2834 | if (psf) |
| 2835 | while (pos && (psf = igmp_mcf_get_next(seq, psf)) != NULL) |
| 2836 | --pos; |
| 2837 | return pos ? NULL : psf; |
| 2838 | } |
| 2839 | |
| 2840 | static void *igmp_mcf_seq_start(struct seq_file *seq, loff_t *pos) |
| 2841 | __acquires(rcu) |
| 2842 | { |
| 2843 | rcu_read_lock(); |
| 2844 | return *pos ? igmp_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 2845 | } |
| 2846 | |
| 2847 | static void *igmp_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2848 | { |
| 2849 | struct ip_sf_list *psf; |
| 2850 | if (v == SEQ_START_TOKEN) |
| 2851 | psf = igmp_mcf_get_first(seq); |
| 2852 | else |
| 2853 | psf = igmp_mcf_get_next(seq, v); |
| 2854 | ++*pos; |
| 2855 | return psf; |
| 2856 | } |
| 2857 | |
| 2858 | static void igmp_mcf_seq_stop(struct seq_file *seq, void *v) |
| 2859 | __releases(rcu) |
| 2860 | { |
| 2861 | struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); |
| 2862 | if (likely(state->im)) { |
| 2863 | spin_unlock_bh(&state->im->lock); |
| 2864 | state->im = NULL; |
| 2865 | } |
| 2866 | state->idev = NULL; |
| 2867 | state->dev = NULL; |
| 2868 | rcu_read_unlock(); |
| 2869 | } |
| 2870 | |
| 2871 | static int igmp_mcf_seq_show(struct seq_file *seq, void *v) |
| 2872 | { |
| 2873 | struct ip_sf_list *psf = (struct ip_sf_list *)v; |
| 2874 | struct igmp_mcf_iter_state *state = igmp_mcf_seq_private(seq); |
| 2875 | |
| 2876 | if (v == SEQ_START_TOKEN) { |
| 2877 | seq_puts(seq, "Idx Device MCA SRC INC EXC\n"); |
| 2878 | } else { |
| 2879 | seq_printf(seq, |
| 2880 | "%3d %6.6s 0x%08x " |
| 2881 | "0x%08x %6lu %6lu\n", |
| 2882 | state->dev->ifindex, state->dev->name, |
| 2883 | ntohl(state->im->multiaddr), |
| 2884 | ntohl(psf->sf_inaddr), |
| 2885 | psf->sf_count[MCAST_INCLUDE], |
| 2886 | psf->sf_count[MCAST_EXCLUDE]); |
| 2887 | } |
| 2888 | return 0; |
| 2889 | } |
| 2890 | |
| 2891 | static const struct seq_operations igmp_mcf_seq_ops = { |
| 2892 | .start = igmp_mcf_seq_start, |
| 2893 | .next = igmp_mcf_seq_next, |
| 2894 | .stop = igmp_mcf_seq_stop, |
| 2895 | .show = igmp_mcf_seq_show, |
| 2896 | }; |
| 2897 | |
| 2898 | static int igmp_mcf_seq_open(struct inode *inode, struct file *file) |
| 2899 | { |
| 2900 | return seq_open_net(inode, file, &igmp_mcf_seq_ops, |
| 2901 | sizeof(struct igmp_mcf_iter_state)); |
| 2902 | } |
| 2903 | |
| 2904 | static const struct file_operations igmp_mcf_seq_fops = { |
| 2905 | .owner = THIS_MODULE, |
| 2906 | .open = igmp_mcf_seq_open, |
| 2907 | .read = seq_read, |
| 2908 | .llseek = seq_lseek, |
| 2909 | .release = seq_release_net, |
| 2910 | }; |
| 2911 | |
| 2912 | static int __net_init igmp_net_init(struct net *net) |
| 2913 | { |
| 2914 | struct proc_dir_entry *pde; |
| 2915 | int err; |
| 2916 | |
| 2917 | pde = proc_create("igmp", S_IRUGO, net->proc_net, &igmp_mc_seq_fops); |
| 2918 | if (!pde) |
| 2919 | goto out_igmp; |
| 2920 | pde = proc_create("mcfilter", S_IRUGO, net->proc_net, |
| 2921 | &igmp_mcf_seq_fops); |
| 2922 | if (!pde) |
| 2923 | goto out_mcfilter; |
| 2924 | err = inet_ctl_sock_create(&net->ipv4.mc_autojoin_sk, AF_INET, |
| 2925 | SOCK_DGRAM, 0, net); |
| 2926 | if (err < 0) { |
| 2927 | pr_err("Failed to initialize the IGMP autojoin socket (err %d)\n", |
| 2928 | err); |
| 2929 | goto out_sock; |
| 2930 | } |
| 2931 | |
| 2932 | return 0; |
| 2933 | |
| 2934 | out_sock: |
| 2935 | remove_proc_entry("mcfilter", net->proc_net); |
| 2936 | out_mcfilter: |
| 2937 | remove_proc_entry("igmp", net->proc_net); |
| 2938 | out_igmp: |
| 2939 | return -ENOMEM; |
| 2940 | } |
| 2941 | |
| 2942 | static void __net_exit igmp_net_exit(struct net *net) |
| 2943 | { |
| 2944 | remove_proc_entry("mcfilter", net->proc_net); |
| 2945 | remove_proc_entry("igmp", net->proc_net); |
| 2946 | inet_ctl_sock_destroy(net->ipv4.mc_autojoin_sk); |
| 2947 | } |
| 2948 | |
| 2949 | static struct pernet_operations igmp_net_ops = { |
| 2950 | .init = igmp_net_init, |
| 2951 | .exit = igmp_net_exit, |
| 2952 | }; |
| 2953 | #endif |
| 2954 | |
| 2955 | static int igmp_netdev_event(struct notifier_block *this, |
| 2956 | unsigned long event, void *ptr) |
| 2957 | { |
| 2958 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
| 2959 | struct in_device *in_dev; |
| 2960 | |
| 2961 | switch (event) { |
| 2962 | case NETDEV_RESEND_IGMP: |
| 2963 | in_dev = __in_dev_get_rtnl(dev); |
| 2964 | if (in_dev) |
| 2965 | ip_mc_rejoin_groups(in_dev); |
| 2966 | break; |
| 2967 | default: |
| 2968 | break; |
| 2969 | } |
| 2970 | return NOTIFY_DONE; |
| 2971 | } |
| 2972 | |
| 2973 | static struct notifier_block igmp_notifier = { |
| 2974 | .notifier_call = igmp_netdev_event, |
| 2975 | }; |
| 2976 | |
| 2977 | int __init igmp_mc_init(void) |
| 2978 | { |
| 2979 | #if defined(CONFIG_PROC_FS) |
| 2980 | int err; |
| 2981 | |
| 2982 | err = register_pernet_subsys(&igmp_net_ops); |
| 2983 | if (err) |
| 2984 | return err; |
| 2985 | err = register_netdevice_notifier(&igmp_notifier); |
| 2986 | if (err) |
| 2987 | goto reg_notif_fail; |
| 2988 | return 0; |
| 2989 | |
| 2990 | reg_notif_fail: |
| 2991 | unregister_pernet_subsys(&igmp_net_ops); |
| 2992 | return err; |
| 2993 | #else |
| 2994 | return register_netdevice_notifier(&igmp_notifier); |
| 2995 | #endif |
| 2996 | } |