1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * IP multicast routing support for mrouted 3.6/3.8
6 * Linux Consultancy and Custom Driver Development
9 * Michael Chastain : Incorrect size of copying.
10 * Alan Cox : Added the cache manager code
11 * Alan Cox : Fixed the clone/copy bug and device race.
12 * Mike McLagan : Routing by source
13 * Malcolm Beattie : Buffer handling fixes.
14 * Alexey Kuznetsov : Double buffer free and other fixes.
15 * SVR Anand : Fixed several multicast bugs and problems.
16 * Alexey Kuznetsov : Status, optimisations and more.
17 * Brad Parker : Better behaviour on mrouted upcall
19 * Carlos Picoto : PIMv1 Support
20 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
21 * Relax this requirement to work with older peers.
24 #include <linux/uaccess.h>
25 #include <linux/types.h>
26 #include <linux/cache.h>
27 #include <linux/capability.h>
28 #include <linux/errno.h>
30 #include <linux/kernel.h>
31 #include <linux/fcntl.h>
32 #include <linux/stat.h>
33 #include <linux/socket.h>
35 #include <linux/inet.h>
36 #include <linux/netdevice.h>
37 #include <linux/inetdevice.h>
38 #include <linux/igmp.h>
39 #include <linux/proc_fs.h>
40 #include <linux/seq_file.h>
41 #include <linux/mroute.h>
42 #include <linux/init.h>
43 #include <linux/if_ether.h>
44 #include <linux/slab.h>
45 #include <net/net_namespace.h>
47 #include <net/protocol.h>
48 #include <linux/skbuff.h>
49 #include <net/route.h>
53 #include <linux/notifier.h>
54 #include <linux/if_arp.h>
55 #include <linux/netfilter_ipv4.h>
56 #include <linux/compat.h>
57 #include <linux/export.h>
58 #include <linux/rhashtable.h>
59 #include <net/ip_tunnels.h>
60 #include <net/checksum.h>
61 #include <net/netlink.h>
62 #include <net/fib_rules.h>
63 #include <linux/netconf.h>
66 #include <linux/nospec.h>
69 struct fib_rule common;
76 /* Big lock, protecting vif table, mrt cache and mroute socket state.
77 * Note that the changes are semaphored via rtnl_lock.
80 static DEFINE_SPINLOCK(mrt_lock);
82 static struct net_device *vif_dev_read(const struct vif_device *vif)
84 return rcu_dereference(vif->dev);
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(const 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(const struct mr_table *mrt, struct sk_buff *pkt);
113 static void mroute_clean_tables(struct mr_table *mrt, int flags);
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, \
119 lockdep_rtnl_is_held() || \
120 list_empty(&net->ipv4.mr_tables))
122 static struct mr_table *ipmr_mr_table_iter(struct net *net,
123 struct mr_table *mrt)
125 struct mr_table *ret;
128 ret = list_entry_rcu(net->ipv4.mr_tables.next,
129 struct mr_table, list);
131 ret = list_entry_rcu(mrt->list.next,
132 struct mr_table, list);
134 if (&ret->list == &net->ipv4.mr_tables)
139 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
141 struct mr_table *mrt;
143 ipmr_for_each_table(mrt, net) {
150 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
151 struct mr_table **mrt)
154 struct ipmr_result res;
155 struct fib_lookup_arg arg = {
157 .flags = FIB_LOOKUP_NOREF,
160 /* update flow if oif or iif point to device enslaved to l3mdev */
161 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
163 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
164 flowi4_to_flowi(flp4), 0, &arg);
171 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
172 int flags, struct fib_lookup_arg *arg)
174 struct ipmr_result *res = arg->result;
175 struct mr_table *mrt;
177 switch (rule->action) {
180 case FR_ACT_UNREACHABLE:
182 case FR_ACT_PROHIBIT:
184 case FR_ACT_BLACKHOLE:
189 arg->table = fib_rule_get_table(rule, arg);
191 mrt = ipmr_get_table(rule->fr_net, arg->table);
198 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
203 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
204 struct fib_rule_hdr *frh, struct nlattr **tb,
205 struct netlink_ext_ack *extack)
210 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
216 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
217 struct fib_rule_hdr *frh)
225 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
226 .family = RTNL_FAMILY_IPMR,
227 .rule_size = sizeof(struct ipmr_rule),
228 .addr_size = sizeof(u32),
229 .action = ipmr_rule_action,
230 .match = ipmr_rule_match,
231 .configure = ipmr_rule_configure,
232 .compare = ipmr_rule_compare,
233 .fill = ipmr_rule_fill,
234 .nlgroup = RTNLGRP_IPV4_RULE,
235 .owner = THIS_MODULE,
238 static int __net_init ipmr_rules_init(struct net *net)
240 struct fib_rules_ops *ops;
241 struct mr_table *mrt;
244 ops = fib_rules_register(&ipmr_rules_ops_template, net);
248 INIT_LIST_HEAD(&net->ipv4.mr_tables);
250 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
256 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT);
260 net->ipv4.mr_rules_ops = ops;
265 ipmr_free_table(mrt);
268 fib_rules_unregister(ops);
272 static void __net_exit ipmr_rules_exit(struct net *net)
274 struct mr_table *mrt, *next;
277 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
278 list_del(&mrt->list);
279 ipmr_free_table(mrt);
281 fib_rules_unregister(net->ipv4.mr_rules_ops);
284 static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,
285 struct netlink_ext_ack *extack)
287 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR, extack);
290 static unsigned int ipmr_rules_seq_read(struct net *net)
292 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
295 bool ipmr_rule_default(const struct fib_rule *rule)
297 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
299 EXPORT_SYMBOL(ipmr_rule_default);
301 #define ipmr_for_each_table(mrt, net) \
302 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
304 static struct mr_table *ipmr_mr_table_iter(struct net *net,
305 struct mr_table *mrt)
308 return net->ipv4.mrt;
312 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
314 return net->ipv4.mrt;
317 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
318 struct mr_table **mrt)
320 *mrt = net->ipv4.mrt;
324 static int __net_init ipmr_rules_init(struct net *net)
326 struct mr_table *mrt;
328 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
335 static void __net_exit ipmr_rules_exit(struct net *net)
338 ipmr_free_table(net->ipv4.mrt);
339 net->ipv4.mrt = NULL;
342 static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,
343 struct netlink_ext_ack *extack)
348 static unsigned int ipmr_rules_seq_read(struct net *net)
353 bool ipmr_rule_default(const struct fib_rule *rule)
357 EXPORT_SYMBOL(ipmr_rule_default);
360 static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
363 const struct mfc_cache_cmp_arg *cmparg = arg->key;
364 const struct mfc_cache *c = ptr;
366 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
367 cmparg->mfc_origin != c->mfc_origin;
370 static const struct rhashtable_params ipmr_rht_params = {
371 .head_offset = offsetof(struct mr_mfc, mnode),
372 .key_offset = offsetof(struct mfc_cache, cmparg),
373 .key_len = sizeof(struct mfc_cache_cmp_arg),
375 .obj_cmpfn = ipmr_hash_cmp,
376 .automatic_shrinking = true,
379 static void ipmr_new_table_set(struct mr_table *mrt,
382 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
383 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
387 static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
388 .mfc_mcastgrp = htonl(INADDR_ANY),
389 .mfc_origin = htonl(INADDR_ANY),
392 static struct mr_table_ops ipmr_mr_table_ops = {
393 .rht_params = &ipmr_rht_params,
394 .cmparg_any = &ipmr_mr_table_ops_cmparg_any,
397 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
399 struct mr_table *mrt;
401 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
402 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
403 return ERR_PTR(-EINVAL);
405 mrt = ipmr_get_table(net, id);
409 return mr_table_alloc(net, id, &ipmr_mr_table_ops,
410 ipmr_expire_process, ipmr_new_table_set);
413 static void ipmr_free_table(struct mr_table *mrt)
415 timer_shutdown_sync(&mrt->ipmr_expire_timer);
416 mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC |
417 MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC);
418 rhltable_destroy(&mrt->mfc_hash);
422 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
424 /* Initialize ipmr pimreg/tunnel in_device */
425 static bool ipmr_init_vif_indev(const struct net_device *dev)
427 struct in_device *in_dev;
431 in_dev = __in_dev_get_rtnl(dev);
434 ipv4_devconf_setall(in_dev);
435 neigh_parms_data_state_setall(in_dev->arp_parms);
436 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
441 static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
443 struct net_device *tunnel_dev, *new_dev;
444 struct ip_tunnel_parm p = { };
447 tunnel_dev = __dev_get_by_name(net, "tunl0");
451 p.iph.daddr = v->vifc_rmt_addr.s_addr;
452 p.iph.saddr = v->vifc_lcl_addr.s_addr;
455 p.iph.protocol = IPPROTO_IPIP;
456 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
458 if (!tunnel_dev->netdev_ops->ndo_tunnel_ctl)
460 err = tunnel_dev->netdev_ops->ndo_tunnel_ctl(tunnel_dev, &p,
465 new_dev = __dev_get_by_name(net, p.name);
469 new_dev->flags |= IFF_MULTICAST;
470 if (!ipmr_init_vif_indev(new_dev))
472 if (dev_open(new_dev, NULL))
475 err = dev_set_allmulti(new_dev, 1);
478 tunnel_dev->netdev_ops->ndo_tunnel_ctl(tunnel_dev, &p,
481 new_dev = ERR_PTR(err);
486 unregister_netdevice(new_dev);
488 return ERR_PTR(-ENOBUFS);
491 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
492 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
494 struct net *net = dev_net(dev);
495 struct mr_table *mrt;
496 struct flowi4 fl4 = {
497 .flowi4_oif = dev->ifindex,
498 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
499 .flowi4_mark = skb->mark,
503 err = ipmr_fib_lookup(net, &fl4, &mrt);
509 DEV_STATS_ADD(dev, tx_bytes, skb->len);
510 DEV_STATS_INC(dev, tx_packets);
513 /* Pairs with WRITE_ONCE() in vif_add() and vif_delete() */
514 ipmr_cache_report(mrt, skb, READ_ONCE(mrt->mroute_reg_vif_num),
522 static int reg_vif_get_iflink(const struct net_device *dev)
527 static const struct net_device_ops reg_vif_netdev_ops = {
528 .ndo_start_xmit = reg_vif_xmit,
529 .ndo_get_iflink = reg_vif_get_iflink,
532 static void reg_vif_setup(struct net_device *dev)
534 dev->type = ARPHRD_PIMREG;
535 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
536 dev->flags = IFF_NOARP;
537 dev->netdev_ops = ®_vif_netdev_ops;
538 dev->needs_free_netdev = true;
539 dev->features |= NETIF_F_NETNS_LOCAL;
542 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
544 struct net_device *dev;
547 if (mrt->id == RT_TABLE_DEFAULT)
548 sprintf(name, "pimreg");
550 sprintf(name, "pimreg%u", mrt->id);
552 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
557 dev_net_set(dev, net);
559 if (register_netdevice(dev)) {
564 if (!ipmr_init_vif_indev(dev))
566 if (dev_open(dev, NULL))
574 unregister_netdevice(dev);
578 /* called with rcu_read_lock() */
579 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
582 struct net_device *reg_dev = NULL;
586 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
588 * a. packet is really sent to a multicast group
589 * b. packet is not a NULL-REGISTER
590 * c. packet is not truncated
592 if (!ipv4_is_multicast(encap->daddr) ||
593 encap->tot_len == 0 ||
594 ntohs(encap->tot_len) + pimlen > skb->len)
597 /* Pairs with WRITE_ONCE() in vif_add()/vid_delete() */
598 vif_num = READ_ONCE(mrt->mroute_reg_vif_num);
600 reg_dev = vif_dev_read(&mrt->vif_table[vif_num]);
604 skb->mac_header = skb->network_header;
605 skb_pull(skb, (u8 *)encap - skb->data);
606 skb_reset_network_header(skb);
607 skb->protocol = htons(ETH_P_IP);
608 skb->ip_summed = CHECKSUM_NONE;
610 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
614 return NET_RX_SUCCESS;
617 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
623 static int call_ipmr_vif_entry_notifiers(struct net *net,
624 enum fib_event_type event_type,
625 struct vif_device *vif,
626 struct net_device *vif_dev,
627 vifi_t vif_index, u32 tb_id)
629 return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
630 vif, vif_dev, vif_index, tb_id,
631 &net->ipv4.ipmr_seq);
634 static int call_ipmr_mfc_entry_notifiers(struct net *net,
635 enum fib_event_type event_type,
636 struct mfc_cache *mfc, u32 tb_id)
638 return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
639 &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
643 * vif_delete - Delete a VIF entry
644 * @mrt: Table to delete from
645 * @vifi: VIF identifier to delete
646 * @notify: Set to 1, if the caller is a notifier_call
647 * @head: if unregistering the VIF, place it on this queue
649 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
650 struct list_head *head)
652 struct net *net = read_pnet(&mrt->net);
653 struct vif_device *v;
654 struct net_device *dev;
655 struct in_device *in_dev;
657 if (vifi < 0 || vifi >= mrt->maxvif)
658 return -EADDRNOTAVAIL;
660 v = &mrt->vif_table[vifi];
662 dev = rtnl_dereference(v->dev);
664 return -EADDRNOTAVAIL;
666 spin_lock(&mrt_lock);
667 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, dev,
669 RCU_INIT_POINTER(v->dev, NULL);
671 if (vifi == mrt->mroute_reg_vif_num) {
672 /* Pairs with READ_ONCE() in ipmr_cache_report() and reg_vif_xmit() */
673 WRITE_ONCE(mrt->mroute_reg_vif_num, -1);
675 if (vifi + 1 == mrt->maxvif) {
678 for (tmp = vifi - 1; tmp >= 0; tmp--) {
679 if (VIF_EXISTS(mrt, tmp))
682 WRITE_ONCE(mrt->maxvif, tmp + 1);
685 spin_unlock(&mrt_lock);
687 dev_set_allmulti(dev, -1);
689 in_dev = __in_dev_get_rtnl(dev);
691 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
692 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
693 NETCONFA_MC_FORWARDING,
694 dev->ifindex, &in_dev->cnf);
695 ip_rt_multicast_event(in_dev);
698 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
699 unregister_netdevice_queue(dev, head);
701 netdev_put(dev, &v->dev_tracker);
705 static void ipmr_cache_free_rcu(struct rcu_head *head)
707 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
709 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
712 static void ipmr_cache_free(struct mfc_cache *c)
714 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
717 /* Destroy an unresolved cache entry, killing queued skbs
718 * and reporting error to netlink readers.
720 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
722 struct net *net = read_pnet(&mrt->net);
726 atomic_dec(&mrt->cache_resolve_queue_len);
728 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
729 if (ip_hdr(skb)->version == 0) {
730 struct nlmsghdr *nlh = skb_pull(skb,
731 sizeof(struct iphdr));
732 nlh->nlmsg_type = NLMSG_ERROR;
733 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
734 skb_trim(skb, nlh->nlmsg_len);
736 e->error = -ETIMEDOUT;
737 memset(&e->msg, 0, sizeof(e->msg));
739 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
748 /* Timer process for the unresolved queue. */
749 static void ipmr_expire_process(struct timer_list *t)
751 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
752 struct mr_mfc *c, *next;
753 unsigned long expires;
756 if (!spin_trylock(&mfc_unres_lock)) {
757 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
761 if (list_empty(&mrt->mfc_unres_queue))
767 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
768 if (time_after(c->mfc_un.unres.expires, now)) {
769 unsigned long interval = c->mfc_un.unres.expires - now;
770 if (interval < expires)
776 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
777 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
780 if (!list_empty(&mrt->mfc_unres_queue))
781 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
784 spin_unlock(&mfc_unres_lock);
787 /* Fill oifs list. It is called under locked mrt_lock. */
788 static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
793 cache->mfc_un.res.minvif = MAXVIFS;
794 cache->mfc_un.res.maxvif = 0;
795 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
797 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
798 if (VIF_EXISTS(mrt, vifi) &&
799 ttls[vifi] && ttls[vifi] < 255) {
800 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
801 if (cache->mfc_un.res.minvif > vifi)
802 cache->mfc_un.res.minvif = vifi;
803 if (cache->mfc_un.res.maxvif <= vifi)
804 cache->mfc_un.res.maxvif = vifi + 1;
807 cache->mfc_un.res.lastuse = jiffies;
810 static int vif_add(struct net *net, struct mr_table *mrt,
811 struct vifctl *vifc, int mrtsock)
813 struct netdev_phys_item_id ppid = { };
814 int vifi = vifc->vifc_vifi;
815 struct vif_device *v = &mrt->vif_table[vifi];
816 struct net_device *dev;
817 struct in_device *in_dev;
821 if (VIF_EXISTS(mrt, vifi))
824 switch (vifc->vifc_flags) {
826 if (!ipmr_pimsm_enabled())
828 /* Special Purpose VIF in PIM
829 * All the packets will be sent to the daemon
831 if (mrt->mroute_reg_vif_num >= 0)
833 dev = ipmr_reg_vif(net, mrt);
836 err = dev_set_allmulti(dev, 1);
838 unregister_netdevice(dev);
844 dev = ipmr_new_tunnel(net, vifc);
848 case VIFF_USE_IFINDEX:
850 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
851 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
852 if (dev && !__in_dev_get_rtnl(dev)) {
854 return -EADDRNOTAVAIL;
857 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
860 return -EADDRNOTAVAIL;
861 err = dev_set_allmulti(dev, 1);
871 in_dev = __in_dev_get_rtnl(dev);
874 return -EADDRNOTAVAIL;
876 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
877 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
878 dev->ifindex, &in_dev->cnf);
879 ip_rt_multicast_event(in_dev);
881 /* Fill in the VIF structures */
882 vif_device_init(v, dev, vifc->vifc_rate_limit,
883 vifc->vifc_threshold,
884 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
885 (VIFF_TUNNEL | VIFF_REGISTER));
887 err = dev_get_port_parent_id(dev, &ppid, true);
889 memcpy(v->dev_parent_id.id, ppid.id, ppid.id_len);
890 v->dev_parent_id.id_len = ppid.id_len;
892 v->dev_parent_id.id_len = 0;
895 v->local = vifc->vifc_lcl_addr.s_addr;
896 v->remote = vifc->vifc_rmt_addr.s_addr;
898 /* And finish update writing critical data */
899 spin_lock(&mrt_lock);
900 rcu_assign_pointer(v->dev, dev);
901 netdev_tracker_alloc(dev, &v->dev_tracker, GFP_ATOMIC);
902 if (v->flags & VIFF_REGISTER) {
903 /* Pairs with READ_ONCE() in ipmr_cache_report() and reg_vif_xmit() */
904 WRITE_ONCE(mrt->mroute_reg_vif_num, vifi);
906 if (vifi+1 > mrt->maxvif)
907 WRITE_ONCE(mrt->maxvif, vifi + 1);
908 spin_unlock(&mrt_lock);
909 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, dev,
914 /* called with rcu_read_lock() */
915 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
919 struct mfc_cache_cmp_arg arg = {
920 .mfc_mcastgrp = mcastgrp,
924 return mr_mfc_find(mrt, &arg);
927 /* Look for a (*,G) entry */
928 static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
929 __be32 mcastgrp, int vifi)
931 struct mfc_cache_cmp_arg arg = {
932 .mfc_mcastgrp = mcastgrp,
933 .mfc_origin = htonl(INADDR_ANY)
936 if (mcastgrp == htonl(INADDR_ANY))
937 return mr_mfc_find_any_parent(mrt, vifi);
938 return mr_mfc_find_any(mrt, vifi, &arg);
941 /* Look for a (S,G,iif) entry if parent != -1 */
942 static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
943 __be32 origin, __be32 mcastgrp,
946 struct mfc_cache_cmp_arg arg = {
947 .mfc_mcastgrp = mcastgrp,
948 .mfc_origin = origin,
951 return mr_mfc_find_parent(mrt, &arg, parent);
954 /* Allocate a multicast cache entry */
955 static struct mfc_cache *ipmr_cache_alloc(void)
957 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
960 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
961 c->_c.mfc_un.res.minvif = MAXVIFS;
962 c->_c.free = ipmr_cache_free_rcu;
963 refcount_set(&c->_c.mfc_un.res.refcount, 1);
968 static struct mfc_cache *ipmr_cache_alloc_unres(void)
970 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
973 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
974 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
979 /* A cache entry has gone into a resolved state from queued */
980 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
981 struct mfc_cache *uc, struct mfc_cache *c)
986 /* Play the pending entries through our router */
987 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
988 if (ip_hdr(skb)->version == 0) {
989 struct nlmsghdr *nlh = skb_pull(skb,
990 sizeof(struct iphdr));
992 if (mr_fill_mroute(mrt, skb, &c->_c,
993 nlmsg_data(nlh)) > 0) {
994 nlh->nlmsg_len = skb_tail_pointer(skb) -
997 nlh->nlmsg_type = NLMSG_ERROR;
998 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
999 skb_trim(skb, nlh->nlmsg_len);
1000 e = nlmsg_data(nlh);
1001 e->error = -EMSGSIZE;
1002 memset(&e->msg, 0, sizeof(e->msg));
1005 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
1008 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
1014 /* Bounce a cache query up to mrouted and netlink.
1016 * Called under rcu_read_lock().
1018 static int ipmr_cache_report(const struct mr_table *mrt,
1019 struct sk_buff *pkt, vifi_t vifi, int assert)
1021 const int ihl = ip_hdrlen(pkt);
1022 struct sock *mroute_sk;
1023 struct igmphdr *igmp;
1024 struct igmpmsg *msg;
1025 struct sk_buff *skb;
1028 mroute_sk = rcu_dereference(mrt->mroute_sk);
1032 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE)
1033 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1035 skb = alloc_skb(128, GFP_ATOMIC);
1040 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) {
1041 /* Ugly, but we have no choice with this interface.
1042 * Duplicate old header, fix ihl, length etc.
1043 * And all this only to mangle msg->im_msgtype and
1044 * to set msg->im_mbz to "mbz" :-)
1046 skb_push(skb, sizeof(struct iphdr));
1047 skb_reset_network_header(skb);
1048 skb_reset_transport_header(skb);
1049 msg = (struct igmpmsg *)skb_network_header(skb);
1050 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
1051 msg->im_msgtype = assert;
1053 if (assert == IGMPMSG_WRVIFWHOLE) {
1055 msg->im_vif_hi = vifi >> 8;
1057 /* Pairs with WRITE_ONCE() in vif_add() and vif_delete() */
1058 int vif_num = READ_ONCE(mrt->mroute_reg_vif_num);
1060 msg->im_vif = vif_num;
1061 msg->im_vif_hi = vif_num >> 8;
1063 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1064 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1065 sizeof(struct iphdr));
1067 /* Copy the IP header */
1068 skb_set_network_header(skb, skb->len);
1070 skb_copy_to_linear_data(skb, pkt->data, ihl);
1071 /* Flag to the kernel this is a route add */
1072 ip_hdr(skb)->protocol = 0;
1073 msg = (struct igmpmsg *)skb_network_header(skb);
1075 msg->im_vif_hi = vifi >> 8;
1076 ipv4_pktinfo_prepare(mroute_sk, pkt, false);
1077 memcpy(skb->cb, pkt->cb, sizeof(skb->cb));
1078 /* Add our header */
1079 igmp = skb_put(skb, sizeof(struct igmphdr));
1080 igmp->type = assert;
1081 msg->im_msgtype = assert;
1083 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1084 skb->transport_header = skb->network_header;
1087 igmpmsg_netlink_event(mrt, skb);
1089 /* Deliver to mrouted */
1090 ret = sock_queue_rcv_skb(mroute_sk, skb);
1093 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1100 /* Queue a packet for resolution. It gets locked cache entry! */
1101 /* Called under rcu_read_lock() */
1102 static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1103 struct sk_buff *skb, struct net_device *dev)
1105 const struct iphdr *iph = ip_hdr(skb);
1106 struct mfc_cache *c;
1110 spin_lock_bh(&mfc_unres_lock);
1111 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
1112 if (c->mfc_mcastgrp == iph->daddr &&
1113 c->mfc_origin == iph->saddr) {
1120 /* Create a new entry if allowable */
1121 c = ipmr_cache_alloc_unres();
1123 spin_unlock_bh(&mfc_unres_lock);
1129 /* Fill in the new cache entry */
1130 c->_c.mfc_parent = -1;
1131 c->mfc_origin = iph->saddr;
1132 c->mfc_mcastgrp = iph->daddr;
1134 /* Reflect first query at mrouted. */
1135 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1138 /* If the report failed throw the cache entry
1141 spin_unlock_bh(&mfc_unres_lock);
1148 atomic_inc(&mrt->cache_resolve_queue_len);
1149 list_add(&c->_c.list, &mrt->mfc_unres_queue);
1150 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1152 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1153 mod_timer(&mrt->ipmr_expire_timer,
1154 c->_c.mfc_un.unres.expires);
1157 /* See if we can append the packet */
1158 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
1164 skb->skb_iif = dev->ifindex;
1166 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
1170 spin_unlock_bh(&mfc_unres_lock);
1174 /* MFC cache manipulation by user space mroute daemon */
1176 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1178 struct net *net = read_pnet(&mrt->net);
1179 struct mfc_cache *c;
1181 /* The entries are added/deleted only under RTNL */
1183 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1184 mfc->mfcc_mcastgrp.s_addr, parent);
1188 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1189 list_del_rcu(&c->_c.list);
1190 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
1191 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1192 mr_cache_put(&c->_c);
1197 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1198 struct mfcctl *mfc, int mrtsock, int parent)
1200 struct mfc_cache *uc, *c;
1205 if (mfc->mfcc_parent >= MAXVIFS)
1208 /* The entries are added/deleted only under RTNL */
1210 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1211 mfc->mfcc_mcastgrp.s_addr, parent);
1214 spin_lock(&mrt_lock);
1215 c->_c.mfc_parent = mfc->mfcc_parent;
1216 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1218 c->_c.mfc_flags |= MFC_STATIC;
1219 spin_unlock(&mrt_lock);
1220 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1222 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1226 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1227 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1230 c = ipmr_cache_alloc();
1234 c->mfc_origin = mfc->mfcc_origin.s_addr;
1235 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
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;
1241 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
1244 pr_err("ipmr: rhtable insert error %d\n", ret);
1248 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
1249 /* Check to see if we resolved a queued list. If so we
1250 * need to send on the frames and tidy up.
1253 spin_lock_bh(&mfc_unres_lock);
1254 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1255 uc = (struct mfc_cache *)_uc;
1256 if (uc->mfc_origin == c->mfc_origin &&
1257 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1258 list_del(&_uc->list);
1259 atomic_dec(&mrt->cache_resolve_queue_len);
1264 if (list_empty(&mrt->mfc_unres_queue))
1265 del_timer(&mrt->ipmr_expire_timer);
1266 spin_unlock_bh(&mfc_unres_lock);
1269 ipmr_cache_resolve(net, mrt, uc, c);
1270 ipmr_cache_free(uc);
1272 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
1273 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1277 /* Close the multicast socket, and clear the vif tables etc */
1278 static void mroute_clean_tables(struct mr_table *mrt, int flags)
1280 struct net *net = read_pnet(&mrt->net);
1281 struct mr_mfc *c, *tmp;
1282 struct mfc_cache *cache;
1286 /* Shut down all active vif entries */
1287 if (flags & (MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC)) {
1288 for (i = 0; i < mrt->maxvif; i++) {
1289 if (((mrt->vif_table[i].flags & VIFF_STATIC) &&
1290 !(flags & MRT_FLUSH_VIFS_STATIC)) ||
1291 (!(mrt->vif_table[i].flags & VIFF_STATIC) && !(flags & MRT_FLUSH_VIFS)))
1293 vif_delete(mrt, i, 0, &list);
1295 unregister_netdevice_many(&list);
1298 /* Wipe the cache */
1299 if (flags & (MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC)) {
1300 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1301 if (((c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC_STATIC)) ||
1302 (!(c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC)))
1304 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1305 list_del_rcu(&c->list);
1306 cache = (struct mfc_cache *)c;
1307 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
1309 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1314 if (flags & MRT_FLUSH_MFC) {
1315 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1316 spin_lock_bh(&mfc_unres_lock);
1317 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1319 cache = (struct mfc_cache *)c;
1320 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1321 ipmr_destroy_unres(mrt, cache);
1323 spin_unlock_bh(&mfc_unres_lock);
1328 /* called from ip_ra_control(), before an RCU grace period,
1329 * we don't need to call synchronize_rcu() here
1331 static void mrtsock_destruct(struct sock *sk)
1333 struct net *net = sock_net(sk);
1334 struct mr_table *mrt;
1337 ipmr_for_each_table(mrt, net) {
1338 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1339 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1340 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1341 NETCONFA_MC_FORWARDING,
1342 NETCONFA_IFINDEX_ALL,
1343 net->ipv4.devconf_all);
1344 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1345 mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_MFC);
1351 /* Socket options and virtual interface manipulation. The whole
1352 * virtual interface system is a complete heap, but unfortunately
1353 * that's how BSD mrouted happens to think. Maybe one day with a proper
1354 * MOSPF/PIM router set up we can clean this up.
1357 int ip_mroute_setsockopt(struct sock *sk, int optname, sockptr_t optval,
1358 unsigned int optlen)
1360 struct net *net = sock_net(sk);
1361 int val, ret = 0, parent = 0;
1362 struct mr_table *mrt;
1368 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1370 if (sk->sk_type != SOCK_RAW ||
1371 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1376 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1381 if (optname != MRT_INIT) {
1382 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1383 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1391 if (optlen != sizeof(int)) {
1395 if (rtnl_dereference(mrt->mroute_sk)) {
1400 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1402 rcu_assign_pointer(mrt->mroute_sk, sk);
1403 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1404 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1405 NETCONFA_MC_FORWARDING,
1406 NETCONFA_IFINDEX_ALL,
1407 net->ipv4.devconf_all);
1411 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1414 /* We need to unlock here because mrtsock_destruct takes
1415 * care of rtnl itself and we can't change that due to
1416 * the IP_ROUTER_ALERT setsockopt which runs without it.
1419 ret = ip_ra_control(sk, 0, NULL);
1425 if (optlen != sizeof(vif)) {
1429 if (copy_from_sockptr(&vif, optval, sizeof(vif))) {
1433 if (vif.vifc_vifi >= MAXVIFS) {
1437 if (optname == MRT_ADD_VIF) {
1438 ret = vif_add(net, mrt, &vif,
1439 sk == rtnl_dereference(mrt->mroute_sk));
1441 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1444 /* Manipulate the forwarding caches. These live
1445 * in a sort of kernel/user symbiosis.
1451 case MRT_ADD_MFC_PROXY:
1452 case MRT_DEL_MFC_PROXY:
1453 if (optlen != sizeof(mfc)) {
1457 if (copy_from_sockptr(&mfc, optval, sizeof(mfc))) {
1462 parent = mfc.mfcc_parent;
1463 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1464 ret = ipmr_mfc_delete(mrt, &mfc, parent);
1466 ret = ipmr_mfc_add(net, mrt, &mfc,
1467 sk == rtnl_dereference(mrt->mroute_sk),
1471 if (optlen != sizeof(val)) {
1475 if (copy_from_sockptr(&val, optval, sizeof(val))) {
1479 mroute_clean_tables(mrt, val);
1481 /* Control PIM assert. */
1483 if (optlen != sizeof(val)) {
1487 if (copy_from_sockptr(&val, optval, sizeof(val))) {
1491 mrt->mroute_do_assert = val;
1494 if (!ipmr_pimsm_enabled()) {
1498 if (optlen != sizeof(val)) {
1502 if (copy_from_sockptr(&val, optval, sizeof(val))) {
1507 do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE);
1509 if (val != mrt->mroute_do_pim) {
1510 mrt->mroute_do_pim = val;
1511 mrt->mroute_do_assert = val;
1512 mrt->mroute_do_wrvifwhole = do_wrvifwhole;
1516 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1520 if (optlen != sizeof(uval)) {
1524 if (copy_from_sockptr(&uval, optval, sizeof(uval))) {
1529 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1532 mrt = ipmr_new_table(net, uval);
1536 raw_sk(sk)->ipmr_table = uval;
1539 /* Spurious command, or MRT_VERSION which you cannot set. */
1549 /* Execute if this ioctl is a special mroute ioctl */
1550 int ipmr_sk_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1553 /* These userspace buffers will be consumed by ipmr_ioctl() */
1554 case SIOCGETVIFCNT: {
1555 struct sioc_vif_req buffer;
1557 return sock_ioctl_inout(sk, cmd, arg, &buffer,
1560 case SIOCGETSGCNT: {
1561 struct sioc_sg_req buffer;
1563 return sock_ioctl_inout(sk, cmd, arg, &buffer,
1567 /* return code > 0 means that the ioctl was not executed */
1571 /* Getsock opt support for the multicast routing system. */
1572 int ip_mroute_getsockopt(struct sock *sk, int optname, sockptr_t optval,
1577 struct net *net = sock_net(sk);
1578 struct mr_table *mrt;
1580 if (sk->sk_type != SOCK_RAW ||
1581 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1584 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1593 if (!ipmr_pimsm_enabled())
1594 return -ENOPROTOOPT;
1595 val = mrt->mroute_do_pim;
1598 val = mrt->mroute_do_assert;
1601 return -ENOPROTOOPT;
1604 if (copy_from_sockptr(&olr, optlen, sizeof(int)))
1609 olr = min_t(unsigned int, olr, sizeof(int));
1611 if (copy_to_sockptr(optlen, &olr, sizeof(int)))
1613 if (copy_to_sockptr(optval, &val, olr))
1618 /* The IP multicast ioctl support routines. */
1619 int ipmr_ioctl(struct sock *sk, int cmd, void *arg)
1621 struct vif_device *vif;
1622 struct mfc_cache *c;
1623 struct net *net = sock_net(sk);
1624 struct sioc_vif_req *vr;
1625 struct sioc_sg_req *sr;
1626 struct mr_table *mrt;
1628 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1634 vr = (struct sioc_vif_req *)arg;
1635 if (vr->vifi >= mrt->maxvif)
1637 vr->vifi = array_index_nospec(vr->vifi, mrt->maxvif);
1639 vif = &mrt->vif_table[vr->vifi];
1640 if (VIF_EXISTS(mrt, vr->vifi)) {
1641 vr->icount = READ_ONCE(vif->pkt_in);
1642 vr->ocount = READ_ONCE(vif->pkt_out);
1643 vr->ibytes = READ_ONCE(vif->bytes_in);
1644 vr->obytes = READ_ONCE(vif->bytes_out);
1650 return -EADDRNOTAVAIL;
1652 sr = (struct sioc_sg_req *)arg;
1655 c = ipmr_cache_find(mrt, sr->src.s_addr, sr->grp.s_addr);
1657 sr->pktcnt = c->_c.mfc_un.res.pkt;
1658 sr->bytecnt = c->_c.mfc_un.res.bytes;
1659 sr->wrong_if = c->_c.mfc_un.res.wrong_if;
1664 return -EADDRNOTAVAIL;
1666 return -ENOIOCTLCMD;
1670 #ifdef CONFIG_COMPAT
1671 struct compat_sioc_sg_req {
1674 compat_ulong_t pktcnt;
1675 compat_ulong_t bytecnt;
1676 compat_ulong_t wrong_if;
1679 struct compat_sioc_vif_req {
1680 vifi_t vifi; /* Which iface */
1681 compat_ulong_t icount;
1682 compat_ulong_t ocount;
1683 compat_ulong_t ibytes;
1684 compat_ulong_t obytes;
1687 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1689 struct compat_sioc_sg_req sr;
1690 struct compat_sioc_vif_req vr;
1691 struct vif_device *vif;
1692 struct mfc_cache *c;
1693 struct net *net = sock_net(sk);
1694 struct mr_table *mrt;
1696 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1702 if (copy_from_user(&vr, arg, sizeof(vr)))
1704 if (vr.vifi >= mrt->maxvif)
1706 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
1708 vif = &mrt->vif_table[vr.vifi];
1709 if (VIF_EXISTS(mrt, vr.vifi)) {
1710 vr.icount = READ_ONCE(vif->pkt_in);
1711 vr.ocount = READ_ONCE(vif->pkt_out);
1712 vr.ibytes = READ_ONCE(vif->bytes_in);
1713 vr.obytes = READ_ONCE(vif->bytes_out);
1716 if (copy_to_user(arg, &vr, sizeof(vr)))
1721 return -EADDRNOTAVAIL;
1723 if (copy_from_user(&sr, arg, sizeof(sr)))
1727 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1729 sr.pktcnt = c->_c.mfc_un.res.pkt;
1730 sr.bytecnt = c->_c.mfc_un.res.bytes;
1731 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1734 if (copy_to_user(arg, &sr, sizeof(sr)))
1739 return -EADDRNOTAVAIL;
1741 return -ENOIOCTLCMD;
1746 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1748 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1749 struct net *net = dev_net(dev);
1750 struct mr_table *mrt;
1751 struct vif_device *v;
1754 if (event != NETDEV_UNREGISTER)
1757 ipmr_for_each_table(mrt, net) {
1758 v = &mrt->vif_table[0];
1759 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1760 if (rcu_access_pointer(v->dev) == dev)
1761 vif_delete(mrt, ct, 1, NULL);
1767 static struct notifier_block ip_mr_notifier = {
1768 .notifier_call = ipmr_device_event,
1771 /* Encapsulate a packet by attaching a valid IPIP header to it.
1772 * This avoids tunnel drivers and other mess and gives us the speed so
1773 * important for multicast video.
1775 static void ip_encap(struct net *net, struct sk_buff *skb,
1776 __be32 saddr, __be32 daddr)
1779 const struct iphdr *old_iph = ip_hdr(skb);
1781 skb_push(skb, sizeof(struct iphdr));
1782 skb->transport_header = skb->network_header;
1783 skb_reset_network_header(skb);
1787 iph->tos = old_iph->tos;
1788 iph->ttl = old_iph->ttl;
1792 iph->protocol = IPPROTO_IPIP;
1794 iph->tot_len = htons(skb->len);
1795 ip_select_ident(net, skb, NULL);
1798 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1802 static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1803 struct sk_buff *skb)
1805 struct ip_options *opt = &(IPCB(skb)->opt);
1807 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1809 if (unlikely(opt->optlen))
1810 ip_forward_options(skb);
1812 return dst_output(net, sk, skb);
1815 #ifdef CONFIG_NET_SWITCHDEV
1816 static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1817 int in_vifi, int out_vifi)
1819 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1820 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1822 if (!skb->offload_l3_fwd_mark)
1824 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1826 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1827 &in_vif->dev_parent_id);
1830 static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1831 int in_vifi, int out_vifi)
1837 /* Processing handlers for ipmr_forward, under rcu_read_lock() */
1839 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1840 int in_vifi, struct sk_buff *skb, int vifi)
1842 const struct iphdr *iph = ip_hdr(skb);
1843 struct vif_device *vif = &mrt->vif_table[vifi];
1844 struct net_device *vif_dev;
1845 struct net_device *dev;
1850 vif_dev = vif_dev_read(vif);
1854 if (vif->flags & VIFF_REGISTER) {
1855 WRITE_ONCE(vif->pkt_out, vif->pkt_out + 1);
1856 WRITE_ONCE(vif->bytes_out, vif->bytes_out + skb->len);
1857 DEV_STATS_ADD(vif_dev, tx_bytes, skb->len);
1858 DEV_STATS_INC(vif_dev, tx_packets);
1859 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1863 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1866 if (vif->flags & VIFF_TUNNEL) {
1867 rt = ip_route_output_ports(net, &fl4, NULL,
1868 vif->remote, vif->local,
1871 RT_TOS(iph->tos), vif->link);
1874 encap = sizeof(struct iphdr);
1876 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1879 RT_TOS(iph->tos), vif->link);
1886 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1887 /* Do not fragment multicasts. Alas, IPv4 does not
1888 * allow to send ICMP, so that packets will disappear
1891 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1896 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1898 if (skb_cow(skb, encap)) {
1903 WRITE_ONCE(vif->pkt_out, vif->pkt_out + 1);
1904 WRITE_ONCE(vif->bytes_out, vif->bytes_out + skb->len);
1907 skb_dst_set(skb, &rt->dst);
1908 ip_decrease_ttl(ip_hdr(skb));
1910 /* FIXME: forward and output firewalls used to be called here.
1911 * What do we do with netfilter? -- RR
1913 if (vif->flags & VIFF_TUNNEL) {
1914 ip_encap(net, skb, vif->local, vif->remote);
1915 /* FIXME: extra output firewall step used to be here. --RR */
1916 DEV_STATS_INC(vif_dev, tx_packets);
1917 DEV_STATS_ADD(vif_dev, tx_bytes, skb->len);
1920 IPCB(skb)->flags |= IPSKB_FORWARDED;
1922 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1923 * not only before forwarding, but after forwarding on all output
1924 * interfaces. It is clear, if mrouter runs a multicasting
1925 * program, it should receive packets not depending to what interface
1926 * program is joined.
1927 * If we will not make it, the program will have to join on all
1928 * interfaces. On the other hand, multihoming host (or router, but
1929 * not mrouter) cannot join to more than one interface - it will
1930 * result in receiving multiple packets.
1932 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1933 net, NULL, skb, skb->dev, dev,
1934 ipmr_forward_finish);
1941 /* Called with mrt_lock or rcu_read_lock() */
1942 static int ipmr_find_vif(const struct mr_table *mrt, struct net_device *dev)
1945 /* Pairs with WRITE_ONCE() in vif_delete()/vif_add() */
1946 for (ct = READ_ONCE(mrt->maxvif) - 1; ct >= 0; ct--) {
1947 if (rcu_access_pointer(mrt->vif_table[ct].dev) == dev)
1953 /* "local" means that we should preserve one skb (for local delivery) */
1954 /* Called uner rcu_read_lock() */
1955 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1956 struct net_device *dev, struct sk_buff *skb,
1957 struct mfc_cache *c, int local)
1959 int true_vifi = ipmr_find_vif(mrt, dev);
1963 vif = c->_c.mfc_parent;
1964 c->_c.mfc_un.res.pkt++;
1965 c->_c.mfc_un.res.bytes += skb->len;
1966 c->_c.mfc_un.res.lastuse = jiffies;
1968 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1969 struct mfc_cache *cache_proxy;
1971 /* For an (*,G) entry, we only check that the incoming
1972 * interface is part of the static tree.
1974 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
1976 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
1980 /* Wrong interface: drop packet and (maybe) send PIM assert. */
1981 if (rcu_access_pointer(mrt->vif_table[vif].dev) != dev) {
1982 if (rt_is_output_route(skb_rtable(skb))) {
1983 /* It is our own packet, looped back.
1984 * Very complicated situation...
1986 * The best workaround until routing daemons will be
1987 * fixed is not to redistribute packet, if it was
1988 * send through wrong interface. It means, that
1989 * multicast applications WILL NOT work for
1990 * (S,G), which have default multicast route pointing
1991 * to wrong oif. In any case, it is not a good
1992 * idea to use multicasting applications on router.
1997 c->_c.mfc_un.res.wrong_if++;
1999 if (true_vifi >= 0 && mrt->mroute_do_assert &&
2000 /* pimsm uses asserts, when switching from RPT to SPT,
2001 * so that we cannot check that packet arrived on an oif.
2002 * It is bad, but otherwise we would need to move pretty
2003 * large chunk of pimd to kernel. Ough... --ANK
2005 (mrt->mroute_do_pim ||
2006 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
2008 c->_c.mfc_un.res.last_assert +
2009 MFC_ASSERT_THRESH)) {
2010 c->_c.mfc_un.res.last_assert = jiffies;
2011 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
2012 if (mrt->mroute_do_wrvifwhole)
2013 ipmr_cache_report(mrt, skb, true_vifi,
2014 IGMPMSG_WRVIFWHOLE);
2020 WRITE_ONCE(mrt->vif_table[vif].pkt_in,
2021 mrt->vif_table[vif].pkt_in + 1);
2022 WRITE_ONCE(mrt->vif_table[vif].bytes_in,
2023 mrt->vif_table[vif].bytes_in + skb->len);
2025 /* Forward the frame */
2026 if (c->mfc_origin == htonl(INADDR_ANY) &&
2027 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
2028 if (true_vifi >= 0 &&
2029 true_vifi != c->_c.mfc_parent &&
2031 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
2032 /* It's an (*,*) entry and the packet is not coming from
2033 * the upstream: forward the packet to the upstream
2036 psend = c->_c.mfc_parent;
2041 for (ct = c->_c.mfc_un.res.maxvif - 1;
2042 ct >= c->_c.mfc_un.res.minvif; ct--) {
2043 /* For (*,G) entry, don't forward to the incoming interface */
2044 if ((c->mfc_origin != htonl(INADDR_ANY) ||
2046 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
2048 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2051 ipmr_queue_xmit(net, mrt, true_vifi,
2060 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2063 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
2066 ipmr_queue_xmit(net, mrt, true_vifi, skb, 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 vif = ipmr_find_vif(mrt, dev);
2181 return ipmr_cache_unresolved(mrt, vif, skb, dev);
2186 ip_mr_forward(net, mrt, dev, skb, cache, local);
2189 return ip_local_deliver(skb);
2195 return ip_local_deliver(skb);
2200 #ifdef CONFIG_IP_PIMSM_V1
2201 /* Handle IGMP messages of PIMv1 */
2202 int pim_rcv_v1(struct sk_buff *skb)
2204 struct igmphdr *pim;
2205 struct net *net = dev_net(skb->dev);
2206 struct mr_table *mrt;
2208 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2211 pim = igmp_hdr(skb);
2213 mrt = ipmr_rt_fib_lookup(net, skb);
2216 if (!mrt->mroute_do_pim ||
2217 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2220 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2228 #ifdef CONFIG_IP_PIMSM_V2
2229 static int pim_rcv(struct sk_buff *skb)
2231 struct pimreghdr *pim;
2232 struct net *net = dev_net(skb->dev);
2233 struct mr_table *mrt;
2235 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2238 pim = (struct pimreghdr *)skb_transport_header(skb);
2239 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
2240 (pim->flags & PIM_NULL_REGISTER) ||
2241 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2242 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2245 mrt = ipmr_rt_fib_lookup(net, skb);
2248 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2256 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2257 __be32 saddr, __be32 daddr,
2258 struct rtmsg *rtm, u32 portid)
2260 struct mfc_cache *cache;
2261 struct mr_table *mrt;
2264 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2269 cache = ipmr_cache_find(mrt, saddr, daddr);
2270 if (!cache && skb->dev) {
2271 int vif = ipmr_find_vif(mrt, skb->dev);
2274 cache = ipmr_cache_find_any(mrt, daddr, vif);
2277 struct sk_buff *skb2;
2279 struct net_device *dev;
2284 vif = ipmr_find_vif(mrt, dev);
2290 skb2 = skb_realloc_headroom(skb, sizeof(struct iphdr));
2296 NETLINK_CB(skb2).portid = portid;
2297 skb_push(skb2, sizeof(struct iphdr));
2298 skb_reset_network_header(skb2);
2300 iph->ihl = sizeof(struct iphdr) >> 2;
2304 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
2309 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
2314 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2315 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2318 struct nlmsghdr *nlh;
2322 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2326 rtm = nlmsg_data(nlh);
2327 rtm->rtm_family = RTNL_FAMILY_IPMR;
2328 rtm->rtm_dst_len = 32;
2329 rtm->rtm_src_len = 32;
2331 rtm->rtm_table = mrt->id;
2332 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2333 goto nla_put_failure;
2334 rtm->rtm_type = RTN_MULTICAST;
2335 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2336 if (c->_c.mfc_flags & MFC_STATIC)
2337 rtm->rtm_protocol = RTPROT_STATIC;
2339 rtm->rtm_protocol = RTPROT_MROUTED;
2342 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2343 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2344 goto nla_put_failure;
2345 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
2346 /* do not break the dump if cache is unresolved */
2347 if (err < 0 && err != -ENOENT)
2348 goto nla_put_failure;
2350 nlmsg_end(skb, nlh);
2354 nlmsg_cancel(skb, nlh);
2358 static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2359 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2362 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2366 static size_t mroute_msgsize(bool unresolved, int maxvif)
2369 NLMSG_ALIGN(sizeof(struct rtmsg))
2370 + nla_total_size(4) /* RTA_TABLE */
2371 + nla_total_size(4) /* RTA_SRC */
2372 + nla_total_size(4) /* RTA_DST */
2377 + nla_total_size(4) /* RTA_IIF */
2378 + nla_total_size(0) /* RTA_MULTIPATH */
2379 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2381 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
2387 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2390 struct net *net = read_pnet(&mrt->net);
2391 struct sk_buff *skb;
2394 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2400 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2404 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2410 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2413 static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2416 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2417 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2418 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2419 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2420 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2421 + nla_total_size(4) /* IPMRA_CREPORT_TABLE */
2422 /* IPMRA_CREPORT_PKT */
2423 + nla_total_size(payloadlen)
2429 static void igmpmsg_netlink_event(const struct mr_table *mrt, struct sk_buff *pkt)
2431 struct net *net = read_pnet(&mrt->net);
2432 struct nlmsghdr *nlh;
2433 struct rtgenmsg *rtgenm;
2434 struct igmpmsg *msg;
2435 struct sk_buff *skb;
2439 payloadlen = pkt->len - sizeof(struct igmpmsg);
2440 msg = (struct igmpmsg *)skb_network_header(pkt);
2442 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2446 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2447 sizeof(struct rtgenmsg), 0);
2450 rtgenm = nlmsg_data(nlh);
2451 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2452 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2453 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif | (msg->im_vif_hi << 8)) ||
2454 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2455 msg->im_src.s_addr) ||
2456 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2457 msg->im_dst.s_addr) ||
2458 nla_put_u32(skb, IPMRA_CREPORT_TABLE, mrt->id))
2459 goto nla_put_failure;
2461 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2462 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2463 nla_data(nla), payloadlen))
2464 goto nla_put_failure;
2466 nlmsg_end(skb, nlh);
2468 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2472 nlmsg_cancel(skb, nlh);
2475 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2478 static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb,
2479 const struct nlmsghdr *nlh,
2481 struct netlink_ext_ack *extack)
2486 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
2487 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for multicast route get request");
2491 if (!netlink_strict_get_check(skb))
2492 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
2493 rtm_ipv4_policy, extack);
2495 rtm = nlmsg_data(nlh);
2496 if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
2497 (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
2498 rtm->rtm_tos || rtm->rtm_table || rtm->rtm_protocol ||
2499 rtm->rtm_scope || rtm->rtm_type || rtm->rtm_flags) {
2500 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for multicast route get request");
2504 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
2505 rtm_ipv4_policy, extack);
2509 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
2510 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
2511 NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
2515 for (i = 0; i <= RTA_MAX; i++) {
2525 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in multicast route get request");
2533 static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2534 struct netlink_ext_ack *extack)
2536 struct net *net = sock_net(in_skb->sk);
2537 struct nlattr *tb[RTA_MAX + 1];
2538 struct sk_buff *skb = NULL;
2539 struct mfc_cache *cache;
2540 struct mr_table *mrt;
2545 err = ipmr_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
2549 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2550 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2551 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2553 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
2559 /* entries are added/deleted only under RTNL */
2561 cache = ipmr_cache_find(mrt, src, grp);
2568 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2574 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2575 nlh->nlmsg_seq, cache,
2580 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2590 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2592 struct fib_dump_filter filter = {
2597 if (cb->strict_check) {
2598 err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh,
2604 if (filter.table_id) {
2605 struct mr_table *mrt;
2607 mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id);
2609 if (rtnl_msg_family(cb->nlh) != RTNL_FAMILY_IPMR)
2612 NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist");
2615 err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute,
2616 &mfc_unres_lock, &filter);
2617 return skb->len ? : err;
2620 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
2621 _ipmr_fill_mroute, &mfc_unres_lock, &filter);
2624 static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2625 [RTA_SRC] = { .type = NLA_U32 },
2626 [RTA_DST] = { .type = NLA_U32 },
2627 [RTA_IIF] = { .type = NLA_U32 },
2628 [RTA_TABLE] = { .type = NLA_U32 },
2629 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2632 static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2634 switch (rtm_protocol) {
2636 case RTPROT_MROUTED:
2642 static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2644 struct rtnexthop *rtnh = nla_data(nla);
2645 int remaining = nla_len(nla), vifi = 0;
2647 while (rtnh_ok(rtnh, remaining)) {
2648 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2649 if (++vifi == MAXVIFS)
2651 rtnh = rtnh_next(rtnh, &remaining);
2654 return remaining > 0 ? -EINVAL : vifi;
2657 /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2658 static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2659 struct mfcctl *mfcc, int *mrtsock,
2660 struct mr_table **mrtret,
2661 struct netlink_ext_ack *extack)
2663 struct net_device *dev = NULL;
2664 u32 tblid = RT_TABLE_DEFAULT;
2665 struct mr_table *mrt;
2666 struct nlattr *attr;
2670 ret = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
2671 rtm_ipmr_policy, extack);
2674 rtm = nlmsg_data(nlh);
2677 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2678 rtm->rtm_type != RTN_MULTICAST ||
2679 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2680 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2683 memset(mfcc, 0, sizeof(*mfcc));
2684 mfcc->mfcc_parent = -1;
2686 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2687 switch (nla_type(attr)) {
2689 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2692 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2695 dev = __dev_get_by_index(net, nla_get_u32(attr));
2702 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2711 tblid = nla_get_u32(attr);
2715 mrt = ipmr_get_table(net, tblid);
2721 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2723 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2729 /* takes care of both newroute and delroute */
2730 static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2731 struct netlink_ext_ack *extack)
2733 struct net *net = sock_net(skb->sk);
2734 int ret, mrtsock, parent;
2735 struct mr_table *tbl;
2740 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
2744 parent = ret ? mfcc.mfcc_parent : -1;
2745 if (nlh->nlmsg_type == RTM_NEWROUTE)
2746 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2748 return ipmr_mfc_delete(tbl, &mfcc, parent);
2751 static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2753 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2755 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2756 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2757 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2758 mrt->mroute_reg_vif_num) ||
2759 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2760 mrt->mroute_do_assert) ||
2761 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
2762 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
2763 mrt->mroute_do_wrvifwhole))
2769 static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2771 struct net_device *vif_dev;
2772 struct nlattr *vif_nest;
2773 struct vif_device *vif;
2775 vif = &mrt->vif_table[vifid];
2776 vif_dev = rtnl_dereference(vif->dev);
2777 /* if the VIF doesn't exist just continue */
2781 vif_nest = nla_nest_start_noflag(skb, IPMRA_VIF);
2785 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif_dev->ifindex) ||
2786 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2787 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2788 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2790 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2792 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2794 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2796 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2797 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2798 nla_nest_cancel(skb, vif_nest);
2801 nla_nest_end(skb, vif_nest);
2806 static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
2807 struct netlink_ext_ack *extack)
2809 struct ifinfomsg *ifm;
2811 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2812 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
2816 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
2817 NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump");
2821 ifm = nlmsg_data(nlh);
2822 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2823 ifm->ifi_change || ifm->ifi_index) {
2824 NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
2831 static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2833 struct net *net = sock_net(skb->sk);
2834 struct nlmsghdr *nlh = NULL;
2835 unsigned int t = 0, s_t;
2836 unsigned int e = 0, s_e;
2837 struct mr_table *mrt;
2839 if (cb->strict_check) {
2840 int err = ipmr_valid_dumplink(cb->nlh, cb->extack);
2849 ipmr_for_each_table(mrt, net) {
2850 struct nlattr *vifs, *af;
2851 struct ifinfomsg *hdr;
2856 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2857 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2858 sizeof(*hdr), NLM_F_MULTI);
2862 hdr = nlmsg_data(nlh);
2863 memset(hdr, 0, sizeof(*hdr));
2864 hdr->ifi_family = RTNL_FAMILY_IPMR;
2866 af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
2868 nlmsg_cancel(skb, nlh);
2872 if (!ipmr_fill_table(mrt, skb)) {
2873 nlmsg_cancel(skb, nlh);
2877 vifs = nla_nest_start_noflag(skb, IPMRA_TABLE_VIFS);
2879 nla_nest_end(skb, af);
2880 nlmsg_end(skb, nlh);
2883 for (i = 0; i < mrt->maxvif; i++) {
2886 if (!ipmr_fill_vif(mrt, i, skb)) {
2887 nla_nest_end(skb, vifs);
2888 nla_nest_end(skb, af);
2889 nlmsg_end(skb, nlh);
2897 nla_nest_end(skb, vifs);
2898 nla_nest_end(skb, af);
2899 nlmsg_end(skb, nlh);
2911 #ifdef CONFIG_PROC_FS
2912 /* The /proc interfaces to multicast routing :
2913 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2916 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2919 struct mr_vif_iter *iter = seq->private;
2920 struct net *net = seq_file_net(seq);
2921 struct mr_table *mrt;
2923 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2925 return ERR_PTR(-ENOENT);
2930 return mr_vif_seq_start(seq, pos);
2933 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2939 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2941 struct mr_vif_iter *iter = seq->private;
2942 struct mr_table *mrt = iter->mrt;
2944 if (v == SEQ_START_TOKEN) {
2946 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2948 const struct vif_device *vif = v;
2949 const struct net_device *vif_dev;
2952 vif_dev = vif_dev_read(vif);
2953 name = vif_dev ? vif_dev->name : "none";
2955 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2956 vif - mrt->vif_table,
2957 name, vif->bytes_in, vif->pkt_in,
2958 vif->bytes_out, vif->pkt_out,
2959 vif->flags, vif->local, vif->remote);
2964 static const struct seq_operations ipmr_vif_seq_ops = {
2965 .start = ipmr_vif_seq_start,
2966 .next = mr_vif_seq_next,
2967 .stop = ipmr_vif_seq_stop,
2968 .show = ipmr_vif_seq_show,
2971 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2973 struct net *net = seq_file_net(seq);
2974 struct mr_table *mrt;
2976 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2978 return ERR_PTR(-ENOENT);
2980 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
2983 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2987 if (v == SEQ_START_TOKEN) {
2989 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2991 const struct mfc_cache *mfc = v;
2992 const struct mr_mfc_iter *it = seq->private;
2993 const struct mr_table *mrt = it->mrt;
2995 seq_printf(seq, "%08X %08X %-3hd",
2996 (__force u32) mfc->mfc_mcastgrp,
2997 (__force u32) mfc->mfc_origin,
2998 mfc->_c.mfc_parent);
3000 if (it->cache != &mrt->mfc_unres_queue) {
3001 seq_printf(seq, " %8lu %8lu %8lu",
3002 mfc->_c.mfc_un.res.pkt,
3003 mfc->_c.mfc_un.res.bytes,
3004 mfc->_c.mfc_un.res.wrong_if);
3005 for (n = mfc->_c.mfc_un.res.minvif;
3006 n < mfc->_c.mfc_un.res.maxvif; n++) {
3007 if (VIF_EXISTS(mrt, n) &&
3008 mfc->_c.mfc_un.res.ttls[n] < 255)
3011 n, mfc->_c.mfc_un.res.ttls[n]);
3014 /* unresolved mfc_caches don't contain
3015 * pkt, bytes and wrong_if values
3017 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
3019 seq_putc(seq, '\n');
3024 static const struct seq_operations ipmr_mfc_seq_ops = {
3025 .start = ipmr_mfc_seq_start,
3026 .next = mr_mfc_seq_next,
3027 .stop = mr_mfc_seq_stop,
3028 .show = ipmr_mfc_seq_show,
3032 #ifdef CONFIG_IP_PIMSM_V2
3033 static const struct net_protocol pim_protocol = {
3038 static unsigned int ipmr_seq_read(struct net *net)
3042 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
3045 static int ipmr_dump(struct net *net, struct notifier_block *nb,
3046 struct netlink_ext_ack *extack)
3048 return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
3049 ipmr_mr_table_iter, extack);
3052 static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3053 .family = RTNL_FAMILY_IPMR,
3054 .fib_seq_read = ipmr_seq_read,
3055 .fib_dump = ipmr_dump,
3056 .owner = THIS_MODULE,
3059 static int __net_init ipmr_notifier_init(struct net *net)
3061 struct fib_notifier_ops *ops;
3063 net->ipv4.ipmr_seq = 0;
3065 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3067 return PTR_ERR(ops);
3068 net->ipv4.ipmr_notifier_ops = ops;
3073 static void __net_exit ipmr_notifier_exit(struct net *net)
3075 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3076 net->ipv4.ipmr_notifier_ops = NULL;
3079 /* Setup for IP multicast routing */
3080 static int __net_init ipmr_net_init(struct net *net)
3084 err = ipmr_notifier_init(net);
3086 goto ipmr_notifier_fail;
3088 err = ipmr_rules_init(net);
3090 goto ipmr_rules_fail;
3092 #ifdef CONFIG_PROC_FS
3094 if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
3095 sizeof(struct mr_vif_iter)))
3097 if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
3098 sizeof(struct mr_mfc_iter)))
3099 goto proc_cache_fail;
3103 #ifdef CONFIG_PROC_FS
3105 remove_proc_entry("ip_mr_vif", net->proc_net);
3108 ipmr_rules_exit(net);
3112 ipmr_notifier_exit(net);
3117 static void __net_exit ipmr_net_exit(struct net *net)
3119 #ifdef CONFIG_PROC_FS
3120 remove_proc_entry("ip_mr_cache", net->proc_net);
3121 remove_proc_entry("ip_mr_vif", net->proc_net);
3123 ipmr_notifier_exit(net);
3126 static void __net_exit ipmr_net_exit_batch(struct list_head *net_list)
3131 list_for_each_entry(net, net_list, exit_list)
3132 ipmr_rules_exit(net);
3136 static struct pernet_operations ipmr_net_ops = {
3137 .init = ipmr_net_init,
3138 .exit = ipmr_net_exit,
3139 .exit_batch = ipmr_net_exit_batch,
3142 int __init ip_mr_init(void)
3146 mrt_cachep = KMEM_CACHE(mfc_cache, SLAB_HWCACHE_ALIGN | SLAB_PANIC);
3148 err = register_pernet_subsys(&ipmr_net_ops);
3150 goto reg_pernet_fail;
3152 err = register_netdevice_notifier(&ip_mr_notifier);
3154 goto reg_notif_fail;
3155 #ifdef CONFIG_IP_PIMSM_V2
3156 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
3157 pr_err("%s: can't add PIM protocol\n", __func__);
3159 goto add_proto_fail;
3162 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
3163 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
3164 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
3165 ipmr_rtm_route, NULL, 0);
3166 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
3167 ipmr_rtm_route, NULL, 0);
3169 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
3170 NULL, ipmr_rtm_dumplink, 0);
3173 #ifdef CONFIG_IP_PIMSM_V2
3175 unregister_netdevice_notifier(&ip_mr_notifier);
3178 unregister_pernet_subsys(&ipmr_net_ops);
3180 kmem_cache_destroy(mrt_cachep);