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