2 * Linux IPv6 multicast routing support for BSD pim6sd
3 * Based on net/ipv4/ipmr.c.
5 * (c) 2004 Mickael Hoerdt, <hoerdt@clarinet.u-strasbg.fr>
6 * LSIIT Laboratory, Strasbourg, France
7 * (c) 2004 Jean-Philippe Andriot, <jean-philippe.andriot@6WIND.com>
9 * Copyright (C)2007,2008 USAGI/WIDE Project
10 * YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version
15 * 2 of the License, or (at your option) any later version.
19 #include <asm/uaccess.h>
20 #include <linux/types.h>
21 #include <linux/sched.h>
22 #include <linux/errno.h>
23 #include <linux/timer.h>
25 #include <linux/kernel.h>
26 #include <linux/fcntl.h>
27 #include <linux/stat.h>
28 #include <linux/socket.h>
29 #include <linux/inet.h>
30 #include <linux/netdevice.h>
31 #include <linux/inetdevice.h>
32 #include <linux/proc_fs.h>
33 #include <linux/seq_file.h>
34 #include <linux/init.h>
35 #include <linux/slab.h>
36 #include <linux/compat.h>
37 #include <net/protocol.h>
38 #include <linux/skbuff.h>
41 #include <linux/notifier.h>
42 #include <linux/if_arp.h>
43 #include <net/checksum.h>
44 #include <net/netlink.h>
45 #include <net/fib_rules.h>
48 #include <net/ip6_route.h>
49 #include <linux/mroute6.h>
50 #include <linux/pim.h>
51 #include <net/addrconf.h>
52 #include <linux/netfilter_ipv6.h>
53 #include <linux/export.h>
54 #include <net/ip6_checksum.h>
55 #include <linux/netconf.h>
58 struct list_head list;
63 struct sock *mroute6_sk;
64 struct timer_list ipmr_expire_timer;
65 struct list_head mfc6_unres_queue;
66 struct list_head mfc6_cache_array[MFC6_LINES];
67 struct mif_device vif6_table[MAXMIFS];
69 atomic_t cache_resolve_queue_len;
70 bool mroute_do_assert;
72 #ifdef CONFIG_IPV6_PIMSM_V2
73 int mroute_reg_vif_num;
78 struct fib_rule common;
82 struct mr6_table *mrt;
85 /* Big lock, protecting vif table, mrt cache and mroute socket state.
86 Note that the changes are semaphored via rtnl_lock.
89 static DEFINE_RWLOCK(mrt_lock);
92 * Multicast router control variables
95 #define MIF_EXISTS(_mrt, _idx) ((_mrt)->vif6_table[_idx].dev != NULL)
97 /* Special spinlock for queue of unresolved entries */
98 static DEFINE_SPINLOCK(mfc_unres_lock);
100 /* We return to original Alan's scheme. Hash table of resolved
101 entries is changed only in process context and protected
102 with weak lock mrt_lock. Queue of unresolved entries is protected
103 with strong spinlock mfc_unres_lock.
105 In this case data path is free of exclusive locks at all.
108 static struct kmem_cache *mrt_cachep __read_mostly;
110 static struct mr6_table *ip6mr_new_table(struct net *net, u32 id);
111 static void ip6mr_free_table(struct mr6_table *mrt);
113 static int ip6_mr_forward(struct net *net, struct mr6_table *mrt,
114 struct sk_buff *skb, struct mfc6_cache *cache);
115 static int ip6mr_cache_report(struct mr6_table *mrt, struct sk_buff *pkt,
116 mifi_t mifi, int assert);
117 static int __ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
118 struct mfc6_cache *c, struct rtmsg *rtm);
119 static void mr6_netlink_event(struct mr6_table *mrt, struct mfc6_cache *mfc,
121 static int ip6mr_rtm_dumproute(struct sk_buff *skb,
122 struct netlink_callback *cb);
123 static void mroute_clean_tables(struct mr6_table *mrt);
124 static void ipmr_expire_process(unsigned long arg);
126 #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
127 #define ip6mr_for_each_table(mrt, net) \
128 list_for_each_entry_rcu(mrt, &net->ipv6.mr6_tables, list)
130 static struct mr6_table *ip6mr_get_table(struct net *net, u32 id)
132 struct mr6_table *mrt;
134 ip6mr_for_each_table(mrt, net) {
141 static int ip6mr_fib_lookup(struct net *net, struct flowi6 *flp6,
142 struct mr6_table **mrt)
144 struct ip6mr_result res;
145 struct fib_lookup_arg arg = { .result = &res, };
148 err = fib_rules_lookup(net->ipv6.mr6_rules_ops,
149 flowi6_to_flowi(flp6), 0, &arg);
156 static int ip6mr_rule_action(struct fib_rule *rule, struct flowi *flp,
157 int flags, struct fib_lookup_arg *arg)
159 struct ip6mr_result *res = arg->result;
160 struct mr6_table *mrt;
162 switch (rule->action) {
165 case FR_ACT_UNREACHABLE:
167 case FR_ACT_PROHIBIT:
169 case FR_ACT_BLACKHOLE:
174 mrt = ip6mr_get_table(rule->fr_net, rule->table);
181 static int ip6mr_rule_match(struct fib_rule *rule, struct flowi *flp, int flags)
186 static const struct nla_policy ip6mr_rule_policy[FRA_MAX + 1] = {
190 static int ip6mr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
191 struct fib_rule_hdr *frh, struct nlattr **tb)
196 static int ip6mr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
202 static int ip6mr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
203 struct fib_rule_hdr *frh)
211 static const struct fib_rules_ops __net_initconst ip6mr_rules_ops_template = {
212 .family = RTNL_FAMILY_IP6MR,
213 .rule_size = sizeof(struct ip6mr_rule),
214 .addr_size = sizeof(struct in6_addr),
215 .action = ip6mr_rule_action,
216 .match = ip6mr_rule_match,
217 .configure = ip6mr_rule_configure,
218 .compare = ip6mr_rule_compare,
219 .default_pref = fib_default_rule_pref,
220 .fill = ip6mr_rule_fill,
221 .nlgroup = RTNLGRP_IPV6_RULE,
222 .policy = ip6mr_rule_policy,
223 .owner = THIS_MODULE,
226 static int __net_init ip6mr_rules_init(struct net *net)
228 struct fib_rules_ops *ops;
229 struct mr6_table *mrt;
232 ops = fib_rules_register(&ip6mr_rules_ops_template, net);
236 INIT_LIST_HEAD(&net->ipv6.mr6_tables);
238 mrt = ip6mr_new_table(net, RT6_TABLE_DFLT);
244 err = fib_default_rule_add(ops, 0x7fff, RT6_TABLE_DFLT, 0);
248 net->ipv6.mr6_rules_ops = ops;
254 fib_rules_unregister(ops);
258 static void __net_exit ip6mr_rules_exit(struct net *net)
260 struct mr6_table *mrt, *next;
262 list_for_each_entry_safe(mrt, next, &net->ipv6.mr6_tables, list) {
263 list_del(&mrt->list);
264 ip6mr_free_table(mrt);
266 fib_rules_unregister(net->ipv6.mr6_rules_ops);
269 #define ip6mr_for_each_table(mrt, net) \
270 for (mrt = net->ipv6.mrt6; mrt; mrt = NULL)
272 static struct mr6_table *ip6mr_get_table(struct net *net, u32 id)
274 return net->ipv6.mrt6;
277 static int ip6mr_fib_lookup(struct net *net, struct flowi6 *flp6,
278 struct mr6_table **mrt)
280 *mrt = net->ipv6.mrt6;
284 static int __net_init ip6mr_rules_init(struct net *net)
286 net->ipv6.mrt6 = ip6mr_new_table(net, RT6_TABLE_DFLT);
287 return net->ipv6.mrt6 ? 0 : -ENOMEM;
290 static void __net_exit ip6mr_rules_exit(struct net *net)
292 ip6mr_free_table(net->ipv6.mrt6);
296 static struct mr6_table *ip6mr_new_table(struct net *net, u32 id)
298 struct mr6_table *mrt;
301 mrt = ip6mr_get_table(net, id);
305 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
309 write_pnet(&mrt->net, net);
311 /* Forwarding cache */
312 for (i = 0; i < MFC6_LINES; i++)
313 INIT_LIST_HEAD(&mrt->mfc6_cache_array[i]);
315 INIT_LIST_HEAD(&mrt->mfc6_unres_queue);
317 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
320 #ifdef CONFIG_IPV6_PIMSM_V2
321 mrt->mroute_reg_vif_num = -1;
323 #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
324 list_add_tail_rcu(&mrt->list, &net->ipv6.mr6_tables);
329 static void ip6mr_free_table(struct mr6_table *mrt)
331 del_timer(&mrt->ipmr_expire_timer);
332 mroute_clean_tables(mrt);
336 #ifdef CONFIG_PROC_FS
338 struct ipmr_mfc_iter {
339 struct seq_net_private p;
340 struct mr6_table *mrt;
341 struct list_head *cache;
346 static struct mfc6_cache *ipmr_mfc_seq_idx(struct net *net,
347 struct ipmr_mfc_iter *it, loff_t pos)
349 struct mr6_table *mrt = it->mrt;
350 struct mfc6_cache *mfc;
352 read_lock(&mrt_lock);
353 for (it->ct = 0; it->ct < MFC6_LINES; it->ct++) {
354 it->cache = &mrt->mfc6_cache_array[it->ct];
355 list_for_each_entry(mfc, it->cache, list)
359 read_unlock(&mrt_lock);
361 spin_lock_bh(&mfc_unres_lock);
362 it->cache = &mrt->mfc6_unres_queue;
363 list_for_each_entry(mfc, it->cache, list)
366 spin_unlock_bh(&mfc_unres_lock);
373 * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
376 struct ipmr_vif_iter {
377 struct seq_net_private p;
378 struct mr6_table *mrt;
382 static struct mif_device *ip6mr_vif_seq_idx(struct net *net,
383 struct ipmr_vif_iter *iter,
386 struct mr6_table *mrt = iter->mrt;
388 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
389 if (!MIF_EXISTS(mrt, iter->ct))
392 return &mrt->vif6_table[iter->ct];
397 static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
400 struct ipmr_vif_iter *iter = seq->private;
401 struct net *net = seq_file_net(seq);
402 struct mr6_table *mrt;
404 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
406 return ERR_PTR(-ENOENT);
410 read_lock(&mrt_lock);
411 return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1)
415 static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
417 struct ipmr_vif_iter *iter = seq->private;
418 struct net *net = seq_file_net(seq);
419 struct mr6_table *mrt = iter->mrt;
422 if (v == SEQ_START_TOKEN)
423 return ip6mr_vif_seq_idx(net, iter, 0);
425 while (++iter->ct < mrt->maxvif) {
426 if (!MIF_EXISTS(mrt, iter->ct))
428 return &mrt->vif6_table[iter->ct];
433 static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
436 read_unlock(&mrt_lock);
439 static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
441 struct ipmr_vif_iter *iter = seq->private;
442 struct mr6_table *mrt = iter->mrt;
444 if (v == SEQ_START_TOKEN) {
446 "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
448 const struct mif_device *vif = v;
449 const char *name = vif->dev ? vif->dev->name : "none";
452 "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
453 vif - mrt->vif6_table,
454 name, vif->bytes_in, vif->pkt_in,
455 vif->bytes_out, vif->pkt_out,
461 static const struct seq_operations ip6mr_vif_seq_ops = {
462 .start = ip6mr_vif_seq_start,
463 .next = ip6mr_vif_seq_next,
464 .stop = ip6mr_vif_seq_stop,
465 .show = ip6mr_vif_seq_show,
468 static int ip6mr_vif_open(struct inode *inode, struct file *file)
470 return seq_open_net(inode, file, &ip6mr_vif_seq_ops,
471 sizeof(struct ipmr_vif_iter));
474 static const struct file_operations ip6mr_vif_fops = {
475 .owner = THIS_MODULE,
476 .open = ip6mr_vif_open,
479 .release = seq_release_net,
482 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
484 struct ipmr_mfc_iter *it = seq->private;
485 struct net *net = seq_file_net(seq);
486 struct mr6_table *mrt;
488 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
490 return ERR_PTR(-ENOENT);
493 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
497 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
499 struct mfc6_cache *mfc = v;
500 struct ipmr_mfc_iter *it = seq->private;
501 struct net *net = seq_file_net(seq);
502 struct mr6_table *mrt = it->mrt;
506 if (v == SEQ_START_TOKEN)
507 return ipmr_mfc_seq_idx(net, seq->private, 0);
509 if (mfc->list.next != it->cache)
510 return list_entry(mfc->list.next, struct mfc6_cache, list);
512 if (it->cache == &mrt->mfc6_unres_queue)
515 BUG_ON(it->cache != &mrt->mfc6_cache_array[it->ct]);
517 while (++it->ct < MFC6_LINES) {
518 it->cache = &mrt->mfc6_cache_array[it->ct];
519 if (list_empty(it->cache))
521 return list_first_entry(it->cache, struct mfc6_cache, list);
524 /* exhausted cache_array, show unresolved */
525 read_unlock(&mrt_lock);
526 it->cache = &mrt->mfc6_unres_queue;
529 spin_lock_bh(&mfc_unres_lock);
530 if (!list_empty(it->cache))
531 return list_first_entry(it->cache, struct mfc6_cache, list);
534 spin_unlock_bh(&mfc_unres_lock);
540 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
542 struct ipmr_mfc_iter *it = seq->private;
543 struct mr6_table *mrt = it->mrt;
545 if (it->cache == &mrt->mfc6_unres_queue)
546 spin_unlock_bh(&mfc_unres_lock);
547 else if (it->cache == mrt->mfc6_cache_array)
548 read_unlock(&mrt_lock);
551 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
555 if (v == SEQ_START_TOKEN) {
559 "Iif Pkts Bytes Wrong Oifs\n");
561 const struct mfc6_cache *mfc = v;
562 const struct ipmr_mfc_iter *it = seq->private;
563 struct mr6_table *mrt = it->mrt;
565 seq_printf(seq, "%pI6 %pI6 %-3hd",
566 &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
569 if (it->cache != &mrt->mfc6_unres_queue) {
570 seq_printf(seq, " %8lu %8lu %8lu",
572 mfc->mfc_un.res.bytes,
573 mfc->mfc_un.res.wrong_if);
574 for (n = mfc->mfc_un.res.minvif;
575 n < mfc->mfc_un.res.maxvif; n++) {
576 if (MIF_EXISTS(mrt, n) &&
577 mfc->mfc_un.res.ttls[n] < 255)
580 n, mfc->mfc_un.res.ttls[n]);
583 /* unresolved mfc_caches don't contain
584 * pkt, bytes and wrong_if values
586 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
593 static const struct seq_operations ipmr_mfc_seq_ops = {
594 .start = ipmr_mfc_seq_start,
595 .next = ipmr_mfc_seq_next,
596 .stop = ipmr_mfc_seq_stop,
597 .show = ipmr_mfc_seq_show,
600 static int ipmr_mfc_open(struct inode *inode, struct file *file)
602 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
603 sizeof(struct ipmr_mfc_iter));
606 static const struct file_operations ip6mr_mfc_fops = {
607 .owner = THIS_MODULE,
608 .open = ipmr_mfc_open,
611 .release = seq_release_net,
615 #ifdef CONFIG_IPV6_PIMSM_V2
617 static int pim6_rcv(struct sk_buff *skb)
619 struct pimreghdr *pim;
620 struct ipv6hdr *encap;
621 struct net_device *reg_dev = NULL;
622 struct net *net = dev_net(skb->dev);
623 struct mr6_table *mrt;
624 struct flowi6 fl6 = {
625 .flowi6_iif = skb->dev->ifindex,
626 .flowi6_mark = skb->mark,
630 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
633 pim = (struct pimreghdr *)skb_transport_header(skb);
634 if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
635 (pim->flags & PIM_NULL_REGISTER) ||
636 (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
637 sizeof(*pim), IPPROTO_PIM,
638 csum_partial((void *)pim, sizeof(*pim), 0)) &&
639 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
642 /* check if the inner packet is destined to mcast group */
643 encap = (struct ipv6hdr *)(skb_transport_header(skb) +
646 if (!ipv6_addr_is_multicast(&encap->daddr) ||
647 encap->payload_len == 0 ||
648 ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
651 if (ip6mr_fib_lookup(net, &fl6, &mrt) < 0)
653 reg_vif_num = mrt->mroute_reg_vif_num;
655 read_lock(&mrt_lock);
656 if (reg_vif_num >= 0)
657 reg_dev = mrt->vif6_table[reg_vif_num].dev;
660 read_unlock(&mrt_lock);
665 skb->mac_header = skb->network_header;
666 skb_pull(skb, (u8 *)encap - skb->data);
667 skb_reset_network_header(skb);
668 skb->protocol = htons(ETH_P_IPV6);
669 skb->ip_summed = CHECKSUM_NONE;
670 skb->pkt_type = PACKET_HOST;
672 skb_tunnel_rx(skb, reg_dev);
683 static const struct inet6_protocol pim6_protocol = {
687 /* Service routines creating virtual interfaces: PIMREG */
689 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb,
690 struct net_device *dev)
692 struct net *net = dev_net(dev);
693 struct mr6_table *mrt;
694 struct flowi6 fl6 = {
695 .flowi6_oif = dev->ifindex,
696 .flowi6_iif = skb->skb_iif,
697 .flowi6_mark = skb->mark,
701 err = ip6mr_fib_lookup(net, &fl6, &mrt);
707 read_lock(&mrt_lock);
708 dev->stats.tx_bytes += skb->len;
709 dev->stats.tx_packets++;
710 ip6mr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, MRT6MSG_WHOLEPKT);
711 read_unlock(&mrt_lock);
716 static const struct net_device_ops reg_vif_netdev_ops = {
717 .ndo_start_xmit = reg_vif_xmit,
720 static void reg_vif_setup(struct net_device *dev)
722 dev->type = ARPHRD_PIMREG;
723 dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
724 dev->flags = IFF_NOARP;
725 dev->netdev_ops = ®_vif_netdev_ops;
726 dev->destructor = free_netdev;
727 dev->features |= NETIF_F_NETNS_LOCAL;
730 static struct net_device *ip6mr_reg_vif(struct net *net, struct mr6_table *mrt)
732 struct net_device *dev;
735 if (mrt->id == RT6_TABLE_DFLT)
736 sprintf(name, "pim6reg");
738 sprintf(name, "pim6reg%u", mrt->id);
740 dev = alloc_netdev(0, name, reg_vif_setup);
744 dev_net_set(dev, net);
746 if (register_netdevice(dev)) {
759 /* allow the register to be completed before unregistering. */
763 unregister_netdevice(dev);
772 static int mif6_delete(struct mr6_table *mrt, int vifi, struct list_head *head)
774 struct mif_device *v;
775 struct net_device *dev;
776 struct inet6_dev *in6_dev;
778 if (vifi < 0 || vifi >= mrt->maxvif)
779 return -EADDRNOTAVAIL;
781 v = &mrt->vif6_table[vifi];
783 write_lock_bh(&mrt_lock);
788 write_unlock_bh(&mrt_lock);
789 return -EADDRNOTAVAIL;
792 #ifdef CONFIG_IPV6_PIMSM_V2
793 if (vifi == mrt->mroute_reg_vif_num)
794 mrt->mroute_reg_vif_num = -1;
797 if (vifi + 1 == mrt->maxvif) {
799 for (tmp = vifi - 1; tmp >= 0; tmp--) {
800 if (MIF_EXISTS(mrt, tmp))
803 mrt->maxvif = tmp + 1;
806 write_unlock_bh(&mrt_lock);
808 dev_set_allmulti(dev, -1);
810 in6_dev = __in6_dev_get(dev);
812 in6_dev->cnf.mc_forwarding--;
813 inet6_netconf_notify_devconf(dev_net(dev),
814 NETCONFA_MC_FORWARDING,
815 dev->ifindex, &in6_dev->cnf);
818 if (v->flags & MIFF_REGISTER)
819 unregister_netdevice_queue(dev, head);
825 static inline void ip6mr_cache_free(struct mfc6_cache *c)
827 kmem_cache_free(mrt_cachep, c);
830 /* Destroy an unresolved cache entry, killing queued skbs
831 and reporting error to netlink readers.
834 static void ip6mr_destroy_unres(struct mr6_table *mrt, struct mfc6_cache *c)
836 struct net *net = read_pnet(&mrt->net);
839 atomic_dec(&mrt->cache_resolve_queue_len);
841 while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
842 if (ipv6_hdr(skb)->version == 0) {
843 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
844 nlh->nlmsg_type = NLMSG_ERROR;
845 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
846 skb_trim(skb, nlh->nlmsg_len);
847 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
848 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
857 /* Timer process for all the unresolved queue. */
859 static void ipmr_do_expire_process(struct mr6_table *mrt)
861 unsigned long now = jiffies;
862 unsigned long expires = 10 * HZ;
863 struct mfc6_cache *c, *next;
865 list_for_each_entry_safe(c, next, &mrt->mfc6_unres_queue, list) {
866 if (time_after(c->mfc_un.unres.expires, now)) {
868 unsigned long interval = c->mfc_un.unres.expires - now;
869 if (interval < expires)
875 mr6_netlink_event(mrt, c, RTM_DELROUTE);
876 ip6mr_destroy_unres(mrt, c);
879 if (!list_empty(&mrt->mfc6_unres_queue))
880 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
883 static void ipmr_expire_process(unsigned long arg)
885 struct mr6_table *mrt = (struct mr6_table *)arg;
887 if (!spin_trylock(&mfc_unres_lock)) {
888 mod_timer(&mrt->ipmr_expire_timer, jiffies + 1);
892 if (!list_empty(&mrt->mfc6_unres_queue))
893 ipmr_do_expire_process(mrt);
895 spin_unlock(&mfc_unres_lock);
898 /* Fill oifs list. It is called under write locked mrt_lock. */
900 static void ip6mr_update_thresholds(struct mr6_table *mrt, struct mfc6_cache *cache,
905 cache->mfc_un.res.minvif = MAXMIFS;
906 cache->mfc_un.res.maxvif = 0;
907 memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
909 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
910 if (MIF_EXISTS(mrt, vifi) &&
911 ttls[vifi] && ttls[vifi] < 255) {
912 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
913 if (cache->mfc_un.res.minvif > vifi)
914 cache->mfc_un.res.minvif = vifi;
915 if (cache->mfc_un.res.maxvif <= vifi)
916 cache->mfc_un.res.maxvif = vifi + 1;
921 static int mif6_add(struct net *net, struct mr6_table *mrt,
922 struct mif6ctl *vifc, int mrtsock)
924 int vifi = vifc->mif6c_mifi;
925 struct mif_device *v = &mrt->vif6_table[vifi];
926 struct net_device *dev;
927 struct inet6_dev *in6_dev;
931 if (MIF_EXISTS(mrt, vifi))
934 switch (vifc->mif6c_flags) {
935 #ifdef CONFIG_IPV6_PIMSM_V2
938 * Special Purpose VIF in PIM
939 * All the packets will be sent to the daemon
941 if (mrt->mroute_reg_vif_num >= 0)
943 dev = ip6mr_reg_vif(net, mrt);
946 err = dev_set_allmulti(dev, 1);
948 unregister_netdevice(dev);
955 dev = dev_get_by_index(net, vifc->mif6c_pifi);
957 return -EADDRNOTAVAIL;
958 err = dev_set_allmulti(dev, 1);
968 in6_dev = __in6_dev_get(dev);
970 in6_dev->cnf.mc_forwarding++;
971 inet6_netconf_notify_devconf(dev_net(dev),
972 NETCONFA_MC_FORWARDING,
973 dev->ifindex, &in6_dev->cnf);
977 * Fill in the VIF structures
979 v->rate_limit = vifc->vifc_rate_limit;
980 v->flags = vifc->mif6c_flags;
982 v->flags |= VIFF_STATIC;
983 v->threshold = vifc->vifc_threshold;
988 v->link = dev->ifindex;
989 if (v->flags & MIFF_REGISTER)
990 v->link = dev->iflink;
992 /* And finish update writing critical data */
993 write_lock_bh(&mrt_lock);
995 #ifdef CONFIG_IPV6_PIMSM_V2
996 if (v->flags & MIFF_REGISTER)
997 mrt->mroute_reg_vif_num = vifi;
999 if (vifi + 1 > mrt->maxvif)
1000 mrt->maxvif = vifi + 1;
1001 write_unlock_bh(&mrt_lock);
1005 static struct mfc6_cache *ip6mr_cache_find(struct mr6_table *mrt,
1006 const struct in6_addr *origin,
1007 const struct in6_addr *mcastgrp)
1009 int line = MFC6_HASH(mcastgrp, origin);
1010 struct mfc6_cache *c;
1012 list_for_each_entry(c, &mrt->mfc6_cache_array[line], list) {
1013 if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
1014 ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
1020 /* Look for a (*,*,oif) entry */
1021 static struct mfc6_cache *ip6mr_cache_find_any_parent(struct mr6_table *mrt,
1024 int line = MFC6_HASH(&in6addr_any, &in6addr_any);
1025 struct mfc6_cache *c;
1027 list_for_each_entry(c, &mrt->mfc6_cache_array[line], list)
1028 if (ipv6_addr_any(&c->mf6c_origin) &&
1029 ipv6_addr_any(&c->mf6c_mcastgrp) &&
1030 (c->mfc_un.res.ttls[mifi] < 255))
1036 /* Look for a (*,G) entry */
1037 static struct mfc6_cache *ip6mr_cache_find_any(struct mr6_table *mrt,
1038 struct in6_addr *mcastgrp,
1041 int line = MFC6_HASH(mcastgrp, &in6addr_any);
1042 struct mfc6_cache *c, *proxy;
1044 if (ipv6_addr_any(mcastgrp))
1047 list_for_each_entry(c, &mrt->mfc6_cache_array[line], list)
1048 if (ipv6_addr_any(&c->mf6c_origin) &&
1049 ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp)) {
1050 if (c->mfc_un.res.ttls[mifi] < 255)
1053 /* It's ok if the mifi is part of the static tree */
1054 proxy = ip6mr_cache_find_any_parent(mrt,
1056 if (proxy && proxy->mfc_un.res.ttls[mifi] < 255)
1061 return ip6mr_cache_find_any_parent(mrt, mifi);
1065 * Allocate a multicast cache entry
1067 static struct mfc6_cache *ip6mr_cache_alloc(void)
1069 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
1072 c->mfc_un.res.minvif = MAXMIFS;
1076 static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
1078 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
1081 skb_queue_head_init(&c->mfc_un.unres.unresolved);
1082 c->mfc_un.unres.expires = jiffies + 10 * HZ;
1087 * A cache entry has gone into a resolved state from queued
1090 static void ip6mr_cache_resolve(struct net *net, struct mr6_table *mrt,
1091 struct mfc6_cache *uc, struct mfc6_cache *c)
1093 struct sk_buff *skb;
1096 * Play the pending entries through our router
1099 while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
1100 if (ipv6_hdr(skb)->version == 0) {
1101 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
1103 if (__ip6mr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
1104 nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
1106 nlh->nlmsg_type = NLMSG_ERROR;
1107 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
1108 skb_trim(skb, nlh->nlmsg_len);
1109 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
1111 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
1113 ip6_mr_forward(net, mrt, skb, c);
1118 * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
1119 * expects the following bizarre scheme.
1121 * Called under mrt_lock.
1124 static int ip6mr_cache_report(struct mr6_table *mrt, struct sk_buff *pkt,
1125 mifi_t mifi, int assert)
1127 struct sk_buff *skb;
1128 struct mrt6msg *msg;
1131 #ifdef CONFIG_IPV6_PIMSM_V2
1132 if (assert == MRT6MSG_WHOLEPKT)
1133 skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
1137 skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
1142 /* I suppose that internal messages
1143 * do not require checksums */
1145 skb->ip_summed = CHECKSUM_UNNECESSARY;
1147 #ifdef CONFIG_IPV6_PIMSM_V2
1148 if (assert == MRT6MSG_WHOLEPKT) {
1149 /* Ugly, but we have no choice with this interface.
1150 Duplicate old header, fix length etc.
1151 And all this only to mangle msg->im6_msgtype and
1152 to set msg->im6_mbz to "mbz" :-)
1154 skb_push(skb, -skb_network_offset(pkt));
1156 skb_push(skb, sizeof(*msg));
1157 skb_reset_transport_header(skb);
1158 msg = (struct mrt6msg *)skb_transport_header(skb);
1160 msg->im6_msgtype = MRT6MSG_WHOLEPKT;
1161 msg->im6_mif = mrt->mroute_reg_vif_num;
1163 msg->im6_src = ipv6_hdr(pkt)->saddr;
1164 msg->im6_dst = ipv6_hdr(pkt)->daddr;
1166 skb->ip_summed = CHECKSUM_UNNECESSARY;
1171 * Copy the IP header
1174 skb_put(skb, sizeof(struct ipv6hdr));
1175 skb_reset_network_header(skb);
1176 skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
1181 skb_put(skb, sizeof(*msg));
1182 skb_reset_transport_header(skb);
1183 msg = (struct mrt6msg *)skb_transport_header(skb);
1186 msg->im6_msgtype = assert;
1187 msg->im6_mif = mifi;
1189 msg->im6_src = ipv6_hdr(pkt)->saddr;
1190 msg->im6_dst = ipv6_hdr(pkt)->daddr;
1192 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1193 skb->ip_summed = CHECKSUM_UNNECESSARY;
1196 if (mrt->mroute6_sk == NULL) {
1202 * Deliver to user space multicast routing algorithms
1204 ret = sock_queue_rcv_skb(mrt->mroute6_sk, skb);
1206 net_warn_ratelimited("mroute6: pending queue full, dropping entries\n");
1214 * Queue a packet for resolution. It gets locked cache entry!
1218 ip6mr_cache_unresolved(struct mr6_table *mrt, mifi_t mifi, struct sk_buff *skb)
1222 struct mfc6_cache *c;
1224 spin_lock_bh(&mfc_unres_lock);
1225 list_for_each_entry(c, &mrt->mfc6_unres_queue, list) {
1226 if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
1227 ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr)) {
1235 * Create a new entry if allowable
1238 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1239 (c = ip6mr_cache_alloc_unres()) == NULL) {
1240 spin_unlock_bh(&mfc_unres_lock);
1247 * Fill in the new cache entry
1249 c->mf6c_parent = -1;
1250 c->mf6c_origin = ipv6_hdr(skb)->saddr;
1251 c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
1254 * Reflect first query at pim6sd
1256 err = ip6mr_cache_report(mrt, skb, mifi, MRT6MSG_NOCACHE);
1258 /* If the report failed throw the cache entry
1261 spin_unlock_bh(&mfc_unres_lock);
1263 ip6mr_cache_free(c);
1268 atomic_inc(&mrt->cache_resolve_queue_len);
1269 list_add(&c->list, &mrt->mfc6_unres_queue);
1270 mr6_netlink_event(mrt, c, RTM_NEWROUTE);
1272 ipmr_do_expire_process(mrt);
1276 * See if we can append the packet
1278 if (c->mfc_un.unres.unresolved.qlen > 3) {
1282 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1286 spin_unlock_bh(&mfc_unres_lock);
1291 * MFC6 cache manipulation by user space
1294 static int ip6mr_mfc_delete(struct mr6_table *mrt, struct mf6cctl *mfc,
1298 struct mfc6_cache *c, *next;
1300 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1302 list_for_each_entry_safe(c, next, &mrt->mfc6_cache_array[line], list) {
1303 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1304 ipv6_addr_equal(&c->mf6c_mcastgrp,
1305 &mfc->mf6cc_mcastgrp.sin6_addr) &&
1306 (parent == -1 || parent == c->mf6c_parent)) {
1307 write_lock_bh(&mrt_lock);
1309 write_unlock_bh(&mrt_lock);
1311 mr6_netlink_event(mrt, c, RTM_DELROUTE);
1312 ip6mr_cache_free(c);
1319 static int ip6mr_device_event(struct notifier_block *this,
1320 unsigned long event, void *ptr)
1322 struct net_device *dev = ptr;
1323 struct net *net = dev_net(dev);
1324 struct mr6_table *mrt;
1325 struct mif_device *v;
1329 if (event != NETDEV_UNREGISTER)
1332 ip6mr_for_each_table(mrt, net) {
1333 v = &mrt->vif6_table[0];
1334 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1336 mif6_delete(mrt, ct, &list);
1339 unregister_netdevice_many(&list);
1344 static struct notifier_block ip6_mr_notifier = {
1345 .notifier_call = ip6mr_device_event
1349 * Setup for IP multicast routing
1352 static int __net_init ip6mr_net_init(struct net *net)
1356 err = ip6mr_rules_init(net);
1360 #ifdef CONFIG_PROC_FS
1362 if (!proc_create("ip6_mr_vif", 0, net->proc_net, &ip6mr_vif_fops))
1364 if (!proc_create("ip6_mr_cache", 0, net->proc_net, &ip6mr_mfc_fops))
1365 goto proc_cache_fail;
1370 #ifdef CONFIG_PROC_FS
1372 proc_net_remove(net, "ip6_mr_vif");
1374 ip6mr_rules_exit(net);
1380 static void __net_exit ip6mr_net_exit(struct net *net)
1382 #ifdef CONFIG_PROC_FS
1383 proc_net_remove(net, "ip6_mr_cache");
1384 proc_net_remove(net, "ip6_mr_vif");
1386 ip6mr_rules_exit(net);
1389 static struct pernet_operations ip6mr_net_ops = {
1390 .init = ip6mr_net_init,
1391 .exit = ip6mr_net_exit,
1394 int __init ip6_mr_init(void)
1398 mrt_cachep = kmem_cache_create("ip6_mrt_cache",
1399 sizeof(struct mfc6_cache),
1400 0, SLAB_HWCACHE_ALIGN,
1405 err = register_pernet_subsys(&ip6mr_net_ops);
1407 goto reg_pernet_fail;
1409 err = register_netdevice_notifier(&ip6_mr_notifier);
1411 goto reg_notif_fail;
1412 #ifdef CONFIG_IPV6_PIMSM_V2
1413 if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) {
1414 pr_err("%s: can't add PIM protocol\n", __func__);
1416 goto add_proto_fail;
1419 rtnl_register(RTNL_FAMILY_IP6MR, RTM_GETROUTE, NULL,
1420 ip6mr_rtm_dumproute, NULL);
1422 #ifdef CONFIG_IPV6_PIMSM_V2
1424 unregister_netdevice_notifier(&ip6_mr_notifier);
1427 unregister_pernet_subsys(&ip6mr_net_ops);
1429 kmem_cache_destroy(mrt_cachep);
1433 void ip6_mr_cleanup(void)
1435 unregister_netdevice_notifier(&ip6_mr_notifier);
1436 unregister_pernet_subsys(&ip6mr_net_ops);
1437 kmem_cache_destroy(mrt_cachep);
1440 static int ip6mr_mfc_add(struct net *net, struct mr6_table *mrt,
1441 struct mf6cctl *mfc, int mrtsock, int parent)
1445 struct mfc6_cache *uc, *c;
1446 unsigned char ttls[MAXMIFS];
1449 if (mfc->mf6cc_parent >= MAXMIFS)
1452 memset(ttls, 255, MAXMIFS);
1453 for (i = 0; i < MAXMIFS; i++) {
1454 if (IF_ISSET(i, &mfc->mf6cc_ifset))
1459 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1461 list_for_each_entry(c, &mrt->mfc6_cache_array[line], list) {
1462 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1463 ipv6_addr_equal(&c->mf6c_mcastgrp,
1464 &mfc->mf6cc_mcastgrp.sin6_addr) &&
1465 (parent == -1 || parent == mfc->mf6cc_parent)) {
1472 write_lock_bh(&mrt_lock);
1473 c->mf6c_parent = mfc->mf6cc_parent;
1474 ip6mr_update_thresholds(mrt, c, ttls);
1476 c->mfc_flags |= MFC_STATIC;
1477 write_unlock_bh(&mrt_lock);
1478 mr6_netlink_event(mrt, c, RTM_NEWROUTE);
1482 if (!ipv6_addr_any(&mfc->mf6cc_mcastgrp.sin6_addr) &&
1483 !ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1486 c = ip6mr_cache_alloc();
1490 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
1491 c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1492 c->mf6c_parent = mfc->mf6cc_parent;
1493 ip6mr_update_thresholds(mrt, c, ttls);
1495 c->mfc_flags |= MFC_STATIC;
1497 write_lock_bh(&mrt_lock);
1498 list_add(&c->list, &mrt->mfc6_cache_array[line]);
1499 write_unlock_bh(&mrt_lock);
1502 * Check to see if we resolved a queued list. If so we
1503 * need to send on the frames and tidy up.
1506 spin_lock_bh(&mfc_unres_lock);
1507 list_for_each_entry(uc, &mrt->mfc6_unres_queue, list) {
1508 if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
1509 ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
1510 list_del(&uc->list);
1511 atomic_dec(&mrt->cache_resolve_queue_len);
1516 if (list_empty(&mrt->mfc6_unres_queue))
1517 del_timer(&mrt->ipmr_expire_timer);
1518 spin_unlock_bh(&mfc_unres_lock);
1521 ip6mr_cache_resolve(net, mrt, uc, c);
1522 ip6mr_cache_free(uc);
1524 mr6_netlink_event(mrt, c, RTM_NEWROUTE);
1529 * Close the multicast socket, and clear the vif tables etc
1532 static void mroute_clean_tables(struct mr6_table *mrt)
1536 struct mfc6_cache *c, *next;
1539 * Shut down all active vif entries
1541 for (i = 0; i < mrt->maxvif; i++) {
1542 if (!(mrt->vif6_table[i].flags & VIFF_STATIC))
1543 mif6_delete(mrt, i, &list);
1545 unregister_netdevice_many(&list);
1550 for (i = 0; i < MFC6_LINES; i++) {
1551 list_for_each_entry_safe(c, next, &mrt->mfc6_cache_array[i], list) {
1552 if (c->mfc_flags & MFC_STATIC)
1554 write_lock_bh(&mrt_lock);
1556 write_unlock_bh(&mrt_lock);
1558 mr6_netlink_event(mrt, c, RTM_DELROUTE);
1559 ip6mr_cache_free(c);
1563 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1564 spin_lock_bh(&mfc_unres_lock);
1565 list_for_each_entry_safe(c, next, &mrt->mfc6_unres_queue, list) {
1567 mr6_netlink_event(mrt, c, RTM_DELROUTE);
1568 ip6mr_destroy_unres(mrt, c);
1570 spin_unlock_bh(&mfc_unres_lock);
1574 static int ip6mr_sk_init(struct mr6_table *mrt, struct sock *sk)
1577 struct net *net = sock_net(sk);
1580 write_lock_bh(&mrt_lock);
1581 if (likely(mrt->mroute6_sk == NULL)) {
1582 mrt->mroute6_sk = sk;
1583 net->ipv6.devconf_all->mc_forwarding++;
1584 inet6_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1585 NETCONFA_IFINDEX_ALL,
1586 net->ipv6.devconf_all);
1590 write_unlock_bh(&mrt_lock);
1597 int ip6mr_sk_done(struct sock *sk)
1600 struct net *net = sock_net(sk);
1601 struct mr6_table *mrt;
1604 ip6mr_for_each_table(mrt, net) {
1605 if (sk == mrt->mroute6_sk) {
1606 write_lock_bh(&mrt_lock);
1607 mrt->mroute6_sk = NULL;
1608 net->ipv6.devconf_all->mc_forwarding--;
1609 inet6_netconf_notify_devconf(net,
1610 NETCONFA_MC_FORWARDING,
1611 NETCONFA_IFINDEX_ALL,
1612 net->ipv6.devconf_all);
1613 write_unlock_bh(&mrt_lock);
1615 mroute_clean_tables(mrt);
1625 struct sock *mroute6_socket(struct net *net, struct sk_buff *skb)
1627 struct mr6_table *mrt;
1628 struct flowi6 fl6 = {
1629 .flowi6_iif = skb->skb_iif,
1630 .flowi6_oif = skb->dev->ifindex,
1631 .flowi6_mark = skb->mark,
1634 if (ip6mr_fib_lookup(net, &fl6, &mrt) < 0)
1637 return mrt->mroute6_sk;
1641 * Socket options and virtual interface manipulation. The whole
1642 * virtual interface system is a complete heap, but unfortunately
1643 * that's how BSD mrouted happens to think. Maybe one day with a proper
1644 * MOSPF/PIM router set up we can clean this up.
1647 int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1649 int ret, parent = 0;
1653 struct net *net = sock_net(sk);
1654 struct mr6_table *mrt;
1656 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1660 if (optname != MRT6_INIT) {
1661 if (sk != mrt->mroute6_sk && !ns_capable(net->user_ns, CAP_NET_ADMIN))
1667 if (sk->sk_type != SOCK_RAW ||
1668 inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1670 if (optlen < sizeof(int))
1673 return ip6mr_sk_init(mrt, sk);
1676 return ip6mr_sk_done(sk);
1679 if (optlen < sizeof(vif))
1681 if (copy_from_user(&vif, optval, sizeof(vif)))
1683 if (vif.mif6c_mifi >= MAXMIFS)
1686 ret = mif6_add(net, mrt, &vif, sk == mrt->mroute6_sk);
1691 if (optlen < sizeof(mifi_t))
1693 if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
1696 ret = mif6_delete(mrt, mifi, NULL);
1701 * Manipulate the forwarding caches. These live
1702 * in a sort of kernel/user symbiosis.
1707 case MRT6_ADD_MFC_PROXY:
1708 case MRT6_DEL_MFC_PROXY:
1709 if (optlen < sizeof(mfc))
1711 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1714 parent = mfc.mf6cc_parent;
1716 if (optname == MRT6_DEL_MFC || optname == MRT6_DEL_MFC_PROXY)
1717 ret = ip6mr_mfc_delete(mrt, &mfc, parent);
1719 ret = ip6mr_mfc_add(net, mrt, &mfc,
1720 sk == mrt->mroute6_sk, parent);
1725 * Control PIM assert (to activate pim will activate assert)
1731 if (optlen != sizeof(v))
1733 if (get_user(v, (int __user *)optval))
1735 mrt->mroute_do_assert = v;
1739 #ifdef CONFIG_IPV6_PIMSM_V2
1744 if (optlen != sizeof(v))
1746 if (get_user(v, (int __user *)optval))
1751 if (v != mrt->mroute_do_pim) {
1752 mrt->mroute_do_pim = v;
1753 mrt->mroute_do_assert = v;
1760 #ifdef CONFIG_IPV6_MROUTE_MULTIPLE_TABLES
1765 if (optlen != sizeof(u32))
1767 if (get_user(v, (u32 __user *)optval))
1769 /* "pim6reg%u" should not exceed 16 bytes (IFNAMSIZ) */
1770 if (v != RT_TABLE_DEFAULT && v >= 100000000)
1772 if (sk == mrt->mroute6_sk)
1777 if (!ip6mr_new_table(net, v))
1779 raw6_sk(sk)->ip6mr_table = v;
1785 * Spurious command, or MRT6_VERSION which you cannot
1789 return -ENOPROTOOPT;
1794 * Getsock opt support for the multicast routing system.
1797 int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
1802 struct net *net = sock_net(sk);
1803 struct mr6_table *mrt;
1805 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1813 #ifdef CONFIG_IPV6_PIMSM_V2
1815 val = mrt->mroute_do_pim;
1819 val = mrt->mroute_do_assert;
1822 return -ENOPROTOOPT;
1825 if (get_user(olr, optlen))
1828 olr = min_t(int, olr, sizeof(int));
1832 if (put_user(olr, optlen))
1834 if (copy_to_user(optval, &val, olr))
1840 * The IP multicast ioctl support routines.
1843 int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
1845 struct sioc_sg_req6 sr;
1846 struct sioc_mif_req6 vr;
1847 struct mif_device *vif;
1848 struct mfc6_cache *c;
1849 struct net *net = sock_net(sk);
1850 struct mr6_table *mrt;
1852 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1857 case SIOCGETMIFCNT_IN6:
1858 if (copy_from_user(&vr, arg, sizeof(vr)))
1860 if (vr.mifi >= mrt->maxvif)
1862 read_lock(&mrt_lock);
1863 vif = &mrt->vif6_table[vr.mifi];
1864 if (MIF_EXISTS(mrt, vr.mifi)) {
1865 vr.icount = vif->pkt_in;
1866 vr.ocount = vif->pkt_out;
1867 vr.ibytes = vif->bytes_in;
1868 vr.obytes = vif->bytes_out;
1869 read_unlock(&mrt_lock);
1871 if (copy_to_user(arg, &vr, sizeof(vr)))
1875 read_unlock(&mrt_lock);
1876 return -EADDRNOTAVAIL;
1877 case SIOCGETSGCNT_IN6:
1878 if (copy_from_user(&sr, arg, sizeof(sr)))
1881 read_lock(&mrt_lock);
1882 c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr);
1884 sr.pktcnt = c->mfc_un.res.pkt;
1885 sr.bytecnt = c->mfc_un.res.bytes;
1886 sr.wrong_if = c->mfc_un.res.wrong_if;
1887 read_unlock(&mrt_lock);
1889 if (copy_to_user(arg, &sr, sizeof(sr)))
1893 read_unlock(&mrt_lock);
1894 return -EADDRNOTAVAIL;
1896 return -ENOIOCTLCMD;
1900 #ifdef CONFIG_COMPAT
1901 struct compat_sioc_sg_req6 {
1902 struct sockaddr_in6 src;
1903 struct sockaddr_in6 grp;
1904 compat_ulong_t pktcnt;
1905 compat_ulong_t bytecnt;
1906 compat_ulong_t wrong_if;
1909 struct compat_sioc_mif_req6 {
1911 compat_ulong_t icount;
1912 compat_ulong_t ocount;
1913 compat_ulong_t ibytes;
1914 compat_ulong_t obytes;
1917 int ip6mr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1919 struct compat_sioc_sg_req6 sr;
1920 struct compat_sioc_mif_req6 vr;
1921 struct mif_device *vif;
1922 struct mfc6_cache *c;
1923 struct net *net = sock_net(sk);
1924 struct mr6_table *mrt;
1926 mrt = ip6mr_get_table(net, raw6_sk(sk)->ip6mr_table ? : RT6_TABLE_DFLT);
1931 case SIOCGETMIFCNT_IN6:
1932 if (copy_from_user(&vr, arg, sizeof(vr)))
1934 if (vr.mifi >= mrt->maxvif)
1936 read_lock(&mrt_lock);
1937 vif = &mrt->vif6_table[vr.mifi];
1938 if (MIF_EXISTS(mrt, vr.mifi)) {
1939 vr.icount = vif->pkt_in;
1940 vr.ocount = vif->pkt_out;
1941 vr.ibytes = vif->bytes_in;
1942 vr.obytes = vif->bytes_out;
1943 read_unlock(&mrt_lock);
1945 if (copy_to_user(arg, &vr, sizeof(vr)))
1949 read_unlock(&mrt_lock);
1950 return -EADDRNOTAVAIL;
1951 case SIOCGETSGCNT_IN6:
1952 if (copy_from_user(&sr, arg, sizeof(sr)))
1955 read_lock(&mrt_lock);
1956 c = ip6mr_cache_find(mrt, &sr.src.sin6_addr, &sr.grp.sin6_addr);
1958 sr.pktcnt = c->mfc_un.res.pkt;
1959 sr.bytecnt = c->mfc_un.res.bytes;
1960 sr.wrong_if = c->mfc_un.res.wrong_if;
1961 read_unlock(&mrt_lock);
1963 if (copy_to_user(arg, &sr, sizeof(sr)))
1967 read_unlock(&mrt_lock);
1968 return -EADDRNOTAVAIL;
1970 return -ENOIOCTLCMD;
1975 static inline int ip6mr_forward2_finish(struct sk_buff *skb)
1977 IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
1978 IPSTATS_MIB_OUTFORWDATAGRAMS);
1979 IP6_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
1980 IPSTATS_MIB_OUTOCTETS, skb->len);
1981 return dst_output(skb);
1985 * Processing handlers for ip6mr_forward
1988 static int ip6mr_forward2(struct net *net, struct mr6_table *mrt,
1989 struct sk_buff *skb, struct mfc6_cache *c, int vifi)
1991 struct ipv6hdr *ipv6h;
1992 struct mif_device *vif = &mrt->vif6_table[vifi];
1993 struct net_device *dev;
1994 struct dst_entry *dst;
1997 if (vif->dev == NULL)
2000 #ifdef CONFIG_IPV6_PIMSM_V2
2001 if (vif->flags & MIFF_REGISTER) {
2003 vif->bytes_out += skb->len;
2004 vif->dev->stats.tx_bytes += skb->len;
2005 vif->dev->stats.tx_packets++;
2006 ip6mr_cache_report(mrt, skb, vifi, MRT6MSG_WHOLEPKT);
2011 ipv6h = ipv6_hdr(skb);
2013 fl6 = (struct flowi6) {
2014 .flowi6_oif = vif->link,
2015 .daddr = ipv6h->daddr,
2018 dst = ip6_route_output(net, NULL, &fl6);
2025 skb_dst_set(skb, dst);
2028 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
2029 * not only before forwarding, but after forwarding on all output
2030 * interfaces. It is clear, if mrouter runs a multicasting
2031 * program, it should receive packets not depending to what interface
2032 * program is joined.
2033 * If we will not make it, the program will have to join on all
2034 * interfaces. On the other hand, multihoming host (or router, but
2035 * not mrouter) cannot join to more than one interface - it will
2036 * result in receiving multiple packets.
2041 vif->bytes_out += skb->len;
2043 /* We are about to write */
2044 /* XXX: extension headers? */
2045 if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
2048 ipv6h = ipv6_hdr(skb);
2051 IP6CB(skb)->flags |= IP6SKB_FORWARDED;
2053 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dev,
2054 ip6mr_forward2_finish);
2061 static int ip6mr_find_vif(struct mr6_table *mrt, struct net_device *dev)
2065 for (ct = mrt->maxvif - 1; ct >= 0; ct--) {
2066 if (mrt->vif6_table[ct].dev == dev)
2072 static int ip6_mr_forward(struct net *net, struct mr6_table *mrt,
2073 struct sk_buff *skb, struct mfc6_cache *cache)
2077 int true_vifi = ip6mr_find_vif(mrt, skb->dev);
2079 vif = cache->mf6c_parent;
2080 cache->mfc_un.res.pkt++;
2081 cache->mfc_un.res.bytes += skb->len;
2083 if (ipv6_addr_any(&cache->mf6c_origin) && true_vifi >= 0) {
2084 struct mfc6_cache *cache_proxy;
2086 /* For an (*,G) entry, we only check that the incomming
2087 * interface is part of the static tree.
2089 cache_proxy = ip6mr_cache_find_any_parent(mrt, vif);
2091 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
2096 * Wrong interface: drop packet and (maybe) send PIM assert.
2098 if (mrt->vif6_table[vif].dev != skb->dev) {
2099 cache->mfc_un.res.wrong_if++;
2101 if (true_vifi >= 0 && mrt->mroute_do_assert &&
2102 /* pimsm uses asserts, when switching from RPT to SPT,
2103 so that we cannot check that packet arrived on an oif.
2104 It is bad, but otherwise we would need to move pretty
2105 large chunk of pimd to kernel. Ough... --ANK
2107 (mrt->mroute_do_pim ||
2108 cache->mfc_un.res.ttls[true_vifi] < 255) &&
2110 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
2111 cache->mfc_un.res.last_assert = jiffies;
2112 ip6mr_cache_report(mrt, skb, true_vifi, MRT6MSG_WRONGMIF);
2118 mrt->vif6_table[vif].pkt_in++;
2119 mrt->vif6_table[vif].bytes_in += skb->len;
2124 if (ipv6_addr_any(&cache->mf6c_origin) &&
2125 ipv6_addr_any(&cache->mf6c_mcastgrp)) {
2126 if (true_vifi >= 0 &&
2127 true_vifi != cache->mf6c_parent &&
2128 ipv6_hdr(skb)->hop_limit >
2129 cache->mfc_un.res.ttls[cache->mf6c_parent]) {
2130 /* It's an (*,*) entry and the packet is not coming from
2131 * the upstream: forward the packet to the upstream
2134 psend = cache->mf6c_parent;
2139 for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
2140 /* For (*,G) entry, don't forward to the incoming interface */
2141 if ((!ipv6_addr_any(&cache->mf6c_origin) || ct != true_vifi) &&
2142 ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
2144 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2146 ip6mr_forward2(net, mrt, skb2, cache, psend);
2153 ip6mr_forward2(net, mrt, skb, cache, psend);
2164 * Multicast packets for forwarding arrive here
2167 int ip6_mr_input(struct sk_buff *skb)
2169 struct mfc6_cache *cache;
2170 struct net *net = dev_net(skb->dev);
2171 struct mr6_table *mrt;
2172 struct flowi6 fl6 = {
2173 .flowi6_iif = skb->dev->ifindex,
2174 .flowi6_mark = skb->mark,
2178 err = ip6mr_fib_lookup(net, &fl6, &mrt);
2184 read_lock(&mrt_lock);
2185 cache = ip6mr_cache_find(mrt,
2186 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
2187 if (cache == NULL) {
2188 int vif = ip6mr_find_vif(mrt, skb->dev);
2191 cache = ip6mr_cache_find_any(mrt,
2192 &ipv6_hdr(skb)->daddr,
2197 * No usable cache entry
2199 if (cache == NULL) {
2202 vif = ip6mr_find_vif(mrt, skb->dev);
2204 int err = ip6mr_cache_unresolved(mrt, vif, skb);
2205 read_unlock(&mrt_lock);
2209 read_unlock(&mrt_lock);
2214 ip6_mr_forward(net, mrt, skb, cache);
2216 read_unlock(&mrt_lock);
2222 static int __ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
2223 struct mfc6_cache *c, struct rtmsg *rtm)
2226 struct rtnexthop *nhp;
2227 struct nlattr *mp_attr;
2228 struct rta_mfc_stats mfcs;
2230 /* If cache is unresolved, don't try to parse IIF and OIF */
2231 if (c->mf6c_parent >= MAXMIFS)
2234 if (MIF_EXISTS(mrt, c->mf6c_parent) &&
2235 nla_put_u32(skb, RTA_IIF, mrt->vif6_table[c->mf6c_parent].dev->ifindex) < 0)
2237 mp_attr = nla_nest_start(skb, RTA_MULTIPATH);
2238 if (mp_attr == NULL)
2241 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2242 if (MIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2243 nhp = nla_reserve_nohdr(skb, sizeof(*nhp));
2245 nla_nest_cancel(skb, mp_attr);
2249 nhp->rtnh_flags = 0;
2250 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2251 nhp->rtnh_ifindex = mrt->vif6_table[ct].dev->ifindex;
2252 nhp->rtnh_len = sizeof(*nhp);
2256 nla_nest_end(skb, mp_attr);
2258 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2259 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2260 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2261 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2264 rtm->rtm_type = RTN_MULTICAST;
2268 int ip6mr_get_route(struct net *net,
2269 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
2272 struct mr6_table *mrt;
2273 struct mfc6_cache *cache;
2274 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
2276 mrt = ip6mr_get_table(net, RT6_TABLE_DFLT);
2280 read_lock(&mrt_lock);
2281 cache = ip6mr_cache_find(mrt, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);
2282 if (!cache && skb->dev) {
2283 int vif = ip6mr_find_vif(mrt, skb->dev);
2286 cache = ip6mr_cache_find_any(mrt, &rt->rt6i_dst.addr,
2291 struct sk_buff *skb2;
2292 struct ipv6hdr *iph;
2293 struct net_device *dev;
2297 read_unlock(&mrt_lock);
2302 if (dev == NULL || (vif = ip6mr_find_vif(mrt, dev)) < 0) {
2303 read_unlock(&mrt_lock);
2307 /* really correct? */
2308 skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
2310 read_unlock(&mrt_lock);
2314 skb_reset_transport_header(skb2);
2316 skb_put(skb2, sizeof(struct ipv6hdr));
2317 skb_reset_network_header(skb2);
2319 iph = ipv6_hdr(skb2);
2322 iph->flow_lbl[0] = 0;
2323 iph->flow_lbl[1] = 0;
2324 iph->flow_lbl[2] = 0;
2325 iph->payload_len = 0;
2326 iph->nexthdr = IPPROTO_NONE;
2328 iph->saddr = rt->rt6i_src.addr;
2329 iph->daddr = rt->rt6i_dst.addr;
2331 err = ip6mr_cache_unresolved(mrt, vif, skb2);
2332 read_unlock(&mrt_lock);
2337 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
2338 cache->mfc_flags |= MFC_NOTIFY;
2340 err = __ip6mr_fill_mroute(mrt, skb, cache, rtm);
2341 read_unlock(&mrt_lock);
2345 static int ip6mr_fill_mroute(struct mr6_table *mrt, struct sk_buff *skb,
2346 u32 portid, u32 seq, struct mfc6_cache *c, int cmd)
2348 struct nlmsghdr *nlh;
2352 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), NLM_F_MULTI);
2356 rtm = nlmsg_data(nlh);
2357 rtm->rtm_family = RTNL_FAMILY_IP6MR;
2358 rtm->rtm_dst_len = 128;
2359 rtm->rtm_src_len = 128;
2361 rtm->rtm_table = mrt->id;
2362 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2363 goto nla_put_failure;
2364 rtm->rtm_type = RTN_MULTICAST;
2365 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2366 if (c->mfc_flags & MFC_STATIC)
2367 rtm->rtm_protocol = RTPROT_STATIC;
2369 rtm->rtm_protocol = RTPROT_MROUTED;
2372 if (nla_put(skb, RTA_SRC, 16, &c->mf6c_origin) ||
2373 nla_put(skb, RTA_DST, 16, &c->mf6c_mcastgrp))
2374 goto nla_put_failure;
2375 err = __ip6mr_fill_mroute(mrt, skb, c, rtm);
2376 /* do not break the dump if cache is unresolved */
2377 if (err < 0 && err != -ENOENT)
2378 goto nla_put_failure;
2380 return nlmsg_end(skb, nlh);
2383 nlmsg_cancel(skb, nlh);
2387 static int mr6_msgsize(bool unresolved, int maxvif)
2390 NLMSG_ALIGN(sizeof(struct rtmsg))
2391 + nla_total_size(4) /* RTA_TABLE */
2392 + nla_total_size(sizeof(struct in6_addr)) /* RTA_SRC */
2393 + nla_total_size(sizeof(struct in6_addr)) /* RTA_DST */
2398 + nla_total_size(4) /* RTA_IIF */
2399 + nla_total_size(0) /* RTA_MULTIPATH */
2400 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2402 + nla_total_size(sizeof(struct rta_mfc_stats))
2408 static void mr6_netlink_event(struct mr6_table *mrt, struct mfc6_cache *mfc,
2411 struct net *net = read_pnet(&mrt->net);
2412 struct sk_buff *skb;
2415 skb = nlmsg_new(mr6_msgsize(mfc->mf6c_parent >= MAXMIFS, mrt->maxvif),
2420 err = ip6mr_fill_mroute(mrt, skb, 0, 0, mfc, cmd);
2424 rtnl_notify(skb, net, 0, RTNLGRP_IPV6_MROUTE, NULL, GFP_ATOMIC);
2430 rtnl_set_sk_err(net, RTNLGRP_IPV6_MROUTE, err);
2433 static int ip6mr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2435 struct net *net = sock_net(skb->sk);
2436 struct mr6_table *mrt;
2437 struct mfc6_cache *mfc;
2438 unsigned int t = 0, s_t;
2439 unsigned int h = 0, s_h;
2440 unsigned int e = 0, s_e;
2446 read_lock(&mrt_lock);
2447 ip6mr_for_each_table(mrt, net) {
2452 for (h = s_h; h < MFC6_LINES; h++) {
2453 list_for_each_entry(mfc, &mrt->mfc6_cache_array[h], list) {
2456 if (ip6mr_fill_mroute(mrt, skb,
2457 NETLINK_CB(cb->skb).portid,
2459 mfc, RTM_NEWROUTE) < 0)
2466 spin_lock_bh(&mfc_unres_lock);
2467 list_for_each_entry(mfc, &mrt->mfc6_unres_queue, list) {
2470 if (ip6mr_fill_mroute(mrt, skb,
2471 NETLINK_CB(cb->skb).portid,
2473 mfc, RTM_NEWROUTE) < 0) {
2474 spin_unlock_bh(&mfc_unres_lock);
2480 spin_unlock_bh(&mfc_unres_lock);
2487 read_unlock(&mrt_lock);