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