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, bool all);
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)
159 struct ipmr_result res;
160 struct fib_lookup_arg arg = {
162 .flags = FIB_LOOKUP_NOREF,
165 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
166 flowi4_to_flowi(flp4), 0, &arg);
173 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
174 int flags, struct fib_lookup_arg *arg)
176 struct ipmr_result *res = arg->result;
177 struct mr_table *mrt;
179 switch (rule->action) {
182 case FR_ACT_UNREACHABLE:
184 case FR_ACT_PROHIBIT:
186 case FR_ACT_BLACKHOLE:
191 mrt = ipmr_get_table(rule->fr_net, rule->table);
198 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
203 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
207 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
208 struct fib_rule_hdr *frh, struct nlattr **tb)
213 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
219 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
220 struct fib_rule_hdr *frh)
228 static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
229 .family = RTNL_FAMILY_IPMR,
230 .rule_size = sizeof(struct ipmr_rule),
231 .addr_size = sizeof(u32),
232 .action = ipmr_rule_action,
233 .match = ipmr_rule_match,
234 .configure = ipmr_rule_configure,
235 .compare = ipmr_rule_compare,
236 .default_pref = fib_default_rule_pref,
237 .fill = ipmr_rule_fill,
238 .nlgroup = RTNLGRP_IPV4_RULE,
239 .policy = ipmr_rule_policy,
240 .owner = THIS_MODULE,
243 static int __net_init ipmr_rules_init(struct net *net)
245 struct fib_rules_ops *ops;
246 struct mr_table *mrt;
249 ops = fib_rules_register(&ipmr_rules_ops_template, net);
253 INIT_LIST_HEAD(&net->ipv4.mr_tables);
255 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
261 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
265 net->ipv4.mr_rules_ops = ops;
271 fib_rules_unregister(ops);
275 static void __net_exit ipmr_rules_exit(struct net *net)
277 struct mr_table *mrt, *next;
279 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
280 list_del(&mrt->list);
281 ipmr_free_table(mrt);
283 fib_rules_unregister(net->ipv4.mr_rules_ops);
286 #define ipmr_for_each_table(mrt, net) \
287 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
289 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
291 return net->ipv4.mrt;
294 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
295 struct mr_table **mrt)
297 *mrt = net->ipv4.mrt;
301 static int __net_init ipmr_rules_init(struct net *net)
303 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
304 return net->ipv4.mrt ? 0 : -ENOMEM;
307 static void __net_exit ipmr_rules_exit(struct net *net)
309 ipmr_free_table(net->ipv4.mrt);
313 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
315 struct mr_table *mrt;
318 mrt = ipmr_get_table(net, id);
322 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
325 write_pnet(&mrt->net, net);
328 /* Forwarding cache */
329 for (i = 0; i < MFC_LINES; i++)
330 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
332 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
334 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
337 #ifdef CONFIG_IP_PIMSM
338 mrt->mroute_reg_vif_num = -1;
340 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
341 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
346 static void ipmr_free_table(struct mr_table *mrt)
348 del_timer_sync(&mrt->ipmr_expire_timer);
349 mroute_clean_tables(mrt, true);
353 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
355 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
357 struct net *net = dev_net(dev);
361 dev = __dev_get_by_name(net, "tunl0");
363 const struct net_device_ops *ops = dev->netdev_ops;
365 struct ip_tunnel_parm p;
367 memset(&p, 0, sizeof(p));
368 p.iph.daddr = v->vifc_rmt_addr.s_addr;
369 p.iph.saddr = v->vifc_lcl_addr.s_addr;
372 p.iph.protocol = IPPROTO_IPIP;
373 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
374 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
376 if (ops->ndo_do_ioctl) {
377 mm_segment_t oldfs = get_fs();
380 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
387 struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
389 struct net_device *dev;
391 dev = __dev_get_by_name(net, "tunl0");
394 const struct net_device_ops *ops = dev->netdev_ops;
397 struct ip_tunnel_parm p;
398 struct in_device *in_dev;
400 memset(&p, 0, sizeof(p));
401 p.iph.daddr = v->vifc_rmt_addr.s_addr;
402 p.iph.saddr = v->vifc_lcl_addr.s_addr;
405 p.iph.protocol = IPPROTO_IPIP;
406 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
407 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
409 if (ops->ndo_do_ioctl) {
410 mm_segment_t oldfs = get_fs();
413 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
421 (dev = __dev_get_by_name(net, p.name)) != NULL) {
422 dev->flags |= IFF_MULTICAST;
424 in_dev = __in_dev_get_rtnl(dev);
428 ipv4_devconf_setall(in_dev);
429 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
439 /* allow the register to be completed before unregistering. */
443 unregister_netdevice(dev);
447 #ifdef CONFIG_IP_PIMSM
449 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
451 struct net *net = dev_net(dev);
452 struct mr_table *mrt;
453 struct flowi4 fl4 = {
454 .flowi4_oif = dev->ifindex,
455 .flowi4_iif = skb->skb_iif,
456 .flowi4_mark = skb->mark,
460 err = ipmr_fib_lookup(net, &fl4, &mrt);
466 read_lock(&mrt_lock);
467 dev->stats.tx_bytes += skb->len;
468 dev->stats.tx_packets++;
469 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
470 read_unlock(&mrt_lock);
475 static const struct net_device_ops reg_vif_netdev_ops = {
476 .ndo_start_xmit = reg_vif_xmit,
479 static void reg_vif_setup(struct net_device *dev)
481 dev->type = ARPHRD_PIMREG;
482 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
483 dev->flags = IFF_NOARP;
484 dev->netdev_ops = ®_vif_netdev_ops,
485 dev->destructor = free_netdev;
486 dev->features |= NETIF_F_NETNS_LOCAL;
489 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
491 struct net_device *dev;
492 struct in_device *in_dev;
495 if (mrt->id == RT_TABLE_DEFAULT)
496 sprintf(name, "pimreg");
498 sprintf(name, "pimreg%u", mrt->id);
500 dev = alloc_netdev(0, name, reg_vif_setup);
505 dev_net_set(dev, net);
507 if (register_netdevice(dev)) {
514 in_dev = __in_dev_get_rcu(dev);
520 ipv4_devconf_setall(in_dev);
521 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
532 /* allow the register to be completed before unregistering. */
536 unregister_netdevice(dev);
543 * @notify: Set to 1, if the caller is a notifier_call
546 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
547 struct list_head *head)
549 struct vif_device *v;
550 struct net_device *dev;
551 struct in_device *in_dev;
553 if (vifi < 0 || vifi >= mrt->maxvif)
554 return -EADDRNOTAVAIL;
556 v = &mrt->vif_table[vifi];
558 write_lock_bh(&mrt_lock);
563 write_unlock_bh(&mrt_lock);
564 return -EADDRNOTAVAIL;
567 #ifdef CONFIG_IP_PIMSM
568 if (vifi == mrt->mroute_reg_vif_num)
569 mrt->mroute_reg_vif_num = -1;
572 if (vifi + 1 == mrt->maxvif) {
575 for (tmp = vifi - 1; tmp >= 0; tmp--) {
576 if (VIF_EXISTS(mrt, tmp))
582 write_unlock_bh(&mrt_lock);
584 dev_set_allmulti(dev, -1);
586 in_dev = __in_dev_get_rtnl(dev);
588 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
589 ip_rt_multicast_event(in_dev);
592 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
593 unregister_netdevice_queue(dev, head);
599 static void ipmr_cache_free_rcu(struct rcu_head *head)
601 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
603 kmem_cache_free(mrt_cachep, c);
606 static inline void ipmr_cache_free(struct mfc_cache *c)
608 call_rcu(&c->rcu, ipmr_cache_free_rcu);
611 /* Destroy an unresolved cache entry, killing queued skbs
612 * and reporting error to netlink readers.
615 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
617 struct net *net = read_pnet(&mrt->net);
621 atomic_dec(&mrt->cache_resolve_queue_len);
623 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
624 if (ip_hdr(skb)->version == 0) {
625 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
626 nlh->nlmsg_type = NLMSG_ERROR;
627 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
628 skb_trim(skb, nlh->nlmsg_len);
630 e->error = -ETIMEDOUT;
631 memset(&e->msg, 0, sizeof(e->msg));
633 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
643 /* Timer process for the unresolved queue. */
645 static void ipmr_expire_process(unsigned long arg)
647 struct mr_table *mrt = (struct mr_table *)arg;
649 unsigned long expires;
650 struct mfc_cache *c, *next;
652 if (!spin_trylock(&mfc_unres_lock)) {
653 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
657 if (list_empty(&mrt->mfc_unres_queue))
663 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
664 if (time_after(c->mfc_un.unres.expires, now)) {
665 unsigned long interval = c->mfc_un.unres.expires - now;
666 if (interval < expires)
672 ipmr_destroy_unres(mrt, c);
675 if (!list_empty(&mrt->mfc_unres_queue))
676 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
679 spin_unlock(&mfc_unres_lock);
682 /* Fill oifs list. It is called under write locked mrt_lock. */
684 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
689 cache->mfc_un.res.minvif = MAXVIFS;
690 cache->mfc_un.res.maxvif = 0;
691 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
693 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
694 if (VIF_EXISTS(mrt, vifi) &&
695 ttls[vifi] && ttls[vifi] < 255) {
696 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
697 if (cache->mfc_un.res.minvif > vifi)
698 cache->mfc_un.res.minvif = vifi;
699 if (cache->mfc_un.res.maxvif <= vifi)
700 cache->mfc_un.res.maxvif = vifi + 1;
705 static int vif_add(struct net *net, struct mr_table *mrt,
706 struct vifctl *vifc, int mrtsock)
708 int vifi = vifc->vifc_vifi;
709 struct vif_device *v = &mrt->vif_table[vifi];
710 struct net_device *dev;
711 struct in_device *in_dev;
715 if (VIF_EXISTS(mrt, vifi))
718 switch (vifc->vifc_flags) {
719 #ifdef CONFIG_IP_PIMSM
722 * Special Purpose VIF in PIM
723 * All the packets will be sent to the daemon
725 if (mrt->mroute_reg_vif_num >= 0)
727 dev = ipmr_reg_vif(net, mrt);
730 err = dev_set_allmulti(dev, 1);
732 unregister_netdevice(dev);
739 dev = ipmr_new_tunnel(net, vifc);
742 err = dev_set_allmulti(dev, 1);
744 ipmr_del_tunnel(dev, vifc);
750 case VIFF_USE_IFINDEX:
752 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
753 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
754 if (dev && __in_dev_get_rtnl(dev) == NULL) {
756 return -EADDRNOTAVAIL;
759 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
762 return -EADDRNOTAVAIL;
763 err = dev_set_allmulti(dev, 1);
773 in_dev = __in_dev_get_rtnl(dev);
776 return -EADDRNOTAVAIL;
778 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
779 ip_rt_multicast_event(in_dev);
781 /* Fill in the VIF structures */
783 v->rate_limit = vifc->vifc_rate_limit;
784 v->local = vifc->vifc_lcl_addr.s_addr;
785 v->remote = vifc->vifc_rmt_addr.s_addr;
786 v->flags = vifc->vifc_flags;
788 v->flags |= VIFF_STATIC;
789 v->threshold = vifc->vifc_threshold;
794 v->link = dev->ifindex;
795 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
796 v->link = dev->iflink;
798 /* And finish update writing critical data */
799 write_lock_bh(&mrt_lock);
801 #ifdef CONFIG_IP_PIMSM
802 if (v->flags & VIFF_REGISTER)
803 mrt->mroute_reg_vif_num = vifi;
805 if (vifi+1 > mrt->maxvif)
806 mrt->maxvif = vifi+1;
807 write_unlock_bh(&mrt_lock);
811 /* called with rcu_read_lock() */
812 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
816 int line = MFC_HASH(mcastgrp, origin);
819 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
820 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
827 * Allocate a multicast cache entry
829 static struct mfc_cache *ipmr_cache_alloc(void)
831 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
834 c->mfc_un.res.minvif = MAXVIFS;
838 static struct mfc_cache *ipmr_cache_alloc_unres(void)
840 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
843 skb_queue_head_init(&c->mfc_un.unres.unresolved);
844 c->mfc_un.unres.expires = jiffies + 10*HZ;
850 * A cache entry has gone into a resolved state from queued
853 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
854 struct mfc_cache *uc, struct mfc_cache *c)
859 /* Play the pending entries through our router */
861 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
862 if (ip_hdr(skb)->version == 0) {
863 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
865 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
866 nlh->nlmsg_len = skb_tail_pointer(skb) -
869 nlh->nlmsg_type = NLMSG_ERROR;
870 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
871 skb_trim(skb, nlh->nlmsg_len);
873 e->error = -EMSGSIZE;
874 memset(&e->msg, 0, sizeof(e->msg));
877 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
879 ip_mr_forward(net, mrt, skb, c, 0);
885 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
886 * expects the following bizarre scheme.
888 * Called under mrt_lock.
891 static int ipmr_cache_report(struct mr_table *mrt,
892 struct sk_buff *pkt, vifi_t vifi, int assert)
895 const int ihl = ip_hdrlen(pkt);
896 struct igmphdr *igmp;
898 struct sock *mroute_sk;
901 #ifdef CONFIG_IP_PIMSM
902 if (assert == IGMPMSG_WHOLEPKT)
903 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
906 skb = alloc_skb(128, GFP_ATOMIC);
911 #ifdef CONFIG_IP_PIMSM
912 if (assert == IGMPMSG_WHOLEPKT) {
913 /* Ugly, but we have no choice with this interface.
914 * Duplicate old header, fix ihl, length etc.
915 * And all this only to mangle msg->im_msgtype and
916 * to set msg->im_mbz to "mbz" :-)
918 skb_push(skb, sizeof(struct iphdr));
919 skb_reset_network_header(skb);
920 skb_reset_transport_header(skb);
921 msg = (struct igmpmsg *)skb_network_header(skb);
922 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
923 msg->im_msgtype = IGMPMSG_WHOLEPKT;
925 msg->im_vif = mrt->mroute_reg_vif_num;
926 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
927 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
928 sizeof(struct iphdr));
933 /* Copy the IP header */
935 skb->network_header = skb->tail;
937 skb_copy_to_linear_data(skb, pkt->data, ihl);
938 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
939 msg = (struct igmpmsg *)skb_network_header(skb);
941 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
945 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
947 msg->im_msgtype = assert;
949 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
950 skb->transport_header = skb->network_header;
954 mroute_sk = rcu_dereference(mrt->mroute_sk);
955 if (mroute_sk == NULL) {
961 /* Deliver to mrouted */
963 ret = sock_queue_rcv_skb(mroute_sk, skb);
967 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
975 * Queue a packet for resolution. It gets locked cache entry!
979 ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
984 const struct iphdr *iph = ip_hdr(skb);
986 spin_lock_bh(&mfc_unres_lock);
987 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
988 if (c->mfc_mcastgrp == iph->daddr &&
989 c->mfc_origin == iph->saddr) {
996 /* Create a new entry if allowable */
998 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
999 (c = ipmr_cache_alloc_unres()) == NULL) {
1000 spin_unlock_bh(&mfc_unres_lock);
1006 /* Fill in the new cache entry */
1009 c->mfc_origin = iph->saddr;
1010 c->mfc_mcastgrp = iph->daddr;
1012 /* Reflect first query at mrouted. */
1014 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1016 /* If the report failed throw the cache entry
1019 spin_unlock_bh(&mfc_unres_lock);
1026 atomic_inc(&mrt->cache_resolve_queue_len);
1027 list_add(&c->list, &mrt->mfc_unres_queue);
1029 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1030 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1033 /* See if we can append the packet */
1035 if (c->mfc_un.unres.unresolved.qlen > 3) {
1039 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1043 spin_unlock_bh(&mfc_unres_lock);
1048 * MFC cache manipulation by user space mroute daemon
1051 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
1054 struct mfc_cache *c, *next;
1056 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1058 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1059 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1060 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1061 list_del_rcu(&c->list);
1070 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1071 struct mfcctl *mfc, int mrtsock)
1075 struct mfc_cache *uc, *c;
1077 if (mfc->mfcc_parent >= MAXVIFS)
1080 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1082 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1083 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1084 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1091 write_lock_bh(&mrt_lock);
1092 c->mfc_parent = mfc->mfcc_parent;
1093 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1095 c->mfc_flags |= MFC_STATIC;
1096 write_unlock_bh(&mrt_lock);
1100 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1103 c = ipmr_cache_alloc();
1107 c->mfc_origin = mfc->mfcc_origin.s_addr;
1108 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1109 c->mfc_parent = mfc->mfcc_parent;
1110 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1112 c->mfc_flags |= MFC_STATIC;
1114 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1117 * Check to see if we resolved a queued list. If so we
1118 * need to send on the frames and tidy up.
1121 spin_lock_bh(&mfc_unres_lock);
1122 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1123 if (uc->mfc_origin == c->mfc_origin &&
1124 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1125 list_del(&uc->list);
1126 atomic_dec(&mrt->cache_resolve_queue_len);
1131 if (list_empty(&mrt->mfc_unres_queue))
1132 del_timer(&mrt->ipmr_expire_timer);
1133 spin_unlock_bh(&mfc_unres_lock);
1136 ipmr_cache_resolve(net, mrt, uc, c);
1137 ipmr_cache_free(uc);
1143 * Close the multicast socket, and clear the vif tables etc
1146 static void mroute_clean_tables(struct mr_table *mrt, bool all)
1150 struct mfc_cache *c, *next;
1152 /* Shut down all active vif entries */
1154 for (i = 0; i < mrt->maxvif; i++) {
1155 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1157 vif_delete(mrt, i, 0, &list);
1159 unregister_netdevice_many(&list);
1161 /* Wipe the cache */
1163 for (i = 0; i < MFC_LINES; i++) {
1164 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1165 if (!all && (c->mfc_flags & MFC_STATIC))
1167 list_del_rcu(&c->list);
1172 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1173 spin_lock_bh(&mfc_unres_lock);
1174 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1176 ipmr_destroy_unres(mrt, c);
1178 spin_unlock_bh(&mfc_unres_lock);
1182 /* called from ip_ra_control(), before an RCU grace period,
1183 * we dont need to call synchronize_rcu() here
1185 static void mrtsock_destruct(struct sock *sk)
1187 struct net *net = sock_net(sk);
1188 struct mr_table *mrt;
1191 ipmr_for_each_table(mrt, net) {
1192 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1193 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1194 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1195 mroute_clean_tables(mrt, false);
1202 * Socket options and virtual interface manipulation. The whole
1203 * virtual interface system is a complete heap, but unfortunately
1204 * that's how BSD mrouted happens to think. Maybe one day with a proper
1205 * MOSPF/PIM router set up we can clean this up.
1208 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1213 struct net *net = sock_net(sk);
1214 struct mr_table *mrt;
1216 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1220 if (optname != MRT_INIT) {
1221 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1222 !capable(CAP_NET_ADMIN))
1228 if (sk->sk_type != SOCK_RAW ||
1229 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1231 if (optlen != sizeof(int))
1232 return -ENOPROTOOPT;
1235 if (rtnl_dereference(mrt->mroute_sk)) {
1240 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1242 rcu_assign_pointer(mrt->mroute_sk, sk);
1243 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1248 if (sk != rcu_access_pointer(mrt->mroute_sk))
1250 return ip_ra_control(sk, 0, NULL);
1253 if (optlen != sizeof(vif))
1255 if (copy_from_user(&vif, optval, sizeof(vif)))
1257 if (vif.vifc_vifi >= MAXVIFS)
1260 if (optname == MRT_ADD_VIF) {
1261 ret = vif_add(net, mrt, &vif,
1262 sk == rtnl_dereference(mrt->mroute_sk));
1264 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1270 * Manipulate the forwarding caches. These live
1271 * in a sort of kernel/user symbiosis.
1275 if (optlen != sizeof(mfc))
1277 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1280 if (optname == MRT_DEL_MFC)
1281 ret = ipmr_mfc_delete(mrt, &mfc);
1283 ret = ipmr_mfc_add(net, mrt, &mfc,
1284 sk == rtnl_dereference(mrt->mroute_sk));
1288 * Control PIM assert.
1293 if (get_user(v, (int __user *)optval))
1295 mrt->mroute_do_assert = (v) ? 1 : 0;
1298 #ifdef CONFIG_IP_PIMSM
1303 if (get_user(v, (int __user *)optval))
1309 if (v != mrt->mroute_do_pim) {
1310 mrt->mroute_do_pim = v;
1311 mrt->mroute_do_assert = v;
1317 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1322 if (optlen != sizeof(u32))
1324 if (get_user(v, (u32 __user *)optval))
1329 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1332 if (!ipmr_new_table(net, v))
1334 raw_sk(sk)->ipmr_table = v;
1341 * Spurious command, or MRT_VERSION which you cannot
1345 return -ENOPROTOOPT;
1350 * Getsock opt support for the multicast routing system.
1353 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1357 struct net *net = sock_net(sk);
1358 struct mr_table *mrt;
1360 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1364 if (optname != MRT_VERSION &&
1365 #ifdef CONFIG_IP_PIMSM
1366 optname != MRT_PIM &&
1368 optname != MRT_ASSERT)
1369 return -ENOPROTOOPT;
1371 if (get_user(olr, optlen))
1374 olr = min_t(unsigned int, olr, sizeof(int));
1378 if (put_user(olr, optlen))
1380 if (optname == MRT_VERSION)
1382 #ifdef CONFIG_IP_PIMSM
1383 else if (optname == MRT_PIM)
1384 val = mrt->mroute_do_pim;
1387 val = mrt->mroute_do_assert;
1388 if (copy_to_user(optval, &val, olr))
1394 * The IP multicast ioctl support routines.
1397 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1399 struct sioc_sg_req sr;
1400 struct sioc_vif_req vr;
1401 struct vif_device *vif;
1402 struct mfc_cache *c;
1403 struct net *net = sock_net(sk);
1404 struct mr_table *mrt;
1406 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1412 if (copy_from_user(&vr, arg, sizeof(vr)))
1414 if (vr.vifi >= mrt->maxvif)
1416 read_lock(&mrt_lock);
1417 vif = &mrt->vif_table[vr.vifi];
1418 if (VIF_EXISTS(mrt, vr.vifi)) {
1419 vr.icount = vif->pkt_in;
1420 vr.ocount = vif->pkt_out;
1421 vr.ibytes = vif->bytes_in;
1422 vr.obytes = vif->bytes_out;
1423 read_unlock(&mrt_lock);
1425 if (copy_to_user(arg, &vr, sizeof(vr)))
1429 read_unlock(&mrt_lock);
1430 return -EADDRNOTAVAIL;
1432 if (copy_from_user(&sr, arg, sizeof(sr)))
1436 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1438 sr.pktcnt = c->mfc_un.res.pkt;
1439 sr.bytecnt = c->mfc_un.res.bytes;
1440 sr.wrong_if = c->mfc_un.res.wrong_if;
1443 if (copy_to_user(arg, &sr, sizeof(sr)))
1448 return -EADDRNOTAVAIL;
1450 return -ENOIOCTLCMD;
1454 #ifdef CONFIG_COMPAT
1455 struct compat_sioc_sg_req {
1458 compat_ulong_t pktcnt;
1459 compat_ulong_t bytecnt;
1460 compat_ulong_t wrong_if;
1463 struct compat_sioc_vif_req {
1464 vifi_t vifi; /* Which iface */
1465 compat_ulong_t icount;
1466 compat_ulong_t ocount;
1467 compat_ulong_t ibytes;
1468 compat_ulong_t obytes;
1471 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1473 struct compat_sioc_sg_req sr;
1474 struct compat_sioc_vif_req vr;
1475 struct vif_device *vif;
1476 struct mfc_cache *c;
1477 struct net *net = sock_net(sk);
1478 struct mr_table *mrt;
1480 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1486 if (copy_from_user(&vr, arg, sizeof(vr)))
1488 if (vr.vifi >= mrt->maxvif)
1490 read_lock(&mrt_lock);
1491 vif = &mrt->vif_table[vr.vifi];
1492 if (VIF_EXISTS(mrt, vr.vifi)) {
1493 vr.icount = vif->pkt_in;
1494 vr.ocount = vif->pkt_out;
1495 vr.ibytes = vif->bytes_in;
1496 vr.obytes = vif->bytes_out;
1497 read_unlock(&mrt_lock);
1499 if (copy_to_user(arg, &vr, sizeof(vr)))
1503 read_unlock(&mrt_lock);
1504 return -EADDRNOTAVAIL;
1506 if (copy_from_user(&sr, arg, sizeof(sr)))
1510 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1512 sr.pktcnt = c->mfc_un.res.pkt;
1513 sr.bytecnt = c->mfc_un.res.bytes;
1514 sr.wrong_if = c->mfc_un.res.wrong_if;
1517 if (copy_to_user(arg, &sr, sizeof(sr)))
1522 return -EADDRNOTAVAIL;
1524 return -ENOIOCTLCMD;
1530 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1532 struct net_device *dev = ptr;
1533 struct net *net = dev_net(dev);
1534 struct mr_table *mrt;
1535 struct vif_device *v;
1539 if (event != NETDEV_UNREGISTER)
1542 ipmr_for_each_table(mrt, net) {
1543 v = &mrt->vif_table[0];
1544 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1546 vif_delete(mrt, ct, 1, &list);
1549 unregister_netdevice_many(&list);
1554 static struct notifier_block ip_mr_notifier = {
1555 .notifier_call = ipmr_device_event,
1559 * Encapsulate a packet by attaching a valid IPIP header to it.
1560 * This avoids tunnel drivers and other mess and gives us the speed so
1561 * important for multicast video.
1564 static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1567 const struct iphdr *old_iph = ip_hdr(skb);
1569 skb_push(skb, sizeof(struct iphdr));
1570 skb->transport_header = skb->network_header;
1571 skb_reset_network_header(skb);
1575 iph->tos = old_iph->tos;
1576 iph->ttl = old_iph->ttl;
1580 iph->protocol = IPPROTO_IPIP;
1582 iph->tot_len = htons(skb->len);
1583 ip_select_ident(skb, NULL);
1586 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1590 static inline int ipmr_forward_finish(struct sk_buff *skb)
1592 struct ip_options *opt = &(IPCB(skb)->opt);
1594 IP_INC_STATS(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
1596 if (unlikely(opt->optlen))
1597 ip_forward_options(skb);
1599 return dst_output(skb);
1603 * Processing handlers for ipmr_forward
1606 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1607 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1609 const struct iphdr *iph = ip_hdr(skb);
1610 struct vif_device *vif = &mrt->vif_table[vifi];
1611 struct net_device *dev;
1616 if (vif->dev == NULL)
1619 #ifdef CONFIG_IP_PIMSM
1620 if (vif->flags & VIFF_REGISTER) {
1622 vif->bytes_out += skb->len;
1623 vif->dev->stats.tx_bytes += skb->len;
1624 vif->dev->stats.tx_packets++;
1625 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1630 if (vif->flags & VIFF_TUNNEL) {
1631 rt = ip_route_output_ports(net, &fl4, NULL,
1632 vif->remote, vif->local,
1635 RT_TOS(iph->tos), vif->link);
1638 encap = sizeof(struct iphdr);
1640 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1643 RT_TOS(iph->tos), vif->link);
1650 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1651 /* Do not fragment multicasts. Alas, IPv4 does not
1652 * allow to send ICMP, so that packets will disappear
1656 IP_INC_STATS(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1661 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1663 if (skb_cow(skb, encap)) {
1669 vif->bytes_out += skb->len;
1672 skb_dst_set(skb, &rt->dst);
1673 ip_decrease_ttl(ip_hdr(skb));
1675 /* FIXME: forward and output firewalls used to be called here.
1676 * What do we do with netfilter? -- RR
1678 if (vif->flags & VIFF_TUNNEL) {
1679 ip_encap(skb, vif->local, vif->remote);
1680 /* FIXME: extra output firewall step used to be here. --RR */
1681 vif->dev->stats.tx_packets++;
1682 vif->dev->stats.tx_bytes += skb->len;
1685 IPCB(skb)->flags |= IPSKB_FORWARDED;
1688 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1689 * not only before forwarding, but after forwarding on all output
1690 * interfaces. It is clear, if mrouter runs a multicasting
1691 * program, it should receive packets not depending to what interface
1692 * program is joined.
1693 * If we will not make it, the program will have to join on all
1694 * interfaces. On the other hand, multihoming host (or router, but
1695 * not mrouter) cannot join to more than one interface - it will
1696 * result in receiving multiple packets.
1698 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
1699 ipmr_forward_finish);
1706 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1710 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1711 if (mrt->vif_table[ct].dev == dev)
1717 /* "local" means that we should preserve one skb (for local delivery) */
1719 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1720 struct sk_buff *skb, struct mfc_cache *cache,
1726 vif = cache->mfc_parent;
1727 cache->mfc_un.res.pkt++;
1728 cache->mfc_un.res.bytes += skb->len;
1731 * Wrong interface: drop packet and (maybe) send PIM assert.
1733 if (mrt->vif_table[vif].dev != skb->dev) {
1736 if (rt_is_output_route(skb_rtable(skb))) {
1737 /* It is our own packet, looped back.
1738 * Very complicated situation...
1740 * The best workaround until routing daemons will be
1741 * fixed is not to redistribute packet, if it was
1742 * send through wrong interface. It means, that
1743 * multicast applications WILL NOT work for
1744 * (S,G), which have default multicast route pointing
1745 * to wrong oif. In any case, it is not a good
1746 * idea to use multicasting applications on router.
1751 cache->mfc_un.res.wrong_if++;
1752 true_vifi = ipmr_find_vif(mrt, skb->dev);
1754 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1755 /* pimsm uses asserts, when switching from RPT to SPT,
1756 * so that we cannot check that packet arrived on an oif.
1757 * It is bad, but otherwise we would need to move pretty
1758 * large chunk of pimd to kernel. Ough... --ANK
1760 (mrt->mroute_do_pim ||
1761 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1763 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1764 cache->mfc_un.res.last_assert = jiffies;
1765 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1770 mrt->vif_table[vif].pkt_in++;
1771 mrt->vif_table[vif].bytes_in += skb->len;
1776 for (ct = cache->mfc_un.res.maxvif - 1;
1777 ct >= cache->mfc_un.res.minvif; ct--) {
1778 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1780 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1783 ipmr_queue_xmit(net, mrt, skb2, cache,
1791 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1794 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1796 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1807 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1809 struct rtable *rt = skb_rtable(skb);
1810 struct iphdr *iph = ip_hdr(skb);
1811 struct flowi4 fl4 = {
1812 .daddr = iph->daddr,
1813 .saddr = iph->saddr,
1814 .flowi4_tos = RT_TOS(iph->tos),
1815 .flowi4_oif = rt->rt_oif,
1816 .flowi4_iif = rt->rt_iif,
1817 .flowi4_mark = rt->rt_mark,
1819 struct mr_table *mrt;
1822 err = ipmr_fib_lookup(net, &fl4, &mrt);
1824 return ERR_PTR(err);
1829 * Multicast packets for forwarding arrive here
1830 * Called with rcu_read_lock();
1833 int ip_mr_input(struct sk_buff *skb)
1835 struct mfc_cache *cache;
1836 struct net *net = dev_net(skb->dev);
1837 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1838 struct mr_table *mrt;
1840 /* Packet is looped back after forward, it should not be
1841 * forwarded second time, but still can be delivered locally.
1843 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1846 mrt = ipmr_rt_fib_lookup(net, skb);
1849 return PTR_ERR(mrt);
1852 if (IPCB(skb)->opt.router_alert) {
1853 if (ip_call_ra_chain(skb))
1855 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1856 /* IGMPv1 (and broken IGMPv2 implementations sort of
1857 * Cisco IOS <= 11.2(8)) do not put router alert
1858 * option to IGMP packets destined to routable
1859 * groups. It is very bad, because it means
1860 * that we can forward NO IGMP messages.
1862 struct sock *mroute_sk;
1864 mroute_sk = rcu_dereference(mrt->mroute_sk);
1867 raw_rcv(mroute_sk, skb);
1873 /* already under rcu_read_lock() */
1874 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1877 * No usable cache entry
1879 if (cache == NULL) {
1883 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1884 ip_local_deliver(skb);
1890 read_lock(&mrt_lock);
1891 vif = ipmr_find_vif(mrt, skb->dev);
1893 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
1894 read_unlock(&mrt_lock);
1898 read_unlock(&mrt_lock);
1903 read_lock(&mrt_lock);
1904 ip_mr_forward(net, mrt, skb, cache, local);
1905 read_unlock(&mrt_lock);
1908 return ip_local_deliver(skb);
1914 return ip_local_deliver(skb);
1919 #ifdef CONFIG_IP_PIMSM
1920 /* called with rcu_read_lock() */
1921 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1922 unsigned int pimlen)
1924 struct net_device *reg_dev = NULL;
1925 struct iphdr *encap;
1927 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
1930 * a. packet is really sent to a multicast group
1931 * b. packet is not a NULL-REGISTER
1932 * c. packet is not truncated
1934 if (!ipv4_is_multicast(encap->daddr) ||
1935 encap->tot_len == 0 ||
1936 ntohs(encap->tot_len) + pimlen > skb->len)
1939 read_lock(&mrt_lock);
1940 if (mrt->mroute_reg_vif_num >= 0)
1941 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
1942 read_unlock(&mrt_lock);
1944 if (reg_dev == NULL)
1947 skb->mac_header = skb->network_header;
1948 skb_pull(skb, (u8 *)encap - skb->data);
1949 skb_reset_network_header(skb);
1950 skb->protocol = htons(ETH_P_IP);
1951 skb->ip_summed = CHECKSUM_NONE;
1952 skb->pkt_type = PACKET_HOST;
1954 skb_tunnel_rx(skb, reg_dev);
1958 return NET_RX_SUCCESS;
1962 #ifdef CONFIG_IP_PIMSM_V1
1964 * Handle IGMP messages of PIMv1
1967 int pim_rcv_v1(struct sk_buff *skb)
1969 struct igmphdr *pim;
1970 struct net *net = dev_net(skb->dev);
1971 struct mr_table *mrt;
1973 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1976 pim = igmp_hdr(skb);
1978 mrt = ipmr_rt_fib_lookup(net, skb);
1981 if (!mrt->mroute_do_pim ||
1982 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1985 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
1993 #ifdef CONFIG_IP_PIMSM_V2
1994 static int pim_rcv(struct sk_buff *skb)
1996 struct pimreghdr *pim;
1997 struct net *net = dev_net(skb->dev);
1998 struct mr_table *mrt;
2000 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2003 pim = (struct pimreghdr *)skb_transport_header(skb);
2004 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2005 (pim->flags & PIM_NULL_REGISTER) ||
2006 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2007 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2010 mrt = ipmr_rt_fib_lookup(net, skb);
2013 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2021 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2022 struct mfc_cache *c, struct rtmsg *rtm)
2025 struct rtnexthop *nhp;
2026 u8 *b = skb_tail_pointer(skb);
2027 struct rtattr *mp_head;
2029 /* If cache is unresolved, don't try to parse IIF and OIF */
2030 if (c->mfc_parent >= MAXVIFS)
2033 if (VIF_EXISTS(mrt, c->mfc_parent))
2034 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
2036 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
2038 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2039 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2040 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
2041 goto rtattr_failure;
2042 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
2043 nhp->rtnh_flags = 0;
2044 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2045 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2046 nhp->rtnh_len = sizeof(*nhp);
2049 mp_head->rta_type = RTA_MULTIPATH;
2050 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
2051 rtm->rtm_type = RTN_MULTICAST;
2059 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2060 __be32 saddr, __be32 daddr,
2061 struct rtmsg *rtm, int nowait, u32 portid)
2063 struct mfc_cache *cache;
2064 struct mr_table *mrt;
2067 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2072 cache = ipmr_cache_find(mrt, saddr, daddr);
2074 if (cache == NULL) {
2075 struct sk_buff *skb2;
2077 struct net_device *dev;
2086 read_lock(&mrt_lock);
2088 vif = ipmr_find_vif(mrt, dev);
2090 read_unlock(&mrt_lock);
2094 skb2 = skb_clone(skb, GFP_ATOMIC);
2096 read_unlock(&mrt_lock);
2101 NETLINK_CB(skb2).pid = portid;
2102 skb_push(skb2, sizeof(struct iphdr));
2103 skb_reset_network_header(skb2);
2105 iph->ihl = sizeof(struct iphdr) >> 2;
2109 err = ipmr_cache_unresolved(mrt, vif, skb2);
2110 read_unlock(&mrt_lock);
2115 read_lock(&mrt_lock);
2116 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
2117 cache->mfc_flags |= MFC_NOTIFY;
2118 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2119 read_unlock(&mrt_lock);
2124 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2125 u32 pid, u32 seq, struct mfc_cache *c)
2127 struct nlmsghdr *nlh;
2130 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2134 rtm = nlmsg_data(nlh);
2135 rtm->rtm_family = RTNL_FAMILY_IPMR;
2136 rtm->rtm_dst_len = 32;
2137 rtm->rtm_src_len = 32;
2139 rtm->rtm_table = mrt->id;
2140 NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2141 rtm->rtm_type = RTN_MULTICAST;
2142 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2143 rtm->rtm_protocol = RTPROT_UNSPEC;
2146 NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2147 NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2149 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2150 goto nla_put_failure;
2152 return nlmsg_end(skb, nlh);
2155 nlmsg_cancel(skb, nlh);
2159 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2161 struct net *net = sock_net(skb->sk);
2162 struct mr_table *mrt;
2163 struct mfc_cache *mfc;
2164 unsigned int t = 0, s_t;
2165 unsigned int h = 0, s_h;
2166 unsigned int e = 0, s_e;
2173 ipmr_for_each_table(mrt, net) {
2178 for (h = s_h; h < MFC_LINES; h++) {
2179 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2182 if (ipmr_fill_mroute(mrt, skb,
2183 NETLINK_CB(cb->skb).pid,
2206 #ifdef CONFIG_PROC_FS
2208 * The /proc interfaces to multicast routing :
2209 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2211 struct ipmr_vif_iter {
2212 struct seq_net_private p;
2213 struct mr_table *mrt;
2217 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2218 struct ipmr_vif_iter *iter,
2221 struct mr_table *mrt = iter->mrt;
2223 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2224 if (!VIF_EXISTS(mrt, iter->ct))
2227 return &mrt->vif_table[iter->ct];
2232 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2233 __acquires(mrt_lock)
2235 struct ipmr_vif_iter *iter = seq->private;
2236 struct net *net = seq_file_net(seq);
2237 struct mr_table *mrt;
2239 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2241 return ERR_PTR(-ENOENT);
2245 read_lock(&mrt_lock);
2246 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2250 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2252 struct ipmr_vif_iter *iter = seq->private;
2253 struct net *net = seq_file_net(seq);
2254 struct mr_table *mrt = iter->mrt;
2257 if (v == SEQ_START_TOKEN)
2258 return ipmr_vif_seq_idx(net, iter, 0);
2260 while (++iter->ct < mrt->maxvif) {
2261 if (!VIF_EXISTS(mrt, iter->ct))
2263 return &mrt->vif_table[iter->ct];
2268 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2269 __releases(mrt_lock)
2271 read_unlock(&mrt_lock);
2274 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2276 struct ipmr_vif_iter *iter = seq->private;
2277 struct mr_table *mrt = iter->mrt;
2279 if (v == SEQ_START_TOKEN) {
2281 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2283 const struct vif_device *vif = v;
2284 const char *name = vif->dev ? vif->dev->name : "none";
2287 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2288 vif - mrt->vif_table,
2289 name, vif->bytes_in, vif->pkt_in,
2290 vif->bytes_out, vif->pkt_out,
2291 vif->flags, vif->local, vif->remote);
2296 static const struct seq_operations ipmr_vif_seq_ops = {
2297 .start = ipmr_vif_seq_start,
2298 .next = ipmr_vif_seq_next,
2299 .stop = ipmr_vif_seq_stop,
2300 .show = ipmr_vif_seq_show,
2303 static int ipmr_vif_open(struct inode *inode, struct file *file)
2305 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2306 sizeof(struct ipmr_vif_iter));
2309 static const struct file_operations ipmr_vif_fops = {
2310 .owner = THIS_MODULE,
2311 .open = ipmr_vif_open,
2313 .llseek = seq_lseek,
2314 .release = seq_release_net,
2317 struct ipmr_mfc_iter {
2318 struct seq_net_private p;
2319 struct mr_table *mrt;
2320 struct list_head *cache;
2325 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2326 struct ipmr_mfc_iter *it, loff_t pos)
2328 struct mr_table *mrt = it->mrt;
2329 struct mfc_cache *mfc;
2332 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2333 it->cache = &mrt->mfc_cache_array[it->ct];
2334 list_for_each_entry_rcu(mfc, it->cache, list)
2340 spin_lock_bh(&mfc_unres_lock);
2341 it->cache = &mrt->mfc_unres_queue;
2342 list_for_each_entry(mfc, it->cache, list)
2345 spin_unlock_bh(&mfc_unres_lock);
2352 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2354 struct ipmr_mfc_iter *it = seq->private;
2355 struct net *net = seq_file_net(seq);
2356 struct mr_table *mrt;
2358 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2360 return ERR_PTR(-ENOENT);
2365 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2369 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2371 struct mfc_cache *mfc = v;
2372 struct ipmr_mfc_iter *it = seq->private;
2373 struct net *net = seq_file_net(seq);
2374 struct mr_table *mrt = it->mrt;
2378 if (v == SEQ_START_TOKEN)
2379 return ipmr_mfc_seq_idx(net, seq->private, 0);
2381 if (mfc->list.next != it->cache)
2382 return list_entry(mfc->list.next, struct mfc_cache, list);
2384 if (it->cache == &mrt->mfc_unres_queue)
2387 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2389 while (++it->ct < MFC_LINES) {
2390 it->cache = &mrt->mfc_cache_array[it->ct];
2391 if (list_empty(it->cache))
2393 return list_first_entry(it->cache, struct mfc_cache, list);
2396 /* exhausted cache_array, show unresolved */
2398 it->cache = &mrt->mfc_unres_queue;
2401 spin_lock_bh(&mfc_unres_lock);
2402 if (!list_empty(it->cache))
2403 return list_first_entry(it->cache, struct mfc_cache, list);
2406 spin_unlock_bh(&mfc_unres_lock);
2412 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2414 struct ipmr_mfc_iter *it = seq->private;
2415 struct mr_table *mrt = it->mrt;
2417 if (it->cache == &mrt->mfc_unres_queue)
2418 spin_unlock_bh(&mfc_unres_lock);
2419 else if (it->cache == &mrt->mfc_cache_array[it->ct])
2423 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2427 if (v == SEQ_START_TOKEN) {
2429 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2431 const struct mfc_cache *mfc = v;
2432 const struct ipmr_mfc_iter *it = seq->private;
2433 const struct mr_table *mrt = it->mrt;
2435 seq_printf(seq, "%08X %08X %-3hd",
2436 (__force u32) mfc->mfc_mcastgrp,
2437 (__force u32) mfc->mfc_origin,
2440 if (it->cache != &mrt->mfc_unres_queue) {
2441 seq_printf(seq, " %8lu %8lu %8lu",
2442 mfc->mfc_un.res.pkt,
2443 mfc->mfc_un.res.bytes,
2444 mfc->mfc_un.res.wrong_if);
2445 for (n = mfc->mfc_un.res.minvif;
2446 n < mfc->mfc_un.res.maxvif; n++) {
2447 if (VIF_EXISTS(mrt, n) &&
2448 mfc->mfc_un.res.ttls[n] < 255)
2451 n, mfc->mfc_un.res.ttls[n]);
2454 /* unresolved mfc_caches don't contain
2455 * pkt, bytes and wrong_if values
2457 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2459 seq_putc(seq, '\n');
2464 static const struct seq_operations ipmr_mfc_seq_ops = {
2465 .start = ipmr_mfc_seq_start,
2466 .next = ipmr_mfc_seq_next,
2467 .stop = ipmr_mfc_seq_stop,
2468 .show = ipmr_mfc_seq_show,
2471 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2473 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2474 sizeof(struct ipmr_mfc_iter));
2477 static const struct file_operations ipmr_mfc_fops = {
2478 .owner = THIS_MODULE,
2479 .open = ipmr_mfc_open,
2481 .llseek = seq_lseek,
2482 .release = seq_release_net,
2486 #ifdef CONFIG_IP_PIMSM_V2
2487 static const struct net_protocol pim_protocol = {
2495 * Setup for IP multicast routing
2497 static int __net_init ipmr_net_init(struct net *net)
2501 err = ipmr_rules_init(net);
2505 #ifdef CONFIG_PROC_FS
2507 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2509 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2510 goto proc_cache_fail;
2514 #ifdef CONFIG_PROC_FS
2516 proc_net_remove(net, "ip_mr_vif");
2518 ipmr_rules_exit(net);
2524 static void __net_exit ipmr_net_exit(struct net *net)
2526 #ifdef CONFIG_PROC_FS
2527 proc_net_remove(net, "ip_mr_cache");
2528 proc_net_remove(net, "ip_mr_vif");
2530 ipmr_rules_exit(net);
2533 static struct pernet_operations ipmr_net_ops = {
2534 .init = ipmr_net_init,
2535 .exit = ipmr_net_exit,
2538 int __init ip_mr_init(void)
2542 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2543 sizeof(struct mfc_cache),
2544 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2549 err = register_pernet_subsys(&ipmr_net_ops);
2551 goto reg_pernet_fail;
2553 err = register_netdevice_notifier(&ip_mr_notifier);
2555 goto reg_notif_fail;
2556 #ifdef CONFIG_IP_PIMSM_V2
2557 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2558 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2560 goto add_proto_fail;
2563 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2564 NULL, ipmr_rtm_dumproute, NULL);
2567 #ifdef CONFIG_IP_PIMSM_V2
2569 unregister_netdevice_notifier(&ip_mr_notifier);
2572 unregister_pernet_subsys(&ipmr_net_ops);
2574 kmem_cache_destroy(mrt_cachep);