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_notifiers(struct net *net,
648 enum fib_event_type event_type,
649 struct vif_device *vif,
650 vifi_t vif_index, u32 tb_id)
652 return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
653 vif, vif_index, tb_id,
654 &net->ipv4.ipmr_seq);
657 static int call_ipmr_mfc_entry_notifiers(struct net *net,
658 enum fib_event_type event_type,
659 struct mfc_cache *mfc, u32 tb_id)
661 return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
662 &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
666 * vif_delete - Delete a VIF entry
667 * @notify: Set to 1, if the caller is a notifier_call
669 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
670 struct list_head *head)
672 struct net *net = read_pnet(&mrt->net);
673 struct vif_device *v;
674 struct net_device *dev;
675 struct in_device *in_dev;
677 if (vifi < 0 || vifi >= mrt->maxvif)
678 return -EADDRNOTAVAIL;
680 v = &mrt->vif_table[vifi];
682 if (VIF_EXISTS(mrt, vifi))
683 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
686 write_lock_bh(&mrt_lock);
691 write_unlock_bh(&mrt_lock);
692 return -EADDRNOTAVAIL;
695 if (vifi == mrt->mroute_reg_vif_num)
696 mrt->mroute_reg_vif_num = -1;
698 if (vifi + 1 == mrt->maxvif) {
701 for (tmp = vifi - 1; tmp >= 0; tmp--) {
702 if (VIF_EXISTS(mrt, tmp))
708 write_unlock_bh(&mrt_lock);
710 dev_set_allmulti(dev, -1);
712 in_dev = __in_dev_get_rtnl(dev);
714 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
715 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
716 NETCONFA_MC_FORWARDING,
717 dev->ifindex, &in_dev->cnf);
718 ip_rt_multicast_event(in_dev);
721 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
722 unregister_netdevice_queue(dev, head);
728 static void ipmr_cache_free_rcu(struct rcu_head *head)
730 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
732 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
735 static void ipmr_cache_free(struct mfc_cache *c)
737 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
740 /* Destroy an unresolved cache entry, killing queued skbs
741 * and reporting error to netlink readers.
743 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
745 struct net *net = read_pnet(&mrt->net);
749 atomic_dec(&mrt->cache_resolve_queue_len);
751 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
752 if (ip_hdr(skb)->version == 0) {
753 struct nlmsghdr *nlh = skb_pull(skb,
754 sizeof(struct iphdr));
755 nlh->nlmsg_type = NLMSG_ERROR;
756 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
757 skb_trim(skb, nlh->nlmsg_len);
759 e->error = -ETIMEDOUT;
760 memset(&e->msg, 0, sizeof(e->msg));
762 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
771 /* Timer process for the unresolved queue. */
772 static void ipmr_expire_process(struct timer_list *t)
774 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
775 struct mr_mfc *c, *next;
776 unsigned long expires;
779 if (!spin_trylock(&mfc_unres_lock)) {
780 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
784 if (list_empty(&mrt->mfc_unres_queue))
790 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
791 if (time_after(c->mfc_un.unres.expires, now)) {
792 unsigned long interval = c->mfc_un.unres.expires - now;
793 if (interval < expires)
799 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
800 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
803 if (!list_empty(&mrt->mfc_unres_queue))
804 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
807 spin_unlock(&mfc_unres_lock);
810 /* Fill oifs list. It is called under write locked mrt_lock. */
811 static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
816 cache->mfc_un.res.minvif = MAXVIFS;
817 cache->mfc_un.res.maxvif = 0;
818 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
820 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
821 if (VIF_EXISTS(mrt, vifi) &&
822 ttls[vifi] && ttls[vifi] < 255) {
823 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
824 if (cache->mfc_un.res.minvif > vifi)
825 cache->mfc_un.res.minvif = vifi;
826 if (cache->mfc_un.res.maxvif <= vifi)
827 cache->mfc_un.res.maxvif = vifi + 1;
830 cache->mfc_un.res.lastuse = jiffies;
833 static int vif_add(struct net *net, struct mr_table *mrt,
834 struct vifctl *vifc, int mrtsock)
836 int vifi = vifc->vifc_vifi;
837 struct switchdev_attr attr = {
838 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
840 struct vif_device *v = &mrt->vif_table[vifi];
841 struct net_device *dev;
842 struct in_device *in_dev;
846 if (VIF_EXISTS(mrt, vifi))
849 switch (vifc->vifc_flags) {
851 if (!ipmr_pimsm_enabled())
853 /* Special Purpose VIF in PIM
854 * All the packets will be sent to the daemon
856 if (mrt->mroute_reg_vif_num >= 0)
858 dev = ipmr_reg_vif(net, mrt);
861 err = dev_set_allmulti(dev, 1);
863 unregister_netdevice(dev);
869 dev = ipmr_new_tunnel(net, vifc);
872 err = dev_set_allmulti(dev, 1);
874 ipmr_del_tunnel(dev, vifc);
879 case VIFF_USE_IFINDEX:
881 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
882 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
883 if (dev && !__in_dev_get_rtnl(dev)) {
885 return -EADDRNOTAVAIL;
888 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
891 return -EADDRNOTAVAIL;
892 err = dev_set_allmulti(dev, 1);
902 in_dev = __in_dev_get_rtnl(dev);
905 return -EADDRNOTAVAIL;
907 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
908 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
909 dev->ifindex, &in_dev->cnf);
910 ip_rt_multicast_event(in_dev);
912 /* Fill in the VIF structures */
913 vif_device_init(v, dev, vifc->vifc_rate_limit,
914 vifc->vifc_threshold,
915 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
916 (VIFF_TUNNEL | VIFF_REGISTER));
919 if (!switchdev_port_attr_get(dev, &attr)) {
920 memcpy(v->dev_parent_id.id, attr.u.ppid.id, attr.u.ppid.id_len);
921 v->dev_parent_id.id_len = attr.u.ppid.id_len;
923 v->dev_parent_id.id_len = 0;
926 v->local = vifc->vifc_lcl_addr.s_addr;
927 v->remote = vifc->vifc_rmt_addr.s_addr;
929 /* And finish update writing critical data */
930 write_lock_bh(&mrt_lock);
932 if (v->flags & VIFF_REGISTER)
933 mrt->mroute_reg_vif_num = vifi;
934 if (vifi+1 > mrt->maxvif)
935 mrt->maxvif = vifi+1;
936 write_unlock_bh(&mrt_lock);
937 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
941 /* called with rcu_read_lock() */
942 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
946 struct mfc_cache_cmp_arg arg = {
947 .mfc_mcastgrp = mcastgrp,
951 return mr_mfc_find(mrt, &arg);
954 /* Look for a (*,G) entry */
955 static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
956 __be32 mcastgrp, int vifi)
958 struct mfc_cache_cmp_arg arg = {
959 .mfc_mcastgrp = mcastgrp,
960 .mfc_origin = htonl(INADDR_ANY)
963 if (mcastgrp == htonl(INADDR_ANY))
964 return mr_mfc_find_any_parent(mrt, vifi);
965 return mr_mfc_find_any(mrt, vifi, &arg);
968 /* Look for a (S,G,iif) entry if parent != -1 */
969 static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
970 __be32 origin, __be32 mcastgrp,
973 struct mfc_cache_cmp_arg arg = {
974 .mfc_mcastgrp = mcastgrp,
975 .mfc_origin = origin,
978 return mr_mfc_find_parent(mrt, &arg, parent);
981 /* Allocate a multicast cache entry */
982 static struct mfc_cache *ipmr_cache_alloc(void)
984 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
987 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
988 c->_c.mfc_un.res.minvif = MAXVIFS;
989 c->_c.free = ipmr_cache_free_rcu;
990 refcount_set(&c->_c.mfc_un.res.refcount, 1);
995 static struct mfc_cache *ipmr_cache_alloc_unres(void)
997 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
1000 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
1001 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
1006 /* A cache entry has gone into a resolved state from queued */
1007 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1008 struct mfc_cache *uc, struct mfc_cache *c)
1010 struct sk_buff *skb;
1013 /* Play the pending entries through our router */
1014 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
1015 if (ip_hdr(skb)->version == 0) {
1016 struct nlmsghdr *nlh = skb_pull(skb,
1017 sizeof(struct iphdr));
1019 if (mr_fill_mroute(mrt, skb, &c->_c,
1020 nlmsg_data(nlh)) > 0) {
1021 nlh->nlmsg_len = skb_tail_pointer(skb) -
1024 nlh->nlmsg_type = NLMSG_ERROR;
1025 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
1026 skb_trim(skb, nlh->nlmsg_len);
1027 e = nlmsg_data(nlh);
1028 e->error = -EMSGSIZE;
1029 memset(&e->msg, 0, sizeof(e->msg));
1032 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
1034 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
1039 /* Bounce a cache query up to mrouted and netlink.
1041 * Called under mrt_lock.
1043 static int ipmr_cache_report(struct mr_table *mrt,
1044 struct sk_buff *pkt, vifi_t vifi, int assert)
1046 const int ihl = ip_hdrlen(pkt);
1047 struct sock *mroute_sk;
1048 struct igmphdr *igmp;
1049 struct igmpmsg *msg;
1050 struct sk_buff *skb;
1053 if (assert == IGMPMSG_WHOLEPKT)
1054 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1056 skb = alloc_skb(128, GFP_ATOMIC);
1061 if (assert == IGMPMSG_WHOLEPKT) {
1062 /* Ugly, but we have no choice with this interface.
1063 * Duplicate old header, fix ihl, length etc.
1064 * And all this only to mangle msg->im_msgtype and
1065 * to set msg->im_mbz to "mbz" :-)
1067 skb_push(skb, sizeof(struct iphdr));
1068 skb_reset_network_header(skb);
1069 skb_reset_transport_header(skb);
1070 msg = (struct igmpmsg *)skb_network_header(skb);
1071 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
1072 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1074 msg->im_vif = mrt->mroute_reg_vif_num;
1075 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1076 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1077 sizeof(struct iphdr));
1079 /* Copy the IP header */
1080 skb_set_network_header(skb, skb->len);
1082 skb_copy_to_linear_data(skb, pkt->data, ihl);
1083 /* Flag to the kernel this is a route add */
1084 ip_hdr(skb)->protocol = 0;
1085 msg = (struct igmpmsg *)skb_network_header(skb);
1087 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1088 /* Add our header */
1089 igmp = skb_put(skb, sizeof(struct igmphdr));
1090 igmp->type = assert;
1091 msg->im_msgtype = assert;
1093 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1094 skb->transport_header = skb->network_header;
1098 mroute_sk = rcu_dereference(mrt->mroute_sk);
1105 igmpmsg_netlink_event(mrt, skb);
1107 /* Deliver to mrouted */
1108 ret = sock_queue_rcv_skb(mroute_sk, skb);
1111 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1118 /* Queue a packet for resolution. It gets locked cache entry! */
1119 static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1120 struct sk_buff *skb, struct net_device *dev)
1122 const struct iphdr *iph = ip_hdr(skb);
1123 struct mfc_cache *c;
1127 spin_lock_bh(&mfc_unres_lock);
1128 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
1129 if (c->mfc_mcastgrp == iph->daddr &&
1130 c->mfc_origin == iph->saddr) {
1137 /* Create a new entry if allowable */
1138 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1139 (c = ipmr_cache_alloc_unres()) == NULL) {
1140 spin_unlock_bh(&mfc_unres_lock);
1146 /* Fill in the new cache entry */
1147 c->_c.mfc_parent = -1;
1148 c->mfc_origin = iph->saddr;
1149 c->mfc_mcastgrp = iph->daddr;
1151 /* Reflect first query at mrouted. */
1152 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1155 /* If the report failed throw the cache entry
1158 spin_unlock_bh(&mfc_unres_lock);
1165 atomic_inc(&mrt->cache_resolve_queue_len);
1166 list_add(&c->_c.list, &mrt->mfc_unres_queue);
1167 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1169 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1170 mod_timer(&mrt->ipmr_expire_timer,
1171 c->_c.mfc_un.unres.expires);
1174 /* See if we can append the packet */
1175 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
1181 skb->skb_iif = dev->ifindex;
1183 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
1187 spin_unlock_bh(&mfc_unres_lock);
1191 /* MFC cache manipulation by user space mroute daemon */
1193 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1195 struct net *net = read_pnet(&mrt->net);
1196 struct mfc_cache *c;
1198 /* The entries are added/deleted only under RTNL */
1200 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1201 mfc->mfcc_mcastgrp.s_addr, parent);
1205 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1206 list_del_rcu(&c->_c.list);
1207 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
1208 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1209 mr_cache_put(&c->_c);
1214 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1215 struct mfcctl *mfc, int mrtsock, int parent)
1217 struct mfc_cache *uc, *c;
1222 if (mfc->mfcc_parent >= MAXVIFS)
1225 /* The entries are added/deleted only under RTNL */
1227 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1228 mfc->mfcc_mcastgrp.s_addr, parent);
1231 write_lock_bh(&mrt_lock);
1232 c->_c.mfc_parent = mfc->mfcc_parent;
1233 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1235 c->_c.mfc_flags |= MFC_STATIC;
1236 write_unlock_bh(&mrt_lock);
1237 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1239 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1243 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1244 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1247 c = ipmr_cache_alloc();
1251 c->mfc_origin = mfc->mfcc_origin.s_addr;
1252 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1253 c->_c.mfc_parent = mfc->mfcc_parent;
1254 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1256 c->_c.mfc_flags |= MFC_STATIC;
1258 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
1261 pr_err("ipmr: rhtable insert error %d\n", ret);
1265 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
1266 /* Check to see if we resolved a queued list. If so we
1267 * need to send on the frames and tidy up.
1270 spin_lock_bh(&mfc_unres_lock);
1271 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1272 uc = (struct mfc_cache *)_uc;
1273 if (uc->mfc_origin == c->mfc_origin &&
1274 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1275 list_del(&_uc->list);
1276 atomic_dec(&mrt->cache_resolve_queue_len);
1281 if (list_empty(&mrt->mfc_unres_queue))
1282 del_timer(&mrt->ipmr_expire_timer);
1283 spin_unlock_bh(&mfc_unres_lock);
1286 ipmr_cache_resolve(net, mrt, uc, c);
1287 ipmr_cache_free(uc);
1289 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
1290 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1294 /* Close the multicast socket, and clear the vif tables etc */
1295 static void mroute_clean_tables(struct mr_table *mrt, bool all)
1297 struct net *net = read_pnet(&mrt->net);
1298 struct mr_mfc *c, *tmp;
1299 struct mfc_cache *cache;
1303 /* Shut down all active vif entries */
1304 for (i = 0; i < mrt->maxvif; i++) {
1305 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1307 vif_delete(mrt, i, 0, &list);
1309 unregister_netdevice_many(&list);
1311 /* Wipe the cache */
1312 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1313 if (!all && (c->mfc_flags & MFC_STATIC))
1315 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1316 list_del_rcu(&c->list);
1317 cache = (struct mfc_cache *)c;
1318 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
1320 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1324 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1325 spin_lock_bh(&mfc_unres_lock);
1326 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1328 cache = (struct mfc_cache *)c;
1329 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1330 ipmr_destroy_unres(mrt, cache);
1332 spin_unlock_bh(&mfc_unres_lock);
1336 /* called from ip_ra_control(), before an RCU grace period,
1337 * we dont need to call synchronize_rcu() here
1339 static void mrtsock_destruct(struct sock *sk)
1341 struct net *net = sock_net(sk);
1342 struct mr_table *mrt;
1345 ipmr_for_each_table(mrt, net) {
1346 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1347 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1348 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1349 NETCONFA_MC_FORWARDING,
1350 NETCONFA_IFINDEX_ALL,
1351 net->ipv4.devconf_all);
1352 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1353 mroute_clean_tables(mrt, false);
1359 /* Socket options and virtual interface manipulation. The whole
1360 * virtual interface system is a complete heap, but unfortunately
1361 * that's how BSD mrouted happens to think. Maybe one day with a proper
1362 * MOSPF/PIM router set up we can clean this up.
1365 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1366 unsigned int optlen)
1368 struct net *net = sock_net(sk);
1369 int val, ret = 0, parent = 0;
1370 struct mr_table *mrt;
1375 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1377 if (sk->sk_type != SOCK_RAW ||
1378 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1383 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1388 if (optname != MRT_INIT) {
1389 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1390 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1398 if (optlen != sizeof(int)) {
1402 if (rtnl_dereference(mrt->mroute_sk)) {
1407 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1409 rcu_assign_pointer(mrt->mroute_sk, sk);
1410 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1411 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1412 NETCONFA_MC_FORWARDING,
1413 NETCONFA_IFINDEX_ALL,
1414 net->ipv4.devconf_all);
1418 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1421 /* We need to unlock here because mrtsock_destruct takes
1422 * care of rtnl itself and we can't change that due to
1423 * the IP_ROUTER_ALERT setsockopt which runs without it.
1426 ret = ip_ra_control(sk, 0, NULL);
1432 if (optlen != sizeof(vif)) {
1436 if (copy_from_user(&vif, optval, sizeof(vif))) {
1440 if (vif.vifc_vifi >= MAXVIFS) {
1444 if (optname == MRT_ADD_VIF) {
1445 ret = vif_add(net, mrt, &vif,
1446 sk == rtnl_dereference(mrt->mroute_sk));
1448 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1451 /* Manipulate the forwarding caches. These live
1452 * in a sort of kernel/user symbiosis.
1458 case MRT_ADD_MFC_PROXY:
1459 case MRT_DEL_MFC_PROXY:
1460 if (optlen != sizeof(mfc)) {
1464 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1469 parent = mfc.mfcc_parent;
1470 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1471 ret = ipmr_mfc_delete(mrt, &mfc, parent);
1473 ret = ipmr_mfc_add(net, mrt, &mfc,
1474 sk == rtnl_dereference(mrt->mroute_sk),
1477 /* Control PIM assert. */
1479 if (optlen != sizeof(val)) {
1483 if (get_user(val, (int __user *)optval)) {
1487 mrt->mroute_do_assert = val;
1490 if (!ipmr_pimsm_enabled()) {
1494 if (optlen != sizeof(val)) {
1498 if (get_user(val, (int __user *)optval)) {
1504 if (val != mrt->mroute_do_pim) {
1505 mrt->mroute_do_pim = val;
1506 mrt->mroute_do_assert = val;
1510 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1514 if (optlen != sizeof(uval)) {
1518 if (get_user(uval, (u32 __user *)optval)) {
1523 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1526 mrt = ipmr_new_table(net, uval);
1530 raw_sk(sk)->ipmr_table = uval;
1533 /* Spurious command, or MRT_VERSION which you cannot set. */
1543 /* Getsock opt support for the multicast routing system. */
1544 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1548 struct net *net = sock_net(sk);
1549 struct mr_table *mrt;
1551 if (sk->sk_type != SOCK_RAW ||
1552 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1555 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1564 if (!ipmr_pimsm_enabled())
1565 return -ENOPROTOOPT;
1566 val = mrt->mroute_do_pim;
1569 val = mrt->mroute_do_assert;
1572 return -ENOPROTOOPT;
1575 if (get_user(olr, optlen))
1577 olr = min_t(unsigned int, olr, sizeof(int));
1580 if (put_user(olr, optlen))
1582 if (copy_to_user(optval, &val, olr))
1587 /* The IP multicast ioctl support routines. */
1588 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1590 struct sioc_sg_req sr;
1591 struct sioc_vif_req vr;
1592 struct vif_device *vif;
1593 struct mfc_cache *c;
1594 struct net *net = sock_net(sk);
1595 struct mr_table *mrt;
1597 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1603 if (copy_from_user(&vr, arg, sizeof(vr)))
1605 if (vr.vifi >= mrt->maxvif)
1607 read_lock(&mrt_lock);
1608 vif = &mrt->vif_table[vr.vifi];
1609 if (VIF_EXISTS(mrt, vr.vifi)) {
1610 vr.icount = vif->pkt_in;
1611 vr.ocount = vif->pkt_out;
1612 vr.ibytes = vif->bytes_in;
1613 vr.obytes = vif->bytes_out;
1614 read_unlock(&mrt_lock);
1616 if (copy_to_user(arg, &vr, sizeof(vr)))
1620 read_unlock(&mrt_lock);
1621 return -EADDRNOTAVAIL;
1623 if (copy_from_user(&sr, arg, sizeof(sr)))
1627 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1629 sr.pktcnt = c->_c.mfc_un.res.pkt;
1630 sr.bytecnt = c->_c.mfc_un.res.bytes;
1631 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1634 if (copy_to_user(arg, &sr, sizeof(sr)))
1639 return -EADDRNOTAVAIL;
1641 return -ENOIOCTLCMD;
1645 #ifdef CONFIG_COMPAT
1646 struct compat_sioc_sg_req {
1649 compat_ulong_t pktcnt;
1650 compat_ulong_t bytecnt;
1651 compat_ulong_t wrong_if;
1654 struct compat_sioc_vif_req {
1655 vifi_t vifi; /* Which iface */
1656 compat_ulong_t icount;
1657 compat_ulong_t ocount;
1658 compat_ulong_t ibytes;
1659 compat_ulong_t obytes;
1662 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1664 struct compat_sioc_sg_req sr;
1665 struct compat_sioc_vif_req vr;
1666 struct vif_device *vif;
1667 struct mfc_cache *c;
1668 struct net *net = sock_net(sk);
1669 struct mr_table *mrt;
1671 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1677 if (copy_from_user(&vr, arg, sizeof(vr)))
1679 if (vr.vifi >= mrt->maxvif)
1681 read_lock(&mrt_lock);
1682 vif = &mrt->vif_table[vr.vifi];
1683 if (VIF_EXISTS(mrt, vr.vifi)) {
1684 vr.icount = vif->pkt_in;
1685 vr.ocount = vif->pkt_out;
1686 vr.ibytes = vif->bytes_in;
1687 vr.obytes = vif->bytes_out;
1688 read_unlock(&mrt_lock);
1690 if (copy_to_user(arg, &vr, sizeof(vr)))
1694 read_unlock(&mrt_lock);
1695 return -EADDRNOTAVAIL;
1697 if (copy_from_user(&sr, arg, sizeof(sr)))
1701 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1703 sr.pktcnt = c->_c.mfc_un.res.pkt;
1704 sr.bytecnt = c->_c.mfc_un.res.bytes;
1705 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1708 if (copy_to_user(arg, &sr, sizeof(sr)))
1713 return -EADDRNOTAVAIL;
1715 return -ENOIOCTLCMD;
1720 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1722 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1723 struct net *net = dev_net(dev);
1724 struct mr_table *mrt;
1725 struct vif_device *v;
1728 if (event != NETDEV_UNREGISTER)
1731 ipmr_for_each_table(mrt, net) {
1732 v = &mrt->vif_table[0];
1733 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1735 vif_delete(mrt, ct, 1, NULL);
1741 static struct notifier_block ip_mr_notifier = {
1742 .notifier_call = ipmr_device_event,
1745 /* Encapsulate a packet by attaching a valid IPIP header to it.
1746 * This avoids tunnel drivers and other mess and gives us the speed so
1747 * important for multicast video.
1749 static void ip_encap(struct net *net, struct sk_buff *skb,
1750 __be32 saddr, __be32 daddr)
1753 const struct iphdr *old_iph = ip_hdr(skb);
1755 skb_push(skb, sizeof(struct iphdr));
1756 skb->transport_header = skb->network_header;
1757 skb_reset_network_header(skb);
1761 iph->tos = old_iph->tos;
1762 iph->ttl = old_iph->ttl;
1766 iph->protocol = IPPROTO_IPIP;
1768 iph->tot_len = htons(skb->len);
1769 ip_select_ident(net, skb, NULL);
1772 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1776 static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1777 struct sk_buff *skb)
1779 struct ip_options *opt = &(IPCB(skb)->opt);
1781 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1782 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
1784 if (unlikely(opt->optlen))
1785 ip_forward_options(skb);
1787 return dst_output(net, sk, skb);
1790 #ifdef CONFIG_NET_SWITCHDEV
1791 static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1792 int in_vifi, int out_vifi)
1794 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1795 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1797 if (!skb->offload_mr_fwd_mark)
1799 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1801 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1802 &in_vif->dev_parent_id);
1805 static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1806 int in_vifi, int out_vifi)
1812 /* Processing handlers for ipmr_forward */
1814 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1815 int in_vifi, struct sk_buff *skb,
1816 struct mfc_cache *c, int vifi)
1818 const struct iphdr *iph = ip_hdr(skb);
1819 struct vif_device *vif = &mrt->vif_table[vifi];
1820 struct net_device *dev;
1828 if (vif->flags & VIFF_REGISTER) {
1830 vif->bytes_out += skb->len;
1831 vif->dev->stats.tx_bytes += skb->len;
1832 vif->dev->stats.tx_packets++;
1833 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1837 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1840 if (vif->flags & VIFF_TUNNEL) {
1841 rt = ip_route_output_ports(net, &fl4, NULL,
1842 vif->remote, vif->local,
1845 RT_TOS(iph->tos), vif->link);
1848 encap = sizeof(struct iphdr);
1850 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1853 RT_TOS(iph->tos), vif->link);
1860 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1861 /* Do not fragment multicasts. Alas, IPv4 does not
1862 * allow to send ICMP, so that packets will disappear
1865 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1870 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1872 if (skb_cow(skb, encap)) {
1878 vif->bytes_out += skb->len;
1881 skb_dst_set(skb, &rt->dst);
1882 ip_decrease_ttl(ip_hdr(skb));
1884 /* FIXME: forward and output firewalls used to be called here.
1885 * What do we do with netfilter? -- RR
1887 if (vif->flags & VIFF_TUNNEL) {
1888 ip_encap(net, skb, vif->local, vif->remote);
1889 /* FIXME: extra output firewall step used to be here. --RR */
1890 vif->dev->stats.tx_packets++;
1891 vif->dev->stats.tx_bytes += skb->len;
1894 IPCB(skb)->flags |= IPSKB_FORWARDED;
1896 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1897 * not only before forwarding, but after forwarding on all output
1898 * interfaces. It is clear, if mrouter runs a multicasting
1899 * program, it should receive packets not depending to what interface
1900 * program is joined.
1901 * If we will not make it, the program will have to join on all
1902 * interfaces. On the other hand, multihoming host (or router, but
1903 * not mrouter) cannot join to more than one interface - it will
1904 * result in receiving multiple packets.
1906 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1907 net, NULL, skb, skb->dev, dev,
1908 ipmr_forward_finish);
1915 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1919 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1920 if (mrt->vif_table[ct].dev == dev)
1926 /* "local" means that we should preserve one skb (for local delivery) */
1927 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1928 struct net_device *dev, struct sk_buff *skb,
1929 struct mfc_cache *c, int local)
1931 int true_vifi = ipmr_find_vif(mrt, dev);
1935 vif = c->_c.mfc_parent;
1936 c->_c.mfc_un.res.pkt++;
1937 c->_c.mfc_un.res.bytes += skb->len;
1938 c->_c.mfc_un.res.lastuse = jiffies;
1940 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1941 struct mfc_cache *cache_proxy;
1943 /* For an (*,G) entry, we only check that the incomming
1944 * interface is part of the static tree.
1946 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
1948 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
1952 /* Wrong interface: drop packet and (maybe) send PIM assert. */
1953 if (mrt->vif_table[vif].dev != dev) {
1954 if (rt_is_output_route(skb_rtable(skb))) {
1955 /* It is our own packet, looped back.
1956 * Very complicated situation...
1958 * The best workaround until routing daemons will be
1959 * fixed is not to redistribute packet, if it was
1960 * send through wrong interface. It means, that
1961 * multicast applications WILL NOT work for
1962 * (S,G), which have default multicast route pointing
1963 * to wrong oif. In any case, it is not a good
1964 * idea to use multicasting applications on router.
1969 c->_c.mfc_un.res.wrong_if++;
1971 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1972 /* pimsm uses asserts, when switching from RPT to SPT,
1973 * so that we cannot check that packet arrived on an oif.
1974 * It is bad, but otherwise we would need to move pretty
1975 * large chunk of pimd to kernel. Ough... --ANK
1977 (mrt->mroute_do_pim ||
1978 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
1980 c->_c.mfc_un.res.last_assert +
1981 MFC_ASSERT_THRESH)) {
1982 c->_c.mfc_un.res.last_assert = jiffies;
1983 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1989 mrt->vif_table[vif].pkt_in++;
1990 mrt->vif_table[vif].bytes_in += skb->len;
1992 /* Forward the frame */
1993 if (c->mfc_origin == htonl(INADDR_ANY) &&
1994 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
1995 if (true_vifi >= 0 &&
1996 true_vifi != c->_c.mfc_parent &&
1998 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
1999 /* It's an (*,*) entry and the packet is not coming from
2000 * the upstream: forward the packet to the upstream
2003 psend = c->_c.mfc_parent;
2008 for (ct = c->_c.mfc_un.res.maxvif - 1;
2009 ct >= c->_c.mfc_un.res.minvif; ct--) {
2010 /* For (*,G) entry, don't forward to the incoming interface */
2011 if ((c->mfc_origin != htonl(INADDR_ANY) ||
2013 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
2015 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2018 ipmr_queue_xmit(net, mrt, true_vifi,
2027 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2030 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
2033 ipmr_queue_xmit(net, mrt, true_vifi, skb, c, psend);
2043 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
2045 struct rtable *rt = skb_rtable(skb);
2046 struct iphdr *iph = ip_hdr(skb);
2047 struct flowi4 fl4 = {
2048 .daddr = iph->daddr,
2049 .saddr = iph->saddr,
2050 .flowi4_tos = RT_TOS(iph->tos),
2051 .flowi4_oif = (rt_is_output_route(rt) ?
2052 skb->dev->ifindex : 0),
2053 .flowi4_iif = (rt_is_output_route(rt) ?
2056 .flowi4_mark = skb->mark,
2058 struct mr_table *mrt;
2061 err = ipmr_fib_lookup(net, &fl4, &mrt);
2063 return ERR_PTR(err);
2067 /* Multicast packets for forwarding arrive here
2068 * Called with rcu_read_lock();
2070 int ip_mr_input(struct sk_buff *skb)
2072 struct mfc_cache *cache;
2073 struct net *net = dev_net(skb->dev);
2074 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
2075 struct mr_table *mrt;
2076 struct net_device *dev;
2078 /* skb->dev passed in is the loX master dev for vrfs.
2079 * As there are no vifs associated with loopback devices,
2080 * get the proper interface that does have a vif associated with it.
2083 if (netif_is_l3_master(skb->dev)) {
2084 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2091 /* Packet is looped back after forward, it should not be
2092 * forwarded second time, but still can be delivered locally.
2094 if (IPCB(skb)->flags & IPSKB_FORWARDED)
2097 mrt = ipmr_rt_fib_lookup(net, skb);
2100 return PTR_ERR(mrt);
2103 if (IPCB(skb)->opt.router_alert) {
2104 if (ip_call_ra_chain(skb))
2106 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2107 /* IGMPv1 (and broken IGMPv2 implementations sort of
2108 * Cisco IOS <= 11.2(8)) do not put router alert
2109 * option to IGMP packets destined to routable
2110 * groups. It is very bad, because it means
2111 * that we can forward NO IGMP messages.
2113 struct sock *mroute_sk;
2115 mroute_sk = rcu_dereference(mrt->mroute_sk);
2118 raw_rcv(mroute_sk, skb);
2124 /* already under rcu_read_lock() */
2125 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
2127 int vif = ipmr_find_vif(mrt, dev);
2130 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2134 /* No usable cache entry */
2139 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2140 ip_local_deliver(skb);
2146 read_lock(&mrt_lock);
2147 vif = ipmr_find_vif(mrt, dev);
2149 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
2150 read_unlock(&mrt_lock);
2154 read_unlock(&mrt_lock);
2159 read_lock(&mrt_lock);
2160 ip_mr_forward(net, mrt, dev, skb, cache, local);
2161 read_unlock(&mrt_lock);
2164 return ip_local_deliver(skb);
2170 return ip_local_deliver(skb);
2175 #ifdef CONFIG_IP_PIMSM_V1
2176 /* Handle IGMP messages of PIMv1 */
2177 int pim_rcv_v1(struct sk_buff *skb)
2179 struct igmphdr *pim;
2180 struct net *net = dev_net(skb->dev);
2181 struct mr_table *mrt;
2183 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2186 pim = igmp_hdr(skb);
2188 mrt = ipmr_rt_fib_lookup(net, skb);
2191 if (!mrt->mroute_do_pim ||
2192 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2195 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2203 #ifdef CONFIG_IP_PIMSM_V2
2204 static int pim_rcv(struct sk_buff *skb)
2206 struct pimreghdr *pim;
2207 struct net *net = dev_net(skb->dev);
2208 struct mr_table *mrt;
2210 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2213 pim = (struct pimreghdr *)skb_transport_header(skb);
2214 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
2215 (pim->flags & PIM_NULL_REGISTER) ||
2216 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2217 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2220 mrt = ipmr_rt_fib_lookup(net, skb);
2223 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2231 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2232 __be32 saddr, __be32 daddr,
2233 struct rtmsg *rtm, u32 portid)
2235 struct mfc_cache *cache;
2236 struct mr_table *mrt;
2239 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2244 cache = ipmr_cache_find(mrt, saddr, daddr);
2245 if (!cache && skb->dev) {
2246 int vif = ipmr_find_vif(mrt, skb->dev);
2249 cache = ipmr_cache_find_any(mrt, daddr, vif);
2252 struct sk_buff *skb2;
2254 struct net_device *dev;
2258 read_lock(&mrt_lock);
2260 vif = ipmr_find_vif(mrt, dev);
2262 read_unlock(&mrt_lock);
2266 skb2 = skb_clone(skb, GFP_ATOMIC);
2268 read_unlock(&mrt_lock);
2273 NETLINK_CB(skb2).portid = portid;
2274 skb_push(skb2, sizeof(struct iphdr));
2275 skb_reset_network_header(skb2);
2277 iph->ihl = sizeof(struct iphdr) >> 2;
2281 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
2282 read_unlock(&mrt_lock);
2287 read_lock(&mrt_lock);
2288 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
2289 read_unlock(&mrt_lock);
2294 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2295 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2298 struct nlmsghdr *nlh;
2302 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2306 rtm = nlmsg_data(nlh);
2307 rtm->rtm_family = RTNL_FAMILY_IPMR;
2308 rtm->rtm_dst_len = 32;
2309 rtm->rtm_src_len = 32;
2311 rtm->rtm_table = mrt->id;
2312 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2313 goto nla_put_failure;
2314 rtm->rtm_type = RTN_MULTICAST;
2315 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2316 if (c->_c.mfc_flags & MFC_STATIC)
2317 rtm->rtm_protocol = RTPROT_STATIC;
2319 rtm->rtm_protocol = RTPROT_MROUTED;
2322 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2323 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2324 goto nla_put_failure;
2325 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
2326 /* do not break the dump if cache is unresolved */
2327 if (err < 0 && err != -ENOENT)
2328 goto nla_put_failure;
2330 nlmsg_end(skb, nlh);
2334 nlmsg_cancel(skb, nlh);
2338 static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2339 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2342 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2346 static size_t mroute_msgsize(bool unresolved, int maxvif)
2349 NLMSG_ALIGN(sizeof(struct rtmsg))
2350 + nla_total_size(4) /* RTA_TABLE */
2351 + nla_total_size(4) /* RTA_SRC */
2352 + nla_total_size(4) /* RTA_DST */
2357 + nla_total_size(4) /* RTA_IIF */
2358 + nla_total_size(0) /* RTA_MULTIPATH */
2359 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2361 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
2367 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2370 struct net *net = read_pnet(&mrt->net);
2371 struct sk_buff *skb;
2374 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2380 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2384 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2390 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2393 static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2396 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2397 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2398 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2399 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2400 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2401 /* IPMRA_CREPORT_PKT */
2402 + nla_total_size(payloadlen)
2408 static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2410 struct net *net = read_pnet(&mrt->net);
2411 struct nlmsghdr *nlh;
2412 struct rtgenmsg *rtgenm;
2413 struct igmpmsg *msg;
2414 struct sk_buff *skb;
2418 payloadlen = pkt->len - sizeof(struct igmpmsg);
2419 msg = (struct igmpmsg *)skb_network_header(pkt);
2421 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2425 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2426 sizeof(struct rtgenmsg), 0);
2429 rtgenm = nlmsg_data(nlh);
2430 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2431 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2432 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2433 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2434 msg->im_src.s_addr) ||
2435 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2436 msg->im_dst.s_addr))
2437 goto nla_put_failure;
2439 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2440 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2441 nla_data(nla), payloadlen))
2442 goto nla_put_failure;
2444 nlmsg_end(skb, nlh);
2446 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2450 nlmsg_cancel(skb, nlh);
2453 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2456 static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2457 struct netlink_ext_ack *extack)
2459 struct net *net = sock_net(in_skb->sk);
2460 struct nlattr *tb[RTA_MAX + 1];
2461 struct sk_buff *skb = NULL;
2462 struct mfc_cache *cache;
2463 struct mr_table *mrt;
2469 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2470 rtm_ipv4_policy, extack);
2474 rtm = nlmsg_data(nlh);
2476 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2477 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2478 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2480 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
2486 /* entries are added/deleted only under RTNL */
2488 cache = ipmr_cache_find(mrt, src, grp);
2495 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2501 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2502 nlh->nlmsg_seq, cache,
2507 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2517 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2519 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
2520 _ipmr_fill_mroute, &mfc_unres_lock);
2523 static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2524 [RTA_SRC] = { .type = NLA_U32 },
2525 [RTA_DST] = { .type = NLA_U32 },
2526 [RTA_IIF] = { .type = NLA_U32 },
2527 [RTA_TABLE] = { .type = NLA_U32 },
2528 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2531 static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2533 switch (rtm_protocol) {
2535 case RTPROT_MROUTED:
2541 static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2543 struct rtnexthop *rtnh = nla_data(nla);
2544 int remaining = nla_len(nla), vifi = 0;
2546 while (rtnh_ok(rtnh, remaining)) {
2547 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2548 if (++vifi == MAXVIFS)
2550 rtnh = rtnh_next(rtnh, &remaining);
2553 return remaining > 0 ? -EINVAL : vifi;
2556 /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2557 static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2558 struct mfcctl *mfcc, int *mrtsock,
2559 struct mr_table **mrtret,
2560 struct netlink_ext_ack *extack)
2562 struct net_device *dev = NULL;
2563 u32 tblid = RT_TABLE_DEFAULT;
2564 struct mr_table *mrt;
2565 struct nlattr *attr;
2569 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
2573 rtm = nlmsg_data(nlh);
2576 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2577 rtm->rtm_type != RTN_MULTICAST ||
2578 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2579 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2582 memset(mfcc, 0, sizeof(*mfcc));
2583 mfcc->mfcc_parent = -1;
2585 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2586 switch (nla_type(attr)) {
2588 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2591 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2594 dev = __dev_get_by_index(net, nla_get_u32(attr));
2601 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2610 tblid = nla_get_u32(attr);
2614 mrt = ipmr_get_table(net, tblid);
2620 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2622 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2628 /* takes care of both newroute and delroute */
2629 static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2630 struct netlink_ext_ack *extack)
2632 struct net *net = sock_net(skb->sk);
2633 int ret, mrtsock, parent;
2634 struct mr_table *tbl;
2639 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
2643 parent = ret ? mfcc.mfcc_parent : -1;
2644 if (nlh->nlmsg_type == RTM_NEWROUTE)
2645 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2647 return ipmr_mfc_delete(tbl, &mfcc, parent);
2650 static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2652 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2654 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2655 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2656 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2657 mrt->mroute_reg_vif_num) ||
2658 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2659 mrt->mroute_do_assert) ||
2660 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim))
2666 static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2668 struct nlattr *vif_nest;
2669 struct vif_device *vif;
2671 /* if the VIF doesn't exist just continue */
2672 if (!VIF_EXISTS(mrt, vifid))
2675 vif = &mrt->vif_table[vifid];
2676 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2679 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2680 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2681 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2682 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2684 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2686 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2688 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2690 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2691 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2692 nla_nest_cancel(skb, vif_nest);
2695 nla_nest_end(skb, vif_nest);
2700 static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2702 struct net *net = sock_net(skb->sk);
2703 struct nlmsghdr *nlh = NULL;
2704 unsigned int t = 0, s_t;
2705 unsigned int e = 0, s_e;
2706 struct mr_table *mrt;
2711 ipmr_for_each_table(mrt, net) {
2712 struct nlattr *vifs, *af;
2713 struct ifinfomsg *hdr;
2718 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2719 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2720 sizeof(*hdr), NLM_F_MULTI);
2724 hdr = nlmsg_data(nlh);
2725 memset(hdr, 0, sizeof(*hdr));
2726 hdr->ifi_family = RTNL_FAMILY_IPMR;
2728 af = nla_nest_start(skb, IFLA_AF_SPEC);
2730 nlmsg_cancel(skb, nlh);
2734 if (!ipmr_fill_table(mrt, skb)) {
2735 nlmsg_cancel(skb, nlh);
2739 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2741 nla_nest_end(skb, af);
2742 nlmsg_end(skb, nlh);
2745 for (i = 0; i < mrt->maxvif; i++) {
2748 if (!ipmr_fill_vif(mrt, i, skb)) {
2749 nla_nest_end(skb, vifs);
2750 nla_nest_end(skb, af);
2751 nlmsg_end(skb, nlh);
2759 nla_nest_end(skb, vifs);
2760 nla_nest_end(skb, af);
2761 nlmsg_end(skb, nlh);
2773 #ifdef CONFIG_PROC_FS
2774 /* The /proc interfaces to multicast routing :
2775 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2778 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2779 __acquires(mrt_lock)
2781 struct mr_vif_iter *iter = seq->private;
2782 struct net *net = seq_file_net(seq);
2783 struct mr_table *mrt;
2785 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2787 return ERR_PTR(-ENOENT);
2791 read_lock(&mrt_lock);
2792 return mr_vif_seq_start(seq, pos);
2795 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2796 __releases(mrt_lock)
2798 read_unlock(&mrt_lock);
2801 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2803 struct mr_vif_iter *iter = seq->private;
2804 struct mr_table *mrt = iter->mrt;
2806 if (v == SEQ_START_TOKEN) {
2808 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2810 const struct vif_device *vif = v;
2811 const char *name = vif->dev ?
2812 vif->dev->name : "none";
2815 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2816 vif - mrt->vif_table,
2817 name, vif->bytes_in, vif->pkt_in,
2818 vif->bytes_out, vif->pkt_out,
2819 vif->flags, vif->local, vif->remote);
2824 static const struct seq_operations ipmr_vif_seq_ops = {
2825 .start = ipmr_vif_seq_start,
2826 .next = mr_vif_seq_next,
2827 .stop = ipmr_vif_seq_stop,
2828 .show = ipmr_vif_seq_show,
2831 static int ipmr_vif_open(struct inode *inode, struct file *file)
2833 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2834 sizeof(struct mr_vif_iter));
2837 static const struct file_operations ipmr_vif_fops = {
2838 .open = ipmr_vif_open,
2840 .llseek = seq_lseek,
2841 .release = seq_release_net,
2844 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2846 struct net *net = seq_file_net(seq);
2847 struct mr_table *mrt;
2849 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2851 return ERR_PTR(-ENOENT);
2853 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
2856 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2860 if (v == SEQ_START_TOKEN) {
2862 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2864 const struct mfc_cache *mfc = v;
2865 const struct mr_mfc_iter *it = seq->private;
2866 const struct mr_table *mrt = it->mrt;
2868 seq_printf(seq, "%08X %08X %-3hd",
2869 (__force u32) mfc->mfc_mcastgrp,
2870 (__force u32) mfc->mfc_origin,
2871 mfc->_c.mfc_parent);
2873 if (it->cache != &mrt->mfc_unres_queue) {
2874 seq_printf(seq, " %8lu %8lu %8lu",
2875 mfc->_c.mfc_un.res.pkt,
2876 mfc->_c.mfc_un.res.bytes,
2877 mfc->_c.mfc_un.res.wrong_if);
2878 for (n = mfc->_c.mfc_un.res.minvif;
2879 n < mfc->_c.mfc_un.res.maxvif; n++) {
2880 if (VIF_EXISTS(mrt, n) &&
2881 mfc->_c.mfc_un.res.ttls[n] < 255)
2884 n, mfc->_c.mfc_un.res.ttls[n]);
2887 /* unresolved mfc_caches don't contain
2888 * pkt, bytes and wrong_if values
2890 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2892 seq_putc(seq, '\n');
2897 static const struct seq_operations ipmr_mfc_seq_ops = {
2898 .start = ipmr_mfc_seq_start,
2899 .next = mr_mfc_seq_next,
2900 .stop = mr_mfc_seq_stop,
2901 .show = ipmr_mfc_seq_show,
2904 static int ipmr_mfc_open(struct inode *inode, struct file *file)
2906 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2907 sizeof(struct mr_mfc_iter));
2910 static const struct file_operations ipmr_mfc_fops = {
2911 .open = ipmr_mfc_open,
2913 .llseek = seq_lseek,
2914 .release = seq_release_net,
2918 #ifdef CONFIG_IP_PIMSM_V2
2919 static const struct net_protocol pim_protocol = {
2925 static unsigned int ipmr_seq_read(struct net *net)
2929 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
2932 static int ipmr_dump(struct net *net, struct notifier_block *nb)
2934 return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
2935 ipmr_mr_table_iter, &mrt_lock);
2938 static const struct fib_notifier_ops ipmr_notifier_ops_template = {
2939 .family = RTNL_FAMILY_IPMR,
2940 .fib_seq_read = ipmr_seq_read,
2941 .fib_dump = ipmr_dump,
2942 .owner = THIS_MODULE,
2945 static int __net_init ipmr_notifier_init(struct net *net)
2947 struct fib_notifier_ops *ops;
2949 net->ipv4.ipmr_seq = 0;
2951 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
2953 return PTR_ERR(ops);
2954 net->ipv4.ipmr_notifier_ops = ops;
2959 static void __net_exit ipmr_notifier_exit(struct net *net)
2961 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
2962 net->ipv4.ipmr_notifier_ops = NULL;
2965 /* Setup for IP multicast routing */
2966 static int __net_init ipmr_net_init(struct net *net)
2970 err = ipmr_notifier_init(net);
2972 goto ipmr_notifier_fail;
2974 err = ipmr_rules_init(net);
2976 goto ipmr_rules_fail;
2978 #ifdef CONFIG_PROC_FS
2980 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
2982 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
2983 goto proc_cache_fail;
2987 #ifdef CONFIG_PROC_FS
2989 remove_proc_entry("ip_mr_vif", net->proc_net);
2991 ipmr_rules_exit(net);
2994 ipmr_notifier_exit(net);
2999 static void __net_exit ipmr_net_exit(struct net *net)
3001 #ifdef CONFIG_PROC_FS
3002 remove_proc_entry("ip_mr_cache", net->proc_net);
3003 remove_proc_entry("ip_mr_vif", net->proc_net);
3005 ipmr_notifier_exit(net);
3006 ipmr_rules_exit(net);
3009 static struct pernet_operations ipmr_net_ops = {
3010 .init = ipmr_net_init,
3011 .exit = ipmr_net_exit,
3014 int __init ip_mr_init(void)
3018 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3019 sizeof(struct mfc_cache),
3020 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
3023 err = register_pernet_subsys(&ipmr_net_ops);
3025 goto reg_pernet_fail;
3027 err = register_netdevice_notifier(&ip_mr_notifier);
3029 goto reg_notif_fail;
3030 #ifdef CONFIG_IP_PIMSM_V2
3031 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
3032 pr_err("%s: can't add PIM protocol\n", __func__);
3034 goto add_proto_fail;
3037 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
3038 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
3039 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
3040 ipmr_rtm_route, NULL, 0);
3041 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
3042 ipmr_rtm_route, NULL, 0);
3044 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
3045 NULL, ipmr_rtm_dumplink, 0);
3048 #ifdef CONFIG_IP_PIMSM_V2
3050 unregister_netdevice_notifier(&ip_mr_notifier);
3053 unregister_pernet_subsys(&ipmr_net_ops);
3055 kmem_cache_destroy(mrt_cachep);