blob: f6be5db16da2640a44032770ca3ef8b90bc14b29 [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>
Pravin B Shelarc5441932013-03-25 14:49:35 +000063#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070065#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000066#include <net/fib_rules.h>
Nicolas Dichteld67b8c62012-12-04 01:13:35 +000067#include <linux/netconf.h>
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +010068#include <net/nexthop.h>
Yotam Gigi5d8b3e62017-10-03 09:58:07 +020069#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Patrick McHardyf0ad0862010-04-13 05:03:23 +000071struct ipmr_rule {
72 struct fib_rule common;
73};
74
75struct ipmr_result {
76 struct mr_table *mrt;
77};
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000080 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static DEFINE_RWLOCK(mrt_lock);
84
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +010085/* Multicast router control variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Special spinlock for queue of unresolved entries */
88static DEFINE_SPINLOCK(mfc_unres_lock);
89
90/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000091 * entries is changed only in process context and protected
92 * with weak lock mrt_lock. Queue of unresolved entries is protected
93 * with strong spinlock mfc_unres_lock.
94 *
95 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
97
Alexey Dobriyan08009a72018-02-24 21:20:33 +030098static struct kmem_cache *mrt_cachep __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000100static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000101static void ipmr_free_table(struct mr_table *mrt);
102
Rami Rosenc4854ec2013-07-20 15:09:28 +0300103static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -0400104 struct net_device *dev, struct sk_buff *skb,
105 struct mfc_cache *cache, int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000106static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000107 struct sk_buff *pkt, vifi_t vifi, int assert);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000108static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
109 int cmd);
Julien Gomes5a645dd2017-06-20 13:54:17 -0700110static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100111static void mroute_clean_tables(struct mr_table *mrt, bool all);
Kees Cooke99e88a2017-10-16 14:43:17 -0700112static void ipmr_expire_process(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000114#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
115#define ipmr_for_each_table(mrt, net) \
116 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
117
Yuval Mintz7b0db852018-02-28 23:29:39 +0200118static struct mr_table *ipmr_mr_table_iter(struct net *net,
119 struct mr_table *mrt)
120{
121 struct mr_table *ret;
122
123 if (!mrt)
124 ret = list_entry_rcu(net->ipv4.mr_tables.next,
125 struct mr_table, list);
126 else
127 ret = list_entry_rcu(mrt->list.next,
128 struct mr_table, list);
129
130 if (&ret->list == &net->ipv4.mr_tables)
131 return NULL;
132 return ret;
133}
134
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000135static struct mr_table *ipmr_get_table(struct net *net, u32 id)
136{
137 struct mr_table *mrt;
138
139 ipmr_for_each_table(mrt, net) {
140 if (mrt->id == id)
141 return mrt;
142 }
143 return NULL;
144}
145
David S. Millerda919812011-03-12 02:04:50 -0500146static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000147 struct mr_table **mrt)
148{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000149 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100150 struct ipmr_result res;
151 struct fib_lookup_arg arg = {
152 .result = &res,
153 .flags = FIB_LOOKUP_NOREF,
154 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000155
David Aherne58e4152016-10-31 15:54:00 -0700156 /* update flow if oif or iif point to device enslaved to l3mdev */
157 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
158
David S. Millerda919812011-03-12 02:04:50 -0500159 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
160 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000161 if (err < 0)
162 return err;
163 *mrt = res.mrt;
164 return 0;
165}
166
167static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
168 int flags, struct fib_lookup_arg *arg)
169{
170 struct ipmr_result *res = arg->result;
171 struct mr_table *mrt;
172
173 switch (rule->action) {
174 case FR_ACT_TO_TBL:
175 break;
176 case FR_ACT_UNREACHABLE:
177 return -ENETUNREACH;
178 case FR_ACT_PROHIBIT:
179 return -EACCES;
180 case FR_ACT_BLACKHOLE:
181 default:
182 return -EINVAL;
183 }
184
David Aherne58e4152016-10-31 15:54:00 -0700185 arg->table = fib_rule_get_table(rule, arg);
186
187 mrt = ipmr_get_table(rule->fr_net, arg->table);
Ian Morris51456b22015-04-03 09:17:26 +0100188 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000189 return -EAGAIN;
190 res->mrt = mrt;
191 return 0;
192}
193
194static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
195{
196 return 1;
197}
198
199static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
200 FRA_GENERIC_POLICY,
201};
202
203static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
204 struct fib_rule_hdr *frh, struct nlattr **tb)
205{
206 return 0;
207}
208
209static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
210 struct nlattr **tb)
211{
212 return 1;
213}
214
215static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
216 struct fib_rule_hdr *frh)
217{
218 frh->dst_len = 0;
219 frh->src_len = 0;
220 frh->tos = 0;
221 return 0;
222}
223
Andi Kleen04a6f822012-10-04 17:12:11 -0700224static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200225 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000226 .rule_size = sizeof(struct ipmr_rule),
227 .addr_size = sizeof(u32),
228 .action = ipmr_rule_action,
229 .match = ipmr_rule_match,
230 .configure = ipmr_rule_configure,
231 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000232 .fill = ipmr_rule_fill,
233 .nlgroup = RTNLGRP_IPV4_RULE,
234 .policy = ipmr_rule_policy,
235 .owner = THIS_MODULE,
236};
237
238static int __net_init ipmr_rules_init(struct net *net)
239{
240 struct fib_rules_ops *ops;
241 struct mr_table *mrt;
242 int err;
243
244 ops = fib_rules_register(&ipmr_rules_ops_template, net);
245 if (IS_ERR(ops))
246 return PTR_ERR(ops);
247
248 INIT_LIST_HEAD(&net->ipv4.mr_tables);
249
250 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100251 if (IS_ERR(mrt)) {
252 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000253 goto err1;
254 }
255
256 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
257 if (err < 0)
258 goto err2;
259
260 net->ipv4.mr_rules_ops = ops;
261 return 0;
262
263err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700264 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000265err1:
266 fib_rules_unregister(ops);
267 return err;
268}
269
270static void __net_exit ipmr_rules_exit(struct net *net)
271{
272 struct mr_table *mrt, *next;
273
WANG Conged785302015-03-31 11:01:45 -0700274 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000275 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
276 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000277 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000278 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000279 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700280 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000281}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200282
283static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
284{
285 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR);
286}
287
288static unsigned int ipmr_rules_seq_read(struct net *net)
289{
290 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
291}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200292
293bool ipmr_rule_default(const struct fib_rule *rule)
294{
295 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
296}
297EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000298#else
299#define ipmr_for_each_table(mrt, net) \
300 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
301
Yuval Mintz7b0db852018-02-28 23:29:39 +0200302static struct mr_table *ipmr_mr_table_iter(struct net *net,
303 struct mr_table *mrt)
304{
305 if (!mrt)
306 return net->ipv4.mrt;
307 return NULL;
308}
309
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000310static struct mr_table *ipmr_get_table(struct net *net, u32 id)
311{
312 return net->ipv4.mrt;
313}
314
David S. Millerda919812011-03-12 02:04:50 -0500315static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000316 struct mr_table **mrt)
317{
318 *mrt = net->ipv4.mrt;
319 return 0;
320}
321
322static int __net_init ipmr_rules_init(struct net *net)
323{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100324 struct mr_table *mrt;
325
326 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
327 if (IS_ERR(mrt))
328 return PTR_ERR(mrt);
329 net->ipv4.mrt = mrt;
330 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000331}
332
333static void __net_exit ipmr_rules_exit(struct net *net)
334{
WANG Conged785302015-03-31 11:01:45 -0700335 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000336 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700337 net->ipv4.mrt = NULL;
338 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000339}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200340
341static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
342{
343 return 0;
344}
345
346static unsigned int ipmr_rules_seq_read(struct net *net)
347{
348 return 0;
349}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200350
351bool ipmr_rule_default(const struct fib_rule *rule)
352{
353 return true;
354}
355EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000356#endif
357
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100358static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
359 const void *ptr)
360{
361 const struct mfc_cache_cmp_arg *cmparg = arg->key;
362 struct mfc_cache *c = (struct mfc_cache *)ptr;
363
364 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
365 cmparg->mfc_origin != c->mfc_origin;
366}
367
368static const struct rhashtable_params ipmr_rht_params = {
Yuval Mintz494fff52018-02-28 23:29:34 +0200369 .head_offset = offsetof(struct mr_mfc, mnode),
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100370 .key_offset = offsetof(struct mfc_cache, cmparg),
371 .key_len = sizeof(struct mfc_cache_cmp_arg),
372 .nelem_hint = 3,
373 .locks_mul = 1,
374 .obj_cmpfn = ipmr_hash_cmp,
375 .automatic_shrinking = true,
376};
377
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200378static void ipmr_new_table_set(struct mr_table *mrt,
379 struct net *net)
380{
381#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
382 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
383#endif
384}
385
Yuval Mintz845c9a72018-02-28 23:29:35 +0200386static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
387 .mfc_mcastgrp = htonl(INADDR_ANY),
388 .mfc_origin = htonl(INADDR_ANY),
389};
390
391static struct mr_table_ops ipmr_mr_table_ops = {
392 .rht_params = &ipmr_rht_params,
393 .cmparg_any = &ipmr_mr_table_ops_cmparg_any,
394};
395
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000396static struct mr_table *ipmr_new_table(struct net *net, u32 id)
397{
398 struct mr_table *mrt;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000399
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100400 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
401 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
402 return ERR_PTR(-EINVAL);
403
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000404 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100405 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000406 return mrt;
407
Yuval Mintz845c9a72018-02-28 23:29:35 +0200408 return mr_table_alloc(net, id, &ipmr_mr_table_ops,
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200409 ipmr_expire_process, ipmr_new_table_set);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000410}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000412static void ipmr_free_table(struct mr_table *mrt)
413{
414 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100415 mroute_clean_tables(mrt, true);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100416 rhltable_destroy(&mrt->mfc_hash);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000417 kfree(mrt);
418}
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
421
Wang Chend6070322008-07-14 20:55:26 -0700422static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
423{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000424 struct net *net = dev_net(dev);
425
Wang Chend6070322008-07-14 20:55:26 -0700426 dev_close(dev);
427
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000428 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700429 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800430 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700431 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700432 struct ip_tunnel_parm p;
433
434 memset(&p, 0, sizeof(p));
435 p.iph.daddr = v->vifc_rmt_addr.s_addr;
436 p.iph.saddr = v->vifc_lcl_addr.s_addr;
437 p.iph.version = 4;
438 p.iph.ihl = 5;
439 p.iph.protocol = IPPROTO_IPIP;
440 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
441 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
442
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800443 if (ops->ndo_do_ioctl) {
444 mm_segment_t oldfs = get_fs();
445
446 set_fs(KERNEL_DS);
447 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
448 set_fs(oldfs);
449 }
Wang Chend6070322008-07-14 20:55:26 -0700450 }
451}
452
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100453/* Initialize ipmr pimreg/tunnel in_device */
454static bool ipmr_init_vif_indev(const struct net_device *dev)
455{
456 struct in_device *in_dev;
457
458 ASSERT_RTNL();
459
460 in_dev = __in_dev_get_rtnl(dev);
461 if (!in_dev)
462 return false;
463 ipv4_devconf_setall(in_dev);
464 neigh_parms_data_state_setall(in_dev->arp_parms);
465 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
466
467 return true;
468}
469
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100470static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471{
472 struct net_device *dev;
473
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000474 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475
476 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800477 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 int err;
479 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
482 memset(&p, 0, sizeof(p));
483 p.iph.daddr = v->vifc_rmt_addr.s_addr;
484 p.iph.saddr = v->vifc_lcl_addr.s_addr;
485 p.iph.version = 4;
486 p.iph.ihl = 5;
487 p.iph.protocol = IPPROTO_IPIP;
488 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800489 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800491 if (ops->ndo_do_ioctl) {
492 mm_segment_t oldfs = get_fs();
493
494 set_fs(KERNEL_DS);
495 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
496 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000497 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800498 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000499 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 dev = NULL;
501
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000502 if (err == 0 &&
503 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100505 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (dev_open(dev))
508 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700509 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 }
511 }
512 return dev;
513
514failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 unregister_netdevice(dev);
516 return NULL;
517}
518
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100519#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000520static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000522 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000523 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500524 struct flowi4 fl4 = {
525 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700526 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500527 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000528 };
529 int err;
530
David S. Millerda919812011-03-12 02:04:50 -0500531 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000532 if (err < 0) {
533 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000534 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000535 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700538 dev->stats.tx_bytes += skb->len;
539 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000540 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 read_unlock(&mrt_lock);
542 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000543 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200546static int reg_vif_get_iflink(const struct net_device *dev)
547{
548 return 0;
549}
550
Stephen Hemminger007c3832008-11-20 20:28:35 -0800551static const struct net_device_ops reg_vif_netdev_ops = {
552 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200553 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800554};
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556static void reg_vif_setup(struct net_device *dev)
557{
558 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800559 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530561 dev->netdev_ops = &reg_vif_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400562 dev->needs_free_netdev = true;
Tom Goff403dbb92009-06-14 03:16:13 -0700563 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564}
565
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000566static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567{
568 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000569 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000571 if (mrt->id == RT_TABLE_DEFAULT)
572 sprintf(name, "pimreg");
573 else
574 sprintf(name, "pimreg%u", mrt->id);
575
Tom Gundersenc835a672014-07-14 16:37:24 +0200576 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Ian Morris51456b22015-04-03 09:17:26 +0100578 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 return NULL;
580
Tom Goff403dbb92009-06-14 03:16:13 -0700581 dev_net_set(dev, net);
582
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 if (register_netdevice(dev)) {
584 free_netdev(dev);
585 return NULL;
586 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100588 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 if (dev_open(dev))
591 goto failure;
592
Wang Chen7dc00c82008-07-14 20:56:34 -0700593 dev_hold(dev);
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 return dev;
596
597failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 unregister_netdevice(dev);
599 return NULL;
600}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100601
602/* called with rcu_read_lock() */
603static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
604 unsigned int pimlen)
605{
606 struct net_device *reg_dev = NULL;
607 struct iphdr *encap;
608
609 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100610 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100611 * a. packet is really sent to a multicast group
612 * b. packet is not a NULL-REGISTER
613 * c. packet is not truncated
614 */
615 if (!ipv4_is_multicast(encap->daddr) ||
616 encap->tot_len == 0 ||
617 ntohs(encap->tot_len) + pimlen > skb->len)
618 return 1;
619
620 read_lock(&mrt_lock);
621 if (mrt->mroute_reg_vif_num >= 0)
622 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
623 read_unlock(&mrt_lock);
624
625 if (!reg_dev)
626 return 1;
627
628 skb->mac_header = skb->network_header;
629 skb_pull(skb, (u8 *)encap - skb->data);
630 skb_reset_network_header(skb);
631 skb->protocol = htons(ETH_P_IP);
632 skb->ip_summed = CHECKSUM_NONE;
633
634 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
635
636 netif_rx(skb);
637
638 return NET_RX_SUCCESS;
639}
640#else
641static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
642{
643 return NULL;
644}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645#endif
646
Yotam Gigi4d65b942017-09-27 08:23:13 +0200647static int call_ipmr_vif_entry_notifier(struct notifier_block *nb,
648 struct net *net,
649 enum fib_event_type event_type,
650 struct vif_device *vif,
651 vifi_t vif_index, u32 tb_id)
652{
653 struct vif_entry_notifier_info info = {
654 .info = {
655 .family = RTNL_FAMILY_IPMR,
656 .net = net,
657 },
658 .dev = vif->dev,
659 .vif_index = vif_index,
660 .vif_flags = vif->flags,
661 .tb_id = tb_id,
662 };
663
664 return call_fib_notifier(nb, net, event_type, &info.info);
665}
666
Yotam Gigib3620532017-09-27 08:23:14 +0200667static int call_ipmr_vif_entry_notifiers(struct net *net,
668 enum fib_event_type event_type,
669 struct vif_device *vif,
670 vifi_t vif_index, u32 tb_id)
671{
672 struct vif_entry_notifier_info info = {
673 .info = {
674 .family = RTNL_FAMILY_IPMR,
675 .net = net,
676 },
677 .dev = vif->dev,
678 .vif_index = vif_index,
679 .vif_flags = vif->flags,
680 .tb_id = tb_id,
681 };
682
683 ASSERT_RTNL();
684 net->ipv4.ipmr_seq++;
685 return call_fib_notifiers(net, event_type, &info.info);
686}
687
Yotam Gigi4d65b942017-09-27 08:23:13 +0200688static int call_ipmr_mfc_entry_notifier(struct notifier_block *nb,
689 struct net *net,
690 enum fib_event_type event_type,
691 struct mfc_cache *mfc, u32 tb_id)
692{
693 struct mfc_entry_notifier_info info = {
694 .info = {
695 .family = RTNL_FAMILY_IPMR,
696 .net = net,
697 },
698 .mfc = mfc,
699 .tb_id = tb_id
700 };
701
702 return call_fib_notifier(nb, net, event_type, &info.info);
703}
704
Yotam Gigib3620532017-09-27 08:23:14 +0200705static int call_ipmr_mfc_entry_notifiers(struct net *net,
706 enum fib_event_type event_type,
707 struct mfc_cache *mfc, u32 tb_id)
708{
709 struct mfc_entry_notifier_info info = {
710 .info = {
711 .family = RTNL_FAMILY_IPMR,
712 .net = net,
713 },
714 .mfc = mfc,
715 .tb_id = tb_id
716 };
717
718 ASSERT_RTNL();
719 net->ipv4.ipmr_seq++;
720 return call_fib_notifiers(net, event_type, &info.info);
721}
722
Ben Hutchings2c530402012-07-10 10:55:09 +0000723/**
724 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700725 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000727static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000728 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729{
Yotam Gigib3620532017-09-27 08:23:14 +0200730 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 struct vif_device *v;
732 struct net_device *dev;
733 struct in_device *in_dev;
734
Patrick McHardy0c122952010-04-13 05:03:22 +0000735 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 return -EADDRNOTAVAIL;
737
Patrick McHardy0c122952010-04-13 05:03:22 +0000738 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Yotam Gigib3620532017-09-27 08:23:14 +0200740 if (VIF_EXISTS(mrt, vifi))
741 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
742 mrt->id);
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 write_lock_bh(&mrt_lock);
745 dev = v->dev;
746 v->dev = NULL;
747
748 if (!dev) {
749 write_unlock_bh(&mrt_lock);
750 return -EADDRNOTAVAIL;
751 }
752
Patrick McHardy0c122952010-04-13 05:03:22 +0000753 if (vifi == mrt->mroute_reg_vif_num)
754 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000756 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000758
759 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000760 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
762 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000763 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765
766 write_unlock_bh(&mrt_lock);
767
768 dev_set_allmulti(dev, -1);
769
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000770 in_dev = __in_dev_get_rtnl(dev);
771 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700772 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -0700773 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000774 NETCONFA_MC_FORWARDING,
775 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 ip_rt_multicast_event(in_dev);
777 }
778
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000779 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000780 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
782 dev_put(dev);
783 return 0;
784}
785
Eric Dumazeta8c94862010-10-01 16:15:08 +0000786static void ipmr_cache_free_rcu(struct rcu_head *head)
787{
Yuval Mintz494fff52018-02-28 23:29:34 +0200788 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000789
Yuval Mintz494fff52018-02-28 23:29:34 +0200790 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000791}
792
Yotam Gigi310ebbb2017-09-27 08:23:12 +0200793void ipmr_cache_free(struct mfc_cache *c)
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000794{
Yuval Mintz494fff52018-02-28 23:29:34 +0200795 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000796}
Yotam Gigi310ebbb2017-09-27 08:23:12 +0200797EXPORT_SYMBOL(ipmr_cache_free);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000800 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000802static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200804 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700806 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Patrick McHardy0c122952010-04-13 05:03:22 +0000808 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Yuval Mintz494fff52018-02-28 23:29:34 +0200810 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700811 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200812 struct nlmsghdr *nlh = skb_pull(skb,
813 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000815 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000817 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700818 e->error = -ETIMEDOUT;
819 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700820
Eric W. Biederman15e47302012-09-07 20:12:54 +0000821 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000822 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
826
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000827 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828}
829
Patrick McHardye258beb2010-04-13 05:03:19 +0000830/* Timer process for the unresolved queue. */
Kees Cooke99e88a2017-10-16 14:43:17 -0700831static void ipmr_expire_process(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832{
Kees Cooke99e88a2017-10-16 14:43:17 -0700833 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
Yuval Mintz494fff52018-02-28 23:29:34 +0200834 struct mr_mfc *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 unsigned long expires;
Yuval Mintz494fff52018-02-28 23:29:34 +0200836 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
838 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000839 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return;
841 }
842
Patrick McHardy0c122952010-04-13 05:03:22 +0000843 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 goto out;
845
846 now = jiffies;
847 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Patrick McHardy0c122952010-04-13 05:03:22 +0000849 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (time_after(c->mfc_un.unres.expires, now)) {
851 unsigned long interval = c->mfc_un.unres.expires - now;
852 if (interval < expires)
853 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 continue;
855 }
856
Patrick McHardy862465f2010-04-13 05:03:21 +0000857 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +0200858 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
859 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 }
861
Patrick McHardy0c122952010-04-13 05:03:22 +0000862 if (!list_empty(&mrt->mfc_unres_queue))
863 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865out:
866 spin_unlock(&mfc_unres_lock);
867}
868
869/* Fill oifs list. It is called under write locked mrt_lock. */
Yuval Mintz494fff52018-02-28 23:29:34 +0200870static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000871 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
873 int vifi;
874
875 cache->mfc_un.res.minvif = MAXVIFS;
876 cache->mfc_un.res.maxvif = 0;
877 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
878
Patrick McHardy0c122952010-04-13 05:03:22 +0000879 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
880 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000881 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
883 if (cache->mfc_un.res.minvif > vifi)
884 cache->mfc_un.res.minvif = vifi;
885 if (cache->mfc_un.res.maxvif <= vifi)
886 cache->mfc_un.res.maxvif = vifi + 1;
887 }
888 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200889 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
Patrick McHardy0c122952010-04-13 05:03:22 +0000892static int vif_add(struct net *net, struct mr_table *mrt,
893 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 int vifi = vifc->vifc_vifi;
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200896 struct switchdev_attr attr = {
897 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
898 };
Patrick McHardy0c122952010-04-13 05:03:22 +0000899 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 struct net_device *dev;
901 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700902 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000905 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 return -EADDRINUSE;
907
908 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100910 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100911 return -EINVAL;
912 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 * All the packets will be sent to the daemon
914 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000915 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000917 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 if (!dev)
919 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700920 err = dev_set_allmulti(dev, 1);
921 if (err) {
922 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700923 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700924 return err;
925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900927 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000928 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!dev)
930 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700931 err = dev_set_allmulti(dev, 1);
932 if (err) {
933 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700934 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700935 return err;
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000938 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000940 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
941 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100942 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000943 dev_put(dev);
944 return -EADDRNOTAVAIL;
945 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000946 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000947 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 if (!dev)
950 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700951 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700952 if (err) {
953 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700954 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 break;
957 default:
958 return -EINVAL;
959 }
960
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000961 in_dev = __in_dev_get_rtnl(dev);
962 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000963 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000965 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700966 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700967 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
968 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 ip_rt_multicast_event(in_dev);
970
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000971 /* Fill in the VIF structures */
Yuval Mintz6853f212018-02-28 23:29:29 +0200972 vif_device_init(v, dev, vifc->vifc_rate_limit,
973 vifc->vifc_threshold,
974 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
975 (VIFF_TUNNEL | VIFF_REGISTER));
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000976
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200977 attr.orig_dev = dev;
978 if (!switchdev_port_attr_get(dev, &attr)) {
979 memcpy(v->dev_parent_id.id, attr.u.ppid.id, attr.u.ppid.id_len);
980 v->dev_parent_id.id_len = attr.u.ppid.id_len;
981 } else {
982 v->dev_parent_id.id_len = 0;
983 }
Yuval Mintz6853f212018-02-28 23:29:29 +0200984
Jianjun Kongc354e122008-11-03 00:28:02 -0800985 v->local = vifc->vifc_lcl_addr.s_addr;
986 v->remote = vifc->vifc_rmt_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987
988 /* And finish update writing critical data */
989 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800990 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000991 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000992 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000993 if (vifi+1 > mrt->maxvif)
994 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +0200996 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 return 0;
998}
999
Eric Dumazeta8c94862010-10-01 16:15:08 +00001000/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001001static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001002 __be32 origin,
1003 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001005 struct mfc_cache_cmp_arg arg = {
1006 .mfc_mcastgrp = mcastgrp,
1007 .mfc_origin = origin
1008 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009
Yuval Mintz845c9a72018-02-28 23:29:35 +02001010 return mr_mfc_find(mrt, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001011}
1012
1013/* Look for a (*,G) entry */
1014static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
1015 __be32 mcastgrp, int vifi)
1016{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001017 struct mfc_cache_cmp_arg arg = {
1018 .mfc_mcastgrp = mcastgrp,
1019 .mfc_origin = htonl(INADDR_ANY)
1020 };
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001021
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001022 if (mcastgrp == htonl(INADDR_ANY))
Yuval Mintz845c9a72018-02-28 23:29:35 +02001023 return mr_mfc_find_any_parent(mrt, vifi);
1024 return mr_mfc_find_any(mrt, vifi, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001025}
1026
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001027/* Look for a (S,G,iif) entry if parent != -1 */
1028static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
1029 __be32 origin, __be32 mcastgrp,
1030 int parent)
1031{
1032 struct mfc_cache_cmp_arg arg = {
1033 .mfc_mcastgrp = mcastgrp,
1034 .mfc_origin = origin,
1035 };
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001036
Yuval Mintz845c9a72018-02-28 23:29:35 +02001037 return mr_mfc_find_parent(mrt, &arg, parent);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001038}
1039
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001040/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001041static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042{
Jianjun Kongc354e122008-11-03 00:28:02 -08001043 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001044
Tom Goff70a0dec2016-06-23 16:11:57 -04001045 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001046 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
1047 c->_c.mfc_un.res.minvif = MAXVIFS;
1048 refcount_set(&c->_c.mfc_un.res.refcount, 1);
Tom Goff70a0dec2016-06-23 16:11:57 -04001049 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return c;
1051}
1052
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001053static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054{
Jianjun Kongc354e122008-11-03 00:28:02 -08001055 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001056
1057 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001058 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
1059 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001060 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 return c;
1062}
1063
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001064/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +00001065static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1066 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067{
1068 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001069 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001071 /* Play the pending entries through our router */
Yuval Mintz494fff52018-02-28 23:29:34 +02001072 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001073 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +02001074 struct nlmsghdr *nlh = skb_pull(skb,
1075 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
Yuval Mintz7b0db852018-02-28 23:29:39 +02001077 if (mr_fill_mroute(mrt, skb, &c->_c,
1078 nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001079 nlh->nlmsg_len = skb_tail_pointer(skb) -
1080 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 } else {
1082 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +00001083 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +00001085 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001086 e->error = -EMSGSIZE;
1087 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 }
Thomas Graf2942e902006-08-15 00:30:25 -07001089
Eric W. Biederman15e47302012-09-07 20:12:54 +00001090 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001091 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001092 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
1095}
1096
Julien Gomes5a645dd2017-06-20 13:54:17 -07001097/* Bounce a cache query up to mrouted and netlink.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001099 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001101static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001102 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001104 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001105 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 struct igmphdr *igmp;
1107 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001108 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 int ret;
1110
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (assert == IGMPMSG_WHOLEPKT)
1112 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1113 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 skb = alloc_skb(128, GFP_ATOMIC);
1115
Stephen Hemminger132adf52007-03-08 20:44:43 -08001116 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return -ENOBUFS;
1118
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 if (assert == IGMPMSG_WHOLEPKT) {
1120 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001121 * Duplicate old header, fix ihl, length etc.
1122 * And all this only to mangle msg->im_msgtype and
1123 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001125 skb_push(skb, sizeof(struct iphdr));
1126 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001127 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001128 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001129 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1131 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001132 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001133 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1134 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1135 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001136 } else {
1137 /* Copy the IP header */
1138 skb_set_network_header(skb, skb->len);
1139 skb_put(skb, ihl);
1140 skb_copy_to_linear_data(skb, pkt->data, ihl);
1141 /* Flag to the kernel this is a route add */
1142 ip_hdr(skb)->protocol = 0;
1143 msg = (struct igmpmsg *)skb_network_header(skb);
1144 msg->im_vif = vifi;
1145 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1146 /* Add our header */
Johannes Berg4df864c2017-06-16 14:29:21 +02001147 igmp = skb_put(skb, sizeof(struct igmphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001148 igmp->type = assert;
1149 msg->im_msgtype = assert;
1150 igmp->code = 0;
1151 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1152 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
Eric Dumazet4c9687092010-10-01 16:15:01 +00001155 rcu_read_lock();
1156 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001157 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001158 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 kfree_skb(skb);
1160 return -EINVAL;
1161 }
1162
Julien Gomes5a645dd2017-06-20 13:54:17 -07001163 igmpmsg_netlink_event(mrt, skb);
1164
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001165 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001166 ret = sock_queue_rcv_skb(mroute_sk, skb);
1167 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001168 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001169 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 kfree_skb(skb);
1171 }
1172
1173 return ret;
1174}
1175
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001176/* Queue a packet for resolution. It gets locked cache entry! */
1177static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001178 struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001180 const struct iphdr *iph = ip_hdr(skb);
1181 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001182 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
1185 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001186 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001187 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001188 c->mfc_origin == iph->saddr) {
1189 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 }
1193
Patrick McHardy862465f2010-04-13 05:03:21 +00001194 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001195 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001196 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001197 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 spin_unlock_bh(&mfc_unres_lock);
1199
1200 kfree_skb(skb);
1201 return -ENOBUFS;
1202 }
1203
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001204 /* Fill in the new cache entry */
Yuval Mintz494fff52018-02-28 23:29:34 +02001205 c->_c.mfc_parent = -1;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001206 c->mfc_origin = iph->saddr;
1207 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001209 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001210 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Yuval Mintz494fff52018-02-28 23:29:34 +02001211
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001212 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001213 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 out - Brad Parker
1215 */
1216 spin_unlock_bh(&mfc_unres_lock);
1217
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001218 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 kfree_skb(skb);
1220 return err;
1221 }
1222
Patrick McHardy0c122952010-04-13 05:03:22 +00001223 atomic_inc(&mrt->cache_resolve_queue_len);
Yuval Mintz494fff52018-02-28 23:29:34 +02001224 list_add(&c->_c.list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001225 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
David S. Miller278554b2010-05-12 00:05:35 -07001227 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
Yuval Mintz494fff52018-02-28 23:29:34 +02001228 mod_timer(&mrt->ipmr_expire_timer,
1229 c->_c.mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
1231
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001232 /* See if we can append the packet */
Yuval Mintz494fff52018-02-28 23:29:34 +02001233 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 kfree_skb(skb);
1235 err = -ENOBUFS;
1236 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001237 if (dev) {
1238 skb->dev = dev;
1239 skb->skb_iif = dev->ifindex;
1240 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001241 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 err = 0;
1243 }
1244
1245 spin_unlock_bh(&mfc_unres_lock);
1246 return err;
1247}
1248
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001249/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001251static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252{
Yotam Gigib3620532017-09-27 08:23:14 +02001253 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001254 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001256 /* The entries are added/deleted only under RTNL */
1257 rcu_read_lock();
1258 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1259 mfc->mfcc_mcastgrp.s_addr, parent);
1260 rcu_read_unlock();
1261 if (!c)
1262 return -ENOENT;
Yuval Mintz494fff52018-02-28 23:29:34 +02001263 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1264 list_del_rcu(&c->_c.list);
Yotam Gigib3620532017-09-27 08:23:14 +02001265 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001266 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yotam Gigi310ebbb2017-09-27 08:23:12 +02001267 ipmr_cache_put(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001269 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
Patrick McHardy0c122952010-04-13 05:03:22 +00001272static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001273 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274{
Patrick McHardy862465f2010-04-13 05:03:21 +00001275 struct mfc_cache *uc, *c;
Yuval Mintz494fff52018-02-28 23:29:34 +02001276 struct mr_mfc *_uc;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001277 bool found;
1278 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279
Patrick McHardya50436f22010-03-17 06:04:14 +00001280 if (mfc->mfcc_parent >= MAXVIFS)
1281 return -ENFILE;
1282
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001283 /* The entries are added/deleted only under RTNL */
1284 rcu_read_lock();
1285 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1286 mfc->mfcc_mcastgrp.s_addr, parent);
1287 rcu_read_unlock();
1288 if (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 write_lock_bh(&mrt_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001290 c->_c.mfc_parent = mfc->mfcc_parent;
1291 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001293 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +02001295 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1296 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
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001301 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001302 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 return -EINVAL;
1304
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001305 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001306 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 return -ENOMEM;
1308
Jianjun Kongc354e122008-11-03 00:28:02 -08001309 c->mfc_origin = mfc->mfcc_origin.s_addr;
1310 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
Yuval Mintz494fff52018-02-28 23:29:34 +02001311 c->_c.mfc_parent = mfc->mfcc_parent;
1312 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001314 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Yuval Mintz494fff52018-02-28 23:29:34 +02001316 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001317 ipmr_rht_params);
1318 if (ret) {
1319 pr_err("ipmr: rhtable insert error %d\n", ret);
1320 ipmr_cache_free(c);
1321 return ret;
1322 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001323 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001324 /* Check to see if we resolved a queued list. If so we
1325 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001327 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001329 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1330 uc = (struct mfc_cache *)_uc;
Patrick McHardye258beb2010-04-13 05:03:19 +00001331 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001333 list_del(&_uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001334 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001335 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 break;
1337 }
1338 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001339 if (list_empty(&mrt->mfc_unres_queue))
1340 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 spin_unlock_bh(&mfc_unres_lock);
1342
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001343 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001344 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001345 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
Yotam Gigib3620532017-09-27 08:23:14 +02001347 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001348 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 return 0;
1350}
1351
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001352/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001353static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354{
Yotam Gigib3620532017-09-27 08:23:14 +02001355 struct net *net = read_pnet(&mrt->net);
Yuval Mintz494fff52018-02-28 23:29:34 +02001356 struct mr_mfc *c, *tmp;
1357 struct mfc_cache *cache;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001358 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001359 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001360
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001361 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001362 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001363 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1364 continue;
1365 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001367 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001369 /* Wipe the cache */
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001370 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1371 if (!all && (c->mfc_flags & MFC_STATIC))
1372 continue;
1373 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1374 list_del_rcu(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +02001375 cache = (struct mfc_cache *)c;
1376 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
Yotam Gigib3620532017-09-27 08:23:14 +02001377 mrt->id);
Yuval Mintz494fff52018-02-28 23:29:34 +02001378 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1379 ipmr_cache_put(cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381
Patrick McHardy0c122952010-04-13 05:03:22 +00001382 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 spin_lock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001384 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001385 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +02001386 cache = (struct mfc_cache *)c;
1387 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1388 ipmr_destroy_unres(mrt, cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
1390 spin_unlock_bh(&mfc_unres_lock);
1391 }
1392}
1393
Eric Dumazet4c9687092010-10-01 16:15:01 +00001394/* called from ip_ra_control(), before an RCU grace period,
1395 * we dont need to call synchronize_rcu() here
1396 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397static void mrtsock_destruct(struct sock *sk)
1398{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001399 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001400 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001401
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001402 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001403 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001404 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001405 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001406 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1407 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001408 NETCONFA_IFINDEX_ALL,
1409 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001410 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001411 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001414 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415}
1416
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001417/* Socket options and virtual interface manipulation. The whole
1418 * virtual interface system is a complete heap, but unfortunately
1419 * that's how BSD mrouted happens to think. Maybe one day with a proper
1420 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001422
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001423int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1424 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001426 struct net *net = sock_net(sk);
1427 int val, ret = 0, parent = 0;
1428 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 struct vifctl vif;
1430 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001431 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001432
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001433 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1434 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001435 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001436 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1437 ret = -EOPNOTSUPP;
1438 goto out_unlock;
1439 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001440
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001441 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001442 if (!mrt) {
1443 ret = -ENOENT;
1444 goto out_unlock;
1445 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001446 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001447 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001448 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1449 ret = -EACCES;
1450 goto out_unlock;
1451 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 }
1453
Stephen Hemminger132adf52007-03-08 20:44:43 -08001454 switch (optname) {
1455 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001456 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001457 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001458 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001459 }
1460 if (rtnl_dereference(mrt->mroute_sk)) {
1461 ret = -EADDRINUSE;
1462 break;
1463 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001464
1465 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1466 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001467 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001468 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001469 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1470 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001471 NETCONFA_IFINDEX_ALL,
1472 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001473 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001474 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001475 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001476 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1477 ret = -EACCES;
1478 } else {
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001479 /* We need to unlock here because mrtsock_destruct takes
1480 * care of rtnl itself and we can't change that due to
1481 * the IP_ROUTER_ALERT setsockopt which runs without it.
1482 */
1483 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001484 ret = ip_ra_control(sk, 0, NULL);
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001485 goto out;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001486 }
1487 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001488 case MRT_ADD_VIF:
1489 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001490 if (optlen != sizeof(vif)) {
1491 ret = -EINVAL;
1492 break;
1493 }
1494 if (copy_from_user(&vif, optval, sizeof(vif))) {
1495 ret = -EFAULT;
1496 break;
1497 }
1498 if (vif.vifc_vifi >= MAXVIFS) {
1499 ret = -ENFILE;
1500 break;
1501 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001502 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001503 ret = vif_add(net, mrt, &vif,
1504 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001505 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001506 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001507 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001508 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001509 /* Manipulate the forwarding caches. These live
1510 * in a sort of kernel/user symbiosis.
1511 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001512 case MRT_ADD_MFC:
1513 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001514 parent = -1;
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05001515 /* fall through */
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001516 case MRT_ADD_MFC_PROXY:
1517 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001518 if (optlen != sizeof(mfc)) {
1519 ret = -EINVAL;
1520 break;
1521 }
1522 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1523 ret = -EFAULT;
1524 break;
1525 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001526 if (parent == 0)
1527 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001528 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1529 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001530 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001531 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001532 sk == rtnl_dereference(mrt->mroute_sk),
1533 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001534 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001535 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001536 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001537 if (optlen != sizeof(val)) {
1538 ret = -EINVAL;
1539 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001541 if (get_user(val, (int __user *)optval)) {
1542 ret = -EFAULT;
1543 break;
1544 }
1545 mrt->mroute_do_assert = val;
1546 break;
1547 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001548 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001549 ret = -ENOPROTOOPT;
1550 break;
1551 }
1552 if (optlen != sizeof(val)) {
1553 ret = -EINVAL;
1554 break;
1555 }
1556 if (get_user(val, (int __user *)optval)) {
1557 ret = -EFAULT;
1558 break;
1559 }
1560
1561 val = !!val;
1562 if (val != mrt->mroute_do_pim) {
1563 mrt->mroute_do_pim = val;
1564 mrt->mroute_do_assert = val;
1565 }
1566 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001567 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001568 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1569 ret = -ENOPROTOOPT;
1570 break;
1571 }
1572 if (optlen != sizeof(uval)) {
1573 ret = -EINVAL;
1574 break;
1575 }
1576 if (get_user(uval, (u32 __user *)optval)) {
1577 ret = -EFAULT;
1578 break;
1579 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001580
Eric Dumazet4c9687092010-10-01 16:15:01 +00001581 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1582 ret = -EBUSY;
1583 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001584 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001585 if (IS_ERR(mrt))
1586 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001587 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001588 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001589 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001590 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001591 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001592 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001593 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001595out_unlock:
1596 rtnl_unlock();
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001597out:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001598 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599}
1600
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001601/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001602int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603{
1604 int olr;
1605 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001606 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001607 struct mr_table *mrt;
1608
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001609 if (sk->sk_type != SOCK_RAW ||
1610 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1611 return -EOPNOTSUPP;
1612
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001613 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001614 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001615 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001617 switch (optname) {
1618 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001619 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001620 break;
1621 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001622 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001623 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001624 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001625 break;
1626 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001627 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001628 break;
1629 default:
1630 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001631 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001632
1633 if (get_user(olr, optlen))
1634 return -EFAULT;
1635 olr = min_t(unsigned int, olr, sizeof(int));
1636 if (olr < 0)
1637 return -EINVAL;
1638 if (put_user(olr, optlen))
1639 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001640 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 return -EFAULT;
1642 return 0;
1643}
1644
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001645/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1647{
1648 struct sioc_sg_req sr;
1649 struct sioc_vif_req vr;
1650 struct vif_device *vif;
1651 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001652 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001653 struct mr_table *mrt;
1654
1655 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001656 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001657 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001658
Stephen Hemminger132adf52007-03-08 20:44:43 -08001659 switch (cmd) {
1660 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001661 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001662 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001663 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001664 return -EINVAL;
1665 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001666 vif = &mrt->vif_table[vr.vifi];
1667 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001668 vr.icount = vif->pkt_in;
1669 vr.ocount = vif->pkt_out;
1670 vr.ibytes = vif->bytes_in;
1671 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001673
Jianjun Kongc354e122008-11-03 00:28:02 -08001674 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001676 return 0;
1677 }
1678 read_unlock(&mrt_lock);
1679 return -EADDRNOTAVAIL;
1680 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001681 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001682 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
Eric Dumazeta8c94862010-10-01 16:15:08 +00001684 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001685 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001686 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001687 sr.pktcnt = c->_c.mfc_un.res.pkt;
1688 sr.bytecnt = c->_c.mfc_un.res.bytes;
1689 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001690 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001691
Jianjun Kongc354e122008-11-03 00:28:02 -08001692 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001693 return -EFAULT;
1694 return 0;
1695 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001696 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001697 return -EADDRNOTAVAIL;
1698 default:
1699 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 }
1701}
1702
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001703#ifdef CONFIG_COMPAT
1704struct compat_sioc_sg_req {
1705 struct in_addr src;
1706 struct in_addr grp;
1707 compat_ulong_t pktcnt;
1708 compat_ulong_t bytecnt;
1709 compat_ulong_t wrong_if;
1710};
1711
David S. Millerca6b8bb02011-02-03 17:24:28 -08001712struct compat_sioc_vif_req {
1713 vifi_t vifi; /* Which iface */
1714 compat_ulong_t icount;
1715 compat_ulong_t ocount;
1716 compat_ulong_t ibytes;
1717 compat_ulong_t obytes;
1718};
1719
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001720int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1721{
David S. Miller0033d5a2011-02-03 17:21:31 -08001722 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001723 struct compat_sioc_vif_req vr;
1724 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001725 struct mfc_cache *c;
1726 struct net *net = sock_net(sk);
1727 struct mr_table *mrt;
1728
1729 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001730 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001731 return -ENOENT;
1732
1733 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001734 case SIOCGETVIFCNT:
1735 if (copy_from_user(&vr, arg, sizeof(vr)))
1736 return -EFAULT;
1737 if (vr.vifi >= mrt->maxvif)
1738 return -EINVAL;
1739 read_lock(&mrt_lock);
1740 vif = &mrt->vif_table[vr.vifi];
1741 if (VIF_EXISTS(mrt, vr.vifi)) {
1742 vr.icount = vif->pkt_in;
1743 vr.ocount = vif->pkt_out;
1744 vr.ibytes = vif->bytes_in;
1745 vr.obytes = vif->bytes_out;
1746 read_unlock(&mrt_lock);
1747
1748 if (copy_to_user(arg, &vr, sizeof(vr)))
1749 return -EFAULT;
1750 return 0;
1751 }
1752 read_unlock(&mrt_lock);
1753 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001754 case SIOCGETSGCNT:
1755 if (copy_from_user(&sr, arg, sizeof(sr)))
1756 return -EFAULT;
1757
1758 rcu_read_lock();
1759 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1760 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001761 sr.pktcnt = c->_c.mfc_un.res.pkt;
1762 sr.bytecnt = c->_c.mfc_un.res.bytes;
1763 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001764 rcu_read_unlock();
1765
1766 if (copy_to_user(arg, &sr, sizeof(sr)))
1767 return -EFAULT;
1768 return 0;
1769 }
1770 rcu_read_unlock();
1771 return -EADDRNOTAVAIL;
1772 default:
1773 return -ENOIOCTLCMD;
1774 }
1775}
1776#endif
1777
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1779{
Jiri Pirko351638e2013-05-28 01:30:21 +00001780 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001781 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001782 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 struct vif_device *v;
1784 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 if (event != NETDEV_UNREGISTER)
1787 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001788
1789 ipmr_for_each_table(mrt, net) {
1790 v = &mrt->vif_table[0];
1791 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1792 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001793 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001794 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796 return NOTIFY_DONE;
1797}
1798
Jianjun Kongc354e122008-11-03 00:28:02 -08001799static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 .notifier_call = ipmr_device_event,
1801};
1802
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001803/* Encapsulate a packet by attaching a valid IPIP header to it.
1804 * This avoids tunnel drivers and other mess and gives us the speed so
1805 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001807static void ip_encap(struct net *net, struct sk_buff *skb,
1808 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001810 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001811 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001812
1813 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001814 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001815 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001816 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001818 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001819 iph->tos = old_iph->tos;
1820 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 iph->frag_off = 0;
1822 iph->daddr = daddr;
1823 iph->saddr = saddr;
1824 iph->protocol = IPPROTO_IPIP;
1825 iph->ihl = 5;
1826 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001827 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 ip_send_check(iph);
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1831 nf_reset(skb);
1832}
1833
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001834static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1835 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001837 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
David S. Miller73186df2015-11-03 13:41:45 -05001839 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1840 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
1842 if (unlikely(opt->optlen))
1843 ip_forward_options(skb);
1844
Eric W. Biederman13206b62015-10-07 16:48:35 -05001845 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
Yotam Gigia5bc9292017-10-03 09:58:08 +02001848#ifdef CONFIG_NET_SWITCHDEV
1849static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1850 int in_vifi, int out_vifi)
1851{
1852 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1853 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1854
1855 if (!skb->offload_mr_fwd_mark)
1856 return false;
1857 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1858 return false;
1859 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1860 &in_vif->dev_parent_id);
1861}
1862#else
1863static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1864 int in_vifi, int out_vifi)
1865{
1866 return false;
1867}
1868#endif
1869
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001870/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Patrick McHardy0c122952010-04-13 05:03:22 +00001872static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
Yotam Gigia5bc9292017-10-03 09:58:08 +02001873 int in_vifi, struct sk_buff *skb,
1874 struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001876 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001877 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 struct net_device *dev;
1879 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001880 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 int encap = 0;
1882
Ian Morris51456b22015-04-03 09:17:26 +01001883 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 goto out_free;
1885
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 if (vif->flags & VIFF_REGISTER) {
1887 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001888 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001889 vif->dev->stats.tx_bytes += skb->len;
1890 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001891 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001892 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Yotam Gigia5bc9292017-10-03 09:58:08 +02001895 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1896 goto out_free;
1897
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001898 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001899 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001900 vif->remote, vif->local,
1901 0, 0,
1902 IPPROTO_IPIP,
1903 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001904 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 goto out_free;
1906 encap = sizeof(struct iphdr);
1907 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001908 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001909 0, 0,
1910 IPPROTO_IPIP,
1911 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001912 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 goto out_free;
1914 }
1915
Changli Gaod8d1f302010-06-10 23:31:35 -07001916 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917
Changli Gaod8d1f302010-06-10 23:31:35 -07001918 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001920 * allow to send ICMP, so that packets will disappear
1921 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 */
David S. Miller73186df2015-11-03 13:41:45 -05001923 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 ip_rt_put(rt);
1925 goto out_free;
1926 }
1927
Changli Gaod8d1f302010-06-10 23:31:35 -07001928 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929
1930 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001931 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 goto out_free;
1933 }
1934
1935 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001936 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
Eric Dumazetadf30902009-06-02 05:19:30 +00001938 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001939 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001940 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941
1942 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001943 * What do we do with netfilter? -- RR
1944 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001946 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001948 vif->dev->stats.tx_packets++;
1949 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 }
1951
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001952 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001954 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 * not only before forwarding, but after forwarding on all output
1956 * interfaces. It is clear, if mrouter runs a multicasting
1957 * program, it should receive packets not depending to what interface
1958 * program is joined.
1959 * If we will not make it, the program will have to join on all
1960 * interfaces. On the other hand, multihoming host (or router, but
1961 * not mrouter) cannot join to more than one interface - it will
1962 * result in receiving multiple packets.
1963 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001964 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1965 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 ipmr_forward_finish);
1967 return;
1968
1969out_free:
1970 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971}
1972
Patrick McHardy0c122952010-04-13 05:03:22 +00001973static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
1975 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001976
1977 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1978 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 break;
1980 }
1981 return ct;
1982}
1983
1984/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001985static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001986 struct net_device *dev, struct sk_buff *skb,
Yuval Mintz494fff52018-02-28 23:29:34 +02001987 struct mfc_cache *c, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001989 int true_vifi = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 int psend = -1;
1991 int vif, ct;
1992
Yuval Mintz494fff52018-02-28 23:29:34 +02001993 vif = c->_c.mfc_parent;
1994 c->_c.mfc_un.res.pkt++;
1995 c->_c.mfc_un.res.bytes += skb->len;
1996 c->_c.mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
Yuval Mintz494fff52018-02-28 23:29:34 +02001998 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001999 struct mfc_cache *cache_proxy;
2000
2001 /* For an (*,G) entry, we only check that the incomming
2002 * interface is part of the static tree.
2003 */
Yuval Mintz845c9a72018-02-28 23:29:35 +02002004 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002005 if (cache_proxy &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002006 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002007 goto forward;
2008 }
2009
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002010 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002011 if (mrt->vif_table[vif].dev != dev) {
David S. Millerc7537962010-11-11 17:07:48 -08002012 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002014 * Very complicated situation...
2015 *
2016 * The best workaround until routing daemons will be
2017 * fixed is not to redistribute packet, if it was
2018 * send through wrong interface. It means, that
2019 * multicast applications WILL NOT work for
2020 * (S,G), which have default multicast route pointing
2021 * to wrong oif. In any case, it is not a good
2022 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 */
2024 goto dont_forward;
2025 }
2026
Yuval Mintz494fff52018-02-28 23:29:34 +02002027 c->_c.mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028
Patrick McHardy0c122952010-04-13 05:03:22 +00002029 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002031 * so that we cannot check that packet arrived on an oif.
2032 * It is bad, but otherwise we would need to move pretty
2033 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 */
Patrick McHardy0c122952010-04-13 05:03:22 +00002035 (mrt->mroute_do_pim ||
Yuval Mintz494fff52018-02-28 23:29:34 +02002036 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002037 time_after(jiffies,
Yuval Mintz494fff52018-02-28 23:29:34 +02002038 c->_c.mfc_un.res.last_assert +
2039 MFC_ASSERT_THRESH)) {
2040 c->_c.mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00002041 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
2043 goto dont_forward;
2044 }
2045
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002046forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00002047 mrt->vif_table[vif].pkt_in++;
2048 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002050 /* Forward the frame */
Yuval Mintz494fff52018-02-28 23:29:34 +02002051 if (c->mfc_origin == htonl(INADDR_ANY) &&
2052 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002053 if (true_vifi >= 0 &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002054 true_vifi != c->_c.mfc_parent &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002055 ip_hdr(skb)->ttl >
Yuval Mintz494fff52018-02-28 23:29:34 +02002056 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002057 /* It's an (*,*) entry and the packet is not coming from
2058 * the upstream: forward the packet to the upstream
2059 * only.
2060 */
Yuval Mintz494fff52018-02-28 23:29:34 +02002061 psend = c->_c.mfc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002062 goto last_forward;
2063 }
2064 goto dont_forward;
2065 }
Yuval Mintz494fff52018-02-28 23:29:34 +02002066 for (ct = c->_c.mfc_un.res.maxvif - 1;
2067 ct >= c->_c.mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002068 /* For (*,G) entry, don't forward to the incoming interface */
Yuval Mintz494fff52018-02-28 23:29:34 +02002069 if ((c->mfc_origin != htonl(INADDR_ANY) ||
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002070 ct != true_vifi) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002071 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072 if (psend != -1) {
2073 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002076 ipmr_queue_xmit(net, mrt, true_vifi,
Yuval Mintz494fff52018-02-28 23:29:34 +02002077 skb2, c, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002079 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 }
2081 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002082last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 if (psend != -1) {
2084 if (local) {
2085 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002086
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002088 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
Yuval Mintz494fff52018-02-28 23:29:34 +02002089 c, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 } else {
Yuval Mintz494fff52018-02-28 23:29:34 +02002091 ipmr_queue_xmit(net, mrt, true_vifi, skb, c, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03002092 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094 }
2095
2096dont_forward:
2097 if (!local)
2098 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099}
2100
David S. Miller417da662011-05-03 19:42:43 -07002101static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08002102{
David S. Miller417da662011-05-03 19:42:43 -07002103 struct rtable *rt = skb_rtable(skb);
2104 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05002105 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07002106 .daddr = iph->daddr,
2107 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00002108 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07002109 .flowi4_oif = (rt_is_output_route(rt) ?
2110 skb->dev->ifindex : 0),
2111 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002112 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07002113 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00002114 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08002115 };
2116 struct mr_table *mrt;
2117 int err;
2118
David S. Millerda919812011-03-12 02:04:50 -05002119 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002120 if (err)
2121 return ERR_PTR(err);
2122 return mrt;
2123}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002125/* Multicast packets for forwarding arrive here
2126 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128int ip_mr_input(struct sk_buff *skb)
2129{
2130 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002131 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00002132 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002133 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002134 struct net_device *dev;
2135
2136 /* skb->dev passed in is the loX master dev for vrfs.
2137 * As there are no vifs associated with loopback devices,
2138 * get the proper interface that does have a vif associated with it.
2139 */
2140 dev = skb->dev;
2141 if (netif_is_l3_master(skb->dev)) {
2142 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2143 if (!dev) {
2144 kfree_skb(skb);
2145 return -ENODEV;
2146 }
2147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
2149 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002150 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00002152 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153 goto dont_forward;
2154
David S. Miller417da662011-05-03 19:42:43 -07002155 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002156 if (IS_ERR(mrt)) {
2157 kfree_skb(skb);
2158 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00002161 if (IPCB(skb)->opt.router_alert) {
2162 if (ip_call_ra_chain(skb))
2163 return 0;
2164 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2165 /* IGMPv1 (and broken IGMPv2 implementations sort of
2166 * Cisco IOS <= 11.2(8)) do not put router alert
2167 * option to IGMP packets destined to routable
2168 * groups. It is very bad, because it means
2169 * that we can forward NO IGMP messages.
2170 */
2171 struct sock *mroute_sk;
2172
2173 mroute_sk = rcu_dereference(mrt->mroute_sk);
2174 if (mroute_sk) {
2175 nf_reset(skb);
2176 raw_rcv(mroute_sk, skb);
2177 return 0;
2178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 }
2180 }
2181
Eric Dumazeta8c94862010-10-01 16:15:08 +00002182 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002183 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002184 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002185 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002186
2187 if (vif >= 0)
2188 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2189 vif);
2190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002192 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002193 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194 int vif;
2195
2196 if (local) {
2197 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2198 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002199 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 skb = skb2;
2202 }
2203
Eric Dumazeta8c94862010-10-01 16:15:08 +00002204 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002205 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 if (vif >= 0) {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002207 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 read_unlock(&mrt_lock);
2209
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002210 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211 }
2212 read_unlock(&mrt_lock);
2213 kfree_skb(skb);
2214 return -ENODEV;
2215 }
2216
Eric Dumazeta8c94862010-10-01 16:15:08 +00002217 read_lock(&mrt_lock);
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002218 ip_mr_forward(net, mrt, dev, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 read_unlock(&mrt_lock);
2220
2221 if (local)
2222 return ip_local_deliver(skb);
2223
2224 return 0;
2225
2226dont_forward:
2227 if (local)
2228 return ip_local_deliver(skb);
2229 kfree_skb(skb);
2230 return 0;
2231}
2232
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002233#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002234/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002235int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002236{
2237 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002238 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002239 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002240
2241 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2242 goto drop;
2243
2244 pim = igmp_hdr(skb);
2245
David S. Miller417da662011-05-03 19:42:43 -07002246 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002247 if (IS_ERR(mrt))
2248 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002249 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002250 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2251 goto drop;
2252
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002253 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002254drop:
2255 kfree_skb(skb);
2256 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return 0;
2258}
2259#endif
2260
2261#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002262static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002263{
2264 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002265 struct net *net = dev_net(skb->dev);
2266 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002268 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 goto drop;
2270
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002271 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002272 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002273 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002274 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002275 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto drop;
2277
David S. Miller417da662011-05-03 19:42:43 -07002278 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002279 if (IS_ERR(mrt))
2280 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002281 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002282drop:
2283 kfree_skb(skb);
2284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 return 0;
2286}
2287#endif
2288
David S. Miller9a1b9492011-05-04 12:18:54 -07002289int ipmr_get_route(struct net *net, struct sk_buff *skb,
2290 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002291 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002294 struct mr_table *mrt;
2295 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002297 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002298 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002299 return -ENOENT;
2300
Eric Dumazeta8c94862010-10-01 16:15:08 +00002301 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002302 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002303 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002304 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002305
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002306 if (vif >= 0)
2307 cache = ipmr_cache_find_any(mrt, daddr, vif);
2308 }
Ian Morris51456b22015-04-03 09:17:26 +01002309 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002310 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002311 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002313 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002316 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002317 if (dev)
2318 vif = ipmr_find_vif(mrt, dev);
2319 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002321 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322 return -ENODEV;
2323 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002324 skb2 = skb_clone(skb, GFP_ATOMIC);
2325 if (!skb2) {
2326 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002327 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002328 return -ENOMEM;
2329 }
2330
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002331 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002332 skb_push(skb2, sizeof(struct iphdr));
2333 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002334 iph = ip_hdr(skb2);
2335 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002336 iph->saddr = saddr;
2337 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002338 iph->version = 0;
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002339 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002340 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002341 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 return err;
2343 }
2344
Eric Dumazeta8c94862010-10-01 16:15:08 +00002345 read_lock(&mrt_lock);
Yuval Mintz7b0db852018-02-28 23:29:39 +02002346 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002348 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 return err;
2350}
2351
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002352static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002353 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2354 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002355{
2356 struct nlmsghdr *nlh;
2357 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002358 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002359
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002360 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002361 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002362 return -EMSGSIZE;
2363
2364 rtm = nlmsg_data(nlh);
2365 rtm->rtm_family = RTNL_FAMILY_IPMR;
2366 rtm->rtm_dst_len = 32;
2367 rtm->rtm_src_len = 32;
2368 rtm->rtm_tos = 0;
2369 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002370 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2371 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002372 rtm->rtm_type = RTN_MULTICAST;
2373 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Yuval Mintz494fff52018-02-28 23:29:34 +02002374 if (c->_c.mfc_flags & MFC_STATIC)
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002375 rtm->rtm_protocol = RTPROT_STATIC;
2376 else
2377 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002378 rtm->rtm_flags = 0;
2379
Jiri Benc930345e2015-03-29 16:59:25 +02002380 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2381 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002382 goto nla_put_failure;
Yuval Mintz7b0db852018-02-28 23:29:39 +02002383 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002384 /* do not break the dump if cache is unresolved */
2385 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002386 goto nla_put_failure;
2387
Johannes Berg053c0952015-01-16 22:09:00 +01002388 nlmsg_end(skb, nlh);
2389 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002390
2391nla_put_failure:
2392 nlmsg_cancel(skb, nlh);
2393 return -EMSGSIZE;
2394}
2395
Yuval Mintz7b0db852018-02-28 23:29:39 +02002396static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2397 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2398 int flags)
2399{
2400 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2401 cmd, flags);
2402}
2403
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002404static size_t mroute_msgsize(bool unresolved, int maxvif)
2405{
2406 size_t len =
2407 NLMSG_ALIGN(sizeof(struct rtmsg))
2408 + nla_total_size(4) /* RTA_TABLE */
2409 + nla_total_size(4) /* RTA_SRC */
2410 + nla_total_size(4) /* RTA_DST */
2411 ;
2412
2413 if (!unresolved)
2414 len = len
2415 + nla_total_size(4) /* RTA_IIF */
2416 + nla_total_size(0) /* RTA_MULTIPATH */
2417 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2418 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002419 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002420 ;
2421
2422 return len;
2423}
2424
2425static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2426 int cmd)
2427{
2428 struct net *net = read_pnet(&mrt->net);
2429 struct sk_buff *skb;
2430 int err = -ENOBUFS;
2431
Yuval Mintz494fff52018-02-28 23:29:34 +02002432 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2433 mrt->maxvif),
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002434 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002435 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002436 goto errout;
2437
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002438 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002439 if (err < 0)
2440 goto errout;
2441
2442 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2443 return;
2444
2445errout:
2446 kfree_skb(skb);
2447 if (err < 0)
2448 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2449}
2450
Julien Gomes5a645dd2017-06-20 13:54:17 -07002451static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2452{
2453 size_t len =
2454 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2455 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2456 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2457 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2458 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2459 /* IPMRA_CREPORT_PKT */
2460 + nla_total_size(payloadlen)
2461 ;
2462
2463 return len;
2464}
2465
2466static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2467{
2468 struct net *net = read_pnet(&mrt->net);
2469 struct nlmsghdr *nlh;
2470 struct rtgenmsg *rtgenm;
2471 struct igmpmsg *msg;
2472 struct sk_buff *skb;
2473 struct nlattr *nla;
2474 int payloadlen;
2475
2476 payloadlen = pkt->len - sizeof(struct igmpmsg);
2477 msg = (struct igmpmsg *)skb_network_header(pkt);
2478
2479 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2480 if (!skb)
2481 goto errout;
2482
2483 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2484 sizeof(struct rtgenmsg), 0);
2485 if (!nlh)
2486 goto errout;
2487 rtgenm = nlmsg_data(nlh);
2488 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2489 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2490 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2491 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2492 msg->im_src.s_addr) ||
2493 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2494 msg->im_dst.s_addr))
2495 goto nla_put_failure;
2496
2497 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2498 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2499 nla_data(nla), payloadlen))
2500 goto nla_put_failure;
2501
2502 nlmsg_end(skb, nlh);
2503
2504 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2505 return;
2506
2507nla_put_failure:
2508 nlmsg_cancel(skb, nlh);
2509errout:
2510 kfree_skb(skb);
2511 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2512}
2513
Donald Sharp4f75ba62017-06-28 13:58:57 -04002514static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2515 struct netlink_ext_ack *extack)
2516{
2517 struct net *net = sock_net(in_skb->sk);
2518 struct nlattr *tb[RTA_MAX + 1];
2519 struct sk_buff *skb = NULL;
2520 struct mfc_cache *cache;
2521 struct mr_table *mrt;
2522 struct rtmsg *rtm;
2523 __be32 src, grp;
2524 u32 tableid;
2525 int err;
2526
2527 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2528 rtm_ipv4_policy, extack);
2529 if (err < 0)
2530 goto errout;
2531
2532 rtm = nlmsg_data(nlh);
2533
2534 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2535 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2536 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2537
2538 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
Dan Carpenter2e3d2322017-07-12 10:56:47 +03002539 if (!mrt) {
2540 err = -ENOENT;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002541 goto errout_free;
2542 }
2543
2544 /* entries are added/deleted only under RTNL */
2545 rcu_read_lock();
2546 cache = ipmr_cache_find(mrt, src, grp);
2547 rcu_read_unlock();
2548 if (!cache) {
2549 err = -ENOENT;
2550 goto errout_free;
2551 }
2552
2553 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2554 if (!skb) {
2555 err = -ENOBUFS;
2556 goto errout_free;
2557 }
2558
2559 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2560 nlh->nlmsg_seq, cache,
2561 RTM_NEWROUTE, 0);
2562 if (err < 0)
2563 goto errout_free;
2564
2565 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2566
2567errout:
2568 return err;
2569
2570errout_free:
2571 kfree_skb(skb);
2572 goto errout;
2573}
2574
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002575static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2576{
Yuval Mintz7b0db852018-02-28 23:29:39 +02002577 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
2578 _ipmr_fill_mroute, &mfc_unres_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002579}
2580
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002581static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2582 [RTA_SRC] = { .type = NLA_U32 },
2583 [RTA_DST] = { .type = NLA_U32 },
2584 [RTA_IIF] = { .type = NLA_U32 },
2585 [RTA_TABLE] = { .type = NLA_U32 },
2586 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2587};
2588
2589static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2590{
2591 switch (rtm_protocol) {
2592 case RTPROT_STATIC:
2593 case RTPROT_MROUTED:
2594 return true;
2595 }
2596 return false;
2597}
2598
2599static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2600{
2601 struct rtnexthop *rtnh = nla_data(nla);
2602 int remaining = nla_len(nla), vifi = 0;
2603
2604 while (rtnh_ok(rtnh, remaining)) {
2605 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2606 if (++vifi == MAXVIFS)
2607 break;
2608 rtnh = rtnh_next(rtnh, &remaining);
2609 }
2610
2611 return remaining > 0 ? -EINVAL : vifi;
2612}
2613
2614/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2615static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2616 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002617 struct mr_table **mrtret,
2618 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002619{
2620 struct net_device *dev = NULL;
2621 u32 tblid = RT_TABLE_DEFAULT;
2622 struct mr_table *mrt;
2623 struct nlattr *attr;
2624 struct rtmsg *rtm;
2625 int ret, rem;
2626
Johannes Bergfceb6432017-04-12 14:34:07 +02002627 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07002628 extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002629 if (ret < 0)
2630 goto out;
2631 rtm = nlmsg_data(nlh);
2632
2633 ret = -EINVAL;
2634 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2635 rtm->rtm_type != RTN_MULTICAST ||
2636 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2637 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2638 goto out;
2639
2640 memset(mfcc, 0, sizeof(*mfcc));
2641 mfcc->mfcc_parent = -1;
2642 ret = 0;
2643 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2644 switch (nla_type(attr)) {
2645 case RTA_SRC:
2646 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2647 break;
2648 case RTA_DST:
2649 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2650 break;
2651 case RTA_IIF:
2652 dev = __dev_get_by_index(net, nla_get_u32(attr));
2653 if (!dev) {
2654 ret = -ENODEV;
2655 goto out;
2656 }
2657 break;
2658 case RTA_MULTIPATH:
2659 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2660 ret = -EINVAL;
2661 goto out;
2662 }
2663 break;
2664 case RTA_PREFSRC:
2665 ret = 1;
2666 break;
2667 case RTA_TABLE:
2668 tblid = nla_get_u32(attr);
2669 break;
2670 }
2671 }
2672 mrt = ipmr_get_table(net, tblid);
2673 if (!mrt) {
2674 ret = -ENOENT;
2675 goto out;
2676 }
2677 *mrtret = mrt;
2678 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2679 if (dev)
2680 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2681
2682out:
2683 return ret;
2684}
2685
2686/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002687static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2688 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002689{
2690 struct net *net = sock_net(skb->sk);
2691 int ret, mrtsock, parent;
2692 struct mr_table *tbl;
2693 struct mfcctl mfcc;
2694
2695 mrtsock = 0;
2696 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002697 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002698 if (ret < 0)
2699 return ret;
2700
2701 parent = ret ? mfcc.mfcc_parent : -1;
2702 if (nlh->nlmsg_type == RTM_NEWROUTE)
2703 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2704 else
2705 return ipmr_mfc_delete(tbl, &mfcc, parent);
2706}
2707
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002708static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2709{
2710 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2711
2712 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2713 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2714 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2715 mrt->mroute_reg_vif_num) ||
2716 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2717 mrt->mroute_do_assert) ||
2718 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim))
2719 return false;
2720
2721 return true;
2722}
2723
2724static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2725{
2726 struct nlattr *vif_nest;
2727 struct vif_device *vif;
2728
2729 /* if the VIF doesn't exist just continue */
2730 if (!VIF_EXISTS(mrt, vifid))
2731 return true;
2732
2733 vif = &mrt->vif_table[vifid];
2734 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2735 if (!vif_nest)
2736 return false;
2737 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2738 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2739 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2740 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2741 IPMRA_VIFA_PAD) ||
2742 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2743 IPMRA_VIFA_PAD) ||
2744 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2745 IPMRA_VIFA_PAD) ||
2746 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2747 IPMRA_VIFA_PAD) ||
2748 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2749 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2750 nla_nest_cancel(skb, vif_nest);
2751 return false;
2752 }
2753 nla_nest_end(skb, vif_nest);
2754
2755 return true;
2756}
2757
2758static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2759{
2760 struct net *net = sock_net(skb->sk);
2761 struct nlmsghdr *nlh = NULL;
2762 unsigned int t = 0, s_t;
2763 unsigned int e = 0, s_e;
2764 struct mr_table *mrt;
2765
2766 s_t = cb->args[0];
2767 s_e = cb->args[1];
2768
2769 ipmr_for_each_table(mrt, net) {
2770 struct nlattr *vifs, *af;
2771 struct ifinfomsg *hdr;
2772 u32 i;
2773
2774 if (t < s_t)
2775 goto skip_table;
2776 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2777 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2778 sizeof(*hdr), NLM_F_MULTI);
2779 if (!nlh)
2780 break;
2781
2782 hdr = nlmsg_data(nlh);
2783 memset(hdr, 0, sizeof(*hdr));
2784 hdr->ifi_family = RTNL_FAMILY_IPMR;
2785
2786 af = nla_nest_start(skb, IFLA_AF_SPEC);
2787 if (!af) {
2788 nlmsg_cancel(skb, nlh);
2789 goto out;
2790 }
2791
2792 if (!ipmr_fill_table(mrt, skb)) {
2793 nlmsg_cancel(skb, nlh);
2794 goto out;
2795 }
2796
2797 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2798 if (!vifs) {
2799 nla_nest_end(skb, af);
2800 nlmsg_end(skb, nlh);
2801 goto out;
2802 }
2803 for (i = 0; i < mrt->maxvif; i++) {
2804 if (e < s_e)
2805 goto skip_entry;
2806 if (!ipmr_fill_vif(mrt, i, skb)) {
2807 nla_nest_end(skb, vifs);
2808 nla_nest_end(skb, af);
2809 nlmsg_end(skb, nlh);
2810 goto out;
2811 }
2812skip_entry:
2813 e++;
2814 }
2815 s_e = 0;
2816 e = 0;
2817 nla_nest_end(skb, vifs);
2818 nla_nest_end(skb, af);
2819 nlmsg_end(skb, nlh);
2820skip_table:
2821 t++;
2822 }
2823
2824out:
2825 cb->args[1] = e;
2826 cb->args[0] = t;
2827
2828 return skb->len;
2829}
2830
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002831#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002832/* The /proc interfaces to multicast routing :
2833 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835
2836static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002837 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002839 struct mr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002840 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002841 struct mr_table *mrt;
2842
2843 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002844 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002845 return ERR_PTR(-ENOENT);
2846
2847 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002848
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 read_lock(&mrt_lock);
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002850 return mr_vif_seq_start(seq, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851}
2852
2853static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002854 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002855{
2856 read_unlock(&mrt_lock);
2857}
2858
2859static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2860{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002861 struct mr_vif_iter *iter = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002862 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002863
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002865 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2867 } else {
2868 const struct vif_device *vif = v;
Yuval Mintz6853f212018-02-28 23:29:29 +02002869 const char *name = vif->dev ?
2870 vif->dev->name : "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
2872 seq_printf(seq,
James Hogan91e6dd82018-01-30 09:48:02 +00002873 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002874 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002875 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 vif->bytes_out, vif->pkt_out,
2877 vif->flags, vif->local, vif->remote);
2878 }
2879 return 0;
2880}
2881
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002882static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002883 .start = ipmr_vif_seq_start,
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002884 .next = mr_vif_seq_next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002885 .stop = ipmr_vif_seq_stop,
2886 .show = ipmr_vif_seq_show,
2887};
2888
2889static int ipmr_vif_open(struct inode *inode, struct file *file)
2890{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002891 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002892 sizeof(struct mr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002893}
2894
Arjan van de Ven9a321442007-02-12 00:55:35 -08002895static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002896 .open = ipmr_vif_open,
2897 .read = seq_read,
2898 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002899 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002900};
2901
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2903{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002904 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002905 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002906
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002907 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002908 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002909 return ERR_PTR(-ENOENT);
2910
Yuval Mintzc8d61962018-02-28 23:29:36 +02002911 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002912}
2913
2914static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2915{
2916 int n;
2917
2918 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002919 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2921 } else {
2922 const struct mfc_cache *mfc = v;
Yuval Mintzc8d61962018-02-28 23:29:36 +02002923 const struct mr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002924 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002925
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002926 seq_printf(seq, "%08X %08X %-3hd",
2927 (__force u32) mfc->mfc_mcastgrp,
2928 (__force u32) mfc->mfc_origin,
Yuval Mintz494fff52018-02-28 23:29:34 +02002929 mfc->_c.mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930
Patrick McHardy0c122952010-04-13 05:03:22 +00002931 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002932 seq_printf(seq, " %8lu %8lu %8lu",
Yuval Mintz494fff52018-02-28 23:29:34 +02002933 mfc->_c.mfc_un.res.pkt,
2934 mfc->_c.mfc_un.res.bytes,
2935 mfc->_c.mfc_un.res.wrong_if);
2936 for (n = mfc->_c.mfc_un.res.minvif;
2937 n < mfc->_c.mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002938 if (VIF_EXISTS(mrt, n) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002939 mfc->_c.mfc_un.res.ttls[n] < 255)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002940 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002941 " %2d:%-3d",
Yuval Mintz494fff52018-02-28 23:29:34 +02002942 n, mfc->_c.mfc_un.res.ttls[n]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002944 } else {
2945 /* unresolved mfc_caches don't contain
2946 * pkt, bytes and wrong_if values
2947 */
2948 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 }
2950 seq_putc(seq, '\n');
2951 }
2952 return 0;
2953}
2954
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002955static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 .start = ipmr_mfc_seq_start,
Yuval Mintzc8d61962018-02-28 23:29:36 +02002957 .next = mr_mfc_seq_next,
2958 .stop = mr_mfc_seq_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959 .show = ipmr_mfc_seq_show,
2960};
2961
2962static int ipmr_mfc_open(struct inode *inode, struct file *file)
2963{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002964 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
Yuval Mintzc8d61962018-02-28 23:29:36 +02002965 sizeof(struct mr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966}
2967
Arjan van de Ven9a321442007-02-12 00:55:35 -08002968static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 .open = ipmr_mfc_open,
2970 .read = seq_read,
2971 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002972 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002974#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975
2976#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002977static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002979 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980};
2981#endif
2982
Yotam Gigi4d65b942017-09-27 08:23:13 +02002983static unsigned int ipmr_seq_read(struct net *net)
2984{
2985 ASSERT_RTNL();
2986
2987 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
2988}
2989
2990static int ipmr_dump(struct net *net, struct notifier_block *nb)
2991{
2992 struct mr_table *mrt;
2993 int err;
2994
2995 err = ipmr_rules_dump(net, nb);
2996 if (err)
2997 return err;
2998
2999 ipmr_for_each_table(mrt, net) {
3000 struct vif_device *v = &mrt->vif_table[0];
Yuval Mintz494fff52018-02-28 23:29:34 +02003001 struct mr_mfc *mfc;
Yotam Gigi4d65b942017-09-27 08:23:13 +02003002 int vifi;
3003
3004 /* Notifiy on table VIF entries */
3005 read_lock(&mrt_lock);
3006 for (vifi = 0; vifi < mrt->maxvif; vifi++, v++) {
3007 if (!v->dev)
3008 continue;
3009
3010 call_ipmr_vif_entry_notifier(nb, net, FIB_EVENT_VIF_ADD,
3011 v, vifi, mrt->id);
3012 }
3013 read_unlock(&mrt_lock);
3014
3015 /* Notify on table MFC entries */
3016 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
3017 call_ipmr_mfc_entry_notifier(nb, net,
Yuval Mintz494fff52018-02-28 23:29:34 +02003018 FIB_EVENT_ENTRY_ADD,
3019 (struct mfc_cache *)mfc,
Yotam Gigi4d65b942017-09-27 08:23:13 +02003020 mrt->id);
3021 }
3022
3023 return 0;
3024}
3025
3026static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3027 .family = RTNL_FAMILY_IPMR,
3028 .fib_seq_read = ipmr_seq_read,
3029 .fib_dump = ipmr_dump,
3030 .owner = THIS_MODULE,
3031};
3032
Colin Ian Kingef739d82017-09-29 14:34:22 +01003033static int __net_init ipmr_notifier_init(struct net *net)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003034{
3035 struct fib_notifier_ops *ops;
3036
3037 net->ipv4.ipmr_seq = 0;
3038
3039 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3040 if (IS_ERR(ops))
3041 return PTR_ERR(ops);
3042 net->ipv4.ipmr_notifier_ops = ops;
3043
3044 return 0;
3045}
3046
3047static void __net_exit ipmr_notifier_exit(struct net *net)
3048{
3049 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3050 net->ipv4.ipmr_notifier_ops = NULL;
3051}
3052
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01003053/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00003054static int __net_init ipmr_net_init(struct net *net)
3055{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003056 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00003057
Yotam Gigi4d65b942017-09-27 08:23:13 +02003058 err = ipmr_notifier_init(net);
3059 if (err)
3060 goto ipmr_notifier_fail;
3061
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003062 err = ipmr_rules_init(net);
3063 if (err < 0)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003064 goto ipmr_rules_fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003065
3066#ifdef CONFIG_PROC_FS
3067 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00003068 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003069 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00003070 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003071 goto proc_cache_fail;
3072#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00003073 return 0;
3074
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003075#ifdef CONFIG_PROC_FS
3076proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00003077 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003078proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003079 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003080#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003081ipmr_rules_fail:
3082 ipmr_notifier_exit(net);
3083ipmr_notifier_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003084 return err;
3085}
3086
3087static void __net_exit ipmr_net_exit(struct net *net)
3088{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003089#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003090 remove_proc_entry("ip_mr_cache", net->proc_net);
3091 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003092#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003093 ipmr_notifier_exit(net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003094 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00003095}
3096
3097static struct pernet_operations ipmr_net_ops = {
3098 .init = ipmr_net_init,
3099 .exit = ipmr_net_exit,
Kirill Tkhaif84c6822018-02-13 12:29:52 +03003100 .async = true,
Benjamin Therycf958ae32009-01-22 04:56:16 +00003101};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003102
Wang Chen03d2f892008-07-03 12:13:36 +08003103int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003104{
Wang Chen03d2f892008-07-03 12:13:36 +08003105 int err;
3106
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3108 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00003109 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003110 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08003111
Benjamin Therycf958ae32009-01-22 04:56:16 +00003112 err = register_pernet_subsys(&ipmr_net_ops);
3113 if (err)
3114 goto reg_pernet_fail;
3115
Wang Chen03d2f892008-07-03 12:13:36 +08003116 err = register_netdevice_notifier(&ip_mr_notifier);
3117 if (err)
3118 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07003119#ifdef CONFIG_IP_PIMSM_V2
3120 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00003121 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07003122 err = -EAGAIN;
3123 goto add_proto_fail;
3124 }
3125#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00003126 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003127 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003128 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003129 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003130 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003131 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03003132
3133 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
Florian Westphalb97bac62017-08-09 20:41:48 +02003134 NULL, ipmr_rtm_dumplink, 0);
Wang Chen03d2f892008-07-03 12:13:36 +08003135 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003136
Tom Goff403dbb92009-06-14 03:16:13 -07003137#ifdef CONFIG_IP_PIMSM_V2
3138add_proto_fail:
3139 unregister_netdevice_notifier(&ip_mr_notifier);
3140#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08003141reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003142 unregister_pernet_subsys(&ipmr_net_ops);
3143reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08003144 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08003145 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146}