2 * IP multicast routing support for mrouted 3.6/3.8
4 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
5 * Linux Consultancy and Custom Driver Development
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
25 * Relax this requirement to work with older peers.
29 #include <linux/uaccess.h>
30 #include <linux/types.h>
31 #include <linux/cache.h>
32 #include <linux/capability.h>
33 #include <linux/errno.h>
35 #include <linux/kernel.h>
36 #include <linux/fcntl.h>
37 #include <linux/stat.h>
38 #include <linux/socket.h>
40 #include <linux/inet.h>
41 #include <linux/netdevice.h>
42 #include <linux/inetdevice.h>
43 #include <linux/igmp.h>
44 #include <linux/proc_fs.h>
45 #include <linux/seq_file.h>
46 #include <linux/mroute.h>
47 #include <linux/init.h>
48 #include <linux/if_ether.h>
49 #include <linux/slab.h>
50 #include <net/net_namespace.h>
52 #include <net/protocol.h>
53 #include <linux/skbuff.h>
54 #include <net/route.h>
58 #include <linux/notifier.h>
59 #include <linux/if_arp.h>
60 #include <linux/netfilter_ipv4.h>
61 #include <linux/compat.h>
62 #include <linux/export.h>
63 #include <linux/rhashtable.h>
64 #include <net/ip_tunnels.h>
65 #include <net/checksum.h>
66 #include <net/netlink.h>
67 #include <net/fib_rules.h>
68 #include <linux/netconf.h>
69 #include <net/nexthop.h>
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))
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))
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_mr_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,
1828 struct mfc_cache *c, int vifi)
1830 const struct iphdr *iph = ip_hdr(skb);
1831 struct vif_device *vif = &mrt->vif_table[vifi];
1832 struct net_device *dev;
1840 if (vif->flags & VIFF_REGISTER) {
1842 vif->bytes_out += skb->len;
1843 vif->dev->stats.tx_bytes += skb->len;
1844 vif->dev->stats.tx_packets++;
1845 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1849 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1852 if (vif->flags & VIFF_TUNNEL) {
1853 rt = ip_route_output_ports(net, &fl4, NULL,
1854 vif->remote, vif->local,
1857 RT_TOS(iph->tos), vif->link);
1860 encap = sizeof(struct iphdr);
1862 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1865 RT_TOS(iph->tos), vif->link);
1872 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1873 /* Do not fragment multicasts. Alas, IPv4 does not
1874 * allow to send ICMP, so that packets will disappear
1877 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1882 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1884 if (skb_cow(skb, encap)) {
1890 vif->bytes_out += skb->len;
1893 skb_dst_set(skb, &rt->dst);
1894 ip_decrease_ttl(ip_hdr(skb));
1896 /* FIXME: forward and output firewalls used to be called here.
1897 * What do we do with netfilter? -- RR
1899 if (vif->flags & VIFF_TUNNEL) {
1900 ip_encap(net, skb, vif->local, vif->remote);
1901 /* FIXME: extra output firewall step used to be here. --RR */
1902 vif->dev->stats.tx_packets++;
1903 vif->dev->stats.tx_bytes += skb->len;
1906 IPCB(skb)->flags |= IPSKB_FORWARDED;
1908 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1909 * not only before forwarding, but after forwarding on all output
1910 * interfaces. It is clear, if mrouter runs a multicasting
1911 * program, it should receive packets not depending to what interface
1912 * program is joined.
1913 * If we will not make it, the program will have to join on all
1914 * interfaces. On the other hand, multihoming host (or router, but
1915 * not mrouter) cannot join to more than one interface - it will
1916 * result in receiving multiple packets.
1918 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1919 net, NULL, skb, skb->dev, dev,
1920 ipmr_forward_finish);
1927 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1931 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1932 if (mrt->vif_table[ct].dev == dev)
1938 /* "local" means that we should preserve one skb (for local delivery) */
1939 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1940 struct net_device *dev, struct sk_buff *skb,
1941 struct mfc_cache *c, int local)
1943 int true_vifi = ipmr_find_vif(mrt, dev);
1947 vif = c->_c.mfc_parent;
1948 c->_c.mfc_un.res.pkt++;
1949 c->_c.mfc_un.res.bytes += skb->len;
1950 c->_c.mfc_un.res.lastuse = jiffies;
1952 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1953 struct mfc_cache *cache_proxy;
1955 /* For an (*,G) entry, we only check that the incomming
1956 * interface is part of the static tree.
1958 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
1960 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
1964 /* Wrong interface: drop packet and (maybe) send PIM assert. */
1965 if (mrt->vif_table[vif].dev != dev) {
1966 if (rt_is_output_route(skb_rtable(skb))) {
1967 /* It is our own packet, looped back.
1968 * Very complicated situation...
1970 * The best workaround until routing daemons will be
1971 * fixed is not to redistribute packet, if it was
1972 * send through wrong interface. It means, that
1973 * multicast applications WILL NOT work for
1974 * (S,G), which have default multicast route pointing
1975 * to wrong oif. In any case, it is not a good
1976 * idea to use multicasting applications on router.
1981 c->_c.mfc_un.res.wrong_if++;
1983 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1984 /* pimsm uses asserts, when switching from RPT to SPT,
1985 * so that we cannot check that packet arrived on an oif.
1986 * It is bad, but otherwise we would need to move pretty
1987 * large chunk of pimd to kernel. Ough... --ANK
1989 (mrt->mroute_do_pim ||
1990 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
1992 c->_c.mfc_un.res.last_assert +
1993 MFC_ASSERT_THRESH)) {
1994 c->_c.mfc_un.res.last_assert = jiffies;
1995 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1996 if (mrt->mroute_do_wrvifwhole)
1997 ipmr_cache_report(mrt, skb, true_vifi,
1998 IGMPMSG_WRVIFWHOLE);
2004 mrt->vif_table[vif].pkt_in++;
2005 mrt->vif_table[vif].bytes_in += skb->len;
2007 /* Forward the frame */
2008 if (c->mfc_origin == htonl(INADDR_ANY) &&
2009 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
2010 if (true_vifi >= 0 &&
2011 true_vifi != c->_c.mfc_parent &&
2013 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
2014 /* It's an (*,*) entry and the packet is not coming from
2015 * the upstream: forward the packet to the upstream
2018 psend = c->_c.mfc_parent;
2023 for (ct = c->_c.mfc_un.res.maxvif - 1;
2024 ct >= c->_c.mfc_un.res.minvif; ct--) {
2025 /* For (*,G) entry, don't forward to the incoming interface */
2026 if ((c->mfc_origin != htonl(INADDR_ANY) ||
2028 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
2030 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2033 ipmr_queue_xmit(net, mrt, true_vifi,
2042 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2045 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
2048 ipmr_queue_xmit(net, mrt, true_vifi, skb, c, psend);
2058 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
2060 struct rtable *rt = skb_rtable(skb);
2061 struct iphdr *iph = ip_hdr(skb);
2062 struct flowi4 fl4 = {
2063 .daddr = iph->daddr,
2064 .saddr = iph->saddr,
2065 .flowi4_tos = RT_TOS(iph->tos),
2066 .flowi4_oif = (rt_is_output_route(rt) ?
2067 skb->dev->ifindex : 0),
2068 .flowi4_iif = (rt_is_output_route(rt) ?
2071 .flowi4_mark = skb->mark,
2073 struct mr_table *mrt;
2076 err = ipmr_fib_lookup(net, &fl4, &mrt);
2078 return ERR_PTR(err);
2082 /* Multicast packets for forwarding arrive here
2083 * Called with rcu_read_lock();
2085 int ip_mr_input(struct sk_buff *skb)
2087 struct mfc_cache *cache;
2088 struct net *net = dev_net(skb->dev);
2089 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
2090 struct mr_table *mrt;
2091 struct net_device *dev;
2093 /* skb->dev passed in is the loX master dev for vrfs.
2094 * As there are no vifs associated with loopback devices,
2095 * get the proper interface that does have a vif associated with it.
2098 if (netif_is_l3_master(skb->dev)) {
2099 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2106 /* Packet is looped back after forward, it should not be
2107 * forwarded second time, but still can be delivered locally.
2109 if (IPCB(skb)->flags & IPSKB_FORWARDED)
2112 mrt = ipmr_rt_fib_lookup(net, skb);
2115 return PTR_ERR(mrt);
2118 if (IPCB(skb)->opt.router_alert) {
2119 if (ip_call_ra_chain(skb))
2121 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2122 /* IGMPv1 (and broken IGMPv2 implementations sort of
2123 * Cisco IOS <= 11.2(8)) do not put router alert
2124 * option to IGMP packets destined to routable
2125 * groups. It is very bad, because it means
2126 * that we can forward NO IGMP messages.
2128 struct sock *mroute_sk;
2130 mroute_sk = rcu_dereference(mrt->mroute_sk);
2133 raw_rcv(mroute_sk, skb);
2139 /* already under rcu_read_lock() */
2140 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
2142 int vif = ipmr_find_vif(mrt, dev);
2145 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2149 /* No usable cache entry */
2154 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2155 ip_local_deliver(skb);
2161 read_lock(&mrt_lock);
2162 vif = ipmr_find_vif(mrt, dev);
2164 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
2165 read_unlock(&mrt_lock);
2169 read_unlock(&mrt_lock);
2174 read_lock(&mrt_lock);
2175 ip_mr_forward(net, mrt, dev, skb, cache, local);
2176 read_unlock(&mrt_lock);
2179 return ip_local_deliver(skb);
2185 return ip_local_deliver(skb);
2190 #ifdef CONFIG_IP_PIMSM_V1
2191 /* Handle IGMP messages of PIMv1 */
2192 int pim_rcv_v1(struct sk_buff *skb)
2194 struct igmphdr *pim;
2195 struct net *net = dev_net(skb->dev);
2196 struct mr_table *mrt;
2198 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2201 pim = igmp_hdr(skb);
2203 mrt = ipmr_rt_fib_lookup(net, skb);
2206 if (!mrt->mroute_do_pim ||
2207 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2210 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2218 #ifdef CONFIG_IP_PIMSM_V2
2219 static int pim_rcv(struct sk_buff *skb)
2221 struct pimreghdr *pim;
2222 struct net *net = dev_net(skb->dev);
2223 struct mr_table *mrt;
2225 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2228 pim = (struct pimreghdr *)skb_transport_header(skb);
2229 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
2230 (pim->flags & PIM_NULL_REGISTER) ||
2231 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2232 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2235 mrt = ipmr_rt_fib_lookup(net, skb);
2238 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2246 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2247 __be32 saddr, __be32 daddr,
2248 struct rtmsg *rtm, u32 portid)
2250 struct mfc_cache *cache;
2251 struct mr_table *mrt;
2254 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2259 cache = ipmr_cache_find(mrt, saddr, daddr);
2260 if (!cache && skb->dev) {
2261 int vif = ipmr_find_vif(mrt, skb->dev);
2264 cache = ipmr_cache_find_any(mrt, daddr, vif);
2267 struct sk_buff *skb2;
2269 struct net_device *dev;
2273 read_lock(&mrt_lock);
2275 vif = ipmr_find_vif(mrt, dev);
2277 read_unlock(&mrt_lock);
2282 skb2 = skb_realloc_headroom(skb, sizeof(struct iphdr));
2284 read_unlock(&mrt_lock);
2289 NETLINK_CB(skb2).portid = portid;
2290 skb_push(skb2, sizeof(struct iphdr));
2291 skb_reset_network_header(skb2);
2293 iph->ihl = sizeof(struct iphdr) >> 2;
2297 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
2298 read_unlock(&mrt_lock);
2303 read_lock(&mrt_lock);
2304 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
2305 read_unlock(&mrt_lock);
2310 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2311 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2314 struct nlmsghdr *nlh;
2318 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2322 rtm = nlmsg_data(nlh);
2323 rtm->rtm_family = RTNL_FAMILY_IPMR;
2324 rtm->rtm_dst_len = 32;
2325 rtm->rtm_src_len = 32;
2327 rtm->rtm_table = mrt->id;
2328 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2329 goto nla_put_failure;
2330 rtm->rtm_type = RTN_MULTICAST;
2331 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2332 if (c->_c.mfc_flags & MFC_STATIC)
2333 rtm->rtm_protocol = RTPROT_STATIC;
2335 rtm->rtm_protocol = RTPROT_MROUTED;
2338 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2339 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2340 goto nla_put_failure;
2341 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
2342 /* do not break the dump if cache is unresolved */
2343 if (err < 0 && err != -ENOENT)
2344 goto nla_put_failure;
2346 nlmsg_end(skb, nlh);
2350 nlmsg_cancel(skb, nlh);
2354 static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2355 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2358 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2362 static size_t mroute_msgsize(bool unresolved, int maxvif)
2365 NLMSG_ALIGN(sizeof(struct rtmsg))
2366 + nla_total_size(4) /* RTA_TABLE */
2367 + nla_total_size(4) /* RTA_SRC */
2368 + nla_total_size(4) /* RTA_DST */
2373 + nla_total_size(4) /* RTA_IIF */
2374 + nla_total_size(0) /* RTA_MULTIPATH */
2375 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2377 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
2383 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2386 struct net *net = read_pnet(&mrt->net);
2387 struct sk_buff *skb;
2390 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2396 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2400 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2406 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2409 static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2412 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2413 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2414 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2415 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2416 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2417 /* IPMRA_CREPORT_PKT */
2418 + nla_total_size(payloadlen)
2424 static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2426 struct net *net = read_pnet(&mrt->net);
2427 struct nlmsghdr *nlh;
2428 struct rtgenmsg *rtgenm;
2429 struct igmpmsg *msg;
2430 struct sk_buff *skb;
2434 payloadlen = pkt->len - sizeof(struct igmpmsg);
2435 msg = (struct igmpmsg *)skb_network_header(pkt);
2437 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2441 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2442 sizeof(struct rtgenmsg), 0);
2445 rtgenm = nlmsg_data(nlh);
2446 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2447 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2448 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2449 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2450 msg->im_src.s_addr) ||
2451 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2452 msg->im_dst.s_addr))
2453 goto nla_put_failure;
2455 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2456 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2457 nla_data(nla), payloadlen))
2458 goto nla_put_failure;
2460 nlmsg_end(skb, nlh);
2462 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2466 nlmsg_cancel(skb, nlh);
2469 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2472 static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2473 struct netlink_ext_ack *extack)
2475 struct net *net = sock_net(in_skb->sk);
2476 struct nlattr *tb[RTA_MAX + 1];
2477 struct sk_buff *skb = NULL;
2478 struct mfc_cache *cache;
2479 struct mr_table *mrt;
2485 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2486 rtm_ipv4_policy, extack);
2490 rtm = nlmsg_data(nlh);
2492 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2493 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2494 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2496 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
2502 /* entries are added/deleted only under RTNL */
2504 cache = ipmr_cache_find(mrt, src, grp);
2511 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2517 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2518 nlh->nlmsg_seq, cache,
2523 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2533 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2535 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
2536 _ipmr_fill_mroute, &mfc_unres_lock);
2539 static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2540 [RTA_SRC] = { .type = NLA_U32 },
2541 [RTA_DST] = { .type = NLA_U32 },
2542 [RTA_IIF] = { .type = NLA_U32 },
2543 [RTA_TABLE] = { .type = NLA_U32 },
2544 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2547 static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2549 switch (rtm_protocol) {
2551 case RTPROT_MROUTED:
2557 static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2559 struct rtnexthop *rtnh = nla_data(nla);
2560 int remaining = nla_len(nla), vifi = 0;
2562 while (rtnh_ok(rtnh, remaining)) {
2563 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2564 if (++vifi == MAXVIFS)
2566 rtnh = rtnh_next(rtnh, &remaining);
2569 return remaining > 0 ? -EINVAL : vifi;
2572 /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2573 static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2574 struct mfcctl *mfcc, int *mrtsock,
2575 struct mr_table **mrtret,
2576 struct netlink_ext_ack *extack)
2578 struct net_device *dev = NULL;
2579 u32 tblid = RT_TABLE_DEFAULT;
2580 struct mr_table *mrt;
2581 struct nlattr *attr;
2585 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
2589 rtm = nlmsg_data(nlh);
2592 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2593 rtm->rtm_type != RTN_MULTICAST ||
2594 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2595 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2598 memset(mfcc, 0, sizeof(*mfcc));
2599 mfcc->mfcc_parent = -1;
2601 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2602 switch (nla_type(attr)) {
2604 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2607 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2610 dev = __dev_get_by_index(net, nla_get_u32(attr));
2617 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2626 tblid = nla_get_u32(attr);
2630 mrt = ipmr_get_table(net, tblid);
2636 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2638 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2644 /* takes care of both newroute and delroute */
2645 static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2646 struct netlink_ext_ack *extack)
2648 struct net *net = sock_net(skb->sk);
2649 int ret, mrtsock, parent;
2650 struct mr_table *tbl;
2655 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
2659 parent = ret ? mfcc.mfcc_parent : -1;
2660 if (nlh->nlmsg_type == RTM_NEWROUTE)
2661 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2663 return ipmr_mfc_delete(tbl, &mfcc, parent);
2666 static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2668 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2670 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2671 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2672 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2673 mrt->mroute_reg_vif_num) ||
2674 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2675 mrt->mroute_do_assert) ||
2676 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
2677 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
2678 mrt->mroute_do_wrvifwhole))
2684 static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2686 struct nlattr *vif_nest;
2687 struct vif_device *vif;
2689 /* if the VIF doesn't exist just continue */
2690 if (!VIF_EXISTS(mrt, vifid))
2693 vif = &mrt->vif_table[vifid];
2694 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2697 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2698 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2699 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2700 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2702 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2704 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2706 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2708 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2709 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2710 nla_nest_cancel(skb, vif_nest);
2713 nla_nest_end(skb, vif_nest);
2718 static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2720 struct net *net = sock_net(skb->sk);
2721 struct nlmsghdr *nlh = NULL;
2722 unsigned int t = 0, s_t;
2723 unsigned int e = 0, s_e;
2724 struct mr_table *mrt;
2729 ipmr_for_each_table(mrt, net) {
2730 struct nlattr *vifs, *af;
2731 struct ifinfomsg *hdr;
2736 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2737 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2738 sizeof(*hdr), NLM_F_MULTI);
2742 hdr = nlmsg_data(nlh);
2743 memset(hdr, 0, sizeof(*hdr));
2744 hdr->ifi_family = RTNL_FAMILY_IPMR;
2746 af = nla_nest_start(skb, IFLA_AF_SPEC);
2748 nlmsg_cancel(skb, nlh);
2752 if (!ipmr_fill_table(mrt, skb)) {
2753 nlmsg_cancel(skb, nlh);
2757 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2759 nla_nest_end(skb, af);
2760 nlmsg_end(skb, nlh);
2763 for (i = 0; i < mrt->maxvif; i++) {
2766 if (!ipmr_fill_vif(mrt, i, skb)) {
2767 nla_nest_end(skb, vifs);
2768 nla_nest_end(skb, af);
2769 nlmsg_end(skb, nlh);
2777 nla_nest_end(skb, vifs);
2778 nla_nest_end(skb, af);
2779 nlmsg_end(skb, nlh);
2791 #ifdef CONFIG_PROC_FS
2792 /* The /proc interfaces to multicast routing :
2793 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2796 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2797 __acquires(mrt_lock)
2799 struct mr_vif_iter *iter = seq->private;
2800 struct net *net = seq_file_net(seq);
2801 struct mr_table *mrt;
2803 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2805 return ERR_PTR(-ENOENT);
2809 read_lock(&mrt_lock);
2810 return mr_vif_seq_start(seq, pos);
2813 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2814 __releases(mrt_lock)
2816 read_unlock(&mrt_lock);
2819 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2821 struct mr_vif_iter *iter = seq->private;
2822 struct mr_table *mrt = iter->mrt;
2824 if (v == SEQ_START_TOKEN) {
2826 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2828 const struct vif_device *vif = v;
2829 const char *name = vif->dev ?
2830 vif->dev->name : "none";
2833 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2834 vif - mrt->vif_table,
2835 name, vif->bytes_in, vif->pkt_in,
2836 vif->bytes_out, vif->pkt_out,
2837 vif->flags, vif->local, vif->remote);
2842 static const struct seq_operations ipmr_vif_seq_ops = {
2843 .start = ipmr_vif_seq_start,
2844 .next = mr_vif_seq_next,
2845 .stop = ipmr_vif_seq_stop,
2846 .show = ipmr_vif_seq_show,
2849 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2851 struct net *net = seq_file_net(seq);
2852 struct mr_table *mrt;
2854 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2856 return ERR_PTR(-ENOENT);
2858 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
2861 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2865 if (v == SEQ_START_TOKEN) {
2867 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2869 const struct mfc_cache *mfc = v;
2870 const struct mr_mfc_iter *it = seq->private;
2871 const struct mr_table *mrt = it->mrt;
2873 seq_printf(seq, "%08X %08X %-3hd",
2874 (__force u32) mfc->mfc_mcastgrp,
2875 (__force u32) mfc->mfc_origin,
2876 mfc->_c.mfc_parent);
2878 if (it->cache != &mrt->mfc_unres_queue) {
2879 seq_printf(seq, " %8lu %8lu %8lu",
2880 mfc->_c.mfc_un.res.pkt,
2881 mfc->_c.mfc_un.res.bytes,
2882 mfc->_c.mfc_un.res.wrong_if);
2883 for (n = mfc->_c.mfc_un.res.minvif;
2884 n < mfc->_c.mfc_un.res.maxvif; n++) {
2885 if (VIF_EXISTS(mrt, n) &&
2886 mfc->_c.mfc_un.res.ttls[n] < 255)
2889 n, mfc->_c.mfc_un.res.ttls[n]);
2892 /* unresolved mfc_caches don't contain
2893 * pkt, bytes and wrong_if values
2895 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2897 seq_putc(seq, '\n');
2902 static const struct seq_operations ipmr_mfc_seq_ops = {
2903 .start = ipmr_mfc_seq_start,
2904 .next = mr_mfc_seq_next,
2905 .stop = mr_mfc_seq_stop,
2906 .show = ipmr_mfc_seq_show,
2910 #ifdef CONFIG_IP_PIMSM_V2
2911 static const struct net_protocol pim_protocol = {
2917 static unsigned int ipmr_seq_read(struct net *net)
2921 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
2924 static int ipmr_dump(struct net *net, struct notifier_block *nb)
2926 return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
2927 ipmr_mr_table_iter, &mrt_lock);
2930 static const struct fib_notifier_ops ipmr_notifier_ops_template = {
2931 .family = RTNL_FAMILY_IPMR,
2932 .fib_seq_read = ipmr_seq_read,
2933 .fib_dump = ipmr_dump,
2934 .owner = THIS_MODULE,
2937 static int __net_init ipmr_notifier_init(struct net *net)
2939 struct fib_notifier_ops *ops;
2941 net->ipv4.ipmr_seq = 0;
2943 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
2945 return PTR_ERR(ops);
2946 net->ipv4.ipmr_notifier_ops = ops;
2951 static void __net_exit ipmr_notifier_exit(struct net *net)
2953 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
2954 net->ipv4.ipmr_notifier_ops = NULL;
2957 /* Setup for IP multicast routing */
2958 static int __net_init ipmr_net_init(struct net *net)
2962 err = ipmr_notifier_init(net);
2964 goto ipmr_notifier_fail;
2966 err = ipmr_rules_init(net);
2968 goto ipmr_rules_fail;
2970 #ifdef CONFIG_PROC_FS
2972 if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
2973 sizeof(struct mr_vif_iter)))
2975 if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
2976 sizeof(struct mr_mfc_iter)))
2977 goto proc_cache_fail;
2981 #ifdef CONFIG_PROC_FS
2983 remove_proc_entry("ip_mr_vif", net->proc_net);
2985 ipmr_rules_exit(net);
2988 ipmr_notifier_exit(net);
2993 static void __net_exit ipmr_net_exit(struct net *net)
2995 #ifdef CONFIG_PROC_FS
2996 remove_proc_entry("ip_mr_cache", net->proc_net);
2997 remove_proc_entry("ip_mr_vif", net->proc_net);
2999 ipmr_notifier_exit(net);
3000 ipmr_rules_exit(net);
3003 static struct pernet_operations ipmr_net_ops = {
3004 .init = ipmr_net_init,
3005 .exit = ipmr_net_exit,
3008 int __init ip_mr_init(void)
3012 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3013 sizeof(struct mfc_cache),
3014 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
3017 err = register_pernet_subsys(&ipmr_net_ops);
3019 goto reg_pernet_fail;
3021 err = register_netdevice_notifier(&ip_mr_notifier);
3023 goto reg_notif_fail;
3024 #ifdef CONFIG_IP_PIMSM_V2
3025 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
3026 pr_err("%s: can't add PIM protocol\n", __func__);
3028 goto add_proto_fail;
3031 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
3032 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
3033 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
3034 ipmr_rtm_route, NULL, 0);
3035 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
3036 ipmr_rtm_route, NULL, 0);
3038 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
3039 NULL, ipmr_rtm_dumplink, 0);
3042 #ifdef CONFIG_IP_PIMSM_V2
3044 unregister_netdevice_notifier(&ip_mr_notifier);
3047 unregister_pernet_subsys(&ipmr_net_ops);
3049 kmem_cache_destroy(mrt_cachep);