blob: 2a795d2c050294eba6fcbb09b0e864f3560fa3a1 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Linux Consultancy and Custom Driver Development
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fixes:
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
22 * overflow.
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
Gilles Espinassef77f13e2010-03-29 15:41:47 +020025 * Relax this requirement to work with older peers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 */
28
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080031#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/timer.h>
34#include <linux/mm.h>
35#include <linux/kernel.h>
36#include <linux/fcntl.h>
37#include <linux/stat.h>
38#include <linux/socket.h>
39#include <linux/in.h>
40#include <linux/inet.h>
41#include <linux/netdevice.h>
42#include <linux/inetdevice.h>
43#include <linux/igmp.h>
44#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
46#include <linux/mroute.h>
47#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080048#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020050#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/ip.h>
52#include <net/protocol.h>
53#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020054#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/icmp.h>
57#include <net/udp.h>
58#include <net/raw.h>
59#include <linux/notifier.h>
60#include <linux/if_arp.h>
61#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000062#include <linux/compat.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040063#include <linux/export.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000064#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070066#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000067#include <net/fib_rules.h>
Nicolas Dichteld67b8c62012-12-04 01:13:35 +000068#include <linux/netconf.h>
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +010069#include <net/nexthop.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Patrick McHardyf0ad0862010-04-13 05:03:23 +000071struct ipmr_rule {
72 struct fib_rule common;
73};
74
75struct ipmr_result {
76 struct mr_table *mrt;
77};
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000080 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static DEFINE_RWLOCK(mrt_lock);
84
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +010085/* Multicast router control variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Special spinlock for queue of unresolved entries */
88static DEFINE_SPINLOCK(mfc_unres_lock);
89
90/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000091 * entries is changed only in process context and protected
92 * with weak lock mrt_lock. Queue of unresolved entries is protected
93 * with strong spinlock mfc_unres_lock.
94 *
95 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
97
Christoph Lametere18b8902006-12-06 20:33:20 -080098static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000100static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000101static void ipmr_free_table(struct mr_table *mrt);
102
Rami Rosenc4854ec2013-07-20 15:09:28 +0300103static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -0400104 struct net_device *dev, struct sk_buff *skb,
105 struct mfc_cache *cache, int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000106static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000107 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200108static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
109 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000110static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
111 int cmd);
Julien Gomes5a645dd2017-06-20 13:54:17 -0700112static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100113static void mroute_clean_tables(struct mr_table *mrt, bool all);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000114static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000116#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
117#define ipmr_for_each_table(mrt, net) \
118 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
119
120static struct mr_table *ipmr_get_table(struct net *net, u32 id)
121{
122 struct mr_table *mrt;
123
124 ipmr_for_each_table(mrt, net) {
125 if (mrt->id == id)
126 return mrt;
127 }
128 return NULL;
129}
130
David S. Millerda919812011-03-12 02:04:50 -0500131static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000132 struct mr_table **mrt)
133{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000134 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100135 struct ipmr_result res;
136 struct fib_lookup_arg arg = {
137 .result = &res,
138 .flags = FIB_LOOKUP_NOREF,
139 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000140
David Aherne58e4152016-10-31 15:54:00 -0700141 /* update flow if oif or iif point to device enslaved to l3mdev */
142 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
143
David S. Millerda919812011-03-12 02:04:50 -0500144 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
145 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000146 if (err < 0)
147 return err;
148 *mrt = res.mrt;
149 return 0;
150}
151
152static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
153 int flags, struct fib_lookup_arg *arg)
154{
155 struct ipmr_result *res = arg->result;
156 struct mr_table *mrt;
157
158 switch (rule->action) {
159 case FR_ACT_TO_TBL:
160 break;
161 case FR_ACT_UNREACHABLE:
162 return -ENETUNREACH;
163 case FR_ACT_PROHIBIT:
164 return -EACCES;
165 case FR_ACT_BLACKHOLE:
166 default:
167 return -EINVAL;
168 }
169
David Aherne58e4152016-10-31 15:54:00 -0700170 arg->table = fib_rule_get_table(rule, arg);
171
172 mrt = ipmr_get_table(rule->fr_net, arg->table);
Ian Morris51456b22015-04-03 09:17:26 +0100173 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000174 return -EAGAIN;
175 res->mrt = mrt;
176 return 0;
177}
178
179static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
180{
181 return 1;
182}
183
184static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
185 FRA_GENERIC_POLICY,
186};
187
188static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
189 struct fib_rule_hdr *frh, struct nlattr **tb)
190{
191 return 0;
192}
193
194static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
195 struct nlattr **tb)
196{
197 return 1;
198}
199
200static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
201 struct fib_rule_hdr *frh)
202{
203 frh->dst_len = 0;
204 frh->src_len = 0;
205 frh->tos = 0;
206 return 0;
207}
208
Andi Kleen04a6f822012-10-04 17:12:11 -0700209static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200210 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000211 .rule_size = sizeof(struct ipmr_rule),
212 .addr_size = sizeof(u32),
213 .action = ipmr_rule_action,
214 .match = ipmr_rule_match,
215 .configure = ipmr_rule_configure,
216 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000217 .fill = ipmr_rule_fill,
218 .nlgroup = RTNLGRP_IPV4_RULE,
219 .policy = ipmr_rule_policy,
220 .owner = THIS_MODULE,
221};
222
223static int __net_init ipmr_rules_init(struct net *net)
224{
225 struct fib_rules_ops *ops;
226 struct mr_table *mrt;
227 int err;
228
229 ops = fib_rules_register(&ipmr_rules_ops_template, net);
230 if (IS_ERR(ops))
231 return PTR_ERR(ops);
232
233 INIT_LIST_HEAD(&net->ipv4.mr_tables);
234
235 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100236 if (IS_ERR(mrt)) {
237 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000238 goto err1;
239 }
240
241 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
242 if (err < 0)
243 goto err2;
244
245 net->ipv4.mr_rules_ops = ops;
246 return 0;
247
248err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700249 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000250err1:
251 fib_rules_unregister(ops);
252 return err;
253}
254
255static void __net_exit ipmr_rules_exit(struct net *net)
256{
257 struct mr_table *mrt, *next;
258
WANG Conged785302015-03-31 11:01:45 -0700259 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000260 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
261 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000262 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000263 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000264 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700265 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000266}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200267
268static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
269{
270 return fib_rules_dump(net, nb, RTNL_FAMILY_IPMR);
271}
272
273static unsigned int ipmr_rules_seq_read(struct net *net)
274{
275 return fib_rules_seq_read(net, RTNL_FAMILY_IPMR);
276}
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000277#else
278#define ipmr_for_each_table(mrt, net) \
279 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
280
281static struct mr_table *ipmr_get_table(struct net *net, u32 id)
282{
283 return net->ipv4.mrt;
284}
285
David S. Millerda919812011-03-12 02:04:50 -0500286static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000287 struct mr_table **mrt)
288{
289 *mrt = net->ipv4.mrt;
290 return 0;
291}
292
293static int __net_init ipmr_rules_init(struct net *net)
294{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100295 struct mr_table *mrt;
296
297 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
298 if (IS_ERR(mrt))
299 return PTR_ERR(mrt);
300 net->ipv4.mrt = mrt;
301 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000302}
303
304static void __net_exit ipmr_rules_exit(struct net *net)
305{
WANG Conged785302015-03-31 11:01:45 -0700306 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000307 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700308 net->ipv4.mrt = NULL;
309 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000310}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200311
312static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
313{
314 return 0;
315}
316
317static unsigned int ipmr_rules_seq_read(struct net *net)
318{
319 return 0;
320}
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000321#endif
322
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100323static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
324 const void *ptr)
325{
326 const struct mfc_cache_cmp_arg *cmparg = arg->key;
327 struct mfc_cache *c = (struct mfc_cache *)ptr;
328
329 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
330 cmparg->mfc_origin != c->mfc_origin;
331}
332
333static const struct rhashtable_params ipmr_rht_params = {
334 .head_offset = offsetof(struct mfc_cache, mnode),
335 .key_offset = offsetof(struct mfc_cache, cmparg),
336 .key_len = sizeof(struct mfc_cache_cmp_arg),
337 .nelem_hint = 3,
338 .locks_mul = 1,
339 .obj_cmpfn = ipmr_hash_cmp,
340 .automatic_shrinking = true,
341};
342
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000343static struct mr_table *ipmr_new_table(struct net *net, u32 id)
344{
345 struct mr_table *mrt;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000346
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100347 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
348 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
349 return ERR_PTR(-EINVAL);
350
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000351 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100352 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000353 return mrt;
354
355 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100356 if (!mrt)
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100357 return ERR_PTR(-ENOMEM);
Patrick McHardy8de53df2010-04-15 13:29:28 +0200358 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000359 mrt->id = id;
360
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100361 rhltable_init(&mrt->mfc_hash, &ipmr_rht_params);
362 INIT_LIST_HEAD(&mrt->mfc_cache_list);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000363 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
364
365 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
366 (unsigned long)mrt);
367
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000368 mrt->mroute_reg_vif_num = -1;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000369#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
370 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
371#endif
372 return mrt;
373}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000375static void ipmr_free_table(struct mr_table *mrt)
376{
377 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100378 mroute_clean_tables(mrt, true);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100379 rhltable_destroy(&mrt->mfc_hash);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000380 kfree(mrt);
381}
382
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
384
Wang Chend6070322008-07-14 20:55:26 -0700385static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
386{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000387 struct net *net = dev_net(dev);
388
Wang Chend6070322008-07-14 20:55:26 -0700389 dev_close(dev);
390
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000391 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700392 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800393 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700394 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700395 struct ip_tunnel_parm p;
396
397 memset(&p, 0, sizeof(p));
398 p.iph.daddr = v->vifc_rmt_addr.s_addr;
399 p.iph.saddr = v->vifc_lcl_addr.s_addr;
400 p.iph.version = 4;
401 p.iph.ihl = 5;
402 p.iph.protocol = IPPROTO_IPIP;
403 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
404 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
405
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800406 if (ops->ndo_do_ioctl) {
407 mm_segment_t oldfs = get_fs();
408
409 set_fs(KERNEL_DS);
410 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
411 set_fs(oldfs);
412 }
Wang Chend6070322008-07-14 20:55:26 -0700413 }
414}
415
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100416/* Initialize ipmr pimreg/tunnel in_device */
417static bool ipmr_init_vif_indev(const struct net_device *dev)
418{
419 struct in_device *in_dev;
420
421 ASSERT_RTNL();
422
423 in_dev = __in_dev_get_rtnl(dev);
424 if (!in_dev)
425 return false;
426 ipv4_devconf_setall(in_dev);
427 neigh_parms_data_state_setall(in_dev->arp_parms);
428 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
429
430 return true;
431}
432
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100433static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434{
435 struct net_device *dev;
436
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000437 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800440 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 int err;
442 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 memset(&p, 0, sizeof(p));
446 p.iph.daddr = v->vifc_rmt_addr.s_addr;
447 p.iph.saddr = v->vifc_lcl_addr.s_addr;
448 p.iph.version = 4;
449 p.iph.ihl = 5;
450 p.iph.protocol = IPPROTO_IPIP;
451 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800452 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800454 if (ops->ndo_do_ioctl) {
455 mm_segment_t oldfs = get_fs();
456
457 set_fs(KERNEL_DS);
458 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
459 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000460 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800461 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000462 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 dev = NULL;
464
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000465 if (err == 0 &&
466 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100468 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 if (dev_open(dev))
471 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700472 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474 }
475 return dev;
476
477failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 unregister_netdevice(dev);
479 return NULL;
480}
481
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100482#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000483static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000485 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000486 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500487 struct flowi4 fl4 = {
488 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700489 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500490 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000491 };
492 int err;
493
David S. Millerda919812011-03-12 02:04:50 -0500494 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000495 if (err < 0) {
496 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000497 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000498 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700501 dev->stats.tx_bytes += skb->len;
502 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000503 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 read_unlock(&mrt_lock);
505 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000506 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200509static int reg_vif_get_iflink(const struct net_device *dev)
510{
511 return 0;
512}
513
Stephen Hemminger007c3832008-11-20 20:28:35 -0800514static const struct net_device_ops reg_vif_netdev_ops = {
515 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200516 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800517};
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519static void reg_vif_setup(struct net_device *dev)
520{
521 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800522 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530524 dev->netdev_ops = &reg_vif_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400525 dev->needs_free_netdev = true;
Tom Goff403dbb92009-06-14 03:16:13 -0700526 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527}
528
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000529static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000532 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000534 if (mrt->id == RT_TABLE_DEFAULT)
535 sprintf(name, "pimreg");
536 else
537 sprintf(name, "pimreg%u", mrt->id);
538
Tom Gundersenc835a672014-07-14 16:37:24 +0200539 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
Ian Morris51456b22015-04-03 09:17:26 +0100541 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 return NULL;
543
Tom Goff403dbb92009-06-14 03:16:13 -0700544 dev_net_set(dev, net);
545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 if (register_netdevice(dev)) {
547 free_netdev(dev);
548 return NULL;
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100551 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 if (dev_open(dev))
554 goto failure;
555
Wang Chen7dc00c82008-07-14 20:56:34 -0700556 dev_hold(dev);
557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 return dev;
559
560failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 unregister_netdevice(dev);
562 return NULL;
563}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100564
565/* called with rcu_read_lock() */
566static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
567 unsigned int pimlen)
568{
569 struct net_device *reg_dev = NULL;
570 struct iphdr *encap;
571
572 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100573 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100574 * a. packet is really sent to a multicast group
575 * b. packet is not a NULL-REGISTER
576 * c. packet is not truncated
577 */
578 if (!ipv4_is_multicast(encap->daddr) ||
579 encap->tot_len == 0 ||
580 ntohs(encap->tot_len) + pimlen > skb->len)
581 return 1;
582
583 read_lock(&mrt_lock);
584 if (mrt->mroute_reg_vif_num >= 0)
585 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
586 read_unlock(&mrt_lock);
587
588 if (!reg_dev)
589 return 1;
590
591 skb->mac_header = skb->network_header;
592 skb_pull(skb, (u8 *)encap - skb->data);
593 skb_reset_network_header(skb);
594 skb->protocol = htons(ETH_P_IP);
595 skb->ip_summed = CHECKSUM_NONE;
596
597 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
598
599 netif_rx(skb);
600
601 return NET_RX_SUCCESS;
602}
603#else
604static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
605{
606 return NULL;
607}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608#endif
609
Yotam Gigi4d65b942017-09-27 08:23:13 +0200610static int call_ipmr_vif_entry_notifier(struct notifier_block *nb,
611 struct net *net,
612 enum fib_event_type event_type,
613 struct vif_device *vif,
614 vifi_t vif_index, u32 tb_id)
615{
616 struct vif_entry_notifier_info info = {
617 .info = {
618 .family = RTNL_FAMILY_IPMR,
619 .net = net,
620 },
621 .dev = vif->dev,
622 .vif_index = vif_index,
623 .vif_flags = vif->flags,
624 .tb_id = tb_id,
625 };
626
627 return call_fib_notifier(nb, net, event_type, &info.info);
628}
629
Yotam Gigib3620532017-09-27 08:23:14 +0200630static int call_ipmr_vif_entry_notifiers(struct net *net,
631 enum fib_event_type event_type,
632 struct vif_device *vif,
633 vifi_t vif_index, u32 tb_id)
634{
635 struct vif_entry_notifier_info info = {
636 .info = {
637 .family = RTNL_FAMILY_IPMR,
638 .net = net,
639 },
640 .dev = vif->dev,
641 .vif_index = vif_index,
642 .vif_flags = vif->flags,
643 .tb_id = tb_id,
644 };
645
646 ASSERT_RTNL();
647 net->ipv4.ipmr_seq++;
648 return call_fib_notifiers(net, event_type, &info.info);
649}
650
Yotam Gigi4d65b942017-09-27 08:23:13 +0200651static int call_ipmr_mfc_entry_notifier(struct notifier_block *nb,
652 struct net *net,
653 enum fib_event_type event_type,
654 struct mfc_cache *mfc, u32 tb_id)
655{
656 struct mfc_entry_notifier_info info = {
657 .info = {
658 .family = RTNL_FAMILY_IPMR,
659 .net = net,
660 },
661 .mfc = mfc,
662 .tb_id = tb_id
663 };
664
665 return call_fib_notifier(nb, net, event_type, &info.info);
666}
667
Yotam Gigib3620532017-09-27 08:23:14 +0200668static int call_ipmr_mfc_entry_notifiers(struct net *net,
669 enum fib_event_type event_type,
670 struct mfc_cache *mfc, u32 tb_id)
671{
672 struct mfc_entry_notifier_info info = {
673 .info = {
674 .family = RTNL_FAMILY_IPMR,
675 .net = net,
676 },
677 .mfc = mfc,
678 .tb_id = tb_id
679 };
680
681 ASSERT_RTNL();
682 net->ipv4.ipmr_seq++;
683 return call_fib_notifiers(net, event_type, &info.info);
684}
685
Ben Hutchings2c530402012-07-10 10:55:09 +0000686/**
687 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700688 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000690static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000691 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692{
Yotam Gigib3620532017-09-27 08:23:14 +0200693 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 struct vif_device *v;
695 struct net_device *dev;
696 struct in_device *in_dev;
697
Patrick McHardy0c122952010-04-13 05:03:22 +0000698 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return -EADDRNOTAVAIL;
700
Patrick McHardy0c122952010-04-13 05:03:22 +0000701 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
Yotam Gigib3620532017-09-27 08:23:14 +0200703 if (VIF_EXISTS(mrt, vifi))
704 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
705 mrt->id);
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 write_lock_bh(&mrt_lock);
708 dev = v->dev;
709 v->dev = NULL;
710
711 if (!dev) {
712 write_unlock_bh(&mrt_lock);
713 return -EADDRNOTAVAIL;
714 }
715
Patrick McHardy0c122952010-04-13 05:03:22 +0000716 if (vifi == mrt->mroute_reg_vif_num)
717 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000719 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000721
722 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000723 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 break;
725 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000726 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 }
728
729 write_unlock_bh(&mrt_lock);
730
731 dev_set_allmulti(dev, -1);
732
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000733 in_dev = __in_dev_get_rtnl(dev);
734 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700735 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -0700736 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000737 NETCONFA_MC_FORWARDING,
738 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 ip_rt_multicast_event(in_dev);
740 }
741
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000742 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000743 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744
745 dev_put(dev);
746 return 0;
747}
748
Eric Dumazeta8c94862010-10-01 16:15:08 +0000749static void ipmr_cache_free_rcu(struct rcu_head *head)
750{
751 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
752
753 kmem_cache_free(mrt_cachep, c);
754}
755
Yotam Gigi310ebbb2017-09-27 08:23:12 +0200756void ipmr_cache_free(struct mfc_cache *c)
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000757{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000758 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000759}
Yotam Gigi310ebbb2017-09-27 08:23:12 +0200760EXPORT_SYMBOL(ipmr_cache_free);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000763 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000765static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200767 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700769 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Patrick McHardy0c122952010-04-13 05:03:22 +0000771 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772
Jianjun Kongc354e122008-11-03 00:28:02 -0800773 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700774 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200775 struct nlmsghdr *nlh = skb_pull(skb,
776 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000778 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000780 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700781 e->error = -ETIMEDOUT;
782 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700783
Eric W. Biederman15e47302012-09-07 20:12:54 +0000784 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000785 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000787 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000790 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791}
792
Patrick McHardye258beb2010-04-13 05:03:19 +0000793/* Timer process for the unresolved queue. */
Patrick McHardye258beb2010-04-13 05:03:19 +0000794static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795{
Patrick McHardy0c122952010-04-13 05:03:22 +0000796 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 unsigned long now;
798 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000799 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800
801 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000802 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return;
804 }
805
Patrick McHardy0c122952010-04-13 05:03:22 +0000806 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 goto out;
808
809 now = jiffies;
810 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Patrick McHardy0c122952010-04-13 05:03:22 +0000812 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 if (time_after(c->mfc_un.unres.expires, now)) {
814 unsigned long interval = c->mfc_un.unres.expires - now;
815 if (interval < expires)
816 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 continue;
818 }
819
Patrick McHardy862465f2010-04-13 05:03:21 +0000820 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000821 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000822 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
824
Patrick McHardy0c122952010-04-13 05:03:22 +0000825 if (!list_empty(&mrt->mfc_unres_queue))
826 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828out:
829 spin_unlock(&mfc_unres_lock);
830}
831
832/* Fill oifs list. It is called under write locked mrt_lock. */
Patrick McHardy0c122952010-04-13 05:03:22 +0000833static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000834 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
836 int vifi;
837
838 cache->mfc_un.res.minvif = MAXVIFS;
839 cache->mfc_un.res.maxvif = 0;
840 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
841
Patrick McHardy0c122952010-04-13 05:03:22 +0000842 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
843 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000844 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
846 if (cache->mfc_un.res.minvif > vifi)
847 cache->mfc_un.res.minvif = vifi;
848 if (cache->mfc_un.res.maxvif <= vifi)
849 cache->mfc_un.res.maxvif = vifi + 1;
850 }
851 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200852 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853}
854
Patrick McHardy0c122952010-04-13 05:03:22 +0000855static int vif_add(struct net *net, struct mr_table *mrt,
856 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
858 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000859 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860 struct net_device *dev;
861 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700862 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000865 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 return -EADDRINUSE;
867
868 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100870 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100871 return -EINVAL;
872 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 * All the packets will be sent to the daemon
874 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000875 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000877 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878 if (!dev)
879 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700880 err = dev_set_allmulti(dev, 1);
881 if (err) {
882 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700883 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700884 return err;
885 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900887 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000888 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (!dev)
890 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700891 err = dev_set_allmulti(dev, 1);
892 if (err) {
893 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700894 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700895 return err;
896 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000898 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000900 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
901 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100902 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000903 dev_put(dev);
904 return -EADDRNOTAVAIL;
905 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000906 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000907 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000908 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 if (!dev)
910 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700911 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700912 if (err) {
913 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700914 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 break;
917 default:
918 return -EINVAL;
919 }
920
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000921 in_dev = __in_dev_get_rtnl(dev);
922 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000923 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000925 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700926 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700927 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
928 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 ip_rt_multicast_event(in_dev);
930
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000931 /* Fill in the VIF structures */
932
Jianjun Kongc354e122008-11-03 00:28:02 -0800933 v->rate_limit = vifc->vifc_rate_limit;
934 v->local = vifc->vifc_lcl_addr.s_addr;
935 v->remote = vifc->vifc_rmt_addr.s_addr;
936 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 if (!mrtsock)
938 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800939 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 v->bytes_in = 0;
941 v->bytes_out = 0;
942 v->pkt_in = 0;
943 v->pkt_out = 0;
944 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000945 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200946 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 /* And finish update writing critical data */
949 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800950 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000951 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000952 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000953 if (vifi+1 > mrt->maxvif)
954 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +0200956 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957 return 0;
958}
959
Eric Dumazeta8c94862010-10-01 16:15:08 +0000960/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000961static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000962 __be32 origin,
963 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100965 struct mfc_cache_cmp_arg arg = {
966 .mfc_mcastgrp = mcastgrp,
967 .mfc_origin = origin
968 };
969 struct rhlist_head *tmp, *list;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 struct mfc_cache *c;
971
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100972 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
973 rhl_for_each_entry_rcu(c, tmp, list, mnode)
974 return c;
975
Patrick McHardy862465f2010-04-13 05:03:21 +0000976 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977}
978
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000979/* Look for a (*,*,oif) entry */
980static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
981 int vifi)
982{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100983 struct mfc_cache_cmp_arg arg = {
984 .mfc_mcastgrp = htonl(INADDR_ANY),
985 .mfc_origin = htonl(INADDR_ANY)
986 };
987 struct rhlist_head *tmp, *list;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000988 struct mfc_cache *c;
989
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100990 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
991 rhl_for_each_entry_rcu(c, tmp, list, mnode)
992 if (c->mfc_un.res.ttls[vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000993 return c;
994
995 return NULL;
996}
997
998/* Look for a (*,G) entry */
999static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
1000 __be32 mcastgrp, int vifi)
1001{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001002 struct mfc_cache_cmp_arg arg = {
1003 .mfc_mcastgrp = mcastgrp,
1004 .mfc_origin = htonl(INADDR_ANY)
1005 };
1006 struct rhlist_head *tmp, *list;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001007 struct mfc_cache *c, *proxy;
1008
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001009 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001010 goto skip;
1011
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001012 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
1013 rhl_for_each_entry_rcu(c, tmp, list, mnode) {
1014 if (c->mfc_un.res.ttls[vifi] < 255)
1015 return c;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001016
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001017 /* It's ok if the vifi is part of the static tree */
1018 proxy = ipmr_cache_find_any_parent(mrt, c->mfc_parent);
1019 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
1020 return c;
1021 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001022
1023skip:
1024 return ipmr_cache_find_any_parent(mrt, vifi);
1025}
1026
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001027/* Look for a (S,G,iif) entry if parent != -1 */
1028static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
1029 __be32 origin, __be32 mcastgrp,
1030 int parent)
1031{
1032 struct mfc_cache_cmp_arg arg = {
1033 .mfc_mcastgrp = mcastgrp,
1034 .mfc_origin = origin,
1035 };
1036 struct rhlist_head *tmp, *list;
1037 struct mfc_cache *c;
1038
1039 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
1040 rhl_for_each_entry_rcu(c, tmp, list, mnode)
1041 if (parent == -1 || parent == c->mfc_parent)
1042 return c;
1043
1044 return NULL;
1045}
1046
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001047/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001048static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
Jianjun Kongc354e122008-11-03 00:28:02 -08001050 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001051
Tom Goff70a0dec2016-06-23 16:11:57 -04001052 if (c) {
1053 c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001054 c->mfc_un.res.minvif = MAXVIFS;
Yotam Gigi310ebbb2017-09-27 08:23:12 +02001055 refcount_set(&c->mfc_un.res.refcount, 1);
Tom Goff70a0dec2016-06-23 16:11:57 -04001056 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 return c;
1058}
1059
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001060static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Jianjun Kongc354e122008-11-03 00:28:02 -08001062 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001063
1064 if (c) {
1065 skb_queue_head_init(&c->mfc_un.unres.unresolved);
1066 c->mfc_un.unres.expires = jiffies + 10*HZ;
1067 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return c;
1069}
1070
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001071/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +00001072static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1073 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074{
1075 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001076 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001078 /* Play the pending entries through our router */
Jianjun Kongc354e122008-11-03 00:28:02 -08001079 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001080 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +02001081 struct nlmsghdr *nlh = skb_pull(skb,
1082 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Hong zhi guo573ce262013-03-27 06:47:04 +00001084 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001085 nlh->nlmsg_len = skb_tail_pointer(skb) -
1086 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 } else {
1088 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +00001089 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +00001091 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001092 e->error = -EMSGSIZE;
1093 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Thomas Graf2942e902006-08-15 00:30:25 -07001095
Eric W. Biederman15e47302012-09-07 20:12:54 +00001096 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001097 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001098 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001099 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
1101}
1102
Julien Gomes5a645dd2017-06-20 13:54:17 -07001103/* Bounce a cache query up to mrouted and netlink.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001105 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001107static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001108 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001110 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001111 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 struct igmphdr *igmp;
1113 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001114 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 int ret;
1116
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 if (assert == IGMPMSG_WHOLEPKT)
1118 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1119 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 skb = alloc_skb(128, GFP_ATOMIC);
1121
Stephen Hemminger132adf52007-03-08 20:44:43 -08001122 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 return -ENOBUFS;
1124
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 if (assert == IGMPMSG_WHOLEPKT) {
1126 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001127 * Duplicate old header, fix ihl, length etc.
1128 * And all this only to mangle msg->im_msgtype and
1129 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001131 skb_push(skb, sizeof(struct iphdr));
1132 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001133 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001134 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001135 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1137 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001138 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001139 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1140 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1141 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001142 } else {
1143 /* Copy the IP header */
1144 skb_set_network_header(skb, skb->len);
1145 skb_put(skb, ihl);
1146 skb_copy_to_linear_data(skb, pkt->data, ihl);
1147 /* Flag to the kernel this is a route add */
1148 ip_hdr(skb)->protocol = 0;
1149 msg = (struct igmpmsg *)skb_network_header(skb);
1150 msg->im_vif = vifi;
1151 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1152 /* Add our header */
Johannes Berg4df864c2017-06-16 14:29:21 +02001153 igmp = skb_put(skb, sizeof(struct igmphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001154 igmp->type = assert;
1155 msg->im_msgtype = assert;
1156 igmp->code = 0;
1157 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1158 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Eric Dumazet4c9687092010-10-01 16:15:01 +00001161 rcu_read_lock();
1162 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001163 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001164 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 kfree_skb(skb);
1166 return -EINVAL;
1167 }
1168
Julien Gomes5a645dd2017-06-20 13:54:17 -07001169 igmpmsg_netlink_event(mrt, skb);
1170
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001171 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001172 ret = sock_queue_rcv_skb(mroute_sk, skb);
1173 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001174 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001175 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 kfree_skb(skb);
1177 }
1178
1179 return ret;
1180}
1181
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001182/* Queue a packet for resolution. It gets locked cache entry! */
1183static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001184 struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001186 const struct iphdr *iph = ip_hdr(skb);
1187 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001188 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001192 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001193 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001194 c->mfc_origin == iph->saddr) {
1195 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 }
1199
Patrick McHardy862465f2010-04-13 05:03:21 +00001200 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001201 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001202 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001203 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204 spin_unlock_bh(&mfc_unres_lock);
1205
1206 kfree_skb(skb);
1207 return -ENOBUFS;
1208 }
1209
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001210 /* Fill in the new cache entry */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001211 c->mfc_parent = -1;
1212 c->mfc_origin = iph->saddr;
1213 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001215 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001216 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001217 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001218 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 out - Brad Parker
1220 */
1221 spin_unlock_bh(&mfc_unres_lock);
1222
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001223 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 kfree_skb(skb);
1225 return err;
1226 }
1227
Patrick McHardy0c122952010-04-13 05:03:22 +00001228 atomic_inc(&mrt->cache_resolve_queue_len);
1229 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001230 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
David S. Miller278554b2010-05-12 00:05:35 -07001232 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1233 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234 }
1235
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001236 /* See if we can append the packet */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001237 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 kfree_skb(skb);
1239 err = -ENOBUFS;
1240 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001241 if (dev) {
1242 skb->dev = dev;
1243 skb->skb_iif = dev->ifindex;
1244 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001245 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 err = 0;
1247 }
1248
1249 spin_unlock_bh(&mfc_unres_lock);
1250 return err;
1251}
1252
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001253/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001255static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256{
Yotam Gigib3620532017-09-27 08:23:14 +02001257 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001258 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001260 /* The entries are added/deleted only under RTNL */
1261 rcu_read_lock();
1262 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1263 mfc->mfcc_mcastgrp.s_addr, parent);
1264 rcu_read_unlock();
1265 if (!c)
1266 return -ENOENT;
1267 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1268 list_del_rcu(&c->list);
Yotam Gigib3620532017-09-27 08:23:14 +02001269 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001270 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yotam Gigi310ebbb2017-09-27 08:23:12 +02001271 ipmr_cache_put(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001273 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274}
1275
Patrick McHardy0c122952010-04-13 05:03:22 +00001276static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001277 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278{
Patrick McHardy862465f2010-04-13 05:03:21 +00001279 struct mfc_cache *uc, *c;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001280 bool found;
1281 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Patrick McHardya50436f22010-03-17 06:04:14 +00001283 if (mfc->mfcc_parent >= MAXVIFS)
1284 return -ENFILE;
1285
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001286 /* The entries are added/deleted only under RTNL */
1287 rcu_read_lock();
1288 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1289 mfc->mfcc_mcastgrp.s_addr, parent);
1290 rcu_read_unlock();
1291 if (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 write_lock_bh(&mrt_lock);
1293 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001294 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (!mrtsock)
1296 c->mfc_flags |= MFC_STATIC;
1297 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +02001298 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1299 mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001300 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301 return 0;
1302 }
1303
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001304 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001305 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 return -EINVAL;
1307
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001308 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001309 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 return -ENOMEM;
1311
Jianjun Kongc354e122008-11-03 00:28:02 -08001312 c->mfc_origin = mfc->mfcc_origin.s_addr;
1313 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1314 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001315 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (!mrtsock)
1317 c->mfc_flags |= MFC_STATIC;
1318
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001319 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->mnode,
1320 ipmr_rht_params);
1321 if (ret) {
1322 pr_err("ipmr: rhtable insert error %d\n", ret);
1323 ipmr_cache_free(c);
1324 return ret;
1325 }
1326 list_add_tail_rcu(&c->list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001327 /* Check to see if we resolved a queued list. If so we
1328 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001330 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001332 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001333 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001335 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001336 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001337 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 break;
1339 }
1340 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001341 if (list_empty(&mrt->mfc_unres_queue))
1342 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 spin_unlock_bh(&mfc_unres_lock);
1344
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001345 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001346 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001347 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 }
Yotam Gigib3620532017-09-27 08:23:14 +02001349 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001350 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return 0;
1352}
1353
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001354/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001355static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356{
Yotam Gigib3620532017-09-27 08:23:14 +02001357 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001358 struct mfc_cache *c, *tmp;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001359 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001360 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001361
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001362 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001363 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001364 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1365 continue;
1366 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001368 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001370 /* Wipe the cache */
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001371 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1372 if (!all && (c->mfc_flags & MFC_STATIC))
1373 continue;
1374 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1375 list_del_rcu(&c->list);
Yotam Gigib3620532017-09-27 08:23:14 +02001376 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c,
1377 mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001378 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yotam Gigi310ebbb2017-09-27 08:23:12 +02001379 ipmr_cache_put(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001380 }
1381
Patrick McHardy0c122952010-04-13 05:03:22 +00001382 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383 spin_lock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001384 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001385 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001386 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001387 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
1389 spin_unlock_bh(&mfc_unres_lock);
1390 }
1391}
1392
Eric Dumazet4c9687092010-10-01 16:15:01 +00001393/* called from ip_ra_control(), before an RCU grace period,
1394 * we dont need to call synchronize_rcu() here
1395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396static void mrtsock_destruct(struct sock *sk)
1397{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001398 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001399 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001400
WANG Cong1215e512017-04-12 12:32:13 -07001401 ASSERT_RTNL();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001402 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001403 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001404 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001405 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1406 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001407 NETCONFA_IFINDEX_ALL,
1408 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001409 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001410 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001411 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413}
1414
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001415/* Socket options and virtual interface manipulation. The whole
1416 * virtual interface system is a complete heap, but unfortunately
1417 * that's how BSD mrouted happens to think. Maybe one day with a proper
1418 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001420
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001421int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1422 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001424 struct net *net = sock_net(sk);
1425 int val, ret = 0, parent = 0;
1426 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 struct vifctl vif;
1428 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001429 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001430
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001431 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1432 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001433 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001434 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1435 ret = -EOPNOTSUPP;
1436 goto out_unlock;
1437 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001438
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001439 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001440 if (!mrt) {
1441 ret = -ENOENT;
1442 goto out_unlock;
1443 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001444 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001445 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001446 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1447 ret = -EACCES;
1448 goto out_unlock;
1449 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 }
1451
Stephen Hemminger132adf52007-03-08 20:44:43 -08001452 switch (optname) {
1453 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001454 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001455 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001456 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001457 }
1458 if (rtnl_dereference(mrt->mroute_sk)) {
1459 ret = -EADDRINUSE;
1460 break;
1461 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001462
1463 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1464 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001465 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001466 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001467 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1468 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001469 NETCONFA_IFINDEX_ALL,
1470 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001471 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001472 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001473 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001474 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1475 ret = -EACCES;
1476 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001477 ret = ip_ra_control(sk, 0, NULL);
WANG Cong1215e512017-04-12 12:32:13 -07001478 goto out_unlock;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001479 }
1480 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001481 case MRT_ADD_VIF:
1482 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001483 if (optlen != sizeof(vif)) {
1484 ret = -EINVAL;
1485 break;
1486 }
1487 if (copy_from_user(&vif, optval, sizeof(vif))) {
1488 ret = -EFAULT;
1489 break;
1490 }
1491 if (vif.vifc_vifi >= MAXVIFS) {
1492 ret = -ENFILE;
1493 break;
1494 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001495 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001496 ret = vif_add(net, mrt, &vif,
1497 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001498 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001499 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001500 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001501 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001502 /* Manipulate the forwarding caches. These live
1503 * in a sort of kernel/user symbiosis.
1504 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001505 case MRT_ADD_MFC:
1506 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001507 parent = -1;
1508 case MRT_ADD_MFC_PROXY:
1509 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001510 if (optlen != sizeof(mfc)) {
1511 ret = -EINVAL;
1512 break;
1513 }
1514 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1515 ret = -EFAULT;
1516 break;
1517 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001518 if (parent == 0)
1519 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001520 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1521 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001522 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001523 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001524 sk == rtnl_dereference(mrt->mroute_sk),
1525 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001526 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001527 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001528 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001529 if (optlen != sizeof(val)) {
1530 ret = -EINVAL;
1531 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001533 if (get_user(val, (int __user *)optval)) {
1534 ret = -EFAULT;
1535 break;
1536 }
1537 mrt->mroute_do_assert = val;
1538 break;
1539 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001540 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001541 ret = -ENOPROTOOPT;
1542 break;
1543 }
1544 if (optlen != sizeof(val)) {
1545 ret = -EINVAL;
1546 break;
1547 }
1548 if (get_user(val, (int __user *)optval)) {
1549 ret = -EFAULT;
1550 break;
1551 }
1552
1553 val = !!val;
1554 if (val != mrt->mroute_do_pim) {
1555 mrt->mroute_do_pim = val;
1556 mrt->mroute_do_assert = val;
1557 }
1558 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001559 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001560 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1561 ret = -ENOPROTOOPT;
1562 break;
1563 }
1564 if (optlen != sizeof(uval)) {
1565 ret = -EINVAL;
1566 break;
1567 }
1568 if (get_user(uval, (u32 __user *)optval)) {
1569 ret = -EFAULT;
1570 break;
1571 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001572
Eric Dumazet4c9687092010-10-01 16:15:01 +00001573 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1574 ret = -EBUSY;
1575 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001576 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001577 if (IS_ERR(mrt))
1578 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001579 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001580 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001581 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001582 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001583 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001584 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001585 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001587out_unlock:
1588 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001589 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001592/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001593int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594{
1595 int olr;
1596 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001597 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001598 struct mr_table *mrt;
1599
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001600 if (sk->sk_type != SOCK_RAW ||
1601 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1602 return -EOPNOTSUPP;
1603
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001604 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001605 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001606 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001608 switch (optname) {
1609 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001610 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001611 break;
1612 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001613 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001614 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001615 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001616 break;
1617 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001618 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001619 break;
1620 default:
1621 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001622 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001623
1624 if (get_user(olr, optlen))
1625 return -EFAULT;
1626 olr = min_t(unsigned int, olr, sizeof(int));
1627 if (olr < 0)
1628 return -EINVAL;
1629 if (put_user(olr, optlen))
1630 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001631 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 return -EFAULT;
1633 return 0;
1634}
1635
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001636/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1638{
1639 struct sioc_sg_req sr;
1640 struct sioc_vif_req vr;
1641 struct vif_device *vif;
1642 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001643 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001644 struct mr_table *mrt;
1645
1646 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001647 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001648 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001649
Stephen Hemminger132adf52007-03-08 20:44:43 -08001650 switch (cmd) {
1651 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001652 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001653 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001654 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001655 return -EINVAL;
1656 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001657 vif = &mrt->vif_table[vr.vifi];
1658 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001659 vr.icount = vif->pkt_in;
1660 vr.ocount = vif->pkt_out;
1661 vr.ibytes = vif->bytes_in;
1662 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001664
Jianjun Kongc354e122008-11-03 00:28:02 -08001665 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001667 return 0;
1668 }
1669 read_unlock(&mrt_lock);
1670 return -EADDRNOTAVAIL;
1671 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001672 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001673 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Eric Dumazeta8c94862010-10-01 16:15:08 +00001675 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001676 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001677 if (c) {
1678 sr.pktcnt = c->mfc_un.res.pkt;
1679 sr.bytecnt = c->mfc_un.res.bytes;
1680 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001681 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001682
Jianjun Kongc354e122008-11-03 00:28:02 -08001683 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001684 return -EFAULT;
1685 return 0;
1686 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001687 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001688 return -EADDRNOTAVAIL;
1689 default:
1690 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 }
1692}
1693
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001694#ifdef CONFIG_COMPAT
1695struct compat_sioc_sg_req {
1696 struct in_addr src;
1697 struct in_addr grp;
1698 compat_ulong_t pktcnt;
1699 compat_ulong_t bytecnt;
1700 compat_ulong_t wrong_if;
1701};
1702
David S. Millerca6b8bb02011-02-03 17:24:28 -08001703struct compat_sioc_vif_req {
1704 vifi_t vifi; /* Which iface */
1705 compat_ulong_t icount;
1706 compat_ulong_t ocount;
1707 compat_ulong_t ibytes;
1708 compat_ulong_t obytes;
1709};
1710
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001711int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1712{
David S. Miller0033d5a2011-02-03 17:21:31 -08001713 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001714 struct compat_sioc_vif_req vr;
1715 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001716 struct mfc_cache *c;
1717 struct net *net = sock_net(sk);
1718 struct mr_table *mrt;
1719
1720 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001721 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001722 return -ENOENT;
1723
1724 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001725 case SIOCGETVIFCNT:
1726 if (copy_from_user(&vr, arg, sizeof(vr)))
1727 return -EFAULT;
1728 if (vr.vifi >= mrt->maxvif)
1729 return -EINVAL;
1730 read_lock(&mrt_lock);
1731 vif = &mrt->vif_table[vr.vifi];
1732 if (VIF_EXISTS(mrt, vr.vifi)) {
1733 vr.icount = vif->pkt_in;
1734 vr.ocount = vif->pkt_out;
1735 vr.ibytes = vif->bytes_in;
1736 vr.obytes = vif->bytes_out;
1737 read_unlock(&mrt_lock);
1738
1739 if (copy_to_user(arg, &vr, sizeof(vr)))
1740 return -EFAULT;
1741 return 0;
1742 }
1743 read_unlock(&mrt_lock);
1744 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001745 case SIOCGETSGCNT:
1746 if (copy_from_user(&sr, arg, sizeof(sr)))
1747 return -EFAULT;
1748
1749 rcu_read_lock();
1750 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1751 if (c) {
1752 sr.pktcnt = c->mfc_un.res.pkt;
1753 sr.bytecnt = c->mfc_un.res.bytes;
1754 sr.wrong_if = c->mfc_un.res.wrong_if;
1755 rcu_read_unlock();
1756
1757 if (copy_to_user(arg, &sr, sizeof(sr)))
1758 return -EFAULT;
1759 return 0;
1760 }
1761 rcu_read_unlock();
1762 return -EADDRNOTAVAIL;
1763 default:
1764 return -ENOIOCTLCMD;
1765 }
1766}
1767#endif
1768
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1770{
Jiri Pirko351638e2013-05-28 01:30:21 +00001771 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001772 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001773 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 struct vif_device *v;
1775 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 if (event != NETDEV_UNREGISTER)
1778 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001779
1780 ipmr_for_each_table(mrt, net) {
1781 v = &mrt->vif_table[0];
1782 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1783 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001784 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001785 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 }
1787 return NOTIFY_DONE;
1788}
1789
Jianjun Kongc354e122008-11-03 00:28:02 -08001790static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 .notifier_call = ipmr_device_event,
1792};
1793
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001794/* Encapsulate a packet by attaching a valid IPIP header to it.
1795 * This avoids tunnel drivers and other mess and gives us the speed so
1796 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001798static void ip_encap(struct net *net, struct sk_buff *skb,
1799 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001801 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001802 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001803
1804 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001805 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001806 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001807 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001809 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001810 iph->tos = old_iph->tos;
1811 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812 iph->frag_off = 0;
1813 iph->daddr = daddr;
1814 iph->saddr = saddr;
1815 iph->protocol = IPPROTO_IPIP;
1816 iph->ihl = 5;
1817 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001818 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 ip_send_check(iph);
1820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1822 nf_reset(skb);
1823}
1824
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001825static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1826 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001828 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829
David S. Miller73186df2015-11-03 13:41:45 -05001830 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1831 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 if (unlikely(opt->optlen))
1834 ip_forward_options(skb);
1835
Eric W. Biederman13206b62015-10-07 16:48:35 -05001836 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837}
1838
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001839/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
Patrick McHardy0c122952010-04-13 05:03:22 +00001841static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1842 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001844 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001845 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 struct net_device *dev;
1847 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001848 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 int encap = 0;
1850
Ian Morris51456b22015-04-03 09:17:26 +01001851 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 goto out_free;
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 if (vif->flags & VIFF_REGISTER) {
1855 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001856 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001857 vif->dev->stats.tx_bytes += skb->len;
1858 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001859 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001860 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001863 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001864 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001865 vif->remote, vif->local,
1866 0, 0,
1867 IPPROTO_IPIP,
1868 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001869 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 goto out_free;
1871 encap = sizeof(struct iphdr);
1872 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001873 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001874 0, 0,
1875 IPPROTO_IPIP,
1876 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001877 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 goto out_free;
1879 }
1880
Changli Gaod8d1f302010-06-10 23:31:35 -07001881 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
Changli Gaod8d1f302010-06-10 23:31:35 -07001883 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001885 * allow to send ICMP, so that packets will disappear
1886 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 */
David S. Miller73186df2015-11-03 13:41:45 -05001888 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 ip_rt_put(rt);
1890 goto out_free;
1891 }
1892
Changli Gaod8d1f302010-06-10 23:31:35 -07001893 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001896 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 goto out_free;
1898 }
1899
1900 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001901 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902
Eric Dumazetadf30902009-06-02 05:19:30 +00001903 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001904 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001905 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906
1907 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001908 * What do we do with netfilter? -- RR
1909 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001911 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001913 vif->dev->stats.tx_packets++;
1914 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001917 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001919 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 * not only before forwarding, but after forwarding on all output
1921 * interfaces. It is clear, if mrouter runs a multicasting
1922 * program, it should receive packets not depending to what interface
1923 * program is joined.
1924 * If we will not make it, the program will have to join on all
1925 * interfaces. On the other hand, multihoming host (or router, but
1926 * not mrouter) cannot join to more than one interface - it will
1927 * result in receiving multiple packets.
1928 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001929 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1930 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 ipmr_forward_finish);
1932 return;
1933
1934out_free:
1935 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936}
1937
Patrick McHardy0c122952010-04-13 05:03:22 +00001938static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939{
1940 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001941
1942 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1943 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 break;
1945 }
1946 return ct;
1947}
1948
1949/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001950static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001951 struct net_device *dev, struct sk_buff *skb,
1952 struct mfc_cache *cache, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953{
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001954 int true_vifi = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 int psend = -1;
1956 int vif, ct;
1957
1958 vif = cache->mfc_parent;
1959 cache->mfc_un.res.pkt++;
1960 cache->mfc_un.res.bytes += skb->len;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03001961 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001963 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001964 struct mfc_cache *cache_proxy;
1965
1966 /* For an (*,G) entry, we only check that the incomming
1967 * interface is part of the static tree.
1968 */
1969 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1970 if (cache_proxy &&
1971 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1972 goto forward;
1973 }
1974
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001975 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001976 if (mrt->vif_table[vif].dev != dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001977 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001979 * Very complicated situation...
1980 *
1981 * The best workaround until routing daemons will be
1982 * fixed is not to redistribute packet, if it was
1983 * send through wrong interface. It means, that
1984 * multicast applications WILL NOT work for
1985 * (S,G), which have default multicast route pointing
1986 * to wrong oif. In any case, it is not a good
1987 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 */
1989 goto dont_forward;
1990 }
1991
1992 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993
Patrick McHardy0c122952010-04-13 05:03:22 +00001994 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001996 * so that we cannot check that packet arrived on an oif.
1997 * It is bad, but otherwise we would need to move pretty
1998 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 */
Patrick McHardy0c122952010-04-13 05:03:22 +00002000 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00002001 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002002 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
2004 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00002005 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
2007 goto dont_forward;
2008 }
2009
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002010forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00002011 mrt->vif_table[vif].pkt_in++;
2012 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002014 /* Forward the frame */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002015 if (cache->mfc_origin == htonl(INADDR_ANY) &&
2016 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002017 if (true_vifi >= 0 &&
2018 true_vifi != cache->mfc_parent &&
2019 ip_hdr(skb)->ttl >
2020 cache->mfc_un.res.ttls[cache->mfc_parent]) {
2021 /* It's an (*,*) entry and the packet is not coming from
2022 * the upstream: forward the packet to the upstream
2023 * only.
2024 */
2025 psend = cache->mfc_parent;
2026 goto last_forward;
2027 }
2028 goto dont_forward;
2029 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002030 for (ct = cache->mfc_un.res.maxvif - 1;
2031 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002032 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002033 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
2034 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002035 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (psend != -1) {
2037 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002038
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00002040 ipmr_queue_xmit(net, mrt, skb2, cache,
2041 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002043 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 }
2045 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002046last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 if (psend != -1) {
2048 if (local) {
2049 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00002052 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00002054 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03002055 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 }
2057 }
2058
2059dont_forward:
2060 if (!local)
2061 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062}
2063
David S. Miller417da662011-05-03 19:42:43 -07002064static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08002065{
David S. Miller417da662011-05-03 19:42:43 -07002066 struct rtable *rt = skb_rtable(skb);
2067 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05002068 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07002069 .daddr = iph->daddr,
2070 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00002071 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07002072 .flowi4_oif = (rt_is_output_route(rt) ?
2073 skb->dev->ifindex : 0),
2074 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002075 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07002076 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00002077 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08002078 };
2079 struct mr_table *mrt;
2080 int err;
2081
David S. Millerda919812011-03-12 02:04:50 -05002082 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002083 if (err)
2084 return ERR_PTR(err);
2085 return mrt;
2086}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002088/* Multicast packets for forwarding arrive here
2089 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091int ip_mr_input(struct sk_buff *skb)
2092{
2093 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002094 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00002095 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002096 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002097 struct net_device *dev;
2098
2099 /* skb->dev passed in is the loX master dev for vrfs.
2100 * As there are no vifs associated with loopback devices,
2101 * get the proper interface that does have a vif associated with it.
2102 */
2103 dev = skb->dev;
2104 if (netif_is_l3_master(skb->dev)) {
2105 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2106 if (!dev) {
2107 kfree_skb(skb);
2108 return -ENODEV;
2109 }
2110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111
2112 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002113 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00002115 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116 goto dont_forward;
2117
David S. Miller417da662011-05-03 19:42:43 -07002118 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002119 if (IS_ERR(mrt)) {
2120 kfree_skb(skb);
2121 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00002124 if (IPCB(skb)->opt.router_alert) {
2125 if (ip_call_ra_chain(skb))
2126 return 0;
2127 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2128 /* IGMPv1 (and broken IGMPv2 implementations sort of
2129 * Cisco IOS <= 11.2(8)) do not put router alert
2130 * option to IGMP packets destined to routable
2131 * groups. It is very bad, because it means
2132 * that we can forward NO IGMP messages.
2133 */
2134 struct sock *mroute_sk;
2135
2136 mroute_sk = rcu_dereference(mrt->mroute_sk);
2137 if (mroute_sk) {
2138 nf_reset(skb);
2139 raw_rcv(mroute_sk, skb);
2140 return 0;
2141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 }
2143 }
2144
Eric Dumazeta8c94862010-10-01 16:15:08 +00002145 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002146 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002147 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002148 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002149
2150 if (vif >= 0)
2151 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2152 vif);
2153 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002155 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002156 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157 int vif;
2158
2159 if (local) {
2160 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2161 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002162 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 skb = skb2;
2165 }
2166
Eric Dumazeta8c94862010-10-01 16:15:08 +00002167 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002168 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (vif >= 0) {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002170 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 read_unlock(&mrt_lock);
2172
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002173 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 }
2175 read_unlock(&mrt_lock);
2176 kfree_skb(skb);
2177 return -ENODEV;
2178 }
2179
Eric Dumazeta8c94862010-10-01 16:15:08 +00002180 read_lock(&mrt_lock);
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002181 ip_mr_forward(net, mrt, dev, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 read_unlock(&mrt_lock);
2183
2184 if (local)
2185 return ip_local_deliver(skb);
2186
2187 return 0;
2188
2189dont_forward:
2190 if (local)
2191 return ip_local_deliver(skb);
2192 kfree_skb(skb);
2193 return 0;
2194}
2195
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002196#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002197/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002198int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002199{
2200 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002201 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002202 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002203
2204 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2205 goto drop;
2206
2207 pim = igmp_hdr(skb);
2208
David S. Miller417da662011-05-03 19:42:43 -07002209 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002210 if (IS_ERR(mrt))
2211 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002212 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002213 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2214 goto drop;
2215
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002216 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002217drop:
2218 kfree_skb(skb);
2219 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 return 0;
2221}
2222#endif
2223
2224#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002225static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226{
2227 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002228 struct net *net = dev_net(skb->dev);
2229 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002231 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 goto drop;
2233
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002234 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002235 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002236 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002237 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002238 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 goto drop;
2240
David S. Miller417da662011-05-03 19:42:43 -07002241 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002242 if (IS_ERR(mrt))
2243 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002244 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002245drop:
2246 kfree_skb(skb);
2247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return 0;
2249}
2250#endif
2251
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002252static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2253 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002255 struct rta_mfc_stats mfcs;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002256 struct nlattr *mp_attr;
2257 struct rtnexthop *nhp;
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002258 unsigned long lastuse;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002259 int ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260
Nicolas Dichtel74381892010-03-25 23:45:35 +00002261 /* If cache is unresolved, don't try to parse IIF and OIF */
Nikolay Aleksandrov1708ebc2017-01-03 12:13:39 +01002262 if (c->mfc_parent >= MAXVIFS) {
2263 rtm->rtm_flags |= RTNH_F_UNRESOLVED;
Nicolas Dichtel74381892010-03-25 23:45:35 +00002264 return -ENOENT;
Nikolay Aleksandrov1708ebc2017-01-03 12:13:39 +01002265 }
Nicolas Dichtel74381892010-03-25 23:45:35 +00002266
Thomas Graf92a395e2012-06-26 23:36:13 +00002267 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2268 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2269 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Yotam Gigic7c0bbe2017-09-27 08:23:15 +02002271 if (c->mfc_flags & MFC_OFFLOAD)
2272 rtm->rtm_flags |= RTNH_F_OFFLOAD;
2273
Thomas Graf92a395e2012-06-26 23:36:13 +00002274 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2275 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
2277 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002278 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002279 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2280 nla_nest_cancel(skb, mp_attr);
2281 return -EMSGSIZE;
2282 }
2283
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284 nhp->rtnh_flags = 0;
2285 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002286 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287 nhp->rtnh_len = sizeof(*nhp);
2288 }
2289 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002290
2291 nla_nest_end(skb, mp_attr);
2292
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002293 lastuse = READ_ONCE(c->mfc_un.res.lastuse);
2294 lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
2295
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002296 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2297 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2298 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002299 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002300 nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002301 RTA_PAD))
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002302 return -EMSGSIZE;
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 rtm->rtm_type = RTN_MULTICAST;
2305 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306}
2307
David S. Miller9a1b9492011-05-04 12:18:54 -07002308int ipmr_get_route(struct net *net, struct sk_buff *skb,
2309 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002310 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002313 struct mr_table *mrt;
2314 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002316 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002317 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002318 return -ENOENT;
2319
Eric Dumazeta8c94862010-10-01 16:15:08 +00002320 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002321 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002322 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002323 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002324
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002325 if (vif >= 0)
2326 cache = ipmr_cache_find_any(mrt, daddr, vif);
2327 }
Ian Morris51456b22015-04-03 09:17:26 +01002328 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002329 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002330 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002332 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002335 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002336 if (dev)
2337 vif = ipmr_find_vif(mrt, dev);
2338 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002340 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341 return -ENODEV;
2342 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002343 skb2 = skb_clone(skb, GFP_ATOMIC);
2344 if (!skb2) {
2345 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002346 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002347 return -ENOMEM;
2348 }
2349
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002350 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002351 skb_push(skb2, sizeof(struct iphdr));
2352 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002353 iph = ip_hdr(skb2);
2354 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002355 iph->saddr = saddr;
2356 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002357 iph->version = 0;
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002358 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002360 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 return err;
2362 }
2363
Eric Dumazeta8c94862010-10-01 16:15:08 +00002364 read_lock(&mrt_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002365 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002367 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368 return err;
2369}
2370
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002371static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002372 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2373 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002374{
2375 struct nlmsghdr *nlh;
2376 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002377 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002378
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002379 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002380 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002381 return -EMSGSIZE;
2382
2383 rtm = nlmsg_data(nlh);
2384 rtm->rtm_family = RTNL_FAMILY_IPMR;
2385 rtm->rtm_dst_len = 32;
2386 rtm->rtm_src_len = 32;
2387 rtm->rtm_tos = 0;
2388 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002389 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2390 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002391 rtm->rtm_type = RTN_MULTICAST;
2392 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002393 if (c->mfc_flags & MFC_STATIC)
2394 rtm->rtm_protocol = RTPROT_STATIC;
2395 else
2396 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002397 rtm->rtm_flags = 0;
2398
Jiri Benc930345e2015-03-29 16:59:25 +02002399 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2400 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002401 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002402 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2403 /* do not break the dump if cache is unresolved */
2404 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002405 goto nla_put_failure;
2406
Johannes Berg053c0952015-01-16 22:09:00 +01002407 nlmsg_end(skb, nlh);
2408 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002409
2410nla_put_failure:
2411 nlmsg_cancel(skb, nlh);
2412 return -EMSGSIZE;
2413}
2414
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002415static size_t mroute_msgsize(bool unresolved, int maxvif)
2416{
2417 size_t len =
2418 NLMSG_ALIGN(sizeof(struct rtmsg))
2419 + nla_total_size(4) /* RTA_TABLE */
2420 + nla_total_size(4) /* RTA_SRC */
2421 + nla_total_size(4) /* RTA_DST */
2422 ;
2423
2424 if (!unresolved)
2425 len = len
2426 + nla_total_size(4) /* RTA_IIF */
2427 + nla_total_size(0) /* RTA_MULTIPATH */
2428 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2429 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002430 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002431 ;
2432
2433 return len;
2434}
2435
2436static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2437 int cmd)
2438{
2439 struct net *net = read_pnet(&mrt->net);
2440 struct sk_buff *skb;
2441 int err = -ENOBUFS;
2442
2443 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2444 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002445 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002446 goto errout;
2447
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002448 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002449 if (err < 0)
2450 goto errout;
2451
2452 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2453 return;
2454
2455errout:
2456 kfree_skb(skb);
2457 if (err < 0)
2458 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2459}
2460
Julien Gomes5a645dd2017-06-20 13:54:17 -07002461static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2462{
2463 size_t len =
2464 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2465 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2466 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2467 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2468 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2469 /* IPMRA_CREPORT_PKT */
2470 + nla_total_size(payloadlen)
2471 ;
2472
2473 return len;
2474}
2475
2476static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2477{
2478 struct net *net = read_pnet(&mrt->net);
2479 struct nlmsghdr *nlh;
2480 struct rtgenmsg *rtgenm;
2481 struct igmpmsg *msg;
2482 struct sk_buff *skb;
2483 struct nlattr *nla;
2484 int payloadlen;
2485
2486 payloadlen = pkt->len - sizeof(struct igmpmsg);
2487 msg = (struct igmpmsg *)skb_network_header(pkt);
2488
2489 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2490 if (!skb)
2491 goto errout;
2492
2493 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2494 sizeof(struct rtgenmsg), 0);
2495 if (!nlh)
2496 goto errout;
2497 rtgenm = nlmsg_data(nlh);
2498 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2499 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2500 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2501 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2502 msg->im_src.s_addr) ||
2503 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2504 msg->im_dst.s_addr))
2505 goto nla_put_failure;
2506
2507 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2508 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2509 nla_data(nla), payloadlen))
2510 goto nla_put_failure;
2511
2512 nlmsg_end(skb, nlh);
2513
2514 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2515 return;
2516
2517nla_put_failure:
2518 nlmsg_cancel(skb, nlh);
2519errout:
2520 kfree_skb(skb);
2521 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2522}
2523
Donald Sharp4f75ba62017-06-28 13:58:57 -04002524static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2525 struct netlink_ext_ack *extack)
2526{
2527 struct net *net = sock_net(in_skb->sk);
2528 struct nlattr *tb[RTA_MAX + 1];
2529 struct sk_buff *skb = NULL;
2530 struct mfc_cache *cache;
2531 struct mr_table *mrt;
2532 struct rtmsg *rtm;
2533 __be32 src, grp;
2534 u32 tableid;
2535 int err;
2536
2537 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2538 rtm_ipv4_policy, extack);
2539 if (err < 0)
2540 goto errout;
2541
2542 rtm = nlmsg_data(nlh);
2543
2544 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2545 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2546 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2547
2548 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
Dan Carpenter2e3d2322017-07-12 10:56:47 +03002549 if (!mrt) {
2550 err = -ENOENT;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002551 goto errout_free;
2552 }
2553
2554 /* entries are added/deleted only under RTNL */
2555 rcu_read_lock();
2556 cache = ipmr_cache_find(mrt, src, grp);
2557 rcu_read_unlock();
2558 if (!cache) {
2559 err = -ENOENT;
2560 goto errout_free;
2561 }
2562
2563 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2564 if (!skb) {
2565 err = -ENOBUFS;
2566 goto errout_free;
2567 }
2568
2569 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2570 nlh->nlmsg_seq, cache,
2571 RTM_NEWROUTE, 0);
2572 if (err < 0)
2573 goto errout_free;
2574
2575 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2576
2577errout:
2578 return err;
2579
2580errout_free:
2581 kfree_skb(skb);
2582 goto errout;
2583}
2584
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002585static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2586{
2587 struct net *net = sock_net(skb->sk);
2588 struct mr_table *mrt;
2589 struct mfc_cache *mfc;
2590 unsigned int t = 0, s_t;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002591 unsigned int e = 0, s_e;
2592
2593 s_t = cb->args[0];
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002594 s_e = cb->args[1];
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002595
Eric Dumazeta8c94862010-10-01 16:15:08 +00002596 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002597 ipmr_for_each_table(mrt, net) {
2598 if (t < s_t)
2599 goto next_table;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002600 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) {
2601 if (e < s_e)
2602 goto next_entry;
2603 if (ipmr_fill_mroute(mrt, skb,
2604 NETLINK_CB(cb->skb).portid,
2605 cb->nlh->nlmsg_seq,
2606 mfc, RTM_NEWROUTE,
2607 NLM_F_MULTI) < 0)
2608 goto done;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002609next_entry:
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002610 e++;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002611 }
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002612 e = 0;
2613 s_e = 0;
2614
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002615 spin_lock_bh(&mfc_unres_lock);
2616 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2617 if (e < s_e)
2618 goto next_entry2;
2619 if (ipmr_fill_mroute(mrt, skb,
2620 NETLINK_CB(cb->skb).portid,
2621 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002622 mfc, RTM_NEWROUTE,
2623 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002624 spin_unlock_bh(&mfc_unres_lock);
2625 goto done;
2626 }
2627next_entry2:
2628 e++;
2629 }
2630 spin_unlock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002631 e = 0;
2632 s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002633next_table:
2634 t++;
2635 }
2636done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002637 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002638
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002639 cb->args[1] = e;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002640 cb->args[0] = t;
2641
2642 return skb->len;
2643}
2644
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002645static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2646 [RTA_SRC] = { .type = NLA_U32 },
2647 [RTA_DST] = { .type = NLA_U32 },
2648 [RTA_IIF] = { .type = NLA_U32 },
2649 [RTA_TABLE] = { .type = NLA_U32 },
2650 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2651};
2652
2653static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2654{
2655 switch (rtm_protocol) {
2656 case RTPROT_STATIC:
2657 case RTPROT_MROUTED:
2658 return true;
2659 }
2660 return false;
2661}
2662
2663static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2664{
2665 struct rtnexthop *rtnh = nla_data(nla);
2666 int remaining = nla_len(nla), vifi = 0;
2667
2668 while (rtnh_ok(rtnh, remaining)) {
2669 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2670 if (++vifi == MAXVIFS)
2671 break;
2672 rtnh = rtnh_next(rtnh, &remaining);
2673 }
2674
2675 return remaining > 0 ? -EINVAL : vifi;
2676}
2677
2678/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2679static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2680 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002681 struct mr_table **mrtret,
2682 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002683{
2684 struct net_device *dev = NULL;
2685 u32 tblid = RT_TABLE_DEFAULT;
2686 struct mr_table *mrt;
2687 struct nlattr *attr;
2688 struct rtmsg *rtm;
2689 int ret, rem;
2690
Johannes Bergfceb6432017-04-12 14:34:07 +02002691 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07002692 extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002693 if (ret < 0)
2694 goto out;
2695 rtm = nlmsg_data(nlh);
2696
2697 ret = -EINVAL;
2698 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2699 rtm->rtm_type != RTN_MULTICAST ||
2700 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2701 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2702 goto out;
2703
2704 memset(mfcc, 0, sizeof(*mfcc));
2705 mfcc->mfcc_parent = -1;
2706 ret = 0;
2707 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2708 switch (nla_type(attr)) {
2709 case RTA_SRC:
2710 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2711 break;
2712 case RTA_DST:
2713 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2714 break;
2715 case RTA_IIF:
2716 dev = __dev_get_by_index(net, nla_get_u32(attr));
2717 if (!dev) {
2718 ret = -ENODEV;
2719 goto out;
2720 }
2721 break;
2722 case RTA_MULTIPATH:
2723 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2724 ret = -EINVAL;
2725 goto out;
2726 }
2727 break;
2728 case RTA_PREFSRC:
2729 ret = 1;
2730 break;
2731 case RTA_TABLE:
2732 tblid = nla_get_u32(attr);
2733 break;
2734 }
2735 }
2736 mrt = ipmr_get_table(net, tblid);
2737 if (!mrt) {
2738 ret = -ENOENT;
2739 goto out;
2740 }
2741 *mrtret = mrt;
2742 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2743 if (dev)
2744 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2745
2746out:
2747 return ret;
2748}
2749
2750/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002751static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2752 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002753{
2754 struct net *net = sock_net(skb->sk);
2755 int ret, mrtsock, parent;
2756 struct mr_table *tbl;
2757 struct mfcctl mfcc;
2758
2759 mrtsock = 0;
2760 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002761 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002762 if (ret < 0)
2763 return ret;
2764
2765 parent = ret ? mfcc.mfcc_parent : -1;
2766 if (nlh->nlmsg_type == RTM_NEWROUTE)
2767 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2768 else
2769 return ipmr_mfc_delete(tbl, &mfcc, parent);
2770}
2771
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002772static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2773{
2774 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2775
2776 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2777 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2778 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2779 mrt->mroute_reg_vif_num) ||
2780 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2781 mrt->mroute_do_assert) ||
2782 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim))
2783 return false;
2784
2785 return true;
2786}
2787
2788static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2789{
2790 struct nlattr *vif_nest;
2791 struct vif_device *vif;
2792
2793 /* if the VIF doesn't exist just continue */
2794 if (!VIF_EXISTS(mrt, vifid))
2795 return true;
2796
2797 vif = &mrt->vif_table[vifid];
2798 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2799 if (!vif_nest)
2800 return false;
2801 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2802 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2803 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2804 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2805 IPMRA_VIFA_PAD) ||
2806 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2807 IPMRA_VIFA_PAD) ||
2808 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2809 IPMRA_VIFA_PAD) ||
2810 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2811 IPMRA_VIFA_PAD) ||
2812 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2813 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2814 nla_nest_cancel(skb, vif_nest);
2815 return false;
2816 }
2817 nla_nest_end(skb, vif_nest);
2818
2819 return true;
2820}
2821
2822static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2823{
2824 struct net *net = sock_net(skb->sk);
2825 struct nlmsghdr *nlh = NULL;
2826 unsigned int t = 0, s_t;
2827 unsigned int e = 0, s_e;
2828 struct mr_table *mrt;
2829
2830 s_t = cb->args[0];
2831 s_e = cb->args[1];
2832
2833 ipmr_for_each_table(mrt, net) {
2834 struct nlattr *vifs, *af;
2835 struct ifinfomsg *hdr;
2836 u32 i;
2837
2838 if (t < s_t)
2839 goto skip_table;
2840 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2841 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2842 sizeof(*hdr), NLM_F_MULTI);
2843 if (!nlh)
2844 break;
2845
2846 hdr = nlmsg_data(nlh);
2847 memset(hdr, 0, sizeof(*hdr));
2848 hdr->ifi_family = RTNL_FAMILY_IPMR;
2849
2850 af = nla_nest_start(skb, IFLA_AF_SPEC);
2851 if (!af) {
2852 nlmsg_cancel(skb, nlh);
2853 goto out;
2854 }
2855
2856 if (!ipmr_fill_table(mrt, skb)) {
2857 nlmsg_cancel(skb, nlh);
2858 goto out;
2859 }
2860
2861 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2862 if (!vifs) {
2863 nla_nest_end(skb, af);
2864 nlmsg_end(skb, nlh);
2865 goto out;
2866 }
2867 for (i = 0; i < mrt->maxvif; i++) {
2868 if (e < s_e)
2869 goto skip_entry;
2870 if (!ipmr_fill_vif(mrt, i, skb)) {
2871 nla_nest_end(skb, vifs);
2872 nla_nest_end(skb, af);
2873 nlmsg_end(skb, nlh);
2874 goto out;
2875 }
2876skip_entry:
2877 e++;
2878 }
2879 s_e = 0;
2880 e = 0;
2881 nla_nest_end(skb, vifs);
2882 nla_nest_end(skb, af);
2883 nlmsg_end(skb, nlh);
2884skip_table:
2885 t++;
2886 }
2887
2888out:
2889 cb->args[1] = e;
2890 cb->args[0] = t;
2891
2892 return skb->len;
2893}
2894
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002895#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002896/* The /proc interfaces to multicast routing :
2897 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002898 */
2899struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002900 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002901 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002902 int ct;
2903};
2904
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002905static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2906 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002907 loff_t pos)
2908{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002909 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002910
2911 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2912 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002913 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002914 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002915 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916 }
2917 return NULL;
2918}
2919
2920static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002921 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002922{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002923 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002924 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002925 struct mr_table *mrt;
2926
2927 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002928 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002929 return ERR_PTR(-ENOENT);
2930
2931 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002932
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002934 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 : SEQ_START_TOKEN;
2936}
2937
2938static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2939{
2940 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002941 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002942 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943
2944 ++*pos;
2945 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002946 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002947
Patrick McHardy0c122952010-04-13 05:03:22 +00002948 while (++iter->ct < mrt->maxvif) {
2949 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002950 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002951 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952 }
2953 return NULL;
2954}
2955
2956static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002957 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002958{
2959 read_unlock(&mrt_lock);
2960}
2961
2962static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2963{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002964 struct ipmr_vif_iter *iter = seq->private;
2965 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002966
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002968 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002969 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2970 } else {
2971 const struct vif_device *vif = v;
2972 const char *name = vif->dev ? vif->dev->name : "none";
2973
2974 seq_printf(seq,
Alexey Dobriyan5b5e0922017-02-27 14:30:02 -08002975 "%2zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002976 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002977 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 vif->bytes_out, vif->pkt_out,
2979 vif->flags, vif->local, vif->remote);
2980 }
2981 return 0;
2982}
2983
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002984static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985 .start = ipmr_vif_seq_start,
2986 .next = ipmr_vif_seq_next,
2987 .stop = ipmr_vif_seq_stop,
2988 .show = ipmr_vif_seq_show,
2989};
2990
2991static int ipmr_vif_open(struct inode *inode, struct file *file)
2992{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002993 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2994 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002995}
2996
Arjan van de Ven9a321442007-02-12 00:55:35 -08002997static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 .owner = THIS_MODULE,
2999 .open = ipmr_vif_open,
3000 .read = seq_read,
3001 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003002 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003};
3004
3005struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003006 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003007 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00003008 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009};
3010
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003011static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
3012 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003013{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003014 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 struct mfc_cache *mfc;
3016
Eric Dumazeta8c94862010-10-01 16:15:08 +00003017 rcu_read_lock();
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003018 it->cache = &mrt->mfc_cache_list;
3019 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
3020 if (pos-- == 0)
3021 return mfc;
Eric Dumazeta8c94862010-10-01 16:15:08 +00003022 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003023
Linus Torvalds1da177e2005-04-16 15:20:36 -07003024 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00003025 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00003026 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00003027 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028 return mfc;
3029 spin_unlock_bh(&mfc_unres_lock);
3030
3031 it->cache = NULL;
3032 return NULL;
3033}
3034
3035
3036static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
3037{
3038 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003039 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003040 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003041
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003042 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01003043 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003044 return ERR_PTR(-ENOENT);
3045
3046 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047 it->cache = NULL;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003048 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049 : SEQ_START_TOKEN;
3050}
3051
3052static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3053{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003055 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003056 struct mr_table *mrt = it->mrt;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003057 struct mfc_cache *mfc = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058
3059 ++*pos;
3060
3061 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003062 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063
Patrick McHardy862465f2010-04-13 05:03:21 +00003064 if (mfc->list.next != it->cache)
3065 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003066
Patrick McHardy0c122952010-04-13 05:03:22 +00003067 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 goto end_of_list;
3069
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00003071 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00003072 it->cache = &mrt->mfc_unres_queue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003073
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00003075 if (!list_empty(it->cache))
3076 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00003078end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 spin_unlock_bh(&mfc_unres_lock);
3080 it->cache = NULL;
3081
3082 return NULL;
3083}
3084
3085static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
3086{
3087 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003088 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089
Patrick McHardy0c122952010-04-13 05:03:22 +00003090 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003091 spin_unlock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003092 else if (it->cache == &mrt->mfc_cache_list)
Eric Dumazeta8c94862010-10-01 16:15:08 +00003093 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094}
3095
3096static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
3097{
3098 int n;
3099
3100 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003101 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
3103 } else {
3104 const struct mfc_cache *mfc = v;
3105 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003106 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003107
Eric Dumazet0eae88f2010-04-20 19:06:52 -07003108 seq_printf(seq, "%08X %08X %-3hd",
3109 (__force u32) mfc->mfc_mcastgrp,
3110 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003111 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003112
Patrick McHardy0c122952010-04-13 05:03:22 +00003113 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003114 seq_printf(seq, " %8lu %8lu %8lu",
3115 mfc->mfc_un.res.pkt,
3116 mfc->mfc_un.res.bytes,
3117 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08003118 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00003119 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00003120 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00003121 mfc->mfc_un.res.ttls[n] < 255)
3122 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003123 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 n, mfc->mfc_un.res.ttls[n]);
3125 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003126 } else {
3127 /* unresolved mfc_caches don't contain
3128 * pkt, bytes and wrong_if values
3129 */
3130 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 }
3132 seq_putc(seq, '\n');
3133 }
3134 return 0;
3135}
3136
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003137static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 .start = ipmr_mfc_seq_start,
3139 .next = ipmr_mfc_seq_next,
3140 .stop = ipmr_mfc_seq_stop,
3141 .show = ipmr_mfc_seq_show,
3142};
3143
3144static int ipmr_mfc_open(struct inode *inode, struct file *file)
3145{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003146 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
3147 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148}
3149
Arjan van de Ven9a321442007-02-12 00:55:35 -08003150static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 .owner = THIS_MODULE,
3152 .open = ipmr_mfc_open,
3153 .read = seq_read,
3154 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003155 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003157#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158
3159#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00003160static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003161 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07003162 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163};
3164#endif
3165
Yotam Gigi4d65b942017-09-27 08:23:13 +02003166static unsigned int ipmr_seq_read(struct net *net)
3167{
3168 ASSERT_RTNL();
3169
3170 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
3171}
3172
3173static int ipmr_dump(struct net *net, struct notifier_block *nb)
3174{
3175 struct mr_table *mrt;
3176 int err;
3177
3178 err = ipmr_rules_dump(net, nb);
3179 if (err)
3180 return err;
3181
3182 ipmr_for_each_table(mrt, net) {
3183 struct vif_device *v = &mrt->vif_table[0];
3184 struct mfc_cache *mfc;
3185 int vifi;
3186
3187 /* Notifiy on table VIF entries */
3188 read_lock(&mrt_lock);
3189 for (vifi = 0; vifi < mrt->maxvif; vifi++, v++) {
3190 if (!v->dev)
3191 continue;
3192
3193 call_ipmr_vif_entry_notifier(nb, net, FIB_EVENT_VIF_ADD,
3194 v, vifi, mrt->id);
3195 }
3196 read_unlock(&mrt_lock);
3197
3198 /* Notify on table MFC entries */
3199 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
3200 call_ipmr_mfc_entry_notifier(nb, net,
3201 FIB_EVENT_ENTRY_ADD, mfc,
3202 mrt->id);
3203 }
3204
3205 return 0;
3206}
3207
3208static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3209 .family = RTNL_FAMILY_IPMR,
3210 .fib_seq_read = ipmr_seq_read,
3211 .fib_dump = ipmr_dump,
3212 .owner = THIS_MODULE,
3213};
3214
3215int __net_init ipmr_notifier_init(struct net *net)
3216{
3217 struct fib_notifier_ops *ops;
3218
3219 net->ipv4.ipmr_seq = 0;
3220
3221 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3222 if (IS_ERR(ops))
3223 return PTR_ERR(ops);
3224 net->ipv4.ipmr_notifier_ops = ops;
3225
3226 return 0;
3227}
3228
3229static void __net_exit ipmr_notifier_exit(struct net *net)
3230{
3231 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3232 net->ipv4.ipmr_notifier_ops = NULL;
3233}
3234
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01003235/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00003236static int __net_init ipmr_net_init(struct net *net)
3237{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003238 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00003239
Yotam Gigi4d65b942017-09-27 08:23:13 +02003240 err = ipmr_notifier_init(net);
3241 if (err)
3242 goto ipmr_notifier_fail;
3243
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003244 err = ipmr_rules_init(net);
3245 if (err < 0)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003246 goto ipmr_rules_fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003247
3248#ifdef CONFIG_PROC_FS
3249 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00003250 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003251 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00003252 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003253 goto proc_cache_fail;
3254#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00003255 return 0;
3256
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003257#ifdef CONFIG_PROC_FS
3258proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00003259 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003260proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003261 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003262#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003263ipmr_rules_fail:
3264 ipmr_notifier_exit(net);
3265ipmr_notifier_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003266 return err;
3267}
3268
3269static void __net_exit ipmr_net_exit(struct net *net)
3270{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003271#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003272 remove_proc_entry("ip_mr_cache", net->proc_net);
3273 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003274#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003275 ipmr_notifier_exit(net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003276 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00003277}
3278
3279static struct pernet_operations ipmr_net_ops = {
3280 .init = ipmr_net_init,
3281 .exit = ipmr_net_exit,
3282};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003283
Wang Chen03d2f892008-07-03 12:13:36 +08003284int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003285{
Wang Chen03d2f892008-07-03 12:13:36 +08003286 int err;
3287
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3289 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00003290 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003291 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08003292
Benjamin Therycf958ae32009-01-22 04:56:16 +00003293 err = register_pernet_subsys(&ipmr_net_ops);
3294 if (err)
3295 goto reg_pernet_fail;
3296
Wang Chen03d2f892008-07-03 12:13:36 +08003297 err = register_netdevice_notifier(&ip_mr_notifier);
3298 if (err)
3299 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07003300#ifdef CONFIG_IP_PIMSM_V2
3301 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00003302 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07003303 err = -EAGAIN;
3304 goto add_proto_fail;
3305 }
3306#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00003307 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003308 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003309 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003310 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003311 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003312 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03003313
3314 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
Florian Westphalb97bac62017-08-09 20:41:48 +02003315 NULL, ipmr_rtm_dumplink, 0);
Wang Chen03d2f892008-07-03 12:13:36 +08003316 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003317
Tom Goff403dbb92009-06-14 03:16:13 -07003318#ifdef CONFIG_IP_PIMSM_V2
3319add_proto_fail:
3320 unregister_netdevice_notifier(&ip_mr_notifier);
3321#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08003322reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003323 unregister_pernet_subsys(&ipmr_net_ops);
3324reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08003325 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08003326 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003327}