blob: a6defbec4f1b4338993756ea7b8681012450aa1a [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Linux Consultancy and Custom Driver Development
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fixes:
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
22 * overflow.
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
Gilles Espinassef77f13e2010-03-29 15:41:47 +020025 * Relax this requirement to work with older peers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 */
28
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
Alexey Dobriyan08009a72018-02-24 21:20:33 +030031#include <linux/cache.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080032#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/mm.h>
35#include <linux/kernel.h>
36#include <linux/fcntl.h>
37#include <linux/stat.h>
38#include <linux/socket.h>
39#include <linux/in.h>
40#include <linux/inet.h>
41#include <linux/netdevice.h>
42#include <linux/inetdevice.h>
43#include <linux/igmp.h>
44#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
46#include <linux/mroute.h>
47#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080048#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020050#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/ip.h>
52#include <net/protocol.h>
53#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020054#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/icmp.h>
56#include <net/udp.h>
57#include <net/raw.h>
58#include <linux/notifier.h>
59#include <linux/if_arp.h>
60#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000061#include <linux/compat.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040062#include <linux/export.h>
NeilBrown0eb71a92018-06-18 12:52:50 +100063#include <linux/rhashtable.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000064#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070066#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000067#include <net/fib_rules.h>
Nicolas Dichteld67b8c62012-12-04 01:13:35 +000068#include <linux/netconf.h>
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +010069#include <net/nexthop.h>
Yotam Gigi5d8b3e62017-10-03 09:58:07 +020070#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Patrick McHardyf0ad0862010-04-13 05:03:23 +000072struct ipmr_rule {
73 struct fib_rule common;
74};
75
76struct ipmr_result {
77 struct mr_table *mrt;
78};
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000081 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
83
84static DEFINE_RWLOCK(mrt_lock);
85
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +010086/* Multicast router control variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* Special spinlock for queue of unresolved entries */
89static DEFINE_SPINLOCK(mfc_unres_lock);
90
91/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000092 * entries is changed only in process context and protected
93 * with weak lock mrt_lock. Queue of unresolved entries is protected
94 * with strong spinlock mfc_unres_lock.
95 *
96 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98
Alexey Dobriyan08009a72018-02-24 21:20:33 +030099static struct kmem_cache *mrt_cachep __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000101static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000102static void ipmr_free_table(struct mr_table *mrt);
103
Rami Rosenc4854ec2013-07-20 15:09:28 +0300104static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -0400105 struct net_device *dev, struct sk_buff *skb,
106 struct mfc_cache *cache, int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000107static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000108 struct sk_buff *pkt, vifi_t vifi, int assert);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000109static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
110 int cmd);
Julien Gomes5a645dd2017-06-20 13:54:17 -0700111static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100112static void mroute_clean_tables(struct mr_table *mrt, bool all);
Kees Cooke99e88a2017-10-16 14:43:17 -0700113static void ipmr_expire_process(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000115#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
116#define ipmr_for_each_table(mrt, net) \
117 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
118
Yuval Mintz7b0db852018-02-28 23:29:39 +0200119static struct mr_table *ipmr_mr_table_iter(struct net *net,
120 struct mr_table *mrt)
121{
122 struct mr_table *ret;
123
124 if (!mrt)
125 ret = list_entry_rcu(net->ipv4.mr_tables.next,
126 struct mr_table, list);
127 else
128 ret = list_entry_rcu(mrt->list.next,
129 struct mr_table, list);
130
131 if (&ret->list == &net->ipv4.mr_tables)
132 return NULL;
133 return ret;
134}
135
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000136static struct mr_table *ipmr_get_table(struct net *net, u32 id)
137{
138 struct mr_table *mrt;
139
140 ipmr_for_each_table(mrt, net) {
141 if (mrt->id == id)
142 return mrt;
143 }
144 return NULL;
145}
146
David S. Millerda919812011-03-12 02:04:50 -0500147static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000148 struct mr_table **mrt)
149{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000150 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100151 struct ipmr_result res;
152 struct fib_lookup_arg arg = {
153 .result = &res,
154 .flags = FIB_LOOKUP_NOREF,
155 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000156
David Aherne58e4152016-10-31 15:54:00 -0700157 /* update flow if oif or iif point to device enslaved to l3mdev */
158 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
159
David S. Millerda919812011-03-12 02:04:50 -0500160 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
161 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000162 if (err < 0)
163 return err;
164 *mrt = res.mrt;
165 return 0;
166}
167
168static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
169 int flags, struct fib_lookup_arg *arg)
170{
171 struct ipmr_result *res = arg->result;
172 struct mr_table *mrt;
173
174 switch (rule->action) {
175 case FR_ACT_TO_TBL:
176 break;
177 case FR_ACT_UNREACHABLE:
178 return -ENETUNREACH;
179 case FR_ACT_PROHIBIT:
180 return -EACCES;
181 case FR_ACT_BLACKHOLE:
182 default:
183 return -EINVAL;
184 }
185
David Aherne58e4152016-10-31 15:54:00 -0700186 arg->table = fib_rule_get_table(rule, arg);
187
188 mrt = ipmr_get_table(rule->fr_net, arg->table);
Ian Morris51456b22015-04-03 09:17:26 +0100189 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000190 return -EAGAIN;
191 res->mrt = mrt;
192 return 0;
193}
194
195static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
196{
197 return 1;
198}
199
200static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
201 FRA_GENERIC_POLICY,
202};
203
204static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
Roopa Prabhub16fb412018-04-21 09:41:31 -0700205 struct fib_rule_hdr *frh, struct nlattr **tb,
206 struct netlink_ext_ack *extack)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000207{
208 return 0;
209}
210
211static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
212 struct nlattr **tb)
213{
214 return 1;
215}
216
217static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
218 struct fib_rule_hdr *frh)
219{
220 frh->dst_len = 0;
221 frh->src_len = 0;
222 frh->tos = 0;
223 return 0;
224}
225
Andi Kleen04a6f822012-10-04 17:12:11 -0700226static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200227 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000228 .rule_size = sizeof(struct ipmr_rule),
229 .addr_size = sizeof(u32),
230 .action = ipmr_rule_action,
231 .match = ipmr_rule_match,
232 .configure = ipmr_rule_configure,
233 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000234 .fill = ipmr_rule_fill,
235 .nlgroup = RTNLGRP_IPV4_RULE,
236 .policy = ipmr_rule_policy,
237 .owner = THIS_MODULE,
238};
239
240static int __net_init ipmr_rules_init(struct net *net)
241{
242 struct fib_rules_ops *ops;
243 struct mr_table *mrt;
244 int err;
245
246 ops = fib_rules_register(&ipmr_rules_ops_template, net);
247 if (IS_ERR(ops))
248 return PTR_ERR(ops);
249
250 INIT_LIST_HEAD(&net->ipv4.mr_tables);
251
252 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100253 if (IS_ERR(mrt)) {
254 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000255 goto err1;
256 }
257
258 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
259 if (err < 0)
260 goto err2;
261
262 net->ipv4.mr_rules_ops = ops;
263 return 0;
264
265err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700266 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000267err1:
268 fib_rules_unregister(ops);
269 return err;
270}
271
272static void __net_exit ipmr_rules_exit(struct net *net)
273{
274 struct mr_table *mrt, *next;
275
WANG Conged785302015-03-31 11:01:45 -0700276 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000277 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
278 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000279 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000280 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000281 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700282 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000283}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200284
285static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
286{
287 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR);
288}
289
290static unsigned int ipmr_rules_seq_read(struct net *net)
291{
292 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
293}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200294
295bool ipmr_rule_default(const struct fib_rule *rule)
296{
297 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
298}
299EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000300#else
301#define ipmr_for_each_table(mrt, net) \
302 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
303
Yuval Mintz7b0db852018-02-28 23:29:39 +0200304static struct mr_table *ipmr_mr_table_iter(struct net *net,
305 struct mr_table *mrt)
306{
307 if (!mrt)
308 return net->ipv4.mrt;
309 return NULL;
310}
311
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000312static struct mr_table *ipmr_get_table(struct net *net, u32 id)
313{
314 return net->ipv4.mrt;
315}
316
David S. Millerda919812011-03-12 02:04:50 -0500317static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000318 struct mr_table **mrt)
319{
320 *mrt = net->ipv4.mrt;
321 return 0;
322}
323
324static int __net_init ipmr_rules_init(struct net *net)
325{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100326 struct mr_table *mrt;
327
328 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
329 if (IS_ERR(mrt))
330 return PTR_ERR(mrt);
331 net->ipv4.mrt = mrt;
332 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000333}
334
335static void __net_exit ipmr_rules_exit(struct net *net)
336{
WANG Conged785302015-03-31 11:01:45 -0700337 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000338 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700339 net->ipv4.mrt = NULL;
340 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000341}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200342
343static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
344{
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,
375 .locks_mul = 1,
376 .obj_cmpfn = ipmr_hash_cmp,
377 .automatic_shrinking = true,
378};
379
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200380static void ipmr_new_table_set(struct mr_table *mrt,
381 struct net *net)
382{
383#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
384 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
385#endif
386}
387
Yuval Mintz845c9a72018-02-28 23:29:35 +0200388static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
389 .mfc_mcastgrp = htonl(INADDR_ANY),
390 .mfc_origin = htonl(INADDR_ANY),
391};
392
393static struct mr_table_ops ipmr_mr_table_ops = {
394 .rht_params = &ipmr_rht_params,
395 .cmparg_any = &ipmr_mr_table_ops_cmparg_any,
396};
397
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000398static struct mr_table *ipmr_new_table(struct net *net, u32 id)
399{
400 struct mr_table *mrt;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000401
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100402 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
403 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
404 return ERR_PTR(-EINVAL);
405
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000406 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100407 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000408 return mrt;
409
Yuval Mintz845c9a72018-02-28 23:29:35 +0200410 return mr_table_alloc(net, id, &ipmr_mr_table_ops,
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200411 ipmr_expire_process, ipmr_new_table_set);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000412}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000414static void ipmr_free_table(struct mr_table *mrt)
415{
416 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100417 mroute_clean_tables(mrt, true);
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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (dev_open(dev))
510 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (dev_open(dev))
593 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{
838 int vifi = vifc->vifc_vifi;
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200839 struct switchdev_attr attr = {
840 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
841 };
Patrick McHardy0c122952010-04-13 05:03:22 +0000842 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct net_device *dev;
844 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700845 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000848 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return -EADDRINUSE;
850
851 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100853 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100854 return -EINVAL;
855 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 * All the packets will be sent to the daemon
857 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000858 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000860 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 if (!dev)
862 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700863 err = dev_set_allmulti(dev, 1);
864 if (err) {
865 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700866 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700867 return err;
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900870 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000871 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (!dev)
873 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700874 err = dev_set_allmulti(dev, 1);
875 if (err) {
876 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700877 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700878 return err;
879 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000881 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000883 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
884 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100885 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000886 dev_put(dev);
887 return -EADDRNOTAVAIL;
888 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000889 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000890 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (!dev)
893 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700894 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700895 if (err) {
896 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700897 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700898 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 break;
900 default:
901 return -EINVAL;
902 }
903
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000904 in_dev = __in_dev_get_rtnl(dev);
905 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000906 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000908 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700909 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700910 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
911 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 ip_rt_multicast_event(in_dev);
913
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000914 /* Fill in the VIF structures */
Yuval Mintz6853f212018-02-28 23:29:29 +0200915 vif_device_init(v, dev, vifc->vifc_rate_limit,
916 vifc->vifc_threshold,
917 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
918 (VIFF_TUNNEL | VIFF_REGISTER));
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000919
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200920 attr.orig_dev = dev;
921 if (!switchdev_port_attr_get(dev, &attr)) {
922 memcpy(v->dev_parent_id.id, attr.u.ppid.id, attr.u.ppid.id_len);
923 v->dev_parent_id.id_len = attr.u.ppid.id_len;
924 } else {
925 v->dev_parent_id.id_len = 0;
926 }
Yuval Mintz6853f212018-02-28 23:29:29 +0200927
Jianjun Kongc354e122008-11-03 00:28:02 -0800928 v->local = vifc->vifc_lcl_addr.s_addr;
929 v->remote = vifc->vifc_rmt_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
931 /* And finish update writing critical data */
932 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800933 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000934 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000935 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000936 if (vifi+1 > mrt->maxvif)
937 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +0200939 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return 0;
941}
942
Eric Dumazeta8c94862010-10-01 16:15:08 +0000943/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000944static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000945 __be32 origin,
946 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100948 struct mfc_cache_cmp_arg arg = {
949 .mfc_mcastgrp = mcastgrp,
950 .mfc_origin = origin
951 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
Yuval Mintz845c9a72018-02-28 23:29:35 +0200953 return mr_mfc_find(mrt, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000954}
955
956/* Look for a (*,G) entry */
957static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
958 __be32 mcastgrp, int vifi)
959{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100960 struct mfc_cache_cmp_arg arg = {
961 .mfc_mcastgrp = mcastgrp,
962 .mfc_origin = htonl(INADDR_ANY)
963 };
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000964
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100965 if (mcastgrp == htonl(INADDR_ANY))
Yuval Mintz845c9a72018-02-28 23:29:35 +0200966 return mr_mfc_find_any_parent(mrt, vifi);
967 return mr_mfc_find_any(mrt, vifi, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000968}
969
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100970/* Look for a (S,G,iif) entry if parent != -1 */
971static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
972 __be32 origin, __be32 mcastgrp,
973 int parent)
974{
975 struct mfc_cache_cmp_arg arg = {
976 .mfc_mcastgrp = mcastgrp,
977 .mfc_origin = origin,
978 };
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100979
Yuval Mintz845c9a72018-02-28 23:29:35 +0200980 return mr_mfc_find_parent(mrt, &arg, parent);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100981}
982
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100983/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000984static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985{
Jianjun Kongc354e122008-11-03 00:28:02 -0800986 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000987
Tom Goff70a0dec2016-06-23 16:11:57 -0400988 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +0200989 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
990 c->_c.mfc_un.res.minvif = MAXVIFS;
Yuval Mintz8c13af22018-03-26 15:01:36 +0300991 c->_c.free = ipmr_cache_free_rcu;
Yuval Mintz494fff52018-02-28 23:29:34 +0200992 refcount_set(&c->_c.mfc_un.res.refcount, 1);
Tom Goff70a0dec2016-06-23 16:11:57 -0400993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return c;
995}
996
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000997static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998{
Jianjun Kongc354e122008-11-03 00:28:02 -0800999 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001000
1001 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001002 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
1003 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 return c;
1006}
1007
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001008/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +00001009static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1010 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
1012 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001013 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001015 /* Play the pending entries through our router */
Yuval Mintz494fff52018-02-28 23:29:34 +02001016 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001017 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +02001018 struct nlmsghdr *nlh = skb_pull(skb,
1019 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
Yuval Mintz7b0db852018-02-28 23:29:39 +02001021 if (mr_fill_mroute(mrt, skb, &c->_c,
1022 nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001023 nlh->nlmsg_len = skb_tail_pointer(skb) -
1024 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 } else {
1026 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +00001027 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +00001029 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001030 e->error = -EMSGSIZE;
1031 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
Thomas Graf2942e902006-08-15 00:30:25 -07001033
Eric W. Biederman15e47302012-09-07 20:12:54 +00001034 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001035 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001036 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 }
1039}
1040
Julien Gomes5a645dd2017-06-20 13:54:17 -07001041/* Bounce a cache query up to mrouted and netlink.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001043 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001045static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001046 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001048 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001049 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 struct igmphdr *igmp;
1051 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001052 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 int ret;
1054
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001055 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1057 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 skb = alloc_skb(128, GFP_ATOMIC);
1059
Stephen Hemminger132adf52007-03-08 20:44:43 -08001060 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return -ENOBUFS;
1062
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001063 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001065 * Duplicate old header, fix ihl, length etc.
1066 * And all this only to mangle msg->im_msgtype and
1067 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001069 skb_push(skb, sizeof(struct iphdr));
1070 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001071 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001072 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001073 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001074 msg->im_msgtype = assert;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 msg->im_mbz = 0;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001076 if (assert == IGMPMSG_WRVIFWHOLE)
1077 msg->im_vif = vifi;
1078 else
1079 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001080 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1081 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1082 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001083 } else {
1084 /* Copy the IP header */
1085 skb_set_network_header(skb, skb->len);
1086 skb_put(skb, ihl);
1087 skb_copy_to_linear_data(skb, pkt->data, ihl);
1088 /* Flag to the kernel this is a route add */
1089 ip_hdr(skb)->protocol = 0;
1090 msg = (struct igmpmsg *)skb_network_header(skb);
1091 msg->im_vif = vifi;
1092 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1093 /* Add our header */
Johannes Berg4df864c2017-06-16 14:29:21 +02001094 igmp = skb_put(skb, sizeof(struct igmphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001095 igmp->type = assert;
1096 msg->im_msgtype = assert;
1097 igmp->code = 0;
1098 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1099 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
Eric Dumazet4c9687092010-10-01 16:15:01 +00001102 rcu_read_lock();
1103 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001104 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001105 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 kfree_skb(skb);
1107 return -EINVAL;
1108 }
1109
Julien Gomes5a645dd2017-06-20 13:54:17 -07001110 igmpmsg_netlink_event(mrt, skb);
1111
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001112 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001113 ret = sock_queue_rcv_skb(mroute_sk, skb);
1114 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001115 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001116 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 kfree_skb(skb);
1118 }
1119
1120 return ret;
1121}
1122
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001123/* Queue a packet for resolution. It gets locked cache entry! */
1124static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001125 struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001127 const struct iphdr *iph = ip_hdr(skb);
1128 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001129 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
1132 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001133 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001134 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001135 c->mfc_origin == iph->saddr) {
1136 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001138 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140
Patrick McHardy862465f2010-04-13 05:03:21 +00001141 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001142 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001143 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001144 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 spin_unlock_bh(&mfc_unres_lock);
1146
1147 kfree_skb(skb);
1148 return -ENOBUFS;
1149 }
1150
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001151 /* Fill in the new cache entry */
Yuval Mintz494fff52018-02-28 23:29:34 +02001152 c->_c.mfc_parent = -1;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001153 c->mfc_origin = iph->saddr;
1154 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001156 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001157 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Yuval Mintz494fff52018-02-28 23:29:34 +02001158
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001159 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001160 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 out - Brad Parker
1162 */
1163 spin_unlock_bh(&mfc_unres_lock);
1164
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001165 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 kfree_skb(skb);
1167 return err;
1168 }
1169
Patrick McHardy0c122952010-04-13 05:03:22 +00001170 atomic_inc(&mrt->cache_resolve_queue_len);
Yuval Mintz494fff52018-02-28 23:29:34 +02001171 list_add(&c->_c.list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001172 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
David S. Miller278554b2010-05-12 00:05:35 -07001174 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
Yuval Mintz494fff52018-02-28 23:29:34 +02001175 mod_timer(&mrt->ipmr_expire_timer,
1176 c->_c.mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 }
1178
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001179 /* See if we can append the packet */
Yuval Mintz494fff52018-02-28 23:29:34 +02001180 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 kfree_skb(skb);
1182 err = -ENOBUFS;
1183 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001184 if (dev) {
1185 skb->dev = dev;
1186 skb->skb_iif = dev->ifindex;
1187 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001188 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 err = 0;
1190 }
1191
1192 spin_unlock_bh(&mfc_unres_lock);
1193 return err;
1194}
1195
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001196/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001198static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199{
Yotam Gigib3620532017-09-27 08:23:14 +02001200 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001201 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001203 /* The entries are added/deleted only under RTNL */
1204 rcu_read_lock();
1205 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1206 mfc->mfcc_mcastgrp.s_addr, parent);
1207 rcu_read_unlock();
1208 if (!c)
1209 return -ENOENT;
Yuval Mintz494fff52018-02-28 23:29:34 +02001210 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1211 list_del_rcu(&c->_c.list);
Yotam Gigib3620532017-09-27 08:23:14 +02001212 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001213 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yuval Mintz8c13af22018-03-26 15:01:36 +03001214 mr_cache_put(&c->_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217}
1218
Patrick McHardy0c122952010-04-13 05:03:22 +00001219static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001220 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
Patrick McHardy862465f2010-04-13 05:03:21 +00001222 struct mfc_cache *uc, *c;
Yuval Mintz494fff52018-02-28 23:29:34 +02001223 struct mr_mfc *_uc;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001224 bool found;
1225 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
Patrick McHardya50436f22010-03-17 06:04:14 +00001227 if (mfc->mfcc_parent >= MAXVIFS)
1228 return -ENFILE;
1229
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001230 /* The entries are added/deleted only under RTNL */
1231 rcu_read_lock();
1232 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1233 mfc->mfcc_mcastgrp.s_addr, parent);
1234 rcu_read_unlock();
1235 if (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 write_lock_bh(&mrt_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001237 c->_c.mfc_parent = mfc->mfcc_parent;
1238 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001240 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +02001242 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1243 mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001244 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 return 0;
1246 }
1247
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001248 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001249 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 return -EINVAL;
1251
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001252 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001253 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 return -ENOMEM;
1255
Jianjun Kongc354e122008-11-03 00:28:02 -08001256 c->mfc_origin = mfc->mfcc_origin.s_addr;
1257 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
Yuval Mintz494fff52018-02-28 23:29:34 +02001258 c->_c.mfc_parent = mfc->mfcc_parent;
1259 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001261 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262
Yuval Mintz494fff52018-02-28 23:29:34 +02001263 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001264 ipmr_rht_params);
1265 if (ret) {
1266 pr_err("ipmr: rhtable insert error %d\n", ret);
1267 ipmr_cache_free(c);
1268 return ret;
1269 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001270 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001271 /* Check to see if we resolved a queued list. If so we
1272 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001274 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001276 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1277 uc = (struct mfc_cache *)_uc;
Patrick McHardye258beb2010-04-13 05:03:19 +00001278 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001280 list_del(&_uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001281 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001282 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 break;
1284 }
1285 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001286 if (list_empty(&mrt->mfc_unres_queue))
1287 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 spin_unlock_bh(&mfc_unres_lock);
1289
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001290 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001291 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001292 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 }
Yotam Gigib3620532017-09-27 08:23:14 +02001294 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001295 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 return 0;
1297}
1298
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001299/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001300static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Yotam Gigib3620532017-09-27 08:23:14 +02001302 struct net *net = read_pnet(&mrt->net);
Yuval Mintz494fff52018-02-28 23:29:34 +02001303 struct mr_mfc *c, *tmp;
1304 struct mfc_cache *cache;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001305 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001306 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001307
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001308 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001309 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001310 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1311 continue;
1312 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001314 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001316 /* Wipe the cache */
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001317 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1318 if (!all && (c->mfc_flags & MFC_STATIC))
1319 continue;
1320 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1321 list_del_rcu(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +02001322 cache = (struct mfc_cache *)c;
1323 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
Yotam Gigib3620532017-09-27 08:23:14 +02001324 mrt->id);
Yuval Mintz494fff52018-02-28 23:29:34 +02001325 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
Yuval Mintz8c13af22018-03-26 15:01:36 +03001326 mr_cache_put(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
1328
Patrick McHardy0c122952010-04-13 05:03:22 +00001329 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 spin_lock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001331 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001332 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +02001333 cache = (struct mfc_cache *)c;
1334 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1335 ipmr_destroy_unres(mrt, cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 }
1337 spin_unlock_bh(&mfc_unres_lock);
1338 }
1339}
1340
Eric Dumazet4c9687092010-10-01 16:15:01 +00001341/* called from ip_ra_control(), before an RCU grace period,
1342 * we dont need to call synchronize_rcu() here
1343 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344static void mrtsock_destruct(struct sock *sk)
1345{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001346 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001347 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001348
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001349 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001350 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001351 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001352 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001353 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1354 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001355 NETCONFA_IFINDEX_ALL,
1356 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001357 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001358 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 }
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001361 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362}
1363
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001364/* Socket options and virtual interface manipulation. The whole
1365 * virtual interface system is a complete heap, but unfortunately
1366 * that's how BSD mrouted happens to think. Maybe one day with a proper
1367 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001369
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001370int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1371 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001373 struct net *net = sock_net(sk);
1374 int val, ret = 0, parent = 0;
1375 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376 struct vifctl vif;
1377 struct mfcctl mfc;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001378 bool do_wrvifwhole;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001379 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001380
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001381 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1382 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001383 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001384 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1385 ret = -EOPNOTSUPP;
1386 goto out_unlock;
1387 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001388
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001389 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001390 if (!mrt) {
1391 ret = -ENOENT;
1392 goto out_unlock;
1393 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001394 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001395 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001396 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1397 ret = -EACCES;
1398 goto out_unlock;
1399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 }
1401
Stephen Hemminger132adf52007-03-08 20:44:43 -08001402 switch (optname) {
1403 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001404 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001405 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001406 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001407 }
1408 if (rtnl_dereference(mrt->mroute_sk)) {
1409 ret = -EADDRINUSE;
1410 break;
1411 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001412
1413 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1414 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001415 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001416 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001417 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1418 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001419 NETCONFA_IFINDEX_ALL,
1420 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001421 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001422 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001423 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001424 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1425 ret = -EACCES;
1426 } else {
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001427 /* We need to unlock here because mrtsock_destruct takes
1428 * care of rtnl itself and we can't change that due to
1429 * the IP_ROUTER_ALERT setsockopt which runs without it.
1430 */
1431 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001432 ret = ip_ra_control(sk, 0, NULL);
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001433 goto out;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001434 }
1435 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001436 case MRT_ADD_VIF:
1437 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001438 if (optlen != sizeof(vif)) {
1439 ret = -EINVAL;
1440 break;
1441 }
1442 if (copy_from_user(&vif, optval, sizeof(vif))) {
1443 ret = -EFAULT;
1444 break;
1445 }
1446 if (vif.vifc_vifi >= MAXVIFS) {
1447 ret = -ENFILE;
1448 break;
1449 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001450 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001451 ret = vif_add(net, mrt, &vif,
1452 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001453 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001454 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001455 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001456 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001457 /* Manipulate the forwarding caches. These live
1458 * in a sort of kernel/user symbiosis.
1459 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001460 case MRT_ADD_MFC:
1461 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001462 parent = -1;
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05001463 /* fall through */
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001464 case MRT_ADD_MFC_PROXY:
1465 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001466 if (optlen != sizeof(mfc)) {
1467 ret = -EINVAL;
1468 break;
1469 }
1470 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1471 ret = -EFAULT;
1472 break;
1473 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001474 if (parent == 0)
1475 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001476 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1477 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001478 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001479 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001480 sk == rtnl_dereference(mrt->mroute_sk),
1481 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001482 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001483 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001484 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001485 if (optlen != sizeof(val)) {
1486 ret = -EINVAL;
1487 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001489 if (get_user(val, (int __user *)optval)) {
1490 ret = -EFAULT;
1491 break;
1492 }
1493 mrt->mroute_do_assert = val;
1494 break;
1495 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001496 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001497 ret = -ENOPROTOOPT;
1498 break;
1499 }
1500 if (optlen != sizeof(val)) {
1501 ret = -EINVAL;
1502 break;
1503 }
1504 if (get_user(val, (int __user *)optval)) {
1505 ret = -EFAULT;
1506 break;
1507 }
1508
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001509 do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001510 val = !!val;
1511 if (val != mrt->mroute_do_pim) {
1512 mrt->mroute_do_pim = val;
1513 mrt->mroute_do_assert = val;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001514 mrt->mroute_do_wrvifwhole = do_wrvifwhole;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001515 }
1516 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001517 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001518 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1519 ret = -ENOPROTOOPT;
1520 break;
1521 }
1522 if (optlen != sizeof(uval)) {
1523 ret = -EINVAL;
1524 break;
1525 }
1526 if (get_user(uval, (u32 __user *)optval)) {
1527 ret = -EFAULT;
1528 break;
1529 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001530
Eric Dumazet4c9687092010-10-01 16:15:01 +00001531 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1532 ret = -EBUSY;
1533 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001534 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001535 if (IS_ERR(mrt))
1536 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001537 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001538 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001539 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001540 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001541 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001542 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001543 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001545out_unlock:
1546 rtnl_unlock();
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001547out:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001548 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549}
1550
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001551/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001552int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553{
1554 int olr;
1555 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001556 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001557 struct mr_table *mrt;
1558
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001559 if (sk->sk_type != SOCK_RAW ||
1560 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1561 return -EOPNOTSUPP;
1562
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001563 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001564 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001565 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001567 switch (optname) {
1568 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001569 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001570 break;
1571 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001572 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001573 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001574 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001575 break;
1576 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001577 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001578 break;
1579 default:
1580 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001581 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001582
1583 if (get_user(olr, optlen))
1584 return -EFAULT;
1585 olr = min_t(unsigned int, olr, sizeof(int));
1586 if (olr < 0)
1587 return -EINVAL;
1588 if (put_user(olr, optlen))
1589 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001590 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 return -EFAULT;
1592 return 0;
1593}
1594
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001595/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1597{
1598 struct sioc_sg_req sr;
1599 struct sioc_vif_req vr;
1600 struct vif_device *vif;
1601 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001602 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001603 struct mr_table *mrt;
1604
1605 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001606 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001607 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001608
Stephen Hemminger132adf52007-03-08 20:44:43 -08001609 switch (cmd) {
1610 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001611 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001612 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001613 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001614 return -EINVAL;
1615 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001616 vif = &mrt->vif_table[vr.vifi];
1617 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001618 vr.icount = vif->pkt_in;
1619 vr.ocount = vif->pkt_out;
1620 vr.ibytes = vif->bytes_in;
1621 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001623
Jianjun Kongc354e122008-11-03 00:28:02 -08001624 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001626 return 0;
1627 }
1628 read_unlock(&mrt_lock);
1629 return -EADDRNOTAVAIL;
1630 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001631 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001632 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Eric Dumazeta8c94862010-10-01 16:15:08 +00001634 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001635 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001636 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001637 sr.pktcnt = c->_c.mfc_un.res.pkt;
1638 sr.bytecnt = c->_c.mfc_un.res.bytes;
1639 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001640 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001641
Jianjun Kongc354e122008-11-03 00:28:02 -08001642 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001643 return -EFAULT;
1644 return 0;
1645 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001646 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001647 return -EADDRNOTAVAIL;
1648 default:
1649 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 }
1651}
1652
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001653#ifdef CONFIG_COMPAT
1654struct compat_sioc_sg_req {
1655 struct in_addr src;
1656 struct in_addr grp;
1657 compat_ulong_t pktcnt;
1658 compat_ulong_t bytecnt;
1659 compat_ulong_t wrong_if;
1660};
1661
David S. Millerca6b8bb02011-02-03 17:24:28 -08001662struct compat_sioc_vif_req {
1663 vifi_t vifi; /* Which iface */
1664 compat_ulong_t icount;
1665 compat_ulong_t ocount;
1666 compat_ulong_t ibytes;
1667 compat_ulong_t obytes;
1668};
1669
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001670int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1671{
David S. Miller0033d5a2011-02-03 17:21:31 -08001672 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001673 struct compat_sioc_vif_req vr;
1674 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001675 struct mfc_cache *c;
1676 struct net *net = sock_net(sk);
1677 struct mr_table *mrt;
1678
1679 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001680 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001681 return -ENOENT;
1682
1683 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001684 case SIOCGETVIFCNT:
1685 if (copy_from_user(&vr, arg, sizeof(vr)))
1686 return -EFAULT;
1687 if (vr.vifi >= mrt->maxvif)
1688 return -EINVAL;
1689 read_lock(&mrt_lock);
1690 vif = &mrt->vif_table[vr.vifi];
1691 if (VIF_EXISTS(mrt, vr.vifi)) {
1692 vr.icount = vif->pkt_in;
1693 vr.ocount = vif->pkt_out;
1694 vr.ibytes = vif->bytes_in;
1695 vr.obytes = vif->bytes_out;
1696 read_unlock(&mrt_lock);
1697
1698 if (copy_to_user(arg, &vr, sizeof(vr)))
1699 return -EFAULT;
1700 return 0;
1701 }
1702 read_unlock(&mrt_lock);
1703 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001704 case SIOCGETSGCNT:
1705 if (copy_from_user(&sr, arg, sizeof(sr)))
1706 return -EFAULT;
1707
1708 rcu_read_lock();
1709 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1710 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001711 sr.pktcnt = c->_c.mfc_un.res.pkt;
1712 sr.bytecnt = c->_c.mfc_un.res.bytes;
1713 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001714 rcu_read_unlock();
1715
1716 if (copy_to_user(arg, &sr, sizeof(sr)))
1717 return -EFAULT;
1718 return 0;
1719 }
1720 rcu_read_unlock();
1721 return -EADDRNOTAVAIL;
1722 default:
1723 return -ENOIOCTLCMD;
1724 }
1725}
1726#endif
1727
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1729{
Jiri Pirko351638e2013-05-28 01:30:21 +00001730 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001731 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001732 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 struct vif_device *v;
1734 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001735
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 if (event != NETDEV_UNREGISTER)
1737 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001738
1739 ipmr_for_each_table(mrt, net) {
1740 v = &mrt->vif_table[0];
1741 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1742 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001743 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001744 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 }
1746 return NOTIFY_DONE;
1747}
1748
Jianjun Kongc354e122008-11-03 00:28:02 -08001749static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 .notifier_call = ipmr_device_event,
1751};
1752
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001753/* Encapsulate a packet by attaching a valid IPIP header to it.
1754 * This avoids tunnel drivers and other mess and gives us the speed so
1755 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001757static void ip_encap(struct net *net, struct sk_buff *skb,
1758 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001760 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001761 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001762
1763 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001764 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001765 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001766 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001768 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001769 iph->tos = old_iph->tos;
1770 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 iph->frag_off = 0;
1772 iph->daddr = daddr;
1773 iph->saddr = saddr;
1774 iph->protocol = IPPROTO_IPIP;
1775 iph->ihl = 5;
1776 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001777 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 ip_send_check(iph);
1779
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1781 nf_reset(skb);
1782}
1783
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001784static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1785 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001787 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
David S. Miller73186df2015-11-03 13:41:45 -05001789 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1790 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791
1792 if (unlikely(opt->optlen))
1793 ip_forward_options(skb);
1794
Eric W. Biederman13206b62015-10-07 16:48:35 -05001795 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796}
1797
Yotam Gigia5bc9292017-10-03 09:58:08 +02001798#ifdef CONFIG_NET_SWITCHDEV
1799static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1800 int in_vifi, int out_vifi)
1801{
1802 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1803 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1804
1805 if (!skb->offload_mr_fwd_mark)
1806 return false;
1807 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1808 return false;
1809 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1810 &in_vif->dev_parent_id);
1811}
1812#else
1813static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1814 int in_vifi, int out_vifi)
1815{
1816 return false;
1817}
1818#endif
1819
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001820/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821
Patrick McHardy0c122952010-04-13 05:03:22 +00001822static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
Yotam Gigia5bc9292017-10-03 09:58:08 +02001823 int in_vifi, struct sk_buff *skb,
1824 struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001826 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001827 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 struct net_device *dev;
1829 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001830 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 int encap = 0;
1832
Ian Morris51456b22015-04-03 09:17:26 +01001833 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 goto out_free;
1835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 if (vif->flags & VIFF_REGISTER) {
1837 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001838 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001839 vif->dev->stats.tx_bytes += skb->len;
1840 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001841 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001842 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Yotam Gigia5bc9292017-10-03 09:58:08 +02001845 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1846 goto out_free;
1847
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001848 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001849 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001850 vif->remote, vif->local,
1851 0, 0,
1852 IPPROTO_IPIP,
1853 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001854 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 goto out_free;
1856 encap = sizeof(struct iphdr);
1857 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001858 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001859 0, 0,
1860 IPPROTO_IPIP,
1861 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001862 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 goto out_free;
1864 }
1865
Changli Gaod8d1f302010-06-10 23:31:35 -07001866 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
Changli Gaod8d1f302010-06-10 23:31:35 -07001868 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001870 * allow to send ICMP, so that packets will disappear
1871 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 */
David S. Miller73186df2015-11-03 13:41:45 -05001873 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 ip_rt_put(rt);
1875 goto out_free;
1876 }
1877
Changli Gaod8d1f302010-06-10 23:31:35 -07001878 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
1880 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001881 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 goto out_free;
1883 }
1884
1885 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001886 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Eric Dumazetadf30902009-06-02 05:19:30 +00001888 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001889 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001890 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001893 * What do we do with netfilter? -- RR
1894 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001896 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001898 vif->dev->stats.tx_packets++;
1899 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 }
1901
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001902 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001904 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 * not only before forwarding, but after forwarding on all output
1906 * interfaces. It is clear, if mrouter runs a multicasting
1907 * program, it should receive packets not depending to what interface
1908 * program is joined.
1909 * If we will not make it, the program will have to join on all
1910 * interfaces. On the other hand, multihoming host (or router, but
1911 * not mrouter) cannot join to more than one interface - it will
1912 * result in receiving multiple packets.
1913 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001914 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1915 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 ipmr_forward_finish);
1917 return;
1918
1919out_free:
1920 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
Patrick McHardy0c122952010-04-13 05:03:22 +00001923static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924{
1925 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001926
1927 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1928 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 break;
1930 }
1931 return ct;
1932}
1933
1934/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001935static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001936 struct net_device *dev, struct sk_buff *skb,
Yuval Mintz494fff52018-02-28 23:29:34 +02001937 struct mfc_cache *c, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938{
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001939 int true_vifi = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 int psend = -1;
1941 int vif, ct;
1942
Yuval Mintz494fff52018-02-28 23:29:34 +02001943 vif = c->_c.mfc_parent;
1944 c->_c.mfc_un.res.pkt++;
1945 c->_c.mfc_un.res.bytes += skb->len;
1946 c->_c.mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Yuval Mintz494fff52018-02-28 23:29:34 +02001948 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001949 struct mfc_cache *cache_proxy;
1950
1951 /* For an (*,G) entry, we only check that the incomming
1952 * interface is part of the static tree.
1953 */
Yuval Mintz845c9a72018-02-28 23:29:35 +02001954 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001955 if (cache_proxy &&
Yuval Mintz494fff52018-02-28 23:29:34 +02001956 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001957 goto forward;
1958 }
1959
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001960 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001961 if (mrt->vif_table[vif].dev != dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001962 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001964 * Very complicated situation...
1965 *
1966 * The best workaround until routing daemons will be
1967 * fixed is not to redistribute packet, if it was
1968 * send through wrong interface. It means, that
1969 * multicast applications WILL NOT work for
1970 * (S,G), which have default multicast route pointing
1971 * to wrong oif. In any case, it is not a good
1972 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 */
1974 goto dont_forward;
1975 }
1976
Yuval Mintz494fff52018-02-28 23:29:34 +02001977 c->_c.mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978
Patrick McHardy0c122952010-04-13 05:03:22 +00001979 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001981 * so that we cannot check that packet arrived on an oif.
1982 * It is bad, but otherwise we would need to move pretty
1983 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001985 (mrt->mroute_do_pim ||
Yuval Mintz494fff52018-02-28 23:29:34 +02001986 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001987 time_after(jiffies,
Yuval Mintz494fff52018-02-28 23:29:34 +02001988 c->_c.mfc_un.res.last_assert +
1989 MFC_ASSERT_THRESH)) {
1990 c->_c.mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001991 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001992 if (mrt->mroute_do_wrvifwhole)
1993 ipmr_cache_report(mrt, skb, true_vifi,
1994 IGMPMSG_WRVIFWHOLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 }
1996 goto dont_forward;
1997 }
1998
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001999forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00002000 mrt->vif_table[vif].pkt_in++;
2001 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002003 /* Forward the frame */
Yuval Mintz494fff52018-02-28 23:29:34 +02002004 if (c->mfc_origin == htonl(INADDR_ANY) &&
2005 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002006 if (true_vifi >= 0 &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002007 true_vifi != c->_c.mfc_parent &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002008 ip_hdr(skb)->ttl >
Yuval Mintz494fff52018-02-28 23:29:34 +02002009 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002010 /* It's an (*,*) entry and the packet is not coming from
2011 * the upstream: forward the packet to the upstream
2012 * only.
2013 */
Yuval Mintz494fff52018-02-28 23:29:34 +02002014 psend = c->_c.mfc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002015 goto last_forward;
2016 }
2017 goto dont_forward;
2018 }
Yuval Mintz494fff52018-02-28 23:29:34 +02002019 for (ct = c->_c.mfc_un.res.maxvif - 1;
2020 ct >= c->_c.mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002021 /* For (*,G) entry, don't forward to the incoming interface */
Yuval Mintz494fff52018-02-28 23:29:34 +02002022 if ((c->mfc_origin != htonl(INADDR_ANY) ||
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002023 ct != true_vifi) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002024 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (psend != -1) {
2026 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002027
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002029 ipmr_queue_xmit(net, mrt, true_vifi,
Yuval Mintz494fff52018-02-28 23:29:34 +02002030 skb2, c, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002032 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 }
2034 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002035last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (psend != -1) {
2037 if (local) {
2038 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002041 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
Yuval Mintz494fff52018-02-28 23:29:34 +02002042 c, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 } else {
Yuval Mintz494fff52018-02-28 23:29:34 +02002044 ipmr_queue_xmit(net, mrt, true_vifi, skb, c, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03002045 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 }
2047 }
2048
2049dont_forward:
2050 if (!local)
2051 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052}
2053
David S. Miller417da662011-05-03 19:42:43 -07002054static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08002055{
David S. Miller417da662011-05-03 19:42:43 -07002056 struct rtable *rt = skb_rtable(skb);
2057 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05002058 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07002059 .daddr = iph->daddr,
2060 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00002061 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07002062 .flowi4_oif = (rt_is_output_route(rt) ?
2063 skb->dev->ifindex : 0),
2064 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002065 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07002066 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00002067 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08002068 };
2069 struct mr_table *mrt;
2070 int err;
2071
David S. Millerda919812011-03-12 02:04:50 -05002072 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002073 if (err)
2074 return ERR_PTR(err);
2075 return mrt;
2076}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002078/* Multicast packets for forwarding arrive here
2079 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081int ip_mr_input(struct sk_buff *skb)
2082{
2083 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002084 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00002085 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002086 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002087 struct net_device *dev;
2088
2089 /* skb->dev passed in is the loX master dev for vrfs.
2090 * As there are no vifs associated with loopback devices,
2091 * get the proper interface that does have a vif associated with it.
2092 */
2093 dev = skb->dev;
2094 if (netif_is_l3_master(skb->dev)) {
2095 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2096 if (!dev) {
2097 kfree_skb(skb);
2098 return -ENODEV;
2099 }
2100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002103 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00002105 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 goto dont_forward;
2107
David S. Miller417da662011-05-03 19:42:43 -07002108 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002109 if (IS_ERR(mrt)) {
2110 kfree_skb(skb);
2111 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002112 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002113 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00002114 if (IPCB(skb)->opt.router_alert) {
2115 if (ip_call_ra_chain(skb))
2116 return 0;
2117 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2118 /* IGMPv1 (and broken IGMPv2 implementations sort of
2119 * Cisco IOS <= 11.2(8)) do not put router alert
2120 * option to IGMP packets destined to routable
2121 * groups. It is very bad, because it means
2122 * that we can forward NO IGMP messages.
2123 */
2124 struct sock *mroute_sk;
2125
2126 mroute_sk = rcu_dereference(mrt->mroute_sk);
2127 if (mroute_sk) {
2128 nf_reset(skb);
2129 raw_rcv(mroute_sk, skb);
2130 return 0;
2131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 }
2133 }
2134
Eric Dumazeta8c94862010-10-01 16:15:08 +00002135 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002136 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002137 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002138 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002139
2140 if (vif >= 0)
2141 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2142 vif);
2143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002145 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002146 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147 int vif;
2148
2149 if (local) {
2150 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2151 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002152 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 skb = skb2;
2155 }
2156
Eric Dumazeta8c94862010-10-01 16:15:08 +00002157 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002158 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 if (vif >= 0) {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002160 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 read_unlock(&mrt_lock);
2162
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002163 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 }
2165 read_unlock(&mrt_lock);
2166 kfree_skb(skb);
2167 return -ENODEV;
2168 }
2169
Eric Dumazeta8c94862010-10-01 16:15:08 +00002170 read_lock(&mrt_lock);
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002171 ip_mr_forward(net, mrt, dev, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 read_unlock(&mrt_lock);
2173
2174 if (local)
2175 return ip_local_deliver(skb);
2176
2177 return 0;
2178
2179dont_forward:
2180 if (local)
2181 return ip_local_deliver(skb);
2182 kfree_skb(skb);
2183 return 0;
2184}
2185
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002186#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002187/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002188int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002189{
2190 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002191 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002192 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002193
2194 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2195 goto drop;
2196
2197 pim = igmp_hdr(skb);
2198
David S. Miller417da662011-05-03 19:42:43 -07002199 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002200 if (IS_ERR(mrt))
2201 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002202 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002203 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2204 goto drop;
2205
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002206 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002207drop:
2208 kfree_skb(skb);
2209 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 return 0;
2211}
2212#endif
2213
2214#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002215static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216{
2217 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002218 struct net *net = dev_net(skb->dev);
2219 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002221 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 goto drop;
2223
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002224 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002225 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002226 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002227 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002228 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 goto drop;
2230
David S. Miller417da662011-05-03 19:42:43 -07002231 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002232 if (IS_ERR(mrt))
2233 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002234 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002235drop:
2236 kfree_skb(skb);
2237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238 return 0;
2239}
2240#endif
2241
David S. Miller9a1b9492011-05-04 12:18:54 -07002242int ipmr_get_route(struct net *net, struct sk_buff *skb,
2243 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002244 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002245{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002247 struct mr_table *mrt;
2248 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002250 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002251 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002252 return -ENOENT;
2253
Eric Dumazeta8c94862010-10-01 16:15:08 +00002254 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002255 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002256 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002257 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002259 if (vif >= 0)
2260 cache = ipmr_cache_find_any(mrt, daddr, vif);
2261 }
Ian Morris51456b22015-04-03 09:17:26 +01002262 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002263 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002264 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002266 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002269 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002270 if (dev)
2271 vif = ipmr_find_vif(mrt, dev);
2272 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002274 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 return -ENODEV;
2276 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002277 skb2 = skb_clone(skb, GFP_ATOMIC);
2278 if (!skb2) {
2279 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002280 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002281 return -ENOMEM;
2282 }
2283
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002284 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002285 skb_push(skb2, sizeof(struct iphdr));
2286 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002287 iph = ip_hdr(skb2);
2288 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002289 iph->saddr = saddr;
2290 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002291 iph->version = 0;
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002292 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002294 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 return err;
2296 }
2297
Eric Dumazeta8c94862010-10-01 16:15:08 +00002298 read_lock(&mrt_lock);
Yuval Mintz7b0db852018-02-28 23:29:39 +02002299 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002301 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 return err;
2303}
2304
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002305static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002306 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2307 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002308{
2309 struct nlmsghdr *nlh;
2310 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002311 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002312
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002313 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002314 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002315 return -EMSGSIZE;
2316
2317 rtm = nlmsg_data(nlh);
2318 rtm->rtm_family = RTNL_FAMILY_IPMR;
2319 rtm->rtm_dst_len = 32;
2320 rtm->rtm_src_len = 32;
2321 rtm->rtm_tos = 0;
2322 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002323 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2324 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002325 rtm->rtm_type = RTN_MULTICAST;
2326 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Yuval Mintz494fff52018-02-28 23:29:34 +02002327 if (c->_c.mfc_flags & MFC_STATIC)
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002328 rtm->rtm_protocol = RTPROT_STATIC;
2329 else
2330 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002331 rtm->rtm_flags = 0;
2332
Jiri Benc930345e2015-03-29 16:59:25 +02002333 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2334 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002335 goto nla_put_failure;
Yuval Mintz7b0db852018-02-28 23:29:39 +02002336 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002337 /* do not break the dump if cache is unresolved */
2338 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002339 goto nla_put_failure;
2340
Johannes Berg053c0952015-01-16 22:09:00 +01002341 nlmsg_end(skb, nlh);
2342 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002343
2344nla_put_failure:
2345 nlmsg_cancel(skb, nlh);
2346 return -EMSGSIZE;
2347}
2348
Yuval Mintz7b0db852018-02-28 23:29:39 +02002349static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2350 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2351 int flags)
2352{
2353 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2354 cmd, flags);
2355}
2356
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002357static size_t mroute_msgsize(bool unresolved, int maxvif)
2358{
2359 size_t len =
2360 NLMSG_ALIGN(sizeof(struct rtmsg))
2361 + nla_total_size(4) /* RTA_TABLE */
2362 + nla_total_size(4) /* RTA_SRC */
2363 + nla_total_size(4) /* RTA_DST */
2364 ;
2365
2366 if (!unresolved)
2367 len = len
2368 + nla_total_size(4) /* RTA_IIF */
2369 + nla_total_size(0) /* RTA_MULTIPATH */
2370 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2371 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002372 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002373 ;
2374
2375 return len;
2376}
2377
2378static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2379 int cmd)
2380{
2381 struct net *net = read_pnet(&mrt->net);
2382 struct sk_buff *skb;
2383 int err = -ENOBUFS;
2384
Yuval Mintz494fff52018-02-28 23:29:34 +02002385 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2386 mrt->maxvif),
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002387 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002388 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002389 goto errout;
2390
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002391 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002392 if (err < 0)
2393 goto errout;
2394
2395 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2396 return;
2397
2398errout:
2399 kfree_skb(skb);
2400 if (err < 0)
2401 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2402}
2403
Julien Gomes5a645dd2017-06-20 13:54:17 -07002404static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2405{
2406 size_t len =
2407 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2408 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2409 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2410 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2411 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2412 /* IPMRA_CREPORT_PKT */
2413 + nla_total_size(payloadlen)
2414 ;
2415
2416 return len;
2417}
2418
2419static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2420{
2421 struct net *net = read_pnet(&mrt->net);
2422 struct nlmsghdr *nlh;
2423 struct rtgenmsg *rtgenm;
2424 struct igmpmsg *msg;
2425 struct sk_buff *skb;
2426 struct nlattr *nla;
2427 int payloadlen;
2428
2429 payloadlen = pkt->len - sizeof(struct igmpmsg);
2430 msg = (struct igmpmsg *)skb_network_header(pkt);
2431
2432 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2433 if (!skb)
2434 goto errout;
2435
2436 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2437 sizeof(struct rtgenmsg), 0);
2438 if (!nlh)
2439 goto errout;
2440 rtgenm = nlmsg_data(nlh);
2441 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2442 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2443 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2444 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2445 msg->im_src.s_addr) ||
2446 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2447 msg->im_dst.s_addr))
2448 goto nla_put_failure;
2449
2450 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2451 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2452 nla_data(nla), payloadlen))
2453 goto nla_put_failure;
2454
2455 nlmsg_end(skb, nlh);
2456
2457 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2458 return;
2459
2460nla_put_failure:
2461 nlmsg_cancel(skb, nlh);
2462errout:
2463 kfree_skb(skb);
2464 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2465}
2466
Donald Sharp4f75ba62017-06-28 13:58:57 -04002467static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2468 struct netlink_ext_ack *extack)
2469{
2470 struct net *net = sock_net(in_skb->sk);
2471 struct nlattr *tb[RTA_MAX + 1];
2472 struct sk_buff *skb = NULL;
2473 struct mfc_cache *cache;
2474 struct mr_table *mrt;
2475 struct rtmsg *rtm;
2476 __be32 src, grp;
2477 u32 tableid;
2478 int err;
2479
2480 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2481 rtm_ipv4_policy, extack);
2482 if (err < 0)
2483 goto errout;
2484
2485 rtm = nlmsg_data(nlh);
2486
2487 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2488 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2489 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2490
2491 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
Dan Carpenter2e3d2322017-07-12 10:56:47 +03002492 if (!mrt) {
2493 err = -ENOENT;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002494 goto errout_free;
2495 }
2496
2497 /* entries are added/deleted only under RTNL */
2498 rcu_read_lock();
2499 cache = ipmr_cache_find(mrt, src, grp);
2500 rcu_read_unlock();
2501 if (!cache) {
2502 err = -ENOENT;
2503 goto errout_free;
2504 }
2505
2506 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2507 if (!skb) {
2508 err = -ENOBUFS;
2509 goto errout_free;
2510 }
2511
2512 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2513 nlh->nlmsg_seq, cache,
2514 RTM_NEWROUTE, 0);
2515 if (err < 0)
2516 goto errout_free;
2517
2518 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2519
2520errout:
2521 return err;
2522
2523errout_free:
2524 kfree_skb(skb);
2525 goto errout;
2526}
2527
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002528static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2529{
David Ahern47246762018-10-15 18:56:42 -07002530 struct fib_dump_filter filter = {};
David Aherncb167892018-10-15 18:56:47 -07002531 int err;
David Aherne8ba3302018-10-07 20:16:35 -07002532
David Ahern47246762018-10-15 18:56:42 -07002533 if (cb->strict_check) {
David Ahern47246762018-10-15 18:56:42 -07002534 err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh,
David Aherneffe6792018-10-15 18:56:48 -07002535 &filter, cb);
David Aherne8ba3302018-10-07 20:16:35 -07002536 if (err < 0)
2537 return err;
2538 }
2539
David Aherncb167892018-10-15 18:56:47 -07002540 if (filter.table_id) {
2541 struct mr_table *mrt;
2542
2543 mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id);
2544 if (!mrt) {
David Ahernae677bb2018-10-24 12:59:01 -07002545 if (filter.dump_all_families)
2546 return skb->len;
2547
David Aherncb167892018-10-15 18:56:47 -07002548 NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist");
2549 return -ENOENT;
2550 }
2551 err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute,
2552 &mfc_unres_lock, &filter);
2553 return skb->len ? : err;
2554 }
2555
Yuval Mintz7b0db852018-02-28 23:29:39 +02002556 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
David Aherncb167892018-10-15 18:56:47 -07002557 _ipmr_fill_mroute, &mfc_unres_lock, &filter);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002558}
2559
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002560static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2561 [RTA_SRC] = { .type = NLA_U32 },
2562 [RTA_DST] = { .type = NLA_U32 },
2563 [RTA_IIF] = { .type = NLA_U32 },
2564 [RTA_TABLE] = { .type = NLA_U32 },
2565 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2566};
2567
2568static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2569{
2570 switch (rtm_protocol) {
2571 case RTPROT_STATIC:
2572 case RTPROT_MROUTED:
2573 return true;
2574 }
2575 return false;
2576}
2577
2578static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2579{
2580 struct rtnexthop *rtnh = nla_data(nla);
2581 int remaining = nla_len(nla), vifi = 0;
2582
2583 while (rtnh_ok(rtnh, remaining)) {
2584 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2585 if (++vifi == MAXVIFS)
2586 break;
2587 rtnh = rtnh_next(rtnh, &remaining);
2588 }
2589
2590 return remaining > 0 ? -EINVAL : vifi;
2591}
2592
2593/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2594static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2595 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002596 struct mr_table **mrtret,
2597 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002598{
2599 struct net_device *dev = NULL;
2600 u32 tblid = RT_TABLE_DEFAULT;
2601 struct mr_table *mrt;
2602 struct nlattr *attr;
2603 struct rtmsg *rtm;
2604 int ret, rem;
2605
Johannes Bergfceb6432017-04-12 14:34:07 +02002606 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07002607 extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002608 if (ret < 0)
2609 goto out;
2610 rtm = nlmsg_data(nlh);
2611
2612 ret = -EINVAL;
2613 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2614 rtm->rtm_type != RTN_MULTICAST ||
2615 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2616 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2617 goto out;
2618
2619 memset(mfcc, 0, sizeof(*mfcc));
2620 mfcc->mfcc_parent = -1;
2621 ret = 0;
2622 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2623 switch (nla_type(attr)) {
2624 case RTA_SRC:
2625 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2626 break;
2627 case RTA_DST:
2628 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2629 break;
2630 case RTA_IIF:
2631 dev = __dev_get_by_index(net, nla_get_u32(attr));
2632 if (!dev) {
2633 ret = -ENODEV;
2634 goto out;
2635 }
2636 break;
2637 case RTA_MULTIPATH:
2638 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2639 ret = -EINVAL;
2640 goto out;
2641 }
2642 break;
2643 case RTA_PREFSRC:
2644 ret = 1;
2645 break;
2646 case RTA_TABLE:
2647 tblid = nla_get_u32(attr);
2648 break;
2649 }
2650 }
2651 mrt = ipmr_get_table(net, tblid);
2652 if (!mrt) {
2653 ret = -ENOENT;
2654 goto out;
2655 }
2656 *mrtret = mrt;
2657 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2658 if (dev)
2659 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2660
2661out:
2662 return ret;
2663}
2664
2665/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002666static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2667 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002668{
2669 struct net *net = sock_net(skb->sk);
2670 int ret, mrtsock, parent;
2671 struct mr_table *tbl;
2672 struct mfcctl mfcc;
2673
2674 mrtsock = 0;
2675 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002676 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002677 if (ret < 0)
2678 return ret;
2679
2680 parent = ret ? mfcc.mfcc_parent : -1;
2681 if (nlh->nlmsg_type == RTM_NEWROUTE)
2682 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2683 else
2684 return ipmr_mfc_delete(tbl, &mfcc, parent);
2685}
2686
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002687static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2688{
2689 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2690
2691 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2692 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2693 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2694 mrt->mroute_reg_vif_num) ||
2695 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2696 mrt->mroute_do_assert) ||
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03002697 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
2698 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
2699 mrt->mroute_do_wrvifwhole))
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002700 return false;
2701
2702 return true;
2703}
2704
2705static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2706{
2707 struct nlattr *vif_nest;
2708 struct vif_device *vif;
2709
2710 /* if the VIF doesn't exist just continue */
2711 if (!VIF_EXISTS(mrt, vifid))
2712 return true;
2713
2714 vif = &mrt->vif_table[vifid];
2715 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2716 if (!vif_nest)
2717 return false;
2718 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2719 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2720 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2721 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2722 IPMRA_VIFA_PAD) ||
2723 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2724 IPMRA_VIFA_PAD) ||
2725 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2726 IPMRA_VIFA_PAD) ||
2727 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2728 IPMRA_VIFA_PAD) ||
2729 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2730 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2731 nla_nest_cancel(skb, vif_nest);
2732 return false;
2733 }
2734 nla_nest_end(skb, vif_nest);
2735
2736 return true;
2737}
2738
David Ahern14fc5bb2018-10-07 20:16:34 -07002739static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
2740 struct netlink_ext_ack *extack)
2741{
2742 struct ifinfomsg *ifm;
2743
2744 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2745 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
2746 return -EINVAL;
2747 }
2748
2749 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
2750 NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump");
2751 return -EINVAL;
2752 }
2753
2754 ifm = nlmsg_data(nlh);
2755 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2756 ifm->ifi_change || ifm->ifi_index) {
2757 NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
2758 return -EINVAL;
2759 }
2760
2761 return 0;
2762}
2763
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002764static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2765{
2766 struct net *net = sock_net(skb->sk);
2767 struct nlmsghdr *nlh = NULL;
2768 unsigned int t = 0, s_t;
2769 unsigned int e = 0, s_e;
2770 struct mr_table *mrt;
2771
David Ahern14fc5bb2018-10-07 20:16:34 -07002772 if (cb->strict_check) {
2773 int err = ipmr_valid_dumplink(cb->nlh, cb->extack);
2774
2775 if (err < 0)
2776 return err;
2777 }
2778
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002779 s_t = cb->args[0];
2780 s_e = cb->args[1];
2781
2782 ipmr_for_each_table(mrt, net) {
2783 struct nlattr *vifs, *af;
2784 struct ifinfomsg *hdr;
2785 u32 i;
2786
2787 if (t < s_t)
2788 goto skip_table;
2789 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2790 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2791 sizeof(*hdr), NLM_F_MULTI);
2792 if (!nlh)
2793 break;
2794
2795 hdr = nlmsg_data(nlh);
2796 memset(hdr, 0, sizeof(*hdr));
2797 hdr->ifi_family = RTNL_FAMILY_IPMR;
2798
2799 af = nla_nest_start(skb, IFLA_AF_SPEC);
2800 if (!af) {
2801 nlmsg_cancel(skb, nlh);
2802 goto out;
2803 }
2804
2805 if (!ipmr_fill_table(mrt, skb)) {
2806 nlmsg_cancel(skb, nlh);
2807 goto out;
2808 }
2809
2810 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2811 if (!vifs) {
2812 nla_nest_end(skb, af);
2813 nlmsg_end(skb, nlh);
2814 goto out;
2815 }
2816 for (i = 0; i < mrt->maxvif; i++) {
2817 if (e < s_e)
2818 goto skip_entry;
2819 if (!ipmr_fill_vif(mrt, i, skb)) {
2820 nla_nest_end(skb, vifs);
2821 nla_nest_end(skb, af);
2822 nlmsg_end(skb, nlh);
2823 goto out;
2824 }
2825skip_entry:
2826 e++;
2827 }
2828 s_e = 0;
2829 e = 0;
2830 nla_nest_end(skb, vifs);
2831 nla_nest_end(skb, af);
2832 nlmsg_end(skb, nlh);
2833skip_table:
2834 t++;
2835 }
2836
2837out:
2838 cb->args[1] = e;
2839 cb->args[0] = t;
2840
2841 return skb->len;
2842}
2843
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002844#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002845/* The /proc interfaces to multicast routing :
2846 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848
2849static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002850 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002852 struct mr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002853 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002854 struct mr_table *mrt;
2855
2856 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002857 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002858 return ERR_PTR(-ENOENT);
2859
2860 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002861
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 read_lock(&mrt_lock);
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002863 return mr_vif_seq_start(seq, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864}
2865
2866static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002867 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868{
2869 read_unlock(&mrt_lock);
2870}
2871
2872static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2873{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002874 struct mr_vif_iter *iter = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002875 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002876
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002878 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2880 } else {
2881 const struct vif_device *vif = v;
Yuval Mintz6853f212018-02-28 23:29:29 +02002882 const char *name = vif->dev ?
2883 vif->dev->name : "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002884
2885 seq_printf(seq,
James Hogan91e6dd82018-01-30 09:48:02 +00002886 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002887 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002888 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002889 vif->bytes_out, vif->pkt_out,
2890 vif->flags, vif->local, vif->remote);
2891 }
2892 return 0;
2893}
2894
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002895static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 .start = ipmr_vif_seq_start,
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002897 .next = mr_vif_seq_next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 .stop = ipmr_vif_seq_stop,
2899 .show = ipmr_vif_seq_show,
2900};
2901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2903{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002904 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002905 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002906
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002907 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002908 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002909 return ERR_PTR(-ENOENT);
2910
Yuval Mintzc8d61962018-02-28 23:29:36 +02002911 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
2913
2914static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2915{
2916 int n;
2917
2918 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002919 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2921 } else {
2922 const struct mfc_cache *mfc = v;
Yuval Mintzc8d61962018-02-28 23:29:36 +02002923 const struct mr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002924 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002925
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002926 seq_printf(seq, "%08X %08X %-3hd",
2927 (__force u32) mfc->mfc_mcastgrp,
2928 (__force u32) mfc->mfc_origin,
Yuval Mintz494fff52018-02-28 23:29:34 +02002929 mfc->_c.mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Patrick McHardy0c122952010-04-13 05:03:22 +00002931 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002932 seq_printf(seq, " %8lu %8lu %8lu",
Yuval Mintz494fff52018-02-28 23:29:34 +02002933 mfc->_c.mfc_un.res.pkt,
2934 mfc->_c.mfc_un.res.bytes,
2935 mfc->_c.mfc_un.res.wrong_if);
2936 for (n = mfc->_c.mfc_un.res.minvif;
2937 n < mfc->_c.mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002938 if (VIF_EXISTS(mrt, n) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002939 mfc->_c.mfc_un.res.ttls[n] < 255)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002940 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002941 " %2d:%-3d",
Yuval Mintz494fff52018-02-28 23:29:34 +02002942 n, mfc->_c.mfc_un.res.ttls[n]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002944 } else {
2945 /* unresolved mfc_caches don't contain
2946 * pkt, bytes and wrong_if values
2947 */
2948 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
2950 seq_putc(seq, '\n');
2951 }
2952 return 0;
2953}
2954
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002955static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 .start = ipmr_mfc_seq_start,
Yuval Mintzc8d61962018-02-28 23:29:36 +02002957 .next = mr_mfc_seq_next,
2958 .stop = mr_mfc_seq_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 .show = ipmr_mfc_seq_show,
2960};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002961#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962
2963#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002964static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002965 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002966 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967};
2968#endif
2969
Yotam Gigi4d65b942017-09-27 08:23:13 +02002970static unsigned int ipmr_seq_read(struct net *net)
2971{
2972 ASSERT_RTNL();
2973
2974 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
2975}
2976
2977static int ipmr_dump(struct net *net, struct notifier_block *nb)
2978{
Yuval Mintzcdc9f942018-03-26 15:01:33 +03002979 return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
2980 ipmr_mr_table_iter, &mrt_lock);
Yotam Gigi4d65b942017-09-27 08:23:13 +02002981}
2982
2983static const struct fib_notifier_ops ipmr_notifier_ops_template = {
2984 .family = RTNL_FAMILY_IPMR,
2985 .fib_seq_read = ipmr_seq_read,
2986 .fib_dump = ipmr_dump,
2987 .owner = THIS_MODULE,
2988};
2989
Colin Ian Kingef739d82017-09-29 14:34:22 +01002990static int __net_init ipmr_notifier_init(struct net *net)
Yotam Gigi4d65b942017-09-27 08:23:13 +02002991{
2992 struct fib_notifier_ops *ops;
2993
2994 net->ipv4.ipmr_seq = 0;
2995
2996 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
2997 if (IS_ERR(ops))
2998 return PTR_ERR(ops);
2999 net->ipv4.ipmr_notifier_ops = ops;
3000
3001 return 0;
3002}
3003
3004static void __net_exit ipmr_notifier_exit(struct net *net)
3005{
3006 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3007 net->ipv4.ipmr_notifier_ops = NULL;
3008}
3009
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01003010/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00003011static int __net_init ipmr_net_init(struct net *net)
3012{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003013 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00003014
Yotam Gigi4d65b942017-09-27 08:23:13 +02003015 err = ipmr_notifier_init(net);
3016 if (err)
3017 goto ipmr_notifier_fail;
3018
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003019 err = ipmr_rules_init(net);
3020 if (err < 0)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003021 goto ipmr_rules_fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003022
3023#ifdef CONFIG_PROC_FS
3024 err = -ENOMEM;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003025 if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
3026 sizeof(struct mr_vif_iter)))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003027 goto proc_vif_fail;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003028 if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
3029 sizeof(struct mr_mfc_iter)))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003030 goto proc_cache_fail;
3031#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00003032 return 0;
3033
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003034#ifdef CONFIG_PROC_FS
3035proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00003036 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003037proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003038 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003039#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003040ipmr_rules_fail:
3041 ipmr_notifier_exit(net);
3042ipmr_notifier_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003043 return err;
3044}
3045
3046static void __net_exit ipmr_net_exit(struct net *net)
3047{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003048#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003049 remove_proc_entry("ip_mr_cache", net->proc_net);
3050 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003051#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003052 ipmr_notifier_exit(net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003053 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00003054}
3055
3056static struct pernet_operations ipmr_net_ops = {
3057 .init = ipmr_net_init,
3058 .exit = ipmr_net_exit,
3059};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003060
Wang Chen03d2f892008-07-03 12:13:36 +08003061int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
Wang Chen03d2f892008-07-03 12:13:36 +08003063 int err;
3064
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3066 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00003067 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003068 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08003069
Benjamin Therycf958ae32009-01-22 04:56:16 +00003070 err = register_pernet_subsys(&ipmr_net_ops);
3071 if (err)
3072 goto reg_pernet_fail;
3073
Wang Chen03d2f892008-07-03 12:13:36 +08003074 err = register_netdevice_notifier(&ip_mr_notifier);
3075 if (err)
3076 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07003077#ifdef CONFIG_IP_PIMSM_V2
3078 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00003079 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07003080 err = -EAGAIN;
3081 goto add_proto_fail;
3082 }
3083#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00003084 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003085 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003086 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003087 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003088 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003089 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03003090
3091 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
Florian Westphalb97bac62017-08-09 20:41:48 +02003092 NULL, ipmr_rtm_dumplink, 0);
Wang Chen03d2f892008-07-03 12:13:36 +08003093 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003094
Tom Goff403dbb92009-06-14 03:16:13 -07003095#ifdef CONFIG_IP_PIMSM_V2
3096add_proto_fail:
3097 unregister_netdevice_notifier(&ip_mr_notifier);
3098#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08003099reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003100 unregister_pernet_subsys(&ipmr_net_ops);
3101reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08003102 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08003103 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104}