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/cache.h>
32 #include <linux/capability.h>
33 #include <linux/errno.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>
58 #include <linux/notifier.h>
59 #include <linux/if_arp.h>
60 #include <linux/netfilter_ipv4.h>
61 #include <linux/compat.h>
62 #include <linux/export.h>
63 #include <linux/rhashtable.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>
71 #include <linux/nospec.h>
74 struct fib_rule common;
81 /* Big lock, protecting vif table, mrt cache and mroute socket state.
82 * Note that the changes are semaphored via rtnl_lock.
85 static DEFINE_RWLOCK(mrt_lock);
87 /* Multicast router control variables */
89 /* Special spinlock for queue of unresolved entries */
90 static DEFINE_SPINLOCK(mfc_unres_lock);
92 /* We return to original Alan's scheme. Hash table of resolved
93 * entries is changed only in process context and protected
94 * with weak lock mrt_lock. Queue of unresolved entries is protected
95 * with strong spinlock mfc_unres_lock.
97 * In this case data path is free of exclusive locks at all.
100 static struct kmem_cache *mrt_cachep __ro_after_init;
102 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
103 static void ipmr_free_table(struct mr_table *mrt);
105 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
106 struct net_device *dev, struct sk_buff *skb,
107 struct mfc_cache *cache, int local);
108 static int ipmr_cache_report(struct mr_table *mrt,
109 struct sk_buff *pkt, vifi_t vifi, int assert);
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(struct timer_list *t);
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_mr_table_iter(struct net *net,
121 struct mr_table *mrt)
123 struct mr_table *ret;
126 ret = list_entry_rcu(net->ipv4.mr_tables.next,
127 struct mr_table, list);
129 ret = list_entry_rcu(mrt->list.next,
130 struct mr_table, list);
132 if (&ret->list == &net->ipv4.mr_tables)
137 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
139 struct mr_table *mrt;
141 ipmr_for_each_table(mrt, net) {
148 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
149 struct mr_table **mrt)
152 struct ipmr_result res;
153 struct fib_lookup_arg arg = {
155 .flags = FIB_LOOKUP_NOREF,
158 /* update flow if oif or iif point to device enslaved to l3mdev */
159 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
161 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
162 flowi4_to_flowi(flp4), 0, &arg);
169 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
170 int flags, struct fib_lookup_arg *arg)
172 struct ipmr_result *res = arg->result;
173 struct mr_table *mrt;
175 switch (rule->action) {
178 case FR_ACT_UNREACHABLE:
180 case FR_ACT_PROHIBIT:
182 case FR_ACT_BLACKHOLE:
187 arg->table = fib_rule_get_table(rule, arg);
189 mrt = ipmr_get_table(rule->fr_net, arg->table);
196 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
201 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
205 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
206 struct fib_rule_hdr *frh, struct nlattr **tb,
207 struct netlink_ext_ack *extack)
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 .fill = ipmr_rule_fill,
236 .nlgroup = RTNLGRP_IPV4_RULE,
237 .policy = ipmr_rule_policy,
238 .owner = THIS_MODULE,
241 static int __net_init ipmr_rules_init(struct net *net)
243 struct fib_rules_ops *ops;
244 struct mr_table *mrt;
247 ops = fib_rules_register(&ipmr_rules_ops_template, net);
251 INIT_LIST_HEAD(&net->ipv4.mr_tables);
253 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
259 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
263 net->ipv4.mr_rules_ops = ops;
267 ipmr_free_table(mrt);
269 fib_rules_unregister(ops);
273 static void __net_exit ipmr_rules_exit(struct net *net)
275 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);
286 static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
288 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR);
291 static unsigned int ipmr_rules_seq_read(struct net *net)
293 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
296 bool ipmr_rule_default(const struct fib_rule *rule)
298 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
300 EXPORT_SYMBOL(ipmr_rule_default);
302 #define ipmr_for_each_table(mrt, net) \
303 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
305 static struct mr_table *ipmr_mr_table_iter(struct net *net,
306 struct mr_table *mrt)
309 return net->ipv4.mrt;
313 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
315 return net->ipv4.mrt;
318 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
319 struct mr_table **mrt)
321 *mrt = net->ipv4.mrt;
325 static int __net_init ipmr_rules_init(struct net *net)
327 struct mr_table *mrt;
329 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
336 static void __net_exit ipmr_rules_exit(struct net *net)
339 ipmr_free_table(net->ipv4.mrt);
340 net->ipv4.mrt = NULL;
344 static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
349 static unsigned int ipmr_rules_seq_read(struct net *net)
354 bool ipmr_rule_default(const struct fib_rule *rule)
358 EXPORT_SYMBOL(ipmr_rule_default);
361 static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
364 const struct mfc_cache_cmp_arg *cmparg = arg->key;
365 struct mfc_cache *c = (struct mfc_cache *)ptr;
367 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
368 cmparg->mfc_origin != c->mfc_origin;
371 static const struct rhashtable_params ipmr_rht_params = {
372 .head_offset = offsetof(struct mr_mfc, mnode),
373 .key_offset = offsetof(struct mfc_cache, cmparg),
374 .key_len = sizeof(struct mfc_cache_cmp_arg),
377 .obj_cmpfn = ipmr_hash_cmp,
378 .automatic_shrinking = true,
381 static void ipmr_new_table_set(struct mr_table *mrt,
384 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
385 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
389 static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
390 .mfc_mcastgrp = htonl(INADDR_ANY),
391 .mfc_origin = htonl(INADDR_ANY),
394 static struct mr_table_ops ipmr_mr_table_ops = {
395 .rht_params = &ipmr_rht_params,
396 .cmparg_any = &ipmr_mr_table_ops_cmparg_any,
399 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
401 struct mr_table *mrt;
403 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
404 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
405 return ERR_PTR(-EINVAL);
407 mrt = ipmr_get_table(net, id);
411 return mr_table_alloc(net, id, &ipmr_mr_table_ops,
412 ipmr_expire_process, ipmr_new_table_set);
415 static void ipmr_free_table(struct mr_table *mrt)
417 del_timer_sync(&mrt->ipmr_expire_timer);
418 mroute_clean_tables(mrt, true);
419 rhltable_destroy(&mrt->mfc_hash);
423 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
425 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
427 struct net *net = dev_net(dev);
431 dev = __dev_get_by_name(net, "tunl0");
433 const struct net_device_ops *ops = dev->netdev_ops;
435 struct ip_tunnel_parm p;
437 memset(&p, 0, sizeof(p));
438 p.iph.daddr = v->vifc_rmt_addr.s_addr;
439 p.iph.saddr = v->vifc_lcl_addr.s_addr;
442 p.iph.protocol = IPPROTO_IPIP;
443 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
444 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
446 if (ops->ndo_do_ioctl) {
447 mm_segment_t oldfs = get_fs();
450 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
456 /* Initialize ipmr pimreg/tunnel in_device */
457 static bool ipmr_init_vif_indev(const struct net_device *dev)
459 struct in_device *in_dev;
463 in_dev = __in_dev_get_rtnl(dev);
466 ipv4_devconf_setall(in_dev);
467 neigh_parms_data_state_setall(in_dev->arp_parms);
468 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
473 static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
475 struct net_device *dev;
477 dev = __dev_get_by_name(net, "tunl0");
480 const struct net_device_ops *ops = dev->netdev_ops;
483 struct ip_tunnel_parm p;
485 memset(&p, 0, sizeof(p));
486 p.iph.daddr = v->vifc_rmt_addr.s_addr;
487 p.iph.saddr = v->vifc_lcl_addr.s_addr;
490 p.iph.protocol = IPPROTO_IPIP;
491 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
492 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
494 if (ops->ndo_do_ioctl) {
495 mm_segment_t oldfs = get_fs();
498 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
506 (dev = __dev_get_by_name(net, p.name)) != NULL) {
507 dev->flags |= IFF_MULTICAST;
508 if (!ipmr_init_vif_indev(dev))
510 if (dev_open(dev, NULL))
518 unregister_netdevice(dev);
522 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
523 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
525 struct net *net = dev_net(dev);
526 struct mr_table *mrt;
527 struct flowi4 fl4 = {
528 .flowi4_oif = dev->ifindex,
529 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
530 .flowi4_mark = skb->mark,
534 err = ipmr_fib_lookup(net, &fl4, &mrt);
540 read_lock(&mrt_lock);
541 dev->stats.tx_bytes += skb->len;
542 dev->stats.tx_packets++;
543 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
544 read_unlock(&mrt_lock);
549 static int reg_vif_get_iflink(const struct net_device *dev)
554 static const struct net_device_ops reg_vif_netdev_ops = {
555 .ndo_start_xmit = reg_vif_xmit,
556 .ndo_get_iflink = reg_vif_get_iflink,
559 static void reg_vif_setup(struct net_device *dev)
561 dev->type = ARPHRD_PIMREG;
562 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
563 dev->flags = IFF_NOARP;
564 dev->netdev_ops = ®_vif_netdev_ops;
565 dev->needs_free_netdev = true;
566 dev->features |= NETIF_F_NETNS_LOCAL;
569 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
571 struct net_device *dev;
574 if (mrt->id == RT_TABLE_DEFAULT)
575 sprintf(name, "pimreg");
577 sprintf(name, "pimreg%u", mrt->id);
579 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
584 dev_net_set(dev, net);
586 if (register_netdevice(dev)) {
591 if (!ipmr_init_vif_indev(dev))
593 if (dev_open(dev, NULL))
601 unregister_netdevice(dev);
605 /* called with rcu_read_lock() */
606 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
609 struct net_device *reg_dev = NULL;
612 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
614 * a. packet is really sent to a multicast group
615 * b. packet is not a NULL-REGISTER
616 * c. packet is not truncated
618 if (!ipv4_is_multicast(encap->daddr) ||
619 encap->tot_len == 0 ||
620 ntohs(encap->tot_len) + pimlen > skb->len)
623 read_lock(&mrt_lock);
624 if (mrt->mroute_reg_vif_num >= 0)
625 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
626 read_unlock(&mrt_lock);
631 skb->mac_header = skb->network_header;
632 skb_pull(skb, (u8 *)encap - skb->data);
633 skb_reset_network_header(skb);
634 skb->protocol = htons(ETH_P_IP);
635 skb->ip_summed = CHECKSUM_NONE;
637 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
641 return NET_RX_SUCCESS;
644 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
650 static int call_ipmr_vif_entry_notifiers(struct net *net,
651 enum fib_event_type event_type,
652 struct vif_device *vif,
653 vifi_t vif_index, u32 tb_id)
655 return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
656 vif, vif_index, tb_id,
657 &net->ipv4.ipmr_seq);
660 static int call_ipmr_mfc_entry_notifiers(struct net *net,
661 enum fib_event_type event_type,
662 struct mfc_cache *mfc, u32 tb_id)
664 return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
665 &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
669 * vif_delete - Delete a VIF entry
670 * @notify: Set to 1, if the caller is a notifier_call
672 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
673 struct list_head *head)
675 struct net *net = read_pnet(&mrt->net);
676 struct vif_device *v;
677 struct net_device *dev;
678 struct in_device *in_dev;
680 if (vifi < 0 || vifi >= mrt->maxvif)
681 return -EADDRNOTAVAIL;
683 v = &mrt->vif_table[vifi];
685 if (VIF_EXISTS(mrt, vifi))
686 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
689 write_lock_bh(&mrt_lock);
694 write_unlock_bh(&mrt_lock);
695 return -EADDRNOTAVAIL;
698 if (vifi == mrt->mroute_reg_vif_num)
699 mrt->mroute_reg_vif_num = -1;
701 if (vifi + 1 == mrt->maxvif) {
704 for (tmp = vifi - 1; tmp >= 0; tmp--) {
705 if (VIF_EXISTS(mrt, tmp))
711 write_unlock_bh(&mrt_lock);
713 dev_set_allmulti(dev, -1);
715 in_dev = __in_dev_get_rtnl(dev);
717 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
718 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
719 NETCONFA_MC_FORWARDING,
720 dev->ifindex, &in_dev->cnf);
721 ip_rt_multicast_event(in_dev);
724 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
725 unregister_netdevice_queue(dev, head);
731 static void ipmr_cache_free_rcu(struct rcu_head *head)
733 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
735 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
738 static void ipmr_cache_free(struct mfc_cache *c)
740 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
743 /* Destroy an unresolved cache entry, killing queued skbs
744 * and reporting error to netlink readers.
746 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
748 struct net *net = read_pnet(&mrt->net);
752 atomic_dec(&mrt->cache_resolve_queue_len);
754 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
755 if (ip_hdr(skb)->version == 0) {
756 struct nlmsghdr *nlh = skb_pull(skb,
757 sizeof(struct iphdr));
758 nlh->nlmsg_type = NLMSG_ERROR;
759 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
760 skb_trim(skb, nlh->nlmsg_len);
762 e->error = -ETIMEDOUT;
763 memset(&e->msg, 0, sizeof(e->msg));
765 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
774 /* Timer process for the unresolved queue. */
775 static void ipmr_expire_process(struct timer_list *t)
777 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
778 struct mr_mfc *c, *next;
779 unsigned long expires;
782 if (!spin_trylock(&mfc_unres_lock)) {
783 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
787 if (list_empty(&mrt->mfc_unres_queue))
793 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
794 if (time_after(c->mfc_un.unres.expires, now)) {
795 unsigned long interval = c->mfc_un.unres.expires - now;
796 if (interval < expires)
802 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
803 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
806 if (!list_empty(&mrt->mfc_unres_queue))
807 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
810 spin_unlock(&mfc_unres_lock);
813 /* Fill oifs list. It is called under write locked mrt_lock. */
814 static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
819 cache->mfc_un.res.minvif = MAXVIFS;
820 cache->mfc_un.res.maxvif = 0;
821 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
823 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
824 if (VIF_EXISTS(mrt, vifi) &&
825 ttls[vifi] && ttls[vifi] < 255) {
826 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
827 if (cache->mfc_un.res.minvif > vifi)
828 cache->mfc_un.res.minvif = vifi;
829 if (cache->mfc_un.res.maxvif <= vifi)
830 cache->mfc_un.res.maxvif = vifi + 1;
833 cache->mfc_un.res.lastuse = jiffies;
836 static int vif_add(struct net *net, struct mr_table *mrt,
837 struct vifctl *vifc, int mrtsock)
839 struct netdev_phys_item_id ppid = { };
840 int vifi = vifc->vifc_vifi;
841 struct vif_device *v = &mrt->vif_table[vifi];
842 struct net_device *dev;
843 struct in_device *in_dev;
847 if (VIF_EXISTS(mrt, vifi))
850 switch (vifc->vifc_flags) {
852 if (!ipmr_pimsm_enabled())
854 /* Special Purpose VIF in PIM
855 * All the packets will be sent to the daemon
857 if (mrt->mroute_reg_vif_num >= 0)
859 dev = ipmr_reg_vif(net, mrt);
862 err = dev_set_allmulti(dev, 1);
864 unregister_netdevice(dev);
870 dev = ipmr_new_tunnel(net, vifc);
873 err = dev_set_allmulti(dev, 1);
875 ipmr_del_tunnel(dev, vifc);
880 case VIFF_USE_IFINDEX:
882 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
883 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
884 if (dev && !__in_dev_get_rtnl(dev)) {
886 return -EADDRNOTAVAIL;
889 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
892 return -EADDRNOTAVAIL;
893 err = dev_set_allmulti(dev, 1);
903 in_dev = __in_dev_get_rtnl(dev);
906 return -EADDRNOTAVAIL;
908 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
909 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
910 dev->ifindex, &in_dev->cnf);
911 ip_rt_multicast_event(in_dev);
913 /* Fill in the VIF structures */
914 vif_device_init(v, dev, vifc->vifc_rate_limit,
915 vifc->vifc_threshold,
916 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
917 (VIFF_TUNNEL | VIFF_REGISTER));
919 err = dev_get_port_parent_id(dev, &ppid, true);
921 memcpy(v->dev_parent_id.id, ppid.id, ppid.id_len);
922 v->dev_parent_id.id_len = ppid.id_len;
924 v->dev_parent_id.id_len = 0;
927 v->local = vifc->vifc_lcl_addr.s_addr;
928 v->remote = vifc->vifc_rmt_addr.s_addr;
930 /* And finish update writing critical data */
931 write_lock_bh(&mrt_lock);
933 if (v->flags & VIFF_REGISTER)
934 mrt->mroute_reg_vif_num = vifi;
935 if (vifi+1 > mrt->maxvif)
936 mrt->maxvif = vifi+1;
937 write_unlock_bh(&mrt_lock);
938 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
942 /* called with rcu_read_lock() */
943 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
947 struct mfc_cache_cmp_arg arg = {
948 .mfc_mcastgrp = mcastgrp,
952 return mr_mfc_find(mrt, &arg);
955 /* Look for a (*,G) entry */
956 static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
957 __be32 mcastgrp, int vifi)
959 struct mfc_cache_cmp_arg arg = {
960 .mfc_mcastgrp = mcastgrp,
961 .mfc_origin = htonl(INADDR_ANY)
964 if (mcastgrp == htonl(INADDR_ANY))
965 return mr_mfc_find_any_parent(mrt, vifi);
966 return mr_mfc_find_any(mrt, vifi, &arg);
969 /* Look for a (S,G,iif) entry if parent != -1 */
970 static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
971 __be32 origin, __be32 mcastgrp,
974 struct mfc_cache_cmp_arg arg = {
975 .mfc_mcastgrp = mcastgrp,
976 .mfc_origin = origin,
979 return mr_mfc_find_parent(mrt, &arg, parent);
982 /* Allocate a multicast cache entry */
983 static struct mfc_cache *ipmr_cache_alloc(void)
985 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
988 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
989 c->_c.mfc_un.res.minvif = MAXVIFS;
990 c->_c.free = ipmr_cache_free_rcu;
991 refcount_set(&c->_c.mfc_un.res.refcount, 1);
996 static struct mfc_cache *ipmr_cache_alloc_unres(void)
998 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
1001 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
1002 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
1007 /* A cache entry has gone into a resolved state from queued */
1008 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1009 struct mfc_cache *uc, struct mfc_cache *c)
1011 struct sk_buff *skb;
1014 /* Play the pending entries through our router */
1015 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
1016 if (ip_hdr(skb)->version == 0) {
1017 struct nlmsghdr *nlh = skb_pull(skb,
1018 sizeof(struct iphdr));
1020 if (mr_fill_mroute(mrt, skb, &c->_c,
1021 nlmsg_data(nlh)) > 0) {
1022 nlh->nlmsg_len = skb_tail_pointer(skb) -
1025 nlh->nlmsg_type = NLMSG_ERROR;
1026 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
1027 skb_trim(skb, nlh->nlmsg_len);
1028 e = nlmsg_data(nlh);
1029 e->error = -EMSGSIZE;
1030 memset(&e->msg, 0, sizeof(e->msg));
1033 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
1035 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
1040 /* Bounce a cache query up to mrouted and netlink.
1042 * Called under mrt_lock.
1044 static int ipmr_cache_report(struct mr_table *mrt,
1045 struct sk_buff *pkt, vifi_t vifi, int assert)
1047 const int ihl = ip_hdrlen(pkt);
1048 struct sock *mroute_sk;
1049 struct igmphdr *igmp;
1050 struct igmpmsg *msg;
1051 struct sk_buff *skb;
1054 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE)
1055 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1057 skb = alloc_skb(128, GFP_ATOMIC);
1062 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) {
1063 /* Ugly, but we have no choice with this interface.
1064 * Duplicate old header, fix ihl, length etc.
1065 * And all this only to mangle msg->im_msgtype and
1066 * to set msg->im_mbz to "mbz" :-)
1068 skb_push(skb, sizeof(struct iphdr));
1069 skb_reset_network_header(skb);
1070 skb_reset_transport_header(skb);
1071 msg = (struct igmpmsg *)skb_network_header(skb);
1072 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
1073 msg->im_msgtype = assert;
1075 if (assert == IGMPMSG_WRVIFWHOLE)
1078 msg->im_vif = mrt->mroute_reg_vif_num;
1079 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1080 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1081 sizeof(struct iphdr));
1083 /* Copy the IP header */
1084 skb_set_network_header(skb, skb->len);
1086 skb_copy_to_linear_data(skb, pkt->data, ihl);
1087 /* Flag to the kernel this is a route add */
1088 ip_hdr(skb)->protocol = 0;
1089 msg = (struct igmpmsg *)skb_network_header(skb);
1091 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1092 /* Add our header */
1093 igmp = skb_put(skb, sizeof(struct igmphdr));
1094 igmp->type = assert;
1095 msg->im_msgtype = assert;
1097 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1098 skb->transport_header = skb->network_header;
1102 mroute_sk = rcu_dereference(mrt->mroute_sk);
1109 igmpmsg_netlink_event(mrt, skb);
1111 /* Deliver to mrouted */
1112 ret = sock_queue_rcv_skb(mroute_sk, skb);
1115 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1122 /* Queue a packet for resolution. It gets locked cache entry! */
1123 static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1124 struct sk_buff *skb, struct net_device *dev)
1126 const struct iphdr *iph = ip_hdr(skb);
1127 struct mfc_cache *c;
1131 spin_lock_bh(&mfc_unres_lock);
1132 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
1133 if (c->mfc_mcastgrp == iph->daddr &&
1134 c->mfc_origin == iph->saddr) {
1141 /* Create a new entry if allowable */
1142 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1143 (c = ipmr_cache_alloc_unres()) == NULL) {
1144 spin_unlock_bh(&mfc_unres_lock);
1150 /* Fill in the new cache entry */
1151 c->_c.mfc_parent = -1;
1152 c->mfc_origin = iph->saddr;
1153 c->mfc_mcastgrp = iph->daddr;
1155 /* Reflect first query at mrouted. */
1156 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1159 /* If the report failed throw the cache entry
1162 spin_unlock_bh(&mfc_unres_lock);
1169 atomic_inc(&mrt->cache_resolve_queue_len);
1170 list_add(&c->_c.list, &mrt->mfc_unres_queue);
1171 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1173 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1174 mod_timer(&mrt->ipmr_expire_timer,
1175 c->_c.mfc_un.unres.expires);
1178 /* See if we can append the packet */
1179 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
1185 skb->skb_iif = dev->ifindex;
1187 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
1191 spin_unlock_bh(&mfc_unres_lock);
1195 /* MFC cache manipulation by user space mroute daemon */
1197 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1199 struct net *net = read_pnet(&mrt->net);
1200 struct mfc_cache *c;
1202 /* The entries are added/deleted only under RTNL */
1204 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1205 mfc->mfcc_mcastgrp.s_addr, parent);
1209 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1210 list_del_rcu(&c->_c.list);
1211 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
1212 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1213 mr_cache_put(&c->_c);
1218 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1219 struct mfcctl *mfc, int mrtsock, int parent)
1221 struct mfc_cache *uc, *c;
1226 if (mfc->mfcc_parent >= MAXVIFS)
1229 /* The entries are added/deleted only under RTNL */
1231 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1232 mfc->mfcc_mcastgrp.s_addr, parent);
1235 write_lock_bh(&mrt_lock);
1236 c->_c.mfc_parent = mfc->mfcc_parent;
1237 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1239 c->_c.mfc_flags |= MFC_STATIC;
1240 write_unlock_bh(&mrt_lock);
1241 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1243 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1247 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1248 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1251 c = ipmr_cache_alloc();
1255 c->mfc_origin = mfc->mfcc_origin.s_addr;
1256 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1257 c->_c.mfc_parent = mfc->mfcc_parent;
1258 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1260 c->_c.mfc_flags |= MFC_STATIC;
1262 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
1265 pr_err("ipmr: rhtable insert error %d\n", ret);
1269 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
1270 /* Check to see if we resolved a queued list. If so we
1271 * need to send on the frames and tidy up.
1274 spin_lock_bh(&mfc_unres_lock);
1275 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1276 uc = (struct mfc_cache *)_uc;
1277 if (uc->mfc_origin == c->mfc_origin &&
1278 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1279 list_del(&_uc->list);
1280 atomic_dec(&mrt->cache_resolve_queue_len);
1285 if (list_empty(&mrt->mfc_unres_queue))
1286 del_timer(&mrt->ipmr_expire_timer);
1287 spin_unlock_bh(&mfc_unres_lock);
1290 ipmr_cache_resolve(net, mrt, uc, c);
1291 ipmr_cache_free(uc);
1293 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
1294 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1298 /* Close the multicast socket, and clear the vif tables etc */
1299 static void mroute_clean_tables(struct mr_table *mrt, bool all)
1301 struct net *net = read_pnet(&mrt->net);
1302 struct mr_mfc *c, *tmp;
1303 struct mfc_cache *cache;
1307 /* Shut down all active vif entries */
1308 for (i = 0; i < mrt->maxvif; i++) {
1309 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1311 vif_delete(mrt, i, 0, &list);
1313 unregister_netdevice_many(&list);
1315 /* Wipe the cache */
1316 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1317 if (!all && (c->mfc_flags & MFC_STATIC))
1319 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1320 list_del_rcu(&c->list);
1321 cache = (struct mfc_cache *)c;
1322 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
1324 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1328 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1329 spin_lock_bh(&mfc_unres_lock);
1330 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1332 cache = (struct mfc_cache *)c;
1333 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1334 ipmr_destroy_unres(mrt, cache);
1336 spin_unlock_bh(&mfc_unres_lock);
1340 /* called from ip_ra_control(), before an RCU grace period,
1341 * we dont need to call synchronize_rcu() here
1343 static void mrtsock_destruct(struct sock *sk)
1345 struct net *net = sock_net(sk);
1346 struct mr_table *mrt;
1349 ipmr_for_each_table(mrt, net) {
1350 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1351 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1352 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1353 NETCONFA_MC_FORWARDING,
1354 NETCONFA_IFINDEX_ALL,
1355 net->ipv4.devconf_all);
1356 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1357 mroute_clean_tables(mrt, false);
1363 /* Socket options and virtual interface manipulation. The whole
1364 * virtual interface system is a complete heap, but unfortunately
1365 * that's how BSD mrouted happens to think. Maybe one day with a proper
1366 * MOSPF/PIM router set up we can clean this up.
1369 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1370 unsigned int optlen)
1372 struct net *net = sock_net(sk);
1373 int val, ret = 0, parent = 0;
1374 struct mr_table *mrt;
1380 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1382 if (sk->sk_type != SOCK_RAW ||
1383 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1388 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1393 if (optname != MRT_INIT) {
1394 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1395 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1403 if (optlen != sizeof(int)) {
1407 if (rtnl_dereference(mrt->mroute_sk)) {
1412 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1414 rcu_assign_pointer(mrt->mroute_sk, sk);
1415 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1416 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1417 NETCONFA_MC_FORWARDING,
1418 NETCONFA_IFINDEX_ALL,
1419 net->ipv4.devconf_all);
1423 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1426 /* We need to unlock here because mrtsock_destruct takes
1427 * care of rtnl itself and we can't change that due to
1428 * the IP_ROUTER_ALERT setsockopt which runs without it.
1431 ret = ip_ra_control(sk, 0, NULL);
1437 if (optlen != sizeof(vif)) {
1441 if (copy_from_user(&vif, optval, sizeof(vif))) {
1445 if (vif.vifc_vifi >= MAXVIFS) {
1449 if (optname == MRT_ADD_VIF) {
1450 ret = vif_add(net, mrt, &vif,
1451 sk == rtnl_dereference(mrt->mroute_sk));
1453 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1456 /* Manipulate the forwarding caches. These live
1457 * in a sort of kernel/user symbiosis.
1463 case MRT_ADD_MFC_PROXY:
1464 case MRT_DEL_MFC_PROXY:
1465 if (optlen != sizeof(mfc)) {
1469 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1474 parent = mfc.mfcc_parent;
1475 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1476 ret = ipmr_mfc_delete(mrt, &mfc, parent);
1478 ret = ipmr_mfc_add(net, mrt, &mfc,
1479 sk == rtnl_dereference(mrt->mroute_sk),
1482 /* Control PIM assert. */
1484 if (optlen != sizeof(val)) {
1488 if (get_user(val, (int __user *)optval)) {
1492 mrt->mroute_do_assert = val;
1495 if (!ipmr_pimsm_enabled()) {
1499 if (optlen != sizeof(val)) {
1503 if (get_user(val, (int __user *)optval)) {
1508 do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE);
1510 if (val != mrt->mroute_do_pim) {
1511 mrt->mroute_do_pim = val;
1512 mrt->mroute_do_assert = val;
1513 mrt->mroute_do_wrvifwhole = do_wrvifwhole;
1517 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1521 if (optlen != sizeof(uval)) {
1525 if (get_user(uval, (u32 __user *)optval)) {
1530 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1533 mrt = ipmr_new_table(net, uval);
1537 raw_sk(sk)->ipmr_table = uval;
1540 /* Spurious command, or MRT_VERSION which you cannot set. */
1550 /* Getsock opt support for the multicast routing system. */
1551 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1555 struct net *net = sock_net(sk);
1556 struct mr_table *mrt;
1558 if (sk->sk_type != SOCK_RAW ||
1559 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1562 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1571 if (!ipmr_pimsm_enabled())
1572 return -ENOPROTOOPT;
1573 val = mrt->mroute_do_pim;
1576 val = mrt->mroute_do_assert;
1579 return -ENOPROTOOPT;
1582 if (get_user(olr, optlen))
1584 olr = min_t(unsigned int, olr, sizeof(int));
1587 if (put_user(olr, optlen))
1589 if (copy_to_user(optval, &val, olr))
1594 /* The IP multicast ioctl support routines. */
1595 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1597 struct sioc_sg_req sr;
1598 struct sioc_vif_req vr;
1599 struct vif_device *vif;
1600 struct mfc_cache *c;
1601 struct net *net = sock_net(sk);
1602 struct mr_table *mrt;
1604 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1610 if (copy_from_user(&vr, arg, sizeof(vr)))
1612 if (vr.vifi >= mrt->maxvif)
1614 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
1615 read_lock(&mrt_lock);
1616 vif = &mrt->vif_table[vr.vifi];
1617 if (VIF_EXISTS(mrt, vr.vifi)) {
1618 vr.icount = vif->pkt_in;
1619 vr.ocount = vif->pkt_out;
1620 vr.ibytes = vif->bytes_in;
1621 vr.obytes = vif->bytes_out;
1622 read_unlock(&mrt_lock);
1624 if (copy_to_user(arg, &vr, sizeof(vr)))
1628 read_unlock(&mrt_lock);
1629 return -EADDRNOTAVAIL;
1631 if (copy_from_user(&sr, arg, sizeof(sr)))
1635 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1637 sr.pktcnt = c->_c.mfc_un.res.pkt;
1638 sr.bytecnt = c->_c.mfc_un.res.bytes;
1639 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1642 if (copy_to_user(arg, &sr, sizeof(sr)))
1647 return -EADDRNOTAVAIL;
1649 return -ENOIOCTLCMD;
1653 #ifdef CONFIG_COMPAT
1654 struct compat_sioc_sg_req {
1657 compat_ulong_t pktcnt;
1658 compat_ulong_t bytecnt;
1659 compat_ulong_t wrong_if;
1662 struct compat_sioc_vif_req {
1663 vifi_t vifi; /* Which iface */
1664 compat_ulong_t icount;
1665 compat_ulong_t ocount;
1666 compat_ulong_t ibytes;
1667 compat_ulong_t obytes;
1670 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1672 struct compat_sioc_sg_req sr;
1673 struct compat_sioc_vif_req vr;
1674 struct vif_device *vif;
1675 struct mfc_cache *c;
1676 struct net *net = sock_net(sk);
1677 struct mr_table *mrt;
1679 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1685 if (copy_from_user(&vr, arg, sizeof(vr)))
1687 if (vr.vifi >= mrt->maxvif)
1689 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
1690 read_lock(&mrt_lock);
1691 vif = &mrt->vif_table[vr.vifi];
1692 if (VIF_EXISTS(mrt, vr.vifi)) {
1693 vr.icount = vif->pkt_in;
1694 vr.ocount = vif->pkt_out;
1695 vr.ibytes = vif->bytes_in;
1696 vr.obytes = vif->bytes_out;
1697 read_unlock(&mrt_lock);
1699 if (copy_to_user(arg, &vr, sizeof(vr)))
1703 read_unlock(&mrt_lock);
1704 return -EADDRNOTAVAIL;
1706 if (copy_from_user(&sr, arg, sizeof(sr)))
1710 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1712 sr.pktcnt = c->_c.mfc_un.res.pkt;
1713 sr.bytecnt = c->_c.mfc_un.res.bytes;
1714 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1717 if (copy_to_user(arg, &sr, sizeof(sr)))
1722 return -EADDRNOTAVAIL;
1724 return -ENOIOCTLCMD;
1729 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1731 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1732 struct net *net = dev_net(dev);
1733 struct mr_table *mrt;
1734 struct vif_device *v;
1737 if (event != NETDEV_UNREGISTER)
1740 ipmr_for_each_table(mrt, net) {
1741 v = &mrt->vif_table[0];
1742 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1744 vif_delete(mrt, ct, 1, NULL);
1750 static struct notifier_block ip_mr_notifier = {
1751 .notifier_call = ipmr_device_event,
1754 /* Encapsulate a packet by attaching a valid IPIP header to it.
1755 * This avoids tunnel drivers and other mess and gives us the speed so
1756 * important for multicast video.
1758 static void ip_encap(struct net *net, struct sk_buff *skb,
1759 __be32 saddr, __be32 daddr)
1762 const struct iphdr *old_iph = ip_hdr(skb);
1764 skb_push(skb, sizeof(struct iphdr));
1765 skb->transport_header = skb->network_header;
1766 skb_reset_network_header(skb);
1770 iph->tos = old_iph->tos;
1771 iph->ttl = old_iph->ttl;
1775 iph->protocol = IPPROTO_IPIP;
1777 iph->tot_len = htons(skb->len);
1778 ip_select_ident(net, skb, NULL);
1781 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1785 static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1786 struct sk_buff *skb)
1788 struct ip_options *opt = &(IPCB(skb)->opt);
1790 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1791 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
1793 if (unlikely(opt->optlen))
1794 ip_forward_options(skb);
1796 return dst_output(net, sk, skb);
1799 #ifdef CONFIG_NET_SWITCHDEV
1800 static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1801 int in_vifi, int out_vifi)
1803 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1804 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1806 if (!skb->offload_l3_fwd_mark)
1808 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1810 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1811 &in_vif->dev_parent_id);
1814 static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1815 int in_vifi, int out_vifi)
1821 /* Processing handlers for ipmr_forward */
1823 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1824 int in_vifi, struct sk_buff *skb, int vifi)
1826 const struct iphdr *iph = ip_hdr(skb);
1827 struct vif_device *vif = &mrt->vif_table[vifi];
1828 struct net_device *dev;
1836 if (vif->flags & VIFF_REGISTER) {
1838 vif->bytes_out += skb->len;
1839 vif->dev->stats.tx_bytes += skb->len;
1840 vif->dev->stats.tx_packets++;
1841 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1845 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1848 if (vif->flags & VIFF_TUNNEL) {
1849 rt = ip_route_output_ports(net, &fl4, NULL,
1850 vif->remote, vif->local,
1853 RT_TOS(iph->tos), vif->link);
1856 encap = sizeof(struct iphdr);
1858 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1861 RT_TOS(iph->tos), vif->link);
1868 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1869 /* Do not fragment multicasts. Alas, IPv4 does not
1870 * allow to send ICMP, so that packets will disappear
1873 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1878 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1880 if (skb_cow(skb, encap)) {
1886 vif->bytes_out += skb->len;
1889 skb_dst_set(skb, &rt->dst);
1890 ip_decrease_ttl(ip_hdr(skb));
1892 /* FIXME: forward and output firewalls used to be called here.
1893 * What do we do with netfilter? -- RR
1895 if (vif->flags & VIFF_TUNNEL) {
1896 ip_encap(net, skb, vif->local, vif->remote);
1897 /* FIXME: extra output firewall step used to be here. --RR */
1898 vif->dev->stats.tx_packets++;
1899 vif->dev->stats.tx_bytes += skb->len;
1902 IPCB(skb)->flags |= IPSKB_FORWARDED;
1904 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1905 * not only before forwarding, but after forwarding on all output
1906 * interfaces. It is clear, if mrouter runs a multicasting
1907 * program, it should receive packets not depending to what interface
1908 * program is joined.
1909 * If we will not make it, the program will have to join on all
1910 * interfaces. On the other hand, multihoming host (or router, but
1911 * not mrouter) cannot join to more than one interface - it will
1912 * result in receiving multiple packets.
1914 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1915 net, NULL, skb, skb->dev, dev,
1916 ipmr_forward_finish);
1923 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1927 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1928 if (mrt->vif_table[ct].dev == dev)
1934 /* "local" means that we should preserve one skb (for local delivery) */
1935 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1936 struct net_device *dev, struct sk_buff *skb,
1937 struct mfc_cache *c, int local)
1939 int true_vifi = ipmr_find_vif(mrt, dev);
1943 vif = c->_c.mfc_parent;
1944 c->_c.mfc_un.res.pkt++;
1945 c->_c.mfc_un.res.bytes += skb->len;
1946 c->_c.mfc_un.res.lastuse = jiffies;
1948 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1949 struct mfc_cache *cache_proxy;
1951 /* For an (*,G) entry, we only check that the incomming
1952 * interface is part of the static tree.
1954 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
1956 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
1960 /* Wrong interface: drop packet and (maybe) send PIM assert. */
1961 if (mrt->vif_table[vif].dev != dev) {
1962 if (rt_is_output_route(skb_rtable(skb))) {
1963 /* It is our own packet, looped back.
1964 * Very complicated situation...
1966 * The best workaround until routing daemons will be
1967 * fixed is not to redistribute packet, if it was
1968 * send through wrong interface. It means, that
1969 * multicast applications WILL NOT work for
1970 * (S,G), which have default multicast route pointing
1971 * to wrong oif. In any case, it is not a good
1972 * idea to use multicasting applications on router.
1977 c->_c.mfc_un.res.wrong_if++;
1979 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1980 /* pimsm uses asserts, when switching from RPT to SPT,
1981 * so that we cannot check that packet arrived on an oif.
1982 * It is bad, but otherwise we would need to move pretty
1983 * large chunk of pimd to kernel. Ough... --ANK
1985 (mrt->mroute_do_pim ||
1986 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
1988 c->_c.mfc_un.res.last_assert +
1989 MFC_ASSERT_THRESH)) {
1990 c->_c.mfc_un.res.last_assert = jiffies;
1991 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1992 if (mrt->mroute_do_wrvifwhole)
1993 ipmr_cache_report(mrt, skb, true_vifi,
1994 IGMPMSG_WRVIFWHOLE);
2000 mrt->vif_table[vif].pkt_in++;
2001 mrt->vif_table[vif].bytes_in += skb->len;
2003 /* Forward the frame */
2004 if (c->mfc_origin == htonl(INADDR_ANY) &&
2005 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
2006 if (true_vifi >= 0 &&
2007 true_vifi != c->_c.mfc_parent &&
2009 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
2010 /* It's an (*,*) entry and the packet is not coming from
2011 * the upstream: forward the packet to the upstream
2014 psend = c->_c.mfc_parent;
2019 for (ct = c->_c.mfc_un.res.maxvif - 1;
2020 ct >= c->_c.mfc_un.res.minvif; ct--) {
2021 /* For (*,G) entry, don't forward to the incoming interface */
2022 if ((c->mfc_origin != htonl(INADDR_ANY) ||
2024 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
2026 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2029 ipmr_queue_xmit(net, mrt, true_vifi,
2038 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2041 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
2044 ipmr_queue_xmit(net, mrt, true_vifi, skb, psend);
2054 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
2056 struct rtable *rt = skb_rtable(skb);
2057 struct iphdr *iph = ip_hdr(skb);
2058 struct flowi4 fl4 = {
2059 .daddr = iph->daddr,
2060 .saddr = iph->saddr,
2061 .flowi4_tos = RT_TOS(iph->tos),
2062 .flowi4_oif = (rt_is_output_route(rt) ?
2063 skb->dev->ifindex : 0),
2064 .flowi4_iif = (rt_is_output_route(rt) ?
2067 .flowi4_mark = skb->mark,
2069 struct mr_table *mrt;
2072 err = ipmr_fib_lookup(net, &fl4, &mrt);
2074 return ERR_PTR(err);
2078 /* Multicast packets for forwarding arrive here
2079 * Called with rcu_read_lock();
2081 int ip_mr_input(struct sk_buff *skb)
2083 struct mfc_cache *cache;
2084 struct net *net = dev_net(skb->dev);
2085 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
2086 struct mr_table *mrt;
2087 struct net_device *dev;
2089 /* skb->dev passed in is the loX master dev for vrfs.
2090 * As there are no vifs associated with loopback devices,
2091 * get the proper interface that does have a vif associated with it.
2094 if (netif_is_l3_master(skb->dev)) {
2095 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2102 /* Packet is looped back after forward, it should not be
2103 * forwarded second time, but still can be delivered locally.
2105 if (IPCB(skb)->flags & IPSKB_FORWARDED)
2108 mrt = ipmr_rt_fib_lookup(net, skb);
2111 return PTR_ERR(mrt);
2114 if (IPCB(skb)->opt.router_alert) {
2115 if (ip_call_ra_chain(skb))
2117 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2118 /* IGMPv1 (and broken IGMPv2 implementations sort of
2119 * Cisco IOS <= 11.2(8)) do not put router alert
2120 * option to IGMP packets destined to routable
2121 * groups. It is very bad, because it means
2122 * that we can forward NO IGMP messages.
2124 struct sock *mroute_sk;
2126 mroute_sk = rcu_dereference(mrt->mroute_sk);
2129 raw_rcv(mroute_sk, skb);
2135 /* already under rcu_read_lock() */
2136 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
2138 int vif = ipmr_find_vif(mrt, dev);
2141 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2145 /* No usable cache entry */
2150 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2151 ip_local_deliver(skb);
2157 read_lock(&mrt_lock);
2158 vif = ipmr_find_vif(mrt, dev);
2160 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
2161 read_unlock(&mrt_lock);
2165 read_unlock(&mrt_lock);
2170 read_lock(&mrt_lock);
2171 ip_mr_forward(net, mrt, dev, skb, cache, local);
2172 read_unlock(&mrt_lock);
2175 return ip_local_deliver(skb);
2181 return ip_local_deliver(skb);
2186 #ifdef CONFIG_IP_PIMSM_V1
2187 /* Handle IGMP messages of PIMv1 */
2188 int pim_rcv_v1(struct sk_buff *skb)
2190 struct igmphdr *pim;
2191 struct net *net = dev_net(skb->dev);
2192 struct mr_table *mrt;
2194 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2197 pim = igmp_hdr(skb);
2199 mrt = ipmr_rt_fib_lookup(net, skb);
2202 if (!mrt->mroute_do_pim ||
2203 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2206 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2214 #ifdef CONFIG_IP_PIMSM_V2
2215 static int pim_rcv(struct sk_buff *skb)
2217 struct pimreghdr *pim;
2218 struct net *net = dev_net(skb->dev);
2219 struct mr_table *mrt;
2221 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2224 pim = (struct pimreghdr *)skb_transport_header(skb);
2225 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
2226 (pim->flags & PIM_NULL_REGISTER) ||
2227 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2228 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2231 mrt = ipmr_rt_fib_lookup(net, skb);
2234 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2242 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2243 __be32 saddr, __be32 daddr,
2244 struct rtmsg *rtm, u32 portid)
2246 struct mfc_cache *cache;
2247 struct mr_table *mrt;
2250 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2255 cache = ipmr_cache_find(mrt, saddr, daddr);
2256 if (!cache && skb->dev) {
2257 int vif = ipmr_find_vif(mrt, skb->dev);
2260 cache = ipmr_cache_find_any(mrt, daddr, vif);
2263 struct sk_buff *skb2;
2265 struct net_device *dev;
2269 read_lock(&mrt_lock);
2271 vif = ipmr_find_vif(mrt, dev);
2273 read_unlock(&mrt_lock);
2277 skb2 = skb_clone(skb, GFP_ATOMIC);
2279 read_unlock(&mrt_lock);
2284 NETLINK_CB(skb2).portid = portid;
2285 skb_push(skb2, sizeof(struct iphdr));
2286 skb_reset_network_header(skb2);
2288 iph->ihl = sizeof(struct iphdr) >> 2;
2292 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
2293 read_unlock(&mrt_lock);
2298 read_lock(&mrt_lock);
2299 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
2300 read_unlock(&mrt_lock);
2305 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2306 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2309 struct nlmsghdr *nlh;
2313 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2317 rtm = nlmsg_data(nlh);
2318 rtm->rtm_family = RTNL_FAMILY_IPMR;
2319 rtm->rtm_dst_len = 32;
2320 rtm->rtm_src_len = 32;
2322 rtm->rtm_table = mrt->id;
2323 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2324 goto nla_put_failure;
2325 rtm->rtm_type = RTN_MULTICAST;
2326 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2327 if (c->_c.mfc_flags & MFC_STATIC)
2328 rtm->rtm_protocol = RTPROT_STATIC;
2330 rtm->rtm_protocol = RTPROT_MROUTED;
2333 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2334 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2335 goto nla_put_failure;
2336 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
2337 /* do not break the dump if cache is unresolved */
2338 if (err < 0 && err != -ENOENT)
2339 goto nla_put_failure;
2341 nlmsg_end(skb, nlh);
2345 nlmsg_cancel(skb, nlh);
2349 static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2350 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2353 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2357 static size_t mroute_msgsize(bool unresolved, int maxvif)
2360 NLMSG_ALIGN(sizeof(struct rtmsg))
2361 + nla_total_size(4) /* RTA_TABLE */
2362 + nla_total_size(4) /* RTA_SRC */
2363 + nla_total_size(4) /* RTA_DST */
2368 + nla_total_size(4) /* RTA_IIF */
2369 + nla_total_size(0) /* RTA_MULTIPATH */
2370 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2372 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
2378 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2381 struct net *net = read_pnet(&mrt->net);
2382 struct sk_buff *skb;
2385 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2391 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2395 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2401 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2404 static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2407 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2408 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2409 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2410 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2411 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2412 /* IPMRA_CREPORT_PKT */
2413 + nla_total_size(payloadlen)
2419 static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2421 struct net *net = read_pnet(&mrt->net);
2422 struct nlmsghdr *nlh;
2423 struct rtgenmsg *rtgenm;
2424 struct igmpmsg *msg;
2425 struct sk_buff *skb;
2429 payloadlen = pkt->len - sizeof(struct igmpmsg);
2430 msg = (struct igmpmsg *)skb_network_header(pkt);
2432 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2436 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2437 sizeof(struct rtgenmsg), 0);
2440 rtgenm = nlmsg_data(nlh);
2441 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2442 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2443 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2444 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2445 msg->im_src.s_addr) ||
2446 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2447 msg->im_dst.s_addr))
2448 goto nla_put_failure;
2450 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2451 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2452 nla_data(nla), payloadlen))
2453 goto nla_put_failure;
2455 nlmsg_end(skb, nlh);
2457 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2461 nlmsg_cancel(skb, nlh);
2464 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2467 static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb,
2468 const struct nlmsghdr *nlh,
2470 struct netlink_ext_ack *extack)
2475 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
2476 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for multicast route get request");
2480 if (!netlink_strict_get_check(skb))
2481 return nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2482 rtm_ipv4_policy, extack);
2484 rtm = nlmsg_data(nlh);
2485 if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
2486 (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
2487 rtm->rtm_tos || rtm->rtm_table || rtm->rtm_protocol ||
2488 rtm->rtm_scope || rtm->rtm_type || rtm->rtm_flags) {
2489 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for multicast route get request");
2493 err = nlmsg_parse_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
2494 rtm_ipv4_policy, extack);
2498 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
2499 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
2500 NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
2504 for (i = 0; i <= RTA_MAX; i++) {
2514 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in multicast route get request");
2522 static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2523 struct netlink_ext_ack *extack)
2525 struct net *net = sock_net(in_skb->sk);
2526 struct nlattr *tb[RTA_MAX + 1];
2527 struct sk_buff *skb = NULL;
2528 struct mfc_cache *cache;
2529 struct mr_table *mrt;
2534 err = ipmr_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
2538 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2539 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2540 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2542 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
2548 /* entries are added/deleted only under RTNL */
2550 cache = ipmr_cache_find(mrt, src, grp);
2557 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2563 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2564 nlh->nlmsg_seq, cache,
2569 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2579 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2581 struct fib_dump_filter filter = {};
2584 if (cb->strict_check) {
2585 err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh,
2591 if (filter.table_id) {
2592 struct mr_table *mrt;
2594 mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id);
2596 if (filter.dump_all_families)
2599 NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist");
2602 err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute,
2603 &mfc_unres_lock, &filter);
2604 return skb->len ? : err;
2607 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
2608 _ipmr_fill_mroute, &mfc_unres_lock, &filter);
2611 static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2612 [RTA_SRC] = { .type = NLA_U32 },
2613 [RTA_DST] = { .type = NLA_U32 },
2614 [RTA_IIF] = { .type = NLA_U32 },
2615 [RTA_TABLE] = { .type = NLA_U32 },
2616 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2619 static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2621 switch (rtm_protocol) {
2623 case RTPROT_MROUTED:
2629 static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2631 struct rtnexthop *rtnh = nla_data(nla);
2632 int remaining = nla_len(nla), vifi = 0;
2634 while (rtnh_ok(rtnh, remaining)) {
2635 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2636 if (++vifi == MAXVIFS)
2638 rtnh = rtnh_next(rtnh, &remaining);
2641 return remaining > 0 ? -EINVAL : vifi;
2644 /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2645 static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2646 struct mfcctl *mfcc, int *mrtsock,
2647 struct mr_table **mrtret,
2648 struct netlink_ext_ack *extack)
2650 struct net_device *dev = NULL;
2651 u32 tblid = RT_TABLE_DEFAULT;
2652 struct mr_table *mrt;
2653 struct nlattr *attr;
2657 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
2661 rtm = nlmsg_data(nlh);
2664 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2665 rtm->rtm_type != RTN_MULTICAST ||
2666 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2667 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2670 memset(mfcc, 0, sizeof(*mfcc));
2671 mfcc->mfcc_parent = -1;
2673 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2674 switch (nla_type(attr)) {
2676 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2679 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2682 dev = __dev_get_by_index(net, nla_get_u32(attr));
2689 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2698 tblid = nla_get_u32(attr);
2702 mrt = ipmr_get_table(net, tblid);
2708 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2710 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2716 /* takes care of both newroute and delroute */
2717 static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2718 struct netlink_ext_ack *extack)
2720 struct net *net = sock_net(skb->sk);
2721 int ret, mrtsock, parent;
2722 struct mr_table *tbl;
2727 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
2731 parent = ret ? mfcc.mfcc_parent : -1;
2732 if (nlh->nlmsg_type == RTM_NEWROUTE)
2733 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2735 return ipmr_mfc_delete(tbl, &mfcc, parent);
2738 static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2740 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2742 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2743 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2744 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2745 mrt->mroute_reg_vif_num) ||
2746 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2747 mrt->mroute_do_assert) ||
2748 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
2749 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
2750 mrt->mroute_do_wrvifwhole))
2756 static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2758 struct nlattr *vif_nest;
2759 struct vif_device *vif;
2761 /* if the VIF doesn't exist just continue */
2762 if (!VIF_EXISTS(mrt, vifid))
2765 vif = &mrt->vif_table[vifid];
2766 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2769 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2770 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2771 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2772 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2774 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2776 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2778 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2780 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2781 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2782 nla_nest_cancel(skb, vif_nest);
2785 nla_nest_end(skb, vif_nest);
2790 static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
2791 struct netlink_ext_ack *extack)
2793 struct ifinfomsg *ifm;
2795 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2796 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
2800 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
2801 NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump");
2805 ifm = nlmsg_data(nlh);
2806 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2807 ifm->ifi_change || ifm->ifi_index) {
2808 NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
2815 static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2817 struct net *net = sock_net(skb->sk);
2818 struct nlmsghdr *nlh = NULL;
2819 unsigned int t = 0, s_t;
2820 unsigned int e = 0, s_e;
2821 struct mr_table *mrt;
2823 if (cb->strict_check) {
2824 int err = ipmr_valid_dumplink(cb->nlh, cb->extack);
2833 ipmr_for_each_table(mrt, net) {
2834 struct nlattr *vifs, *af;
2835 struct ifinfomsg *hdr;
2840 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2841 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2842 sizeof(*hdr), NLM_F_MULTI);
2846 hdr = nlmsg_data(nlh);
2847 memset(hdr, 0, sizeof(*hdr));
2848 hdr->ifi_family = RTNL_FAMILY_IPMR;
2850 af = nla_nest_start(skb, IFLA_AF_SPEC);
2852 nlmsg_cancel(skb, nlh);
2856 if (!ipmr_fill_table(mrt, skb)) {
2857 nlmsg_cancel(skb, nlh);
2861 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2863 nla_nest_end(skb, af);
2864 nlmsg_end(skb, nlh);
2867 for (i = 0; i < mrt->maxvif; i++) {
2870 if (!ipmr_fill_vif(mrt, i, skb)) {
2871 nla_nest_end(skb, vifs);
2872 nla_nest_end(skb, af);
2873 nlmsg_end(skb, nlh);
2881 nla_nest_end(skb, vifs);
2882 nla_nest_end(skb, af);
2883 nlmsg_end(skb, nlh);
2895 #ifdef CONFIG_PROC_FS
2896 /* The /proc interfaces to multicast routing :
2897 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2900 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2901 __acquires(mrt_lock)
2903 struct mr_vif_iter *iter = seq->private;
2904 struct net *net = seq_file_net(seq);
2905 struct mr_table *mrt;
2907 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2909 return ERR_PTR(-ENOENT);
2913 read_lock(&mrt_lock);
2914 return mr_vif_seq_start(seq, pos);
2917 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2918 __releases(mrt_lock)
2920 read_unlock(&mrt_lock);
2923 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2925 struct mr_vif_iter *iter = seq->private;
2926 struct mr_table *mrt = iter->mrt;
2928 if (v == SEQ_START_TOKEN) {
2930 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2932 const struct vif_device *vif = v;
2933 const char *name = vif->dev ?
2934 vif->dev->name : "none";
2937 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2938 vif - mrt->vif_table,
2939 name, vif->bytes_in, vif->pkt_in,
2940 vif->bytes_out, vif->pkt_out,
2941 vif->flags, vif->local, vif->remote);
2946 static const struct seq_operations ipmr_vif_seq_ops = {
2947 .start = ipmr_vif_seq_start,
2948 .next = mr_vif_seq_next,
2949 .stop = ipmr_vif_seq_stop,
2950 .show = ipmr_vif_seq_show,
2953 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2955 struct net *net = seq_file_net(seq);
2956 struct mr_table *mrt;
2958 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2960 return ERR_PTR(-ENOENT);
2962 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
2965 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2969 if (v == SEQ_START_TOKEN) {
2971 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2973 const struct mfc_cache *mfc = v;
2974 const struct mr_mfc_iter *it = seq->private;
2975 const struct mr_table *mrt = it->mrt;
2977 seq_printf(seq, "%08X %08X %-3hd",
2978 (__force u32) mfc->mfc_mcastgrp,
2979 (__force u32) mfc->mfc_origin,
2980 mfc->_c.mfc_parent);
2982 if (it->cache != &mrt->mfc_unres_queue) {
2983 seq_printf(seq, " %8lu %8lu %8lu",
2984 mfc->_c.mfc_un.res.pkt,
2985 mfc->_c.mfc_un.res.bytes,
2986 mfc->_c.mfc_un.res.wrong_if);
2987 for (n = mfc->_c.mfc_un.res.minvif;
2988 n < mfc->_c.mfc_un.res.maxvif; n++) {
2989 if (VIF_EXISTS(mrt, n) &&
2990 mfc->_c.mfc_un.res.ttls[n] < 255)
2993 n, mfc->_c.mfc_un.res.ttls[n]);
2996 /* unresolved mfc_caches don't contain
2997 * pkt, bytes and wrong_if values
2999 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
3001 seq_putc(seq, '\n');
3006 static const struct seq_operations ipmr_mfc_seq_ops = {
3007 .start = ipmr_mfc_seq_start,
3008 .next = mr_mfc_seq_next,
3009 .stop = mr_mfc_seq_stop,
3010 .show = ipmr_mfc_seq_show,
3014 #ifdef CONFIG_IP_PIMSM_V2
3015 static const struct net_protocol pim_protocol = {
3021 static unsigned int ipmr_seq_read(struct net *net)
3025 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
3028 static int ipmr_dump(struct net *net, struct notifier_block *nb)
3030 return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
3031 ipmr_mr_table_iter, &mrt_lock);
3034 static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3035 .family = RTNL_FAMILY_IPMR,
3036 .fib_seq_read = ipmr_seq_read,
3037 .fib_dump = ipmr_dump,
3038 .owner = THIS_MODULE,
3041 static int __net_init ipmr_notifier_init(struct net *net)
3043 struct fib_notifier_ops *ops;
3045 net->ipv4.ipmr_seq = 0;
3047 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3049 return PTR_ERR(ops);
3050 net->ipv4.ipmr_notifier_ops = ops;
3055 static void __net_exit ipmr_notifier_exit(struct net *net)
3057 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3058 net->ipv4.ipmr_notifier_ops = NULL;
3061 /* Setup for IP multicast routing */
3062 static int __net_init ipmr_net_init(struct net *net)
3066 err = ipmr_notifier_init(net);
3068 goto ipmr_notifier_fail;
3070 err = ipmr_rules_init(net);
3072 goto ipmr_rules_fail;
3074 #ifdef CONFIG_PROC_FS
3076 if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
3077 sizeof(struct mr_vif_iter)))
3079 if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
3080 sizeof(struct mr_mfc_iter)))
3081 goto proc_cache_fail;
3085 #ifdef CONFIG_PROC_FS
3087 remove_proc_entry("ip_mr_vif", net->proc_net);
3089 ipmr_rules_exit(net);
3092 ipmr_notifier_exit(net);
3097 static void __net_exit ipmr_net_exit(struct net *net)
3099 #ifdef CONFIG_PROC_FS
3100 remove_proc_entry("ip_mr_cache", net->proc_net);
3101 remove_proc_entry("ip_mr_vif", net->proc_net);
3103 ipmr_notifier_exit(net);
3104 ipmr_rules_exit(net);
3107 static struct pernet_operations ipmr_net_ops = {
3108 .init = ipmr_net_init,
3109 .exit = ipmr_net_exit,
3112 int __init ip_mr_init(void)
3116 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3117 sizeof(struct mfc_cache),
3118 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
3121 err = register_pernet_subsys(&ipmr_net_ops);
3123 goto reg_pernet_fail;
3125 err = register_netdevice_notifier(&ip_mr_notifier);
3127 goto reg_notif_fail;
3128 #ifdef CONFIG_IP_PIMSM_V2
3129 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
3130 pr_err("%s: can't add PIM protocol\n", __func__);
3132 goto add_proto_fail;
3135 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
3136 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
3137 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
3138 ipmr_rtm_route, NULL, 0);
3139 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
3140 ipmr_rtm_route, NULL, 0);
3142 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
3143 NULL, ipmr_rtm_dumplink, 0);
3146 #ifdef CONFIG_IP_PIMSM_V2
3148 unregister_netdevice_notifier(&ip_mr_notifier);
3151 unregister_pernet_subsys(&ipmr_net_ops);
3153 kmem_cache_destroy(mrt_cachep);