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>
70 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
71 #define CONFIG_IP_PIMSM 1
75 struct list_head list;
80 struct sock __rcu *mroute_sk;
81 struct timer_list ipmr_expire_timer;
82 struct list_head mfc_unres_queue;
83 struct list_head mfc_cache_array[MFC_LINES];
84 struct vif_device vif_table[MAXVIFS];
86 atomic_t cache_resolve_queue_len;
87 bool mroute_do_assert;
89 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
90 int mroute_reg_vif_num;
95 struct fib_rule common;
102 /* Big lock, protecting vif table, mrt cache and mroute socket state.
103 * Note that the changes are semaphored via rtnl_lock.
106 static DEFINE_RWLOCK(mrt_lock);
109 * Multicast router control variables
112 #define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
114 /* Special spinlock for queue of unresolved entries */
115 static DEFINE_SPINLOCK(mfc_unres_lock);
117 /* We return to original Alan's scheme. Hash table of resolved
118 * entries is changed only in process context and protected
119 * with weak lock mrt_lock. Queue of unresolved entries is protected
120 * with strong spinlock mfc_unres_lock.
122 * In this case data path is free of exclusive locks at all.
125 static struct kmem_cache *mrt_cachep __read_mostly;
127 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
128 static void ipmr_free_table(struct mr_table *mrt);
130 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
131 struct sk_buff *skb, struct mfc_cache *cache,
133 static int ipmr_cache_report(struct mr_table *mrt,
134 struct sk_buff *pkt, vifi_t vifi, int assert);
135 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
136 struct mfc_cache *c, struct rtmsg *rtm);
137 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
139 static void mroute_clean_tables(struct mr_table *mrt);
140 static void ipmr_expire_process(unsigned long arg);
142 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
143 #define ipmr_for_each_table(mrt, net) \
144 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
146 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
148 struct mr_table *mrt;
150 ipmr_for_each_table(mrt, net) {
157 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
158 struct mr_table **mrt)
160 struct ipmr_result res;
161 struct fib_lookup_arg arg = { .result = &res, };
164 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
165 flowi4_to_flowi(flp4), 0, &arg);
172 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
173 int flags, struct fib_lookup_arg *arg)
175 struct ipmr_result *res = arg->result;
176 struct mr_table *mrt;
178 switch (rule->action) {
181 case FR_ACT_UNREACHABLE:
183 case FR_ACT_PROHIBIT:
185 case FR_ACT_BLACKHOLE:
190 mrt = ipmr_get_table(rule->fr_net, rule->table);
197 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
202 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
206 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
207 struct fib_rule_hdr *frh, struct nlattr **tb)
212 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
218 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
219 struct fib_rule_hdr *frh)
227 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
228 .family = RTNL_FAMILY_IPMR,
229 .rule_size = sizeof(struct ipmr_rule),
230 .addr_size = sizeof(u32),
231 .action = ipmr_rule_action,
232 .match = ipmr_rule_match,
233 .configure = ipmr_rule_configure,
234 .compare = ipmr_rule_compare,
235 .default_pref = fib_default_rule_pref,
236 .fill = ipmr_rule_fill,
237 .nlgroup = RTNLGRP_IPV4_RULE,
238 .policy = ipmr_rule_policy,
239 .owner = THIS_MODULE,
242 static int __net_init ipmr_rules_init(struct net *net)
244 struct fib_rules_ops *ops;
245 struct mr_table *mrt;
248 ops = fib_rules_register(&ipmr_rules_ops_template, net);
252 INIT_LIST_HEAD(&net->ipv4.mr_tables);
254 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
260 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
264 net->ipv4.mr_rules_ops = ops;
270 fib_rules_unregister(ops);
274 static void __net_exit ipmr_rules_exit(struct net *net)
276 struct mr_table *mrt, *next;
278 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
279 list_del(&mrt->list);
280 ipmr_free_table(mrt);
282 fib_rules_unregister(net->ipv4.mr_rules_ops);
285 #define ipmr_for_each_table(mrt, net) \
286 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
288 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
290 return net->ipv4.mrt;
293 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
294 struct mr_table **mrt)
296 *mrt = net->ipv4.mrt;
300 static int __net_init ipmr_rules_init(struct net *net)
302 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
303 return net->ipv4.mrt ? 0 : -ENOMEM;
306 static void __net_exit ipmr_rules_exit(struct net *net)
308 ipmr_free_table(net->ipv4.mrt);
312 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
314 struct mr_table *mrt;
317 mrt = ipmr_get_table(net, id);
321 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
324 write_pnet(&mrt->net, net);
327 /* Forwarding cache */
328 for (i = 0; i < MFC_LINES; i++)
329 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
331 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
333 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
336 #ifdef CONFIG_IP_PIMSM
337 mrt->mroute_reg_vif_num = -1;
339 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
340 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
345 static void ipmr_free_table(struct mr_table *mrt)
347 del_timer_sync(&mrt->ipmr_expire_timer);
348 mroute_clean_tables(mrt);
352 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
354 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
356 struct net *net = dev_net(dev);
360 dev = __dev_get_by_name(net, "tunl0");
362 const struct net_device_ops *ops = dev->netdev_ops;
364 struct ip_tunnel_parm p;
366 memset(&p, 0, sizeof(p));
367 p.iph.daddr = v->vifc_rmt_addr.s_addr;
368 p.iph.saddr = v->vifc_lcl_addr.s_addr;
371 p.iph.protocol = IPPROTO_IPIP;
372 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
373 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
375 if (ops->ndo_do_ioctl) {
376 mm_segment_t oldfs = get_fs();
379 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
386 struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
388 struct net_device *dev;
390 dev = __dev_get_by_name(net, "tunl0");
393 const struct net_device_ops *ops = dev->netdev_ops;
396 struct ip_tunnel_parm p;
397 struct in_device *in_dev;
399 memset(&p, 0, sizeof(p));
400 p.iph.daddr = v->vifc_rmt_addr.s_addr;
401 p.iph.saddr = v->vifc_lcl_addr.s_addr;
404 p.iph.protocol = IPPROTO_IPIP;
405 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
406 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
408 if (ops->ndo_do_ioctl) {
409 mm_segment_t oldfs = get_fs();
412 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
420 (dev = __dev_get_by_name(net, p.name)) != NULL) {
421 dev->flags |= IFF_MULTICAST;
423 in_dev = __in_dev_get_rtnl(dev);
427 ipv4_devconf_setall(in_dev);
428 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
438 /* allow the register to be completed before unregistering. */
442 unregister_netdevice(dev);
446 #ifdef CONFIG_IP_PIMSM
448 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
450 struct net *net = dev_net(dev);
451 struct mr_table *mrt;
452 struct flowi4 fl4 = {
453 .flowi4_oif = dev->ifindex,
454 .flowi4_iif = skb->skb_iif,
455 .flowi4_mark = skb->mark,
459 err = ipmr_fib_lookup(net, &fl4, &mrt);
465 read_lock(&mrt_lock);
466 dev->stats.tx_bytes += skb->len;
467 dev->stats.tx_packets++;
468 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
469 read_unlock(&mrt_lock);
474 static const struct net_device_ops reg_vif_netdev_ops = {
475 .ndo_start_xmit = reg_vif_xmit,
478 static void reg_vif_setup(struct net_device *dev)
480 dev->type = ARPHRD_PIMREG;
481 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
482 dev->flags = IFF_NOARP;
483 dev->netdev_ops = ®_vif_netdev_ops,
484 dev->destructor = free_netdev;
485 dev->features |= NETIF_F_NETNS_LOCAL;
488 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
490 struct net_device *dev;
491 struct in_device *in_dev;
494 if (mrt->id == RT_TABLE_DEFAULT)
495 sprintf(name, "pimreg");
497 sprintf(name, "pimreg%u", mrt->id);
499 dev = alloc_netdev(0, name, reg_vif_setup);
504 dev_net_set(dev, net);
506 if (register_netdevice(dev)) {
513 in_dev = __in_dev_get_rcu(dev);
519 ipv4_devconf_setall(in_dev);
520 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
531 /* allow the register to be completed before unregistering. */
535 unregister_netdevice(dev);
541 * vif_delete - Delete a VIF entry
542 * @notify: Set to 1, if the caller is a notifier_call
545 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
546 struct list_head *head)
548 struct vif_device *v;
549 struct net_device *dev;
550 struct in_device *in_dev;
552 if (vifi < 0 || vifi >= mrt->maxvif)
553 return -EADDRNOTAVAIL;
555 v = &mrt->vif_table[vifi];
557 write_lock_bh(&mrt_lock);
562 write_unlock_bh(&mrt_lock);
563 return -EADDRNOTAVAIL;
566 #ifdef CONFIG_IP_PIMSM
567 if (vifi == mrt->mroute_reg_vif_num)
568 mrt->mroute_reg_vif_num = -1;
571 if (vifi + 1 == mrt->maxvif) {
574 for (tmp = vifi - 1; tmp >= 0; tmp--) {
575 if (VIF_EXISTS(mrt, tmp))
581 write_unlock_bh(&mrt_lock);
583 dev_set_allmulti(dev, -1);
585 in_dev = __in_dev_get_rtnl(dev);
587 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
588 inet_netconf_notify_devconf(dev_net(dev),
589 NETCONFA_MC_FORWARDING,
590 dev->ifindex, &in_dev->cnf);
591 ip_rt_multicast_event(in_dev);
594 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
595 unregister_netdevice_queue(dev, head);
601 static void ipmr_cache_free_rcu(struct rcu_head *head)
603 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
605 kmem_cache_free(mrt_cachep, c);
608 static inline void ipmr_cache_free(struct mfc_cache *c)
610 call_rcu(&c->rcu, ipmr_cache_free_rcu);
613 /* Destroy an unresolved cache entry, killing queued skbs
614 * and reporting error to netlink readers.
617 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
619 struct net *net = read_pnet(&mrt->net);
623 atomic_dec(&mrt->cache_resolve_queue_len);
625 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
626 if (ip_hdr(skb)->version == 0) {
627 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
628 nlh->nlmsg_type = NLMSG_ERROR;
629 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
630 skb_trim(skb, nlh->nlmsg_len);
632 e->error = -ETIMEDOUT;
633 memset(&e->msg, 0, sizeof(e->msg));
635 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
645 /* Timer process for the unresolved queue. */
647 static void ipmr_expire_process(unsigned long arg)
649 struct mr_table *mrt = (struct mr_table *)arg;
651 unsigned long expires;
652 struct mfc_cache *c, *next;
654 if (!spin_trylock(&mfc_unres_lock)) {
655 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
659 if (list_empty(&mrt->mfc_unres_queue))
665 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
666 if (time_after(c->mfc_un.unres.expires, now)) {
667 unsigned long interval = c->mfc_un.unres.expires - now;
668 if (interval < expires)
674 mroute_netlink_event(mrt, c, RTM_DELROUTE);
675 ipmr_destroy_unres(mrt, c);
678 if (!list_empty(&mrt->mfc_unres_queue))
679 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
682 spin_unlock(&mfc_unres_lock);
685 /* Fill oifs list. It is called under write locked mrt_lock. */
687 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
692 cache->mfc_un.res.minvif = MAXVIFS;
693 cache->mfc_un.res.maxvif = 0;
694 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
696 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
697 if (VIF_EXISTS(mrt, vifi) &&
698 ttls[vifi] && ttls[vifi] < 255) {
699 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
700 if (cache->mfc_un.res.minvif > vifi)
701 cache->mfc_un.res.minvif = vifi;
702 if (cache->mfc_un.res.maxvif <= vifi)
703 cache->mfc_un.res.maxvif = vifi + 1;
708 static int vif_add(struct net *net, struct mr_table *mrt,
709 struct vifctl *vifc, int mrtsock)
711 int vifi = vifc->vifc_vifi;
712 struct vif_device *v = &mrt->vif_table[vifi];
713 struct net_device *dev;
714 struct in_device *in_dev;
718 if (VIF_EXISTS(mrt, vifi))
721 switch (vifc->vifc_flags) {
722 #ifdef CONFIG_IP_PIMSM
725 * Special Purpose VIF in PIM
726 * All the packets will be sent to the daemon
728 if (mrt->mroute_reg_vif_num >= 0)
730 dev = ipmr_reg_vif(net, mrt);
733 err = dev_set_allmulti(dev, 1);
735 unregister_netdevice(dev);
742 dev = ipmr_new_tunnel(net, vifc);
745 err = dev_set_allmulti(dev, 1);
747 ipmr_del_tunnel(dev, vifc);
753 case VIFF_USE_IFINDEX:
755 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
756 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
757 if (dev && __in_dev_get_rtnl(dev) == NULL) {
759 return -EADDRNOTAVAIL;
762 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
765 return -EADDRNOTAVAIL;
766 err = dev_set_allmulti(dev, 1);
776 in_dev = __in_dev_get_rtnl(dev);
779 return -EADDRNOTAVAIL;
781 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
782 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
784 ip_rt_multicast_event(in_dev);
786 /* Fill in the VIF structures */
788 v->rate_limit = vifc->vifc_rate_limit;
789 v->local = vifc->vifc_lcl_addr.s_addr;
790 v->remote = vifc->vifc_rmt_addr.s_addr;
791 v->flags = vifc->vifc_flags;
793 v->flags |= VIFF_STATIC;
794 v->threshold = vifc->vifc_threshold;
799 v->link = dev->ifindex;
800 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
801 v->link = dev->iflink;
803 /* And finish update writing critical data */
804 write_lock_bh(&mrt_lock);
806 #ifdef CONFIG_IP_PIMSM
807 if (v->flags & VIFF_REGISTER)
808 mrt->mroute_reg_vif_num = vifi;
810 if (vifi+1 > mrt->maxvif)
811 mrt->maxvif = vifi+1;
812 write_unlock_bh(&mrt_lock);
816 /* called with rcu_read_lock() */
817 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
821 int line = MFC_HASH(mcastgrp, origin);
824 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
825 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
831 /* Look for a (*,*,oif) entry */
832 static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
835 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
838 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
839 if (c->mfc_origin == htonl(INADDR_ANY) &&
840 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
841 c->mfc_un.res.ttls[vifi] < 255)
847 /* Look for a (*,G) entry */
848 static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
849 __be32 mcastgrp, int vifi)
851 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
852 struct mfc_cache *c, *proxy;
854 if (mcastgrp == htonl(INADDR_ANY))
857 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
858 if (c->mfc_origin == htonl(INADDR_ANY) &&
859 c->mfc_mcastgrp == mcastgrp) {
860 if (c->mfc_un.res.ttls[vifi] < 255)
863 /* It's ok if the vifi is part of the static tree */
864 proxy = ipmr_cache_find_any_parent(mrt,
866 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
871 return ipmr_cache_find_any_parent(mrt, vifi);
875 * Allocate a multicast cache entry
877 static struct mfc_cache *ipmr_cache_alloc(void)
879 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
882 c->mfc_un.res.minvif = MAXVIFS;
886 static struct mfc_cache *ipmr_cache_alloc_unres(void)
888 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
891 skb_queue_head_init(&c->mfc_un.unres.unresolved);
892 c->mfc_un.unres.expires = jiffies + 10*HZ;
898 * A cache entry has gone into a resolved state from queued
901 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
902 struct mfc_cache *uc, struct mfc_cache *c)
907 /* Play the pending entries through our router */
909 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
910 if (ip_hdr(skb)->version == 0) {
911 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
913 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
914 nlh->nlmsg_len = skb_tail_pointer(skb) -
917 nlh->nlmsg_type = NLMSG_ERROR;
918 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
919 skb_trim(skb, nlh->nlmsg_len);
921 e->error = -EMSGSIZE;
922 memset(&e->msg, 0, sizeof(e->msg));
925 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
927 ip_mr_forward(net, mrt, skb, c, 0);
933 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
934 * expects the following bizarre scheme.
936 * Called under mrt_lock.
939 static int ipmr_cache_report(struct mr_table *mrt,
940 struct sk_buff *pkt, vifi_t vifi, int assert)
943 const int ihl = ip_hdrlen(pkt);
944 struct igmphdr *igmp;
946 struct sock *mroute_sk;
948 unsigned long tail_offset;
950 #ifdef CONFIG_IP_PIMSM
951 if (assert == IGMPMSG_WHOLEPKT)
952 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
955 skb = alloc_skb(128, GFP_ATOMIC);
960 #ifdef CONFIG_IP_PIMSM
961 if (assert == IGMPMSG_WHOLEPKT) {
962 /* Ugly, but we have no choice with this interface.
963 * Duplicate old header, fix ihl, length etc.
964 * And all this only to mangle msg->im_msgtype and
965 * to set msg->im_mbz to "mbz" :-)
967 skb_push(skb, sizeof(struct iphdr));
968 skb_reset_network_header(skb);
969 skb_reset_transport_header(skb);
970 msg = (struct igmpmsg *)skb_network_header(skb);
971 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
972 msg->im_msgtype = IGMPMSG_WHOLEPKT;
974 msg->im_vif = mrt->mroute_reg_vif_num;
975 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
976 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
977 sizeof(struct iphdr));
982 /* Copy the IP header */
984 tail_offset = skb_tail_offset(skb);
985 if (tail_offset > 0xffff) {
989 skb_set_network_header(skb, tail_offset);
991 skb_copy_to_linear_data(skb, pkt->data, ihl);
992 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
993 msg = (struct igmpmsg *)skb_network_header(skb);
995 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
999 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
1001 msg->im_msgtype = assert;
1003 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1004 skb->transport_header = skb->network_header;
1008 mroute_sk = rcu_dereference(mrt->mroute_sk);
1009 if (mroute_sk == NULL) {
1015 /* Deliver to mrouted */
1017 ret = sock_queue_rcv_skb(mroute_sk, skb);
1020 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1028 * Queue a packet for resolution. It gets locked cache entry!
1032 ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
1036 struct mfc_cache *c;
1037 const struct iphdr *iph = ip_hdr(skb);
1039 spin_lock_bh(&mfc_unres_lock);
1040 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
1041 if (c->mfc_mcastgrp == iph->daddr &&
1042 c->mfc_origin == iph->saddr) {
1049 /* 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 */
1062 c->mfc_origin = iph->saddr;
1063 c->mfc_mcastgrp = iph->daddr;
1065 /* Reflect first query at mrouted. */
1067 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1069 /* If the report failed throw the cache entry
1072 spin_unlock_bh(&mfc_unres_lock);
1079 atomic_inc(&mrt->cache_resolve_queue_len);
1080 list_add(&c->list, &mrt->mfc_unres_queue);
1081 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1083 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1084 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1087 /* See if we can append the packet */
1089 if (c->mfc_un.unres.unresolved.qlen > 3) {
1093 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1097 spin_unlock_bh(&mfc_unres_lock);
1102 * MFC cache manipulation by user space mroute daemon
1105 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1108 struct mfc_cache *c, *next;
1110 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1112 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
1113 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1114 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1115 (parent == -1 || parent == c->mfc_parent)) {
1116 list_del_rcu(&c->list);
1117 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1125 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1126 struct mfcctl *mfc, int mrtsock, int parent)
1130 struct mfc_cache *uc, *c;
1132 if (mfc->mfcc_parent >= MAXVIFS)
1135 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
1137 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
1138 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
1139 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1140 (parent == -1 || parent == c->mfc_parent)) {
1147 write_lock_bh(&mrt_lock);
1148 c->mfc_parent = mfc->mfcc_parent;
1149 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1151 c->mfc_flags |= MFC_STATIC;
1152 write_unlock_bh(&mrt_lock);
1153 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1157 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1158 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1161 c = ipmr_cache_alloc();
1165 c->mfc_origin = mfc->mfcc_origin.s_addr;
1166 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1167 c->mfc_parent = mfc->mfcc_parent;
1168 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1170 c->mfc_flags |= MFC_STATIC;
1172 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
1175 * Check to see if we resolved a queued list. If so we
1176 * need to send on the frames and tidy up.
1179 spin_lock_bh(&mfc_unres_lock);
1180 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1181 if (uc->mfc_origin == c->mfc_origin &&
1182 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1183 list_del(&uc->list);
1184 atomic_dec(&mrt->cache_resolve_queue_len);
1189 if (list_empty(&mrt->mfc_unres_queue))
1190 del_timer(&mrt->ipmr_expire_timer);
1191 spin_unlock_bh(&mfc_unres_lock);
1194 ipmr_cache_resolve(net, mrt, uc, c);
1195 ipmr_cache_free(uc);
1197 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1202 * Close the multicast socket, and clear the vif tables etc
1205 static void mroute_clean_tables(struct mr_table *mrt)
1209 struct mfc_cache *c, *next;
1211 /* Shut down all active vif entries */
1213 for (i = 0; i < mrt->maxvif; i++) {
1214 if (!(mrt->vif_table[i].flags & VIFF_STATIC))
1215 vif_delete(mrt, i, 0, &list);
1217 unregister_netdevice_many(&list);
1219 /* Wipe the cache */
1221 for (i = 0; i < MFC_LINES; i++) {
1222 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
1223 if (c->mfc_flags & MFC_STATIC)
1225 list_del_rcu(&c->list);
1226 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1231 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1232 spin_lock_bh(&mfc_unres_lock);
1233 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
1235 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1236 ipmr_destroy_unres(mrt, c);
1238 spin_unlock_bh(&mfc_unres_lock);
1242 /* called from ip_ra_control(), before an RCU grace period,
1243 * we dont need to call synchronize_rcu() here
1245 static void mrtsock_destruct(struct sock *sk)
1247 struct net *net = sock_net(sk);
1248 struct mr_table *mrt;
1251 ipmr_for_each_table(mrt, net) {
1252 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1253 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1254 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1255 NETCONFA_IFINDEX_ALL,
1256 net->ipv4.devconf_all);
1257 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1258 mroute_clean_tables(mrt);
1265 * Socket options and virtual interface manipulation. The whole
1266 * virtual interface system is a complete heap, but unfortunately
1267 * that's how BSD mrouted happens to think. Maybe one day with a proper
1268 * MOSPF/PIM router set up we can clean this up.
1271 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
1273 int ret, parent = 0;
1276 struct net *net = sock_net(sk);
1277 struct mr_table *mrt;
1279 if (sk->sk_type != SOCK_RAW ||
1280 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1283 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1287 if (optname != MRT_INIT) {
1288 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1289 !ns_capable(net->user_ns, CAP_NET_ADMIN))
1295 if (optlen != sizeof(int))
1299 if (rtnl_dereference(mrt->mroute_sk)) {
1304 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1306 rcu_assign_pointer(mrt->mroute_sk, sk);
1307 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1308 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1309 NETCONFA_IFINDEX_ALL,
1310 net->ipv4.devconf_all);
1315 if (sk != rcu_access_pointer(mrt->mroute_sk))
1317 return ip_ra_control(sk, 0, NULL);
1320 if (optlen != sizeof(vif))
1322 if (copy_from_user(&vif, optval, sizeof(vif)))
1324 if (vif.vifc_vifi >= MAXVIFS)
1327 if (optname == MRT_ADD_VIF) {
1328 ret = vif_add(net, mrt, &vif,
1329 sk == rtnl_dereference(mrt->mroute_sk));
1331 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1337 * Manipulate the forwarding caches. These live
1338 * in a sort of kernel/user symbiosis.
1343 case MRT_ADD_MFC_PROXY:
1344 case MRT_DEL_MFC_PROXY:
1345 if (optlen != sizeof(mfc))
1347 if (copy_from_user(&mfc, optval, sizeof(mfc)))
1350 parent = mfc.mfcc_parent;
1352 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1353 ret = ipmr_mfc_delete(mrt, &mfc, parent);
1355 ret = ipmr_mfc_add(net, mrt, &mfc,
1356 sk == rtnl_dereference(mrt->mroute_sk),
1361 * Control PIM assert.
1366 if (optlen != sizeof(v))
1368 if (get_user(v, (int __user *)optval))
1370 mrt->mroute_do_assert = v;
1373 #ifdef CONFIG_IP_PIMSM
1378 if (optlen != sizeof(v))
1380 if (get_user(v, (int __user *)optval))
1386 if (v != mrt->mroute_do_pim) {
1387 mrt->mroute_do_pim = v;
1388 mrt->mroute_do_assert = v;
1394 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1399 if (optlen != sizeof(u32))
1401 if (get_user(v, (u32 __user *)optval))
1404 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1405 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1410 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1413 if (!ipmr_new_table(net, v))
1416 raw_sk(sk)->ipmr_table = v;
1423 * Spurious command, or MRT_VERSION which you cannot
1427 return -ENOPROTOOPT;
1432 * Getsock opt support for the multicast routing system.
1435 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1439 struct net *net = sock_net(sk);
1440 struct mr_table *mrt;
1442 if (sk->sk_type != SOCK_RAW ||
1443 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1446 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1450 if (optname != MRT_VERSION &&
1451 #ifdef CONFIG_IP_PIMSM
1452 optname != MRT_PIM &&
1454 optname != MRT_ASSERT)
1455 return -ENOPROTOOPT;
1457 if (get_user(olr, optlen))
1460 olr = min_t(unsigned int, olr, sizeof(int));
1464 if (put_user(olr, optlen))
1466 if (optname == MRT_VERSION)
1468 #ifdef CONFIG_IP_PIMSM
1469 else if (optname == MRT_PIM)
1470 val = mrt->mroute_do_pim;
1473 val = mrt->mroute_do_assert;
1474 if (copy_to_user(optval, &val, olr))
1480 * The IP multicast ioctl support routines.
1483 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1485 struct sioc_sg_req sr;
1486 struct sioc_vif_req vr;
1487 struct vif_device *vif;
1488 struct mfc_cache *c;
1489 struct net *net = sock_net(sk);
1490 struct mr_table *mrt;
1492 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1498 if (copy_from_user(&vr, arg, sizeof(vr)))
1500 if (vr.vifi >= mrt->maxvif)
1502 read_lock(&mrt_lock);
1503 vif = &mrt->vif_table[vr.vifi];
1504 if (VIF_EXISTS(mrt, vr.vifi)) {
1505 vr.icount = vif->pkt_in;
1506 vr.ocount = vif->pkt_out;
1507 vr.ibytes = vif->bytes_in;
1508 vr.obytes = vif->bytes_out;
1509 read_unlock(&mrt_lock);
1511 if (copy_to_user(arg, &vr, sizeof(vr)))
1515 read_unlock(&mrt_lock);
1516 return -EADDRNOTAVAIL;
1518 if (copy_from_user(&sr, arg, sizeof(sr)))
1522 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1524 sr.pktcnt = c->mfc_un.res.pkt;
1525 sr.bytecnt = c->mfc_un.res.bytes;
1526 sr.wrong_if = c->mfc_un.res.wrong_if;
1529 if (copy_to_user(arg, &sr, sizeof(sr)))
1534 return -EADDRNOTAVAIL;
1536 return -ENOIOCTLCMD;
1540 #ifdef CONFIG_COMPAT
1541 struct compat_sioc_sg_req {
1544 compat_ulong_t pktcnt;
1545 compat_ulong_t bytecnt;
1546 compat_ulong_t wrong_if;
1549 struct compat_sioc_vif_req {
1550 vifi_t vifi; /* Which iface */
1551 compat_ulong_t icount;
1552 compat_ulong_t ocount;
1553 compat_ulong_t ibytes;
1554 compat_ulong_t obytes;
1557 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1559 struct compat_sioc_sg_req sr;
1560 struct compat_sioc_vif_req vr;
1561 struct vif_device *vif;
1562 struct mfc_cache *c;
1563 struct net *net = sock_net(sk);
1564 struct mr_table *mrt;
1566 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1572 if (copy_from_user(&vr, arg, sizeof(vr)))
1574 if (vr.vifi >= mrt->maxvif)
1576 read_lock(&mrt_lock);
1577 vif = &mrt->vif_table[vr.vifi];
1578 if (VIF_EXISTS(mrt, vr.vifi)) {
1579 vr.icount = vif->pkt_in;
1580 vr.ocount = vif->pkt_out;
1581 vr.ibytes = vif->bytes_in;
1582 vr.obytes = vif->bytes_out;
1583 read_unlock(&mrt_lock);
1585 if (copy_to_user(arg, &vr, sizeof(vr)))
1589 read_unlock(&mrt_lock);
1590 return -EADDRNOTAVAIL;
1592 if (copy_from_user(&sr, arg, sizeof(sr)))
1596 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1598 sr.pktcnt = c->mfc_un.res.pkt;
1599 sr.bytecnt = c->mfc_un.res.bytes;
1600 sr.wrong_if = c->mfc_un.res.wrong_if;
1603 if (copy_to_user(arg, &sr, sizeof(sr)))
1608 return -EADDRNOTAVAIL;
1610 return -ENOIOCTLCMD;
1616 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1618 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1619 struct net *net = dev_net(dev);
1620 struct mr_table *mrt;
1621 struct vif_device *v;
1624 if (event != NETDEV_UNREGISTER)
1627 ipmr_for_each_table(mrt, net) {
1628 v = &mrt->vif_table[0];
1629 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1631 vif_delete(mrt, ct, 1, NULL);
1638 static struct notifier_block ip_mr_notifier = {
1639 .notifier_call = ipmr_device_event,
1643 * Encapsulate a packet by attaching a valid IPIP header to it.
1644 * This avoids tunnel drivers and other mess and gives us the speed so
1645 * important for multicast video.
1648 static void ip_encap(struct sk_buff *skb, __be32 saddr, __be32 daddr)
1651 const struct iphdr *old_iph = ip_hdr(skb);
1653 skb_push(skb, sizeof(struct iphdr));
1654 skb->transport_header = skb->network_header;
1655 skb_reset_network_header(skb);
1659 iph->tos = old_iph->tos;
1660 iph->ttl = old_iph->ttl;
1664 iph->protocol = IPPROTO_IPIP;
1666 iph->tot_len = htons(skb->len);
1667 ip_select_ident(iph, skb_dst(skb), NULL);
1670 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1674 static inline int ipmr_forward_finish(struct sk_buff *skb)
1676 struct ip_options *opt = &(IPCB(skb)->opt);
1678 IP_INC_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTFORWDATAGRAMS);
1679 IP_ADD_STATS_BH(dev_net(skb_dst(skb)->dev), IPSTATS_MIB_OUTOCTETS, skb->len);
1681 if (unlikely(opt->optlen))
1682 ip_forward_options(skb);
1684 return dst_output(skb);
1688 * Processing handlers for ipmr_forward
1691 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1692 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1694 const struct iphdr *iph = ip_hdr(skb);
1695 struct vif_device *vif = &mrt->vif_table[vifi];
1696 struct net_device *dev;
1701 if (vif->dev == NULL)
1704 #ifdef CONFIG_IP_PIMSM
1705 if (vif->flags & VIFF_REGISTER) {
1707 vif->bytes_out += skb->len;
1708 vif->dev->stats.tx_bytes += skb->len;
1709 vif->dev->stats.tx_packets++;
1710 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1715 if (vif->flags & VIFF_TUNNEL) {
1716 rt = ip_route_output_ports(net, &fl4, NULL,
1717 vif->remote, vif->local,
1720 RT_TOS(iph->tos), vif->link);
1723 encap = sizeof(struct iphdr);
1725 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1728 RT_TOS(iph->tos), vif->link);
1735 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1736 /* Do not fragment multicasts. Alas, IPv4 does not
1737 * allow to send ICMP, so that packets will disappear
1741 IP_INC_STATS_BH(dev_net(dev), IPSTATS_MIB_FRAGFAILS);
1746 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1748 if (skb_cow(skb, encap)) {
1754 vif->bytes_out += skb->len;
1757 skb_dst_set(skb, &rt->dst);
1758 ip_decrease_ttl(ip_hdr(skb));
1760 /* FIXME: forward and output firewalls used to be called here.
1761 * What do we do with netfilter? -- RR
1763 if (vif->flags & VIFF_TUNNEL) {
1764 ip_encap(skb, vif->local, vif->remote);
1765 /* FIXME: extra output firewall step used to be here. --RR */
1766 vif->dev->stats.tx_packets++;
1767 vif->dev->stats.tx_bytes += skb->len;
1770 IPCB(skb)->flags |= IPSKB_FORWARDED;
1773 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1774 * not only before forwarding, but after forwarding on all output
1775 * interfaces. It is clear, if mrouter runs a multicasting
1776 * program, it should receive packets not depending to what interface
1777 * program is joined.
1778 * If we will not make it, the program will have to join on all
1779 * interfaces. On the other hand, multihoming host (or router, but
1780 * not mrouter) cannot join to more than one interface - it will
1781 * result in receiving multiple packets.
1783 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD, skb, skb->dev, dev,
1784 ipmr_forward_finish);
1791 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1795 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1796 if (mrt->vif_table[ct].dev == dev)
1802 /* "local" means that we should preserve one skb (for local delivery) */
1804 static int ip_mr_forward(struct net *net, struct mr_table *mrt,
1805 struct sk_buff *skb, struct mfc_cache *cache,
1810 int true_vifi = ipmr_find_vif(mrt, skb->dev);
1812 vif = cache->mfc_parent;
1813 cache->mfc_un.res.pkt++;
1814 cache->mfc_un.res.bytes += skb->len;
1816 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1817 struct mfc_cache *cache_proxy;
1819 /* For an (*,G) entry, we only check that the incomming
1820 * interface is part of the static tree.
1822 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1824 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1829 * Wrong interface: drop packet and (maybe) send PIM assert.
1831 if (mrt->vif_table[vif].dev != skb->dev) {
1832 if (rt_is_output_route(skb_rtable(skb))) {
1833 /* It is our own packet, looped back.
1834 * Very complicated situation...
1836 * The best workaround until routing daemons will be
1837 * fixed is not to redistribute packet, if it was
1838 * send through wrong interface. It means, that
1839 * multicast applications WILL NOT work for
1840 * (S,G), which have default multicast route pointing
1841 * to wrong oif. In any case, it is not a good
1842 * idea to use multicasting applications on router.
1847 cache->mfc_un.res.wrong_if++;
1849 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1850 /* pimsm uses asserts, when switching from RPT to SPT,
1851 * so that we cannot check that packet arrived on an oif.
1852 * It is bad, but otherwise we would need to move pretty
1853 * large chunk of pimd to kernel. Ough... --ANK
1855 (mrt->mroute_do_pim ||
1856 cache->mfc_un.res.ttls[true_vifi] < 255) &&
1858 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1859 cache->mfc_un.res.last_assert = jiffies;
1860 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1866 mrt->vif_table[vif].pkt_in++;
1867 mrt->vif_table[vif].bytes_in += skb->len;
1872 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1873 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
1874 if (true_vifi >= 0 &&
1875 true_vifi != cache->mfc_parent &&
1877 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1878 /* It's an (*,*) entry and the packet is not coming from
1879 * the upstream: forward the packet to the upstream
1882 psend = cache->mfc_parent;
1887 for (ct = cache->mfc_un.res.maxvif - 1;
1888 ct >= cache->mfc_un.res.minvif; ct--) {
1889 /* For (*,G) entry, don't forward to the incoming interface */
1890 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1892 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
1894 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1897 ipmr_queue_xmit(net, mrt, skb2, cache,
1906 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1909 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
1911 ipmr_queue_xmit(net, mrt, skb, cache, psend);
1922 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
1924 struct rtable *rt = skb_rtable(skb);
1925 struct iphdr *iph = ip_hdr(skb);
1926 struct flowi4 fl4 = {
1927 .daddr = iph->daddr,
1928 .saddr = iph->saddr,
1929 .flowi4_tos = RT_TOS(iph->tos),
1930 .flowi4_oif = (rt_is_output_route(rt) ?
1931 skb->dev->ifindex : 0),
1932 .flowi4_iif = (rt_is_output_route(rt) ?
1935 .flowi4_mark = skb->mark,
1937 struct mr_table *mrt;
1940 err = ipmr_fib_lookup(net, &fl4, &mrt);
1942 return ERR_PTR(err);
1947 * Multicast packets for forwarding arrive here
1948 * Called with rcu_read_lock();
1951 int ip_mr_input(struct sk_buff *skb)
1953 struct mfc_cache *cache;
1954 struct net *net = dev_net(skb->dev);
1955 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
1956 struct mr_table *mrt;
1958 /* Packet is looped back after forward, it should not be
1959 * forwarded second time, but still can be delivered locally.
1961 if (IPCB(skb)->flags & IPSKB_FORWARDED)
1964 mrt = ipmr_rt_fib_lookup(net, skb);
1967 return PTR_ERR(mrt);
1970 if (IPCB(skb)->opt.router_alert) {
1971 if (ip_call_ra_chain(skb))
1973 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1974 /* IGMPv1 (and broken IGMPv2 implementations sort of
1975 * Cisco IOS <= 11.2(8)) do not put router alert
1976 * option to IGMP packets destined to routable
1977 * groups. It is very bad, because it means
1978 * that we can forward NO IGMP messages.
1980 struct sock *mroute_sk;
1982 mroute_sk = rcu_dereference(mrt->mroute_sk);
1985 raw_rcv(mroute_sk, skb);
1991 /* already under rcu_read_lock() */
1992 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
1993 if (cache == NULL) {
1994 int vif = ipmr_find_vif(mrt, skb->dev);
1997 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2002 * No usable cache entry
2004 if (cache == NULL) {
2008 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2009 ip_local_deliver(skb);
2015 read_lock(&mrt_lock);
2016 vif = ipmr_find_vif(mrt, skb->dev);
2018 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
2019 read_unlock(&mrt_lock);
2023 read_unlock(&mrt_lock);
2028 read_lock(&mrt_lock);
2029 ip_mr_forward(net, mrt, skb, cache, local);
2030 read_unlock(&mrt_lock);
2033 return ip_local_deliver(skb);
2039 return ip_local_deliver(skb);
2044 #ifdef CONFIG_IP_PIMSM
2045 /* called with rcu_read_lock() */
2046 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2047 unsigned int pimlen)
2049 struct net_device *reg_dev = NULL;
2050 struct iphdr *encap;
2052 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
2055 * a. packet is really sent to a multicast group
2056 * b. packet is not a NULL-REGISTER
2057 * c. packet is not truncated
2059 if (!ipv4_is_multicast(encap->daddr) ||
2060 encap->tot_len == 0 ||
2061 ntohs(encap->tot_len) + pimlen > skb->len)
2064 read_lock(&mrt_lock);
2065 if (mrt->mroute_reg_vif_num >= 0)
2066 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
2067 read_unlock(&mrt_lock);
2069 if (reg_dev == NULL)
2072 skb->mac_header = skb->network_header;
2073 skb_pull(skb, (u8 *)encap - skb->data);
2074 skb_reset_network_header(skb);
2075 skb->protocol = htons(ETH_P_IP);
2076 skb->ip_summed = CHECKSUM_NONE;
2077 skb->pkt_type = PACKET_HOST;
2079 skb_tunnel_rx(skb, reg_dev);
2083 return NET_RX_SUCCESS;
2087 #ifdef CONFIG_IP_PIMSM_V1
2089 * Handle IGMP messages of PIMv1
2092 int pim_rcv_v1(struct sk_buff *skb)
2094 struct igmphdr *pim;
2095 struct net *net = dev_net(skb->dev);
2096 struct mr_table *mrt;
2098 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2101 pim = igmp_hdr(skb);
2103 mrt = ipmr_rt_fib_lookup(net, skb);
2106 if (!mrt->mroute_do_pim ||
2107 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2110 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2118 #ifdef CONFIG_IP_PIMSM_V2
2119 static int pim_rcv(struct sk_buff *skb)
2121 struct pimreghdr *pim;
2122 struct net *net = dev_net(skb->dev);
2123 struct mr_table *mrt;
2125 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2128 pim = (struct pimreghdr *)skb_transport_header(skb);
2129 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2130 (pim->flags & PIM_NULL_REGISTER) ||
2131 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2132 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2135 mrt = ipmr_rt_fib_lookup(net, skb);
2138 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2146 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2147 struct mfc_cache *c, struct rtmsg *rtm)
2150 struct rtnexthop *nhp;
2151 struct nlattr *mp_attr;
2152 struct rta_mfc_stats mfcs;
2154 /* If cache is unresolved, don't try to parse IIF and OIF */
2155 if (c->mfc_parent >= MAXVIFS)
2158 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2159 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2162 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2165 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2166 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2167 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2168 nla_nest_cancel(skb, mp_attr);
2172 nhp->rtnh_flags = 0;
2173 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2174 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2175 nhp->rtnh_len = sizeof(*nhp);
2179 nla_nest_end(skb, mp_attr);
2181 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2182 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2183 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2184 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2187 rtm->rtm_type = RTN_MULTICAST;
2191 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2192 __be32 saddr, __be32 daddr,
2193 struct rtmsg *rtm, int nowait)
2195 struct mfc_cache *cache;
2196 struct mr_table *mrt;
2199 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2204 cache = ipmr_cache_find(mrt, saddr, daddr);
2205 if (cache == NULL && skb->dev) {
2206 int vif = ipmr_find_vif(mrt, skb->dev);
2209 cache = ipmr_cache_find_any(mrt, daddr, vif);
2211 if (cache == NULL) {
2212 struct sk_buff *skb2;
2214 struct net_device *dev;
2223 read_lock(&mrt_lock);
2225 vif = ipmr_find_vif(mrt, dev);
2227 read_unlock(&mrt_lock);
2231 skb2 = skb_clone(skb, GFP_ATOMIC);
2233 read_unlock(&mrt_lock);
2238 skb_push(skb2, sizeof(struct iphdr));
2239 skb_reset_network_header(skb2);
2241 iph->ihl = sizeof(struct iphdr) >> 2;
2245 err = ipmr_cache_unresolved(mrt, vif, skb2);
2246 read_unlock(&mrt_lock);
2251 read_lock(&mrt_lock);
2252 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
2253 cache->mfc_flags |= MFC_NOTIFY;
2254 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2255 read_unlock(&mrt_lock);
2260 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2261 u32 portid, u32 seq, struct mfc_cache *c, int cmd)
2263 struct nlmsghdr *nlh;
2267 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), NLM_F_MULTI);
2271 rtm = nlmsg_data(nlh);
2272 rtm->rtm_family = RTNL_FAMILY_IPMR;
2273 rtm->rtm_dst_len = 32;
2274 rtm->rtm_src_len = 32;
2276 rtm->rtm_table = mrt->id;
2277 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2278 goto nla_put_failure;
2279 rtm->rtm_type = RTN_MULTICAST;
2280 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2281 if (c->mfc_flags & MFC_STATIC)
2282 rtm->rtm_protocol = RTPROT_STATIC;
2284 rtm->rtm_protocol = RTPROT_MROUTED;
2287 if (nla_put_be32(skb, RTA_SRC, c->mfc_origin) ||
2288 nla_put_be32(skb, RTA_DST, c->mfc_mcastgrp))
2289 goto nla_put_failure;
2290 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2291 /* do not break the dump if cache is unresolved */
2292 if (err < 0 && err != -ENOENT)
2293 goto nla_put_failure;
2295 return nlmsg_end(skb, nlh);
2298 nlmsg_cancel(skb, nlh);
2302 static size_t mroute_msgsize(bool unresolved, int maxvif)
2305 NLMSG_ALIGN(sizeof(struct rtmsg))
2306 + nla_total_size(4) /* RTA_TABLE */
2307 + nla_total_size(4) /* RTA_SRC */
2308 + nla_total_size(4) /* RTA_DST */
2313 + nla_total_size(4) /* RTA_IIF */
2314 + nla_total_size(0) /* RTA_MULTIPATH */
2315 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2317 + nla_total_size(sizeof(struct rta_mfc_stats))
2323 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2326 struct net *net = read_pnet(&mrt->net);
2327 struct sk_buff *skb;
2330 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2335 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd);
2339 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2345 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2348 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2350 struct net *net = sock_net(skb->sk);
2351 struct mr_table *mrt;
2352 struct mfc_cache *mfc;
2353 unsigned int t = 0, s_t;
2354 unsigned int h = 0, s_h;
2355 unsigned int e = 0, s_e;
2362 ipmr_for_each_table(mrt, net) {
2367 for (h = s_h; h < MFC_LINES; h++) {
2368 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
2371 if (ipmr_fill_mroute(mrt, skb,
2372 NETLINK_CB(cb->skb).portid,
2374 mfc, RTM_NEWROUTE) < 0)
2381 spin_lock_bh(&mfc_unres_lock);
2382 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2385 if (ipmr_fill_mroute(mrt, skb,
2386 NETLINK_CB(cb->skb).portid,
2388 mfc, RTM_NEWROUTE) < 0) {
2389 spin_unlock_bh(&mfc_unres_lock);
2395 spin_unlock_bh(&mfc_unres_lock);
2411 #ifdef CONFIG_PROC_FS
2413 * The /proc interfaces to multicast routing :
2414 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2416 struct ipmr_vif_iter {
2417 struct seq_net_private p;
2418 struct mr_table *mrt;
2422 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2423 struct ipmr_vif_iter *iter,
2426 struct mr_table *mrt = iter->mrt;
2428 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2429 if (!VIF_EXISTS(mrt, iter->ct))
2432 return &mrt->vif_table[iter->ct];
2437 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2438 __acquires(mrt_lock)
2440 struct ipmr_vif_iter *iter = seq->private;
2441 struct net *net = seq_file_net(seq);
2442 struct mr_table *mrt;
2444 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2446 return ERR_PTR(-ENOENT);
2450 read_lock(&mrt_lock);
2451 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2455 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2457 struct ipmr_vif_iter *iter = seq->private;
2458 struct net *net = seq_file_net(seq);
2459 struct mr_table *mrt = iter->mrt;
2462 if (v == SEQ_START_TOKEN)
2463 return ipmr_vif_seq_idx(net, iter, 0);
2465 while (++iter->ct < mrt->maxvif) {
2466 if (!VIF_EXISTS(mrt, iter->ct))
2468 return &mrt->vif_table[iter->ct];
2473 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2474 __releases(mrt_lock)
2476 read_unlock(&mrt_lock);
2479 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2481 struct ipmr_vif_iter *iter = seq->private;
2482 struct mr_table *mrt = iter->mrt;
2484 if (v == SEQ_START_TOKEN) {
2486 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2488 const struct vif_device *vif = v;
2489 const char *name = vif->dev ? vif->dev->name : "none";
2492 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2493 vif - mrt->vif_table,
2494 name, vif->bytes_in, vif->pkt_in,
2495 vif->bytes_out, vif->pkt_out,
2496 vif->flags, vif->local, vif->remote);
2501 static const struct seq_operations ipmr_vif_seq_ops = {
2502 .start = ipmr_vif_seq_start,
2503 .next = ipmr_vif_seq_next,
2504 .stop = ipmr_vif_seq_stop,
2505 .show = ipmr_vif_seq_show,
2508 static int ipmr_vif_open(struct inode *inode, struct file *file)
2510 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2511 sizeof(struct ipmr_vif_iter));
2514 static const struct file_operations ipmr_vif_fops = {
2515 .owner = THIS_MODULE,
2516 .open = ipmr_vif_open,
2518 .llseek = seq_lseek,
2519 .release = seq_release_net,
2522 struct ipmr_mfc_iter {
2523 struct seq_net_private p;
2524 struct mr_table *mrt;
2525 struct list_head *cache;
2530 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2531 struct ipmr_mfc_iter *it, loff_t pos)
2533 struct mr_table *mrt = it->mrt;
2534 struct mfc_cache *mfc;
2537 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
2538 it->cache = &mrt->mfc_cache_array[it->ct];
2539 list_for_each_entry_rcu(mfc, it->cache, list)
2545 spin_lock_bh(&mfc_unres_lock);
2546 it->cache = &mrt->mfc_unres_queue;
2547 list_for_each_entry(mfc, it->cache, list)
2550 spin_unlock_bh(&mfc_unres_lock);
2557 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2559 struct ipmr_mfc_iter *it = seq->private;
2560 struct net *net = seq_file_net(seq);
2561 struct mr_table *mrt;
2563 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2565 return ERR_PTR(-ENOENT);
2570 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
2574 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2576 struct mfc_cache *mfc = v;
2577 struct ipmr_mfc_iter *it = seq->private;
2578 struct net *net = seq_file_net(seq);
2579 struct mr_table *mrt = it->mrt;
2583 if (v == SEQ_START_TOKEN)
2584 return ipmr_mfc_seq_idx(net, seq->private, 0);
2586 if (mfc->list.next != it->cache)
2587 return list_entry(mfc->list.next, struct mfc_cache, list);
2589 if (it->cache == &mrt->mfc_unres_queue)
2592 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
2594 while (++it->ct < MFC_LINES) {
2595 it->cache = &mrt->mfc_cache_array[it->ct];
2596 if (list_empty(it->cache))
2598 return list_first_entry(it->cache, struct mfc_cache, list);
2601 /* exhausted cache_array, show unresolved */
2603 it->cache = &mrt->mfc_unres_queue;
2606 spin_lock_bh(&mfc_unres_lock);
2607 if (!list_empty(it->cache))
2608 return list_first_entry(it->cache, struct mfc_cache, list);
2611 spin_unlock_bh(&mfc_unres_lock);
2617 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2619 struct ipmr_mfc_iter *it = seq->private;
2620 struct mr_table *mrt = it->mrt;
2622 if (it->cache == &mrt->mfc_unres_queue)
2623 spin_unlock_bh(&mfc_unres_lock);
2624 else if (it->cache == &mrt->mfc_cache_array[it->ct])
2628 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2632 if (v == SEQ_START_TOKEN) {
2634 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2636 const struct mfc_cache *mfc = v;
2637 const struct ipmr_mfc_iter *it = seq->private;
2638 const struct mr_table *mrt = it->mrt;
2640 seq_printf(seq, "%08X %08X %-3hd",
2641 (__force u32) mfc->mfc_mcastgrp,
2642 (__force u32) mfc->mfc_origin,
2645 if (it->cache != &mrt->mfc_unres_queue) {
2646 seq_printf(seq, " %8lu %8lu %8lu",
2647 mfc->mfc_un.res.pkt,
2648 mfc->mfc_un.res.bytes,
2649 mfc->mfc_un.res.wrong_if);
2650 for (n = mfc->mfc_un.res.minvif;
2651 n < mfc->mfc_un.res.maxvif; n++) {
2652 if (VIF_EXISTS(mrt, n) &&
2653 mfc->mfc_un.res.ttls[n] < 255)
2656 n, mfc->mfc_un.res.ttls[n]);
2659 /* unresolved mfc_caches don't contain
2660 * pkt, bytes and wrong_if values
2662 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2664 seq_putc(seq, '\n');
2669 static const struct seq_operations ipmr_mfc_seq_ops = {
2670 .start = ipmr_mfc_seq_start,
2671 .next = ipmr_mfc_seq_next,
2672 .stop = ipmr_mfc_seq_stop,
2673 .show = ipmr_mfc_seq_show,
2676 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2678 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2679 sizeof(struct ipmr_mfc_iter));
2682 static const struct file_operations ipmr_mfc_fops = {
2683 .owner = THIS_MODULE,
2684 .open = ipmr_mfc_open,
2686 .llseek = seq_lseek,
2687 .release = seq_release_net,
2691 #ifdef CONFIG_IP_PIMSM_V2
2692 static const struct net_protocol pim_protocol = {
2700 * Setup for IP multicast routing
2702 static int __net_init ipmr_net_init(struct net *net)
2706 err = ipmr_rules_init(net);
2710 #ifdef CONFIG_PROC_FS
2712 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
2714 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
2715 goto proc_cache_fail;
2719 #ifdef CONFIG_PROC_FS
2721 remove_proc_entry("ip_mr_vif", net->proc_net);
2723 ipmr_rules_exit(net);
2729 static void __net_exit ipmr_net_exit(struct net *net)
2731 #ifdef CONFIG_PROC_FS
2732 remove_proc_entry("ip_mr_cache", net->proc_net);
2733 remove_proc_entry("ip_mr_vif", net->proc_net);
2735 ipmr_rules_exit(net);
2738 static struct pernet_operations ipmr_net_ops = {
2739 .init = ipmr_net_init,
2740 .exit = ipmr_net_exit,
2743 int __init ip_mr_init(void)
2747 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2748 sizeof(struct mfc_cache),
2749 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
2754 err = register_pernet_subsys(&ipmr_net_ops);
2756 goto reg_pernet_fail;
2758 err = register_netdevice_notifier(&ip_mr_notifier);
2760 goto reg_notif_fail;
2761 #ifdef CONFIG_IP_PIMSM_V2
2762 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
2763 pr_err("%s: can't add PIM protocol\n", __func__);
2765 goto add_proto_fail;
2768 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2769 NULL, ipmr_rtm_dumproute, NULL);
2772 #ifdef CONFIG_IP_PIMSM_V2
2774 unregister_netdevice_notifier(&ip_mr_notifier);
2777 unregister_pernet_subsys(&ipmr_net_ops);
2779 kmem_cache_destroy(mrt_cachep);