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 <linux/uaccess.h>
30 #include <linux/types.h>
31 #include <linux/capability.h>
32 #include <linux/errno.h>
33 #include <linux/timer.h>
35 #include <linux/kernel.h>
36 #include <linux/fcntl.h>
37 #include <linux/stat.h>
38 #include <linux/socket.h>
40 #include <linux/inet.h>
41 #include <linux/netdevice.h>
42 #include <linux/inetdevice.h>
43 #include <linux/igmp.h>
44 #include <linux/proc_fs.h>
45 #include <linux/seq_file.h>
46 #include <linux/mroute.h>
47 #include <linux/init.h>
48 #include <linux/if_ether.h>
49 #include <linux/slab.h>
50 #include <net/net_namespace.h>
52 #include <net/protocol.h>
53 #include <linux/skbuff.h>
54 #include <net/route.h>
59 #include <linux/notifier.h>
60 #include <linux/if_arp.h>
61 #include <linux/netfilter_ipv4.h>
62 #include <linux/compat.h>
63 #include <linux/export.h>
64 #include <net/ip_tunnels.h>
65 #include <net/checksum.h>
66 #include <net/netlink.h>
67 #include <net/fib_rules.h>
68 #include <linux/netconf.h>
69 #include <net/nexthop.h>
72 struct fib_rule common;
79 /* Big lock, protecting vif table, mrt cache and mroute socket state.
80 * Note that the changes are semaphored via rtnl_lock.
83 static DEFINE_RWLOCK(mrt_lock);
85 /* Multicast router control variables */
87 /* Special spinlock for queue of unresolved entries */
88 static DEFINE_SPINLOCK(mfc_unres_lock);
90 /* We return to original Alan's scheme. Hash table of resolved
91 * entries is changed only in process context and protected
92 * with weak lock mrt_lock. Queue of unresolved entries is protected
93 * with strong spinlock mfc_unres_lock.
95 * In this case data path is free of exclusive locks at all.
98 static struct kmem_cache *mrt_cachep __read_mostly;
100 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
101 static void ipmr_free_table(struct mr_table *mrt);
103 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
104 struct net_device *dev, struct sk_buff *skb,
105 struct mfc_cache *cache, int local);
106 static int ipmr_cache_report(struct mr_table *mrt,
107 struct sk_buff *pkt, vifi_t vifi, int assert);
108 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
109 struct mfc_cache *c, struct rtmsg *rtm);
110 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
112 static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
113 static void mroute_clean_tables(struct mr_table *mrt, bool all);
114 static void ipmr_expire_process(unsigned long arg);
116 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
117 #define ipmr_for_each_table(mrt, net) \
118 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
120 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
122 struct mr_table *mrt;
124 ipmr_for_each_table(mrt, net) {
131 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
132 struct mr_table **mrt)
135 struct ipmr_result res;
136 struct fib_lookup_arg arg = {
138 .flags = FIB_LOOKUP_NOREF,
141 /* update flow if oif or iif point to device enslaved to l3mdev */
142 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
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 arg->table = fib_rule_get_table(rule, arg);
172 mrt = ipmr_get_table(rule->fr_net, arg->table);
179 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
184 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
188 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
189 struct fib_rule_hdr *frh, struct nlattr **tb)
194 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
200 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
201 struct fib_rule_hdr *frh)
209 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
210 .family = RTNL_FAMILY_IPMR,
211 .rule_size = sizeof(struct ipmr_rule),
212 .addr_size = sizeof(u32),
213 .action = ipmr_rule_action,
214 .match = ipmr_rule_match,
215 .configure = ipmr_rule_configure,
216 .compare = ipmr_rule_compare,
217 .fill = ipmr_rule_fill,
218 .nlgroup = RTNLGRP_IPV4_RULE,
219 .policy = ipmr_rule_policy,
220 .owner = THIS_MODULE,
223 static int __net_init ipmr_rules_init(struct net *net)
225 struct fib_rules_ops *ops;
226 struct mr_table *mrt;
229 ops = fib_rules_register(&ipmr_rules_ops_template, net);
233 INIT_LIST_HEAD(&net->ipv4.mr_tables);
235 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
241 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
245 net->ipv4.mr_rules_ops = ops;
249 ipmr_free_table(mrt);
251 fib_rules_unregister(ops);
255 static void __net_exit ipmr_rules_exit(struct net *net)
257 struct mr_table *mrt, *next;
260 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
261 list_del(&mrt->list);
262 ipmr_free_table(mrt);
264 fib_rules_unregister(net->ipv4.mr_rules_ops);
268 static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
270 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR);
273 static unsigned int ipmr_rules_seq_read(struct net *net)
275 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
278 bool ipmr_rule_default(const struct fib_rule *rule)
280 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
282 EXPORT_SYMBOL(ipmr_rule_default);
284 #define ipmr_for_each_table(mrt, net) \
285 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
287 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
289 return net->ipv4.mrt;
292 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
293 struct mr_table **mrt)
295 *mrt = net->ipv4.mrt;
299 static int __net_init ipmr_rules_init(struct net *net)
301 struct mr_table *mrt;
303 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
310 static void __net_exit ipmr_rules_exit(struct net *net)
313 ipmr_free_table(net->ipv4.mrt);
314 net->ipv4.mrt = NULL;
318 static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
323 static unsigned int ipmr_rules_seq_read(struct net *net)
328 bool ipmr_rule_default(const struct fib_rule *rule)
332 EXPORT_SYMBOL(ipmr_rule_default);
335 static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
338 const struct mfc_cache_cmp_arg *cmparg = arg->key;
339 struct mfc_cache *c = (struct mfc_cache *)ptr;
341 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
342 cmparg->mfc_origin != c->mfc_origin;
345 static const struct rhashtable_params ipmr_rht_params = {
346 .head_offset = offsetof(struct mfc_cache, mnode),
347 .key_offset = offsetof(struct mfc_cache, cmparg),
348 .key_len = sizeof(struct mfc_cache_cmp_arg),
351 .obj_cmpfn = ipmr_hash_cmp,
352 .automatic_shrinking = true,
355 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
357 struct mr_table *mrt;
359 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
360 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
361 return ERR_PTR(-EINVAL);
363 mrt = ipmr_get_table(net, id);
367 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
369 return ERR_PTR(-ENOMEM);
370 write_pnet(&mrt->net, net);
373 rhltable_init(&mrt->mfc_hash, &ipmr_rht_params);
374 INIT_LIST_HEAD(&mrt->mfc_cache_list);
375 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
377 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
380 mrt->mroute_reg_vif_num = -1;
381 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
382 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
387 static void ipmr_free_table(struct mr_table *mrt)
389 del_timer_sync(&mrt->ipmr_expire_timer);
390 mroute_clean_tables(mrt, true);
391 rhltable_destroy(&mrt->mfc_hash);
395 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
397 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
399 struct net *net = dev_net(dev);
403 dev = __dev_get_by_name(net, "tunl0");
405 const struct net_device_ops *ops = dev->netdev_ops;
407 struct ip_tunnel_parm p;
409 memset(&p, 0, sizeof(p));
410 p.iph.daddr = v->vifc_rmt_addr.s_addr;
411 p.iph.saddr = v->vifc_lcl_addr.s_addr;
414 p.iph.protocol = IPPROTO_IPIP;
415 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
416 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
418 if (ops->ndo_do_ioctl) {
419 mm_segment_t oldfs = get_fs();
422 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
428 /* Initialize ipmr pimreg/tunnel in_device */
429 static bool ipmr_init_vif_indev(const struct net_device *dev)
431 struct in_device *in_dev;
435 in_dev = __in_dev_get_rtnl(dev);
438 ipv4_devconf_setall(in_dev);
439 neigh_parms_data_state_setall(in_dev->arp_parms);
440 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
445 static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
447 struct net_device *dev;
449 dev = __dev_get_by_name(net, "tunl0");
452 const struct net_device_ops *ops = dev->netdev_ops;
455 struct ip_tunnel_parm p;
457 memset(&p, 0, sizeof(p));
458 p.iph.daddr = v->vifc_rmt_addr.s_addr;
459 p.iph.saddr = v->vifc_lcl_addr.s_addr;
462 p.iph.protocol = IPPROTO_IPIP;
463 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
464 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
466 if (ops->ndo_do_ioctl) {
467 mm_segment_t oldfs = get_fs();
470 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
478 (dev = __dev_get_by_name(net, p.name)) != NULL) {
479 dev->flags |= IFF_MULTICAST;
480 if (!ipmr_init_vif_indev(dev))
490 unregister_netdevice(dev);
494 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
495 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
497 struct net *net = dev_net(dev);
498 struct mr_table *mrt;
499 struct flowi4 fl4 = {
500 .flowi4_oif = dev->ifindex,
501 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
502 .flowi4_mark = skb->mark,
506 err = ipmr_fib_lookup(net, &fl4, &mrt);
512 read_lock(&mrt_lock);
513 dev->stats.tx_bytes += skb->len;
514 dev->stats.tx_packets++;
515 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
516 read_unlock(&mrt_lock);
521 static int reg_vif_get_iflink(const struct net_device *dev)
526 static const struct net_device_ops reg_vif_netdev_ops = {
527 .ndo_start_xmit = reg_vif_xmit,
528 .ndo_get_iflink = reg_vif_get_iflink,
531 static void reg_vif_setup(struct net_device *dev)
533 dev->type = ARPHRD_PIMREG;
534 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
535 dev->flags = IFF_NOARP;
536 dev->netdev_ops = ®_vif_netdev_ops;
537 dev->needs_free_netdev = true;
538 dev->features |= NETIF_F_NETNS_LOCAL;
541 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
543 struct net_device *dev;
546 if (mrt->id == RT_TABLE_DEFAULT)
547 sprintf(name, "pimreg");
549 sprintf(name, "pimreg%u", mrt->id);
551 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
556 dev_net_set(dev, net);
558 if (register_netdevice(dev)) {
563 if (!ipmr_init_vif_indev(dev))
573 unregister_netdevice(dev);
577 /* called with rcu_read_lock() */
578 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
581 struct net_device *reg_dev = NULL;
584 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
586 * a. packet is really sent to a multicast group
587 * b. packet is not a NULL-REGISTER
588 * c. packet is not truncated
590 if (!ipv4_is_multicast(encap->daddr) ||
591 encap->tot_len == 0 ||
592 ntohs(encap->tot_len) + pimlen > skb->len)
595 read_lock(&mrt_lock);
596 if (mrt->mroute_reg_vif_num >= 0)
597 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
598 read_unlock(&mrt_lock);
603 skb->mac_header = skb->network_header;
604 skb_pull(skb, (u8 *)encap - skb->data);
605 skb_reset_network_header(skb);
606 skb->protocol = htons(ETH_P_IP);
607 skb->ip_summed = CHECKSUM_NONE;
609 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
613 return NET_RX_SUCCESS;
616 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
622 static int call_ipmr_vif_entry_notifier(struct notifier_block *nb,
624 enum fib_event_type event_type,
625 struct vif_device *vif,
626 vifi_t vif_index, u32 tb_id)
628 struct vif_entry_notifier_info info = {
630 .family = RTNL_FAMILY_IPMR,
634 .vif_index = vif_index,
635 .vif_flags = vif->flags,
639 return call_fib_notifier(nb, net, event_type, &info.info);
642 static int call_ipmr_vif_entry_notifiers(struct net *net,
643 enum fib_event_type event_type,
644 struct vif_device *vif,
645 vifi_t vif_index, u32 tb_id)
647 struct vif_entry_notifier_info info = {
649 .family = RTNL_FAMILY_IPMR,
653 .vif_index = vif_index,
654 .vif_flags = vif->flags,
659 net->ipv4.ipmr_seq++;
660 return call_fib_notifiers(net, event_type, &info.info);
663 static int call_ipmr_mfc_entry_notifier(struct notifier_block *nb,
665 enum fib_event_type event_type,
666 struct mfc_cache *mfc, u32 tb_id)
668 struct mfc_entry_notifier_info info = {
670 .family = RTNL_FAMILY_IPMR,
677 return call_fib_notifier(nb, net, event_type, &info.info);
680 static int call_ipmr_mfc_entry_notifiers(struct net *net,
681 enum fib_event_type event_type,
682 struct mfc_cache *mfc, u32 tb_id)
684 struct mfc_entry_notifier_info info = {
686 .family = RTNL_FAMILY_IPMR,
694 net->ipv4.ipmr_seq++;
695 return call_fib_notifiers(net, event_type, &info.info);
699 * vif_delete - Delete a VIF entry
700 * @notify: Set to 1, if the caller is a notifier_call
702 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
703 struct list_head *head)
705 struct net *net = read_pnet(&mrt->net);
706 struct vif_device *v;
707 struct net_device *dev;
708 struct in_device *in_dev;
710 if (vifi < 0 || vifi >= mrt->maxvif)
711 return -EADDRNOTAVAIL;
713 v = &mrt->vif_table[vifi];
715 if (VIF_EXISTS(mrt, vifi))
716 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
719 write_lock_bh(&mrt_lock);
724 write_unlock_bh(&mrt_lock);
725 return -EADDRNOTAVAIL;
728 if (vifi == mrt->mroute_reg_vif_num)
729 mrt->mroute_reg_vif_num = -1;
731 if (vifi + 1 == mrt->maxvif) {
734 for (tmp = vifi - 1; tmp >= 0; tmp--) {
735 if (VIF_EXISTS(mrt, tmp))
741 write_unlock_bh(&mrt_lock);
743 dev_set_allmulti(dev, -1);
745 in_dev = __in_dev_get_rtnl(dev);
747 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
748 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
749 NETCONFA_MC_FORWARDING,
750 dev->ifindex, &in_dev->cnf);
751 ip_rt_multicast_event(in_dev);
754 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
755 unregister_netdevice_queue(dev, head);
761 static void ipmr_cache_free_rcu(struct rcu_head *head)
763 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
765 kmem_cache_free(mrt_cachep, c);
768 void ipmr_cache_free(struct mfc_cache *c)
770 call_rcu(&c->rcu, ipmr_cache_free_rcu);
772 EXPORT_SYMBOL(ipmr_cache_free);
774 /* Destroy an unresolved cache entry, killing queued skbs
775 * and reporting error to netlink readers.
777 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
779 struct net *net = read_pnet(&mrt->net);
783 atomic_dec(&mrt->cache_resolve_queue_len);
785 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
786 if (ip_hdr(skb)->version == 0) {
787 struct nlmsghdr *nlh = skb_pull(skb,
788 sizeof(struct iphdr));
789 nlh->nlmsg_type = NLMSG_ERROR;
790 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
791 skb_trim(skb, nlh->nlmsg_len);
793 e->error = -ETIMEDOUT;
794 memset(&e->msg, 0, sizeof(e->msg));
796 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
805 /* Timer process for the unresolved queue. */
806 static void ipmr_expire_process(unsigned long arg)
808 struct mr_table *mrt = (struct mr_table *)arg;
810 unsigned long expires;
811 struct mfc_cache *c, *next;
813 if (!spin_trylock(&mfc_unres_lock)) {
814 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
818 if (list_empty(&mrt->mfc_unres_queue))
824 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
825 if (time_after(c->mfc_un.unres.expires, now)) {
826 unsigned long interval = c->mfc_un.unres.expires - now;
827 if (interval < expires)
833 mroute_netlink_event(mrt, c, RTM_DELROUTE);
834 ipmr_destroy_unres(mrt, c);
837 if (!list_empty(&mrt->mfc_unres_queue))
838 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
841 spin_unlock(&mfc_unres_lock);
844 /* Fill oifs list. It is called under write locked mrt_lock. */
845 static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
850 cache->mfc_un.res.minvif = MAXVIFS;
851 cache->mfc_un.res.maxvif = 0;
852 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
854 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
855 if (VIF_EXISTS(mrt, vifi) &&
856 ttls[vifi] && ttls[vifi] < 255) {
857 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
858 if (cache->mfc_un.res.minvif > vifi)
859 cache->mfc_un.res.minvif = vifi;
860 if (cache->mfc_un.res.maxvif <= vifi)
861 cache->mfc_un.res.maxvif = vifi + 1;
864 cache->mfc_un.res.lastuse = jiffies;
867 static int vif_add(struct net *net, struct mr_table *mrt,
868 struct vifctl *vifc, int mrtsock)
870 int vifi = vifc->vifc_vifi;
871 struct vif_device *v = &mrt->vif_table[vifi];
872 struct net_device *dev;
873 struct in_device *in_dev;
877 if (VIF_EXISTS(mrt, vifi))
880 switch (vifc->vifc_flags) {
882 if (!ipmr_pimsm_enabled())
884 /* Special Purpose VIF in PIM
885 * All the packets will be sent to the daemon
887 if (mrt->mroute_reg_vif_num >= 0)
889 dev = ipmr_reg_vif(net, mrt);
892 err = dev_set_allmulti(dev, 1);
894 unregister_netdevice(dev);
900 dev = ipmr_new_tunnel(net, vifc);
903 err = dev_set_allmulti(dev, 1);
905 ipmr_del_tunnel(dev, vifc);
910 case VIFF_USE_IFINDEX:
912 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
913 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
914 if (dev && !__in_dev_get_rtnl(dev)) {
916 return -EADDRNOTAVAIL;
919 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
922 return -EADDRNOTAVAIL;
923 err = dev_set_allmulti(dev, 1);
933 in_dev = __in_dev_get_rtnl(dev);
936 return -EADDRNOTAVAIL;
938 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
939 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
940 dev->ifindex, &in_dev->cnf);
941 ip_rt_multicast_event(in_dev);
943 /* Fill in the VIF structures */
945 v->rate_limit = vifc->vifc_rate_limit;
946 v->local = vifc->vifc_lcl_addr.s_addr;
947 v->remote = vifc->vifc_rmt_addr.s_addr;
948 v->flags = vifc->vifc_flags;
950 v->flags |= VIFF_STATIC;
951 v->threshold = vifc->vifc_threshold;
956 v->link = dev->ifindex;
957 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
958 v->link = dev_get_iflink(dev);
960 /* And finish update writing critical data */
961 write_lock_bh(&mrt_lock);
963 if (v->flags & VIFF_REGISTER)
964 mrt->mroute_reg_vif_num = vifi;
965 if (vifi+1 > mrt->maxvif)
966 mrt->maxvif = vifi+1;
967 write_unlock_bh(&mrt_lock);
968 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
972 /* called with rcu_read_lock() */
973 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
977 struct mfc_cache_cmp_arg arg = {
978 .mfc_mcastgrp = mcastgrp,
981 struct rhlist_head *tmp, *list;
984 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
985 rhl_for_each_entry_rcu(c, tmp, list, mnode)
991 /* Look for a (*,*,oif) entry */
992 static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
995 struct mfc_cache_cmp_arg arg = {
996 .mfc_mcastgrp = htonl(INADDR_ANY),
997 .mfc_origin = htonl(INADDR_ANY)
999 struct rhlist_head *tmp, *list;
1000 struct mfc_cache *c;
1002 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
1003 rhl_for_each_entry_rcu(c, tmp, list, mnode)
1004 if (c->mfc_un.res.ttls[vifi] < 255)
1010 /* Look for a (*,G) entry */
1011 static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
1012 __be32 mcastgrp, int vifi)
1014 struct mfc_cache_cmp_arg arg = {
1015 .mfc_mcastgrp = mcastgrp,
1016 .mfc_origin = htonl(INADDR_ANY)
1018 struct rhlist_head *tmp, *list;
1019 struct mfc_cache *c, *proxy;
1021 if (mcastgrp == htonl(INADDR_ANY))
1024 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
1025 rhl_for_each_entry_rcu(c, tmp, list, mnode) {
1026 if (c->mfc_un.res.ttls[vifi] < 255)
1029 /* It's ok if the vifi is part of the static tree */
1030 proxy = ipmr_cache_find_any_parent(mrt, c->mfc_parent);
1031 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
1036 return ipmr_cache_find_any_parent(mrt, vifi);
1039 /* Look for a (S,G,iif) entry if parent != -1 */
1040 static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
1041 __be32 origin, __be32 mcastgrp,
1044 struct mfc_cache_cmp_arg arg = {
1045 .mfc_mcastgrp = mcastgrp,
1046 .mfc_origin = origin,
1048 struct rhlist_head *tmp, *list;
1049 struct mfc_cache *c;
1051 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
1052 rhl_for_each_entry_rcu(c, tmp, list, mnode)
1053 if (parent == -1 || parent == c->mfc_parent)
1059 /* Allocate a multicast cache entry */
1060 static struct mfc_cache *ipmr_cache_alloc(void)
1062 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
1065 c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
1066 c->mfc_un.res.minvif = MAXVIFS;
1067 refcount_set(&c->mfc_un.res.refcount, 1);
1072 static struct mfc_cache *ipmr_cache_alloc_unres(void)
1074 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
1077 skb_queue_head_init(&c->mfc_un.unres.unresolved);
1078 c->mfc_un.unres.expires = jiffies + 10*HZ;
1083 /* A cache entry has gone into a resolved state from queued */
1084 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1085 struct mfc_cache *uc, struct mfc_cache *c)
1087 struct sk_buff *skb;
1090 /* Play the pending entries through our router */
1091 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
1092 if (ip_hdr(skb)->version == 0) {
1093 struct nlmsghdr *nlh = skb_pull(skb,
1094 sizeof(struct iphdr));
1096 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
1097 nlh->nlmsg_len = skb_tail_pointer(skb) -
1100 nlh->nlmsg_type = NLMSG_ERROR;
1101 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
1102 skb_trim(skb, nlh->nlmsg_len);
1103 e = nlmsg_data(nlh);
1104 e->error = -EMSGSIZE;
1105 memset(&e->msg, 0, sizeof(e->msg));
1108 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
1110 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
1115 /* Bounce a cache query up to mrouted and netlink.
1117 * Called under mrt_lock.
1119 static int ipmr_cache_report(struct mr_table *mrt,
1120 struct sk_buff *pkt, vifi_t vifi, int assert)
1122 const int ihl = ip_hdrlen(pkt);
1123 struct sock *mroute_sk;
1124 struct igmphdr *igmp;
1125 struct igmpmsg *msg;
1126 struct sk_buff *skb;
1129 if (assert == IGMPMSG_WHOLEPKT)
1130 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1132 skb = alloc_skb(128, GFP_ATOMIC);
1137 if (assert == IGMPMSG_WHOLEPKT) {
1138 /* Ugly, but we have no choice with this interface.
1139 * Duplicate old header, fix ihl, length etc.
1140 * And all this only to mangle msg->im_msgtype and
1141 * to set msg->im_mbz to "mbz" :-)
1143 skb_push(skb, sizeof(struct iphdr));
1144 skb_reset_network_header(skb);
1145 skb_reset_transport_header(skb);
1146 msg = (struct igmpmsg *)skb_network_header(skb);
1147 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
1148 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1150 msg->im_vif = mrt->mroute_reg_vif_num;
1151 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1152 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1153 sizeof(struct iphdr));
1155 /* Copy the IP header */
1156 skb_set_network_header(skb, skb->len);
1158 skb_copy_to_linear_data(skb, pkt->data, ihl);
1159 /* Flag to the kernel this is a route add */
1160 ip_hdr(skb)->protocol = 0;
1161 msg = (struct igmpmsg *)skb_network_header(skb);
1163 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1164 /* Add our header */
1165 igmp = skb_put(skb, sizeof(struct igmphdr));
1166 igmp->type = assert;
1167 msg->im_msgtype = assert;
1169 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1170 skb->transport_header = skb->network_header;
1174 mroute_sk = rcu_dereference(mrt->mroute_sk);
1181 igmpmsg_netlink_event(mrt, skb);
1183 /* Deliver to mrouted */
1184 ret = sock_queue_rcv_skb(mroute_sk, skb);
1187 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1194 /* Queue a packet for resolution. It gets locked cache entry! */
1195 static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1196 struct sk_buff *skb, struct net_device *dev)
1198 const struct iphdr *iph = ip_hdr(skb);
1199 struct mfc_cache *c;
1203 spin_lock_bh(&mfc_unres_lock);
1204 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
1205 if (c->mfc_mcastgrp == iph->daddr &&
1206 c->mfc_origin == iph->saddr) {
1213 /* Create a new entry if allowable */
1214 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1215 (c = ipmr_cache_alloc_unres()) == NULL) {
1216 spin_unlock_bh(&mfc_unres_lock);
1222 /* Fill in the new cache entry */
1224 c->mfc_origin = iph->saddr;
1225 c->mfc_mcastgrp = iph->daddr;
1227 /* Reflect first query at mrouted. */
1228 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1230 /* If the report failed throw the cache entry
1233 spin_unlock_bh(&mfc_unres_lock);
1240 atomic_inc(&mrt->cache_resolve_queue_len);
1241 list_add(&c->list, &mrt->mfc_unres_queue);
1242 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1244 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1245 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
1248 /* See if we can append the packet */
1249 if (c->mfc_un.unres.unresolved.qlen > 3) {
1255 skb->skb_iif = dev->ifindex;
1257 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
1261 spin_unlock_bh(&mfc_unres_lock);
1265 /* MFC cache manipulation by user space mroute daemon */
1267 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1269 struct net *net = read_pnet(&mrt->net);
1270 struct mfc_cache *c;
1272 /* The entries are added/deleted only under RTNL */
1274 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1275 mfc->mfcc_mcastgrp.s_addr, parent);
1279 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1280 list_del_rcu(&c->list);
1281 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
1282 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1288 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1289 struct mfcctl *mfc, int mrtsock, int parent)
1291 struct mfc_cache *uc, *c;
1295 if (mfc->mfcc_parent >= MAXVIFS)
1298 /* The entries are added/deleted only under RTNL */
1300 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1301 mfc->mfcc_mcastgrp.s_addr, parent);
1304 write_lock_bh(&mrt_lock);
1305 c->mfc_parent = mfc->mfcc_parent;
1306 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1308 c->mfc_flags |= MFC_STATIC;
1309 write_unlock_bh(&mrt_lock);
1310 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1312 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1316 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1317 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1320 c = ipmr_cache_alloc();
1324 c->mfc_origin = mfc->mfcc_origin.s_addr;
1325 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1326 c->mfc_parent = mfc->mfcc_parent;
1327 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
1329 c->mfc_flags |= MFC_STATIC;
1331 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->mnode,
1334 pr_err("ipmr: rhtable insert error %d\n", ret);
1338 list_add_tail_rcu(&c->list, &mrt->mfc_cache_list);
1339 /* Check to see if we resolved a queued list. If so we
1340 * need to send on the frames and tidy up.
1343 spin_lock_bh(&mfc_unres_lock);
1344 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
1345 if (uc->mfc_origin == c->mfc_origin &&
1346 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1347 list_del(&uc->list);
1348 atomic_dec(&mrt->cache_resolve_queue_len);
1353 if (list_empty(&mrt->mfc_unres_queue))
1354 del_timer(&mrt->ipmr_expire_timer);
1355 spin_unlock_bh(&mfc_unres_lock);
1358 ipmr_cache_resolve(net, mrt, uc, c);
1359 ipmr_cache_free(uc);
1361 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
1362 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1366 /* Close the multicast socket, and clear the vif tables etc */
1367 static void mroute_clean_tables(struct mr_table *mrt, bool all)
1369 struct net *net = read_pnet(&mrt->net);
1370 struct mfc_cache *c, *tmp;
1374 /* Shut down all active vif entries */
1375 for (i = 0; i < mrt->maxvif; i++) {
1376 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1378 vif_delete(mrt, i, 0, &list);
1380 unregister_netdevice_many(&list);
1382 /* Wipe the cache */
1383 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1384 if (!all && (c->mfc_flags & MFC_STATIC))
1386 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1387 list_del_rcu(&c->list);
1388 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c,
1390 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1394 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1395 spin_lock_bh(&mfc_unres_lock);
1396 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1398 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1399 ipmr_destroy_unres(mrt, c);
1401 spin_unlock_bh(&mfc_unres_lock);
1405 /* called from ip_ra_control(), before an RCU grace period,
1406 * we dont need to call synchronize_rcu() here
1408 static void mrtsock_destruct(struct sock *sk)
1410 struct net *net = sock_net(sk);
1411 struct mr_table *mrt;
1414 ipmr_for_each_table(mrt, net) {
1415 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1416 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1417 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1418 NETCONFA_MC_FORWARDING,
1419 NETCONFA_IFINDEX_ALL,
1420 net->ipv4.devconf_all);
1421 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1422 mroute_clean_tables(mrt, false);
1427 /* Socket options and virtual interface manipulation. The whole
1428 * virtual interface system is a complete heap, but unfortunately
1429 * that's how BSD mrouted happens to think. Maybe one day with a proper
1430 * MOSPF/PIM router set up we can clean this up.
1433 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1434 unsigned int optlen)
1436 struct net *net = sock_net(sk);
1437 int val, ret = 0, parent = 0;
1438 struct mr_table *mrt;
1443 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1445 if (sk->sk_type != SOCK_RAW ||
1446 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1451 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1456 if (optname != MRT_INIT) {
1457 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1458 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1466 if (optlen != sizeof(int)) {
1470 if (rtnl_dereference(mrt->mroute_sk)) {
1475 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1477 rcu_assign_pointer(mrt->mroute_sk, sk);
1478 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1479 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1480 NETCONFA_MC_FORWARDING,
1481 NETCONFA_IFINDEX_ALL,
1482 net->ipv4.devconf_all);
1486 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1489 ret = ip_ra_control(sk, 0, NULL);
1495 if (optlen != sizeof(vif)) {
1499 if (copy_from_user(&vif, optval, sizeof(vif))) {
1503 if (vif.vifc_vifi >= MAXVIFS) {
1507 if (optname == MRT_ADD_VIF) {
1508 ret = vif_add(net, mrt, &vif,
1509 sk == rtnl_dereference(mrt->mroute_sk));
1511 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1514 /* Manipulate the forwarding caches. These live
1515 * in a sort of kernel/user symbiosis.
1520 case MRT_ADD_MFC_PROXY:
1521 case MRT_DEL_MFC_PROXY:
1522 if (optlen != sizeof(mfc)) {
1526 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1531 parent = mfc.mfcc_parent;
1532 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1533 ret = ipmr_mfc_delete(mrt, &mfc, parent);
1535 ret = ipmr_mfc_add(net, mrt, &mfc,
1536 sk == rtnl_dereference(mrt->mroute_sk),
1539 /* Control PIM assert. */
1541 if (optlen != sizeof(val)) {
1545 if (get_user(val, (int __user *)optval)) {
1549 mrt->mroute_do_assert = val;
1552 if (!ipmr_pimsm_enabled()) {
1556 if (optlen != sizeof(val)) {
1560 if (get_user(val, (int __user *)optval)) {
1566 if (val != mrt->mroute_do_pim) {
1567 mrt->mroute_do_pim = val;
1568 mrt->mroute_do_assert = val;
1572 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1576 if (optlen != sizeof(uval)) {
1580 if (get_user(uval, (u32 __user *)optval)) {
1585 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1588 mrt = ipmr_new_table(net, uval);
1592 raw_sk(sk)->ipmr_table = uval;
1595 /* Spurious command, or MRT_VERSION which you cannot set. */
1604 /* Getsock opt support for the multicast routing system. */
1605 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1609 struct net *net = sock_net(sk);
1610 struct mr_table *mrt;
1612 if (sk->sk_type != SOCK_RAW ||
1613 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1616 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1625 if (!ipmr_pimsm_enabled())
1626 return -ENOPROTOOPT;
1627 val = mrt->mroute_do_pim;
1630 val = mrt->mroute_do_assert;
1633 return -ENOPROTOOPT;
1636 if (get_user(olr, optlen))
1638 olr = min_t(unsigned int, olr, sizeof(int));
1641 if (put_user(olr, optlen))
1643 if (copy_to_user(optval, &val, olr))
1648 /* The IP multicast ioctl support routines. */
1649 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1651 struct sioc_sg_req sr;
1652 struct sioc_vif_req vr;
1653 struct vif_device *vif;
1654 struct mfc_cache *c;
1655 struct net *net = sock_net(sk);
1656 struct mr_table *mrt;
1658 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1664 if (copy_from_user(&vr, arg, sizeof(vr)))
1666 if (vr.vifi >= mrt->maxvif)
1668 read_lock(&mrt_lock);
1669 vif = &mrt->vif_table[vr.vifi];
1670 if (VIF_EXISTS(mrt, vr.vifi)) {
1671 vr.icount = vif->pkt_in;
1672 vr.ocount = vif->pkt_out;
1673 vr.ibytes = vif->bytes_in;
1674 vr.obytes = vif->bytes_out;
1675 read_unlock(&mrt_lock);
1677 if (copy_to_user(arg, &vr, sizeof(vr)))
1681 read_unlock(&mrt_lock);
1682 return -EADDRNOTAVAIL;
1684 if (copy_from_user(&sr, arg, sizeof(sr)))
1688 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1690 sr.pktcnt = c->mfc_un.res.pkt;
1691 sr.bytecnt = c->mfc_un.res.bytes;
1692 sr.wrong_if = c->mfc_un.res.wrong_if;
1695 if (copy_to_user(arg, &sr, sizeof(sr)))
1700 return -EADDRNOTAVAIL;
1702 return -ENOIOCTLCMD;
1706 #ifdef CONFIG_COMPAT
1707 struct compat_sioc_sg_req {
1710 compat_ulong_t pktcnt;
1711 compat_ulong_t bytecnt;
1712 compat_ulong_t wrong_if;
1715 struct compat_sioc_vif_req {
1716 vifi_t vifi; /* Which iface */
1717 compat_ulong_t icount;
1718 compat_ulong_t ocount;
1719 compat_ulong_t ibytes;
1720 compat_ulong_t obytes;
1723 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1725 struct compat_sioc_sg_req sr;
1726 struct compat_sioc_vif_req vr;
1727 struct vif_device *vif;
1728 struct mfc_cache *c;
1729 struct net *net = sock_net(sk);
1730 struct mr_table *mrt;
1732 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1738 if (copy_from_user(&vr, arg, sizeof(vr)))
1740 if (vr.vifi >= mrt->maxvif)
1742 read_lock(&mrt_lock);
1743 vif = &mrt->vif_table[vr.vifi];
1744 if (VIF_EXISTS(mrt, vr.vifi)) {
1745 vr.icount = vif->pkt_in;
1746 vr.ocount = vif->pkt_out;
1747 vr.ibytes = vif->bytes_in;
1748 vr.obytes = vif->bytes_out;
1749 read_unlock(&mrt_lock);
1751 if (copy_to_user(arg, &vr, sizeof(vr)))
1755 read_unlock(&mrt_lock);
1756 return -EADDRNOTAVAIL;
1758 if (copy_from_user(&sr, arg, sizeof(sr)))
1762 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1764 sr.pktcnt = c->mfc_un.res.pkt;
1765 sr.bytecnt = c->mfc_un.res.bytes;
1766 sr.wrong_if = c->mfc_un.res.wrong_if;
1769 if (copy_to_user(arg, &sr, sizeof(sr)))
1774 return -EADDRNOTAVAIL;
1776 return -ENOIOCTLCMD;
1781 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1783 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1784 struct net *net = dev_net(dev);
1785 struct mr_table *mrt;
1786 struct vif_device *v;
1789 if (event != NETDEV_UNREGISTER)
1792 ipmr_for_each_table(mrt, net) {
1793 v = &mrt->vif_table[0];
1794 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1796 vif_delete(mrt, ct, 1, NULL);
1802 static struct notifier_block ip_mr_notifier = {
1803 .notifier_call = ipmr_device_event,
1806 /* Encapsulate a packet by attaching a valid IPIP header to it.
1807 * This avoids tunnel drivers and other mess and gives us the speed so
1808 * important for multicast video.
1810 static void ip_encap(struct net *net, struct sk_buff *skb,
1811 __be32 saddr, __be32 daddr)
1814 const struct iphdr *old_iph = ip_hdr(skb);
1816 skb_push(skb, sizeof(struct iphdr));
1817 skb->transport_header = skb->network_header;
1818 skb_reset_network_header(skb);
1822 iph->tos = old_iph->tos;
1823 iph->ttl = old_iph->ttl;
1827 iph->protocol = IPPROTO_IPIP;
1829 iph->tot_len = htons(skb->len);
1830 ip_select_ident(net, skb, NULL);
1833 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1837 static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1838 struct sk_buff *skb)
1840 struct ip_options *opt = &(IPCB(skb)->opt);
1842 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1843 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
1845 if (unlikely(opt->optlen))
1846 ip_forward_options(skb);
1848 return dst_output(net, sk, skb);
1851 /* Processing handlers for ipmr_forward */
1853 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1854 struct sk_buff *skb, struct mfc_cache *c, int vifi)
1856 const struct iphdr *iph = ip_hdr(skb);
1857 struct vif_device *vif = &mrt->vif_table[vifi];
1858 struct net_device *dev;
1866 if (vif->flags & VIFF_REGISTER) {
1868 vif->bytes_out += skb->len;
1869 vif->dev->stats.tx_bytes += skb->len;
1870 vif->dev->stats.tx_packets++;
1871 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1875 if (vif->flags & VIFF_TUNNEL) {
1876 rt = ip_route_output_ports(net, &fl4, NULL,
1877 vif->remote, vif->local,
1880 RT_TOS(iph->tos), vif->link);
1883 encap = sizeof(struct iphdr);
1885 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1888 RT_TOS(iph->tos), vif->link);
1895 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1896 /* Do not fragment multicasts. Alas, IPv4 does not
1897 * allow to send ICMP, so that packets will disappear
1900 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1905 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1907 if (skb_cow(skb, encap)) {
1913 vif->bytes_out += skb->len;
1916 skb_dst_set(skb, &rt->dst);
1917 ip_decrease_ttl(ip_hdr(skb));
1919 /* FIXME: forward and output firewalls used to be called here.
1920 * What do we do with netfilter? -- RR
1922 if (vif->flags & VIFF_TUNNEL) {
1923 ip_encap(net, skb, vif->local, vif->remote);
1924 /* FIXME: extra output firewall step used to be here. --RR */
1925 vif->dev->stats.tx_packets++;
1926 vif->dev->stats.tx_bytes += skb->len;
1929 IPCB(skb)->flags |= IPSKB_FORWARDED;
1931 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1932 * not only before forwarding, but after forwarding on all output
1933 * interfaces. It is clear, if mrouter runs a multicasting
1934 * program, it should receive packets not depending to what interface
1935 * program is joined.
1936 * If we will not make it, the program will have to join on all
1937 * interfaces. On the other hand, multihoming host (or router, but
1938 * not mrouter) cannot join to more than one interface - it will
1939 * result in receiving multiple packets.
1941 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1942 net, NULL, skb, skb->dev, dev,
1943 ipmr_forward_finish);
1950 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1954 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1955 if (mrt->vif_table[ct].dev == dev)
1961 /* "local" means that we should preserve one skb (for local delivery) */
1962 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1963 struct net_device *dev, struct sk_buff *skb,
1964 struct mfc_cache *cache, int local)
1966 int true_vifi = ipmr_find_vif(mrt, dev);
1970 vif = cache->mfc_parent;
1971 cache->mfc_un.res.pkt++;
1972 cache->mfc_un.res.bytes += skb->len;
1973 cache->mfc_un.res.lastuse = jiffies;
1975 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1976 struct mfc_cache *cache_proxy;
1978 /* For an (*,G) entry, we only check that the incomming
1979 * interface is part of the static tree.
1981 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1983 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1987 /* Wrong interface: drop packet and (maybe) send PIM assert. */
1988 if (mrt->vif_table[vif].dev != dev) {
1989 if (rt_is_output_route(skb_rtable(skb))) {
1990 /* It is our own packet, looped back.
1991 * Very complicated situation...
1993 * The best workaround until routing daemons will be
1994 * fixed is not to redistribute packet, if it was
1995 * send through wrong interface. It means, that
1996 * multicast applications WILL NOT work for
1997 * (S,G), which have default multicast route pointing
1998 * to wrong oif. In any case, it is not a good
1999 * idea to use multicasting applications on router.
2004 cache->mfc_un.res.wrong_if++;
2006 if (true_vifi >= 0 && mrt->mroute_do_assert &&
2007 /* pimsm uses asserts, when switching from RPT to SPT,
2008 * so that we cannot check that packet arrived on an oif.
2009 * It is bad, but otherwise we would need to move pretty
2010 * large chunk of pimd to kernel. Ough... --ANK
2012 (mrt->mroute_do_pim ||
2013 cache->mfc_un.res.ttls[true_vifi] < 255) &&
2015 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
2016 cache->mfc_un.res.last_assert = jiffies;
2017 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
2023 mrt->vif_table[vif].pkt_in++;
2024 mrt->vif_table[vif].bytes_in += skb->len;
2026 /* Forward the frame */
2027 if (cache->mfc_origin == htonl(INADDR_ANY) &&
2028 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
2029 if (true_vifi >= 0 &&
2030 true_vifi != cache->mfc_parent &&
2032 cache->mfc_un.res.ttls[cache->mfc_parent]) {
2033 /* It's an (*,*) entry and the packet is not coming from
2034 * the upstream: forward the packet to the upstream
2037 psend = cache->mfc_parent;
2042 for (ct = cache->mfc_un.res.maxvif - 1;
2043 ct >= cache->mfc_un.res.minvif; ct--) {
2044 /* For (*,G) entry, don't forward to the incoming interface */
2045 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
2047 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
2049 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2052 ipmr_queue_xmit(net, mrt, skb2, cache,
2061 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2064 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
2066 ipmr_queue_xmit(net, mrt, skb, cache, psend);
2076 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
2078 struct rtable *rt = skb_rtable(skb);
2079 struct iphdr *iph = ip_hdr(skb);
2080 struct flowi4 fl4 = {
2081 .daddr = iph->daddr,
2082 .saddr = iph->saddr,
2083 .flowi4_tos = RT_TOS(iph->tos),
2084 .flowi4_oif = (rt_is_output_route(rt) ?
2085 skb->dev->ifindex : 0),
2086 .flowi4_iif = (rt_is_output_route(rt) ?
2089 .flowi4_mark = skb->mark,
2091 struct mr_table *mrt;
2094 err = ipmr_fib_lookup(net, &fl4, &mrt);
2096 return ERR_PTR(err);
2100 /* Multicast packets for forwarding arrive here
2101 * Called with rcu_read_lock();
2103 int ip_mr_input(struct sk_buff *skb)
2105 struct mfc_cache *cache;
2106 struct net *net = dev_net(skb->dev);
2107 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
2108 struct mr_table *mrt;
2109 struct net_device *dev;
2111 /* skb->dev passed in is the loX master dev for vrfs.
2112 * As there are no vifs associated with loopback devices,
2113 * get the proper interface that does have a vif associated with it.
2116 if (netif_is_l3_master(skb->dev)) {
2117 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2124 /* Packet is looped back after forward, it should not be
2125 * forwarded second time, but still can be delivered locally.
2127 if (IPCB(skb)->flags & IPSKB_FORWARDED)
2130 mrt = ipmr_rt_fib_lookup(net, skb);
2133 return PTR_ERR(mrt);
2136 if (IPCB(skb)->opt.router_alert) {
2137 if (ip_call_ra_chain(skb))
2139 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2140 /* IGMPv1 (and broken IGMPv2 implementations sort of
2141 * Cisco IOS <= 11.2(8)) do not put router alert
2142 * option to IGMP packets destined to routable
2143 * groups. It is very bad, because it means
2144 * that we can forward NO IGMP messages.
2146 struct sock *mroute_sk;
2148 mroute_sk = rcu_dereference(mrt->mroute_sk);
2151 raw_rcv(mroute_sk, skb);
2157 /* already under rcu_read_lock() */
2158 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
2160 int vif = ipmr_find_vif(mrt, dev);
2163 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2167 /* No usable cache entry */
2172 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2173 ip_local_deliver(skb);
2179 read_lock(&mrt_lock);
2180 vif = ipmr_find_vif(mrt, dev);
2182 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
2183 read_unlock(&mrt_lock);
2187 read_unlock(&mrt_lock);
2192 read_lock(&mrt_lock);
2193 ip_mr_forward(net, mrt, dev, skb, cache, local);
2194 read_unlock(&mrt_lock);
2197 return ip_local_deliver(skb);
2203 return ip_local_deliver(skb);
2208 #ifdef CONFIG_IP_PIMSM_V1
2209 /* Handle IGMP messages of PIMv1 */
2210 int pim_rcv_v1(struct sk_buff *skb)
2212 struct igmphdr *pim;
2213 struct net *net = dev_net(skb->dev);
2214 struct mr_table *mrt;
2216 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2219 pim = igmp_hdr(skb);
2221 mrt = ipmr_rt_fib_lookup(net, skb);
2224 if (!mrt->mroute_do_pim ||
2225 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2228 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2236 #ifdef CONFIG_IP_PIMSM_V2
2237 static int pim_rcv(struct sk_buff *skb)
2239 struct pimreghdr *pim;
2240 struct net *net = dev_net(skb->dev);
2241 struct mr_table *mrt;
2243 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2246 pim = (struct pimreghdr *)skb_transport_header(skb);
2247 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
2248 (pim->flags & PIM_NULL_REGISTER) ||
2249 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2250 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2253 mrt = ipmr_rt_fib_lookup(net, skb);
2256 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2264 static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2265 struct mfc_cache *c, struct rtmsg *rtm)
2267 struct rta_mfc_stats mfcs;
2268 struct nlattr *mp_attr;
2269 struct rtnexthop *nhp;
2270 unsigned long lastuse;
2273 /* If cache is unresolved, don't try to parse IIF and OIF */
2274 if (c->mfc_parent >= MAXVIFS) {
2275 rtm->rtm_flags |= RTNH_F_UNRESOLVED;
2279 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2280 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2283 if (c->mfc_flags & MFC_OFFLOAD)
2284 rtm->rtm_flags |= RTNH_F_OFFLOAD;
2286 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2289 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
2290 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
2291 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2292 nla_nest_cancel(skb, mp_attr);
2296 nhp->rtnh_flags = 0;
2297 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
2298 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
2299 nhp->rtnh_len = sizeof(*nhp);
2303 nla_nest_end(skb, mp_attr);
2305 lastuse = READ_ONCE(c->mfc_un.res.lastuse);
2306 lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
2308 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2309 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2310 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2311 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
2312 nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
2316 rtm->rtm_type = RTN_MULTICAST;
2320 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2321 __be32 saddr, __be32 daddr,
2322 struct rtmsg *rtm, u32 portid)
2324 struct mfc_cache *cache;
2325 struct mr_table *mrt;
2328 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2333 cache = ipmr_cache_find(mrt, saddr, daddr);
2334 if (!cache && skb->dev) {
2335 int vif = ipmr_find_vif(mrt, skb->dev);
2338 cache = ipmr_cache_find_any(mrt, daddr, vif);
2341 struct sk_buff *skb2;
2343 struct net_device *dev;
2347 read_lock(&mrt_lock);
2349 vif = ipmr_find_vif(mrt, dev);
2351 read_unlock(&mrt_lock);
2355 skb2 = skb_clone(skb, GFP_ATOMIC);
2357 read_unlock(&mrt_lock);
2362 NETLINK_CB(skb2).portid = portid;
2363 skb_push(skb2, sizeof(struct iphdr));
2364 skb_reset_network_header(skb2);
2366 iph->ihl = sizeof(struct iphdr) >> 2;
2370 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
2371 read_unlock(&mrt_lock);
2376 read_lock(&mrt_lock);
2377 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
2378 read_unlock(&mrt_lock);
2383 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2384 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2387 struct nlmsghdr *nlh;
2391 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2395 rtm = nlmsg_data(nlh);
2396 rtm->rtm_family = RTNL_FAMILY_IPMR;
2397 rtm->rtm_dst_len = 32;
2398 rtm->rtm_src_len = 32;
2400 rtm->rtm_table = mrt->id;
2401 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2402 goto nla_put_failure;
2403 rtm->rtm_type = RTN_MULTICAST;
2404 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2405 if (c->mfc_flags & MFC_STATIC)
2406 rtm->rtm_protocol = RTPROT_STATIC;
2408 rtm->rtm_protocol = RTPROT_MROUTED;
2411 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2412 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2413 goto nla_put_failure;
2414 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2415 /* do not break the dump if cache is unresolved */
2416 if (err < 0 && err != -ENOENT)
2417 goto nla_put_failure;
2419 nlmsg_end(skb, nlh);
2423 nlmsg_cancel(skb, nlh);
2427 static size_t mroute_msgsize(bool unresolved, int maxvif)
2430 NLMSG_ALIGN(sizeof(struct rtmsg))
2431 + nla_total_size(4) /* RTA_TABLE */
2432 + nla_total_size(4) /* RTA_SRC */
2433 + nla_total_size(4) /* RTA_DST */
2438 + nla_total_size(4) /* RTA_IIF */
2439 + nla_total_size(0) /* RTA_MULTIPATH */
2440 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2442 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
2448 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2451 struct net *net = read_pnet(&mrt->net);
2452 struct sk_buff *skb;
2455 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2460 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2464 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2470 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2473 static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2476 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2477 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2478 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2479 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2480 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2481 /* IPMRA_CREPORT_PKT */
2482 + nla_total_size(payloadlen)
2488 static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2490 struct net *net = read_pnet(&mrt->net);
2491 struct nlmsghdr *nlh;
2492 struct rtgenmsg *rtgenm;
2493 struct igmpmsg *msg;
2494 struct sk_buff *skb;
2498 payloadlen = pkt->len - sizeof(struct igmpmsg);
2499 msg = (struct igmpmsg *)skb_network_header(pkt);
2501 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2505 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2506 sizeof(struct rtgenmsg), 0);
2509 rtgenm = nlmsg_data(nlh);
2510 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2511 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2512 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2513 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2514 msg->im_src.s_addr) ||
2515 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2516 msg->im_dst.s_addr))
2517 goto nla_put_failure;
2519 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2520 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2521 nla_data(nla), payloadlen))
2522 goto nla_put_failure;
2524 nlmsg_end(skb, nlh);
2526 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2530 nlmsg_cancel(skb, nlh);
2533 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2536 static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2537 struct netlink_ext_ack *extack)
2539 struct net *net = sock_net(in_skb->sk);
2540 struct nlattr *tb[RTA_MAX + 1];
2541 struct sk_buff *skb = NULL;
2542 struct mfc_cache *cache;
2543 struct mr_table *mrt;
2549 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2550 rtm_ipv4_policy, extack);
2554 rtm = nlmsg_data(nlh);
2556 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2557 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2558 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2560 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
2566 /* entries are added/deleted only under RTNL */
2568 cache = ipmr_cache_find(mrt, src, grp);
2575 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2581 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2582 nlh->nlmsg_seq, cache,
2587 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2597 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2599 struct net *net = sock_net(skb->sk);
2600 struct mr_table *mrt;
2601 struct mfc_cache *mfc;
2602 unsigned int t = 0, s_t;
2603 unsigned int e = 0, s_e;
2609 ipmr_for_each_table(mrt, net) {
2612 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) {
2615 if (ipmr_fill_mroute(mrt, skb,
2616 NETLINK_CB(cb->skb).portid,
2627 spin_lock_bh(&mfc_unres_lock);
2628 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2631 if (ipmr_fill_mroute(mrt, skb,
2632 NETLINK_CB(cb->skb).portid,
2636 spin_unlock_bh(&mfc_unres_lock);
2642 spin_unlock_bh(&mfc_unres_lock);
2657 static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2658 [RTA_SRC] = { .type = NLA_U32 },
2659 [RTA_DST] = { .type = NLA_U32 },
2660 [RTA_IIF] = { .type = NLA_U32 },
2661 [RTA_TABLE] = { .type = NLA_U32 },
2662 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2665 static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2667 switch (rtm_protocol) {
2669 case RTPROT_MROUTED:
2675 static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2677 struct rtnexthop *rtnh = nla_data(nla);
2678 int remaining = nla_len(nla), vifi = 0;
2680 while (rtnh_ok(rtnh, remaining)) {
2681 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2682 if (++vifi == MAXVIFS)
2684 rtnh = rtnh_next(rtnh, &remaining);
2687 return remaining > 0 ? -EINVAL : vifi;
2690 /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2691 static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2692 struct mfcctl *mfcc, int *mrtsock,
2693 struct mr_table **mrtret,
2694 struct netlink_ext_ack *extack)
2696 struct net_device *dev = NULL;
2697 u32 tblid = RT_TABLE_DEFAULT;
2698 struct mr_table *mrt;
2699 struct nlattr *attr;
2703 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
2707 rtm = nlmsg_data(nlh);
2710 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2711 rtm->rtm_type != RTN_MULTICAST ||
2712 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2713 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2716 memset(mfcc, 0, sizeof(*mfcc));
2717 mfcc->mfcc_parent = -1;
2719 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2720 switch (nla_type(attr)) {
2722 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2725 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2728 dev = __dev_get_by_index(net, nla_get_u32(attr));
2735 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2744 tblid = nla_get_u32(attr);
2748 mrt = ipmr_get_table(net, tblid);
2754 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2756 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2762 /* takes care of both newroute and delroute */
2763 static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2764 struct netlink_ext_ack *extack)
2766 struct net *net = sock_net(skb->sk);
2767 int ret, mrtsock, parent;
2768 struct mr_table *tbl;
2773 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
2777 parent = ret ? mfcc.mfcc_parent : -1;
2778 if (nlh->nlmsg_type == RTM_NEWROUTE)
2779 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2781 return ipmr_mfc_delete(tbl, &mfcc, parent);
2784 static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2786 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2788 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2789 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2790 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2791 mrt->mroute_reg_vif_num) ||
2792 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2793 mrt->mroute_do_assert) ||
2794 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim))
2800 static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2802 struct nlattr *vif_nest;
2803 struct vif_device *vif;
2805 /* if the VIF doesn't exist just continue */
2806 if (!VIF_EXISTS(mrt, vifid))
2809 vif = &mrt->vif_table[vifid];
2810 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2813 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2814 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2815 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2816 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2818 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2820 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2822 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2824 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2825 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2826 nla_nest_cancel(skb, vif_nest);
2829 nla_nest_end(skb, vif_nest);
2834 static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2836 struct net *net = sock_net(skb->sk);
2837 struct nlmsghdr *nlh = NULL;
2838 unsigned int t = 0, s_t;
2839 unsigned int e = 0, s_e;
2840 struct mr_table *mrt;
2845 ipmr_for_each_table(mrt, net) {
2846 struct nlattr *vifs, *af;
2847 struct ifinfomsg *hdr;
2852 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2853 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2854 sizeof(*hdr), NLM_F_MULTI);
2858 hdr = nlmsg_data(nlh);
2859 memset(hdr, 0, sizeof(*hdr));
2860 hdr->ifi_family = RTNL_FAMILY_IPMR;
2862 af = nla_nest_start(skb, IFLA_AF_SPEC);
2864 nlmsg_cancel(skb, nlh);
2868 if (!ipmr_fill_table(mrt, skb)) {
2869 nlmsg_cancel(skb, nlh);
2873 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2875 nla_nest_end(skb, af);
2876 nlmsg_end(skb, nlh);
2879 for (i = 0; i < mrt->maxvif; i++) {
2882 if (!ipmr_fill_vif(mrt, i, skb)) {
2883 nla_nest_end(skb, vifs);
2884 nla_nest_end(skb, af);
2885 nlmsg_end(skb, nlh);
2893 nla_nest_end(skb, vifs);
2894 nla_nest_end(skb, af);
2895 nlmsg_end(skb, nlh);
2907 #ifdef CONFIG_PROC_FS
2908 /* The /proc interfaces to multicast routing :
2909 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2911 struct ipmr_vif_iter {
2912 struct seq_net_private p;
2913 struct mr_table *mrt;
2917 static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2918 struct ipmr_vif_iter *iter,
2921 struct mr_table *mrt = iter->mrt;
2923 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2924 if (!VIF_EXISTS(mrt, iter->ct))
2927 return &mrt->vif_table[iter->ct];
2932 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2933 __acquires(mrt_lock)
2935 struct ipmr_vif_iter *iter = seq->private;
2936 struct net *net = seq_file_net(seq);
2937 struct mr_table *mrt;
2939 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2941 return ERR_PTR(-ENOENT);
2945 read_lock(&mrt_lock);
2946 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
2950 static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2952 struct ipmr_vif_iter *iter = seq->private;
2953 struct net *net = seq_file_net(seq);
2954 struct mr_table *mrt = iter->mrt;
2957 if (v == SEQ_START_TOKEN)
2958 return ipmr_vif_seq_idx(net, iter, 0);
2960 while (++iter->ct < mrt->maxvif) {
2961 if (!VIF_EXISTS(mrt, iter->ct))
2963 return &mrt->vif_table[iter->ct];
2968 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2969 __releases(mrt_lock)
2971 read_unlock(&mrt_lock);
2974 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2976 struct ipmr_vif_iter *iter = seq->private;
2977 struct mr_table *mrt = iter->mrt;
2979 if (v == SEQ_START_TOKEN) {
2981 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2983 const struct vif_device *vif = v;
2984 const char *name = vif->dev ? vif->dev->name : "none";
2987 "%2zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2988 vif - mrt->vif_table,
2989 name, vif->bytes_in, vif->pkt_in,
2990 vif->bytes_out, vif->pkt_out,
2991 vif->flags, vif->local, vif->remote);
2996 static const struct seq_operations ipmr_vif_seq_ops = {
2997 .start = ipmr_vif_seq_start,
2998 .next = ipmr_vif_seq_next,
2999 .stop = ipmr_vif_seq_stop,
3000 .show = ipmr_vif_seq_show,
3003 static int ipmr_vif_open(struct inode *inode, struct file *file)
3005 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
3006 sizeof(struct ipmr_vif_iter));
3009 static const struct file_operations ipmr_vif_fops = {
3010 .owner = THIS_MODULE,
3011 .open = ipmr_vif_open,
3013 .llseek = seq_lseek,
3014 .release = seq_release_net,
3017 struct ipmr_mfc_iter {
3018 struct seq_net_private p;
3019 struct mr_table *mrt;
3020 struct list_head *cache;
3023 static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
3024 struct ipmr_mfc_iter *it, loff_t pos)
3026 struct mr_table *mrt = it->mrt;
3027 struct mfc_cache *mfc;
3030 it->cache = &mrt->mfc_cache_list;
3031 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
3036 spin_lock_bh(&mfc_unres_lock);
3037 it->cache = &mrt->mfc_unres_queue;
3038 list_for_each_entry(mfc, it->cache, list)
3041 spin_unlock_bh(&mfc_unres_lock);
3048 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
3050 struct ipmr_mfc_iter *it = seq->private;
3051 struct net *net = seq_file_net(seq);
3052 struct mr_table *mrt;
3054 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
3056 return ERR_PTR(-ENOENT);
3060 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
3064 static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3066 struct ipmr_mfc_iter *it = seq->private;
3067 struct net *net = seq_file_net(seq);
3068 struct mr_table *mrt = it->mrt;
3069 struct mfc_cache *mfc = v;
3073 if (v == SEQ_START_TOKEN)
3074 return ipmr_mfc_seq_idx(net, seq->private, 0);
3076 if (mfc->list.next != it->cache)
3077 return list_entry(mfc->list.next, struct mfc_cache, list);
3079 if (it->cache == &mrt->mfc_unres_queue)
3082 /* exhausted cache_array, show unresolved */
3084 it->cache = &mrt->mfc_unres_queue;
3086 spin_lock_bh(&mfc_unres_lock);
3087 if (!list_empty(it->cache))
3088 return list_first_entry(it->cache, struct mfc_cache, list);
3091 spin_unlock_bh(&mfc_unres_lock);
3097 static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
3099 struct ipmr_mfc_iter *it = seq->private;
3100 struct mr_table *mrt = it->mrt;
3102 if (it->cache == &mrt->mfc_unres_queue)
3103 spin_unlock_bh(&mfc_unres_lock);
3104 else if (it->cache == &mrt->mfc_cache_list)
3108 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
3112 if (v == SEQ_START_TOKEN) {
3114 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
3116 const struct mfc_cache *mfc = v;
3117 const struct ipmr_mfc_iter *it = seq->private;
3118 const struct mr_table *mrt = it->mrt;
3120 seq_printf(seq, "%08X %08X %-3hd",
3121 (__force u32) mfc->mfc_mcastgrp,
3122 (__force u32) mfc->mfc_origin,
3125 if (it->cache != &mrt->mfc_unres_queue) {
3126 seq_printf(seq, " %8lu %8lu %8lu",
3127 mfc->mfc_un.res.pkt,
3128 mfc->mfc_un.res.bytes,
3129 mfc->mfc_un.res.wrong_if);
3130 for (n = mfc->mfc_un.res.minvif;
3131 n < mfc->mfc_un.res.maxvif; n++) {
3132 if (VIF_EXISTS(mrt, n) &&
3133 mfc->mfc_un.res.ttls[n] < 255)
3136 n, mfc->mfc_un.res.ttls[n]);
3139 /* unresolved mfc_caches don't contain
3140 * pkt, bytes and wrong_if values
3142 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
3144 seq_putc(seq, '\n');
3149 static const struct seq_operations ipmr_mfc_seq_ops = {
3150 .start = ipmr_mfc_seq_start,
3151 .next = ipmr_mfc_seq_next,
3152 .stop = ipmr_mfc_seq_stop,
3153 .show = ipmr_mfc_seq_show,
3156 static int ipmr_mfc_open(struct inode *inode, struct file *file)
3158 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
3159 sizeof(struct ipmr_mfc_iter));
3162 static const struct file_operations ipmr_mfc_fops = {
3163 .owner = THIS_MODULE,
3164 .open = ipmr_mfc_open,
3166 .llseek = seq_lseek,
3167 .release = seq_release_net,
3171 #ifdef CONFIG_IP_PIMSM_V2
3172 static const struct net_protocol pim_protocol = {
3178 static unsigned int ipmr_seq_read(struct net *net)
3182 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
3185 static int ipmr_dump(struct net *net, struct notifier_block *nb)
3187 struct mr_table *mrt;
3190 err = ipmr_rules_dump(net, nb);
3194 ipmr_for_each_table(mrt, net) {
3195 struct vif_device *v = &mrt->vif_table[0];
3196 struct mfc_cache *mfc;
3199 /* Notifiy on table VIF entries */
3200 read_lock(&mrt_lock);
3201 for (vifi = 0; vifi < mrt->maxvif; vifi++, v++) {
3205 call_ipmr_vif_entry_notifier(nb, net, FIB_EVENT_VIF_ADD,
3208 read_unlock(&mrt_lock);
3210 /* Notify on table MFC entries */
3211 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
3212 call_ipmr_mfc_entry_notifier(nb, net,
3213 FIB_EVENT_ENTRY_ADD, mfc,
3220 static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3221 .family = RTNL_FAMILY_IPMR,
3222 .fib_seq_read = ipmr_seq_read,
3223 .fib_dump = ipmr_dump,
3224 .owner = THIS_MODULE,
3227 static int __net_init ipmr_notifier_init(struct net *net)
3229 struct fib_notifier_ops *ops;
3231 net->ipv4.ipmr_seq = 0;
3233 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3235 return PTR_ERR(ops);
3236 net->ipv4.ipmr_notifier_ops = ops;
3241 static void __net_exit ipmr_notifier_exit(struct net *net)
3243 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3244 net->ipv4.ipmr_notifier_ops = NULL;
3247 /* Setup for IP multicast routing */
3248 static int __net_init ipmr_net_init(struct net *net)
3252 err = ipmr_notifier_init(net);
3254 goto ipmr_notifier_fail;
3256 err = ipmr_rules_init(net);
3258 goto ipmr_rules_fail;
3260 #ifdef CONFIG_PROC_FS
3262 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
3264 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
3265 goto proc_cache_fail;
3269 #ifdef CONFIG_PROC_FS
3271 remove_proc_entry("ip_mr_vif", net->proc_net);
3273 ipmr_rules_exit(net);
3276 ipmr_notifier_exit(net);
3281 static void __net_exit ipmr_net_exit(struct net *net)
3283 #ifdef CONFIG_PROC_FS
3284 remove_proc_entry("ip_mr_cache", net->proc_net);
3285 remove_proc_entry("ip_mr_vif", net->proc_net);
3287 ipmr_notifier_exit(net);
3288 ipmr_rules_exit(net);
3291 static struct pernet_operations ipmr_net_ops = {
3292 .init = ipmr_net_init,
3293 .exit = ipmr_net_exit,
3296 int __init ip_mr_init(void)
3300 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3301 sizeof(struct mfc_cache),
3302 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
3305 err = register_pernet_subsys(&ipmr_net_ops);
3307 goto reg_pernet_fail;
3309 err = register_netdevice_notifier(&ip_mr_notifier);
3311 goto reg_notif_fail;
3312 #ifdef CONFIG_IP_PIMSM_V2
3313 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
3314 pr_err("%s: can't add PIM protocol\n", __func__);
3316 goto add_proto_fail;
3319 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
3320 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
3321 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
3322 ipmr_rtm_route, NULL, 0);
3323 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
3324 ipmr_rtm_route, NULL, 0);
3326 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
3327 NULL, ipmr_rtm_dumplink, 0);
3330 #ifdef CONFIG_IP_PIMSM_V2
3332 unregister_netdevice_notifier(&ip_mr_notifier);
3335 unregister_pernet_subsys(&ipmr_net_ops);
3337 kmem_cache_destroy(mrt_cachep);