2 * IP multicast routing support for mrouted 3.6/3.8
5 * Linux Consultancy and Custom Driver Development
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
25 * Relax this requirement to work with older peers.
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31 #include <linux/types.h>
32 #include <linux/capability.h>
33 #include <linux/errno.h>
34 #include <linux/timer.h>
36 #include <linux/kernel.h>
37 #include <linux/fcntl.h>
38 #include <linux/stat.h>
39 #include <linux/socket.h>
41 #include <linux/inet.h>
42 #include <linux/netdevice.h>
43 #include <linux/inetdevice.h>
44 #include <linux/igmp.h>
45 #include <linux/proc_fs.h>
46 #include <linux/seq_file.h>
47 #include <linux/mroute.h>
48 #include <linux/init.h>
49 #include <linux/if_ether.h>
50 #include <linux/slab.h>
51 #include <net/net_namespace.h>
53 #include <net/protocol.h>
54 #include <linux/skbuff.h>
55 #include <net/route.h>
60 #include <linux/notifier.h>
61 #include <linux/if_arp.h>
62 #include <linux/netfilter_ipv4.h>
64 #include <net/checksum.h>
65 #include <net/netlink.h>
66 #include <net/fib_rules.h>
68 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
69 #define CONFIG_IP_PIMSM 1
73 struct list_head list;
78 struct sock __rcu *mroute_sk;
79 struct timer_list ipmr_expire_timer;
80 struct list_head mfc_unres_queue;
81 struct list_head mfc_cache_array[MFC_LINES];
82 struct vif_device vif_table[MAXVIFS];
84 atomic_t cache_resolve_queue_len;
87 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
88 int mroute_reg_vif_num;
93 struct fib_rule common;
100 /* Big lock, protecting vif table, mrt cache and mroute socket state.
101 * Note that the changes are semaphored via rtnl_lock.
104 static DEFINE_RWLOCK(mrt_lock);
107 * Multicast router control variables
110 #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
112 /* Special spinlock for queue of unresolved entries */
113 static DEFINE_SPINLOCK(mfc_unres_lock);
115 /* We return to original Alan's scheme. Hash table of resolved
116 * entries is changed only in process context and protected
117 * with weak lock mrt_lock. Queue of unresolved entries is protected
118 * with strong spinlock mfc_unres_lock.
120 * In this case data path is free of exclusive locks at all.
123 static struct kmem_cache *mrt_cachep __read_mostly;
125 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
126 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
127 struct sk_buff *skb, struct mfc_cache *cache,
129 static int ipmr_cache_report(struct mr_table *mrt,
130 struct sk_buff *pkt, vifi_t vifi, int assert);
131 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
132 struct mfc_cache *c, struct rtmsg *rtm);
133 static void ipmr_expire_process(unsigned long arg);
135 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
136 #define ipmr_for_each_table(mrt, net) \
137 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
139 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
141 struct mr_table *mrt;
143 ipmr_for_each_table(mrt, net) {
150 static int ipmr_fib_lookup(struct net *net, struct flowi *flp,
151 struct mr_table **mrt)
153 struct ipmr_result res;
154 struct fib_lookup_arg arg = { .result = &res, };
157 err = fib_rules_lookup(net->ipv4.mr_rules_ops, flp, 0, &arg);
164 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
165 int flags, struct fib_lookup_arg *arg)
167 struct ipmr_result *res = arg->result;
168 struct mr_table *mrt;
170 switch (rule->action) {
173 case FR_ACT_UNREACHABLE:
175 case FR_ACT_PROHIBIT:
177 case FR_ACT_BLACKHOLE:
182 mrt = ipmr_get_table(rule->fr_net, rule->table);
189 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
194 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
198 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
199 struct fib_rule_hdr *frh, struct nlattr **tb)
204 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
210 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
211 struct fib_rule_hdr *frh)
219 static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
220 .family = RTNL_FAMILY_IPMR,
221 .rule_size = sizeof(struct ipmr_rule),
222 .addr_size = sizeof(u32),
223 .action = ipmr_rule_action,
224 .match = ipmr_rule_match,
225 .configure = ipmr_rule_configure,
226 .compare = ipmr_rule_compare,
227 .default_pref = fib_default_rule_pref,
228 .fill = ipmr_rule_fill,
229 .nlgroup = RTNLGRP_IPV4_RULE,
230 .policy = ipmr_rule_policy,
231 .owner = THIS_MODULE,
234 static int __net_init ipmr_rules_init(struct net *net)
236 struct fib_rules_ops *ops;
237 struct mr_table *mrt;
240 ops = fib_rules_register(&ipmr_rules_ops_template, net);
244 INIT_LIST_HEAD(&net->ipv4.mr_tables);
246 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
252 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
256 net->ipv4.mr_rules_ops = ops;
262 fib_rules_unregister(ops);
266 static void __net_exit ipmr_rules_exit(struct net *net)
268 struct mr_table *mrt, *next;
270 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
271 list_del(&mrt->list);
274 fib_rules_unregister(net->ipv4.mr_rules_ops);
277 #define ipmr_for_each_table(mrt, net) \
278 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
280 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
282 return net->ipv4.mrt;
285 static int ipmr_fib_lookup(struct net *net, struct flowi *flp,
286 struct mr_table **mrt)
288 *mrt = net->ipv4.mrt;
292 static int __net_init ipmr_rules_init(struct net *net)
294 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
295 return net->ipv4.mrt ? 0 : -ENOMEM;
298 static void __net_exit ipmr_rules_exit(struct net *net)
300 kfree(net->ipv4.mrt);
304 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
306 struct mr_table *mrt;
309 mrt = ipmr_get_table(net, id);
313 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
316 write_pnet(&mrt->net, net);
319 /* Forwarding cache */
320 for (i = 0; i < MFC_LINES; i++)
321 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
323 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
325 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
328 #ifdef CONFIG_IP_PIMSM
329 mrt->mroute_reg_vif_num = -1;
331 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
332 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
337 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
339 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
341 struct net *net = dev_net(dev);
345 dev = __dev_get_by_name(net, "tunl0");
347 const struct net_device_ops *ops = dev->netdev_ops;
349 struct ip_tunnel_parm p;
351 memset(&p, 0, sizeof(p));
352 p.iph.daddr = v->vifc_rmt_addr.s_addr;
353 p.iph.saddr = v->vifc_lcl_addr.s_addr;
356 p.iph.protocol = IPPROTO_IPIP;
357 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
358 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
360 if (ops->ndo_do_ioctl) {
361 mm_segment_t oldfs = get_fs();
364 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
371 struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
373 struct net_device *dev;
375 dev = __dev_get_by_name(net, "tunl0");
378 const struct net_device_ops *ops = dev->netdev_ops;
381 struct ip_tunnel_parm p;
382 struct in_device *in_dev;
384 memset(&p, 0, sizeof(p));
385 p.iph.daddr = v->vifc_rmt_addr.s_addr;
386 p.iph.saddr = v->vifc_lcl_addr.s_addr;
389 p.iph.protocol = IPPROTO_IPIP;
390 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
391 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
393 if (ops->ndo_do_ioctl) {
394 mm_segment_t oldfs = get_fs();
397 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
405 (dev = __dev_get_by_name(net, p.name)) != NULL) {
406 dev->flags |= IFF_MULTICAST;
408 in_dev = __in_dev_get_rtnl(dev);
412 ipv4_devconf_setall(in_dev);
413 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
423 /* allow the register to be completed before unregistering. */
427 unregister_netdevice(dev);
431 #ifdef CONFIG_IP_PIMSM
433 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
435 struct net *net = dev_net(dev);
436 struct mr_table *mrt;
444 err = ipmr_fib_lookup(net, &fl, &mrt);
450 read_lock(&mrt_lock);
451 dev->stats.tx_bytes += skb->len;
452 dev->stats.tx_packets++;
453 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
454 read_unlock(&mrt_lock);
459 static const struct net_device_ops reg_vif_netdev_ops = {
460 .ndo_start_xmit = reg_vif_xmit,
463 static void reg_vif_setup(struct net_device *dev)
465 dev->type = ARPHRD_PIMREG;
466 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
467 dev->flags = IFF_NOARP;
468 dev->netdev_ops = ®_vif_netdev_ops,
469 dev->destructor = free_netdev;
470 dev->features |= NETIF_F_NETNS_LOCAL;
473 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
475 struct net_device *dev;
476 struct in_device *in_dev;
479 if (mrt->id == RT_TABLE_DEFAULT)
480 sprintf(name, "pimreg");
482 sprintf(name, "pimreg%u", mrt->id);
484 dev = alloc_netdev(0, name, reg_vif_setup);
489 dev_net_set(dev, net);
491 if (register_netdevice(dev)) {
498 in_dev = __in_dev_get_rcu(dev);
504 ipv4_devconf_setall(in_dev);
505 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
516 /* allow the register to be completed before unregistering. */
520 unregister_netdevice(dev);
527 * @notify: Set to 1, if the caller is a notifier_call
530 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
531 struct list_head *head)
533 struct vif_device *v;
534 struct net_device *dev;
535 struct in_device *in_dev;
537 if (vifi < 0 || vifi >= mrt->maxvif)
538 return -EADDRNOTAVAIL;
540 v = &mrt->vif_table[vifi];
542 write_lock_bh(&mrt_lock);
547 write_unlock_bh(&mrt_lock);
548 return -EADDRNOTAVAIL;
551 #ifdef CONFIG_IP_PIMSM
552 if (vifi == mrt->mroute_reg_vif_num)
553 mrt->mroute_reg_vif_num = -1;
556 if (vifi + 1 == mrt->maxvif) {
559 for (tmp = vifi - 1; tmp >= 0; tmp--) {
560 if (VIF_EXISTS(mrt, tmp))
566 write_unlock_bh(&mrt_lock);
568 dev_set_allmulti(dev, -1);
570 in_dev = __in_dev_get_rtnl(dev);
572 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
573 ip_rt_multicast_event(in_dev);
576 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
577 unregister_netdevice_queue(dev, head);
583 static void ipmr_cache_free_rcu(struct rcu_head *head)
585 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
587 kmem_cache_free(mrt_cachep, c);
590 static inline void ipmr_cache_free(struct mfc_cache *c)
592 call_rcu(&c->rcu, ipmr_cache_free_rcu);
595 /* Destroy an unresolved cache entry, killing queued skbs
596 * and reporting error to netlink readers.
599 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
601 struct net *net = read_pnet(&mrt->net);
605 atomic_dec(&mrt->cache_resolve_queue_len);
607 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
608 if (ip_hdr(skb)->version == 0) {
609 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
610 nlh->nlmsg_type = NLMSG_ERROR;
611 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
612 skb_trim(skb, nlh->nlmsg_len);
614 e->error = -ETIMEDOUT;
615 memset(&e->msg, 0, sizeof(e->msg));
617 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
627 /* Timer process for the unresolved queue. */
629 static void ipmr_expire_process(unsigned long arg)
631 struct mr_table *mrt = (struct mr_table *)arg;
633 unsigned long expires;
634 struct mfc_cache *c, *next;
636 if (!spin_trylock(&mfc_unres_lock)) {
637 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
641 if (list_empty(&mrt->mfc_unres_queue))
647 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
648 if (time_after(c->mfc_un.unres.expires, now)) {
649 unsigned long interval = c->mfc_un.unres.expires - now;
650 if (interval < expires)
656 ipmr_destroy_unres(mrt, c);
659 if (!list_empty(&mrt->mfc_unres_queue))
660 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
663 spin_unlock(&mfc_unres_lock);
666 /* Fill oifs list. It is called under write locked mrt_lock. */
668 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
673 cache->mfc_un.res.minvif = MAXVIFS;
674 cache->mfc_un.res.maxvif = 0;
675 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
677 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
678 if (VIF_EXISTS(mrt, vifi) &&
679 ttls[vifi] && ttls[vifi] < 255) {
680 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
681 if (cache->mfc_un.res.minvif > vifi)
682 cache->mfc_un.res.minvif = vifi;
683 if (cache->mfc_un.res.maxvif <= vifi)
684 cache->mfc_un.res.maxvif = vifi + 1;
689 static int vif_add(struct net *net, struct mr_table *mrt,
690 struct vifctl *vifc, int mrtsock)
692 int vifi = vifc->vifc_vifi;
693 struct vif_device *v = &mrt->vif_table[vifi];
694 struct net_device *dev;
695 struct in_device *in_dev;
699 if (VIF_EXISTS(mrt, vifi))
702 switch (vifc->vifc_flags) {
703 #ifdef CONFIG_IP_PIMSM
706 * Special Purpose VIF in PIM
707 * All the packets will be sent to the daemon
709 if (mrt->mroute_reg_vif_num >= 0)
711 dev = ipmr_reg_vif(net, mrt);
714 err = dev_set_allmulti(dev, 1);
716 unregister_netdevice(dev);
723 dev = ipmr_new_tunnel(net, vifc);
726 err = dev_set_allmulti(dev, 1);
728 ipmr_del_tunnel(dev, vifc);
734 case VIFF_USE_IFINDEX:
736 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
737 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
738 if (dev && __in_dev_get_rtnl(dev) == NULL) {
740 return -EADDRNOTAVAIL;
743 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
746 return -EADDRNOTAVAIL;
747 err = dev_set_allmulti(dev, 1);
757 in_dev = __in_dev_get_rtnl(dev);
760 return -EADDRNOTAVAIL;
762 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
763 ip_rt_multicast_event(in_dev);
765 /* Fill in the VIF structures */
767 v->rate_limit = vifc->vifc_rate_limit;
768 v->local = vifc->vifc_lcl_addr.s_addr;
769 v->remote = vifc->vifc_rmt_addr.s_addr;
770 v->flags = vifc->vifc_flags;
772 v->flags |= VIFF_STATIC;
773 v->threshold = vifc->vifc_threshold;
778 v->link = dev->ifindex;
779 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
780 v->link = dev->iflink;
782 /* And finish update writing critical data */
783 write_lock_bh(&mrt_lock);
785 #ifdef CONFIG_IP_PIMSM
786 if (v->flags & VIFF_REGISTER)
787 mrt->mroute_reg_vif_num = vifi;
789 if (vifi+1 > mrt->maxvif)
790 mrt->maxvif = vifi+1;
791 write_unlock_bh(&mrt_lock);
795 /* called with rcu_read_lock() */
796 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
800 int line = MFC_HASH(mcastgrp, origin);
803 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
804 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
811 * Allocate a multicast cache entry
813 static struct mfc_cache *ipmr_cache_alloc(void)
815 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
818 c->mfc_un.res.minvif = MAXVIFS;
822 static struct mfc_cache *ipmr_cache_alloc_unres(void)
824 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
827 skb_queue_head_init(&c->mfc_un.unres.unresolved);
828 c->mfc_un.unres.expires = jiffies + 10*HZ;
834 * A cache entry has gone into a resolved state from queued
837 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
838 struct mfc_cache *uc, struct mfc_cache *c)
843 /* Play the pending entries through our router */
845 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
846 if (ip_hdr(skb)->version == 0) {
847 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
849 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
850 nlh->nlmsg_len = skb_tail_pointer(skb) -
853 nlh->nlmsg_type = NLMSG_ERROR;
854 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
855 skb_trim(skb, nlh->nlmsg_len);
857 e->error = -EMSGSIZE;
858 memset(&e->msg, 0, sizeof(e->msg));
861 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
863 ip_mr_forward(net, mrt, skb, c, 0);
869 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
870 * expects the following bizarre scheme.
872 * Called under mrt_lock.
875 static int ipmr_cache_report(struct mr_table *mrt,
876 struct sk_buff *pkt, vifi_t vifi, int assert)
879 const int ihl = ip_hdrlen(pkt);
880 struct igmphdr *igmp;
882 struct sock *mroute_sk;
885 #ifdef CONFIG_IP_PIMSM
886 if (assert == IGMPMSG_WHOLEPKT)
887 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
890 skb = alloc_skb(128, GFP_ATOMIC);
895 #ifdef CONFIG_IP_PIMSM
896 if (assert == IGMPMSG_WHOLEPKT) {
897 /* Ugly, but we have no choice with this interface.
898 * Duplicate old header, fix ihl, length etc.
899 * And all this only to mangle msg->im_msgtype and
900 * to set msg->im_mbz to "mbz" :-)
902 skb_push(skb, sizeof(struct iphdr));
903 skb_reset_network_header(skb);
904 skb_reset_transport_header(skb);
905 msg = (struct igmpmsg *)skb_network_header(skb);
906 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
907 msg->im_msgtype = IGMPMSG_WHOLEPKT;
909 msg->im_vif = mrt->mroute_reg_vif_num;
910 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
911 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
912 sizeof(struct iphdr));
917 /* Copy the IP header */
919 skb->network_header = skb->tail;
921 skb_copy_to_linear_data(skb, pkt->data, ihl);
922 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
923 msg = (struct igmpmsg *)skb_network_header(skb);
925 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
929 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
931 msg->im_msgtype = assert;
933 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
934 skb->transport_header = skb->network_header;
938 mroute_sk = rcu_dereference(mrt->mroute_sk);
939 if (mroute_sk == NULL) {
945 /* Deliver to mrouted */
947 ret = sock_queue_rcv_skb(mroute_sk, skb);
951 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
959 * Queue a packet for resolution. It gets locked cache entry!
963 ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
968 const struct iphdr *iph = ip_hdr(skb);
970 spin_lock_bh(&mfc_unres_lock);
971 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
972 if (c->mfc_mcastgrp == iph->daddr &&
973 c->mfc_origin == iph->saddr) {
980 /* Create a new entry if allowable */
982 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
983 (c = ipmr_cache_alloc_unres()) == NULL) {
984 spin_unlock_bh(&mfc_unres_lock);
990 /* Fill in the new cache entry */
993 c->mfc_origin = iph->saddr;
994 c->mfc_mcastgrp = iph->daddr;
996 /* Reflect first query at mrouted. */
998 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1000 /* If the report failed throw the cache entry
1003 spin_unlock_bh(&mfc_unres_lock);
1010 atomic_inc(&mrt->cache_resolve_queue_len);
1011 list_add(&c->list, &mrt->mfc_unres_queue);
1013 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1014 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1017 /* See if we can append the packet */
1019 if (c->mfc_un.unres.unresolved.qlen > 3) {
1023 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1027 spin_unlock_bh(&mfc_unres_lock);
1032 * MFC cache manipulation by user space mroute daemon
1035 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
1038 struct mfc_cache *c, *next;
1040 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1042 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1043 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1044 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1045 list_del_rcu(&c->list);
1054 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1055 struct mfcctl *mfc, int mrtsock)
1059 struct mfc_cache *uc, *c;
1061 if (mfc->mfcc_parent >= MAXVIFS)
1064 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1066 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1067 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1068 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1075 write_lock_bh(&mrt_lock);
1076 c->mfc_parent = mfc->mfcc_parent;
1077 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1079 c->mfc_flags |= MFC_STATIC;
1080 write_unlock_bh(&mrt_lock);
1084 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1087 c = ipmr_cache_alloc();
1091 c->mfc_origin = mfc->mfcc_origin.s_addr;
1092 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1093 c->mfc_parent = mfc->mfcc_parent;
1094 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1096 c->mfc_flags |= MFC_STATIC;
1098 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1101 * Check to see if we resolved a queued list. If so we
1102 * need to send on the frames and tidy up.
1105 spin_lock_bh(&mfc_unres_lock);
1106 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1107 if (uc->mfc_origin == c->mfc_origin &&
1108 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1109 list_del(&uc->list);
1110 atomic_dec(&mrt->cache_resolve_queue_len);
1115 if (list_empty(&mrt->mfc_unres_queue))
1116 del_timer(&mrt->ipmr_expire_timer);
1117 spin_unlock_bh(&mfc_unres_lock);
1120 ipmr_cache_resolve(net, mrt, uc, c);
1121 ipmr_cache_free(uc);
1127 * Close the multicast socket, and clear the vif tables etc
1130 static void mroute_clean_tables(struct mr_table *mrt)
1134 struct mfc_cache *c, *next;
1136 /* Shut down all active vif entries */
1138 for (i = 0; i < mrt->maxvif; i++) {
1139 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
1140 vif_delete(mrt, i, 0, &list);
1142 unregister_netdevice_many(&list);
1144 /* Wipe the cache */
1146 for (i = 0; i < MFC_LINES; i++) {
1147 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1148 if (c->mfc_flags & MFC_STATIC)
1150 list_del_rcu(&c->list);
1155 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1156 spin_lock_bh(&mfc_unres_lock);
1157 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1159 ipmr_destroy_unres(mrt, c);
1161 spin_unlock_bh(&mfc_unres_lock);
1165 /* called from ip_ra_control(), before an RCU grace period,
1166 * we dont need to call synchronize_rcu() here
1168 static void mrtsock_destruct(struct sock *sk)
1170 struct net *net = sock_net(sk);
1171 struct mr_table *mrt;
1174 ipmr_for_each_table(mrt, net) {
1175 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1176 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1177 rcu_assign_pointer(mrt->mroute_sk, NULL);
1178 mroute_clean_tables(mrt);
1185 * Socket options and virtual interface manipulation. The whole
1186 * virtual interface system is a complete heap, but unfortunately
1187 * that's how BSD mrouted happens to think. Maybe one day with a proper
1188 * MOSPF/PIM router set up we can clean this up.
1191 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1196 struct net *net = sock_net(sk);
1197 struct mr_table *mrt;
1199 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1203 if (optname != MRT_INIT) {
1204 if (sk != rcu_dereference_raw(mrt->mroute_sk) &&
1205 !capable(CAP_NET_ADMIN))
1211 if (sk->sk_type != SOCK_RAW ||
1212 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1214 if (optlen != sizeof(int))
1215 return -ENOPROTOOPT;
1218 if (rtnl_dereference(mrt->mroute_sk)) {
1223 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1225 rcu_assign_pointer(mrt->mroute_sk, sk);
1226 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1231 if (sk != rcu_dereference_raw(mrt->mroute_sk))
1233 return ip_ra_control(sk, 0, NULL);
1236 if (optlen != sizeof(vif))
1238 if (copy_from_user(&vif, optval, sizeof(vif)))
1240 if (vif.vifc_vifi >= MAXVIFS)
1243 if (optname == MRT_ADD_VIF) {
1244 ret = vif_add(net, mrt, &vif,
1245 sk == rtnl_dereference(mrt->mroute_sk));
1247 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1253 * Manipulate the forwarding caches. These live
1254 * in a sort of kernel/user symbiosis.
1258 if (optlen != sizeof(mfc))
1260 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1263 if (optname == MRT_DEL_MFC)
1264 ret = ipmr_mfc_delete(mrt, &mfc);
1266 ret = ipmr_mfc_add(net, mrt, &mfc,
1267 sk == rtnl_dereference(mrt->mroute_sk));
1271 * Control PIM assert.
1276 if (get_user(v, (int __user *)optval))
1278 mrt->mroute_do_assert = (v) ? 1 : 0;
1281 #ifdef CONFIG_IP_PIMSM
1286 if (get_user(v, (int __user *)optval))
1292 if (v != mrt->mroute_do_pim) {
1293 mrt->mroute_do_pim = v;
1294 mrt->mroute_do_assert = v;
1300 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1305 if (optlen != sizeof(u32))
1307 if (get_user(v, (u32 __user *)optval))
1312 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1315 if (!ipmr_new_table(net, v))
1317 raw_sk(sk)->ipmr_table = v;
1324 * Spurious command, or MRT_VERSION which you cannot
1328 return -ENOPROTOOPT;
1333 * Getsock opt support for the multicast routing system.
1336 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1340 struct net *net = sock_net(sk);
1341 struct mr_table *mrt;
1343 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1347 if (optname != MRT_VERSION &&
1348 #ifdef CONFIG_IP_PIMSM
1349 optname != MRT_PIM &&
1351 optname != MRT_ASSERT)
1352 return -ENOPROTOOPT;
1354 if (get_user(olr, optlen))
1357 olr = min_t(unsigned int, olr, sizeof(int));
1361 if (put_user(olr, optlen))
1363 if (optname == MRT_VERSION)
1365 #ifdef CONFIG_IP_PIMSM
1366 else if (optname == MRT_PIM)
1367 val = mrt->mroute_do_pim;
1370 val = mrt->mroute_do_assert;
1371 if (copy_to_user(optval, &val, olr))
1377 * The IP multicast ioctl support routines.
1380 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1382 struct sioc_sg_req sr;
1383 struct sioc_vif_req vr;
1384 struct vif_device *vif;
1385 struct mfc_cache *c;
1386 struct net *net = sock_net(sk);
1387 struct mr_table *mrt;
1389 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1395 if (copy_from_user(&vr, arg, sizeof(vr)))
1397 if (vr.vifi >= mrt->maxvif)
1399 read_lock(&mrt_lock);
1400 vif = &mrt->vif_table[vr.vifi];
1401 if (VIF_EXISTS(mrt, vr.vifi)) {
1402 vr.icount = vif->pkt_in;
1403 vr.ocount = vif->pkt_out;
1404 vr.ibytes = vif->bytes_in;
1405 vr.obytes = vif->bytes_out;
1406 read_unlock(&mrt_lock);
1408 if (copy_to_user(arg, &vr, sizeof(vr)))
1412 read_unlock(&mrt_lock);
1413 return -EADDRNOTAVAIL;
1415 if (copy_from_user(&sr, arg, sizeof(sr)))
1419 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1421 sr.pktcnt = c->mfc_un.res.pkt;
1422 sr.bytecnt = c->mfc_un.res.bytes;
1423 sr.wrong_if = c->mfc_un.res.wrong_if;
1426 if (copy_to_user(arg, &sr, sizeof(sr)))
1431 return -EADDRNOTAVAIL;
1433 return -ENOIOCTLCMD;
1438 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1440 struct net_device *dev = ptr;
1441 struct net *net = dev_net(dev);
1442 struct mr_table *mrt;
1443 struct vif_device *v;
1447 if (event != NETDEV_UNREGISTER)
1450 ipmr_for_each_table(mrt, net) {
1451 v = &mrt->vif_table[0];
1452 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1454 vif_delete(mrt, ct, 1, &list);
1457 unregister_netdevice_many(&list);
1462 static struct notifier_block ip_mr_notifier = {
1463 .notifier_call = ipmr_device_event,
1467 * Encapsulate a packet by attaching a valid IPIP header to it.
1468 * This avoids tunnel drivers and other mess and gives us the speed so
1469 * important for multicast video.
1472 static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1475 struct iphdr *old_iph = ip_hdr(skb);
1477 skb_push(skb, sizeof(struct iphdr));
1478 skb->transport_header = skb->network_header;
1479 skb_reset_network_header(skb);
1483 iph->tos = old_iph->tos;
1484 iph->ttl = old_iph->ttl;
1488 iph->protocol = IPPROTO_IPIP;
1490 iph->tot_len = htons(skb->len);
1491 ip_select_ident(iph, skb_dst(skb), NULL);
1494 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1498 static inline int ipmr_forward_finish(struct sk_buff *skb)
1500 struct ip_options *opt = &(IPCB(skb)->opt);
1502 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
1504 if (unlikely(opt->optlen))
1505 ip_forward_options(skb);
1507 return dst_output(skb);
1511 * Processing handlers for ipmr_forward
1514 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1515 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1517 const struct iphdr *iph = ip_hdr(skb);
1518 struct vif_device *vif = &mrt->vif_table[vifi];
1519 struct net_device *dev;
1523 if (vif->dev == NULL)
1526 #ifdef CONFIG_IP_PIMSM
1527 if (vif->flags & VIFF_REGISTER) {
1529 vif->bytes_out += skb->len;
1530 vif->dev->stats.tx_bytes += skb->len;
1531 vif->dev->stats.tx_packets++;
1532 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1537 if (vif->flags & VIFF_TUNNEL) {
1540 .fl4_dst = vif->remote,
1541 .fl4_src = vif->local,
1542 .fl4_tos = RT_TOS(iph->tos),
1543 .proto = IPPROTO_IPIP
1546 if (ip_route_output_key(net, &rt, &fl))
1548 encap = sizeof(struct iphdr);
1552 .fl4_dst = iph->daddr,
1553 .fl4_tos = RT_TOS(iph->tos),
1554 .proto = IPPROTO_IPIP
1557 if (ip_route_output_key(net, &rt, &fl))
1563 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1564 /* Do not fragment multicasts. Alas, IPv4 does not
1565 * allow to send ICMP, so that packets will disappear
1569 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1574 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1576 if (skb_cow(skb, encap)) {
1582 vif->bytes_out += skb->len;
1585 skb_dst_set(skb, &rt->dst);
1586 ip_decrease_ttl(ip_hdr(skb));
1588 /* FIXME: forward and output firewalls used to be called here.
1589 * What do we do with netfilter? -- RR
1591 if (vif->flags & VIFF_TUNNEL) {
1592 ip_encap(skb, vif->local, vif->remote);
1593 /* FIXME: extra output firewall step used to be here. --RR */
1594 vif->dev->stats.tx_packets++;
1595 vif->dev->stats.tx_bytes += skb->len;
1598 IPCB(skb)->flags |= IPSKB_FORWARDED;
1601 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1602 * not only before forwarding, but after forwarding on all output
1603 * interfaces. It is clear, if mrouter runs a multicasting
1604 * program, it should receive packets not depending to what interface
1605 * program is joined.
1606 * If we will not make it, the program will have to join on all
1607 * interfaces. On the other hand, multihoming host (or router, but
1608 * not mrouter) cannot join to more than one interface - it will
1609 * result in receiving multiple packets.
1611 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
1612 ipmr_forward_finish);
1619 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1623 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1624 if (mrt->vif_table[ct].dev == dev)
1630 /* "local" means that we should preserve one skb (for local delivery) */
1632 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1633 struct sk_buff *skb, struct mfc_cache *cache,
1639 vif = cache->mfc_parent;
1640 cache->mfc_un.res.pkt++;
1641 cache->mfc_un.res.bytes += skb->len;
1644 * Wrong interface: drop packet and (maybe) send PIM assert.
1646 if (mrt->vif_table[vif].dev != skb->dev) {
1649 if (rt_is_output_route(skb_rtable(skb))) {
1650 /* It is our own packet, looped back.
1651 * Very complicated situation...
1653 * The best workaround until routing daemons will be
1654 * fixed is not to redistribute packet, if it was
1655 * send through wrong interface. It means, that
1656 * multicast applications WILL NOT work for
1657 * (S,G), which have default multicast route pointing
1658 * to wrong oif. In any case, it is not a good
1659 * idea to use multicasting applications on router.
1664 cache->mfc_un.res.wrong_if++;
1665 true_vifi = ipmr_find_vif(mrt, skb->dev);
1667 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1668 /* pimsm uses asserts, when switching from RPT to SPT,
1669 * so that we cannot check that packet arrived on an oif.
1670 * It is bad, but otherwise we would need to move pretty
1671 * large chunk of pimd to kernel. Ough... --ANK
1673 (mrt->mroute_do_pim ||
1674 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1676 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1677 cache->mfc_un.res.last_assert = jiffies;
1678 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1683 mrt->vif_table[vif].pkt_in++;
1684 mrt->vif_table[vif].bytes_in += skb->len;
1689 for (ct = cache->mfc_un.res.maxvif - 1;
1690 ct >= cache->mfc_un.res.minvif; ct--) {
1691 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1693 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1696 ipmr_queue_xmit(net, mrt, skb2, cache,
1704 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1707 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1709 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1722 * Multicast packets for forwarding arrive here
1723 * Called with rcu_read_lock();
1726 int ip_mr_input(struct sk_buff *skb)
1728 struct mfc_cache *cache;
1729 struct net *net = dev_net(skb->dev);
1730 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1731 struct mr_table *mrt;
1734 /* Packet is looped back after forward, it should not be
1735 * forwarded second time, but still can be delivered locally.
1737 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1740 err = ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt);
1747 if (IPCB(skb)->opt.router_alert) {
1748 if (ip_call_ra_chain(skb))
1750 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1751 /* IGMPv1 (and broken IGMPv2 implementations sort of
1752 * Cisco IOS <= 11.2(8)) do not put router alert
1753 * option to IGMP packets destined to routable
1754 * groups. It is very bad, because it means
1755 * that we can forward NO IGMP messages.
1757 struct sock *mroute_sk;
1759 mroute_sk = rcu_dereference(mrt->mroute_sk);
1762 raw_rcv(mroute_sk, skb);
1768 /* already under rcu_read_lock() */
1769 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1772 * No usable cache entry
1774 if (cache == NULL) {
1778 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1779 ip_local_deliver(skb);
1785 read_lock(&mrt_lock);
1786 vif = ipmr_find_vif(mrt, skb->dev);
1788 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
1789 read_unlock(&mrt_lock);
1793 read_unlock(&mrt_lock);
1798 read_lock(&mrt_lock);
1799 ip_mr_forward(net, mrt, skb, cache, local);
1800 read_unlock(&mrt_lock);
1803 return ip_local_deliver(skb);
1809 return ip_local_deliver(skb);
1814 #ifdef CONFIG_IP_PIMSM
1815 /* called with rcu_read_lock() */
1816 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1817 unsigned int pimlen)
1819 struct net_device *reg_dev = NULL;
1820 struct iphdr *encap;
1822 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
1825 * a. packet is really sent to a multicast group
1826 * b. packet is not a NULL-REGISTER
1827 * c. packet is not truncated
1829 if (!ipv4_is_multicast(encap->daddr) ||
1830 encap->tot_len == 0 ||
1831 ntohs(encap->tot_len) + pimlen > skb->len)
1834 read_lock(&mrt_lock);
1835 if (mrt->mroute_reg_vif_num >= 0)
1836 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
1837 read_unlock(&mrt_lock);
1839 if (reg_dev == NULL)
1842 skb->mac_header = skb->network_header;
1843 skb_pull(skb, (u8 *)encap - skb->data);
1844 skb_reset_network_header(skb);
1845 skb->protocol = htons(ETH_P_IP);
1846 skb->ip_summed = CHECKSUM_NONE;
1847 skb->pkt_type = PACKET_HOST;
1849 skb_tunnel_rx(skb, reg_dev);
1853 return NET_RX_SUCCESS;
1857 #ifdef CONFIG_IP_PIMSM_V1
1859 * Handle IGMP messages of PIMv1
1862 int pim_rcv_v1(struct sk_buff *skb)
1864 struct igmphdr *pim;
1865 struct net *net = dev_net(skb->dev);
1866 struct mr_table *mrt;
1868 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1871 pim = igmp_hdr(skb);
1873 if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0)
1876 if (!mrt->mroute_do_pim ||
1877 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1880 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
1888 #ifdef CONFIG_IP_PIMSM_V2
1889 static int pim_rcv(struct sk_buff *skb)
1891 struct pimreghdr *pim;
1892 struct net *net = dev_net(skb->dev);
1893 struct mr_table *mrt;
1895 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1898 pim = (struct pimreghdr *)skb_transport_header(skb);
1899 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
1900 (pim->flags & PIM_NULL_REGISTER) ||
1901 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
1902 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
1905 if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0)
1908 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
1916 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
1917 struct mfc_cache *c, struct rtmsg *rtm)
1920 struct rtnexthop *nhp;
1921 u8 *b = skb_tail_pointer(skb);
1922 struct rtattr *mp_head;
1924 /* If cache is unresolved, don't try to parse IIF and OIF */
1925 if (c->mfc_parent >= MAXVIFS)
1928 if (VIF_EXISTS(mrt, c->mfc_parent))
1929 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
1931 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
1933 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
1934 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
1935 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
1936 goto rtattr_failure;
1937 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
1938 nhp->rtnh_flags = 0;
1939 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
1940 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
1941 nhp->rtnh_len = sizeof(*nhp);
1944 mp_head->rta_type = RTA_MULTIPATH;
1945 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
1946 rtm->rtm_type = RTN_MULTICAST;
1954 int ipmr_get_route(struct net *net,
1955 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
1958 struct mr_table *mrt;
1959 struct mfc_cache *cache;
1960 struct rtable *rt = skb_rtable(skb);
1962 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
1967 cache = ipmr_cache_find(mrt, rt->rt_src, rt->rt_dst);
1969 if (cache == NULL) {
1970 struct sk_buff *skb2;
1972 struct net_device *dev;
1981 read_lock(&mrt_lock);
1983 vif = ipmr_find_vif(mrt, dev);
1985 read_unlock(&mrt_lock);
1989 skb2 = skb_clone(skb, GFP_ATOMIC);
1991 read_unlock(&mrt_lock);
1996 skb_push(skb2, sizeof(struct iphdr));
1997 skb_reset_network_header(skb2);
1999 iph->ihl = sizeof(struct iphdr) >> 2;
2000 iph->saddr = rt->rt_src;
2001 iph->daddr = rt->rt_dst;
2003 err = ipmr_cache_unresolved(mrt, vif, skb2);
2004 read_unlock(&mrt_lock);
2009 read_lock(&mrt_lock);
2010 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
2011 cache->mfc_flags |= MFC_NOTIFY;
2012 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2013 read_unlock(&mrt_lock);
2018 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2019 u32 pid, u32 seq, struct mfc_cache *c)
2021 struct nlmsghdr *nlh;
2024 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2028 rtm = nlmsg_data(nlh);
2029 rtm->rtm_family = RTNL_FAMILY_IPMR;
2030 rtm->rtm_dst_len = 32;
2031 rtm->rtm_src_len = 32;
2033 rtm->rtm_table = mrt->id;
2034 NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2035 rtm->rtm_type = RTN_MULTICAST;
2036 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2037 rtm->rtm_protocol = RTPROT_UNSPEC;
2040 NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2041 NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2043 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2044 goto nla_put_failure;
2046 return nlmsg_end(skb, nlh);
2049 nlmsg_cancel(skb, nlh);
2053 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2055 struct net *net = sock_net(skb->sk);
2056 struct mr_table *mrt;
2057 struct mfc_cache *mfc;
2058 unsigned int t = 0, s_t;
2059 unsigned int h = 0, s_h;
2060 unsigned int e = 0, s_e;
2067 ipmr_for_each_table(mrt, net) {
2072 for (h = s_h; h < MFC_LINES; h++) {
2073 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2076 if (ipmr_fill_mroute(mrt, skb,
2077 NETLINK_CB(cb->skb).pid,
2100 #ifdef CONFIG_PROC_FS
2102 * The /proc interfaces to multicast routing :
2103 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2105 struct ipmr_vif_iter {
2106 struct seq_net_private p;
2107 struct mr_table *mrt;
2111 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2112 struct ipmr_vif_iter *iter,
2115 struct mr_table *mrt = iter->mrt;
2117 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2118 if (!VIF_EXISTS(mrt, iter->ct))
2121 return &mrt->vif_table[iter->ct];
2126 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2127 __acquires(mrt_lock)
2129 struct ipmr_vif_iter *iter = seq->private;
2130 struct net *net = seq_file_net(seq);
2131 struct mr_table *mrt;
2133 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2135 return ERR_PTR(-ENOENT);
2139 read_lock(&mrt_lock);
2140 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2144 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2146 struct ipmr_vif_iter *iter = seq->private;
2147 struct net *net = seq_file_net(seq);
2148 struct mr_table *mrt = iter->mrt;
2151 if (v == SEQ_START_TOKEN)
2152 return ipmr_vif_seq_idx(net, iter, 0);
2154 while (++iter->ct < mrt->maxvif) {
2155 if (!VIF_EXISTS(mrt, iter->ct))
2157 return &mrt->vif_table[iter->ct];
2162 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2163 __releases(mrt_lock)
2165 read_unlock(&mrt_lock);
2168 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2170 struct ipmr_vif_iter *iter = seq->private;
2171 struct mr_table *mrt = iter->mrt;
2173 if (v == SEQ_START_TOKEN) {
2175 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2177 const struct vif_device *vif = v;
2178 const char *name = vif->dev ? vif->dev->name : "none";
2181 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2182 vif - mrt->vif_table,
2183 name, vif->bytes_in, vif->pkt_in,
2184 vif->bytes_out, vif->pkt_out,
2185 vif->flags, vif->local, vif->remote);
2190 static const struct seq_operations ipmr_vif_seq_ops = {
2191 .start = ipmr_vif_seq_start,
2192 .next = ipmr_vif_seq_next,
2193 .stop = ipmr_vif_seq_stop,
2194 .show = ipmr_vif_seq_show,
2197 static int ipmr_vif_open(struct inode *inode, struct file *file)
2199 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2200 sizeof(struct ipmr_vif_iter));
2203 static const struct file_operations ipmr_vif_fops = {
2204 .owner = THIS_MODULE,
2205 .open = ipmr_vif_open,
2207 .llseek = seq_lseek,
2208 .release = seq_release_net,
2211 struct ipmr_mfc_iter {
2212 struct seq_net_private p;
2213 struct mr_table *mrt;
2214 struct list_head *cache;
2219 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2220 struct ipmr_mfc_iter *it, loff_t pos)
2222 struct mr_table *mrt = it->mrt;
2223 struct mfc_cache *mfc;
2226 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2227 it->cache = &mrt->mfc_cache_array[it->ct];
2228 list_for_each_entry_rcu(mfc, it->cache, list)
2234 spin_lock_bh(&mfc_unres_lock);
2235 it->cache = &mrt->mfc_unres_queue;
2236 list_for_each_entry(mfc, it->cache, list)
2239 spin_unlock_bh(&mfc_unres_lock);
2246 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2248 struct ipmr_mfc_iter *it = seq->private;
2249 struct net *net = seq_file_net(seq);
2250 struct mr_table *mrt;
2252 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2254 return ERR_PTR(-ENOENT);
2259 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2263 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2265 struct mfc_cache *mfc = v;
2266 struct ipmr_mfc_iter *it = seq->private;
2267 struct net *net = seq_file_net(seq);
2268 struct mr_table *mrt = it->mrt;
2272 if (v == SEQ_START_TOKEN)
2273 return ipmr_mfc_seq_idx(net, seq->private, 0);
2275 if (mfc->list.next != it->cache)
2276 return list_entry(mfc->list.next, struct mfc_cache, list);
2278 if (it->cache == &mrt->mfc_unres_queue)
2281 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2283 while (++it->ct < MFC_LINES) {
2284 it->cache = &mrt->mfc_cache_array[it->ct];
2285 if (list_empty(it->cache))
2287 return list_first_entry(it->cache, struct mfc_cache, list);
2290 /* exhausted cache_array, show unresolved */
2292 it->cache = &mrt->mfc_unres_queue;
2295 spin_lock_bh(&mfc_unres_lock);
2296 if (!list_empty(it->cache))
2297 return list_first_entry(it->cache, struct mfc_cache, list);
2300 spin_unlock_bh(&mfc_unres_lock);
2306 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2308 struct ipmr_mfc_iter *it = seq->private;
2309 struct mr_table *mrt = it->mrt;
2311 if (it->cache == &mrt->mfc_unres_queue)
2312 spin_unlock_bh(&mfc_unres_lock);
2313 else if (it->cache == &mrt->mfc_cache_array[it->ct])
2317 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2321 if (v == SEQ_START_TOKEN) {
2323 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2325 const struct mfc_cache *mfc = v;
2326 const struct ipmr_mfc_iter *it = seq->private;
2327 const struct mr_table *mrt = it->mrt;
2329 seq_printf(seq, "%08X %08X %-3hd",
2330 (__force u32) mfc->mfc_mcastgrp,
2331 (__force u32) mfc->mfc_origin,
2334 if (it->cache != &mrt->mfc_unres_queue) {
2335 seq_printf(seq, " %8lu %8lu %8lu",
2336 mfc->mfc_un.res.pkt,
2337 mfc->mfc_un.res.bytes,
2338 mfc->mfc_un.res.wrong_if);
2339 for (n = mfc->mfc_un.res.minvif;
2340 n < mfc->mfc_un.res.maxvif; n++) {
2341 if (VIF_EXISTS(mrt, n) &&
2342 mfc->mfc_un.res.ttls[n] < 255)
2345 n, mfc->mfc_un.res.ttls[n]);
2348 /* unresolved mfc_caches don't contain
2349 * pkt, bytes and wrong_if values
2351 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2353 seq_putc(seq, '\n');
2358 static const struct seq_operations ipmr_mfc_seq_ops = {
2359 .start = ipmr_mfc_seq_start,
2360 .next = ipmr_mfc_seq_next,
2361 .stop = ipmr_mfc_seq_stop,
2362 .show = ipmr_mfc_seq_show,
2365 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2367 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2368 sizeof(struct ipmr_mfc_iter));
2371 static const struct file_operations ipmr_mfc_fops = {
2372 .owner = THIS_MODULE,
2373 .open = ipmr_mfc_open,
2375 .llseek = seq_lseek,
2376 .release = seq_release_net,
2380 #ifdef CONFIG_IP_PIMSM_V2
2381 static const struct net_protocol pim_protocol = {
2389 * Setup for IP multicast routing
2391 static int __net_init ipmr_net_init(struct net *net)
2395 err = ipmr_rules_init(net);
2399 #ifdef CONFIG_PROC_FS
2401 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2403 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2404 goto proc_cache_fail;
2408 #ifdef CONFIG_PROC_FS
2410 proc_net_remove(net, "ip_mr_vif");
2412 ipmr_rules_exit(net);
2418 static void __net_exit ipmr_net_exit(struct net *net)
2420 #ifdef CONFIG_PROC_FS
2421 proc_net_remove(net, "ip_mr_cache");
2422 proc_net_remove(net, "ip_mr_vif");
2424 ipmr_rules_exit(net);
2427 static struct pernet_operations ipmr_net_ops = {
2428 .init = ipmr_net_init,
2429 .exit = ipmr_net_exit,
2432 int __init ip_mr_init(void)
2436 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2437 sizeof(struct mfc_cache),
2438 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2443 err = register_pernet_subsys(&ipmr_net_ops);
2445 goto reg_pernet_fail;
2447 err = register_netdevice_notifier(&ip_mr_notifier);
2449 goto reg_notif_fail;
2450 #ifdef CONFIG_IP_PIMSM_V2
2451 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2452 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2454 goto add_proto_fail;
2457 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE, NULL, ipmr_rtm_dumproute);
2460 #ifdef CONFIG_IP_PIMSM_V2
2462 unregister_netdevice_notifier(&ip_mr_notifier);
2465 unregister_pernet_subsys(&ipmr_net_ops);
2467 kmem_cache_destroy(mrt_cachep);