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