blob: c3a38353f5dc8094de5c1dcec06ae54ab0b29a9e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IP multicast routing support for mrouted 3.6/3.8
3 *
Alan Cox113aa832008-10-13 19:01:08 -07004 * (c) 1995 Alan Cox, <alan@lxorguk.ukuu.org.uk>
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * Linux Consultancy and Custom Driver Development
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
11 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * Fixes:
13 * Michael Chastain : Incorrect size of copying.
14 * Alan Cox : Added the cache manager code
15 * Alan Cox : Fixed the clone/copy bug and device race.
16 * Mike McLagan : Routing by source
17 * Malcolm Beattie : Buffer handling fixes.
18 * Alexey Kuznetsov : Double buffer free and other fixes.
19 * SVR Anand : Fixed several multicast bugs and problems.
20 * Alexey Kuznetsov : Status, optimisations and more.
21 * Brad Parker : Better behaviour on mrouted upcall
22 * overflow.
23 * Carlos Picoto : PIMv1 Support
24 * Pavlin Ivanov Radoslavov: PIMv2 Registers must checksum only PIM header
Gilles Espinassef77f13e2010-03-29 15:41:47 +020025 * Relax this requirement to work with older peers.
Linus Torvalds1da177e2005-04-16 15:20:36 -070026 *
27 */
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <asm/uaccess.h>
30#include <linux/types.h>
Randy Dunlap4fc268d2006-01-11 12:17:47 -080031#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/errno.h>
33#include <linux/timer.h>
34#include <linux/mm.h>
35#include <linux/kernel.h>
36#include <linux/fcntl.h>
37#include <linux/stat.h>
38#include <linux/socket.h>
39#include <linux/in.h>
40#include <linux/inet.h>
41#include <linux/netdevice.h>
42#include <linux/inetdevice.h>
43#include <linux/igmp.h>
44#include <linux/proc_fs.h>
45#include <linux/seq_file.h>
46#include <linux/mroute.h>
47#include <linux/init.h>
Kris Katterjohn46f25df2006-01-05 16:35:42 -080048#include <linux/if_ether.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090049#include <linux/slab.h>
Eric W. Biederman457c4cb2007-09-12 12:01:34 +020050#include <net/net_namespace.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <net/ip.h>
52#include <net/protocol.h>
53#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020054#include <net/route.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <net/sock.h>
56#include <net/icmp.h>
57#include <net/udp.h>
58#include <net/raw.h>
59#include <linux/notifier.h>
60#include <linux/if_arp.h>
61#include <linux/netfilter_ipv4.h>
Eric W. Biederman709b46e2011-01-29 16:15:56 +000062#include <linux/compat.h>
Paul Gortmakerbc3b2d72011-07-15 11:47:34 -040063#include <linux/export.h>
Pravin B Shelarc5441932013-03-25 14:49:35 +000064#include <net/ip_tunnels.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#include <net/checksum.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070066#include <net/netlink.h>
Patrick McHardyf0ad0862010-04-13 05:03:23 +000067#include <net/fib_rules.h>
Nicolas Dichteld67b8c62012-12-04 01:13:35 +000068#include <linux/netconf.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
70#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
71#define CONFIG_IP_PIMSM 1
72#endif
73
Patrick McHardy0c122952010-04-13 05:03:22 +000074struct mr_table {
Patrick McHardyf0ad0862010-04-13 05:03:23 +000075 struct list_head list;
Eric W. Biederman0c5c9fb2015-03-11 23:06:44 -050076 possible_net_t net;
Patrick McHardyf0ad0862010-04-13 05:03:23 +000077 u32 id;
Eric Dumazet4c968702010-10-01 16:15:01 +000078 struct sock __rcu *mroute_sk;
Patrick McHardy0c122952010-04-13 05:03:22 +000079 struct timer_list ipmr_expire_timer;
80 struct list_head mfc_unres_queue;
81 struct list_head mfc_cache_array[MFC_LINES];
82 struct vif_device vif_table[MAXVIFS];
83 int maxvif;
84 atomic_t cache_resolve_queue_len;
Joe Perches53d68412012-11-25 09:35:30 +000085 bool mroute_do_assert;
86 bool mroute_do_pim;
Patrick McHardy0c122952010-04-13 05:03:22 +000087#if defined(CONFIG_IP_PIMSM_V1) || defined(CONFIG_IP_PIMSM_V2)
88 int mroute_reg_vif_num;
89#endif
90};
91
Patrick McHardyf0ad0862010-04-13 05:03:23 +000092struct ipmr_rule {
93 struct fib_rule common;
94};
95
96struct ipmr_result {
97 struct mr_table *mrt;
98};
99
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100/* Big lock, protecting vif table, mrt cache and mroute socket state.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000101 * Note that the changes are semaphored via rtnl_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 */
103
104static DEFINE_RWLOCK(mrt_lock);
105
106/*
107 * Multicast router control variables
108 */
109
Patrick McHardy0c122952010-04-13 05:03:22 +0000110#define VIF_EXISTS(_mrt, _idx) ((_mrt)->vif_table[_idx].dev != NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
112/* Special spinlock for queue of unresolved entries */
113static DEFINE_SPINLOCK(mfc_unres_lock);
114
115/* We return to original Alan's scheme. Hash table of resolved
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000116 * entries is changed only in process context and protected
117 * with weak lock mrt_lock. Queue of unresolved entries is protected
118 * with strong spinlock mfc_unres_lock.
119 *
120 * In this case data path is free of exclusive locks at all.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 */
122
Christoph Lametere18b8902006-12-06 20:33:20 -0800123static struct kmem_cache *mrt_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000125static struct mr_table *ipmr_new_table(struct net *net, u32 id);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000126static void ipmr_free_table(struct mr_table *mrt);
127
Rami Rosenc4854ec2013-07-20 15:09:28 +0300128static void ip_mr_forward(struct net *net, struct mr_table *mrt,
129 struct sk_buff *skb, struct mfc_cache *cache,
130 int local);
Patrick McHardy0c122952010-04-13 05:03:22 +0000131static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000132 struct sk_buff *pkt, vifi_t vifi, int assert);
Patrick McHardycb6a4e42010-04-26 16:02:08 +0200133static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
134 struct mfc_cache *c, struct rtmsg *rtm);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000135static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
136 int cmd);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100137static void mroute_clean_tables(struct mr_table *mrt, bool all);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000138static void ipmr_expire_process(unsigned long arg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000140#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
141#define ipmr_for_each_table(mrt, net) \
142 list_for_each_entry_rcu(mrt, &net->ipv4.mr_tables, list)
143
144static struct mr_table *ipmr_get_table(struct net *net, u32 id)
145{
146 struct mr_table *mrt;
147
148 ipmr_for_each_table(mrt, net) {
149 if (mrt->id == id)
150 return mrt;
151 }
152 return NULL;
153}
154
David S. Millerda919812011-03-12 02:04:50 -0500155static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000156 struct mr_table **mrt)
157{
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000158 int err;
Hannes Frederic Sowa95f4a452014-01-13 02:45:22 +0100159 struct ipmr_result res;
160 struct fib_lookup_arg arg = {
161 .result = &res,
162 .flags = FIB_LOOKUP_NOREF,
163 };
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000164
David S. Millerda919812011-03-12 02:04:50 -0500165 err = fib_rules_lookup(net->ipv4.mr_rules_ops,
166 flowi4_to_flowi(flp4), 0, &arg);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000167 if (err < 0)
168 return err;
169 *mrt = res.mrt;
170 return 0;
171}
172
173static int ipmr_rule_action(struct fib_rule *rule, struct flowi *flp,
174 int flags, struct fib_lookup_arg *arg)
175{
176 struct ipmr_result *res = arg->result;
177 struct mr_table *mrt;
178
179 switch (rule->action) {
180 case FR_ACT_TO_TBL:
181 break;
182 case FR_ACT_UNREACHABLE:
183 return -ENETUNREACH;
184 case FR_ACT_PROHIBIT:
185 return -EACCES;
186 case FR_ACT_BLACKHOLE:
187 default:
188 return -EINVAL;
189 }
190
191 mrt = ipmr_get_table(rule->fr_net, rule->table);
Ian Morris51456b22015-04-03 09:17:26 +0100192 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000193 return -EAGAIN;
194 res->mrt = mrt;
195 return 0;
196}
197
198static int ipmr_rule_match(struct fib_rule *rule, struct flowi *fl, int flags)
199{
200 return 1;
201}
202
203static const struct nla_policy ipmr_rule_policy[FRA_MAX + 1] = {
204 FRA_GENERIC_POLICY,
205};
206
207static int ipmr_rule_configure(struct fib_rule *rule, struct sk_buff *skb,
208 struct fib_rule_hdr *frh, struct nlattr **tb)
209{
210 return 0;
211}
212
213static int ipmr_rule_compare(struct fib_rule *rule, struct fib_rule_hdr *frh,
214 struct nlattr **tb)
215{
216 return 1;
217}
218
219static int ipmr_rule_fill(struct fib_rule *rule, struct sk_buff *skb,
220 struct fib_rule_hdr *frh)
221{
222 frh->dst_len = 0;
223 frh->src_len = 0;
224 frh->tos = 0;
225 return 0;
226}
227
Andi Kleen04a6f822012-10-04 17:12:11 -0700228static const struct fib_rules_ops __net_initconst ipmr_rules_ops_template = {
Patrick McHardy25239ce2010-04-26 16:02:05 +0200229 .family = RTNL_FAMILY_IPMR,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000230 .rule_size = sizeof(struct ipmr_rule),
231 .addr_size = sizeof(u32),
232 .action = ipmr_rule_action,
233 .match = ipmr_rule_match,
234 .configure = ipmr_rule_configure,
235 .compare = ipmr_rule_compare,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000236 .fill = ipmr_rule_fill,
237 .nlgroup = RTNLGRP_IPV4_RULE,
238 .policy = ipmr_rule_policy,
239 .owner = THIS_MODULE,
240};
241
242static int __net_init ipmr_rules_init(struct net *net)
243{
244 struct fib_rules_ops *ops;
245 struct mr_table *mrt;
246 int err;
247
248 ops = fib_rules_register(&ipmr_rules_ops_template, net);
249 if (IS_ERR(ops))
250 return PTR_ERR(ops);
251
252 INIT_LIST_HEAD(&net->ipv4.mr_tables);
253
254 mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +0100255 if (!mrt) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000256 err = -ENOMEM;
257 goto err1;
258 }
259
260 err = fib_default_rule_add(ops, 0x7fff, RT_TABLE_DEFAULT, 0);
261 if (err < 0)
262 goto err2;
263
264 net->ipv4.mr_rules_ops = ops;
265 return 0;
266
267err2:
WANG Congf243e5a2015-03-25 14:45:03 -0700268 ipmr_free_table(mrt);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000269err1:
270 fib_rules_unregister(ops);
271 return err;
272}
273
274static void __net_exit ipmr_rules_exit(struct net *net)
275{
276 struct mr_table *mrt, *next;
277
WANG Conged785302015-03-31 11:01:45 -0700278 rtnl_lock();
Eric Dumazet035320d2010-06-06 23:48:40 +0000279 list_for_each_entry_safe(mrt, next, &net->ipv4.mr_tables, list) {
280 list_del(&mrt->list);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000281 ipmr_free_table(mrt);
Eric Dumazet035320d2010-06-06 23:48:40 +0000282 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000283 fib_rules_unregister(net->ipv4.mr_rules_ops);
WANG Cong419df122015-03-31 11:01:46 -0700284 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000285}
286#else
287#define ipmr_for_each_table(mrt, net) \
288 for (mrt = net->ipv4.mrt; mrt; mrt = NULL)
289
290static struct mr_table *ipmr_get_table(struct net *net, u32 id)
291{
292 return net->ipv4.mrt;
293}
294
David S. Millerda919812011-03-12 02:04:50 -0500295static int ipmr_fib_lookup(struct net *net, struct flowi4 *flp4,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000296 struct mr_table **mrt)
297{
298 *mrt = net->ipv4.mrt;
299 return 0;
300}
301
302static int __net_init ipmr_rules_init(struct net *net)
303{
304 net->ipv4.mrt = ipmr_new_table(net, RT_TABLE_DEFAULT);
305 return net->ipv4.mrt ? 0 : -ENOMEM;
306}
307
308static void __net_exit ipmr_rules_exit(struct net *net)
309{
WANG Conged785302015-03-31 11:01:45 -0700310 rtnl_lock();
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000311 ipmr_free_table(net->ipv4.mrt);
WANG Conged785302015-03-31 11:01:45 -0700312 net->ipv4.mrt = NULL;
313 rtnl_unlock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000314}
315#endif
316
317static struct mr_table *ipmr_new_table(struct net *net, u32 id)
318{
319 struct mr_table *mrt;
320 unsigned int i;
321
322 mrt = ipmr_get_table(net, id);
Ian Morris00db4122015-04-03 09:17:27 +0100323 if (mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000324 return mrt;
325
326 mrt = kzalloc(sizeof(*mrt), GFP_KERNEL);
Ian Morris51456b22015-04-03 09:17:26 +0100327 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000328 return NULL;
Patrick McHardy8de53df2010-04-15 13:29:28 +0200329 write_pnet(&mrt->net, net);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000330 mrt->id = id;
331
332 /* Forwarding cache */
333 for (i = 0; i < MFC_LINES; i++)
334 INIT_LIST_HEAD(&mrt->mfc_cache_array[i]);
335
336 INIT_LIST_HEAD(&mrt->mfc_unres_queue);
337
338 setup_timer(&mrt->ipmr_expire_timer, ipmr_expire_process,
339 (unsigned long)mrt);
340
341#ifdef CONFIG_IP_PIMSM
342 mrt->mroute_reg_vif_num = -1;
343#endif
344#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
345 list_add_tail_rcu(&mrt->list, &net->ipv4.mr_tables);
346#endif
347 return mrt;
348}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000350static void ipmr_free_table(struct mr_table *mrt)
351{
352 del_timer_sync(&mrt->ipmr_expire_timer);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +0100353 mroute_clean_tables(mrt, true);
Francesco Ruggeriacbb2192012-08-24 07:38:35 +0000354 kfree(mrt);
355}
356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357/* Service routines creating virtual interfaces: DVMRP tunnels and PIMREG */
358
Wang Chend6070322008-07-14 20:55:26 -0700359static void ipmr_del_tunnel(struct net_device *dev, struct vifctl *v)
360{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000361 struct net *net = dev_net(dev);
362
Wang Chend6070322008-07-14 20:55:26 -0700363 dev_close(dev);
364
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000365 dev = __dev_get_by_name(net, "tunl0");
Wang Chend6070322008-07-14 20:55:26 -0700366 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800367 const struct net_device_ops *ops = dev->netdev_ops;
Wang Chend6070322008-07-14 20:55:26 -0700368 struct ifreq ifr;
Wang Chend6070322008-07-14 20:55:26 -0700369 struct ip_tunnel_parm p;
370
371 memset(&p, 0, sizeof(p));
372 p.iph.daddr = v->vifc_rmt_addr.s_addr;
373 p.iph.saddr = v->vifc_lcl_addr.s_addr;
374 p.iph.version = 4;
375 p.iph.ihl = 5;
376 p.iph.protocol = IPPROTO_IPIP;
377 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
378 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
379
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800380 if (ops->ndo_do_ioctl) {
381 mm_segment_t oldfs = get_fs();
382
383 set_fs(KERNEL_DS);
384 ops->ndo_do_ioctl(dev, &ifr, SIOCDELTUNNEL);
385 set_fs(oldfs);
386 }
Wang Chend6070322008-07-14 20:55:26 -0700387 }
388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390static
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000391struct net_device *ipmr_new_tunnel(struct net *net, struct vifctl *v)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392{
393 struct net_device *dev;
394
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000395 dev = __dev_get_by_name(net, "tunl0");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 if (dev) {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800398 const struct net_device_ops *ops = dev->netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 int err;
400 struct ifreq ifr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct ip_tunnel_parm p;
402 struct in_device *in_dev;
403
404 memset(&p, 0, sizeof(p));
405 p.iph.daddr = v->vifc_rmt_addr.s_addr;
406 p.iph.saddr = v->vifc_lcl_addr.s_addr;
407 p.iph.version = 4;
408 p.iph.ihl = 5;
409 p.iph.protocol = IPPROTO_IPIP;
410 sprintf(p.name, "dvmrp%d", v->vifc_vifi);
Stephen Hemmingerba93ef72008-01-21 17:28:59 -0800411 ifr.ifr_ifru.ifru_data = (__force void __user *)&p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800413 if (ops->ndo_do_ioctl) {
414 mm_segment_t oldfs = get_fs();
415
416 set_fs(KERNEL_DS);
417 err = ops->ndo_do_ioctl(dev, &ifr, SIOCADDTUNNEL);
418 set_fs(oldfs);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000419 } else {
Stephen Hemminger5bc3eb72008-11-19 21:52:05 -0800420 err = -EOPNOTSUPP;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 dev = NULL;
423
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000424 if (err == 0 &&
425 (dev = __dev_get_by_name(net, p.name)) != NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 dev->flags |= IFF_MULTICAST;
427
Herbert Xue5ed6392005-10-03 14:35:55 -0700428 in_dev = __in_dev_get_rtnl(dev);
Ian Morris51456b22015-04-03 09:17:26 +0100429 if (!in_dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700431
432 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100433 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700434 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 if (dev_open(dev))
437 goto failure;
Wang Chen7dc00c82008-07-14 20:56:34 -0700438 dev_hold(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 }
440 }
441 return dev;
442
443failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 unregister_netdevice(dev);
445 return NULL;
446}
447
448#ifdef CONFIG_IP_PIMSM
449
Stephen Hemminger6fef4c02009-08-31 19:50:41 +0000450static netdev_tx_t reg_vif_xmit(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451{
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000452 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000453 struct mr_table *mrt;
David S. Millerda919812011-03-12 02:04:50 -0500454 struct flowi4 fl4 = {
455 .flowi4_oif = dev->ifindex,
Cong Wang6a662712014-04-15 16:25:34 -0700456 .flowi4_iif = skb->skb_iif ? : LOOPBACK_IFINDEX,
David S. Millerda919812011-03-12 02:04:50 -0500457 .flowi4_mark = skb->mark,
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000458 };
459 int err;
460
David S. Millerda919812011-03-12 02:04:50 -0500461 err = ipmr_fib_lookup(net, &fl4, &mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +0000462 if (err < 0) {
463 kfree_skb(skb);
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000464 return err;
Ben Greeare40dbc52010-07-15 13:22:33 +0000465 }
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 read_lock(&mrt_lock);
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -0700468 dev->stats.tx_bytes += skb->len;
469 dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +0000470 ipmr_cache_report(mrt, skb, mrt->mroute_reg_vif_num, IGMPMSG_WHOLEPKT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 read_unlock(&mrt_lock);
472 kfree_skb(skb);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000473 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474}
475
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200476static int reg_vif_get_iflink(const struct net_device *dev)
477{
478 return 0;
479}
480
Stephen Hemminger007c3832008-11-20 20:28:35 -0800481static const struct net_device_ops reg_vif_netdev_ops = {
482 .ndo_start_xmit = reg_vif_xmit,
Nicolas Dichtelee9b9592015-04-02 17:07:03 +0200483 .ndo_get_iflink = reg_vif_get_iflink,
Stephen Hemminger007c3832008-11-20 20:28:35 -0800484};
485
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486static void reg_vif_setup(struct net_device *dev)
487{
488 dev->type = ARPHRD_PIMREG;
Kris Katterjohn46f25df2006-01-05 16:35:42 -0800489 dev->mtu = ETH_DATA_LEN - sizeof(struct iphdr) - 8;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 dev->flags = IFF_NOARP;
Himangi Saraogi70cb4a42014-05-30 21:10:48 +0530491 dev->netdev_ops = &reg_vif_netdev_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 dev->destructor = free_netdev;
Tom Goff403dbb92009-06-14 03:16:13 -0700493 dev->features |= NETIF_F_NETNS_LOCAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494}
495
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000496static struct net_device *ipmr_reg_vif(struct net *net, struct mr_table *mrt)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497{
498 struct net_device *dev;
499 struct in_device *in_dev;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000500 char name[IFNAMSIZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000502 if (mrt->id == RT_TABLE_DEFAULT)
503 sprintf(name, "pimreg");
504 else
505 sprintf(name, "pimreg%u", mrt->id);
506
Tom Gundersenc835a672014-07-14 16:37:24 +0200507 dev = alloc_netdev(0, name, NET_NAME_UNKNOWN, reg_vif_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Ian Morris51456b22015-04-03 09:17:26 +0100509 if (!dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 return NULL;
511
Tom Goff403dbb92009-06-14 03:16:13 -0700512 dev_net_set(dev, net);
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 if (register_netdevice(dev)) {
515 free_netdev(dev);
516 return NULL;
517 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Herbert Xu71e27da2007-06-04 23:36:06 -0700519 rcu_read_lock();
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000520 in_dev = __in_dev_get_rcu(dev);
521 if (!in_dev) {
Herbert Xu71e27da2007-06-04 23:36:06 -0700522 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 goto failure;
Herbert Xu71e27da2007-06-04 23:36:06 -0700524 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525
Herbert Xu71e27da2007-06-04 23:36:06 -0700526 ipv4_devconf_setall(in_dev);
Jiri Pirko1d4c8c22013-12-07 19:26:56 +0100527 neigh_parms_data_state_setall(in_dev->arp_parms);
Herbert Xu71e27da2007-06-04 23:36:06 -0700528 IPV4_DEVCONF(in_dev->cnf, RP_FILTER) = 0;
529 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530
531 if (dev_open(dev))
532 goto failure;
533
Wang Chen7dc00c82008-07-14 20:56:34 -0700534 dev_hold(dev);
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return dev;
537
538failure:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 unregister_netdevice(dev);
540 return NULL;
541}
542#endif
543
Ben Hutchings2c530402012-07-10 10:55:09 +0000544/**
545 * vif_delete - Delete a VIF entry
Wang Chen7dc00c82008-07-14 20:56:34 -0700546 * @notify: Set to 1, if the caller is a notifier_call
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900548
Patrick McHardy0c122952010-04-13 05:03:22 +0000549static int vif_delete(struct mr_table *mrt, int vifi, int notify,
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000550 struct list_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551{
552 struct vif_device *v;
553 struct net_device *dev;
554 struct in_device *in_dev;
555
Patrick McHardy0c122952010-04-13 05:03:22 +0000556 if (vifi < 0 || vifi >= mrt->maxvif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 return -EADDRNOTAVAIL;
558
Patrick McHardy0c122952010-04-13 05:03:22 +0000559 v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 write_lock_bh(&mrt_lock);
562 dev = v->dev;
563 v->dev = NULL;
564
565 if (!dev) {
566 write_unlock_bh(&mrt_lock);
567 return -EADDRNOTAVAIL;
568 }
569
570#ifdef CONFIG_IP_PIMSM
Patrick McHardy0c122952010-04-13 05:03:22 +0000571 if (vifi == mrt->mroute_reg_vif_num)
572 mrt->mroute_reg_vif_num = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573#endif
574
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000575 if (vifi + 1 == mrt->maxvif) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 int tmp;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000577
578 for (tmp = vifi - 1; tmp >= 0; tmp--) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000579 if (VIF_EXISTS(mrt, tmp))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 break;
581 }
Patrick McHardy0c122952010-04-13 05:03:22 +0000582 mrt->maxvif = tmp+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
585 write_unlock_bh(&mrt_lock);
586
587 dev_set_allmulti(dev, -1);
588
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000589 in_dev = __in_dev_get_rtnl(dev);
590 if (in_dev) {
Herbert Xu42f811b2007-06-04 23:34:44 -0700591 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000592 inet_netconf_notify_devconf(dev_net(dev),
593 NETCONFA_MC_FORWARDING,
594 dev->ifindex, &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 ip_rt_multicast_event(in_dev);
596 }
597
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000598 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER) && !notify)
Eric Dumazetd17fa6f2009-10-28 05:21:38 +0000599 unregister_netdevice_queue(dev, head);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 dev_put(dev);
602 return 0;
603}
604
Eric Dumazeta8c94862010-10-01 16:15:08 +0000605static void ipmr_cache_free_rcu(struct rcu_head *head)
606{
607 struct mfc_cache *c = container_of(head, struct mfc_cache, rcu);
608
609 kmem_cache_free(mrt_cachep, c);
610}
611
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000612static inline void ipmr_cache_free(struct mfc_cache *c)
613{
Eric Dumazeta8c94862010-10-01 16:15:08 +0000614 call_rcu(&c->rcu, ipmr_cache_free_rcu);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000615}
616
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617/* Destroy an unresolved cache entry, killing queued skbs
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000618 * and reporting error to netlink readers.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 */
620
Patrick McHardy0c122952010-04-13 05:03:22 +0000621static void ipmr_destroy_unres(struct mr_table *mrt, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
Patrick McHardy8de53df2010-04-15 13:29:28 +0200623 struct net *net = read_pnet(&mrt->net);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700625 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Patrick McHardy0c122952010-04-13 05:03:22 +0000627 atomic_dec(&mrt->cache_resolve_queue_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Jianjun Kongc354e122008-11-03 00:28:02 -0800629 while ((skb = skb_dequeue(&c->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700630 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
632 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000633 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000635 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700636 e->error = -ETIMEDOUT;
637 memset(&e->msg, 0, sizeof(e->msg));
Thomas Graf2942e902006-08-15 00:30:25 -0700638
Eric W. Biederman15e47302012-09-07 20:12:54 +0000639 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000640 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 kfree_skb(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
Benjamin Thery5c0a66f2009-01-22 04:56:17 +0000645 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648
Patrick McHardye258beb2010-04-13 05:03:19 +0000649/* Timer process for the unresolved queue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Patrick McHardye258beb2010-04-13 05:03:19 +0000651static void ipmr_expire_process(unsigned long arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652{
Patrick McHardy0c122952010-04-13 05:03:22 +0000653 struct mr_table *mrt = (struct mr_table *)arg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 unsigned long now;
655 unsigned long expires;
Patrick McHardy862465f2010-04-13 05:03:21 +0000656 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 if (!spin_trylock(&mfc_unres_lock)) {
Patrick McHardy0c122952010-04-13 05:03:22 +0000659 mod_timer(&mrt->ipmr_expire_timer, jiffies+HZ/10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 return;
661 }
662
Patrick McHardy0c122952010-04-13 05:03:22 +0000663 if (list_empty(&mrt->mfc_unres_queue))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 goto out;
665
666 now = jiffies;
667 expires = 10*HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Patrick McHardy0c122952010-04-13 05:03:22 +0000669 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 if (time_after(c->mfc_un.unres.expires, now)) {
671 unsigned long interval = c->mfc_un.unres.expires - now;
672 if (interval < expires)
673 expires = interval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 continue;
675 }
676
Patrick McHardy862465f2010-04-13 05:03:21 +0000677 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +0000678 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +0000679 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
Patrick McHardy0c122952010-04-13 05:03:22 +0000682 if (!list_empty(&mrt->mfc_unres_queue))
683 mod_timer(&mrt->ipmr_expire_timer, jiffies + expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685out:
686 spin_unlock(&mfc_unres_lock);
687}
688
689/* Fill oifs list. It is called under write locked mrt_lock. */
690
Patrick McHardy0c122952010-04-13 05:03:22 +0000691static void ipmr_update_thresholds(struct mr_table *mrt, struct mfc_cache *cache,
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000692 unsigned char *ttls)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693{
694 int vifi;
695
696 cache->mfc_un.res.minvif = MAXVIFS;
697 cache->mfc_un.res.maxvif = 0;
698 memset(cache->mfc_un.res.ttls, 255, MAXVIFS);
699
Patrick McHardy0c122952010-04-13 05:03:22 +0000700 for (vifi = 0; vifi < mrt->maxvif; vifi++) {
701 if (VIF_EXISTS(mrt, vifi) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +0000702 ttls[vifi] && ttls[vifi] < 255) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 cache->mfc_un.res.ttls[vifi] = ttls[vifi];
704 if (cache->mfc_un.res.minvif > vifi)
705 cache->mfc_un.res.minvif = vifi;
706 if (cache->mfc_un.res.maxvif <= vifi)
707 cache->mfc_un.res.maxvif = vifi + 1;
708 }
709 }
710}
711
Patrick McHardy0c122952010-04-13 05:03:22 +0000712static int vif_add(struct net *net, struct mr_table *mrt,
713 struct vifctl *vifc, int mrtsock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
715 int vifi = vifc->vifc_vifi;
Patrick McHardy0c122952010-04-13 05:03:22 +0000716 struct vif_device *v = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 struct net_device *dev;
718 struct in_device *in_dev;
Wang Chend6070322008-07-14 20:55:26 -0700719 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 /* Is vif busy ? */
Patrick McHardy0c122952010-04-13 05:03:22 +0000722 if (VIF_EXISTS(mrt, vifi))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 return -EADDRINUSE;
724
725 switch (vifc->vifc_flags) {
726#ifdef CONFIG_IP_PIMSM
727 case VIFF_REGISTER:
728 /*
729 * Special Purpose VIF in PIM
730 * All the packets will be sent to the daemon
731 */
Patrick McHardy0c122952010-04-13 05:03:22 +0000732 if (mrt->mroute_reg_vif_num >= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return -EADDRINUSE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +0000734 dev = ipmr_reg_vif(net, mrt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 if (!dev)
736 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700737 err = dev_set_allmulti(dev, 1);
738 if (err) {
739 unregister_netdevice(dev);
Wang Chen7dc00c82008-07-14 20:56:34 -0700740 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700741 return err;
742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 break;
744#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900745 case VIFF_TUNNEL:
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000746 dev = ipmr_new_tunnel(net, vifc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 if (!dev)
748 return -ENOBUFS;
Wang Chend6070322008-07-14 20:55:26 -0700749 err = dev_set_allmulti(dev, 1);
750 if (err) {
751 ipmr_del_tunnel(dev, vifc);
Wang Chen7dc00c82008-07-14 20:56:34 -0700752 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700753 return err;
754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 break;
Ilia Kee5e81f2009-09-16 05:53:07 +0000756
757 case VIFF_USE_IFINDEX:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 case 0:
Ilia Kee5e81f2009-09-16 05:53:07 +0000759 if (vifc->vifc_flags == VIFF_USE_IFINDEX) {
760 dev = dev_get_by_index(net, vifc->vifc_lcl_ifindex);
Ian Morris51456b22015-04-03 09:17:26 +0100761 if (dev && !__in_dev_get_rtnl(dev)) {
Ilia Kee5e81f2009-09-16 05:53:07 +0000762 dev_put(dev);
763 return -EADDRNOTAVAIL;
764 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000765 } else {
Ilia Kee5e81f2009-09-16 05:53:07 +0000766 dev = ip_dev_find(net, vifc->vifc_lcl_addr.s_addr);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000767 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if (!dev)
769 return -EADDRNOTAVAIL;
Wang Chend6070322008-07-14 20:55:26 -0700770 err = dev_set_allmulti(dev, 1);
Wang Chen7dc00c82008-07-14 20:56:34 -0700771 if (err) {
772 dev_put(dev);
Wang Chend6070322008-07-14 20:55:26 -0700773 return err;
Wang Chen7dc00c82008-07-14 20:56:34 -0700774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 break;
776 default:
777 return -EINVAL;
778 }
779
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000780 in_dev = __in_dev_get_rtnl(dev);
781 if (!in_dev) {
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000782 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 return -EADDRNOTAVAIL;
Dan Carpenterd0490cf2009-11-11 02:03:54 +0000784 }
Herbert Xu42f811b2007-06-04 23:34:44 -0700785 IPV4_DEVCONF(in_dev->cnf, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +0000786 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING, dev->ifindex,
787 &in_dev->cnf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 ip_rt_multicast_event(in_dev);
789
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000790 /* Fill in the VIF structures */
791
Jianjun Kongc354e122008-11-03 00:28:02 -0800792 v->rate_limit = vifc->vifc_rate_limit;
793 v->local = vifc->vifc_lcl_addr.s_addr;
794 v->remote = vifc->vifc_rmt_addr.s_addr;
795 v->flags = vifc->vifc_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 if (!mrtsock)
797 v->flags |= VIFF_STATIC;
Jianjun Kongc354e122008-11-03 00:28:02 -0800798 v->threshold = vifc->vifc_threshold;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 v->bytes_in = 0;
800 v->bytes_out = 0;
801 v->pkt_in = 0;
802 v->pkt_out = 0;
803 v->link = dev->ifindex;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000804 if (v->flags & (VIFF_TUNNEL | VIFF_REGISTER))
Nicolas Dichtela54acb32015-04-02 17:07:00 +0200805 v->link = dev_get_iflink(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807 /* And finish update writing critical data */
808 write_lock_bh(&mrt_lock);
Jianjun Kongc354e122008-11-03 00:28:02 -0800809 v->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000811 if (v->flags & VIFF_REGISTER)
Patrick McHardy0c122952010-04-13 05:03:22 +0000812 mrt->mroute_reg_vif_num = vifi;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813#endif
Patrick McHardy0c122952010-04-13 05:03:22 +0000814 if (vifi+1 > mrt->maxvif)
815 mrt->maxvif = vifi+1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 write_unlock_bh(&mrt_lock);
817 return 0;
818}
819
Eric Dumazeta8c94862010-10-01 16:15:08 +0000820/* called with rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +0000821static struct mfc_cache *ipmr_cache_find(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000822 __be32 origin,
823 __be32 mcastgrp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824{
Jianjun Kongc354e122008-11-03 00:28:02 -0800825 int line = MFC_HASH(mcastgrp, origin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 struct mfc_cache *c;
827
Eric Dumazeta8c94862010-10-01 16:15:08 +0000828 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list) {
Patrick McHardy862465f2010-04-13 05:03:21 +0000829 if (c->mfc_origin == origin && c->mfc_mcastgrp == mcastgrp)
830 return c;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Patrick McHardy862465f2010-04-13 05:03:21 +0000832 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
834
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000835/* Look for a (*,*,oif) entry */
836static struct mfc_cache *ipmr_cache_find_any_parent(struct mr_table *mrt,
837 int vifi)
838{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100839 int line = MFC_HASH(htonl(INADDR_ANY), htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000840 struct mfc_cache *c;
841
842 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100843 if (c->mfc_origin == htonl(INADDR_ANY) &&
844 c->mfc_mcastgrp == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000845 c->mfc_un.res.ttls[vifi] < 255)
846 return c;
847
848 return NULL;
849}
850
851/* Look for a (*,G) entry */
852static struct mfc_cache *ipmr_cache_find_any(struct mr_table *mrt,
853 __be32 mcastgrp, int vifi)
854{
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100855 int line = MFC_HASH(mcastgrp, htonl(INADDR_ANY));
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000856 struct mfc_cache *c, *proxy;
857
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100858 if (mcastgrp == htonl(INADDR_ANY))
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000859 goto skip;
860
861 list_for_each_entry_rcu(c, &mrt->mfc_cache_array[line], list)
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +0100862 if (c->mfc_origin == htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +0000863 c->mfc_mcastgrp == mcastgrp) {
864 if (c->mfc_un.res.ttls[vifi] < 255)
865 return c;
866
867 /* It's ok if the vifi is part of the static tree */
868 proxy = ipmr_cache_find_any_parent(mrt,
869 c->mfc_parent);
870 if (proxy && proxy->mfc_un.res.ttls[vifi] < 255)
871 return c;
872 }
873
874skip:
875 return ipmr_cache_find_any_parent(mrt, vifi);
876}
877
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878/*
879 * Allocate a multicast cache entry
880 */
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000881static struct mfc_cache *ipmr_cache_alloc(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882{
Jianjun Kongc354e122008-11-03 00:28:02 -0800883 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_KERNEL);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000884
885 if (c)
886 c->mfc_un.res.minvif = MAXVIFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 return c;
888}
889
Patrick McHardyd658f8a2010-04-13 05:03:20 +0000890static struct mfc_cache *ipmr_cache_alloc_unres(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Jianjun Kongc354e122008-11-03 00:28:02 -0800892 struct mfc_cache *c = kmem_cache_zalloc(mrt_cachep, GFP_ATOMIC);
Eric Dumazeta8c94862010-10-01 16:15:08 +0000893
894 if (c) {
895 skb_queue_head_init(&c->mfc_un.unres.unresolved);
896 c->mfc_un.unres.expires = jiffies + 10*HZ;
897 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return c;
899}
900
901/*
902 * A cache entry has gone into a resolved state from queued
903 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900904
Patrick McHardy0c122952010-04-13 05:03:22 +0000905static void ipmr_cache_resolve(struct net *net, struct mr_table *mrt,
906 struct mfc_cache *uc, struct mfc_cache *c)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907{
908 struct sk_buff *skb;
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700909 struct nlmsgerr *e;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000911 /* Play the pending entries through our router */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Jianjun Kongc354e122008-11-03 00:28:02 -0800913 while ((skb = __skb_dequeue(&uc->mfc_un.unres.unresolved))) {
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700914 if (ip_hdr(skb)->version == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 struct nlmsghdr *nlh = (struct nlmsghdr *)skb_pull(skb, sizeof(struct iphdr));
916
Hong zhi guo573ce262013-03-27 06:47:04 +0000917 if (__ipmr_fill_mroute(mrt, skb, c, nlmsg_data(nlh)) > 0) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000918 nlh->nlmsg_len = skb_tail_pointer(skb) -
919 (u8 *)nlh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 } else {
921 nlh->nlmsg_type = NLMSG_ERROR;
Hong zhi guo573ce262013-03-27 06:47:04 +0000922 nlh->nlmsg_len = nlmsg_msg_size(sizeof(struct nlmsgerr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 skb_trim(skb, nlh->nlmsg_len);
Hong zhi guo573ce262013-03-27 06:47:04 +0000924 e = nlmsg_data(nlh);
Patrick McHardy9ef1d4c2005-06-28 12:55:30 -0700925 e->error = -EMSGSIZE;
926 memset(&e->msg, 0, sizeof(e->msg));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 }
Thomas Graf2942e902006-08-15 00:30:25 -0700928
Eric W. Biederman15e47302012-09-07 20:12:54 +0000929 rtnl_unicast(skb, net, NETLINK_CB(skb).portid);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000930 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +0000931 ip_mr_forward(net, mrt, skb, c, 0);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000932 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
934}
935
936/*
937 * Bounce a cache query up to mrouted. We could use netlink for this but mrouted
938 * expects the following bizarre scheme.
939 *
940 * Called under mrt_lock.
941 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900942
Patrick McHardy0c122952010-04-13 05:03:22 +0000943static int ipmr_cache_report(struct mr_table *mrt,
Benjamin Thery4feb88e2009-01-22 04:56:23 +0000944 struct sk_buff *pkt, vifi_t vifi, int assert)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945{
946 struct sk_buff *skb;
Arnaldo Carvalho de Meloc9bdd4b2007-03-12 20:09:15 -0300947 const int ihl = ip_hdrlen(pkt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 struct igmphdr *igmp;
949 struct igmpmsg *msg;
Eric Dumazet4c968702010-10-01 16:15:01 +0000950 struct sock *mroute_sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 int ret;
952
953#ifdef CONFIG_IP_PIMSM
954 if (assert == IGMPMSG_WHOLEPKT)
955 skb = skb_realloc_headroom(pkt, sizeof(struct iphdr));
956 else
957#endif
958 skb = alloc_skb(128, GFP_ATOMIC);
959
Stephen Hemminger132adf52007-03-08 20:44:43 -0800960 if (!skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return -ENOBUFS;
962
963#ifdef CONFIG_IP_PIMSM
964 if (assert == IGMPMSG_WHOLEPKT) {
965 /* Ugly, but we have no choice with this interface.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000966 * Duplicate old header, fix ihl, length etc.
967 * And all this only to mangle msg->im_msgtype and
968 * to set msg->im_mbz to "mbz" :-)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 */
Arnaldo Carvalho de Melo878c8142007-03-11 22:38:29 -0300970 skb_push(skb, sizeof(struct iphdr));
971 skb_reset_network_header(skb);
Arnaldo Carvalho de Melobadff6d2007-03-13 13:06:52 -0300972 skb_reset_transport_header(skb);
Arnaldo Carvalho de Melo0272ffc2007-03-12 20:05:39 -0300973 msg = (struct igmpmsg *)skb_network_header(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700974 memcpy(msg, skb_network_header(pkt), sizeof(struct iphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 msg->im_msgtype = IGMPMSG_WHOLEPKT;
976 msg->im_mbz = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +0000977 msg->im_vif = mrt->mroute_reg_vif_num;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700978 ip_hdr(skb)->ihl = sizeof(struct iphdr) >> 2;
979 ip_hdr(skb)->tot_len = htons(ntohs(ip_hdr(pkt)->tot_len) +
980 sizeof(struct iphdr));
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900981 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982#endif
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +0900983 {
984
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000985 /* Copy the IP header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700986
Cong Wang30f3a402013-06-05 20:14:10 +0800987 skb_set_network_header(skb, skb->len);
Arnaldo Carvalho de Meloddc7b8e2007-03-15 21:42:27 -0300988 skb_put(skb, ihl);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300989 skb_copy_to_linear_data(skb, pkt->data, ihl);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000990 ip_hdr(skb)->protocol = 0; /* Flag to the kernel this is a route add */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700991 msg = (struct igmpmsg *)skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 msg->im_vif = vifi;
Eric Dumazetadf30902009-06-02 05:19:30 +0000993 skb_dst_set(skb, dst_clone(skb_dst(pkt)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000995 /* Add our header */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Eric Dumazeta8cb16d2010-10-01 16:15:29 +0000997 igmp = (struct igmphdr *)skb_put(skb, sizeof(struct igmphdr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 igmp->type =
999 msg->im_msgtype = assert;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001000 igmp->code = 0;
1001 ip_hdr(skb)->tot_len = htons(skb->len); /* Fix the length */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001002 skb->transport_header = skb->network_header;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001003 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Eric Dumazet4c968702010-10-01 16:15:01 +00001005 rcu_read_lock();
1006 mroute_sk = rcu_dereference(mrt->mroute_sk);
Ian Morris51456b22015-04-03 09:17:26 +01001007 if (!mroute_sk) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001008 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 kfree_skb(skb);
1010 return -EINVAL;
1011 }
1012
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001013 /* Deliver to mrouted */
1014
Eric Dumazet4c968702010-10-01 16:15:01 +00001015 ret = sock_queue_rcv_skb(mroute_sk, skb);
1016 rcu_read_unlock();
Benjamin Thery70a269e2009-01-22 04:56:15 +00001017 if (ret < 0) {
Joe Perchese87cc472012-05-13 21:56:26 +00001018 net_warn_ratelimited("mroute: pending queue full, dropping entries\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 kfree_skb(skb);
1020 }
1021
1022 return ret;
1023}
1024
1025/*
1026 * Queue a packet for resolution. It gets locked cache entry!
1027 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029static int
Patrick McHardy0c122952010-04-13 05:03:22 +00001030ipmr_cache_unresolved(struct mr_table *mrt, vifi_t vifi, struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
Patrick McHardy862465f2010-04-13 05:03:21 +00001032 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 int err;
1034 struct mfc_cache *c;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001035 const struct iphdr *iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001038 list_for_each_entry(c, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001039 if (c->mfc_mcastgrp == iph->daddr &&
Patrick McHardy862465f2010-04-13 05:03:21 +00001040 c->mfc_origin == iph->saddr) {
1041 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001043 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 }
1045
Patrick McHardy862465f2010-04-13 05:03:21 +00001046 if (!found) {
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001047 /* Create a new entry if allowable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048
Patrick McHardy0c122952010-04-13 05:03:22 +00001049 if (atomic_read(&mrt->cache_resolve_queue_len) >= 10 ||
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001050 (c = ipmr_cache_alloc_unres()) == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 spin_unlock_bh(&mfc_unres_lock);
1052
1053 kfree_skb(skb);
1054 return -ENOBUFS;
1055 }
1056
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001057 /* Fill in the new cache entry */
1058
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001059 c->mfc_parent = -1;
1060 c->mfc_origin = iph->saddr;
1061 c->mfc_mcastgrp = iph->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001063 /* Reflect first query at mrouted. */
1064
Patrick McHardy0c122952010-04-13 05:03:22 +00001065 err = ipmr_cache_report(mrt, skb, vifi, IGMPMSG_NOCACHE);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001066 if (err < 0) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001067 /* If the report failed throw the cache entry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 out - Brad Parker
1069 */
1070 spin_unlock_bh(&mfc_unres_lock);
1071
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001072 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 kfree_skb(skb);
1074 return err;
1075 }
1076
Patrick McHardy0c122952010-04-13 05:03:22 +00001077 atomic_inc(&mrt->cache_resolve_queue_len);
1078 list_add(&c->list, &mrt->mfc_unres_queue);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001079 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080
David S. Miller278554b2010-05-12 00:05:35 -07001081 if (atomic_read(&mrt->cache_resolve_queue_len) == 1)
1082 mod_timer(&mrt->ipmr_expire_timer, c->mfc_un.unres.expires);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001085 /* See if we can append the packet */
1086
1087 if (c->mfc_un.unres.unresolved.qlen > 3) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 kfree_skb(skb);
1089 err = -ENOBUFS;
1090 } else {
Jianjun Kongc354e122008-11-03 00:28:02 -08001091 skb_queue_tail(&c->mfc_un.unres.unresolved, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 err = 0;
1093 }
1094
1095 spin_unlock_bh(&mfc_unres_lock);
1096 return err;
1097}
1098
1099/*
1100 * MFC cache manipulation by user space mroute daemon
1101 */
1102
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001103static int ipmr_mfc_delete(struct mr_table *mrt, struct mfcctl *mfc, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104{
1105 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001106 struct mfc_cache *c, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Jianjun Kongc354e122008-11-03 00:28:02 -08001108 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Patrick McHardy0c122952010-04-13 05:03:22 +00001110 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001112 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1113 (parent == -1 || parent == c->mfc_parent)) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00001114 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001115 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001116 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001117 return 0;
1118 }
1119 }
1120 return -ENOENT;
1121}
1122
Patrick McHardy0c122952010-04-13 05:03:22 +00001123static int ipmr_mfc_add(struct net *net, struct mr_table *mrt,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001124 struct mfcctl *mfc, int mrtsock, int parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125{
Patrick McHardy862465f2010-04-13 05:03:21 +00001126 bool found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 int line;
Patrick McHardy862465f2010-04-13 05:03:21 +00001128 struct mfc_cache *uc, *c;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Patrick McHardya50436f22010-03-17 06:04:14 +00001130 if (mfc->mfcc_parent >= MAXVIFS)
1131 return -ENFILE;
1132
Jianjun Kongc354e122008-11-03 00:28:02 -08001133 line = MFC_HASH(mfc->mfcc_mcastgrp.s_addr, mfc->mfcc_origin.s_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Patrick McHardy0c122952010-04-13 05:03:22 +00001135 list_for_each_entry(c, &mrt->mfc_cache_array[line], list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 if (c->mfc_origin == mfc->mfcc_origin.s_addr &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001137 c->mfc_mcastgrp == mfc->mfcc_mcastgrp.s_addr &&
1138 (parent == -1 || parent == c->mfc_parent)) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001139 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 break;
Patrick McHardy862465f2010-04-13 05:03:21 +00001141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 }
1143
Patrick McHardy862465f2010-04-13 05:03:21 +00001144 if (found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 write_lock_bh(&mrt_lock);
1146 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001147 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 if (!mrtsock)
1149 c->mfc_flags |= MFC_STATIC;
1150 write_unlock_bh(&mrt_lock);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001151 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 return 0;
1153 }
1154
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001155 if (mfc->mfcc_mcastgrp.s_addr != htonl(INADDR_ANY) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001156 !ipv4_is_multicast(mfc->mfcc_mcastgrp.s_addr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 return -EINVAL;
1158
Patrick McHardyd658f8a2010-04-13 05:03:20 +00001159 c = ipmr_cache_alloc();
Ian Morris51456b22015-04-03 09:17:26 +01001160 if (!c)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 return -ENOMEM;
1162
Jianjun Kongc354e122008-11-03 00:28:02 -08001163 c->mfc_origin = mfc->mfcc_origin.s_addr;
1164 c->mfc_mcastgrp = mfc->mfcc_mcastgrp.s_addr;
1165 c->mfc_parent = mfc->mfcc_parent;
Patrick McHardy0c122952010-04-13 05:03:22 +00001166 ipmr_update_thresholds(mrt, c, mfc->mfcc_ttls);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 if (!mrtsock)
1168 c->mfc_flags |= MFC_STATIC;
1169
Eric Dumazeta8c94862010-10-01 16:15:08 +00001170 list_add_rcu(&c->list, &mrt->mfc_cache_array[line]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171
1172 /*
1173 * Check to see if we resolved a queued list. If so we
1174 * need to send on the frames and tidy up.
1175 */
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001176 found = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001178 list_for_each_entry(uc, &mrt->mfc_unres_queue, list) {
Patrick McHardye258beb2010-04-13 05:03:19 +00001179 if (uc->mfc_origin == c->mfc_origin &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 uc->mfc_mcastgrp == c->mfc_mcastgrp) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001181 list_del(&uc->list);
Patrick McHardy0c122952010-04-13 05:03:22 +00001182 atomic_dec(&mrt->cache_resolve_queue_len);
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001183 found = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 break;
1185 }
1186 }
Patrick McHardy0c122952010-04-13 05:03:22 +00001187 if (list_empty(&mrt->mfc_unres_queue))
1188 del_timer(&mrt->ipmr_expire_timer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 spin_unlock_bh(&mfc_unres_lock);
1190
Patrick McHardyb0ebb732010-04-15 13:29:28 +02001191 if (found) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001192 ipmr_cache_resolve(net, mrt, uc, c);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001193 ipmr_cache_free(uc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001195 mroute_netlink_event(mrt, c, RTM_NEWROUTE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 return 0;
1197}
1198
1199/*
1200 * Close the multicast socket, and clear the vif tables etc
1201 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001202
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001203static void mroute_clean_tables(struct mr_table *mrt, bool all)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204{
1205 int i;
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001206 LIST_HEAD(list);
Patrick McHardy862465f2010-04-13 05:03:21 +00001207 struct mfc_cache *c, *next;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001208
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001209 /* Shut down all active vif entries */
1210
Patrick McHardy0c122952010-04-13 05:03:22 +00001211 for (i = 0; i < mrt->maxvif; i++) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001212 if (!all && (mrt->vif_table[i].flags & VIFF_STATIC))
1213 continue;
1214 vif_delete(mrt, i, 0, &list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 }
Eric Dumazetd17fa6f2009-10-28 05:21:38 +00001216 unregister_netdevice_many(&list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001218 /* Wipe the cache */
1219
Patrick McHardy862465f2010-04-13 05:03:21 +00001220 for (i = 0; i < MFC_LINES; i++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00001221 list_for_each_entry_safe(c, next, &mrt->mfc_cache_array[i], list) {
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001222 if (!all && (c->mfc_flags & MFC_STATIC))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 continue;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001224 list_del_rcu(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001225 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Benjamin Thery5c0a66f2009-01-22 04:56:17 +00001226 ipmr_cache_free(c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
1228 }
1229
Patrick McHardy0c122952010-04-13 05:03:22 +00001230 if (atomic_read(&mrt->cache_resolve_queue_len) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001232 list_for_each_entry_safe(c, next, &mrt->mfc_unres_queue, list) {
Patrick McHardy862465f2010-04-13 05:03:21 +00001233 list_del(&c->list);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00001234 mroute_netlink_event(mrt, c, RTM_DELROUTE);
Patrick McHardy0c122952010-04-13 05:03:22 +00001235 ipmr_destroy_unres(mrt, c);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 }
1237 spin_unlock_bh(&mfc_unres_lock);
1238 }
1239}
1240
Eric Dumazet4c968702010-10-01 16:15:01 +00001241/* called from ip_ra_control(), before an RCU grace period,
1242 * we dont need to call synchronize_rcu() here
1243 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244static void mrtsock_destruct(struct sock *sk)
1245{
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001246 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001247 struct mr_table *mrt;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 rtnl_lock();
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001250 ipmr_for_each_table(mrt, net) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001251 if (sk == rtnl_dereference(mrt->mroute_sk)) {
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001252 IPV4_DEVCONF_ALL(net, MC_FORWARDING)--;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001253 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1254 NETCONFA_IFINDEX_ALL,
1255 net->ipv4.devconf_all);
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +00001256 RCU_INIT_POINTER(mrt->mroute_sk, NULL);
Nikolay Aleksandrov0e615e92015-11-20 13:54:19 +01001257 mroute_clean_tables(mrt, false);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 }
1260 rtnl_unlock();
1261}
1262
1263/*
1264 * Socket options and virtual interface manipulation. The whole
1265 * virtual interface system is a complete heap, but unfortunately
1266 * that's how BSD mrouted happens to think. Maybe one day with a proper
1267 * MOSPF/PIM router set up we can clean this up.
1268 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001269
David S. Millerb7058842009-09-30 16:12:20 -07001270int ip_mroute_setsockopt(struct sock *sk, int optname, char __user *optval, unsigned int optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271{
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001272 int ret, parent = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 struct vifctl vif;
1274 struct mfcctl mfc;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001275 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001276 struct mr_table *mrt;
1277
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001278 if (sk->sk_type != SOCK_RAW ||
1279 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1280 return -EOPNOTSUPP;
1281
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001282 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001283 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001284 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001285
Stephen Hemminger132adf52007-03-08 20:44:43 -08001286 if (optname != MRT_INIT) {
Eric Dumazet33d480c2011-08-11 19:30:52 +00001287 if (sk != rcu_access_pointer(mrt->mroute_sk) &&
Eric W. Biederman52e804c2012-11-16 03:03:05 +00001288 !ns_capable(net->user_ns, CAP_NET_ADMIN))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 return -EACCES;
1290 }
1291
Stephen Hemminger132adf52007-03-08 20:44:43 -08001292 switch (optname) {
1293 case MRT_INIT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001294 if (optlen != sizeof(int))
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001295 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
Stephen Hemminger132adf52007-03-08 20:44:43 -08001297 rtnl_lock();
Eric Dumazet4c968702010-10-01 16:15:01 +00001298 if (rtnl_dereference(mrt->mroute_sk)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 rtnl_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001300 return -EADDRINUSE;
1301 }
1302
1303 ret = ip_ra_control(sk, 1, mrtsock_destruct);
1304 if (ret == 0) {
Eric Dumazetcf778b02012-01-12 04:41:32 +00001305 rcu_assign_pointer(mrt->mroute_sk, sk);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001306 IPV4_DEVCONF_ALL(net, MC_FORWARDING)++;
Nicolas Dichteld67b8c62012-12-04 01:13:35 +00001307 inet_netconf_notify_devconf(net, NETCONFA_MC_FORWARDING,
1308 NETCONFA_IFINDEX_ALL,
1309 net->ipv4.devconf_all);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001310 }
1311 rtnl_unlock();
1312 return ret;
1313 case MRT_DONE:
Eric Dumazet33d480c2011-08-11 19:30:52 +00001314 if (sk != rcu_access_pointer(mrt->mroute_sk))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001315 return -EACCES;
1316 return ip_ra_control(sk, 0, NULL);
1317 case MRT_ADD_VIF:
1318 case MRT_DEL_VIF:
Jianjun Kongc354e122008-11-03 00:28:02 -08001319 if (optlen != sizeof(vif))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001320 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001321 if (copy_from_user(&vif, optval, sizeof(vif)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001322 return -EFAULT;
1323 if (vif.vifc_vifi >= MAXVIFS)
1324 return -ENFILE;
1325 rtnl_lock();
Jianjun Kongc354e122008-11-03 00:28:02 -08001326 if (optname == MRT_ADD_VIF) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001327 ret = vif_add(net, mrt, &vif,
1328 sk == rtnl_dereference(mrt->mroute_sk));
Stephen Hemminger132adf52007-03-08 20:44:43 -08001329 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001330 ret = vif_delete(mrt, vif.vifc_vifi, 0, NULL);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001331 }
1332 rtnl_unlock();
1333 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 /*
1336 * Manipulate the forwarding caches. These live
1337 * in a sort of kernel/user symbiosis.
1338 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001339 case MRT_ADD_MFC:
1340 case MRT_DEL_MFC:
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001341 parent = -1;
1342 case MRT_ADD_MFC_PROXY:
1343 case MRT_DEL_MFC_PROXY:
Jianjun Kongc354e122008-11-03 00:28:02 -08001344 if (optlen != sizeof(mfc))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001345 return -EINVAL;
Jianjun Kongc354e122008-11-03 00:28:02 -08001346 if (copy_from_user(&mfc, optval, sizeof(mfc)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001347 return -EFAULT;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001348 if (parent == 0)
1349 parent = mfc.mfcc_parent;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001350 rtnl_lock();
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001351 if (optname == MRT_DEL_MFC || optname == MRT_DEL_MFC_PROXY)
1352 ret = ipmr_mfc_delete(mrt, &mfc, parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001353 else
Eric Dumazet4c968702010-10-01 16:15:01 +00001354 ret = ipmr_mfc_add(net, mrt, &mfc,
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001355 sk == rtnl_dereference(mrt->mroute_sk),
1356 parent);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001357 rtnl_unlock();
1358 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359 /*
1360 * Control PIM assert.
1361 */
Stephen Hemminger132adf52007-03-08 20:44:43 -08001362 case MRT_ASSERT:
1363 {
1364 int v;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001365 if (optlen != sizeof(v))
1366 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001367 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001368 return -EFAULT;
Joe Perches53d68412012-11-25 09:35:30 +00001369 mrt->mroute_do_assert = v;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001370 return 0;
1371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372#ifdef CONFIG_IP_PIMSM
Stephen Hemminger132adf52007-03-08 20:44:43 -08001373 case MRT_PIM:
1374 {
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001375 int v;
1376
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001377 if (optlen != sizeof(v))
1378 return -EINVAL;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001379 if (get_user(v, (int __user *)optval))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001380 return -EFAULT;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001381 v = !!v;
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08001382
Stephen Hemminger132adf52007-03-08 20:44:43 -08001383 rtnl_lock();
1384 ret = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00001385 if (v != mrt->mroute_do_pim) {
1386 mrt->mroute_do_pim = v;
1387 mrt->mroute_do_assert = v;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 }
Stephen Hemminger132adf52007-03-08 20:44:43 -08001389 rtnl_unlock();
1390 return ret;
1391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001393#ifdef CONFIG_IP_MROUTE_MULTIPLE_TABLES
1394 case MRT_TABLE:
1395 {
1396 u32 v;
1397
1398 if (optlen != sizeof(u32))
1399 return -EINVAL;
1400 if (get_user(v, (u32 __user *)optval))
1401 return -EFAULT;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001402
Eric Dumazetb49d3c12012-11-25 09:44:29 +00001403 /* "pimreg%u" should not exceed 16 bytes (IFNAMSIZ) */
1404 if (v != RT_TABLE_DEFAULT && v >= 1000000000)
1405 return -EINVAL;
1406
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001407 rtnl_lock();
1408 ret = 0;
Eric Dumazet4c968702010-10-01 16:15:01 +00001409 if (sk == rtnl_dereference(mrt->mroute_sk)) {
1410 ret = -EBUSY;
1411 } else {
1412 if (!ipmr_new_table(net, v))
1413 ret = -ENOMEM;
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001414 else
1415 raw_sk(sk)->ipmr_table = v;
Eric Dumazet4c968702010-10-01 16:15:01 +00001416 }
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001417 rtnl_unlock();
1418 return ret;
1419 }
1420#endif
Stephen Hemminger132adf52007-03-08 20:44:43 -08001421 /*
1422 * Spurious command, or MRT_VERSION which you cannot
1423 * set.
1424 */
1425 default:
1426 return -ENOPROTOOPT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427 }
1428}
1429
1430/*
1431 * Getsock opt support for the multicast routing system.
1432 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001433
Jianjun Kongc354e122008-11-03 00:28:02 -08001434int ip_mroute_getsockopt(struct sock *sk, int optname, char __user *optval, int __user *optlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435{
1436 int olr;
1437 int val;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001438 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001439 struct mr_table *mrt;
1440
Eric Dumazet5e1859f2012-11-25 06:41:45 +00001441 if (sk->sk_type != SOCK_RAW ||
1442 inet_sk(sk)->inet_num != IPPROTO_IGMP)
1443 return -EOPNOTSUPP;
1444
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001445 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001446 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001447 return -ENOENT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001448
Jianjun Kongc354e122008-11-03 00:28:02 -08001449 if (optname != MRT_VERSION &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450#ifdef CONFIG_IP_PIMSM
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001451 optname != MRT_PIM &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452#endif
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001453 optname != MRT_ASSERT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454 return -ENOPROTOOPT;
1455
1456 if (get_user(olr, optlen))
1457 return -EFAULT;
1458
1459 olr = min_t(unsigned int, olr, sizeof(int));
1460 if (olr < 0)
1461 return -EINVAL;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001462
Jianjun Kongc354e122008-11-03 00:28:02 -08001463 if (put_user(olr, optlen))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464 return -EFAULT;
Jianjun Kongc354e122008-11-03 00:28:02 -08001465 if (optname == MRT_VERSION)
1466 val = 0x0305;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467#ifdef CONFIG_IP_PIMSM
Jianjun Kongc354e122008-11-03 00:28:02 -08001468 else if (optname == MRT_PIM)
Patrick McHardy0c122952010-04-13 05:03:22 +00001469 val = mrt->mroute_do_pim;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470#endif
1471 else
Patrick McHardy0c122952010-04-13 05:03:22 +00001472 val = mrt->mroute_do_assert;
Jianjun Kongc354e122008-11-03 00:28:02 -08001473 if (copy_to_user(optval, &val, olr))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 return -EFAULT;
1475 return 0;
1476}
1477
1478/*
1479 * The IP multicast ioctl support routines.
1480 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001481
Linus Torvalds1da177e2005-04-16 15:20:36 -07001482int ipmr_ioctl(struct sock *sk, int cmd, void __user *arg)
1483{
1484 struct sioc_sg_req sr;
1485 struct sioc_vif_req vr;
1486 struct vif_device *vif;
1487 struct mfc_cache *c;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001488 struct net *net = sock_net(sk);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001489 struct mr_table *mrt;
1490
1491 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001492 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001493 return -ENOENT;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001494
Stephen Hemminger132adf52007-03-08 20:44:43 -08001495 switch (cmd) {
1496 case SIOCGETVIFCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001497 if (copy_from_user(&vr, arg, sizeof(vr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001498 return -EFAULT;
Patrick McHardy0c122952010-04-13 05:03:22 +00001499 if (vr.vifi >= mrt->maxvif)
Stephen Hemminger132adf52007-03-08 20:44:43 -08001500 return -EINVAL;
1501 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00001502 vif = &mrt->vif_table[vr.vifi];
1503 if (VIF_EXISTS(mrt, vr.vifi)) {
Jianjun Kongc354e122008-11-03 00:28:02 -08001504 vr.icount = vif->pkt_in;
1505 vr.ocount = vif->pkt_out;
1506 vr.ibytes = vif->bytes_in;
1507 vr.obytes = vif->bytes_out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 read_unlock(&mrt_lock);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001509
Jianjun Kongc354e122008-11-03 00:28:02 -08001510 if (copy_to_user(arg, &vr, sizeof(vr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return -EFAULT;
Stephen Hemminger132adf52007-03-08 20:44:43 -08001512 return 0;
1513 }
1514 read_unlock(&mrt_lock);
1515 return -EADDRNOTAVAIL;
1516 case SIOCGETSGCNT:
Jianjun Kongc354e122008-11-03 00:28:02 -08001517 if (copy_from_user(&sr, arg, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001518 return -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519
Eric Dumazeta8c94862010-10-01 16:15:08 +00001520 rcu_read_lock();
Patrick McHardy0c122952010-04-13 05:03:22 +00001521 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
Stephen Hemminger132adf52007-03-08 20:44:43 -08001522 if (c) {
1523 sr.pktcnt = c->mfc_un.res.pkt;
1524 sr.bytecnt = c->mfc_un.res.bytes;
1525 sr.wrong_if = c->mfc_un.res.wrong_if;
Eric Dumazeta8c94862010-10-01 16:15:08 +00001526 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001527
Jianjun Kongc354e122008-11-03 00:28:02 -08001528 if (copy_to_user(arg, &sr, sizeof(sr)))
Stephen Hemminger132adf52007-03-08 20:44:43 -08001529 return -EFAULT;
1530 return 0;
1531 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00001532 rcu_read_unlock();
Stephen Hemminger132adf52007-03-08 20:44:43 -08001533 return -EADDRNOTAVAIL;
1534 default:
1535 return -ENOIOCTLCMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 }
1537}
1538
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001539#ifdef CONFIG_COMPAT
1540struct compat_sioc_sg_req {
1541 struct in_addr src;
1542 struct in_addr grp;
1543 compat_ulong_t pktcnt;
1544 compat_ulong_t bytecnt;
1545 compat_ulong_t wrong_if;
1546};
1547
David S. Millerca6b8bb02011-02-03 17:24:28 -08001548struct compat_sioc_vif_req {
1549 vifi_t vifi; /* Which iface */
1550 compat_ulong_t icount;
1551 compat_ulong_t ocount;
1552 compat_ulong_t ibytes;
1553 compat_ulong_t obytes;
1554};
1555
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001556int ipmr_compat_ioctl(struct sock *sk, unsigned int cmd, void __user *arg)
1557{
David S. Miller0033d5a2011-02-03 17:21:31 -08001558 struct compat_sioc_sg_req sr;
David S. Millerca6b8bb02011-02-03 17:24:28 -08001559 struct compat_sioc_vif_req vr;
1560 struct vif_device *vif;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001561 struct mfc_cache *c;
1562 struct net *net = sock_net(sk);
1563 struct mr_table *mrt;
1564
1565 mrt = ipmr_get_table(net, raw_sk(sk)->ipmr_table ? : RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01001566 if (!mrt)
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001567 return -ENOENT;
1568
1569 switch (cmd) {
David S. Millerca6b8bb02011-02-03 17:24:28 -08001570 case SIOCGETVIFCNT:
1571 if (copy_from_user(&vr, arg, sizeof(vr)))
1572 return -EFAULT;
1573 if (vr.vifi >= mrt->maxvif)
1574 return -EINVAL;
1575 read_lock(&mrt_lock);
1576 vif = &mrt->vif_table[vr.vifi];
1577 if (VIF_EXISTS(mrt, vr.vifi)) {
1578 vr.icount = vif->pkt_in;
1579 vr.ocount = vif->pkt_out;
1580 vr.ibytes = vif->bytes_in;
1581 vr.obytes = vif->bytes_out;
1582 read_unlock(&mrt_lock);
1583
1584 if (copy_to_user(arg, &vr, sizeof(vr)))
1585 return -EFAULT;
1586 return 0;
1587 }
1588 read_unlock(&mrt_lock);
1589 return -EADDRNOTAVAIL;
Eric W. Biederman709b46e2011-01-29 16:15:56 +00001590 case SIOCGETSGCNT:
1591 if (copy_from_user(&sr, arg, sizeof(sr)))
1592 return -EFAULT;
1593
1594 rcu_read_lock();
1595 c = ipmr_cache_find(mrt, sr.src.s_addr, sr.grp.s_addr);
1596 if (c) {
1597 sr.pktcnt = c->mfc_un.res.pkt;
1598 sr.bytecnt = c->mfc_un.res.bytes;
1599 sr.wrong_if = c->mfc_un.res.wrong_if;
1600 rcu_read_unlock();
1601
1602 if (copy_to_user(arg, &sr, sizeof(sr)))
1603 return -EFAULT;
1604 return 0;
1605 }
1606 rcu_read_unlock();
1607 return -EADDRNOTAVAIL;
1608 default:
1609 return -ENOIOCTLCMD;
1610 }
1611}
1612#endif
1613
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
1615static int ipmr_device_event(struct notifier_block *this, unsigned long event, void *ptr)
1616{
Jiri Pirko351638e2013-05-28 01:30:21 +00001617 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001618 struct net *net = dev_net(dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001619 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 struct vif_device *v;
1621 int ct;
Eric W. Biedermane9dc8652007-09-12 13:02:17 +02001622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (event != NETDEV_UNREGISTER)
1624 return NOTIFY_DONE;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001625
1626 ipmr_for_each_table(mrt, net) {
1627 v = &mrt->vif_table[0];
1628 for (ct = 0; ct < mrt->maxvif; ct++, v++) {
1629 if (v->dev == dev)
RongQing.Lie92036a2011-11-23 23:10:52 +00001630 vif_delete(mrt, ct, 1, NULL);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001631 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 }
1633 return NOTIFY_DONE;
1634}
1635
1636
Jianjun Kongc354e122008-11-03 00:28:02 -08001637static struct notifier_block ip_mr_notifier = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 .notifier_call = ipmr_device_event,
1639};
1640
1641/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001642 * Encapsulate a packet by attaching a valid IPIP header to it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 * This avoids tunnel drivers and other mess and gives us the speed so
1644 * important for multicast video.
1645 */
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001646
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001647static void ip_encap(struct net *net, struct sk_buff *skb,
1648 __be32 saddr, __be32 daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001650 struct iphdr *iph;
Eric Dumazetb71d1d42011-04-22 04:53:02 +00001651 const struct iphdr *old_iph = ip_hdr(skb);
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001652
1653 skb_push(skb, sizeof(struct iphdr));
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07001654 skb->transport_header = skb->network_header;
Arnaldo Carvalho de Melo8856dfa2007-03-10 19:40:39 -03001655 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001656 iph = ip_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001658 iph->version = 4;
Arnaldo Carvalho de Meloe023dd62007-03-12 20:09:36 -03001659 iph->tos = old_iph->tos;
1660 iph->ttl = old_iph->ttl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 iph->frag_off = 0;
1662 iph->daddr = daddr;
1663 iph->saddr = saddr;
1664 iph->protocol = IPPROTO_IPIP;
1665 iph->ihl = 5;
1666 iph->tot_len = htons(skb->len);
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001667 ip_select_ident(net, skb, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 ip_send_check(iph);
1669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 memset(&(IPCB(skb)->opt), 0, sizeof(IPCB(skb)->opt));
1671 nf_reset(skb);
1672}
1673
Eric W. Biederman0c4b51f2015-09-15 20:04:18 -05001674static inline int ipmr_forward_finish(struct net *net, struct sock *sk,
1675 struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676{
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001677 struct ip_options *opt = &(IPCB(skb)->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678
David S. Miller73186df2015-11-03 13:41:45 -05001679 IP_INC_STATS(net, IPSTATS_MIB_OUTFORWDATAGRAMS);
1680 IP_ADD_STATS(net, IPSTATS_MIB_OUTOCTETS, skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681
1682 if (unlikely(opt->optlen))
1683 ip_forward_options(skb);
1684
Eric W. Biederman13206b62015-10-07 16:48:35 -05001685 return dst_output(net, sk, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686}
1687
1688/*
1689 * Processing handlers for ipmr_forward
1690 */
1691
Patrick McHardy0c122952010-04-13 05:03:22 +00001692static void ipmr_queue_xmit(struct net *net, struct mr_table *mrt,
1693 struct sk_buff *skb, struct mfc_cache *c, int vifi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694{
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001695 const struct iphdr *iph = ip_hdr(skb);
Patrick McHardy0c122952010-04-13 05:03:22 +00001696 struct vif_device *vif = &mrt->vif_table[vifi];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 struct net_device *dev;
1698 struct rtable *rt;
David S. Miller31e45432011-05-03 20:25:42 -07001699 struct flowi4 fl4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700 int encap = 0;
1701
Ian Morris51456b22015-04-03 09:17:26 +01001702 if (!vif->dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 goto out_free;
1704
1705#ifdef CONFIG_IP_PIMSM
1706 if (vif->flags & VIFF_REGISTER) {
1707 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001708 vif->bytes_out += skb->len;
Pavel Emelyanovcf3677a2008-05-21 14:17:33 -07001709 vif->dev->stats.tx_bytes += skb->len;
1710 vif->dev->stats.tx_packets++;
Patrick McHardy0c122952010-04-13 05:03:22 +00001711 ipmr_cache_report(mrt, skb, vifi, IGMPMSG_WHOLEPKT);
Ilpo Järvinen69ebbf52009-02-06 23:46:51 -08001712 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713 }
1714#endif
1715
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001716 if (vif->flags & VIFF_TUNNEL) {
David S. Miller31e45432011-05-03 20:25:42 -07001717 rt = ip_route_output_ports(net, &fl4, NULL,
David S. Miller78fbfd82011-03-12 00:00:52 -05001718 vif->remote, vif->local,
1719 0, 0,
1720 IPPROTO_IPIP,
1721 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001722 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 goto out_free;
1724 encap = sizeof(struct iphdr);
1725 } else {
David S. Miller31e45432011-05-03 20:25:42 -07001726 rt = ip_route_output_ports(net, &fl4, NULL, iph->daddr, 0,
David S. Miller78fbfd82011-03-12 00:00:52 -05001727 0, 0,
1728 IPPROTO_IPIP,
1729 RT_TOS(iph->tos), vif->link);
David S. Millerb23dd4f2011-03-02 14:31:35 -08001730 if (IS_ERR(rt))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 goto out_free;
1732 }
1733
Changli Gaod8d1f302010-06-10 23:31:35 -07001734 dev = rt->dst.dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735
Changli Gaod8d1f302010-06-10 23:31:35 -07001736 if (skb->len+encap > dst_mtu(&rt->dst) && (ntohs(iph->frag_off) & IP_DF)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 /* Do not fragment multicasts. Alas, IPv4 does not
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001738 * allow to send ICMP, so that packets will disappear
1739 * to blackhole.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 */
1741
David S. Miller73186df2015-11-03 13:41:45 -05001742 IP_INC_STATS(net, IPSTATS_MIB_FRAGFAILS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 ip_rt_put(rt);
1744 goto out_free;
1745 }
1746
Changli Gaod8d1f302010-06-10 23:31:35 -07001747 encap += LL_RESERVED_SPACE(dev) + rt->dst.header_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749 if (skb_cow(skb, encap)) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001750 ip_rt_put(rt);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751 goto out_free;
1752 }
1753
1754 vif->pkt_out++;
Jianjun Kongc354e122008-11-03 00:28:02 -08001755 vif->bytes_out += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
Eric Dumazetadf30902009-06-02 05:19:30 +00001757 skb_dst_drop(skb);
Changli Gaod8d1f302010-06-10 23:31:35 -07001758 skb_dst_set(skb, &rt->dst);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07001759 ip_decrease_ttl(ip_hdr(skb));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760
1761 /* FIXME: forward and output firewalls used to be called here.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001762 * What do we do with netfilter? -- RR
1763 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 if (vif->flags & VIFF_TUNNEL) {
Hannes Frederic Sowab6a77192015-03-25 17:07:44 +01001765 ip_encap(net, skb, vif->local, vif->remote);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 /* FIXME: extra output firewall step used to be here. --RR */
Pavel Emelyanov2f4c02d2008-05-21 14:16:14 -07001767 vif->dev->stats.tx_packets++;
1768 vif->dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769 }
1770
1771 IPCB(skb)->flags |= IPSKB_FORWARDED;
1772
1773 /*
1774 * RFC1584 teaches, that DVMRP/PIM router must deliver packets locally
1775 * not only before forwarding, but after forwarding on all output
1776 * interfaces. It is clear, if mrouter runs a multicasting
1777 * program, it should receive packets not depending to what interface
1778 * program is joined.
1779 * If we will not make it, the program will have to join on all
1780 * interfaces. On the other hand, multihoming host (or router, but
1781 * not mrouter) cannot join to more than one interface - it will
1782 * result in receiving multiple packets.
1783 */
Eric W. Biederman29a26a52015-09-15 20:04:16 -05001784 NF_HOOK(NFPROTO_IPV4, NF_INET_FORWARD,
1785 net, NULL, skb, skb->dev, dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 ipmr_forward_finish);
1787 return;
1788
1789out_free:
1790 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791}
1792
Patrick McHardy0c122952010-04-13 05:03:22 +00001793static int ipmr_find_vif(struct mr_table *mrt, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794{
1795 int ct;
Patrick McHardy0c122952010-04-13 05:03:22 +00001796
1797 for (ct = mrt->maxvif-1; ct >= 0; ct--) {
1798 if (mrt->vif_table[ct].dev == dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799 break;
1800 }
1801 return ct;
1802}
1803
1804/* "local" means that we should preserve one skb (for local delivery) */
1805
Rami Rosenc4854ec2013-07-20 15:09:28 +03001806static void ip_mr_forward(struct net *net, struct mr_table *mrt,
1807 struct sk_buff *skb, struct mfc_cache *cache,
1808 int local)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
1810 int psend = -1;
1811 int vif, ct;
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001812 int true_vifi = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813
1814 vif = cache->mfc_parent;
1815 cache->mfc_un.res.pkt++;
1816 cache->mfc_un.res.bytes += skb->len;
1817
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001818 if (cache->mfc_origin == htonl(INADDR_ANY) && true_vifi >= 0) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001819 struct mfc_cache *cache_proxy;
1820
1821 /* For an (*,G) entry, we only check that the incomming
1822 * interface is part of the static tree.
1823 */
1824 cache_proxy = ipmr_cache_find_any_parent(mrt, vif);
1825 if (cache_proxy &&
1826 cache_proxy->mfc_un.res.ttls[true_vifi] < 255)
1827 goto forward;
1828 }
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 /*
1831 * Wrong interface: drop packet and (maybe) send PIM assert.
1832 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001833 if (mrt->vif_table[vif].dev != skb->dev) {
David S. Millerc7537962010-11-11 17:07:48 -08001834 if (rt_is_output_route(skb_rtable(skb))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 /* It is our own packet, looped back.
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001836 * Very complicated situation...
1837 *
1838 * The best workaround until routing daemons will be
1839 * fixed is not to redistribute packet, if it was
1840 * send through wrong interface. It means, that
1841 * multicast applications WILL NOT work for
1842 * (S,G), which have default multicast route pointing
1843 * to wrong oif. In any case, it is not a good
1844 * idea to use multicasting applications on router.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 */
1846 goto dont_forward;
1847 }
1848
1849 cache->mfc_un.res.wrong_if++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850
Patrick McHardy0c122952010-04-13 05:03:22 +00001851 if (true_vifi >= 0 && mrt->mroute_do_assert &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 /* pimsm uses asserts, when switching from RPT to SPT,
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001853 * so that we cannot check that packet arrived on an oif.
1854 * It is bad, but otherwise we would need to move pretty
1855 * large chunk of pimd to kernel. Ough... --ANK
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 */
Patrick McHardy0c122952010-04-13 05:03:22 +00001857 (mrt->mroute_do_pim ||
Benjamin Thery6f9374a2009-01-22 04:56:20 +00001858 cache->mfc_un.res.ttls[true_vifi] < 255) &&
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09001859 time_after(jiffies,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 cache->mfc_un.res.last_assert + MFC_ASSERT_THRESH)) {
1861 cache->mfc_un.res.last_assert = jiffies;
Patrick McHardy0c122952010-04-13 05:03:22 +00001862 ipmr_cache_report(mrt, skb, true_vifi, IGMPMSG_WRONGVIF);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 }
1864 goto dont_forward;
1865 }
1866
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001867forward:
Patrick McHardy0c122952010-04-13 05:03:22 +00001868 mrt->vif_table[vif].pkt_in++;
1869 mrt->vif_table[vif].bytes_in += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870
1871 /*
1872 * Forward the frame
1873 */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001874 if (cache->mfc_origin == htonl(INADDR_ANY) &&
1875 cache->mfc_mcastgrp == htonl(INADDR_ANY)) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001876 if (true_vifi >= 0 &&
1877 true_vifi != cache->mfc_parent &&
1878 ip_hdr(skb)->ttl >
1879 cache->mfc_un.res.ttls[cache->mfc_parent]) {
1880 /* It's an (*,*) entry and the packet is not coming from
1881 * the upstream: forward the packet to the upstream
1882 * only.
1883 */
1884 psend = cache->mfc_parent;
1885 goto last_forward;
1886 }
1887 goto dont_forward;
1888 }
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001889 for (ct = cache->mfc_un.res.maxvif - 1;
1890 ct >= cache->mfc_un.res.minvif; ct--) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001891 /* For (*,G) entry, don't forward to the incoming interface */
Nicolas Dichtel360eb5d2013-01-22 11:18:03 +01001892 if ((cache->mfc_origin != htonl(INADDR_ANY) ||
1893 ct != true_vifi) &&
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001894 ip_hdr(skb)->ttl > cache->mfc_un.res.ttls[ct]) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 if (psend != -1) {
1896 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001897
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001899 ipmr_queue_xmit(net, mrt, skb2, cache,
1900 psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
Jianjun Kongc354e122008-11-03 00:28:02 -08001902 psend = ct;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 }
1904 }
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001905last_forward:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 if (psend != -1) {
1907 if (local) {
1908 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 if (skb2)
Patrick McHardy0c122952010-04-13 05:03:22 +00001911 ipmr_queue_xmit(net, mrt, skb2, cache, psend);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 } else {
Patrick McHardy0c122952010-04-13 05:03:22 +00001913 ipmr_queue_xmit(net, mrt, skb, cache, psend);
Rami Rosenc4854ec2013-07-20 15:09:28 +03001914 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 }
1916 }
1917
1918dont_forward:
1919 if (!local)
1920 kfree_skb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921}
1922
David S. Miller417da662011-05-03 19:42:43 -07001923static struct mr_table *ipmr_rt_fib_lookup(struct net *net, struct sk_buff *skb)
David S. Milleree3f1aa2011-03-09 14:06:20 -08001924{
David S. Miller417da662011-05-03 19:42:43 -07001925 struct rtable *rt = skb_rtable(skb);
1926 struct iphdr *iph = ip_hdr(skb);
David S. Millerda919812011-03-12 02:04:50 -05001927 struct flowi4 fl4 = {
David S. Miller417da662011-05-03 19:42:43 -07001928 .daddr = iph->daddr,
1929 .saddr = iph->saddr,
Julian Anastasovb0fe4a32011-07-23 02:00:41 +00001930 .flowi4_tos = RT_TOS(iph->tos),
David S. Miller4fd551d2012-07-17 14:39:44 -07001931 .flowi4_oif = (rt_is_output_route(rt) ?
1932 skb->dev->ifindex : 0),
1933 .flowi4_iif = (rt_is_output_route(rt) ?
Pavel Emelyanov1fb94892012-08-08 21:53:36 +00001934 LOOPBACK_IFINDEX :
David S. Miller4fd551d2012-07-17 14:39:44 -07001935 skb->dev->ifindex),
David Millerb4869882012-07-01 02:03:01 +00001936 .flowi4_mark = skb->mark,
David S. Milleree3f1aa2011-03-09 14:06:20 -08001937 };
1938 struct mr_table *mrt;
1939 int err;
1940
David S. Millerda919812011-03-12 02:04:50 -05001941 err = ipmr_fib_lookup(net, &fl4, &mrt);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001942 if (err)
1943 return ERR_PTR(err);
1944 return mrt;
1945}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946
1947/*
1948 * Multicast packets for forwarding arrive here
Eric Dumazet4c968702010-10-01 16:15:01 +00001949 * Called with rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 */
1951
1952int ip_mr_input(struct sk_buff *skb)
1953{
1954 struct mfc_cache *cache;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00001955 struct net *net = dev_net(skb->dev);
Eric Dumazet511c3f92009-06-02 05:14:27 +00001956 int local = skb_rtable(skb)->rt_flags & RTCF_LOCAL;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00001957 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958
1959 /* Packet is looped back after forward, it should not be
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00001960 * forwarded second time, but still can be delivered locally.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 */
Eric Dumazet4c968702010-10-01 16:15:01 +00001962 if (IPCB(skb)->flags & IPSKB_FORWARDED)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 goto dont_forward;
1964
David S. Miller417da662011-05-03 19:42:43 -07001965 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08001966 if (IS_ERR(mrt)) {
1967 kfree_skb(skb);
1968 return PTR_ERR(mrt);
Ben Greeare40dbc52010-07-15 13:22:33 +00001969 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 if (!local) {
Eric Dumazet4c968702010-10-01 16:15:01 +00001971 if (IPCB(skb)->opt.router_alert) {
1972 if (ip_call_ra_chain(skb))
1973 return 0;
1974 } else if (ip_hdr(skb)->protocol == IPPROTO_IGMP) {
1975 /* IGMPv1 (and broken IGMPv2 implementations sort of
1976 * Cisco IOS <= 11.2(8)) do not put router alert
1977 * option to IGMP packets destined to routable
1978 * groups. It is very bad, because it means
1979 * that we can forward NO IGMP messages.
1980 */
1981 struct sock *mroute_sk;
1982
1983 mroute_sk = rcu_dereference(mrt->mroute_sk);
1984 if (mroute_sk) {
1985 nf_reset(skb);
1986 raw_rcv(mroute_sk, skb);
1987 return 0;
1988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 }
1990 }
1991
Eric Dumazeta8c94862010-10-01 16:15:08 +00001992 /* already under rcu_read_lock() */
Patrick McHardy0c122952010-04-13 05:03:22 +00001993 cache = ipmr_cache_find(mrt, ip_hdr(skb)->saddr, ip_hdr(skb)->daddr);
Ian Morris51456b22015-04-03 09:17:26 +01001994 if (!cache) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00001995 int vif = ipmr_find_vif(mrt, skb->dev);
1996
1997 if (vif >= 0)
1998 cache = ipmr_cache_find_any(mrt, ip_hdr(skb)->daddr,
1999 vif);
2000 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 /*
2003 * No usable cache entry
2004 */
Ian Morris51456b22015-04-03 09:17:26 +01002005 if (!cache) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 int vif;
2007
2008 if (local) {
2009 struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
2010 ip_local_deliver(skb);
Ian Morris51456b22015-04-03 09:17:26 +01002011 if (!skb2)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 return -ENOBUFS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 skb = skb2;
2014 }
2015
Eric Dumazeta8c94862010-10-01 16:15:08 +00002016 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002017 vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002018 if (vif >= 0) {
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002019 int err2 = ipmr_cache_unresolved(mrt, vif, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 read_unlock(&mrt_lock);
2021
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002022 return err2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 }
2024 read_unlock(&mrt_lock);
2025 kfree_skb(skb);
2026 return -ENODEV;
2027 }
2028
Eric Dumazeta8c94862010-10-01 16:15:08 +00002029 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002030 ip_mr_forward(net, mrt, skb, cache, local);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 read_unlock(&mrt_lock);
2032
2033 if (local)
2034 return ip_local_deliver(skb);
2035
2036 return 0;
2037
2038dont_forward:
2039 if (local)
2040 return ip_local_deliver(skb);
2041 kfree_skb(skb);
2042 return 0;
2043}
2044
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002045#ifdef CONFIG_IP_PIMSM
Eric Dumazet55747a02010-10-01 16:14:55 +00002046/* called with rcu_read_lock() */
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002047static int __pim_rcv(struct mr_table *mrt, struct sk_buff *skb,
2048 unsigned int pimlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049{
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002050 struct net_device *reg_dev = NULL;
2051 struct iphdr *encap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002053 encap = (struct iphdr *)(skb_transport_header(skb) + pimlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 /*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002055 * Check that:
2056 * a. packet is really sent to a multicast group
2057 * b. packet is not a NULL-REGISTER
2058 * c. packet is not truncated
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 */
Joe Perchesf97c1e02007-12-16 13:45:43 -08002060 if (!ipv4_is_multicast(encap->daddr) ||
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 encap->tot_len == 0 ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002062 ntohs(encap->tot_len) + pimlen > skb->len)
2063 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064
2065 read_lock(&mrt_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002066 if (mrt->mroute_reg_vif_num >= 0)
2067 reg_dev = mrt->vif_table[mrt->mroute_reg_vif_num].dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 read_unlock(&mrt_lock);
2069
Ian Morris51456b22015-04-03 09:17:26 +01002070 if (!reg_dev)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002071 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -07002073 skb->mac_header = skb->network_header;
Eric Dumazet55747a02010-10-01 16:14:55 +00002074 skb_pull(skb, (u8 *)encap - skb->data);
Arnaldo Carvalho de Melo31c77112007-03-10 19:04:55 -03002075 skb_reset_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 skb->protocol = htons(ETH_P_IP);
Eric Dumazet55747a02010-10-01 16:14:55 +00002077 skb->ip_summed = CHECKSUM_NONE;
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002078
Nicolas Dichtelea231922013-09-02 15:34:58 +02002079 skb_tunnel_rx(skb, reg_dev, dev_net(reg_dev));
Eric Dumazetd19d56d2010-05-17 22:36:55 -07002080
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 netif_rx(skb);
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002082
Eric Dumazet55747a02010-10-01 16:14:55 +00002083 return NET_RX_SUCCESS;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002084}
2085#endif
2086
2087#ifdef CONFIG_IP_PIMSM_V1
2088/*
2089 * Handle IGMP messages of PIMv1
2090 */
2091
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002092int pim_rcv_v1(struct sk_buff *skb)
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002093{
2094 struct igmphdr *pim;
Benjamin Thery4feb88e2009-01-22 04:56:23 +00002095 struct net *net = dev_net(skb->dev);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002096 struct mr_table *mrt;
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002097
2098 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
2099 goto drop;
2100
2101 pim = igmp_hdr(skb);
2102
David S. Miller417da662011-05-03 19:42:43 -07002103 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002104 if (IS_ERR(mrt))
2105 goto drop;
Patrick McHardy0c122952010-04-13 05:03:22 +00002106 if (!mrt->mroute_do_pim ||
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002107 pim->group != PIM_V1_VERSION || pim->code != PIM_V1_REGISTER)
2108 goto drop;
2109
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002110 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002111drop:
2112 kfree_skb(skb);
2113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return 0;
2115}
2116#endif
2117
2118#ifdef CONFIG_IP_PIMSM_V2
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002119static int pim_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 struct pimreghdr *pim;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002122 struct net *net = dev_net(skb->dev);
2123 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002125 if (!pskb_may_pull(skb, sizeof(*pim) + sizeof(struct iphdr)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 goto drop;
2127
Arnaldo Carvalho de Melo9c702202007-04-25 18:04:18 -07002128 pim = (struct pimreghdr *)skb_transport_header(skb);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002129 if (pim->type != ((PIM_VERSION << 4) | (PIM_REGISTER)) ||
2130 (pim->flags & PIM_NULL_REGISTER) ||
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002131 (ip_compute_csum((void *)pim, sizeof(*pim)) != 0 &&
Al Virod3bc23e2006-11-14 21:24:49 -08002132 csum_fold(skb_checksum(skb, 0, skb->len, 0))))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 goto drop;
2134
David S. Miller417da662011-05-03 19:42:43 -07002135 mrt = ipmr_rt_fib_lookup(net, skb);
David S. Milleree3f1aa2011-03-09 14:06:20 -08002136 if (IS_ERR(mrt))
2137 goto drop;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002138 if (__pim_rcv(mrt, skb, sizeof(*pim))) {
Ilpo Järvinenb1879202008-12-16 01:15:11 -08002139drop:
2140 kfree_skb(skb);
2141 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return 0;
2143}
2144#endif
2145
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002146static int __ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
2147 struct mfc_cache *c, struct rtmsg *rtm)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148{
2149 int ct;
2150 struct rtnexthop *nhp;
Thomas Graf92a395e2012-06-26 23:36:13 +00002151 struct nlattr *mp_attr;
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002152 struct rta_mfc_stats mfcs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002153
Nicolas Dichtel74381892010-03-25 23:45:35 +00002154 /* If cache is unresolved, don't try to parse IIF and OIF */
Dan Carpentered0f160a2010-05-26 00:38:56 -07002155 if (c->mfc_parent >= MAXVIFS)
Nicolas Dichtel74381892010-03-25 23:45:35 +00002156 return -ENOENT;
2157
Thomas Graf92a395e2012-06-26 23:36:13 +00002158 if (VIF_EXISTS(mrt, c->mfc_parent) &&
2159 nla_put_u32(skb, RTA_IIF, mrt->vif_table[c->mfc_parent].dev->ifindex) < 0)
2160 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002161
Thomas Graf92a395e2012-06-26 23:36:13 +00002162 if (!(mp_attr = nla_nest_start(skb, RTA_MULTIPATH)))
2163 return -EMSGSIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002164
2165 for (ct = c->mfc_un.res.minvif; ct < c->mfc_un.res.maxvif; ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002166 if (VIF_EXISTS(mrt, ct) && c->mfc_un.res.ttls[ct] < 255) {
Thomas Graf92a395e2012-06-26 23:36:13 +00002167 if (!(nhp = nla_reserve_nohdr(skb, sizeof(*nhp)))) {
2168 nla_nest_cancel(skb, mp_attr);
2169 return -EMSGSIZE;
2170 }
2171
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 nhp->rtnh_flags = 0;
2173 nhp->rtnh_hops = c->mfc_un.res.ttls[ct];
Patrick McHardy0c122952010-04-13 05:03:22 +00002174 nhp->rtnh_ifindex = mrt->vif_table[ct].dev->ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175 nhp->rtnh_len = sizeof(*nhp);
2176 }
2177 }
Thomas Graf92a395e2012-06-26 23:36:13 +00002178
2179 nla_nest_end(skb, mp_attr);
2180
Nicolas Dichteladfa85e2012-12-04 01:13:37 +00002181 mfcs.mfcs_packets = c->mfc_un.res.pkt;
2182 mfcs.mfcs_bytes = c->mfc_un.res.bytes;
2183 mfcs.mfcs_wrong_if = c->mfc_un.res.wrong_if;
2184 if (nla_put(skb, RTA_MFC_STATS, sizeof(mfcs), &mfcs) < 0)
2185 return -EMSGSIZE;
2186
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187 rtm->rtm_type = RTN_MULTICAST;
2188 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189}
2190
David S. Miller9a1b9492011-05-04 12:18:54 -07002191int ipmr_get_route(struct net *net, struct sk_buff *skb,
2192 __be32 saddr, __be32 daddr,
2193 struct rtmsg *rtm, int nowait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 struct mfc_cache *cache;
David S. Miller9a1b9492011-05-04 12:18:54 -07002196 struct mr_table *mrt;
2197 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002199 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002200 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002201 return -ENOENT;
2202
Eric Dumazeta8c94862010-10-01 16:15:08 +00002203 rcu_read_lock();
David S. Miller9a1b9492011-05-04 12:18:54 -07002204 cache = ipmr_cache_find(mrt, saddr, daddr);
Ian Morris51456b22015-04-03 09:17:26 +01002205 if (!cache && skb->dev) {
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002206 int vif = ipmr_find_vif(mrt, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207
Nicolas Dichtel660b26d2013-01-21 06:00:26 +00002208 if (vif >= 0)
2209 cache = ipmr_cache_find_any(mrt, daddr, vif);
2210 }
Ian Morris51456b22015-04-03 09:17:26 +01002211 if (!cache) {
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002212 struct sk_buff *skb2;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002213 struct iphdr *iph;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 struct net_device *dev;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002215 int vif = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216
2217 if (nowait) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002218 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002219 return -EAGAIN;
2220 }
2221
2222 dev = skb->dev;
Eric Dumazeta8c94862010-10-01 16:15:08 +00002223 read_lock(&mrt_lock);
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002224 if (dev)
2225 vif = ipmr_find_vif(mrt, dev);
2226 if (vif < 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002228 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return -ENODEV;
2230 }
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002231 skb2 = skb_clone(skb, GFP_ATOMIC);
2232 if (!skb2) {
2233 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002234 rcu_read_unlock();
Alexey Kuznetsov72287492006-07-25 16:45:12 -07002235 return -ENOMEM;
2236 }
2237
Arnaldo Carvalho de Meloe2d1bca2007-04-10 20:46:21 -07002238 skb_push(skb2, sizeof(struct iphdr));
2239 skb_reset_network_header(skb2);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002240 iph = ip_hdr(skb2);
2241 iph->ihl = sizeof(struct iphdr) >> 2;
David S. Miller9a1b9492011-05-04 12:18:54 -07002242 iph->saddr = saddr;
2243 iph->daddr = daddr;
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002244 iph->version = 0;
Patrick McHardy0c122952010-04-13 05:03:22 +00002245 err = ipmr_cache_unresolved(mrt, vif, skb2);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002247 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return err;
2249 }
2250
Eric Dumazeta8c94862010-10-01 16:15:08 +00002251 read_lock(&mrt_lock);
2252 if (!nowait && (rtm->rtm_flags & RTM_F_NOTIFY))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002253 cache->mfc_flags |= MFC_NOTIFY;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002254 err = __ipmr_fill_mroute(mrt, skb, cache, rtm);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 read_unlock(&mrt_lock);
Eric Dumazeta8c94862010-10-01 16:15:08 +00002256 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return err;
2258}
2259
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002260static int ipmr_fill_mroute(struct mr_table *mrt, struct sk_buff *skb,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002261 u32 portid, u32 seq, struct mfc_cache *c, int cmd,
2262 int flags)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002263{
2264 struct nlmsghdr *nlh;
2265 struct rtmsg *rtm;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002266 int err;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002267
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002268 nlh = nlmsg_put(skb, portid, seq, cmd, sizeof(*rtm), flags);
Ian Morris51456b22015-04-03 09:17:26 +01002269 if (!nlh)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002270 return -EMSGSIZE;
2271
2272 rtm = nlmsg_data(nlh);
2273 rtm->rtm_family = RTNL_FAMILY_IPMR;
2274 rtm->rtm_dst_len = 32;
2275 rtm->rtm_src_len = 32;
2276 rtm->rtm_tos = 0;
2277 rtm->rtm_table = mrt->id;
David S. Millerf3756b72012-04-01 20:39:02 -04002278 if (nla_put_u32(skb, RTA_TABLE, mrt->id))
2279 goto nla_put_failure;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002280 rtm->rtm_type = RTN_MULTICAST;
2281 rtm->rtm_scope = RT_SCOPE_UNIVERSE;
Nicolas Dichtel9a68ac72012-12-04 01:13:38 +00002282 if (c->mfc_flags & MFC_STATIC)
2283 rtm->rtm_protocol = RTPROT_STATIC;
2284 else
2285 rtm->rtm_protocol = RTPROT_MROUTED;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002286 rtm->rtm_flags = 0;
2287
Jiri Benc930345e2015-03-29 16:59:25 +02002288 if (nla_put_in_addr(skb, RTA_SRC, c->mfc_origin) ||
2289 nla_put_in_addr(skb, RTA_DST, c->mfc_mcastgrp))
David S. Millerf3756b72012-04-01 20:39:02 -04002290 goto nla_put_failure;
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002291 err = __ipmr_fill_mroute(mrt, skb, c, rtm);
2292 /* do not break the dump if cache is unresolved */
2293 if (err < 0 && err != -ENOENT)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002294 goto nla_put_failure;
2295
Johannes Berg053c0952015-01-16 22:09:00 +01002296 nlmsg_end(skb, nlh);
2297 return 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002298
2299nla_put_failure:
2300 nlmsg_cancel(skb, nlh);
2301 return -EMSGSIZE;
2302}
2303
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002304static size_t mroute_msgsize(bool unresolved, int maxvif)
2305{
2306 size_t len =
2307 NLMSG_ALIGN(sizeof(struct rtmsg))
2308 + nla_total_size(4) /* RTA_TABLE */
2309 + nla_total_size(4) /* RTA_SRC */
2310 + nla_total_size(4) /* RTA_DST */
2311 ;
2312
2313 if (!unresolved)
2314 len = len
2315 + nla_total_size(4) /* RTA_IIF */
2316 + nla_total_size(0) /* RTA_MULTIPATH */
2317 + maxvif * NLA_ALIGN(sizeof(struct rtnexthop))
2318 /* RTA_MFC_STATS */
2319 + nla_total_size(sizeof(struct rta_mfc_stats))
2320 ;
2321
2322 return len;
2323}
2324
2325static void mroute_netlink_event(struct mr_table *mrt, struct mfc_cache *mfc,
2326 int cmd)
2327{
2328 struct net *net = read_pnet(&mrt->net);
2329 struct sk_buff *skb;
2330 int err = -ENOBUFS;
2331
2332 skb = nlmsg_new(mroute_msgsize(mfc->mfc_parent >= MAXVIFS, mrt->maxvif),
2333 GFP_ATOMIC);
Ian Morris51456b22015-04-03 09:17:26 +01002334 if (!skb)
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002335 goto errout;
2336
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002337 err = ipmr_fill_mroute(mrt, skb, 0, 0, mfc, cmd, 0);
Nicolas Dichtel8cd3ac92012-12-04 01:13:40 +00002338 if (err < 0)
2339 goto errout;
2340
2341 rtnl_notify(skb, net, 0, RTNLGRP_IPV4_MROUTE, NULL, GFP_ATOMIC);
2342 return;
2343
2344errout:
2345 kfree_skb(skb);
2346 if (err < 0)
2347 rtnl_set_sk_err(net, RTNLGRP_IPV4_MROUTE, err);
2348}
2349
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002350static int ipmr_rtm_dumproute(struct sk_buff *skb, struct netlink_callback *cb)
2351{
2352 struct net *net = sock_net(skb->sk);
2353 struct mr_table *mrt;
2354 struct mfc_cache *mfc;
2355 unsigned int t = 0, s_t;
2356 unsigned int h = 0, s_h;
2357 unsigned int e = 0, s_e;
2358
2359 s_t = cb->args[0];
2360 s_h = cb->args[1];
2361 s_e = cb->args[2];
2362
Eric Dumazeta8c94862010-10-01 16:15:08 +00002363 rcu_read_lock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002364 ipmr_for_each_table(mrt, net) {
2365 if (t < s_t)
2366 goto next_table;
2367 if (t > s_t)
2368 s_h = 0;
2369 for (h = s_h; h < MFC_LINES; h++) {
Eric Dumazeta8c94862010-10-01 16:15:08 +00002370 list_for_each_entry_rcu(mfc, &mrt->mfc_cache_array[h], list) {
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002371 if (e < s_e)
2372 goto next_entry;
2373 if (ipmr_fill_mroute(mrt, skb,
Eric W. Biederman15e47302012-09-07 20:12:54 +00002374 NETLINK_CB(cb->skb).portid,
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002375 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002376 mfc, RTM_NEWROUTE,
2377 NLM_F_MULTI) < 0)
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002378 goto done;
2379next_entry:
2380 e++;
2381 }
2382 e = s_e = 0;
2383 }
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002384 spin_lock_bh(&mfc_unres_lock);
2385 list_for_each_entry(mfc, &mrt->mfc_unres_queue, list) {
2386 if (e < s_e)
2387 goto next_entry2;
2388 if (ipmr_fill_mroute(mrt, skb,
2389 NETLINK_CB(cb->skb).portid,
2390 cb->nlh->nlmsg_seq,
Nicolas Dichtel65886f42014-03-19 17:47:50 +01002391 mfc, RTM_NEWROUTE,
2392 NLM_F_MULTI) < 0) {
Nicolas Dichtel1eb99af2012-12-04 01:13:39 +00002393 spin_unlock_bh(&mfc_unres_lock);
2394 goto done;
2395 }
2396next_entry2:
2397 e++;
2398 }
2399 spin_unlock_bh(&mfc_unres_lock);
2400 e = s_e = 0;
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002401 s_h = 0;
2402next_table:
2403 t++;
2404 }
2405done:
Eric Dumazeta8c94862010-10-01 16:15:08 +00002406 rcu_read_unlock();
Patrick McHardycb6a4e42010-04-26 16:02:08 +02002407
2408 cb->args[2] = e;
2409 cb->args[1] = h;
2410 cb->args[0] = t;
2411
2412 return skb->len;
2413}
2414
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002415#ifdef CONFIG_PROC_FS
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416/*
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002417 * The /proc interfaces to multicast routing :
2418 * /proc/net/ip_mr_cache & /proc/net/ip_mr_vif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419 */
2420struct ipmr_vif_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002421 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002422 struct mr_table *mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 int ct;
2424};
2425
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002426static struct vif_device *ipmr_vif_seq_idx(struct net *net,
2427 struct ipmr_vif_iter *iter,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428 loff_t pos)
2429{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002430 struct mr_table *mrt = iter->mrt;
Patrick McHardy0c122952010-04-13 05:03:22 +00002431
2432 for (iter->ct = 0; iter->ct < mrt->maxvif; ++iter->ct) {
2433 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002434 continue;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002435 if (pos-- == 0)
Patrick McHardy0c122952010-04-13 05:03:22 +00002436 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002437 }
2438 return NULL;
2439}
2440
2441static void *ipmr_vif_seq_start(struct seq_file *seq, loff_t *pos)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002442 __acquires(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002444 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002445 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002446 struct mr_table *mrt;
2447
2448 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002449 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002450 return ERR_PTR(-ENOENT);
2451
2452 iter->mrt = mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002453
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 read_lock(&mrt_lock);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002455 return *pos ? ipmr_vif_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456 : SEQ_START_TOKEN;
2457}
2458
2459static void *ipmr_vif_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2460{
2461 struct ipmr_vif_iter *iter = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002462 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002463 struct mr_table *mrt = iter->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464
2465 ++*pos;
2466 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002467 return ipmr_vif_seq_idx(net, iter, 0);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002468
Patrick McHardy0c122952010-04-13 05:03:22 +00002469 while (++iter->ct < mrt->maxvif) {
2470 if (!VIF_EXISTS(mrt, iter->ct))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 continue;
Patrick McHardy0c122952010-04-13 05:03:22 +00002472 return &mrt->vif_table[iter->ct];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
2474 return NULL;
2475}
2476
2477static void ipmr_vif_seq_stop(struct seq_file *seq, void *v)
Stephen Hemmingerba93ef72008-01-21 17:28:59 -08002478 __releases(mrt_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479{
2480 read_unlock(&mrt_lock);
2481}
2482
2483static int ipmr_vif_seq_show(struct seq_file *seq, void *v)
2484{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002485 struct ipmr_vif_iter *iter = seq->private;
2486 struct mr_table *mrt = iter->mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002489 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490 "Interface BytesIn PktsIn BytesOut PktsOut Flags Local Remote\n");
2491 } else {
2492 const struct vif_device *vif = v;
2493 const char *name = vif->dev ? vif->dev->name : "none";
2494
2495 seq_printf(seq,
2496 "%2Zd %-10s %8ld %7ld %8ld %7ld %05X %08X %08X\n",
Patrick McHardy0c122952010-04-13 05:03:22 +00002497 vif - mrt->vif_table,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002498 name, vif->bytes_in, vif->pkt_in,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 vif->bytes_out, vif->pkt_out,
2500 vif->flags, vif->local, vif->remote);
2501 }
2502 return 0;
2503}
2504
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002505static const struct seq_operations ipmr_vif_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002506 .start = ipmr_vif_seq_start,
2507 .next = ipmr_vif_seq_next,
2508 .stop = ipmr_vif_seq_stop,
2509 .show = ipmr_vif_seq_show,
2510};
2511
2512static int ipmr_vif_open(struct inode *inode, struct file *file)
2513{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002514 return seq_open_net(inode, file, &ipmr_vif_seq_ops,
2515 sizeof(struct ipmr_vif_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516}
2517
Arjan van de Ven9a321442007-02-12 00:55:35 -08002518static const struct file_operations ipmr_vif_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 .owner = THIS_MODULE,
2520 .open = ipmr_vif_open,
2521 .read = seq_read,
2522 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002523 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524};
2525
2526struct ipmr_mfc_iter {
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002527 struct seq_net_private p;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002528 struct mr_table *mrt;
Patrick McHardy862465f2010-04-13 05:03:21 +00002529 struct list_head *cache;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530 int ct;
2531};
2532
2533
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002534static struct mfc_cache *ipmr_mfc_seq_idx(struct net *net,
2535 struct ipmr_mfc_iter *it, loff_t pos)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002537 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538 struct mfc_cache *mfc;
2539
Eric Dumazeta8c94862010-10-01 16:15:08 +00002540 rcu_read_lock();
Patrick McHardy862465f2010-04-13 05:03:21 +00002541 for (it->ct = 0; it->ct < MFC_LINES; it->ct++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002542 it->cache = &mrt->mfc_cache_array[it->ct];
Eric Dumazeta8c94862010-10-01 16:15:08 +00002543 list_for_each_entry_rcu(mfc, it->cache, list)
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002544 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 return mfc;
Patrick McHardy862465f2010-04-13 05:03:21 +00002546 }
Eric Dumazeta8c94862010-10-01 16:15:08 +00002547 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002548
Linus Torvalds1da177e2005-04-16 15:20:36 -07002549 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002550 it->cache = &mrt->mfc_unres_queue;
Patrick McHardy862465f2010-04-13 05:03:21 +00002551 list_for_each_entry(mfc, it->cache, list)
Patrick McHardye258beb2010-04-13 05:03:19 +00002552 if (pos-- == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002553 return mfc;
2554 spin_unlock_bh(&mfc_unres_lock);
2555
2556 it->cache = NULL;
2557 return NULL;
2558}
2559
2560
2561static void *ipmr_mfc_seq_start(struct seq_file *seq, loff_t *pos)
2562{
2563 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002564 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002565 struct mr_table *mrt;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002566
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002567 mrt = ipmr_get_table(net, RT_TABLE_DEFAULT);
Ian Morris51456b22015-04-03 09:17:26 +01002568 if (!mrt)
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002569 return ERR_PTR(-ENOENT);
2570
2571 it->mrt = mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 it->cache = NULL;
2573 it->ct = 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002574 return *pos ? ipmr_mfc_seq_idx(net, seq->private, *pos - 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 : SEQ_START_TOKEN;
2576}
2577
2578static void *ipmr_mfc_seq_next(struct seq_file *seq, void *v, loff_t *pos)
2579{
2580 struct mfc_cache *mfc = v;
2581 struct ipmr_mfc_iter *it = seq->private;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002582 struct net *net = seq_file_net(seq);
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002583 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584
2585 ++*pos;
2586
2587 if (v == SEQ_START_TOKEN)
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002588 return ipmr_mfc_seq_idx(net, seq->private, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
Patrick McHardy862465f2010-04-13 05:03:21 +00002590 if (mfc->list.next != it->cache)
2591 return list_entry(mfc->list.next, struct mfc_cache, list);
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002592
Patrick McHardy0c122952010-04-13 05:03:22 +00002593 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594 goto end_of_list;
2595
Patrick McHardy0c122952010-04-13 05:03:22 +00002596 BUG_ON(it->cache != &mrt->mfc_cache_array[it->ct]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002597
2598 while (++it->ct < MFC_LINES) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002599 it->cache = &mrt->mfc_cache_array[it->ct];
Patrick McHardy862465f2010-04-13 05:03:21 +00002600 if (list_empty(it->cache))
2601 continue;
2602 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 }
2604
2605 /* exhausted cache_array, show unresolved */
Eric Dumazeta8c94862010-10-01 16:15:08 +00002606 rcu_read_unlock();
Patrick McHardy0c122952010-04-13 05:03:22 +00002607 it->cache = &mrt->mfc_unres_queue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608 it->ct = 0;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 spin_lock_bh(&mfc_unres_lock);
Patrick McHardy862465f2010-04-13 05:03:21 +00002611 if (!list_empty(it->cache))
2612 return list_first_entry(it->cache, struct mfc_cache, list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002614end_of_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 spin_unlock_bh(&mfc_unres_lock);
2616 it->cache = NULL;
2617
2618 return NULL;
2619}
2620
2621static void ipmr_mfc_seq_stop(struct seq_file *seq, void *v)
2622{
2623 struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002624 struct mr_table *mrt = it->mrt;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Patrick McHardy0c122952010-04-13 05:03:22 +00002626 if (it->cache == &mrt->mfc_unres_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 spin_unlock_bh(&mfc_unres_lock);
Patrick McHardy0c122952010-04-13 05:03:22 +00002628 else if (it->cache == &mrt->mfc_cache_array[it->ct])
Eric Dumazeta8c94862010-10-01 16:15:08 +00002629 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630}
2631
2632static int ipmr_mfc_seq_show(struct seq_file *seq, void *v)
2633{
2634 int n;
2635
2636 if (v == SEQ_START_TOKEN) {
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002637 seq_puts(seq,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638 "Group Origin Iif Pkts Bytes Wrong Oifs\n");
2639 } else {
2640 const struct mfc_cache *mfc = v;
2641 const struct ipmr_mfc_iter *it = seq->private;
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002642 const struct mr_table *mrt = it->mrt;
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002643
Eric Dumazet0eae88f2010-04-20 19:06:52 -07002644 seq_printf(seq, "%08X %08X %-3hd",
2645 (__force u32) mfc->mfc_mcastgrp,
2646 (__force u32) mfc->mfc_origin,
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002647 mfc->mfc_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648
Patrick McHardy0c122952010-04-13 05:03:22 +00002649 if (it->cache != &mrt->mfc_unres_queue) {
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002650 seq_printf(seq, " %8lu %8lu %8lu",
2651 mfc->mfc_un.res.pkt,
2652 mfc->mfc_un.res.bytes,
2653 mfc->mfc_un.res.wrong_if);
Stephen Hemminger132adf52007-03-08 20:44:43 -08002654 for (n = mfc->mfc_un.res.minvif;
Eric Dumazeta8cb16d2010-10-01 16:15:29 +00002655 n < mfc->mfc_un.res.maxvif; n++) {
Patrick McHardy0c122952010-04-13 05:03:22 +00002656 if (VIF_EXISTS(mrt, n) &&
Benjamin Therycf958ae32009-01-22 04:56:16 +00002657 mfc->mfc_un.res.ttls[n] < 255)
2658 seq_printf(seq,
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002659 " %2d:%-3d",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 n, mfc->mfc_un.res.ttls[n]);
2661 }
Benjamin Thery1ea472e2008-12-03 22:21:47 -08002662 } else {
2663 /* unresolved mfc_caches don't contain
2664 * pkt, bytes and wrong_if values
2665 */
2666 seq_printf(seq, " %8lu %8lu %8lu", 0ul, 0ul, 0ul);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 }
2668 seq_putc(seq, '\n');
2669 }
2670 return 0;
2671}
2672
Stephen Hemmingerf6908082007-03-12 14:34:29 -07002673static const struct seq_operations ipmr_mfc_seq_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 .start = ipmr_mfc_seq_start,
2675 .next = ipmr_mfc_seq_next,
2676 .stop = ipmr_mfc_seq_stop,
2677 .show = ipmr_mfc_seq_show,
2678};
2679
2680static int ipmr_mfc_open(struct inode *inode, struct file *file)
2681{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002682 return seq_open_net(inode, file, &ipmr_mfc_seq_ops,
2683 sizeof(struct ipmr_mfc_iter));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684}
2685
Arjan van de Ven9a321442007-02-12 00:55:35 -08002686static const struct file_operations ipmr_mfc_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 .owner = THIS_MODULE,
2688 .open = ipmr_mfc_open,
2689 .read = seq_read,
2690 .llseek = seq_lseek,
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002691 .release = seq_release_net,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002692};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002693#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002694
2695#ifdef CONFIG_IP_PIMSM_V2
Alexey Dobriyan32613092009-09-14 12:21:47 +00002696static const struct net_protocol pim_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002697 .handler = pim_rcv,
Tom Goff403dbb92009-06-14 03:16:13 -07002698 .netns_ok = 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699};
2700#endif
2701
2702
2703/*
2704 * Setup for IP multicast routing
2705 */
Benjamin Therycf958ae32009-01-22 04:56:16 +00002706static int __net_init ipmr_net_init(struct net *net)
2707{
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002708 int err;
Benjamin Therycf958ae32009-01-22 04:56:16 +00002709
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002710 err = ipmr_rules_init(net);
2711 if (err < 0)
Benjamin Therycf958ae32009-01-22 04:56:16 +00002712 goto fail;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002713
2714#ifdef CONFIG_PROC_FS
2715 err = -ENOMEM;
Gao fengd4beaa62013-02-18 01:34:54 +00002716 if (!proc_create("ip_mr_vif", 0, net->proc_net, &ipmr_vif_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002717 goto proc_vif_fail;
Gao fengd4beaa62013-02-18 01:34:54 +00002718 if (!proc_create("ip_mr_cache", 0, net->proc_net, &ipmr_mfc_fops))
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002719 goto proc_cache_fail;
2720#endif
Benjamin Thery2bb8b262009-01-22 04:56:18 +00002721 return 0;
2722
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002723#ifdef CONFIG_PROC_FS
2724proc_cache_fail:
Gao fengece31ff2013-02-18 01:34:56 +00002725 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002726proc_vif_fail:
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002727 ipmr_rules_exit(net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002728#endif
Benjamin Therycf958ae32009-01-22 04:56:16 +00002729fail:
2730 return err;
2731}
2732
2733static void __net_exit ipmr_net_exit(struct net *net)
2734{
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002735#ifdef CONFIG_PROC_FS
Gao fengece31ff2013-02-18 01:34:56 +00002736 remove_proc_entry("ip_mr_cache", net->proc_net);
2737 remove_proc_entry("ip_mr_vif", net->proc_net);
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002738#endif
Patrick McHardyf0ad0862010-04-13 05:03:23 +00002739 ipmr_rules_exit(net);
Benjamin Therycf958ae32009-01-22 04:56:16 +00002740}
2741
2742static struct pernet_operations ipmr_net_ops = {
2743 .init = ipmr_net_init,
2744 .exit = ipmr_net_exit,
2745};
YOSHIFUJI Hideakie905a9e2007-02-09 23:24:47 +09002746
Wang Chen03d2f892008-07-03 12:13:36 +08002747int __init ip_mr_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748{
Wang Chen03d2f892008-07-03 12:13:36 +08002749 int err;
2750
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 mrt_cachep = kmem_cache_create("ip_mrt_cache",
2752 sizeof(struct mfc_cache),
Eric Dumazeta8c94862010-10-01 16:15:08 +00002753 0, SLAB_HWCACHE_ALIGN | SLAB_PANIC,
Paul Mundt20c2df82007-07-20 10:11:58 +09002754 NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002755 if (!mrt_cachep)
2756 return -ENOMEM;
2757
Benjamin Therycf958ae32009-01-22 04:56:16 +00002758 err = register_pernet_subsys(&ipmr_net_ops);
2759 if (err)
2760 goto reg_pernet_fail;
2761
Wang Chen03d2f892008-07-03 12:13:36 +08002762 err = register_netdevice_notifier(&ip_mr_notifier);
2763 if (err)
2764 goto reg_notif_fail;
Tom Goff403dbb92009-06-14 03:16:13 -07002765#ifdef CONFIG_IP_PIMSM_V2
2766 if (inet_add_protocol(&pim_protocol, IPPROTO_PIM) < 0) {
Joe Perches058bd4d2012-03-11 18:36:11 +00002767 pr_err("%s: can't add PIM protocol\n", __func__);
Tom Goff403dbb92009-06-14 03:16:13 -07002768 err = -EAGAIN;
2769 goto add_proto_fail;
2770 }
2771#endif
Greg Rosec7ac8672011-06-10 01:27:09 +00002772 rtnl_register(RTNL_FAMILY_IPMR, RTM_GETROUTE,
2773 NULL, ipmr_rtm_dumproute, NULL);
Wang Chen03d2f892008-07-03 12:13:36 +08002774 return 0;
Benjamin Theryf6bb4512009-01-22 04:56:22 +00002775
Tom Goff403dbb92009-06-14 03:16:13 -07002776#ifdef CONFIG_IP_PIMSM_V2
2777add_proto_fail:
2778 unregister_netdevice_notifier(&ip_mr_notifier);
2779#endif
Benjamin Theryc3e38892008-11-19 14:07:41 -08002780reg_notif_fail:
Benjamin Therycf958ae32009-01-22 04:56:16 +00002781 unregister_pernet_subsys(&ipmr_net_ops);
2782reg_pernet_fail:
Benjamin Theryc3e38892008-11-19 14:07:41 -08002783 kmem_cache_destroy(mrt_cachep);
Wang Chen03d2f892008-07-03 12:13:36 +08002784 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785}