blob: 3f7515086e85729913350d0010e6eb044c90f42c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Linux Consultancy and Custom Driver Development
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fixes:
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
22 * overflow.
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
Gilles Espinassef77f13e2010-03-29 15:41:47 +020025 * Relax this requirement to work with older peers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 */
28
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
Alexey Dobriyan08009a72018-02-24 21:20:33 +030031#include <linux/cache.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080032#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <linux/errno.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/mm.h>
35#include <linux/kernel.h>
36#include <linux/fcntl.h>
37#include <linux/stat.h>
38#include <linux/socket.h>
39#include <linux/in.h>
40#include <linux/inet.h>
41#include <linux/netdevice.h>
42#include <linux/inetdevice.h>
43#include <linux/igmp.h>
44#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
46#include <linux/mroute.h>
47#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080048#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020050#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/ip.h>
52#include <net/protocol.h>
53#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020054#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/icmp.h>
56#include <net/udp.h>
57#include <net/raw.h>
58#include <linux/notifier.h>
59#include <linux/if_arp.h>
60#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000061#include <linux/compat.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040062#include <linux/export.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000063#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070065#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000066#include <net/fib_rules.h>
Nicolas Dichteld67b8c62012-12-04 01:13:35 +000067#include <linux/netconf.h>
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +010068#include <net/nexthop.h>
Yotam Gigi5d8b3e62017-10-03 09:58:07 +020069#include <net/switchdev.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Patrick McHardyf0ad0862010-04-13 05:03:23 +000071struct ipmr_rule {
72 struct fib_rule common;
73};
74
75struct ipmr_result {
76 struct mr_table *mrt;
77};
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000080 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static DEFINE_RWLOCK(mrt_lock);
84
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +010085/* Multicast router control variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Special spinlock for queue of unresolved entries */
88static DEFINE_SPINLOCK(mfc_unres_lock);
89
90/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000091 * entries is changed only in process context and protected
92 * with weak lock mrt_lock. Queue of unresolved entries is protected
93 * with strong spinlock mfc_unres_lock.
94 *
95 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
97
Alexey Dobriyan08009a72018-02-24 21:20:33 +030098static struct kmem_cache *mrt_cachep __ro_after_init;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000100static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000101static void ipmr_free_table(struct mr_table *mrt);
102
Rami Rosenc4854ec2013-07-20 15:09:28 +0300103static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -0400104 struct net_device *dev, struct sk_buff *skb,
105 struct mfc_cache *cache, int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000106static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000107 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200108static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Yuval Mintz494fff52018-02-28 23:29:34 +0200109 struct mr_mfc *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);
Kees Cooke99e88a2017-10-16 14:43:17 -0700114static void ipmr_expire_process(struct timer_list *t);
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}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200277
278bool ipmr_rule_default(const struct fib_rule *rule)
279{
280 return fib_rule_matchall(rule) && rule->table == RT_TABLE_DEFAULT;
281}
282EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000283#else
284#define ipmr_for_each_table(mrt, net) \
285 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
286
287static struct mr_table *ipmr_get_table(struct net *net, u32 id)
288{
289 return net->ipv4.mrt;
290}
291
David S. Millerda919812011-03-12 02:04:50 -0500292static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000293 struct mr_table **mrt)
294{
295 *mrt = net->ipv4.mrt;
296 return 0;
297}
298
299static int __net_init ipmr_rules_init(struct net *net)
300{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100301 struct mr_table *mrt;
302
303 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
304 if (IS_ERR(mrt))
305 return PTR_ERR(mrt);
306 net->ipv4.mrt = mrt;
307 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000308}
309
310static void __net_exit ipmr_rules_exit(struct net *net)
311{
WANG Conged785302015-03-31 11:01:45 -0700312 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000313 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700314 net->ipv4.mrt = NULL;
315 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000316}
Yotam Gigi4d65b942017-09-27 08:23:13 +0200317
318static int ipmr_rules_dump(struct net *net, struct notifier_block *nb)
319{
320 return 0;
321}
322
323static unsigned int ipmr_rules_seq_read(struct net *net)
324{
325 return 0;
326}
Yotam Gigi478e4c22017-09-27 08:23:16 +0200327
328bool ipmr_rule_default(const struct fib_rule *rule)
329{
330 return true;
331}
332EXPORT_SYMBOL(ipmr_rule_default);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000333#endif
334
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100335static inline int ipmr_hash_cmp(struct rhashtable_compare_arg *arg,
336 const void *ptr)
337{
338 const struct mfc_cache_cmp_arg *cmparg = arg->key;
339 struct mfc_cache *c = (struct mfc_cache *)ptr;
340
341 return cmparg->mfc_mcastgrp != c->mfc_mcastgrp ||
342 cmparg->mfc_origin != c->mfc_origin;
343}
344
345static const struct rhashtable_params ipmr_rht_params = {
Yuval Mintz494fff52018-02-28 23:29:34 +0200346 .head_offset = offsetof(struct mr_mfc, mnode),
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100347 .key_offset = offsetof(struct mfc_cache, cmparg),
348 .key_len = sizeof(struct mfc_cache_cmp_arg),
349 .nelem_hint = 3,
350 .locks_mul = 1,
351 .obj_cmpfn = ipmr_hash_cmp,
352 .automatic_shrinking = true,
353};
354
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200355static void ipmr_new_table_set(struct mr_table *mrt,
356 struct net *net)
357{
358#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
359 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
360#endif
361}
362
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000363static struct mr_table *ipmr_new_table(struct net *net, u32 id)
364{
365 struct mr_table *mrt;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000366
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100367 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
368 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
369 return ERR_PTR(-EINVAL);
370
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000371 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100372 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000373 return mrt;
374
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200375 return mr_table_alloc(net, id, &ipmr_rht_params,
376 ipmr_expire_process, ipmr_new_table_set);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000377}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000379static void ipmr_free_table(struct mr_table *mrt)
380{
381 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100382 mroute_clean_tables(mrt, true);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100383 rhltable_destroy(&mrt->mfc_hash);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000384 kfree(mrt);
385}
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
388
Wang Chend6070322008-07-14 20:55:26 -0700389static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
390{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000391 struct net *net = dev_net(dev);
392
Wang Chend6070322008-07-14 20:55:26 -0700393 dev_close(dev);
394
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000395 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700396 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800397 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700398 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700399 struct ip_tunnel_parm p;
400
401 memset(&p, 0, sizeof(p));
402 p.iph.daddr = v->vifc_rmt_addr.s_addr;
403 p.iph.saddr = v->vifc_lcl_addr.s_addr;
404 p.iph.version = 4;
405 p.iph.ihl = 5;
406 p.iph.protocol = IPPROTO_IPIP;
407 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
408 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
409
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800410 if (ops->ndo_do_ioctl) {
411 mm_segment_t oldfs = get_fs();
412
413 set_fs(KERNEL_DS);
414 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
415 set_fs(oldfs);
416 }
Wang Chend6070322008-07-14 20:55:26 -0700417 }
418}
419
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100420/* Initialize ipmr pimreg/tunnel in_device */
421static bool ipmr_init_vif_indev(const struct net_device *dev)
422{
423 struct in_device *in_dev;
424
425 ASSERT_RTNL();
426
427 in_dev = __in_dev_get_rtnl(dev);
428 if (!in_dev)
429 return false;
430 ipv4_devconf_setall(in_dev);
431 neigh_parms_data_state_setall(in_dev->arp_parms);
432 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
433
434 return true;
435}
436
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100437static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 struct net_device *dev;
440
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000441 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
443 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800444 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 int err;
446 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
449 memset(&p, 0, sizeof(p));
450 p.iph.daddr = v->vifc_rmt_addr.s_addr;
451 p.iph.saddr = v->vifc_lcl_addr.s_addr;
452 p.iph.version = 4;
453 p.iph.ihl = 5;
454 p.iph.protocol = IPPROTO_IPIP;
455 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800456 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800458 if (ops->ndo_do_ioctl) {
459 mm_segment_t oldfs = get_fs();
460
461 set_fs(KERNEL_DS);
462 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
463 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000464 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800465 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 dev = NULL;
468
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000469 if (err == 0 &&
470 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100472 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (dev_open(dev))
475 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700476 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
478 }
479 return dev;
480
481failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 unregister_netdevice(dev);
483 return NULL;
484}
485
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100486#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000487static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000489 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000490 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500491 struct flowi4 fl4 = {
492 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700493 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500494 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000495 };
496 int err;
497
David S. Millerda919812011-03-12 02:04:50 -0500498 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000499 if (err < 0) {
500 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000501 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000502 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700505 dev->stats.tx_bytes += skb->len;
506 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000507 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 read_unlock(&mrt_lock);
509 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000510 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511}
512
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200513static int reg_vif_get_iflink(const struct net_device *dev)
514{
515 return 0;
516}
517
Stephen Hemminger007c3832008-11-20 20:28:35 -0800518static const struct net_device_ops reg_vif_netdev_ops = {
519 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200520 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800521};
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523static void reg_vif_setup(struct net_device *dev)
524{
525 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800526 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530528 dev->netdev_ops = &reg_vif_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400529 dev->needs_free_netdev = true;
Tom Goff403dbb92009-06-14 03:16:13 -0700530 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531}
532
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000533static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534{
535 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000536 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000538 if (mrt->id == RT_TABLE_DEFAULT)
539 sprintf(name, "pimreg");
540 else
541 sprintf(name, "pimreg%u", mrt->id);
542
Tom Gundersenc835a672014-07-14 16:37:24 +0200543 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Ian Morris51456b22015-04-03 09:17:26 +0100545 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 return NULL;
547
Tom Goff403dbb92009-06-14 03:16:13 -0700548 dev_net_set(dev, net);
549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (register_netdevice(dev)) {
551 free_netdev(dev);
552 return NULL;
553 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100555 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 if (dev_open(dev))
558 goto failure;
559
Wang Chen7dc00c82008-07-14 20:56:34 -0700560 dev_hold(dev);
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 return dev;
563
564failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 unregister_netdevice(dev);
566 return NULL;
567}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100568
569/* called with rcu_read_lock() */
570static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
571 unsigned int pimlen)
572{
573 struct net_device *reg_dev = NULL;
574 struct iphdr *encap;
575
576 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100577 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100578 * a. packet is really sent to a multicast group
579 * b. packet is not a NULL-REGISTER
580 * c. packet is not truncated
581 */
582 if (!ipv4_is_multicast(encap->daddr) ||
583 encap->tot_len == 0 ||
584 ntohs(encap->tot_len) + pimlen > skb->len)
585 return 1;
586
587 read_lock(&mrt_lock);
588 if (mrt->mroute_reg_vif_num >= 0)
589 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
590 read_unlock(&mrt_lock);
591
592 if (!reg_dev)
593 return 1;
594
595 skb->mac_header = skb->network_header;
596 skb_pull(skb, (u8 *)encap - skb->data);
597 skb_reset_network_header(skb);
598 skb->protocol = htons(ETH_P_IP);
599 skb->ip_summed = CHECKSUM_NONE;
600
601 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
602
603 netif_rx(skb);
604
605 return NET_RX_SUCCESS;
606}
607#else
608static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
609{
610 return NULL;
611}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612#endif
613
Yotam Gigi4d65b942017-09-27 08:23:13 +0200614static int call_ipmr_vif_entry_notifier(struct notifier_block *nb,
615 struct net *net,
616 enum fib_event_type event_type,
617 struct vif_device *vif,
618 vifi_t vif_index, u32 tb_id)
619{
620 struct vif_entry_notifier_info info = {
621 .info = {
622 .family = RTNL_FAMILY_IPMR,
623 .net = net,
624 },
625 .dev = vif->dev,
626 .vif_index = vif_index,
627 .vif_flags = vif->flags,
628 .tb_id = tb_id,
629 };
630
631 return call_fib_notifier(nb, net, event_type, &info.info);
632}
633
Yotam Gigib3620532017-09-27 08:23:14 +0200634static int call_ipmr_vif_entry_notifiers(struct net *net,
635 enum fib_event_type event_type,
636 struct vif_device *vif,
637 vifi_t vif_index, u32 tb_id)
638{
639 struct vif_entry_notifier_info info = {
640 .info = {
641 .family = RTNL_FAMILY_IPMR,
642 .net = net,
643 },
644 .dev = vif->dev,
645 .vif_index = vif_index,
646 .vif_flags = vif->flags,
647 .tb_id = tb_id,
648 };
649
650 ASSERT_RTNL();
651 net->ipv4.ipmr_seq++;
652 return call_fib_notifiers(net, event_type, &info.info);
653}
654
Yotam Gigi4d65b942017-09-27 08:23:13 +0200655static int call_ipmr_mfc_entry_notifier(struct notifier_block *nb,
656 struct net *net,
657 enum fib_event_type event_type,
658 struct mfc_cache *mfc, u32 tb_id)
659{
660 struct mfc_entry_notifier_info info = {
661 .info = {
662 .family = RTNL_FAMILY_IPMR,
663 .net = net,
664 },
665 .mfc = mfc,
666 .tb_id = tb_id
667 };
668
669 return call_fib_notifier(nb, net, event_type, &info.info);
670}
671
Yotam Gigib3620532017-09-27 08:23:14 +0200672static int call_ipmr_mfc_entry_notifiers(struct net *net,
673 enum fib_event_type event_type,
674 struct mfc_cache *mfc, u32 tb_id)
675{
676 struct mfc_entry_notifier_info info = {
677 .info = {
678 .family = RTNL_FAMILY_IPMR,
679 .net = net,
680 },
681 .mfc = mfc,
682 .tb_id = tb_id
683 };
684
685 ASSERT_RTNL();
686 net->ipv4.ipmr_seq++;
687 return call_fib_notifiers(net, event_type, &info.info);
688}
689
Ben Hutchings2c530402012-07-10 10:55:09 +0000690/**
691 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700692 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000694static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000695 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696{
Yotam Gigib3620532017-09-27 08:23:14 +0200697 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct vif_device *v;
699 struct net_device *dev;
700 struct in_device *in_dev;
701
Patrick McHardy0c122952010-04-13 05:03:22 +0000702 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 return -EADDRNOTAVAIL;
704
Patrick McHardy0c122952010-04-13 05:03:22 +0000705 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Yotam Gigib3620532017-09-27 08:23:14 +0200707 if (VIF_EXISTS(mrt, vifi))
708 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
709 mrt->id);
710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 write_lock_bh(&mrt_lock);
712 dev = v->dev;
713 v->dev = NULL;
714
715 if (!dev) {
716 write_unlock_bh(&mrt_lock);
717 return -EADDRNOTAVAIL;
718 }
719
Patrick McHardy0c122952010-04-13 05:03:22 +0000720 if (vifi == mrt->mroute_reg_vif_num)
721 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000723 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000725
726 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000727 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 break;
729 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000730 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 }
732
733 write_unlock_bh(&mrt_lock);
734
735 dev_set_allmulti(dev, -1);
736
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000737 in_dev = __in_dev_get_rtnl(dev);
738 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700739 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -0700740 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000741 NETCONFA_MC_FORWARDING,
742 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 ip_rt_multicast_event(in_dev);
744 }
745
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000746 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000747 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749 dev_put(dev);
750 return 0;
751}
752
Eric Dumazeta8c94862010-10-01 16:15:08 +0000753static void ipmr_cache_free_rcu(struct rcu_head *head)
754{
Yuval Mintz494fff52018-02-28 23:29:34 +0200755 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000756
Yuval Mintz494fff52018-02-28 23:29:34 +0200757 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000758}
759
Yotam Gigi310ebbb2017-09-27 08:23:12 +0200760void ipmr_cache_free(struct mfc_cache *c)
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000761{
Yuval Mintz494fff52018-02-28 23:29:34 +0200762 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000763}
Yotam Gigi310ebbb2017-09-27 08:23:12 +0200764EXPORT_SYMBOL(ipmr_cache_free);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000767 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000769static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200771 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700773 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
Patrick McHardy0c122952010-04-13 05:03:22 +0000775 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776
Yuval Mintz494fff52018-02-28 23:29:34 +0200777 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700778 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200779 struct nlmsghdr *nlh = skb_pull(skb,
780 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000782 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000784 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700785 e->error = -ETIMEDOUT;
786 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700787
Eric W. Biederman15e47302012-09-07 20:12:54 +0000788 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000789 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000791 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 }
793
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000794 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
Patrick McHardye258beb2010-04-13 05:03:19 +0000797/* Timer process for the unresolved queue. */
Kees Cooke99e88a2017-10-16 14:43:17 -0700798static void ipmr_expire_process(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
Kees Cooke99e88a2017-10-16 14:43:17 -0700800 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
Yuval Mintz494fff52018-02-28 23:29:34 +0200801 struct mr_mfc *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 unsigned long expires;
Yuval Mintz494fff52018-02-28 23:29:34 +0200803 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000806 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 return;
808 }
809
Patrick McHardy0c122952010-04-13 05:03:22 +0000810 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 goto out;
812
813 now = jiffies;
814 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Patrick McHardy0c122952010-04-13 05:03:22 +0000816 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (time_after(c->mfc_un.unres.expires, now)) {
818 unsigned long interval = c->mfc_un.unres.expires - now;
819 if (interval < expires)
820 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 continue;
822 }
823
Patrick McHardy862465f2010-04-13 05:03:21 +0000824 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +0200825 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
826 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 }
828
Patrick McHardy0c122952010-04-13 05:03:22 +0000829 if (!list_empty(&mrt->mfc_unres_queue))
830 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
832out:
833 spin_unlock(&mfc_unres_lock);
834}
835
836/* Fill oifs list. It is called under write locked mrt_lock. */
Yuval Mintz494fff52018-02-28 23:29:34 +0200837static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000838 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
840 int vifi;
841
842 cache->mfc_un.res.minvif = MAXVIFS;
843 cache->mfc_un.res.maxvif = 0;
844 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
845
Patrick McHardy0c122952010-04-13 05:03:22 +0000846 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
847 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000848 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
850 if (cache->mfc_un.res.minvif > vifi)
851 cache->mfc_un.res.minvif = vifi;
852 if (cache->mfc_un.res.maxvif <= vifi)
853 cache->mfc_un.res.maxvif = vifi + 1;
854 }
855 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200856 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Patrick McHardy0c122952010-04-13 05:03:22 +0000859static int vif_add(struct net *net, struct mr_table *mrt,
860 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861{
862 int vifi = vifc->vifc_vifi;
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200863 struct switchdev_attr attr = {
864 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
865 };
Patrick McHardy0c122952010-04-13 05:03:22 +0000866 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 struct net_device *dev;
868 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700869 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
871 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000872 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 return -EADDRINUSE;
874
875 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100877 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100878 return -EINVAL;
879 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 * All the packets will be sent to the daemon
881 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000882 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000884 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 if (!dev)
886 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700887 err = dev_set_allmulti(dev, 1);
888 if (err) {
889 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700890 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700891 return err;
892 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900894 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000895 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 if (!dev)
897 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700898 err = dev_set_allmulti(dev, 1);
899 if (err) {
900 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700901 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700902 return err;
903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000905 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000907 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
908 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100909 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000910 dev_put(dev);
911 return -EADDRNOTAVAIL;
912 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000913 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000914 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000915 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (!dev)
917 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700918 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700919 if (err) {
920 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700921 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 break;
924 default:
925 return -EINVAL;
926 }
927
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000928 in_dev = __in_dev_get_rtnl(dev);
929 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000930 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000932 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700933 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700934 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
935 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 ip_rt_multicast_event(in_dev);
937
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000938 /* Fill in the VIF structures */
Yuval Mintz6853f212018-02-28 23:29:29 +0200939 vif_device_init(v, dev, vifc->vifc_rate_limit,
940 vifc->vifc_threshold,
941 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
942 (VIFF_TUNNEL | VIFF_REGISTER));
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000943
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200944 attr.orig_dev = dev;
945 if (!switchdev_port_attr_get(dev, &attr)) {
946 memcpy(v->dev_parent_id.id, attr.u.ppid.id, attr.u.ppid.id_len);
947 v->dev_parent_id.id_len = attr.u.ppid.id_len;
948 } else {
949 v->dev_parent_id.id_len = 0;
950 }
Yuval Mintz6853f212018-02-28 23:29:29 +0200951
Jianjun Kongc354e122008-11-03 00:28:02 -0800952 v->local = vifc->vifc_lcl_addr.s_addr;
953 v->remote = vifc->vifc_rmt_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954
955 /* And finish update writing critical data */
956 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800957 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000958 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000959 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000960 if (vifi+1 > mrt->maxvif)
961 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +0200963 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 return 0;
965}
966
Eric Dumazeta8c94862010-10-01 16:15:08 +0000967/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000968static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000969 __be32 origin,
970 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100972 struct mfc_cache_cmp_arg arg = {
973 .mfc_mcastgrp = mcastgrp,
974 .mfc_origin = origin
975 };
976 struct rhlist_head *tmp, *list;
Yuval Mintz494fff52018-02-28 23:29:34 +0200977 struct mr_mfc *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100979 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
980 rhl_for_each_entry_rcu(c, tmp, list, mnode)
Yuval Mintz494fff52018-02-28 23:29:34 +0200981 return (struct mfc_cache *)c;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100982
Patrick McHardy862465f2010-04-13 05:03:21 +0000983 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000986/* Look for a (*,*,oif) entry */
987static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
988 int vifi)
989{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100990 struct mfc_cache_cmp_arg arg = {
991 .mfc_mcastgrp = htonl(INADDR_ANY),
992 .mfc_origin = htonl(INADDR_ANY)
993 };
994 struct rhlist_head *tmp, *list;
Yuval Mintz494fff52018-02-28 23:29:34 +0200995 struct mr_mfc *c;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000996
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100997 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
998 rhl_for_each_entry_rcu(c, tmp, list, mnode)
999 if (c->mfc_un.res.ttls[vifi] < 255)
Yuval Mintz494fff52018-02-28 23:29:34 +02001000 return (struct mfc_cache *)c;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001001
1002 return NULL;
1003}
1004
1005/* Look for a (*,G) entry */
1006static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
1007 __be32 mcastgrp, int vifi)
1008{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001009 struct mfc_cache_cmp_arg arg = {
1010 .mfc_mcastgrp = mcastgrp,
1011 .mfc_origin = htonl(INADDR_ANY)
1012 };
1013 struct rhlist_head *tmp, *list;
Yuval Mintz494fff52018-02-28 23:29:34 +02001014 struct mr_mfc *c;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001015
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001016 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001017 goto skip;
1018
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001019 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
1020 rhl_for_each_entry_rcu(c, tmp, list, mnode) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001021 struct mfc_cache *proxy;
1022
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001023 if (c->mfc_un.res.ttls[vifi] < 255)
Yuval Mintz494fff52018-02-28 23:29:34 +02001024 return (struct mfc_cache *)c;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001025
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001026 /* It's ok if the vifi is part of the static tree */
1027 proxy = ipmr_cache_find_any_parent(mrt, c->mfc_parent);
Yuval Mintz494fff52018-02-28 23:29:34 +02001028 if (proxy && proxy->_c.mfc_un.res.ttls[vifi] < 255)
1029 return (struct mfc_cache *)c;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001030 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001031
1032skip:
1033 return ipmr_cache_find_any_parent(mrt, vifi);
1034}
1035
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001036/* Look for a (S,G,iif) entry if parent != -1 */
1037static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
1038 __be32 origin, __be32 mcastgrp,
1039 int parent)
1040{
1041 struct mfc_cache_cmp_arg arg = {
1042 .mfc_mcastgrp = mcastgrp,
1043 .mfc_origin = origin,
1044 };
1045 struct rhlist_head *tmp, *list;
Yuval Mintz494fff52018-02-28 23:29:34 +02001046 struct mr_mfc *c;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001047
1048 list = rhltable_lookup(&mrt->mfc_hash, &arg, ipmr_rht_params);
1049 rhl_for_each_entry_rcu(c, tmp, list, mnode)
1050 if (parent == -1 || parent == c->mfc_parent)
Yuval Mintz494fff52018-02-28 23:29:34 +02001051 return (struct mfc_cache *)c;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001052
1053 return NULL;
1054}
1055
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001056/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001057static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058{
Jianjun Kongc354e122008-11-03 00:28:02 -08001059 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001060
Tom Goff70a0dec2016-06-23 16:11:57 -04001061 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001062 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
1063 c->_c.mfc_un.res.minvif = MAXVIFS;
1064 refcount_set(&c->_c.mfc_un.res.refcount, 1);
Tom Goff70a0dec2016-06-23 16:11:57 -04001065 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 return c;
1067}
1068
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001069static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070{
Jianjun Kongc354e122008-11-03 00:28:02 -08001071 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001072
1073 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001074 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
1075 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001076 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 return c;
1078}
1079
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001080/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +00001081static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1082 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001085 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001087 /* Play the pending entries through our router */
Yuval Mintz494fff52018-02-28 23:29:34 +02001088 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001089 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +02001090 struct nlmsghdr *nlh = skb_pull(skb,
1091 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Yuval Mintz494fff52018-02-28 23:29:34 +02001093 if (__ipmr_fill_mroute(mrt, skb, &c->_c,
1094 nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001095 nlh->nlmsg_len = skb_tail_pointer(skb) -
1096 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 } else {
1098 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +00001099 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +00001101 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001102 e->error = -EMSGSIZE;
1103 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 }
Thomas Graf2942e902006-08-15 00:30:25 -07001105
Eric W. Biederman15e47302012-09-07 20:12:54 +00001106 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001107 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001108 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 }
1111}
1112
Julien Gomes5a645dd2017-06-20 13:54:17 -07001113/* Bounce a cache query up to mrouted and netlink.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001115 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001117static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001118 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001120 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001121 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 struct igmphdr *igmp;
1123 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001124 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 int ret;
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 if (assert == IGMPMSG_WHOLEPKT)
1128 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1129 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 skb = alloc_skb(128, GFP_ATOMIC);
1131
Stephen Hemminger132adf52007-03-08 20:44:43 -08001132 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 return -ENOBUFS;
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 if (assert == IGMPMSG_WHOLEPKT) {
1136 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001137 * Duplicate old header, fix ihl, length etc.
1138 * And all this only to mangle msg->im_msgtype and
1139 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001141 skb_push(skb, sizeof(struct iphdr));
1142 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001143 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001144 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001145 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1147 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001148 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001149 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1150 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1151 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001152 } else {
1153 /* Copy the IP header */
1154 skb_set_network_header(skb, skb->len);
1155 skb_put(skb, ihl);
1156 skb_copy_to_linear_data(skb, pkt->data, ihl);
1157 /* Flag to the kernel this is a route add */
1158 ip_hdr(skb)->protocol = 0;
1159 msg = (struct igmpmsg *)skb_network_header(skb);
1160 msg->im_vif = vifi;
1161 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1162 /* Add our header */
Johannes Berg4df864c2017-06-16 14:29:21 +02001163 igmp = skb_put(skb, sizeof(struct igmphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001164 igmp->type = assert;
1165 msg->im_msgtype = assert;
1166 igmp->code = 0;
1167 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1168 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170
Eric Dumazet4c9687092010-10-01 16:15:01 +00001171 rcu_read_lock();
1172 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001173 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001174 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 kfree_skb(skb);
1176 return -EINVAL;
1177 }
1178
Julien Gomes5a645dd2017-06-20 13:54:17 -07001179 igmpmsg_netlink_event(mrt, skb);
1180
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001181 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001182 ret = sock_queue_rcv_skb(mroute_sk, skb);
1183 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001184 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001185 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 kfree_skb(skb);
1187 }
1188
1189 return ret;
1190}
1191
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001192/* Queue a packet for resolution. It gets locked cache entry! */
1193static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001194 struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001195{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001196 const struct iphdr *iph = ip_hdr(skb);
1197 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001198 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001202 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001203 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001204 c->mfc_origin == iph->saddr) {
1205 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 }
1209
Patrick McHardy862465f2010-04-13 05:03:21 +00001210 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001211 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001212 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001213 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 spin_unlock_bh(&mfc_unres_lock);
1215
1216 kfree_skb(skb);
1217 return -ENOBUFS;
1218 }
1219
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001220 /* Fill in the new cache entry */
Yuval Mintz494fff52018-02-28 23:29:34 +02001221 c->_c.mfc_parent = -1;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001222 c->mfc_origin = iph->saddr;
1223 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001225 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001226 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Yuval Mintz494fff52018-02-28 23:29:34 +02001227
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001228 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001229 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 out - Brad Parker
1231 */
1232 spin_unlock_bh(&mfc_unres_lock);
1233
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001234 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 kfree_skb(skb);
1236 return err;
1237 }
1238
Patrick McHardy0c122952010-04-13 05:03:22 +00001239 atomic_inc(&mrt->cache_resolve_queue_len);
Yuval Mintz494fff52018-02-28 23:29:34 +02001240 list_add(&c->_c.list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001241 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
David S. Miller278554b2010-05-12 00:05:35 -07001243 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
Yuval Mintz494fff52018-02-28 23:29:34 +02001244 mod_timer(&mrt->ipmr_expire_timer,
1245 c->_c.mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001248 /* See if we can append the packet */
Yuval Mintz494fff52018-02-28 23:29:34 +02001249 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 kfree_skb(skb);
1251 err = -ENOBUFS;
1252 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001253 if (dev) {
1254 skb->dev = dev;
1255 skb->skb_iif = dev->ifindex;
1256 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001257 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 err = 0;
1259 }
1260
1261 spin_unlock_bh(&mfc_unres_lock);
1262 return err;
1263}
1264
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001265/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001267static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268{
Yotam Gigib3620532017-09-27 08:23:14 +02001269 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001270 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001272 /* The entries are added/deleted only under RTNL */
1273 rcu_read_lock();
1274 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1275 mfc->mfcc_mcastgrp.s_addr, parent);
1276 rcu_read_unlock();
1277 if (!c)
1278 return -ENOENT;
Yuval Mintz494fff52018-02-28 23:29:34 +02001279 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1280 list_del_rcu(&c->_c.list);
Yotam Gigib3620532017-09-27 08:23:14 +02001281 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001282 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yotam Gigi310ebbb2017-09-27 08:23:12 +02001283 ipmr_cache_put(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286}
1287
Patrick McHardy0c122952010-04-13 05:03:22 +00001288static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001289 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290{
Patrick McHardy862465f2010-04-13 05:03:21 +00001291 struct mfc_cache *uc, *c;
Yuval Mintz494fff52018-02-28 23:29:34 +02001292 struct mr_mfc *_uc;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001293 bool found;
1294 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295
Patrick McHardya50436f22010-03-17 06:04:14 +00001296 if (mfc->mfcc_parent >= MAXVIFS)
1297 return -ENFILE;
1298
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001299 /* The entries are added/deleted only under RTNL */
1300 rcu_read_lock();
1301 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1302 mfc->mfcc_mcastgrp.s_addr, parent);
1303 rcu_read_unlock();
1304 if (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 write_lock_bh(&mrt_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001306 c->_c.mfc_parent = mfc->mfcc_parent;
1307 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001309 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +02001311 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1312 mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001313 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314 return 0;
1315 }
1316
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001317 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001318 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319 return -EINVAL;
1320
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001321 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001322 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 return -ENOMEM;
1324
Jianjun Kongc354e122008-11-03 00:28:02 -08001325 c->mfc_origin = mfc->mfcc_origin.s_addr;
1326 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
Yuval Mintz494fff52018-02-28 23:29:34 +02001327 c->_c.mfc_parent = mfc->mfcc_parent;
1328 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001330 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
Yuval Mintz494fff52018-02-28 23:29:34 +02001332 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001333 ipmr_rht_params);
1334 if (ret) {
1335 pr_err("ipmr: rhtable insert error %d\n", ret);
1336 ipmr_cache_free(c);
1337 return ret;
1338 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001339 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001340 /* Check to see if we resolved a queued list. If so we
1341 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001343 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001345 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1346 uc = (struct mfc_cache *)_uc;
Patrick McHardye258beb2010-04-13 05:03:19 +00001347 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001349 list_del(&_uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001350 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001351 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 break;
1353 }
1354 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001355 if (list_empty(&mrt->mfc_unres_queue))
1356 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 spin_unlock_bh(&mfc_unres_lock);
1358
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001359 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001360 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001361 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 }
Yotam Gigib3620532017-09-27 08:23:14 +02001363 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001364 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 return 0;
1366}
1367
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001368/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001369static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370{
Yotam Gigib3620532017-09-27 08:23:14 +02001371 struct net *net = read_pnet(&mrt->net);
Yuval Mintz494fff52018-02-28 23:29:34 +02001372 struct mr_mfc *c, *tmp;
1373 struct mfc_cache *cache;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001374 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001375 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001376
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001377 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001378 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001379 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1380 continue;
1381 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001383 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001385 /* Wipe the cache */
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001386 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1387 if (!all && (c->mfc_flags & MFC_STATIC))
1388 continue;
1389 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1390 list_del_rcu(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +02001391 cache = (struct mfc_cache *)c;
1392 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
Yotam Gigib3620532017-09-27 08:23:14 +02001393 mrt->id);
Yuval Mintz494fff52018-02-28 23:29:34 +02001394 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1395 ipmr_cache_put(cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396 }
1397
Patrick McHardy0c122952010-04-13 05:03:22 +00001398 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 spin_lock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001400 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001401 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +02001402 cache = (struct mfc_cache *)c;
1403 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1404 ipmr_destroy_unres(mrt, cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 }
1406 spin_unlock_bh(&mfc_unres_lock);
1407 }
1408}
1409
Eric Dumazet4c9687092010-10-01 16:15:01 +00001410/* called from ip_ra_control(), before an RCU grace period,
1411 * we dont need to call synchronize_rcu() here
1412 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413static void mrtsock_destruct(struct sock *sk)
1414{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001415 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001416 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001417
WANG Cong1215e512017-04-12 12:32:13 -07001418 ASSERT_RTNL();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001419 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001420 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001421 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001422 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1423 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001424 NETCONFA_IFINDEX_ALL,
1425 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001426 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001427 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001428 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001432/* Socket options and virtual interface manipulation. The whole
1433 * virtual interface system is a complete heap, but unfortunately
1434 * that's how BSD mrouted happens to think. Maybe one day with a proper
1435 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001436 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001437
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001438int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1439 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001441 struct net *net = sock_net(sk);
1442 int val, ret = 0, parent = 0;
1443 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444 struct vifctl vif;
1445 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001446 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001447
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001448 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1449 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001450 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001451 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1452 ret = -EOPNOTSUPP;
1453 goto out_unlock;
1454 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001455
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001456 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001457 if (!mrt) {
1458 ret = -ENOENT;
1459 goto out_unlock;
1460 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001461 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001462 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001463 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1464 ret = -EACCES;
1465 goto out_unlock;
1466 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 }
1468
Stephen Hemminger132adf52007-03-08 20:44:43 -08001469 switch (optname) {
1470 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001471 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001472 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001473 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001474 }
1475 if (rtnl_dereference(mrt->mroute_sk)) {
1476 ret = -EADDRINUSE;
1477 break;
1478 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001479
1480 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1481 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001482 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001483 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001484 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1485 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001486 NETCONFA_IFINDEX_ALL,
1487 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001488 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001489 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001490 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001491 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1492 ret = -EACCES;
1493 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001494 ret = ip_ra_control(sk, 0, NULL);
WANG Cong1215e512017-04-12 12:32:13 -07001495 goto out_unlock;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001496 }
1497 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001498 case MRT_ADD_VIF:
1499 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001500 if (optlen != sizeof(vif)) {
1501 ret = -EINVAL;
1502 break;
1503 }
1504 if (copy_from_user(&vif, optval, sizeof(vif))) {
1505 ret = -EFAULT;
1506 break;
1507 }
1508 if (vif.vifc_vifi >= MAXVIFS) {
1509 ret = -ENFILE;
1510 break;
1511 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001512 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001513 ret = vif_add(net, mrt, &vif,
1514 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001515 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001516 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001517 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001518 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001519 /* Manipulate the forwarding caches. These live
1520 * in a sort of kernel/user symbiosis.
1521 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001522 case MRT_ADD_MFC:
1523 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001524 parent = -1;
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05001525 /* fall through */
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001526 case MRT_ADD_MFC_PROXY:
1527 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001528 if (optlen != sizeof(mfc)) {
1529 ret = -EINVAL;
1530 break;
1531 }
1532 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1533 ret = -EFAULT;
1534 break;
1535 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001536 if (parent == 0)
1537 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001538 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1539 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001540 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001541 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001542 sk == rtnl_dereference(mrt->mroute_sk),
1543 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001544 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001545 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001546 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001547 if (optlen != sizeof(val)) {
1548 ret = -EINVAL;
1549 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001550 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001551 if (get_user(val, (int __user *)optval)) {
1552 ret = -EFAULT;
1553 break;
1554 }
1555 mrt->mroute_do_assert = val;
1556 break;
1557 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001558 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001559 ret = -ENOPROTOOPT;
1560 break;
1561 }
1562 if (optlen != sizeof(val)) {
1563 ret = -EINVAL;
1564 break;
1565 }
1566 if (get_user(val, (int __user *)optval)) {
1567 ret = -EFAULT;
1568 break;
1569 }
1570
1571 val = !!val;
1572 if (val != mrt->mroute_do_pim) {
1573 mrt->mroute_do_pim = val;
1574 mrt->mroute_do_assert = val;
1575 }
1576 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001577 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001578 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1579 ret = -ENOPROTOOPT;
1580 break;
1581 }
1582 if (optlen != sizeof(uval)) {
1583 ret = -EINVAL;
1584 break;
1585 }
1586 if (get_user(uval, (u32 __user *)optval)) {
1587 ret = -EFAULT;
1588 break;
1589 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001590
Eric Dumazet4c9687092010-10-01 16:15:01 +00001591 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1592 ret = -EBUSY;
1593 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001594 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001595 if (IS_ERR(mrt))
1596 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001597 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001598 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001599 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001600 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001601 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001602 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001603 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001605out_unlock:
1606 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001607 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608}
1609
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001610/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001611int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612{
1613 int olr;
1614 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001615 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001616 struct mr_table *mrt;
1617
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001618 if (sk->sk_type != SOCK_RAW ||
1619 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1620 return -EOPNOTSUPP;
1621
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001622 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001623 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001624 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001626 switch (optname) {
1627 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001628 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001629 break;
1630 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001631 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001632 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001633 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001634 break;
1635 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001636 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001637 break;
1638 default:
1639 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001640 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001641
1642 if (get_user(olr, optlen))
1643 return -EFAULT;
1644 olr = min_t(unsigned int, olr, sizeof(int));
1645 if (olr < 0)
1646 return -EINVAL;
1647 if (put_user(olr, optlen))
1648 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001649 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 return -EFAULT;
1651 return 0;
1652}
1653
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001654/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1656{
1657 struct sioc_sg_req sr;
1658 struct sioc_vif_req vr;
1659 struct vif_device *vif;
1660 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001661 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001662 struct mr_table *mrt;
1663
1664 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001665 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001666 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001667
Stephen Hemminger132adf52007-03-08 20:44:43 -08001668 switch (cmd) {
1669 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001670 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001671 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001672 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001673 return -EINVAL;
1674 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001675 vif = &mrt->vif_table[vr.vifi];
1676 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001677 vr.icount = vif->pkt_in;
1678 vr.ocount = vif->pkt_out;
1679 vr.ibytes = vif->bytes_in;
1680 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001682
Jianjun Kongc354e122008-11-03 00:28:02 -08001683 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001685 return 0;
1686 }
1687 read_unlock(&mrt_lock);
1688 return -EADDRNOTAVAIL;
1689 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001690 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001691 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692
Eric Dumazeta8c94862010-10-01 16:15:08 +00001693 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001694 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001695 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001696 sr.pktcnt = c->_c.mfc_un.res.pkt;
1697 sr.bytecnt = c->_c.mfc_un.res.bytes;
1698 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001699 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001700
Jianjun Kongc354e122008-11-03 00:28:02 -08001701 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001702 return -EFAULT;
1703 return 0;
1704 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001705 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001706 return -EADDRNOTAVAIL;
1707 default:
1708 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 }
1710}
1711
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001712#ifdef CONFIG_COMPAT
1713struct compat_sioc_sg_req {
1714 struct in_addr src;
1715 struct in_addr grp;
1716 compat_ulong_t pktcnt;
1717 compat_ulong_t bytecnt;
1718 compat_ulong_t wrong_if;
1719};
1720
David S. Millerca6b8bb02011-02-03 17:24:28 -08001721struct compat_sioc_vif_req {
1722 vifi_t vifi; /* Which iface */
1723 compat_ulong_t icount;
1724 compat_ulong_t ocount;
1725 compat_ulong_t ibytes;
1726 compat_ulong_t obytes;
1727};
1728
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001729int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1730{
David S. Miller0033d5a2011-02-03 17:21:31 -08001731 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001732 struct compat_sioc_vif_req vr;
1733 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001734 struct mfc_cache *c;
1735 struct net *net = sock_net(sk);
1736 struct mr_table *mrt;
1737
1738 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001739 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001740 return -ENOENT;
1741
1742 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001743 case SIOCGETVIFCNT:
1744 if (copy_from_user(&vr, arg, sizeof(vr)))
1745 return -EFAULT;
1746 if (vr.vifi >= mrt->maxvif)
1747 return -EINVAL;
1748 read_lock(&mrt_lock);
1749 vif = &mrt->vif_table[vr.vifi];
1750 if (VIF_EXISTS(mrt, vr.vifi)) {
1751 vr.icount = vif->pkt_in;
1752 vr.ocount = vif->pkt_out;
1753 vr.ibytes = vif->bytes_in;
1754 vr.obytes = vif->bytes_out;
1755 read_unlock(&mrt_lock);
1756
1757 if (copy_to_user(arg, &vr, sizeof(vr)))
1758 return -EFAULT;
1759 return 0;
1760 }
1761 read_unlock(&mrt_lock);
1762 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001763 case SIOCGETSGCNT:
1764 if (copy_from_user(&sr, arg, sizeof(sr)))
1765 return -EFAULT;
1766
1767 rcu_read_lock();
1768 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1769 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001770 sr.pktcnt = c->_c.mfc_un.res.pkt;
1771 sr.bytecnt = c->_c.mfc_un.res.bytes;
1772 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001773 rcu_read_unlock();
1774
1775 if (copy_to_user(arg, &sr, sizeof(sr)))
1776 return -EFAULT;
1777 return 0;
1778 }
1779 rcu_read_unlock();
1780 return -EADDRNOTAVAIL;
1781 default:
1782 return -ENOIOCTLCMD;
1783 }
1784}
1785#endif
1786
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1788{
Jiri Pirko351638e2013-05-28 01:30:21 +00001789 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001790 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001791 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 struct vif_device *v;
1793 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (event != NETDEV_UNREGISTER)
1796 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001797
1798 ipmr_for_each_table(mrt, net) {
1799 v = &mrt->vif_table[0];
1800 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1801 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001802 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001803 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805 return NOTIFY_DONE;
1806}
1807
Jianjun Kongc354e122008-11-03 00:28:02 -08001808static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 .notifier_call = ipmr_device_event,
1810};
1811
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001812/* Encapsulate a packet by attaching a valid IPIP header to it.
1813 * This avoids tunnel drivers and other mess and gives us the speed so
1814 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001816static void ip_encap(struct net *net, struct sk_buff *skb,
1817 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001819 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001820 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001821
1822 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001823 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001824 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001825 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001827 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001828 iph->tos = old_iph->tos;
1829 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 iph->frag_off = 0;
1831 iph->daddr = daddr;
1832 iph->saddr = saddr;
1833 iph->protocol = IPPROTO_IPIP;
1834 iph->ihl = 5;
1835 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001836 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 ip_send_check(iph);
1838
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1840 nf_reset(skb);
1841}
1842
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001843static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1844 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001846 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847
David S. Miller73186df2015-11-03 13:41:45 -05001848 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1849 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
1851 if (unlikely(opt->optlen))
1852 ip_forward_options(skb);
1853
Eric W. Biederman13206b62015-10-07 16:48:35 -05001854 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
Yotam Gigia5bc9292017-10-03 09:58:08 +02001857#ifdef CONFIG_NET_SWITCHDEV
1858static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1859 int in_vifi, int out_vifi)
1860{
1861 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1862 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1863
1864 if (!skb->offload_mr_fwd_mark)
1865 return false;
1866 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1867 return false;
1868 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1869 &in_vif->dev_parent_id);
1870}
1871#else
1872static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1873 int in_vifi, int out_vifi)
1874{
1875 return false;
1876}
1877#endif
1878
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001879/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880
Patrick McHardy0c122952010-04-13 05:03:22 +00001881static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
Yotam Gigia5bc9292017-10-03 09:58:08 +02001882 int in_vifi, struct sk_buff *skb,
1883 struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001885 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001886 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 struct net_device *dev;
1888 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001889 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 int encap = 0;
1891
Ian Morris51456b22015-04-03 09:17:26 +01001892 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 goto out_free;
1894
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (vif->flags & VIFF_REGISTER) {
1896 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001897 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001898 vif->dev->stats.tx_bytes += skb->len;
1899 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001900 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001901 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Yotam Gigia5bc9292017-10-03 09:58:08 +02001904 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1905 goto out_free;
1906
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001907 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001908 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001909 vif->remote, vif->local,
1910 0, 0,
1911 IPPROTO_IPIP,
1912 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001913 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914 goto out_free;
1915 encap = sizeof(struct iphdr);
1916 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001917 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001918 0, 0,
1919 IPPROTO_IPIP,
1920 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001921 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 goto out_free;
1923 }
1924
Changli Gaod8d1f302010-06-10 23:31:35 -07001925 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Changli Gaod8d1f302010-06-10 23:31:35 -07001927 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001929 * allow to send ICMP, so that packets will disappear
1930 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 */
David S. Miller73186df2015-11-03 13:41:45 -05001932 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 ip_rt_put(rt);
1934 goto out_free;
1935 }
1936
Changli Gaod8d1f302010-06-10 23:31:35 -07001937 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938
1939 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001940 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941 goto out_free;
1942 }
1943
1944 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001945 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
Eric Dumazetadf30902009-06-02 05:19:30 +00001947 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001948 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001949 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950
1951 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001952 * What do we do with netfilter? -- RR
1953 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001955 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001957 vif->dev->stats.tx_packets++;
1958 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 }
1960
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001961 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001963 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 * not only before forwarding, but after forwarding on all output
1965 * interfaces. It is clear, if mrouter runs a multicasting
1966 * program, it should receive packets not depending to what interface
1967 * program is joined.
1968 * If we will not make it, the program will have to join on all
1969 * interfaces. On the other hand, multihoming host (or router, but
1970 * not mrouter) cannot join to more than one interface - it will
1971 * result in receiving multiple packets.
1972 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001973 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1974 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 ipmr_forward_finish);
1976 return;
1977
1978out_free:
1979 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980}
1981
Patrick McHardy0c122952010-04-13 05:03:22 +00001982static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983{
1984 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001985
1986 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1987 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988 break;
1989 }
1990 return ct;
1991}
1992
1993/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001994static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001995 struct net_device *dev, struct sk_buff *skb,
Yuval Mintz494fff52018-02-28 23:29:34 +02001996 struct mfc_cache *c, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001998 int true_vifi = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 int psend = -1;
2000 int vif, ct;
2001
Yuval Mintz494fff52018-02-28 23:29:34 +02002002 vif = c->_c.mfc_parent;
2003 c->_c.mfc_un.res.pkt++;
2004 c->_c.mfc_un.res.bytes += skb->len;
2005 c->_c.mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
Yuval Mintz494fff52018-02-28 23:29:34 +02002007 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002008 struct mfc_cache *cache_proxy;
2009
2010 /* For an (*,G) entry, we only check that the incomming
2011 * interface is part of the static tree.
2012 */
2013 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
2014 if (cache_proxy &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002015 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002016 goto forward;
2017 }
2018
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002019 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002020 if (mrt->vif_table[vif].dev != dev) {
David S. Millerc7537962010-11-11 17:07:48 -08002021 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002023 * Very complicated situation...
2024 *
2025 * The best workaround until routing daemons will be
2026 * fixed is not to redistribute packet, if it was
2027 * send through wrong interface. It means, that
2028 * multicast applications WILL NOT work for
2029 * (S,G), which have default multicast route pointing
2030 * to wrong oif. In any case, it is not a good
2031 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 */
2033 goto dont_forward;
2034 }
2035
Yuval Mintz494fff52018-02-28 23:29:34 +02002036 c->_c.mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Patrick McHardy0c122952010-04-13 05:03:22 +00002038 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002040 * so that we cannot check that packet arrived on an oif.
2041 * It is bad, but otherwise we would need to move pretty
2042 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002043 */
Patrick McHardy0c122952010-04-13 05:03:22 +00002044 (mrt->mroute_do_pim ||
Yuval Mintz494fff52018-02-28 23:29:34 +02002045 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002046 time_after(jiffies,
Yuval Mintz494fff52018-02-28 23:29:34 +02002047 c->_c.mfc_un.res.last_assert +
2048 MFC_ASSERT_THRESH)) {
2049 c->_c.mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00002050 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 }
2052 goto dont_forward;
2053 }
2054
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002055forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00002056 mrt->vif_table[vif].pkt_in++;
2057 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002059 /* Forward the frame */
Yuval Mintz494fff52018-02-28 23:29:34 +02002060 if (c->mfc_origin == htonl(INADDR_ANY) &&
2061 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002062 if (true_vifi >= 0 &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002063 true_vifi != c->_c.mfc_parent &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002064 ip_hdr(skb)->ttl >
Yuval Mintz494fff52018-02-28 23:29:34 +02002065 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002066 /* It's an (*,*) entry and the packet is not coming from
2067 * the upstream: forward the packet to the upstream
2068 * only.
2069 */
Yuval Mintz494fff52018-02-28 23:29:34 +02002070 psend = c->_c.mfc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002071 goto last_forward;
2072 }
2073 goto dont_forward;
2074 }
Yuval Mintz494fff52018-02-28 23:29:34 +02002075 for (ct = c->_c.mfc_un.res.maxvif - 1;
2076 ct >= c->_c.mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002077 /* For (*,G) entry, don't forward to the incoming interface */
Yuval Mintz494fff52018-02-28 23:29:34 +02002078 if ((c->mfc_origin != htonl(INADDR_ANY) ||
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002079 ct != true_vifi) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002080 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 if (psend != -1) {
2082 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002085 ipmr_queue_xmit(net, mrt, true_vifi,
Yuval Mintz494fff52018-02-28 23:29:34 +02002086 skb2, c, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002088 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 }
2090 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002091last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092 if (psend != -1) {
2093 if (local) {
2094 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002097 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
Yuval Mintz494fff52018-02-28 23:29:34 +02002098 c, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 } else {
Yuval Mintz494fff52018-02-28 23:29:34 +02002100 ipmr_queue_xmit(net, mrt, true_vifi, skb, c, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03002101 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 }
2103 }
2104
2105dont_forward:
2106 if (!local)
2107 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108}
2109
David S. Miller417da662011-05-03 19:42:43 -07002110static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08002111{
David S. Miller417da662011-05-03 19:42:43 -07002112 struct rtable *rt = skb_rtable(skb);
2113 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05002114 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07002115 .daddr = iph->daddr,
2116 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00002117 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07002118 .flowi4_oif = (rt_is_output_route(rt) ?
2119 skb->dev->ifindex : 0),
2120 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002121 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07002122 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00002123 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08002124 };
2125 struct mr_table *mrt;
2126 int err;
2127
David S. Millerda919812011-03-12 02:04:50 -05002128 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002129 if (err)
2130 return ERR_PTR(err);
2131 return mrt;
2132}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002134/* Multicast packets for forwarding arrive here
2135 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137int ip_mr_input(struct sk_buff *skb)
2138{
2139 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002140 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00002141 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002142 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002143 struct net_device *dev;
2144
2145 /* skb->dev passed in is the loX master dev for vrfs.
2146 * As there are no vifs associated with loopback devices,
2147 * get the proper interface that does have a vif associated with it.
2148 */
2149 dev = skb->dev;
2150 if (netif_is_l3_master(skb->dev)) {
2151 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2152 if (!dev) {
2153 kfree_skb(skb);
2154 return -ENODEV;
2155 }
2156 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
2158 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002159 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00002161 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 goto dont_forward;
2163
David S. Miller417da662011-05-03 19:42:43 -07002164 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002165 if (IS_ERR(mrt)) {
2166 kfree_skb(skb);
2167 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00002170 if (IPCB(skb)->opt.router_alert) {
2171 if (ip_call_ra_chain(skb))
2172 return 0;
2173 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2174 /* IGMPv1 (and broken IGMPv2 implementations sort of
2175 * Cisco IOS <= 11.2(8)) do not put router alert
2176 * option to IGMP packets destined to routable
2177 * groups. It is very bad, because it means
2178 * that we can forward NO IGMP messages.
2179 */
2180 struct sock *mroute_sk;
2181
2182 mroute_sk = rcu_dereference(mrt->mroute_sk);
2183 if (mroute_sk) {
2184 nf_reset(skb);
2185 raw_rcv(mroute_sk, skb);
2186 return 0;
2187 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
2189 }
2190
Eric Dumazeta8c94862010-10-01 16:15:08 +00002191 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002192 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002193 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002194 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002195
2196 if (vif >= 0)
2197 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2198 vif);
2199 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002201 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002202 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 int vif;
2204
2205 if (local) {
2206 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2207 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002208 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 skb = skb2;
2211 }
2212
Eric Dumazeta8c94862010-10-01 16:15:08 +00002213 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002214 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215 if (vif >= 0) {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002216 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 read_unlock(&mrt_lock);
2218
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002219 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220 }
2221 read_unlock(&mrt_lock);
2222 kfree_skb(skb);
2223 return -ENODEV;
2224 }
2225
Eric Dumazeta8c94862010-10-01 16:15:08 +00002226 read_lock(&mrt_lock);
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002227 ip_mr_forward(net, mrt, dev, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228 read_unlock(&mrt_lock);
2229
2230 if (local)
2231 return ip_local_deliver(skb);
2232
2233 return 0;
2234
2235dont_forward:
2236 if (local)
2237 return ip_local_deliver(skb);
2238 kfree_skb(skb);
2239 return 0;
2240}
2241
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002242#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002243/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002244int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002245{
2246 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002247 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002248 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002249
2250 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2251 goto drop;
2252
2253 pim = igmp_hdr(skb);
2254
David S. Miller417da662011-05-03 19:42:43 -07002255 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002256 if (IS_ERR(mrt))
2257 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002258 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002259 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2260 goto drop;
2261
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002262 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002263drop:
2264 kfree_skb(skb);
2265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 return 0;
2267}
2268#endif
2269
2270#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002271static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272{
2273 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002274 struct net *net = dev_net(skb->dev);
2275 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002277 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 goto drop;
2279
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002280 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002281 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002282 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002283 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002284 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285 goto drop;
2286
David S. Miller417da662011-05-03 19:42:43 -07002287 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002288 if (IS_ERR(mrt))
2289 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002290 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002291drop:
2292 kfree_skb(skb);
2293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 return 0;
2295}
2296#endif
2297
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002298static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Yuval Mintz494fff52018-02-28 23:29:34 +02002299 struct mr_mfc *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300{
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002301 struct rta_mfc_stats mfcs;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002302 struct nlattr *mp_attr;
2303 struct rtnexthop *nhp;
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002304 unsigned long lastuse;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002305 int ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306
Nicolas Dichtel74381892010-03-25 23:45:35 +00002307 /* If cache is unresolved, don't try to parse IIF and OIF */
Nikolay Aleksandrov1708ebc2017-01-03 12:13:39 +01002308 if (c->mfc_parent >= MAXVIFS) {
2309 rtm->rtm_flags |= RTNH_F_UNRESOLVED;
Nicolas Dichtel74381892010-03-25 23:45:35 +00002310 return -ENOENT;
Nikolay Aleksandrov1708ebc2017-01-03 12:13:39 +01002311 }
Nicolas Dichtel74381892010-03-25 23:45:35 +00002312
Thomas Graf92a395e2012-06-26 23:36:13 +00002313 if (VIF_EXISTS(mrt, c->mfc_parent) &&
Yuval Mintz6853f212018-02-28 23:29:29 +02002314 nla_put_u32(skb, RTA_IIF,
2315 mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
Thomas Graf92a395e2012-06-26 23:36:13 +00002316 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Yotam Gigic7c0bbe2017-09-27 08:23:15 +02002318 if (c->mfc_flags & MFC_OFFLOAD)
2319 rtm->rtm_flags |= RTNH_F_OFFLOAD;
2320
Thomas Graf92a395e2012-06-26 23:36:13 +00002321 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2322 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323
2324 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002325 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Yuval Mintz6853f212018-02-28 23:29:29 +02002326 struct vif_device *vif;
2327
Thomas Graf92a395e2012-06-26 23:36:13 +00002328 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2329 nla_nest_cancel(skb, mp_attr);
2330 return -EMSGSIZE;
2331 }
2332
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 nhp->rtnh_flags = 0;
2334 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Yuval Mintz6853f212018-02-28 23:29:29 +02002335 vif = &mrt->vif_table[ct];
2336 nhp->rtnh_ifindex = vif->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 nhp->rtnh_len = sizeof(*nhp);
2338 }
2339 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002340
2341 nla_nest_end(skb, mp_attr);
2342
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002343 lastuse = READ_ONCE(c->mfc_un.res.lastuse);
2344 lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
2345
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002346 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2347 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2348 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002349 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002350 nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002351 RTA_PAD))
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002352 return -EMSGSIZE;
2353
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354 rtm->rtm_type = RTN_MULTICAST;
2355 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356}
2357
David S. Miller9a1b9492011-05-04 12:18:54 -07002358int ipmr_get_route(struct net *net, struct sk_buff *skb,
2359 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002360 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002363 struct mr_table *mrt;
2364 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002366 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002367 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002368 return -ENOENT;
2369
Eric Dumazeta8c94862010-10-01 16:15:08 +00002370 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002371 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002372 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002373 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002375 if (vif >= 0)
2376 cache = ipmr_cache_find_any(mrt, daddr, vif);
2377 }
Ian Morris51456b22015-04-03 09:17:26 +01002378 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002379 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002380 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002381 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002382 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002383
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002385 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002386 if (dev)
2387 vif = ipmr_find_vif(mrt, dev);
2388 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002389 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002390 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 return -ENODEV;
2392 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002393 skb2 = skb_clone(skb, GFP_ATOMIC);
2394 if (!skb2) {
2395 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002396 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002397 return -ENOMEM;
2398 }
2399
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002400 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002401 skb_push(skb2, sizeof(struct iphdr));
2402 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002403 iph = ip_hdr(skb2);
2404 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002405 iph->saddr = saddr;
2406 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002407 iph->version = 0;
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002408 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002410 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return err;
2412 }
2413
Eric Dumazeta8c94862010-10-01 16:15:08 +00002414 read_lock(&mrt_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02002415 err = __ipmr_fill_mroute(mrt, skb, &cache->_c, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002417 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 return err;
2419}
2420
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002421static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002422 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2423 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002424{
2425 struct nlmsghdr *nlh;
2426 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002427 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002428
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002429 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002430 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002431 return -EMSGSIZE;
2432
2433 rtm = nlmsg_data(nlh);
2434 rtm->rtm_family = RTNL_FAMILY_IPMR;
2435 rtm->rtm_dst_len = 32;
2436 rtm->rtm_src_len = 32;
2437 rtm->rtm_tos = 0;
2438 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002439 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2440 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002441 rtm->rtm_type = RTN_MULTICAST;
2442 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Yuval Mintz494fff52018-02-28 23:29:34 +02002443 if (c->_c.mfc_flags & MFC_STATIC)
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002444 rtm->rtm_protocol = RTPROT_STATIC;
2445 else
2446 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002447 rtm->rtm_flags = 0;
2448
Jiri Benc930345e2015-03-29 16:59:25 +02002449 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2450 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002451 goto nla_put_failure;
Yuval Mintz494fff52018-02-28 23:29:34 +02002452 err = __ipmr_fill_mroute(mrt, skb, &c->_c, rtm);
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002453 /* do not break the dump if cache is unresolved */
2454 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002455 goto nla_put_failure;
2456
Johannes Berg053c0952015-01-16 22:09:00 +01002457 nlmsg_end(skb, nlh);
2458 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002459
2460nla_put_failure:
2461 nlmsg_cancel(skb, nlh);
2462 return -EMSGSIZE;
2463}
2464
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002465static size_t mroute_msgsize(bool unresolved, int maxvif)
2466{
2467 size_t len =
2468 NLMSG_ALIGN(sizeof(struct rtmsg))
2469 + nla_total_size(4) /* RTA_TABLE */
2470 + nla_total_size(4) /* RTA_SRC */
2471 + nla_total_size(4) /* RTA_DST */
2472 ;
2473
2474 if (!unresolved)
2475 len = len
2476 + nla_total_size(4) /* RTA_IIF */
2477 + nla_total_size(0) /* RTA_MULTIPATH */
2478 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2479 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002480 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002481 ;
2482
2483 return len;
2484}
2485
2486static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2487 int cmd)
2488{
2489 struct net *net = read_pnet(&mrt->net);
2490 struct sk_buff *skb;
2491 int err = -ENOBUFS;
2492
Yuval Mintz494fff52018-02-28 23:29:34 +02002493 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2494 mrt->maxvif),
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002495 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002496 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002497 goto errout;
2498
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002499 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002500 if (err < 0)
2501 goto errout;
2502
2503 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2504 return;
2505
2506errout:
2507 kfree_skb(skb);
2508 if (err < 0)
2509 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2510}
2511
Julien Gomes5a645dd2017-06-20 13:54:17 -07002512static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2513{
2514 size_t len =
2515 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2516 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2517 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2518 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2519 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2520 /* IPMRA_CREPORT_PKT */
2521 + nla_total_size(payloadlen)
2522 ;
2523
2524 return len;
2525}
2526
2527static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2528{
2529 struct net *net = read_pnet(&mrt->net);
2530 struct nlmsghdr *nlh;
2531 struct rtgenmsg *rtgenm;
2532 struct igmpmsg *msg;
2533 struct sk_buff *skb;
2534 struct nlattr *nla;
2535 int payloadlen;
2536
2537 payloadlen = pkt->len - sizeof(struct igmpmsg);
2538 msg = (struct igmpmsg *)skb_network_header(pkt);
2539
2540 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2541 if (!skb)
2542 goto errout;
2543
2544 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2545 sizeof(struct rtgenmsg), 0);
2546 if (!nlh)
2547 goto errout;
2548 rtgenm = nlmsg_data(nlh);
2549 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2550 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2551 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2552 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2553 msg->im_src.s_addr) ||
2554 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2555 msg->im_dst.s_addr))
2556 goto nla_put_failure;
2557
2558 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2559 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2560 nla_data(nla), payloadlen))
2561 goto nla_put_failure;
2562
2563 nlmsg_end(skb, nlh);
2564
2565 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2566 return;
2567
2568nla_put_failure:
2569 nlmsg_cancel(skb, nlh);
2570errout:
2571 kfree_skb(skb);
2572 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2573}
2574
Donald Sharp4f75ba62017-06-28 13:58:57 -04002575static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2576 struct netlink_ext_ack *extack)
2577{
2578 struct net *net = sock_net(in_skb->sk);
2579 struct nlattr *tb[RTA_MAX + 1];
2580 struct sk_buff *skb = NULL;
2581 struct mfc_cache *cache;
2582 struct mr_table *mrt;
2583 struct rtmsg *rtm;
2584 __be32 src, grp;
2585 u32 tableid;
2586 int err;
2587
2588 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2589 rtm_ipv4_policy, extack);
2590 if (err < 0)
2591 goto errout;
2592
2593 rtm = nlmsg_data(nlh);
2594
2595 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2596 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2597 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2598
2599 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
Dan Carpenter2e3d2322017-07-12 10:56:47 +03002600 if (!mrt) {
2601 err = -ENOENT;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002602 goto errout_free;
2603 }
2604
2605 /* entries are added/deleted only under RTNL */
2606 rcu_read_lock();
2607 cache = ipmr_cache_find(mrt, src, grp);
2608 rcu_read_unlock();
2609 if (!cache) {
2610 err = -ENOENT;
2611 goto errout_free;
2612 }
2613
2614 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2615 if (!skb) {
2616 err = -ENOBUFS;
2617 goto errout_free;
2618 }
2619
2620 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2621 nlh->nlmsg_seq, cache,
2622 RTM_NEWROUTE, 0);
2623 if (err < 0)
2624 goto errout_free;
2625
2626 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2627
2628errout:
2629 return err;
2630
2631errout_free:
2632 kfree_skb(skb);
2633 goto errout;
2634}
2635
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002636static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2637{
2638 struct net *net = sock_net(skb->sk);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002639 unsigned int t = 0, s_t;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002640 unsigned int e = 0, s_e;
Yuval Mintz494fff52018-02-28 23:29:34 +02002641 struct mr_table *mrt;
2642 struct mr_mfc *mfc;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002643
2644 s_t = cb->args[0];
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002645 s_e = cb->args[1];
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002646
Eric Dumazeta8c94862010-10-01 16:15:08 +00002647 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002648 ipmr_for_each_table(mrt, net) {
2649 if (t < s_t)
2650 goto next_table;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002651 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) {
2652 if (e < s_e)
2653 goto next_entry;
2654 if (ipmr_fill_mroute(mrt, skb,
2655 NETLINK_CB(cb->skb).portid,
2656 cb->nlh->nlmsg_seq,
Yuval Mintz494fff52018-02-28 23:29:34 +02002657 (struct mfc_cache *)mfc,
2658 RTM_NEWROUTE, NLM_F_MULTI) < 0)
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002659 goto done;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002660next_entry:
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002661 e++;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002662 }
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002663 e = 0;
2664 s_e = 0;
2665
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002666 spin_lock_bh(&mfc_unres_lock);
2667 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2668 if (e < s_e)
2669 goto next_entry2;
2670 if (ipmr_fill_mroute(mrt, skb,
2671 NETLINK_CB(cb->skb).portid,
2672 cb->nlh->nlmsg_seq,
Yuval Mintz494fff52018-02-28 23:29:34 +02002673 (struct mfc_cache *)mfc,
2674 RTM_NEWROUTE, NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002675 spin_unlock_bh(&mfc_unres_lock);
2676 goto done;
2677 }
2678next_entry2:
2679 e++;
2680 }
2681 spin_unlock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002682 e = 0;
2683 s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002684next_table:
2685 t++;
2686 }
2687done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002688 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002689
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002690 cb->args[1] = e;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002691 cb->args[0] = t;
2692
2693 return skb->len;
2694}
2695
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002696static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2697 [RTA_SRC] = { .type = NLA_U32 },
2698 [RTA_DST] = { .type = NLA_U32 },
2699 [RTA_IIF] = { .type = NLA_U32 },
2700 [RTA_TABLE] = { .type = NLA_U32 },
2701 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2702};
2703
2704static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2705{
2706 switch (rtm_protocol) {
2707 case RTPROT_STATIC:
2708 case RTPROT_MROUTED:
2709 return true;
2710 }
2711 return false;
2712}
2713
2714static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2715{
2716 struct rtnexthop *rtnh = nla_data(nla);
2717 int remaining = nla_len(nla), vifi = 0;
2718
2719 while (rtnh_ok(rtnh, remaining)) {
2720 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2721 if (++vifi == MAXVIFS)
2722 break;
2723 rtnh = rtnh_next(rtnh, &remaining);
2724 }
2725
2726 return remaining > 0 ? -EINVAL : vifi;
2727}
2728
2729/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2730static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2731 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002732 struct mr_table **mrtret,
2733 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002734{
2735 struct net_device *dev = NULL;
2736 u32 tblid = RT_TABLE_DEFAULT;
2737 struct mr_table *mrt;
2738 struct nlattr *attr;
2739 struct rtmsg *rtm;
2740 int ret, rem;
2741
Johannes Bergfceb6432017-04-12 14:34:07 +02002742 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07002743 extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002744 if (ret < 0)
2745 goto out;
2746 rtm = nlmsg_data(nlh);
2747
2748 ret = -EINVAL;
2749 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2750 rtm->rtm_type != RTN_MULTICAST ||
2751 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2752 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2753 goto out;
2754
2755 memset(mfcc, 0, sizeof(*mfcc));
2756 mfcc->mfcc_parent = -1;
2757 ret = 0;
2758 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2759 switch (nla_type(attr)) {
2760 case RTA_SRC:
2761 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2762 break;
2763 case RTA_DST:
2764 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2765 break;
2766 case RTA_IIF:
2767 dev = __dev_get_by_index(net, nla_get_u32(attr));
2768 if (!dev) {
2769 ret = -ENODEV;
2770 goto out;
2771 }
2772 break;
2773 case RTA_MULTIPATH:
2774 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2775 ret = -EINVAL;
2776 goto out;
2777 }
2778 break;
2779 case RTA_PREFSRC:
2780 ret = 1;
2781 break;
2782 case RTA_TABLE:
2783 tblid = nla_get_u32(attr);
2784 break;
2785 }
2786 }
2787 mrt = ipmr_get_table(net, tblid);
2788 if (!mrt) {
2789 ret = -ENOENT;
2790 goto out;
2791 }
2792 *mrtret = mrt;
2793 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2794 if (dev)
2795 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2796
2797out:
2798 return ret;
2799}
2800
2801/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002802static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2803 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002804{
2805 struct net *net = sock_net(skb->sk);
2806 int ret, mrtsock, parent;
2807 struct mr_table *tbl;
2808 struct mfcctl mfcc;
2809
2810 mrtsock = 0;
2811 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002812 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002813 if (ret < 0)
2814 return ret;
2815
2816 parent = ret ? mfcc.mfcc_parent : -1;
2817 if (nlh->nlmsg_type == RTM_NEWROUTE)
2818 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2819 else
2820 return ipmr_mfc_delete(tbl, &mfcc, parent);
2821}
2822
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002823static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2824{
2825 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2826
2827 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2828 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2829 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2830 mrt->mroute_reg_vif_num) ||
2831 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2832 mrt->mroute_do_assert) ||
2833 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim))
2834 return false;
2835
2836 return true;
2837}
2838
2839static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2840{
2841 struct nlattr *vif_nest;
2842 struct vif_device *vif;
2843
2844 /* if the VIF doesn't exist just continue */
2845 if (!VIF_EXISTS(mrt, vifid))
2846 return true;
2847
2848 vif = &mrt->vif_table[vifid];
2849 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2850 if (!vif_nest)
2851 return false;
2852 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2853 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2854 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2855 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2856 IPMRA_VIFA_PAD) ||
2857 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2858 IPMRA_VIFA_PAD) ||
2859 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2860 IPMRA_VIFA_PAD) ||
2861 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2862 IPMRA_VIFA_PAD) ||
2863 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2864 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2865 nla_nest_cancel(skb, vif_nest);
2866 return false;
2867 }
2868 nla_nest_end(skb, vif_nest);
2869
2870 return true;
2871}
2872
2873static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2874{
2875 struct net *net = sock_net(skb->sk);
2876 struct nlmsghdr *nlh = NULL;
2877 unsigned int t = 0, s_t;
2878 unsigned int e = 0, s_e;
2879 struct mr_table *mrt;
2880
2881 s_t = cb->args[0];
2882 s_e = cb->args[1];
2883
2884 ipmr_for_each_table(mrt, net) {
2885 struct nlattr *vifs, *af;
2886 struct ifinfomsg *hdr;
2887 u32 i;
2888
2889 if (t < s_t)
2890 goto skip_table;
2891 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2892 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2893 sizeof(*hdr), NLM_F_MULTI);
2894 if (!nlh)
2895 break;
2896
2897 hdr = nlmsg_data(nlh);
2898 memset(hdr, 0, sizeof(*hdr));
2899 hdr->ifi_family = RTNL_FAMILY_IPMR;
2900
2901 af = nla_nest_start(skb, IFLA_AF_SPEC);
2902 if (!af) {
2903 nlmsg_cancel(skb, nlh);
2904 goto out;
2905 }
2906
2907 if (!ipmr_fill_table(mrt, skb)) {
2908 nlmsg_cancel(skb, nlh);
2909 goto out;
2910 }
2911
2912 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2913 if (!vifs) {
2914 nla_nest_end(skb, af);
2915 nlmsg_end(skb, nlh);
2916 goto out;
2917 }
2918 for (i = 0; i < mrt->maxvif; i++) {
2919 if (e < s_e)
2920 goto skip_entry;
2921 if (!ipmr_fill_vif(mrt, i, skb)) {
2922 nla_nest_end(skb, vifs);
2923 nla_nest_end(skb, af);
2924 nlmsg_end(skb, nlh);
2925 goto out;
2926 }
2927skip_entry:
2928 e++;
2929 }
2930 s_e = 0;
2931 e = 0;
2932 nla_nest_end(skb, vifs);
2933 nla_nest_end(skb, af);
2934 nlmsg_end(skb, nlh);
2935skip_table:
2936 t++;
2937 }
2938
2939out:
2940 cb->args[1] = e;
2941 cb->args[0] = t;
2942
2943 return skb->len;
2944}
2945
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002946#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002947/* The /proc interfaces to multicast routing :
2948 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 */
2950struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002951 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002952 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002953 int ct;
2954};
2955
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002956static struct vif_device *ipmr_vif_seq_idx(struct net *net,
Yuval Mintz6853f212018-02-28 23:29:29 +02002957 struct ipmr_vif_iter *iter,
2958 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002960 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002961
2962 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2963 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002965 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002966 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 }
2968 return NULL;
2969}
2970
2971static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002972 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002974 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002975 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002976 struct mr_table *mrt;
2977
2978 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002979 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002980 return ERR_PTR(-ENOENT);
2981
2982 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002983
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002985 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 : SEQ_START_TOKEN;
2987}
2988
2989static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2990{
2991 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002992 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002993 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994
2995 ++*pos;
2996 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002997 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002998
Patrick McHardy0c122952010-04-13 05:03:22 +00002999 while (++iter->ct < mrt->maxvif) {
3000 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00003002 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003003 }
3004 return NULL;
3005}
3006
3007static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08003008 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003009{
3010 read_unlock(&mrt_lock);
3011}
3012
3013static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
3014{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003015 struct ipmr_vif_iter *iter = seq->private;
3016 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003017
Linus Torvalds1da177e2005-04-16 15:20:36 -07003018 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003019 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003020 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
3021 } else {
3022 const struct vif_device *vif = v;
Yuval Mintz6853f212018-02-28 23:29:29 +02003023 const char *name = vif->dev ?
3024 vif->dev->name : "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025
3026 seq_printf(seq,
James Hogan91e6dd82018-01-30 09:48:02 +00003027 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00003028 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003029 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003030 vif->bytes_out, vif->pkt_out,
3031 vif->flags, vif->local, vif->remote);
3032 }
3033 return 0;
3034}
3035
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003036static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003037 .start = ipmr_vif_seq_start,
3038 .next = ipmr_vif_seq_next,
3039 .stop = ipmr_vif_seq_stop,
3040 .show = ipmr_vif_seq_show,
3041};
3042
3043static int ipmr_vif_open(struct inode *inode, struct file *file)
3044{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003045 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
3046 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003047}
3048
Arjan van de Ven9a321442007-02-12 00:55:35 -08003049static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050 .open = ipmr_vif_open,
3051 .read = seq_read,
3052 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003053 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003054};
3055
3056struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003057 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003058 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00003059 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060};
3061
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003062static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
3063 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003064{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003065 struct mr_table *mrt = it->mrt;
Yuval Mintz494fff52018-02-28 23:29:34 +02003066 struct mr_mfc *mfc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067
Eric Dumazeta8c94862010-10-01 16:15:08 +00003068 rcu_read_lock();
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003069 it->cache = &mrt->mfc_cache_list;
3070 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
3071 if (pos-- == 0)
Yuval Mintz494fff52018-02-28 23:29:34 +02003072 return (struct mfc_cache *)mfc;
Eric Dumazeta8c94862010-10-01 16:15:08 +00003073 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00003076 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00003077 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00003078 if (pos-- == 0)
Yuval Mintz494fff52018-02-28 23:29:34 +02003079 return (struct mfc_cache *)mfc;
3080
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 spin_unlock_bh(&mfc_unres_lock);
3082
3083 it->cache = NULL;
3084 return NULL;
3085}
3086
3087
3088static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
3089{
3090 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003091 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003092 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003093
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003094 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01003095 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003096 return ERR_PTR(-ENOENT);
3097
3098 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 it->cache = NULL;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003100 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 : SEQ_START_TOKEN;
3102}
3103
3104static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3105{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003107 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003108 struct mr_table *mrt = it->mrt;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003109 struct mfc_cache *mfc = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110
3111 ++*pos;
3112
3113 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003114 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115
Yuval Mintz494fff52018-02-28 23:29:34 +02003116 if (mfc->_c.list.next != it->cache)
3117 return (struct mfc_cache *)(list_entry(mfc->_c.list.next,
3118 struct mr_mfc, list));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003119
Patrick McHardy0c122952010-04-13 05:03:22 +00003120 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003121 goto end_of_list;
3122
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00003124 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00003125 it->cache = &mrt->mfc_unres_queue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003126
Linus Torvalds1da177e2005-04-16 15:20:36 -07003127 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00003128 if (!list_empty(it->cache))
Yuval Mintz494fff52018-02-28 23:29:34 +02003129 return (struct mfc_cache *)(list_first_entry(it->cache,
3130 struct mr_mfc,
3131 list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003132
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00003133end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 spin_unlock_bh(&mfc_unres_lock);
3135 it->cache = NULL;
3136
3137 return NULL;
3138}
3139
3140static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
3141{
3142 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003143 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003144
Patrick McHardy0c122952010-04-13 05:03:22 +00003145 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003146 spin_unlock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003147 else if (it->cache == &mrt->mfc_cache_list)
Eric Dumazeta8c94862010-10-01 16:15:08 +00003148 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003149}
3150
3151static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
3152{
3153 int n;
3154
3155 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003156 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
3158 } else {
3159 const struct mfc_cache *mfc = v;
3160 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003161 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003162
Eric Dumazet0eae88f2010-04-20 19:06:52 -07003163 seq_printf(seq, "%08X %08X %-3hd",
3164 (__force u32) mfc->mfc_mcastgrp,
3165 (__force u32) mfc->mfc_origin,
Yuval Mintz494fff52018-02-28 23:29:34 +02003166 mfc->_c.mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167
Patrick McHardy0c122952010-04-13 05:03:22 +00003168 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003169 seq_printf(seq, " %8lu %8lu %8lu",
Yuval Mintz494fff52018-02-28 23:29:34 +02003170 mfc->_c.mfc_un.res.pkt,
3171 mfc->_c.mfc_un.res.bytes,
3172 mfc->_c.mfc_un.res.wrong_if);
3173 for (n = mfc->_c.mfc_un.res.minvif;
3174 n < mfc->_c.mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00003175 if (VIF_EXISTS(mrt, n) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02003176 mfc->_c.mfc_un.res.ttls[n] < 255)
Benjamin Therycf958ae32009-01-22 04:56:16 +00003177 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003178 " %2d:%-3d",
Yuval Mintz494fff52018-02-28 23:29:34 +02003179 n, mfc->_c.mfc_un.res.ttls[n]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003180 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003181 } else {
3182 /* unresolved mfc_caches don't contain
3183 * pkt, bytes and wrong_if values
3184 */
3185 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 }
3187 seq_putc(seq, '\n');
3188 }
3189 return 0;
3190}
3191
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003192static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 .start = ipmr_mfc_seq_start,
3194 .next = ipmr_mfc_seq_next,
3195 .stop = ipmr_mfc_seq_stop,
3196 .show = ipmr_mfc_seq_show,
3197};
3198
3199static int ipmr_mfc_open(struct inode *inode, struct file *file)
3200{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003201 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
3202 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003203}
3204
Arjan van de Ven9a321442007-02-12 00:55:35 -08003205static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 .open = ipmr_mfc_open,
3207 .read = seq_read,
3208 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003209 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003210};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003211#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212
3213#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00003214static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07003216 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217};
3218#endif
3219
Yotam Gigi4d65b942017-09-27 08:23:13 +02003220static unsigned int ipmr_seq_read(struct net *net)
3221{
3222 ASSERT_RTNL();
3223
3224 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
3225}
3226
3227static int ipmr_dump(struct net *net, struct notifier_block *nb)
3228{
3229 struct mr_table *mrt;
3230 int err;
3231
3232 err = ipmr_rules_dump(net, nb);
3233 if (err)
3234 return err;
3235
3236 ipmr_for_each_table(mrt, net) {
3237 struct vif_device *v = &mrt->vif_table[0];
Yuval Mintz494fff52018-02-28 23:29:34 +02003238 struct mr_mfc *mfc;
Yotam Gigi4d65b942017-09-27 08:23:13 +02003239 int vifi;
3240
3241 /* Notifiy on table VIF entries */
3242 read_lock(&mrt_lock);
3243 for (vifi = 0; vifi < mrt->maxvif; vifi++, v++) {
3244 if (!v->dev)
3245 continue;
3246
3247 call_ipmr_vif_entry_notifier(nb, net, FIB_EVENT_VIF_ADD,
3248 v, vifi, mrt->id);
3249 }
3250 read_unlock(&mrt_lock);
3251
3252 /* Notify on table MFC entries */
3253 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
3254 call_ipmr_mfc_entry_notifier(nb, net,
Yuval Mintz494fff52018-02-28 23:29:34 +02003255 FIB_EVENT_ENTRY_ADD,
3256 (struct mfc_cache *)mfc,
Yotam Gigi4d65b942017-09-27 08:23:13 +02003257 mrt->id);
3258 }
3259
3260 return 0;
3261}
3262
3263static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3264 .family = RTNL_FAMILY_IPMR,
3265 .fib_seq_read = ipmr_seq_read,
3266 .fib_dump = ipmr_dump,
3267 .owner = THIS_MODULE,
3268};
3269
Colin Ian Kingef739d82017-09-29 14:34:22 +01003270static int __net_init ipmr_notifier_init(struct net *net)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003271{
3272 struct fib_notifier_ops *ops;
3273
3274 net->ipv4.ipmr_seq = 0;
3275
3276 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3277 if (IS_ERR(ops))
3278 return PTR_ERR(ops);
3279 net->ipv4.ipmr_notifier_ops = ops;
3280
3281 return 0;
3282}
3283
3284static void __net_exit ipmr_notifier_exit(struct net *net)
3285{
3286 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3287 net->ipv4.ipmr_notifier_ops = NULL;
3288}
3289
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01003290/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00003291static int __net_init ipmr_net_init(struct net *net)
3292{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003293 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00003294
Yotam Gigi4d65b942017-09-27 08:23:13 +02003295 err = ipmr_notifier_init(net);
3296 if (err)
3297 goto ipmr_notifier_fail;
3298
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003299 err = ipmr_rules_init(net);
3300 if (err < 0)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003301 goto ipmr_rules_fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003302
3303#ifdef CONFIG_PROC_FS
3304 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00003305 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003306 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00003307 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003308 goto proc_cache_fail;
3309#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00003310 return 0;
3311
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003312#ifdef CONFIG_PROC_FS
3313proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00003314 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003315proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003316 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003317#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003318ipmr_rules_fail:
3319 ipmr_notifier_exit(net);
3320ipmr_notifier_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003321 return err;
3322}
3323
3324static void __net_exit ipmr_net_exit(struct net *net)
3325{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003326#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003327 remove_proc_entry("ip_mr_cache", net->proc_net);
3328 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003329#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003330 ipmr_notifier_exit(net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003331 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00003332}
3333
3334static struct pernet_operations ipmr_net_ops = {
3335 .init = ipmr_net_init,
3336 .exit = ipmr_net_exit,
Kirill Tkhaif84c6822018-02-13 12:29:52 +03003337 .async = true,
Benjamin Therycf958ae32009-01-22 04:56:16 +00003338};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003339
Wang Chen03d2f892008-07-03 12:13:36 +08003340int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003341{
Wang Chen03d2f892008-07-03 12:13:36 +08003342 int err;
3343
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3345 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00003346 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003347 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08003348
Benjamin Therycf958ae32009-01-22 04:56:16 +00003349 err = register_pernet_subsys(&ipmr_net_ops);
3350 if (err)
3351 goto reg_pernet_fail;
3352
Wang Chen03d2f892008-07-03 12:13:36 +08003353 err = register_netdevice_notifier(&ip_mr_notifier);
3354 if (err)
3355 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07003356#ifdef CONFIG_IP_PIMSM_V2
3357 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00003358 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07003359 err = -EAGAIN;
3360 goto add_proto_fail;
3361 }
3362#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00003363 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003364 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003365 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003366 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003367 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003368 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03003369
3370 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
Florian Westphalb97bac62017-08-09 20:41:48 +02003371 NULL, ipmr_rtm_dumplink, 0);
Wang Chen03d2f892008-07-03 12:13:36 +08003372 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003373
Tom Goff403dbb92009-06-14 03:16:13 -07003374#ifdef CONFIG_IP_PIMSM_V2
3375add_proto_fail:
3376 unregister_netdevice_notifier(&ip_mr_notifier);
3377#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08003378reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003379 unregister_pernet_subsys(&ipmr_net_ops);
3380reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08003381 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08003382 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383}