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