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 mfc6_cache **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 it->cache = net->ipv6.mfc6_cache_array;
103 read_lock(&mrt_lock);
104 for (it->ct = 0; it->ct < MFC6_LINES; it->ct++)
105 for (mfc = net->ipv6.mfc6_cache_array[it->ct];
106 mfc; mfc = mfc->next)
109 read_unlock(&mrt_lock);
111 it->cache = &net->ipv6.mfc6_unres_queue;
112 spin_lock_bh(&mfc_unres_lock);
113 for (mfc = net->ipv6.mfc6_unres_queue; mfc; mfc = mfc->next)
116 spin_unlock_bh(&mfc_unres_lock);
126 * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
129 struct ipmr_vif_iter {
130 struct seq_net_private p;
134 static struct mif_device *ip6mr_vif_seq_idx(struct net *net,
135 struct ipmr_vif_iter *iter,
138 for (iter->ct = 0; iter->ct < net->ipv6.maxvif; ++iter->ct) {
139 if (!MIF_EXISTS(net, iter->ct))
142 return &net->ipv6.vif6_table[iter->ct];
147 static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
150 struct net *net = seq_file_net(seq);
152 read_lock(&mrt_lock);
153 return *pos ? ip6mr_vif_seq_idx(net, seq->private, *pos - 1)
157 static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
159 struct ipmr_vif_iter *iter = seq->private;
160 struct net *net = seq_file_net(seq);
163 if (v == SEQ_START_TOKEN)
164 return ip6mr_vif_seq_idx(net, iter, 0);
166 while (++iter->ct < net->ipv6.maxvif) {
167 if (!MIF_EXISTS(net, iter->ct))
169 return &net->ipv6.vif6_table[iter->ct];
174 static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
177 read_unlock(&mrt_lock);
180 static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
182 struct net *net = seq_file_net(seq);
184 if (v == SEQ_START_TOKEN) {
186 "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
188 const struct mif_device *vif = v;
189 const char *name = vif->dev ? vif->dev->name : "none";
192 "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
193 vif - net->ipv6.vif6_table,
194 name, vif->bytes_in, vif->pkt_in,
195 vif->bytes_out, vif->pkt_out,
201 static const struct seq_operations ip6mr_vif_seq_ops = {
202 .start = ip6mr_vif_seq_start,
203 .next = ip6mr_vif_seq_next,
204 .stop = ip6mr_vif_seq_stop,
205 .show = ip6mr_vif_seq_show,
208 static int ip6mr_vif_open(struct inode *inode, struct file *file)
210 return seq_open_net(inode, file, &ip6mr_vif_seq_ops,
211 sizeof(struct ipmr_vif_iter));
214 static const struct file_operations ip6mr_vif_fops = {
215 .owner = THIS_MODULE,
216 .open = ip6mr_vif_open,
219 .release = seq_release_net,
222 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
224 struct net *net = seq_file_net(seq);
226 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
230 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
232 struct mfc6_cache *mfc = v;
233 struct ipmr_mfc_iter *it = seq->private;
234 struct net *net = seq_file_net(seq);
238 if (v == SEQ_START_TOKEN)
239 return ipmr_mfc_seq_idx(net, seq->private, 0);
244 if (it->cache == &net->ipv6.mfc6_unres_queue)
247 BUG_ON(it->cache != net->ipv6.mfc6_cache_array);
249 while (++it->ct < MFC6_LINES) {
250 mfc = net->ipv6.mfc6_cache_array[it->ct];
255 /* exhausted cache_array, show unresolved */
256 read_unlock(&mrt_lock);
257 it->cache = &net->ipv6.mfc6_unres_queue;
260 spin_lock_bh(&mfc_unres_lock);
261 mfc = net->ipv6.mfc6_unres_queue;
266 spin_unlock_bh(&mfc_unres_lock);
272 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
274 struct ipmr_mfc_iter *it = seq->private;
275 struct net *net = seq_file_net(seq);
277 if (it->cache == &net->ipv6.mfc6_unres_queue)
278 spin_unlock_bh(&mfc_unres_lock);
279 else if (it->cache == net->ipv6.mfc6_cache_array)
280 read_unlock(&mrt_lock);
283 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
286 struct net *net = seq_file_net(seq);
288 if (v == SEQ_START_TOKEN) {
292 "Iif Pkts Bytes Wrong Oifs\n");
294 const struct mfc6_cache *mfc = v;
295 const struct ipmr_mfc_iter *it = seq->private;
297 seq_printf(seq, "%pI6 %pI6 %-3hd",
298 &mfc->mf6c_mcastgrp, &mfc->mf6c_origin,
301 if (it->cache != &net->ipv6.mfc6_unres_queue) {
302 seq_printf(seq, " %8lu %8lu %8lu",
304 mfc->mfc_un.res.bytes,
305 mfc->mfc_un.res.wrong_if);
306 for (n = mfc->mfc_un.res.minvif;
307 n < mfc->mfc_un.res.maxvif; n++) {
308 if (MIF_EXISTS(net, n) &&
309 mfc->mfc_un.res.ttls[n] < 255)
312 n, mfc->mfc_un.res.ttls[n]);
315 /* unresolved mfc_caches don't contain
316 * pkt, bytes and wrong_if values
318 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
325 static const struct seq_operations ipmr_mfc_seq_ops = {
326 .start = ipmr_mfc_seq_start,
327 .next = ipmr_mfc_seq_next,
328 .stop = ipmr_mfc_seq_stop,
329 .show = ipmr_mfc_seq_show,
332 static int ipmr_mfc_open(struct inode *inode, struct file *file)
334 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
335 sizeof(struct ipmr_mfc_iter));
338 static const struct file_operations ip6mr_mfc_fops = {
339 .owner = THIS_MODULE,
340 .open = ipmr_mfc_open,
343 .release = seq_release_net,
347 #ifdef CONFIG_IPV6_PIMSM_V2
349 static int pim6_rcv(struct sk_buff *skb)
351 struct pimreghdr *pim;
352 struct ipv6hdr *encap;
353 struct net_device *reg_dev = NULL;
354 struct net *net = dev_net(skb->dev);
355 int reg_vif_num = net->ipv6.mroute_reg_vif_num;
357 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
360 pim = (struct pimreghdr *)skb_transport_header(skb);
361 if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
362 (pim->flags & PIM_NULL_REGISTER) ||
363 (csum_ipv6_magic(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr,
364 sizeof(*pim), IPPROTO_PIM,
365 csum_partial((void *)pim, sizeof(*pim), 0)) &&
366 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
369 /* check if the inner packet is destined to mcast group */
370 encap = (struct ipv6hdr *)(skb_transport_header(skb) +
373 if (!ipv6_addr_is_multicast(&encap->daddr) ||
374 encap->payload_len == 0 ||
375 ntohs(encap->payload_len) + sizeof(*pim) > skb->len)
378 read_lock(&mrt_lock);
379 if (reg_vif_num >= 0)
380 reg_dev = net->ipv6.vif6_table[reg_vif_num].dev;
383 read_unlock(&mrt_lock);
388 skb->mac_header = skb->network_header;
389 skb_pull(skb, (u8 *)encap - skb->data);
390 skb_reset_network_header(skb);
392 skb->protocol = htons(ETH_P_IPV6);
394 skb->pkt_type = PACKET_HOST;
396 reg_dev->stats.rx_bytes += skb->len;
397 reg_dev->stats.rx_packets++;
407 static const struct inet6_protocol pim6_protocol = {
411 /* Service routines creating virtual interfaces: PIMREG */
413 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb,
414 struct net_device *dev)
416 struct net *net = dev_net(dev);
418 read_lock(&mrt_lock);
419 dev->stats.tx_bytes += skb->len;
420 dev->stats.tx_packets++;
421 ip6mr_cache_report(net, skb, net->ipv6.mroute_reg_vif_num,
423 read_unlock(&mrt_lock);
428 static const struct net_device_ops reg_vif_netdev_ops = {
429 .ndo_start_xmit = reg_vif_xmit,
432 static void reg_vif_setup(struct net_device *dev)
434 dev->type = ARPHRD_PIMREG;
435 dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
436 dev->flags = IFF_NOARP;
437 dev->netdev_ops = ®_vif_netdev_ops;
438 dev->destructor = free_netdev;
439 dev->features |= NETIF_F_NETNS_LOCAL;
442 static struct net_device *ip6mr_reg_vif(struct net *net)
444 struct net_device *dev;
446 dev = alloc_netdev(0, "pim6reg", reg_vif_setup);
450 dev_net_set(dev, net);
452 if (register_netdevice(dev)) {
465 /* allow the register to be completed before unregistering. */
469 unregister_netdevice(dev);
478 static int mif6_delete(struct net *net, int vifi, struct list_head *head)
480 struct mif_device *v;
481 struct net_device *dev;
482 struct inet6_dev *in6_dev;
483 if (vifi < 0 || vifi >= net->ipv6.maxvif)
484 return -EADDRNOTAVAIL;
486 v = &net->ipv6.vif6_table[vifi];
488 write_lock_bh(&mrt_lock);
493 write_unlock_bh(&mrt_lock);
494 return -EADDRNOTAVAIL;
497 #ifdef CONFIG_IPV6_PIMSM_V2
498 if (vifi == net->ipv6.mroute_reg_vif_num)
499 net->ipv6.mroute_reg_vif_num = -1;
502 if (vifi + 1 == net->ipv6.maxvif) {
504 for (tmp = vifi - 1; tmp >= 0; tmp--) {
505 if (MIF_EXISTS(net, tmp))
508 net->ipv6.maxvif = tmp + 1;
511 write_unlock_bh(&mrt_lock);
513 dev_set_allmulti(dev, -1);
515 in6_dev = __in6_dev_get(dev);
517 in6_dev->cnf.mc_forwarding--;
519 if (v->flags & MIFF_REGISTER)
520 unregister_netdevice_queue(dev, head);
526 static inline void ip6mr_cache_free(struct mfc6_cache *c)
528 kmem_cache_free(mrt_cachep, c);
531 /* Destroy an unresolved cache entry, killing queued skbs
532 and reporting error to netlink readers.
535 static void ip6mr_destroy_unres(struct net *net, struct mfc6_cache *c)
539 atomic_dec(&net->ipv6.cache_resolve_queue_len);
541 while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
542 if (ipv6_hdr(skb)->version == 0) {
543 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
544 nlh->nlmsg_type = NLMSG_ERROR;
545 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
546 skb_trim(skb, nlh->nlmsg_len);
547 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
548 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
557 /* Timer process for all the unresolved queue. */
559 static void ipmr_do_expire_process(struct net *net)
561 unsigned long now = jiffies;
562 unsigned long expires = 10 * HZ;
563 struct mfc6_cache *c, **cp;
565 cp = &net->ipv6.mfc6_unres_queue;
567 while ((c = *cp) != NULL) {
568 if (time_after(c->mfc_un.unres.expires, now)) {
570 unsigned long interval = c->mfc_un.unres.expires - now;
571 if (interval < expires)
578 ip6mr_destroy_unres(net, c);
581 if (net->ipv6.mfc6_unres_queue != NULL)
582 mod_timer(&net->ipv6.ipmr_expire_timer, jiffies + expires);
585 static void ipmr_expire_process(unsigned long arg)
587 struct net *net = (struct net *)arg;
589 if (!spin_trylock(&mfc_unres_lock)) {
590 mod_timer(&net->ipv6.ipmr_expire_timer, jiffies + 1);
594 if (net->ipv6.mfc6_unres_queue != NULL)
595 ipmr_do_expire_process(net);
597 spin_unlock(&mfc_unres_lock);
600 /* Fill oifs list. It is called under write locked mrt_lock. */
602 static void ip6mr_update_thresholds(struct net *net, struct mfc6_cache *cache,
607 cache->mfc_un.res.minvif = MAXMIFS;
608 cache->mfc_un.res.maxvif = 0;
609 memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
611 for (vifi = 0; vifi < net->ipv6.maxvif; vifi++) {
612 if (MIF_EXISTS(net, vifi) &&
613 ttls[vifi] && ttls[vifi] < 255) {
614 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
615 if (cache->mfc_un.res.minvif > vifi)
616 cache->mfc_un.res.minvif = vifi;
617 if (cache->mfc_un.res.maxvif <= vifi)
618 cache->mfc_un.res.maxvif = vifi + 1;
623 static int mif6_add(struct net *net, struct mif6ctl *vifc, int mrtsock)
625 int vifi = vifc->mif6c_mifi;
626 struct mif_device *v = &net->ipv6.vif6_table[vifi];
627 struct net_device *dev;
628 struct inet6_dev *in6_dev;
632 if (MIF_EXISTS(net, vifi))
635 switch (vifc->mif6c_flags) {
636 #ifdef CONFIG_IPV6_PIMSM_V2
639 * Special Purpose VIF in PIM
640 * All the packets will be sent to the daemon
642 if (net->ipv6.mroute_reg_vif_num >= 0)
644 dev = ip6mr_reg_vif(net);
647 err = dev_set_allmulti(dev, 1);
649 unregister_netdevice(dev);
656 dev = dev_get_by_index(net, vifc->mif6c_pifi);
658 return -EADDRNOTAVAIL;
659 err = dev_set_allmulti(dev, 1);
669 in6_dev = __in6_dev_get(dev);
671 in6_dev->cnf.mc_forwarding++;
674 * Fill in the VIF structures
676 v->rate_limit = vifc->vifc_rate_limit;
677 v->flags = vifc->mif6c_flags;
679 v->flags |= VIFF_STATIC;
680 v->threshold = vifc->vifc_threshold;
685 v->link = dev->ifindex;
686 if (v->flags & MIFF_REGISTER)
687 v->link = dev->iflink;
689 /* And finish update writing critical data */
690 write_lock_bh(&mrt_lock);
692 #ifdef CONFIG_IPV6_PIMSM_V2
693 if (v->flags & MIFF_REGISTER)
694 net->ipv6.mroute_reg_vif_num = vifi;
696 if (vifi + 1 > net->ipv6.maxvif)
697 net->ipv6.maxvif = vifi + 1;
698 write_unlock_bh(&mrt_lock);
702 static struct mfc6_cache *ip6mr_cache_find(struct net *net,
703 struct in6_addr *origin,
704 struct in6_addr *mcastgrp)
706 int line = MFC6_HASH(mcastgrp, origin);
707 struct mfc6_cache *c;
709 for (c = net->ipv6.mfc6_cache_array[line]; c; c = c->next) {
710 if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
711 ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
718 * Allocate a multicast cache entry
720 static struct mfc6_cache *ip6mr_cache_alloc(void)
722 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
725 c->mfc_un.res.minvif = MAXMIFS;
729 static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
731 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
734 skb_queue_head_init(&c->mfc_un.unres.unresolved);
735 c->mfc_un.unres.expires = jiffies + 10 * HZ;
740 * A cache entry has gone into a resolved state from queued
743 static void ip6mr_cache_resolve(struct net *net, struct mfc6_cache *uc,
744 struct mfc6_cache *c)
749 * Play the pending entries through our router
752 while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
753 if (ipv6_hdr(skb)->version == 0) {
755 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
757 if (ip6mr_fill_mroute(net, skb, c, NLMSG_DATA(nlh)) > 0) {
758 nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
760 nlh->nlmsg_type = NLMSG_ERROR;
761 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
762 skb_trim(skb, nlh->nlmsg_len);
763 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
765 err = rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
767 ip6_mr_forward(net, skb, c);
772 * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
773 * expects the following bizarre scheme.
775 * Called under mrt_lock.
778 static int ip6mr_cache_report(struct net *net, struct sk_buff *pkt, mifi_t mifi,
785 #ifdef CONFIG_IPV6_PIMSM_V2
786 if (assert == MRT6MSG_WHOLEPKT)
787 skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
791 skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
796 /* I suppose that internal messages
797 * do not require checksums */
799 skb->ip_summed = CHECKSUM_UNNECESSARY;
801 #ifdef CONFIG_IPV6_PIMSM_V2
802 if (assert == MRT6MSG_WHOLEPKT) {
803 /* Ugly, but we have no choice with this interface.
804 Duplicate old header, fix length etc.
805 And all this only to mangle msg->im6_msgtype and
806 to set msg->im6_mbz to "mbz" :-)
808 skb_push(skb, -skb_network_offset(pkt));
810 skb_push(skb, sizeof(*msg));
811 skb_reset_transport_header(skb);
812 msg = (struct mrt6msg *)skb_transport_header(skb);
814 msg->im6_msgtype = MRT6MSG_WHOLEPKT;
815 msg->im6_mif = net->ipv6.mroute_reg_vif_num;
817 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
818 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
820 skb->ip_summed = CHECKSUM_UNNECESSARY;
828 skb_put(skb, sizeof(struct ipv6hdr));
829 skb_reset_network_header(skb);
830 skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
835 skb_put(skb, sizeof(*msg));
836 skb_reset_transport_header(skb);
837 msg = (struct mrt6msg *)skb_transport_header(skb);
840 msg->im6_msgtype = assert;
843 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
844 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
846 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
847 skb->ip_summed = CHECKSUM_UNNECESSARY;
850 if (net->ipv6.mroute6_sk == NULL) {
856 * Deliver to user space multicast routing algorithms
858 ret = sock_queue_rcv_skb(net->ipv6.mroute6_sk, skb);
861 printk(KERN_WARNING "mroute6: pending queue full, dropping entries.\n");
869 * Queue a packet for resolution. It gets locked cache entry!
873 ip6mr_cache_unresolved(struct net *net, mifi_t mifi, struct sk_buff *skb)
876 struct mfc6_cache *c;
878 spin_lock_bh(&mfc_unres_lock);
879 for (c = net->ipv6.mfc6_unres_queue; c; c = c->next) {
880 if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
881 ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr))
887 * Create a new entry if allowable
890 if (atomic_read(&net->ipv6.cache_resolve_queue_len) >= 10 ||
891 (c = ip6mr_cache_alloc_unres()) == NULL) {
892 spin_unlock_bh(&mfc_unres_lock);
899 * Fill in the new cache entry
902 c->mf6c_origin = ipv6_hdr(skb)->saddr;
903 c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
906 * Reflect first query at pim6sd
908 err = ip6mr_cache_report(net, skb, mifi, MRT6MSG_NOCACHE);
910 /* If the report failed throw the cache entry
913 spin_unlock_bh(&mfc_unres_lock);
920 atomic_inc(&net->ipv6.cache_resolve_queue_len);
921 c->next = net->ipv6.mfc6_unres_queue;
922 net->ipv6.mfc6_unres_queue = c;
924 ipmr_do_expire_process(net);
928 * See if we can append the packet
930 if (c->mfc_un.unres.unresolved.qlen > 3) {
934 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
938 spin_unlock_bh(&mfc_unres_lock);
943 * MFC6 cache manipulation by user space
946 static int ip6mr_mfc_delete(struct net *net, struct mf6cctl *mfc)
949 struct mfc6_cache *c, **cp;
951 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
953 for (cp = &net->ipv6.mfc6_cache_array[line];
954 (c = *cp) != NULL; cp = &c->next) {
955 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
956 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
957 write_lock_bh(&mrt_lock);
959 write_unlock_bh(&mrt_lock);
968 static int ip6mr_device_event(struct notifier_block *this,
969 unsigned long event, void *ptr)
971 struct net_device *dev = ptr;
972 struct net *net = dev_net(dev);
973 struct mif_device *v;
977 if (event != NETDEV_UNREGISTER)
980 v = &net->ipv6.vif6_table[0];
981 for (ct = 0; ct < net->ipv6.maxvif; ct++, v++) {
983 mif6_delete(net, ct, &list);
985 unregister_netdevice_many(&list);
990 static struct notifier_block ip6_mr_notifier = {
991 .notifier_call = ip6mr_device_event
995 * Setup for IP multicast routing
998 static int __net_init ip6mr_net_init(struct net *net)
1001 net->ipv6.vif6_table = kcalloc(MAXMIFS, sizeof(struct mif_device),
1003 if (!net->ipv6.vif6_table) {
1008 /* Forwarding cache */
1009 net->ipv6.mfc6_cache_array = kcalloc(MFC6_LINES,
1010 sizeof(struct mfc6_cache *),
1012 if (!net->ipv6.mfc6_cache_array) {
1014 goto fail_mfc6_cache;
1017 setup_timer(&net->ipv6.ipmr_expire_timer, ipmr_expire_process,
1018 (unsigned long)net);
1020 #ifdef CONFIG_IPV6_PIMSM_V2
1021 net->ipv6.mroute_reg_vif_num = -1;
1024 #ifdef CONFIG_PROC_FS
1026 if (!proc_net_fops_create(net, "ip6_mr_vif", 0, &ip6mr_vif_fops))
1028 if (!proc_net_fops_create(net, "ip6_mr_cache", 0, &ip6mr_mfc_fops))
1029 goto proc_cache_fail;
1033 #ifdef CONFIG_PROC_FS
1035 proc_net_remove(net, "ip6_mr_vif");
1037 kfree(net->ipv6.mfc6_cache_array);
1040 kfree(net->ipv6.vif6_table);
1045 static void __net_exit ip6mr_net_exit(struct net *net)
1047 #ifdef CONFIG_PROC_FS
1048 proc_net_remove(net, "ip6_mr_cache");
1049 proc_net_remove(net, "ip6_mr_vif");
1051 del_timer(&net->ipv6.ipmr_expire_timer);
1052 mroute_clean_tables(net);
1053 kfree(net->ipv6.mfc6_cache_array);
1054 kfree(net->ipv6.vif6_table);
1057 static struct pernet_operations ip6mr_net_ops = {
1058 .init = ip6mr_net_init,
1059 .exit = ip6mr_net_exit,
1062 int __init ip6_mr_init(void)
1066 mrt_cachep = kmem_cache_create("ip6_mrt_cache",
1067 sizeof(struct mfc6_cache),
1068 0, SLAB_HWCACHE_ALIGN,
1073 err = register_pernet_subsys(&ip6mr_net_ops);
1075 goto reg_pernet_fail;
1077 err = register_netdevice_notifier(&ip6_mr_notifier);
1079 goto reg_notif_fail;
1080 #ifdef CONFIG_IPV6_PIMSM_V2
1081 if (inet6_add_protocol(&pim6_protocol, IPPROTO_PIM) < 0) {
1082 printk(KERN_ERR "ip6_mr_init: can't add PIM protocol\n");
1084 goto add_proto_fail;
1088 #ifdef CONFIG_IPV6_PIMSM_V2
1090 unregister_netdevice_notifier(&ip6_mr_notifier);
1093 unregister_pernet_subsys(&ip6mr_net_ops);
1095 kmem_cache_destroy(mrt_cachep);
1099 void ip6_mr_cleanup(void)
1101 unregister_netdevice_notifier(&ip6_mr_notifier);
1102 unregister_pernet_subsys(&ip6mr_net_ops);
1103 kmem_cache_destroy(mrt_cachep);
1106 static int ip6mr_mfc_add(struct net *net, struct mf6cctl *mfc, int mrtsock)
1109 struct mfc6_cache *uc, *c, **cp;
1110 unsigned char ttls[MAXMIFS];
1113 if (mfc->mf6cc_parent >= MAXMIFS)
1116 memset(ttls, 255, MAXMIFS);
1117 for (i = 0; i < MAXMIFS; i++) {
1118 if (IF_ISSET(i, &mfc->mf6cc_ifset))
1123 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1125 for (cp = &net->ipv6.mfc6_cache_array[line];
1126 (c = *cp) != NULL; cp = &c->next) {
1127 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1128 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr))
1133 write_lock_bh(&mrt_lock);
1134 c->mf6c_parent = mfc->mf6cc_parent;
1135 ip6mr_update_thresholds(net, c, ttls);
1137 c->mfc_flags |= MFC_STATIC;
1138 write_unlock_bh(&mrt_lock);
1142 if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1145 c = ip6mr_cache_alloc();
1149 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
1150 c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1151 c->mf6c_parent = mfc->mf6cc_parent;
1152 ip6mr_update_thresholds(net, c, ttls);
1154 c->mfc_flags |= MFC_STATIC;
1156 write_lock_bh(&mrt_lock);
1157 c->next = net->ipv6.mfc6_cache_array[line];
1158 net->ipv6.mfc6_cache_array[line] = c;
1159 write_unlock_bh(&mrt_lock);
1162 * Check to see if we resolved a queued list. If so we
1163 * need to send on the frames and tidy up.
1165 spin_lock_bh(&mfc_unres_lock);
1166 for (cp = &net->ipv6.mfc6_unres_queue; (uc = *cp) != NULL;
1168 if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
1169 ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
1171 atomic_dec(&net->ipv6.cache_resolve_queue_len);
1175 if (net->ipv6.mfc6_unres_queue == NULL)
1176 del_timer(&net->ipv6.ipmr_expire_timer);
1177 spin_unlock_bh(&mfc_unres_lock);
1180 ip6mr_cache_resolve(net, uc, c);
1181 ip6mr_cache_free(uc);
1187 * Close the multicast socket, and clear the vif tables etc
1190 static void mroute_clean_tables(struct net *net)
1196 * Shut down all active vif entries
1198 for (i = 0; i < net->ipv6.maxvif; i++) {
1199 if (!(net->ipv6.vif6_table[i].flags & VIFF_STATIC))
1200 mif6_delete(net, i, &list);
1202 unregister_netdevice_many(&list);
1207 for (i = 0; i < MFC6_LINES; i++) {
1208 struct mfc6_cache *c, **cp;
1210 cp = &net->ipv6.mfc6_cache_array[i];
1211 while ((c = *cp) != NULL) {
1212 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 struct mfc6_cache *c, **cp;
1227 spin_lock_bh(&mfc_unres_lock);
1228 cp = &net->ipv6.mfc6_unres_queue;
1229 while ((c = *cp) != NULL) {
1231 ip6mr_destroy_unres(net, c);
1233 spin_unlock_bh(&mfc_unres_lock);
1237 static int ip6mr_sk_init(struct sock *sk)
1240 struct net *net = sock_net(sk);
1243 write_lock_bh(&mrt_lock);
1244 if (likely(net->ipv6.mroute6_sk == NULL)) {
1245 net->ipv6.mroute6_sk = sk;
1246 net->ipv6.devconf_all->mc_forwarding++;
1250 write_unlock_bh(&mrt_lock);
1257 int ip6mr_sk_done(struct sock *sk)
1260 struct net *net = sock_net(sk);
1263 if (sk == net->ipv6.mroute6_sk) {
1264 write_lock_bh(&mrt_lock);
1265 net->ipv6.mroute6_sk = NULL;
1266 net->ipv6.devconf_all->mc_forwarding--;
1267 write_unlock_bh(&mrt_lock);
1269 mroute_clean_tables(net);
1278 * Socket options and virtual interface manipulation. The whole
1279 * virtual interface system is a complete heap, but unfortunately
1280 * that's how BSD mrouted happens to think. Maybe one day with a proper
1281 * MOSPF/PIM router set up we can clean this up.
1284 int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1290 struct net *net = sock_net(sk);
1292 if (optname != MRT6_INIT) {
1293 if (sk != net->ipv6.mroute6_sk && !capable(CAP_NET_ADMIN))
1299 if (sk->sk_type != SOCK_RAW ||
1300 inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
1302 if (optlen < sizeof(int))
1305 return ip6mr_sk_init(sk);
1308 return ip6mr_sk_done(sk);
1311 if (optlen < sizeof(vif))
1313 if (copy_from_user(&vif, optval, sizeof(vif)))
1315 if (vif.mif6c_mifi >= MAXMIFS)
1318 ret = mif6_add(net, &vif, sk == net->ipv6.mroute6_sk);
1323 if (optlen < sizeof(mifi_t))
1325 if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
1328 ret = mif6_delete(net, mifi, NULL);
1333 * Manipulate the forwarding caches. These live
1334 * in a sort of kernel/user symbiosis.
1338 if (optlen < sizeof(mfc))
1340 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1343 if (optname == MRT6_DEL_MFC)
1344 ret = ip6mr_mfc_delete(net, &mfc);
1346 ret = ip6mr_mfc_add(net, &mfc,
1347 sk == net->ipv6.mroute6_sk);
1352 * Control PIM assert (to activate pim will activate assert)
1357 if (get_user(v, (int __user *)optval))
1359 net->ipv6.mroute_do_assert = !!v;
1363 #ifdef CONFIG_IPV6_PIMSM_V2
1367 if (get_user(v, (int __user *)optval))
1372 if (v != net->ipv6.mroute_do_pim) {
1373 net->ipv6.mroute_do_pim = v;
1374 net->ipv6.mroute_do_assert = v;
1382 * Spurious command, or MRT6_VERSION which you cannot
1386 return -ENOPROTOOPT;
1391 * Getsock opt support for the multicast routing system.
1394 int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
1399 struct net *net = sock_net(sk);
1405 #ifdef CONFIG_IPV6_PIMSM_V2
1407 val = net->ipv6.mroute_do_pim;
1411 val = net->ipv6.mroute_do_assert;
1414 return -ENOPROTOOPT;
1417 if (get_user(olr, optlen))
1420 olr = min_t(int, olr, sizeof(int));
1424 if (put_user(olr, optlen))
1426 if (copy_to_user(optval, &val, olr))
1432 * The IP multicast ioctl support routines.
1435 int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
1437 struct sioc_sg_req6 sr;
1438 struct sioc_mif_req6 vr;
1439 struct mif_device *vif;
1440 struct mfc6_cache *c;
1441 struct net *net = sock_net(sk);
1444 case SIOCGETMIFCNT_IN6:
1445 if (copy_from_user(&vr, arg, sizeof(vr)))
1447 if (vr.mifi >= net->ipv6.maxvif)
1449 read_lock(&mrt_lock);
1450 vif = &net->ipv6.vif6_table[vr.mifi];
1451 if (MIF_EXISTS(net, vr.mifi)) {
1452 vr.icount = vif->pkt_in;
1453 vr.ocount = vif->pkt_out;
1454 vr.ibytes = vif->bytes_in;
1455 vr.obytes = vif->bytes_out;
1456 read_unlock(&mrt_lock);
1458 if (copy_to_user(arg, &vr, sizeof(vr)))
1462 read_unlock(&mrt_lock);
1463 return -EADDRNOTAVAIL;
1464 case SIOCGETSGCNT_IN6:
1465 if (copy_from_user(&sr, arg, sizeof(sr)))
1468 read_lock(&mrt_lock);
1469 c = ip6mr_cache_find(net, &sr.src.sin6_addr, &sr.grp.sin6_addr);
1471 sr.pktcnt = c->mfc_un.res.pkt;
1472 sr.bytecnt = c->mfc_un.res.bytes;
1473 sr.wrong_if = c->mfc_un.res.wrong_if;
1474 read_unlock(&mrt_lock);
1476 if (copy_to_user(arg, &sr, sizeof(sr)))
1480 read_unlock(&mrt_lock);
1481 return -EADDRNOTAVAIL;
1483 return -ENOIOCTLCMD;
1488 static inline int ip6mr_forward2_finish(struct sk_buff *skb)
1490 IP6_INC_STATS_BH(dev_net(skb_dst(skb)->dev), ip6_dst_idev(skb_dst(skb)),
1491 IPSTATS_MIB_OUTFORWDATAGRAMS);
1492 return dst_output(skb);
1496 * Processing handlers for ip6mr_forward
1499 static int ip6mr_forward2(struct net *net, struct sk_buff *skb,
1500 struct mfc6_cache *c, int vifi)
1502 struct ipv6hdr *ipv6h;
1503 struct mif_device *vif = &net->ipv6.vif6_table[vifi];
1504 struct net_device *dev;
1505 struct dst_entry *dst;
1508 if (vif->dev == NULL)
1511 #ifdef CONFIG_IPV6_PIMSM_V2
1512 if (vif->flags & MIFF_REGISTER) {
1514 vif->bytes_out += skb->len;
1515 vif->dev->stats.tx_bytes += skb->len;
1516 vif->dev->stats.tx_packets++;
1517 ip6mr_cache_report(net, skb, vifi, MRT6MSG_WHOLEPKT);
1522 ipv6h = ipv6_hdr(skb);
1524 fl = (struct flowi) {
1527 { .daddr = ipv6h->daddr, }
1531 dst = ip6_route_output(net, NULL, &fl);
1536 skb_dst_set(skb, dst);
1539 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1540 * not only before forwarding, but after forwarding on all output
1541 * interfaces. It is clear, if mrouter runs a multicasting
1542 * program, it should receive packets not depending to what interface
1543 * program is joined.
1544 * If we will not make it, the program will have to join on all
1545 * interfaces. On the other hand, multihoming host (or router, but
1546 * not mrouter) cannot join to more than one interface - it will
1547 * result in receiving multiple packets.
1552 vif->bytes_out += skb->len;
1554 /* We are about to write */
1555 /* XXX: extension headers? */
1556 if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
1559 ipv6h = ipv6_hdr(skb);
1562 IP6CB(skb)->flags |= IP6SKB_FORWARDED;
1564 return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dev,
1565 ip6mr_forward2_finish);
1572 static int ip6mr_find_vif(struct net_device *dev)
1574 struct net *net = dev_net(dev);
1576 for (ct = net->ipv6.maxvif - 1; ct >= 0; ct--) {
1577 if (net->ipv6.vif6_table[ct].dev == dev)
1583 static int ip6_mr_forward(struct net *net, struct sk_buff *skb,
1584 struct mfc6_cache *cache)
1589 vif = cache->mf6c_parent;
1590 cache->mfc_un.res.pkt++;
1591 cache->mfc_un.res.bytes += skb->len;
1594 * Wrong interface: drop packet and (maybe) send PIM assert.
1596 if (net->ipv6.vif6_table[vif].dev != skb->dev) {
1599 cache->mfc_un.res.wrong_if++;
1600 true_vifi = ip6mr_find_vif(skb->dev);
1602 if (true_vifi >= 0 && net->ipv6.mroute_do_assert &&
1603 /* pimsm uses asserts, when switching from RPT to SPT,
1604 so that we cannot check that packet arrived on an oif.
1605 It is bad, but otherwise we would need to move pretty
1606 large chunk of pimd to kernel. Ough... --ANK
1608 (net->ipv6.mroute_do_pim ||
1609 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1611 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1612 cache->mfc_un.res.last_assert = jiffies;
1613 ip6mr_cache_report(net, skb, true_vifi, MRT6MSG_WRONGMIF);
1618 net->ipv6.vif6_table[vif].pkt_in++;
1619 net->ipv6.vif6_table[vif].bytes_in += skb->len;
1624 for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
1625 if (ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
1627 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1629 ip6mr_forward2(net, skb2, cache, psend);
1635 ip6mr_forward2(net, skb, cache, psend);
1646 * Multicast packets for forwarding arrive here
1649 int ip6_mr_input(struct sk_buff *skb)
1651 struct mfc6_cache *cache;
1652 struct net *net = dev_net(skb->dev);
1654 read_lock(&mrt_lock);
1655 cache = ip6mr_cache_find(net,
1656 &ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
1659 * No usable cache entry
1661 if (cache == NULL) {
1664 vif = ip6mr_find_vif(skb->dev);
1666 int err = ip6mr_cache_unresolved(net, vif, skb);
1667 read_unlock(&mrt_lock);
1671 read_unlock(&mrt_lock);
1676 ip6_mr_forward(net, skb, cache);
1678 read_unlock(&mrt_lock);
1685 ip6mr_fill_mroute(struct net *net, struct sk_buff *skb, struct mfc6_cache *c,
1689 struct rtnexthop *nhp;
1690 u8 *b = skb_tail_pointer(skb);
1691 struct rtattr *mp_head;
1693 /* If cache is unresolved, don't try to parse IIF and OIF */
1694 if (c->mf6c_parent > MAXMIFS)
1697 if (MIF_EXISTS(net, c->mf6c_parent))
1698 RTA_PUT(skb, RTA_IIF, 4, &net->ipv6.vif6_table[c->mf6c_parent].dev->ifindex);
1700 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
1702 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
1703 if (MIF_EXISTS(net, ct) && c->mfc_un.res.ttls[ct] < 255) {
1704 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1705 goto rtattr_failure;
1706 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1707 nhp->rtnh_flags = 0;
1708 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
1709 nhp->rtnh_ifindex = net->ipv6.vif6_table[ct].dev->ifindex;
1710 nhp->rtnh_len = sizeof(*nhp);
1713 mp_head->rta_type = RTA_MULTIPATH;
1714 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
1715 rtm->rtm_type = RTN_MULTICAST;
1723 int ip6mr_get_route(struct net *net,
1724 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
1727 struct mfc6_cache *cache;
1728 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
1730 read_lock(&mrt_lock);
1731 cache = ip6mr_cache_find(net, &rt->rt6i_src.addr, &rt->rt6i_dst.addr);
1734 struct sk_buff *skb2;
1735 struct ipv6hdr *iph;
1736 struct net_device *dev;
1740 read_unlock(&mrt_lock);
1745 if (dev == NULL || (vif = ip6mr_find_vif(dev)) < 0) {
1746 read_unlock(&mrt_lock);
1750 /* really correct? */
1751 skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
1753 read_unlock(&mrt_lock);
1757 skb_reset_transport_header(skb2);
1759 skb_put(skb2, sizeof(struct ipv6hdr));
1760 skb_reset_network_header(skb2);
1762 iph = ipv6_hdr(skb2);
1765 iph->flow_lbl[0] = 0;
1766 iph->flow_lbl[1] = 0;
1767 iph->flow_lbl[2] = 0;
1768 iph->payload_len = 0;
1769 iph->nexthdr = IPPROTO_NONE;
1771 ipv6_addr_copy(&iph->saddr, &rt->rt6i_src.addr);
1772 ipv6_addr_copy(&iph->daddr, &rt->rt6i_dst.addr);
1774 err = ip6mr_cache_unresolved(net, vif, skb2);
1775 read_unlock(&mrt_lock);
1780 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
1781 cache->mfc_flags |= MFC_NOTIFY;
1783 err = ip6mr_fill_mroute(net, skb, cache, rtm);
1784 read_unlock(&mrt_lock);