2 * IP multicast routing support for mrouted 3.6/3.8
4 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
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>
63 #include <linux/compat.h>
64 #include <linux/export.h>
66 #include <net/checksum.h>
67 #include <net/netlink.h>
68 #include <net/fib_rules.h>
70 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
71 #define CONFIG_IP_PIMSM 1
75 struct list_head list;
80 struct sock __rcu *mroute_sk;
81 struct timer_list ipmr_expire_timer;
82 struct list_head mfc_unres_queue;
83 struct list_head mfc_cache_array[MFC_LINES];
84 struct vif_device vif_table[MAXVIFS];
86 atomic_t cache_resolve_queue_len;
89 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
90 int mroute_reg_vif_num;
95 struct fib_rule common;
102 /* Big lock, protecting vif table, mrt cache and mroute socket state.
103 * Note that the changes are semaphored via rtnl_lock.
106 static DEFINE_RWLOCK(mrt_lock);
109 * Multicast router control variables
112 #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
114 /* Special spinlock for queue of unresolved entries */
115 static DEFINE_SPINLOCK(mfc_unres_lock);
117 /* We return to original Alan's scheme. Hash table of resolved
118 * entries is changed only in process context and protected
119 * with weak lock mrt_lock. Queue of unresolved entries is protected
120 * with strong spinlock mfc_unres_lock.
122 * In this case data path is free of exclusive locks at all.
125 static struct kmem_cache *mrt_cachep __read_mostly;
127 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
128 static void ipmr_free_table(struct mr_table *mrt);
130 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
131 struct sk_buff *skb, struct mfc_cache *cache,
133 static int ipmr_cache_report(struct mr_table *mrt,
134 struct sk_buff *pkt, vifi_t vifi, int assert);
135 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
136 struct mfc_cache *c, struct rtmsg *rtm);
137 static void mroute_clean_tables(struct mr_table *mrt);
138 static void ipmr_expire_process(unsigned long arg);
140 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
141 #define ipmr_for_each_table(mrt, net) \
142 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
144 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
146 struct mr_table *mrt;
148 ipmr_for_each_table(mrt, net) {
155 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
156 struct mr_table **mrt)
158 struct ipmr_result res;
159 struct fib_lookup_arg arg = { .result = &res, };
162 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
163 flowi4_to_flowi(flp4), 0, &arg);
170 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
171 int flags, struct fib_lookup_arg *arg)
173 struct ipmr_result *res = arg->result;
174 struct mr_table *mrt;
176 switch (rule->action) {
179 case FR_ACT_UNREACHABLE:
181 case FR_ACT_PROHIBIT:
183 case FR_ACT_BLACKHOLE:
188 mrt = ipmr_get_table(rule->fr_net, rule->table);
195 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
200 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
204 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
205 struct fib_rule_hdr *frh, struct nlattr **tb)
210 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
216 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
217 struct fib_rule_hdr *frh)
225 static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
226 .family = RTNL_FAMILY_IPMR,
227 .rule_size = sizeof(struct ipmr_rule),
228 .addr_size = sizeof(u32),
229 .action = ipmr_rule_action,
230 .match = ipmr_rule_match,
231 .configure = ipmr_rule_configure,
232 .compare = ipmr_rule_compare,
233 .default_pref = fib_default_rule_pref,
234 .fill = ipmr_rule_fill,
235 .nlgroup = RTNLGRP_IPV4_RULE,
236 .policy = ipmr_rule_policy,
237 .owner = THIS_MODULE,
240 static int __net_init ipmr_rules_init(struct net *net)
242 struct fib_rules_ops *ops;
243 struct mr_table *mrt;
246 ops = fib_rules_register(&ipmr_rules_ops_template, net);
250 INIT_LIST_HEAD(&net->ipv4.mr_tables);
252 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
258 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
262 net->ipv4.mr_rules_ops = ops;
268 fib_rules_unregister(ops);
272 static void __net_exit ipmr_rules_exit(struct net *net)
274 struct mr_table *mrt, *next;
276 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
277 list_del(&mrt->list);
278 ipmr_free_table(mrt);
280 fib_rules_unregister(net->ipv4.mr_rules_ops);
283 #define ipmr_for_each_table(mrt, net) \
284 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
286 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
288 return net->ipv4.mrt;
291 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
292 struct mr_table **mrt)
294 *mrt = net->ipv4.mrt;
298 static int __net_init ipmr_rules_init(struct net *net)
300 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
301 return net->ipv4.mrt ? 0 : -ENOMEM;
304 static void __net_exit ipmr_rules_exit(struct net *net)
306 ipmr_free_table(net->ipv4.mrt);
310 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
312 struct mr_table *mrt;
315 mrt = ipmr_get_table(net, id);
319 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
322 write_pnet(&mrt->net, net);
325 /* Forwarding cache */
326 for (i = 0; i < MFC_LINES; i++)
327 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
329 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
331 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
334 #ifdef CONFIG_IP_PIMSM
335 mrt->mroute_reg_vif_num = -1;
337 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
338 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
343 static void ipmr_free_table(struct mr_table *mrt)
345 del_timer_sync(&mrt->ipmr_expire_timer);
346 mroute_clean_tables(mrt);
350 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
352 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
354 struct net *net = dev_net(dev);
358 dev = __dev_get_by_name(net, "tunl0");
360 const struct net_device_ops *ops = dev->netdev_ops;
362 struct ip_tunnel_parm p;
364 memset(&p, 0, sizeof(p));
365 p.iph.daddr = v->vifc_rmt_addr.s_addr;
366 p.iph.saddr = v->vifc_lcl_addr.s_addr;
369 p.iph.protocol = IPPROTO_IPIP;
370 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
371 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
373 if (ops->ndo_do_ioctl) {
374 mm_segment_t oldfs = get_fs();
377 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
384 struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
386 struct net_device *dev;
388 dev = __dev_get_by_name(net, "tunl0");
391 const struct net_device_ops *ops = dev->netdev_ops;
394 struct ip_tunnel_parm p;
395 struct in_device *in_dev;
397 memset(&p, 0, sizeof(p));
398 p.iph.daddr = v->vifc_rmt_addr.s_addr;
399 p.iph.saddr = v->vifc_lcl_addr.s_addr;
402 p.iph.protocol = IPPROTO_IPIP;
403 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
404 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
406 if (ops->ndo_do_ioctl) {
407 mm_segment_t oldfs = get_fs();
410 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
418 (dev = __dev_get_by_name(net, p.name)) != NULL) {
419 dev->flags |= IFF_MULTICAST;
421 in_dev = __in_dev_get_rtnl(dev);
425 ipv4_devconf_setall(in_dev);
426 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
436 /* allow the register to be completed before unregistering. */
440 unregister_netdevice(dev);
444 #ifdef CONFIG_IP_PIMSM
446 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
448 struct net *net = dev_net(dev);
449 struct mr_table *mrt;
450 struct flowi4 fl4 = {
451 .flowi4_oif = dev->ifindex,
452 .flowi4_iif = skb->skb_iif,
453 .flowi4_mark = skb->mark,
457 err = ipmr_fib_lookup(net, &fl4, &mrt);
463 read_lock(&mrt_lock);
464 dev->stats.tx_bytes += skb->len;
465 dev->stats.tx_packets++;
466 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
467 read_unlock(&mrt_lock);
472 static const struct net_device_ops reg_vif_netdev_ops = {
473 .ndo_start_xmit = reg_vif_xmit,
476 static void reg_vif_setup(struct net_device *dev)
478 dev->type = ARPHRD_PIMREG;
479 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
480 dev->flags = IFF_NOARP;
481 dev->netdev_ops = ®_vif_netdev_ops,
482 dev->destructor = free_netdev;
483 dev->features |= NETIF_F_NETNS_LOCAL;
486 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
488 struct net_device *dev;
489 struct in_device *in_dev;
492 if (mrt->id == RT_TABLE_DEFAULT)
493 sprintf(name, "pimreg");
495 sprintf(name, "pimreg%u", mrt->id);
497 dev = alloc_netdev(0, name, reg_vif_setup);
502 dev_net_set(dev, net);
504 if (register_netdevice(dev)) {
511 in_dev = __in_dev_get_rcu(dev);
517 ipv4_devconf_setall(in_dev);
518 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
529 /* allow the register to be completed before unregistering. */
533 unregister_netdevice(dev);
540 * @notify: Set to 1, if the caller is a notifier_call
543 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
544 struct list_head *head)
546 struct vif_device *v;
547 struct net_device *dev;
548 struct in_device *in_dev;
550 if (vifi < 0 || vifi >= mrt->maxvif)
551 return -EADDRNOTAVAIL;
553 v = &mrt->vif_table[vifi];
555 write_lock_bh(&mrt_lock);
560 write_unlock_bh(&mrt_lock);
561 return -EADDRNOTAVAIL;
564 #ifdef CONFIG_IP_PIMSM
565 if (vifi == mrt->mroute_reg_vif_num)
566 mrt->mroute_reg_vif_num = -1;
569 if (vifi + 1 == mrt->maxvif) {
572 for (tmp = vifi - 1; tmp >= 0; tmp--) {
573 if (VIF_EXISTS(mrt, tmp))
579 write_unlock_bh(&mrt_lock);
581 dev_set_allmulti(dev, -1);
583 in_dev = __in_dev_get_rtnl(dev);
585 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
586 ip_rt_multicast_event(in_dev);
589 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
590 unregister_netdevice_queue(dev, head);
596 static void ipmr_cache_free_rcu(struct rcu_head *head)
598 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
600 kmem_cache_free(mrt_cachep, c);
603 static inline void ipmr_cache_free(struct mfc_cache *c)
605 call_rcu(&c->rcu, ipmr_cache_free_rcu);
608 /* Destroy an unresolved cache entry, killing queued skbs
609 * and reporting error to netlink readers.
612 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
614 struct net *net = read_pnet(&mrt->net);
618 atomic_dec(&mrt->cache_resolve_queue_len);
620 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
621 if (ip_hdr(skb)->version == 0) {
622 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
623 nlh->nlmsg_type = NLMSG_ERROR;
624 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
625 skb_trim(skb, nlh->nlmsg_len);
627 e->error = -ETIMEDOUT;
628 memset(&e->msg, 0, sizeof(e->msg));
630 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
640 /* Timer process for the unresolved queue. */
642 static void ipmr_expire_process(unsigned long arg)
644 struct mr_table *mrt = (struct mr_table *)arg;
646 unsigned long expires;
647 struct mfc_cache *c, *next;
649 if (!spin_trylock(&mfc_unres_lock)) {
650 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
654 if (list_empty(&mrt->mfc_unres_queue))
660 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
661 if (time_after(c->mfc_un.unres.expires, now)) {
662 unsigned long interval = c->mfc_un.unres.expires - now;
663 if (interval < expires)
669 ipmr_destroy_unres(mrt, c);
672 if (!list_empty(&mrt->mfc_unres_queue))
673 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
676 spin_unlock(&mfc_unres_lock);
679 /* Fill oifs list. It is called under write locked mrt_lock. */
681 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
686 cache->mfc_un.res.minvif = MAXVIFS;
687 cache->mfc_un.res.maxvif = 0;
688 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
690 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
691 if (VIF_EXISTS(mrt, vifi) &&
692 ttls[vifi] && ttls[vifi] < 255) {
693 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
694 if (cache->mfc_un.res.minvif > vifi)
695 cache->mfc_un.res.minvif = vifi;
696 if (cache->mfc_un.res.maxvif <= vifi)
697 cache->mfc_un.res.maxvif = vifi + 1;
702 static int vif_add(struct net *net, struct mr_table *mrt,
703 struct vifctl *vifc, int mrtsock)
705 int vifi = vifc->vifc_vifi;
706 struct vif_device *v = &mrt->vif_table[vifi];
707 struct net_device *dev;
708 struct in_device *in_dev;
712 if (VIF_EXISTS(mrt, vifi))
715 switch (vifc->vifc_flags) {
716 #ifdef CONFIG_IP_PIMSM
719 * Special Purpose VIF in PIM
720 * All the packets will be sent to the daemon
722 if (mrt->mroute_reg_vif_num >= 0)
724 dev = ipmr_reg_vif(net, mrt);
727 err = dev_set_allmulti(dev, 1);
729 unregister_netdevice(dev);
736 dev = ipmr_new_tunnel(net, vifc);
739 err = dev_set_allmulti(dev, 1);
741 ipmr_del_tunnel(dev, vifc);
747 case VIFF_USE_IFINDEX:
749 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
750 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
751 if (dev && __in_dev_get_rtnl(dev) == NULL) {
753 return -EADDRNOTAVAIL;
756 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
759 return -EADDRNOTAVAIL;
760 err = dev_set_allmulti(dev, 1);
770 in_dev = __in_dev_get_rtnl(dev);
773 return -EADDRNOTAVAIL;
775 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
776 ip_rt_multicast_event(in_dev);
778 /* Fill in the VIF structures */
780 v->rate_limit = vifc->vifc_rate_limit;
781 v->local = vifc->vifc_lcl_addr.s_addr;
782 v->remote = vifc->vifc_rmt_addr.s_addr;
783 v->flags = vifc->vifc_flags;
785 v->flags |= VIFF_STATIC;
786 v->threshold = vifc->vifc_threshold;
791 v->link = dev->ifindex;
792 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
793 v->link = dev->iflink;
795 /* And finish update writing critical data */
796 write_lock_bh(&mrt_lock);
798 #ifdef CONFIG_IP_PIMSM
799 if (v->flags & VIFF_REGISTER)
800 mrt->mroute_reg_vif_num = vifi;
802 if (vifi+1 > mrt->maxvif)
803 mrt->maxvif = vifi+1;
804 write_unlock_bh(&mrt_lock);
808 /* called with rcu_read_lock() */
809 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
813 int line = MFC_HASH(mcastgrp, origin);
816 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
817 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
824 * Allocate a multicast cache entry
826 static struct mfc_cache *ipmr_cache_alloc(void)
828 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
831 c->mfc_un.res.minvif = MAXVIFS;
835 static struct mfc_cache *ipmr_cache_alloc_unres(void)
837 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
840 skb_queue_head_init(&c->mfc_un.unres.unresolved);
841 c->mfc_un.unres.expires = jiffies + 10*HZ;
847 * A cache entry has gone into a resolved state from queued
850 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
851 struct mfc_cache *uc, struct mfc_cache *c)
856 /* Play the pending entries through our router */
858 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
859 if (ip_hdr(skb)->version == 0) {
860 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
862 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
863 nlh->nlmsg_len = skb_tail_pointer(skb) -
866 nlh->nlmsg_type = NLMSG_ERROR;
867 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
868 skb_trim(skb, nlh->nlmsg_len);
870 e->error = -EMSGSIZE;
871 memset(&e->msg, 0, sizeof(e->msg));
874 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
876 ip_mr_forward(net, mrt, skb, c, 0);
882 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
883 * expects the following bizarre scheme.
885 * Called under mrt_lock.
888 static int ipmr_cache_report(struct mr_table *mrt,
889 struct sk_buff *pkt, vifi_t vifi, int assert)
892 const int ihl = ip_hdrlen(pkt);
893 struct igmphdr *igmp;
895 struct sock *mroute_sk;
898 #ifdef CONFIG_IP_PIMSM
899 if (assert == IGMPMSG_WHOLEPKT)
900 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
903 skb = alloc_skb(128, GFP_ATOMIC);
908 #ifdef CONFIG_IP_PIMSM
909 if (assert == IGMPMSG_WHOLEPKT) {
910 /* Ugly, but we have no choice with this interface.
911 * Duplicate old header, fix ihl, length etc.
912 * And all this only to mangle msg->im_msgtype and
913 * to set msg->im_mbz to "mbz" :-)
915 skb_push(skb, sizeof(struct iphdr));
916 skb_reset_network_header(skb);
917 skb_reset_transport_header(skb);
918 msg = (struct igmpmsg *)skb_network_header(skb);
919 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
920 msg->im_msgtype = IGMPMSG_WHOLEPKT;
922 msg->im_vif = mrt->mroute_reg_vif_num;
923 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
924 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
925 sizeof(struct iphdr));
930 /* Copy the IP header */
932 skb->network_header = skb->tail;
934 skb_copy_to_linear_data(skb, pkt->data, ihl);
935 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
936 msg = (struct igmpmsg *)skb_network_header(skb);
938 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
942 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
944 msg->im_msgtype = assert;
946 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
947 skb->transport_header = skb->network_header;
951 mroute_sk = rcu_dereference(mrt->mroute_sk);
952 if (mroute_sk == NULL) {
958 /* Deliver to mrouted */
960 ret = sock_queue_rcv_skb(mroute_sk, skb);
964 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
972 * Queue a packet for resolution. It gets locked cache entry!
976 ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
981 const struct iphdr *iph = ip_hdr(skb);
983 spin_lock_bh(&mfc_unres_lock);
984 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
985 if (c->mfc_mcastgrp == iph->daddr &&
986 c->mfc_origin == iph->saddr) {
993 /* Create a new entry if allowable */
995 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
996 (c = ipmr_cache_alloc_unres()) == NULL) {
997 spin_unlock_bh(&mfc_unres_lock);
1003 /* Fill in the new cache entry */
1006 c->mfc_origin = iph->saddr;
1007 c->mfc_mcastgrp = iph->daddr;
1009 /* Reflect first query at mrouted. */
1011 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1013 /* If the report failed throw the cache entry
1016 spin_unlock_bh(&mfc_unres_lock);
1023 atomic_inc(&mrt->cache_resolve_queue_len);
1024 list_add(&c->list, &mrt->mfc_unres_queue);
1026 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1027 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1030 /* See if we can append the packet */
1032 if (c->mfc_un.unres.unresolved.qlen > 3) {
1036 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1040 spin_unlock_bh(&mfc_unres_lock);
1045 * MFC cache manipulation by user space mroute daemon
1048 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
1051 struct mfc_cache *c, *next;
1053 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1055 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1056 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1057 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1058 list_del_rcu(&c->list);
1067 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1068 struct mfcctl *mfc, int mrtsock)
1072 struct mfc_cache *uc, *c;
1074 if (mfc->mfcc_parent >= MAXVIFS)
1077 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1079 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1080 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1081 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1088 write_lock_bh(&mrt_lock);
1089 c->mfc_parent = mfc->mfcc_parent;
1090 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1092 c->mfc_flags |= MFC_STATIC;
1093 write_unlock_bh(&mrt_lock);
1097 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1100 c = ipmr_cache_alloc();
1104 c->mfc_origin = mfc->mfcc_origin.s_addr;
1105 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1106 c->mfc_parent = mfc->mfcc_parent;
1107 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1109 c->mfc_flags |= MFC_STATIC;
1111 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1114 * Check to see if we resolved a queued list. If so we
1115 * need to send on the frames and tidy up.
1118 spin_lock_bh(&mfc_unres_lock);
1119 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1120 if (uc->mfc_origin == c->mfc_origin &&
1121 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1122 list_del(&uc->list);
1123 atomic_dec(&mrt->cache_resolve_queue_len);
1128 if (list_empty(&mrt->mfc_unres_queue))
1129 del_timer(&mrt->ipmr_expire_timer);
1130 spin_unlock_bh(&mfc_unres_lock);
1133 ipmr_cache_resolve(net, mrt, uc, c);
1134 ipmr_cache_free(uc);
1140 * Close the multicast socket, and clear the vif tables etc
1143 static void mroute_clean_tables(struct mr_table *mrt)
1147 struct mfc_cache *c, *next;
1149 /* Shut down all active vif entries */
1151 for (i = 0; i < mrt->maxvif; i++) {
1152 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
1153 vif_delete(mrt, i, 0, &list);
1155 unregister_netdevice_many(&list);
1157 /* Wipe the cache */
1159 for (i = 0; i < MFC_LINES; i++) {
1160 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1161 if (c->mfc_flags & MFC_STATIC)
1163 list_del_rcu(&c->list);
1168 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1169 spin_lock_bh(&mfc_unres_lock);
1170 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1172 ipmr_destroy_unres(mrt, c);
1174 spin_unlock_bh(&mfc_unres_lock);
1178 /* called from ip_ra_control(), before an RCU grace period,
1179 * we dont need to call synchronize_rcu() here
1181 static void mrtsock_destruct(struct sock *sk)
1183 struct net *net = sock_net(sk);
1184 struct mr_table *mrt;
1187 ipmr_for_each_table(mrt, net) {
1188 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1189 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1190 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1191 mroute_clean_tables(mrt);
1198 * Socket options and virtual interface manipulation. The whole
1199 * virtual interface system is a complete heap, but unfortunately
1200 * that's how BSD mrouted happens to think. Maybe one day with a proper
1201 * MOSPF/PIM router set up we can clean this up.
1204 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1209 struct net *net = sock_net(sk);
1210 struct mr_table *mrt;
1212 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1216 if (optname != MRT_INIT) {
1217 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1218 !capable(CAP_NET_ADMIN))
1224 if (sk->sk_type != SOCK_RAW ||
1225 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1227 if (optlen != sizeof(int))
1228 return -ENOPROTOOPT;
1231 if (rtnl_dereference(mrt->mroute_sk)) {
1236 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1238 rcu_assign_pointer(mrt->mroute_sk, sk);
1239 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1244 if (sk != rcu_access_pointer(mrt->mroute_sk))
1246 return ip_ra_control(sk, 0, NULL);
1249 if (optlen != sizeof(vif))
1251 if (copy_from_user(&vif, optval, sizeof(vif)))
1253 if (vif.vifc_vifi >= MAXVIFS)
1256 if (optname == MRT_ADD_VIF) {
1257 ret = vif_add(net, mrt, &vif,
1258 sk == rtnl_dereference(mrt->mroute_sk));
1260 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1266 * Manipulate the forwarding caches. These live
1267 * in a sort of kernel/user symbiosis.
1271 if (optlen != sizeof(mfc))
1273 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1276 if (optname == MRT_DEL_MFC)
1277 ret = ipmr_mfc_delete(mrt, &mfc);
1279 ret = ipmr_mfc_add(net, mrt, &mfc,
1280 sk == rtnl_dereference(mrt->mroute_sk));
1284 * Control PIM assert.
1289 if (get_user(v, (int __user *)optval))
1291 mrt->mroute_do_assert = (v) ? 1 : 0;
1294 #ifdef CONFIG_IP_PIMSM
1299 if (get_user(v, (int __user *)optval))
1305 if (v != mrt->mroute_do_pim) {
1306 mrt->mroute_do_pim = v;
1307 mrt->mroute_do_assert = v;
1313 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1318 if (optlen != sizeof(u32))
1320 if (get_user(v, (u32 __user *)optval))
1325 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1328 if (!ipmr_new_table(net, v))
1330 raw_sk(sk)->ipmr_table = v;
1337 * Spurious command, or MRT_VERSION which you cannot
1341 return -ENOPROTOOPT;
1346 * Getsock opt support for the multicast routing system.
1349 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1353 struct net *net = sock_net(sk);
1354 struct mr_table *mrt;
1356 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1360 if (optname != MRT_VERSION &&
1361 #ifdef CONFIG_IP_PIMSM
1362 optname != MRT_PIM &&
1364 optname != MRT_ASSERT)
1365 return -ENOPROTOOPT;
1367 if (get_user(olr, optlen))
1370 olr = min_t(unsigned int, olr, sizeof(int));
1374 if (put_user(olr, optlen))
1376 if (optname == MRT_VERSION)
1378 #ifdef CONFIG_IP_PIMSM
1379 else if (optname == MRT_PIM)
1380 val = mrt->mroute_do_pim;
1383 val = mrt->mroute_do_assert;
1384 if (copy_to_user(optval, &val, olr))
1390 * The IP multicast ioctl support routines.
1393 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1395 struct sioc_sg_req sr;
1396 struct sioc_vif_req vr;
1397 struct vif_device *vif;
1398 struct mfc_cache *c;
1399 struct net *net = sock_net(sk);
1400 struct mr_table *mrt;
1402 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1408 if (copy_from_user(&vr, arg, sizeof(vr)))
1410 if (vr.vifi >= mrt->maxvif)
1412 read_lock(&mrt_lock);
1413 vif = &mrt->vif_table[vr.vifi];
1414 if (VIF_EXISTS(mrt, vr.vifi)) {
1415 vr.icount = vif->pkt_in;
1416 vr.ocount = vif->pkt_out;
1417 vr.ibytes = vif->bytes_in;
1418 vr.obytes = vif->bytes_out;
1419 read_unlock(&mrt_lock);
1421 if (copy_to_user(arg, &vr, sizeof(vr)))
1425 read_unlock(&mrt_lock);
1426 return -EADDRNOTAVAIL;
1428 if (copy_from_user(&sr, arg, sizeof(sr)))
1432 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1434 sr.pktcnt = c->mfc_un.res.pkt;
1435 sr.bytecnt = c->mfc_un.res.bytes;
1436 sr.wrong_if = c->mfc_un.res.wrong_if;
1439 if (copy_to_user(arg, &sr, sizeof(sr)))
1444 return -EADDRNOTAVAIL;
1446 return -ENOIOCTLCMD;
1450 #ifdef CONFIG_COMPAT
1451 struct compat_sioc_sg_req {
1454 compat_ulong_t pktcnt;
1455 compat_ulong_t bytecnt;
1456 compat_ulong_t wrong_if;
1459 struct compat_sioc_vif_req {
1460 vifi_t vifi; /* Which iface */
1461 compat_ulong_t icount;
1462 compat_ulong_t ocount;
1463 compat_ulong_t ibytes;
1464 compat_ulong_t obytes;
1467 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1469 struct compat_sioc_sg_req sr;
1470 struct compat_sioc_vif_req vr;
1471 struct vif_device *vif;
1472 struct mfc_cache *c;
1473 struct net *net = sock_net(sk);
1474 struct mr_table *mrt;
1476 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1482 if (copy_from_user(&vr, arg, sizeof(vr)))
1484 if (vr.vifi >= mrt->maxvif)
1486 read_lock(&mrt_lock);
1487 vif = &mrt->vif_table[vr.vifi];
1488 if (VIF_EXISTS(mrt, vr.vifi)) {
1489 vr.icount = vif->pkt_in;
1490 vr.ocount = vif->pkt_out;
1491 vr.ibytes = vif->bytes_in;
1492 vr.obytes = vif->bytes_out;
1493 read_unlock(&mrt_lock);
1495 if (copy_to_user(arg, &vr, sizeof(vr)))
1499 read_unlock(&mrt_lock);
1500 return -EADDRNOTAVAIL;
1502 if (copy_from_user(&sr, arg, sizeof(sr)))
1506 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1508 sr.pktcnt = c->mfc_un.res.pkt;
1509 sr.bytecnt = c->mfc_un.res.bytes;
1510 sr.wrong_if = c->mfc_un.res.wrong_if;
1513 if (copy_to_user(arg, &sr, sizeof(sr)))
1518 return -EADDRNOTAVAIL;
1520 return -ENOIOCTLCMD;
1526 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1528 struct net_device *dev = ptr;
1529 struct net *net = dev_net(dev);
1530 struct mr_table *mrt;
1531 struct vif_device *v;
1535 if (event != NETDEV_UNREGISTER)
1538 ipmr_for_each_table(mrt, net) {
1539 v = &mrt->vif_table[0];
1540 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1542 vif_delete(mrt, ct, 1, &list);
1545 unregister_netdevice_many(&list);
1550 static struct notifier_block ip_mr_notifier = {
1551 .notifier_call = ipmr_device_event,
1555 * Encapsulate a packet by attaching a valid IPIP header to it.
1556 * This avoids tunnel drivers and other mess and gives us the speed so
1557 * important for multicast video.
1560 static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1563 const struct iphdr *old_iph = ip_hdr(skb);
1565 skb_push(skb, sizeof(struct iphdr));
1566 skb->transport_header = skb->network_header;
1567 skb_reset_network_header(skb);
1571 iph->tos = old_iph->tos;
1572 iph->ttl = old_iph->ttl;
1576 iph->protocol = IPPROTO_IPIP;
1578 iph->tot_len = htons(skb->len);
1579 ip_select_ident(skb, skb_dst(skb), NULL);
1582 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1586 static inline int ipmr_forward_finish(struct sk_buff *skb)
1588 struct ip_options *opt = &(IPCB(skb)->opt);
1590 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
1592 if (unlikely(opt->optlen))
1593 ip_forward_options(skb);
1595 return dst_output(skb);
1599 * Processing handlers for ipmr_forward
1602 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1603 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1605 const struct iphdr *iph = ip_hdr(skb);
1606 struct vif_device *vif = &mrt->vif_table[vifi];
1607 struct net_device *dev;
1612 if (vif->dev == NULL)
1615 #ifdef CONFIG_IP_PIMSM
1616 if (vif->flags & VIFF_REGISTER) {
1618 vif->bytes_out += skb->len;
1619 vif->dev->stats.tx_bytes += skb->len;
1620 vif->dev->stats.tx_packets++;
1621 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1626 if (vif->flags & VIFF_TUNNEL) {
1627 rt = ip_route_output_ports(net, &fl4, NULL,
1628 vif->remote, vif->local,
1631 RT_TOS(iph->tos), vif->link);
1634 encap = sizeof(struct iphdr);
1636 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1639 RT_TOS(iph->tos), vif->link);
1646 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1647 /* Do not fragment multicasts. Alas, IPv4 does not
1648 * allow to send ICMP, so that packets will disappear
1652 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1657 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1659 if (skb_cow(skb, encap)) {
1665 vif->bytes_out += skb->len;
1668 skb_dst_set(skb, &rt->dst);
1669 ip_decrease_ttl(ip_hdr(skb));
1671 /* FIXME: forward and output firewalls used to be called here.
1672 * What do we do with netfilter? -- RR
1674 if (vif->flags & VIFF_TUNNEL) {
1675 ip_encap(skb, vif->local, vif->remote);
1676 /* FIXME: extra output firewall step used to be here. --RR */
1677 vif->dev->stats.tx_packets++;
1678 vif->dev->stats.tx_bytes += skb->len;
1681 IPCB(skb)->flags |= IPSKB_FORWARDED;
1684 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1685 * not only before forwarding, but after forwarding on all output
1686 * interfaces. It is clear, if mrouter runs a multicasting
1687 * program, it should receive packets not depending to what interface
1688 * program is joined.
1689 * If we will not make it, the program will have to join on all
1690 * interfaces. On the other hand, multihoming host (or router, but
1691 * not mrouter) cannot join to more than one interface - it will
1692 * result in receiving multiple packets.
1694 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
1695 ipmr_forward_finish);
1702 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1706 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1707 if (mrt->vif_table[ct].dev == dev)
1713 /* "local" means that we should preserve one skb (for local delivery) */
1715 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1716 struct sk_buff *skb, struct mfc_cache *cache,
1722 vif = cache->mfc_parent;
1723 cache->mfc_un.res.pkt++;
1724 cache->mfc_un.res.bytes += skb->len;
1727 * Wrong interface: drop packet and (maybe) send PIM assert.
1729 if (mrt->vif_table[vif].dev != skb->dev) {
1732 if (rt_is_output_route(skb_rtable(skb))) {
1733 /* It is our own packet, looped back.
1734 * Very complicated situation...
1736 * The best workaround until routing daemons will be
1737 * fixed is not to redistribute packet, if it was
1738 * send through wrong interface. It means, that
1739 * multicast applications WILL NOT work for
1740 * (S,G), which have default multicast route pointing
1741 * to wrong oif. In any case, it is not a good
1742 * idea to use multicasting applications on router.
1747 cache->mfc_un.res.wrong_if++;
1748 true_vifi = ipmr_find_vif(mrt, skb->dev);
1750 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1751 /* pimsm uses asserts, when switching from RPT to SPT,
1752 * so that we cannot check that packet arrived on an oif.
1753 * It is bad, but otherwise we would need to move pretty
1754 * large chunk of pimd to kernel. Ough... --ANK
1756 (mrt->mroute_do_pim ||
1757 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1759 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1760 cache->mfc_un.res.last_assert = jiffies;
1761 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1766 mrt->vif_table[vif].pkt_in++;
1767 mrt->vif_table[vif].bytes_in += skb->len;
1772 for (ct = cache->mfc_un.res.maxvif - 1;
1773 ct >= cache->mfc_un.res.minvif; ct--) {
1774 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1776 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1779 ipmr_queue_xmit(net, mrt, skb2, cache,
1787 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1790 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1792 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1803 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1805 struct rtable *rt = skb_rtable(skb);
1806 struct iphdr *iph = ip_hdr(skb);
1807 struct flowi4 fl4 = {
1808 .daddr = iph->daddr,
1809 .saddr = iph->saddr,
1810 .flowi4_tos = RT_TOS(iph->tos),
1811 .flowi4_oif = rt->rt_oif,
1812 .flowi4_iif = rt->rt_iif,
1813 .flowi4_mark = rt->rt_mark,
1815 struct mr_table *mrt;
1818 err = ipmr_fib_lookup(net, &fl4, &mrt);
1820 return ERR_PTR(err);
1825 * Multicast packets for forwarding arrive here
1826 * Called with rcu_read_lock();
1829 int ip_mr_input(struct sk_buff *skb)
1831 struct mfc_cache *cache;
1832 struct net *net = dev_net(skb->dev);
1833 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1834 struct mr_table *mrt;
1836 /* Packet is looped back after forward, it should not be
1837 * forwarded second time, but still can be delivered locally.
1839 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1842 mrt = ipmr_rt_fib_lookup(net, skb);
1845 return PTR_ERR(mrt);
1848 if (IPCB(skb)->opt.router_alert) {
1849 if (ip_call_ra_chain(skb))
1851 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1852 /* IGMPv1 (and broken IGMPv2 implementations sort of
1853 * Cisco IOS <= 11.2(8)) do not put router alert
1854 * option to IGMP packets destined to routable
1855 * groups. It is very bad, because it means
1856 * that we can forward NO IGMP messages.
1858 struct sock *mroute_sk;
1860 mroute_sk = rcu_dereference(mrt->mroute_sk);
1863 raw_rcv(mroute_sk, skb);
1869 /* already under rcu_read_lock() */
1870 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1873 * No usable cache entry
1875 if (cache == NULL) {
1879 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1880 ip_local_deliver(skb);
1886 read_lock(&mrt_lock);
1887 vif = ipmr_find_vif(mrt, skb->dev);
1889 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
1890 read_unlock(&mrt_lock);
1894 read_unlock(&mrt_lock);
1899 read_lock(&mrt_lock);
1900 ip_mr_forward(net, mrt, skb, cache, local);
1901 read_unlock(&mrt_lock);
1904 return ip_local_deliver(skb);
1910 return ip_local_deliver(skb);
1915 #ifdef CONFIG_IP_PIMSM
1916 /* called with rcu_read_lock() */
1917 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1918 unsigned int pimlen)
1920 struct net_device *reg_dev = NULL;
1921 struct iphdr *encap;
1923 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
1926 * a. packet is really sent to a multicast group
1927 * b. packet is not a NULL-REGISTER
1928 * c. packet is not truncated
1930 if (!ipv4_is_multicast(encap->daddr) ||
1931 encap->tot_len == 0 ||
1932 ntohs(encap->tot_len) + pimlen > skb->len)
1935 read_lock(&mrt_lock);
1936 if (mrt->mroute_reg_vif_num >= 0)
1937 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
1938 read_unlock(&mrt_lock);
1940 if (reg_dev == NULL)
1943 skb->mac_header = skb->network_header;
1944 skb_pull(skb, (u8 *)encap - skb->data);
1945 skb_reset_network_header(skb);
1946 skb->protocol = htons(ETH_P_IP);
1947 skb->ip_summed = CHECKSUM_NONE;
1948 skb->pkt_type = PACKET_HOST;
1950 skb_tunnel_rx(skb, reg_dev);
1954 return NET_RX_SUCCESS;
1958 #ifdef CONFIG_IP_PIMSM_V1
1960 * Handle IGMP messages of PIMv1
1963 int pim_rcv_v1(struct sk_buff *skb)
1965 struct igmphdr *pim;
1966 struct net *net = dev_net(skb->dev);
1967 struct mr_table *mrt;
1969 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1972 pim = igmp_hdr(skb);
1974 mrt = ipmr_rt_fib_lookup(net, skb);
1977 if (!mrt->mroute_do_pim ||
1978 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1981 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
1989 #ifdef CONFIG_IP_PIMSM_V2
1990 static int pim_rcv(struct sk_buff *skb)
1992 struct pimreghdr *pim;
1993 struct net *net = dev_net(skb->dev);
1994 struct mr_table *mrt;
1996 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1999 pim = (struct pimreghdr *)skb_transport_header(skb);
2000 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2001 (pim->flags & PIM_NULL_REGISTER) ||
2002 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2003 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2006 mrt = ipmr_rt_fib_lookup(net, skb);
2009 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2017 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2018 struct mfc_cache *c, struct rtmsg *rtm)
2021 struct rtnexthop *nhp;
2022 u8 *b = skb_tail_pointer(skb);
2023 struct rtattr *mp_head;
2025 /* If cache is unresolved, don't try to parse IIF and OIF */
2026 if (c->mfc_parent >= MAXVIFS)
2029 if (VIF_EXISTS(mrt, c->mfc_parent))
2030 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
2032 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
2034 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2035 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2036 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
2037 goto rtattr_failure;
2038 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
2039 nhp->rtnh_flags = 0;
2040 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2041 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2042 nhp->rtnh_len = sizeof(*nhp);
2045 mp_head->rta_type = RTA_MULTIPATH;
2046 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
2047 rtm->rtm_type = RTN_MULTICAST;
2055 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2056 __be32 saddr, __be32 daddr,
2057 struct rtmsg *rtm, int nowait)
2059 struct mfc_cache *cache;
2060 struct mr_table *mrt;
2063 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2068 cache = ipmr_cache_find(mrt, saddr, daddr);
2070 if (cache == NULL) {
2071 struct sk_buff *skb2;
2073 struct net_device *dev;
2082 read_lock(&mrt_lock);
2084 vif = ipmr_find_vif(mrt, dev);
2086 read_unlock(&mrt_lock);
2090 skb2 = skb_clone(skb, GFP_ATOMIC);
2092 read_unlock(&mrt_lock);
2097 skb_push(skb2, sizeof(struct iphdr));
2098 skb_reset_network_header(skb2);
2100 iph->ihl = sizeof(struct iphdr) >> 2;
2104 err = ipmr_cache_unresolved(mrt, vif, skb2);
2105 read_unlock(&mrt_lock);
2110 read_lock(&mrt_lock);
2111 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
2112 cache->mfc_flags |= MFC_NOTIFY;
2113 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2114 read_unlock(&mrt_lock);
2119 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2120 u32 pid, u32 seq, struct mfc_cache *c)
2122 struct nlmsghdr *nlh;
2125 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2129 rtm = nlmsg_data(nlh);
2130 rtm->rtm_family = RTNL_FAMILY_IPMR;
2131 rtm->rtm_dst_len = 32;
2132 rtm->rtm_src_len = 32;
2134 rtm->rtm_table = mrt->id;
2135 NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2136 rtm->rtm_type = RTN_MULTICAST;
2137 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2138 rtm->rtm_protocol = RTPROT_UNSPEC;
2141 NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2142 NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2144 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2145 goto nla_put_failure;
2147 return nlmsg_end(skb, nlh);
2150 nlmsg_cancel(skb, nlh);
2154 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2156 struct net *net = sock_net(skb->sk);
2157 struct mr_table *mrt;
2158 struct mfc_cache *mfc;
2159 unsigned int t = 0, s_t;
2160 unsigned int h = 0, s_h;
2161 unsigned int e = 0, s_e;
2168 ipmr_for_each_table(mrt, net) {
2173 for (h = s_h; h < MFC_LINES; h++) {
2174 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2177 if (ipmr_fill_mroute(mrt, skb,
2178 NETLINK_CB(cb->skb).pid,
2201 #ifdef CONFIG_PROC_FS
2203 * The /proc interfaces to multicast routing :
2204 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2206 struct ipmr_vif_iter {
2207 struct seq_net_private p;
2208 struct mr_table *mrt;
2212 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2213 struct ipmr_vif_iter *iter,
2216 struct mr_table *mrt = iter->mrt;
2218 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2219 if (!VIF_EXISTS(mrt, iter->ct))
2222 return &mrt->vif_table[iter->ct];
2227 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2228 __acquires(mrt_lock)
2230 struct ipmr_vif_iter *iter = seq->private;
2231 struct net *net = seq_file_net(seq);
2232 struct mr_table *mrt;
2234 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2236 return ERR_PTR(-ENOENT);
2240 read_lock(&mrt_lock);
2241 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2245 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2247 struct ipmr_vif_iter *iter = seq->private;
2248 struct net *net = seq_file_net(seq);
2249 struct mr_table *mrt = iter->mrt;
2252 if (v == SEQ_START_TOKEN)
2253 return ipmr_vif_seq_idx(net, iter, 0);
2255 while (++iter->ct < mrt->maxvif) {
2256 if (!VIF_EXISTS(mrt, iter->ct))
2258 return &mrt->vif_table[iter->ct];
2263 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2264 __releases(mrt_lock)
2266 read_unlock(&mrt_lock);
2269 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2271 struct ipmr_vif_iter *iter = seq->private;
2272 struct mr_table *mrt = iter->mrt;
2274 if (v == SEQ_START_TOKEN) {
2276 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2278 const struct vif_device *vif = v;
2279 const char *name = vif->dev ? vif->dev->name : "none";
2282 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2283 vif - mrt->vif_table,
2284 name, vif->bytes_in, vif->pkt_in,
2285 vif->bytes_out, vif->pkt_out,
2286 vif->flags, vif->local, vif->remote);
2291 static const struct seq_operations ipmr_vif_seq_ops = {
2292 .start = ipmr_vif_seq_start,
2293 .next = ipmr_vif_seq_next,
2294 .stop = ipmr_vif_seq_stop,
2295 .show = ipmr_vif_seq_show,
2298 static int ipmr_vif_open(struct inode *inode, struct file *file)
2300 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2301 sizeof(struct ipmr_vif_iter));
2304 static const struct file_operations ipmr_vif_fops = {
2305 .owner = THIS_MODULE,
2306 .open = ipmr_vif_open,
2308 .llseek = seq_lseek,
2309 .release = seq_release_net,
2312 struct ipmr_mfc_iter {
2313 struct seq_net_private p;
2314 struct mr_table *mrt;
2315 struct list_head *cache;
2320 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2321 struct ipmr_mfc_iter *it, loff_t pos)
2323 struct mr_table *mrt = it->mrt;
2324 struct mfc_cache *mfc;
2327 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2328 it->cache = &mrt->mfc_cache_array[it->ct];
2329 list_for_each_entry_rcu(mfc, it->cache, list)
2335 spin_lock_bh(&mfc_unres_lock);
2336 it->cache = &mrt->mfc_unres_queue;
2337 list_for_each_entry(mfc, it->cache, list)
2340 spin_unlock_bh(&mfc_unres_lock);
2347 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2349 struct ipmr_mfc_iter *it = seq->private;
2350 struct net *net = seq_file_net(seq);
2351 struct mr_table *mrt;
2353 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2355 return ERR_PTR(-ENOENT);
2360 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2364 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2366 struct mfc_cache *mfc = v;
2367 struct ipmr_mfc_iter *it = seq->private;
2368 struct net *net = seq_file_net(seq);
2369 struct mr_table *mrt = it->mrt;
2373 if (v == SEQ_START_TOKEN)
2374 return ipmr_mfc_seq_idx(net, seq->private, 0);
2376 if (mfc->list.next != it->cache)
2377 return list_entry(mfc->list.next, struct mfc_cache, list);
2379 if (it->cache == &mrt->mfc_unres_queue)
2382 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2384 while (++it->ct < MFC_LINES) {
2385 it->cache = &mrt->mfc_cache_array[it->ct];
2386 if (list_empty(it->cache))
2388 return list_first_entry(it->cache, struct mfc_cache, list);
2391 /* exhausted cache_array, show unresolved */
2393 it->cache = &mrt->mfc_unres_queue;
2396 spin_lock_bh(&mfc_unres_lock);
2397 if (!list_empty(it->cache))
2398 return list_first_entry(it->cache, struct mfc_cache, list);
2401 spin_unlock_bh(&mfc_unres_lock);
2407 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2409 struct ipmr_mfc_iter *it = seq->private;
2410 struct mr_table *mrt = it->mrt;
2412 if (it->cache == &mrt->mfc_unres_queue)
2413 spin_unlock_bh(&mfc_unres_lock);
2414 else if (it->cache == &mrt->mfc_cache_array[it->ct])
2418 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2422 if (v == SEQ_START_TOKEN) {
2424 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2426 const struct mfc_cache *mfc = v;
2427 const struct ipmr_mfc_iter *it = seq->private;
2428 const struct mr_table *mrt = it->mrt;
2430 seq_printf(seq, "%08X %08X %-3hd",
2431 (__force u32) mfc->mfc_mcastgrp,
2432 (__force u32) mfc->mfc_origin,
2435 if (it->cache != &mrt->mfc_unres_queue) {
2436 seq_printf(seq, " %8lu %8lu %8lu",
2437 mfc->mfc_un.res.pkt,
2438 mfc->mfc_un.res.bytes,
2439 mfc->mfc_un.res.wrong_if);
2440 for (n = mfc->mfc_un.res.minvif;
2441 n < mfc->mfc_un.res.maxvif; n++) {
2442 if (VIF_EXISTS(mrt, n) &&
2443 mfc->mfc_un.res.ttls[n] < 255)
2446 n, mfc->mfc_un.res.ttls[n]);
2449 /* unresolved mfc_caches don't contain
2450 * pkt, bytes and wrong_if values
2452 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2454 seq_putc(seq, '\n');
2459 static const struct seq_operations ipmr_mfc_seq_ops = {
2460 .start = ipmr_mfc_seq_start,
2461 .next = ipmr_mfc_seq_next,
2462 .stop = ipmr_mfc_seq_stop,
2463 .show = ipmr_mfc_seq_show,
2466 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2468 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2469 sizeof(struct ipmr_mfc_iter));
2472 static const struct file_operations ipmr_mfc_fops = {
2473 .owner = THIS_MODULE,
2474 .open = ipmr_mfc_open,
2476 .llseek = seq_lseek,
2477 .release = seq_release_net,
2481 #ifdef CONFIG_IP_PIMSM_V2
2482 static const struct net_protocol pim_protocol = {
2490 * Setup for IP multicast routing
2492 static int __net_init ipmr_net_init(struct net *net)
2496 err = ipmr_rules_init(net);
2500 #ifdef CONFIG_PROC_FS
2502 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2504 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2505 goto proc_cache_fail;
2509 #ifdef CONFIG_PROC_FS
2511 proc_net_remove(net, "ip_mr_vif");
2513 ipmr_rules_exit(net);
2519 static void __net_exit ipmr_net_exit(struct net *net)
2521 #ifdef CONFIG_PROC_FS
2522 proc_net_remove(net, "ip_mr_cache");
2523 proc_net_remove(net, "ip_mr_vif");
2525 ipmr_rules_exit(net);
2528 static struct pernet_operations ipmr_net_ops = {
2529 .init = ipmr_net_init,
2530 .exit = ipmr_net_exit,
2533 int __init ip_mr_init(void)
2537 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2538 sizeof(struct mfc_cache),
2539 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2544 err = register_pernet_subsys(&ipmr_net_ops);
2546 goto reg_pernet_fail;
2548 err = register_netdevice_notifier(&ip_mr_notifier);
2550 goto reg_notif_fail;
2551 #ifdef CONFIG_IP_PIMSM_V2
2552 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2553 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2555 goto add_proto_fail;
2558 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2559 NULL, ipmr_rtm_dumproute, NULL);
2562 #ifdef CONFIG_IP_PIMSM_V2
2564 unregister_netdevice_notifier(&ip_mr_notifier);
2567 unregister_pernet_subsys(&ipmr_net_ops);
2569 kmem_cache_destroy(mrt_cachep);