blob: 29bbe2b08ae970e5accd7e1b01bcd5f502a66810 [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
Amol Grover70139082020-05-14 23:31:03 +0530112#define ipmr_for_each_table(mrt, net) \
113 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list, \
114 lockdep_rtnl_is_held() || \
115 list_empty(&net->ipv4.mr_tables))
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000116
Yuval Mintz7b0db852018-02-28 23:29:39 +0200117static struct mr_table *ipmr_mr_table_iter(struct net *net,
118 struct mr_table *mrt)
119{
120 struct mr_table *ret;
121
122 if (!mrt)
123 ret = list_entry_rcu(net->ipv4.mr_tables.next,
124 struct mr_table, list);
125 else
126 ret = list_entry_rcu(mrt->list.next,
127 struct mr_table, list);
128
129 if (&ret->list == &net->ipv4.mr_tables)
130 return NULL;
131 return ret;
132}
133
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000134static struct mr_table *ipmr_get_table(struct net *net, u32 id)
135{
136 struct mr_table *mrt;
137
138 ipmr_for_each_table(mrt, net) {
139 if (mrt->id == id)
140 return mrt;
141 }
142 return NULL;
143}
144
David S. Millerda919812011-03-12 02:04:50 -0500145static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000146 struct mr_table **mrt)
147{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000148 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100149 struct ipmr_result res;
150 struct fib_lookup_arg arg = {
151 .result = &res,
152 .flags = FIB_LOOKUP_NOREF,
153 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000154
David Aherne58e4152016-10-31 15:54:00 -0700155 /* update flow if oif or iif point to device enslaved to l3mdev */
156 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
157
David S. Millerda919812011-03-12 02:04:50 -0500158 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
159 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000160 if (err < 0)
161 return err;
162 *mrt = res.mrt;
163 return 0;
164}
165
166static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
167 int flags, struct fib_lookup_arg *arg)
168{
169 struct ipmr_result *res = arg->result;
170 struct mr_table *mrt;
171
172 switch (rule->action) {
173 case FR_ACT_TO_TBL:
174 break;
175 case FR_ACT_UNREACHABLE:
176 return -ENETUNREACH;
177 case FR_ACT_PROHIBIT:
178 return -EACCES;
179 case FR_ACT_BLACKHOLE:
180 default:
181 return -EINVAL;
182 }
183
David Aherne58e4152016-10-31 15:54:00 -0700184 arg->table = fib_rule_get_table(rule, arg);
185
186 mrt = ipmr_get_table(rule->fr_net, arg->table);
Ian Morris51456b22015-04-03 09:17:26 +0100187 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000188 return -EAGAIN;
189 res->mrt = mrt;
190 return 0;
191}
192
193static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
194{
195 return 1;
196}
197
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000198static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
Roopa Prabhub16fb412018-04-21 09:41:31 -0700199 struct fib_rule_hdr *frh, struct nlattr **tb,
200 struct netlink_ext_ack *extack)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000201{
202 return 0;
203}
204
205static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
206 struct nlattr **tb)
207{
208 return 1;
209}
210
211static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
212 struct fib_rule_hdr *frh)
213{
214 frh->dst_len = 0;
215 frh->src_len = 0;
216 frh->tos = 0;
217 return 0;
218}
219
Andi Kleen04a6f822012-10-04 17:12:11 -0700220static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200221 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000222 .rule_size = sizeof(struct ipmr_rule),
223 .addr_size = sizeof(u32),
224 .action = ipmr_rule_action,
225 .match = ipmr_rule_match,
226 .configure = ipmr_rule_configure,
227 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000228 .fill = ipmr_rule_fill,
229 .nlgroup = RTNLGRP_IPV4_RULE,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000230 .owner = THIS_MODULE,
231};
232
233static int __net_init ipmr_rules_init(struct net *net)
234{
235 struct fib_rules_ops *ops;
236 struct mr_table *mrt;
237 int err;
238
239 ops = fib_rules_register(&ipmr_rules_ops_template, net);
240 if (IS_ERR(ops))
241 return PTR_ERR(ops);
242
243 INIT_LIST_HEAD(&net->ipv4.mr_tables);
244
245 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100246 if (IS_ERR(mrt)) {
247 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000248 goto err1;
249 }
250
251 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
252 if (err < 0)
253 goto err2;
254
255 net->ipv4.mr_rules_ops = ops;
256 return 0;
257
258err2:
Eric Dumazet5611a002022-02-07 21:34:51 -0800259 rtnl_lock();
WANG Congf243e5a2015-03-25 14:45:03 -0700260 ipmr_free_table(mrt);
Eric Dumazet5611a002022-02-07 21:34:51 -0800261 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000262err1:
263 fib_rules_unregister(ops);
264 return err;
265}
266
267static void __net_exit ipmr_rules_exit(struct net *net)
268{
269 struct mr_table *mrt, *next;
270
WANG Conged785302015-03-31 11:01:45 -0700271 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000272 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
273 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000274 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000275 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000276 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700277 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000278}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200279
Jiri Pirkob7a59552019-10-03 11:49:30 +0200280static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,
281 struct netlink_ext_ack *extack)
Yotam Gigi4d65b942017-09-27 08:23:13 +0200282{
Jiri Pirkob7a59552019-10-03 11:49:30 +0200283 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR, extack);
Yotam Gigi4d65b942017-09-27 08:23:13 +0200284}
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
Jiri Pirkob7a59552019-10-03 11:49:30 +0200339static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,
340 struct netlink_ext_ack *extack)
Yotam Gigi4d65b942017-09-27 08:23:13 +0200341{
342 return 0;
343}
344
345static unsigned int ipmr_rules_seq_read(struct net *net)
346{
347 return 0;
348}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200349
350bool ipmr_rule_default(const struct fib_rule *rule)
351{
352 return true;
353}
354EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000355#endif
356
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100357static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
358 const void *ptr)
359{
360 const struct mfc_cache_cmp_arg *cmparg = arg->key;
361 struct mfc_cache *c = (struct mfc_cache *)ptr;
362
363 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
364 cmparg->mfc_origin != c->mfc_origin;
365}
366
367static const struct rhashtable_params ipmr_rht_params = {
Yuval Mintz494fff52018-02-28 23:29:34 +0200368 .head_offset = offsetof(struct mr_mfc, mnode),
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100369 .key_offset = offsetof(struct mfc_cache, cmparg),
370 .key_len = sizeof(struct mfc_cache_cmp_arg),
371 .nelem_hint = 3,
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100372 .obj_cmpfn = ipmr_hash_cmp,
373 .automatic_shrinking = true,
374};
375
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200376static void ipmr_new_table_set(struct mr_table *mrt,
377 struct net *net)
378{
379#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
380 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
381#endif
382}
383
Yuval Mintz845c9a72018-02-28 23:29:35 +0200384static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
385 .mfc_mcastgrp = htonl(INADDR_ANY),
386 .mfc_origin = htonl(INADDR_ANY),
387};
388
389static struct mr_table_ops ipmr_mr_table_ops = {
390 .rht_params = &ipmr_rht_params,
391 .cmparg_any = &ipmr_mr_table_ops_cmparg_any,
392};
393
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000394static struct mr_table *ipmr_new_table(struct net *net, u32 id)
395{
396 struct mr_table *mrt;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000397
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100398 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
399 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
400 return ERR_PTR(-EINVAL);
401
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000402 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100403 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000404 return mrt;
405
Yuval Mintz845c9a72018-02-28 23:29:35 +0200406 return mr_table_alloc(net, id, &ipmr_mr_table_ops,
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200407 ipmr_expire_process, ipmr_new_table_set);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000408}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000410static void ipmr_free_table(struct mr_table *mrt)
411{
412 del_timer_sync(&mrt->ipmr_expire_timer);
Callum Sinclairca8d4792019-02-18 10:07:52 +1300413 mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC |
414 MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100415 rhltable_destroy(&mrt->mfc_hash);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000416 kfree(mrt);
417}
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
420
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100421/* Initialize ipmr pimreg/tunnel in_device */
422static bool ipmr_init_vif_indev(const struct net_device *dev)
423{
424 struct in_device *in_dev;
425
426 ASSERT_RTNL();
427
428 in_dev = __in_dev_get_rtnl(dev);
429 if (!in_dev)
430 return false;
431 ipv4_devconf_setall(in_dev);
432 neigh_parms_data_state_setall(in_dev->arp_parms);
433 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
434
435 return true;
436}
437
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100438static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439{
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200440 struct net_device *tunnel_dev, *new_dev;
441 struct ip_tunnel_parm p = { };
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200442 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200444 tunnel_dev = __dev_get_by_name(net, "tunl0");
445 if (!tunnel_dev)
446 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200448 p.iph.daddr = v->vifc_rmt_addr.s_addr;
449 p.iph.saddr = v->vifc_lcl_addr.s_addr;
450 p.iph.version = 4;
451 p.iph.ihl = 5;
452 p.iph.protocol = IPPROTO_IPIP;
453 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
Christoph Hellwigc7e367052020-05-19 15:03:14 +0200455 if (!tunnel_dev->netdev_ops->ndo_tunnel_ctl)
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200456 goto out;
Christoph Hellwigc7e367052020-05-19 15:03:14 +0200457 err = tunnel_dev->netdev_ops->ndo_tunnel_ctl(tunnel_dev, &p,
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200458 SIOCADDTUNNEL);
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200459 if (err)
460 goto out;
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800461
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200462 new_dev = __dev_get_by_name(net, p.name);
463 if (!new_dev)
464 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200466 new_dev->flags |= IFF_MULTICAST;
467 if (!ipmr_init_vif_indev(new_dev))
468 goto out_unregister;
469 if (dev_open(new_dev, NULL))
470 goto out_unregister;
471 dev_hold(new_dev);
Christoph Hellwigc1fd11822020-05-19 15:03:12 +0200472 err = dev_set_allmulti(new_dev, 1);
473 if (err) {
474 dev_close(new_dev);
Christoph Hellwigc7e367052020-05-19 15:03:14 +0200475 tunnel_dev->netdev_ops->ndo_tunnel_ctl(tunnel_dev, &p,
Christoph Hellwigc1fd11822020-05-19 15:03:12 +0200476 SIOCDELTUNNEL);
Christoph Hellwigc1fd11822020-05-19 15:03:12 +0200477 dev_put(new_dev);
478 new_dev = ERR_PTR(err);
479 }
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200480 return new_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200482out_unregister:
483 unregister_netdevice(new_dev);
484out:
Christoph Hellwigc1fd11822020-05-19 15:03:12 +0200485 return ERR_PTR(-ENOBUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100488#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000489static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000491 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000492 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500493 struct flowi4 fl4 = {
494 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700495 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500496 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000497 };
498 int err;
499
David S. Millerda919812011-03-12 02:04:50 -0500500 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000501 if (err < 0) {
502 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000503 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000504 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700507 dev->stats.tx_bytes += skb->len;
508 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000509 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 read_unlock(&mrt_lock);
511 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000512 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513}
514
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200515static int reg_vif_get_iflink(const struct net_device *dev)
516{
517 return 0;
518}
519
Stephen Hemminger007c3832008-11-20 20:28:35 -0800520static const struct net_device_ops reg_vif_netdev_ops = {
521 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200522 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800523};
524
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525static void reg_vif_setup(struct net_device *dev)
526{
527 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800528 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530530 dev->netdev_ops = &reg_vif_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400531 dev->needs_free_netdev = true;
Tom Goff403dbb92009-06-14 03:16:13 -0700532 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533}
534
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000535static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536{
537 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000538 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000540 if (mrt->id == RT_TABLE_DEFAULT)
541 sprintf(name, "pimreg");
542 else
543 sprintf(name, "pimreg%u", mrt->id);
544
Tom Gundersenc835a672014-07-14 16:37:24 +0200545 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Ian Morris51456b22015-04-03 09:17:26 +0100547 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 return NULL;
549
Tom Goff403dbb92009-06-14 03:16:13 -0700550 dev_net_set(dev, net);
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 if (register_netdevice(dev)) {
553 free_netdev(dev);
554 return NULL;
555 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100557 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 goto failure;
Petr Machata00f54e62018-12-06 17:05:36 +0000559 if (dev_open(dev, NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 goto failure;
561
Wang Chen7dc00c82008-07-14 20:56:34 -0700562 dev_hold(dev);
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return dev;
565
566failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 unregister_netdevice(dev);
568 return NULL;
569}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100570
571/* called with rcu_read_lock() */
572static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
573 unsigned int pimlen)
574{
575 struct net_device *reg_dev = NULL;
576 struct iphdr *encap;
577
578 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100579 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100580 * a. packet is really sent to a multicast group
581 * b. packet is not a NULL-REGISTER
582 * c. packet is not truncated
583 */
584 if (!ipv4_is_multicast(encap->daddr) ||
585 encap->tot_len == 0 ||
586 ntohs(encap->tot_len) + pimlen > skb->len)
587 return 1;
588
589 read_lock(&mrt_lock);
590 if (mrt->mroute_reg_vif_num >= 0)
591 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
592 read_unlock(&mrt_lock);
593
594 if (!reg_dev)
595 return 1;
596
597 skb->mac_header = skb->network_header;
598 skb_pull(skb, (u8 *)encap - skb->data);
599 skb_reset_network_header(skb);
600 skb->protocol = htons(ETH_P_IP);
601 skb->ip_summed = CHECKSUM_NONE;
602
603 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
604
605 netif_rx(skb);
606
607 return NET_RX_SUCCESS;
608}
609#else
610static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
611{
612 return NULL;
613}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614#endif
615
Yotam Gigib3620532017-09-27 08:23:14 +0200616static int call_ipmr_vif_entry_notifiers(struct net *net,
617 enum fib_event_type event_type,
618 struct vif_device *vif,
619 vifi_t vif_index, u32 tb_id)
620{
Yuval Mintzbc67a0d2018-03-26 15:01:31 +0300621 return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
622 vif, vif_index, tb_id,
623 &net->ipv4.ipmr_seq);
Yotam Gigib3620532017-09-27 08:23:14 +0200624}
625
Yotam Gigib3620532017-09-27 08:23:14 +0200626static int call_ipmr_mfc_entry_notifiers(struct net *net,
627 enum fib_event_type event_type,
628 struct mfc_cache *mfc, u32 tb_id)
629{
Yuval Mintz54c4cad2018-03-26 15:01:32 +0300630 return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
631 &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
Yotam Gigib3620532017-09-27 08:23:14 +0200632}
633
Ben Hutchings2c530402012-07-10 10:55:09 +0000634/**
635 * vif_delete - Delete a VIF entry
Andrew Lunn3628e3c2020-07-13 01:15:02 +0200636 * @mrt: Table to delete from
637 * @vifi: VIF identifier to delete
Wang Chen7dc00c82008-07-14 20:56:34 -0700638 * @notify: Set to 1, if the caller is a notifier_call
Andrew Lunn3628e3c2020-07-13 01:15:02 +0200639 * @head: if unregistering the VIF, place it on this queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000641static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000642 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Yotam Gigib3620532017-09-27 08:23:14 +0200644 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 struct vif_device *v;
646 struct net_device *dev;
647 struct in_device *in_dev;
648
Patrick McHardy0c122952010-04-13 05:03:22 +0000649 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 return -EADDRNOTAVAIL;
651
Patrick McHardy0c122952010-04-13 05:03:22 +0000652 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Yotam Gigib3620532017-09-27 08:23:14 +0200654 if (VIF_EXISTS(mrt, vifi))
655 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
656 mrt->id);
657
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 write_lock_bh(&mrt_lock);
659 dev = v->dev;
660 v->dev = NULL;
661
662 if (!dev) {
663 write_unlock_bh(&mrt_lock);
664 return -EADDRNOTAVAIL;
665 }
666
Patrick McHardy0c122952010-04-13 05:03:22 +0000667 if (vifi == mrt->mroute_reg_vif_num)
668 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000670 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000672
673 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000674 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 break;
676 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000677 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 }
679
680 write_unlock_bh(&mrt_lock);
681
682 dev_set_allmulti(dev, -1);
683
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000684 in_dev = __in_dev_get_rtnl(dev);
685 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700686 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -0700687 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000688 NETCONFA_MC_FORWARDING,
689 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 ip_rt_multicast_event(in_dev);
691 }
692
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000693 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000694 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Eric Dumazet42120a82021-12-04 20:22:16 -0800696 dev_put_track(dev, &v->dev_tracker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return 0;
698}
699
Eric Dumazeta8c94862010-10-01 16:15:08 +0000700static void ipmr_cache_free_rcu(struct rcu_head *head)
701{
Yuval Mintz494fff52018-02-28 23:29:34 +0200702 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000703
Yuval Mintz494fff52018-02-28 23:29:34 +0200704 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000705}
706
Yuval Mintz8c13af22018-03-26 15:01:36 +0300707static void ipmr_cache_free(struct mfc_cache *c)
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000708{
Yuval Mintz494fff52018-02-28 23:29:34 +0200709 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000710}
711
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000713 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000715static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200717 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700719 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
Patrick McHardy0c122952010-04-13 05:03:22 +0000721 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Yuval Mintz494fff52018-02-28 23:29:34 +0200723 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700724 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200725 struct nlmsghdr *nlh = skb_pull(skb,
726 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000728 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000730 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700731 e->error = -ETIMEDOUT;
732 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700733
Eric W. Biederman15e47302012-09-07 20:12:54 +0000734 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000735 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000737 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000740 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
Patrick McHardye258beb2010-04-13 05:03:19 +0000743/* Timer process for the unresolved queue. */
Kees Cooke99e88a2017-10-16 14:43:17 -0700744static void ipmr_expire_process(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745{
Kees Cooke99e88a2017-10-16 14:43:17 -0700746 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
Yuval Mintz494fff52018-02-28 23:29:34 +0200747 struct mr_mfc *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 unsigned long expires;
Yuval Mintz494fff52018-02-28 23:29:34 +0200749 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000752 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 return;
754 }
755
Patrick McHardy0c122952010-04-13 05:03:22 +0000756 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 goto out;
758
759 now = jiffies;
760 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
Patrick McHardy0c122952010-04-13 05:03:22 +0000762 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 if (time_after(c->mfc_un.unres.expires, now)) {
764 unsigned long interval = c->mfc_un.unres.expires - now;
765 if (interval < expires)
766 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 continue;
768 }
769
Patrick McHardy862465f2010-04-13 05:03:21 +0000770 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +0200771 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
772 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 }
774
Patrick McHardy0c122952010-04-13 05:03:22 +0000775 if (!list_empty(&mrt->mfc_unres_queue))
776 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777
778out:
779 spin_unlock(&mfc_unres_lock);
780}
781
782/* Fill oifs list. It is called under write locked mrt_lock. */
Yuval Mintz494fff52018-02-28 23:29:34 +0200783static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000784 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785{
786 int vifi;
787
788 cache->mfc_un.res.minvif = MAXVIFS;
789 cache->mfc_un.res.maxvif = 0;
790 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
791
Patrick McHardy0c122952010-04-13 05:03:22 +0000792 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
793 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000794 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
796 if (cache->mfc_un.res.minvif > vifi)
797 cache->mfc_un.res.minvif = vifi;
798 if (cache->mfc_un.res.maxvif <= vifi)
799 cache->mfc_un.res.maxvif = vifi + 1;
800 }
801 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200802 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Patrick McHardy0c122952010-04-13 05:03:22 +0000805static int vif_add(struct net *net, struct mr_table *mrt,
806 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Florian Fainellibccb3022019-02-06 09:45:46 -0800808 struct netdev_phys_item_id ppid = { };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000810 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 struct net_device *dev;
812 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700813 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000816 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return -EADDRINUSE;
818
819 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100821 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100822 return -EINVAL;
823 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 * All the packets will be sent to the daemon
825 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000826 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000828 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 if (!dev)
830 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700831 err = dev_set_allmulti(dev, 1);
832 if (err) {
833 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700834 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700835 return err;
836 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900838 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000839 dev = ipmr_new_tunnel(net, vifc);
Christoph Hellwigc1fd11822020-05-19 15:03:12 +0200840 if (IS_ERR(dev))
841 return PTR_ERR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000843 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000845 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
846 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100847 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000848 dev_put(dev);
849 return -EADDRNOTAVAIL;
850 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000851 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000852 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000853 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 if (!dev)
855 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700856 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700857 if (err) {
858 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700859 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 break;
862 default:
863 return -EINVAL;
864 }
865
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000866 in_dev = __in_dev_get_rtnl(dev);
867 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000868 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000870 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700871 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700872 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
873 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 ip_rt_multicast_event(in_dev);
875
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000876 /* Fill in the VIF structures */
Yuval Mintz6853f212018-02-28 23:29:29 +0200877 vif_device_init(v, dev, vifc->vifc_rate_limit,
878 vifc->vifc_threshold,
879 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
880 (VIFF_TUNNEL | VIFF_REGISTER));
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000881
Florian Fainellibccb3022019-02-06 09:45:46 -0800882 err = dev_get_port_parent_id(dev, &ppid, true);
883 if (err == 0) {
884 memcpy(v->dev_parent_id.id, ppid.id, ppid.id_len);
885 v->dev_parent_id.id_len = ppid.id_len;
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200886 } else {
887 v->dev_parent_id.id_len = 0;
888 }
Yuval Mintz6853f212018-02-28 23:29:29 +0200889
Jianjun Kongc354e122008-11-03 00:28:02 -0800890 v->local = vifc->vifc_lcl_addr.s_addr;
891 v->remote = vifc->vifc_rmt_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
893 /* And finish update writing critical data */
894 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800895 v->dev = dev;
Eric Dumazet42120a82021-12-04 20:22:16 -0800896 netdev_tracker_alloc(dev, &v->dev_tracker, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000897 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000898 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000899 if (vifi+1 > mrt->maxvif)
900 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +0200902 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 return 0;
904}
905
Eric Dumazeta8c94862010-10-01 16:15:08 +0000906/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000907static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000908 __be32 origin,
909 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100911 struct mfc_cache_cmp_arg arg = {
912 .mfc_mcastgrp = mcastgrp,
913 .mfc_origin = origin
914 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Yuval Mintz845c9a72018-02-28 23:29:35 +0200916 return mr_mfc_find(mrt, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000917}
918
919/* Look for a (*,G) entry */
920static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
921 __be32 mcastgrp, int vifi)
922{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100923 struct mfc_cache_cmp_arg arg = {
924 .mfc_mcastgrp = mcastgrp,
925 .mfc_origin = htonl(INADDR_ANY)
926 };
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000927
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100928 if (mcastgrp == htonl(INADDR_ANY))
Yuval Mintz845c9a72018-02-28 23:29:35 +0200929 return mr_mfc_find_any_parent(mrt, vifi);
930 return mr_mfc_find_any(mrt, vifi, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000931}
932
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100933/* Look for a (S,G,iif) entry if parent != -1 */
934static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
935 __be32 origin, __be32 mcastgrp,
936 int parent)
937{
938 struct mfc_cache_cmp_arg arg = {
939 .mfc_mcastgrp = mcastgrp,
940 .mfc_origin = origin,
941 };
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100942
Yuval Mintz845c9a72018-02-28 23:29:35 +0200943 return mr_mfc_find_parent(mrt, &arg, parent);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100944}
945
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100946/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000947static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948{
Jianjun Kongc354e122008-11-03 00:28:02 -0800949 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000950
Tom Goff70a0dec2016-06-23 16:11:57 -0400951 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +0200952 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
953 c->_c.mfc_un.res.minvif = MAXVIFS;
Yuval Mintz8c13af22018-03-26 15:01:36 +0300954 c->_c.free = ipmr_cache_free_rcu;
Yuval Mintz494fff52018-02-28 23:29:34 +0200955 refcount_set(&c->_c.mfc_un.res.refcount, 1);
Tom Goff70a0dec2016-06-23 16:11:57 -0400956 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return c;
958}
959
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000960static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961{
Jianjun Kongc354e122008-11-03 00:28:02 -0800962 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000963
964 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +0200965 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
966 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
Eric Dumazeta8c94862010-10-01 16:15:08 +0000967 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 return c;
969}
970
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100971/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +0000972static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
973 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974{
975 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700976 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000978 /* Play the pending entries through our router */
Yuval Mintz494fff52018-02-28 23:29:34 +0200979 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700980 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200981 struct nlmsghdr *nlh = skb_pull(skb,
982 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
Yuval Mintz7b0db852018-02-28 23:29:39 +0200984 if (mr_fill_mroute(mrt, skb, &c->_c,
985 nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000986 nlh->nlmsg_len = skb_tail_pointer(skb) -
987 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 } else {
989 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000990 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000992 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700993 e->error = -EMSGSIZE;
994 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 }
Thomas Graf2942e902006-08-15 00:30:25 -0700996
Eric W. Biederman15e47302012-09-07 20:12:54 +0000997 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000998 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -0400999 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 }
1002}
1003
Julien Gomes5a645dd2017-06-20 13:54:17 -07001004/* Bounce a cache query up to mrouted and netlink.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001006 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001008static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001009 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001011 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001012 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 struct igmphdr *igmp;
1014 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001015 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 int ret;
1017
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001018 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1020 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 skb = alloc_skb(128, GFP_ATOMIC);
1022
Stephen Hemminger132adf52007-03-08 20:44:43 -08001023 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 return -ENOBUFS;
1025
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001026 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001028 * Duplicate old header, fix ihl, length etc.
1029 * And all this only to mangle msg->im_msgtype and
1030 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001032 skb_push(skb, sizeof(struct iphdr));
1033 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001034 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001035 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001036 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001037 msg->im_msgtype = assert;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 msg->im_mbz = 0;
Paul Daveyc8715a82020-09-08 10:04:07 +12001039 if (assert == IGMPMSG_WRVIFWHOLE) {
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001040 msg->im_vif = vifi;
Paul Daveyc8715a82020-09-08 10:04:07 +12001041 msg->im_vif_hi = vifi >> 8;
1042 } else {
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001043 msg->im_vif = mrt->mroute_reg_vif_num;
Paul Daveyc8715a82020-09-08 10:04:07 +12001044 msg->im_vif_hi = mrt->mroute_reg_vif_num >> 8;
1045 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001046 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1047 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1048 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001049 } else {
1050 /* Copy the IP header */
1051 skb_set_network_header(skb, skb->len);
1052 skb_put(skb, ihl);
1053 skb_copy_to_linear_data(skb, pkt->data, ihl);
1054 /* Flag to the kernel this is a route add */
1055 ip_hdr(skb)->protocol = 0;
1056 msg = (struct igmpmsg *)skb_network_header(skb);
1057 msg->im_vif = vifi;
Paul Daveyc8715a82020-09-08 10:04:07 +12001058 msg->im_vif_hi = vifi >> 8;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001059 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1060 /* Add our header */
Johannes Berg4df864c2017-06-16 14:29:21 +02001061 igmp = skb_put(skb, sizeof(struct igmphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001062 igmp->type = assert;
1063 msg->im_msgtype = assert;
1064 igmp->code = 0;
1065 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1066 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
Eric Dumazet4c9687092010-10-01 16:15:01 +00001069 rcu_read_lock();
1070 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001071 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001072 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 kfree_skb(skb);
1074 return -EINVAL;
1075 }
1076
Julien Gomes5a645dd2017-06-20 13:54:17 -07001077 igmpmsg_netlink_event(mrt, skb);
1078
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001079 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001080 ret = sock_queue_rcv_skb(mroute_sk, skb);
1081 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001082 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001083 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 kfree_skb(skb);
1085 }
1086
1087 return ret;
1088}
1089
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001090/* Queue a packet for resolution. It gets locked cache entry! */
1091static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001092 struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001094 const struct iphdr *iph = ip_hdr(skb);
1095 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001096 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001100 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001101 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001102 c->mfc_origin == iph->saddr) {
1103 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001105 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 }
1107
Patrick McHardy862465f2010-04-13 05:03:21 +00001108 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001109 /* Create a new entry if allowable */
Hangbin Liu0079ad82019-09-06 15:36:01 +08001110 c = ipmr_cache_alloc_unres();
1111 if (!c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 spin_unlock_bh(&mfc_unres_lock);
1113
1114 kfree_skb(skb);
1115 return -ENOBUFS;
1116 }
1117
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001118 /* Fill in the new cache entry */
Yuval Mintz494fff52018-02-28 23:29:34 +02001119 c->_c.mfc_parent = -1;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001120 c->mfc_origin = iph->saddr;
1121 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001123 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001124 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Yuval Mintz494fff52018-02-28 23:29:34 +02001125
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001126 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001127 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 out - Brad Parker
1129 */
1130 spin_unlock_bh(&mfc_unres_lock);
1131
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001132 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 kfree_skb(skb);
1134 return err;
1135 }
1136
Patrick McHardy0c122952010-04-13 05:03:22 +00001137 atomic_inc(&mrt->cache_resolve_queue_len);
Yuval Mintz494fff52018-02-28 23:29:34 +02001138 list_add(&c->_c.list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001139 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
David S. Miller278554b2010-05-12 00:05:35 -07001141 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
Yuval Mintz494fff52018-02-28 23:29:34 +02001142 mod_timer(&mrt->ipmr_expire_timer,
1143 c->_c.mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 }
1145
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001146 /* See if we can append the packet */
Yuval Mintz494fff52018-02-28 23:29:34 +02001147 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 kfree_skb(skb);
1149 err = -ENOBUFS;
1150 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001151 if (dev) {
1152 skb->dev = dev;
1153 skb->skb_iif = dev->ifindex;
1154 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001155 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 err = 0;
1157 }
1158
1159 spin_unlock_bh(&mfc_unres_lock);
1160 return err;
1161}
1162
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001163/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001165static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166{
Yotam Gigib3620532017-09-27 08:23:14 +02001167 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001168 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001170 /* The entries are added/deleted only under RTNL */
1171 rcu_read_lock();
1172 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1173 mfc->mfcc_mcastgrp.s_addr, parent);
1174 rcu_read_unlock();
1175 if (!c)
1176 return -ENOENT;
Yuval Mintz494fff52018-02-28 23:29:34 +02001177 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1178 list_del_rcu(&c->_c.list);
Yotam Gigib3620532017-09-27 08:23:14 +02001179 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001180 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yuval Mintz8c13af22018-03-26 15:01:36 +03001181 mr_cache_put(&c->_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
Patrick McHardy0c122952010-04-13 05:03:22 +00001186static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001187 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188{
Patrick McHardy862465f2010-04-13 05:03:21 +00001189 struct mfc_cache *uc, *c;
Yuval Mintz494fff52018-02-28 23:29:34 +02001190 struct mr_mfc *_uc;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001191 bool found;
1192 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
Patrick McHardya50436f22010-03-17 06:04:14 +00001194 if (mfc->mfcc_parent >= MAXVIFS)
1195 return -ENFILE;
1196
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) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 write_lock_bh(&mrt_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001204 c->_c.mfc_parent = mfc->mfcc_parent;
1205 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001207 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +02001209 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1210 mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001211 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 return 0;
1213 }
1214
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001215 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001216 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 return -EINVAL;
1218
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001219 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001220 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 return -ENOMEM;
1222
Jianjun Kongc354e122008-11-03 00:28:02 -08001223 c->mfc_origin = mfc->mfcc_origin.s_addr;
1224 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
Yuval Mintz494fff52018-02-28 23:29:34 +02001225 c->_c.mfc_parent = mfc->mfcc_parent;
1226 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001228 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Yuval Mintz494fff52018-02-28 23:29:34 +02001230 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001231 ipmr_rht_params);
1232 if (ret) {
1233 pr_err("ipmr: rhtable insert error %d\n", ret);
1234 ipmr_cache_free(c);
1235 return ret;
1236 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001237 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001238 /* Check to see if we resolved a queued list. If so we
1239 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001241 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001243 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1244 uc = (struct mfc_cache *)_uc;
Patrick McHardye258beb2010-04-13 05:03:19 +00001245 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001247 list_del(&_uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001248 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001249 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 break;
1251 }
1252 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001253 if (list_empty(&mrt->mfc_unres_queue))
1254 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 spin_unlock_bh(&mfc_unres_lock);
1256
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001257 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001258 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001259 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 }
Yotam Gigib3620532017-09-27 08:23:14 +02001261 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001262 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return 0;
1264}
1265
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001266/* Close the multicast socket, and clear the vif tables etc */
Callum Sinclairca8d4792019-02-18 10:07:52 +13001267static void mroute_clean_tables(struct mr_table *mrt, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Yotam Gigib3620532017-09-27 08:23:14 +02001269 struct net *net = read_pnet(&mrt->net);
Yuval Mintz494fff52018-02-28 23:29:34 +02001270 struct mr_mfc *c, *tmp;
1271 struct mfc_cache *cache;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001272 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001273 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001274
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001275 /* Shut down all active vif entries */
Callum Sinclairca8d4792019-02-18 10:07:52 +13001276 if (flags & (MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC)) {
1277 for (i = 0; i < mrt->maxvif; i++) {
1278 if (((mrt->vif_table[i].flags & VIFF_STATIC) &&
1279 !(flags & MRT_FLUSH_VIFS_STATIC)) ||
1280 (!(mrt->vif_table[i].flags & VIFF_STATIC) && !(flags & MRT_FLUSH_VIFS)))
1281 continue;
1282 vif_delete(mrt, i, 0, &list);
1283 }
1284 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001287 /* Wipe the cache */
Callum Sinclairca8d4792019-02-18 10:07:52 +13001288 if (flags & (MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC)) {
1289 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1290 if (((c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC_STATIC)) ||
1291 (!(c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC)))
1292 continue;
1293 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1294 list_del_rcu(&c->list);
1295 cache = (struct mfc_cache *)c;
1296 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
1297 mrt->id);
1298 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1299 mr_cache_put(c);
1300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 }
1302
Callum Sinclairca8d4792019-02-18 10:07:52 +13001303 if (flags & MRT_FLUSH_MFC) {
1304 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1305 spin_lock_bh(&mfc_unres_lock);
1306 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1307 list_del(&c->list);
1308 cache = (struct mfc_cache *)c;
1309 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1310 ipmr_destroy_unres(mrt, cache);
1311 }
1312 spin_unlock_bh(&mfc_unres_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 }
1315}
1316
Eric Dumazet4c9687092010-10-01 16:15:01 +00001317/* called from ip_ra_control(), before an RCU grace period,
Zheng Yongjun974d8f82021-06-07 23:01:09 +08001318 * we don't need to call synchronize_rcu() here
Eric Dumazet4c9687092010-10-01 16:15:01 +00001319 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320static void mrtsock_destruct(struct sock *sk)
1321{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001322 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001323 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001324
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001325 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001326 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001327 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001328 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001329 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1330 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001331 NETCONFA_IFINDEX_ALL,
1332 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001333 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Callum Sinclairca8d4792019-02-18 10:07:52 +13001334 mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_MFC);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001337 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338}
1339
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001340/* Socket options and virtual interface manipulation. The whole
1341 * virtual interface system is a complete heap, but unfortunately
1342 * that's how BSD mrouted happens to think. Maybe one day with a proper
1343 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001345
Christoph Hellwig01ccb5b2020-07-23 08:08:56 +02001346int ip_mroute_setsockopt(struct sock *sk, int optname, sockptr_t optval,
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001347 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001349 struct net *net = sock_net(sk);
1350 int val, ret = 0, parent = 0;
1351 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 struct vifctl vif;
1353 struct mfcctl mfc;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001354 bool do_wrvifwhole;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001355 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001356
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001357 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1358 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001359 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001360 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1361 ret = -EOPNOTSUPP;
1362 goto out_unlock;
1363 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001364
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001365 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001366 if (!mrt) {
1367 ret = -ENOENT;
1368 goto out_unlock;
1369 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001370 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001371 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001372 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1373 ret = -EACCES;
1374 goto out_unlock;
1375 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 }
1377
Stephen Hemminger132adf52007-03-08 20:44:43 -08001378 switch (optname) {
1379 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001380 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001381 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001382 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001383 }
1384 if (rtnl_dereference(mrt->mroute_sk)) {
1385 ret = -EADDRINUSE;
1386 break;
1387 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001388
1389 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1390 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001391 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001392 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001393 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1394 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001395 NETCONFA_IFINDEX_ALL,
1396 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001397 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001398 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001399 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001400 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1401 ret = -EACCES;
1402 } else {
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001403 /* We need to unlock here because mrtsock_destruct takes
1404 * care of rtnl itself and we can't change that due to
1405 * the IP_ROUTER_ALERT setsockopt which runs without it.
1406 */
1407 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001408 ret = ip_ra_control(sk, 0, NULL);
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001409 goto out;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001410 }
1411 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001412 case MRT_ADD_VIF:
1413 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001414 if (optlen != sizeof(vif)) {
1415 ret = -EINVAL;
1416 break;
1417 }
Christoph Hellwig01ccb5b2020-07-23 08:08:56 +02001418 if (copy_from_sockptr(&vif, optval, sizeof(vif))) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001419 ret = -EFAULT;
1420 break;
1421 }
1422 if (vif.vifc_vifi >= MAXVIFS) {
1423 ret = -ENFILE;
1424 break;
1425 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001426 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001427 ret = vif_add(net, mrt, &vif,
1428 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001429 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001430 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001431 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001432 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001433 /* Manipulate the forwarding caches. These live
1434 * in a sort of kernel/user symbiosis.
1435 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001436 case MRT_ADD_MFC:
1437 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001438 parent = -1;
Joe Perchesa8eceea2020-03-12 15:50:22 -07001439 fallthrough;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001440 case MRT_ADD_MFC_PROXY:
1441 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001442 if (optlen != sizeof(mfc)) {
1443 ret = -EINVAL;
1444 break;
1445 }
Ido Schimmel73cb1192020-07-27 10:18:34 +03001446 if (copy_from_sockptr(&mfc, optval, sizeof(mfc))) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001447 ret = -EFAULT;
1448 break;
1449 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001450 if (parent == 0)
1451 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001452 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1453 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001454 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001455 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001456 sk == rtnl_dereference(mrt->mroute_sk),
1457 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001458 break;
Callum Sinclairca8d4792019-02-18 10:07:52 +13001459 case MRT_FLUSH:
1460 if (optlen != sizeof(val)) {
1461 ret = -EINVAL;
1462 break;
1463 }
Christoph Hellwig01ccb5b2020-07-23 08:08:56 +02001464 if (copy_from_sockptr(&val, optval, sizeof(val))) {
Callum Sinclairca8d4792019-02-18 10:07:52 +13001465 ret = -EFAULT;
1466 break;
1467 }
1468 mroute_clean_tables(mrt, val);
1469 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001470 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001471 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001472 if (optlen != sizeof(val)) {
1473 ret = -EINVAL;
1474 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475 }
Christoph Hellwig01ccb5b2020-07-23 08:08:56 +02001476 if (copy_from_sockptr(&val, optval, sizeof(val))) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001477 ret = -EFAULT;
1478 break;
1479 }
1480 mrt->mroute_do_assert = val;
1481 break;
1482 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001483 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001484 ret = -ENOPROTOOPT;
1485 break;
1486 }
1487 if (optlen != sizeof(val)) {
1488 ret = -EINVAL;
1489 break;
1490 }
Christoph Hellwig01ccb5b2020-07-23 08:08:56 +02001491 if (copy_from_sockptr(&val, optval, sizeof(val))) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001492 ret = -EFAULT;
1493 break;
1494 }
1495
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001496 do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001497 val = !!val;
1498 if (val != mrt->mroute_do_pim) {
1499 mrt->mroute_do_pim = val;
1500 mrt->mroute_do_assert = val;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001501 mrt->mroute_do_wrvifwhole = do_wrvifwhole;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001502 }
1503 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001504 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001505 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1506 ret = -ENOPROTOOPT;
1507 break;
1508 }
1509 if (optlen != sizeof(uval)) {
1510 ret = -EINVAL;
1511 break;
1512 }
Christoph Hellwig01ccb5b2020-07-23 08:08:56 +02001513 if (copy_from_sockptr(&uval, optval, sizeof(uval))) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001514 ret = -EFAULT;
1515 break;
1516 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001517
Eric Dumazet4c9687092010-10-01 16:15:01 +00001518 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1519 ret = -EBUSY;
1520 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001521 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001522 if (IS_ERR(mrt))
1523 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001524 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001525 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001526 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001527 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001528 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001529 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001530 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001532out_unlock:
1533 rtnl_unlock();
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001534out:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001535 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536}
1537
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001538/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001539int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540{
1541 int olr;
1542 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001543 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001544 struct mr_table *mrt;
1545
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001546 if (sk->sk_type != SOCK_RAW ||
1547 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1548 return -EOPNOTSUPP;
1549
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001550 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001551 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001552 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001554 switch (optname) {
1555 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001556 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001557 break;
1558 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001559 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001560 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001561 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001562 break;
1563 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001564 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001565 break;
1566 default:
1567 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001568 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001569
1570 if (get_user(olr, optlen))
1571 return -EFAULT;
1572 olr = min_t(unsigned int, olr, sizeof(int));
1573 if (olr < 0)
1574 return -EINVAL;
1575 if (put_user(olr, optlen))
1576 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001577 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 return -EFAULT;
1579 return 0;
1580}
1581
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001582/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001583int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1584{
1585 struct sioc_sg_req sr;
1586 struct sioc_vif_req vr;
1587 struct vif_device *vif;
1588 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001589 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001590 struct mr_table *mrt;
1591
1592 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001593 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001594 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001595
Stephen Hemminger132adf52007-03-08 20:44:43 -08001596 switch (cmd) {
1597 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001598 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001599 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001600 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001601 return -EINVAL;
Gustavo A. R. Silva56484512018-12-10 12:41:24 -06001602 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001603 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001604 vif = &mrt->vif_table[vr.vifi];
1605 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001606 vr.icount = vif->pkt_in;
1607 vr.ocount = vif->pkt_out;
1608 vr.ibytes = vif->bytes_in;
1609 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001611
Jianjun Kongc354e122008-11-03 00:28:02 -08001612 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001614 return 0;
1615 }
1616 read_unlock(&mrt_lock);
1617 return -EADDRNOTAVAIL;
1618 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001619 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001620 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621
Eric Dumazeta8c94862010-10-01 16:15:08 +00001622 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001623 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001624 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001625 sr.pktcnt = c->_c.mfc_un.res.pkt;
1626 sr.bytecnt = c->_c.mfc_un.res.bytes;
1627 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001628 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001629
Jianjun Kongc354e122008-11-03 00:28:02 -08001630 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001631 return -EFAULT;
1632 return 0;
1633 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001634 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001635 return -EADDRNOTAVAIL;
1636 default:
1637 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 }
1639}
1640
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001641#ifdef CONFIG_COMPAT
1642struct compat_sioc_sg_req {
1643 struct in_addr src;
1644 struct in_addr grp;
1645 compat_ulong_t pktcnt;
1646 compat_ulong_t bytecnt;
1647 compat_ulong_t wrong_if;
1648};
1649
David S. Millerca6b8bb02011-02-03 17:24:28 -08001650struct compat_sioc_vif_req {
1651 vifi_t vifi; /* Which iface */
1652 compat_ulong_t icount;
1653 compat_ulong_t ocount;
1654 compat_ulong_t ibytes;
1655 compat_ulong_t obytes;
1656};
1657
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001658int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1659{
David S. Miller0033d5a2011-02-03 17:21:31 -08001660 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001661 struct compat_sioc_vif_req vr;
1662 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001663 struct mfc_cache *c;
1664 struct net *net = sock_net(sk);
1665 struct mr_table *mrt;
1666
1667 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001668 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001669 return -ENOENT;
1670
1671 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001672 case SIOCGETVIFCNT:
1673 if (copy_from_user(&vr, arg, sizeof(vr)))
1674 return -EFAULT;
1675 if (vr.vifi >= mrt->maxvif)
1676 return -EINVAL;
Gustavo A. R. Silva56484512018-12-10 12:41:24 -06001677 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
David S. Millerca6b8bb02011-02-03 17:24:28 -08001678 read_lock(&mrt_lock);
1679 vif = &mrt->vif_table[vr.vifi];
1680 if (VIF_EXISTS(mrt, vr.vifi)) {
1681 vr.icount = vif->pkt_in;
1682 vr.ocount = vif->pkt_out;
1683 vr.ibytes = vif->bytes_in;
1684 vr.obytes = vif->bytes_out;
1685 read_unlock(&mrt_lock);
1686
1687 if (copy_to_user(arg, &vr, sizeof(vr)))
1688 return -EFAULT;
1689 return 0;
1690 }
1691 read_unlock(&mrt_lock);
1692 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001693 case SIOCGETSGCNT:
1694 if (copy_from_user(&sr, arg, sizeof(sr)))
1695 return -EFAULT;
1696
1697 rcu_read_lock();
1698 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1699 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001700 sr.pktcnt = c->_c.mfc_un.res.pkt;
1701 sr.bytecnt = c->_c.mfc_un.res.bytes;
1702 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001703 rcu_read_unlock();
1704
1705 if (copy_to_user(arg, &sr, sizeof(sr)))
1706 return -EFAULT;
1707 return 0;
1708 }
1709 rcu_read_unlock();
1710 return -EADDRNOTAVAIL;
1711 default:
1712 return -ENOIOCTLCMD;
1713 }
1714}
1715#endif
1716
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1718{
Jiri Pirko351638e2013-05-28 01:30:21 +00001719 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001720 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001721 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 struct vif_device *v;
1723 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001724
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 if (event != NETDEV_UNREGISTER)
1726 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001727
1728 ipmr_for_each_table(mrt, net) {
1729 v = &mrt->vif_table[0];
1730 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1731 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001732 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001733 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 }
1735 return NOTIFY_DONE;
1736}
1737
Jianjun Kongc354e122008-11-03 00:28:02 -08001738static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 .notifier_call = ipmr_device_event,
1740};
1741
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001742/* Encapsulate a packet by attaching a valid IPIP header to it.
1743 * This avoids tunnel drivers and other mess and gives us the speed so
1744 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001746static void ip_encap(struct net *net, struct sk_buff *skb,
1747 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001749 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001750 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001751
1752 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001753 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001754 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001755 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001757 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001758 iph->tos = old_iph->tos;
1759 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 iph->frag_off = 0;
1761 iph->daddr = daddr;
1762 iph->saddr = saddr;
1763 iph->protocol = IPPROTO_IPIP;
1764 iph->ihl = 5;
1765 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001766 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 ip_send_check(iph);
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Florian Westphal895b5c92019-09-29 20:54:03 +02001770 nf_reset_ct(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771}
1772
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001773static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1774 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001776 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
David S. Miller73186df2015-11-03 13:41:45 -05001778 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1779 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 if (unlikely(opt->optlen))
1782 ip_forward_options(skb);
1783
Eric W. Biederman13206b62015-10-07 16:48:35 -05001784 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785}
1786
Yotam Gigia5bc9292017-10-03 09:58:08 +02001787#ifdef CONFIG_NET_SWITCHDEV
1788static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1789 int in_vifi, int out_vifi)
1790{
1791 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1792 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1793
Ido Schimmel875e8932018-12-04 08:15:10 +00001794 if (!skb->offload_l3_fwd_mark)
Yotam Gigia5bc9292017-10-03 09:58:08 +02001795 return false;
1796 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1797 return false;
1798 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1799 &in_vif->dev_parent_id);
1800}
1801#else
1802static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1803 int in_vifi, int out_vifi)
1804{
1805 return false;
1806}
1807#endif
1808
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001809/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
Patrick McHardy0c122952010-04-13 05:03:22 +00001811static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
David Ahern6e0735d2018-12-17 15:34:48 -08001812 int in_vifi, struct sk_buff *skb, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001814 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001815 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 struct net_device *dev;
1817 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001818 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 int encap = 0;
1820
Ian Morris51456b22015-04-03 09:17:26 +01001821 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 goto out_free;
1823
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (vif->flags & VIFF_REGISTER) {
1825 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001826 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001827 vif->dev->stats.tx_bytes += skb->len;
1828 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001829 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001830 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Yotam Gigia5bc9292017-10-03 09:58:08 +02001833 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1834 goto out_free;
1835
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001836 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001837 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001838 vif->remote, vif->local,
1839 0, 0,
1840 IPPROTO_IPIP,
1841 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001842 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 goto out_free;
1844 encap = sizeof(struct iphdr);
1845 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001846 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001847 0, 0,
1848 IPPROTO_IPIP,
1849 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001850 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 goto out_free;
1852 }
1853
Changli Gaod8d1f302010-06-10 23:31:35 -07001854 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Changli Gaod8d1f302010-06-10 23:31:35 -07001856 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001858 * allow to send ICMP, so that packets will disappear
1859 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 */
David S. Miller73186df2015-11-03 13:41:45 -05001861 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 ip_rt_put(rt);
1863 goto out_free;
1864 }
1865
Changli Gaod8d1f302010-06-10 23:31:35 -07001866 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001869 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto out_free;
1871 }
1872
1873 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001874 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Eric Dumazetadf30902009-06-02 05:19:30 +00001876 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001877 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001878 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001881 * What do we do with netfilter? -- RR
1882 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001884 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001886 vif->dev->stats.tx_packets++;
1887 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
1889
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001890 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001892 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 * not only before forwarding, but after forwarding on all output
1894 * interfaces. It is clear, if mrouter runs a multicasting
1895 * program, it should receive packets not depending to what interface
1896 * program is joined.
1897 * If we will not make it, the program will have to join on all
1898 * interfaces. On the other hand, multihoming host (or router, but
1899 * not mrouter) cannot join to more than one interface - it will
1900 * result in receiving multiple packets.
1901 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001902 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1903 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 ipmr_forward_finish);
1905 return;
1906
1907out_free:
1908 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909}
1910
Patrick McHardy0c122952010-04-13 05:03:22 +00001911static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912{
1913 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001914
1915 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1916 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 break;
1918 }
1919 return ct;
1920}
1921
1922/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001923static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001924 struct net_device *dev, struct sk_buff *skb,
Yuval Mintz494fff52018-02-28 23:29:34 +02001925 struct mfc_cache *c, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001927 int true_vifi = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 int psend = -1;
1929 int vif, ct;
1930
Yuval Mintz494fff52018-02-28 23:29:34 +02001931 vif = c->_c.mfc_parent;
1932 c->_c.mfc_un.res.pkt++;
1933 c->_c.mfc_un.res.bytes += skb->len;
1934 c->_c.mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935
Yuval Mintz494fff52018-02-28 23:29:34 +02001936 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001937 struct mfc_cache *cache_proxy;
1938
Zheng Yongjun974d8f82021-06-07 23:01:09 +08001939 /* For an (*,G) entry, we only check that the incoming
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001940 * interface is part of the static tree.
1941 */
Yuval Mintz845c9a72018-02-28 23:29:35 +02001942 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001943 if (cache_proxy &&
Yuval Mintz494fff52018-02-28 23:29:34 +02001944 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001945 goto forward;
1946 }
1947
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001948 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001949 if (mrt->vif_table[vif].dev != dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001950 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001952 * Very complicated situation...
1953 *
1954 * The best workaround until routing daemons will be
1955 * fixed is not to redistribute packet, if it was
1956 * send through wrong interface. It means, that
1957 * multicast applications WILL NOT work for
1958 * (S,G), which have default multicast route pointing
1959 * to wrong oif. In any case, it is not a good
1960 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 */
1962 goto dont_forward;
1963 }
1964
Yuval Mintz494fff52018-02-28 23:29:34 +02001965 c->_c.mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Patrick McHardy0c122952010-04-13 05:03:22 +00001967 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001969 * so that we cannot check that packet arrived on an oif.
1970 * It is bad, but otherwise we would need to move pretty
1971 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001973 (mrt->mroute_do_pim ||
Yuval Mintz494fff52018-02-28 23:29:34 +02001974 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001975 time_after(jiffies,
Yuval Mintz494fff52018-02-28 23:29:34 +02001976 c->_c.mfc_un.res.last_assert +
1977 MFC_ASSERT_THRESH)) {
1978 c->_c.mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001979 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001980 if (mrt->mroute_do_wrvifwhole)
1981 ipmr_cache_report(mrt, skb, true_vifi,
1982 IGMPMSG_WRVIFWHOLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 }
1984 goto dont_forward;
1985 }
1986
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001987forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001988 mrt->vif_table[vif].pkt_in++;
1989 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001991 /* Forward the frame */
Yuval Mintz494fff52018-02-28 23:29:34 +02001992 if (c->mfc_origin == htonl(INADDR_ANY) &&
1993 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001994 if (true_vifi >= 0 &&
Yuval Mintz494fff52018-02-28 23:29:34 +02001995 true_vifi != c->_c.mfc_parent &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001996 ip_hdr(skb)->ttl >
Yuval Mintz494fff52018-02-28 23:29:34 +02001997 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001998 /* It's an (*,*) entry and the packet is not coming from
1999 * the upstream: forward the packet to the upstream
2000 * only.
2001 */
Yuval Mintz494fff52018-02-28 23:29:34 +02002002 psend = c->_c.mfc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002003 goto last_forward;
2004 }
2005 goto dont_forward;
2006 }
Yuval Mintz494fff52018-02-28 23:29:34 +02002007 for (ct = c->_c.mfc_un.res.maxvif - 1;
2008 ct >= c->_c.mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002009 /* For (*,G) entry, don't forward to the incoming interface */
Yuval Mintz494fff52018-02-28 23:29:34 +02002010 if ((c->mfc_origin != htonl(INADDR_ANY) ||
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002011 ct != true_vifi) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002012 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 if (psend != -1) {
2014 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002015
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002017 ipmr_queue_xmit(net, mrt, true_vifi,
David Ahern6e0735d2018-12-17 15:34:48 -08002018 skb2, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002020 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 }
2022 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002023last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 if (psend != -1) {
2025 if (local) {
2026 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002029 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
David Ahern6e0735d2018-12-17 15:34:48 -08002030 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 } else {
David Ahern6e0735d2018-12-17 15:34:48 -08002032 ipmr_queue_xmit(net, mrt, true_vifi, skb, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03002033 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
2035 }
2036
2037dont_forward:
2038 if (!local)
2039 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040}
2041
David S. Miller417da662011-05-03 19:42:43 -07002042static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08002043{
David S. Miller417da662011-05-03 19:42:43 -07002044 struct rtable *rt = skb_rtable(skb);
2045 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05002046 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07002047 .daddr = iph->daddr,
2048 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00002049 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07002050 .flowi4_oif = (rt_is_output_route(rt) ?
2051 skb->dev->ifindex : 0),
2052 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002053 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07002054 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00002055 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08002056 };
2057 struct mr_table *mrt;
2058 int err;
2059
David S. Millerda919812011-03-12 02:04:50 -05002060 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002061 if (err)
2062 return ERR_PTR(err);
2063 return mrt;
2064}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002066/* Multicast packets for forwarding arrive here
2067 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069int ip_mr_input(struct sk_buff *skb)
2070{
2071 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002072 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00002073 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002074 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002075 struct net_device *dev;
2076
2077 /* skb->dev passed in is the loX master dev for vrfs.
2078 * As there are no vifs associated with loopback devices,
2079 * get the proper interface that does have a vif associated with it.
2080 */
2081 dev = skb->dev;
2082 if (netif_is_l3_master(skb->dev)) {
2083 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2084 if (!dev) {
2085 kfree_skb(skb);
2086 return -ENODEV;
2087 }
2088 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
2090 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002091 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00002093 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 goto dont_forward;
2095
David S. Miller417da662011-05-03 19:42:43 -07002096 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002097 if (IS_ERR(mrt)) {
2098 kfree_skb(skb);
2099 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00002102 if (IPCB(skb)->opt.router_alert) {
2103 if (ip_call_ra_chain(skb))
2104 return 0;
2105 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2106 /* IGMPv1 (and broken IGMPv2 implementations sort of
2107 * Cisco IOS <= 11.2(8)) do not put router alert
2108 * option to IGMP packets destined to routable
2109 * groups. It is very bad, because it means
2110 * that we can forward NO IGMP messages.
2111 */
2112 struct sock *mroute_sk;
2113
2114 mroute_sk = rcu_dereference(mrt->mroute_sk);
2115 if (mroute_sk) {
Florian Westphal895b5c92019-09-29 20:54:03 +02002116 nf_reset_ct(skb);
Eric Dumazet4c9687092010-10-01 16:15:01 +00002117 raw_rcv(mroute_sk, skb);
2118 return 0;
2119 }
Roy, UjjaL92c4bed2021-07-07 23:48:33 +05302120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 }
2122
Eric Dumazeta8c94862010-10-01 16:15:08 +00002123 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002124 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002125 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002126 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002127
2128 if (vif >= 0)
2129 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2130 vif);
2131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002133 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002134 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 int vif;
2136
2137 if (local) {
2138 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2139 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002140 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 skb = skb2;
2143 }
2144
Eric Dumazeta8c94862010-10-01 16:15:08 +00002145 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002146 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 if (vif >= 0) {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002148 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 read_unlock(&mrt_lock);
2150
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002151 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 }
2153 read_unlock(&mrt_lock);
2154 kfree_skb(skb);
2155 return -ENODEV;
2156 }
2157
Eric Dumazeta8c94862010-10-01 16:15:08 +00002158 read_lock(&mrt_lock);
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002159 ip_mr_forward(net, mrt, dev, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 read_unlock(&mrt_lock);
2161
2162 if (local)
2163 return ip_local_deliver(skb);
2164
2165 return 0;
2166
2167dont_forward:
2168 if (local)
2169 return ip_local_deliver(skb);
2170 kfree_skb(skb);
2171 return 0;
2172}
2173
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002174#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002175/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002176int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002177{
2178 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002179 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002180 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002181
2182 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2183 goto drop;
2184
2185 pim = igmp_hdr(skb);
2186
David S. Miller417da662011-05-03 19:42:43 -07002187 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002188 if (IS_ERR(mrt))
2189 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002190 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002191 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2192 goto drop;
2193
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002194 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002195drop:
2196 kfree_skb(skb);
2197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 return 0;
2199}
2200#endif
2201
2202#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002203static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204{
2205 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002206 struct net *net = dev_net(skb->dev);
2207 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002209 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 goto drop;
2211
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002212 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002213 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002214 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002215 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002216 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 goto drop;
2218
David S. Miller417da662011-05-03 19:42:43 -07002219 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002220 if (IS_ERR(mrt))
2221 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002222 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002223drop:
2224 kfree_skb(skb);
2225 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 return 0;
2227}
2228#endif
2229
David S. Miller9a1b9492011-05-04 12:18:54 -07002230int ipmr_get_route(struct net *net, struct sk_buff *skb,
2231 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002232 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002234 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002235 struct mr_table *mrt;
2236 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002238 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002239 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002240 return -ENOENT;
2241
Eric Dumazeta8c94862010-10-01 16:15:08 +00002242 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002243 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002244 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002245 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002247 if (vif >= 0)
2248 cache = ipmr_cache_find_any(mrt, daddr, vif);
2249 }
Ian Morris51456b22015-04-03 09:17:26 +01002250 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002251 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002252 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002254 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002257 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002258 if (dev)
2259 vif = ipmr_find_vif(mrt, dev);
2260 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002262 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263 return -ENODEV;
2264 }
Guillaume Nault7901cd92019-11-15 18:29:52 +01002265
2266 skb2 = skb_realloc_headroom(skb, sizeof(struct iphdr));
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002267 if (!skb2) {
2268 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002269 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002270 return -ENOMEM;
2271 }
2272
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002273 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002274 skb_push(skb2, sizeof(struct iphdr));
2275 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002276 iph = ip_hdr(skb2);
2277 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002278 iph->saddr = saddr;
2279 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002280 iph->version = 0;
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002281 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002283 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 return err;
2285 }
2286
Eric Dumazeta8c94862010-10-01 16:15:08 +00002287 read_lock(&mrt_lock);
Yuval Mintz7b0db852018-02-28 23:29:39 +02002288 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002290 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return err;
2292}
2293
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002294static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002295 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2296 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002297{
2298 struct nlmsghdr *nlh;
2299 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002300 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002301
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002302 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002303 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002304 return -EMSGSIZE;
2305
2306 rtm = nlmsg_data(nlh);
2307 rtm->rtm_family = RTNL_FAMILY_IPMR;
2308 rtm->rtm_dst_len = 32;
2309 rtm->rtm_src_len = 32;
2310 rtm->rtm_tos = 0;
2311 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002312 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2313 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002314 rtm->rtm_type = RTN_MULTICAST;
2315 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Yuval Mintz494fff52018-02-28 23:29:34 +02002316 if (c->_c.mfc_flags & MFC_STATIC)
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002317 rtm->rtm_protocol = RTPROT_STATIC;
2318 else
2319 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002320 rtm->rtm_flags = 0;
2321
Jiri Benc930345e2015-03-29 16:59:25 +02002322 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2323 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002324 goto nla_put_failure;
Yuval Mintz7b0db852018-02-28 23:29:39 +02002325 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002326 /* do not break the dump if cache is unresolved */
2327 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002328 goto nla_put_failure;
2329
Johannes Berg053c0952015-01-16 22:09:00 +01002330 nlmsg_end(skb, nlh);
2331 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002332
2333nla_put_failure:
2334 nlmsg_cancel(skb, nlh);
2335 return -EMSGSIZE;
2336}
2337
Yuval Mintz7b0db852018-02-28 23:29:39 +02002338static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2339 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2340 int flags)
2341{
2342 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2343 cmd, flags);
2344}
2345
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002346static size_t mroute_msgsize(bool unresolved, int maxvif)
2347{
2348 size_t len =
2349 NLMSG_ALIGN(sizeof(struct rtmsg))
2350 + nla_total_size(4) /* RTA_TABLE */
2351 + nla_total_size(4) /* RTA_SRC */
2352 + nla_total_size(4) /* RTA_DST */
2353 ;
2354
2355 if (!unresolved)
2356 len = len
2357 + nla_total_size(4) /* RTA_IIF */
2358 + nla_total_size(0) /* RTA_MULTIPATH */
2359 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2360 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002361 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002362 ;
2363
2364 return len;
2365}
2366
2367static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2368 int cmd)
2369{
2370 struct net *net = read_pnet(&mrt->net);
2371 struct sk_buff *skb;
2372 int err = -ENOBUFS;
2373
Yuval Mintz494fff52018-02-28 23:29:34 +02002374 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2375 mrt->maxvif),
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002376 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002377 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002378 goto errout;
2379
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002380 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002381 if (err < 0)
2382 goto errout;
2383
2384 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2385 return;
2386
2387errout:
2388 kfree_skb(skb);
2389 if (err < 0)
2390 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2391}
2392
Julien Gomes5a645dd2017-06-20 13:54:17 -07002393static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2394{
2395 size_t len =
2396 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2397 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2398 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2399 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2400 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
Paul Davey501cb002020-09-08 10:04:06 +12002401 + nla_total_size(4) /* IPMRA_CREPORT_TABLE */
Julien Gomes5a645dd2017-06-20 13:54:17 -07002402 /* IPMRA_CREPORT_PKT */
2403 + nla_total_size(payloadlen)
2404 ;
2405
2406 return len;
2407}
2408
2409static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2410{
2411 struct net *net = read_pnet(&mrt->net);
2412 struct nlmsghdr *nlh;
2413 struct rtgenmsg *rtgenm;
2414 struct igmpmsg *msg;
2415 struct sk_buff *skb;
2416 struct nlattr *nla;
2417 int payloadlen;
2418
2419 payloadlen = pkt->len - sizeof(struct igmpmsg);
2420 msg = (struct igmpmsg *)skb_network_header(pkt);
2421
2422 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2423 if (!skb)
2424 goto errout;
2425
2426 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2427 sizeof(struct rtgenmsg), 0);
2428 if (!nlh)
2429 goto errout;
2430 rtgenm = nlmsg_data(nlh);
2431 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2432 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
Paul Daveybb82067c2020-09-08 10:04:08 +12002433 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif | (msg->im_vif_hi << 8)) ||
Julien Gomes5a645dd2017-06-20 13:54:17 -07002434 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2435 msg->im_src.s_addr) ||
2436 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
Paul Davey501cb002020-09-08 10:04:06 +12002437 msg->im_dst.s_addr) ||
2438 nla_put_u32(skb, IPMRA_CREPORT_TABLE, mrt->id))
Julien Gomes5a645dd2017-06-20 13:54:17 -07002439 goto nla_put_failure;
2440
2441 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2442 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2443 nla_data(nla), payloadlen))
2444 goto nla_put_failure;
2445
2446 nlmsg_end(skb, nlh);
2447
2448 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2449 return;
2450
2451nla_put_failure:
2452 nlmsg_cancel(skb, nlh);
2453errout:
2454 kfree_skb(skb);
2455 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2456}
2457
Jakub Kicinskid0440022019-01-18 10:46:20 -08002458static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb,
2459 const struct nlmsghdr *nlh,
2460 struct nlattr **tb,
2461 struct netlink_ext_ack *extack)
2462{
2463 struct rtmsg *rtm;
2464 int i, err;
2465
2466 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
2467 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for multicast route get request");
2468 return -EINVAL;
2469 }
2470
2471 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02002472 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
2473 rtm_ipv4_policy, extack);
Jakub Kicinskid0440022019-01-18 10:46:20 -08002474
2475 rtm = nlmsg_data(nlh);
2476 if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
2477 (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
2478 rtm->rtm_tos || rtm->rtm_table || rtm->rtm_protocol ||
2479 rtm->rtm_scope || rtm->rtm_type || rtm->rtm_flags) {
2480 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for multicast route get request");
2481 return -EINVAL;
2482 }
2483
Johannes Berg8cb08172019-04-26 14:07:28 +02002484 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
2485 rtm_ipv4_policy, extack);
Jakub Kicinskid0440022019-01-18 10:46:20 -08002486 if (err)
2487 return err;
2488
2489 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
2490 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
2491 NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
2492 return -EINVAL;
2493 }
2494
2495 for (i = 0; i <= RTA_MAX; i++) {
2496 if (!tb[i])
2497 continue;
2498
2499 switch (i) {
2500 case RTA_SRC:
2501 case RTA_DST:
2502 case RTA_TABLE:
2503 break;
2504 default:
2505 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in multicast route get request");
2506 return -EINVAL;
2507 }
2508 }
2509
2510 return 0;
2511}
2512
Donald Sharp4f75ba62017-06-28 13:58:57 -04002513static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2514 struct netlink_ext_ack *extack)
2515{
2516 struct net *net = sock_net(in_skb->sk);
2517 struct nlattr *tb[RTA_MAX + 1];
2518 struct sk_buff *skb = NULL;
2519 struct mfc_cache *cache;
2520 struct mr_table *mrt;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002521 __be32 src, grp;
2522 u32 tableid;
2523 int err;
2524
Jakub Kicinskid0440022019-01-18 10:46:20 -08002525 err = ipmr_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Donald Sharp4f75ba62017-06-28 13:58:57 -04002526 if (err < 0)
2527 goto errout;
2528
Donald Sharp4f75ba62017-06-28 13:58:57 -04002529 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2530 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2531 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2532
2533 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
Dan Carpenter2e3d2322017-07-12 10:56:47 +03002534 if (!mrt) {
2535 err = -ENOENT;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002536 goto errout_free;
2537 }
2538
2539 /* entries are added/deleted only under RTNL */
2540 rcu_read_lock();
2541 cache = ipmr_cache_find(mrt, src, grp);
2542 rcu_read_unlock();
2543 if (!cache) {
2544 err = -ENOENT;
2545 goto errout_free;
2546 }
2547
2548 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2549 if (!skb) {
2550 err = -ENOBUFS;
2551 goto errout_free;
2552 }
2553
2554 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2555 nlh->nlmsg_seq, cache,
2556 RTM_NEWROUTE, 0);
2557 if (err < 0)
2558 goto errout_free;
2559
2560 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2561
2562errout:
2563 return err;
2564
2565errout_free:
2566 kfree_skb(skb);
2567 goto errout;
2568}
2569
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002570static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2571{
David Ahern47246762018-10-15 18:56:42 -07002572 struct fib_dump_filter filter = {};
David Aherncb167892018-10-15 18:56:47 -07002573 int err;
David Aherne8ba3302018-10-07 20:16:35 -07002574
David Ahern47246762018-10-15 18:56:42 -07002575 if (cb->strict_check) {
David Ahern47246762018-10-15 18:56:42 -07002576 err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh,
David Aherneffe6792018-10-15 18:56:48 -07002577 &filter, cb);
David Aherne8ba3302018-10-07 20:16:35 -07002578 if (err < 0)
2579 return err;
2580 }
2581
David Aherncb167892018-10-15 18:56:47 -07002582 if (filter.table_id) {
2583 struct mr_table *mrt;
2584
2585 mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id);
2586 if (!mrt) {
Sabrina Dubroca41b4bd92020-05-20 11:15:46 +02002587 if (rtnl_msg_family(cb->nlh) != RTNL_FAMILY_IPMR)
David Ahernae677bb2018-10-24 12:59:01 -07002588 return skb->len;
2589
David Aherncb167892018-10-15 18:56:47 -07002590 NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist");
2591 return -ENOENT;
2592 }
2593 err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute,
2594 &mfc_unres_lock, &filter);
2595 return skb->len ? : err;
2596 }
2597
Yuval Mintz7b0db852018-02-28 23:29:39 +02002598 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
David Aherncb167892018-10-15 18:56:47 -07002599 _ipmr_fill_mroute, &mfc_unres_lock, &filter);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002600}
2601
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002602static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2603 [RTA_SRC] = { .type = NLA_U32 },
2604 [RTA_DST] = { .type = NLA_U32 },
2605 [RTA_IIF] = { .type = NLA_U32 },
2606 [RTA_TABLE] = { .type = NLA_U32 },
2607 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2608};
2609
2610static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2611{
2612 switch (rtm_protocol) {
2613 case RTPROT_STATIC:
2614 case RTPROT_MROUTED:
2615 return true;
2616 }
2617 return false;
2618}
2619
2620static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2621{
2622 struct rtnexthop *rtnh = nla_data(nla);
2623 int remaining = nla_len(nla), vifi = 0;
2624
2625 while (rtnh_ok(rtnh, remaining)) {
2626 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2627 if (++vifi == MAXVIFS)
2628 break;
2629 rtnh = rtnh_next(rtnh, &remaining);
2630 }
2631
2632 return remaining > 0 ? -EINVAL : vifi;
2633}
2634
2635/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2636static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2637 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002638 struct mr_table **mrtret,
2639 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002640{
2641 struct net_device *dev = NULL;
2642 u32 tblid = RT_TABLE_DEFAULT;
2643 struct mr_table *mrt;
2644 struct nlattr *attr;
2645 struct rtmsg *rtm;
2646 int ret, rem;
2647
Johannes Berg8cb08172019-04-26 14:07:28 +02002648 ret = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
2649 rtm_ipmr_policy, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002650 if (ret < 0)
2651 goto out;
2652 rtm = nlmsg_data(nlh);
2653
2654 ret = -EINVAL;
2655 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2656 rtm->rtm_type != RTN_MULTICAST ||
2657 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2658 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2659 goto out;
2660
2661 memset(mfcc, 0, sizeof(*mfcc));
2662 mfcc->mfcc_parent = -1;
2663 ret = 0;
2664 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2665 switch (nla_type(attr)) {
2666 case RTA_SRC:
2667 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2668 break;
2669 case RTA_DST:
2670 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2671 break;
2672 case RTA_IIF:
2673 dev = __dev_get_by_index(net, nla_get_u32(attr));
2674 if (!dev) {
2675 ret = -ENODEV;
2676 goto out;
2677 }
2678 break;
2679 case RTA_MULTIPATH:
2680 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2681 ret = -EINVAL;
2682 goto out;
2683 }
2684 break;
2685 case RTA_PREFSRC:
2686 ret = 1;
2687 break;
2688 case RTA_TABLE:
2689 tblid = nla_get_u32(attr);
2690 break;
2691 }
2692 }
2693 mrt = ipmr_get_table(net, tblid);
2694 if (!mrt) {
2695 ret = -ENOENT;
2696 goto out;
2697 }
2698 *mrtret = mrt;
2699 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2700 if (dev)
2701 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2702
2703out:
2704 return ret;
2705}
2706
2707/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002708static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2709 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002710{
2711 struct net *net = sock_net(skb->sk);
2712 int ret, mrtsock, parent;
2713 struct mr_table *tbl;
2714 struct mfcctl mfcc;
2715
2716 mrtsock = 0;
2717 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002718 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002719 if (ret < 0)
2720 return ret;
2721
2722 parent = ret ? mfcc.mfcc_parent : -1;
2723 if (nlh->nlmsg_type == RTM_NEWROUTE)
2724 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2725 else
2726 return ipmr_mfc_delete(tbl, &mfcc, parent);
2727}
2728
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002729static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2730{
2731 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2732
2733 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2734 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2735 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2736 mrt->mroute_reg_vif_num) ||
2737 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2738 mrt->mroute_do_assert) ||
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03002739 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
2740 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
2741 mrt->mroute_do_wrvifwhole))
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002742 return false;
2743
2744 return true;
2745}
2746
2747static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2748{
2749 struct nlattr *vif_nest;
2750 struct vif_device *vif;
2751
2752 /* if the VIF doesn't exist just continue */
2753 if (!VIF_EXISTS(mrt, vifid))
2754 return true;
2755
2756 vif = &mrt->vif_table[vifid];
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002757 vif_nest = nla_nest_start_noflag(skb, IPMRA_VIF);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002758 if (!vif_nest)
2759 return false;
2760 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2761 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2762 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2763 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2764 IPMRA_VIFA_PAD) ||
2765 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2766 IPMRA_VIFA_PAD) ||
2767 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2768 IPMRA_VIFA_PAD) ||
2769 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2770 IPMRA_VIFA_PAD) ||
2771 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2772 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2773 nla_nest_cancel(skb, vif_nest);
2774 return false;
2775 }
2776 nla_nest_end(skb, vif_nest);
2777
2778 return true;
2779}
2780
David Ahern14fc5bb2018-10-07 20:16:34 -07002781static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
2782 struct netlink_ext_ack *extack)
2783{
2784 struct ifinfomsg *ifm;
2785
2786 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2787 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
2788 return -EINVAL;
2789 }
2790
2791 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
2792 NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump");
2793 return -EINVAL;
2794 }
2795
2796 ifm = nlmsg_data(nlh);
2797 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2798 ifm->ifi_change || ifm->ifi_index) {
2799 NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
2800 return -EINVAL;
2801 }
2802
2803 return 0;
2804}
2805
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002806static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2807{
2808 struct net *net = sock_net(skb->sk);
2809 struct nlmsghdr *nlh = NULL;
2810 unsigned int t = 0, s_t;
2811 unsigned int e = 0, s_e;
2812 struct mr_table *mrt;
2813
David Ahern14fc5bb2018-10-07 20:16:34 -07002814 if (cb->strict_check) {
2815 int err = ipmr_valid_dumplink(cb->nlh, cb->extack);
2816
2817 if (err < 0)
2818 return err;
2819 }
2820
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002821 s_t = cb->args[0];
2822 s_e = cb->args[1];
2823
2824 ipmr_for_each_table(mrt, net) {
2825 struct nlattr *vifs, *af;
2826 struct ifinfomsg *hdr;
2827 u32 i;
2828
2829 if (t < s_t)
2830 goto skip_table;
2831 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2832 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2833 sizeof(*hdr), NLM_F_MULTI);
2834 if (!nlh)
2835 break;
2836
2837 hdr = nlmsg_data(nlh);
2838 memset(hdr, 0, sizeof(*hdr));
2839 hdr->ifi_family = RTNL_FAMILY_IPMR;
2840
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002841 af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002842 if (!af) {
2843 nlmsg_cancel(skb, nlh);
2844 goto out;
2845 }
2846
2847 if (!ipmr_fill_table(mrt, skb)) {
2848 nlmsg_cancel(skb, nlh);
2849 goto out;
2850 }
2851
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002852 vifs = nla_nest_start_noflag(skb, IPMRA_TABLE_VIFS);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002853 if (!vifs) {
2854 nla_nest_end(skb, af);
2855 nlmsg_end(skb, nlh);
2856 goto out;
2857 }
2858 for (i = 0; i < mrt->maxvif; i++) {
2859 if (e < s_e)
2860 goto skip_entry;
2861 if (!ipmr_fill_vif(mrt, i, skb)) {
2862 nla_nest_end(skb, vifs);
2863 nla_nest_end(skb, af);
2864 nlmsg_end(skb, nlh);
2865 goto out;
2866 }
2867skip_entry:
2868 e++;
2869 }
2870 s_e = 0;
2871 e = 0;
2872 nla_nest_end(skb, vifs);
2873 nla_nest_end(skb, af);
2874 nlmsg_end(skb, nlh);
2875skip_table:
2876 t++;
2877 }
2878
2879out:
2880 cb->args[1] = e;
2881 cb->args[0] = t;
2882
2883 return skb->len;
2884}
2885
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002886#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002887/* The /proc interfaces to multicast routing :
2888 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890
2891static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002892 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002894 struct mr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002895 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002896 struct mr_table *mrt;
2897
2898 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002899 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002900 return ERR_PTR(-ENOENT);
2901
2902 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002903
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 read_lock(&mrt_lock);
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002905 return mr_vif_seq_start(seq, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002906}
2907
2908static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002909 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910{
2911 read_unlock(&mrt_lock);
2912}
2913
2914static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2915{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002916 struct mr_vif_iter *iter = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002917 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002918
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002920 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002921 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2922 } else {
2923 const struct vif_device *vif = v;
Yuval Mintz6853f212018-02-28 23:29:29 +02002924 const char *name = vif->dev ?
2925 vif->dev->name : "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926
2927 seq_printf(seq,
James Hogan91e6dd82018-01-30 09:48:02 +00002928 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002929 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002930 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 vif->bytes_out, vif->pkt_out,
2932 vif->flags, vif->local, vif->remote);
2933 }
2934 return 0;
2935}
2936
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002937static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938 .start = ipmr_vif_seq_start,
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002939 .next = mr_vif_seq_next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 .stop = ipmr_vif_seq_stop,
2941 .show = ipmr_vif_seq_show,
2942};
2943
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2945{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002946 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002947 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002948
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002949 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002950 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002951 return ERR_PTR(-ENOENT);
2952
Yuval Mintzc8d61962018-02-28 23:29:36 +02002953 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954}
2955
2956static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2957{
2958 int n;
2959
2960 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002961 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2963 } else {
2964 const struct mfc_cache *mfc = v;
Yuval Mintzc8d61962018-02-28 23:29:36 +02002965 const struct mr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002966 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002967
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002968 seq_printf(seq, "%08X %08X %-3hd",
2969 (__force u32) mfc->mfc_mcastgrp,
2970 (__force u32) mfc->mfc_origin,
Yuval Mintz494fff52018-02-28 23:29:34 +02002971 mfc->_c.mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
Patrick McHardy0c122952010-04-13 05:03:22 +00002973 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002974 seq_printf(seq, " %8lu %8lu %8lu",
Yuval Mintz494fff52018-02-28 23:29:34 +02002975 mfc->_c.mfc_un.res.pkt,
2976 mfc->_c.mfc_un.res.bytes,
2977 mfc->_c.mfc_un.res.wrong_if);
2978 for (n = mfc->_c.mfc_un.res.minvif;
2979 n < mfc->_c.mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002980 if (VIF_EXISTS(mrt, n) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002981 mfc->_c.mfc_un.res.ttls[n] < 255)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002982 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002983 " %2d:%-3d",
Yuval Mintz494fff52018-02-28 23:29:34 +02002984 n, mfc->_c.mfc_un.res.ttls[n]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002986 } else {
2987 /* unresolved mfc_caches don't contain
2988 * pkt, bytes and wrong_if values
2989 */
2990 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 }
2992 seq_putc(seq, '\n');
2993 }
2994 return 0;
2995}
2996
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002997static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 .start = ipmr_mfc_seq_start,
Yuval Mintzc8d61962018-02-28 23:29:36 +02002999 .next = mr_mfc_seq_next,
3000 .stop = mr_mfc_seq_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 .show = ipmr_mfc_seq_show,
3002};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003003#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004
3005#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00003006static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007 .handler = pim_rcv,
3008};
3009#endif
3010
Yotam Gigi4d65b942017-09-27 08:23:13 +02003011static unsigned int ipmr_seq_read(struct net *net)
3012{
3013 ASSERT_RTNL();
3014
3015 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
3016}
3017
Jiri Pirkob7a59552019-10-03 11:49:30 +02003018static int ipmr_dump(struct net *net, struct notifier_block *nb,
3019 struct netlink_ext_ack *extack)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003020{
Yuval Mintzcdc9f942018-03-26 15:01:33 +03003021 return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
Jiri Pirkob7a59552019-10-03 11:49:30 +02003022 ipmr_mr_table_iter, &mrt_lock, extack);
Yotam Gigi4d65b942017-09-27 08:23:13 +02003023}
3024
3025static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3026 .family = RTNL_FAMILY_IPMR,
3027 .fib_seq_read = ipmr_seq_read,
3028 .fib_dump = ipmr_dump,
3029 .owner = THIS_MODULE,
3030};
3031
Colin Ian Kingef739d82017-09-29 14:34:22 +01003032static int __net_init ipmr_notifier_init(struct net *net)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003033{
3034 struct fib_notifier_ops *ops;
3035
3036 net->ipv4.ipmr_seq = 0;
3037
3038 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3039 if (IS_ERR(ops))
3040 return PTR_ERR(ops);
3041 net->ipv4.ipmr_notifier_ops = ops;
3042
3043 return 0;
3044}
3045
3046static void __net_exit ipmr_notifier_exit(struct net *net)
3047{
3048 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3049 net->ipv4.ipmr_notifier_ops = NULL;
3050}
3051
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01003052/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00003053static int __net_init ipmr_net_init(struct net *net)
3054{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003055 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00003056
Yotam Gigi4d65b942017-09-27 08:23:13 +02003057 err = ipmr_notifier_init(net);
3058 if (err)
3059 goto ipmr_notifier_fail;
3060
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003061 err = ipmr_rules_init(net);
3062 if (err < 0)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003063 goto ipmr_rules_fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003064
3065#ifdef CONFIG_PROC_FS
3066 err = -ENOMEM;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003067 if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
3068 sizeof(struct mr_vif_iter)))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003069 goto proc_vif_fail;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003070 if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
3071 sizeof(struct mr_mfc_iter)))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003072 goto proc_cache_fail;
3073#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00003074 return 0;
3075
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003076#ifdef CONFIG_PROC_FS
3077proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00003078 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003079proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003080 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003081#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003082ipmr_rules_fail:
3083 ipmr_notifier_exit(net);
3084ipmr_notifier_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003085 return err;
3086}
3087
3088static void __net_exit ipmr_net_exit(struct net *net)
3089{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003090#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003091 remove_proc_entry("ip_mr_cache", net->proc_net);
3092 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003093#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003094 ipmr_notifier_exit(net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003095 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00003096}
3097
3098static struct pernet_operations ipmr_net_ops = {
3099 .init = ipmr_net_init,
3100 .exit = ipmr_net_exit,
3101};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003102
Wang Chen03d2f892008-07-03 12:13:36 +08003103int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Wang Chen03d2f892008-07-03 12:13:36 +08003105 int err;
3106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3108 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00003109 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003110 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08003111
Benjamin Therycf958ae32009-01-22 04:56:16 +00003112 err = register_pernet_subsys(&ipmr_net_ops);
3113 if (err)
3114 goto reg_pernet_fail;
3115
Wang Chen03d2f892008-07-03 12:13:36 +08003116 err = register_netdevice_notifier(&ip_mr_notifier);
3117 if (err)
3118 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07003119#ifdef CONFIG_IP_PIMSM_V2
3120 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00003121 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07003122 err = -EAGAIN;
3123 goto add_proto_fail;
3124 }
3125#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00003126 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003127 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003128 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003129 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003130 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003131 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03003132
3133 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
Florian Westphalb97bac62017-08-09 20:41:48 +02003134 NULL, ipmr_rtm_dumplink, 0);
Wang Chen03d2f892008-07-03 12:13:36 +08003135 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003136
Tom Goff403dbb92009-06-14 03:16:13 -07003137#ifdef CONFIG_IP_PIMSM_V2
3138add_proto_fail:
3139 unregister_netdevice_notifier(&ip_mr_notifier);
3140#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08003141reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003142 unregister_pernet_subsys(&ipmr_net_ops);
3143reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08003144 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08003145 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146}