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/uaccess.h>
30 #include <linux/types.h>
31 #include <linux/capability.h>
32 #include <linux/errno.h>
33 #include <linux/timer.h>
35 #include <linux/kernel.h>
36 #include <linux/fcntl.h>
37 #include <linux/stat.h>
38 #include <linux/socket.h>
40 #include <linux/inet.h>
41 #include <linux/netdevice.h>
42 #include <linux/inetdevice.h>
43 #include <linux/igmp.h>
44 #include <linux/proc_fs.h>
45 #include <linux/seq_file.h>
46 #include <linux/mroute.h>
47 #include <linux/init.h>
48 #include <linux/if_ether.h>
49 #include <linux/slab.h>
50 #include <net/net_namespace.h>
52 #include <net/protocol.h>
53 #include <linux/skbuff.h>
54 #include <net/route.h>
59 #include <linux/notifier.h>
60 #include <linux/if_arp.h>
61 #include <linux/netfilter_ipv4.h>
62 #include <linux/compat.h>
63 #include <linux/export.h>
64 #include <net/ip_tunnels.h>
65 #include <net/checksum.h>
66 #include <net/netlink.h>
67 #include <net/fib_rules.h>
68 #include <linux/netconf.h>
69 #include <net/nexthop.h>
72 struct fib_rule common;
79 /* Big lock, protecting vif table, mrt cache and mroute socket state.
80 * Note that the changes are semaphored via rtnl_lock.
83 static DEFINE_RWLOCK(mrt_lock);
85 /* Multicast router control variables */
87 /* Special spinlock for queue of unresolved entries */
88 static DEFINE_SPINLOCK(mfc_unres_lock);
90 /* We return to original Alan's scheme. Hash table of resolved
91 * entries is changed only in process context and protected
92 * with weak lock mrt_lock. Queue of unresolved entries is protected
93 * with strong spinlock mfc_unres_lock.
95 * In this case data path is free of exclusive locks at all.
98 static struct kmem_cache *mrt_cachep __read_mostly;
100 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
101 static void ipmr_free_table(struct mr_table *mrt);
103 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
104 struct sk_buff *skb, struct mfc_cache *cache,
106 static int ipmr_cache_report(struct mr_table *mrt,
107 struct sk_buff *pkt, vifi_t vifi, int assert);
108 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
109 struct mfc_cache *c, struct rtmsg *rtm);
110 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
112 static void mroute_clean_tables(struct mr_table *mrt, bool all);
113 static void ipmr_expire_process(unsigned long arg);
115 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
116 #define ipmr_for_each_table(mrt, net) \
117 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
119 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
121 struct mr_table *mrt;
123 ipmr_for_each_table(mrt, net) {
130 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
131 struct mr_table **mrt)
134 struct ipmr_result res;
135 struct fib_lookup_arg arg = {
137 .flags = FIB_LOOKUP_NOREF,
140 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
141 flowi4_to_flowi(flp4), 0, &arg);
148 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
149 int flags, struct fib_lookup_arg *arg)
151 struct ipmr_result *res = arg->result;
152 struct mr_table *mrt;
154 switch (rule->action) {
157 case FR_ACT_UNREACHABLE:
159 case FR_ACT_PROHIBIT:
161 case FR_ACT_BLACKHOLE:
166 mrt = ipmr_get_table(rule->fr_net, rule->table);
173 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
178 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
182 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
183 struct fib_rule_hdr *frh, struct nlattr **tb)
188 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
194 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
195 struct fib_rule_hdr *frh)
203 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
204 .family = RTNL_FAMILY_IPMR,
205 .rule_size = sizeof(struct ipmr_rule),
206 .addr_size = sizeof(u32),
207 .action = ipmr_rule_action,
208 .match = ipmr_rule_match,
209 .configure = ipmr_rule_configure,
210 .compare = ipmr_rule_compare,
211 .fill = ipmr_rule_fill,
212 .nlgroup = RTNLGRP_IPV4_RULE,
213 .policy = ipmr_rule_policy,
214 .owner = THIS_MODULE,
217 static int __net_init ipmr_rules_init(struct net *net)
219 struct fib_rules_ops *ops;
220 struct mr_table *mrt;
223 ops = fib_rules_register(&ipmr_rules_ops_template, net);
227 INIT_LIST_HEAD(&net->ipv4.mr_tables);
229 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
235 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
239 net->ipv4.mr_rules_ops = ops;
243 ipmr_free_table(mrt);
245 fib_rules_unregister(ops);
249 static void __net_exit ipmr_rules_exit(struct net *net)
251 struct mr_table *mrt, *next;
254 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
255 list_del(&mrt->list);
256 ipmr_free_table(mrt);
258 fib_rules_unregister(net->ipv4.mr_rules_ops);
262 #define ipmr_for_each_table(mrt, net) \
263 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
265 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
267 return net->ipv4.mrt;
270 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
271 struct mr_table **mrt)
273 *mrt = net->ipv4.mrt;
277 static int __net_init ipmr_rules_init(struct net *net)
279 struct mr_table *mrt;
281 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
288 static void __net_exit ipmr_rules_exit(struct net *net)
291 ipmr_free_table(net->ipv4.mrt);
292 net->ipv4.mrt = NULL;
297 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
299 struct mr_table *mrt;
302 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
303 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
304 return ERR_PTR(-EINVAL);
306 mrt = ipmr_get_table(net, id);
310 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
312 return ERR_PTR(-ENOMEM);
313 write_pnet(&mrt->net, net);
316 /* Forwarding cache */
317 for (i = 0; i < MFC_LINES; i++)
318 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
320 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
322 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
325 mrt->mroute_reg_vif_num = -1;
326 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
327 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
332 static void ipmr_free_table(struct mr_table *mrt)
334 del_timer_sync(&mrt->ipmr_expire_timer);
335 mroute_clean_tables(mrt, true);
339 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
341 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
343 struct net *net = dev_net(dev);
347 dev = __dev_get_by_name(net, "tunl0");
349 const struct net_device_ops *ops = dev->netdev_ops;
351 struct ip_tunnel_parm p;
353 memset(&p, 0, sizeof(p));
354 p.iph.daddr = v->vifc_rmt_addr.s_addr;
355 p.iph.saddr = v->vifc_lcl_addr.s_addr;
358 p.iph.protocol = IPPROTO_IPIP;
359 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
360 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
362 if (ops->ndo_do_ioctl) {
363 mm_segment_t oldfs = get_fs();
366 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
372 /* Initialize ipmr pimreg/tunnel in_device */
373 static bool ipmr_init_vif_indev(const struct net_device *dev)
375 struct in_device *in_dev;
379 in_dev = __in_dev_get_rtnl(dev);
382 ipv4_devconf_setall(in_dev);
383 neigh_parms_data_state_setall(in_dev->arp_parms);
384 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
389 static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
391 struct net_device *dev;
393 dev = __dev_get_by_name(net, "tunl0");
396 const struct net_device_ops *ops = dev->netdev_ops;
399 struct ip_tunnel_parm p;
401 memset(&p, 0, sizeof(p));
402 p.iph.daddr = v->vifc_rmt_addr.s_addr;
403 p.iph.saddr = v->vifc_lcl_addr.s_addr;
406 p.iph.protocol = IPPROTO_IPIP;
407 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
408 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
410 if (ops->ndo_do_ioctl) {
411 mm_segment_t oldfs = get_fs();
414 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
422 (dev = __dev_get_by_name(net, p.name)) != NULL) {
423 dev->flags |= IFF_MULTICAST;
424 if (!ipmr_init_vif_indev(dev))
434 unregister_netdevice(dev);
438 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
439 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
441 struct net *net = dev_net(dev);
442 struct mr_table *mrt;
443 struct flowi4 fl4 = {
444 .flowi4_oif = dev->ifindex,
445 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
446 .flowi4_mark = skb->mark,
450 err = ipmr_fib_lookup(net, &fl4, &mrt);
456 read_lock(&mrt_lock);
457 dev->stats.tx_bytes += skb->len;
458 dev->stats.tx_packets++;
459 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
460 read_unlock(&mrt_lock);
465 static int reg_vif_get_iflink(const struct net_device *dev)
470 static const struct net_device_ops reg_vif_netdev_ops = {
471 .ndo_start_xmit = reg_vif_xmit,
472 .ndo_get_iflink = reg_vif_get_iflink,
475 static void reg_vif_setup(struct net_device *dev)
477 dev->type = ARPHRD_PIMREG;
478 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
479 dev->flags = IFF_NOARP;
480 dev->netdev_ops = ®_vif_netdev_ops;
481 dev->destructor = free_netdev;
482 dev->features |= NETIF_F_NETNS_LOCAL;
485 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
487 struct net_device *dev;
490 if (mrt->id == RT_TABLE_DEFAULT)
491 sprintf(name, "pimreg");
493 sprintf(name, "pimreg%u", mrt->id);
495 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
500 dev_net_set(dev, net);
502 if (register_netdevice(dev)) {
507 if (!ipmr_init_vif_indev(dev))
517 unregister_netdevice(dev);
521 /* called with rcu_read_lock() */
522 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
525 struct net_device *reg_dev = NULL;
528 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
530 * a. packet is really sent to a multicast group
531 * b. packet is not a NULL-REGISTER
532 * c. packet is not truncated
534 if (!ipv4_is_multicast(encap->daddr) ||
535 encap->tot_len == 0 ||
536 ntohs(encap->tot_len) + pimlen > skb->len)
539 read_lock(&mrt_lock);
540 if (mrt->mroute_reg_vif_num >= 0)
541 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
542 read_unlock(&mrt_lock);
547 skb->mac_header = skb->network_header;
548 skb_pull(skb, (u8 *)encap - skb->data);
549 skb_reset_network_header(skb);
550 skb->protocol = htons(ETH_P_IP);
551 skb->ip_summed = CHECKSUM_NONE;
553 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
557 return NET_RX_SUCCESS;
560 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
567 * vif_delete - Delete a VIF entry
568 * @notify: Set to 1, if the caller is a notifier_call
570 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
571 struct list_head *head)
573 struct vif_device *v;
574 struct net_device *dev;
575 struct in_device *in_dev;
577 if (vifi < 0 || vifi >= mrt->maxvif)
578 return -EADDRNOTAVAIL;
580 v = &mrt->vif_table[vifi];
582 write_lock_bh(&mrt_lock);
587 write_unlock_bh(&mrt_lock);
588 return -EADDRNOTAVAIL;
591 if (vifi == mrt->mroute_reg_vif_num)
592 mrt->mroute_reg_vif_num = -1;
594 if (vifi + 1 == mrt->maxvif) {
597 for (tmp = vifi - 1; tmp >= 0; tmp--) {
598 if (VIF_EXISTS(mrt, tmp))
604 write_unlock_bh(&mrt_lock);
606 dev_set_allmulti(dev, -1);
608 in_dev = __in_dev_get_rtnl(dev);
610 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
611 inet_netconf_notify_devconf(dev_net(dev),
612 NETCONFA_MC_FORWARDING,
613 dev->ifindex, &in_dev->cnf);
614 ip_rt_multicast_event(in_dev);
617 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
618 unregister_netdevice_queue(dev, head);
624 static void ipmr_cache_free_rcu(struct rcu_head *head)
626 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
628 kmem_cache_free(mrt_cachep, c);
631 static inline void ipmr_cache_free(struct mfc_cache *c)
633 call_rcu(&c->rcu, ipmr_cache_free_rcu);
636 /* Destroy an unresolved cache entry, killing queued skbs
637 * and reporting error to netlink readers.
639 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
641 struct net *net = read_pnet(&mrt->net);
645 atomic_dec(&mrt->cache_resolve_queue_len);
647 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
648 if (ip_hdr(skb)->version == 0) {
649 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
650 nlh->nlmsg_type = NLMSG_ERROR;
651 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
652 skb_trim(skb, nlh->nlmsg_len);
654 e->error = -ETIMEDOUT;
655 memset(&e->msg, 0, sizeof(e->msg));
657 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
666 /* Timer process for the unresolved queue. */
667 static void ipmr_expire_process(unsigned long arg)
669 struct mr_table *mrt = (struct mr_table *)arg;
671 unsigned long expires;
672 struct mfc_cache *c, *next;
674 if (!spin_trylock(&mfc_unres_lock)) {
675 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
679 if (list_empty(&mrt->mfc_unres_queue))
685 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
686 if (time_after(c->mfc_un.unres.expires, now)) {
687 unsigned long interval = c->mfc_un.unres.expires - now;
688 if (interval < expires)
694 mroute_netlink_event(mrt, c, RTM_DELROUTE);
695 ipmr_destroy_unres(mrt, c);
698 if (!list_empty(&mrt->mfc_unres_queue))
699 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
702 spin_unlock(&mfc_unres_lock);
705 /* Fill oifs list. It is called under write locked mrt_lock. */
706 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
711 cache->mfc_un.res.minvif = MAXVIFS;
712 cache->mfc_un.res.maxvif = 0;
713 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
715 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
716 if (VIF_EXISTS(mrt, vifi) &&
717 ttls[vifi] && ttls[vifi] < 255) {
718 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
719 if (cache->mfc_un.res.minvif > vifi)
720 cache->mfc_un.res.minvif = vifi;
721 if (cache->mfc_un.res.maxvif <= vifi)
722 cache->mfc_un.res.maxvif = vifi + 1;
725 cache->mfc_un.res.lastuse = jiffies;
728 static int vif_add(struct net *net, struct mr_table *mrt,
729 struct vifctl *vifc, int mrtsock)
731 int vifi = vifc->vifc_vifi;
732 struct vif_device *v = &mrt->vif_table[vifi];
733 struct net_device *dev;
734 struct in_device *in_dev;
738 if (VIF_EXISTS(mrt, vifi))
741 switch (vifc->vifc_flags) {
743 if (!ipmr_pimsm_enabled())
745 /* Special Purpose VIF in PIM
746 * All the packets will be sent to the daemon
748 if (mrt->mroute_reg_vif_num >= 0)
750 dev = ipmr_reg_vif(net, mrt);
753 err = dev_set_allmulti(dev, 1);
755 unregister_netdevice(dev);
761 dev = ipmr_new_tunnel(net, vifc);
764 err = dev_set_allmulti(dev, 1);
766 ipmr_del_tunnel(dev, vifc);
771 case VIFF_USE_IFINDEX:
773 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
774 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
775 if (dev && !__in_dev_get_rtnl(dev)) {
777 return -EADDRNOTAVAIL;
780 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
783 return -EADDRNOTAVAIL;
784 err = dev_set_allmulti(dev, 1);
794 in_dev = __in_dev_get_rtnl(dev);
797 return -EADDRNOTAVAIL;
799 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
800 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
802 ip_rt_multicast_event(in_dev);
804 /* Fill in the VIF structures */
806 v->rate_limit = vifc->vifc_rate_limit;
807 v->local = vifc->vifc_lcl_addr.s_addr;
808 v->remote = vifc->vifc_rmt_addr.s_addr;
809 v->flags = vifc->vifc_flags;
811 v->flags |= VIFF_STATIC;
812 v->threshold = vifc->vifc_threshold;
817 v->link = dev->ifindex;
818 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
819 v->link = dev_get_iflink(dev);
821 /* And finish update writing critical data */
822 write_lock_bh(&mrt_lock);
824 if (v->flags & VIFF_REGISTER)
825 mrt->mroute_reg_vif_num = vifi;
826 if (vifi+1 > mrt->maxvif)
827 mrt->maxvif = vifi+1;
828 write_unlock_bh(&mrt_lock);
832 /* called with rcu_read_lock() */
833 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
837 int line = MFC_HASH(mcastgrp, origin);
840 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
841 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
847 /* Look for a (*,*,oif) entry */
848 static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
851 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
854 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
855 if (c->mfc_origin == htonl(INADDR_ANY) &&
856 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
857 c->mfc_un.res.ttls[vifi] < 255)
863 /* Look for a (*,G) entry */
864 static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
865 __be32 mcastgrp, int vifi)
867 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
868 struct mfc_cache *c, *proxy;
870 if (mcastgrp == htonl(INADDR_ANY))
873 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
874 if (c->mfc_origin == htonl(INADDR_ANY) &&
875 c->mfc_mcastgrp == mcastgrp) {
876 if (c->mfc_un.res.ttls[vifi] < 255)
879 /* It's ok if the vifi is part of the static tree */
880 proxy = ipmr_cache_find_any_parent(mrt,
882 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
887 return ipmr_cache_find_any_parent(mrt, vifi);
890 /* Allocate a multicast cache entry */
891 static struct mfc_cache *ipmr_cache_alloc(void)
893 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
896 c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
897 c->mfc_un.res.minvif = MAXVIFS;
902 static struct mfc_cache *ipmr_cache_alloc_unres(void)
904 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
907 skb_queue_head_init(&c->mfc_un.unres.unresolved);
908 c->mfc_un.unres.expires = jiffies + 10*HZ;
913 /* A cache entry has gone into a resolved state from queued */
914 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
915 struct mfc_cache *uc, struct mfc_cache *c)
920 /* Play the pending entries through our router */
921 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
922 if (ip_hdr(skb)->version == 0) {
923 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
925 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
926 nlh->nlmsg_len = skb_tail_pointer(skb) -
929 nlh->nlmsg_type = NLMSG_ERROR;
930 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
931 skb_trim(skb, nlh->nlmsg_len);
933 e->error = -EMSGSIZE;
934 memset(&e->msg, 0, sizeof(e->msg));
937 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
939 ip_mr_forward(net, mrt, skb, c, 0);
944 /* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
945 * expects the following bizarre scheme.
947 * Called under mrt_lock.
949 static int ipmr_cache_report(struct mr_table *mrt,
950 struct sk_buff *pkt, vifi_t vifi, int assert)
952 const int ihl = ip_hdrlen(pkt);
953 struct sock *mroute_sk;
954 struct igmphdr *igmp;
959 if (assert == IGMPMSG_WHOLEPKT)
960 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
962 skb = alloc_skb(128, GFP_ATOMIC);
967 if (assert == IGMPMSG_WHOLEPKT) {
968 /* Ugly, but we have no choice with this interface.
969 * Duplicate old header, fix ihl, length etc.
970 * And all this only to mangle msg->im_msgtype and
971 * to set msg->im_mbz to "mbz" :-)
973 skb_push(skb, sizeof(struct iphdr));
974 skb_reset_network_header(skb);
975 skb_reset_transport_header(skb);
976 msg = (struct igmpmsg *)skb_network_header(skb);
977 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
978 msg->im_msgtype = IGMPMSG_WHOLEPKT;
980 msg->im_vif = mrt->mroute_reg_vif_num;
981 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
982 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
983 sizeof(struct iphdr));
985 /* Copy the IP header */
986 skb_set_network_header(skb, skb->len);
988 skb_copy_to_linear_data(skb, pkt->data, ihl);
989 /* Flag to the kernel this is a route add */
990 ip_hdr(skb)->protocol = 0;
991 msg = (struct igmpmsg *)skb_network_header(skb);
993 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
995 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
997 msg->im_msgtype = assert;
999 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1000 skb->transport_header = skb->network_header;
1004 mroute_sk = rcu_dereference(mrt->mroute_sk);
1011 /* Deliver to mrouted */
1012 ret = sock_queue_rcv_skb(mroute_sk, skb);
1015 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1022 /* Queue a packet for resolution. It gets locked cache entry! */
1023 static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1024 struct sk_buff *skb)
1028 struct mfc_cache *c;
1029 const struct iphdr *iph = ip_hdr(skb);
1031 spin_lock_bh(&mfc_unres_lock);
1032 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
1033 if (c->mfc_mcastgrp == iph->daddr &&
1034 c->mfc_origin == iph->saddr) {
1041 /* Create a new entry if allowable */
1042 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1043 (c = ipmr_cache_alloc_unres()) == NULL) {
1044 spin_unlock_bh(&mfc_unres_lock);
1050 /* Fill in the new cache entry */
1052 c->mfc_origin = iph->saddr;
1053 c->mfc_mcastgrp = iph->daddr;
1055 /* Reflect first query at mrouted. */
1056 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1058 /* If the report failed throw the cache entry
1061 spin_unlock_bh(&mfc_unres_lock);
1068 atomic_inc(&mrt->cache_resolve_queue_len);
1069 list_add(&c->list, &mrt->mfc_unres_queue);
1070 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1072 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1073 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1076 /* See if we can append the packet */
1077 if (c->mfc_un.unres.unresolved.qlen > 3) {
1081 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1085 spin_unlock_bh(&mfc_unres_lock);
1089 /* MFC cache manipulation by user space mroute daemon */
1091 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1094 struct mfc_cache *c, *next;
1096 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1098 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1099 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1100 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1101 (parent == -1 || parent == c->mfc_parent)) {
1102 list_del_rcu(&c->list);
1103 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1111 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1112 struct mfcctl *mfc, int mrtsock, int parent)
1116 struct mfc_cache *uc, *c;
1118 if (mfc->mfcc_parent >= MAXVIFS)
1121 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1123 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1124 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1125 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1126 (parent == -1 || parent == c->mfc_parent)) {
1133 write_lock_bh(&mrt_lock);
1134 c->mfc_parent = mfc->mfcc_parent;
1135 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1137 c->mfc_flags |= MFC_STATIC;
1138 write_unlock_bh(&mrt_lock);
1139 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1143 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1144 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1147 c = ipmr_cache_alloc();
1151 c->mfc_origin = mfc->mfcc_origin.s_addr;
1152 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1153 c->mfc_parent = mfc->mfcc_parent;
1154 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1156 c->mfc_flags |= MFC_STATIC;
1158 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1160 /* Check to see if we resolved a queued list. If so we
1161 * need to send on the frames and tidy up.
1164 spin_lock_bh(&mfc_unres_lock);
1165 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1166 if (uc->mfc_origin == c->mfc_origin &&
1167 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1168 list_del(&uc->list);
1169 atomic_dec(&mrt->cache_resolve_queue_len);
1174 if (list_empty(&mrt->mfc_unres_queue))
1175 del_timer(&mrt->ipmr_expire_timer);
1176 spin_unlock_bh(&mfc_unres_lock);
1179 ipmr_cache_resolve(net, mrt, uc, c);
1180 ipmr_cache_free(uc);
1182 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1186 /* Close the multicast socket, and clear the vif tables etc */
1187 static void mroute_clean_tables(struct mr_table *mrt, bool all)
1191 struct mfc_cache *c, *next;
1193 /* Shut down all active vif entries */
1194 for (i = 0; i < mrt->maxvif; i++) {
1195 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1197 vif_delete(mrt, i, 0, &list);
1199 unregister_netdevice_many(&list);
1201 /* Wipe the cache */
1202 for (i = 0; i < MFC_LINES; i++) {
1203 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1204 if (!all && (c->mfc_flags & MFC_STATIC))
1206 list_del_rcu(&c->list);
1207 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1212 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1213 spin_lock_bh(&mfc_unres_lock);
1214 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1216 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1217 ipmr_destroy_unres(mrt, c);
1219 spin_unlock_bh(&mfc_unres_lock);
1223 /* called from ip_ra_control(), before an RCU grace period,
1224 * we dont need to call synchronize_rcu() here
1226 static void mrtsock_destruct(struct sock *sk)
1228 struct net *net = sock_net(sk);
1229 struct mr_table *mrt;
1232 ipmr_for_each_table(mrt, net) {
1233 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1234 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1235 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1236 NETCONFA_IFINDEX_ALL,
1237 net->ipv4.devconf_all);
1238 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1239 mroute_clean_tables(mrt, false);
1245 /* Socket options and virtual interface manipulation. The whole
1246 * virtual interface system is a complete heap, but unfortunately
1247 * that's how BSD mrouted happens to think. Maybe one day with a proper
1248 * MOSPF/PIM router set up we can clean this up.
1251 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1252 unsigned int optlen)
1254 struct net *net = sock_net(sk);
1255 int val, ret = 0, parent = 0;
1256 struct mr_table *mrt;
1261 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1263 if (sk->sk_type != SOCK_RAW ||
1264 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1269 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1274 if (optname != MRT_INIT) {
1275 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1276 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1284 if (optlen != sizeof(int)) {
1288 if (rtnl_dereference(mrt->mroute_sk)) {
1293 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1295 rcu_assign_pointer(mrt->mroute_sk, sk);
1296 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1297 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1298 NETCONFA_IFINDEX_ALL,
1299 net->ipv4.devconf_all);
1303 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1306 /* We need to unlock here because mrtsock_destruct takes
1307 * care of rtnl itself and we can't change that due to
1308 * the IP_ROUTER_ALERT setsockopt which runs without it.
1311 ret = ip_ra_control(sk, 0, NULL);
1317 if (optlen != sizeof(vif)) {
1321 if (copy_from_user(&vif, optval, sizeof(vif))) {
1325 if (vif.vifc_vifi >= MAXVIFS) {
1329 if (optname == MRT_ADD_VIF) {
1330 ret = vif_add(net, mrt, &vif,
1331 sk == rtnl_dereference(mrt->mroute_sk));
1333 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1336 /* Manipulate the forwarding caches. These live
1337 * in a sort of kernel/user symbiosis.
1342 case MRT_ADD_MFC_PROXY:
1343 case MRT_DEL_MFC_PROXY:
1344 if (optlen != sizeof(mfc)) {
1348 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1353 parent = mfc.mfcc_parent;
1354 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1355 ret = ipmr_mfc_delete(mrt, &mfc, parent);
1357 ret = ipmr_mfc_add(net, mrt, &mfc,
1358 sk == rtnl_dereference(mrt->mroute_sk),
1361 /* Control PIM assert. */
1363 if (optlen != sizeof(val)) {
1367 if (get_user(val, (int __user *)optval)) {
1371 mrt->mroute_do_assert = val;
1374 if (!ipmr_pimsm_enabled()) {
1378 if (optlen != sizeof(val)) {
1382 if (get_user(val, (int __user *)optval)) {
1388 if (val != mrt->mroute_do_pim) {
1389 mrt->mroute_do_pim = val;
1390 mrt->mroute_do_assert = val;
1394 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1398 if (optlen != sizeof(uval)) {
1402 if (get_user(uval, (u32 __user *)optval)) {
1407 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1410 mrt = ipmr_new_table(net, uval);
1414 raw_sk(sk)->ipmr_table = uval;
1417 /* Spurious command, or MRT_VERSION which you cannot set. */
1427 /* Getsock opt support for the multicast routing system. */
1428 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1432 struct net *net = sock_net(sk);
1433 struct mr_table *mrt;
1435 if (sk->sk_type != SOCK_RAW ||
1436 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1439 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1448 if (!ipmr_pimsm_enabled())
1449 return -ENOPROTOOPT;
1450 val = mrt->mroute_do_pim;
1453 val = mrt->mroute_do_assert;
1456 return -ENOPROTOOPT;
1459 if (get_user(olr, optlen))
1461 olr = min_t(unsigned int, olr, sizeof(int));
1464 if (put_user(olr, optlen))
1466 if (copy_to_user(optval, &val, olr))
1471 /* The IP multicast ioctl support routines. */
1472 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1474 struct sioc_sg_req sr;
1475 struct sioc_vif_req vr;
1476 struct vif_device *vif;
1477 struct mfc_cache *c;
1478 struct net *net = sock_net(sk);
1479 struct mr_table *mrt;
1481 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1487 if (copy_from_user(&vr, arg, sizeof(vr)))
1489 if (vr.vifi >= mrt->maxvif)
1491 read_lock(&mrt_lock);
1492 vif = &mrt->vif_table[vr.vifi];
1493 if (VIF_EXISTS(mrt, vr.vifi)) {
1494 vr.icount = vif->pkt_in;
1495 vr.ocount = vif->pkt_out;
1496 vr.ibytes = vif->bytes_in;
1497 vr.obytes = vif->bytes_out;
1498 read_unlock(&mrt_lock);
1500 if (copy_to_user(arg, &vr, sizeof(vr)))
1504 read_unlock(&mrt_lock);
1505 return -EADDRNOTAVAIL;
1507 if (copy_from_user(&sr, arg, sizeof(sr)))
1511 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1513 sr.pktcnt = c->mfc_un.res.pkt;
1514 sr.bytecnt = c->mfc_un.res.bytes;
1515 sr.wrong_if = c->mfc_un.res.wrong_if;
1518 if (copy_to_user(arg, &sr, sizeof(sr)))
1523 return -EADDRNOTAVAIL;
1525 return -ENOIOCTLCMD;
1529 #ifdef CONFIG_COMPAT
1530 struct compat_sioc_sg_req {
1533 compat_ulong_t pktcnt;
1534 compat_ulong_t bytecnt;
1535 compat_ulong_t wrong_if;
1538 struct compat_sioc_vif_req {
1539 vifi_t vifi; /* Which iface */
1540 compat_ulong_t icount;
1541 compat_ulong_t ocount;
1542 compat_ulong_t ibytes;
1543 compat_ulong_t obytes;
1546 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1548 struct compat_sioc_sg_req sr;
1549 struct compat_sioc_vif_req vr;
1550 struct vif_device *vif;
1551 struct mfc_cache *c;
1552 struct net *net = sock_net(sk);
1553 struct mr_table *mrt;
1555 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1561 if (copy_from_user(&vr, arg, sizeof(vr)))
1563 if (vr.vifi >= mrt->maxvif)
1565 read_lock(&mrt_lock);
1566 vif = &mrt->vif_table[vr.vifi];
1567 if (VIF_EXISTS(mrt, vr.vifi)) {
1568 vr.icount = vif->pkt_in;
1569 vr.ocount = vif->pkt_out;
1570 vr.ibytes = vif->bytes_in;
1571 vr.obytes = vif->bytes_out;
1572 read_unlock(&mrt_lock);
1574 if (copy_to_user(arg, &vr, sizeof(vr)))
1578 read_unlock(&mrt_lock);
1579 return -EADDRNOTAVAIL;
1581 if (copy_from_user(&sr, arg, sizeof(sr)))
1585 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1587 sr.pktcnt = c->mfc_un.res.pkt;
1588 sr.bytecnt = c->mfc_un.res.bytes;
1589 sr.wrong_if = c->mfc_un.res.wrong_if;
1592 if (copy_to_user(arg, &sr, sizeof(sr)))
1597 return -EADDRNOTAVAIL;
1599 return -ENOIOCTLCMD;
1604 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1606 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1607 struct net *net = dev_net(dev);
1608 struct mr_table *mrt;
1609 struct vif_device *v;
1612 if (event != NETDEV_UNREGISTER)
1615 ipmr_for_each_table(mrt, net) {
1616 v = &mrt->vif_table[0];
1617 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1619 vif_delete(mrt, ct, 1, NULL);
1625 static struct notifier_block ip_mr_notifier = {
1626 .notifier_call = ipmr_device_event,
1629 /* Encapsulate a packet by attaching a valid IPIP header to it.
1630 * This avoids tunnel drivers and other mess and gives us the speed so
1631 * important for multicast video.
1633 static void ip_encap(struct net *net, struct sk_buff *skb,
1634 __be32 saddr, __be32 daddr)
1637 const struct iphdr *old_iph = ip_hdr(skb);
1639 skb_push(skb, sizeof(struct iphdr));
1640 skb->transport_header = skb->network_header;
1641 skb_reset_network_header(skb);
1645 iph->tos = old_iph->tos;
1646 iph->ttl = old_iph->ttl;
1650 iph->protocol = IPPROTO_IPIP;
1652 iph->tot_len = htons(skb->len);
1653 ip_select_ident(net, skb, NULL);
1656 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1660 static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1661 struct sk_buff *skb)
1663 struct ip_options *opt = &(IPCB(skb)->opt);
1665 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1666 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
1668 if (unlikely(opt->optlen))
1669 ip_forward_options(skb);
1671 return dst_output(net, sk, skb);
1674 /* Processing handlers for ipmr_forward */
1676 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1677 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1679 const struct iphdr *iph = ip_hdr(skb);
1680 struct vif_device *vif = &mrt->vif_table[vifi];
1681 struct net_device *dev;
1689 if (vif->flags & VIFF_REGISTER) {
1691 vif->bytes_out += skb->len;
1692 vif->dev->stats.tx_bytes += skb->len;
1693 vif->dev->stats.tx_packets++;
1694 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1698 if (vif->flags & VIFF_TUNNEL) {
1699 rt = ip_route_output_ports(net, &fl4, NULL,
1700 vif->remote, vif->local,
1703 RT_TOS(iph->tos), vif->link);
1706 encap = sizeof(struct iphdr);
1708 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1711 RT_TOS(iph->tos), vif->link);
1718 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1719 /* Do not fragment multicasts. Alas, IPv4 does not
1720 * allow to send ICMP, so that packets will disappear
1723 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1728 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1730 if (skb_cow(skb, encap)) {
1736 vif->bytes_out += skb->len;
1739 skb_dst_set(skb, &rt->dst);
1740 ip_decrease_ttl(ip_hdr(skb));
1742 /* FIXME: forward and output firewalls used to be called here.
1743 * What do we do with netfilter? -- RR
1745 if (vif->flags & VIFF_TUNNEL) {
1746 ip_encap(net, skb, vif->local, vif->remote);
1747 /* FIXME: extra output firewall step used to be here. --RR */
1748 vif->dev->stats.tx_packets++;
1749 vif->dev->stats.tx_bytes += skb->len;
1752 IPCB(skb)->flags |= IPSKB_FORWARDED | IPSKB_FRAG_SEGS;
1754 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1755 * not only before forwarding, but after forwarding on all output
1756 * interfaces. It is clear, if mrouter runs a multicasting
1757 * program, it should receive packets not depending to what interface
1758 * program is joined.
1759 * If we will not make it, the program will have to join on all
1760 * interfaces. On the other hand, multihoming host (or router, but
1761 * not mrouter) cannot join to more than one interface - it will
1762 * result in receiving multiple packets.
1764 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1765 net, NULL, skb, skb->dev, dev,
1766 ipmr_forward_finish);
1773 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1777 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1778 if (mrt->vif_table[ct].dev == dev)
1784 /* "local" means that we should preserve one skb (for local delivery) */
1785 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1786 struct sk_buff *skb, struct mfc_cache *cache,
1791 int true_vifi = ipmr_find_vif(mrt, skb->dev);
1793 vif = cache->mfc_parent;
1794 cache->mfc_un.res.pkt++;
1795 cache->mfc_un.res.bytes += skb->len;
1796 cache->mfc_un.res.lastuse = jiffies;
1798 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1799 struct mfc_cache *cache_proxy;
1801 /* For an (*,G) entry, we only check that the incomming
1802 * interface is part of the static tree.
1804 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1806 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1810 /* Wrong interface: drop packet and (maybe) send PIM assert. */
1811 if (mrt->vif_table[vif].dev != skb->dev) {
1812 if (rt_is_output_route(skb_rtable(skb))) {
1813 /* It is our own packet, looped back.
1814 * Very complicated situation...
1816 * The best workaround until routing daemons will be
1817 * fixed is not to redistribute packet, if it was
1818 * send through wrong interface. It means, that
1819 * multicast applications WILL NOT work for
1820 * (S,G), which have default multicast route pointing
1821 * to wrong oif. In any case, it is not a good
1822 * idea to use multicasting applications on router.
1827 cache->mfc_un.res.wrong_if++;
1829 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1830 /* pimsm uses asserts, when switching from RPT to SPT,
1831 * so that we cannot check that packet arrived on an oif.
1832 * It is bad, but otherwise we would need to move pretty
1833 * large chunk of pimd to kernel. Ough... --ANK
1835 (mrt->mroute_do_pim ||
1836 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1838 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1839 cache->mfc_un.res.last_assert = jiffies;
1840 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1846 mrt->vif_table[vif].pkt_in++;
1847 mrt->vif_table[vif].bytes_in += skb->len;
1849 /* Forward the frame */
1850 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1851 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
1852 if (true_vifi >= 0 &&
1853 true_vifi != cache->mfc_parent &&
1855 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1856 /* It's an (*,*) entry and the packet is not coming from
1857 * the upstream: forward the packet to the upstream
1860 psend = cache->mfc_parent;
1865 for (ct = cache->mfc_un.res.maxvif - 1;
1866 ct >= cache->mfc_un.res.minvif; ct--) {
1867 /* For (*,G) entry, don't forward to the incoming interface */
1868 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1870 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1872 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1875 ipmr_queue_xmit(net, mrt, skb2, cache,
1884 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1887 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1889 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1899 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1901 struct rtable *rt = skb_rtable(skb);
1902 struct iphdr *iph = ip_hdr(skb);
1903 struct flowi4 fl4 = {
1904 .daddr = iph->daddr,
1905 .saddr = iph->saddr,
1906 .flowi4_tos = RT_TOS(iph->tos),
1907 .flowi4_oif = (rt_is_output_route(rt) ?
1908 skb->dev->ifindex : 0),
1909 .flowi4_iif = (rt_is_output_route(rt) ?
1912 .flowi4_mark = skb->mark,
1914 struct mr_table *mrt;
1917 err = ipmr_fib_lookup(net, &fl4, &mrt);
1919 return ERR_PTR(err);
1923 /* Multicast packets for forwarding arrive here
1924 * Called with rcu_read_lock();
1926 int ip_mr_input(struct sk_buff *skb)
1928 struct mfc_cache *cache;
1929 struct net *net = dev_net(skb->dev);
1930 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1931 struct mr_table *mrt;
1933 /* Packet is looped back after forward, it should not be
1934 * forwarded second time, but still can be delivered locally.
1936 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1939 mrt = ipmr_rt_fib_lookup(net, skb);
1942 return PTR_ERR(mrt);
1945 if (IPCB(skb)->opt.router_alert) {
1946 if (ip_call_ra_chain(skb))
1948 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1949 /* IGMPv1 (and broken IGMPv2 implementations sort of
1950 * Cisco IOS <= 11.2(8)) do not put router alert
1951 * option to IGMP packets destined to routable
1952 * groups. It is very bad, because it means
1953 * that we can forward NO IGMP messages.
1955 struct sock *mroute_sk;
1957 mroute_sk = rcu_dereference(mrt->mroute_sk);
1960 raw_rcv(mroute_sk, skb);
1966 /* already under rcu_read_lock() */
1967 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1969 int vif = ipmr_find_vif(mrt, skb->dev);
1972 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1976 /* No usable cache entry */
1981 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1982 ip_local_deliver(skb);
1988 read_lock(&mrt_lock);
1989 vif = ipmr_find_vif(mrt, skb->dev);
1991 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
1992 read_unlock(&mrt_lock);
1996 read_unlock(&mrt_lock);
2001 read_lock(&mrt_lock);
2002 ip_mr_forward(net, mrt, skb, cache, local);
2003 read_unlock(&mrt_lock);
2006 return ip_local_deliver(skb);
2012 return ip_local_deliver(skb);
2017 #ifdef CONFIG_IP_PIMSM_V1
2018 /* Handle IGMP messages of PIMv1 */
2019 int pim_rcv_v1(struct sk_buff *skb)
2021 struct igmphdr *pim;
2022 struct net *net = dev_net(skb->dev);
2023 struct mr_table *mrt;
2025 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2028 pim = igmp_hdr(skb);
2030 mrt = ipmr_rt_fib_lookup(net, skb);
2033 if (!mrt->mroute_do_pim ||
2034 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2037 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2045 #ifdef CONFIG_IP_PIMSM_V2
2046 static int pim_rcv(struct sk_buff *skb)
2048 struct pimreghdr *pim;
2049 struct net *net = dev_net(skb->dev);
2050 struct mr_table *mrt;
2052 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2055 pim = (struct pimreghdr *)skb_transport_header(skb);
2056 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2057 (pim->flags & PIM_NULL_REGISTER) ||
2058 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2059 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2062 mrt = ipmr_rt_fib_lookup(net, skb);
2065 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2073 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2074 struct mfc_cache *c, struct rtmsg *rtm)
2076 struct rta_mfc_stats mfcs;
2077 struct nlattr *mp_attr;
2078 struct rtnexthop *nhp;
2079 unsigned long lastuse;
2082 /* If cache is unresolved, don't try to parse IIF and OIF */
2083 if (c->mfc_parent >= MAXVIFS)
2086 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2087 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2090 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2093 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2094 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2095 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2096 nla_nest_cancel(skb, mp_attr);
2100 nhp->rtnh_flags = 0;
2101 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2102 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2103 nhp->rtnh_len = sizeof(*nhp);
2107 nla_nest_end(skb, mp_attr);
2109 lastuse = READ_ONCE(c->mfc_un.res.lastuse);
2110 lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
2112 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2113 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2114 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2115 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
2116 nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
2120 rtm->rtm_type = RTN_MULTICAST;
2124 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2125 __be32 saddr, __be32 daddr,
2126 struct rtmsg *rtm, int nowait, u32 portid)
2128 struct mfc_cache *cache;
2129 struct mr_table *mrt;
2132 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2137 cache = ipmr_cache_find(mrt, saddr, daddr);
2138 if (!cache && skb->dev) {
2139 int vif = ipmr_find_vif(mrt, skb->dev);
2142 cache = ipmr_cache_find_any(mrt, daddr, vif);
2145 struct sk_buff *skb2;
2147 struct net_device *dev;
2156 read_lock(&mrt_lock);
2158 vif = ipmr_find_vif(mrt, dev);
2160 read_unlock(&mrt_lock);
2164 skb2 = skb_clone(skb, GFP_ATOMIC);
2166 read_unlock(&mrt_lock);
2171 NETLINK_CB(skb2).portid = portid;
2172 skb_push(skb2, sizeof(struct iphdr));
2173 skb_reset_network_header(skb2);
2175 iph->ihl = sizeof(struct iphdr) >> 2;
2179 err = ipmr_cache_unresolved(mrt, vif, skb2);
2180 read_unlock(&mrt_lock);
2185 read_lock(&mrt_lock);
2186 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2187 read_unlock(&mrt_lock);
2192 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2193 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2196 struct nlmsghdr *nlh;
2200 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2204 rtm = nlmsg_data(nlh);
2205 rtm->rtm_family = RTNL_FAMILY_IPMR;
2206 rtm->rtm_dst_len = 32;
2207 rtm->rtm_src_len = 32;
2209 rtm->rtm_table = mrt->id;
2210 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2211 goto nla_put_failure;
2212 rtm->rtm_type = RTN_MULTICAST;
2213 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2214 if (c->mfc_flags & MFC_STATIC)
2215 rtm->rtm_protocol = RTPROT_STATIC;
2217 rtm->rtm_protocol = RTPROT_MROUTED;
2220 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2221 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2222 goto nla_put_failure;
2223 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2224 /* do not break the dump if cache is unresolved */
2225 if (err < 0 && err != -ENOENT)
2226 goto nla_put_failure;
2228 nlmsg_end(skb, nlh);
2232 nlmsg_cancel(skb, nlh);
2236 static size_t mroute_msgsize(bool unresolved, int maxvif)
2239 NLMSG_ALIGN(sizeof(struct rtmsg))
2240 + nla_total_size(4) /* RTA_TABLE */
2241 + nla_total_size(4) /* RTA_SRC */
2242 + nla_total_size(4) /* RTA_DST */
2247 + nla_total_size(4) /* RTA_IIF */
2248 + nla_total_size(0) /* RTA_MULTIPATH */
2249 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2251 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
2257 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2260 struct net *net = read_pnet(&mrt->net);
2261 struct sk_buff *skb;
2264 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2269 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2273 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2279 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2282 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2284 struct net *net = sock_net(skb->sk);
2285 struct mr_table *mrt;
2286 struct mfc_cache *mfc;
2287 unsigned int t = 0, s_t;
2288 unsigned int h = 0, s_h;
2289 unsigned int e = 0, s_e;
2296 ipmr_for_each_table(mrt, net) {
2301 for (h = s_h; h < MFC_LINES; h++) {
2302 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2305 if (ipmr_fill_mroute(mrt, skb,
2306 NETLINK_CB(cb->skb).portid,
2316 spin_lock_bh(&mfc_unres_lock);
2317 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2320 if (ipmr_fill_mroute(mrt, skb,
2321 NETLINK_CB(cb->skb).portid,
2325 spin_unlock_bh(&mfc_unres_lock);
2331 spin_unlock_bh(&mfc_unres_lock);
2347 static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2348 [RTA_SRC] = { .type = NLA_U32 },
2349 [RTA_DST] = { .type = NLA_U32 },
2350 [RTA_IIF] = { .type = NLA_U32 },
2351 [RTA_TABLE] = { .type = NLA_U32 },
2352 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2355 static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2357 switch (rtm_protocol) {
2359 case RTPROT_MROUTED:
2365 static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2367 struct rtnexthop *rtnh = nla_data(nla);
2368 int remaining = nla_len(nla), vifi = 0;
2370 while (rtnh_ok(rtnh, remaining)) {
2371 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2372 if (++vifi == MAXVIFS)
2374 rtnh = rtnh_next(rtnh, &remaining);
2377 return remaining > 0 ? -EINVAL : vifi;
2380 /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2381 static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2382 struct mfcctl *mfcc, int *mrtsock,
2383 struct mr_table **mrtret)
2385 struct net_device *dev = NULL;
2386 u32 tblid = RT_TABLE_DEFAULT;
2387 struct mr_table *mrt;
2388 struct nlattr *attr;
2392 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy);
2395 rtm = nlmsg_data(nlh);
2398 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2399 rtm->rtm_type != RTN_MULTICAST ||
2400 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2401 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2404 memset(mfcc, 0, sizeof(*mfcc));
2405 mfcc->mfcc_parent = -1;
2407 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2408 switch (nla_type(attr)) {
2410 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2413 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2416 dev = __dev_get_by_index(net, nla_get_u32(attr));
2423 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2432 tblid = nla_get_u32(attr);
2436 mrt = ipmr_get_table(net, tblid);
2442 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2444 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2450 /* takes care of both newroute and delroute */
2451 static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh)
2453 struct net *net = sock_net(skb->sk);
2454 int ret, mrtsock, parent;
2455 struct mr_table *tbl;
2460 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl);
2464 parent = ret ? mfcc.mfcc_parent : -1;
2465 if (nlh->nlmsg_type == RTM_NEWROUTE)
2466 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2468 return ipmr_mfc_delete(tbl, &mfcc, parent);
2471 #ifdef CONFIG_PROC_FS
2472 /* The /proc interfaces to multicast routing :
2473 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2475 struct ipmr_vif_iter {
2476 struct seq_net_private p;
2477 struct mr_table *mrt;
2481 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2482 struct ipmr_vif_iter *iter,
2485 struct mr_table *mrt = iter->mrt;
2487 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2488 if (!VIF_EXISTS(mrt, iter->ct))
2491 return &mrt->vif_table[iter->ct];
2496 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2497 __acquires(mrt_lock)
2499 struct ipmr_vif_iter *iter = seq->private;
2500 struct net *net = seq_file_net(seq);
2501 struct mr_table *mrt;
2503 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2505 return ERR_PTR(-ENOENT);
2509 read_lock(&mrt_lock);
2510 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2514 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2516 struct ipmr_vif_iter *iter = seq->private;
2517 struct net *net = seq_file_net(seq);
2518 struct mr_table *mrt = iter->mrt;
2521 if (v == SEQ_START_TOKEN)
2522 return ipmr_vif_seq_idx(net, iter, 0);
2524 while (++iter->ct < mrt->maxvif) {
2525 if (!VIF_EXISTS(mrt, iter->ct))
2527 return &mrt->vif_table[iter->ct];
2532 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2533 __releases(mrt_lock)
2535 read_unlock(&mrt_lock);
2538 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2540 struct ipmr_vif_iter *iter = seq->private;
2541 struct mr_table *mrt = iter->mrt;
2543 if (v == SEQ_START_TOKEN) {
2545 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2547 const struct vif_device *vif = v;
2548 const char *name = vif->dev ? vif->dev->name : "none";
2551 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2552 vif - mrt->vif_table,
2553 name, vif->bytes_in, vif->pkt_in,
2554 vif->bytes_out, vif->pkt_out,
2555 vif->flags, vif->local, vif->remote);
2560 static const struct seq_operations ipmr_vif_seq_ops = {
2561 .start = ipmr_vif_seq_start,
2562 .next = ipmr_vif_seq_next,
2563 .stop = ipmr_vif_seq_stop,
2564 .show = ipmr_vif_seq_show,
2567 static int ipmr_vif_open(struct inode *inode, struct file *file)
2569 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2570 sizeof(struct ipmr_vif_iter));
2573 static const struct file_operations ipmr_vif_fops = {
2574 .owner = THIS_MODULE,
2575 .open = ipmr_vif_open,
2577 .llseek = seq_lseek,
2578 .release = seq_release_net,
2581 struct ipmr_mfc_iter {
2582 struct seq_net_private p;
2583 struct mr_table *mrt;
2584 struct list_head *cache;
2589 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2590 struct ipmr_mfc_iter *it, loff_t pos)
2592 struct mr_table *mrt = it->mrt;
2593 struct mfc_cache *mfc;
2596 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2597 it->cache = &mrt->mfc_cache_array[it->ct];
2598 list_for_each_entry_rcu(mfc, it->cache, list)
2604 spin_lock_bh(&mfc_unres_lock);
2605 it->cache = &mrt->mfc_unres_queue;
2606 list_for_each_entry(mfc, it->cache, list)
2609 spin_unlock_bh(&mfc_unres_lock);
2616 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2618 struct ipmr_mfc_iter *it = seq->private;
2619 struct net *net = seq_file_net(seq);
2620 struct mr_table *mrt;
2622 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2624 return ERR_PTR(-ENOENT);
2629 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2633 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2635 struct mfc_cache *mfc = v;
2636 struct ipmr_mfc_iter *it = seq->private;
2637 struct net *net = seq_file_net(seq);
2638 struct mr_table *mrt = it->mrt;
2642 if (v == SEQ_START_TOKEN)
2643 return ipmr_mfc_seq_idx(net, seq->private, 0);
2645 if (mfc->list.next != it->cache)
2646 return list_entry(mfc->list.next, struct mfc_cache, list);
2648 if (it->cache == &mrt->mfc_unres_queue)
2651 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2653 while (++it->ct < MFC_LINES) {
2654 it->cache = &mrt->mfc_cache_array[it->ct];
2655 if (list_empty(it->cache))
2657 return list_first_entry(it->cache, struct mfc_cache, list);
2660 /* exhausted cache_array, show unresolved */
2662 it->cache = &mrt->mfc_unres_queue;
2665 spin_lock_bh(&mfc_unres_lock);
2666 if (!list_empty(it->cache))
2667 return list_first_entry(it->cache, struct mfc_cache, list);
2670 spin_unlock_bh(&mfc_unres_lock);
2676 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2678 struct ipmr_mfc_iter *it = seq->private;
2679 struct mr_table *mrt = it->mrt;
2681 if (it->cache == &mrt->mfc_unres_queue)
2682 spin_unlock_bh(&mfc_unres_lock);
2683 else if (it->cache == &mrt->mfc_cache_array[it->ct])
2687 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2691 if (v == SEQ_START_TOKEN) {
2693 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2695 const struct mfc_cache *mfc = v;
2696 const struct ipmr_mfc_iter *it = seq->private;
2697 const struct mr_table *mrt = it->mrt;
2699 seq_printf(seq, "%08X %08X %-3hd",
2700 (__force u32) mfc->mfc_mcastgrp,
2701 (__force u32) mfc->mfc_origin,
2704 if (it->cache != &mrt->mfc_unres_queue) {
2705 seq_printf(seq, " %8lu %8lu %8lu",
2706 mfc->mfc_un.res.pkt,
2707 mfc->mfc_un.res.bytes,
2708 mfc->mfc_un.res.wrong_if);
2709 for (n = mfc->mfc_un.res.minvif;
2710 n < mfc->mfc_un.res.maxvif; n++) {
2711 if (VIF_EXISTS(mrt, n) &&
2712 mfc->mfc_un.res.ttls[n] < 255)
2715 n, mfc->mfc_un.res.ttls[n]);
2718 /* unresolved mfc_caches don't contain
2719 * pkt, bytes and wrong_if values
2721 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2723 seq_putc(seq, '\n');
2728 static const struct seq_operations ipmr_mfc_seq_ops = {
2729 .start = ipmr_mfc_seq_start,
2730 .next = ipmr_mfc_seq_next,
2731 .stop = ipmr_mfc_seq_stop,
2732 .show = ipmr_mfc_seq_show,
2735 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2737 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2738 sizeof(struct ipmr_mfc_iter));
2741 static const struct file_operations ipmr_mfc_fops = {
2742 .owner = THIS_MODULE,
2743 .open = ipmr_mfc_open,
2745 .llseek = seq_lseek,
2746 .release = seq_release_net,
2750 #ifdef CONFIG_IP_PIMSM_V2
2751 static const struct net_protocol pim_protocol = {
2757 /* Setup for IP multicast routing */
2758 static int __net_init ipmr_net_init(struct net *net)
2762 err = ipmr_rules_init(net);
2766 #ifdef CONFIG_PROC_FS
2768 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
2770 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
2771 goto proc_cache_fail;
2775 #ifdef CONFIG_PROC_FS
2777 remove_proc_entry("ip_mr_vif", net->proc_net);
2779 ipmr_rules_exit(net);
2785 static void __net_exit ipmr_net_exit(struct net *net)
2787 #ifdef CONFIG_PROC_FS
2788 remove_proc_entry("ip_mr_cache", net->proc_net);
2789 remove_proc_entry("ip_mr_vif", net->proc_net);
2791 ipmr_rules_exit(net);
2794 static struct pernet_operations ipmr_net_ops = {
2795 .init = ipmr_net_init,
2796 .exit = ipmr_net_exit,
2799 int __init ip_mr_init(void)
2803 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2804 sizeof(struct mfc_cache),
2805 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2808 err = register_pernet_subsys(&ipmr_net_ops);
2810 goto reg_pernet_fail;
2812 err = register_netdevice_notifier(&ip_mr_notifier);
2814 goto reg_notif_fail;
2815 #ifdef CONFIG_IP_PIMSM_V2
2816 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2817 pr_err("%s: can't add PIM protocol\n", __func__);
2819 goto add_proto_fail;
2822 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2823 NULL, ipmr_rtm_dumproute, NULL);
2824 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
2825 ipmr_rtm_route, NULL, NULL);
2826 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
2827 ipmr_rtm_route, NULL, NULL);
2830 #ifdef CONFIG_IP_PIMSM_V2
2832 unregister_netdevice_notifier(&ip_mr_notifier);
2835 unregister_pernet_subsys(&ipmr_net_ops);
2837 kmem_cache_destroy(mrt_cachep);