blob: 313470f6bb148326b4afbc00d265b6a1e40d93bd [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * IP multicast routing support for mrouted 3.6/3.8
4 *
Alan Cox113aa832008-10-13 19:01:08 -07005 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Linux Consultancy and Custom Driver Development
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Fixes:
9 * Michael Chastain : Incorrect size of copying.
10 * Alan Cox : Added the cache manager code
11 * Alan Cox : Fixed the clone/copy bug and device race.
12 * Mike McLagan : Routing by source
13 * Malcolm Beattie : Buffer handling fixes.
14 * Alexey Kuznetsov : Double buffer free and other fixes.
15 * SVR Anand : Fixed several multicast bugs and problems.
16 * Alexey Kuznetsov : Status, optimisations and more.
17 * Brad Parker : Better behaviour on mrouted upcall
18 * overflow.
19 * Carlos Picoto : PIMv1 Support
20 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
Gilles Espinassef77f13e2010-03-29 15:41:47 +020021 * Relax this requirement to work with older peers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 */
23
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080024#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/types.h>
Alexey Dobriyan08009a72018-02-24 21:20:33 +030026#include <linux/cache.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080027#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/mm.h>
30#include <linux/kernel.h>
31#include <linux/fcntl.h>
32#include <linux/stat.h>
33#include <linux/socket.h>
34#include <linux/in.h>
35#include <linux/inet.h>
36#include <linux/netdevice.h>
37#include <linux/inetdevice.h>
38#include <linux/igmp.h>
39#include <linux/proc_fs.h>
40#include <linux/seq_file.h>
41#include <linux/mroute.h>
42#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080043#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020045#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <net/ip.h>
47#include <net/protocol.h>
48#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020049#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#include <net/icmp.h>
51#include <net/udp.h>
52#include <net/raw.h>
53#include <linux/notifier.h>
54#include <linux/if_arp.h>
55#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000056#include <linux/compat.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040057#include <linux/export.h>
NeilBrown0eb71a92018-06-18 12:52:50 +100058#include <linux/rhashtable.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000059#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070061#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000062#include <net/fib_rules.h>
Nicolas Dichteld67b8c62012-12-04 01:13:35 +000063#include <linux/netconf.h>
David Ahern3c618c12019-04-20 09:28:20 -070064#include <net/rtnh.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Gustavo A. R. Silva56484512018-12-10 12:41:24 -060066#include <linux/nospec.h>
67
Patrick McHardyf0ad0862010-04-13 05:03:23 +000068struct ipmr_rule {
69 struct fib_rule common;
70};
71
72struct ipmr_result {
73 struct mr_table *mrt;
74};
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000077 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 */
79
80static DEFINE_RWLOCK(mrt_lock);
81
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +010082/* Multicast router control variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/* Special spinlock for queue of unresolved entries */
85static DEFINE_SPINLOCK(mfc_unres_lock);
86
87/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000088 * entries is changed only in process context and protected
89 * with weak lock mrt_lock. Queue of unresolved entries is protected
90 * with strong spinlock mfc_unres_lock.
91 *
92 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 */
94
Alexey Dobriyan08009a72018-02-24 21:20:33 +030095static struct kmem_cache *mrt_cachep __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Patrick McHardyf0ad0862010-04-13 05:03:23 +000097static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +000098static void ipmr_free_table(struct mr_table *mrt);
99
Rami Rosenc4854ec2013-07-20 15:09:28 +0300100static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -0400101 struct net_device *dev, struct sk_buff *skb,
102 struct mfc_cache *cache, int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000103static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000104 struct sk_buff *pkt, vifi_t vifi, int assert);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000105static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
106 int cmd);
Julien Gomes5a645dd2017-06-20 13:54:17 -0700107static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
Callum Sinclairca8d4792019-02-18 10:07:52 +1300108static void mroute_clean_tables(struct mr_table *mrt, int flags);
Kees Cooke99e88a2017-10-16 14:43:17 -0700109static void ipmr_expire_process(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000111#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
112#define ipmr_for_each_table(mrt, net) \
113 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
114
Yuval Mintz7b0db852018-02-28 23:29:39 +0200115static struct mr_table *ipmr_mr_table_iter(struct net *net,
116 struct mr_table *mrt)
117{
118 struct mr_table *ret;
119
120 if (!mrt)
121 ret = list_entry_rcu(net->ipv4.mr_tables.next,
122 struct mr_table, list);
123 else
124 ret = list_entry_rcu(mrt->list.next,
125 struct mr_table, list);
126
127 if (&ret->list == &net->ipv4.mr_tables)
128 return NULL;
129 return ret;
130}
131
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000132static struct mr_table *ipmr_get_table(struct net *net, u32 id)
133{
134 struct mr_table *mrt;
135
136 ipmr_for_each_table(mrt, net) {
137 if (mrt->id == id)
138 return mrt;
139 }
140 return NULL;
141}
142
David S. Millerda919812011-03-12 02:04:50 -0500143static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000144 struct mr_table **mrt)
145{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000146 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100147 struct ipmr_result res;
148 struct fib_lookup_arg arg = {
149 .result = &res,
150 .flags = FIB_LOOKUP_NOREF,
151 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000152
David Aherne58e4152016-10-31 15:54:00 -0700153 /* update flow if oif or iif point to device enslaved to l3mdev */
154 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
155
David S. Millerda919812011-03-12 02:04:50 -0500156 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
157 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000158 if (err < 0)
159 return err;
160 *mrt = res.mrt;
161 return 0;
162}
163
164static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
165 int flags, struct fib_lookup_arg *arg)
166{
167 struct ipmr_result *res = arg->result;
168 struct mr_table *mrt;
169
170 switch (rule->action) {
171 case FR_ACT_TO_TBL:
172 break;
173 case FR_ACT_UNREACHABLE:
174 return -ENETUNREACH;
175 case FR_ACT_PROHIBIT:
176 return -EACCES;
177 case FR_ACT_BLACKHOLE:
178 default:
179 return -EINVAL;
180 }
181
David Aherne58e4152016-10-31 15:54:00 -0700182 arg->table = fib_rule_get_table(rule, arg);
183
184 mrt = ipmr_get_table(rule->fr_net, arg->table);
Ian Morris51456b22015-04-03 09:17:26 +0100185 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000186 return -EAGAIN;
187 res->mrt = mrt;
188 return 0;
189}
190
191static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
192{
193 return 1;
194}
195
196static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
197 FRA_GENERIC_POLICY,
198};
199
200static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
Roopa Prabhub16fb412018-04-21 09:41:31 -0700201 struct fib_rule_hdr *frh, struct nlattr **tb,
202 struct netlink_ext_ack *extack)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000203{
204 return 0;
205}
206
207static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
208 struct nlattr **tb)
209{
210 return 1;
211}
212
213static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
214 struct fib_rule_hdr *frh)
215{
216 frh->dst_len = 0;
217 frh->src_len = 0;
218 frh->tos = 0;
219 return 0;
220}
221
Andi Kleen04a6f822012-10-04 17:12:11 -0700222static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200223 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000224 .rule_size = sizeof(struct ipmr_rule),
225 .addr_size = sizeof(u32),
226 .action = ipmr_rule_action,
227 .match = ipmr_rule_match,
228 .configure = ipmr_rule_configure,
229 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000230 .fill = ipmr_rule_fill,
231 .nlgroup = RTNLGRP_IPV4_RULE,
232 .policy = ipmr_rule_policy,
233 .owner = THIS_MODULE,
234};
235
236static int __net_init ipmr_rules_init(struct net *net)
237{
238 struct fib_rules_ops *ops;
239 struct mr_table *mrt;
240 int err;
241
242 ops = fib_rules_register(&ipmr_rules_ops_template, net);
243 if (IS_ERR(ops))
244 return PTR_ERR(ops);
245
246 INIT_LIST_HEAD(&net->ipv4.mr_tables);
247
248 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100249 if (IS_ERR(mrt)) {
250 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000251 goto err1;
252 }
253
254 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
255 if (err < 0)
256 goto err2;
257
258 net->ipv4.mr_rules_ops = ops;
259 return 0;
260
261err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700262 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000263err1:
264 fib_rules_unregister(ops);
265 return err;
266}
267
268static void __net_exit ipmr_rules_exit(struct net *net)
269{
270 struct mr_table *mrt, *next;
271
WANG Conged785302015-03-31 11:01:45 -0700272 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000273 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
274 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000275 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000276 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000277 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700278 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000279}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200280
281static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
282{
283 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR);
284}
285
286static unsigned int ipmr_rules_seq_read(struct net *net)
287{
288 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
289}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200290
291bool ipmr_rule_default(const struct fib_rule *rule)
292{
293 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
294}
295EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000296#else
297#define ipmr_for_each_table(mrt, net) \
298 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
299
Yuval Mintz7b0db852018-02-28 23:29:39 +0200300static struct mr_table *ipmr_mr_table_iter(struct net *net,
301 struct mr_table *mrt)
302{
303 if (!mrt)
304 return net->ipv4.mrt;
305 return NULL;
306}
307
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000308static struct mr_table *ipmr_get_table(struct net *net, u32 id)
309{
310 return net->ipv4.mrt;
311}
312
David S. Millerda919812011-03-12 02:04:50 -0500313static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000314 struct mr_table **mrt)
315{
316 *mrt = net->ipv4.mrt;
317 return 0;
318}
319
320static int __net_init ipmr_rules_init(struct net *net)
321{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100322 struct mr_table *mrt;
323
324 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
325 if (IS_ERR(mrt))
326 return PTR_ERR(mrt);
327 net->ipv4.mrt = mrt;
328 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000329}
330
331static void __net_exit ipmr_rules_exit(struct net *net)
332{
WANG Conged785302015-03-31 11:01:45 -0700333 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000334 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700335 net->ipv4.mrt = NULL;
336 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000337}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200338
339static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
340{
341 return 0;
342}
343
344static unsigned int ipmr_rules_seq_read(struct net *net)
345{
346 return 0;
347}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200348
349bool ipmr_rule_default(const struct fib_rule *rule)
350{
351 return true;
352}
353EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000354#endif
355
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100356static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
357 const void *ptr)
358{
359 const struct mfc_cache_cmp_arg *cmparg = arg->key;
360 struct mfc_cache *c = (struct mfc_cache *)ptr;
361
362 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
363 cmparg->mfc_origin != c->mfc_origin;
364}
365
366static const struct rhashtable_params ipmr_rht_params = {
Yuval Mintz494fff52018-02-28 23:29:34 +0200367 .head_offset = offsetof(struct mr_mfc, mnode),
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100368 .key_offset = offsetof(struct mfc_cache, cmparg),
369 .key_len = sizeof(struct mfc_cache_cmp_arg),
370 .nelem_hint = 3,
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100371 .obj_cmpfn = ipmr_hash_cmp,
372 .automatic_shrinking = true,
373};
374
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200375static void ipmr_new_table_set(struct mr_table *mrt,
376 struct net *net)
377{
378#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
379 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
380#endif
381}
382
Yuval Mintz845c9a72018-02-28 23:29:35 +0200383static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
384 .mfc_mcastgrp = htonl(INADDR_ANY),
385 .mfc_origin = htonl(INADDR_ANY),
386};
387
388static struct mr_table_ops ipmr_mr_table_ops = {
389 .rht_params = &ipmr_rht_params,
390 .cmparg_any = &ipmr_mr_table_ops_cmparg_any,
391};
392
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000393static struct mr_table *ipmr_new_table(struct net *net, u32 id)
394{
395 struct mr_table *mrt;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000396
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100397 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
398 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
399 return ERR_PTR(-EINVAL);
400
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000401 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100402 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000403 return mrt;
404
Yuval Mintz845c9a72018-02-28 23:29:35 +0200405 return mr_table_alloc(net, id, &ipmr_mr_table_ops,
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200406 ipmr_expire_process, ipmr_new_table_set);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000407}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000409static void ipmr_free_table(struct mr_table *mrt)
410{
411 del_timer_sync(&mrt->ipmr_expire_timer);
Callum Sinclairca8d4792019-02-18 10:07:52 +1300412 mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC |
413 MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100414 rhltable_destroy(&mrt->mfc_hash);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000415 kfree(mrt);
416}
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
419
Wang Chend6070322008-07-14 20:55:26 -0700420static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
421{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000422 struct net *net = dev_net(dev);
423
Wang Chend6070322008-07-14 20:55:26 -0700424 dev_close(dev);
425
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000426 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700427 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800428 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700429 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700430 struct ip_tunnel_parm p;
431
432 memset(&p, 0, sizeof(p));
433 p.iph.daddr = v->vifc_rmt_addr.s_addr;
434 p.iph.saddr = v->vifc_lcl_addr.s_addr;
435 p.iph.version = 4;
436 p.iph.ihl = 5;
437 p.iph.protocol = IPPROTO_IPIP;
438 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
439 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
440
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800441 if (ops->ndo_do_ioctl) {
442 mm_segment_t oldfs = get_fs();
443
444 set_fs(KERNEL_DS);
445 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
446 set_fs(oldfs);
447 }
Wang Chend6070322008-07-14 20:55:26 -0700448 }
449}
450
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100451/* Initialize ipmr pimreg/tunnel in_device */
452static bool ipmr_init_vif_indev(const struct net_device *dev)
453{
454 struct in_device *in_dev;
455
456 ASSERT_RTNL();
457
458 in_dev = __in_dev_get_rtnl(dev);
459 if (!in_dev)
460 return false;
461 ipv4_devconf_setall(in_dev);
462 neigh_parms_data_state_setall(in_dev->arp_parms);
463 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
464
465 return true;
466}
467
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100468static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469{
470 struct net_device *dev;
471
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000472 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800475 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 int err;
477 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 memset(&p, 0, sizeof(p));
481 p.iph.daddr = v->vifc_rmt_addr.s_addr;
482 p.iph.saddr = v->vifc_lcl_addr.s_addr;
483 p.iph.version = 4;
484 p.iph.ihl = 5;
485 p.iph.protocol = IPPROTO_IPIP;
486 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800487 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800489 if (ops->ndo_do_ioctl) {
490 mm_segment_t oldfs = get_fs();
491
492 set_fs(KERNEL_DS);
493 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
494 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000495 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800496 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000497 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 dev = NULL;
499
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000500 if (err == 0 &&
501 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100503 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 goto failure;
Petr Machata00f54e62018-12-06 17:05:36 +0000505 if (dev_open(dev, NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700507 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
509 }
510 return dev;
511
512failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 unregister_netdevice(dev);
514 return NULL;
515}
516
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100517#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000518static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000520 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000521 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500522 struct flowi4 fl4 = {
523 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700524 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500525 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000526 };
527 int err;
528
David S. Millerda919812011-03-12 02:04:50 -0500529 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000530 if (err < 0) {
531 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000532 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000533 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000534
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700536 dev->stats.tx_bytes += skb->len;
537 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000538 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 read_unlock(&mrt_lock);
540 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000541 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542}
543
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200544static int reg_vif_get_iflink(const struct net_device *dev)
545{
546 return 0;
547}
548
Stephen Hemminger007c3832008-11-20 20:28:35 -0800549static const struct net_device_ops reg_vif_netdev_ops = {
550 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200551 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800552};
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static void reg_vif_setup(struct net_device *dev)
555{
556 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800557 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530559 dev->netdev_ops = &reg_vif_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400560 dev->needs_free_netdev = true;
Tom Goff403dbb92009-06-14 03:16:13 -0700561 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562}
563
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000564static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565{
566 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000567 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000569 if (mrt->id == RT_TABLE_DEFAULT)
570 sprintf(name, "pimreg");
571 else
572 sprintf(name, "pimreg%u", mrt->id);
573
Tom Gundersenc835a672014-07-14 16:37:24 +0200574 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Ian Morris51456b22015-04-03 09:17:26 +0100576 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 return NULL;
578
Tom Goff403dbb92009-06-14 03:16:13 -0700579 dev_net_set(dev, net);
580
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 if (register_netdevice(dev)) {
582 free_netdev(dev);
583 return NULL;
584 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100586 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 goto failure;
Petr Machata00f54e62018-12-06 17:05:36 +0000588 if (dev_open(dev, NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 goto failure;
590
Wang Chen7dc00c82008-07-14 20:56:34 -0700591 dev_hold(dev);
592
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 return dev;
594
595failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596 unregister_netdevice(dev);
597 return NULL;
598}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100599
600/* called with rcu_read_lock() */
601static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
602 unsigned int pimlen)
603{
604 struct net_device *reg_dev = NULL;
605 struct iphdr *encap;
606
607 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100608 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100609 * a. packet is really sent to a multicast group
610 * b. packet is not a NULL-REGISTER
611 * c. packet is not truncated
612 */
613 if (!ipv4_is_multicast(encap->daddr) ||
614 encap->tot_len == 0 ||
615 ntohs(encap->tot_len) + pimlen > skb->len)
616 return 1;
617
618 read_lock(&mrt_lock);
619 if (mrt->mroute_reg_vif_num >= 0)
620 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
621 read_unlock(&mrt_lock);
622
623 if (!reg_dev)
624 return 1;
625
626 skb->mac_header = skb->network_header;
627 skb_pull(skb, (u8 *)encap - skb->data);
628 skb_reset_network_header(skb);
629 skb->protocol = htons(ETH_P_IP);
630 skb->ip_summed = CHECKSUM_NONE;
631
632 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
633
634 netif_rx(skb);
635
636 return NET_RX_SUCCESS;
637}
638#else
639static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
640{
641 return NULL;
642}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643#endif
644
Yotam Gigib3620532017-09-27 08:23:14 +0200645static int call_ipmr_vif_entry_notifiers(struct net *net,
646 enum fib_event_type event_type,
647 struct vif_device *vif,
648 vifi_t vif_index, u32 tb_id)
649{
Yuval Mintzbc67a0d2018-03-26 15:01:31 +0300650 return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
651 vif, vif_index, tb_id,
652 &net->ipv4.ipmr_seq);
Yotam Gigib3620532017-09-27 08:23:14 +0200653}
654
Yotam Gigib3620532017-09-27 08:23:14 +0200655static int call_ipmr_mfc_entry_notifiers(struct net *net,
656 enum fib_event_type event_type,
657 struct mfc_cache *mfc, u32 tb_id)
658{
Yuval Mintz54c4cad2018-03-26 15:01:32 +0300659 return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
660 &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
Yotam Gigib3620532017-09-27 08:23:14 +0200661}
662
Ben Hutchings2c530402012-07-10 10:55:09 +0000663/**
664 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700665 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000667static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000668 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669{
Yotam Gigib3620532017-09-27 08:23:14 +0200670 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct vif_device *v;
672 struct net_device *dev;
673 struct in_device *in_dev;
674
Patrick McHardy0c122952010-04-13 05:03:22 +0000675 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 return -EADDRNOTAVAIL;
677
Patrick McHardy0c122952010-04-13 05:03:22 +0000678 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
Yotam Gigib3620532017-09-27 08:23:14 +0200680 if (VIF_EXISTS(mrt, vifi))
681 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
682 mrt->id);
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 write_lock_bh(&mrt_lock);
685 dev = v->dev;
686 v->dev = NULL;
687
688 if (!dev) {
689 write_unlock_bh(&mrt_lock);
690 return -EADDRNOTAVAIL;
691 }
692
Patrick McHardy0c122952010-04-13 05:03:22 +0000693 if (vifi == mrt->mroute_reg_vif_num)
694 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000696 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000698
699 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000700 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 break;
702 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000703 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 }
705
706 write_unlock_bh(&mrt_lock);
707
708 dev_set_allmulti(dev, -1);
709
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000710 in_dev = __in_dev_get_rtnl(dev);
711 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700712 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -0700713 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000714 NETCONFA_MC_FORWARDING,
715 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 ip_rt_multicast_event(in_dev);
717 }
718
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000719 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000720 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 dev_put(dev);
723 return 0;
724}
725
Eric Dumazeta8c94862010-10-01 16:15:08 +0000726static void ipmr_cache_free_rcu(struct rcu_head *head)
727{
Yuval Mintz494fff52018-02-28 23:29:34 +0200728 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000729
Yuval Mintz494fff52018-02-28 23:29:34 +0200730 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000731}
732
Yuval Mintz8c13af22018-03-26 15:01:36 +0300733static void ipmr_cache_free(struct mfc_cache *c)
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000734{
Yuval Mintz494fff52018-02-28 23:29:34 +0200735 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000739 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000741static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200743 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700745 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Patrick McHardy0c122952010-04-13 05:03:22 +0000747 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Yuval Mintz494fff52018-02-28 23:29:34 +0200749 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700750 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200751 struct nlmsghdr *nlh = skb_pull(skb,
752 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000754 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000756 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700757 e->error = -ETIMEDOUT;
758 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700759
Eric W. Biederman15e47302012-09-07 20:12:54 +0000760 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000761 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000766 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
Patrick McHardye258beb2010-04-13 05:03:19 +0000769/* Timer process for the unresolved queue. */
Kees Cooke99e88a2017-10-16 14:43:17 -0700770static void ipmr_expire_process(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Kees Cooke99e88a2017-10-16 14:43:17 -0700772 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
Yuval Mintz494fff52018-02-28 23:29:34 +0200773 struct mr_mfc *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 unsigned long expires;
Yuval Mintz494fff52018-02-28 23:29:34 +0200775 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
777 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000778 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 return;
780 }
781
Patrick McHardy0c122952010-04-13 05:03:22 +0000782 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 goto out;
784
785 now = jiffies;
786 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Patrick McHardy0c122952010-04-13 05:03:22 +0000788 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 if (time_after(c->mfc_un.unres.expires, now)) {
790 unsigned long interval = c->mfc_un.unres.expires - now;
791 if (interval < expires)
792 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 continue;
794 }
795
Patrick McHardy862465f2010-04-13 05:03:21 +0000796 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +0200797 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
798 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 }
800
Patrick McHardy0c122952010-04-13 05:03:22 +0000801 if (!list_empty(&mrt->mfc_unres_queue))
802 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803
804out:
805 spin_unlock(&mfc_unres_lock);
806}
807
808/* Fill oifs list. It is called under write locked mrt_lock. */
Yuval Mintz494fff52018-02-28 23:29:34 +0200809static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000810 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 int vifi;
813
814 cache->mfc_un.res.minvif = MAXVIFS;
815 cache->mfc_un.res.maxvif = 0;
816 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
817
Patrick McHardy0c122952010-04-13 05:03:22 +0000818 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
819 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000820 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
822 if (cache->mfc_un.res.minvif > vifi)
823 cache->mfc_un.res.minvif = vifi;
824 if (cache->mfc_un.res.maxvif <= vifi)
825 cache->mfc_un.res.maxvif = vifi + 1;
826 }
827 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200828 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829}
830
Patrick McHardy0c122952010-04-13 05:03:22 +0000831static int vif_add(struct net *net, struct mr_table *mrt,
832 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
Florian Fainellibccb3022019-02-06 09:45:46 -0800834 struct netdev_phys_item_id ppid = { };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000836 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 struct net_device *dev;
838 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700839 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000842 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 return -EADDRINUSE;
844
845 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100847 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100848 return -EINVAL;
849 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 * All the packets will be sent to the daemon
851 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000852 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000854 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 if (!dev)
856 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700857 err = dev_set_allmulti(dev, 1);
858 if (err) {
859 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700860 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700861 return err;
862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900864 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000865 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 if (!dev)
867 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700868 err = dev_set_allmulti(dev, 1);
869 if (err) {
870 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700871 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700872 return err;
873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000875 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000877 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
878 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100879 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000880 dev_put(dev);
881 return -EADDRNOTAVAIL;
882 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000883 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000884 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 if (!dev)
887 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700888 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700889 if (err) {
890 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700891 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 break;
894 default:
895 return -EINVAL;
896 }
897
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000898 in_dev = __in_dev_get_rtnl(dev);
899 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000900 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000902 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700903 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700904 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
905 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 ip_rt_multicast_event(in_dev);
907
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000908 /* Fill in the VIF structures */
Yuval Mintz6853f212018-02-28 23:29:29 +0200909 vif_device_init(v, dev, vifc->vifc_rate_limit,
910 vifc->vifc_threshold,
911 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
912 (VIFF_TUNNEL | VIFF_REGISTER));
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000913
Florian Fainellibccb3022019-02-06 09:45:46 -0800914 err = dev_get_port_parent_id(dev, &ppid, true);
915 if (err == 0) {
916 memcpy(v->dev_parent_id.id, ppid.id, ppid.id_len);
917 v->dev_parent_id.id_len = ppid.id_len;
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200918 } else {
919 v->dev_parent_id.id_len = 0;
920 }
Yuval Mintz6853f212018-02-28 23:29:29 +0200921
Jianjun Kongc354e122008-11-03 00:28:02 -0800922 v->local = vifc->vifc_lcl_addr.s_addr;
923 v->remote = vifc->vifc_rmt_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 /* And finish update writing critical data */
926 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800927 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000928 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000929 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000930 if (vifi+1 > mrt->maxvif)
931 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +0200933 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 return 0;
935}
936
Eric Dumazeta8c94862010-10-01 16:15:08 +0000937/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000938static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000939 __be32 origin,
940 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100942 struct mfc_cache_cmp_arg arg = {
943 .mfc_mcastgrp = mcastgrp,
944 .mfc_origin = origin
945 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Yuval Mintz845c9a72018-02-28 23:29:35 +0200947 return mr_mfc_find(mrt, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000948}
949
950/* Look for a (*,G) entry */
951static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
952 __be32 mcastgrp, int vifi)
953{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100954 struct mfc_cache_cmp_arg arg = {
955 .mfc_mcastgrp = mcastgrp,
956 .mfc_origin = htonl(INADDR_ANY)
957 };
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000958
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100959 if (mcastgrp == htonl(INADDR_ANY))
Yuval Mintz845c9a72018-02-28 23:29:35 +0200960 return mr_mfc_find_any_parent(mrt, vifi);
961 return mr_mfc_find_any(mrt, vifi, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000962}
963
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100964/* Look for a (S,G,iif) entry if parent != -1 */
965static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
966 __be32 origin, __be32 mcastgrp,
967 int parent)
968{
969 struct mfc_cache_cmp_arg arg = {
970 .mfc_mcastgrp = mcastgrp,
971 .mfc_origin = origin,
972 };
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100973
Yuval Mintz845c9a72018-02-28 23:29:35 +0200974 return mr_mfc_find_parent(mrt, &arg, parent);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100975}
976
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100977/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000978static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979{
Jianjun Kongc354e122008-11-03 00:28:02 -0800980 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000981
Tom Goff70a0dec2016-06-23 16:11:57 -0400982 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +0200983 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
984 c->_c.mfc_un.res.minvif = MAXVIFS;
Yuval Mintz8c13af22018-03-26 15:01:36 +0300985 c->_c.free = ipmr_cache_free_rcu;
Yuval Mintz494fff52018-02-28 23:29:34 +0200986 refcount_set(&c->_c.mfc_un.res.refcount, 1);
Tom Goff70a0dec2016-06-23 16:11:57 -0400987 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return c;
989}
990
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000991static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992{
Jianjun Kongc354e122008-11-03 00:28:02 -0800993 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000994
995 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +0200996 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
997 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
Eric Dumazeta8c94862010-10-01 16:15:08 +0000998 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 return c;
1000}
1001
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001002/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +00001003static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1004 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005{
1006 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001007 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001009 /* Play the pending entries through our router */
Yuval Mintz494fff52018-02-28 23:29:34 +02001010 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001011 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +02001012 struct nlmsghdr *nlh = skb_pull(skb,
1013 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Yuval Mintz7b0db852018-02-28 23:29:39 +02001015 if (mr_fill_mroute(mrt, skb, &c->_c,
1016 nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001017 nlh->nlmsg_len = skb_tail_pointer(skb) -
1018 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 } else {
1020 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +00001021 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +00001023 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001024 e->error = -EMSGSIZE;
1025 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
Thomas Graf2942e902006-08-15 00:30:25 -07001027
Eric W. Biederman15e47302012-09-07 20:12:54 +00001028 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001029 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001030 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001031 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033}
1034
Julien Gomes5a645dd2017-06-20 13:54:17 -07001035/* Bounce a cache query up to mrouted and netlink.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001037 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001039static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001040 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001042 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001043 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 struct igmphdr *igmp;
1045 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001046 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 int ret;
1048
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001049 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1051 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 skb = alloc_skb(128, GFP_ATOMIC);
1053
Stephen Hemminger132adf52007-03-08 20:44:43 -08001054 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 return -ENOBUFS;
1056
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001057 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001059 * Duplicate old header, fix ihl, length etc.
1060 * And all this only to mangle msg->im_msgtype and
1061 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001063 skb_push(skb, sizeof(struct iphdr));
1064 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001065 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001066 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001067 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001068 msg->im_msgtype = assert;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 msg->im_mbz = 0;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001070 if (assert == IGMPMSG_WRVIFWHOLE)
1071 msg->im_vif = vifi;
1072 else
1073 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001074 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1075 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1076 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001077 } else {
1078 /* Copy the IP header */
1079 skb_set_network_header(skb, skb->len);
1080 skb_put(skb, ihl);
1081 skb_copy_to_linear_data(skb, pkt->data, ihl);
1082 /* Flag to the kernel this is a route add */
1083 ip_hdr(skb)->protocol = 0;
1084 msg = (struct igmpmsg *)skb_network_header(skb);
1085 msg->im_vif = vifi;
1086 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1087 /* Add our header */
Johannes Berg4df864c2017-06-16 14:29:21 +02001088 igmp = skb_put(skb, sizeof(struct igmphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001089 igmp->type = assert;
1090 msg->im_msgtype = assert;
1091 igmp->code = 0;
1092 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1093 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Eric Dumazet4c9687092010-10-01 16:15:01 +00001096 rcu_read_lock();
1097 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001098 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001099 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 kfree_skb(skb);
1101 return -EINVAL;
1102 }
1103
Julien Gomes5a645dd2017-06-20 13:54:17 -07001104 igmpmsg_netlink_event(mrt, skb);
1105
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001106 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001107 ret = sock_queue_rcv_skb(mroute_sk, skb);
1108 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001109 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001110 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 kfree_skb(skb);
1112 }
1113
1114 return ret;
1115}
1116
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001117/* Queue a packet for resolution. It gets locked cache entry! */
1118static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001119 struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001121 const struct iphdr *iph = ip_hdr(skb);
1122 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001123 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
1126 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001127 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001128 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001129 c->mfc_origin == iph->saddr) {
1130 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001132 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
1134
Patrick McHardy862465f2010-04-13 05:03:21 +00001135 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001136 /* Create a new entry if allowable */
Hangbin Liu0079ad82019-09-06 15:36:01 +08001137 c = ipmr_cache_alloc_unres();
1138 if (!c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 spin_unlock_bh(&mfc_unres_lock);
1140
1141 kfree_skb(skb);
1142 return -ENOBUFS;
1143 }
1144
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001145 /* Fill in the new cache entry */
Yuval Mintz494fff52018-02-28 23:29:34 +02001146 c->_c.mfc_parent = -1;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001147 c->mfc_origin = iph->saddr;
1148 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001150 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001151 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Yuval Mintz494fff52018-02-28 23:29:34 +02001152
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001153 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001154 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 out - Brad Parker
1156 */
1157 spin_unlock_bh(&mfc_unres_lock);
1158
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001159 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 kfree_skb(skb);
1161 return err;
1162 }
1163
Patrick McHardy0c122952010-04-13 05:03:22 +00001164 atomic_inc(&mrt->cache_resolve_queue_len);
Yuval Mintz494fff52018-02-28 23:29:34 +02001165 list_add(&c->_c.list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001166 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
David S. Miller278554b2010-05-12 00:05:35 -07001168 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
Yuval Mintz494fff52018-02-28 23:29:34 +02001169 mod_timer(&mrt->ipmr_expire_timer,
1170 c->_c.mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 }
1172
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001173 /* See if we can append the packet */
Yuval Mintz494fff52018-02-28 23:29:34 +02001174 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 kfree_skb(skb);
1176 err = -ENOBUFS;
1177 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001178 if (dev) {
1179 skb->dev = dev;
1180 skb->skb_iif = dev->ifindex;
1181 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001182 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 err = 0;
1184 }
1185
1186 spin_unlock_bh(&mfc_unres_lock);
1187 return err;
1188}
1189
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001190/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001192static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
Yotam Gigib3620532017-09-27 08:23:14 +02001194 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001195 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001197 /* The entries are added/deleted only under RTNL */
1198 rcu_read_lock();
1199 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1200 mfc->mfcc_mcastgrp.s_addr, parent);
1201 rcu_read_unlock();
1202 if (!c)
1203 return -ENOENT;
Yuval Mintz494fff52018-02-28 23:29:34 +02001204 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1205 list_del_rcu(&c->_c.list);
Yotam Gigib3620532017-09-27 08:23:14 +02001206 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001207 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yuval Mintz8c13af22018-03-26 15:01:36 +03001208 mr_cache_put(&c->_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001210 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211}
1212
Patrick McHardy0c122952010-04-13 05:03:22 +00001213static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001214 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215{
Patrick McHardy862465f2010-04-13 05:03:21 +00001216 struct mfc_cache *uc, *c;
Yuval Mintz494fff52018-02-28 23:29:34 +02001217 struct mr_mfc *_uc;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001218 bool found;
1219 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Patrick McHardya50436f22010-03-17 06:04:14 +00001221 if (mfc->mfcc_parent >= MAXVIFS)
1222 return -ENFILE;
1223
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001224 /* The entries are added/deleted only under RTNL */
1225 rcu_read_lock();
1226 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1227 mfc->mfcc_mcastgrp.s_addr, parent);
1228 rcu_read_unlock();
1229 if (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 write_lock_bh(&mrt_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001231 c->_c.mfc_parent = mfc->mfcc_parent;
1232 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001234 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +02001236 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1237 mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001238 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 return 0;
1240 }
1241
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001242 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001243 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 return -EINVAL;
1245
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001246 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001247 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return -ENOMEM;
1249
Jianjun Kongc354e122008-11-03 00:28:02 -08001250 c->mfc_origin = mfc->mfcc_origin.s_addr;
1251 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
Yuval Mintz494fff52018-02-28 23:29:34 +02001252 c->_c.mfc_parent = mfc->mfcc_parent;
1253 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001255 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Yuval Mintz494fff52018-02-28 23:29:34 +02001257 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001258 ipmr_rht_params);
1259 if (ret) {
1260 pr_err("ipmr: rhtable insert error %d\n", ret);
1261 ipmr_cache_free(c);
1262 return ret;
1263 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001264 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001265 /* Check to see if we resolved a queued list. If so we
1266 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001268 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001270 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1271 uc = (struct mfc_cache *)_uc;
Patrick McHardye258beb2010-04-13 05:03:19 +00001272 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001274 list_del(&_uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001275 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001276 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 break;
1278 }
1279 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001280 if (list_empty(&mrt->mfc_unres_queue))
1281 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 spin_unlock_bh(&mfc_unres_lock);
1283
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001284 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001285 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001286 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 }
Yotam Gigib3620532017-09-27 08:23:14 +02001288 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001289 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 return 0;
1291}
1292
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001293/* Close the multicast socket, and clear the vif tables etc */
Callum Sinclairca8d4792019-02-18 10:07:52 +13001294static void mroute_clean_tables(struct mr_table *mrt, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295{
Yotam Gigib3620532017-09-27 08:23:14 +02001296 struct net *net = read_pnet(&mrt->net);
Yuval Mintz494fff52018-02-28 23:29:34 +02001297 struct mr_mfc *c, *tmp;
1298 struct mfc_cache *cache;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001299 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001300 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001301
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001302 /* Shut down all active vif entries */
Callum Sinclairca8d4792019-02-18 10:07:52 +13001303 if (flags & (MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC)) {
1304 for (i = 0; i < mrt->maxvif; i++) {
1305 if (((mrt->vif_table[i].flags & VIFF_STATIC) &&
1306 !(flags & MRT_FLUSH_VIFS_STATIC)) ||
1307 (!(mrt->vif_table[i].flags & VIFF_STATIC) && !(flags & MRT_FLUSH_VIFS)))
1308 continue;
1309 vif_delete(mrt, i, 0, &list);
1310 }
1311 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 }
1313
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001314 /* Wipe the cache */
Callum Sinclairca8d4792019-02-18 10:07:52 +13001315 if (flags & (MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC)) {
1316 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1317 if (((c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC_STATIC)) ||
1318 (!(c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC)))
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);
1327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 }
1329
Callum Sinclairca8d4792019-02-18 10:07:52 +13001330 if (flags & MRT_FLUSH_MFC) {
1331 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1332 spin_lock_bh(&mfc_unres_lock);
1333 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1334 list_del(&c->list);
1335 cache = (struct mfc_cache *)c;
1336 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1337 ipmr_destroy_unres(mrt, cache);
1338 }
1339 spin_unlock_bh(&mfc_unres_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001340 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342}
1343
Eric Dumazet4c9687092010-10-01 16:15:01 +00001344/* called from ip_ra_control(), before an RCU grace period,
1345 * we dont need to call synchronize_rcu() here
1346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347static void mrtsock_destruct(struct sock *sk)
1348{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001349 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001350 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001351
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001352 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001353 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001354 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001355 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001356 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1357 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001358 NETCONFA_IFINDEX_ALL,
1359 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001360 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Callum Sinclairca8d4792019-02-18 10:07:52 +13001361 mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_MFC);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 }
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001364 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365}
1366
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001367/* Socket options and virtual interface manipulation. The whole
1368 * virtual interface system is a complete heap, but unfortunately
1369 * that's how BSD mrouted happens to think. Maybe one day with a proper
1370 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001372
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001373int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1374 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001376 struct net *net = sock_net(sk);
1377 int val, ret = 0, parent = 0;
1378 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 struct vifctl vif;
1380 struct mfcctl mfc;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001381 bool do_wrvifwhole;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001382 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001383
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001384 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1385 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001386 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001387 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1388 ret = -EOPNOTSUPP;
1389 goto out_unlock;
1390 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001391
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001392 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001393 if (!mrt) {
1394 ret = -ENOENT;
1395 goto out_unlock;
1396 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001397 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001398 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001399 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1400 ret = -EACCES;
1401 goto out_unlock;
1402 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 }
1404
Stephen Hemminger132adf52007-03-08 20:44:43 -08001405 switch (optname) {
1406 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001407 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001408 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001409 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001410 }
1411 if (rtnl_dereference(mrt->mroute_sk)) {
1412 ret = -EADDRINUSE;
1413 break;
1414 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001415
1416 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1417 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001418 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001419 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001420 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1421 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001422 NETCONFA_IFINDEX_ALL,
1423 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001424 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001425 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001426 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001427 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1428 ret = -EACCES;
1429 } else {
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001430 /* We need to unlock here because mrtsock_destruct takes
1431 * care of rtnl itself and we can't change that due to
1432 * the IP_ROUTER_ALERT setsockopt which runs without it.
1433 */
1434 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001435 ret = ip_ra_control(sk, 0, NULL);
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001436 goto out;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001437 }
1438 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001439 case MRT_ADD_VIF:
1440 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001441 if (optlen != sizeof(vif)) {
1442 ret = -EINVAL;
1443 break;
1444 }
1445 if (copy_from_user(&vif, optval, sizeof(vif))) {
1446 ret = -EFAULT;
1447 break;
1448 }
1449 if (vif.vifc_vifi >= MAXVIFS) {
1450 ret = -ENFILE;
1451 break;
1452 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001453 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001454 ret = vif_add(net, mrt, &vif,
1455 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001456 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001457 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001458 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001459 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001460 /* Manipulate the forwarding caches. These live
1461 * in a sort of kernel/user symbiosis.
1462 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001463 case MRT_ADD_MFC:
1464 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001465 parent = -1;
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05001466 /* fall through */
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001467 case MRT_ADD_MFC_PROXY:
1468 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001469 if (optlen != sizeof(mfc)) {
1470 ret = -EINVAL;
1471 break;
1472 }
1473 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1474 ret = -EFAULT;
1475 break;
1476 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001477 if (parent == 0)
1478 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001479 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1480 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001481 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001482 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001483 sk == rtnl_dereference(mrt->mroute_sk),
1484 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001485 break;
Callum Sinclairca8d4792019-02-18 10:07:52 +13001486 case MRT_FLUSH:
1487 if (optlen != sizeof(val)) {
1488 ret = -EINVAL;
1489 break;
1490 }
1491 if (get_user(val, (int __user *)optval)) {
1492 ret = -EFAULT;
1493 break;
1494 }
1495 mroute_clean_tables(mrt, val);
1496 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001497 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001498 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001499 if (optlen != sizeof(val)) {
1500 ret = -EINVAL;
1501 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001503 if (get_user(val, (int __user *)optval)) {
1504 ret = -EFAULT;
1505 break;
1506 }
1507 mrt->mroute_do_assert = val;
1508 break;
1509 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001510 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001511 ret = -ENOPROTOOPT;
1512 break;
1513 }
1514 if (optlen != sizeof(val)) {
1515 ret = -EINVAL;
1516 break;
1517 }
1518 if (get_user(val, (int __user *)optval)) {
1519 ret = -EFAULT;
1520 break;
1521 }
1522
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001523 do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001524 val = !!val;
1525 if (val != mrt->mroute_do_pim) {
1526 mrt->mroute_do_pim = val;
1527 mrt->mroute_do_assert = val;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001528 mrt->mroute_do_wrvifwhole = do_wrvifwhole;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001529 }
1530 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001531 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001532 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1533 ret = -ENOPROTOOPT;
1534 break;
1535 }
1536 if (optlen != sizeof(uval)) {
1537 ret = -EINVAL;
1538 break;
1539 }
1540 if (get_user(uval, (u32 __user *)optval)) {
1541 ret = -EFAULT;
1542 break;
1543 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001544
Eric Dumazet4c9687092010-10-01 16:15:01 +00001545 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1546 ret = -EBUSY;
1547 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001548 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001549 if (IS_ERR(mrt))
1550 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001551 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001552 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001553 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001554 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001555 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001556 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001557 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001559out_unlock:
1560 rtnl_unlock();
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001561out:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001562 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563}
1564
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001565/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001566int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567{
1568 int olr;
1569 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001570 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001571 struct mr_table *mrt;
1572
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001573 if (sk->sk_type != SOCK_RAW ||
1574 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1575 return -EOPNOTSUPP;
1576
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001577 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001578 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001579 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001581 switch (optname) {
1582 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001583 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001584 break;
1585 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001586 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001587 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001588 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001589 break;
1590 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001591 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001592 break;
1593 default:
1594 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001595 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001596
1597 if (get_user(olr, optlen))
1598 return -EFAULT;
1599 olr = min_t(unsigned int, olr, sizeof(int));
1600 if (olr < 0)
1601 return -EINVAL;
1602 if (put_user(olr, optlen))
1603 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001604 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return -EFAULT;
1606 return 0;
1607}
1608
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001609/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1611{
1612 struct sioc_sg_req sr;
1613 struct sioc_vif_req vr;
1614 struct vif_device *vif;
1615 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001616 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001617 struct mr_table *mrt;
1618
1619 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001620 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001621 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001622
Stephen Hemminger132adf52007-03-08 20:44:43 -08001623 switch (cmd) {
1624 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001625 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001626 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001627 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001628 return -EINVAL;
Gustavo A. R. Silva56484512018-12-10 12:41:24 -06001629 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001630 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001631 vif = &mrt->vif_table[vr.vifi];
1632 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001633 vr.icount = vif->pkt_in;
1634 vr.ocount = vif->pkt_out;
1635 vr.ibytes = vif->bytes_in;
1636 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001638
Jianjun Kongc354e122008-11-03 00:28:02 -08001639 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001641 return 0;
1642 }
1643 read_unlock(&mrt_lock);
1644 return -EADDRNOTAVAIL;
1645 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001646 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001647 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Eric Dumazeta8c94862010-10-01 16:15:08 +00001649 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001650 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001651 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001652 sr.pktcnt = c->_c.mfc_un.res.pkt;
1653 sr.bytecnt = c->_c.mfc_un.res.bytes;
1654 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001655 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001656
Jianjun Kongc354e122008-11-03 00:28:02 -08001657 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001658 return -EFAULT;
1659 return 0;
1660 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001661 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001662 return -EADDRNOTAVAIL;
1663 default:
1664 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 }
1666}
1667
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001668#ifdef CONFIG_COMPAT
1669struct compat_sioc_sg_req {
1670 struct in_addr src;
1671 struct in_addr grp;
1672 compat_ulong_t pktcnt;
1673 compat_ulong_t bytecnt;
1674 compat_ulong_t wrong_if;
1675};
1676
David S. Millerca6b8bb02011-02-03 17:24:28 -08001677struct compat_sioc_vif_req {
1678 vifi_t vifi; /* Which iface */
1679 compat_ulong_t icount;
1680 compat_ulong_t ocount;
1681 compat_ulong_t ibytes;
1682 compat_ulong_t obytes;
1683};
1684
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001685int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1686{
David S. Miller0033d5a2011-02-03 17:21:31 -08001687 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001688 struct compat_sioc_vif_req vr;
1689 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001690 struct mfc_cache *c;
1691 struct net *net = sock_net(sk);
1692 struct mr_table *mrt;
1693
1694 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001695 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001696 return -ENOENT;
1697
1698 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001699 case SIOCGETVIFCNT:
1700 if (copy_from_user(&vr, arg, sizeof(vr)))
1701 return -EFAULT;
1702 if (vr.vifi >= mrt->maxvif)
1703 return -EINVAL;
Gustavo A. R. Silva56484512018-12-10 12:41:24 -06001704 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
David S. Millerca6b8bb02011-02-03 17:24:28 -08001705 read_lock(&mrt_lock);
1706 vif = &mrt->vif_table[vr.vifi];
1707 if (VIF_EXISTS(mrt, vr.vifi)) {
1708 vr.icount = vif->pkt_in;
1709 vr.ocount = vif->pkt_out;
1710 vr.ibytes = vif->bytes_in;
1711 vr.obytes = vif->bytes_out;
1712 read_unlock(&mrt_lock);
1713
1714 if (copy_to_user(arg, &vr, sizeof(vr)))
1715 return -EFAULT;
1716 return 0;
1717 }
1718 read_unlock(&mrt_lock);
1719 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001720 case SIOCGETSGCNT:
1721 if (copy_from_user(&sr, arg, sizeof(sr)))
1722 return -EFAULT;
1723
1724 rcu_read_lock();
1725 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1726 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001727 sr.pktcnt = c->_c.mfc_un.res.pkt;
1728 sr.bytecnt = c->_c.mfc_un.res.bytes;
1729 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001730 rcu_read_unlock();
1731
1732 if (copy_to_user(arg, &sr, sizeof(sr)))
1733 return -EFAULT;
1734 return 0;
1735 }
1736 rcu_read_unlock();
1737 return -EADDRNOTAVAIL;
1738 default:
1739 return -ENOIOCTLCMD;
1740 }
1741}
1742#endif
1743
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1745{
Jiri Pirko351638e2013-05-28 01:30:21 +00001746 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001747 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001748 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 struct vif_device *v;
1750 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 if (event != NETDEV_UNREGISTER)
1753 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001754
1755 ipmr_for_each_table(mrt, net) {
1756 v = &mrt->vif_table[0];
1757 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1758 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001759 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 }
1762 return NOTIFY_DONE;
1763}
1764
Jianjun Kongc354e122008-11-03 00:28:02 -08001765static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 .notifier_call = ipmr_device_event,
1767};
1768
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001769/* Encapsulate a packet by attaching a valid IPIP header to it.
1770 * This avoids tunnel drivers and other mess and gives us the speed so
1771 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001773static void ip_encap(struct net *net, struct sk_buff *skb,
1774 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001776 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001777 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001778
1779 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001780 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001781 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001782 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001784 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001785 iph->tos = old_iph->tos;
1786 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 iph->frag_off = 0;
1788 iph->daddr = daddr;
1789 iph->saddr = saddr;
1790 iph->protocol = IPPROTO_IPIP;
1791 iph->ihl = 5;
1792 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001793 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 ip_send_check(iph);
1795
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1797 nf_reset(skb);
1798}
1799
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001800static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1801 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001803 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804
David S. Miller73186df2015-11-03 13:41:45 -05001805 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1806 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
1808 if (unlikely(opt->optlen))
1809 ip_forward_options(skb);
1810
Eric W. Biederman13206b62015-10-07 16:48:35 -05001811 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812}
1813
Yotam Gigia5bc9292017-10-03 09:58:08 +02001814#ifdef CONFIG_NET_SWITCHDEV
1815static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1816 int in_vifi, int out_vifi)
1817{
1818 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1819 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1820
Ido Schimmel875e8932018-12-04 08:15:10 +00001821 if (!skb->offload_l3_fwd_mark)
Yotam Gigia5bc9292017-10-03 09:58:08 +02001822 return false;
1823 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1824 return false;
1825 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1826 &in_vif->dev_parent_id);
1827}
1828#else
1829static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1830 int in_vifi, int out_vifi)
1831{
1832 return false;
1833}
1834#endif
1835
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001836/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Patrick McHardy0c122952010-04-13 05:03:22 +00001838static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
David Ahern6e0735d2018-12-17 15:34:48 -08001839 int in_vifi, struct sk_buff *skb, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001841 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001842 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 struct net_device *dev;
1844 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001845 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 int encap = 0;
1847
Ian Morris51456b22015-04-03 09:17:26 +01001848 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 goto out_free;
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 if (vif->flags & VIFF_REGISTER) {
1852 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001853 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001854 vif->dev->stats.tx_bytes += skb->len;
1855 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001856 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001857 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Yotam Gigia5bc9292017-10-03 09:58:08 +02001860 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1861 goto out_free;
1862
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001863 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001864 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001865 vif->remote, vif->local,
1866 0, 0,
1867 IPPROTO_IPIP,
1868 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001869 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto out_free;
1871 encap = sizeof(struct iphdr);
1872 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001873 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001874 0, 0,
1875 IPPROTO_IPIP,
1876 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001877 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 goto out_free;
1879 }
1880
Changli Gaod8d1f302010-06-10 23:31:35 -07001881 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Changli Gaod8d1f302010-06-10 23:31:35 -07001883 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001885 * allow to send ICMP, so that packets will disappear
1886 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 */
David S. Miller73186df2015-11-03 13:41:45 -05001888 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 ip_rt_put(rt);
1890 goto out_free;
1891 }
1892
Changli Gaod8d1f302010-06-10 23:31:35 -07001893 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001896 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 goto out_free;
1898 }
1899
1900 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001901 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Eric Dumazetadf30902009-06-02 05:19:30 +00001903 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001904 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001905 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001908 * What do we do with netfilter? -- RR
1909 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001911 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001913 vif->dev->stats.tx_packets++;
1914 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001917 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001919 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 * not only before forwarding, but after forwarding on all output
1921 * interfaces. It is clear, if mrouter runs a multicasting
1922 * program, it should receive packets not depending to what interface
1923 * program is joined.
1924 * If we will not make it, the program will have to join on all
1925 * interfaces. On the other hand, multihoming host (or router, but
1926 * not mrouter) cannot join to more than one interface - it will
1927 * result in receiving multiple packets.
1928 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001929 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1930 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 ipmr_forward_finish);
1932 return;
1933
1934out_free:
1935 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
Patrick McHardy0c122952010-04-13 05:03:22 +00001938static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001941
1942 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1943 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 break;
1945 }
1946 return ct;
1947}
1948
1949/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001950static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001951 struct net_device *dev, struct sk_buff *skb,
Yuval Mintz494fff52018-02-28 23:29:34 +02001952 struct mfc_cache *c, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001954 int true_vifi = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 int psend = -1;
1956 int vif, ct;
1957
Yuval Mintz494fff52018-02-28 23:29:34 +02001958 vif = c->_c.mfc_parent;
1959 c->_c.mfc_un.res.pkt++;
1960 c->_c.mfc_un.res.bytes += skb->len;
1961 c->_c.mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Yuval Mintz494fff52018-02-28 23:29:34 +02001963 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001964 struct mfc_cache *cache_proxy;
1965
1966 /* For an (*,G) entry, we only check that the incomming
1967 * interface is part of the static tree.
1968 */
Yuval Mintz845c9a72018-02-28 23:29:35 +02001969 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001970 if (cache_proxy &&
Yuval Mintz494fff52018-02-28 23:29:34 +02001971 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001972 goto forward;
1973 }
1974
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001975 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001976 if (mrt->vif_table[vif].dev != dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001977 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001979 * Very complicated situation...
1980 *
1981 * The best workaround until routing daemons will be
1982 * fixed is not to redistribute packet, if it was
1983 * send through wrong interface. It means, that
1984 * multicast applications WILL NOT work for
1985 * (S,G), which have default multicast route pointing
1986 * to wrong oif. In any case, it is not a good
1987 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 */
1989 goto dont_forward;
1990 }
1991
Yuval Mintz494fff52018-02-28 23:29:34 +02001992 c->_c.mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Patrick McHardy0c122952010-04-13 05:03:22 +00001994 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001996 * so that we cannot check that packet arrived on an oif.
1997 * It is bad, but otherwise we would need to move pretty
1998 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 */
Patrick McHardy0c122952010-04-13 05:03:22 +00002000 (mrt->mroute_do_pim ||
Yuval Mintz494fff52018-02-28 23:29:34 +02002001 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002002 time_after(jiffies,
Yuval Mintz494fff52018-02-28 23:29:34 +02002003 c->_c.mfc_un.res.last_assert +
2004 MFC_ASSERT_THRESH)) {
2005 c->_c.mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00002006 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03002007 if (mrt->mroute_do_wrvifwhole)
2008 ipmr_cache_report(mrt, skb, true_vifi,
2009 IGMPMSG_WRVIFWHOLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010 }
2011 goto dont_forward;
2012 }
2013
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002014forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00002015 mrt->vif_table[vif].pkt_in++;
2016 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002018 /* Forward the frame */
Yuval Mintz494fff52018-02-28 23:29:34 +02002019 if (c->mfc_origin == htonl(INADDR_ANY) &&
2020 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002021 if (true_vifi >= 0 &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002022 true_vifi != c->_c.mfc_parent &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002023 ip_hdr(skb)->ttl >
Yuval Mintz494fff52018-02-28 23:29:34 +02002024 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002025 /* It's an (*,*) entry and the packet is not coming from
2026 * the upstream: forward the packet to the upstream
2027 * only.
2028 */
Yuval Mintz494fff52018-02-28 23:29:34 +02002029 psend = c->_c.mfc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002030 goto last_forward;
2031 }
2032 goto dont_forward;
2033 }
Yuval Mintz494fff52018-02-28 23:29:34 +02002034 for (ct = c->_c.mfc_un.res.maxvif - 1;
2035 ct >= c->_c.mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002036 /* For (*,G) entry, don't forward to the incoming interface */
Yuval Mintz494fff52018-02-28 23:29:34 +02002037 if ((c->mfc_origin != htonl(INADDR_ANY) ||
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002038 ct != true_vifi) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002039 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (psend != -1) {
2041 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002044 ipmr_queue_xmit(net, mrt, true_vifi,
David Ahern6e0735d2018-12-17 15:34:48 -08002045 skb2, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002047 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
2049 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002050last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (psend != -1) {
2052 if (local) {
2053 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002054
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002056 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
David Ahern6e0735d2018-12-17 15:34:48 -08002057 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 } else {
David Ahern6e0735d2018-12-17 15:34:48 -08002059 ipmr_queue_xmit(net, mrt, true_vifi, skb, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03002060 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 }
2062 }
2063
2064dont_forward:
2065 if (!local)
2066 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067}
2068
David S. Miller417da662011-05-03 19:42:43 -07002069static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08002070{
David S. Miller417da662011-05-03 19:42:43 -07002071 struct rtable *rt = skb_rtable(skb);
2072 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05002073 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07002074 .daddr = iph->daddr,
2075 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00002076 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07002077 .flowi4_oif = (rt_is_output_route(rt) ?
2078 skb->dev->ifindex : 0),
2079 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002080 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07002081 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00002082 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08002083 };
2084 struct mr_table *mrt;
2085 int err;
2086
David S. Millerda919812011-03-12 02:04:50 -05002087 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002088 if (err)
2089 return ERR_PTR(err);
2090 return mrt;
2091}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002093/* Multicast packets for forwarding arrive here
2094 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096int ip_mr_input(struct sk_buff *skb)
2097{
2098 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002099 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00002100 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002101 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002102 struct net_device *dev;
2103
2104 /* skb->dev passed in is the loX master dev for vrfs.
2105 * As there are no vifs associated with loopback devices,
2106 * get the proper interface that does have a vif associated with it.
2107 */
2108 dev = skb->dev;
2109 if (netif_is_l3_master(skb->dev)) {
2110 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2111 if (!dev) {
2112 kfree_skb(skb);
2113 return -ENODEV;
2114 }
2115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116
2117 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002118 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00002120 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 goto dont_forward;
2122
David S. Miller417da662011-05-03 19:42:43 -07002123 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002124 if (IS_ERR(mrt)) {
2125 kfree_skb(skb);
2126 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00002129 if (IPCB(skb)->opt.router_alert) {
2130 if (ip_call_ra_chain(skb))
2131 return 0;
2132 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2133 /* IGMPv1 (and broken IGMPv2 implementations sort of
2134 * Cisco IOS <= 11.2(8)) do not put router alert
2135 * option to IGMP packets destined to routable
2136 * groups. It is very bad, because it means
2137 * that we can forward NO IGMP messages.
2138 */
2139 struct sock *mroute_sk;
2140
2141 mroute_sk = rcu_dereference(mrt->mroute_sk);
2142 if (mroute_sk) {
2143 nf_reset(skb);
2144 raw_rcv(mroute_sk, skb);
2145 return 0;
2146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 }
2148 }
2149
Eric Dumazeta8c94862010-10-01 16:15:08 +00002150 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002151 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002152 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002153 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002154
2155 if (vif >= 0)
2156 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2157 vif);
2158 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002160 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002161 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 int vif;
2163
2164 if (local) {
2165 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2166 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002167 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 skb = skb2;
2170 }
2171
Eric Dumazeta8c94862010-10-01 16:15:08 +00002172 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002173 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 if (vif >= 0) {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002175 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 read_unlock(&mrt_lock);
2177
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002178 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
2180 read_unlock(&mrt_lock);
2181 kfree_skb(skb);
2182 return -ENODEV;
2183 }
2184
Eric Dumazeta8c94862010-10-01 16:15:08 +00002185 read_lock(&mrt_lock);
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002186 ip_mr_forward(net, mrt, dev, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 read_unlock(&mrt_lock);
2188
2189 if (local)
2190 return ip_local_deliver(skb);
2191
2192 return 0;
2193
2194dont_forward:
2195 if (local)
2196 return ip_local_deliver(skb);
2197 kfree_skb(skb);
2198 return 0;
2199}
2200
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002201#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002202/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002203int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002204{
2205 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002206 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002207 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002208
2209 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2210 goto drop;
2211
2212 pim = igmp_hdr(skb);
2213
David S. Miller417da662011-05-03 19:42:43 -07002214 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002215 if (IS_ERR(mrt))
2216 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002217 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002218 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2219 goto drop;
2220
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002221 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002222drop:
2223 kfree_skb(skb);
2224 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 return 0;
2226}
2227#endif
2228
2229#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002230static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231{
2232 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002233 struct net *net = dev_net(skb->dev);
2234 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002236 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 goto drop;
2238
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002239 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002240 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002241 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002242 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002243 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 goto drop;
2245
David S. Miller417da662011-05-03 19:42:43 -07002246 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002247 if (IS_ERR(mrt))
2248 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002249 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002250drop:
2251 kfree_skb(skb);
2252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 return 0;
2254}
2255#endif
2256
David S. Miller9a1b9492011-05-04 12:18:54 -07002257int ipmr_get_route(struct net *net, struct sk_buff *skb,
2258 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002259 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002262 struct mr_table *mrt;
2263 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002265 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002266 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002267 return -ENOENT;
2268
Eric Dumazeta8c94862010-10-01 16:15:08 +00002269 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002270 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002271 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002272 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002274 if (vif >= 0)
2275 cache = ipmr_cache_find_any(mrt, daddr, vif);
2276 }
Ian Morris51456b22015-04-03 09:17:26 +01002277 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002278 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002279 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002281 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002284 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002285 if (dev)
2286 vif = ipmr_find_vif(mrt, dev);
2287 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002288 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002289 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 return -ENODEV;
2291 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002292 skb2 = skb_clone(skb, GFP_ATOMIC);
2293 if (!skb2) {
2294 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002295 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002296 return -ENOMEM;
2297 }
2298
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002299 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002300 skb_push(skb2, sizeof(struct iphdr));
2301 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002302 iph = ip_hdr(skb2);
2303 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002304 iph->saddr = saddr;
2305 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002306 iph->version = 0;
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002307 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002309 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return err;
2311 }
2312
Eric Dumazeta8c94862010-10-01 16:15:08 +00002313 read_lock(&mrt_lock);
Yuval Mintz7b0db852018-02-28 23:29:39 +02002314 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002316 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317 return err;
2318}
2319
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002320static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002321 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2322 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002323{
2324 struct nlmsghdr *nlh;
2325 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002326 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002327
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002328 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002329 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002330 return -EMSGSIZE;
2331
2332 rtm = nlmsg_data(nlh);
2333 rtm->rtm_family = RTNL_FAMILY_IPMR;
2334 rtm->rtm_dst_len = 32;
2335 rtm->rtm_src_len = 32;
2336 rtm->rtm_tos = 0;
2337 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002338 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2339 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002340 rtm->rtm_type = RTN_MULTICAST;
2341 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Yuval Mintz494fff52018-02-28 23:29:34 +02002342 if (c->_c.mfc_flags & MFC_STATIC)
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002343 rtm->rtm_protocol = RTPROT_STATIC;
2344 else
2345 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002346 rtm->rtm_flags = 0;
2347
Jiri Benc930345e2015-03-29 16:59:25 +02002348 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2349 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002350 goto nla_put_failure;
Yuval Mintz7b0db852018-02-28 23:29:39 +02002351 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002352 /* do not break the dump if cache is unresolved */
2353 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002354 goto nla_put_failure;
2355
Johannes Berg053c0952015-01-16 22:09:00 +01002356 nlmsg_end(skb, nlh);
2357 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002358
2359nla_put_failure:
2360 nlmsg_cancel(skb, nlh);
2361 return -EMSGSIZE;
2362}
2363
Yuval Mintz7b0db852018-02-28 23:29:39 +02002364static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2365 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2366 int flags)
2367{
2368 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2369 cmd, flags);
2370}
2371
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002372static size_t mroute_msgsize(bool unresolved, int maxvif)
2373{
2374 size_t len =
2375 NLMSG_ALIGN(sizeof(struct rtmsg))
2376 + nla_total_size(4) /* RTA_TABLE */
2377 + nla_total_size(4) /* RTA_SRC */
2378 + nla_total_size(4) /* RTA_DST */
2379 ;
2380
2381 if (!unresolved)
2382 len = len
2383 + nla_total_size(4) /* RTA_IIF */
2384 + nla_total_size(0) /* RTA_MULTIPATH */
2385 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2386 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002387 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002388 ;
2389
2390 return len;
2391}
2392
2393static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2394 int cmd)
2395{
2396 struct net *net = read_pnet(&mrt->net);
2397 struct sk_buff *skb;
2398 int err = -ENOBUFS;
2399
Yuval Mintz494fff52018-02-28 23:29:34 +02002400 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2401 mrt->maxvif),
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002402 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002403 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002404 goto errout;
2405
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002406 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002407 if (err < 0)
2408 goto errout;
2409
2410 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2411 return;
2412
2413errout:
2414 kfree_skb(skb);
2415 if (err < 0)
2416 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2417}
2418
Julien Gomes5a645dd2017-06-20 13:54:17 -07002419static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2420{
2421 size_t len =
2422 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2423 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2424 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2425 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2426 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2427 /* IPMRA_CREPORT_PKT */
2428 + nla_total_size(payloadlen)
2429 ;
2430
2431 return len;
2432}
2433
2434static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2435{
2436 struct net *net = read_pnet(&mrt->net);
2437 struct nlmsghdr *nlh;
2438 struct rtgenmsg *rtgenm;
2439 struct igmpmsg *msg;
2440 struct sk_buff *skb;
2441 struct nlattr *nla;
2442 int payloadlen;
2443
2444 payloadlen = pkt->len - sizeof(struct igmpmsg);
2445 msg = (struct igmpmsg *)skb_network_header(pkt);
2446
2447 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2448 if (!skb)
2449 goto errout;
2450
2451 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2452 sizeof(struct rtgenmsg), 0);
2453 if (!nlh)
2454 goto errout;
2455 rtgenm = nlmsg_data(nlh);
2456 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2457 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2458 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2459 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2460 msg->im_src.s_addr) ||
2461 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2462 msg->im_dst.s_addr))
2463 goto nla_put_failure;
2464
2465 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2466 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2467 nla_data(nla), payloadlen))
2468 goto nla_put_failure;
2469
2470 nlmsg_end(skb, nlh);
2471
2472 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2473 return;
2474
2475nla_put_failure:
2476 nlmsg_cancel(skb, nlh);
2477errout:
2478 kfree_skb(skb);
2479 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2480}
2481
Jakub Kicinskid0440022019-01-18 10:46:20 -08002482static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb,
2483 const struct nlmsghdr *nlh,
2484 struct nlattr **tb,
2485 struct netlink_ext_ack *extack)
2486{
2487 struct rtmsg *rtm;
2488 int i, err;
2489
2490 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
2491 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for multicast route get request");
2492 return -EINVAL;
2493 }
2494
2495 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02002496 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
2497 rtm_ipv4_policy, extack);
Jakub Kicinskid0440022019-01-18 10:46:20 -08002498
2499 rtm = nlmsg_data(nlh);
2500 if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
2501 (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
2502 rtm->rtm_tos || rtm->rtm_table || rtm->rtm_protocol ||
2503 rtm->rtm_scope || rtm->rtm_type || rtm->rtm_flags) {
2504 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for multicast route get request");
2505 return -EINVAL;
2506 }
2507
Johannes Berg8cb08172019-04-26 14:07:28 +02002508 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
2509 rtm_ipv4_policy, extack);
Jakub Kicinskid0440022019-01-18 10:46:20 -08002510 if (err)
2511 return err;
2512
2513 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
2514 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
2515 NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
2516 return -EINVAL;
2517 }
2518
2519 for (i = 0; i <= RTA_MAX; i++) {
2520 if (!tb[i])
2521 continue;
2522
2523 switch (i) {
2524 case RTA_SRC:
2525 case RTA_DST:
2526 case RTA_TABLE:
2527 break;
2528 default:
2529 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in multicast route get request");
2530 return -EINVAL;
2531 }
2532 }
2533
2534 return 0;
2535}
2536
Donald Sharp4f75ba62017-06-28 13:58:57 -04002537static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2538 struct netlink_ext_ack *extack)
2539{
2540 struct net *net = sock_net(in_skb->sk);
2541 struct nlattr *tb[RTA_MAX + 1];
2542 struct sk_buff *skb = NULL;
2543 struct mfc_cache *cache;
2544 struct mr_table *mrt;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002545 __be32 src, grp;
2546 u32 tableid;
2547 int err;
2548
Jakub Kicinskid0440022019-01-18 10:46:20 -08002549 err = ipmr_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Donald Sharp4f75ba62017-06-28 13:58:57 -04002550 if (err < 0)
2551 goto errout;
2552
Donald Sharp4f75ba62017-06-28 13:58:57 -04002553 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2554 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2555 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2556
2557 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
Dan Carpenter2e3d2322017-07-12 10:56:47 +03002558 if (!mrt) {
2559 err = -ENOENT;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002560 goto errout_free;
2561 }
2562
2563 /* entries are added/deleted only under RTNL */
2564 rcu_read_lock();
2565 cache = ipmr_cache_find(mrt, src, grp);
2566 rcu_read_unlock();
2567 if (!cache) {
2568 err = -ENOENT;
2569 goto errout_free;
2570 }
2571
2572 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2573 if (!skb) {
2574 err = -ENOBUFS;
2575 goto errout_free;
2576 }
2577
2578 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2579 nlh->nlmsg_seq, cache,
2580 RTM_NEWROUTE, 0);
2581 if (err < 0)
2582 goto errout_free;
2583
2584 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2585
2586errout:
2587 return err;
2588
2589errout_free:
2590 kfree_skb(skb);
2591 goto errout;
2592}
2593
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002594static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2595{
David Ahern47246762018-10-15 18:56:42 -07002596 struct fib_dump_filter filter = {};
David Aherncb167892018-10-15 18:56:47 -07002597 int err;
David Aherne8ba3302018-10-07 20:16:35 -07002598
David Ahern47246762018-10-15 18:56:42 -07002599 if (cb->strict_check) {
David Ahern47246762018-10-15 18:56:42 -07002600 err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh,
David Aherneffe6792018-10-15 18:56:48 -07002601 &filter, cb);
David Aherne8ba3302018-10-07 20:16:35 -07002602 if (err < 0)
2603 return err;
2604 }
2605
David Aherncb167892018-10-15 18:56:47 -07002606 if (filter.table_id) {
2607 struct mr_table *mrt;
2608
2609 mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id);
2610 if (!mrt) {
David Ahernae677bb2018-10-24 12:59:01 -07002611 if (filter.dump_all_families)
2612 return skb->len;
2613
David Aherncb167892018-10-15 18:56:47 -07002614 NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist");
2615 return -ENOENT;
2616 }
2617 err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute,
2618 &mfc_unres_lock, &filter);
2619 return skb->len ? : err;
2620 }
2621
Yuval Mintz7b0db852018-02-28 23:29:39 +02002622 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
David Aherncb167892018-10-15 18:56:47 -07002623 _ipmr_fill_mroute, &mfc_unres_lock, &filter);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002624}
2625
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002626static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2627 [RTA_SRC] = { .type = NLA_U32 },
2628 [RTA_DST] = { .type = NLA_U32 },
2629 [RTA_IIF] = { .type = NLA_U32 },
2630 [RTA_TABLE] = { .type = NLA_U32 },
2631 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2632};
2633
2634static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2635{
2636 switch (rtm_protocol) {
2637 case RTPROT_STATIC:
2638 case RTPROT_MROUTED:
2639 return true;
2640 }
2641 return false;
2642}
2643
2644static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2645{
2646 struct rtnexthop *rtnh = nla_data(nla);
2647 int remaining = nla_len(nla), vifi = 0;
2648
2649 while (rtnh_ok(rtnh, remaining)) {
2650 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2651 if (++vifi == MAXVIFS)
2652 break;
2653 rtnh = rtnh_next(rtnh, &remaining);
2654 }
2655
2656 return remaining > 0 ? -EINVAL : vifi;
2657}
2658
2659/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2660static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2661 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002662 struct mr_table **mrtret,
2663 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002664{
2665 struct net_device *dev = NULL;
2666 u32 tblid = RT_TABLE_DEFAULT;
2667 struct mr_table *mrt;
2668 struct nlattr *attr;
2669 struct rtmsg *rtm;
2670 int ret, rem;
2671
Johannes Berg8cb08172019-04-26 14:07:28 +02002672 ret = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
2673 rtm_ipmr_policy, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002674 if (ret < 0)
2675 goto out;
2676 rtm = nlmsg_data(nlh);
2677
2678 ret = -EINVAL;
2679 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2680 rtm->rtm_type != RTN_MULTICAST ||
2681 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2682 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2683 goto out;
2684
2685 memset(mfcc, 0, sizeof(*mfcc));
2686 mfcc->mfcc_parent = -1;
2687 ret = 0;
2688 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2689 switch (nla_type(attr)) {
2690 case RTA_SRC:
2691 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2692 break;
2693 case RTA_DST:
2694 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2695 break;
2696 case RTA_IIF:
2697 dev = __dev_get_by_index(net, nla_get_u32(attr));
2698 if (!dev) {
2699 ret = -ENODEV;
2700 goto out;
2701 }
2702 break;
2703 case RTA_MULTIPATH:
2704 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2705 ret = -EINVAL;
2706 goto out;
2707 }
2708 break;
2709 case RTA_PREFSRC:
2710 ret = 1;
2711 break;
2712 case RTA_TABLE:
2713 tblid = nla_get_u32(attr);
2714 break;
2715 }
2716 }
2717 mrt = ipmr_get_table(net, tblid);
2718 if (!mrt) {
2719 ret = -ENOENT;
2720 goto out;
2721 }
2722 *mrtret = mrt;
2723 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2724 if (dev)
2725 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2726
2727out:
2728 return ret;
2729}
2730
2731/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002732static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2733 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002734{
2735 struct net *net = sock_net(skb->sk);
2736 int ret, mrtsock, parent;
2737 struct mr_table *tbl;
2738 struct mfcctl mfcc;
2739
2740 mrtsock = 0;
2741 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002742 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002743 if (ret < 0)
2744 return ret;
2745
2746 parent = ret ? mfcc.mfcc_parent : -1;
2747 if (nlh->nlmsg_type == RTM_NEWROUTE)
2748 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2749 else
2750 return ipmr_mfc_delete(tbl, &mfcc, parent);
2751}
2752
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002753static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2754{
2755 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2756
2757 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2758 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2759 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2760 mrt->mroute_reg_vif_num) ||
2761 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2762 mrt->mroute_do_assert) ||
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03002763 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
2764 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
2765 mrt->mroute_do_wrvifwhole))
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002766 return false;
2767
2768 return true;
2769}
2770
2771static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2772{
2773 struct nlattr *vif_nest;
2774 struct vif_device *vif;
2775
2776 /* if the VIF doesn't exist just continue */
2777 if (!VIF_EXISTS(mrt, vifid))
2778 return true;
2779
2780 vif = &mrt->vif_table[vifid];
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002781 vif_nest = nla_nest_start_noflag(skb, IPMRA_VIF);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002782 if (!vif_nest)
2783 return false;
2784 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2785 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2786 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2787 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2788 IPMRA_VIFA_PAD) ||
2789 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2790 IPMRA_VIFA_PAD) ||
2791 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2792 IPMRA_VIFA_PAD) ||
2793 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2794 IPMRA_VIFA_PAD) ||
2795 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2796 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2797 nla_nest_cancel(skb, vif_nest);
2798 return false;
2799 }
2800 nla_nest_end(skb, vif_nest);
2801
2802 return true;
2803}
2804
David Ahern14fc5bb2018-10-07 20:16:34 -07002805static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
2806 struct netlink_ext_ack *extack)
2807{
2808 struct ifinfomsg *ifm;
2809
2810 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2811 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
2812 return -EINVAL;
2813 }
2814
2815 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
2816 NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump");
2817 return -EINVAL;
2818 }
2819
2820 ifm = nlmsg_data(nlh);
2821 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2822 ifm->ifi_change || ifm->ifi_index) {
2823 NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
2824 return -EINVAL;
2825 }
2826
2827 return 0;
2828}
2829
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002830static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2831{
2832 struct net *net = sock_net(skb->sk);
2833 struct nlmsghdr *nlh = NULL;
2834 unsigned int t = 0, s_t;
2835 unsigned int e = 0, s_e;
2836 struct mr_table *mrt;
2837
David Ahern14fc5bb2018-10-07 20:16:34 -07002838 if (cb->strict_check) {
2839 int err = ipmr_valid_dumplink(cb->nlh, cb->extack);
2840
2841 if (err < 0)
2842 return err;
2843 }
2844
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002845 s_t = cb->args[0];
2846 s_e = cb->args[1];
2847
2848 ipmr_for_each_table(mrt, net) {
2849 struct nlattr *vifs, *af;
2850 struct ifinfomsg *hdr;
2851 u32 i;
2852
2853 if (t < s_t)
2854 goto skip_table;
2855 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2856 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2857 sizeof(*hdr), NLM_F_MULTI);
2858 if (!nlh)
2859 break;
2860
2861 hdr = nlmsg_data(nlh);
2862 memset(hdr, 0, sizeof(*hdr));
2863 hdr->ifi_family = RTNL_FAMILY_IPMR;
2864
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002865 af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002866 if (!af) {
2867 nlmsg_cancel(skb, nlh);
2868 goto out;
2869 }
2870
2871 if (!ipmr_fill_table(mrt, skb)) {
2872 nlmsg_cancel(skb, nlh);
2873 goto out;
2874 }
2875
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002876 vifs = nla_nest_start_noflag(skb, IPMRA_TABLE_VIFS);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002877 if (!vifs) {
2878 nla_nest_end(skb, af);
2879 nlmsg_end(skb, nlh);
2880 goto out;
2881 }
2882 for (i = 0; i < mrt->maxvif; i++) {
2883 if (e < s_e)
2884 goto skip_entry;
2885 if (!ipmr_fill_vif(mrt, i, skb)) {
2886 nla_nest_end(skb, vifs);
2887 nla_nest_end(skb, af);
2888 nlmsg_end(skb, nlh);
2889 goto out;
2890 }
2891skip_entry:
2892 e++;
2893 }
2894 s_e = 0;
2895 e = 0;
2896 nla_nest_end(skb, vifs);
2897 nla_nest_end(skb, af);
2898 nlmsg_end(skb, nlh);
2899skip_table:
2900 t++;
2901 }
2902
2903out:
2904 cb->args[1] = e;
2905 cb->args[0] = t;
2906
2907 return skb->len;
2908}
2909
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002910#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002911/* The /proc interfaces to multicast routing :
2912 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
2915static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002916 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002918 struct mr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002919 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002920 struct mr_table *mrt;
2921
2922 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002923 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002924 return ERR_PTR(-ENOENT);
2925
2926 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002927
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 read_lock(&mrt_lock);
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002929 return mr_vif_seq_start(seq, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930}
2931
2932static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002933 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
2935 read_unlock(&mrt_lock);
2936}
2937
2938static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2939{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002940 struct mr_vif_iter *iter = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002941 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002942
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002944 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2946 } else {
2947 const struct vif_device *vif = v;
Yuval Mintz6853f212018-02-28 23:29:29 +02002948 const char *name = vif->dev ?
2949 vif->dev->name : "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950
2951 seq_printf(seq,
James Hogan91e6dd82018-01-30 09:48:02 +00002952 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002953 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002954 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 vif->bytes_out, vif->pkt_out,
2956 vif->flags, vif->local, vif->remote);
2957 }
2958 return 0;
2959}
2960
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002961static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 .start = ipmr_vif_seq_start,
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002963 .next = mr_vif_seq_next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 .stop = ipmr_vif_seq_stop,
2965 .show = ipmr_vif_seq_show,
2966};
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2969{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002970 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002971 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002972
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002973 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002974 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002975 return ERR_PTR(-ENOENT);
2976
Yuval Mintzc8d61962018-02-28 23:29:36 +02002977 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978}
2979
2980static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2981{
2982 int n;
2983
2984 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002985 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2987 } else {
2988 const struct mfc_cache *mfc = v;
Yuval Mintzc8d61962018-02-28 23:29:36 +02002989 const struct mr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002990 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002991
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002992 seq_printf(seq, "%08X %08X %-3hd",
2993 (__force u32) mfc->mfc_mcastgrp,
2994 (__force u32) mfc->mfc_origin,
Yuval Mintz494fff52018-02-28 23:29:34 +02002995 mfc->_c.mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996
Patrick McHardy0c122952010-04-13 05:03:22 +00002997 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002998 seq_printf(seq, " %8lu %8lu %8lu",
Yuval Mintz494fff52018-02-28 23:29:34 +02002999 mfc->_c.mfc_un.res.pkt,
3000 mfc->_c.mfc_un.res.bytes,
3001 mfc->_c.mfc_un.res.wrong_if);
3002 for (n = mfc->_c.mfc_un.res.minvif;
3003 n < mfc->_c.mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00003004 if (VIF_EXISTS(mrt, n) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02003005 mfc->_c.mfc_un.res.ttls[n] < 255)
Benjamin Therycf958ae32009-01-22 04:56:16 +00003006 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003007 " %2d:%-3d",
Yuval Mintz494fff52018-02-28 23:29:34 +02003008 n, mfc->_c.mfc_un.res.ttls[n]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003010 } else {
3011 /* unresolved mfc_caches don't contain
3012 * pkt, bytes and wrong_if values
3013 */
3014 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 }
3016 seq_putc(seq, '\n');
3017 }
3018 return 0;
3019}
3020
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003021static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 .start = ipmr_mfc_seq_start,
Yuval Mintzc8d61962018-02-28 23:29:36 +02003023 .next = mr_mfc_seq_next,
3024 .stop = mr_mfc_seq_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025 .show = ipmr_mfc_seq_show,
3026};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003027#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
3029#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00003030static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07003032 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033};
3034#endif
3035
Yotam Gigi4d65b942017-09-27 08:23:13 +02003036static unsigned int ipmr_seq_read(struct net *net)
3037{
3038 ASSERT_RTNL();
3039
3040 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
3041}
3042
3043static int ipmr_dump(struct net *net, struct notifier_block *nb)
3044{
Yuval Mintzcdc9f942018-03-26 15:01:33 +03003045 return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
3046 ipmr_mr_table_iter, &mrt_lock);
Yotam Gigi4d65b942017-09-27 08:23:13 +02003047}
3048
3049static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3050 .family = RTNL_FAMILY_IPMR,
3051 .fib_seq_read = ipmr_seq_read,
3052 .fib_dump = ipmr_dump,
3053 .owner = THIS_MODULE,
3054};
3055
Colin Ian Kingef739d82017-09-29 14:34:22 +01003056static int __net_init ipmr_notifier_init(struct net *net)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003057{
3058 struct fib_notifier_ops *ops;
3059
3060 net->ipv4.ipmr_seq = 0;
3061
3062 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3063 if (IS_ERR(ops))
3064 return PTR_ERR(ops);
3065 net->ipv4.ipmr_notifier_ops = ops;
3066
3067 return 0;
3068}
3069
3070static void __net_exit ipmr_notifier_exit(struct net *net)
3071{
3072 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3073 net->ipv4.ipmr_notifier_ops = NULL;
3074}
3075
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01003076/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00003077static int __net_init ipmr_net_init(struct net *net)
3078{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003079 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00003080
Yotam Gigi4d65b942017-09-27 08:23:13 +02003081 err = ipmr_notifier_init(net);
3082 if (err)
3083 goto ipmr_notifier_fail;
3084
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003085 err = ipmr_rules_init(net);
3086 if (err < 0)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003087 goto ipmr_rules_fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003088
3089#ifdef CONFIG_PROC_FS
3090 err = -ENOMEM;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003091 if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
3092 sizeof(struct mr_vif_iter)))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003093 goto proc_vif_fail;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003094 if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
3095 sizeof(struct mr_mfc_iter)))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003096 goto proc_cache_fail;
3097#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00003098 return 0;
3099
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003100#ifdef CONFIG_PROC_FS
3101proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00003102 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003103proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003104 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003105#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003106ipmr_rules_fail:
3107 ipmr_notifier_exit(net);
3108ipmr_notifier_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003109 return err;
3110}
3111
3112static void __net_exit ipmr_net_exit(struct net *net)
3113{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003114#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003115 remove_proc_entry("ip_mr_cache", net->proc_net);
3116 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003117#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003118 ipmr_notifier_exit(net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003119 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00003120}
3121
3122static struct pernet_operations ipmr_net_ops = {
3123 .init = ipmr_net_init,
3124 .exit = ipmr_net_exit,
3125};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003126
Wang Chen03d2f892008-07-03 12:13:36 +08003127int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128{
Wang Chen03d2f892008-07-03 12:13:36 +08003129 int err;
3130
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3132 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00003133 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003134 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08003135
Benjamin Therycf958ae32009-01-22 04:56:16 +00003136 err = register_pernet_subsys(&ipmr_net_ops);
3137 if (err)
3138 goto reg_pernet_fail;
3139
Wang Chen03d2f892008-07-03 12:13:36 +08003140 err = register_netdevice_notifier(&ip_mr_notifier);
3141 if (err)
3142 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07003143#ifdef CONFIG_IP_PIMSM_V2
3144 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00003145 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07003146 err = -EAGAIN;
3147 goto add_proto_fail;
3148 }
3149#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00003150 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003151 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003152 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003153 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003154 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003155 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03003156
3157 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
Florian Westphalb97bac62017-08-09 20:41:48 +02003158 NULL, ipmr_rtm_dumplink, 0);
Wang Chen03d2f892008-07-03 12:13:36 +08003159 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003160
Tom Goff403dbb92009-06-14 03:16:13 -07003161#ifdef CONFIG_IP_PIMSM_V2
3162add_proto_fail:
3163 unregister_netdevice_notifier(&ip_mr_notifier);
3164#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08003165reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003166 unregister_pernet_subsys(&ipmr_net_ops);
3167reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08003168 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08003169 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170}