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>
71 struct fib_rule common;
78 static inline bool pimsm_enabled(void)
80 return IS_BUILTIN(CONFIG_IP_PIMSM_V1) || IS_BUILTIN(CONFIG_IP_PIMSM_V2);
83 /* Big lock, protecting vif table, mrt cache and mroute socket state.
84 * Note that the changes are semaphored via rtnl_lock.
87 static DEFINE_RWLOCK(mrt_lock);
89 /* Multicast router control variables */
91 /* Special spinlock for queue of unresolved entries */
92 static DEFINE_SPINLOCK(mfc_unres_lock);
94 /* We return to original Alan's scheme. Hash table of resolved
95 * entries is changed only in process context and protected
96 * with weak lock mrt_lock. Queue of unresolved entries is protected
97 * with strong spinlock mfc_unres_lock.
99 * In this case data path is free of exclusive locks at all.
102 static struct kmem_cache *mrt_cachep __read_mostly;
104 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
105 static void ipmr_free_table(struct mr_table *mrt);
107 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
108 struct sk_buff *skb, struct mfc_cache *cache,
110 static int ipmr_cache_report(struct mr_table *mrt,
111 struct sk_buff *pkt, vifi_t vifi, int assert);
112 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
113 struct mfc_cache *c, struct rtmsg *rtm);
114 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
116 static void mroute_clean_tables(struct mr_table *mrt);
117 static void ipmr_expire_process(unsigned long arg);
119 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
120 #define ipmr_for_each_table(mrt, net) \
121 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
123 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
125 struct mr_table *mrt;
127 ipmr_for_each_table(mrt, net) {
134 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
135 struct mr_table **mrt)
138 struct ipmr_result res;
139 struct fib_lookup_arg arg = {
141 .flags = FIB_LOOKUP_NOREF,
144 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
145 flowi4_to_flowi(flp4), 0, &arg);
152 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
153 int flags, struct fib_lookup_arg *arg)
155 struct ipmr_result *res = arg->result;
156 struct mr_table *mrt;
158 switch (rule->action) {
161 case FR_ACT_UNREACHABLE:
163 case FR_ACT_PROHIBIT:
165 case FR_ACT_BLACKHOLE:
170 mrt = ipmr_get_table(rule->fr_net, rule->table);
177 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
182 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
186 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
187 struct fib_rule_hdr *frh, struct nlattr **tb)
192 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
198 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
199 struct fib_rule_hdr *frh)
207 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
208 .family = RTNL_FAMILY_IPMR,
209 .rule_size = sizeof(struct ipmr_rule),
210 .addr_size = sizeof(u32),
211 .action = ipmr_rule_action,
212 .match = ipmr_rule_match,
213 .configure = ipmr_rule_configure,
214 .compare = ipmr_rule_compare,
215 .fill = ipmr_rule_fill,
216 .nlgroup = RTNLGRP_IPV4_RULE,
217 .policy = ipmr_rule_policy,
218 .owner = THIS_MODULE,
221 static int __net_init ipmr_rules_init(struct net *net)
223 struct fib_rules_ops *ops;
224 struct mr_table *mrt;
227 ops = fib_rules_register(&ipmr_rules_ops_template, net);
231 INIT_LIST_HEAD(&net->ipv4.mr_tables);
233 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
239 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
243 net->ipv4.mr_rules_ops = ops;
247 ipmr_free_table(mrt);
249 fib_rules_unregister(ops);
253 static void __net_exit ipmr_rules_exit(struct net *net)
255 struct mr_table *mrt, *next;
258 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
259 list_del(&mrt->list);
260 ipmr_free_table(mrt);
262 fib_rules_unregister(net->ipv4.mr_rules_ops);
266 #define ipmr_for_each_table(mrt, net) \
267 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
269 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
271 return net->ipv4.mrt;
274 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
275 struct mr_table **mrt)
277 *mrt = net->ipv4.mrt;
281 static int __net_init ipmr_rules_init(struct net *net)
283 struct mr_table *mrt;
285 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
292 static void __net_exit ipmr_rules_exit(struct net *net)
295 ipmr_free_table(net->ipv4.mrt);
296 net->ipv4.mrt = NULL;
301 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
303 struct mr_table *mrt;
306 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
307 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
308 return ERR_PTR(-EINVAL);
310 mrt = ipmr_get_table(net, id);
314 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
316 return ERR_PTR(-ENOMEM);
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 mrt->mroute_reg_vif_num = -1;
330 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
331 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
336 static void ipmr_free_table(struct mr_table *mrt)
338 del_timer_sync(&mrt->ipmr_expire_timer);
339 mroute_clean_tables(mrt);
343 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
345 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
347 struct net *net = dev_net(dev);
351 dev = __dev_get_by_name(net, "tunl0");
353 const struct net_device_ops *ops = dev->netdev_ops;
355 struct ip_tunnel_parm p;
357 memset(&p, 0, sizeof(p));
358 p.iph.daddr = v->vifc_rmt_addr.s_addr;
359 p.iph.saddr = v->vifc_lcl_addr.s_addr;
362 p.iph.protocol = IPPROTO_IPIP;
363 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
364 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
366 if (ops->ndo_do_ioctl) {
367 mm_segment_t oldfs = get_fs();
370 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
376 /* Initialize ipmr pimreg/tunnel in_device */
377 static bool ipmr_init_vif_indev(const struct net_device *dev)
379 struct in_device *in_dev;
383 in_dev = __in_dev_get_rtnl(dev);
386 ipv4_devconf_setall(in_dev);
387 neigh_parms_data_state_setall(in_dev->arp_parms);
388 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
393 static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
395 struct net_device *dev;
397 dev = __dev_get_by_name(net, "tunl0");
400 const struct net_device_ops *ops = dev->netdev_ops;
403 struct ip_tunnel_parm p;
405 memset(&p, 0, sizeof(p));
406 p.iph.daddr = v->vifc_rmt_addr.s_addr;
407 p.iph.saddr = v->vifc_lcl_addr.s_addr;
410 p.iph.protocol = IPPROTO_IPIP;
411 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
412 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
414 if (ops->ndo_do_ioctl) {
415 mm_segment_t oldfs = get_fs();
418 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
426 (dev = __dev_get_by_name(net, p.name)) != NULL) {
427 dev->flags |= IFF_MULTICAST;
428 if (!ipmr_init_vif_indev(dev))
438 /* allow the register to be completed before unregistering. */
442 unregister_netdevice(dev);
446 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
447 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
449 struct net *net = dev_net(dev);
450 struct mr_table *mrt;
451 struct flowi4 fl4 = {
452 .flowi4_oif = dev->ifindex,
453 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
454 .flowi4_mark = skb->mark,
458 err = ipmr_fib_lookup(net, &fl4, &mrt);
464 read_lock(&mrt_lock);
465 dev->stats.tx_bytes += skb->len;
466 dev->stats.tx_packets++;
467 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
468 read_unlock(&mrt_lock);
473 static int reg_vif_get_iflink(const struct net_device *dev)
478 static const struct net_device_ops reg_vif_netdev_ops = {
479 .ndo_start_xmit = reg_vif_xmit,
480 .ndo_get_iflink = reg_vif_get_iflink,
483 static void reg_vif_setup(struct net_device *dev)
485 dev->type = ARPHRD_PIMREG;
486 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
487 dev->flags = IFF_NOARP;
488 dev->netdev_ops = ®_vif_netdev_ops;
489 dev->destructor = free_netdev;
490 dev->features |= NETIF_F_NETNS_LOCAL;
493 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
495 struct net_device *dev;
498 if (mrt->id == RT_TABLE_DEFAULT)
499 sprintf(name, "pimreg");
501 sprintf(name, "pimreg%u", mrt->id);
503 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
508 dev_net_set(dev, net);
510 if (register_netdevice(dev)) {
515 if (!ipmr_init_vif_indev(dev))
525 /* allow the register to be completed before unregistering. */
529 unregister_netdevice(dev);
533 /* called with rcu_read_lock() */
534 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
537 struct net_device *reg_dev = NULL;
540 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
542 * a. packet is really sent to a multicast group
543 * b. packet is not a NULL-REGISTER
544 * c. packet is not truncated
546 if (!ipv4_is_multicast(encap->daddr) ||
547 encap->tot_len == 0 ||
548 ntohs(encap->tot_len) + pimlen > skb->len)
551 read_lock(&mrt_lock);
552 if (mrt->mroute_reg_vif_num >= 0)
553 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
554 read_unlock(&mrt_lock);
559 skb->mac_header = skb->network_header;
560 skb_pull(skb, (u8 *)encap - skb->data);
561 skb_reset_network_header(skb);
562 skb->protocol = htons(ETH_P_IP);
563 skb->ip_summed = CHECKSUM_NONE;
565 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
569 return NET_RX_SUCCESS;
572 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
579 * vif_delete - Delete a VIF entry
580 * @notify: Set to 1, if the caller is a notifier_call
582 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
583 struct list_head *head)
585 struct vif_device *v;
586 struct net_device *dev;
587 struct in_device *in_dev;
589 if (vifi < 0 || vifi >= mrt->maxvif)
590 return -EADDRNOTAVAIL;
592 v = &mrt->vif_table[vifi];
594 write_lock_bh(&mrt_lock);
599 write_unlock_bh(&mrt_lock);
600 return -EADDRNOTAVAIL;
603 if (vifi == mrt->mroute_reg_vif_num)
604 mrt->mroute_reg_vif_num = -1;
606 if (vifi + 1 == mrt->maxvif) {
609 for (tmp = vifi - 1; tmp >= 0; tmp--) {
610 if (VIF_EXISTS(mrt, tmp))
616 write_unlock_bh(&mrt_lock);
618 dev_set_allmulti(dev, -1);
620 in_dev = __in_dev_get_rtnl(dev);
622 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
623 inet_netconf_notify_devconf(dev_net(dev),
624 NETCONFA_MC_FORWARDING,
625 dev->ifindex, &in_dev->cnf);
626 ip_rt_multicast_event(in_dev);
629 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
630 unregister_netdevice_queue(dev, head);
636 static void ipmr_cache_free_rcu(struct rcu_head *head)
638 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
640 kmem_cache_free(mrt_cachep, c);
643 static inline void ipmr_cache_free(struct mfc_cache *c)
645 call_rcu(&c->rcu, ipmr_cache_free_rcu);
648 /* Destroy an unresolved cache entry, killing queued skbs
649 * and reporting error to netlink readers.
651 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
653 struct net *net = read_pnet(&mrt->net);
657 atomic_dec(&mrt->cache_resolve_queue_len);
659 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
660 if (ip_hdr(skb)->version == 0) {
661 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
662 nlh->nlmsg_type = NLMSG_ERROR;
663 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
664 skb_trim(skb, nlh->nlmsg_len);
666 e->error = -ETIMEDOUT;
667 memset(&e->msg, 0, sizeof(e->msg));
669 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
678 /* Timer process for the unresolved queue. */
679 static void ipmr_expire_process(unsigned long arg)
681 struct mr_table *mrt = (struct mr_table *)arg;
683 unsigned long expires;
684 struct mfc_cache *c, *next;
686 if (!spin_trylock(&mfc_unres_lock)) {
687 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
691 if (list_empty(&mrt->mfc_unres_queue))
697 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
698 if (time_after(c->mfc_un.unres.expires, now)) {
699 unsigned long interval = c->mfc_un.unres.expires - now;
700 if (interval < expires)
706 mroute_netlink_event(mrt, c, RTM_DELROUTE);
707 ipmr_destroy_unres(mrt, c);
710 if (!list_empty(&mrt->mfc_unres_queue))
711 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
714 spin_unlock(&mfc_unres_lock);
717 /* Fill oifs list. It is called under write locked mrt_lock. */
718 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
723 cache->mfc_un.res.minvif = MAXVIFS;
724 cache->mfc_un.res.maxvif = 0;
725 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
727 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
728 if (VIF_EXISTS(mrt, vifi) &&
729 ttls[vifi] && ttls[vifi] < 255) {
730 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
731 if (cache->mfc_un.res.minvif > vifi)
732 cache->mfc_un.res.minvif = vifi;
733 if (cache->mfc_un.res.maxvif <= vifi)
734 cache->mfc_un.res.maxvif = vifi + 1;
739 static int vif_add(struct net *net, struct mr_table *mrt,
740 struct vifctl *vifc, int mrtsock)
742 int vifi = vifc->vifc_vifi;
743 struct vif_device *v = &mrt->vif_table[vifi];
744 struct net_device *dev;
745 struct in_device *in_dev;
749 if (VIF_EXISTS(mrt, vifi))
752 switch (vifc->vifc_flags) {
754 if (!pimsm_enabled())
756 /* Special Purpose VIF in PIM
757 * All the packets will be sent to the daemon
759 if (mrt->mroute_reg_vif_num >= 0)
761 dev = ipmr_reg_vif(net, mrt);
764 err = dev_set_allmulti(dev, 1);
766 unregister_netdevice(dev);
772 dev = ipmr_new_tunnel(net, vifc);
775 err = dev_set_allmulti(dev, 1);
777 ipmr_del_tunnel(dev, vifc);
782 case VIFF_USE_IFINDEX:
784 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
785 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
786 if (dev && !__in_dev_get_rtnl(dev)) {
788 return -EADDRNOTAVAIL;
791 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
794 return -EADDRNOTAVAIL;
795 err = dev_set_allmulti(dev, 1);
805 in_dev = __in_dev_get_rtnl(dev);
808 return -EADDRNOTAVAIL;
810 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
811 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
813 ip_rt_multicast_event(in_dev);
815 /* Fill in the VIF structures */
817 v->rate_limit = vifc->vifc_rate_limit;
818 v->local = vifc->vifc_lcl_addr.s_addr;
819 v->remote = vifc->vifc_rmt_addr.s_addr;
820 v->flags = vifc->vifc_flags;
822 v->flags |= VIFF_STATIC;
823 v->threshold = vifc->vifc_threshold;
828 v->link = dev->ifindex;
829 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
830 v->link = dev_get_iflink(dev);
832 /* And finish update writing critical data */
833 write_lock_bh(&mrt_lock);
835 if (v->flags & VIFF_REGISTER)
836 mrt->mroute_reg_vif_num = vifi;
837 if (vifi+1 > mrt->maxvif)
838 mrt->maxvif = vifi+1;
839 write_unlock_bh(&mrt_lock);
843 /* called with rcu_read_lock() */
844 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
848 int line = MFC_HASH(mcastgrp, origin);
851 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
852 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
858 /* Look for a (*,*,oif) entry */
859 static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
862 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
865 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
866 if (c->mfc_origin == htonl(INADDR_ANY) &&
867 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
868 c->mfc_un.res.ttls[vifi] < 255)
874 /* Look for a (*,G) entry */
875 static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
876 __be32 mcastgrp, int vifi)
878 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
879 struct mfc_cache *c, *proxy;
881 if (mcastgrp == htonl(INADDR_ANY))
884 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
885 if (c->mfc_origin == htonl(INADDR_ANY) &&
886 c->mfc_mcastgrp == mcastgrp) {
887 if (c->mfc_un.res.ttls[vifi] < 255)
890 /* It's ok if the vifi is part of the static tree */
891 proxy = ipmr_cache_find_any_parent(mrt,
893 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
898 return ipmr_cache_find_any_parent(mrt, vifi);
901 /* Allocate a multicast cache entry */
902 static struct mfc_cache *ipmr_cache_alloc(void)
904 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
907 c->mfc_un.res.minvif = MAXVIFS;
911 static struct mfc_cache *ipmr_cache_alloc_unres(void)
913 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
916 skb_queue_head_init(&c->mfc_un.unres.unresolved);
917 c->mfc_un.unres.expires = jiffies + 10*HZ;
922 /* A cache entry has gone into a resolved state from queued */
923 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
924 struct mfc_cache *uc, struct mfc_cache *c)
929 /* Play the pending entries through our router */
930 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
931 if (ip_hdr(skb)->version == 0) {
932 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
934 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
935 nlh->nlmsg_len = skb_tail_pointer(skb) -
938 nlh->nlmsg_type = NLMSG_ERROR;
939 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
940 skb_trim(skb, nlh->nlmsg_len);
942 e->error = -EMSGSIZE;
943 memset(&e->msg, 0, sizeof(e->msg));
946 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
948 ip_mr_forward(net, mrt, skb, c, 0);
953 /* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
954 * expects the following bizarre scheme.
956 * Called under mrt_lock.
958 static int ipmr_cache_report(struct mr_table *mrt,
959 struct sk_buff *pkt, vifi_t vifi, int assert)
961 const int ihl = ip_hdrlen(pkt);
962 struct sock *mroute_sk;
963 struct igmphdr *igmp;
968 if (assert == IGMPMSG_WHOLEPKT)
969 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
971 skb = alloc_skb(128, GFP_ATOMIC);
976 if (assert == IGMPMSG_WHOLEPKT) {
977 /* Ugly, but we have no choice with this interface.
978 * Duplicate old header, fix ihl, length etc.
979 * And all this only to mangle msg->im_msgtype and
980 * to set msg->im_mbz to "mbz" :-)
982 skb_push(skb, sizeof(struct iphdr));
983 skb_reset_network_header(skb);
984 skb_reset_transport_header(skb);
985 msg = (struct igmpmsg *)skb_network_header(skb);
986 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
987 msg->im_msgtype = IGMPMSG_WHOLEPKT;
989 msg->im_vif = mrt->mroute_reg_vif_num;
990 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
991 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
992 sizeof(struct iphdr));
994 /* Copy the IP header */
995 skb_set_network_header(skb, skb->len);
997 skb_copy_to_linear_data(skb, pkt->data, ihl);
998 /* Flag to the kernel this is a route add */
999 ip_hdr(skb)->protocol = 0;
1000 msg = (struct igmpmsg *)skb_network_header(skb);
1002 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1003 /* Add our header */
1004 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
1005 igmp->type = assert;
1006 msg->im_msgtype = assert;
1008 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1009 skb->transport_header = skb->network_header;
1013 mroute_sk = rcu_dereference(mrt->mroute_sk);
1020 /* Deliver to mrouted */
1021 ret = sock_queue_rcv_skb(mroute_sk, skb);
1024 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1031 /* Queue a packet for resolution. It gets locked cache entry! */
1032 static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1033 struct sk_buff *skb)
1037 struct mfc_cache *c;
1038 const struct iphdr *iph = ip_hdr(skb);
1040 spin_lock_bh(&mfc_unres_lock);
1041 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
1042 if (c->mfc_mcastgrp == iph->daddr &&
1043 c->mfc_origin == iph->saddr) {
1050 /* Create a new entry if allowable */
1051 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1052 (c = ipmr_cache_alloc_unres()) == NULL) {
1053 spin_unlock_bh(&mfc_unres_lock);
1059 /* Fill in the new cache entry */
1061 c->mfc_origin = iph->saddr;
1062 c->mfc_mcastgrp = iph->daddr;
1064 /* Reflect first query at mrouted. */
1065 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1067 /* If the report failed throw the cache entry
1070 spin_unlock_bh(&mfc_unres_lock);
1077 atomic_inc(&mrt->cache_resolve_queue_len);
1078 list_add(&c->list, &mrt->mfc_unres_queue);
1079 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1081 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1082 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1085 /* See if we can append the packet */
1086 if (c->mfc_un.unres.unresolved.qlen > 3) {
1090 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1094 spin_unlock_bh(&mfc_unres_lock);
1098 /* MFC cache manipulation by user space mroute daemon */
1100 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1103 struct mfc_cache *c, *next;
1105 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1107 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1108 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1109 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1110 (parent == -1 || parent == c->mfc_parent)) {
1111 list_del_rcu(&c->list);
1112 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1120 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1121 struct mfcctl *mfc, int mrtsock, int parent)
1125 struct mfc_cache *uc, *c;
1127 if (mfc->mfcc_parent >= MAXVIFS)
1130 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1132 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1133 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1134 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1135 (parent == -1 || parent == c->mfc_parent)) {
1142 write_lock_bh(&mrt_lock);
1143 c->mfc_parent = mfc->mfcc_parent;
1144 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1146 c->mfc_flags |= MFC_STATIC;
1147 write_unlock_bh(&mrt_lock);
1148 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1152 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1153 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1156 c = ipmr_cache_alloc();
1160 c->mfc_origin = mfc->mfcc_origin.s_addr;
1161 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1162 c->mfc_parent = mfc->mfcc_parent;
1163 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1165 c->mfc_flags |= MFC_STATIC;
1167 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1169 /* Check to see if we resolved a queued list. If so we
1170 * need to send on the frames and tidy up.
1173 spin_lock_bh(&mfc_unres_lock);
1174 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1175 if (uc->mfc_origin == c->mfc_origin &&
1176 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1177 list_del(&uc->list);
1178 atomic_dec(&mrt->cache_resolve_queue_len);
1183 if (list_empty(&mrt->mfc_unres_queue))
1184 del_timer(&mrt->ipmr_expire_timer);
1185 spin_unlock_bh(&mfc_unres_lock);
1188 ipmr_cache_resolve(net, mrt, uc, c);
1189 ipmr_cache_free(uc);
1191 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1195 /* Close the multicast socket, and clear the vif tables etc */
1196 static void mroute_clean_tables(struct mr_table *mrt)
1200 struct mfc_cache *c, *next;
1202 /* Shut down all active vif entries */
1203 for (i = 0; i < mrt->maxvif; i++) {
1204 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
1205 vif_delete(mrt, i, 0, &list);
1207 unregister_netdevice_many(&list);
1209 /* Wipe the cache */
1210 for (i = 0; i < MFC_LINES; i++) {
1211 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1212 if (c->mfc_flags & MFC_STATIC)
1214 list_del_rcu(&c->list);
1215 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1220 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1221 spin_lock_bh(&mfc_unres_lock);
1222 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1224 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1225 ipmr_destroy_unres(mrt, c);
1227 spin_unlock_bh(&mfc_unres_lock);
1231 /* called from ip_ra_control(), before an RCU grace period,
1232 * we dont need to call synchronize_rcu() here
1234 static void mrtsock_destruct(struct sock *sk)
1236 struct net *net = sock_net(sk);
1237 struct mr_table *mrt;
1240 ipmr_for_each_table(mrt, net) {
1241 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1242 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1243 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1244 NETCONFA_IFINDEX_ALL,
1245 net->ipv4.devconf_all);
1246 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1247 mroute_clean_tables(mrt);
1253 /* Socket options and virtual interface manipulation. The whole
1254 * virtual interface system is a complete heap, but unfortunately
1255 * that's how BSD mrouted happens to think. Maybe one day with a proper
1256 * MOSPF/PIM router set up we can clean this up.
1259 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1260 unsigned int optlen)
1262 struct net *net = sock_net(sk);
1263 int val, ret = 0, parent = 0;
1264 struct mr_table *mrt;
1269 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1271 if (sk->sk_type != SOCK_RAW ||
1272 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1277 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1282 if (optname != MRT_INIT) {
1283 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1284 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1292 if (optlen != sizeof(int))
1294 if (rtnl_dereference(mrt->mroute_sk))
1299 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1301 rcu_assign_pointer(mrt->mroute_sk, sk);
1302 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1303 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1304 NETCONFA_IFINDEX_ALL,
1305 net->ipv4.devconf_all);
1309 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1312 /* We need to unlock here because mrtsock_destruct takes
1313 * care of rtnl itself and we can't change that due to
1314 * the IP_ROUTER_ALERT setsockopt which runs without it.
1317 ret = ip_ra_control(sk, 0, NULL);
1323 if (optlen != sizeof(vif)) {
1327 if (copy_from_user(&vif, optval, sizeof(vif))) {
1331 if (vif.vifc_vifi >= MAXVIFS) {
1335 if (optname == MRT_ADD_VIF) {
1336 ret = vif_add(net, mrt, &vif,
1337 sk == rtnl_dereference(mrt->mroute_sk));
1339 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1342 /* Manipulate the forwarding caches. These live
1343 * in a sort of kernel/user symbiosis.
1348 case MRT_ADD_MFC_PROXY:
1349 case MRT_DEL_MFC_PROXY:
1350 if (optlen != sizeof(mfc)) {
1354 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1359 parent = mfc.mfcc_parent;
1360 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1361 ret = ipmr_mfc_delete(mrt, &mfc, parent);
1363 ret = ipmr_mfc_add(net, mrt, &mfc,
1364 sk == rtnl_dereference(mrt->mroute_sk),
1367 /* Control PIM assert. */
1369 if (optlen != sizeof(val)) {
1373 if (get_user(val, (int __user *)optval)) {
1377 mrt->mroute_do_assert = val;
1380 if (!pimsm_enabled()) {
1384 if (optlen != sizeof(val)) {
1388 if (get_user(val, (int __user *)optval)) {
1394 if (val != mrt->mroute_do_pim) {
1395 mrt->mroute_do_pim = val;
1396 mrt->mroute_do_assert = val;
1400 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1404 if (optlen != sizeof(uval)) {
1408 if (get_user(uval, (u32 __user *)optval)) {
1413 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1416 mrt = ipmr_new_table(net, uval);
1420 raw_sk(sk)->ipmr_table = uval;
1423 /* Spurious command, or MRT_VERSION which you cannot set. */
1433 /* Getsock opt support for the multicast routing system. */
1434 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1438 struct net *net = sock_net(sk);
1439 struct mr_table *mrt;
1441 if (sk->sk_type != SOCK_RAW ||
1442 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1445 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1454 if (!pimsm_enabled())
1455 return -ENOPROTOOPT;
1456 val = mrt->mroute_do_pim;
1459 val = mrt->mroute_do_assert;
1462 return -ENOPROTOOPT;
1465 if (get_user(olr, optlen))
1467 olr = min_t(unsigned int, olr, sizeof(int));
1470 if (put_user(olr, optlen))
1472 if (copy_to_user(optval, &val, olr))
1477 /* The IP multicast ioctl support routines. */
1478 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1480 struct sioc_sg_req sr;
1481 struct sioc_vif_req vr;
1482 struct vif_device *vif;
1483 struct mfc_cache *c;
1484 struct net *net = sock_net(sk);
1485 struct mr_table *mrt;
1487 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1493 if (copy_from_user(&vr, arg, sizeof(vr)))
1495 if (vr.vifi >= mrt->maxvif)
1497 read_lock(&mrt_lock);
1498 vif = &mrt->vif_table[vr.vifi];
1499 if (VIF_EXISTS(mrt, vr.vifi)) {
1500 vr.icount = vif->pkt_in;
1501 vr.ocount = vif->pkt_out;
1502 vr.ibytes = vif->bytes_in;
1503 vr.obytes = vif->bytes_out;
1504 read_unlock(&mrt_lock);
1506 if (copy_to_user(arg, &vr, sizeof(vr)))
1510 read_unlock(&mrt_lock);
1511 return -EADDRNOTAVAIL;
1513 if (copy_from_user(&sr, arg, sizeof(sr)))
1517 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1519 sr.pktcnt = c->mfc_un.res.pkt;
1520 sr.bytecnt = c->mfc_un.res.bytes;
1521 sr.wrong_if = c->mfc_un.res.wrong_if;
1524 if (copy_to_user(arg, &sr, sizeof(sr)))
1529 return -EADDRNOTAVAIL;
1531 return -ENOIOCTLCMD;
1535 #ifdef CONFIG_COMPAT
1536 struct compat_sioc_sg_req {
1539 compat_ulong_t pktcnt;
1540 compat_ulong_t bytecnt;
1541 compat_ulong_t wrong_if;
1544 struct compat_sioc_vif_req {
1545 vifi_t vifi; /* Which iface */
1546 compat_ulong_t icount;
1547 compat_ulong_t ocount;
1548 compat_ulong_t ibytes;
1549 compat_ulong_t obytes;
1552 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1554 struct compat_sioc_sg_req sr;
1555 struct compat_sioc_vif_req vr;
1556 struct vif_device *vif;
1557 struct mfc_cache *c;
1558 struct net *net = sock_net(sk);
1559 struct mr_table *mrt;
1561 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1567 if (copy_from_user(&vr, arg, sizeof(vr)))
1569 if (vr.vifi >= mrt->maxvif)
1571 read_lock(&mrt_lock);
1572 vif = &mrt->vif_table[vr.vifi];
1573 if (VIF_EXISTS(mrt, vr.vifi)) {
1574 vr.icount = vif->pkt_in;
1575 vr.ocount = vif->pkt_out;
1576 vr.ibytes = vif->bytes_in;
1577 vr.obytes = vif->bytes_out;
1578 read_unlock(&mrt_lock);
1580 if (copy_to_user(arg, &vr, sizeof(vr)))
1584 read_unlock(&mrt_lock);
1585 return -EADDRNOTAVAIL;
1587 if (copy_from_user(&sr, arg, sizeof(sr)))
1591 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1593 sr.pktcnt = c->mfc_un.res.pkt;
1594 sr.bytecnt = c->mfc_un.res.bytes;
1595 sr.wrong_if = c->mfc_un.res.wrong_if;
1598 if (copy_to_user(arg, &sr, sizeof(sr)))
1603 return -EADDRNOTAVAIL;
1605 return -ENOIOCTLCMD;
1610 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1612 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1613 struct net *net = dev_net(dev);
1614 struct mr_table *mrt;
1615 struct vif_device *v;
1618 if (event != NETDEV_UNREGISTER)
1621 ipmr_for_each_table(mrt, net) {
1622 v = &mrt->vif_table[0];
1623 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1625 vif_delete(mrt, ct, 1, NULL);
1631 static struct notifier_block ip_mr_notifier = {
1632 .notifier_call = ipmr_device_event,
1635 /* Encapsulate a packet by attaching a valid IPIP header to it.
1636 * This avoids tunnel drivers and other mess and gives us the speed so
1637 * important for multicast video.
1639 static void ip_encap(struct net *net, struct sk_buff *skb,
1640 __be32 saddr, __be32 daddr)
1643 const struct iphdr *old_iph = ip_hdr(skb);
1645 skb_push(skb, sizeof(struct iphdr));
1646 skb->transport_header = skb->network_header;
1647 skb_reset_network_header(skb);
1651 iph->tos = old_iph->tos;
1652 iph->ttl = old_iph->ttl;
1656 iph->protocol = IPPROTO_IPIP;
1658 iph->tot_len = htons(skb->len);
1659 ip_select_ident(net, skb, NULL);
1662 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1666 static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1667 struct sk_buff *skb)
1669 struct ip_options *opt = &(IPCB(skb)->opt);
1671 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1672 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
1674 if (unlikely(opt->optlen))
1675 ip_forward_options(skb);
1677 return dst_output(net, sk, skb);
1680 /* Processing handlers for ipmr_forward */
1682 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1683 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1685 const struct iphdr *iph = ip_hdr(skb);
1686 struct vif_device *vif = &mrt->vif_table[vifi];
1687 struct net_device *dev;
1695 if (vif->flags & VIFF_REGISTER) {
1697 vif->bytes_out += skb->len;
1698 vif->dev->stats.tx_bytes += skb->len;
1699 vif->dev->stats.tx_packets++;
1700 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1704 if (vif->flags & VIFF_TUNNEL) {
1705 rt = ip_route_output_ports(net, &fl4, NULL,
1706 vif->remote, vif->local,
1709 RT_TOS(iph->tos), vif->link);
1712 encap = sizeof(struct iphdr);
1714 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1717 RT_TOS(iph->tos), vif->link);
1724 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1725 /* Do not fragment multicasts. Alas, IPv4 does not
1726 * allow to send ICMP, so that packets will disappear
1729 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1734 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1736 if (skb_cow(skb, encap)) {
1742 vif->bytes_out += skb->len;
1745 skb_dst_set(skb, &rt->dst);
1746 ip_decrease_ttl(ip_hdr(skb));
1748 /* FIXME: forward and output firewalls used to be called here.
1749 * What do we do with netfilter? -- RR
1751 if (vif->flags & VIFF_TUNNEL) {
1752 ip_encap(net, skb, vif->local, vif->remote);
1753 /* FIXME: extra output firewall step used to be here. --RR */
1754 vif->dev->stats.tx_packets++;
1755 vif->dev->stats.tx_bytes += skb->len;
1758 IPCB(skb)->flags |= IPSKB_FORWARDED;
1760 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1761 * not only before forwarding, but after forwarding on all output
1762 * interfaces. It is clear, if mrouter runs a multicasting
1763 * program, it should receive packets not depending to what interface
1764 * program is joined.
1765 * If we will not make it, the program will have to join on all
1766 * interfaces. On the other hand, multihoming host (or router, but
1767 * not mrouter) cannot join to more than one interface - it will
1768 * result in receiving multiple packets.
1770 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1771 net, NULL, skb, skb->dev, dev,
1772 ipmr_forward_finish);
1779 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1783 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1784 if (mrt->vif_table[ct].dev == dev)
1790 /* "local" means that we should preserve one skb (for local delivery) */
1791 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1792 struct sk_buff *skb, struct mfc_cache *cache,
1797 int true_vifi = ipmr_find_vif(mrt, skb->dev);
1799 vif = cache->mfc_parent;
1800 cache->mfc_un.res.pkt++;
1801 cache->mfc_un.res.bytes += skb->len;
1803 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1804 struct mfc_cache *cache_proxy;
1806 /* For an (*,G) entry, we only check that the incomming
1807 * interface is part of the static tree.
1809 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1811 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1815 /* Wrong interface: drop packet and (maybe) send PIM assert. */
1816 if (mrt->vif_table[vif].dev != skb->dev) {
1817 if (rt_is_output_route(skb_rtable(skb))) {
1818 /* It is our own packet, looped back.
1819 * Very complicated situation...
1821 * The best workaround until routing daemons will be
1822 * fixed is not to redistribute packet, if it was
1823 * send through wrong interface. It means, that
1824 * multicast applications WILL NOT work for
1825 * (S,G), which have default multicast route pointing
1826 * to wrong oif. In any case, it is not a good
1827 * idea to use multicasting applications on router.
1832 cache->mfc_un.res.wrong_if++;
1834 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1835 /* pimsm uses asserts, when switching from RPT to SPT,
1836 * so that we cannot check that packet arrived on an oif.
1837 * It is bad, but otherwise we would need to move pretty
1838 * large chunk of pimd to kernel. Ough... --ANK
1840 (mrt->mroute_do_pim ||
1841 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1843 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1844 cache->mfc_un.res.last_assert = jiffies;
1845 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1851 mrt->vif_table[vif].pkt_in++;
1852 mrt->vif_table[vif].bytes_in += skb->len;
1854 /* Forward the frame */
1855 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1856 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
1857 if (true_vifi >= 0 &&
1858 true_vifi != cache->mfc_parent &&
1860 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1861 /* It's an (*,*) entry and the packet is not coming from
1862 * the upstream: forward the packet to the upstream
1865 psend = cache->mfc_parent;
1870 for (ct = cache->mfc_un.res.maxvif - 1;
1871 ct >= cache->mfc_un.res.minvif; ct--) {
1872 /* For (*,G) entry, don't forward to the incoming interface */
1873 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1875 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1877 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1880 ipmr_queue_xmit(net, mrt, skb2, cache,
1889 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1892 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1894 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1904 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1906 struct rtable *rt = skb_rtable(skb);
1907 struct iphdr *iph = ip_hdr(skb);
1908 struct flowi4 fl4 = {
1909 .daddr = iph->daddr,
1910 .saddr = iph->saddr,
1911 .flowi4_tos = RT_TOS(iph->tos),
1912 .flowi4_oif = (rt_is_output_route(rt) ?
1913 skb->dev->ifindex : 0),
1914 .flowi4_iif = (rt_is_output_route(rt) ?
1917 .flowi4_mark = skb->mark,
1919 struct mr_table *mrt;
1922 err = ipmr_fib_lookup(net, &fl4, &mrt);
1924 return ERR_PTR(err);
1928 /* Multicast packets for forwarding arrive here
1929 * Called with rcu_read_lock();
1931 int ip_mr_input(struct sk_buff *skb)
1933 struct mfc_cache *cache;
1934 struct net *net = dev_net(skb->dev);
1935 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1936 struct mr_table *mrt;
1938 /* Packet is looped back after forward, it should not be
1939 * forwarded second time, but still can be delivered locally.
1941 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1944 mrt = ipmr_rt_fib_lookup(net, skb);
1947 return PTR_ERR(mrt);
1950 if (IPCB(skb)->opt.router_alert) {
1951 if (ip_call_ra_chain(skb))
1953 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1954 /* IGMPv1 (and broken IGMPv2 implementations sort of
1955 * Cisco IOS <= 11.2(8)) do not put router alert
1956 * option to IGMP packets destined to routable
1957 * groups. It is very bad, because it means
1958 * that we can forward NO IGMP messages.
1960 struct sock *mroute_sk;
1962 mroute_sk = rcu_dereference(mrt->mroute_sk);
1965 raw_rcv(mroute_sk, skb);
1971 /* already under rcu_read_lock() */
1972 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1974 int vif = ipmr_find_vif(mrt, skb->dev);
1977 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1981 /* No usable cache entry */
1986 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1987 ip_local_deliver(skb);
1993 read_lock(&mrt_lock);
1994 vif = ipmr_find_vif(mrt, skb->dev);
1996 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
1997 read_unlock(&mrt_lock);
2001 read_unlock(&mrt_lock);
2006 read_lock(&mrt_lock);
2007 ip_mr_forward(net, mrt, skb, cache, local);
2008 read_unlock(&mrt_lock);
2011 return ip_local_deliver(skb);
2017 return ip_local_deliver(skb);
2022 #ifdef CONFIG_IP_PIMSM_V1
2023 /* Handle IGMP messages of PIMv1 */
2024 int pim_rcv_v1(struct sk_buff *skb)
2026 struct igmphdr *pim;
2027 struct net *net = dev_net(skb->dev);
2028 struct mr_table *mrt;
2030 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2033 pim = igmp_hdr(skb);
2035 mrt = ipmr_rt_fib_lookup(net, skb);
2038 if (!mrt->mroute_do_pim ||
2039 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2042 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2050 #ifdef CONFIG_IP_PIMSM_V2
2051 static int pim_rcv(struct sk_buff *skb)
2053 struct pimreghdr *pim;
2054 struct net *net = dev_net(skb->dev);
2055 struct mr_table *mrt;
2057 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2060 pim = (struct pimreghdr *)skb_transport_header(skb);
2061 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2062 (pim->flags & PIM_NULL_REGISTER) ||
2063 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2064 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2067 mrt = ipmr_rt_fib_lookup(net, skb);
2070 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2078 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2079 struct mfc_cache *c, struct rtmsg *rtm)
2082 struct rtnexthop *nhp;
2083 struct nlattr *mp_attr;
2084 struct rta_mfc_stats mfcs;
2086 /* If cache is unresolved, don't try to parse IIF and OIF */
2087 if (c->mfc_parent >= MAXVIFS)
2090 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2091 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2094 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2097 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2098 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2099 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2100 nla_nest_cancel(skb, mp_attr);
2104 nhp->rtnh_flags = 0;
2105 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2106 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2107 nhp->rtnh_len = sizeof(*nhp);
2111 nla_nest_end(skb, mp_attr);
2113 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2114 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2115 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2116 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2119 rtm->rtm_type = RTN_MULTICAST;
2123 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2124 __be32 saddr, __be32 daddr,
2125 struct rtmsg *rtm, int nowait)
2127 struct mfc_cache *cache;
2128 struct mr_table *mrt;
2131 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2136 cache = ipmr_cache_find(mrt, saddr, daddr);
2137 if (!cache && skb->dev) {
2138 int vif = ipmr_find_vif(mrt, skb->dev);
2141 cache = ipmr_cache_find_any(mrt, daddr, vif);
2144 struct sk_buff *skb2;
2146 struct net_device *dev;
2155 read_lock(&mrt_lock);
2157 vif = ipmr_find_vif(mrt, dev);
2159 read_unlock(&mrt_lock);
2163 skb2 = skb_clone(skb, GFP_ATOMIC);
2165 read_unlock(&mrt_lock);
2170 skb_push(skb2, sizeof(struct iphdr));
2171 skb_reset_network_header(skb2);
2173 iph->ihl = sizeof(struct iphdr) >> 2;
2177 err = ipmr_cache_unresolved(mrt, vif, skb2);
2178 read_unlock(&mrt_lock);
2183 read_lock(&mrt_lock);
2184 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2185 read_unlock(&mrt_lock);
2190 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2191 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2194 struct nlmsghdr *nlh;
2198 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2202 rtm = nlmsg_data(nlh);
2203 rtm->rtm_family = RTNL_FAMILY_IPMR;
2204 rtm->rtm_dst_len = 32;
2205 rtm->rtm_src_len = 32;
2207 rtm->rtm_table = mrt->id;
2208 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2209 goto nla_put_failure;
2210 rtm->rtm_type = RTN_MULTICAST;
2211 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2212 if (c->mfc_flags & MFC_STATIC)
2213 rtm->rtm_protocol = RTPROT_STATIC;
2215 rtm->rtm_protocol = RTPROT_MROUTED;
2218 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2219 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2220 goto nla_put_failure;
2221 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2222 /* do not break the dump if cache is unresolved */
2223 if (err < 0 && err != -ENOENT)
2224 goto nla_put_failure;
2226 nlmsg_end(skb, nlh);
2230 nlmsg_cancel(skb, nlh);
2234 static size_t mroute_msgsize(bool unresolved, int maxvif)
2237 NLMSG_ALIGN(sizeof(struct rtmsg))
2238 + nla_total_size(4) /* RTA_TABLE */
2239 + nla_total_size(4) /* RTA_SRC */
2240 + nla_total_size(4) /* RTA_DST */
2245 + nla_total_size(4) /* RTA_IIF */
2246 + nla_total_size(0) /* RTA_MULTIPATH */
2247 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2249 + nla_total_size(sizeof(struct rta_mfc_stats))
2255 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2258 struct net *net = read_pnet(&mrt->net);
2259 struct sk_buff *skb;
2262 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2267 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2271 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2277 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2280 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2282 struct net *net = sock_net(skb->sk);
2283 struct mr_table *mrt;
2284 struct mfc_cache *mfc;
2285 unsigned int t = 0, s_t;
2286 unsigned int h = 0, s_h;
2287 unsigned int e = 0, s_e;
2294 ipmr_for_each_table(mrt, net) {
2299 for (h = s_h; h < MFC_LINES; h++) {
2300 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2303 if (ipmr_fill_mroute(mrt, skb,
2304 NETLINK_CB(cb->skb).portid,
2314 spin_lock_bh(&mfc_unres_lock);
2315 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2318 if (ipmr_fill_mroute(mrt, skb,
2319 NETLINK_CB(cb->skb).portid,
2323 spin_unlock_bh(&mfc_unres_lock);
2329 spin_unlock_bh(&mfc_unres_lock);
2345 #ifdef CONFIG_PROC_FS
2346 /* The /proc interfaces to multicast routing :
2347 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2349 struct ipmr_vif_iter {
2350 struct seq_net_private p;
2351 struct mr_table *mrt;
2355 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2356 struct ipmr_vif_iter *iter,
2359 struct mr_table *mrt = iter->mrt;
2361 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2362 if (!VIF_EXISTS(mrt, iter->ct))
2365 return &mrt->vif_table[iter->ct];
2370 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2371 __acquires(mrt_lock)
2373 struct ipmr_vif_iter *iter = seq->private;
2374 struct net *net = seq_file_net(seq);
2375 struct mr_table *mrt;
2377 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2379 return ERR_PTR(-ENOENT);
2383 read_lock(&mrt_lock);
2384 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2388 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2390 struct ipmr_vif_iter *iter = seq->private;
2391 struct net *net = seq_file_net(seq);
2392 struct mr_table *mrt = iter->mrt;
2395 if (v == SEQ_START_TOKEN)
2396 return ipmr_vif_seq_idx(net, iter, 0);
2398 while (++iter->ct < mrt->maxvif) {
2399 if (!VIF_EXISTS(mrt, iter->ct))
2401 return &mrt->vif_table[iter->ct];
2406 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2407 __releases(mrt_lock)
2409 read_unlock(&mrt_lock);
2412 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2414 struct ipmr_vif_iter *iter = seq->private;
2415 struct mr_table *mrt = iter->mrt;
2417 if (v == SEQ_START_TOKEN) {
2419 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2421 const struct vif_device *vif = v;
2422 const char *name = vif->dev ? vif->dev->name : "none";
2425 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2426 vif - mrt->vif_table,
2427 name, vif->bytes_in, vif->pkt_in,
2428 vif->bytes_out, vif->pkt_out,
2429 vif->flags, vif->local, vif->remote);
2434 static const struct seq_operations ipmr_vif_seq_ops = {
2435 .start = ipmr_vif_seq_start,
2436 .next = ipmr_vif_seq_next,
2437 .stop = ipmr_vif_seq_stop,
2438 .show = ipmr_vif_seq_show,
2441 static int ipmr_vif_open(struct inode *inode, struct file *file)
2443 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2444 sizeof(struct ipmr_vif_iter));
2447 static const struct file_operations ipmr_vif_fops = {
2448 .owner = THIS_MODULE,
2449 .open = ipmr_vif_open,
2451 .llseek = seq_lseek,
2452 .release = seq_release_net,
2455 struct ipmr_mfc_iter {
2456 struct seq_net_private p;
2457 struct mr_table *mrt;
2458 struct list_head *cache;
2463 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2464 struct ipmr_mfc_iter *it, loff_t pos)
2466 struct mr_table *mrt = it->mrt;
2467 struct mfc_cache *mfc;
2470 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2471 it->cache = &mrt->mfc_cache_array[it->ct];
2472 list_for_each_entry_rcu(mfc, it->cache, list)
2478 spin_lock_bh(&mfc_unres_lock);
2479 it->cache = &mrt->mfc_unres_queue;
2480 list_for_each_entry(mfc, it->cache, list)
2483 spin_unlock_bh(&mfc_unres_lock);
2490 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2492 struct ipmr_mfc_iter *it = seq->private;
2493 struct net *net = seq_file_net(seq);
2494 struct mr_table *mrt;
2496 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2498 return ERR_PTR(-ENOENT);
2503 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2507 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2509 struct mfc_cache *mfc = v;
2510 struct ipmr_mfc_iter *it = seq->private;
2511 struct net *net = seq_file_net(seq);
2512 struct mr_table *mrt = it->mrt;
2516 if (v == SEQ_START_TOKEN)
2517 return ipmr_mfc_seq_idx(net, seq->private, 0);
2519 if (mfc->list.next != it->cache)
2520 return list_entry(mfc->list.next, struct mfc_cache, list);
2522 if (it->cache == &mrt->mfc_unres_queue)
2525 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2527 while (++it->ct < MFC_LINES) {
2528 it->cache = &mrt->mfc_cache_array[it->ct];
2529 if (list_empty(it->cache))
2531 return list_first_entry(it->cache, struct mfc_cache, list);
2534 /* exhausted cache_array, show unresolved */
2536 it->cache = &mrt->mfc_unres_queue;
2539 spin_lock_bh(&mfc_unres_lock);
2540 if (!list_empty(it->cache))
2541 return list_first_entry(it->cache, struct mfc_cache, list);
2544 spin_unlock_bh(&mfc_unres_lock);
2550 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2552 struct ipmr_mfc_iter *it = seq->private;
2553 struct mr_table *mrt = it->mrt;
2555 if (it->cache == &mrt->mfc_unres_queue)
2556 spin_unlock_bh(&mfc_unres_lock);
2557 else if (it->cache == &mrt->mfc_cache_array[it->ct])
2561 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2565 if (v == SEQ_START_TOKEN) {
2567 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2569 const struct mfc_cache *mfc = v;
2570 const struct ipmr_mfc_iter *it = seq->private;
2571 const struct mr_table *mrt = it->mrt;
2573 seq_printf(seq, "%08X %08X %-3hd",
2574 (__force u32) mfc->mfc_mcastgrp,
2575 (__force u32) mfc->mfc_origin,
2578 if (it->cache != &mrt->mfc_unres_queue) {
2579 seq_printf(seq, " %8lu %8lu %8lu",
2580 mfc->mfc_un.res.pkt,
2581 mfc->mfc_un.res.bytes,
2582 mfc->mfc_un.res.wrong_if);
2583 for (n = mfc->mfc_un.res.minvif;
2584 n < mfc->mfc_un.res.maxvif; n++) {
2585 if (VIF_EXISTS(mrt, n) &&
2586 mfc->mfc_un.res.ttls[n] < 255)
2589 n, mfc->mfc_un.res.ttls[n]);
2592 /* unresolved mfc_caches don't contain
2593 * pkt, bytes and wrong_if values
2595 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2597 seq_putc(seq, '\n');
2602 static const struct seq_operations ipmr_mfc_seq_ops = {
2603 .start = ipmr_mfc_seq_start,
2604 .next = ipmr_mfc_seq_next,
2605 .stop = ipmr_mfc_seq_stop,
2606 .show = ipmr_mfc_seq_show,
2609 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2611 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2612 sizeof(struct ipmr_mfc_iter));
2615 static const struct file_operations ipmr_mfc_fops = {
2616 .owner = THIS_MODULE,
2617 .open = ipmr_mfc_open,
2619 .llseek = seq_lseek,
2620 .release = seq_release_net,
2624 #ifdef CONFIG_IP_PIMSM_V2
2625 static const struct net_protocol pim_protocol = {
2631 /* Setup for IP multicast routing */
2632 static int __net_init ipmr_net_init(struct net *net)
2636 err = ipmr_rules_init(net);
2640 #ifdef CONFIG_PROC_FS
2642 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
2644 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
2645 goto proc_cache_fail;
2649 #ifdef CONFIG_PROC_FS
2651 remove_proc_entry("ip_mr_vif", net->proc_net);
2653 ipmr_rules_exit(net);
2659 static void __net_exit ipmr_net_exit(struct net *net)
2661 #ifdef CONFIG_PROC_FS
2662 remove_proc_entry("ip_mr_cache", net->proc_net);
2663 remove_proc_entry("ip_mr_vif", net->proc_net);
2665 ipmr_rules_exit(net);
2668 static struct pernet_operations ipmr_net_ops = {
2669 .init = ipmr_net_init,
2670 .exit = ipmr_net_exit,
2673 int __init ip_mr_init(void)
2677 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2678 sizeof(struct mfc_cache),
2679 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2682 err = register_pernet_subsys(&ipmr_net_ops);
2684 goto reg_pernet_fail;
2686 err = register_netdevice_notifier(&ip_mr_notifier);
2688 goto reg_notif_fail;
2689 #ifdef CONFIG_IP_PIMSM_V2
2690 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2691 pr_err("%s: can't add PIM protocol\n", __func__);
2693 goto add_proto_fail;
2696 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2697 NULL, ipmr_rtm_dumproute, NULL);
2700 #ifdef CONFIG_IP_PIMSM_V2
2702 unregister_netdevice_notifier(&ip_mr_notifier);
2705 unregister_pernet_subsys(&ipmr_net_ops);
2707 kmem_cache_destroy(mrt_cachep);