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