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