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/system.h>
20 #include <asm/uaccess.h>
21 #include <linux/types.h>
22 #include <linux/sched.h>
23 #include <linux/errno.h>
24 #include <linux/timer.h>
26 #include <linux/kernel.h>
27 #include <linux/fcntl.h>
28 #include <linux/stat.h>
29 #include <linux/socket.h>
30 #include <linux/inet.h>
31 #include <linux/netdevice.h>
32 #include <linux/inetdevice.h>
33 #include <linux/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <linux/init.h>
36 #include <linux/slab.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>
47 #include <net/ip6_route.h>
48 #include <linux/mroute6.h>
49 #include <linux/pim.h>
50 #include <net/addrconf.h>
51 #include <linux/netfilter_ipv6.h>
52 #include <net/ip6_checksum.h>
54 /* Big lock, protecting vif table, mrt cache and mroute socket state.
55 Note that the changes are semaphored via rtnl_lock.
58 static DEFINE_RWLOCK(mrt_lock);
61 * Multicast router control variables
64 #define MIF_EXISTS(_net, _idx) ((_net)->ipv6.vif6_table[_idx].dev != NULL)
66 /* Special spinlock for queue of unresolved entries */
67 static DEFINE_SPINLOCK(mfc_unres_lock);
69 /* We return to original Alan's scheme. Hash table of resolved
70 entries is changed only in process context and protected
71 with weak lock mrt_lock. Queue of unresolved entries is protected
72 with strong spinlock mfc_unres_lock.
74 In this case data path is free of exclusive locks at all.
77 static struct kmem_cache *mrt_cachep __read_mostly;
79 static int ip6_mr_forward(struct net *net, struct sk_buff *skb,
80 struct mfc6_cache *cache);
81 static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt,
82 mifi_t mifi, int assert);
83 static int ip6mr_fill_mroute(struct net *net, struct sk_buff *skb,
84 struct mfc6_cache *c, struct rtmsg *rtm);
85 static void mroute_clean_tables(struct net *net);
90 struct ipmr_mfc_iter {
91 struct seq_net_private p;
92 struct list_head *cache;
97 static struct mfc6_cache *ipmr_mfc_seq_idx(struct net *net,
98 struct ipmr_mfc_iter *it, loff_t pos)
100 struct mfc6_cache *mfc;
102 read_lock(&mrt_lock);
103 for (it->ct = 0; it->ct < MFC6_LINES; it->ct++) {
104 it->cache = &net->ipv6.mfc6_cache_array[it->ct];
105 list_for_each_entry(mfc, it->cache, list)
109 read_unlock(&mrt_lock);
111 spin_lock_bh(&mfc_unres_lock);
112 it->cache = &net->ipv6.mfc6_unres_queue;
113 list_for_each_entry(mfc, it->cache, list)
116 spin_unlock_bh(&mfc_unres_lock);
123 * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
126 struct ipmr_vif_iter {
127 struct seq_net_private p;
131 static struct mif_device *ip6mr_vif_seq_idx(struct net *net,
132 struct ipmr_vif_iter *iter,
135 for (iter->ct = 0; iter->ct < net->ipv6.maxvif; ++iter->ct) {
136 if (!MIF_EXISTS(net, iter->ct))
139 return &net->ipv6.vif6_table[iter->ct];
144 static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
147 struct net *net = seq_file_net(seq);
149 read_lock(&mrt_lock);
150 return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1)
154 static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
156 struct ipmr_vif_iter *iter = seq->private;
157 struct net *net = seq_file_net(seq);
160 if (v == SEQ_START_TOKEN)
161 return ip6mr_vif_seq_idx(net, iter, 0);
163 while (++iter->ct < net->ipv6.maxvif) {
164 if (!MIF_EXISTS(net, iter->ct))
166 return &net->ipv6.vif6_table[iter->ct];
171 static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
174 read_unlock(&mrt_lock);
177 static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
179 struct net *net = seq_file_net(seq);
181 if (v == SEQ_START_TOKEN) {
183 "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
185 const struct mif_device *vif = v;
186 const char *name = vif->dev ? vif->dev->name : "none";
189 "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
190 vif - net->ipv6.vif6_table,
191 name, vif->bytes_in, vif->pkt_in,
192 vif->bytes_out, vif->pkt_out,
198 static const struct seq_operations ip6mr_vif_seq_ops = {
199 .start = ip6mr_vif_seq_start,
200 .next = ip6mr_vif_seq_next,
201 .stop = ip6mr_vif_seq_stop,
202 .show = ip6mr_vif_seq_show,
205 static int ip6mr_vif_open(struct inode *inode, struct file *file)
207 return seq_open_net(inode, file, &ip6mr_vif_seq_ops,
208 sizeof(struct ipmr_vif_iter));
211 static const struct file_operations ip6mr_vif_fops = {
212 .owner = THIS_MODULE,
213 .open = ip6mr_vif_open,
216 .release = seq_release_net,
219 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
221 struct net *net = seq_file_net(seq);
223 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
227 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
229 struct mfc6_cache *mfc = v;
230 struct ipmr_mfc_iter *it = seq->private;
231 struct net *net = seq_file_net(seq);
235 if (v == SEQ_START_TOKEN)
236 return ipmr_mfc_seq_idx(net, seq->private, 0);
238 if (mfc->list.next != it->cache)
239 return list_entry(mfc->list.next, struct mfc6_cache, list);
241 if (it->cache == &net->ipv6.mfc6_unres_queue)
244 BUG_ON(it->cache != &net->ipv6.mfc6_cache_array[it->ct]);
246 while (++it->ct < MFC6_LINES) {
247 it->cache = &net->ipv6.mfc6_cache_array[it->ct];
248 if (list_empty(it->cache))
250 return list_first_entry(it->cache, struct mfc6_cache, list);
253 /* exhausted cache_array, show unresolved */
254 read_unlock(&mrt_lock);
255 it->cache = &net->ipv6.mfc6_unres_queue;
258 spin_lock_bh(&mfc_unres_lock);
259 if (!list_empty(it->cache))
260 return list_first_entry(it->cache, struct mfc6_cache, list);
263 spin_unlock_bh(&mfc_unres_lock);
269 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
271 struct ipmr_mfc_iter *it = seq->private;
272 struct net *net = seq_file_net(seq);
274 if (it->cache == &net->ipv6.mfc6_unres_queue)
275 spin_unlock_bh(&mfc_unres_lock);
276 else if (it->cache == net->ipv6.mfc6_cache_array)
277 read_unlock(&mrt_lock);
280 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
283 struct net *net = seq_file_net(seq);
285 if (v == SEQ_START_TOKEN) {
289 "Iif Pkts Bytes Wrong Oifs\n");
291 const struct mfc6_cache *mfc = v;
292 const struct ipmr_mfc_iter *it = seq->private;
294 seq_printf(seq, "%pI6 %pI6 %-3hd",
295 &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
298 if (it->cache != &net->ipv6.mfc6_unres_queue) {
299 seq_printf(seq, " %8lu %8lu %8lu",
301 mfc->mfc_un.res.bytes,
302 mfc->mfc_un.res.wrong_if);
303 for (n = mfc->mfc_un.res.minvif;
304 n < mfc->mfc_un.res.maxvif; n++) {
305 if (MIF_EXISTS(net, n) &&
306 mfc->mfc_un.res.ttls[n] < 255)
309 n, mfc->mfc_un.res.ttls[n]);
312 /* unresolved mfc_caches don't contain
313 * pkt, bytes and wrong_if values
315 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
322 static const struct seq_operations ipmr_mfc_seq_ops = {
323 .start = ipmr_mfc_seq_start,
324 .next = ipmr_mfc_seq_next,
325 .stop = ipmr_mfc_seq_stop,
326 .show = ipmr_mfc_seq_show,
329 static int ipmr_mfc_open(struct inode *inode, struct file *file)
331 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
332 sizeof(struct ipmr_mfc_iter));
335 static const struct file_operations ip6mr_mfc_fops = {
336 .owner = THIS_MODULE,
337 .open = ipmr_mfc_open,
340 .release = seq_release_net,
344 #ifdef CONFIG_IPV6_PIMSM_V2
346 static int pim6_rcv(struct sk_buff *skb)
348 struct pimreghdr *pim;
349 struct ipv6hdr *encap;
350 struct net_device *reg_dev = NULL;
351 struct net *net = dev_net(skb->dev);
352 int reg_vif_num = net->ipv6.mroute_reg_vif_num;
354 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
357 pim = (struct pimreghdr *)skb_transport_header(skb);
358 if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
359 (pim->flags & PIM_NULL_REGISTER) ||
360 (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
361 sizeof(*pim), IPPROTO_PIM,
362 csum_partial((void *)pim, sizeof(*pim), 0)) &&
363 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
366 /* check if the inner packet is destined to mcast group */
367 encap = (struct ipv6hdr *)(skb_transport_header(skb) +
370 if (!ipv6_addr_is_multicast(&encap->daddr) ||
371 encap->payload_len == 0 ||
372 ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
375 read_lock(&mrt_lock);
376 if (reg_vif_num >= 0)
377 reg_dev = net->ipv6.vif6_table[reg_vif_num].dev;
380 read_unlock(&mrt_lock);
385 skb->mac_header = skb->network_header;
386 skb_pull(skb, (u8 *)encap - skb->data);
387 skb_reset_network_header(skb);
389 skb->protocol = htons(ETH_P_IPV6);
391 skb->pkt_type = PACKET_HOST;
393 reg_dev->stats.rx_bytes += skb->len;
394 reg_dev->stats.rx_packets++;
404 static const struct inet6_protocol pim6_protocol = {
408 /* Service routines creating virtual interfaces: PIMREG */
410 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb,
411 struct net_device *dev)
413 struct net *net = dev_net(dev);
415 read_lock(&mrt_lock);
416 dev->stats.tx_bytes += skb->len;
417 dev->stats.tx_packets++;
418 ip6mr_cache_report(net, skb, net->ipv6.mroute_reg_vif_num,
420 read_unlock(&mrt_lock);
425 static const struct net_device_ops reg_vif_netdev_ops = {
426 .ndo_start_xmit = reg_vif_xmit,
429 static void reg_vif_setup(struct net_device *dev)
431 dev->type = ARPHRD_PIMREG;
432 dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
433 dev->flags = IFF_NOARP;
434 dev->netdev_ops = ®_vif_netdev_ops;
435 dev->destructor = free_netdev;
436 dev->features |= NETIF_F_NETNS_LOCAL;
439 static struct net_device *ip6mr_reg_vif(struct net *net)
441 struct net_device *dev;
443 dev = alloc_netdev(0, "pim6reg", reg_vif_setup);
447 dev_net_set(dev, net);
449 if (register_netdevice(dev)) {
462 /* allow the register to be completed before unregistering. */
466 unregister_netdevice(dev);
475 static int mif6_delete(struct net *net, int vifi, struct list_head *head)
477 struct mif_device *v;
478 struct net_device *dev;
479 struct inet6_dev *in6_dev;
480 if (vifi < 0 || vifi >= net->ipv6.maxvif)
481 return -EADDRNOTAVAIL;
483 v = &net->ipv6.vif6_table[vifi];
485 write_lock_bh(&mrt_lock);
490 write_unlock_bh(&mrt_lock);
491 return -EADDRNOTAVAIL;
494 #ifdef CONFIG_IPV6_PIMSM_V2
495 if (vifi == net->ipv6.mroute_reg_vif_num)
496 net->ipv6.mroute_reg_vif_num = -1;
499 if (vifi + 1 == net->ipv6.maxvif) {
501 for (tmp = vifi - 1; tmp >= 0; tmp--) {
502 if (MIF_EXISTS(net, tmp))
505 net->ipv6.maxvif = tmp + 1;
508 write_unlock_bh(&mrt_lock);
510 dev_set_allmulti(dev, -1);
512 in6_dev = __in6_dev_get(dev);
514 in6_dev->cnf.mc_forwarding--;
516 if (v->flags & MIFF_REGISTER)
517 unregister_netdevice_queue(dev, head);
523 static inline void ip6mr_cache_free(struct mfc6_cache *c)
525 kmem_cache_free(mrt_cachep, c);
528 /* Destroy an unresolved cache entry, killing queued skbs
529 and reporting error to netlink readers.
532 static void ip6mr_destroy_unres(struct net *net, struct mfc6_cache *c)
536 atomic_dec(&net->ipv6.cache_resolve_queue_len);
538 while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
539 if (ipv6_hdr(skb)->version == 0) {
540 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
541 nlh->nlmsg_type = NLMSG_ERROR;
542 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
543 skb_trim(skb, nlh->nlmsg_len);
544 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
545 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
554 /* Timer process for all the unresolved queue. */
556 static void ipmr_do_expire_process(struct net *net)
558 unsigned long now = jiffies;
559 unsigned long expires = 10 * HZ;
560 struct mfc6_cache *c, *next;
562 list_for_each_entry_safe(c, next, &net->ipv6.mfc6_unres_queue, list) {
563 if (time_after(c->mfc_un.unres.expires, now)) {
565 unsigned long interval = c->mfc_un.unres.expires - now;
566 if (interval < expires)
572 ip6mr_destroy_unres(net, c);
575 if (!list_empty(&net->ipv6.mfc6_unres_queue))
576 mod_timer(&net->ipv6.ipmr_expire_timer, jiffies + expires);
579 static void ipmr_expire_process(unsigned long arg)
581 struct net *net = (struct net *)arg;
583 if (!spin_trylock(&mfc_unres_lock)) {
584 mod_timer(&net->ipv6.ipmr_expire_timer, jiffies + 1);
588 if (!list_empty(&net->ipv6.mfc6_unres_queue))
589 ipmr_do_expire_process(net);
591 spin_unlock(&mfc_unres_lock);
594 /* Fill oifs list. It is called under write locked mrt_lock. */
596 static void ip6mr_update_thresholds(struct net *net, struct mfc6_cache *cache,
601 cache->mfc_un.res.minvif = MAXMIFS;
602 cache->mfc_un.res.maxvif = 0;
603 memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
605 for (vifi = 0; vifi < net->ipv6.maxvif; vifi++) {
606 if (MIF_EXISTS(net, vifi) &&
607 ttls[vifi] && ttls[vifi] < 255) {
608 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
609 if (cache->mfc_un.res.minvif > vifi)
610 cache->mfc_un.res.minvif = vifi;
611 if (cache->mfc_un.res.maxvif <= vifi)
612 cache->mfc_un.res.maxvif = vifi + 1;
617 static int mif6_add(struct net *net, struct mif6ctl *vifc, int mrtsock)
619 int vifi = vifc->mif6c_mifi;
620 struct mif_device *v = &net->ipv6.vif6_table[vifi];
621 struct net_device *dev;
622 struct inet6_dev *in6_dev;
626 if (MIF_EXISTS(net, vifi))
629 switch (vifc->mif6c_flags) {
630 #ifdef CONFIG_IPV6_PIMSM_V2
633 * Special Purpose VIF in PIM
634 * All the packets will be sent to the daemon
636 if (net->ipv6.mroute_reg_vif_num >= 0)
638 dev = ip6mr_reg_vif(net);
641 err = dev_set_allmulti(dev, 1);
643 unregister_netdevice(dev);
650 dev = dev_get_by_index(net, vifc->mif6c_pifi);
652 return -EADDRNOTAVAIL;
653 err = dev_set_allmulti(dev, 1);
663 in6_dev = __in6_dev_get(dev);
665 in6_dev->cnf.mc_forwarding++;
668 * Fill in the VIF structures
670 v->rate_limit = vifc->vifc_rate_limit;
671 v->flags = vifc->mif6c_flags;
673 v->flags |= VIFF_STATIC;
674 v->threshold = vifc->vifc_threshold;
679 v->link = dev->ifindex;
680 if (v->flags & MIFF_REGISTER)
681 v->link = dev->iflink;
683 /* And finish update writing critical data */
684 write_lock_bh(&mrt_lock);
686 #ifdef CONFIG_IPV6_PIMSM_V2
687 if (v->flags & MIFF_REGISTER)
688 net->ipv6.mroute_reg_vif_num = vifi;
690 if (vifi + 1 > net->ipv6.maxvif)
691 net->ipv6.maxvif = vifi + 1;
692 write_unlock_bh(&mrt_lock);
696 static struct mfc6_cache *ip6mr_cache_find(struct net *net,
697 struct in6_addr *origin,
698 struct in6_addr *mcastgrp)
700 int line = MFC6_HASH(mcastgrp, origin);
701 struct mfc6_cache *c;
703 list_for_each_entry(c, &net->ipv6.mfc6_cache_array[line], list) {
704 if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
705 ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
712 * Allocate a multicast cache entry
714 static struct mfc6_cache *ip6mr_cache_alloc(void)
716 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
719 c->mfc_un.res.minvif = MAXMIFS;
723 static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
725 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
728 skb_queue_head_init(&c->mfc_un.unres.unresolved);
729 c->mfc_un.unres.expires = jiffies + 10 * HZ;
734 * A cache entry has gone into a resolved state from queued
737 static void ip6mr_cache_resolve(struct net *net, struct mfc6_cache *uc,
738 struct mfc6_cache *c)
743 * Play the pending entries through our router
746 while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
747 if (ipv6_hdr(skb)->version == 0) {
749 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
751 if (ip6mr_fill_mroute(net, skb, c, NLMSG_DATA(nlh)) > 0) {
752 nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
754 nlh->nlmsg_type = NLMSG_ERROR;
755 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
756 skb_trim(skb, nlh->nlmsg_len);
757 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
759 err = rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
761 ip6_mr_forward(net, skb, c);
766 * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
767 * expects the following bizarre scheme.
769 * Called under mrt_lock.
772 static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt, mifi_t mifi,
779 #ifdef CONFIG_IPV6_PIMSM_V2
780 if (assert == MRT6MSG_WHOLEPKT)
781 skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
785 skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
790 /* I suppose that internal messages
791 * do not require checksums */
793 skb->ip_summed = CHECKSUM_UNNECESSARY;
795 #ifdef CONFIG_IPV6_PIMSM_V2
796 if (assert == MRT6MSG_WHOLEPKT) {
797 /* Ugly, but we have no choice with this interface.
798 Duplicate old header, fix length etc.
799 And all this only to mangle msg->im6_msgtype and
800 to set msg->im6_mbz to "mbz" :-)
802 skb_push(skb, -skb_network_offset(pkt));
804 skb_push(skb, sizeof(*msg));
805 skb_reset_transport_header(skb);
806 msg = (struct mrt6msg *)skb_transport_header(skb);
808 msg->im6_msgtype = MRT6MSG_WHOLEPKT;
809 msg->im6_mif = net->ipv6.mroute_reg_vif_num;
811 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
812 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
814 skb->ip_summed = CHECKSUM_UNNECESSARY;
822 skb_put(skb, sizeof(struct ipv6hdr));
823 skb_reset_network_header(skb);
824 skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
829 skb_put(skb, sizeof(*msg));
830 skb_reset_transport_header(skb);
831 msg = (struct mrt6msg *)skb_transport_header(skb);
834 msg->im6_msgtype = assert;
837 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
838 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
840 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
841 skb->ip_summed = CHECKSUM_UNNECESSARY;
844 if (net->ipv6.mroute6_sk == NULL) {
850 * Deliver to user space multicast routing algorithms
852 ret = sock_queue_rcv_skb(net->ipv6.mroute6_sk, skb);
855 printk(KERN_WARNING "mroute6: pending queue full, dropping entries.\n");
863 * Queue a packet for resolution. It gets locked cache entry!
867 ip6mr_cache_unresolved(struct net *net, mifi_t mifi, struct sk_buff *skb)
871 struct mfc6_cache *c;
873 spin_lock_bh(&mfc_unres_lock);
874 list_for_each_entry(c, &net->ipv6.mfc6_unres_queue, list) {
875 if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
876 ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr)) {
884 * Create a new entry if allowable
887 if (atomic_read(&net->ipv6.cache_resolve_queue_len) >= 10 ||
888 (c = ip6mr_cache_alloc_unres()) == NULL) {
889 spin_unlock_bh(&mfc_unres_lock);
896 * Fill in the new cache entry
899 c->mf6c_origin = ipv6_hdr(skb)->saddr;
900 c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
903 * Reflect first query at pim6sd
905 err = ip6mr_cache_report(net, skb, mifi, MRT6MSG_NOCACHE);
907 /* If the report failed throw the cache entry
910 spin_unlock_bh(&mfc_unres_lock);
917 atomic_inc(&net->ipv6.cache_resolve_queue_len);
918 list_add(&c->list, &net->ipv6.mfc6_unres_queue);
920 ipmr_do_expire_process(net);
924 * See if we can append the packet
926 if (c->mfc_un.unres.unresolved.qlen > 3) {
930 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
934 spin_unlock_bh(&mfc_unres_lock);
939 * MFC6 cache manipulation by user space
942 static int ip6mr_mfc_delete(struct net *net, struct mf6cctl *mfc)
945 struct mfc6_cache *c, *next;
947 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
949 list_for_each_entry_safe(c, next, &net->ipv6.mfc6_cache_array[line], list) {
950 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
951 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
952 write_lock_bh(&mrt_lock);
954 write_unlock_bh(&mrt_lock);
963 static int ip6mr_device_event(struct notifier_block *this,
964 unsigned long event, void *ptr)
966 struct net_device *dev = ptr;
967 struct net *net = dev_net(dev);
968 struct mif_device *v;
972 if (event != NETDEV_UNREGISTER)
975 v = &net->ipv6.vif6_table[0];
976 for (ct = 0; ct < net->ipv6.maxvif; ct++, v++) {
978 mif6_delete(net, ct, &list);
980 unregister_netdevice_many(&list);
985 static struct notifier_block ip6_mr_notifier = {
986 .notifier_call = ip6mr_device_event
990 * Setup for IP multicast routing
993 static int __net_init ip6mr_net_init(struct net *net)
998 net->ipv6.vif6_table = kcalloc(MAXMIFS, sizeof(struct mif_device),
1000 if (!net->ipv6.vif6_table) {
1005 /* Forwarding cache */
1006 net->ipv6.mfc6_cache_array = kcalloc(MFC6_LINES,
1007 sizeof(struct list_head),
1009 if (!net->ipv6.mfc6_cache_array) {
1011 goto fail_mfc6_cache;
1014 for (i = 0; i < MFC6_LINES; i++)
1015 INIT_LIST_HEAD(&net->ipv6.mfc6_cache_array[i]);
1017 INIT_LIST_HEAD(&net->ipv6.mfc6_unres_queue);
1019 setup_timer(&net->ipv6.ipmr_expire_timer, ipmr_expire_process,
1020 (unsigned long)net);
1022 #ifdef CONFIG_IPV6_PIMSM_V2
1023 net->ipv6.mroute_reg_vif_num = -1;
1026 #ifdef CONFIG_PROC_FS
1028 if (!proc_net_fops_create(net, "ip6_mr_vif", 0, &ip6mr_vif_fops))
1030 if (!proc_net_fops_create(net, "ip6_mr_cache", 0, &ip6mr_mfc_fops))
1031 goto proc_cache_fail;
1035 #ifdef CONFIG_PROC_FS
1037 proc_net_remove(net, "ip6_mr_vif");
1039 kfree(net->ipv6.mfc6_cache_array);
1042 kfree(net->ipv6.vif6_table);
1047 static void __net_exit ip6mr_net_exit(struct net *net)
1049 #ifdef CONFIG_PROC_FS
1050 proc_net_remove(net, "ip6_mr_cache");
1051 proc_net_remove(net, "ip6_mr_vif");
1053 del_timer(&net->ipv6.ipmr_expire_timer);
1054 mroute_clean_tables(net);
1055 kfree(net->ipv6.mfc6_cache_array);
1056 kfree(net->ipv6.vif6_table);
1059 static struct pernet_operations ip6mr_net_ops = {
1060 .init = ip6mr_net_init,
1061 .exit = ip6mr_net_exit,
1064 int __init ip6_mr_init(void)
1068 mrt_cachep = kmem_cache_create("ip6_mrt_cache",
1069 sizeof(struct mfc6_cache),
1070 0, SLAB_HWCACHE_ALIGN,
1075 err = register_pernet_subsys(&ip6mr_net_ops);
1077 goto reg_pernet_fail;
1079 err = register_netdevice_notifier(&ip6_mr_notifier);
1081 goto reg_notif_fail;
1082 #ifdef CONFIG_IPV6_PIMSM_V2
1083 if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) {
1084 printk(KERN_ERR "ip6_mr_init: can't add PIM protocol\n");
1086 goto add_proto_fail;
1090 #ifdef CONFIG_IPV6_PIMSM_V2
1092 unregister_netdevice_notifier(&ip6_mr_notifier);
1095 unregister_pernet_subsys(&ip6mr_net_ops);
1097 kmem_cache_destroy(mrt_cachep);
1101 void ip6_mr_cleanup(void)
1103 unregister_netdevice_notifier(&ip6_mr_notifier);
1104 unregister_pernet_subsys(&ip6mr_net_ops);
1105 kmem_cache_destroy(mrt_cachep);
1108 static int ip6mr_mfc_add(struct net *net, struct mf6cctl *mfc, int mrtsock)
1112 struct mfc6_cache *uc, *c;
1113 unsigned char ttls[MAXMIFS];
1116 if (mfc->mf6cc_parent >= MAXMIFS)
1119 memset(ttls, 255, MAXMIFS);
1120 for (i = 0; i < MAXMIFS; i++) {
1121 if (IF_ISSET(i, &mfc->mf6cc_ifset))
1126 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1128 list_for_each_entry(c, &net->ipv6.mfc6_cache_array[line], list) {
1129 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1130 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
1137 write_lock_bh(&mrt_lock);
1138 c->mf6c_parent = mfc->mf6cc_parent;
1139 ip6mr_update_thresholds(net, c, ttls);
1141 c->mfc_flags |= MFC_STATIC;
1142 write_unlock_bh(&mrt_lock);
1146 if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1149 c = ip6mr_cache_alloc();
1153 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
1154 c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1155 c->mf6c_parent = mfc->mf6cc_parent;
1156 ip6mr_update_thresholds(net, c, ttls);
1158 c->mfc_flags |= MFC_STATIC;
1160 write_lock_bh(&mrt_lock);
1161 list_add(&c->list, &net->ipv6.mfc6_cache_array[line]);
1162 write_unlock_bh(&mrt_lock);
1165 * Check to see if we resolved a queued list. If so we
1166 * need to send on the frames and tidy up.
1169 spin_lock_bh(&mfc_unres_lock);
1170 list_for_each_entry(uc, &net->ipv6.mfc6_unres_queue, list) {
1171 if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
1172 ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
1173 list_del(&uc->list);
1174 atomic_dec(&net->ipv6.cache_resolve_queue_len);
1179 if (list_empty(&net->ipv6.mfc6_unres_queue))
1180 del_timer(&net->ipv6.ipmr_expire_timer);
1181 spin_unlock_bh(&mfc_unres_lock);
1184 ip6mr_cache_resolve(net, uc, c);
1185 ip6mr_cache_free(uc);
1191 * Close the multicast socket, and clear the vif tables etc
1194 static void mroute_clean_tables(struct net *net)
1198 struct mfc6_cache *c, *next;
1201 * Shut down all active vif entries
1203 for (i = 0; i < net->ipv6.maxvif; i++) {
1204 if (!(net->ipv6.vif6_table[i].flags & VIFF_STATIC))
1205 mif6_delete(net, i, &list);
1207 unregister_netdevice_many(&list);
1212 for (i = 0; i < MFC6_LINES; i++) {
1213 list_for_each_entry_safe(c, next, &net->ipv6.mfc6_cache_array[i], list) {
1214 if (c->mfc_flags & MFC_STATIC)
1216 write_lock_bh(&mrt_lock);
1218 write_unlock_bh(&mrt_lock);
1220 ip6mr_cache_free(c);
1224 if (atomic_read(&net->ipv6.cache_resolve_queue_len) != 0) {
1225 spin_lock_bh(&mfc_unres_lock);
1226 list_for_each_entry_safe(c, next, &net->ipv6.mfc6_unres_queue, list) {
1228 ip6mr_destroy_unres(net, c);
1230 spin_unlock_bh(&mfc_unres_lock);
1234 static int ip6mr_sk_init(struct sock *sk)
1237 struct net *net = sock_net(sk);
1240 write_lock_bh(&mrt_lock);
1241 if (likely(net->ipv6.mroute6_sk == NULL)) {
1242 net->ipv6.mroute6_sk = sk;
1243 net->ipv6.devconf_all->mc_forwarding++;
1247 write_unlock_bh(&mrt_lock);
1254 int ip6mr_sk_done(struct sock *sk)
1257 struct net *net = sock_net(sk);
1260 if (sk == net->ipv6.mroute6_sk) {
1261 write_lock_bh(&mrt_lock);
1262 net->ipv6.mroute6_sk = NULL;
1263 net->ipv6.devconf_all->mc_forwarding--;
1264 write_unlock_bh(&mrt_lock);
1266 mroute_clean_tables(net);
1275 * Socket options and virtual interface manipulation. The whole
1276 * virtual interface system is a complete heap, but unfortunately
1277 * that's how BSD mrouted happens to think. Maybe one day with a proper
1278 * MOSPF/PIM router set up we can clean this up.
1281 int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1287 struct net *net = sock_net(sk);
1289 if (optname != MRT6_INIT) {
1290 if (sk != net->ipv6.mroute6_sk && !capable(CAP_NET_ADMIN))
1296 if (sk->sk_type != SOCK_RAW ||
1297 inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1299 if (optlen < sizeof(int))
1302 return ip6mr_sk_init(sk);
1305 return ip6mr_sk_done(sk);
1308 if (optlen < sizeof(vif))
1310 if (copy_from_user(&vif, optval, sizeof(vif)))
1312 if (vif.mif6c_mifi >= MAXMIFS)
1315 ret = mif6_add(net, &vif, sk == net->ipv6.mroute6_sk);
1320 if (optlen < sizeof(mifi_t))
1322 if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
1325 ret = mif6_delete(net, mifi, NULL);
1330 * Manipulate the forwarding caches. These live
1331 * in a sort of kernel/user symbiosis.
1335 if (optlen < sizeof(mfc))
1337 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1340 if (optname == MRT6_DEL_MFC)
1341 ret = ip6mr_mfc_delete(net, &mfc);
1343 ret = ip6mr_mfc_add(net, &mfc,
1344 sk == net->ipv6.mroute6_sk);
1349 * Control PIM assert (to activate pim will activate assert)
1354 if (get_user(v, (int __user *)optval))
1356 net->ipv6.mroute_do_assert = !!v;
1360 #ifdef CONFIG_IPV6_PIMSM_V2
1364 if (get_user(v, (int __user *)optval))
1369 if (v != net->ipv6.mroute_do_pim) {
1370 net->ipv6.mroute_do_pim = v;
1371 net->ipv6.mroute_do_assert = v;
1379 * Spurious command, or MRT6_VERSION which you cannot
1383 return -ENOPROTOOPT;
1388 * Getsock opt support for the multicast routing system.
1391 int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
1396 struct net *net = sock_net(sk);
1402 #ifdef CONFIG_IPV6_PIMSM_V2
1404 val = net->ipv6.mroute_do_pim;
1408 val = net->ipv6.mroute_do_assert;
1411 return -ENOPROTOOPT;
1414 if (get_user(olr, optlen))
1417 olr = min_t(int, olr, sizeof(int));
1421 if (put_user(olr, optlen))
1423 if (copy_to_user(optval, &val, olr))
1429 * The IP multicast ioctl support routines.
1432 int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
1434 struct sioc_sg_req6 sr;
1435 struct sioc_mif_req6 vr;
1436 struct mif_device *vif;
1437 struct mfc6_cache *c;
1438 struct net *net = sock_net(sk);
1441 case SIOCGETMIFCNT_IN6:
1442 if (copy_from_user(&vr, arg, sizeof(vr)))
1444 if (vr.mifi >= net->ipv6.maxvif)
1446 read_lock(&mrt_lock);
1447 vif = &net->ipv6.vif6_table[vr.mifi];
1448 if (MIF_EXISTS(net, vr.mifi)) {
1449 vr.icount = vif->pkt_in;
1450 vr.ocount = vif->pkt_out;
1451 vr.ibytes = vif->bytes_in;
1452 vr.obytes = vif->bytes_out;
1453 read_unlock(&mrt_lock);
1455 if (copy_to_user(arg, &vr, sizeof(vr)))
1459 read_unlock(&mrt_lock);
1460 return -EADDRNOTAVAIL;
1461 case SIOCGETSGCNT_IN6:
1462 if (copy_from_user(&sr, arg, sizeof(sr)))
1465 read_lock(&mrt_lock);
1466 c = ip6mr_cache_find(net, &sr.src.sin6_addr, &sr.grp.sin6_addr);
1468 sr.pktcnt = c->mfc_un.res.pkt;
1469 sr.bytecnt = c->mfc_un.res.bytes;
1470 sr.wrong_if = c->mfc_un.res.wrong_if;
1471 read_unlock(&mrt_lock);
1473 if (copy_to_user(arg, &sr, sizeof(sr)))
1477 read_unlock(&mrt_lock);
1478 return -EADDRNOTAVAIL;
1480 return -ENOIOCTLCMD;
1485 static inline int ip6mr_forward2_finish(struct sk_buff *skb)
1487 IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
1488 IPSTATS_MIB_OUTFORWDATAGRAMS);
1489 return dst_output(skb);
1493 * Processing handlers for ip6mr_forward
1496 static int ip6mr_forward2(struct net *net, struct sk_buff *skb,
1497 struct mfc6_cache *c, int vifi)
1499 struct ipv6hdr *ipv6h;
1500 struct mif_device *vif = &net->ipv6.vif6_table[vifi];
1501 struct net_device *dev;
1502 struct dst_entry *dst;
1505 if (vif->dev == NULL)
1508 #ifdef CONFIG_IPV6_PIMSM_V2
1509 if (vif->flags & MIFF_REGISTER) {
1511 vif->bytes_out += skb->len;
1512 vif->dev->stats.tx_bytes += skb->len;
1513 vif->dev->stats.tx_packets++;
1514 ip6mr_cache_report(net, skb, vifi, MRT6MSG_WHOLEPKT);
1519 ipv6h = ipv6_hdr(skb);
1521 fl = (struct flowi) {
1524 { .daddr = ipv6h->daddr, }
1528 dst = ip6_route_output(net, NULL, &fl);
1533 skb_dst_set(skb, dst);
1536 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1537 * not only before forwarding, but after forwarding on all output
1538 * interfaces. It is clear, if mrouter runs a multicasting
1539 * program, it should receive packets not depending to what interface
1540 * program is joined.
1541 * If we will not make it, the program will have to join on all
1542 * interfaces. On the other hand, multihoming host (or router, but
1543 * not mrouter) cannot join to more than one interface - it will
1544 * result in receiving multiple packets.
1549 vif->bytes_out += skb->len;
1551 /* We are about to write */
1552 /* XXX: extension headers? */
1553 if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
1556 ipv6h = ipv6_hdr(skb);
1559 IP6CB(skb)->flags |= IP6SKB_FORWARDED;
1561 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dev,
1562 ip6mr_forward2_finish);
1569 static int ip6mr_find_vif(struct net_device *dev)
1571 struct net *net = dev_net(dev);
1573 for (ct = net->ipv6.maxvif - 1; ct >= 0; ct--) {
1574 if (net->ipv6.vif6_table[ct].dev == dev)
1580 static int ip6_mr_forward(struct net *net, struct sk_buff *skb,
1581 struct mfc6_cache *cache)
1586 vif = cache->mf6c_parent;
1587 cache->mfc_un.res.pkt++;
1588 cache->mfc_un.res.bytes += skb->len;
1591 * Wrong interface: drop packet and (maybe) send PIM assert.
1593 if (net->ipv6.vif6_table[vif].dev != skb->dev) {
1596 cache->mfc_un.res.wrong_if++;
1597 true_vifi = ip6mr_find_vif(skb->dev);
1599 if (true_vifi >= 0 && net->ipv6.mroute_do_assert &&
1600 /* pimsm uses asserts, when switching from RPT to SPT,
1601 so that we cannot check that packet arrived on an oif.
1602 It is bad, but otherwise we would need to move pretty
1603 large chunk of pimd to kernel. Ough... --ANK
1605 (net->ipv6.mroute_do_pim ||
1606 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1608 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1609 cache->mfc_un.res.last_assert = jiffies;
1610 ip6mr_cache_report(net, skb, true_vifi, MRT6MSG_WRONGMIF);
1615 net->ipv6.vif6_table[vif].pkt_in++;
1616 net->ipv6.vif6_table[vif].bytes_in += skb->len;
1621 for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
1622 if (ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
1624 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1626 ip6mr_forward2(net, skb2, cache, psend);
1632 ip6mr_forward2(net, skb, cache, psend);
1643 * Multicast packets for forwarding arrive here
1646 int ip6_mr_input(struct sk_buff *skb)
1648 struct mfc6_cache *cache;
1649 struct net *net = dev_net(skb->dev);
1651 read_lock(&mrt_lock);
1652 cache = ip6mr_cache_find(net,
1653 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
1656 * No usable cache entry
1658 if (cache == NULL) {
1661 vif = ip6mr_find_vif(skb->dev);
1663 int err = ip6mr_cache_unresolved(net, vif, skb);
1664 read_unlock(&mrt_lock);
1668 read_unlock(&mrt_lock);
1673 ip6_mr_forward(net, skb, cache);
1675 read_unlock(&mrt_lock);
1682 ip6mr_fill_mroute(struct net *net, struct sk_buff *skb, struct mfc6_cache *c,
1686 struct rtnexthop *nhp;
1687 u8 *b = skb_tail_pointer(skb);
1688 struct rtattr *mp_head;
1690 /* If cache is unresolved, don't try to parse IIF and OIF */
1691 if (c->mf6c_parent > MAXMIFS)
1694 if (MIF_EXISTS(net, c->mf6c_parent))
1695 RTA_PUT(skb, RTA_IIF, 4, &net->ipv6.vif6_table[c->mf6c_parent].dev->ifindex);
1697 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
1699 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
1700 if (MIF_EXISTS(net, ct) && c->mfc_un.res.ttls[ct] < 255) {
1701 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1702 goto rtattr_failure;
1703 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1704 nhp->rtnh_flags = 0;
1705 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
1706 nhp->rtnh_ifindex = net->ipv6.vif6_table[ct].dev->ifindex;
1707 nhp->rtnh_len = sizeof(*nhp);
1710 mp_head->rta_type = RTA_MULTIPATH;
1711 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
1712 rtm->rtm_type = RTN_MULTICAST;
1720 int ip6mr_get_route(struct net *net,
1721 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
1724 struct mfc6_cache *cache;
1725 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
1727 read_lock(&mrt_lock);
1728 cache = ip6mr_cache_find(net, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);
1731 struct sk_buff *skb2;
1732 struct ipv6hdr *iph;
1733 struct net_device *dev;
1737 read_unlock(&mrt_lock);
1742 if (dev == NULL || (vif = ip6mr_find_vif(dev)) < 0) {
1743 read_unlock(&mrt_lock);
1747 /* really correct? */
1748 skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
1750 read_unlock(&mrt_lock);
1754 skb_reset_transport_header(skb2);
1756 skb_put(skb2, sizeof(struct ipv6hdr));
1757 skb_reset_network_header(skb2);
1759 iph = ipv6_hdr(skb2);
1762 iph->flow_lbl[0] = 0;
1763 iph->flow_lbl[1] = 0;
1764 iph->flow_lbl[2] = 0;
1765 iph->payload_len = 0;
1766 iph->nexthdr = IPPROTO_NONE;
1768 ipv6_addr_copy(&iph->saddr, &rt->rt6i_src.addr);
1769 ipv6_addr_copy(&iph->daddr, &rt->rt6i_dst.addr);
1771 err = ip6mr_cache_unresolved(net, vif, skb2);
1772 read_unlock(&mrt_lock);
1777 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
1778 cache->mfc_flags |= MFC_NOTIFY;
1780 err = ip6mr_fill_mroute(net, skb, cache, rtm);
1781 read_unlock(&mrt_lock);