blob: 1370edad64bfe7999f374c7e11e8d5d6e9eaab82 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Linux Consultancy and Custom Driver Development
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fixes:
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
22 * overflow.
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
Gilles Espinassef77f13e2010-03-29 15:41:47 +020025 * Relax this requirement to work with older peers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 */
28
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
Alexey Dobriyan08009a72018-02-24 21:20:33 +030031#include <linux/cache.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080032#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
34#include <linux/timer.h>
35#include <linux/mm.h>
36#include <linux/kernel.h>
37#include <linux/fcntl.h>
38#include <linux/stat.h>
39#include <linux/socket.h>
40#include <linux/in.h>
41#include <linux/inet.h>
42#include <linux/netdevice.h>
43#include <linux/inetdevice.h>
44#include <linux/igmp.h>
45#include <linux/proc_fs.h>
46#include <linux/seq_file.h>
47#include <linux/mroute.h>
48#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080049#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090050#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020051#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <net/ip.h>
53#include <net/protocol.h>
54#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020055#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <net/sock.h>
57#include <net/icmp.h>
58#include <net/udp.h>
59#include <net/raw.h>
60#include <linux/notifier.h>
61#include <linux/if_arp.h>
62#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000063#include <linux/compat.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040064#include <linux/export.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000065#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070067#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000068#include <net/fib_rules.h>
Nicolas Dichteld67b8c62012-12-04 01:13:35 +000069#include <linux/netconf.h>
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +010070#include <net/nexthop.h>
Yotam Gigi5d8b3e62017-10-03 09:58:07 +020071#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Patrick McHardyf0ad0862010-04-13 05:03:23 +000073struct ipmr_rule {
74 struct fib_rule common;
75};
76
77struct ipmr_result {
78 struct mr_table *mrt;
79};
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000082 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
84
85static DEFINE_RWLOCK(mrt_lock);
86
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +010087/* Multicast router control variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Special spinlock for queue of unresolved entries */
90static DEFINE_SPINLOCK(mfc_unres_lock);
91
92/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000093 * entries is changed only in process context and protected
94 * with weak lock mrt_lock. Queue of unresolved entries is protected
95 * with strong spinlock mfc_unres_lock.
96 *
97 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
99
Alexey Dobriyan08009a72018-02-24 21:20:33 +0300100static struct kmem_cache *mrt_cachep __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000102static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000103static void ipmr_free_table(struct mr_table *mrt);
104
Rami Rosenc4854ec2013-07-20 15:09:28 +0300105static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -0400106 struct net_device *dev, struct sk_buff *skb,
107 struct mfc_cache *cache, int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000108static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000109 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200110static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
111 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000112static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
113 int cmd);
Julien Gomes5a645dd2017-06-20 13:54:17 -0700114static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100115static void mroute_clean_tables(struct mr_table *mrt, bool all);
Kees Cooke99e88a2017-10-16 14:43:17 -0700116static void ipmr_expire_process(struct timer_list *t);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000118#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
119#define ipmr_for_each_table(mrt, net) \
120 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
121
122static struct mr_table *ipmr_get_table(struct net *net, u32 id)
123{
124 struct mr_table *mrt;
125
126 ipmr_for_each_table(mrt, net) {
127 if (mrt->id == id)
128 return mrt;
129 }
130 return NULL;
131}
132
David S. Millerda919812011-03-12 02:04:50 -0500133static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000134 struct mr_table **mrt)
135{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000136 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100137 struct ipmr_result res;
138 struct fib_lookup_arg arg = {
139 .result = &res,
140 .flags = FIB_LOOKUP_NOREF,
141 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000142
David Aherne58e4152016-10-31 15:54:00 -0700143 /* update flow if oif or iif point to device enslaved to l3mdev */
144 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
145
David S. Millerda919812011-03-12 02:04:50 -0500146 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
147 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000148 if (err < 0)
149 return err;
150 *mrt = res.mrt;
151 return 0;
152}
153
154static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
155 int flags, struct fib_lookup_arg *arg)
156{
157 struct ipmr_result *res = arg->result;
158 struct mr_table *mrt;
159
160 switch (rule->action) {
161 case FR_ACT_TO_TBL:
162 break;
163 case FR_ACT_UNREACHABLE:
164 return -ENETUNREACH;
165 case FR_ACT_PROHIBIT:
166 return -EACCES;
167 case FR_ACT_BLACKHOLE:
168 default:
169 return -EINVAL;
170 }
171
David Aherne58e4152016-10-31 15:54:00 -0700172 arg->table = fib_rule_get_table(rule, arg);
173
174 mrt = ipmr_get_table(rule->fr_net, arg->table);
Ian Morris51456b22015-04-03 09:17:26 +0100175 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000176 return -EAGAIN;
177 res->mrt = mrt;
178 return 0;
179}
180
181static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
182{
183 return 1;
184}
185
186static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
187 FRA_GENERIC_POLICY,
188};
189
190static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
191 struct fib_rule_hdr *frh, struct nlattr **tb)
192{
193 return 0;
194}
195
196static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
197 struct nlattr **tb)
198{
199 return 1;
200}
201
202static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
203 struct fib_rule_hdr *frh)
204{
205 frh->dst_len = 0;
206 frh->src_len = 0;
207 frh->tos = 0;
208 return 0;
209}
210
Andi Kleen04a6f822012-10-04 17:12:11 -0700211static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200212 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000213 .rule_size = sizeof(struct ipmr_rule),
214 .addr_size = sizeof(u32),
215 .action = ipmr_rule_action,
216 .match = ipmr_rule_match,
217 .configure = ipmr_rule_configure,
218 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000219 .fill = ipmr_rule_fill,
220 .nlgroup = RTNLGRP_IPV4_RULE,
221 .policy = ipmr_rule_policy,
222 .owner = THIS_MODULE,
223};
224
225static int __net_init ipmr_rules_init(struct net *net)
226{
227 struct fib_rules_ops *ops;
228 struct mr_table *mrt;
229 int err;
230
231 ops = fib_rules_register(&ipmr_rules_ops_template, net);
232 if (IS_ERR(ops))
233 return PTR_ERR(ops);
234
235 INIT_LIST_HEAD(&net->ipv4.mr_tables);
236
237 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100238 if (IS_ERR(mrt)) {
239 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000240 goto err1;
241 }
242
243 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
244 if (err < 0)
245 goto err2;
246
247 net->ipv4.mr_rules_ops = ops;
248 return 0;
249
250err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700251 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000252err1:
253 fib_rules_unregister(ops);
254 return err;
255}
256
257static void __net_exit ipmr_rules_exit(struct net *net)
258{
259 struct mr_table *mrt, *next;
260
WANG Conged785302015-03-31 11:01:45 -0700261 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000262 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
263 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000264 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000265 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000266 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700267 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000268}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200269
270static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
271{
272 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR);
273}
274
275static unsigned int ipmr_rules_seq_read(struct net *net)
276{
277 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
278}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200279
280bool ipmr_rule_default(const struct fib_rule *rule)
281{
282 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
283}
284EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000285#else
286#define ipmr_for_each_table(mrt, net) \
287 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
288
289static struct mr_table *ipmr_get_table(struct net *net, u32 id)
290{
291 return net->ipv4.mrt;
292}
293
David S. Millerda919812011-03-12 02:04:50 -0500294static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000295 struct mr_table **mrt)
296{
297 *mrt = net->ipv4.mrt;
298 return 0;
299}
300
301static int __net_init ipmr_rules_init(struct net *net)
302{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100303 struct mr_table *mrt;
304
305 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
306 if (IS_ERR(mrt))
307 return PTR_ERR(mrt);
308 net->ipv4.mrt = mrt;
309 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000310}
311
312static void __net_exit ipmr_rules_exit(struct net *net)
313{
WANG Conged785302015-03-31 11:01:45 -0700314 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000315 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700316 net->ipv4.mrt = NULL;
317 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000318}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200319
320static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
321{
322 return 0;
323}
324
325static unsigned int ipmr_rules_seq_read(struct net *net)
326{
327 return 0;
328}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200329
330bool ipmr_rule_default(const struct fib_rule *rule)
331{
332 return true;
333}
334EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000335#endif
336
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100337static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
338 const void *ptr)
339{
340 const struct mfc_cache_cmp_arg *cmparg = arg->key;
341 struct mfc_cache *c = (struct mfc_cache *)ptr;
342
343 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
344 cmparg->mfc_origin != c->mfc_origin;
345}
346
347static const struct rhashtable_params ipmr_rht_params = {
348 .head_offset = offsetof(struct mfc_cache, mnode),
349 .key_offset = offsetof(struct mfc_cache, cmparg),
350 .key_len = sizeof(struct mfc_cache_cmp_arg),
351 .nelem_hint = 3,
352 .locks_mul = 1,
353 .obj_cmpfn = ipmr_hash_cmp,
354 .automatic_shrinking = true,
355};
356
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000357static struct mr_table *ipmr_new_table(struct net *net, u32 id)
358{
359 struct mr_table *mrt;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000360
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100361 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
362 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
363 return ERR_PTR(-EINVAL);
364
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000365 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100366 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000367 return mrt;
368
369 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100370 if (!mrt)
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100371 return ERR_PTR(-ENOMEM);
Patrick McHardy8de53df2010-04-15 13:29:28 +0200372 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000373 mrt->id = id;
374
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100375 rhltable_init(&mrt->mfc_hash, &ipmr_rht_params);
376 INIT_LIST_HEAD(&mrt->mfc_cache_list);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000377 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
378
Kees Cooke99e88a2017-10-16 14:43:17 -0700379 timer_setup(&mrt->ipmr_expire_timer, ipmr_expire_process, 0);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000380
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000381 mrt->mroute_reg_vif_num = -1;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000382#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
383 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
384#endif
385 return mrt;
386}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000388static void ipmr_free_table(struct mr_table *mrt)
389{
390 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100391 mroute_clean_tables(mrt, true);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100392 rhltable_destroy(&mrt->mfc_hash);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000393 kfree(mrt);
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
397
Wang Chend6070322008-07-14 20:55:26 -0700398static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
399{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000400 struct net *net = dev_net(dev);
401
Wang Chend6070322008-07-14 20:55:26 -0700402 dev_close(dev);
403
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000404 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700405 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800406 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700407 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700408 struct ip_tunnel_parm p;
409
410 memset(&p, 0, sizeof(p));
411 p.iph.daddr = v->vifc_rmt_addr.s_addr;
412 p.iph.saddr = v->vifc_lcl_addr.s_addr;
413 p.iph.version = 4;
414 p.iph.ihl = 5;
415 p.iph.protocol = IPPROTO_IPIP;
416 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
417 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
418
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800419 if (ops->ndo_do_ioctl) {
420 mm_segment_t oldfs = get_fs();
421
422 set_fs(KERNEL_DS);
423 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
424 set_fs(oldfs);
425 }
Wang Chend6070322008-07-14 20:55:26 -0700426 }
427}
428
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100429/* Initialize ipmr pimreg/tunnel in_device */
430static bool ipmr_init_vif_indev(const struct net_device *dev)
431{
432 struct in_device *in_dev;
433
434 ASSERT_RTNL();
435
436 in_dev = __in_dev_get_rtnl(dev);
437 if (!in_dev)
438 return false;
439 ipv4_devconf_setall(in_dev);
440 neigh_parms_data_state_setall(in_dev->arp_parms);
441 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
442
443 return true;
444}
445
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100446static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 struct net_device *dev;
449
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000450 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800453 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 int err;
455 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
458 memset(&p, 0, sizeof(p));
459 p.iph.daddr = v->vifc_rmt_addr.s_addr;
460 p.iph.saddr = v->vifc_lcl_addr.s_addr;
461 p.iph.version = 4;
462 p.iph.ihl = 5;
463 p.iph.protocol = IPPROTO_IPIP;
464 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800465 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800467 if (ops->ndo_do_ioctl) {
468 mm_segment_t oldfs = get_fs();
469
470 set_fs(KERNEL_DS);
471 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
472 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000473 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800474 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000475 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 dev = NULL;
477
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000478 if (err == 0 &&
479 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100481 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 if (dev_open(dev))
484 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700485 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 }
488 return dev;
489
490failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 unregister_netdevice(dev);
492 return NULL;
493}
494
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100495#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000496static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000498 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000499 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500500 struct flowi4 fl4 = {
501 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700502 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500503 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000504 };
505 int err;
506
David S. Millerda919812011-03-12 02:04:50 -0500507 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000508 if (err < 0) {
509 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000510 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000511 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700514 dev->stats.tx_bytes += skb->len;
515 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000516 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 read_unlock(&mrt_lock);
518 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000519 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200522static int reg_vif_get_iflink(const struct net_device *dev)
523{
524 return 0;
525}
526
Stephen Hemminger007c3832008-11-20 20:28:35 -0800527static const struct net_device_ops reg_vif_netdev_ops = {
528 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200529 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800530};
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532static void reg_vif_setup(struct net_device *dev)
533{
534 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800535 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530537 dev->netdev_ops = &reg_vif_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400538 dev->needs_free_netdev = true;
Tom Goff403dbb92009-06-14 03:16:13 -0700539 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}
541
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000542static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000545 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000547 if (mrt->id == RT_TABLE_DEFAULT)
548 sprintf(name, "pimreg");
549 else
550 sprintf(name, "pimreg%u", mrt->id);
551
Tom Gundersenc835a672014-07-14 16:37:24 +0200552 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Ian Morris51456b22015-04-03 09:17:26 +0100554 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 return NULL;
556
Tom Goff403dbb92009-06-14 03:16:13 -0700557 dev_net_set(dev, net);
558
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (register_netdevice(dev)) {
560 free_netdev(dev);
561 return NULL;
562 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100564 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 if (dev_open(dev))
567 goto failure;
568
Wang Chen7dc00c82008-07-14 20:56:34 -0700569 dev_hold(dev);
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return dev;
572
573failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 unregister_netdevice(dev);
575 return NULL;
576}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100577
578/* called with rcu_read_lock() */
579static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
580 unsigned int pimlen)
581{
582 struct net_device *reg_dev = NULL;
583 struct iphdr *encap;
584
585 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100586 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100587 * a. packet is really sent to a multicast group
588 * b. packet is not a NULL-REGISTER
589 * c. packet is not truncated
590 */
591 if (!ipv4_is_multicast(encap->daddr) ||
592 encap->tot_len == 0 ||
593 ntohs(encap->tot_len) + pimlen > skb->len)
594 return 1;
595
596 read_lock(&mrt_lock);
597 if (mrt->mroute_reg_vif_num >= 0)
598 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
599 read_unlock(&mrt_lock);
600
601 if (!reg_dev)
602 return 1;
603
604 skb->mac_header = skb->network_header;
605 skb_pull(skb, (u8 *)encap - skb->data);
606 skb_reset_network_header(skb);
607 skb->protocol = htons(ETH_P_IP);
608 skb->ip_summed = CHECKSUM_NONE;
609
610 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
611
612 netif_rx(skb);
613
614 return NET_RX_SUCCESS;
615}
616#else
617static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
618{
619 return NULL;
620}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621#endif
622
Yotam Gigi4d65b942017-09-27 08:23:13 +0200623static int call_ipmr_vif_entry_notifier(struct notifier_block *nb,
624 struct net *net,
625 enum fib_event_type event_type,
626 struct vif_device *vif,
627 vifi_t vif_index, u32 tb_id)
628{
629 struct vif_entry_notifier_info info = {
630 .info = {
631 .family = RTNL_FAMILY_IPMR,
632 .net = net,
633 },
634 .dev = vif->dev,
635 .vif_index = vif_index,
636 .vif_flags = vif->flags,
637 .tb_id = tb_id,
638 };
639
640 return call_fib_notifier(nb, net, event_type, &info.info);
641}
642
Yotam Gigib3620532017-09-27 08:23:14 +0200643static int call_ipmr_vif_entry_notifiers(struct net *net,
644 enum fib_event_type event_type,
645 struct vif_device *vif,
646 vifi_t vif_index, u32 tb_id)
647{
648 struct vif_entry_notifier_info info = {
649 .info = {
650 .family = RTNL_FAMILY_IPMR,
651 .net = net,
652 },
653 .dev = vif->dev,
654 .vif_index = vif_index,
655 .vif_flags = vif->flags,
656 .tb_id = tb_id,
657 };
658
659 ASSERT_RTNL();
660 net->ipv4.ipmr_seq++;
661 return call_fib_notifiers(net, event_type, &info.info);
662}
663
Yotam Gigi4d65b942017-09-27 08:23:13 +0200664static int call_ipmr_mfc_entry_notifier(struct notifier_block *nb,
665 struct net *net,
666 enum fib_event_type event_type,
667 struct mfc_cache *mfc, u32 tb_id)
668{
669 struct mfc_entry_notifier_info info = {
670 .info = {
671 .family = RTNL_FAMILY_IPMR,
672 .net = net,
673 },
674 .mfc = mfc,
675 .tb_id = tb_id
676 };
677
678 return call_fib_notifier(nb, net, event_type, &info.info);
679}
680
Yotam Gigib3620532017-09-27 08:23:14 +0200681static int call_ipmr_mfc_entry_notifiers(struct net *net,
682 enum fib_event_type event_type,
683 struct mfc_cache *mfc, u32 tb_id)
684{
685 struct mfc_entry_notifier_info info = {
686 .info = {
687 .family = RTNL_FAMILY_IPMR,
688 .net = net,
689 },
690 .mfc = mfc,
691 .tb_id = tb_id
692 };
693
694 ASSERT_RTNL();
695 net->ipv4.ipmr_seq++;
696 return call_fib_notifiers(net, event_type, &info.info);
697}
698
Ben Hutchings2c530402012-07-10 10:55:09 +0000699/**
700 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700701 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000703static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000704 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705{
Yotam Gigib3620532017-09-27 08:23:14 +0200706 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 struct vif_device *v;
708 struct net_device *dev;
709 struct in_device *in_dev;
710
Patrick McHardy0c122952010-04-13 05:03:22 +0000711 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 return -EADDRNOTAVAIL;
713
Patrick McHardy0c122952010-04-13 05:03:22 +0000714 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715
Yotam Gigib3620532017-09-27 08:23:14 +0200716 if (VIF_EXISTS(mrt, vifi))
717 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
718 mrt->id);
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 write_lock_bh(&mrt_lock);
721 dev = v->dev;
722 v->dev = NULL;
723
724 if (!dev) {
725 write_unlock_bh(&mrt_lock);
726 return -EADDRNOTAVAIL;
727 }
728
Patrick McHardy0c122952010-04-13 05:03:22 +0000729 if (vifi == mrt->mroute_reg_vif_num)
730 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000732 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000734
735 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000736 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 break;
738 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000739 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 }
741
742 write_unlock_bh(&mrt_lock);
743
744 dev_set_allmulti(dev, -1);
745
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000746 in_dev = __in_dev_get_rtnl(dev);
747 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700748 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -0700749 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000750 NETCONFA_MC_FORWARDING,
751 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 ip_rt_multicast_event(in_dev);
753 }
754
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000755 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000756 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757
758 dev_put(dev);
759 return 0;
760}
761
Eric Dumazeta8c94862010-10-01 16:15:08 +0000762static void ipmr_cache_free_rcu(struct rcu_head *head)
763{
764 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
765
766 kmem_cache_free(mrt_cachep, c);
767}
768
Yotam Gigi310ebbb2017-09-27 08:23:12 +0200769void ipmr_cache_free(struct mfc_cache *c)
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000770{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000771 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000772}
Yotam Gigi310ebbb2017-09-27 08:23:12 +0200773EXPORT_SYMBOL(ipmr_cache_free);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000776 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000778static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200780 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700782 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Patrick McHardy0c122952010-04-13 05:03:22 +0000784 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
Jianjun Kongc354e122008-11-03 00:28:02 -0800786 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700787 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200788 struct nlmsghdr *nlh = skb_pull(skb,
789 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000791 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000793 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700794 e->error = -ETIMEDOUT;
795 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700796
Eric W. Biederman15e47302012-09-07 20:12:54 +0000797 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000798 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 }
802
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000803 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804}
805
Patrick McHardye258beb2010-04-13 05:03:19 +0000806/* Timer process for the unresolved queue. */
Kees Cooke99e88a2017-10-16 14:43:17 -0700807static void ipmr_expire_process(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
Kees Cooke99e88a2017-10-16 14:43:17 -0700809 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 unsigned long now;
811 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000812 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000815 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 return;
817 }
818
Patrick McHardy0c122952010-04-13 05:03:22 +0000819 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 goto out;
821
822 now = jiffies;
823 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824
Patrick McHardy0c122952010-04-13 05:03:22 +0000825 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 if (time_after(c->mfc_un.unres.expires, now)) {
827 unsigned long interval = c->mfc_un.unres.expires - now;
828 if (interval < expires)
829 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 continue;
831 }
832
Patrick McHardy862465f2010-04-13 05:03:21 +0000833 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000834 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000835 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 }
837
Patrick McHardy0c122952010-04-13 05:03:22 +0000838 if (!list_empty(&mrt->mfc_unres_queue))
839 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840
841out:
842 spin_unlock(&mfc_unres_lock);
843}
844
845/* Fill oifs list. It is called under write locked mrt_lock. */
Patrick McHardy0c122952010-04-13 05:03:22 +0000846static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000847 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848{
849 int vifi;
850
851 cache->mfc_un.res.minvif = MAXVIFS;
852 cache->mfc_un.res.maxvif = 0;
853 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
854
Patrick McHardy0c122952010-04-13 05:03:22 +0000855 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
856 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000857 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
859 if (cache->mfc_un.res.minvif > vifi)
860 cache->mfc_un.res.minvif = vifi;
861 if (cache->mfc_un.res.maxvif <= vifi)
862 cache->mfc_un.res.maxvif = vifi + 1;
863 }
864 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200865 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866}
867
Patrick McHardy0c122952010-04-13 05:03:22 +0000868static int vif_add(struct net *net, struct mr_table *mrt,
869 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870{
871 int vifi = vifc->vifc_vifi;
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200872 struct switchdev_attr attr = {
873 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
874 };
Patrick McHardy0c122952010-04-13 05:03:22 +0000875 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 struct net_device *dev;
877 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700878 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000881 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return -EADDRINUSE;
883
884 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100886 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100887 return -EINVAL;
888 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 * All the packets will be sent to the daemon
890 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000891 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000893 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if (!dev)
895 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700896 err = dev_set_allmulti(dev, 1);
897 if (err) {
898 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700899 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700900 return err;
901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900903 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000904 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 if (!dev)
906 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700907 err = dev_set_allmulti(dev, 1);
908 if (err) {
909 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700910 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700911 return err;
912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000914 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000916 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
917 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100918 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000919 dev_put(dev);
920 return -EADDRNOTAVAIL;
921 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000922 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000923 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000924 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (!dev)
926 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700927 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700928 if (err) {
929 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700930 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 break;
933 default:
934 return -EINVAL;
935 }
936
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000937 in_dev = __in_dev_get_rtnl(dev);
938 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000939 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000941 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700942 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700943 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
944 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 ip_rt_multicast_event(in_dev);
946
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000947 /* Fill in the VIF structures */
Yuval Mintz6853f212018-02-28 23:29:29 +0200948 vif_device_init(v, dev, vifc->vifc_rate_limit,
949 vifc->vifc_threshold,
950 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
951 (VIFF_TUNNEL | VIFF_REGISTER));
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000952
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200953 attr.orig_dev = dev;
954 if (!switchdev_port_attr_get(dev, &attr)) {
955 memcpy(v->dev_parent_id.id, attr.u.ppid.id, attr.u.ppid.id_len);
956 v->dev_parent_id.id_len = attr.u.ppid.id_len;
957 } else {
958 v->dev_parent_id.id_len = 0;
959 }
Yuval Mintz6853f212018-02-28 23:29:29 +0200960
Jianjun Kongc354e122008-11-03 00:28:02 -0800961 v->local = vifc->vifc_lcl_addr.s_addr;
962 v->remote = vifc->vifc_rmt_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
964 /* And finish update writing critical data */
965 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800966 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000967 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000968 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000969 if (vifi+1 > mrt->maxvif)
970 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +0200972 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 return 0;
974}
975
Eric Dumazeta8c94862010-10-01 16:15:08 +0000976/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000977static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000978 __be32 origin,
979 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100981 struct mfc_cache_cmp_arg arg = {
982 .mfc_mcastgrp = mcastgrp,
983 .mfc_origin = origin
984 };
985 struct rhlist_head *tmp, *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986 struct mfc_cache *c;
987
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100988 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
989 rhl_for_each_entry_rcu(c, tmp, list, mnode)
990 return c;
991
Patrick McHardy862465f2010-04-13 05:03:21 +0000992 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993}
994
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000995/* Look for a (*,*,oif) entry */
996static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
997 int vifi)
998{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100999 struct mfc_cache_cmp_arg arg = {
1000 .mfc_mcastgrp = htonl(INADDR_ANY),
1001 .mfc_origin = htonl(INADDR_ANY)
1002 };
1003 struct rhlist_head *tmp, *list;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001004 struct mfc_cache *c;
1005
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001006 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
1007 rhl_for_each_entry_rcu(c, tmp, list, mnode)
1008 if (c->mfc_un.res.ttls[vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001009 return c;
1010
1011 return NULL;
1012}
1013
1014/* Look for a (*,G) entry */
1015static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
1016 __be32 mcastgrp, int vifi)
1017{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001018 struct mfc_cache_cmp_arg arg = {
1019 .mfc_mcastgrp = mcastgrp,
1020 .mfc_origin = htonl(INADDR_ANY)
1021 };
1022 struct rhlist_head *tmp, *list;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001023 struct mfc_cache *c, *proxy;
1024
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001025 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001026 goto skip;
1027
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001028 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
1029 rhl_for_each_entry_rcu(c, tmp, list, mnode) {
1030 if (c->mfc_un.res.ttls[vifi] < 255)
1031 return c;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001032
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001033 /* It's ok if the vifi is part of the static tree */
1034 proxy = ipmr_cache_find_any_parent(mrt, c->mfc_parent);
1035 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
1036 return c;
1037 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001038
1039skip:
1040 return ipmr_cache_find_any_parent(mrt, vifi);
1041}
1042
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001043/* Look for a (S,G,iif) entry if parent != -1 */
1044static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
1045 __be32 origin, __be32 mcastgrp,
1046 int parent)
1047{
1048 struct mfc_cache_cmp_arg arg = {
1049 .mfc_mcastgrp = mcastgrp,
1050 .mfc_origin = origin,
1051 };
1052 struct rhlist_head *tmp, *list;
1053 struct mfc_cache *c;
1054
1055 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
1056 rhl_for_each_entry_rcu(c, tmp, list, mnode)
1057 if (parent == -1 || parent == c->mfc_parent)
1058 return c;
1059
1060 return NULL;
1061}
1062
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001063/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001064static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Jianjun Kongc354e122008-11-03 00:28:02 -08001066 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001067
Tom Goff70a0dec2016-06-23 16:11:57 -04001068 if (c) {
1069 c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001070 c->mfc_un.res.minvif = MAXVIFS;
Yotam Gigi310ebbb2017-09-27 08:23:12 +02001071 refcount_set(&c->mfc_un.res.refcount, 1);
Tom Goff70a0dec2016-06-23 16:11:57 -04001072 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 return c;
1074}
1075
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001076static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077{
Jianjun Kongc354e122008-11-03 00:28:02 -08001078 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001079
1080 if (c) {
1081 skb_queue_head_init(&c->mfc_un.unres.unresolved);
1082 c->mfc_un.unres.expires = jiffies + 10*HZ;
1083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return c;
1085}
1086
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001087/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +00001088static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1089 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090{
1091 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001092 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001094 /* Play the pending entries through our router */
Jianjun Kongc354e122008-11-03 00:28:02 -08001095 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001096 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +02001097 struct nlmsghdr *nlh = skb_pull(skb,
1098 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Hong zhi guo573ce262013-03-27 06:47:04 +00001100 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001101 nlh->nlmsg_len = skb_tail_pointer(skb) -
1102 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 } else {
1104 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +00001105 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +00001107 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001108 e->error = -EMSGSIZE;
1109 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
Thomas Graf2942e902006-08-15 00:30:25 -07001111
Eric W. Biederman15e47302012-09-07 20:12:54 +00001112 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001113 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001114 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 }
1117}
1118
Julien Gomes5a645dd2017-06-20 13:54:17 -07001119/* Bounce a cache query up to mrouted and netlink.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001121 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001123static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001124 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001126 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001127 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 struct igmphdr *igmp;
1129 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001130 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 int ret;
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 if (assert == IGMPMSG_WHOLEPKT)
1134 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1135 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 skb = alloc_skb(128, GFP_ATOMIC);
1137
Stephen Hemminger132adf52007-03-08 20:44:43 -08001138 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 return -ENOBUFS;
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (assert == IGMPMSG_WHOLEPKT) {
1142 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001143 * Duplicate old header, fix ihl, length etc.
1144 * And all this only to mangle msg->im_msgtype and
1145 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001147 skb_push(skb, sizeof(struct iphdr));
1148 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001149 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001150 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001151 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1153 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001154 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001155 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1156 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1157 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001158 } else {
1159 /* Copy the IP header */
1160 skb_set_network_header(skb, skb->len);
1161 skb_put(skb, ihl);
1162 skb_copy_to_linear_data(skb, pkt->data, ihl);
1163 /* Flag to the kernel this is a route add */
1164 ip_hdr(skb)->protocol = 0;
1165 msg = (struct igmpmsg *)skb_network_header(skb);
1166 msg->im_vif = vifi;
1167 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1168 /* Add our header */
Johannes Berg4df864c2017-06-16 14:29:21 +02001169 igmp = skb_put(skb, sizeof(struct igmphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001170 igmp->type = assert;
1171 msg->im_msgtype = assert;
1172 igmp->code = 0;
1173 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1174 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001175 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
Eric Dumazet4c9687092010-10-01 16:15:01 +00001177 rcu_read_lock();
1178 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001179 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001180 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 kfree_skb(skb);
1182 return -EINVAL;
1183 }
1184
Julien Gomes5a645dd2017-06-20 13:54:17 -07001185 igmpmsg_netlink_event(mrt, skb);
1186
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001187 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001188 ret = sock_queue_rcv_skb(mroute_sk, skb);
1189 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001190 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001191 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 kfree_skb(skb);
1193 }
1194
1195 return ret;
1196}
1197
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001198/* Queue a packet for resolution. It gets locked cache entry! */
1199static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001200 struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001202 const struct iphdr *iph = ip_hdr(skb);
1203 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001204 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
1207 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001208 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001209 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001210 c->mfc_origin == iph->saddr) {
1211 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001213 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215
Patrick McHardy862465f2010-04-13 05:03:21 +00001216 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001217 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001218 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001219 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 spin_unlock_bh(&mfc_unres_lock);
1221
1222 kfree_skb(skb);
1223 return -ENOBUFS;
1224 }
1225
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001226 /* Fill in the new cache entry */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001227 c->mfc_parent = -1;
1228 c->mfc_origin = iph->saddr;
1229 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001231 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001232 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001233 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001234 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 out - Brad Parker
1236 */
1237 spin_unlock_bh(&mfc_unres_lock);
1238
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001239 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 kfree_skb(skb);
1241 return err;
1242 }
1243
Patrick McHardy0c122952010-04-13 05:03:22 +00001244 atomic_inc(&mrt->cache_resolve_queue_len);
1245 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001246 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247
David S. Miller278554b2010-05-12 00:05:35 -07001248 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1249 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 }
1251
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001252 /* See if we can append the packet */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001253 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 kfree_skb(skb);
1255 err = -ENOBUFS;
1256 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001257 if (dev) {
1258 skb->dev = dev;
1259 skb->skb_iif = dev->ifindex;
1260 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001261 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 err = 0;
1263 }
1264
1265 spin_unlock_bh(&mfc_unres_lock);
1266 return err;
1267}
1268
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001269/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001271static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272{
Yotam Gigib3620532017-09-27 08:23:14 +02001273 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001274 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001276 /* The entries are added/deleted only under RTNL */
1277 rcu_read_lock();
1278 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1279 mfc->mfcc_mcastgrp.s_addr, parent);
1280 rcu_read_unlock();
1281 if (!c)
1282 return -ENOENT;
1283 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1284 list_del_rcu(&c->list);
Yotam Gigib3620532017-09-27 08:23:14 +02001285 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001286 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yotam Gigi310ebbb2017-09-27 08:23:12 +02001287 ipmr_cache_put(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001289 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290}
1291
Patrick McHardy0c122952010-04-13 05:03:22 +00001292static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001293 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294{
Patrick McHardy862465f2010-04-13 05:03:21 +00001295 struct mfc_cache *uc, *c;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001296 bool found;
1297 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
Patrick McHardya50436f22010-03-17 06:04:14 +00001299 if (mfc->mfcc_parent >= MAXVIFS)
1300 return -ENFILE;
1301
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001302 /* The entries are added/deleted only under RTNL */
1303 rcu_read_lock();
1304 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1305 mfc->mfcc_mcastgrp.s_addr, parent);
1306 rcu_read_unlock();
1307 if (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 write_lock_bh(&mrt_lock);
1309 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001310 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 if (!mrtsock)
1312 c->mfc_flags |= MFC_STATIC;
1313 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +02001314 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1315 mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001316 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317 return 0;
1318 }
1319
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001320 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001321 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001322 return -EINVAL;
1323
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001324 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001325 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return -ENOMEM;
1327
Jianjun Kongc354e122008-11-03 00:28:02 -08001328 c->mfc_origin = mfc->mfcc_origin.s_addr;
1329 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1330 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001331 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001332 if (!mrtsock)
1333 c->mfc_flags |= MFC_STATIC;
1334
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001335 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->mnode,
1336 ipmr_rht_params);
1337 if (ret) {
1338 pr_err("ipmr: rhtable insert error %d\n", ret);
1339 ipmr_cache_free(c);
1340 return ret;
1341 }
1342 list_add_tail_rcu(&c->list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001343 /* Check to see if we resolved a queued list. If so we
1344 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001346 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001348 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001349 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001351 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001352 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001353 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 break;
1355 }
1356 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001357 if (list_empty(&mrt->mfc_unres_queue))
1358 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 spin_unlock_bh(&mfc_unres_lock);
1360
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001361 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001362 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001363 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
Yotam Gigib3620532017-09-27 08:23:14 +02001365 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001366 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return 0;
1368}
1369
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001370/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001371static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Yotam Gigib3620532017-09-27 08:23:14 +02001373 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001374 struct mfc_cache *c, *tmp;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001375 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001376 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001377
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001378 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001379 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001380 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1381 continue;
1382 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001384 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001386 /* Wipe the cache */
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001387 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1388 if (!all && (c->mfc_flags & MFC_STATIC))
1389 continue;
1390 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1391 list_del_rcu(&c->list);
Yotam Gigib3620532017-09-27 08:23:14 +02001392 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c,
1393 mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001394 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yotam Gigi310ebbb2017-09-27 08:23:12 +02001395 ipmr_cache_put(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
Patrick McHardy0c122952010-04-13 05:03:22 +00001398 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 spin_lock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001400 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001401 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001402 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001403 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404 }
1405 spin_unlock_bh(&mfc_unres_lock);
1406 }
1407}
1408
Eric Dumazet4c9687092010-10-01 16:15:01 +00001409/* called from ip_ra_control(), before an RCU grace period,
1410 * we dont need to call synchronize_rcu() here
1411 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412static void mrtsock_destruct(struct sock *sk)
1413{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001414 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001415 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001416
WANG Cong1215e512017-04-12 12:32:13 -07001417 ASSERT_RTNL();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001418 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001419 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001420 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001421 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1422 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001423 NETCONFA_IFINDEX_ALL,
1424 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001425 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001426 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429}
1430
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001431/* Socket options and virtual interface manipulation. The whole
1432 * virtual interface system is a complete heap, but unfortunately
1433 * that's how BSD mrouted happens to think. Maybe one day with a proper
1434 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001436
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001437int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1438 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001440 struct net *net = sock_net(sk);
1441 int val, ret = 0, parent = 0;
1442 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 struct vifctl vif;
1444 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001445 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001446
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001447 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1448 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001449 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001450 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1451 ret = -EOPNOTSUPP;
1452 goto out_unlock;
1453 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001454
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001455 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001456 if (!mrt) {
1457 ret = -ENOENT;
1458 goto out_unlock;
1459 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001460 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001461 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001462 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1463 ret = -EACCES;
1464 goto out_unlock;
1465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 }
1467
Stephen Hemminger132adf52007-03-08 20:44:43 -08001468 switch (optname) {
1469 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001470 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001471 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001472 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001473 }
1474 if (rtnl_dereference(mrt->mroute_sk)) {
1475 ret = -EADDRINUSE;
1476 break;
1477 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001478
1479 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1480 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001481 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001482 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001483 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1484 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001485 NETCONFA_IFINDEX_ALL,
1486 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001487 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001488 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001489 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001490 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1491 ret = -EACCES;
1492 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001493 ret = ip_ra_control(sk, 0, NULL);
WANG Cong1215e512017-04-12 12:32:13 -07001494 goto out_unlock;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001495 }
1496 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001497 case MRT_ADD_VIF:
1498 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001499 if (optlen != sizeof(vif)) {
1500 ret = -EINVAL;
1501 break;
1502 }
1503 if (copy_from_user(&vif, optval, sizeof(vif))) {
1504 ret = -EFAULT;
1505 break;
1506 }
1507 if (vif.vifc_vifi >= MAXVIFS) {
1508 ret = -ENFILE;
1509 break;
1510 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001511 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001512 ret = vif_add(net, mrt, &vif,
1513 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001514 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001515 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001516 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001517 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001518 /* Manipulate the forwarding caches. These live
1519 * in a sort of kernel/user symbiosis.
1520 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001521 case MRT_ADD_MFC:
1522 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001523 parent = -1;
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05001524 /* fall through */
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001525 case MRT_ADD_MFC_PROXY:
1526 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001527 if (optlen != sizeof(mfc)) {
1528 ret = -EINVAL;
1529 break;
1530 }
1531 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1532 ret = -EFAULT;
1533 break;
1534 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001535 if (parent == 0)
1536 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001537 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1538 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001539 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001540 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001541 sk == rtnl_dereference(mrt->mroute_sk),
1542 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001543 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001544 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001545 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001546 if (optlen != sizeof(val)) {
1547 ret = -EINVAL;
1548 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001550 if (get_user(val, (int __user *)optval)) {
1551 ret = -EFAULT;
1552 break;
1553 }
1554 mrt->mroute_do_assert = val;
1555 break;
1556 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001557 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001558 ret = -ENOPROTOOPT;
1559 break;
1560 }
1561 if (optlen != sizeof(val)) {
1562 ret = -EINVAL;
1563 break;
1564 }
1565 if (get_user(val, (int __user *)optval)) {
1566 ret = -EFAULT;
1567 break;
1568 }
1569
1570 val = !!val;
1571 if (val != mrt->mroute_do_pim) {
1572 mrt->mroute_do_pim = val;
1573 mrt->mroute_do_assert = val;
1574 }
1575 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001576 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001577 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1578 ret = -ENOPROTOOPT;
1579 break;
1580 }
1581 if (optlen != sizeof(uval)) {
1582 ret = -EINVAL;
1583 break;
1584 }
1585 if (get_user(uval, (u32 __user *)optval)) {
1586 ret = -EFAULT;
1587 break;
1588 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001589
Eric Dumazet4c9687092010-10-01 16:15:01 +00001590 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1591 ret = -EBUSY;
1592 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001593 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001594 if (IS_ERR(mrt))
1595 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001596 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001597 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001598 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001599 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001600 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001601 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001602 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001604out_unlock:
1605 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001606 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607}
1608
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001609/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001610int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611{
1612 int olr;
1613 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001614 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001615 struct mr_table *mrt;
1616
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001617 if (sk->sk_type != SOCK_RAW ||
1618 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1619 return -EOPNOTSUPP;
1620
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001621 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001622 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001623 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001625 switch (optname) {
1626 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001627 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001628 break;
1629 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001630 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001631 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001632 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001633 break;
1634 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001635 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001636 break;
1637 default:
1638 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001639 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001640
1641 if (get_user(olr, optlen))
1642 return -EFAULT;
1643 olr = min_t(unsigned int, olr, sizeof(int));
1644 if (olr < 0)
1645 return -EINVAL;
1646 if (put_user(olr, optlen))
1647 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001648 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 return -EFAULT;
1650 return 0;
1651}
1652
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001653/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1655{
1656 struct sioc_sg_req sr;
1657 struct sioc_vif_req vr;
1658 struct vif_device *vif;
1659 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001660 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001661 struct mr_table *mrt;
1662
1663 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001664 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001665 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001666
Stephen Hemminger132adf52007-03-08 20:44:43 -08001667 switch (cmd) {
1668 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001669 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001670 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001671 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001672 return -EINVAL;
1673 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001674 vif = &mrt->vif_table[vr.vifi];
1675 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001676 vr.icount = vif->pkt_in;
1677 vr.ocount = vif->pkt_out;
1678 vr.ibytes = vif->bytes_in;
1679 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001681
Jianjun Kongc354e122008-11-03 00:28:02 -08001682 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001684 return 0;
1685 }
1686 read_unlock(&mrt_lock);
1687 return -EADDRNOTAVAIL;
1688 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001689 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001690 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691
Eric Dumazeta8c94862010-10-01 16:15:08 +00001692 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001693 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001694 if (c) {
1695 sr.pktcnt = c->mfc_un.res.pkt;
1696 sr.bytecnt = c->mfc_un.res.bytes;
1697 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001698 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001699
Jianjun Kongc354e122008-11-03 00:28:02 -08001700 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001701 return -EFAULT;
1702 return 0;
1703 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001704 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001705 return -EADDRNOTAVAIL;
1706 default:
1707 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709}
1710
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001711#ifdef CONFIG_COMPAT
1712struct compat_sioc_sg_req {
1713 struct in_addr src;
1714 struct in_addr grp;
1715 compat_ulong_t pktcnt;
1716 compat_ulong_t bytecnt;
1717 compat_ulong_t wrong_if;
1718};
1719
David S. Millerca6b8bb02011-02-03 17:24:28 -08001720struct compat_sioc_vif_req {
1721 vifi_t vifi; /* Which iface */
1722 compat_ulong_t icount;
1723 compat_ulong_t ocount;
1724 compat_ulong_t ibytes;
1725 compat_ulong_t obytes;
1726};
1727
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001728int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1729{
David S. Miller0033d5a2011-02-03 17:21:31 -08001730 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001731 struct compat_sioc_vif_req vr;
1732 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001733 struct mfc_cache *c;
1734 struct net *net = sock_net(sk);
1735 struct mr_table *mrt;
1736
1737 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001738 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001739 return -ENOENT;
1740
1741 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001742 case SIOCGETVIFCNT:
1743 if (copy_from_user(&vr, arg, sizeof(vr)))
1744 return -EFAULT;
1745 if (vr.vifi >= mrt->maxvif)
1746 return -EINVAL;
1747 read_lock(&mrt_lock);
1748 vif = &mrt->vif_table[vr.vifi];
1749 if (VIF_EXISTS(mrt, vr.vifi)) {
1750 vr.icount = vif->pkt_in;
1751 vr.ocount = vif->pkt_out;
1752 vr.ibytes = vif->bytes_in;
1753 vr.obytes = vif->bytes_out;
1754 read_unlock(&mrt_lock);
1755
1756 if (copy_to_user(arg, &vr, sizeof(vr)))
1757 return -EFAULT;
1758 return 0;
1759 }
1760 read_unlock(&mrt_lock);
1761 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001762 case SIOCGETSGCNT:
1763 if (copy_from_user(&sr, arg, sizeof(sr)))
1764 return -EFAULT;
1765
1766 rcu_read_lock();
1767 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1768 if (c) {
1769 sr.pktcnt = c->mfc_un.res.pkt;
1770 sr.bytecnt = c->mfc_un.res.bytes;
1771 sr.wrong_if = c->mfc_un.res.wrong_if;
1772 rcu_read_unlock();
1773
1774 if (copy_to_user(arg, &sr, sizeof(sr)))
1775 return -EFAULT;
1776 return 0;
1777 }
1778 rcu_read_unlock();
1779 return -EADDRNOTAVAIL;
1780 default:
1781 return -ENOIOCTLCMD;
1782 }
1783}
1784#endif
1785
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1787{
Jiri Pirko351638e2013-05-28 01:30:21 +00001788 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001789 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001790 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 struct vif_device *v;
1792 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 if (event != NETDEV_UNREGISTER)
1795 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001796
1797 ipmr_for_each_table(mrt, net) {
1798 v = &mrt->vif_table[0];
1799 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1800 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001801 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001802 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 }
1804 return NOTIFY_DONE;
1805}
1806
Jianjun Kongc354e122008-11-03 00:28:02 -08001807static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 .notifier_call = ipmr_device_event,
1809};
1810
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001811/* Encapsulate a packet by attaching a valid IPIP header to it.
1812 * This avoids tunnel drivers and other mess and gives us the speed so
1813 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001815static void ip_encap(struct net *net, struct sk_buff *skb,
1816 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001818 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001819 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001820
1821 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001822 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001823 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001824 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001826 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001827 iph->tos = old_iph->tos;
1828 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 iph->frag_off = 0;
1830 iph->daddr = daddr;
1831 iph->saddr = saddr;
1832 iph->protocol = IPPROTO_IPIP;
1833 iph->ihl = 5;
1834 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001835 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 ip_send_check(iph);
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1839 nf_reset(skb);
1840}
1841
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001842static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1843 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001845 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846
David S. Miller73186df2015-11-03 13:41:45 -05001847 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1848 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
1850 if (unlikely(opt->optlen))
1851 ip_forward_options(skb);
1852
Eric W. Biederman13206b62015-10-07 16:48:35 -05001853 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854}
1855
Yotam Gigia5bc9292017-10-03 09:58:08 +02001856#ifdef CONFIG_NET_SWITCHDEV
1857static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1858 int in_vifi, int out_vifi)
1859{
1860 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1861 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1862
1863 if (!skb->offload_mr_fwd_mark)
1864 return false;
1865 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1866 return false;
1867 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1868 &in_vif->dev_parent_id);
1869}
1870#else
1871static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1872 int in_vifi, int out_vifi)
1873{
1874 return false;
1875}
1876#endif
1877
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001878/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Patrick McHardy0c122952010-04-13 05:03:22 +00001880static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
Yotam Gigia5bc9292017-10-03 09:58:08 +02001881 int in_vifi, struct sk_buff *skb,
1882 struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001884 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001885 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886 struct net_device *dev;
1887 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001888 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 int encap = 0;
1890
Ian Morris51456b22015-04-03 09:17:26 +01001891 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 goto out_free;
1893
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 if (vif->flags & VIFF_REGISTER) {
1895 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001896 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001897 vif->dev->stats.tx_bytes += skb->len;
1898 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001899 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001900 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Yotam Gigia5bc9292017-10-03 09:58:08 +02001903 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1904 goto out_free;
1905
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001906 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001907 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001908 vif->remote, vif->local,
1909 0, 0,
1910 IPPROTO_IPIP,
1911 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001912 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 goto out_free;
1914 encap = sizeof(struct iphdr);
1915 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001916 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001917 0, 0,
1918 IPPROTO_IPIP,
1919 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001920 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921 goto out_free;
1922 }
1923
Changli Gaod8d1f302010-06-10 23:31:35 -07001924 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Changli Gaod8d1f302010-06-10 23:31:35 -07001926 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001928 * allow to send ICMP, so that packets will disappear
1929 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 */
David S. Miller73186df2015-11-03 13:41:45 -05001931 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 ip_rt_put(rt);
1933 goto out_free;
1934 }
1935
Changli Gaod8d1f302010-06-10 23:31:35 -07001936 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937
1938 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001939 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 goto out_free;
1941 }
1942
1943 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001944 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Eric Dumazetadf30902009-06-02 05:19:30 +00001946 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001947 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001948 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001951 * What do we do with netfilter? -- RR
1952 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001954 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001956 vif->dev->stats.tx_packets++;
1957 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001960 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001962 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 * not only before forwarding, but after forwarding on all output
1964 * interfaces. It is clear, if mrouter runs a multicasting
1965 * program, it should receive packets not depending to what interface
1966 * program is joined.
1967 * If we will not make it, the program will have to join on all
1968 * interfaces. On the other hand, multihoming host (or router, but
1969 * not mrouter) cannot join to more than one interface - it will
1970 * result in receiving multiple packets.
1971 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001972 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1973 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 ipmr_forward_finish);
1975 return;
1976
1977out_free:
1978 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979}
1980
Patrick McHardy0c122952010-04-13 05:03:22 +00001981static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982{
1983 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001984
1985 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1986 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 break;
1988 }
1989 return ct;
1990}
1991
1992/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001993static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001994 struct net_device *dev, struct sk_buff *skb,
1995 struct mfc_cache *cache, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001997 int true_vifi = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 int psend = -1;
1999 int vif, ct;
2000
2001 vif = cache->mfc_parent;
2002 cache->mfc_un.res.pkt++;
2003 cache->mfc_un.res.bytes += skb->len;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002004 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002006 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002007 struct mfc_cache *cache_proxy;
2008
2009 /* For an (*,G) entry, we only check that the incomming
2010 * interface is part of the static tree.
2011 */
2012 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
2013 if (cache_proxy &&
2014 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
2015 goto forward;
2016 }
2017
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002018 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002019 if (mrt->vif_table[vif].dev != dev) {
David S. Millerc7537962010-11-11 17:07:48 -08002020 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002022 * Very complicated situation...
2023 *
2024 * The best workaround until routing daemons will be
2025 * fixed is not to redistribute packet, if it was
2026 * send through wrong interface. It means, that
2027 * multicast applications WILL NOT work for
2028 * (S,G), which have default multicast route pointing
2029 * to wrong oif. In any case, it is not a good
2030 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 */
2032 goto dont_forward;
2033 }
2034
2035 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Patrick McHardy0c122952010-04-13 05:03:22 +00002037 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002039 * so that we cannot check that packet arrived on an oif.
2040 * It is bad, but otherwise we would need to move pretty
2041 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 */
Patrick McHardy0c122952010-04-13 05:03:22 +00002043 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00002044 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002045 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
2047 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00002048 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 }
2050 goto dont_forward;
2051 }
2052
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002053forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00002054 mrt->vif_table[vif].pkt_in++;
2055 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002057 /* Forward the frame */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002058 if (cache->mfc_origin == htonl(INADDR_ANY) &&
2059 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002060 if (true_vifi >= 0 &&
2061 true_vifi != cache->mfc_parent &&
2062 ip_hdr(skb)->ttl >
2063 cache->mfc_un.res.ttls[cache->mfc_parent]) {
2064 /* It's an (*,*) entry and the packet is not coming from
2065 * the upstream: forward the packet to the upstream
2066 * only.
2067 */
2068 psend = cache->mfc_parent;
2069 goto last_forward;
2070 }
2071 goto dont_forward;
2072 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002073 for (ct = cache->mfc_un.res.maxvif - 1;
2074 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002075 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002076 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
2077 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002078 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if (psend != -1) {
2080 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002081
Linus Torvalds1da177e2005-04-16 15:20:36 -07002082 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002083 ipmr_queue_xmit(net, mrt, true_vifi,
2084 skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002086 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
2088 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002089last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 if (psend != -1) {
2091 if (local) {
2092 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002093
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002095 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
2096 cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 } else {
Yotam Gigia5bc9292017-10-03 09:58:08 +02002098 ipmr_queue_xmit(net, mrt, true_vifi, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03002099 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 }
2101 }
2102
2103dont_forward:
2104 if (!local)
2105 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106}
2107
David S. Miller417da662011-05-03 19:42:43 -07002108static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08002109{
David S. Miller417da662011-05-03 19:42:43 -07002110 struct rtable *rt = skb_rtable(skb);
2111 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05002112 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07002113 .daddr = iph->daddr,
2114 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00002115 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07002116 .flowi4_oif = (rt_is_output_route(rt) ?
2117 skb->dev->ifindex : 0),
2118 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002119 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07002120 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00002121 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08002122 };
2123 struct mr_table *mrt;
2124 int err;
2125
David S. Millerda919812011-03-12 02:04:50 -05002126 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002127 if (err)
2128 return ERR_PTR(err);
2129 return mrt;
2130}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002132/* Multicast packets for forwarding arrive here
2133 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135int ip_mr_input(struct sk_buff *skb)
2136{
2137 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002138 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00002139 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002140 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002141 struct net_device *dev;
2142
2143 /* skb->dev passed in is the loX master dev for vrfs.
2144 * As there are no vifs associated with loopback devices,
2145 * get the proper interface that does have a vif associated with it.
2146 */
2147 dev = skb->dev;
2148 if (netif_is_l3_master(skb->dev)) {
2149 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2150 if (!dev) {
2151 kfree_skb(skb);
2152 return -ENODEV;
2153 }
2154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002157 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00002159 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 goto dont_forward;
2161
David S. Miller417da662011-05-03 19:42:43 -07002162 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002163 if (IS_ERR(mrt)) {
2164 kfree_skb(skb);
2165 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002166 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00002168 if (IPCB(skb)->opt.router_alert) {
2169 if (ip_call_ra_chain(skb))
2170 return 0;
2171 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2172 /* IGMPv1 (and broken IGMPv2 implementations sort of
2173 * Cisco IOS <= 11.2(8)) do not put router alert
2174 * option to IGMP packets destined to routable
2175 * groups. It is very bad, because it means
2176 * that we can forward NO IGMP messages.
2177 */
2178 struct sock *mroute_sk;
2179
2180 mroute_sk = rcu_dereference(mrt->mroute_sk);
2181 if (mroute_sk) {
2182 nf_reset(skb);
2183 raw_rcv(mroute_sk, skb);
2184 return 0;
2185 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186 }
2187 }
2188
Eric Dumazeta8c94862010-10-01 16:15:08 +00002189 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002190 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002191 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002192 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002193
2194 if (vif >= 0)
2195 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2196 vif);
2197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002199 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002200 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 int vif;
2202
2203 if (local) {
2204 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2205 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002206 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 skb = skb2;
2209 }
2210
Eric Dumazeta8c94862010-10-01 16:15:08 +00002211 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002212 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 if (vif >= 0) {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002214 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 read_unlock(&mrt_lock);
2216
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002217 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 }
2219 read_unlock(&mrt_lock);
2220 kfree_skb(skb);
2221 return -ENODEV;
2222 }
2223
Eric Dumazeta8c94862010-10-01 16:15:08 +00002224 read_lock(&mrt_lock);
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002225 ip_mr_forward(net, mrt, dev, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226 read_unlock(&mrt_lock);
2227
2228 if (local)
2229 return ip_local_deliver(skb);
2230
2231 return 0;
2232
2233dont_forward:
2234 if (local)
2235 return ip_local_deliver(skb);
2236 kfree_skb(skb);
2237 return 0;
2238}
2239
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002240#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002241/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002242int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002243{
2244 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002245 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002246 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002247
2248 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2249 goto drop;
2250
2251 pim = igmp_hdr(skb);
2252
David S. Miller417da662011-05-03 19:42:43 -07002253 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002254 if (IS_ERR(mrt))
2255 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002256 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002257 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2258 goto drop;
2259
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002260 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002261drop:
2262 kfree_skb(skb);
2263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 return 0;
2265}
2266#endif
2267
2268#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002269static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270{
2271 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002272 struct net *net = dev_net(skb->dev);
2273 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002275 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 goto drop;
2277
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002278 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002279 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002280 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002281 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002282 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 goto drop;
2284
David S. Miller417da662011-05-03 19:42:43 -07002285 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002286 if (IS_ERR(mrt))
2287 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002288 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002289drop:
2290 kfree_skb(skb);
2291 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 return 0;
2293}
2294#endif
2295
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002296static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2297 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298{
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002299 struct rta_mfc_stats mfcs;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002300 struct nlattr *mp_attr;
2301 struct rtnexthop *nhp;
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002302 unsigned long lastuse;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002303 int ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304
Nicolas Dichtel74381892010-03-25 23:45:35 +00002305 /* If cache is unresolved, don't try to parse IIF and OIF */
Nikolay Aleksandrov1708ebc2017-01-03 12:13:39 +01002306 if (c->mfc_parent >= MAXVIFS) {
2307 rtm->rtm_flags |= RTNH_F_UNRESOLVED;
Nicolas Dichtel74381892010-03-25 23:45:35 +00002308 return -ENOENT;
Nikolay Aleksandrov1708ebc2017-01-03 12:13:39 +01002309 }
Nicolas Dichtel74381892010-03-25 23:45:35 +00002310
Thomas Graf92a395e2012-06-26 23:36:13 +00002311 if (VIF_EXISTS(mrt, c->mfc_parent) &&
Yuval Mintz6853f212018-02-28 23:29:29 +02002312 nla_put_u32(skb, RTA_IIF,
2313 mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
Thomas Graf92a395e2012-06-26 23:36:13 +00002314 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Yotam Gigic7c0bbe2017-09-27 08:23:15 +02002316 if (c->mfc_flags & MFC_OFFLOAD)
2317 rtm->rtm_flags |= RTNH_F_OFFLOAD;
2318
Thomas Graf92a395e2012-06-26 23:36:13 +00002319 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2320 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
2322 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002323 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Yuval Mintz6853f212018-02-28 23:29:29 +02002324 struct vif_device *vif;
2325
Thomas Graf92a395e2012-06-26 23:36:13 +00002326 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2327 nla_nest_cancel(skb, mp_attr);
2328 return -EMSGSIZE;
2329 }
2330
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 nhp->rtnh_flags = 0;
2332 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Yuval Mintz6853f212018-02-28 23:29:29 +02002333 vif = &mrt->vif_table[ct];
2334 nhp->rtnh_ifindex = vif->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 nhp->rtnh_len = sizeof(*nhp);
2336 }
2337 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002338
2339 nla_nest_end(skb, mp_attr);
2340
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002341 lastuse = READ_ONCE(c->mfc_un.res.lastuse);
2342 lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
2343
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002344 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2345 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2346 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002347 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002348 nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002349 RTA_PAD))
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002350 return -EMSGSIZE;
2351
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 rtm->rtm_type = RTN_MULTICAST;
2353 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354}
2355
David S. Miller9a1b9492011-05-04 12:18:54 -07002356int ipmr_get_route(struct net *net, struct sk_buff *skb,
2357 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002358 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002361 struct mr_table *mrt;
2362 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002364 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002365 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002366 return -ENOENT;
2367
Eric Dumazeta8c94862010-10-01 16:15:08 +00002368 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002369 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002370 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002371 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002373 if (vif >= 0)
2374 cache = ipmr_cache_find_any(mrt, daddr, vif);
2375 }
Ian Morris51456b22015-04-03 09:17:26 +01002376 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002377 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002378 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002380 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002383 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002384 if (dev)
2385 vif = ipmr_find_vif(mrt, dev);
2386 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002388 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 return -ENODEV;
2390 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002391 skb2 = skb_clone(skb, GFP_ATOMIC);
2392 if (!skb2) {
2393 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002394 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002395 return -ENOMEM;
2396 }
2397
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002398 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002399 skb_push(skb2, sizeof(struct iphdr));
2400 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002401 iph = ip_hdr(skb2);
2402 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002403 iph->saddr = saddr;
2404 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002405 iph->version = 0;
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002406 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002407 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002408 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 return err;
2410 }
2411
Eric Dumazeta8c94862010-10-01 16:15:08 +00002412 read_lock(&mrt_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002413 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002414 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002415 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 return err;
2417}
2418
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002419static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002420 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2421 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002422{
2423 struct nlmsghdr *nlh;
2424 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002425 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002426
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002427 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002428 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002429 return -EMSGSIZE;
2430
2431 rtm = nlmsg_data(nlh);
2432 rtm->rtm_family = RTNL_FAMILY_IPMR;
2433 rtm->rtm_dst_len = 32;
2434 rtm->rtm_src_len = 32;
2435 rtm->rtm_tos = 0;
2436 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002437 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2438 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002439 rtm->rtm_type = RTN_MULTICAST;
2440 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002441 if (c->mfc_flags & MFC_STATIC)
2442 rtm->rtm_protocol = RTPROT_STATIC;
2443 else
2444 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002445 rtm->rtm_flags = 0;
2446
Jiri Benc930345e2015-03-29 16:59:25 +02002447 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2448 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002449 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002450 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2451 /* do not break the dump if cache is unresolved */
2452 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002453 goto nla_put_failure;
2454
Johannes Berg053c0952015-01-16 22:09:00 +01002455 nlmsg_end(skb, nlh);
2456 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002457
2458nla_put_failure:
2459 nlmsg_cancel(skb, nlh);
2460 return -EMSGSIZE;
2461}
2462
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002463static size_t mroute_msgsize(bool unresolved, int maxvif)
2464{
2465 size_t len =
2466 NLMSG_ALIGN(sizeof(struct rtmsg))
2467 + nla_total_size(4) /* RTA_TABLE */
2468 + nla_total_size(4) /* RTA_SRC */
2469 + nla_total_size(4) /* RTA_DST */
2470 ;
2471
2472 if (!unresolved)
2473 len = len
2474 + nla_total_size(4) /* RTA_IIF */
2475 + nla_total_size(0) /* RTA_MULTIPATH */
2476 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2477 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002478 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002479 ;
2480
2481 return len;
2482}
2483
2484static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2485 int cmd)
2486{
2487 struct net *net = read_pnet(&mrt->net);
2488 struct sk_buff *skb;
2489 int err = -ENOBUFS;
2490
2491 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2492 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002493 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002494 goto errout;
2495
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002496 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002497 if (err < 0)
2498 goto errout;
2499
2500 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2501 return;
2502
2503errout:
2504 kfree_skb(skb);
2505 if (err < 0)
2506 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2507}
2508
Julien Gomes5a645dd2017-06-20 13:54:17 -07002509static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2510{
2511 size_t len =
2512 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2513 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2514 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2515 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2516 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2517 /* IPMRA_CREPORT_PKT */
2518 + nla_total_size(payloadlen)
2519 ;
2520
2521 return len;
2522}
2523
2524static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2525{
2526 struct net *net = read_pnet(&mrt->net);
2527 struct nlmsghdr *nlh;
2528 struct rtgenmsg *rtgenm;
2529 struct igmpmsg *msg;
2530 struct sk_buff *skb;
2531 struct nlattr *nla;
2532 int payloadlen;
2533
2534 payloadlen = pkt->len - sizeof(struct igmpmsg);
2535 msg = (struct igmpmsg *)skb_network_header(pkt);
2536
2537 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2538 if (!skb)
2539 goto errout;
2540
2541 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2542 sizeof(struct rtgenmsg), 0);
2543 if (!nlh)
2544 goto errout;
2545 rtgenm = nlmsg_data(nlh);
2546 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2547 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2548 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2549 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2550 msg->im_src.s_addr) ||
2551 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2552 msg->im_dst.s_addr))
2553 goto nla_put_failure;
2554
2555 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2556 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2557 nla_data(nla), payloadlen))
2558 goto nla_put_failure;
2559
2560 nlmsg_end(skb, nlh);
2561
2562 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2563 return;
2564
2565nla_put_failure:
2566 nlmsg_cancel(skb, nlh);
2567errout:
2568 kfree_skb(skb);
2569 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2570}
2571
Donald Sharp4f75ba62017-06-28 13:58:57 -04002572static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2573 struct netlink_ext_ack *extack)
2574{
2575 struct net *net = sock_net(in_skb->sk);
2576 struct nlattr *tb[RTA_MAX + 1];
2577 struct sk_buff *skb = NULL;
2578 struct mfc_cache *cache;
2579 struct mr_table *mrt;
2580 struct rtmsg *rtm;
2581 __be32 src, grp;
2582 u32 tableid;
2583 int err;
2584
2585 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2586 rtm_ipv4_policy, extack);
2587 if (err < 0)
2588 goto errout;
2589
2590 rtm = nlmsg_data(nlh);
2591
2592 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2593 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2594 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2595
2596 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
Dan Carpenter2e3d2322017-07-12 10:56:47 +03002597 if (!mrt) {
2598 err = -ENOENT;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002599 goto errout_free;
2600 }
2601
2602 /* entries are added/deleted only under RTNL */
2603 rcu_read_lock();
2604 cache = ipmr_cache_find(mrt, src, grp);
2605 rcu_read_unlock();
2606 if (!cache) {
2607 err = -ENOENT;
2608 goto errout_free;
2609 }
2610
2611 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2612 if (!skb) {
2613 err = -ENOBUFS;
2614 goto errout_free;
2615 }
2616
2617 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2618 nlh->nlmsg_seq, cache,
2619 RTM_NEWROUTE, 0);
2620 if (err < 0)
2621 goto errout_free;
2622
2623 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2624
2625errout:
2626 return err;
2627
2628errout_free:
2629 kfree_skb(skb);
2630 goto errout;
2631}
2632
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002633static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2634{
2635 struct net *net = sock_net(skb->sk);
2636 struct mr_table *mrt;
2637 struct mfc_cache *mfc;
2638 unsigned int t = 0, s_t;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002639 unsigned int e = 0, s_e;
2640
2641 s_t = cb->args[0];
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002642 s_e = cb->args[1];
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002643
Eric Dumazeta8c94862010-10-01 16:15:08 +00002644 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002645 ipmr_for_each_table(mrt, net) {
2646 if (t < s_t)
2647 goto next_table;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002648 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) {
2649 if (e < s_e)
2650 goto next_entry;
2651 if (ipmr_fill_mroute(mrt, skb,
2652 NETLINK_CB(cb->skb).portid,
2653 cb->nlh->nlmsg_seq,
2654 mfc, RTM_NEWROUTE,
2655 NLM_F_MULTI) < 0)
2656 goto done;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002657next_entry:
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002658 e++;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002659 }
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002660 e = 0;
2661 s_e = 0;
2662
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002663 spin_lock_bh(&mfc_unres_lock);
2664 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2665 if (e < s_e)
2666 goto next_entry2;
2667 if (ipmr_fill_mroute(mrt, skb,
2668 NETLINK_CB(cb->skb).portid,
2669 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002670 mfc, RTM_NEWROUTE,
2671 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002672 spin_unlock_bh(&mfc_unres_lock);
2673 goto done;
2674 }
2675next_entry2:
2676 e++;
2677 }
2678 spin_unlock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002679 e = 0;
2680 s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002681next_table:
2682 t++;
2683 }
2684done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002685 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002686
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002687 cb->args[1] = e;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002688 cb->args[0] = t;
2689
2690 return skb->len;
2691}
2692
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002693static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2694 [RTA_SRC] = { .type = NLA_U32 },
2695 [RTA_DST] = { .type = NLA_U32 },
2696 [RTA_IIF] = { .type = NLA_U32 },
2697 [RTA_TABLE] = { .type = NLA_U32 },
2698 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2699};
2700
2701static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2702{
2703 switch (rtm_protocol) {
2704 case RTPROT_STATIC:
2705 case RTPROT_MROUTED:
2706 return true;
2707 }
2708 return false;
2709}
2710
2711static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2712{
2713 struct rtnexthop *rtnh = nla_data(nla);
2714 int remaining = nla_len(nla), vifi = 0;
2715
2716 while (rtnh_ok(rtnh, remaining)) {
2717 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2718 if (++vifi == MAXVIFS)
2719 break;
2720 rtnh = rtnh_next(rtnh, &remaining);
2721 }
2722
2723 return remaining > 0 ? -EINVAL : vifi;
2724}
2725
2726/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2727static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2728 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002729 struct mr_table **mrtret,
2730 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002731{
2732 struct net_device *dev = NULL;
2733 u32 tblid = RT_TABLE_DEFAULT;
2734 struct mr_table *mrt;
2735 struct nlattr *attr;
2736 struct rtmsg *rtm;
2737 int ret, rem;
2738
Johannes Bergfceb6432017-04-12 14:34:07 +02002739 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07002740 extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002741 if (ret < 0)
2742 goto out;
2743 rtm = nlmsg_data(nlh);
2744
2745 ret = -EINVAL;
2746 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2747 rtm->rtm_type != RTN_MULTICAST ||
2748 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2749 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2750 goto out;
2751
2752 memset(mfcc, 0, sizeof(*mfcc));
2753 mfcc->mfcc_parent = -1;
2754 ret = 0;
2755 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2756 switch (nla_type(attr)) {
2757 case RTA_SRC:
2758 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2759 break;
2760 case RTA_DST:
2761 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2762 break;
2763 case RTA_IIF:
2764 dev = __dev_get_by_index(net, nla_get_u32(attr));
2765 if (!dev) {
2766 ret = -ENODEV;
2767 goto out;
2768 }
2769 break;
2770 case RTA_MULTIPATH:
2771 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2772 ret = -EINVAL;
2773 goto out;
2774 }
2775 break;
2776 case RTA_PREFSRC:
2777 ret = 1;
2778 break;
2779 case RTA_TABLE:
2780 tblid = nla_get_u32(attr);
2781 break;
2782 }
2783 }
2784 mrt = ipmr_get_table(net, tblid);
2785 if (!mrt) {
2786 ret = -ENOENT;
2787 goto out;
2788 }
2789 *mrtret = mrt;
2790 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2791 if (dev)
2792 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2793
2794out:
2795 return ret;
2796}
2797
2798/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002799static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2800 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002801{
2802 struct net *net = sock_net(skb->sk);
2803 int ret, mrtsock, parent;
2804 struct mr_table *tbl;
2805 struct mfcctl mfcc;
2806
2807 mrtsock = 0;
2808 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002809 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002810 if (ret < 0)
2811 return ret;
2812
2813 parent = ret ? mfcc.mfcc_parent : -1;
2814 if (nlh->nlmsg_type == RTM_NEWROUTE)
2815 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2816 else
2817 return ipmr_mfc_delete(tbl, &mfcc, parent);
2818}
2819
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002820static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2821{
2822 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2823
2824 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2825 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2826 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2827 mrt->mroute_reg_vif_num) ||
2828 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2829 mrt->mroute_do_assert) ||
2830 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim))
2831 return false;
2832
2833 return true;
2834}
2835
2836static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2837{
2838 struct nlattr *vif_nest;
2839 struct vif_device *vif;
2840
2841 /* if the VIF doesn't exist just continue */
2842 if (!VIF_EXISTS(mrt, vifid))
2843 return true;
2844
2845 vif = &mrt->vif_table[vifid];
2846 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2847 if (!vif_nest)
2848 return false;
2849 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2850 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2851 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2852 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2853 IPMRA_VIFA_PAD) ||
2854 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2855 IPMRA_VIFA_PAD) ||
2856 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2857 IPMRA_VIFA_PAD) ||
2858 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2859 IPMRA_VIFA_PAD) ||
2860 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2861 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2862 nla_nest_cancel(skb, vif_nest);
2863 return false;
2864 }
2865 nla_nest_end(skb, vif_nest);
2866
2867 return true;
2868}
2869
2870static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2871{
2872 struct net *net = sock_net(skb->sk);
2873 struct nlmsghdr *nlh = NULL;
2874 unsigned int t = 0, s_t;
2875 unsigned int e = 0, s_e;
2876 struct mr_table *mrt;
2877
2878 s_t = cb->args[0];
2879 s_e = cb->args[1];
2880
2881 ipmr_for_each_table(mrt, net) {
2882 struct nlattr *vifs, *af;
2883 struct ifinfomsg *hdr;
2884 u32 i;
2885
2886 if (t < s_t)
2887 goto skip_table;
2888 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2889 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2890 sizeof(*hdr), NLM_F_MULTI);
2891 if (!nlh)
2892 break;
2893
2894 hdr = nlmsg_data(nlh);
2895 memset(hdr, 0, sizeof(*hdr));
2896 hdr->ifi_family = RTNL_FAMILY_IPMR;
2897
2898 af = nla_nest_start(skb, IFLA_AF_SPEC);
2899 if (!af) {
2900 nlmsg_cancel(skb, nlh);
2901 goto out;
2902 }
2903
2904 if (!ipmr_fill_table(mrt, skb)) {
2905 nlmsg_cancel(skb, nlh);
2906 goto out;
2907 }
2908
2909 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2910 if (!vifs) {
2911 nla_nest_end(skb, af);
2912 nlmsg_end(skb, nlh);
2913 goto out;
2914 }
2915 for (i = 0; i < mrt->maxvif; i++) {
2916 if (e < s_e)
2917 goto skip_entry;
2918 if (!ipmr_fill_vif(mrt, i, skb)) {
2919 nla_nest_end(skb, vifs);
2920 nla_nest_end(skb, af);
2921 nlmsg_end(skb, nlh);
2922 goto out;
2923 }
2924skip_entry:
2925 e++;
2926 }
2927 s_e = 0;
2928 e = 0;
2929 nla_nest_end(skb, vifs);
2930 nla_nest_end(skb, af);
2931 nlmsg_end(skb, nlh);
2932skip_table:
2933 t++;
2934 }
2935
2936out:
2937 cb->args[1] = e;
2938 cb->args[0] = t;
2939
2940 return skb->len;
2941}
2942
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002943#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002944/* The /proc interfaces to multicast routing :
2945 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002946 */
2947struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002948 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002949 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 int ct;
2951};
2952
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002953static struct vif_device *ipmr_vif_seq_idx(struct net *net,
Yuval Mintz6853f212018-02-28 23:29:29 +02002954 struct ipmr_vif_iter *iter,
2955 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002957 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002958
2959 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2960 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002962 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002963 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
2965 return NULL;
2966}
2967
2968static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002969 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002971 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002972 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002973 struct mr_table *mrt;
2974
2975 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002976 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002977 return ERR_PTR(-ENOENT);
2978
2979 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002980
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002982 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002983 : SEQ_START_TOKEN;
2984}
2985
2986static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2987{
2988 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002989 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002990 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991
2992 ++*pos;
2993 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002994 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002995
Patrick McHardy0c122952010-04-13 05:03:22 +00002996 while (++iter->ct < mrt->maxvif) {
2997 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002999 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003000 }
3001 return NULL;
3002}
3003
3004static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08003005 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003006{
3007 read_unlock(&mrt_lock);
3008}
3009
3010static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
3011{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003012 struct ipmr_vif_iter *iter = seq->private;
3013 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003014
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003016 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003017 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
3018 } else {
3019 const struct vif_device *vif = v;
Yuval Mintz6853f212018-02-28 23:29:29 +02003020 const char *name = vif->dev ?
3021 vif->dev->name : "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022
3023 seq_printf(seq,
James Hogan91e6dd82018-01-30 09:48:02 +00003024 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00003025 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003026 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003027 vif->bytes_out, vif->pkt_out,
3028 vif->flags, vif->local, vif->remote);
3029 }
3030 return 0;
3031}
3032
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003033static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003034 .start = ipmr_vif_seq_start,
3035 .next = ipmr_vif_seq_next,
3036 .stop = ipmr_vif_seq_stop,
3037 .show = ipmr_vif_seq_show,
3038};
3039
3040static int ipmr_vif_open(struct inode *inode, struct file *file)
3041{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003042 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
3043 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003044}
3045
Arjan van de Ven9a321442007-02-12 00:55:35 -08003046static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 .open = ipmr_vif_open,
3048 .read = seq_read,
3049 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003050 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003051};
3052
3053struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003054 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003055 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00003056 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057};
3058
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003059static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
3060 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003062 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063 struct mfc_cache *mfc;
3064
Eric Dumazeta8c94862010-10-01 16:15:08 +00003065 rcu_read_lock();
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003066 it->cache = &mrt->mfc_cache_list;
3067 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
3068 if (pos-- == 0)
3069 return mfc;
Eric Dumazeta8c94862010-10-01 16:15:08 +00003070 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00003073 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00003074 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00003075 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 return mfc;
3077 spin_unlock_bh(&mfc_unres_lock);
3078
3079 it->cache = NULL;
3080 return NULL;
3081}
3082
3083
3084static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
3085{
3086 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003087 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003088 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003089
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003090 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01003091 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003092 return ERR_PTR(-ENOENT);
3093
3094 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 it->cache = NULL;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003096 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 : SEQ_START_TOKEN;
3098}
3099
3100static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3101{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003103 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003104 struct mr_table *mrt = it->mrt;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003105 struct mfc_cache *mfc = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106
3107 ++*pos;
3108
3109 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003110 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111
Patrick McHardy862465f2010-04-13 05:03:21 +00003112 if (mfc->list.next != it->cache)
3113 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003114
Patrick McHardy0c122952010-04-13 05:03:22 +00003115 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 goto end_of_list;
3117
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00003119 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00003120 it->cache = &mrt->mfc_unres_queue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00003123 if (!list_empty(it->cache))
3124 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00003126end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 spin_unlock_bh(&mfc_unres_lock);
3128 it->cache = NULL;
3129
3130 return NULL;
3131}
3132
3133static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
3134{
3135 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003136 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003137
Patrick McHardy0c122952010-04-13 05:03:22 +00003138 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 spin_unlock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003140 else if (it->cache == &mrt->mfc_cache_list)
Eric Dumazeta8c94862010-10-01 16:15:08 +00003141 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003142}
3143
3144static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
3145{
3146 int n;
3147
3148 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003149 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
3151 } else {
3152 const struct mfc_cache *mfc = v;
3153 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003154 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003155
Eric Dumazet0eae88f2010-04-20 19:06:52 -07003156 seq_printf(seq, "%08X %08X %-3hd",
3157 (__force u32) mfc->mfc_mcastgrp,
3158 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003159 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160
Patrick McHardy0c122952010-04-13 05:03:22 +00003161 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003162 seq_printf(seq, " %8lu %8lu %8lu",
3163 mfc->mfc_un.res.pkt,
3164 mfc->mfc_un.res.bytes,
3165 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08003166 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00003167 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00003168 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00003169 mfc->mfc_un.res.ttls[n] < 255)
3170 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003171 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 n, mfc->mfc_un.res.ttls[n]);
3173 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003174 } else {
3175 /* unresolved mfc_caches don't contain
3176 * pkt, bytes and wrong_if values
3177 */
3178 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 }
3180 seq_putc(seq, '\n');
3181 }
3182 return 0;
3183}
3184
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003185static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 .start = ipmr_mfc_seq_start,
3187 .next = ipmr_mfc_seq_next,
3188 .stop = ipmr_mfc_seq_stop,
3189 .show = ipmr_mfc_seq_show,
3190};
3191
3192static int ipmr_mfc_open(struct inode *inode, struct file *file)
3193{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003194 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
3195 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
Arjan van de Ven9a321442007-02-12 00:55:35 -08003198static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 .open = ipmr_mfc_open,
3200 .read = seq_read,
3201 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003202 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003204#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003205
3206#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00003207static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003208 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07003209 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210};
3211#endif
3212
Yotam Gigi4d65b942017-09-27 08:23:13 +02003213static unsigned int ipmr_seq_read(struct net *net)
3214{
3215 ASSERT_RTNL();
3216
3217 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
3218}
3219
3220static int ipmr_dump(struct net *net, struct notifier_block *nb)
3221{
3222 struct mr_table *mrt;
3223 int err;
3224
3225 err = ipmr_rules_dump(net, nb);
3226 if (err)
3227 return err;
3228
3229 ipmr_for_each_table(mrt, net) {
3230 struct vif_device *v = &mrt->vif_table[0];
3231 struct mfc_cache *mfc;
3232 int vifi;
3233
3234 /* Notifiy on table VIF entries */
3235 read_lock(&mrt_lock);
3236 for (vifi = 0; vifi < mrt->maxvif; vifi++, v++) {
3237 if (!v->dev)
3238 continue;
3239
3240 call_ipmr_vif_entry_notifier(nb, net, FIB_EVENT_VIF_ADD,
3241 v, vifi, mrt->id);
3242 }
3243 read_unlock(&mrt_lock);
3244
3245 /* Notify on table MFC entries */
3246 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
3247 call_ipmr_mfc_entry_notifier(nb, net,
3248 FIB_EVENT_ENTRY_ADD, mfc,
3249 mrt->id);
3250 }
3251
3252 return 0;
3253}
3254
3255static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3256 .family = RTNL_FAMILY_IPMR,
3257 .fib_seq_read = ipmr_seq_read,
3258 .fib_dump = ipmr_dump,
3259 .owner = THIS_MODULE,
3260};
3261
Colin Ian Kingef739d82017-09-29 14:34:22 +01003262static int __net_init ipmr_notifier_init(struct net *net)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003263{
3264 struct fib_notifier_ops *ops;
3265
3266 net->ipv4.ipmr_seq = 0;
3267
3268 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3269 if (IS_ERR(ops))
3270 return PTR_ERR(ops);
3271 net->ipv4.ipmr_notifier_ops = ops;
3272
3273 return 0;
3274}
3275
3276static void __net_exit ipmr_notifier_exit(struct net *net)
3277{
3278 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3279 net->ipv4.ipmr_notifier_ops = NULL;
3280}
3281
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01003282/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00003283static int __net_init ipmr_net_init(struct net *net)
3284{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003285 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00003286
Yotam Gigi4d65b942017-09-27 08:23:13 +02003287 err = ipmr_notifier_init(net);
3288 if (err)
3289 goto ipmr_notifier_fail;
3290
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003291 err = ipmr_rules_init(net);
3292 if (err < 0)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003293 goto ipmr_rules_fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003294
3295#ifdef CONFIG_PROC_FS
3296 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00003297 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003298 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00003299 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003300 goto proc_cache_fail;
3301#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00003302 return 0;
3303
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003304#ifdef CONFIG_PROC_FS
3305proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00003306 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003307proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003308 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003309#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003310ipmr_rules_fail:
3311 ipmr_notifier_exit(net);
3312ipmr_notifier_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003313 return err;
3314}
3315
3316static void __net_exit ipmr_net_exit(struct net *net)
3317{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003318#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003319 remove_proc_entry("ip_mr_cache", net->proc_net);
3320 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003321#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003322 ipmr_notifier_exit(net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003323 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00003324}
3325
3326static struct pernet_operations ipmr_net_ops = {
3327 .init = ipmr_net_init,
3328 .exit = ipmr_net_exit,
Kirill Tkhaif84c6822018-02-13 12:29:52 +03003329 .async = true,
Benjamin Therycf958ae32009-01-22 04:56:16 +00003330};
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}