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