blob: a819fab45d173bd2f3e6bfe84a3cde41a14f74ff [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>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080031#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/timer.h>
34#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/sock.h>
56#include <net/icmp.h>
57#include <net/udp.h>
58#include <net/raw.h>
59#include <linux/notifier.h>
60#include <linux/if_arp.h>
61#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000062#include <linux/compat.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040063#include <linux/export.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000064#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070066#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000067#include <net/fib_rules.h>
Nicolas Dichteld67b8c62012-12-04 01:13:35 +000068#include <linux/netconf.h>
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +010069#include <net/nexthop.h>
Yotam Gigi5d8b3e62017-10-03 09:58:07 +020070#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Patrick McHardyf0ad0862010-04-13 05:03:23 +000072struct ipmr_rule {
73 struct fib_rule common;
74};
75
76struct ipmr_result {
77 struct mr_table *mrt;
78};
79
Linus Torvalds1da177e2005-04-16 15:20:36 -070080/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000081 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 */
83
84static DEFINE_RWLOCK(mrt_lock);
85
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +010086/* Multicast router control variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/* Special spinlock for queue of unresolved entries */
89static DEFINE_SPINLOCK(mfc_unres_lock);
90
91/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000092 * entries is changed only in process context and protected
93 * with weak lock mrt_lock. Queue of unresolved entries is protected
94 * with strong spinlock mfc_unres_lock.
95 *
96 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 */
98
Christoph Lametere18b8902006-12-06 20:33:20 -080099static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000101static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000102static void ipmr_free_table(struct mr_table *mrt);
103
Rami Rosenc4854ec2013-07-20 15:09:28 +0300104static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -0400105 struct net_device *dev, struct sk_buff *skb,
106 struct mfc_cache *cache, int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000107static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000108 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200109static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
110 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000111static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
112 int cmd);
Julien Gomes5a645dd2017-06-20 13:54:17 -0700113static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100114static void mroute_clean_tables(struct mr_table *mrt, bool all);
Kees Cooke99e88a2017-10-16 14:43:17 -0700115static void ipmr_expire_process(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000117#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
118#define ipmr_for_each_table(mrt, net) \
119 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
120
121static struct mr_table *ipmr_get_table(struct net *net, u32 id)
122{
123 struct mr_table *mrt;
124
125 ipmr_for_each_table(mrt, net) {
126 if (mrt->id == id)
127 return mrt;
128 }
129 return NULL;
130}
131
David S. Millerda919812011-03-12 02:04:50 -0500132static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000133 struct mr_table **mrt)
134{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000135 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100136 struct ipmr_result res;
137 struct fib_lookup_arg arg = {
138 .result = &res,
139 .flags = FIB_LOOKUP_NOREF,
140 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000141
David Aherne58e4152016-10-31 15:54:00 -0700142 /* update flow if oif or iif point to device enslaved to l3mdev */
143 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
144
David S. Millerda919812011-03-12 02:04:50 -0500145 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
146 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000147 if (err < 0)
148 return err;
149 *mrt = res.mrt;
150 return 0;
151}
152
153static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
154 int flags, struct fib_lookup_arg *arg)
155{
156 struct ipmr_result *res = arg->result;
157 struct mr_table *mrt;
158
159 switch (rule->action) {
160 case FR_ACT_TO_TBL:
161 break;
162 case FR_ACT_UNREACHABLE:
163 return -ENETUNREACH;
164 case FR_ACT_PROHIBIT:
165 return -EACCES;
166 case FR_ACT_BLACKHOLE:
167 default:
168 return -EINVAL;
169 }
170
David Aherne58e4152016-10-31 15:54:00 -0700171 arg->table = fib_rule_get_table(rule, arg);
172
173 mrt = ipmr_get_table(rule->fr_net, arg->table);
Ian Morris51456b22015-04-03 09:17:26 +0100174 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000175 return -EAGAIN;
176 res->mrt = mrt;
177 return 0;
178}
179
180static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
181{
182 return 1;
183}
184
185static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
186 FRA_GENERIC_POLICY,
187};
188
189static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
190 struct fib_rule_hdr *frh, struct nlattr **tb)
191{
192 return 0;
193}
194
195static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
196 struct nlattr **tb)
197{
198 return 1;
199}
200
201static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
202 struct fib_rule_hdr *frh)
203{
204 frh->dst_len = 0;
205 frh->src_len = 0;
206 frh->tos = 0;
207 return 0;
208}
209
Andi Kleen04a6f822012-10-04 17:12:11 -0700210static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200211 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000212 .rule_size = sizeof(struct ipmr_rule),
213 .addr_size = sizeof(u32),
214 .action = ipmr_rule_action,
215 .match = ipmr_rule_match,
216 .configure = ipmr_rule_configure,
217 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000218 .fill = ipmr_rule_fill,
219 .nlgroup = RTNLGRP_IPV4_RULE,
220 .policy = ipmr_rule_policy,
221 .owner = THIS_MODULE,
222};
223
224static int __net_init ipmr_rules_init(struct net *net)
225{
226 struct fib_rules_ops *ops;
227 struct mr_table *mrt;
228 int err;
229
230 ops = fib_rules_register(&ipmr_rules_ops_template, net);
231 if (IS_ERR(ops))
232 return PTR_ERR(ops);
233
234 INIT_LIST_HEAD(&net->ipv4.mr_tables);
235
236 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100237 if (IS_ERR(mrt)) {
238 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000239 goto err1;
240 }
241
242 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
243 if (err < 0)
244 goto err2;
245
246 net->ipv4.mr_rules_ops = ops;
247 return 0;
248
249err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700250 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000251err1:
252 fib_rules_unregister(ops);
253 return err;
254}
255
256static void __net_exit ipmr_rules_exit(struct net *net)
257{
258 struct mr_table *mrt, *next;
259
WANG Conged785302015-03-31 11:01:45 -0700260 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000261 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
262 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000263 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000264 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000265 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700266 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000267}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200268
269static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
270{
271 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR);
272}
273
274static unsigned int ipmr_rules_seq_read(struct net *net)
275{
276 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
277}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200278
279bool ipmr_rule_default(const struct fib_rule *rule)
280{
281 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
282}
283EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000284#else
285#define ipmr_for_each_table(mrt, net) \
286 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
287
288static struct mr_table *ipmr_get_table(struct net *net, u32 id)
289{
290 return net->ipv4.mrt;
291}
292
David S. Millerda919812011-03-12 02:04:50 -0500293static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000294 struct mr_table **mrt)
295{
296 *mrt = net->ipv4.mrt;
297 return 0;
298}
299
300static int __net_init ipmr_rules_init(struct net *net)
301{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100302 struct mr_table *mrt;
303
304 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
305 if (IS_ERR(mrt))
306 return PTR_ERR(mrt);
307 net->ipv4.mrt = mrt;
308 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000309}
310
311static void __net_exit ipmr_rules_exit(struct net *net)
312{
WANG Conged785302015-03-31 11:01:45 -0700313 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000314 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700315 net->ipv4.mrt = NULL;
316 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000317}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200318
319static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
320{
321 return 0;
322}
323
324static unsigned int ipmr_rules_seq_read(struct net *net)
325{
326 return 0;
327}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200328
329bool ipmr_rule_default(const struct fib_rule *rule)
330{
331 return true;
332}
333EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000334#endif
335
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100336static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
337 const void *ptr)
338{
339 const struct mfc_cache_cmp_arg *cmparg = arg->key;
340 struct mfc_cache *c = (struct mfc_cache *)ptr;
341
342 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
343 cmparg->mfc_origin != c->mfc_origin;
344}
345
346static const struct rhashtable_params ipmr_rht_params = {
347 .head_offset = offsetof(struct mfc_cache, mnode),
348 .key_offset = offsetof(struct mfc_cache, cmparg),
349 .key_len = sizeof(struct mfc_cache_cmp_arg),
350 .nelem_hint = 3,
351 .locks_mul = 1,
352 .obj_cmpfn = ipmr_hash_cmp,
353 .automatic_shrinking = true,
354};
355
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000356static struct mr_table *ipmr_new_table(struct net *net, u32 id)
357{
358 struct mr_table *mrt;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000359
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100360 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
361 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
362 return ERR_PTR(-EINVAL);
363
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000364 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100365 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000366 return mrt;
367
368 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100369 if (!mrt)
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100370 return ERR_PTR(-ENOMEM);
Patrick McHardy8de53df2010-04-15 13:29:28 +0200371 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000372 mrt->id = id;
373
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100374 rhltable_init(&mrt->mfc_hash, &ipmr_rht_params);
375 INIT_LIST_HEAD(&mrt->mfc_cache_list);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000376 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
377
Kees Cooke99e88a2017-10-16 14:43:17 -0700378 timer_setup(&mrt->ipmr_expire_timer, ipmr_expire_process, 0);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000379
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000380 mrt->mroute_reg_vif_num = -1;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000381#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
382 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
383#endif
384 return mrt;
385}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000387static void ipmr_free_table(struct mr_table *mrt)
388{
389 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100390 mroute_clean_tables(mrt, true);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100391 rhltable_destroy(&mrt->mfc_hash);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000392 kfree(mrt);
393}
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
396
Wang Chend6070322008-07-14 20:55:26 -0700397static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
398{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000399 struct net *net = dev_net(dev);
400
Wang Chend6070322008-07-14 20:55:26 -0700401 dev_close(dev);
402
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000403 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700404 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800405 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700406 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700407 struct ip_tunnel_parm p;
408
409 memset(&p, 0, sizeof(p));
410 p.iph.daddr = v->vifc_rmt_addr.s_addr;
411 p.iph.saddr = v->vifc_lcl_addr.s_addr;
412 p.iph.version = 4;
413 p.iph.ihl = 5;
414 p.iph.protocol = IPPROTO_IPIP;
415 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
416 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
417
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800418 if (ops->ndo_do_ioctl) {
419 mm_segment_t oldfs = get_fs();
420
421 set_fs(KERNEL_DS);
422 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
423 set_fs(oldfs);
424 }
Wang Chend6070322008-07-14 20:55:26 -0700425 }
426}
427
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100428/* Initialize ipmr pimreg/tunnel in_device */
429static bool ipmr_init_vif_indev(const struct net_device *dev)
430{
431 struct in_device *in_dev;
432
433 ASSERT_RTNL();
434
435 in_dev = __in_dev_get_rtnl(dev);
436 if (!in_dev)
437 return false;
438 ipv4_devconf_setall(in_dev);
439 neigh_parms_data_state_setall(in_dev->arp_parms);
440 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
441
442 return true;
443}
444
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100445static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446{
447 struct net_device *dev;
448
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000449 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800452 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 int err;
454 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 memset(&p, 0, sizeof(p));
458 p.iph.daddr = v->vifc_rmt_addr.s_addr;
459 p.iph.saddr = v->vifc_lcl_addr.s_addr;
460 p.iph.version = 4;
461 p.iph.ihl = 5;
462 p.iph.protocol = IPPROTO_IPIP;
463 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800464 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800466 if (ops->ndo_do_ioctl) {
467 mm_segment_t oldfs = get_fs();
468
469 set_fs(KERNEL_DS);
470 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
471 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000472 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800473 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000474 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 dev = NULL;
476
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000477 if (err == 0 &&
478 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100480 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 if (dev_open(dev))
483 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700484 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 }
486 }
487 return dev;
488
489failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 unregister_netdevice(dev);
491 return NULL;
492}
493
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100494#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000495static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000497 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000498 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500499 struct flowi4 fl4 = {
500 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700501 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500502 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000503 };
504 int err;
505
David S. Millerda919812011-03-12 02:04:50 -0500506 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000507 if (err < 0) {
508 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000509 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000510 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700513 dev->stats.tx_bytes += skb->len;
514 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000515 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 read_unlock(&mrt_lock);
517 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000518 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519}
520
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200521static int reg_vif_get_iflink(const struct net_device *dev)
522{
523 return 0;
524}
525
Stephen Hemminger007c3832008-11-20 20:28:35 -0800526static const struct net_device_ops reg_vif_netdev_ops = {
527 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200528 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800529};
530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531static void reg_vif_setup(struct net_device *dev)
532{
533 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800534 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530536 dev->netdev_ops = &reg_vif_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400537 dev->needs_free_netdev = true;
Tom Goff403dbb92009-06-14 03:16:13 -0700538 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000541static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
543 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000544 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000546 if (mrt->id == RT_TABLE_DEFAULT)
547 sprintf(name, "pimreg");
548 else
549 sprintf(name, "pimreg%u", mrt->id);
550
Tom Gundersenc835a672014-07-14 16:37:24 +0200551 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
Ian Morris51456b22015-04-03 09:17:26 +0100553 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 return NULL;
555
Tom Goff403dbb92009-06-14 03:16:13 -0700556 dev_net_set(dev, net);
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (register_netdevice(dev)) {
559 free_netdev(dev);
560 return NULL;
561 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100563 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 if (dev_open(dev))
566 goto failure;
567
Wang Chen7dc00c82008-07-14 20:56:34 -0700568 dev_hold(dev);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 return dev;
571
572failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 unregister_netdevice(dev);
574 return NULL;
575}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100576
577/* called with rcu_read_lock() */
578static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
579 unsigned int pimlen)
580{
581 struct net_device *reg_dev = NULL;
582 struct iphdr *encap;
583
584 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100585 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100586 * a. packet is really sent to a multicast group
587 * b. packet is not a NULL-REGISTER
588 * c. packet is not truncated
589 */
590 if (!ipv4_is_multicast(encap->daddr) ||
591 encap->tot_len == 0 ||
592 ntohs(encap->tot_len) + pimlen > skb->len)
593 return 1;
594
595 read_lock(&mrt_lock);
596 if (mrt->mroute_reg_vif_num >= 0)
597 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
598 read_unlock(&mrt_lock);
599
600 if (!reg_dev)
601 return 1;
602
603 skb->mac_header = skb->network_header;
604 skb_pull(skb, (u8 *)encap - skb->data);
605 skb_reset_network_header(skb);
606 skb->protocol = htons(ETH_P_IP);
607 skb->ip_summed = CHECKSUM_NONE;
608
609 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
610
611 netif_rx(skb);
612
613 return NET_RX_SUCCESS;
614}
615#else
616static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
617{
618 return NULL;
619}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620#endif
621
Yotam Gigi4d65b942017-09-27 08:23:13 +0200622static int call_ipmr_vif_entry_notifier(struct notifier_block *nb,
623 struct net *net,
624 enum fib_event_type event_type,
625 struct vif_device *vif,
626 vifi_t vif_index, u32 tb_id)
627{
628 struct vif_entry_notifier_info info = {
629 .info = {
630 .family = RTNL_FAMILY_IPMR,
631 .net = net,
632 },
633 .dev = vif->dev,
634 .vif_index = vif_index,
635 .vif_flags = vif->flags,
636 .tb_id = tb_id,
637 };
638
639 return call_fib_notifier(nb, net, event_type, &info.info);
640}
641
Yotam Gigib3620532017-09-27 08:23:14 +0200642static int call_ipmr_vif_entry_notifiers(struct net *net,
643 enum fib_event_type event_type,
644 struct vif_device *vif,
645 vifi_t vif_index, u32 tb_id)
646{
647 struct vif_entry_notifier_info info = {
648 .info = {
649 .family = RTNL_FAMILY_IPMR,
650 .net = net,
651 },
652 .dev = vif->dev,
653 .vif_index = vif_index,
654 .vif_flags = vif->flags,
655 .tb_id = tb_id,
656 };
657
658 ASSERT_RTNL();
659 net->ipv4.ipmr_seq++;
660 return call_fib_notifiers(net, event_type, &info.info);
661}
662
Yotam Gigi4d65b942017-09-27 08:23:13 +0200663static int call_ipmr_mfc_entry_notifier(struct notifier_block *nb,
664 struct net *net,
665 enum fib_event_type event_type,
666 struct mfc_cache *mfc, u32 tb_id)
667{
668 struct mfc_entry_notifier_info info = {
669 .info = {
670 .family = RTNL_FAMILY_IPMR,
671 .net = net,
672 },
673 .mfc = mfc,
674 .tb_id = tb_id
675 };
676
677 return call_fib_notifier(nb, net, event_type, &info.info);
678}
679
Yotam Gigib3620532017-09-27 08:23:14 +0200680static int call_ipmr_mfc_entry_notifiers(struct net *net,
681 enum fib_event_type event_type,
682 struct mfc_cache *mfc, u32 tb_id)
683{
684 struct mfc_entry_notifier_info info = {
685 .info = {
686 .family = RTNL_FAMILY_IPMR,
687 .net = net,
688 },
689 .mfc = mfc,
690 .tb_id = tb_id
691 };
692
693 ASSERT_RTNL();
694 net->ipv4.ipmr_seq++;
695 return call_fib_notifiers(net, event_type, &info.info);
696}
697
Ben Hutchings2c530402012-07-10 10:55:09 +0000698/**
699 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700700 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000702static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000703 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704{
Yotam Gigib3620532017-09-27 08:23:14 +0200705 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 struct vif_device *v;
707 struct net_device *dev;
708 struct in_device *in_dev;
709
Patrick McHardy0c122952010-04-13 05:03:22 +0000710 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 return -EADDRNOTAVAIL;
712
Patrick McHardy0c122952010-04-13 05:03:22 +0000713 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Yotam Gigib3620532017-09-27 08:23:14 +0200715 if (VIF_EXISTS(mrt, vifi))
716 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
717 mrt->id);
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 write_lock_bh(&mrt_lock);
720 dev = v->dev;
721 v->dev = NULL;
722
723 if (!dev) {
724 write_unlock_bh(&mrt_lock);
725 return -EADDRNOTAVAIL;
726 }
727
Patrick McHardy0c122952010-04-13 05:03:22 +0000728 if (vifi == mrt->mroute_reg_vif_num)
729 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000731 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000733
734 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000735 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 break;
737 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000738 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740
741 write_unlock_bh(&mrt_lock);
742
743 dev_set_allmulti(dev, -1);
744
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000745 in_dev = __in_dev_get_rtnl(dev);
746 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700747 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -0700748 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000749 NETCONFA_MC_FORWARDING,
750 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 ip_rt_multicast_event(in_dev);
752 }
753
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000754 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000755 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756
757 dev_put(dev);
758 return 0;
759}
760
Eric Dumazeta8c94862010-10-01 16:15:08 +0000761static void ipmr_cache_free_rcu(struct rcu_head *head)
762{
763 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
764
765 kmem_cache_free(mrt_cachep, c);
766}
767
Yotam Gigi310ebbb2017-09-27 08:23:12 +0200768void ipmr_cache_free(struct mfc_cache *c)
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000769{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000770 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000771}
Yotam Gigi310ebbb2017-09-27 08:23:12 +0200772EXPORT_SYMBOL(ipmr_cache_free);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000773
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000775 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000777static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200779 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700781 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Patrick McHardy0c122952010-04-13 05:03:22 +0000783 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Jianjun Kongc354e122008-11-03 00:28:02 -0800785 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700786 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200787 struct nlmsghdr *nlh = skb_pull(skb,
788 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000790 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000792 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700793 e->error = -ETIMEDOUT;
794 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700795
Eric W. Biederman15e47302012-09-07 20:12:54 +0000796 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000797 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000799 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 }
801
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000802 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
804
Patrick McHardye258beb2010-04-13 05:03:19 +0000805/* Timer process for the unresolved queue. */
Kees Cooke99e88a2017-10-16 14:43:17 -0700806static void ipmr_expire_process(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Kees Cooke99e88a2017-10-16 14:43:17 -0700808 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 unsigned long now;
810 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000811 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812
813 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000814 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 return;
816 }
817
Patrick McHardy0c122952010-04-13 05:03:22 +0000818 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 goto out;
820
821 now = jiffies;
822 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Patrick McHardy0c122952010-04-13 05:03:22 +0000824 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 if (time_after(c->mfc_un.unres.expires, now)) {
826 unsigned long interval = c->mfc_un.unres.expires - now;
827 if (interval < expires)
828 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 continue;
830 }
831
Patrick McHardy862465f2010-04-13 05:03:21 +0000832 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000833 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000834 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 }
836
Patrick McHardy0c122952010-04-13 05:03:22 +0000837 if (!list_empty(&mrt->mfc_unres_queue))
838 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840out:
841 spin_unlock(&mfc_unres_lock);
842}
843
844/* Fill oifs list. It is called under write locked mrt_lock. */
Patrick McHardy0c122952010-04-13 05:03:22 +0000845static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000846 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847{
848 int vifi;
849
850 cache->mfc_un.res.minvif = MAXVIFS;
851 cache->mfc_un.res.maxvif = 0;
852 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
853
Patrick McHardy0c122952010-04-13 05:03:22 +0000854 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
855 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000856 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
858 if (cache->mfc_un.res.minvif > vifi)
859 cache->mfc_un.res.minvif = vifi;
860 if (cache->mfc_un.res.maxvif <= vifi)
861 cache->mfc_un.res.maxvif = vifi + 1;
862 }
863 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200864 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865}
866
Patrick McHardy0c122952010-04-13 05:03:22 +0000867static int vif_add(struct net *net, struct mr_table *mrt,
868 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869{
870 int vifi = vifc->vifc_vifi;
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200871 struct switchdev_attr attr = {
872 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
873 };
Patrick McHardy0c122952010-04-13 05:03:22 +0000874 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 struct net_device *dev;
876 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700877 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000880 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 return -EADDRINUSE;
882
883 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100885 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100886 return -EINVAL;
887 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 * All the packets will be sent to the daemon
889 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000890 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000892 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 if (!dev)
894 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700895 err = dev_set_allmulti(dev, 1);
896 if (err) {
897 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700898 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700899 return err;
900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900902 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000903 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 if (!dev)
905 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700906 err = dev_set_allmulti(dev, 1);
907 if (err) {
908 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700909 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700910 return err;
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000913 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000915 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
916 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100917 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000918 dev_put(dev);
919 return -EADDRNOTAVAIL;
920 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000921 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000922 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 if (!dev)
925 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700926 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700927 if (err) {
928 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700929 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700930 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 break;
932 default:
933 return -EINVAL;
934 }
935
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000936 in_dev = __in_dev_get_rtnl(dev);
937 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000938 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000940 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700941 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700942 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
943 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 ip_rt_multicast_event(in_dev);
945
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000946 /* Fill in the VIF structures */
947
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200948 attr.orig_dev = dev;
949 if (!switchdev_port_attr_get(dev, &attr)) {
950 memcpy(v->dev_parent_id.id, attr.u.ppid.id, attr.u.ppid.id_len);
951 v->dev_parent_id.id_len = attr.u.ppid.id_len;
952 } else {
953 v->dev_parent_id.id_len = 0;
954 }
Jianjun Kongc354e122008-11-03 00:28:02 -0800955 v->rate_limit = vifc->vifc_rate_limit;
956 v->local = vifc->vifc_lcl_addr.s_addr;
957 v->remote = vifc->vifc_rmt_addr.s_addr;
958 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 if (!mrtsock)
960 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800961 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 v->bytes_in = 0;
963 v->bytes_out = 0;
964 v->pkt_in = 0;
965 v->pkt_out = 0;
966 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000967 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200968 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969
970 /* And finish update writing critical data */
971 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800972 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000973 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000974 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000975 if (vifi+1 > mrt->maxvif)
976 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +0200978 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return 0;
980}
981
Eric Dumazeta8c94862010-10-01 16:15:08 +0000982/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000983static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000984 __be32 origin,
985 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100987 struct mfc_cache_cmp_arg arg = {
988 .mfc_mcastgrp = mcastgrp,
989 .mfc_origin = origin
990 };
991 struct rhlist_head *tmp, *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 struct mfc_cache *c;
993
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100994 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
995 rhl_for_each_entry_rcu(c, tmp, list, mnode)
996 return c;
997
Patrick McHardy862465f2010-04-13 05:03:21 +0000998 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
1000
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001001/* Look for a (*,*,oif) entry */
1002static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
1003 int vifi)
1004{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001005 struct mfc_cache_cmp_arg arg = {
1006 .mfc_mcastgrp = htonl(INADDR_ANY),
1007 .mfc_origin = htonl(INADDR_ANY)
1008 };
1009 struct rhlist_head *tmp, *list;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001010 struct mfc_cache *c;
1011
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001012 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
1013 rhl_for_each_entry_rcu(c, tmp, list, mnode)
1014 if (c->mfc_un.res.ttls[vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001015 return c;
1016
1017 return NULL;
1018}
1019
1020/* Look for a (*,G) entry */
1021static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
1022 __be32 mcastgrp, int vifi)
1023{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001024 struct mfc_cache_cmp_arg arg = {
1025 .mfc_mcastgrp = mcastgrp,
1026 .mfc_origin = htonl(INADDR_ANY)
1027 };
1028 struct rhlist_head *tmp, *list;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001029 struct mfc_cache *c, *proxy;
1030
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001031 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001032 goto skip;
1033
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001034 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
1035 rhl_for_each_entry_rcu(c, tmp, list, mnode) {
1036 if (c->mfc_un.res.ttls[vifi] < 255)
1037 return c;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001038
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001039 /* It's ok if the vifi is part of the static tree */
1040 proxy = ipmr_cache_find_any_parent(mrt, c->mfc_parent);
1041 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
1042 return c;
1043 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001044
1045skip:
1046 return ipmr_cache_find_any_parent(mrt, vifi);
1047}
1048
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001049/* Look for a (S,G,iif) entry if parent != -1 */
1050static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
1051 __be32 origin, __be32 mcastgrp,
1052 int parent)
1053{
1054 struct mfc_cache_cmp_arg arg = {
1055 .mfc_mcastgrp = mcastgrp,
1056 .mfc_origin = origin,
1057 };
1058 struct rhlist_head *tmp, *list;
1059 struct mfc_cache *c;
1060
1061 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
1062 rhl_for_each_entry_rcu(c, tmp, list, mnode)
1063 if (parent == -1 || parent == c->mfc_parent)
1064 return c;
1065
1066 return NULL;
1067}
1068
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001069/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001070static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071{
Jianjun Kongc354e122008-11-03 00:28:02 -08001072 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001073
Tom Goff70a0dec2016-06-23 16:11:57 -04001074 if (c) {
1075 c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001076 c->mfc_un.res.minvif = MAXVIFS;
Yotam Gigi310ebbb2017-09-27 08:23:12 +02001077 refcount_set(&c->mfc_un.res.refcount, 1);
Tom Goff70a0dec2016-06-23 16:11:57 -04001078 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 return c;
1080}
1081
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001082static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
Jianjun Kongc354e122008-11-03 00:28:02 -08001084 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001085
1086 if (c) {
1087 skb_queue_head_init(&c->mfc_un.unres.unresolved);
1088 c->mfc_un.unres.expires = jiffies + 10*HZ;
1089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return c;
1091}
1092
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001093/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +00001094static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1095 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096{
1097 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001098 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001100 /* Play the pending entries through our router */
Jianjun Kongc354e122008-11-03 00:28:02 -08001101 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001102 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +02001103 struct nlmsghdr *nlh = skb_pull(skb,
1104 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
Hong zhi guo573ce262013-03-27 06:47:04 +00001106 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001107 nlh->nlmsg_len = skb_tail_pointer(skb) -
1108 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109 } else {
1110 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +00001111 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +00001113 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001114 e->error = -EMSGSIZE;
1115 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
Thomas Graf2942e902006-08-15 00:30:25 -07001117
Eric W. Biederman15e47302012-09-07 20:12:54 +00001118 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001119 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001120 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001121 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123}
1124
Julien Gomes5a645dd2017-06-20 13:54:17 -07001125/* Bounce a cache query up to mrouted and netlink.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001127 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001129static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001130 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001132 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001133 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 struct igmphdr *igmp;
1135 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001136 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 int ret;
1138
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 if (assert == IGMPMSG_WHOLEPKT)
1140 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1141 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 skb = alloc_skb(128, GFP_ATOMIC);
1143
Stephen Hemminger132adf52007-03-08 20:44:43 -08001144 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return -ENOBUFS;
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (assert == IGMPMSG_WHOLEPKT) {
1148 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001149 * Duplicate old header, fix ihl, length etc.
1150 * And all this only to mangle msg->im_msgtype and
1151 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001153 skb_push(skb, sizeof(struct iphdr));
1154 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001155 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001156 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001157 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1159 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001160 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001161 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1162 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1163 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001164 } else {
1165 /* Copy the IP header */
1166 skb_set_network_header(skb, skb->len);
1167 skb_put(skb, ihl);
1168 skb_copy_to_linear_data(skb, pkt->data, ihl);
1169 /* Flag to the kernel this is a route add */
1170 ip_hdr(skb)->protocol = 0;
1171 msg = (struct igmpmsg *)skb_network_header(skb);
1172 msg->im_vif = vifi;
1173 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1174 /* Add our header */
Johannes Berg4df864c2017-06-16 14:29:21 +02001175 igmp = skb_put(skb, sizeof(struct igmphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001176 igmp->type = assert;
1177 msg->im_msgtype = assert;
1178 igmp->code = 0;
1179 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1180 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001181 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182
Eric Dumazet4c9687092010-10-01 16:15:01 +00001183 rcu_read_lock();
1184 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001185 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001186 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 kfree_skb(skb);
1188 return -EINVAL;
1189 }
1190
Julien Gomes5a645dd2017-06-20 13:54:17 -07001191 igmpmsg_netlink_event(mrt, skb);
1192
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001193 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001194 ret = sock_queue_rcv_skb(mroute_sk, skb);
1195 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001196 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001197 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 kfree_skb(skb);
1199 }
1200
1201 return ret;
1202}
1203
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001204/* Queue a packet for resolution. It gets locked cache entry! */
1205static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001206 struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001208 const struct iphdr *iph = ip_hdr(skb);
1209 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001210 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001214 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001215 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001216 c->mfc_origin == iph->saddr) {
1217 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221
Patrick McHardy862465f2010-04-13 05:03:21 +00001222 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001223 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001224 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001225 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226 spin_unlock_bh(&mfc_unres_lock);
1227
1228 kfree_skb(skb);
1229 return -ENOBUFS;
1230 }
1231
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001232 /* Fill in the new cache entry */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001233 c->mfc_parent = -1;
1234 c->mfc_origin = iph->saddr;
1235 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001237 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001238 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001239 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001240 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241 out - Brad Parker
1242 */
1243 spin_unlock_bh(&mfc_unres_lock);
1244
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001245 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 kfree_skb(skb);
1247 return err;
1248 }
1249
Patrick McHardy0c122952010-04-13 05:03:22 +00001250 atomic_inc(&mrt->cache_resolve_queue_len);
1251 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001252 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
David S. Miller278554b2010-05-12 00:05:35 -07001254 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1255 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 }
1257
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001258 /* See if we can append the packet */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001259 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 kfree_skb(skb);
1261 err = -ENOBUFS;
1262 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001263 if (dev) {
1264 skb->dev = dev;
1265 skb->skb_iif = dev->ifindex;
1266 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001267 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 err = 0;
1269 }
1270
1271 spin_unlock_bh(&mfc_unres_lock);
1272 return err;
1273}
1274
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001275/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001277static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Yotam Gigib3620532017-09-27 08:23:14 +02001279 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001280 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001282 /* The entries are added/deleted only under RTNL */
1283 rcu_read_lock();
1284 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1285 mfc->mfcc_mcastgrp.s_addr, parent);
1286 rcu_read_unlock();
1287 if (!c)
1288 return -ENOENT;
1289 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1290 list_del_rcu(&c->list);
Yotam Gigib3620532017-09-27 08:23:14 +02001291 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001292 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yotam Gigi310ebbb2017-09-27 08:23:12 +02001293 ipmr_cache_put(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296}
1297
Patrick McHardy0c122952010-04-13 05:03:22 +00001298static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001299 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300{
Patrick McHardy862465f2010-04-13 05:03:21 +00001301 struct mfc_cache *uc, *c;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001302 bool found;
1303 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Patrick McHardya50436f22010-03-17 06:04:14 +00001305 if (mfc->mfcc_parent >= MAXVIFS)
1306 return -ENFILE;
1307
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001308 /* The entries are added/deleted only under RTNL */
1309 rcu_read_lock();
1310 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1311 mfc->mfcc_mcastgrp.s_addr, parent);
1312 rcu_read_unlock();
1313 if (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 write_lock_bh(&mrt_lock);
1315 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001316 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 if (!mrtsock)
1318 c->mfc_flags |= MFC_STATIC;
1319 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +02001320 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1321 mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001322 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return 0;
1324 }
1325
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001326 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001327 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328 return -EINVAL;
1329
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001330 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001331 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 return -ENOMEM;
1333
Jianjun Kongc354e122008-11-03 00:28:02 -08001334 c->mfc_origin = mfc->mfcc_origin.s_addr;
1335 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1336 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001337 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (!mrtsock)
1339 c->mfc_flags |= MFC_STATIC;
1340
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001341 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->mnode,
1342 ipmr_rht_params);
1343 if (ret) {
1344 pr_err("ipmr: rhtable insert error %d\n", ret);
1345 ipmr_cache_free(c);
1346 return ret;
1347 }
1348 list_add_tail_rcu(&c->list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001349 /* Check to see if we resolved a queued list. If so we
1350 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001352 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001354 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001355 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001357 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001358 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001359 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 break;
1361 }
1362 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001363 if (list_empty(&mrt->mfc_unres_queue))
1364 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 spin_unlock_bh(&mfc_unres_lock);
1366
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001367 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001368 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001369 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 }
Yotam Gigib3620532017-09-27 08:23:14 +02001371 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001372 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 return 0;
1374}
1375
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001376/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001377static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
Yotam Gigib3620532017-09-27 08:23:14 +02001379 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001380 struct mfc_cache *c, *tmp;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001381 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001382 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001383
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001384 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001385 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001386 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1387 continue;
1388 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001390 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001392 /* Wipe the cache */
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001393 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1394 if (!all && (c->mfc_flags & MFC_STATIC))
1395 continue;
1396 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1397 list_del_rcu(&c->list);
Yotam Gigib3620532017-09-27 08:23:14 +02001398 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c,
1399 mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001400 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yotam Gigi310ebbb2017-09-27 08:23:12 +02001401 ipmr_cache_put(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 }
1403
Patrick McHardy0c122952010-04-13 05:03:22 +00001404 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 spin_lock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001406 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001407 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001408 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001409 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001410 }
1411 spin_unlock_bh(&mfc_unres_lock);
1412 }
1413}
1414
Eric Dumazet4c9687092010-10-01 16:15:01 +00001415/* called from ip_ra_control(), before an RCU grace period,
1416 * we dont need to call synchronize_rcu() here
1417 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418static void mrtsock_destruct(struct sock *sk)
1419{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001420 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001421 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001422
WANG Cong1215e512017-04-12 12:32:13 -07001423 ASSERT_RTNL();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001424 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001425 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001426 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001427 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1428 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001429 NETCONFA_IFINDEX_ALL,
1430 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001431 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001432 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435}
1436
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001437/* Socket options and virtual interface manipulation. The whole
1438 * virtual interface system is a complete heap, but unfortunately
1439 * that's how BSD mrouted happens to think. Maybe one day with a proper
1440 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001442
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001443int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1444 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001446 struct net *net = sock_net(sk);
1447 int val, ret = 0, parent = 0;
1448 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 struct vifctl vif;
1450 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001451 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001452
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001453 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1454 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001455 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001456 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1457 ret = -EOPNOTSUPP;
1458 goto out_unlock;
1459 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001460
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001461 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001462 if (!mrt) {
1463 ret = -ENOENT;
1464 goto out_unlock;
1465 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001466 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001467 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001468 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1469 ret = -EACCES;
1470 goto out_unlock;
1471 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 }
1473
Stephen Hemminger132adf52007-03-08 20:44:43 -08001474 switch (optname) {
1475 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001476 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001477 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001478 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001479 }
1480 if (rtnl_dereference(mrt->mroute_sk)) {
1481 ret = -EADDRINUSE;
1482 break;
1483 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001484
1485 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1486 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001487 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001488 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001489 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1490 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001491 NETCONFA_IFINDEX_ALL,
1492 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001493 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001494 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001495 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001496 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1497 ret = -EACCES;
1498 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001499 ret = ip_ra_control(sk, 0, NULL);
WANG Cong1215e512017-04-12 12:32:13 -07001500 goto out_unlock;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001501 }
1502 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001503 case MRT_ADD_VIF:
1504 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001505 if (optlen != sizeof(vif)) {
1506 ret = -EINVAL;
1507 break;
1508 }
1509 if (copy_from_user(&vif, optval, sizeof(vif))) {
1510 ret = -EFAULT;
1511 break;
1512 }
1513 if (vif.vifc_vifi >= MAXVIFS) {
1514 ret = -ENFILE;
1515 break;
1516 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001517 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001518 ret = vif_add(net, mrt, &vif,
1519 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001520 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001521 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001522 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001523 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001524 /* Manipulate the forwarding caches. These live
1525 * in a sort of kernel/user symbiosis.
1526 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001527 case MRT_ADD_MFC:
1528 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001529 parent = -1;
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05001530 /* fall through */
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001531 case MRT_ADD_MFC_PROXY:
1532 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001533 if (optlen != sizeof(mfc)) {
1534 ret = -EINVAL;
1535 break;
1536 }
1537 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1538 ret = -EFAULT;
1539 break;
1540 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001541 if (parent == 0)
1542 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001543 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1544 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001545 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001546 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001547 sk == rtnl_dereference(mrt->mroute_sk),
1548 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001549 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001550 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001551 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001552 if (optlen != sizeof(val)) {
1553 ret = -EINVAL;
1554 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001555 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001556 if (get_user(val, (int __user *)optval)) {
1557 ret = -EFAULT;
1558 break;
1559 }
1560 mrt->mroute_do_assert = val;
1561 break;
1562 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001563 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001564 ret = -ENOPROTOOPT;
1565 break;
1566 }
1567 if (optlen != sizeof(val)) {
1568 ret = -EINVAL;
1569 break;
1570 }
1571 if (get_user(val, (int __user *)optval)) {
1572 ret = -EFAULT;
1573 break;
1574 }
1575
1576 val = !!val;
1577 if (val != mrt->mroute_do_pim) {
1578 mrt->mroute_do_pim = val;
1579 mrt->mroute_do_assert = val;
1580 }
1581 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001582 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001583 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1584 ret = -ENOPROTOOPT;
1585 break;
1586 }
1587 if (optlen != sizeof(uval)) {
1588 ret = -EINVAL;
1589 break;
1590 }
1591 if (get_user(uval, (u32 __user *)optval)) {
1592 ret = -EFAULT;
1593 break;
1594 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001595
Eric Dumazet4c9687092010-10-01 16:15:01 +00001596 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1597 ret = -EBUSY;
1598 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001599 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001600 if (IS_ERR(mrt))
1601 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001602 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001603 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001604 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001605 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001606 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001607 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001608 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001610out_unlock:
1611 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001612 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613}
1614
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001615/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001616int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617{
1618 int olr;
1619 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001620 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001621 struct mr_table *mrt;
1622
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001623 if (sk->sk_type != SOCK_RAW ||
1624 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1625 return -EOPNOTSUPP;
1626
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001627 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001628 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001629 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001631 switch (optname) {
1632 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001633 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001634 break;
1635 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001636 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001637 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001638 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001639 break;
1640 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001641 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001642 break;
1643 default:
1644 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001645 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001646
1647 if (get_user(olr, optlen))
1648 return -EFAULT;
1649 olr = min_t(unsigned int, olr, sizeof(int));
1650 if (olr < 0)
1651 return -EINVAL;
1652 if (put_user(olr, optlen))
1653 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001654 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 return -EFAULT;
1656 return 0;
1657}
1658
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001659/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1661{
1662 struct sioc_sg_req sr;
1663 struct sioc_vif_req vr;
1664 struct vif_device *vif;
1665 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001666 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001667 struct mr_table *mrt;
1668
1669 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001670 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001671 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001672
Stephen Hemminger132adf52007-03-08 20:44:43 -08001673 switch (cmd) {
1674 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001675 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001676 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001677 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001678 return -EINVAL;
1679 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001680 vif = &mrt->vif_table[vr.vifi];
1681 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001682 vr.icount = vif->pkt_in;
1683 vr.ocount = vif->pkt_out;
1684 vr.ibytes = vif->bytes_in;
1685 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001687
Jianjun Kongc354e122008-11-03 00:28:02 -08001688 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001690 return 0;
1691 }
1692 read_unlock(&mrt_lock);
1693 return -EADDRNOTAVAIL;
1694 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001695 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001696 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Eric Dumazeta8c94862010-10-01 16:15:08 +00001698 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001699 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001700 if (c) {
1701 sr.pktcnt = c->mfc_un.res.pkt;
1702 sr.bytecnt = c->mfc_un.res.bytes;
1703 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001704 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001705
Jianjun Kongc354e122008-11-03 00:28:02 -08001706 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001707 return -EFAULT;
1708 return 0;
1709 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001710 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001711 return -EADDRNOTAVAIL;
1712 default:
1713 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 }
1715}
1716
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001717#ifdef CONFIG_COMPAT
1718struct compat_sioc_sg_req {
1719 struct in_addr src;
1720 struct in_addr grp;
1721 compat_ulong_t pktcnt;
1722 compat_ulong_t bytecnt;
1723 compat_ulong_t wrong_if;
1724};
1725
David S. Millerca6b8bb02011-02-03 17:24:28 -08001726struct compat_sioc_vif_req {
1727 vifi_t vifi; /* Which iface */
1728 compat_ulong_t icount;
1729 compat_ulong_t ocount;
1730 compat_ulong_t ibytes;
1731 compat_ulong_t obytes;
1732};
1733
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001734int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1735{
David S. Miller0033d5a2011-02-03 17:21:31 -08001736 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001737 struct compat_sioc_vif_req vr;
1738 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001739 struct mfc_cache *c;
1740 struct net *net = sock_net(sk);
1741 struct mr_table *mrt;
1742
1743 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001744 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001745 return -ENOENT;
1746
1747 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001748 case SIOCGETVIFCNT:
1749 if (copy_from_user(&vr, arg, sizeof(vr)))
1750 return -EFAULT;
1751 if (vr.vifi >= mrt->maxvif)
1752 return -EINVAL;
1753 read_lock(&mrt_lock);
1754 vif = &mrt->vif_table[vr.vifi];
1755 if (VIF_EXISTS(mrt, vr.vifi)) {
1756 vr.icount = vif->pkt_in;
1757 vr.ocount = vif->pkt_out;
1758 vr.ibytes = vif->bytes_in;
1759 vr.obytes = vif->bytes_out;
1760 read_unlock(&mrt_lock);
1761
1762 if (copy_to_user(arg, &vr, sizeof(vr)))
1763 return -EFAULT;
1764 return 0;
1765 }
1766 read_unlock(&mrt_lock);
1767 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001768 case SIOCGETSGCNT:
1769 if (copy_from_user(&sr, arg, sizeof(sr)))
1770 return -EFAULT;
1771
1772 rcu_read_lock();
1773 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1774 if (c) {
1775 sr.pktcnt = c->mfc_un.res.pkt;
1776 sr.bytecnt = c->mfc_un.res.bytes;
1777 sr.wrong_if = c->mfc_un.res.wrong_if;
1778 rcu_read_unlock();
1779
1780 if (copy_to_user(arg, &sr, sizeof(sr)))
1781 return -EFAULT;
1782 return 0;
1783 }
1784 rcu_read_unlock();
1785 return -EADDRNOTAVAIL;
1786 default:
1787 return -ENOIOCTLCMD;
1788 }
1789}
1790#endif
1791
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1793{
Jiri Pirko351638e2013-05-28 01:30:21 +00001794 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001795 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001796 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 struct vif_device *v;
1798 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 if (event != NETDEV_UNREGISTER)
1801 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001802
1803 ipmr_for_each_table(mrt, net) {
1804 v = &mrt->vif_table[0];
1805 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1806 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001807 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 }
1810 return NOTIFY_DONE;
1811}
1812
Jianjun Kongc354e122008-11-03 00:28:02 -08001813static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 .notifier_call = ipmr_device_event,
1815};
1816
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001817/* Encapsulate a packet by attaching a valid IPIP header to it.
1818 * This avoids tunnel drivers and other mess and gives us the speed so
1819 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001821static void ip_encap(struct net *net, struct sk_buff *skb,
1822 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001824 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001825 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001826
1827 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001828 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001829 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001830 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001832 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001833 iph->tos = old_iph->tos;
1834 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 iph->frag_off = 0;
1836 iph->daddr = daddr;
1837 iph->saddr = saddr;
1838 iph->protocol = IPPROTO_IPIP;
1839 iph->ihl = 5;
1840 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001841 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 ip_send_check(iph);
1843
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1845 nf_reset(skb);
1846}
1847
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001848static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1849 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001851 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
David S. Miller73186df2015-11-03 13:41:45 -05001853 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1854 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 if (unlikely(opt->optlen))
1857 ip_forward_options(skb);
1858
Eric W. Biederman13206b62015-10-07 16:48:35 -05001859 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860}
1861
Yotam Gigia5bc9292017-10-03 09:58:08 +02001862#ifdef CONFIG_NET_SWITCHDEV
1863static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1864 int in_vifi, int out_vifi)
1865{
1866 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1867 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1868
1869 if (!skb->offload_mr_fwd_mark)
1870 return false;
1871 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1872 return false;
1873 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1874 &in_vif->dev_parent_id);
1875}
1876#else
1877static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1878 int in_vifi, int out_vifi)
1879{
1880 return false;
1881}
1882#endif
1883
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001884/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885
Patrick McHardy0c122952010-04-13 05:03:22 +00001886static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
Yotam Gigia5bc9292017-10-03 09:58:08 +02001887 int in_vifi, struct sk_buff *skb,
1888 struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001890 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001891 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 struct net_device *dev;
1893 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001894 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 int encap = 0;
1896
Ian Morris51456b22015-04-03 09:17:26 +01001897 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 goto out_free;
1899
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 if (vif->flags & VIFF_REGISTER) {
1901 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001902 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001903 vif->dev->stats.tx_bytes += skb->len;
1904 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001905 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001906 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Yotam Gigia5bc9292017-10-03 09:58:08 +02001909 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1910 goto out_free;
1911
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001912 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001913 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001914 vif->remote, vif->local,
1915 0, 0,
1916 IPPROTO_IPIP,
1917 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001918 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 goto out_free;
1920 encap = sizeof(struct iphdr);
1921 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001922 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001923 0, 0,
1924 IPPROTO_IPIP,
1925 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001926 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 goto out_free;
1928 }
1929
Changli Gaod8d1f302010-06-10 23:31:35 -07001930 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Changli Gaod8d1f302010-06-10 23:31:35 -07001932 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001934 * allow to send ICMP, so that packets will disappear
1935 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 */
David S. Miller73186df2015-11-03 13:41:45 -05001937 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 ip_rt_put(rt);
1939 goto out_free;
1940 }
1941
Changli Gaod8d1f302010-06-10 23:31:35 -07001942 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001945 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 goto out_free;
1947 }
1948
1949 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001950 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Eric Dumazetadf30902009-06-02 05:19:30 +00001952 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001953 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001954 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955
1956 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001957 * What do we do with netfilter? -- RR
1958 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001960 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001962 vif->dev->stats.tx_packets++;
1963 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 }
1965
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001966 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001968 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969 * not only before forwarding, but after forwarding on all output
1970 * interfaces. It is clear, if mrouter runs a multicasting
1971 * program, it should receive packets not depending to what interface
1972 * program is joined.
1973 * If we will not make it, the program will have to join on all
1974 * interfaces. On the other hand, multihoming host (or router, but
1975 * not mrouter) cannot join to more than one interface - it will
1976 * result in receiving multiple packets.
1977 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001978 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1979 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 ipmr_forward_finish);
1981 return;
1982
1983out_free:
1984 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
Patrick McHardy0c122952010-04-13 05:03:22 +00001987static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001990
1991 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1992 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 break;
1994 }
1995 return ct;
1996}
1997
1998/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001999static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002000 struct net_device *dev, struct sk_buff *skb,
2001 struct mfc_cache *cache, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002{
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002003 int true_vifi = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 int psend = -1;
2005 int vif, ct;
2006
2007 vif = cache->mfc_parent;
2008 cache->mfc_un.res.pkt++;
2009 cache->mfc_un.res.bytes += skb->len;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002010 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002012 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002013 struct mfc_cache *cache_proxy;
2014
2015 /* For an (*,G) entry, we only check that the incomming
2016 * interface is part of the static tree.
2017 */
2018 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
2019 if (cache_proxy &&
2020 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
2021 goto forward;
2022 }
2023
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002024 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002025 if (mrt->vif_table[vif].dev != dev) {
David S. Millerc7537962010-11-11 17:07:48 -08002026 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002028 * Very complicated situation...
2029 *
2030 * The best workaround until routing daemons will be
2031 * fixed is not to redistribute packet, if it was
2032 * send through wrong interface. It means, that
2033 * multicast applications WILL NOT work for
2034 * (S,G), which have default multicast route pointing
2035 * to wrong oif. In any case, it is not a good
2036 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 */
2038 goto dont_forward;
2039 }
2040
2041 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
Patrick McHardy0c122952010-04-13 05:03:22 +00002043 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002045 * so that we cannot check that packet arrived on an oif.
2046 * It is bad, but otherwise we would need to move pretty
2047 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 */
Patrick McHardy0c122952010-04-13 05:03:22 +00002049 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00002050 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002051 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
2053 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00002054 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 }
2056 goto dont_forward;
2057 }
2058
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002059forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00002060 mrt->vif_table[vif].pkt_in++;
2061 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002063 /* Forward the frame */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002064 if (cache->mfc_origin == htonl(INADDR_ANY) &&
2065 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002066 if (true_vifi >= 0 &&
2067 true_vifi != cache->mfc_parent &&
2068 ip_hdr(skb)->ttl >
2069 cache->mfc_un.res.ttls[cache->mfc_parent]) {
2070 /* It's an (*,*) entry and the packet is not coming from
2071 * the upstream: forward the packet to the upstream
2072 * only.
2073 */
2074 psend = cache->mfc_parent;
2075 goto last_forward;
2076 }
2077 goto dont_forward;
2078 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002079 for (ct = cache->mfc_un.res.maxvif - 1;
2080 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002081 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002082 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
2083 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002084 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 if (psend != -1) {
2086 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002089 ipmr_queue_xmit(net, mrt, true_vifi,
2090 skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002092 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 }
2094 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002095last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (psend != -1) {
2097 if (local) {
2098 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002099
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002101 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
2102 cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103 } else {
Yotam Gigia5bc9292017-10-03 09:58:08 +02002104 ipmr_queue_xmit(net, mrt, true_vifi, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03002105 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 }
2107 }
2108
2109dont_forward:
2110 if (!local)
2111 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112}
2113
David S. Miller417da662011-05-03 19:42:43 -07002114static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08002115{
David S. Miller417da662011-05-03 19:42:43 -07002116 struct rtable *rt = skb_rtable(skb);
2117 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05002118 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07002119 .daddr = iph->daddr,
2120 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00002121 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07002122 .flowi4_oif = (rt_is_output_route(rt) ?
2123 skb->dev->ifindex : 0),
2124 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002125 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07002126 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00002127 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08002128 };
2129 struct mr_table *mrt;
2130 int err;
2131
David S. Millerda919812011-03-12 02:04:50 -05002132 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002133 if (err)
2134 return ERR_PTR(err);
2135 return mrt;
2136}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002138/* Multicast packets for forwarding arrive here
2139 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141int ip_mr_input(struct sk_buff *skb)
2142{
2143 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002144 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00002145 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002146 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002147 struct net_device *dev;
2148
2149 /* skb->dev passed in is the loX master dev for vrfs.
2150 * As there are no vifs associated with loopback devices,
2151 * get the proper interface that does have a vif associated with it.
2152 */
2153 dev = skb->dev;
2154 if (netif_is_l3_master(skb->dev)) {
2155 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2156 if (!dev) {
2157 kfree_skb(skb);
2158 return -ENODEV;
2159 }
2160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
2162 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002163 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00002165 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 goto dont_forward;
2167
David S. Miller417da662011-05-03 19:42:43 -07002168 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002169 if (IS_ERR(mrt)) {
2170 kfree_skb(skb);
2171 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00002174 if (IPCB(skb)->opt.router_alert) {
2175 if (ip_call_ra_chain(skb))
2176 return 0;
2177 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2178 /* IGMPv1 (and broken IGMPv2 implementations sort of
2179 * Cisco IOS <= 11.2(8)) do not put router alert
2180 * option to IGMP packets destined to routable
2181 * groups. It is very bad, because it means
2182 * that we can forward NO IGMP messages.
2183 */
2184 struct sock *mroute_sk;
2185
2186 mroute_sk = rcu_dereference(mrt->mroute_sk);
2187 if (mroute_sk) {
2188 nf_reset(skb);
2189 raw_rcv(mroute_sk, skb);
2190 return 0;
2191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002192 }
2193 }
2194
Eric Dumazeta8c94862010-10-01 16:15:08 +00002195 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002196 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002197 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002198 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002199
2200 if (vif >= 0)
2201 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2202 vif);
2203 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002205 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002206 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 int vif;
2208
2209 if (local) {
2210 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2211 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002212 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 skb = skb2;
2215 }
2216
Eric Dumazeta8c94862010-10-01 16:15:08 +00002217 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002218 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 if (vif >= 0) {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002220 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221 read_unlock(&mrt_lock);
2222
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002223 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 }
2225 read_unlock(&mrt_lock);
2226 kfree_skb(skb);
2227 return -ENODEV;
2228 }
2229
Eric Dumazeta8c94862010-10-01 16:15:08 +00002230 read_lock(&mrt_lock);
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002231 ip_mr_forward(net, mrt, dev, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 read_unlock(&mrt_lock);
2233
2234 if (local)
2235 return ip_local_deliver(skb);
2236
2237 return 0;
2238
2239dont_forward:
2240 if (local)
2241 return ip_local_deliver(skb);
2242 kfree_skb(skb);
2243 return 0;
2244}
2245
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002246#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002247/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002248int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002249{
2250 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002251 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002252 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002253
2254 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2255 goto drop;
2256
2257 pim = igmp_hdr(skb);
2258
David S. Miller417da662011-05-03 19:42:43 -07002259 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002260 if (IS_ERR(mrt))
2261 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002262 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002263 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2264 goto drop;
2265
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002266 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002267drop:
2268 kfree_skb(skb);
2269 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 return 0;
2271}
2272#endif
2273
2274#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002275static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276{
2277 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002278 struct net *net = dev_net(skb->dev);
2279 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002281 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 goto drop;
2283
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002284 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002285 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002286 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002287 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002288 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002289 goto drop;
2290
David S. Miller417da662011-05-03 19:42:43 -07002291 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002292 if (IS_ERR(mrt))
2293 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002294 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002295drop:
2296 kfree_skb(skb);
2297 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 return 0;
2299}
2300#endif
2301
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002302static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2303 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304{
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002305 struct rta_mfc_stats mfcs;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002306 struct nlattr *mp_attr;
2307 struct rtnexthop *nhp;
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002308 unsigned long lastuse;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002309 int ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Nicolas Dichtel74381892010-03-25 23:45:35 +00002311 /* If cache is unresolved, don't try to parse IIF and OIF */
Nikolay Aleksandrov1708ebc2017-01-03 12:13:39 +01002312 if (c->mfc_parent >= MAXVIFS) {
2313 rtm->rtm_flags |= RTNH_F_UNRESOLVED;
Nicolas Dichtel74381892010-03-25 23:45:35 +00002314 return -ENOENT;
Nikolay Aleksandrov1708ebc2017-01-03 12:13:39 +01002315 }
Nicolas Dichtel74381892010-03-25 23:45:35 +00002316
Thomas Graf92a395e2012-06-26 23:36:13 +00002317 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2318 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2319 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Yotam Gigic7c0bbe2017-09-27 08:23:15 +02002321 if (c->mfc_flags & MFC_OFFLOAD)
2322 rtm->rtm_flags |= RTNH_F_OFFLOAD;
2323
Thomas Graf92a395e2012-06-26 23:36:13 +00002324 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2325 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
2327 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002328 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002329 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2330 nla_nest_cancel(skb, mp_attr);
2331 return -EMSGSIZE;
2332 }
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 nhp->rtnh_flags = 0;
2335 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002336 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 nhp->rtnh_len = sizeof(*nhp);
2338 }
2339 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002340
2341 nla_nest_end(skb, mp_attr);
2342
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002343 lastuse = READ_ONCE(c->mfc_un.res.lastuse);
2344 lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
2345
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002346 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2347 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2348 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002349 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002350 nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002351 RTA_PAD))
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002352 return -EMSGSIZE;
2353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 rtm->rtm_type = RTN_MULTICAST;
2355 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
David S. Miller9a1b9492011-05-04 12:18:54 -07002358int ipmr_get_route(struct net *net, struct sk_buff *skb,
2359 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002360 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002363 struct mr_table *mrt;
2364 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002366 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002367 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002368 return -ENOENT;
2369
Eric Dumazeta8c94862010-10-01 16:15:08 +00002370 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002371 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002372 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002373 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002375 if (vif >= 0)
2376 cache = ipmr_cache_find_any(mrt, daddr, vif);
2377 }
Ian Morris51456b22015-04-03 09:17:26 +01002378 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002379 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002380 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002382 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002385 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002386 if (dev)
2387 vif = ipmr_find_vif(mrt, dev);
2388 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002390 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return -ENODEV;
2392 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002393 skb2 = skb_clone(skb, GFP_ATOMIC);
2394 if (!skb2) {
2395 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002396 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002397 return -ENOMEM;
2398 }
2399
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002400 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002401 skb_push(skb2, sizeof(struct iphdr));
2402 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002403 iph = ip_hdr(skb2);
2404 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002405 iph->saddr = saddr;
2406 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002407 iph->version = 0;
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002408 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002410 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return err;
2412 }
2413
Eric Dumazeta8c94862010-10-01 16:15:08 +00002414 read_lock(&mrt_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002415 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002417 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return err;
2419}
2420
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002421static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002422 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2423 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002424{
2425 struct nlmsghdr *nlh;
2426 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002427 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002428
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002429 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002430 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002431 return -EMSGSIZE;
2432
2433 rtm = nlmsg_data(nlh);
2434 rtm->rtm_family = RTNL_FAMILY_IPMR;
2435 rtm->rtm_dst_len = 32;
2436 rtm->rtm_src_len = 32;
2437 rtm->rtm_tos = 0;
2438 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002439 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2440 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002441 rtm->rtm_type = RTN_MULTICAST;
2442 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002443 if (c->mfc_flags & MFC_STATIC)
2444 rtm->rtm_protocol = RTPROT_STATIC;
2445 else
2446 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002447 rtm->rtm_flags = 0;
2448
Jiri Benc930345e2015-03-29 16:59:25 +02002449 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2450 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002451 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002452 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2453 /* do not break the dump if cache is unresolved */
2454 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002455 goto nla_put_failure;
2456
Johannes Berg053c0952015-01-16 22:09:00 +01002457 nlmsg_end(skb, nlh);
2458 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002459
2460nla_put_failure:
2461 nlmsg_cancel(skb, nlh);
2462 return -EMSGSIZE;
2463}
2464
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002465static size_t mroute_msgsize(bool unresolved, int maxvif)
2466{
2467 size_t len =
2468 NLMSG_ALIGN(sizeof(struct rtmsg))
2469 + nla_total_size(4) /* RTA_TABLE */
2470 + nla_total_size(4) /* RTA_SRC */
2471 + nla_total_size(4) /* RTA_DST */
2472 ;
2473
2474 if (!unresolved)
2475 len = len
2476 + nla_total_size(4) /* RTA_IIF */
2477 + nla_total_size(0) /* RTA_MULTIPATH */
2478 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2479 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002480 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002481 ;
2482
2483 return len;
2484}
2485
2486static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2487 int cmd)
2488{
2489 struct net *net = read_pnet(&mrt->net);
2490 struct sk_buff *skb;
2491 int err = -ENOBUFS;
2492
2493 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2494 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002495 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002496 goto errout;
2497
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002498 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002499 if (err < 0)
2500 goto errout;
2501
2502 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2503 return;
2504
2505errout:
2506 kfree_skb(skb);
2507 if (err < 0)
2508 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2509}
2510
Julien Gomes5a645dd2017-06-20 13:54:17 -07002511static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2512{
2513 size_t len =
2514 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2515 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2516 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2517 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2518 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2519 /* IPMRA_CREPORT_PKT */
2520 + nla_total_size(payloadlen)
2521 ;
2522
2523 return len;
2524}
2525
2526static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2527{
2528 struct net *net = read_pnet(&mrt->net);
2529 struct nlmsghdr *nlh;
2530 struct rtgenmsg *rtgenm;
2531 struct igmpmsg *msg;
2532 struct sk_buff *skb;
2533 struct nlattr *nla;
2534 int payloadlen;
2535
2536 payloadlen = pkt->len - sizeof(struct igmpmsg);
2537 msg = (struct igmpmsg *)skb_network_header(pkt);
2538
2539 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2540 if (!skb)
2541 goto errout;
2542
2543 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2544 sizeof(struct rtgenmsg), 0);
2545 if (!nlh)
2546 goto errout;
2547 rtgenm = nlmsg_data(nlh);
2548 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2549 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2550 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2551 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2552 msg->im_src.s_addr) ||
2553 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2554 msg->im_dst.s_addr))
2555 goto nla_put_failure;
2556
2557 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2558 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2559 nla_data(nla), payloadlen))
2560 goto nla_put_failure;
2561
2562 nlmsg_end(skb, nlh);
2563
2564 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2565 return;
2566
2567nla_put_failure:
2568 nlmsg_cancel(skb, nlh);
2569errout:
2570 kfree_skb(skb);
2571 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2572}
2573
Donald Sharp4f75ba62017-06-28 13:58:57 -04002574static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2575 struct netlink_ext_ack *extack)
2576{
2577 struct net *net = sock_net(in_skb->sk);
2578 struct nlattr *tb[RTA_MAX + 1];
2579 struct sk_buff *skb = NULL;
2580 struct mfc_cache *cache;
2581 struct mr_table *mrt;
2582 struct rtmsg *rtm;
2583 __be32 src, grp;
2584 u32 tableid;
2585 int err;
2586
2587 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2588 rtm_ipv4_policy, extack);
2589 if (err < 0)
2590 goto errout;
2591
2592 rtm = nlmsg_data(nlh);
2593
2594 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2595 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2596 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2597
2598 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
Dan Carpenter2e3d2322017-07-12 10:56:47 +03002599 if (!mrt) {
2600 err = -ENOENT;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002601 goto errout_free;
2602 }
2603
2604 /* entries are added/deleted only under RTNL */
2605 rcu_read_lock();
2606 cache = ipmr_cache_find(mrt, src, grp);
2607 rcu_read_unlock();
2608 if (!cache) {
2609 err = -ENOENT;
2610 goto errout_free;
2611 }
2612
2613 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2614 if (!skb) {
2615 err = -ENOBUFS;
2616 goto errout_free;
2617 }
2618
2619 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2620 nlh->nlmsg_seq, cache,
2621 RTM_NEWROUTE, 0);
2622 if (err < 0)
2623 goto errout_free;
2624
2625 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2626
2627errout:
2628 return err;
2629
2630errout_free:
2631 kfree_skb(skb);
2632 goto errout;
2633}
2634
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002635static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2636{
2637 struct net *net = sock_net(skb->sk);
2638 struct mr_table *mrt;
2639 struct mfc_cache *mfc;
2640 unsigned int t = 0, s_t;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002641 unsigned int e = 0, s_e;
2642
2643 s_t = cb->args[0];
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002644 s_e = cb->args[1];
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002645
Eric Dumazeta8c94862010-10-01 16:15:08 +00002646 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002647 ipmr_for_each_table(mrt, net) {
2648 if (t < s_t)
2649 goto next_table;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002650 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) {
2651 if (e < s_e)
2652 goto next_entry;
2653 if (ipmr_fill_mroute(mrt, skb,
2654 NETLINK_CB(cb->skb).portid,
2655 cb->nlh->nlmsg_seq,
2656 mfc, RTM_NEWROUTE,
2657 NLM_F_MULTI) < 0)
2658 goto done;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002659next_entry:
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002660 e++;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002661 }
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002662 e = 0;
2663 s_e = 0;
2664
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002665 spin_lock_bh(&mfc_unres_lock);
2666 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2667 if (e < s_e)
2668 goto next_entry2;
2669 if (ipmr_fill_mroute(mrt, skb,
2670 NETLINK_CB(cb->skb).portid,
2671 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002672 mfc, RTM_NEWROUTE,
2673 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002674 spin_unlock_bh(&mfc_unres_lock);
2675 goto done;
2676 }
2677next_entry2:
2678 e++;
2679 }
2680 spin_unlock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002681 e = 0;
2682 s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002683next_table:
2684 t++;
2685 }
2686done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002687 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002688
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002689 cb->args[1] = e;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002690 cb->args[0] = t;
2691
2692 return skb->len;
2693}
2694
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002695static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2696 [RTA_SRC] = { .type = NLA_U32 },
2697 [RTA_DST] = { .type = NLA_U32 },
2698 [RTA_IIF] = { .type = NLA_U32 },
2699 [RTA_TABLE] = { .type = NLA_U32 },
2700 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2701};
2702
2703static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2704{
2705 switch (rtm_protocol) {
2706 case RTPROT_STATIC:
2707 case RTPROT_MROUTED:
2708 return true;
2709 }
2710 return false;
2711}
2712
2713static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2714{
2715 struct rtnexthop *rtnh = nla_data(nla);
2716 int remaining = nla_len(nla), vifi = 0;
2717
2718 while (rtnh_ok(rtnh, remaining)) {
2719 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2720 if (++vifi == MAXVIFS)
2721 break;
2722 rtnh = rtnh_next(rtnh, &remaining);
2723 }
2724
2725 return remaining > 0 ? -EINVAL : vifi;
2726}
2727
2728/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2729static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2730 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002731 struct mr_table **mrtret,
2732 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002733{
2734 struct net_device *dev = NULL;
2735 u32 tblid = RT_TABLE_DEFAULT;
2736 struct mr_table *mrt;
2737 struct nlattr *attr;
2738 struct rtmsg *rtm;
2739 int ret, rem;
2740
Johannes Bergfceb6432017-04-12 14:34:07 +02002741 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07002742 extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002743 if (ret < 0)
2744 goto out;
2745 rtm = nlmsg_data(nlh);
2746
2747 ret = -EINVAL;
2748 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2749 rtm->rtm_type != RTN_MULTICAST ||
2750 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2751 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2752 goto out;
2753
2754 memset(mfcc, 0, sizeof(*mfcc));
2755 mfcc->mfcc_parent = -1;
2756 ret = 0;
2757 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2758 switch (nla_type(attr)) {
2759 case RTA_SRC:
2760 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2761 break;
2762 case RTA_DST:
2763 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2764 break;
2765 case RTA_IIF:
2766 dev = __dev_get_by_index(net, nla_get_u32(attr));
2767 if (!dev) {
2768 ret = -ENODEV;
2769 goto out;
2770 }
2771 break;
2772 case RTA_MULTIPATH:
2773 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2774 ret = -EINVAL;
2775 goto out;
2776 }
2777 break;
2778 case RTA_PREFSRC:
2779 ret = 1;
2780 break;
2781 case RTA_TABLE:
2782 tblid = nla_get_u32(attr);
2783 break;
2784 }
2785 }
2786 mrt = ipmr_get_table(net, tblid);
2787 if (!mrt) {
2788 ret = -ENOENT;
2789 goto out;
2790 }
2791 *mrtret = mrt;
2792 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2793 if (dev)
2794 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2795
2796out:
2797 return ret;
2798}
2799
2800/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002801static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2802 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002803{
2804 struct net *net = sock_net(skb->sk);
2805 int ret, mrtsock, parent;
2806 struct mr_table *tbl;
2807 struct mfcctl mfcc;
2808
2809 mrtsock = 0;
2810 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002811 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002812 if (ret < 0)
2813 return ret;
2814
2815 parent = ret ? mfcc.mfcc_parent : -1;
2816 if (nlh->nlmsg_type == RTM_NEWROUTE)
2817 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2818 else
2819 return ipmr_mfc_delete(tbl, &mfcc, parent);
2820}
2821
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002822static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2823{
2824 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2825
2826 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2827 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2828 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2829 mrt->mroute_reg_vif_num) ||
2830 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2831 mrt->mroute_do_assert) ||
2832 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim))
2833 return false;
2834
2835 return true;
2836}
2837
2838static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2839{
2840 struct nlattr *vif_nest;
2841 struct vif_device *vif;
2842
2843 /* if the VIF doesn't exist just continue */
2844 if (!VIF_EXISTS(mrt, vifid))
2845 return true;
2846
2847 vif = &mrt->vif_table[vifid];
2848 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2849 if (!vif_nest)
2850 return false;
2851 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2852 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2853 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2854 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2855 IPMRA_VIFA_PAD) ||
2856 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2857 IPMRA_VIFA_PAD) ||
2858 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2859 IPMRA_VIFA_PAD) ||
2860 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2861 IPMRA_VIFA_PAD) ||
2862 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2863 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2864 nla_nest_cancel(skb, vif_nest);
2865 return false;
2866 }
2867 nla_nest_end(skb, vif_nest);
2868
2869 return true;
2870}
2871
2872static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2873{
2874 struct net *net = sock_net(skb->sk);
2875 struct nlmsghdr *nlh = NULL;
2876 unsigned int t = 0, s_t;
2877 unsigned int e = 0, s_e;
2878 struct mr_table *mrt;
2879
2880 s_t = cb->args[0];
2881 s_e = cb->args[1];
2882
2883 ipmr_for_each_table(mrt, net) {
2884 struct nlattr *vifs, *af;
2885 struct ifinfomsg *hdr;
2886 u32 i;
2887
2888 if (t < s_t)
2889 goto skip_table;
2890 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2891 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2892 sizeof(*hdr), NLM_F_MULTI);
2893 if (!nlh)
2894 break;
2895
2896 hdr = nlmsg_data(nlh);
2897 memset(hdr, 0, sizeof(*hdr));
2898 hdr->ifi_family = RTNL_FAMILY_IPMR;
2899
2900 af = nla_nest_start(skb, IFLA_AF_SPEC);
2901 if (!af) {
2902 nlmsg_cancel(skb, nlh);
2903 goto out;
2904 }
2905
2906 if (!ipmr_fill_table(mrt, skb)) {
2907 nlmsg_cancel(skb, nlh);
2908 goto out;
2909 }
2910
2911 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2912 if (!vifs) {
2913 nla_nest_end(skb, af);
2914 nlmsg_end(skb, nlh);
2915 goto out;
2916 }
2917 for (i = 0; i < mrt->maxvif; i++) {
2918 if (e < s_e)
2919 goto skip_entry;
2920 if (!ipmr_fill_vif(mrt, i, skb)) {
2921 nla_nest_end(skb, vifs);
2922 nla_nest_end(skb, af);
2923 nlmsg_end(skb, nlh);
2924 goto out;
2925 }
2926skip_entry:
2927 e++;
2928 }
2929 s_e = 0;
2930 e = 0;
2931 nla_nest_end(skb, vifs);
2932 nla_nest_end(skb, af);
2933 nlmsg_end(skb, nlh);
2934skip_table:
2935 t++;
2936 }
2937
2938out:
2939 cb->args[1] = e;
2940 cb->args[0] = t;
2941
2942 return skb->len;
2943}
2944
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002945#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002946/* The /proc interfaces to multicast routing :
2947 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 */
2949struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002950 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002951 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 int ct;
2953};
2954
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002955static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2956 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 loff_t pos)
2958{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002959 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002960
2961 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2962 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002964 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002965 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966 }
2967 return NULL;
2968}
2969
2970static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002971 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002973 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002974 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002975 struct mr_table *mrt;
2976
2977 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002978 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002979 return ERR_PTR(-ENOENT);
2980
2981 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002982
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002984 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 : SEQ_START_TOKEN;
2986}
2987
2988static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2989{
2990 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002991 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002992 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993
2994 ++*pos;
2995 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002996 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002997
Patrick McHardy0c122952010-04-13 05:03:22 +00002998 while (++iter->ct < mrt->maxvif) {
2999 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00003001 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003002 }
3003 return NULL;
3004}
3005
3006static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08003007 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008{
3009 read_unlock(&mrt_lock);
3010}
3011
3012static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
3013{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003014 struct ipmr_vif_iter *iter = seq->private;
3015 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003016
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003018 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
3020 } else {
3021 const struct vif_device *vif = v;
3022 const char *name = vif->dev ? vif->dev->name : "none";
3023
3024 seq_printf(seq,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08003025 "%2zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00003026 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003027 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 vif->bytes_out, vif->pkt_out,
3029 vif->flags, vif->local, vif->remote);
3030 }
3031 return 0;
3032}
3033
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003034static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035 .start = ipmr_vif_seq_start,
3036 .next = ipmr_vif_seq_next,
3037 .stop = ipmr_vif_seq_stop,
3038 .show = ipmr_vif_seq_show,
3039};
3040
3041static int ipmr_vif_open(struct inode *inode, struct file *file)
3042{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003043 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
3044 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045}
3046
Arjan van de Ven9a321442007-02-12 00:55:35 -08003047static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003048 .open = ipmr_vif_open,
3049 .read = seq_read,
3050 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003051 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052};
3053
3054struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003055 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003056 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00003057 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058};
3059
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003060static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
3061 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003063 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064 struct mfc_cache *mfc;
3065
Eric Dumazeta8c94862010-10-01 16:15:08 +00003066 rcu_read_lock();
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003067 it->cache = &mrt->mfc_cache_list;
3068 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
3069 if (pos-- == 0)
3070 return mfc;
Eric Dumazeta8c94862010-10-01 16:15:08 +00003071 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00003074 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00003075 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00003076 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 return mfc;
3078 spin_unlock_bh(&mfc_unres_lock);
3079
3080 it->cache = NULL;
3081 return NULL;
3082}
3083
3084
3085static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
3086{
3087 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003088 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003089 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003090
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003091 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01003092 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003093 return ERR_PTR(-ENOENT);
3094
3095 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096 it->cache = NULL;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003097 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098 : SEQ_START_TOKEN;
3099}
3100
3101static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3102{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003104 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003105 struct mr_table *mrt = it->mrt;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003106 struct mfc_cache *mfc = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107
3108 ++*pos;
3109
3110 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003111 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Patrick McHardy862465f2010-04-13 05:03:21 +00003113 if (mfc->list.next != it->cache)
3114 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003115
Patrick McHardy0c122952010-04-13 05:03:22 +00003116 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 goto end_of_list;
3118
Linus Torvalds1da177e2005-04-16 15:20:36 -07003119 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00003120 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00003121 it->cache = &mrt->mfc_unres_queue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00003124 if (!list_empty(it->cache))
3125 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00003127end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 spin_unlock_bh(&mfc_unres_lock);
3129 it->cache = NULL;
3130
3131 return NULL;
3132}
3133
3134static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
3135{
3136 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003137 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138
Patrick McHardy0c122952010-04-13 05:03:22 +00003139 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003140 spin_unlock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003141 else if (it->cache == &mrt->mfc_cache_list)
Eric Dumazeta8c94862010-10-01 16:15:08 +00003142 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143}
3144
3145static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
3146{
3147 int n;
3148
3149 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003150 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
3152 } else {
3153 const struct mfc_cache *mfc = v;
3154 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003155 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003156
Eric Dumazet0eae88f2010-04-20 19:06:52 -07003157 seq_printf(seq, "%08X %08X %-3hd",
3158 (__force u32) mfc->mfc_mcastgrp,
3159 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003160 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161
Patrick McHardy0c122952010-04-13 05:03:22 +00003162 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003163 seq_printf(seq, " %8lu %8lu %8lu",
3164 mfc->mfc_un.res.pkt,
3165 mfc->mfc_un.res.bytes,
3166 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08003167 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00003168 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00003169 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00003170 mfc->mfc_un.res.ttls[n] < 255)
3171 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003172 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 n, mfc->mfc_un.res.ttls[n]);
3174 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003175 } else {
3176 /* unresolved mfc_caches don't contain
3177 * pkt, bytes and wrong_if values
3178 */
3179 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 }
3181 seq_putc(seq, '\n');
3182 }
3183 return 0;
3184}
3185
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003186static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 .start = ipmr_mfc_seq_start,
3188 .next = ipmr_mfc_seq_next,
3189 .stop = ipmr_mfc_seq_stop,
3190 .show = ipmr_mfc_seq_show,
3191};
3192
3193static int ipmr_mfc_open(struct inode *inode, struct file *file)
3194{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003195 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
3196 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197}
3198
Arjan van de Ven9a321442007-02-12 00:55:35 -08003199static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200 .open = ipmr_mfc_open,
3201 .read = seq_read,
3202 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003203 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003204};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003205#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206
3207#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00003208static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07003210 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211};
3212#endif
3213
Yotam Gigi4d65b942017-09-27 08:23:13 +02003214static unsigned int ipmr_seq_read(struct net *net)
3215{
3216 ASSERT_RTNL();
3217
3218 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
3219}
3220
3221static int ipmr_dump(struct net *net, struct notifier_block *nb)
3222{
3223 struct mr_table *mrt;
3224 int err;
3225
3226 err = ipmr_rules_dump(net, nb);
3227 if (err)
3228 return err;
3229
3230 ipmr_for_each_table(mrt, net) {
3231 struct vif_device *v = &mrt->vif_table[0];
3232 struct mfc_cache *mfc;
3233 int vifi;
3234
3235 /* Notifiy on table VIF entries */
3236 read_lock(&mrt_lock);
3237 for (vifi = 0; vifi < mrt->maxvif; vifi++, v++) {
3238 if (!v->dev)
3239 continue;
3240
3241 call_ipmr_vif_entry_notifier(nb, net, FIB_EVENT_VIF_ADD,
3242 v, vifi, mrt->id);
3243 }
3244 read_unlock(&mrt_lock);
3245
3246 /* Notify on table MFC entries */
3247 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
3248 call_ipmr_mfc_entry_notifier(nb, net,
3249 FIB_EVENT_ENTRY_ADD, mfc,
3250 mrt->id);
3251 }
3252
3253 return 0;
3254}
3255
3256static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3257 .family = RTNL_FAMILY_IPMR,
3258 .fib_seq_read = ipmr_seq_read,
3259 .fib_dump = ipmr_dump,
3260 .owner = THIS_MODULE,
3261};
3262
Colin Ian Kingef739d82017-09-29 14:34:22 +01003263static int __net_init ipmr_notifier_init(struct net *net)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003264{
3265 struct fib_notifier_ops *ops;
3266
3267 net->ipv4.ipmr_seq = 0;
3268
3269 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3270 if (IS_ERR(ops))
3271 return PTR_ERR(ops);
3272 net->ipv4.ipmr_notifier_ops = ops;
3273
3274 return 0;
3275}
3276
3277static void __net_exit ipmr_notifier_exit(struct net *net)
3278{
3279 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3280 net->ipv4.ipmr_notifier_ops = NULL;
3281}
3282
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01003283/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00003284static int __net_init ipmr_net_init(struct net *net)
3285{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003286 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00003287
Yotam Gigi4d65b942017-09-27 08:23:13 +02003288 err = ipmr_notifier_init(net);
3289 if (err)
3290 goto ipmr_notifier_fail;
3291
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003292 err = ipmr_rules_init(net);
3293 if (err < 0)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003294 goto ipmr_rules_fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003295
3296#ifdef CONFIG_PROC_FS
3297 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00003298 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003299 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00003300 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003301 goto proc_cache_fail;
3302#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00003303 return 0;
3304
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003305#ifdef CONFIG_PROC_FS
3306proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00003307 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003308proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003309 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003310#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003311ipmr_rules_fail:
3312 ipmr_notifier_exit(net);
3313ipmr_notifier_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003314 return err;
3315}
3316
3317static void __net_exit ipmr_net_exit(struct net *net)
3318{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003319#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003320 remove_proc_entry("ip_mr_cache", net->proc_net);
3321 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003322#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003323 ipmr_notifier_exit(net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003324 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00003325}
3326
3327static struct pernet_operations ipmr_net_ops = {
3328 .init = ipmr_net_init,
3329 .exit = ipmr_net_exit,
3330};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003331
Wang Chen03d2f892008-07-03 12:13:36 +08003332int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003333{
Wang Chen03d2f892008-07-03 12:13:36 +08003334 int err;
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3337 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00003338 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003339 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08003340
Benjamin Therycf958ae32009-01-22 04:56:16 +00003341 err = register_pernet_subsys(&ipmr_net_ops);
3342 if (err)
3343 goto reg_pernet_fail;
3344
Wang Chen03d2f892008-07-03 12:13:36 +08003345 err = register_netdevice_notifier(&ip_mr_notifier);
3346 if (err)
3347 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07003348#ifdef CONFIG_IP_PIMSM_V2
3349 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00003350 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07003351 err = -EAGAIN;
3352 goto add_proto_fail;
3353 }
3354#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00003355 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003356 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003357 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003358 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003359 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003360 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03003361
3362 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
Florian Westphalb97bac62017-08-09 20:41:48 +02003363 NULL, ipmr_rtm_dumplink, 0);
Wang Chen03d2f892008-07-03 12:13:36 +08003364 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003365
Tom Goff403dbb92009-06-14 03:16:13 -07003366#ifdef CONFIG_IP_PIMSM_V2
3367add_proto_fail:
3368 unregister_netdevice_notifier(&ip_mr_notifier);
3369#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08003370reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003371 unregister_pernet_subsys(&ipmr_net_ops);
3372reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08003373 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08003374 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375}