perf bpf: Move perf_event_output() from stdio.h to bpf.h
[linux/fpc-iii.git] / net / ipv4 / ipmr.c
bloba6defbec4f1b4338993756ea7b8681012450aa1a
1 /*
2 * IP multicast routing support for mrouted 3.6/3.8
4 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
5 * Linux Consultancy and Custom Driver Development
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * Fixes:
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
22 * overflow.
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>
34 #include <linux/mm.h>
35 #include <linux/kernel.h>
36 #include <linux/fcntl.h>
37 #include <linux/stat.h>
38 #include <linux/socket.h>
39 #include <linux/in.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>
51 #include <net/ip.h>
52 #include <net/protocol.h>
53 #include <linux/skbuff.h>
54 #include <net/route.h>
55 #include <net/icmp.h>
56 #include <net/udp.h>
57 #include <net/raw.h>
58 #include <linux/notifier.h>
59 #include <linux/if_arp.h>
60 #include <linux/netfilter_ipv4.h>
61 #include <linux/compat.h>
62 #include <linux/export.h>
63 #include <linux/rhashtable.h>
64 #include <net/ip_tunnels.h>
65 #include <net/checksum.h>
66 #include <net/netlink.h>
67 #include <net/fib_rules.h>
68 #include <linux/netconf.h>
69 #include <net/nexthop.h>
70 #include <net/switchdev.h>
72 struct ipmr_rule {
73 struct fib_rule common;
76 struct ipmr_result {
77 struct mr_table *mrt;
80 /* Big lock, protecting vif table, mrt cache and mroute socket state.
81 * Note that the changes are semaphored via rtnl_lock.
84 static DEFINE_RWLOCK(mrt_lock);
86 /* Multicast router control variables */
88 /* Special spinlock for queue of unresolved entries */
89 static DEFINE_SPINLOCK(mfc_unres_lock);
91 /* We return to original Alan's scheme. Hash table of resolved
92 * entries is changed only in process context and protected
93 * with weak lock mrt_lock. Queue of unresolved entries is protected
94 * with strong spinlock mfc_unres_lock.
96 * In this case data path is free of exclusive locks at all.
99 static struct kmem_cache *mrt_cachep __ro_after_init;
101 static struct mr_table *ipmr_new_table(struct net *net, u32 id);
102 static void ipmr_free_table(struct mr_table *mrt);
104 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
105 struct net_device *dev, struct sk_buff *skb,
106 struct mfc_cache *cache, int local);
107 static int ipmr_cache_report(struct mr_table *mrt,
108 struct sk_buff *pkt, vifi_t vifi, int assert);
109 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
110 int cmd);
111 static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
112 static void mroute_clean_tables(struct mr_table *mrt, bool all);
113 static void ipmr_expire_process(struct timer_list *t);
115 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
116 #define ipmr_for_each_table(mrt, net) \
117 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
119 static struct mr_table *ipmr_mr_table_iter(struct net *net,
120 struct mr_table *mrt)
122 struct mr_table *ret;
124 if (!mrt)
125 ret = list_entry_rcu(net->ipv4.mr_tables.next,
126 struct mr_table, list);
127 else
128 ret = list_entry_rcu(mrt->list.next,
129 struct mr_table, list);
131 if (&ret->list == &net->ipv4.mr_tables)
132 return NULL;
133 return ret;
136 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
138 struct mr_table *mrt;
140 ipmr_for_each_table(mrt, net) {
141 if (mrt->id == id)
142 return mrt;
144 return NULL;
147 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
148 struct mr_table **mrt)
150 int err;
151 struct ipmr_result res;
152 struct fib_lookup_arg arg = {
153 .result = &res,
154 .flags = FIB_LOOKUP_NOREF,
157 /* update flow if oif or iif point to device enslaved to l3mdev */
158 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
160 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
161 flowi4_to_flowi(flp4), 0, &arg);
162 if (err < 0)
163 return err;
164 *mrt = res.mrt;
165 return 0;
168 static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
169 int flags, struct fib_lookup_arg *arg)
171 struct ipmr_result *res = arg->result;
172 struct mr_table *mrt;
174 switch (rule->action) {
175 case FR_ACT_TO_TBL:
176 break;
177 case FR_ACT_UNREACHABLE:
178 return -ENETUNREACH;
179 case FR_ACT_PROHIBIT:
180 return -EACCES;
181 case FR_ACT_BLACKHOLE:
182 default:
183 return -EINVAL;
186 arg->table = fib_rule_get_table(rule, arg);
188 mrt = ipmr_get_table(rule->fr_net, arg->table);
189 if (!mrt)
190 return -EAGAIN;
191 res->mrt = mrt;
192 return 0;
195 static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
197 return 1;
200 static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
201 FRA_GENERIC_POLICY,
204 static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
205 struct fib_rule_hdr *frh, struct nlattr **tb,
206 struct netlink_ext_ack *extack)
208 return 0;
211 static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
212 struct nlattr **tb)
214 return 1;
217 static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
218 struct fib_rule_hdr *frh)
220 frh->dst_len = 0;
221 frh->src_len = 0;
222 frh->tos = 0;
223 return 0;
226 static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
227 .family = RTNL_FAMILY_IPMR,
228 .rule_size = sizeof(struct ipmr_rule),
229 .addr_size = sizeof(u32),
230 .action = ipmr_rule_action,
231 .match = ipmr_rule_match,
232 .configure = ipmr_rule_configure,
233 .compare = ipmr_rule_compare,
234 .fill = ipmr_rule_fill,
235 .nlgroup = RTNLGRP_IPV4_RULE,
236 .policy = ipmr_rule_policy,
237 .owner = THIS_MODULE,
240 static int __net_init ipmr_rules_init(struct net *net)
242 struct fib_rules_ops *ops;
243 struct mr_table *mrt;
244 int err;
246 ops = fib_rules_register(&ipmr_rules_ops_template, net);
247 if (IS_ERR(ops))
248 return PTR_ERR(ops);
250 INIT_LIST_HEAD(&net->ipv4.mr_tables);
252 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
253 if (IS_ERR(mrt)) {
254 err = PTR_ERR(mrt);
255 goto err1;
258 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
259 if (err < 0)
260 goto err2;
262 net->ipv4.mr_rules_ops = ops;
263 return 0;
265 err2:
266 ipmr_free_table(mrt);
267 err1:
268 fib_rules_unregister(ops);
269 return err;
272 static void __net_exit ipmr_rules_exit(struct net *net)
274 struct mr_table *mrt, *next;
276 rtnl_lock();
277 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
278 list_del(&mrt->list);
279 ipmr_free_table(mrt);
281 fib_rules_unregister(net->ipv4.mr_rules_ops);
282 rtnl_unlock();
285 static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
287 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR);
290 static unsigned int ipmr_rules_seq_read(struct net *net)
292 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
295 bool ipmr_rule_default(const struct fib_rule *rule)
297 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
299 EXPORT_SYMBOL(ipmr_rule_default);
300 #else
301 #define ipmr_for_each_table(mrt, net) \
302 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
304 static struct mr_table *ipmr_mr_table_iter(struct net *net,
305 struct mr_table *mrt)
307 if (!mrt)
308 return net->ipv4.mrt;
309 return NULL;
312 static struct mr_table *ipmr_get_table(struct net *net, u32 id)
314 return net->ipv4.mrt;
317 static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
318 struct mr_table **mrt)
320 *mrt = net->ipv4.mrt;
321 return 0;
324 static int __net_init ipmr_rules_init(struct net *net)
326 struct mr_table *mrt;
328 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
329 if (IS_ERR(mrt))
330 return PTR_ERR(mrt);
331 net->ipv4.mrt = mrt;
332 return 0;
335 static void __net_exit ipmr_rules_exit(struct net *net)
337 rtnl_lock();
338 ipmr_free_table(net->ipv4.mrt);
339 net->ipv4.mrt = NULL;
340 rtnl_unlock();
343 static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
345 return 0;
348 static unsigned int ipmr_rules_seq_read(struct net *net)
350 return 0;
353 bool ipmr_rule_default(const struct fib_rule *rule)
355 return true;
357 EXPORT_SYMBOL(ipmr_rule_default);
358 #endif
360 static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
361 const void *ptr)
363 const struct mfc_cache_cmp_arg *cmparg = arg->key;
364 struct mfc_cache *c = (struct mfc_cache *)ptr;
366 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
367 cmparg->mfc_origin != c->mfc_origin;
370 static const struct rhashtable_params ipmr_rht_params = {
371 .head_offset = offsetof(struct mr_mfc, mnode),
372 .key_offset = offsetof(struct mfc_cache, cmparg),
373 .key_len = sizeof(struct mfc_cache_cmp_arg),
374 .nelem_hint = 3,
375 .locks_mul = 1,
376 .obj_cmpfn = ipmr_hash_cmp,
377 .automatic_shrinking = true,
380 static void ipmr_new_table_set(struct mr_table *mrt,
381 struct net *net)
383 #ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
384 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
385 #endif
388 static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
389 .mfc_mcastgrp = htonl(INADDR_ANY),
390 .mfc_origin = htonl(INADDR_ANY),
393 static struct mr_table_ops ipmr_mr_table_ops = {
394 .rht_params = &ipmr_rht_params,
395 .cmparg_any = &ipmr_mr_table_ops_cmparg_any,
398 static struct mr_table *ipmr_new_table(struct net *net, u32 id)
400 struct mr_table *mrt;
402 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
403 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
404 return ERR_PTR(-EINVAL);
406 mrt = ipmr_get_table(net, id);
407 if (mrt)
408 return mrt;
410 return mr_table_alloc(net, id, &ipmr_mr_table_ops,
411 ipmr_expire_process, ipmr_new_table_set);
414 static void ipmr_free_table(struct mr_table *mrt)
416 del_timer_sync(&mrt->ipmr_expire_timer);
417 mroute_clean_tables(mrt, true);
418 rhltable_destroy(&mrt->mfc_hash);
419 kfree(mrt);
422 /* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
424 static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
426 struct net *net = dev_net(dev);
428 dev_close(dev);
430 dev = __dev_get_by_name(net, "tunl0");
431 if (dev) {
432 const struct net_device_ops *ops = dev->netdev_ops;
433 struct ifreq ifr;
434 struct ip_tunnel_parm p;
436 memset(&p, 0, sizeof(p));
437 p.iph.daddr = v->vifc_rmt_addr.s_addr;
438 p.iph.saddr = v->vifc_lcl_addr.s_addr;
439 p.iph.version = 4;
440 p.iph.ihl = 5;
441 p.iph.protocol = IPPROTO_IPIP;
442 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
443 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
445 if (ops->ndo_do_ioctl) {
446 mm_segment_t oldfs = get_fs();
448 set_fs(KERNEL_DS);
449 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
450 set_fs(oldfs);
455 /* Initialize ipmr pimreg/tunnel in_device */
456 static bool ipmr_init_vif_indev(const struct net_device *dev)
458 struct in_device *in_dev;
460 ASSERT_RTNL();
462 in_dev = __in_dev_get_rtnl(dev);
463 if (!in_dev)
464 return false;
465 ipv4_devconf_setall(in_dev);
466 neigh_parms_data_state_setall(in_dev->arp_parms);
467 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
469 return true;
472 static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
474 struct net_device *dev;
476 dev = __dev_get_by_name(net, "tunl0");
478 if (dev) {
479 const struct net_device_ops *ops = dev->netdev_ops;
480 int err;
481 struct ifreq ifr;
482 struct ip_tunnel_parm p;
484 memset(&p, 0, sizeof(p));
485 p.iph.daddr = v->vifc_rmt_addr.s_addr;
486 p.iph.saddr = v->vifc_lcl_addr.s_addr;
487 p.iph.version = 4;
488 p.iph.ihl = 5;
489 p.iph.protocol = IPPROTO_IPIP;
490 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
491 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
493 if (ops->ndo_do_ioctl) {
494 mm_segment_t oldfs = get_fs();
496 set_fs(KERNEL_DS);
497 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
498 set_fs(oldfs);
499 } else {
500 err = -EOPNOTSUPP;
502 dev = NULL;
504 if (err == 0 &&
505 (dev = __dev_get_by_name(net, p.name)) != NULL) {
506 dev->flags |= IFF_MULTICAST;
507 if (!ipmr_init_vif_indev(dev))
508 goto failure;
509 if (dev_open(dev))
510 goto failure;
511 dev_hold(dev);
514 return dev;
516 failure:
517 unregister_netdevice(dev);
518 return NULL;
521 #if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
522 static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
524 struct net *net = dev_net(dev);
525 struct mr_table *mrt;
526 struct flowi4 fl4 = {
527 .flowi4_oif = dev->ifindex,
528 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
529 .flowi4_mark = skb->mark,
531 int err;
533 err = ipmr_fib_lookup(net, &fl4, &mrt);
534 if (err < 0) {
535 kfree_skb(skb);
536 return err;
539 read_lock(&mrt_lock);
540 dev->stats.tx_bytes += skb->len;
541 dev->stats.tx_packets++;
542 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
543 read_unlock(&mrt_lock);
544 kfree_skb(skb);
545 return NETDEV_TX_OK;
548 static int reg_vif_get_iflink(const struct net_device *dev)
550 return 0;
553 static const struct net_device_ops reg_vif_netdev_ops = {
554 .ndo_start_xmit = reg_vif_xmit,
555 .ndo_get_iflink = reg_vif_get_iflink,
558 static void reg_vif_setup(struct net_device *dev)
560 dev->type = ARPHRD_PIMREG;
561 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
562 dev->flags = IFF_NOARP;
563 dev->netdev_ops = &reg_vif_netdev_ops;
564 dev->needs_free_netdev = true;
565 dev->features |= NETIF_F_NETNS_LOCAL;
568 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
570 struct net_device *dev;
571 char name[IFNAMSIZ];
573 if (mrt->id == RT_TABLE_DEFAULT)
574 sprintf(name, "pimreg");
575 else
576 sprintf(name, "pimreg%u", mrt->id);
578 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
580 if (!dev)
581 return NULL;
583 dev_net_set(dev, net);
585 if (register_netdevice(dev)) {
586 free_netdev(dev);
587 return NULL;
590 if (!ipmr_init_vif_indev(dev))
591 goto failure;
592 if (dev_open(dev))
593 goto failure;
595 dev_hold(dev);
597 return dev;
599 failure:
600 unregister_netdevice(dev);
601 return NULL;
604 /* called with rcu_read_lock() */
605 static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
606 unsigned int pimlen)
608 struct net_device *reg_dev = NULL;
609 struct iphdr *encap;
611 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
612 /* Check that:
613 * a. packet is really sent to a multicast group
614 * b. packet is not a NULL-REGISTER
615 * c. packet is not truncated
617 if (!ipv4_is_multicast(encap->daddr) ||
618 encap->tot_len == 0 ||
619 ntohs(encap->tot_len) + pimlen > skb->len)
620 return 1;
622 read_lock(&mrt_lock);
623 if (mrt->mroute_reg_vif_num >= 0)
624 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
625 read_unlock(&mrt_lock);
627 if (!reg_dev)
628 return 1;
630 skb->mac_header = skb->network_header;
631 skb_pull(skb, (u8 *)encap - skb->data);
632 skb_reset_network_header(skb);
633 skb->protocol = htons(ETH_P_IP);
634 skb->ip_summed = CHECKSUM_NONE;
636 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
638 netif_rx(skb);
640 return NET_RX_SUCCESS;
642 #else
643 static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
645 return NULL;
647 #endif
649 static int call_ipmr_vif_entry_notifiers(struct net *net,
650 enum fib_event_type event_type,
651 struct vif_device *vif,
652 vifi_t vif_index, u32 tb_id)
654 return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
655 vif, vif_index, tb_id,
656 &net->ipv4.ipmr_seq);
659 static int call_ipmr_mfc_entry_notifiers(struct net *net,
660 enum fib_event_type event_type,
661 struct mfc_cache *mfc, u32 tb_id)
663 return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
664 &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
668 * vif_delete - Delete a VIF entry
669 * @notify: Set to 1, if the caller is a notifier_call
671 static int vif_delete(struct mr_table *mrt, int vifi, int notify,
672 struct list_head *head)
674 struct net *net = read_pnet(&mrt->net);
675 struct vif_device *v;
676 struct net_device *dev;
677 struct in_device *in_dev;
679 if (vifi < 0 || vifi >= mrt->maxvif)
680 return -EADDRNOTAVAIL;
682 v = &mrt->vif_table[vifi];
684 if (VIF_EXISTS(mrt, vifi))
685 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
686 mrt->id);
688 write_lock_bh(&mrt_lock);
689 dev = v->dev;
690 v->dev = NULL;
692 if (!dev) {
693 write_unlock_bh(&mrt_lock);
694 return -EADDRNOTAVAIL;
697 if (vifi == mrt->mroute_reg_vif_num)
698 mrt->mroute_reg_vif_num = -1;
700 if (vifi + 1 == mrt->maxvif) {
701 int tmp;
703 for (tmp = vifi - 1; tmp >= 0; tmp--) {
704 if (VIF_EXISTS(mrt, tmp))
705 break;
707 mrt->maxvif = tmp+1;
710 write_unlock_bh(&mrt_lock);
712 dev_set_allmulti(dev, -1);
714 in_dev = __in_dev_get_rtnl(dev);
715 if (in_dev) {
716 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
717 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
718 NETCONFA_MC_FORWARDING,
719 dev->ifindex, &in_dev->cnf);
720 ip_rt_multicast_event(in_dev);
723 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
724 unregister_netdevice_queue(dev, head);
726 dev_put(dev);
727 return 0;
730 static void ipmr_cache_free_rcu(struct rcu_head *head)
732 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
734 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
737 static void ipmr_cache_free(struct mfc_cache *c)
739 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
742 /* Destroy an unresolved cache entry, killing queued skbs
743 * and reporting error to netlink readers.
745 static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
747 struct net *net = read_pnet(&mrt->net);
748 struct sk_buff *skb;
749 struct nlmsgerr *e;
751 atomic_dec(&mrt->cache_resolve_queue_len);
753 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
754 if (ip_hdr(skb)->version == 0) {
755 struct nlmsghdr *nlh = skb_pull(skb,
756 sizeof(struct iphdr));
757 nlh->nlmsg_type = NLMSG_ERROR;
758 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
759 skb_trim(skb, nlh->nlmsg_len);
760 e = nlmsg_data(nlh);
761 e->error = -ETIMEDOUT;
762 memset(&e->msg, 0, sizeof(e->msg));
764 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
765 } else {
766 kfree_skb(skb);
770 ipmr_cache_free(c);
773 /* Timer process for the unresolved queue. */
774 static void ipmr_expire_process(struct timer_list *t)
776 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
777 struct mr_mfc *c, *next;
778 unsigned long expires;
779 unsigned long now;
781 if (!spin_trylock(&mfc_unres_lock)) {
782 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
783 return;
786 if (list_empty(&mrt->mfc_unres_queue))
787 goto out;
789 now = jiffies;
790 expires = 10*HZ;
792 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
793 if (time_after(c->mfc_un.unres.expires, now)) {
794 unsigned long interval = c->mfc_un.unres.expires - now;
795 if (interval < expires)
796 expires = interval;
797 continue;
800 list_del(&c->list);
801 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
802 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
805 if (!list_empty(&mrt->mfc_unres_queue))
806 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
808 out:
809 spin_unlock(&mfc_unres_lock);
812 /* Fill oifs list. It is called under write locked mrt_lock. */
813 static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
814 unsigned char *ttls)
816 int vifi;
818 cache->mfc_un.res.minvif = MAXVIFS;
819 cache->mfc_un.res.maxvif = 0;
820 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
822 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
823 if (VIF_EXISTS(mrt, vifi) &&
824 ttls[vifi] && ttls[vifi] < 255) {
825 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
826 if (cache->mfc_un.res.minvif > vifi)
827 cache->mfc_un.res.minvif = vifi;
828 if (cache->mfc_un.res.maxvif <= vifi)
829 cache->mfc_un.res.maxvif = vifi + 1;
832 cache->mfc_un.res.lastuse = jiffies;
835 static int vif_add(struct net *net, struct mr_table *mrt,
836 struct vifctl *vifc, int mrtsock)
838 int vifi = vifc->vifc_vifi;
839 struct switchdev_attr attr = {
840 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
842 struct vif_device *v = &mrt->vif_table[vifi];
843 struct net_device *dev;
844 struct in_device *in_dev;
845 int err;
847 /* Is vif busy ? */
848 if (VIF_EXISTS(mrt, vifi))
849 return -EADDRINUSE;
851 switch (vifc->vifc_flags) {
852 case VIFF_REGISTER:
853 if (!ipmr_pimsm_enabled())
854 return -EINVAL;
855 /* Special Purpose VIF in PIM
856 * All the packets will be sent to the daemon
858 if (mrt->mroute_reg_vif_num >= 0)
859 return -EADDRINUSE;
860 dev = ipmr_reg_vif(net, mrt);
861 if (!dev)
862 return -ENOBUFS;
863 err = dev_set_allmulti(dev, 1);
864 if (err) {
865 unregister_netdevice(dev);
866 dev_put(dev);
867 return err;
869 break;
870 case VIFF_TUNNEL:
871 dev = ipmr_new_tunnel(net, vifc);
872 if (!dev)
873 return -ENOBUFS;
874 err = dev_set_allmulti(dev, 1);
875 if (err) {
876 ipmr_del_tunnel(dev, vifc);
877 dev_put(dev);
878 return err;
880 break;
881 case VIFF_USE_IFINDEX:
882 case 0:
883 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
884 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
885 if (dev && !__in_dev_get_rtnl(dev)) {
886 dev_put(dev);
887 return -EADDRNOTAVAIL;
889 } else {
890 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
892 if (!dev)
893 return -EADDRNOTAVAIL;
894 err = dev_set_allmulti(dev, 1);
895 if (err) {
896 dev_put(dev);
897 return err;
899 break;
900 default:
901 return -EINVAL;
904 in_dev = __in_dev_get_rtnl(dev);
905 if (!in_dev) {
906 dev_put(dev);
907 return -EADDRNOTAVAIL;
909 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
910 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
911 dev->ifindex, &in_dev->cnf);
912 ip_rt_multicast_event(in_dev);
914 /* Fill in the VIF structures */
915 vif_device_init(v, dev, vifc->vifc_rate_limit,
916 vifc->vifc_threshold,
917 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
918 (VIFF_TUNNEL | VIFF_REGISTER));
920 attr.orig_dev = dev;
921 if (!switchdev_port_attr_get(dev, &attr)) {
922 memcpy(v->dev_parent_id.id, attr.u.ppid.id, attr.u.ppid.id_len);
923 v->dev_parent_id.id_len = attr.u.ppid.id_len;
924 } else {
925 v->dev_parent_id.id_len = 0;
928 v->local = vifc->vifc_lcl_addr.s_addr;
929 v->remote = vifc->vifc_rmt_addr.s_addr;
931 /* And finish update writing critical data */
932 write_lock_bh(&mrt_lock);
933 v->dev = dev;
934 if (v->flags & VIFF_REGISTER)
935 mrt->mroute_reg_vif_num = vifi;
936 if (vifi+1 > mrt->maxvif)
937 mrt->maxvif = vifi+1;
938 write_unlock_bh(&mrt_lock);
939 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
940 return 0;
943 /* called with rcu_read_lock() */
944 static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
945 __be32 origin,
946 __be32 mcastgrp)
948 struct mfc_cache_cmp_arg arg = {
949 .mfc_mcastgrp = mcastgrp,
950 .mfc_origin = origin
953 return mr_mfc_find(mrt, &arg);
956 /* Look for a (*,G) entry */
957 static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
958 __be32 mcastgrp, int vifi)
960 struct mfc_cache_cmp_arg arg = {
961 .mfc_mcastgrp = mcastgrp,
962 .mfc_origin = htonl(INADDR_ANY)
965 if (mcastgrp == htonl(INADDR_ANY))
966 return mr_mfc_find_any_parent(mrt, vifi);
967 return mr_mfc_find_any(mrt, vifi, &arg);
970 /* Look for a (S,G,iif) entry if parent != -1 */
971 static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
972 __be32 origin, __be32 mcastgrp,
973 int parent)
975 struct mfc_cache_cmp_arg arg = {
976 .mfc_mcastgrp = mcastgrp,
977 .mfc_origin = origin,
980 return mr_mfc_find_parent(mrt, &arg, parent);
983 /* Allocate a multicast cache entry */
984 static struct mfc_cache *ipmr_cache_alloc(void)
986 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
988 if (c) {
989 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
990 c->_c.mfc_un.res.minvif = MAXVIFS;
991 c->_c.free = ipmr_cache_free_rcu;
992 refcount_set(&c->_c.mfc_un.res.refcount, 1);
994 return c;
997 static struct mfc_cache *ipmr_cache_alloc_unres(void)
999 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
1001 if (c) {
1002 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
1003 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
1005 return c;
1008 /* A cache entry has gone into a resolved state from queued */
1009 static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1010 struct mfc_cache *uc, struct mfc_cache *c)
1012 struct sk_buff *skb;
1013 struct nlmsgerr *e;
1015 /* Play the pending entries through our router */
1016 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
1017 if (ip_hdr(skb)->version == 0) {
1018 struct nlmsghdr *nlh = skb_pull(skb,
1019 sizeof(struct iphdr));
1021 if (mr_fill_mroute(mrt, skb, &c->_c,
1022 nlmsg_data(nlh)) > 0) {
1023 nlh->nlmsg_len = skb_tail_pointer(skb) -
1024 (u8 *)nlh;
1025 } else {
1026 nlh->nlmsg_type = NLMSG_ERROR;
1027 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
1028 skb_trim(skb, nlh->nlmsg_len);
1029 e = nlmsg_data(nlh);
1030 e->error = -EMSGSIZE;
1031 memset(&e->msg, 0, sizeof(e->msg));
1034 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
1035 } else {
1036 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
1041 /* Bounce a cache query up to mrouted and netlink.
1043 * Called under mrt_lock.
1045 static int ipmr_cache_report(struct mr_table *mrt,
1046 struct sk_buff *pkt, vifi_t vifi, int assert)
1048 const int ihl = ip_hdrlen(pkt);
1049 struct sock *mroute_sk;
1050 struct igmphdr *igmp;
1051 struct igmpmsg *msg;
1052 struct sk_buff *skb;
1053 int ret;
1055 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE)
1056 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1057 else
1058 skb = alloc_skb(128, GFP_ATOMIC);
1060 if (!skb)
1061 return -ENOBUFS;
1063 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) {
1064 /* Ugly, but we have no choice with this interface.
1065 * Duplicate old header, fix ihl, length etc.
1066 * And all this only to mangle msg->im_msgtype and
1067 * to set msg->im_mbz to "mbz" :-)
1069 skb_push(skb, sizeof(struct iphdr));
1070 skb_reset_network_header(skb);
1071 skb_reset_transport_header(skb);
1072 msg = (struct igmpmsg *)skb_network_header(skb);
1073 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
1074 msg->im_msgtype = assert;
1075 msg->im_mbz = 0;
1076 if (assert == IGMPMSG_WRVIFWHOLE)
1077 msg->im_vif = vifi;
1078 else
1079 msg->im_vif = mrt->mroute_reg_vif_num;
1080 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1081 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1082 sizeof(struct iphdr));
1083 } else {
1084 /* Copy the IP header */
1085 skb_set_network_header(skb, skb->len);
1086 skb_put(skb, ihl);
1087 skb_copy_to_linear_data(skb, pkt->data, ihl);
1088 /* Flag to the kernel this is a route add */
1089 ip_hdr(skb)->protocol = 0;
1090 msg = (struct igmpmsg *)skb_network_header(skb);
1091 msg->im_vif = vifi;
1092 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1093 /* Add our header */
1094 igmp = skb_put(skb, sizeof(struct igmphdr));
1095 igmp->type = assert;
1096 msg->im_msgtype = assert;
1097 igmp->code = 0;
1098 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1099 skb->transport_header = skb->network_header;
1102 rcu_read_lock();
1103 mroute_sk = rcu_dereference(mrt->mroute_sk);
1104 if (!mroute_sk) {
1105 rcu_read_unlock();
1106 kfree_skb(skb);
1107 return -EINVAL;
1110 igmpmsg_netlink_event(mrt, skb);
1112 /* Deliver to mrouted */
1113 ret = sock_queue_rcv_skb(mroute_sk, skb);
1114 rcu_read_unlock();
1115 if (ret < 0) {
1116 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
1117 kfree_skb(skb);
1120 return ret;
1123 /* Queue a packet for resolution. It gets locked cache entry! */
1124 static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1125 struct sk_buff *skb, struct net_device *dev)
1127 const struct iphdr *iph = ip_hdr(skb);
1128 struct mfc_cache *c;
1129 bool found = false;
1130 int err;
1132 spin_lock_bh(&mfc_unres_lock);
1133 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
1134 if (c->mfc_mcastgrp == iph->daddr &&
1135 c->mfc_origin == iph->saddr) {
1136 found = true;
1137 break;
1141 if (!found) {
1142 /* Create a new entry if allowable */
1143 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
1144 (c = ipmr_cache_alloc_unres()) == NULL) {
1145 spin_unlock_bh(&mfc_unres_lock);
1147 kfree_skb(skb);
1148 return -ENOBUFS;
1151 /* Fill in the new cache entry */
1152 c->_c.mfc_parent = -1;
1153 c->mfc_origin = iph->saddr;
1154 c->mfc_mcastgrp = iph->daddr;
1156 /* Reflect first query at mrouted. */
1157 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
1159 if (err < 0) {
1160 /* If the report failed throw the cache entry
1161 out - Brad Parker
1163 spin_unlock_bh(&mfc_unres_lock);
1165 ipmr_cache_free(c);
1166 kfree_skb(skb);
1167 return err;
1170 atomic_inc(&mrt->cache_resolve_queue_len);
1171 list_add(&c->_c.list, &mrt->mfc_unres_queue);
1172 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1174 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1175 mod_timer(&mrt->ipmr_expire_timer,
1176 c->_c.mfc_un.unres.expires);
1179 /* See if we can append the packet */
1180 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
1181 kfree_skb(skb);
1182 err = -ENOBUFS;
1183 } else {
1184 if (dev) {
1185 skb->dev = dev;
1186 skb->skb_iif = dev->ifindex;
1188 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
1189 err = 0;
1192 spin_unlock_bh(&mfc_unres_lock);
1193 return err;
1196 /* MFC cache manipulation by user space mroute daemon */
1198 static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
1200 struct net *net = read_pnet(&mrt->net);
1201 struct mfc_cache *c;
1203 /* The entries are added/deleted only under RTNL */
1204 rcu_read_lock();
1205 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1206 mfc->mfcc_mcastgrp.s_addr, parent);
1207 rcu_read_unlock();
1208 if (!c)
1209 return -ENOENT;
1210 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1211 list_del_rcu(&c->_c.list);
1212 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
1213 mroute_netlink_event(mrt, c, RTM_DELROUTE);
1214 mr_cache_put(&c->_c);
1216 return 0;
1219 static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
1220 struct mfcctl *mfc, int mrtsock, int parent)
1222 struct mfc_cache *uc, *c;
1223 struct mr_mfc *_uc;
1224 bool found;
1225 int ret;
1227 if (mfc->mfcc_parent >= MAXVIFS)
1228 return -ENFILE;
1230 /* The entries are added/deleted only under RTNL */
1231 rcu_read_lock();
1232 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1233 mfc->mfcc_mcastgrp.s_addr, parent);
1234 rcu_read_unlock();
1235 if (c) {
1236 write_lock_bh(&mrt_lock);
1237 c->_c.mfc_parent = mfc->mfcc_parent;
1238 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1239 if (!mrtsock)
1240 c->_c.mfc_flags |= MFC_STATIC;
1241 write_unlock_bh(&mrt_lock);
1242 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1243 mrt->id);
1244 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1245 return 0;
1248 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
1249 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
1250 return -EINVAL;
1252 c = ipmr_cache_alloc();
1253 if (!c)
1254 return -ENOMEM;
1256 c->mfc_origin = mfc->mfcc_origin.s_addr;
1257 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1258 c->_c.mfc_parent = mfc->mfcc_parent;
1259 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
1260 if (!mrtsock)
1261 c->_c.mfc_flags |= MFC_STATIC;
1263 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
1264 ipmr_rht_params);
1265 if (ret) {
1266 pr_err("ipmr: rhtable insert error %d\n", ret);
1267 ipmr_cache_free(c);
1268 return ret;
1270 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
1271 /* Check to see if we resolved a queued list. If so we
1272 * need to send on the frames and tidy up.
1274 found = false;
1275 spin_lock_bh(&mfc_unres_lock);
1276 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1277 uc = (struct mfc_cache *)_uc;
1278 if (uc->mfc_origin == c->mfc_origin &&
1279 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
1280 list_del(&_uc->list);
1281 atomic_dec(&mrt->cache_resolve_queue_len);
1282 found = true;
1283 break;
1286 if (list_empty(&mrt->mfc_unres_queue))
1287 del_timer(&mrt->ipmr_expire_timer);
1288 spin_unlock_bh(&mfc_unres_lock);
1290 if (found) {
1291 ipmr_cache_resolve(net, mrt, uc, c);
1292 ipmr_cache_free(uc);
1294 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
1295 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
1296 return 0;
1299 /* Close the multicast socket, and clear the vif tables etc */
1300 static void mroute_clean_tables(struct mr_table *mrt, bool all)
1302 struct net *net = read_pnet(&mrt->net);
1303 struct mr_mfc *c, *tmp;
1304 struct mfc_cache *cache;
1305 LIST_HEAD(list);
1306 int i;
1308 /* Shut down all active vif entries */
1309 for (i = 0; i < mrt->maxvif; i++) {
1310 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1311 continue;
1312 vif_delete(mrt, i, 0, &list);
1314 unregister_netdevice_many(&list);
1316 /* Wipe the cache */
1317 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1318 if (!all && (c->mfc_flags & MFC_STATIC))
1319 continue;
1320 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1321 list_del_rcu(&c->list);
1322 cache = (struct mfc_cache *)c;
1323 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
1324 mrt->id);
1325 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1326 mr_cache_put(c);
1329 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1330 spin_lock_bh(&mfc_unres_lock);
1331 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1332 list_del(&c->list);
1333 cache = (struct mfc_cache *)c;
1334 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1335 ipmr_destroy_unres(mrt, cache);
1337 spin_unlock_bh(&mfc_unres_lock);
1341 /* called from ip_ra_control(), before an RCU grace period,
1342 * we dont need to call synchronize_rcu() here
1344 static void mrtsock_destruct(struct sock *sk)
1346 struct net *net = sock_net(sk);
1347 struct mr_table *mrt;
1349 rtnl_lock();
1350 ipmr_for_each_table(mrt, net) {
1351 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1352 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
1353 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1354 NETCONFA_MC_FORWARDING,
1355 NETCONFA_IFINDEX_ALL,
1356 net->ipv4.devconf_all);
1357 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
1358 mroute_clean_tables(mrt, false);
1361 rtnl_unlock();
1364 /* Socket options and virtual interface manipulation. The whole
1365 * virtual interface system is a complete heap, but unfortunately
1366 * that's how BSD mrouted happens to think. Maybe one day with a proper
1367 * MOSPF/PIM router set up we can clean this up.
1370 int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1371 unsigned int optlen)
1373 struct net *net = sock_net(sk);
1374 int val, ret = 0, parent = 0;
1375 struct mr_table *mrt;
1376 struct vifctl vif;
1377 struct mfcctl mfc;
1378 bool do_wrvifwhole;
1379 u32 uval;
1381 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1382 rtnl_lock();
1383 if (sk->sk_type != SOCK_RAW ||
1384 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1385 ret = -EOPNOTSUPP;
1386 goto out_unlock;
1389 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1390 if (!mrt) {
1391 ret = -ENOENT;
1392 goto out_unlock;
1394 if (optname != MRT_INIT) {
1395 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
1396 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1397 ret = -EACCES;
1398 goto out_unlock;
1402 switch (optname) {
1403 case MRT_INIT:
1404 if (optlen != sizeof(int)) {
1405 ret = -EINVAL;
1406 break;
1408 if (rtnl_dereference(mrt->mroute_sk)) {
1409 ret = -EADDRINUSE;
1410 break;
1413 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1414 if (ret == 0) {
1415 rcu_assign_pointer(mrt->mroute_sk, sk);
1416 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
1417 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1418 NETCONFA_MC_FORWARDING,
1419 NETCONFA_IFINDEX_ALL,
1420 net->ipv4.devconf_all);
1422 break;
1423 case MRT_DONE:
1424 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1425 ret = -EACCES;
1426 } else {
1427 /* We need to unlock here because mrtsock_destruct takes
1428 * care of rtnl itself and we can't change that due to
1429 * the IP_ROUTER_ALERT setsockopt which runs without it.
1431 rtnl_unlock();
1432 ret = ip_ra_control(sk, 0, NULL);
1433 goto out;
1435 break;
1436 case MRT_ADD_VIF:
1437 case MRT_DEL_VIF:
1438 if (optlen != sizeof(vif)) {
1439 ret = -EINVAL;
1440 break;
1442 if (copy_from_user(&vif, optval, sizeof(vif))) {
1443 ret = -EFAULT;
1444 break;
1446 if (vif.vifc_vifi >= MAXVIFS) {
1447 ret = -ENFILE;
1448 break;
1450 if (optname == MRT_ADD_VIF) {
1451 ret = vif_add(net, mrt, &vif,
1452 sk == rtnl_dereference(mrt->mroute_sk));
1453 } else {
1454 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
1456 break;
1457 /* Manipulate the forwarding caches. These live
1458 * in a sort of kernel/user symbiosis.
1460 case MRT_ADD_MFC:
1461 case MRT_DEL_MFC:
1462 parent = -1;
1463 /* fall through */
1464 case MRT_ADD_MFC_PROXY:
1465 case MRT_DEL_MFC_PROXY:
1466 if (optlen != sizeof(mfc)) {
1467 ret = -EINVAL;
1468 break;
1470 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1471 ret = -EFAULT;
1472 break;
1474 if (parent == 0)
1475 parent = mfc.mfcc_parent;
1476 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1477 ret = ipmr_mfc_delete(mrt, &mfc, parent);
1478 else
1479 ret = ipmr_mfc_add(net, mrt, &mfc,
1480 sk == rtnl_dereference(mrt->mroute_sk),
1481 parent);
1482 break;
1483 /* Control PIM assert. */
1484 case MRT_ASSERT:
1485 if (optlen != sizeof(val)) {
1486 ret = -EINVAL;
1487 break;
1489 if (get_user(val, (int __user *)optval)) {
1490 ret = -EFAULT;
1491 break;
1493 mrt->mroute_do_assert = val;
1494 break;
1495 case MRT_PIM:
1496 if (!ipmr_pimsm_enabled()) {
1497 ret = -ENOPROTOOPT;
1498 break;
1500 if (optlen != sizeof(val)) {
1501 ret = -EINVAL;
1502 break;
1504 if (get_user(val, (int __user *)optval)) {
1505 ret = -EFAULT;
1506 break;
1509 do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE);
1510 val = !!val;
1511 if (val != mrt->mroute_do_pim) {
1512 mrt->mroute_do_pim = val;
1513 mrt->mroute_do_assert = val;
1514 mrt->mroute_do_wrvifwhole = do_wrvifwhole;
1516 break;
1517 case MRT_TABLE:
1518 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1519 ret = -ENOPROTOOPT;
1520 break;
1522 if (optlen != sizeof(uval)) {
1523 ret = -EINVAL;
1524 break;
1526 if (get_user(uval, (u32 __user *)optval)) {
1527 ret = -EFAULT;
1528 break;
1531 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1532 ret = -EBUSY;
1533 } else {
1534 mrt = ipmr_new_table(net, uval);
1535 if (IS_ERR(mrt))
1536 ret = PTR_ERR(mrt);
1537 else
1538 raw_sk(sk)->ipmr_table = uval;
1540 break;
1541 /* Spurious command, or MRT_VERSION which you cannot set. */
1542 default:
1543 ret = -ENOPROTOOPT;
1545 out_unlock:
1546 rtnl_unlock();
1547 out:
1548 return ret;
1551 /* Getsock opt support for the multicast routing system. */
1552 int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
1554 int olr;
1555 int val;
1556 struct net *net = sock_net(sk);
1557 struct mr_table *mrt;
1559 if (sk->sk_type != SOCK_RAW ||
1560 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1561 return -EOPNOTSUPP;
1563 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1564 if (!mrt)
1565 return -ENOENT;
1567 switch (optname) {
1568 case MRT_VERSION:
1569 val = 0x0305;
1570 break;
1571 case MRT_PIM:
1572 if (!ipmr_pimsm_enabled())
1573 return -ENOPROTOOPT;
1574 val = mrt->mroute_do_pim;
1575 break;
1576 case MRT_ASSERT:
1577 val = mrt->mroute_do_assert;
1578 break;
1579 default:
1580 return -ENOPROTOOPT;
1583 if (get_user(olr, optlen))
1584 return -EFAULT;
1585 olr = min_t(unsigned int, olr, sizeof(int));
1586 if (olr < 0)
1587 return -EINVAL;
1588 if (put_user(olr, optlen))
1589 return -EFAULT;
1590 if (copy_to_user(optval, &val, olr))
1591 return -EFAULT;
1592 return 0;
1595 /* The IP multicast ioctl support routines. */
1596 int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1598 struct sioc_sg_req sr;
1599 struct sioc_vif_req vr;
1600 struct vif_device *vif;
1601 struct mfc_cache *c;
1602 struct net *net = sock_net(sk);
1603 struct mr_table *mrt;
1605 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1606 if (!mrt)
1607 return -ENOENT;
1609 switch (cmd) {
1610 case SIOCGETVIFCNT:
1611 if (copy_from_user(&vr, arg, sizeof(vr)))
1612 return -EFAULT;
1613 if (vr.vifi >= mrt->maxvif)
1614 return -EINVAL;
1615 read_lock(&mrt_lock);
1616 vif = &mrt->vif_table[vr.vifi];
1617 if (VIF_EXISTS(mrt, vr.vifi)) {
1618 vr.icount = vif->pkt_in;
1619 vr.ocount = vif->pkt_out;
1620 vr.ibytes = vif->bytes_in;
1621 vr.obytes = vif->bytes_out;
1622 read_unlock(&mrt_lock);
1624 if (copy_to_user(arg, &vr, sizeof(vr)))
1625 return -EFAULT;
1626 return 0;
1628 read_unlock(&mrt_lock);
1629 return -EADDRNOTAVAIL;
1630 case SIOCGETSGCNT:
1631 if (copy_from_user(&sr, arg, sizeof(sr)))
1632 return -EFAULT;
1634 rcu_read_lock();
1635 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1636 if (c) {
1637 sr.pktcnt = c->_c.mfc_un.res.pkt;
1638 sr.bytecnt = c->_c.mfc_un.res.bytes;
1639 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1640 rcu_read_unlock();
1642 if (copy_to_user(arg, &sr, sizeof(sr)))
1643 return -EFAULT;
1644 return 0;
1646 rcu_read_unlock();
1647 return -EADDRNOTAVAIL;
1648 default:
1649 return -ENOIOCTLCMD;
1653 #ifdef CONFIG_COMPAT
1654 struct compat_sioc_sg_req {
1655 struct in_addr src;
1656 struct in_addr grp;
1657 compat_ulong_t pktcnt;
1658 compat_ulong_t bytecnt;
1659 compat_ulong_t wrong_if;
1662 struct compat_sioc_vif_req {
1663 vifi_t vifi; /* Which iface */
1664 compat_ulong_t icount;
1665 compat_ulong_t ocount;
1666 compat_ulong_t ibytes;
1667 compat_ulong_t obytes;
1670 int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1672 struct compat_sioc_sg_req sr;
1673 struct compat_sioc_vif_req vr;
1674 struct vif_device *vif;
1675 struct mfc_cache *c;
1676 struct net *net = sock_net(sk);
1677 struct mr_table *mrt;
1679 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
1680 if (!mrt)
1681 return -ENOENT;
1683 switch (cmd) {
1684 case SIOCGETVIFCNT:
1685 if (copy_from_user(&vr, arg, sizeof(vr)))
1686 return -EFAULT;
1687 if (vr.vifi >= mrt->maxvif)
1688 return -EINVAL;
1689 read_lock(&mrt_lock);
1690 vif = &mrt->vif_table[vr.vifi];
1691 if (VIF_EXISTS(mrt, vr.vifi)) {
1692 vr.icount = vif->pkt_in;
1693 vr.ocount = vif->pkt_out;
1694 vr.ibytes = vif->bytes_in;
1695 vr.obytes = vif->bytes_out;
1696 read_unlock(&mrt_lock);
1698 if (copy_to_user(arg, &vr, sizeof(vr)))
1699 return -EFAULT;
1700 return 0;
1702 read_unlock(&mrt_lock);
1703 return -EADDRNOTAVAIL;
1704 case SIOCGETSGCNT:
1705 if (copy_from_user(&sr, arg, sizeof(sr)))
1706 return -EFAULT;
1708 rcu_read_lock();
1709 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1710 if (c) {
1711 sr.pktcnt = c->_c.mfc_un.res.pkt;
1712 sr.bytecnt = c->_c.mfc_un.res.bytes;
1713 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
1714 rcu_read_unlock();
1716 if (copy_to_user(arg, &sr, sizeof(sr)))
1717 return -EFAULT;
1718 return 0;
1720 rcu_read_unlock();
1721 return -EADDRNOTAVAIL;
1722 default:
1723 return -ENOIOCTLCMD;
1726 #endif
1728 static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1730 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1731 struct net *net = dev_net(dev);
1732 struct mr_table *mrt;
1733 struct vif_device *v;
1734 int ct;
1736 if (event != NETDEV_UNREGISTER)
1737 return NOTIFY_DONE;
1739 ipmr_for_each_table(mrt, net) {
1740 v = &mrt->vif_table[0];
1741 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1742 if (v->dev == dev)
1743 vif_delete(mrt, ct, 1, NULL);
1746 return NOTIFY_DONE;
1749 static struct notifier_block ip_mr_notifier = {
1750 .notifier_call = ipmr_device_event,
1753 /* Encapsulate a packet by attaching a valid IPIP header to it.
1754 * This avoids tunnel drivers and other mess and gives us the speed so
1755 * important for multicast video.
1757 static void ip_encap(struct net *net, struct sk_buff *skb,
1758 __be32 saddr, __be32 daddr)
1760 struct iphdr *iph;
1761 const struct iphdr *old_iph = ip_hdr(skb);
1763 skb_push(skb, sizeof(struct iphdr));
1764 skb->transport_header = skb->network_header;
1765 skb_reset_network_header(skb);
1766 iph = ip_hdr(skb);
1768 iph->version = 4;
1769 iph->tos = old_iph->tos;
1770 iph->ttl = old_iph->ttl;
1771 iph->frag_off = 0;
1772 iph->daddr = daddr;
1773 iph->saddr = saddr;
1774 iph->protocol = IPPROTO_IPIP;
1775 iph->ihl = 5;
1776 iph->tot_len = htons(skb->len);
1777 ip_select_ident(net, skb, NULL);
1778 ip_send_check(iph);
1780 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1781 nf_reset(skb);
1784 static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1785 struct sk_buff *skb)
1787 struct ip_options *opt = &(IPCB(skb)->opt);
1789 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1790 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
1792 if (unlikely(opt->optlen))
1793 ip_forward_options(skb);
1795 return dst_output(net, sk, skb);
1798 #ifdef CONFIG_NET_SWITCHDEV
1799 static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1800 int in_vifi, int out_vifi)
1802 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1803 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1805 if (!skb->offload_mr_fwd_mark)
1806 return false;
1807 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1808 return false;
1809 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1810 &in_vif->dev_parent_id);
1812 #else
1813 static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1814 int in_vifi, int out_vifi)
1816 return false;
1818 #endif
1820 /* Processing handlers for ipmr_forward */
1822 static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1823 int in_vifi, struct sk_buff *skb,
1824 struct mfc_cache *c, int vifi)
1826 const struct iphdr *iph = ip_hdr(skb);
1827 struct vif_device *vif = &mrt->vif_table[vifi];
1828 struct net_device *dev;
1829 struct rtable *rt;
1830 struct flowi4 fl4;
1831 int encap = 0;
1833 if (!vif->dev)
1834 goto out_free;
1836 if (vif->flags & VIFF_REGISTER) {
1837 vif->pkt_out++;
1838 vif->bytes_out += skb->len;
1839 vif->dev->stats.tx_bytes += skb->len;
1840 vif->dev->stats.tx_packets++;
1841 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
1842 goto out_free;
1845 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1846 goto out_free;
1848 if (vif->flags & VIFF_TUNNEL) {
1849 rt = ip_route_output_ports(net, &fl4, NULL,
1850 vif->remote, vif->local,
1851 0, 0,
1852 IPPROTO_IPIP,
1853 RT_TOS(iph->tos), vif->link);
1854 if (IS_ERR(rt))
1855 goto out_free;
1856 encap = sizeof(struct iphdr);
1857 } else {
1858 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
1859 0, 0,
1860 IPPROTO_IPIP,
1861 RT_TOS(iph->tos), vif->link);
1862 if (IS_ERR(rt))
1863 goto out_free;
1866 dev = rt->dst.dev;
1868 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
1869 /* Do not fragment multicasts. Alas, IPv4 does not
1870 * allow to send ICMP, so that packets will disappear
1871 * to blackhole.
1873 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
1874 ip_rt_put(rt);
1875 goto out_free;
1878 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
1880 if (skb_cow(skb, encap)) {
1881 ip_rt_put(rt);
1882 goto out_free;
1885 vif->pkt_out++;
1886 vif->bytes_out += skb->len;
1888 skb_dst_drop(skb);
1889 skb_dst_set(skb, &rt->dst);
1890 ip_decrease_ttl(ip_hdr(skb));
1892 /* FIXME: forward and output firewalls used to be called here.
1893 * What do we do with netfilter? -- RR
1895 if (vif->flags & VIFF_TUNNEL) {
1896 ip_encap(net, skb, vif->local, vif->remote);
1897 /* FIXME: extra output firewall step used to be here. --RR */
1898 vif->dev->stats.tx_packets++;
1899 vif->dev->stats.tx_bytes += skb->len;
1902 IPCB(skb)->flags |= IPSKB_FORWARDED;
1904 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1905 * not only before forwarding, but after forwarding on all output
1906 * interfaces. It is clear, if mrouter runs a multicasting
1907 * program, it should receive packets not depending to what interface
1908 * program is joined.
1909 * If we will not make it, the program will have to join on all
1910 * interfaces. On the other hand, multihoming host (or router, but
1911 * not mrouter) cannot join to more than one interface - it will
1912 * result in receiving multiple packets.
1914 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1915 net, NULL, skb, skb->dev, dev,
1916 ipmr_forward_finish);
1917 return;
1919 out_free:
1920 kfree_skb(skb);
1923 static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
1925 int ct;
1927 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1928 if (mrt->vif_table[ct].dev == dev)
1929 break;
1931 return ct;
1934 /* "local" means that we should preserve one skb (for local delivery) */
1935 static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1936 struct net_device *dev, struct sk_buff *skb,
1937 struct mfc_cache *c, int local)
1939 int true_vifi = ipmr_find_vif(mrt, dev);
1940 int psend = -1;
1941 int vif, ct;
1943 vif = c->_c.mfc_parent;
1944 c->_c.mfc_un.res.pkt++;
1945 c->_c.mfc_un.res.bytes += skb->len;
1946 c->_c.mfc_un.res.lastuse = jiffies;
1948 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
1949 struct mfc_cache *cache_proxy;
1951 /* For an (*,G) entry, we only check that the incomming
1952 * interface is part of the static tree.
1954 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
1955 if (cache_proxy &&
1956 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
1957 goto forward;
1960 /* Wrong interface: drop packet and (maybe) send PIM assert. */
1961 if (mrt->vif_table[vif].dev != dev) {
1962 if (rt_is_output_route(skb_rtable(skb))) {
1963 /* It is our own packet, looped back.
1964 * Very complicated situation...
1966 * The best workaround until routing daemons will be
1967 * fixed is not to redistribute packet, if it was
1968 * send through wrong interface. It means, that
1969 * multicast applications WILL NOT work for
1970 * (S,G), which have default multicast route pointing
1971 * to wrong oif. In any case, it is not a good
1972 * idea to use multicasting applications on router.
1974 goto dont_forward;
1977 c->_c.mfc_un.res.wrong_if++;
1979 if (true_vifi >= 0 && mrt->mroute_do_assert &&
1980 /* pimsm uses asserts, when switching from RPT to SPT,
1981 * so that we cannot check that packet arrived on an oif.
1982 * It is bad, but otherwise we would need to move pretty
1983 * large chunk of pimd to kernel. Ough... --ANK
1985 (mrt->mroute_do_pim ||
1986 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
1987 time_after(jiffies,
1988 c->_c.mfc_un.res.last_assert +
1989 MFC_ASSERT_THRESH)) {
1990 c->_c.mfc_un.res.last_assert = jiffies;
1991 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
1992 if (mrt->mroute_do_wrvifwhole)
1993 ipmr_cache_report(mrt, skb, true_vifi,
1994 IGMPMSG_WRVIFWHOLE);
1996 goto dont_forward;
1999 forward:
2000 mrt->vif_table[vif].pkt_in++;
2001 mrt->vif_table[vif].bytes_in += skb->len;
2003 /* Forward the frame */
2004 if (c->mfc_origin == htonl(INADDR_ANY) &&
2005 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
2006 if (true_vifi >= 0 &&
2007 true_vifi != c->_c.mfc_parent &&
2008 ip_hdr(skb)->ttl >
2009 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
2010 /* It's an (*,*) entry and the packet is not coming from
2011 * the upstream: forward the packet to the upstream
2012 * only.
2014 psend = c->_c.mfc_parent;
2015 goto last_forward;
2017 goto dont_forward;
2019 for (ct = c->_c.mfc_un.res.maxvif - 1;
2020 ct >= c->_c.mfc_un.res.minvif; ct--) {
2021 /* For (*,G) entry, don't forward to the incoming interface */
2022 if ((c->mfc_origin != htonl(INADDR_ANY) ||
2023 ct != true_vifi) &&
2024 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
2025 if (psend != -1) {
2026 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2028 if (skb2)
2029 ipmr_queue_xmit(net, mrt, true_vifi,
2030 skb2, c, psend);
2032 psend = ct;
2035 last_forward:
2036 if (psend != -1) {
2037 if (local) {
2038 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2040 if (skb2)
2041 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
2042 c, psend);
2043 } else {
2044 ipmr_queue_xmit(net, mrt, true_vifi, skb, c, psend);
2045 return;
2049 dont_forward:
2050 if (!local)
2051 kfree_skb(skb);
2054 static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
2056 struct rtable *rt = skb_rtable(skb);
2057 struct iphdr *iph = ip_hdr(skb);
2058 struct flowi4 fl4 = {
2059 .daddr = iph->daddr,
2060 .saddr = iph->saddr,
2061 .flowi4_tos = RT_TOS(iph->tos),
2062 .flowi4_oif = (rt_is_output_route(rt) ?
2063 skb->dev->ifindex : 0),
2064 .flowi4_iif = (rt_is_output_route(rt) ?
2065 LOOPBACK_IFINDEX :
2066 skb->dev->ifindex),
2067 .flowi4_mark = skb->mark,
2069 struct mr_table *mrt;
2070 int err;
2072 err = ipmr_fib_lookup(net, &fl4, &mrt);
2073 if (err)
2074 return ERR_PTR(err);
2075 return mrt;
2078 /* Multicast packets for forwarding arrive here
2079 * Called with rcu_read_lock();
2081 int ip_mr_input(struct sk_buff *skb)
2083 struct mfc_cache *cache;
2084 struct net *net = dev_net(skb->dev);
2085 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
2086 struct mr_table *mrt;
2087 struct net_device *dev;
2089 /* skb->dev passed in is the loX master dev for vrfs.
2090 * As there are no vifs associated with loopback devices,
2091 * get the proper interface that does have a vif associated with it.
2093 dev = skb->dev;
2094 if (netif_is_l3_master(skb->dev)) {
2095 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2096 if (!dev) {
2097 kfree_skb(skb);
2098 return -ENODEV;
2102 /* Packet is looped back after forward, it should not be
2103 * forwarded second time, but still can be delivered locally.
2105 if (IPCB(skb)->flags & IPSKB_FORWARDED)
2106 goto dont_forward;
2108 mrt = ipmr_rt_fib_lookup(net, skb);
2109 if (IS_ERR(mrt)) {
2110 kfree_skb(skb);
2111 return PTR_ERR(mrt);
2113 if (!local) {
2114 if (IPCB(skb)->opt.router_alert) {
2115 if (ip_call_ra_chain(skb))
2116 return 0;
2117 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2118 /* IGMPv1 (and broken IGMPv2 implementations sort of
2119 * Cisco IOS <= 11.2(8)) do not put router alert
2120 * option to IGMP packets destined to routable
2121 * groups. It is very bad, because it means
2122 * that we can forward NO IGMP messages.
2124 struct sock *mroute_sk;
2126 mroute_sk = rcu_dereference(mrt->mroute_sk);
2127 if (mroute_sk) {
2128 nf_reset(skb);
2129 raw_rcv(mroute_sk, skb);
2130 return 0;
2135 /* already under rcu_read_lock() */
2136 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
2137 if (!cache) {
2138 int vif = ipmr_find_vif(mrt, dev);
2140 if (vif >= 0)
2141 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2142 vif);
2145 /* No usable cache entry */
2146 if (!cache) {
2147 int vif;
2149 if (local) {
2150 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2151 ip_local_deliver(skb);
2152 if (!skb2)
2153 return -ENOBUFS;
2154 skb = skb2;
2157 read_lock(&mrt_lock);
2158 vif = ipmr_find_vif(mrt, dev);
2159 if (vif >= 0) {
2160 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
2161 read_unlock(&mrt_lock);
2163 return err2;
2165 read_unlock(&mrt_lock);
2166 kfree_skb(skb);
2167 return -ENODEV;
2170 read_lock(&mrt_lock);
2171 ip_mr_forward(net, mrt, dev, skb, cache, local);
2172 read_unlock(&mrt_lock);
2174 if (local)
2175 return ip_local_deliver(skb);
2177 return 0;
2179 dont_forward:
2180 if (local)
2181 return ip_local_deliver(skb);
2182 kfree_skb(skb);
2183 return 0;
2186 #ifdef CONFIG_IP_PIMSM_V1
2187 /* Handle IGMP messages of PIMv1 */
2188 int pim_rcv_v1(struct sk_buff *skb)
2190 struct igmphdr *pim;
2191 struct net *net = dev_net(skb->dev);
2192 struct mr_table *mrt;
2194 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2195 goto drop;
2197 pim = igmp_hdr(skb);
2199 mrt = ipmr_rt_fib_lookup(net, skb);
2200 if (IS_ERR(mrt))
2201 goto drop;
2202 if (!mrt->mroute_do_pim ||
2203 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2204 goto drop;
2206 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2207 drop:
2208 kfree_skb(skb);
2210 return 0;
2212 #endif
2214 #ifdef CONFIG_IP_PIMSM_V2
2215 static int pim_rcv(struct sk_buff *skb)
2217 struct pimreghdr *pim;
2218 struct net *net = dev_net(skb->dev);
2219 struct mr_table *mrt;
2221 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2222 goto drop;
2224 pim = (struct pimreghdr *)skb_transport_header(skb);
2225 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
2226 (pim->flags & PIM_NULL_REGISTER) ||
2227 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
2228 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
2229 goto drop;
2231 mrt = ipmr_rt_fib_lookup(net, skb);
2232 if (IS_ERR(mrt))
2233 goto drop;
2234 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
2235 drop:
2236 kfree_skb(skb);
2238 return 0;
2240 #endif
2242 int ipmr_get_route(struct net *net, struct sk_buff *skb,
2243 __be32 saddr, __be32 daddr,
2244 struct rtmsg *rtm, u32 portid)
2246 struct mfc_cache *cache;
2247 struct mr_table *mrt;
2248 int err;
2250 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2251 if (!mrt)
2252 return -ENOENT;
2254 rcu_read_lock();
2255 cache = ipmr_cache_find(mrt, saddr, daddr);
2256 if (!cache && skb->dev) {
2257 int vif = ipmr_find_vif(mrt, skb->dev);
2259 if (vif >= 0)
2260 cache = ipmr_cache_find_any(mrt, daddr, vif);
2262 if (!cache) {
2263 struct sk_buff *skb2;
2264 struct iphdr *iph;
2265 struct net_device *dev;
2266 int vif = -1;
2268 dev = skb->dev;
2269 read_lock(&mrt_lock);
2270 if (dev)
2271 vif = ipmr_find_vif(mrt, dev);
2272 if (vif < 0) {
2273 read_unlock(&mrt_lock);
2274 rcu_read_unlock();
2275 return -ENODEV;
2277 skb2 = skb_clone(skb, GFP_ATOMIC);
2278 if (!skb2) {
2279 read_unlock(&mrt_lock);
2280 rcu_read_unlock();
2281 return -ENOMEM;
2284 NETLINK_CB(skb2).portid = portid;
2285 skb_push(skb2, sizeof(struct iphdr));
2286 skb_reset_network_header(skb2);
2287 iph = ip_hdr(skb2);
2288 iph->ihl = sizeof(struct iphdr) >> 2;
2289 iph->saddr = saddr;
2290 iph->daddr = daddr;
2291 iph->version = 0;
2292 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
2293 read_unlock(&mrt_lock);
2294 rcu_read_unlock();
2295 return err;
2298 read_lock(&mrt_lock);
2299 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
2300 read_unlock(&mrt_lock);
2301 rcu_read_unlock();
2302 return err;
2305 static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2306 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2307 int flags)
2309 struct nlmsghdr *nlh;
2310 struct rtmsg *rtm;
2311 int err;
2313 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
2314 if (!nlh)
2315 return -EMSGSIZE;
2317 rtm = nlmsg_data(nlh);
2318 rtm->rtm_family = RTNL_FAMILY_IPMR;
2319 rtm->rtm_dst_len = 32;
2320 rtm->rtm_src_len = 32;
2321 rtm->rtm_tos = 0;
2322 rtm->rtm_table = mrt->id;
2323 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2324 goto nla_put_failure;
2325 rtm->rtm_type = RTN_MULTICAST;
2326 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
2327 if (c->_c.mfc_flags & MFC_STATIC)
2328 rtm->rtm_protocol = RTPROT_STATIC;
2329 else
2330 rtm->rtm_protocol = RTPROT_MROUTED;
2331 rtm->rtm_flags = 0;
2333 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2334 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
2335 goto nla_put_failure;
2336 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
2337 /* do not break the dump if cache is unresolved */
2338 if (err < 0 && err != -ENOENT)
2339 goto nla_put_failure;
2341 nlmsg_end(skb, nlh);
2342 return 0;
2344 nla_put_failure:
2345 nlmsg_cancel(skb, nlh);
2346 return -EMSGSIZE;
2349 static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2350 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2351 int flags)
2353 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2354 cmd, flags);
2357 static size_t mroute_msgsize(bool unresolved, int maxvif)
2359 size_t len =
2360 NLMSG_ALIGN(sizeof(struct rtmsg))
2361 + nla_total_size(4) /* RTA_TABLE */
2362 + nla_total_size(4) /* RTA_SRC */
2363 + nla_total_size(4) /* RTA_DST */
2366 if (!unresolved)
2367 len = len
2368 + nla_total_size(4) /* RTA_IIF */
2369 + nla_total_size(0) /* RTA_MULTIPATH */
2370 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2371 /* RTA_MFC_STATS */
2372 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
2375 return len;
2378 static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2379 int cmd)
2381 struct net *net = read_pnet(&mrt->net);
2382 struct sk_buff *skb;
2383 int err = -ENOBUFS;
2385 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2386 mrt->maxvif),
2387 GFP_ATOMIC);
2388 if (!skb)
2389 goto errout;
2391 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
2392 if (err < 0)
2393 goto errout;
2395 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2396 return;
2398 errout:
2399 kfree_skb(skb);
2400 if (err < 0)
2401 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2404 static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2406 size_t len =
2407 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2408 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2409 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2410 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2411 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2412 /* IPMRA_CREPORT_PKT */
2413 + nla_total_size(payloadlen)
2416 return len;
2419 static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2421 struct net *net = read_pnet(&mrt->net);
2422 struct nlmsghdr *nlh;
2423 struct rtgenmsg *rtgenm;
2424 struct igmpmsg *msg;
2425 struct sk_buff *skb;
2426 struct nlattr *nla;
2427 int payloadlen;
2429 payloadlen = pkt->len - sizeof(struct igmpmsg);
2430 msg = (struct igmpmsg *)skb_network_header(pkt);
2432 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2433 if (!skb)
2434 goto errout;
2436 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2437 sizeof(struct rtgenmsg), 0);
2438 if (!nlh)
2439 goto errout;
2440 rtgenm = nlmsg_data(nlh);
2441 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2442 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2443 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2444 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2445 msg->im_src.s_addr) ||
2446 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2447 msg->im_dst.s_addr))
2448 goto nla_put_failure;
2450 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2451 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2452 nla_data(nla), payloadlen))
2453 goto nla_put_failure;
2455 nlmsg_end(skb, nlh);
2457 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2458 return;
2460 nla_put_failure:
2461 nlmsg_cancel(skb, nlh);
2462 errout:
2463 kfree_skb(skb);
2464 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2467 static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2468 struct netlink_ext_ack *extack)
2470 struct net *net = sock_net(in_skb->sk);
2471 struct nlattr *tb[RTA_MAX + 1];
2472 struct sk_buff *skb = NULL;
2473 struct mfc_cache *cache;
2474 struct mr_table *mrt;
2475 struct rtmsg *rtm;
2476 __be32 src, grp;
2477 u32 tableid;
2478 int err;
2480 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2481 rtm_ipv4_policy, extack);
2482 if (err < 0)
2483 goto errout;
2485 rtm = nlmsg_data(nlh);
2487 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2488 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2489 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2491 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
2492 if (!mrt) {
2493 err = -ENOENT;
2494 goto errout_free;
2497 /* entries are added/deleted only under RTNL */
2498 rcu_read_lock();
2499 cache = ipmr_cache_find(mrt, src, grp);
2500 rcu_read_unlock();
2501 if (!cache) {
2502 err = -ENOENT;
2503 goto errout_free;
2506 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2507 if (!skb) {
2508 err = -ENOBUFS;
2509 goto errout_free;
2512 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2513 nlh->nlmsg_seq, cache,
2514 RTM_NEWROUTE, 0);
2515 if (err < 0)
2516 goto errout_free;
2518 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2520 errout:
2521 return err;
2523 errout_free:
2524 kfree_skb(skb);
2525 goto errout;
2528 static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2530 struct fib_dump_filter filter = {};
2531 int err;
2533 if (cb->strict_check) {
2534 err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh,
2535 &filter, cb);
2536 if (err < 0)
2537 return err;
2540 if (filter.table_id) {
2541 struct mr_table *mrt;
2543 mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id);
2544 if (!mrt) {
2545 if (filter.dump_all_families)
2546 return skb->len;
2548 NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist");
2549 return -ENOENT;
2551 err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute,
2552 &mfc_unres_lock, &filter);
2553 return skb->len ? : err;
2556 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
2557 _ipmr_fill_mroute, &mfc_unres_lock, &filter);
2560 static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2561 [RTA_SRC] = { .type = NLA_U32 },
2562 [RTA_DST] = { .type = NLA_U32 },
2563 [RTA_IIF] = { .type = NLA_U32 },
2564 [RTA_TABLE] = { .type = NLA_U32 },
2565 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2568 static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2570 switch (rtm_protocol) {
2571 case RTPROT_STATIC:
2572 case RTPROT_MROUTED:
2573 return true;
2575 return false;
2578 static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2580 struct rtnexthop *rtnh = nla_data(nla);
2581 int remaining = nla_len(nla), vifi = 0;
2583 while (rtnh_ok(rtnh, remaining)) {
2584 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2585 if (++vifi == MAXVIFS)
2586 break;
2587 rtnh = rtnh_next(rtnh, &remaining);
2590 return remaining > 0 ? -EINVAL : vifi;
2593 /* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2594 static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2595 struct mfcctl *mfcc, int *mrtsock,
2596 struct mr_table **mrtret,
2597 struct netlink_ext_ack *extack)
2599 struct net_device *dev = NULL;
2600 u32 tblid = RT_TABLE_DEFAULT;
2601 struct mr_table *mrt;
2602 struct nlattr *attr;
2603 struct rtmsg *rtm;
2604 int ret, rem;
2606 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
2607 extack);
2608 if (ret < 0)
2609 goto out;
2610 rtm = nlmsg_data(nlh);
2612 ret = -EINVAL;
2613 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2614 rtm->rtm_type != RTN_MULTICAST ||
2615 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2616 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2617 goto out;
2619 memset(mfcc, 0, sizeof(*mfcc));
2620 mfcc->mfcc_parent = -1;
2621 ret = 0;
2622 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2623 switch (nla_type(attr)) {
2624 case RTA_SRC:
2625 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2626 break;
2627 case RTA_DST:
2628 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2629 break;
2630 case RTA_IIF:
2631 dev = __dev_get_by_index(net, nla_get_u32(attr));
2632 if (!dev) {
2633 ret = -ENODEV;
2634 goto out;
2636 break;
2637 case RTA_MULTIPATH:
2638 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2639 ret = -EINVAL;
2640 goto out;
2642 break;
2643 case RTA_PREFSRC:
2644 ret = 1;
2645 break;
2646 case RTA_TABLE:
2647 tblid = nla_get_u32(attr);
2648 break;
2651 mrt = ipmr_get_table(net, tblid);
2652 if (!mrt) {
2653 ret = -ENOENT;
2654 goto out;
2656 *mrtret = mrt;
2657 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2658 if (dev)
2659 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2661 out:
2662 return ret;
2665 /* takes care of both newroute and delroute */
2666 static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2667 struct netlink_ext_ack *extack)
2669 struct net *net = sock_net(skb->sk);
2670 int ret, mrtsock, parent;
2671 struct mr_table *tbl;
2672 struct mfcctl mfcc;
2674 mrtsock = 0;
2675 tbl = NULL;
2676 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
2677 if (ret < 0)
2678 return ret;
2680 parent = ret ? mfcc.mfcc_parent : -1;
2681 if (nlh->nlmsg_type == RTM_NEWROUTE)
2682 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2683 else
2684 return ipmr_mfc_delete(tbl, &mfcc, parent);
2687 static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2689 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2691 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2692 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2693 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2694 mrt->mroute_reg_vif_num) ||
2695 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2696 mrt->mroute_do_assert) ||
2697 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
2698 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
2699 mrt->mroute_do_wrvifwhole))
2700 return false;
2702 return true;
2705 static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2707 struct nlattr *vif_nest;
2708 struct vif_device *vif;
2710 /* if the VIF doesn't exist just continue */
2711 if (!VIF_EXISTS(mrt, vifid))
2712 return true;
2714 vif = &mrt->vif_table[vifid];
2715 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2716 if (!vif_nest)
2717 return false;
2718 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2719 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2720 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2721 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2722 IPMRA_VIFA_PAD) ||
2723 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2724 IPMRA_VIFA_PAD) ||
2725 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2726 IPMRA_VIFA_PAD) ||
2727 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2728 IPMRA_VIFA_PAD) ||
2729 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2730 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2731 nla_nest_cancel(skb, vif_nest);
2732 return false;
2734 nla_nest_end(skb, vif_nest);
2736 return true;
2739 static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
2740 struct netlink_ext_ack *extack)
2742 struct ifinfomsg *ifm;
2744 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2745 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
2746 return -EINVAL;
2749 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
2750 NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump");
2751 return -EINVAL;
2754 ifm = nlmsg_data(nlh);
2755 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2756 ifm->ifi_change || ifm->ifi_index) {
2757 NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
2758 return -EINVAL;
2761 return 0;
2764 static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2766 struct net *net = sock_net(skb->sk);
2767 struct nlmsghdr *nlh = NULL;
2768 unsigned int t = 0, s_t;
2769 unsigned int e = 0, s_e;
2770 struct mr_table *mrt;
2772 if (cb->strict_check) {
2773 int err = ipmr_valid_dumplink(cb->nlh, cb->extack);
2775 if (err < 0)
2776 return err;
2779 s_t = cb->args[0];
2780 s_e = cb->args[1];
2782 ipmr_for_each_table(mrt, net) {
2783 struct nlattr *vifs, *af;
2784 struct ifinfomsg *hdr;
2785 u32 i;
2787 if (t < s_t)
2788 goto skip_table;
2789 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2790 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2791 sizeof(*hdr), NLM_F_MULTI);
2792 if (!nlh)
2793 break;
2795 hdr = nlmsg_data(nlh);
2796 memset(hdr, 0, sizeof(*hdr));
2797 hdr->ifi_family = RTNL_FAMILY_IPMR;
2799 af = nla_nest_start(skb, IFLA_AF_SPEC);
2800 if (!af) {
2801 nlmsg_cancel(skb, nlh);
2802 goto out;
2805 if (!ipmr_fill_table(mrt, skb)) {
2806 nlmsg_cancel(skb, nlh);
2807 goto out;
2810 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2811 if (!vifs) {
2812 nla_nest_end(skb, af);
2813 nlmsg_end(skb, nlh);
2814 goto out;
2816 for (i = 0; i < mrt->maxvif; i++) {
2817 if (e < s_e)
2818 goto skip_entry;
2819 if (!ipmr_fill_vif(mrt, i, skb)) {
2820 nla_nest_end(skb, vifs);
2821 nla_nest_end(skb, af);
2822 nlmsg_end(skb, nlh);
2823 goto out;
2825 skip_entry:
2826 e++;
2828 s_e = 0;
2829 e = 0;
2830 nla_nest_end(skb, vifs);
2831 nla_nest_end(skb, af);
2832 nlmsg_end(skb, nlh);
2833 skip_table:
2834 t++;
2837 out:
2838 cb->args[1] = e;
2839 cb->args[0] = t;
2841 return skb->len;
2844 #ifdef CONFIG_PROC_FS
2845 /* The /proc interfaces to multicast routing :
2846 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
2849 static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
2850 __acquires(mrt_lock)
2852 struct mr_vif_iter *iter = seq->private;
2853 struct net *net = seq_file_net(seq);
2854 struct mr_table *mrt;
2856 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2857 if (!mrt)
2858 return ERR_PTR(-ENOENT);
2860 iter->mrt = mrt;
2862 read_lock(&mrt_lock);
2863 return mr_vif_seq_start(seq, pos);
2866 static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
2867 __releases(mrt_lock)
2869 read_unlock(&mrt_lock);
2872 static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2874 struct mr_vif_iter *iter = seq->private;
2875 struct mr_table *mrt = iter->mrt;
2877 if (v == SEQ_START_TOKEN) {
2878 seq_puts(seq,
2879 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2880 } else {
2881 const struct vif_device *vif = v;
2882 const char *name = vif->dev ?
2883 vif->dev->name : "none";
2885 seq_printf(seq,
2886 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
2887 vif - mrt->vif_table,
2888 name, vif->bytes_in, vif->pkt_in,
2889 vif->bytes_out, vif->pkt_out,
2890 vif->flags, vif->local, vif->remote);
2892 return 0;
2895 static const struct seq_operations ipmr_vif_seq_ops = {
2896 .start = ipmr_vif_seq_start,
2897 .next = mr_vif_seq_next,
2898 .stop = ipmr_vif_seq_stop,
2899 .show = ipmr_vif_seq_show,
2902 static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2904 struct net *net = seq_file_net(seq);
2905 struct mr_table *mrt;
2907 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
2908 if (!mrt)
2909 return ERR_PTR(-ENOENT);
2911 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
2914 static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2916 int n;
2918 if (v == SEQ_START_TOKEN) {
2919 seq_puts(seq,
2920 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2921 } else {
2922 const struct mfc_cache *mfc = v;
2923 const struct mr_mfc_iter *it = seq->private;
2924 const struct mr_table *mrt = it->mrt;
2926 seq_printf(seq, "%08X %08X %-3hd",
2927 (__force u32) mfc->mfc_mcastgrp,
2928 (__force u32) mfc->mfc_origin,
2929 mfc->_c.mfc_parent);
2931 if (it->cache != &mrt->mfc_unres_queue) {
2932 seq_printf(seq, " %8lu %8lu %8lu",
2933 mfc->_c.mfc_un.res.pkt,
2934 mfc->_c.mfc_un.res.bytes,
2935 mfc->_c.mfc_un.res.wrong_if);
2936 for (n = mfc->_c.mfc_un.res.minvif;
2937 n < mfc->_c.mfc_un.res.maxvif; n++) {
2938 if (VIF_EXISTS(mrt, n) &&
2939 mfc->_c.mfc_un.res.ttls[n] < 255)
2940 seq_printf(seq,
2941 " %2d:%-3d",
2942 n, mfc->_c.mfc_un.res.ttls[n]);
2944 } else {
2945 /* unresolved mfc_caches don't contain
2946 * pkt, bytes and wrong_if values
2948 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
2950 seq_putc(seq, '\n');
2952 return 0;
2955 static const struct seq_operations ipmr_mfc_seq_ops = {
2956 .start = ipmr_mfc_seq_start,
2957 .next = mr_mfc_seq_next,
2958 .stop = mr_mfc_seq_stop,
2959 .show = ipmr_mfc_seq_show,
2961 #endif
2963 #ifdef CONFIG_IP_PIMSM_V2
2964 static const struct net_protocol pim_protocol = {
2965 .handler = pim_rcv,
2966 .netns_ok = 1,
2968 #endif
2970 static unsigned int ipmr_seq_read(struct net *net)
2972 ASSERT_RTNL();
2974 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
2977 static int ipmr_dump(struct net *net, struct notifier_block *nb)
2979 return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
2980 ipmr_mr_table_iter, &mrt_lock);
2983 static const struct fib_notifier_ops ipmr_notifier_ops_template = {
2984 .family = RTNL_FAMILY_IPMR,
2985 .fib_seq_read = ipmr_seq_read,
2986 .fib_dump = ipmr_dump,
2987 .owner = THIS_MODULE,
2990 static int __net_init ipmr_notifier_init(struct net *net)
2992 struct fib_notifier_ops *ops;
2994 net->ipv4.ipmr_seq = 0;
2996 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
2997 if (IS_ERR(ops))
2998 return PTR_ERR(ops);
2999 net->ipv4.ipmr_notifier_ops = ops;
3001 return 0;
3004 static void __net_exit ipmr_notifier_exit(struct net *net)
3006 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3007 net->ipv4.ipmr_notifier_ops = NULL;
3010 /* Setup for IP multicast routing */
3011 static int __net_init ipmr_net_init(struct net *net)
3013 int err;
3015 err = ipmr_notifier_init(net);
3016 if (err)
3017 goto ipmr_notifier_fail;
3019 err = ipmr_rules_init(net);
3020 if (err < 0)
3021 goto ipmr_rules_fail;
3023 #ifdef CONFIG_PROC_FS
3024 err = -ENOMEM;
3025 if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
3026 sizeof(struct mr_vif_iter)))
3027 goto proc_vif_fail;
3028 if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
3029 sizeof(struct mr_mfc_iter)))
3030 goto proc_cache_fail;
3031 #endif
3032 return 0;
3034 #ifdef CONFIG_PROC_FS
3035 proc_cache_fail:
3036 remove_proc_entry("ip_mr_vif", net->proc_net);
3037 proc_vif_fail:
3038 ipmr_rules_exit(net);
3039 #endif
3040 ipmr_rules_fail:
3041 ipmr_notifier_exit(net);
3042 ipmr_notifier_fail:
3043 return err;
3046 static void __net_exit ipmr_net_exit(struct net *net)
3048 #ifdef CONFIG_PROC_FS
3049 remove_proc_entry("ip_mr_cache", net->proc_net);
3050 remove_proc_entry("ip_mr_vif", net->proc_net);
3051 #endif
3052 ipmr_notifier_exit(net);
3053 ipmr_rules_exit(net);
3056 static struct pernet_operations ipmr_net_ops = {
3057 .init = ipmr_net_init,
3058 .exit = ipmr_net_exit,
3061 int __init ip_mr_init(void)
3063 int err;
3065 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3066 sizeof(struct mfc_cache),
3067 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
3068 NULL);
3070 err = register_pernet_subsys(&ipmr_net_ops);
3071 if (err)
3072 goto reg_pernet_fail;
3074 err = register_netdevice_notifier(&ip_mr_notifier);
3075 if (err)
3076 goto reg_notif_fail;
3077 #ifdef CONFIG_IP_PIMSM_V2
3078 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
3079 pr_err("%s: can't add PIM protocol\n", __func__);
3080 err = -EAGAIN;
3081 goto add_proto_fail;
3083 #endif
3084 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
3085 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
3086 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
3087 ipmr_rtm_route, NULL, 0);
3088 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
3089 ipmr_rtm_route, NULL, 0);
3091 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
3092 NULL, ipmr_rtm_dumplink, 0);
3093 return 0;
3095 #ifdef CONFIG_IP_PIMSM_V2
3096 add_proto_fail:
3097 unregister_netdevice_notifier(&ip_mr_notifier);
3098 #endif
3099 reg_notif_fail:
3100 unregister_pernet_subsys(&ipmr_net_ops);
3101 reg_pernet_fail:
3102 kmem_cache_destroy(mrt_cachep);
3103 return err;