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 <net/protocol.h>
37 #include <linux/skbuff.h>
40 #include <linux/notifier.h>
41 #include <linux/if_arp.h>
42 #include <net/checksum.h>
43 #include <net/netlink.h>
46 #include <net/ip6_route.h>
47 #include <linux/mroute6.h>
48 #include <linux/pim.h>
49 #include <net/addrconf.h>
50 #include <linux/netfilter_ipv6.h>
52 /* Big lock, protecting vif table, mrt cache and mroute socket state.
53 Note that the changes are semaphored via rtnl_lock.
56 static DEFINE_RWLOCK(mrt_lock);
59 * Multicast router control variables
62 static struct mif_device vif6_table[MAXMIFS]; /* Devices */
65 #define MIF_EXISTS(idx) (vif6_table[idx].dev != NULL)
67 static int mroute_do_assert; /* Set in PIM assert */
68 #ifdef CONFIG_IPV6_PIMSM_V2
69 static int mroute_do_pim;
71 #define mroute_do_pim 0
74 static struct mfc6_cache *mfc6_cache_array[MFC6_LINES]; /* Forwarding cache */
76 static struct mfc6_cache *mfc_unres_queue; /* Queue of unresolved entries */
77 static atomic_t cache_resolve_queue_len; /* Size of unresolved */
79 /* Special spinlock for queue of unresolved entries */
80 static DEFINE_SPINLOCK(mfc_unres_lock);
82 /* We return to original Alan's scheme. Hash table of resolved
83 entries is changed only in process context and protected
84 with weak lock mrt_lock. Queue of unresolved entries is protected
85 with strong spinlock mfc_unres_lock.
87 In this case data path is free of exclusive locks at all.
90 static struct kmem_cache *mrt_cachep __read_mostly;
92 static int ip6_mr_forward(struct sk_buff *skb, struct mfc6_cache *cache);
93 static int ip6mr_cache_report(struct sk_buff *pkt, mifi_t mifi, int assert);
94 static int ip6mr_fill_mroute(struct sk_buff *skb, struct mfc6_cache *c, struct rtmsg *rtm);
96 #ifdef CONFIG_IPV6_PIMSM_V2
97 static struct inet6_protocol pim6_protocol;
100 static struct timer_list ipmr_expire_timer;
103 #ifdef CONFIG_PROC_FS
105 struct ipmr_mfc_iter {
106 struct mfc6_cache **cache;
111 static struct mfc6_cache *ipmr_mfc_seq_idx(struct ipmr_mfc_iter *it, loff_t pos)
113 struct mfc6_cache *mfc;
115 it->cache = mfc6_cache_array;
116 read_lock(&mrt_lock);
117 for (it->ct = 0; it->ct < ARRAY_SIZE(mfc6_cache_array); it->ct++)
118 for (mfc = mfc6_cache_array[it->ct]; mfc; mfc = mfc->next)
121 read_unlock(&mrt_lock);
123 it->cache = &mfc_unres_queue;
124 spin_lock_bh(&mfc_unres_lock);
125 for (mfc = mfc_unres_queue; mfc; mfc = mfc->next)
128 spin_unlock_bh(&mfc_unres_lock);
138 * The /proc interfaces to multicast routing /proc/ip6_mr_cache /proc/ip6_mr_vif
141 struct ipmr_vif_iter {
145 static struct mif_device *ip6mr_vif_seq_idx(struct ipmr_vif_iter *iter,
148 for (iter->ct = 0; iter->ct < maxvif; ++iter->ct) {
149 if (!MIF_EXISTS(iter->ct))
152 return &vif6_table[iter->ct];
157 static void *ip6mr_vif_seq_start(struct seq_file *seq, loff_t *pos)
160 read_lock(&mrt_lock);
161 return (*pos ? ip6mr_vif_seq_idx(seq->private, *pos - 1)
165 static void *ip6mr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
167 struct ipmr_vif_iter *iter = seq->private;
170 if (v == SEQ_START_TOKEN)
171 return ip6mr_vif_seq_idx(iter, 0);
173 while (++iter->ct < maxvif) {
174 if (!MIF_EXISTS(iter->ct))
176 return &vif6_table[iter->ct];
181 static void ip6mr_vif_seq_stop(struct seq_file *seq, void *v)
184 read_unlock(&mrt_lock);
187 static int ip6mr_vif_seq_show(struct seq_file *seq, void *v)
189 if (v == SEQ_START_TOKEN) {
191 "Interface BytesIn PktsIn BytesOut PktsOut Flags\n");
193 const struct mif_device *vif = v;
194 const char *name = vif->dev ? vif->dev->name : "none";
197 "%2td %-10s %8ld %7ld %8ld %7ld %05X\n",
199 name, vif->bytes_in, vif->pkt_in,
200 vif->bytes_out, vif->pkt_out,
206 static struct seq_operations ip6mr_vif_seq_ops = {
207 .start = ip6mr_vif_seq_start,
208 .next = ip6mr_vif_seq_next,
209 .stop = ip6mr_vif_seq_stop,
210 .show = ip6mr_vif_seq_show,
213 static int ip6mr_vif_open(struct inode *inode, struct file *file)
215 return seq_open_private(file, &ip6mr_vif_seq_ops,
216 sizeof(struct ipmr_vif_iter));
219 static struct file_operations ip6mr_vif_fops = {
220 .owner = THIS_MODULE,
221 .open = ip6mr_vif_open,
224 .release = seq_release_private,
227 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
229 return (*pos ? ipmr_mfc_seq_idx(seq->private, *pos - 1)
233 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
235 struct mfc6_cache *mfc = v;
236 struct ipmr_mfc_iter *it = seq->private;
240 if (v == SEQ_START_TOKEN)
241 return ipmr_mfc_seq_idx(seq->private, 0);
246 if (it->cache == &mfc_unres_queue)
249 BUG_ON(it->cache != mfc6_cache_array);
251 while (++it->ct < ARRAY_SIZE(mfc6_cache_array)) {
252 mfc = mfc6_cache_array[it->ct];
257 /* exhausted cache_array, show unresolved */
258 read_unlock(&mrt_lock);
259 it->cache = &mfc_unres_queue;
262 spin_lock_bh(&mfc_unres_lock);
263 mfc = mfc_unres_queue;
268 spin_unlock_bh(&mfc_unres_lock);
274 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
276 struct ipmr_mfc_iter *it = seq->private;
278 if (it->cache == &mfc_unres_queue)
279 spin_unlock_bh(&mfc_unres_lock);
280 else if (it->cache == mfc6_cache_array)
281 read_unlock(&mrt_lock);
284 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
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 != &mfc_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++) {
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 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_private(file, &ipmr_mfc_seq_ops,
335 sizeof(struct ipmr_mfc_iter));
338 static struct file_operations ip6mr_mfc_fops = {
339 .owner = THIS_MODULE,
340 .open = ipmr_mfc_open,
343 .release = seq_release_private,
347 #ifdef CONFIG_IPV6_PIMSM_V2
348 static int reg_vif_num = -1;
350 static int pim6_rcv(struct sk_buff *skb)
352 struct pimreghdr *pim;
353 struct ipv6hdr *encap;
354 struct net_device *reg_dev = NULL;
356 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(*encap)))
359 pim = (struct pimreghdr *)skb_transport_header(skb);
360 if (pim->type != ((PIM_VERSION << 4) | PIM_REGISTER) ||
361 (pim->flags & PIM_NULL_REGISTER) ||
362 (ip_compute_csum((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 = 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_IP);
391 skb->pkt_type = PACKET_HOST;
392 dst_release(skb->dst);
393 reg_dev->stats.rx_bytes += skb->len;
394 reg_dev->stats.rx_packets++;
405 static struct inet6_protocol pim6_protocol = {
409 /* Service routines creating virtual interfaces: PIMREG */
411 static int reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
413 read_lock(&mrt_lock);
414 dev->stats.tx_bytes += skb->len;
415 dev->stats.tx_packets++;
416 ip6mr_cache_report(skb, reg_vif_num, MRT6MSG_WHOLEPKT);
417 read_unlock(&mrt_lock);
422 static const struct net_device_ops reg_vif_netdev_ops = {
423 .ndo_start_xmit = reg_vif_xmit,
426 static void reg_vif_setup(struct net_device *dev)
428 dev->type = ARPHRD_PIMREG;
429 dev->mtu = 1500 - sizeof(struct ipv6hdr) - 8;
430 dev->flags = IFF_NOARP;
431 dev->netdev_ops = ®_vif_netdev_ops;
432 dev->destructor = free_netdev;
435 static struct net_device *ip6mr_reg_vif(void)
437 struct net_device *dev;
439 dev = alloc_netdev(0, "pim6reg", reg_vif_setup);
443 if (register_netdevice(dev)) {
456 /* allow the register to be completed before unregistering. */
460 unregister_netdevice(dev);
469 static int mif6_delete(int vifi)
471 struct mif_device *v;
472 struct net_device *dev;
473 if (vifi < 0 || vifi >= maxvif)
474 return -EADDRNOTAVAIL;
476 v = &vif6_table[vifi];
478 write_lock_bh(&mrt_lock);
483 write_unlock_bh(&mrt_lock);
484 return -EADDRNOTAVAIL;
487 #ifdef CONFIG_IPV6_PIMSM_V2
488 if (vifi == reg_vif_num)
492 if (vifi + 1 == maxvif) {
494 for (tmp = vifi - 1; tmp >= 0; tmp--) {
501 write_unlock_bh(&mrt_lock);
503 dev_set_allmulti(dev, -1);
505 if (v->flags & MIFF_REGISTER)
506 unregister_netdevice(dev);
512 /* Destroy an unresolved cache entry, killing queued skbs
513 and reporting error to netlink readers.
516 static void ip6mr_destroy_unres(struct mfc6_cache *c)
520 atomic_dec(&cache_resolve_queue_len);
522 while((skb = skb_dequeue(&c->mfc_un.unres.unresolved)) != NULL) {
523 if (ipv6_hdr(skb)->version == 0) {
524 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
525 nlh->nlmsg_type = NLMSG_ERROR;
526 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
527 skb_trim(skb, nlh->nlmsg_len);
528 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -ETIMEDOUT;
529 rtnl_unicast(skb, &init_net, NETLINK_CB(skb).pid);
534 kmem_cache_free(mrt_cachep, c);
538 /* Single timer process for all the unresolved queue. */
540 static void ipmr_do_expire_process(unsigned long dummy)
542 unsigned long now = jiffies;
543 unsigned long expires = 10 * HZ;
544 struct mfc6_cache *c, **cp;
546 cp = &mfc_unres_queue;
548 while ((c = *cp) != NULL) {
549 if (time_after(c->mfc_un.unres.expires, now)) {
551 unsigned long interval = c->mfc_un.unres.expires - now;
552 if (interval < expires)
559 ip6mr_destroy_unres(c);
562 if (atomic_read(&cache_resolve_queue_len))
563 mod_timer(&ipmr_expire_timer, jiffies + expires);
566 static void ipmr_expire_process(unsigned long dummy)
568 if (!spin_trylock(&mfc_unres_lock)) {
569 mod_timer(&ipmr_expire_timer, jiffies + 1);
573 if (atomic_read(&cache_resolve_queue_len))
574 ipmr_do_expire_process(dummy);
576 spin_unlock(&mfc_unres_lock);
579 /* Fill oifs list. It is called under write locked mrt_lock. */
581 static void ip6mr_update_thresholds(struct mfc6_cache *cache, unsigned char *ttls)
585 cache->mfc_un.res.minvif = MAXMIFS;
586 cache->mfc_un.res.maxvif = 0;
587 memset(cache->mfc_un.res.ttls, 255, MAXMIFS);
589 for (vifi = 0; vifi < maxvif; vifi++) {
590 if (MIF_EXISTS(vifi) && ttls[vifi] && ttls[vifi] < 255) {
591 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
592 if (cache->mfc_un.res.minvif > vifi)
593 cache->mfc_un.res.minvif = vifi;
594 if (cache->mfc_un.res.maxvif <= vifi)
595 cache->mfc_un.res.maxvif = vifi + 1;
600 static int mif6_add(struct mif6ctl *vifc, int mrtsock)
602 int vifi = vifc->mif6c_mifi;
603 struct mif_device *v = &vif6_table[vifi];
604 struct net_device *dev;
608 if (MIF_EXISTS(vifi))
611 switch (vifc->mif6c_flags) {
612 #ifdef CONFIG_IPV6_PIMSM_V2
615 * Special Purpose VIF in PIM
616 * All the packets will be sent to the daemon
618 if (reg_vif_num >= 0)
620 dev = ip6mr_reg_vif();
623 err = dev_set_allmulti(dev, 1);
625 unregister_netdevice(dev);
632 dev = dev_get_by_index(&init_net, vifc->mif6c_pifi);
634 return -EADDRNOTAVAIL;
635 err = dev_set_allmulti(dev, 1);
646 * Fill in the VIF structures
648 v->rate_limit = vifc->vifc_rate_limit;
649 v->flags = vifc->mif6c_flags;
651 v->flags |= VIFF_STATIC;
652 v->threshold = vifc->vifc_threshold;
657 v->link = dev->ifindex;
658 if (v->flags & MIFF_REGISTER)
659 v->link = dev->iflink;
661 /* And finish update writing critical data */
662 write_lock_bh(&mrt_lock);
664 #ifdef CONFIG_IPV6_PIMSM_V2
665 if (v->flags & MIFF_REGISTER)
668 if (vifi + 1 > maxvif)
670 write_unlock_bh(&mrt_lock);
674 static struct mfc6_cache *ip6mr_cache_find(struct in6_addr *origin, struct in6_addr *mcastgrp)
676 int line = MFC6_HASH(mcastgrp, origin);
677 struct mfc6_cache *c;
679 for (c = mfc6_cache_array[line]; c; c = c->next) {
680 if (ipv6_addr_equal(&c->mf6c_origin, origin) &&
681 ipv6_addr_equal(&c->mf6c_mcastgrp, mcastgrp))
688 * Allocate a multicast cache entry
690 static struct mfc6_cache *ip6mr_cache_alloc(void)
692 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
695 c->mfc_un.res.minvif = MAXMIFS;
699 static struct mfc6_cache *ip6mr_cache_alloc_unres(void)
701 struct mfc6_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
704 skb_queue_head_init(&c->mfc_un.unres.unresolved);
705 c->mfc_un.unres.expires = jiffies + 10 * HZ;
710 * A cache entry has gone into a resolved state from queued
713 static void ip6mr_cache_resolve(struct mfc6_cache *uc, struct mfc6_cache *c)
718 * Play the pending entries through our router
721 while((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
722 if (ipv6_hdr(skb)->version == 0) {
724 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct ipv6hdr));
726 if (ip6mr_fill_mroute(skb, c, NLMSG_DATA(nlh)) > 0) {
727 nlh->nlmsg_len = skb_tail_pointer(skb) - (u8 *)nlh;
729 nlh->nlmsg_type = NLMSG_ERROR;
730 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
731 skb_trim(skb, nlh->nlmsg_len);
732 ((struct nlmsgerr *)NLMSG_DATA(nlh))->error = -EMSGSIZE;
734 err = rtnl_unicast(skb, &init_net, NETLINK_CB(skb).pid);
736 ip6_mr_forward(skb, c);
741 * Bounce a cache query up to pim6sd. We could use netlink for this but pim6sd
742 * expects the following bizarre scheme.
744 * Called under mrt_lock.
747 static int ip6mr_cache_report(struct sk_buff *pkt, mifi_t mifi, int assert)
753 #ifdef CONFIG_IPV6_PIMSM_V2
754 if (assert == MRT6MSG_WHOLEPKT)
755 skb = skb_realloc_headroom(pkt, -skb_network_offset(pkt)
759 skb = alloc_skb(sizeof(struct ipv6hdr) + sizeof(*msg), GFP_ATOMIC);
764 /* I suppose that internal messages
765 * do not require checksums */
767 skb->ip_summed = CHECKSUM_UNNECESSARY;
769 #ifdef CONFIG_IPV6_PIMSM_V2
770 if (assert == MRT6MSG_WHOLEPKT) {
771 /* Ugly, but we have no choice with this interface.
772 Duplicate old header, fix length etc.
773 And all this only to mangle msg->im6_msgtype and
774 to set msg->im6_mbz to "mbz" :-)
776 skb_push(skb, -skb_network_offset(pkt));
778 skb_push(skb, sizeof(*msg));
779 skb_reset_transport_header(skb);
780 msg = (struct mrt6msg *)skb_transport_header(skb);
782 msg->im6_msgtype = MRT6MSG_WHOLEPKT;
783 msg->im6_mif = reg_vif_num;
785 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
786 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
788 skb->ip_summed = CHECKSUM_UNNECESSARY;
796 skb_put(skb, sizeof(struct ipv6hdr));
797 skb_reset_network_header(skb);
798 skb_copy_to_linear_data(skb, ipv6_hdr(pkt), sizeof(struct ipv6hdr));
803 skb_put(skb, sizeof(*msg));
804 skb_reset_transport_header(skb);
805 msg = (struct mrt6msg *)skb_transport_header(skb);
808 msg->im6_msgtype = assert;
811 ipv6_addr_copy(&msg->im6_src, &ipv6_hdr(pkt)->saddr);
812 ipv6_addr_copy(&msg->im6_dst, &ipv6_hdr(pkt)->daddr);
814 skb->dst = dst_clone(pkt->dst);
815 skb->ip_summed = CHECKSUM_UNNECESSARY;
817 skb_pull(skb, sizeof(struct ipv6hdr));
820 if (init_net.ipv6.mroute6_sk == NULL) {
826 * Deliver to user space multicast routing algorithms
828 ret = sock_queue_rcv_skb(init_net.ipv6.mroute6_sk, skb);
831 printk(KERN_WARNING "mroute6: pending queue full, dropping entries.\n");
839 * Queue a packet for resolution. It gets locked cache entry!
843 ip6mr_cache_unresolved(mifi_t mifi, struct sk_buff *skb)
846 struct mfc6_cache *c;
848 spin_lock_bh(&mfc_unres_lock);
849 for (c = mfc_unres_queue; c; c = c->next) {
850 if (ipv6_addr_equal(&c->mf6c_mcastgrp, &ipv6_hdr(skb)->daddr) &&
851 ipv6_addr_equal(&c->mf6c_origin, &ipv6_hdr(skb)->saddr))
857 * Create a new entry if allowable
860 if (atomic_read(&cache_resolve_queue_len) >= 10 ||
861 (c = ip6mr_cache_alloc_unres()) == NULL) {
862 spin_unlock_bh(&mfc_unres_lock);
869 * Fill in the new cache entry
872 c->mf6c_origin = ipv6_hdr(skb)->saddr;
873 c->mf6c_mcastgrp = ipv6_hdr(skb)->daddr;
876 * Reflect first query at pim6sd
878 if ((err = ip6mr_cache_report(skb, mifi, MRT6MSG_NOCACHE)) < 0) {
879 /* If the report failed throw the cache entry
882 spin_unlock_bh(&mfc_unres_lock);
884 kmem_cache_free(mrt_cachep, c);
889 atomic_inc(&cache_resolve_queue_len);
890 c->next = mfc_unres_queue;
893 ipmr_do_expire_process(1);
897 * See if we can append the packet
899 if (c->mfc_un.unres.unresolved.qlen > 3) {
903 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
907 spin_unlock_bh(&mfc_unres_lock);
912 * MFC6 cache manipulation by user space
915 static int ip6mr_mfc_delete(struct mf6cctl *mfc)
918 struct mfc6_cache *c, **cp;
920 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
922 for (cp = &mfc6_cache_array[line]; (c = *cp) != NULL; cp = &c->next) {
923 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
924 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr)) {
925 write_lock_bh(&mrt_lock);
927 write_unlock_bh(&mrt_lock);
929 kmem_cache_free(mrt_cachep, c);
936 static int ip6mr_device_event(struct notifier_block *this,
937 unsigned long event, void *ptr)
939 struct net_device *dev = ptr;
940 struct mif_device *v;
943 if (!net_eq(dev_net(dev), &init_net))
946 if (event != NETDEV_UNREGISTER)
950 for (ct = 0; ct < maxvif; ct++, v++) {
957 static struct notifier_block ip6_mr_notifier = {
958 .notifier_call = ip6mr_device_event
962 * Setup for IP multicast routing
965 int __init ip6_mr_init(void)
969 mrt_cachep = kmem_cache_create("ip6_mrt_cache",
970 sizeof(struct mfc6_cache),
971 0, SLAB_HWCACHE_ALIGN,
976 setup_timer(&ipmr_expire_timer, ipmr_expire_process, 0);
977 err = register_netdevice_notifier(&ip6_mr_notifier);
980 #ifdef CONFIG_PROC_FS
982 if (!proc_net_fops_create(&init_net, "ip6_mr_vif", 0, &ip6mr_vif_fops))
984 if (!proc_net_fops_create(&init_net, "ip6_mr_cache",
986 goto proc_cache_fail;
989 #ifdef CONFIG_PROC_FS
991 proc_net_remove(&init_net, "ip6_mr_vif");
993 unregister_netdevice_notifier(&ip6_mr_notifier);
996 del_timer(&ipmr_expire_timer);
997 kmem_cache_destroy(mrt_cachep);
1001 void ip6_mr_cleanup(void)
1003 #ifdef CONFIG_PROC_FS
1004 proc_net_remove(&init_net, "ip6_mr_cache");
1005 proc_net_remove(&init_net, "ip6_mr_vif");
1007 unregister_netdevice_notifier(&ip6_mr_notifier);
1008 del_timer(&ipmr_expire_timer);
1009 kmem_cache_destroy(mrt_cachep);
1012 static int ip6mr_mfc_add(struct mf6cctl *mfc, int mrtsock)
1015 struct mfc6_cache *uc, *c, **cp;
1016 unsigned char ttls[MAXMIFS];
1019 memset(ttls, 255, MAXMIFS);
1020 for (i = 0; i < MAXMIFS; i++) {
1021 if (IF_ISSET(i, &mfc->mf6cc_ifset))
1026 line = MFC6_HASH(&mfc->mf6cc_mcastgrp.sin6_addr, &mfc->mf6cc_origin.sin6_addr);
1028 for (cp = &mfc6_cache_array[line]; (c = *cp) != NULL; cp = &c->next) {
1029 if (ipv6_addr_equal(&c->mf6c_origin, &mfc->mf6cc_origin.sin6_addr) &&
1030 ipv6_addr_equal(&c->mf6c_mcastgrp, &mfc->mf6cc_mcastgrp.sin6_addr))
1035 write_lock_bh(&mrt_lock);
1036 c->mf6c_parent = mfc->mf6cc_parent;
1037 ip6mr_update_thresholds(c, ttls);
1039 c->mfc_flags |= MFC_STATIC;
1040 write_unlock_bh(&mrt_lock);
1044 if (!ipv6_addr_is_multicast(&mfc->mf6cc_mcastgrp.sin6_addr))
1047 c = ip6mr_cache_alloc();
1051 c->mf6c_origin = mfc->mf6cc_origin.sin6_addr;
1052 c->mf6c_mcastgrp = mfc->mf6cc_mcastgrp.sin6_addr;
1053 c->mf6c_parent = mfc->mf6cc_parent;
1054 ip6mr_update_thresholds(c, ttls);
1056 c->mfc_flags |= MFC_STATIC;
1058 write_lock_bh(&mrt_lock);
1059 c->next = mfc6_cache_array[line];
1060 mfc6_cache_array[line] = c;
1061 write_unlock_bh(&mrt_lock);
1064 * Check to see if we resolved a queued list. If so we
1065 * need to send on the frames and tidy up.
1067 spin_lock_bh(&mfc_unres_lock);
1068 for (cp = &mfc_unres_queue; (uc = *cp) != NULL;
1070 if (ipv6_addr_equal(&uc->mf6c_origin, &c->mf6c_origin) &&
1071 ipv6_addr_equal(&uc->mf6c_mcastgrp, &c->mf6c_mcastgrp)) {
1073 if (atomic_dec_and_test(&cache_resolve_queue_len))
1074 del_timer(&ipmr_expire_timer);
1078 spin_unlock_bh(&mfc_unres_lock);
1081 ip6mr_cache_resolve(uc, c);
1082 kmem_cache_free(mrt_cachep, uc);
1088 * Close the multicast socket, and clear the vif tables etc
1091 static void mroute_clean_tables(struct sock *sk)
1096 * Shut down all active vif entries
1098 for (i = 0; i < maxvif; i++) {
1099 if (!(vif6_table[i].flags & VIFF_STATIC))
1106 for (i = 0; i < ARRAY_SIZE(mfc6_cache_array); i++) {
1107 struct mfc6_cache *c, **cp;
1109 cp = &mfc6_cache_array[i];
1110 while ((c = *cp) != NULL) {
1111 if (c->mfc_flags & MFC_STATIC) {
1115 write_lock_bh(&mrt_lock);
1117 write_unlock_bh(&mrt_lock);
1119 kmem_cache_free(mrt_cachep, c);
1123 if (atomic_read(&cache_resolve_queue_len) != 0) {
1124 struct mfc6_cache *c;
1126 spin_lock_bh(&mfc_unres_lock);
1127 while (mfc_unres_queue != NULL) {
1128 c = mfc_unres_queue;
1129 mfc_unres_queue = c->next;
1130 spin_unlock_bh(&mfc_unres_lock);
1132 ip6mr_destroy_unres(c);
1134 spin_lock_bh(&mfc_unres_lock);
1136 spin_unlock_bh(&mfc_unres_lock);
1140 static int ip6mr_sk_init(struct sock *sk)
1145 write_lock_bh(&mrt_lock);
1146 if (likely(init_net.ipv6.mroute6_sk == NULL))
1147 init_net.ipv6.mroute6_sk = sk;
1150 write_unlock_bh(&mrt_lock);
1157 int ip6mr_sk_done(struct sock *sk)
1162 if (sk == init_net.ipv6.mroute6_sk) {
1163 write_lock_bh(&mrt_lock);
1164 init_net.ipv6.mroute6_sk = NULL;
1165 write_unlock_bh(&mrt_lock);
1167 mroute_clean_tables(sk);
1176 * Socket options and virtual interface manipulation. The whole
1177 * virtual interface system is a complete heap, but unfortunately
1178 * that's how BSD mrouted happens to think. Maybe one day with a proper
1179 * MOSPF/PIM router set up we can clean this up.
1182 int ip6_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, int optlen)
1189 if (optname != MRT6_INIT) {
1190 if (sk != init_net.ipv6.mroute6_sk && !capable(CAP_NET_ADMIN))
1196 if (sk->sk_type != SOCK_RAW ||
1197 inet_sk(sk)->num != IPPROTO_ICMPV6)
1199 if (optlen < sizeof(int))
1202 return ip6mr_sk_init(sk);
1205 return ip6mr_sk_done(sk);
1208 if (optlen < sizeof(vif))
1210 if (copy_from_user(&vif, optval, sizeof(vif)))
1212 if (vif.mif6c_mifi >= MAXMIFS)
1215 ret = mif6_add(&vif, sk == init_net.ipv6.mroute6_sk);
1220 if (optlen < sizeof(mifi_t))
1222 if (copy_from_user(&mifi, optval, sizeof(mifi_t)))
1225 ret = mif6_delete(mifi);
1230 * Manipulate the forwarding caches. These live
1231 * in a sort of kernel/user symbiosis.
1235 if (optlen < sizeof(mfc))
1237 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1240 if (optname == MRT6_DEL_MFC)
1241 ret = ip6mr_mfc_delete(&mfc);
1243 ret = ip6mr_mfc_add(&mfc, sk == init_net.ipv6.mroute6_sk);
1248 * Control PIM assert (to activate pim will activate assert)
1253 if (get_user(v, (int __user *)optval))
1255 mroute_do_assert = !!v;
1259 #ifdef CONFIG_IPV6_PIMSM_V2
1263 if (get_user(v, (int __user *)optval))
1268 if (v != mroute_do_pim) {
1270 mroute_do_assert = v;
1272 ret = inet6_add_protocol(&pim6_protocol,
1275 ret = inet6_del_protocol(&pim6_protocol,
1286 * Spurious command, or MRT6_VERSION which you cannot
1290 return -ENOPROTOOPT;
1295 * Getsock opt support for the multicast routing system.
1298 int ip6_mroute_getsockopt(struct sock *sk, int optname, char __user *optval,
1308 #ifdef CONFIG_IPV6_PIMSM_V2
1310 val = mroute_do_pim;
1314 val = mroute_do_assert;
1317 return -ENOPROTOOPT;
1320 if (get_user(olr, optlen))
1323 olr = min_t(int, olr, sizeof(int));
1327 if (put_user(olr, optlen))
1329 if (copy_to_user(optval, &val, olr))
1335 * The IP multicast ioctl support routines.
1338 int ip6mr_ioctl(struct sock *sk, int cmd, void __user *arg)
1340 struct sioc_sg_req6 sr;
1341 struct sioc_mif_req6 vr;
1342 struct mif_device *vif;
1343 struct mfc6_cache *c;
1346 case SIOCGETMIFCNT_IN6:
1347 if (copy_from_user(&vr, arg, sizeof(vr)))
1349 if (vr.mifi >= maxvif)
1351 read_lock(&mrt_lock);
1352 vif = &vif6_table[vr.mifi];
1353 if (MIF_EXISTS(vr.mifi)) {
1354 vr.icount = vif->pkt_in;
1355 vr.ocount = vif->pkt_out;
1356 vr.ibytes = vif->bytes_in;
1357 vr.obytes = vif->bytes_out;
1358 read_unlock(&mrt_lock);
1360 if (copy_to_user(arg, &vr, sizeof(vr)))
1364 read_unlock(&mrt_lock);
1365 return -EADDRNOTAVAIL;
1366 case SIOCGETSGCNT_IN6:
1367 if (copy_from_user(&sr, arg, sizeof(sr)))
1370 read_lock(&mrt_lock);
1371 c = ip6mr_cache_find(&sr.src.sin6_addr, &sr.grp.sin6_addr);
1373 sr.pktcnt = c->mfc_un.res.pkt;
1374 sr.bytecnt = c->mfc_un.res.bytes;
1375 sr.wrong_if = c->mfc_un.res.wrong_if;
1376 read_unlock(&mrt_lock);
1378 if (copy_to_user(arg, &sr, sizeof(sr)))
1382 read_unlock(&mrt_lock);
1383 return -EADDRNOTAVAIL;
1385 return -ENOIOCTLCMD;
1390 static inline int ip6mr_forward2_finish(struct sk_buff *skb)
1392 IP6_INC_STATS_BH(dev_net(skb->dst->dev), ip6_dst_idev(skb->dst),
1393 IPSTATS_MIB_OUTFORWDATAGRAMS);
1394 return dst_output(skb);
1398 * Processing handlers for ip6mr_forward
1401 static int ip6mr_forward2(struct sk_buff *skb, struct mfc6_cache *c, int vifi)
1403 struct ipv6hdr *ipv6h;
1404 struct mif_device *vif = &vif6_table[vifi];
1405 struct net_device *dev;
1406 struct dst_entry *dst;
1409 if (vif->dev == NULL)
1412 #ifdef CONFIG_IPV6_PIMSM_V2
1413 if (vif->flags & MIFF_REGISTER) {
1415 vif->bytes_out += skb->len;
1416 vif->dev->stats.tx_bytes += skb->len;
1417 vif->dev->stats.tx_packets++;
1418 ip6mr_cache_report(skb, vifi, MRT6MSG_WHOLEPKT);
1424 ipv6h = ipv6_hdr(skb);
1426 fl = (struct flowi) {
1429 { .daddr = ipv6h->daddr, }
1433 dst = ip6_route_output(&init_net, NULL, &fl);
1437 dst_release(skb->dst);
1441 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1442 * not only before forwarding, but after forwarding on all output
1443 * interfaces. It is clear, if mrouter runs a multicasting
1444 * program, it should receive packets not depending to what interface
1445 * program is joined.
1446 * If we will not make it, the program will have to join on all
1447 * interfaces. On the other hand, multihoming host (or router, but
1448 * not mrouter) cannot join to more than one interface - it will
1449 * result in receiving multiple packets.
1454 vif->bytes_out += skb->len;
1456 /* We are about to write */
1457 /* XXX: extension headers? */
1458 if (skb_cow(skb, sizeof(*ipv6h) + LL_RESERVED_SPACE(dev)))
1461 ipv6h = ipv6_hdr(skb);
1464 IP6CB(skb)->flags |= IP6SKB_FORWARDED;
1466 return NF_HOOK(PF_INET6, NF_INET_FORWARD, skb, skb->dev, dev,
1467 ip6mr_forward2_finish);
1474 static int ip6mr_find_vif(struct net_device *dev)
1477 for (ct = maxvif - 1; ct >= 0; ct--) {
1478 if (vif6_table[ct].dev == dev)
1484 static int ip6_mr_forward(struct sk_buff *skb, struct mfc6_cache *cache)
1489 vif = cache->mf6c_parent;
1490 cache->mfc_un.res.pkt++;
1491 cache->mfc_un.res.bytes += skb->len;
1494 * Wrong interface: drop packet and (maybe) send PIM assert.
1496 if (vif6_table[vif].dev != skb->dev) {
1499 cache->mfc_un.res.wrong_if++;
1500 true_vifi = ip6mr_find_vif(skb->dev);
1502 if (true_vifi >= 0 && mroute_do_assert &&
1503 /* pimsm uses asserts, when switching from RPT to SPT,
1504 so that we cannot check that packet arrived on an oif.
1505 It is bad, but otherwise we would need to move pretty
1506 large chunk of pimd to kernel. Ough... --ANK
1508 (mroute_do_pim || cache->mfc_un.res.ttls[true_vifi] < 255) &&
1510 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1511 cache->mfc_un.res.last_assert = jiffies;
1512 ip6mr_cache_report(skb, true_vifi, MRT6MSG_WRONGMIF);
1517 vif6_table[vif].pkt_in++;
1518 vif6_table[vif].bytes_in += skb->len;
1523 for (ct = cache->mfc_un.res.maxvif - 1; ct >= cache->mfc_un.res.minvif; ct--) {
1524 if (ipv6_hdr(skb)->hop_limit > cache->mfc_un.res.ttls[ct]) {
1526 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1528 ip6mr_forward2(skb2, cache, psend);
1534 ip6mr_forward2(skb, cache, psend);
1545 * Multicast packets for forwarding arrive here
1548 int ip6_mr_input(struct sk_buff *skb)
1550 struct mfc6_cache *cache;
1552 read_lock(&mrt_lock);
1553 cache = ip6mr_cache_find(&ipv6_hdr(skb)->saddr, &ipv6_hdr(skb)->daddr);
1556 * No usable cache entry
1558 if (cache == NULL) {
1561 vif = ip6mr_find_vif(skb->dev);
1563 int err = ip6mr_cache_unresolved(vif, skb);
1564 read_unlock(&mrt_lock);
1568 read_unlock(&mrt_lock);
1573 ip6_mr_forward(skb, cache);
1575 read_unlock(&mrt_lock);
1582 ip6mr_fill_mroute(struct sk_buff *skb, struct mfc6_cache *c, struct rtmsg *rtm)
1585 struct rtnexthop *nhp;
1586 struct net_device *dev = vif6_table[c->mf6c_parent].dev;
1587 u8 *b = skb_tail_pointer(skb);
1588 struct rtattr *mp_head;
1591 RTA_PUT(skb, RTA_IIF, 4, &dev->ifindex);
1593 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
1595 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
1596 if (c->mfc_un.res.ttls[ct] < 255) {
1597 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1598 goto rtattr_failure;
1599 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1600 nhp->rtnh_flags = 0;
1601 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
1602 nhp->rtnh_ifindex = vif6_table[ct].dev->ifindex;
1603 nhp->rtnh_len = sizeof(*nhp);
1606 mp_head->rta_type = RTA_MULTIPATH;
1607 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
1608 rtm->rtm_type = RTN_MULTICAST;
1616 int ip6mr_get_route(struct sk_buff *skb, struct rtmsg *rtm, int nowait)
1619 struct mfc6_cache *cache;
1620 struct rt6_info *rt = (struct rt6_info *)skb->dst;
1622 read_lock(&mrt_lock);
1623 cache = ip6mr_cache_find(&rt->rt6i_src.addr, &rt->rt6i_dst.addr);
1626 struct sk_buff *skb2;
1627 struct ipv6hdr *iph;
1628 struct net_device *dev;
1632 read_unlock(&mrt_lock);
1637 if (dev == NULL || (vif = ip6mr_find_vif(dev)) < 0) {
1638 read_unlock(&mrt_lock);
1642 /* really correct? */
1643 skb2 = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
1645 read_unlock(&mrt_lock);
1649 skb_reset_transport_header(skb2);
1651 skb_put(skb2, sizeof(struct ipv6hdr));
1652 skb_reset_network_header(skb2);
1654 iph = ipv6_hdr(skb2);
1657 iph->flow_lbl[0] = 0;
1658 iph->flow_lbl[1] = 0;
1659 iph->flow_lbl[2] = 0;
1660 iph->payload_len = 0;
1661 iph->nexthdr = IPPROTO_NONE;
1663 ipv6_addr_copy(&iph->saddr, &rt->rt6i_src.addr);
1664 ipv6_addr_copy(&iph->daddr, &rt->rt6i_dst.addr);
1666 err = ip6mr_cache_unresolved(vif, skb2);
1667 read_unlock(&mrt_lock);
1672 if (!nowait && (rtm->rtm_flags&RTM_F_NOTIFY))
1673 cache->mfc_flags |= MFC_NOTIFY;
1675 err = ip6mr_fill_mroute(skb, cache, rtm);
1676 read_unlock(&mrt_lock);