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