blob: 470956d2d8ad96ec08042a641772987c4e3f6a0b [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{
Yuval Mintzbc67a0d2018-03-26 15:01:31 +0300653 return mr_call_vif_notifier(nb, net, RTNL_FAMILY_IPMR, event_type,
654 vif, vif_index, tb_id);
Yotam Gigi4d65b942017-09-27 08:23:13 +0200655}
656
Yotam Gigib3620532017-09-27 08:23:14 +0200657static int call_ipmr_vif_entry_notifiers(struct net *net,
658 enum fib_event_type event_type,
659 struct vif_device *vif,
660 vifi_t vif_index, u32 tb_id)
661{
Yuval Mintzbc67a0d2018-03-26 15:01:31 +0300662 return mr_call_vif_notifiers(net, RTNL_FAMILY_IPMR, event_type,
663 vif, vif_index, tb_id,
664 &net->ipv4.ipmr_seq);
Yotam Gigib3620532017-09-27 08:23:14 +0200665}
666
Yotam Gigi4d65b942017-09-27 08:23:13 +0200667static int call_ipmr_mfc_entry_notifier(struct notifier_block *nb,
668 struct net *net,
669 enum fib_event_type event_type,
670 struct mfc_cache *mfc, u32 tb_id)
671{
Yuval Mintz54c4cad2018-03-26 15:01:32 +0300672 return mr_call_mfc_notifier(nb, net, RTNL_FAMILY_IPMR,
673 event_type, &mfc->_c, tb_id);
Yotam Gigi4d65b942017-09-27 08:23:13 +0200674}
675
Yotam Gigib3620532017-09-27 08:23:14 +0200676static int call_ipmr_mfc_entry_notifiers(struct net *net,
677 enum fib_event_type event_type,
678 struct mfc_cache *mfc, u32 tb_id)
679{
Yuval Mintz54c4cad2018-03-26 15:01:32 +0300680 return mr_call_mfc_notifiers(net, RTNL_FAMILY_IPMR, event_type,
681 &mfc->_c, tb_id, &net->ipv4.ipmr_seq);
Yotam Gigib3620532017-09-27 08:23:14 +0200682}
683
Ben Hutchings2c530402012-07-10 10:55:09 +0000684/**
685 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700686 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000688static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000689 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Yotam Gigib3620532017-09-27 08:23:14 +0200691 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 struct vif_device *v;
693 struct net_device *dev;
694 struct in_device *in_dev;
695
Patrick McHardy0c122952010-04-13 05:03:22 +0000696 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return -EADDRNOTAVAIL;
698
Patrick McHardy0c122952010-04-13 05:03:22 +0000699 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700
Yotam Gigib3620532017-09-27 08:23:14 +0200701 if (VIF_EXISTS(mrt, vifi))
702 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
703 mrt->id);
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 write_lock_bh(&mrt_lock);
706 dev = v->dev;
707 v->dev = NULL;
708
709 if (!dev) {
710 write_unlock_bh(&mrt_lock);
711 return -EADDRNOTAVAIL;
712 }
713
Patrick McHardy0c122952010-04-13 05:03:22 +0000714 if (vifi == mrt->mroute_reg_vif_num)
715 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000717 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000719
720 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000721 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 break;
723 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000724 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 }
726
727 write_unlock_bh(&mrt_lock);
728
729 dev_set_allmulti(dev, -1);
730
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000731 in_dev = __in_dev_get_rtnl(dev);
732 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700733 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -0700734 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000735 NETCONFA_MC_FORWARDING,
736 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 ip_rt_multicast_event(in_dev);
738 }
739
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000740 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000741 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 dev_put(dev);
744 return 0;
745}
746
Eric Dumazeta8c94862010-10-01 16:15:08 +0000747static void ipmr_cache_free_rcu(struct rcu_head *head)
748{
Yuval Mintz494fff52018-02-28 23:29:34 +0200749 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000750
Yuval Mintz494fff52018-02-28 23:29:34 +0200751 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000752}
753
Yotam Gigi310ebbb2017-09-27 08:23:12 +0200754void ipmr_cache_free(struct mfc_cache *c)
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000755{
Yuval Mintz494fff52018-02-28 23:29:34 +0200756 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000757}
Yotam Gigi310ebbb2017-09-27 08:23:12 +0200758EXPORT_SYMBOL(ipmr_cache_free);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000759
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000761 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000763static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200765 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700767 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768
Patrick McHardy0c122952010-04-13 05:03:22 +0000769 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Yuval Mintz494fff52018-02-28 23:29:34 +0200771 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700772 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200773 struct nlmsghdr *nlh = skb_pull(skb,
774 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000776 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000778 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700779 e->error = -ETIMEDOUT;
780 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700781
Eric W. Biederman15e47302012-09-07 20:12:54 +0000782 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000783 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000788 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789}
790
Patrick McHardye258beb2010-04-13 05:03:19 +0000791/* Timer process for the unresolved queue. */
Kees Cooke99e88a2017-10-16 14:43:17 -0700792static void ipmr_expire_process(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793{
Kees Cooke99e88a2017-10-16 14:43:17 -0700794 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
Yuval Mintz494fff52018-02-28 23:29:34 +0200795 struct mr_mfc *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 unsigned long expires;
Yuval Mintz494fff52018-02-28 23:29:34 +0200797 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798
799 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000800 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 return;
802 }
803
Patrick McHardy0c122952010-04-13 05:03:22 +0000804 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 goto out;
806
807 now = jiffies;
808 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Patrick McHardy0c122952010-04-13 05:03:22 +0000810 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 if (time_after(c->mfc_un.unres.expires, now)) {
812 unsigned long interval = c->mfc_un.unres.expires - now;
813 if (interval < expires)
814 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 continue;
816 }
817
Patrick McHardy862465f2010-04-13 05:03:21 +0000818 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +0200819 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
820 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 }
822
Patrick McHardy0c122952010-04-13 05:03:22 +0000823 if (!list_empty(&mrt->mfc_unres_queue))
824 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826out:
827 spin_unlock(&mfc_unres_lock);
828}
829
830/* Fill oifs list. It is called under write locked mrt_lock. */
Yuval Mintz494fff52018-02-28 23:29:34 +0200831static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000832 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 int vifi;
835
836 cache->mfc_un.res.minvif = MAXVIFS;
837 cache->mfc_un.res.maxvif = 0;
838 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
839
Patrick McHardy0c122952010-04-13 05:03:22 +0000840 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
841 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000842 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
844 if (cache->mfc_un.res.minvif > vifi)
845 cache->mfc_un.res.minvif = vifi;
846 if (cache->mfc_un.res.maxvif <= vifi)
847 cache->mfc_un.res.maxvif = vifi + 1;
848 }
849 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200850 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Patrick McHardy0c122952010-04-13 05:03:22 +0000853static int vif_add(struct net *net, struct mr_table *mrt,
854 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855{
856 int vifi = vifc->vifc_vifi;
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200857 struct switchdev_attr attr = {
858 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
859 };
Patrick McHardy0c122952010-04-13 05:03:22 +0000860 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 struct net_device *dev;
862 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700863 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000866 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 return -EADDRINUSE;
868
869 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100871 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100872 return -EINVAL;
873 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 * All the packets will be sent to the daemon
875 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000876 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000878 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 if (!dev)
880 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700881 err = dev_set_allmulti(dev, 1);
882 if (err) {
883 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700884 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700885 return err;
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900888 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000889 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 if (!dev)
891 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700892 err = dev_set_allmulti(dev, 1);
893 if (err) {
894 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700895 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700896 return err;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000899 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000901 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
902 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100903 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000904 dev_put(dev);
905 return -EADDRNOTAVAIL;
906 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000907 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000908 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000909 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (!dev)
911 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700912 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700913 if (err) {
914 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700915 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700916 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 break;
918 default:
919 return -EINVAL;
920 }
921
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000922 in_dev = __in_dev_get_rtnl(dev);
923 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000924 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000926 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700927 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700928 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
929 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 ip_rt_multicast_event(in_dev);
931
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000932 /* Fill in the VIF structures */
Yuval Mintz6853f212018-02-28 23:29:29 +0200933 vif_device_init(v, dev, vifc->vifc_rate_limit,
934 vifc->vifc_threshold,
935 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
936 (VIFF_TUNNEL | VIFF_REGISTER));
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000937
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200938 attr.orig_dev = dev;
939 if (!switchdev_port_attr_get(dev, &attr)) {
940 memcpy(v->dev_parent_id.id, attr.u.ppid.id, attr.u.ppid.id_len);
941 v->dev_parent_id.id_len = attr.u.ppid.id_len;
942 } else {
943 v->dev_parent_id.id_len = 0;
944 }
Yuval Mintz6853f212018-02-28 23:29:29 +0200945
Jianjun Kongc354e122008-11-03 00:28:02 -0800946 v->local = vifc->vifc_lcl_addr.s_addr;
947 v->remote = vifc->vifc_rmt_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
949 /* And finish update writing critical data */
950 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800951 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000952 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000953 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000954 if (vifi+1 > mrt->maxvif)
955 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +0200957 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return 0;
959}
960
Eric Dumazeta8c94862010-10-01 16:15:08 +0000961/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000962static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000963 __be32 origin,
964 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100966 struct mfc_cache_cmp_arg arg = {
967 .mfc_mcastgrp = mcastgrp,
968 .mfc_origin = origin
969 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970
Yuval Mintz845c9a72018-02-28 23:29:35 +0200971 return mr_mfc_find(mrt, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000972}
973
974/* Look for a (*,G) entry */
975static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
976 __be32 mcastgrp, int vifi)
977{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100978 struct mfc_cache_cmp_arg arg = {
979 .mfc_mcastgrp = mcastgrp,
980 .mfc_origin = htonl(INADDR_ANY)
981 };
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000982
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100983 if (mcastgrp == htonl(INADDR_ANY))
Yuval Mintz845c9a72018-02-28 23:29:35 +0200984 return mr_mfc_find_any_parent(mrt, vifi);
985 return mr_mfc_find_any(mrt, vifi, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000986}
987
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100988/* Look for a (S,G,iif) entry if parent != -1 */
989static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
990 __be32 origin, __be32 mcastgrp,
991 int parent)
992{
993 struct mfc_cache_cmp_arg arg = {
994 .mfc_mcastgrp = mcastgrp,
995 .mfc_origin = origin,
996 };
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100997
Yuval Mintz845c9a72018-02-28 23:29:35 +0200998 return mr_mfc_find_parent(mrt, &arg, parent);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100999}
1000
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001001/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001002static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003{
Jianjun Kongc354e122008-11-03 00:28:02 -08001004 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001005
Tom Goff70a0dec2016-06-23 16:11:57 -04001006 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001007 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
1008 c->_c.mfc_un.res.minvif = MAXVIFS;
1009 refcount_set(&c->_c.mfc_un.res.refcount, 1);
Tom Goff70a0dec2016-06-23 16:11:57 -04001010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 return c;
1012}
1013
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001014static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015{
Jianjun Kongc354e122008-11-03 00:28:02 -08001016 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001017
1018 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001019 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
1020 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001021 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return c;
1023}
1024
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001025/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +00001026static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1027 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028{
1029 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001030 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001032 /* Play the pending entries through our router */
Yuval Mintz494fff52018-02-28 23:29:34 +02001033 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001034 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +02001035 struct nlmsghdr *nlh = skb_pull(skb,
1036 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Yuval Mintz7b0db852018-02-28 23:29:39 +02001038 if (mr_fill_mroute(mrt, skb, &c->_c,
1039 nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001040 nlh->nlmsg_len = skb_tail_pointer(skb) -
1041 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 } else {
1043 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +00001044 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +00001046 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001047 e->error = -EMSGSIZE;
1048 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 }
Thomas Graf2942e902006-08-15 00:30:25 -07001050
Eric W. Biederman15e47302012-09-07 20:12:54 +00001051 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001052 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001053 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001054 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 }
1056}
1057
Julien Gomes5a645dd2017-06-20 13:54:17 -07001058/* Bounce a cache query up to mrouted and netlink.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001060 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001062static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001063 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001065 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001066 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 struct igmphdr *igmp;
1068 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001069 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 int ret;
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 if (assert == IGMPMSG_WHOLEPKT)
1073 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1074 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 skb = alloc_skb(128, GFP_ATOMIC);
1076
Stephen Hemminger132adf52007-03-08 20:44:43 -08001077 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 return -ENOBUFS;
1079
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 if (assert == IGMPMSG_WHOLEPKT) {
1081 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001082 * Duplicate old header, fix ihl, length etc.
1083 * And all this only to mangle msg->im_msgtype and
1084 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001086 skb_push(skb, sizeof(struct iphdr));
1087 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001088 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001089 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001090 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1092 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001093 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001094 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1095 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1096 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001097 } else {
1098 /* Copy the IP header */
1099 skb_set_network_header(skb, skb->len);
1100 skb_put(skb, ihl);
1101 skb_copy_to_linear_data(skb, pkt->data, ihl);
1102 /* Flag to the kernel this is a route add */
1103 ip_hdr(skb)->protocol = 0;
1104 msg = (struct igmpmsg *)skb_network_header(skb);
1105 msg->im_vif = vifi;
1106 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1107 /* Add our header */
Johannes Berg4df864c2017-06-16 14:29:21 +02001108 igmp = skb_put(skb, sizeof(struct igmphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001109 igmp->type = assert;
1110 msg->im_msgtype = assert;
1111 igmp->code = 0;
1112 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1113 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001114 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115
Eric Dumazet4c968702010-10-01 16:15:01 +00001116 rcu_read_lock();
1117 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001118 if (!mroute_sk) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001119 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 kfree_skb(skb);
1121 return -EINVAL;
1122 }
1123
Julien Gomes5a645dd2017-06-20 13:54:17 -07001124 igmpmsg_netlink_event(mrt, skb);
1125
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001126 /* Deliver to mrouted */
Eric Dumazet4c968702010-10-01 16:15:01 +00001127 ret = sock_queue_rcv_skb(mroute_sk, skb);
1128 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001129 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001130 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 kfree_skb(skb);
1132 }
1133
1134 return ret;
1135}
1136
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001137/* Queue a packet for resolution. It gets locked cache entry! */
1138static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001139 struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001141 const struct iphdr *iph = ip_hdr(skb);
1142 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001143 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145
1146 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001147 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001148 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001149 c->mfc_origin == iph->saddr) {
1150 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001152 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 }
1154
Patrick McHardy862465f2010-04-13 05:03:21 +00001155 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001156 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001157 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001158 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 spin_unlock_bh(&mfc_unres_lock);
1160
1161 kfree_skb(skb);
1162 return -ENOBUFS;
1163 }
1164
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001165 /* Fill in the new cache entry */
Yuval Mintz494fff52018-02-28 23:29:34 +02001166 c->_c.mfc_parent = -1;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001167 c->mfc_origin = iph->saddr;
1168 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001170 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001171 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Yuval Mintz494fff52018-02-28 23:29:34 +02001172
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001173 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001174 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 out - Brad Parker
1176 */
1177 spin_unlock_bh(&mfc_unres_lock);
1178
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001179 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 kfree_skb(skb);
1181 return err;
1182 }
1183
Patrick McHardy0c122952010-04-13 05:03:22 +00001184 atomic_inc(&mrt->cache_resolve_queue_len);
Yuval Mintz494fff52018-02-28 23:29:34 +02001185 list_add(&c->_c.list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001186 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
David S. Miller278554b2010-05-12 00:05:35 -07001188 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
Yuval Mintz494fff52018-02-28 23:29:34 +02001189 mod_timer(&mrt->ipmr_expire_timer,
1190 c->_c.mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 }
1192
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001193 /* See if we can append the packet */
Yuval Mintz494fff52018-02-28 23:29:34 +02001194 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195 kfree_skb(skb);
1196 err = -ENOBUFS;
1197 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001198 if (dev) {
1199 skb->dev = dev;
1200 skb->skb_iif = dev->ifindex;
1201 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001202 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 err = 0;
1204 }
1205
1206 spin_unlock_bh(&mfc_unres_lock);
1207 return err;
1208}
1209
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001210/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001212static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213{
Yotam Gigib3620532017-09-27 08:23:14 +02001214 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001215 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001217 /* The entries are added/deleted only under RTNL */
1218 rcu_read_lock();
1219 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1220 mfc->mfcc_mcastgrp.s_addr, parent);
1221 rcu_read_unlock();
1222 if (!c)
1223 return -ENOENT;
Yuval Mintz494fff52018-02-28 23:29:34 +02001224 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1225 list_del_rcu(&c->_c.list);
Yotam Gigib3620532017-09-27 08:23:14 +02001226 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001227 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yotam Gigi310ebbb2017-09-27 08:23:12 +02001228 ipmr_cache_put(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Patrick McHardy0c122952010-04-13 05:03:22 +00001233static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001234 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235{
Patrick McHardy862465f2010-04-13 05:03:21 +00001236 struct mfc_cache *uc, *c;
Yuval Mintz494fff52018-02-28 23:29:34 +02001237 struct mr_mfc *_uc;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001238 bool found;
1239 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Patrick McHardya50436f22010-03-17 06:04:14 +00001241 if (mfc->mfcc_parent >= MAXVIFS)
1242 return -ENFILE;
1243
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001244 /* The entries are added/deleted only under RTNL */
1245 rcu_read_lock();
1246 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1247 mfc->mfcc_mcastgrp.s_addr, parent);
1248 rcu_read_unlock();
1249 if (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 write_lock_bh(&mrt_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001251 c->_c.mfc_parent = mfc->mfcc_parent;
1252 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001254 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +02001256 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1257 mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001258 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 return 0;
1260 }
1261
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001262 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001263 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 return -EINVAL;
1265
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001266 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001267 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return -ENOMEM;
1269
Jianjun Kongc354e122008-11-03 00:28:02 -08001270 c->mfc_origin = mfc->mfcc_origin.s_addr;
1271 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
Yuval Mintz494fff52018-02-28 23:29:34 +02001272 c->_c.mfc_parent = mfc->mfcc_parent;
1273 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001275 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Yuval Mintz494fff52018-02-28 23:29:34 +02001277 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001278 ipmr_rht_params);
1279 if (ret) {
1280 pr_err("ipmr: rhtable insert error %d\n", ret);
1281 ipmr_cache_free(c);
1282 return ret;
1283 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001284 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001285 /* Check to see if we resolved a queued list. If so we
1286 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001288 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001290 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1291 uc = (struct mfc_cache *)_uc;
Patrick McHardye258beb2010-04-13 05:03:19 +00001292 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001294 list_del(&_uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001295 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001296 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 break;
1298 }
1299 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001300 if (list_empty(&mrt->mfc_unres_queue))
1301 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302 spin_unlock_bh(&mfc_unres_lock);
1303
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001304 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001305 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001306 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307 }
Yotam Gigib3620532017-09-27 08:23:14 +02001308 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001309 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return 0;
1311}
1312
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001313/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001314static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315{
Yotam Gigib3620532017-09-27 08:23:14 +02001316 struct net *net = read_pnet(&mrt->net);
Yuval Mintz494fff52018-02-28 23:29:34 +02001317 struct mr_mfc *c, *tmp;
1318 struct mfc_cache *cache;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001319 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001320 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001321
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001322 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001323 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001324 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1325 continue;
1326 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001328 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001330 /* Wipe the cache */
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001331 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1332 if (!all && (c->mfc_flags & MFC_STATIC))
1333 continue;
1334 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1335 list_del_rcu(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +02001336 cache = (struct mfc_cache *)c;
1337 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
Yotam Gigib3620532017-09-27 08:23:14 +02001338 mrt->id);
Yuval Mintz494fff52018-02-28 23:29:34 +02001339 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1340 ipmr_cache_put(cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 }
1342
Patrick McHardy0c122952010-04-13 05:03:22 +00001343 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 spin_lock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001345 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001346 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +02001347 cache = (struct mfc_cache *)c;
1348 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1349 ipmr_destroy_unres(mrt, cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 }
1351 spin_unlock_bh(&mfc_unres_lock);
1352 }
1353}
1354
Eric Dumazet4c968702010-10-01 16:15:01 +00001355/* called from ip_ra_control(), before an RCU grace period,
1356 * we dont need to call synchronize_rcu() here
1357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358static void mrtsock_destruct(struct sock *sk)
1359{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001360 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001361 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001362
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001363 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001364 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001365 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001366 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001367 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1368 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001369 NETCONFA_IFINDEX_ALL,
1370 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001371 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001372 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 }
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001375 rtnl_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001376}
1377
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001378/* Socket options and virtual interface manipulation. The whole
1379 * virtual interface system is a complete heap, but unfortunately
1380 * that's how BSD mrouted happens to think. Maybe one day with a proper
1381 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001383
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001384int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1385 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001387 struct net *net = sock_net(sk);
1388 int val, ret = 0, parent = 0;
1389 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 struct vifctl vif;
1391 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001392 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001393
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001394 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1395 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001396 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001397 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1398 ret = -EOPNOTSUPP;
1399 goto out_unlock;
1400 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001401
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001402 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001403 if (!mrt) {
1404 ret = -ENOENT;
1405 goto out_unlock;
1406 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001407 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001408 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001409 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1410 ret = -EACCES;
1411 goto out_unlock;
1412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 }
1414
Stephen Hemminger132adf52007-03-08 20:44:43 -08001415 switch (optname) {
1416 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001417 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001418 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001419 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001420 }
1421 if (rtnl_dereference(mrt->mroute_sk)) {
1422 ret = -EADDRINUSE;
1423 break;
1424 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001425
1426 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1427 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001428 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001429 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001430 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1431 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001432 NETCONFA_IFINDEX_ALL,
1433 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001434 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001435 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001436 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001437 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1438 ret = -EACCES;
1439 } else {
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001440 /* We need to unlock here because mrtsock_destruct takes
1441 * care of rtnl itself and we can't change that due to
1442 * the IP_ROUTER_ALERT setsockopt which runs without it.
1443 */
1444 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001445 ret = ip_ra_control(sk, 0, NULL);
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001446 goto out;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001447 }
1448 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001449 case MRT_ADD_VIF:
1450 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001451 if (optlen != sizeof(vif)) {
1452 ret = -EINVAL;
1453 break;
1454 }
1455 if (copy_from_user(&vif, optval, sizeof(vif))) {
1456 ret = -EFAULT;
1457 break;
1458 }
1459 if (vif.vifc_vifi >= MAXVIFS) {
1460 ret = -ENFILE;
1461 break;
1462 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001463 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001464 ret = vif_add(net, mrt, &vif,
1465 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001466 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001467 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001468 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001469 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001470 /* Manipulate the forwarding caches. These live
1471 * in a sort of kernel/user symbiosis.
1472 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001473 case MRT_ADD_MFC:
1474 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001475 parent = -1;
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05001476 /* fall through */
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001477 case MRT_ADD_MFC_PROXY:
1478 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001479 if (optlen != sizeof(mfc)) {
1480 ret = -EINVAL;
1481 break;
1482 }
1483 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1484 ret = -EFAULT;
1485 break;
1486 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001487 if (parent == 0)
1488 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001489 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1490 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001491 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001492 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001493 sk == rtnl_dereference(mrt->mroute_sk),
1494 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001495 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001496 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001497 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001498 if (optlen != sizeof(val)) {
1499 ret = -EINVAL;
1500 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001502 if (get_user(val, (int __user *)optval)) {
1503 ret = -EFAULT;
1504 break;
1505 }
1506 mrt->mroute_do_assert = val;
1507 break;
1508 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001509 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001510 ret = -ENOPROTOOPT;
1511 break;
1512 }
1513 if (optlen != sizeof(val)) {
1514 ret = -EINVAL;
1515 break;
1516 }
1517 if (get_user(val, (int __user *)optval)) {
1518 ret = -EFAULT;
1519 break;
1520 }
1521
1522 val = !!val;
1523 if (val != mrt->mroute_do_pim) {
1524 mrt->mroute_do_pim = val;
1525 mrt->mroute_do_assert = val;
1526 }
1527 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001528 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001529 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1530 ret = -ENOPROTOOPT;
1531 break;
1532 }
1533 if (optlen != sizeof(uval)) {
1534 ret = -EINVAL;
1535 break;
1536 }
1537 if (get_user(uval, (u32 __user *)optval)) {
1538 ret = -EFAULT;
1539 break;
1540 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001541
Eric Dumazet4c968702010-10-01 16:15:01 +00001542 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1543 ret = -EBUSY;
1544 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001545 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001546 if (IS_ERR(mrt))
1547 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001548 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001549 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c968702010-10-01 16:15:01 +00001550 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001551 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001552 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001553 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001554 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001556out_unlock:
1557 rtnl_unlock();
Kirill Tkhai128aaa92018-03-22 12:45:22 +03001558out:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001559 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560}
1561
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001562/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001563int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564{
1565 int olr;
1566 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001567 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001568 struct mr_table *mrt;
1569
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001570 if (sk->sk_type != SOCK_RAW ||
1571 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1572 return -EOPNOTSUPP;
1573
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001574 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001575 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001576 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001578 switch (optname) {
1579 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001580 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001581 break;
1582 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001583 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001584 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001585 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001586 break;
1587 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001588 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001589 break;
1590 default:
1591 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001592 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001593
1594 if (get_user(olr, optlen))
1595 return -EFAULT;
1596 olr = min_t(unsigned int, olr, sizeof(int));
1597 if (olr < 0)
1598 return -EINVAL;
1599 if (put_user(olr, optlen))
1600 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001601 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 return -EFAULT;
1603 return 0;
1604}
1605
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001606/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1608{
1609 struct sioc_sg_req sr;
1610 struct sioc_vif_req vr;
1611 struct vif_device *vif;
1612 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001613 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001614 struct mr_table *mrt;
1615
1616 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001617 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001618 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001619
Stephen Hemminger132adf52007-03-08 20:44:43 -08001620 switch (cmd) {
1621 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001622 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001623 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001624 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001625 return -EINVAL;
1626 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001627 vif = &mrt->vif_table[vr.vifi];
1628 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001629 vr.icount = vif->pkt_in;
1630 vr.ocount = vif->pkt_out;
1631 vr.ibytes = vif->bytes_in;
1632 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001634
Jianjun Kongc354e122008-11-03 00:28:02 -08001635 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001637 return 0;
1638 }
1639 read_unlock(&mrt_lock);
1640 return -EADDRNOTAVAIL;
1641 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001642 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001643 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Eric Dumazeta8c94862010-10-01 16:15:08 +00001645 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001646 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001647 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001648 sr.pktcnt = c->_c.mfc_un.res.pkt;
1649 sr.bytecnt = c->_c.mfc_un.res.bytes;
1650 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001651 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001652
Jianjun Kongc354e122008-11-03 00:28:02 -08001653 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001654 return -EFAULT;
1655 return 0;
1656 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001657 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001658 return -EADDRNOTAVAIL;
1659 default:
1660 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 }
1662}
1663
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001664#ifdef CONFIG_COMPAT
1665struct compat_sioc_sg_req {
1666 struct in_addr src;
1667 struct in_addr grp;
1668 compat_ulong_t pktcnt;
1669 compat_ulong_t bytecnt;
1670 compat_ulong_t wrong_if;
1671};
1672
David S. Millerca6b8bb02011-02-03 17:24:28 -08001673struct compat_sioc_vif_req {
1674 vifi_t vifi; /* Which iface */
1675 compat_ulong_t icount;
1676 compat_ulong_t ocount;
1677 compat_ulong_t ibytes;
1678 compat_ulong_t obytes;
1679};
1680
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001681int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1682{
David S. Miller0033d5a2011-02-03 17:21:31 -08001683 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001684 struct compat_sioc_vif_req vr;
1685 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001686 struct mfc_cache *c;
1687 struct net *net = sock_net(sk);
1688 struct mr_table *mrt;
1689
1690 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001691 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001692 return -ENOENT;
1693
1694 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001695 case SIOCGETVIFCNT:
1696 if (copy_from_user(&vr, arg, sizeof(vr)))
1697 return -EFAULT;
1698 if (vr.vifi >= mrt->maxvif)
1699 return -EINVAL;
1700 read_lock(&mrt_lock);
1701 vif = &mrt->vif_table[vr.vifi];
1702 if (VIF_EXISTS(mrt, vr.vifi)) {
1703 vr.icount = vif->pkt_in;
1704 vr.ocount = vif->pkt_out;
1705 vr.ibytes = vif->bytes_in;
1706 vr.obytes = vif->bytes_out;
1707 read_unlock(&mrt_lock);
1708
1709 if (copy_to_user(arg, &vr, sizeof(vr)))
1710 return -EFAULT;
1711 return 0;
1712 }
1713 read_unlock(&mrt_lock);
1714 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001715 case SIOCGETSGCNT:
1716 if (copy_from_user(&sr, arg, sizeof(sr)))
1717 return -EFAULT;
1718
1719 rcu_read_lock();
1720 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1721 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001722 sr.pktcnt = c->_c.mfc_un.res.pkt;
1723 sr.bytecnt = c->_c.mfc_un.res.bytes;
1724 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001725 rcu_read_unlock();
1726
1727 if (copy_to_user(arg, &sr, sizeof(sr)))
1728 return -EFAULT;
1729 return 0;
1730 }
1731 rcu_read_unlock();
1732 return -EADDRNOTAVAIL;
1733 default:
1734 return -ENOIOCTLCMD;
1735 }
1736}
1737#endif
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1740{
Jiri Pirko351638e2013-05-28 01:30:21 +00001741 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001742 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001743 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 struct vif_device *v;
1745 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001746
Linus Torvalds1da177e2005-04-16 15:20:36 -07001747 if (event != NETDEV_UNREGISTER)
1748 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001749
1750 ipmr_for_each_table(mrt, net) {
1751 v = &mrt->vif_table[0];
1752 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1753 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001754 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001755 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 }
1757 return NOTIFY_DONE;
1758}
1759
Jianjun Kongc354e122008-11-03 00:28:02 -08001760static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 .notifier_call = ipmr_device_event,
1762};
1763
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001764/* Encapsulate a packet by attaching a valid IPIP header to it.
1765 * This avoids tunnel drivers and other mess and gives us the speed so
1766 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001768static void ip_encap(struct net *net, struct sk_buff *skb,
1769 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001771 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001772 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001773
1774 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001775 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001776 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001777 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001779 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001780 iph->tos = old_iph->tos;
1781 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 iph->frag_off = 0;
1783 iph->daddr = daddr;
1784 iph->saddr = saddr;
1785 iph->protocol = IPPROTO_IPIP;
1786 iph->ihl = 5;
1787 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001788 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 ip_send_check(iph);
1790
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1792 nf_reset(skb);
1793}
1794
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001795static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1796 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001798 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
David S. Miller73186df2015-11-03 13:41:45 -05001800 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1801 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
1803 if (unlikely(opt->optlen))
1804 ip_forward_options(skb);
1805
Eric W. Biederman13206b62015-10-07 16:48:35 -05001806 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807}
1808
Yotam Gigia5bc9292017-10-03 09:58:08 +02001809#ifdef CONFIG_NET_SWITCHDEV
1810static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1811 int in_vifi, int out_vifi)
1812{
1813 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1814 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1815
1816 if (!skb->offload_mr_fwd_mark)
1817 return false;
1818 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1819 return false;
1820 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1821 &in_vif->dev_parent_id);
1822}
1823#else
1824static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1825 int in_vifi, int out_vifi)
1826{
1827 return false;
1828}
1829#endif
1830
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001831/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Patrick McHardy0c122952010-04-13 05:03:22 +00001833static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
Yotam Gigia5bc9292017-10-03 09:58:08 +02001834 int in_vifi, struct sk_buff *skb,
1835 struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001837 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001838 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 struct net_device *dev;
1840 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001841 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 int encap = 0;
1843
Ian Morris51456b22015-04-03 09:17:26 +01001844 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 goto out_free;
1846
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (vif->flags & VIFF_REGISTER) {
1848 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001849 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001850 vif->dev->stats.tx_bytes += skb->len;
1851 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001852 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001853 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
Yotam Gigia5bc9292017-10-03 09:58:08 +02001856 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1857 goto out_free;
1858
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001859 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001860 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001861 vif->remote, vif->local,
1862 0, 0,
1863 IPPROTO_IPIP,
1864 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001865 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 goto out_free;
1867 encap = sizeof(struct iphdr);
1868 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001869 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001870 0, 0,
1871 IPPROTO_IPIP,
1872 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001873 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 goto out_free;
1875 }
1876
Changli Gaod8d1f302010-06-10 23:31:35 -07001877 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878
Changli Gaod8d1f302010-06-10 23:31:35 -07001879 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001881 * allow to send ICMP, so that packets will disappear
1882 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 */
David S. Miller73186df2015-11-03 13:41:45 -05001884 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 ip_rt_put(rt);
1886 goto out_free;
1887 }
1888
Changli Gaod8d1f302010-06-10 23:31:35 -07001889 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890
1891 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001892 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 goto out_free;
1894 }
1895
1896 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001897 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898
Eric Dumazetadf30902009-06-02 05:19:30 +00001899 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001900 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001901 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
1903 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001904 * What do we do with netfilter? -- RR
1905 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001907 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001909 vif->dev->stats.tx_packets++;
1910 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 }
1912
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001913 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001915 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 * not only before forwarding, but after forwarding on all output
1917 * interfaces. It is clear, if mrouter runs a multicasting
1918 * program, it should receive packets not depending to what interface
1919 * program is joined.
1920 * If we will not make it, the program will have to join on all
1921 * interfaces. On the other hand, multihoming host (or router, but
1922 * not mrouter) cannot join to more than one interface - it will
1923 * result in receiving multiple packets.
1924 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001925 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1926 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 ipmr_forward_finish);
1928 return;
1929
1930out_free:
1931 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932}
1933
Patrick McHardy0c122952010-04-13 05:03:22 +00001934static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935{
1936 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001937
1938 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1939 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 break;
1941 }
1942 return ct;
1943}
1944
1945/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001946static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001947 struct net_device *dev, struct sk_buff *skb,
Yuval Mintz494fff52018-02-28 23:29:34 +02001948 struct mfc_cache *c, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949{
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001950 int true_vifi = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 int psend = -1;
1952 int vif, ct;
1953
Yuval Mintz494fff52018-02-28 23:29:34 +02001954 vif = c->_c.mfc_parent;
1955 c->_c.mfc_un.res.pkt++;
1956 c->_c.mfc_un.res.bytes += skb->len;
1957 c->_c.mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
Yuval Mintz494fff52018-02-28 23:29:34 +02001959 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001960 struct mfc_cache *cache_proxy;
1961
1962 /* For an (*,G) entry, we only check that the incomming
1963 * interface is part of the static tree.
1964 */
Yuval Mintz845c9a72018-02-28 23:29:35 +02001965 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001966 if (cache_proxy &&
Yuval Mintz494fff52018-02-28 23:29:34 +02001967 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001968 goto forward;
1969 }
1970
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001971 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001972 if (mrt->vif_table[vif].dev != dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001973 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001975 * Very complicated situation...
1976 *
1977 * The best workaround until routing daemons will be
1978 * fixed is not to redistribute packet, if it was
1979 * send through wrong interface. It means, that
1980 * multicast applications WILL NOT work for
1981 * (S,G), which have default multicast route pointing
1982 * to wrong oif. In any case, it is not a good
1983 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984 */
1985 goto dont_forward;
1986 }
1987
Yuval Mintz494fff52018-02-28 23:29:34 +02001988 c->_c.mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989
Patrick McHardy0c122952010-04-13 05:03:22 +00001990 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001992 * so that we cannot check that packet arrived on an oif.
1993 * It is bad, but otherwise we would need to move pretty
1994 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001996 (mrt->mroute_do_pim ||
Yuval Mintz494fff52018-02-28 23:29:34 +02001997 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001998 time_after(jiffies,
Yuval Mintz494fff52018-02-28 23:29:34 +02001999 c->_c.mfc_un.res.last_assert +
2000 MFC_ASSERT_THRESH)) {
2001 c->_c.mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00002002 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 }
2004 goto dont_forward;
2005 }
2006
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002007forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00002008 mrt->vif_table[vif].pkt_in++;
2009 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002010
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002011 /* Forward the frame */
Yuval Mintz494fff52018-02-28 23:29:34 +02002012 if (c->mfc_origin == htonl(INADDR_ANY) &&
2013 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002014 if (true_vifi >= 0 &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002015 true_vifi != c->_c.mfc_parent &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002016 ip_hdr(skb)->ttl >
Yuval Mintz494fff52018-02-28 23:29:34 +02002017 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002018 /* It's an (*,*) entry and the packet is not coming from
2019 * the upstream: forward the packet to the upstream
2020 * only.
2021 */
Yuval Mintz494fff52018-02-28 23:29:34 +02002022 psend = c->_c.mfc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002023 goto last_forward;
2024 }
2025 goto dont_forward;
2026 }
Yuval Mintz494fff52018-02-28 23:29:34 +02002027 for (ct = c->_c.mfc_un.res.maxvif - 1;
2028 ct >= c->_c.mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002029 /* For (*,G) entry, don't forward to the incoming interface */
Yuval Mintz494fff52018-02-28 23:29:34 +02002030 if ((c->mfc_origin != htonl(INADDR_ANY) ||
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002031 ct != true_vifi) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002032 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 if (psend != -1) {
2034 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002037 ipmr_queue_xmit(net, mrt, true_vifi,
Yuval Mintz494fff52018-02-28 23:29:34 +02002038 skb2, c, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002040 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 }
2042 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002043last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (psend != -1) {
2045 if (local) {
2046 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002049 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
Yuval Mintz494fff52018-02-28 23:29:34 +02002050 c, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 } else {
Yuval Mintz494fff52018-02-28 23:29:34 +02002052 ipmr_queue_xmit(net, mrt, true_vifi, skb, c, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03002053 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 }
2055 }
2056
2057dont_forward:
2058 if (!local)
2059 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060}
2061
David S. Miller417da662011-05-03 19:42:43 -07002062static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08002063{
David S. Miller417da662011-05-03 19:42:43 -07002064 struct rtable *rt = skb_rtable(skb);
2065 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05002066 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07002067 .daddr = iph->daddr,
2068 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00002069 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07002070 .flowi4_oif = (rt_is_output_route(rt) ?
2071 skb->dev->ifindex : 0),
2072 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002073 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07002074 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00002075 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08002076 };
2077 struct mr_table *mrt;
2078 int err;
2079
David S. Millerda919812011-03-12 02:04:50 -05002080 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002081 if (err)
2082 return ERR_PTR(err);
2083 return mrt;
2084}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002086/* Multicast packets for forwarding arrive here
2087 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089int ip_mr_input(struct sk_buff *skb)
2090{
2091 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002092 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00002093 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002094 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002095 struct net_device *dev;
2096
2097 /* skb->dev passed in is the loX master dev for vrfs.
2098 * As there are no vifs associated with loopback devices,
2099 * get the proper interface that does have a vif associated with it.
2100 */
2101 dev = skb->dev;
2102 if (netif_is_l3_master(skb->dev)) {
2103 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2104 if (!dev) {
2105 kfree_skb(skb);
2106 return -ENODEV;
2107 }
2108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002111 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 */
Eric Dumazet4c968702010-10-01 16:15:01 +00002113 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 goto dont_forward;
2115
David S. Miller417da662011-05-03 19:42:43 -07002116 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002117 if (IS_ERR(mrt)) {
2118 kfree_skb(skb);
2119 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00002122 if (IPCB(skb)->opt.router_alert) {
2123 if (ip_call_ra_chain(skb))
2124 return 0;
2125 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2126 /* IGMPv1 (and broken IGMPv2 implementations sort of
2127 * Cisco IOS <= 11.2(8)) do not put router alert
2128 * option to IGMP packets destined to routable
2129 * groups. It is very bad, because it means
2130 * that we can forward NO IGMP messages.
2131 */
2132 struct sock *mroute_sk;
2133
2134 mroute_sk = rcu_dereference(mrt->mroute_sk);
2135 if (mroute_sk) {
2136 nf_reset(skb);
2137 raw_rcv(mroute_sk, skb);
2138 return 0;
2139 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 }
2141 }
2142
Eric Dumazeta8c94862010-10-01 16:15:08 +00002143 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002144 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002145 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002146 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002147
2148 if (vif >= 0)
2149 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2150 vif);
2151 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002152
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002153 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002154 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155 int vif;
2156
2157 if (local) {
2158 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2159 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002160 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 skb = skb2;
2163 }
2164
Eric Dumazeta8c94862010-10-01 16:15:08 +00002165 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002166 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (vif >= 0) {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002168 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 read_unlock(&mrt_lock);
2170
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002171 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 }
2173 read_unlock(&mrt_lock);
2174 kfree_skb(skb);
2175 return -ENODEV;
2176 }
2177
Eric Dumazeta8c94862010-10-01 16:15:08 +00002178 read_lock(&mrt_lock);
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002179 ip_mr_forward(net, mrt, dev, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 read_unlock(&mrt_lock);
2181
2182 if (local)
2183 return ip_local_deliver(skb);
2184
2185 return 0;
2186
2187dont_forward:
2188 if (local)
2189 return ip_local_deliver(skb);
2190 kfree_skb(skb);
2191 return 0;
2192}
2193
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002194#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002195/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002196int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002197{
2198 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002199 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002200 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002201
2202 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2203 goto drop;
2204
2205 pim = igmp_hdr(skb);
2206
David S. Miller417da662011-05-03 19:42:43 -07002207 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002208 if (IS_ERR(mrt))
2209 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002210 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002211 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2212 goto drop;
2213
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002214 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002215drop:
2216 kfree_skb(skb);
2217 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 return 0;
2219}
2220#endif
2221
2222#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002223static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
2225 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002226 struct net *net = dev_net(skb->dev);
2227 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002229 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 goto drop;
2231
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002232 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002233 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002234 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002235 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002236 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237 goto drop;
2238
David S. Miller417da662011-05-03 19:42:43 -07002239 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002240 if (IS_ERR(mrt))
2241 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002242 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002243drop:
2244 kfree_skb(skb);
2245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return 0;
2247}
2248#endif
2249
David S. Miller9a1b9492011-05-04 12:18:54 -07002250int ipmr_get_route(struct net *net, struct sk_buff *skb,
2251 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002252 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002255 struct mr_table *mrt;
2256 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002258 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002259 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002260 return -ENOENT;
2261
Eric Dumazeta8c94862010-10-01 16:15:08 +00002262 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002263 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002264 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002265 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002267 if (vif >= 0)
2268 cache = ipmr_cache_find_any(mrt, daddr, vif);
2269 }
Ian Morris51456b22015-04-03 09:17:26 +01002270 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002271 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002272 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002274 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002277 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002278 if (dev)
2279 vif = ipmr_find_vif(mrt, dev);
2280 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002282 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return -ENODEV;
2284 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002285 skb2 = skb_clone(skb, GFP_ATOMIC);
2286 if (!skb2) {
2287 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002288 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002289 return -ENOMEM;
2290 }
2291
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002292 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002293 skb_push(skb2, sizeof(struct iphdr));
2294 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002295 iph = ip_hdr(skb2);
2296 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002297 iph->saddr = saddr;
2298 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002299 iph->version = 0;
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002300 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002302 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return err;
2304 }
2305
Eric Dumazeta8c94862010-10-01 16:15:08 +00002306 read_lock(&mrt_lock);
Yuval Mintz7b0db852018-02-28 23:29:39 +02002307 err = mr_fill_mroute(mrt, skb, &cache->_c, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002309 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310 return err;
2311}
2312
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002313static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002314 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2315 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002316{
2317 struct nlmsghdr *nlh;
2318 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002319 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002320
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002321 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002322 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002323 return -EMSGSIZE;
2324
2325 rtm = nlmsg_data(nlh);
2326 rtm->rtm_family = RTNL_FAMILY_IPMR;
2327 rtm->rtm_dst_len = 32;
2328 rtm->rtm_src_len = 32;
2329 rtm->rtm_tos = 0;
2330 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002331 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2332 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002333 rtm->rtm_type = RTN_MULTICAST;
2334 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Yuval Mintz494fff52018-02-28 23:29:34 +02002335 if (c->_c.mfc_flags & MFC_STATIC)
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002336 rtm->rtm_protocol = RTPROT_STATIC;
2337 else
2338 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002339 rtm->rtm_flags = 0;
2340
Jiri Benc930345e2015-03-29 16:59:25 +02002341 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2342 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002343 goto nla_put_failure;
Yuval Mintz7b0db852018-02-28 23:29:39 +02002344 err = mr_fill_mroute(mrt, skb, &c->_c, rtm);
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002345 /* do not break the dump if cache is unresolved */
2346 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002347 goto nla_put_failure;
2348
Johannes Berg053c0952015-01-16 22:09:00 +01002349 nlmsg_end(skb, nlh);
2350 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002351
2352nla_put_failure:
2353 nlmsg_cancel(skb, nlh);
2354 return -EMSGSIZE;
2355}
2356
Yuval Mintz7b0db852018-02-28 23:29:39 +02002357static int _ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2358 u32 portid, u32 seq, struct mr_mfc *c, int cmd,
2359 int flags)
2360{
2361 return ipmr_fill_mroute(mrt, skb, portid, seq, (struct mfc_cache *)c,
2362 cmd, flags);
2363}
2364
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002365static size_t mroute_msgsize(bool unresolved, int maxvif)
2366{
2367 size_t len =
2368 NLMSG_ALIGN(sizeof(struct rtmsg))
2369 + nla_total_size(4) /* RTA_TABLE */
2370 + nla_total_size(4) /* RTA_SRC */
2371 + nla_total_size(4) /* RTA_DST */
2372 ;
2373
2374 if (!unresolved)
2375 len = len
2376 + nla_total_size(4) /* RTA_IIF */
2377 + nla_total_size(0) /* RTA_MULTIPATH */
2378 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2379 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002380 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002381 ;
2382
2383 return len;
2384}
2385
2386static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2387 int cmd)
2388{
2389 struct net *net = read_pnet(&mrt->net);
2390 struct sk_buff *skb;
2391 int err = -ENOBUFS;
2392
Yuval Mintz494fff52018-02-28 23:29:34 +02002393 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2394 mrt->maxvif),
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002395 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002396 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002397 goto errout;
2398
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002399 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002400 if (err < 0)
2401 goto errout;
2402
2403 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2404 return;
2405
2406errout:
2407 kfree_skb(skb);
2408 if (err < 0)
2409 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2410}
2411
Julien Gomes5a645dd2017-06-20 13:54:17 -07002412static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2413{
2414 size_t len =
2415 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2416 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2417 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2418 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2419 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2420 /* IPMRA_CREPORT_PKT */
2421 + nla_total_size(payloadlen)
2422 ;
2423
2424 return len;
2425}
2426
2427static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2428{
2429 struct net *net = read_pnet(&mrt->net);
2430 struct nlmsghdr *nlh;
2431 struct rtgenmsg *rtgenm;
2432 struct igmpmsg *msg;
2433 struct sk_buff *skb;
2434 struct nlattr *nla;
2435 int payloadlen;
2436
2437 payloadlen = pkt->len - sizeof(struct igmpmsg);
2438 msg = (struct igmpmsg *)skb_network_header(pkt);
2439
2440 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2441 if (!skb)
2442 goto errout;
2443
2444 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2445 sizeof(struct rtgenmsg), 0);
2446 if (!nlh)
2447 goto errout;
2448 rtgenm = nlmsg_data(nlh);
2449 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2450 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2451 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2452 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2453 msg->im_src.s_addr) ||
2454 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2455 msg->im_dst.s_addr))
2456 goto nla_put_failure;
2457
2458 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2459 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2460 nla_data(nla), payloadlen))
2461 goto nla_put_failure;
2462
2463 nlmsg_end(skb, nlh);
2464
2465 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2466 return;
2467
2468nla_put_failure:
2469 nlmsg_cancel(skb, nlh);
2470errout:
2471 kfree_skb(skb);
2472 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2473}
2474
Donald Sharp4f75ba62017-06-28 13:58:57 -04002475static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2476 struct netlink_ext_ack *extack)
2477{
2478 struct net *net = sock_net(in_skb->sk);
2479 struct nlattr *tb[RTA_MAX + 1];
2480 struct sk_buff *skb = NULL;
2481 struct mfc_cache *cache;
2482 struct mr_table *mrt;
2483 struct rtmsg *rtm;
2484 __be32 src, grp;
2485 u32 tableid;
2486 int err;
2487
2488 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2489 rtm_ipv4_policy, extack);
2490 if (err < 0)
2491 goto errout;
2492
2493 rtm = nlmsg_data(nlh);
2494
2495 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2496 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2497 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2498
2499 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
Dan Carpenter2e3d2322017-07-12 10:56:47 +03002500 if (!mrt) {
2501 err = -ENOENT;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002502 goto errout_free;
2503 }
2504
2505 /* entries are added/deleted only under RTNL */
2506 rcu_read_lock();
2507 cache = ipmr_cache_find(mrt, src, grp);
2508 rcu_read_unlock();
2509 if (!cache) {
2510 err = -ENOENT;
2511 goto errout_free;
2512 }
2513
2514 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2515 if (!skb) {
2516 err = -ENOBUFS;
2517 goto errout_free;
2518 }
2519
2520 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2521 nlh->nlmsg_seq, cache,
2522 RTM_NEWROUTE, 0);
2523 if (err < 0)
2524 goto errout_free;
2525
2526 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2527
2528errout:
2529 return err;
2530
2531errout_free:
2532 kfree_skb(skb);
2533 goto errout;
2534}
2535
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002536static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2537{
Yuval Mintz7b0db852018-02-28 23:29:39 +02002538 return mr_rtm_dumproute(skb, cb, ipmr_mr_table_iter,
2539 _ipmr_fill_mroute, &mfc_unres_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002540}
2541
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002542static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2543 [RTA_SRC] = { .type = NLA_U32 },
2544 [RTA_DST] = { .type = NLA_U32 },
2545 [RTA_IIF] = { .type = NLA_U32 },
2546 [RTA_TABLE] = { .type = NLA_U32 },
2547 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2548};
2549
2550static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2551{
2552 switch (rtm_protocol) {
2553 case RTPROT_STATIC:
2554 case RTPROT_MROUTED:
2555 return true;
2556 }
2557 return false;
2558}
2559
2560static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2561{
2562 struct rtnexthop *rtnh = nla_data(nla);
2563 int remaining = nla_len(nla), vifi = 0;
2564
2565 while (rtnh_ok(rtnh, remaining)) {
2566 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2567 if (++vifi == MAXVIFS)
2568 break;
2569 rtnh = rtnh_next(rtnh, &remaining);
2570 }
2571
2572 return remaining > 0 ? -EINVAL : vifi;
2573}
2574
2575/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2576static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2577 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002578 struct mr_table **mrtret,
2579 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002580{
2581 struct net_device *dev = NULL;
2582 u32 tblid = RT_TABLE_DEFAULT;
2583 struct mr_table *mrt;
2584 struct nlattr *attr;
2585 struct rtmsg *rtm;
2586 int ret, rem;
2587
Johannes Bergfceb6432017-04-12 14:34:07 +02002588 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07002589 extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002590 if (ret < 0)
2591 goto out;
2592 rtm = nlmsg_data(nlh);
2593
2594 ret = -EINVAL;
2595 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2596 rtm->rtm_type != RTN_MULTICAST ||
2597 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2598 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2599 goto out;
2600
2601 memset(mfcc, 0, sizeof(*mfcc));
2602 mfcc->mfcc_parent = -1;
2603 ret = 0;
2604 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2605 switch (nla_type(attr)) {
2606 case RTA_SRC:
2607 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2608 break;
2609 case RTA_DST:
2610 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2611 break;
2612 case RTA_IIF:
2613 dev = __dev_get_by_index(net, nla_get_u32(attr));
2614 if (!dev) {
2615 ret = -ENODEV;
2616 goto out;
2617 }
2618 break;
2619 case RTA_MULTIPATH:
2620 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2621 ret = -EINVAL;
2622 goto out;
2623 }
2624 break;
2625 case RTA_PREFSRC:
2626 ret = 1;
2627 break;
2628 case RTA_TABLE:
2629 tblid = nla_get_u32(attr);
2630 break;
2631 }
2632 }
2633 mrt = ipmr_get_table(net, tblid);
2634 if (!mrt) {
2635 ret = -ENOENT;
2636 goto out;
2637 }
2638 *mrtret = mrt;
2639 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2640 if (dev)
2641 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2642
2643out:
2644 return ret;
2645}
2646
2647/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002648static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2649 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002650{
2651 struct net *net = sock_net(skb->sk);
2652 int ret, mrtsock, parent;
2653 struct mr_table *tbl;
2654 struct mfcctl mfcc;
2655
2656 mrtsock = 0;
2657 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002658 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002659 if (ret < 0)
2660 return ret;
2661
2662 parent = ret ? mfcc.mfcc_parent : -1;
2663 if (nlh->nlmsg_type == RTM_NEWROUTE)
2664 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2665 else
2666 return ipmr_mfc_delete(tbl, &mfcc, parent);
2667}
2668
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002669static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2670{
2671 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2672
2673 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2674 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2675 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2676 mrt->mroute_reg_vif_num) ||
2677 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2678 mrt->mroute_do_assert) ||
2679 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim))
2680 return false;
2681
2682 return true;
2683}
2684
2685static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2686{
2687 struct nlattr *vif_nest;
2688 struct vif_device *vif;
2689
2690 /* if the VIF doesn't exist just continue */
2691 if (!VIF_EXISTS(mrt, vifid))
2692 return true;
2693
2694 vif = &mrt->vif_table[vifid];
2695 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2696 if (!vif_nest)
2697 return false;
2698 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2699 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2700 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2701 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2702 IPMRA_VIFA_PAD) ||
2703 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2704 IPMRA_VIFA_PAD) ||
2705 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2706 IPMRA_VIFA_PAD) ||
2707 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2708 IPMRA_VIFA_PAD) ||
2709 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2710 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2711 nla_nest_cancel(skb, vif_nest);
2712 return false;
2713 }
2714 nla_nest_end(skb, vif_nest);
2715
2716 return true;
2717}
2718
2719static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2720{
2721 struct net *net = sock_net(skb->sk);
2722 struct nlmsghdr *nlh = NULL;
2723 unsigned int t = 0, s_t;
2724 unsigned int e = 0, s_e;
2725 struct mr_table *mrt;
2726
2727 s_t = cb->args[0];
2728 s_e = cb->args[1];
2729
2730 ipmr_for_each_table(mrt, net) {
2731 struct nlattr *vifs, *af;
2732 struct ifinfomsg *hdr;
2733 u32 i;
2734
2735 if (t < s_t)
2736 goto skip_table;
2737 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2738 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2739 sizeof(*hdr), NLM_F_MULTI);
2740 if (!nlh)
2741 break;
2742
2743 hdr = nlmsg_data(nlh);
2744 memset(hdr, 0, sizeof(*hdr));
2745 hdr->ifi_family = RTNL_FAMILY_IPMR;
2746
2747 af = nla_nest_start(skb, IFLA_AF_SPEC);
2748 if (!af) {
2749 nlmsg_cancel(skb, nlh);
2750 goto out;
2751 }
2752
2753 if (!ipmr_fill_table(mrt, skb)) {
2754 nlmsg_cancel(skb, nlh);
2755 goto out;
2756 }
2757
2758 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2759 if (!vifs) {
2760 nla_nest_end(skb, af);
2761 nlmsg_end(skb, nlh);
2762 goto out;
2763 }
2764 for (i = 0; i < mrt->maxvif; i++) {
2765 if (e < s_e)
2766 goto skip_entry;
2767 if (!ipmr_fill_vif(mrt, i, skb)) {
2768 nla_nest_end(skb, vifs);
2769 nla_nest_end(skb, af);
2770 nlmsg_end(skb, nlh);
2771 goto out;
2772 }
2773skip_entry:
2774 e++;
2775 }
2776 s_e = 0;
2777 e = 0;
2778 nla_nest_end(skb, vifs);
2779 nla_nest_end(skb, af);
2780 nlmsg_end(skb, nlh);
2781skip_table:
2782 t++;
2783 }
2784
2785out:
2786 cb->args[1] = e;
2787 cb->args[0] = t;
2788
2789 return skb->len;
2790}
2791
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002792#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002793/* The /proc interfaces to multicast routing :
2794 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002795 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002796
2797static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002798 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002800 struct mr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002801 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002802 struct mr_table *mrt;
2803
2804 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002805 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002806 return ERR_PTR(-ENOENT);
2807
2808 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002809
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 read_lock(&mrt_lock);
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002811 return mr_vif_seq_start(seq, pos);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002812}
2813
2814static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002815 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
2817 read_unlock(&mrt_lock);
2818}
2819
2820static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2821{
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002822 struct mr_vif_iter *iter = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002823 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002824
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002826 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2828 } else {
2829 const struct vif_device *vif = v;
Yuval Mintz6853f212018-02-28 23:29:29 +02002830 const char *name = vif->dev ?
2831 vif->dev->name : "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832
2833 seq_printf(seq,
James Hogan91e6dd82018-01-30 09:48:02 +00002834 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002835 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002836 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 vif->bytes_out, vif->pkt_out,
2838 vif->flags, vif->local, vif->remote);
2839 }
2840 return 0;
2841}
2842
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002843static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 .start = ipmr_vif_seq_start,
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002845 .next = mr_vif_seq_next,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846 .stop = ipmr_vif_seq_stop,
2847 .show = ipmr_vif_seq_show,
2848};
2849
2850static int ipmr_vif_open(struct inode *inode, struct file *file)
2851{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002852 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
Yuval Mintz3feda6b2018-02-28 23:29:37 +02002853 sizeof(struct mr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854}
2855
Arjan van de Ven9a321442007-02-12 00:55:35 -08002856static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857 .open = ipmr_vif_open,
2858 .read = seq_read,
2859 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002860 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861};
2862
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2864{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002865 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002866 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002867
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002868 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002869 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002870 return ERR_PTR(-ENOENT);
2871
Yuval Mintzc8d61962018-02-28 23:29:36 +02002872 return mr_mfc_seq_start(seq, pos, mrt, &mfc_unres_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873}
2874
2875static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2876{
2877 int n;
2878
2879 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002880 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2882 } else {
2883 const struct mfc_cache *mfc = v;
Yuval Mintzc8d61962018-02-28 23:29:36 +02002884 const struct mr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002885 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002886
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002887 seq_printf(seq, "%08X %08X %-3hd",
2888 (__force u32) mfc->mfc_mcastgrp,
2889 (__force u32) mfc->mfc_origin,
Yuval Mintz494fff52018-02-28 23:29:34 +02002890 mfc->_c.mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002891
Patrick McHardy0c122952010-04-13 05:03:22 +00002892 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002893 seq_printf(seq, " %8lu %8lu %8lu",
Yuval Mintz494fff52018-02-28 23:29:34 +02002894 mfc->_c.mfc_un.res.pkt,
2895 mfc->_c.mfc_un.res.bytes,
2896 mfc->_c.mfc_un.res.wrong_if);
2897 for (n = mfc->_c.mfc_un.res.minvif;
2898 n < mfc->_c.mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002899 if (VIF_EXISTS(mrt, n) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002900 mfc->_c.mfc_un.res.ttls[n] < 255)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002901 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002902 " %2d:%-3d",
Yuval Mintz494fff52018-02-28 23:29:34 +02002903 n, mfc->_c.mfc_un.res.ttls[n]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002905 } else {
2906 /* unresolved mfc_caches don't contain
2907 * pkt, bytes and wrong_if values
2908 */
2909 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 }
2911 seq_putc(seq, '\n');
2912 }
2913 return 0;
2914}
2915
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002916static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002917 .start = ipmr_mfc_seq_start,
Yuval Mintzc8d61962018-02-28 23:29:36 +02002918 .next = mr_mfc_seq_next,
2919 .stop = mr_mfc_seq_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920 .show = ipmr_mfc_seq_show,
2921};
2922
2923static int ipmr_mfc_open(struct inode *inode, struct file *file)
2924{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002925 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
Yuval Mintzc8d61962018-02-28 23:29:36 +02002926 sizeof(struct mr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927}
2928
Arjan van de Ven9a321442007-02-12 00:55:35 -08002929static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002930 .open = ipmr_mfc_open,
2931 .read = seq_read,
2932 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002933 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002935#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936
2937#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002938static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002940 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941};
2942#endif
2943
Yotam Gigi4d65b942017-09-27 08:23:13 +02002944static unsigned int ipmr_seq_read(struct net *net)
2945{
2946 ASSERT_RTNL();
2947
2948 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
2949}
2950
2951static int ipmr_dump(struct net *net, struct notifier_block *nb)
2952{
2953 struct mr_table *mrt;
2954 int err;
2955
2956 err = ipmr_rules_dump(net, nb);
2957 if (err)
2958 return err;
2959
2960 ipmr_for_each_table(mrt, net) {
2961 struct vif_device *v = &mrt->vif_table[0];
Yuval Mintz494fff52018-02-28 23:29:34 +02002962 struct mr_mfc *mfc;
Yotam Gigi4d65b942017-09-27 08:23:13 +02002963 int vifi;
2964
2965 /* Notifiy on table VIF entries */
2966 read_lock(&mrt_lock);
2967 for (vifi = 0; vifi < mrt->maxvif; vifi++, v++) {
2968 if (!v->dev)
2969 continue;
2970
2971 call_ipmr_vif_entry_notifier(nb, net, FIB_EVENT_VIF_ADD,
2972 v, vifi, mrt->id);
2973 }
2974 read_unlock(&mrt_lock);
2975
2976 /* Notify on table MFC entries */
2977 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
2978 call_ipmr_mfc_entry_notifier(nb, net,
Yuval Mintz494fff52018-02-28 23:29:34 +02002979 FIB_EVENT_ENTRY_ADD,
2980 (struct mfc_cache *)mfc,
Yotam Gigi4d65b942017-09-27 08:23:13 +02002981 mrt->id);
2982 }
2983
2984 return 0;
2985}
2986
2987static const struct fib_notifier_ops ipmr_notifier_ops_template = {
2988 .family = RTNL_FAMILY_IPMR,
2989 .fib_seq_read = ipmr_seq_read,
2990 .fib_dump = ipmr_dump,
2991 .owner = THIS_MODULE,
2992};
2993
Colin Ian Kingef739d82017-09-29 14:34:22 +01002994static int __net_init ipmr_notifier_init(struct net *net)
Yotam Gigi4d65b942017-09-27 08:23:13 +02002995{
2996 struct fib_notifier_ops *ops;
2997
2998 net->ipv4.ipmr_seq = 0;
2999
3000 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3001 if (IS_ERR(ops))
3002 return PTR_ERR(ops);
3003 net->ipv4.ipmr_notifier_ops = ops;
3004
3005 return 0;
3006}
3007
3008static void __net_exit ipmr_notifier_exit(struct net *net)
3009{
3010 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3011 net->ipv4.ipmr_notifier_ops = NULL;
3012}
3013
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01003014/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00003015static int __net_init ipmr_net_init(struct net *net)
3016{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003017 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00003018
Yotam Gigi4d65b942017-09-27 08:23:13 +02003019 err = ipmr_notifier_init(net);
3020 if (err)
3021 goto ipmr_notifier_fail;
3022
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003023 err = ipmr_rules_init(net);
3024 if (err < 0)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003025 goto ipmr_rules_fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003026
3027#ifdef CONFIG_PROC_FS
3028 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00003029 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003030 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00003031 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003032 goto proc_cache_fail;
3033#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00003034 return 0;
3035
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003036#ifdef CONFIG_PROC_FS
3037proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00003038 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003039proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003040 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003041#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003042ipmr_rules_fail:
3043 ipmr_notifier_exit(net);
3044ipmr_notifier_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003045 return err;
3046}
3047
3048static void __net_exit ipmr_net_exit(struct net *net)
3049{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003050#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003051 remove_proc_entry("ip_mr_cache", net->proc_net);
3052 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003053#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003054 ipmr_notifier_exit(net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003055 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00003056}
3057
3058static struct pernet_operations ipmr_net_ops = {
3059 .init = ipmr_net_init,
3060 .exit = ipmr_net_exit,
Kirill Tkhaif84c6822018-02-13 12:29:52 +03003061 .async = true,
Benjamin Therycf958ae32009-01-22 04:56:16 +00003062};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003063
Wang Chen03d2f892008-07-03 12:13:36 +08003064int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003065{
Wang Chen03d2f892008-07-03 12:13:36 +08003066 int err;
3067
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3069 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00003070 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003071 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08003072
Benjamin Therycf958ae32009-01-22 04:56:16 +00003073 err = register_pernet_subsys(&ipmr_net_ops);
3074 if (err)
3075 goto reg_pernet_fail;
3076
Wang Chen03d2f892008-07-03 12:13:36 +08003077 err = register_netdevice_notifier(&ip_mr_notifier);
3078 if (err)
3079 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07003080#ifdef CONFIG_IP_PIMSM_V2
3081 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00003082 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07003083 err = -EAGAIN;
3084 goto add_proto_fail;
3085 }
3086#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00003087 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003088 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003089 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003090 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003091 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003092 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03003093
3094 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
Florian Westphalb97bac62017-08-09 20:41:48 +02003095 NULL, ipmr_rtm_dumplink, 0);
Wang Chen03d2f892008-07-03 12:13:36 +08003096 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003097
Tom Goff403dbb92009-06-14 03:16:13 -07003098#ifdef CONFIG_IP_PIMSM_V2
3099add_proto_fail:
3100 unregister_netdevice_notifier(&ip_mr_notifier);
3101#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08003102reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003103 unregister_pernet_subsys(&ipmr_net_ops);
3104reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08003105 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08003106 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107}