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>
37 #include <linux/net.h>
39 #include <linux/in6.h>
40 #include <linux/netdevice.h>
41 #include <linux/if_arp.h>
46 #include <linux/slab.h>
49 #include <linux/netfilter.h>
50 #include <linux/netfilter_ipv6.h>
70 #define MDBG(x) printk x
76 static void *__mld2_query_bugs[]
__attribute__((__unused__)) = {
87 static void igmp6_join_group(
struct ifmcaddr6 *ma);
88 static void igmp6_leave_group(
struct ifmcaddr6 *ma);
89 static void igmp6_timer_handler(
unsigned long data);
91 static void mld_gq_timer_expire(
unsigned long data);
92 static void mld_ifc_timer_expire(
unsigned long data);
97 static int sf_setstate(
struct ifmcaddr6 *pmc);
98 static void sf_markstate(
struct ifmcaddr6 *pmc);
99 static void ip6_mc_clear_src(
struct ifmcaddr6 *pmc);
101 int sfmode,
int sfcount,
const struct in6_addr *psfsrc,
104 int sfmode,
int sfcount,
const struct in6_addr *psfsrc,
110 #define IGMP6_UNSOLICITED_IVAL (10*HZ)
111 #define MLD_QRV_DEFAULT 2
113 #define MLD_V1_SEEN(idev) (dev_net((idev)->dev)->ipv6.devconf_all->force_mld_version == 1 || \
114 (idev)->cnf.force_mld_version == 1 || \
115 ((idev)->mc_v1_seen && \
116 time_before(jiffies, (idev)->mc_v1_seen)))
118 #define IPV6_MLD_MAX_MSF 64
126 #define for_each_pmc_rcu(np, pmc) \
127 for (pmc = rcu_dereference(np->ipv6_mc_list); \
129 pmc = rcu_dereference(pmc->next))
136 struct net *
net = sock_net(sk);
139 if (!ipv6_addr_is_multicast(addr))
144 if ((ifindex == 0 || mc_lst->
ifindex == ifindex) &&
145 ipv6_addr_equal(&mc_lst->
addr, addr)) {
194 spin_lock(&ipv6_sk_mc_lock);
197 spin_unlock(&ipv6_sk_mc_lock);
212 struct net *
net = sock_net(sk);
214 if (!ipv6_addr_is_multicast(addr))
217 spin_lock(&ipv6_sk_mc_lock);
220 lockdep_is_held(&ipv6_sk_mc_lock))) !=
NULL ;
221 lnk = &mc_lst->
next) {
222 if ((ifindex == 0 || mc_lst->
ifindex == ifindex) &&
223 ipv6_addr_equal(&mc_lst->
addr, addr)) {
227 spin_unlock(&ipv6_sk_mc_lock);
234 (
void) ip6_mc_leave_src(sk, mc_lst, idev);
238 (
void) ip6_mc_leave_src(sk, mc_lst,
NULL);
245 spin_unlock(&ipv6_sk_mc_lock);
270 idev = __in6_dev_get(dev);
285 struct net *net = sock_net(sk);
287 spin_lock(&ipv6_sk_mc_lock);
289 lockdep_is_held(&ipv6_sk_mc_lock))) !=
NULL) {
293 spin_unlock(&ipv6_sk_mc_lock);
298 struct inet6_dev *idev = __in6_dev_get(dev);
300 (
void) ip6_mc_leave_src(sk, mc_lst, idev);
304 (
void) ip6_mc_leave_src(sk, mc_lst,
NULL);
310 spin_lock(&ipv6_sk_mc_lock);
312 spin_unlock(&ipv6_sk_mc_lock);
323 struct net *net = sock_net(sk);
332 if (!ipv6_addr_is_multicast(group))
347 if (ipv6_addr_equal(&pmc->
addr, group))
356 if (pmc->
sfmode != omode) {
360 }
else if (pmc->
sfmode != omode) {
362 ip6_mc_add_src(idev, group, omode, 0,
NULL, 0);
363 ip6_mc_del_src(idev, group, pmc->
sfmode, 0,
NULL, 0);
391 ip6_mc_del_src(idev, group, omode, 1, source, 1);
423 pmc->
sflist = psl = newpsl;
439 ip6_mc_add_src(idev, group, omode, 1, source, 1);
457 struct net *net = sock_net(sk);
463 if (!ipv6_addr_is_multicast(group))
470 idev = ip6_mc_find_dev_rcu(net, group, gsf->
gf_interface);
487 if (ipv6_addr_equal(&pmc->
addr, group))
508 err = ip6_mc_add_src(idev, group, gsf->
gf_fmode,
522 (
void) ip6_mc_del_src(idev, group, pmc->
sfmode,
526 (
void) ip6_mc_del_src(idev, group, pmc->
sfmode, 0,
NULL, 0);
548 struct net *net = sock_net(sk);
552 if (!ipv6_addr_is_multicast(group))
556 idev = ip6_mc_find_dev_rcu(net, group, gsf->
gf_interface);
573 if (ipv6_addr_equal(group, &pmc->
addr))
594 for (i=0; i<copycount; i++) {
599 memset(&ss, 0,
sizeof(ss));
602 if (
copy_to_user(&optval->gf_slist[i], &ss,
sizeof(ss)))
622 if (ipv6_addr_equal(&mc->
addr, mc_addr))
637 if (ipv6_addr_equal(&psl->
sl_addr[i], src_addr))
654 in6_dev_put(mc->
idev);
659 static void igmp6_group_added(
struct ifmcaddr6 *mc)
676 igmp6_join_group(mc);
682 mld_ifc_event(mc->
idev);
685 static void igmp6_group_dropped(
struct ifmcaddr6 *mc)
702 igmp6_leave_group(mc);
708 ip6_mc_clear_src(mc);
750 spin_unlock_bh(&idev->
mc_lock);
761 if (ipv6_addr_equal(&pmc->
mca_addr, pmca))
771 spin_unlock_bh(&idev->
mc_lock);
774 for (psf=pmc->
mca_tomb; psf; psf=psf_next) {
778 in6_dev_put(pmc->
idev);
783 static void mld_clear_delrec(
struct inet6_dev *idev)
790 spin_unlock_bh(&idev->
mc_lock);
792 for (; pmc; pmc = nextpmc) {
794 ip6_mc_clear_src(pmc);
795 in6_dev_put(pmc->
idev);
808 for (; psf; psf=psf_next) {
826 idev = in6_dev_get(dev);
839 if (ipv6_addr_equal(&mc->
mca_addr, addr)) {
863 mc->mca_addr = *
addr;
867 mc->mca_cstamp = mc->mca_tstamp =
jiffies;
875 if (ipv6_addr_is_ll_all_nodes(&mc->mca_addr) ||
883 mld_del_delrec(idev, &mc->mca_addr);
884 igmp6_group_added(mc);
898 if (ipv6_addr_equal(&ma->
mca_addr, addr)) {
903 igmp6_group_dropped(ma);
924 idev = __in6_dev_get(dev);
944 if (!pskb_may_pull(skb,
sizeof(
struct icmp6hdr)))
947 pic = icmp6_hdr(skb);
972 idev = __in6_dev_get(dev);
976 if (ipv6_addr_equal(&mc->
mca_addr, group))
980 if (src_addr && !ipv6_addr_any(src_addr)) {
985 if (ipv6_addr_equal(&psf->
sf_addr, src_addr))
1004 static void mld_gq_start_timer(
struct inet6_dev *idev)
1013 static void mld_ifc_start_timer(
struct inet6_dev *idev,
int delay)
1025 static void igmp6_group_queried(
struct ifmcaddr6 *ma,
unsigned long resptime)
1027 unsigned long delay = resptime;
1030 if (ipv6_addr_is_ll_all_nodes(&ma->
mca_addr) ||
1039 if (delay >= resptime) {
1052 static bool mld_xmarksources(
struct ifmcaddr6 *pmc,
int nsrcs,
1060 if (scount == nsrcs)
1062 for (i=0; i<nsrcs; i++) {
1068 if (ipv6_addr_equal(&srcs[i], &psf->
sf_addr)) {
1075 if (scount == nsrcs)
1080 static bool mld_marksources(
struct ifmcaddr6 *pmc,
int nsrcs,
1087 return mld_xmarksources(pmc, nsrcs, srcs);
1093 if (scount == nsrcs)
1095 for (i=0; i<nsrcs; i++) {
1096 if (ipv6_addr_equal(&srcs[i], &psf->
sf_addr)) {
1117 unsigned long max_delay;
1124 if (!pskb_may_pull(skb,
sizeof(
struct in6_addr)))
1129 len -= skb_network_header_len(skb);
1135 idev = __in6_dev_get(skb->
dev);
1140 mld = (
struct mld_msg *)icmp6_hdr(skb);
1142 group_type = ipv6_addr_type(group);
1153 max_delay = (
ntohs(mld->mld_maxdelay)*
HZ)/1000;
1155 switchback = (idev->
mc_qrv + 1) * max_delay;
1161 __in6_dev_put(idev);
1163 mld_clear_delrec(idev);
1164 }
else if (len >= 28) {
1165 int srcs_offset =
sizeof(
struct mld2_query) -
1166 sizeof(struct icmp6hdr);
1167 if (!pskb_may_pull(skb, srcs_offset))
1170 mlh2 = (
struct mld2_query *)skb_transport_header(skb);
1175 if (mlh2->mld2q_qrv)
1176 idev->
mc_qrv = mlh2->mld2q_qrv;
1181 mld_gq_start_timer(idev);
1186 if (!pskb_may_pull(skb, srcs_offset +
1190 mlh2 = (
struct mld2_query *)skb_transport_header(skb);
1200 igmp6_group_queried(ma, max_delay);
1205 if (!ipv6_addr_equal(group, &ma->
mca_addr))
1221 igmp6_group_queried(ma, max_delay);
1248 if (!pskb_may_pull(skb,
sizeof(*mld) -
sizeof(
struct icmp6hdr)))
1251 mld = (
struct mld_msg *)icmp6_hdr(skb);
1254 addr_type = ipv6_addr_type(&ipv6_hdr(skb)->
saddr);
1259 idev = __in6_dev_get(skb->
dev);
1283 int gdeleted,
int sdeleted)
1288 if (gdeleted || sdeleted)
1303 if (gdeleted || sdeleted)
1307 if (gdeleted || sdeleted)
1320 return gdeleted || (psf->
sf_crcount && sdeleted);
1321 return psf->
sf_crcount && !gdeleted && !sdeleted;
1327 mld_scount(
struct ifmcaddr6 *pmc,
int type,
int gdeleted,
int sdeleted)
1333 if (!is_in(pmc, psf, type, gdeleted, sdeleted))
1342 struct net *net = dev_net(dev);
1343 struct sock *
sk = net->ipv6.igmp_sk;
1356 size += hlen + tlen;
1364 skb_reserve(skb, hlen);
1379 skb_set_transport_header(skb, skb_tail_pointer(skb) - skb->
data);
1381 pmr = (
struct mld2_report *)skb_transport_header(skb);
1383 pmr->mld2r_resv1 = 0;
1384 pmr->mld2r_cksum = 0;
1385 pmr->mld2r_resv2 = 0;
1386 pmr->mld2r_ngrec = 0;
1390 static void mld_sendpack(
struct sk_buff *skb)
1392 struct ipv6hdr *pip6 = ipv6_hdr(skb);
1397 struct net *net = dev_net(skb->
dev);
1403 idev = __in6_dev_get(skb->
dev);
1416 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->
daddr,
1425 skb_dst_set(skb, dst);
1429 payload_len = skb->
len;
1449 static int grec_size(
struct ifmcaddr6 *pmc,
int type,
int gdel,
int sdel)
1451 return sizeof(
struct mld2_grec) + 16 * mld_scount(pmc,type,gdel,sdel);
1462 skb = mld_newpack(dev, dev->
mtu);
1470 pmr = (
struct mld2_report *)skb_transport_header(skb);
1471 pmr->mld2r_ngrec =
htons(
ntohs(pmr->mld2r_ngrec)+1);
1476 #define AVAILABLE(skb) ((skb) ? ((skb)->dev ? (skb)->dev->mtu - (skb)->len : \
1477 skb_tailroom(skb)) : 0)
1480 int type,
int gdeleted,
int sdeleted)
1485 struct ip6_sf_list *psf, *psf_next, *psf_prev, **psf_list;
1496 stotal = scount = 0;
1507 if (pmr && pmr->mld2r_ngrec &&
1508 AVAILABLE(skb) < grec_size(pmc, type, gdeleted, sdeleted)) {
1511 skb = mld_newpack(dev, dev->
mtu);
1516 for (psf=*psf_list; psf; psf=psf_next) {
1521 if (!is_in(pmc, psf, type, gdeleted, sdeleted)) {
1532 if (truncate && !first)
1538 skb = mld_newpack(dev, dev->
mtu);
1543 skb = add_grhead(skb, pmc, type, &pgr);
1554 if ((sdeleted || gdeleted) && psf->
sf_crcount == 0) {
1577 skb = add_grhead(skb, pmc, type, &pgr);
1603 skb = add_grec(skb, pmc, type, 0, 0);
1613 skb = add_grec(skb, pmc, type, 0, 0);
1623 static void mld_clear_zeros(
struct ip6_sf_list **ppsf)
1628 for (psf=*ppsf; psf; psf = psf_next) {
1641 static void mld_send_cr(
struct inet6_dev *idev)
1643 struct ifmcaddr6 *pmc, *pmc_prev, *pmc_next;
1652 for (pmc=idev->
mc_tomb; pmc; pmc=pmc_next) {
1653 pmc_next = pmc->
next;
1657 skb = add_grec(skb, pmc, type, 1, 0);
1658 skb = add_grec(skb, pmc, dtype, 1, 1);
1663 skb = add_grec(skb, pmc, type, 1, 0);
1674 pmc_prev->
next = pmc_next;
1677 in6_dev_put(pmc->
idev);
1694 skb = add_grec(skb, pmc, type, 0, 0);
1695 skb = add_grec(skb, pmc, dtype, 0, 1);
1703 skb = add_grec(skb, pmc, type, 0, 0);
1711 (
void) mld_sendpack(skb);
1716 struct net *net = dev_net(dev);
1717 struct sock *sk = net->ipv6.igmp_sk;
1738 payload_len = len +
sizeof(
ra);
1739 full_len =
sizeof(
struct ipv6hdr) + payload_len;
1743 IPSTATS_MIB_OUT, full_len);
1756 skb_reserve(skb, hlen);
1773 hdr->mld_type =
type;
1781 idev = __in6_dev_get(skb->
dev);
1784 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->
daddr,
1792 skb_dst_set(skb, dst);
1811 static int ip6_mc_del1_src(
struct ifmcaddr6 *pmc,
int sfmode,
1819 if (ipv6_addr_equal(&psf->
sf_addr, psfsrc))
1823 if (!psf || psf->
sf_count[sfmode] == 0) {
1849 int sfmode,
int sfcount,
const struct in6_addr *psfsrc,
1860 if (ipv6_addr_equal(pmca, &pmc->
mca_addr))
1879 for (i=0; i<sfcount; i++) {
1880 int rv = ip6_mc_del1_src(pmc, sfmode, &psfsrc[i]);
1882 changerec |= rv > 0;
1897 mld_ifc_event(pmc->
idev);
1898 }
else if (sf_setstate(pmc) || changerec)
1899 mld_ifc_event(pmc->
idev);
1908 static int ip6_mc_add1_src(
struct ifmcaddr6 *pmc,
int sfmode,
1915 if (ipv6_addr_equal(&psf->
sf_addr, psfsrc))
1934 static void sf_markstate(
struct ifmcaddr6 *pmc)
1948 static int sf_setstate(
struct ifmcaddr6 *pmc)
1952 int qrv = pmc->
idev->mc_qrv;
1968 if (ipv6_addr_equal(&dpsf->
sf_addr,
1990 if (ipv6_addr_equal(&dpsf->
sf_addr,
2013 int sfmode,
int sfcount,
const struct in6_addr *psfsrc,
2024 if (ipv6_addr_equal(pmca, &pmc->
mca_addr))
2039 for (i=0; i<sfcount; i++) {
2040 err = ip6_mc_add1_src(pmc, sfmode, &psfsrc[i]);
2050 ip6_mc_del1_src(pmc, sfmode, &psfsrc[j]);
2065 mld_ifc_event(idev);
2066 }
else if (sf_setstate(pmc))
2067 mld_ifc_event(idev);
2073 static void ip6_mc_clear_src(
struct ifmcaddr6 *pmc)
2077 for (psf=pmc->
mca_tomb; psf; psf=nextpsf) {
2093 static void igmp6_join_group(
struct ifmcaddr6 *ma)
2095 unsigned long delay;
2126 return ip6_mc_del_src(idev, &iml->
addr, iml->
sfmode, 0,
NULL, 0);
2128 err = ip6_mc_del_src(idev, &iml->
addr, iml->
sfmode,
2135 static void igmp6_leave_group(
struct ifmcaddr6 *ma)
2142 mld_add_delrec(ma->
idev, ma);
2143 mld_ifc_event(ma->
idev);
2147 static void mld_gq_timer_expire(
unsigned long data)
2152 mld_send_report(idev,
NULL);
2153 __in6_dev_put(idev);
2156 static void mld_ifc_timer_expire(
unsigned long data)
2166 __in6_dev_put(idev);
2169 static void mld_ifc_event(
struct inet6_dev *idev)
2174 mld_ifc_start_timer(idev, 1);
2178 static void igmp6_timer_handler(
unsigned long data)
2185 mld_send_report(ma->
idev, ma);
2204 igmp6_group_dropped(i);
2224 __in6_dev_put(idev);
2227 __in6_dev_put(idev);
2230 igmp6_group_dropped(i);
2233 mld_clear_delrec(idev);
2247 igmp6_group_added(i);
2259 (
unsigned long)idev);
2260 idev->mc_tomb =
NULL;
2261 idev->mc_ifc_count = 0;
2262 setup_timer(&idev->mc_ifc_timer, mld_ifc_timer_expire,
2263 (
unsigned long)idev);
2266 idev->mc_v1_seen = 0;
2288 if (idev->
cnf.forwarding)
2296 igmp6_group_dropped(i);
2304 #ifdef CONFIG_PROC_FS
2305 struct igmp6_mc_iter_state {
2311 #define igmp6_mc_seq_private(seq) ((struct igmp6_mc_iter_state *)(seq)->private)
2316 struct igmp6_mc_iter_state *
state = igmp6_mc_seq_private(seq);
2317 struct net *net = seq_file_net(seq);
2322 idev = __in6_dev_get(state->dev);
2338 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2345 state->dev = next_net_device_rcu(state->dev);
2350 state->idev = __in6_dev_get(state->dev);
2354 im = state->idev->mc_list;
2361 struct ifmcaddr6 *im = igmp6_mc_get_first(seq);
2363 while (pos && (im = igmp6_mc_get_next(seq, im)) !=
NULL)
2365 return pos ?
NULL : im;
2368 static void *igmp6_mc_seq_start(
struct seq_file *seq, loff_t *pos)
2372 return igmp6_mc_get_idx(seq, *pos);
2375 static void *igmp6_mc_seq_next(
struct seq_file *seq,
void *
v, loff_t *pos)
2377 struct ifmcaddr6 *im = igmp6_mc_get_next(seq, v);
2383 static void igmp6_mc_seq_stop(
struct seq_file *seq,
void *
v)
2386 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2396 static int igmp6_mc_seq_show(
struct seq_file *seq,
void *
v)
2399 struct igmp6_mc_iter_state *state = igmp6_mc_seq_private(seq);
2402 "%-4d %-15s %pi6 %5d %08X %ld\n",
2403 state->dev->ifindex, state->dev->name,
2412 .
start = igmp6_mc_seq_start,
2413 .next = igmp6_mc_seq_next,
2414 .stop = igmp6_mc_seq_stop,
2415 .show = igmp6_mc_seq_show,
2421 sizeof(
struct igmp6_mc_iter_state));
2426 .open = igmp6_mc_seq_open,
2432 struct igmp6_mcf_iter_state {
2439 #define igmp6_mcf_seq_private(seq) ((struct igmp6_mcf_iter_state *)(seq)->private)
2445 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2446 struct net *net = seq_file_net(seq);
2452 idev = __in6_dev_get(state->dev);
2474 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2478 spin_unlock_bh(&state->im->mca_lock);
2479 state->im = state->im->next;
2480 while (!state->im) {
2484 state->dev = next_net_device_rcu(state->dev);
2489 state->idev = __in6_dev_get(state->dev);
2493 state->im = state->idev->mc_list;
2497 spin_lock_bh(&state->im->mca_lock);
2498 psf = state->im->mca_sources;
2506 struct ip6_sf_list *psf = igmp6_mcf_get_first(seq);
2508 while (pos && (psf = igmp6_mcf_get_next(seq, psf)) !=
NULL)
2510 return pos ?
NULL : psf;
2513 static void *igmp6_mcf_seq_start(
struct seq_file *seq, loff_t *pos)
2520 static void *igmp6_mcf_seq_next(
struct seq_file *seq,
void *v, loff_t *pos)
2524 psf = igmp6_mcf_get_first(seq);
2526 psf = igmp6_mcf_get_next(seq, v);
2531 static void igmp6_mcf_seq_stop(
struct seq_file *seq,
void *v)
2534 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2536 spin_unlock_bh(&state->im->mca_lock);
2547 static int igmp6_mcf_seq_show(
struct seq_file *seq,
void *v)
2550 struct igmp6_mcf_iter_state *state = igmp6_mcf_seq_private(seq);
2555 "%32s %32s %6s %6s\n",
"Idx",
2556 "Device",
"Multicast Address",
2557 "Source Address",
"INC",
"EXC");
2560 "%3d %6.6s %pi6 %pi6 %6lu %6lu\n",
2561 state->dev->ifindex, state->dev->name,
2562 &state->im->mca_addr,
2571 .
start = igmp6_mcf_seq_start,
2572 .next = igmp6_mcf_seq_next,
2573 .stop = igmp6_mcf_seq_stop,
2574 .show = igmp6_mcf_seq_show,
2577 static int igmp6_mcf_seq_open(
struct inode *inode,
struct file *file)
2580 sizeof(
struct igmp6_mcf_iter_state));
2585 .open = igmp6_mcf_seq_open,
2591 static int __net_init igmp6_proc_init(
struct net *net)
2599 &igmp6_mcf_seq_fops))
2600 goto out_proc_net_igmp6;
2611 static void __net_exit igmp6_proc_exit(
struct net *net)
2617 static inline int igmp6_proc_init(
struct net *net)
2621 static inline void igmp6_proc_exit(
struct net *net)
2626 static int __net_init igmp6_net_init(
struct net *net)
2633 pr_err(
"Failed to initialize the IGMP6 control socket (err %d)\n",
2638 inet6_sk(net->ipv6.igmp_sk)->hop_limit = 1;
2640 err = igmp6_proc_init(net);
2642 goto out_sock_create;
2647 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2651 static void __net_exit igmp6_net_exit(
struct net *net)
2653 inet_ctl_sock_destroy(net->ipv6.igmp_sk);
2654 igmp6_proc_exit(net);
2658 .init = igmp6_net_init,
2659 .exit = igmp6_net_exit,