blob: efc1e76d49770994f065c1bbad1ef8f93fa99bc7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Linux Consultancy and Custom Driver Development
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fixes:
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
22 * overflow.
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
Gilles Espinassef77f13e2010-03-29 15:41:47 +020025 * Relax this requirement to work with older peers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 */
28
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080029#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080031#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/timer.h>
34#include <linux/mm.h>
35#include <linux/kernel.h>
36#include <linux/fcntl.h>
37#include <linux/stat.h>
38#include <linux/socket.h>
39#include <linux/in.h>
40#include <linux/inet.h>
41#include <linux/netdevice.h>
42#include <linux/inetdevice.h>
43#include <linux/igmp.h>
44#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
46#include <linux/mroute.h>
47#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080048#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020050#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/ip.h>
52#include <net/protocol.h>
53#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020054#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/icmp.h>
57#include <net/udp.h>
58#include <net/raw.h>
59#include <linux/notifier.h>
60#include <linux/if_arp.h>
61#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000062#include <linux/compat.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040063#include <linux/export.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000064#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070066#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000067#include <net/fib_rules.h>
Nicolas Dichteld67b8c62012-12-04 01:13:35 +000068#include <linux/netconf.h>
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +010069#include <net/nexthop.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Patrick McHardyf0ad0862010-04-13 05:03:23 +000071struct ipmr_rule {
72 struct fib_rule common;
73};
74
75struct ipmr_result {
76 struct mr_table *mrt;
77};
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000080 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 */
82
83static DEFINE_RWLOCK(mrt_lock);
84
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +010085/* Multicast router control variables */
Linus Torvalds1da177e2005-04-16 15:20:36 -070086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087/* Special spinlock for queue of unresolved entries */
88static DEFINE_SPINLOCK(mfc_unres_lock);
89
90/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +000091 * entries is changed only in process context and protected
92 * with weak lock mrt_lock. Queue of unresolved entries is protected
93 * with strong spinlock mfc_unres_lock.
94 *
95 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 */
97
Christoph Lametere18b8902006-12-06 20:33:20 -080098static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000100static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000101static void ipmr_free_table(struct mr_table *mrt);
102
Rami Rosenc4854ec2013-07-20 15:09:28 +0300103static void ip_mr_forward(struct net *net, struct mr_table *mrt,
104 struct sk_buff *skb, struct mfc_cache *cache,
105 int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000106static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000107 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200108static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
109 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000110static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
111 int cmd);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100112static void mroute_clean_tables(struct mr_table *mrt, bool all);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000113static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000115#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
116#define ipmr_for_each_table(mrt, net) \
117 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
118
119static struct mr_table *ipmr_get_table(struct net *net, u32 id)
120{
121 struct mr_table *mrt;
122
123 ipmr_for_each_table(mrt, net) {
124 if (mrt->id == id)
125 return mrt;
126 }
127 return NULL;
128}
129
David S. Millerda919812011-03-12 02:04:50 -0500130static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000131 struct mr_table **mrt)
132{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000133 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100134 struct ipmr_result res;
135 struct fib_lookup_arg arg = {
136 .result = &res,
137 .flags = FIB_LOOKUP_NOREF,
138 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000139
David Aherne58e4152016-10-31 15:54:00 -0700140 /* update flow if oif or iif point to device enslaved to l3mdev */
141 l3mdev_update_flow(net, flowi4_to_flowi(flp4));
142
David S. Millerda919812011-03-12 02:04:50 -0500143 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
144 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000145 if (err < 0)
146 return err;
147 *mrt = res.mrt;
148 return 0;
149}
150
151static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
152 int flags, struct fib_lookup_arg *arg)
153{
154 struct ipmr_result *res = arg->result;
155 struct mr_table *mrt;
156
157 switch (rule->action) {
158 case FR_ACT_TO_TBL:
159 break;
160 case FR_ACT_UNREACHABLE:
161 return -ENETUNREACH;
162 case FR_ACT_PROHIBIT:
163 return -EACCES;
164 case FR_ACT_BLACKHOLE:
165 default:
166 return -EINVAL;
167 }
168
David Aherne58e4152016-10-31 15:54:00 -0700169 arg->table = fib_rule_get_table(rule, arg);
170
171 mrt = ipmr_get_table(rule->fr_net, arg->table);
Ian Morris51456b22015-04-03 09:17:26 +0100172 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000173 return -EAGAIN;
174 res->mrt = mrt;
175 return 0;
176}
177
178static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
179{
180 return 1;
181}
182
183static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
184 FRA_GENERIC_POLICY,
185};
186
187static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
188 struct fib_rule_hdr *frh, struct nlattr **tb)
189{
190 return 0;
191}
192
193static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
194 struct nlattr **tb)
195{
196 return 1;
197}
198
199static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
200 struct fib_rule_hdr *frh)
201{
202 frh->dst_len = 0;
203 frh->src_len = 0;
204 frh->tos = 0;
205 return 0;
206}
207
Andi Kleen04a6f822012-10-04 17:12:11 -0700208static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200209 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000210 .rule_size = sizeof(struct ipmr_rule),
211 .addr_size = sizeof(u32),
212 .action = ipmr_rule_action,
213 .match = ipmr_rule_match,
214 .configure = ipmr_rule_configure,
215 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000216 .fill = ipmr_rule_fill,
217 .nlgroup = RTNLGRP_IPV4_RULE,
218 .policy = ipmr_rule_policy,
219 .owner = THIS_MODULE,
220};
221
222static int __net_init ipmr_rules_init(struct net *net)
223{
224 struct fib_rules_ops *ops;
225 struct mr_table *mrt;
226 int err;
227
228 ops = fib_rules_register(&ipmr_rules_ops_template, net);
229 if (IS_ERR(ops))
230 return PTR_ERR(ops);
231
232 INIT_LIST_HEAD(&net->ipv4.mr_tables);
233
234 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100235 if (IS_ERR(mrt)) {
236 err = PTR_ERR(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000237 goto err1;
238 }
239
240 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
241 if (err < 0)
242 goto err2;
243
244 net->ipv4.mr_rules_ops = ops;
245 return 0;
246
247err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700248 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000249err1:
250 fib_rules_unregister(ops);
251 return err;
252}
253
254static void __net_exit ipmr_rules_exit(struct net *net)
255{
256 struct mr_table *mrt, *next;
257
WANG Conged785302015-03-31 11:01:45 -0700258 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000259 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
260 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000261 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000262 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000263 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700264 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000265}
266#else
267#define ipmr_for_each_table(mrt, net) \
268 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
269
270static struct mr_table *ipmr_get_table(struct net *net, u32 id)
271{
272 return net->ipv4.mrt;
273}
274
David S. Millerda919812011-03-12 02:04:50 -0500275static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000276 struct mr_table **mrt)
277{
278 *mrt = net->ipv4.mrt;
279 return 0;
280}
281
282static int __net_init ipmr_rules_init(struct net *net)
283{
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100284 struct mr_table *mrt;
285
286 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
287 if (IS_ERR(mrt))
288 return PTR_ERR(mrt);
289 net->ipv4.mrt = mrt;
290 return 0;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000291}
292
293static void __net_exit ipmr_rules_exit(struct net *net)
294{
WANG Conged785302015-03-31 11:01:45 -0700295 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000296 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700297 net->ipv4.mrt = NULL;
298 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000299}
300#endif
301
302static struct mr_table *ipmr_new_table(struct net *net, u32 id)
303{
304 struct mr_table *mrt;
305 unsigned int i;
306
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100307 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
308 if (id != RT_TABLE_DEFAULT && id >= 1000000000)
309 return ERR_PTR(-EINVAL);
310
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000311 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100312 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000313 return mrt;
314
315 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100316 if (!mrt)
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +0100317 return ERR_PTR(-ENOMEM);
Patrick McHardy8de53df2010-04-15 13:29:28 +0200318 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000319 mrt->id = id;
320
321 /* Forwarding cache */
322 for (i = 0; i < MFC_LINES; i++)
323 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
324
325 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
326
327 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
328 (unsigned long)mrt);
329
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000330 mrt->mroute_reg_vif_num = -1;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000331#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
332 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
333#endif
334 return mrt;
335}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000337static void ipmr_free_table(struct mr_table *mrt)
338{
339 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100340 mroute_clean_tables(mrt, true);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000341 kfree(mrt);
342}
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
345
Wang Chend6070322008-07-14 20:55:26 -0700346static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
347{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000348 struct net *net = dev_net(dev);
349
Wang Chend6070322008-07-14 20:55:26 -0700350 dev_close(dev);
351
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000352 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700353 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800354 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700355 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700356 struct ip_tunnel_parm p;
357
358 memset(&p, 0, sizeof(p));
359 p.iph.daddr = v->vifc_rmt_addr.s_addr;
360 p.iph.saddr = v->vifc_lcl_addr.s_addr;
361 p.iph.version = 4;
362 p.iph.ihl = 5;
363 p.iph.protocol = IPPROTO_IPIP;
364 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
365 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
366
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800367 if (ops->ndo_do_ioctl) {
368 mm_segment_t oldfs = get_fs();
369
370 set_fs(KERNEL_DS);
371 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
372 set_fs(oldfs);
373 }
Wang Chend6070322008-07-14 20:55:26 -0700374 }
375}
376
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100377/* Initialize ipmr pimreg/tunnel in_device */
378static bool ipmr_init_vif_indev(const struct net_device *dev)
379{
380 struct in_device *in_dev;
381
382 ASSERT_RTNL();
383
384 in_dev = __in_dev_get_rtnl(dev);
385 if (!in_dev)
386 return false;
387 ipv4_devconf_setall(in_dev);
388 neigh_parms_data_state_setall(in_dev->arp_parms);
389 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
390
391 return true;
392}
393
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100394static struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
396 struct net_device *dev;
397
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000398 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800401 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 int err;
403 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 struct ip_tunnel_parm p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 memset(&p, 0, sizeof(p));
407 p.iph.daddr = v->vifc_rmt_addr.s_addr;
408 p.iph.saddr = v->vifc_lcl_addr.s_addr;
409 p.iph.version = 4;
410 p.iph.ihl = 5;
411 p.iph.protocol = IPPROTO_IPIP;
412 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800413 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800415 if (ops->ndo_do_ioctl) {
416 mm_segment_t oldfs = get_fs();
417
418 set_fs(KERNEL_DS);
419 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
420 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000421 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800422 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 dev = NULL;
425
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000426 if (err == 0 &&
427 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 dev->flags |= IFF_MULTICAST;
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100429 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (dev_open(dev))
432 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700433 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435 }
436 return dev;
437
438failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 unregister_netdevice(dev);
440 return NULL;
441}
442
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100443#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000444static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000446 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000447 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500448 struct flowi4 fl4 = {
449 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700450 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500451 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000452 };
453 int err;
454
David S. Millerda919812011-03-12 02:04:50 -0500455 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000456 if (err < 0) {
457 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000458 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000459 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700462 dev->stats.tx_bytes += skb->len;
463 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000464 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 read_unlock(&mrt_lock);
466 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000467 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468}
469
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200470static int reg_vif_get_iflink(const struct net_device *dev)
471{
472 return 0;
473}
474
Stephen Hemminger007c3832008-11-20 20:28:35 -0800475static const struct net_device_ops reg_vif_netdev_ops = {
476 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200477 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800478};
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480static void reg_vif_setup(struct net_device *dev)
481{
482 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800483 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530485 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700487 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
489
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000490static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491{
492 struct net_device *dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000493 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000495 if (mrt->id == RT_TABLE_DEFAULT)
496 sprintf(name, "pimreg");
497 else
498 sprintf(name, "pimreg%u", mrt->id);
499
Tom Gundersenc835a672014-07-14 16:37:24 +0200500 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Ian Morris51456b22015-04-03 09:17:26 +0100502 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 return NULL;
504
Tom Goff403dbb92009-06-14 03:16:13 -0700505 dev_net_set(dev, net);
506
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 if (register_netdevice(dev)) {
508 free_netdev(dev);
509 return NULL;
510 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Nikolay Aleksandrova0b47732015-11-21 15:57:32 +0100512 if (!ipmr_init_vif_indev(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 goto failure;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (dev_open(dev))
515 goto failure;
516
Wang Chen7dc00c82008-07-14 20:56:34 -0700517 dev_hold(dev);
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return dev;
520
521failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 unregister_netdevice(dev);
523 return NULL;
524}
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100525
526/* called with rcu_read_lock() */
527static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
528 unsigned int pimlen)
529{
530 struct net_device *reg_dev = NULL;
531 struct iphdr *encap;
532
533 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100534 /* Check that:
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100535 * a. packet is really sent to a multicast group
536 * b. packet is not a NULL-REGISTER
537 * c. packet is not truncated
538 */
539 if (!ipv4_is_multicast(encap->daddr) ||
540 encap->tot_len == 0 ||
541 ntohs(encap->tot_len) + pimlen > skb->len)
542 return 1;
543
544 read_lock(&mrt_lock);
545 if (mrt->mroute_reg_vif_num >= 0)
546 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
547 read_unlock(&mrt_lock);
548
549 if (!reg_dev)
550 return 1;
551
552 skb->mac_header = skb->network_header;
553 skb_pull(skb, (u8 *)encap - skb->data);
554 skb_reset_network_header(skb);
555 skb->protocol = htons(ETH_P_IP);
556 skb->ip_summed = CHECKSUM_NONE;
557
558 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
559
560 netif_rx(skb);
561
562 return NET_RX_SUCCESS;
563}
564#else
565static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
566{
567 return NULL;
568}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569#endif
570
Ben Hutchings2c530402012-07-10 10:55:09 +0000571/**
572 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700573 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000575static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000576 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577{
578 struct vif_device *v;
579 struct net_device *dev;
580 struct in_device *in_dev;
581
Patrick McHardy0c122952010-04-13 05:03:22 +0000582 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 return -EADDRNOTAVAIL;
584
Patrick McHardy0c122952010-04-13 05:03:22 +0000585 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
587 write_lock_bh(&mrt_lock);
588 dev = v->dev;
589 v->dev = NULL;
590
591 if (!dev) {
592 write_unlock_bh(&mrt_lock);
593 return -EADDRNOTAVAIL;
594 }
595
Patrick McHardy0c122952010-04-13 05:03:22 +0000596 if (vifi == mrt->mroute_reg_vif_num)
597 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000599 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000601
602 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000603 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 break;
605 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000606 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608
609 write_unlock_bh(&mrt_lock);
610
611 dev_set_allmulti(dev, -1);
612
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000613 in_dev = __in_dev_get_rtnl(dev);
614 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700615 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000616 inet_netconf_notify_devconf(dev_net(dev),
617 NETCONFA_MC_FORWARDING,
618 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 ip_rt_multicast_event(in_dev);
620 }
621
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000622 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000623 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624
625 dev_put(dev);
626 return 0;
627}
628
Eric Dumazeta8c94862010-10-01 16:15:08 +0000629static void ipmr_cache_free_rcu(struct rcu_head *head)
630{
631 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
632
633 kmem_cache_free(mrt_cachep, c);
634}
635
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000636static inline void ipmr_cache_free(struct mfc_cache *c)
637{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000638 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000642 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000644static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200646 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700648 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Patrick McHardy0c122952010-04-13 05:03:22 +0000650 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
Jianjun Kongc354e122008-11-03 00:28:02 -0800652 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700653 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
655 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000656 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000658 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700659 e->error = -ETIMEDOUT;
660 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700661
Eric W. Biederman15e47302012-09-07 20:12:54 +0000662 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000663 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000665 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
667
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000668 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
Patrick McHardye258beb2010-04-13 05:03:19 +0000671/* Timer process for the unresolved queue. */
Patrick McHardye258beb2010-04-13 05:03:19 +0000672static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Patrick McHardy0c122952010-04-13 05:03:22 +0000674 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 unsigned long now;
676 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000677 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000680 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 return;
682 }
683
Patrick McHardy0c122952010-04-13 05:03:22 +0000684 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 goto out;
686
687 now = jiffies;
688 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Patrick McHardy0c122952010-04-13 05:03:22 +0000690 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 if (time_after(c->mfc_un.unres.expires, now)) {
692 unsigned long interval = c->mfc_un.unres.expires - now;
693 if (interval < expires)
694 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 continue;
696 }
697
Patrick McHardy862465f2010-04-13 05:03:21 +0000698 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000699 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000700 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
Patrick McHardy0c122952010-04-13 05:03:22 +0000703 if (!list_empty(&mrt->mfc_unres_queue))
704 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706out:
707 spin_unlock(&mfc_unres_lock);
708}
709
710/* Fill oifs list. It is called under write locked mrt_lock. */
Patrick McHardy0c122952010-04-13 05:03:22 +0000711static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000712 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
714 int vifi;
715
716 cache->mfc_un.res.minvif = MAXVIFS;
717 cache->mfc_un.res.maxvif = 0;
718 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
719
Patrick McHardy0c122952010-04-13 05:03:22 +0000720 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
721 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000722 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
724 if (cache->mfc_un.res.minvif > vifi)
725 cache->mfc_un.res.minvif = vifi;
726 if (cache->mfc_un.res.maxvif <= vifi)
727 cache->mfc_un.res.maxvif = vifi + 1;
728 }
729 }
Nikolay Aleksandrov90b5ca12016-07-26 18:54:52 +0200730 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731}
732
Patrick McHardy0c122952010-04-13 05:03:22 +0000733static int vif_add(struct net *net, struct mr_table *mrt,
734 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000737 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 struct net_device *dev;
739 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700740 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
742 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000743 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 return -EADDRINUSE;
745
746 switch (vifc->vifc_flags) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 case VIFF_REGISTER:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +0100748 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100749 return -EINVAL;
750 /* Special Purpose VIF in PIM
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * All the packets will be sent to the daemon
752 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000753 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000755 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 if (!dev)
757 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700758 err = dev_set_allmulti(dev, 1);
759 if (err) {
760 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700761 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700762 return err;
763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 break;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900765 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000766 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 if (!dev)
768 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700769 err = dev_set_allmulti(dev, 1);
770 if (err) {
771 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700772 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700773 return err;
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000776 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000778 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
779 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100780 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000781 dev_put(dev);
782 return -EADDRNOTAVAIL;
783 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000784 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000785 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 if (!dev)
788 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700789 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700790 if (err) {
791 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700792 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 break;
795 default:
796 return -EINVAL;
797 }
798
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000799 in_dev = __in_dev_get_rtnl(dev);
800 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000801 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000803 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700804 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000805 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
806 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 ip_rt_multicast_event(in_dev);
808
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000809 /* Fill in the VIF structures */
810
Jianjun Kongc354e122008-11-03 00:28:02 -0800811 v->rate_limit = vifc->vifc_rate_limit;
812 v->local = vifc->vifc_lcl_addr.s_addr;
813 v->remote = vifc->vifc_rmt_addr.s_addr;
814 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 if (!mrtsock)
816 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800817 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 v->bytes_in = 0;
819 v->bytes_out = 0;
820 v->pkt_in = 0;
821 v->pkt_out = 0;
822 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000823 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200824 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 /* And finish update writing critical data */
827 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800828 v->dev = dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000829 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000830 mrt->mroute_reg_vif_num = vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000831 if (vifi+1 > mrt->maxvif)
832 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 write_unlock_bh(&mrt_lock);
834 return 0;
835}
836
Eric Dumazeta8c94862010-10-01 16:15:08 +0000837/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000838static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000839 __be32 origin,
840 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841{
Jianjun Kongc354e122008-11-03 00:28:02 -0800842 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 struct mfc_cache *c;
844
Eric Dumazeta8c94862010-10-01 16:15:08 +0000845 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000846 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
847 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000849 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}
851
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000852/* Look for a (*,*,oif) entry */
853static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
854 int vifi)
855{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100856 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000857 struct mfc_cache *c;
858
859 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100860 if (c->mfc_origin == htonl(INADDR_ANY) &&
861 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000862 c->mfc_un.res.ttls[vifi] < 255)
863 return c;
864
865 return NULL;
866}
867
868/* Look for a (*,G) entry */
869static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
870 __be32 mcastgrp, int vifi)
871{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100872 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000873 struct mfc_cache *c, *proxy;
874
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100875 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000876 goto skip;
877
878 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100879 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000880 c->mfc_mcastgrp == mcastgrp) {
881 if (c->mfc_un.res.ttls[vifi] < 255)
882 return c;
883
884 /* It's ok if the vifi is part of the static tree */
885 proxy = ipmr_cache_find_any_parent(mrt,
886 c->mfc_parent);
887 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
888 return c;
889 }
890
891skip:
892 return ipmr_cache_find_any_parent(mrt, vifi);
893}
894
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100895/* Allocate a multicast cache entry */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000896static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897{
Jianjun Kongc354e122008-11-03 00:28:02 -0800898 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000899
Tom Goff70a0dec2016-06-23 16:11:57 -0400900 if (c) {
901 c->mfc_un.res.last_assert = jiffies - MFC_ASSERT_THRESH - 1;
Eric Dumazeta8c94862010-10-01 16:15:08 +0000902 c->mfc_un.res.minvif = MAXVIFS;
Tom Goff70a0dec2016-06-23 16:11:57 -0400903 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 return c;
905}
906
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000907static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Jianjun Kongc354e122008-11-03 00:28:02 -0800909 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000910
911 if (c) {
912 skb_queue_head_init(&c->mfc_un.unres.unresolved);
913 c->mfc_un.unres.expires = jiffies + 10*HZ;
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 return c;
916}
917
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +0100918/* A cache entry has gone into a resolved state from queued */
Patrick McHardy0c122952010-04-13 05:03:22 +0000919static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
920 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921{
922 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700923 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000925 /* Play the pending entries through our router */
Jianjun Kongc354e122008-11-03 00:28:02 -0800926 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700927 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
929
Hong zhi guo573ce262013-03-27 06:47:04 +0000930 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000931 nlh->nlmsg_len = skb_tail_pointer(skb) -
932 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 } else {
934 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000935 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000937 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700938 e->error = -EMSGSIZE;
939 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
Thomas Graf2942e902006-08-15 00:30:25 -0700941
Eric W. Biederman15e47302012-09-07 20:12:54 +0000942 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000943 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000944 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000945 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946 }
947}
948
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100949/* Bounce a cache query up to mrouted. We could use netlink for this but mrouted
950 * expects the following bizarre scheme.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 *
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100952 * Called under mrt_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000954static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000955 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956{
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300957 const int ihl = ip_hdrlen(pkt);
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100958 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 struct igmphdr *igmp;
960 struct igmpmsg *msg;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100961 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 int ret;
963
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (assert == IGMPMSG_WHOLEPKT)
965 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
966 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 skb = alloc_skb(128, GFP_ATOMIC);
968
Stephen Hemminger132adf52007-03-08 20:44:43 -0800969 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return -ENOBUFS;
971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 if (assert == IGMPMSG_WHOLEPKT) {
973 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000974 * Duplicate old header, fix ihl, length etc.
975 * And all this only to mangle msg->im_msgtype and
976 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300978 skb_push(skb, sizeof(struct iphdr));
979 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300980 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300981 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700982 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 msg->im_msgtype = IGMPMSG_WHOLEPKT;
984 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000985 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700986 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
987 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
988 sizeof(struct iphdr));
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +0100989 } else {
990 /* Copy the IP header */
991 skb_set_network_header(skb, skb->len);
992 skb_put(skb, ihl);
993 skb_copy_to_linear_data(skb, pkt->data, ihl);
994 /* Flag to the kernel this is a route add */
995 ip_hdr(skb)->protocol = 0;
996 msg = (struct igmpmsg *)skb_network_header(skb);
997 msg->im_vif = vifi;
998 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
999 /* Add our header */
1000 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
1001 igmp->type = assert;
1002 msg->im_msgtype = assert;
1003 igmp->code = 0;
1004 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
1005 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Eric Dumazet4c968702010-10-01 16:15:01 +00001008 rcu_read_lock();
1009 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001010 if (!mroute_sk) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001011 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 kfree_skb(skb);
1013 return -EINVAL;
1014 }
1015
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001016 /* Deliver to mrouted */
Eric Dumazet4c968702010-10-01 16:15:01 +00001017 ret = sock_queue_rcv_skb(mroute_sk, skb);
1018 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001019 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001020 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 kfree_skb(skb);
1022 }
1023
1024 return ret;
1025}
1026
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001027/* Queue a packet for resolution. It gets locked cache entry! */
1028static int ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi,
1029 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030{
Patrick McHardy862465f2010-04-13 05:03:21 +00001031 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 int err;
1033 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001034 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
1036 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001037 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001038 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001039 c->mfc_origin == iph->saddr) {
1040 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
Patrick McHardy862465f2010-04-13 05:03:21 +00001045 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001046 /* Create a new entry if allowable */
Patrick McHardy0c122952010-04-13 05:03:22 +00001047 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001048 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 spin_unlock_bh(&mfc_unres_lock);
1050
1051 kfree_skb(skb);
1052 return -ENOBUFS;
1053 }
1054
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001055 /* Fill in the new cache entry */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001056 c->mfc_parent = -1;
1057 c->mfc_origin = iph->saddr;
1058 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001060 /* Reflect first query at mrouted. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001061 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001062 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001063 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 out - Brad Parker
1065 */
1066 spin_unlock_bh(&mfc_unres_lock);
1067
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001068 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 kfree_skb(skb);
1070 return err;
1071 }
1072
Patrick McHardy0c122952010-04-13 05:03:22 +00001073 atomic_inc(&mrt->cache_resolve_queue_len);
1074 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001075 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076
David S. Miller278554b2010-05-12 00:05:35 -07001077 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1078 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001081 /* See if we can append the packet */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001082 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 kfree_skb(skb);
1084 err = -ENOBUFS;
1085 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001086 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 err = 0;
1088 }
1089
1090 spin_unlock_bh(&mfc_unres_lock);
1091 return err;
1092}
1093
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001094/* MFC cache manipulation by user space mroute daemon */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001096static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097{
1098 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001099 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Jianjun Kongc354e122008-11-03 00:28:02 -08001101 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102
Patrick McHardy0c122952010-04-13 05:03:22 +00001103 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001105 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1106 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001107 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001108 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001109 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 return 0;
1111 }
1112 }
1113 return -ENOENT;
1114}
1115
Patrick McHardy0c122952010-04-13 05:03:22 +00001116static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001117 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
Patrick McHardy862465f2010-04-13 05:03:21 +00001119 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001121 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Patrick McHardya50436f22010-03-17 06:04:14 +00001123 if (mfc->mfcc_parent >= MAXVIFS)
1124 return -ENFILE;
1125
Jianjun Kongc354e122008-11-03 00:28:02 -08001126 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Patrick McHardy0c122952010-04-13 05:03:22 +00001128 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001130 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1131 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001132 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001134 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 }
1136
Patrick McHardy862465f2010-04-13 05:03:21 +00001137 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 write_lock_bh(&mrt_lock);
1139 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001140 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 if (!mrtsock)
1142 c->mfc_flags |= MFC_STATIC;
1143 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001144 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 return 0;
1146 }
1147
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001148 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001149 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 return -EINVAL;
1151
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001152 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001153 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 return -ENOMEM;
1155
Jianjun Kongc354e122008-11-03 00:28:02 -08001156 c->mfc_origin = mfc->mfcc_origin.s_addr;
1157 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1158 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001159 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 if (!mrtsock)
1161 c->mfc_flags |= MFC_STATIC;
1162
Eric Dumazeta8c94862010-10-01 16:15:08 +00001163 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001165 /* Check to see if we resolved a queued list. If so we
1166 * need to send on the frames and tidy up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001168 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001170 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001171 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001173 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001174 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001175 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 break;
1177 }
1178 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001179 if (list_empty(&mrt->mfc_unres_queue))
1180 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 spin_unlock_bh(&mfc_unres_lock);
1182
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001183 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001184 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001185 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001187 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 return 0;
1189}
1190
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001191/* Close the multicast socket, and clear the vif tables etc */
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001192static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001195 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001196 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001197
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001198 /* Shut down all active vif entries */
Patrick McHardy0c122952010-04-13 05:03:22 +00001199 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001200 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1201 continue;
1202 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001204 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001206 /* Wipe the cache */
Patrick McHardy862465f2010-04-13 05:03:21 +00001207 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001208 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001209 if (!all && (c->mfc_flags & MFC_STATIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001211 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001212 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001213 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 }
1215 }
1216
Patrick McHardy0c122952010-04-13 05:03:22 +00001217 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001219 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001220 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001221 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001222 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 }
1224 spin_unlock_bh(&mfc_unres_lock);
1225 }
1226}
1227
Eric Dumazet4c968702010-10-01 16:15:01 +00001228/* called from ip_ra_control(), before an RCU grace period,
1229 * we dont need to call synchronize_rcu() here
1230 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231static void mrtsock_destruct(struct sock *sk)
1232{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001233 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001234 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001237 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001238 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001239 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001240 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1241 NETCONFA_IFINDEX_ALL,
1242 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001243 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001244 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247 rtnl_unlock();
1248}
1249
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001250/* Socket options and virtual interface manipulation. The whole
1251 * virtual interface system is a complete heap, but unfortunately
1252 * that's how BSD mrouted happens to think. Maybe one day with a proper
1253 * MOSPF/PIM router set up we can clean this up.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001255
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001256int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval,
1257 unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258{
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001259 struct net *net = sock_net(sk);
1260 int val, ret = 0, parent = 0;
1261 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 struct vifctl vif;
1263 struct mfcctl mfc;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001264 u32 uval;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001265
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001266 /* There's one exception to the lock - MRT_DONE which needs to unlock */
1267 rtnl_lock();
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001268 if (sk->sk_type != SOCK_RAW ||
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001269 inet_sk(sk)->inet_num != IPPROTO_IGMP) {
1270 ret = -EOPNOTSUPP;
1271 goto out_unlock;
1272 }
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001273
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001274 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001275 if (!mrt) {
1276 ret = -ENOENT;
1277 goto out_unlock;
1278 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001279 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001280 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001281 !ns_capable(net->user_ns, CAP_NET_ADMIN)) {
1282 ret = -EACCES;
1283 goto out_unlock;
1284 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 }
1286
Stephen Hemminger132adf52007-03-08 20:44:43 -08001287 switch (optname) {
1288 case MRT_INIT:
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001289 if (optlen != sizeof(int)) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001290 ret = -EINVAL;
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001291 break;
Nikolay Aleksandrov42e6b892015-11-26 15:23:49 +01001292 }
1293 if (rtnl_dereference(mrt->mroute_sk)) {
1294 ret = -EADDRINUSE;
1295 break;
1296 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001297
1298 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1299 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001300 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001301 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001302 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1303 NETCONFA_IFINDEX_ALL,
1304 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001305 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001306 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001307 case MRT_DONE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001308 if (sk != rcu_access_pointer(mrt->mroute_sk)) {
1309 ret = -EACCES;
1310 } else {
1311 /* We need to unlock here because mrtsock_destruct takes
1312 * care of rtnl itself and we can't change that due to
1313 * the IP_ROUTER_ALERT setsockopt which runs without it.
1314 */
1315 rtnl_unlock();
1316 ret = ip_ra_control(sk, 0, NULL);
1317 goto out;
1318 }
1319 break;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001320 case MRT_ADD_VIF:
1321 case MRT_DEL_VIF:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001322 if (optlen != sizeof(vif)) {
1323 ret = -EINVAL;
1324 break;
1325 }
1326 if (copy_from_user(&vif, optval, sizeof(vif))) {
1327 ret = -EFAULT;
1328 break;
1329 }
1330 if (vif.vifc_vifi >= MAXVIFS) {
1331 ret = -ENFILE;
1332 break;
1333 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001334 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001335 ret = vif_add(net, mrt, &vif,
1336 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001337 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001338 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001339 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001340 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001341 /* Manipulate the forwarding caches. These live
1342 * in a sort of kernel/user symbiosis.
1343 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001344 case MRT_ADD_MFC:
1345 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001346 parent = -1;
1347 case MRT_ADD_MFC_PROXY:
1348 case MRT_DEL_MFC_PROXY:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001349 if (optlen != sizeof(mfc)) {
1350 ret = -EINVAL;
1351 break;
1352 }
1353 if (copy_from_user(&mfc, optval, sizeof(mfc))) {
1354 ret = -EFAULT;
1355 break;
1356 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001357 if (parent == 0)
1358 parent = mfc.mfcc_parent;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001359 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1360 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001361 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001362 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001363 sk == rtnl_dereference(mrt->mroute_sk),
1364 parent);
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001365 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001366 /* Control PIM assert. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001367 case MRT_ASSERT:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001368 if (optlen != sizeof(val)) {
1369 ret = -EINVAL;
1370 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001372 if (get_user(val, (int __user *)optval)) {
1373 ret = -EFAULT;
1374 break;
1375 }
1376 mrt->mroute_do_assert = val;
1377 break;
1378 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001379 if (!ipmr_pimsm_enabled()) {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001380 ret = -ENOPROTOOPT;
1381 break;
1382 }
1383 if (optlen != sizeof(val)) {
1384 ret = -EINVAL;
1385 break;
1386 }
1387 if (get_user(val, (int __user *)optval)) {
1388 ret = -EFAULT;
1389 break;
1390 }
1391
1392 val = !!val;
1393 if (val != mrt->mroute_do_pim) {
1394 mrt->mroute_do_pim = val;
1395 mrt->mroute_do_assert = val;
1396 }
1397 break;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001398 case MRT_TABLE:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001399 if (!IS_BUILTIN(CONFIG_IP_MROUTE_MULTIPLE_TABLES)) {
1400 ret = -ENOPROTOOPT;
1401 break;
1402 }
1403 if (optlen != sizeof(uval)) {
1404 ret = -EINVAL;
1405 break;
1406 }
1407 if (get_user(uval, (u32 __user *)optval)) {
1408 ret = -EFAULT;
1409 break;
1410 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001411
Eric Dumazet4c968702010-10-01 16:15:01 +00001412 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1413 ret = -EBUSY;
1414 } else {
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001415 mrt = ipmr_new_table(net, uval);
Nikolay Aleksandrov1113ebb2015-11-21 15:57:24 +01001416 if (IS_ERR(mrt))
1417 ret = PTR_ERR(mrt);
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001418 else
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001419 raw_sk(sk)->ipmr_table = uval;
Eric Dumazet4c968702010-10-01 16:15:01 +00001420 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001421 break;
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001422 /* Spurious command, or MRT_VERSION which you cannot set. */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001423 default:
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001424 ret = -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 }
Nikolay Aleksandrov29e97d22015-11-21 15:57:31 +01001426out_unlock:
1427 rtnl_unlock();
1428out:
1429 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430}
1431
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001432/* Getsock opt support for the multicast routing system. */
Jianjun Kongc354e122008-11-03 00:28:02 -08001433int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001434{
1435 int olr;
1436 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001437 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001438 struct mr_table *mrt;
1439
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001440 if (sk->sk_type != SOCK_RAW ||
1441 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1442 return -EOPNOTSUPP;
1443
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001444 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001445 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001446 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001448 switch (optname) {
1449 case MRT_VERSION:
Jianjun Kongc354e122008-11-03 00:28:02 -08001450 val = 0x0305;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001451 break;
1452 case MRT_PIM:
Nikolay Aleksandrov1973a4e2015-11-26 15:23:48 +01001453 if (!ipmr_pimsm_enabled())
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001454 return -ENOPROTOOPT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001455 val = mrt->mroute_do_pim;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001456 break;
1457 case MRT_ASSERT:
Patrick McHardy0c122952010-04-13 05:03:22 +00001458 val = mrt->mroute_do_assert;
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001459 break;
1460 default:
1461 return -ENOPROTOOPT;
Nikolay Aleksandrovc316c622015-11-21 15:57:26 +01001462 }
Nikolay Aleksandrovfe9ef3c2015-11-21 15:57:28 +01001463
1464 if (get_user(olr, optlen))
1465 return -EFAULT;
1466 olr = min_t(unsigned int, olr, sizeof(int));
1467 if (olr < 0)
1468 return -EINVAL;
1469 if (put_user(olr, optlen))
1470 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001471 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472 return -EFAULT;
1473 return 0;
1474}
1475
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001476/* The IP multicast ioctl support routines. */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1478{
1479 struct sioc_sg_req sr;
1480 struct sioc_vif_req vr;
1481 struct vif_device *vif;
1482 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001483 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001484 struct mr_table *mrt;
1485
1486 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001487 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001488 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001489
Stephen Hemminger132adf52007-03-08 20:44:43 -08001490 switch (cmd) {
1491 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001492 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001493 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001494 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001495 return -EINVAL;
1496 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001497 vif = &mrt->vif_table[vr.vifi];
1498 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001499 vr.icount = vif->pkt_in;
1500 vr.ocount = vif->pkt_out;
1501 vr.ibytes = vif->bytes_in;
1502 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001504
Jianjun Kongc354e122008-11-03 00:28:02 -08001505 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001507 return 0;
1508 }
1509 read_unlock(&mrt_lock);
1510 return -EADDRNOTAVAIL;
1511 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001512 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001513 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Eric Dumazeta8c94862010-10-01 16:15:08 +00001515 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001516 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001517 if (c) {
1518 sr.pktcnt = c->mfc_un.res.pkt;
1519 sr.bytecnt = c->mfc_un.res.bytes;
1520 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001521 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001522
Jianjun Kongc354e122008-11-03 00:28:02 -08001523 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001524 return -EFAULT;
1525 return 0;
1526 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001527 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001528 return -EADDRNOTAVAIL;
1529 default:
1530 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532}
1533
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001534#ifdef CONFIG_COMPAT
1535struct compat_sioc_sg_req {
1536 struct in_addr src;
1537 struct in_addr grp;
1538 compat_ulong_t pktcnt;
1539 compat_ulong_t bytecnt;
1540 compat_ulong_t wrong_if;
1541};
1542
David S. Millerca6b8bb02011-02-03 17:24:28 -08001543struct compat_sioc_vif_req {
1544 vifi_t vifi; /* Which iface */
1545 compat_ulong_t icount;
1546 compat_ulong_t ocount;
1547 compat_ulong_t ibytes;
1548 compat_ulong_t obytes;
1549};
1550
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001551int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1552{
David S. Miller0033d5a2011-02-03 17:21:31 -08001553 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001554 struct compat_sioc_vif_req vr;
1555 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001556 struct mfc_cache *c;
1557 struct net *net = sock_net(sk);
1558 struct mr_table *mrt;
1559
1560 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001561 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001562 return -ENOENT;
1563
1564 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001565 case SIOCGETVIFCNT:
1566 if (copy_from_user(&vr, arg, sizeof(vr)))
1567 return -EFAULT;
1568 if (vr.vifi >= mrt->maxvif)
1569 return -EINVAL;
1570 read_lock(&mrt_lock);
1571 vif = &mrt->vif_table[vr.vifi];
1572 if (VIF_EXISTS(mrt, vr.vifi)) {
1573 vr.icount = vif->pkt_in;
1574 vr.ocount = vif->pkt_out;
1575 vr.ibytes = vif->bytes_in;
1576 vr.obytes = vif->bytes_out;
1577 read_unlock(&mrt_lock);
1578
1579 if (copy_to_user(arg, &vr, sizeof(vr)))
1580 return -EFAULT;
1581 return 0;
1582 }
1583 read_unlock(&mrt_lock);
1584 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001585 case SIOCGETSGCNT:
1586 if (copy_from_user(&sr, arg, sizeof(sr)))
1587 return -EFAULT;
1588
1589 rcu_read_lock();
1590 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1591 if (c) {
1592 sr.pktcnt = c->mfc_un.res.pkt;
1593 sr.bytecnt = c->mfc_un.res.bytes;
1594 sr.wrong_if = c->mfc_un.res.wrong_if;
1595 rcu_read_unlock();
1596
1597 if (copy_to_user(arg, &sr, sizeof(sr)))
1598 return -EFAULT;
1599 return 0;
1600 }
1601 rcu_read_unlock();
1602 return -EADDRNOTAVAIL;
1603 default:
1604 return -ENOIOCTLCMD;
1605 }
1606}
1607#endif
1608
Linus Torvalds1da177e2005-04-16 15:20:36 -07001609static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1610{
Jiri Pirko351638e2013-05-28 01:30:21 +00001611 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001612 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001613 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614 struct vif_device *v;
1615 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001616
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 if (event != NETDEV_UNREGISTER)
1618 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001619
1620 ipmr_for_each_table(mrt, net) {
1621 v = &mrt->vif_table[0];
1622 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1623 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001624 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 }
1627 return NOTIFY_DONE;
1628}
1629
Jianjun Kongc354e122008-11-03 00:28:02 -08001630static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 .notifier_call = ipmr_device_event,
1632};
1633
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001634/* Encapsulate a packet by attaching a valid IPIP header to it.
1635 * This avoids tunnel drivers and other mess and gives us the speed so
1636 * important for multicast video.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 */
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001638static void ip_encap(struct net *net, struct sk_buff *skb,
1639 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001641 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001642 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001643
1644 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001645 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001646 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001647 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001649 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001650 iph->tos = old_iph->tos;
1651 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 iph->frag_off = 0;
1653 iph->daddr = daddr;
1654 iph->saddr = saddr;
1655 iph->protocol = IPPROTO_IPIP;
1656 iph->ihl = 5;
1657 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001658 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 ip_send_check(iph);
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1662 nf_reset(skb);
1663}
1664
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001665static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1666 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001668 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
David S. Miller73186df2015-11-03 13:41:45 -05001670 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1671 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
1673 if (unlikely(opt->optlen))
1674 ip_forward_options(skb);
1675
Eric W. Biederman13206b62015-10-07 16:48:35 -05001676 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677}
1678
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001679/* Processing handlers for ipmr_forward */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680
Patrick McHardy0c122952010-04-13 05:03:22 +00001681static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1682 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001684 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001685 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 struct net_device *dev;
1687 struct rtable *rt;
David S. Miller31e4543d2011-05-03 20:25:42 -07001688 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 int encap = 0;
1690
Ian Morris51456b22015-04-03 09:17:26 +01001691 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 goto out_free;
1693
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 if (vif->flags & VIFF_REGISTER) {
1695 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001696 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001697 vif->dev->stats.tx_bytes += skb->len;
1698 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001699 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001700 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001703 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e4543d2011-05-03 20:25:42 -07001704 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001705 vif->remote, vif->local,
1706 0, 0,
1707 IPPROTO_IPIP,
1708 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001709 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 goto out_free;
1711 encap = sizeof(struct iphdr);
1712 } else {
David S. Miller31e4543d2011-05-03 20:25:42 -07001713 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001714 0, 0,
1715 IPPROTO_IPIP,
1716 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001717 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 goto out_free;
1719 }
1720
Changli Gaod8d1f302010-06-10 23:31:35 -07001721 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722
Changli Gaod8d1f302010-06-10 23:31:35 -07001723 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001725 * allow to send ICMP, so that packets will disappear
1726 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 */
David S. Miller73186df2015-11-03 13:41:45 -05001728 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 ip_rt_put(rt);
1730 goto out_free;
1731 }
1732
Changli Gaod8d1f302010-06-10 23:31:35 -07001733 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734
1735 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001736 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 goto out_free;
1738 }
1739
1740 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001741 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742
Eric Dumazetadf30902009-06-02 05:19:30 +00001743 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001744 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001745 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001748 * What do we do with netfilter? -- RR
1749 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001751 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001753 vif->dev->stats.tx_packets++;
1754 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756
Lance Richardson9ee6c5d2016-11-02 16:36:17 -04001757 IPCB(skb)->flags |= IPSKB_FORWARDED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001759 /* RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 * not only before forwarding, but after forwarding on all output
1761 * interfaces. It is clear, if mrouter runs a multicasting
1762 * program, it should receive packets not depending to what interface
1763 * program is joined.
1764 * If we will not make it, the program will have to join on all
1765 * interfaces. On the other hand, multihoming host (or router, but
1766 * not mrouter) cannot join to more than one interface - it will
1767 * result in receiving multiple packets.
1768 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001769 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1770 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 ipmr_forward_finish);
1772 return;
1773
1774out_free:
1775 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776}
1777
Patrick McHardy0c122952010-04-13 05:03:22 +00001778static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779{
1780 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001781
1782 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1783 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 break;
1785 }
1786 return ct;
1787}
1788
1789/* "local" means that we should preserve one skb (for local delivery) */
Rami Rosenc4854ec2013-07-20 15:09:28 +03001790static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1791 struct sk_buff *skb, struct mfc_cache *cache,
1792 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
1794 int psend = -1;
1795 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001796 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 vif = cache->mfc_parent;
1799 cache->mfc_un.res.pkt++;
1800 cache->mfc_un.res.bytes += skb->len;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03001801 cache->mfc_un.res.lastuse = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001803 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001804 struct mfc_cache *cache_proxy;
1805
1806 /* For an (*,G) entry, we only check that the incomming
1807 * interface is part of the static tree.
1808 */
1809 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1810 if (cache_proxy &&
1811 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1812 goto forward;
1813 }
1814
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001815 /* Wrong interface: drop packet and (maybe) send PIM assert. */
Patrick McHardy0c122952010-04-13 05:03:22 +00001816 if (mrt->vif_table[vif].dev != skb->dev) {
David Aherne58e4152016-10-31 15:54:00 -07001817 struct net_device *mdev;
1818
1819 mdev = l3mdev_master_dev_rcu(mrt->vif_table[vif].dev);
1820 if (mdev == skb->dev)
1821 goto forward;
1822
David S. Millerc7537962010-11-11 17:07:48 -08001823 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001825 * Very complicated situation...
1826 *
1827 * The best workaround until routing daemons will be
1828 * fixed is not to redistribute packet, if it was
1829 * send through wrong interface. It means, that
1830 * multicast applications WILL NOT work for
1831 * (S,G), which have default multicast route pointing
1832 * to wrong oif. In any case, it is not a good
1833 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 */
1835 goto dont_forward;
1836 }
1837
1838 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
Patrick McHardy0c122952010-04-13 05:03:22 +00001840 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001842 * so that we cannot check that packet arrived on an oif.
1843 * It is bad, but otherwise we would need to move pretty
1844 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001846 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001847 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001848 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1850 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001851 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 }
1853 goto dont_forward;
1854 }
1855
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001856forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001857 mrt->vif_table[vif].pkt_in++;
1858 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001860 /* Forward the frame */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001861 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1862 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001863 if (true_vifi >= 0 &&
1864 true_vifi != cache->mfc_parent &&
1865 ip_hdr(skb)->ttl >
1866 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1867 /* It's an (*,*) entry and the packet is not coming from
1868 * the upstream: forward the packet to the upstream
1869 * only.
1870 */
1871 psend = cache->mfc_parent;
1872 goto last_forward;
1873 }
1874 goto dont_forward;
1875 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001876 for (ct = cache->mfc_un.res.maxvif - 1;
1877 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001878 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001879 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1880 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001881 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 if (psend != -1) {
1883 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001886 ipmr_queue_xmit(net, mrt, skb2, cache,
1887 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001889 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 }
1891 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001892last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001893 if (psend != -1) {
1894 if (local) {
1895 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001896
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001898 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001900 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001901 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 }
1903 }
1904
1905dont_forward:
1906 if (!local)
1907 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908}
1909
David S. Miller417da662011-05-03 19:42:43 -07001910static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001911{
David S. Miller417da662011-05-03 19:42:43 -07001912 struct rtable *rt = skb_rtable(skb);
1913 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001914 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001915 .daddr = iph->daddr,
1916 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001917 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001918 .flowi4_oif = (rt_is_output_route(rt) ?
1919 skb->dev->ifindex : 0),
1920 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001921 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001922 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001923 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001924 };
1925 struct mr_table *mrt;
1926 int err;
1927
David S. Millerda919812011-03-12 02:04:50 -05001928 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001929 if (err)
1930 return ERR_PTR(err);
1931 return mrt;
1932}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001934/* Multicast packets for forwarding arrive here
1935 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937int ip_mr_input(struct sk_buff *skb)
1938{
1939 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001940 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001941 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001942 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943
1944 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001945 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001947 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 goto dont_forward;
1949
David S. Miller417da662011-05-03 19:42:43 -07001950 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001951 if (IS_ERR(mrt)) {
1952 kfree_skb(skb);
1953 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001954 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001956 if (IPCB(skb)->opt.router_alert) {
1957 if (ip_call_ra_chain(skb))
1958 return 0;
1959 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1960 /* IGMPv1 (and broken IGMPv2 implementations sort of
1961 * Cisco IOS <= 11.2(8)) do not put router alert
1962 * option to IGMP packets destined to routable
1963 * groups. It is very bad, because it means
1964 * that we can forward NO IGMP messages.
1965 */
1966 struct sock *mroute_sk;
1967
1968 mroute_sk = rcu_dereference(mrt->mroute_sk);
1969 if (mroute_sk) {
1970 nf_reset(skb);
1971 raw_rcv(mroute_sk, skb);
1972 return 0;
1973 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 }
1975 }
1976
Eric Dumazeta8c94862010-10-01 16:15:08 +00001977 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001978 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01001979 if (!cache) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001980 int vif = ipmr_find_vif(mrt, skb->dev);
1981
1982 if (vif >= 0)
1983 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1984 vif);
1985 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01001987 /* No usable cache entry */
Ian Morris51456b22015-04-03 09:17:26 +01001988 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 int vif;
1990
1991 if (local) {
1992 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
1993 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01001994 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 skb = skb2;
1997 }
1998
Eric Dumazeta8c94862010-10-01 16:15:08 +00001999 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002000 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002002 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 read_unlock(&mrt_lock);
2004
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002005 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 }
2007 read_unlock(&mrt_lock);
2008 kfree_skb(skb);
2009 return -ENODEV;
2010 }
2011
Eric Dumazeta8c94862010-10-01 16:15:08 +00002012 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002013 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 read_unlock(&mrt_lock);
2015
2016 if (local)
2017 return ip_local_deliver(skb);
2018
2019 return 0;
2020
2021dont_forward:
2022 if (local)
2023 return ip_local_deliver(skb);
2024 kfree_skb(skb);
2025 return 0;
2026}
2027
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002028#ifdef CONFIG_IP_PIMSM_V1
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002029/* Handle IGMP messages of PIMv1 */
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002030int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002031{
2032 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002033 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002034 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002035
2036 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2037 goto drop;
2038
2039 pim = igmp_hdr(skb);
2040
David S. Miller417da662011-05-03 19:42:43 -07002041 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002042 if (IS_ERR(mrt))
2043 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002044 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002045 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2046 goto drop;
2047
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002048 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002049drop:
2050 kfree_skb(skb);
2051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 return 0;
2053}
2054#endif
2055
2056#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002057static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058{
2059 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002060 struct net *net = dev_net(skb->dev);
2061 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002063 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 goto drop;
2065
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002066 pim = (struct pimreghdr *)skb_transport_header(skb);
Nikolay Aleksandrov56245ca2016-10-31 13:21:04 +01002067 if (pim->type != ((PIM_VERSION << 4) | (PIM_TYPE_REGISTER)) ||
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002068 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002069 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002070 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 goto drop;
2072
David S. Miller417da662011-05-03 19:42:43 -07002073 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002074 if (IS_ERR(mrt))
2075 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002076 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002077drop:
2078 kfree_skb(skb);
2079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return 0;
2081}
2082#endif
2083
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002084static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2085 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086{
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002087 struct rta_mfc_stats mfcs;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002088 struct nlattr *mp_attr;
2089 struct rtnexthop *nhp;
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002090 unsigned long lastuse;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002091 int ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
Nicolas Dichtel74381892010-03-25 23:45:35 +00002093 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002094 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002095 return -ENOENT;
2096
Thomas Graf92a395e2012-06-26 23:36:13 +00002097 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2098 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2099 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100
Thomas Graf92a395e2012-06-26 23:36:13 +00002101 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2102 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
2104 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002105 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002106 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2107 nla_nest_cancel(skb, mp_attr);
2108 return -EMSGSIZE;
2109 }
2110
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 nhp->rtnh_flags = 0;
2112 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002113 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 nhp->rtnh_len = sizeof(*nhp);
2115 }
2116 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002117
2118 nla_nest_end(skb, mp_attr);
2119
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002120 lastuse = READ_ONCE(c->mfc_un.res.lastuse);
2121 lastuse = time_after_eq(jiffies, lastuse) ? jiffies - lastuse : 0;
2122
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002123 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2124 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2125 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002126 if (nla_put_64bit(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs, RTA_PAD) ||
Nikolay Aleksandrovb5036cd2016-09-20 16:17:22 +02002127 nla_put_u64_64bit(skb, RTA_EXPIRES, jiffies_to_clock_t(lastuse),
Nikolay Aleksandrov43b9e122016-07-14 19:28:27 +03002128 RTA_PAD))
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002129 return -EMSGSIZE;
2130
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 rtm->rtm_type = RTN_MULTICAST;
2132 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133}
2134
David S. Miller9a1b9492011-05-04 12:18:54 -07002135int ipmr_get_route(struct net *net, struct sk_buff *skb,
2136 __be32 saddr, __be32 daddr,
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002137 struct rtmsg *rtm, int nowait, u32 portid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002140 struct mr_table *mrt;
2141 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002143 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002144 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002145 return -ENOENT;
2146
Eric Dumazeta8c94862010-10-01 16:15:08 +00002147 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002148 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002149 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002150 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002152 if (vif >= 0)
2153 cache = ipmr_cache_find_any(mrt, daddr, vif);
2154 }
Ian Morris51456b22015-04-03 09:17:26 +01002155 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002156 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002157 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002159 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002160
2161 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002162 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 return -EAGAIN;
2164 }
2165
2166 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002167 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002168 if (dev)
2169 vif = ipmr_find_vif(mrt, dev);
2170 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002172 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173 return -ENODEV;
2174 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002175 skb2 = skb_clone(skb, GFP_ATOMIC);
2176 if (!skb2) {
2177 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002178 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002179 return -ENOMEM;
2180 }
2181
Nikolay Aleksandrov2cf75072016-09-25 23:08:31 +02002182 NETLINK_CB(skb2).portid = portid;
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002183 skb_push(skb2, sizeof(struct iphdr));
2184 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002185 iph = ip_hdr(skb2);
2186 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002187 iph->saddr = saddr;
2188 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002189 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002190 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002191 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002192 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 return err;
2194 }
2195
Eric Dumazeta8c94862010-10-01 16:15:08 +00002196 read_lock(&mrt_lock);
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002197 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002199 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200 return err;
2201}
2202
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002203static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002204 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2205 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002206{
2207 struct nlmsghdr *nlh;
2208 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002209 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002210
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002211 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002212 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002213 return -EMSGSIZE;
2214
2215 rtm = nlmsg_data(nlh);
2216 rtm->rtm_family = RTNL_FAMILY_IPMR;
2217 rtm->rtm_dst_len = 32;
2218 rtm->rtm_src_len = 32;
2219 rtm->rtm_tos = 0;
2220 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002221 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2222 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002223 rtm->rtm_type = RTN_MULTICAST;
2224 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002225 if (c->mfc_flags & MFC_STATIC)
2226 rtm->rtm_protocol = RTPROT_STATIC;
2227 else
2228 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002229 rtm->rtm_flags = 0;
2230
Jiri Benc930345e2015-03-29 16:59:25 +02002231 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2232 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002233 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002234 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2235 /* do not break the dump if cache is unresolved */
2236 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002237 goto nla_put_failure;
2238
Johannes Berg053c0952015-01-16 22:09:00 +01002239 nlmsg_end(skb, nlh);
2240 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002241
2242nla_put_failure:
2243 nlmsg_cancel(skb, nlh);
2244 return -EMSGSIZE;
2245}
2246
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002247static size_t mroute_msgsize(bool unresolved, int maxvif)
2248{
2249 size_t len =
2250 NLMSG_ALIGN(sizeof(struct rtmsg))
2251 + nla_total_size(4) /* RTA_TABLE */
2252 + nla_total_size(4) /* RTA_SRC */
2253 + nla_total_size(4) /* RTA_DST */
2254 ;
2255
2256 if (!unresolved)
2257 len = len
2258 + nla_total_size(4) /* RTA_IIF */
2259 + nla_total_size(0) /* RTA_MULTIPATH */
2260 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2261 /* RTA_MFC_STATS */
Nicolas Dichtela9a08042016-04-21 18:58:26 +02002262 + nla_total_size_64bit(sizeof(struct rta_mfc_stats))
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002263 ;
2264
2265 return len;
2266}
2267
2268static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2269 int cmd)
2270{
2271 struct net *net = read_pnet(&mrt->net);
2272 struct sk_buff *skb;
2273 int err = -ENOBUFS;
2274
2275 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2276 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002277 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002278 goto errout;
2279
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002280 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002281 if (err < 0)
2282 goto errout;
2283
2284 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2285 return;
2286
2287errout:
2288 kfree_skb(skb);
2289 if (err < 0)
2290 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2291}
2292
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002293static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2294{
2295 struct net *net = sock_net(skb->sk);
2296 struct mr_table *mrt;
2297 struct mfc_cache *mfc;
2298 unsigned int t = 0, s_t;
2299 unsigned int h = 0, s_h;
2300 unsigned int e = 0, s_e;
2301
2302 s_t = cb->args[0];
2303 s_h = cb->args[1];
2304 s_e = cb->args[2];
2305
Eric Dumazeta8c94862010-10-01 16:15:08 +00002306 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002307 ipmr_for_each_table(mrt, net) {
2308 if (t < s_t)
2309 goto next_table;
2310 if (t > s_t)
2311 s_h = 0;
2312 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002313 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002314 if (e < s_e)
2315 goto next_entry;
2316 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002317 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002318 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002319 mfc, RTM_NEWROUTE,
2320 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002321 goto done;
2322next_entry:
2323 e++;
2324 }
2325 e = s_e = 0;
2326 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002327 spin_lock_bh(&mfc_unres_lock);
2328 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2329 if (e < s_e)
2330 goto next_entry2;
2331 if (ipmr_fill_mroute(mrt, skb,
2332 NETLINK_CB(cb->skb).portid,
2333 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002334 mfc, RTM_NEWROUTE,
2335 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002336 spin_unlock_bh(&mfc_unres_lock);
2337 goto done;
2338 }
2339next_entry2:
2340 e++;
2341 }
2342 spin_unlock_bh(&mfc_unres_lock);
2343 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002344 s_h = 0;
2345next_table:
2346 t++;
2347 }
2348done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002349 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002350
2351 cb->args[2] = e;
2352 cb->args[1] = h;
2353 cb->args[0] = t;
2354
2355 return skb->len;
2356}
2357
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002358static const struct nla_policy rtm_ipmr_policy[RTA_MAX + 1] = {
2359 [RTA_SRC] = { .type = NLA_U32 },
2360 [RTA_DST] = { .type = NLA_U32 },
2361 [RTA_IIF] = { .type = NLA_U32 },
2362 [RTA_TABLE] = { .type = NLA_U32 },
2363 [RTA_MULTIPATH] = { .len = sizeof(struct rtnexthop) },
2364};
2365
2366static bool ipmr_rtm_validate_proto(unsigned char rtm_protocol)
2367{
2368 switch (rtm_protocol) {
2369 case RTPROT_STATIC:
2370 case RTPROT_MROUTED:
2371 return true;
2372 }
2373 return false;
2374}
2375
2376static int ipmr_nla_get_ttls(const struct nlattr *nla, struct mfcctl *mfcc)
2377{
2378 struct rtnexthop *rtnh = nla_data(nla);
2379 int remaining = nla_len(nla), vifi = 0;
2380
2381 while (rtnh_ok(rtnh, remaining)) {
2382 mfcc->mfcc_ttls[vifi] = rtnh->rtnh_hops;
2383 if (++vifi == MAXVIFS)
2384 break;
2385 rtnh = rtnh_next(rtnh, &remaining);
2386 }
2387
2388 return remaining > 0 ? -EINVAL : vifi;
2389}
2390
2391/* returns < 0 on error, 0 for ADD_MFC and 1 for ADD_MFC_PROXY */
2392static int rtm_to_ipmr_mfcc(struct net *net, struct nlmsghdr *nlh,
2393 struct mfcctl *mfcc, int *mrtsock,
2394 struct mr_table **mrtret)
2395{
2396 struct net_device *dev = NULL;
2397 u32 tblid = RT_TABLE_DEFAULT;
2398 struct mr_table *mrt;
2399 struct nlattr *attr;
2400 struct rtmsg *rtm;
2401 int ret, rem;
2402
2403 ret = nlmsg_validate(nlh, sizeof(*rtm), RTA_MAX, rtm_ipmr_policy);
2404 if (ret < 0)
2405 goto out;
2406 rtm = nlmsg_data(nlh);
2407
2408 ret = -EINVAL;
2409 if (rtm->rtm_family != RTNL_FAMILY_IPMR || rtm->rtm_dst_len != 32 ||
2410 rtm->rtm_type != RTN_MULTICAST ||
2411 rtm->rtm_scope != RT_SCOPE_UNIVERSE ||
2412 !ipmr_rtm_validate_proto(rtm->rtm_protocol))
2413 goto out;
2414
2415 memset(mfcc, 0, sizeof(*mfcc));
2416 mfcc->mfcc_parent = -1;
2417 ret = 0;
2418 nlmsg_for_each_attr(attr, nlh, sizeof(struct rtmsg), rem) {
2419 switch (nla_type(attr)) {
2420 case RTA_SRC:
2421 mfcc->mfcc_origin.s_addr = nla_get_be32(attr);
2422 break;
2423 case RTA_DST:
2424 mfcc->mfcc_mcastgrp.s_addr = nla_get_be32(attr);
2425 break;
2426 case RTA_IIF:
2427 dev = __dev_get_by_index(net, nla_get_u32(attr));
2428 if (!dev) {
2429 ret = -ENODEV;
2430 goto out;
2431 }
2432 break;
2433 case RTA_MULTIPATH:
2434 if (ipmr_nla_get_ttls(attr, mfcc) < 0) {
2435 ret = -EINVAL;
2436 goto out;
2437 }
2438 break;
2439 case RTA_PREFSRC:
2440 ret = 1;
2441 break;
2442 case RTA_TABLE:
2443 tblid = nla_get_u32(attr);
2444 break;
2445 }
2446 }
2447 mrt = ipmr_get_table(net, tblid);
2448 if (!mrt) {
2449 ret = -ENOENT;
2450 goto out;
2451 }
2452 *mrtret = mrt;
2453 *mrtsock = rtm->rtm_protocol == RTPROT_MROUTED ? 1 : 0;
2454 if (dev)
2455 mfcc->mfcc_parent = ipmr_find_vif(mrt, dev);
2456
2457out:
2458 return ret;
2459}
2460
2461/* takes care of both newroute and delroute */
2462static int ipmr_rtm_route(struct sk_buff *skb, struct nlmsghdr *nlh)
2463{
2464 struct net *net = sock_net(skb->sk);
2465 int ret, mrtsock, parent;
2466 struct mr_table *tbl;
2467 struct mfcctl mfcc;
2468
2469 mrtsock = 0;
2470 tbl = NULL;
2471 ret = rtm_to_ipmr_mfcc(net, nlh, &mfcc, &mrtsock, &tbl);
2472 if (ret < 0)
2473 return ret;
2474
2475 parent = ret ? mfcc.mfcc_parent : -1;
2476 if (nlh->nlmsg_type == RTM_NEWROUTE)
2477 return ipmr_mfc_add(net, tbl, &mfcc, mrtsock, parent);
2478 else
2479 return ipmr_mfc_delete(tbl, &mfcc, parent);
2480}
2481
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002482#ifdef CONFIG_PROC_FS
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002483/* The /proc interfaces to multicast routing :
2484 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002485 */
2486struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002487 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002488 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489 int ct;
2490};
2491
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002492static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2493 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494 loff_t pos)
2495{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002496 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002497
2498 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2499 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002501 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002502 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 }
2504 return NULL;
2505}
2506
2507static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002508 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002510 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002511 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002512 struct mr_table *mrt;
2513
2514 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002515 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002516 return ERR_PTR(-ENOENT);
2517
2518 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002519
Linus Torvalds1da177e2005-04-16 15:20:36 -07002520 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002521 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002522 : SEQ_START_TOKEN;
2523}
2524
2525static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2526{
2527 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002528 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002529 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
2531 ++*pos;
2532 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002533 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002534
Patrick McHardy0c122952010-04-13 05:03:22 +00002535 while (++iter->ct < mrt->maxvif) {
2536 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002538 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539 }
2540 return NULL;
2541}
2542
2543static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002544 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545{
2546 read_unlock(&mrt_lock);
2547}
2548
2549static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2550{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002551 struct ipmr_vif_iter *iter = seq->private;
2552 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002555 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2557 } else {
2558 const struct vif_device *vif = v;
2559 const char *name = vif->dev ? vif->dev->name : "none";
2560
2561 seq_printf(seq,
2562 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002563 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002564 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 vif->bytes_out, vif->pkt_out,
2566 vif->flags, vif->local, vif->remote);
2567 }
2568 return 0;
2569}
2570
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002571static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 .start = ipmr_vif_seq_start,
2573 .next = ipmr_vif_seq_next,
2574 .stop = ipmr_vif_seq_stop,
2575 .show = ipmr_vif_seq_show,
2576};
2577
2578static int ipmr_vif_open(struct inode *inode, struct file *file)
2579{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002580 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2581 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002582}
2583
Arjan van de Ven9a321442007-02-12 00:55:35 -08002584static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585 .owner = THIS_MODULE,
2586 .open = ipmr_vif_open,
2587 .read = seq_read,
2588 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002589 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002590};
2591
2592struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002593 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002594 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002595 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 int ct;
2597};
2598
2599
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002600static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2601 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002603 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604 struct mfc_cache *mfc;
2605
Eric Dumazeta8c94862010-10-01 16:15:08 +00002606 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002607 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002608 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002609 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002610 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002612 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002613 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002616 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002617 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002618 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002619 return mfc;
2620 spin_unlock_bh(&mfc_unres_lock);
2621
2622 it->cache = NULL;
2623 return NULL;
2624}
2625
2626
2627static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2628{
2629 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002630 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002631 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002632
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002633 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002634 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002635 return ERR_PTR(-ENOENT);
2636
2637 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 it->cache = NULL;
2639 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002640 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 : SEQ_START_TOKEN;
2642}
2643
2644static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2645{
2646 struct mfc_cache *mfc = v;
2647 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002648 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002649 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
2651 ++*pos;
2652
2653 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002654 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655
Patrick McHardy862465f2010-04-13 05:03:21 +00002656 if (mfc->list.next != it->cache)
2657 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002658
Patrick McHardy0c122952010-04-13 05:03:22 +00002659 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 goto end_of_list;
2661
Patrick McHardy0c122952010-04-13 05:03:22 +00002662 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663
2664 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002665 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002666 if (list_empty(it->cache))
2667 continue;
2668 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002669 }
2670
2671 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002672 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002673 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002675
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002677 if (!list_empty(it->cache))
2678 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002680end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 spin_unlock_bh(&mfc_unres_lock);
2682 it->cache = NULL;
2683
2684 return NULL;
2685}
2686
2687static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2688{
2689 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002690 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691
Patrick McHardy0c122952010-04-13 05:03:22 +00002692 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002694 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002695 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}
2697
2698static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2699{
2700 int n;
2701
2702 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002703 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2705 } else {
2706 const struct mfc_cache *mfc = v;
2707 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002708 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002709
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002710 seq_printf(seq, "%08X %08X %-3hd",
2711 (__force u32) mfc->mfc_mcastgrp,
2712 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002713 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714
Patrick McHardy0c122952010-04-13 05:03:22 +00002715 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002716 seq_printf(seq, " %8lu %8lu %8lu",
2717 mfc->mfc_un.res.pkt,
2718 mfc->mfc_un.res.bytes,
2719 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002720 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002721 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002722 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002723 mfc->mfc_un.res.ttls[n] < 255)
2724 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002725 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002726 n, mfc->mfc_un.res.ttls[n]);
2727 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002728 } else {
2729 /* unresolved mfc_caches don't contain
2730 * pkt, bytes and wrong_if values
2731 */
2732 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 }
2734 seq_putc(seq, '\n');
2735 }
2736 return 0;
2737}
2738
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002739static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 .start = ipmr_mfc_seq_start,
2741 .next = ipmr_mfc_seq_next,
2742 .stop = ipmr_mfc_seq_stop,
2743 .show = ipmr_mfc_seq_show,
2744};
2745
2746static int ipmr_mfc_open(struct inode *inode, struct file *file)
2747{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002748 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2749 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002750}
2751
Arjan van de Ven9a321442007-02-12 00:55:35 -08002752static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 .owner = THIS_MODULE,
2754 .open = ipmr_mfc_open,
2755 .read = seq_read,
2756 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002757 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002759#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002760
2761#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002762static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002764 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002765};
2766#endif
2767
Nikolay Aleksandrov7ef8f652015-11-21 15:57:27 +01002768/* Setup for IP multicast routing */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002769static int __net_init ipmr_net_init(struct net *net)
2770{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002771 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002772
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002773 err = ipmr_rules_init(net);
2774 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002775 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002776
2777#ifdef CONFIG_PROC_FS
2778 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002779 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002780 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002781 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002782 goto proc_cache_fail;
2783#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002784 return 0;
2785
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002786#ifdef CONFIG_PROC_FS
2787proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002788 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002789proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002790 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002791#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002792fail:
2793 return err;
2794}
2795
2796static void __net_exit ipmr_net_exit(struct net *net)
2797{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002798#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002799 remove_proc_entry("ip_mr_cache", net->proc_net);
2800 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002801#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002802 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002803}
2804
2805static struct pernet_operations ipmr_net_ops = {
2806 .init = ipmr_net_init,
2807 .exit = ipmr_net_exit,
2808};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002809
Wang Chen03d2f892008-07-03 12:13:36 +08002810int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811{
Wang Chen03d2f892008-07-03 12:13:36 +08002812 int err;
2813
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2815 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002816 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002817 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002818
Benjamin Therycf958ae32009-01-22 04:56:16 +00002819 err = register_pernet_subsys(&ipmr_net_ops);
2820 if (err)
2821 goto reg_pernet_fail;
2822
Wang Chen03d2f892008-07-03 12:13:36 +08002823 err = register_netdevice_notifier(&ip_mr_notifier);
2824 if (err)
2825 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002826#ifdef CONFIG_IP_PIMSM_V2
2827 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002828 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002829 err = -EAGAIN;
2830 goto add_proto_fail;
2831 }
2832#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002833 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2834 NULL, ipmr_rtm_dumproute, NULL);
Nikolay Aleksandrovccbb0aa2015-11-26 15:23:50 +01002835 rtnl_register(RTNL_FAMILY_IPMR, RTM_NEWROUTE,
2836 ipmr_rtm_route, NULL, NULL);
2837 rtnl_register(RTNL_FAMILY_IPMR, RTM_DELROUTE,
2838 ipmr_rtm_route, NULL, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002839 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002840
Tom Goff403dbb92009-06-14 03:16:13 -07002841#ifdef CONFIG_IP_PIMSM_V2
2842add_proto_fail:
2843 unregister_netdevice_notifier(&ip_mr_notifier);
2844#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002845reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002846 unregister_pernet_subsys(&ipmr_net_ops);
2847reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002848 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002849 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850}