blob: b2363b82b48d5ea2c31c34581d151fc2265c743d [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
Wang Chend6070322008-07-14 20:55:26 -0700424static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
425{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000426 struct net *net = dev_net(dev);
427
Wang Chend6070322008-07-14 20:55:26 -0700428 dev_close(dev);
429
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000430 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700431 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800432 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700433 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700434 struct ip_tunnel_parm p;
435
436 memset(&p, 0, sizeof(p));
437 p.iph.daddr = v->vifc_rmt_addr.s_addr;
438 p.iph.saddr = v->vifc_lcl_addr.s_addr;
439 p.iph.version = 4;
440 p.iph.ihl = 5;
441 p.iph.protocol = IPPROTO_IPIP;
442 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
443 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
444
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800445 if (ops->ndo_do_ioctl) {
446 mm_segment_t oldfs = get_fs();
447
448 set_fs(KERNEL_DS);
449 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
450 set_fs(oldfs);
451 }
Wang Chend6070322008-07-14 20:55:26 -0700452 }
453}
454
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100455/* Initialize ipmr pimreg/tunnel in_device */
456static bool ipmr_init_vif_indev(const struct net_device *dev)
457{
458 struct in_device *in_dev;
459
460 ASSERT_RTNL();
461
462 in_dev = __in_dev_get_rtnl(dev);
463 if (!in_dev)
464 return false;
465 ipv4_devconf_setall(in_dev);
466 neigh_parms_data_state_setall(in_dev->arp_parms);
467 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
468
469 return true;
470}
471
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100472static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473{
474 struct net_device *dev;
475
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000476 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800479 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 int err;
481 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 memset(&p, 0, sizeof(p));
485 p.iph.daddr = v->vifc_rmt_addr.s_addr;
486 p.iph.saddr = v->vifc_lcl_addr.s_addr;
487 p.iph.version = 4;
488 p.iph.ihl = 5;
489 p.iph.protocol = IPPROTO_IPIP;
490 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800491 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800493 if (ops->ndo_do_ioctl) {
494 mm_segment_t oldfs = get_fs();
495
496 set_fs(KERNEL_DS);
497 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
498 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000499 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800500 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 dev = NULL;
503
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000504 if (err == 0 &&
505 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100507 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 goto failure;
Petr Machata00f54e62018-12-06 17:05:36 +0000509 if (dev_open(dev, NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700511 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513 }
514 return dev;
515
516failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 unregister_netdevice(dev);
518 return NULL;
519}
520
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100521#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000522static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000524 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000525 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500526 struct flowi4 fl4 = {
527 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700528 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500529 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000530 };
531 int err;
532
David S. Millerda919812011-03-12 02:04:50 -0500533 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000534 if (err < 0) {
535 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000536 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000537 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700540 dev->stats.tx_bytes += skb->len;
541 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000542 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 read_unlock(&mrt_lock);
544 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000545 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546}
547
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200548static int reg_vif_get_iflink(const struct net_device *dev)
549{
550 return 0;
551}
552
Stephen Hemminger007c3832008-11-20 20:28:35 -0800553static const struct net_device_ops reg_vif_netdev_ops = {
554 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200555 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800556};
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558static void reg_vif_setup(struct net_device *dev)
559{
560 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800561 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530563 dev->netdev_ops = &reg_vif_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400564 dev->needs_free_netdev = true;
Tom Goff403dbb92009-06-14 03:16:13 -0700565 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566}
567
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000568static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569{
570 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000571 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000573 if (mrt->id == RT_TABLE_DEFAULT)
574 sprintf(name, "pimreg");
575 else
576 sprintf(name, "pimreg%u", mrt->id);
577
Tom Gundersenc835a672014-07-14 16:37:24 +0200578 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579
Ian Morris51456b22015-04-03 09:17:26 +0100580 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return NULL;
582
Tom Goff403dbb92009-06-14 03:16:13 -0700583 dev_net_set(dev, net);
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 if (register_netdevice(dev)) {
586 free_netdev(dev);
587 return NULL;
588 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100590 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 goto failure;
Petr Machata00f54e62018-12-06 17:05:36 +0000592 if (dev_open(dev, NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 goto failure;
594
Wang Chen7dc00c82008-07-14 20:56:34 -0700595 dev_hold(dev);
596
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 return dev;
598
599failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 unregister_netdevice(dev);
601 return NULL;
602}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100603
604/* called with rcu_read_lock() */
605static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
606 unsigned int pimlen)
607{
608 struct net_device *reg_dev = NULL;
609 struct iphdr *encap;
610
611 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100612 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100613 * a. packet is really sent to a multicast group
614 * b. packet is not a NULL-REGISTER
615 * c. packet is not truncated
616 */
617 if (!ipv4_is_multicast(encap->daddr) ||
618 encap->tot_len == 0 ||
619 ntohs(encap->tot_len) + pimlen > skb->len)
620 return 1;
621
622 read_lock(&mrt_lock);
623 if (mrt->mroute_reg_vif_num >= 0)
624 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
625 read_unlock(&mrt_lock);
626
627 if (!reg_dev)
628 return 1;
629
630 skb->mac_header = skb->network_header;
631 skb_pull(skb, (u8 *)encap - skb->data);
632 skb_reset_network_header(skb);
633 skb->protocol = htons(ETH_P_IP);
634 skb->ip_summed = CHECKSUM_NONE;
635
636 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
637
638 netif_rx(skb);
639
640 return NET_RX_SUCCESS;
641}
642#else
643static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
644{
645 return NULL;
646}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647#endif
648
Yotam Gigib3620532017-09-27 08:23:14 +0200649static int call_ipmr_vif_entry_notifiers(struct net *net,
650 enum fib_event_type event_type,
651 struct vif_device *vif,
652 vifi_t vif_index, u32 tb_id)
653{
Yuval Mintzbc67a0d2018-03-26 15:01:31 +0300654 return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
655 vif, vif_index, tb_id,
656 &net->ipv4.ipmr_seq);
Yotam Gigib3620532017-09-27 08:23:14 +0200657}
658
Yotam Gigib3620532017-09-27 08:23:14 +0200659static int call_ipmr_mfc_entry_notifiers(struct net *net,
660 enum fib_event_type event_type,
661 struct mfc_cache *mfc, u32 tb_id)
662{
Yuval Mintz54c4cad2018-03-26 15:01:32 +0300663 return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
664 &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
Yotam Gigib3620532017-09-27 08:23:14 +0200665}
666
Ben Hutchings2c530402012-07-10 10:55:09 +0000667/**
668 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700669 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000671static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000672 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Yotam Gigib3620532017-09-27 08:23:14 +0200674 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 struct vif_device *v;
676 struct net_device *dev;
677 struct in_device *in_dev;
678
Patrick McHardy0c122952010-04-13 05:03:22 +0000679 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return -EADDRNOTAVAIL;
681
Patrick McHardy0c122952010-04-13 05:03:22 +0000682 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Yotam Gigib3620532017-09-27 08:23:14 +0200684 if (VIF_EXISTS(mrt, vifi))
685 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
686 mrt->id);
687
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 write_lock_bh(&mrt_lock);
689 dev = v->dev;
690 v->dev = NULL;
691
692 if (!dev) {
693 write_unlock_bh(&mrt_lock);
694 return -EADDRNOTAVAIL;
695 }
696
Patrick McHardy0c122952010-04-13 05:03:22 +0000697 if (vifi == mrt->mroute_reg_vif_num)
698 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000700 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000702
703 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000704 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 break;
706 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000707 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 }
709
710 write_unlock_bh(&mrt_lock);
711
712 dev_set_allmulti(dev, -1);
713
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000714 in_dev = __in_dev_get_rtnl(dev);
715 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700716 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -0700717 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000718 NETCONFA_MC_FORWARDING,
719 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 ip_rt_multicast_event(in_dev);
721 }
722
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000723 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000724 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 dev_put(dev);
727 return 0;
728}
729
Eric Dumazeta8c94862010-10-01 16:15:08 +0000730static void ipmr_cache_free_rcu(struct rcu_head *head)
731{
Yuval Mintz494fff52018-02-28 23:29:34 +0200732 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000733
Yuval Mintz494fff52018-02-28 23:29:34 +0200734 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000735}
736
Yuval Mintz8c13af22018-03-26 15:01:36 +0300737static void ipmr_cache_free(struct mfc_cache *c)
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000738{
Yuval Mintz494fff52018-02-28 23:29:34 +0200739 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000740}
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000743 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000745static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200747 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700749 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Patrick McHardy0c122952010-04-13 05:03:22 +0000751 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Yuval Mintz494fff52018-02-28 23:29:34 +0200753 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700754 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200755 struct nlmsghdr *nlh = skb_pull(skb,
756 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000758 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000760 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700761 e->error = -ETIMEDOUT;
762 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700763
Eric W. Biederman15e47302012-09-07 20:12:54 +0000764 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000765 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 }
769
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000770 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771}
772
Patrick McHardye258beb2010-04-13 05:03:19 +0000773/* Timer process for the unresolved queue. */
Kees Cooke99e88a2017-10-16 14:43:17 -0700774static void ipmr_expire_process(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Kees Cooke99e88a2017-10-16 14:43:17 -0700776 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
Yuval Mintz494fff52018-02-28 23:29:34 +0200777 struct mr_mfc *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 unsigned long expires;
Yuval Mintz494fff52018-02-28 23:29:34 +0200779 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
781 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000782 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return;
784 }
785
Patrick McHardy0c122952010-04-13 05:03:22 +0000786 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 goto out;
788
789 now = jiffies;
790 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
Patrick McHardy0c122952010-04-13 05:03:22 +0000792 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 if (time_after(c->mfc_un.unres.expires, now)) {
794 unsigned long interval = c->mfc_un.unres.expires - now;
795 if (interval < expires)
796 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 continue;
798 }
799
Patrick McHardy862465f2010-04-13 05:03:21 +0000800 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +0200801 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
802 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 }
804
Patrick McHardy0c122952010-04-13 05:03:22 +0000805 if (!list_empty(&mrt->mfc_unres_queue))
806 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808out:
809 spin_unlock(&mfc_unres_lock);
810}
811
812/* Fill oifs list. It is called under write locked mrt_lock. */
Yuval Mintz494fff52018-02-28 23:29:34 +0200813static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000814 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815{
816 int vifi;
817
818 cache->mfc_un.res.minvif = MAXVIFS;
819 cache->mfc_un.res.maxvif = 0;
820 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
821
Patrick McHardy0c122952010-04-13 05:03:22 +0000822 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
823 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000824 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
826 if (cache->mfc_un.res.minvif > vifi)
827 cache->mfc_un.res.minvif = vifi;
828 if (cache->mfc_un.res.maxvif <= vifi)
829 cache->mfc_un.res.maxvif = vifi + 1;
830 }
831 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200832 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Patrick McHardy0c122952010-04-13 05:03:22 +0000835static int vif_add(struct net *net, struct mr_table *mrt,
836 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837{
Florian Fainellibccb3022019-02-06 09:45:46 -0800838 struct netdev_phys_item_id ppid = { };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000840 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct net_device *dev;
842 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700843 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000846 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 return -EADDRINUSE;
848
849 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100851 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100852 return -EINVAL;
853 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 * All the packets will be sent to the daemon
855 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000856 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000858 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (!dev)
860 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700861 err = dev_set_allmulti(dev, 1);
862 if (err) {
863 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700864 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700865 return err;
866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900868 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000869 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 if (!dev)
871 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700872 err = dev_set_allmulti(dev, 1);
873 if (err) {
874 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700875 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700876 return err;
877 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000879 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000881 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
882 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100883 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000884 dev_put(dev);
885 return -EADDRNOTAVAIL;
886 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000887 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000888 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000889 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (!dev)
891 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700892 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700893 if (err) {
894 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700895 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 break;
898 default:
899 return -EINVAL;
900 }
901
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000902 in_dev = __in_dev_get_rtnl(dev);
903 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000904 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000906 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700907 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700908 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
909 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 ip_rt_multicast_event(in_dev);
911
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000912 /* Fill in the VIF structures */
Yuval Mintz6853f212018-02-28 23:29:29 +0200913 vif_device_init(v, dev, vifc->vifc_rate_limit,
914 vifc->vifc_threshold,
915 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
916 (VIFF_TUNNEL | VIFF_REGISTER));
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000917
Florian Fainellibccb3022019-02-06 09:45:46 -0800918 err = dev_get_port_parent_id(dev, &ppid, true);
919 if (err == 0) {
920 memcpy(v->dev_parent_id.id, ppid.id, ppid.id_len);
921 v->dev_parent_id.id_len = ppid.id_len;
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200922 } else {
923 v->dev_parent_id.id_len = 0;
924 }
Yuval Mintz6853f212018-02-28 23:29:29 +0200925
Jianjun Kongc354e122008-11-03 00:28:02 -0800926 v->local = vifc->vifc_lcl_addr.s_addr;
927 v->remote = vifc->vifc_rmt_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 /* And finish update writing critical data */
930 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800931 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000932 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000933 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000934 if (vifi+1 > mrt->maxvif)
935 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +0200937 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 return 0;
939}
940
Eric Dumazeta8c94862010-10-01 16:15:08 +0000941/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000942static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000943 __be32 origin,
944 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100946 struct mfc_cache_cmp_arg arg = {
947 .mfc_mcastgrp = mcastgrp,
948 .mfc_origin = origin
949 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950
Yuval Mintz845c9a72018-02-28 23:29:35 +0200951 return mr_mfc_find(mrt, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000952}
953
954/* Look for a (*,G) entry */
955static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
956 __be32 mcastgrp, int vifi)
957{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100958 struct mfc_cache_cmp_arg arg = {
959 .mfc_mcastgrp = mcastgrp,
960 .mfc_origin = htonl(INADDR_ANY)
961 };
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000962
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100963 if (mcastgrp == htonl(INADDR_ANY))
Yuval Mintz845c9a72018-02-28 23:29:35 +0200964 return mr_mfc_find_any_parent(mrt, vifi);
965 return mr_mfc_find_any(mrt, vifi, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000966}
967
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100968/* Look for a (S,G,iif) entry if parent != -1 */
969static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
970 __be32 origin, __be32 mcastgrp,
971 int parent)
972{
973 struct mfc_cache_cmp_arg arg = {
974 .mfc_mcastgrp = mcastgrp,
975 .mfc_origin = origin,
976 };
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100977
Yuval Mintz845c9a72018-02-28 23:29:35 +0200978 return mr_mfc_find_parent(mrt, &arg, parent);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100979}
980
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100981/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000982static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983{
Jianjun Kongc354e122008-11-03 00:28:02 -0800984 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000985
Tom Goff70a0dec2016-06-23 16:11:57 -0400986 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +0200987 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
988 c->_c.mfc_un.res.minvif = MAXVIFS;
Yuval Mintz8c13af22018-03-26 15:01:36 +0300989 c->_c.free = ipmr_cache_free_rcu;
Yuval Mintz494fff52018-02-28 23:29:34 +0200990 refcount_set(&c->_c.mfc_un.res.refcount, 1);
Tom Goff70a0dec2016-06-23 16:11:57 -0400991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 return c;
993}
994
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000995static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996{
Jianjun Kongc354e122008-11-03 00:28:02 -0800997 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000998
999 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001000 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
1001 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001002 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return c;
1004}
1005
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001006/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +00001007static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1008 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001011 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001013 /* Play the pending entries through our router */
Yuval Mintz494fff52018-02-28 23:29:34 +02001014 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001015 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +02001016 struct nlmsghdr *nlh = skb_pull(skb,
1017 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Yuval Mintz7b0db852018-02-28 23:29:39 +02001019 if (mr_fill_mroute(mrt, skb, &c->_c,
1020 nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001021 nlh->nlmsg_len = skb_tail_pointer(skb) -
1022 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 } else {
1024 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +00001025 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +00001027 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001028 e->error = -EMSGSIZE;
1029 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 }
Thomas Graf2942e902006-08-15 00:30:25 -07001031
Eric W. Biederman15e47302012-09-07 20:12:54 +00001032 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001033 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001034 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001035 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037}
1038
Julien Gomes5a645dd2017-06-20 13:54:17 -07001039/* Bounce a cache query up to mrouted and netlink.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001041 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001043static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001044 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001046 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001047 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 struct igmphdr *igmp;
1049 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001050 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 int ret;
1052
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001053 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1055 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 skb = alloc_skb(128, GFP_ATOMIC);
1057
Stephen Hemminger132adf52007-03-08 20:44:43 -08001058 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return -ENOBUFS;
1060
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001061 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001063 * Duplicate old header, fix ihl, length etc.
1064 * And all this only to mangle msg->im_msgtype and
1065 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001067 skb_push(skb, sizeof(struct iphdr));
1068 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001069 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001070 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001071 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001072 msg->im_msgtype = assert;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 msg->im_mbz = 0;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001074 if (assert == IGMPMSG_WRVIFWHOLE)
1075 msg->im_vif = vifi;
1076 else
1077 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001078 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1079 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1080 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001081 } else {
1082 /* Copy the IP header */
1083 skb_set_network_header(skb, skb->len);
1084 skb_put(skb, ihl);
1085 skb_copy_to_linear_data(skb, pkt->data, ihl);
1086 /* Flag to the kernel this is a route add */
1087 ip_hdr(skb)->protocol = 0;
1088 msg = (struct igmpmsg *)skb_network_header(skb);
1089 msg->im_vif = vifi;
1090 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1091 /* Add our header */
Johannes Berg4df864c2017-06-16 14:29:21 +02001092 igmp = skb_put(skb, sizeof(struct igmphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001093 igmp->type = assert;
1094 msg->im_msgtype = assert;
1095 igmp->code = 0;
1096 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1097 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Eric Dumazet4c9687092010-10-01 16:15:01 +00001100 rcu_read_lock();
1101 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001102 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001103 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 kfree_skb(skb);
1105 return -EINVAL;
1106 }
1107
Julien Gomes5a645dd2017-06-20 13:54:17 -07001108 igmpmsg_netlink_event(mrt, skb);
1109
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001110 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001111 ret = sock_queue_rcv_skb(mroute_sk, skb);
1112 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001113 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001114 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 kfree_skb(skb);
1116 }
1117
1118 return ret;
1119}
1120
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001121/* Queue a packet for resolution. It gets locked cache entry! */
1122static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001123 struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001125 const struct iphdr *iph = ip_hdr(skb);
1126 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001127 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001131 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001132 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001133 c->mfc_origin == iph->saddr) {
1134 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 }
1138
Patrick McHardy862465f2010-04-13 05:03:21 +00001139 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001140 /* Create a new entry if allowable */
Hangbin Liu0079ad82019-09-06 15:36:01 +08001141 c = ipmr_cache_alloc_unres();
1142 if (!c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 spin_unlock_bh(&mfc_unres_lock);
1144
1145 kfree_skb(skb);
1146 return -ENOBUFS;
1147 }
1148
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001149 /* Fill in the new cache entry */
Yuval Mintz494fff52018-02-28 23:29:34 +02001150 c->_c.mfc_parent = -1;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001151 c->mfc_origin = iph->saddr;
1152 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001154 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001155 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Yuval Mintz494fff52018-02-28 23:29:34 +02001156
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001157 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001158 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 out - Brad Parker
1160 */
1161 spin_unlock_bh(&mfc_unres_lock);
1162
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001163 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 kfree_skb(skb);
1165 return err;
1166 }
1167
Patrick McHardy0c122952010-04-13 05:03:22 +00001168 atomic_inc(&mrt->cache_resolve_queue_len);
Yuval Mintz494fff52018-02-28 23:29:34 +02001169 list_add(&c->_c.list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001170 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
David S. Miller278554b2010-05-12 00:05:35 -07001172 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
Yuval Mintz494fff52018-02-28 23:29:34 +02001173 mod_timer(&mrt->ipmr_expire_timer,
1174 c->_c.mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 }
1176
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001177 /* See if we can append the packet */
Yuval Mintz494fff52018-02-28 23:29:34 +02001178 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 kfree_skb(skb);
1180 err = -ENOBUFS;
1181 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001182 if (dev) {
1183 skb->dev = dev;
1184 skb->skb_iif = dev->ifindex;
1185 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001186 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 err = 0;
1188 }
1189
1190 spin_unlock_bh(&mfc_unres_lock);
1191 return err;
1192}
1193
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001194/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001196static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Yotam Gigib3620532017-09-27 08:23:14 +02001198 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001199 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001201 /* The entries are added/deleted only under RTNL */
1202 rcu_read_lock();
1203 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1204 mfc->mfcc_mcastgrp.s_addr, parent);
1205 rcu_read_unlock();
1206 if (!c)
1207 return -ENOENT;
Yuval Mintz494fff52018-02-28 23:29:34 +02001208 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1209 list_del_rcu(&c->_c.list);
Yotam Gigib3620532017-09-27 08:23:14 +02001210 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001211 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yuval Mintz8c13af22018-03-26 15:01:36 +03001212 mr_cache_put(&c->_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001214 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Patrick McHardy0c122952010-04-13 05:03:22 +00001217static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001218 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219{
Patrick McHardy862465f2010-04-13 05:03:21 +00001220 struct mfc_cache *uc, *c;
Yuval Mintz494fff52018-02-28 23:29:34 +02001221 struct mr_mfc *_uc;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001222 bool found;
1223 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Patrick McHardya50436f22010-03-17 06:04:14 +00001225 if (mfc->mfcc_parent >= MAXVIFS)
1226 return -ENFILE;
1227
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001228 /* The entries are added/deleted only under RTNL */
1229 rcu_read_lock();
1230 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1231 mfc->mfcc_mcastgrp.s_addr, parent);
1232 rcu_read_unlock();
1233 if (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 write_lock_bh(&mrt_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001235 c->_c.mfc_parent = mfc->mfcc_parent;
1236 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001238 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +02001240 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1241 mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001242 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return 0;
1244 }
1245
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001246 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001247 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 return -EINVAL;
1249
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001250 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001251 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return -ENOMEM;
1253
Jianjun Kongc354e122008-11-03 00:28:02 -08001254 c->mfc_origin = mfc->mfcc_origin.s_addr;
1255 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
Yuval Mintz494fff52018-02-28 23:29:34 +02001256 c->_c.mfc_parent = mfc->mfcc_parent;
1257 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001259 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
Yuval Mintz494fff52018-02-28 23:29:34 +02001261 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001262 ipmr_rht_params);
1263 if (ret) {
1264 pr_err("ipmr: rhtable insert error %d\n", ret);
1265 ipmr_cache_free(c);
1266 return ret;
1267 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001268 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001269 /* Check to see if we resolved a queued list. If so we
1270 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001272 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001274 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1275 uc = (struct mfc_cache *)_uc;
Patrick McHardye258beb2010-04-13 05:03:19 +00001276 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001278 list_del(&_uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001279 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001280 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 break;
1282 }
1283 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001284 if (list_empty(&mrt->mfc_unres_queue))
1285 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286 spin_unlock_bh(&mfc_unres_lock);
1287
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001288 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001289 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001290 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 }
Yotam Gigib3620532017-09-27 08:23:14 +02001292 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001293 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 return 0;
1295}
1296
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001297/* Close the multicast socket, and clear the vif tables etc */
Callum Sinclairca8d4792019-02-18 10:07:52 +13001298static void mroute_clean_tables(struct mr_table *mrt, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299{
Yotam Gigib3620532017-09-27 08:23:14 +02001300 struct net *net = read_pnet(&mrt->net);
Yuval Mintz494fff52018-02-28 23:29:34 +02001301 struct mr_mfc *c, *tmp;
1302 struct mfc_cache *cache;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001303 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001304 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001305
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001306 /* Shut down all active vif entries */
Callum Sinclairca8d4792019-02-18 10:07:52 +13001307 if (flags & (MRT_FLUSH_VIFS | MRT_FLUSH_VIFS_STATIC)) {
1308 for (i = 0; i < mrt->maxvif; i++) {
1309 if (((mrt->vif_table[i].flags & VIFF_STATIC) &&
1310 !(flags & MRT_FLUSH_VIFS_STATIC)) ||
1311 (!(mrt->vif_table[i].flags & VIFF_STATIC) && !(flags & MRT_FLUSH_VIFS)))
1312 continue;
1313 vif_delete(mrt, i, 0, &list);
1314 }
1315 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 }
1317
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001318 /* Wipe the cache */
Callum Sinclairca8d4792019-02-18 10:07:52 +13001319 if (flags & (MRT_FLUSH_MFC | MRT_FLUSH_MFC_STATIC)) {
1320 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1321 if (((c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC_STATIC)) ||
1322 (!(c->mfc_flags & MFC_STATIC) && !(flags & MRT_FLUSH_MFC)))
1323 continue;
1324 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1325 list_del_rcu(&c->list);
1326 cache = (struct mfc_cache *)c;
1327 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
1328 mrt->id);
1329 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1330 mr_cache_put(c);
1331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 }
1333
Callum Sinclairca8d4792019-02-18 10:07:52 +13001334 if (flags & MRT_FLUSH_MFC) {
1335 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
1336 spin_lock_bh(&mfc_unres_lock);
1337 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
1338 list_del(&c->list);
1339 cache = (struct mfc_cache *)c;
1340 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1341 ipmr_destroy_unres(mrt, cache);
1342 }
1343 spin_unlock_bh(&mfc_unres_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 }
1346}
1347
Eric Dumazet4c9687092010-10-01 16:15:01 +00001348/* called from ip_ra_control(), before an RCU grace period,
1349 * we dont need to call synchronize_rcu() here
1350 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351static void mrtsock_destruct(struct sock *sk)
1352{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001353 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001354 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001355
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001356 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001357 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001358 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001359 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001360 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1361 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001362 NETCONFA_IFINDEX_ALL,
1363 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001364 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Callum Sinclairca8d4792019-02-18 10:07:52 +13001365 mroute_clean_tables(mrt, MRT_FLUSH_VIFS | MRT_FLUSH_MFC);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001366 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001368 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369}
1370
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001371/* Socket options and virtual interface manipulation. The whole
1372 * virtual interface system is a complete heap, but unfortunately
1373 * that's how BSD mrouted happens to think. Maybe one day with a proper
1374 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001376
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001377int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1378 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001380 struct net *net = sock_net(sk);
1381 int val, ret = 0, parent = 0;
1382 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 struct vifctl vif;
1384 struct mfcctl mfc;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001385 bool do_wrvifwhole;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001386 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001387
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001388 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1389 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001390 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001391 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1392 ret = -EOPNOTSUPP;
1393 goto out_unlock;
1394 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001395
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001396 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001397 if (!mrt) {
1398 ret = -ENOENT;
1399 goto out_unlock;
1400 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001401 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001402 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001403 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1404 ret = -EACCES;
1405 goto out_unlock;
1406 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 }
1408
Stephen Hemminger132adf52007-03-08 20:44:43 -08001409 switch (optname) {
1410 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001411 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001412 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001413 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001414 }
1415 if (rtnl_dereference(mrt->mroute_sk)) {
1416 ret = -EADDRINUSE;
1417 break;
1418 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001419
1420 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1421 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001422 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001423 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001424 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1425 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001426 NETCONFA_IFINDEX_ALL,
1427 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001428 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001429 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001430 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001431 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1432 ret = -EACCES;
1433 } else {
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001434 /* We need to unlock here because mrtsock_destruct takes
1435 * care of rtnl itself and we can't change that due to
1436 * the IP_ROUTER_ALERT setsockopt which runs without it.
1437 */
1438 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001439 ret = ip_ra_control(sk, 0, NULL);
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001440 goto out;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001441 }
1442 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001443 case MRT_ADD_VIF:
1444 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001445 if (optlen != sizeof(vif)) {
1446 ret = -EINVAL;
1447 break;
1448 }
1449 if (copy_from_user(&vif, optval, sizeof(vif))) {
1450 ret = -EFAULT;
1451 break;
1452 }
1453 if (vif.vifc_vifi >= MAXVIFS) {
1454 ret = -ENFILE;
1455 break;
1456 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001457 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001458 ret = vif_add(net, mrt, &vif,
1459 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001460 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001461 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001462 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001463 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001464 /* Manipulate the forwarding caches. These live
1465 * in a sort of kernel/user symbiosis.
1466 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001467 case MRT_ADD_MFC:
1468 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001469 parent = -1;
Joe Perchesa8eceea2020-03-12 15:50:22 -07001470 fallthrough;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001471 case MRT_ADD_MFC_PROXY:
1472 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001473 if (optlen != sizeof(mfc)) {
1474 ret = -EINVAL;
1475 break;
1476 }
1477 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1478 ret = -EFAULT;
1479 break;
1480 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001481 if (parent == 0)
1482 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001483 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1484 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001485 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001486 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001487 sk == rtnl_dereference(mrt->mroute_sk),
1488 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001489 break;
Callum Sinclairca8d4792019-02-18 10:07:52 +13001490 case MRT_FLUSH:
1491 if (optlen != sizeof(val)) {
1492 ret = -EINVAL;
1493 break;
1494 }
1495 if (get_user(val, (int __user *)optval)) {
1496 ret = -EFAULT;
1497 break;
1498 }
1499 mroute_clean_tables(mrt, val);
1500 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001501 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001502 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001503 if (optlen != sizeof(val)) {
1504 ret = -EINVAL;
1505 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001507 if (get_user(val, (int __user *)optval)) {
1508 ret = -EFAULT;
1509 break;
1510 }
1511 mrt->mroute_do_assert = val;
1512 break;
1513 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001514 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001515 ret = -ENOPROTOOPT;
1516 break;
1517 }
1518 if (optlen != sizeof(val)) {
1519 ret = -EINVAL;
1520 break;
1521 }
1522 if (get_user(val, (int __user *)optval)) {
1523 ret = -EFAULT;
1524 break;
1525 }
1526
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001527 do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001528 val = !!val;
1529 if (val != mrt->mroute_do_pim) {
1530 mrt->mroute_do_pim = val;
1531 mrt->mroute_do_assert = val;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001532 mrt->mroute_do_wrvifwhole = do_wrvifwhole;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001533 }
1534 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001535 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001536 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1537 ret = -ENOPROTOOPT;
1538 break;
1539 }
1540 if (optlen != sizeof(uval)) {
1541 ret = -EINVAL;
1542 break;
1543 }
1544 if (get_user(uval, (u32 __user *)optval)) {
1545 ret = -EFAULT;
1546 break;
1547 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001548
Eric Dumazet4c9687092010-10-01 16:15:01 +00001549 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1550 ret = -EBUSY;
1551 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001552 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001553 if (IS_ERR(mrt))
1554 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001555 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001556 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001557 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001558 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001559 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001560 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001561 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001562 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001563out_unlock:
1564 rtnl_unlock();
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001565out:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001566 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567}
1568
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001569/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001570int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571{
1572 int olr;
1573 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001574 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001575 struct mr_table *mrt;
1576
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001577 if (sk->sk_type != SOCK_RAW ||
1578 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1579 return -EOPNOTSUPP;
1580
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001581 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001582 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001583 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001585 switch (optname) {
1586 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001587 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001588 break;
1589 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001590 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001591 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001592 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001593 break;
1594 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001595 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001596 break;
1597 default:
1598 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001599 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001600
1601 if (get_user(olr, optlen))
1602 return -EFAULT;
1603 olr = min_t(unsigned int, olr, sizeof(int));
1604 if (olr < 0)
1605 return -EINVAL;
1606 if (put_user(olr, optlen))
1607 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001608 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 return -EFAULT;
1610 return 0;
1611}
1612
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001613/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1615{
1616 struct sioc_sg_req sr;
1617 struct sioc_vif_req vr;
1618 struct vif_device *vif;
1619 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001620 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001621 struct mr_table *mrt;
1622
1623 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001624 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001625 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001626
Stephen Hemminger132adf52007-03-08 20:44:43 -08001627 switch (cmd) {
1628 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001629 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001630 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001631 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001632 return -EINVAL;
Gustavo A. R. Silva56484512018-12-10 12:41:24 -06001633 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001634 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001635 vif = &mrt->vif_table[vr.vifi];
1636 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001637 vr.icount = vif->pkt_in;
1638 vr.ocount = vif->pkt_out;
1639 vr.ibytes = vif->bytes_in;
1640 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001642
Jianjun Kongc354e122008-11-03 00:28:02 -08001643 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001645 return 0;
1646 }
1647 read_unlock(&mrt_lock);
1648 return -EADDRNOTAVAIL;
1649 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001650 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001651 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
Eric Dumazeta8c94862010-10-01 16:15:08 +00001653 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001654 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001655 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001656 sr.pktcnt = c->_c.mfc_un.res.pkt;
1657 sr.bytecnt = c->_c.mfc_un.res.bytes;
1658 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001659 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001660
Jianjun Kongc354e122008-11-03 00:28:02 -08001661 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001662 return -EFAULT;
1663 return 0;
1664 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001665 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001666 return -EADDRNOTAVAIL;
1667 default:
1668 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
1670}
1671
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001672#ifdef CONFIG_COMPAT
1673struct compat_sioc_sg_req {
1674 struct in_addr src;
1675 struct in_addr grp;
1676 compat_ulong_t pktcnt;
1677 compat_ulong_t bytecnt;
1678 compat_ulong_t wrong_if;
1679};
1680
David S. Millerca6b8bb02011-02-03 17:24:28 -08001681struct compat_sioc_vif_req {
1682 vifi_t vifi; /* Which iface */
1683 compat_ulong_t icount;
1684 compat_ulong_t ocount;
1685 compat_ulong_t ibytes;
1686 compat_ulong_t obytes;
1687};
1688
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001689int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1690{
David S. Miller0033d5a2011-02-03 17:21:31 -08001691 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001692 struct compat_sioc_vif_req vr;
1693 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001694 struct mfc_cache *c;
1695 struct net *net = sock_net(sk);
1696 struct mr_table *mrt;
1697
1698 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001699 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001700 return -ENOENT;
1701
1702 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001703 case SIOCGETVIFCNT:
1704 if (copy_from_user(&vr, arg, sizeof(vr)))
1705 return -EFAULT;
1706 if (vr.vifi >= mrt->maxvif)
1707 return -EINVAL;
Gustavo A. R. Silva56484512018-12-10 12:41:24 -06001708 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
David S. Millerca6b8bb02011-02-03 17:24:28 -08001709 read_lock(&mrt_lock);
1710 vif = &mrt->vif_table[vr.vifi];
1711 if (VIF_EXISTS(mrt, vr.vifi)) {
1712 vr.icount = vif->pkt_in;
1713 vr.ocount = vif->pkt_out;
1714 vr.ibytes = vif->bytes_in;
1715 vr.obytes = vif->bytes_out;
1716 read_unlock(&mrt_lock);
1717
1718 if (copy_to_user(arg, &vr, sizeof(vr)))
1719 return -EFAULT;
1720 return 0;
1721 }
1722 read_unlock(&mrt_lock);
1723 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001724 case SIOCGETSGCNT:
1725 if (copy_from_user(&sr, arg, sizeof(sr)))
1726 return -EFAULT;
1727
1728 rcu_read_lock();
1729 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1730 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001731 sr.pktcnt = c->_c.mfc_un.res.pkt;
1732 sr.bytecnt = c->_c.mfc_un.res.bytes;
1733 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001734 rcu_read_unlock();
1735
1736 if (copy_to_user(arg, &sr, sizeof(sr)))
1737 return -EFAULT;
1738 return 0;
1739 }
1740 rcu_read_unlock();
1741 return -EADDRNOTAVAIL;
1742 default:
1743 return -ENOIOCTLCMD;
1744 }
1745}
1746#endif
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1749{
Jiri Pirko351638e2013-05-28 01:30:21 +00001750 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001751 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001752 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 struct vif_device *v;
1754 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (event != NETDEV_UNREGISTER)
1757 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001758
1759 ipmr_for_each_table(mrt, net) {
1760 v = &mrt->vif_table[0];
1761 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1762 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001763 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766 return NOTIFY_DONE;
1767}
1768
Jianjun Kongc354e122008-11-03 00:28:02 -08001769static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 .notifier_call = ipmr_device_event,
1771};
1772
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001773/* Encapsulate a packet by attaching a valid IPIP header to it.
1774 * This avoids tunnel drivers and other mess and gives us the speed so
1775 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001777static void ip_encap(struct net *net, struct sk_buff *skb,
1778 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001780 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001781 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001782
1783 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001784 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001785 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001786 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001788 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001789 iph->tos = old_iph->tos;
1790 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 iph->frag_off = 0;
1792 iph->daddr = daddr;
1793 iph->saddr = saddr;
1794 iph->protocol = IPPROTO_IPIP;
1795 iph->ihl = 5;
1796 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001797 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 ip_send_check(iph);
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
Florian Westphal895b5c92019-09-29 20:54:03 +02001801 nf_reset_ct(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802}
1803
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001804static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1805 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001807 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
David S. Miller73186df2015-11-03 13:41:45 -05001809 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1810 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 if (unlikely(opt->optlen))
1813 ip_forward_options(skb);
1814
Eric W. Biederman13206b62015-10-07 16:48:35 -05001815 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
Yotam Gigia5bc9292017-10-03 09:58:08 +02001818#ifdef CONFIG_NET_SWITCHDEV
1819static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1820 int in_vifi, int out_vifi)
1821{
1822 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1823 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1824
Ido Schimmel875e8932018-12-04 08:15:10 +00001825 if (!skb->offload_l3_fwd_mark)
Yotam Gigia5bc9292017-10-03 09:58:08 +02001826 return false;
1827 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1828 return false;
1829 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1830 &in_vif->dev_parent_id);
1831}
1832#else
1833static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1834 int in_vifi, int out_vifi)
1835{
1836 return false;
1837}
1838#endif
1839
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001840/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Patrick McHardy0c122952010-04-13 05:03:22 +00001842static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
David Ahern6e0735d2018-12-17 15:34:48 -08001843 int in_vifi, struct sk_buff *skb, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001845 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001846 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 struct net_device *dev;
1848 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001849 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 int encap = 0;
1851
Ian Morris51456b22015-04-03 09:17:26 +01001852 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 goto out_free;
1854
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 if (vif->flags & VIFF_REGISTER) {
1856 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001857 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001858 vif->dev->stats.tx_bytes += skb->len;
1859 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001860 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001861 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
Yotam Gigia5bc9292017-10-03 09:58:08 +02001864 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1865 goto out_free;
1866
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001867 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001868 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001869 vif->remote, vif->local,
1870 0, 0,
1871 IPPROTO_IPIP,
1872 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001873 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 goto out_free;
1875 encap = sizeof(struct iphdr);
1876 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001877 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001878 0, 0,
1879 IPPROTO_IPIP,
1880 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001881 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 goto out_free;
1883 }
1884
Changli Gaod8d1f302010-06-10 23:31:35 -07001885 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
Changli Gaod8d1f302010-06-10 23:31:35 -07001887 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001889 * allow to send ICMP, so that packets will disappear
1890 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 */
David S. Miller73186df2015-11-03 13:41:45 -05001892 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 ip_rt_put(rt);
1894 goto out_free;
1895 }
1896
Changli Gaod8d1f302010-06-10 23:31:35 -07001897 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
1899 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001900 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 goto out_free;
1902 }
1903
1904 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001905 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Eric Dumazetadf30902009-06-02 05:19:30 +00001907 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001908 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001909 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910
1911 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001912 * What do we do with netfilter? -- RR
1913 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001915 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001917 vif->dev->stats.tx_packets++;
1918 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 }
1920
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001921 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001923 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 * not only before forwarding, but after forwarding on all output
1925 * interfaces. It is clear, if mrouter runs a multicasting
1926 * program, it should receive packets not depending to what interface
1927 * program is joined.
1928 * If we will not make it, the program will have to join on all
1929 * interfaces. On the other hand, multihoming host (or router, but
1930 * not mrouter) cannot join to more than one interface - it will
1931 * result in receiving multiple packets.
1932 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001933 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1934 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 ipmr_forward_finish);
1936 return;
1937
1938out_free:
1939 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940}
1941
Patrick McHardy0c122952010-04-13 05:03:22 +00001942static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943{
1944 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001945
1946 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1947 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 break;
1949 }
1950 return ct;
1951}
1952
1953/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001954static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001955 struct net_device *dev, struct sk_buff *skb,
Yuval Mintz494fff52018-02-28 23:29:34 +02001956 struct mfc_cache *c, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957{
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001958 int true_vifi = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 int psend = -1;
1960 int vif, ct;
1961
Yuval Mintz494fff52018-02-28 23:29:34 +02001962 vif = c->_c.mfc_parent;
1963 c->_c.mfc_un.res.pkt++;
1964 c->_c.mfc_un.res.bytes += skb->len;
1965 c->_c.mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
Yuval Mintz494fff52018-02-28 23:29:34 +02001967 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001968 struct mfc_cache *cache_proxy;
1969
1970 /* For an (*,G) entry, we only check that the incomming
1971 * interface is part of the static tree.
1972 */
Yuval Mintz845c9a72018-02-28 23:29:35 +02001973 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001974 if (cache_proxy &&
Yuval Mintz494fff52018-02-28 23:29:34 +02001975 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001976 goto forward;
1977 }
1978
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001979 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001980 if (mrt->vif_table[vif].dev != dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001981 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001983 * Very complicated situation...
1984 *
1985 * The best workaround until routing daemons will be
1986 * fixed is not to redistribute packet, if it was
1987 * send through wrong interface. It means, that
1988 * multicast applications WILL NOT work for
1989 * (S,G), which have default multicast route pointing
1990 * to wrong oif. In any case, it is not a good
1991 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 */
1993 goto dont_forward;
1994 }
1995
Yuval Mintz494fff52018-02-28 23:29:34 +02001996 c->_c.mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Patrick McHardy0c122952010-04-13 05:03:22 +00001998 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002000 * so that we cannot check that packet arrived on an oif.
2001 * It is bad, but otherwise we would need to move pretty
2002 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 */
Patrick McHardy0c122952010-04-13 05:03:22 +00002004 (mrt->mroute_do_pim ||
Yuval Mintz494fff52018-02-28 23:29:34 +02002005 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002006 time_after(jiffies,
Yuval Mintz494fff52018-02-28 23:29:34 +02002007 c->_c.mfc_un.res.last_assert +
2008 MFC_ASSERT_THRESH)) {
2009 c->_c.mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00002010 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03002011 if (mrt->mroute_do_wrvifwhole)
2012 ipmr_cache_report(mrt, skb, true_vifi,
2013 IGMPMSG_WRVIFWHOLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 }
2015 goto dont_forward;
2016 }
2017
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002018forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00002019 mrt->vif_table[vif].pkt_in++;
2020 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002022 /* Forward the frame */
Yuval Mintz494fff52018-02-28 23:29:34 +02002023 if (c->mfc_origin == htonl(INADDR_ANY) &&
2024 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002025 if (true_vifi >= 0 &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002026 true_vifi != c->_c.mfc_parent &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002027 ip_hdr(skb)->ttl >
Yuval Mintz494fff52018-02-28 23:29:34 +02002028 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002029 /* It's an (*,*) entry and the packet is not coming from
2030 * the upstream: forward the packet to the upstream
2031 * only.
2032 */
Yuval Mintz494fff52018-02-28 23:29:34 +02002033 psend = c->_c.mfc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002034 goto last_forward;
2035 }
2036 goto dont_forward;
2037 }
Yuval Mintz494fff52018-02-28 23:29:34 +02002038 for (ct = c->_c.mfc_un.res.maxvif - 1;
2039 ct >= c->_c.mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002040 /* For (*,G) entry, don't forward to the incoming interface */
Yuval Mintz494fff52018-02-28 23:29:34 +02002041 if ((c->mfc_origin != htonl(INADDR_ANY) ||
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002042 ct != true_vifi) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002043 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (psend != -1) {
2045 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002048 ipmr_queue_xmit(net, mrt, true_vifi,
David Ahern6e0735d2018-12-17 15:34:48 -08002049 skb2, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002051 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
2053 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002054last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 if (psend != -1) {
2056 if (local) {
2057 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002060 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
David Ahern6e0735d2018-12-17 15:34:48 -08002061 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 } else {
David Ahern6e0735d2018-12-17 15:34:48 -08002063 ipmr_queue_xmit(net, mrt, true_vifi, skb, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03002064 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 }
2066 }
2067
2068dont_forward:
2069 if (!local)
2070 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071}
2072
David S. Miller417da662011-05-03 19:42:43 -07002073static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08002074{
David S. Miller417da662011-05-03 19:42:43 -07002075 struct rtable *rt = skb_rtable(skb);
2076 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05002077 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07002078 .daddr = iph->daddr,
2079 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00002080 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07002081 .flowi4_oif = (rt_is_output_route(rt) ?
2082 skb->dev->ifindex : 0),
2083 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002084 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07002085 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00002086 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08002087 };
2088 struct mr_table *mrt;
2089 int err;
2090
David S. Millerda919812011-03-12 02:04:50 -05002091 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002092 if (err)
2093 return ERR_PTR(err);
2094 return mrt;
2095}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002097/* Multicast packets for forwarding arrive here
2098 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100int ip_mr_input(struct sk_buff *skb)
2101{
2102 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002103 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00002104 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002105 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002106 struct net_device *dev;
2107
2108 /* skb->dev passed in is the loX master dev for vrfs.
2109 * As there are no vifs associated with loopback devices,
2110 * get the proper interface that does have a vif associated with it.
2111 */
2112 dev = skb->dev;
2113 if (netif_is_l3_master(skb->dev)) {
2114 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2115 if (!dev) {
2116 kfree_skb(skb);
2117 return -ENODEV;
2118 }
2119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120
2121 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002122 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00002124 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 goto dont_forward;
2126
David S. Miller417da662011-05-03 19:42:43 -07002127 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002128 if (IS_ERR(mrt)) {
2129 kfree_skb(skb);
2130 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00002133 if (IPCB(skb)->opt.router_alert) {
2134 if (ip_call_ra_chain(skb))
2135 return 0;
2136 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2137 /* IGMPv1 (and broken IGMPv2 implementations sort of
2138 * Cisco IOS <= 11.2(8)) do not put router alert
2139 * option to IGMP packets destined to routable
2140 * groups. It is very bad, because it means
2141 * that we can forward NO IGMP messages.
2142 */
2143 struct sock *mroute_sk;
2144
2145 mroute_sk = rcu_dereference(mrt->mroute_sk);
2146 if (mroute_sk) {
Florian Westphal895b5c92019-09-29 20:54:03 +02002147 nf_reset_ct(skb);
Eric Dumazet4c9687092010-10-01 16:15:01 +00002148 raw_rcv(mroute_sk, skb);
2149 return 0;
2150 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 }
2152 }
2153
Eric Dumazeta8c94862010-10-01 16:15:08 +00002154 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002155 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002156 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002157 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002158
2159 if (vif >= 0)
2160 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2161 vif);
2162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002164 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002165 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 int vif;
2167
2168 if (local) {
2169 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2170 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002171 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 skb = skb2;
2174 }
2175
Eric Dumazeta8c94862010-10-01 16:15:08 +00002176 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002177 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 if (vif >= 0) {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002179 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 read_unlock(&mrt_lock);
2181
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002182 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 }
2184 read_unlock(&mrt_lock);
2185 kfree_skb(skb);
2186 return -ENODEV;
2187 }
2188
Eric Dumazeta8c94862010-10-01 16:15:08 +00002189 read_lock(&mrt_lock);
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002190 ip_mr_forward(net, mrt, dev, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 read_unlock(&mrt_lock);
2192
2193 if (local)
2194 return ip_local_deliver(skb);
2195
2196 return 0;
2197
2198dont_forward:
2199 if (local)
2200 return ip_local_deliver(skb);
2201 kfree_skb(skb);
2202 return 0;
2203}
2204
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002205#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002206/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002207int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002208{
2209 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002210 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002211 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002212
2213 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2214 goto drop;
2215
2216 pim = igmp_hdr(skb);
2217
David S. Miller417da662011-05-03 19:42:43 -07002218 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002219 if (IS_ERR(mrt))
2220 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002221 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002222 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2223 goto drop;
2224
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
2233#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002234static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235{
2236 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002237 struct net *net = dev_net(skb->dev);
2238 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002240 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 goto drop;
2242
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002243 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002244 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002245 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002246 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002247 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 goto drop;
2249
David S. Miller417da662011-05-03 19:42:43 -07002250 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002251 if (IS_ERR(mrt))
2252 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002253 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002254drop:
2255 kfree_skb(skb);
2256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return 0;
2258}
2259#endif
2260
David S. Miller9a1b9492011-05-04 12:18:54 -07002261int ipmr_get_route(struct net *net, struct sk_buff *skb,
2262 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002263 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002266 struct mr_table *mrt;
2267 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002269 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002270 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002271 return -ENOENT;
2272
Eric Dumazeta8c94862010-10-01 16:15:08 +00002273 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002274 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002275 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002276 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002277
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002278 if (vif >= 0)
2279 cache = ipmr_cache_find_any(mrt, daddr, vif);
2280 }
Ian Morris51456b22015-04-03 09:17:26 +01002281 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002282 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002283 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002285 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002286
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002288 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002289 if (dev)
2290 vif = ipmr_find_vif(mrt, dev);
2291 if (vif < 0) {
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 -ENODEV;
2295 }
Guillaume Nault7901cd92019-11-15 18:29:52 +01002296
2297 skb2 = skb_realloc_headroom(skb, sizeof(struct iphdr));
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002298 if (!skb2) {
2299 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002300 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002301 return -ENOMEM;
2302 }
2303
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002304 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002305 skb_push(skb2, sizeof(struct iphdr));
2306 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002307 iph = ip_hdr(skb2);
2308 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002309 iph->saddr = saddr;
2310 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002311 iph->version = 0;
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002312 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002314 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 return err;
2316 }
2317
Eric Dumazeta8c94862010-10-01 16:15:08 +00002318 read_lock(&mrt_lock);
Yuval Mintz7b0db852018-02-28 23:29:39 +02002319 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002321 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return err;
2323}
2324
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002325static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002326 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2327 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002328{
2329 struct nlmsghdr *nlh;
2330 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002331 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002332
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002333 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002334 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002335 return -EMSGSIZE;
2336
2337 rtm = nlmsg_data(nlh);
2338 rtm->rtm_family = RTNL_FAMILY_IPMR;
2339 rtm->rtm_dst_len = 32;
2340 rtm->rtm_src_len = 32;
2341 rtm->rtm_tos = 0;
2342 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002343 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2344 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002345 rtm->rtm_type = RTN_MULTICAST;
2346 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Yuval Mintz494fff52018-02-28 23:29:34 +02002347 if (c->_c.mfc_flags & MFC_STATIC)
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002348 rtm->rtm_protocol = RTPROT_STATIC;
2349 else
2350 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002351 rtm->rtm_flags = 0;
2352
Jiri Benc930345e2015-03-29 16:59:25 +02002353 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2354 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002355 goto nla_put_failure;
Yuval Mintz7b0db852018-02-28 23:29:39 +02002356 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002357 /* do not break the dump if cache is unresolved */
2358 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002359 goto nla_put_failure;
2360
Johannes Berg053c0952015-01-16 22:09:00 +01002361 nlmsg_end(skb, nlh);
2362 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002363
2364nla_put_failure:
2365 nlmsg_cancel(skb, nlh);
2366 return -EMSGSIZE;
2367}
2368
Yuval Mintz7b0db852018-02-28 23:29:39 +02002369static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2370 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2371 int flags)
2372{
2373 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2374 cmd, flags);
2375}
2376
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002377static size_t mroute_msgsize(bool unresolved, int maxvif)
2378{
2379 size_t len =
2380 NLMSG_ALIGN(sizeof(struct rtmsg))
2381 + nla_total_size(4) /* RTA_TABLE */
2382 + nla_total_size(4) /* RTA_SRC */
2383 + nla_total_size(4) /* RTA_DST */
2384 ;
2385
2386 if (!unresolved)
2387 len = len
2388 + nla_total_size(4) /* RTA_IIF */
2389 + nla_total_size(0) /* RTA_MULTIPATH */
2390 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2391 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002392 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002393 ;
2394
2395 return len;
2396}
2397
2398static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2399 int cmd)
2400{
2401 struct net *net = read_pnet(&mrt->net);
2402 struct sk_buff *skb;
2403 int err = -ENOBUFS;
2404
Yuval Mintz494fff52018-02-28 23:29:34 +02002405 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2406 mrt->maxvif),
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002407 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002408 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002409 goto errout;
2410
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002411 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002412 if (err < 0)
2413 goto errout;
2414
2415 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2416 return;
2417
2418errout:
2419 kfree_skb(skb);
2420 if (err < 0)
2421 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2422}
2423
Julien Gomes5a645dd2017-06-20 13:54:17 -07002424static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2425{
2426 size_t len =
2427 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2428 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2429 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2430 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2431 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2432 /* IPMRA_CREPORT_PKT */
2433 + nla_total_size(payloadlen)
2434 ;
2435
2436 return len;
2437}
2438
2439static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2440{
2441 struct net *net = read_pnet(&mrt->net);
2442 struct nlmsghdr *nlh;
2443 struct rtgenmsg *rtgenm;
2444 struct igmpmsg *msg;
2445 struct sk_buff *skb;
2446 struct nlattr *nla;
2447 int payloadlen;
2448
2449 payloadlen = pkt->len - sizeof(struct igmpmsg);
2450 msg = (struct igmpmsg *)skb_network_header(pkt);
2451
2452 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2453 if (!skb)
2454 goto errout;
2455
2456 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2457 sizeof(struct rtgenmsg), 0);
2458 if (!nlh)
2459 goto errout;
2460 rtgenm = nlmsg_data(nlh);
2461 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2462 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2463 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2464 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2465 msg->im_src.s_addr) ||
2466 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2467 msg->im_dst.s_addr))
2468 goto nla_put_failure;
2469
2470 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2471 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2472 nla_data(nla), payloadlen))
2473 goto nla_put_failure;
2474
2475 nlmsg_end(skb, nlh);
2476
2477 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2478 return;
2479
2480nla_put_failure:
2481 nlmsg_cancel(skb, nlh);
2482errout:
2483 kfree_skb(skb);
2484 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2485}
2486
Jakub Kicinskid0440022019-01-18 10:46:20 -08002487static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb,
2488 const struct nlmsghdr *nlh,
2489 struct nlattr **tb,
2490 struct netlink_ext_ack *extack)
2491{
2492 struct rtmsg *rtm;
2493 int i, err;
2494
2495 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
2496 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for multicast route get request");
2497 return -EINVAL;
2498 }
2499
2500 if (!netlink_strict_get_check(skb))
Johannes Berg8cb08172019-04-26 14:07:28 +02002501 return nlmsg_parse_deprecated(nlh, sizeof(*rtm), tb, RTA_MAX,
2502 rtm_ipv4_policy, extack);
Jakub Kicinskid0440022019-01-18 10:46:20 -08002503
2504 rtm = nlmsg_data(nlh);
2505 if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
2506 (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
2507 rtm->rtm_tos || rtm->rtm_table || rtm->rtm_protocol ||
2508 rtm->rtm_scope || rtm->rtm_type || rtm->rtm_flags) {
2509 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for multicast route get request");
2510 return -EINVAL;
2511 }
2512
Johannes Berg8cb08172019-04-26 14:07:28 +02002513 err = nlmsg_parse_deprecated_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
2514 rtm_ipv4_policy, extack);
Jakub Kicinskid0440022019-01-18 10:46:20 -08002515 if (err)
2516 return err;
2517
2518 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
2519 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
2520 NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
2521 return -EINVAL;
2522 }
2523
2524 for (i = 0; i <= RTA_MAX; i++) {
2525 if (!tb[i])
2526 continue;
2527
2528 switch (i) {
2529 case RTA_SRC:
2530 case RTA_DST:
2531 case RTA_TABLE:
2532 break;
2533 default:
2534 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in multicast route get request");
2535 return -EINVAL;
2536 }
2537 }
2538
2539 return 0;
2540}
2541
Donald Sharp4f75ba62017-06-28 13:58:57 -04002542static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2543 struct netlink_ext_ack *extack)
2544{
2545 struct net *net = sock_net(in_skb->sk);
2546 struct nlattr *tb[RTA_MAX + 1];
2547 struct sk_buff *skb = NULL;
2548 struct mfc_cache *cache;
2549 struct mr_table *mrt;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002550 __be32 src, grp;
2551 u32 tableid;
2552 int err;
2553
Jakub Kicinskid0440022019-01-18 10:46:20 -08002554 err = ipmr_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Donald Sharp4f75ba62017-06-28 13:58:57 -04002555 if (err < 0)
2556 goto errout;
2557
Donald Sharp4f75ba62017-06-28 13:58:57 -04002558 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2559 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2560 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2561
2562 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
Dan Carpenter2e3d2322017-07-12 10:56:47 +03002563 if (!mrt) {
2564 err = -ENOENT;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002565 goto errout_free;
2566 }
2567
2568 /* entries are added/deleted only under RTNL */
2569 rcu_read_lock();
2570 cache = ipmr_cache_find(mrt, src, grp);
2571 rcu_read_unlock();
2572 if (!cache) {
2573 err = -ENOENT;
2574 goto errout_free;
2575 }
2576
2577 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2578 if (!skb) {
2579 err = -ENOBUFS;
2580 goto errout_free;
2581 }
2582
2583 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2584 nlh->nlmsg_seq, cache,
2585 RTM_NEWROUTE, 0);
2586 if (err < 0)
2587 goto errout_free;
2588
2589 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2590
2591errout:
2592 return err;
2593
2594errout_free:
2595 kfree_skb(skb);
2596 goto errout;
2597}
2598
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002599static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2600{
David Ahern47246762018-10-15 18:56:42 -07002601 struct fib_dump_filter filter = {};
David Aherncb167892018-10-15 18:56:47 -07002602 int err;
David Aherne8ba3302018-10-07 20:16:35 -07002603
David Ahern47246762018-10-15 18:56:42 -07002604 if (cb->strict_check) {
David Ahern47246762018-10-15 18:56:42 -07002605 err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh,
David Aherneffe6792018-10-15 18:56:48 -07002606 &filter, cb);
David Aherne8ba3302018-10-07 20:16:35 -07002607 if (err < 0)
2608 return err;
2609 }
2610
David Aherncb167892018-10-15 18:56:47 -07002611 if (filter.table_id) {
2612 struct mr_table *mrt;
2613
2614 mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id);
2615 if (!mrt) {
Sabrina Dubroca41b4bd92020-05-20 11:15:46 +02002616 if (rtnl_msg_family(cb->nlh) != RTNL_FAMILY_IPMR)
David Ahernae677bb2018-10-24 12:59:01 -07002617 return skb->len;
2618
David Aherncb167892018-10-15 18:56:47 -07002619 NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist");
2620 return -ENOENT;
2621 }
2622 err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute,
2623 &mfc_unres_lock, &filter);
2624 return skb->len ? : err;
2625 }
2626
Yuval Mintz7b0db852018-02-28 23:29:39 +02002627 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
David Aherncb167892018-10-15 18:56:47 -07002628 _ipmr_fill_mroute, &mfc_unres_lock, &filter);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002629}
2630
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002631static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2632 [RTA_SRC] = { .type = NLA_U32 },
2633 [RTA_DST] = { .type = NLA_U32 },
2634 [RTA_IIF] = { .type = NLA_U32 },
2635 [RTA_TABLE] = { .type = NLA_U32 },
2636 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2637};
2638
2639static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2640{
2641 switch (rtm_protocol) {
2642 case RTPROT_STATIC:
2643 case RTPROT_MROUTED:
2644 return true;
2645 }
2646 return false;
2647}
2648
2649static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2650{
2651 struct rtnexthop *rtnh = nla_data(nla);
2652 int remaining = nla_len(nla), vifi = 0;
2653
2654 while (rtnh_ok(rtnh, remaining)) {
2655 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2656 if (++vifi == MAXVIFS)
2657 break;
2658 rtnh = rtnh_next(rtnh, &remaining);
2659 }
2660
2661 return remaining > 0 ? -EINVAL : vifi;
2662}
2663
2664/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2665static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2666 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002667 struct mr_table **mrtret,
2668 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002669{
2670 struct net_device *dev = NULL;
2671 u32 tblid = RT_TABLE_DEFAULT;
2672 struct mr_table *mrt;
2673 struct nlattr *attr;
2674 struct rtmsg *rtm;
2675 int ret, rem;
2676
Johannes Berg8cb08172019-04-26 14:07:28 +02002677 ret = nlmsg_validate_deprecated(nlh, sizeof(*rtm), RTA_MAX,
2678 rtm_ipmr_policy, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002679 if (ret < 0)
2680 goto out;
2681 rtm = nlmsg_data(nlh);
2682
2683 ret = -EINVAL;
2684 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2685 rtm->rtm_type != RTN_MULTICAST ||
2686 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2687 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2688 goto out;
2689
2690 memset(mfcc, 0, sizeof(*mfcc));
2691 mfcc->mfcc_parent = -1;
2692 ret = 0;
2693 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2694 switch (nla_type(attr)) {
2695 case RTA_SRC:
2696 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2697 break;
2698 case RTA_DST:
2699 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2700 break;
2701 case RTA_IIF:
2702 dev = __dev_get_by_index(net, nla_get_u32(attr));
2703 if (!dev) {
2704 ret = -ENODEV;
2705 goto out;
2706 }
2707 break;
2708 case RTA_MULTIPATH:
2709 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2710 ret = -EINVAL;
2711 goto out;
2712 }
2713 break;
2714 case RTA_PREFSRC:
2715 ret = 1;
2716 break;
2717 case RTA_TABLE:
2718 tblid = nla_get_u32(attr);
2719 break;
2720 }
2721 }
2722 mrt = ipmr_get_table(net, tblid);
2723 if (!mrt) {
2724 ret = -ENOENT;
2725 goto out;
2726 }
2727 *mrtret = mrt;
2728 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2729 if (dev)
2730 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2731
2732out:
2733 return ret;
2734}
2735
2736/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002737static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2738 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002739{
2740 struct net *net = sock_net(skb->sk);
2741 int ret, mrtsock, parent;
2742 struct mr_table *tbl;
2743 struct mfcctl mfcc;
2744
2745 mrtsock = 0;
2746 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002747 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002748 if (ret < 0)
2749 return ret;
2750
2751 parent = ret ? mfcc.mfcc_parent : -1;
2752 if (nlh->nlmsg_type == RTM_NEWROUTE)
2753 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2754 else
2755 return ipmr_mfc_delete(tbl, &mfcc, parent);
2756}
2757
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002758static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2759{
2760 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2761
2762 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2763 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2764 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2765 mrt->mroute_reg_vif_num) ||
2766 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2767 mrt->mroute_do_assert) ||
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03002768 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
2769 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
2770 mrt->mroute_do_wrvifwhole))
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002771 return false;
2772
2773 return true;
2774}
2775
2776static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2777{
2778 struct nlattr *vif_nest;
2779 struct vif_device *vif;
2780
2781 /* if the VIF doesn't exist just continue */
2782 if (!VIF_EXISTS(mrt, vifid))
2783 return true;
2784
2785 vif = &mrt->vif_table[vifid];
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002786 vif_nest = nla_nest_start_noflag(skb, IPMRA_VIF);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002787 if (!vif_nest)
2788 return false;
2789 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2790 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2791 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2792 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2793 IPMRA_VIFA_PAD) ||
2794 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2795 IPMRA_VIFA_PAD) ||
2796 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2797 IPMRA_VIFA_PAD) ||
2798 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2799 IPMRA_VIFA_PAD) ||
2800 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2801 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2802 nla_nest_cancel(skb, vif_nest);
2803 return false;
2804 }
2805 nla_nest_end(skb, vif_nest);
2806
2807 return true;
2808}
2809
David Ahern14fc5bb2018-10-07 20:16:34 -07002810static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
2811 struct netlink_ext_ack *extack)
2812{
2813 struct ifinfomsg *ifm;
2814
2815 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2816 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
2817 return -EINVAL;
2818 }
2819
2820 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
2821 NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump");
2822 return -EINVAL;
2823 }
2824
2825 ifm = nlmsg_data(nlh);
2826 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2827 ifm->ifi_change || ifm->ifi_index) {
2828 NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
2829 return -EINVAL;
2830 }
2831
2832 return 0;
2833}
2834
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002835static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2836{
2837 struct net *net = sock_net(skb->sk);
2838 struct nlmsghdr *nlh = NULL;
2839 unsigned int t = 0, s_t;
2840 unsigned int e = 0, s_e;
2841 struct mr_table *mrt;
2842
David Ahern14fc5bb2018-10-07 20:16:34 -07002843 if (cb->strict_check) {
2844 int err = ipmr_valid_dumplink(cb->nlh, cb->extack);
2845
2846 if (err < 0)
2847 return err;
2848 }
2849
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002850 s_t = cb->args[0];
2851 s_e = cb->args[1];
2852
2853 ipmr_for_each_table(mrt, net) {
2854 struct nlattr *vifs, *af;
2855 struct ifinfomsg *hdr;
2856 u32 i;
2857
2858 if (t < s_t)
2859 goto skip_table;
2860 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2861 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2862 sizeof(*hdr), NLM_F_MULTI);
2863 if (!nlh)
2864 break;
2865
2866 hdr = nlmsg_data(nlh);
2867 memset(hdr, 0, sizeof(*hdr));
2868 hdr->ifi_family = RTNL_FAMILY_IPMR;
2869
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002870 af = nla_nest_start_noflag(skb, IFLA_AF_SPEC);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002871 if (!af) {
2872 nlmsg_cancel(skb, nlh);
2873 goto out;
2874 }
2875
2876 if (!ipmr_fill_table(mrt, skb)) {
2877 nlmsg_cancel(skb, nlh);
2878 goto out;
2879 }
2880
Michal Kubecekae0be8d2019-04-26 11:13:06 +02002881 vifs = nla_nest_start_noflag(skb, IPMRA_TABLE_VIFS);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002882 if (!vifs) {
2883 nla_nest_end(skb, af);
2884 nlmsg_end(skb, nlh);
2885 goto out;
2886 }
2887 for (i = 0; i < mrt->maxvif; i++) {
2888 if (e < s_e)
2889 goto skip_entry;
2890 if (!ipmr_fill_vif(mrt, i, skb)) {
2891 nla_nest_end(skb, vifs);
2892 nla_nest_end(skb, af);
2893 nlmsg_end(skb, nlh);
2894 goto out;
2895 }
2896skip_entry:
2897 e++;
2898 }
2899 s_e = 0;
2900 e = 0;
2901 nla_nest_end(skb, vifs);
2902 nla_nest_end(skb, af);
2903 nlmsg_end(skb, nlh);
2904skip_table:
2905 t++;
2906 }
2907
2908out:
2909 cb->args[1] = e;
2910 cb->args[0] = t;
2911
2912 return skb->len;
2913}
2914
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002915#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002916/* The /proc interfaces to multicast routing :
2917 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002919
2920static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002921 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002923 struct mr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002924 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002925 struct mr_table *mrt;
2926
2927 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002928 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002929 return ERR_PTR(-ENOENT);
2930
2931 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 read_lock(&mrt_lock);
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002934 return mr_vif_seq_start(seq, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935}
2936
2937static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002938 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939{
2940 read_unlock(&mrt_lock);
2941}
2942
2943static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2944{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002945 struct mr_vif_iter *iter = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002946 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002947
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002949 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2951 } else {
2952 const struct vif_device *vif = v;
Yuval Mintz6853f212018-02-28 23:29:29 +02002953 const char *name = vif->dev ?
2954 vif->dev->name : "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956 seq_printf(seq,
James Hogan91e6dd82018-01-30 09:48:02 +00002957 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002958 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002959 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960 vif->bytes_out, vif->pkt_out,
2961 vif->flags, vif->local, vif->remote);
2962 }
2963 return 0;
2964}
2965
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002966static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 .start = ipmr_vif_seq_start,
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002968 .next = mr_vif_seq_next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 .stop = ipmr_vif_seq_stop,
2970 .show = ipmr_vif_seq_show,
2971};
2972
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2974{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002975 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002976 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002977
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002978 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002979 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002980 return ERR_PTR(-ENOENT);
2981
Yuval Mintzc8d61962018-02-28 23:29:36 +02002982 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983}
2984
2985static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2986{
2987 int n;
2988
2989 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002990 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2992 } else {
2993 const struct mfc_cache *mfc = v;
Yuval Mintzc8d61962018-02-28 23:29:36 +02002994 const struct mr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002995 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002996
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002997 seq_printf(seq, "%08X %08X %-3hd",
2998 (__force u32) mfc->mfc_mcastgrp,
2999 (__force u32) mfc->mfc_origin,
Yuval Mintz494fff52018-02-28 23:29:34 +02003000 mfc->_c.mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001
Patrick McHardy0c122952010-04-13 05:03:22 +00003002 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003003 seq_printf(seq, " %8lu %8lu %8lu",
Yuval Mintz494fff52018-02-28 23:29:34 +02003004 mfc->_c.mfc_un.res.pkt,
3005 mfc->_c.mfc_un.res.bytes,
3006 mfc->_c.mfc_un.res.wrong_if);
3007 for (n = mfc->_c.mfc_un.res.minvif;
3008 n < mfc->_c.mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00003009 if (VIF_EXISTS(mrt, n) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02003010 mfc->_c.mfc_un.res.ttls[n] < 255)
Benjamin Therycf958ae32009-01-22 04:56:16 +00003011 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003012 " %2d:%-3d",
Yuval Mintz494fff52018-02-28 23:29:34 +02003013 n, mfc->_c.mfc_un.res.ttls[n]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003014 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003015 } else {
3016 /* unresolved mfc_caches don't contain
3017 * pkt, bytes and wrong_if values
3018 */
3019 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 }
3021 seq_putc(seq, '\n');
3022 }
3023 return 0;
3024}
3025
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003026static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 .start = ipmr_mfc_seq_start,
Yuval Mintzc8d61962018-02-28 23:29:36 +02003028 .next = mr_mfc_seq_next,
3029 .stop = mr_mfc_seq_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 .show = ipmr_mfc_seq_show,
3031};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003032#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003033
3034#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00003035static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07003037 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038};
3039#endif
3040
Yotam Gigi4d65b942017-09-27 08:23:13 +02003041static unsigned int ipmr_seq_read(struct net *net)
3042{
3043 ASSERT_RTNL();
3044
3045 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
3046}
3047
Jiri Pirkob7a59552019-10-03 11:49:30 +02003048static int ipmr_dump(struct net *net, struct notifier_block *nb,
3049 struct netlink_ext_ack *extack)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003050{
Yuval Mintzcdc9f942018-03-26 15:01:33 +03003051 return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
Jiri Pirkob7a59552019-10-03 11:49:30 +02003052 ipmr_mr_table_iter, &mrt_lock, extack);
Yotam Gigi4d65b942017-09-27 08:23:13 +02003053}
3054
3055static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3056 .family = RTNL_FAMILY_IPMR,
3057 .fib_seq_read = ipmr_seq_read,
3058 .fib_dump = ipmr_dump,
3059 .owner = THIS_MODULE,
3060};
3061
Colin Ian Kingef739d82017-09-29 14:34:22 +01003062static int __net_init ipmr_notifier_init(struct net *net)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003063{
3064 struct fib_notifier_ops *ops;
3065
3066 net->ipv4.ipmr_seq = 0;
3067
3068 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3069 if (IS_ERR(ops))
3070 return PTR_ERR(ops);
3071 net->ipv4.ipmr_notifier_ops = ops;
3072
3073 return 0;
3074}
3075
3076static void __net_exit ipmr_notifier_exit(struct net *net)
3077{
3078 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3079 net->ipv4.ipmr_notifier_ops = NULL;
3080}
3081
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01003082/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00003083static int __net_init ipmr_net_init(struct net *net)
3084{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003085 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00003086
Yotam Gigi4d65b942017-09-27 08:23:13 +02003087 err = ipmr_notifier_init(net);
3088 if (err)
3089 goto ipmr_notifier_fail;
3090
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003091 err = ipmr_rules_init(net);
3092 if (err < 0)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003093 goto ipmr_rules_fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003094
3095#ifdef CONFIG_PROC_FS
3096 err = -ENOMEM;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003097 if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
3098 sizeof(struct mr_vif_iter)))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003099 goto proc_vif_fail;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003100 if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
3101 sizeof(struct mr_mfc_iter)))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003102 goto proc_cache_fail;
3103#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00003104 return 0;
3105
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003106#ifdef CONFIG_PROC_FS
3107proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00003108 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003109proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003110 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003111#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003112ipmr_rules_fail:
3113 ipmr_notifier_exit(net);
3114ipmr_notifier_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003115 return err;
3116}
3117
3118static void __net_exit ipmr_net_exit(struct net *net)
3119{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003120#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003121 remove_proc_entry("ip_mr_cache", net->proc_net);
3122 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003123#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003124 ipmr_notifier_exit(net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003125 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00003126}
3127
3128static struct pernet_operations ipmr_net_ops = {
3129 .init = ipmr_net_init,
3130 .exit = ipmr_net_exit,
3131};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003132
Wang Chen03d2f892008-07-03 12:13:36 +08003133int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Wang Chen03d2f892008-07-03 12:13:36 +08003135 int err;
3136
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3138 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00003139 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003140 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08003141
Benjamin Therycf958ae32009-01-22 04:56:16 +00003142 err = register_pernet_subsys(&ipmr_net_ops);
3143 if (err)
3144 goto reg_pernet_fail;
3145
Wang Chen03d2f892008-07-03 12:13:36 +08003146 err = register_netdevice_notifier(&ip_mr_notifier);
3147 if (err)
3148 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07003149#ifdef CONFIG_IP_PIMSM_V2
3150 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00003151 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07003152 err = -EAGAIN;
3153 goto add_proto_fail;
3154 }
3155#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00003156 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003157 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003158 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003159 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003160 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003161 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03003162
3163 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
Florian Westphalb97bac62017-08-09 20:41:48 +02003164 NULL, ipmr_rtm_dumplink, 0);
Wang Chen03d2f892008-07-03 12:13:36 +08003165 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003166
Tom Goff403dbb92009-06-14 03:16:13 -07003167#ifdef CONFIG_IP_PIMSM_V2
3168add_proto_fail:
3169 unregister_netdevice_notifier(&ip_mr_notifier);
3170#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08003171reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003172 unregister_pernet_subsys(&ipmr_net_ops);
3173reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08003174 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08003175 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176}