blob: 00898c319cc5f31b2a782e5d36a853c1e7c5c1f9 [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
Yuval Mintz845c9a72018-02-28 23:29:35 +0200363static struct mfc_cache_cmp_arg ipmr_mr_table_ops_cmparg_any = {
364 .mfc_mcastgrp = htonl(INADDR_ANY),
365 .mfc_origin = htonl(INADDR_ANY),
366};
367
368static struct mr_table_ops ipmr_mr_table_ops = {
369 .rht_params = &ipmr_rht_params,
370 .cmparg_any = &ipmr_mr_table_ops_cmparg_any,
371};
372
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000373static struct mr_table *ipmr_new_table(struct net *net, u32 id)
374{
375 struct mr_table *mrt;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000376
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100377 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
378 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
379 return ERR_PTR(-EINVAL);
380
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000381 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100382 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000383 return mrt;
384
Yuval Mintz845c9a72018-02-28 23:29:35 +0200385 return mr_table_alloc(net, id, &ipmr_mr_table_ops,
Yuval Mintz0bbbf0e2018-02-28 23:29:33 +0200386 ipmr_expire_process, ipmr_new_table_set);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000387}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000389static void ipmr_free_table(struct mr_table *mrt)
390{
391 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100392 mroute_clean_tables(mrt, true);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100393 rhltable_destroy(&mrt->mfc_hash);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000394 kfree(mrt);
395}
396
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
398
Wang Chend6070322008-07-14 20:55:26 -0700399static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
400{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000401 struct net *net = dev_net(dev);
402
Wang Chend6070322008-07-14 20:55:26 -0700403 dev_close(dev);
404
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000405 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700406 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800407 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700408 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700409 struct ip_tunnel_parm p;
410
411 memset(&p, 0, sizeof(p));
412 p.iph.daddr = v->vifc_rmt_addr.s_addr;
413 p.iph.saddr = v->vifc_lcl_addr.s_addr;
414 p.iph.version = 4;
415 p.iph.ihl = 5;
416 p.iph.protocol = IPPROTO_IPIP;
417 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
418 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
419
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800420 if (ops->ndo_do_ioctl) {
421 mm_segment_t oldfs = get_fs();
422
423 set_fs(KERNEL_DS);
424 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
425 set_fs(oldfs);
426 }
Wang Chend6070322008-07-14 20:55:26 -0700427 }
428}
429
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100430/* Initialize ipmr pimreg/tunnel in_device */
431static bool ipmr_init_vif_indev(const struct net_device *dev)
432{
433 struct in_device *in_dev;
434
435 ASSERT_RTNL();
436
437 in_dev = __in_dev_get_rtnl(dev);
438 if (!in_dev)
439 return false;
440 ipv4_devconf_setall(in_dev);
441 neigh_parms_data_state_setall(in_dev->arp_parms);
442 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
443
444 return true;
445}
446
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100447static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
449 struct net_device *dev;
450
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000451 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
453 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800454 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 int err;
456 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
459 memset(&p, 0, sizeof(p));
460 p.iph.daddr = v->vifc_rmt_addr.s_addr;
461 p.iph.saddr = v->vifc_lcl_addr.s_addr;
462 p.iph.version = 4;
463 p.iph.ihl = 5;
464 p.iph.protocol = IPPROTO_IPIP;
465 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800466 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800468 if (ops->ndo_do_ioctl) {
469 mm_segment_t oldfs = get_fs();
470
471 set_fs(KERNEL_DS);
472 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
473 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000474 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800475 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000476 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 dev = NULL;
478
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000479 if (err == 0 &&
480 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100482 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 if (dev_open(dev))
485 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700486 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488 }
489 return dev;
490
491failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 unregister_netdevice(dev);
493 return NULL;
494}
495
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100496#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000497static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000499 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000500 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500501 struct flowi4 fl4 = {
502 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700503 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500504 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000505 };
506 int err;
507
David S. Millerda919812011-03-12 02:04:50 -0500508 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000509 if (err < 0) {
510 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000511 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000512 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700515 dev->stats.tx_bytes += skb->len;
516 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000517 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 read_unlock(&mrt_lock);
519 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000520 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200523static int reg_vif_get_iflink(const struct net_device *dev)
524{
525 return 0;
526}
527
Stephen Hemminger007c3832008-11-20 20:28:35 -0800528static const struct net_device_ops reg_vif_netdev_ops = {
529 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200530 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800531};
532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533static void reg_vif_setup(struct net_device *dev)
534{
535 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800536 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530538 dev->netdev_ops = &reg_vif_netdev_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400539 dev->needs_free_netdev = true;
Tom Goff403dbb92009-06-14 03:16:13 -0700540 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541}
542
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000543static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000546 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000548 if (mrt->id == RT_TABLE_DEFAULT)
549 sprintf(name, "pimreg");
550 else
551 sprintf(name, "pimreg%u", mrt->id);
552
Tom Gundersenc835a672014-07-14 16:37:24 +0200553 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Ian Morris51456b22015-04-03 09:17:26 +0100555 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 return NULL;
557
Tom Goff403dbb92009-06-14 03:16:13 -0700558 dev_net_set(dev, net);
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 if (register_netdevice(dev)) {
561 free_netdev(dev);
562 return NULL;
563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100565 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 if (dev_open(dev))
568 goto failure;
569
Wang Chen7dc00c82008-07-14 20:56:34 -0700570 dev_hold(dev);
571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 return dev;
573
574failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 unregister_netdevice(dev);
576 return NULL;
577}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100578
579/* called with rcu_read_lock() */
580static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
581 unsigned int pimlen)
582{
583 struct net_device *reg_dev = NULL;
584 struct iphdr *encap;
585
586 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100587 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100588 * a. packet is really sent to a multicast group
589 * b. packet is not a NULL-REGISTER
590 * c. packet is not truncated
591 */
592 if (!ipv4_is_multicast(encap->daddr) ||
593 encap->tot_len == 0 ||
594 ntohs(encap->tot_len) + pimlen > skb->len)
595 return 1;
596
597 read_lock(&mrt_lock);
598 if (mrt->mroute_reg_vif_num >= 0)
599 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
600 read_unlock(&mrt_lock);
601
602 if (!reg_dev)
603 return 1;
604
605 skb->mac_header = skb->network_header;
606 skb_pull(skb, (u8 *)encap - skb->data);
607 skb_reset_network_header(skb);
608 skb->protocol = htons(ETH_P_IP);
609 skb->ip_summed = CHECKSUM_NONE;
610
611 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
612
613 netif_rx(skb);
614
615 return NET_RX_SUCCESS;
616}
617#else
618static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
619{
620 return NULL;
621}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622#endif
623
Yotam Gigi4d65b942017-09-27 08:23:13 +0200624static int call_ipmr_vif_entry_notifier(struct notifier_block *nb,
625 struct net *net,
626 enum fib_event_type event_type,
627 struct vif_device *vif,
628 vifi_t vif_index, u32 tb_id)
629{
630 struct vif_entry_notifier_info info = {
631 .info = {
632 .family = RTNL_FAMILY_IPMR,
633 .net = net,
634 },
635 .dev = vif->dev,
636 .vif_index = vif_index,
637 .vif_flags = vif->flags,
638 .tb_id = tb_id,
639 };
640
641 return call_fib_notifier(nb, net, event_type, &info.info);
642}
643
Yotam Gigib3620532017-09-27 08:23:14 +0200644static int call_ipmr_vif_entry_notifiers(struct net *net,
645 enum fib_event_type event_type,
646 struct vif_device *vif,
647 vifi_t vif_index, u32 tb_id)
648{
649 struct vif_entry_notifier_info info = {
650 .info = {
651 .family = RTNL_FAMILY_IPMR,
652 .net = net,
653 },
654 .dev = vif->dev,
655 .vif_index = vif_index,
656 .vif_flags = vif->flags,
657 .tb_id = tb_id,
658 };
659
660 ASSERT_RTNL();
661 net->ipv4.ipmr_seq++;
662 return call_fib_notifiers(net, event_type, &info.info);
663}
664
Yotam Gigi4d65b942017-09-27 08:23:13 +0200665static int call_ipmr_mfc_entry_notifier(struct notifier_block *nb,
666 struct net *net,
667 enum fib_event_type event_type,
668 struct mfc_cache *mfc, u32 tb_id)
669{
670 struct mfc_entry_notifier_info info = {
671 .info = {
672 .family = RTNL_FAMILY_IPMR,
673 .net = net,
674 },
675 .mfc = mfc,
676 .tb_id = tb_id
677 };
678
679 return call_fib_notifier(nb, net, event_type, &info.info);
680}
681
Yotam Gigib3620532017-09-27 08:23:14 +0200682static int call_ipmr_mfc_entry_notifiers(struct net *net,
683 enum fib_event_type event_type,
684 struct mfc_cache *mfc, u32 tb_id)
685{
686 struct mfc_entry_notifier_info info = {
687 .info = {
688 .family = RTNL_FAMILY_IPMR,
689 .net = net,
690 },
691 .mfc = mfc,
692 .tb_id = tb_id
693 };
694
695 ASSERT_RTNL();
696 net->ipv4.ipmr_seq++;
697 return call_fib_notifiers(net, event_type, &info.info);
698}
699
Ben Hutchings2c530402012-07-10 10:55:09 +0000700/**
701 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700702 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000704static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000705 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
Yotam Gigib3620532017-09-27 08:23:14 +0200707 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 struct vif_device *v;
709 struct net_device *dev;
710 struct in_device *in_dev;
711
Patrick McHardy0c122952010-04-13 05:03:22 +0000712 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 return -EADDRNOTAVAIL;
714
Patrick McHardy0c122952010-04-13 05:03:22 +0000715 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
Yotam Gigib3620532017-09-27 08:23:14 +0200717 if (VIF_EXISTS(mrt, vifi))
718 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_DEL, v, vifi,
719 mrt->id);
720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 write_lock_bh(&mrt_lock);
722 dev = v->dev;
723 v->dev = NULL;
724
725 if (!dev) {
726 write_unlock_bh(&mrt_lock);
727 return -EADDRNOTAVAIL;
728 }
729
Patrick McHardy0c122952010-04-13 05:03:22 +0000730 if (vifi == mrt->mroute_reg_vif_num)
731 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000733 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000735
736 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000737 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 break;
739 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000740 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 }
742
743 write_unlock_bh(&mrt_lock);
744
745 dev_set_allmulti(dev, -1);
746
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000747 in_dev = __in_dev_get_rtnl(dev);
748 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700749 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -0700750 inet_netconf_notify_devconf(dev_net(dev), RTM_NEWNETCONF,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000751 NETCONFA_MC_FORWARDING,
752 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 ip_rt_multicast_event(in_dev);
754 }
755
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000756 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000757 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 dev_put(dev);
760 return 0;
761}
762
Eric Dumazeta8c94862010-10-01 16:15:08 +0000763static void ipmr_cache_free_rcu(struct rcu_head *head)
764{
Yuval Mintz494fff52018-02-28 23:29:34 +0200765 struct mr_mfc *c = container_of(head, struct mr_mfc, rcu);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000766
Yuval Mintz494fff52018-02-28 23:29:34 +0200767 kmem_cache_free(mrt_cachep, (struct mfc_cache *)c);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000768}
769
Yotam Gigi310ebbb2017-09-27 08:23:12 +0200770void ipmr_cache_free(struct mfc_cache *c)
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000771{
Yuval Mintz494fff52018-02-28 23:29:34 +0200772 call_rcu(&c->_c.rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000773}
Yotam Gigi310ebbb2017-09-27 08:23:12 +0200774EXPORT_SYMBOL(ipmr_cache_free);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000775
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000777 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000779static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200781 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700783 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
Patrick McHardy0c122952010-04-13 05:03:22 +0000785 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Yuval Mintz494fff52018-02-28 23:29:34 +0200787 while ((skb = skb_dequeue(&c->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700788 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +0200789 struct nlmsghdr *nlh = skb_pull(skb,
790 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000792 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000794 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700795 e->error = -ETIMEDOUT;
796 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700797
Eric W. Biederman15e47302012-09-07 20:12:54 +0000798 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000799 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000801 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000804 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Patrick McHardye258beb2010-04-13 05:03:19 +0000807/* Timer process for the unresolved queue. */
Kees Cooke99e88a2017-10-16 14:43:17 -0700808static void ipmr_expire_process(struct timer_list *t)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Kees Cooke99e88a2017-10-16 14:43:17 -0700810 struct mr_table *mrt = from_timer(mrt, t, ipmr_expire_timer);
Yuval Mintz494fff52018-02-28 23:29:34 +0200811 struct mr_mfc *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 unsigned long expires;
Yuval Mintz494fff52018-02-28 23:29:34 +0200813 unsigned long now;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000816 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 return;
818 }
819
Patrick McHardy0c122952010-04-13 05:03:22 +0000820 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 goto out;
822
823 now = jiffies;
824 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
Patrick McHardy0c122952010-04-13 05:03:22 +0000826 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 if (time_after(c->mfc_un.unres.expires, now)) {
828 unsigned long interval = c->mfc_un.unres.expires - now;
829 if (interval < expires)
830 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 continue;
832 }
833
Patrick McHardy862465f2010-04-13 05:03:21 +0000834 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +0200835 mroute_netlink_event(mrt, (struct mfc_cache *)c, RTM_DELROUTE);
836 ipmr_destroy_unres(mrt, (struct mfc_cache *)c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 }
838
Patrick McHardy0c122952010-04-13 05:03:22 +0000839 if (!list_empty(&mrt->mfc_unres_queue))
840 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842out:
843 spin_unlock(&mfc_unres_lock);
844}
845
846/* Fill oifs list. It is called under write locked mrt_lock. */
Yuval Mintz494fff52018-02-28 23:29:34 +0200847static void ipmr_update_thresholds(struct mr_table *mrt, struct mr_mfc *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000848 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849{
850 int vifi;
851
852 cache->mfc_un.res.minvif = MAXVIFS;
853 cache->mfc_un.res.maxvif = 0;
854 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
855
Patrick McHardy0c122952010-04-13 05:03:22 +0000856 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
857 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000858 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
860 if (cache->mfc_un.res.minvif > vifi)
861 cache->mfc_un.res.minvif = vifi;
862 if (cache->mfc_un.res.maxvif <= vifi)
863 cache->mfc_un.res.maxvif = vifi + 1;
864 }
865 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200866 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867}
868
Patrick McHardy0c122952010-04-13 05:03:22 +0000869static int vif_add(struct net *net, struct mr_table *mrt,
870 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
872 int vifi = vifc->vifc_vifi;
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200873 struct switchdev_attr attr = {
874 .id = SWITCHDEV_ATTR_ID_PORT_PARENT_ID,
875 };
Patrick McHardy0c122952010-04-13 05:03:22 +0000876 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 struct net_device *dev;
878 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700879 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
881 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000882 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 return -EADDRINUSE;
884
885 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100887 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100888 return -EINVAL;
889 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 * All the packets will be sent to the daemon
891 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000892 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000894 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if (!dev)
896 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700897 err = dev_set_allmulti(dev, 1);
898 if (err) {
899 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700900 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700901 return err;
902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900904 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000905 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (!dev)
907 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700908 err = dev_set_allmulti(dev, 1);
909 if (err) {
910 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700911 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700912 return err;
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000915 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000917 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
918 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100919 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000920 dev_put(dev);
921 return -EADDRNOTAVAIL;
922 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000923 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000924 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000925 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 if (!dev)
927 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700928 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700929 if (err) {
930 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700931 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 break;
934 default:
935 return -EINVAL;
936 }
937
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000938 in_dev = __in_dev_get_rtnl(dev);
939 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000940 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000942 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700943 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -0700944 inet_netconf_notify_devconf(net, RTM_NEWNETCONF, NETCONFA_MC_FORWARDING,
945 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 ip_rt_multicast_event(in_dev);
947
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000948 /* Fill in the VIF structures */
Yuval Mintz6853f212018-02-28 23:29:29 +0200949 vif_device_init(v, dev, vifc->vifc_rate_limit,
950 vifc->vifc_threshold,
951 vifc->vifc_flags | (!mrtsock ? VIFF_STATIC : 0),
952 (VIFF_TUNNEL | VIFF_REGISTER));
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000953
Yotam Gigi5d8b3e62017-10-03 09:58:07 +0200954 attr.orig_dev = dev;
955 if (!switchdev_port_attr_get(dev, &attr)) {
956 memcpy(v->dev_parent_id.id, attr.u.ppid.id, attr.u.ppid.id_len);
957 v->dev_parent_id.id_len = attr.u.ppid.id_len;
958 } else {
959 v->dev_parent_id.id_len = 0;
960 }
Yuval Mintz6853f212018-02-28 23:29:29 +0200961
Jianjun Kongc354e122008-11-03 00:28:02 -0800962 v->local = vifc->vifc_lcl_addr.s_addr;
963 v->remote = vifc->vifc_rmt_addr.s_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 /* And finish update writing critical data */
966 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800967 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000968 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000969 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000970 if (vifi+1 > mrt->maxvif)
971 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +0200973 call_ipmr_vif_entry_notifiers(net, FIB_EVENT_VIF_ADD, v, vifi, mrt->id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 return 0;
975}
976
Eric Dumazeta8c94862010-10-01 16:15:08 +0000977/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000978static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000979 __be32 origin,
980 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100982 struct mfc_cache_cmp_arg arg = {
983 .mfc_mcastgrp = mcastgrp,
984 .mfc_origin = origin
985 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Yuval Mintz845c9a72018-02-28 23:29:35 +0200987 return mr_mfc_find(mrt, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000988}
989
990/* Look for a (*,G) entry */
991static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
992 __be32 mcastgrp, int vifi)
993{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +0100994 struct mfc_cache_cmp_arg arg = {
995 .mfc_mcastgrp = mcastgrp,
996 .mfc_origin = htonl(INADDR_ANY)
997 };
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000998
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100999 if (mcastgrp == htonl(INADDR_ANY))
Yuval Mintz845c9a72018-02-28 23:29:35 +02001000 return mr_mfc_find_any_parent(mrt, vifi);
1001 return mr_mfc_find_any(mrt, vifi, &arg);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001002}
1003
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001004/* Look for a (S,G,iif) entry if parent != -1 */
1005static struct mfc_cache *ipmr_cache_find_parent(struct mr_table *mrt,
1006 __be32 origin, __be32 mcastgrp,
1007 int parent)
1008{
1009 struct mfc_cache_cmp_arg arg = {
1010 .mfc_mcastgrp = mcastgrp,
1011 .mfc_origin = origin,
1012 };
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001013
Yuval Mintz845c9a72018-02-28 23:29:35 +02001014 return mr_mfc_find_parent(mrt, &arg, parent);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001015}
1016
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001017/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001018static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
Jianjun Kongc354e122008-11-03 00:28:02 -08001020 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001021
Tom Goff70a0dec2016-06-23 16:11:57 -04001022 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001023 c->_c.mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
1024 c->_c.mfc_un.res.minvif = MAXVIFS;
1025 refcount_set(&c->_c.mfc_un.res.refcount, 1);
Tom Goff70a0dec2016-06-23 16:11:57 -04001026 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return c;
1028}
1029
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001030static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Jianjun Kongc354e122008-11-03 00:28:02 -08001032 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +00001033
1034 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001035 skb_queue_head_init(&c->_c.mfc_un.unres.unresolved);
1036 c->_c.mfc_un.unres.expires = jiffies + 10 * HZ;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return c;
1039}
1040
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001041/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +00001042static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
1043 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001046 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001048 /* Play the pending entries through our router */
Yuval Mintz494fff52018-02-28 23:29:34 +02001049 while ((skb = __skb_dequeue(&uc->_c.mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001050 if (ip_hdr(skb)->version == 0) {
Johannes Bergaf728682017-06-16 14:29:22 +02001051 struct nlmsghdr *nlh = skb_pull(skb,
1052 sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
Yuval Mintz494fff52018-02-28 23:29:34 +02001054 if (__ipmr_fill_mroute(mrt, skb, &c->_c,
1055 nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001056 nlh->nlmsg_len = skb_tail_pointer(skb) -
1057 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 } else {
1059 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +00001060 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +00001062 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -07001063 e->error = -EMSGSIZE;
1064 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 }
Thomas Graf2942e902006-08-15 00:30:25 -07001066
Eric W. Biederman15e47302012-09-07 20:12:54 +00001067 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001068 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001069 ip_mr_forward(net, mrt, skb->dev, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001070 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 }
1072}
1073
Julien Gomes5a645dd2017-06-20 13:54:17 -07001074/* Bounce a cache query up to mrouted and netlink.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001076 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001078static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001079 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -03001081 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001082 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 struct igmphdr *igmp;
1084 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001085 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 int ret;
1087
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 if (assert == IGMPMSG_WHOLEPKT)
1089 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
1090 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 skb = alloc_skb(128, GFP_ATOMIC);
1092
Stephen Hemminger132adf52007-03-08 20:44:43 -08001093 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 return -ENOBUFS;
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 if (assert == IGMPMSG_WHOLEPKT) {
1097 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001098 * Duplicate old header, fix ihl, length etc.
1099 * And all this only to mangle msg->im_msgtype and
1100 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -03001102 skb_push(skb, sizeof(struct iphdr));
1103 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -03001104 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -03001105 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -07001106 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 msg->im_msgtype = IGMPMSG_WHOLEPKT;
1108 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001109 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001110 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
1111 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
1112 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001113 } else {
1114 /* Copy the IP header */
1115 skb_set_network_header(skb, skb->len);
1116 skb_put(skb, ihl);
1117 skb_copy_to_linear_data(skb, pkt->data, ihl);
1118 /* Flag to the kernel this is a route add */
1119 ip_hdr(skb)->protocol = 0;
1120 msg = (struct igmpmsg *)skb_network_header(skb);
1121 msg->im_vif = vifi;
1122 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
1123 /* Add our header */
Johannes Berg4df864c2017-06-16 14:29:21 +02001124 igmp = skb_put(skb, sizeof(struct igmphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001125 igmp->type = assert;
1126 msg->im_msgtype = assert;
1127 igmp->code = 0;
1128 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1129 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001130 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Eric Dumazet4c9687092010-10-01 16:15:01 +00001132 rcu_read_lock();
1133 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001134 if (!mroute_sk) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001135 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 kfree_skb(skb);
1137 return -EINVAL;
1138 }
1139
Julien Gomes5a645dd2017-06-20 13:54:17 -07001140 igmpmsg_netlink_event(mrt, skb);
1141
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001142 /* Deliver to mrouted */
Eric Dumazet4c9687092010-10-01 16:15:01 +00001143 ret = sock_queue_rcv_skb(mroute_sk, skb);
1144 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001145 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001146 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 kfree_skb(skb);
1148 }
1149
1150 return ret;
1151}
1152
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001153/* Queue a packet for resolution. It gets locked cache entry! */
1154static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001155 struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156{
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001157 const struct iphdr *iph = ip_hdr(skb);
1158 struct mfc_cache *c;
Patrick McHardy862465f2010-04-13 05:03:21 +00001159 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
1162 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001163 list_for_each_entry(c, &mrt->mfc_unres_queue, _c.list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001164 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001165 c->mfc_origin == iph->saddr) {
1166 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001168 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170
Patrick McHardy862465f2010-04-13 05:03:21 +00001171 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001172 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001173 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001174 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175 spin_unlock_bh(&mfc_unres_lock);
1176
1177 kfree_skb(skb);
1178 return -ENOBUFS;
1179 }
1180
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001181 /* Fill in the new cache entry */
Yuval Mintz494fff52018-02-28 23:29:34 +02001182 c->_c.mfc_parent = -1;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001183 c->mfc_origin = iph->saddr;
1184 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001186 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001187 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Yuval Mintz494fff52018-02-28 23:29:34 +02001188
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001189 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001190 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 out - Brad Parker
1192 */
1193 spin_unlock_bh(&mfc_unres_lock);
1194
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001195 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 kfree_skb(skb);
1197 return err;
1198 }
1199
Patrick McHardy0c122952010-04-13 05:03:22 +00001200 atomic_inc(&mrt->cache_resolve_queue_len);
Yuval Mintz494fff52018-02-28 23:29:34 +02001201 list_add(&c->_c.list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001202 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
David S. Miller278554b2010-05-12 00:05:35 -07001204 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
Yuval Mintz494fff52018-02-28 23:29:34 +02001205 mod_timer(&mrt->ipmr_expire_timer,
1206 c->_c.mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 }
1208
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001209 /* See if we can append the packet */
Yuval Mintz494fff52018-02-28 23:29:34 +02001210 if (c->_c.mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 kfree_skb(skb);
1212 err = -ENOBUFS;
1213 } else {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001214 if (dev) {
1215 skb->dev = dev;
1216 skb->skb_iif = dev->ifindex;
1217 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001218 skb_queue_tail(&c->_c.mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219 err = 0;
1220 }
1221
1222 spin_unlock_bh(&mfc_unres_lock);
1223 return err;
1224}
1225
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001226/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001228static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229{
Yotam Gigib3620532017-09-27 08:23:14 +02001230 struct net *net = read_pnet(&mrt->net);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001231 struct mfc_cache *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001233 /* The entries are added/deleted only under RTNL */
1234 rcu_read_lock();
1235 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1236 mfc->mfcc_mcastgrp.s_addr, parent);
1237 rcu_read_unlock();
1238 if (!c)
1239 return -ENOENT;
Yuval Mintz494fff52018-02-28 23:29:34 +02001240 rhltable_remove(&mrt->mfc_hash, &c->_c.mnode, ipmr_rht_params);
1241 list_del_rcu(&c->_c.list);
Yotam Gigib3620532017-09-27 08:23:14 +02001242 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, c, mrt->id);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001243 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Yotam Gigi310ebbb2017-09-27 08:23:12 +02001244 ipmr_cache_put(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}
1248
Patrick McHardy0c122952010-04-13 05:03:22 +00001249static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001250 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251{
Patrick McHardy862465f2010-04-13 05:03:21 +00001252 struct mfc_cache *uc, *c;
Yuval Mintz494fff52018-02-28 23:29:34 +02001253 struct mr_mfc *_uc;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001254 bool found;
1255 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
Patrick McHardya50436f22010-03-17 06:04:14 +00001257 if (mfc->mfcc_parent >= MAXVIFS)
1258 return -ENFILE;
1259
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001260 /* The entries are added/deleted only under RTNL */
1261 rcu_read_lock();
1262 c = ipmr_cache_find_parent(mrt, mfc->mfcc_origin.s_addr,
1263 mfc->mfcc_mcastgrp.s_addr, parent);
1264 rcu_read_unlock();
1265 if (c) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 write_lock_bh(&mrt_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001267 c->_c.mfc_parent = mfc->mfcc_parent;
1268 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001270 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 write_unlock_bh(&mrt_lock);
Yotam Gigib3620532017-09-27 08:23:14 +02001272 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_REPLACE, c,
1273 mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001274 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 return 0;
1276 }
1277
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001278 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001279 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280 return -EINVAL;
1281
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001282 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001283 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 return -ENOMEM;
1285
Jianjun Kongc354e122008-11-03 00:28:02 -08001286 c->mfc_origin = mfc->mfcc_origin.s_addr;
1287 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
Yuval Mintz494fff52018-02-28 23:29:34 +02001288 c->_c.mfc_parent = mfc->mfcc_parent;
1289 ipmr_update_thresholds(mrt, &c->_c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290 if (!mrtsock)
Yuval Mintz494fff52018-02-28 23:29:34 +02001291 c->_c.mfc_flags |= MFC_STATIC;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292
Yuval Mintz494fff52018-02-28 23:29:34 +02001293 ret = rhltable_insert_key(&mrt->mfc_hash, &c->cmparg, &c->_c.mnode,
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001294 ipmr_rht_params);
1295 if (ret) {
1296 pr_err("ipmr: rhtable insert error %d\n", ret);
1297 ipmr_cache_free(c);
1298 return ret;
1299 }
Yuval Mintz494fff52018-02-28 23:29:34 +02001300 list_add_tail_rcu(&c->_c.list, &mrt->mfc_cache_list);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001301 /* Check to see if we resolved a queued list. If so we
1302 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001304 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305 spin_lock_bh(&mfc_unres_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02001306 list_for_each_entry(_uc, &mrt->mfc_unres_queue, list) {
1307 uc = (struct mfc_cache *)_uc;
Patrick McHardye258beb2010-04-13 05:03:19 +00001308 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001310 list_del(&_uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001311 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001312 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 break;
1314 }
1315 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001316 if (list_empty(&mrt->mfc_unres_queue))
1317 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 spin_unlock_bh(&mfc_unres_lock);
1319
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001320 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001321 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001322 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001323 }
Yotam Gigib3620532017-09-27 08:23:14 +02001324 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_ADD, c, mrt->id);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001325 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326 return 0;
1327}
1328
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001329/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001330static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331{
Yotam Gigib3620532017-09-27 08:23:14 +02001332 struct net *net = read_pnet(&mrt->net);
Yuval Mintz494fff52018-02-28 23:29:34 +02001333 struct mr_mfc *c, *tmp;
1334 struct mfc_cache *cache;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001335 LIST_HEAD(list);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001336 int i;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001337
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001338 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001339 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001340 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1341 continue;
1342 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001344 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001346 /* Wipe the cache */
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001347 list_for_each_entry_safe(c, tmp, &mrt->mfc_cache_list, list) {
1348 if (!all && (c->mfc_flags & MFC_STATIC))
1349 continue;
1350 rhltable_remove(&mrt->mfc_hash, &c->mnode, ipmr_rht_params);
1351 list_del_rcu(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +02001352 cache = (struct mfc_cache *)c;
1353 call_ipmr_mfc_entry_notifiers(net, FIB_EVENT_ENTRY_DEL, cache,
Yotam Gigib3620532017-09-27 08:23:14 +02001354 mrt->id);
Yuval Mintz494fff52018-02-28 23:29:34 +02001355 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1356 ipmr_cache_put(cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 }
1358
Patrick McHardy0c122952010-04-13 05:03:22 +00001359 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 spin_lock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01001361 list_for_each_entry_safe(c, tmp, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001362 list_del(&c->list);
Yuval Mintz494fff52018-02-28 23:29:34 +02001363 cache = (struct mfc_cache *)c;
1364 mroute_netlink_event(mrt, cache, RTM_DELROUTE);
1365 ipmr_destroy_unres(mrt, cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367 spin_unlock_bh(&mfc_unres_lock);
1368 }
1369}
1370
Eric Dumazet4c9687092010-10-01 16:15:01 +00001371/* called from ip_ra_control(), before an RCU grace period,
1372 * we dont need to call synchronize_rcu() here
1373 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374static void mrtsock_destruct(struct sock *sk)
1375{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001376 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001377 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001378
WANG Cong1215e512017-04-12 12:32:13 -07001379 ASSERT_RTNL();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001380 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001381 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001382 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
David Ahern3b022862017-03-28 14:28:02 -07001383 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1384 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001385 NETCONFA_IFINDEX_ALL,
1386 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001387 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001388 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001389 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001393/* Socket options and virtual interface manipulation. The whole
1394 * virtual interface system is a complete heap, but unfortunately
1395 * that's how BSD mrouted happens to think. Maybe one day with a proper
1396 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001397 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001398
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001399int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1400 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001402 struct net *net = sock_net(sk);
1403 int val, ret = 0, parent = 0;
1404 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 struct vifctl vif;
1406 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001407 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001408
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001409 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1410 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001411 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001412 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1413 ret = -EOPNOTSUPP;
1414 goto out_unlock;
1415 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001416
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001417 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001418 if (!mrt) {
1419 ret = -ENOENT;
1420 goto out_unlock;
1421 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001422 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001423 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001424 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1425 ret = -EACCES;
1426 goto out_unlock;
1427 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 }
1429
Stephen Hemminger132adf52007-03-08 20:44:43 -08001430 switch (optname) {
1431 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001432 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001433 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001434 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001435 }
1436 if (rtnl_dereference(mrt->mroute_sk)) {
1437 ret = -EADDRINUSE;
1438 break;
1439 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001440
1441 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1442 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001443 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001444 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
David Ahern3b022862017-03-28 14:28:02 -07001445 inet_netconf_notify_devconf(net, RTM_NEWNETCONF,
1446 NETCONFA_MC_FORWARDING,
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001447 NETCONFA_IFINDEX_ALL,
1448 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001449 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001450 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001451 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001452 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1453 ret = -EACCES;
1454 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001455 ret = ip_ra_control(sk, 0, NULL);
WANG Cong1215e512017-04-12 12:32:13 -07001456 goto out_unlock;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001457 }
1458 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001459 case MRT_ADD_VIF:
1460 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001461 if (optlen != sizeof(vif)) {
1462 ret = -EINVAL;
1463 break;
1464 }
1465 if (copy_from_user(&vif, optval, sizeof(vif))) {
1466 ret = -EFAULT;
1467 break;
1468 }
1469 if (vif.vifc_vifi >= MAXVIFS) {
1470 ret = -ENFILE;
1471 break;
1472 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001473 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00001474 ret = vif_add(net, mrt, &vif,
1475 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001476 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001477 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001478 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001479 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001480 /* Manipulate the forwarding caches. These live
1481 * in a sort of kernel/user symbiosis.
1482 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001483 case MRT_ADD_MFC:
1484 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001485 parent = -1;
Gustavo A. R. Silvafcfd6df2017-10-16 15:48:55 -05001486 /* fall through */
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001487 case MRT_ADD_MFC_PROXY:
1488 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001489 if (optlen != sizeof(mfc)) {
1490 ret = -EINVAL;
1491 break;
1492 }
1493 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1494 ret = -EFAULT;
1495 break;
1496 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001497 if (parent == 0)
1498 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001499 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1500 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001501 else
Eric Dumazet4c9687092010-10-01 16:15:01 +00001502 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001503 sk == rtnl_dereference(mrt->mroute_sk),
1504 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001505 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001506 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001507 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001508 if (optlen != sizeof(val)) {
1509 ret = -EINVAL;
1510 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001512 if (get_user(val, (int __user *)optval)) {
1513 ret = -EFAULT;
1514 break;
1515 }
1516 mrt->mroute_do_assert = val;
1517 break;
1518 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001519 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001520 ret = -ENOPROTOOPT;
1521 break;
1522 }
1523 if (optlen != sizeof(val)) {
1524 ret = -EINVAL;
1525 break;
1526 }
1527 if (get_user(val, (int __user *)optval)) {
1528 ret = -EFAULT;
1529 break;
1530 }
1531
1532 val = !!val;
1533 if (val != mrt->mroute_do_pim) {
1534 mrt->mroute_do_pim = val;
1535 mrt->mroute_do_assert = val;
1536 }
1537 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001538 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001539 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1540 ret = -ENOPROTOOPT;
1541 break;
1542 }
1543 if (optlen != sizeof(uval)) {
1544 ret = -EINVAL;
1545 break;
1546 }
1547 if (get_user(uval, (u32 __user *)optval)) {
1548 ret = -EFAULT;
1549 break;
1550 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001551
Eric Dumazet4c9687092010-10-01 16:15:01 +00001552 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1553 ret = -EBUSY;
1554 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001555 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001556 if (IS_ERR(mrt))
1557 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001558 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001559 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c9687092010-10-01 16:15:01 +00001560 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001561 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001562 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001563 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001564 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001566out_unlock:
1567 rtnl_unlock();
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001568 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569}
1570
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001571/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001572int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573{
1574 int olr;
1575 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001576 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001577 struct mr_table *mrt;
1578
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001579 if (sk->sk_type != SOCK_RAW ||
1580 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1581 return -EOPNOTSUPP;
1582
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001583 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001584 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001585 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001587 switch (optname) {
1588 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001589 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001590 break;
1591 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001592 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001593 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001594 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001595 break;
1596 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001597 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001598 break;
1599 default:
1600 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001601 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001602
1603 if (get_user(olr, optlen))
1604 return -EFAULT;
1605 olr = min_t(unsigned int, olr, sizeof(int));
1606 if (olr < 0)
1607 return -EINVAL;
1608 if (put_user(olr, optlen))
1609 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001610 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001611 return -EFAULT;
1612 return 0;
1613}
1614
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001615/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1617{
1618 struct sioc_sg_req sr;
1619 struct sioc_vif_req vr;
1620 struct vif_device *vif;
1621 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001622 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001623 struct mr_table *mrt;
1624
1625 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001626 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001627 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001628
Stephen Hemminger132adf52007-03-08 20:44:43 -08001629 switch (cmd) {
1630 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001631 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001632 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001633 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001634 return -EINVAL;
1635 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001636 vif = &mrt->vif_table[vr.vifi];
1637 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001638 vr.icount = vif->pkt_in;
1639 vr.ocount = vif->pkt_out;
1640 vr.ibytes = vif->bytes_in;
1641 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001643
Jianjun Kongc354e122008-11-03 00:28:02 -08001644 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001646 return 0;
1647 }
1648 read_unlock(&mrt_lock);
1649 return -EADDRNOTAVAIL;
1650 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001651 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001652 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Eric Dumazeta8c94862010-10-01 16:15:08 +00001654 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001655 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001656 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001657 sr.pktcnt = c->_c.mfc_un.res.pkt;
1658 sr.bytecnt = c->_c.mfc_un.res.bytes;
1659 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001660 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001661
Jianjun Kongc354e122008-11-03 00:28:02 -08001662 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001663 return -EFAULT;
1664 return 0;
1665 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001666 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001667 return -EADDRNOTAVAIL;
1668 default:
1669 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 }
1671}
1672
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001673#ifdef CONFIG_COMPAT
1674struct compat_sioc_sg_req {
1675 struct in_addr src;
1676 struct in_addr grp;
1677 compat_ulong_t pktcnt;
1678 compat_ulong_t bytecnt;
1679 compat_ulong_t wrong_if;
1680};
1681
David S. Millerca6b8bb02011-02-03 17:24:28 -08001682struct compat_sioc_vif_req {
1683 vifi_t vifi; /* Which iface */
1684 compat_ulong_t icount;
1685 compat_ulong_t ocount;
1686 compat_ulong_t ibytes;
1687 compat_ulong_t obytes;
1688};
1689
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001690int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1691{
David S. Miller0033d5a2011-02-03 17:21:31 -08001692 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001693 struct compat_sioc_vif_req vr;
1694 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001695 struct mfc_cache *c;
1696 struct net *net = sock_net(sk);
1697 struct mr_table *mrt;
1698
1699 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001700 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001701 return -ENOENT;
1702
1703 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001704 case SIOCGETVIFCNT:
1705 if (copy_from_user(&vr, arg, sizeof(vr)))
1706 return -EFAULT;
1707 if (vr.vifi >= mrt->maxvif)
1708 return -EINVAL;
1709 read_lock(&mrt_lock);
1710 vif = &mrt->vif_table[vr.vifi];
1711 if (VIF_EXISTS(mrt, vr.vifi)) {
1712 vr.icount = vif->pkt_in;
1713 vr.ocount = vif->pkt_out;
1714 vr.ibytes = vif->bytes_in;
1715 vr.obytes = vif->bytes_out;
1716 read_unlock(&mrt_lock);
1717
1718 if (copy_to_user(arg, &vr, sizeof(vr)))
1719 return -EFAULT;
1720 return 0;
1721 }
1722 read_unlock(&mrt_lock);
1723 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001724 case SIOCGETSGCNT:
1725 if (copy_from_user(&sr, arg, sizeof(sr)))
1726 return -EFAULT;
1727
1728 rcu_read_lock();
1729 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1730 if (c) {
Yuval Mintz494fff52018-02-28 23:29:34 +02001731 sr.pktcnt = c->_c.mfc_un.res.pkt;
1732 sr.bytecnt = c->_c.mfc_un.res.bytes;
1733 sr.wrong_if = c->_c.mfc_un.res.wrong_if;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001734 rcu_read_unlock();
1735
1736 if (copy_to_user(arg, &sr, sizeof(sr)))
1737 return -EFAULT;
1738 return 0;
1739 }
1740 rcu_read_unlock();
1741 return -EADDRNOTAVAIL;
1742 default:
1743 return -ENOIOCTLCMD;
1744 }
1745}
1746#endif
1747
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1749{
Jiri Pirko351638e2013-05-28 01:30:21 +00001750 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001751 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001752 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 struct vif_device *v;
1754 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 if (event != NETDEV_UNREGISTER)
1757 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001758
1759 ipmr_for_each_table(mrt, net) {
1760 v = &mrt->vif_table[0];
1761 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1762 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001763 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001764 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 }
1766 return NOTIFY_DONE;
1767}
1768
Jianjun Kongc354e122008-11-03 00:28:02 -08001769static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770 .notifier_call = ipmr_device_event,
1771};
1772
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001773/* Encapsulate a packet by attaching a valid IPIP header to it.
1774 * This avoids tunnel drivers and other mess and gives us the speed so
1775 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001777static void ip_encap(struct net *net, struct sk_buff *skb,
1778 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001780 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001781 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001782
1783 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001784 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001785 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001786 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001788 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001789 iph->tos = old_iph->tos;
1790 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 iph->frag_off = 0;
1792 iph->daddr = daddr;
1793 iph->saddr = saddr;
1794 iph->protocol = IPPROTO_IPIP;
1795 iph->ihl = 5;
1796 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001797 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 ip_send_check(iph);
1799
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1801 nf_reset(skb);
1802}
1803
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001804static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1805 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001807 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
David S. Miller73186df2015-11-03 13:41:45 -05001809 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1810 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811
1812 if (unlikely(opt->optlen))
1813 ip_forward_options(skb);
1814
Eric W. Biederman13206b62015-10-07 16:48:35 -05001815 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
1817
Yotam Gigia5bc9292017-10-03 09:58:08 +02001818#ifdef CONFIG_NET_SWITCHDEV
1819static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1820 int in_vifi, int out_vifi)
1821{
1822 struct vif_device *out_vif = &mrt->vif_table[out_vifi];
1823 struct vif_device *in_vif = &mrt->vif_table[in_vifi];
1824
1825 if (!skb->offload_mr_fwd_mark)
1826 return false;
1827 if (!out_vif->dev_parent_id.id_len || !in_vif->dev_parent_id.id_len)
1828 return false;
1829 return netdev_phys_item_id_same(&out_vif->dev_parent_id,
1830 &in_vif->dev_parent_id);
1831}
1832#else
1833static bool ipmr_forward_offloaded(struct sk_buff *skb, struct mr_table *mrt,
1834 int in_vifi, int out_vifi)
1835{
1836 return false;
1837}
1838#endif
1839
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001840/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Patrick McHardy0c122952010-04-13 05:03:22 +00001842static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
Yotam Gigia5bc9292017-10-03 09:58:08 +02001843 int in_vifi, struct sk_buff *skb,
1844 struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001846 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001847 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 struct net_device *dev;
1849 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001850 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 int encap = 0;
1852
Ian Morris51456b22015-04-03 09:17:26 +01001853 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 goto out_free;
1855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 if (vif->flags & VIFF_REGISTER) {
1857 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001858 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001859 vif->dev->stats.tx_bytes += skb->len;
1860 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001861 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001862 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
Yotam Gigia5bc9292017-10-03 09:58:08 +02001865 if (ipmr_forward_offloaded(skb, mrt, in_vifi, vifi))
1866 goto out_free;
1867
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001868 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001869 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001870 vif->remote, vif->local,
1871 0, 0,
1872 IPPROTO_IPIP,
1873 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001874 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875 goto out_free;
1876 encap = sizeof(struct iphdr);
1877 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001878 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001879 0, 0,
1880 IPPROTO_IPIP,
1881 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001882 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883 goto out_free;
1884 }
1885
Changli Gaod8d1f302010-06-10 23:31:35 -07001886 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887
Changli Gaod8d1f302010-06-10 23:31:35 -07001888 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001890 * allow to send ICMP, so that packets will disappear
1891 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 */
David S. Miller73186df2015-11-03 13:41:45 -05001893 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 ip_rt_put(rt);
1895 goto out_free;
1896 }
1897
Changli Gaod8d1f302010-06-10 23:31:35 -07001898 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
1900 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001901 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 goto out_free;
1903 }
1904
1905 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001906 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Eric Dumazetadf30902009-06-02 05:19:30 +00001908 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001909 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001910 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911
1912 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001913 * What do we do with netfilter? -- RR
1914 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001916 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001918 vif->dev->stats.tx_packets++;
1919 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
1921
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001922 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001924 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 * not only before forwarding, but after forwarding on all output
1926 * interfaces. It is clear, if mrouter runs a multicasting
1927 * program, it should receive packets not depending to what interface
1928 * program is joined.
1929 * If we will not make it, the program will have to join on all
1930 * interfaces. On the other hand, multihoming host (or router, but
1931 * not mrouter) cannot join to more than one interface - it will
1932 * result in receiving multiple packets.
1933 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001934 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1935 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 ipmr_forward_finish);
1937 return;
1938
1939out_free:
1940 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001941}
1942
Patrick McHardy0c122952010-04-13 05:03:22 +00001943static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944{
1945 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001946
1947 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1948 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949 break;
1950 }
1951 return ct;
1952}
1953
1954/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001955static void ip_mr_forward(struct net *net, struct mr_table *mrt,
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001956 struct net_device *dev, struct sk_buff *skb,
Yuval Mintz494fff52018-02-28 23:29:34 +02001957 struct mfc_cache *c, int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958{
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001959 int true_vifi = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 int psend = -1;
1961 int vif, ct;
1962
Yuval Mintz494fff52018-02-28 23:29:34 +02001963 vif = c->_c.mfc_parent;
1964 c->_c.mfc_un.res.pkt++;
1965 c->_c.mfc_un.res.bytes += skb->len;
1966 c->_c.mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Yuval Mintz494fff52018-02-28 23:29:34 +02001968 if (c->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001969 struct mfc_cache *cache_proxy;
1970
1971 /* For an (*,G) entry, we only check that the incomming
1972 * interface is part of the static tree.
1973 */
Yuval Mintz845c9a72018-02-28 23:29:35 +02001974 cache_proxy = mr_mfc_find_any_parent(mrt, vif);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001975 if (cache_proxy &&
Yuval Mintz494fff52018-02-28 23:29:34 +02001976 cache_proxy->_c.mfc_un.res.ttls[true_vifi] < 255)
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001977 goto forward;
1978 }
1979
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001980 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Donald Sharp4b1f0d32017-06-10 16:30:17 -04001981 if (mrt->vif_table[vif].dev != dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001982 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001984 * Very complicated situation...
1985 *
1986 * The best workaround until routing daemons will be
1987 * fixed is not to redistribute packet, if it was
1988 * send through wrong interface. It means, that
1989 * multicast applications WILL NOT work for
1990 * (S,G), which have default multicast route pointing
1991 * to wrong oif. In any case, it is not a good
1992 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 */
1994 goto dont_forward;
1995 }
1996
Yuval Mintz494fff52018-02-28 23:29:34 +02001997 c->_c.mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998
Patrick McHardy0c122952010-04-13 05:03:22 +00001999 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002001 * so that we cannot check that packet arrived on an oif.
2002 * It is bad, but otherwise we would need to move pretty
2003 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 */
Patrick McHardy0c122952010-04-13 05:03:22 +00002005 (mrt->mroute_do_pim ||
Yuval Mintz494fff52018-02-28 23:29:34 +02002006 c->_c.mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002007 time_after(jiffies,
Yuval Mintz494fff52018-02-28 23:29:34 +02002008 c->_c.mfc_un.res.last_assert +
2009 MFC_ASSERT_THRESH)) {
2010 c->_c.mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00002011 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013 goto dont_forward;
2014 }
2015
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002016forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00002017 mrt->vif_table[vif].pkt_in++;
2018 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002020 /* Forward the frame */
Yuval Mintz494fff52018-02-28 23:29:34 +02002021 if (c->mfc_origin == htonl(INADDR_ANY) &&
2022 c->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002023 if (true_vifi >= 0 &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002024 true_vifi != c->_c.mfc_parent &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002025 ip_hdr(skb)->ttl >
Yuval Mintz494fff52018-02-28 23:29:34 +02002026 c->_c.mfc_un.res.ttls[c->_c.mfc_parent]) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002027 /* It's an (*,*) entry and the packet is not coming from
2028 * the upstream: forward the packet to the upstream
2029 * only.
2030 */
Yuval Mintz494fff52018-02-28 23:29:34 +02002031 psend = c->_c.mfc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002032 goto last_forward;
2033 }
2034 goto dont_forward;
2035 }
Yuval Mintz494fff52018-02-28 23:29:34 +02002036 for (ct = c->_c.mfc_un.res.maxvif - 1;
2037 ct >= c->_c.mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002038 /* For (*,G) entry, don't forward to the incoming interface */
Yuval Mintz494fff52018-02-28 23:29:34 +02002039 if ((c->mfc_origin != htonl(INADDR_ANY) ||
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01002040 ct != true_vifi) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02002041 ip_hdr(skb)->ttl > c->_c.mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 if (psend != -1) {
2043 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002044
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002046 ipmr_queue_xmit(net, mrt, true_vifi,
Yuval Mintz494fff52018-02-28 23:29:34 +02002047 skb2, c, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 }
Jianjun Kongc354e122008-11-03 00:28:02 -08002049 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 }
2051 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002052last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 if (psend != -1) {
2054 if (local) {
2055 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002056
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 if (skb2)
Yotam Gigia5bc9292017-10-03 09:58:08 +02002058 ipmr_queue_xmit(net, mrt, true_vifi, skb2,
Yuval Mintz494fff52018-02-28 23:29:34 +02002059 c, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 } else {
Yuval Mintz494fff52018-02-28 23:29:34 +02002061 ipmr_queue_xmit(net, mrt, true_vifi, skb, c, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03002062 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 }
2064 }
2065
2066dont_forward:
2067 if (!local)
2068 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069}
2070
David S. Miller417da662011-05-03 19:42:43 -07002071static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08002072{
David S. Miller417da662011-05-03 19:42:43 -07002073 struct rtable *rt = skb_rtable(skb);
2074 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05002075 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07002076 .daddr = iph->daddr,
2077 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00002078 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07002079 .flowi4_oif = (rt_is_output_route(rt) ?
2080 skb->dev->ifindex : 0),
2081 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00002082 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07002083 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00002084 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08002085 };
2086 struct mr_table *mrt;
2087 int err;
2088
David S. Millerda919812011-03-12 02:04:50 -05002089 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002090 if (err)
2091 return ERR_PTR(err);
2092 return mrt;
2093}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002095/* Multicast packets for forwarding arrive here
2096 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098int ip_mr_input(struct sk_buff *skb)
2099{
2100 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002101 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00002102 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002103 struct mr_table *mrt;
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002104 struct net_device *dev;
2105
2106 /* skb->dev passed in is the loX master dev for vrfs.
2107 * As there are no vifs associated with loopback devices,
2108 * get the proper interface that does have a vif associated with it.
2109 */
2110 dev = skb->dev;
2111 if (netif_is_l3_master(skb->dev)) {
2112 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
2113 if (!dev) {
2114 kfree_skb(skb);
2115 return -ENODEV;
2116 }
2117 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002120 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121 */
Eric Dumazet4c9687092010-10-01 16:15:01 +00002122 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123 goto dont_forward;
2124
David S. Miller417da662011-05-03 19:42:43 -07002125 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002126 if (IS_ERR(mrt)) {
2127 kfree_skb(skb);
2128 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00002129 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 if (!local) {
Eric Dumazet4c9687092010-10-01 16:15:01 +00002131 if (IPCB(skb)->opt.router_alert) {
2132 if (ip_call_ra_chain(skb))
2133 return 0;
2134 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
2135 /* IGMPv1 (and broken IGMPv2 implementations sort of
2136 * Cisco IOS <= 11.2(8)) do not put router alert
2137 * option to IGMP packets destined to routable
2138 * groups. It is very bad, because it means
2139 * that we can forward NO IGMP messages.
2140 */
2141 struct sock *mroute_sk;
2142
2143 mroute_sk = rcu_dereference(mrt->mroute_sk);
2144 if (mroute_sk) {
2145 nf_reset(skb);
2146 raw_rcv(mroute_sk, skb);
2147 return 0;
2148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 }
2150 }
2151
Eric Dumazeta8c94862010-10-01 16:15:08 +00002152 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00002153 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002154 if (!cache) {
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002155 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002156
2157 if (vif >= 0)
2158 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
2159 vif);
2160 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002162 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01002163 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 int vif;
2165
2166 if (local) {
2167 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2168 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002169 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 skb = skb2;
2172 }
2173
Eric Dumazeta8c94862010-10-01 16:15:08 +00002174 read_lock(&mrt_lock);
Thomas Winterbcfc7d32017-05-16 10:14:44 +12002175 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 if (vif >= 0) {
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002177 int err2 = ipmr_cache_unresolved(mrt, vif, skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 read_unlock(&mrt_lock);
2179
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002180 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002181 }
2182 read_unlock(&mrt_lock);
2183 kfree_skb(skb);
2184 return -ENODEV;
2185 }
2186
Eric Dumazeta8c94862010-10-01 16:15:08 +00002187 read_lock(&mrt_lock);
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002188 ip_mr_forward(net, mrt, dev, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 read_unlock(&mrt_lock);
2190
2191 if (local)
2192 return ip_local_deliver(skb);
2193
2194 return 0;
2195
2196dont_forward:
2197 if (local)
2198 return ip_local_deliver(skb);
2199 kfree_skb(skb);
2200 return 0;
2201}
2202
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002203#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002204/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002205int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002206{
2207 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002208 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002209 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002210
2211 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2212 goto drop;
2213
2214 pim = igmp_hdr(skb);
2215
David S. Miller417da662011-05-03 19:42:43 -07002216 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002217 if (IS_ERR(mrt))
2218 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002219 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002220 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2221 goto drop;
2222
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002223 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002224drop:
2225 kfree_skb(skb);
2226 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 return 0;
2228}
2229#endif
2230
2231#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002232static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233{
2234 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002235 struct net *net = dev_net(skb->dev);
2236 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002238 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 goto drop;
2240
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002241 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002242 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002243 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002244 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002245 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 goto drop;
2247
David S. Miller417da662011-05-03 19:42:43 -07002248 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002249 if (IS_ERR(mrt))
2250 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002251 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002252drop:
2253 kfree_skb(skb);
2254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 return 0;
2256}
2257#endif
2258
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002259static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Yuval Mintz494fff52018-02-28 23:29:34 +02002260 struct mr_mfc *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261{
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002262 struct rta_mfc_stats mfcs;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002263 struct nlattr *mp_attr;
2264 struct rtnexthop *nhp;
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002265 unsigned long lastuse;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002266 int ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267
Nicolas Dichtel74381892010-03-25 23:45:35 +00002268 /* If cache is unresolved, don't try to parse IIF and OIF */
Nikolay Aleksandrov1708ebc2017-01-03 12:13:39 +01002269 if (c->mfc_parent >= MAXVIFS) {
2270 rtm->rtm_flags |= RTNH_F_UNRESOLVED;
Nicolas Dichtel74381892010-03-25 23:45:35 +00002271 return -ENOENT;
Nikolay Aleksandrov1708ebc2017-01-03 12:13:39 +01002272 }
Nicolas Dichtel74381892010-03-25 23:45:35 +00002273
Thomas Graf92a395e2012-06-26 23:36:13 +00002274 if (VIF_EXISTS(mrt, c->mfc_parent) &&
Yuval Mintz6853f212018-02-28 23:29:29 +02002275 nla_put_u32(skb, RTA_IIF,
2276 mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
Thomas Graf92a395e2012-06-26 23:36:13 +00002277 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Yotam Gigic7c0bbe2017-09-27 08:23:15 +02002279 if (c->mfc_flags & MFC_OFFLOAD)
2280 rtm->rtm_flags |= RTNH_F_OFFLOAD;
2281
Thomas Graf92a395e2012-06-26 23:36:13 +00002282 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2283 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284
2285 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002286 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Yuval Mintz6853f212018-02-28 23:29:29 +02002287 struct vif_device *vif;
2288
Thomas Graf92a395e2012-06-26 23:36:13 +00002289 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2290 nla_nest_cancel(skb, mp_attr);
2291 return -EMSGSIZE;
2292 }
2293
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 nhp->rtnh_flags = 0;
2295 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Yuval Mintz6853f212018-02-28 23:29:29 +02002296 vif = &mrt->vif_table[ct];
2297 nhp->rtnh_ifindex = vif->dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 nhp->rtnh_len = sizeof(*nhp);
2299 }
2300 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002301
2302 nla_nest_end(skb, mp_attr);
2303
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002304 lastuse = READ_ONCE(c->mfc_un.res.lastuse);
2305 lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
2306
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002307 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2308 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2309 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002310 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002311 nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002312 RTA_PAD))
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002313 return -EMSGSIZE;
2314
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 rtm->rtm_type = RTN_MULTICAST;
2316 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317}
2318
David S. Miller9a1b9492011-05-04 12:18:54 -07002319int ipmr_get_route(struct net *net, struct sk_buff *skb,
2320 __be32 saddr, __be32 daddr,
David Ahern9f09eae2017-01-06 17:39:06 -08002321 struct rtmsg *rtm, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002322{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002324 struct mr_table *mrt;
2325 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002327 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002328 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002329 return -ENOENT;
2330
Eric Dumazeta8c94862010-10-01 16:15:08 +00002331 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002332 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002333 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002334 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002336 if (vif >= 0)
2337 cache = ipmr_cache_find_any(mrt, daddr, vif);
2338 }
Ian Morris51456b22015-04-03 09:17:26 +01002339 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002340 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002341 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002343 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Linus Torvalds1da177e2005-04-16 15:20:36 -07002345 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002346 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002347 if (dev)
2348 vif = ipmr_find_vif(mrt, dev);
2349 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002350 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002351 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352 return -ENODEV;
2353 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002354 skb2 = skb_clone(skb, GFP_ATOMIC);
2355 if (!skb2) {
2356 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002357 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002358 return -ENOMEM;
2359 }
2360
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002361 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002362 skb_push(skb2, sizeof(struct iphdr));
2363 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002364 iph = ip_hdr(skb2);
2365 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002366 iph->saddr = saddr;
2367 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002368 iph->version = 0;
Donald Sharp4b1f0d32017-06-10 16:30:17 -04002369 err = ipmr_cache_unresolved(mrt, vif, skb2, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002371 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 return err;
2373 }
2374
Eric Dumazeta8c94862010-10-01 16:15:08 +00002375 read_lock(&mrt_lock);
Yuval Mintz494fff52018-02-28 23:29:34 +02002376 err = __ipmr_fill_mroute(mrt, skb, &cache->_c, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002378 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 return err;
2380}
2381
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002382static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002383 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2384 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002385{
2386 struct nlmsghdr *nlh;
2387 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002388 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002389
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002390 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002391 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002392 return -EMSGSIZE;
2393
2394 rtm = nlmsg_data(nlh);
2395 rtm->rtm_family = RTNL_FAMILY_IPMR;
2396 rtm->rtm_dst_len = 32;
2397 rtm->rtm_src_len = 32;
2398 rtm->rtm_tos = 0;
2399 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002400 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2401 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002402 rtm->rtm_type = RTN_MULTICAST;
2403 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Yuval Mintz494fff52018-02-28 23:29:34 +02002404 if (c->_c.mfc_flags & MFC_STATIC)
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002405 rtm->rtm_protocol = RTPROT_STATIC;
2406 else
2407 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002408 rtm->rtm_flags = 0;
2409
Jiri Benc930345e2015-03-29 16:59:25 +02002410 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2411 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002412 goto nla_put_failure;
Yuval Mintz494fff52018-02-28 23:29:34 +02002413 err = __ipmr_fill_mroute(mrt, skb, &c->_c, rtm);
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002414 /* do not break the dump if cache is unresolved */
2415 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002416 goto nla_put_failure;
2417
Johannes Berg053c0952015-01-16 22:09:00 +01002418 nlmsg_end(skb, nlh);
2419 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002420
2421nla_put_failure:
2422 nlmsg_cancel(skb, nlh);
2423 return -EMSGSIZE;
2424}
2425
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002426static size_t mroute_msgsize(bool unresolved, int maxvif)
2427{
2428 size_t len =
2429 NLMSG_ALIGN(sizeof(struct rtmsg))
2430 + nla_total_size(4) /* RTA_TABLE */
2431 + nla_total_size(4) /* RTA_SRC */
2432 + nla_total_size(4) /* RTA_DST */
2433 ;
2434
2435 if (!unresolved)
2436 len = len
2437 + nla_total_size(4) /* RTA_IIF */
2438 + nla_total_size(0) /* RTA_MULTIPATH */
2439 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2440 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002441 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002442 ;
2443
2444 return len;
2445}
2446
2447static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2448 int cmd)
2449{
2450 struct net *net = read_pnet(&mrt->net);
2451 struct sk_buff *skb;
2452 int err = -ENOBUFS;
2453
Yuval Mintz494fff52018-02-28 23:29:34 +02002454 skb = nlmsg_new(mroute_msgsize(mfc->_c.mfc_parent >= MAXVIFS,
2455 mrt->maxvif),
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002456 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002457 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002458 goto errout;
2459
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002460 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002461 if (err < 0)
2462 goto errout;
2463
2464 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2465 return;
2466
2467errout:
2468 kfree_skb(skb);
2469 if (err < 0)
2470 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2471}
2472
Julien Gomes5a645dd2017-06-20 13:54:17 -07002473static size_t igmpmsg_netlink_msgsize(size_t payloadlen)
2474{
2475 size_t len =
2476 NLMSG_ALIGN(sizeof(struct rtgenmsg))
2477 + nla_total_size(1) /* IPMRA_CREPORT_MSGTYPE */
2478 + nla_total_size(4) /* IPMRA_CREPORT_VIF_ID */
2479 + nla_total_size(4) /* IPMRA_CREPORT_SRC_ADDR */
2480 + nla_total_size(4) /* IPMRA_CREPORT_DST_ADDR */
2481 /* IPMRA_CREPORT_PKT */
2482 + nla_total_size(payloadlen)
2483 ;
2484
2485 return len;
2486}
2487
2488static void igmpmsg_netlink_event(struct mr_table *mrt, struct sk_buff *pkt)
2489{
2490 struct net *net = read_pnet(&mrt->net);
2491 struct nlmsghdr *nlh;
2492 struct rtgenmsg *rtgenm;
2493 struct igmpmsg *msg;
2494 struct sk_buff *skb;
2495 struct nlattr *nla;
2496 int payloadlen;
2497
2498 payloadlen = pkt->len - sizeof(struct igmpmsg);
2499 msg = (struct igmpmsg *)skb_network_header(pkt);
2500
2501 skb = nlmsg_new(igmpmsg_netlink_msgsize(payloadlen), GFP_ATOMIC);
2502 if (!skb)
2503 goto errout;
2504
2505 nlh = nlmsg_put(skb, 0, 0, RTM_NEWCACHEREPORT,
2506 sizeof(struct rtgenmsg), 0);
2507 if (!nlh)
2508 goto errout;
2509 rtgenm = nlmsg_data(nlh);
2510 rtgenm->rtgen_family = RTNL_FAMILY_IPMR;
2511 if (nla_put_u8(skb, IPMRA_CREPORT_MSGTYPE, msg->im_msgtype) ||
2512 nla_put_u32(skb, IPMRA_CREPORT_VIF_ID, msg->im_vif) ||
2513 nla_put_in_addr(skb, IPMRA_CREPORT_SRC_ADDR,
2514 msg->im_src.s_addr) ||
2515 nla_put_in_addr(skb, IPMRA_CREPORT_DST_ADDR,
2516 msg->im_dst.s_addr))
2517 goto nla_put_failure;
2518
2519 nla = nla_reserve(skb, IPMRA_CREPORT_PKT, payloadlen);
2520 if (!nla || skb_copy_bits(pkt, sizeof(struct igmpmsg),
2521 nla_data(nla), payloadlen))
2522 goto nla_put_failure;
2523
2524 nlmsg_end(skb, nlh);
2525
2526 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE_R, NULL, GFP_ATOMIC);
2527 return;
2528
2529nla_put_failure:
2530 nlmsg_cancel(skb, nlh);
2531errout:
2532 kfree_skb(skb);
2533 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE_R, -ENOBUFS);
2534}
2535
Donald Sharp4f75ba62017-06-28 13:58:57 -04002536static int ipmr_rtm_getroute(struct sk_buff *in_skb, struct nlmsghdr *nlh,
2537 struct netlink_ext_ack *extack)
2538{
2539 struct net *net = sock_net(in_skb->sk);
2540 struct nlattr *tb[RTA_MAX + 1];
2541 struct sk_buff *skb = NULL;
2542 struct mfc_cache *cache;
2543 struct mr_table *mrt;
2544 struct rtmsg *rtm;
2545 __be32 src, grp;
2546 u32 tableid;
2547 int err;
2548
2549 err = nlmsg_parse(nlh, sizeof(*rtm), tb, RTA_MAX,
2550 rtm_ipv4_policy, extack);
2551 if (err < 0)
2552 goto errout;
2553
2554 rtm = nlmsg_data(nlh);
2555
2556 src = tb[RTA_SRC] ? nla_get_in_addr(tb[RTA_SRC]) : 0;
2557 grp = tb[RTA_DST] ? nla_get_in_addr(tb[RTA_DST]) : 0;
2558 tableid = tb[RTA_TABLE] ? nla_get_u32(tb[RTA_TABLE]) : 0;
2559
2560 mrt = ipmr_get_table(net, tableid ? tableid : RT_TABLE_DEFAULT);
Dan Carpenter2e3d2322017-07-12 10:56:47 +03002561 if (!mrt) {
2562 err = -ENOENT;
Donald Sharp4f75ba62017-06-28 13:58:57 -04002563 goto errout_free;
2564 }
2565
2566 /* entries are added/deleted only under RTNL */
2567 rcu_read_lock();
2568 cache = ipmr_cache_find(mrt, src, grp);
2569 rcu_read_unlock();
2570 if (!cache) {
2571 err = -ENOENT;
2572 goto errout_free;
2573 }
2574
2575 skb = nlmsg_new(mroute_msgsize(false, mrt->maxvif), GFP_KERNEL);
2576 if (!skb) {
2577 err = -ENOBUFS;
2578 goto errout_free;
2579 }
2580
2581 err = ipmr_fill_mroute(mrt, skb, NETLINK_CB(in_skb).portid,
2582 nlh->nlmsg_seq, cache,
2583 RTM_NEWROUTE, 0);
2584 if (err < 0)
2585 goto errout_free;
2586
2587 err = rtnl_unicast(skb, net, NETLINK_CB(in_skb).portid);
2588
2589errout:
2590 return err;
2591
2592errout_free:
2593 kfree_skb(skb);
2594 goto errout;
2595}
2596
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002597static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2598{
2599 struct net *net = sock_net(skb->sk);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002600 unsigned int t = 0, s_t;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002601 unsigned int e = 0, s_e;
Yuval Mintz494fff52018-02-28 23:29:34 +02002602 struct mr_table *mrt;
2603 struct mr_mfc *mfc;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002604
2605 s_t = cb->args[0];
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002606 s_e = cb->args[1];
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002607
Eric Dumazeta8c94862010-10-01 16:15:08 +00002608 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002609 ipmr_for_each_table(mrt, net) {
2610 if (t < s_t)
2611 goto next_table;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002612 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list) {
2613 if (e < s_e)
2614 goto next_entry;
2615 if (ipmr_fill_mroute(mrt, skb,
2616 NETLINK_CB(cb->skb).portid,
2617 cb->nlh->nlmsg_seq,
Yuval Mintz494fff52018-02-28 23:29:34 +02002618 (struct mfc_cache *)mfc,
2619 RTM_NEWROUTE, NLM_F_MULTI) < 0)
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002620 goto done;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002621next_entry:
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002622 e++;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002623 }
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002624 e = 0;
2625 s_e = 0;
2626
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002627 spin_lock_bh(&mfc_unres_lock);
2628 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2629 if (e < s_e)
2630 goto next_entry2;
2631 if (ipmr_fill_mroute(mrt, skb,
2632 NETLINK_CB(cb->skb).portid,
2633 cb->nlh->nlmsg_seq,
Yuval Mintz494fff52018-02-28 23:29:34 +02002634 (struct mfc_cache *)mfc,
2635 RTM_NEWROUTE, NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002636 spin_unlock_bh(&mfc_unres_lock);
2637 goto done;
2638 }
2639next_entry2:
2640 e++;
2641 }
2642 spin_unlock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002643 e = 0;
2644 s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002645next_table:
2646 t++;
2647 }
2648done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002649 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002650
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01002651 cb->args[1] = e;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002652 cb->args[0] = t;
2653
2654 return skb->len;
2655}
2656
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002657static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2658 [RTA_SRC] = { .type = NLA_U32 },
2659 [RTA_DST] = { .type = NLA_U32 },
2660 [RTA_IIF] = { .type = NLA_U32 },
2661 [RTA_TABLE] = { .type = NLA_U32 },
2662 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2663};
2664
2665static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2666{
2667 switch (rtm_protocol) {
2668 case RTPROT_STATIC:
2669 case RTPROT_MROUTED:
2670 return true;
2671 }
2672 return false;
2673}
2674
2675static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2676{
2677 struct rtnexthop *rtnh = nla_data(nla);
2678 int remaining = nla_len(nla), vifi = 0;
2679
2680 while (rtnh_ok(rtnh, remaining)) {
2681 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2682 if (++vifi == MAXVIFS)
2683 break;
2684 rtnh = rtnh_next(rtnh, &remaining);
2685 }
2686
2687 return remaining > 0 ? -EINVAL : vifi;
2688}
2689
2690/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2691static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2692 struct mfcctl *mfcc, int *mrtsock,
David Ahernc21ef3e2017-04-16 09:48:24 -07002693 struct mr_table **mrtret,
2694 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002695{
2696 struct net_device *dev = NULL;
2697 u32 tblid = RT_TABLE_DEFAULT;
2698 struct mr_table *mrt;
2699 struct nlattr *attr;
2700 struct rtmsg *rtm;
2701 int ret, rem;
2702
Johannes Bergfceb6432017-04-12 14:34:07 +02002703 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy,
David Ahernc21ef3e2017-04-16 09:48:24 -07002704 extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002705 if (ret < 0)
2706 goto out;
2707 rtm = nlmsg_data(nlh);
2708
2709 ret = -EINVAL;
2710 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2711 rtm->rtm_type != RTN_MULTICAST ||
2712 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2713 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2714 goto out;
2715
2716 memset(mfcc, 0, sizeof(*mfcc));
2717 mfcc->mfcc_parent = -1;
2718 ret = 0;
2719 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2720 switch (nla_type(attr)) {
2721 case RTA_SRC:
2722 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2723 break;
2724 case RTA_DST:
2725 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2726 break;
2727 case RTA_IIF:
2728 dev = __dev_get_by_index(net, nla_get_u32(attr));
2729 if (!dev) {
2730 ret = -ENODEV;
2731 goto out;
2732 }
2733 break;
2734 case RTA_MULTIPATH:
2735 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2736 ret = -EINVAL;
2737 goto out;
2738 }
2739 break;
2740 case RTA_PREFSRC:
2741 ret = 1;
2742 break;
2743 case RTA_TABLE:
2744 tblid = nla_get_u32(attr);
2745 break;
2746 }
2747 }
2748 mrt = ipmr_get_table(net, tblid);
2749 if (!mrt) {
2750 ret = -ENOENT;
2751 goto out;
2752 }
2753 *mrtret = mrt;
2754 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2755 if (dev)
2756 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2757
2758out:
2759 return ret;
2760}
2761
2762/* takes care of both newroute and delroute */
David Ahernc21ef3e2017-04-16 09:48:24 -07002763static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh,
2764 struct netlink_ext_ack *extack)
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002765{
2766 struct net *net = sock_net(skb->sk);
2767 int ret, mrtsock, parent;
2768 struct mr_table *tbl;
2769 struct mfcctl mfcc;
2770
2771 mrtsock = 0;
2772 tbl = NULL;
David Ahernc21ef3e2017-04-16 09:48:24 -07002773 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl, extack);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002774 if (ret < 0)
2775 return ret;
2776
2777 parent = ret ? mfcc.mfcc_parent : -1;
2778 if (nlh->nlmsg_type == RTM_NEWROUTE)
2779 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2780 else
2781 return ipmr_mfc_delete(tbl, &mfcc, parent);
2782}
2783
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03002784static bool ipmr_fill_table(struct mr_table *mrt, struct sk_buff *skb)
2785{
2786 u32 queue_len = atomic_read(&mrt->cache_resolve_queue_len);
2787
2788 if (nla_put_u32(skb, IPMRA_TABLE_ID, mrt->id) ||
2789 nla_put_u32(skb, IPMRA_TABLE_CACHE_RES_QUEUE_LEN, queue_len) ||
2790 nla_put_s32(skb, IPMRA_TABLE_MROUTE_REG_VIF_NUM,
2791 mrt->mroute_reg_vif_num) ||
2792 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_ASSERT,
2793 mrt->mroute_do_assert) ||
2794 nla_put_u8(skb, IPMRA_TABLE_MROUTE_DO_PIM, mrt->mroute_do_pim))
2795 return false;
2796
2797 return true;
2798}
2799
2800static bool ipmr_fill_vif(struct mr_table *mrt, u32 vifid, struct sk_buff *skb)
2801{
2802 struct nlattr *vif_nest;
2803 struct vif_device *vif;
2804
2805 /* if the VIF doesn't exist just continue */
2806 if (!VIF_EXISTS(mrt, vifid))
2807 return true;
2808
2809 vif = &mrt->vif_table[vifid];
2810 vif_nest = nla_nest_start(skb, IPMRA_VIF);
2811 if (!vif_nest)
2812 return false;
2813 if (nla_put_u32(skb, IPMRA_VIFA_IFINDEX, vif->dev->ifindex) ||
2814 nla_put_u32(skb, IPMRA_VIFA_VIF_ID, vifid) ||
2815 nla_put_u16(skb, IPMRA_VIFA_FLAGS, vif->flags) ||
2816 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_IN, vif->bytes_in,
2817 IPMRA_VIFA_PAD) ||
2818 nla_put_u64_64bit(skb, IPMRA_VIFA_BYTES_OUT, vif->bytes_out,
2819 IPMRA_VIFA_PAD) ||
2820 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_IN, vif->pkt_in,
2821 IPMRA_VIFA_PAD) ||
2822 nla_put_u64_64bit(skb, IPMRA_VIFA_PACKETS_OUT, vif->pkt_out,
2823 IPMRA_VIFA_PAD) ||
2824 nla_put_be32(skb, IPMRA_VIFA_LOCAL_ADDR, vif->local) ||
2825 nla_put_be32(skb, IPMRA_VIFA_REMOTE_ADDR, vif->remote)) {
2826 nla_nest_cancel(skb, vif_nest);
2827 return false;
2828 }
2829 nla_nest_end(skb, vif_nest);
2830
2831 return true;
2832}
2833
2834static int ipmr_rtm_dumplink(struct sk_buff *skb, struct netlink_callback *cb)
2835{
2836 struct net *net = sock_net(skb->sk);
2837 struct nlmsghdr *nlh = NULL;
2838 unsigned int t = 0, s_t;
2839 unsigned int e = 0, s_e;
2840 struct mr_table *mrt;
2841
2842 s_t = cb->args[0];
2843 s_e = cb->args[1];
2844
2845 ipmr_for_each_table(mrt, net) {
2846 struct nlattr *vifs, *af;
2847 struct ifinfomsg *hdr;
2848 u32 i;
2849
2850 if (t < s_t)
2851 goto skip_table;
2852 nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
2853 cb->nlh->nlmsg_seq, RTM_NEWLINK,
2854 sizeof(*hdr), NLM_F_MULTI);
2855 if (!nlh)
2856 break;
2857
2858 hdr = nlmsg_data(nlh);
2859 memset(hdr, 0, sizeof(*hdr));
2860 hdr->ifi_family = RTNL_FAMILY_IPMR;
2861
2862 af = nla_nest_start(skb, IFLA_AF_SPEC);
2863 if (!af) {
2864 nlmsg_cancel(skb, nlh);
2865 goto out;
2866 }
2867
2868 if (!ipmr_fill_table(mrt, skb)) {
2869 nlmsg_cancel(skb, nlh);
2870 goto out;
2871 }
2872
2873 vifs = nla_nest_start(skb, IPMRA_TABLE_VIFS);
2874 if (!vifs) {
2875 nla_nest_end(skb, af);
2876 nlmsg_end(skb, nlh);
2877 goto out;
2878 }
2879 for (i = 0; i < mrt->maxvif; i++) {
2880 if (e < s_e)
2881 goto skip_entry;
2882 if (!ipmr_fill_vif(mrt, i, skb)) {
2883 nla_nest_end(skb, vifs);
2884 nla_nest_end(skb, af);
2885 nlmsg_end(skb, nlh);
2886 goto out;
2887 }
2888skip_entry:
2889 e++;
2890 }
2891 s_e = 0;
2892 e = 0;
2893 nla_nest_end(skb, vifs);
2894 nla_nest_end(skb, af);
2895 nlmsg_end(skb, nlh);
2896skip_table:
2897 t++;
2898 }
2899
2900out:
2901 cb->args[1] = e;
2902 cb->args[0] = t;
2903
2904 return skb->len;
2905}
2906
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002907#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002908/* The /proc interfaces to multicast routing :
2909 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910 */
2911struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002912 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002913 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914 int ct;
2915};
2916
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002917static struct vif_device *ipmr_vif_seq_idx(struct net *net,
Yuval Mintz6853f212018-02-28 23:29:29 +02002918 struct ipmr_vif_iter *iter,
2919 loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002920{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002921 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002922
2923 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2924 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002926 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002927 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002928 }
2929 return NULL;
2930}
2931
2932static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002933 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002935 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002936 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002937 struct mr_table *mrt;
2938
2939 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002940 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002941 return ERR_PTR(-ENOENT);
2942
2943 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002944
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002946 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 : SEQ_START_TOKEN;
2948}
2949
2950static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2951{
2952 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002953 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002954 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955
2956 ++*pos;
2957 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002958 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002959
Patrick McHardy0c122952010-04-13 05:03:22 +00002960 while (++iter->ct < mrt->maxvif) {
2961 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002963 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 }
2965 return NULL;
2966}
2967
2968static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002969 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970{
2971 read_unlock(&mrt_lock);
2972}
2973
2974static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2975{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002976 struct ipmr_vif_iter *iter = seq->private;
2977 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002978
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002980 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002981 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2982 } else {
2983 const struct vif_device *vif = v;
Yuval Mintz6853f212018-02-28 23:29:29 +02002984 const char *name = vif->dev ?
2985 vif->dev->name : "none";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 seq_printf(seq,
James Hogan91e6dd82018-01-30 09:48:02 +00002988 "%2td %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002989 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002990 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002991 vif->bytes_out, vif->pkt_out,
2992 vif->flags, vif->local, vif->remote);
2993 }
2994 return 0;
2995}
2996
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002997static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002998 .start = ipmr_vif_seq_start,
2999 .next = ipmr_vif_seq_next,
3000 .stop = ipmr_vif_seq_stop,
3001 .show = ipmr_vif_seq_show,
3002};
3003
3004static int ipmr_vif_open(struct inode *inode, struct file *file)
3005{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003006 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
3007 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003008}
3009
Arjan van de Ven9a321442007-02-12 00:55:35 -08003010static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 .open = ipmr_vif_open,
3012 .read = seq_read,
3013 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003014 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015};
3016
3017struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003018 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003019 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00003020 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003021};
3022
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003023static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
3024 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003025{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003026 struct mr_table *mrt = it->mrt;
Yuval Mintz494fff52018-02-28 23:29:34 +02003027 struct mr_mfc *mfc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028
Eric Dumazeta8c94862010-10-01 16:15:08 +00003029 rcu_read_lock();
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003030 it->cache = &mrt->mfc_cache_list;
3031 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
3032 if (pos-- == 0)
Yuval Mintz494fff52018-02-28 23:29:34 +02003033 return (struct mfc_cache *)mfc;
Eric Dumazeta8c94862010-10-01 16:15:08 +00003034 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003035
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00003037 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00003038 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00003039 if (pos-- == 0)
Yuval Mintz494fff52018-02-28 23:29:34 +02003040 return (struct mfc_cache *)mfc;
3041
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042 spin_unlock_bh(&mfc_unres_lock);
3043
3044 it->cache = NULL;
3045 return NULL;
3046}
3047
3048
3049static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
3050{
3051 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003052 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003053 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003054
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003055 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01003056 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003057 return ERR_PTR(-ENOENT);
3058
3059 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003060 it->cache = NULL;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003061 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 : SEQ_START_TOKEN;
3063}
3064
3065static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
3066{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003068 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003069 struct mr_table *mrt = it->mrt;
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003070 struct mfc_cache *mfc = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003071
3072 ++*pos;
3073
3074 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003075 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076
Yuval Mintz494fff52018-02-28 23:29:34 +02003077 if (mfc->_c.list.next != it->cache)
3078 return (struct mfc_cache *)(list_entry(mfc->_c.list.next,
3079 struct mr_mfc, list));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003080
Patrick McHardy0c122952010-04-13 05:03:22 +00003081 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082 goto end_of_list;
3083
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00003085 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00003086 it->cache = &mrt->mfc_unres_queue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003087
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00003089 if (!list_empty(it->cache))
Yuval Mintz494fff52018-02-28 23:29:34 +02003090 return (struct mfc_cache *)(list_first_entry(it->cache,
3091 struct mr_mfc,
3092 list));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003093
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00003094end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 spin_unlock_bh(&mfc_unres_lock);
3096 it->cache = NULL;
3097
3098 return NULL;
3099}
3100
3101static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
3102{
3103 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003104 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
Patrick McHardy0c122952010-04-13 05:03:22 +00003106 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003107 spin_unlock_bh(&mfc_unres_lock);
Nikolay Aleksandrov8fb472c2017-01-12 15:53:33 +01003108 else if (it->cache == &mrt->mfc_cache_list)
Eric Dumazeta8c94862010-10-01 16:15:08 +00003109 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110}
3111
3112static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
3113{
3114 int n;
3115
3116 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003117 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003118 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
3119 } else {
3120 const struct mfc_cache *mfc = v;
3121 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003122 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003123
Eric Dumazet0eae88f2010-04-20 19:06:52 -07003124 seq_printf(seq, "%08X %08X %-3hd",
3125 (__force u32) mfc->mfc_mcastgrp,
3126 (__force u32) mfc->mfc_origin,
Yuval Mintz494fff52018-02-28 23:29:34 +02003127 mfc->_c.mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
Patrick McHardy0c122952010-04-13 05:03:22 +00003129 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003130 seq_printf(seq, " %8lu %8lu %8lu",
Yuval Mintz494fff52018-02-28 23:29:34 +02003131 mfc->_c.mfc_un.res.pkt,
3132 mfc->_c.mfc_un.res.bytes,
3133 mfc->_c.mfc_un.res.wrong_if);
3134 for (n = mfc->_c.mfc_un.res.minvif;
3135 n < mfc->_c.mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00003136 if (VIF_EXISTS(mrt, n) &&
Yuval Mintz494fff52018-02-28 23:29:34 +02003137 mfc->_c.mfc_un.res.ttls[n] < 255)
Benjamin Therycf958ae32009-01-22 04:56:16 +00003138 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003139 " %2d:%-3d",
Yuval Mintz494fff52018-02-28 23:29:34 +02003140 n, mfc->_c.mfc_un.res.ttls[n]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08003142 } else {
3143 /* unresolved mfc_caches don't contain
3144 * pkt, bytes and wrong_if values
3145 */
3146 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003147 }
3148 seq_putc(seq, '\n');
3149 }
3150 return 0;
3151}
3152
Stephen Hemmingerf6908082007-03-12 14:34:29 -07003153static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 .start = ipmr_mfc_seq_start,
3155 .next = ipmr_mfc_seq_next,
3156 .stop = ipmr_mfc_seq_stop,
3157 .show = ipmr_mfc_seq_show,
3158};
3159
3160static int ipmr_mfc_open(struct inode *inode, struct file *file)
3161{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003162 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
3163 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003164}
3165
Arjan van de Ven9a321442007-02-12 00:55:35 -08003166static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167 .open = ipmr_mfc_open,
3168 .read = seq_read,
3169 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003170 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003172#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173
3174#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00003175static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07003177 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178};
3179#endif
3180
Yotam Gigi4d65b942017-09-27 08:23:13 +02003181static unsigned int ipmr_seq_read(struct net *net)
3182{
3183 ASSERT_RTNL();
3184
3185 return net->ipv4.ipmr_seq + ipmr_rules_seq_read(net);
3186}
3187
3188static int ipmr_dump(struct net *net, struct notifier_block *nb)
3189{
3190 struct mr_table *mrt;
3191 int err;
3192
3193 err = ipmr_rules_dump(net, nb);
3194 if (err)
3195 return err;
3196
3197 ipmr_for_each_table(mrt, net) {
3198 struct vif_device *v = &mrt->vif_table[0];
Yuval Mintz494fff52018-02-28 23:29:34 +02003199 struct mr_mfc *mfc;
Yotam Gigi4d65b942017-09-27 08:23:13 +02003200 int vifi;
3201
3202 /* Notifiy on table VIF entries */
3203 read_lock(&mrt_lock);
3204 for (vifi = 0; vifi < mrt->maxvif; vifi++, v++) {
3205 if (!v->dev)
3206 continue;
3207
3208 call_ipmr_vif_entry_notifier(nb, net, FIB_EVENT_VIF_ADD,
3209 v, vifi, mrt->id);
3210 }
3211 read_unlock(&mrt_lock);
3212
3213 /* Notify on table MFC entries */
3214 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_list, list)
3215 call_ipmr_mfc_entry_notifier(nb, net,
Yuval Mintz494fff52018-02-28 23:29:34 +02003216 FIB_EVENT_ENTRY_ADD,
3217 (struct mfc_cache *)mfc,
Yotam Gigi4d65b942017-09-27 08:23:13 +02003218 mrt->id);
3219 }
3220
3221 return 0;
3222}
3223
3224static const struct fib_notifier_ops ipmr_notifier_ops_template = {
3225 .family = RTNL_FAMILY_IPMR,
3226 .fib_seq_read = ipmr_seq_read,
3227 .fib_dump = ipmr_dump,
3228 .owner = THIS_MODULE,
3229};
3230
Colin Ian Kingef739d82017-09-29 14:34:22 +01003231static int __net_init ipmr_notifier_init(struct net *net)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003232{
3233 struct fib_notifier_ops *ops;
3234
3235 net->ipv4.ipmr_seq = 0;
3236
3237 ops = fib_notifier_ops_register(&ipmr_notifier_ops_template, net);
3238 if (IS_ERR(ops))
3239 return PTR_ERR(ops);
3240 net->ipv4.ipmr_notifier_ops = ops;
3241
3242 return 0;
3243}
3244
3245static void __net_exit ipmr_notifier_exit(struct net *net)
3246{
3247 fib_notifier_ops_unregister(net->ipv4.ipmr_notifier_ops);
3248 net->ipv4.ipmr_notifier_ops = NULL;
3249}
3250
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01003251/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00003252static int __net_init ipmr_net_init(struct net *net)
3253{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003254 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00003255
Yotam Gigi4d65b942017-09-27 08:23:13 +02003256 err = ipmr_notifier_init(net);
3257 if (err)
3258 goto ipmr_notifier_fail;
3259
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003260 err = ipmr_rules_init(net);
3261 if (err < 0)
Yotam Gigi4d65b942017-09-27 08:23:13 +02003262 goto ipmr_rules_fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003263
3264#ifdef CONFIG_PROC_FS
3265 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00003266 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003267 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00003268 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003269 goto proc_cache_fail;
3270#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00003271 return 0;
3272
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003273#ifdef CONFIG_PROC_FS
3274proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00003275 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003276proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003277 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003278#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003279ipmr_rules_fail:
3280 ipmr_notifier_exit(net);
3281ipmr_notifier_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003282 return err;
3283}
3284
3285static void __net_exit ipmr_net_exit(struct net *net)
3286{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003287#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00003288 remove_proc_entry("ip_mr_cache", net->proc_net);
3289 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003290#endif
Yotam Gigi4d65b942017-09-27 08:23:13 +02003291 ipmr_notifier_exit(net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00003292 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00003293}
3294
3295static struct pernet_operations ipmr_net_ops = {
3296 .init = ipmr_net_init,
3297 .exit = ipmr_net_exit,
Kirill Tkhaif84c6822018-02-13 12:29:52 +03003298 .async = true,
Benjamin Therycf958ae32009-01-22 04:56:16 +00003299};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09003300
Wang Chen03d2f892008-07-03 12:13:36 +08003301int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302{
Wang Chen03d2f892008-07-03 12:13:36 +08003303 int err;
3304
Linus Torvalds1da177e2005-04-16 15:20:36 -07003305 mrt_cachep = kmem_cache_create("ip_mrt_cache",
3306 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00003307 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09003308 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08003309
Benjamin Therycf958ae32009-01-22 04:56:16 +00003310 err = register_pernet_subsys(&ipmr_net_ops);
3311 if (err)
3312 goto reg_pernet_fail;
3313
Wang Chen03d2f892008-07-03 12:13:36 +08003314 err = register_netdevice_notifier(&ip_mr_notifier);
3315 if (err)
3316 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07003317#ifdef CONFIG_IP_PIMSM_V2
3318 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00003319 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07003320 err = -EAGAIN;
3321 goto add_proto_fail;
3322 }
3323#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00003324 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003325 ipmr_rtm_getroute, ipmr_rtm_dumproute, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003326 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003327 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01003328 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
Florian Westphalb97bac62017-08-09 20:41:48 +02003329 ipmr_rtm_route, NULL, 0);
Nikolay Aleksandrov772c3442017-06-07 18:02:32 +03003330
3331 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETLINK,
Florian Westphalb97bac62017-08-09 20:41:48 +02003332 NULL, ipmr_rtm_dumplink, 0);
Wang Chen03d2f892008-07-03 12:13:36 +08003333 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00003334
Tom Goff403dbb92009-06-14 03:16:13 -07003335#ifdef CONFIG_IP_PIMSM_V2
3336add_proto_fail:
3337 unregister_netdevice_notifier(&ip_mr_notifier);
3338#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08003339reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00003340 unregister_pernet_subsys(&ipmr_net_ops);
3341reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08003342 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08003343 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344}