blob: 354926e61f06788f2b835f2a9794cc2c3ae1a2f2 [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 Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
30#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080031#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/timer.h>
34#include <linux/mm.h>
35#include <linux/kernel.h>
36#include <linux/fcntl.h>
37#include <linux/stat.h>
38#include <linux/socket.h>
39#include <linux/in.h>
40#include <linux/inet.h>
41#include <linux/netdevice.h>
42#include <linux/inetdevice.h>
43#include <linux/igmp.h>
44#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
46#include <linux/mroute.h>
47#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080048#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020050#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/ip.h>
52#include <net/protocol.h>
53#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020054#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/icmp.h>
57#include <net/udp.h>
58#include <net/raw.h>
59#include <linux/notifier.h>
60#include <linux/if_arp.h>
61#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000062#include <linux/compat.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040063#include <linux/export.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000064#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070066#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000067#include <net/fib_rules.h>
Nicolas Dichteld67b8c62012-12-04 01:13:35 +000068#include <linux/netconf.h>
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +010069#include <net/nexthop.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Gustavo A. R. Silva35775cc2018-12-10 12:41:24 -060071#include <linux/nospec.h>
72
Patrick McHardyf0ad0862010-04-13 05:03:23 +000073struct ipmr_rule {
74 struct fib_rule common;
75};
76
77struct ipmr_result {
78 struct mr_table *mrt;
79};
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000082 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 */
84
85static DEFINE_RWLOCK(mrt_lock);
86
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +010087/* Multicast router control variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/* Special spinlock for queue of unresolved entries */
90static DEFINE_SPINLOCK(mfc_unres_lock);
91
92/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000093 * entries is changed only in process context and protected
94 * with weak lock mrt_lock. Queue of unresolved entries is protected
95 * with strong spinlock mfc_unres_lock.
96 *
97 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 */
99
Christoph Lametere18b8902006-12-06 20:33:20 -0800100static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000102static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000103static void ipmr_free_table(struct mr_table *mrt);
104
Rami Rosenc4854ec2013-07-20 15:09:28 +0300105static void ip_mr_forward(struct net *net, struct mr_table *mrt,
106 struct sk_buff *skb, struct mfc_cache *cache,
107 int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000108static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000109 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200110static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
111 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000112static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
113 int cmd);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100114static void mroute_clean_tables(struct mr_table *mrt, bool all);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000115static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000117#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
118#define ipmr_for_each_table(mrt, net) \
119 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
120
121static struct mr_table *ipmr_get_table(struct net *net, u32 id)
122{
123 struct mr_table *mrt;
124
125 ipmr_for_each_table(mrt, net) {
126 if (mrt->id == id)
127 return mrt;
128 }
129 return NULL;
130}
131
David S. Millerda919812011-03-12 02:04:50 -0500132static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000133 struct mr_table **mrt)
134{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000135 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100136 struct ipmr_result res;
137 struct fib_lookup_arg arg = {
138 .result = &res,
139 .flags = FIB_LOOKUP_NOREF,
140 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000141
David S. Millerda919812011-03-12 02:04:50 -0500142 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
143 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000144 if (err < 0)
145 return err;
146 *mrt = res.mrt;
147 return 0;
148}
149
150static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
151 int flags, struct fib_lookup_arg *arg)
152{
153 struct ipmr_result *res = arg->result;
154 struct mr_table *mrt;
155
156 switch (rule->action) {
157 case FR_ACT_TO_TBL:
158 break;
159 case FR_ACT_UNREACHABLE:
160 return -ENETUNREACH;
161 case FR_ACT_PROHIBIT:
162 return -EACCES;
163 case FR_ACT_BLACKHOLE:
164 default:
165 return -EINVAL;
166 }
167
168 mrt = ipmr_get_table(rule->fr_net, rule->table);
Ian Morris51456b22015-04-03 09:17:26 +0100169 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000170 return -EAGAIN;
171 res->mrt = mrt;
172 return 0;
173}
174
175static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
176{
177 return 1;
178}
179
180static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
181 FRA_GENERIC_POLICY,
182};
183
184static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
185 struct fib_rule_hdr *frh, struct nlattr **tb)
186{
187 return 0;
188}
189
190static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
191 struct nlattr **tb)
192{
193 return 1;
194}
195
196static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
197 struct fib_rule_hdr *frh)
198{
199 frh->dst_len = 0;
200 frh->src_len = 0;
201 frh->tos = 0;
202 return 0;
203}
204
Andi Kleen04a6f822012-10-04 17:12:11 -0700205static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200206 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000207 .rule_size = sizeof(struct ipmr_rule),
208 .addr_size = sizeof(u32),
209 .action = ipmr_rule_action,
210 .match = ipmr_rule_match,
211 .configure = ipmr_rule_configure,
212 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000213 .fill = ipmr_rule_fill,
214 .nlgroup = RTNLGRP_IPV4_RULE,
215 .policy = ipmr_rule_policy,
216 .owner = THIS_MODULE,
217};
218
219static int __net_init ipmr_rules_init(struct net *net)
220{
221 struct fib_rules_ops *ops;
222 struct mr_table *mrt;
223 int err;
224
225 ops = fib_rules_register(&ipmr_rules_ops_template, net);
226 if (IS_ERR(ops))
227 return PTR_ERR(ops);
228
229 INIT_LIST_HEAD(&net->ipv4.mr_tables);
230
231 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100232 if (IS_ERR(mrt)) {
233 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000234 goto err1;
235 }
236
237 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
238 if (err < 0)
239 goto err2;
240
241 net->ipv4.mr_rules_ops = ops;
242 return 0;
243
244err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700245 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000246err1:
247 fib_rules_unregister(ops);
248 return err;
249}
250
251static void __net_exit ipmr_rules_exit(struct net *net)
252{
253 struct mr_table *mrt, *next;
254
WANG Conged785302015-03-31 11:01:45 -0700255 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000256 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
257 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000258 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000259 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000260 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700261 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000262}
263#else
264#define ipmr_for_each_table(mrt, net) \
265 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
266
267static struct mr_table *ipmr_get_table(struct net *net, u32 id)
268{
269 return net->ipv4.mrt;
270}
271
David S. Millerda919812011-03-12 02:04:50 -0500272static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000273 struct mr_table **mrt)
274{
275 *mrt = net->ipv4.mrt;
276 return 0;
277}
278
279static int __net_init ipmr_rules_init(struct net *net)
280{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100281 struct mr_table *mrt;
282
283 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
284 if (IS_ERR(mrt))
285 return PTR_ERR(mrt);
286 net->ipv4.mrt = mrt;
287 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000288}
289
290static void __net_exit ipmr_rules_exit(struct net *net)
291{
WANG Conged785302015-03-31 11:01:45 -0700292 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000293 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700294 net->ipv4.mrt = NULL;
295 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000296}
297#endif
298
299static struct mr_table *ipmr_new_table(struct net *net, u32 id)
300{
301 struct mr_table *mrt;
302 unsigned int i;
303
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100304 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
305 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
306 return ERR_PTR(-EINVAL);
307
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000308 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100309 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000310 return mrt;
311
312 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100313 if (!mrt)
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100314 return ERR_PTR(-ENOMEM);
Patrick McHardy8de53df2010-04-15 13:29:28 +0200315 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000316 mrt->id = id;
317
318 /* Forwarding cache */
319 for (i = 0; i < MFC_LINES; i++)
320 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
321
322 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
323
324 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
325 (unsigned long)mrt);
326
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000327 mrt->mroute_reg_vif_num = -1;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000328#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
329 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
330#endif
331 return mrt;
332}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000334static void ipmr_free_table(struct mr_table *mrt)
335{
336 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100337 mroute_clean_tables(mrt, true);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000338 kfree(mrt);
339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
342
Wang Chend6070322008-07-14 20:55:26 -0700343static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
344{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000345 struct net *net = dev_net(dev);
346
Wang Chend6070322008-07-14 20:55:26 -0700347 dev_close(dev);
348
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000349 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700350 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800351 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700352 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700353 struct ip_tunnel_parm p;
354
355 memset(&p, 0, sizeof(p));
356 p.iph.daddr = v->vifc_rmt_addr.s_addr;
357 p.iph.saddr = v->vifc_lcl_addr.s_addr;
358 p.iph.version = 4;
359 p.iph.ihl = 5;
360 p.iph.protocol = IPPROTO_IPIP;
361 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
362 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
363
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800364 if (ops->ndo_do_ioctl) {
365 mm_segment_t oldfs = get_fs();
366
367 set_fs(KERNEL_DS);
368 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
369 set_fs(oldfs);
370 }
Wang Chend6070322008-07-14 20:55:26 -0700371 }
372}
373
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100374/* Initialize ipmr pimreg/tunnel in_device */
375static bool ipmr_init_vif_indev(const struct net_device *dev)
376{
377 struct in_device *in_dev;
378
379 ASSERT_RTNL();
380
381 in_dev = __in_dev_get_rtnl(dev);
382 if (!in_dev)
383 return false;
384 ipv4_devconf_setall(in_dev);
385 neigh_parms_data_state_setall(in_dev->arp_parms);
386 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
387
388 return true;
389}
390
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100391static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct net_device *dev;
394
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000395 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800398 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 int err;
400 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 memset(&p, 0, sizeof(p));
404 p.iph.daddr = v->vifc_rmt_addr.s_addr;
405 p.iph.saddr = v->vifc_lcl_addr.s_addr;
406 p.iph.version = 4;
407 p.iph.ihl = 5;
408 p.iph.protocol = IPPROTO_IPIP;
409 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800410 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800412 if (ops->ndo_do_ioctl) {
413 mm_segment_t oldfs = get_fs();
414
415 set_fs(KERNEL_DS);
416 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
417 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000418 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800419 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000420 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 dev = NULL;
422
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000423 if (err == 0 &&
424 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100426 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (dev_open(dev))
429 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700430 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432 }
433 return dev;
434
435failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 unregister_netdevice(dev);
437 return NULL;
438}
439
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100440#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000441static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000443 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000444 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500445 struct flowi4 fl4 = {
446 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700447 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500448 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000449 };
450 int err;
451
David S. Millerda919812011-03-12 02:04:50 -0500452 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000453 if (err < 0) {
454 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000455 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000456 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000457
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700459 dev->stats.tx_bytes += skb->len;
460 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000461 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 read_unlock(&mrt_lock);
463 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000464 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465}
466
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200467static int reg_vif_get_iflink(const struct net_device *dev)
468{
469 return 0;
470}
471
Stephen Hemminger007c3832008-11-20 20:28:35 -0800472static const struct net_device_ops reg_vif_netdev_ops = {
473 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200474 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800475};
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477static void reg_vif_setup(struct net_device *dev)
478{
479 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800480 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530482 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700484 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485}
486
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000487static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000490 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000492 if (mrt->id == RT_TABLE_DEFAULT)
493 sprintf(name, "pimreg");
494 else
495 sprintf(name, "pimreg%u", mrt->id);
496
Tom Gundersenc835a672014-07-14 16:37:24 +0200497 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Ian Morris51456b22015-04-03 09:17:26 +0100499 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return NULL;
501
Tom Goff403dbb92009-06-14 03:16:13 -0700502 dev_net_set(dev, net);
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (register_netdevice(dev)) {
505 free_netdev(dev);
506 return NULL;
507 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100509 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 if (dev_open(dev))
512 goto failure;
513
Wang Chen7dc00c82008-07-14 20:56:34 -0700514 dev_hold(dev);
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return dev;
517
518failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 unregister_netdevice(dev);
520 return NULL;
521}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100522
523/* called with rcu_read_lock() */
524static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
525 unsigned int pimlen)
526{
527 struct net_device *reg_dev = NULL;
528 struct iphdr *encap;
529
530 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100531 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100532 * a. packet is really sent to a multicast group
533 * b. packet is not a NULL-REGISTER
534 * c. packet is not truncated
535 */
536 if (!ipv4_is_multicast(encap->daddr) ||
537 encap->tot_len == 0 ||
538 ntohs(encap->tot_len) + pimlen > skb->len)
539 return 1;
540
541 read_lock(&mrt_lock);
542 if (mrt->mroute_reg_vif_num >= 0)
543 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
544 read_unlock(&mrt_lock);
545
546 if (!reg_dev)
547 return 1;
548
549 skb->mac_header = skb->network_header;
550 skb_pull(skb, (u8 *)encap - skb->data);
551 skb_reset_network_header(skb);
552 skb->protocol = htons(ETH_P_IP);
553 skb->ip_summed = CHECKSUM_NONE;
554
555 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
556
557 netif_rx(skb);
558
559 return NET_RX_SUCCESS;
560}
561#else
562static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
563{
564 return NULL;
565}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566#endif
567
Ben Hutchings2c530402012-07-10 10:55:09 +0000568/**
569 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700570 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000572static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000573 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
575 struct vif_device *v;
576 struct net_device *dev;
577 struct in_device *in_dev;
578
Patrick McHardy0c122952010-04-13 05:03:22 +0000579 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 return -EADDRNOTAVAIL;
581
Patrick McHardy0c122952010-04-13 05:03:22 +0000582 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
584 write_lock_bh(&mrt_lock);
585 dev = v->dev;
586 v->dev = NULL;
587
588 if (!dev) {
589 write_unlock_bh(&mrt_lock);
590 return -EADDRNOTAVAIL;
591 }
592
Patrick McHardy0c122952010-04-13 05:03:22 +0000593 if (vifi == mrt->mroute_reg_vif_num)
594 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000596 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000598
599 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000600 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 break;
602 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000603 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 }
605
606 write_unlock_bh(&mrt_lock);
607
608 dev_set_allmulti(dev, -1);
609
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000610 in_dev = __in_dev_get_rtnl(dev);
611 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700612 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000613 inet_netconf_notify_devconf(dev_net(dev),
614 NETCONFA_MC_FORWARDING,
615 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 ip_rt_multicast_event(in_dev);
617 }
618
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000619 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000620 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
622 dev_put(dev);
623 return 0;
624}
625
Eric Dumazeta8c94862010-10-01 16:15:08 +0000626static void ipmr_cache_free_rcu(struct rcu_head *head)
627{
628 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
629
630 kmem_cache_free(mrt_cachep, c);
631}
632
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000633static inline void ipmr_cache_free(struct mfc_cache *c)
634{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000635 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000636}
637
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000639 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000641static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200643 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700645 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
Patrick McHardy0c122952010-04-13 05:03:22 +0000647 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Jianjun Kongc354e122008-11-03 00:28:02 -0800649 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700650 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
652 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000653 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000655 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700656 e->error = -ETIMEDOUT;
657 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700658
Eric W. Biederman15e47302012-09-07 20:12:54 +0000659 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000660 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
664
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000665 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666}
667
Patrick McHardye258beb2010-04-13 05:03:19 +0000668/* Timer process for the unresolved queue. */
Patrick McHardye258beb2010-04-13 05:03:19 +0000669static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670{
Patrick McHardy0c122952010-04-13 05:03:22 +0000671 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 unsigned long now;
673 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000674 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000677 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 return;
679 }
680
Patrick McHardy0c122952010-04-13 05:03:22 +0000681 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 goto out;
683
684 now = jiffies;
685 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Patrick McHardy0c122952010-04-13 05:03:22 +0000687 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 if (time_after(c->mfc_un.unres.expires, now)) {
689 unsigned long interval = c->mfc_un.unres.expires - now;
690 if (interval < expires)
691 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 continue;
693 }
694
Patrick McHardy862465f2010-04-13 05:03:21 +0000695 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000696 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000697 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 }
699
Patrick McHardy0c122952010-04-13 05:03:22 +0000700 if (!list_empty(&mrt->mfc_unres_queue))
701 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702
703out:
704 spin_unlock(&mfc_unres_lock);
705}
706
707/* Fill oifs list. It is called under write locked mrt_lock. */
Patrick McHardy0c122952010-04-13 05:03:22 +0000708static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000709 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
711 int vifi;
712
713 cache->mfc_un.res.minvif = MAXVIFS;
714 cache->mfc_un.res.maxvif = 0;
715 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
716
Patrick McHardy0c122952010-04-13 05:03:22 +0000717 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
718 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000719 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
721 if (cache->mfc_un.res.minvif > vifi)
722 cache->mfc_un.res.minvif = vifi;
723 if (cache->mfc_un.res.maxvif <= vifi)
724 cache->mfc_un.res.maxvif = vifi + 1;
725 }
726 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200727 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728}
729
Patrick McHardy0c122952010-04-13 05:03:22 +0000730static int vif_add(struct net *net, struct mr_table *mrt,
731 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000734 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 struct net_device *dev;
736 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700737 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738
739 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000740 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 return -EADDRINUSE;
742
743 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100745 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100746 return -EINVAL;
747 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 * All the packets will be sent to the daemon
749 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000750 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000752 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 if (!dev)
754 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700755 err = dev_set_allmulti(dev, 1);
756 if (err) {
757 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700758 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700759 return err;
760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900762 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000763 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if (!dev)
765 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700766 err = dev_set_allmulti(dev, 1);
767 if (err) {
768 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700769 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700770 return err;
771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000773 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000775 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
776 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100777 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000778 dev_put(dev);
779 return -EADDRNOTAVAIL;
780 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000781 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000782 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 if (!dev)
785 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700786 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700787 if (err) {
788 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700789 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 break;
792 default:
793 return -EINVAL;
794 }
795
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000796 in_dev = __in_dev_get_rtnl(dev);
797 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000798 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000800 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700801 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000802 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
803 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 ip_rt_multicast_event(in_dev);
805
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000806 /* Fill in the VIF structures */
807
Jianjun Kongc354e122008-11-03 00:28:02 -0800808 v->rate_limit = vifc->vifc_rate_limit;
809 v->local = vifc->vifc_lcl_addr.s_addr;
810 v->remote = vifc->vifc_rmt_addr.s_addr;
811 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 if (!mrtsock)
813 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800814 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 v->bytes_in = 0;
816 v->bytes_out = 0;
817 v->pkt_in = 0;
818 v->pkt_out = 0;
819 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000820 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200821 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 /* And finish update writing critical data */
824 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800825 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000826 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000827 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000828 if (vifi+1 > mrt->maxvif)
829 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 write_unlock_bh(&mrt_lock);
831 return 0;
832}
833
Eric Dumazeta8c94862010-10-01 16:15:08 +0000834/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000835static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000836 __be32 origin,
837 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Jianjun Kongc354e122008-11-03 00:28:02 -0800839 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 struct mfc_cache *c;
841
Eric Dumazeta8c94862010-10-01 16:15:08 +0000842 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000843 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
844 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000846 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847}
848
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000849/* Look for a (*,*,oif) entry */
850static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
851 int vifi)
852{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100853 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000854 struct mfc_cache *c;
855
856 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100857 if (c->mfc_origin == htonl(INADDR_ANY) &&
858 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000859 c->mfc_un.res.ttls[vifi] < 255)
860 return c;
861
862 return NULL;
863}
864
865/* Look for a (*,G) entry */
866static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
867 __be32 mcastgrp, int vifi)
868{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100869 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000870 struct mfc_cache *c, *proxy;
871
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100872 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000873 goto skip;
874
875 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100876 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000877 c->mfc_mcastgrp == mcastgrp) {
878 if (c->mfc_un.res.ttls[vifi] < 255)
879 return c;
880
881 /* It's ok if the vifi is part of the static tree */
882 proxy = ipmr_cache_find_any_parent(mrt,
883 c->mfc_parent);
884 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
885 return c;
886 }
887
888skip:
889 return ipmr_cache_find_any_parent(mrt, vifi);
890}
891
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100892/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000893static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
Jianjun Kongc354e122008-11-03 00:28:02 -0800895 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000896
Tom Goff70a0dec2016-06-23 16:11:57 -0400897 if (c) {
898 c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
Eric Dumazeta8c94862010-10-01 16:15:08 +0000899 c->mfc_un.res.minvif = MAXVIFS;
Tom Goff70a0dec2016-06-23 16:11:57 -0400900 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 return c;
902}
903
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000904static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905{
Jianjun Kongc354e122008-11-03 00:28:02 -0800906 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000907
908 if (c) {
909 skb_queue_head_init(&c->mfc_un.unres.unresolved);
910 c->mfc_un.unres.expires = jiffies + 10*HZ;
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 return c;
913}
914
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100915/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +0000916static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
917 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
919 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700920 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000922 /* Play the pending entries through our router */
Jianjun Kongc354e122008-11-03 00:28:02 -0800923 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700924 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
926
Hong zhi guo573ce262013-03-27 06:47:04 +0000927 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000928 nlh->nlmsg_len = skb_tail_pointer(skb) -
929 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 } else {
931 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000932 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000934 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700935 e->error = -EMSGSIZE;
936 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 }
Thomas Graf2942e902006-08-15 00:30:25 -0700938
Eric W. Biederman15e47302012-09-07 20:12:54 +0000939 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000940 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000941 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944}
945
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100946/* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
947 * expects the following bizarre scheme.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100949 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000951static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000952 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300954 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100955 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 struct igmphdr *igmp;
957 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100958 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 int ret;
960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 if (assert == IGMPMSG_WHOLEPKT)
962 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
963 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 skb = alloc_skb(128, GFP_ATOMIC);
965
Stephen Hemminger132adf52007-03-08 20:44:43 -0800966 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return -ENOBUFS;
968
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 if (assert == IGMPMSG_WHOLEPKT) {
970 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000971 * Duplicate old header, fix ihl, length etc.
972 * And all this only to mangle msg->im_msgtype and
973 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300975 skb_push(skb, sizeof(struct iphdr));
976 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300977 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300978 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700979 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 msg->im_msgtype = IGMPMSG_WHOLEPKT;
981 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000982 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700983 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
984 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
985 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100986 } else {
987 /* Copy the IP header */
988 skb_set_network_header(skb, skb->len);
989 skb_put(skb, ihl);
990 skb_copy_to_linear_data(skb, pkt->data, ihl);
991 /* Flag to the kernel this is a route add */
992 ip_hdr(skb)->protocol = 0;
993 msg = (struct igmpmsg *)skb_network_header(skb);
994 msg->im_vif = vifi;
995 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
996 /* Add our header */
997 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
998 igmp->type = assert;
999 msg->im_msgtype = assert;
1000 igmp->code = 0;
1001 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1002 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Eric Dumazet4c968702010-10-01 16:15:01 +00001005 rcu_read_lock();
1006 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001007 if (!mroute_sk) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001008 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 kfree_skb(skb);
1010 return -EINVAL;
1011 }
1012
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001013 /* Deliver to mrouted */
Eric Dumazet4c968702010-10-01 16:15:01 +00001014 ret = sock_queue_rcv_skb(mroute_sk, skb);
1015 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001016 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001017 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 kfree_skb(skb);
1019 }
1020
1021 return ret;
1022}
1023
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001024/* Queue a packet for resolution. It gets locked cache entry! */
1025static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1026 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027{
Patrick McHardy862465f2010-04-13 05:03:21 +00001028 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 int err;
1030 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001031 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032
1033 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001034 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001035 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001036 c->mfc_origin == iph->saddr) {
1037 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001039 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040 }
1041
Patrick McHardy862465f2010-04-13 05:03:21 +00001042 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001043 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001044 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001045 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 spin_unlock_bh(&mfc_unres_lock);
1047
1048 kfree_skb(skb);
1049 return -ENOBUFS;
1050 }
1051
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001052 /* Fill in the new cache entry */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001053 c->mfc_parent = -1;
1054 c->mfc_origin = iph->saddr;
1055 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001057 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001058 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001059 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001060 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 out - Brad Parker
1062 */
1063 spin_unlock_bh(&mfc_unres_lock);
1064
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001065 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 kfree_skb(skb);
1067 return err;
1068 }
1069
Patrick McHardy0c122952010-04-13 05:03:22 +00001070 atomic_inc(&mrt->cache_resolve_queue_len);
1071 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001072 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
David S. Miller278554b2010-05-12 00:05:35 -07001074 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1075 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001078 /* See if we can append the packet */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001079 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 kfree_skb(skb);
1081 err = -ENOBUFS;
1082 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001083 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 err = 0;
1085 }
1086
1087 spin_unlock_bh(&mfc_unres_lock);
1088 return err;
1089}
1090
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001091/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001093static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094{
1095 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001096 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
Jianjun Kongc354e122008-11-03 00:28:02 -08001098 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Patrick McHardy0c122952010-04-13 05:03:22 +00001100 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001102 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1103 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001104 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001105 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001106 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 return 0;
1108 }
1109 }
1110 return -ENOENT;
1111}
1112
Patrick McHardy0c122952010-04-13 05:03:22 +00001113static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001114 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115{
Patrick McHardy862465f2010-04-13 05:03:21 +00001116 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001118 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119
Patrick McHardya50436f22010-03-17 06:04:14 +00001120 if (mfc->mfcc_parent >= MAXVIFS)
1121 return -ENFILE;
1122
Jianjun Kongc354e122008-11-03 00:28:02 -08001123 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Patrick McHardy0c122952010-04-13 05:03:22 +00001125 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001127 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1128 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001129 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 }
1133
Patrick McHardy862465f2010-04-13 05:03:21 +00001134 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 write_lock_bh(&mrt_lock);
1136 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001137 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 if (!mrtsock)
1139 c->mfc_flags |= MFC_STATIC;
1140 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001141 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 return 0;
1143 }
1144
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001145 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001146 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 return -EINVAL;
1148
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001149 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001150 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 return -ENOMEM;
1152
Jianjun Kongc354e122008-11-03 00:28:02 -08001153 c->mfc_origin = mfc->mfcc_origin.s_addr;
1154 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1155 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001156 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 if (!mrtsock)
1158 c->mfc_flags |= MFC_STATIC;
1159
Eric Dumazeta8c94862010-10-01 16:15:08 +00001160 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001162 /* Check to see if we resolved a queued list. If so we
1163 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001165 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001167 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001168 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001170 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001171 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001172 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 break;
1174 }
1175 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001176 if (list_empty(&mrt->mfc_unres_queue))
1177 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 spin_unlock_bh(&mfc_unres_lock);
1179
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001180 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001181 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001182 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001184 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 return 0;
1186}
1187
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001188/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001189static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190{
1191 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001192 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001193 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001194
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001195 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001196 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001197 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1198 continue;
1199 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001201 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001203 /* Wipe the cache */
Patrick McHardy862465f2010-04-13 05:03:21 +00001204 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001205 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001206 if (!all && (c->mfc_flags & MFC_STATIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001208 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001209 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001210 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 }
1212 }
1213
Patrick McHardy0c122952010-04-13 05:03:22 +00001214 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001216 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001217 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001218 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001219 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
1221 spin_unlock_bh(&mfc_unres_lock);
1222 }
1223}
1224
Eric Dumazet4c968702010-10-01 16:15:01 +00001225/* called from ip_ra_control(), before an RCU grace period,
1226 * we dont need to call synchronize_rcu() here
1227 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228static void mrtsock_destruct(struct sock *sk)
1229{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001230 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001231 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001234 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001235 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001236 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001237 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1238 NETCONFA_IFINDEX_ALL,
1239 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001240 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001241 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001242 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 }
1244 rtnl_unlock();
1245}
1246
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001247/* Socket options and virtual interface manipulation. The whole
1248 * virtual interface system is a complete heap, but unfortunately
1249 * that's how BSD mrouted happens to think. Maybe one day with a proper
1250 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001252
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001253int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1254 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001256 struct net *net = sock_net(sk);
1257 int val, ret = 0, parent = 0;
1258 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 struct vifctl vif;
1260 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001261 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001262
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001263 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1264 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001265 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001266 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1267 ret = -EOPNOTSUPP;
1268 goto out_unlock;
1269 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001270
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001271 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001272 if (!mrt) {
1273 ret = -ENOENT;
1274 goto out_unlock;
1275 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001276 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001277 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001278 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1279 ret = -EACCES;
1280 goto out_unlock;
1281 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 }
1283
Stephen Hemminger132adf52007-03-08 20:44:43 -08001284 switch (optname) {
1285 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001286 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001287 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001288 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001289 }
1290 if (rtnl_dereference(mrt->mroute_sk)) {
1291 ret = -EADDRINUSE;
1292 break;
1293 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001294
1295 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1296 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001297 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001298 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001299 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1300 NETCONFA_IFINDEX_ALL,
1301 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001302 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001303 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001304 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001305 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1306 ret = -EACCES;
1307 } else {
1308 /* We need to unlock here because mrtsock_destruct takes
1309 * care of rtnl itself and we can't change that due to
1310 * the IP_ROUTER_ALERT setsockopt which runs without it.
1311 */
1312 rtnl_unlock();
1313 ret = ip_ra_control(sk, 0, NULL);
1314 goto out;
1315 }
1316 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001317 case MRT_ADD_VIF:
1318 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001319 if (optlen != sizeof(vif)) {
1320 ret = -EINVAL;
1321 break;
1322 }
1323 if (copy_from_user(&vif, optval, sizeof(vif))) {
1324 ret = -EFAULT;
1325 break;
1326 }
1327 if (vif.vifc_vifi >= MAXVIFS) {
1328 ret = -ENFILE;
1329 break;
1330 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001331 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001332 ret = vif_add(net, mrt, &vif,
1333 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001334 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001335 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001336 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001337 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001338 /* Manipulate the forwarding caches. These live
1339 * in a sort of kernel/user symbiosis.
1340 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001341 case MRT_ADD_MFC:
1342 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001343 parent = -1;
1344 case MRT_ADD_MFC_PROXY:
1345 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001346 if (optlen != sizeof(mfc)) {
1347 ret = -EINVAL;
1348 break;
1349 }
1350 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1351 ret = -EFAULT;
1352 break;
1353 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001354 if (parent == 0)
1355 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001356 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1357 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001358 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001359 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001360 sk == rtnl_dereference(mrt->mroute_sk),
1361 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001362 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001363 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001364 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001365 if (optlen != sizeof(val)) {
1366 ret = -EINVAL;
1367 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001369 if (get_user(val, (int __user *)optval)) {
1370 ret = -EFAULT;
1371 break;
1372 }
1373 mrt->mroute_do_assert = val;
1374 break;
1375 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001376 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001377 ret = -ENOPROTOOPT;
1378 break;
1379 }
1380 if (optlen != sizeof(val)) {
1381 ret = -EINVAL;
1382 break;
1383 }
1384 if (get_user(val, (int __user *)optval)) {
1385 ret = -EFAULT;
1386 break;
1387 }
1388
1389 val = !!val;
1390 if (val != mrt->mroute_do_pim) {
1391 mrt->mroute_do_pim = val;
1392 mrt->mroute_do_assert = val;
1393 }
1394 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001395 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001396 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1397 ret = -ENOPROTOOPT;
1398 break;
1399 }
1400 if (optlen != sizeof(uval)) {
1401 ret = -EINVAL;
1402 break;
1403 }
1404 if (get_user(uval, (u32 __user *)optval)) {
1405 ret = -EFAULT;
1406 break;
1407 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001408
Eric Dumazet4c968702010-10-01 16:15:01 +00001409 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1410 ret = -EBUSY;
1411 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001412 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001413 if (IS_ERR(mrt))
1414 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001415 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001416 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c968702010-10-01 16:15:01 +00001417 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001418 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001419 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001420 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001421 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001423out_unlock:
1424 rtnl_unlock();
1425out:
1426 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427}
1428
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001429/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001430int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431{
1432 int olr;
1433 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001434 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001435 struct mr_table *mrt;
1436
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001437 if (sk->sk_type != SOCK_RAW ||
1438 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1439 return -EOPNOTSUPP;
1440
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001441 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001442 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001443 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001444
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001445 switch (optname) {
1446 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001447 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001448 break;
1449 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001450 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001451 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001452 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001453 break;
1454 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001455 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001456 break;
1457 default:
1458 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001459 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001460
1461 if (get_user(olr, optlen))
1462 return -EFAULT;
1463 olr = min_t(unsigned int, olr, sizeof(int));
1464 if (olr < 0)
1465 return -EINVAL;
1466 if (put_user(olr, optlen))
1467 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001468 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 return -EFAULT;
1470 return 0;
1471}
1472
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001473/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1475{
1476 struct sioc_sg_req sr;
1477 struct sioc_vif_req vr;
1478 struct vif_device *vif;
1479 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001480 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001481 struct mr_table *mrt;
1482
1483 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001484 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001485 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001486
Stephen Hemminger132adf52007-03-08 20:44:43 -08001487 switch (cmd) {
1488 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001489 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001490 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001491 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001492 return -EINVAL;
1493 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001494 vif = &mrt->vif_table[vr.vifi];
1495 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001496 vr.icount = vif->pkt_in;
1497 vr.ocount = vif->pkt_out;
1498 vr.ibytes = vif->bytes_in;
1499 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001500 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001501
Jianjun Kongc354e122008-11-03 00:28:02 -08001502 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001504 return 0;
1505 }
1506 read_unlock(&mrt_lock);
1507 return -EADDRNOTAVAIL;
1508 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001509 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001510 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511
Eric Dumazeta8c94862010-10-01 16:15:08 +00001512 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001513 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001514 if (c) {
1515 sr.pktcnt = c->mfc_un.res.pkt;
1516 sr.bytecnt = c->mfc_un.res.bytes;
1517 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001518 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001519
Jianjun Kongc354e122008-11-03 00:28:02 -08001520 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001521 return -EFAULT;
1522 return 0;
1523 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001524 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001525 return -EADDRNOTAVAIL;
1526 default:
1527 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 }
1529}
1530
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001531#ifdef CONFIG_COMPAT
1532struct compat_sioc_sg_req {
1533 struct in_addr src;
1534 struct in_addr grp;
1535 compat_ulong_t pktcnt;
1536 compat_ulong_t bytecnt;
1537 compat_ulong_t wrong_if;
1538};
1539
David S. Millerca6b8bb02011-02-03 17:24:28 -08001540struct compat_sioc_vif_req {
1541 vifi_t vifi; /* Which iface */
1542 compat_ulong_t icount;
1543 compat_ulong_t ocount;
1544 compat_ulong_t ibytes;
1545 compat_ulong_t obytes;
1546};
1547
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001548int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1549{
David S. Miller0033d5a2011-02-03 17:21:31 -08001550 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001551 struct compat_sioc_vif_req vr;
1552 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001553 struct mfc_cache *c;
1554 struct net *net = sock_net(sk);
1555 struct mr_table *mrt;
1556
1557 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001558 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001559 return -ENOENT;
1560
1561 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001562 case SIOCGETVIFCNT:
1563 if (copy_from_user(&vr, arg, sizeof(vr)))
1564 return -EFAULT;
1565 if (vr.vifi >= mrt->maxvif)
1566 return -EINVAL;
Gustavo A. R. Silva35775cc2018-12-10 12:41:24 -06001567 vr.vifi = array_index_nospec(vr.vifi, mrt->maxvif);
David S. Millerca6b8bb02011-02-03 17:24:28 -08001568 read_lock(&mrt_lock);
1569 vif = &mrt->vif_table[vr.vifi];
1570 if (VIF_EXISTS(mrt, vr.vifi)) {
1571 vr.icount = vif->pkt_in;
1572 vr.ocount = vif->pkt_out;
1573 vr.ibytes = vif->bytes_in;
1574 vr.obytes = vif->bytes_out;
1575 read_unlock(&mrt_lock);
1576
1577 if (copy_to_user(arg, &vr, sizeof(vr)))
1578 return -EFAULT;
1579 return 0;
1580 }
1581 read_unlock(&mrt_lock);
1582 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001583 case SIOCGETSGCNT:
1584 if (copy_from_user(&sr, arg, sizeof(sr)))
1585 return -EFAULT;
1586
1587 rcu_read_lock();
1588 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1589 if (c) {
1590 sr.pktcnt = c->mfc_un.res.pkt;
1591 sr.bytecnt = c->mfc_un.res.bytes;
1592 sr.wrong_if = c->mfc_un.res.wrong_if;
1593 rcu_read_unlock();
1594
1595 if (copy_to_user(arg, &sr, sizeof(sr)))
1596 return -EFAULT;
1597 return 0;
1598 }
1599 rcu_read_unlock();
1600 return -EADDRNOTAVAIL;
1601 default:
1602 return -ENOIOCTLCMD;
1603 }
1604}
1605#endif
1606
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1608{
Jiri Pirko351638e2013-05-28 01:30:21 +00001609 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001610 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001611 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 struct vif_device *v;
1613 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001614
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 if (event != NETDEV_UNREGISTER)
1616 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001617
1618 ipmr_for_each_table(mrt, net) {
1619 v = &mrt->vif_table[0];
1620 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1621 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001622 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 }
1625 return NOTIFY_DONE;
1626}
1627
Jianjun Kongc354e122008-11-03 00:28:02 -08001628static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 .notifier_call = ipmr_device_event,
1630};
1631
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001632/* Encapsulate a packet by attaching a valid IPIP header to it.
1633 * This avoids tunnel drivers and other mess and gives us the speed so
1634 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001636static void ip_encap(struct net *net, struct sk_buff *skb,
1637 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001639 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001640 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001641
1642 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001643 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001644 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001645 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001647 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001648 iph->tos = old_iph->tos;
1649 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650 iph->frag_off = 0;
1651 iph->daddr = daddr;
1652 iph->saddr = saddr;
1653 iph->protocol = IPPROTO_IPIP;
1654 iph->ihl = 5;
1655 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001656 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 ip_send_check(iph);
1658
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1660 nf_reset(skb);
1661}
1662
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001663static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1664 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001666 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667
David S. Miller73186df2015-11-03 13:41:45 -05001668 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1669 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
1671 if (unlikely(opt->optlen))
1672 ip_forward_options(skb);
1673
Eric W. Biederman13206b62015-10-07 16:48:35 -05001674 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675}
1676
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001677/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
Patrick McHardy0c122952010-04-13 05:03:22 +00001679static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1680 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001682 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001683 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 struct net_device *dev;
1685 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001686 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 int encap = 0;
1688
Ian Morris51456b22015-04-03 09:17:26 +01001689 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 goto out_free;
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 if (vif->flags & VIFF_REGISTER) {
1693 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001694 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001695 vif->dev->stats.tx_bytes += skb->len;
1696 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001697 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001698 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001701 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001702 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001703 vif->remote, vif->local,
1704 0, 0,
1705 IPPROTO_IPIP,
1706 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001707 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 goto out_free;
1709 encap = sizeof(struct iphdr);
1710 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001711 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001712 0, 0,
1713 IPPROTO_IPIP,
1714 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001715 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716 goto out_free;
1717 }
1718
Changli Gaod8d1f302010-06-10 23:31:35 -07001719 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Changli Gaod8d1f302010-06-10 23:31:35 -07001721 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001723 * allow to send ICMP, so that packets will disappear
1724 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 */
David S. Miller73186df2015-11-03 13:41:45 -05001726 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 ip_rt_put(rt);
1728 goto out_free;
1729 }
1730
Changli Gaod8d1f302010-06-10 23:31:35 -07001731 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732
1733 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001734 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 goto out_free;
1736 }
1737
1738 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001739 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Eric Dumazetadf30902009-06-02 05:19:30 +00001741 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001742 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001743 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744
1745 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001746 * What do we do with netfilter? -- RR
1747 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001749 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001751 vif->dev->stats.tx_packets++;
1752 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001755 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001757 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 * not only before forwarding, but after forwarding on all output
1759 * interfaces. It is clear, if mrouter runs a multicasting
1760 * program, it should receive packets not depending to what interface
1761 * program is joined.
1762 * If we will not make it, the program will have to join on all
1763 * interfaces. On the other hand, multihoming host (or router, but
1764 * not mrouter) cannot join to more than one interface - it will
1765 * result in receiving multiple packets.
1766 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001767 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1768 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 ipmr_forward_finish);
1770 return;
1771
1772out_free:
1773 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Patrick McHardy0c122952010-04-13 05:03:22 +00001776static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777{
1778 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001779
1780 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1781 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 break;
1783 }
1784 return ct;
1785}
1786
1787/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001788static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1789 struct sk_buff *skb, struct mfc_cache *cache,
1790 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791{
1792 int psend = -1;
1793 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001794 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 vif = cache->mfc_parent;
1797 cache->mfc_un.res.pkt++;
1798 cache->mfc_un.res.bytes += skb->len;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03001799 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001801 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001802 struct mfc_cache *cache_proxy;
1803
1804 /* For an (*,G) entry, we only check that the incomming
1805 * interface is part of the static tree.
1806 */
1807 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1808 if (cache_proxy &&
1809 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1810 goto forward;
1811 }
1812
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001813 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001814 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001815 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001817 * Very complicated situation...
1818 *
1819 * The best workaround until routing daemons will be
1820 * fixed is not to redistribute packet, if it was
1821 * send through wrong interface. It means, that
1822 * multicast applications WILL NOT work for
1823 * (S,G), which have default multicast route pointing
1824 * to wrong oif. In any case, it is not a good
1825 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 */
1827 goto dont_forward;
1828 }
1829
1830 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831
Patrick McHardy0c122952010-04-13 05:03:22 +00001832 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001834 * so that we cannot check that packet arrived on an oif.
1835 * It is bad, but otherwise we would need to move pretty
1836 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001838 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001839 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001840 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1842 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001843 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
1845 goto dont_forward;
1846 }
1847
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001848forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001849 mrt->vif_table[vif].pkt_in++;
1850 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001852 /* Forward the frame */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001853 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1854 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001855 if (true_vifi >= 0 &&
1856 true_vifi != cache->mfc_parent &&
1857 ip_hdr(skb)->ttl >
1858 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1859 /* It's an (*,*) entry and the packet is not coming from
1860 * the upstream: forward the packet to the upstream
1861 * only.
1862 */
1863 psend = cache->mfc_parent;
1864 goto last_forward;
1865 }
1866 goto dont_forward;
1867 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001868 for (ct = cache->mfc_un.res.maxvif - 1;
1869 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001870 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001871 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1872 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001873 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 if (psend != -1) {
1875 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001878 ipmr_queue_xmit(net, mrt, skb2, cache,
1879 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001881 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001884last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (psend != -1) {
1886 if (local) {
1887 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001888
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001890 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001892 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001893 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 }
1895 }
1896
1897dont_forward:
1898 if (!local)
1899 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
David S. Miller417da662011-05-03 19:42:43 -07001902static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001903{
David S. Miller417da662011-05-03 19:42:43 -07001904 struct rtable *rt = skb_rtable(skb);
1905 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001906 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001907 .daddr = iph->daddr,
1908 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001909 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001910 .flowi4_oif = (rt_is_output_route(rt) ?
1911 skb->dev->ifindex : 0),
1912 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001913 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001914 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001915 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001916 };
1917 struct mr_table *mrt;
1918 int err;
1919
David S. Millerda919812011-03-12 02:04:50 -05001920 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001921 if (err)
1922 return ERR_PTR(err);
1923 return mrt;
1924}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001926/* Multicast packets for forwarding arrive here
1927 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929int ip_mr_input(struct sk_buff *skb)
1930{
1931 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001932 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001933 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001934 struct mr_table *mrt;
Thomas Winterc4eca7e2017-05-16 10:14:44 +12001935 struct net_device *dev;
1936
1937 /* skb->dev passed in is the loX master dev for vrfs.
1938 * As there are no vifs associated with loopback devices,
1939 * get the proper interface that does have a vif associated with it.
1940 */
1941 dev = skb->dev;
1942 if (netif_is_l3_master(skb->dev)) {
1943 dev = dev_get_by_index_rcu(net, IPCB(skb)->iif);
1944 if (!dev) {
1945 kfree_skb(skb);
1946 return -ENODEV;
1947 }
1948 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
1950 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001951 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001953 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 goto dont_forward;
1955
David S. Miller417da662011-05-03 19:42:43 -07001956 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001957 if (IS_ERR(mrt)) {
1958 kfree_skb(skb);
1959 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001962 if (IPCB(skb)->opt.router_alert) {
1963 if (ip_call_ra_chain(skb))
1964 return 0;
1965 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1966 /* IGMPv1 (and broken IGMPv2 implementations sort of
1967 * Cisco IOS <= 11.2(8)) do not put router alert
1968 * option to IGMP packets destined to routable
1969 * groups. It is very bad, because it means
1970 * that we can forward NO IGMP messages.
1971 */
1972 struct sock *mroute_sk;
1973
1974 mroute_sk = rcu_dereference(mrt->mroute_sk);
1975 if (mroute_sk) {
1976 nf_reset(skb);
1977 raw_rcv(mroute_sk, skb);
1978 return 0;
1979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 }
1981 }
1982
Eric Dumazeta8c94862010-10-01 16:15:08 +00001983 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001984 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01001985 if (!cache) {
Thomas Winterc4eca7e2017-05-16 10:14:44 +12001986 int vif = ipmr_find_vif(mrt, dev);
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001987
1988 if (vif >= 0)
1989 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1990 vif);
1991 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001993 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01001994 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 int vif;
1996
1997 if (local) {
1998 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1999 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002000 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 skb = skb2;
2003 }
2004
Eric Dumazeta8c94862010-10-01 16:15:08 +00002005 read_lock(&mrt_lock);
Thomas Winterc4eca7e2017-05-16 10:14:44 +12002006 vif = ipmr_find_vif(mrt, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002008 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 read_unlock(&mrt_lock);
2010
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002011 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013 read_unlock(&mrt_lock);
2014 kfree_skb(skb);
2015 return -ENODEV;
2016 }
2017
Eric Dumazeta8c94862010-10-01 16:15:08 +00002018 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002019 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 read_unlock(&mrt_lock);
2021
2022 if (local)
2023 return ip_local_deliver(skb);
2024
2025 return 0;
2026
2027dont_forward:
2028 if (local)
2029 return ip_local_deliver(skb);
2030 kfree_skb(skb);
2031 return 0;
2032}
2033
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002034#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002035/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002036int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002037{
2038 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002039 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002040 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002041
2042 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2043 goto drop;
2044
2045 pim = igmp_hdr(skb);
2046
David S. Miller417da662011-05-03 19:42:43 -07002047 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002048 if (IS_ERR(mrt))
2049 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002050 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002051 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2052 goto drop;
2053
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002054 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002055drop:
2056 kfree_skb(skb);
2057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return 0;
2059}
2060#endif
2061
2062#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002063static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064{
2065 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002066 struct net *net = dev_net(skb->dev);
2067 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002069 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 goto drop;
2071
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002072 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002073 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2074 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002075 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002076 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077 goto drop;
2078
David S. Miller417da662011-05-03 19:42:43 -07002079 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002080 if (IS_ERR(mrt))
2081 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002082 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002083drop:
2084 kfree_skb(skb);
2085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 return 0;
2087}
2088#endif
2089
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002090static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2091 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002093 struct rta_mfc_stats mfcs;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002094 struct nlattr *mp_attr;
2095 struct rtnexthop *nhp;
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002096 unsigned long lastuse;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002097 int ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002098
Nicolas Dichtel74381892010-03-25 23:45:35 +00002099 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002100 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002101 return -ENOENT;
2102
Thomas Graf92a395e2012-06-26 23:36:13 +00002103 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2104 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2105 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106
Thomas Graf92a395e2012-06-26 23:36:13 +00002107 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2108 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109
2110 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002111 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002112 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2113 nla_nest_cancel(skb, mp_attr);
2114 return -EMSGSIZE;
2115 }
2116
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117 nhp->rtnh_flags = 0;
2118 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002119 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 nhp->rtnh_len = sizeof(*nhp);
2121 }
2122 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002123
2124 nla_nest_end(skb, mp_attr);
2125
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002126 lastuse = READ_ONCE(c->mfc_un.res.lastuse);
2127 lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
2128
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002129 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2130 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2131 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002132 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002133 nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002134 RTA_PAD))
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002135 return -EMSGSIZE;
2136
Linus Torvalds1da177e2005-04-16 15:20:36 -07002137 rtm->rtm_type = RTN_MULTICAST;
2138 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139}
2140
David S. Miller9a1b9492011-05-04 12:18:54 -07002141int ipmr_get_route(struct net *net, struct sk_buff *skb,
2142 __be32 saddr, __be32 daddr,
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002143 struct rtmsg *rtm, int nowait, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002146 struct mr_table *mrt;
2147 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002149 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002150 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002151 return -ENOENT;
2152
Eric Dumazeta8c94862010-10-01 16:15:08 +00002153 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002154 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002155 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002156 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002157
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002158 if (vif >= 0)
2159 cache = ipmr_cache_find_any(mrt, daddr, vif);
2160 }
Ian Morris51456b22015-04-03 09:17:26 +01002161 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002162 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002163 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002165 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166
2167 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002168 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169 return -EAGAIN;
2170 }
2171
2172 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002173 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002174 if (dev)
2175 vif = ipmr_find_vif(mrt, dev);
2176 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002178 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 return -ENODEV;
2180 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002181 skb2 = skb_clone(skb, GFP_ATOMIC);
2182 if (!skb2) {
2183 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002184 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002185 return -ENOMEM;
2186 }
2187
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002188 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002189 skb_push(skb2, sizeof(struct iphdr));
2190 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002191 iph = ip_hdr(skb2);
2192 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002193 iph->saddr = saddr;
2194 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002195 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002196 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002198 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return err;
2200 }
2201
Eric Dumazeta8c94862010-10-01 16:15:08 +00002202 read_lock(&mrt_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002203 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002205 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206 return err;
2207}
2208
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002209static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002210 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2211 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002212{
2213 struct nlmsghdr *nlh;
2214 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002215 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002216
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002217 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002218 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002219 return -EMSGSIZE;
2220
2221 rtm = nlmsg_data(nlh);
2222 rtm->rtm_family = RTNL_FAMILY_IPMR;
2223 rtm->rtm_dst_len = 32;
2224 rtm->rtm_src_len = 32;
2225 rtm->rtm_tos = 0;
2226 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002227 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2228 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002229 rtm->rtm_type = RTN_MULTICAST;
2230 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002231 if (c->mfc_flags & MFC_STATIC)
2232 rtm->rtm_protocol = RTPROT_STATIC;
2233 else
2234 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002235 rtm->rtm_flags = 0;
2236
Jiri Benc930345e2015-03-29 16:59:25 +02002237 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2238 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002239 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002240 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2241 /* do not break the dump if cache is unresolved */
2242 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002243 goto nla_put_failure;
2244
Johannes Berg053c0952015-01-16 22:09:00 +01002245 nlmsg_end(skb, nlh);
2246 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002247
2248nla_put_failure:
2249 nlmsg_cancel(skb, nlh);
2250 return -EMSGSIZE;
2251}
2252
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002253static size_t mroute_msgsize(bool unresolved, int maxvif)
2254{
2255 size_t len =
2256 NLMSG_ALIGN(sizeof(struct rtmsg))
2257 + nla_total_size(4) /* RTA_TABLE */
2258 + nla_total_size(4) /* RTA_SRC */
2259 + nla_total_size(4) /* RTA_DST */
2260 ;
2261
2262 if (!unresolved)
2263 len = len
2264 + nla_total_size(4) /* RTA_IIF */
2265 + nla_total_size(0) /* RTA_MULTIPATH */
2266 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2267 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002268 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002269 ;
2270
2271 return len;
2272}
2273
2274static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2275 int cmd)
2276{
2277 struct net *net = read_pnet(&mrt->net);
2278 struct sk_buff *skb;
2279 int err = -ENOBUFS;
2280
2281 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2282 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002283 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002284 goto errout;
2285
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002286 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002287 if (err < 0)
2288 goto errout;
2289
2290 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2291 return;
2292
2293errout:
2294 kfree_skb(skb);
2295 if (err < 0)
2296 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2297}
2298
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002299static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2300{
2301 struct net *net = sock_net(skb->sk);
2302 struct mr_table *mrt;
2303 struct mfc_cache *mfc;
2304 unsigned int t = 0, s_t;
2305 unsigned int h = 0, s_h;
2306 unsigned int e = 0, s_e;
2307
2308 s_t = cb->args[0];
2309 s_h = cb->args[1];
2310 s_e = cb->args[2];
2311
Eric Dumazeta8c94862010-10-01 16:15:08 +00002312 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002313 ipmr_for_each_table(mrt, net) {
2314 if (t < s_t)
2315 goto next_table;
2316 if (t > s_t)
2317 s_h = 0;
2318 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002319 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002320 if (e < s_e)
2321 goto next_entry;
2322 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002323 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002324 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002325 mfc, RTM_NEWROUTE,
2326 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002327 goto done;
2328next_entry:
2329 e++;
2330 }
2331 e = s_e = 0;
2332 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002333 spin_lock_bh(&mfc_unres_lock);
2334 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2335 if (e < s_e)
2336 goto next_entry2;
2337 if (ipmr_fill_mroute(mrt, skb,
2338 NETLINK_CB(cb->skb).portid,
2339 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002340 mfc, RTM_NEWROUTE,
2341 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002342 spin_unlock_bh(&mfc_unres_lock);
2343 goto done;
2344 }
2345next_entry2:
2346 e++;
2347 }
2348 spin_unlock_bh(&mfc_unres_lock);
2349 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002350 s_h = 0;
2351next_table:
2352 t++;
2353 }
2354done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002355 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002356
2357 cb->args[2] = e;
2358 cb->args[1] = h;
2359 cb->args[0] = t;
2360
2361 return skb->len;
2362}
2363
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002364static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2365 [RTA_SRC] = { .type = NLA_U32 },
2366 [RTA_DST] = { .type = NLA_U32 },
2367 [RTA_IIF] = { .type = NLA_U32 },
2368 [RTA_TABLE] = { .type = NLA_U32 },
2369 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2370};
2371
2372static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2373{
2374 switch (rtm_protocol) {
2375 case RTPROT_STATIC:
2376 case RTPROT_MROUTED:
2377 return true;
2378 }
2379 return false;
2380}
2381
2382static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2383{
2384 struct rtnexthop *rtnh = nla_data(nla);
2385 int remaining = nla_len(nla), vifi = 0;
2386
2387 while (rtnh_ok(rtnh, remaining)) {
2388 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2389 if (++vifi == MAXVIFS)
2390 break;
2391 rtnh = rtnh_next(rtnh, &remaining);
2392 }
2393
2394 return remaining > 0 ? -EINVAL : vifi;
2395}
2396
2397/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2398static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2399 struct mfcctl *mfcc, int *mrtsock,
2400 struct mr_table **mrtret)
2401{
2402 struct net_device *dev = NULL;
2403 u32 tblid = RT_TABLE_DEFAULT;
2404 struct mr_table *mrt;
2405 struct nlattr *attr;
2406 struct rtmsg *rtm;
2407 int ret, rem;
2408
2409 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy);
2410 if (ret < 0)
2411 goto out;
2412 rtm = nlmsg_data(nlh);
2413
2414 ret = -EINVAL;
2415 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2416 rtm->rtm_type != RTN_MULTICAST ||
2417 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2418 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2419 goto out;
2420
2421 memset(mfcc, 0, sizeof(*mfcc));
2422 mfcc->mfcc_parent = -1;
2423 ret = 0;
2424 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2425 switch (nla_type(attr)) {
2426 case RTA_SRC:
2427 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2428 break;
2429 case RTA_DST:
2430 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2431 break;
2432 case RTA_IIF:
2433 dev = __dev_get_by_index(net, nla_get_u32(attr));
2434 if (!dev) {
2435 ret = -ENODEV;
2436 goto out;
2437 }
2438 break;
2439 case RTA_MULTIPATH:
2440 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2441 ret = -EINVAL;
2442 goto out;
2443 }
2444 break;
2445 case RTA_PREFSRC:
2446 ret = 1;
2447 break;
2448 case RTA_TABLE:
2449 tblid = nla_get_u32(attr);
2450 break;
2451 }
2452 }
2453 mrt = ipmr_get_table(net, tblid);
2454 if (!mrt) {
2455 ret = -ENOENT;
2456 goto out;
2457 }
2458 *mrtret = mrt;
2459 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2460 if (dev)
2461 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2462
2463out:
2464 return ret;
2465}
2466
2467/* takes care of both newroute and delroute */
2468static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh)
2469{
2470 struct net *net = sock_net(skb->sk);
2471 int ret, mrtsock, parent;
2472 struct mr_table *tbl;
2473 struct mfcctl mfcc;
2474
2475 mrtsock = 0;
2476 tbl = NULL;
2477 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl);
2478 if (ret < 0)
2479 return ret;
2480
2481 parent = ret ? mfcc.mfcc_parent : -1;
2482 if (nlh->nlmsg_type == RTM_NEWROUTE)
2483 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2484 else
2485 return ipmr_mfc_delete(tbl, &mfcc, parent);
2486}
2487
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002488#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002489/* The /proc interfaces to multicast routing :
2490 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491 */
2492struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002493 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002494 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002495 int ct;
2496};
2497
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002498static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2499 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 loff_t pos)
2501{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002502 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002503
2504 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2505 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002507 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002508 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 }
2510 return NULL;
2511}
2512
2513static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002514 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002516 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002517 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002518 struct mr_table *mrt;
2519
2520 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002521 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002522 return ERR_PTR(-ENOENT);
2523
2524 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002527 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 : SEQ_START_TOKEN;
2529}
2530
2531static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2532{
2533 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002534 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002535 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
2537 ++*pos;
2538 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002539 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002540
Patrick McHardy0c122952010-04-13 05:03:22 +00002541 while (++iter->ct < mrt->maxvif) {
2542 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002544 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 }
2546 return NULL;
2547}
2548
2549static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002550 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551{
2552 read_unlock(&mrt_lock);
2553}
2554
2555static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2556{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002557 struct ipmr_vif_iter *iter = seq->private;
2558 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002559
Linus Torvalds1da177e2005-04-16 15:20:36 -07002560 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002561 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2563 } else {
2564 const struct vif_device *vif = v;
2565 const char *name = vif->dev ? vif->dev->name : "none";
2566
2567 seq_printf(seq,
2568 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002569 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002570 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 vif->bytes_out, vif->pkt_out,
2572 vif->flags, vif->local, vif->remote);
2573 }
2574 return 0;
2575}
2576
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002577static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 .start = ipmr_vif_seq_start,
2579 .next = ipmr_vif_seq_next,
2580 .stop = ipmr_vif_seq_stop,
2581 .show = ipmr_vif_seq_show,
2582};
2583
2584static int ipmr_vif_open(struct inode *inode, struct file *file)
2585{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002586 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2587 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588}
2589
Arjan van de Ven9a321442007-02-12 00:55:35 -08002590static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591 .owner = THIS_MODULE,
2592 .open = ipmr_vif_open,
2593 .read = seq_read,
2594 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002595 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596};
2597
2598struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002599 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002600 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002601 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602 int ct;
2603};
2604
2605
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002606static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2607 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002609 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 struct mfc_cache *mfc;
2611
Eric Dumazeta8c94862010-10-01 16:15:08 +00002612 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002613 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002614 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002615 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002616 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002618 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002619 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002622 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002623 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002624 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 return mfc;
2626 spin_unlock_bh(&mfc_unres_lock);
2627
2628 it->cache = NULL;
2629 return NULL;
2630}
2631
2632
2633static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2634{
2635 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002636 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002637 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002638
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002639 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002640 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002641 return ERR_PTR(-ENOENT);
2642
2643 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644 it->cache = NULL;
2645 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002646 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002647 : SEQ_START_TOKEN;
2648}
2649
2650static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2651{
2652 struct mfc_cache *mfc = v;
2653 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002654 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002655 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
2657 ++*pos;
2658
2659 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002660 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661
Patrick McHardy862465f2010-04-13 05:03:21 +00002662 if (mfc->list.next != it->cache)
2663 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002664
Patrick McHardy0c122952010-04-13 05:03:22 +00002665 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666 goto end_of_list;
2667
Patrick McHardy0c122952010-04-13 05:03:22 +00002668 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669
2670 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002671 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002672 if (list_empty(it->cache))
2673 continue;
2674 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 }
2676
2677 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002678 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002679 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002681
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002683 if (!list_empty(it->cache))
2684 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002686end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 spin_unlock_bh(&mfc_unres_lock);
2688 it->cache = NULL;
2689
2690 return NULL;
2691}
2692
2693static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2694{
2695 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002696 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697
Patrick McHardy0c122952010-04-13 05:03:22 +00002698 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002700 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002701 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002702}
2703
2704static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2705{
2706 int n;
2707
2708 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002709 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2711 } else {
2712 const struct mfc_cache *mfc = v;
2713 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002714 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002715
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002716 seq_printf(seq, "%08X %08X %-3hd",
2717 (__force u32) mfc->mfc_mcastgrp,
2718 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002719 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
Patrick McHardy0c122952010-04-13 05:03:22 +00002721 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002722 seq_printf(seq, " %8lu %8lu %8lu",
2723 mfc->mfc_un.res.pkt,
2724 mfc->mfc_un.res.bytes,
2725 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002726 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002727 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002728 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002729 mfc->mfc_un.res.ttls[n] < 255)
2730 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002731 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732 n, mfc->mfc_un.res.ttls[n]);
2733 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002734 } else {
2735 /* unresolved mfc_caches don't contain
2736 * pkt, bytes and wrong_if values
2737 */
2738 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 }
2740 seq_putc(seq, '\n');
2741 }
2742 return 0;
2743}
2744
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002745static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 .start = ipmr_mfc_seq_start,
2747 .next = ipmr_mfc_seq_next,
2748 .stop = ipmr_mfc_seq_stop,
2749 .show = ipmr_mfc_seq_show,
2750};
2751
2752static int ipmr_mfc_open(struct inode *inode, struct file *file)
2753{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002754 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2755 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756}
2757
Arjan van de Ven9a321442007-02-12 00:55:35 -08002758static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 .owner = THIS_MODULE,
2760 .open = ipmr_mfc_open,
2761 .read = seq_read,
2762 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002763 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002765#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
2767#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002768static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002769 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002770 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771};
2772#endif
2773
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002774/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002775static int __net_init ipmr_net_init(struct net *net)
2776{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002777 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002778
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002779 err = ipmr_rules_init(net);
2780 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002781 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002782
2783#ifdef CONFIG_PROC_FS
2784 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002785 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002786 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002787 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002788 goto proc_cache_fail;
2789#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002790 return 0;
2791
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002792#ifdef CONFIG_PROC_FS
2793proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002794 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002795proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002796 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002797#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002798fail:
2799 return err;
2800}
2801
2802static void __net_exit ipmr_net_exit(struct net *net)
2803{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002804#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002805 remove_proc_entry("ip_mr_cache", net->proc_net);
2806 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002807#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002808 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002809}
2810
2811static struct pernet_operations ipmr_net_ops = {
2812 .init = ipmr_net_init,
2813 .exit = ipmr_net_exit,
2814};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002815
Wang Chen03d2f892008-07-03 12:13:36 +08002816int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002817{
Wang Chen03d2f892008-07-03 12:13:36 +08002818 int err;
2819
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2821 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002822 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002823 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002824
Benjamin Therycf958ae32009-01-22 04:56:16 +00002825 err = register_pernet_subsys(&ipmr_net_ops);
2826 if (err)
2827 goto reg_pernet_fail;
2828
Wang Chen03d2f892008-07-03 12:13:36 +08002829 err = register_netdevice_notifier(&ip_mr_notifier);
2830 if (err)
2831 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002832#ifdef CONFIG_IP_PIMSM_V2
2833 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002834 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002835 err = -EAGAIN;
2836 goto add_proto_fail;
2837 }
2838#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002839 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2840 NULL, ipmr_rtm_dumproute, NULL);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002841 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
2842 ipmr_rtm_route, NULL, NULL);
2843 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
2844 ipmr_rtm_route, NULL, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002845 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002846
Tom Goff403dbb92009-06-14 03:16:13 -07002847#ifdef CONFIG_IP_PIMSM_V2
2848add_proto_fail:
2849 unregister_netdevice_notifier(&ip_mr_notifier);
2850#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002851reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002852 unregister_pernet_subsys(&ipmr_net_ops);
2853reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002854 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002855 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856}