blob: 4c7aca884fa9a35816008a5f3a1a58dd1baf6c06 [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
198static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
199 FRA_GENERIC_POLICY,
200};
201
202static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
Roopa Prabhub16fb412018-04-21 09:41:31 -0700203 struct fib_rule_hdr *frh, struct nlattr **tb,
204 struct netlink_ext_ack *extack)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000205{
206 return 0;
207}
208
209static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
210 struct nlattr **tb)
211{
212 return 1;
213}
214
215static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
216 struct fib_rule_hdr *frh)
217{
218 frh->dst_len = 0;
219 frh->src_len = 0;
220 frh->tos = 0;
221 return 0;
222}
223
Andi Kleen04a6f822012-10-04 17:12:11 -0700224static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200225 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000226 .rule_size = sizeof(struct ipmr_rule),
227 .addr_size = sizeof(u32),
228 .action = ipmr_rule_action,
229 .match = ipmr_rule_match,
230 .configure = ipmr_rule_configure,
231 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000232 .fill = ipmr_rule_fill,
233 .nlgroup = RTNLGRP_IPV4_RULE,
234 .policy = ipmr_rule_policy,
235 .owner = THIS_MODULE,
236};
237
238static int __net_init ipmr_rules_init(struct net *net)
239{
240 struct fib_rules_ops *ops;
241 struct mr_table *mrt;
242 int err;
243
244 ops = fib_rules_register(&ipmr_rules_ops_template, net);
245 if (IS_ERR(ops))
246 return PTR_ERR(ops);
247
248 INIT_LIST_HEAD(&net->ipv4.mr_tables);
249
250 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100251 if (IS_ERR(mrt)) {
252 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000253 goto err1;
254 }
255
256 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
257 if (err < 0)
258 goto err2;
259
260 net->ipv4.mr_rules_ops = ops;
261 return 0;
262
263err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700264 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000265err1:
266 fib_rules_unregister(ops);
267 return err;
268}
269
270static void __net_exit ipmr_rules_exit(struct net *net)
271{
272 struct mr_table *mrt, *next;
273
WANG Conged785302015-03-31 11:01:45 -0700274 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000275 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
276 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000277 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000278 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000279 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700280 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000281}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200282
Jiri Pirkob7a59552019-10-03 11:49:30 +0200283static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,
284 struct netlink_ext_ack *extack)
Yotam Gigi4d65b942017-09-27 08:23:13 +0200285{
Jiri Pirkob7a59552019-10-03 11:49:30 +0200286 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR, extack);
Yotam Gigi4d65b942017-09-27 08:23:13 +0200287}
288
289static unsigned int ipmr_rules_seq_read(struct net *net)
290{
291 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
292}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200293
294bool ipmr_rule_default(const struct fib_rule *rule)
295{
296 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
297}
298EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000299#else
300#define ipmr_for_each_table(mrt, net) \
301 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
302
Yuval Mintz7b0db852018-02-28 23:29:39 +0200303static struct mr_table *ipmr_mr_table_iter(struct net *net,
304 struct mr_table *mrt)
305{
306 if (!mrt)
307 return net->ipv4.mrt;
308 return NULL;
309}
310
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000311static struct mr_table *ipmr_get_table(struct net *net, u32 id)
312{
313 return net->ipv4.mrt;
314}
315
David S. Millerda919812011-03-12 02:04:50 -0500316static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000317 struct mr_table **mrt)
318{
319 *mrt = net->ipv4.mrt;
320 return 0;
321}
322
323static int __net_init ipmr_rules_init(struct net *net)
324{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100325 struct mr_table *mrt;
326
327 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
328 if (IS_ERR(mrt))
329 return PTR_ERR(mrt);
330 net->ipv4.mrt = mrt;
331 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000332}
333
334static void __net_exit ipmr_rules_exit(struct net *net)
335{
WANG Conged785302015-03-31 11:01:45 -0700336 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000337 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700338 net->ipv4.mrt = NULL;
339 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000340}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200341
Jiri Pirkob7a59552019-10-03 11:49:30 +0200342static int ipmr_rules_dump(struct net *net, struct notifier_block *nb,
343 struct netlink_ext_ack *extack)
Yotam Gigi4d65b942017-09-27 08:23:13 +0200344{
345 return 0;
346}
347
348static unsigned int ipmr_rules_seq_read(struct net *net)
349{
350 return 0;
351}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200352
353bool ipmr_rule_default(const struct fib_rule *rule)
354{
355 return true;
356}
357EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000358#endif
359
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100360static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
361 const void *ptr)
362{
363 const struct mfc_cache_cmp_arg *cmparg = arg->key;
364 struct mfc_cache *c = (struct mfc_cache *)ptr;
365
366 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
367 cmparg->mfc_origin != c->mfc_origin;
368}
369
370static const struct rhashtable_params ipmr_rht_params = {
Yuval Mintz494fff52018-02-28 23:29:34 +0200371 .head_offset = offsetof(struct mr_mfc, mnode),
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100372 .key_offset = offsetof(struct mfc_cache, cmparg),
373 .key_len = sizeof(struct mfc_cache_cmp_arg),
374 .nelem_hint = 3,
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100375 .obj_cmpfn = ipmr_hash_cmp,
376 .automatic_shrinking = true,
377};
378
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200379static void ipmr_new_table_set(struct mr_table *mrt,
380 struct net *net)
381{
382#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
383 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
384#endif
385}
386
Yuval Mintz845c9a72018-02-28 23:29:35 +0200387static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
388 .mfc_mcastgrp = htonl(INADDR_ANY),
389 .mfc_origin = htonl(INADDR_ANY),
390};
391
392static struct mr_table_ops ipmr_mr_table_ops = {
393 .rht_params = &ipmr_rht_params,
394 .cmparg_any = &ipmr_mr_table_ops_cmparg_any,
395};
396
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000397static struct mr_table *ipmr_new_table(struct net *net, u32 id)
398{
399 struct mr_table *mrt;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000400
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100401 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
402 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
403 return ERR_PTR(-EINVAL);
404
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000405 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100406 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000407 return mrt;
408
Yuval Mintz845c9a72018-02-28 23:29:35 +0200409 return mr_table_alloc(net, id, &ipmr_mr_table_ops,
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200410 ipmr_expire_process, ipmr_new_table_set);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000411}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000413static void ipmr_free_table(struct mr_table *mrt)
414{
415 del_timer_sync(&mrt->ipmr_expire_timer);
Callum Sinclairca8d4792019-02-18 10:07:52 +1300416 mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC |
417 MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100418 rhltable_destroy(&mrt->mfc_hash);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000419 kfree(mrt);
420}
421
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
423
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100424/* Initialize ipmr pimreg/tunnel in_device */
425static bool ipmr_init_vif_indev(const struct net_device *dev)
426{
427 struct in_device *in_dev;
428
429 ASSERT_RTNL();
430
431 in_dev = __in_dev_get_rtnl(dev);
432 if (!in_dev)
433 return false;
434 ipv4_devconf_setall(in_dev);
435 neigh_parms_data_state_setall(in_dev->arp_parms);
436 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
437
438 return true;
439}
440
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100441static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200443 struct net_device *tunnel_dev, *new_dev;
444 struct ip_tunnel_parm p = { };
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200445 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200447 tunnel_dev = __dev_get_by_name(net, "tunl0");
448 if (!tunnel_dev)
449 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200451 p.iph.daddr = v->vifc_rmt_addr.s_addr;
452 p.iph.saddr = v->vifc_lcl_addr.s_addr;
453 p.iph.version = 4;
454 p.iph.ihl = 5;
455 p.iph.protocol = IPPROTO_IPIP;
456 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Christoph Hellwigc7e367052020-05-19 15:03:14 +0200458 if (!tunnel_dev->netdev_ops->ndo_tunnel_ctl)
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200459 goto out;
Christoph Hellwigc7e367052020-05-19 15:03:14 +0200460 err = tunnel_dev->netdev_ops->ndo_tunnel_ctl(tunnel_dev, &p,
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200461 SIOCADDTUNNEL);
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200462 if (err)
463 goto out;
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800464
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200465 new_dev = __dev_get_by_name(net, p.name);
466 if (!new_dev)
467 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200469 new_dev->flags |= IFF_MULTICAST;
470 if (!ipmr_init_vif_indev(new_dev))
471 goto out_unregister;
472 if (dev_open(new_dev, NULL))
473 goto out_unregister;
474 dev_hold(new_dev);
Christoph Hellwigc1fd11822020-05-19 15:03:12 +0200475 err = dev_set_allmulti(new_dev, 1);
476 if (err) {
477 dev_close(new_dev);
Christoph Hellwigc7e367052020-05-19 15:03:14 +0200478 tunnel_dev->netdev_ops->ndo_tunnel_ctl(tunnel_dev, &p,
Christoph Hellwigc1fd11822020-05-19 15:03:12 +0200479 SIOCDELTUNNEL);
Christoph Hellwigc1fd11822020-05-19 15:03:12 +0200480 dev_put(new_dev);
481 new_dev = ERR_PTR(err);
482 }
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200483 return new_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484
Christoph Hellwigc384b8a2020-05-19 15:03:11 +0200485out_unregister:
486 unregister_netdevice(new_dev);
487out:
Christoph Hellwigc1fd11822020-05-19 15:03:12 +0200488 return ERR_PTR(-ENOBUFS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489}
490
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100491#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000492static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000494 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000495 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500496 struct flowi4 fl4 = {
497 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700498 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500499 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000500 };
501 int err;
502
David S. Millerda919812011-03-12 02:04:50 -0500503 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000504 if (err < 0) {
505 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000506 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000507 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000508
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700510 dev->stats.tx_bytes += skb->len;
511 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000512 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 read_unlock(&mrt_lock);
514 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000515 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516}
517
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200518static int reg_vif_get_iflink(const struct net_device *dev)
519{
520 return 0;
521}
522
Stephen Hemminger007c3832008-11-20 20:28:35 -0800523static const struct net_device_ops reg_vif_netdev_ops = {
524 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200525 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800526};
527
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528static void reg_vif_setup(struct net_device *dev)
529{
530 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800531 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530533 dev->netdev_ops = &reg_vif_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400534 dev->needs_free_netdev = true;
Tom Goff403dbb92009-06-14 03:16:13 -0700535 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000538static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
540 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000541 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000543 if (mrt->id == RT_TABLE_DEFAULT)
544 sprintf(name, "pimreg");
545 else
546 sprintf(name, "pimreg%u", mrt->id);
547
Tom Gundersenc835a672014-07-14 16:37:24 +0200548 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Ian Morris51456b22015-04-03 09:17:26 +0100550 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return NULL;
552
Tom Goff403dbb92009-06-14 03:16:13 -0700553 dev_net_set(dev, net);
554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (register_netdevice(dev)) {
556 free_netdev(dev);
557 return NULL;
558 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100560 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 goto failure;
Petr Machata00f54e62018-12-06 17:05:36 +0000562 if (dev_open(dev, NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 goto failure;
564
Wang Chen7dc00c82008-07-14 20:56:34 -0700565 dev_hold(dev);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 return dev;
568
569failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 unregister_netdevice(dev);
571 return NULL;
572}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100573
574/* called with rcu_read_lock() */
575static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
576 unsigned int pimlen)
577{
578 struct net_device *reg_dev = NULL;
579 struct iphdr *encap;
580
581 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100582 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100583 * a. packet is really sent to a multicast group
584 * b. packet is not a NULL-REGISTER
585 * c. packet is not truncated
586 */
587 if (!ipv4_is_multicast(encap->daddr) ||
588 encap->tot_len == 0 ||
589 ntohs(encap->tot_len) + pimlen > skb->len)
590 return 1;
591
592 read_lock(&mrt_lock);
593 if (mrt->mroute_reg_vif_num >= 0)
594 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
595 read_unlock(&mrt_lock);
596
597 if (!reg_dev)
598 return 1;
599
600 skb->mac_header = skb->network_header;
601 skb_pull(skb, (u8 *)encap - skb->data);
602 skb_reset_network_header(skb);
603 skb->protocol = htons(ETH_P_IP);
604 skb->ip_summed = CHECKSUM_NONE;
605
606 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
607
608 netif_rx(skb);
609
610 return NET_RX_SUCCESS;
611}
612#else
613static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
614{
615 return NULL;
616}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617#endif
618
Yotam Gigib3620532017-09-27 08:23:14 +0200619static int call_ipmr_vif_entry_notifiers(struct net *net,
620 enum fib_event_type event_type,
621 struct vif_device *vif,
622 vifi_t vif_index, u32 tb_id)
623{
Yuval Mintzbc67a0d2018-03-26 15:01:31 +0300624 return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
625 vif, vif_index, tb_id,
626 &net->ipv4.ipmr_seq);
Yotam Gigib3620532017-09-27 08:23:14 +0200627}
628
Yotam Gigib3620532017-09-27 08:23:14 +0200629static int call_ipmr_mfc_entry_notifiers(struct net *net,
630 enum fib_event_type event_type,
631 struct mfc_cache *mfc, u32 tb_id)
632{
Yuval Mintz54c4cad2018-03-26 15:01:32 +0300633 return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
634 &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
Yotam Gigib3620532017-09-27 08:23:14 +0200635}
636
Ben Hutchings2c530402012-07-10 10:55:09 +0000637/**
638 * vif_delete - Delete a VIF entry
Andrew Lunn3628e3c2020-07-13 01:15:02 +0200639 * @mrt: Table to delete from
640 * @vifi: VIF identifier to delete
Wang Chen7dc00c82008-07-14 20:56:34 -0700641 * @notify: Set to 1, if the caller is a notifier_call
Andrew Lunn3628e3c2020-07-13 01:15:02 +0200642 * @head: if unregistering the VIF, place it on this queue
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000644static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000645 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646{
Yotam Gigib3620532017-09-27 08:23:14 +0200647 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 struct vif_device *v;
649 struct net_device *dev;
650 struct in_device *in_dev;
651
Patrick McHardy0c122952010-04-13 05:03:22 +0000652 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return -EADDRNOTAVAIL;
654
Patrick McHardy0c122952010-04-13 05:03:22 +0000655 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Yotam Gigib3620532017-09-27 08:23:14 +0200657 if (VIF_EXISTS(mrt, vifi))
658 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
659 mrt->id);
660
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 write_lock_bh(&mrt_lock);
662 dev = v->dev;
663 v->dev = NULL;
664
665 if (!dev) {
666 write_unlock_bh(&mrt_lock);
667 return -EADDRNOTAVAIL;
668 }
669
Patrick McHardy0c122952010-04-13 05:03:22 +0000670 if (vifi == mrt->mroute_reg_vif_num)
671 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000673 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000675
676 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000677 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 break;
679 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000680 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 }
682
683 write_unlock_bh(&mrt_lock);
684
685 dev_set_allmulti(dev, -1);
686
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000687 in_dev = __in_dev_get_rtnl(dev);
688 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700689 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -0700690 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000691 NETCONFA_MC_FORWARDING,
692 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 ip_rt_multicast_event(in_dev);
694 }
695
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000696 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000697 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
Eric Dumazet42120a82021-12-04 20:22:16 -0800699 dev_put_track(dev, &v->dev_tracker);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 return 0;
701}
702
Eric Dumazeta8c94862010-10-01 16:15:08 +0000703static void ipmr_cache_free_rcu(struct rcu_head *head)
704{
Yuval Mintz494fff52018-02-28 23:29:34 +0200705 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000706
Yuval Mintz494fff52018-02-28 23:29:34 +0200707 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000708}
709
Yuval Mintz8c13af22018-03-26 15:01:36 +0300710static void ipmr_cache_free(struct mfc_cache *c)
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000711{
Yuval Mintz494fff52018-02-28 23:29:34 +0200712 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000713}
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000716 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000718static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200720 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700722 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Patrick McHardy0c122952010-04-13 05:03:22 +0000724 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Yuval Mintz494fff52018-02-28 23:29:34 +0200726 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700727 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200728 struct nlmsghdr *nlh = skb_pull(skb,
729 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000731 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000733 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700734 e->error = -ETIMEDOUT;
735 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700736
Eric W. Biederman15e47302012-09-07 20:12:54 +0000737 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000738 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000743 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744}
745
Patrick McHardye258beb2010-04-13 05:03:19 +0000746/* Timer process for the unresolved queue. */
Kees Cooke99e88a2017-10-16 14:43:17 -0700747static void ipmr_expire_process(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Kees Cooke99e88a2017-10-16 14:43:17 -0700749 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
Yuval Mintz494fff52018-02-28 23:29:34 +0200750 struct mr_mfc *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 unsigned long expires;
Yuval Mintz494fff52018-02-28 23:29:34 +0200752 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753
754 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000755 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 return;
757 }
758
Patrick McHardy0c122952010-04-13 05:03:22 +0000759 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 goto out;
761
762 now = jiffies;
763 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
Patrick McHardy0c122952010-04-13 05:03:22 +0000765 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (time_after(c->mfc_un.unres.expires, now)) {
767 unsigned long interval = c->mfc_un.unres.expires - now;
768 if (interval < expires)
769 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 continue;
771 }
772
Patrick McHardy862465f2010-04-13 05:03:21 +0000773 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +0200774 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
775 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 }
777
Patrick McHardy0c122952010-04-13 05:03:22 +0000778 if (!list_empty(&mrt->mfc_unres_queue))
779 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781out:
782 spin_unlock(&mfc_unres_lock);
783}
784
785/* Fill oifs list. It is called under write locked mrt_lock. */
Yuval Mintz494fff52018-02-28 23:29:34 +0200786static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000787 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 int vifi;
790
791 cache->mfc_un.res.minvif = MAXVIFS;
792 cache->mfc_un.res.maxvif = 0;
793 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
794
Patrick McHardy0c122952010-04-13 05:03:22 +0000795 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
796 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000797 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
799 if (cache->mfc_un.res.minvif > vifi)
800 cache->mfc_un.res.minvif = vifi;
801 if (cache->mfc_un.res.maxvif <= vifi)
802 cache->mfc_un.res.maxvif = vifi + 1;
803 }
804 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200805 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Patrick McHardy0c122952010-04-13 05:03:22 +0000808static int vif_add(struct net *net, struct mr_table *mrt,
809 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Florian Fainellibccb3022019-02-06 09:45:46 -0800811 struct netdev_phys_item_id ppid = { };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000813 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 struct net_device *dev;
815 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700816 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000819 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return -EADDRINUSE;
821
822 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100824 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100825 return -EINVAL;
826 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 * All the packets will be sent to the daemon
828 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000829 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000831 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (!dev)
833 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700834 err = dev_set_allmulti(dev, 1);
835 if (err) {
836 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700837 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700838 return err;
839 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900841 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000842 dev = ipmr_new_tunnel(net, vifc);
Christoph Hellwigc1fd11822020-05-19 15:03:12 +0200843 if (IS_ERR(dev))
844 return PTR_ERR(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000846 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000848 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
849 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100850 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000851 dev_put(dev);
852 return -EADDRNOTAVAIL;
853 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000854 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000855 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000856 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (!dev)
858 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700859 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700860 if (err) {
861 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700862 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 break;
865 default:
866 return -EINVAL;
867 }
868
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000869 in_dev = __in_dev_get_rtnl(dev);
870 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000871 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000873 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700874 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700875 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
876 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 ip_rt_multicast_event(in_dev);
878
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000879 /* Fill in the VIF structures */
Yuval Mintz6853f212018-02-28 23:29:29 +0200880 vif_device_init(v, dev, vifc->vifc_rate_limit,
881 vifc->vifc_threshold,
882 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
883 (VIFF_TUNNEL | VIFF_REGISTER));
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000884
Florian Fainellibccb3022019-02-06 09:45:46 -0800885 err = dev_get_port_parent_id(dev, &ppid, true);
886 if (err == 0) {
887 memcpy(v->dev_parent_id.id, ppid.id, ppid.id_len);
888 v->dev_parent_id.id_len = ppid.id_len;
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200889 } else {
890 v->dev_parent_id.id_len = 0;
891 }
Yuval Mintz6853f212018-02-28 23:29:29 +0200892
Jianjun Kongc354e122008-11-03 00:28:02 -0800893 v->local = vifc->vifc_lcl_addr.s_addr;
894 v->remote = vifc->vifc_rmt_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
896 /* And finish update writing critical data */
897 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800898 v->dev = dev;
Eric Dumazet42120a82021-12-04 20:22:16 -0800899 netdev_tracker_alloc(dev, &v->dev_tracker, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000900 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000901 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000902 if (vifi+1 > mrt->maxvif)
903 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +0200905 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return 0;
907}
908
Eric Dumazeta8c94862010-10-01 16:15:08 +0000909/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000910static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000911 __be32 origin,
912 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100914 struct mfc_cache_cmp_arg arg = {
915 .mfc_mcastgrp = mcastgrp,
916 .mfc_origin = origin
917 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Yuval Mintz845c9a72018-02-28 23:29:35 +0200919 return mr_mfc_find(mrt, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000920}
921
922/* Look for a (*,G) entry */
923static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
924 __be32 mcastgrp, int vifi)
925{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100926 struct mfc_cache_cmp_arg arg = {
927 .mfc_mcastgrp = mcastgrp,
928 .mfc_origin = htonl(INADDR_ANY)
929 };
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000930
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100931 if (mcastgrp == htonl(INADDR_ANY))
Yuval Mintz845c9a72018-02-28 23:29:35 +0200932 return mr_mfc_find_any_parent(mrt, vifi);
933 return mr_mfc_find_any(mrt, vifi, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000934}
935
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100936/* Look for a (S,G,iif) entry if parent != -1 */
937static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
938 __be32 origin, __be32 mcastgrp,
939 int parent)
940{
941 struct mfc_cache_cmp_arg arg = {
942 .mfc_mcastgrp = mcastgrp,
943 .mfc_origin = origin,
944 };
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100945
Yuval Mintz845c9a72018-02-28 23:29:35 +0200946 return mr_mfc_find_parent(mrt, &arg, parent);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100947}
948
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100949/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000950static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951{
Jianjun Kongc354e122008-11-03 00:28:02 -0800952 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000953
Tom Goff70a0dec2016-06-23 16:11:57 -0400954 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +0200955 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
956 c->_c.mfc_un.res.minvif = MAXVIFS;
Yuval Mintz8c13af22018-03-26 15:01:36 +0300957 c->_c.free = ipmr_cache_free_rcu;
Yuval Mintz494fff52018-02-28 23:29:34 +0200958 refcount_set(&c->_c.mfc_un.res.refcount, 1);
Tom Goff70a0dec2016-06-23 16:11:57 -0400959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 return c;
961}
962
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000963static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Jianjun Kongc354e122008-11-03 00:28:02 -0800965 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000966
967 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +0200968 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
969 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
Eric Dumazeta8c94862010-10-01 16:15:08 +0000970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 return c;
972}
973
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100974/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +0000975static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
976 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977{
978 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700979 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000981 /* Play the pending entries through our router */
Yuval Mintz494fff52018-02-28 23:29:34 +0200982 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700983 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200984 struct nlmsghdr *nlh = skb_pull(skb,
985 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Yuval Mintz7b0db852018-02-28 23:29:39 +0200987 if (mr_fill_mroute(mrt, skb, &c->_c,
988 nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000989 nlh->nlmsg_len = skb_tail_pointer(skb) -
990 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 } else {
992 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000993 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000995 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700996 e->error = -EMSGSIZE;
997 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 }
Thomas Graf2942e902006-08-15 00:30:25 -0700999
Eric W. Biederman15e47302012-09-07 20:12:54 +00001000 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001001 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001002 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 }
1005}
1006
Julien Gomes5a645dd2017-06-20 13:54:17 -07001007/* Bounce a cache query up to mrouted and netlink.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001009 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001011static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001012 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001014 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001015 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 struct igmphdr *igmp;
1017 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001018 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 int ret;
1020
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001021 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1023 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 skb = alloc_skb(128, GFP_ATOMIC);
1025
Stephen Hemminger132adf52007-03-08 20:44:43 -08001026 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return -ENOBUFS;
1028
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001029 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001031 * Duplicate old header, fix ihl, length etc.
1032 * And all this only to mangle msg->im_msgtype and
1033 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001035 skb_push(skb, sizeof(struct iphdr));
1036 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001037 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001038 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001039 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001040 msg->im_msgtype = assert;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 msg->im_mbz = 0;
Paul Daveyc8715a82020-09-08 10:04:07 +12001042 if (assert == IGMPMSG_WRVIFWHOLE) {
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001043 msg->im_vif = vifi;
Paul Daveyc8715a82020-09-08 10:04:07 +12001044 msg->im_vif_hi = vifi >> 8;
1045 } else {
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001046 msg->im_vif = mrt->mroute_reg_vif_num;
Paul Daveyc8715a82020-09-08 10:04:07 +12001047 msg->im_vif_hi = mrt->mroute_reg_vif_num >> 8;
1048 }
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001049 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1050 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1051 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001052 } else {
1053 /* Copy the IP header */
1054 skb_set_network_header(skb, skb->len);
1055 skb_put(skb, ihl);
1056 skb_copy_to_linear_data(skb, pkt->data, ihl);
1057 /* Flag to the kernel this is a route add */
1058 ip_hdr(skb)->protocol = 0;
1059 msg = (struct igmpmsg *)skb_network_header(skb);
1060 msg->im_vif = vifi;
Paul Daveyc8715a82020-09-08 10:04:07 +12001061 msg->im_vif_hi = vifi >> 8;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001062 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1063 /* Add our header */
Johannes Berg4df864c2017-06-16 14:29:21 +02001064 igmp = skb_put(skb, sizeof(struct igmphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001065 igmp->type = assert;
1066 msg->im_msgtype = assert;
1067 igmp->code = 0;
1068 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1069 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
Eric Dumazet4c9687092010-10-01 16:15:01 +00001072 rcu_read_lock();
1073 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001074 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001075 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 kfree_skb(skb);
1077 return -EINVAL;
1078 }
1079
Julien Gomes5a645dd2017-06-20 13:54:17 -07001080 igmpmsg_netlink_event(mrt, skb);
1081
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001082 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001083 ret = sock_queue_rcv_skb(mroute_sk, skb);
1084 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001085 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001086 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 kfree_skb(skb);
1088 }
1089
1090 return ret;
1091}
1092
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001093/* Queue a packet for resolution. It gets locked cache entry! */
1094static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001095 struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001097 const struct iphdr *iph = ip_hdr(skb);
1098 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001099 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001103 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001104 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001105 c->mfc_origin == iph->saddr) {
1106 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 }
1110
Patrick McHardy862465f2010-04-13 05:03:21 +00001111 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001112 /* Create a new entry if allowable */
Hangbin Liu0079ad82019-09-06 15:36:01 +08001113 c = ipmr_cache_alloc_unres();
1114 if (!c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 spin_unlock_bh(&mfc_unres_lock);
1116
1117 kfree_skb(skb);
1118 return -ENOBUFS;
1119 }
1120
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001121 /* Fill in the new cache entry */
Yuval Mintz494fff52018-02-28 23:29:34 +02001122 c->_c.mfc_parent = -1;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001123 c->mfc_origin = iph->saddr;
1124 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001126 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001127 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Yuval Mintz494fff52018-02-28 23:29:34 +02001128
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001129 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001130 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 out - Brad Parker
1132 */
1133 spin_unlock_bh(&mfc_unres_lock);
1134
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001135 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 kfree_skb(skb);
1137 return err;
1138 }
1139
Patrick McHardy0c122952010-04-13 05:03:22 +00001140 atomic_inc(&mrt->cache_resolve_queue_len);
Yuval Mintz494fff52018-02-28 23:29:34 +02001141 list_add(&c->_c.list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001142 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
David S. Miller278554b2010-05-12 00:05:35 -07001144 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
Yuval Mintz494fff52018-02-28 23:29:34 +02001145 mod_timer(&mrt->ipmr_expire_timer,
1146 c->_c.mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 }
1148
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001149 /* See if we can append the packet */
Yuval Mintz494fff52018-02-28 23:29:34 +02001150 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 kfree_skb(skb);
1152 err = -ENOBUFS;
1153 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001154 if (dev) {
1155 skb->dev = dev;
1156 skb->skb_iif = dev->ifindex;
1157 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001158 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 err = 0;
1160 }
1161
1162 spin_unlock_bh(&mfc_unres_lock);
1163 return err;
1164}
1165
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001166/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001168static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169{
Yotam Gigib3620532017-09-27 08:23:14 +02001170 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001171 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001173 /* The entries are added/deleted only under RTNL */
1174 rcu_read_lock();
1175 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1176 mfc->mfcc_mcastgrp.s_addr, parent);
1177 rcu_read_unlock();
1178 if (!c)
1179 return -ENOENT;
Yuval Mintz494fff52018-02-28 23:29:34 +02001180 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1181 list_del_rcu(&c->_c.list);
Yotam Gigib3620532017-09-27 08:23:14 +02001182 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001183 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yuval Mintz8c13af22018-03-26 15:01:36 +03001184 mr_cache_put(&c->_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001186 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Patrick McHardy0c122952010-04-13 05:03:22 +00001189static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001190 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191{
Patrick McHardy862465f2010-04-13 05:03:21 +00001192 struct mfc_cache *uc, *c;
Yuval Mintz494fff52018-02-28 23:29:34 +02001193 struct mr_mfc *_uc;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001194 bool found;
1195 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196
Patrick McHardya50436f22010-03-17 06:04:14 +00001197 if (mfc->mfcc_parent >= MAXVIFS)
1198 return -ENFILE;
1199
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001200 /* The entries are added/deleted only under RTNL */
1201 rcu_read_lock();
1202 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1203 mfc->mfcc_mcastgrp.s_addr, parent);
1204 rcu_read_unlock();
1205 if (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 write_lock_bh(&mrt_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001207 c->_c.mfc_parent = mfc->mfcc_parent;
1208 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001210 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +02001212 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1213 mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001214 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 return 0;
1216 }
1217
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001218 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001219 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 return -EINVAL;
1221
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001222 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001223 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 return -ENOMEM;
1225
Jianjun Kongc354e122008-11-03 00:28:02 -08001226 c->mfc_origin = mfc->mfcc_origin.s_addr;
1227 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
Yuval Mintz494fff52018-02-28 23:29:34 +02001228 c->_c.mfc_parent = mfc->mfcc_parent;
1229 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001231 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Yuval Mintz494fff52018-02-28 23:29:34 +02001233 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001234 ipmr_rht_params);
1235 if (ret) {
1236 pr_err("ipmr: rhtable insert error %d\n", ret);
1237 ipmr_cache_free(c);
1238 return ret;
1239 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001240 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001241 /* Check to see if we resolved a queued list. If so we
1242 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001244 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001246 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1247 uc = (struct mfc_cache *)_uc;
Patrick McHardye258beb2010-04-13 05:03:19 +00001248 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001250 list_del(&_uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001251 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001252 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 break;
1254 }
1255 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001256 if (list_empty(&mrt->mfc_unres_queue))
1257 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 spin_unlock_bh(&mfc_unres_lock);
1259
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001260 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001261 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001262 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 }
Yotam Gigib3620532017-09-27 08:23:14 +02001264 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001265 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 return 0;
1267}
1268
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001269/* Close the multicast socket, and clear the vif tables etc */
Callum Sinclairca8d4792019-02-18 10:07:52 +13001270static void mroute_clean_tables(struct mr_table *mrt, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Yotam Gigib3620532017-09-27 08:23:14 +02001272 struct net *net = read_pnet(&mrt->net);
Yuval Mintz494fff52018-02-28 23:29:34 +02001273 struct mr_mfc *c, *tmp;
1274 struct mfc_cache *cache;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001275 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001276 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001277
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001278 /* Shut down all active vif entries */
Callum Sinclairca8d4792019-02-18 10:07:52 +13001279 if (flags & (MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC)) {
1280 for (i = 0; i < mrt->maxvif; i++) {
1281 if (((mrt->vif_table[i].flags & VIFF_STATIC) &&
1282 !(flags & MRT_FLUSH_VIFS_STATIC)) ||
1283 (!(mrt->vif_table[i].flags & VIFF_STATIC) && !(flags & MRT_FLUSH_VIFS)))
1284 continue;
1285 vif_delete(mrt, i, 0, &list);
1286 }
1287 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 }
1289
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001290 /* Wipe the cache */
Callum Sinclairca8d4792019-02-18 10:07:52 +13001291 if (flags & (MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC)) {
1292 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1293 if (((c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC_STATIC)) ||
1294 (!(c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC)))
1295 continue;
1296 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1297 list_del_rcu(&c->list);
1298 cache = (struct mfc_cache *)c;
1299 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
1300 mrt->id);
1301 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1302 mr_cache_put(c);
1303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 }
1305
Callum Sinclairca8d4792019-02-18 10:07:52 +13001306 if (flags & MRT_FLUSH_MFC) {
1307 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1308 spin_lock_bh(&mfc_unres_lock);
1309 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1310 list_del(&c->list);
1311 cache = (struct mfc_cache *)c;
1312 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1313 ipmr_destroy_unres(mrt, cache);
1314 }
1315 spin_unlock_bh(&mfc_unres_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 }
1318}
1319
Eric Dumazet4c9687092010-10-01 16:15:01 +00001320/* called from ip_ra_control(), before an RCU grace period,
Zheng Yongjun974d8f82021-06-07 23:01:09 +08001321 * we don't need to call synchronize_rcu() here
Eric Dumazet4c9687092010-10-01 16:15:01 +00001322 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323static void mrtsock_destruct(struct sock *sk)
1324{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001325 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001326 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001327
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001328 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001329 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001330 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001331 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001332 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1333 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001334 NETCONFA_IFINDEX_ALL,
1335 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001336 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Callum Sinclairca8d4792019-02-18 10:07:52 +13001337 mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_MFC);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001338 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 }
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001340 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341}
1342
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001343/* Socket options and virtual interface manipulation. The whole
1344 * virtual interface system is a complete heap, but unfortunately
1345 * that's how BSD mrouted happens to think. Maybe one day with a proper
1346 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001348
Christoph Hellwig01ccb5b2020-07-23 08:08:56 +02001349int ip_mroute_setsockopt(struct sock *sk, int optname, sockptr_t optval,
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001350 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001352 struct net *net = sock_net(sk);
1353 int val, ret = 0, parent = 0;
1354 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 struct vifctl vif;
1356 struct mfcctl mfc;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001357 bool do_wrvifwhole;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001358 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001359
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001360 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1361 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001362 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001363 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1364 ret = -EOPNOTSUPP;
1365 goto out_unlock;
1366 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001367
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001368 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001369 if (!mrt) {
1370 ret = -ENOENT;
1371 goto out_unlock;
1372 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001373 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001374 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001375 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1376 ret = -EACCES;
1377 goto out_unlock;
1378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 }
1380
Stephen Hemminger132adf52007-03-08 20:44:43 -08001381 switch (optname) {
1382 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001383 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001384 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001385 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001386 }
1387 if (rtnl_dereference(mrt->mroute_sk)) {
1388 ret = -EADDRINUSE;
1389 break;
1390 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001391
1392 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1393 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001394 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001395 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001396 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1397 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001398 NETCONFA_IFINDEX_ALL,
1399 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001400 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001401 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001402 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001403 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1404 ret = -EACCES;
1405 } else {
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001406 /* We need to unlock here because mrtsock_destruct takes
1407 * care of rtnl itself and we can't change that due to
1408 * the IP_ROUTER_ALERT setsockopt which runs without it.
1409 */
1410 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001411 ret = ip_ra_control(sk, 0, NULL);
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001412 goto out;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001413 }
1414 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001415 case MRT_ADD_VIF:
1416 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001417 if (optlen != sizeof(vif)) {
1418 ret = -EINVAL;
1419 break;
1420 }
Christoph Hellwig01ccb5b2020-07-23 08:08:56 +02001421 if (copy_from_sockptr(&vif, optval, sizeof(vif))) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001422 ret = -EFAULT;
1423 break;
1424 }
1425 if (vif.vifc_vifi >= MAXVIFS) {
1426 ret = -ENFILE;
1427 break;
1428 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001429 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001430 ret = vif_add(net, mrt, &vif,
1431 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001432 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001433 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001434 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001435 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001436 /* Manipulate the forwarding caches. These live
1437 * in a sort of kernel/user symbiosis.
1438 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001439 case MRT_ADD_MFC:
1440 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001441 parent = -1;
Joe Perchesa8eceea2020-03-12 15:50:22 -07001442 fallthrough;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001443 case MRT_ADD_MFC_PROXY:
1444 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001445 if (optlen != sizeof(mfc)) {
1446 ret = -EINVAL;
1447 break;
1448 }
Ido Schimmel73cb1192020-07-27 10:18:34 +03001449 if (copy_from_sockptr(&mfc, optval, sizeof(mfc))) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001450 ret = -EFAULT;
1451 break;
1452 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001453 if (parent == 0)
1454 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001455 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1456 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001457 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001458 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001459 sk == rtnl_dereference(mrt->mroute_sk),
1460 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001461 break;
Callum Sinclairca8d4792019-02-18 10:07:52 +13001462 case MRT_FLUSH:
1463 if (optlen != sizeof(val)) {
1464 ret = -EINVAL;
1465 break;
1466 }
Christoph Hellwig01ccb5b2020-07-23 08:08:56 +02001467 if (copy_from_sockptr(&val, optval, sizeof(val))) {
Callum Sinclairca8d4792019-02-18 10:07:52 +13001468 ret = -EFAULT;
1469 break;
1470 }
1471 mroute_clean_tables(mrt, val);
1472 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001473 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001474 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001475 if (optlen != sizeof(val)) {
1476 ret = -EINVAL;
1477 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478 }
Christoph Hellwig01ccb5b2020-07-23 08:08:56 +02001479 if (copy_from_sockptr(&val, optval, sizeof(val))) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001480 ret = -EFAULT;
1481 break;
1482 }
1483 mrt->mroute_do_assert = val;
1484 break;
1485 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001486 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001487 ret = -ENOPROTOOPT;
1488 break;
1489 }
1490 if (optlen != sizeof(val)) {
1491 ret = -EINVAL;
1492 break;
1493 }
Christoph Hellwig01ccb5b2020-07-23 08:08:56 +02001494 if (copy_from_sockptr(&val, optval, sizeof(val))) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001495 ret = -EFAULT;
1496 break;
1497 }
1498
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001499 do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001500 val = !!val;
1501 if (val != mrt->mroute_do_pim) {
1502 mrt->mroute_do_pim = val;
1503 mrt->mroute_do_assert = val;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001504 mrt->mroute_do_wrvifwhole = do_wrvifwhole;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001505 }
1506 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001507 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001508 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1509 ret = -ENOPROTOOPT;
1510 break;
1511 }
1512 if (optlen != sizeof(uval)) {
1513 ret = -EINVAL;
1514 break;
1515 }
Christoph Hellwig01ccb5b2020-07-23 08:08:56 +02001516 if (copy_from_sockptr(&uval, optval, sizeof(uval))) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001517 ret = -EFAULT;
1518 break;
1519 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001520
Eric Dumazet4c9687092010-10-01 16:15:01 +00001521 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1522 ret = -EBUSY;
1523 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001524 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001525 if (IS_ERR(mrt))
1526 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001527 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001528 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001529 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001530 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001531 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001532 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001533 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001535out_unlock:
1536 rtnl_unlock();
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001537out:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001538 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539}
1540
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001541/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001542int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543{
1544 int olr;
1545 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001546 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001547 struct mr_table *mrt;
1548
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001549 if (sk->sk_type != SOCK_RAW ||
1550 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1551 return -EOPNOTSUPP;
1552
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001553 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001554 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001555 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001557 switch (optname) {
1558 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001559 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001560 break;
1561 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001562 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001563 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001564 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001565 break;
1566 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001567 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001568 break;
1569 default:
1570 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001571 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001572
1573 if (get_user(olr, optlen))
1574 return -EFAULT;
1575 olr = min_t(unsigned int, olr, sizeof(int));
1576 if (olr < 0)
1577 return -EINVAL;
1578 if (put_user(olr, optlen))
1579 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001580 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581 return -EFAULT;
1582 return 0;
1583}
1584
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001585/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1587{
1588 struct sioc_sg_req sr;
1589 struct sioc_vif_req vr;
1590 struct vif_device *vif;
1591 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001592 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001593 struct mr_table *mrt;
1594
1595 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001596 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001597 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001598
Stephen Hemminger132adf52007-03-08 20:44:43 -08001599 switch (cmd) {
1600 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001601 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001602 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001603 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001604 return -EINVAL;
Gustavo A. R. Silva56484512018-12-10 12:41:24 -06001605 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001606 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001607 vif = &mrt->vif_table[vr.vifi];
1608 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001609 vr.icount = vif->pkt_in;
1610 vr.ocount = vif->pkt_out;
1611 vr.ibytes = vif->bytes_in;
1612 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001614
Jianjun Kongc354e122008-11-03 00:28:02 -08001615 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001617 return 0;
1618 }
1619 read_unlock(&mrt_lock);
1620 return -EADDRNOTAVAIL;
1621 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001622 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001623 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Eric Dumazeta8c94862010-10-01 16:15:08 +00001625 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001626 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001627 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001628 sr.pktcnt = c->_c.mfc_un.res.pkt;
1629 sr.bytecnt = c->_c.mfc_un.res.bytes;
1630 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001631 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001632
Jianjun Kongc354e122008-11-03 00:28:02 -08001633 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001634 return -EFAULT;
1635 return 0;
1636 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001637 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001638 return -EADDRNOTAVAIL;
1639 default:
1640 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 }
1642}
1643
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001644#ifdef CONFIG_COMPAT
1645struct compat_sioc_sg_req {
1646 struct in_addr src;
1647 struct in_addr grp;
1648 compat_ulong_t pktcnt;
1649 compat_ulong_t bytecnt;
1650 compat_ulong_t wrong_if;
1651};
1652
David S. Millerca6b8bb02011-02-03 17:24:28 -08001653struct compat_sioc_vif_req {
1654 vifi_t vifi; /* Which iface */
1655 compat_ulong_t icount;
1656 compat_ulong_t ocount;
1657 compat_ulong_t ibytes;
1658 compat_ulong_t obytes;
1659};
1660
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001661int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1662{
David S. Miller0033d5a2011-02-03 17:21:31 -08001663 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001664 struct compat_sioc_vif_req vr;
1665 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001666 struct mfc_cache *c;
1667 struct net *net = sock_net(sk);
1668 struct mr_table *mrt;
1669
1670 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001671 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001672 return -ENOENT;
1673
1674 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001675 case SIOCGETVIFCNT:
1676 if (copy_from_user(&vr, arg, sizeof(vr)))
1677 return -EFAULT;
1678 if (vr.vifi >= mrt->maxvif)
1679 return -EINVAL;
Gustavo A. R. Silva56484512018-12-10 12:41:24 -06001680 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
David S. Millerca6b8bb02011-02-03 17:24:28 -08001681 read_lock(&mrt_lock);
1682 vif = &mrt->vif_table[vr.vifi];
1683 if (VIF_EXISTS(mrt, vr.vifi)) {
1684 vr.icount = vif->pkt_in;
1685 vr.ocount = vif->pkt_out;
1686 vr.ibytes = vif->bytes_in;
1687 vr.obytes = vif->bytes_out;
1688 read_unlock(&mrt_lock);
1689
1690 if (copy_to_user(arg, &vr, sizeof(vr)))
1691 return -EFAULT;
1692 return 0;
1693 }
1694 read_unlock(&mrt_lock);
1695 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001696 case SIOCGETSGCNT:
1697 if (copy_from_user(&sr, arg, sizeof(sr)))
1698 return -EFAULT;
1699
1700 rcu_read_lock();
1701 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1702 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001703 sr.pktcnt = c->_c.mfc_un.res.pkt;
1704 sr.bytecnt = c->_c.mfc_un.res.bytes;
1705 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001706 rcu_read_unlock();
1707
1708 if (copy_to_user(arg, &sr, sizeof(sr)))
1709 return -EFAULT;
1710 return 0;
1711 }
1712 rcu_read_unlock();
1713 return -EADDRNOTAVAIL;
1714 default:
1715 return -ENOIOCTLCMD;
1716 }
1717}
1718#endif
1719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1721{
Jiri Pirko351638e2013-05-28 01:30:21 +00001722 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001723 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001724 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 struct vif_device *v;
1726 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 if (event != NETDEV_UNREGISTER)
1729 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001730
1731 ipmr_for_each_table(mrt, net) {
1732 v = &mrt->vif_table[0];
1733 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1734 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001735 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001736 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 }
1738 return NOTIFY_DONE;
1739}
1740
Jianjun Kongc354e122008-11-03 00:28:02 -08001741static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 .notifier_call = ipmr_device_event,
1743};
1744
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001745/* Encapsulate a packet by attaching a valid IPIP header to it.
1746 * This avoids tunnel drivers and other mess and gives us the speed so
1747 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001749static void ip_encap(struct net *net, struct sk_buff *skb,
1750 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001752 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001753 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001754
1755 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001756 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001757 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001758 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001760 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001761 iph->tos = old_iph->tos;
1762 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 iph->frag_off = 0;
1764 iph->daddr = daddr;
1765 iph->saddr = saddr;
1766 iph->protocol = IPPROTO_IPIP;
1767 iph->ihl = 5;
1768 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001769 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 ip_send_check(iph);
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Florian Westphal895b5c92019-09-29 20:54:03 +02001773 nf_reset_ct(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001776static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1777 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001779 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
David S. Miller73186df2015-11-03 13:41:45 -05001781 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1782 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784 if (unlikely(opt->optlen))
1785 ip_forward_options(skb);
1786
Eric W. Biederman13206b62015-10-07 16:48:35 -05001787 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788}
1789
Yotam Gigia5bc9292017-10-03 09:58:08 +02001790#ifdef CONFIG_NET_SWITCHDEV
1791static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1792 int in_vifi, int out_vifi)
1793{
1794 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1795 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1796
Ido Schimmel875e8932018-12-04 08:15:10 +00001797 if (!skb->offload_l3_fwd_mark)
Yotam Gigia5bc9292017-10-03 09:58:08 +02001798 return false;
1799 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1800 return false;
1801 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1802 &in_vif->dev_parent_id);
1803}
1804#else
1805static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1806 int in_vifi, int out_vifi)
1807{
1808 return false;
1809}
1810#endif
1811
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001812/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
Patrick McHardy0c122952010-04-13 05:03:22 +00001814static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
David Ahern6e0735d2018-12-17 15:34:48 -08001815 int in_vifi, struct sk_buff *skb, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001817 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001818 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 struct net_device *dev;
1820 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001821 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 int encap = 0;
1823
Ian Morris51456b22015-04-03 09:17:26 +01001824 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825 goto out_free;
1826
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 if (vif->flags & VIFF_REGISTER) {
1828 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001829 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001830 vif->dev->stats.tx_bytes += skb->len;
1831 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001832 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001833 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Yotam Gigia5bc9292017-10-03 09:58:08 +02001836 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1837 goto out_free;
1838
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001839 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001840 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001841 vif->remote, vif->local,
1842 0, 0,
1843 IPPROTO_IPIP,
1844 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001845 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 goto out_free;
1847 encap = sizeof(struct iphdr);
1848 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001849 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001850 0, 0,
1851 IPPROTO_IPIP,
1852 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001853 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 goto out_free;
1855 }
1856
Changli Gaod8d1f302010-06-10 23:31:35 -07001857 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858
Changli Gaod8d1f302010-06-10 23:31:35 -07001859 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001861 * allow to send ICMP, so that packets will disappear
1862 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 */
David S. Miller73186df2015-11-03 13:41:45 -05001864 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 ip_rt_put(rt);
1866 goto out_free;
1867 }
1868
Changli Gaod8d1f302010-06-10 23:31:35 -07001869 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001872 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 goto out_free;
1874 }
1875
1876 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001877 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Eric Dumazetadf30902009-06-02 05:19:30 +00001879 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001880 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001881 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001884 * What do we do with netfilter? -- RR
1885 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001887 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001889 vif->dev->stats.tx_packets++;
1890 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 }
1892
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001893 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001895 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 * not only before forwarding, but after forwarding on all output
1897 * interfaces. It is clear, if mrouter runs a multicasting
1898 * program, it should receive packets not depending to what interface
1899 * program is joined.
1900 * If we will not make it, the program will have to join on all
1901 * interfaces. On the other hand, multihoming host (or router, but
1902 * not mrouter) cannot join to more than one interface - it will
1903 * result in receiving multiple packets.
1904 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001905 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1906 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 ipmr_forward_finish);
1908 return;
1909
1910out_free:
1911 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912}
1913
Patrick McHardy0c122952010-04-13 05:03:22 +00001914static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915{
1916 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001917
1918 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1919 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 break;
1921 }
1922 return ct;
1923}
1924
1925/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001926static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001927 struct net_device *dev, struct sk_buff *skb,
Yuval Mintz494fff52018-02-28 23:29:34 +02001928 struct mfc_cache *c, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929{
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001930 int true_vifi = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 int psend = -1;
1932 int vif, ct;
1933
Yuval Mintz494fff52018-02-28 23:29:34 +02001934 vif = c->_c.mfc_parent;
1935 c->_c.mfc_un.res.pkt++;
1936 c->_c.mfc_un.res.bytes += skb->len;
1937 c->_c.mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
Yuval Mintz494fff52018-02-28 23:29:34 +02001939 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001940 struct mfc_cache *cache_proxy;
1941
Zheng Yongjun974d8f82021-06-07 23:01:09 +08001942 /* For an (*,G) entry, we only check that the incoming
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001943 * interface is part of the static tree.
1944 */
Yuval Mintz845c9a72018-02-28 23:29:35 +02001945 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001946 if (cache_proxy &&
Yuval Mintz494fff52018-02-28 23:29:34 +02001947 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001948 goto forward;
1949 }
1950
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001951 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001952 if (mrt->vif_table[vif].dev != dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001953 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001955 * Very complicated situation...
1956 *
1957 * The best workaround until routing daemons will be
1958 * fixed is not to redistribute packet, if it was
1959 * send through wrong interface. It means, that
1960 * multicast applications WILL NOT work for
1961 * (S,G), which have default multicast route pointing
1962 * to wrong oif. In any case, it is not a good
1963 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 */
1965 goto dont_forward;
1966 }
1967
Yuval Mintz494fff52018-02-28 23:29:34 +02001968 c->_c.mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
Patrick McHardy0c122952010-04-13 05:03:22 +00001970 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001972 * so that we cannot check that packet arrived on an oif.
1973 * It is bad, but otherwise we would need to move pretty
1974 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001976 (mrt->mroute_do_pim ||
Yuval Mintz494fff52018-02-28 23:29:34 +02001977 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001978 time_after(jiffies,
Yuval Mintz494fff52018-02-28 23:29:34 +02001979 c->_c.mfc_un.res.last_assert +
1980 MFC_ASSERT_THRESH)) {
1981 c->_c.mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001982 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001983 if (mrt->mroute_do_wrvifwhole)
1984 ipmr_cache_report(mrt, skb, true_vifi,
1985 IGMPMSG_WRVIFWHOLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 }
1987 goto dont_forward;
1988 }
1989
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001990forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001991 mrt->vif_table[vif].pkt_in++;
1992 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001994 /* Forward the frame */
Yuval Mintz494fff52018-02-28 23:29:34 +02001995 if (c->mfc_origin == htonl(INADDR_ANY) &&
1996 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001997 if (true_vifi >= 0 &&
Yuval Mintz494fff52018-02-28 23:29:34 +02001998 true_vifi != c->_c.mfc_parent &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001999 ip_hdr(skb)->ttl >
Yuval Mintz494fff52018-02-28 23:29:34 +02002000 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002001 /* It's an (*,*) entry and the packet is not coming from
2002 * the upstream: forward the packet to the upstream
2003 * only.
2004 */
Yuval Mintz494fff52018-02-28 23:29:34 +02002005 psend = c->_c.mfc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002006 goto last_forward;
2007 }
2008 goto dont_forward;
2009 }
Yuval Mintz494fff52018-02-28 23:29:34 +02002010 for (ct = c->_c.mfc_un.res.maxvif - 1;
2011 ct >= c->_c.mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002012 /* For (*,G) entry, don't forward to the incoming interface */
Yuval Mintz494fff52018-02-28 23:29:34 +02002013 if ((c->mfc_origin != htonl(INADDR_ANY) ||
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002014 ct != true_vifi) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002015 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 if (psend != -1) {
2017 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002020 ipmr_queue_xmit(net, mrt, true_vifi,
David Ahern6e0735d2018-12-17 15:34:48 -08002021 skb2, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002023 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024 }
2025 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002026last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (psend != -1) {
2028 if (local) {
2029 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002032 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
David Ahern6e0735d2018-12-17 15:34:48 -08002033 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 } else {
David Ahern6e0735d2018-12-17 15:34:48 -08002035 ipmr_queue_xmit(net, mrt, true_vifi, skb, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03002036 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 }
2038 }
2039
2040dont_forward:
2041 if (!local)
2042 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043}
2044
David S. Miller417da662011-05-03 19:42:43 -07002045static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08002046{
David S. Miller417da662011-05-03 19:42:43 -07002047 struct rtable *rt = skb_rtable(skb);
2048 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05002049 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07002050 .daddr = iph->daddr,
2051 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00002052 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07002053 .flowi4_oif = (rt_is_output_route(rt) ?
2054 skb->dev->ifindex : 0),
2055 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002056 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07002057 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00002058 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08002059 };
2060 struct mr_table *mrt;
2061 int err;
2062
David S. Millerda919812011-03-12 02:04:50 -05002063 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002064 if (err)
2065 return ERR_PTR(err);
2066 return mrt;
2067}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002069/* Multicast packets for forwarding arrive here
2070 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072int ip_mr_input(struct sk_buff *skb)
2073{
2074 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002075 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00002076 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002077 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002078 struct net_device *dev;
2079
2080 /* skb->dev passed in is the loX master dev for vrfs.
2081 * As there are no vifs associated with loopback devices,
2082 * get the proper interface that does have a vif associated with it.
2083 */
2084 dev = skb->dev;
2085 if (netif_is_l3_master(skb->dev)) {
2086 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2087 if (!dev) {
2088 kfree_skb(skb);
2089 return -ENODEV;
2090 }
2091 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002094 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002095 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00002096 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 goto dont_forward;
2098
David S. Miller417da662011-05-03 19:42:43 -07002099 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002100 if (IS_ERR(mrt)) {
2101 kfree_skb(skb);
2102 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00002105 if (IPCB(skb)->opt.router_alert) {
2106 if (ip_call_ra_chain(skb))
2107 return 0;
2108 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2109 /* IGMPv1 (and broken IGMPv2 implementations sort of
2110 * Cisco IOS <= 11.2(8)) do not put router alert
2111 * option to IGMP packets destined to routable
2112 * groups. It is very bad, because it means
2113 * that we can forward NO IGMP messages.
2114 */
2115 struct sock *mroute_sk;
2116
2117 mroute_sk = rcu_dereference(mrt->mroute_sk);
2118 if (mroute_sk) {
Florian Westphal895b5c92019-09-29 20:54:03 +02002119 nf_reset_ct(skb);
Eric Dumazet4c9687092010-10-01 16:15:01 +00002120 raw_rcv(mroute_sk, skb);
2121 return 0;
2122 }
Roy, UjjaL92c4bed2021-07-07 23:48:33 +05302123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124 }
2125
Eric Dumazeta8c94862010-10-01 16:15:08 +00002126 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002127 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002128 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002129 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002130
2131 if (vif >= 0)
2132 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2133 vif);
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002136 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002137 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138 int vif;
2139
2140 if (local) {
2141 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2142 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002143 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 skb = skb2;
2146 }
2147
Eric Dumazeta8c94862010-10-01 16:15:08 +00002148 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002149 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 if (vif >= 0) {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002151 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152 read_unlock(&mrt_lock);
2153
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002154 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 }
2156 read_unlock(&mrt_lock);
2157 kfree_skb(skb);
2158 return -ENODEV;
2159 }
2160
Eric Dumazeta8c94862010-10-01 16:15:08 +00002161 read_lock(&mrt_lock);
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002162 ip_mr_forward(net, mrt, dev, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 read_unlock(&mrt_lock);
2164
2165 if (local)
2166 return ip_local_deliver(skb);
2167
2168 return 0;
2169
2170dont_forward:
2171 if (local)
2172 return ip_local_deliver(skb);
2173 kfree_skb(skb);
2174 return 0;
2175}
2176
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002177#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002178/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002179int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002180{
2181 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002182 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002183 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002184
2185 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2186 goto drop;
2187
2188 pim = igmp_hdr(skb);
2189
David S. Miller417da662011-05-03 19:42:43 -07002190 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002191 if (IS_ERR(mrt))
2192 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002193 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002194 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2195 goto drop;
2196
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002197 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002198drop:
2199 kfree_skb(skb);
2200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return 0;
2202}
2203#endif
2204
2205#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002206static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
2208 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002209 struct net *net = dev_net(skb->dev);
2210 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002212 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 goto drop;
2214
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002215 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002216 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002217 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002218 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002219 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 goto drop;
2221
David S. Miller417da662011-05-03 19:42:43 -07002222 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002223 if (IS_ERR(mrt))
2224 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002225 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002226drop:
2227 kfree_skb(skb);
2228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return 0;
2230}
2231#endif
2232
David S. Miller9a1b9492011-05-04 12:18:54 -07002233int ipmr_get_route(struct net *net, struct sk_buff *skb,
2234 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002235 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002238 struct mr_table *mrt;
2239 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002241 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002242 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002243 return -ENOENT;
2244
Eric Dumazeta8c94862010-10-01 16:15:08 +00002245 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002246 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002247 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002248 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002250 if (vif >= 0)
2251 cache = ipmr_cache_find_any(mrt, daddr, vif);
2252 }
Ian Morris51456b22015-04-03 09:17:26 +01002253 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002254 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002255 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002256 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002257 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002260 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002261 if (dev)
2262 vif = ipmr_find_vif(mrt, dev);
2263 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002265 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return -ENODEV;
2267 }
Guillaume Nault7901cd92019-11-15 18:29:52 +01002268
2269 skb2 = skb_realloc_headroom(skb, sizeof(struct iphdr));
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002270 if (!skb2) {
2271 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002272 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002273 return -ENOMEM;
2274 }
2275
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002276 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002277 skb_push(skb2, sizeof(struct iphdr));
2278 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002279 iph = ip_hdr(skb2);
2280 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002281 iph->saddr = saddr;
2282 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002283 iph->version = 0;
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002284 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002286 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 return err;
2288 }
2289
Eric Dumazeta8c94862010-10-01 16:15:08 +00002290 read_lock(&mrt_lock);
Yuval Mintz7b0db852018-02-28 23:29:39 +02002291 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002293 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return err;
2295}
2296
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002297static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002298 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2299 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002300{
2301 struct nlmsghdr *nlh;
2302 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002303 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002304
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002305 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002306 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002307 return -EMSGSIZE;
2308
2309 rtm = nlmsg_data(nlh);
2310 rtm->rtm_family = RTNL_FAMILY_IPMR;
2311 rtm->rtm_dst_len = 32;
2312 rtm->rtm_src_len = 32;
2313 rtm->rtm_tos = 0;
2314 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002315 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2316 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002317 rtm->rtm_type = RTN_MULTICAST;
2318 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Yuval Mintz494fff52018-02-28 23:29:34 +02002319 if (c->_c.mfc_flags & MFC_STATIC)
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002320 rtm->rtm_protocol = RTPROT_STATIC;
2321 else
2322 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002323 rtm->rtm_flags = 0;
2324
Jiri Benc930345e2015-03-29 16:59:25 +02002325 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2326 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002327 goto nla_put_failure;
Yuval Mintz7b0db852018-02-28 23:29:39 +02002328 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002329 /* do not break the dump if cache is unresolved */
2330 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002331 goto nla_put_failure;
2332
Johannes Berg053c0952015-01-16 22:09:00 +01002333 nlmsg_end(skb, nlh);
2334 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002335
2336nla_put_failure:
2337 nlmsg_cancel(skb, nlh);
2338 return -EMSGSIZE;
2339}
2340
Yuval Mintz7b0db852018-02-28 23:29:39 +02002341static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2342 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2343 int flags)
2344{
2345 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2346 cmd, flags);
2347}
2348
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002349static size_t mroute_msgsize(bool unresolved, int maxvif)
2350{
2351 size_t len =
2352 NLMSG_ALIGN(sizeof(struct rtmsg))
2353 + nla_total_size(4) /* RTA_TABLE */
2354 + nla_total_size(4) /* RTA_SRC */
2355 + nla_total_size(4) /* RTA_DST */
2356 ;
2357
2358 if (!unresolved)
2359 len = len
2360 + nla_total_size(4) /* RTA_IIF */
2361 + nla_total_size(0) /* RTA_MULTIPATH */
2362 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2363 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002364 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002365 ;
2366
2367 return len;
2368}
2369
2370static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2371 int cmd)
2372{
2373 struct net *net = read_pnet(&mrt->net);
2374 struct sk_buff *skb;
2375 int err = -ENOBUFS;
2376
Yuval Mintz494fff52018-02-28 23:29:34 +02002377 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2378 mrt->maxvif),
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002379 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002380 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002381 goto errout;
2382
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002383 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002384 if (err < 0)
2385 goto errout;
2386
2387 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2388 return;
2389
2390errout:
2391 kfree_skb(skb);
2392 if (err < 0)
2393 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2394}
2395
Julien Gomes5a645dd2017-06-20 13:54:17 -07002396static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2397{
2398 size_t len =
2399 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2400 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2401 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2402 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2403 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
Paul Davey501cb002020-09-08 10:04:06 +12002404 + nla_total_size(4) /* IPMRA_CREPORT_TABLE */
Julien Gomes5a645dd2017-06-20 13:54:17 -07002405 /* IPMRA_CREPORT_PKT */
2406 + nla_total_size(payloadlen)
2407 ;
2408
2409 return len;
2410}
2411
2412static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2413{
2414 struct net *net = read_pnet(&mrt->net);
2415 struct nlmsghdr *nlh;
2416 struct rtgenmsg *rtgenm;
2417 struct igmpmsg *msg;
2418 struct sk_buff *skb;
2419 struct nlattr *nla;
2420 int payloadlen;
2421
2422 payloadlen = pkt->len - sizeof(struct igmpmsg);
2423 msg = (struct igmpmsg *)skb_network_header(pkt);
2424
2425 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2426 if (!skb)
2427 goto errout;
2428
2429 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2430 sizeof(struct rtgenmsg), 0);
2431 if (!nlh)
2432 goto errout;
2433 rtgenm = nlmsg_data(nlh);
2434 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2435 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
Paul Daveybb82067c2020-09-08 10:04:08 +12002436 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif | (msg->im_vif_hi << 8)) ||
Julien Gomes5a645dd2017-06-20 13:54:17 -07002437 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2438 msg->im_src.s_addr) ||
2439 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
Paul Davey501cb002020-09-08 10:04:06 +12002440 msg->im_dst.s_addr) ||
2441 nla_put_u32(skb, IPMRA_CREPORT_TABLE, mrt->id))
Julien Gomes5a645dd2017-06-20 13:54:17 -07002442 goto nla_put_failure;
2443
2444 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2445 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2446 nla_data(nla), payloadlen))
2447 goto nla_put_failure;
2448
2449 nlmsg_end(skb, nlh);
2450
2451 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2452 return;
2453
2454nla_put_failure:
2455 nlmsg_cancel(skb, nlh);
2456errout:
2457 kfree_skb(skb);
2458 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2459}
2460
Jakub Kicinskid0440022019-01-18 10:46:20 -08002461static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb,
2462 const struct nlmsghdr *nlh,
2463 struct nlattr **tb,
2464 struct netlink_ext_ack *extack)
2465{
2466 struct rtmsg *rtm;
2467 int i, err;
2468
2469 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
2470 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for multicast route get request");
2471 return -EINVAL;
2472 }
2473
2474 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02002475 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
2476 rtm_ipv4_policy, extack);
Jakub Kicinskid0440022019-01-18 10:46:20 -08002477
2478 rtm = nlmsg_data(nlh);
2479 if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
2480 (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
2481 rtm->rtm_tos || rtm->rtm_table || rtm->rtm_protocol ||
2482 rtm->rtm_scope || rtm->rtm_type || rtm->rtm_flags) {
2483 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for multicast route get request");
2484 return -EINVAL;
2485 }
2486
Johannes Berg8cb08172019-04-26 14:07:28 +02002487 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
2488 rtm_ipv4_policy, extack);
Jakub Kicinskid0440022019-01-18 10:46:20 -08002489 if (err)
2490 return err;
2491
2492 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
2493 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
2494 NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
2495 return -EINVAL;
2496 }
2497
2498 for (i = 0; i <= RTA_MAX; i++) {
2499 if (!tb[i])
2500 continue;
2501
2502 switch (i) {
2503 case RTA_SRC:
2504 case RTA_DST:
2505 case RTA_TABLE:
2506 break;
2507 default:
2508 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in multicast route get request");
2509 return -EINVAL;
2510 }
2511 }
2512
2513 return 0;
2514}
2515
Donald Sharp4f75ba62017-06-28 13:58:57 -04002516static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2517 struct netlink_ext_ack *extack)
2518{
2519 struct net *net = sock_net(in_skb->sk);
2520 struct nlattr *tb[RTA_MAX + 1];
2521 struct sk_buff *skb = NULL;
2522 struct mfc_cache *cache;
2523 struct mr_table *mrt;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002524 __be32 src, grp;
2525 u32 tableid;
2526 int err;
2527
Jakub Kicinskid0440022019-01-18 10:46:20 -08002528 err = ipmr_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Donald Sharp4f75ba62017-06-28 13:58:57 -04002529 if (err < 0)
2530 goto errout;
2531
Donald Sharp4f75ba62017-06-28 13:58:57 -04002532 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2533 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2534 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2535
2536 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
Dan Carpenter2e3d2322017-07-12 10:56:47 +03002537 if (!mrt) {
2538 err = -ENOENT;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002539 goto errout_free;
2540 }
2541
2542 /* entries are added/deleted only under RTNL */
2543 rcu_read_lock();
2544 cache = ipmr_cache_find(mrt, src, grp);
2545 rcu_read_unlock();
2546 if (!cache) {
2547 err = -ENOENT;
2548 goto errout_free;
2549 }
2550
2551 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2552 if (!skb) {
2553 err = -ENOBUFS;
2554 goto errout_free;
2555 }
2556
2557 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2558 nlh->nlmsg_seq, cache,
2559 RTM_NEWROUTE, 0);
2560 if (err < 0)
2561 goto errout_free;
2562
2563 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2564
2565errout:
2566 return err;
2567
2568errout_free:
2569 kfree_skb(skb);
2570 goto errout;
2571}
2572
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002573static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2574{
David Ahern47246762018-10-15 18:56:42 -07002575 struct fib_dump_filter filter = {};
David Aherncb167892018-10-15 18:56:47 -07002576 int err;
David Aherne8ba3302018-10-07 20:16:35 -07002577
David Ahern47246762018-10-15 18:56:42 -07002578 if (cb->strict_check) {
David Ahern47246762018-10-15 18:56:42 -07002579 err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh,
David Aherneffe6792018-10-15 18:56:48 -07002580 &filter, cb);
David Aherne8ba3302018-10-07 20:16:35 -07002581 if (err < 0)
2582 return err;
2583 }
2584
David Aherncb167892018-10-15 18:56:47 -07002585 if (filter.table_id) {
2586 struct mr_table *mrt;
2587
2588 mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id);
2589 if (!mrt) {
Sabrina Dubroca41b4bd92020-05-20 11:15:46 +02002590 if (rtnl_msg_family(cb->nlh) != RTNL_FAMILY_IPMR)
David Ahernae677bb2018-10-24 12:59:01 -07002591 return skb->len;
2592
David Aherncb167892018-10-15 18:56:47 -07002593 NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist");
2594 return -ENOENT;
2595 }
2596 err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute,
2597 &mfc_unres_lock, &filter);
2598 return skb->len ? : err;
2599 }
2600
Yuval Mintz7b0db852018-02-28 23:29:39 +02002601 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
David Aherncb167892018-10-15 18:56:47 -07002602 _ipmr_fill_mroute, &mfc_unres_lock, &filter);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002603}
2604
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002605static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2606 [RTA_SRC] = { .type = NLA_U32 },
2607 [RTA_DST] = { .type = NLA_U32 },
2608 [RTA_IIF] = { .type = NLA_U32 },
2609 [RTA_TABLE] = { .type = NLA_U32 },
2610 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2611};
2612
2613static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2614{
2615 switch (rtm_protocol) {
2616 case RTPROT_STATIC:
2617 case RTPROT_MROUTED:
2618 return true;
2619 }
2620 return false;
2621}
2622
2623static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2624{
2625 struct rtnexthop *rtnh = nla_data(nla);
2626 int remaining = nla_len(nla), vifi = 0;
2627
2628 while (rtnh_ok(rtnh, remaining)) {
2629 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2630 if (++vifi == MAXVIFS)
2631 break;
2632 rtnh = rtnh_next(rtnh, &remaining);
2633 }
2634
2635 return remaining > 0 ? -EINVAL : vifi;
2636}
2637
2638/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2639static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2640 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002641 struct mr_table **mrtret,
2642 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002643{
2644 struct net_device *dev = NULL;
2645 u32 tblid = RT_TABLE_DEFAULT;
2646 struct mr_table *mrt;
2647 struct nlattr *attr;
2648 struct rtmsg *rtm;
2649 int ret, rem;
2650
Johannes Berg8cb08172019-04-26 14:07:28 +02002651 ret = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
2652 rtm_ipmr_policy, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002653 if (ret < 0)
2654 goto out;
2655 rtm = nlmsg_data(nlh);
2656
2657 ret = -EINVAL;
2658 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2659 rtm->rtm_type != RTN_MULTICAST ||
2660 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2661 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2662 goto out;
2663
2664 memset(mfcc, 0, sizeof(*mfcc));
2665 mfcc->mfcc_parent = -1;
2666 ret = 0;
2667 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2668 switch (nla_type(attr)) {
2669 case RTA_SRC:
2670 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2671 break;
2672 case RTA_DST:
2673 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2674 break;
2675 case RTA_IIF:
2676 dev = __dev_get_by_index(net, nla_get_u32(attr));
2677 if (!dev) {
2678 ret = -ENODEV;
2679 goto out;
2680 }
2681 break;
2682 case RTA_MULTIPATH:
2683 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2684 ret = -EINVAL;
2685 goto out;
2686 }
2687 break;
2688 case RTA_PREFSRC:
2689 ret = 1;
2690 break;
2691 case RTA_TABLE:
2692 tblid = nla_get_u32(attr);
2693 break;
2694 }
2695 }
2696 mrt = ipmr_get_table(net, tblid);
2697 if (!mrt) {
2698 ret = -ENOENT;
2699 goto out;
2700 }
2701 *mrtret = mrt;
2702 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2703 if (dev)
2704 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2705
2706out:
2707 return ret;
2708}
2709
2710/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002711static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2712 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002713{
2714 struct net *net = sock_net(skb->sk);
2715 int ret, mrtsock, parent;
2716 struct mr_table *tbl;
2717 struct mfcctl mfcc;
2718
2719 mrtsock = 0;
2720 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002721 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002722 if (ret < 0)
2723 return ret;
2724
2725 parent = ret ? mfcc.mfcc_parent : -1;
2726 if (nlh->nlmsg_type == RTM_NEWROUTE)
2727 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2728 else
2729 return ipmr_mfc_delete(tbl, &mfcc, parent);
2730}
2731
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002732static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2733{
2734 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2735
2736 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2737 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2738 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2739 mrt->mroute_reg_vif_num) ||
2740 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2741 mrt->mroute_do_assert) ||
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03002742 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
2743 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
2744 mrt->mroute_do_wrvifwhole))
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002745 return false;
2746
2747 return true;
2748}
2749
2750static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2751{
2752 struct nlattr *vif_nest;
2753 struct vif_device *vif;
2754
2755 /* if the VIF doesn't exist just continue */
2756 if (!VIF_EXISTS(mrt, vifid))
2757 return true;
2758
2759 vif = &mrt->vif_table[vifid];
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002760 vif_nest = nla_nest_start_noflag(skb, IPMRA_VIF);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002761 if (!vif_nest)
2762 return false;
2763 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2764 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2765 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2766 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2767 IPMRA_VIFA_PAD) ||
2768 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2769 IPMRA_VIFA_PAD) ||
2770 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2771 IPMRA_VIFA_PAD) ||
2772 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2773 IPMRA_VIFA_PAD) ||
2774 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2775 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2776 nla_nest_cancel(skb, vif_nest);
2777 return false;
2778 }
2779 nla_nest_end(skb, vif_nest);
2780
2781 return true;
2782}
2783
David Ahern14fc5bb2018-10-07 20:16:34 -07002784static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
2785 struct netlink_ext_ack *extack)
2786{
2787 struct ifinfomsg *ifm;
2788
2789 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2790 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
2791 return -EINVAL;
2792 }
2793
2794 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
2795 NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump");
2796 return -EINVAL;
2797 }
2798
2799 ifm = nlmsg_data(nlh);
2800 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2801 ifm->ifi_change || ifm->ifi_index) {
2802 NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
2803 return -EINVAL;
2804 }
2805
2806 return 0;
2807}
2808
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002809static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2810{
2811 struct net *net = sock_net(skb->sk);
2812 struct nlmsghdr *nlh = NULL;
2813 unsigned int t = 0, s_t;
2814 unsigned int e = 0, s_e;
2815 struct mr_table *mrt;
2816
David Ahern14fc5bb2018-10-07 20:16:34 -07002817 if (cb->strict_check) {
2818 int err = ipmr_valid_dumplink(cb->nlh, cb->extack);
2819
2820 if (err < 0)
2821 return err;
2822 }
2823
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002824 s_t = cb->args[0];
2825 s_e = cb->args[1];
2826
2827 ipmr_for_each_table(mrt, net) {
2828 struct nlattr *vifs, *af;
2829 struct ifinfomsg *hdr;
2830 u32 i;
2831
2832 if (t < s_t)
2833 goto skip_table;
2834 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2835 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2836 sizeof(*hdr), NLM_F_MULTI);
2837 if (!nlh)
2838 break;
2839
2840 hdr = nlmsg_data(nlh);
2841 memset(hdr, 0, sizeof(*hdr));
2842 hdr->ifi_family = RTNL_FAMILY_IPMR;
2843
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002844 af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002845 if (!af) {
2846 nlmsg_cancel(skb, nlh);
2847 goto out;
2848 }
2849
2850 if (!ipmr_fill_table(mrt, skb)) {
2851 nlmsg_cancel(skb, nlh);
2852 goto out;
2853 }
2854
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002855 vifs = nla_nest_start_noflag(skb, IPMRA_TABLE_VIFS);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002856 if (!vifs) {
2857 nla_nest_end(skb, af);
2858 nlmsg_end(skb, nlh);
2859 goto out;
2860 }
2861 for (i = 0; i < mrt->maxvif; i++) {
2862 if (e < s_e)
2863 goto skip_entry;
2864 if (!ipmr_fill_vif(mrt, i, skb)) {
2865 nla_nest_end(skb, vifs);
2866 nla_nest_end(skb, af);
2867 nlmsg_end(skb, nlh);
2868 goto out;
2869 }
2870skip_entry:
2871 e++;
2872 }
2873 s_e = 0;
2874 e = 0;
2875 nla_nest_end(skb, vifs);
2876 nla_nest_end(skb, af);
2877 nlmsg_end(skb, nlh);
2878skip_table:
2879 t++;
2880 }
2881
2882out:
2883 cb->args[1] = e;
2884 cb->args[0] = t;
2885
2886 return skb->len;
2887}
2888
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002889#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002890/* The /proc interfaces to multicast routing :
2891 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002892 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893
2894static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002895 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002897 struct mr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002898 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002899 struct mr_table *mrt;
2900
2901 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002902 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002903 return ERR_PTR(-ENOENT);
2904
2905 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002906
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 read_lock(&mrt_lock);
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002908 return mr_vif_seq_start(seq, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002909}
2910
2911static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002912 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913{
2914 read_unlock(&mrt_lock);
2915}
2916
2917static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2918{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002919 struct mr_vif_iter *iter = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002920 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002921
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002923 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2925 } else {
2926 const struct vif_device *vif = v;
Yuval Mintz6853f212018-02-28 23:29:29 +02002927 const char *name = vif->dev ?
2928 vif->dev->name : "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002929
2930 seq_printf(seq,
James Hogan91e6dd82018-01-30 09:48:02 +00002931 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002932 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002933 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934 vif->bytes_out, vif->pkt_out,
2935 vif->flags, vif->local, vif->remote);
2936 }
2937 return 0;
2938}
2939
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002940static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 .start = ipmr_vif_seq_start,
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002942 .next = mr_vif_seq_next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 .stop = ipmr_vif_seq_stop,
2944 .show = ipmr_vif_seq_show,
2945};
2946
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2948{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002949 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002950 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002951
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002952 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002953 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002954 return ERR_PTR(-ENOENT);
2955
Yuval Mintzc8d61962018-02-28 23:29:36 +02002956 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957}
2958
2959static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2960{
2961 int n;
2962
2963 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002964 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2966 } else {
2967 const struct mfc_cache *mfc = v;
Yuval Mintzc8d61962018-02-28 23:29:36 +02002968 const struct mr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002969 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002970
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002971 seq_printf(seq, "%08X %08X %-3hd",
2972 (__force u32) mfc->mfc_mcastgrp,
2973 (__force u32) mfc->mfc_origin,
Yuval Mintz494fff52018-02-28 23:29:34 +02002974 mfc->_c.mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
Patrick McHardy0c122952010-04-13 05:03:22 +00002976 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002977 seq_printf(seq, " %8lu %8lu %8lu",
Yuval Mintz494fff52018-02-28 23:29:34 +02002978 mfc->_c.mfc_un.res.pkt,
2979 mfc->_c.mfc_un.res.bytes,
2980 mfc->_c.mfc_un.res.wrong_if);
2981 for (n = mfc->_c.mfc_un.res.minvif;
2982 n < mfc->_c.mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002983 if (VIF_EXISTS(mrt, n) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002984 mfc->_c.mfc_un.res.ttls[n] < 255)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002985 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002986 " %2d:%-3d",
Yuval Mintz494fff52018-02-28 23:29:34 +02002987 n, mfc->_c.mfc_un.res.ttls[n]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002988 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002989 } else {
2990 /* unresolved mfc_caches don't contain
2991 * pkt, bytes and wrong_if values
2992 */
2993 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 }
2995 seq_putc(seq, '\n');
2996 }
2997 return 0;
2998}
2999
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003000static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 .start = ipmr_mfc_seq_start,
Yuval Mintzc8d61962018-02-28 23:29:36 +02003002 .next = mr_mfc_seq_next,
3003 .stop = mr_mfc_seq_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 .show = ipmr_mfc_seq_show,
3005};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003006#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003007
3008#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00003009static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 .handler = pim_rcv,
3011};
3012#endif
3013
Yotam Gigi4d65b942017-09-27 08:23:13 +02003014static unsigned int ipmr_seq_read(struct net *net)
3015{
3016 ASSERT_RTNL();
3017
3018 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
3019}
3020
Jiri Pirkob7a59552019-10-03 11:49:30 +02003021static int ipmr_dump(struct net *net, struct notifier_block *nb,
3022 struct netlink_ext_ack *extack)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003023{
Yuval Mintzcdc9f942018-03-26 15:01:33 +03003024 return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
Jiri Pirkob7a59552019-10-03 11:49:30 +02003025 ipmr_mr_table_iter, &mrt_lock, extack);
Yotam Gigi4d65b942017-09-27 08:23:13 +02003026}
3027
3028static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3029 .family = RTNL_FAMILY_IPMR,
3030 .fib_seq_read = ipmr_seq_read,
3031 .fib_dump = ipmr_dump,
3032 .owner = THIS_MODULE,
3033};
3034
Colin Ian Kingef739d82017-09-29 14:34:22 +01003035static int __net_init ipmr_notifier_init(struct net *net)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003036{
3037 struct fib_notifier_ops *ops;
3038
3039 net->ipv4.ipmr_seq = 0;
3040
3041 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3042 if (IS_ERR(ops))
3043 return PTR_ERR(ops);
3044 net->ipv4.ipmr_notifier_ops = ops;
3045
3046 return 0;
3047}
3048
3049static void __net_exit ipmr_notifier_exit(struct net *net)
3050{
3051 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3052 net->ipv4.ipmr_notifier_ops = NULL;
3053}
3054
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01003055/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00003056static int __net_init ipmr_net_init(struct net *net)
3057{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003058 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00003059
Yotam Gigi4d65b942017-09-27 08:23:13 +02003060 err = ipmr_notifier_init(net);
3061 if (err)
3062 goto ipmr_notifier_fail;
3063
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003064 err = ipmr_rules_init(net);
3065 if (err < 0)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003066 goto ipmr_rules_fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003067
3068#ifdef CONFIG_PROC_FS
3069 err = -ENOMEM;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003070 if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
3071 sizeof(struct mr_vif_iter)))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003072 goto proc_vif_fail;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003073 if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
3074 sizeof(struct mr_mfc_iter)))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003075 goto proc_cache_fail;
3076#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00003077 return 0;
3078
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003079#ifdef CONFIG_PROC_FS
3080proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00003081 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003082proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003083 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003084#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003085ipmr_rules_fail:
3086 ipmr_notifier_exit(net);
3087ipmr_notifier_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003088 return err;
3089}
3090
3091static void __net_exit ipmr_net_exit(struct net *net)
3092{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003093#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003094 remove_proc_entry("ip_mr_cache", net->proc_net);
3095 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003096#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003097 ipmr_notifier_exit(net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003098 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00003099}
3100
3101static struct pernet_operations ipmr_net_ops = {
3102 .init = ipmr_net_init,
3103 .exit = ipmr_net_exit,
3104};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003105
Wang Chen03d2f892008-07-03 12:13:36 +08003106int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107{
Wang Chen03d2f892008-07-03 12:13:36 +08003108 int err;
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3111 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00003112 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003113 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08003114
Benjamin Therycf958ae32009-01-22 04:56:16 +00003115 err = register_pernet_subsys(&ipmr_net_ops);
3116 if (err)
3117 goto reg_pernet_fail;
3118
Wang Chen03d2f892008-07-03 12:13:36 +08003119 err = register_netdevice_notifier(&ip_mr_notifier);
3120 if (err)
3121 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07003122#ifdef CONFIG_IP_PIMSM_V2
3123 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00003124 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07003125 err = -EAGAIN;
3126 goto add_proto_fail;
3127 }
3128#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00003129 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003130 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003131 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003132 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003133 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003134 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03003135
3136 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
Florian Westphalb97bac62017-08-09 20:41:48 +02003137 NULL, ipmr_rtm_dumplink, 0);
Wang Chen03d2f892008-07-03 12:13:36 +08003138 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003139
Tom Goff403dbb92009-06-14 03:16:13 -07003140#ifdef CONFIG_IP_PIMSM_V2
3141add_proto_fail:
3142 unregister_netdevice_notifier(&ip_mr_notifier);
3143#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08003144reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003145 unregister_pernet_subsys(&ipmr_net_ops);
3146reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08003147 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08003148 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}