blob: fb99002c3d4e8523f2aa7926fee41ea8272bdb76 [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
Gustavo A. R. Silva56484512018-12-10 12:41:24 -060072#include <linux/nospec.h>
73
Patrick McHardyf0ad0862010-04-13 05:03:23 +000074struct ipmr_rule {
75 struct fib_rule common;
76};
77
78struct ipmr_result {
79 struct mr_table *mrt;
80};
81
Linus Torvalds1da177e2005-04-16 15:20:36 -070082/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000083 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 */
85
86static DEFINE_RWLOCK(mrt_lock);
87
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +010088/* Multicast router control variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/* Special spinlock for queue of unresolved entries */
91static DEFINE_SPINLOCK(mfc_unres_lock);
92
93/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000094 * entries is changed only in process context and protected
95 * with weak lock mrt_lock. Queue of unresolved entries is protected
96 * with strong spinlock mfc_unres_lock.
97 *
98 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 */
100
Alexey Dobriyan08009a72018-02-24 21:20:33 +0300101static struct kmem_cache *mrt_cachep __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000103static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000104static void ipmr_free_table(struct mr_table *mrt);
105
Rami Rosenc4854ec2013-07-20 15:09:28 +0300106static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -0400107 struct net_device *dev, struct sk_buff *skb,
108 struct mfc_cache *cache, int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000109static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000110 struct sk_buff *pkt, vifi_t vifi, int assert);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000111static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
112 int cmd);
Julien Gomes5a645dd2017-06-20 13:54:17 -0700113static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100114static void mroute_clean_tables(struct mr_table *mrt, bool all);
Kees Cooke99e88a2017-10-16 14:43:17 -0700115static void ipmr_expire_process(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000117#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
118#define ipmr_for_each_table(mrt, net) \
119 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
120
Yuval Mintz7b0db852018-02-28 23:29:39 +0200121static struct mr_table *ipmr_mr_table_iter(struct net *net,
122 struct mr_table *mrt)
123{
124 struct mr_table *ret;
125
126 if (!mrt)
127 ret = list_entry_rcu(net->ipv4.mr_tables.next,
128 struct mr_table, list);
129 else
130 ret = list_entry_rcu(mrt->list.next,
131 struct mr_table, list);
132
133 if (&ret->list == &net->ipv4.mr_tables)
134 return NULL;
135 return ret;
136}
137
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000138static struct mr_table *ipmr_get_table(struct net *net, u32 id)
139{
140 struct mr_table *mrt;
141
142 ipmr_for_each_table(mrt, net) {
143 if (mrt->id == id)
144 return mrt;
145 }
146 return NULL;
147}
148
David S. Millerda919812011-03-12 02:04:50 -0500149static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000150 struct mr_table **mrt)
151{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000152 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100153 struct ipmr_result res;
154 struct fib_lookup_arg arg = {
155 .result = &res,
156 .flags = FIB_LOOKUP_NOREF,
157 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000158
David Aherne58e4152016-10-31 15:54:00 -0700159 /* update flow if oif or iif point to device enslaved to l3mdev */
160 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
161
David S. Millerda919812011-03-12 02:04:50 -0500162 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
163 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000164 if (err < 0)
165 return err;
166 *mrt = res.mrt;
167 return 0;
168}
169
170static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
171 int flags, struct fib_lookup_arg *arg)
172{
173 struct ipmr_result *res = arg->result;
174 struct mr_table *mrt;
175
176 switch (rule->action) {
177 case FR_ACT_TO_TBL:
178 break;
179 case FR_ACT_UNREACHABLE:
180 return -ENETUNREACH;
181 case FR_ACT_PROHIBIT:
182 return -EACCES;
183 case FR_ACT_BLACKHOLE:
184 default:
185 return -EINVAL;
186 }
187
David Aherne58e4152016-10-31 15:54:00 -0700188 arg->table = fib_rule_get_table(rule, arg);
189
190 mrt = ipmr_get_table(rule->fr_net, arg->table);
Ian Morris51456b22015-04-03 09:17:26 +0100191 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000192 return -EAGAIN;
193 res->mrt = mrt;
194 return 0;
195}
196
197static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
198{
199 return 1;
200}
201
202static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
203 FRA_GENERIC_POLICY,
204};
205
206static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
Roopa Prabhub16fb412018-04-21 09:41:31 -0700207 struct fib_rule_hdr *frh, struct nlattr **tb,
208 struct netlink_ext_ack *extack)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000209{
210 return 0;
211}
212
213static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
214 struct nlattr **tb)
215{
216 return 1;
217}
218
219static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
220 struct fib_rule_hdr *frh)
221{
222 frh->dst_len = 0;
223 frh->src_len = 0;
224 frh->tos = 0;
225 return 0;
226}
227
Andi Kleen04a6f822012-10-04 17:12:11 -0700228static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200229 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000230 .rule_size = sizeof(struct ipmr_rule),
231 .addr_size = sizeof(u32),
232 .action = ipmr_rule_action,
233 .match = ipmr_rule_match,
234 .configure = ipmr_rule_configure,
235 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000236 .fill = ipmr_rule_fill,
237 .nlgroup = RTNLGRP_IPV4_RULE,
238 .policy = ipmr_rule_policy,
239 .owner = THIS_MODULE,
240};
241
242static int __net_init ipmr_rules_init(struct net *net)
243{
244 struct fib_rules_ops *ops;
245 struct mr_table *mrt;
246 int err;
247
248 ops = fib_rules_register(&ipmr_rules_ops_template, net);
249 if (IS_ERR(ops))
250 return PTR_ERR(ops);
251
252 INIT_LIST_HEAD(&net->ipv4.mr_tables);
253
254 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100255 if (IS_ERR(mrt)) {
256 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000257 goto err1;
258 }
259
260 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
261 if (err < 0)
262 goto err2;
263
264 net->ipv4.mr_rules_ops = ops;
265 return 0;
266
267err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700268 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000269err1:
270 fib_rules_unregister(ops);
271 return err;
272}
273
274static void __net_exit ipmr_rules_exit(struct net *net)
275{
276 struct mr_table *mrt, *next;
277
WANG Conged785302015-03-31 11:01:45 -0700278 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000279 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
280 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000281 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000282 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000283 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700284 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000285}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200286
287static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
288{
289 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR);
290}
291
292static unsigned int ipmr_rules_seq_read(struct net *net)
293{
294 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
295}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200296
297bool ipmr_rule_default(const struct fib_rule *rule)
298{
299 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
300}
301EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000302#else
303#define ipmr_for_each_table(mrt, net) \
304 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
305
Yuval Mintz7b0db852018-02-28 23:29:39 +0200306static struct mr_table *ipmr_mr_table_iter(struct net *net,
307 struct mr_table *mrt)
308{
309 if (!mrt)
310 return net->ipv4.mrt;
311 return NULL;
312}
313
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000314static struct mr_table *ipmr_get_table(struct net *net, u32 id)
315{
316 return net->ipv4.mrt;
317}
318
David S. Millerda919812011-03-12 02:04:50 -0500319static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000320 struct mr_table **mrt)
321{
322 *mrt = net->ipv4.mrt;
323 return 0;
324}
325
326static int __net_init ipmr_rules_init(struct net *net)
327{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100328 struct mr_table *mrt;
329
330 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
331 if (IS_ERR(mrt))
332 return PTR_ERR(mrt);
333 net->ipv4.mrt = mrt;
334 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000335}
336
337static void __net_exit ipmr_rules_exit(struct net *net)
338{
WANG Conged785302015-03-31 11:01:45 -0700339 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000340 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700341 net->ipv4.mrt = NULL;
342 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000343}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200344
345static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
346{
347 return 0;
348}
349
350static unsigned int ipmr_rules_seq_read(struct net *net)
351{
352 return 0;
353}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200354
355bool ipmr_rule_default(const struct fib_rule *rule)
356{
357 return true;
358}
359EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000360#endif
361
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100362static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
363 const void *ptr)
364{
365 const struct mfc_cache_cmp_arg *cmparg = arg->key;
366 struct mfc_cache *c = (struct mfc_cache *)ptr;
367
368 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
369 cmparg->mfc_origin != c->mfc_origin;
370}
371
372static const struct rhashtable_params ipmr_rht_params = {
Yuval Mintz494fff52018-02-28 23:29:34 +0200373 .head_offset = offsetof(struct mr_mfc, mnode),
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100374 .key_offset = offsetof(struct mfc_cache, cmparg),
375 .key_len = sizeof(struct mfc_cache_cmp_arg),
376 .nelem_hint = 3,
377 .locks_mul = 1,
378 .obj_cmpfn = ipmr_hash_cmp,
379 .automatic_shrinking = true,
380};
381
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200382static void ipmr_new_table_set(struct mr_table *mrt,
383 struct net *net)
384{
385#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
386 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
387#endif
388}
389
Yuval Mintz845c9a72018-02-28 23:29:35 +0200390static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
391 .mfc_mcastgrp = htonl(INADDR_ANY),
392 .mfc_origin = htonl(INADDR_ANY),
393};
394
395static struct mr_table_ops ipmr_mr_table_ops = {
396 .rht_params = &ipmr_rht_params,
397 .cmparg_any = &ipmr_mr_table_ops_cmparg_any,
398};
399
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000400static struct mr_table *ipmr_new_table(struct net *net, u32 id)
401{
402 struct mr_table *mrt;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000403
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100404 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
405 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
406 return ERR_PTR(-EINVAL);
407
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000408 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100409 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000410 return mrt;
411
Yuval Mintz845c9a72018-02-28 23:29:35 +0200412 return mr_table_alloc(net, id, &ipmr_mr_table_ops,
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200413 ipmr_expire_process, ipmr_new_table_set);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000414}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000416static void ipmr_free_table(struct mr_table *mrt)
417{
418 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100419 mroute_clean_tables(mrt, true);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100420 rhltable_destroy(&mrt->mfc_hash);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000421 kfree(mrt);
422}
423
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
425
Wang Chend6070322008-07-14 20:55:26 -0700426static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
427{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000428 struct net *net = dev_net(dev);
429
Wang Chend6070322008-07-14 20:55:26 -0700430 dev_close(dev);
431
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000432 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700433 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800434 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700435 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700436 struct ip_tunnel_parm p;
437
438 memset(&p, 0, sizeof(p));
439 p.iph.daddr = v->vifc_rmt_addr.s_addr;
440 p.iph.saddr = v->vifc_lcl_addr.s_addr;
441 p.iph.version = 4;
442 p.iph.ihl = 5;
443 p.iph.protocol = IPPROTO_IPIP;
444 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
445 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
446
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800447 if (ops->ndo_do_ioctl) {
448 mm_segment_t oldfs = get_fs();
449
450 set_fs(KERNEL_DS);
451 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
452 set_fs(oldfs);
453 }
Wang Chend6070322008-07-14 20:55:26 -0700454 }
455}
456
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100457/* Initialize ipmr pimreg/tunnel in_device */
458static bool ipmr_init_vif_indev(const struct net_device *dev)
459{
460 struct in_device *in_dev;
461
462 ASSERT_RTNL();
463
464 in_dev = __in_dev_get_rtnl(dev);
465 if (!in_dev)
466 return false;
467 ipv4_devconf_setall(in_dev);
468 neigh_parms_data_state_setall(in_dev->arp_parms);
469 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
470
471 return true;
472}
473
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100474static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
476 struct net_device *dev;
477
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000478 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800481 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 int err;
483 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
486 memset(&p, 0, sizeof(p));
487 p.iph.daddr = v->vifc_rmt_addr.s_addr;
488 p.iph.saddr = v->vifc_lcl_addr.s_addr;
489 p.iph.version = 4;
490 p.iph.ihl = 5;
491 p.iph.protocol = IPPROTO_IPIP;
492 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800493 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800495 if (ops->ndo_do_ioctl) {
496 mm_segment_t oldfs = get_fs();
497
498 set_fs(KERNEL_DS);
499 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
500 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000501 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800502 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 dev = NULL;
505
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000506 if (err == 0 &&
507 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100509 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 goto failure;
Petr Machata00f54e62018-12-06 17:05:36 +0000511 if (dev_open(dev, NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700513 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 }
516 return dev;
517
518failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 unregister_netdevice(dev);
520 return NULL;
521}
522
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100523#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000524static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000526 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000527 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500528 struct flowi4 fl4 = {
529 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700530 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500531 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000532 };
533 int err;
534
David S. Millerda919812011-03-12 02:04:50 -0500535 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000536 if (err < 0) {
537 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000538 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000539 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700542 dev->stats.tx_bytes += skb->len;
543 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000544 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 read_unlock(&mrt_lock);
546 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000547 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548}
549
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200550static int reg_vif_get_iflink(const struct net_device *dev)
551{
552 return 0;
553}
554
Stephen Hemminger007c3832008-11-20 20:28:35 -0800555static const struct net_device_ops reg_vif_netdev_ops = {
556 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200557 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800558};
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560static void reg_vif_setup(struct net_device *dev)
561{
562 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800563 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530565 dev->netdev_ops = &reg_vif_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400566 dev->needs_free_netdev = true;
Tom Goff403dbb92009-06-14 03:16:13 -0700567 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000570static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571{
572 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000573 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000575 if (mrt->id == RT_TABLE_DEFAULT)
576 sprintf(name, "pimreg");
577 else
578 sprintf(name, "pimreg%u", mrt->id);
579
Tom Gundersenc835a672014-07-14 16:37:24 +0200580 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Ian Morris51456b22015-04-03 09:17:26 +0100582 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return NULL;
584
Tom Goff403dbb92009-06-14 03:16:13 -0700585 dev_net_set(dev, net);
586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (register_netdevice(dev)) {
588 free_netdev(dev);
589 return NULL;
590 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100592 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 goto failure;
Petr Machata00f54e62018-12-06 17:05:36 +0000594 if (dev_open(dev, NULL))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 goto failure;
596
Wang Chen7dc00c82008-07-14 20:56:34 -0700597 dev_hold(dev);
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 return dev;
600
601failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 unregister_netdevice(dev);
603 return NULL;
604}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100605
606/* called with rcu_read_lock() */
607static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
608 unsigned int pimlen)
609{
610 struct net_device *reg_dev = NULL;
611 struct iphdr *encap;
612
613 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100614 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100615 * a. packet is really sent to a multicast group
616 * b. packet is not a NULL-REGISTER
617 * c. packet is not truncated
618 */
619 if (!ipv4_is_multicast(encap->daddr) ||
620 encap->tot_len == 0 ||
621 ntohs(encap->tot_len) + pimlen > skb->len)
622 return 1;
623
624 read_lock(&mrt_lock);
625 if (mrt->mroute_reg_vif_num >= 0)
626 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
627 read_unlock(&mrt_lock);
628
629 if (!reg_dev)
630 return 1;
631
632 skb->mac_header = skb->network_header;
633 skb_pull(skb, (u8 *)encap - skb->data);
634 skb_reset_network_header(skb);
635 skb->protocol = htons(ETH_P_IP);
636 skb->ip_summed = CHECKSUM_NONE;
637
638 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
639
640 netif_rx(skb);
641
642 return NET_RX_SUCCESS;
643}
644#else
645static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
646{
647 return NULL;
648}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649#endif
650
Yotam Gigib3620532017-09-27 08:23:14 +0200651static int call_ipmr_vif_entry_notifiers(struct net *net,
652 enum fib_event_type event_type,
653 struct vif_device *vif,
654 vifi_t vif_index, u32 tb_id)
655{
Yuval Mintzbc67a0d2018-03-26 15:01:31 +0300656 return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
657 vif, vif_index, tb_id,
658 &net->ipv4.ipmr_seq);
Yotam Gigib3620532017-09-27 08:23:14 +0200659}
660
Yotam Gigib3620532017-09-27 08:23:14 +0200661static int call_ipmr_mfc_entry_notifiers(struct net *net,
662 enum fib_event_type event_type,
663 struct mfc_cache *mfc, u32 tb_id)
664{
Yuval Mintz54c4cad2018-03-26 15:01:32 +0300665 return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
666 &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
Yotam Gigib3620532017-09-27 08:23:14 +0200667}
668
Ben Hutchings2c530402012-07-10 10:55:09 +0000669/**
670 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700671 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000673static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000674 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675{
Yotam Gigib3620532017-09-27 08:23:14 +0200676 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 struct vif_device *v;
678 struct net_device *dev;
679 struct in_device *in_dev;
680
Patrick McHardy0c122952010-04-13 05:03:22 +0000681 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 return -EADDRNOTAVAIL;
683
Patrick McHardy0c122952010-04-13 05:03:22 +0000684 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
Yotam Gigib3620532017-09-27 08:23:14 +0200686 if (VIF_EXISTS(mrt, vifi))
687 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
688 mrt->id);
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 write_lock_bh(&mrt_lock);
691 dev = v->dev;
692 v->dev = NULL;
693
694 if (!dev) {
695 write_unlock_bh(&mrt_lock);
696 return -EADDRNOTAVAIL;
697 }
698
Patrick McHardy0c122952010-04-13 05:03:22 +0000699 if (vifi == mrt->mroute_reg_vif_num)
700 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000702 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000704
705 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000706 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 break;
708 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000709 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 }
711
712 write_unlock_bh(&mrt_lock);
713
714 dev_set_allmulti(dev, -1);
715
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000716 in_dev = __in_dev_get_rtnl(dev);
717 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700718 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -0700719 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000720 NETCONFA_MC_FORWARDING,
721 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 ip_rt_multicast_event(in_dev);
723 }
724
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000725 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000726 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727
728 dev_put(dev);
729 return 0;
730}
731
Eric Dumazeta8c94862010-10-01 16:15:08 +0000732static void ipmr_cache_free_rcu(struct rcu_head *head)
733{
Yuval Mintz494fff52018-02-28 23:29:34 +0200734 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000735
Yuval Mintz494fff52018-02-28 23:29:34 +0200736 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000737}
738
Yuval Mintz8c13af22018-03-26 15:01:36 +0300739static void ipmr_cache_free(struct mfc_cache *c)
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000740{
Yuval Mintz494fff52018-02-28 23:29:34 +0200741 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000742}
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000745 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000747static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200749 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700751 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Patrick McHardy0c122952010-04-13 05:03:22 +0000753 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Yuval Mintz494fff52018-02-28 23:29:34 +0200755 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700756 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200757 struct nlmsghdr *nlh = skb_pull(skb,
758 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000760 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000762 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700763 e->error = -ETIMEDOUT;
764 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700765
Eric W. Biederman15e47302012-09-07 20:12:54 +0000766 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000767 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000769 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
771
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000772 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773}
774
Patrick McHardye258beb2010-04-13 05:03:19 +0000775/* Timer process for the unresolved queue. */
Kees Cooke99e88a2017-10-16 14:43:17 -0700776static void ipmr_expire_process(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777{
Kees Cooke99e88a2017-10-16 14:43:17 -0700778 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
Yuval Mintz494fff52018-02-28 23:29:34 +0200779 struct mr_mfc *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 unsigned long expires;
Yuval Mintz494fff52018-02-28 23:29:34 +0200781 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000784 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return;
786 }
787
Patrick McHardy0c122952010-04-13 05:03:22 +0000788 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 goto out;
790
791 now = jiffies;
792 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
Patrick McHardy0c122952010-04-13 05:03:22 +0000794 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 if (time_after(c->mfc_un.unres.expires, now)) {
796 unsigned long interval = c->mfc_un.unres.expires - now;
797 if (interval < expires)
798 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 continue;
800 }
801
Patrick McHardy862465f2010-04-13 05:03:21 +0000802 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +0200803 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
804 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 }
806
Patrick McHardy0c122952010-04-13 05:03:22 +0000807 if (!list_empty(&mrt->mfc_unres_queue))
808 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
810out:
811 spin_unlock(&mfc_unres_lock);
812}
813
814/* Fill oifs list. It is called under write locked mrt_lock. */
Yuval Mintz494fff52018-02-28 23:29:34 +0200815static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000816 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 int vifi;
819
820 cache->mfc_un.res.minvif = MAXVIFS;
821 cache->mfc_un.res.maxvif = 0;
822 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
823
Patrick McHardy0c122952010-04-13 05:03:22 +0000824 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
825 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000826 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
828 if (cache->mfc_un.res.minvif > vifi)
829 cache->mfc_un.res.minvif = vifi;
830 if (cache->mfc_un.res.maxvif <= vifi)
831 cache->mfc_un.res.maxvif = vifi + 1;
832 }
833 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200834 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Patrick McHardy0c122952010-04-13 05:03:22 +0000837static int vif_add(struct net *net, struct mr_table *mrt,
838 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 int vifi = vifc->vifc_vifi;
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200841 struct switchdev_attr attr = {
842 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
843 };
Patrick McHardy0c122952010-04-13 05:03:22 +0000844 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 struct net_device *dev;
846 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700847 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000850 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 return -EADDRINUSE;
852
853 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100855 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100856 return -EINVAL;
857 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 * All the packets will be sent to the daemon
859 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000860 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000862 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863 if (!dev)
864 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700865 err = dev_set_allmulti(dev, 1);
866 if (err) {
867 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700868 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700869 return err;
870 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900872 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000873 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (!dev)
875 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700876 err = dev_set_allmulti(dev, 1);
877 if (err) {
878 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700879 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700880 return err;
881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000883 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000885 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
886 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100887 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000888 dev_put(dev);
889 return -EADDRNOTAVAIL;
890 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000891 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000892 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (!dev)
895 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700896 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700897 if (err) {
898 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700899 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 break;
902 default:
903 return -EINVAL;
904 }
905
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000906 in_dev = __in_dev_get_rtnl(dev);
907 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000908 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000910 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700911 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700912 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
913 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 ip_rt_multicast_event(in_dev);
915
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000916 /* Fill in the VIF structures */
Yuval Mintz6853f212018-02-28 23:29:29 +0200917 vif_device_init(v, dev, vifc->vifc_rate_limit,
918 vifc->vifc_threshold,
919 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
920 (VIFF_TUNNEL | VIFF_REGISTER));
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000921
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200922 attr.orig_dev = dev;
923 if (!switchdev_port_attr_get(dev, &attr)) {
924 memcpy(v->dev_parent_id.id, attr.u.ppid.id, attr.u.ppid.id_len);
925 v->dev_parent_id.id_len = attr.u.ppid.id_len;
926 } else {
927 v->dev_parent_id.id_len = 0;
928 }
Yuval Mintz6853f212018-02-28 23:29:29 +0200929
Jianjun Kongc354e122008-11-03 00:28:02 -0800930 v->local = vifc->vifc_lcl_addr.s_addr;
931 v->remote = vifc->vifc_rmt_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
933 /* And finish update writing critical data */
934 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800935 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000936 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000937 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000938 if (vifi+1 > mrt->maxvif)
939 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +0200941 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return 0;
943}
944
Eric Dumazeta8c94862010-10-01 16:15:08 +0000945/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000946static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000947 __be32 origin,
948 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100950 struct mfc_cache_cmp_arg arg = {
951 .mfc_mcastgrp = mcastgrp,
952 .mfc_origin = origin
953 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
Yuval Mintz845c9a72018-02-28 23:29:35 +0200955 return mr_mfc_find(mrt, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000956}
957
958/* Look for a (*,G) entry */
959static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
960 __be32 mcastgrp, int vifi)
961{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100962 struct mfc_cache_cmp_arg arg = {
963 .mfc_mcastgrp = mcastgrp,
964 .mfc_origin = htonl(INADDR_ANY)
965 };
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000966
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100967 if (mcastgrp == htonl(INADDR_ANY))
Yuval Mintz845c9a72018-02-28 23:29:35 +0200968 return mr_mfc_find_any_parent(mrt, vifi);
969 return mr_mfc_find_any(mrt, vifi, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000970}
971
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100972/* Look for a (S,G,iif) entry if parent != -1 */
973static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
974 __be32 origin, __be32 mcastgrp,
975 int parent)
976{
977 struct mfc_cache_cmp_arg arg = {
978 .mfc_mcastgrp = mcastgrp,
979 .mfc_origin = origin,
980 };
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100981
Yuval Mintz845c9a72018-02-28 23:29:35 +0200982 return mr_mfc_find_parent(mrt, &arg, parent);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100983}
984
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100985/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000986static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987{
Jianjun Kongc354e122008-11-03 00:28:02 -0800988 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000989
Tom Goff70a0dec2016-06-23 16:11:57 -0400990 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +0200991 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
992 c->_c.mfc_un.res.minvif = MAXVIFS;
Yuval Mintz8c13af22018-03-26 15:01:36 +0300993 c->_c.free = ipmr_cache_free_rcu;
Yuval Mintz494fff52018-02-28 23:29:34 +0200994 refcount_set(&c->_c.mfc_un.res.refcount, 1);
Tom Goff70a0dec2016-06-23 16:11:57 -0400995 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 return c;
997}
998
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000999static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000{
Jianjun Kongc354e122008-11-03 00:28:02 -08001001 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001002
1003 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001004 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
1005 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 return c;
1008}
1009
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001010/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +00001011static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1012 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013{
1014 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001015 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001017 /* Play the pending entries through our router */
Yuval Mintz494fff52018-02-28 23:29:34 +02001018 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001019 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +02001020 struct nlmsghdr *nlh = skb_pull(skb,
1021 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Yuval Mintz7b0db852018-02-28 23:29:39 +02001023 if (mr_fill_mroute(mrt, skb, &c->_c,
1024 nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001025 nlh->nlmsg_len = skb_tail_pointer(skb) -
1026 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 } else {
1028 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +00001029 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +00001031 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001032 e->error = -EMSGSIZE;
1033 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 }
Thomas Graf2942e902006-08-15 00:30:25 -07001035
Eric W. Biederman15e47302012-09-07 20:12:54 +00001036 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001037 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001038 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041}
1042
Julien Gomes5a645dd2017-06-20 13:54:17 -07001043/* Bounce a cache query up to mrouted and netlink.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001045 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001047static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001048 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001050 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001051 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 struct igmphdr *igmp;
1053 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001054 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 int ret;
1056
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001057 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1059 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 skb = alloc_skb(128, GFP_ATOMIC);
1061
Stephen Hemminger132adf52007-03-08 20:44:43 -08001062 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 return -ENOBUFS;
1064
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001065 if (assert == IGMPMSG_WHOLEPKT || assert == IGMPMSG_WRVIFWHOLE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001067 * Duplicate old header, fix ihl, length etc.
1068 * And all this only to mangle msg->im_msgtype and
1069 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001071 skb_push(skb, sizeof(struct iphdr));
1072 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001073 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001074 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001075 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001076 msg->im_msgtype = assert;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 msg->im_mbz = 0;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001078 if (assert == IGMPMSG_WRVIFWHOLE)
1079 msg->im_vif = vifi;
1080 else
1081 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001082 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1083 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1084 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001085 } else {
1086 /* Copy the IP header */
1087 skb_set_network_header(skb, skb->len);
1088 skb_put(skb, ihl);
1089 skb_copy_to_linear_data(skb, pkt->data, ihl);
1090 /* Flag to the kernel this is a route add */
1091 ip_hdr(skb)->protocol = 0;
1092 msg = (struct igmpmsg *)skb_network_header(skb);
1093 msg->im_vif = vifi;
1094 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1095 /* Add our header */
Johannes Berg4df864c2017-06-16 14:29:21 +02001096 igmp = skb_put(skb, sizeof(struct igmphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001097 igmp->type = assert;
1098 msg->im_msgtype = assert;
1099 igmp->code = 0;
1100 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1101 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001102 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Eric Dumazet4c9687092010-10-01 16:15:01 +00001104 rcu_read_lock();
1105 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001106 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001107 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 kfree_skb(skb);
1109 return -EINVAL;
1110 }
1111
Julien Gomes5a645dd2017-06-20 13:54:17 -07001112 igmpmsg_netlink_event(mrt, skb);
1113
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001114 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001115 ret = sock_queue_rcv_skb(mroute_sk, skb);
1116 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001117 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001118 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 kfree_skb(skb);
1120 }
1121
1122 return ret;
1123}
1124
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001125/* Queue a packet for resolution. It gets locked cache entry! */
1126static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001127 struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001129 const struct iphdr *iph = ip_hdr(skb);
1130 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001131 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133
1134 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001135 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001136 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001137 c->mfc_origin == iph->saddr) {
1138 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 }
1142
Patrick McHardy862465f2010-04-13 05:03:21 +00001143 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001144 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001145 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001146 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 spin_unlock_bh(&mfc_unres_lock);
1148
1149 kfree_skb(skb);
1150 return -ENOBUFS;
1151 }
1152
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001153 /* Fill in the new cache entry */
Yuval Mintz494fff52018-02-28 23:29:34 +02001154 c->_c.mfc_parent = -1;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001155 c->mfc_origin = iph->saddr;
1156 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001158 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001159 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Yuval Mintz494fff52018-02-28 23:29:34 +02001160
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001161 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001162 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 out - Brad Parker
1164 */
1165 spin_unlock_bh(&mfc_unres_lock);
1166
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001167 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168 kfree_skb(skb);
1169 return err;
1170 }
1171
Patrick McHardy0c122952010-04-13 05:03:22 +00001172 atomic_inc(&mrt->cache_resolve_queue_len);
Yuval Mintz494fff52018-02-28 23:29:34 +02001173 list_add(&c->_c.list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001174 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
David S. Miller278554b2010-05-12 00:05:35 -07001176 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
Yuval Mintz494fff52018-02-28 23:29:34 +02001177 mod_timer(&mrt->ipmr_expire_timer,
1178 c->_c.mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 }
1180
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001181 /* See if we can append the packet */
Yuval Mintz494fff52018-02-28 23:29:34 +02001182 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 kfree_skb(skb);
1184 err = -ENOBUFS;
1185 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001186 if (dev) {
1187 skb->dev = dev;
1188 skb->skb_iif = dev->ifindex;
1189 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001190 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 err = 0;
1192 }
1193
1194 spin_unlock_bh(&mfc_unres_lock);
1195 return err;
1196}
1197
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001198/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001200static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Yotam Gigib3620532017-09-27 08:23:14 +02001202 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001203 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001205 /* The entries are added/deleted only under RTNL */
1206 rcu_read_lock();
1207 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1208 mfc->mfcc_mcastgrp.s_addr, parent);
1209 rcu_read_unlock();
1210 if (!c)
1211 return -ENOENT;
Yuval Mintz494fff52018-02-28 23:29:34 +02001212 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1213 list_del_rcu(&c->_c.list);
Yotam Gigib3620532017-09-27 08:23:14 +02001214 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001215 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yuval Mintz8c13af22018-03-26 15:01:36 +03001216 mr_cache_put(&c->_c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Patrick McHardy0c122952010-04-13 05:03:22 +00001221static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001222 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223{
Patrick McHardy862465f2010-04-13 05:03:21 +00001224 struct mfc_cache *uc, *c;
Yuval Mintz494fff52018-02-28 23:29:34 +02001225 struct mr_mfc *_uc;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001226 bool found;
1227 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
Patrick McHardya50436f22010-03-17 06:04:14 +00001229 if (mfc->mfcc_parent >= MAXVIFS)
1230 return -ENFILE;
1231
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001232 /* The entries are added/deleted only under RTNL */
1233 rcu_read_lock();
1234 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1235 mfc->mfcc_mcastgrp.s_addr, parent);
1236 rcu_read_unlock();
1237 if (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 write_lock_bh(&mrt_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001239 c->_c.mfc_parent = mfc->mfcc_parent;
1240 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001242 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +02001244 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1245 mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001246 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 return 0;
1248 }
1249
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001250 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001251 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252 return -EINVAL;
1253
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001254 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001255 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return -ENOMEM;
1257
Jianjun Kongc354e122008-11-03 00:28:02 -08001258 c->mfc_origin = mfc->mfcc_origin.s_addr;
1259 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
Yuval Mintz494fff52018-02-28 23:29:34 +02001260 c->_c.mfc_parent = mfc->mfcc_parent;
1261 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001263 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
Yuval Mintz494fff52018-02-28 23:29:34 +02001265 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001266 ipmr_rht_params);
1267 if (ret) {
1268 pr_err("ipmr: rhtable insert error %d\n", ret);
1269 ipmr_cache_free(c);
1270 return ret;
1271 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001272 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001273 /* Check to see if we resolved a queued list. If so we
1274 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001276 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001278 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1279 uc = (struct mfc_cache *)_uc;
Patrick McHardye258beb2010-04-13 05:03:19 +00001280 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001282 list_del(&_uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001283 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001284 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 break;
1286 }
1287 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001288 if (list_empty(&mrt->mfc_unres_queue))
1289 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 spin_unlock_bh(&mfc_unres_lock);
1291
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001292 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001293 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001294 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
Yotam Gigib3620532017-09-27 08:23:14 +02001296 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001297 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 return 0;
1299}
1300
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001301/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001302static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303{
Yotam Gigib3620532017-09-27 08:23:14 +02001304 struct net *net = read_pnet(&mrt->net);
Yuval Mintz494fff52018-02-28 23:29:34 +02001305 struct mr_mfc *c, *tmp;
1306 struct mfc_cache *cache;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001307 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001308 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001309
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001310 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001311 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001312 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1313 continue;
1314 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001316 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001318 /* Wipe the cache */
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001319 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1320 if (!all && (c->mfc_flags & MFC_STATIC))
1321 continue;
1322 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1323 list_del_rcu(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +02001324 cache = (struct mfc_cache *)c;
1325 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
Yotam Gigib3620532017-09-27 08:23:14 +02001326 mrt->id);
Yuval Mintz494fff52018-02-28 23:29:34 +02001327 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
Yuval Mintz8c13af22018-03-26 15:01:36 +03001328 mr_cache_put(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 }
1330
Patrick McHardy0c122952010-04-13 05:03:22 +00001331 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 spin_lock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001333 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001334 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +02001335 cache = (struct mfc_cache *)c;
1336 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1337 ipmr_destroy_unres(mrt, cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 }
1339 spin_unlock_bh(&mfc_unres_lock);
1340 }
1341}
1342
Eric Dumazet4c9687092010-10-01 16:15:01 +00001343/* called from ip_ra_control(), before an RCU grace period,
1344 * we dont need to call synchronize_rcu() here
1345 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346static void mrtsock_destruct(struct sock *sk)
1347{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001348 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001349 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001350
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001351 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001352 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001353 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001354 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001355 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1356 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001357 NETCONFA_IFINDEX_ALL,
1358 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001359 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001360 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001363 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364}
1365
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001366/* Socket options and virtual interface manipulation. The whole
1367 * virtual interface system is a complete heap, but unfortunately
1368 * that's how BSD mrouted happens to think. Maybe one day with a proper
1369 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001371
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001372int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1373 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001375 struct net *net = sock_net(sk);
1376 int val, ret = 0, parent = 0;
1377 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378 struct vifctl vif;
1379 struct mfcctl mfc;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001380 bool do_wrvifwhole;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001381 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001382
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001383 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1384 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001385 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001386 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1387 ret = -EOPNOTSUPP;
1388 goto out_unlock;
1389 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001390
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001391 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001392 if (!mrt) {
1393 ret = -ENOENT;
1394 goto out_unlock;
1395 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001396 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001397 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001398 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1399 ret = -EACCES;
1400 goto out_unlock;
1401 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403
Stephen Hemminger132adf52007-03-08 20:44:43 -08001404 switch (optname) {
1405 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001406 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001407 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001408 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001409 }
1410 if (rtnl_dereference(mrt->mroute_sk)) {
1411 ret = -EADDRINUSE;
1412 break;
1413 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001414
1415 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1416 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001417 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001418 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001419 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1420 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001421 NETCONFA_IFINDEX_ALL,
1422 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001423 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001424 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001425 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001426 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1427 ret = -EACCES;
1428 } else {
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001429 /* We need to unlock here because mrtsock_destruct takes
1430 * care of rtnl itself and we can't change that due to
1431 * the IP_ROUTER_ALERT setsockopt which runs without it.
1432 */
1433 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001434 ret = ip_ra_control(sk, 0, NULL);
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001435 goto out;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001436 }
1437 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001438 case MRT_ADD_VIF:
1439 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001440 if (optlen != sizeof(vif)) {
1441 ret = -EINVAL;
1442 break;
1443 }
1444 if (copy_from_user(&vif, optval, sizeof(vif))) {
1445 ret = -EFAULT;
1446 break;
1447 }
1448 if (vif.vifc_vifi >= MAXVIFS) {
1449 ret = -ENFILE;
1450 break;
1451 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001452 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001453 ret = vif_add(net, mrt, &vif,
1454 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001455 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001456 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001457 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001458 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001459 /* Manipulate the forwarding caches. These live
1460 * in a sort of kernel/user symbiosis.
1461 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001462 case MRT_ADD_MFC:
1463 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001464 parent = -1;
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05001465 /* fall through */
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001466 case MRT_ADD_MFC_PROXY:
1467 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001468 if (optlen != sizeof(mfc)) {
1469 ret = -EINVAL;
1470 break;
1471 }
1472 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1473 ret = -EFAULT;
1474 break;
1475 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001476 if (parent == 0)
1477 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001478 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1479 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001480 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001481 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001482 sk == rtnl_dereference(mrt->mroute_sk),
1483 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001484 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001485 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001486 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001487 if (optlen != sizeof(val)) {
1488 ret = -EINVAL;
1489 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001491 if (get_user(val, (int __user *)optval)) {
1492 ret = -EFAULT;
1493 break;
1494 }
1495 mrt->mroute_do_assert = val;
1496 break;
1497 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001498 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001499 ret = -ENOPROTOOPT;
1500 break;
1501 }
1502 if (optlen != sizeof(val)) {
1503 ret = -EINVAL;
1504 break;
1505 }
1506 if (get_user(val, (int __user *)optval)) {
1507 ret = -EFAULT;
1508 break;
1509 }
1510
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001511 do_wrvifwhole = (val == IGMPMSG_WRVIFWHOLE);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001512 val = !!val;
1513 if (val != mrt->mroute_do_pim) {
1514 mrt->mroute_do_pim = val;
1515 mrt->mroute_do_assert = val;
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001516 mrt->mroute_do_wrvifwhole = do_wrvifwhole;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001517 }
1518 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001519 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001520 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1521 ret = -ENOPROTOOPT;
1522 break;
1523 }
1524 if (optlen != sizeof(uval)) {
1525 ret = -EINVAL;
1526 break;
1527 }
1528 if (get_user(uval, (u32 __user *)optval)) {
1529 ret = -EFAULT;
1530 break;
1531 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001532
Eric Dumazet4c9687092010-10-01 16:15:01 +00001533 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1534 ret = -EBUSY;
1535 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001536 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001537 if (IS_ERR(mrt))
1538 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001539 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001540 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001541 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001542 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001543 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001544 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001545 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001547out_unlock:
1548 rtnl_unlock();
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001549out:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001550 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551}
1552
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001553/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001554int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555{
1556 int olr;
1557 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001558 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001559 struct mr_table *mrt;
1560
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001561 if (sk->sk_type != SOCK_RAW ||
1562 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1563 return -EOPNOTSUPP;
1564
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001565 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001566 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001567 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001569 switch (optname) {
1570 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001571 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001572 break;
1573 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001574 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001575 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001576 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001577 break;
1578 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001579 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001580 break;
1581 default:
1582 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001583 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001584
1585 if (get_user(olr, optlen))
1586 return -EFAULT;
1587 olr = min_t(unsigned int, olr, sizeof(int));
1588 if (olr < 0)
1589 return -EINVAL;
1590 if (put_user(olr, optlen))
1591 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001592 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 return -EFAULT;
1594 return 0;
1595}
1596
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001597/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1599{
1600 struct sioc_sg_req sr;
1601 struct sioc_vif_req vr;
1602 struct vif_device *vif;
1603 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001604 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001605 struct mr_table *mrt;
1606
1607 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001608 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001609 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001610
Stephen Hemminger132adf52007-03-08 20:44:43 -08001611 switch (cmd) {
1612 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001613 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001614 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001615 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001616 return -EINVAL;
Gustavo A. R. Silva56484512018-12-10 12:41:24 -06001617 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001618 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001619 vif = &mrt->vif_table[vr.vifi];
1620 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001621 vr.icount = vif->pkt_in;
1622 vr.ocount = vif->pkt_out;
1623 vr.ibytes = vif->bytes_in;
1624 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001626
Jianjun Kongc354e122008-11-03 00:28:02 -08001627 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001629 return 0;
1630 }
1631 read_unlock(&mrt_lock);
1632 return -EADDRNOTAVAIL;
1633 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001634 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001635 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Eric Dumazeta8c94862010-10-01 16:15:08 +00001637 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001638 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001639 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001640 sr.pktcnt = c->_c.mfc_un.res.pkt;
1641 sr.bytecnt = c->_c.mfc_un.res.bytes;
1642 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001643 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001644
Jianjun Kongc354e122008-11-03 00:28:02 -08001645 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001646 return -EFAULT;
1647 return 0;
1648 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001649 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001650 return -EADDRNOTAVAIL;
1651 default:
1652 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 }
1654}
1655
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001656#ifdef CONFIG_COMPAT
1657struct compat_sioc_sg_req {
1658 struct in_addr src;
1659 struct in_addr grp;
1660 compat_ulong_t pktcnt;
1661 compat_ulong_t bytecnt;
1662 compat_ulong_t wrong_if;
1663};
1664
David S. Millerca6b8bb02011-02-03 17:24:28 -08001665struct compat_sioc_vif_req {
1666 vifi_t vifi; /* Which iface */
1667 compat_ulong_t icount;
1668 compat_ulong_t ocount;
1669 compat_ulong_t ibytes;
1670 compat_ulong_t obytes;
1671};
1672
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001673int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1674{
David S. Miller0033d5a2011-02-03 17:21:31 -08001675 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001676 struct compat_sioc_vif_req vr;
1677 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001678 struct mfc_cache *c;
1679 struct net *net = sock_net(sk);
1680 struct mr_table *mrt;
1681
1682 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001683 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001684 return -ENOENT;
1685
1686 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001687 case SIOCGETVIFCNT:
1688 if (copy_from_user(&vr, arg, sizeof(vr)))
1689 return -EFAULT;
1690 if (vr.vifi >= mrt->maxvif)
1691 return -EINVAL;
Gustavo A. R. Silva56484512018-12-10 12:41:24 -06001692 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
David S. Millerca6b8bb02011-02-03 17:24:28 -08001693 read_lock(&mrt_lock);
1694 vif = &mrt->vif_table[vr.vifi];
1695 if (VIF_EXISTS(mrt, vr.vifi)) {
1696 vr.icount = vif->pkt_in;
1697 vr.ocount = vif->pkt_out;
1698 vr.ibytes = vif->bytes_in;
1699 vr.obytes = vif->bytes_out;
1700 read_unlock(&mrt_lock);
1701
1702 if (copy_to_user(arg, &vr, sizeof(vr)))
1703 return -EFAULT;
1704 return 0;
1705 }
1706 read_unlock(&mrt_lock);
1707 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001708 case SIOCGETSGCNT:
1709 if (copy_from_user(&sr, arg, sizeof(sr)))
1710 return -EFAULT;
1711
1712 rcu_read_lock();
1713 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1714 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001715 sr.pktcnt = c->_c.mfc_un.res.pkt;
1716 sr.bytecnt = c->_c.mfc_un.res.bytes;
1717 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001718 rcu_read_unlock();
1719
1720 if (copy_to_user(arg, &sr, sizeof(sr)))
1721 return -EFAULT;
1722 return 0;
1723 }
1724 rcu_read_unlock();
1725 return -EADDRNOTAVAIL;
1726 default:
1727 return -ENOIOCTLCMD;
1728 }
1729}
1730#endif
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1733{
Jiri Pirko351638e2013-05-28 01:30:21 +00001734 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001735 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001736 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 struct vif_device *v;
1738 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (event != NETDEV_UNREGISTER)
1741 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001742
1743 ipmr_for_each_table(mrt, net) {
1744 v = &mrt->vif_table[0];
1745 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1746 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001747 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 }
1750 return NOTIFY_DONE;
1751}
1752
Jianjun Kongc354e122008-11-03 00:28:02 -08001753static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 .notifier_call = ipmr_device_event,
1755};
1756
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001757/* Encapsulate a packet by attaching a valid IPIP header to it.
1758 * This avoids tunnel drivers and other mess and gives us the speed so
1759 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001761static void ip_encap(struct net *net, struct sk_buff *skb,
1762 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001764 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001765 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001766
1767 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001768 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001769 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001770 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001772 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001773 iph->tos = old_iph->tos;
1774 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 iph->frag_off = 0;
1776 iph->daddr = daddr;
1777 iph->saddr = saddr;
1778 iph->protocol = IPPROTO_IPIP;
1779 iph->ihl = 5;
1780 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001781 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 ip_send_check(iph);
1783
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1785 nf_reset(skb);
1786}
1787
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001788static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1789 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001791 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
David S. Miller73186df2015-11-03 13:41:45 -05001793 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1794 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 if (unlikely(opt->optlen))
1797 ip_forward_options(skb);
1798
Eric W. Biederman13206b62015-10-07 16:48:35 -05001799 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800}
1801
Yotam Gigia5bc9292017-10-03 09:58:08 +02001802#ifdef CONFIG_NET_SWITCHDEV
1803static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1804 int in_vifi, int out_vifi)
1805{
1806 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1807 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1808
Ido Schimmel875e8932018-12-04 08:15:10 +00001809 if (!skb->offload_l3_fwd_mark)
Yotam Gigia5bc9292017-10-03 09:58:08 +02001810 return false;
1811 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1812 return false;
1813 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1814 &in_vif->dev_parent_id);
1815}
1816#else
1817static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1818 int in_vifi, int out_vifi)
1819{
1820 return false;
1821}
1822#endif
1823
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001824/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Patrick McHardy0c122952010-04-13 05:03:22 +00001826static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
David Ahern6e0735d2018-12-17 15:34:48 -08001827 int in_vifi, struct sk_buff *skb, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001829 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001830 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 struct net_device *dev;
1832 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001833 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 int encap = 0;
1835
Ian Morris51456b22015-04-03 09:17:26 +01001836 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 goto out_free;
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 if (vif->flags & VIFF_REGISTER) {
1840 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001841 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001842 vif->dev->stats.tx_bytes += skb->len;
1843 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001844 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001845 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
Yotam Gigia5bc9292017-10-03 09:58:08 +02001848 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1849 goto out_free;
1850
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001851 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001852 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001853 vif->remote, vif->local,
1854 0, 0,
1855 IPPROTO_IPIP,
1856 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001857 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 goto out_free;
1859 encap = sizeof(struct iphdr);
1860 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001861 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001862 0, 0,
1863 IPPROTO_IPIP,
1864 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001865 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 goto out_free;
1867 }
1868
Changli Gaod8d1f302010-06-10 23:31:35 -07001869 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
Changli Gaod8d1f302010-06-10 23:31:35 -07001871 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001873 * allow to send ICMP, so that packets will disappear
1874 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 */
David S. Miller73186df2015-11-03 13:41:45 -05001876 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 ip_rt_put(rt);
1878 goto out_free;
1879 }
1880
Changli Gaod8d1f302010-06-10 23:31:35 -07001881 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001884 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 goto out_free;
1886 }
1887
1888 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001889 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
Eric Dumazetadf30902009-06-02 05:19:30 +00001891 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001892 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001893 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001896 * What do we do with netfilter? -- RR
1897 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001899 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001901 vif->dev->stats.tx_packets++;
1902 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001905 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001907 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 * not only before forwarding, but after forwarding on all output
1909 * interfaces. It is clear, if mrouter runs a multicasting
1910 * program, it should receive packets not depending to what interface
1911 * program is joined.
1912 * If we will not make it, the program will have to join on all
1913 * interfaces. On the other hand, multihoming host (or router, but
1914 * not mrouter) cannot join to more than one interface - it will
1915 * result in receiving multiple packets.
1916 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001917 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1918 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 ipmr_forward_finish);
1920 return;
1921
1922out_free:
1923 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924}
1925
Patrick McHardy0c122952010-04-13 05:03:22 +00001926static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927{
1928 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001929
1930 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1931 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 break;
1933 }
1934 return ct;
1935}
1936
1937/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001938static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001939 struct net_device *dev, struct sk_buff *skb,
Yuval Mintz494fff52018-02-28 23:29:34 +02001940 struct mfc_cache *c, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941{
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001942 int true_vifi = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 int psend = -1;
1944 int vif, ct;
1945
Yuval Mintz494fff52018-02-28 23:29:34 +02001946 vif = c->_c.mfc_parent;
1947 c->_c.mfc_un.res.pkt++;
1948 c->_c.mfc_un.res.bytes += skb->len;
1949 c->_c.mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
Yuval Mintz494fff52018-02-28 23:29:34 +02001951 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001952 struct mfc_cache *cache_proxy;
1953
1954 /* For an (*,G) entry, we only check that the incomming
1955 * interface is part of the static tree.
1956 */
Yuval Mintz845c9a72018-02-28 23:29:35 +02001957 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001958 if (cache_proxy &&
Yuval Mintz494fff52018-02-28 23:29:34 +02001959 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001960 goto forward;
1961 }
1962
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001963 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001964 if (mrt->vif_table[vif].dev != dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001965 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001967 * Very complicated situation...
1968 *
1969 * The best workaround until routing daemons will be
1970 * fixed is not to redistribute packet, if it was
1971 * send through wrong interface. It means, that
1972 * multicast applications WILL NOT work for
1973 * (S,G), which have default multicast route pointing
1974 * to wrong oif. In any case, it is not a good
1975 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976 */
1977 goto dont_forward;
1978 }
1979
Yuval Mintz494fff52018-02-28 23:29:34 +02001980 c->_c.mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
Patrick McHardy0c122952010-04-13 05:03:22 +00001982 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001984 * so that we cannot check that packet arrived on an oif.
1985 * It is bad, but otherwise we would need to move pretty
1986 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001988 (mrt->mroute_do_pim ||
Yuval Mintz494fff52018-02-28 23:29:34 +02001989 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001990 time_after(jiffies,
Yuval Mintz494fff52018-02-28 23:29:34 +02001991 c->_c.mfc_un.res.last_assert +
1992 MFC_ASSERT_THRESH)) {
1993 c->_c.mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001994 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03001995 if (mrt->mroute_do_wrvifwhole)
1996 ipmr_cache_report(mrt, skb, true_vifi,
1997 IGMPMSG_WRVIFWHOLE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 }
1999 goto dont_forward;
2000 }
2001
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002002forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00002003 mrt->vif_table[vif].pkt_in++;
2004 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002006 /* Forward the frame */
Yuval Mintz494fff52018-02-28 23:29:34 +02002007 if (c->mfc_origin == htonl(INADDR_ANY) &&
2008 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002009 if (true_vifi >= 0 &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002010 true_vifi != c->_c.mfc_parent &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002011 ip_hdr(skb)->ttl >
Yuval Mintz494fff52018-02-28 23:29:34 +02002012 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002013 /* It's an (*,*) entry and the packet is not coming from
2014 * the upstream: forward the packet to the upstream
2015 * only.
2016 */
Yuval Mintz494fff52018-02-28 23:29:34 +02002017 psend = c->_c.mfc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002018 goto last_forward;
2019 }
2020 goto dont_forward;
2021 }
Yuval Mintz494fff52018-02-28 23:29:34 +02002022 for (ct = c->_c.mfc_un.res.maxvif - 1;
2023 ct >= c->_c.mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002024 /* For (*,G) entry, don't forward to the incoming interface */
Yuval Mintz494fff52018-02-28 23:29:34 +02002025 if ((c->mfc_origin != htonl(INADDR_ANY) ||
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002026 ct != true_vifi) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002027 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 if (psend != -1) {
2029 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002032 ipmr_queue_xmit(net, mrt, true_vifi,
David Ahern6e0735d2018-12-17 15:34:48 -08002033 skb2, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002035 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 }
2037 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002038last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (psend != -1) {
2040 if (local) {
2041 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002042
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002044 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
David Ahern6e0735d2018-12-17 15:34:48 -08002045 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 } else {
David Ahern6e0735d2018-12-17 15:34:48 -08002047 ipmr_queue_xmit(net, mrt, true_vifi, skb, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03002048 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050 }
2051
2052dont_forward:
2053 if (!local)
2054 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055}
2056
David S. Miller417da662011-05-03 19:42:43 -07002057static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08002058{
David S. Miller417da662011-05-03 19:42:43 -07002059 struct rtable *rt = skb_rtable(skb);
2060 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05002061 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07002062 .daddr = iph->daddr,
2063 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00002064 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07002065 .flowi4_oif = (rt_is_output_route(rt) ?
2066 skb->dev->ifindex : 0),
2067 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002068 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07002069 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00002070 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08002071 };
2072 struct mr_table *mrt;
2073 int err;
2074
David S. Millerda919812011-03-12 02:04:50 -05002075 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002076 if (err)
2077 return ERR_PTR(err);
2078 return mrt;
2079}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002081/* Multicast packets for forwarding arrive here
2082 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084int ip_mr_input(struct sk_buff *skb)
2085{
2086 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002087 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00002088 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002089 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002090 struct net_device *dev;
2091
2092 /* skb->dev passed in is the loX master dev for vrfs.
2093 * As there are no vifs associated with loopback devices,
2094 * get the proper interface that does have a vif associated with it.
2095 */
2096 dev = skb->dev;
2097 if (netif_is_l3_master(skb->dev)) {
2098 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2099 if (!dev) {
2100 kfree_skb(skb);
2101 return -ENODEV;
2102 }
2103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002106 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00002108 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109 goto dont_forward;
2110
David S. Miller417da662011-05-03 19:42:43 -07002111 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002112 if (IS_ERR(mrt)) {
2113 kfree_skb(skb);
2114 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00002117 if (IPCB(skb)->opt.router_alert) {
2118 if (ip_call_ra_chain(skb))
2119 return 0;
2120 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2121 /* IGMPv1 (and broken IGMPv2 implementations sort of
2122 * Cisco IOS <= 11.2(8)) do not put router alert
2123 * option to IGMP packets destined to routable
2124 * groups. It is very bad, because it means
2125 * that we can forward NO IGMP messages.
2126 */
2127 struct sock *mroute_sk;
2128
2129 mroute_sk = rcu_dereference(mrt->mroute_sk);
2130 if (mroute_sk) {
2131 nf_reset(skb);
2132 raw_rcv(mroute_sk, skb);
2133 return 0;
2134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 }
2136 }
2137
Eric Dumazeta8c94862010-10-01 16:15:08 +00002138 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002139 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002140 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002141 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002142
2143 if (vif >= 0)
2144 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2145 vif);
2146 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002148 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002149 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150 int vif;
2151
2152 if (local) {
2153 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2154 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002155 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 skb = skb2;
2158 }
2159
Eric Dumazeta8c94862010-10-01 16:15:08 +00002160 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002161 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 if (vif >= 0) {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002163 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 read_unlock(&mrt_lock);
2165
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002166 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 }
2168 read_unlock(&mrt_lock);
2169 kfree_skb(skb);
2170 return -ENODEV;
2171 }
2172
Eric Dumazeta8c94862010-10-01 16:15:08 +00002173 read_lock(&mrt_lock);
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002174 ip_mr_forward(net, mrt, dev, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 read_unlock(&mrt_lock);
2176
2177 if (local)
2178 return ip_local_deliver(skb);
2179
2180 return 0;
2181
2182dont_forward:
2183 if (local)
2184 return ip_local_deliver(skb);
2185 kfree_skb(skb);
2186 return 0;
2187}
2188
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002189#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002190/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002191int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002192{
2193 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002194 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002195 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002196
2197 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2198 goto drop;
2199
2200 pim = igmp_hdr(skb);
2201
David S. Miller417da662011-05-03 19:42:43 -07002202 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002203 if (IS_ERR(mrt))
2204 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002205 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002206 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2207 goto drop;
2208
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002209 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002210drop:
2211 kfree_skb(skb);
2212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return 0;
2214}
2215#endif
2216
2217#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002218static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219{
2220 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002221 struct net *net = dev_net(skb->dev);
2222 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002224 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225 goto drop;
2226
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002227 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002228 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002229 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002230 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002231 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 goto drop;
2233
David S. Miller417da662011-05-03 19:42:43 -07002234 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002235 if (IS_ERR(mrt))
2236 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002237 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002238drop:
2239 kfree_skb(skb);
2240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241 return 0;
2242}
2243#endif
2244
David S. Miller9a1b9492011-05-04 12:18:54 -07002245int ipmr_get_route(struct net *net, struct sk_buff *skb,
2246 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002247 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002250 struct mr_table *mrt;
2251 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002253 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002254 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002255 return -ENOENT;
2256
Eric Dumazeta8c94862010-10-01 16:15:08 +00002257 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002258 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002259 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002260 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002262 if (vif >= 0)
2263 cache = ipmr_cache_find_any(mrt, daddr, vif);
2264 }
Ian Morris51456b22015-04-03 09:17:26 +01002265 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002266 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002267 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002269 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002272 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002273 if (dev)
2274 vif = ipmr_find_vif(mrt, dev);
2275 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002277 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return -ENODEV;
2279 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002280 skb2 = skb_clone(skb, GFP_ATOMIC);
2281 if (!skb2) {
2282 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002283 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002284 return -ENOMEM;
2285 }
2286
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002287 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002288 skb_push(skb2, sizeof(struct iphdr));
2289 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002290 iph = ip_hdr(skb2);
2291 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002292 iph->saddr = saddr;
2293 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002294 iph->version = 0;
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002295 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002297 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return err;
2299 }
2300
Eric Dumazeta8c94862010-10-01 16:15:08 +00002301 read_lock(&mrt_lock);
Yuval Mintz7b0db852018-02-28 23:29:39 +02002302 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002304 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305 return err;
2306}
2307
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002308static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002309 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2310 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002311{
2312 struct nlmsghdr *nlh;
2313 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002314 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002315
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002316 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002317 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002318 return -EMSGSIZE;
2319
2320 rtm = nlmsg_data(nlh);
2321 rtm->rtm_family = RTNL_FAMILY_IPMR;
2322 rtm->rtm_dst_len = 32;
2323 rtm->rtm_src_len = 32;
2324 rtm->rtm_tos = 0;
2325 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002326 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2327 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002328 rtm->rtm_type = RTN_MULTICAST;
2329 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Yuval Mintz494fff52018-02-28 23:29:34 +02002330 if (c->_c.mfc_flags & MFC_STATIC)
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002331 rtm->rtm_protocol = RTPROT_STATIC;
2332 else
2333 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002334 rtm->rtm_flags = 0;
2335
Jiri Benc930345e2015-03-29 16:59:25 +02002336 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2337 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002338 goto nla_put_failure;
Yuval Mintz7b0db852018-02-28 23:29:39 +02002339 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002340 /* do not break the dump if cache is unresolved */
2341 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002342 goto nla_put_failure;
2343
Johannes Berg053c0952015-01-16 22:09:00 +01002344 nlmsg_end(skb, nlh);
2345 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002346
2347nla_put_failure:
2348 nlmsg_cancel(skb, nlh);
2349 return -EMSGSIZE;
2350}
2351
Yuval Mintz7b0db852018-02-28 23:29:39 +02002352static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2353 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2354 int flags)
2355{
2356 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2357 cmd, flags);
2358}
2359
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002360static size_t mroute_msgsize(bool unresolved, int maxvif)
2361{
2362 size_t len =
2363 NLMSG_ALIGN(sizeof(struct rtmsg))
2364 + nla_total_size(4) /* RTA_TABLE */
2365 + nla_total_size(4) /* RTA_SRC */
2366 + nla_total_size(4) /* RTA_DST */
2367 ;
2368
2369 if (!unresolved)
2370 len = len
2371 + nla_total_size(4) /* RTA_IIF */
2372 + nla_total_size(0) /* RTA_MULTIPATH */
2373 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2374 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002375 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002376 ;
2377
2378 return len;
2379}
2380
2381static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2382 int cmd)
2383{
2384 struct net *net = read_pnet(&mrt->net);
2385 struct sk_buff *skb;
2386 int err = -ENOBUFS;
2387
Yuval Mintz494fff52018-02-28 23:29:34 +02002388 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2389 mrt->maxvif),
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002390 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002391 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002392 goto errout;
2393
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002394 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002395 if (err < 0)
2396 goto errout;
2397
2398 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2399 return;
2400
2401errout:
2402 kfree_skb(skb);
2403 if (err < 0)
2404 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2405}
2406
Julien Gomes5a645dd2017-06-20 13:54:17 -07002407static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2408{
2409 size_t len =
2410 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2411 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2412 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2413 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2414 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2415 /* IPMRA_CREPORT_PKT */
2416 + nla_total_size(payloadlen)
2417 ;
2418
2419 return len;
2420}
2421
2422static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2423{
2424 struct net *net = read_pnet(&mrt->net);
2425 struct nlmsghdr *nlh;
2426 struct rtgenmsg *rtgenm;
2427 struct igmpmsg *msg;
2428 struct sk_buff *skb;
2429 struct nlattr *nla;
2430 int payloadlen;
2431
2432 payloadlen = pkt->len - sizeof(struct igmpmsg);
2433 msg = (struct igmpmsg *)skb_network_header(pkt);
2434
2435 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2436 if (!skb)
2437 goto errout;
2438
2439 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2440 sizeof(struct rtgenmsg), 0);
2441 if (!nlh)
2442 goto errout;
2443 rtgenm = nlmsg_data(nlh);
2444 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2445 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2446 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2447 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2448 msg->im_src.s_addr) ||
2449 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2450 msg->im_dst.s_addr))
2451 goto nla_put_failure;
2452
2453 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2454 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2455 nla_data(nla), payloadlen))
2456 goto nla_put_failure;
2457
2458 nlmsg_end(skb, nlh);
2459
2460 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2461 return;
2462
2463nla_put_failure:
2464 nlmsg_cancel(skb, nlh);
2465errout:
2466 kfree_skb(skb);
2467 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2468}
2469
Jakub Kicinskid0440022019-01-18 10:46:20 -08002470static int ipmr_rtm_valid_getroute_req(struct sk_buff *skb,
2471 const struct nlmsghdr *nlh,
2472 struct nlattr **tb,
2473 struct netlink_ext_ack *extack)
2474{
2475 struct rtmsg *rtm;
2476 int i, err;
2477
2478 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*rtm))) {
2479 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for multicast route get request");
2480 return -EINVAL;
2481 }
2482
2483 if (!netlink_strict_get_check(skb))
2484 return nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2485 rtm_ipv4_policy, extack);
2486
2487 rtm = nlmsg_data(nlh);
2488 if ((rtm->rtm_src_len && rtm->rtm_src_len != 32) ||
2489 (rtm->rtm_dst_len && rtm->rtm_dst_len != 32) ||
2490 rtm->rtm_tos || rtm->rtm_table || rtm->rtm_protocol ||
2491 rtm->rtm_scope || rtm->rtm_type || rtm->rtm_flags) {
2492 NL_SET_ERR_MSG(extack, "ipv4: Invalid values in header for multicast route get request");
2493 return -EINVAL;
2494 }
2495
2496 err = nlmsg_parse_strict(nlh, sizeof(*rtm), tb, RTA_MAX,
2497 rtm_ipv4_policy, extack);
2498 if (err)
2499 return err;
2500
2501 if ((tb[RTA_SRC] && !rtm->rtm_src_len) ||
2502 (tb[RTA_DST] && !rtm->rtm_dst_len)) {
2503 NL_SET_ERR_MSG(extack, "ipv4: rtm_src_len and rtm_dst_len must be 32 for IPv4");
2504 return -EINVAL;
2505 }
2506
2507 for (i = 0; i <= RTA_MAX; i++) {
2508 if (!tb[i])
2509 continue;
2510
2511 switch (i) {
2512 case RTA_SRC:
2513 case RTA_DST:
2514 case RTA_TABLE:
2515 break;
2516 default:
2517 NL_SET_ERR_MSG(extack, "ipv4: Unsupported attribute in multicast route get request");
2518 return -EINVAL;
2519 }
2520 }
2521
2522 return 0;
2523}
2524
Donald Sharp4f75ba62017-06-28 13:58:57 -04002525static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2526 struct netlink_ext_ack *extack)
2527{
2528 struct net *net = sock_net(in_skb->sk);
2529 struct nlattr *tb[RTA_MAX + 1];
2530 struct sk_buff *skb = NULL;
2531 struct mfc_cache *cache;
2532 struct mr_table *mrt;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002533 __be32 src, grp;
2534 u32 tableid;
2535 int err;
2536
Jakub Kicinskid0440022019-01-18 10:46:20 -08002537 err = ipmr_rtm_valid_getroute_req(in_skb, nlh, tb, extack);
Donald Sharp4f75ba62017-06-28 13:58:57 -04002538 if (err < 0)
2539 goto errout;
2540
Donald Sharp4f75ba62017-06-28 13:58:57 -04002541 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2542 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2543 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2544
2545 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
Dan Carpenter2e3d2322017-07-12 10:56:47 +03002546 if (!mrt) {
2547 err = -ENOENT;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002548 goto errout_free;
2549 }
2550
2551 /* entries are added/deleted only under RTNL */
2552 rcu_read_lock();
2553 cache = ipmr_cache_find(mrt, src, grp);
2554 rcu_read_unlock();
2555 if (!cache) {
2556 err = -ENOENT;
2557 goto errout_free;
2558 }
2559
2560 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2561 if (!skb) {
2562 err = -ENOBUFS;
2563 goto errout_free;
2564 }
2565
2566 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2567 nlh->nlmsg_seq, cache,
2568 RTM_NEWROUTE, 0);
2569 if (err < 0)
2570 goto errout_free;
2571
2572 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2573
2574errout:
2575 return err;
2576
2577errout_free:
2578 kfree_skb(skb);
2579 goto errout;
2580}
2581
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002582static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2583{
David Ahern47246762018-10-15 18:56:42 -07002584 struct fib_dump_filter filter = {};
David Aherncb167892018-10-15 18:56:47 -07002585 int err;
David Aherne8ba3302018-10-07 20:16:35 -07002586
David Ahern47246762018-10-15 18:56:42 -07002587 if (cb->strict_check) {
David Ahern47246762018-10-15 18:56:42 -07002588 err = ip_valid_fib_dump_req(sock_net(skb->sk), cb->nlh,
David Aherneffe6792018-10-15 18:56:48 -07002589 &filter, cb);
David Aherne8ba3302018-10-07 20:16:35 -07002590 if (err < 0)
2591 return err;
2592 }
2593
David Aherncb167892018-10-15 18:56:47 -07002594 if (filter.table_id) {
2595 struct mr_table *mrt;
2596
2597 mrt = ipmr_get_table(sock_net(skb->sk), filter.table_id);
2598 if (!mrt) {
David Ahernae677bb2018-10-24 12:59:01 -07002599 if (filter.dump_all_families)
2600 return skb->len;
2601
David Aherncb167892018-10-15 18:56:47 -07002602 NL_SET_ERR_MSG(cb->extack, "ipv4: MR table does not exist");
2603 return -ENOENT;
2604 }
2605 err = mr_table_dump(mrt, skb, cb, _ipmr_fill_mroute,
2606 &mfc_unres_lock, &filter);
2607 return skb->len ? : err;
2608 }
2609
Yuval Mintz7b0db852018-02-28 23:29:39 +02002610 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
David Aherncb167892018-10-15 18:56:47 -07002611 _ipmr_fill_mroute, &mfc_unres_lock, &filter);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002612}
2613
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002614static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2615 [RTA_SRC] = { .type = NLA_U32 },
2616 [RTA_DST] = { .type = NLA_U32 },
2617 [RTA_IIF] = { .type = NLA_U32 },
2618 [RTA_TABLE] = { .type = NLA_U32 },
2619 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2620};
2621
2622static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2623{
2624 switch (rtm_protocol) {
2625 case RTPROT_STATIC:
2626 case RTPROT_MROUTED:
2627 return true;
2628 }
2629 return false;
2630}
2631
2632static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2633{
2634 struct rtnexthop *rtnh = nla_data(nla);
2635 int remaining = nla_len(nla), vifi = 0;
2636
2637 while (rtnh_ok(rtnh, remaining)) {
2638 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2639 if (++vifi == MAXVIFS)
2640 break;
2641 rtnh = rtnh_next(rtnh, &remaining);
2642 }
2643
2644 return remaining > 0 ? -EINVAL : vifi;
2645}
2646
2647/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2648static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2649 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002650 struct mr_table **mrtret,
2651 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002652{
2653 struct net_device *dev = NULL;
2654 u32 tblid = RT_TABLE_DEFAULT;
2655 struct mr_table *mrt;
2656 struct nlattr *attr;
2657 struct rtmsg *rtm;
2658 int ret, rem;
2659
Johannes Bergfceb6432017-04-12 14:34:07 +02002660 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07002661 extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002662 if (ret < 0)
2663 goto out;
2664 rtm = nlmsg_data(nlh);
2665
2666 ret = -EINVAL;
2667 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2668 rtm->rtm_type != RTN_MULTICAST ||
2669 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2670 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2671 goto out;
2672
2673 memset(mfcc, 0, sizeof(*mfcc));
2674 mfcc->mfcc_parent = -1;
2675 ret = 0;
2676 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2677 switch (nla_type(attr)) {
2678 case RTA_SRC:
2679 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2680 break;
2681 case RTA_DST:
2682 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2683 break;
2684 case RTA_IIF:
2685 dev = __dev_get_by_index(net, nla_get_u32(attr));
2686 if (!dev) {
2687 ret = -ENODEV;
2688 goto out;
2689 }
2690 break;
2691 case RTA_MULTIPATH:
2692 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2693 ret = -EINVAL;
2694 goto out;
2695 }
2696 break;
2697 case RTA_PREFSRC:
2698 ret = 1;
2699 break;
2700 case RTA_TABLE:
2701 tblid = nla_get_u32(attr);
2702 break;
2703 }
2704 }
2705 mrt = ipmr_get_table(net, tblid);
2706 if (!mrt) {
2707 ret = -ENOENT;
2708 goto out;
2709 }
2710 *mrtret = mrt;
2711 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2712 if (dev)
2713 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2714
2715out:
2716 return ret;
2717}
2718
2719/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002720static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2721 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002722{
2723 struct net *net = sock_net(skb->sk);
2724 int ret, mrtsock, parent;
2725 struct mr_table *tbl;
2726 struct mfcctl mfcc;
2727
2728 mrtsock = 0;
2729 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002730 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002731 if (ret < 0)
2732 return ret;
2733
2734 parent = ret ? mfcc.mfcc_parent : -1;
2735 if (nlh->nlmsg_type == RTM_NEWROUTE)
2736 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2737 else
2738 return ipmr_mfc_delete(tbl, &mfcc, parent);
2739}
2740
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002741static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2742{
2743 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2744
2745 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2746 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2747 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2748 mrt->mroute_reg_vif_num) ||
2749 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2750 mrt->mroute_do_assert) ||
Nikolay Aleksandrovc921c202018-07-13 12:16:43 +03002751 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim) ||
2752 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_WRVIFWHOLE,
2753 mrt->mroute_do_wrvifwhole))
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002754 return false;
2755
2756 return true;
2757}
2758
2759static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2760{
2761 struct nlattr *vif_nest;
2762 struct vif_device *vif;
2763
2764 /* if the VIF doesn't exist just continue */
2765 if (!VIF_EXISTS(mrt, vifid))
2766 return true;
2767
2768 vif = &mrt->vif_table[vifid];
2769 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2770 if (!vif_nest)
2771 return false;
2772 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2773 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2774 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2775 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2776 IPMRA_VIFA_PAD) ||
2777 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2778 IPMRA_VIFA_PAD) ||
2779 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2780 IPMRA_VIFA_PAD) ||
2781 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2782 IPMRA_VIFA_PAD) ||
2783 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2784 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2785 nla_nest_cancel(skb, vif_nest);
2786 return false;
2787 }
2788 nla_nest_end(skb, vif_nest);
2789
2790 return true;
2791}
2792
David Ahern14fc5bb2018-10-07 20:16:34 -07002793static int ipmr_valid_dumplink(const struct nlmsghdr *nlh,
2794 struct netlink_ext_ack *extack)
2795{
2796 struct ifinfomsg *ifm;
2797
2798 if (nlh->nlmsg_len < nlmsg_msg_size(sizeof(*ifm))) {
2799 NL_SET_ERR_MSG(extack, "ipv4: Invalid header for ipmr link dump");
2800 return -EINVAL;
2801 }
2802
2803 if (nlmsg_attrlen(nlh, sizeof(*ifm))) {
2804 NL_SET_ERR_MSG(extack, "Invalid data after header in ipmr link dump");
2805 return -EINVAL;
2806 }
2807
2808 ifm = nlmsg_data(nlh);
2809 if (ifm->__ifi_pad || ifm->ifi_type || ifm->ifi_flags ||
2810 ifm->ifi_change || ifm->ifi_index) {
2811 NL_SET_ERR_MSG(extack, "Invalid values in header for ipmr link dump request");
2812 return -EINVAL;
2813 }
2814
2815 return 0;
2816}
2817
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002818static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2819{
2820 struct net *net = sock_net(skb->sk);
2821 struct nlmsghdr *nlh = NULL;
2822 unsigned int t = 0, s_t;
2823 unsigned int e = 0, s_e;
2824 struct mr_table *mrt;
2825
David Ahern14fc5bb2018-10-07 20:16:34 -07002826 if (cb->strict_check) {
2827 int err = ipmr_valid_dumplink(cb->nlh, cb->extack);
2828
2829 if (err < 0)
2830 return err;
2831 }
2832
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002833 s_t = cb->args[0];
2834 s_e = cb->args[1];
2835
2836 ipmr_for_each_table(mrt, net) {
2837 struct nlattr *vifs, *af;
2838 struct ifinfomsg *hdr;
2839 u32 i;
2840
2841 if (t < s_t)
2842 goto skip_table;
2843 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2844 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2845 sizeof(*hdr), NLM_F_MULTI);
2846 if (!nlh)
2847 break;
2848
2849 hdr = nlmsg_data(nlh);
2850 memset(hdr, 0, sizeof(*hdr));
2851 hdr->ifi_family = RTNL_FAMILY_IPMR;
2852
2853 af = nla_nest_start(skb, IFLA_AF_SPEC);
2854 if (!af) {
2855 nlmsg_cancel(skb, nlh);
2856 goto out;
2857 }
2858
2859 if (!ipmr_fill_table(mrt, skb)) {
2860 nlmsg_cancel(skb, nlh);
2861 goto out;
2862 }
2863
2864 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2865 if (!vifs) {
2866 nla_nest_end(skb, af);
2867 nlmsg_end(skb, nlh);
2868 goto out;
2869 }
2870 for (i = 0; i < mrt->maxvif; i++) {
2871 if (e < s_e)
2872 goto skip_entry;
2873 if (!ipmr_fill_vif(mrt, i, skb)) {
2874 nla_nest_end(skb, vifs);
2875 nla_nest_end(skb, af);
2876 nlmsg_end(skb, nlh);
2877 goto out;
2878 }
2879skip_entry:
2880 e++;
2881 }
2882 s_e = 0;
2883 e = 0;
2884 nla_nest_end(skb, vifs);
2885 nla_nest_end(skb, af);
2886 nlmsg_end(skb, nlh);
2887skip_table:
2888 t++;
2889 }
2890
2891out:
2892 cb->args[1] = e;
2893 cb->args[0] = t;
2894
2895 return skb->len;
2896}
2897
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002898#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002899/* The /proc interfaces to multicast routing :
2900 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002901 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902
2903static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002904 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002905{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002906 struct mr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002907 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002908 struct mr_table *mrt;
2909
2910 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002911 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002912 return ERR_PTR(-ENOENT);
2913
2914 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002915
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 read_lock(&mrt_lock);
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002917 return mr_vif_seq_start(seq, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002918}
2919
2920static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002921 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
2923 read_unlock(&mrt_lock);
2924}
2925
2926static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2927{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002928 struct mr_vif_iter *iter = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002929 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002930
Linus Torvalds1da177e2005-04-16 15:20:36 -07002931 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002932 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2934 } else {
2935 const struct vif_device *vif = v;
Yuval Mintz6853f212018-02-28 23:29:29 +02002936 const char *name = vif->dev ?
2937 vif->dev->name : "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
2939 seq_printf(seq,
James Hogan91e6dd82018-01-30 09:48:02 +00002940 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002941 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002942 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 vif->bytes_out, vif->pkt_out,
2944 vif->flags, vif->local, vif->remote);
2945 }
2946 return 0;
2947}
2948
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002949static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 .start = ipmr_vif_seq_start,
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002951 .next = mr_vif_seq_next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 .stop = ipmr_vif_seq_stop,
2953 .show = ipmr_vif_seq_show,
2954};
2955
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2957{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002958 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002959 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002960
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002961 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002962 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002963 return ERR_PTR(-ENOENT);
2964
Yuval Mintzc8d61962018-02-28 23:29:36 +02002965 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966}
2967
2968static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2969{
2970 int n;
2971
2972 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002973 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002974 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2975 } else {
2976 const struct mfc_cache *mfc = v;
Yuval Mintzc8d61962018-02-28 23:29:36 +02002977 const struct mr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002978 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002979
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002980 seq_printf(seq, "%08X %08X %-3hd",
2981 (__force u32) mfc->mfc_mcastgrp,
2982 (__force u32) mfc->mfc_origin,
Yuval Mintz494fff52018-02-28 23:29:34 +02002983 mfc->_c.mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984
Patrick McHardy0c122952010-04-13 05:03:22 +00002985 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002986 seq_printf(seq, " %8lu %8lu %8lu",
Yuval Mintz494fff52018-02-28 23:29:34 +02002987 mfc->_c.mfc_un.res.pkt,
2988 mfc->_c.mfc_un.res.bytes,
2989 mfc->_c.mfc_un.res.wrong_if);
2990 for (n = mfc->_c.mfc_un.res.minvif;
2991 n < mfc->_c.mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002992 if (VIF_EXISTS(mrt, n) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002993 mfc->_c.mfc_un.res.ttls[n] < 255)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002994 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002995 " %2d:%-3d",
Yuval Mintz494fff52018-02-28 23:29:34 +02002996 n, mfc->_c.mfc_un.res.ttls[n]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002997 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002998 } else {
2999 /* unresolved mfc_caches don't contain
3000 * pkt, bytes and wrong_if values
3001 */
3002 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 }
3004 seq_putc(seq, '\n');
3005 }
3006 return 0;
3007}
3008
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003009static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003010 .start = ipmr_mfc_seq_start,
Yuval Mintzc8d61962018-02-28 23:29:36 +02003011 .next = mr_mfc_seq_next,
3012 .stop = mr_mfc_seq_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013 .show = ipmr_mfc_seq_show,
3014};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003015#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016
3017#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00003018static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07003020 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021};
3022#endif
3023
Yotam Gigi4d65b942017-09-27 08:23:13 +02003024static unsigned int ipmr_seq_read(struct net *net)
3025{
3026 ASSERT_RTNL();
3027
3028 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
3029}
3030
3031static int ipmr_dump(struct net *net, struct notifier_block *nb)
3032{
Yuval Mintzcdc9f942018-03-26 15:01:33 +03003033 return mr_dump(net, nb, RTNL_FAMILY_IPMR, ipmr_rules_dump,
3034 ipmr_mr_table_iter, &mrt_lock);
Yotam Gigi4d65b942017-09-27 08:23:13 +02003035}
3036
3037static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3038 .family = RTNL_FAMILY_IPMR,
3039 .fib_seq_read = ipmr_seq_read,
3040 .fib_dump = ipmr_dump,
3041 .owner = THIS_MODULE,
3042};
3043
Colin Ian Kingef739d82017-09-29 14:34:22 +01003044static int __net_init ipmr_notifier_init(struct net *net)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003045{
3046 struct fib_notifier_ops *ops;
3047
3048 net->ipv4.ipmr_seq = 0;
3049
3050 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3051 if (IS_ERR(ops))
3052 return PTR_ERR(ops);
3053 net->ipv4.ipmr_notifier_ops = ops;
3054
3055 return 0;
3056}
3057
3058static void __net_exit ipmr_notifier_exit(struct net *net)
3059{
3060 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3061 net->ipv4.ipmr_notifier_ops = NULL;
3062}
3063
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01003064/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00003065static int __net_init ipmr_net_init(struct net *net)
3066{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003067 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00003068
Yotam Gigi4d65b942017-09-27 08:23:13 +02003069 err = ipmr_notifier_init(net);
3070 if (err)
3071 goto ipmr_notifier_fail;
3072
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003073 err = ipmr_rules_init(net);
3074 if (err < 0)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003075 goto ipmr_rules_fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003076
3077#ifdef CONFIG_PROC_FS
3078 err = -ENOMEM;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003079 if (!proc_create_net("ip_mr_vif", 0, net->proc_net, &ipmr_vif_seq_ops,
3080 sizeof(struct mr_vif_iter)))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003081 goto proc_vif_fail;
Christoph Hellwigc3506372018-04-10 19:42:55 +02003082 if (!proc_create_net("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_seq_ops,
3083 sizeof(struct mr_mfc_iter)))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003084 goto proc_cache_fail;
3085#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00003086 return 0;
3087
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003088#ifdef CONFIG_PROC_FS
3089proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00003090 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003091proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003092 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003093#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003094ipmr_rules_fail:
3095 ipmr_notifier_exit(net);
3096ipmr_notifier_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003097 return err;
3098}
3099
3100static void __net_exit ipmr_net_exit(struct net *net)
3101{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003102#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003103 remove_proc_entry("ip_mr_cache", net->proc_net);
3104 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003105#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003106 ipmr_notifier_exit(net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003107 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00003108}
3109
3110static struct pernet_operations ipmr_net_ops = {
3111 .init = ipmr_net_init,
3112 .exit = ipmr_net_exit,
3113};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003114
Wang Chen03d2f892008-07-03 12:13:36 +08003115int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116{
Wang Chen03d2f892008-07-03 12:13:36 +08003117 int err;
3118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3120 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00003121 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003122 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08003123
Benjamin Therycf958ae32009-01-22 04:56:16 +00003124 err = register_pernet_subsys(&ipmr_net_ops);
3125 if (err)
3126 goto reg_pernet_fail;
3127
Wang Chen03d2f892008-07-03 12:13:36 +08003128 err = register_netdevice_notifier(&ip_mr_notifier);
3129 if (err)
3130 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07003131#ifdef CONFIG_IP_PIMSM_V2
3132 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00003133 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07003134 err = -EAGAIN;
3135 goto add_proto_fail;
3136 }
3137#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00003138 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003139 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003140 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003141 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003142 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003143 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03003144
3145 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
Florian Westphalb97bac62017-08-09 20:41:48 +02003146 NULL, ipmr_rtm_dumplink, 0);
Wang Chen03d2f892008-07-03 12:13:36 +08003147 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003148
Tom Goff403dbb92009-06-14 03:16:13 -07003149#ifdef CONFIG_IP_PIMSM_V2
3150add_proto_fail:
3151 unregister_netdevice_notifier(&ip_mr_notifier);
3152#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08003153reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003154 unregister_pernet_subsys(&ipmr_net_ops);
3155reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08003156 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08003157 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158}