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