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 <net/ip_tunnels.h>
64 #include <net/checksum.h>
65 #include <net/netlink.h>
66 #include <net/fib_rules.h>
67 #include <linux/netconf.h>
68 #include <net/nexthop.h>
69 #include <net/switchdev.h>
72 struct fib_rule common;
79 /* Big lock, protecting vif table, mrt cache and mroute socket state.
80 * Note that the changes are semaphored via rtnl_lock.
83 static DEFINE_RWLOCK(mrt_lock);
85 /* Multicast router control variables */
87 /* Special spinlock for queue of unresolved entries */
88 static DEFINE_SPINLOCK(mfc_unres_lock);
90 /* We return to original Alan's scheme. Hash table of resolved
91 * entries is changed only in process context and protected
92 * with weak lock mrt_lock. Queue of unresolved entries is protected
93 * with strong spinlock mfc_unres_lock.
95 * In this case data path is free of exclusive locks at all.
98 static struct kmem_cache *mrt_cachep __ro_after_init;
100 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
101 static void ipmr_free_table(struct mr_table *mrt);
103 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
104 struct net_device *dev, struct sk_buff *skb,
105 struct mfc_cache *cache, int local);
106 static int ipmr_cache_report(struct mr_table *mrt,
107 struct sk_buff *pkt, vifi_t vifi, int assert);
108 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
110 static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
111 static void mroute_clean_tables(struct mr_table *mrt, bool all);
112 static void ipmr_expire_process(struct timer_list *t);
114 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
115 #define ipmr_for_each_table(mrt, net) \
116 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
118 static struct mr_table *ipmr_mr_table_iter(struct net *net,
119 struct mr_table *mrt)
121 struct mr_table *ret;
124 ret = list_entry_rcu(net->ipv4.mr_tables.next,
125 struct mr_table, list);
127 ret = list_entry_rcu(mrt->list.next,
128 struct mr_table, list);
130 if (&ret->list == &net->ipv4.mr_tables)
135 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
137 struct mr_table *mrt;
139 ipmr_for_each_table(mrt, net) {
146 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
147 struct mr_table **mrt)
150 struct ipmr_result res;
151 struct fib_lookup_arg arg = {
153 .flags = FIB_LOOKUP_NOREF,
156 /* update flow if oif or iif point to device enslaved to l3mdev */
157 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
159 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
160 flowi4_to_flowi(flp4), 0, &arg);
167 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
168 int flags, struct fib_lookup_arg *arg)
170 struct ipmr_result *res = arg->result;
171 struct mr_table *mrt;
173 switch (rule->action) {
176 case FR_ACT_UNREACHABLE:
178 case FR_ACT_PROHIBIT:
180 case FR_ACT_BLACKHOLE:
185 arg->table = fib_rule_get_table(rule, arg);
187 mrt = ipmr_get_table(rule->fr_net, arg->table);
194 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
199 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
203 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
204 struct fib_rule_hdr *frh, struct nlattr **tb)
209 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
215 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
216 struct fib_rule_hdr *frh)
224 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
225 .family = RTNL_FAMILY_IPMR,
226 .rule_size = sizeof(struct ipmr_rule),
227 .addr_size = sizeof(u32),
228 .action = ipmr_rule_action,
229 .match = ipmr_rule_match,
230 .configure = ipmr_rule_configure,
231 .compare = ipmr_rule_compare,
232 .fill = ipmr_rule_fill,
233 .nlgroup = RTNLGRP_IPV4_RULE,
234 .policy = ipmr_rule_policy,
235 .owner = THIS_MODULE,
238 static int __net_init ipmr_rules_init(struct net *net)
240 struct fib_rules_ops *ops;
241 struct mr_table *mrt;
244 ops = fib_rules_register(&ipmr_rules_ops_template, net);
248 INIT_LIST_HEAD(&net->ipv4.mr_tables);
250 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
256 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
260 net->ipv4.mr_rules_ops = ops;
264 ipmr_free_table(mrt);
266 fib_rules_unregister(ops);
270 static void __net_exit ipmr_rules_exit(struct net *net)
272 struct mr_table *mrt, *next;
275 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
276 list_del(&mrt->list);
277 ipmr_free_table(mrt);
279 fib_rules_unregister(net->ipv4.mr_rules_ops);
283 static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
285 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR);
288 static unsigned int ipmr_rules_seq_read(struct net *net)
290 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
293 bool ipmr_rule_default(const struct fib_rule *rule)
295 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
297 EXPORT_SYMBOL(ipmr_rule_default);
299 #define ipmr_for_each_table(mrt, net) \
300 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
302 static struct mr_table *ipmr_mr_table_iter(struct net *net,
303 struct mr_table *mrt)
306 return net->ipv4.mrt;
310 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
312 return net->ipv4.mrt;
315 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
316 struct mr_table **mrt)
318 *mrt = net->ipv4.mrt;
322 static int __net_init ipmr_rules_init(struct net *net)
324 struct mr_table *mrt;
326 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
333 static void __net_exit ipmr_rules_exit(struct net *net)
336 ipmr_free_table(net->ipv4.mrt);
337 net->ipv4.mrt = NULL;
341 static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
346 static unsigned int ipmr_rules_seq_read(struct net *net)
351 bool ipmr_rule_default(const struct fib_rule *rule)
355 EXPORT_SYMBOL(ipmr_rule_default);
358 static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
361 const struct mfc_cache_cmp_arg *cmparg = arg->key;
362 struct mfc_cache *c = (struct mfc_cache *)ptr;
364 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
365 cmparg->mfc_origin != c->mfc_origin;
368 static const struct rhashtable_params ipmr_rht_params = {
369 .head_offset = offsetof(struct mr_mfc, mnode),
370 .key_offset = offsetof(struct mfc_cache, cmparg),
371 .key_len = sizeof(struct mfc_cache_cmp_arg),
374 .obj_cmpfn = ipmr_hash_cmp,
375 .automatic_shrinking = true,
378 static void ipmr_new_table_set(struct mr_table *mrt,
381 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
382 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
386 static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
387 .mfc_mcastgrp = htonl(INADDR_ANY),
388 .mfc_origin = htonl(INADDR_ANY),
391 static struct mr_table_ops ipmr_mr_table_ops = {
392 .rht_params = &ipmr_rht_params,
393 .cmparg_any = &ipmr_mr_table_ops_cmparg_any,
396 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
398 struct mr_table *mrt;
400 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
401 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
402 return ERR_PTR(-EINVAL);
404 mrt = ipmr_get_table(net, id);
408 return mr_table_alloc(net, id, &ipmr_mr_table_ops,
409 ipmr_expire_process, ipmr_new_table_set);
412 static void ipmr_free_table(struct mr_table *mrt)
414 del_timer_sync(&mrt->ipmr_expire_timer);
415 mroute_clean_tables(mrt, true);
416 rhltable_destroy(&mrt->mfc_hash);
420 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
422 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
424 struct net *net = dev_net(dev);
428 dev = __dev_get_by_name(net, "tunl0");
430 const struct net_device_ops *ops = dev->netdev_ops;
432 struct ip_tunnel_parm p;
434 memset(&p, 0, sizeof(p));
435 p.iph.daddr = v->vifc_rmt_addr.s_addr;
436 p.iph.saddr = v->vifc_lcl_addr.s_addr;
439 p.iph.protocol = IPPROTO_IPIP;
440 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
441 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
443 if (ops->ndo_do_ioctl) {
444 mm_segment_t oldfs = get_fs();
447 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
453 /* Initialize ipmr pimreg/tunnel in_device */
454 static bool ipmr_init_vif_indev(const struct net_device *dev)
456 struct in_device *in_dev;
460 in_dev = __in_dev_get_rtnl(dev);
463 ipv4_devconf_setall(in_dev);
464 neigh_parms_data_state_setall(in_dev->arp_parms);
465 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
470 static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
472 struct net_device *dev;
474 dev = __dev_get_by_name(net, "tunl0");
477 const struct net_device_ops *ops = dev->netdev_ops;
480 struct ip_tunnel_parm p;
482 memset(&p, 0, sizeof(p));
483 p.iph.daddr = v->vifc_rmt_addr.s_addr;
484 p.iph.saddr = v->vifc_lcl_addr.s_addr;
487 p.iph.protocol = IPPROTO_IPIP;
488 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
489 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
491 if (ops->ndo_do_ioctl) {
492 mm_segment_t oldfs = get_fs();
495 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
503 (dev = __dev_get_by_name(net, p.name)) != NULL) {
504 dev->flags |= IFF_MULTICAST;
505 if (!ipmr_init_vif_indev(dev))
515 unregister_netdevice(dev);
519 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
520 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
522 struct net *net = dev_net(dev);
523 struct mr_table *mrt;
524 struct flowi4 fl4 = {
525 .flowi4_oif = dev->ifindex,
526 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
527 .flowi4_mark = skb->mark,
531 err = ipmr_fib_lookup(net, &fl4, &mrt);
537 read_lock(&mrt_lock);
538 dev->stats.tx_bytes += skb->len;
539 dev->stats.tx_packets++;
540 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
541 read_unlock(&mrt_lock);
546 static int reg_vif_get_iflink(const struct net_device *dev)
551 static const struct net_device_ops reg_vif_netdev_ops = {
552 .ndo_start_xmit = reg_vif_xmit,
553 .ndo_get_iflink = reg_vif_get_iflink,
556 static void reg_vif_setup(struct net_device *dev)
558 dev->type = ARPHRD_PIMREG;
559 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
560 dev->flags = IFF_NOARP;
561 dev->netdev_ops = ®_vif_netdev_ops;
562 dev->needs_free_netdev = true;
563 dev->features |= NETIF_F_NETNS_LOCAL;
566 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
568 struct net_device *dev;
571 if (mrt->id == RT_TABLE_DEFAULT)
572 sprintf(name, "pimreg");
574 sprintf(name, "pimreg%u", mrt->id);
576 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
581 dev_net_set(dev, net);
583 if (register_netdevice(dev)) {
588 if (!ipmr_init_vif_indev(dev))
598 unregister_netdevice(dev);
602 /* called with rcu_read_lock() */
603 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
606 struct net_device *reg_dev = NULL;
609 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
611 * a. packet is really sent to a multicast group
612 * b. packet is not a NULL-REGISTER
613 * c. packet is not truncated
615 if (!ipv4_is_multicast(encap->daddr) ||
616 encap->tot_len == 0 ||
617 ntohs(encap->tot_len) + pimlen > skb->len)
620 read_lock(&mrt_lock);
621 if (mrt->mroute_reg_vif_num >= 0)
622 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
623 read_unlock(&mrt_lock);
628 skb->mac_header = skb->network_header;
629 skb_pull(skb, (u8 *)encap - skb->data);
630 skb_reset_network_header(skb);
631 skb->protocol = htons(ETH_P_IP);
632 skb->ip_summed = CHECKSUM_NONE;
634 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
638 return NET_RX_SUCCESS;
641 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
647 static int call_ipmr_vif_entry_notifier(struct notifier_block *nb,
649 enum fib_event_type event_type,
650 struct vif_device *vif,
651 vifi_t vif_index, u32 tb_id)
653 struct vif_entry_notifier_info info = {
655 .family = RTNL_FAMILY_IPMR,
659 .vif_index = vif_index,
660 .vif_flags = vif->flags,
664 return call_fib_notifier(nb, net, event_type, &info.info);
667 static int call_ipmr_vif_entry_notifiers(struct net *net,
668 enum fib_event_type event_type,
669 struct vif_device *vif,
670 vifi_t vif_index, u32 tb_id)
672 struct vif_entry_notifier_info info = {
674 .family = RTNL_FAMILY_IPMR,
678 .vif_index = vif_index,
679 .vif_flags = vif->flags,
684 net->ipv4.ipmr_seq++;
685 return call_fib_notifiers(net, event_type, &info.info);
688 static int call_ipmr_mfc_entry_notifier(struct notifier_block *nb,
690 enum fib_event_type event_type,
691 struct mfc_cache *mfc, u32 tb_id)
693 struct mfc_entry_notifier_info info = {
695 .family = RTNL_FAMILY_IPMR,
702 return call_fib_notifier(nb, net, event_type, &info.info);
705 static int call_ipmr_mfc_entry_notifiers(struct net *net,
706 enum fib_event_type event_type,
707 struct mfc_cache *mfc, u32 tb_id)
709 struct mfc_entry_notifier_info info = {
711 .family = RTNL_FAMILY_IPMR,
719 net->ipv4.ipmr_seq++;
720 return call_fib_notifiers(net, event_type, &info.info);
724 * vif_delete - Delete a VIF entry
725 * @notify: Set to 1, if the caller is a notifier_call
727 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
728 struct list_head *head)
730 struct net *net = read_pnet(&mrt->net);
731 struct vif_device *v;
732 struct net_device *dev;
733 struct in_device *in_dev;
735 if (vifi < 0 || vifi >= mrt->maxvif)
736 return -EADDRNOTAVAIL;
738 v = &mrt->vif_table[vifi];
740 if (VIF_EXISTS(mrt, vifi))
741 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
744 write_lock_bh(&mrt_lock);
749 write_unlock_bh(&mrt_lock);
750 return -EADDRNOTAVAIL;
753 if (vifi == mrt->mroute_reg_vif_num)
754 mrt->mroute_reg_vif_num = -1;
756 if (vifi + 1 == mrt->maxvif) {
759 for (tmp = vifi - 1; tmp >= 0; tmp--) {
760 if (VIF_EXISTS(mrt, tmp))
766 write_unlock_bh(&mrt_lock);
768 dev_set_allmulti(dev, -1);
770 in_dev = __in_dev_get_rtnl(dev);
772 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
773 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
774 NETCONFA_MC_FORWARDING,
775 dev->ifindex, &in_dev->cnf);
776 ip_rt_multicast_event(in_dev);
779 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
780 unregister_netdevice_queue(dev, head);
786 static void ipmr_cache_free_rcu(struct rcu_head *head)
788 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
790 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
793 void ipmr_cache_free(struct mfc_cache *c)
795 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
797 EXPORT_SYMBOL(ipmr_cache_free);
799 /* Destroy an unresolved cache entry, killing queued skbs
800 * and reporting error to netlink readers.
802 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
804 struct net *net = read_pnet(&mrt->net);
808 atomic_dec(&mrt->cache_resolve_queue_len);
810 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
811 if (ip_hdr(skb)->version == 0) {
812 struct nlmsghdr *nlh = skb_pull(skb,
813 sizeof(struct iphdr));
814 nlh->nlmsg_type = NLMSG_ERROR;
815 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
816 skb_trim(skb, nlh->nlmsg_len);
818 e->error = -ETIMEDOUT;
819 memset(&e->msg, 0, sizeof(e->msg));
821 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
830 /* Timer process for the unresolved queue. */
831 static void ipmr_expire_process(struct timer_list *t)
833 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
834 struct mr_mfc *c, *next;
835 unsigned long expires;
838 if (!spin_trylock(&mfc_unres_lock)) {
839 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
843 if (list_empty(&mrt->mfc_unres_queue))
849 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
850 if (time_after(c->mfc_un.unres.expires, now)) {
851 unsigned long interval = c->mfc_un.unres.expires - now;
852 if (interval < expires)
858 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
859 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
862 if (!list_empty(&mrt->mfc_unres_queue))
863 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
866 spin_unlock(&mfc_unres_lock);
869 /* Fill oifs list. It is called under write locked mrt_lock. */
870 static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
875 cache->mfc_un.res.minvif = MAXVIFS;
876 cache->mfc_un.res.maxvif = 0;
877 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
879 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
880 if (VIF_EXISTS(mrt, vifi) &&
881 ttls[vifi] && ttls[vifi] < 255) {
882 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
883 if (cache->mfc_un.res.minvif > vifi)
884 cache->mfc_un.res.minvif = vifi;
885 if (cache->mfc_un.res.maxvif <= vifi)
886 cache->mfc_un.res.maxvif = vifi + 1;
889 cache->mfc_un.res.lastuse = jiffies;
892 static int vif_add(struct net *net, struct mr_table *mrt,
893 struct vifctl *vifc, int mrtsock)
895 int vifi = vifc->vifc_vifi;
896 struct switchdev_attr attr = {
897 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
899 struct vif_device *v = &mrt->vif_table[vifi];
900 struct net_device *dev;
901 struct in_device *in_dev;
905 if (VIF_EXISTS(mrt, vifi))
908 switch (vifc->vifc_flags) {
910 if (!ipmr_pimsm_enabled())
912 /* Special Purpose VIF in PIM
913 * All the packets will be sent to the daemon
915 if (mrt->mroute_reg_vif_num >= 0)
917 dev = ipmr_reg_vif(net, mrt);
920 err = dev_set_allmulti(dev, 1);
922 unregister_netdevice(dev);
928 dev = ipmr_new_tunnel(net, vifc);
931 err = dev_set_allmulti(dev, 1);
933 ipmr_del_tunnel(dev, vifc);
938 case VIFF_USE_IFINDEX:
940 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
941 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
942 if (dev && !__in_dev_get_rtnl(dev)) {
944 return -EADDRNOTAVAIL;
947 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
950 return -EADDRNOTAVAIL;
951 err = dev_set_allmulti(dev, 1);
961 in_dev = __in_dev_get_rtnl(dev);
964 return -EADDRNOTAVAIL;
966 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
967 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
968 dev->ifindex, &in_dev->cnf);
969 ip_rt_multicast_event(in_dev);
971 /* Fill in the VIF structures */
972 vif_device_init(v, dev, vifc->vifc_rate_limit,
973 vifc->vifc_threshold,
974 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
975 (VIFF_TUNNEL | VIFF_REGISTER));
978 if (!switchdev_port_attr_get(dev, &attr)) {
979 memcpy(v->dev_parent_id.id, attr.u.ppid.id, attr.u.ppid.id_len);
980 v->dev_parent_id.id_len = attr.u.ppid.id_len;
982 v->dev_parent_id.id_len = 0;
985 v->local = vifc->vifc_lcl_addr.s_addr;
986 v->remote = vifc->vifc_rmt_addr.s_addr;
988 /* And finish update writing critical data */
989 write_lock_bh(&mrt_lock);
991 if (v->flags & VIFF_REGISTER)
992 mrt->mroute_reg_vif_num = vifi;
993 if (vifi+1 > mrt->maxvif)
994 mrt->maxvif = vifi+1;
995 write_unlock_bh(&mrt_lock);
996 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
1000 /* called with rcu_read_lock() */
1001 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
1005 struct mfc_cache_cmp_arg arg = {
1006 .mfc_mcastgrp = mcastgrp,
1007 .mfc_origin = origin
1010 return mr_mfc_find(mrt, &arg);
1013 /* Look for a (*,G) entry */
1014 static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
1015 __be32 mcastgrp, int vifi)
1017 struct mfc_cache_cmp_arg arg = {
1018 .mfc_mcastgrp = mcastgrp,
1019 .mfc_origin = htonl(INADDR_ANY)
1022 if (mcastgrp == htonl(INADDR_ANY))
1023 return mr_mfc_find_any_parent(mrt, vifi);
1024 return mr_mfc_find_any(mrt, vifi, &arg);
1027 /* Look for a (S,G,iif) entry if parent != -1 */
1028 static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
1029 __be32 origin, __be32 mcastgrp,
1032 struct mfc_cache_cmp_arg arg = {
1033 .mfc_mcastgrp = mcastgrp,
1034 .mfc_origin = origin,
1037 return mr_mfc_find_parent(mrt, &arg, parent);
1040 /* Allocate a multicast cache entry */
1041 static struct mfc_cache *ipmr_cache_alloc(void)
1043 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
1046 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
1047 c->_c.mfc_un.res.minvif = MAXVIFS;
1048 refcount_set(&c->_c.mfc_un.res.refcount, 1);
1053 static struct mfc_cache *ipmr_cache_alloc_unres(void)
1055 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
1058 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
1059 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
1064 /* A cache entry has gone into a resolved state from queued */
1065 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1066 struct mfc_cache *uc, struct mfc_cache *c)
1068 struct sk_buff *skb;
1071 /* Play the pending entries through our router */
1072 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
1073 if (ip_hdr(skb)->version == 0) {
1074 struct nlmsghdr *nlh = skb_pull(skb,
1075 sizeof(struct iphdr));
1077 if (mr_fill_mroute(mrt, skb, &c->_c,
1078 nlmsg_data(nlh)) > 0) {
1079 nlh->nlmsg_len = skb_tail_pointer(skb) -
1082 nlh->nlmsg_type = NLMSG_ERROR;
1083 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
1084 skb_trim(skb, nlh->nlmsg_len);
1085 e = nlmsg_data(nlh);
1086 e->error = -EMSGSIZE;
1087 memset(&e->msg, 0, sizeof(e->msg));
1090 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
1092 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
1097 /* Bounce a cache query up to mrouted and netlink.
1099 * Called under mrt_lock.
1101 static int ipmr_cache_report(struct mr_table *mrt,
1102 struct sk_buff *pkt, vifi_t vifi, int assert)
1104 const int ihl = ip_hdrlen(pkt);
1105 struct sock *mroute_sk;
1106 struct igmphdr *igmp;
1107 struct igmpmsg *msg;
1108 struct sk_buff *skb;
1111 if (assert == IGMPMSG_WHOLEPKT)
1112 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1114 skb = alloc_skb(128, GFP_ATOMIC);
1119 if (assert == IGMPMSG_WHOLEPKT) {
1120 /* Ugly, but we have no choice with this interface.
1121 * Duplicate old header, fix ihl, length etc.
1122 * And all this only to mangle msg->im_msgtype and
1123 * to set msg->im_mbz to "mbz" :-)
1125 skb_push(skb, sizeof(struct iphdr));
1126 skb_reset_network_header(skb);
1127 skb_reset_transport_header(skb);
1128 msg = (struct igmpmsg *)skb_network_header(skb);
1129 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
1130 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1132 msg->im_vif = mrt->mroute_reg_vif_num;
1133 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1134 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1135 sizeof(struct iphdr));
1137 /* Copy the IP header */
1138 skb_set_network_header(skb, skb->len);
1140 skb_copy_to_linear_data(skb, pkt->data, ihl);
1141 /* Flag to the kernel this is a route add */
1142 ip_hdr(skb)->protocol = 0;
1143 msg = (struct igmpmsg *)skb_network_header(skb);
1145 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1146 /* Add our header */
1147 igmp = skb_put(skb, sizeof(struct igmphdr));
1148 igmp->type = assert;
1149 msg->im_msgtype = assert;
1151 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1152 skb->transport_header = skb->network_header;
1156 mroute_sk = rcu_dereference(mrt->mroute_sk);
1163 igmpmsg_netlink_event(mrt, skb);
1165 /* Deliver to mrouted */
1166 ret = sock_queue_rcv_skb(mroute_sk, skb);
1169 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1176 /* Queue a packet for resolution. It gets locked cache entry! */
1177 static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1178 struct sk_buff *skb, struct net_device *dev)
1180 const struct iphdr *iph = ip_hdr(skb);
1181 struct mfc_cache *c;
1185 spin_lock_bh(&mfc_unres_lock);
1186 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
1187 if (c->mfc_mcastgrp == iph->daddr &&
1188 c->mfc_origin == iph->saddr) {
1195 /* Create a new entry if allowable */
1196 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1197 (c = ipmr_cache_alloc_unres()) == NULL) {
1198 spin_unlock_bh(&mfc_unres_lock);
1204 /* Fill in the new cache entry */
1205 c->_c.mfc_parent = -1;
1206 c->mfc_origin = iph->saddr;
1207 c->mfc_mcastgrp = iph->daddr;
1209 /* Reflect first query at mrouted. */
1210 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1213 /* If the report failed throw the cache entry
1216 spin_unlock_bh(&mfc_unres_lock);
1223 atomic_inc(&mrt->cache_resolve_queue_len);
1224 list_add(&c->_c.list, &mrt->mfc_unres_queue);
1225 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1227 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1228 mod_timer(&mrt->ipmr_expire_timer,
1229 c->_c.mfc_un.unres.expires);
1232 /* See if we can append the packet */
1233 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
1239 skb->skb_iif = dev->ifindex;
1241 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
1245 spin_unlock_bh(&mfc_unres_lock);
1249 /* MFC cache manipulation by user space mroute daemon */
1251 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1253 struct net *net = read_pnet(&mrt->net);
1254 struct mfc_cache *c;
1256 /* The entries are added/deleted only under RTNL */
1258 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1259 mfc->mfcc_mcastgrp.s_addr, parent);
1263 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1264 list_del_rcu(&c->_c.list);
1265 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
1266 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1272 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1273 struct mfcctl *mfc, int mrtsock, int parent)
1275 struct mfc_cache *uc, *c;
1280 if (mfc->mfcc_parent >= MAXVIFS)
1283 /* The entries are added/deleted only under RTNL */
1285 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1286 mfc->mfcc_mcastgrp.s_addr, parent);
1289 write_lock_bh(&mrt_lock);
1290 c->_c.mfc_parent = mfc->mfcc_parent;
1291 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1293 c->_c.mfc_flags |= MFC_STATIC;
1294 write_unlock_bh(&mrt_lock);
1295 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1297 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1301 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1302 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1305 c = ipmr_cache_alloc();
1309 c->mfc_origin = mfc->mfcc_origin.s_addr;
1310 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1311 c->_c.mfc_parent = mfc->mfcc_parent;
1312 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1314 c->_c.mfc_flags |= MFC_STATIC;
1316 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
1319 pr_err("ipmr: rhtable insert error %d\n", ret);
1323 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
1324 /* Check to see if we resolved a queued list. If so we
1325 * need to send on the frames and tidy up.
1328 spin_lock_bh(&mfc_unres_lock);
1329 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1330 uc = (struct mfc_cache *)_uc;
1331 if (uc->mfc_origin == c->mfc_origin &&
1332 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1333 list_del(&_uc->list);
1334 atomic_dec(&mrt->cache_resolve_queue_len);
1339 if (list_empty(&mrt->mfc_unres_queue))
1340 del_timer(&mrt->ipmr_expire_timer);
1341 spin_unlock_bh(&mfc_unres_lock);
1344 ipmr_cache_resolve(net, mrt, uc, c);
1345 ipmr_cache_free(uc);
1347 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
1348 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1352 /* Close the multicast socket, and clear the vif tables etc */
1353 static void mroute_clean_tables(struct mr_table *mrt, bool all)
1355 struct net *net = read_pnet(&mrt->net);
1356 struct mr_mfc *c, *tmp;
1357 struct mfc_cache *cache;
1361 /* Shut down all active vif entries */
1362 for (i = 0; i < mrt->maxvif; i++) {
1363 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1365 vif_delete(mrt, i, 0, &list);
1367 unregister_netdevice_many(&list);
1369 /* Wipe the cache */
1370 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1371 if (!all && (c->mfc_flags & MFC_STATIC))
1373 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1374 list_del_rcu(&c->list);
1375 cache = (struct mfc_cache *)c;
1376 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
1378 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1379 ipmr_cache_put(cache);
1382 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1383 spin_lock_bh(&mfc_unres_lock);
1384 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1386 cache = (struct mfc_cache *)c;
1387 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1388 ipmr_destroy_unres(mrt, cache);
1390 spin_unlock_bh(&mfc_unres_lock);
1394 /* called from ip_ra_control(), before an RCU grace period,
1395 * we dont need to call synchronize_rcu() here
1397 static void mrtsock_destruct(struct sock *sk)
1399 struct net *net = sock_net(sk);
1400 struct mr_table *mrt;
1403 ipmr_for_each_table(mrt, net) {
1404 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1405 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1406 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1407 NETCONFA_MC_FORWARDING,
1408 NETCONFA_IFINDEX_ALL,
1409 net->ipv4.devconf_all);
1410 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1411 mroute_clean_tables(mrt, false);
1416 /* Socket options and virtual interface manipulation. The whole
1417 * virtual interface system is a complete heap, but unfortunately
1418 * that's how BSD mrouted happens to think. Maybe one day with a proper
1419 * MOSPF/PIM router set up we can clean this up.
1422 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1423 unsigned int optlen)
1425 struct net *net = sock_net(sk);
1426 int val, ret = 0, parent = 0;
1427 struct mr_table *mrt;
1432 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1434 if (sk->sk_type != SOCK_RAW ||
1435 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1440 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1445 if (optname != MRT_INIT) {
1446 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1447 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1455 if (optlen != sizeof(int)) {
1459 if (rtnl_dereference(mrt->mroute_sk)) {
1464 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1466 rcu_assign_pointer(mrt->mroute_sk, sk);
1467 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1468 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1469 NETCONFA_MC_FORWARDING,
1470 NETCONFA_IFINDEX_ALL,
1471 net->ipv4.devconf_all);
1475 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1478 ret = ip_ra_control(sk, 0, NULL);
1484 if (optlen != sizeof(vif)) {
1488 if (copy_from_user(&vif, optval, sizeof(vif))) {
1492 if (vif.vifc_vifi >= MAXVIFS) {
1496 if (optname == MRT_ADD_VIF) {
1497 ret = vif_add(net, mrt, &vif,
1498 sk == rtnl_dereference(mrt->mroute_sk));
1500 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1503 /* Manipulate the forwarding caches. These live
1504 * in a sort of kernel/user symbiosis.
1510 case MRT_ADD_MFC_PROXY:
1511 case MRT_DEL_MFC_PROXY:
1512 if (optlen != sizeof(mfc)) {
1516 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1521 parent = mfc.mfcc_parent;
1522 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1523 ret = ipmr_mfc_delete(mrt, &mfc, parent);
1525 ret = ipmr_mfc_add(net, mrt, &mfc,
1526 sk == rtnl_dereference(mrt->mroute_sk),
1529 /* Control PIM assert. */
1531 if (optlen != sizeof(val)) {
1535 if (get_user(val, (int __user *)optval)) {
1539 mrt->mroute_do_assert = val;
1542 if (!ipmr_pimsm_enabled()) {
1546 if (optlen != sizeof(val)) {
1550 if (get_user(val, (int __user *)optval)) {
1556 if (val != mrt->mroute_do_pim) {
1557 mrt->mroute_do_pim = val;
1558 mrt->mroute_do_assert = val;
1562 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1566 if (optlen != sizeof(uval)) {
1570 if (get_user(uval, (u32 __user *)optval)) {
1575 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1578 mrt = ipmr_new_table(net, uval);
1582 raw_sk(sk)->ipmr_table = uval;
1585 /* Spurious command, or MRT_VERSION which you cannot set. */
1594 /* Getsock opt support for the multicast routing system. */
1595 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1599 struct net *net = sock_net(sk);
1600 struct mr_table *mrt;
1602 if (sk->sk_type != SOCK_RAW ||
1603 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1606 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1615 if (!ipmr_pimsm_enabled())
1616 return -ENOPROTOOPT;
1617 val = mrt->mroute_do_pim;
1620 val = mrt->mroute_do_assert;
1623 return -ENOPROTOOPT;
1626 if (get_user(olr, optlen))
1628 olr = min_t(unsigned int, olr, sizeof(int));
1631 if (put_user(olr, optlen))
1633 if (copy_to_user(optval, &val, olr))
1638 /* The IP multicast ioctl support routines. */
1639 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1641 struct sioc_sg_req sr;
1642 struct sioc_vif_req vr;
1643 struct vif_device *vif;
1644 struct mfc_cache *c;
1645 struct net *net = sock_net(sk);
1646 struct mr_table *mrt;
1648 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1654 if (copy_from_user(&vr, arg, sizeof(vr)))
1656 if (vr.vifi >= mrt->maxvif)
1658 read_lock(&mrt_lock);
1659 vif = &mrt->vif_table[vr.vifi];
1660 if (VIF_EXISTS(mrt, vr.vifi)) {
1661 vr.icount = vif->pkt_in;
1662 vr.ocount = vif->pkt_out;
1663 vr.ibytes = vif->bytes_in;
1664 vr.obytes = vif->bytes_out;
1665 read_unlock(&mrt_lock);
1667 if (copy_to_user(arg, &vr, sizeof(vr)))
1671 read_unlock(&mrt_lock);
1672 return -EADDRNOTAVAIL;
1674 if (copy_from_user(&sr, arg, sizeof(sr)))
1678 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1680 sr.pktcnt = c->_c.mfc_un.res.pkt;
1681 sr.bytecnt = c->_c.mfc_un.res.bytes;
1682 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1685 if (copy_to_user(arg, &sr, sizeof(sr)))
1690 return -EADDRNOTAVAIL;
1692 return -ENOIOCTLCMD;
1696 #ifdef CONFIG_COMPAT
1697 struct compat_sioc_sg_req {
1700 compat_ulong_t pktcnt;
1701 compat_ulong_t bytecnt;
1702 compat_ulong_t wrong_if;
1705 struct compat_sioc_vif_req {
1706 vifi_t vifi; /* Which iface */
1707 compat_ulong_t icount;
1708 compat_ulong_t ocount;
1709 compat_ulong_t ibytes;
1710 compat_ulong_t obytes;
1713 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1715 struct compat_sioc_sg_req sr;
1716 struct compat_sioc_vif_req vr;
1717 struct vif_device *vif;
1718 struct mfc_cache *c;
1719 struct net *net = sock_net(sk);
1720 struct mr_table *mrt;
1722 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1728 if (copy_from_user(&vr, arg, sizeof(vr)))
1730 if (vr.vifi >= mrt->maxvif)
1732 read_lock(&mrt_lock);
1733 vif = &mrt->vif_table[vr.vifi];
1734 if (VIF_EXISTS(mrt, vr.vifi)) {
1735 vr.icount = vif->pkt_in;
1736 vr.ocount = vif->pkt_out;
1737 vr.ibytes = vif->bytes_in;
1738 vr.obytes = vif->bytes_out;
1739 read_unlock(&mrt_lock);
1741 if (copy_to_user(arg, &vr, sizeof(vr)))
1745 read_unlock(&mrt_lock);
1746 return -EADDRNOTAVAIL;
1748 if (copy_from_user(&sr, arg, sizeof(sr)))
1752 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1754 sr.pktcnt = c->_c.mfc_un.res.pkt;
1755 sr.bytecnt = c->_c.mfc_un.res.bytes;
1756 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1759 if (copy_to_user(arg, &sr, sizeof(sr)))
1764 return -EADDRNOTAVAIL;
1766 return -ENOIOCTLCMD;
1771 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1773 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1774 struct net *net = dev_net(dev);
1775 struct mr_table *mrt;
1776 struct vif_device *v;
1779 if (event != NETDEV_UNREGISTER)
1782 ipmr_for_each_table(mrt, net) {
1783 v = &mrt->vif_table[0];
1784 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1786 vif_delete(mrt, ct, 1, NULL);
1792 static struct notifier_block ip_mr_notifier = {
1793 .notifier_call = ipmr_device_event,
1796 /* Encapsulate a packet by attaching a valid IPIP header to it.
1797 * This avoids tunnel drivers and other mess and gives us the speed so
1798 * important for multicast video.
1800 static void ip_encap(struct net *net, struct sk_buff *skb,
1801 __be32 saddr, __be32 daddr)
1804 const struct iphdr *old_iph = ip_hdr(skb);
1806 skb_push(skb, sizeof(struct iphdr));
1807 skb->transport_header = skb->network_header;
1808 skb_reset_network_header(skb);
1812 iph->tos = old_iph->tos;
1813 iph->ttl = old_iph->ttl;
1817 iph->protocol = IPPROTO_IPIP;
1819 iph->tot_len = htons(skb->len);
1820 ip_select_ident(net, skb, NULL);
1823 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1827 static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1828 struct sk_buff *skb)
1830 struct ip_options *opt = &(IPCB(skb)->opt);
1832 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1833 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
1835 if (unlikely(opt->optlen))
1836 ip_forward_options(skb);
1838 return dst_output(net, sk, skb);
1841 #ifdef CONFIG_NET_SWITCHDEV
1842 static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1843 int in_vifi, int out_vifi)
1845 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1846 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1848 if (!skb->offload_mr_fwd_mark)
1850 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1852 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1853 &in_vif->dev_parent_id);
1856 static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1857 int in_vifi, int out_vifi)
1863 /* Processing handlers for ipmr_forward */
1865 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1866 int in_vifi, struct sk_buff *skb,
1867 struct mfc_cache *c, int vifi)
1869 const struct iphdr *iph = ip_hdr(skb);
1870 struct vif_device *vif = &mrt->vif_table[vifi];
1871 struct net_device *dev;
1879 if (vif->flags & VIFF_REGISTER) {
1881 vif->bytes_out += skb->len;
1882 vif->dev->stats.tx_bytes += skb->len;
1883 vif->dev->stats.tx_packets++;
1884 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1888 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1891 if (vif->flags & VIFF_TUNNEL) {
1892 rt = ip_route_output_ports(net, &fl4, NULL,
1893 vif->remote, vif->local,
1896 RT_TOS(iph->tos), vif->link);
1899 encap = sizeof(struct iphdr);
1901 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1904 RT_TOS(iph->tos), vif->link);
1911 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1912 /* Do not fragment multicasts. Alas, IPv4 does not
1913 * allow to send ICMP, so that packets will disappear
1916 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1921 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1923 if (skb_cow(skb, encap)) {
1929 vif->bytes_out += skb->len;
1932 skb_dst_set(skb, &rt->dst);
1933 ip_decrease_ttl(ip_hdr(skb));
1935 /* FIXME: forward and output firewalls used to be called here.
1936 * What do we do with netfilter? -- RR
1938 if (vif->flags & VIFF_TUNNEL) {
1939 ip_encap(net, skb, vif->local, vif->remote);
1940 /* FIXME: extra output firewall step used to be here. --RR */
1941 vif->dev->stats.tx_packets++;
1942 vif->dev->stats.tx_bytes += skb->len;
1945 IPCB(skb)->flags |= IPSKB_FORWARDED;
1947 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1948 * not only before forwarding, but after forwarding on all output
1949 * interfaces. It is clear, if mrouter runs a multicasting
1950 * program, it should receive packets not depending to what interface
1951 * program is joined.
1952 * If we will not make it, the program will have to join on all
1953 * interfaces. On the other hand, multihoming host (or router, but
1954 * not mrouter) cannot join to more than one interface - it will
1955 * result in receiving multiple packets.
1957 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1958 net, NULL, skb, skb->dev, dev,
1959 ipmr_forward_finish);
1966 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1970 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1971 if (mrt->vif_table[ct].dev == dev)
1977 /* "local" means that we should preserve one skb (for local delivery) */
1978 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1979 struct net_device *dev, struct sk_buff *skb,
1980 struct mfc_cache *c, int local)
1982 int true_vifi = ipmr_find_vif(mrt, dev);
1986 vif = c->_c.mfc_parent;
1987 c->_c.mfc_un.res.pkt++;
1988 c->_c.mfc_un.res.bytes += skb->len;
1989 c->_c.mfc_un.res.lastuse = jiffies;
1991 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1992 struct mfc_cache *cache_proxy;
1994 /* For an (*,G) entry, we only check that the incomming
1995 * interface is part of the static tree.
1997 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
1999 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
2003 /* Wrong interface: drop packet and (maybe) send PIM assert. */
2004 if (mrt->vif_table[vif].dev != dev) {
2005 if (rt_is_output_route(skb_rtable(skb))) {
2006 /* It is our own packet, looped back.
2007 * Very complicated situation...
2009 * The best workaround until routing daemons will be
2010 * fixed is not to redistribute packet, if it was
2011 * send through wrong interface. It means, that
2012 * multicast applications WILL NOT work for
2013 * (S,G), which have default multicast route pointing
2014 * to wrong oif. In any case, it is not a good
2015 * idea to use multicasting applications on router.
2020 c->_c.mfc_un.res.wrong_if++;
2022 if (true_vifi >= 0 && mrt->mroute_do_assert &&
2023 /* pimsm uses asserts, when switching from RPT to SPT,
2024 * so that we cannot check that packet arrived on an oif.
2025 * It is bad, but otherwise we would need to move pretty
2026 * large chunk of pimd to kernel. Ough... --ANK
2028 (mrt->mroute_do_pim ||
2029 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
2031 c->_c.mfc_un.res.last_assert +
2032 MFC_ASSERT_THRESH)) {
2033 c->_c.mfc_un.res.last_assert = jiffies;
2034 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
2040 mrt->vif_table[vif].pkt_in++;
2041 mrt->vif_table[vif].bytes_in += skb->len;
2043 /* Forward the frame */
2044 if (c->mfc_origin == htonl(INADDR_ANY) &&
2045 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
2046 if (true_vifi >= 0 &&
2047 true_vifi != c->_c.mfc_parent &&
2049 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
2050 /* It's an (*,*) entry and the packet is not coming from
2051 * the upstream: forward the packet to the upstream
2054 psend = c->_c.mfc_parent;
2059 for (ct = c->_c.mfc_un.res.maxvif - 1;
2060 ct >= c->_c.mfc_un.res.minvif; ct--) {
2061 /* For (*,G) entry, don't forward to the incoming interface */
2062 if ((c->mfc_origin != htonl(INADDR_ANY) ||
2064 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
2066 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2069 ipmr_queue_xmit(net, mrt, true_vifi,
2078 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2081 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
2084 ipmr_queue_xmit(net, mrt, true_vifi, skb, c, psend);
2094 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
2096 struct rtable *rt = skb_rtable(skb);
2097 struct iphdr *iph = ip_hdr(skb);
2098 struct flowi4 fl4 = {
2099 .daddr = iph->daddr,
2100 .saddr = iph->saddr,
2101 .flowi4_tos = RT_TOS(iph->tos),
2102 .flowi4_oif = (rt_is_output_route(rt) ?
2103 skb->dev->ifindex : 0),
2104 .flowi4_iif = (rt_is_output_route(rt) ?
2107 .flowi4_mark = skb->mark,
2109 struct mr_table *mrt;
2112 err = ipmr_fib_lookup(net, &fl4, &mrt);
2114 return ERR_PTR(err);
2118 /* Multicast packets for forwarding arrive here
2119 * Called with rcu_read_lock();
2121 int ip_mr_input(struct sk_buff *skb)
2123 struct mfc_cache *cache;
2124 struct net *net = dev_net(skb->dev);
2125 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
2126 struct mr_table *mrt;
2127 struct net_device *dev;
2129 /* skb->dev passed in is the loX master dev for vrfs.
2130 * As there are no vifs associated with loopback devices,
2131 * get the proper interface that does have a vif associated with it.
2134 if (netif_is_l3_master(skb->dev)) {
2135 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2142 /* Packet is looped back after forward, it should not be
2143 * forwarded second time, but still can be delivered locally.
2145 if (IPCB(skb)->flags & IPSKB_FORWARDED)
2148 mrt = ipmr_rt_fib_lookup(net, skb);
2151 return PTR_ERR(mrt);
2154 if (IPCB(skb)->opt.router_alert) {
2155 if (ip_call_ra_chain(skb))
2157 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2158 /* IGMPv1 (and broken IGMPv2 implementations sort of
2159 * Cisco IOS <= 11.2(8)) do not put router alert
2160 * option to IGMP packets destined to routable
2161 * groups. It is very bad, because it means
2162 * that we can forward NO IGMP messages.
2164 struct sock *mroute_sk;
2166 mroute_sk = rcu_dereference(mrt->mroute_sk);
2169 raw_rcv(mroute_sk, skb);
2175 /* already under rcu_read_lock() */
2176 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
2178 int vif = ipmr_find_vif(mrt, dev);
2181 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2185 /* No usable cache entry */
2190 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2191 ip_local_deliver(skb);
2197 read_lock(&mrt_lock);
2198 vif = ipmr_find_vif(mrt, dev);
2200 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
2201 read_unlock(&mrt_lock);
2205 read_unlock(&mrt_lock);
2210 read_lock(&mrt_lock);
2211 ip_mr_forward(net, mrt, dev, skb, cache, local);
2212 read_unlock(&mrt_lock);
2215 return ip_local_deliver(skb);
2221 return ip_local_deliver(skb);
2226 #ifdef CONFIG_IP_PIMSM_V1
2227 /* Handle IGMP messages of PIMv1 */
2228 int pim_rcv_v1(struct sk_buff *skb)
2230 struct igmphdr *pim;
2231 struct net *net = dev_net(skb->dev);
2232 struct mr_table *mrt;
2234 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2237 pim = igmp_hdr(skb);
2239 mrt = ipmr_rt_fib_lookup(net, skb);
2242 if (!mrt->mroute_do_pim ||
2243 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2246 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2254 #ifdef CONFIG_IP_PIMSM_V2
2255 static int pim_rcv(struct sk_buff *skb)
2257 struct pimreghdr *pim;
2258 struct net *net = dev_net(skb->dev);
2259 struct mr_table *mrt;
2261 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2264 pim = (struct pimreghdr *)skb_transport_header(skb);
2265 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
2266 (pim->flags & PIM_NULL_REGISTER) ||
2267 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2268 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2271 mrt = ipmr_rt_fib_lookup(net, skb);
2274 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2282 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2283 __be32 saddr, __be32 daddr,
2284 struct rtmsg *rtm, u32 portid)
2286 struct mfc_cache *cache;
2287 struct mr_table *mrt;
2290 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2295 cache = ipmr_cache_find(mrt, saddr, daddr);
2296 if (!cache && skb->dev) {
2297 int vif = ipmr_find_vif(mrt, skb->dev);
2300 cache = ipmr_cache_find_any(mrt, daddr, vif);
2303 struct sk_buff *skb2;
2305 struct net_device *dev;
2309 read_lock(&mrt_lock);
2311 vif = ipmr_find_vif(mrt, dev);
2313 read_unlock(&mrt_lock);
2317 skb2 = skb_clone(skb, GFP_ATOMIC);
2319 read_unlock(&mrt_lock);
2324 NETLINK_CB(skb2).portid = portid;
2325 skb_push(skb2, sizeof(struct iphdr));
2326 skb_reset_network_header(skb2);
2328 iph->ihl = sizeof(struct iphdr) >> 2;
2332 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
2333 read_unlock(&mrt_lock);
2338 read_lock(&mrt_lock);
2339 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
2340 read_unlock(&mrt_lock);
2345 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2346 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2349 struct nlmsghdr *nlh;
2353 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2357 rtm = nlmsg_data(nlh);
2358 rtm->rtm_family = RTNL_FAMILY_IPMR;
2359 rtm->rtm_dst_len = 32;
2360 rtm->rtm_src_len = 32;
2362 rtm->rtm_table = mrt->id;
2363 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2364 goto nla_put_failure;
2365 rtm->rtm_type = RTN_MULTICAST;
2366 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2367 if (c->_c.mfc_flags & MFC_STATIC)
2368 rtm->rtm_protocol = RTPROT_STATIC;
2370 rtm->rtm_protocol = RTPROT_MROUTED;
2373 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2374 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2375 goto nla_put_failure;
2376 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
2377 /* do not break the dump if cache is unresolved */
2378 if (err < 0 && err != -ENOENT)
2379 goto nla_put_failure;
2381 nlmsg_end(skb, nlh);
2385 nlmsg_cancel(skb, nlh);
2389 static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2390 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2393 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2397 static size_t mroute_msgsize(bool unresolved, int maxvif)
2400 NLMSG_ALIGN(sizeof(struct rtmsg))
2401 + nla_total_size(4) /* RTA_TABLE */
2402 + nla_total_size(4) /* RTA_SRC */
2403 + nla_total_size(4) /* RTA_DST */
2408 + nla_total_size(4) /* RTA_IIF */
2409 + nla_total_size(0) /* RTA_MULTIPATH */
2410 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2412 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
2418 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2421 struct net *net = read_pnet(&mrt->net);
2422 struct sk_buff *skb;
2425 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2431 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2435 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2441 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2444 static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2447 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2448 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2449 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2450 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2451 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2452 /* IPMRA_CREPORT_PKT */
2453 + nla_total_size(payloadlen)
2459 static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2461 struct net *net = read_pnet(&mrt->net);
2462 struct nlmsghdr *nlh;
2463 struct rtgenmsg *rtgenm;
2464 struct igmpmsg *msg;
2465 struct sk_buff *skb;
2469 payloadlen = pkt->len - sizeof(struct igmpmsg);
2470 msg = (struct igmpmsg *)skb_network_header(pkt);
2472 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2476 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2477 sizeof(struct rtgenmsg), 0);
2480 rtgenm = nlmsg_data(nlh);
2481 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2482 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2483 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2484 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2485 msg->im_src.s_addr) ||
2486 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2487 msg->im_dst.s_addr))
2488 goto nla_put_failure;
2490 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2491 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2492 nla_data(nla), payloadlen))
2493 goto nla_put_failure;
2495 nlmsg_end(skb, nlh);
2497 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2501 nlmsg_cancel(skb, nlh);
2504 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2507 static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2508 struct netlink_ext_ack *extack)
2510 struct net *net = sock_net(in_skb->sk);
2511 struct nlattr *tb[RTA_MAX + 1];
2512 struct sk_buff *skb = NULL;
2513 struct mfc_cache *cache;
2514 struct mr_table *mrt;
2520 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2521 rtm_ipv4_policy, extack);
2525 rtm = nlmsg_data(nlh);
2527 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2528 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2529 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2531 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
2537 /* entries are added/deleted only under RTNL */
2539 cache = ipmr_cache_find(mrt, src, grp);
2546 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2552 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2553 nlh->nlmsg_seq, cache,
2558 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2568 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2570 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
2571 _ipmr_fill_mroute, &mfc_unres_lock);
2574 static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2575 [RTA_SRC] = { .type = NLA_U32 },
2576 [RTA_DST] = { .type = NLA_U32 },
2577 [RTA_IIF] = { .type = NLA_U32 },
2578 [RTA_TABLE] = { .type = NLA_U32 },
2579 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2582 static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2584 switch (rtm_protocol) {
2586 case RTPROT_MROUTED:
2592 static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2594 struct rtnexthop *rtnh = nla_data(nla);
2595 int remaining = nla_len(nla), vifi = 0;
2597 while (rtnh_ok(rtnh, remaining)) {
2598 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2599 if (++vifi == MAXVIFS)
2601 rtnh = rtnh_next(rtnh, &remaining);
2604 return remaining > 0 ? -EINVAL : vifi;
2607 /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2608 static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2609 struct mfcctl *mfcc, int *mrtsock,
2610 struct mr_table **mrtret,
2611 struct netlink_ext_ack *extack)
2613 struct net_device *dev = NULL;
2614 u32 tblid = RT_TABLE_DEFAULT;
2615 struct mr_table *mrt;
2616 struct nlattr *attr;
2620 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
2624 rtm = nlmsg_data(nlh);
2627 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2628 rtm->rtm_type != RTN_MULTICAST ||
2629 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2630 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2633 memset(mfcc, 0, sizeof(*mfcc));
2634 mfcc->mfcc_parent = -1;
2636 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2637 switch (nla_type(attr)) {
2639 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2642 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2645 dev = __dev_get_by_index(net, nla_get_u32(attr));
2652 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2661 tblid = nla_get_u32(attr);
2665 mrt = ipmr_get_table(net, tblid);
2671 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2673 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2679 /* takes care of both newroute and delroute */
2680 static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2681 struct netlink_ext_ack *extack)
2683 struct net *net = sock_net(skb->sk);
2684 int ret, mrtsock, parent;
2685 struct mr_table *tbl;
2690 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
2694 parent = ret ? mfcc.mfcc_parent : -1;
2695 if (nlh->nlmsg_type == RTM_NEWROUTE)
2696 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2698 return ipmr_mfc_delete(tbl, &mfcc, parent);
2701 static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2703 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2705 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2706 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2707 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2708 mrt->mroute_reg_vif_num) ||
2709 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2710 mrt->mroute_do_assert) ||
2711 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim))
2717 static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2719 struct nlattr *vif_nest;
2720 struct vif_device *vif;
2722 /* if the VIF doesn't exist just continue */
2723 if (!VIF_EXISTS(mrt, vifid))
2726 vif = &mrt->vif_table[vifid];
2727 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2730 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2731 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2732 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2733 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2735 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2737 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2739 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2741 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2742 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2743 nla_nest_cancel(skb, vif_nest);
2746 nla_nest_end(skb, vif_nest);
2751 static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2753 struct net *net = sock_net(skb->sk);
2754 struct nlmsghdr *nlh = NULL;
2755 unsigned int t = 0, s_t;
2756 unsigned int e = 0, s_e;
2757 struct mr_table *mrt;
2762 ipmr_for_each_table(mrt, net) {
2763 struct nlattr *vifs, *af;
2764 struct ifinfomsg *hdr;
2769 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2770 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2771 sizeof(*hdr), NLM_F_MULTI);
2775 hdr = nlmsg_data(nlh);
2776 memset(hdr, 0, sizeof(*hdr));
2777 hdr->ifi_family = RTNL_FAMILY_IPMR;
2779 af = nla_nest_start(skb, IFLA_AF_SPEC);
2781 nlmsg_cancel(skb, nlh);
2785 if (!ipmr_fill_table(mrt, skb)) {
2786 nlmsg_cancel(skb, nlh);
2790 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2792 nla_nest_end(skb, af);
2793 nlmsg_end(skb, nlh);
2796 for (i = 0; i < mrt->maxvif; i++) {
2799 if (!ipmr_fill_vif(mrt, i, skb)) {
2800 nla_nest_end(skb, vifs);
2801 nla_nest_end(skb, af);
2802 nlmsg_end(skb, nlh);
2810 nla_nest_end(skb, vifs);
2811 nla_nest_end(skb, af);
2812 nlmsg_end(skb, nlh);
2824 #ifdef CONFIG_PROC_FS
2825 /* The /proc interfaces to multicast routing :
2826 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2829 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2830 __acquires(mrt_lock)
2832 struct mr_vif_iter *iter = seq->private;
2833 struct net *net = seq_file_net(seq);
2834 struct mr_table *mrt;
2836 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2838 return ERR_PTR(-ENOENT);
2842 read_lock(&mrt_lock);
2843 return mr_vif_seq_start(seq, pos);
2846 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2847 __releases(mrt_lock)
2849 read_unlock(&mrt_lock);
2852 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2854 struct mr_vif_iter *iter = seq->private;
2855 struct mr_table *mrt = iter->mrt;
2857 if (v == SEQ_START_TOKEN) {
2859 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2861 const struct vif_device *vif = v;
2862 const char *name = vif->dev ?
2863 vif->dev->name : "none";
2866 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2867 vif - mrt->vif_table,
2868 name, vif->bytes_in, vif->pkt_in,
2869 vif->bytes_out, vif->pkt_out,
2870 vif->flags, vif->local, vif->remote);
2875 static const struct seq_operations ipmr_vif_seq_ops = {
2876 .start = ipmr_vif_seq_start,
2877 .next = mr_vif_seq_next,
2878 .stop = ipmr_vif_seq_stop,
2879 .show = ipmr_vif_seq_show,
2882 static int ipmr_vif_open(struct inode *inode, struct file *file)
2884 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2885 sizeof(struct mr_vif_iter));
2888 static const struct file_operations ipmr_vif_fops = {
2889 .open = ipmr_vif_open,
2891 .llseek = seq_lseek,
2892 .release = seq_release_net,
2895 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2897 struct net *net = seq_file_net(seq);
2898 struct mr_table *mrt;
2900 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2902 return ERR_PTR(-ENOENT);
2904 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
2907 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2911 if (v == SEQ_START_TOKEN) {
2913 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2915 const struct mfc_cache *mfc = v;
2916 const struct mr_mfc_iter *it = seq->private;
2917 const struct mr_table *mrt = it->mrt;
2919 seq_printf(seq, "%08X %08X %-3hd",
2920 (__force u32) mfc->mfc_mcastgrp,
2921 (__force u32) mfc->mfc_origin,
2922 mfc->_c.mfc_parent);
2924 if (it->cache != &mrt->mfc_unres_queue) {
2925 seq_printf(seq, " %8lu %8lu %8lu",
2926 mfc->_c.mfc_un.res.pkt,
2927 mfc->_c.mfc_un.res.bytes,
2928 mfc->_c.mfc_un.res.wrong_if);
2929 for (n = mfc->_c.mfc_un.res.minvif;
2930 n < mfc->_c.mfc_un.res.maxvif; n++) {
2931 if (VIF_EXISTS(mrt, n) &&
2932 mfc->_c.mfc_un.res.ttls[n] < 255)
2935 n, mfc->_c.mfc_un.res.ttls[n]);
2938 /* unresolved mfc_caches don't contain
2939 * pkt, bytes and wrong_if values
2941 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2943 seq_putc(seq, '\n');
2948 static const struct seq_operations ipmr_mfc_seq_ops = {
2949 .start = ipmr_mfc_seq_start,
2950 .next = mr_mfc_seq_next,
2951 .stop = mr_mfc_seq_stop,
2952 .show = ipmr_mfc_seq_show,
2955 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2957 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2958 sizeof(struct mr_mfc_iter));
2961 static const struct file_operations ipmr_mfc_fops = {
2962 .open = ipmr_mfc_open,
2964 .llseek = seq_lseek,
2965 .release = seq_release_net,
2969 #ifdef CONFIG_IP_PIMSM_V2
2970 static const struct net_protocol pim_protocol = {
2976 static unsigned int ipmr_seq_read(struct net *net)
2980 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
2983 static int ipmr_dump(struct net *net, struct notifier_block *nb)
2985 struct mr_table *mrt;
2988 err = ipmr_rules_dump(net, nb);
2992 ipmr_for_each_table(mrt, net) {
2993 struct vif_device *v = &mrt->vif_table[0];
2997 /* Notifiy on table VIF entries */
2998 read_lock(&mrt_lock);
2999 for (vifi = 0; vifi < mrt->maxvif; vifi++, v++) {
3003 call_ipmr_vif_entry_notifier(nb, net, FIB_EVENT_VIF_ADD,
3006 read_unlock(&mrt_lock);
3008 /* Notify on table MFC entries */
3009 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
3010 call_ipmr_mfc_entry_notifier(nb, net,
3011 FIB_EVENT_ENTRY_ADD,
3012 (struct mfc_cache *)mfc,
3019 static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3020 .family = RTNL_FAMILY_IPMR,
3021 .fib_seq_read = ipmr_seq_read,
3022 .fib_dump = ipmr_dump,
3023 .owner = THIS_MODULE,
3026 static int __net_init ipmr_notifier_init(struct net *net)
3028 struct fib_notifier_ops *ops;
3030 net->ipv4.ipmr_seq = 0;
3032 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3034 return PTR_ERR(ops);
3035 net->ipv4.ipmr_notifier_ops = ops;
3040 static void __net_exit ipmr_notifier_exit(struct net *net)
3042 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3043 net->ipv4.ipmr_notifier_ops = NULL;
3046 /* Setup for IP multicast routing */
3047 static int __net_init ipmr_net_init(struct net *net)
3051 err = ipmr_notifier_init(net);
3053 goto ipmr_notifier_fail;
3055 err = ipmr_rules_init(net);
3057 goto ipmr_rules_fail;
3059 #ifdef CONFIG_PROC_FS
3061 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
3063 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
3064 goto proc_cache_fail;
3068 #ifdef CONFIG_PROC_FS
3070 remove_proc_entry("ip_mr_vif", net->proc_net);
3072 ipmr_rules_exit(net);
3075 ipmr_notifier_exit(net);
3080 static void __net_exit ipmr_net_exit(struct net *net)
3082 #ifdef CONFIG_PROC_FS
3083 remove_proc_entry("ip_mr_cache", net->proc_net);
3084 remove_proc_entry("ip_mr_vif", net->proc_net);
3086 ipmr_notifier_exit(net);
3087 ipmr_rules_exit(net);
3090 static struct pernet_operations ipmr_net_ops = {
3091 .init = ipmr_net_init,
3092 .exit = ipmr_net_exit,
3096 int __init ip_mr_init(void)
3100 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3101 sizeof(struct mfc_cache),
3102 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
3105 err = register_pernet_subsys(&ipmr_net_ops);
3107 goto reg_pernet_fail;
3109 err = register_netdevice_notifier(&ip_mr_notifier);
3111 goto reg_notif_fail;
3112 #ifdef CONFIG_IP_PIMSM_V2
3113 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
3114 pr_err("%s: can't add PIM protocol\n", __func__);
3116 goto add_proto_fail;
3119 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
3120 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
3121 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
3122 ipmr_rtm_route, NULL, 0);
3123 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
3124 ipmr_rtm_route, NULL, 0);
3126 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
3127 NULL, ipmr_rtm_dumplink, 0);
3130 #ifdef CONFIG_IP_PIMSM_V2
3132 unregister_netdevice_notifier(&ip_mr_notifier);
3135 unregister_pernet_subsys(&ipmr_net_ops);
3137 kmem_cache_destroy(mrt_cachep);