Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Multicast support for IPv6 |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 3 | * Linux INET6 implementation |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * Authors: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 6 | * Pedro Roque <roque@di.fc.ul.pt> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 8 | * Based on linux/ipv4/igmp.c and linux/ipv4/ip_sockglue.c |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License |
| 12 | * as published by the Free Software Foundation; either version |
| 13 | * 2 of the License, or (at your option) any later version. |
| 14 | */ |
| 15 | |
| 16 | /* Changes: |
| 17 | * |
| 18 | * yoshfuji : fix format of router-alert option |
| 19 | * YOSHIFUJI Hideaki @USAGI: |
| 20 | * Fixed source address for MLD message based on |
| 21 | * <draft-ietf-magma-mld-source-05.txt>. |
| 22 | * YOSHIFUJI Hideaki @USAGI: |
| 23 | * - Ignore Queries for invalid addresses. |
| 24 | * - MLD for link-local addresses. |
| 25 | * David L Stevens <dlstevens@us.ibm.com>: |
| 26 | * - MLDv2 support |
| 27 | */ |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/module.h> |
| 30 | #include <linux/errno.h> |
| 31 | #include <linux/types.h> |
| 32 | #include <linux/string.h> |
| 33 | #include <linux/socket.h> |
| 34 | #include <linux/sockios.h> |
| 35 | #include <linux/jiffies.h> |
| 36 | #include <linux/times.h> |
| 37 | #include <linux/net.h> |
| 38 | #include <linux/in.h> |
| 39 | #include <linux/in6.h> |
| 40 | #include <linux/netdevice.h> |
| 41 | #include <linux/if_arp.h> |
| 42 | #include <linux/route.h> |
| 43 | #include <linux/init.h> |
| 44 | #include <linux/proc_fs.h> |
| 45 | #include <linux/seq_file.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 46 | #include <linux/slab.h> |
Hannes Frederic Sowa | 9d4a031 | 2013-07-26 17:05:16 +0200 | [diff] [blame^] | 47 | #include <linux/pkt_sched.h> |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 48 | #include <net/mld.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | #include <linux/netfilter.h> |
| 51 | #include <linux/netfilter_ipv6.h> |
| 52 | |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 53 | #include <net/net_namespace.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include <net/sock.h> |
| 55 | #include <net/snmp.h> |
| 56 | |
| 57 | #include <net/ipv6.h> |
| 58 | #include <net/protocol.h> |
| 59 | #include <net/if_inet6.h> |
| 60 | #include <net/ndisc.h> |
| 61 | #include <net/addrconf.h> |
| 62 | #include <net/ip6_route.h> |
Denis V. Lunev | 1ed8516 | 2008-04-03 14:31:03 -0700 | [diff] [blame] | 63 | #include <net/inet_common.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | |
| 65 | #include <net/ip6_checksum.h> |
| 66 | |
| 67 | /* Set to 3 to get tracing... */ |
| 68 | #define MCAST_DEBUG 2 |
| 69 | |
| 70 | #if MCAST_DEBUG >= 3 |
| 71 | #define MDBG(x) printk x |
| 72 | #else |
| 73 | #define MDBG(x) |
| 74 | #endif |
| 75 | |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 76 | /* Ensure that we have struct in6_addr aligned on 32bit word. */ |
| 77 | static void *__mld2_query_bugs[] __attribute__((__unused__)) = { |
| 78 | BUILD_BUG_ON_NULL(offsetof(struct mld2_query, mld2q_srcs) % 4), |
| 79 | BUILD_BUG_ON_NULL(offsetof(struct mld2_report, mld2r_grec) % 4), |
| 80 | BUILD_BUG_ON_NULL(offsetof(struct mld2_grec, grec_mca) % 4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | static struct in6_addr mld2_all_mcr = MLD2_ALL_MCR_INIT; |
| 84 | |
| 85 | /* Big mc list lock for all the sockets */ |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 86 | static DEFINE_SPINLOCK(ipv6_sk_mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 87 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | static void igmp6_join_group(struct ifmcaddr6 *ma); |
| 89 | static void igmp6_leave_group(struct ifmcaddr6 *ma); |
| 90 | static void igmp6_timer_handler(unsigned long data); |
| 91 | |
| 92 | static void mld_gq_timer_expire(unsigned long data); |
| 93 | static void mld_ifc_timer_expire(unsigned long data); |
| 94 | static void mld_ifc_event(struct inet6_dev *idev); |
| 95 | static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *pmc); |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 96 | static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | static void mld_clear_delrec(struct inet6_dev *idev); |
| 98 | static int sf_setstate(struct ifmcaddr6 *pmc); |
| 99 | static void sf_markstate(struct ifmcaddr6 *pmc); |
| 100 | static void ip6_mc_clear_src(struct ifmcaddr6 *pmc); |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 101 | static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca, |
| 102 | int sfmode, int sfcount, const struct in6_addr *psfsrc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | int delta); |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 104 | static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca, |
| 105 | int sfmode, int sfcount, const struct in6_addr *psfsrc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | int delta); |
| 107 | static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml, |
| 108 | struct inet6_dev *idev); |
| 109 | |
| 110 | |
| 111 | #define IGMP6_UNSOLICITED_IVAL (10*HZ) |
| 112 | #define MLD_QRV_DEFAULT 2 |
| 113 | |
YOSHIFUJI Hideaki | 53b7997 | 2008-07-19 22:35:03 -0700 | [diff] [blame] | 114 | #define MLD_V1_SEEN(idev) (dev_net((idev)->dev)->ipv6.devconf_all->force_mld_version == 1 || \ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | (idev)->cnf.force_mld_version == 1 || \ |
| 116 | ((idev)->mc_v1_seen && \ |
| 117 | time_before(jiffies, (idev)->mc_v1_seen))) |
| 118 | |
David L Stevens | 6f4353d | 2005-12-26 17:03:46 -0800 | [diff] [blame] | 119 | #define IPV6_MLD_MAX_MSF 64 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 120 | |
Brian Haley | ab32ea5 | 2006-09-22 14:15:41 -0700 | [diff] [blame] | 121 | int sysctl_mld_max_msf __read_mostly = IPV6_MLD_MAX_MSF; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 122 | |
| 123 | /* |
| 124 | * socket join on multicast group |
| 125 | */ |
| 126 | |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 127 | #define for_each_pmc_rcu(np, pmc) \ |
| 128 | for (pmc = rcu_dereference(np->ipv6_mc_list); \ |
| 129 | pmc != NULL; \ |
| 130 | pmc = rcu_dereference(pmc->next)) |
| 131 | |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 132 | int ipv6_sock_mc_join(struct sock *sk, int ifindex, const struct in6_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | { |
| 134 | struct net_device *dev = NULL; |
| 135 | struct ipv6_mc_socklist *mc_lst; |
| 136 | struct ipv6_pinfo *np = inet6_sk(sk); |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 137 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | int err; |
| 139 | |
| 140 | if (!ipv6_addr_is_multicast(addr)) |
| 141 | return -EINVAL; |
| 142 | |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 143 | rcu_read_lock(); |
| 144 | for_each_pmc_rcu(np, mc_lst) { |
David L Stevens | c9e3e8b | 2005-06-21 13:58:25 -0700 | [diff] [blame] | 145 | if ((ifindex == 0 || mc_lst->ifindex == ifindex) && |
| 146 | ipv6_addr_equal(&mc_lst->addr, addr)) { |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 147 | rcu_read_unlock(); |
David L Stevens | c9e3e8b | 2005-06-21 13:58:25 -0700 | [diff] [blame] | 148 | return -EADDRINUSE; |
| 149 | } |
| 150 | } |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 151 | rcu_read_unlock(); |
David L Stevens | c9e3e8b | 2005-06-21 13:58:25 -0700 | [diff] [blame] | 152 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 153 | mc_lst = sock_kmalloc(sk, sizeof(struct ipv6_mc_socklist), GFP_KERNEL); |
| 154 | |
| 155 | if (mc_lst == NULL) |
| 156 | return -ENOMEM; |
| 157 | |
| 158 | mc_lst->next = NULL; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 159 | mc_lst->addr = *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 161 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | if (ifindex == 0) { |
| 163 | struct rt6_info *rt; |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 164 | rt = rt6_lookup(net, addr, NULL, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | if (rt) { |
David S. Miller | d191854 | 2011-12-28 20:19:20 -0500 | [diff] [blame] | 166 | dev = rt->dst.dev; |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 167 | ip6_rt_put(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | } |
| 169 | } else |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 170 | dev = dev_get_by_index_rcu(net, ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | |
| 172 | if (dev == NULL) { |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 173 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 174 | sock_kfree_s(sk, mc_lst, sizeof(*mc_lst)); |
| 175 | return -ENODEV; |
| 176 | } |
| 177 | |
| 178 | mc_lst->ifindex = dev->ifindex; |
| 179 | mc_lst->sfmode = MCAST_EXCLUDE; |
YOSHIFUJI Hideaki | 196433c | 2006-01-04 13:56:31 -0800 | [diff] [blame] | 180 | rwlock_init(&mc_lst->sflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | mc_lst->sflist = NULL; |
| 182 | |
| 183 | /* |
| 184 | * now add/increase the group membership on the device |
| 185 | */ |
| 186 | |
| 187 | err = ipv6_dev_mc_inc(dev, addr); |
| 188 | |
| 189 | if (err) { |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 190 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | sock_kfree_s(sk, mc_lst, sizeof(*mc_lst)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | return err; |
| 193 | } |
| 194 | |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 195 | spin_lock(&ipv6_sk_mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | mc_lst->next = np->ipv6_mc_list; |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 197 | rcu_assign_pointer(np->ipv6_mc_list, mc_lst); |
| 198 | spin_unlock(&ipv6_sk_mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 200 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | return 0; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * socket leave on multicast group |
| 207 | */ |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 208 | int ipv6_sock_mc_drop(struct sock *sk, int ifindex, const struct in6_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | { |
| 210 | struct ipv6_pinfo *np = inet6_sk(sk); |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 211 | struct ipv6_mc_socklist *mc_lst; |
| 212 | struct ipv6_mc_socklist __rcu **lnk; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 213 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Li Wei | a858d64 | 2012-07-17 15:28:59 +0800 | [diff] [blame] | 215 | if (!ipv6_addr_is_multicast(addr)) |
| 216 | return -EINVAL; |
| 217 | |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 218 | spin_lock(&ipv6_sk_mc_lock); |
| 219 | for (lnk = &np->ipv6_mc_list; |
| 220 | (mc_lst = rcu_dereference_protected(*lnk, |
| 221 | lockdep_is_held(&ipv6_sk_mc_lock))) !=NULL ; |
| 222 | lnk = &mc_lst->next) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | if ((ifindex == 0 || mc_lst->ifindex == ifindex) && |
| 224 | ipv6_addr_equal(&mc_lst->addr, addr)) { |
| 225 | struct net_device *dev; |
| 226 | |
| 227 | *lnk = mc_lst->next; |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 228 | spin_unlock(&ipv6_sk_mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 230 | rcu_read_lock(); |
| 231 | dev = dev_get_by_index_rcu(net, mc_lst->ifindex); |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 232 | if (dev != NULL) { |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 233 | struct inet6_dev *idev = __in6_dev_get(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 235 | (void) ip6_mc_leave_src(sk, mc_lst, idev); |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 236 | if (idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 237 | __ipv6_dev_mc_dec(idev, &mc_lst->addr); |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 238 | } else |
| 239 | (void) ip6_mc_leave_src(sk, mc_lst, NULL); |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 240 | rcu_read_unlock(); |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 241 | atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc); |
Lai Jiangshan | e3cbf28 | 2011-03-18 12:00:50 +0800 | [diff] [blame] | 242 | kfree_rcu(mc_lst, rcu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | return 0; |
| 244 | } |
| 245 | } |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 246 | spin_unlock(&ipv6_sk_mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | |
David L Stevens | 9951f03 | 2005-07-08 17:47:28 -0700 | [diff] [blame] | 248 | return -EADDRNOTAVAIL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | } |
| 250 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 251 | /* called with rcu_read_lock() */ |
| 252 | static struct inet6_dev *ip6_mc_find_dev_rcu(struct net *net, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 253 | const struct in6_addr *group, |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 254 | int ifindex) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | { |
| 256 | struct net_device *dev = NULL; |
| 257 | struct inet6_dev *idev = NULL; |
| 258 | |
| 259 | if (ifindex == 0) { |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 260 | struct rt6_info *rt = rt6_lookup(net, group, NULL, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | if (rt) { |
David S. Miller | d191854 | 2011-12-28 20:19:20 -0500 | [diff] [blame] | 263 | dev = rt->dst.dev; |
Amerigo Wang | 94e187c | 2012-10-29 00:13:19 +0000 | [diff] [blame] | 264 | ip6_rt_put(rt); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 265 | } |
| 266 | } else |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 267 | dev = dev_get_by_index_rcu(net, ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | |
| 269 | if (!dev) |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 270 | return NULL; |
| 271 | idev = __in6_dev_get(dev); |
Ilpo Järvinen | 448eb71 | 2008-12-14 23:15:21 -0800 | [diff] [blame] | 272 | if (!idev) |
Joe Perches | 8a22c99 | 2010-11-14 17:05:00 +0000 | [diff] [blame] | 273 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | read_lock_bh(&idev->lock); |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 275 | if (idev->dead) { |
| 276 | read_unlock_bh(&idev->lock); |
| 277 | return NULL; |
| 278 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 | return idev; |
| 280 | } |
| 281 | |
| 282 | void ipv6_sock_mc_close(struct sock *sk) |
| 283 | { |
| 284 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 285 | struct ipv6_mc_socklist *mc_lst; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 286 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | |
Eric Dumazet | 0e1efe9 | 2012-12-05 09:18:10 +0000 | [diff] [blame] | 288 | if (!rcu_access_pointer(np->ipv6_mc_list)) |
| 289 | return; |
| 290 | |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 291 | spin_lock(&ipv6_sk_mc_lock); |
| 292 | while ((mc_lst = rcu_dereference_protected(np->ipv6_mc_list, |
| 293 | lockdep_is_held(&ipv6_sk_mc_lock))) != NULL) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | struct net_device *dev; |
| 295 | |
| 296 | np->ipv6_mc_list = mc_lst->next; |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 297 | spin_unlock(&ipv6_sk_mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 299 | rcu_read_lock(); |
| 300 | dev = dev_get_by_index_rcu(net, mc_lst->ifindex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | if (dev) { |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 302 | struct inet6_dev *idev = __in6_dev_get(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 304 | (void) ip6_mc_leave_src(sk, mc_lst, idev); |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 305 | if (idev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | __ipv6_dev_mc_dec(idev, &mc_lst->addr); |
David L Stevens | acd6e00 | 2006-08-17 16:27:39 -0700 | [diff] [blame] | 307 | } else |
| 308 | (void) ip6_mc_leave_src(sk, mc_lst, NULL); |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 309 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 311 | atomic_sub(sizeof(*mc_lst), &sk->sk_omem_alloc); |
Lai Jiangshan | e3cbf28 | 2011-03-18 12:00:50 +0800 | [diff] [blame] | 312 | kfree_rcu(mc_lst, rcu); |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 313 | |
| 314 | spin_lock(&ipv6_sk_mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | } |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 316 | spin_unlock(&ipv6_sk_mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | int ip6_mc_source(int add, int omode, struct sock *sk, |
| 320 | struct group_source_req *pgsr) |
| 321 | { |
| 322 | struct in6_addr *source, *group; |
| 323 | struct ipv6_mc_socklist *pmc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | struct inet6_dev *idev; |
| 325 | struct ipv6_pinfo *inet6 = inet6_sk(sk); |
| 326 | struct ip6_sf_socklist *psl; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 327 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | int i, j, rv; |
David L Stevens | c9e3e8b | 2005-06-21 13:58:25 -0700 | [diff] [blame] | 329 | int leavegroup = 0; |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 330 | int pmclocked = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | int err; |
| 332 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | source = &((struct sockaddr_in6 *)&pgsr->gsr_source)->sin6_addr; |
| 334 | group = &((struct sockaddr_in6 *)&pgsr->gsr_group)->sin6_addr; |
| 335 | |
| 336 | if (!ipv6_addr_is_multicast(group)) |
| 337 | return -EINVAL; |
| 338 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 339 | rcu_read_lock(); |
| 340 | idev = ip6_mc_find_dev_rcu(net, group, pgsr->gsr_interface); |
| 341 | if (!idev) { |
| 342 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | return -ENODEV; |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 344 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
| 346 | err = -EADDRNOTAVAIL; |
| 347 | |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 348 | for_each_pmc_rcu(inet6, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | if (pgsr->gsr_interface && pmc->ifindex != pgsr->gsr_interface) |
| 350 | continue; |
| 351 | if (ipv6_addr_equal(&pmc->addr, group)) |
| 352 | break; |
| 353 | } |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 354 | if (!pmc) { /* must have a prior join */ |
| 355 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 356 | goto done; |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 357 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | /* if a source filter was set, must be the same mode as before */ |
| 359 | if (pmc->sflist) { |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 360 | if (pmc->sfmode != omode) { |
| 361 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | goto done; |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 363 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | } else if (pmc->sfmode != omode) { |
| 365 | /* allow mode switches for empty-set filters */ |
| 366 | ip6_mc_add_src(idev, group, omode, 0, NULL, 0); |
| 367 | ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0); |
| 368 | pmc->sfmode = omode; |
| 369 | } |
| 370 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 371 | write_lock(&pmc->sflock); |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 372 | pmclocked = 1; |
| 373 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 374 | psl = pmc->sflist; |
| 375 | if (!add) { |
| 376 | if (!psl) |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 377 | goto done; /* err = -EADDRNOTAVAIL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 378 | rv = !0; |
| 379 | for (i=0; i<psl->sl_count; i++) { |
YOSHIFUJI Hideaki / 吉藤英明 | 07c2fec | 2013-01-29 12:48:23 +0000 | [diff] [blame] | 380 | rv = !ipv6_addr_equal(&psl->sl_addr[i], source); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 381 | if (rv == 0) |
| 382 | break; |
| 383 | } |
| 384 | if (rv) /* source not found */ |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 385 | goto done; /* err = -EADDRNOTAVAIL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
David L Stevens | c9e3e8b | 2005-06-21 13:58:25 -0700 | [diff] [blame] | 387 | /* special case - (INCLUDE, empty) == LEAVE_GROUP */ |
| 388 | if (psl->sl_count == 1 && omode == MCAST_INCLUDE) { |
| 389 | leavegroup = 1; |
| 390 | goto done; |
| 391 | } |
| 392 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | /* update the interface filter */ |
| 394 | ip6_mc_del_src(idev, group, omode, 1, source, 1); |
| 395 | |
| 396 | for (j=i+1; j<psl->sl_count; j++) |
| 397 | psl->sl_addr[j-1] = psl->sl_addr[j]; |
| 398 | psl->sl_count--; |
| 399 | err = 0; |
| 400 | goto done; |
| 401 | } |
| 402 | /* else, add a new source to the filter */ |
| 403 | |
| 404 | if (psl && psl->sl_count >= sysctl_mld_max_msf) { |
| 405 | err = -ENOBUFS; |
| 406 | goto done; |
| 407 | } |
| 408 | if (!psl || psl->sl_count == psl->sl_max) { |
| 409 | struct ip6_sf_socklist *newpsl; |
| 410 | int count = IP6_SFBLOCK; |
| 411 | |
| 412 | if (psl) |
| 413 | count += psl->sl_max; |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 414 | newpsl = sock_kmalloc(sk, IP6_SFLSIZE(count), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | if (!newpsl) { |
| 416 | err = -ENOBUFS; |
| 417 | goto done; |
| 418 | } |
| 419 | newpsl->sl_max = count; |
| 420 | newpsl->sl_count = count - IP6_SFBLOCK; |
| 421 | if (psl) { |
| 422 | for (i=0; i<psl->sl_count; i++) |
| 423 | newpsl->sl_addr[i] = psl->sl_addr[i]; |
| 424 | sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max)); |
| 425 | } |
| 426 | pmc->sflist = psl = newpsl; |
| 427 | } |
| 428 | rv = 1; /* > 0 for insert logic below if sl_count is 0 */ |
| 429 | for (i=0; i<psl->sl_count; i++) { |
YOSHIFUJI Hideaki / 吉藤英明 | 07c2fec | 2013-01-29 12:48:23 +0000 | [diff] [blame] | 430 | rv = !ipv6_addr_equal(&psl->sl_addr[i], source); |
Jean Sacren | 56db1c5 | 2013-02-03 21:34:10 +0000 | [diff] [blame] | 431 | if (rv == 0) /* There is an error in the address. */ |
| 432 | goto done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | for (j=psl->sl_count-1; j>=i; j--) |
| 435 | psl->sl_addr[j+1] = psl->sl_addr[j]; |
| 436 | psl->sl_addr[i] = *source; |
| 437 | psl->sl_count++; |
| 438 | err = 0; |
| 439 | /* update the interface list */ |
| 440 | ip6_mc_add_src(idev, group, omode, 1, source, 1); |
| 441 | done: |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 442 | if (pmclocked) |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 443 | write_unlock(&pmc->sflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | read_unlock_bh(&idev->lock); |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 445 | rcu_read_unlock(); |
David L Stevens | c9e3e8b | 2005-06-21 13:58:25 -0700 | [diff] [blame] | 446 | if (leavegroup) |
| 447 | return ipv6_sock_mc_drop(sk, pgsr->gsr_interface, group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | return err; |
| 449 | } |
| 450 | |
| 451 | int ip6_mc_msfilter(struct sock *sk, struct group_filter *gsf) |
| 452 | { |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 453 | const struct in6_addr *group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | struct ipv6_mc_socklist *pmc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | struct inet6_dev *idev; |
| 456 | struct ipv6_pinfo *inet6 = inet6_sk(sk); |
| 457 | struct ip6_sf_socklist *newpsl, *psl; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 458 | struct net *net = sock_net(sk); |
David L Stevens | 9951f03 | 2005-07-08 17:47:28 -0700 | [diff] [blame] | 459 | int leavegroup = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | int i, err; |
| 461 | |
| 462 | group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr; |
| 463 | |
| 464 | if (!ipv6_addr_is_multicast(group)) |
| 465 | return -EINVAL; |
| 466 | if (gsf->gf_fmode != MCAST_INCLUDE && |
| 467 | gsf->gf_fmode != MCAST_EXCLUDE) |
| 468 | return -EINVAL; |
| 469 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 470 | rcu_read_lock(); |
| 471 | idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 473 | if (!idev) { |
| 474 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | return -ENODEV; |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 476 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | |
David S. Miller | 9c05989 | 2005-07-08 21:44:39 -0700 | [diff] [blame] | 478 | err = 0; |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 479 | |
David L Stevens | 9951f03 | 2005-07-08 17:47:28 -0700 | [diff] [blame] | 480 | if (gsf->gf_fmode == MCAST_INCLUDE && gsf->gf_numsrc == 0) { |
| 481 | leavegroup = 1; |
| 482 | goto done; |
| 483 | } |
| 484 | |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 485 | for_each_pmc_rcu(inet6, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | if (pmc->ifindex != gsf->gf_interface) |
| 487 | continue; |
| 488 | if (ipv6_addr_equal(&pmc->addr, group)) |
| 489 | break; |
| 490 | } |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 491 | if (!pmc) { /* must have a prior join */ |
| 492 | err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | goto done; |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 494 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | if (gsf->gf_numsrc) { |
Kris Katterjohn | 8b3a700 | 2006-01-11 15:56:43 -0800 | [diff] [blame] | 496 | newpsl = sock_kmalloc(sk, IP6_SFLSIZE(gsf->gf_numsrc), |
| 497 | GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | if (!newpsl) { |
| 499 | err = -ENOBUFS; |
| 500 | goto done; |
| 501 | } |
| 502 | newpsl->sl_max = newpsl->sl_count = gsf->gf_numsrc; |
| 503 | for (i=0; i<newpsl->sl_count; ++i) { |
| 504 | struct sockaddr_in6 *psin6; |
| 505 | |
| 506 | psin6 = (struct sockaddr_in6 *)&gsf->gf_slist[i]; |
| 507 | newpsl->sl_addr[i] = psin6->sin6_addr; |
| 508 | } |
| 509 | err = ip6_mc_add_src(idev, group, gsf->gf_fmode, |
| 510 | newpsl->sl_count, newpsl->sl_addr, 0); |
| 511 | if (err) { |
| 512 | sock_kfree_s(sk, newpsl, IP6_SFLSIZE(newpsl->sl_max)); |
| 513 | goto done; |
| 514 | } |
Yan Zheng | 8713dbf | 2005-10-28 08:02:08 +0800 | [diff] [blame] | 515 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | newpsl = NULL; |
Yan Zheng | 8713dbf | 2005-10-28 08:02:08 +0800 | [diff] [blame] | 517 | (void) ip6_mc_add_src(idev, group, gsf->gf_fmode, 0, NULL, 0); |
| 518 | } |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 519 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 520 | write_lock(&pmc->sflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 521 | psl = pmc->sflist; |
| 522 | if (psl) { |
| 523 | (void) ip6_mc_del_src(idev, group, pmc->sfmode, |
| 524 | psl->sl_count, psl->sl_addr, 0); |
| 525 | sock_kfree_s(sk, psl, IP6_SFLSIZE(psl->sl_max)); |
| 526 | } else |
| 527 | (void) ip6_mc_del_src(idev, group, pmc->sfmode, 0, NULL, 0); |
| 528 | pmc->sflist = newpsl; |
| 529 | pmc->sfmode = gsf->gf_fmode; |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 530 | write_unlock(&pmc->sflock); |
David L Stevens | 917f2f1 | 2005-07-08 17:45:16 -0700 | [diff] [blame] | 531 | err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 532 | done: |
| 533 | read_unlock_bh(&idev->lock); |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 534 | rcu_read_unlock(); |
David L Stevens | 9951f03 | 2005-07-08 17:47:28 -0700 | [diff] [blame] | 535 | if (leavegroup) |
| 536 | err = ipv6_sock_mc_drop(sk, gsf->gf_interface, group); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | return err; |
| 538 | } |
| 539 | |
| 540 | int ip6_mc_msfget(struct sock *sk, struct group_filter *gsf, |
| 541 | struct group_filter __user *optval, int __user *optlen) |
| 542 | { |
| 543 | int err, i, count, copycount; |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 544 | const struct in6_addr *group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | struct ipv6_mc_socklist *pmc; |
| 546 | struct inet6_dev *idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | struct ipv6_pinfo *inet6 = inet6_sk(sk); |
| 548 | struct ip6_sf_socklist *psl; |
YOSHIFUJI Hideaki | 3b1e0a6 | 2008-03-26 02:26:21 +0900 | [diff] [blame] | 549 | struct net *net = sock_net(sk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | |
| 551 | group = &((struct sockaddr_in6 *)&gsf->gf_group)->sin6_addr; |
| 552 | |
| 553 | if (!ipv6_addr_is_multicast(group)) |
| 554 | return -EINVAL; |
| 555 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 556 | rcu_read_lock(); |
| 557 | idev = ip6_mc_find_dev_rcu(net, group, gsf->gf_interface); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 558 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 559 | if (!idev) { |
| 560 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | return -ENODEV; |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 562 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 563 | |
| 564 | err = -EADDRNOTAVAIL; |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 565 | /* |
| 566 | * changes to the ipv6_mc_list require the socket lock and |
| 567 | * a read lock on ip6_sk_mc_lock. We have the socket lock, |
| 568 | * so reading the list is safe. |
| 569 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 571 | for_each_pmc_rcu(inet6, pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 572 | if (pmc->ifindex != gsf->gf_interface) |
| 573 | continue; |
| 574 | if (ipv6_addr_equal(group, &pmc->addr)) |
| 575 | break; |
| 576 | } |
| 577 | if (!pmc) /* must have a prior join */ |
| 578 | goto done; |
| 579 | gsf->gf_fmode = pmc->sfmode; |
| 580 | psl = pmc->sflist; |
| 581 | count = psl ? psl->sl_count : 0; |
| 582 | read_unlock_bh(&idev->lock); |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 583 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 584 | |
| 585 | copycount = count < gsf->gf_numsrc ? count : gsf->gf_numsrc; |
| 586 | gsf->gf_numsrc = count; |
| 587 | if (put_user(GROUP_FILTER_SIZE(copycount), optlen) || |
| 588 | copy_to_user(optval, gsf, GROUP_FILTER_SIZE(0))) { |
| 589 | return -EFAULT; |
| 590 | } |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 591 | /* changes to psl require the socket lock, a read lock on |
| 592 | * on ipv6_sk_mc_lock and a write lock on pmc->sflock. We |
| 593 | * have the socket lock, so reading here is safe. |
| 594 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | for (i=0; i<copycount; i++) { |
| 596 | struct sockaddr_in6 *psin6; |
| 597 | struct sockaddr_storage ss; |
| 598 | |
| 599 | psin6 = (struct sockaddr_in6 *)&ss; |
| 600 | memset(&ss, 0, sizeof(ss)); |
| 601 | psin6->sin6_family = AF_INET6; |
| 602 | psin6->sin6_addr = psl->sl_addr[i]; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 603 | if (copy_to_user(&optval->gf_slist[i], &ss, sizeof(ss))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 604 | return -EFAULT; |
| 605 | } |
| 606 | return 0; |
| 607 | done: |
| 608 | read_unlock_bh(&idev->lock); |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 609 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 610 | return err; |
| 611 | } |
| 612 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 613 | bool inet6_mc_check(struct sock *sk, const struct in6_addr *mc_addr, |
| 614 | const struct in6_addr *src_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | { |
| 616 | struct ipv6_pinfo *np = inet6_sk(sk); |
| 617 | struct ipv6_mc_socklist *mc; |
| 618 | struct ip6_sf_socklist *psl; |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 619 | bool rv = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 620 | |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 621 | rcu_read_lock(); |
| 622 | for_each_pmc_rcu(np, mc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | if (ipv6_addr_equal(&mc->addr, mc_addr)) |
| 624 | break; |
| 625 | } |
| 626 | if (!mc) { |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 627 | rcu_read_unlock(); |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 628 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | } |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 630 | read_lock(&mc->sflock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | psl = mc->sflist; |
| 632 | if (!psl) { |
| 633 | rv = mc->sfmode == MCAST_EXCLUDE; |
| 634 | } else { |
| 635 | int i; |
| 636 | |
| 637 | for (i=0; i<psl->sl_count; i++) { |
| 638 | if (ipv6_addr_equal(&psl->sl_addr[i], src_addr)) |
| 639 | break; |
| 640 | } |
| 641 | if (mc->sfmode == MCAST_INCLUDE && i >= psl->sl_count) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 642 | rv = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | if (mc->sfmode == MCAST_EXCLUDE && i < psl->sl_count) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 644 | rv = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 645 | } |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 646 | read_unlock(&mc->sflock); |
Eric Dumazet | 456b61b | 2010-11-23 13:12:15 +0000 | [diff] [blame] | 647 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
| 649 | return rv; |
| 650 | } |
| 651 | |
| 652 | static void ma_put(struct ifmcaddr6 *mc) |
| 653 | { |
| 654 | if (atomic_dec_and_test(&mc->mca_refcnt)) { |
| 655 | in6_dev_put(mc->idev); |
| 656 | kfree(mc); |
| 657 | } |
| 658 | } |
| 659 | |
| 660 | static void igmp6_group_added(struct ifmcaddr6 *mc) |
| 661 | { |
| 662 | struct net_device *dev = mc->idev->dev; |
| 663 | char buf[MAX_ADDR_LEN]; |
| 664 | |
YOSHIFUJI Hideaki / 吉藤英明 | ec16ef2 | 2013-02-09 04:29:58 +0000 | [diff] [blame] | 665 | if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < |
| 666 | IPV6_ADDR_SCOPE_LINKLOCAL) |
| 667 | return; |
| 668 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | spin_lock_bh(&mc->mca_lock); |
| 670 | if (!(mc->mca_flags&MAF_LOADED)) { |
| 671 | mc->mca_flags |= MAF_LOADED; |
| 672 | if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0) |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 673 | dev_mc_add(dev, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
| 675 | spin_unlock_bh(&mc->mca_lock); |
| 676 | |
| 677 | if (!(dev->flags & IFF_UP) || (mc->mca_flags & MAF_NOREPORT)) |
| 678 | return; |
| 679 | |
| 680 | if (MLD_V1_SEEN(mc->idev)) { |
| 681 | igmp6_join_group(mc); |
| 682 | return; |
| 683 | } |
| 684 | /* else v2 */ |
| 685 | |
| 686 | mc->mca_crcount = mc->idev->mc_qrv; |
| 687 | mld_ifc_event(mc->idev); |
| 688 | } |
| 689 | |
| 690 | static void igmp6_group_dropped(struct ifmcaddr6 *mc) |
| 691 | { |
| 692 | struct net_device *dev = mc->idev->dev; |
| 693 | char buf[MAX_ADDR_LEN]; |
| 694 | |
YOSHIFUJI Hideaki / 吉藤英明 | ec16ef2 | 2013-02-09 04:29:58 +0000 | [diff] [blame] | 695 | if (IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < |
| 696 | IPV6_ADDR_SCOPE_LINKLOCAL) |
| 697 | return; |
| 698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | spin_lock_bh(&mc->mca_lock); |
| 700 | if (mc->mca_flags&MAF_LOADED) { |
| 701 | mc->mca_flags &= ~MAF_LOADED; |
| 702 | if (ndisc_mc_map(&mc->mca_addr, buf, dev, 0) == 0) |
Jiri Pirko | 22bedad3 | 2010-04-01 21:22:57 +0000 | [diff] [blame] | 703 | dev_mc_del(dev, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | if (mc->mca_flags & MAF_NOREPORT) |
| 707 | goto done; |
| 708 | spin_unlock_bh(&mc->mca_lock); |
| 709 | |
| 710 | if (!mc->idev->dead) |
| 711 | igmp6_leave_group(mc); |
| 712 | |
| 713 | spin_lock_bh(&mc->mca_lock); |
| 714 | if (del_timer(&mc->mca_timer)) |
| 715 | atomic_dec(&mc->mca_refcnt); |
| 716 | done: |
| 717 | ip6_mc_clear_src(mc); |
| 718 | spin_unlock_bh(&mc->mca_lock); |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * deleted ifmcaddr6 manipulation |
| 723 | */ |
| 724 | static void mld_add_delrec(struct inet6_dev *idev, struct ifmcaddr6 *im) |
| 725 | { |
| 726 | struct ifmcaddr6 *pmc; |
| 727 | |
| 728 | /* this is an "ifmcaddr6" for convenience; only the fields below |
| 729 | * are actually used. In particular, the refcnt and users are not |
| 730 | * used for management of the delete list. Using the same structure |
| 731 | * for deleted items allows change reports to use common code with |
| 732 | * non-deleted or query-response MCA's. |
| 733 | */ |
Ingo Oeser | 0c600ed | 2006-03-20 23:01:32 -0800 | [diff] [blame] | 734 | pmc = kzalloc(sizeof(*pmc), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 735 | if (!pmc) |
| 736 | return; |
Ingo Oeser | 0c600ed | 2006-03-20 23:01:32 -0800 | [diff] [blame] | 737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | spin_lock_bh(&im->mca_lock); |
| 739 | spin_lock_init(&pmc->mca_lock); |
| 740 | pmc->idev = im->idev; |
| 741 | in6_dev_hold(idev); |
| 742 | pmc->mca_addr = im->mca_addr; |
| 743 | pmc->mca_crcount = idev->mc_qrv; |
| 744 | pmc->mca_sfmode = im->mca_sfmode; |
| 745 | if (pmc->mca_sfmode == MCAST_INCLUDE) { |
| 746 | struct ip6_sf_list *psf; |
| 747 | |
| 748 | pmc->mca_tomb = im->mca_tomb; |
| 749 | pmc->mca_sources = im->mca_sources; |
| 750 | im->mca_tomb = im->mca_sources = NULL; |
| 751 | for (psf=pmc->mca_sources; psf; psf=psf->sf_next) |
| 752 | psf->sf_crcount = pmc->mca_crcount; |
| 753 | } |
| 754 | spin_unlock_bh(&im->mca_lock); |
| 755 | |
Stephen Hemminger | 6457d26 | 2010-02-17 18:48:44 -0800 | [diff] [blame] | 756 | spin_lock_bh(&idev->mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 757 | pmc->next = idev->mc_tomb; |
| 758 | idev->mc_tomb = pmc; |
Stephen Hemminger | 6457d26 | 2010-02-17 18:48:44 -0800 | [diff] [blame] | 759 | spin_unlock_bh(&idev->mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
| 761 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 762 | static void mld_del_delrec(struct inet6_dev *idev, const struct in6_addr *pmca) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 763 | { |
| 764 | struct ifmcaddr6 *pmc, *pmc_prev; |
| 765 | struct ip6_sf_list *psf, *psf_next; |
| 766 | |
Stephen Hemminger | 6457d26 | 2010-02-17 18:48:44 -0800 | [diff] [blame] | 767 | spin_lock_bh(&idev->mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | pmc_prev = NULL; |
| 769 | for (pmc=idev->mc_tomb; pmc; pmc=pmc->next) { |
| 770 | if (ipv6_addr_equal(&pmc->mca_addr, pmca)) |
| 771 | break; |
| 772 | pmc_prev = pmc; |
| 773 | } |
| 774 | if (pmc) { |
| 775 | if (pmc_prev) |
| 776 | pmc_prev->next = pmc->next; |
| 777 | else |
| 778 | idev->mc_tomb = pmc->next; |
| 779 | } |
Stephen Hemminger | 6457d26 | 2010-02-17 18:48:44 -0800 | [diff] [blame] | 780 | spin_unlock_bh(&idev->mc_lock); |
| 781 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | if (pmc) { |
| 783 | for (psf=pmc->mca_tomb; psf; psf=psf_next) { |
| 784 | psf_next = psf->sf_next; |
| 785 | kfree(psf); |
| 786 | } |
| 787 | in6_dev_put(pmc->idev); |
| 788 | kfree(pmc); |
| 789 | } |
| 790 | } |
| 791 | |
| 792 | static void mld_clear_delrec(struct inet6_dev *idev) |
| 793 | { |
| 794 | struct ifmcaddr6 *pmc, *nextpmc; |
| 795 | |
Stephen Hemminger | 6457d26 | 2010-02-17 18:48:44 -0800 | [diff] [blame] | 796 | spin_lock_bh(&idev->mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | pmc = idev->mc_tomb; |
| 798 | idev->mc_tomb = NULL; |
Stephen Hemminger | 6457d26 | 2010-02-17 18:48:44 -0800 | [diff] [blame] | 799 | spin_unlock_bh(&idev->mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | |
| 801 | for (; pmc; pmc = nextpmc) { |
| 802 | nextpmc = pmc->next; |
| 803 | ip6_mc_clear_src(pmc); |
| 804 | in6_dev_put(pmc->idev); |
| 805 | kfree(pmc); |
| 806 | } |
| 807 | |
| 808 | /* clear dead sources, too */ |
| 809 | read_lock_bh(&idev->lock); |
| 810 | for (pmc=idev->mc_list; pmc; pmc=pmc->next) { |
| 811 | struct ip6_sf_list *psf, *psf_next; |
| 812 | |
| 813 | spin_lock_bh(&pmc->mca_lock); |
| 814 | psf = pmc->mca_tomb; |
| 815 | pmc->mca_tomb = NULL; |
| 816 | spin_unlock_bh(&pmc->mca_lock); |
| 817 | for (; psf; psf=psf_next) { |
| 818 | psf_next = psf->sf_next; |
| 819 | kfree(psf); |
| 820 | } |
| 821 | } |
| 822 | read_unlock_bh(&idev->lock); |
| 823 | } |
| 824 | |
| 825 | |
| 826 | /* |
| 827 | * device multicast group inc (add if not found) |
| 828 | */ |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 829 | int ipv6_dev_mc_inc(struct net_device *dev, const struct in6_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | { |
| 831 | struct ifmcaddr6 *mc; |
| 832 | struct inet6_dev *idev; |
| 833 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 834 | /* we need to take a reference on idev */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 835 | idev = in6_dev_get(dev); |
| 836 | |
| 837 | if (idev == NULL) |
| 838 | return -EINVAL; |
| 839 | |
| 840 | write_lock_bh(&idev->lock); |
| 841 | if (idev->dead) { |
| 842 | write_unlock_bh(&idev->lock); |
| 843 | in6_dev_put(idev); |
| 844 | return -ENODEV; |
| 845 | } |
| 846 | |
| 847 | for (mc = idev->mc_list; mc; mc = mc->next) { |
| 848 | if (ipv6_addr_equal(&mc->mca_addr, addr)) { |
| 849 | mc->mca_users++; |
| 850 | write_unlock_bh(&idev->lock); |
| 851 | ip6_mc_add_src(idev, &mc->mca_addr, MCAST_EXCLUDE, 0, |
| 852 | NULL, 0); |
| 853 | in6_dev_put(idev); |
| 854 | return 0; |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | /* |
| 859 | * not found: create a new one. |
| 860 | */ |
| 861 | |
Ingo Oeser | 0c600ed | 2006-03-20 23:01:32 -0800 | [diff] [blame] | 862 | mc = kzalloc(sizeof(struct ifmcaddr6), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
| 864 | if (mc == NULL) { |
| 865 | write_unlock_bh(&idev->lock); |
| 866 | in6_dev_put(idev); |
| 867 | return -ENOMEM; |
| 868 | } |
| 869 | |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 870 | setup_timer(&mc->mca_timer, igmp6_timer_handler, (unsigned long)mc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 871 | |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 872 | mc->mca_addr = *addr; |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 873 | mc->idev = idev; /* (reference taken) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | mc->mca_users = 1; |
| 875 | /* mca_stamp should be updated upon changes */ |
| 876 | mc->mca_cstamp = mc->mca_tstamp = jiffies; |
| 877 | atomic_set(&mc->mca_refcnt, 2); |
| 878 | spin_lock_init(&mc->mca_lock); |
| 879 | |
| 880 | /* initial mode is (EX, empty) */ |
| 881 | mc->mca_sfmode = MCAST_EXCLUDE; |
| 882 | mc->mca_sfcount[MCAST_EXCLUDE] = 1; |
| 883 | |
| 884 | if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) || |
| 885 | IPV6_ADDR_MC_SCOPE(&mc->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) |
| 886 | mc->mca_flags |= MAF_NOREPORT; |
| 887 | |
| 888 | mc->next = idev->mc_list; |
| 889 | idev->mc_list = mc; |
| 890 | write_unlock_bh(&idev->lock); |
| 891 | |
| 892 | mld_del_delrec(idev, &mc->mca_addr); |
| 893 | igmp6_group_added(mc); |
| 894 | ma_put(mc); |
| 895 | return 0; |
| 896 | } |
| 897 | |
| 898 | /* |
| 899 | * device multicast group del |
| 900 | */ |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 901 | int __ipv6_dev_mc_dec(struct inet6_dev *idev, const struct in6_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 902 | { |
| 903 | struct ifmcaddr6 *ma, **map; |
| 904 | |
| 905 | write_lock_bh(&idev->lock); |
| 906 | for (map = &idev->mc_list; (ma=*map) != NULL; map = &ma->next) { |
| 907 | if (ipv6_addr_equal(&ma->mca_addr, addr)) { |
| 908 | if (--ma->mca_users == 0) { |
| 909 | *map = ma->next; |
| 910 | write_unlock_bh(&idev->lock); |
| 911 | |
| 912 | igmp6_group_dropped(ma); |
| 913 | |
| 914 | ma_put(ma); |
| 915 | return 0; |
| 916 | } |
| 917 | write_unlock_bh(&idev->lock); |
| 918 | return 0; |
| 919 | } |
| 920 | } |
| 921 | write_unlock_bh(&idev->lock); |
| 922 | |
| 923 | return -ENOENT; |
| 924 | } |
| 925 | |
YOSHIFUJI Hideaki | 9acd9f3 | 2008-04-10 15:42:10 +0900 | [diff] [blame] | 926 | int ipv6_dev_mc_dec(struct net_device *dev, const struct in6_addr *addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 927 | { |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 928 | struct inet6_dev *idev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | int err; |
| 930 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 931 | rcu_read_lock(); |
| 932 | |
| 933 | idev = __in6_dev_get(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 934 | if (!idev) |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 935 | err = -ENODEV; |
| 936 | else |
| 937 | err = __ipv6_dev_mc_dec(idev, addr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 938 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 939 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | return err; |
| 941 | } |
| 942 | |
| 943 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | * check if the interface/address pair is valid |
| 945 | */ |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 946 | bool ipv6_chk_mcast_addr(struct net_device *dev, const struct in6_addr *group, |
| 947 | const struct in6_addr *src_addr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 948 | { |
| 949 | struct inet6_dev *idev; |
| 950 | struct ifmcaddr6 *mc; |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 951 | bool rv = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 952 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 953 | rcu_read_lock(); |
| 954 | idev = __in6_dev_get(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 955 | if (idev) { |
| 956 | read_lock_bh(&idev->lock); |
| 957 | for (mc = idev->mc_list; mc; mc=mc->next) { |
| 958 | if (ipv6_addr_equal(&mc->mca_addr, group)) |
| 959 | break; |
| 960 | } |
| 961 | if (mc) { |
| 962 | if (src_addr && !ipv6_addr_any(src_addr)) { |
| 963 | struct ip6_sf_list *psf; |
| 964 | |
| 965 | spin_lock_bh(&mc->mca_lock); |
| 966 | for (psf=mc->mca_sources;psf;psf=psf->sf_next) { |
| 967 | if (ipv6_addr_equal(&psf->sf_addr, src_addr)) |
| 968 | break; |
| 969 | } |
| 970 | if (psf) |
| 971 | rv = psf->sf_count[MCAST_INCLUDE] || |
| 972 | psf->sf_count[MCAST_EXCLUDE] != |
| 973 | mc->mca_sfcount[MCAST_EXCLUDE]; |
| 974 | else |
| 975 | rv = mc->mca_sfcount[MCAST_EXCLUDE] !=0; |
| 976 | spin_unlock_bh(&mc->mca_lock); |
| 977 | } else |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 978 | rv = true; /* don't filter unspecified source */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 979 | } |
| 980 | read_unlock_bh(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | } |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 982 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | return rv; |
| 984 | } |
| 985 | |
| 986 | static void mld_gq_start_timer(struct inet6_dev *idev) |
| 987 | { |
| 988 | int tv = net_random() % idev->mc_maxdelay; |
| 989 | |
| 990 | idev->mc_gq_running = 1; |
| 991 | if (!mod_timer(&idev->mc_gq_timer, jiffies+tv+2)) |
| 992 | in6_dev_hold(idev); |
| 993 | } |
| 994 | |
| 995 | static void mld_ifc_start_timer(struct inet6_dev *idev, int delay) |
| 996 | { |
| 997 | int tv = net_random() % delay; |
| 998 | |
| 999 | if (!mod_timer(&idev->mc_ifc_timer, jiffies+tv+2)) |
| 1000 | in6_dev_hold(idev); |
| 1001 | } |
| 1002 | |
Hannes Frederic Sowa | b173ee4 | 2013-06-27 00:07:01 +0200 | [diff] [blame] | 1003 | static void mld_dad_start_timer(struct inet6_dev *idev, int delay) |
| 1004 | { |
| 1005 | int tv = net_random() % delay; |
| 1006 | |
| 1007 | if (!mod_timer(&idev->mc_dad_timer, jiffies+tv+2)) |
| 1008 | in6_dev_hold(idev); |
| 1009 | } |
| 1010 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1011 | /* |
| 1012 | * IGMP handling (alias multicast ICMPv6 messages) |
| 1013 | */ |
| 1014 | |
| 1015 | static void igmp6_group_queried(struct ifmcaddr6 *ma, unsigned long resptime) |
| 1016 | { |
| 1017 | unsigned long delay = resptime; |
| 1018 | |
| 1019 | /* Do not start timer for these addresses */ |
| 1020 | if (ipv6_addr_is_ll_all_nodes(&ma->mca_addr) || |
| 1021 | IPV6_ADDR_MC_SCOPE(&ma->mca_addr) < IPV6_ADDR_SCOPE_LINKLOCAL) |
| 1022 | return; |
| 1023 | |
| 1024 | if (del_timer(&ma->mca_timer)) { |
| 1025 | atomic_dec(&ma->mca_refcnt); |
| 1026 | delay = ma->mca_timer.expires - jiffies; |
| 1027 | } |
| 1028 | |
| 1029 | if (delay >= resptime) { |
| 1030 | if (resptime) |
| 1031 | delay = net_random() % resptime; |
| 1032 | else |
| 1033 | delay = 1; |
| 1034 | } |
| 1035 | ma->mca_timer.expires = jiffies + delay; |
| 1036 | if (!mod_timer(&ma->mca_timer, jiffies + delay)) |
| 1037 | atomic_inc(&ma->mca_refcnt); |
| 1038 | ma->mca_flags |= MAF_TIMER_RUNNING; |
| 1039 | } |
| 1040 | |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1041 | /* mark EXCLUDE-mode sources */ |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1042 | static bool mld_xmarksources(struct ifmcaddr6 *pmc, int nsrcs, |
| 1043 | const struct in6_addr *srcs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1044 | { |
| 1045 | struct ip6_sf_list *psf; |
| 1046 | int i, scount; |
| 1047 | |
| 1048 | scount = 0; |
| 1049 | for (psf=pmc->mca_sources; psf; psf=psf->sf_next) { |
| 1050 | if (scount == nsrcs) |
| 1051 | break; |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1052 | for (i=0; i<nsrcs; i++) { |
| 1053 | /* skip inactive filters */ |
Yan, Zheng | e05c4ad3 | 2011-08-23 22:54:37 +0000 | [diff] [blame] | 1054 | if (psf->sf_count[MCAST_INCLUDE] || |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1055 | pmc->mca_sfcount[MCAST_EXCLUDE] != |
| 1056 | psf->sf_count[MCAST_EXCLUDE]) |
RongQing.Li | ce713ee | 2012-04-05 17:36:29 +0800 | [diff] [blame] | 1057 | break; |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1058 | if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) { |
| 1059 | scount++; |
| 1060 | break; |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | pmc->mca_flags &= ~MAF_GSQUERY; |
| 1065 | if (scount == nsrcs) /* all sources excluded */ |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1066 | return false; |
| 1067 | return true; |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1068 | } |
| 1069 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1070 | static bool mld_marksources(struct ifmcaddr6 *pmc, int nsrcs, |
| 1071 | const struct in6_addr *srcs) |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1072 | { |
| 1073 | struct ip6_sf_list *psf; |
| 1074 | int i, scount; |
| 1075 | |
| 1076 | if (pmc->mca_sfmode == MCAST_EXCLUDE) |
| 1077 | return mld_xmarksources(pmc, nsrcs, srcs); |
| 1078 | |
| 1079 | /* mark INCLUDE-mode sources */ |
| 1080 | |
| 1081 | scount = 0; |
| 1082 | for (psf=pmc->mca_sources; psf; psf=psf->sf_next) { |
| 1083 | if (scount == nsrcs) |
| 1084 | break; |
| 1085 | for (i=0; i<nsrcs; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1086 | if (ipv6_addr_equal(&srcs[i], &psf->sf_addr)) { |
| 1087 | psf->sf_gsresp = 1; |
| 1088 | scount++; |
| 1089 | break; |
| 1090 | } |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1091 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1092 | } |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1093 | if (!scount) { |
| 1094 | pmc->mca_flags &= ~MAF_GSQUERY; |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1095 | return false; |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1096 | } |
| 1097 | pmc->mca_flags |= MAF_GSQUERY; |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1098 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1099 | } |
| 1100 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1101 | /* called with rcu_read_lock() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | int igmp6_event_query(struct sk_buff *skb) |
| 1103 | { |
Yan Zheng | 97300b5 | 2005-10-31 20:09:45 +0800 | [diff] [blame] | 1104 | struct mld2_query *mlh2 = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1105 | struct ifmcaddr6 *ma; |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1106 | const struct in6_addr *group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1107 | unsigned long max_delay; |
| 1108 | struct inet6_dev *idev; |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1109 | struct mld_msg *mld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | int group_type; |
| 1111 | int mark = 0; |
| 1112 | int len; |
| 1113 | |
| 1114 | if (!pskb_may_pull(skb, sizeof(struct in6_addr))) |
| 1115 | return -EINVAL; |
| 1116 | |
| 1117 | /* compute payload length excluding extension headers */ |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1118 | len = ntohs(ipv6_hdr(skb)->payload_len) + sizeof(struct ipv6hdr); |
Arnaldo Carvalho de Melo | cfe1fc7 | 2007-03-16 17:26:39 -0300 | [diff] [blame] | 1119 | len -= skb_network_header_len(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | |
| 1121 | /* Drop queries with not link local source */ |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1122 | if (!(ipv6_addr_type(&ipv6_hdr(skb)->saddr) & IPV6_ADDR_LINKLOCAL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1123 | return -EINVAL; |
| 1124 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1125 | idev = __in6_dev_get(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1126 | |
| 1127 | if (idev == NULL) |
| 1128 | return 0; |
| 1129 | |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1130 | mld = (struct mld_msg *)icmp6_hdr(skb); |
| 1131 | group = &mld->mld_mca; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1132 | group_type = ipv6_addr_type(group); |
| 1133 | |
| 1134 | if (group_type != IPV6_ADDR_ANY && |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1135 | !(group_type&IPV6_ADDR_MULTICAST)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1136 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1137 | |
| 1138 | if (len == 24) { |
| 1139 | int switchback; |
| 1140 | /* MLDv1 router present */ |
| 1141 | |
| 1142 | /* Translate milliseconds to jiffies */ |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1143 | max_delay = (ntohs(mld->mld_maxdelay)*HZ)/1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1144 | |
| 1145 | switchback = (idev->mc_qrv + 1) * max_delay; |
| 1146 | idev->mc_v1_seen = jiffies + switchback; |
| 1147 | |
| 1148 | /* cancel the interface change timer */ |
| 1149 | idev->mc_ifc_count = 0; |
| 1150 | if (del_timer(&idev->mc_ifc_timer)) |
| 1151 | __in6_dev_put(idev); |
| 1152 | /* clear deleted report items */ |
| 1153 | mld_clear_delrec(idev); |
| 1154 | } else if (len >= 28) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1155 | int srcs_offset = sizeof(struct mld2_query) - |
Yan Zheng | 97300b5 | 2005-10-31 20:09:45 +0800 | [diff] [blame] | 1156 | sizeof(struct icmp6hdr); |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1157 | if (!pskb_may_pull(skb, srcs_offset)) |
Yan Zheng | 97300b5 | 2005-10-31 20:09:45 +0800 | [diff] [blame] | 1158 | return -EINVAL; |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1159 | |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 1160 | mlh2 = (struct mld2_query *)skb_transport_header(skb); |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1161 | max_delay = (MLDV2_MRC(ntohs(mlh2->mld2q_mrc))*HZ)/1000; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1162 | if (!max_delay) |
| 1163 | max_delay = 1; |
| 1164 | idev->mc_maxdelay = max_delay; |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1165 | if (mlh2->mld2q_qrv) |
| 1166 | idev->mc_qrv = mlh2->mld2q_qrv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1167 | if (group_type == IPV6_ADDR_ANY) { /* general query */ |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1168 | if (mlh2->mld2q_nsrcs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1169 | return -EINVAL; /* no sources allowed */ |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1171 | mld_gq_start_timer(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | return 0; |
| 1173 | } |
| 1174 | /* mark sources to include, if group & source-specific */ |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1175 | if (mlh2->mld2q_nsrcs != 0) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1176 | if (!pskb_may_pull(skb, srcs_offset + |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1177 | ntohs(mlh2->mld2q_nsrcs) * sizeof(struct in6_addr))) |
Yan Zheng | 97300b5 | 2005-10-31 20:09:45 +0800 | [diff] [blame] | 1178 | return -EINVAL; |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1179 | |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 1180 | mlh2 = (struct mld2_query *)skb_transport_header(skb); |
Yan Zheng | 97300b5 | 2005-10-31 20:09:45 +0800 | [diff] [blame] | 1181 | mark = 1; |
| 1182 | } |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1183 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1184 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | |
| 1186 | read_lock_bh(&idev->lock); |
| 1187 | if (group_type == IPV6_ADDR_ANY) { |
| 1188 | for (ma = idev->mc_list; ma; ma=ma->next) { |
| 1189 | spin_lock_bh(&ma->mca_lock); |
| 1190 | igmp6_group_queried(ma, max_delay); |
| 1191 | spin_unlock_bh(&ma->mca_lock); |
| 1192 | } |
| 1193 | } else { |
| 1194 | for (ma = idev->mc_list; ma; ma=ma->next) { |
David L Stevens | 7add2a4 | 2006-01-24 13:06:39 -0800 | [diff] [blame] | 1195 | if (!ipv6_addr_equal(group, &ma->mca_addr)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | continue; |
| 1197 | spin_lock_bh(&ma->mca_lock); |
| 1198 | if (ma->mca_flags & MAF_TIMER_RUNNING) { |
| 1199 | /* gsquery <- gsquery && mark */ |
| 1200 | if (!mark) |
| 1201 | ma->mca_flags &= ~MAF_GSQUERY; |
| 1202 | } else { |
| 1203 | /* gsquery <- mark */ |
| 1204 | if (mark) |
| 1205 | ma->mca_flags |= MAF_GSQUERY; |
| 1206 | else |
| 1207 | ma->mca_flags &= ~MAF_GSQUERY; |
| 1208 | } |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1209 | if (!(ma->mca_flags & MAF_GSQUERY) || |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1210 | mld_marksources(ma, ntohs(mlh2->mld2q_nsrcs), mlh2->mld2q_srcs)) |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1211 | igmp6_group_queried(ma, max_delay); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | spin_unlock_bh(&ma->mca_lock); |
David L Stevens | 7add2a4 | 2006-01-24 13:06:39 -0800 | [diff] [blame] | 1213 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1214 | } |
| 1215 | } |
| 1216 | read_unlock_bh(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1221 | /* called with rcu_read_lock() */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1222 | int igmp6_event_report(struct sk_buff *skb) |
| 1223 | { |
| 1224 | struct ifmcaddr6 *ma; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | struct inet6_dev *idev; |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1226 | struct mld_msg *mld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | int addr_type; |
| 1228 | |
| 1229 | /* Our own report looped back. Ignore it. */ |
| 1230 | if (skb->pkt_type == PACKET_LOOPBACK) |
| 1231 | return 0; |
| 1232 | |
David Stevens | 24c6927 | 2005-12-02 20:32:59 -0800 | [diff] [blame] | 1233 | /* send our report if the MC router may not have heard this report */ |
| 1234 | if (skb->pkt_type != PACKET_MULTICAST && |
| 1235 | skb->pkt_type != PACKET_BROADCAST) |
| 1236 | return 0; |
| 1237 | |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1238 | if (!pskb_may_pull(skb, sizeof(*mld) - sizeof(struct icmp6hdr))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1239 | return -EINVAL; |
| 1240 | |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1241 | mld = (struct mld_msg *)icmp6_hdr(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1242 | |
| 1243 | /* Drop reports with not link local source */ |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1244 | addr_type = ipv6_addr_type(&ipv6_hdr(skb)->saddr); |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1245 | if (addr_type != IPV6_ADDR_ANY && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1246 | !(addr_type&IPV6_ADDR_LINKLOCAL)) |
| 1247 | return -EINVAL; |
| 1248 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1249 | idev = __in6_dev_get(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | if (idev == NULL) |
| 1251 | return -ENODEV; |
| 1252 | |
| 1253 | /* |
| 1254 | * Cancel the timer for this group |
| 1255 | */ |
| 1256 | |
| 1257 | read_lock_bh(&idev->lock); |
| 1258 | for (ma = idev->mc_list; ma; ma=ma->next) { |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1259 | if (ipv6_addr_equal(&ma->mca_addr, &mld->mld_mca)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1260 | spin_lock(&ma->mca_lock); |
| 1261 | if (del_timer(&ma->mca_timer)) |
| 1262 | atomic_dec(&ma->mca_refcnt); |
| 1263 | ma->mca_flags &= ~(MAF_LAST_REPORTER|MAF_TIMER_RUNNING); |
| 1264 | spin_unlock(&ma->mca_lock); |
| 1265 | break; |
| 1266 | } |
| 1267 | } |
| 1268 | read_unlock_bh(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1269 | return 0; |
| 1270 | } |
| 1271 | |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1272 | static bool is_in(struct ifmcaddr6 *pmc, struct ip6_sf_list *psf, int type, |
| 1273 | int gdeleted, int sdeleted) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1274 | { |
| 1275 | switch (type) { |
| 1276 | case MLD2_MODE_IS_INCLUDE: |
| 1277 | case MLD2_MODE_IS_EXCLUDE: |
| 1278 | if (gdeleted || sdeleted) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1279 | return false; |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1280 | if (!((pmc->mca_flags & MAF_GSQUERY) && !psf->sf_gsresp)) { |
| 1281 | if (pmc->mca_sfmode == MCAST_INCLUDE) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1282 | return true; |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1283 | /* don't include if this source is excluded |
| 1284 | * in all filters |
| 1285 | */ |
| 1286 | if (psf->sf_count[MCAST_INCLUDE]) |
David L Stevens | 7add2a4 | 2006-01-24 13:06:39 -0800 | [diff] [blame] | 1287 | return type == MLD2_MODE_IS_INCLUDE; |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1288 | return pmc->mca_sfcount[MCAST_EXCLUDE] == |
| 1289 | psf->sf_count[MCAST_EXCLUDE]; |
| 1290 | } |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1291 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1292 | case MLD2_CHANGE_TO_INCLUDE: |
| 1293 | if (gdeleted || sdeleted) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1294 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1295 | return psf->sf_count[MCAST_INCLUDE] != 0; |
| 1296 | case MLD2_CHANGE_TO_EXCLUDE: |
| 1297 | if (gdeleted || sdeleted) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1298 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1299 | if (pmc->mca_sfcount[MCAST_EXCLUDE] == 0 || |
| 1300 | psf->sf_count[MCAST_INCLUDE]) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1301 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | return pmc->mca_sfcount[MCAST_EXCLUDE] == |
| 1303 | psf->sf_count[MCAST_EXCLUDE]; |
| 1304 | case MLD2_ALLOW_NEW_SOURCES: |
| 1305 | if (gdeleted || !psf->sf_crcount) |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1306 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | return (pmc->mca_sfmode == MCAST_INCLUDE) ^ sdeleted; |
| 1308 | case MLD2_BLOCK_OLD_SOURCES: |
| 1309 | if (pmc->mca_sfmode == MCAST_INCLUDE) |
| 1310 | return gdeleted || (psf->sf_crcount && sdeleted); |
| 1311 | return psf->sf_crcount && !gdeleted && !sdeleted; |
| 1312 | } |
Eric Dumazet | a50feda | 2012-05-18 18:57:34 +0000 | [diff] [blame] | 1313 | return false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | static int |
| 1317 | mld_scount(struct ifmcaddr6 *pmc, int type, int gdeleted, int sdeleted) |
| 1318 | { |
| 1319 | struct ip6_sf_list *psf; |
| 1320 | int scount = 0; |
| 1321 | |
| 1322 | for (psf=pmc->mca_sources; psf; psf=psf->sf_next) { |
| 1323 | if (!is_in(pmc, psf, type, gdeleted, sdeleted)) |
| 1324 | continue; |
| 1325 | scount++; |
| 1326 | } |
| 1327 | return scount; |
| 1328 | } |
| 1329 | |
YOSHIFUJI Hideaki / 吉藤英明 | 2576f17 | 2013-01-21 06:48:19 +0000 | [diff] [blame] | 1330 | static void ip6_mc_hdr(struct sock *sk, struct sk_buff *skb, |
| 1331 | struct net_device *dev, |
| 1332 | const struct in6_addr *saddr, |
| 1333 | const struct in6_addr *daddr, |
| 1334 | int proto, int len) |
| 1335 | { |
| 1336 | struct ipv6hdr *hdr; |
| 1337 | |
| 1338 | skb->protocol = htons(ETH_P_IPV6); |
| 1339 | skb->dev = dev; |
| 1340 | |
| 1341 | skb_reset_network_header(skb); |
| 1342 | skb_put(skb, sizeof(struct ipv6hdr)); |
| 1343 | hdr = ipv6_hdr(skb); |
| 1344 | |
| 1345 | ip6_flow_hdr(hdr, 0, 0); |
| 1346 | |
| 1347 | hdr->payload_len = htons(len); |
| 1348 | hdr->nexthdr = proto; |
| 1349 | hdr->hop_limit = inet6_sk(sk)->hop_limit; |
| 1350 | |
| 1351 | hdr->saddr = *saddr; |
| 1352 | hdr->daddr = *daddr; |
| 1353 | } |
| 1354 | |
Amerigo Wang | 8965779 | 2013-06-29 21:30:49 +0800 | [diff] [blame] | 1355 | static struct sk_buff *mld_newpack(struct inet6_dev *idev, int size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | { |
Amerigo Wang | 8965779 | 2013-06-29 21:30:49 +0800 | [diff] [blame] | 1357 | struct net_device *dev = idev->dev; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1358 | struct net *net = dev_net(dev); |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 1359 | struct sock *sk = net->ipv6.igmp_sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | struct sk_buff *skb; |
| 1361 | struct mld2_report *pmr; |
| 1362 | struct in6_addr addr_buf; |
YOSHIFUJI Hideaki | d7aabf2 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 1363 | const struct in6_addr *saddr; |
Herbert Xu | a7ae199 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 1364 | int hlen = LL_RESERVED_SPACE(dev); |
| 1365 | int tlen = dev->needed_tailroom; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | int err; |
| 1367 | u8 ra[8] = { IPPROTO_ICMPV6, 0, |
| 1368 | IPV6_TLV_ROUTERALERT, 2, 0, 0, |
| 1369 | IPV6_TLV_PADN, 0 }; |
| 1370 | |
| 1371 | /* we assume size > sizeof(ra) here */ |
Herbert Xu | a7ae199 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 1372 | size += hlen + tlen; |
Eric Dumazet | 72e09ad | 2010-06-05 03:03:30 -0700 | [diff] [blame] | 1373 | /* limit our allocations to order-0 page */ |
| 1374 | size = min_t(int, size, SKB_MAX_ORDER(0, 0)); |
| 1375 | skb = sock_alloc_send_skb(sk, size, 1, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1376 | |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 1377 | if (!skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | return NULL; |
| 1379 | |
Hannes Frederic Sowa | 9d4a031 | 2013-07-26 17:05:16 +0200 | [diff] [blame^] | 1380 | skb->priority = TC_PRIO_CONTROL; |
Herbert Xu | a7ae199 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 1381 | skb_reserve(skb, hlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1382 | |
Amerigo Wang | 8965779 | 2013-06-29 21:30:49 +0800 | [diff] [blame] | 1383 | if (__ipv6_get_lladdr(idev, &addr_buf, IFA_F_TENTATIVE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1384 | /* <draft-ietf-magma-mld-source-05.txt>: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1385 | * use unspecified address as the source address |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1386 | * when a valid link-local address is not available. |
| 1387 | */ |
YOSHIFUJI Hideaki | d7aabf2 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 1388 | saddr = &in6addr_any; |
| 1389 | } else |
| 1390 | saddr = &addr_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | |
YOSHIFUJI Hideaki / 吉藤英明 | 2576f17 | 2013-01-21 06:48:19 +0000 | [diff] [blame] | 1392 | ip6_mc_hdr(sk, skb, dev, saddr, &mld2_all_mcr, NEXTHDR_HOP, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1393 | |
| 1394 | memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra)); |
| 1395 | |
Arnaldo Carvalho de Melo | 27a884d | 2007-04-19 20:29:13 -0700 | [diff] [blame] | 1396 | skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->data); |
Arnaldo Carvalho de Melo | d10ba34 | 2007-03-14 21:05:37 -0300 | [diff] [blame] | 1397 | skb_put(skb, sizeof(*pmr)); |
| 1398 | pmr = (struct mld2_report *)skb_transport_header(skb); |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1399 | pmr->mld2r_type = ICMPV6_MLD2_REPORT; |
| 1400 | pmr->mld2r_resv1 = 0; |
| 1401 | pmr->mld2r_cksum = 0; |
| 1402 | pmr->mld2r_resv2 = 0; |
| 1403 | pmr->mld2r_ngrec = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | return skb; |
| 1405 | } |
| 1406 | |
| 1407 | static void mld_sendpack(struct sk_buff *skb) |
| 1408 | { |
Arnaldo Carvalho de Melo | 0660e03 | 2007-04-25 17:54:47 -0700 | [diff] [blame] | 1409 | struct ipv6hdr *pip6 = ipv6_hdr(skb); |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 1410 | struct mld2_report *pmr = |
| 1411 | (struct mld2_report *)skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | int payload_len, mldlen; |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1413 | struct inet6_dev *idev; |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1414 | struct net *net = dev_net(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | int err; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1416 | struct flowi6 fl6; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1417 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1419 | rcu_read_lock(); |
| 1420 | idev = __in6_dev_get(skb->dev); |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 1421 | IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUT, skb->len); |
| 1422 | |
Simon Horman | 29a3cad | 2013-05-28 20:34:26 +0000 | [diff] [blame] | 1423 | payload_len = (skb_tail_pointer(skb) - skb_network_header(skb)) - |
| 1424 | sizeof(*pip6); |
| 1425 | mldlen = skb_tail_pointer(skb) - skb_transport_header(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | pip6->payload_len = htons(payload_len); |
| 1427 | |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1428 | pmr->mld2r_cksum = csum_ipv6_magic(&pip6->saddr, &pip6->daddr, mldlen, |
| 1429 | IPPROTO_ICMPV6, |
| 1430 | csum_partial(skb_transport_header(skb), |
| 1431 | mldlen, 0)); |
YOSHIFUJI Hideaki | 4192717 | 2007-12-06 17:40:56 -0800 | [diff] [blame] | 1432 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1433 | icmpv6_flow_init(net->ipv6.igmp_sk, &fl6, ICMPV6_MLD2_REPORT, |
YOSHIFUJI Hideaki | 4192717 | 2007-12-06 17:40:56 -0800 | [diff] [blame] | 1434 | &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr, |
| 1435 | skb->dev->ifindex); |
YOSHIFUJI Hideaki / 吉藤英明 | 12fd84f | 2013-01-18 02:00:24 +0000 | [diff] [blame] | 1436 | dst = icmp6_dst_alloc(skb->dev, &fl6); |
YOSHIFUJI Hideaki | 4192717 | 2007-12-06 17:40:56 -0800 | [diff] [blame] | 1437 | |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 1438 | err = 0; |
| 1439 | if (IS_ERR(dst)) { |
| 1440 | err = PTR_ERR(dst); |
| 1441 | dst = NULL; |
| 1442 | } |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1443 | skb_dst_set(skb, dst); |
YOSHIFUJI Hideaki | 4192717 | 2007-12-06 17:40:56 -0800 | [diff] [blame] | 1444 | if (err) |
| 1445 | goto err_out; |
| 1446 | |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 1447 | payload_len = skb->len; |
| 1448 | |
Jan Engelhardt | b2e0b38 | 2010-03-23 04:09:07 +0100 | [diff] [blame] | 1449 | err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev, |
YOSHIFUJI Hideaki | 4192717 | 2007-12-06 17:40:56 -0800 | [diff] [blame] | 1450 | dst_output); |
| 1451 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | if (!err) { |
Denis V. Lunev | 5a57d4c | 2008-10-08 10:34:14 -0700 | [diff] [blame] | 1453 | ICMP6MSGOUT_INC_STATS_BH(net, idev, ICMPV6_MLD2_REPORT); |
Denis V. Lunev | e41b536 | 2008-10-08 10:33:26 -0700 | [diff] [blame] | 1454 | ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_OUTMSGS); |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 1455 | IP6_UPD_PO_STATS_BH(net, idev, IPSTATS_MIB_OUTMCAST, payload_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1456 | } else |
Denis V. Lunev | 483a47d | 2008-10-08 11:09:27 -0700 | [diff] [blame] | 1457 | IP6_INC_STATS_BH(net, idev, IPSTATS_MIB_OUTDISCARDS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1459 | rcu_read_unlock(); |
YOSHIFUJI Hideaki | 4192717 | 2007-12-06 17:40:56 -0800 | [diff] [blame] | 1460 | return; |
| 1461 | |
| 1462 | err_out: |
| 1463 | kfree_skb(skb); |
| 1464 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1465 | } |
| 1466 | |
| 1467 | static int grec_size(struct ifmcaddr6 *pmc, int type, int gdel, int sdel) |
| 1468 | { |
Yan Zheng | fab10fe | 2005-10-05 12:08:13 -0700 | [diff] [blame] | 1469 | return sizeof(struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | static struct sk_buff *add_grhead(struct sk_buff *skb, struct ifmcaddr6 *pmc, |
| 1473 | int type, struct mld2_grec **ppgr) |
| 1474 | { |
| 1475 | struct net_device *dev = pmc->idev->dev; |
| 1476 | struct mld2_report *pmr; |
| 1477 | struct mld2_grec *pgr; |
| 1478 | |
| 1479 | if (!skb) |
Amerigo Wang | 8965779 | 2013-06-29 21:30:49 +0800 | [diff] [blame] | 1480 | skb = mld_newpack(pmc->idev, dev->mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1481 | if (!skb) |
| 1482 | return NULL; |
| 1483 | pgr = (struct mld2_grec *)skb_put(skb, sizeof(struct mld2_grec)); |
| 1484 | pgr->grec_type = type; |
| 1485 | pgr->grec_auxwords = 0; |
| 1486 | pgr->grec_nsrcs = 0; |
| 1487 | pgr->grec_mca = pmc->mca_addr; /* structure copy */ |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 1488 | pmr = (struct mld2_report *)skb_transport_header(skb); |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1489 | pmr->mld2r_ngrec = htons(ntohs(pmr->mld2r_ngrec)+1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1490 | *ppgr = pgr; |
| 1491 | return skb; |
| 1492 | } |
| 1493 | |
| 1494 | #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \ |
| 1495 | skb_tailroom(skb)) : 0) |
| 1496 | |
| 1497 | static struct sk_buff *add_grec(struct sk_buff *skb, struct ifmcaddr6 *pmc, |
| 1498 | int type, int gdeleted, int sdeleted) |
| 1499 | { |
Amerigo Wang | 8965779 | 2013-06-29 21:30:49 +0800 | [diff] [blame] | 1500 | struct inet6_dev *idev = pmc->idev; |
| 1501 | struct net_device *dev = idev->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | struct mld2_report *pmr; |
| 1503 | struct mld2_grec *pgr = NULL; |
| 1504 | struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list; |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1505 | int scount, stotal, first, isquery, truncate; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1506 | |
| 1507 | if (pmc->mca_flags & MAF_NOREPORT) |
| 1508 | return skb; |
| 1509 | |
| 1510 | isquery = type == MLD2_MODE_IS_INCLUDE || |
| 1511 | type == MLD2_MODE_IS_EXCLUDE; |
| 1512 | truncate = type == MLD2_MODE_IS_EXCLUDE || |
| 1513 | type == MLD2_CHANGE_TO_EXCLUDE; |
| 1514 | |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1515 | stotal = scount = 0; |
| 1516 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | psf_list = sdeleted ? &pmc->mca_tomb : &pmc->mca_sources; |
| 1518 | |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1519 | if (!*psf_list) |
| 1520 | goto empty_source; |
| 1521 | |
Arnaldo Carvalho de Melo | 9c70220 | 2007-04-25 18:04:18 -0700 | [diff] [blame] | 1522 | pmr = skb ? (struct mld2_report *)skb_transport_header(skb) : NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1523 | |
| 1524 | /* EX and TO_EX get a fresh packet, if needed */ |
| 1525 | if (truncate) { |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1526 | if (pmr && pmr->mld2r_ngrec && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1527 | AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) { |
| 1528 | if (skb) |
| 1529 | mld_sendpack(skb); |
Amerigo Wang | 8965779 | 2013-06-29 21:30:49 +0800 | [diff] [blame] | 1530 | skb = mld_newpack(idev, dev->mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1531 | } |
| 1532 | } |
| 1533 | first = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1534 | psf_prev = NULL; |
| 1535 | for (psf=*psf_list; psf; psf=psf_next) { |
| 1536 | struct in6_addr *psrc; |
| 1537 | |
| 1538 | psf_next = psf->sf_next; |
| 1539 | |
| 1540 | if (!is_in(pmc, psf, type, gdeleted, sdeleted)) { |
| 1541 | psf_prev = psf; |
| 1542 | continue; |
| 1543 | } |
| 1544 | |
| 1545 | /* clear marks on query responses */ |
| 1546 | if (isquery) |
| 1547 | psf->sf_gsresp = 0; |
| 1548 | |
| 1549 | if (AVAILABLE(skb) < sizeof(*psrc) + |
| 1550 | first*sizeof(struct mld2_grec)) { |
| 1551 | if (truncate && !first) |
| 1552 | break; /* truncate these */ |
| 1553 | if (pgr) |
| 1554 | pgr->grec_nsrcs = htons(scount); |
| 1555 | if (skb) |
| 1556 | mld_sendpack(skb); |
Amerigo Wang | 8965779 | 2013-06-29 21:30:49 +0800 | [diff] [blame] | 1557 | skb = mld_newpack(idev, dev->mtu); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | first = 1; |
| 1559 | scount = 0; |
| 1560 | } |
| 1561 | if (first) { |
| 1562 | skb = add_grhead(skb, pmc, type, &pgr); |
| 1563 | first = 0; |
| 1564 | } |
Alexey Dobriyan | cc63f70 | 2007-02-06 14:35:25 -0800 | [diff] [blame] | 1565 | if (!skb) |
| 1566 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | psrc = (struct in6_addr *)skb_put(skb, sizeof(*psrc)); |
| 1568 | *psrc = psf->sf_addr; |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1569 | scount++; stotal++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | if ((type == MLD2_ALLOW_NEW_SOURCES || |
| 1571 | type == MLD2_BLOCK_OLD_SOURCES) && psf->sf_crcount) { |
| 1572 | psf->sf_crcount--; |
| 1573 | if ((sdeleted || gdeleted) && psf->sf_crcount == 0) { |
| 1574 | if (psf_prev) |
| 1575 | psf_prev->sf_next = psf->sf_next; |
| 1576 | else |
| 1577 | *psf_list = psf->sf_next; |
| 1578 | kfree(psf); |
| 1579 | continue; |
| 1580 | } |
| 1581 | } |
| 1582 | psf_prev = psf; |
| 1583 | } |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1584 | |
| 1585 | empty_source: |
| 1586 | if (!stotal) { |
| 1587 | if (type == MLD2_ALLOW_NEW_SOURCES || |
| 1588 | type == MLD2_BLOCK_OLD_SOURCES) |
| 1589 | return skb; |
| 1590 | if (pmc->mca_crcount || isquery) { |
| 1591 | /* make sure we have room for group header */ |
| 1592 | if (skb && AVAILABLE(skb) < sizeof(struct mld2_grec)) { |
| 1593 | mld_sendpack(skb); |
| 1594 | skb = NULL; /* add_grhead will get a new one */ |
| 1595 | } |
| 1596 | skb = add_grhead(skb, pmc, type, &pgr); |
| 1597 | } |
| 1598 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1599 | if (pgr) |
| 1600 | pgr->grec_nsrcs = htons(scount); |
| 1601 | |
| 1602 | if (isquery) |
| 1603 | pmc->mca_flags &= ~MAF_GSQUERY; /* clear query state */ |
| 1604 | return skb; |
| 1605 | } |
| 1606 | |
| 1607 | static void mld_send_report(struct inet6_dev *idev, struct ifmcaddr6 *pmc) |
| 1608 | { |
| 1609 | struct sk_buff *skb = NULL; |
| 1610 | int type; |
| 1611 | |
Amerigo Wang | 8965779 | 2013-06-29 21:30:49 +0800 | [diff] [blame] | 1612 | read_lock_bh(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1613 | if (!pmc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | for (pmc=idev->mc_list; pmc; pmc=pmc->next) { |
| 1615 | if (pmc->mca_flags & MAF_NOREPORT) |
| 1616 | continue; |
| 1617 | spin_lock_bh(&pmc->mca_lock); |
| 1618 | if (pmc->mca_sfcount[MCAST_EXCLUDE]) |
| 1619 | type = MLD2_MODE_IS_EXCLUDE; |
| 1620 | else |
| 1621 | type = MLD2_MODE_IS_INCLUDE; |
| 1622 | skb = add_grec(skb, pmc, type, 0, 0); |
| 1623 | spin_unlock_bh(&pmc->mca_lock); |
| 1624 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1625 | } else { |
| 1626 | spin_lock_bh(&pmc->mca_lock); |
| 1627 | if (pmc->mca_sfcount[MCAST_EXCLUDE]) |
| 1628 | type = MLD2_MODE_IS_EXCLUDE; |
| 1629 | else |
| 1630 | type = MLD2_MODE_IS_INCLUDE; |
| 1631 | skb = add_grec(skb, pmc, type, 0, 0); |
| 1632 | spin_unlock_bh(&pmc->mca_lock); |
| 1633 | } |
Amerigo Wang | 8965779 | 2013-06-29 21:30:49 +0800 | [diff] [blame] | 1634 | read_unlock_bh(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1635 | if (skb) |
| 1636 | mld_sendpack(skb); |
| 1637 | } |
| 1638 | |
| 1639 | /* |
| 1640 | * remove zero-count source records from a source filter list |
| 1641 | */ |
| 1642 | static void mld_clear_zeros(struct ip6_sf_list **ppsf) |
| 1643 | { |
| 1644 | struct ip6_sf_list *psf_prev, *psf_next, *psf; |
| 1645 | |
| 1646 | psf_prev = NULL; |
| 1647 | for (psf=*ppsf; psf; psf = psf_next) { |
| 1648 | psf_next = psf->sf_next; |
| 1649 | if (psf->sf_crcount == 0) { |
| 1650 | if (psf_prev) |
| 1651 | psf_prev->sf_next = psf->sf_next; |
| 1652 | else |
| 1653 | *ppsf = psf->sf_next; |
| 1654 | kfree(psf); |
| 1655 | } else |
| 1656 | psf_prev = psf; |
| 1657 | } |
| 1658 | } |
| 1659 | |
| 1660 | static void mld_send_cr(struct inet6_dev *idev) |
| 1661 | { |
| 1662 | struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next; |
| 1663 | struct sk_buff *skb = NULL; |
| 1664 | int type, dtype; |
| 1665 | |
| 1666 | read_lock_bh(&idev->lock); |
Stephen Hemminger | 6457d26 | 2010-02-17 18:48:44 -0800 | [diff] [blame] | 1667 | spin_lock(&idev->mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | |
| 1669 | /* deleted MCA's */ |
| 1670 | pmc_prev = NULL; |
| 1671 | for (pmc=idev->mc_tomb; pmc; pmc=pmc_next) { |
| 1672 | pmc_next = pmc->next; |
| 1673 | if (pmc->mca_sfmode == MCAST_INCLUDE) { |
| 1674 | type = MLD2_BLOCK_OLD_SOURCES; |
| 1675 | dtype = MLD2_BLOCK_OLD_SOURCES; |
| 1676 | skb = add_grec(skb, pmc, type, 1, 0); |
| 1677 | skb = add_grec(skb, pmc, dtype, 1, 1); |
| 1678 | } |
| 1679 | if (pmc->mca_crcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1680 | if (pmc->mca_sfmode == MCAST_EXCLUDE) { |
| 1681 | type = MLD2_CHANGE_TO_INCLUDE; |
| 1682 | skb = add_grec(skb, pmc, type, 1, 0); |
| 1683 | } |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1684 | pmc->mca_crcount--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1685 | if (pmc->mca_crcount == 0) { |
| 1686 | mld_clear_zeros(&pmc->mca_tomb); |
| 1687 | mld_clear_zeros(&pmc->mca_sources); |
| 1688 | } |
| 1689 | } |
| 1690 | if (pmc->mca_crcount == 0 && !pmc->mca_tomb && |
| 1691 | !pmc->mca_sources) { |
| 1692 | if (pmc_prev) |
| 1693 | pmc_prev->next = pmc_next; |
| 1694 | else |
| 1695 | idev->mc_tomb = pmc_next; |
| 1696 | in6_dev_put(pmc->idev); |
| 1697 | kfree(pmc); |
| 1698 | } else |
| 1699 | pmc_prev = pmc; |
| 1700 | } |
Stephen Hemminger | 6457d26 | 2010-02-17 18:48:44 -0800 | [diff] [blame] | 1701 | spin_unlock(&idev->mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | |
| 1703 | /* change recs */ |
| 1704 | for (pmc=idev->mc_list; pmc; pmc=pmc->next) { |
| 1705 | spin_lock_bh(&pmc->mca_lock); |
| 1706 | if (pmc->mca_sfcount[MCAST_EXCLUDE]) { |
| 1707 | type = MLD2_BLOCK_OLD_SOURCES; |
| 1708 | dtype = MLD2_ALLOW_NEW_SOURCES; |
| 1709 | } else { |
| 1710 | type = MLD2_ALLOW_NEW_SOURCES; |
| 1711 | dtype = MLD2_BLOCK_OLD_SOURCES; |
| 1712 | } |
| 1713 | skb = add_grec(skb, pmc, type, 0, 0); |
| 1714 | skb = add_grec(skb, pmc, dtype, 0, 1); /* deleted sources */ |
| 1715 | |
| 1716 | /* filter mode changes */ |
| 1717 | if (pmc->mca_crcount) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1718 | if (pmc->mca_sfmode == MCAST_EXCLUDE) |
| 1719 | type = MLD2_CHANGE_TO_EXCLUDE; |
| 1720 | else |
| 1721 | type = MLD2_CHANGE_TO_INCLUDE; |
| 1722 | skb = add_grec(skb, pmc, type, 0, 0); |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 1723 | pmc->mca_crcount--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | } |
| 1725 | spin_unlock_bh(&pmc->mca_lock); |
| 1726 | } |
| 1727 | read_unlock_bh(&idev->lock); |
| 1728 | if (!skb) |
| 1729 | return; |
| 1730 | (void) mld_sendpack(skb); |
| 1731 | } |
| 1732 | |
| 1733 | static void igmp6_send(struct in6_addr *addr, struct net_device *dev, int type) |
| 1734 | { |
YOSHIFUJI Hideaki | c346dca | 2008-03-25 21:47:49 +0900 | [diff] [blame] | 1735 | struct net *net = dev_net(dev); |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 1736 | struct sock *sk = net->ipv6.igmp_sk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1737 | struct inet6_dev *idev; |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1738 | struct sk_buff *skb; |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1739 | struct mld_msg *hdr; |
YOSHIFUJI Hideaki | d7aabf2 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 1740 | const struct in6_addr *snd_addr, *saddr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1741 | struct in6_addr addr_buf; |
Herbert Xu | a7ae199 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 1742 | int hlen = LL_RESERVED_SPACE(dev); |
| 1743 | int tlen = dev->needed_tailroom; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | int err, len, payload_len, full_len; |
| 1745 | u8 ra[8] = { IPPROTO_ICMPV6, 0, |
| 1746 | IPV6_TLV_ROUTERALERT, 2, 0, 0, |
| 1747 | IPV6_TLV_PADN, 0 }; |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1748 | struct flowi6 fl6; |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1749 | struct dst_entry *dst; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | |
YOSHIFUJI Hideaki | f3ee401 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 1751 | if (type == ICMPV6_MGM_REDUCTION) |
| 1752 | snd_addr = &in6addr_linklocal_allrouters; |
| 1753 | else |
| 1754 | snd_addr = addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1755 | |
| 1756 | len = sizeof(struct icmp6hdr) + sizeof(struct in6_addr); |
| 1757 | payload_len = len + sizeof(ra); |
| 1758 | full_len = sizeof(struct ipv6hdr) + payload_len; |
| 1759 | |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 1760 | rcu_read_lock(); |
| 1761 | IP6_UPD_PO_STATS(net, __in6_dev_get(dev), |
| 1762 | IPSTATS_MIB_OUT, full_len); |
| 1763 | rcu_read_unlock(); |
| 1764 | |
Herbert Xu | a7ae199 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 1765 | skb = sock_alloc_send_skb(sk, hlen + tlen + full_len, 1, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | |
| 1767 | if (skb == NULL) { |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 1768 | rcu_read_lock(); |
Denis V. Lunev | 3bd653c | 2008-10-08 10:54:51 -0700 | [diff] [blame] | 1769 | IP6_INC_STATS(net, __in6_dev_get(dev), |
YOSHIFUJI Hideaki | a11d206 | 2006-11-04 20:11:37 +0900 | [diff] [blame] | 1770 | IPSTATS_MIB_OUTDISCARDS); |
| 1771 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | return; |
| 1773 | } |
Hannes Frederic Sowa | 9d4a031 | 2013-07-26 17:05:16 +0200 | [diff] [blame^] | 1774 | skb->priority = TC_PRIO_CONTROL; |
Herbert Xu | a7ae199 | 2011-11-18 02:20:04 +0000 | [diff] [blame] | 1775 | skb_reserve(skb, hlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1776 | |
Neil Horman | 95c385b | 2007-04-25 17:08:10 -0700 | [diff] [blame] | 1777 | if (ipv6_get_lladdr(dev, &addr_buf, IFA_F_TENTATIVE)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | /* <draft-ietf-magma-mld-source-05.txt>: |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 1779 | * use unspecified address as the source address |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | * when a valid link-local address is not available. |
| 1781 | */ |
YOSHIFUJI Hideaki | d7aabf2 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 1782 | saddr = &in6addr_any; |
| 1783 | } else |
| 1784 | saddr = &addr_buf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1785 | |
YOSHIFUJI Hideaki / 吉藤英明 | 2576f17 | 2013-01-21 06:48:19 +0000 | [diff] [blame] | 1786 | ip6_mc_hdr(sk, skb, dev, saddr, snd_addr, NEXTHDR_HOP, payload_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | |
| 1788 | memcpy(skb_put(skb, sizeof(ra)), ra, sizeof(ra)); |
| 1789 | |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1790 | hdr = (struct mld_msg *) skb_put(skb, sizeof(struct mld_msg)); |
| 1791 | memset(hdr, 0, sizeof(struct mld_msg)); |
| 1792 | hdr->mld_type = type; |
Alexey Dobriyan | 4e3fd7a | 2011-11-21 03:39:03 +0000 | [diff] [blame] | 1793 | hdr->mld_mca = *addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1794 | |
YOSHIFUJI Hideaki | 6e7cb83 | 2010-04-18 12:42:05 +0900 | [diff] [blame] | 1795 | hdr->mld_cksum = csum_ipv6_magic(saddr, snd_addr, len, |
| 1796 | IPPROTO_ICMPV6, |
| 1797 | csum_partial(hdr, len, 0)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1798 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1799 | rcu_read_lock(); |
| 1800 | idev = __in6_dev_get(skb->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | |
David S. Miller | 4c9483b | 2011-03-12 16:22:43 -0500 | [diff] [blame] | 1802 | icmpv6_flow_init(sk, &fl6, type, |
YOSHIFUJI Hideaki | 4192717 | 2007-12-06 17:40:56 -0800 | [diff] [blame] | 1803 | &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr, |
| 1804 | skb->dev->ifindex); |
YOSHIFUJI Hideaki / 吉藤英明 | 12fd84f | 2013-01-18 02:00:24 +0000 | [diff] [blame] | 1805 | dst = icmp6_dst_alloc(skb->dev, &fl6); |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 1806 | if (IS_ERR(dst)) { |
| 1807 | err = PTR_ERR(dst); |
YOSHIFUJI Hideaki | 4192717 | 2007-12-06 17:40:56 -0800 | [diff] [blame] | 1808 | goto err_out; |
David S. Miller | 452edd5 | 2011-03-02 13:27:41 -0800 | [diff] [blame] | 1809 | } |
YOSHIFUJI Hideaki | 4192717 | 2007-12-06 17:40:56 -0800 | [diff] [blame] | 1810 | |
Eric Dumazet | adf3090 | 2009-06-02 05:19:30 +0000 | [diff] [blame] | 1811 | skb_dst_set(skb, dst); |
Jan Engelhardt | b2e0b38 | 2010-03-23 04:09:07 +0100 | [diff] [blame] | 1812 | err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL, skb->dev, |
YOSHIFUJI Hideaki | 4192717 | 2007-12-06 17:40:56 -0800 | [diff] [blame] | 1813 | dst_output); |
| 1814 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | if (!err) { |
Denis V. Lunev | 5c5d244 | 2008-10-08 10:33:50 -0700 | [diff] [blame] | 1816 | ICMP6MSGOUT_INC_STATS(net, idev, type); |
Denis V. Lunev | a862f6a | 2008-10-08 10:33:06 -0700 | [diff] [blame] | 1817 | ICMP6_INC_STATS(net, idev, ICMP6_MIB_OUTMSGS); |
Neil Horman | edf391f | 2009-04-27 02:45:02 -0700 | [diff] [blame] | 1818 | IP6_UPD_PO_STATS(net, idev, IPSTATS_MIB_OUTMCAST, full_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | } else |
Denis V. Lunev | 3bd653c | 2008-10-08 10:54:51 -0700 | [diff] [blame] | 1820 | IP6_INC_STATS(net, idev, IPSTATS_MIB_OUTDISCARDS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1821 | |
Eric Dumazet | 96b52e6 | 2010-06-07 21:05:02 +0000 | [diff] [blame] | 1822 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | return; |
YOSHIFUJI Hideaki | 4192717 | 2007-12-06 17:40:56 -0800 | [diff] [blame] | 1824 | |
| 1825 | err_out: |
| 1826 | kfree_skb(skb); |
| 1827 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | } |
| 1829 | |
Hannes Frederic Sowa | b173ee4 | 2013-06-27 00:07:01 +0200 | [diff] [blame] | 1830 | static void mld_resend_report(struct inet6_dev *idev) |
| 1831 | { |
| 1832 | if (MLD_V1_SEEN(idev)) { |
| 1833 | struct ifmcaddr6 *mcaddr; |
| 1834 | read_lock_bh(&idev->lock); |
| 1835 | for (mcaddr = idev->mc_list; mcaddr; mcaddr = mcaddr->next) { |
| 1836 | if (!(mcaddr->mca_flags & MAF_NOREPORT)) |
| 1837 | igmp6_send(&mcaddr->mca_addr, idev->dev, |
| 1838 | ICMPV6_MGM_REPORT); |
| 1839 | } |
| 1840 | read_unlock_bh(&idev->lock); |
| 1841 | } else { |
| 1842 | mld_send_report(idev, NULL); |
| 1843 | } |
| 1844 | } |
| 1845 | |
| 1846 | void ipv6_mc_dad_complete(struct inet6_dev *idev) |
| 1847 | { |
| 1848 | idev->mc_dad_count = idev->mc_qrv; |
| 1849 | if (idev->mc_dad_count) { |
| 1850 | mld_resend_report(idev); |
| 1851 | idev->mc_dad_count--; |
| 1852 | if (idev->mc_dad_count) |
| 1853 | mld_dad_start_timer(idev, idev->mc_maxdelay); |
| 1854 | } |
| 1855 | } |
| 1856 | |
| 1857 | static void mld_dad_timer_expire(unsigned long data) |
| 1858 | { |
| 1859 | struct inet6_dev *idev = (struct inet6_dev *)data; |
| 1860 | |
| 1861 | mld_resend_report(idev); |
| 1862 | if (idev->mc_dad_count) { |
| 1863 | idev->mc_dad_count--; |
| 1864 | if (idev->mc_dad_count) |
| 1865 | mld_dad_start_timer(idev, idev->mc_maxdelay); |
| 1866 | } |
| 1867 | __in6_dev_put(idev); |
| 1868 | } |
| 1869 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | static int ip6_mc_del1_src(struct ifmcaddr6 *pmc, int sfmode, |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1871 | const struct in6_addr *psfsrc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1872 | { |
| 1873 | struct ip6_sf_list *psf, *psf_prev; |
| 1874 | int rv = 0; |
| 1875 | |
| 1876 | psf_prev = NULL; |
| 1877 | for (psf=pmc->mca_sources; psf; psf=psf->sf_next) { |
| 1878 | if (ipv6_addr_equal(&psf->sf_addr, psfsrc)) |
| 1879 | break; |
| 1880 | psf_prev = psf; |
| 1881 | } |
| 1882 | if (!psf || psf->sf_count[sfmode] == 0) { |
| 1883 | /* source filter not found, or count wrong => bug */ |
| 1884 | return -ESRCH; |
| 1885 | } |
| 1886 | psf->sf_count[sfmode]--; |
| 1887 | if (!psf->sf_count[MCAST_INCLUDE] && !psf->sf_count[MCAST_EXCLUDE]) { |
| 1888 | struct inet6_dev *idev = pmc->idev; |
| 1889 | |
| 1890 | /* no more filters for this source */ |
| 1891 | if (psf_prev) |
| 1892 | psf_prev->sf_next = psf->sf_next; |
| 1893 | else |
| 1894 | pmc->mca_sources = psf->sf_next; |
| 1895 | if (psf->sf_oldin && !(pmc->mca_flags & MAF_NOREPORT) && |
| 1896 | !MLD_V1_SEEN(idev)) { |
| 1897 | psf->sf_crcount = idev->mc_qrv; |
| 1898 | psf->sf_next = pmc->mca_tomb; |
| 1899 | pmc->mca_tomb = psf; |
| 1900 | rv = 1; |
| 1901 | } else |
| 1902 | kfree(psf); |
| 1903 | } |
| 1904 | return rv; |
| 1905 | } |
| 1906 | |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 1907 | static int ip6_mc_del_src(struct inet6_dev *idev, const struct in6_addr *pmca, |
| 1908 | int sfmode, int sfcount, const struct in6_addr *psfsrc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | int delta) |
| 1910 | { |
| 1911 | struct ifmcaddr6 *pmc; |
| 1912 | int changerec = 0; |
| 1913 | int i, err; |
| 1914 | |
| 1915 | if (!idev) |
| 1916 | return -ENODEV; |
| 1917 | read_lock_bh(&idev->lock); |
| 1918 | for (pmc=idev->mc_list; pmc; pmc=pmc->next) { |
| 1919 | if (ipv6_addr_equal(pmca, &pmc->mca_addr)) |
| 1920 | break; |
| 1921 | } |
| 1922 | if (!pmc) { |
| 1923 | /* MCA not found?? bug */ |
| 1924 | read_unlock_bh(&idev->lock); |
| 1925 | return -ESRCH; |
| 1926 | } |
| 1927 | spin_lock_bh(&pmc->mca_lock); |
| 1928 | sf_markstate(pmc); |
| 1929 | if (!delta) { |
| 1930 | if (!pmc->mca_sfcount[sfmode]) { |
| 1931 | spin_unlock_bh(&pmc->mca_lock); |
| 1932 | read_unlock_bh(&idev->lock); |
| 1933 | return -EINVAL; |
| 1934 | } |
| 1935 | pmc->mca_sfcount[sfmode]--; |
| 1936 | } |
| 1937 | err = 0; |
| 1938 | for (i=0; i<sfcount; i++) { |
| 1939 | int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]); |
| 1940 | |
| 1941 | changerec |= rv > 0; |
| 1942 | if (!err && rv < 0) |
| 1943 | err = rv; |
| 1944 | } |
| 1945 | if (pmc->mca_sfmode == MCAST_EXCLUDE && |
| 1946 | pmc->mca_sfcount[MCAST_EXCLUDE] == 0 && |
| 1947 | pmc->mca_sfcount[MCAST_INCLUDE]) { |
| 1948 | struct ip6_sf_list *psf; |
| 1949 | |
| 1950 | /* filter mode change */ |
| 1951 | pmc->mca_sfmode = MCAST_INCLUDE; |
| 1952 | pmc->mca_crcount = idev->mc_qrv; |
| 1953 | idev->mc_ifc_count = pmc->mca_crcount; |
| 1954 | for (psf=pmc->mca_sources; psf; psf = psf->sf_next) |
| 1955 | psf->sf_crcount = 0; |
| 1956 | mld_ifc_event(pmc->idev); |
| 1957 | } else if (sf_setstate(pmc) || changerec) |
| 1958 | mld_ifc_event(pmc->idev); |
| 1959 | spin_unlock_bh(&pmc->mca_lock); |
| 1960 | read_unlock_bh(&idev->lock); |
| 1961 | return err; |
| 1962 | } |
| 1963 | |
| 1964 | /* |
| 1965 | * Add multicast single-source filter to the interface list |
| 1966 | */ |
| 1967 | static int ip6_mc_add1_src(struct ifmcaddr6 *pmc, int sfmode, |
Jun Zhao | 99d2f47 | 2011-11-30 06:21:05 +0000 | [diff] [blame] | 1968 | const struct in6_addr *psfsrc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | { |
| 1970 | struct ip6_sf_list *psf, *psf_prev; |
| 1971 | |
| 1972 | psf_prev = NULL; |
| 1973 | for (psf=pmc->mca_sources; psf; psf=psf->sf_next) { |
| 1974 | if (ipv6_addr_equal(&psf->sf_addr, psfsrc)) |
| 1975 | break; |
| 1976 | psf_prev = psf; |
| 1977 | } |
| 1978 | if (!psf) { |
Ingo Oeser | 0c600ed | 2006-03-20 23:01:32 -0800 | [diff] [blame] | 1979 | psf = kzalloc(sizeof(*psf), GFP_ATOMIC); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | if (!psf) |
| 1981 | return -ENOBUFS; |
Ingo Oeser | 0c600ed | 2006-03-20 23:01:32 -0800 | [diff] [blame] | 1982 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | psf->sf_addr = *psfsrc; |
| 1984 | if (psf_prev) { |
| 1985 | psf_prev->sf_next = psf; |
| 1986 | } else |
| 1987 | pmc->mca_sources = psf; |
| 1988 | } |
| 1989 | psf->sf_count[sfmode]++; |
| 1990 | return 0; |
| 1991 | } |
| 1992 | |
| 1993 | static void sf_markstate(struct ifmcaddr6 *pmc) |
| 1994 | { |
| 1995 | struct ip6_sf_list *psf; |
| 1996 | int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE]; |
| 1997 | |
| 1998 | for (psf=pmc->mca_sources; psf; psf=psf->sf_next) |
| 1999 | if (pmc->mca_sfcount[MCAST_EXCLUDE]) { |
| 2000 | psf->sf_oldin = mca_xcount == |
| 2001 | psf->sf_count[MCAST_EXCLUDE] && |
| 2002 | !psf->sf_count[MCAST_INCLUDE]; |
| 2003 | } else |
| 2004 | psf->sf_oldin = psf->sf_count[MCAST_INCLUDE] != 0; |
| 2005 | } |
| 2006 | |
| 2007 | static int sf_setstate(struct ifmcaddr6 *pmc) |
| 2008 | { |
David L Stevens | 7add2a4 | 2006-01-24 13:06:39 -0800 | [diff] [blame] | 2009 | struct ip6_sf_list *psf, *dpsf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | int mca_xcount = pmc->mca_sfcount[MCAST_EXCLUDE]; |
| 2011 | int qrv = pmc->idev->mc_qrv; |
| 2012 | int new_in, rv; |
| 2013 | |
| 2014 | rv = 0; |
| 2015 | for (psf=pmc->mca_sources; psf; psf=psf->sf_next) { |
| 2016 | if (pmc->mca_sfcount[MCAST_EXCLUDE]) { |
| 2017 | new_in = mca_xcount == psf->sf_count[MCAST_EXCLUDE] && |
| 2018 | !psf->sf_count[MCAST_INCLUDE]; |
| 2019 | } else |
| 2020 | new_in = psf->sf_count[MCAST_INCLUDE] != 0; |
David L Stevens | 7add2a4 | 2006-01-24 13:06:39 -0800 | [diff] [blame] | 2021 | if (new_in) { |
| 2022 | if (!psf->sf_oldin) { |
Al Viro | e80e28b | 2006-02-03 20:10:03 -0500 | [diff] [blame] | 2023 | struct ip6_sf_list *prev = NULL; |
David L Stevens | 7add2a4 | 2006-01-24 13:06:39 -0800 | [diff] [blame] | 2024 | |
| 2025 | for (dpsf=pmc->mca_tomb; dpsf; |
| 2026 | dpsf=dpsf->sf_next) { |
| 2027 | if (ipv6_addr_equal(&dpsf->sf_addr, |
| 2028 | &psf->sf_addr)) |
| 2029 | break; |
| 2030 | prev = dpsf; |
| 2031 | } |
| 2032 | if (dpsf) { |
| 2033 | if (prev) |
| 2034 | prev->sf_next = dpsf->sf_next; |
| 2035 | else |
| 2036 | pmc->mca_tomb = dpsf->sf_next; |
| 2037 | kfree(dpsf); |
| 2038 | } |
| 2039 | psf->sf_crcount = qrv; |
| 2040 | rv++; |
| 2041 | } |
| 2042 | } else if (psf->sf_oldin) { |
| 2043 | psf->sf_crcount = 0; |
| 2044 | /* |
| 2045 | * add or update "delete" records if an active filter |
| 2046 | * is now inactive |
| 2047 | */ |
| 2048 | for (dpsf=pmc->mca_tomb; dpsf; dpsf=dpsf->sf_next) |
| 2049 | if (ipv6_addr_equal(&dpsf->sf_addr, |
| 2050 | &psf->sf_addr)) |
| 2051 | break; |
| 2052 | if (!dpsf) { |
Joe Perches | 5d55354 | 2010-05-31 17:23:22 +0000 | [diff] [blame] | 2053 | dpsf = kmalloc(sizeof(*dpsf), GFP_ATOMIC); |
David L Stevens | 7add2a4 | 2006-01-24 13:06:39 -0800 | [diff] [blame] | 2054 | if (!dpsf) |
| 2055 | continue; |
| 2056 | *dpsf = *psf; |
| 2057 | /* pmc->mca_lock held by callers */ |
| 2058 | dpsf->sf_next = pmc->mca_tomb; |
| 2059 | pmc->mca_tomb = dpsf; |
| 2060 | } |
| 2061 | dpsf->sf_crcount = qrv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2062 | rv++; |
| 2063 | } |
| 2064 | } |
| 2065 | return rv; |
| 2066 | } |
| 2067 | |
| 2068 | /* |
| 2069 | * Add multicast source filter list to the interface list |
| 2070 | */ |
Eric Dumazet | b71d1d4 | 2011-04-22 04:53:02 +0000 | [diff] [blame] | 2071 | static int ip6_mc_add_src(struct inet6_dev *idev, const struct in6_addr *pmca, |
| 2072 | int sfmode, int sfcount, const struct in6_addr *psfsrc, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2073 | int delta) |
| 2074 | { |
| 2075 | struct ifmcaddr6 *pmc; |
| 2076 | int isexclude; |
| 2077 | int i, err; |
| 2078 | |
| 2079 | if (!idev) |
| 2080 | return -ENODEV; |
| 2081 | read_lock_bh(&idev->lock); |
| 2082 | for (pmc=idev->mc_list; pmc; pmc=pmc->next) { |
| 2083 | if (ipv6_addr_equal(pmca, &pmc->mca_addr)) |
| 2084 | break; |
| 2085 | } |
| 2086 | if (!pmc) { |
| 2087 | /* MCA not found?? bug */ |
| 2088 | read_unlock_bh(&idev->lock); |
| 2089 | return -ESRCH; |
| 2090 | } |
| 2091 | spin_lock_bh(&pmc->mca_lock); |
| 2092 | |
| 2093 | sf_markstate(pmc); |
| 2094 | isexclude = pmc->mca_sfmode == MCAST_EXCLUDE; |
| 2095 | if (!delta) |
| 2096 | pmc->mca_sfcount[sfmode]++; |
| 2097 | err = 0; |
| 2098 | for (i=0; i<sfcount; i++) { |
Jun Zhao | 99d2f47 | 2011-11-30 06:21:05 +0000 | [diff] [blame] | 2099 | err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | if (err) |
| 2101 | break; |
| 2102 | } |
| 2103 | if (err) { |
| 2104 | int j; |
| 2105 | |
| 2106 | if (!delta) |
| 2107 | pmc->mca_sfcount[sfmode]--; |
| 2108 | for (j=0; j<i; j++) |
RongQing.Li | 78d5021 | 2012-04-04 16:47:04 +0000 | [diff] [blame] | 2109 | ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | } else if (isexclude != (pmc->mca_sfcount[MCAST_EXCLUDE] != 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | struct ip6_sf_list *psf; |
| 2112 | |
| 2113 | /* filter mode change */ |
| 2114 | if (pmc->mca_sfcount[MCAST_EXCLUDE]) |
| 2115 | pmc->mca_sfmode = MCAST_EXCLUDE; |
| 2116 | else if (pmc->mca_sfcount[MCAST_INCLUDE]) |
| 2117 | pmc->mca_sfmode = MCAST_INCLUDE; |
| 2118 | /* else no filters; keep old mode for reports */ |
| 2119 | |
| 2120 | pmc->mca_crcount = idev->mc_qrv; |
| 2121 | idev->mc_ifc_count = pmc->mca_crcount; |
| 2122 | for (psf=pmc->mca_sources; psf; psf = psf->sf_next) |
| 2123 | psf->sf_crcount = 0; |
| 2124 | mld_ifc_event(idev); |
| 2125 | } else if (sf_setstate(pmc)) |
| 2126 | mld_ifc_event(idev); |
| 2127 | spin_unlock_bh(&pmc->mca_lock); |
| 2128 | read_unlock_bh(&idev->lock); |
| 2129 | return err; |
| 2130 | } |
| 2131 | |
| 2132 | static void ip6_mc_clear_src(struct ifmcaddr6 *pmc) |
| 2133 | { |
| 2134 | struct ip6_sf_list *psf, *nextpsf; |
| 2135 | |
| 2136 | for (psf=pmc->mca_tomb; psf; psf=nextpsf) { |
| 2137 | nextpsf = psf->sf_next; |
| 2138 | kfree(psf); |
| 2139 | } |
| 2140 | pmc->mca_tomb = NULL; |
| 2141 | for (psf=pmc->mca_sources; psf; psf=nextpsf) { |
| 2142 | nextpsf = psf->sf_next; |
| 2143 | kfree(psf); |
| 2144 | } |
| 2145 | pmc->mca_sources = NULL; |
| 2146 | pmc->mca_sfmode = MCAST_EXCLUDE; |
Denis Lukianov | de9daad | 2005-09-14 20:53:42 -0700 | [diff] [blame] | 2147 | pmc->mca_sfcount[MCAST_INCLUDE] = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | pmc->mca_sfcount[MCAST_EXCLUDE] = 1; |
| 2149 | } |
| 2150 | |
| 2151 | |
| 2152 | static void igmp6_join_group(struct ifmcaddr6 *ma) |
| 2153 | { |
| 2154 | unsigned long delay; |
| 2155 | |
| 2156 | if (ma->mca_flags & MAF_NOREPORT) |
| 2157 | return; |
| 2158 | |
| 2159 | igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT); |
| 2160 | |
| 2161 | delay = net_random() % IGMP6_UNSOLICITED_IVAL; |
| 2162 | |
| 2163 | spin_lock_bh(&ma->mca_lock); |
| 2164 | if (del_timer(&ma->mca_timer)) { |
| 2165 | atomic_dec(&ma->mca_refcnt); |
| 2166 | delay = ma->mca_timer.expires - jiffies; |
| 2167 | } |
| 2168 | |
| 2169 | if (!mod_timer(&ma->mca_timer, jiffies + delay)) |
| 2170 | atomic_inc(&ma->mca_refcnt); |
| 2171 | ma->mca_flags |= MAF_TIMER_RUNNING | MAF_LAST_REPORTER; |
| 2172 | spin_unlock_bh(&ma->mca_lock); |
| 2173 | } |
| 2174 | |
| 2175 | static int ip6_mc_leave_src(struct sock *sk, struct ipv6_mc_socklist *iml, |
| 2176 | struct inet6_dev *idev) |
| 2177 | { |
| 2178 | int err; |
| 2179 | |
David L Stevens | 5ab4a6c | 2005-12-27 14:03:00 -0800 | [diff] [blame] | 2180 | /* callers have the socket lock and a write lock on ipv6_sk_mc_lock, |
| 2181 | * so no other readers or writers of iml or its sflist |
| 2182 | */ |
Stephen Hemminger | cfcabdc | 2007-10-09 01:59:42 -0700 | [diff] [blame] | 2183 | if (!iml->sflist) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2184 | /* any-source empty exclude case */ |
| 2185 | return ip6_mc_del_src(idev, &iml->addr, iml->sfmode, 0, NULL, 0); |
| 2186 | } |
| 2187 | err = ip6_mc_del_src(idev, &iml->addr, iml->sfmode, |
| 2188 | iml->sflist->sl_count, iml->sflist->sl_addr, 0); |
| 2189 | sock_kfree_s(sk, iml->sflist, IP6_SFLSIZE(iml->sflist->sl_max)); |
| 2190 | iml->sflist = NULL; |
| 2191 | return err; |
| 2192 | } |
| 2193 | |
| 2194 | static void igmp6_leave_group(struct ifmcaddr6 *ma) |
| 2195 | { |
| 2196 | if (MLD_V1_SEEN(ma->idev)) { |
| 2197 | if (ma->mca_flags & MAF_LAST_REPORTER) |
| 2198 | igmp6_send(&ma->mca_addr, ma->idev->dev, |
| 2199 | ICMPV6_MGM_REDUCTION); |
| 2200 | } else { |
| 2201 | mld_add_delrec(ma->idev, ma); |
| 2202 | mld_ifc_event(ma->idev); |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | static void mld_gq_timer_expire(unsigned long data) |
| 2207 | { |
| 2208 | struct inet6_dev *idev = (struct inet6_dev *)data; |
| 2209 | |
| 2210 | idev->mc_gq_running = 0; |
| 2211 | mld_send_report(idev, NULL); |
| 2212 | __in6_dev_put(idev); |
| 2213 | } |
| 2214 | |
| 2215 | static void mld_ifc_timer_expire(unsigned long data) |
| 2216 | { |
| 2217 | struct inet6_dev *idev = (struct inet6_dev *)data; |
| 2218 | |
| 2219 | mld_send_cr(idev); |
| 2220 | if (idev->mc_ifc_count) { |
| 2221 | idev->mc_ifc_count--; |
| 2222 | if (idev->mc_ifc_count) |
| 2223 | mld_ifc_start_timer(idev, idev->mc_maxdelay); |
| 2224 | } |
| 2225 | __in6_dev_put(idev); |
| 2226 | } |
| 2227 | |
| 2228 | static void mld_ifc_event(struct inet6_dev *idev) |
| 2229 | { |
| 2230 | if (MLD_V1_SEEN(idev)) |
| 2231 | return; |
| 2232 | idev->mc_ifc_count = idev->mc_qrv; |
| 2233 | mld_ifc_start_timer(idev, 1); |
| 2234 | } |
| 2235 | |
| 2236 | |
| 2237 | static void igmp6_timer_handler(unsigned long data) |
| 2238 | { |
| 2239 | struct ifmcaddr6 *ma = (struct ifmcaddr6 *) data; |
| 2240 | |
| 2241 | if (MLD_V1_SEEN(ma->idev)) |
| 2242 | igmp6_send(&ma->mca_addr, ma->idev->dev, ICMPV6_MGM_REPORT); |
| 2243 | else |
| 2244 | mld_send_report(ma->idev, ma); |
| 2245 | |
| 2246 | spin_lock(&ma->mca_lock); |
| 2247 | ma->mca_flags |= MAF_LAST_REPORTER; |
| 2248 | ma->mca_flags &= ~MAF_TIMER_RUNNING; |
| 2249 | spin_unlock(&ma->mca_lock); |
| 2250 | ma_put(ma); |
| 2251 | } |
| 2252 | |
Moni Shoua | 75c7850 | 2009-09-15 02:37:40 -0700 | [diff] [blame] | 2253 | /* Device changing type */ |
| 2254 | |
| 2255 | void ipv6_mc_unmap(struct inet6_dev *idev) |
| 2256 | { |
| 2257 | struct ifmcaddr6 *i; |
| 2258 | |
| 2259 | /* Install multicast list, except for all-nodes (already installed) */ |
| 2260 | |
| 2261 | read_lock_bh(&idev->lock); |
| 2262 | for (i = idev->mc_list; i; i = i->next) |
| 2263 | igmp6_group_dropped(i); |
| 2264 | read_unlock_bh(&idev->lock); |
| 2265 | } |
| 2266 | |
| 2267 | void ipv6_mc_remap(struct inet6_dev *idev) |
| 2268 | { |
| 2269 | ipv6_mc_up(idev); |
| 2270 | } |
| 2271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2272 | /* Device going down */ |
| 2273 | |
| 2274 | void ipv6_mc_down(struct inet6_dev *idev) |
| 2275 | { |
| 2276 | struct ifmcaddr6 *i; |
| 2277 | |
| 2278 | /* Withdraw multicast list */ |
| 2279 | |
| 2280 | read_lock_bh(&idev->lock); |
| 2281 | idev->mc_ifc_count = 0; |
| 2282 | if (del_timer(&idev->mc_ifc_timer)) |
| 2283 | __in6_dev_put(idev); |
| 2284 | idev->mc_gq_running = 0; |
| 2285 | if (del_timer(&idev->mc_gq_timer)) |
| 2286 | __in6_dev_put(idev); |
Hannes Frederic Sowa | b173ee4 | 2013-06-27 00:07:01 +0200 | [diff] [blame] | 2287 | if (del_timer(&idev->mc_dad_timer)) |
| 2288 | __in6_dev_put(idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | |
| 2290 | for (i = idev->mc_list; i; i=i->next) |
| 2291 | igmp6_group_dropped(i); |
| 2292 | read_unlock_bh(&idev->lock); |
| 2293 | |
| 2294 | mld_clear_delrec(idev); |
| 2295 | } |
| 2296 | |
| 2297 | |
| 2298 | /* Device going up */ |
| 2299 | |
| 2300 | void ipv6_mc_up(struct inet6_dev *idev) |
| 2301 | { |
| 2302 | struct ifmcaddr6 *i; |
| 2303 | |
| 2304 | /* Install multicast list, except for all-nodes (already installed) */ |
| 2305 | |
| 2306 | read_lock_bh(&idev->lock); |
| 2307 | for (i = idev->mc_list; i; i=i->next) |
| 2308 | igmp6_group_added(i); |
| 2309 | read_unlock_bh(&idev->lock); |
| 2310 | } |
| 2311 | |
| 2312 | /* IPv6 device initialization. */ |
| 2313 | |
| 2314 | void ipv6_mc_init_dev(struct inet6_dev *idev) |
| 2315 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2316 | write_lock_bh(&idev->lock); |
Stephen Hemminger | 6457d26 | 2010-02-17 18:48:44 -0800 | [diff] [blame] | 2317 | spin_lock_init(&idev->mc_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2318 | idev->mc_gq_running = 0; |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 2319 | setup_timer(&idev->mc_gq_timer, mld_gq_timer_expire, |
| 2320 | (unsigned long)idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2321 | idev->mc_tomb = NULL; |
| 2322 | idev->mc_ifc_count = 0; |
Pavel Emelyanov | b24b8a2 | 2008-01-23 21:20:07 -0800 | [diff] [blame] | 2323 | setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire, |
| 2324 | (unsigned long)idev); |
Hannes Frederic Sowa | b173ee4 | 2013-06-27 00:07:01 +0200 | [diff] [blame] | 2325 | setup_timer(&idev->mc_dad_timer, mld_dad_timer_expire, |
| 2326 | (unsigned long)idev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2327 | idev->mc_qrv = MLD_QRV_DEFAULT; |
| 2328 | idev->mc_maxdelay = IGMP6_UNSOLICITED_IVAL; |
| 2329 | idev->mc_v1_seen = 0; |
| 2330 | write_unlock_bh(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2331 | } |
| 2332 | |
| 2333 | /* |
| 2334 | * Device is about to be destroyed: clean up. |
| 2335 | */ |
| 2336 | |
| 2337 | void ipv6_mc_destroy_dev(struct inet6_dev *idev) |
| 2338 | { |
| 2339 | struct ifmcaddr6 *i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | |
| 2341 | /* Deactivate timers */ |
| 2342 | ipv6_mc_down(idev); |
| 2343 | |
| 2344 | /* Delete all-nodes address. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | /* We cannot call ipv6_dev_mc_dec() directly, our caller in |
| 2346 | * addrconf.c has NULL'd out dev->ip6_ptr so in6_dev_get() will |
| 2347 | * fail. |
| 2348 | */ |
YOSHIFUJI Hideaki | f3ee401 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 2349 | __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allnodes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2350 | |
YOSHIFUJI Hideaki | f3ee401 | 2008-04-10 15:42:11 +0900 | [diff] [blame] | 2351 | if (idev->cnf.forwarding) |
| 2352 | __ipv6_dev_mc_dec(idev, &in6addr_linklocal_allrouters); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2353 | |
| 2354 | write_lock_bh(&idev->lock); |
| 2355 | while ((i = idev->mc_list) != NULL) { |
| 2356 | idev->mc_list = i->next; |
| 2357 | write_unlock_bh(&idev->lock); |
| 2358 | |
| 2359 | igmp6_group_dropped(i); |
| 2360 | ma_put(i); |
| 2361 | |
| 2362 | write_lock_bh(&idev->lock); |
| 2363 | } |
| 2364 | write_unlock_bh(&idev->lock); |
| 2365 | } |
| 2366 | |
| 2367 | #ifdef CONFIG_PROC_FS |
| 2368 | struct igmp6_mc_iter_state { |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 2369 | struct seq_net_private p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2370 | struct net_device *dev; |
| 2371 | struct inet6_dev *idev; |
| 2372 | }; |
| 2373 | |
| 2374 | #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private) |
| 2375 | |
| 2376 | static inline struct ifmcaddr6 *igmp6_mc_get_first(struct seq_file *seq) |
| 2377 | { |
| 2378 | struct ifmcaddr6 *im = NULL; |
| 2379 | struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq); |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 2380 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 2382 | state->idev = NULL; |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2383 | for_each_netdev_rcu(net, state->dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2384 | struct inet6_dev *idev; |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2385 | idev = __in6_dev_get(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2386 | if (!idev) |
| 2387 | continue; |
| 2388 | read_lock_bh(&idev->lock); |
| 2389 | im = idev->mc_list; |
| 2390 | if (im) { |
| 2391 | state->idev = idev; |
| 2392 | break; |
| 2393 | } |
| 2394 | read_unlock_bh(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | } |
| 2396 | return im; |
| 2397 | } |
| 2398 | |
| 2399 | static struct ifmcaddr6 *igmp6_mc_get_next(struct seq_file *seq, struct ifmcaddr6 *im) |
| 2400 | { |
| 2401 | struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq); |
| 2402 | |
| 2403 | im = im->next; |
| 2404 | while (!im) { |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2405 | if (likely(state->idev != NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2406 | read_unlock_bh(&state->idev->lock); |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2407 | |
| 2408 | state->dev = next_net_device_rcu(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | if (!state->dev) { |
| 2410 | state->idev = NULL; |
| 2411 | break; |
| 2412 | } |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2413 | state->idev = __in6_dev_get(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2414 | if (!state->idev) |
| 2415 | continue; |
| 2416 | read_lock_bh(&state->idev->lock); |
| 2417 | im = state->idev->mc_list; |
| 2418 | } |
| 2419 | return im; |
| 2420 | } |
| 2421 | |
| 2422 | static struct ifmcaddr6 *igmp6_mc_get_idx(struct seq_file *seq, loff_t pos) |
| 2423 | { |
| 2424 | struct ifmcaddr6 *im = igmp6_mc_get_first(seq); |
| 2425 | if (im) |
| 2426 | while (pos && (im = igmp6_mc_get_next(seq, im)) != NULL) |
| 2427 | --pos; |
| 2428 | return pos ? NULL : im; |
| 2429 | } |
| 2430 | |
| 2431 | static void *igmp6_mc_seq_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2432 | __acquires(RCU) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | { |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2434 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2435 | return igmp6_mc_get_idx(seq, *pos); |
| 2436 | } |
| 2437 | |
| 2438 | static void *igmp6_mc_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2439 | { |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2440 | struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v); |
| 2441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | ++*pos; |
| 2443 | return im; |
| 2444 | } |
| 2445 | |
| 2446 | static void igmp6_mc_seq_stop(struct seq_file *seq, void *v) |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2447 | __releases(RCU) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | { |
| 2449 | struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq); |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2451 | if (likely(state->idev != NULL)) { |
| 2452 | read_unlock_bh(&state->idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2453 | state->idev = NULL; |
| 2454 | } |
| 2455 | state->dev = NULL; |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2456 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2457 | } |
| 2458 | |
| 2459 | static int igmp6_mc_seq_show(struct seq_file *seq, void *v) |
| 2460 | { |
| 2461 | struct ifmcaddr6 *im = (struct ifmcaddr6 *)v; |
| 2462 | struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq); |
| 2463 | |
| 2464 | seq_printf(seq, |
Harvey Harrison | 4b7a427 | 2008-10-29 12:50:24 -0700 | [diff] [blame] | 2465 | "%-4d %-15s %pi6 %5d %08X %ld\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | state->dev->ifindex, state->dev->name, |
Harvey Harrison | b071195 | 2008-10-28 16:05:40 -0700 | [diff] [blame] | 2467 | &im->mca_addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2468 | im->mca_users, im->mca_flags, |
| 2469 | (im->mca_flags&MAF_TIMER_RUNNING) ? |
| 2470 | jiffies_to_clock_t(im->mca_timer.expires-jiffies) : 0); |
| 2471 | return 0; |
| 2472 | } |
| 2473 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 2474 | static const struct seq_operations igmp6_mc_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2475 | .start = igmp6_mc_seq_start, |
| 2476 | .next = igmp6_mc_seq_next, |
| 2477 | .stop = igmp6_mc_seq_stop, |
| 2478 | .show = igmp6_mc_seq_show, |
| 2479 | }; |
| 2480 | |
| 2481 | static int igmp6_mc_seq_open(struct inode *inode, struct file *file) |
| 2482 | { |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 2483 | return seq_open_net(inode, file, &igmp6_mc_seq_ops, |
| 2484 | sizeof(struct igmp6_mc_iter_state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | } |
| 2486 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2487 | static const struct file_operations igmp6_mc_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | .owner = THIS_MODULE, |
| 2489 | .open = igmp6_mc_seq_open, |
| 2490 | .read = seq_read, |
| 2491 | .llseek = seq_lseek, |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 2492 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2493 | }; |
| 2494 | |
| 2495 | struct igmp6_mcf_iter_state { |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 2496 | struct seq_net_private p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2497 | struct net_device *dev; |
| 2498 | struct inet6_dev *idev; |
| 2499 | struct ifmcaddr6 *im; |
| 2500 | }; |
| 2501 | |
| 2502 | #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private) |
| 2503 | |
| 2504 | static inline struct ip6_sf_list *igmp6_mcf_get_first(struct seq_file *seq) |
| 2505 | { |
| 2506 | struct ip6_sf_list *psf = NULL; |
| 2507 | struct ifmcaddr6 *im = NULL; |
| 2508 | struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq); |
YOSHIFUJI Hideaki | 1218854 | 2008-03-26 02:36:06 +0900 | [diff] [blame] | 2509 | struct net *net = seq_file_net(seq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | |
Pavel Emelianov | 7562f87 | 2007-05-03 15:13:45 -0700 | [diff] [blame] | 2511 | state->idev = NULL; |
| 2512 | state->im = NULL; |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2513 | for_each_netdev_rcu(net, state->dev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | struct inet6_dev *idev; |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2515 | idev = __in6_dev_get(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | if (unlikely(idev == NULL)) |
| 2517 | continue; |
| 2518 | read_lock_bh(&idev->lock); |
| 2519 | im = idev->mc_list; |
| 2520 | if (likely(im != NULL)) { |
| 2521 | spin_lock_bh(&im->mca_lock); |
| 2522 | psf = im->mca_sources; |
| 2523 | if (likely(psf != NULL)) { |
| 2524 | state->im = im; |
| 2525 | state->idev = idev; |
| 2526 | break; |
| 2527 | } |
| 2528 | spin_unlock_bh(&im->mca_lock); |
| 2529 | } |
| 2530 | read_unlock_bh(&idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2531 | } |
| 2532 | return psf; |
| 2533 | } |
| 2534 | |
| 2535 | static struct ip6_sf_list *igmp6_mcf_get_next(struct seq_file *seq, struct ip6_sf_list *psf) |
| 2536 | { |
| 2537 | struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq); |
| 2538 | |
| 2539 | psf = psf->sf_next; |
| 2540 | while (!psf) { |
| 2541 | spin_unlock_bh(&state->im->mca_lock); |
| 2542 | state->im = state->im->next; |
| 2543 | while (!state->im) { |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2544 | if (likely(state->idev != NULL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2545 | read_unlock_bh(&state->idev->lock); |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2546 | |
| 2547 | state->dev = next_net_device_rcu(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2548 | if (!state->dev) { |
| 2549 | state->idev = NULL; |
| 2550 | goto out; |
| 2551 | } |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2552 | state->idev = __in6_dev_get(state->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | if (!state->idev) |
| 2554 | continue; |
| 2555 | read_lock_bh(&state->idev->lock); |
| 2556 | state->im = state->idev->mc_list; |
| 2557 | } |
| 2558 | if (!state->im) |
| 2559 | break; |
| 2560 | spin_lock_bh(&state->im->mca_lock); |
| 2561 | psf = state->im->mca_sources; |
| 2562 | } |
| 2563 | out: |
| 2564 | return psf; |
| 2565 | } |
| 2566 | |
| 2567 | static struct ip6_sf_list *igmp6_mcf_get_idx(struct seq_file *seq, loff_t pos) |
| 2568 | { |
| 2569 | struct ip6_sf_list *psf = igmp6_mcf_get_first(seq); |
| 2570 | if (psf) |
| 2571 | while (pos && (psf = igmp6_mcf_get_next(seq, psf)) != NULL) |
| 2572 | --pos; |
| 2573 | return pos ? NULL : psf; |
| 2574 | } |
| 2575 | |
| 2576 | static void *igmp6_mcf_seq_start(struct seq_file *seq, loff_t *pos) |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2577 | __acquires(RCU) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2578 | { |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2579 | rcu_read_lock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | return *pos ? igmp6_mcf_get_idx(seq, *pos - 1) : SEQ_START_TOKEN; |
| 2581 | } |
| 2582 | |
| 2583 | static void *igmp6_mcf_seq_next(struct seq_file *seq, void *v, loff_t *pos) |
| 2584 | { |
| 2585 | struct ip6_sf_list *psf; |
| 2586 | if (v == SEQ_START_TOKEN) |
| 2587 | psf = igmp6_mcf_get_first(seq); |
| 2588 | else |
| 2589 | psf = igmp6_mcf_get_next(seq, v); |
| 2590 | ++*pos; |
| 2591 | return psf; |
| 2592 | } |
| 2593 | |
| 2594 | static void igmp6_mcf_seq_stop(struct seq_file *seq, void *v) |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2595 | __releases(RCU) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | { |
| 2597 | struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq); |
| 2598 | if (likely(state->im != NULL)) { |
| 2599 | spin_unlock_bh(&state->im->mca_lock); |
| 2600 | state->im = NULL; |
| 2601 | } |
| 2602 | if (likely(state->idev != NULL)) { |
| 2603 | read_unlock_bh(&state->idev->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2604 | state->idev = NULL; |
| 2605 | } |
| 2606 | state->dev = NULL; |
Eric Dumazet | ce81b76 | 2009-11-11 17:34:30 +0000 | [diff] [blame] | 2607 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | static int igmp6_mcf_seq_show(struct seq_file *seq, void *v) |
| 2611 | { |
| 2612 | struct ip6_sf_list *psf = (struct ip6_sf_list *)v; |
| 2613 | struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq); |
| 2614 | |
| 2615 | if (v == SEQ_START_TOKEN) { |
YOSHIFUJI Hideaki | 1ab1457 | 2007-02-09 23:24:49 +0900 | [diff] [blame] | 2616 | seq_printf(seq, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | "%3s %6s " |
YOSHIFUJI Hideaki | 9343e79 | 2006-01-17 02:10:53 -0800 | [diff] [blame] | 2618 | "%32s %32s %6s %6s\n", "Idx", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2619 | "Device", "Multicast Address", |
| 2620 | "Source Address", "INC", "EXC"); |
| 2621 | } else { |
| 2622 | seq_printf(seq, |
Harvey Harrison | 4b7a427 | 2008-10-29 12:50:24 -0700 | [diff] [blame] | 2623 | "%3d %6.6s %pi6 %pi6 %6lu %6lu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2624 | state->dev->ifindex, state->dev->name, |
Harvey Harrison | b071195 | 2008-10-28 16:05:40 -0700 | [diff] [blame] | 2625 | &state->im->mca_addr, |
| 2626 | &psf->sf_addr, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2627 | psf->sf_count[MCAST_INCLUDE], |
| 2628 | psf->sf_count[MCAST_EXCLUDE]); |
| 2629 | } |
| 2630 | return 0; |
| 2631 | } |
| 2632 | |
Philippe De Muyter | 56b3d97 | 2007-07-10 23:07:31 -0700 | [diff] [blame] | 2633 | static const struct seq_operations igmp6_mcf_seq_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | .start = igmp6_mcf_seq_start, |
| 2635 | .next = igmp6_mcf_seq_next, |
| 2636 | .stop = igmp6_mcf_seq_stop, |
| 2637 | .show = igmp6_mcf_seq_show, |
| 2638 | }; |
| 2639 | |
| 2640 | static int igmp6_mcf_seq_open(struct inode *inode, struct file *file) |
| 2641 | { |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 2642 | return seq_open_net(inode, file, &igmp6_mcf_seq_ops, |
| 2643 | sizeof(struct igmp6_mcf_iter_state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2644 | } |
| 2645 | |
Arjan van de Ven | 9a32144 | 2007-02-12 00:55:35 -0800 | [diff] [blame] | 2646 | static const struct file_operations igmp6_mcf_seq_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2647 | .owner = THIS_MODULE, |
| 2648 | .open = igmp6_mcf_seq_open, |
| 2649 | .read = seq_read, |
| 2650 | .llseek = seq_lseek, |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 2651 | .release = seq_release_net, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | }; |
Daniel Lezcano | ea82edf | 2008-03-21 04:10:53 -0700 | [diff] [blame] | 2653 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2654 | static int __net_init igmp6_proc_init(struct net *net) |
Daniel Lezcano | ea82edf | 2008-03-21 04:10:53 -0700 | [diff] [blame] | 2655 | { |
| 2656 | int err; |
| 2657 | |
| 2658 | err = -ENOMEM; |
Gao feng | d4beaa6 | 2013-02-18 01:34:54 +0000 | [diff] [blame] | 2659 | if (!proc_create("igmp6", S_IRUGO, net->proc_net, &igmp6_mc_seq_fops)) |
Daniel Lezcano | ea82edf | 2008-03-21 04:10:53 -0700 | [diff] [blame] | 2660 | goto out; |
Gao feng | d4beaa6 | 2013-02-18 01:34:54 +0000 | [diff] [blame] | 2661 | if (!proc_create("mcfilter6", S_IRUGO, net->proc_net, |
| 2662 | &igmp6_mcf_seq_fops)) |
Daniel Lezcano | ea82edf | 2008-03-21 04:10:53 -0700 | [diff] [blame] | 2663 | goto out_proc_net_igmp6; |
| 2664 | |
| 2665 | err = 0; |
| 2666 | out: |
| 2667 | return err; |
| 2668 | |
| 2669 | out_proc_net_igmp6: |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 2670 | remove_proc_entry("igmp6", net->proc_net); |
Daniel Lezcano | ea82edf | 2008-03-21 04:10:53 -0700 | [diff] [blame] | 2671 | goto out; |
| 2672 | } |
| 2673 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2674 | static void __net_exit igmp6_proc_exit(struct net *net) |
Daniel Lezcano | ea82edf | 2008-03-21 04:10:53 -0700 | [diff] [blame] | 2675 | { |
Gao feng | ece31ff | 2013-02-18 01:34:56 +0000 | [diff] [blame] | 2676 | remove_proc_entry("mcfilter6", net->proc_net); |
| 2677 | remove_proc_entry("igmp6", net->proc_net); |
Daniel Lezcano | ea82edf | 2008-03-21 04:10:53 -0700 | [diff] [blame] | 2678 | } |
| 2679 | #else |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2680 | static inline int igmp6_proc_init(struct net *net) |
Daniel Lezcano | ea82edf | 2008-03-21 04:10:53 -0700 | [diff] [blame] | 2681 | { |
| 2682 | return 0; |
| 2683 | } |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2684 | static inline void igmp6_proc_exit(struct net *net) |
Daniel Lezcano | ea82edf | 2008-03-21 04:10:53 -0700 | [diff] [blame] | 2685 | { |
Daniel Lezcano | ea82edf | 2008-03-21 04:10:53 -0700 | [diff] [blame] | 2686 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2687 | #endif |
| 2688 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2689 | static int __net_init igmp6_net_init(struct net *net) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2690 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2691 | int err; |
| 2692 | |
Denis V. Lunev | 1ed8516 | 2008-04-03 14:31:03 -0700 | [diff] [blame] | 2693 | err = inet_ctl_sock_create(&net->ipv6.igmp_sk, PF_INET6, |
| 2694 | SOCK_RAW, IPPROTO_ICMPV6, net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2695 | if (err < 0) { |
Joe Perches | f321383 | 2012-05-15 14:11:53 +0000 | [diff] [blame] | 2696 | pr_err("Failed to initialize the IGMP6 control socket (err %d)\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2697 | err); |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 2698 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | } |
| 2700 | |
Denis V. Lunev | 1ed8516 | 2008-04-03 14:31:03 -0700 | [diff] [blame] | 2701 | inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | |
Daniel Lezcano | ea82edf | 2008-03-21 04:10:53 -0700 | [diff] [blame] | 2703 | err = igmp6_proc_init(net); |
| 2704 | if (err) |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 2705 | goto out_sock_create; |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 2706 | out: |
| 2707 | return err; |
| 2708 | |
| 2709 | out_sock_create: |
Denis V. Lunev | 1ed8516 | 2008-04-03 14:31:03 -0700 | [diff] [blame] | 2710 | inet_ctl_sock_destroy(net->ipv6.igmp_sk); |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 2711 | goto out; |
| 2712 | } |
| 2713 | |
Alexey Dobriyan | 2c8c1e7 | 2010-01-17 03:35:32 +0000 | [diff] [blame] | 2714 | static void __net_exit igmp6_net_exit(struct net *net) |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 2715 | { |
Denis V. Lunev | 1ed8516 | 2008-04-03 14:31:03 -0700 | [diff] [blame] | 2716 | inet_ctl_sock_destroy(net->ipv6.igmp_sk); |
Daniel Lezcano | ea82edf | 2008-03-21 04:10:53 -0700 | [diff] [blame] | 2717 | igmp6_proc_exit(net); |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 2718 | } |
| 2719 | |
| 2720 | static struct pernet_operations igmp6_net_ops = { |
| 2721 | .init = igmp6_net_init, |
| 2722 | .exit = igmp6_net_exit, |
| 2723 | }; |
| 2724 | |
| 2725 | int __init igmp6_init(void) |
| 2726 | { |
| 2727 | return register_pernet_subsys(&igmp6_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2728 | } |
| 2729 | |
| 2730 | void igmp6_cleanup(void) |
| 2731 | { |
Daniel Lezcano | b8ad0cb | 2008-03-07 11:16:55 -0800 | [diff] [blame] | 2732 | unregister_pernet_subsys(&igmp6_net_ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | } |