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