2 * IP multicast routing support for mrouted 3.6/3.8
5 * Linux Consultancy and Custom Driver Development
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
25 * Relax this requirement to work with older peers.
29 #include <asm/system.h>
30 #include <asm/uaccess.h>
31 #include <linux/types.h>
32 #include <linux/capability.h>
33 #include <linux/errno.h>
34 #include <linux/timer.h>
36 #include <linux/kernel.h>
37 #include <linux/fcntl.h>
38 #include <linux/stat.h>
39 #include <linux/socket.h>
41 #include <linux/inet.h>
42 #include <linux/netdevice.h>
43 #include <linux/inetdevice.h>
44 #include <linux/igmp.h>
45 #include <linux/proc_fs.h>
46 #include <linux/seq_file.h>
47 #include <linux/mroute.h>
48 #include <linux/init.h>
49 #include <linux/if_ether.h>
50 #include <linux/slab.h>
51 #include <net/net_namespace.h>
53 #include <net/protocol.h>
54 #include <linux/skbuff.h>
55 #include <net/route.h>
60 #include <linux/notifier.h>
61 #include <linux/if_arp.h>
62 #include <linux/netfilter_ipv4.h>
63 #include <linux/compat.h>
65 #include <net/checksum.h>
66 #include <net/netlink.h>
67 #include <net/fib_rules.h>
69 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
70 #define CONFIG_IP_PIMSM 1
74 struct list_head list;
79 struct sock __rcu *mroute_sk;
80 struct timer_list ipmr_expire_timer;
81 struct list_head mfc_unres_queue;
82 struct list_head mfc_cache_array[MFC_LINES];
83 struct vif_device vif_table[MAXVIFS];
85 atomic_t cache_resolve_queue_len;
88 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
89 int mroute_reg_vif_num;
94 struct fib_rule common;
101 /* Big lock, protecting vif table, mrt cache and mroute socket state.
102 * Note that the changes are semaphored via rtnl_lock.
105 static DEFINE_RWLOCK(mrt_lock);
108 * Multicast router control variables
111 #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
113 /* Special spinlock for queue of unresolved entries */
114 static DEFINE_SPINLOCK(mfc_unres_lock);
116 /* We return to original Alan's scheme. Hash table of resolved
117 * entries is changed only in process context and protected
118 * with weak lock mrt_lock. Queue of unresolved entries is protected
119 * with strong spinlock mfc_unres_lock.
121 * In this case data path is free of exclusive locks at all.
124 static struct kmem_cache *mrt_cachep __read_mostly;
126 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
127 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
128 struct sk_buff *skb, struct mfc_cache *cache,
130 static int ipmr_cache_report(struct mr_table *mrt,
131 struct sk_buff *pkt, vifi_t vifi, int assert);
132 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
133 struct mfc_cache *c, struct rtmsg *rtm);
134 static void ipmr_expire_process(unsigned long arg);
136 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
137 #define ipmr_for_each_table(mrt, net) \
138 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
140 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
142 struct mr_table *mrt;
144 ipmr_for_each_table(mrt, net) {
151 static int ipmr_fib_lookup(struct net *net, struct flowi *flp,
152 struct mr_table **mrt)
154 struct ipmr_result res;
155 struct fib_lookup_arg arg = { .result = &res, };
158 err = fib_rules_lookup(net->ipv4.mr_rules_ops, flp, 0, &arg);
165 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
166 int flags, struct fib_lookup_arg *arg)
168 struct ipmr_result *res = arg->result;
169 struct mr_table *mrt;
171 switch (rule->action) {
174 case FR_ACT_UNREACHABLE:
176 case FR_ACT_PROHIBIT:
178 case FR_ACT_BLACKHOLE:
183 mrt = ipmr_get_table(rule->fr_net, rule->table);
190 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
195 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
199 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
200 struct fib_rule_hdr *frh, struct nlattr **tb)
205 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
211 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
212 struct fib_rule_hdr *frh)
220 static const struct fib_rules_ops __net_initdata ipmr_rules_ops_template = {
221 .family = RTNL_FAMILY_IPMR,
222 .rule_size = sizeof(struct ipmr_rule),
223 .addr_size = sizeof(u32),
224 .action = ipmr_rule_action,
225 .match = ipmr_rule_match,
226 .configure = ipmr_rule_configure,
227 .compare = ipmr_rule_compare,
228 .default_pref = fib_default_rule_pref,
229 .fill = ipmr_rule_fill,
230 .nlgroup = RTNLGRP_IPV4_RULE,
231 .policy = ipmr_rule_policy,
232 .owner = THIS_MODULE,
235 static int __net_init ipmr_rules_init(struct net *net)
237 struct fib_rules_ops *ops;
238 struct mr_table *mrt;
241 ops = fib_rules_register(&ipmr_rules_ops_template, net);
245 INIT_LIST_HEAD(&net->ipv4.mr_tables);
247 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
253 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
257 net->ipv4.mr_rules_ops = ops;
263 fib_rules_unregister(ops);
267 static void __net_exit ipmr_rules_exit(struct net *net)
269 struct mr_table *mrt, *next;
271 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
272 list_del(&mrt->list);
275 fib_rules_unregister(net->ipv4.mr_rules_ops);
278 #define ipmr_for_each_table(mrt, net) \
279 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
281 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
283 return net->ipv4.mrt;
286 static int ipmr_fib_lookup(struct net *net, struct flowi *flp,
287 struct mr_table **mrt)
289 *mrt = net->ipv4.mrt;
293 static int __net_init ipmr_rules_init(struct net *net)
295 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
296 return net->ipv4.mrt ? 0 : -ENOMEM;
299 static void __net_exit ipmr_rules_exit(struct net *net)
301 kfree(net->ipv4.mrt);
305 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
307 struct mr_table *mrt;
310 mrt = ipmr_get_table(net, id);
314 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
317 write_pnet(&mrt->net, net);
320 /* Forwarding cache */
321 for (i = 0; i < MFC_LINES; i++)
322 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
324 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
326 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
329 #ifdef CONFIG_IP_PIMSM
330 mrt->mroute_reg_vif_num = -1;
332 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
333 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
338 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
340 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
342 struct net *net = dev_net(dev);
346 dev = __dev_get_by_name(net, "tunl0");
348 const struct net_device_ops *ops = dev->netdev_ops;
350 struct ip_tunnel_parm p;
352 memset(&p, 0, sizeof(p));
353 p.iph.daddr = v->vifc_rmt_addr.s_addr;
354 p.iph.saddr = v->vifc_lcl_addr.s_addr;
357 p.iph.protocol = IPPROTO_IPIP;
358 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
359 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
361 if (ops->ndo_do_ioctl) {
362 mm_segment_t oldfs = get_fs();
365 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
372 struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
374 struct net_device *dev;
376 dev = __dev_get_by_name(net, "tunl0");
379 const struct net_device_ops *ops = dev->netdev_ops;
382 struct ip_tunnel_parm p;
383 struct in_device *in_dev;
385 memset(&p, 0, sizeof(p));
386 p.iph.daddr = v->vifc_rmt_addr.s_addr;
387 p.iph.saddr = v->vifc_lcl_addr.s_addr;
390 p.iph.protocol = IPPROTO_IPIP;
391 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
392 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
394 if (ops->ndo_do_ioctl) {
395 mm_segment_t oldfs = get_fs();
398 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
406 (dev = __dev_get_by_name(net, p.name)) != NULL) {
407 dev->flags |= IFF_MULTICAST;
409 in_dev = __in_dev_get_rtnl(dev);
413 ipv4_devconf_setall(in_dev);
414 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
424 /* allow the register to be completed before unregistering. */
428 unregister_netdevice(dev);
432 #ifdef CONFIG_IP_PIMSM
434 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
436 struct net *net = dev_net(dev);
437 struct mr_table *mrt;
445 err = ipmr_fib_lookup(net, &fl, &mrt);
451 read_lock(&mrt_lock);
452 dev->stats.tx_bytes += skb->len;
453 dev->stats.tx_packets++;
454 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
455 read_unlock(&mrt_lock);
460 static const struct net_device_ops reg_vif_netdev_ops = {
461 .ndo_start_xmit = reg_vif_xmit,
464 static void reg_vif_setup(struct net_device *dev)
466 dev->type = ARPHRD_PIMREG;
467 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
468 dev->flags = IFF_NOARP;
469 dev->netdev_ops = ®_vif_netdev_ops,
470 dev->destructor = free_netdev;
471 dev->features |= NETIF_F_NETNS_LOCAL;
474 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
476 struct net_device *dev;
477 struct in_device *in_dev;
480 if (mrt->id == RT_TABLE_DEFAULT)
481 sprintf(name, "pimreg");
483 sprintf(name, "pimreg%u", mrt->id);
485 dev = alloc_netdev(0, name, reg_vif_setup);
490 dev_net_set(dev, net);
492 if (register_netdevice(dev)) {
499 in_dev = __in_dev_get_rcu(dev);
505 ipv4_devconf_setall(in_dev);
506 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
517 /* allow the register to be completed before unregistering. */
521 unregister_netdevice(dev);
528 * @notify: Set to 1, if the caller is a notifier_call
531 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
532 struct list_head *head)
534 struct vif_device *v;
535 struct net_device *dev;
536 struct in_device *in_dev;
538 if (vifi < 0 || vifi >= mrt->maxvif)
539 return -EADDRNOTAVAIL;
541 v = &mrt->vif_table[vifi];
543 write_lock_bh(&mrt_lock);
548 write_unlock_bh(&mrt_lock);
549 return -EADDRNOTAVAIL;
552 #ifdef CONFIG_IP_PIMSM
553 if (vifi == mrt->mroute_reg_vif_num)
554 mrt->mroute_reg_vif_num = -1;
557 if (vifi + 1 == mrt->maxvif) {
560 for (tmp = vifi - 1; tmp >= 0; tmp--) {
561 if (VIF_EXISTS(mrt, tmp))
567 write_unlock_bh(&mrt_lock);
569 dev_set_allmulti(dev, -1);
571 in_dev = __in_dev_get_rtnl(dev);
573 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
574 ip_rt_multicast_event(in_dev);
577 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
578 unregister_netdevice_queue(dev, head);
584 static void ipmr_cache_free_rcu(struct rcu_head *head)
586 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
588 kmem_cache_free(mrt_cachep, c);
591 static inline void ipmr_cache_free(struct mfc_cache *c)
593 call_rcu(&c->rcu, ipmr_cache_free_rcu);
596 /* Destroy an unresolved cache entry, killing queued skbs
597 * and reporting error to netlink readers.
600 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
602 struct net *net = read_pnet(&mrt->net);
606 atomic_dec(&mrt->cache_resolve_queue_len);
608 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
609 if (ip_hdr(skb)->version == 0) {
610 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
611 nlh->nlmsg_type = NLMSG_ERROR;
612 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
613 skb_trim(skb, nlh->nlmsg_len);
615 e->error = -ETIMEDOUT;
616 memset(&e->msg, 0, sizeof(e->msg));
618 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
628 /* Timer process for the unresolved queue. */
630 static void ipmr_expire_process(unsigned long arg)
632 struct mr_table *mrt = (struct mr_table *)arg;
634 unsigned long expires;
635 struct mfc_cache *c, *next;
637 if (!spin_trylock(&mfc_unres_lock)) {
638 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
642 if (list_empty(&mrt->mfc_unres_queue))
648 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
649 if (time_after(c->mfc_un.unres.expires, now)) {
650 unsigned long interval = c->mfc_un.unres.expires - now;
651 if (interval < expires)
657 ipmr_destroy_unres(mrt, c);
660 if (!list_empty(&mrt->mfc_unres_queue))
661 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
664 spin_unlock(&mfc_unres_lock);
667 /* Fill oifs list. It is called under write locked mrt_lock. */
669 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
674 cache->mfc_un.res.minvif = MAXVIFS;
675 cache->mfc_un.res.maxvif = 0;
676 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
678 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
679 if (VIF_EXISTS(mrt, vifi) &&
680 ttls[vifi] && ttls[vifi] < 255) {
681 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
682 if (cache->mfc_un.res.minvif > vifi)
683 cache->mfc_un.res.minvif = vifi;
684 if (cache->mfc_un.res.maxvif <= vifi)
685 cache->mfc_un.res.maxvif = vifi + 1;
690 static int vif_add(struct net *net, struct mr_table *mrt,
691 struct vifctl *vifc, int mrtsock)
693 int vifi = vifc->vifc_vifi;
694 struct vif_device *v = &mrt->vif_table[vifi];
695 struct net_device *dev;
696 struct in_device *in_dev;
700 if (VIF_EXISTS(mrt, vifi))
703 switch (vifc->vifc_flags) {
704 #ifdef CONFIG_IP_PIMSM
707 * Special Purpose VIF in PIM
708 * All the packets will be sent to the daemon
710 if (mrt->mroute_reg_vif_num >= 0)
712 dev = ipmr_reg_vif(net, mrt);
715 err = dev_set_allmulti(dev, 1);
717 unregister_netdevice(dev);
724 dev = ipmr_new_tunnel(net, vifc);
727 err = dev_set_allmulti(dev, 1);
729 ipmr_del_tunnel(dev, vifc);
735 case VIFF_USE_IFINDEX:
737 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
738 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
739 if (dev && __in_dev_get_rtnl(dev) == NULL) {
741 return -EADDRNOTAVAIL;
744 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
747 return -EADDRNOTAVAIL;
748 err = dev_set_allmulti(dev, 1);
758 in_dev = __in_dev_get_rtnl(dev);
761 return -EADDRNOTAVAIL;
763 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
764 ip_rt_multicast_event(in_dev);
766 /* Fill in the VIF structures */
768 v->rate_limit = vifc->vifc_rate_limit;
769 v->local = vifc->vifc_lcl_addr.s_addr;
770 v->remote = vifc->vifc_rmt_addr.s_addr;
771 v->flags = vifc->vifc_flags;
773 v->flags |= VIFF_STATIC;
774 v->threshold = vifc->vifc_threshold;
779 v->link = dev->ifindex;
780 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
781 v->link = dev->iflink;
783 /* And finish update writing critical data */
784 write_lock_bh(&mrt_lock);
786 #ifdef CONFIG_IP_PIMSM
787 if (v->flags & VIFF_REGISTER)
788 mrt->mroute_reg_vif_num = vifi;
790 if (vifi+1 > mrt->maxvif)
791 mrt->maxvif = vifi+1;
792 write_unlock_bh(&mrt_lock);
796 /* called with rcu_read_lock() */
797 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
801 int line = MFC_HASH(mcastgrp, origin);
804 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
805 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
812 * Allocate a multicast cache entry
814 static struct mfc_cache *ipmr_cache_alloc(void)
816 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
819 c->mfc_un.res.minvif = MAXVIFS;
823 static struct mfc_cache *ipmr_cache_alloc_unres(void)
825 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
828 skb_queue_head_init(&c->mfc_un.unres.unresolved);
829 c->mfc_un.unres.expires = jiffies + 10*HZ;
835 * A cache entry has gone into a resolved state from queued
838 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
839 struct mfc_cache *uc, struct mfc_cache *c)
844 /* Play the pending entries through our router */
846 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
847 if (ip_hdr(skb)->version == 0) {
848 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
850 if (__ipmr_fill_mroute(mrt, skb, c, NLMSG_DATA(nlh)) > 0) {
851 nlh->nlmsg_len = skb_tail_pointer(skb) -
854 nlh->nlmsg_type = NLMSG_ERROR;
855 nlh->nlmsg_len = NLMSG_LENGTH(sizeof(struct nlmsgerr));
856 skb_trim(skb, nlh->nlmsg_len);
858 e->error = -EMSGSIZE;
859 memset(&e->msg, 0, sizeof(e->msg));
862 rtnl_unicast(skb, net, NETLINK_CB(skb).pid);
864 ip_mr_forward(net, mrt, skb, c, 0);
870 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
871 * expects the following bizarre scheme.
873 * Called under mrt_lock.
876 static int ipmr_cache_report(struct mr_table *mrt,
877 struct sk_buff *pkt, vifi_t vifi, int assert)
880 const int ihl = ip_hdrlen(pkt);
881 struct igmphdr *igmp;
883 struct sock *mroute_sk;
886 #ifdef CONFIG_IP_PIMSM
887 if (assert == IGMPMSG_WHOLEPKT)
888 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
891 skb = alloc_skb(128, GFP_ATOMIC);
896 #ifdef CONFIG_IP_PIMSM
897 if (assert == IGMPMSG_WHOLEPKT) {
898 /* Ugly, but we have no choice with this interface.
899 * Duplicate old header, fix ihl, length etc.
900 * And all this only to mangle msg->im_msgtype and
901 * to set msg->im_mbz to "mbz" :-)
903 skb_push(skb, sizeof(struct iphdr));
904 skb_reset_network_header(skb);
905 skb_reset_transport_header(skb);
906 msg = (struct igmpmsg *)skb_network_header(skb);
907 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
908 msg->im_msgtype = IGMPMSG_WHOLEPKT;
910 msg->im_vif = mrt->mroute_reg_vif_num;
911 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
912 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
913 sizeof(struct iphdr));
918 /* Copy the IP header */
920 skb->network_header = skb->tail;
922 skb_copy_to_linear_data(skb, pkt->data, ihl);
923 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
924 msg = (struct igmpmsg *)skb_network_header(skb);
926 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
930 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
932 msg->im_msgtype = assert;
934 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
935 skb->transport_header = skb->network_header;
939 mroute_sk = rcu_dereference(mrt->mroute_sk);
940 if (mroute_sk == NULL) {
946 /* Deliver to mrouted */
948 ret = sock_queue_rcv_skb(mroute_sk, skb);
952 printk(KERN_WARNING "mroute: pending queue full, dropping entries.\n");
960 * Queue a packet for resolution. It gets locked cache entry!
964 ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
969 const struct iphdr *iph = ip_hdr(skb);
971 spin_lock_bh(&mfc_unres_lock);
972 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
973 if (c->mfc_mcastgrp == iph->daddr &&
974 c->mfc_origin == iph->saddr) {
981 /* Create a new entry if allowable */
983 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
984 (c = ipmr_cache_alloc_unres()) == NULL) {
985 spin_unlock_bh(&mfc_unres_lock);
991 /* Fill in the new cache entry */
994 c->mfc_origin = iph->saddr;
995 c->mfc_mcastgrp = iph->daddr;
997 /* Reflect first query at mrouted. */
999 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1001 /* If the report failed throw the cache entry
1004 spin_unlock_bh(&mfc_unres_lock);
1011 atomic_inc(&mrt->cache_resolve_queue_len);
1012 list_add(&c->list, &mrt->mfc_unres_queue);
1014 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1015 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1018 /* See if we can append the packet */
1020 if (c->mfc_un.unres.unresolved.qlen > 3) {
1024 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1028 spin_unlock_bh(&mfc_unres_lock);
1033 * MFC cache manipulation by user space mroute daemon
1036 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc)
1039 struct mfc_cache *c, *next;
1041 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1043 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1044 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1045 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1046 list_del_rcu(&c->list);
1055 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1056 struct mfcctl *mfc, int mrtsock)
1060 struct mfc_cache *uc, *c;
1062 if (mfc->mfcc_parent >= MAXVIFS)
1065 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1067 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1068 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1069 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr) {
1076 write_lock_bh(&mrt_lock);
1077 c->mfc_parent = mfc->mfcc_parent;
1078 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1080 c->mfc_flags |= MFC_STATIC;
1081 write_unlock_bh(&mrt_lock);
1085 if (!ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1088 c = ipmr_cache_alloc();
1092 c->mfc_origin = mfc->mfcc_origin.s_addr;
1093 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1094 c->mfc_parent = mfc->mfcc_parent;
1095 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1097 c->mfc_flags |= MFC_STATIC;
1099 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1102 * Check to see if we resolved a queued list. If so we
1103 * need to send on the frames and tidy up.
1106 spin_lock_bh(&mfc_unres_lock);
1107 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1108 if (uc->mfc_origin == c->mfc_origin &&
1109 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1110 list_del(&uc->list);
1111 atomic_dec(&mrt->cache_resolve_queue_len);
1116 if (list_empty(&mrt->mfc_unres_queue))
1117 del_timer(&mrt->ipmr_expire_timer);
1118 spin_unlock_bh(&mfc_unres_lock);
1121 ipmr_cache_resolve(net, mrt, uc, c);
1122 ipmr_cache_free(uc);
1128 * Close the multicast socket, and clear the vif tables etc
1131 static void mroute_clean_tables(struct mr_table *mrt)
1135 struct mfc_cache *c, *next;
1137 /* Shut down all active vif entries */
1139 for (i = 0; i < mrt->maxvif; i++) {
1140 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
1141 vif_delete(mrt, i, 0, &list);
1143 unregister_netdevice_many(&list);
1145 /* Wipe the cache */
1147 for (i = 0; i < MFC_LINES; i++) {
1148 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1149 if (c->mfc_flags & MFC_STATIC)
1151 list_del_rcu(&c->list);
1156 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1157 spin_lock_bh(&mfc_unres_lock);
1158 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1160 ipmr_destroy_unres(mrt, c);
1162 spin_unlock_bh(&mfc_unres_lock);
1166 /* called from ip_ra_control(), before an RCU grace period,
1167 * we dont need to call synchronize_rcu() here
1169 static void mrtsock_destruct(struct sock *sk)
1171 struct net *net = sock_net(sk);
1172 struct mr_table *mrt;
1175 ipmr_for_each_table(mrt, net) {
1176 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1177 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1178 rcu_assign_pointer(mrt->mroute_sk, NULL);
1179 mroute_clean_tables(mrt);
1186 * Socket options and virtual interface manipulation. The whole
1187 * virtual interface system is a complete heap, but unfortunately
1188 * that's how BSD mrouted happens to think. Maybe one day with a proper
1189 * MOSPF/PIM router set up we can clean this up.
1192 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1197 struct net *net = sock_net(sk);
1198 struct mr_table *mrt;
1200 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1204 if (optname != MRT_INIT) {
1205 if (sk != rcu_dereference_raw(mrt->mroute_sk) &&
1206 !capable(CAP_NET_ADMIN))
1212 if (sk->sk_type != SOCK_RAW ||
1213 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1215 if (optlen != sizeof(int))
1216 return -ENOPROTOOPT;
1219 if (rtnl_dereference(mrt->mroute_sk)) {
1224 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1226 rcu_assign_pointer(mrt->mroute_sk, sk);
1227 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1232 if (sk != rcu_dereference_raw(mrt->mroute_sk))
1234 return ip_ra_control(sk, 0, NULL);
1237 if (optlen != sizeof(vif))
1239 if (copy_from_user(&vif, optval, sizeof(vif)))
1241 if (vif.vifc_vifi >= MAXVIFS)
1244 if (optname == MRT_ADD_VIF) {
1245 ret = vif_add(net, mrt, &vif,
1246 sk == rtnl_dereference(mrt->mroute_sk));
1248 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1254 * Manipulate the forwarding caches. These live
1255 * in a sort of kernel/user symbiosis.
1259 if (optlen != sizeof(mfc))
1261 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1264 if (optname == MRT_DEL_MFC)
1265 ret = ipmr_mfc_delete(mrt, &mfc);
1267 ret = ipmr_mfc_add(net, mrt, &mfc,
1268 sk == rtnl_dereference(mrt->mroute_sk));
1272 * Control PIM assert.
1277 if (get_user(v, (int __user *)optval))
1279 mrt->mroute_do_assert = (v) ? 1 : 0;
1282 #ifdef CONFIG_IP_PIMSM
1287 if (get_user(v, (int __user *)optval))
1293 if (v != mrt->mroute_do_pim) {
1294 mrt->mroute_do_pim = v;
1295 mrt->mroute_do_assert = v;
1301 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1306 if (optlen != sizeof(u32))
1308 if (get_user(v, (u32 __user *)optval))
1313 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1316 if (!ipmr_new_table(net, v))
1318 raw_sk(sk)->ipmr_table = v;
1325 * Spurious command, or MRT_VERSION which you cannot
1329 return -ENOPROTOOPT;
1334 * Getsock opt support for the multicast routing system.
1337 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1341 struct net *net = sock_net(sk);
1342 struct mr_table *mrt;
1344 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1348 if (optname != MRT_VERSION &&
1349 #ifdef CONFIG_IP_PIMSM
1350 optname != MRT_PIM &&
1352 optname != MRT_ASSERT)
1353 return -ENOPROTOOPT;
1355 if (get_user(olr, optlen))
1358 olr = min_t(unsigned int, olr, sizeof(int));
1362 if (put_user(olr, optlen))
1364 if (optname == MRT_VERSION)
1366 #ifdef CONFIG_IP_PIMSM
1367 else if (optname == MRT_PIM)
1368 val = mrt->mroute_do_pim;
1371 val = mrt->mroute_do_assert;
1372 if (copy_to_user(optval, &val, olr))
1378 * The IP multicast ioctl support routines.
1381 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1383 struct sioc_sg_req sr;
1384 struct sioc_vif_req vr;
1385 struct vif_device *vif;
1386 struct mfc_cache *c;
1387 struct net *net = sock_net(sk);
1388 struct mr_table *mrt;
1390 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1396 if (copy_from_user(&vr, arg, sizeof(vr)))
1398 if (vr.vifi >= mrt->maxvif)
1400 read_lock(&mrt_lock);
1401 vif = &mrt->vif_table[vr.vifi];
1402 if (VIF_EXISTS(mrt, vr.vifi)) {
1403 vr.icount = vif->pkt_in;
1404 vr.ocount = vif->pkt_out;
1405 vr.ibytes = vif->bytes_in;
1406 vr.obytes = vif->bytes_out;
1407 read_unlock(&mrt_lock);
1409 if (copy_to_user(arg, &vr, sizeof(vr)))
1413 read_unlock(&mrt_lock);
1414 return -EADDRNOTAVAIL;
1416 if (copy_from_user(&sr, arg, sizeof(sr)))
1420 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1422 sr.pktcnt = c->mfc_un.res.pkt;
1423 sr.bytecnt = c->mfc_un.res.bytes;
1424 sr.wrong_if = c->mfc_un.res.wrong_if;
1427 if (copy_to_user(arg, &sr, sizeof(sr)))
1432 return -EADDRNOTAVAIL;
1434 return -ENOIOCTLCMD;
1438 #ifdef CONFIG_COMPAT
1439 struct compat_sioc_sg_req {
1442 compat_ulong_t pktcnt;
1443 compat_ulong_t bytecnt;
1444 compat_ulong_t wrong_if;
1447 struct compat_sioc_vif_req {
1448 vifi_t vifi; /* Which iface */
1449 compat_ulong_t icount;
1450 compat_ulong_t ocount;
1451 compat_ulong_t ibytes;
1452 compat_ulong_t obytes;
1455 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1457 struct compat_sioc_sg_req sr;
1458 struct compat_sioc_vif_req vr;
1459 struct vif_device *vif;
1460 struct mfc_cache *c;
1461 struct net *net = sock_net(sk);
1462 struct mr_table *mrt;
1464 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1470 if (copy_from_user(&vr, arg, sizeof(vr)))
1472 if (vr.vifi >= mrt->maxvif)
1474 read_lock(&mrt_lock);
1475 vif = &mrt->vif_table[vr.vifi];
1476 if (VIF_EXISTS(mrt, vr.vifi)) {
1477 vr.icount = vif->pkt_in;
1478 vr.ocount = vif->pkt_out;
1479 vr.ibytes = vif->bytes_in;
1480 vr.obytes = vif->bytes_out;
1481 read_unlock(&mrt_lock);
1483 if (copy_to_user(arg, &vr, sizeof(vr)))
1487 read_unlock(&mrt_lock);
1488 return -EADDRNOTAVAIL;
1490 if (copy_from_user(&sr, arg, sizeof(sr)))
1494 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1496 sr.pktcnt = c->mfc_un.res.pkt;
1497 sr.bytecnt = c->mfc_un.res.bytes;
1498 sr.wrong_if = c->mfc_un.res.wrong_if;
1501 if (copy_to_user(arg, &sr, sizeof(sr)))
1506 return -EADDRNOTAVAIL;
1508 return -ENOIOCTLCMD;
1514 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1516 struct net_device *dev = ptr;
1517 struct net *net = dev_net(dev);
1518 struct mr_table *mrt;
1519 struct vif_device *v;
1523 if (event != NETDEV_UNREGISTER)
1526 ipmr_for_each_table(mrt, net) {
1527 v = &mrt->vif_table[0];
1528 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1530 vif_delete(mrt, ct, 1, &list);
1533 unregister_netdevice_many(&list);
1538 static struct notifier_block ip_mr_notifier = {
1539 .notifier_call = ipmr_device_event,
1543 * Encapsulate a packet by attaching a valid IPIP header to it.
1544 * This avoids tunnel drivers and other mess and gives us the speed so
1545 * important for multicast video.
1548 static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1551 struct iphdr *old_iph = ip_hdr(skb);
1553 skb_push(skb, sizeof(struct iphdr));
1554 skb->transport_header = skb->network_header;
1555 skb_reset_network_header(skb);
1559 iph->tos = old_iph->tos;
1560 iph->ttl = old_iph->ttl;
1564 iph->protocol = IPPROTO_IPIP;
1566 iph->tot_len = htons(skb->len);
1567 ip_select_ident(iph, skb_dst(skb), NULL);
1570 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1574 static inline int ipmr_forward_finish(struct sk_buff *skb)
1576 struct ip_options *opt = &(IPCB(skb)->opt);
1578 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
1580 if (unlikely(opt->optlen))
1581 ip_forward_options(skb);
1583 return dst_output(skb);
1587 * Processing handlers for ipmr_forward
1590 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1591 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1593 const struct iphdr *iph = ip_hdr(skb);
1594 struct vif_device *vif = &mrt->vif_table[vifi];
1595 struct net_device *dev;
1599 if (vif->dev == NULL)
1602 #ifdef CONFIG_IP_PIMSM
1603 if (vif->flags & VIFF_REGISTER) {
1605 vif->bytes_out += skb->len;
1606 vif->dev->stats.tx_bytes += skb->len;
1607 vif->dev->stats.tx_packets++;
1608 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1613 if (vif->flags & VIFF_TUNNEL) {
1616 .fl4_dst = vif->remote,
1617 .fl4_src = vif->local,
1618 .fl4_tos = RT_TOS(iph->tos),
1619 .proto = IPPROTO_IPIP
1622 if (ip_route_output_key(net, &rt, &fl))
1624 encap = sizeof(struct iphdr);
1628 .fl4_dst = iph->daddr,
1629 .fl4_tos = RT_TOS(iph->tos),
1630 .proto = IPPROTO_IPIP
1633 if (ip_route_output_key(net, &rt, &fl))
1639 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1640 /* Do not fragment multicasts. Alas, IPv4 does not
1641 * allow to send ICMP, so that packets will disappear
1645 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1650 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1652 if (skb_cow(skb, encap)) {
1658 vif->bytes_out += skb->len;
1661 skb_dst_set(skb, &rt->dst);
1662 ip_decrease_ttl(ip_hdr(skb));
1664 /* FIXME: forward and output firewalls used to be called here.
1665 * What do we do with netfilter? -- RR
1667 if (vif->flags & VIFF_TUNNEL) {
1668 ip_encap(skb, vif->local, vif->remote);
1669 /* FIXME: extra output firewall step used to be here. --RR */
1670 vif->dev->stats.tx_packets++;
1671 vif->dev->stats.tx_bytes += skb->len;
1674 IPCB(skb)->flags |= IPSKB_FORWARDED;
1677 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1678 * not only before forwarding, but after forwarding on all output
1679 * interfaces. It is clear, if mrouter runs a multicasting
1680 * program, it should receive packets not depending to what interface
1681 * program is joined.
1682 * If we will not make it, the program will have to join on all
1683 * interfaces. On the other hand, multihoming host (or router, but
1684 * not mrouter) cannot join to more than one interface - it will
1685 * result in receiving multiple packets.
1687 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
1688 ipmr_forward_finish);
1695 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1699 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1700 if (mrt->vif_table[ct].dev == dev)
1706 /* "local" means that we should preserve one skb (for local delivery) */
1708 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1709 struct sk_buff *skb, struct mfc_cache *cache,
1715 vif = cache->mfc_parent;
1716 cache->mfc_un.res.pkt++;
1717 cache->mfc_un.res.bytes += skb->len;
1720 * Wrong interface: drop packet and (maybe) send PIM assert.
1722 if (mrt->vif_table[vif].dev != skb->dev) {
1725 if (rt_is_output_route(skb_rtable(skb))) {
1726 /* It is our own packet, looped back.
1727 * Very complicated situation...
1729 * The best workaround until routing daemons will be
1730 * fixed is not to redistribute packet, if it was
1731 * send through wrong interface. It means, that
1732 * multicast applications WILL NOT work for
1733 * (S,G), which have default multicast route pointing
1734 * to wrong oif. In any case, it is not a good
1735 * idea to use multicasting applications on router.
1740 cache->mfc_un.res.wrong_if++;
1741 true_vifi = ipmr_find_vif(mrt, skb->dev);
1743 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1744 /* pimsm uses asserts, when switching from RPT to SPT,
1745 * so that we cannot check that packet arrived on an oif.
1746 * It is bad, but otherwise we would need to move pretty
1747 * large chunk of pimd to kernel. Ough... --ANK
1749 (mrt->mroute_do_pim ||
1750 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1752 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1753 cache->mfc_un.res.last_assert = jiffies;
1754 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1759 mrt->vif_table[vif].pkt_in++;
1760 mrt->vif_table[vif].bytes_in += skb->len;
1765 for (ct = cache->mfc_un.res.maxvif - 1;
1766 ct >= cache->mfc_un.res.minvif; ct--) {
1767 if (ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1769 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1772 ipmr_queue_xmit(net, mrt, skb2, cache,
1780 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1783 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1785 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1798 * Multicast packets for forwarding arrive here
1799 * Called with rcu_read_lock();
1802 int ip_mr_input(struct sk_buff *skb)
1804 struct mfc_cache *cache;
1805 struct net *net = dev_net(skb->dev);
1806 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1807 struct mr_table *mrt;
1810 /* Packet is looped back after forward, it should not be
1811 * forwarded second time, but still can be delivered locally.
1813 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1816 err = ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt);
1823 if (IPCB(skb)->opt.router_alert) {
1824 if (ip_call_ra_chain(skb))
1826 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1827 /* IGMPv1 (and broken IGMPv2 implementations sort of
1828 * Cisco IOS <= 11.2(8)) do not put router alert
1829 * option to IGMP packets destined to routable
1830 * groups. It is very bad, because it means
1831 * that we can forward NO IGMP messages.
1833 struct sock *mroute_sk;
1835 mroute_sk = rcu_dereference(mrt->mroute_sk);
1838 raw_rcv(mroute_sk, skb);
1844 /* already under rcu_read_lock() */
1845 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1848 * No usable cache entry
1850 if (cache == NULL) {
1854 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1855 ip_local_deliver(skb);
1861 read_lock(&mrt_lock);
1862 vif = ipmr_find_vif(mrt, skb->dev);
1864 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
1865 read_unlock(&mrt_lock);
1869 read_unlock(&mrt_lock);
1874 read_lock(&mrt_lock);
1875 ip_mr_forward(net, mrt, skb, cache, local);
1876 read_unlock(&mrt_lock);
1879 return ip_local_deliver(skb);
1885 return ip_local_deliver(skb);
1890 #ifdef CONFIG_IP_PIMSM
1891 /* called with rcu_read_lock() */
1892 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
1893 unsigned int pimlen)
1895 struct net_device *reg_dev = NULL;
1896 struct iphdr *encap;
1898 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
1901 * a. packet is really sent to a multicast group
1902 * b. packet is not a NULL-REGISTER
1903 * c. packet is not truncated
1905 if (!ipv4_is_multicast(encap->daddr) ||
1906 encap->tot_len == 0 ||
1907 ntohs(encap->tot_len) + pimlen > skb->len)
1910 read_lock(&mrt_lock);
1911 if (mrt->mroute_reg_vif_num >= 0)
1912 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
1913 read_unlock(&mrt_lock);
1915 if (reg_dev == NULL)
1918 skb->mac_header = skb->network_header;
1919 skb_pull(skb, (u8 *)encap - skb->data);
1920 skb_reset_network_header(skb);
1921 skb->protocol = htons(ETH_P_IP);
1922 skb->ip_summed = CHECKSUM_NONE;
1923 skb->pkt_type = PACKET_HOST;
1925 skb_tunnel_rx(skb, reg_dev);
1929 return NET_RX_SUCCESS;
1933 #ifdef CONFIG_IP_PIMSM_V1
1935 * Handle IGMP messages of PIMv1
1938 int pim_rcv_v1(struct sk_buff *skb)
1940 struct igmphdr *pim;
1941 struct net *net = dev_net(skb->dev);
1942 struct mr_table *mrt;
1944 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1947 pim = igmp_hdr(skb);
1949 if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0)
1952 if (!mrt->mroute_do_pim ||
1953 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
1956 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
1964 #ifdef CONFIG_IP_PIMSM_V2
1965 static int pim_rcv(struct sk_buff *skb)
1967 struct pimreghdr *pim;
1968 struct net *net = dev_net(skb->dev);
1969 struct mr_table *mrt;
1971 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
1974 pim = (struct pimreghdr *)skb_transport_header(skb);
1975 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
1976 (pim->flags & PIM_NULL_REGISTER) ||
1977 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
1978 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
1981 if (ipmr_fib_lookup(net, &skb_rtable(skb)->fl, &mrt) < 0)
1984 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
1992 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
1993 struct mfc_cache *c, struct rtmsg *rtm)
1996 struct rtnexthop *nhp;
1997 u8 *b = skb_tail_pointer(skb);
1998 struct rtattr *mp_head;
2000 /* If cache is unresolved, don't try to parse IIF and OIF */
2001 if (c->mfc_parent >= MAXVIFS)
2004 if (VIF_EXISTS(mrt, c->mfc_parent))
2005 RTA_PUT(skb, RTA_IIF, 4, &mrt->vif_table[c->mfc_parent].dev->ifindex);
2007 mp_head = (struct rtattr *)skb_put(skb, RTA_LENGTH(0));
2009 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2010 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2011 if (skb_tailroom(skb) < RTA_ALIGN(RTA_ALIGN(sizeof(*nhp)) + 4))
2012 goto rtattr_failure;
2013 nhp = (struct rtnexthop *)skb_put(skb, RTA_ALIGN(sizeof(*nhp)));
2014 nhp->rtnh_flags = 0;
2015 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2016 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2017 nhp->rtnh_len = sizeof(*nhp);
2020 mp_head->rta_type = RTA_MULTIPATH;
2021 mp_head->rta_len = skb_tail_pointer(skb) - (u8 *)mp_head;
2022 rtm->rtm_type = RTN_MULTICAST;
2030 int ipmr_get_route(struct net *net,
2031 struct sk_buff *skb, struct rtmsg *rtm, int nowait)
2034 struct mr_table *mrt;
2035 struct mfc_cache *cache;
2036 struct rtable *rt = skb_rtable(skb);
2038 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2043 cache = ipmr_cache_find(mrt, rt->rt_src, rt->rt_dst);
2045 if (cache == NULL) {
2046 struct sk_buff *skb2;
2048 struct net_device *dev;
2057 read_lock(&mrt_lock);
2059 vif = ipmr_find_vif(mrt, dev);
2061 read_unlock(&mrt_lock);
2065 skb2 = skb_clone(skb, GFP_ATOMIC);
2067 read_unlock(&mrt_lock);
2072 skb_push(skb2, sizeof(struct iphdr));
2073 skb_reset_network_header(skb2);
2075 iph->ihl = sizeof(struct iphdr) >> 2;
2076 iph->saddr = rt->rt_src;
2077 iph->daddr = rt->rt_dst;
2079 err = ipmr_cache_unresolved(mrt, vif, skb2);
2080 read_unlock(&mrt_lock);
2085 read_lock(&mrt_lock);
2086 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
2087 cache->mfc_flags |= MFC_NOTIFY;
2088 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2089 read_unlock(&mrt_lock);
2094 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2095 u32 pid, u32 seq, struct mfc_cache *c)
2097 struct nlmsghdr *nlh;
2100 nlh = nlmsg_put(skb, pid, seq, RTM_NEWROUTE, sizeof(*rtm), NLM_F_MULTI);
2104 rtm = nlmsg_data(nlh);
2105 rtm->rtm_family = RTNL_FAMILY_IPMR;
2106 rtm->rtm_dst_len = 32;
2107 rtm->rtm_src_len = 32;
2109 rtm->rtm_table = mrt->id;
2110 NLA_PUT_U32(skb, RTA_TABLE, mrt->id);
2111 rtm->rtm_type = RTN_MULTICAST;
2112 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2113 rtm->rtm_protocol = RTPROT_UNSPEC;
2116 NLA_PUT_BE32(skb, RTA_SRC, c->mfc_origin);
2117 NLA_PUT_BE32(skb, RTA_DST, c->mfc_mcastgrp);
2119 if (__ipmr_fill_mroute(mrt, skb, c, rtm) < 0)
2120 goto nla_put_failure;
2122 return nlmsg_end(skb, nlh);
2125 nlmsg_cancel(skb, nlh);
2129 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2131 struct net *net = sock_net(skb->sk);
2132 struct mr_table *mrt;
2133 struct mfc_cache *mfc;
2134 unsigned int t = 0, s_t;
2135 unsigned int h = 0, s_h;
2136 unsigned int e = 0, s_e;
2143 ipmr_for_each_table(mrt, net) {
2148 for (h = s_h; h < MFC_LINES; h++) {
2149 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2152 if (ipmr_fill_mroute(mrt, skb,
2153 NETLINK_CB(cb->skb).pid,
2176 #ifdef CONFIG_PROC_FS
2178 * The /proc interfaces to multicast routing :
2179 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2181 struct ipmr_vif_iter {
2182 struct seq_net_private p;
2183 struct mr_table *mrt;
2187 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2188 struct ipmr_vif_iter *iter,
2191 struct mr_table *mrt = iter->mrt;
2193 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2194 if (!VIF_EXISTS(mrt, iter->ct))
2197 return &mrt->vif_table[iter->ct];
2202 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2203 __acquires(mrt_lock)
2205 struct ipmr_vif_iter *iter = seq->private;
2206 struct net *net = seq_file_net(seq);
2207 struct mr_table *mrt;
2209 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2211 return ERR_PTR(-ENOENT);
2215 read_lock(&mrt_lock);
2216 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2220 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2222 struct ipmr_vif_iter *iter = seq->private;
2223 struct net *net = seq_file_net(seq);
2224 struct mr_table *mrt = iter->mrt;
2227 if (v == SEQ_START_TOKEN)
2228 return ipmr_vif_seq_idx(net, iter, 0);
2230 while (++iter->ct < mrt->maxvif) {
2231 if (!VIF_EXISTS(mrt, iter->ct))
2233 return &mrt->vif_table[iter->ct];
2238 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2239 __releases(mrt_lock)
2241 read_unlock(&mrt_lock);
2244 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2246 struct ipmr_vif_iter *iter = seq->private;
2247 struct mr_table *mrt = iter->mrt;
2249 if (v == SEQ_START_TOKEN) {
2251 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2253 const struct vif_device *vif = v;
2254 const char *name = vif->dev ? vif->dev->name : "none";
2257 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2258 vif - mrt->vif_table,
2259 name, vif->bytes_in, vif->pkt_in,
2260 vif->bytes_out, vif->pkt_out,
2261 vif->flags, vif->local, vif->remote);
2266 static const struct seq_operations ipmr_vif_seq_ops = {
2267 .start = ipmr_vif_seq_start,
2268 .next = ipmr_vif_seq_next,
2269 .stop = ipmr_vif_seq_stop,
2270 .show = ipmr_vif_seq_show,
2273 static int ipmr_vif_open(struct inode *inode, struct file *file)
2275 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2276 sizeof(struct ipmr_vif_iter));
2279 static const struct file_operations ipmr_vif_fops = {
2280 .owner = THIS_MODULE,
2281 .open = ipmr_vif_open,
2283 .llseek = seq_lseek,
2284 .release = seq_release_net,
2287 struct ipmr_mfc_iter {
2288 struct seq_net_private p;
2289 struct mr_table *mrt;
2290 struct list_head *cache;
2295 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2296 struct ipmr_mfc_iter *it, loff_t pos)
2298 struct mr_table *mrt = it->mrt;
2299 struct mfc_cache *mfc;
2302 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2303 it->cache = &mrt->mfc_cache_array[it->ct];
2304 list_for_each_entry_rcu(mfc, it->cache, list)
2310 spin_lock_bh(&mfc_unres_lock);
2311 it->cache = &mrt->mfc_unres_queue;
2312 list_for_each_entry(mfc, it->cache, list)
2315 spin_unlock_bh(&mfc_unres_lock);
2322 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2324 struct ipmr_mfc_iter *it = seq->private;
2325 struct net *net = seq_file_net(seq);
2326 struct mr_table *mrt;
2328 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2330 return ERR_PTR(-ENOENT);
2335 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2339 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2341 struct mfc_cache *mfc = v;
2342 struct ipmr_mfc_iter *it = seq->private;
2343 struct net *net = seq_file_net(seq);
2344 struct mr_table *mrt = it->mrt;
2348 if (v == SEQ_START_TOKEN)
2349 return ipmr_mfc_seq_idx(net, seq->private, 0);
2351 if (mfc->list.next != it->cache)
2352 return list_entry(mfc->list.next, struct mfc_cache, list);
2354 if (it->cache == &mrt->mfc_unres_queue)
2357 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2359 while (++it->ct < MFC_LINES) {
2360 it->cache = &mrt->mfc_cache_array[it->ct];
2361 if (list_empty(it->cache))
2363 return list_first_entry(it->cache, struct mfc_cache, list);
2366 /* exhausted cache_array, show unresolved */
2368 it->cache = &mrt->mfc_unres_queue;
2371 spin_lock_bh(&mfc_unres_lock);
2372 if (!list_empty(it->cache))
2373 return list_first_entry(it->cache, struct mfc_cache, list);
2376 spin_unlock_bh(&mfc_unres_lock);
2382 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2384 struct ipmr_mfc_iter *it = seq->private;
2385 struct mr_table *mrt = it->mrt;
2387 if (it->cache == &mrt->mfc_unres_queue)
2388 spin_unlock_bh(&mfc_unres_lock);
2389 else if (it->cache == &mrt->mfc_cache_array[it->ct])
2393 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2397 if (v == SEQ_START_TOKEN) {
2399 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2401 const struct mfc_cache *mfc = v;
2402 const struct ipmr_mfc_iter *it = seq->private;
2403 const struct mr_table *mrt = it->mrt;
2405 seq_printf(seq, "%08X %08X %-3hd",
2406 (__force u32) mfc->mfc_mcastgrp,
2407 (__force u32) mfc->mfc_origin,
2410 if (it->cache != &mrt->mfc_unres_queue) {
2411 seq_printf(seq, " %8lu %8lu %8lu",
2412 mfc->mfc_un.res.pkt,
2413 mfc->mfc_un.res.bytes,
2414 mfc->mfc_un.res.wrong_if);
2415 for (n = mfc->mfc_un.res.minvif;
2416 n < mfc->mfc_un.res.maxvif; n++) {
2417 if (VIF_EXISTS(mrt, n) &&
2418 mfc->mfc_un.res.ttls[n] < 255)
2421 n, mfc->mfc_un.res.ttls[n]);
2424 /* unresolved mfc_caches don't contain
2425 * pkt, bytes and wrong_if values
2427 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2429 seq_putc(seq, '\n');
2434 static const struct seq_operations ipmr_mfc_seq_ops = {
2435 .start = ipmr_mfc_seq_start,
2436 .next = ipmr_mfc_seq_next,
2437 .stop = ipmr_mfc_seq_stop,
2438 .show = ipmr_mfc_seq_show,
2441 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2443 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2444 sizeof(struct ipmr_mfc_iter));
2447 static const struct file_operations ipmr_mfc_fops = {
2448 .owner = THIS_MODULE,
2449 .open = ipmr_mfc_open,
2451 .llseek = seq_lseek,
2452 .release = seq_release_net,
2456 #ifdef CONFIG_IP_PIMSM_V2
2457 static const struct net_protocol pim_protocol = {
2465 * Setup for IP multicast routing
2467 static int __net_init ipmr_net_init(struct net *net)
2471 err = ipmr_rules_init(net);
2475 #ifdef CONFIG_PROC_FS
2477 if (!proc_net_fops_create(net, "ip_mr_vif", 0, &ipmr_vif_fops))
2479 if (!proc_net_fops_create(net, "ip_mr_cache", 0, &ipmr_mfc_fops))
2480 goto proc_cache_fail;
2484 #ifdef CONFIG_PROC_FS
2486 proc_net_remove(net, "ip_mr_vif");
2488 ipmr_rules_exit(net);
2494 static void __net_exit ipmr_net_exit(struct net *net)
2496 #ifdef CONFIG_PROC_FS
2497 proc_net_remove(net, "ip_mr_cache");
2498 proc_net_remove(net, "ip_mr_vif");
2500 ipmr_rules_exit(net);
2503 static struct pernet_operations ipmr_net_ops = {
2504 .init = ipmr_net_init,
2505 .exit = ipmr_net_exit,
2508 int __init ip_mr_init(void)
2512 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2513 sizeof(struct mfc_cache),
2514 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2519 err = register_pernet_subsys(&ipmr_net_ops);
2521 goto reg_pernet_fail;
2523 err = register_netdevice_notifier(&ip_mr_notifier);
2525 goto reg_notif_fail;
2526 #ifdef CONFIG_IP_PIMSM_V2
2527 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2528 printk(KERN_ERR "ip_mr_init: can't add PIM protocol\n");
2530 goto add_proto_fail;
2533 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE, NULL, ipmr_rtm_dumproute);
2536 #ifdef CONFIG_IP_PIMSM_V2
2538 unregister_netdevice_notifier(&ip_mr_notifier);
2541 unregister_pernet_subsys(&ipmr_net_ops);
2543 kmem_cache_destroy(mrt_cachep);