2 * IP multicast routing support for mrouted 3.6/3.8
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>
70 #include <net/switchdev.h>
72 #include <linux/nospec.h>
75 struct fib_rule common;
82 /* Big lock, protecting vif table, mrt cache and mroute socket state.
83 * Note that the changes are semaphored via rtnl_lock.
86 static DEFINE_RWLOCK(mrt_lock);
88 /* Multicast router control variables */
90 /* Special spinlock for queue of unresolved entries */
91 static DEFINE_SPINLOCK(mfc_unres_lock);
93 /* We return to original Alan's scheme. Hash table of resolved
94 * entries is changed only in process context and protected
95 * with weak lock mrt_lock. Queue of unresolved entries is protected
96 * with strong spinlock mfc_unres_lock.
98 * In this case data path is free of exclusive locks at all.
101 static struct kmem_cache *mrt_cachep __ro_after_init;
103 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
104 static void ipmr_free_table(struct mr_table *mrt);
106 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
107 struct net_device *dev, struct sk_buff *skb,
108 struct mfc_cache *cache, int local);
109 static int ipmr_cache_report(struct mr_table *mrt,
110 struct sk_buff *pkt, vifi_t vifi, int assert);
111 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
113 static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
114 static void mroute_clean_tables(struct mr_table *mrt, bool all);
115 static void ipmr_expire_process(struct timer_list *t);
117 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
118 #define ipmr_for_each_table(mrt, net) \
119 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
121 static struct mr_table *ipmr_mr_table_iter(struct net *net,
122 struct mr_table *mrt)
124 struct mr_table *ret;
127 ret = list_entry_rcu(net->ipv4.mr_tables.next,
128 struct mr_table, list);
130 ret = list_entry_rcu(mrt->list.next,
131 struct mr_table, list);
133 if (&ret->list == &net->ipv4.mr_tables)
138 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
140 struct mr_table *mrt;
142 ipmr_for_each_table(mrt, net) {
149 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
150 struct mr_table **mrt)
153 struct ipmr_result res;
154 struct fib_lookup_arg arg = {
156 .flags = FIB_LOOKUP_NOREF,
159 /* update flow if oif or iif point to device enslaved to l3mdev */
160 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
162 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
163 flowi4_to_flowi(flp4), 0, &arg);
170 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
171 int flags, struct fib_lookup_arg *arg)
173 struct ipmr_result *res = arg->result;
174 struct mr_table *mrt;
176 switch (rule->action) {
179 case FR_ACT_UNREACHABLE:
181 case FR_ACT_PROHIBIT:
183 case FR_ACT_BLACKHOLE:
188 arg->table = fib_rule_get_table(rule, arg);
190 mrt = ipmr_get_table(rule->fr_net, arg->table);
197 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
202 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
206 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
207 struct fib_rule_hdr *frh, struct nlattr **tb,
208 struct netlink_ext_ack *extack)
213 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
219 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
220 struct fib_rule_hdr *frh)
228 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
229 .family = RTNL_FAMILY_IPMR,
230 .rule_size = sizeof(struct ipmr_rule),
231 .addr_size = sizeof(u32),
232 .action = ipmr_rule_action,
233 .match = ipmr_rule_match,
234 .configure = ipmr_rule_configure,
235 .compare = ipmr_rule_compare,
236 .fill = ipmr_rule_fill,
237 .nlgroup = RTNLGRP_IPV4_RULE,
238 .policy = ipmr_rule_policy,
239 .owner = THIS_MODULE,
242 static int __net_init ipmr_rules_init(struct net *net)
244 struct fib_rules_ops *ops;
245 struct mr_table *mrt;
248 ops = fib_rules_register(&ipmr_rules_ops_template, net);
252 INIT_LIST_HEAD(&net->ipv4.mr_tables);
254 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
260 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
264 net->ipv4.mr_rules_ops = ops;
268 ipmr_free_table(mrt);
270 fib_rules_unregister(ops);
274 static void __net_exit ipmr_rules_exit(struct net *net)
276 struct mr_table *mrt, *next;
279 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
280 list_del(&mrt->list);
281 ipmr_free_table(mrt);
283 fib_rules_unregister(net->ipv4.mr_rules_ops);
287 static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
289 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR);
292 static unsigned int ipmr_rules_seq_read(struct net *net)
294 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
297 bool ipmr_rule_default(const struct fib_rule *rule)
299 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
301 EXPORT_SYMBOL(ipmr_rule_default);
303 #define ipmr_for_each_table(mrt, net) \
304 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
306 static struct mr_table *ipmr_mr_table_iter(struct net *net,
307 struct mr_table *mrt)
310 return net->ipv4.mrt;
314 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
316 return net->ipv4.mrt;
319 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
320 struct mr_table **mrt)
322 *mrt = net->ipv4.mrt;
326 static int __net_init ipmr_rules_init(struct net *net)
328 struct mr_table *mrt;
330 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
337 static void __net_exit ipmr_rules_exit(struct net *net)
340 ipmr_free_table(net->ipv4.mrt);
341 net->ipv4.mrt = NULL;
345 static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
350 static unsigned int ipmr_rules_seq_read(struct net *net)
355 bool ipmr_rule_default(const struct fib_rule *rule)
359 EXPORT_SYMBOL(ipmr_rule_default);
362 static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
365 const struct mfc_cache_cmp_arg *cmparg = arg->key;
366 struct mfc_cache *c = (struct mfc_cache *)ptr;
368 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
369 cmparg->mfc_origin != c->mfc_origin;
372 static const struct rhashtable_params ipmr_rht_params = {
373 .head_offset = offsetof(struct mr_mfc, mnode),
374 .key_offset = offsetof(struct mfc_cache, cmparg),
375 .key_len = sizeof(struct mfc_cache_cmp_arg),
378 .obj_cmpfn = ipmr_hash_cmp,
379 .automatic_shrinking = true,
382 static void ipmr_new_table_set(struct mr_table *mrt,
385 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
386 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
390 static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
391 .mfc_mcastgrp = htonl(INADDR_ANY),
392 .mfc_origin = htonl(INADDR_ANY),
395 static struct mr_table_ops ipmr_mr_table_ops = {
396 .rht_params = &ipmr_rht_params,
397 .cmparg_any = &ipmr_mr_table_ops_cmparg_any,
400 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
402 struct mr_table *mrt;
404 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
405 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
406 return ERR_PTR(-EINVAL);
408 mrt = ipmr_get_table(net, id);
412 return mr_table_alloc(net, id, &ipmr_mr_table_ops,
413 ipmr_expire_process, ipmr_new_table_set);
416 static void ipmr_free_table(struct mr_table *mrt)
418 del_timer_sync(&mrt->ipmr_expire_timer);
419 mroute_clean_tables(mrt, true);
420 rhltable_destroy(&mrt->mfc_hash);
424 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
426 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
428 struct net *net = dev_net(dev);
432 dev = __dev_get_by_name(net, "tunl0");
434 const struct net_device_ops *ops = dev->netdev_ops;
436 struct ip_tunnel_parm p;
438 memset(&p, 0, sizeof(p));
439 p.iph.daddr = v->vifc_rmt_addr.s_addr;
440 p.iph.saddr = v->vifc_lcl_addr.s_addr;
443 p.iph.protocol = IPPROTO_IPIP;
444 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
445 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
447 if (ops->ndo_do_ioctl) {
448 mm_segment_t oldfs = get_fs();
451 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
457 /* Initialize ipmr pimreg/tunnel in_device */
458 static bool ipmr_init_vif_indev(const struct net_device *dev)
460 struct in_device *in_dev;
464 in_dev = __in_dev_get_rtnl(dev);
467 ipv4_devconf_setall(in_dev);
468 neigh_parms_data_state_setall(in_dev->arp_parms);
469 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
474 static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
476 struct net_device *dev;
478 dev = __dev_get_by_name(net, "tunl0");
481 const struct net_device_ops *ops = dev->netdev_ops;
484 struct ip_tunnel_parm p;
486 memset(&p, 0, sizeof(p));
487 p.iph.daddr = v->vifc_rmt_addr.s_addr;
488 p.iph.saddr = v->vifc_lcl_addr.s_addr;
491 p.iph.protocol = IPPROTO_IPIP;
492 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
493 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
495 if (ops->ndo_do_ioctl) {
496 mm_segment_t oldfs = get_fs();
499 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
507 (dev = __dev_get_by_name(net, p.name)) != NULL) {
508 dev->flags |= IFF_MULTICAST;
509 if (!ipmr_init_vif_indev(dev))
511 if (dev_open(dev, NULL))
519 unregister_netdevice(dev);
523 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
524 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
526 struct net *net = dev_net(dev);
527 struct mr_table *mrt;
528 struct flowi4 fl4 = {
529 .flowi4_oif = dev->ifindex,
530 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
531 .flowi4_mark = skb->mark,
535 err = ipmr_fib_lookup(net, &fl4, &mrt);
541 read_lock(&mrt_lock);
542 dev->stats.tx_bytes += skb->len;
543 dev->stats.tx_packets++;
544 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
545 read_unlock(&mrt_lock);
550 static int reg_vif_get_iflink(const struct net_device *dev)
555 static const struct net_device_ops reg_vif_netdev_ops = {
556 .ndo_start_xmit = reg_vif_xmit,
557 .ndo_get_iflink = reg_vif_get_iflink,
560 static void reg_vif_setup(struct net_device *dev)
562 dev->type = ARPHRD_PIMREG;
563 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
564 dev->flags = IFF_NOARP;
565 dev->netdev_ops = ®_vif_netdev_ops;
566 dev->needs_free_netdev = true;
567 dev->features |= NETIF_F_NETNS_LOCAL;
570 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
572 struct net_device *dev;
575 if (mrt->id == RT_TABLE_DEFAULT)
576 sprintf(name, "pimreg");
578 sprintf(name, "pimreg%u", mrt->id);
580 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
585 dev_net_set(dev, net);
587 if (register_netdevice(dev)) {
592 if (!ipmr_init_vif_indev(dev))
594 if (dev_open(dev, NULL))
602 unregister_netdevice(dev);
606 /* called with rcu_read_lock() */
607 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
610 struct net_device *reg_dev = NULL;
613 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
615 * a. packet is really sent to a multicast group
616 * b. packet is not a NULL-REGISTER
617 * c. packet is not truncated
619 if (!ipv4_is_multicast(encap->daddr) ||
620 encap->tot_len == 0 ||
621 ntohs(encap->tot_len) + pimlen > skb->len)
624 read_lock(&mrt_lock);
625 if (mrt->mroute_reg_vif_num >= 0)
626 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
627 read_unlock(&mrt_lock);
632 skb->mac_header = skb->network_header;
633 skb_pull(skb, (u8 *)encap - skb->data);
634 skb_reset_network_header(skb);
635 skb->protocol = htons(ETH_P_IP);
636 skb->ip_summed = CHECKSUM_NONE;
638 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
642 return NET_RX_SUCCESS;
645 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
651 static int call_ipmr_vif_entry_notifiers(struct net *net,
652 enum fib_event_type event_type,
653 struct vif_device *vif,
654 vifi_t vif_index, u32 tb_id)
656 return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
657 vif, vif_index, tb_id,
658 &net->ipv4.ipmr_seq);
661 static int call_ipmr_mfc_entry_notifiers(struct net *net,
662 enum fib_event_type event_type,
663 struct mfc_cache *mfc, u32 tb_id)
665 return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
666 &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
670 * vif_delete - Delete a VIF entry
671 * @notify: Set to 1, if the caller is a notifier_call
673 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
674 struct list_head *head)
676 struct net *net = read_pnet(&mrt->net);
677 struct vif_device *v;
678 struct net_device *dev;
679 struct in_device *in_dev;
681 if (vifi < 0 || vifi >= mrt->maxvif)
682 return -EADDRNOTAVAIL;
684 v = &mrt->vif_table[vifi];
686 if (VIF_EXISTS(mrt, vifi))
687 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
690 write_lock_bh(&mrt_lock);
695 write_unlock_bh(&mrt_lock);
696 return -EADDRNOTAVAIL;
699 if (vifi == mrt->mroute_reg_vif_num)
700 mrt->mroute_reg_vif_num = -1;
702 if (vifi + 1 == mrt->maxvif) {
705 for (tmp = vifi - 1; tmp >= 0; tmp--) {
706 if (VIF_EXISTS(mrt, tmp))
712 write_unlock_bh(&mrt_lock);
714 dev_set_allmulti(dev, -1);
716 in_dev = __in_dev_get_rtnl(dev);
718 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
719 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
720 NETCONFA_MC_FORWARDING,
721 dev->ifindex, &in_dev->cnf);
722 ip_rt_multicast_event(in_dev);
725 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
726 unregister_netdevice_queue(dev, head);
732 static void ipmr_cache_free_rcu(struct rcu_head *head)
734 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
736 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
739 static void ipmr_cache_free(struct mfc_cache *c)
741 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
744 /* Destroy an unresolved cache entry, killing queued skbs
745 * and reporting error to netlink readers.
747 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
749 struct net *net = read_pnet(&mrt->net);
753 atomic_dec(&mrt->cache_resolve_queue_len);
755 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
756 if (ip_hdr(skb)->version == 0) {
757 struct nlmsghdr *nlh = skb_pull(skb,
758 sizeof(struct iphdr));
759 nlh->nlmsg_type = NLMSG_ERROR;
760 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
761 skb_trim(skb, nlh->nlmsg_len);
763 e->error = -ETIMEDOUT;
764 memset(&e->msg, 0, sizeof(e->msg));
766 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
775 /* Timer process for the unresolved queue. */
776 static void ipmr_expire_process(struct timer_list *t)
778 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
779 struct mr_mfc *c, *next;
780 unsigned long expires;
783 if (!spin_trylock(&mfc_unres_lock)) {
784 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
788 if (list_empty(&mrt->mfc_unres_queue))
794 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
795 if (time_after(c->mfc_un.unres.expires, now)) {
796 unsigned long interval = c->mfc_un.unres.expires - now;
797 if (interval < expires)
803 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
804 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
807 if (!list_empty(&mrt->mfc_unres_queue))
808 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
811 spin_unlock(&mfc_unres_lock);
814 /* Fill oifs list. It is called under write locked mrt_lock. */
815 static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
820 cache->mfc_un.res.minvif = MAXVIFS;
821 cache->mfc_un.res.maxvif = 0;
822 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
824 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
825 if (VIF_EXISTS(mrt, vifi) &&
826 ttls[vifi] && ttls[vifi] < 255) {
827 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
828 if (cache->mfc_un.res.minvif > vifi)
829 cache->mfc_un.res.minvif = vifi;
830 if (cache->mfc_un.res.maxvif <= vifi)
831 cache->mfc_un.res.maxvif = vifi + 1;
834 cache->mfc_un.res.lastuse = jiffies;
837 static int vif_add(struct net *net, struct mr_table *mrt,
838 struct vifctl *vifc, int mrtsock)
840 int vifi = vifc->vifc_vifi;
841 struct switchdev_attr attr = {
842 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
844 struct vif_device *v = &mrt->vif_table[vifi];
845 struct net_device *dev;
846 struct in_device *in_dev;
850 if (VIF_EXISTS(mrt, vifi))
853 switch (vifc->vifc_flags) {
855 if (!ipmr_pimsm_enabled())
857 /* Special Purpose VIF in PIM
858 * All the packets will be sent to the daemon
860 if (mrt->mroute_reg_vif_num >= 0)
862 dev = ipmr_reg_vif(net, mrt);
865 err = dev_set_allmulti(dev, 1);
867 unregister_netdevice(dev);
873 dev = ipmr_new_tunnel(net, vifc);
876 err = dev_set_allmulti(dev, 1);
878 ipmr_del_tunnel(dev, vifc);
883 case VIFF_USE_IFINDEX:
885 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
886 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
887 if (dev && !__in_dev_get_rtnl(dev)) {
889 return -EADDRNOTAVAIL;
892 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
895 return -EADDRNOTAVAIL;
896 err = dev_set_allmulti(dev, 1);
906 in_dev = __in_dev_get_rtnl(dev);
909 return -EADDRNOTAVAIL;
911 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
912 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
913 dev->ifindex, &in_dev->cnf);
914 ip_rt_multicast_event(in_dev);
916 /* Fill in the VIF structures */
917 vif_device_init(v, dev, vifc->vifc_rate_limit,
918 vifc->vifc_threshold,
919 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
920 (VIFF_TUNNEL | VIFF_REGISTER));
923 if (!switchdev_port_attr_get(dev, &attr)) {
924 memcpy(v->dev_parent_id.id, attr.u.ppid.id, attr.u.ppid.id_len);
925 v->dev_parent_id.id_len = attr.u.ppid.id_len;
927 v->dev_parent_id.id_len = 0;
930 v->local = vifc->vifc_lcl_addr.s_addr;
931 v->remote = vifc->vifc_rmt_addr.s_addr;
933 /* And finish update writing critical data */
934 write_lock_bh(&mrt_lock);
936 if (v->flags & VIFF_REGISTER)
937 mrt->mroute_reg_vif_num = vifi;
938 if (vifi+1 > mrt->maxvif)
939 mrt->maxvif = vifi+1;
940 write_unlock_bh(&mrt_lock);
941 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
945 /* called with rcu_read_lock() */
946 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
950 struct mfc_cache_cmp_arg arg = {
951 .mfc_mcastgrp = mcastgrp,
955 return mr_mfc_find(mrt, &arg);
958 /* Look for a (*,G) entry */
959 static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
960 __be32 mcastgrp, int vifi)
962 struct mfc_cache_cmp_arg arg = {
963 .mfc_mcastgrp = mcastgrp,
964 .mfc_origin = htonl(INADDR_ANY)
967 if (mcastgrp == htonl(INADDR_ANY))
968 return mr_mfc_find_any_parent(mrt, vifi);
969 return mr_mfc_find_any(mrt, vifi, &arg);
972 /* Look for a (S,G,iif) entry if parent != -1 */
973 static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
974 __be32 origin, __be32 mcastgrp,
977 struct mfc_cache_cmp_arg arg = {
978 .mfc_mcastgrp = mcastgrp,
979 .mfc_origin = origin,
982 return mr_mfc_find_parent(mrt, &arg, parent);
985 /* Allocate a multicast cache entry */
986 static struct mfc_cache *ipmr_cache_alloc(void)
988 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
991 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
992 c->_c.mfc_un.res.minvif = MAXVIFS;
993 c->_c.free = ipmr_cache_free_rcu;
994 refcount_set(&c->_c.mfc_un.res.refcount, 1);
999 static struct mfc_cache *ipmr_cache_alloc_unres(void)
1001 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
1004 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
1005 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
1010 /* A cache entry has gone into a resolved state from queued */
1011 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1012 struct mfc_cache *uc, struct mfc_cache *c)
1014 struct sk_buff *skb;
1017 /* Play the pending entries through our router */
1018 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
1019 if (ip_hdr(skb)->version == 0) {
1020 struct nlmsghdr *nlh = skb_pull(skb,
1021 sizeof(struct iphdr));
1023 if (mr_fill_mroute(mrt, skb, &c->_c,
1024 nlmsg_data(nlh)) > 0) {
1025 nlh->nlmsg_len = skb_tail_pointer(skb) -
1028 nlh->nlmsg_type = NLMSG_ERROR;
1029 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
1030 skb_trim(skb, nlh->nlmsg_len);
1031 e = nlmsg_data(nlh);
1032 e->error = -EMSGSIZE;
1033 memset(&e->msg, 0, sizeof(e->msg));
1036 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
1038 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
1043 /* Bounce a cache query up to mrouted and netlink.
1045 * Called under mrt_lock.
1047 static int ipmr_cache_report(struct mr_table *mrt,
1048 struct sk_buff *pkt, vifi_t vifi, int assert)
1050 const int ihl = ip_hdrlen(pkt);
1051 struct sock *mroute_sk;
1052 struct igmphdr *igmp;
1053 struct igmpmsg *msg;
1054 struct sk_buff *skb;
1057 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE)
1058 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1060 skb = alloc_skb(128, GFP_ATOMIC);
1065 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) {
1066 /* Ugly, but we have no choice with this interface.
1067 * Duplicate old header, fix ihl, length etc.
1068 * And all this only to mangle msg->im_msgtype and
1069 * to set msg->im_mbz to "mbz" :-)
1071 skb_push(skb, sizeof(struct iphdr));
1072 skb_reset_network_header(skb);
1073 skb_reset_transport_header(skb);
1074 msg = (struct igmpmsg *)skb_network_header(skb);
1075 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
1076 msg->im_msgtype = assert;
1078 if (assert == IGMPMSG_WRVIFWHOLE)
1081 msg->im_vif = mrt->mroute_reg_vif_num;
1082 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1083 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1084 sizeof(struct iphdr));
1086 /* Copy the IP header */
1087 skb_set_network_header(skb, skb->len);
1089 skb_copy_to_linear_data(skb, pkt->data, ihl);
1090 /* Flag to the kernel this is a route add */
1091 ip_hdr(skb)->protocol = 0;
1092 msg = (struct igmpmsg *)skb_network_header(skb);
1094 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1095 /* Add our header */
1096 igmp = skb_put(skb, sizeof(struct igmphdr));
1097 igmp->type = assert;
1098 msg->im_msgtype = assert;
1100 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1101 skb->transport_header = skb->network_header;
1105 mroute_sk = rcu_dereference(mrt->mroute_sk);
1112 igmpmsg_netlink_event(mrt, skb);
1114 /* Deliver to mrouted */
1115 ret = sock_queue_rcv_skb(mroute_sk, skb);
1118 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1125 /* Queue a packet for resolution. It gets locked cache entry! */
1126 static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1127 struct sk_buff *skb, struct net_device *dev)
1129 const struct iphdr *iph = ip_hdr(skb);
1130 struct mfc_cache *c;
1134 spin_lock_bh(&mfc_unres_lock);
1135 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
1136 if (c->mfc_mcastgrp == iph->daddr &&
1137 c->mfc_origin == iph->saddr) {
1144 /* Create a new entry if allowable */
1145 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1146 (c = ipmr_cache_alloc_unres()) == NULL) {
1147 spin_unlock_bh(&mfc_unres_lock);
1153 /* Fill in the new cache entry */
1154 c->_c.mfc_parent = -1;
1155 c->mfc_origin = iph->saddr;
1156 c->mfc_mcastgrp = iph->daddr;
1158 /* Reflect first query at mrouted. */
1159 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1162 /* If the report failed throw the cache entry
1165 spin_unlock_bh(&mfc_unres_lock);
1172 atomic_inc(&mrt->cache_resolve_queue_len);
1173 list_add(&c->_c.list, &mrt->mfc_unres_queue);
1174 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1176 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1177 mod_timer(&mrt->ipmr_expire_timer,
1178 c->_c.mfc_un.unres.expires);
1181 /* See if we can append the packet */
1182 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
1188 skb->skb_iif = dev->ifindex;
1190 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
1194 spin_unlock_bh(&mfc_unres_lock);
1198 /* MFC cache manipulation by user space mroute daemon */
1200 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1202 struct net *net = read_pnet(&mrt->net);
1203 struct mfc_cache *c;
1205 /* The entries are added/deleted only under RTNL */
1207 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1208 mfc->mfcc_mcastgrp.s_addr, parent);
1212 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1213 list_del_rcu(&c->_c.list);
1214 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
1215 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1216 mr_cache_put(&c->_c);
1221 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1222 struct mfcctl *mfc, int mrtsock, int parent)
1224 struct mfc_cache *uc, *c;
1229 if (mfc->mfcc_parent >= MAXVIFS)
1232 /* The entries are added/deleted only under RTNL */
1234 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1235 mfc->mfcc_mcastgrp.s_addr, parent);
1238 write_lock_bh(&mrt_lock);
1239 c->_c.mfc_parent = mfc->mfcc_parent;
1240 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1242 c->_c.mfc_flags |= MFC_STATIC;
1243 write_unlock_bh(&mrt_lock);
1244 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1246 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1250 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1251 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1254 c = ipmr_cache_alloc();
1258 c->mfc_origin = mfc->mfcc_origin.s_addr;
1259 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1260 c->_c.mfc_parent = mfc->mfcc_parent;
1261 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1263 c->_c.mfc_flags |= MFC_STATIC;
1265 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
1268 pr_err("ipmr: rhtable insert error %d\n", ret);
1272 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
1273 /* Check to see if we resolved a queued list. If so we
1274 * need to send on the frames and tidy up.
1277 spin_lock_bh(&mfc_unres_lock);
1278 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1279 uc = (struct mfc_cache *)_uc;
1280 if (uc->mfc_origin == c->mfc_origin &&
1281 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1282 list_del(&_uc->list);
1283 atomic_dec(&mrt->cache_resolve_queue_len);
1288 if (list_empty(&mrt->mfc_unres_queue))
1289 del_timer(&mrt->ipmr_expire_timer);
1290 spin_unlock_bh(&mfc_unres_lock);
1293 ipmr_cache_resolve(net, mrt, uc, c);
1294 ipmr_cache_free(uc);
1296 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
1297 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1301 /* Close the multicast socket, and clear the vif tables etc */
1302 static void mroute_clean_tables(struct mr_table *mrt, bool all)
1304 struct net *net = read_pnet(&mrt->net);
1305 struct mr_mfc *c, *tmp;
1306 struct mfc_cache *cache;
1310 /* Shut down all active vif entries */
1311 for (i = 0; i < mrt->maxvif; i++) {
1312 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1314 vif_delete(mrt, i, 0, &list);
1316 unregister_netdevice_many(&list);
1318 /* Wipe the cache */
1319 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1320 if (!all && (c->mfc_flags & MFC_STATIC))
1322 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1323 list_del_rcu(&c->list);
1324 cache = (struct mfc_cache *)c;
1325 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
1327 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1331 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1332 spin_lock_bh(&mfc_unres_lock);
1333 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1335 cache = (struct mfc_cache *)c;
1336 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1337 ipmr_destroy_unres(mrt, cache);
1339 spin_unlock_bh(&mfc_unres_lock);
1343 /* called from ip_ra_control(), before an RCU grace period,
1344 * we dont need to call synchronize_rcu() here
1346 static void mrtsock_destruct(struct sock *sk)
1348 struct net *net = sock_net(sk);
1349 struct mr_table *mrt;
1352 ipmr_for_each_table(mrt, net) {
1353 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1354 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1355 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1356 NETCONFA_MC_FORWARDING,
1357 NETCONFA_IFINDEX_ALL,
1358 net->ipv4.devconf_all);
1359 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1360 mroute_clean_tables(mrt, false);
1366 /* Socket options and virtual interface manipulation. The whole
1367 * virtual interface system is a complete heap, but unfortunately
1368 * that's how BSD mrouted happens to think. Maybe one day with a proper
1369 * MOSPF/PIM router set up we can clean this up.
1372 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1373 unsigned int optlen)
1375 struct net *net = sock_net(sk);
1376 int val, ret = 0, parent = 0;
1377 struct mr_table *mrt;
1383 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1385 if (sk->sk_type != SOCK_RAW ||
1386 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1391 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1396 if (optname != MRT_INIT) {
1397 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1398 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1406 if (optlen != sizeof(int)) {
1410 if (rtnl_dereference(mrt->mroute_sk)) {
1415 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1417 rcu_assign_pointer(mrt->mroute_sk, sk);
1418 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1419 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1420 NETCONFA_MC_FORWARDING,
1421 NETCONFA_IFINDEX_ALL,
1422 net->ipv4.devconf_all);
1426 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1429 /* We need to unlock here because mrtsock_destruct takes
1430 * care of rtnl itself and we can't change that due to
1431 * the IP_ROUTER_ALERT setsockopt which runs without it.
1434 ret = ip_ra_control(sk, 0, NULL);
1440 if (optlen != sizeof(vif)) {
1444 if (copy_from_user(&vif, optval, sizeof(vif))) {
1448 if (vif.vifc_vifi >= MAXVIFS) {
1452 if (optname == MRT_ADD_VIF) {
1453 ret = vif_add(net, mrt, &vif,
1454 sk == rtnl_dereference(mrt->mroute_sk));
1456 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1459 /* Manipulate the forwarding caches. These live
1460 * in a sort of kernel/user symbiosis.
1466 case MRT_ADD_MFC_PROXY:
1467 case MRT_DEL_MFC_PROXY:
1468 if (optlen != sizeof(mfc)) {
1472 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1477 parent = mfc.mfcc_parent;
1478 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1479 ret = ipmr_mfc_delete(mrt, &mfc, parent);
1481 ret = ipmr_mfc_add(net, mrt, &mfc,
1482 sk == rtnl_dereference(mrt->mroute_sk),
1485 /* Control PIM assert. */
1487 if (optlen != sizeof(val)) {
1491 if (get_user(val, (int __user *)optval)) {
1495 mrt->mroute_do_assert = val;
1498 if (!ipmr_pimsm_enabled()) {
1502 if (optlen != sizeof(val)) {
1506 if (get_user(val, (int __user *)optval)) {
1511 do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE);
1513 if (val != mrt->mroute_do_pim) {
1514 mrt->mroute_do_pim = val;
1515 mrt->mroute_do_assert = val;
1516 mrt->mroute_do_wrvifwhole = do_wrvifwhole;
1520 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1524 if (optlen != sizeof(uval)) {
1528 if (get_user(uval, (u32 __user *)optval)) {
1533 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1536 mrt = ipmr_new_table(net, uval);
1540 raw_sk(sk)->ipmr_table = uval;
1543 /* Spurious command, or MRT_VERSION which you cannot set. */
1553 /* Getsock opt support for the multicast routing system. */
1554 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1558 struct net *net = sock_net(sk);
1559 struct mr_table *mrt;
1561 if (sk->sk_type != SOCK_RAW ||
1562 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1565 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1574 if (!ipmr_pimsm_enabled())
1575 return -ENOPROTOOPT;
1576 val = mrt->mroute_do_pim;
1579 val = mrt->mroute_do_assert;
1582 return -ENOPROTOOPT;
1585 if (get_user(olr, optlen))
1587 olr = min_t(unsigned int, olr, sizeof(int));
1590 if (put_user(olr, optlen))
1592 if (copy_to_user(optval, &val, olr))
1597 /* The IP multicast ioctl support routines. */
1598 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1600 struct sioc_sg_req sr;
1601 struct sioc_vif_req vr;
1602 struct vif_device *vif;
1603 struct mfc_cache *c;
1604 struct net *net = sock_net(sk);
1605 struct mr_table *mrt;
1607 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1613 if (copy_from_user(&vr, arg, sizeof(vr)))
1615 if (vr.vifi >= mrt->maxvif)
1617 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
1618 read_lock(&mrt_lock);
1619 vif = &mrt->vif_table[vr.vifi];
1620 if (VIF_EXISTS(mrt, vr.vifi)) {
1621 vr.icount = vif->pkt_in;
1622 vr.ocount = vif->pkt_out;
1623 vr.ibytes = vif->bytes_in;
1624 vr.obytes = vif->bytes_out;
1625 read_unlock(&mrt_lock);
1627 if (copy_to_user(arg, &vr, sizeof(vr)))
1631 read_unlock(&mrt_lock);
1632 return -EADDRNOTAVAIL;
1634 if (copy_from_user(&sr, arg, sizeof(sr)))
1638 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1640 sr.pktcnt = c->_c.mfc_un.res.pkt;
1641 sr.bytecnt = c->_c.mfc_un.res.bytes;
1642 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1645 if (copy_to_user(arg, &sr, sizeof(sr)))
1650 return -EADDRNOTAVAIL;
1652 return -ENOIOCTLCMD;
1656 #ifdef CONFIG_COMPAT
1657 struct compat_sioc_sg_req {
1660 compat_ulong_t pktcnt;
1661 compat_ulong_t bytecnt;
1662 compat_ulong_t wrong_if;
1665 struct compat_sioc_vif_req {
1666 vifi_t vifi; /* Which iface */
1667 compat_ulong_t icount;
1668 compat_ulong_t ocount;
1669 compat_ulong_t ibytes;
1670 compat_ulong_t obytes;
1673 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1675 struct compat_sioc_sg_req sr;
1676 struct compat_sioc_vif_req vr;
1677 struct vif_device *vif;
1678 struct mfc_cache *c;
1679 struct net *net = sock_net(sk);
1680 struct mr_table *mrt;
1682 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1688 if (copy_from_user(&vr, arg, sizeof(vr)))
1690 if (vr.vifi >= mrt->maxvif)
1692 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
1693 read_lock(&mrt_lock);
1694 vif = &mrt->vif_table[vr.vifi];
1695 if (VIF_EXISTS(mrt, vr.vifi)) {
1696 vr.icount = vif->pkt_in;
1697 vr.ocount = vif->pkt_out;
1698 vr.ibytes = vif->bytes_in;
1699 vr.obytes = vif->bytes_out;
1700 read_unlock(&mrt_lock);
1702 if (copy_to_user(arg, &vr, sizeof(vr)))
1706 read_unlock(&mrt_lock);
1707 return -EADDRNOTAVAIL;
1709 if (copy_from_user(&sr, arg, sizeof(sr)))
1713 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1715 sr.pktcnt = c->_c.mfc_un.res.pkt;
1716 sr.bytecnt = c->_c.mfc_un.res.bytes;
1717 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1720 if (copy_to_user(arg, &sr, sizeof(sr)))
1725 return -EADDRNOTAVAIL;
1727 return -ENOIOCTLCMD;
1732 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1734 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1735 struct net *net = dev_net(dev);
1736 struct mr_table *mrt;
1737 struct vif_device *v;
1740 if (event != NETDEV_UNREGISTER)
1743 ipmr_for_each_table(mrt, net) {
1744 v = &mrt->vif_table[0];
1745 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1747 vif_delete(mrt, ct, 1, NULL);
1753 static struct notifier_block ip_mr_notifier = {
1754 .notifier_call = ipmr_device_event,
1757 /* Encapsulate a packet by attaching a valid IPIP header to it.
1758 * This avoids tunnel drivers and other mess and gives us the speed so
1759 * important for multicast video.
1761 static void ip_encap(struct net *net, struct sk_buff *skb,
1762 __be32 saddr, __be32 daddr)
1765 const struct iphdr *old_iph = ip_hdr(skb);
1767 skb_push(skb, sizeof(struct iphdr));
1768 skb->transport_header = skb->network_header;
1769 skb_reset_network_header(skb);
1773 iph->tos = old_iph->tos;
1774 iph->ttl = old_iph->ttl;
1778 iph->protocol = IPPROTO_IPIP;
1780 iph->tot_len = htons(skb->len);
1781 ip_select_ident(net, skb, NULL);
1784 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1788 static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1789 struct sk_buff *skb)
1791 struct ip_options *opt = &(IPCB(skb)->opt);
1793 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1794 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
1796 if (unlikely(opt->optlen))
1797 ip_forward_options(skb);
1799 return dst_output(net, sk, skb);
1802 #ifdef CONFIG_NET_SWITCHDEV
1803 static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1804 int in_vifi, int out_vifi)
1806 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1807 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1809 if (!skb->offload_l3_fwd_mark)
1811 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1813 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1814 &in_vif->dev_parent_id);
1817 static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1818 int in_vifi, int out_vifi)
1824 /* Processing handlers for ipmr_forward */
1826 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1827 int in_vifi, struct sk_buff *skb, int vifi)
1829 const struct iphdr *iph = ip_hdr(skb);
1830 struct vif_device *vif = &mrt->vif_table[vifi];
1831 struct net_device *dev;
1839 if (vif->flags & VIFF_REGISTER) {
1841 vif->bytes_out += skb->len;
1842 vif->dev->stats.tx_bytes += skb->len;
1843 vif->dev->stats.tx_packets++;
1844 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1848 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1851 if (vif->flags & VIFF_TUNNEL) {
1852 rt = ip_route_output_ports(net, &fl4, NULL,
1853 vif->remote, vif->local,
1856 RT_TOS(iph->tos), vif->link);
1859 encap = sizeof(struct iphdr);
1861 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1864 RT_TOS(iph->tos), vif->link);
1871 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1872 /* Do not fragment multicasts. Alas, IPv4 does not
1873 * allow to send ICMP, so that packets will disappear
1876 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1881 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1883 if (skb_cow(skb, encap)) {
1889 vif->bytes_out += skb->len;
1892 skb_dst_set(skb, &rt->dst);
1893 ip_decrease_ttl(ip_hdr(skb));
1895 /* FIXME: forward and output firewalls used to be called here.
1896 * What do we do with netfilter? -- RR
1898 if (vif->flags & VIFF_TUNNEL) {
1899 ip_encap(net, skb, vif->local, vif->remote);
1900 /* FIXME: extra output firewall step used to be here. --RR */
1901 vif->dev->stats.tx_packets++;
1902 vif->dev->stats.tx_bytes += skb->len;
1905 IPCB(skb)->flags |= IPSKB_FORWARDED;
1907 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1908 * not only before forwarding, but after forwarding on all output
1909 * interfaces. It is clear, if mrouter runs a multicasting
1910 * program, it should receive packets not depending to what interface
1911 * program is joined.
1912 * If we will not make it, the program will have to join on all
1913 * interfaces. On the other hand, multihoming host (or router, but
1914 * not mrouter) cannot join to more than one interface - it will
1915 * result in receiving multiple packets.
1917 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1918 net, NULL, skb, skb->dev, dev,
1919 ipmr_forward_finish);
1926 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1930 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1931 if (mrt->vif_table[ct].dev == dev)
1937 /* "local" means that we should preserve one skb (for local delivery) */
1938 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1939 struct net_device *dev, struct sk_buff *skb,
1940 struct mfc_cache *c, int local)
1942 int true_vifi = ipmr_find_vif(mrt, dev);
1946 vif = c->_c.mfc_parent;
1947 c->_c.mfc_un.res.pkt++;
1948 c->_c.mfc_un.res.bytes += skb->len;
1949 c->_c.mfc_un.res.lastuse = jiffies;
1951 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1952 struct mfc_cache *cache_proxy;
1954 /* For an (*,G) entry, we only check that the incomming
1955 * interface is part of the static tree.
1957 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
1959 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
1963 /* Wrong interface: drop packet and (maybe) send PIM assert. */
1964 if (mrt->vif_table[vif].dev != dev) {
1965 if (rt_is_output_route(skb_rtable(skb))) {
1966 /* It is our own packet, looped back.
1967 * Very complicated situation...
1969 * The best workaround until routing daemons will be
1970 * fixed is not to redistribute packet, if it was
1971 * send through wrong interface. It means, that
1972 * multicast applications WILL NOT work for
1973 * (S,G), which have default multicast route pointing
1974 * to wrong oif. In any case, it is not a good
1975 * idea to use multicasting applications on router.
1980 c->_c.mfc_un.res.wrong_if++;
1982 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1983 /* pimsm uses asserts, when switching from RPT to SPT,
1984 * so that we cannot check that packet arrived on an oif.
1985 * It is bad, but otherwise we would need to move pretty
1986 * large chunk of pimd to kernel. Ough... --ANK
1988 (mrt->mroute_do_pim ||
1989 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
1991 c->_c.mfc_un.res.last_assert +
1992 MFC_ASSERT_THRESH)) {
1993 c->_c.mfc_un.res.last_assert = jiffies;
1994 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1995 if (mrt->mroute_do_wrvifwhole)
1996 ipmr_cache_report(mrt, skb, true_vifi,
1997 IGMPMSG_WRVIFWHOLE);
2003 mrt->vif_table[vif].pkt_in++;
2004 mrt->vif_table[vif].bytes_in += skb->len;
2006 /* Forward the frame */
2007 if (c->mfc_origin == htonl(INADDR_ANY) &&
2008 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
2009 if (true_vifi >= 0 &&
2010 true_vifi != c->_c.mfc_parent &&
2012 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
2013 /* It's an (*,*) entry and the packet is not coming from
2014 * the upstream: forward the packet to the upstream
2017 psend = c->_c.mfc_parent;
2022 for (ct = c->_c.mfc_un.res.maxvif - 1;
2023 ct >= c->_c.mfc_un.res.minvif; ct--) {
2024 /* For (*,G) entry, don't forward to the incoming interface */
2025 if ((c->mfc_origin != htonl(INADDR_ANY) ||
2027 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
2029 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2032 ipmr_queue_xmit(net, mrt, true_vifi,
2041 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2044 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
2047 ipmr_queue_xmit(net, mrt, true_vifi, skb, psend);
2057 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
2059 struct rtable *rt = skb_rtable(skb);
2060 struct iphdr *iph = ip_hdr(skb);
2061 struct flowi4 fl4 = {
2062 .daddr = iph->daddr,
2063 .saddr = iph->saddr,
2064 .flowi4_tos = RT_TOS(iph->tos),
2065 .flowi4_oif = (rt_is_output_route(rt) ?
2066 skb->dev->ifindex : 0),
2067 .flowi4_iif = (rt_is_output_route(rt) ?
2070 .flowi4_mark = skb->mark,
2072 struct mr_table *mrt;
2075 err = ipmr_fib_lookup(net, &fl4, &mrt);
2077 return ERR_PTR(err);
2081 /* Multicast packets for forwarding arrive here
2082 * Called with rcu_read_lock();
2084 int ip_mr_input(struct sk_buff *skb)
2086 struct mfc_cache *cache;
2087 struct net *net = dev_net(skb->dev);
2088 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
2089 struct mr_table *mrt;
2090 struct net_device *dev;
2092 /* skb->dev passed in is the loX master dev for vrfs.
2093 * As there are no vifs associated with loopback devices,
2094 * get the proper interface that does have a vif associated with it.
2097 if (netif_is_l3_master(skb->dev)) {
2098 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2105 /* Packet is looped back after forward, it should not be
2106 * forwarded second time, but still can be delivered locally.
2108 if (IPCB(skb)->flags & IPSKB_FORWARDED)
2111 mrt = ipmr_rt_fib_lookup(net, skb);
2114 return PTR_ERR(mrt);
2117 if (IPCB(skb)->opt.router_alert) {
2118 if (ip_call_ra_chain(skb))
2120 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2121 /* IGMPv1 (and broken IGMPv2 implementations sort of
2122 * Cisco IOS <= 11.2(8)) do not put router alert
2123 * option to IGMP packets destined to routable
2124 * groups. It is very bad, because it means
2125 * that we can forward NO IGMP messages.
2127 struct sock *mroute_sk;
2129 mroute_sk = rcu_dereference(mrt->mroute_sk);
2132 raw_rcv(mroute_sk, skb);
2138 /* already under rcu_read_lock() */
2139 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
2141 int vif = ipmr_find_vif(mrt, dev);
2144 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2148 /* No usable cache entry */
2153 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2154 ip_local_deliver(skb);
2160 read_lock(&mrt_lock);
2161 vif = ipmr_find_vif(mrt, dev);
2163 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
2164 read_unlock(&mrt_lock);
2168 read_unlock(&mrt_lock);
2173 read_lock(&mrt_lock);
2174 ip_mr_forward(net, mrt, dev, skb, cache, local);
2175 read_unlock(&mrt_lock);
2178 return ip_local_deliver(skb);
2184 return ip_local_deliver(skb);
2189 #ifdef CONFIG_IP_PIMSM_V1
2190 /* Handle IGMP messages of PIMv1 */
2191 int pim_rcv_v1(struct sk_buff *skb)
2193 struct igmphdr *pim;
2194 struct net *net = dev_net(skb->dev);
2195 struct mr_table *mrt;
2197 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2200 pim = igmp_hdr(skb);
2202 mrt = ipmr_rt_fib_lookup(net, skb);
2205 if (!mrt->mroute_do_pim ||
2206 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2209 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2217 #ifdef CONFIG_IP_PIMSM_V2
2218 static int pim_rcv(struct sk_buff *skb)
2220 struct pimreghdr *pim;
2221 struct net *net = dev_net(skb->dev);
2222 struct mr_table *mrt;
2224 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2227 pim = (struct pimreghdr *)skb_transport_header(skb);
2228 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
2229 (pim->flags & PIM_NULL_REGISTER) ||
2230 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2231 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2234 mrt = ipmr_rt_fib_lookup(net, skb);
2237 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2245 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2246 __be32 saddr, __be32 daddr,
2247 struct rtmsg *rtm, u32 portid)
2249 struct mfc_cache *cache;
2250 struct mr_table *mrt;
2253 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2258 cache = ipmr_cache_find(mrt, saddr, daddr);
2259 if (!cache && skb->dev) {
2260 int vif = ipmr_find_vif(mrt, skb->dev);
2263 cache = ipmr_cache_find_any(mrt, daddr, vif);
2266 struct sk_buff *skb2;
2268 struct net_device *dev;
2272 read_lock(&mrt_lock);
2274 vif = ipmr_find_vif(mrt, dev);
2276 read_unlock(&mrt_lock);
2280 skb2 = skb_clone(skb, GFP_ATOMIC);
2282 read_unlock(&mrt_lock);
2287 NETLINK_CB(skb2).portid = portid;
2288 skb_push(skb2, sizeof(struct iphdr));
2289 skb_reset_network_header(skb2);
2291 iph->ihl = sizeof(struct iphdr) >> 2;
2295 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
2296 read_unlock(&mrt_lock);
2301 read_lock(&mrt_lock);
2302 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
2303 read_unlock(&mrt_lock);
2308 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2309 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2312 struct nlmsghdr *nlh;
2316 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2320 rtm = nlmsg_data(nlh);
2321 rtm->rtm_family = RTNL_FAMILY_IPMR;
2322 rtm->rtm_dst_len = 32;
2323 rtm->rtm_src_len = 32;
2325 rtm->rtm_table = mrt->id;
2326 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2327 goto nla_put_failure;
2328 rtm->rtm_type = RTN_MULTICAST;
2329 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2330 if (c->_c.mfc_flags & MFC_STATIC)
2331 rtm->rtm_protocol = RTPROT_STATIC;
2333 rtm->rtm_protocol = RTPROT_MROUTED;
2336 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2337 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2338 goto nla_put_failure;
2339 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
2340 /* do not break the dump if cache is unresolved */
2341 if (err < 0 && err != -ENOENT)
2342 goto nla_put_failure;
2344 nlmsg_end(skb, nlh);
2348 nlmsg_cancel(skb, nlh);
2352 static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2353 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2356 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2360 static size_t mroute_msgsize(bool unresolved, int maxvif)
2363 NLMSG_ALIGN(sizeof(struct rtmsg))
2364 + nla_total_size(4) /* RTA_TABLE */
2365 + nla_total_size(4) /* RTA_SRC */
2366 + nla_total_size(4) /* RTA_DST */
2371 + nla_total_size(4) /* RTA_IIF */
2372 + nla_total_size(0) /* RTA_MULTIPATH */
2373 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2375 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
2381 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2384 struct net *net = read_pnet(&mrt->net);
2385 struct sk_buff *skb;
2388 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2394 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2398 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2404 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2407 static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2410 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2411 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2412 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2413 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2414 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2415 /* IPMRA_CREPORT_PKT */
2416 + nla_total_size(payloadlen)
2422 static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2424 struct net *net = read_pnet(&mrt->net);
2425 struct nlmsghdr *nlh;
2426 struct rtgenmsg *rtgenm;
2427 struct igmpmsg *msg;
2428 struct sk_buff *skb;
2432 payloadlen = pkt->len - sizeof(struct igmpmsg);
2433 msg = (struct igmpmsg *)skb_network_header(pkt);
2435 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2439 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2440 sizeof(struct rtgenmsg), 0);
2443 rtgenm = nlmsg_data(nlh);
2444 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2445 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2446 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2447 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2448 msg->im_src.s_addr) ||
2449 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2450 msg->im_dst.s_addr))
2451 goto nla_put_failure;
2453 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2454 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2455 nla_data(nla), payloadlen))
2456 goto nla_put_failure;
2458 nlmsg_end(skb, nlh);
2460 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2464 nlmsg_cancel(skb, nlh);
2467 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2470 static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2471 struct netlink_ext_ack *extack)
2473 struct net *net = sock_net(in_skb->sk);
2474 struct nlattr *tb[RTA_MAX + 1];
2475 struct sk_buff *skb = NULL;
2476 struct mfc_cache *cache;
2477 struct mr_table *mrt;
2483 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2484 rtm_ipv4_policy, extack);
2488 rtm = nlmsg_data(nlh);
2490 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2491 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2492 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2494 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
2500 /* entries are added/deleted only under RTNL */
2502 cache = ipmr_cache_find(mrt, src, grp);
2509 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2515 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2516 nlh->nlmsg_seq, cache,
2521 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2531 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2533 struct fib_dump_filter filter = {};
2536 if (cb->strict_check) {
2537 err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh,
2543 if (filter.table_id) {
2544 struct mr_table *mrt;
2546 mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id);
2548 if (filter.dump_all_families)
2551 NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist");
2554 err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute,
2555 &mfc_unres_lock, &filter);
2556 return skb->len ? : err;
2559 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
2560 _ipmr_fill_mroute, &mfc_unres_lock, &filter);
2563 static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2564 [RTA_SRC] = { .type = NLA_U32 },
2565 [RTA_DST] = { .type = NLA_U32 },
2566 [RTA_IIF] = { .type = NLA_U32 },
2567 [RTA_TABLE] = { .type = NLA_U32 },
2568 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2571 static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2573 switch (rtm_protocol) {
2575 case RTPROT_MROUTED:
2581 static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2583 struct rtnexthop *rtnh = nla_data(nla);
2584 int remaining = nla_len(nla), vifi = 0;
2586 while (rtnh_ok(rtnh, remaining)) {
2587 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2588 if (++vifi == MAXVIFS)
2590 rtnh = rtnh_next(rtnh, &remaining);
2593 return remaining > 0 ? -EINVAL : vifi;
2596 /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2597 static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2598 struct mfcctl *mfcc, int *mrtsock,
2599 struct mr_table **mrtret,
2600 struct netlink_ext_ack *extack)
2602 struct net_device *dev = NULL;
2603 u32 tblid = RT_TABLE_DEFAULT;
2604 struct mr_table *mrt;
2605 struct nlattr *attr;
2609 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
2613 rtm = nlmsg_data(nlh);
2616 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2617 rtm->rtm_type != RTN_MULTICAST ||
2618 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2619 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2622 memset(mfcc, 0, sizeof(*mfcc));
2623 mfcc->mfcc_parent = -1;
2625 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2626 switch (nla_type(attr)) {
2628 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2631 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2634 dev = __dev_get_by_index(net, nla_get_u32(attr));
2641 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2650 tblid = nla_get_u32(attr);
2654 mrt = ipmr_get_table(net, tblid);
2660 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2662 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2668 /* takes care of both newroute and delroute */
2669 static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2670 struct netlink_ext_ack *extack)
2672 struct net *net = sock_net(skb->sk);
2673 int ret, mrtsock, parent;
2674 struct mr_table *tbl;
2679 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
2683 parent = ret ? mfcc.mfcc_parent : -1;
2684 if (nlh->nlmsg_type == RTM_NEWROUTE)
2685 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2687 return ipmr_mfc_delete(tbl, &mfcc, parent);
2690 static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2692 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2694 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2695 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2696 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2697 mrt->mroute_reg_vif_num) ||
2698 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2699 mrt->mroute_do_assert) ||
2700 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
2701 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
2702 mrt->mroute_do_wrvifwhole))
2708 static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2710 struct nlattr *vif_nest;
2711 struct vif_device *vif;
2713 /* if the VIF doesn't exist just continue */
2714 if (!VIF_EXISTS(mrt, vifid))
2717 vif = &mrt->vif_table[vifid];
2718 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2721 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2722 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2723 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2724 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2726 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2728 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2730 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2732 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2733 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2734 nla_nest_cancel(skb, vif_nest);
2737 nla_nest_end(skb, vif_nest);
2742 static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
2743 struct netlink_ext_ack *extack)
2745 struct ifinfomsg *ifm;
2747 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2748 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
2752 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
2753 NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump");
2757 ifm = nlmsg_data(nlh);
2758 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2759 ifm->ifi_change || ifm->ifi_index) {
2760 NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
2767 static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2769 struct net *net = sock_net(skb->sk);
2770 struct nlmsghdr *nlh = NULL;
2771 unsigned int t = 0, s_t;
2772 unsigned int e = 0, s_e;
2773 struct mr_table *mrt;
2775 if (cb->strict_check) {
2776 int err = ipmr_valid_dumplink(cb->nlh, cb->extack);
2785 ipmr_for_each_table(mrt, net) {
2786 struct nlattr *vifs, *af;
2787 struct ifinfomsg *hdr;
2792 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2793 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2794 sizeof(*hdr), NLM_F_MULTI);
2798 hdr = nlmsg_data(nlh);
2799 memset(hdr, 0, sizeof(*hdr));
2800 hdr->ifi_family = RTNL_FAMILY_IPMR;
2802 af = nla_nest_start(skb, IFLA_AF_SPEC);
2804 nlmsg_cancel(skb, nlh);
2808 if (!ipmr_fill_table(mrt, skb)) {
2809 nlmsg_cancel(skb, nlh);
2813 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2815 nla_nest_end(skb, af);
2816 nlmsg_end(skb, nlh);
2819 for (i = 0; i < mrt->maxvif; i++) {
2822 if (!ipmr_fill_vif(mrt, i, skb)) {
2823 nla_nest_end(skb, vifs);
2824 nla_nest_end(skb, af);
2825 nlmsg_end(skb, nlh);
2833 nla_nest_end(skb, vifs);
2834 nla_nest_end(skb, af);
2835 nlmsg_end(skb, nlh);
2847 #ifdef CONFIG_PROC_FS
2848 /* The /proc interfaces to multicast routing :
2849 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2852 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2853 __acquires(mrt_lock)
2855 struct mr_vif_iter *iter = seq->private;
2856 struct net *net = seq_file_net(seq);
2857 struct mr_table *mrt;
2859 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2861 return ERR_PTR(-ENOENT);
2865 read_lock(&mrt_lock);
2866 return mr_vif_seq_start(seq, pos);
2869 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2870 __releases(mrt_lock)
2872 read_unlock(&mrt_lock);
2875 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2877 struct mr_vif_iter *iter = seq->private;
2878 struct mr_table *mrt = iter->mrt;
2880 if (v == SEQ_START_TOKEN) {
2882 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2884 const struct vif_device *vif = v;
2885 const char *name = vif->dev ?
2886 vif->dev->name : "none";
2889 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2890 vif - mrt->vif_table,
2891 name, vif->bytes_in, vif->pkt_in,
2892 vif->bytes_out, vif->pkt_out,
2893 vif->flags, vif->local, vif->remote);
2898 static const struct seq_operations ipmr_vif_seq_ops = {
2899 .start = ipmr_vif_seq_start,
2900 .next = mr_vif_seq_next,
2901 .stop = ipmr_vif_seq_stop,
2902 .show = ipmr_vif_seq_show,
2905 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2907 struct net *net = seq_file_net(seq);
2908 struct mr_table *mrt;
2910 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2912 return ERR_PTR(-ENOENT);
2914 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
2917 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2921 if (v == SEQ_START_TOKEN) {
2923 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2925 const struct mfc_cache *mfc = v;
2926 const struct mr_mfc_iter *it = seq->private;
2927 const struct mr_table *mrt = it->mrt;
2929 seq_printf(seq, "%08X %08X %-3hd",
2930 (__force u32) mfc->mfc_mcastgrp,
2931 (__force u32) mfc->mfc_origin,
2932 mfc->_c.mfc_parent);
2934 if (it->cache != &mrt->mfc_unres_queue) {
2935 seq_printf(seq, " %8lu %8lu %8lu",
2936 mfc->_c.mfc_un.res.pkt,
2937 mfc->_c.mfc_un.res.bytes,
2938 mfc->_c.mfc_un.res.wrong_if);
2939 for (n = mfc->_c.mfc_un.res.minvif;
2940 n < mfc->_c.mfc_un.res.maxvif; n++) {
2941 if (VIF_EXISTS(mrt, n) &&
2942 mfc->_c.mfc_un.res.ttls[n] < 255)
2945 n, mfc->_c.mfc_un.res.ttls[n]);
2948 /* unresolved mfc_caches don't contain
2949 * pkt, bytes and wrong_if values
2951 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2953 seq_putc(seq, '\n');
2958 static const struct seq_operations ipmr_mfc_seq_ops = {
2959 .start = ipmr_mfc_seq_start,
2960 .next = mr_mfc_seq_next,
2961 .stop = mr_mfc_seq_stop,
2962 .show = ipmr_mfc_seq_show,
2966 #ifdef CONFIG_IP_PIMSM_V2
2967 static const struct net_protocol pim_protocol = {
2973 static unsigned int ipmr_seq_read(struct net *net)
2977 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
2980 static int ipmr_dump(struct net *net, struct notifier_block *nb)
2982 return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
2983 ipmr_mr_table_iter, &mrt_lock);
2986 static const struct fib_notifier_ops ipmr_notifier_ops_template = {
2987 .family = RTNL_FAMILY_IPMR,
2988 .fib_seq_read = ipmr_seq_read,
2989 .fib_dump = ipmr_dump,
2990 .owner = THIS_MODULE,
2993 static int __net_init ipmr_notifier_init(struct net *net)
2995 struct fib_notifier_ops *ops;
2997 net->ipv4.ipmr_seq = 0;
2999 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3001 return PTR_ERR(ops);
3002 net->ipv4.ipmr_notifier_ops = ops;
3007 static void __net_exit ipmr_notifier_exit(struct net *net)
3009 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3010 net->ipv4.ipmr_notifier_ops = NULL;
3013 /* Setup for IP multicast routing */
3014 static int __net_init ipmr_net_init(struct net *net)
3018 err = ipmr_notifier_init(net);
3020 goto ipmr_notifier_fail;
3022 err = ipmr_rules_init(net);
3024 goto ipmr_rules_fail;
3026 #ifdef CONFIG_PROC_FS
3028 if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
3029 sizeof(struct mr_vif_iter)))
3031 if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
3032 sizeof(struct mr_mfc_iter)))
3033 goto proc_cache_fail;
3037 #ifdef CONFIG_PROC_FS
3039 remove_proc_entry("ip_mr_vif", net->proc_net);
3041 ipmr_rules_exit(net);
3044 ipmr_notifier_exit(net);
3049 static void __net_exit ipmr_net_exit(struct net *net)
3051 #ifdef CONFIG_PROC_FS
3052 remove_proc_entry("ip_mr_cache", net->proc_net);
3053 remove_proc_entry("ip_mr_vif", net->proc_net);
3055 ipmr_notifier_exit(net);
3056 ipmr_rules_exit(net);
3059 static struct pernet_operations ipmr_net_ops = {
3060 .init = ipmr_net_init,
3061 .exit = ipmr_net_exit,
3064 int __init ip_mr_init(void)
3068 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3069 sizeof(struct mfc_cache),
3070 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
3073 err = register_pernet_subsys(&ipmr_net_ops);
3075 goto reg_pernet_fail;
3077 err = register_netdevice_notifier(&ip_mr_notifier);
3079 goto reg_notif_fail;
3080 #ifdef CONFIG_IP_PIMSM_V2
3081 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
3082 pr_err("%s: can't add PIM protocol\n", __func__);
3084 goto add_proto_fail;
3087 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
3088 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
3089 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
3090 ipmr_rtm_route, NULL, 0);
3091 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
3092 ipmr_rtm_route, NULL, 0);
3094 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
3095 NULL, ipmr_rtm_dumplink, 0);
3098 #ifdef CONFIG_IP_PIMSM_V2
3100 unregister_netdevice_notifier(&ip_mr_notifier);
3103 unregister_pernet_subsys(&ipmr_net_ops);
3105 kmem_cache_destroy(mrt_cachep);